# \[ANN\] MonteCarloMeasurements.jl

**URL:** <https://discourse.julialang.org/t/ann-montecarlomeasurements-jl/23021>\
**Category:** Package Announcements\
**Tags:** announcement, monte-carlo, measurements\
**Created:** [April 11, 2019, 7:21am UTC](https://discourse.julialang.org/t/ann-montecarlomeasurements-jl/23021 "2019-04-11T07:21:14Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [April 24, 2019, 12:17pm UTC](https://discourse.julialang.org/t/ann-montecarlomeasurements-jl/23021/19 "2019-04-24T12:17:05Z")

</div>

# Updates in v0.1.4

I implemented the weighted particles with [resampling functionality](https://github.com/baggepinnen/MonteCarloMeasurements.jl/blob/master/src/resampling.jl) (using [numerically stable logsumexp](https://discourse.julialang.org/t/fast-logsumexp/22827/7)).  
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.

```julia
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:

```julia
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.

---

_[View the full topic](https://discourse.julialang.org/t/ann-montecarlomeasurements-jl/23021)._
