Plot with ribbon in MonteCarloMeasurements.jl

Given a function y = a * x + b where a and b are of the type Particles. By using MonteCarloMeasurements.jl and Plots.jl, I can plot many sample function y(x) as follows

using MonteCarloMeasurements
using Plots

p = Particles(1000, MvNormal([0,0],[2. 1; 1 4]))
p2mat = Matrix(p)
f(x,a,b) = a*x + b
fig = plot(legend=false)
[plot!(x->f(x,p2mat[i,1],p2mat[i,2])) for i = 1:30]
fig

Please tell me how can I plot y(x) with ribbon with (0.05;0.95) quantile?

Thank you in advance.

If you plot vectors of particles instead of using the function interface to plot, you should get ribbons automatically.

2 Likes

Thank you @baggepinnen. Following your suggestion [if I understand correctly], I already tried

using MonteCarloMeasurements
using Plots
p = Particles(1000, MvNormal([0,0],[2. 1; 1 4]))
a = p[1]
b = p[2]
f(x) = a*x + b
plot(x->f(x))

It returns to the following error
ERROR: MethodError: no method matching Float64(::Particles{Float64, 1000})

I also tried with

x = LinRange(1:0.1:3)
ribbonplot(x,f(x))

but still failed with error
ERROR: MethodError: no method matching +(::Vector{Particles{Float64, 1000}}, ::Particles{Float64, 1000})

Could you please be more specific to show me your solution?

Thank you for your help.

You must broadcast the function f over x since x is a vector and f only works for scalar x.

Try this

x = LinRange(1:0.1:3)
plot(x,f.(x))
1 Like

Great, thank you for your help.