# How to find de Mean and Variance of a defined distribution

I would like to find the Mean and Variance expressions for a Poisson-Lognormal Distribution

$$f(x;\mu,\sigma)=\frac{1}{x!\sigma\sqrt{2\pi}}\int_{0}^{\infty}\lambda^{x-1} e^{-\lambda} e^{\frac{(log(\lambda-\mu)^2}{2\sigma^2} }\text{d}\lambda$$

I have been checking the Expectation[] command to find the expected value of predefined Mathematica pdfs but I do not know how to apply to this expression, and to find the $$Var(X)=E[X^2]-E[X]^2$$.

• What didn't work with Expectation? Please show what you tried. Your LaTeX display seems to be in error. $(\log(\lambda-\mu)^2$ should probably be $-(\log(\lambda)-\mu)^2$. – JimB Sep 15 '19 at 22:28

I suspect that you will need to use numeric techniques.

Clear["Global*"]

f[x_Integer?NonNegative,
μ_?(NumericQ[#] && Element[#, Reals] &),
σ_?(NumericQ[#] && # > 0 &)] := NIntegrate[
E^-λ λ^(x -
1) E^(-(Log[λ] - μ)^2/(2 σ^2)),
{λ, 0, ∞}]/(Sqrt[2 π] σ x!)

mean[μ_?(NumericQ[#] && Element[#, Reals] &),
σ_?(NumericQ[#] && # > 0 &)] := mean[μ, σ] =
NSum[x*f[x, μ, σ], {x, 0, ∞}, NSumTerms -> 50,
WorkingPrecision -> $MachinePrecision] var[μ_?(NumericQ[#] && Element[#, Reals] &), σ_?(NumericQ[#] && # > 0 &)] := var[μ, σ] = Module[{m = mean[μ, σ]}, NSum[(x - m)^2*f[x, μ, σ], {x, 0, ∞}, NSumTerms -> 50, WorkingPrecision ->$MachinePrecision]]

mean[0, 1]

(* 1.6487197 *)

var[0, 1]

(* 6.318 *)

• @BobHanion Thank you. Both mean[0,1] and var[0,1] gives me ComplexInfinity as a result. – pablo Sep 17 '19 at 8:59
• I just pasted the code above into a notebook and executed it with the same result as shown above. I am running v12.0.0 on a Mac. Which version are you using? – Bob Hanlon Sep 17 '19 at 13:47
• @BobHanion I am running the 11.0.0 installed on a Mac too. That could be the issue. – pablo Sep 17 '19 at 15:34
• On my Mac, code works with v11.3. The problem is present with v11.2. Perhaps you can upgrade to v11.3 – Bob Hanlon Sep 17 '19 at 16:42
• @BonHanion I have downloades a 12.0 trial versioin and works fine. I suppose I should upgrade soon. I would ask you how I could get the \sigma such that mean[2.32, \sigma]==23?. – pablo Sep 17 '19 at 17:16