From 1da4884f105d0bbf27012213150a50b2c438b8aa Mon Sep 17 00:00:00 2001 From: Xianda Sun Date: Fri, 10 May 2024 14:29:55 +0100 Subject: [PATCH 1/7] update prob query section of the doc --- .../using-turing-guide.jmd | 61 +++++++++---------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd b/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd index 40a2cc600..fdd810399 100644 --- a/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd +++ b/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd @@ -331,66 +331,65 @@ The element type of a vector (or matrix) of random variables should match the `e ### Querying Probabilities from Model or Chain -Consider first the following simplified `gdemo` model: +Turing offers three functions: [`loglikelihood`](https://turinglang.org/DynamicPPL.jl/dev/api/#StatsAPI.loglikelihood), [`logprior`](https://turinglang.org/DynamicPPL.jl/dev/api/#DynamicPPL.logprior), and [`logjoint`](https://turinglang.org/DynamicPPL.jl/dev/api/#DynamicPPL.logjoint) to query the log-likelihood, log-prior, and log-joint probabilities of a model, respectively. + +Let's look at a simple model called `gdemo`: ```julia -@model function gdemo0(x) +@model function gdemo0() s ~ InverseGamma(2, 3) m ~ Normal(0, sqrt(s)) return x ~ Normal(m, sqrt(s)) end - -# Instantiate three models, with different value of x -model1 = gdemo0(1) -model4 = gdemo0(4) -model10 = gdemo0(10) ``` -Now, query the instantiated models: compute the likelihood of `x = 1.0` given the values of `s = 1.0` and `m = 1.0` for the parameters: +If we observe x to be 1.0, we can condition the model on this datum using the [`condition`](https://turinglang.org/DynamicPPL.jl/dev/api/#AbstractPPL.condition) syntax: ```julia -prob"x = 1.0 | model = model1, s = 1.0, m = 1.0" +model = gdemo0() | (x = 1.0,) ``` +Now, let's compute the log-likelihood of the observation given specific values of the model parameters, `s` and `m`: + ```julia -prob"x = 1.0 | model = model4, s = 1.0, m = 1.0" +loglikelihood(model, (s = 1.0, m = 1.0)) ``` +We can easily verify that value in this case: + ```julia -prob"x = 1.0 | model = model10, s = 1.0, m = 1.0" +logpdf(Normal(1.0, 1.0), 1.0) ``` -Notice that even if we use three models, instantiated with three different values of `x`, we should obtain the same likelihood. We can easily verify that value in this case: +We can also compute the log-prior probability of the model for the same values of s and m: ```julia -pdf(Normal(1.0, 1.0), 1.0) +logprior(model, (s = 1.0, m = 1.0)) ``` -Let us now consider the following `gdemo` model: - ```julia -@model function gdemo(x, y) - s² ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s²)) - x ~ Normal(m, sqrt(s²)) - return y ~ Normal(m, sqrt(s²)) -end +logpdf(InverseGamma(2, 3), 1.0) + logpdf(Normal(0, sqrt(1.0)), 1.0) +``` + +Finally, we can compute the log-joint probability of the model parameters and data: -# Instantiate the model. -model = gdemo(2.0, 4.0) +```julia +logjoint(model, (s = 1.0, m = 1.0)) ``` -The following are examples of valid queries of the `Turing` model or chain: +```julia +logpdf(Normal(1.0, 1.0), 1.0) + logpdf(InverseGamma(2, 3), 1.0) + logpdf(Normal(0, sqrt(1.0)), 1.0) +``` - - `prob"x = 1.0, y = 1.0 | model = model, s = 1.0, m = 1.0"` calculates the likelihood of `x = 1` and `y = 1` given `s = 1` and `m = 1`. +Querying with `Chains` object is easy as well: - - `prob"s² = 1.0, m = 1.0 | model = model, x = nothing, y = nothing"` calculates the joint probability of `s = 1` and `m = 1` ignoring `x` and `y`. `x` and `y` are ignored so they can be optionally dropped from the RHS of `|`, but it is recommended to define them. - - `prob"s² = 1.0, m = 1.0, x = 1.0 | model = model, y = nothing"` calculates the joint probability of `s = 1`, `m = 1` and `x = 1` ignoring `y`. - - `prob"s² = 1.0, m = 1.0, x = 1.0, y = 1.0 | model = model"` calculates the joint probability of all the variables. - - After the MCMC sampling, given a `chain`, `prob"x = 1.0, y = 1.0 | chain = chain, model = model"` calculates the element-wise likelihood of `x = 1.0` and `y = 1.0` for each sample in `chain`. - - If `save_state=true` was used during sampling (i.e., `sample(model, sampler, N; save_state=true)`), you can simply do `prob"x = 1.0, y = 1.0 | chain = chain"`. +```julia +chn = sample(model, NUTS(), 10) +``` -In all the above cases, `logprob` can be used instead of `prob` to calculate the log probabilities instead. +```julia +loglikelihood(model, chn) +``` ### Maximum likelihood and maximum a posterior estimates From a316430acf52dc77ced61730c034baa1ebc676c8 Mon Sep 17 00:00:00 2001 From: Xianda Sun <5433119+sunxd3@users.noreply.github.com> Date: Fri, 10 May 2024 14:48:21 +0100 Subject: [PATCH 2/7] Update tutorials/docs-12-using-turing-guide/using-turing-guide.jmd Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- tutorials/docs-12-using-turing-guide/using-turing-guide.jmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd b/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd index fdd810399..5b9d03f93 100644 --- a/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd +++ b/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd @@ -346,7 +346,7 @@ end If we observe x to be 1.0, we can condition the model on this datum using the [`condition`](https://turinglang.org/DynamicPPL.jl/dev/api/#AbstractPPL.condition) syntax: ```julia -model = gdemo0() | (x = 1.0,) +model = gdemo0() | (x=1.0,) ``` Now, let's compute the log-likelihood of the observation given specific values of the model parameters, `s` and `m`: From eb38750d962326c27f33a2f144816341b9c073ab Mon Sep 17 00:00:00 2001 From: Xianda Sun <5433119+sunxd3@users.noreply.github.com> Date: Fri, 10 May 2024 14:48:28 +0100 Subject: [PATCH 3/7] Update tutorials/docs-12-using-turing-guide/using-turing-guide.jmd Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- tutorials/docs-12-using-turing-guide/using-turing-guide.jmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd b/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd index 5b9d03f93..6ff80b760 100644 --- a/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd +++ b/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd @@ -374,7 +374,7 @@ logpdf(InverseGamma(2, 3), 1.0) + logpdf(Normal(0, sqrt(1.0)), 1.0) Finally, we can compute the log-joint probability of the model parameters and data: ```julia -logjoint(model, (s = 1.0, m = 1.0)) +logjoint(model, (s=1.0, m=1.0)) ``` ```julia From e3cb3f25fc3c98ea957b8b40954538bba8c69a8b Mon Sep 17 00:00:00 2001 From: Xianda Sun <5433119+sunxd3@users.noreply.github.com> Date: Fri, 10 May 2024 14:48:32 +0100 Subject: [PATCH 4/7] Update tutorials/docs-12-using-turing-guide/using-turing-guide.jmd Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- tutorials/docs-12-using-turing-guide/using-turing-guide.jmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd b/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd index 6ff80b760..9f818ca31 100644 --- a/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd +++ b/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd @@ -364,7 +364,7 @@ logpdf(Normal(1.0, 1.0), 1.0) We can also compute the log-prior probability of the model for the same values of s and m: ```julia -logprior(model, (s = 1.0, m = 1.0)) +logprior(model, (s=1.0, m=1.0)) ``` ```julia From f6ebe5e1dd715940c5598acb08612c9665dc2697 Mon Sep 17 00:00:00 2001 From: Xianda Sun <5433119+sunxd3@users.noreply.github.com> Date: Fri, 10 May 2024 14:48:36 +0100 Subject: [PATCH 5/7] Update tutorials/docs-12-using-turing-guide/using-turing-guide.jmd Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- tutorials/docs-12-using-turing-guide/using-turing-guide.jmd | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd b/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd index 9f818ca31..20cde0cdb 100644 --- a/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd +++ b/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd @@ -378,7 +378,9 @@ logjoint(model, (s=1.0, m=1.0)) ``` ```julia -logpdf(Normal(1.0, 1.0), 1.0) + logpdf(InverseGamma(2, 3), 1.0) + logpdf(Normal(0, sqrt(1.0)), 1.0) +logpdf(Normal(1.0, 1.0), 1.0) + +logpdf(InverseGamma(2, 3), 1.0) + +logpdf(Normal(0, sqrt(1.0)), 1.0) ``` Querying with `Chains` object is easy as well: From df0af5fc3cd34eba8a3464b2fe4348040c6a12ce Mon Sep 17 00:00:00 2001 From: Xianda Sun <5433119+sunxd3@users.noreply.github.com> Date: Fri, 10 May 2024 14:48:41 +0100 Subject: [PATCH 6/7] Update tutorials/docs-12-using-turing-guide/using-turing-guide.jmd Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- tutorials/docs-12-using-turing-guide/using-turing-guide.jmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd b/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd index 20cde0cdb..455f23715 100644 --- a/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd +++ b/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd @@ -352,7 +352,7 @@ model = gdemo0() | (x=1.0,) Now, let's compute the log-likelihood of the observation given specific values of the model parameters, `s` and `m`: ```julia -loglikelihood(model, (s = 1.0, m = 1.0)) +loglikelihood(model, (s=1.0, m=1.0)) ``` We can easily verify that value in this case: From 340c634fd6831fbc5faa30618506d93332eb157b Mon Sep 17 00:00:00 2001 From: Xianda Sun <5433119+sunxd3@users.noreply.github.com> Date: Sat, 11 May 2024 19:43:57 +0100 Subject: [PATCH 7/7] Update tutorials/docs-12-using-turing-guide/using-turing-guide.jmd Co-authored-by: Tor Erlend Fjelde --- tutorials/docs-12-using-turing-guide/using-turing-guide.jmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd b/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd index 455f23715..012274de3 100644 --- a/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd +++ b/tutorials/docs-12-using-turing-guide/using-turing-guide.jmd @@ -386,7 +386,7 @@ logpdf(Normal(0, sqrt(1.0)), 1.0) Querying with `Chains` object is easy as well: ```julia -chn = sample(model, NUTS(), 10) +chn = sample(model, Prior(), 10) ``` ```julia