Exported functions and types
Index
MonteCarloMeasurements.MonteCarloMeasurementsMonteCarloMeasurements.ParticlesMonteCarloMeasurements.StaticParticlesMonteCarloMeasurements.WorkspaceMonteCarloMeasurements.WorkspaceBase.:≈Base.:≉MonteCarloMeasurements.:..MonteCarloMeasurements.:±MonteCarloMeasurements.:∓MonteCarloMeasurements.:⊗MonteCarloMeasurements.:⊞MonteCarloMeasurements.:⊠MonteCarloMeasurements.array_of_structsMonteCarloMeasurements.bootstrapMonteCarloMeasurements.bootstrapMonteCarloMeasurements.build_containerMonteCarloMeasurements.build_mutable_containerMonteCarloMeasurements.bymapMonteCarloMeasurements.bymap_aMonteCarloMeasurements.bypmapMonteCarloMeasurements.errorbarplotMonteCarloMeasurements.essMonteCarloMeasurements.has_particlesMonteCarloMeasurements.has_particlesMonteCarloMeasurements.mcplotMonteCarloMeasurements.mean_objectMonteCarloMeasurements.mean_objectMonteCarloMeasurements.nominalMonteCarloMeasurements.outer_productMonteCarloMeasurements.register_primitiveMonteCarloMeasurements.register_primitive_multiMonteCarloMeasurements.register_primitive_singleMonteCarloMeasurements.replace_particlesMonteCarloMeasurements.ribbonplotMonteCarloMeasurements.set_comparison_functionMonteCarloMeasurements.sigmapointsMonteCarloMeasurements.systematic_sampleMonteCarloMeasurements.transform_momentsMonteCarloMeasurements.unsafe_comparisonsMonteCarloMeasurements.wassersteinMonteCarloMeasurements.with_nominalMonteCarloMeasurements.with_workspaceMonteCarloMeasurements.ℂ2ℂ_functionMonteCarloMeasurements.ℝⁿ2ℂⁿ_functionMonteCarloMeasurements.ℝⁿ2ℂⁿ_functionMonteCarloMeasurements.ℝⁿ2ℝⁿ_functionMonteCarloMeasurements.ℝⁿ2ℝⁿ_functionMonteCarloMeasurements.@bymapMonteCarloMeasurements.@bypmapMonteCarloMeasurements.@probMonteCarloMeasurements.@unsafe
MonteCarloMeasurements.MonteCarloMeasurements — ModuleThis 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: Particles and 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 quantities are handled as well, see multivariate particles below.
A number of type Particles behaves just as any other Number while partaking in calculations. Particles also behave like a distribution, so after a calculation, an approximation to the complete distribution of the output is captured and represented by the output particles. mean, std etc. can be extracted from the particles using the corresponding functions pmean and pstd. Particles also interact with Distributions.jl, so that you can call, e.g., Normal(p) and get back a Normal type from distributions or fit(Gamma, p) to get a Gammadistribution. Particles can also be asked for maximum/minimum, quantile etc. using functions with a prefix p, i.e., pmaximum. If particles are plotted with plot(p), a histogram is displayed. This requires Plots.jl. A kernel-density estimate can be obtained by density(p) is StatsPlots.jl is loaded.
Quick start
julia> using MonteCarloMeasurements, Plots
julia> a = π ± 0.1 # Construct Gaussian uncertain parameters using ± (\pm)
Particles{Float64,2000}
3.14159 ± 0.1
julia> b = 2 ∓ 0.1 # ∓ (\mp) creates StaticParticles (with StaticArrays)
StaticParticles{Float64,100}
2.0 ± 0.0999
julia> pstd(a) # Ask about statistical properties
0.09999231528930486
julia> sin(a) # Use them like any real number
Particles{Float64,2000}
1.2168e-16 ± 0.0995
julia> plot(a) # Plot them
julia> b = sin.(1:0.1:5) .± 0.1; # Create multivariate uncertain numbers
julia> plot(b) # Vectors of particles can be plotted
julia> using Distributions
julia> 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.
MonteCarloMeasurements.Particles — Typestruct Particles{T, N} <: AbstractParticles{T, N}This type represents uncertainty using a cloud of particles.
Constructors:
Particles()Particles(N::Integer)Particles([rng::AbstractRNG,] d::Distribution)Particles([rng::AbstractRNG,] N::Integer, d::Distribution; permute=true, systematic=true)Particles(v::Vector{T} where T)Particles(m::Matrix{T} where T): Creates multivariate particles (Vector{Particles})
MonteCarloMeasurements.StaticParticles — Typestruct StaticParticles{T, N} <: AbstractParticles{T, N}See ?Particles for help. The difference between StaticParticles and Particles is that the StaticParticles store particles in a static vecetor. This makes runtimes much shorter, but compile times longer. See the documentation for some benchmarks. Only recommended for sample sizes of ≲ 300-400
MonteCarloMeasurements.Workspace — Typestruct Workspace{T1, T2, T3, T4, T5, T6}Arguments:
simple_input: Input objectfwill be called with, does not contain any particlessimple_result: Simple output fromfwithout particlesresult: Complete output offincluding particlesbuffersetter: Helper function to shift data between objectsresultsetter: Helper function to shift data between objectsf: Function to callN: Number of particles
MonteCarloMeasurements.Workspace — MethodWorkspace(f, input)Create a Workspace object for inputs of type typeof(input). Useful if input is a structure with fields of type <: AbstractParticles (can be deeply nested). See also with_workspace.
MonteCarloMeasurements.:.. — MethodMonteCarloMeasurements.:± — Functionμ ± σCreates 2000 Particles with mean μ and std σ. It can also be used as a unary operator, a mean of 0 is then used with std σ. If μ is a vector, the constructor MvNormal is used, and σ is thus treated as std if it's a scalar, and variances if it's a matrix or vector. See also ∓, ..
MonteCarloMeasurements.:∓ — Functionμ ∓ σCreates 100 StaticParticles with mean μ and std σ. It can also be used as a unary operator, a mean of 0 is then used with std σ. If μ is a vector, the constructor MvNormal is used, and σ is thus treated as std if it's a scalar, and variances if it's a matrix or vector. See also ±, ⊗
MonteCarloMeasurements.:⊞ — Methoda ⊞ Distribution()Adds 2000 Particles sampled from a specified ::Distribution to a. Shorthand for a + Particles(Distribution()), e.g., 1 ⊞ Binomial(3).
MonteCarloMeasurements.:⊠ — Methoda ⊠ Distribution()Multiplies a by 2000 Particles sampled from a specified ::Distribution. Shorthand for a * Particles(Distribution()), e.g., a ⊠ Gamma(1).
MonteCarloMeasurements.bootstrap — Functionbootstrap([rng::AbstractRNG,] p::Particles, n = nparticles(p))Return Particles resampled with replacement. n specifies the number of samples to draw. Also works for arrays of Particles, in which case a single set of indices are drawn and used to extract samples from all elements in the array.
MonteCarloMeasurements.bootstrap — Functionbootstrap([rng::AbstractRNG,] p::Particles, n = nparticles(p))Return Particles resampled with replacement. n specifies the number of samples to draw. Also works for arrays of Particles, in which case a single set of indices are drawn and used to extract samples from all elements in the array.
MonteCarloMeasurements.bymap — Methodbymap(f, args...)Uncertainty propagation using the map function.
Call f with particles or vectors of particles by using map. This can be utilized if registering f using register_primitive fails. See also Workspace if bymap fails.
MonteCarloMeasurements.bymap_a — Functionbymap_a(f, args...)Like f(args...), but with uncertainty propagation.
Similar to bymap, but expands particles contained anywhere within args. In addition, reassembles the result to be like the original f output with Particles instead of numbers.
This function is experimental and requires loading the AccessorsExtra package.
MonteCarloMeasurements.bypmap — MethodDistributed uncertainty propagation using the pmap function. See bymap for more details.
MonteCarloMeasurements.errorbarplot — Functionerrorbarplot(x,y,[q=0.025])Plots a vector of particles with error bars at quantile q. If q::Tuple, then you can specify both lower and upper quantile, e.g., (0.01, 0.99).
MonteCarloMeasurements.ess — Methodess(p::AbstractParticles{T,N})Calculates the effective sample size. This is useful if particles come from MCMC sampling and are correlated in time. The ESS is a number between [0,N].
Initial source: https://github.com/tpapp/MCMCDiagnostics.jl
MonteCarloMeasurements.has_particles — Methodhas_particles(P)Determine whether or no the object P has some kind of particles inside it. This function examins fields of P recursively and looks inside arrays etc.
MonteCarloMeasurements.mcplot — Functionmcplot(x,y,[N=0])Plots all trajectories represented by a vector of particles. N > 1 controls the number of trajectories to plot.
MonteCarloMeasurements.mean_object — Methodmean_object(x)Returns an object similar to x, but where all internal instances of Particles are replaced with their mean. The generalization of this function is replace_particles.
MonteCarloMeasurements.nominal — Methodnominal(p)Return the nominal value of p (assumes that p has been endowed with a nominal value using with_nominal).
MonteCarloMeasurements.outer_product — Functionp = outer_product([rng::AbstractRNG,] dists::Vector{<:Distribution}, N=100_000)Creates a multivariate systematic sample where each dimension is sampled according to the corresponding univariate distribution in dists. Returns p::Vector{Particles} where each Particles has a length approximately equal to N. The particles form the outer product between d systematically sampled vectors with length given by the d:th root of N, where d is the length of dists, All particles will be independent and have marginal distributions given by dists.
See also MonteCarloMeasurements.⊗
MonteCarloMeasurements.register_primitive — Functionregister_primitive(f, eval=eval)Register both single and multi-argument function so that it works with particles. If you want to register functions from within a module, you must pass the modules eval function.
MonteCarloMeasurements.register_primitive_multi — Functionregister_primitive_multi(ff, eval=eval)Register a multi-argument function so that it works with particles. If you want to register functions from within a module, you must pass the modules eval function.
MonteCarloMeasurements.register_primitive_single — Functionregister_primitive_single(ff, eval=eval)Register a single-argument function so that it works with particles. If you want to register functions from within a module, you must pass the modules eval function.
MonteCarloMeasurements.ribbonplot — Functionribbonplot(x,y,[q=0.025]; N=true)Plots a vector of particles with a ribbon covering quantiles q, 1-q. If q::Tuple, then you can specify both lower and upper quantile, e.g., (0.01, 0.99).
If a positive number N is provided, N sample trajectories will be plotted on top of the ribbon.
MonteCarloMeasurements.set_comparison_function — Methodset_comparison_function(f)Change the Function used to reduce particles to a number for comparison operators Toggle the use of a comparison Function without warning using the Function unsafe_comparisons.
MonteCarloMeasurements.sigmapoints — Methodsigmapoints(m, Σ)
sigmapoints(d::Normal)
sigmapoints(d::MvNormal)The unscented transform uses a small number of points to propagate the first and second moments of a probability density, called sigma points. We provide a function sigmapoints(μ, Σ) that creates a Matrix of 2n+1 sigma points, where n is the dimension. This can be used to initialize any kind of AbstractParticles, e.g.:
julia> m = [1,2];
julia> Σ = [3. 1; 1 4];
julia> p = StaticParticles(sigmapoints(m,Σ))
2-element Vector{StaticParticles{Float64, 5}}:
1.0 ± 1.7
2.0 ± 2.0
julia> cov(p) ≈ Σ
true
julia> mean(p) ≈ m
trueMake sure to pass the variance (not std) as second argument in case μ and Σ are scalars.
Caveat
If you are creating several one-dimensional uncertain values using sigmapoints independently, they will be strongly correlated. Use the multidimensional constructor! Example:
p = StaticParticles(sigmapoints(1, 0.1^2)) # Wrong!
ζ = StaticParticles(sigmapoints(0.3, 0.1^2)) # Wrong!
ω = StaticParticles(sigmapoints(1, 0.1^2)) # Wrong!
p,ζ,ω = StaticParticles(sigmapoints([1, 0.3, 1], 0.1^2)) # CorrectMonteCarloMeasurements.systematic_sample — Functionsystematic_sample([rng::AbstractRNG,] N, d=Normal(0,1); permute=true)returns a Vector of length N sampled systematically from the distribution d. If permute=false, this vector will be sorted.
MonteCarloMeasurements.transform_moments — MethodY = transform_moments(X::Matrix, m, Σ; preserve_latin=false)Transforms X such that it get the specified mean and covariance.
julia> m, Σ = [1,2], [2 1; 1 4]; # Desired mean and covariance
julia> particles = transform_moments(X, m, Σ);
julia> cov(particles) ≈ Σ
trueNote, if X is a latin hypercube and Σ is non-diagonal, then the latin property is destroyed for all dimensions but the first. We provide a method preserve_latin=true) which absolutely preserves the latin property in all dimensions, but if you use this, the covariance of the sample will be slightly wrong
MonteCarloMeasurements.unsafe_comparisons — Functionunsafe_comparisons(onoff=true; verbose=true)Toggle the use of a comparison function without warning. By default mean is used to reduce particles to a floating point number for comparisons. This function can be changed, example: set_comparison_function(median)
unsafe_comparisons(mode=:reduction; verbose=true)One can also specify a comparison mode, mode can take the values :safe, :montecarlo, :reduction. :safe is the same as calling unsafe_comparisons(false) and :reduction corresponds to true.
See Documentation: Comparison mode for more details.
MonteCarloMeasurements.wasserstein — Methodwasserstein(p1::AbstractParticles,p2::AbstractParticles,p)Returns the Wasserstein distance (Earth-movers distance) of order p, to the pth power, between p1 and p2. I.e., for p=2, this returns W₂²
MonteCarloMeasurements.with_nominal — Methodpn = with_nominal(p, val)Endow particles p with a nominal value val. The particle closest to val will be replaced with val, and moved to index 1. This operation introduces a slight bias in the statistics of pn, but the operation is asymptotically unbiased for large sample sizes. To obtain the nominal value of pn, call nominal(pn).
MonteCarloMeasurements.with_workspace — Methodwith_workspace(f,P)In some cases, defining a primitive function which particles are to be propagate through is not possible but allowing unsafe comparisons are not acceptable. One such case is functions that internally calculate eigenvalues of uncertain matrices. The eigenvalue calculation makes use of comparison operators. If the uncertainty is large, eigenvalues might change place in the sorted list of returned eigenvalues, completely ruining downstream computations. For this we recommend, in order of preference
- Use
@bymapdetailed in the documentation. Applicable if all uncertain values appears as arguments to your entry function. - Create a
Workspaceobject and call it using your entry function. Applicable if uncertain parameters appear nested in an object that is an argument to your entry function:
# desired computation: y = f(obj), obj contains uncertain parameters inside
y = with_workspace(f, obj)
# or equivalently
w = Workspace(f, obj)
use_invokelatest = true # Set this to false to gain 0.1-1 ms, at the expense of world-age problems if w is created and used in the same function.
w(obj, use_invokelatest)MonteCarloMeasurements.ℂ2ℂ_function — Methodℂ2ℂ_function(f::Function, z::Complex{<:AbstractParticles})Helper function for uncertainty propagation through complex-valued functions of complex arguments. applies f : ℂ → ℂ to z::Complex{<:AbstractParticles}.
MonteCarloMeasurements.ℝⁿ2ℂⁿ_function — Methodℝⁿ2ℂⁿ_function(f::Function, p::AbstractArray{T})Helper function for performing uncertainty propagation through complex-valued functions with vector inputs. Applies f : ℝⁿ → Cⁿ to an array of particles. E.g., LinearAlgebra.eigvals(p::Matrix{<:AbstractParticles}) = ℝⁿ2ℂⁿ_function(eigvals,p)
MonteCarloMeasurements.ℝⁿ2ℂⁿ_function — Methodℝⁿ2ℂⁿ_function(f::Function, p::AbstractArray{T})Helper function for performing uncertainty propagation through complex-valued functions with vector inputs. Applies f : ℝⁿ → Cⁿ to an array of particles. E.g., LinearAlgebra.eigvals(p::Matrix{<:AbstractParticles}) = ℝⁿ2ℂⁿ_function(eigvals,p)
MonteCarloMeasurements.ℝⁿ2ℝⁿ_function — Methodℝⁿ2ℝⁿ_function(f::Function, p::AbstractArray{T})Helper function for performing uncertainty propagation through vector-valued functions with vector inputs. Applies f : ℝⁿ → ℝⁿ to an array of particles. E.g., Base.log(p::Matrix{<:AbstractParticles}) = ℝⁿ2ℝⁿ_function(log,p)
MonteCarloMeasurements.ℝⁿ2ℝⁿ_function — Methodℝⁿ2ℝⁿ_function(f::Function, p::AbstractArray{T})Helper function for performing uncertainty propagation through vector-valued functions with vector inputs. Applies f : ℝⁿ → ℝⁿ to an array of particles. E.g., Base.log(p::Matrix{<:AbstractParticles}) = ℝⁿ2ℝⁿ_function(log,p)
MonteCarloMeasurements.@bymap — Macro@bymap f(p, args...)Call f with particles or vectors of particles by using map. This can be utilized if registering f using register_primitive fails. See also Workspace if bymap fails.
MonteCarloMeasurements.@bypmap — Macro@bypmap f(p, args...)Call f with particles or vectors of particles by using parallel pmap. This can be utilized if registering f using register_primitive fails. See also Workspace if bymap fails.
MonteCarloMeasurements.@prob — Macro@prob a < bCalculate the probability that an event on any of the forms a < b, a > b, a <= b, a >= b occurs, where a and/or b are of type AbstractParticles.
MonteCarloMeasurements.@unsafe — Macro@unsafe expressionActivates unsafe comparisons for the provided expression only. The expression is surrounded by a try/catch block to robustly restore unsafe comparisons in case of exception.
Base.:≈ — Methodp1 ≈ p2Determine if two particles are not significantly different
Base.:≉ — Methodp1 ≉ p2Determine if two particles are significantly different
MonteCarloMeasurements.:⊗ — Function⊗(μ,σ) = outer_product(Normal.(μ,σ))See also outer_product, ±