From 7234054e22304f32fe091cb09363a038ab36482c Mon Sep 17 00:00:00 2001 From: Riccardo Buscicchio Date: Mon, 7 Apr 2025 21:41:18 +0200 Subject: [PATCH] feat: univariate Gaussian likelihood in Gaussian prior --- src/models/Models.jl | 1 + src/models/univariategaussiangaussian.jl | 37 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/models/univariategaussiangaussian.jl diff --git a/src/models/Models.jl b/src/models/Models.jl index a89a0ffc..369108de 100644 --- a/src/models/Models.jl +++ b/src/models/Models.jl @@ -15,5 +15,6 @@ using LogExpFunctions include("shells.jl") include("correlated.jl") include("eggbox.jl") +include("univariagaussiangaussian.jl") end # module diff --git a/src/models/univariategaussiangaussian.jl b/src/models/univariategaussiangaussian.jl new file mode 100644 index 00000000..7c4f70c0 --- /dev/null +++ b/src/models/univariategaussiangaussian.jl @@ -0,0 +1,37 @@ +using Distributions +using NestedSamplers +using StatsBase + +@doc raw""" +Models.UnivariateGaussianGaussian() + +Creates a model with Normal prior and Normal likelihood. + +```math +\mathbf\theta \sim \mathcal{N}\left(\mu_p, \sigma_p\right) +``` +```math +\mathbf{d} \sim \mathcal{N}\left(\mathbf\theta, \sigma_d\right) +``` +the analytical evidence of the model is + +```math +Z = \mathcal{N}\left(\mu_p, \sqrt{\sigma_p^2 + \sigma_d^2}\right) +``` + +## Examples +```jldoctest +julia> model, lnZ = Models.UnivariateNormalNormal(2, 0.5, 1, 0); + +julia> lnZ +-2.6305103088617776 +``` +""" + +function UnivariateGaussianGaussian(μp::Float64, σp::Float64, σd::Float64, d::Float64=0.0) + priors = [Normal(μp, σp)] # θ ~ N(μ, σ) + loglike(θ) = logpdf(Normal(d, σd),θ[1]) # d ~ N(θ, σd) + model = NestedModel(loglike, priors) + true_lnZ = logpdf(Normal(μp, sqrt(σp^2 + σd^2)), d) # lnZ is analytical in the data and the prior hyperparameters + return model, true_lnZ +end