From cc6439ee19e843be1c86d8ee2eb69663316d1973 Mon Sep 17 00:00:00 2001 From: Xianda Sun <5433119+sunxd3@users.noreply.github.com> Date: Sun, 27 Oct 2024 19:45:23 +0000 Subject: [PATCH] Add `fix` and `unfix` functions for parameter fixing and unfixing (#109) * Add fix() and unfix() functions for parameter fixing and unfixing * bump version and add export * add to doc * add sentence following Penny's comment --- Project.toml | 2 +- docs/src/api.md | 2 ++ src/AbstractPPL.jl | 2 +- src/abstractprobprog.jl | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 2a74776..db0655f 100644 --- a/Project.toml +++ b/Project.toml @@ -3,7 +3,7 @@ uuid = "7a57a42e-76ec-4ea3-a279-07e840d6d9cf" keywords = ["probablistic programming"] license = "MIT" desc = "Common interfaces for probabilistic programming" -version = "0.9.0" +version = "0.10.0" [deps] AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001" diff --git a/docs/src/api.md b/docs/src/api.md index 0e7f3f8..e4ca9e2 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -29,6 +29,8 @@ string_to_varname AbstractProbabilisticProgram condition decondition +fix +unfix logdensityof AbstractContext evaluate!! diff --git a/src/AbstractPPL.jl b/src/AbstractPPL.jl index 33fa417..f121b97 100644 --- a/src/AbstractPPL.jl +++ b/src/AbstractPPL.jl @@ -18,7 +18,7 @@ export VarName, # Abstract model functions -export AbstractProbabilisticProgram, condition, decondition, logdensityof, densityof, AbstractContext, evaluate!! +export AbstractProbabilisticProgram, condition, decondition, fix, unfix, logdensityof, densityof, AbstractContext, evaluate!! # Abstract traces export AbstractModelTrace diff --git a/src/abstractprobprog.jl b/src/abstractprobprog.jl index 30b8b35..9051d3e 100644 --- a/src/abstractprobprog.jl +++ b/src/abstractprobprog.jl @@ -62,6 +62,47 @@ should hold for generative models `m` and arbitrary `obs`. """ function condition end +""" + fix(model, params) + +Fix the values of parameters specified in `params` within the probabilistic model `model`. +This operation is equivalent to treating the fixed parameters as being drawn from a point mass +distribution centered at the values specified in `params`. Thus these parameters no longer contribute +to the accumulated log density. + +Conceptually, this is similar to Pearl's do-operator in causal inference, where we intervene +on variables by setting them to specific values, effectively cutting off their dependencies +on their usual causes in the model. + +The invariant + +``` +m == unfix(fix(m, params)) +``` + +should hold for any model `m` and parameters `params`. +""" +function fix end + + +""" + unfix(model) + +Remove any fixed parameters from the model `model`, returning a new model without the fixed parameters. + +This function reverses the effect of `fix` by removing parameter constraints that were previously set. +It returns a new model where all previously fixed parameters are allowed to vary according to their +original distributions in the model. + +The invariant + +``` +m == unfix(fix(m, params)) +``` + +should hold for any model `m` and parameters `params`. +""" +function unfix end """ rand([rng=Random.default_rng()], [T=NamedTuple], model::AbstractProbabilisticProgram) -> T