# Question on behavior with comparisons in MonteCarloMeasurements

**URL:** <https://discourse.julialang.org/t/question-on-behavior-with-comparisons-in-montecarlomeasurements/33679>\
**Category:** Modelling & Simulations\
**Created:** [January 22, 2020, 11:52pm UTC](https://discourse.julialang.org/t/question-on-behavior-with-comparisons-in-montecarlomeasurements/33679 "2020-01-22T23:52:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [January 22, 2020, 11:52pm UTC](https://discourse.julialang.org/t/question-on-behavior-with-comparisons-in-montecarlomeasurements/33679/1 "2020-01-22T23:52:21Z")

</div>

I’m playing with MonteCarloMeasurements but running up against limitations around comparisons.

```julia
julia> a = Particles(1000, Uniform(0.0,10.0))
Part1000(5.0 ± 2.89)

julia> b = Particles(1000, Uniform(0.0, 10.0))
Part1000(5.0 ± 2.89)

julia> a>b
ERROR: Comparison operators are not well defined for uncertain values and are currently turned off. Call `unsafe_comparisons(true)` to enable comparison operators for particles using the current reduction function mean. Change this function using `set_comparison_function(f)`.
Stacktrace:
 [1] _comparison_operator at C:\Users\klaffedk\.julia\packages\MonteCarloMeasurements\7ez8R\src\particles.jl:333 [inlined]
 [2] <(::Particles{Float64,1000}, ::Particles{Float64,1000}) at C:\Users\klaffedk\.julia\packages\MonteCarloMeasurements\7ez8R\src\particles.jl:345
 [3] >(::Particles{Float64,1000}, ::Particles{Float64,1000}) at .\operators.jl:294
 [4] top-level scope at none:0

julia> min(a,b)
Part1000(3.282 ± 2.33)

```

Why does `min(a,b)` seem to work? Isn’t it ultimately doing comparisons?

I’m trying to figure out if MonteCarloMeasurements.jl can be used for a real world problem which includes saturation effects.

[Edit] P.S. I was able to rewrite my actual function with min and max and move forward, but I would still like to understand why.

---

<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:** [January 23, 2020, 7:06am UTC](https://discourse.julialang.org/t/question-on-behavior-with-comparisons-in-montecarlomeasurements/33679/2 "2020-01-23T07:06:27Z")

</div>

The reason comparisons are not defined by default is that it’s not clear how you compare two probability distributions. When is one smaller than the other? I outline the limitations in [this section of the docs](https://baggepinnen.github.io/MonteCarloMeasurements.jl/latest/#Limitations-1)

You can use the function `unsafe_comparisons`

```julia
help?> unsafe_comparisons
search: unsafe_comparisons

  unsafe_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)

```

to compare particles using their mean or median etc., but this is not always a reasonable way of comparing distributions, why it is disabled by default. You may even define your own comparison in a way customized to your application, e.g., `quantile(a,0.9) < quantile(b,0.1)` etc. by calling `set_comparison_function`.

`min` works because it is defined as a primitive function through which each particle propagates one by one.

If you want `<,>,<=, >=` to behave like `min`, i.e., operate elementwise, you can run the following

```julia
foreach(register_primitive, [<=, >=, <, >])

```

Note that if `a < b` appears in a boolean context, e.g., `if a < b` you will get an error message.

See also this section of the docs on [overloading new functions](https://baggepinnen.github.io/MonteCarloMeasurements.jl/latest/overloading/) where a few different alternatives are outlined to make your custom “difficult” function work with particles.

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [January 23, 2020, 3:33pm UTC](https://discourse.julialang.org/t/question-on-behavior-with-comparisons-in-montecarlomeasurements/33679/3 "2020-01-23T15:33:57Z")

</div>

Thank you! I just read your new paper (very nice!) and am coming up to speed on what’s happening under the covers.

---

<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:** [January 23, 2020, 4:32pm UTC](https://discourse.julialang.org/t/question-on-behavior-with-comparisons-in-montecarlomeasurements/33679/4 "2020-01-23T16:32:42Z")

</div>

Thanks 🙂 drop a line here or the issue tracker if the documentation is lacking somewhere!

---

<div class="post-metadata">

**Author:** ![francesco.alemanno](https://avatars.discourse-cdn.com/v4/letter/f/e8c25b/32.png) [@francesco.alemanno](https://discourse.julialang.org/u/francesco.alemanno)\
**Post date:** [January 23, 2020, 10:22pm UTC](https://discourse.julialang.org/t/question-on-behavior-with-comparisons-in-montecarlomeasurements/33679/5 "2020-01-23T22:22:07Z")

</div>

you can compare them (usefully) using fuzzy comparison:  
given two continuous random variables A and B

A \> B \overset{\cdot}{=} \left\langle \frac{\rm{sign}(a-b)+1}{2} \right\rangle\_{a\sim A,\,b\sim B} \in [0,1]\_{\mathbb{R}}

in actual code

```julia
using Statistics: mean

A=3.2 .+ randn(1000) .* 0.14
B=3.3 .+ randn(1000) .* 0.1

function fuzzyge(A,B)
    mean(@. (sign(A-B)+1)/2) #not exactly the formula on the top, in this case it will work the same
end
@show fuzzyge(A,B)

```

```
fuzzyge(A, B) = 0.276

```

you can also return a binomial with p=...

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [January 24, 2020, 12:44am UTC](https://discourse.julialang.org/t/question-on-behavior-with-comparisons-in-montecarlomeasurements/33679/6 "2020-01-24T00:44:58Z")

</div>

The easiest thing to do would just be to return a particle of Bools, but the problem is that comparison is usually used within if statements, while loops or short-circuit logic operators, which will only accept a single Bool. The only way to make this work would be to do some source-to-source rewriting, like what Zygote does, but this would require a complete rewrite and make the API less convenient. You can still work around this with `@bymap` for functions that won’t work, but you’ll loose a lot of the performance advantages from betrer vectorizarion.

---

<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:** [January 24, 2020, 7:12am UTC](https://discourse.julialang.org/t/question-on-behavior-with-comparisons-in-montecarlomeasurements/33679/7 "2020-01-24T07:12:01Z")

</div>

As Simeon commented, comparing two distributions can be done in a lot of ways, but unless the comparison produces a `Bool`, it will fail for control flow. I opted for having them undefined by default, forcing the user to think about how they want the comparison to be done, rather than defaulting to some kind of comparison that turns out to be invalid for the user’s use case.

The source-rewriting strategy is indeed an interesting idea and I created a branch to experiment with this a couple of months ago. It turned out to be over my head at the time and I chose to wait until a more general solution appeared in ecosystem. Hydra.jl promises to be such a general solution, but it is not really mature yet. I had a discussion with Mike about it [here](https://github.com/FluxML/Hydra.jl/issues/13), but it is so far an open issue.

---

<div class="post-metadata">

**Author:** ![francesco.alemanno](https://avatars.discourse-cdn.com/v4/letter/f/e8c25b/32.png) [@francesco.alemanno](https://discourse.julialang.org/u/francesco.alemanno)\
**Post date:** [January 24, 2020, 8:54am UTC](https://discourse.julialang.org/t/question-on-behavior-with-comparisons-in-montecarlomeasurements/33679/8 "2020-01-24T08:54:35Z")

</div>

> [@baggepinnen](#):
>
> source-rewriting strategy is indeed an interesting idea and I created a branch to experiment with this a couple of months ago. It turned out to be over my head at the time an

Your package is very nice, and i plan on using it 😃 , indeed it would not help to define a behavior for comparisons that restricts the use case of the package.

Very nice work
