baggepinnen / MonteCarloMeasurements.jl

Propagation of distributions by Monte-Carlo sampling: Real number types with uncertainty represented by samples.
https://baggepinnen.github.io/MonteCarloMeasurements.jl/stable/
MIT License
264 stars 17 forks source link

Why MonteCarlo and why Particles? #8

Closed carstenbauer closed 5 years ago

carstenbauer commented 5 years ago

Hi,

first of all, great package!

I'm curious why you chose the name MonteCarloMeasurements.jl for this package. Could you elaborate the Monte Carlo context that you had in mind (apart from Monte Carlo generically representing randomness)? Typically, say in a MCMC scenario, I do not know the distribution function of an observable in advance but would get individual, correlated "Particles" from a simulation one after another. AFAICS, I can't use this package in such a case as there

Is this assessment correct?

On a side note, I was confused by the name Particles at first (maybe that's because I'm a physicist). It seems to expose the technical detail that the uncertainty is represented by a "data cloud" to the user. Personally I find Measurements.jl's choice measurement a bit more natural. Note that the related python package seems to be called uncertainties, a general name as well.

EDIT: FYI, my package BinningAnalysis.jl provides tools to estimate the standard error of the mean of correlated data.

baggepinnen commented 5 years ago

The name Monte Carlo refers to

Monte Carlo methods, or Monte Carlo experiments, are a broad class of computational algorithms that rely on repeated random sampling to obtain numerical results. https://en.wikipedia.org/wiki/Monte_Carlo_method This is exactly what we do here, sample from the distribution of the input to estimate the distribution of the output.

The name particles comes from particle filter https://en.wikipedia.org/wiki/Particle_filter which is a class of sequential Monte Carlo methods for nonlinear filtering, which is my background. See also this table https://github.com/baggepinnen/MonteCarloMeasurements.jl#notes

Why would you add particles one by one?

We do respect correlation in the data, the code you linked to is for a one-dimensional quantity that only has a univariate distribution. To handle correlation for multivariate particles, you must either construct multivariate particles from a constructor, or manually generate the correlated data. See for instance here https://github.com/baggepinnen/MonteCarloMeasurements.jl/blob/3548f772e17e4e818dc960208094015147de1adc/src/particles.jl#L297 This demonstrates the respected correlation

julia> using MonteCarloMeasurements

julia> A = randn(2,2)
2×2 Array{Float64,2}:
 -1.68834   -0.419691
  0.352097  -1.37353

julia> p = [Particles(500), Particles(500)] ;

julia> cov(A*p)
2×2 Array{Float64,2}:
  2.9411    -0.153216
 -0.153216   2.06976

julia> A*A' # this is the expected covariance if the covariance of p is I
2×2 Array{Float64,2}:
  3.02664    -0.0180007
 -0.0180007   2.01056

I have nothing against exposing the implementation by using the name Particles, especially since this is so integral to understanding how the package works. In fact I believe competent use of research software benefits from some level of understanding of the tools in use. In this case, the user needs to make an informed choice on the number of particles to use to reach a desired level of confidence in the result etc.

The name Measurement however is more narrow than I would like, as uncertainty can arise from other sources than from measurements, such as model uncertainty etc. The name was somewhat established in the Julia ecosystem already so that's why I chose that for the repository.

baggepinnen commented 5 years ago

Maybe the most suitable name would be MonteCarloUncertaintyPropagation.jl 🤔

baggepinnen commented 5 years ago

Btw, MCMC methods solve a much harder problem. They are trying to infer the distribution of some unobserved parameter that influences the generation of observed variables given some model. Here, We are simply propagating distributions through functions, or inferring distributions of outputs given inputs with known distributions, a much simpler problem. If you think of the problem as a graph, we are only calculating output nodes. In MCMC, we are trying to go backwards and infer something about nodes that lead to the output.

baggepinnen commented 5 years ago

@crstnbr I added some additional explanation for how the package works in the beginning of the readme and also clarified how covariance between quantities are handled in the section about multivariate particles.

If you feel that this extra documentation makes you less confused, feel free to close this issue. If you feel that there's more room for improvement, please indicate what I have failed to explain :smile:

carstenbauer commented 5 years ago

Thanks for your answers! I think the extra documentation is great and I'll close this issue.

As I feel my inital post was very confusing about what I ment by correlations, let me clarify this point. I wasn't talking about correlations in the sense of multivariate distributions but correlated samples of a, for example, univariate distribution as one would get from MCMC. The particles would represent the distribution but would be statistically correlated. Hence the formula I linked isn't valid as it assumes statistically independent particles.

However, I see that this is not what you are trying to address with this package. It is therefore perfectly fine to leave it to other packages to provide algorithms to construct effectively independent samples from a correlated set of samples.

Thanks again and keep up the good work!