Need help with MonteCarloMeasurements.jl?

Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

157 Stars 6 Forks MIT License 427 Commits 19 Opened issues

Propagation of distributions by Monte-Carlo sampling: Real number types with uncertainty represented by samples.

Readme

*Imagine you had a type that behaved like your standard *

Float64

Gamma(0.5)or

MvNormal(m, S). Then you could call

y=f(x)and have

ybe the probability distribution

y=p(f(x)). This package gives you such a type.

This package facilitates working with probability distributions by means of Monte-Carlo methods, in a way that allows for propagation of probability distributions through functions. This is useful for, e.g., nonlinear uncertainty propagation. A variable or parameter might be associated with uncertainty if it is measured or otherwise estimated from data. We provide two core types to represent probability distributions:

Particlesand

StaticParticles, both

<: real>. (The name "Particles" comes from the particle-filtering literature.) These types all form a Monte-Carlo approximation of the distribution of a floating point number, i.e., the distribution is represented by samples/particles.Correlated quantitiesare handled as well, see multivariate particles below.Although several interesting use cases for doing calculations with probability distributions have popped up (see Examples), the original goal of the package is similar to that of Measurements.jl, to propagate the uncertainty from input of a function to the output. The difference compared to a

Measurementis thatParticlesrepresent the distribution using a vector of unweighted particles, and can thus represent arbitrary distributions and handle nonlinear uncertainty propagation well. Functions likef(x) = x²,f(x) = sign(x)atx=0and long-time integration, are examples that are not handled well using linear uncertainty propagation ala Measurements.jl. MonteCarloMeasurements also support correlations between quantities.A number of type

Particlesbehaves just as any otherNumberwhile partaking in calculations. After a calculation, an approximation to thecomplete distributionof the output is captured and represented by the output particles.mean,stdetc. can be extracted from the particles using the corresponding functions.Particlesalso interact with Distributions.jl, so that you can call, e.g.,Normal(p)and get back aNormaltype from distributions orfit(Gamma, p)to get aGammadistribution. Particles can also be iterated, asked formaximum/minimum,quantileetc. If particles are plotted withplot(p), a histogram is displayed. This requires Plots.jl. A kernel-density estimate can be obtained bydensity(p)is StatsPlots.jl is loaded.Below, we show an example where an input uncertainty is propagated through

σ(x)In the figure above, we see the probability-density function of the input

p(x)depicted on the x-axis. The density of the outputp(y) = f(x)is shown on the y-axis. Linear uncertainty propagation does this by linearizingf(x)and using the equations for an affine transformation of a Gaussian distribution, and hence produces a Gaussian approximation to the output density. The particles form a sampled approximation of the input densityp(x). After propagating them throughf(x), they form a sampled approximation top(y)which correspond very well to the true output density, even though only 20 particles were used in this example. The figure can be reproduced byexamples/transformed_densities.jl.## Quick start

using MonteCarloMeasurements, Plots a = π ± 0.1 # Construct Gaussian uncertain parameters using ± (\\pm) # Particles{Float64,2000} # 3.14159 ± 0.1 b = 2 ∓ 0.1 # ∓ (\\mp) creates StaticParticles (with StaticArrays) # StaticParticles{Float64,100} # 2.0 ± 0.0999 std(a) # Ask about statistical properties # 0.09999231528930486 sin(a) # Use them like any real number # Particles{Float64,2000} # 1.2168e-16 ± 0.0995 plot(a) # Plot them b = sin.(1:0.1:5) .± 0.1; # Create multivariate uncertain numbers plot(b) # Vectors of particles can be plotted using Distributions c = Particles(500, Poisson(3.)) # Create uncertain numbers distributed according to a given distribution # Particles{Int64,500} # 2.882 ± 1.7For further help, see the documentation, the examples folder or the arXiv paper.