[ANN] MonteCarloMeasurements.jl

Updates in v0.1.4

I implemented the weighted particles with resampling functionality (using numerically stable logsumexp).
I also added two macros, @bymap, @bypmap, detailed below.

Weighted particles

The type WeightedParticles contains an additional field logweights. You may modify this field as you see fit, e.g.

reweight(p,y) = (p.logweights .+= logpdf.(Normal(0,1), y .- p.particles))

where y would be some measurement. After this you can resample the particles using resample!(p). This performs a systematic resample with replacement, where each particle is sampled proportionally to exp.(logweights).

Monte-Carlo simulation by map/pmap

Some functions will not work when the input arguments are of type Particles. For this kind of function, we provide a fallback onto a traditional map(f,p.particles). The only thing you need to do is to decorate the function call with the macro @bymap like so:

f(x) = 3x^2
p = 1 ± 0.1
r = @bymap f(p)

We further provide the macro @bypmap which does exactly the same thing, but with a pmap (parallel map) instead, allowing you to run several invocations of f in a distributed fashion.

These macros will map the function f over each element of p::Particles{T,N}, such that f is only called with arguments of type T, e.g., Float64. This handles arguments that are multivaiate particles <: Vector{<:AbstractParticles} as well.

These macros will typically be slower than calling f(p), but allow for Monte-Carlo simulation of things like calls to Optim.optimize etc., which fail if called like optimize(f,p). If f is very expensive, @bypmap might prove prove faster than calling f with p, it’s worth a try. The usual caveats for distributed computing applies, all code must be loaded on all workers etc.

4 Likes