# Histogram from a set of values from EnsembleProblem

**URL:** https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891
**Category:** New to Julia
**Tags:** question
**Created:** [April 2, 2020, 8:37am UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891 "2020-04-02T08:37:39Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![Frederic\_Gilt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frederic_gilt/32/13807_2.png) [@Frederic\_Gilt](https://discourse.julialang.org/u/Frederic_Gilt)
#### Post date: [April 2, 2020, 8:37am UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/1 "2020-04-02T08:37:39Z")

</div>

Dear All,  
I have a system of six SDEs I solved with

```julia
prob_sde = SDEProblem(qdelta,qsigma,u0,(0.0,600.0),noise=W)
ensembleprob = EnsembleProblem(prob_sde,prob_func=prob_func)
sol = solve(ensembleprob,SRIW1(),EnsembleDistributed(),trajectories=50)
plotly()
plot(sol,vars=(0,6),linealpha=0.4)

```

which seems to work well, unfortunately I need the histogram of u[6], but I merely don’t know how to extract from ‘sol’ all the values from the 50 trajectories so that I can simply call the histogram ploting tool… I tried `convert(Array,sol)` which failed, then `v=hcat(sol.u[6,:])` which failed too…

sorry again for this naive question  
thank you !  
Frederic

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 2, 2020, 8:45am UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/2 "2020-04-02T08:45:28Z")

</div>

Each trajectory has a different number of points due to adaptivity. You either need to adjust your thinking about the timeseries to that or just make it save at even spots, like `saveat=1` so that each trajectory has the same saving spots.

---

<div class="post-metadata">

### Author: ![Frederic\_Gilt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frederic_gilt/32/13807_2.png) [@Frederic\_Gilt](https://discourse.julialang.org/u/Frederic_Gilt)
#### Post date: [April 6, 2020, 4:47am UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/3 "2020-04-06T04:47:20Z")

</div>

thank you for the help ! the `saveat` option will give me values at a given time for all trajectories, right ? I guess it is not adequate for my case.  
let suppose I need the histogram of only the nth trajectory, how do I extract the values then as `hcat` refuse to do so ?  
sorry for my basics questions !

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 6, 2020, 5:17am UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/4 "2020-04-06T05:17:09Z")

</div>

> [@Frederic\_Gilt](#):
>
> v=hcat(sol.u[6,:])

`v=reduce(hcat,[sol[i].u[6,:] for i in 1:length(sol)])`. Remember `sol` is an ensemble of trajectories, so you want to grab the timeseries of the 6th variable from each trajectory, and then hcat them all together (via `reduce(hcat,x)`).

---

<div class="post-metadata">

### Author: ![Frederic\_Gilt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frederic_gilt/32/13807_2.png) [@Frederic\_Gilt](https://discourse.julialang.org/u/Frederic_Gilt)
#### Post date: [April 7, 2020, 1:13am UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/5 "2020-04-07T01:13:17Z")

</div>

Thanks Chris ! the outuput of v is `1×50 Array{Array{Float64,1},2}` so when I just do `histogram(v[1,:])` result is … strange.

So I am suppose to have 50 timesseries of a lot of points (600seconds), but obviously the `reduce` reduced them all 🙂 .

what I don’t get is when I ask to `plot(sol[1],vars=(0,6))`, it works well

 ![Capture d’écran 2020-04-07 à 12.18.19](https://global.discourse-cdn.com/julialang/original/3X/b/b/bb6a664b5f8417073d47e832ed369ecdbdc636af.png)

I tried to read and understand the manual page on MultiDimensional arrays but is doesn’t help me much…

---

<div class="post-metadata">

### Author: ![Frederic\_Gilt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frederic_gilt/32/13807_2.png) [@Frederic\_Gilt](https://discourse.julialang.org/u/Frederic_Gilt)
#### Post date: [April 26, 2020, 12:59pm UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/6 "2020-04-26T12:59:09Z")

</div>

hello  
I’m coming back to this problem. As far as I understood, the `v=reduce(hcat,[sol[i].u[6,:] for i in 1:length(sol)])` output very low amount of data, so I think it is more `u[:,6]` which correspond to `plot(sol[1],vars=(0,6))`, but when I do so Julia does not agree :

> BoundsError: attempt to access 13796-element Array{Array{Float64,1},1} at index [Base.Slice(Base.OneTo(13796)), 6]

So how is it possible to extract, for the solution i, all the values of the 6th variable and use it in the GR `histogram` function ?

Sorry for such basic question but at that time I’m stuck !

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 26, 2020, 1:06pm UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/7 "2020-04-26T13:06:11Z")

</div>

`sol[1,6,:]`

---

<div class="post-metadata">

### Author: ![Frederic\_Gilt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frederic_gilt/32/13807_2.png) [@Frederic\_Gilt](https://discourse.julialang.org/u/Frederic_Gilt)
#### Post date: [April 26, 2020, 1:24pm UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/8 "2020-04-26T13:24:07Z")

</div>

thanks a lot ! in fact `sol[1,:,6]` in my case no ? else I have only 20 elements. then histogram works well.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 26, 2020, 2:20pm UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/9 "2020-04-26T14:20:45Z")

