# The mean of 3-d EmsembleSolution from StochasticDelayDiffEq

**URL:** <https://discourse.julialang.org/t/the-mean-of-3-d-emsemblesolution-from-stochasticdelaydiffeq/67287>\
**Category:** New to Julia\
**Tags:** diffeq\
**Created:** [August 29, 2021, 3:38pm UTC](https://discourse.julialang.org/t/the-mean-of-3-d-emsemblesolution-from-stochasticdelaydiffeq/67287 "2021-08-29T15:38:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Liwei17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liwei17/32/28423_2.png) [@Liwei17](https://discourse.julialang.org/u/Liwei17)\
**Post date:** [August 29, 2021, 3:38pm UTC](https://discourse.julialang.org/t/the-mean-of-3-d-emsemblesolution-from-stochasticdelaydiffeq/67287/1 "2021-08-29T15:38:09Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 29, 2021, 3:42pm UTC](https://discourse.julialang.org/t/the-mean-of-3-d-emsemblesolution-from-stochasticdelaydiffeq/67287/2 "2021-08-29T15:42:32Z")

</div>

I can’t reproduce your problem. If I make a 3d array with `sol = rand(2,4412,1000)`, then `mean(sol, dim=2)` gives a `MethodError` because the keyword is spelled `dims`, not `dim`. If I use the correct spelling, it works fine:

```julia
julia> using Statistics

julia> sol = rand(2,4412,1000);

julia> mean(sol, dims=3)
2×4412×1 Array{Float64, 3}:
[:, :, 1] =
 0.488939 0.483749 0.499478 0.502813 … 0.494693 0.50745 0.482576
 0.522986 0.515288 0.510555 0.492776 0.486172 0.493854 0.508768

```

Are you using some other implementation of `mean` than [the one in the `Statistics` standard library](https://docs.julialang.org/en/v1/stdlib/Statistics/#Statistics.mean)?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 29, 2021, 3:45pm UTC](https://discourse.julialang.org/t/the-mean-of-3-d-emsemblesolution-from-stochasticdelaydiffeq/67287/4 "2021-08-29T15:45:51Z")

</div>

General comments:

1. Don’t post screenshots, [post quoted code](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530).

2. Post a complete reproducible example, like the code I posted. See [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

**Author:** ![Liwei17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liwei17/32/28423_2.png) [@Liwei17](https://discourse.julialang.org/u/Liwei17)\
**Post date:** [August 29, 2021, 3:46pm UTC](https://discourse.julialang.org/t/the-mean-of-3-d-emsemblesolution-from-stochasticdelaydiffeq/67287/5 "2021-08-29T15:46:00Z")

</div>

Yes I’m using the mean in the Statistics Library.

I changed the “dim” to “dims” and the result still like this…  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/1/11047922ed0589cb4cfd254478e0cd922a89a583.png)

---

<div class="post-metadata">

**Author:** ![Liwei17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liwei17/32/28423_2.png) [@Liwei17](https://discourse.julialang.org/u/Liwei17)\
**Post date:** [August 29, 2021, 3:47pm UTC](https://discourse.julialang.org/t/the-mean-of-3-d-emsemblesolution-from-stochasticdelaydiffeq/67287/6 "2021-08-29T15:47:59Z")

</div>

```julia
# using Pkg
# Pkg.add("DifferentialEquations")
# Pkg.add("StochasticDelayDiffEq")

using DifferentialEquations, StochasticDelayDiffEq, Plots, Statistics

function f(du, u, h, p, t)
    M_A, M_B = u;
    tau, mu, n, P0, b, sigma_A, sigma_B = p;
    hist_A = h(p, t-tau)[1];
    hist_B = h(p, t-tau)[2];
    du[1] = -mu*M_A + (1+b)*(1/(1+(hist_B/P0)^n));
    du[2] = -mu*M_B + (1/(1+(hist_A/P0)^n));
end

function g(du, u, h, p, t)
    tau, mu, n, P0, b, sigma_A, sigma_B = p;
    du[1] = sigma_A;
    du[2] = sigma_B;
end

h(p, t) = zeros(2);

tau = 100; lag = [tau];
P0 = 0.1; n = 5;
mu = 0.03; b = 0.01;
sigma_A = 0.003; sigma_B = 0.003; # only sigma < 0.07 show realiable staeady states
p = (tau, mu, n, P0, b, sigma_A, sigma_B);
tspan = (0, 50000);
u0 = [0, 0];

prob = SDDEProblem(f, g, u0, h, tspan, p; constant_lags = lag);

# single trajectory
# sol = solve(prob, RKMil());
# plot(sol)

# small multiple trajectories

# multiple trajectories
ensembleprob = EnsembleProblem(prob);
sol = solve(ensembleprob, RKMil(), EnsembleThreads(), trajectories = 1000);
# summ = EnsembleSummary(sol, 0:1:50000);
# plot(summ)

m_sol = mean(sol ,dims = 3)

# plot(sol[1, :, 1], sol[2, :, 1], xlabel = "mRNA in cell A", ylabel = "mRNA in cell B")

```

Sorry for posting screen shot.  
This is the code

And also the “sol” is not a matrix. It’s an EnsembleSolution.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/a/fa804fc2cc893d384a539d458ee77e92c7ef00d7.png)

Is it because its a EnsembleSolution so I can’t use “mean”?

Thank you

---

<div class="post-metadata">

**Author:** ![Liwei17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liwei17/32/28423_2.png) [@Liwei17](https://discourse.julialang.org/u/Liwei17)\
**Post date:** [August 29, 2021, 4:05pm UTC](https://discourse.julialang.org/t/the-mean-of-3-d-emsemblesolution-from-stochasticdelaydiffeq/67287/7 "2021-08-29T16:05:14Z")

</div>

typeof(sol) is an EmsembleSolution.

I wanted to plot the average trajectory. I should use EnsembleSummary(sol) to get its average trajectories then plot(…, :).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 29, 2021, 9:23pm UTC](https://discourse.julialang.org/t/the-mean-of-3-d-emsemblesolution-from-stochasticdelaydiffeq/67287/8 "2021-08-29T21:23:07Z")

</div>

> [@Liwei17](#):
>
> I wanted to plot the average trajectory.

See [How to sum outputs from DifferentialEquations.jl](https://discourse.julialang.org/t/how-to-sum-outputs-from-differentialequations-jl/67034)
