# Computing the Expectation

The following solution to this problem was found by @Wolfies:

(E^(-((-2 z + μ1 + μ2)^2/(2 (σ1^2 + σ2^2)))) Sqrt[2/π] (-1 + Erf[((z - μ2) σ1^2 + (-z + μ1) σ2^2)/
(Sqrt σ1 σ2 Sqrt[σ1^2 + σ2^2])]))/((-1 + Erf[(μ1 - μ2)/(Sqrt Sqrt[σ1^2 + σ2^2])]) Sqrt[σ1^2 + σ2^2])


Note that $$Z = \frac{X + Y}{2}$$ where $$X$$ and $$Y$$ are independent random variables that both follow the Normal distribution, with possibly different means and variances (denoted by subscripts $$1$$ for $$X$$ and $$2$$ for $$Y$$).

I am now trying to compute the mean of the above, after first creating a probability distribution as follows:

\[ScriptCapitalD] = ProbabilityDistribution[((E^(-((-2 z + μ1 + μ2)^2/(2 (σ1^2 + σ2^2)))) Sqrt[2/π] (-1 + Erf[((z - μ2) σ1^2 + (-z + μ1) σ2^2)/(Sqrt σ1 σ2 Sqrt[σ1^2 + σ2^2])]))/((-1 + Erf[(μ1 - μ2)/(Sqrt Sqrt[σ1^2 + σ2^2])]) Sqrt[σ1^2 + σ2^2]),{z, -∞, ∞}]


and then,

Expectation[a, a \[Distributed] \[ScriptCapitalD]]


but Mathematica won't give me an answer. Is this beyond Mathematica? (I wonder if the mathStatica add-on will help)?

• Sometimes (and certainly no always) it helps to look at special cases to see if some general form becomes apparent. If $\sigma_1=\sigma_2=\sigma$, then the expectation is $\frac{\sigma (\text{$\mu $1}+\text{$\mu $2}) \text{erfc}\left(\frac{\text{$\mu $1}-\text{$\mu $2}}{2 \sigma }\right)}{\sqrt{2}-2 \sigma \text{erf}\left(\frac{\text{$\mu $1}-\text{$\mu $2}}{2 \sigma }\right)}$. I'm a big fan of mathStatica (bought a copy when it first came out maybe 18 years ago) and it might help. I was wrong about your previous question having an essentially closed-form solution but maybe... – JimB Apr 22 '19 at 19:26
• @JimB careful, the given probability distribution is not normalized. When $\sigma_1=\sigma_2$ the expectation value is $(\mu_1+\mu_2)/2$. – Roman Apr 22 '19 at 19:32
• @Roman. Uh, oh. I thought the previous question did give a normalized density. Thanks. I'll check on that. – JimB Apr 22 '19 at 19:35
• @Roman There's a missing set of parentheses in the OP's text. After adding those in the pdf integrates to 1 and one gets the expected value that you gave. – JimB Apr 22 '19 at 20:37

Evaluating the Integral for the conditional distribution is not required to find the conditional mean, as you can exploit the fact that you are using a linear transformation of a bivariate distribution, and the fact that the mean of a truncated normal has a nice closed form.

The idea is to decompose your transformation into two partial transformations, which, when applied in sequence, are equivalent to your full transformation.

We'll chose the first partial transformation so that the first marginal will be our difference, but the other marginal will be uncorrelated, and by joint normality, independent. This will allow us to truncate the first marginal and not effect the other.

We can then compute the mean vector after our first partial transformation and marginal truncation and then apply the second partial linear transformation to obtain the mean vector of the conditional distribution.

This technique actually allows you to find the mean and variance of the marginal distribution of a bivariate normal conditional on some truncation of the other marginal for any bivariate normal distribution. For bivariate normal distributions with small covariance, the conditional distribution is approximately normal, and you can use this technique to approximate the joint CDF for the bivariate normal, which has no nice closed form. For more information, see https://apps.dtic.mil/dtic/tr/fulltext/u2/a125033.pdf

In:= means = Array[μ, 2]

Out= {μ, μ}

In:= covar = DiagonalMatrix[Array[σ, 2]]

Out= {{σ, 0}, {0, σ}}

In:= trans = {{1, -1}, {1/2, 1/2}}

Out= {{1, -1}, {1/2, 1/2}}

In:= ptransa = {{1, -1}, {1, a}}

Out= {{1, -1}, {1, a}}

In:= acovar = (ptransa.covar.Transpose[ptransa])[[1, 2]]

Out= σ - a σ

In:= ptrans = ptransa /. Solve[acovar == 0, a][]

Out= {{1, -1}, {1, σ/σ}}

In:= ftrans = trans.Inverse[ptrans] // FullSimplify

Out= {{1,
0}, {-(1/2) + σ/(σ + σ), σ[
2]/(σ + σ)}}

In:= pmeans = ptrans.means

Out= {μ - μ, μ[
1] + (μ σ)/σ}

In:= pcovar = ptrans.covar.Transpose[ptrans] // FullSimplify

Out= {{σ + σ,
0}, {0, (σ (σ + σ))/σ}}

In:= dist =
TruncatedDistribution[{-Infinity, 0},
NormalDistribution[pmeans[], Sqrt[pcovar[[1, 1]]]]]

Out= TruncatedDistribution[{-∞, 0},
NormalDistribution[μ - μ,
Sqrt[σ + σ]]]

In:= tmeans = {Mean[dist], pmeans[]} // FullSimplify

Out= {μ - μ - (
E^(-((μ - μ)^2/(2 (σ + σ)))) Sqrt[
2/π] Sqrt[σ + σ])/
Erfc[(μ - μ)/(
Sqrt Sqrt[σ + σ])], μ[
1] + (μ σ)/σ}

In:= cmean = ftrans.tmeans // FullSimplify

Out= {μ - μ - (
E^(-((μ - μ)^2/(2 (σ + σ)))) Sqrt[
2/π] Sqrt[σ + σ])/
Erfc[(μ - μ)/(
Sqrt Sqrt[σ + σ])],
1/2 (μ + μ) + (
E^(-((μ - μ)^2/(
2 (σ + σ)))) (-σ + σ))/(
Sqrt[2 π]
Erfc[(μ - μ)/(
Sqrt Sqrt[σ + σ])] Sqrt[σ[
1] + σ])}

In:= cmean[]

Out= 1/2 (μ + μ) + (
E^(-((μ - μ)^2/(
2 (σ + σ)))) (-σ + σ))/(
Sqrt[2 π]
Erfc[(μ - μ)/(
Sqrt Sqrt[σ + σ])] Sqrt[σ[
1] + σ])


As @Roman pointed out the expectation is $$(\mu_1+\mu_2)/2$$ when $$\sigma_1=\sigma_2$$:

\[ScriptCapitalD] = ProbabilityDistribution[Exp[-(-2 z + μ1 + μ2)^2/(2 (σ1^2 + σ2^2))] *
Sqrt[2/π] (-1 + Erf[((z - μ2) σ1^2 + (-z + μ1) σ2^2)/(Sqrt σ1 σ2 Sqrt[σ1^2 + σ2^2])])/
(Sqrt[σ1^2 + σ2^2] (-1 + Erf[(μ1 - μ2)/(Sqrt Sqrt[σ1^2 + σ2^2])])),
{z, -∞, ∞}, Assumptions -> {σ1 > 0, σ2 > 0}];

Expectation[z, z \[Distributed] \[ScriptCapitalD] /. {σ1 -> σ, σ2 -> σ}]
(* (μ1 + μ2)/2 *)


But I think that when $$\sigma_1 \neq \sigma_2$$, then you'll need to use numerical methods for the expectation. (And hopefully I'm wrong about that.)

NExpectation[z, z \[Distributed] \[ScriptCapitalD] /. {μ1 -> 1, μ2 -> 3, σ1 -> 1, σ2 -> 2}]
(* 2.22026 *)