MonteCarloMeasurements.jl

Documentation for MonteCarloMeasurements.jl ↗

MonteCarloMeasurements.jl is a package that lets you treat probability distributions as first-class numbers. FlexiChains contains an extension which allows you to directly convert a FlexiChain into a collection of MonteCarloMeasurements.Particles objects, in essence creating Particles from the distribution which the chain represents.

using FlexiChainschn = FlexiChains.EXAMPLE_CHAIN
╭─FlexiChain (10 iterations, 2 chains) ────────────────────────────────────────╮
│ ↓ iter  = 101:110                                                            │
│ → chain = 1:2                                                                │
│                                                                              │
│ Parameters (3) ── VarName                                                    │
│  Float64          mu, tau                                                    │
│  Vector{Float64}  theta (8,)                                                 │
│                                                                              │
│ Extras (2)                                                                   │
│  Float64  logjoint                                                           │
│  Bool     is_divergent                                                       │
╰──────────────────────────────────────────────────────────────────────────────╯
using MonteCarloMeasurements: Particlespdict = Particles(chn)
OrderedCollections.OrderedDict{VarName, Any} with 3 entries:
  mu    => 6.29 ± 4.0
  tau   => 2.44 ± 1.8
  theta => Particles{Float64, 20}[7.89 ± 4.6, 6.91 ± 4.3, 6.31 ± 4.2, 5.95 ± 5.…
# Calculates the distribution of `mu + tau` (for example).pdict[@vn(mu)] + pdict[@vn(tau)]
8.72428 ± 3.87 MonteCarloMeasurements.Particles{Float64, 20}

Notice that pdict[@vn(mu)] can be constructed quite trivially without an extension via

Particles(vec(chn[@vn(mu)]))
6.28924 ± 4.04 MonteCarloMeasurements.Particles{Float64, 20}

However, FlexiChains' extension additionally handles array- and NamedTuple-valued variables for you, such as theta above.

pdict[@vn(theta)]
8-element Vector{MonteCarloMeasurements.Particles{Float64, 20}}:
 7.89 ± 4.6
 6.91 ± 4.3
 6.31 ± 4.2
 5.95 ± 5.7
 5.87 ± 4.1
 5.25 ± 5.5
 6.79 ± 5.0
 7.01 ± 4.5

The default output is an OrderedDict, which is not always the most convenient type to work with. If you prefer a NamedTuple, you can pass this as an argument to the constructor:

pnt = Particles(chn, NamedTuple)
(mu = 6.29 ± 4.0, tau = 2.44 ± 1.8, theta = MonteCarloMeasurements.Particles{Float64, 20}[7.89 ± 4.6, 6.91 ± 4.3, 6.31 ± 4.2, 5.95 ± 5.7, 5.87 ± 4.1, 5.25 ± 5.5, 6.79 ± 5.0, 7.01 ± 4.5])

This can be easier to use sometimes but note that since NamedTuple keys are plain Symbols this conversion might be lossy depending on your chain's parameter type.

Unhandled parameter types

Not every parameter can be meaningfully converted into a Particles object. For example, Cholesky factors (x ~ LKJCholesky(...)) are not supported and will error. If you want to convert a chain that contains such a variable into a Particles object, subset the chain first to only include the parameters you want to convert, e.g.

Particles(chn[[param1, param2, param3]])

Docstrings

MonteCarloMeasurements.Particles — Type
MonteCarloMeasurements.Particles(
    chain::FC.FlexiChain{Tparam},
    ::Type{Tout}=OrderedDict,
) where {Tparam,Tout}

Convert a chain into an OrderedDict, mapping parameters in the chain (of type Tparam) to MonteCarloMeasurements.Particles or collections thereof.

Extras in the chain will be dropped.

Scalar-valued parameters will become single Particles; array-valued parameters will become arrays of Particles (as long as all the arrays have the same size).

Note that if parameters have different sizes or types in different samples, this will conservatively throw an error. Suggestions for improvements are very welcome!

Tout can also be:

  • Dict, in which case the output will be Dict rather than OrderedDict;
  • NamedTuple. Note that this conversion can be lossy if Tparam is a richer type than

Symbol.

source