@@ -14,17 +14,17 @@ handle_args(p) = handle_args(p.args...)
1414handle_args (args... ) = throw (ArgumentError (" The plot function should be called with the signature plotfun([x=1:length(y)], y::Vector{Particles}, [q=0.025])" ))
1515
1616function quantiles (y,q:: Number )
17- m = mean .(y)
17+ m = vec ( mean .(y) )
1818 q > 0.5 && (q = 1 - q)
19- lower = - (quantile .(y ,q)- m)
20- upper = quantile .(y ,1 - q)- m
19+ lower = reshape ( - (quantile .(vec (y) ,q)- m), size (y) )
20+ upper = reshape ( quantile .(vec (y) ,1 - q)- m, size (y))
2121 lower,upper
2222end
2323
2424function quantiles (y,q)
25- m = mean .(y)
26- lower = - (quantile .(y ,q[1 ])- m)
27- upper = quantile .(y ,q[2 ])- m
25+ m = vec ( mean .(y) )
26+ lower = reshape ( - (quantile .(vec (y) ,q[1 ])- m), size (y) )
27+ upper = reshape ( quantile .(vec (y) ,q[2 ])- m, size (y))
2828 lower,upper
2929end
3030
@@ -55,6 +55,8 @@ function to1series(x,y)
5555 x2,y2
5656end
5757
58+ to1series (y) = to1series (1 : size (y,1 ),y)
59+
5860@userplot MCplot
5961@recipe function plt (p:: MCplot )
6062 x,y,q = handle_args (p)
7577end
7678
7779@userplot Ribbonplot
78- @recipe function plt (p:: Ribbonplot )
80+ @recipe function plt (p:: Ribbonplot ; N = false )
7981 x,y,q = handle_args (p)
80- label --> " Mean with $q quantile"
81- m = mean .(y)
82- ribbon := quantiles (y, q)
83- x,m
82+ if N > 0
83+ for col = 1 : size (y,2 )
84+ yc = y[:,col]
85+ m = mean .(yc)
86+ @series begin
87+ label --> " Mean with $q quantile"
88+ ribbon := quantiles (yc, q)
89+ x,m
90+ end
91+ @series begin
92+ ribbon := quantiles (yc, q)
93+ m
94+ end
95+ @series begin
96+ M = Matrix (yc)
97+ np,ny = size (M)
98+ primary := false
99+ nc = N > 1 ? N : min (np, 50 )
100+ seriesalpha --> max (1 / sqrt (nc), 0.1 )
101+ chosen = randperm (np)[1 : nc]
102+ to1series (M[chosen, :]' )
103+ end
104+ end
105+ else
106+ @series begin
107+ label --> " Mean with $q quantile"
108+ m = mean .(y)
109+ ribbon := quantiles (y, q)
110+ x,m
111+ end
112+ end
84113end
85114
86115"""
@@ -99,31 +128,49 @@ Plots all trajectories represented by a vector of particles. `N > 1` controls th
99128mcplot
100129
101130"""
102- ribbonplot(x,y,[q=0.025])
131+ ribbonplot(x,y,[q=0.025]; N=true )
103132
104133Plots a vector of particles with a ribbon covering quantiles `q, 1-q`.
105134If `q::Tuple`, then you can specify both lower and upper quantile, e.g., `(0.01, 0.99)`.
135+
136+ If a positive number `N` is provided, `N` sample trajectories will be plotted on top of the ribbon.
106137"""
107138ribbonplot
108139
109140
110141
111- @recipe function plt (y:: Union{MvParticles,AbstractMatrix{<:AbstractParticles}} , q= 0.025 )
142+ @recipe function plt (y:: Union{MvParticles,AbstractMatrix{<:AbstractParticles}} , q= 0.025 ; N = true )
112143 label --> " Mean with ($q , $(1 - q) ) quantiles"
113- ribbon := quantiles (y, q)
114- mean .(y)
144+ if N > 0
145+ for col = 1 : size (y,2 )
146+ yc = y[:,col]
147+ @series begin
148+ ribbon := quantiles (yc, q)
149+ mean .(yc)
150+ end
151+ @series begin
152+ M = Matrix (yc)
153+ np,ny = size (M)
154+ primary := false
155+ nc = N > 1 ? N : min (np, 50 )
156+ seriesalpha --> max (1 / sqrt (nc), 0.1 )
157+ chosen = randperm (np)[1 : nc]
158+ M[chosen, :]'
159+ end
160+ end
161+ end
115162end
116163
117164
118- @recipe function plt (func:: Function , x:: MvParticles , q= 0.025 )
165+ @recipe function plt (func:: Function , x:: Union{ MvParticles,AbstractMatrix{<:AbstractParticles}} , q= 0.025 )
119166 y = func .(x)
120167 label --> " Mean with ($q , $(1 - q) ) quantiles"
121168 xerror := quantiles (x, q)
122169 yerror := quantiles (y, q)
123170 mean .(x), mean .(y)
124171end
125172
126- @recipe function plt (x:: MvParticles , y:: MvParticles , q= 0.025 ; points= false )
173+ @recipe function plt (x:: Union{ MvParticles,AbstractMatrix{<:AbstractParticles}} , y:: Union{ MvParticles,AbstractMatrix{<:AbstractParticles}} , q= 0.025 ; points= false )
127174 my = mean .(y)
128175 mx = mean .(x)
129176 if points
@@ -133,23 +180,50 @@ end
133180 seriesalpha --> 0.1
134181 Matrix (x), Matrix (y)
135182 end
183+ @series begin
184+ mx, my
185+ end
136186 else
137- yerror := quantiles (y, q)
138- xerror := quantiles (x, q)
139- label --> " Mean with $q quantile"
187+ @series begin
188+ yerror := quantiles (y, q)
189+ xerror := quantiles (x, q)
190+ label --> " Mean with $q quantile"
191+ mx, my
192+ end
140193 end
141- mx, my
142194end
143195
144- @recipe function plt (x:: MvParticles , y:: AbstractArray , q= 0.025 )
196+ @recipe function plt (x:: Union{ MvParticles,AbstractMatrix{<:AbstractParticles}} , y:: AbstractArray , q= 0.025 )
145197 mx = mean .(x)
146198 lower,upper = quantiles (x, q)
147199 xerror := (lower,upper)
148200 mx, y
149201end
150202
151- @recipe function plt (x:: AbstractArray , y:: MvParticles , q= 0.025 )
152- ribbon := quantiles (y, q)
153- label --> " Mean with ($q , $(1 - q) ) quantiles"
154- x, mean .(y)
203+ @recipe function plt (x:: AbstractArray , y:: Union{MvParticles,AbstractMatrix{<:AbstractParticles}} , q= 0.025 ; N= true )
204+ if N > 0
205+ for col = 1 : size (y,2 )
206+ yc = y[:,col]
207+ @series begin
208+ ribbon := quantiles (yc, q)
209+ label --> " Mean with ($q , $(1 - q) ) quantiles"
210+ x, mean .(yc)
211+ end
212+ @series begin
213+ M = Matrix (yc)
214+ np,ny = size (M)
215+ primary := false
216+ nc = N > 1 ? N : min (np, 50 )
217+ seriesalpha --> max (1 / sqrt (nc), 0.1 )
218+ chosen = randperm (np)[1 : nc]
219+ to1series (x, M[chosen, :]' )
220+ end
221+ end
222+ else
223+ @series begin
224+ ribbon := quantiles (y, q)
225+ label --> " Mean with ($q , $(1 - q) ) quantiles"
226+ x, mean .(y)
227+ end
228+ end
155229end
0 commit comments