</div>

Oh, `sol[6,:,1]` my bad.

---

<div class="post-metadata">

### Author: ![Frederic\_Gilt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frederic_gilt/32/13807_2.png) [@Frederic\_Gilt](https://discourse.julialang.org/u/Frederic_Gilt)
#### Post date: [April 27, 2020, 1:56am UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/10 "2020-04-27T01:56:57Z")

</div>

then I am a bit lost again, what are the i,j,k in `sol[i,j,k]` ?  
initially I was hoping for  
i → trajectory  
j → time  
k → variable  
to comply with `sol[i],vars=(j,k)`

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 27, 2020, 2:51am UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/11 "2020-04-27T02:51:47Z")

</div>

No, it’s column order, it’s

i → variable  
j → time  
k → trajectory

s[i,j] is variable and time, and then s[i,j,k] is variable time trajectory. That’s the only thing that makes sense for column-ordering since trajectories are stored in memory together, so the 3rd axis is trajectory. Arrays in the language are made to be column order so if you orient it that way it’s clear.

---

<div class="post-metadata">

### Author: ![Frederic\_Gilt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frederic_gilt/32/13807_2.png) [@Frederic\_Gilt](https://discourse.julialang.org/u/Frederic_Gilt)
#### Post date: [April 27, 2020, 6:09am UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/12 "2020-04-27T06:09:31Z")

</div>

ok understood, anyway if I call `￼ ht = sol[6,:,15]` it output ` BoundsError: attempt to access 14223-element Array{Array{Float64,1},1} at index [14224]` I am supposed to have 20 trajectories,  
`sol = solve(ensembleprob,SRIW1(),EnsembleDistributed(),trajectories=20);`  
but only k=1 and 2 works… where am I wrong ?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 27, 2020, 9:26am UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/13 "2020-04-27T09:26:08Z")

</div>

> [@Frederic\_Gilt](#):
>
> sol = solve(ensembleprob,SRIW1(),EnsembleDistributed(),trajectories=20);

Try `sol = solve(ensembleprob,SRIW1(),EnsembleDistributed(),trajectories=20,saveat=0.1);` or something like that: the issue is raggedly-sized trajectories since adaptive timestepping allows each different trajectory to have different amounts of outputs (which is why `hcat`ing failed)

---

<div class="post-metadata">

### Author: ![Frederic\_Gilt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frederic_gilt/32/13807_2.png) [@Frederic\_Gilt](https://discourse.julialang.org/u/Frederic_Gilt)
#### Post date: [April 28, 2020, 12:03am UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/14 "2020-04-28T00:03:44Z")

</div>

In that case it works as every trajectory has the same size. Thanks a lot !

What I don’t get is why Julia doesn’t want to output one array even if its size is different from other arrays… why is it compared to others ? As it exists it can be extracted no ? worst process ever might be element by element and rebuilding an array that way …

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 28, 2020, 4:27pm UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/15 "2020-04-28T16:27:30Z")

</div>

It’s not Julia, it’s my definition of indexing on a `VectorOfArray`. All of the indexing stuff somewhat assumes `size(x)` makes sense, which it doesn’t when the outputs are ragged. It would be good to get an issue on this.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 28, 2020, 4:28pm UTC](https://discourse.julialang.org/t/histogram-from-a-set-of-values-from-ensembleproblem/36891/16 "2020-04-28T16:28:04Z")

</div>

Track it here: [Indexing ragged size EnsembleSolutions · Issue #597 · SciML/DifferentialEquations.jl · GitHub](https://github.com/SciML/DifferentialEquations.jl/issues/597)
