From 8a4b8944eb81a4ac3f339b55a8471afde2013ce9 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 7 Dec 2022 22:19:34 +0000 Subject: [PATCH 001/218] extended methods for `logprior`, `loglikelihood`, `logposterior` for chains. --- src/logp.jl | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 src/logp.jl diff --git a/src/logp.jl b/src/logp.jl new file mode 100644 index 000000000..1e76aa6a2 --- /dev/null +++ b/src/logp.jl @@ -0,0 +1,149 @@ +# functions for evaluating logp: log posterior, log likelihood and log prior +using Turing, DynamicPPL, MCMCChains, StatsBase + +## 1. evaluate log prior at sample parameter positions +function DynamicPPL.logprior(model_instance::Model, chain::Chains) + """ + This function evaluates the `log prior` for chain. + -- Inputs + model: the probabilistic model instance; + chain: an MCMC chain. + -- Outputs + lls: a Vector of log prior values. + """ + varinfo = VarInfo(model_instance) # extract variables info from the model + lls = Matrix{Float64}(undef, size(chain, 1), size(chain, 3)) # initialize a matrix to store the evaluated log posterior + for chain_idx = 1:size(chain, 3) + for iteration_idx = 1:size(chain, 1) + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] + for vn_parent in keys(varinfo) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) + ) + # Compute and store. + lls[iteration_idx, chain_idx] = DynamicPPL.logprior(model_instance, argvals_dict) + end + end + return lls +end + +## 2. evaluate log likelihood at sample parameter positions +function DynamicPPL.loglikelihood(model_instance::Model, chain::Chains) + """ + This function evaluates the `log likelihood` for chain. + -- Inputs + model: the probabilistic model instance; + chain: an MCMC chain + -- Outputs + lls: a Vector of log likelihood values. + """ + varinfo = VarInfo(model_instance) # extract variables info from the model + lls = Matrix{Float64}(undef, size(chain, 1), size(chain, 3)) # initialize a matrix to store the evaluated log posterior + for chain_idx = 1:size(chain, 3) + for iteration_idx = 1:size(chain, 1) + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] + for vn_parent in keys(varinfo) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) + ) + # Compute and store. + lls[iteration_idx, chain_idx] = StatsBase.loglikelihood(model_instance, argvals_dict) + end + end + return lls +end + +## 3. evaluate log posterior at sample parameter positions +function DynamicPPL.logjoint(model_instance::Model, chain::Chains) + """ + This function evaluates the `log posterior` for chain. + -- Inputs + model: the probabilistic model instance; + chain: an MCMC chain object. + -- Outputs + lls: a Vector of log posterior values. + """ + varinfo = VarInfo(model_instance) # extract variables info from the model + lls = Matrix{Float64}(undef, size(chain, 1), size(chain, 3)) # initialize a matrix to store the evaluated log posterior + for chain_idx = 1:size(chain, 3) + for iteration_idx = 1:size(chain, 1) + # Extract sample parameter values using `varinfo` from the chain. + # TODO: This does not work for cases where the model has dynamic support, i.e. some of the iterations might have differently sized parameter space. + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] + for vn_parent in keys(varinfo) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) + ) + # Compute and store. + lls[iteration_idx, chain_idx] = StatsBase.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) + end + end + return lls + end + + function DynamicPPL.logjoint(model_instance::Model, arr::AbstractArray) + """ + This function evaluates the `log posterior` for chain. + -- Inputs + model: the probabilistic model instance; + arr: an un-named array of sample parameter values. + -- Outputs + lls: a Vector of log posterior values. + """ + varinfo = VarInfo(model_instance) # extract variables info from the model + lls = Array{Float64}(undef, size(arr, 1)) # initialize a matrix to store the evaluated log posterior + for param_idx = 1:size(arr, 1) + # Extract sample parameter values using `varinfo` from the chain. + # TODO: This does not work for cases where the model has dynamic support, i.e. some of the iterations might have differently sized parameter space. + argvals_dict = OrderedDict( + vn => arr[param_idx] + for vn_parent in keys(varinfo) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) + ) + # Compute and store. + lls[param_idx] = StatsBase.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) + end + return lls + end + + function DynamicPPL.logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) + """ + This function evaluates the `log posterior` for chain. + -- Inputs + model: the probabilistic model instance; + nt_array: an array of NamedTuple of sample parameter values. + -- Outputs + lls: a Vector of log posterior values. + """ + lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior + for param_idx = 1:size(nt_arr, 1) + # Compute and store. + lls[param_idx] = StatsBase.loglikelihood(model_instance, nt_arr[param_idx]) + DynamicPPL.logprior(model_instance, nt_arr[param_idx]) + end + return lls + end + +## Test +@model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end +end + +# generate data +using StableRNGs +rng = StableRNG(111) +using Random, Distributions +Random.seed!(111) +x = rand(Normal(1.0, 1.0), 1000) + +# MCMC sampling +chain = sample(demo_model(x), NUTS(0.65), 3_000) # chain: 1st index is the iteration no, 3rd index is the chain no. + +# evaluate logp +demo_model_instance = demo_model(x[1:10]) +lls = DynamicPPL.logprior(demo_model_instance, chain) +lls = DynamicPPL.loglikelihood(demo_model_instance, chain) +lls = DynamicPPL.logjoint(demo_model_instance, chain) \ No newline at end of file From 7fcebc57da02c1d15c9548d9b15626284bc64521 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 8 Dec 2022 10:33:26 +0000 Subject: [PATCH 002/218] accept Github Actions. --- src/logp.jl | 55 ++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 1e76aa6a2..af4efbd6e 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -13,12 +13,12 @@ function DynamicPPL.logprior(model_instance::Model, chain::Chains) """ varinfo = VarInfo(model_instance) # extract variables info from the model lls = Matrix{Float64}(undef, size(chain, 1), size(chain, 3)) # initialize a matrix to store the evaluated log posterior - for chain_idx = 1:size(chain, 3) - for iteration_idx = 1:size(chain, 1) + for chain_idx in 1:size(chain, 3) + for iteration_idx in 1:size(chain, 1) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] - for vn_parent in keys(varinfo) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for + vn_parent in keys(varinfo) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) ) # Compute and store. lls[iteration_idx, chain_idx] = DynamicPPL.logprior(model_instance, argvals_dict) @@ -39,12 +39,12 @@ function DynamicPPL.loglikelihood(model_instance::Model, chain::Chains) """ varinfo = VarInfo(model_instance) # extract variables info from the model lls = Matrix{Float64}(undef, size(chain, 1), size(chain, 3)) # initialize a matrix to store the evaluated log posterior - for chain_idx = 1:size(chain, 3) - for iteration_idx = 1:size(chain, 1) + for chain_idx in 1:size(chain, 3) + for iteration_idx in 1:size(chain, 1) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] - for vn_parent in keys(varinfo) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for + vn_parent in keys(varinfo) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) ) # Compute and store. lls[iteration_idx, chain_idx] = StatsBase.loglikelihood(model_instance, argvals_dict) @@ -65,23 +65,23 @@ function DynamicPPL.logjoint(model_instance::Model, chain::Chains) """ varinfo = VarInfo(model_instance) # extract variables info from the model lls = Matrix{Float64}(undef, size(chain, 1), size(chain, 3)) # initialize a matrix to store the evaluated log posterior - for chain_idx = 1:size(chain, 3) - for iteration_idx = 1:size(chain, 1) + for chain_idx in 1:size(chain, 3) + for iteration_idx in 1:size(chain, 1) # Extract sample parameter values using `varinfo` from the chain. # TODO: This does not work for cases where the model has dynamic support, i.e. some of the iterations might have differently sized parameter space. argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] - for vn_parent in keys(varinfo) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for + vn_parent in keys(varinfo) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) ) # Compute and store. lls[iteration_idx, chain_idx] = StatsBase.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) end end return lls - end +end - function DynamicPPL.logjoint(model_instance::Model, arr::AbstractArray) +function DynamicPPL.logjoint(model_instance::Model, arr::AbstractArray) """ This function evaluates the `log posterior` for chain. -- Inputs @@ -92,21 +92,20 @@ function DynamicPPL.logjoint(model_instance::Model, chain::Chains) """ varinfo = VarInfo(model_instance) # extract variables info from the model lls = Array{Float64}(undef, size(arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx = 1:size(arr, 1) + for param_idx in 1:size(arr, 1) # Extract sample parameter values using `varinfo` from the chain. # TODO: This does not work for cases where the model has dynamic support, i.e. some of the iterations might have differently sized parameter space. argvals_dict = OrderedDict( - vn => arr[param_idx] - for vn_parent in keys(varinfo) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) + vn => arr[param_idx] for vn_parent in keys(varinfo) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) ) # Compute and store. lls[param_idx] = StatsBase.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) end return lls - end +end - function DynamicPPL.logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) +function DynamicPPL.logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) """ This function evaluates the `log posterior` for chain. -- Inputs @@ -116,12 +115,12 @@ function DynamicPPL.logjoint(model_instance::Model, chain::Chains) lls: a Vector of log posterior values. """ lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx = 1:size(nt_arr, 1) + for param_idx in 1:size(nt_arr, 1) # Compute and store. lls[param_idx] = StatsBase.loglikelihood(model_instance, nt_arr[param_idx]) + DynamicPPL.logprior(model_instance, nt_arr[param_idx]) end return lls - end +end ## Test @model function demo_model(x) @@ -144,6 +143,6 @@ chain = sample(demo_model(x), NUTS(0.65), 3_000) # chain: 1st index is the itera # evaluate logp demo_model_instance = demo_model(x[1:10]) -lls = DynamicPPL.logprior(demo_model_instance, chain) -lls = DynamicPPL.loglikelihood(demo_model_instance, chain) -lls = DynamicPPL.logjoint(demo_model_instance, chain) \ No newline at end of file +lls = DynamicPPL.logprior(demo_model_instance, chain) +lls = DynamicPPL.loglikelihood(demo_model_instance, chain) +lls = DynamicPPL.logjoint(demo_model_instance, chain) \ No newline at end of file From 63d1970dfe64f78d7fbc7e83c8530ce9e45935f0 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 8 Dec 2022 10:39:20 +0000 Subject: [PATCH 003/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index af4efbd6e..34c6bbd57 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -21,7 +21,9 @@ function DynamicPPL.logprior(model_instance::Model, chain::Chains) vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) ) # Compute and store. - lls[iteration_idx, chain_idx] = DynamicPPL.logprior(model_instance, argvals_dict) + lls[iteration_idx, chain_idx] = DynamicPPL.logprior( + model_instance, argvals_dict + ) end end return lls From 3cc491220f6de0874bcf899900148e3bf2cce63e Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 8 Dec 2022 10:39:32 +0000 Subject: [PATCH 004/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 34c6bbd57..2bc56a72e 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -49,7 +49,9 @@ function DynamicPPL.loglikelihood(model_instance::Model, chain::Chains) vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) ) # Compute and store. - lls[iteration_idx, chain_idx] = StatsBase.loglikelihood(model_instance, argvals_dict) + lls[iteration_idx, chain_idx] = StatsBase.loglikelihood( + model_instance, argvals_dict + ) end end return lls From 0df18e0ad5d60041dc651302c0c59b7eef0835d2 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 8 Dec 2022 10:39:42 +0000 Subject: [PATCH 005/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 2bc56a72e..c2462ebcd 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -79,7 +79,9 @@ function DynamicPPL.logjoint(model_instance::Model, chain::Chains) vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) ) # Compute and store. - lls[iteration_idx, chain_idx] = StatsBase.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) + lls[iteration_idx, chain_idx] = + StatsBase.loglikelihood(model_instance, argvals_dict) + + DynamicPPL.logprior(model_instance, argvals_dict) end end return lls From 390265dc642da9dd14ddbebd544595c84ee5a3be Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 8 Dec 2022 10:39:54 +0000 Subject: [PATCH 006/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index c2462ebcd..2f466a332 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -106,7 +106,9 @@ function DynamicPPL.logjoint(model_instance::Model, arr::AbstractArray) vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) ) # Compute and store. - lls[param_idx] = StatsBase.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) + lls[param_idx] = + StatsBase.loglikelihood(model_instance, argvals_dict) + + DynamicPPL.logprior(model_instance, argvals_dict) end return lls end From ee329a2092bab5b724f52beb457d77e2ea9c40e4 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 8 Dec 2022 10:40:05 +0000 Subject: [PATCH 007/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 2f466a332..9ab6d5e3c 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -125,7 +125,9 @@ function DynamicPPL.logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior for param_idx in 1:size(nt_arr, 1) # Compute and store. - lls[param_idx] = StatsBase.loglikelihood(model_instance, nt_arr[param_idx]) + DynamicPPL.logprior(model_instance, nt_arr[param_idx]) + lls[param_idx] = + StatsBase.loglikelihood(model_instance, nt_arr[param_idx]) + + DynamicPPL.logprior(model_instance, nt_arr[param_idx]) end return lls end From e7bb1b8879b80533ef31ef0e6bb544194d801856 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 16 Dec 2022 16:46:53 +0000 Subject: [PATCH 008/218] typed `AbstractChains`; removed Array inputs. --- src/logp.jl | 43 +++++++++++-------------------------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 9ab6d5e3c..5dadbdf6b 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -1,12 +1,13 @@ # functions for evaluating logp: log posterior, log likelihood and log prior -using Turing, DynamicPPL, MCMCChains, StatsBase + +using DynamicPPL, AbstractMCMC, StatsBase ## 1. evaluate log prior at sample parameter positions -function DynamicPPL.logprior(model_instance::Model, chain::Chains) +function DynamicPPL.logprior(model_instance::Model, chain::AbstractChains) """ This function evaluates the `log prior` for chain. -- Inputs - model: the probabilistic model instance; + model_instance: the probabilistic model instance; chain: an MCMC chain. -- Outputs lls: a Vector of log prior values. @@ -30,7 +31,7 @@ function DynamicPPL.logprior(model_instance::Model, chain::Chains) end ## 2. evaluate log likelihood at sample parameter positions -function DynamicPPL.loglikelihood(model_instance::Model, chain::Chains) +function DynamicPPL.loglikelihood(model_instance::Model, chain::AbstractChains) """ This function evaluates the `log likelihood` for chain. -- Inputs @@ -58,7 +59,7 @@ function DynamicPPL.loglikelihood(model_instance::Model, chain::Chains) end ## 3. evaluate log posterior at sample parameter positions -function DynamicPPL.logjoint(model_instance::Model, chain::Chains) +function DynamicPPL.logjoint(model_instance::Model, chain::AbstractChains) """ This function evaluates the `log posterior` for chain. -- Inputs @@ -87,32 +88,6 @@ function DynamicPPL.logjoint(model_instance::Model, chain::Chains) return lls end -function DynamicPPL.logjoint(model_instance::Model, arr::AbstractArray) - """ - This function evaluates the `log posterior` for chain. - -- Inputs - model: the probabilistic model instance; - arr: an un-named array of sample parameter values. - -- Outputs - lls: a Vector of log posterior values. - """ - varinfo = VarInfo(model_instance) # extract variables info from the model - lls = Array{Float64}(undef, size(arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(arr, 1) - # Extract sample parameter values using `varinfo` from the chain. - # TODO: This does not work for cases where the model has dynamic support, i.e. some of the iterations might have differently sized parameter space. - argvals_dict = OrderedDict( - vn => arr[param_idx] for vn_parent in keys(varinfo) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) - ) - # Compute and store. - lls[param_idx] = - StatsBase.loglikelihood(model_instance, argvals_dict) + - DynamicPPL.logprior(model_instance, argvals_dict) - end - return lls -end - function DynamicPPL.logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) """ This function evaluates the `log posterior` for chain. @@ -155,4 +130,8 @@ chain = sample(demo_model(x), NUTS(0.65), 3_000) # chain: 1st index is the itera demo_model_instance = demo_model(x[1:10]) lls = DynamicPPL.logprior(demo_model_instance, chain) lls = DynamicPPL.loglikelihood(demo_model_instance, chain) -lls = DynamicPPL.logjoint(demo_model_instance, chain) \ No newline at end of file +lls = DynamicPPL.logjoint(demo_model_instance, chain) + +# final comments: +# 1. this script is doing similar to `pointwise_loglikelihoods`: https://beta.turing.ml/DynamicPPL.jl/stable/api/#DynamicPPL.pointwise_loglikelihoods +# 2. if the probabilistic model has a return statement for the log likelihood you would like to calculate, you can use `generated_quantities(model, chain)` to evaluate the likelihoods at sample positions. \ No newline at end of file From f6ec7e0b59e66c7057a1c61c66aebedc1aaf8682 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 16 Dec 2022 16:56:47 +0000 Subject: [PATCH 009/218] re-formatting. --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 5dadbdf6b..4f5f89961 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -133,5 +133,5 @@ lls = DynamicPPL.loglikelihood(demo_model_instance, chain) lls = DynamicPPL.logjoint(demo_model_instance, chain) # final comments: -# 1. this script is doing similar to `pointwise_loglikelihoods`: https://beta.turing.ml/DynamicPPL.jl/stable/api/#DynamicPPL.pointwise_loglikelihoods +# 1. this script is doing similar to `pointwise_loglikelihoods` ("https://beta.turing.ml/DynamicPPL.jl/stable/api/#DynamicPPL.pointwise_loglikelihoods") # 2. if the probabilistic model has a return statement for the log likelihood you would like to calculate, you can use `generated_quantities(model, chain)` to evaluate the likelihoods at sample positions. \ No newline at end of file From d7fa16feb276d0d8eb4c439f069315b49adb81c9 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 16 Dec 2022 17:03:49 +0000 Subject: [PATCH 010/218] removed comments to pass formatting test. --- src/logp.jl | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 4f5f89961..5d451683a 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -3,7 +3,7 @@ using DynamicPPL, AbstractMCMC, StatsBase ## 1. evaluate log prior at sample parameter positions -function DynamicPPL.logprior(model_instance::Model, chain::AbstractChains) +function DynamicPPL.logprior(model_instance::Model, chain<:AbstractChains) """ This function evaluates the `log prior` for chain. -- Inputs @@ -31,7 +31,7 @@ function DynamicPPL.logprior(model_instance::Model, chain::AbstractChains) end ## 2. evaluate log likelihood at sample parameter positions -function DynamicPPL.loglikelihood(model_instance::Model, chain::AbstractChains) +function DynamicPPL.loglikelihood(model_instance::Model, chain<:AbstractChains) """ This function evaluates the `log likelihood` for chain. -- Inputs @@ -59,7 +59,7 @@ function DynamicPPL.loglikelihood(model_instance::Model, chain::AbstractChains) end ## 3. evaluate log posterior at sample parameter positions -function DynamicPPL.logjoint(model_instance::Model, chain::AbstractChains) +function DynamicPPL.logjoint(model_instance::Model, chain<:AbstractChains) """ This function evaluates the `log posterior` for chain. -- Inputs @@ -130,8 +130,4 @@ chain = sample(demo_model(x), NUTS(0.65), 3_000) # chain: 1st index is the itera demo_model_instance = demo_model(x[1:10]) lls = DynamicPPL.logprior(demo_model_instance, chain) lls = DynamicPPL.loglikelihood(demo_model_instance, chain) -lls = DynamicPPL.logjoint(demo_model_instance, chain) - -# final comments: -# 1. this script is doing similar to `pointwise_loglikelihoods` ("https://beta.turing.ml/DynamicPPL.jl/stable/api/#DynamicPPL.pointwise_loglikelihoods") -# 2. if the probabilistic model has a return statement for the log likelihood you would like to calculate, you can use `generated_quantities(model, chain)` to evaluate the likelihoods at sample positions. \ No newline at end of file +lls = DynamicPPL.logjoint(demo_model_instance, chain) \ No newline at end of file From 5e1b144583ac7ad19a5884b04f91db962f97627c Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 16 Dec 2022 17:05:44 +0000 Subject: [PATCH 011/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 5d451683a..940d1e326 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -3,7 +3,7 @@ using DynamicPPL, AbstractMCMC, StatsBase ## 1. evaluate log prior at sample parameter positions -function DynamicPPL.logprior(model_instance::Model, chain<:AbstractChains) +function DynamicPPL.logprior(model_instance::Model, chain <: AbstractChains) """ This function evaluates the `log prior` for chain. -- Inputs From 4b114de681d5aed2ce6c4aaca00719542726fdf9 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 16 Dec 2022 17:05:57 +0000 Subject: [PATCH 012/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 940d1e326..3d027b4d1 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -31,7 +31,7 @@ function DynamicPPL.logprior(model_instance::Model, chain <: AbstractChains) end ## 2. evaluate log likelihood at sample parameter positions -function DynamicPPL.loglikelihood(model_instance::Model, chain<:AbstractChains) +function DynamicPPL.loglikelihood(model_instance::Model, chain <: AbstractChains) """ This function evaluates the `log likelihood` for chain. -- Inputs From 1ca088d8d01f60f1e00059e546ff137f4a8845ed Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 16 Dec 2022 17:06:06 +0000 Subject: [PATCH 013/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 3d027b4d1..bf9dca8b8 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -59,7 +59,7 @@ function DynamicPPL.loglikelihood(model_instance::Model, chain <: AbstractChains end ## 3. evaluate log posterior at sample parameter positions -function DynamicPPL.logjoint(model_instance::Model, chain<:AbstractChains) +function DynamicPPL.logjoint(model_instance::Model, chain <: AbstractChains) """ This function evaluates the `log posterior` for chain. -- Inputs From be54a5ae5f0a35e8b052c70cf4ff2eafa7462d68 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Sun, 18 Dec 2022 12:01:32 +0000 Subject: [PATCH 014/218] 1. removed the import statements in `lop.jl`; 2. removed the DynamicPPL. namespace declarations of the functions; 3. used Distributions.loglikelihood (instead of `StatsBase.loglikelihood`); 4. moved the tests to test/logp.jl and included them in test/runtests.jl. --- src/DynamicPPL.jl | 1 + src/logp.jl | 43 ++++++++----------------------------------- test/logp.jl | 25 +++++++++++++++++++++++++ test/runtests.jl | 2 ++ 4 files changed, 36 insertions(+), 35 deletions(-) create mode 100644 test/logp.jl diff --git a/src/DynamicPPL.jl b/src/DynamicPPL.jl index 443bdd797..9633d95cf 100644 --- a/src/DynamicPPL.jl +++ b/src/DynamicPPL.jl @@ -163,5 +163,6 @@ include("loglikelihoods.jl") include("submodel_macro.jl") include("test_utils.jl") include("transforming.jl") +include("logp.jl") end # module diff --git a/src/logp.jl b/src/logp.jl index bf9dca8b8..14c99cec6 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -1,9 +1,7 @@ # functions for evaluating logp: log posterior, log likelihood and log prior -using DynamicPPL, AbstractMCMC, StatsBase - ## 1. evaluate log prior at sample parameter positions -function DynamicPPL.logprior(model_instance::Model, chain <: AbstractChains) +function logprior(model_instance::Model, chain::T) where {T <: AbstractMCMC.AbstractChains} """ This function evaluates the `log prior` for chain. -- Inputs @@ -31,7 +29,7 @@ function DynamicPPL.logprior(model_instance::Model, chain <: AbstractChains) end ## 2. evaluate log likelihood at sample parameter positions -function DynamicPPL.loglikelihood(model_instance::Model, chain <: AbstractChains) +function loglikelihood(model_instance::Model, chain::T) where {T <: AbstractMCMC.AbstractChains} """ This function evaluates the `log likelihood` for chain. -- Inputs @@ -50,7 +48,7 @@ function DynamicPPL.loglikelihood(model_instance::Model, chain <: AbstractChains vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) ) # Compute and store. - lls[iteration_idx, chain_idx] = StatsBase.loglikelihood( + lls[iteration_idx, chain_idx] = Distributions.loglikelihood( model_instance, argvals_dict ) end @@ -59,7 +57,7 @@ function DynamicPPL.loglikelihood(model_instance::Model, chain <: AbstractChains end ## 3. evaluate log posterior at sample parameter positions -function DynamicPPL.logjoint(model_instance::Model, chain <: AbstractChains) +function logjoint(model_instance::Model, chain::T) where {T <: AbstractMCMC.AbstractChains} """ This function evaluates the `log posterior` for chain. -- Inputs @@ -81,14 +79,14 @@ function DynamicPPL.logjoint(model_instance::Model, chain <: AbstractChains) ) # Compute and store. lls[iteration_idx, chain_idx] = - StatsBase.loglikelihood(model_instance, argvals_dict) + + Distributions.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) end end return lls end -function DynamicPPL.logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) +function logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) """ This function evaluates the `log posterior` for chain. -- Inputs @@ -101,33 +99,8 @@ function DynamicPPL.logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) for param_idx in 1:size(nt_arr, 1) # Compute and store. lls[param_idx] = - StatsBase.loglikelihood(model_instance, nt_arr[param_idx]) + + Distributions.loglikelihood(model_instance, nt_arr[param_idx]) + DynamicPPL.logprior(model_instance, nt_arr[param_idx]) end return lls -end - -## Test -@model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end -end - -# generate data -using StableRNGs -rng = StableRNG(111) -using Random, Distributions -Random.seed!(111) -x = rand(Normal(1.0, 1.0), 1000) - -# MCMC sampling -chain = sample(demo_model(x), NUTS(0.65), 3_000) # chain: 1st index is the iteration no, 3rd index is the chain no. - -# evaluate logp -demo_model_instance = demo_model(x[1:10]) -lls = DynamicPPL.logprior(demo_model_instance, chain) -lls = DynamicPPL.loglikelihood(demo_model_instance, chain) -lls = DynamicPPL.logjoint(demo_model_instance, chain) \ No newline at end of file +end \ No newline at end of file diff --git a/test/logp.jl b/test/logp.jl new file mode 100644 index 000000000..bc3f3630e --- /dev/null +++ b/test/logp.jl @@ -0,0 +1,25 @@ + +## Test +@model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end +end + +# generate data +using StableRNGs, Turing +rng = StableRNG(111) +using Random, Distributions +Random.seed!(111) +x = rand(Normal(1.0, 1.0), 1000) + +# MCMC sampling +demo_model_instance = demo_model(x) +chain = sample(demo_model_instance, NUTS(0.65), 5_00) # chain: 1st index is the iteration no, 3rd index is the chain no. + +# evaluate logp +lls = logprior(demo_model_instance, chain) +lls = loglikelihood(demo_model_instance, chain) +lls = logjoint(demo_model_instance, chain) \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index dfc8d7acf..304fa76a1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -49,6 +49,8 @@ include("test_util.jl") include("serialization.jl") include("loglikelihoods.jl") + + include("logp.jl") end @testset "compat" begin From aa470245424407dbe0761e03f5e49d92814a51a8 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Sun, 18 Dec 2022 12:08:48 +0000 Subject: [PATCH 015/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 14c99cec6..de373a54b 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -1,7 +1,7 @@ # functions for evaluating logp: log posterior, log likelihood and log prior ## 1. evaluate log prior at sample parameter positions -function logprior(model_instance::Model, chain::T) where {T <: AbstractMCMC.AbstractChains} +function logprior(model_instance::Model, chain::T) where {T<:AbstractMCMC.AbstractChains} """ This function evaluates the `log prior` for chain. -- Inputs From 18a88317eb3195d9516d493b2ac9d2572d90c8bb Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Sun, 18 Dec 2022 12:08:58 +0000 Subject: [PATCH 016/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index de373a54b..72942ca91 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -29,7 +29,9 @@ function logprior(model_instance::Model, chain::T) where {T<:AbstractMCMC.Abstra end ## 2. evaluate log likelihood at sample parameter positions -function loglikelihood(model_instance::Model, chain::T) where {T <: AbstractMCMC.AbstractChains} +function loglikelihood( + model_instance::Model, chain::T +) where {T<:AbstractMCMC.AbstractChains} """ This function evaluates the `log likelihood` for chain. -- Inputs From 063eed0a2ec8d36623feb19f563f3fbe9f29746a Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Sun, 18 Dec 2022 12:09:21 +0000 Subject: [PATCH 017/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 72942ca91..8a14c19e6 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -59,7 +59,7 @@ function loglikelihood( end ## 3. evaluate log posterior at sample parameter positions -function logjoint(model_instance::Model, chain::T) where {T <: AbstractMCMC.AbstractChains} +function logjoint(model_instance::Model, chain::T) where {T<:AbstractMCMC.AbstractChains} """ This function evaluates the `log posterior` for chain. -- Inputs From d29f80d78b8b23a162e26201887c7033be73abbc Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 19 Dec 2022 17:33:36 +0000 Subject: [PATCH 018/218] Update src/logp.jl Co-authored-by: David Widmann --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 8a14c19e6..e029cabd8 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -1,7 +1,7 @@ # functions for evaluating logp: log posterior, log likelihood and log prior ## 1. evaluate log prior at sample parameter positions -function logprior(model_instance::Model, chain::T) where {T<:AbstractMCMC.AbstractChains} +function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) """ This function evaluates the `log prior` for chain. -- Inputs From 0e773ab2e05bf92efc1360caa895b08bb01eb717 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 19 Dec 2022 17:34:14 +0000 Subject: [PATCH 019/218] Update src/logp.jl Co-authored-by: David Widmann --- src/logp.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index e029cabd8..2795e02b9 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -29,9 +29,9 @@ function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) end ## 2. evaluate log likelihood at sample parameter positions -function loglikelihood( - model_instance::Model, chain::T -) where {T<:AbstractMCMC.AbstractChains} +function Distributions.loglikelihood( + model_instance::Model, chain::AbstractMCMC.AbstractChains +) """ This function evaluates the `log likelihood` for chain. -- Inputs From efd0c8748f16ecda386a7c2b57d19d7d1a624a8b Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 19 Dec 2022 17:34:27 +0000 Subject: [PATCH 020/218] Update src/logp.jl Co-authored-by: David Widmann --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 2795e02b9..3bd7f1943 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -59,7 +59,7 @@ function Distributions.loglikelihood( end ## 3. evaluate log posterior at sample parameter positions -function logjoint(model_instance::Model, chain::T) where {T<:AbstractMCMC.AbstractChains} +function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) """ This function evaluates the `log posterior` for chain. -- Inputs From 075ac8190d7cd890774f35180b0b8d4994711278 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:46:49 +0000 Subject: [PATCH 021/218] modified src/logp.jl; added test/logp.jl --- src/logp.jl | 175 ++++++++++++++++++++++++++++++++++++++------------- test/logp.jl | 61 +++++++++++------- 2 files changed, 169 insertions(+), 67 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 3bd7f1943..364df3722 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -1,23 +1,23 @@ # functions for evaluating logp: log posterior, log likelihood and log prior -## 1. evaluate log prior at sample parameter positions -function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) - """ - This function evaluates the `log prior` for chain. - -- Inputs - model_instance: the probabilistic model instance; - chain: an MCMC chain. - -- Outputs - lls: a Vector of log prior values. - """ - varinfo = VarInfo(model_instance) # extract variables info from the model +#### 1. logprior #### +""" +This function evaluates the `log prior` for chain. +-- Inputs + model_instance: the probabilistic model instance; + chain: an MCMC chain. +-- Outputs + lls: a Vector of log prior values. +""" +function chain_logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) + vi = VarInfo(model_instance) # extract variables info from the model lls = Matrix{Float64}(undef, size(chain, 1), size(chain, 3)) # initialize a matrix to store the evaluated log posterior for chain_idx in 1:size(chain, 3) for iteration_idx in 1:size(chain, 1) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for - vn_parent in keys(varinfo) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) + vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) # Compute and store. lls[iteration_idx, chain_idx] = DynamicPPL.logprior( @@ -28,26 +28,59 @@ function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) return lls end -## 2. evaluate log likelihood at sample parameter positions -function Distributions.loglikelihood( - model_instance::Model, chain::AbstractMCMC.AbstractChains -) - """ +""" + This function evaluates the `log prior` for chain. + -- Inputs + model: the probabilistic model instance; + nt_array: an array of NamedTuple of sample parameter values. + -- Outputs + lls: a Vector of log prior values. +""" +function chain_logprior(model_instance::Model, nt_arr::Vector{NamedTuple}) + lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior + for param_idx in 1:size(nt_arr, 1) + # Compute and store. + lls[param_idx] = DynamicPPL.logprior(model_instance, nt_arr[param_idx]) + end + return lls +end + +""" + This function evaluates the `log prior` for chain. + -- Inputs + model: the probabilistic model instance; + nt_array: an array of NamedTuple of sample parameter values. + -- Outputs + lls: a Vector of log prior values. +""" +function chain_logprior(model_instance::Model, nt_arr::Vector{Any}) + lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior + for param_idx in 1:size(nt_arr, 1) + # Compute and store. + lls[param_idx] = DynamicPPL.logprior(model_instance, nt_arr[param_idx]) + end + return lls +end + + +#### 2. loglikelihood #### +""" This function evaluates the `log likelihood` for chain. -- Inputs model: the probabilistic model instance; chain: an MCMC chain -- Outputs lls: a Vector of log likelihood values. - """ - varinfo = VarInfo(model_instance) # extract variables info from the model +""" +function chain_loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains) + vi = VarInfo(model_instance) # extract variables info from the model lls = Matrix{Float64}(undef, size(chain, 1), size(chain, 3)) # initialize a matrix to store the evaluated log posterior for chain_idx in 1:size(chain, 3) for iteration_idx in 1:size(chain, 1) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for - vn_parent in keys(varinfo) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) + vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) # Compute and store. lls[iteration_idx, chain_idx] = Distributions.loglikelihood( @@ -58,17 +91,52 @@ function Distributions.loglikelihood( return lls end -## 3. evaluate log posterior at sample parameter positions -function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) - """ - This function evaluates the `log posterior` for chain. - -- Inputs - model: the probabilistic model instance; - chain: an MCMC chain object. - -- Outputs - lls: a Vector of log posterior values. - """ - varinfo = VarInfo(model_instance) # extract variables info from the model +""" +This function evaluates the `log likelihood` for chain. +-- Inputs + model: the probabilistic model instance; + nt_array: an array of NamedTuple of sample parameter values. +-- Outputs + lls: a Vector of log likelihood values. +""" +function chain_loglikelihoods(model_instance::Model, nt_arr::Vector{NamedTuple}) + lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior + for param_idx in 1:size(nt_arr, 1) + # Compute and store. + lls[param_idx] = Distributions.loglikelihood(model_instance, nt_arr[param_idx]) + end + return lls +end + +""" +This function evaluates the `log likelihood` for chain. +-- Inputs + model: the probabilistic model instance; + nt_array: an array of NamedTuple of sample parameter values. +-- Outputs + lls: a Vector of log likelihood values. +""" +function chain_loglikelihoods(model_instance::Model, nt_arr::Vector{Any}) + lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior + for param_idx in 1:size(nt_arr, 1) + # Compute and store. + lls[param_idx] = Distributions.loglikelihood(model_instance, nt_arr[param_idx]) + end + return lls +end + +#### 3. logjoint #### +""" +This function evaluates the `log posterior` for chain. +-- Inputs + model: the probabilistic model instance; + chain: an MCMC chain object. +-- Outputs + lls: a Vector of log posterior values. +""" +## evaluate log posterior at sample parameter positions +function chain_logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) + vi = VarInfo(model_instance) # extract variables info from the model lls = Matrix{Float64}(undef, size(chain, 1), size(chain, 3)) # initialize a matrix to store the evaluated log posterior for chain_idx in 1:size(chain, 3) for iteration_idx in 1:size(chain, 1) @@ -76,8 +144,8 @@ function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) # TODO: This does not work for cases where the model has dynamic support, i.e. some of the iterations might have differently sized parameter space. argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for - vn_parent in keys(varinfo) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, varinfo[vn_parent]) + vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) # Compute and store. lls[iteration_idx, chain_idx] = @@ -88,15 +156,34 @@ function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) return lls end -function logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) - """ - This function evaluates the `log posterior` for chain. - -- Inputs - model: the probabilistic model instance; - nt_array: an array of NamedTuple of sample parameter values. - -- Outputs - lls: a Vector of log posterior values. - """ +""" +This function evaluates the `log posterior` for chain. +-- Inputs + model: the probabilistic model instance; + nt_array: an array of NamedTuple of sample parameter values. +-- Outputs + lls: a Vector of log posterior values. +""" +function chain_logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) + lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior + for param_idx in 1:size(nt_arr, 1) + # Compute and store. + lls[param_idx] = + Distributions.loglikelihood(model_instance, nt_arr[param_idx]) + + DynamicPPL.logprior(model_instance, nt_arr[param_idx]) + end + return lls +end + +""" +This function evaluates the `log posterior` for chain. +-- Inputs + model: the probabilistic model instance; + nt_array: an array of NamedTuple of sample parameter values. +-- Outputs + lls: a Vector of log posterior values. +""" +function chain_logjoint(model_instance::Model, nt_arr::Vector{Any}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior for param_idx in 1:size(nt_arr, 1) # Compute and store. diff --git a/test/logp.jl b/test/logp.jl index bc3f3630e..c8822401f 100644 --- a/test/logp.jl +++ b/test/logp.jl @@ -1,25 +1,40 @@ +@testset "logp.jl" begin + Test.@testset "$(m.f)" for m in DynamicPPL.TestUtils.DEMO_MODELS + # generate a chain of sample parameter values. + N = 100 + chain = Vector(undef, N) + vi_vector = Dict() + logpriors_true = Vector(undef, N) + loglikelihoods_true = Vector(undef, N) + logposteriors_true = Vector(undef, N) + for i in 1:N + # generate samples and extrac vi + example_values = rand(NamedTuple, m) + print(example_values) + chain[i] = example_values + # append!(chain, [example_values]) + # Instantiate a `VarInfo` with the example values. + vi = VarInfo(m) + for vn in DynamicPPL.TestUtils.varnames(m) + vi = DynamicPPL.setindex!!(vi, get(example_values, vn), vn) + end + vi_vector["$i"] = vi -## Test -@model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) + # calculate the true pointwise likelihood + logprior_true = DynamicPPL.TestUtils.logprior_true(m, example_values...) + logpriors_true[i] = logprior_true + loglikelihood_true = DynamicPPL.TestUtils.loglikelihood_true(m, example_values...) + loglikelihoods_true[i] = loglikelihood_true + logposterior_true = logprior_true + loglikelihood_true + logposteriors_true[i] = logposterior_true + end + # calculate the pointwise loglikelihoods for the whole chain using custom logprior. + logpriors_new = chain_logprior(m, chain) + loglikelihoods_new = chain_loglikelihoods(m, chain) + logposteriors_new = chain_logjoint(m, chain) + # compare the likelihoods + @test logpriors_new ≈ logpriors_true + @test loglikelihoods_new ≈ loglikelihoods_true + @test logposteriors_new ≈ logposteriors_true end -end - -# generate data -using StableRNGs, Turing -rng = StableRNG(111) -using Random, Distributions -Random.seed!(111) -x = rand(Normal(1.0, 1.0), 1000) - -# MCMC sampling -demo_model_instance = demo_model(x) -chain = sample(demo_model_instance, NUTS(0.65), 5_00) # chain: 1st index is the iteration no, 3rd index is the chain no. - -# evaluate logp -lls = logprior(demo_model_instance, chain) -lls = loglikelihood(demo_model_instance, chain) -lls = logjoint(demo_model_instance, chain) \ No newline at end of file +end \ No newline at end of file From 17aee2b1a8d2ebb5b43caf65d9a812c2e4af2df3 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:49:35 +0000 Subject: [PATCH 022/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 364df3722..90c9629b9 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -15,9 +15,8 @@ function chain_logprior(model_instance::Model, chain::AbstractMCMC.AbstractChain for chain_idx in 1:size(chain, 3) for iteration_idx in 1:size(chain, 1) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for - vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) # Compute and store. lls[iteration_idx, chain_idx] = DynamicPPL.logprior( From d1add251b5c4f2940b172b94ccfc7a84aa5f36d2 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:49:53 +0000 Subject: [PATCH 023/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 90c9629b9..95b92ed83 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -61,7 +61,6 @@ function chain_logprior(model_instance::Model, nt_arr::Vector{Any}) return lls end - #### 2. loglikelihood #### """ This function evaluates the `log likelihood` for chain. From 9623302111e77ae89888a8291d1632eb48e8e1cd Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:50:06 +0000 Subject: [PATCH 024/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 95b92ed83..ecc626ce1 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -76,9 +76,8 @@ function chain_loglikelihoods(model_instance::Model, chain::AbstractMCMC.Abstrac for chain_idx in 1:size(chain, 3) for iteration_idx in 1:size(chain, 1) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for - vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) # Compute and store. lls[iteration_idx, chain_idx] = Distributions.loglikelihood( From d31a7c33270c2375c14ab8516e32a06bcee1d622 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:50:17 +0000 Subject: [PATCH 025/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index ecc626ce1..2619ef17b 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -140,9 +140,8 @@ function chain_logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChain # Extract sample parameter values using `varinfo` from the chain. # TODO: This does not work for cases where the model has dynamic support, i.e. some of the iterations might have differently sized parameter space. argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for - vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) # Compute and store. lls[iteration_idx, chain_idx] = From 374cfde41d50e8b9710cf6ec2079bbf587c3c0f8 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:50:30 +0000 Subject: [PATCH 026/218] Update test/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/logp.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/logp.jl b/test/logp.jl index c8822401f..38c61c844 100644 --- a/test/logp.jl +++ b/test/logp.jl @@ -23,7 +23,9 @@ # calculate the true pointwise likelihood logprior_true = DynamicPPL.TestUtils.logprior_true(m, example_values...) logpriors_true[i] = logprior_true - loglikelihood_true = DynamicPPL.TestUtils.loglikelihood_true(m, example_values...) + loglikelihood_true = DynamicPPL.TestUtils.loglikelihood_true( + m, example_values... + ) loglikelihoods_true[i] = loglikelihood_true logposterior_true = logprior_true + loglikelihood_true logposteriors_true[i] = logposterior_true From 94c38644ee232363ed73b8292a7d6f91a98ef09a Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 12 Jan 2023 15:10:48 +0000 Subject: [PATCH 027/218] Modified Docstrings; Changed names; Modified methods following Tor's suggestions. --- src/logp.jl | 307 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 183 insertions(+), 124 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 2619ef17b..f198ffede 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -2,40 +2,69 @@ #### 1. logprior #### """ -This function evaluates the `log prior` for chain. --- Inputs - model_instance: the probabilistic model instance; - chain: an MCMC chain. --- Outputs - lls: a Vector of log prior values. -""" -function chain_logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) - vi = VarInfo(model_instance) # extract variables info from the model - lls = Matrix{Float64}(undef, size(chain, 1), size(chain, 3)) # initialize a matrix to store the evaluated log posterior - for chain_idx in 1:size(chain, 3) - for iteration_idx in 1:size(chain, 1) - argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) - # Compute and store. - lls[iteration_idx, chain_idx] = DynamicPPL.logprior( - model_instance, argvals_dict - ) + logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) + logprior(model_instance::Model, nt_arr::Vector{NamedTuple}) + logprior(model_instance::Model, nt_arr::Vector{Any}) + +Return an array of log priors evaluated at each sample in an MCMC chain or sample array. + +Example 1: + + ```julia + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) end end - return lls -end + # generate data + using Random, Distributions, Turing + Random.seed!(111) + x = rand(Normal(1.0, 1.0), 1000) + # construct a chain of samples via MCMC sampling + using Turing + chain = sample(demo_model(x), NUTS(0.65), 3_000) # chain: 1st index is the iteration no, 3rd index is the chain no. + logprior(demo_model(x), chain) + + Outputs: + julia> 3000×1 Matrix{Float64}: + -2.2188656502119937 + -2.233332271475687 + ⋮ + ``` -""" - This function evaluates the `log prior` for chain. - -- Inputs - model: the probabilistic model instance; - nt_array: an array of NamedTuple of sample parameter values. - -- Outputs - lls: a Vector of log prior values. -""" -function chain_logprior(model_instance::Model, nt_arr::Vector{NamedTuple}) +Example 2: + + ```julia + # generate data + sample_array = Vector(undef, 100) + m = DynamicPPL.TestUtils.DEMO_MODELS[1] + for i in 1:100 + example_values = rand(NamedTuple, m) + sample_array[i] = example_values + end + # calculate the pointwise loglikelihoods for the whole array. + logprior(m, sample_array) + + Outputs: + julia> 100-element Vector{Float64}: + -3.8653211194703863 + -6.727832990780729 + ⋮ + ``` +""" +function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) + vi = VarInfo(model_instance) # extract variables info from the model + map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] + for vn_parent in keys(vi) for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) + DynamicPPL.logprior(model_instance, argvals_dict) + end +end +function logprior(model_instance::Model, nt_arr::Vector{NamedTuple}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior for param_idx in 1:size(nt_arr, 1) # Compute and store. @@ -43,16 +72,7 @@ function chain_logprior(model_instance::Model, nt_arr::Vector{NamedTuple}) end return lls end - -""" - This function evaluates the `log prior` for chain. - -- Inputs - model: the probabilistic model instance; - nt_array: an array of NamedTuple of sample parameter values. - -- Outputs - lls: a Vector of log prior values. -""" -function chain_logprior(model_instance::Model, nt_arr::Vector{Any}) +function logprior(model_instance::Model, nt_arr::Vector{Any}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior for param_idx in 1:size(nt_arr, 1) # Compute and store. @@ -61,42 +81,72 @@ function chain_logprior(model_instance::Model, nt_arr::Vector{Any}) return lls end + #### 2. loglikelihood #### """ - This function evaluates the `log likelihood` for chain. - -- Inputs - model: the probabilistic model instance; - chain: an MCMC chain - -- Outputs - lls: a Vector of log likelihood values. -""" -function chain_loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains) - vi = VarInfo(model_instance) # extract variables info from the model - lls = Matrix{Float64}(undef, size(chain, 1), size(chain, 3)) # initialize a matrix to store the evaluated log posterior - for chain_idx in 1:size(chain, 3) - for iteration_idx in 1:size(chain, 1) - argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) - # Compute and store. - lls[iteration_idx, chain_idx] = Distributions.loglikelihood( - model_instance, argvals_dict - ) + loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains) + loglikelihoods(model_instance::Model, nt_arr::Vector{NamedTuple}) + loglikelihoods(model_instance::Model, nt_arr::Vector{Any}) + +Return an array of log likelihoods evaluated at each sample in an MCMC chain or sample array. + +Example 1: + + ```julia + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) end end - return lls -end + # generate data + using Random, Distributions, Turing + Random.seed!(111) + x = rand(Normal(1.0, 1.0), 1000) + # construct a chain of samples via MCMC sampling + using Turing + chain = sample(demo_model(x), NUTS(0.65), 3_000) # chain: 1st index is the iteration no, 3rd index is the chain no. + loglikelihoods(demo_model(x), chain) -""" -This function evaluates the `log likelihood` for chain. --- Inputs - model: the probabilistic model instance; - nt_array: an array of NamedTuple of sample parameter values. --- Outputs - lls: a Vector of log likelihood values. -""" -function chain_loglikelihoods(model_instance::Model, nt_arr::Vector{NamedTuple}) + Outputs: + julia> 3000×1 Matrix{Float64}: + -1460.969366266108 + -1460.115380195131 + ⋮ + ``` + +Example 2: + + ```julia + # generate data + sample_array = Vector(undef, 100) + m = DynamicPPL.TestUtils.DEMO_MODELS[1] + for i in 1:100 + example_values = rand(NamedTuple, m) + sample_array[i] = example_values + end + # calculate the pointwise loglikelihoods for the whole array. + loglikelihoods(m, sample_array) + + Outputs: + julia> 100-element Vector{Float64}: + -7.31146824079265 + -3.006725528921822 + ⋮ + ``` +""" +function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains) + vi = VarInfo(model_instance) # extract variables info from the model + map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] + for vn_parent in keys(vi) for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) + Distributions.loglikelihood(model_instance, argvals_dict) + end +end +function loglikelihoods(model_instance::Model, nt_arr::Vector{NamedTuple}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior for param_idx in 1:size(nt_arr, 1) # Compute and store. @@ -104,16 +154,7 @@ function chain_loglikelihoods(model_instance::Model, nt_arr::Vector{NamedTuple}) end return lls end - -""" -This function evaluates the `log likelihood` for chain. --- Inputs - model: the probabilistic model instance; - nt_array: an array of NamedTuple of sample parameter values. --- Outputs - lls: a Vector of log likelihood values. -""" -function chain_loglikelihoods(model_instance::Model, nt_arr::Vector{Any}) +function loglikelihoods(model_instance::Model, nt_arr::Vector{Any}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior for param_idx in 1:size(nt_arr, 1) # Compute and store. @@ -124,43 +165,70 @@ end #### 3. logjoint #### """ -This function evaluates the `log posterior` for chain. --- Inputs - model: the probabilistic model instance; - chain: an MCMC chain object. --- Outputs - lls: a Vector of log posterior values. + logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) + logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) + logjoint(model_instance::Model, nt_arr::Vector{Any}) + +Return an array of log posteriors evaluated at each sample in an MCMC chain or sample array. + +Example 1: + + ```julia + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end + end + # generate data + using Random, Distributions, Turing + Random.seed!(111) + x = rand(Normal(1.0, 1.0), 1000) + # construct a chain of samples via MCMC sampling + using Turing + chain = sample(demo_model(x), NUTS(0.65), 3_000) # chain: 1st index is the iteration no, 3rd index is the chain no. + logjoint(demo_model(x), chain) + + Outputs: + julia> 3000×1 Matrix{Float64}: + -1463.1882319163199 + -1462.3487124666067 + ⋮ + ``` + +Example 2: + + ```julia + # generate data + sample_array = Vector(undef, 100) + m = DynamicPPL.TestUtils.DEMO_MODELS[1] + for i in 1:100 + example_values = rand(NamedTuple, m) + sample_array[i] = example_values + end + # calculate the pointwise loglikelihoods for the whole array. + logjoint(m, sample_array) + + Outputs: + julia> 100-element Vector{Float64}: + -11.176789360263037 + -9.734558519702551 + ⋮ + ``` """ ## evaluate log posterior at sample parameter positions -function chain_logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) +function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model - lls = Matrix{Float64}(undef, size(chain, 1), size(chain, 3)) # initialize a matrix to store the evaluated log posterior - for chain_idx in 1:size(chain, 3) - for iteration_idx in 1:size(chain, 1) - # Extract sample parameter values using `varinfo` from the chain. - # TODO: This does not work for cases where the model has dynamic support, i.e. some of the iterations might have differently sized parameter space. - argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) - # Compute and store. - lls[iteration_idx, chain_idx] = - Distributions.loglikelihood(model_instance, argvals_dict) + - DynamicPPL.logprior(model_instance, argvals_dict) - end + map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] + for vn_parent in keys(vi) for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) + Distributions.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) end - return lls end - -""" -This function evaluates the `log posterior` for chain. --- Inputs - model: the probabilistic model instance; - nt_array: an array of NamedTuple of sample parameter values. --- Outputs - lls: a Vector of log posterior values. -""" -function chain_logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) +function logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior for param_idx in 1:size(nt_arr, 1) # Compute and store. @@ -170,16 +238,7 @@ function chain_logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) end return lls end - -""" -This function evaluates the `log posterior` for chain. --- Inputs - model: the probabilistic model instance; - nt_array: an array of NamedTuple of sample parameter values. --- Outputs - lls: a Vector of log posterior values. -""" -function chain_logjoint(model_instance::Model, nt_arr::Vector{Any}) +function logjoint(model_instance::Model, nt_arr::Vector{Any}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior for param_idx in 1:size(nt_arr, 1) # Compute and store. From 35683c78f62c97b1a36e7a8118af11493622ddc4 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 12 Jan 2023 15:14:13 +0000 Subject: [PATCH 028/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index f198ffede..78eb4454d 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -58,8 +58,8 @@ function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] - for vn_parent in keys(vi) for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) DynamicPPL.logprior(model_instance, argvals_dict) end From 189550cb313575dce31c11df77013cbeb26774da Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 12 Jan 2023 15:14:27 +0000 Subject: [PATCH 029/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 78eb4454d..8a5b67cb7 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -140,8 +140,8 @@ function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChain vi = VarInfo(model_instance) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] - for vn_parent in keys(vi) for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) Distributions.loglikelihood(model_instance, argvals_dict) end From 20ac6c03288dc9eee0f54379651ed28c7ecc043f Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 12 Jan 2023 15:14:43 +0000 Subject: [PATCH 030/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 8a5b67cb7..53bf58d00 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -222,8 +222,8 @@ function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] - for vn_parent in keys(vi) for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) Distributions.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) end From 5303e6fab4a58cfe0d6a5da58e74fed0a26e96c3 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 12 Jan 2023 15:14:58 +0000 Subject: [PATCH 031/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 53bf58d00..0f6d4a4f5 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -225,7 +225,8 @@ function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) - Distributions.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) + Distributions.loglikelihood(model_instance, argvals_dict) + + DynamicPPL.logprior(model_instance, argvals_dict) end end function logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) From 34e9383a81a35c7072613391f1c6c7ebcd9bdf93 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 16 Jan 2023 19:53:25 +0000 Subject: [PATCH 032/218] renamed `chain_logprior`,`chain_loglikelihood',`chain_logposterior' to `logprior`,`loglikelihood',`logposterior' . --- test/logp.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/logp.jl b/test/logp.jl index 38c61c844..4dc56beca 100644 --- a/test/logp.jl +++ b/test/logp.jl @@ -31,9 +31,9 @@ logposteriors_true[i] = logposterior_true end # calculate the pointwise loglikelihoods for the whole chain using custom logprior. - logpriors_new = chain_logprior(m, chain) - loglikelihoods_new = chain_loglikelihoods(m, chain) - logposteriors_new = chain_logjoint(m, chain) + logpriors_new = logprior(m, chain) + loglikelihoods_new = loglikelihoods(m, chain) + logposteriors_new = logjoint(m, chain) # compare the likelihoods @test logpriors_new ≈ logpriors_true @test loglikelihoods_new ≈ loglikelihoods_true From db984b33205b7af0a6652787e5e018fc526611c2 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 16:57:17 +0000 Subject: [PATCH 033/218] added `include("logdensityfunction.jl")` to `DynamicPPL.jl` --- src/DynamicPPL.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DynamicPPL.jl b/src/DynamicPPL.jl index 8e09b3d7b..edb65a0c5 100644 --- a/src/DynamicPPL.jl +++ b/src/DynamicPPL.jl @@ -164,6 +164,7 @@ include("loglikelihoods.jl") include("submodel_macro.jl") include("test_utils.jl") include("transforming.jl") +include("logdensityfunction.jl") include("logp.jl") end # module From 417d3d008bf319b5b203c319157657892182616f Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:30:27 +0000 Subject: [PATCH 034/218] formatted `test/logp.jl`. --- test/logp.jl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/logp.jl b/test/logp.jl index 4dc56beca..dbf5db7d2 100644 --- a/test/logp.jl +++ b/test/logp.jl @@ -7,7 +7,7 @@ logpriors_true = Vector(undef, N) loglikelihoods_true = Vector(undef, N) logposteriors_true = Vector(undef, N) - for i in 1:N + for i = 1:N # generate samples and extrac vi example_values = rand(NamedTuple, m) print(example_values) @@ -23,9 +23,8 @@ # calculate the true pointwise likelihood logprior_true = DynamicPPL.TestUtils.logprior_true(m, example_values...) logpriors_true[i] = logprior_true - loglikelihood_true = DynamicPPL.TestUtils.loglikelihood_true( - m, example_values... - ) + loglikelihood_true = + DynamicPPL.TestUtils.loglikelihood_true(m, example_values...) loglikelihoods_true[i] = loglikelihood_true logposterior_true = logprior_true + loglikelihood_true logposteriors_true[i] = logposterior_true @@ -39,4 +38,4 @@ @test loglikelihoods_new ≈ loglikelihoods_true @test logposteriors_new ≈ logposteriors_true end -end \ No newline at end of file +end From 94d981be37b5780b2bf2af42e476264ec360461b Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:32:02 +0000 Subject: [PATCH 035/218] Update test/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/logp.jl b/test/logp.jl index dbf5db7d2..76ec5fd54 100644 --- a/test/logp.jl +++ b/test/logp.jl @@ -7,7 +7,7 @@ logpriors_true = Vector(undef, N) loglikelihoods_true = Vector(undef, N) logposteriors_true = Vector(undef, N) - for i = 1:N + for i in 1:N # generate samples and extrac vi example_values = rand(NamedTuple, m) print(example_values) From d3b8d5ac30600730ab8262dd7cc31d6083de28a0 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:32:25 +0000 Subject: [PATCH 036/218] Update test/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/logp.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/logp.jl b/test/logp.jl index 76ec5fd54..a5de423b5 100644 --- a/test/logp.jl +++ b/test/logp.jl @@ -23,8 +23,9 @@ # calculate the true pointwise likelihood logprior_true = DynamicPPL.TestUtils.logprior_true(m, example_values...) logpriors_true[i] = logprior_true - loglikelihood_true = - DynamicPPL.TestUtils.loglikelihood_true(m, example_values...) + loglikelihood_true = DynamicPPL.TestUtils.loglikelihood_true( + m, example_values... + ) loglikelihoods_true[i] = loglikelihood_true logposterior_true = logprior_true + loglikelihood_true logposteriors_true[i] = logposterior_true From 254544d1b9f47109f73d947bec4d427516f43dd9 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:41:36 +0000 Subject: [PATCH 037/218] formatted `scr/logp.jl`. --- src/logp.jl | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 0f6d4a4f5..9f9ebdee9 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -58,15 +58,15 @@ function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) DynamicPPL.logprior(model_instance, argvals_dict) end end function logprior(model_instance::Model, nt_arr::Vector{NamedTuple}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(nt_arr, 1) + for param_idx = 1:size(nt_arr, 1) # Compute and store. lls[param_idx] = DynamicPPL.logprior(model_instance, nt_arr[param_idx]) end @@ -74,7 +74,7 @@ function logprior(model_instance::Model, nt_arr::Vector{NamedTuple}) end function logprior(model_instance::Model, nt_arr::Vector{Any}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(nt_arr, 1) + for param_idx = 1:size(nt_arr, 1) # Compute and store. lls[param_idx] = DynamicPPL.logprior(model_instance, nt_arr[param_idx]) end @@ -140,15 +140,15 @@ function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChain vi = VarInfo(model_instance) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) Distributions.loglikelihood(model_instance, argvals_dict) end end function loglikelihoods(model_instance::Model, nt_arr::Vector{NamedTuple}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(nt_arr, 1) + for param_idx = 1:size(nt_arr, 1) # Compute and store. lls[param_idx] = Distributions.loglikelihood(model_instance, nt_arr[param_idx]) end @@ -156,7 +156,7 @@ function loglikelihoods(model_instance::Model, nt_arr::Vector{NamedTuple}) end function loglikelihoods(model_instance::Model, nt_arr::Vector{Any}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(nt_arr, 1) + for param_idx = 1:size(nt_arr, 1) # Compute and store. lls[param_idx] = Distributions.loglikelihood(model_instance, nt_arr[param_idx]) end @@ -222,8 +222,8 @@ function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) Distributions.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) @@ -231,7 +231,7 @@ function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) end function logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(nt_arr, 1) + for param_idx = 1:size(nt_arr, 1) # Compute and store. lls[param_idx] = Distributions.loglikelihood(model_instance, nt_arr[param_idx]) + @@ -241,11 +241,11 @@ function logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) end function logjoint(model_instance::Model, nt_arr::Vector{Any}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(nt_arr, 1) + for param_idx = 1:size(nt_arr, 1) # Compute and store. lls[param_idx] = Distributions.loglikelihood(model_instance, nt_arr[param_idx]) + DynamicPPL.logprior(model_instance, nt_arr[param_idx]) end return lls -end \ No newline at end of file +end From 58cf49204432bb4f83b1bbf66a68dcbccd16ab80 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:43:13 +0000 Subject: [PATCH 038/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 9f9ebdee9..9d9719c43 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -58,8 +58,8 @@ function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) DynamicPPL.logprior(model_instance, argvals_dict) end From 7050e0b275da60ec29aa47574fd35ee358211fd8 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:43:28 +0000 Subject: [PATCH 039/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 9d9719c43..7811ef59a 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -66,7 +66,7 @@ function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) end function logprior(model_instance::Model, nt_arr::Vector{NamedTuple}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx = 1:size(nt_arr, 1) + for param_idx in 1:size(nt_arr, 1) # Compute and store. lls[param_idx] = DynamicPPL.logprior(model_instance, nt_arr[param_idx]) end From 51b3c49d512371895b2fd4308c1c128ad8af2f8f Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:43:39 +0000 Subject: [PATCH 040/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 7811ef59a..85832d412 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -74,7 +74,7 @@ function logprior(model_instance::Model, nt_arr::Vector{NamedTuple}) end function logprior(model_instance::Model, nt_arr::Vector{Any}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx = 1:size(nt_arr, 1) + for param_idx in 1:size(nt_arr, 1) # Compute and store. lls[param_idx] = DynamicPPL.logprior(model_instance, nt_arr[param_idx]) end From 60706376253808608c099294260feef606bef90b Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:43:49 +0000 Subject: [PATCH 041/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 85832d412..84f8e5bf6 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -140,8 +140,8 @@ function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChain vi = VarInfo(model_instance) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) Distributions.loglikelihood(model_instance, argvals_dict) end From 9137dc84e3ad225ff1b3784e2f090db974349af3 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:44:08 +0000 Subject: [PATCH 042/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 84f8e5bf6..b58be52e4 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -148,7 +148,7 @@ function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChain end function loglikelihoods(model_instance::Model, nt_arr::Vector{NamedTuple}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx = 1:size(nt_arr, 1) + for param_idx in 1:size(nt_arr, 1) # Compute and store. lls[param_idx] = Distributions.loglikelihood(model_instance, nt_arr[param_idx]) end From ee04a4661d9a7038649c5e32d4b6dd5603d449e8 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:44:17 +0000 Subject: [PATCH 043/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index b58be52e4..f1d59045f 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -156,7 +156,7 @@ function loglikelihoods(model_instance::Model, nt_arr::Vector{NamedTuple}) end function loglikelihoods(model_instance::Model, nt_arr::Vector{Any}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx = 1:size(nt_arr, 1) + for param_idx in 1:size(nt_arr, 1) # Compute and store. lls[param_idx] = Distributions.loglikelihood(model_instance, nt_arr[param_idx]) end From 6138b254b56d34ee6c3a02445258357d6b392fac Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:44:41 +0000 Subject: [PATCH 044/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index f1d59045f..80d5d8f7e 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -222,8 +222,8 @@ function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) Distributions.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) From c1e9d09e80d482b5f06a1f16b869542d543e4701 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:44:50 +0000 Subject: [PATCH 045/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 80d5d8f7e..728d324e3 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -231,7 +231,7 @@ function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) end function logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx = 1:size(nt_arr, 1) + for param_idx in 1:size(nt_arr, 1) # Compute and store. lls[param_idx] = Distributions.loglikelihood(model_instance, nt_arr[param_idx]) + From 60d4e2cab68a2c89b37dff415a0409e41acc34ea Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:44:59 +0000 Subject: [PATCH 046/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 728d324e3..e56a15533 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -241,7 +241,7 @@ function logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) end function logjoint(model_instance::Model, nt_arr::Vector{Any}) lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx = 1:size(nt_arr, 1) + for param_idx in 1:size(nt_arr, 1) # Compute and store. lls[param_idx] = Distributions.loglikelihood(model_instance, nt_arr[param_idx]) + From c54ddd5b074388b2315791d1f009ce491463fc59 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:54:44 +0000 Subject: [PATCH 047/218] formatted `scr/logp.jl`. --- src/logp.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index e56a15533..4f566bf4e 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -1,6 +1,6 @@ # functions for evaluating logp: log posterior, log likelihood and log prior -#### 1. logprior #### +# 1. logprior """ logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) logprior(model_instance::Model, nt_arr::Vector{NamedTuple}) @@ -82,7 +82,7 @@ function logprior(model_instance::Model, nt_arr::Vector{Any}) end -#### 2. loglikelihood #### +# 2. loglikelihood """ loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains) loglikelihoods(model_instance::Model, nt_arr::Vector{NamedTuple}) @@ -163,7 +163,7 @@ function loglikelihoods(model_instance::Model, nt_arr::Vector{Any}) return lls end -#### 3. logjoint #### +# 3. logjoint """ logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) @@ -217,7 +217,6 @@ Example 2: ⋮ ``` """ -## evaluate log posterior at sample parameter positions function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) From f91c4eabbae2e2b0d719a291c8f563ae129c01f8 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:58:51 +0000 Subject: [PATCH 048/218] Removed comments. --- src/logp.jl | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 4f566bf4e..73cf207aa 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -1,6 +1,3 @@ -# functions for evaluating logp: log posterior, log likelihood and log prior - -# 1. logprior """ logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) logprior(model_instance::Model, nt_arr::Vector{NamedTuple}) @@ -81,8 +78,6 @@ function logprior(model_instance::Model, nt_arr::Vector{Any}) return lls end - -# 2. loglikelihood """ loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains) loglikelihoods(model_instance::Model, nt_arr::Vector{NamedTuple}) @@ -163,7 +158,6 @@ function loglikelihoods(model_instance::Model, nt_arr::Vector{Any}) return lls end -# 3. logjoint """ logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) From 78cc213028b0e82b7d693646626aa905ac601d27 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:02:15 +0000 Subject: [PATCH 049/218] Update src/logp.jl Co-authored-by: David Widmann --- src/logp.jl | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 73cf207aa..9e5b91b0f 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -61,22 +61,6 @@ function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) DynamicPPL.logprior(model_instance, argvals_dict) end end -function logprior(model_instance::Model, nt_arr::Vector{NamedTuple}) - lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(nt_arr, 1) - # Compute and store. - lls[param_idx] = DynamicPPL.logprior(model_instance, nt_arr[param_idx]) - end - return lls -end -function logprior(model_instance::Model, nt_arr::Vector{Any}) - lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(nt_arr, 1) - # Compute and store. - lls[param_idx] = DynamicPPL.logprior(model_instance, nt_arr[param_idx]) - end - return lls -end """ loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains) From ef2110eb8f0161603bbb2d3ae5492027a9124ac4 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:11:47 +0000 Subject: [PATCH 050/218] Update src/logp.jl Co-authored-by: David Widmann --- src/logp.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 9e5b91b0f..f6857cfb4 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -35,11 +35,9 @@ Example 2: ```julia # generate data - sample_array = Vector(undef, 100) m = DynamicPPL.TestUtils.DEMO_MODELS[1] - for i in 1:100 - example_values = rand(NamedTuple, m) - sample_array[i] = example_values + samples = map(1:100) do _ + return rand(NamedTuple, m) end # calculate the pointwise loglikelihoods for the whole array. logprior(m, sample_array) From 262b6c215398d01ea14db79badc11009711cf7b6 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:22:08 +0000 Subject: [PATCH 051/218] Update src/logp.jl Co-authored-by: David Widmann --- src/logp.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index f6857cfb4..e4f01e1ef 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -97,11 +97,9 @@ Example 2: ```julia # generate data - sample_array = Vector(undef, 100) m = DynamicPPL.TestUtils.DEMO_MODELS[1] - for i in 1:100 - example_values = rand(NamedTuple, m) - sample_array[i] = example_values + samples = map(1:100) do _ + return rand(NamedTuple, m) end # calculate the pointwise loglikelihoods for the whole array. loglikelihoods(m, sample_array) From 63e486a3e340fefb089f58d628a4fe31f20732bf Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:22:29 +0000 Subject: [PATCH 052/218] Update src/logp.jl Co-authored-by: David Widmann --- src/logp.jl | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index e4f01e1ef..64a2f4902 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -121,22 +121,6 @@ function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChain Distributions.loglikelihood(model_instance, argvals_dict) end end -function loglikelihoods(model_instance::Model, nt_arr::Vector{NamedTuple}) - lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(nt_arr, 1) - # Compute and store. - lls[param_idx] = Distributions.loglikelihood(model_instance, nt_arr[param_idx]) - end - return lls -end -function loglikelihoods(model_instance::Model, nt_arr::Vector{Any}) - lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(nt_arr, 1) - # Compute and store. - lls[param_idx] = Distributions.loglikelihood(model_instance, nt_arr[param_idx]) - end - return lls -end """ logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) From 6d9a8847b7ec218c14cbd1fbae9d12434407fee3 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:22:43 +0000 Subject: [PATCH 053/218] Update src/logp.jl Co-authored-by: David Widmann --- src/logp.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 64a2f4902..1597524d1 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -159,11 +159,9 @@ Example 2: ```julia # generate data - sample_array = Vector(undef, 100) m = DynamicPPL.TestUtils.DEMO_MODELS[1] - for i in 1:100 - example_values = rand(NamedTuple, m) - sample_array[i] = example_values + samples = map(1:100) do _ + return rand(NamedTuple, m) end # calculate the pointwise loglikelihoods for the whole array. logjoint(m, sample_array) From 3e954be45a913774b85d3b1c166d2a123b4dac47 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:22:56 +0000 Subject: [PATCH 054/218] Update src/logp.jl Co-authored-by: David Widmann --- src/logp.jl | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 1597524d1..92043f72c 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -184,23 +184,3 @@ function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) DynamicPPL.logprior(model_instance, argvals_dict) end end -function logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) - lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(nt_arr, 1) - # Compute and store. - lls[param_idx] = - Distributions.loglikelihood(model_instance, nt_arr[param_idx]) + - DynamicPPL.logprior(model_instance, nt_arr[param_idx]) - end - return lls -end -function logjoint(model_instance::Model, nt_arr::Vector{Any}) - lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(nt_arr, 1) - # Compute and store. - lls[param_idx] = - Distributions.loglikelihood(model_instance, nt_arr[param_idx]) + - DynamicPPL.logprior(model_instance, nt_arr[param_idx]) - end - return lls -end From 7ebcf10044f3bf5bf60fea03d283abcfd5511363 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:23:53 +0000 Subject: [PATCH 055/218] Update test/logp.jl Co-authored-by: David Widmann --- test/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/logp.jl b/test/logp.jl index a5de423b5..f14e7d526 100644 --- a/test/logp.jl +++ b/test/logp.jl @@ -1,5 +1,5 @@ @testset "logp.jl" begin - Test.@testset "$(m.f)" for m in DynamicPPL.TestUtils.DEMO_MODELS + @testset "$(m.f)" for m in DynamicPPL.TestUtils.DEMO_MODELS # generate a chain of sample parameter values. N = 100 chain = Vector(undef, N) From ee82b65d8f466ade9901c7171d135eb0f9e1ebcf Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:33:14 +0000 Subject: [PATCH 056/218] removed redundant methods (NamedTuples and Array inputs). --- src/logp.jl | 217 ++++++++++++++++++++++++---------------------------- 1 file changed, 101 insertions(+), 116 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 92043f72c..01458c203 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -1,53 +1,38 @@ """ logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) - logprior(model_instance::Model, nt_arr::Vector{NamedTuple}) - logprior(model_instance::Model, nt_arr::Vector{Any}) Return an array of log priors evaluated at each sample in an MCMC chain or sample array. -Example 1: - - ```julia - @model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end - end - # generate data - using Random, Distributions, Turing - Random.seed!(111) - x = rand(Normal(1.0, 1.0), 1000) - # construct a chain of samples via MCMC sampling - using Turing - chain = sample(demo_model(x), NUTS(0.65), 3_000) # chain: 1st index is the iteration no, 3rd index is the chain no. - logprior(demo_model(x), chain) +Example: - Outputs: - julia> 3000×1 Matrix{Float64}: - -2.2188656502119937 - -2.233332271475687 +```jldoctest +# generate data +using Random, MCMCChains +Random.seed!(111) +# construct a chain of samples using MCMCChains +val = rand(500, 2, 3) +chain = Chains(val, [:s, :m]) +logprior(demo_model(x), chain) + +# output +julia> logprior(demo_model(x), chain) +500×3 Matrix{Float64}: + -61.6682 -1.70769 -2.11987 + -2.19736 -5.04622 -1.835 + -2.15858 -1.69405 -2.31419 + -1.74928 -66.3069 -1.76168 + -6367.73 -4.74417 -2.21238 + -8.8449 -1.70692 -1.73748 + -5.65338 -2.04857 -4.46512 ⋮ - ``` - -Example 2: - - ```julia - # generate data - m = DynamicPPL.TestUtils.DEMO_MODELS[1] - samples = map(1:100) do _ - return rand(NamedTuple, m) - end - # calculate the pointwise loglikelihoods for the whole array. - logprior(m, sample_array) - - Outputs: - julia> 100-element Vector{Float64}: - -3.8653211194703863 - -6.727832990780729 - ⋮ - ``` + -9.09991 -4.5134 -2.5894 + -5.45221 -170.779 -1.97001 + -2.04826 -11.3178 -93.6076 + -13.68 -8.1437 -2.35059 + -4.45329 -1.70161 -1.88288 + -1.94955 -2.53816 -2.84721 + -21.7945 -1.99002 -3.27705 +``` """ function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model @@ -60,56 +45,29 @@ function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) end end + """ loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains) - loglikelihoods(model_instance::Model, nt_arr::Vector{NamedTuple}) - loglikelihoods(model_instance::Model, nt_arr::Vector{Any}) Return an array of log likelihoods evaluated at each sample in an MCMC chain or sample array. -Example 1: +Example: - ```julia - @model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end - end - # generate data - using Random, Distributions, Turing - Random.seed!(111) - x = rand(Normal(1.0, 1.0), 1000) - # construct a chain of samples via MCMC sampling - using Turing - chain = sample(demo_model(x), NUTS(0.65), 3_000) # chain: 1st index is the iteration no, 3rd index is the chain no. - loglikelihoods(demo_model(x), chain) - - Outputs: - julia> 3000×1 Matrix{Float64}: - -1460.969366266108 - -1460.115380195131 - ⋮ - ``` - -Example 2: - - ```julia - # generate data - m = DynamicPPL.TestUtils.DEMO_MODELS[1] - samples = map(1:100) do _ - return rand(NamedTuple, m) - end - # calculate the pointwise loglikelihoods for the whole array. - loglikelihoods(m, sample_array) - - Outputs: - julia> 100-element Vector{Float64}: - -7.31146824079265 - -3.006725528921822 - ⋮ - ``` +```jldoctest + +# generate data +using Random, MCMCChains +Random.seed!(111) +val = rand(500, 2, 3) +chain = Chains(val, [:s, :m]) +loglikelihoods(demo_model(x), chain) + +# output +julia> 3000×1 Matrix{Float64}: +-1460.969366266108 +-1460.115380195131 +⋮ +``` """ function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model @@ -122,6 +80,7 @@ function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChain end end + """ logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) @@ -131,32 +90,34 @@ Return an array of log posteriors evaluated at each sample in an MCMC chain or s Example 1: - ```julia - @model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end - end - # generate data - using Random, Distributions, Turing - Random.seed!(111) - x = rand(Normal(1.0, 1.0), 1000) - # construct a chain of samples via MCMC sampling - using Turing - chain = sample(demo_model(x), NUTS(0.65), 3_000) # chain: 1st index is the iteration no, 3rd index is the chain no. - logjoint(demo_model(x), chain) - - Outputs: - julia> 3000×1 Matrix{Float64}: - -1463.1882319163199 - -1462.3487124666067 - ⋮ - ``` +```jldoctest +# generate data +using Random, MCMCChains +Random.seed!(111) +# construct a chain of samples using MCMCChains +val = rand(500, 2, 3) +chain = Chains(val, [:s, :m]) +logjoint(demo_model(x), chain) + +# output +julia> 3000×1 Matrix{Float64}: +-1463.1882319163199 +-1462.3487124666067 +⋮ +``` Example 2: +<<<<<<< HEAD +```jldoctest +# generate data +m = DynamicPPL.TestUtils.DEMO_MODELS[1] +samples = map(1:100) do _ + return rand(NamedTuple, m) +end +# calculate the pointwise loglikelihoods for the whole array. +logjoint(m, samples) +======= ```julia # generate data m = DynamicPPL.TestUtils.DEMO_MODELS[1] @@ -165,13 +126,14 @@ Example 2: end # calculate the pointwise loglikelihoods for the whole array. logjoint(m, sample_array) +>>>>>>> 7ebcf10044f3bf5bf60fea03d283abcfd5511363 - Outputs: - julia> 100-element Vector{Float64}: - -11.176789360263037 - -9.734558519702551 - ⋮ - ``` +# output +julia> 100-element Vector{Float64}: +-11.176789360263037 + -9.734558519702551 + ⋮ +``` """ function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model @@ -184,3 +146,26 @@ function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) DynamicPPL.logprior(model_instance, argvals_dict) end end +<<<<<<< HEAD +function logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) + lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior + for param_idx in 1:size(nt_arr, 1) + # Compute and store. + lls[param_idx] = + Distributions.loglikelihood(model_instance, nt_arr[param_idx]) + + DynamicPPL.logprior(model_instance, nt_arr[param_idx]) + end + return lls +end +function logjoint(::Model, ::Vector{NamedTuple{Any, T} where T<:Tuple}) + lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior + for param_idx in 1:size(nt_arr, 1) + # Compute and store. + lls[param_idx] = + Distributions.loglikelihood(model_instance, nt_arr[param_idx]) + + DynamicPPL.logprior(model_instance, nt_arr[param_idx]) + end + return lls +end +======= +>>>>>>> 7ebcf10044f3bf5bf60fea03d283abcfd5511363 From 9adb6e7b9091f928328e5b964a0ad6c077890515 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:38:59 +0000 Subject: [PATCH 057/218] added REPL examples to docstrings. --- src/logp.jl | 398 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 293 insertions(+), 105 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 01458c203..755a68698 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -6,32 +6,104 @@ Return an array of log priors evaluated at each sample in an MCMC chain or sampl Example: ```jldoctest -# generate data -using Random, MCMCChains -Random.seed!(111) -# construct a chain of samples using MCMCChains -val = rand(500, 2, 3) -chain = Chains(val, [:s, :m]) -logprior(demo_model(x), chain) - -# output +julia> using Random, MCMCChains + +julia> Random.seed!(111) +MersenneTwister(111) + +julia> val = rand(500, 2, 3) +500×2×3 Array{Float64, 3}: +[:, :, 1] = + 0.390386 0.0837452 + 0.485358 0.637198 + 0.519265 0.575001 + 0.057931 0.606497 + 0.845293 0.867838 + 0.264531 0.646586 + 0.938287 0.610489 + ⋮ + 0.570775 0.36348 + 0.65202 0.371192 + 0.579922 0.57587 + 0.929339 0.968619 + 0.997855 0.177522 + 0.726988 0.0112906 + 0.411814 0.450745 + +[:, :, 2] = + 0.14221 0.816536 + 0.635809 0.422885 + 0.359449 0.0230222 + 0.868051 0.313322 + 0.718046 0.15864 + 0.703807 0.703968 + 0.215787 0.24148 + ⋮ + 0.395223 0.461259 + 0.156243 0.281266 + 0.17801 0.642005 + 0.399439 0.477756 + 0.301772 0.0444831 + 0.514639 0.781145 + 0.994754 0.932154 + +[:, :, 3] = + 0.837316 0.163184 + 0.386275 0.37846 + 0.751263 0.965999 + 0.667769 0.0963822 + 0.0440691 0.71489 + 0.521811 0.0560717 + 0.540227 0.204813 + ⋮ + 0.28595 0.0897536 + 0.769292 0.328548 + 0.984564 0.396496 + 0.142841 0.664338 + 0.185144 0.1697 + 0.318288 0.91384 + 0.683415 0.606488 + +julia> chain = Chains(val, [:s, :m]) # construct a chain of samples using MCMCChains +Chains MCMC chain (500×2×3 Array{Float64, 3}): + +Iterations = 1:1:500 +Number of chains = 3 +Samples per chain = 500 +parameters = s, m + +Summary Statistics + parameters mean std naive_se mcse ess rhat + Symbol Float64 Float64 Float64 Float64 Float64 Float64 + + s 0.4950 0.2858 0.0074 0.0081 1471.9090 0.9987 + m 0.4896 0.2856 0.0074 0.0077 1413.4976 0.9984 + +Quantiles + parameters 2.5% 25.0% 50.0% 75.0% 97.5% + Symbol Float64 Float64 Float64 Float64 Float64 + + s 0.0370 0.2549 0.4820 0.7424 0.9769 + m 0.0241 0.2415 0.4910 0.7389 0.9634 + + julia> logprior(demo_model(x), chain) 500×3 Matrix{Float64}: - -61.6682 -1.70769 -2.11987 - -2.19736 -5.04622 -1.835 - -2.15858 -1.69405 -2.31419 - -1.74928 -66.3069 -1.76168 - -6367.73 -4.74417 -2.21238 - -8.8449 -1.70692 -1.73748 - -5.65338 -2.04857 -4.46512 - ⋮ - -9.09991 -4.5134 -2.5894 - -5.45221 -170.779 -1.97001 - -2.04826 -11.3178 -93.6076 - -13.68 -8.1437 -2.35059 - -4.45329 -1.70161 -1.88288 - -1.94955 -2.53816 -2.84721 - -21.7945 -1.99002 -3.27705 + -3.12323 -15.3349 -1.69905 + -2.79095 -1.99575 -3.34438 + -2.52378 -3.48741 -2.33505 + -43.7125 -1.73901 -1.8079 + -2.12802 -1.75797 -61.6682 + -6.19847 -2.10693 -2.19736 + -1.89469 -7.39229 -2.15858 + ⋮ + -2.1308 -3.33246 -4.84536 + -1.93158 -11.6785 -1.77357 + -2.27373 -10.6917 -1.79414 + -2.19811 -3.30603 -14.4578 + -1.73644 -4.47298 -9.09991 + -1.73246 -2.81886 -5.45221 + -3.14809 -2.15587 -2.04826 ``` """ function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) @@ -54,19 +126,104 @@ Return an array of log likelihoods evaluated at each sample in an MCMC chain or Example: ```jldoctest +julia> using Random, MCMCChains -# generate data -using Random, MCMCChains -Random.seed!(111) -val = rand(500, 2, 3) -chain = Chains(val, [:s, :m]) -loglikelihoods(demo_model(x), chain) - -# output -julia> 3000×1 Matrix{Float64}: --1460.969366266108 --1460.115380195131 -⋮ +julia> Random.seed!(111) +MersenneTwister(111) + +julia> val = rand(500, 2, 3) +500×2×3 Array{Float64, 3}: +[:, :, 1] = + 0.390386 0.0837452 + 0.485358 0.637198 + 0.519265 0.575001 + 0.057931 0.606497 + 0.845293 0.867838 + 0.264531 0.646586 + 0.938287 0.610489 + ⋮ + 0.570775 0.36348 + 0.65202 0.371192 + 0.579922 0.57587 + 0.929339 0.968619 + 0.997855 0.177522 + 0.726988 0.0112906 + 0.411814 0.450745 + +[:, :, 2] = + 0.14221 0.816536 + 0.635809 0.422885 + 0.359449 0.0230222 + 0.868051 0.313322 + 0.718046 0.15864 + 0.703807 0.703968 + 0.215787 0.24148 + ⋮ + 0.395223 0.461259 + 0.156243 0.281266 + 0.17801 0.642005 + 0.399439 0.477756 + 0.301772 0.0444831 + 0.514639 0.781145 + 0.994754 0.932154 + +[:, :, 3] = + 0.837316 0.163184 + 0.386275 0.37846 + 0.751263 0.965999 + 0.667769 0.0963822 + 0.0440691 0.71489 + 0.521811 0.0560717 + 0.540227 0.204813 + ⋮ + 0.28595 0.0897536 + 0.769292 0.328548 + 0.984564 0.396496 + 0.142841 0.664338 + 0.185144 0.1697 + 0.318288 0.91384 + 0.683415 0.606488 + +julia> chain = Chains(val, [:s, :m]) +Chains MCMC chain (500×2×3 Array{Float64, 3}): + +Iterations = 1:1:500 +Number of chains = 3 +Samples per chain = 500 +parameters = s, m + +Summary Statistics + parameters mean std naive_se mcse ess rhat + Symbol Float64 Float64 Float64 Float64 Float64 Float64 + + s 0.4950 0.2858 0.0074 0.0081 1471.9090 0.9987 + m 0.4896 0.2856 0.0074 0.0077 1413.4976 0.9984 + +Quantiles + parameters 2.5% 25.0% 50.0% 75.0% 97.5% + Symbol Float64 Float64 Float64 Float64 Float64 + + s 0.0370 0.2549 0.4820 0.7424 0.9769 + m 0.0241 0.2415 0.4910 0.7389 0.9634 + + +julia> loglikelihoods(demo_model(x), chain) +500×3 Matrix{Float64}: + -2710.79 -3362.82 -1802.48 + -1657.18 -1687.59 -2149.22 + -1665.32 -3022.72 -1403.17 + -8903.99 -1655.76 -2022.45 + -1401.05 -1892.41 -10912.7 + -2259.29 -1471.2 -2335.01 + -1466.4 -3638.66 -2055.84 + ⋮ + -1809.32 -2002.24 -3362.31 + -1722.59 -4619.92 -1685.76 + -1607.6 -3044.81 -1569.36 + -1389.27 -1969.64 -3617.69 + -1721.95 -3367.02 -4443.91 + -2068.38 -1544.86 -1835.49 + -1974.14 -1391.46 -1526.21 ``` """ function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains) @@ -83,57 +240,111 @@ end """ logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) - logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) - logjoint(model_instance::Model, nt_arr::Vector{Any}) Return an array of log posteriors evaluated at each sample in an MCMC chain or sample array. -Example 1: +Example: ```jldoctest -# generate data -using Random, MCMCChains -Random.seed!(111) -# construct a chain of samples using MCMCChains -val = rand(500, 2, 3) -chain = Chains(val, [:s, :m]) -logjoint(demo_model(x), chain) - -# output -julia> 3000×1 Matrix{Float64}: --1463.1882319163199 --1462.3487124666067 -⋮ -``` +julia> using Random, MCMCChains -Example 2: - -<<<<<<< HEAD -```jldoctest -# generate data -m = DynamicPPL.TestUtils.DEMO_MODELS[1] -samples = map(1:100) do _ - return rand(NamedTuple, m) -end -# calculate the pointwise loglikelihoods for the whole array. -logjoint(m, samples) -======= - ```julia - # generate data - m = DynamicPPL.TestUtils.DEMO_MODELS[1] - samples = map(1:100) do _ - return rand(NamedTuple, m) - end - # calculate the pointwise loglikelihoods for the whole array. - logjoint(m, sample_array) ->>>>>>> 7ebcf10044f3bf5bf60fea03d283abcfd5511363 - -# output -julia> 100-element Vector{Float64}: --11.176789360263037 - -9.734558519702551 - ⋮ -``` +julia> Random.seed!(111) +MersenneTwister(111) + +julia> val = rand(500, 2, 3) +500×2×3 Array{Float64, 3}: +[:, :, 1] = + 0.390386 0.0837452 + 0.485358 0.637198 + 0.519265 0.575001 + 0.057931 0.606497 + 0.845293 0.867838 + 0.264531 0.646586 + 0.938287 0.610489 + ⋮ + 0.570775 0.36348 + 0.65202 0.371192 + 0.579922 0.57587 + 0.929339 0.968619 + 0.997855 0.177522 + 0.726988 0.0112906 + 0.411814 0.450745 + +[:, :, 2] = + 0.14221 0.816536 + 0.635809 0.422885 + 0.359449 0.0230222 + 0.868051 0.313322 + 0.718046 0.15864 + 0.703807 0.703968 + 0.215787 0.24148 + ⋮ + 0.395223 0.461259 + 0.156243 0.281266 + 0.17801 0.642005 + 0.399439 0.477756 + 0.301772 0.0444831 + 0.514639 0.781145 + 0.994754 0.932154 + +[:, :, 3] = + 0.837316 0.163184 + 0.386275 0.37846 + 0.751263 0.965999 + 0.667769 0.0963822 + 0.0440691 0.71489 + 0.521811 0.0560717 + 0.540227 0.204813 + ⋮ + 0.28595 0.0897536 + 0.769292 0.328548 + 0.984564 0.396496 + 0.142841 0.664338 + 0.185144 0.1697 + 0.318288 0.91384 + 0.683415 0.606488 + +julia> chain = Chains(val, [:s, :m]) # construct a chain of samples using MCMCChains +Chains MCMC chain (500×2×3 Array{Float64, 3}): + +Iterations = 1:1:500 +Number of chains = 3 +Samples per chain = 500 +parameters = s, m + +Summary Statistics + parameters mean std naive_se mcse ess rhat + Symbol Float64 Float64 Float64 Float64 Float64 Float64 + + s 0.4950 0.2858 0.0074 0.0081 1471.9090 0.9987 + m 0.4896 0.2856 0.0074 0.0077 1413.4976 0.9984 + +Quantiles + parameters 2.5% 25.0% 50.0% 75.0% 97.5% + Symbol Float64 Float64 Float64 Float64 Float64 + + s 0.0370 0.2549 0.4820 0.7424 0.9769 + m 0.0241 0.2415 0.4910 0.7389 0.9634 + + +julia> logjoint(demo_model(x), chain) +500×3 Matrix{Float64}: + -2713.91 -3378.15 -1804.18 + -1659.97 -1689.59 -2152.57 + -1667.85 -3026.21 -1405.5 + -8947.7 -1657.5 -2024.26 + -1403.18 -1894.17 -10974.3 + -2265.48 -1473.3 -2337.2 + -1468.29 -3646.05 -2058.0 + ⋮ + -1811.45 -2005.57 -3367.15 + -1724.52 -4631.6 -1687.54 + -1609.87 -3055.5 -1571.16 + -1391.47 -1972.95 -3632.15 + -1723.69 -3371.49 -4453.01 + -2070.11 -1547.68 -1840.94 + -1977.29 -1393.61 -1528.26 +``` """ function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model @@ -145,27 +356,4 @@ function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) Distributions.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) end -end -<<<<<<< HEAD -function logjoint(model_instance::Model, nt_arr::Vector{NamedTuple}) - lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(nt_arr, 1) - # Compute and store. - lls[param_idx] = - Distributions.loglikelihood(model_instance, nt_arr[param_idx]) + - DynamicPPL.logprior(model_instance, nt_arr[param_idx]) - end - return lls -end -function logjoint(::Model, ::Vector{NamedTuple{Any, T} where T<:Tuple}) - lls = Array{Float64}(undef, size(nt_arr, 1)) # initialize a matrix to store the evaluated log posterior - for param_idx in 1:size(nt_arr, 1) - # Compute and store. - lls[param_idx] = - Distributions.loglikelihood(model_instance, nt_arr[param_idx]) + - DynamicPPL.logprior(model_instance, nt_arr[param_idx]) - end - return lls -end -======= ->>>>>>> 7ebcf10044f3bf5bf60fea03d283abcfd5511363 +end \ No newline at end of file From 68d76aec3636c6050f5fbf2bcf0d8ae219155cdb Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:42:37 +0000 Subject: [PATCH 058/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 755a68698..d16c53053 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -117,7 +117,6 @@ function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) end end - """ loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains) From 3244d2f461775df3561dbc698efa7da6150e7910 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:42:49 +0000 Subject: [PATCH 059/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index d16c53053..23a0cb148 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -236,7 +236,6 @@ function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChain end end - """ logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) From 4a28c2adaaff4624b54cabd4acf772928340be04 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:35:29 +0000 Subject: [PATCH 060/218] added `start_idx` into `src/logp.jl`; rewrite `test/logp.jl` using `map-do`; added `MCMCChains` & `StableRNGs` to `DynamicPPL.jl`. --- src/DynamicPPL.jl | 2 ++ test/logp.jl | 49 +++++++++++++++++------------------------------ 2 files changed, 20 insertions(+), 31 deletions(-) diff --git a/src/DynamicPPL.jl b/src/DynamicPPL.jl index edb65a0c5..c3dba25a8 100644 --- a/src/DynamicPPL.jl +++ b/src/DynamicPPL.jl @@ -1,6 +1,7 @@ module DynamicPPL using AbstractMCMC: AbstractSampler, AbstractChains +using MCMCChains using AbstractPPL using Bijectors using Distributions @@ -18,6 +19,7 @@ using LogDensityProblems: LogDensityProblems using DocStringExtensions using Random: Random +using StableRNGs import Base: Symbol, diff --git a/test/logp.jl b/test/logp.jl index f14e7d526..3fe914293 100644 --- a/test/logp.jl +++ b/test/logp.jl @@ -1,39 +1,26 @@ @testset "logp.jl" begin - @testset "$(m.f)" for m in DynamicPPL.TestUtils.DEMO_MODELS + Test.@testset "$(m.f)" for m in DynamicPPL.TestUtils.DEMO_MODELS # generate a chain of sample parameter values. - N = 100 - chain = Vector(undef, N) - vi_vector = Dict() - logpriors_true = Vector(undef, N) - loglikelihoods_true = Vector(undef, N) - logposteriors_true = Vector(undef, N) - for i in 1:N - # generate samples and extrac vi - example_values = rand(NamedTuple, m) - print(example_values) - chain[i] = example_values - # append!(chain, [example_values]) - # Instantiate a `VarInfo` with the example values. - vi = VarInfo(m) - for vn in DynamicPPL.TestUtils.varnames(m) - vi = DynamicPPL.setindex!!(vi, get(example_values, vn), vn) - end - vi_vector["$i"] = vi + N = 200 + start_idx = 100 + + logpriors_true = Vector{Float64}[undef, N-start_idx] + loglikelihoods_true = Vector{Float64}[undef, N-start_idx] + logposteriors_true = Vector{Float64}[undef, N-start_idx] - # calculate the true pointwise likelihood - logprior_true = DynamicPPL.TestUtils.logprior_true(m, example_values...) - logpriors_true[i] = logprior_true - loglikelihood_true = DynamicPPL.TestUtils.loglikelihood_true( - m, example_values... - ) - loglikelihoods_true[i] = loglikelihood_true - logposterior_true = logprior_true + loglikelihood_true - logposteriors_true[i] = logposterior_true + chain = sample(m, NUTS(), N) + + map(start_idx:N) do i + val = get_params(chain[i, :, :]) + example_values = (m=collect(Iterators.flatten(val.m)), s=collect(Iterators.flatten(val.s))) + logpriors_true[i] = DynamicPPL.TestUtils.logprior_true(m, example_values...) + loglikelihoods_true[i] = DynamicPPL.TestUtils.loglikelihood_true(m, example_values...) + logposteriors_true[i] = logpriors_true[i] + loglikelihoods_true[i] end # calculate the pointwise loglikelihoods for the whole chain using custom logprior. - logpriors_new = logprior(m, chain) - loglikelihoods_new = loglikelihoods(m, chain) - logposteriors_new = logjoint(m, chain) + logpriors_new = logprior(m, chain, start_idx) + loglikelihoods_new = loglikelihoods(m, chain, start_idx) + logposteriors_new = logjoint(m, chain, start_idx) # compare the likelihoods @test logpriors_new ≈ logpriors_true @test loglikelihoods_new ≈ loglikelihoods_true From 96bfbf12d41f0bc170e1241581c486290536791e Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:36:31 +0000 Subject: [PATCH 061/218] added `start_idx` to the 3 methods. --- src/logp.jl | 728 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 468 insertions(+), 260 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 23a0cb148..047edd6c3 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -6,109 +6,181 @@ Return an array of log priors evaluated at each sample in an MCMC chain or sampl Example: ```jldoctest -julia> using Random, MCMCChains +julia> @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end +end +demo_model (generic function with 2 methods) -julia> Random.seed!(111) -MersenneTwister(111) +julia> using StableRNGs, MCMCChains -julia> val = rand(500, 2, 3) +julia> rng = StableRNG(123) +StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) + +julia> val = rand(rng, 500, 2, 3) 500×2×3 Array{Float64, 3}: [:, :, 1] = - 0.390386 0.0837452 - 0.485358 0.637198 - 0.519265 0.575001 - 0.057931 0.606497 - 0.845293 0.867838 - 0.264531 0.646586 - 0.938287 0.610489 + 0.181026 0.263433 + 0.36749 0.762454 + 0.669058 0.94654 + 0.0427306 0.868867 + 0.437991 0.906265 + 0.48329 0.558984 + 0.763974 0.827459 + 0.924483 0.736597 + 0.515045 0.966827 + 0.495042 0.114093 + 0.70134 0.489265 + 0.693569 0.867234 + 0.876273 0.987065 + 0.944219 0.482919 + 0.739213 0.824262 ⋮ - 0.570775 0.36348 - 0.65202 0.371192 - 0.579922 0.57587 - 0.929339 0.968619 - 0.997855 0.177522 - 0.726988 0.0112906 - 0.411814 0.450745 + 0.103866 0.494221 + 0.47533 0.864538 + 0.97594 0.638122 + 0.063996 0.699831 + 0.00780044 0.395067 + 0.755876 0.400533 + 0.2739 0.175565 + 0.973318 0.337066 + 0.930787 0.942264 + 0.146077 0.459817 + 0.410755 0.0794176 + 0.770016 0.312727 + 0.676963 0.892313 + 0.61136 0.0634304 + 0.0768056 0.606559 [:, :, 2] = - 0.14221 0.816536 - 0.635809 0.422885 - 0.359449 0.0230222 - 0.868051 0.313322 - 0.718046 0.15864 - 0.703807 0.703968 - 0.215787 0.24148 + 0.124993 0.112357 + 0.598843 0.398668 + 0.682362 0.283058 + 0.0241038 0.165438 + 0.308107 0.980733 + 0.782956 0.274585 + 0.21124 0.772331 + 0.435111 0.104037 + 0.610605 0.151797 + 0.737831 0.426415 + 0.338639 0.343338 + 0.0810963 0.199654 + 0.858859 0.923886 + 0.847797 0.0127262 + 0.553432 0.397772 ⋮ - 0.395223 0.461259 - 0.156243 0.281266 - 0.17801 0.642005 - 0.399439 0.477756 - 0.301772 0.0444831 - 0.514639 0.781145 - 0.994754 0.932154 + 0.925461 0.731948 + 0.972514 0.161052 + 0.0606383 0.401677 + 0.211883 0.822142 + 0.710227 0.564018 + 0.991651 0.290745 + 0.401151 0.517758 + 0.52277 0.701787 + 0.631634 0.533718 + 0.544402 0.132847 + 0.706127 0.274981 + 0.372036 0.986048 + 0.943445 0.329579 + 0.203314 0.886782 + 0.553447 0.164148 [:, :, 3] = - 0.837316 0.163184 - 0.386275 0.37846 - 0.751263 0.965999 - 0.667769 0.0963822 - 0.0440691 0.71489 - 0.521811 0.0560717 - 0.540227 0.204813 + 0.950801 0.432819 + 0.457132 0.480515 + 0.358365 0.027524 + 0.0906524 0.682524 + 0.0906396 0.646061 + 0.0944409 0.428408 + 0.880841 0.788028 + 0.935565 0.574513 + 0.505625 0.382395 + 0.581101 0.709303 + 0.405819 0.286478 + 0.465678 0.423926 + 0.668932 0.831664 + 0.641558 0.0827791 + 0.311849 0.848705 ⋮ - 0.28595 0.0897536 - 0.769292 0.328548 - 0.984564 0.396496 - 0.142841 0.664338 - 0.185144 0.1697 - 0.318288 0.91384 - 0.683415 0.606488 - -julia> chain = Chains(val, [:s, :m]) # construct a chain of samples using MCMCChains -Chains MCMC chain (500×2×3 Array{Float64, 3}): - -Iterations = 1:1:500 -Number of chains = 3 -Samples per chain = 500 -parameters = s, m - -Summary Statistics - parameters mean std naive_se mcse ess rhat - Symbol Float64 Float64 Float64 Float64 Float64 Float64 - - s 0.4950 0.2858 0.0074 0.0081 1471.9090 0.9987 - m 0.4896 0.2856 0.0074 0.0077 1413.4976 0.9984 - -Quantiles - parameters 2.5% 25.0% 50.0% 75.0% 97.5% - Symbol Float64 Float64 Float64 Float64 Float64 - - s 0.0370 0.2549 0.4820 0.7424 0.9769 - m 0.0241 0.2415 0.4910 0.7389 0.9634 - + 0.652521 0.367347 + 0.52651 0.336222 + 0.176594 0.973352 + 0.748731 0.754104 + 0.0619507 0.0665547 + 0.27748 0.408158 + 0.155415 0.816216 + 0.273364 0.55913 + 0.197207 0.372033 + 0.248851 0.812108 + 0.57863 0.970562 + 0.530468 0.809154 + 0.438612 0.339707 + 0.860922 0.876219 + 0.598552 0.458746 + + julia> chain = Chains(val, [:s, :m]) # construct a chain of samples using MCMCChains + Chains MCMC chain (500×2×3 Array{Float64, 3}): + + Iterations = 1:1:500 + Number of chains = 3 + Samples per chain = 500 + parameters = s, m + + Summary Statistics + parameters mean std naive_se mcse ess rhat + Symbol Float64 Float64 Float64 Float64 Float64 Float64 + + s 0.4995 0.2832 0.0073 0.0066 1433.4683 0.9986 + m 0.5059 0.2962 0.0076 0.0073 1640.8136 0.9992 + + Quantiles + parameters 2.5% 25.0% 50.0% 75.0% 97.5% + Symbol Float64 Float64 Float64 Float64 Float64 + + s 0.0244 0.2641 0.5018 0.7469 0.9716 + m 0.0235 0.2453 0.5024 0.7653 0.9746 julia> logprior(demo_model(x), chain) 500×3 Matrix{Float64}: - -3.12323 -15.3349 -1.69905 - -2.79095 -1.99575 -3.34438 - -2.52378 -3.48741 -2.33505 - -43.7125 -1.73901 -1.8079 - -2.12802 -1.75797 -61.6682 - -6.19847 -2.10693 -2.19736 - -1.89469 -7.39229 -2.15858 - ⋮ - -2.1308 -3.33246 -4.84536 - -1.93158 -11.6785 -1.77357 - -2.27373 -10.6917 -1.79414 - -2.19811 -3.30603 -14.4578 - -1.73644 -4.47298 -9.09991 - -1.73246 -2.81886 -5.45221 - -3.14809 -2.15587 -2.04826 + -9.50373 -15.4953 -1.79888 + -4.17245 -2.06943 -2.79718 + -2.46858 -1.83923 -3.50242 + -66.7277 -110.712 -25.982 + -3.61932 -5.89889 -25.7193 + -2.70745 -1.74512 -23.2001 + -2.15438 -8.89379 -2.03597 + -1.9854 -2.71641 -1.87162 + -3.13164 -1.92717 -2.4127 + -2.33406 -1.84676 -2.41731 + -1.92823 -3.96488 -3.05882 + -2.30869 -27.1681 -2.68198 + -2.23897 -2.17911 -2.31621 + -1.82155 -1.68249 -1.84968 + -2.18204 -2.21472 -5.41831 + ⋮ + -20.8545 -1.98167 -1.92848 + -3.21623 -1.72229 -2.28177 + -1.91905 -39.7158 -12.3236 + -39.8049 -9.0445 -2.09544 + -376.333 -1.97207 -37.4481 + -1.81717 -1.76025 -5.34647 + -5.19842 -3.33737 -13.6525 + -1.76766 -2.66128 -5.72858 + -2.1707 -2.08873 -8.60283 + -13.2499 -2.12032 -7.23408 + -2.91886 -1.80592 -2.80554 + -1.76654 -4.6315 -2.77524 + -2.37587 -1.75536 -2.80853 + -1.90986 -9.83558 -2.12811 + -31.1938 -2.09607 -2.11326 ``` """ -function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) +function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1) vi = VarInfo(model_instance) # extract variables info from the model - map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) + map(Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) @@ -125,109 +197,182 @@ Return an array of log likelihoods evaluated at each sample in an MCMC chain or Example: ```jldoctest -julia> using Random, MCMCChains +julia> @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end +end +demo_model (generic function with 2 methods) + +julia> using StableRNGs, MCMCChains -julia> Random.seed!(111) -MersenneTwister(111) +julia> rng = StableRNG(123) +StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) -julia> val = rand(500, 2, 3) +julia> val = rand(rng, 500, 2, 3) 500×2×3 Array{Float64, 3}: [:, :, 1] = - 0.390386 0.0837452 - 0.485358 0.637198 - 0.519265 0.575001 - 0.057931 0.606497 - 0.845293 0.867838 - 0.264531 0.646586 - 0.938287 0.610489 + 0.181026 0.263433 + 0.36749 0.762454 + 0.669058 0.94654 + 0.0427306 0.868867 + 0.437991 0.906265 + 0.48329 0.558984 + 0.763974 0.827459 + 0.924483 0.736597 + 0.515045 0.966827 + 0.495042 0.114093 + 0.70134 0.489265 + 0.693569 0.867234 + 0.876273 0.987065 + 0.944219 0.482919 + 0.739213 0.824262 ⋮ - 0.570775 0.36348 - 0.65202 0.371192 - 0.579922 0.57587 - 0.929339 0.968619 - 0.997855 0.177522 - 0.726988 0.0112906 - 0.411814 0.450745 + 0.103866 0.494221 + 0.47533 0.864538 + 0.97594 0.638122 + 0.063996 0.699831 + 0.00780044 0.395067 + 0.755876 0.400533 + 0.2739 0.175565 + 0.973318 0.337066 + 0.930787 0.942264 + 0.146077 0.459817 + 0.410755 0.0794176 + 0.770016 0.312727 + 0.676963 0.892313 + 0.61136 0.0634304 + 0.0768056 0.606559 [:, :, 2] = - 0.14221 0.816536 - 0.635809 0.422885 - 0.359449 0.0230222 - 0.868051 0.313322 - 0.718046 0.15864 - 0.703807 0.703968 - 0.215787 0.24148 + 0.124993 0.112357 + 0.598843 0.398668 + 0.682362 0.283058 + 0.0241038 0.165438 + 0.308107 0.980733 + 0.782956 0.274585 + 0.21124 0.772331 + 0.435111 0.104037 + 0.610605 0.151797 + 0.737831 0.426415 + 0.338639 0.343338 + 0.0810963 0.199654 + 0.858859 0.923886 + 0.847797 0.0127262 + 0.553432 0.397772 ⋮ - 0.395223 0.461259 - 0.156243 0.281266 - 0.17801 0.642005 - 0.399439 0.477756 - 0.301772 0.0444831 - 0.514639 0.781145 - 0.994754 0.932154 + 0.925461 0.731948 + 0.972514 0.161052 + 0.0606383 0.401677 + 0.211883 0.822142 + 0.710227 0.564018 + 0.991651 0.290745 + 0.401151 0.517758 + 0.52277 0.701787 + 0.631634 0.533718 + 0.544402 0.132847 + 0.706127 0.274981 + 0.372036 0.986048 + 0.943445 0.329579 + 0.203314 0.886782 + 0.553447 0.164148 [:, :, 3] = - 0.837316 0.163184 - 0.386275 0.37846 - 0.751263 0.965999 - 0.667769 0.0963822 - 0.0440691 0.71489 - 0.521811 0.0560717 - 0.540227 0.204813 + 0.950801 0.432819 + 0.457132 0.480515 + 0.358365 0.027524 + 0.0906524 0.682524 + 0.0906396 0.646061 + 0.0944409 0.428408 + 0.880841 0.788028 + 0.935565 0.574513 + 0.505625 0.382395 + 0.581101 0.709303 + 0.405819 0.286478 + 0.465678 0.423926 + 0.668932 0.831664 + 0.641558 0.0827791 + 0.311849 0.848705 ⋮ - 0.28595 0.0897536 - 0.769292 0.328548 - 0.984564 0.396496 - 0.142841 0.664338 - 0.185144 0.1697 - 0.318288 0.91384 - 0.683415 0.606488 - -julia> chain = Chains(val, [:s, :m]) -Chains MCMC chain (500×2×3 Array{Float64, 3}): - -Iterations = 1:1:500 -Number of chains = 3 -Samples per chain = 500 -parameters = s, m - -Summary Statistics - parameters mean std naive_se mcse ess rhat - Symbol Float64 Float64 Float64 Float64 Float64 Float64 - - s 0.4950 0.2858 0.0074 0.0081 1471.9090 0.9987 - m 0.4896 0.2856 0.0074 0.0077 1413.4976 0.9984 - -Quantiles - parameters 2.5% 25.0% 50.0% 75.0% 97.5% - Symbol Float64 Float64 Float64 Float64 Float64 - - s 0.0370 0.2549 0.4820 0.7424 0.9769 - m 0.0241 0.2415 0.4910 0.7389 0.9634 + 0.652521 0.367347 + 0.52651 0.336222 + 0.176594 0.973352 + 0.748731 0.754104 + 0.0619507 0.0665547 + 0.27748 0.408158 + 0.155415 0.816216 + 0.273364 0.55913 + 0.197207 0.372033 + 0.248851 0.812108 + 0.57863 0.970562 + 0.530468 0.809154 + 0.438612 0.339707 + 0.860922 0.876219 + 0.598552 0.458746 + + julia> chain = Chains(val, [:s, :m]) + Chains MCMC chain (500×2×3 Array{Float64, 3}): + + Iterations = 1:1:500 + Number of chains = 3 + Samples per chain = 500 + parameters = s, m + + Summary Statistics + parameters mean std naive_se mcse ess rhat + Symbol Float64 Float64 Float64 Float64 Float64 Float64 + + s 0.4995 0.2832 0.0073 0.0066 1433.4683 0.9986 + m 0.5059 0.2962 0.0076 0.0073 1640.8136 0.9992 + + Quantiles + parameters 2.5% 25.0% 50.0% 75.0% 97.5% + Symbol Float64 Float64 Float64 Float64 Float64 + + s 0.0244 0.2641 0.5018 0.7469 0.9716 + m 0.0235 0.2453 0.5024 0.7653 0.9746 julia> loglikelihoods(demo_model(x), chain) 500×3 Matrix{Float64}: - -2710.79 -3362.82 -1802.48 - -1657.18 -1687.59 -2149.22 - -1665.32 -3022.72 -1403.17 - -8903.99 -1655.76 -2022.45 - -1401.05 -1892.41 -10912.7 - -2259.29 -1471.2 -2335.01 - -1466.4 -3638.66 -2055.84 - ⋮ - -1809.32 -2002.24 -3362.31 - -1722.59 -4619.92 -1685.76 - -1607.6 -3044.81 -1569.36 - -1389.27 -1969.64 -3617.69 - -1721.95 -3367.02 -4443.91 - -2068.38 -1544.86 -1835.49 - -1974.14 -1391.46 -1526.21 + -4130.67 -6739.94 -1553.24 + -1771.29 -1742.56 -1843.49 + -1423.32 -1785.93 -3016.99 + -10539.0 -32755.2 -5440.33 + -1589.6 -1858.79 -5572.8 + -1723.45 -1726.46 -6405.19 + -1418.35 -2484.64 -1413.64 + -1424.23 -2490.62 -1481.99 + -1502.04 -2021.18 -1876.42 + -2296.57 -1621.7 -1526.49 + -1592.96 -2388.81 -2241.18 + -1426.09 -9337.64 -1894.17 + -1390.28 -1393.81 -1440.99 + -1526.04 -1957.08 -2074.9 + -1423.83 -1792.71 -1879.09 + ⋮ + -5511.73 -1425.48 -1725.86 + -1554.73 -1743.97 -1900.62 + -1453.32 -10153.9 -2719.41 + -7568.23 -2433.33 -1440.87 + -81679.2 -1539.65 -14037.7 + -1633.17 -1637.36 -2588.62 + -3206.67 -1916.08 -3117.08 + -1609.38 -1575.79 -2335.33 + -1390.29 -1600.78 -3468.65 + -4149.14 -2157.4 -2180.55 + -2633.63 -1775.59 -1459.57 + -1699.18 -1690.29 -1521.04 + -1426.75 -1621.31 -2065.1 + -2147.92 -2465.56 -1398.76 + -6732.4 -2092.74 -1686.33 ``` """ -function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains) +function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1) vi = VarInfo(model_instance) # extract variables info from the model - map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) + map(Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) @@ -244,109 +389,172 @@ Return an array of log posteriors evaluated at each sample in an MCMC chain or s Example: ```jldoctest -julia> using Random, MCMCChains +julia> using StableRNGs, MCMCChains -julia> Random.seed!(111) -MersenneTwister(111) +julia> rng = StableRNG(123) +StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) -julia> val = rand(500, 2, 3) +julia> val = rand(rng, 500, 2, 3) 500×2×3 Array{Float64, 3}: [:, :, 1] = - 0.390386 0.0837452 - 0.485358 0.637198 - 0.519265 0.575001 - 0.057931 0.606497 - 0.845293 0.867838 - 0.264531 0.646586 - 0.938287 0.610489 + 0.181026 0.263433 + 0.36749 0.762454 + 0.669058 0.94654 + 0.0427306 0.868867 + 0.437991 0.906265 + 0.48329 0.558984 + 0.763974 0.827459 + 0.924483 0.736597 + 0.515045 0.966827 + 0.495042 0.114093 + 0.70134 0.489265 + 0.693569 0.867234 + 0.876273 0.987065 + 0.944219 0.482919 + 0.739213 0.824262 ⋮ - 0.570775 0.36348 - 0.65202 0.371192 - 0.579922 0.57587 - 0.929339 0.968619 - 0.997855 0.177522 - 0.726988 0.0112906 - 0.411814 0.450745 + 0.103866 0.494221 + 0.47533 0.864538 + 0.97594 0.638122 + 0.063996 0.699831 + 0.00780044 0.395067 + 0.755876 0.400533 + 0.2739 0.175565 + 0.973318 0.337066 + 0.930787 0.942264 + 0.146077 0.459817 + 0.410755 0.0794176 + 0.770016 0.312727 + 0.676963 0.892313 + 0.61136 0.0634304 + 0.0768056 0.606559 [:, :, 2] = - 0.14221 0.816536 - 0.635809 0.422885 - 0.359449 0.0230222 - 0.868051 0.313322 - 0.718046 0.15864 - 0.703807 0.703968 - 0.215787 0.24148 + 0.124993 0.112357 + 0.598843 0.398668 + 0.682362 0.283058 + 0.0241038 0.165438 + 0.308107 0.980733 + 0.782956 0.274585 + 0.21124 0.772331 + 0.435111 0.104037 + 0.610605 0.151797 + 0.737831 0.426415 + 0.338639 0.343338 + 0.0810963 0.199654 + 0.858859 0.923886 + 0.847797 0.0127262 + 0.553432 0.397772 ⋮ - 0.395223 0.461259 - 0.156243 0.281266 - 0.17801 0.642005 - 0.399439 0.477756 - 0.301772 0.0444831 - 0.514639 0.781145 - 0.994754 0.932154 + 0.925461 0.731948 + 0.972514 0.161052 + 0.0606383 0.401677 + 0.211883 0.822142 + 0.710227 0.564018 + 0.991651 0.290745 + 0.401151 0.517758 + 0.52277 0.701787 + 0.631634 0.533718 + 0.544402 0.132847 + 0.706127 0.274981 + 0.372036 0.986048 + 0.943445 0.329579 + 0.203314 0.886782 + 0.553447 0.164148 [:, :, 3] = - 0.837316 0.163184 - 0.386275 0.37846 - 0.751263 0.965999 - 0.667769 0.0963822 - 0.0440691 0.71489 - 0.521811 0.0560717 - 0.540227 0.204813 + 0.950801 0.432819 + 0.457132 0.480515 + 0.358365 0.027524 + 0.0906524 0.682524 + 0.0906396 0.646061 + 0.0944409 0.428408 + 0.880841 0.788028 + 0.935565 0.574513 + 0.505625 0.382395 + 0.581101 0.709303 + 0.405819 0.286478 + 0.465678 0.423926 + 0.668932 0.831664 + 0.641558 0.0827791 + 0.311849 0.848705 ⋮ - 0.28595 0.0897536 - 0.769292 0.328548 - 0.984564 0.396496 - 0.142841 0.664338 - 0.185144 0.1697 - 0.318288 0.91384 - 0.683415 0.606488 - -julia> chain = Chains(val, [:s, :m]) # construct a chain of samples using MCMCChains -Chains MCMC chain (500×2×3 Array{Float64, 3}): - -Iterations = 1:1:500 -Number of chains = 3 -Samples per chain = 500 -parameters = s, m - -Summary Statistics - parameters mean std naive_se mcse ess rhat - Symbol Float64 Float64 Float64 Float64 Float64 Float64 - - s 0.4950 0.2858 0.0074 0.0081 1471.9090 0.9987 - m 0.4896 0.2856 0.0074 0.0077 1413.4976 0.9984 - -Quantiles - parameters 2.5% 25.0% 50.0% 75.0% 97.5% - Symbol Float64 Float64 Float64 Float64 Float64 - - s 0.0370 0.2549 0.4820 0.7424 0.9769 - m 0.0241 0.2415 0.4910 0.7389 0.9634 - - -julia> logjoint(demo_model(x), chain) -500×3 Matrix{Float64}: - -2713.91 -3378.15 -1804.18 - -1659.97 -1689.59 -2152.57 - -1667.85 -3026.21 -1405.5 - -8947.7 -1657.5 -2024.26 - -1403.18 -1894.17 -10974.3 - -2265.48 -1473.3 -2337.2 - -1468.29 -3646.05 -2058.0 - ⋮ - -1811.45 -2005.57 -3367.15 - -1724.52 -4631.6 -1687.54 - -1609.87 -3055.5 -1571.16 - -1391.47 -1972.95 -3632.15 - -1723.69 -3371.49 -4453.01 - -2070.11 -1547.68 -1840.94 - -1977.29 -1393.61 -1528.26 + 0.652521 0.367347 + 0.52651 0.336222 + 0.176594 0.973352 + 0.748731 0.754104 + 0.0619507 0.0665547 + 0.27748 0.408158 + 0.155415 0.816216 + 0.273364 0.55913 + 0.197207 0.372033 + 0.248851 0.812108 + 0.57863 0.970562 + 0.530468 0.809154 + 0.438612 0.339707 + 0.860922 0.876219 + 0.598552 0.458746 + + julia> chain = Chains(val, [:s, :m]) # construct a chain of samples using MCMCChains + Chains MCMC chain (500×2×3 Array{Float64, 3}): + + Iterations = 1:1:500 + Number of chains = 3 + Samples per chain = 500 + parameters = s, m + + Summary Statistics + parameters mean std naive_se mcse ess rhat + Symbol Float64 Float64 Float64 Float64 Float64 Float64 + + s 0.4995 0.2832 0.0073 0.0066 1433.4683 0.9986 + m 0.5059 0.2962 0.0076 0.0073 1640.8136 0.9992 + + Quantiles + parameters 2.5% 25.0% 50.0% 75.0% 97.5% + Symbol Float64 Float64 Float64 Float64 Float64 + + s 0.0244 0.2641 0.5018 0.7469 0.9716 + m 0.0235 0.2453 0.5024 0.7653 0.9746 + +julia> logjoint(demo_model(x), chain, 100) +401×3 Matrix{Float64}: + -2025.91 -1458.8 -1536.82 + -1700.44 -2697.42 -1531.88 + -1618.02 -3476.19 -1550.19 + -2094.43 -1774.56 -2739.01 + -1701.21 -1543.22 -1434.27 + -3252.53 -1655.19 -2350.89 + -1407.15 -1476.78 -4667.16 + -1392.67 -1910.5 -1730.32 + -3775.11 -1804.75 -1535.41 + -1602.54 -1679.16 -1932.07 + -1703.7 -2608.63 -1505.0 + -1401.68 -2128.77 -1460.04 + -1418.54 -1409.34 -3038.4 + -1702.97 -1586.78 -1758.35 + -5507.52 -1674.26 -1874.96 + ⋮ + -5532.58 -1427.46 -1727.79 + -1557.94 -1745.69 -1902.9 + -1455.24 -10193.6 -2731.73 + -7608.04 -2442.37 -1442.97 + -82055.5 -1541.63 -14075.2 + -1634.99 -1639.12 -2593.97 + -3211.87 -1919.42 -3130.73 + -1611.15 -1578.45 -2341.06 + -1392.46 -1602.87 -3477.25 + -4162.39 -2159.52 -2187.78 + -2636.55 -1777.4 -1462.37 + -1700.94 -1694.92 -1523.81 + -1429.13 -1623.07 -2067.91 + -2149.83 -2475.39 -1400.89 + -6763.59 -2094.83 -1688.45 ``` """ -function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) +function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1) vi = VarInfo(model_instance) # extract variables info from the model - map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) + map(Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) From 8b75a8c382dab798d181f8a4bf78fe6be1ffcbff Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:44:04 +0000 Subject: [PATCH 062/218] Reduced chainn size in the docstrings example. --- src/logp.jl | 616 ++++++++++++++++------------------------------------ 1 file changed, 182 insertions(+), 434 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 047edd6c3..531806d08 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -20,162 +20,78 @@ julia> using StableRNGs, MCMCChains julia> rng = StableRNG(123) StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) -julia> val = rand(rng, 500, 2, 3) -500×2×3 Array{Float64, 3}: +julia> val = rand(rng, 10, 2, 3) +10×2×3 Array{Float64, 3}: [:, :, 1] = - 0.181026 0.263433 - 0.36749 0.762454 - 0.669058 0.94654 - 0.0427306 0.868867 - 0.437991 0.906265 - 0.48329 0.558984 - 0.763974 0.827459 - 0.924483 0.736597 - 0.515045 0.966827 - 0.495042 0.114093 - 0.70134 0.489265 - 0.693569 0.867234 - 0.876273 0.987065 - 0.944219 0.482919 - 0.739213 0.824262 - ⋮ - 0.103866 0.494221 - 0.47533 0.864538 - 0.97594 0.638122 - 0.063996 0.699831 - 0.00780044 0.395067 - 0.755876 0.400533 - 0.2739 0.175565 - 0.973318 0.337066 - 0.930787 0.942264 - 0.146077 0.459817 - 0.410755 0.0794176 - 0.770016 0.312727 - 0.676963 0.892313 - 0.61136 0.0634304 - 0.0768056 0.606559 + 0.530608 0.725495 + 0.680607 0.0657651 + 0.301843 0.697142 + 0.696529 0.434764 + 0.677034 0.15186 + 0.579365 0.57197 + 0.662948 0.998378 + 0.187804 0.7701 + 0.871568 0.797581 + 0.111344 0.750877 [:, :, 2] = - 0.124993 0.112357 - 0.598843 0.398668 - 0.682362 0.283058 - 0.0241038 0.165438 - 0.308107 0.980733 - 0.782956 0.274585 - 0.21124 0.772331 - 0.435111 0.104037 - 0.610605 0.151797 - 0.737831 0.426415 - 0.338639 0.343338 - 0.0810963 0.199654 - 0.858859 0.923886 - 0.847797 0.0127262 - 0.553432 0.397772 - ⋮ - 0.925461 0.731948 - 0.972514 0.161052 - 0.0606383 0.401677 - 0.211883 0.822142 - 0.710227 0.564018 - 0.991651 0.290745 - 0.401151 0.517758 - 0.52277 0.701787 - 0.631634 0.533718 - 0.544402 0.132847 - 0.706127 0.274981 - 0.372036 0.986048 - 0.943445 0.329579 - 0.203314 0.886782 - 0.553447 0.164148 + 0.355385 0.778479 + 0.195597 0.297894 + 0.827821 0.760928 + 0.877069 0.617249 + 0.933193 0.968544 + 0.0534829 0.115737 + 0.729433 0.14046 + 0.19727 0.947731 + 0.146534 0.0746781 + 0.516934 0.57386 [:, :, 3] = - 0.950801 0.432819 - 0.457132 0.480515 - 0.358365 0.027524 - 0.0906524 0.682524 - 0.0906396 0.646061 - 0.0944409 0.428408 - 0.880841 0.788028 - 0.935565 0.574513 - 0.505625 0.382395 - 0.581101 0.709303 - 0.405819 0.286478 - 0.465678 0.423926 - 0.668932 0.831664 - 0.641558 0.0827791 - 0.311849 0.848705 - ⋮ - 0.652521 0.367347 - 0.52651 0.336222 - 0.176594 0.973352 - 0.748731 0.754104 - 0.0619507 0.0665547 - 0.27748 0.408158 - 0.155415 0.816216 - 0.273364 0.55913 - 0.197207 0.372033 - 0.248851 0.812108 - 0.57863 0.970562 - 0.530468 0.809154 - 0.438612 0.339707 - 0.860922 0.876219 - 0.598552 0.458746 + 0.832656 0.993766 + 0.245658 0.564889 + 0.881817 0.325685 + 0.182307 0.782469 + 0.884893 0.909196 + 0.517765 0.587938 + 0.39805 0.225796 + 0.151747 0.785742 + 0.817745 0.886162 + 0.321821 0.469467 julia> chain = Chains(val, [:s, :m]) # construct a chain of samples using MCMCChains - Chains MCMC chain (500×2×3 Array{Float64, 3}): - - Iterations = 1:1:500 - Number of chains = 3 - Samples per chain = 500 - parameters = s, m - - Summary Statistics - parameters mean std naive_se mcse ess rhat - Symbol Float64 Float64 Float64 Float64 Float64 Float64 - - s 0.4995 0.2832 0.0073 0.0066 1433.4683 0.9986 - m 0.5059 0.2962 0.0076 0.0073 1640.8136 0.9992 - - Quantiles - parameters 2.5% 25.0% 50.0% 75.0% 97.5% - Symbol Float64 Float64 Float64 Float64 Float64 - - s 0.0244 0.2641 0.5018 0.7469 0.9716 - m 0.0235 0.2453 0.5024 0.7653 0.9746 +Chains MCMC chain (10×2×3 Array{Float64, 3}): + +Iterations = 1:1:10 +Number of chains = 3 +Samples per chain = 10 +parameters = s, m + +Summary Statistics + parameters mean std naive_se mcse ess rhat + Symbol Float64 Float64 Float64 Float64 Float64 Float64 + + s 0.5122 0.2884 0.0527 0.0477 -1435.6207 0.9789 + m 0.5924 0.2965 0.0541 0.0680 29.9515 1.0764 + +Quantiles + parameters 2.5% 25.0% 50.0% 75.0% 97.5% + Symbol Float64 Float64 Float64 Float64 Float64 + + s 0.0954 0.2094 0.5242 0.7957 0.8982 + m 0.0722 0.3530 0.6572 0.7849 0.9950 julia> logprior(demo_model(x), chain) -500×3 Matrix{Float64}: - -9.50373 -15.4953 -1.79888 - -4.17245 -2.06943 -2.79718 - -2.46858 -1.83923 -3.50242 - -66.7277 -110.712 -25.982 - -3.61932 -5.89889 -25.7193 - -2.70745 -1.74512 -23.2001 - -2.15438 -8.89379 -2.03597 - -1.9854 -2.71641 -1.87162 - -3.13164 -1.92717 -2.4127 - -2.33406 -1.84676 -2.41731 - -1.92823 -3.96488 -3.05882 - -2.30869 -27.1681 -2.68198 - -2.23897 -2.17911 -2.31621 - -1.82155 -1.68249 -1.84968 - -2.18204 -2.21472 -5.41831 - ⋮ - -20.8545 -1.98167 -1.92848 - -3.21623 -1.72229 -2.28177 - -1.91905 -39.7158 -12.3236 - -39.8049 -9.0445 -2.09544 - -376.333 -1.97207 -37.4481 - -1.81717 -1.76025 -5.34647 - -5.19842 -3.33737 -13.6525 - -1.76766 -2.66128 -5.72858 - -2.1707 -2.08873 -8.60283 - -13.2499 -2.12032 -7.23408 - -2.91886 -1.80592 -2.80554 - -1.76654 -4.6315 -2.77524 - -2.37587 -1.75536 -2.80853 - -1.90986 -9.83558 -2.12811 - -31.1938 -2.09607 -2.11326 +10×3 Matrix{Float64}: + -2.65353 -4.39497 -2.2767 + -1.78603 -8.57528 -6.66996 + -5.27324 -2.03405 -1.74373 + -1.89871 -1.9003 -10.8995 + -1.80472 -2.1971 -2.15103 + -2.27175 -44.6902 -2.54584 + -2.56001 -1.74381 -3.09837 +-10.4215 -10.5247 -13.9263 + -2.04761 -12.492 -2.16627 +-20.5141 -2.53425 -4.41793 ``` """ function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1) @@ -189,6 +105,7 @@ function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains, sta end end + """ loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains) @@ -211,163 +128,78 @@ julia> using StableRNGs, MCMCChains julia> rng = StableRNG(123) StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) -julia> val = rand(rng, 500, 2, 3) -500×2×3 Array{Float64, 3}: +julia> val = rand(rng, 10, 2, 3) +10×2×3 Array{Float64, 3}: [:, :, 1] = - 0.181026 0.263433 - 0.36749 0.762454 - 0.669058 0.94654 - 0.0427306 0.868867 - 0.437991 0.906265 - 0.48329 0.558984 - 0.763974 0.827459 - 0.924483 0.736597 - 0.515045 0.966827 - 0.495042 0.114093 - 0.70134 0.489265 - 0.693569 0.867234 - 0.876273 0.987065 - 0.944219 0.482919 - 0.739213 0.824262 - ⋮ - 0.103866 0.494221 - 0.47533 0.864538 - 0.97594 0.638122 - 0.063996 0.699831 - 0.00780044 0.395067 - 0.755876 0.400533 - 0.2739 0.175565 - 0.973318 0.337066 - 0.930787 0.942264 - 0.146077 0.459817 - 0.410755 0.0794176 - 0.770016 0.312727 - 0.676963 0.892313 - 0.61136 0.0634304 - 0.0768056 0.606559 + 0.58566 0.154556 + 0.686969 0.847473 + 0.863187 0.445798 + 0.472695 0.556557 + 0.887645 0.481231 + 0.209934 0.248418 + 0.478046 0.435529 + 0.426059 0.804087 + 0.913847 0.944763 + 0.0162749 0.414059 [:, :, 2] = - 0.124993 0.112357 - 0.598843 0.398668 - 0.682362 0.283058 - 0.0241038 0.165438 - 0.308107 0.980733 - 0.782956 0.274585 - 0.21124 0.772331 - 0.435111 0.104037 - 0.610605 0.151797 - 0.737831 0.426415 - 0.338639 0.343338 - 0.0810963 0.199654 - 0.858859 0.923886 - 0.847797 0.0127262 - 0.553432 0.397772 - ⋮ - 0.925461 0.731948 - 0.972514 0.161052 - 0.0606383 0.401677 - 0.211883 0.822142 - 0.710227 0.564018 - 0.991651 0.290745 - 0.401151 0.517758 - 0.52277 0.701787 - 0.631634 0.533718 - 0.544402 0.132847 - 0.706127 0.274981 - 0.372036 0.986048 - 0.943445 0.329579 - 0.203314 0.886782 - 0.553447 0.164148 + 0.222991 0.929401 + 0.514089 0.766995 + 0.979532 0.171396 + 0.864463 0.617206 + 0.924191 0.371671 + 0.907488 0.534833 + 0.578085 0.288955 + 0.431816 0.199648 + 0.0709966 0.348349 + 0.324199 0.931152 [:, :, 3] = - 0.950801 0.432819 - 0.457132 0.480515 - 0.358365 0.027524 - 0.0906524 0.682524 - 0.0906396 0.646061 - 0.0944409 0.428408 - 0.880841 0.788028 - 0.935565 0.574513 - 0.505625 0.382395 - 0.581101 0.709303 - 0.405819 0.286478 - 0.465678 0.423926 - 0.668932 0.831664 - 0.641558 0.0827791 - 0.311849 0.848705 - ⋮ - 0.652521 0.367347 - 0.52651 0.336222 - 0.176594 0.973352 - 0.748731 0.754104 - 0.0619507 0.0665547 - 0.27748 0.408158 - 0.155415 0.816216 - 0.273364 0.55913 - 0.197207 0.372033 - 0.248851 0.812108 - 0.57863 0.970562 - 0.530468 0.809154 - 0.438612 0.339707 - 0.860922 0.876219 - 0.598552 0.458746 + 0.906302 0.39177 + 0.777222 0.578203 + 0.613677 0.980324 + 0.520658 0.885295 + 0.112626 0.0805126 + 0.117799 0.596072 + 0.505948 0.308587 + 0.893709 0.124035 + 0.225277 0.743494 + 0.377099 0.317035 julia> chain = Chains(val, [:s, :m]) - Chains MCMC chain (500×2×3 Array{Float64, 3}): - - Iterations = 1:1:500 - Number of chains = 3 - Samples per chain = 500 - parameters = s, m - - Summary Statistics - parameters mean std naive_se mcse ess rhat - Symbol Float64 Float64 Float64 Float64 Float64 Float64 - - s 0.4995 0.2832 0.0073 0.0066 1433.4683 0.9986 - m 0.5059 0.2962 0.0076 0.0073 1640.8136 0.9992 - - Quantiles - parameters 2.5% 25.0% 50.0% 75.0% 97.5% - Symbol Float64 Float64 Float64 Float64 Float64 - - s 0.0244 0.2641 0.5018 0.7469 0.9716 - m 0.0235 0.2453 0.5024 0.7653 0.9746 +Chains MCMC chain (10×2×3 Array{Float64, 3}): + +Iterations = 1:1:10 +Number of chains = 3 +Samples per chain = 10 +parameters = s, m + +Summary Statistics + parameters mean std naive_se mcse ess rhat + Symbol Float64 Float64 Float64 Float64 Float64 Float64 + + s 0.5469 0.2973 0.0543 0.0547 30.4571 1.0020 + m 0.5166 0.2751 0.0502 0.0280 86.4365 0.9243 + +Quantiles + parameters 2.5% 25.0% 50.0% 75.0% 97.5% + Symbol Float64 Float64 Float64 Float64 Float64 + s 0.0559 0.3374 0.5174 0.8641 0.9394 + m 0.1121 0.3107 0.4635 0.7611 0.9545 julia> loglikelihoods(demo_model(x), chain) -500×3 Matrix{Float64}: - -4130.67 -6739.94 -1553.24 - -1771.29 -1742.56 -1843.49 - -1423.32 -1785.93 -3016.99 - -10539.0 -32755.2 -5440.33 - -1589.6 -1858.79 -5572.8 - -1723.45 -1726.46 -6405.19 - -1418.35 -2484.64 -1413.64 - -1424.23 -2490.62 -1481.99 - -1502.04 -2021.18 -1876.42 - -2296.57 -1621.7 -1526.49 - -1592.96 -2388.81 -2241.18 - -1426.09 -9337.64 -1894.17 - -1390.28 -1393.81 -1440.99 - -1526.04 -1957.08 -2074.9 - -1423.83 -1792.71 -1879.09 - ⋮ - -5511.73 -1425.48 -1725.86 - -1554.73 -1743.97 -1900.62 - -1453.32 -10153.9 -2719.41 - -7568.23 -2433.33 -1440.87 - -81679.2 -1539.65 -14037.7 - -1633.17 -1637.36 -2588.62 - -3206.67 -1916.08 -3117.08 - -1609.38 -1575.79 -2335.33 - -1390.29 -1600.78 -3468.65 - -4149.14 -2157.4 -2180.55 - -2633.63 -1775.59 -1459.57 - -1699.18 -1690.29 -1521.04 - -1426.75 -1621.31 -2065.1 - -2147.92 -2465.56 -1398.76 - -6732.4 -2092.74 -1686.33 +10×3 Matrix{Float64}: + -2053.82 -2289.0 -1587.9 + -1431.8 -1551.34 -1508.8 + -1563.54 -1732.84 -1442.21 + -1740.78 -1471.96 -1507.89 + -1536.64 -1597.04 -7694.37 + -3697.44 -1504.31 -4511.38 + -1858.51 -1886.68 -1970.19 + -1638.88 -2316.02 -1810.87 + -1390.37 -9144.19 -2400.74 +-38045.7 -1813.85 -2283.61 ``` """ function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1) @@ -381,6 +213,7 @@ function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChain end end + """ logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) @@ -394,162 +227,77 @@ julia> using StableRNGs, MCMCChains julia> rng = StableRNG(123) StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) -julia> val = rand(rng, 500, 2, 3) -500×2×3 Array{Float64, 3}: +julia> val = rand(rng, 10, 2, 3) +10×2×3 Array{Float64, 3}: [:, :, 1] = - 0.181026 0.263433 - 0.36749 0.762454 - 0.669058 0.94654 - 0.0427306 0.868867 - 0.437991 0.906265 - 0.48329 0.558984 - 0.763974 0.827459 - 0.924483 0.736597 - 0.515045 0.966827 - 0.495042 0.114093 - 0.70134 0.489265 - 0.693569 0.867234 - 0.876273 0.987065 - 0.944219 0.482919 - 0.739213 0.824262 - ⋮ - 0.103866 0.494221 - 0.47533 0.864538 - 0.97594 0.638122 - 0.063996 0.699831 - 0.00780044 0.395067 - 0.755876 0.400533 - 0.2739 0.175565 - 0.973318 0.337066 - 0.930787 0.942264 - 0.146077 0.459817 - 0.410755 0.0794176 - 0.770016 0.312727 - 0.676963 0.892313 - 0.61136 0.0634304 - 0.0768056 0.606559 + 0.775123 0.888009 + 0.0348401 0.328242 + 0.0419726 0.0832775 + 0.434804 0.828488 + 0.990519 0.791443 + 0.303966 0.559475 + 0.256094 0.145148 + 0.0708956 0.609441 + 0.9972 0.209455 + 0.215559 0.755402 [:, :, 2] = - 0.124993 0.112357 - 0.598843 0.398668 - 0.682362 0.283058 - 0.0241038 0.165438 - 0.308107 0.980733 - 0.782956 0.274585 - 0.21124 0.772331 - 0.435111 0.104037 - 0.610605 0.151797 - 0.737831 0.426415 - 0.338639 0.343338 - 0.0810963 0.199654 - 0.858859 0.923886 - 0.847797 0.0127262 - 0.553432 0.397772 - ⋮ - 0.925461 0.731948 - 0.972514 0.161052 - 0.0606383 0.401677 - 0.211883 0.822142 - 0.710227 0.564018 - 0.991651 0.290745 - 0.401151 0.517758 - 0.52277 0.701787 - 0.631634 0.533718 - 0.544402 0.132847 - 0.706127 0.274981 - 0.372036 0.986048 - 0.943445 0.329579 - 0.203314 0.886782 - 0.553447 0.164148 + 0.109065 0.33411 + 0.907512 0.715396 + 0.0931766 0.583011 + 0.166632 0.0419226 + 0.344997 0.308126 + 0.771344 0.322862 + 0.90522 0.743365 + 0.692612 0.819923 + 0.650188 0.107918 + 0.458714 0.742119 [:, :, 3] = - 0.950801 0.432819 - 0.457132 0.480515 - 0.358365 0.027524 - 0.0906524 0.682524 - 0.0906396 0.646061 - 0.0944409 0.428408 - 0.880841 0.788028 - 0.935565 0.574513 - 0.505625 0.382395 - 0.581101 0.709303 - 0.405819 0.286478 - 0.465678 0.423926 - 0.668932 0.831664 - 0.641558 0.0827791 - 0.311849 0.848705 - ⋮ - 0.652521 0.367347 - 0.52651 0.336222 - 0.176594 0.973352 - 0.748731 0.754104 - 0.0619507 0.0665547 - 0.27748 0.408158 - 0.155415 0.816216 - 0.273364 0.55913 - 0.197207 0.372033 - 0.248851 0.812108 - 0.57863 0.970562 - 0.530468 0.809154 - 0.438612 0.339707 - 0.860922 0.876219 - 0.598552 0.458746 - - julia> chain = Chains(val, [:s, :m]) # construct a chain of samples using MCMCChains - Chains MCMC chain (500×2×3 Array{Float64, 3}): + 0.398571 0.0512853 + 0.384821 0.408064 + 0.295453 0.0493099 + 0.299926 0.141914 + 0.447855 0.979068 + 0.490272 0.918014 + 0.739946 0.381837 + 0.780228 0.25193 + 0.595156 0.96152 + 0.22909 0.626672 + + julia> chain = Chains(val, [:s, :m]) + Chains MCMC chain (10×2×3 Array{Float64, 3}): - Iterations = 1:1:500 + Iterations = 1:1:10 Number of chains = 3 - Samples per chain = 500 + Samples per chain = 10 parameters = s, m Summary Statistics - parameters mean std naive_se mcse ess rhat - Symbol Float64 Float64 Float64 Float64 Float64 Float64 + parameters mean std naive_se mcse ess rhat + Symbol Float64 Float64 Float64 Float64 Float64 Float64 - s 0.4995 0.2832 0.0073 0.0066 1433.4683 0.9986 - m 0.5059 0.2962 0.0076 0.0073 1640.8136 0.9992 + s 0.4627 0.2972 0.0543 0.0585 42.0294 1.0201 + m 0.4896 0.3117 0.0569 0.0476 95.8121 0.9652 Quantiles parameters 2.5% 25.0% 50.0% 75.0% 97.5% Symbol Float64 Float64 Float64 Float64 Float64 - s 0.0244 0.2641 0.5018 0.7469 0.9716 - m 0.0235 0.2453 0.5024 0.7653 0.9746 - -julia> logjoint(demo_model(x), chain, 100) -401×3 Matrix{Float64}: - -2025.91 -1458.8 -1536.82 - -1700.44 -2697.42 -1531.88 - -1618.02 -3476.19 -1550.19 - -2094.43 -1774.56 -2739.01 - -1701.21 -1543.22 -1434.27 - -3252.53 -1655.19 -2350.89 - -1407.15 -1476.78 -4667.16 - -1392.67 -1910.5 -1730.32 - -3775.11 -1804.75 -1535.41 - -1602.54 -1679.16 -1932.07 - -1703.7 -2608.63 -1505.0 - -1401.68 -2128.77 -1460.04 - -1418.54 -1409.34 -3038.4 - -1702.97 -1586.78 -1758.35 - -5507.52 -1674.26 -1874.96 - ⋮ - -5532.58 -1427.46 -1727.79 - -1557.94 -1745.69 -1902.9 - -1455.24 -10193.6 -2731.73 - -7608.04 -2442.37 -1442.97 - -82055.5 -1541.63 -14075.2 - -1634.99 -1639.12 -2593.97 - -3211.87 -1919.42 -3130.73 - -1611.15 -1578.45 -2341.06 - -1392.46 -1602.87 -3477.25 - -4162.39 -2159.52 -2187.78 - -2636.55 -1777.4 -1462.37 - -1700.94 -1694.92 -1523.81 - -1429.13 -1623.07 -2067.91 - -2149.83 -2475.39 -1400.89 - -6763.59 -2094.83 -1688.45 + s 0.0400 0.2358 0.4167 0.7281 0.9924 + m 0.0473 0.2201 0.4838 0.7524 0.9663 + +julia> logjoint(demo_model(x), chain, 2) +9×3 Matrix{Float64}: + -19148.6 -1433.33 -2111.14 + -20443.2 -5706.89 -3410.79 + -1619.63 -5566.35 -3095.36 + -1411.85 -2432.85 -1572.57 + -2184.95 -1691.38 -1531.74 + -3481.6 -1425.4 -1657.91 + -7302.75 -1438.73 -1750.86 + -1698.33 -2030.47 -1454.15 + -2474.31 -1626.86 -2535.97 ``` """ function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1) From 97d4e949b74e776774a1c6f5444a0da504b9a5ae Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:48:47 +0000 Subject: [PATCH 063/218] applied formatting. --- src/logp.jl | 44 +++++++++++++++++++++++++++++++------------- test/logp.jl | 12 ++++++++---- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 531806d08..53ca17217 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -94,12 +94,18 @@ julia> logprior(demo_model(x), chain) -20.5141 -2.53425 -4.41793 ``` """ -function logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1) +function logprior( + model_instance::Model, + chain::AbstractMCMC.AbstractChains, + start_idx::Int = 1, +) vi = VarInfo(model_instance) # extract variables info from the model - map(Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) + map( + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), + ) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) DynamicPPL.logprior(model_instance, argvals_dict) end @@ -202,12 +208,18 @@ julia> loglikelihoods(demo_model(x), chain) -38045.7 -1813.85 -2283.61 ``` """ -function loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1) +function loglikelihoods( + model_instance::Model, + chain::AbstractMCMC.AbstractChains, + start_idx::Int = 1, +) vi = VarInfo(model_instance) # extract variables info from the model - map(Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) + map( + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), + ) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) Distributions.loglikelihood(model_instance, argvals_dict) end @@ -300,14 +312,20 @@ julia> logjoint(demo_model(x), chain, 2) -2474.31 -1626.86 -2535.97 ``` """ -function logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1) +function logjoint( + model_instance::Model, + chain::AbstractMCMC.AbstractChains, + start_idx::Int = 1, +) vi = VarInfo(model_instance) # extract variables info from the model - map(Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) + map( + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), + ) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) Distributions.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) end -end \ No newline at end of file +end diff --git a/test/logp.jl b/test/logp.jl index 3fe914293..a91639bd4 100644 --- a/test/logp.jl +++ b/test/logp.jl @@ -3,18 +3,22 @@ # generate a chain of sample parameter values. N = 200 start_idx = 100 - + logpriors_true = Vector{Float64}[undef, N-start_idx] loglikelihoods_true = Vector{Float64}[undef, N-start_idx] logposteriors_true = Vector{Float64}[undef, N-start_idx] chain = sample(m, NUTS(), N) - + map(start_idx:N) do i val = get_params(chain[i, :, :]) - example_values = (m=collect(Iterators.flatten(val.m)), s=collect(Iterators.flatten(val.s))) + example_values = ( + m = collect(Iterators.flatten(val.m)), + s = collect(Iterators.flatten(val.s)), + ) logpriors_true[i] = DynamicPPL.TestUtils.logprior_true(m, example_values...) - loglikelihoods_true[i] = DynamicPPL.TestUtils.loglikelihood_true(m, example_values...) + loglikelihoods_true[i] = + DynamicPPL.TestUtils.loglikelihood_true(m, example_values...) logposteriors_true[i] = logpriors_true[i] + loglikelihoods_true[i] end # calculate the pointwise loglikelihoods for the whole chain using custom logprior. From cfc76d29e68ab14230f0734ab75cb0751273ba64 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:52:42 +0000 Subject: [PATCH 064/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 53ca17217..9eea2a9b3 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -95,9 +95,7 @@ julia> logprior(demo_model(x), chain) ``` """ function logprior( - model_instance::Model, - chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1, + model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 ) vi = VarInfo(model_instance) # extract variables info from the model map( From 5217a919d5d74ef0941ac93bf49426ac4f3be392 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:53:01 +0000 Subject: [PATCH 065/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 9eea2a9b3..6e443e29e 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -99,7 +99,7 @@ function logprior( ) vi = VarInfo(model_instance) # extract variables info from the model map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) ) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) From f426418604bc2fe8905eec96d39f25c782f52f92 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:53:14 +0000 Subject: [PATCH 066/218] Update test/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/logp.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/logp.jl b/test/logp.jl index a91639bd4..1d183c283 100644 --- a/test/logp.jl +++ b/test/logp.jl @@ -17,8 +17,9 @@ s = collect(Iterators.flatten(val.s)), ) logpriors_true[i] = DynamicPPL.TestUtils.logprior_true(m, example_values...) - loglikelihoods_true[i] = - DynamicPPL.TestUtils.loglikelihood_true(m, example_values...) + loglikelihoods_true[i] = DynamicPPL.TestUtils.loglikelihood_true( + m, example_values... + ) logposteriors_true[i] = logpriors_true[i] + loglikelihoods_true[i] end # calculate the pointwise loglikelihoods for the whole chain using custom logprior. From ac939aed459202614452b6b0d9f3c19064202bc6 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:53:25 +0000 Subject: [PATCH 067/218] Update test/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/logp.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/logp.jl b/test/logp.jl index 1d183c283..5cef68cd0 100644 --- a/test/logp.jl +++ b/test/logp.jl @@ -13,8 +13,7 @@ map(start_idx:N) do i val = get_params(chain[i, :, :]) example_values = ( - m = collect(Iterators.flatten(val.m)), - s = collect(Iterators.flatten(val.s)), + m=collect(Iterators.flatten(val.m)), s=collect(Iterators.flatten(val.s)) ) logpriors_true[i] = DynamicPPL.TestUtils.logprior_true(m, example_values...) loglikelihoods_true[i] = DynamicPPL.TestUtils.loglikelihood_true( From 9e383da32d5817b3d06697ac86b9d343a2f0619d Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:53:38 +0000 Subject: [PATCH 068/218] Update test/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/logp.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/logp.jl b/test/logp.jl index 5cef68cd0..707cdb30a 100644 --- a/test/logp.jl +++ b/test/logp.jl @@ -4,9 +4,9 @@ N = 200 start_idx = 100 - logpriors_true = Vector{Float64}[undef, N-start_idx] - loglikelihoods_true = Vector{Float64}[undef, N-start_idx] - logposteriors_true = Vector{Float64}[undef, N-start_idx] + logpriors_true = Vector{Float64}[undef, N - start_idx] + loglikelihoods_true = Vector{Float64}[undef, N - start_idx] + logposteriors_true = Vector{Float64}[undef, N - start_idx] chain = sample(m, NUTS(), N) From ee0b63de787f2b7d8505033c171d7c0bc7c83eca Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:53:48 +0000 Subject: [PATCH 069/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 6e443e29e..0472ff82b 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -317,7 +317,7 @@ function logjoint( ) vi = VarInfo(model_instance) # extract variables info from the model map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) ) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) From d4706b5f0e29dd78ec6e46b56faae8199fa94346 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:53:58 +0000 Subject: [PATCH 070/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 0472ff82b..57c60595f 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -311,9 +311,7 @@ julia> logjoint(demo_model(x), chain, 2) ``` """ function logjoint( - model_instance::Model, - chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1, + model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 ) vi = VarInfo(model_instance) # extract variables info from the model map( From 398e0d215a5c3d797c77fcd78a1345f7d534c196 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:54:09 +0000 Subject: [PATCH 071/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logp.jl b/src/logp.jl index 57c60595f..b10a74fce 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -213,7 +213,7 @@ function loglikelihoods( ) vi = VarInfo(model_instance) # extract variables info from the model map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) ) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) From 3b95070a51dd90e5702b8fbad014cfb2c70e24be Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:54:18 +0000 Subject: [PATCH 072/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index b10a74fce..b46a2560c 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -207,9 +207,7 @@ julia> loglikelihoods(demo_model(x), chain) ``` """ function loglikelihoods( - model_instance::Model, - chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1, + model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 ) vi = VarInfo(model_instance) # extract variables info from the model map( From dc8808b1a5d0acb58ce33f0be39547137df5a181 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 14:19:20 +0000 Subject: [PATCH 073/218] upated signatures in docstrings. --- src/logp.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index b46a2560c..130523172 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -1,5 +1,5 @@ """ - logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains) + logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) Return an array of log priors evaluated at each sample in an MCMC chain or sample array. @@ -111,7 +111,7 @@ end """ - loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains) + loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) Return an array of log likelihoods evaluated at each sample in an MCMC chain or sample array. @@ -223,7 +223,7 @@ end """ - logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains) + logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) Return an array of log posteriors evaluated at each sample in an MCMC chain or sample array. From 5c05964a6faccd4ba58e014c53d7854773e7afc4 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 14:24:34 +0000 Subject: [PATCH 074/218] applied formatting. --- src/logp.jl | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 130523172..fba1da19d 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -95,11 +95,13 @@ julia> logprior(demo_model(x), chain) ``` """ function logprior( - model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 + model_instance::Model, + chain::AbstractMCMC.AbstractChains, + start_idx::Int = 1, ) vi = VarInfo(model_instance) # extract variables info from the model map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), ) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) @@ -207,11 +209,13 @@ julia> loglikelihoods(demo_model(x), chain) ``` """ function loglikelihoods( - model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 + model_instance::Model, + chain::AbstractMCMC.AbstractChains, + start_idx::Int = 1, ) vi = VarInfo(model_instance) # extract variables info from the model map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), ) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) @@ -309,11 +313,13 @@ julia> logjoint(demo_model(x), chain, 2) ``` """ function logjoint( - model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 + model_instance::Model, + chain::AbstractMCMC.AbstractChains, + start_idx::Int = 1, ) vi = VarInfo(model_instance) # extract variables info from the model map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), ) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) From 8f40c130e5676b336f989eed140f57fd66a64fe3 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 14:36:40 +0000 Subject: [PATCH 075/218] formatted again. --- src/logp.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index fba1da19d..d72de8000 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -97,7 +97,7 @@ julia> logprior(demo_model(x), chain) function logprior( model_instance::Model, chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1, + start_idx::Int = 1 ) vi = VarInfo(model_instance) # extract variables info from the model map( @@ -211,7 +211,7 @@ julia> loglikelihoods(demo_model(x), chain) function loglikelihoods( model_instance::Model, chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1, + start_idx::Int = 1 ) vi = VarInfo(model_instance) # extract variables info from the model map( @@ -315,7 +315,7 @@ julia> logjoint(demo_model(x), chain, 2) function logjoint( model_instance::Model, chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1, + start_idx::Int = 1 ) vi = VarInfo(model_instance) # extract variables info from the model map( From e8e813c697df13ccbf903d923f6370b1713bade4 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 14:40:39 +0000 Subject: [PATCH 076/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index d72de8000..d1018fc73 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -95,9 +95,7 @@ julia> logprior(demo_model(x), chain) ``` """ function logprior( - model_instance::Model, - chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1 + model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 ) vi = VarInfo(model_instance) # extract variables info from the model map( From af6571d50fdaf47d80467120e9a7295715de575e Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 14:40:57 +0000 Subject: [PATCH 077/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index d1018fc73..7edd07c6e 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -207,9 +207,7 @@ julia> loglikelihoods(demo_model(x), chain) ``` """ function loglikelihoods( - model_instance::Model, - chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1 + model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 ) vi = VarInfo(model_instance) # extract variables info from the model map( From 87994268d0467eb002d3f6adcbc9a59372f7e99e Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 19 Jan 2023 14:41:13 +0000 Subject: [PATCH 078/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 7edd07c6e..feafa3e32 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -309,9 +309,7 @@ julia> logjoint(demo_model(x), chain, 2) ``` """ function logjoint( - model_instance::Model, - chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1 + model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 ) vi = VarInfo(model_instance) # extract variables info from the model map( From 05ae602b0498d0a1217e5cf8b4f9344a79c20213 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 20 Jan 2023 18:30:21 +0000 Subject: [PATCH 079/218] fix doctests setup --- docs/Project.toml | 1 + docs/make.jl | 2 +- src/DynamicPPL.jl | 2 - src/logp.jl | 268 +++++++--------------------------------------- test/runtests.jl | 2 +- 5 files changed, 40 insertions(+), 235 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index 35ae31dba..fd00f1c9c 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -4,6 +4,7 @@ Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b" LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c" +MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d" MLUtils = "f1d291b0-491e-4a28-83b9-f70985020b54" Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46" StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3" diff --git a/docs/make.jl b/docs/make.jl index cdf236655..000ad1e9c 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -4,7 +4,7 @@ using DynamicPPL: AbstractPPL # Doctest setup DocMeta.setdocmeta!( - DynamicPPL, :DocTestSetup, :(using DynamicPPL, Distributions); recursive=true + DynamicPPL, :DocTestSetup, :(using DynamicPPL, Distributions, StableRNGs, MCMCChains); recursive=true ) makedocs(; diff --git a/src/DynamicPPL.jl b/src/DynamicPPL.jl index c3dba25a8..edb65a0c5 100644 --- a/src/DynamicPPL.jl +++ b/src/DynamicPPL.jl @@ -1,7 +1,6 @@ module DynamicPPL using AbstractMCMC: AbstractSampler, AbstractChains -using MCMCChains using AbstractPPL using Bijectors using Distributions @@ -19,7 +18,6 @@ using LogDensityProblems: LogDensityProblems using DocStringExtensions using Random: Random -using StableRNGs import Base: Symbol, diff --git a/src/logp.jl b/src/logp.jl index feafa3e32..af7531bad 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -5,93 +5,25 @@ Return an array of log priors evaluated at each sample in an MCMC chain or sampl Example: -```jldoctest -julia> @model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end -end +```jldoctest; setup = :(using MCMCChains, StableRNGs), strict=false +julia> # + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end + end demo_model (generic function with 2 methods) -julia> using StableRNGs, MCMCChains - julia> rng = StableRNG(123) StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) -julia> val = rand(rng, 10, 2, 3) -10×2×3 Array{Float64, 3}: -[:, :, 1] = - 0.530608 0.725495 - 0.680607 0.0657651 - 0.301843 0.697142 - 0.696529 0.434764 - 0.677034 0.15186 - 0.579365 0.57197 - 0.662948 0.998378 - 0.187804 0.7701 - 0.871568 0.797581 - 0.111344 0.750877 - -[:, :, 2] = - 0.355385 0.778479 - 0.195597 0.297894 - 0.827821 0.760928 - 0.877069 0.617249 - 0.933193 0.968544 - 0.0534829 0.115737 - 0.729433 0.14046 - 0.19727 0.947731 - 0.146534 0.0746781 - 0.516934 0.57386 - -[:, :, 3] = - 0.832656 0.993766 - 0.245658 0.564889 - 0.881817 0.325685 - 0.182307 0.782469 - 0.884893 0.909196 - 0.517765 0.587938 - 0.39805 0.225796 - 0.151747 0.785742 - 0.817745 0.886162 - 0.321821 0.469467 - - julia> chain = Chains(val, [:s, :m]) # construct a chain of samples using MCMCChains -Chains MCMC chain (10×2×3 Array{Float64, 3}): +julia> val = rand(rng, 10, 2, 3); -Iterations = 1:1:10 -Number of chains = 3 -Samples per chain = 10 -parameters = s, m +julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCChains -Summary Statistics - parameters mean std naive_se mcse ess rhat - Symbol Float64 Float64 Float64 Float64 Float64 Float64 - - s 0.5122 0.2884 0.0527 0.0477 -1435.6207 0.9789 - m 0.5924 0.2965 0.0541 0.0680 29.9515 1.0764 - -Quantiles - parameters 2.5% 25.0% 50.0% 75.0% 97.5% - Symbol Float64 Float64 Float64 Float64 Float64 - - s 0.0954 0.2094 0.5242 0.7957 0.8982 - m 0.0722 0.3530 0.6572 0.7849 0.9950 - -julia> logprior(demo_model(x), chain) -10×3 Matrix{Float64}: - -2.65353 -4.39497 -2.2767 - -1.78603 -8.57528 -6.66996 - -5.27324 -2.03405 -1.74373 - -1.89871 -1.9003 -10.8995 - -1.80472 -2.1971 -2.15103 - -2.27175 -44.6902 -2.54584 - -2.56001 -1.74381 -3.09837 --10.4215 -10.5247 -13.9263 - -2.04761 -12.492 -2.16627 --20.5141 -2.53425 -4.41793 +julia> logprior(demo_model([1., 2.]), chain); ``` """ function logprior( @@ -117,93 +49,25 @@ Return an array of log likelihoods evaluated at each sample in an MCMC chain or Example: -```jldoctest -julia> @model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end -end +```jldoctest; setup = :(using MCMCChains, StableRNGs), strict=false +julia> # + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end + end demo_model (generic function with 2 methods) -julia> using StableRNGs, MCMCChains - julia> rng = StableRNG(123) StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) -julia> val = rand(rng, 10, 2, 3) -10×2×3 Array{Float64, 3}: -[:, :, 1] = - 0.58566 0.154556 - 0.686969 0.847473 - 0.863187 0.445798 - 0.472695 0.556557 - 0.887645 0.481231 - 0.209934 0.248418 - 0.478046 0.435529 - 0.426059 0.804087 - 0.913847 0.944763 - 0.0162749 0.414059 - -[:, :, 2] = - 0.222991 0.929401 - 0.514089 0.766995 - 0.979532 0.171396 - 0.864463 0.617206 - 0.924191 0.371671 - 0.907488 0.534833 - 0.578085 0.288955 - 0.431816 0.199648 - 0.0709966 0.348349 - 0.324199 0.931152 - -[:, :, 3] = - 0.906302 0.39177 - 0.777222 0.578203 - 0.613677 0.980324 - 0.520658 0.885295 - 0.112626 0.0805126 - 0.117799 0.596072 - 0.505948 0.308587 - 0.893709 0.124035 - 0.225277 0.743494 - 0.377099 0.317035 - - julia> chain = Chains(val, [:s, :m]) -Chains MCMC chain (10×2×3 Array{Float64, 3}): - -Iterations = 1:1:10 -Number of chains = 3 -Samples per chain = 10 -parameters = s, m - -Summary Statistics - parameters mean std naive_se mcse ess rhat - Symbol Float64 Float64 Float64 Float64 Float64 Float64 +julia> val = rand(rng, 10, 2, 3); - s 0.5469 0.2973 0.0543 0.0547 30.4571 1.0020 - m 0.5166 0.2751 0.0502 0.0280 86.4365 0.9243 +julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCChains -Quantiles - parameters 2.5% 25.0% 50.0% 75.0% 97.5% - Symbol Float64 Float64 Float64 Float64 Float64 - - s 0.0559 0.3374 0.5174 0.8641 0.9394 - m 0.1121 0.3107 0.4635 0.7611 0.9545 - -julia> loglikelihoods(demo_model(x), chain) -10×3 Matrix{Float64}: - -2053.82 -2289.0 -1587.9 - -1431.8 -1551.34 -1508.8 - -1563.54 -1732.84 -1442.21 - -1740.78 -1471.96 -1507.89 - -1536.64 -1597.04 -7694.37 - -3697.44 -1504.31 -4511.38 - -1858.51 -1886.68 -1970.19 - -1638.88 -2316.02 -1810.87 - -1390.37 -9144.19 -2400.74 --38045.7 -1813.85 -2283.61 +julia> DynamicPPL.loglikelihoods(demo_model([1., 2.]), chain); ``` """ function loglikelihoods( @@ -229,83 +93,25 @@ Return an array of log posteriors evaluated at each sample in an MCMC chain or s Example: -```jldoctest -julia> using StableRNGs, MCMCChains +```jldoctest; setup = :(using MCMCChains, StableRNGs), strict=false +julia> # + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end + end +demo_model (generic function with 2 methods) julia> rng = StableRNG(123) StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) -julia> val = rand(rng, 10, 2, 3) -10×2×3 Array{Float64, 3}: -[:, :, 1] = - 0.775123 0.888009 - 0.0348401 0.328242 - 0.0419726 0.0832775 - 0.434804 0.828488 - 0.990519 0.791443 - 0.303966 0.559475 - 0.256094 0.145148 - 0.0708956 0.609441 - 0.9972 0.209455 - 0.215559 0.755402 - -[:, :, 2] = - 0.109065 0.33411 - 0.907512 0.715396 - 0.0931766 0.583011 - 0.166632 0.0419226 - 0.344997 0.308126 - 0.771344 0.322862 - 0.90522 0.743365 - 0.692612 0.819923 - 0.650188 0.107918 - 0.458714 0.742119 - -[:, :, 3] = - 0.398571 0.0512853 - 0.384821 0.408064 - 0.295453 0.0493099 - 0.299926 0.141914 - 0.447855 0.979068 - 0.490272 0.918014 - 0.739946 0.381837 - 0.780228 0.25193 - 0.595156 0.96152 - 0.22909 0.626672 +julia> val = rand(rng, 10, 2, 3); - julia> chain = Chains(val, [:s, :m]) - Chains MCMC chain (10×2×3 Array{Float64, 3}): - - Iterations = 1:1:10 - Number of chains = 3 - Samples per chain = 10 - parameters = s, m - - Summary Statistics - parameters mean std naive_se mcse ess rhat - Symbol Float64 Float64 Float64 Float64 Float64 Float64 - - s 0.4627 0.2972 0.0543 0.0585 42.0294 1.0201 - m 0.4896 0.3117 0.0569 0.0476 95.8121 0.9652 - - Quantiles - parameters 2.5% 25.0% 50.0% 75.0% 97.5% - Symbol Float64 Float64 Float64 Float64 Float64 - - s 0.0400 0.2358 0.4167 0.7281 0.9924 - m 0.0473 0.2201 0.4838 0.7524 0.9663 +julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCChains -julia> logjoint(demo_model(x), chain, 2) -9×3 Matrix{Float64}: - -19148.6 -1433.33 -2111.14 - -20443.2 -5706.89 -3410.79 - -1619.63 -5566.35 -3095.36 - -1411.85 -2432.85 -1572.57 - -2184.95 -1691.38 -1531.74 - -3481.6 -1425.4 -1657.91 - -7302.75 -1438.73 -1750.86 - -1698.33 -2030.47 -1454.15 - -2474.31 -1626.86 -2535.97 +julia> logjoint(demo_model([1., 2.]), chain, 2); ``` """ function logjoint( diff --git a/test/runtests.jl b/test/runtests.jl index 00a4ce51e..ca1ac5aa0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -60,7 +60,7 @@ include("test_util.jl") @testset "doctests" begin DocMeta.setdocmeta!( - DynamicPPL, :DocTestSetup, :(using DynamicPPL); recursive=true + DynamicPPL, :DocTestSetup, :(using DynamicPPL, Distributions, MCMCChains, StableRNGs); recursive=true ) doctestfilters = [ # Older versions will show "0 element Array" instead of "Type[]". From a604da91ebd407f8efe5781c16b104086e94a73d Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 20 Jan 2023 18:47:14 +0000 Subject: [PATCH 080/218] Update docs/make.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- docs/make.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/make.jl b/docs/make.jl index 000ad1e9c..3e846e367 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -4,7 +4,10 @@ using DynamicPPL: AbstractPPL # Doctest setup DocMeta.setdocmeta!( - DynamicPPL, :DocTestSetup, :(using DynamicPPL, Distributions, StableRNGs, MCMCChains); recursive=true + DynamicPPL, + :DocTestSetup, + :(using DynamicPPL, Distributions, StableRNGs, MCMCChains); + recursive=true, ) makedocs(; From abb83fd5381e719729e4a688165ed80b7bbcd4b8 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 20 Jan 2023 18:47:59 +0000 Subject: [PATCH 081/218] Update test/runtests.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/runtests.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index ca1ac5aa0..9d10ed7c5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -60,7 +60,10 @@ include("test_util.jl") @testset "doctests" begin DocMeta.setdocmeta!( - DynamicPPL, :DocTestSetup, :(using DynamicPPL, Distributions, MCMCChains, StableRNGs); recursive=true + DynamicPPL, + :DocTestSetup, + :(using DynamicPPL, Distributions, MCMCChains, StableRNGs); + recursive=true, ) doctestfilters = [ # Older versions will show "0 element Array" instead of "Type[]". From 8ce9422535d53eb92be4538f3e9d58eb4f527c62 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 20 Jan 2023 19:08:11 +0000 Subject: [PATCH 082/218] applied formatting. --- src/logp.jl | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index af7531bad..7d8c62535 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -27,7 +27,9 @@ julia> logprior(demo_model([1., 2.]), chain); ``` """ function logprior( - model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 + model_instance::Model, + chain::AbstractMCMC.AbstractChains, + start_idx::Int = 1, ) vi = VarInfo(model_instance) # extract variables info from the model map( @@ -71,7 +73,9 @@ julia> DynamicPPL.loglikelihoods(demo_model([1., 2.]), chain); ``` """ function loglikelihoods( - model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 + model_instance::Model, + chain::AbstractMCMC.AbstractChains, + start_idx::Int = 1, ) vi = VarInfo(model_instance) # extract variables info from the model map( @@ -115,7 +119,9 @@ julia> logjoint(demo_model([1., 2.]), chain, 2); ``` """ function logjoint( - model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 + model_instance::Model, + chain::AbstractMCMC.AbstractChains, + start_idx::Int = 1, ) vi = VarInfo(model_instance) # extract variables info from the model map( From 560b96bd32ef0e3b50214c0cec47b572644875ba Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 20 Jan 2023 19:34:14 +0000 Subject: [PATCH 083/218] Formatting. --- src/logp.jl | 134 ++++++++++++++++++++++++++-------------------------- 1 file changed, 67 insertions(+), 67 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 7d8c62535..b38491c7f 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -1,19 +1,19 @@ """ - logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) + logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) Return an array of log priors evaluated at each sample in an MCMC chain or sample array. Example: - + ```jldoctest; setup = :(using MCMCChains, StableRNGs), strict=false julia> # - @model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end - end + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end + end demo_model (generic function with 2 methods) julia> rng = StableRNG(123) @@ -27,39 +27,39 @@ julia> logprior(demo_model([1., 2.]), chain); ``` """ function logprior( - model_instance::Model, - chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1, + model_instance::Model, + chain::AbstractMCMC.AbstractChains, + start_idx::Int = 1, ) - vi = VarInfo(model_instance) # extract variables info from the model - map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), - ) do (iteration_idx, chain_idx) - argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) - DynamicPPL.logprior(model_instance, argvals_dict) - end + vi = VarInfo(model_instance) # extract variables info from the model + map( + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), + ) do (iteration_idx, chain_idx) + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) + DynamicPPL.logprior(model_instance, argvals_dict) + end end """ - loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) + loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) Return an array of log likelihoods evaluated at each sample in an MCMC chain or sample array. Example: - + ```jldoctest; setup = :(using MCMCChains, StableRNGs), strict=false julia> # - @model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end - end + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end + end demo_model (generic function with 2 methods) julia> rng = StableRNG(123) @@ -73,39 +73,39 @@ julia> DynamicPPL.loglikelihoods(demo_model([1., 2.]), chain); ``` """ function loglikelihoods( - model_instance::Model, - chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1, + model_instance::Model, + chain::AbstractMCMC.AbstractChains, + start_idx::Int = 1, ) - vi = VarInfo(model_instance) # extract variables info from the model - map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), - ) do (iteration_idx, chain_idx) - argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) - Distributions.loglikelihood(model_instance, argvals_dict) - end + vi = VarInfo(model_instance) # extract variables info from the model + map( + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), + ) do (iteration_idx, chain_idx) + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) + Distributions.loglikelihood(model_instance, argvals_dict) + end end """ - logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) + logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) Return an array of log posteriors evaluated at each sample in an MCMC chain or sample array. Example: - + ```jldoctest; setup = :(using MCMCChains, StableRNGs), strict=false julia> # - @model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end - end + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end + end demo_model (generic function with 2 methods) julia> rng = StableRNG(123) @@ -119,19 +119,19 @@ julia> logjoint(demo_model([1., 2.]), chain, 2); ``` """ function logjoint( - model_instance::Model, - chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1, + model_instance::Model, + chain::AbstractMCMC.AbstractChains, + start_idx::Int = 1, ) - vi = VarInfo(model_instance) # extract variables info from the model - map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), - ) do (iteration_idx, chain_idx) - argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) - Distributions.loglikelihood(model_instance, argvals_dict) + - DynamicPPL.logprior(model_instance, argvals_dict) - end + vi = VarInfo(model_instance) # extract variables info from the model + map( + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), + ) do (iteration_idx, chain_idx) + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) + for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) + Distributions.loglikelihood(model_instance, argvals_dict) + + DynamicPPL.logprior(model_instance, argvals_dict) + end end From 49032dd3edd3990525d7d1bb81314efabd26474c Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 20 Jan 2023 19:36:14 +0000 Subject: [PATCH 084/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index b38491c7f..0b0740498 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -27,9 +27,7 @@ julia> logprior(demo_model([1., 2.]), chain); ``` """ function logprior( - model_instance::Model, - chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1, + model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 ) vi = VarInfo(model_instance) # extract variables info from the model map( From 303f9317362141300293b0cbdcfb81b77e2fa8aa Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 20 Jan 2023 19:36:45 +0000 Subject: [PATCH 085/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 0b0740498..8d01fbbdc 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -29,16 +29,16 @@ julia> logprior(demo_model([1., 2.]), chain); function logprior( model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 ) - vi = VarInfo(model_instance) # extract variables info from the model - map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), - ) do (iteration_idx, chain_idx) - argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) - DynamicPPL.logprior(model_instance, argvals_dict) - end + vi = VarInfo(model_instance) # extract variables info from the model + map( + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) + ) do (iteration_idx, chain_idx) + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) + DynamicPPL.logprior(model_instance, argvals_dict) + end end From e10130320fa081fea2782d14bec64699c86a6d03 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 20 Jan 2023 19:37:00 +0000 Subject: [PATCH 086/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 8d01fbbdc..53d9c9f5b 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -71,9 +71,7 @@ julia> DynamicPPL.loglikelihoods(demo_model([1., 2.]), chain); ``` """ function loglikelihoods( - model_instance::Model, - chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1, + model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 ) vi = VarInfo(model_instance) # extract variables info from the model map( From 3a29f6ff969caa792e4dda5a8e43ef0fb833724a Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 20 Jan 2023 19:37:20 +0000 Subject: [PATCH 087/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 53d9c9f5b..407783528 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -73,16 +73,16 @@ julia> DynamicPPL.loglikelihoods(demo_model([1., 2.]), chain); function loglikelihoods( model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 ) - vi = VarInfo(model_instance) # extract variables info from the model - map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), - ) do (iteration_idx, chain_idx) - argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) - Distributions.loglikelihood(model_instance, argvals_dict) - end + vi = VarInfo(model_instance) # extract variables info from the model + map( + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) + ) do (iteration_idx, chain_idx) + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) + Distributions.loglikelihood(model_instance, argvals_dict) + end end From e9e515f2feaf5d3fae066bb68f86982bd29de10e Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 20 Jan 2023 19:37:35 +0000 Subject: [PATCH 088/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 407783528..aa777ea8b 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -115,9 +115,7 @@ julia> logjoint(demo_model([1., 2.]), chain, 2); ``` """ function logjoint( - model_instance::Model, - chain::AbstractMCMC.AbstractChains, - start_idx::Int = 1, + model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 ) vi = VarInfo(model_instance) # extract variables info from the model map( From 63596e21700ce2f9e708e123f1c32725bd267906 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 20 Jan 2023 19:37:48 +0000 Subject: [PATCH 089/218] Update src/logp.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/logp.jl | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index aa777ea8b..0f7cb7bd5 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -117,15 +117,15 @@ julia> logjoint(demo_model([1., 2.]), chain, 2); function logjoint( model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 ) - vi = VarInfo(model_instance) # extract variables info from the model - map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)), - ) do (iteration_idx, chain_idx) - argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) - for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) - Distributions.loglikelihood(model_instance, argvals_dict) + - DynamicPPL.logprior(model_instance, argvals_dict) - end + vi = VarInfo(model_instance) # extract variables info from the model + map( + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) + ) do (iteration_idx, chain_idx) + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) + Distributions.loglikelihood(model_instance, argvals_dict) + + DynamicPPL.logprior(model_instance, argvals_dict) + end end From 2c5ab9ea271a3ec6ddfeea6a12408481e8bc7e59 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 20 Jan 2023 21:21:37 +0000 Subject: [PATCH 090/218] Fix doc tests again. --- src/logp.jl | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index 0f7cb7bd5..d9214e2b6 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -5,15 +5,15 @@ Return an array of log priors evaluated at each sample in an MCMC chain or sampl Example: -```jldoctest; setup = :(using MCMCChains, StableRNGs), strict=false +```jldoctest; setup = :(using MCMCChains, StableRNGs) julia> # - @model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end - end + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end + end demo_model (generic function with 2 methods) julia> rng = StableRNG(123) @@ -49,15 +49,15 @@ Return an array of log likelihoods evaluated at each sample in an MCMC chain or Example: -```jldoctest; setup = :(using MCMCChains, StableRNGs), strict=false +```jldoctest; setup = :(using MCMCChains, StableRNGs) julia> # - @model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end - end + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end + end demo_model (generic function with 2 methods) julia> rng = StableRNG(123) @@ -93,15 +93,15 @@ Return an array of log posteriors evaluated at each sample in an MCMC chain or s Example: -```jldoctest; setup = :(using MCMCChains, StableRNGs), strict=false +```jldoctest; setup = :(using MCMCChains, StableRNGs) julia> # - @model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end - end + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end + end demo_model (generic function with 2 methods) julia> rng = StableRNG(123) From 4c62f0744a8a448bc475309be67f9d3ec3ff3a25 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 20 Jan 2023 21:24:21 +0000 Subject: [PATCH 091/218] Fixed formatting. --- src/logp.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/logp.jl b/src/logp.jl index d9214e2b6..50f763c5a 100644 --- a/src/logp.jl +++ b/src/logp.jl @@ -41,7 +41,6 @@ function logprior( end end - """ loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) @@ -85,7 +84,6 @@ function loglikelihoods( end end - """ logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) From 9673f1ac6b5e1443b299dcac618f8103228d3d45 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 20 Jan 2023 21:44:47 +0000 Subject: [PATCH 092/218] Merged `logp.jl` into `model.jl` --- src/DynamicPPL.jl | 1 - src/logp.jl | 129 --------------------------------------------- src/model.jl | 130 ++++++++++++++++++++++++++++++++++++++++++++++ test/logp.jl | 33 ------------ test/model.jl | 34 ++++++++++++ test/runtests.jl | 2 - 6 files changed, 164 insertions(+), 165 deletions(-) delete mode 100644 src/logp.jl delete mode 100644 test/logp.jl diff --git a/src/DynamicPPL.jl b/src/DynamicPPL.jl index edb65a0c5..594084d66 100644 --- a/src/DynamicPPL.jl +++ b/src/DynamicPPL.jl @@ -165,6 +165,5 @@ include("submodel_macro.jl") include("test_utils.jl") include("transforming.jl") include("logdensityfunction.jl") -include("logp.jl") end # module diff --git a/src/logp.jl b/src/logp.jl deleted file mode 100644 index 50f763c5a..000000000 --- a/src/logp.jl +++ /dev/null @@ -1,129 +0,0 @@ -""" - logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) - -Return an array of log priors evaluated at each sample in an MCMC chain or sample array. - -Example: - -```jldoctest; setup = :(using MCMCChains, StableRNGs) -julia> # - @model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end - end -demo_model (generic function with 2 methods) - -julia> rng = StableRNG(123) -StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) - -julia> val = rand(rng, 10, 2, 3); - -julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCChains - -julia> logprior(demo_model([1., 2.]), chain); -``` -""" -function logprior( - model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 -) - vi = VarInfo(model_instance) # extract variables info from the model - map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) - ) do (iteration_idx, chain_idx) - argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) - DynamicPPL.logprior(model_instance, argvals_dict) - end -end - -""" - loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) - -Return an array of log likelihoods evaluated at each sample in an MCMC chain or sample array. - -Example: - -```jldoctest; setup = :(using MCMCChains, StableRNGs) -julia> # - @model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end - end -demo_model (generic function with 2 methods) - -julia> rng = StableRNG(123) -StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) - -julia> val = rand(rng, 10, 2, 3); - -julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCChains - -julia> DynamicPPL.loglikelihoods(demo_model([1., 2.]), chain); -``` -""" -function loglikelihoods( - model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 -) - vi = VarInfo(model_instance) # extract variables info from the model - map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) - ) do (iteration_idx, chain_idx) - argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) - Distributions.loglikelihood(model_instance, argvals_dict) - end -end - -""" - logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) - -Return an array of log posteriors evaluated at each sample in an MCMC chain or sample array. - -Example: - -```jldoctest; setup = :(using MCMCChains, StableRNGs) -julia> # - @model function demo_model(x) - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) - end - end -demo_model (generic function with 2 methods) - -julia> rng = StableRNG(123) -StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) - -julia> val = rand(rng, 10, 2, 3); - -julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCChains - -julia> logjoint(demo_model([1., 2.]), chain, 2); -``` -""" -function logjoint( - model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 -) - vi = VarInfo(model_instance) # extract variables info from the model - map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) - ) do (iteration_idx, chain_idx) - argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) - Distributions.loglikelihood(model_instance, argvals_dict) + - DynamicPPL.logprior(model_instance, argvals_dict) - end -end diff --git a/src/model.jl b/src/model.jl index b7e0984c5..6f10a726d 100644 --- a/src/model.jl +++ b/src/model.jl @@ -657,6 +657,50 @@ function logjoint(model::Model, varinfo::AbstractVarInfo) return getlogp(last(evaluate!!(model, varinfo, DefaultContext()))) end +""" + logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) + +Return an array of log posteriors evaluated at each sample in an MCMC chain or sample array. + +Example: + +```jldoctest; setup = :(using MCMCChains, StableRNGs) +julia> # + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end + end +demo_model (generic function with 2 methods) + +julia> rng = StableRNG(123) +StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) + +julia> val = rand(rng, 10, 2, 3); + +julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCChains + +julia> logjoint(demo_model([1., 2.]), chain, 2); +``` +""" +function logjoint( + model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 +) + vi = VarInfo(model_instance) # extract variables info from the model + map( + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) + ) do (iteration_idx, chain_idx) + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) + Distributions.loglikelihood(model_instance, argvals_dict) + + DynamicPPL.logprior(model_instance, argvals_dict) + end +end + """ logprior(model::Model, varinfo::AbstractVarInfo) @@ -668,6 +712,49 @@ function logprior(model::Model, varinfo::AbstractVarInfo) return getlogp(last(evaluate!!(model, varinfo, PriorContext()))) end +""" + logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) + +Return an array of log priors evaluated at each sample in an MCMC chain or sample array. + +Example: + +```jldoctest; setup = :(using MCMCChains, StableRNGs) +julia> # + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end + end +demo_model (generic function with 2 methods) + +julia> rng = StableRNG(123) +StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) + +julia> val = rand(rng, 10, 2, 3); + +julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCChains + +julia> logprior(demo_model([1., 2.]), chain); +``` +""" +function logprior( + model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 +) + vi = VarInfo(model_instance) # extract variables info from the model + map( + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) + ) do (iteration_idx, chain_idx) + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) + DynamicPPL.logprior(model_instance, argvals_dict) + end +end + """ loglikelihood(model::Model, varinfo::AbstractVarInfo) @@ -679,6 +766,49 @@ function Distributions.loglikelihood(model::Model, varinfo::AbstractVarInfo) return getlogp(last(evaluate!!(model, varinfo, LikelihoodContext()))) end +""" + loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) + +Return an array of log likelihoods evaluated at each sample in an MCMC chain or sample array. + +Example: + +```jldoctest; setup = :(using MCMCChains, StableRNGs) +julia> # + @model function demo_model(x) + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + for i in 1:length(x) + x[i] ~ Normal(m, sqrt(s)) + end + end +demo_model (generic function with 2 methods) + +julia> rng = StableRNG(123) +StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) + +julia> val = rand(rng, 10, 2, 3); + +julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCChains + +julia> DynamicPPL.loglikelihoods(demo_model([1., 2.]), chain); +``` +""" +function loglikelihoods( + model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 +) + vi = VarInfo(model_instance) # extract variables info from the model + map( + Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) + ) do (iteration_idx, chain_idx) + argvals_dict = OrderedDict( + vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for + vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) + Distributions.loglikelihood(model_instance, argvals_dict) + end +end + """ generated_quantities(model::Model, chain::AbstractChains) diff --git a/test/logp.jl b/test/logp.jl deleted file mode 100644 index 707cdb30a..000000000 --- a/test/logp.jl +++ /dev/null @@ -1,33 +0,0 @@ -@testset "logp.jl" begin - Test.@testset "$(m.f)" for m in DynamicPPL.TestUtils.DEMO_MODELS - # generate a chain of sample parameter values. - N = 200 - start_idx = 100 - - logpriors_true = Vector{Float64}[undef, N - start_idx] - loglikelihoods_true = Vector{Float64}[undef, N - start_idx] - logposteriors_true = Vector{Float64}[undef, N - start_idx] - - chain = sample(m, NUTS(), N) - - map(start_idx:N) do i - val = get_params(chain[i, :, :]) - example_values = ( - m=collect(Iterators.flatten(val.m)), s=collect(Iterators.flatten(val.s)) - ) - logpriors_true[i] = DynamicPPL.TestUtils.logprior_true(m, example_values...) - loglikelihoods_true[i] = DynamicPPL.TestUtils.loglikelihood_true( - m, example_values... - ) - logposteriors_true[i] = logpriors_true[i] + loglikelihoods_true[i] - end - # calculate the pointwise loglikelihoods for the whole chain using custom logprior. - logpriors_new = logprior(m, chain, start_idx) - loglikelihoods_new = loglikelihoods(m, chain, start_idx) - logposteriors_new = logjoint(m, chain, start_idx) - # compare the likelihoods - @test logpriors_new ≈ logpriors_true - @test loglikelihoods_new ≈ loglikelihoods_true - @test logposteriors_new ≈ logposteriors_true - end -end diff --git a/test/model.jl b/test/model.jl index b091cc87b..b8a196e46 100644 --- a/test/model.jl +++ b/test/model.jl @@ -150,3 +150,37 @@ end @test rand(Dict, model) == sample_dict end end + +@testset "logp.jl" begin + Test.@testset "$(m.f)" for m in DynamicPPL.TestUtils.DEMO_MODELS + # generate a chain of sample parameter values. + N = 200 + start_idx = 100 + + logpriors_true = Vector{Float64}[undef, N - start_idx] + loglikelihoods_true = Vector{Float64}[undef, N - start_idx] + logposteriors_true = Vector{Float64}[undef, N - start_idx] + + chain = sample(m, NUTS(), N) + + map(start_idx:N) do i + val = get_params(chain[i, :, :]) + example_values = ( + m=collect(Iterators.flatten(val.m)), s=collect(Iterators.flatten(val.s)) + ) + logpriors_true[i] = DynamicPPL.TestUtils.logprior_true(m, example_values...) + loglikelihoods_true[i] = DynamicPPL.TestUtils.loglikelihood_true( + m, example_values... + ) + logposteriors_true[i] = logpriors_true[i] + loglikelihoods_true[i] + end + # calculate the pointwise loglikelihoods for the whole chain using custom logprior. + logpriors_new = logprior(m, chain, start_idx) + loglikelihoods_new = loglikelihoods(m, chain, start_idx) + logposteriors_new = logjoint(m, chain, start_idx) + # compare the likelihoods + @test logpriors_new ≈ logpriors_true + @test loglikelihoods_new ≈ loglikelihoods_true + @test logposteriors_new ≈ logposteriors_true + end +end diff --git a/test/runtests.jl b/test/runtests.jl index 9d10ed7c5..6b79ce4d7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -50,8 +50,6 @@ include("test_util.jl") include("serialization.jl") include("loglikelihoods.jl") - - include("logp.jl") end @testset "compat" begin From ef8526738d3bdfaa7524958e93293228f2534b35 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 20 Jan 2023 21:45:46 +0000 Subject: [PATCH 093/218] CompatHelper: bump compat for Turing to 0.24 for package turing, (keep existing compat) (#448) * CompatHelper: bump compat for Turing to 0.24 for package turing, (keep existing compat) * Update test/turing/Project.toml Co-authored-by: CompatHelper Julia Co-authored-by: Tor Erlend Fjelde --- test/turing/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/turing/Project.toml b/test/turing/Project.toml index 26d34fb3d..0009b2e7b 100644 --- a/test/turing/Project.toml +++ b/test/turing/Project.toml @@ -6,5 +6,5 @@ Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" [compat] DynamicPPL = "0.20, 0.21" -Turing = "0.21" +Turing = "0.24" julia = "1.6" From bbff92d88f893ece00b8e1a940073d96e4add43e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 20 Jan 2023 21:46:33 +0000 Subject: [PATCH 094/218] CompatHelper: bump compat for Turing to 0.23 for package turing, (keep existing compat) (#439) * CompatHelper: bump compat for Turing to 0.23 for package turing, (keep existing compat) * Update test/turing/Project.toml Co-authored-by: CompatHelper Julia Co-authored-by: Hong Ge <3279477+yebai@users.noreply.github.com> --- test/turing/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/turing/Project.toml b/test/turing/Project.toml index 0009b2e7b..5bda1e2dd 100644 --- a/test/turing/Project.toml +++ b/test/turing/Project.toml @@ -6,5 +6,5 @@ Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" [compat] DynamicPPL = "0.20, 0.21" -Turing = "0.24" +Turing = "0.21, 0.22, 0.23, 0.24" julia = "1.6" From 824dcb610ddbe2a60b452ff9679ad241627e8e54 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 20 Jan 2023 23:23:40 +0000 Subject: [PATCH 095/218] Fixed obsolete `TArray` reference. --- test/turing/compiler.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/turing/compiler.jl b/test/turing/compiler.jl index 0b2b5362d..bf0d125e7 100644 --- a/test/turing/compiler.jl +++ b/test/turing/compiler.jl @@ -70,7 +70,7 @@ x = Float64[1 2] @model function gauss(x) - priors = TArray{Float64}(2) + priors = Array{Float64}(2) priors[1] ~ InverseGamma(2, 3) # s priors[2] ~ Normal(0, sqrt(priors[1])) # m for i in 1:length(x) From 97028a52ef94225bb8db9452cad3b8072d2ee91a Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 20 Jan 2023 23:29:28 +0000 Subject: [PATCH 096/218] Fixed incorrect code. --- src/model.jl | 6 +++--- test/model.jl | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/model.jl b/src/model.jl index 6f10a726d..9109b4001 100644 --- a/src/model.jl +++ b/src/model.jl @@ -767,7 +767,7 @@ function Distributions.loglikelihood(model::Model, varinfo::AbstractVarInfo) end """ - loglikelihoods(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) + loglikelihood(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) Return an array of log likelihoods evaluated at each sample in an MCMC chain or sample array. @@ -791,10 +791,10 @@ julia> val = rand(rng, 10, 2, 3); julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCChains -julia> DynamicPPL.loglikelihoods(demo_model([1., 2.]), chain); +julia> loglikelihood(demo_model([1., 2.]), chain); ``` """ -function loglikelihoods( +function Distributions.loglikelihood( model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 ) vi = VarInfo(model_instance) # extract variables info from the model diff --git a/test/model.jl b/test/model.jl index b8a196e46..dcd7fc91e 100644 --- a/test/model.jl +++ b/test/model.jl @@ -157,9 +157,9 @@ end N = 200 start_idx = 100 - logpriors_true = Vector{Float64}[undef, N - start_idx] - loglikelihoods_true = Vector{Float64}[undef, N - start_idx] - logposteriors_true = Vector{Float64}[undef, N - start_idx] + logpriors_true = Vector{Float64}(undef, N - start_idx) + loglikelihoods_true = Vector{Float64}(undef, N - start_idx) + logposteriors_true = Vector{Float64}(undef, N - start_idx) chain = sample(m, NUTS(), N) @@ -176,7 +176,7 @@ end end # calculate the pointwise loglikelihoods for the whole chain using custom logprior. logpriors_new = logprior(m, chain, start_idx) - loglikelihoods_new = loglikelihoods(m, chain, start_idx) + loglikelihoods_new = loglikelihood(m, chain, start_idx) logposteriors_new = logjoint(m, chain, start_idx) # compare the likelihoods @test logpriors_new ≈ logpriors_true From 895384ed557c6a3b4be1ab4a25b6b6e55e09507e Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 20 Jan 2023 23:52:11 +0000 Subject: [PATCH 097/218] More bugfixes in logp tests. --- test/model.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/model.jl b/test/model.jl index dcd7fc91e..2904ab6a6 100644 --- a/test/model.jl +++ b/test/model.jl @@ -163,21 +163,21 @@ end chain = sample(m, NUTS(), N) - map(start_idx:N) do i + map((start_idx+1):N) do i val = get_params(chain[i, :, :]) example_values = ( - m=collect(Iterators.flatten(val.m)), s=collect(Iterators.flatten(val.s)) + s=collect(Iterators.flatten(val.s)), m=collect(Iterators.flatten(val.m)) ) - logpriors_true[i] = DynamicPPL.TestUtils.logprior_true(m, example_values...) - loglikelihoods_true[i] = DynamicPPL.TestUtils.loglikelihood_true( + logpriors_true[i-start_idx] = DynamicPPL.TestUtils.logprior_true(m, example_values...) + loglikelihoods_true[i-start_idx] = DynamicPPL.TestUtils.loglikelihood_true( m, example_values... ) - logposteriors_true[i] = logpriors_true[i] + loglikelihoods_true[i] + logposteriors_true[i-start_idx] = logpriors_true[i-start_idx] + loglikelihoods_true[i-start_idx] end # calculate the pointwise loglikelihoods for the whole chain using custom logprior. - logpriors_new = logprior(m, chain, start_idx) - loglikelihoods_new = loglikelihood(m, chain, start_idx) - logposteriors_new = logjoint(m, chain, start_idx) + logpriors_new = logprior(m, chain, start_idx+1) + loglikelihoods_new = loglikelihood(m, chain, start_idx+1) + logposteriors_new = logjoint(m, chain, start_idx+1) # compare the likelihoods @test logpriors_new ≈ logpriors_true @test loglikelihoods_new ≈ loglikelihoods_true From 28fdf7dd0ac04620a0e93c5178e255bd5fd8865b Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 20 Jan 2023 23:55:57 +0000 Subject: [PATCH 098/218] Avoid calling Turing sampler. --- test/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index 2904ab6a6..1e9f825a7 100644 --- a/test/model.jl +++ b/test/model.jl @@ -161,7 +161,7 @@ end loglikelihoods_true = Vector{Float64}(undef, N - start_idx) logposteriors_true = Vector{Float64}(undef, N - start_idx) - chain = sample(m, NUTS(), N) + chain = sample(m, SampleFromPrior(), N) map((start_idx+1):N) do i val = get_params(chain[i, :, :]) From a456147f244922981b0f2d2c31526d5f69d87b40 Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Fri, 20 Jan 2023 23:57:02 +0000 Subject: [PATCH 099/218] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/test/model.jl b/test/model.jl index 1e9f825a7..299f6367b 100644 --- a/test/model.jl +++ b/test/model.jl @@ -163,21 +163,24 @@ end chain = sample(m, SampleFromPrior(), N) - map((start_idx+1):N) do i + map((start_idx + 1):N) do i val = get_params(chain[i, :, :]) example_values = ( s=collect(Iterators.flatten(val.s)), m=collect(Iterators.flatten(val.m)) ) - logpriors_true[i-start_idx] = DynamicPPL.TestUtils.logprior_true(m, example_values...) - loglikelihoods_true[i-start_idx] = DynamicPPL.TestUtils.loglikelihood_true( + logpriors_true[i - start_idx] = DynamicPPL.TestUtils.logprior_true( m, example_values... ) - logposteriors_true[i-start_idx] = logpriors_true[i-start_idx] + loglikelihoods_true[i-start_idx] + loglikelihoods_true[i - start_idx] = DynamicPPL.TestUtils.loglikelihood_true( + m, example_values... + ) + logposteriors_true[i - start_idx] = + logpriors_true[i - start_idx] + loglikelihoods_true[i - start_idx] end # calculate the pointwise loglikelihoods for the whole chain using custom logprior. - logpriors_new = logprior(m, chain, start_idx+1) - loglikelihoods_new = loglikelihood(m, chain, start_idx+1) - logposteriors_new = logjoint(m, chain, start_idx+1) + logpriors_new = logprior(m, chain, start_idx + 1) + loglikelihoods_new = loglikelihood(m, chain, start_idx + 1) + logposteriors_new = logjoint(m, chain, start_idx + 1) # compare the likelihoods @test logpriors_new ≈ logpriors_true @test loglikelihoods_new ≈ loglikelihoods_true From 45c014175740b959a2187455b334e1a821a5f6c2 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Sat, 21 Jan 2023 00:47:39 +0000 Subject: [PATCH 100/218] Replace SampleFromPrior with synthetic chain. --- test/model.jl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/model.jl b/test/model.jl index 299f6367b..3e8e15fbc 100644 --- a/test/model.jl +++ b/test/model.jl @@ -152,7 +152,8 @@ end end @testset "logp.jl" begin - Test.@testset "$(m.f)" for m in DynamicPPL.TestUtils.DEMO_MODELS + m = DynamicPPL.TestUtils.DEMO_MODELS[1] + Test.@testset "$(m.f)" begin # generate a chain of sample parameter values. N = 200 start_idx = 100 @@ -161,7 +162,9 @@ end loglikelihoods_true = Vector{Float64}(undef, N - start_idx) logposteriors_true = Vector{Float64}(undef, N - start_idx) - chain = sample(m, SampleFromPrior(), N) + d = rand(Dict, m) + val = rand(N, length(collect(values(d))), 1) + chain = Chains(val, string.(collect(keys(d)))); # construct a chain of samples using MCMCChains map((start_idx + 1):N) do i val = get_params(chain[i, :, :]) From e4558ddaa762eaec691f1142397a9eadcff99350 Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Sat, 21 Jan 2023 00:52:25 +0000 Subject: [PATCH 101/218] Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index 3e8e15fbc..15cd74ffc 100644 --- a/test/model.jl +++ b/test/model.jl @@ -164,7 +164,7 @@ end d = rand(Dict, m) val = rand(N, length(collect(values(d))), 1) - chain = Chains(val, string.(collect(keys(d)))); # construct a chain of samples using MCMCChains + chain = Chains(val, string.(collect(keys(d)))) # construct a chain of samples using MCMCChains map((start_idx + 1):N) do i val = get_params(chain[i, :, :]) From d8a4d32b0cb0f89bb29a921f96889616a90ff9d5 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Sat, 21 Jan 2023 00:55:29 +0000 Subject: [PATCH 102/218] Minor bugfix. --- test/turing/compiler.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/turing/compiler.jl b/test/turing/compiler.jl index bf0d125e7..4a864a84f 100644 --- a/test/turing/compiler.jl +++ b/test/turing/compiler.jl @@ -70,7 +70,7 @@ x = Float64[1 2] @model function gauss(x) - priors = Array{Float64}(2) + priors = Array{Float64}(undef, 2) priors[1] ~ InverseGamma(2, 3) # s priors[2] ~ Normal(0, sqrt(priors[1])) # m for i in 1:length(x) From 5bf860b064abdd14994e83a333eaa10277e1c2a3 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Sat, 21 Jan 2023 23:44:47 +0000 Subject: [PATCH 103/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 9109b4001..1dceefd46 100644 --- a/src/model.jl +++ b/src/model.jl @@ -660,7 +660,7 @@ end """ logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) -Return an array of log posteriors evaluated at each sample in an MCMC chain or sample array. +Return an array of log joint probabilities evaluated at each sample in an MCMC `chain`. Example: From d63c18575b3be37bfaab92a1a36cffc022d4c58e Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Sat, 21 Jan 2023 23:47:06 +0000 Subject: [PATCH 104/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/model.jl b/src/model.jl index 1dceefd46..177a3ecc1 100644 --- a/src/model.jl +++ b/src/model.jl @@ -665,8 +665,7 @@ Return an array of log joint probabilities evaluated at each sample in an MCMC ` Example: ```jldoctest; setup = :(using MCMCChains, StableRNGs) -julia> # - @model function demo_model(x) +julia> @model function demo_model(x) s ~ InverseGamma(2, 3) m ~ Normal(0, sqrt(s)) for i in 1:length(x) From b9bae1189785a6e32464cb805b74d4e069e2f9cd Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Sat, 21 Jan 2023 23:48:50 +0000 Subject: [PATCH 105/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 177a3ecc1..2ce12dc23 100644 --- a/src/model.jl +++ b/src/model.jl @@ -669,7 +669,7 @@ julia> @model function demo_model(x) s ~ InverseGamma(2, 3) m ~ Normal(0, sqrt(s)) for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) + x[i] ~ Normal(m, sqrt(s)) end end demo_model (generic function with 2 methods) From 2d58d183398c22ad8feb04ca4c06a43455a5226e Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Sat, 21 Jan 2023 23:49:14 +0000 Subject: [PATCH 106/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/model.jl b/src/model.jl index 2ce12dc23..37162872f 100644 --- a/src/model.jl +++ b/src/model.jl @@ -671,8 +671,7 @@ julia> @model function demo_model(x) for i in 1:length(x) x[i] ~ Normal(m, sqrt(s)) end - end -demo_model (generic function with 2 methods) + end; julia> rng = StableRNG(123) StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) From a34d5e43fb5d5ee20187ae636548be058da6099a Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Sun, 22 Jan 2023 10:30:25 +0000 Subject: [PATCH 107/218] Update Project.toml --- docs/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/Project.toml b/docs/Project.toml index fd00f1c9c..2d1b672c7 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -18,3 +18,4 @@ LogDensityProblems = "2" MLUtils = "0.3, 0.4" Setfield = "0.7.1, 0.8, 1" StableRNGs = "1" +MCMCChains = "5" From 394967c455ef017d210f45dfe3d4d77059a389bc Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Sun, 22 Jan 2023 13:47:51 +0000 Subject: [PATCH 108/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 37162872f..fffdb5889 100644 --- a/src/model.jl +++ b/src/model.jl @@ -664,7 +664,9 @@ Return an array of log joint probabilities evaluated at each sample in an MCMC ` Example: -```jldoctest; setup = :(using MCMCChains, StableRNGs) +```jldoctest +julia> using MCMCChains, StableRNGs + julia> @model function demo_model(x) s ~ InverseGamma(2, 3) m ~ Normal(0, sqrt(s)) From 6c8d253018dd32bf374b15d339ffdbc33188059c Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Sun, 22 Jan 2023 13:49:03 +0000 Subject: [PATCH 109/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index fffdb5889..edd23cfc5 100644 --- a/src/model.jl +++ b/src/model.jl @@ -682,7 +682,7 @@ julia> val = rand(rng, 10, 2, 3); julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCChains -julia> logjoint(demo_model([1., 2.]), chain, 2); +julia> logjoint(demo_model([1., 2.]), chain); ``` """ function logjoint( From a364e3b1523ceb136cf488f8e2fb9034ad8f7831 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Sun, 22 Jan 2023 14:02:38 +0000 Subject: [PATCH 110/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/model.jl b/src/model.jl index edd23cfc5..433bed7b9 100644 --- a/src/model.jl +++ b/src/model.jl @@ -798,9 +798,7 @@ function Distributions.loglikelihood( model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 ) vi = VarInfo(model_instance) # extract variables info from the model - map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) - ) do (iteration_idx, chain_idx) + map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) From 1f16544de736ed635e8672301843024c94eda1d9 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:11:31 +0000 Subject: [PATCH 111/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 433bed7b9..9c740f6a6 100644 --- a/src/model.jl +++ b/src/model.jl @@ -658,7 +658,7 @@ function logjoint(model::Model, varinfo::AbstractVarInfo) end """ - logjoint(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) + logjoint(model::Model, chain::AbstractMCMC.AbstractChains) Return an array of log joint probabilities evaluated at each sample in an MCMC `chain`. From 732a1ed238f4f7aabf526671f3a58c43eaa06235 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:12:06 +0000 Subject: [PATCH 112/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/model.jl b/src/model.jl index 9c740f6a6..e41ec73b8 100644 --- a/src/model.jl +++ b/src/model.jl @@ -685,9 +685,7 @@ julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCC julia> logjoint(demo_model([1., 2.]), chain); ``` """ -function logjoint( - model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 -) +function logjoint(model::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model map( Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) From b9251583f51cbcf4273aef1164400d017baa77b7 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:12:35 +0000 Subject: [PATCH 113/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index e41ec73b8..124dae48a 100644 --- a/src/model.jl +++ b/src/model.jl @@ -686,7 +686,7 @@ julia> logjoint(demo_model([1., 2.]), chain); ``` """ function logjoint(model::Model, chain::AbstractMCMC.AbstractChains) - vi = VarInfo(model_instance) # extract variables info from the model + vi = VarInfo(model) # extract variables info from the model map( Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) ) do (iteration_idx, chain_idx) From 5af2d52d4e7315f9b7a70e1e7e091731030bfe53 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:13:18 +0000 Subject: [PATCH 114/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/model.jl b/src/model.jl index 124dae48a..efcb676d7 100644 --- a/src/model.jl +++ b/src/model.jl @@ -687,9 +687,7 @@ julia> logjoint(demo_model([1., 2.]), chain); """ function logjoint(model::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model) # extract variables info from the model - map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) - ) do (iteration_idx, chain_idx) + map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) From 364cfc7711b01738b5938bd961116f100dd4ab00 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:13:45 +0000 Subject: [PATCH 115/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index efcb676d7..ffb1f1e7d 100644 --- a/src/model.jl +++ b/src/model.jl @@ -690,7 +690,7 @@ function logjoint(model::Model, chain::AbstractMCMC.AbstractChains) map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) Distributions.loglikelihood(model_instance, argvals_dict) + DynamicPPL.logprior(model_instance, argvals_dict) From 8e9ee49afd330db2c948017b6fba3bc5008cf16e Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:14:15 +0000 Subject: [PATCH 116/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/model.jl b/src/model.jl index ffb1f1e7d..113adecd1 100644 --- a/src/model.jl +++ b/src/model.jl @@ -692,8 +692,7 @@ function logjoint(model::Model, chain::AbstractMCMC.AbstractChains) vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) - Distributions.loglikelihood(model_instance, argvals_dict) + - DynamicPPL.logprior(model_instance, argvals_dict) + loglikelihood(model, argvals_dict) + logprior(model, argvals_dict) end end From d53a79daf18165aa9326597205005537f4628cb9 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:14:46 +0000 Subject: [PATCH 117/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 113adecd1..cfd2e5075 100644 --- a/src/model.jl +++ b/src/model.jl @@ -708,7 +708,7 @@ function logprior(model::Model, varinfo::AbstractVarInfo) end """ - logprior(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) + logprior(model::Model, chain::AbstractMCMC.AbstractChains) Return an array of log priors evaluated at each sample in an MCMC chain or sample array. From bf05924d8124cceae695416fd50a84a5af9fb43b Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:15:07 +0000 Subject: [PATCH 118/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index cfd2e5075..2edcbb187 100644 --- a/src/model.jl +++ b/src/model.jl @@ -710,7 +710,7 @@ end """ logprior(model::Model, chain::AbstractMCMC.AbstractChains) -Return an array of log priors evaluated at each sample in an MCMC chain or sample array. +Return an array of log prior probabilities evaluated at each sample in an MCMC `chain`. Example: From 376a604ef59d5913e4f567a005a94b52dcf5b77e Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:18:43 +0000 Subject: [PATCH 119/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 2edcbb187..01bcfecc5 100644 --- a/src/model.jl +++ b/src/model.jl @@ -662,7 +662,7 @@ end Return an array of log joint probabilities evaluated at each sample in an MCMC `chain`. -Example: +# Examples ```jldoctest julia> using MCMCChains, StableRNGs From c4da915340ad6d3893a59bcd05ee49f3f92de3df Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:21:10 +0000 Subject: [PATCH 120/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 01bcfecc5..67db12453 100644 --- a/src/model.jl +++ b/src/model.jl @@ -712,7 +712,7 @@ end Return an array of log prior probabilities evaluated at each sample in an MCMC `chain`. -Example: +# Examples ```jldoctest; setup = :(using MCMCChains, StableRNGs) julia> # From 221805b158d4ebe5b705f9cbe498bf36dd0882d7 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:27:57 +0000 Subject: [PATCH 121/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 67db12453..cd2ea4a34 100644 --- a/src/model.jl +++ b/src/model.jl @@ -714,7 +714,9 @@ Return an array of log prior probabilities evaluated at each sample in an MCMC ` # Examples -```jldoctest; setup = :(using MCMCChains, StableRNGs) +```jldoctest +julia> using MCMCChains, StableRNGs + julia> # @model function demo_model(x) s ~ InverseGamma(2, 3) From b7ebadea19fcc2a9b453623f42543f9695dafed4 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:29:15 +0000 Subject: [PATCH 122/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/model.jl b/src/model.jl index cd2ea4a34..46084bf5d 100644 --- a/src/model.jl +++ b/src/model.jl @@ -717,8 +717,7 @@ Return an array of log prior probabilities evaluated at each sample in an MCMC ` ```jldoctest julia> using MCMCChains, StableRNGs -julia> # - @model function demo_model(x) +julia> @model function demo_model(x) s ~ InverseGamma(2, 3) m ~ Normal(0, sqrt(s)) for i in 1:length(x) From e94f2f3755945755632cd01942d03819a7b937a8 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:29:35 +0000 Subject: [PATCH 123/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 46084bf5d..d7e61194f 100644 --- a/src/model.jl +++ b/src/model.jl @@ -721,7 +721,7 @@ julia> @model function demo_model(x) s ~ InverseGamma(2, 3) m ~ Normal(0, sqrt(s)) for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) + x[i] ~ Normal(m, sqrt(s)) end end demo_model (generic function with 2 methods) From 08c6dc70ad57403326b0003e40ab9de33a06d0ac Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:30:00 +0000 Subject: [PATCH 124/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/model.jl b/src/model.jl index d7e61194f..36c0f7e2b 100644 --- a/src/model.jl +++ b/src/model.jl @@ -723,8 +723,7 @@ julia> @model function demo_model(x) for i in 1:length(x) x[i] ~ Normal(m, sqrt(s)) end - end -demo_model (generic function with 2 methods) + end; julia> rng = StableRNG(123) StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) From 01569fc88108b730a77cc2f3de9b1c6fa20599b7 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:30:24 +0000 Subject: [PATCH 125/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/model.jl b/src/model.jl index 36c0f7e2b..95598fe95 100644 --- a/src/model.jl +++ b/src/model.jl @@ -735,9 +735,7 @@ julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCC julia> logprior(demo_model([1., 2.]), chain); ``` """ -function logprior( - model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 -) +function logprior(model::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model map( Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) From 57a0671dbf573c78ba71064d81b26011de6d760f Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:30:45 +0000 Subject: [PATCH 126/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 95598fe95..96633a245 100644 --- a/src/model.jl +++ b/src/model.jl @@ -736,7 +736,7 @@ julia> logprior(demo_model([1., 2.]), chain); ``` """ function logprior(model::Model, chain::AbstractMCMC.AbstractChains) - vi = VarInfo(model_instance) # extract variables info from the model + vi = VarInfo(model) # extract variables info from the model map( Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) ) do (iteration_idx, chain_idx) From e74b0fe2b2b0c9e0cd3237e4932573ada4c10a6c Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:31:06 +0000 Subject: [PATCH 127/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/model.jl b/src/model.jl index 96633a245..b87e531b0 100644 --- a/src/model.jl +++ b/src/model.jl @@ -737,9 +737,7 @@ julia> logprior(demo_model([1., 2.]), chain); """ function logprior(model::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model) # extract variables info from the model - map( - Iterators.product(start_idx:size(chain, 1), 1:size(chain, 3)) - ) do (iteration_idx, chain_idx) + map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) From 13b9a7f9d0dd0c6689a6d918fd3e64b232a870b8 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:31:27 +0000 Subject: [PATCH 128/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index b87e531b0..5fa15dabd 100644 --- a/src/model.jl +++ b/src/model.jl @@ -740,7 +740,7 @@ function logprior(model::Model, chain::AbstractMCMC.AbstractChains) map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) DynamicPPL.logprior(model_instance, argvals_dict) end From 0abde5a1e67107ea829fc0c4ed8f8fe5363bc800 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:31:49 +0000 Subject: [PATCH 129/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 5fa15dabd..9167d0071 100644 --- a/src/model.jl +++ b/src/model.jl @@ -742,7 +742,7 @@ function logprior(model::Model, chain::AbstractMCMC.AbstractChains) vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) - DynamicPPL.logprior(model_instance, argvals_dict) + logprior(model, argvals_dict) end end From 825a2d286c279f390fc38cf8714267571403320b Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:32:15 +0000 Subject: [PATCH 130/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 9167d0071..fcf37ee1a 100644 --- a/src/model.jl +++ b/src/model.jl @@ -758,7 +758,7 @@ function Distributions.loglikelihood(model::Model, varinfo::AbstractVarInfo) end """ - loglikelihood(model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int) + loglikelihood(model::Model, chain::AbstractMCMC.AbstractChains) Return an array of log likelihoods evaluated at each sample in an MCMC chain or sample array. From 78e511b5a0ec50df9c71b57e9b1d1dfe5dd8addd Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:37:29 +0000 Subject: [PATCH 131/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index fcf37ee1a..c3cd27028 100644 --- a/src/model.jl +++ b/src/model.jl @@ -760,7 +760,7 @@ end """ loglikelihood(model::Model, chain::AbstractMCMC.AbstractChains) -Return an array of log likelihoods evaluated at each sample in an MCMC chain or sample array. +Return an array of log likelihoods evaluated at each sample in an MCMC `chain`. Example: From aea5fdf829ae48959973cf0d9a05c624d5ee4885 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:38:41 +0000 Subject: [PATCH 132/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index c3cd27028..3c87c514e 100644 --- a/src/model.jl +++ b/src/model.jl @@ -762,7 +762,7 @@ end Return an array of log likelihoods evaluated at each sample in an MCMC `chain`. -Example: +# Examples ```jldoctest; setup = :(using MCMCChains, StableRNGs) julia> # From c544a06d1cc3c56983915977b1714155573d20f6 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:40:10 +0000 Subject: [PATCH 133/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 3c87c514e..8ced71bd2 100644 --- a/src/model.jl +++ b/src/model.jl @@ -764,7 +764,9 @@ Return an array of log likelihoods evaluated at each sample in an MCMC `chain`. # Examples -```jldoctest; setup = :(using MCMCChains, StableRNGs) +```jldoctest +julia> using MCMCChains, StableRNGs + julia> # @model function demo_model(x) s ~ InverseGamma(2, 3) From 9e3d260984e8ec2b2e7ca4bc937427d240c61285 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:47:54 +0000 Subject: [PATCH 134/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/model.jl b/src/model.jl index 8ced71bd2..171a3248a 100644 --- a/src/model.jl +++ b/src/model.jl @@ -767,8 +767,7 @@ Return an array of log likelihoods evaluated at each sample in an MCMC `chain`. ```jldoctest julia> using MCMCChains, StableRNGs -julia> # - @model function demo_model(x) +julia> @model function demo_model(x) s ~ InverseGamma(2, 3) m ~ Normal(0, sqrt(s)) for i in 1:length(x) From a0dbb13bcdfc89bd707c79dfd6dc4e0714b525c2 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:48:57 +0000 Subject: [PATCH 135/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 171a3248a..ffdb993dc 100644 --- a/src/model.jl +++ b/src/model.jl @@ -771,7 +771,7 @@ julia> @model function demo_model(x) s ~ InverseGamma(2, 3) m ~ Normal(0, sqrt(s)) for i in 1:length(x) - x[i] ~ Normal(m, sqrt(s)) + x[i] ~ Normal(m, sqrt(s)) end end demo_model (generic function with 2 methods) From b06a374f695dc01680f61766d9ef8fd43f9496eb Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 10:50:18 +0000 Subject: [PATCH 136/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index ffdb993dc..322b379da 100644 --- a/src/model.jl +++ b/src/model.jl @@ -670,7 +670,7 @@ julia> using MCMCChains, StableRNGs julia> @model function demo_model(x) s ~ InverseGamma(2, 3) m ~ Normal(0, sqrt(s)) - for i in 1:length(x) + for i in eachindex(x) x[i] ~ Normal(m, sqrt(s)) end end; From 33fb855bad7fd89a8b04af4a47ecaa1f4b66bfb5 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 13:25:23 +0000 Subject: [PATCH 137/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 322b379da..b3591b984 100644 --- a/src/model.jl +++ b/src/model.jl @@ -720,7 +720,7 @@ julia> using MCMCChains, StableRNGs julia> @model function demo_model(x) s ~ InverseGamma(2, 3) m ~ Normal(0, sqrt(s)) - for i in 1:length(x) + for i in eachindex(x) x[i] ~ Normal(m, sqrt(s)) end end; From f7f68b8b6b2bcb63e07f80d9f3cd8cabfd0d2128 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 13:48:12 +0000 Subject: [PATCH 138/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index b3591b984..ca0494d48 100644 --- a/src/model.jl +++ b/src/model.jl @@ -770,7 +770,7 @@ julia> using MCMCChains, StableRNGs julia> @model function demo_model(x) s ~ InverseGamma(2, 3) m ~ Normal(0, sqrt(s)) - for i in 1:length(x) + for i in eachindex(x) x[i] ~ Normal(m, sqrt(s)) end end From bf95fb0f16caaa7e34fd08ab95e6a79e67cec0a4 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 13:49:33 +0000 Subject: [PATCH 139/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/model.jl b/src/model.jl index ca0494d48..3df0ff901 100644 --- a/src/model.jl +++ b/src/model.jl @@ -773,8 +773,7 @@ julia> @model function demo_model(x) for i in eachindex(x) x[i] ~ Normal(m, sqrt(s)) end - end -demo_model (generic function with 2 methods) + end; julia> rng = StableRNG(123) StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) From 88662d1ac7b926c9b0c0c784173e1ec99a418331 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 13:50:46 +0000 Subject: [PATCH 140/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/model.jl b/src/model.jl index 3df0ff901..de7b3096a 100644 --- a/src/model.jl +++ b/src/model.jl @@ -775,12 +775,8 @@ julia> @model function demo_model(x) end end; -julia> rng = StableRNG(123) -StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) - -julia> val = rand(rng, 10, 2, 3); - -julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCChains +julia> # construct a chain of samples using MCMCChains + chain = Chains(rand(StableRNG(123), 10, 2, 3), [:s, :m]); julia> loglikelihood(demo_model([1., 2.]), chain); ``` From de3720fe083dcf01f38919cb7d850cd9b59e4eab Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 13:51:49 +0000 Subject: [PATCH 141/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/model.jl b/src/model.jl index de7b3096a..ead5620fc 100644 --- a/src/model.jl +++ b/src/model.jl @@ -675,12 +675,8 @@ julia> @model function demo_model(x) end end; -julia> rng = StableRNG(123) -StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) - -julia> val = rand(rng, 10, 2, 3); - -julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCChains +julia> # construct a chain of samples using MCMCChains + chain = Chains(rand(StableRNG(123), 10, 2, 3), [:s, :m]); julia> logjoint(demo_model([1., 2.]), chain); ``` From 7cf464da2e130b86d730c789bc88befe4c51f9b6 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 13:52:58 +0000 Subject: [PATCH 142/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/model.jl b/src/model.jl index ead5620fc..6afcd4215 100644 --- a/src/model.jl +++ b/src/model.jl @@ -721,12 +721,8 @@ julia> @model function demo_model(x) end end; -julia> rng = StableRNG(123) -StableRNGs.LehmerRNG(state=0x000000000000000000000000000000f7) - -julia> val = rand(rng, 10, 2, 3); - -julia> chain = Chains(val, [:s, :m]); # construct a chain of samples using MCMCChains +julia> # construct a chain of samples using MCMCChains + chain = Chains(rand(StableRNG(123), 10, 2, 3), [:s, :m]); julia> logprior(demo_model([1., 2.]), chain); ``` From 94cff03566925eaccf7fde95cc3d6a43a61f457f Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 13:54:00 +0000 Subject: [PATCH 143/218] Update test/model.jl Co-authored-by: David Widmann --- test/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index 15cd74ffc..8d3a5b849 100644 --- a/test/model.jl +++ b/test/model.jl @@ -153,7 +153,7 @@ end @testset "logp.jl" begin m = DynamicPPL.TestUtils.DEMO_MODELS[1] - Test.@testset "$(m.f)" begin + @testset "$(m.f)" begin # generate a chain of sample parameter values. N = 200 start_idx = 100 From a96fc437aeaf29bf49f1dc94877203648195124d Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 13:59:24 +0000 Subject: [PATCH 144/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/model.jl b/src/model.jl index 6afcd4215..388288560 100644 --- a/src/model.jl +++ b/src/model.jl @@ -773,9 +773,7 @@ julia> # construct a chain of samples using MCMCChains julia> loglikelihood(demo_model([1., 2.]), chain); ``` """ -function Distributions.loglikelihood( - model_instance::Model, chain::AbstractMCMC.AbstractChains, start_idx::Int=1 -) +function Distributions.loglikelihood(model::Model, chain::AbstractMCMC.AbstractChains) vi = VarInfo(model_instance) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( From 322ad7bb6d0c7de1b32d39867f07693931ca04e2 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 14:01:02 +0000 Subject: [PATCH 145/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 388288560..172de2ca9 100644 --- a/src/model.jl +++ b/src/model.jl @@ -774,7 +774,7 @@ julia> loglikelihood(demo_model([1., 2.]), chain); ``` """ function Distributions.loglikelihood(model::Model, chain::AbstractMCMC.AbstractChains) - vi = VarInfo(model_instance) # extract variables info from the model + vi = VarInfo(model) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for From b46bb44c458d8c79102610a26e1202c71fe8d462 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 14:04:43 +0000 Subject: [PATCH 146/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 172de2ca9..729dd946b 100644 --- a/src/model.jl +++ b/src/model.jl @@ -778,7 +778,7 @@ function Distributions.loglikelihood(model::Model, chain::AbstractMCMC.AbstractC map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in DynamicPPL.TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) Distributions.loglikelihood(model_instance, argvals_dict) end From c6bda2beeb1690444aee2f21b70e341fb7d7370b Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 23 Jan 2023 14:05:51 +0000 Subject: [PATCH 147/218] Update src/model.jl Co-authored-by: David Widmann --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index 729dd946b..29fdc4351 100644 --- a/src/model.jl +++ b/src/model.jl @@ -780,7 +780,7 @@ function Distributions.loglikelihood(model::Model, chain::AbstractMCMC.AbstractC vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) - Distributions.loglikelihood(model_instance, argvals_dict) + loglikelihood(model, argvals_dict) end end From f3c67b1e606d15d30588db0c151da84e2f3f55ca Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Fri, 27 Jan 2023 18:54:45 +0000 Subject: [PATCH 148/218] Added `logprior_true(model,NamedTuple)' and `loglikelihood_true(model, NamedTuple)' methods; revised test/model.jl accordingly (removed `MCMCChains.get_param()' ). --- src/test_utils.jl | 6 ++++++ test/model.jl | 39 +++++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/test_utils.jl b/src/test_utils.jl index 605952d88..1983c53f2 100644 --- a/src/test_utils.jl +++ b/src/test_utils.jl @@ -232,9 +232,15 @@ end function logprior_true(model::Model{typeof(demo_dot_assume_dot_observe)}, s, m) return loglikelihood(InverseGamma(2, 3), s) + sum(logpdf.(Normal.(0, sqrt.(s)), m)) end +function logprior_true(model::Model{typeof(DynamicPPL.TestUtils.demo_dot_assume_dot_observe)}, nt::NamedTuple{(:m, :s), Tuple{Vector{Float64}, Vector{Float64}}}) + return loglikelihood(InverseGamma(2, 3), nt[:s]) + sum(logpdf.(Normal.(0, sqrt.(nt[:s])), nt[:m])) +end function loglikelihood_true(model::Model{typeof(demo_dot_assume_dot_observe)}, s, m) return loglikelihood(MvNormal(m, Diagonal(s)), model.args.x) end +function loglikelihood_true(model::Model{typeof(DynamicPPL.TestUtils.demo_dot_assume_dot_observe)}, nt::NamedTuple{(:m, :s), Tuple{Vector{Float64}, Vector{Float64}}}) + return loglikelihood(MvNormal(nt[:m], Diagonal(nt[:s])), model.args.x) +end function logprior_true_with_logabsdet_jacobian( model::Model{typeof(demo_dot_assume_dot_observe)}, s, m ) diff --git a/test/model.jl b/test/model.jl index 8d3a5b849..846aa49c8 100644 --- a/test/model.jl +++ b/test/model.jl @@ -153,37 +153,36 @@ end @testset "logp.jl" begin m = DynamicPPL.TestUtils.DEMO_MODELS[1] + vi = VarInfo(m) @testset "$(m.f)" begin # generate a chain of sample parameter values. N = 200 - start_idx = 100 - logpriors_true = Vector{Float64}(undef, N - start_idx) - loglikelihoods_true = Vector{Float64}(undef, N - start_idx) - logposteriors_true = Vector{Float64}(undef, N - start_idx) + logpriors_true = Vector{Float64}(undef, N) + loglikelihoods_true = Vector{Float64}(undef, N) + logposteriors_true = Vector{Float64}(undef, N) d = rand(Dict, m) val = rand(N, length(collect(values(d))), 1) chain = Chains(val, string.(collect(keys(d)))) # construct a chain of samples using MCMCChains - map((start_idx + 1):N) do i - val = get_params(chain[i, :, :]) - example_values = ( - s=collect(Iterators.flatten(val.s)), m=collect(Iterators.flatten(val.m)) - ) - logpriors_true[i - start_idx] = DynamicPPL.TestUtils.logprior_true( - m, example_values... - ) - loglikelihoods_true[i - start_idx] = DynamicPPL.TestUtils.loglikelihood_true( - m, example_values... - ) - logposteriors_true[i - start_idx] = - logpriors_true[i - start_idx] + loglikelihoods_true[i - start_idx] + map(1:N) do i + argvals_dict = OrderedDict( + vn => chain[i, Symbol(vn), 1] for vn_parent in keys(vi) for + vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) + setval!(vi, argvals_dict.vals, argvals_dict.keys) + argvals_dict_temp = Dict(vn_parent => collect(vi.metadata[vn_parent].vals) for vn_parent in propertynames(vi.metadata)) + example_values = NamedTuple{(collect(keys(argvals_dict_temp))...,)}((collect(values(argvals_dict_temp))...,)) + + logpriors_true[i] = logprior_true(m, example_values) + loglikelihoods_true[i] = loglikelihood_true(m, example_values) + logposteriors_true[i] = logpriors_true[i] + loglikelihoods_true[i] end # calculate the pointwise loglikelihoods for the whole chain using custom logprior. - logpriors_new = logprior(m, chain, start_idx + 1) - loglikelihoods_new = loglikelihood(m, chain, start_idx + 1) - logposteriors_new = logjoint(m, chain, start_idx + 1) + logpriors_new = logprior(m, chain) + loglikelihoods_new = loglikelihood(m, chain) + logposteriors_new = logjoint(m, chain) # compare the likelihoods @test logpriors_new ≈ logpriors_true @test loglikelihoods_new ≈ loglikelihoods_true From 6e9639bb45db7b3741beb750d37995d589d4e6cb Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 27 Jan 2023 19:05:33 +0000 Subject: [PATCH 149/218] Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/model.jl b/test/model.jl index 846aa49c8..4cd31e52f 100644 --- a/test/model.jl +++ b/test/model.jl @@ -168,9 +168,9 @@ end map(1:N) do i argvals_dict = OrderedDict( - vn => chain[i, Symbol(vn), 1] for vn_parent in keys(vi) for - vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) + vn => chain[i, Symbol(vn), 1] for vn_parent in keys(vi) for + vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) setval!(vi, argvals_dict.vals, argvals_dict.keys) argvals_dict_temp = Dict(vn_parent => collect(vi.metadata[vn_parent].vals) for vn_parent in propertynames(vi.metadata)) example_values = NamedTuple{(collect(keys(argvals_dict_temp))...,)}((collect(values(argvals_dict_temp))...,)) From 339ef0d2b82d1a497913530f8eb96810fd04caaa Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 27 Jan 2023 19:06:19 +0000 Subject: [PATCH 150/218] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/model.jl b/test/model.jl index 4cd31e52f..9a73d9510 100644 --- a/test/model.jl +++ b/test/model.jl @@ -172,9 +172,14 @@ end vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) setval!(vi, argvals_dict.vals, argvals_dict.keys) - argvals_dict_temp = Dict(vn_parent => collect(vi.metadata[vn_parent].vals) for vn_parent in propertynames(vi.metadata)) - example_values = NamedTuple{(collect(keys(argvals_dict_temp))...,)}((collect(values(argvals_dict_temp))...,)) - + argvals_dict_temp = Dict( + vn_parent => collect(vi.metadata[vn_parent].vals) for + vn_parent in propertynames(vi.metadata) + ) + example_values = NamedTuple{(collect(keys(argvals_dict_temp))...,)}(( + collect(values(argvals_dict_temp))..., + )) + logpriors_true[i] = logprior_true(m, example_values) loglikelihoods_true[i] = loglikelihood_true(m, example_values) logposteriors_true[i] = logpriors_true[i] + loglikelihoods_true[i] From 0984762d9936a6f8e6f92ff43c6e8d4fdc1efce0 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 27 Jan 2023 19:31:35 +0000 Subject: [PATCH 151/218] Fixed missing prefix and imports. --- test/model.jl | 6 +++--- test/runtests.jl | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/model.jl b/test/model.jl index 9a73d9510..7d9d8b68a 100644 --- a/test/model.jl +++ b/test/model.jl @@ -171,7 +171,7 @@ end vn => chain[i, Symbol(vn), 1] for vn_parent in keys(vi) for vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) ) - setval!(vi, argvals_dict.vals, argvals_dict.keys) + DynamicPPL.setval!(vi, argvals_dict.vals, argvals_dict.keys) argvals_dict_temp = Dict( vn_parent => collect(vi.metadata[vn_parent].vals) for vn_parent in propertynames(vi.metadata) @@ -180,8 +180,8 @@ end collect(values(argvals_dict_temp))..., )) - logpriors_true[i] = logprior_true(m, example_values) - loglikelihoods_true[i] = loglikelihood_true(m, example_values) + logpriors_true[i] = TestUtils.logprior_true(m, example_values) + loglikelihoods_true[i] = TestUtils.loglikelihood_true(m, example_values) logposteriors_true[i] = logpriors_true[i] + loglikelihoods_true[i] end # calculate the pointwise loglikelihoods for the whole chain using custom logprior. diff --git a/test/runtests.jl b/test/runtests.jl index 6b79ce4d7..d8ddde53b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,7 +19,7 @@ using Random using Serialization using Test -using DynamicPPL: getargs_dottilde, getargs_tilde, Selector +using DynamicPPL: getargs_dottilde, getargs_tilde, Selector, TestUtils const DIRECTORY_DynamicPPL = dirname(dirname(pathof(DynamicPPL))) const DIRECTORY_Turing_tests = joinpath(DIRECTORY_DynamicPPL, "test", "turing") From 2df3e65b9a3c1cb35864711180e6657270452e5f Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 27 Jan 2023 19:35:57 +0000 Subject: [PATCH 152/218] Move tests into convenience functions. --- test/model.jl | 84 +++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/test/model.jl b/test/model.jl index 7d9d8b68a..e3d533612 100644 --- a/test/model.jl +++ b/test/model.jl @@ -36,6 +36,47 @@ end ljoint = logjoint(model, vi) @test ljoint ≈ lprior + llikelihood @test ljoint ≈ lp + + # logprior, logjoint, loglikelihood for MCMC chains + m = DynamicPPL.TestUtils.DEMO_MODELS[1] + vi = VarInfo(m) + # generate a chain of sample parameter values. + N = 200 + + logpriors_true = Vector{Float64}(undef, N) + loglikelihoods_true = Vector{Float64}(undef, N) + logposteriors_true = Vector{Float64}(undef, N) + + d = rand(Dict, m) + val = rand(N, length(collect(values(d))), 1) + chain = Chains(val, string.(collect(keys(d)))) # construct a chain of samples using MCMCChains + + map(1:N) do i + argvals_dict = OrderedDict( + vn => chain[i, Symbol(vn), 1] for vn_parent in keys(vi) for + vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + ) + DynamicPPL.setval!(vi, argvals_dict.vals, argvals_dict.keys) + argvals_dict_temp = Dict( + vn_parent => collect(vi.metadata[vn_parent].vals) for + vn_parent in propertynames(vi.metadata) + ) + example_values = NamedTuple{(collect(keys(argvals_dict_temp))...,)}(( + collect(values(argvals_dict_temp))..., + )) + + logpriors_true[i] = TestUtils.logprior_true(m, example_values) + loglikelihoods_true[i] = TestUtils.loglikelihood_true(m, example_values) + logposteriors_true[i] = logpriors_true[i] + loglikelihoods_true[i] + end + # calculate the pointwise loglikelihoods for the whole chain using custom logprior. + logpriors_new = logprior(m, chain) + loglikelihoods_new = loglikelihood(m, chain) + logposteriors_new = logjoint(m, chain) + # compare the likelihoods + @test logpriors_new ≈ logpriors_true + @test loglikelihoods_new ≈ loglikelihoods_true + @test logposteriors_new ≈ logposteriors_true end @testset "rng" begin @@ -151,46 +192,3 @@ end end end -@testset "logp.jl" begin - m = DynamicPPL.TestUtils.DEMO_MODELS[1] - vi = VarInfo(m) - @testset "$(m.f)" begin - # generate a chain of sample parameter values. - N = 200 - - logpriors_true = Vector{Float64}(undef, N) - loglikelihoods_true = Vector{Float64}(undef, N) - logposteriors_true = Vector{Float64}(undef, N) - - d = rand(Dict, m) - val = rand(N, length(collect(values(d))), 1) - chain = Chains(val, string.(collect(keys(d)))) # construct a chain of samples using MCMCChains - - map(1:N) do i - argvals_dict = OrderedDict( - vn => chain[i, Symbol(vn), 1] for vn_parent in keys(vi) for - vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) - DynamicPPL.setval!(vi, argvals_dict.vals, argvals_dict.keys) - argvals_dict_temp = Dict( - vn_parent => collect(vi.metadata[vn_parent].vals) for - vn_parent in propertynames(vi.metadata) - ) - example_values = NamedTuple{(collect(keys(argvals_dict_temp))...,)}(( - collect(values(argvals_dict_temp))..., - )) - - logpriors_true[i] = TestUtils.logprior_true(m, example_values) - loglikelihoods_true[i] = TestUtils.loglikelihood_true(m, example_values) - logposteriors_true[i] = logpriors_true[i] + loglikelihoods_true[i] - end - # calculate the pointwise loglikelihoods for the whole chain using custom logprior. - logpriors_new = logprior(m, chain) - loglikelihoods_new = loglikelihood(m, chain) - logposteriors_new = logjoint(m, chain) - # compare the likelihoods - @test logpriors_new ≈ logpriors_true - @test loglikelihoods_new ≈ loglikelihoods_true - @test logposteriors_new ≈ logposteriors_true - end -end From 37c477cae446a2f3baee83b8d6b818512ddbf885 Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Fri, 27 Jan 2023 19:40:50 +0000 Subject: [PATCH 153/218] Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index e3d533612..a57790eb6 100644 --- a/test/model.jl +++ b/test/model.jl @@ -191,4 +191,3 @@ end @test rand(Dict, model) == sample_dict end end - From a717419eafc78d5d7e4650030a48023f2d3c5d30 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 27 Jan 2023 20:42:38 +0000 Subject: [PATCH 154/218] Removed constraints on floating number precision. --- src/test_utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test_utils.jl b/src/test_utils.jl index 1983c53f2..6849bc32d 100644 --- a/src/test_utils.jl +++ b/src/test_utils.jl @@ -232,7 +232,7 @@ end function logprior_true(model::Model{typeof(demo_dot_assume_dot_observe)}, s, m) return loglikelihood(InverseGamma(2, 3), s) + sum(logpdf.(Normal.(0, sqrt.(s)), m)) end -function logprior_true(model::Model{typeof(DynamicPPL.TestUtils.demo_dot_assume_dot_observe)}, nt::NamedTuple{(:m, :s), Tuple{Vector{Float64}, Vector{Float64}}}) +function logprior_true(model::Model{typeof(DynamicPPL.TestUtils.demo_dot_assume_dot_observe)}, nt::NamedTuple{(:m, :s), Tuple{Vector{<:AbstractFloat}, Vector{<:AbstractFloat}}}) return loglikelihood(InverseGamma(2, 3), nt[:s]) + sum(logpdf.(Normal.(0, sqrt.(nt[:s])), nt[:m])) end function loglikelihood_true(model::Model{typeof(demo_dot_assume_dot_observe)}, s, m) From 8eeed1cbc8feb733d08cef993f98dde682488d99 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 27 Jan 2023 21:00:02 +0000 Subject: [PATCH 155/218] Fix type constraint again. --- src/test_utils.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test_utils.jl b/src/test_utils.jl index 6849bc32d..b4132085c 100644 --- a/src/test_utils.jl +++ b/src/test_utils.jl @@ -232,13 +232,13 @@ end function logprior_true(model::Model{typeof(demo_dot_assume_dot_observe)}, s, m) return loglikelihood(InverseGamma(2, 3), s) + sum(logpdf.(Normal.(0, sqrt.(s)), m)) end -function logprior_true(model::Model{typeof(DynamicPPL.TestUtils.demo_dot_assume_dot_observe)}, nt::NamedTuple{(:m, :s), Tuple{Vector{<:AbstractFloat}, Vector{<:AbstractFloat}}}) +function logprior_true(model::Model{typeof(DynamicPPL.TestUtils.demo_dot_assume_dot_observe)}, nt::NamedTuple{(:m, :s), Tuple{Vector{F}, Vector{F}}}) where {F<:AbstractFloat} return loglikelihood(InverseGamma(2, 3), nt[:s]) + sum(logpdf.(Normal.(0, sqrt.(nt[:s])), nt[:m])) end function loglikelihood_true(model::Model{typeof(demo_dot_assume_dot_observe)}, s, m) return loglikelihood(MvNormal(m, Diagonal(s)), model.args.x) end -function loglikelihood_true(model::Model{typeof(DynamicPPL.TestUtils.demo_dot_assume_dot_observe)}, nt::NamedTuple{(:m, :s), Tuple{Vector{Float64}, Vector{Float64}}}) +function loglikelihood_true(model::Model{typeof(DynamicPPL.TestUtils.demo_dot_assume_dot_observe)}, nt::NamedTuple{(:m, :s), Tuple{Vector{F}, Vector{F}}}) where {F<:AbstractFloat} return loglikelihood(MvNormal(nt[:m], Diagonal(nt[:s])), model.args.x) end function logprior_true_with_logabsdet_jacobian( From f34dd291aeace7cd9c883592d4a253ff38bf8de3 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 27 Jan 2023 21:39:45 +0000 Subject: [PATCH 156/218] Apply suggestions from code review Co-authored-by: David Widmann --- src/model.jl | 6 +++--- src/test_utils.jl | 4 ++-- test/runtests.jl | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/model.jl b/src/model.jl index 29fdc4351..6388be9ee 100644 --- a/src/model.jl +++ b/src/model.jl @@ -686,7 +686,7 @@ function logjoint(model::Model, chain::AbstractMCMC.AbstractChains) map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn in varname_leaves(vn_parent, vi[vn_parent]) ) loglikelihood(model, argvals_dict) + logprior(model, argvals_dict) end @@ -732,7 +732,7 @@ function logprior(model::Model, chain::AbstractMCMC.AbstractChains) map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn in varname_leaves(vn_parent, vi[vn_parent]) ) logprior(model, argvals_dict) end @@ -778,7 +778,7 @@ function Distributions.loglikelihood(model::Model, chain::AbstractMCMC.AbstractC map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) + vn in varname_leaves(vn_parent, vi[vn_parent]) ) loglikelihood(model, argvals_dict) end diff --git a/src/test_utils.jl b/src/test_utils.jl index b4132085c..eae2696a1 100644 --- a/src/test_utils.jl +++ b/src/test_utils.jl @@ -232,13 +232,13 @@ end function logprior_true(model::Model{typeof(demo_dot_assume_dot_observe)}, s, m) return loglikelihood(InverseGamma(2, 3), s) + sum(logpdf.(Normal.(0, sqrt.(s)), m)) end -function logprior_true(model::Model{typeof(DynamicPPL.TestUtils.demo_dot_assume_dot_observe)}, nt::NamedTuple{(:m, :s), Tuple{Vector{F}, Vector{F}}}) where {F<:AbstractFloat} +function logprior_true(model::Model{typeof(demo_dot_assume_dot_observe)}, nt::NamedTuple{(:m, :s), Tuple{Vector{F}, Vector{F}}}) where {F<:AbstractFloat} return loglikelihood(InverseGamma(2, 3), nt[:s]) + sum(logpdf.(Normal.(0, sqrt.(nt[:s])), nt[:m])) end function loglikelihood_true(model::Model{typeof(demo_dot_assume_dot_observe)}, s, m) return loglikelihood(MvNormal(m, Diagonal(s)), model.args.x) end -function loglikelihood_true(model::Model{typeof(DynamicPPL.TestUtils.demo_dot_assume_dot_observe)}, nt::NamedTuple{(:m, :s), Tuple{Vector{F}, Vector{F}}}) where {F<:AbstractFloat} +function loglikelihood_true(model::Model{typeof(demo_dot_assume_dot_observe)}, nt::NamedTuple{(:m, :s), Tuple{Vector{F}, Vector{F}}}) where {F<:AbstractFloat} return loglikelihood(MvNormal(nt[:m], Diagonal(nt[:s])), model.args.x) end function logprior_true_with_logabsdet_jacobian( diff --git a/test/runtests.jl b/test/runtests.jl index d8ddde53b..6b79ce4d7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,7 +19,7 @@ using Random using Serialization using Test -using DynamicPPL: getargs_dottilde, getargs_tilde, Selector, TestUtils +using DynamicPPL: getargs_dottilde, getargs_tilde, Selector const DIRECTORY_DynamicPPL = dirname(dirname(pathof(DynamicPPL))) const DIRECTORY_Turing_tests = joinpath(DIRECTORY_DynamicPPL, "test", "turing") From 4400f48dd37d2e986be05eaf70bba3a92218afb4 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Fri, 27 Jan 2023 21:59:14 +0000 Subject: [PATCH 157/218] 1. removed `StableRNGs`; 2. replaced `map(1:N) do i` in test/model.jl by `for i in 1:N`. --- src/model.jl | 12 ++++++------ test/model.jl | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/model.jl b/src/model.jl index 6388be9ee..a5494a474 100644 --- a/src/model.jl +++ b/src/model.jl @@ -665,7 +665,7 @@ Return an array of log joint probabilities evaluated at each sample in an MCMC ` # Examples ```jldoctest -julia> using MCMCChains, StableRNGs +julia> using MCMCChains julia> @model function demo_model(x) s ~ InverseGamma(2, 3) @@ -676,7 +676,7 @@ julia> @model function demo_model(x) end; julia> # construct a chain of samples using MCMCChains - chain = Chains(rand(StableRNG(123), 10, 2, 3), [:s, :m]); + chain = Chains(rand(10, 2, 3), [:s, :m]); julia> logjoint(demo_model([1., 2.]), chain); ``` @@ -711,7 +711,7 @@ Return an array of log prior probabilities evaluated at each sample in an MCMC ` # Examples ```jldoctest -julia> using MCMCChains, StableRNGs +julia> using MCMCChains julia> @model function demo_model(x) s ~ InverseGamma(2, 3) @@ -722,7 +722,7 @@ julia> @model function demo_model(x) end; julia> # construct a chain of samples using MCMCChains - chain = Chains(rand(StableRNG(123), 10, 2, 3), [:s, :m]); + chain = Chains(rand(10, 2, 3), [:s, :m]); julia> logprior(demo_model([1., 2.]), chain); ``` @@ -757,7 +757,7 @@ Return an array of log likelihoods evaluated at each sample in an MCMC `chain`. # Examples ```jldoctest -julia> using MCMCChains, StableRNGs +julia> using MCMCChains julia> @model function demo_model(x) s ~ InverseGamma(2, 3) @@ -768,7 +768,7 @@ julia> @model function demo_model(x) end; julia> # construct a chain of samples using MCMCChains - chain = Chains(rand(StableRNG(123), 10, 2, 3), [:s, :m]); + chain = Chains(rand(10, 2, 3), [:s, :m]); julia> loglikelihood(demo_model([1., 2.]), chain); ``` diff --git a/test/model.jl b/test/model.jl index a57790eb6..ce72d677b 100644 --- a/test/model.jl +++ b/test/model.jl @@ -51,7 +51,7 @@ end val = rand(N, length(collect(values(d))), 1) chain = Chains(val, string.(collect(keys(d)))) # construct a chain of samples using MCMCChains - map(1:N) do i + for i in 1:N argvals_dict = OrderedDict( vn => chain[i, Symbol(vn), 1] for vn_parent in keys(vi) for vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) From 33e5ee57ae1c538bdd919f7a00e7a6dffb5a45c1 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 27 Jan 2023 22:15:41 +0000 Subject: [PATCH 158/218] Bugfix. --- src/test_utils.jl | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/test_utils.jl b/src/test_utils.jl index b6649bc99..5d78e3e49 100644 --- a/src/test_utils.jl +++ b/src/test_utils.jl @@ -214,15 +214,14 @@ end function logprior_true(model::Model{typeof(demo_dot_assume_dot_observe)}, s, m) return loglikelihood(InverseGamma(2, 3), s) + sum(logpdf.(Normal.(0, sqrt.(s)), m)) end -function logprior_true(model::Model{typeof(demo_dot_assume_dot_observe)}, nt::NamedTuple{(:m, :s), Tuple{Vector{F}, Vector{F}}}) where {F<:AbstractFloat} - return loglikelihood(InverseGamma(2, 3), nt[:s]) + sum(logpdf.(Normal.(0, sqrt.(nt[:s])), nt[:m])) -end function loglikelihood_true(model::Model{typeof(demo_dot_assume_dot_observe)}, s, m) return loglikelihood(MvNormal(m, Diagonal(s)), model.args.x) end -function loglikelihood_true(model::Model{typeof(demo_dot_assume_dot_observe)}, nt::NamedTuple{(:m, :s), Tuple{Vector{F}, Vector{F}}}) where {F<:AbstractFloat} - return loglikelihood(MvNormal(nt[:m], Diagonal(nt[:s])), model.args.x) -end + +# generic interfaces for namedtuple inputs; see https://github.com/TuringLang/DynamicPPL.jl/pull/438 +logprior_true(model, nt::NamedTuple) = logprior_true(model, nt[:s], nt[:m]) +loglikelihood_true(model, nt::NamedTuple) = loglikelihood_true(model, nt[:s], nt[:m]) + function logprior_true_with_logabsdet_jacobian( model::Model{typeof(demo_dot_assume_dot_observe)}, s, m ) From a5d36713859b278b7caa91541d3a4dd749ecc9b5 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 27 Jan 2023 22:34:38 +0000 Subject: [PATCH 159/218] Import TestUtils -- it is not exported by DPPL. --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 6b79ce4d7..d8ddde53b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,7 +19,7 @@ using Random using Serialization using Test -using DynamicPPL: getargs_dottilde, getargs_tilde, Selector +using DynamicPPL: getargs_dottilde, getargs_tilde, Selector, TestUtils const DIRECTORY_DynamicPPL = dirname(dirname(pathof(DynamicPPL))) const DIRECTORY_Turing_tests = joinpath(DIRECTORY_DynamicPPL, "test", "turing") From be2c2edcd77f936a9149fb9ce5919b38aa9ce72d Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 27 Jan 2023 23:17:19 +0000 Subject: [PATCH 160/218] Specialise on model type. --- src/test_utils.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test_utils.jl b/src/test_utils.jl index 5d78e3e49..233d53cd5 100644 --- a/src/test_utils.jl +++ b/src/test_utils.jl @@ -219,8 +219,8 @@ function loglikelihood_true(model::Model{typeof(demo_dot_assume_dot_observe)}, s end # generic interfaces for namedtuple inputs; see https://github.com/TuringLang/DynamicPPL.jl/pull/438 -logprior_true(model, nt::NamedTuple) = logprior_true(model, nt[:s], nt[:m]) -loglikelihood_true(model, nt::NamedTuple) = loglikelihood_true(model, nt[:s], nt[:m]) +logprior_true(model::Model{typeof(demo_dot_assume_dot_observe)}, nt::NamedTuple) = logprior_true(model, nt[:s], nt[:m]) +loglikelihood_true(model::Model{typeof(demo_dot_assume_dot_observe)}, nt::NamedTuple) = loglikelihood_true(model, nt[:s], nt[:m]) function logprior_true_with_logabsdet_jacobian( model::Model{typeof(demo_dot_assume_dot_observe)}, s, m From 0a93a2170cc85cd32a44e35f23f7c421e5bbbb64 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Sat, 28 Jan 2023 01:00:28 +0000 Subject: [PATCH 161/218] Improve test. --- test/model.jl | 53 +++++++++++++++++---------------------------------- 1 file changed, 18 insertions(+), 35 deletions(-) diff --git a/test/model.jl b/test/model.jl index ce72d677b..3f0b1b77d 100644 --- a/test/model.jl +++ b/test/model.jl @@ -38,45 +38,28 @@ end @test ljoint ≈ lp # logprior, logjoint, loglikelihood for MCMC chains - m = DynamicPPL.TestUtils.DEMO_MODELS[1] - vi = VarInfo(m) + model = DynamicPPL.TestUtils.DEMO_MODELS[1] + vns = DynamicPPL.TestUtils.varnames(model) + syms = unique!(map(DynamicPPL.getsym, vns)) + # generate a chain of sample parameter values. N = 200 - - logpriors_true = Vector{Float64}(undef, N) - loglikelihoods_true = Vector{Float64}(undef, N) - logposteriors_true = Vector{Float64}(undef, N) - - d = rand(Dict, m) - val = rand(N, length(collect(values(d))), 1) - chain = Chains(val, string.(collect(keys(d)))) # construct a chain of samples using MCMCChains - + vals = mapreduce(hcat, 1:N) do _ + samples = rand(Dict, model) # order of samples is fixed below + [samples[vn] for vn in vns] + end + chain = Chains(vals', [Symbol(vn) for vn in vns]) + # calculate the pointwise loglikelihoods for the whole chain + logpriors = logprior(model, chain) + loglikelihoods = loglikelihood(model, chain) + logjoints = logjoint(model, chain) + # compare them with true values for i in 1:N - argvals_dict = OrderedDict( - vn => chain[i, Symbol(vn), 1] for vn_parent in keys(vi) for - vn in TestUtils.varname_leaves(vn_parent, vi[vn_parent]) - ) - DynamicPPL.setval!(vi, argvals_dict.vals, argvals_dict.keys) - argvals_dict_temp = Dict( - vn_parent => collect(vi.metadata[vn_parent].vals) for - vn_parent in propertynames(vi.metadata) - ) - example_values = NamedTuple{(collect(keys(argvals_dict_temp))...,)}(( - collect(values(argvals_dict_temp))..., - )) - - logpriors_true[i] = TestUtils.logprior_true(m, example_values) - loglikelihoods_true[i] = TestUtils.loglikelihood_true(m, example_values) - logposteriors_true[i] = logpriors_true[i] + loglikelihoods_true[i] + samples = [[chain[i, Symbol(vn), 1] for vn in vns if DynamicPPL.getsym(vn) === sym] for sym in syms] + @test logpriors[i] ≈ DynamicPPL.TestUtils.logprior_true(model, samples...) + @test loglikelihoods[i] ≈ DynamicPPL.TestUtils.loglikelihood_true(model, samples...) + @test logjoints[i] ≈ DynamicPPL.TestUtils.logjoint_true(model, samples...) end - # calculate the pointwise loglikelihoods for the whole chain using custom logprior. - logpriors_new = logprior(m, chain) - loglikelihoods_new = loglikelihood(m, chain) - logposteriors_new = logjoint(m, chain) - # compare the likelihoods - @test logpriors_new ≈ logpriors_true - @test loglikelihoods_new ≈ loglikelihoods_true - @test logposteriors_new ≈ logposteriors_true end @testset "rng" begin From 2f62fadb032f977fddc35d2a4b8a25f69bf055a4 Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Sat, 28 Jan 2023 01:31:27 +0000 Subject: [PATCH 162/218] Update src/test_utils.jl --- src/test_utils.jl | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/test_utils.jl b/src/test_utils.jl index 233d53cd5..b5f3a80b3 100644 --- a/src/test_utils.jl +++ b/src/test_utils.jl @@ -217,11 +217,6 @@ end function loglikelihood_true(model::Model{typeof(demo_dot_assume_dot_observe)}, s, m) return loglikelihood(MvNormal(m, Diagonal(s)), model.args.x) end - -# generic interfaces for namedtuple inputs; see https://github.com/TuringLang/DynamicPPL.jl/pull/438 -logprior_true(model::Model{typeof(demo_dot_assume_dot_observe)}, nt::NamedTuple) = logprior_true(model, nt[:s], nt[:m]) -loglikelihood_true(model::Model{typeof(demo_dot_assume_dot_observe)}, nt::NamedTuple) = loglikelihood_true(model, nt[:s], nt[:m]) - function logprior_true_with_logabsdet_jacobian( model::Model{typeof(demo_dot_assume_dot_observe)}, s, m ) From 1a8fa89edd89d715219f7f6ecadb196f0a07ceca Mon Sep 17 00:00:00 2001 From: Jose Storopoli Date: Sun, 29 Jan 2023 06:25:23 +0100 Subject: [PATCH 163/218] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/model.jl b/test/model.jl index 3f0b1b77d..ed6a498bf 100644 --- a/test/model.jl +++ b/test/model.jl @@ -41,7 +41,6 @@ end model = DynamicPPL.TestUtils.DEMO_MODELS[1] vns = DynamicPPL.TestUtils.varnames(model) syms = unique!(map(DynamicPPL.getsym, vns)) - # generate a chain of sample parameter values. N = 200 vals = mapreduce(hcat, 1:N) do _ @@ -55,9 +54,13 @@ end logjoints = logjoint(model, chain) # compare them with true values for i in 1:N - samples = [[chain[i, Symbol(vn), 1] for vn in vns if DynamicPPL.getsym(vn) === sym] for sym in syms] + samples = [ + [chain[i, Symbol(vn), 1] for vn in vns if DynamicPPL.getsym(vn) === sym] for + sym in syms + ] @test logpriors[i] ≈ DynamicPPL.TestUtils.logprior_true(model, samples...) - @test loglikelihoods[i] ≈ DynamicPPL.TestUtils.loglikelihood_true(model, samples...) + @test loglikelihoods[i] ≈ + DynamicPPL.TestUtils.loglikelihood_true(model, samples...) @test logjoints[i] ≈ DynamicPPL.TestUtils.logjoint_true(model, samples...) end end From 6945b4e5f5cbf99288fb7f99ecd34a1ee5d22868 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 30 Jan 2023 12:36:53 +0000 Subject: [PATCH 164/218] Apply suggestions from code review Co-authored-by: Tor Erlend Fjelde Co-authored-by: David Widmann --- src/model.jl | 2 +- test/model.jl | 3 +-- test/runtests.jl | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/model.jl b/src/model.jl index a5494a474..861a1d0c4 100644 --- a/src/model.jl +++ b/src/model.jl @@ -688,7 +688,7 @@ function logjoint(model::Model, chain::AbstractMCMC.AbstractChains) vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for vn in varname_leaves(vn_parent, vi[vn_parent]) ) - loglikelihood(model, argvals_dict) + logprior(model, argvals_dict) + logjoint(model, argvals_dict) end end diff --git a/test/model.jl b/test/model.jl index ed6a498bf..89922c663 100644 --- a/test/model.jl +++ b/test/model.jl @@ -44,8 +44,7 @@ end # generate a chain of sample parameter values. N = 200 vals = mapreduce(hcat, 1:N) do _ - samples = rand(Dict, model) # order of samples is fixed below - [samples[vn] for vn in vns] + samples = rand(OrderedDict, model) end chain = Chains(vals', [Symbol(vn) for vn in vns]) # calculate the pointwise loglikelihoods for the whole chain diff --git a/test/runtests.jl b/test/runtests.jl index d8ddde53b..6b79ce4d7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,7 +19,7 @@ using Random using Serialization using Test -using DynamicPPL: getargs_dottilde, getargs_tilde, Selector, TestUtils +using DynamicPPL: getargs_dottilde, getargs_tilde, Selector const DIRECTORY_DynamicPPL = dirname(dirname(pathof(DynamicPPL))) const DIRECTORY_Turing_tests = joinpath(DIRECTORY_DynamicPPL, "test", "turing") From d36b9ca03c154856dbd35aff86af0e8865cdc925 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 30 Jan 2023 13:52:53 +0000 Subject: [PATCH 165/218] Apply suggestions from code review Co-authored-by: David Widmann --- test/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index 89922c663..5e9d8db27 100644 --- a/test/model.jl +++ b/test/model.jl @@ -40,7 +40,7 @@ end # logprior, logjoint, loglikelihood for MCMC chains model = DynamicPPL.TestUtils.DEMO_MODELS[1] vns = DynamicPPL.TestUtils.varnames(model) - syms = unique!(map(DynamicPPL.getsym, vns)) + syms = unique(DynamicPPL.getsym, vns) # generate a chain of sample parameter values. N = 200 vals = mapreduce(hcat, 1:N) do _ From fd225a5f685af77b128e06318414edb3faf7611d Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Tue, 7 Feb 2023 18:51:24 +0000 Subject: [PATCH 166/218] midified the way chain value was extracted in all 3 methods. --- src/model.jl | 15 ++++++--------- src/utils.jl | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/model.jl b/src/model.jl index a5494a474..19e366213 100644 --- a/src/model.jl +++ b/src/model.jl @@ -682,11 +682,10 @@ julia> logjoint(demo_model([1., 2.]), chain); ``` """ function logjoint(model::Model, chain::AbstractMCMC.AbstractChains) - vi = VarInfo(model) # extract variables info from the model + var_info = VarInfo(model) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in varname_leaves(vn_parent, vi[vn_parent]) + vn_parent => values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) for vn_parent in keys(var_info) ) loglikelihood(model, argvals_dict) + logprior(model, argvals_dict) end @@ -728,11 +727,10 @@ julia> logprior(demo_model([1., 2.]), chain); ``` """ function logprior(model::Model, chain::AbstractMCMC.AbstractChains) - vi = VarInfo(model) # extract variables info from the model + var_info = VarInfo(model) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in varname_leaves(vn_parent, vi[vn_parent]) + vn_parent => values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) for vn_parent in keys(var_info) ) logprior(model, argvals_dict) end @@ -774,11 +772,10 @@ julia> loglikelihood(demo_model([1., 2.]), chain); ``` """ function Distributions.loglikelihood(model::Model, chain::AbstractMCMC.AbstractChains) - vi = VarInfo(model) # extract variables info from the model + var_info = VarInfo(model) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => chain[iteration_idx, Symbol(vn), chain_idx] for vn_parent in keys(vi) for - vn in varname_leaves(vn_parent, vi[vn_parent]) + vn => values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) for vn_parent in keys(var_info) ) loglikelihood(model, argvals_dict) end diff --git a/src/utils.jl b/src/utils.jl index 78595cb90..4e9b26beb 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -786,3 +786,26 @@ function varname_leaves(vn::DynamicPPL.VarName, val::NamedTuple) end return Iterators.flatten(iter) end + +function values_from_chain(x, vn_parent, chain, chain_idx, iteration_idx) + # HACK: If it's not an array, we fall back to just returning the first value. + return first(chain[iteration_idx, Symbol(vn_parent), chain_idx]) +end +function values_from_chain(x::AbstractArray, vn_parent::VarName{sym}, chain, chain_idx, iteration_idx) where {sym} + # We use `VarName{sym}()` so that the resulting leaf `vn` only contains the tail of the lens. + # This way we can use `getlens(vn)` to extract the value from `x` and use `vn_parent ∘ getlens(vn)` + # to extract the value from the `chain`. + return reduce(varname_leaves(VarName{sym}(), x); init=similar(x)) do x, vn + # Update `x`, possibly in place, and return. + l = AbstractPPL.getlens(vn) + Setfield.set( + x, + BangBang.prefermutation(l), + chain[iteration_idx, Symbol(vn_parent ∘ l), chain_idx] + ) + end +end +function values_from_chain(vi::AbstractVarInfo, vn_parent, chain, chain_idx, iteration_idx) + # Use the value `vi[vn_parent]` to obtain a buffer. + return values_from_chain(vi[vn_parent], vn_parent, chain, chain_idx, iteration_idx) +end \ No newline at end of file From c46cf5629cba2745ee0e7916a261c118403d48f7 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 7 Feb 2023 18:56:54 +0000 Subject: [PATCH 167/218] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/model.jl | 11 ++++++++--- src/utils.jl | 6 ++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/model.jl b/src/model.jl index f45629a73..efd0bf4b9 100644 --- a/src/model.jl +++ b/src/model.jl @@ -685,7 +685,9 @@ function logjoint(model::Model, chain::AbstractMCMC.AbstractChains) var_info = VarInfo(model) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn_parent => values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) for vn_parent in keys(var_info) + vn_parent => + values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) for + vn_parent in keys(var_info) ) logjoint(model, argvals_dict) end @@ -730,7 +732,9 @@ function logprior(model::Model, chain::AbstractMCMC.AbstractChains) var_info = VarInfo(model) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn_parent => values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) for vn_parent in keys(var_info) + vn_parent => + values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) for + vn_parent in keys(var_info) ) logprior(model, argvals_dict) end @@ -775,7 +779,8 @@ function Distributions.loglikelihood(model::Model, chain::AbstractMCMC.AbstractC var_info = VarInfo(model) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) for vn_parent in keys(var_info) + vn => values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) + for vn_parent in keys(var_info) ) loglikelihood(model, argvals_dict) end diff --git a/src/utils.jl b/src/utils.jl index 4e9b26beb..44ff2f62c 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -791,7 +791,9 @@ function values_from_chain(x, vn_parent, chain, chain_idx, iteration_idx) # HACK: If it's not an array, we fall back to just returning the first value. return first(chain[iteration_idx, Symbol(vn_parent), chain_idx]) end -function values_from_chain(x::AbstractArray, vn_parent::VarName{sym}, chain, chain_idx, iteration_idx) where {sym} +function values_from_chain( + x::AbstractArray, vn_parent::VarName{sym}, chain, chain_idx, iteration_idx +) where {sym} # We use `VarName{sym}()` so that the resulting leaf `vn` only contains the tail of the lens. # This way we can use `getlens(vn)` to extract the value from `x` and use `vn_parent ∘ getlens(vn)` # to extract the value from the `chain`. @@ -801,7 +803,7 @@ function values_from_chain(x::AbstractArray, vn_parent::VarName{sym}, chain, cha Setfield.set( x, BangBang.prefermutation(l), - chain[iteration_idx, Symbol(vn_parent ∘ l), chain_idx] + chain[iteration_idx, Symbol(vn_parent ∘ l), chain_idx], ) end end From 6fc57380faf0ef5be372280c71f5c77cad9f95f6 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 7 Feb 2023 19:03:27 +0000 Subject: [PATCH 168/218] Apply suggestions from code review Co-authored-by: David Widmann --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 44ff2f62c..5a6336854 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -789,7 +789,7 @@ end function values_from_chain(x, vn_parent, chain, chain_idx, iteration_idx) # HACK: If it's not an array, we fall back to just returning the first value. - return first(chain[iteration_idx, Symbol(vn_parent), chain_idx]) + return only(chain[iteration_idx, Symbol(vn_parent), chain_idx]) end function values_from_chain( x::AbstractArray, vn_parent::VarName{sym}, chain, chain_idx, iteration_idx From 585e896be1e5e97b975015a1bdfc646a32e73581 Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Tue, 7 Feb 2023 22:31:53 +0000 Subject: [PATCH 169/218] Update src/model.jl --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index efd0bf4b9..f57ba6df3 100644 --- a/src/model.jl +++ b/src/model.jl @@ -779,7 +779,7 @@ function Distributions.loglikelihood(model::Model, chain::AbstractMCMC.AbstractC var_info = VarInfo(model) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn => values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) + vn_parent => values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) for vn_parent in keys(var_info) ) loglikelihood(model, argvals_dict) From 11bef7ff5c530a43c9d9d5677bf43ebb425f3484 Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Tue, 7 Feb 2023 22:34:52 +0000 Subject: [PATCH 170/218] Update src/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/model.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/model.jl b/src/model.jl index f57ba6df3..759d935dd 100644 --- a/src/model.jl +++ b/src/model.jl @@ -779,8 +779,9 @@ function Distributions.loglikelihood(model::Model, chain::AbstractMCMC.AbstractC var_info = VarInfo(model) # extract variables info from the model map(Iterators.product(1:size(chain, 1), 1:size(chain, 3))) do (iteration_idx, chain_idx) argvals_dict = OrderedDict( - vn_parent => values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) - for vn_parent in keys(var_info) + vn_parent => + values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) for + vn_parent in keys(var_info) ) loglikelihood(model, argvals_dict) end From c6eb9c2bd4d09683097a2922e19d24f83acffa32 Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Tue, 7 Feb 2023 22:36:28 +0000 Subject: [PATCH 171/218] Update src/utils.jl --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 5a6336854..0aefbdaad 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -810,4 +810,4 @@ end function values_from_chain(vi::AbstractVarInfo, vn_parent, chain, chain_idx, iteration_idx) # Use the value `vi[vn_parent]` to obtain a buffer. return values_from_chain(vi[vn_parent], vn_parent, chain, chain_idx, iteration_idx) -end \ No newline at end of file +end From 32499199838717e2b5e71e4a61d7c3b0ebab0a56 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Sun, 12 Feb 2023 16:25:17 +0000 Subject: [PATCH 172/218] rewrote the tests (mainly the way extracting parameter values from chain). --- src/model.jl | 3 +- test/model.jl | 86 ++++++++++++++++++++++++++++++++++-------------- test/runtests.jl | 2 +- 3 files changed, 64 insertions(+), 27 deletions(-) diff --git a/src/model.jl b/src/model.jl index 759d935dd..fad90651d 100644 --- a/src/model.jl +++ b/src/model.jl @@ -689,7 +689,8 @@ function logjoint(model::Model, chain::AbstractMCMC.AbstractChains) values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) for vn_parent in keys(var_info) ) - logjoint(model, argvals_dict) + # logjoint(model, argvals_dict) + loglikelihood(model, argvals_mat_dict) + logprior(model, argvals_mat_dict) end end diff --git a/test/model.jl b/test/model.jl index 5e9d8db27..c02499c7c 100644 --- a/test/model.jl +++ b/test/model.jl @@ -38,30 +38,66 @@ end @test ljoint ≈ lp # logprior, logjoint, loglikelihood for MCMC chains - model = DynamicPPL.TestUtils.DEMO_MODELS[1] - vns = DynamicPPL.TestUtils.varnames(model) - syms = unique(DynamicPPL.getsym, vns) - # generate a chain of sample parameter values. - N = 200 - vals = mapreduce(hcat, 1:N) do _ - samples = rand(OrderedDict, model) - end - chain = Chains(vals', [Symbol(vn) for vn in vns]) - # calculate the pointwise loglikelihoods for the whole chain - logpriors = logprior(model, chain) - loglikelihoods = loglikelihood(model, chain) - logjoints = logjoint(model, chain) - # compare them with true values - for i in 1:N - samples = [ - [chain[i, Symbol(vn), 1] for vn in vns if DynamicPPL.getsym(vn) === sym] for - sym in syms - ] - @test logpriors[i] ≈ DynamicPPL.TestUtils.logprior_true(model, samples...) - @test loglikelihoods[i] ≈ - DynamicPPL.TestUtils.loglikelihood_true(model, samples...) - @test logjoints[i] ≈ DynamicPPL.TestUtils.logjoint_true(model, samples...) - end + for model in DynamicPPL.TestUtils.DEMO_MODELS + vns = DynamicPPL.TestUtils.varnames(model) + syms = unique(DynamicPPL.getsym.(vns)) + # generate a chain of sample parameter values. + N = 200 + vals_OrderedDict = mapreduce(hcat, 1:N) do _ + rand(OrderedDict, model) + end + vals_mat = mapreduce(hcat, 1:N) do i + [vals_OrderedDict[i][vn] for vn in vns] + end + vec_of_vec = [vcat(x...)' for x in eachcol(vals_mat)] + chain_mat = vcat(vec_of_vec...) + # devise parameter names for chain + symbol_names = [] + if size(chain_mat, 2) != length(keys(var_info)) # some parameter names need to be splatted + # examine each vn in vns, and create splatted new variable symbol_names. + for key in keys(vals_OrderedDict[1]) + print(key) + if length(vals_OrderedDict[1][key]) > 1 + # splat the key + spatted_key_names = [Symbol(String(Symbol(key))*"[$kk]") for kk in 1:length(vals_OrderedDict[1][key])] + push!(symbol_names, Symbol.(spatted_key_names)...) + else + push!(symbol_names, Symbol(key)) + end + end + else + symbol_names = keys(var_info) + end + if typeof(model) <: Union{Model{typeof(DynamicPPL.TestUtils.demo_dot_assume_matrix_dot_observe_matrix)}} # some parameter names need to be splatted + symbol_names = [Symbol(String(Symbol(vns[k]))*"[$kk]") for k in 1:length(vns) for kk in 1:size(vals_mat[k,1],1)] + end + chain = Chains(chain_mat, symbol_names) + # count repeatitions of parameter names in keys(chain), for laster use in constructing samples_dict in tests below. + reps = Dict() + for sym in syms + reps[sym] = count(i->contains(String(i), String(sym)), keys(chain)) + end + # calculate the pointwise loglikelihoods for the whole chain + logpriors = logprior(model, chain) + loglikelihoods = loglikelihood(model, chain) + logjoints = logjoint(model, chain) + # compare them with true values + for i in 1:N + # extract parameter values from chain: we need to aggregate the values belonging to the same parameter into a vector. + samples_dict = Dict() + for sym in syms + if reps[sym] > 1 # collect all the values from chain which belong to the same parameter + chain_param_names = [key for key in keys(chain) if contains(String(key), String(sym))] + samples_dict[sym] = [chain[i, chain_param_name, 1] for chain_param_name in chain_param_names] + else + samples_dict[sym] = chain[i, Symbol(sym), 1] + end + end + samples = (; samples_dict...) + @test logpriors[i] ≈ DynamicPPL.TestUtils.logprior_true(model, samples[:s], samples[:m]) + @test loglikelihoods[i] ≈ DynamicPPL.TestUtils.loglikelihood_true(model, samples[:s], samples[:m]) + @test logjoints[i] ≈ DynamicPPL.TestUtils.logjoint_true(model, samples[:s], samples[:m]) + end end @testset "rng" begin @@ -142,7 +178,7 @@ end model = DynamicPPL.TestUtils.demo_dynamic_constraint() vi = VarInfo(model) spl = SampleFromPrior() - link!(vi, spl) + link!!(vi, spl, model) # `link!(varinfo, sampler)` is deprecated, use `link!!(varinfo, sampler, model)` instead. for i in 1:10 # Sample with large variations. diff --git a/test/runtests.jl b/test/runtests.jl index 6b79ce4d7..e6d03779d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -60,7 +60,7 @@ include("test_util.jl") DocMeta.setdocmeta!( DynamicPPL, :DocTestSetup, - :(using DynamicPPL, Distributions, MCMCChains, StableRNGs); + :(using DynamicPPL, AbstractPPL, Distributions, MCMCChains, AbstractMCMC, LinearAlgebra, StableRNGs, Setfield, BangBang); recursive=true, ) doctestfilters = [ From 9f64941231d7caf8e5e1efd844ef9eee446288bc Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Sun, 12 Feb 2023 16:40:40 +0000 Subject: [PATCH 173/218] removed BangBang from doctest setup; fixed imcomplete end in test/model.jl. --- test/model.jl | 1 + test/runtests.jl | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index c02499c7c..18c03d3d1 100644 --- a/test/model.jl +++ b/test/model.jl @@ -98,6 +98,7 @@ end @test loglikelihoods[i] ≈ DynamicPPL.TestUtils.loglikelihood_true(model, samples[:s], samples[:m]) @test logjoints[i] ≈ DynamicPPL.TestUtils.logjoint_true(model, samples[:s], samples[:m]) end + end end @testset "rng" begin diff --git a/test/runtests.jl b/test/runtests.jl index e6d03779d..dd2bc359d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -60,7 +60,7 @@ include("test_util.jl") DocMeta.setdocmeta!( DynamicPPL, :DocTestSetup, - :(using DynamicPPL, AbstractPPL, Distributions, MCMCChains, AbstractMCMC, LinearAlgebra, StableRNGs, Setfield, BangBang); + :(using DynamicPPL, AbstractPPL, Distributions, MCMCChains, AbstractMCMC, LinearAlgebra, StableRNGs, Setfield); recursive=true, ) doctestfilters = [ From 2ea8de8413c6fd6d59b4ffaee26f53ffd0bd3614 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Sun, 12 Feb 2023 16:43:43 +0000 Subject: [PATCH 174/218] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 37 ++++++++++++++++++++++++++++--------- test/runtests.jl | 9 ++++++++- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/test/model.jl b/test/model.jl index 18c03d3d1..cbc6d6eb1 100644 --- a/test/model.jl +++ b/test/model.jl @@ -59,7 +59,10 @@ end print(key) if length(vals_OrderedDict[1][key]) > 1 # splat the key - spatted_key_names = [Symbol(String(Symbol(key))*"[$kk]") for kk in 1:length(vals_OrderedDict[1][key])] + spatted_key_names = [ + Symbol(String(Symbol(key)) * "[$kk]") for + kk in 1:length(vals_OrderedDict[1][key]) + ] push!(symbol_names, Symbol.(spatted_key_names)...) else push!(symbol_names, Symbol(key)) @@ -68,14 +71,21 @@ end else symbol_names = keys(var_info) end - if typeof(model) <: Union{Model{typeof(DynamicPPL.TestUtils.demo_dot_assume_matrix_dot_observe_matrix)}} # some parameter names need to be splatted - symbol_names = [Symbol(String(Symbol(vns[k]))*"[$kk]") for k in 1:length(vns) for kk in 1:size(vals_mat[k,1],1)] + if typeof(model) <: Union{ + Model{ + typeof(DynamicPPL.TestUtils.demo_dot_assume_matrix_dot_observe_matrix) + }, + } # some parameter names need to be splatted + symbol_names = [ + Symbol(String(Symbol(vns[k])) * "[$kk]") for k in 1:length(vns) for + kk in 1:size(vals_mat[k, 1], 1) + ] end chain = Chains(chain_mat, symbol_names) # count repeatitions of parameter names in keys(chain), for laster use in constructing samples_dict in tests below. reps = Dict() for sym in syms - reps[sym] = count(i->contains(String(i), String(sym)), keys(chain)) + reps[sym] = count(i -> contains(String(i), String(sym)), keys(chain)) end # calculate the pointwise loglikelihoods for the whole chain logpriors = logprior(model, chain) @@ -87,16 +97,25 @@ end samples_dict = Dict() for sym in syms if reps[sym] > 1 # collect all the values from chain which belong to the same parameter - chain_param_names = [key for key in keys(chain) if contains(String(key), String(sym))] - samples_dict[sym] = [chain[i, chain_param_name, 1] for chain_param_name in chain_param_names] + chain_param_names = [ + key for key in keys(chain) if contains(String(key), String(sym)) + ] + samples_dict[sym] = [ + chain[i, chain_param_name, 1] for + chain_param_name in chain_param_names + ] else samples_dict[sym] = chain[i, Symbol(sym), 1] end end samples = (; samples_dict...) - @test logpriors[i] ≈ DynamicPPL.TestUtils.logprior_true(model, samples[:s], samples[:m]) - @test loglikelihoods[i] ≈ DynamicPPL.TestUtils.loglikelihood_true(model, samples[:s], samples[:m]) - @test logjoints[i] ≈ DynamicPPL.TestUtils.logjoint_true(model, samples[:s], samples[:m]) + @test logpriors[i] ≈ + DynamicPPL.TestUtils.logprior_true(model, samples[:s], samples[:m]) + @test loglikelihoods[i] ≈ DynamicPPL.TestUtils.loglikelihood_true( + model, samples[:s], samples[:m] + ) + @test logjoints[i] ≈ + DynamicPPL.TestUtils.logjoint_true(model, samples[:s], samples[:m]) end end end diff --git a/test/runtests.jl b/test/runtests.jl index dd2bc359d..3fcc2dcbc 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -60,7 +60,14 @@ include("test_util.jl") DocMeta.setdocmeta!( DynamicPPL, :DocTestSetup, - :(using DynamicPPL, AbstractPPL, Distributions, MCMCChains, AbstractMCMC, LinearAlgebra, StableRNGs, Setfield); + :(using DynamicPPL, + AbstractPPL, + Distributions, + MCMCChains, + AbstractMCMC, + LinearAlgebra, + StableRNGs, + Setfield); recursive=true, ) doctestfilters = [ From 0ad2fc5e3a86e53d460c2d047694b001f7ca767c Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Sun, 12 Feb 2023 16:50:58 +0000 Subject: [PATCH 175/218] fixed a naming bug (argvals_mat_dict) in src/model.jl. --- src/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.jl b/src/model.jl index fad90651d..6de243350 100644 --- a/src/model.jl +++ b/src/model.jl @@ -690,7 +690,7 @@ function logjoint(model::Model, chain::AbstractMCMC.AbstractChains) vn_parent in keys(var_info) ) # logjoint(model, argvals_dict) - loglikelihood(model, argvals_mat_dict) + logprior(model, argvals_mat_dict) + loglikelihood(model, argvals_dict) + logprior(model, argvals_dict) end end From c3c7a6a2fab676dcbffe18a647689281aca5d9bc Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Sun, 12 Feb 2023 17:12:08 +0000 Subject: [PATCH 176/218] fixed a typo - missing `var_info`. --- test/model.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/model.jl b/test/model.jl index cbc6d6eb1..113fc8667 100644 --- a/test/model.jl +++ b/test/model.jl @@ -39,6 +39,7 @@ end # logprior, logjoint, loglikelihood for MCMC chains for model in DynamicPPL.TestUtils.DEMO_MODELS + var_info = VarInfo(model) vns = DynamicPPL.TestUtils.varnames(model) syms = unique(DynamicPPL.getsym.(vns)) # generate a chain of sample parameter values. From 3dbbdae0b1ef7e2700406939f490bf92e29d95a2 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Sun, 12 Feb 2023 17:14:33 +0000 Subject: [PATCH 177/218] Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index 113fc8667..744bb1647 100644 --- a/test/model.jl +++ b/test/model.jl @@ -39,7 +39,7 @@ end # logprior, logjoint, loglikelihood for MCMC chains for model in DynamicPPL.TestUtils.DEMO_MODELS - var_info = VarInfo(model) + var_info = VarInfo(model) vns = DynamicPPL.TestUtils.varnames(model) syms = unique(DynamicPPL.getsym.(vns)) # generate a chain of sample parameter values. From 3c617d8e98815455290cc9b43da29707666ca00b Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 13 Feb 2023 14:49:28 +0000 Subject: [PATCH 178/218] Apply suggestions from code review Co-authored-by: David Widmann --- src/model.jl | 3 +-- test/model.jl | 29 +++++++++++------------------ test/runtests.jl | 12 +----------- 3 files changed, 13 insertions(+), 31 deletions(-) diff --git a/src/model.jl b/src/model.jl index 6de243350..759d935dd 100644 --- a/src/model.jl +++ b/src/model.jl @@ -689,8 +689,7 @@ function logjoint(model::Model, chain::AbstractMCMC.AbstractChains) values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) for vn_parent in keys(var_info) ) - # logjoint(model, argvals_dict) - loglikelihood(model, argvals_dict) + logprior(model, argvals_dict) + logjoint(model, argvals_dict) end end diff --git a/test/model.jl b/test/model.jl index 744bb1647..80137a5e7 100644 --- a/test/model.jl +++ b/test/model.jl @@ -53,18 +53,14 @@ end vec_of_vec = [vcat(x...)' for x in eachcol(vals_mat)] chain_mat = vcat(vec_of_vec...) # devise parameter names for chain - symbol_names = [] + symbol_names = Symbol[] if size(chain_mat, 2) != length(keys(var_info)) # some parameter names need to be splatted # examine each vn in vns, and create splatted new variable symbol_names. - for key in keys(vals_OrderedDict[1]) - print(key) - if length(vals_OrderedDict[1][key]) > 1 - # splat the key - spatted_key_names = [ - Symbol(String(Symbol(key)) * "[$kk]") for - kk in 1:length(vals_OrderedDict[1][key]) - ] - push!(symbol_names, Symbol.(spatted_key_names)...) + for (key, val) in vals_OrderedDict[1] + if length(val) > 1 + for kk in 1:length(val) + push!(symbol_names, Symbol(key, "[", kk, "]")) + end else push!(symbol_names, Symbol(key)) end @@ -72,14 +68,11 @@ end else symbol_names = keys(var_info) end - if typeof(model) <: Union{ - Model{ - typeof(DynamicPPL.TestUtils.demo_dot_assume_matrix_dot_observe_matrix) - }, - } # some parameter names need to be splatted + if model isa Model{ + typeof(DynamicPPL.TestUtils.demo_dot_assume_matrix_dot_observe_matrix) + } symbol_names = [ - Symbol(String(Symbol(vns[k])) * "[$kk]") for k in 1:length(vns) for - kk in 1:size(vals_mat[k, 1], 1) + Symbol(vns[k], "[", kk, "]) for k in 1:length(vns) for kk in 1:size(vals_mat[k, 1], 1) ] end chain = Chains(chain_mat, symbol_names) @@ -199,7 +192,7 @@ end model = DynamicPPL.TestUtils.demo_dynamic_constraint() vi = VarInfo(model) spl = SampleFromPrior() - link!!(vi, spl, model) # `link!(varinfo, sampler)` is deprecated, use `link!!(varinfo, sampler, model)` instead. + link!!(vi, spl, model) for i in 1:10 # Sample with large variations. diff --git a/test/runtests.jl b/test/runtests.jl index 3fcc2dcbc..27889b5e5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -58,17 +58,7 @@ include("test_util.jl") @testset "doctests" begin DocMeta.setdocmeta!( - DynamicPPL, - :DocTestSetup, - :(using DynamicPPL, - AbstractPPL, - Distributions, - MCMCChains, - AbstractMCMC, - LinearAlgebra, - StableRNGs, - Setfield); - recursive=true, + DynamicPPL, :DocTestSetup, :(using DynamicPPL); recursive=true ) doctestfilters = [ # Older versions will show "0 element Array" instead of "Type[]". From bf8621811d9ffebfba912182f31d8fdfe404e7d3 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 13 Feb 2023 16:30:53 +0000 Subject: [PATCH 179/218] Apply suggestions from code review Co-authored-by: David Widmann --- test/model.jl | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/model.jl b/test/model.jl index 80137a5e7..aa19dad7b 100644 --- a/test/model.jl +++ b/test/model.jl @@ -77,10 +77,7 @@ end end chain = Chains(chain_mat, symbol_names) # count repeatitions of parameter names in keys(chain), for laster use in constructing samples_dict in tests below. - reps = Dict() - for sym in syms - reps[sym] = count(i -> contains(String(i), String(sym)), keys(chain)) - end + reps = Dict(sym => count(i -> contains(String(i), String(sym)), keys(chain)) for sym in syms) # calculate the pointwise loglikelihoods for the whole chain logpriors = logprior(model, chain) loglikelihoods = loglikelihood(model, chain) From a150215a8f3fea1ff2ef17875e4b8ff227fe3138 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Tue, 14 Feb 2023 19:53:31 +0000 Subject: [PATCH 180/218] Explicitly added `using Distributions` in doctests; Accepted suggestion in test/model.jl, tests passed locally. --- Project.toml | 5 +++++ src/model.jl | 6 +++--- test/model.jl | 8 ++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Project.toml b/Project.toml index 562180c08..506edaa6a 100644 --- a/Project.toml +++ b/Project.toml @@ -10,7 +10,10 @@ Bijectors = "76274a88-744f-5084-9051-94815aaf08c4" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" +DistributionsAD = "ced4e74d-a319-5a8a-b0ac-84af2272839c" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" +Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" +ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c" MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" @@ -18,6 +21,8 @@ OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c" +Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444" [compat] diff --git a/src/model.jl b/src/model.jl index 759d935dd..2dfde7c53 100644 --- a/src/model.jl +++ b/src/model.jl @@ -665,7 +665,7 @@ Return an array of log joint probabilities evaluated at each sample in an MCMC ` # Examples ```jldoctest -julia> using MCMCChains +julia> using MCMCChains, Distributions julia> @model function demo_model(x) s ~ InverseGamma(2, 3) @@ -712,7 +712,7 @@ Return an array of log prior probabilities evaluated at each sample in an MCMC ` # Examples ```jldoctest -julia> using MCMCChains +julia> using MCMCChains, Distributions julia> @model function demo_model(x) s ~ InverseGamma(2, 3) @@ -759,7 +759,7 @@ Return an array of log likelihoods evaluated at each sample in an MCMC `chain`. # Examples ```jldoctest -julia> using MCMCChains +julia> using MCMCChains, Distributions julia> @model function demo_model(x) s ~ InverseGamma(2, 3) diff --git a/test/model.jl b/test/model.jl index aa19dad7b..ad27faf97 100644 --- a/test/model.jl +++ b/test/model.jl @@ -72,7 +72,7 @@ end typeof(DynamicPPL.TestUtils.demo_dot_assume_matrix_dot_observe_matrix) } symbol_names = [ - Symbol(vns[k], "[", kk, "]) for k in 1:length(vns) for kk in 1:size(vals_mat[k, 1], 1) + Symbol(vns[k], "[", kk, "]") for k in 1:length(vns) for kk in 1:size(vals_mat[k, 1], 1) ] end chain = Chains(chain_mat, symbol_names) @@ -101,12 +101,12 @@ end end samples = (; samples_dict...) @test logpriors[i] ≈ - DynamicPPL.TestUtils.logprior_true(model, samples[:s], samples[:m]) + DynamicPPL.TestUtils.logprior_true(model, [samples[sym] for sym in syms]...) @test loglikelihoods[i] ≈ DynamicPPL.TestUtils.loglikelihood_true( - model, samples[:s], samples[:m] + model, [samples[sym] for sym in syms]... ) @test logjoints[i] ≈ - DynamicPPL.TestUtils.logjoint_true(model, samples[:s], samples[:m]) + DynamicPPL.TestUtils.logjoint_true(model, [samples[sym] for sym in syms]...) end end end From 0bbf948b44e7285206a03f6a76f9968dad5d27cc Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 14 Feb 2023 19:59:37 +0000 Subject: [PATCH 181/218] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/test/model.jl b/test/model.jl index ad27faf97..2252b8825 100644 --- a/test/model.jl +++ b/test/model.jl @@ -68,16 +68,19 @@ end else symbol_names = keys(var_info) end - if model isa Model{ - typeof(DynamicPPL.TestUtils.demo_dot_assume_matrix_dot_observe_matrix) - } + if model isa + Model{typeof(DynamicPPL.TestUtils.demo_dot_assume_matrix_dot_observe_matrix)} symbol_names = [ - Symbol(vns[k], "[", kk, "]") for k in 1:length(vns) for kk in 1:size(vals_mat[k, 1], 1) + Symbol(vns[k], "[", kk, "]") for k in 1:length(vns) for + kk in 1:size(vals_mat[k, 1], 1) ] end chain = Chains(chain_mat, symbol_names) # count repeatitions of parameter names in keys(chain), for laster use in constructing samples_dict in tests below. - reps = Dict(sym => count(i -> contains(String(i), String(sym)), keys(chain)) for sym in syms) + reps = Dict( + sym => count(i -> contains(String(i), String(sym)), keys(chain)) for + sym in syms + ) # calculate the pointwise loglikelihoods for the whole chain logpriors = logprior(model, chain) loglikelihoods = loglikelihood(model, chain) @@ -100,13 +103,15 @@ end end end samples = (; samples_dict...) - @test logpriors[i] ≈ - DynamicPPL.TestUtils.logprior_true(model, [samples[sym] for sym in syms]...) + @test logpriors[i] ≈ DynamicPPL.TestUtils.logprior_true( + model, [samples[sym] for sym in syms]... + ) @test loglikelihoods[i] ≈ DynamicPPL.TestUtils.loglikelihood_true( model, [samples[sym] for sym in syms]... ) - @test logjoints[i] ≈ - DynamicPPL.TestUtils.logjoint_true(model, [samples[sym] for sym in syms]...) + @test logjoints[i] ≈ DynamicPPL.TestUtils.logjoint_true( + model, [samples[sym] for sym in syms]... + ) end end end From 7b7f13cc9cbd6738160c88d8e0f5904a25ab3262 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 24 Feb 2023 21:24:13 +0000 Subject: [PATCH 182/218] rm unnecessary deps --- Project.toml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Project.toml b/Project.toml index 8b45a3619..0b8dc927b 100644 --- a/Project.toml +++ b/Project.toml @@ -10,10 +10,7 @@ Bijectors = "76274a88-744f-5084-9051-94815aaf08c4" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" -DistributionsAD = "ced4e74d-a319-5a8a-b0ac-84af2272839c" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" -Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" -ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c" MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" @@ -21,8 +18,6 @@ OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c" -Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444" [compat] From b6a609738afd5b3bc246bb6ec0c05fb47e2ee625 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 24 Feb 2023 22:31:00 +0000 Subject: [PATCH 183/218] replace contains with subsumes. --- test/model.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/model.jl b/test/model.jl index 2252b8825..4e000b4a8 100644 --- a/test/model.jl +++ b/test/model.jl @@ -78,7 +78,7 @@ end chain = Chains(chain_mat, symbol_names) # count repeatitions of parameter names in keys(chain), for laster use in constructing samples_dict in tests below. reps = Dict( - sym => count(i -> contains(String(i), String(sym)), keys(chain)) for + sym => count(i -> subsumes(@varname(sym), @varname(i)), keys(chain)) for sym in syms ) # calculate the pointwise loglikelihoods for the whole chain @@ -92,7 +92,7 @@ end for sym in syms if reps[sym] > 1 # collect all the values from chain which belong to the same parameter chain_param_names = [ - key for key in keys(chain) if contains(String(key), String(sym)) + key for key in keys(chain) if subsumes(@varname(sym), @varname(key)) ] samples_dict[sym] = [ chain[i, chain_param_name, 1] for From 276c76c3f4ec0294fae843fc0ac7a854aaa181ef Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 24 Feb 2023 22:32:27 +0000 Subject: [PATCH 184/218] rm redundant deps in docs build script. --- docs/make.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/make.jl b/docs/make.jl index 3e846e367..ebe2b2472 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -6,7 +6,7 @@ using DynamicPPL: AbstractPPL DocMeta.setdocmeta!( DynamicPPL, :DocTestSetup, - :(using DynamicPPL, Distributions, StableRNGs, MCMCChains); + :(using DynamicPPL); recursive=true, ) From c7717b06ea14de9b6e4a1411b3a1e03323581600 Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Fri, 24 Feb 2023 22:33:07 +0000 Subject: [PATCH 185/218] Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index 4e000b4a8..d83f5c8f1 100644 --- a/test/model.jl +++ b/test/model.jl @@ -92,7 +92,8 @@ end for sym in syms if reps[sym] > 1 # collect all the values from chain which belong to the same parameter chain_param_names = [ - key for key in keys(chain) if subsumes(@varname(sym), @varname(key)) + key for + key in keys(chain) if subsumes(@varname(sym), @varname(key)) ] samples_dict[sym] = [ chain[i, chain_param_name, 1] for From 23e2ab13e05c53436ba17edf524bc016a5841a0b Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 24 Feb 2023 22:37:16 +0000 Subject: [PATCH 186/218] Fix format. --- docs/make.jl | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/docs/make.jl b/docs/make.jl index ebe2b2472..83bfdd624 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -3,12 +3,7 @@ using DynamicPPL using DynamicPPL: AbstractPPL # Doctest setup -DocMeta.setdocmeta!( - DynamicPPL, - :DocTestSetup, - :(using DynamicPPL); - recursive=true, -) +DocMeta.setdocmeta!(DynamicPPL, :DocTestSetup, :(using DynamicPPL); recursive=true) makedocs(; sitename="DynamicPPL", From 84e01b947524d4b73e52ba8402e2142d8c371479 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Mon, 3 Apr 2023 10:56:57 +0100 Subject: [PATCH 187/218] Replaced `subsumes` by `contains`. --- test/model.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/model.jl b/test/model.jl index d83f5c8f1..22b4d44c5 100644 --- a/test/model.jl +++ b/test/model.jl @@ -78,7 +78,7 @@ end chain = Chains(chain_mat, symbol_names) # count repeatitions of parameter names in keys(chain), for laster use in constructing samples_dict in tests below. reps = Dict( - sym => count(i -> subsumes(@varname(sym), @varname(i)), keys(chain)) for + sym => count(i -> contains(String(i), String(sym)), keys(chain)) for sym in syms ) # calculate the pointwise loglikelihoods for the whole chain @@ -93,7 +93,7 @@ end if reps[sym] > 1 # collect all the values from chain which belong to the same parameter chain_param_names = [ key for - key in keys(chain) if subsumes(@varname(sym), @varname(key)) + key in keys(chain) if contains(String(key), String(sym)) ] samples_dict[sym] = [ chain[i, chain_param_name, 1] for From 1dd81f9fef2a46e97c57ff69a5c2ea9a0eae6ebd Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Mon, 3 Apr 2023 11:03:49 +0100 Subject: [PATCH 188/218] Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/model.jl b/test/model.jl index 22b4d44c5..2252b8825 100644 --- a/test/model.jl +++ b/test/model.jl @@ -92,8 +92,7 @@ end for sym in syms if reps[sym] > 1 # collect all the values from chain which belong to the same parameter chain_param_names = [ - key for - key in keys(chain) if contains(String(key), String(sym)) + key for key in keys(chain) if contains(String(key), String(sym)) ] samples_dict[sym] = [ chain[i, chain_param_name, 1] for From 9b379279c04d0825a28ca360be2c4954dc4b7b1e Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Wed, 12 Apr 2023 14:17:30 +0100 Subject: [PATCH 189/218] replaced 'contains' by a new, temporary method 'subsumes_sym', just for testing purpose. --- test/model.jl | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test/model.jl b/test/model.jl index 2252b8825..3b8919327 100644 --- a/test/model.jl +++ b/test/model.jl @@ -77,8 +77,21 @@ end end chain = Chains(chain_mat, symbol_names) # count repeatitions of parameter names in keys(chain), for laster use in constructing samples_dict in tests below. + # a dispatched `subsumes(parent::Symbol, child::Symbol)` method has been created in PR#83 for AbstractPPL.jl; here we temporary replicate it here - just for tesing purpose. + # This 'subsumes_sym(parent::Symbol, child::Symbol)' method can be removed from here, once PR#83 in AbstractPPL has been merged. + function subsumes_sym(parent::Symbol, child::Symbol) + parent_str = string(parent) + child_str = string(child) + if parent_str == child_str + return true + end + if length(parent_str) > length(child_str) + return false + end + return child_str[1:length(parent_str)] == parent_str + end reps = Dict( - sym => count(i -> contains(String(i), String(sym)), keys(chain)) for + sym => count(i->subsumes_sym(Symbol(sym), Symbol(i)), keys(chain)) for sym in syms ) # calculate the pointwise loglikelihoods for the whole chain @@ -92,7 +105,7 @@ end for sym in syms if reps[sym] > 1 # collect all the values from chain which belong to the same parameter chain_param_names = [ - key for key in keys(chain) if contains(String(key), String(sym)) + key for key in keys(chain) if subsumes_sym(Symbol(sym), Symbol(key)) ] samples_dict[sym] = [ chain[i, chain_param_name, 1] for From da0f39cf9797ac956817f958bf4c83c82863fee8 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 12 Apr 2023 14:20:14 +0100 Subject: [PATCH 190/218] Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index 3b8919327..d584211f8 100644 --- a/test/model.jl +++ b/test/model.jl @@ -91,7 +91,7 @@ end return child_str[1:length(parent_str)] == parent_str end reps = Dict( - sym => count(i->subsumes_sym(Symbol(sym), Symbol(i)), keys(chain)) for + sym => count(i -> subsumes_sym(Symbol(sym), Symbol(i)), keys(chain)) for sym in syms ) # calculate the pointwise loglikelihoods for the whole chain From 01804266b0b1e255d974bdca86c13333042bbc40 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Wed, 12 Apr 2023 14:20:26 +0100 Subject: [PATCH 191/218] Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index d584211f8..82779e086 100644 --- a/test/model.jl +++ b/test/model.jl @@ -105,7 +105,8 @@ end for sym in syms if reps[sym] > 1 # collect all the values from chain which belong to the same parameter chain_param_names = [ - key for key in keys(chain) if subsumes_sym(Symbol(sym), Symbol(key)) + key for + key in keys(chain) if subsumes_sym(Symbol(sym), Symbol(key)) ] samples_dict[sym] = [ chain[i, chain_param_name, 1] for From e01542981544cec07d4cadcccf49619710e030e1 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Thu, 22 Jun 2023 12:17:22 +0100 Subject: [PATCH 192/218] modified `/test/model.jl`: 1. build a map between model parameter symbol (`s`,`m`) and chain parameter names (which is obtained via `varname_leaves`.) 2. use this naming map to collect sample values from chain and drop into `log_true` for validation. --- test/model.jl | 99 +++++++++++++++++++-------------------------------- 1 file changed, 36 insertions(+), 63 deletions(-) diff --git a/test/model.jl b/test/model.jl index 82779e086..4950501bc 100644 --- a/test/model.jl +++ b/test/model.jl @@ -37,7 +37,20 @@ end @test ljoint ≈ lprior + llikelihood @test ljoint ≈ lp - # logprior, logjoint, loglikelihood for MCMC chains + #### logprior, logjoint, loglikelihood for MCMC chains #### + # Function to modify the representation of values based on their length + function modify_value_representation(nt::NamedTuple) + modified_nt = NamedTuple() + for (key, value) in zip(keys(samples), values(samples)) + if length(value) == 1 # Scalar value + modified_value = value[1] + else # Non-scalar value + modified_value = value + end + modified_nt = merge(modified_nt, (key => modified_value,)) + end + return modified_nt + end for model in DynamicPPL.TestUtils.DEMO_MODELS var_info = VarInfo(model) vns = DynamicPPL.TestUtils.varnames(model) @@ -53,79 +66,39 @@ end vec_of_vec = [vcat(x...)' for x in eachcol(vals_mat)] chain_mat = vcat(vec_of_vec...) # devise parameter names for chain - symbol_names = Symbol[] - if size(chain_mat, 2) != length(keys(var_info)) # some parameter names need to be splatted - # examine each vn in vns, and create splatted new variable symbol_names. - for (key, val) in vals_OrderedDict[1] - if length(val) > 1 - for kk in 1:length(val) - push!(symbol_names, Symbol(key, "[", kk, "]")) - end - else - push!(symbol_names, Symbol(key)) - end + sample_values_vec = collect(values(vals_OrderedDict[1])) + symbol_names = [] + chain_sym_map = Dict() + for k in 1:length(keys(var_info)) + vn_parent = keys(var_info)[k] + sym = DynamicPPL.getsym(vn_parent) + vn_children = DynamicPPL.varname_leaves(vn_parent, sample_values_vec[k]) + for vn_child in vn_children + chain_sym_map[Symbol(vn_child)] = sym + symbol_names = [symbol_names; Symbol(vn_child)] end - else - symbol_names = keys(var_info) - end - if model isa - Model{typeof(DynamicPPL.TestUtils.demo_dot_assume_matrix_dot_observe_matrix)} - symbol_names = [ - Symbol(vns[k], "[", kk, "]") for k in 1:length(vns) for - kk in 1:size(vals_mat[k, 1], 1) - ] end chain = Chains(chain_mat, symbol_names) - # count repeatitions of parameter names in keys(chain), for laster use in constructing samples_dict in tests below. - # a dispatched `subsumes(parent::Symbol, child::Symbol)` method has been created in PR#83 for AbstractPPL.jl; here we temporary replicate it here - just for tesing purpose. - # This 'subsumes_sym(parent::Symbol, child::Symbol)' method can be removed from here, once PR#83 in AbstractPPL has been merged. - function subsumes_sym(parent::Symbol, child::Symbol) - parent_str = string(parent) - child_str = string(child) - if parent_str == child_str - return true - end - if length(parent_str) > length(child_str) - return false - end - return child_str[1:length(parent_str)] == parent_str - end - reps = Dict( - sym => count(i -> subsumes_sym(Symbol(sym), Symbol(i)), keys(chain)) for - sym in syms - ) - # calculate the pointwise loglikelihoods for the whole chain + # calculate the pointwise loglikelihoods for the whole chain using the newly written functions logpriors = logprior(model, chain) loglikelihoods = loglikelihood(model, chain) logjoints = logjoint(model, chain) # compare them with true values for i in 1:N - # extract parameter values from chain: we need to aggregate the values belonging to the same parameter into a vector. samples_dict = Dict() - for sym in syms - if reps[sym] > 1 # collect all the values from chain which belong to the same parameter - chain_param_names = [ - key for - key in keys(chain) if subsumes_sym(Symbol(sym), Symbol(key)) - ] - samples_dict[sym] = [ - chain[i, chain_param_name, 1] for - chain_param_name in chain_param_names - ] - else - samples_dict[sym] = chain[i, Symbol(sym), 1] - end + for chain_key in keys(chain) + value = chain[i, chain_key, 1] + key = chain_sym_map[chain_key] + existing_value = get(samples_dict, key, Float64[]) + push!(existing_value, value) + samples_dict[key] = existing_value end samples = (; samples_dict...) - @test logpriors[i] ≈ DynamicPPL.TestUtils.logprior_true( - model, [samples[sym] for sym in syms]... - ) - @test loglikelihoods[i] ≈ DynamicPPL.TestUtils.loglikelihood_true( - model, [samples[sym] for sym in syms]... - ) - @test logjoints[i] ≈ DynamicPPL.TestUtils.logjoint_true( - model, [samples[sym] for sym in syms]... - ) + samples = modify_value_representation(samples) + @test logpriors[i] ≈ DynamicPPL.TestUtils.logprior_true(model, samples[:s], samples[:m]) + @test loglikelihoods[i] ≈ + DynamicPPL.TestUtils.loglikelihood_true(model, samples[:s], samples[:m]) + @test logjoints[i] ≈ DynamicPPL.TestUtils.logjoint_true(model, samples[:s], samples[:m]) end end end From b5223585060886eb7135fafe9d1d66660e226b45 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 22 Jun 2023 12:21:29 +0100 Subject: [PATCH 193/218] Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/model.jl b/test/model.jl index 4950501bc..b2fb62a69 100644 --- a/test/model.jl +++ b/test/model.jl @@ -95,10 +95,13 @@ end end samples = (; samples_dict...) samples = modify_value_representation(samples) - @test logpriors[i] ≈ DynamicPPL.TestUtils.logprior_true(model, samples[:s], samples[:m]) - @test loglikelihoods[i] ≈ - DynamicPPL.TestUtils.loglikelihood_true(model, samples[:s], samples[:m]) - @test logjoints[i] ≈ DynamicPPL.TestUtils.logjoint_true(model, samples[:s], samples[:m]) + @test logpriors[i] ≈ + DynamicPPL.TestUtils.logprior_true(model, samples[:s], samples[:m]) + @test loglikelihoods[i] ≈ DynamicPPL.TestUtils.loglikelihood_true( + model, samples[:s], samples[:m] + ) + @test logjoints[i] ≈ + DynamicPPL.TestUtils.logjoint_true(model, samples[:s], samples[:m]) end end end From 32ee32f0887adcd24082708410800dbc64459084 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Thu, 22 Jun 2023 13:40:19 +0100 Subject: [PATCH 194/218] Fixed a mistake in `modify_value_representation`. --- test/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index b2fb62a69..6cf0aa1f2 100644 --- a/test/model.jl +++ b/test/model.jl @@ -41,7 +41,7 @@ end # Function to modify the representation of values based on their length function modify_value_representation(nt::NamedTuple) modified_nt = NamedTuple() - for (key, value) in zip(keys(samples), values(samples)) + for (key, value) in zip(keys(nt), values(nt)) if length(value) == 1 # Scalar value modified_value = value[1] else # Non-scalar value From c29554b9f38069fe7d6ced30de1cd6bcd89f7af6 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Thu, 22 Jun 2023 14:04:17 +0100 Subject: [PATCH 195/218] fixed `gdemo_default` --- test/model.jl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index 6cf0aa1f2..4fed23a69 100644 --- a/test/model.jl +++ b/test/model.jl @@ -14,7 +14,14 @@ end @testset "model.jl" begin @testset "convenience functions" begin - model = gdemo_default + @model function gdemo_d() + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + 1.5 ~ Normal(m, sqrt(s)) + 2.0 ~ Normal(m, sqrt(s)) + return s, m + end + gdemo_default = gdemo_d() # sample from model and extract variables vi = VarInfo(model) From c6edb500e6b41f2adde13b2848019dfa27012fee Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Thu, 22 Jun 2023 14:24:47 +0100 Subject: [PATCH 196/218] assigned `model=gdemo_default`. --- test/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index 4fed23a69..aeb753ecc 100644 --- a/test/model.jl +++ b/test/model.jl @@ -21,7 +21,7 @@ end 2.0 ~ Normal(m, sqrt(s)) return s, m end - gdemo_default = gdemo_d() + model = gdemo_d() # sample from model and extract variables vi = VarInfo(model) From 02b9a437ae8fb143669134e236ee704799fb0ca1 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Thu, 22 Jun 2023 16:27:54 +0100 Subject: [PATCH 197/218] src/model.jl: added `DynamicPPL.` to `logprior` and `logjoint`. --- src/model.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/model.jl b/src/model.jl index 2dfde7c53..38b85c818 100644 --- a/src/model.jl +++ b/src/model.jl @@ -689,7 +689,7 @@ function logjoint(model::Model, chain::AbstractMCMC.AbstractChains) values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) for vn_parent in keys(var_info) ) - logjoint(model, argvals_dict) + DynamicPPL.logjoint(model, argvals_dict) end end @@ -736,7 +736,7 @@ function logprior(model::Model, chain::AbstractMCMC.AbstractChains) values_from_chain(var_info, vn_parent, chain, chain_idx, iteration_idx) for vn_parent in keys(var_info) ) - logprior(model, argvals_dict) + DynamicPPL.logprior(model, argvals_dict) end end From 4e1f424519478ceaef3746be3ab737d432c7bdc2 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Thu, 22 Jun 2023 17:07:05 +0100 Subject: [PATCH 198/218] commented out `gdemo_d()` as a trial test. --- test/model.jl | 58 +++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/test/model.jl b/test/model.jl index aeb753ecc..f9579caa8 100644 --- a/test/model.jl +++ b/test/model.jl @@ -14,35 +14,35 @@ end @testset "model.jl" begin @testset "convenience functions" begin - @model function gdemo_d() - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - 1.5 ~ Normal(m, sqrt(s)) - 2.0 ~ Normal(m, sqrt(s)) - return s, m - end - model = gdemo_d() - - # sample from model and extract variables - vi = VarInfo(model) - s = vi[@varname(s)] - m = vi[@varname(m)] - - # extract log pdf of variable object - lp = getlogp(vi) - - # log prior probability - lprior = logprior(model, vi) - @test lprior ≈ logpdf(InverseGamma(2, 3), s) + logpdf(Normal(0, sqrt(s)), m) - - # log likelihood - llikelihood = loglikelihood(model, vi) - @test llikelihood ≈ loglikelihood(Normal(m, sqrt(s)), [1.5, 2.0]) - - # log joint probability - ljoint = logjoint(model, vi) - @test ljoint ≈ lprior + llikelihood - @test ljoint ≈ lp + # @model function gdemo_d() + # s ~ InverseGamma(2, 3) + # m ~ Normal(0, sqrt(s)) + # 1.5 ~ Normal(m, sqrt(s)) + # 2.0 ~ Normal(m, sqrt(s)) + # return s, m + # end + # model = gdemo_d() + + # # sample from model and extract variables + # vi = VarInfo(model) + # s = vi[@varname(s)] + # m = vi[@varname(m)] + + # # extract log pdf of variable object + # lp = getlogp(vi) + + # # log prior probability + # lprior = logprior(model, vi) + # @test lprior ≈ logpdf(InverseGamma(2, 3), s) + logpdf(Normal(0, sqrt(s)), m) + + # # log likelihood + # llikelihood = loglikelihood(model, vi) + # @test llikelihood ≈ loglikelihood(Normal(m, sqrt(s)), [1.5, 2.0]) + + # # log joint probability + # ljoint = logjoint(model, vi) + # @test ljoint ≈ lprior + llikelihood + # @test ljoint ≈ lp #### logprior, logjoint, loglikelihood for MCMC chains #### # Function to modify the representation of values based on their length From 9a0404b1339aa3698bfd654ff4a142ab6c9caf62 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Fri, 23 Jun 2023 15:14:41 +0100 Subject: [PATCH 199/218] used `Symbol(vn_child)` as keys in `chain_sym_map`. --- test/model.jl | 95 +++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 45 deletions(-) diff --git a/test/model.jl b/test/model.jl index f9579caa8..d6c2f0803 100644 --- a/test/model.jl +++ b/test/model.jl @@ -11,57 +11,59 @@ struct MyZeroModel end m ~ Normal(0, 1) return x ~ Normal(m, 1) end +@model function gdemo_d() + s ~ InverseGamma(2, 3) + m ~ Normal(0, sqrt(s)) + 1.5 ~ Normal(m, sqrt(s)) + 2.0 ~ Normal(m, sqrt(s)) + return s, m +end +gdemo_default = gdemo_d() +# function to modify the representation of values based on their length +function modify_value_representation(nt::NamedTuple) + modified_nt = NamedTuple() + for (key, value) in zip(keys(nt), values(nt)) + if length(value) == 1 # Scalar value + modified_value = value[1] + else # Non-scalar value + modified_value = value + end + modified_nt = merge(modified_nt, (key => modified_value,)) + end + return modified_nt +end @testset "model.jl" begin @testset "convenience functions" begin - # @model function gdemo_d() - # s ~ InverseGamma(2, 3) - # m ~ Normal(0, sqrt(s)) - # 1.5 ~ Normal(m, sqrt(s)) - # 2.0 ~ Normal(m, sqrt(s)) - # return s, m - # end - # model = gdemo_d() - - # # sample from model and extract variables - # vi = VarInfo(model) - # s = vi[@varname(s)] - # m = vi[@varname(m)] - - # # extract log pdf of variable object - # lp = getlogp(vi) - - # # log prior probability - # lprior = logprior(model, vi) - # @test lprior ≈ logpdf(InverseGamma(2, 3), s) + logpdf(Normal(0, sqrt(s)), m) - - # # log likelihood - # llikelihood = loglikelihood(model, vi) - # @test llikelihood ≈ loglikelihood(Normal(m, sqrt(s)), [1.5, 2.0]) - - # # log joint probability - # ljoint = logjoint(model, vi) - # @test ljoint ≈ lprior + llikelihood - # @test ljoint ≈ lp + model = gdemo_default + + # sample from model and extract variables + vi = VarInfo(model) + s = vi[@varname(s)] + m = vi[@varname(m)] + + # extract log pdf of variable object + lp = getlogp(vi) + + # log prior probability + lprior = logprior(model, vi) + @test lprior ≈ logpdf(InverseGamma(2, 3), s) + logpdf(Normal(0, sqrt(s)), m) + + # log likelihood + llikelihood = loglikelihood(model, vi) + @test llikelihood ≈ loglikelihood(Normal(m, sqrt(s)), [1.5, 2.0]) + + # log joint probability + ljoint = logjoint(model, vi) + @test ljoint ≈ lprior + llikelihood + @test ljoint ≈ lp #### logprior, logjoint, loglikelihood for MCMC chains #### - # Function to modify the representation of values based on their length - function modify_value_representation(nt::NamedTuple) - modified_nt = NamedTuple() - for (key, value) in zip(keys(nt), values(nt)) - if length(value) == 1 # Scalar value - modified_value = value[1] - else # Non-scalar value - modified_value = value - end - modified_nt = merge(modified_nt, (key => modified_value,)) - end - return modified_nt - end for model in DynamicPPL.TestUtils.DEMO_MODELS var_info = VarInfo(model) vns = DynamicPPL.TestUtils.varnames(model) syms = unique(DynamicPPL.getsym.(vns)) + # generate a chain of sample parameter values. N = 200 vals_OrderedDict = mapreduce(hcat, 1:N) do _ @@ -72,6 +74,7 @@ end end vec_of_vec = [vcat(x...)' for x in eachcol(vals_mat)] chain_mat = vcat(vec_of_vec...) + # devise parameter names for chain sample_values_vec = collect(values(vals_OrderedDict[1])) symbol_names = [] @@ -79,17 +82,19 @@ end for k in 1:length(keys(var_info)) vn_parent = keys(var_info)[k] sym = DynamicPPL.getsym(vn_parent) - vn_children = DynamicPPL.varname_leaves(vn_parent, sample_values_vec[k]) + vn_children = varname_leaves(vn_parent, sample_values_vec[k]) for vn_child in vn_children chain_sym_map[Symbol(vn_child)] = sym symbol_names = [symbol_names; Symbol(vn_child)] end end chain = Chains(chain_mat, symbol_names) + # calculate the pointwise loglikelihoods for the whole chain using the newly written functions - logpriors = logprior(model, chain) + logpriors = logprior_temp(model, chain) loglikelihoods = loglikelihood(model, chain) - logjoints = logjoint(model, chain) + logjoints = logjoint_temp(model, chain) + # compare them with true values for i in 1:N samples_dict = Dict() From a8af236de8653be31037e71b32c8c79d2c7b2d8f Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 23 Jun 2023 15:17:39 +0100 Subject: [PATCH 200/218] Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index d6c2f0803..1891556e0 100644 --- a/test/model.jl +++ b/test/model.jl @@ -94,7 +94,6 @@ end logpriors = logprior_temp(model, chain) loglikelihoods = loglikelihood(model, chain) logjoints = logjoint_temp(model, chain) - # compare them with true values for i in 1:N samples_dict = Dict() From c57f276952f19697ff734754ba392f08f6aa794c Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Fri, 23 Jun 2023 15:46:37 +0100 Subject: [PATCH 201/218] explicitly loaded `varname_leaves` and `values_from_chain`. --- test/model.jl | 52 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/test/model.jl b/test/model.jl index 415278ddf..03a707b92 100644 --- a/test/model.jl +++ b/test/model.jl @@ -1,3 +1,51 @@ +# helper functions for testing (both varname_leaves and values_from_chain are in utils.jl, for some reason they are not loaded) +varname_leaves(vn::VarName, ::Real) = [vn] +function varname_leaves(vn::VarName, val::AbstractArray{<:Union{Real,Missing}}) + return ( + VarName(vn, getlens(vn) ∘ Setfield.IndexLens(Tuple(I))) for + I in CartesianIndices(val) + ) +end +function varname_leaves(vn::VarName, val::AbstractArray) + return Iterators.flatten( + varname_leaves(VarName(vn, getlens(vn) ∘ Setfield.IndexLens(Tuple(I))), val[I]) for + I in CartesianIndices(val) + ) +end +function varname_leaves(vn::DynamicPPL.VarName, val::NamedTuple) + iter = Iterators.map(keys(val)) do sym + lens = Setfield.PropertyLens{sym}() + varname_leaves(vn ∘ lens, get(val, lens)) + end + return Iterators.flatten(iter) +end + +#481 +function values_from_chain(x, vn_parent, chain, chain_idx, iteration_idx) + # HACK: If it's not an array, we fall back to just returning the first value. + return only(chain[iteration_idx, Symbol(vn_parent), chain_idx]) +end +function values_from_chain( + x::AbstractArray, vn_parent::VarName{sym}, chain, chain_idx, iteration_idx +) where {sym} + # We use `VarName{sym}()` so that the resulting leaf `vn` only contains the tail of the lens. + # This way we can use `getlens(vn)` to extract the value from `x` and use `vn_parent ∘ getlens(vn)` + # to extract the value from the `chain`. + return reduce(varname_leaves(VarName{sym}(), x); init=similar(x)) do x, vn + # Update `x`, possibly in place, and return. + l = AbstractPPL.getlens(vn) + Setfield.set( + x, + BangBang.prefermutation(l), + chain[iteration_idx, Symbol(vn_parent ∘ l), chain_idx], + ) + end +end +function values_from_chain(vi::AbstractVarInfo, vn_parent, chain, chain_idx, iteration_idx) + # Use the value `vi[vn_parent]` to obtain a buffer. + return values_from_chain(vi[vn_parent], vn_parent, chain, chain_idx, iteration_idx) +end + # some functors (#367) struct MyModel a::Int @@ -91,9 +139,9 @@ end chain = Chains(chain_mat, symbol_names) # calculate the pointwise loglikelihoods for the whole chain using the newly written functions - logpriors = logprior_temp(model, chain) + logpriors = logprior(model, chain) loglikelihoods = loglikelihood(model, chain) - logjoints = logjoint_temp(model, chain) + logjoints = logjoint(model, chain) # compare them with true values for i in 1:N samples_dict = Dict() From 02e3610be7f2fd64ec6872152e406b7cc87d2db4 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Fri, 23 Jun 2023 16:16:23 +0100 Subject: [PATCH 202/218] added `print` statements for temporary diagnosis purpose. --- test/model.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/model.jl b/test/model.jl index 03a707b92..dfa9327e7 100644 --- a/test/model.jl +++ b/test/model.jl @@ -107,7 +107,9 @@ end @test ljoint ≈ lp #### logprior, logjoint, loglikelihood for MCMC chains #### + model_no = 0 for model in DynamicPPL.TestUtils.DEMO_MODELS + model_no += 1; println("model $model_no") var_info = VarInfo(model) vns = DynamicPPL.TestUtils.varnames(model) syms = unique(DynamicPPL.getsym.(vns)) @@ -120,8 +122,11 @@ end vals_mat = mapreduce(hcat, 1:N) do i [vals_OrderedDict[i][vn] for vn in vns] end + println("vals_mat: ", size(vals_mat)) vec_of_vec = [vcat(x...)' for x in eachcol(vals_mat)] + println("vec_of_vec: ", size(vec_of_vec)) chain_mat = vcat(vec_of_vec...) + println("chain_mat: ", size(chain_mat)) # devise parameter names for chain sample_values_vec = collect(values(vals_OrderedDict[1])) From fa2fb6a81aee25adadb7366b9959c168f80453ad Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Fri, 23 Jun 2023 16:19:08 +0100 Subject: [PATCH 203/218] added 'print` statements for temporary diagnostics purpose. --- test/model.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/model.jl b/test/model.jl index dfa9327e7..573dfc79b 100644 --- a/test/model.jl +++ b/test/model.jl @@ -109,7 +109,7 @@ end #### logprior, logjoint, loglikelihood for MCMC chains #### model_no = 0 for model in DynamicPPL.TestUtils.DEMO_MODELS - model_no += 1; println("model $model_no") + model_no += 1; println("\n model $model_no \n") var_info = VarInfo(model) vns = DynamicPPL.TestUtils.varnames(model) syms = unique(DynamicPPL.getsym.(vns)) @@ -122,11 +122,11 @@ end vals_mat = mapreduce(hcat, 1:N) do i [vals_OrderedDict[i][vn] for vn in vns] end - println("vals_mat: ", size(vals_mat)) + println("\n vals_mat: ", size(vals_mat)) vec_of_vec = [vcat(x...)' for x in eachcol(vals_mat)] - println("vec_of_vec: ", size(vec_of_vec)) + println("\n vec_of_vec: ", size(vec_of_vec)) chain_mat = vcat(vec_of_vec...) - println("chain_mat: ", size(chain_mat)) + println("\n chain_mat: ", size(chain_mat)) # devise parameter names for chain sample_values_vec = collect(values(vals_OrderedDict[1])) From 57585ffb3080de5269d95043b806a4da7a8cf360 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Fri, 23 Jun 2023 16:25:33 +0100 Subject: [PATCH 204/218] Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index 573dfc79b..6660d64b2 100644 --- a/test/model.jl +++ b/test/model.jl @@ -109,7 +109,8 @@ end #### logprior, logjoint, loglikelihood for MCMC chains #### model_no = 0 for model in DynamicPPL.TestUtils.DEMO_MODELS - model_no += 1; println("\n model $model_no \n") + model_no += 1 + println("\n model $model_no \n") var_info = VarInfo(model) vns = DynamicPPL.TestUtils.varnames(model) syms = unique(DynamicPPL.getsym.(vns)) From 80f23f93aff3fd601f755d40d52710356b38f387 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Fri, 23 Jun 2023 16:50:42 +0100 Subject: [PATCH 205/218] diagnostics again. --- test/model.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/model.jl b/test/model.jl index 6660d64b2..26a1409cc 100644 --- a/test/model.jl +++ b/test/model.jl @@ -107,10 +107,8 @@ end @test ljoint ≈ lp #### logprior, logjoint, loglikelihood for MCMC chains #### - model_no = 0 - for model in DynamicPPL.TestUtils.DEMO_MODELS - model_no += 1 - println("\n model $model_no \n") + model_no = 1 + for model in DynamicPPL.TestUtils.DEMO_MODELS[1:12] var_info = VarInfo(model) vns = DynamicPPL.TestUtils.varnames(model) syms = unique(DynamicPPL.getsym.(vns)) @@ -168,6 +166,8 @@ end @test logjoints[i] ≈ DynamicPPL.TestUtils.logjoint_true(model, samples[:s], samples[:m]) end + println("\n model $model_no done!!! \n") + model_no += 1 end end From 673fb6aadeb6f7fae91de9c19c7f26dada959c4c Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Sat, 24 Jun 2023 10:06:58 +0100 Subject: [PATCH 206/218] Removed some `print` statements as it's working. --- test/model.jl | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/test/model.jl b/test/model.jl index 26a1409cc..d16eb868f 100644 --- a/test/model.jl +++ b/test/model.jl @@ -20,7 +20,7 @@ function varname_leaves(vn::DynamicPPL.VarName, val::NamedTuple) return Iterators.flatten(iter) end -#481 +# dependency: this is part of PR#481 function values_from_chain(x, vn_parent, chain, chain_idx, iteration_idx) # HACK: If it's not an array, we fall back to just returning the first value. return only(chain[iteration_idx, Symbol(vn_parent), chain_idx]) @@ -108,7 +108,7 @@ end #### logprior, logjoint, loglikelihood for MCMC chains #### model_no = 1 - for model in DynamicPPL.TestUtils.DEMO_MODELS[1:12] + for model in DynamicPPL.TestUtils.DEMO_MODELS[1:12] # length(DynamicPPL.TestUtils.DEMO_MODELS)=12 var_info = VarInfo(model) vns = DynamicPPL.TestUtils.varnames(model) syms = unique(DynamicPPL.getsym.(vns)) @@ -121,11 +121,8 @@ end vals_mat = mapreduce(hcat, 1:N) do i [vals_OrderedDict[i][vn] for vn in vns] end - println("\n vals_mat: ", size(vals_mat)) vec_of_vec = [vcat(x...)' for x in eachcol(vals_mat)] - println("\n vec_of_vec: ", size(vec_of_vec)) chain_mat = vcat(vec_of_vec...) - println("\n chain_mat: ", size(chain_mat)) # devise parameter names for chain sample_values_vec = collect(values(vals_OrderedDict[1])) @@ -166,7 +163,7 @@ end @test logjoints[i] ≈ DynamicPPL.TestUtils.logjoint_true(model, samples[:s], samples[:m]) end - println("\n model $model_no done!!! \n") + println("\n model $model_no passed !!! \n") model_no += 1 end end From e5159435a64303980dba4c16d8c9805ff173a7a4 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 4 Jul 2023 17:22:30 +0100 Subject: [PATCH 207/218] Update test/model.jl Co-authored-by: Hong Ge <3279477+yebai@users.noreply.github.com> --- test/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index d16eb868f..50668c9de 100644 --- a/test/model.jl +++ b/test/model.jl @@ -20,7 +20,7 @@ function varname_leaves(vn::DynamicPPL.VarName, val::NamedTuple) return Iterators.flatten(iter) end -# dependency: this is part of PR#481 +# TODO: remove after PR#481 is merged function values_from_chain(x, vn_parent, chain, chain_idx, iteration_idx) # HACK: If it's not an array, we fall back to just returning the first value. return only(chain[iteration_idx, Symbol(vn_parent), chain_idx]) From d4edf589f86e9bb1d3c0fbc82d99245684c7bf6e Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 4 Jul 2023 17:35:57 +0100 Subject: [PATCH 208/218] Update test/model.jl Co-authored-by: Hong Ge <3279477+yebai@users.noreply.github.com> --- test/model.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index 50668c9de..4be0302b9 100644 --- a/test/model.jl +++ b/test/model.jl @@ -164,7 +164,6 @@ end DynamicPPL.TestUtils.logjoint_true(model, samples[:s], samples[:m]) end println("\n model $model_no passed !!! \n") - model_no += 1 end end From 3e4770b05668d6763545fe6d57d9291d1ec668f2 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 4 Jul 2023 17:36:16 +0100 Subject: [PATCH 209/218] Update test/model.jl Co-authored-by: Hong Ge <3279477+yebai@users.noreply.github.com> --- test/model.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index 4be0302b9..846742093 100644 --- a/test/model.jl +++ b/test/model.jl @@ -107,7 +107,6 @@ end @test ljoint ≈ lp #### logprior, logjoint, loglikelihood for MCMC chains #### - model_no = 1 for model in DynamicPPL.TestUtils.DEMO_MODELS[1:12] # length(DynamicPPL.TestUtils.DEMO_MODELS)=12 var_info = VarInfo(model) vns = DynamicPPL.TestUtils.varnames(model) From 55652677c91bb84154fc6de34889c45b6ee617f0 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Tue, 4 Jul 2023 17:36:37 +0100 Subject: [PATCH 210/218] Update test/model.jl Co-authored-by: Hong Ge <3279477+yebai@users.noreply.github.com> --- test/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index 846742093..e5a33623a 100644 --- a/test/model.jl +++ b/test/model.jl @@ -162,7 +162,7 @@ end @test logjoints[i] ≈ DynamicPPL.TestUtils.logjoint_true(model, samples[:s], samples[:m]) end - println("\n model $model_no passed !!! \n") + println("\n model $(model) passed !!! \n") end end From 6e8c84834fe5955b6921f89d093cd7b5e8497be2 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Thu, 6 Jul 2023 16:36:26 +0100 Subject: [PATCH 211/218] 1. moved helper functions to `test_util.jl`; 2. re-wrote the way `chain_mat` can be generated. --- test/model.jl | 91 +++++++++-------------------------------------- test/test_util.jl | 63 ++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 75 deletions(-) diff --git a/test/model.jl b/test/model.jl index e5a33623a..7ec795069 100644 --- a/test/model.jl +++ b/test/model.jl @@ -1,51 +1,3 @@ -# helper functions for testing (both varname_leaves and values_from_chain are in utils.jl, for some reason they are not loaded) -varname_leaves(vn::VarName, ::Real) = [vn] -function varname_leaves(vn::VarName, val::AbstractArray{<:Union{Real,Missing}}) - return ( - VarName(vn, getlens(vn) ∘ Setfield.IndexLens(Tuple(I))) for - I in CartesianIndices(val) - ) -end -function varname_leaves(vn::VarName, val::AbstractArray) - return Iterators.flatten( - varname_leaves(VarName(vn, getlens(vn) ∘ Setfield.IndexLens(Tuple(I))), val[I]) for - I in CartesianIndices(val) - ) -end -function varname_leaves(vn::DynamicPPL.VarName, val::NamedTuple) - iter = Iterators.map(keys(val)) do sym - lens = Setfield.PropertyLens{sym}() - varname_leaves(vn ∘ lens, get(val, lens)) - end - return Iterators.flatten(iter) -end - -# TODO: remove after PR#481 is merged -function values_from_chain(x, vn_parent, chain, chain_idx, iteration_idx) - # HACK: If it's not an array, we fall back to just returning the first value. - return only(chain[iteration_idx, Symbol(vn_parent), chain_idx]) -end -function values_from_chain( - x::AbstractArray, vn_parent::VarName{sym}, chain, chain_idx, iteration_idx -) where {sym} - # We use `VarName{sym}()` so that the resulting leaf `vn` only contains the tail of the lens. - # This way we can use `getlens(vn)` to extract the value from `x` and use `vn_parent ∘ getlens(vn)` - # to extract the value from the `chain`. - return reduce(varname_leaves(VarName{sym}(), x); init=similar(x)) do x, vn - # Update `x`, possibly in place, and return. - l = AbstractPPL.getlens(vn) - Setfield.set( - x, - BangBang.prefermutation(l), - chain[iteration_idx, Symbol(vn_parent ∘ l), chain_idx], - ) - end -end -function values_from_chain(vi::AbstractVarInfo, vn_parent, chain, chain_idx, iteration_idx) - # Use the value `vi[vn_parent]` to obtain a buffer. - return values_from_chain(vi[vn_parent], vn_parent, chain, chain_idx, iteration_idx) -end - # some functors (#367) struct MyModel a::Int @@ -59,31 +11,10 @@ struct MyZeroModel end m ~ Normal(0, 1) return x ~ Normal(m, 1) end -@model function gdemo_d() - s ~ InverseGamma(2, 3) - m ~ Normal(0, sqrt(s)) - 1.5 ~ Normal(m, sqrt(s)) - 2.0 ~ Normal(m, sqrt(s)) - return s, m -end -gdemo_default = gdemo_d() -# function to modify the representation of values based on their length -function modify_value_representation(nt::NamedTuple) - modified_nt = NamedTuple() - for (key, value) in zip(keys(nt), values(nt)) - if length(value) == 1 # Scalar value - modified_value = value[1] - else # Non-scalar value - modified_value = value - end - modified_nt = merge(modified_nt, (key => modified_value,)) - end - return modified_nt -end @testset "model.jl" begin @testset "convenience functions" begin - model = gdemo_default + model = gdemo_default # defined in test/test_util.jl # sample from model and extract variables vi = VarInfo(model) @@ -107,7 +38,7 @@ end @test ljoint ≈ lp #### logprior, logjoint, loglikelihood for MCMC chains #### - for model in DynamicPPL.TestUtils.DEMO_MODELS[1:12] # length(DynamicPPL.TestUtils.DEMO_MODELS)=12 + for model in DynamicPPL.TestUtils.DEMO_MODELS # length(DynamicPPL.TestUtils.DEMO_MODELS)=12 var_info = VarInfo(model) vns = DynamicPPL.TestUtils.varnames(model) syms = unique(DynamicPPL.getsym.(vns)) @@ -120,8 +51,18 @@ end vals_mat = mapreduce(hcat, 1:N) do i [vals_OrderedDict[i][vn] for vn in vns] end - vec_of_vec = [vcat(x...)' for x in eachcol(vals_mat)] - chain_mat = vcat(vec_of_vec...) + i = 1 + for col in eachcol(vals_mat) + col_flattened = [] + [push!(col_flattened, x...) for x in col] + if i == 1 + chain_mat = Matrix(reshape(col_flattened, 1, length(col_flattened))) + else + chain_mat = vcat(chain_mat, reshape(col_flattened, 1, length(col_flattened))) + end + i += 1 + end + chain_mat = convert(Matrix{Float64}, chain_mat) # devise parameter names for chain sample_values_vec = collect(values(vals_OrderedDict[1])) @@ -130,7 +71,7 @@ end for k in 1:length(keys(var_info)) vn_parent = keys(var_info)[k] sym = DynamicPPL.getsym(vn_parent) - vn_children = varname_leaves(vn_parent, sample_values_vec[k]) + vn_children = varname_leaves(vn_parent, sample_values_vec[k]) # `varname_leaves` defined in test/test_util.jl for vn_child in vn_children chain_sym_map[Symbol(vn_child)] = sym symbol_names = [symbol_names; Symbol(vn_child)] @@ -153,7 +94,7 @@ end samples_dict[key] = existing_value end samples = (; samples_dict...) - samples = modify_value_representation(samples) + samples = modify_value_representation(samples) # `modify_value_representation` defined in test/test_util.jl @test logpriors[i] ≈ DynamicPPL.TestUtils.logprior_true(model, samples[:s], samples[:m]) @test loglikelihoods[i] ≈ DynamicPPL.TestUtils.loglikelihood_true( diff --git a/test/test_util.jl b/test/test_util.jl index f3e54c437..e6812d4f8 100644 --- a/test/test_util.jl +++ b/test/test_util.jl @@ -82,3 +82,66 @@ short_varinfo_name(::TypedVarInfo) = "TypedVarInfo" short_varinfo_name(::UntypedVarInfo) = "UntypedVarInfo" short_varinfo_name(::SimpleVarInfo{<:NamedTuple}) = "SimpleVarInfo{<:NamedTuple}" short_varinfo_name(::SimpleVarInfo{<:OrderedDict}) = "SimpleVarInfo{<:OrderedDict}" + +# convenient functions for testing model.jl +# function to modify the representation of values based on their length +function modify_value_representation(nt::NamedTuple) + modified_nt = NamedTuple() + for (key, value) in zip(keys(nt), values(nt)) + if length(value) == 1 # Scalar value + modified_value = value[1] + else # Non-scalar value + modified_value = value + end + modified_nt = merge(modified_nt, (key => modified_value,)) + end + return modified_nt +end + +# helper functions for testing (both varname_leaves and values_from_chain are in utils.jl, for some reason they are not loaded) +varname_leaves(vn::VarName, ::Real) = [vn] +function varname_leaves(vn::VarName, val::AbstractArray{<:Union{Real,Missing}}) + return ( + VarName(vn, getlens(vn) ∘ Setfield.IndexLens(Tuple(I))) for + I in CartesianIndices(val) + ) +end +function varname_leaves(vn::VarName, val::AbstractArray) + return Iterators.flatten( + varname_leaves(VarName(vn, getlens(vn) ∘ Setfield.IndexLens(Tuple(I))), val[I]) for + I in CartesianIndices(val) + ) +end +function varname_leaves(vn::DynamicPPL.VarName, val::NamedTuple) + iter = Iterators.map(keys(val)) do sym + lens = Setfield.PropertyLens{sym}() + varname_leaves(vn ∘ lens, get(val, lens)) + end + return Iterators.flatten(iter) +end + +# TODO: remove after PR#481 is merged +function values_from_chain(x, vn_parent, chain, chain_idx, iteration_idx) + # HACK: If it's not an array, we fall back to just returning the first value. + return only(chain[iteration_idx, Symbol(vn_parent), chain_idx]) +end +function values_from_chain( + x::AbstractArray, vn_parent::VarName{sym}, chain, chain_idx, iteration_idx +) where {sym} + # We use `VarName{sym}()` so that the resulting leaf `vn` only contains the tail of the lens. + # This way we can use `getlens(vn)` to extract the value from `x` and use `vn_parent ∘ getlens(vn)` + # to extract the value from the `chain`. + return reduce(varname_leaves(VarName{sym}(), x); init=similar(x)) do x, vn + # Update `x`, possibly in place, and return. + l = AbstractPPL.getlens(vn) + Setfield.set( + x, + BangBang.prefermutation(l), + chain[iteration_idx, Symbol(vn_parent ∘ l), chain_idx], + ) + end +end +function values_from_chain(vi::AbstractVarInfo, vn_parent, chain, chain_idx, iteration_idx) + # Use the value `vi[vn_parent]` to obtain a buffer. + return values_from_chain(vi[vn_parent], vn_parent, chain, chain_idx, iteration_idx) +end \ No newline at end of file From e109bbf59d9071cfaba72402b2feb9ae08d906a6 Mon Sep 17 00:00:00 2001 From: YongchaoHuang <34540771+YongchaoHuang@users.noreply.github.com> Date: Thu, 6 Jul 2023 16:41:11 +0100 Subject: [PATCH 212/218] Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/model.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index 7ec795069..d3969ba14 100644 --- a/test/model.jl +++ b/test/model.jl @@ -58,7 +58,9 @@ end if i == 1 chain_mat = Matrix(reshape(col_flattened, 1, length(col_flattened))) else - chain_mat = vcat(chain_mat, reshape(col_flattened, 1, length(col_flattened))) + chain_mat = vcat( + chain_mat, reshape(col_flattened, 1, length(col_flattened)) + ) end i += 1 end From 6007a90fa7c1e055b386793a8b2f660ad8c18ea9 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Thu, 6 Jul 2023 16:48:46 +0100 Subject: [PATCH 213/218] formatting. --- test/test_util.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_util.jl b/test/test_util.jl index e6812d4f8..c6b970fb1 100644 --- a/test/test_util.jl +++ b/test/test_util.jl @@ -120,6 +120,7 @@ function varname_leaves(vn::DynamicPPL.VarName, val::NamedTuple) return Iterators.flatten(iter) end +# values_from_chain # TODO: remove after PR#481 is merged function values_from_chain(x, vn_parent, chain, chain_idx, iteration_idx) # HACK: If it's not an array, we fall back to just returning the first value. From 761723789232b85a75c526637c81494a245878f4 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Thu, 6 Jul 2023 16:57:57 +0100 Subject: [PATCH 214/218] formatting. --- test/test_util.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_util.jl b/test/test_util.jl index c6b970fb1..3202d3a16 100644 --- a/test/test_util.jl +++ b/test/test_util.jl @@ -120,7 +120,6 @@ function varname_leaves(vn::DynamicPPL.VarName, val::NamedTuple) return Iterators.flatten(iter) end -# values_from_chain # TODO: remove after PR#481 is merged function values_from_chain(x, vn_parent, chain, chain_idx, iteration_idx) # HACK: If it's not an array, we fall back to just returning the first value. @@ -142,6 +141,7 @@ function values_from_chain( ) end end + function values_from_chain(vi::AbstractVarInfo, vn_parent, chain, chain_idx, iteration_idx) # Use the value `vi[vn_parent]` to obtain a buffer. return values_from_chain(vi[vn_parent], vn_parent, chain, chain_idx, iteration_idx) From dbfc1719f8531bb8c6afd772ab14f1d5e9ff2252 Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Fri, 21 Jul 2023 14:21:41 +0100 Subject: [PATCH 215/218] Update utils.jl --- src/utils.jl | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index da74f0f52..5c129f4a4 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -862,28 +862,3 @@ function varname_leaves(vn::DynamicPPL.VarName, val::NamedTuple) end return Iterators.flatten(iter) end - -function values_from_chain(x, vn_parent, chain, chain_idx, iteration_idx) - # HACK: If it's not an array, we fall back to just returning the first value. - return only(chain[iteration_idx, Symbol(vn_parent), chain_idx]) -end -function values_from_chain( - x::AbstractArray, vn_parent::VarName{sym}, chain, chain_idx, iteration_idx -) where {sym} - # We use `VarName{sym}()` so that the resulting leaf `vn` only contains the tail of the lens. - # This way we can use `getlens(vn)` to extract the value from `x` and use `vn_parent ∘ getlens(vn)` - # to extract the value from the `chain`. - return reduce(varname_leaves(VarName{sym}(), x); init=similar(x)) do x, vn - # Update `x`, possibly in place, and return. - l = AbstractPPL.getlens(vn) - Setfield.set( - x, - BangBang.prefermutation(l), - chain[iteration_idx, Symbol(vn_parent ∘ l), chain_idx], - ) - end -end -function values_from_chain(vi::AbstractVarInfo, vn_parent, chain, chain_idx, iteration_idx) - # Use the value `vi[vn_parent]` to obtain a buffer. - return values_from_chain(vi[vn_parent], vn_parent, chain, chain_idx, iteration_idx) -end From 7c6888dfb23b369bb164c48ab45794d94a86c984 Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Fri, 21 Jul 2023 14:23:38 +0100 Subject: [PATCH 216/218] Update test_util.jl --- test/test_util.jl | 49 ----------------------------------------------- 1 file changed, 49 deletions(-) diff --git a/test/test_util.jl b/test/test_util.jl index 3202d3a16..892f7221a 100644 --- a/test/test_util.jl +++ b/test/test_util.jl @@ -97,52 +97,3 @@ function modify_value_representation(nt::NamedTuple) end return modified_nt end - -# helper functions for testing (both varname_leaves and values_from_chain are in utils.jl, for some reason they are not loaded) -varname_leaves(vn::VarName, ::Real) = [vn] -function varname_leaves(vn::VarName, val::AbstractArray{<:Union{Real,Missing}}) - return ( - VarName(vn, getlens(vn) ∘ Setfield.IndexLens(Tuple(I))) for - I in CartesianIndices(val) - ) -end -function varname_leaves(vn::VarName, val::AbstractArray) - return Iterators.flatten( - varname_leaves(VarName(vn, getlens(vn) ∘ Setfield.IndexLens(Tuple(I))), val[I]) for - I in CartesianIndices(val) - ) -end -function varname_leaves(vn::DynamicPPL.VarName, val::NamedTuple) - iter = Iterators.map(keys(val)) do sym - lens = Setfield.PropertyLens{sym}() - varname_leaves(vn ∘ lens, get(val, lens)) - end - return Iterators.flatten(iter) -end - -# TODO: remove after PR#481 is merged -function values_from_chain(x, vn_parent, chain, chain_idx, iteration_idx) - # HACK: If it's not an array, we fall back to just returning the first value. - return only(chain[iteration_idx, Symbol(vn_parent), chain_idx]) -end -function values_from_chain( - x::AbstractArray, vn_parent::VarName{sym}, chain, chain_idx, iteration_idx -) where {sym} - # We use `VarName{sym}()` so that the resulting leaf `vn` only contains the tail of the lens. - # This way we can use `getlens(vn)` to extract the value from `x` and use `vn_parent ∘ getlens(vn)` - # to extract the value from the `chain`. - return reduce(varname_leaves(VarName{sym}(), x); init=similar(x)) do x, vn - # Update `x`, possibly in place, and return. - l = AbstractPPL.getlens(vn) - Setfield.set( - x, - BangBang.prefermutation(l), - chain[iteration_idx, Symbol(vn_parent ∘ l), chain_idx], - ) - end -end - -function values_from_chain(vi::AbstractVarInfo, vn_parent, chain, chain_idx, iteration_idx) - # Use the value `vi[vn_parent]` to obtain a buffer. - return values_from_chain(vi[vn_parent], vn_parent, chain, chain_idx, iteration_idx) -end \ No newline at end of file From 1397dfc45cf606bef9fcad45e664c35e68b07ac5 Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Fri, 21 Jul 2023 14:34:12 +0100 Subject: [PATCH 217/218] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 6b8abb913..928327b08 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "DynamicPPL" uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8" -version = "0.23.7" +version = "0.23.8" [deps] AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001" From ac8d9e5d98fd460089ca81a56b0ebfd1ba542e12 Mon Sep 17 00:00:00 2001 From: YongchaoHuang Date: Sun, 23 Jul 2023 16:44:56 +0100 Subject: [PATCH 218/218] replaced 'varname_leaves' by 'DynamicPPL.varname_leaves'. --- test/model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.jl b/test/model.jl index f3c68271f..481aa4e38 100644 --- a/test/model.jl +++ b/test/model.jl @@ -86,7 +86,7 @@ end for k in 1:length(keys(var_info)) vn_parent = keys(var_info)[k] sym = DynamicPPL.getsym(vn_parent) - vn_children = varname_leaves(vn_parent, sample_values_vec[k]) # `varname_leaves` defined in test/test_util.jl + vn_children = DynamicPPL.varname_leaves(vn_parent, sample_values_vec[k]) # `varname_leaves` defined in src/utils.jl for vn_child in vn_children chain_sym_map[Symbol(vn_child)] = sym symbol_names = [symbol_names; Symbol(vn_child)]