# Working with means and variances

My goal is to work out on the following equation:

$$Mean(P)(1-Mean (P))\left (h + (1 - 2 h) (1 - \frac {Var (P)} {Mean (P) (1 - Mean (P))}) (1 - Mean (P)) + \frac {Var (P)} {Mean (P) (1 - Mean (P))} (1 - h) \right)$$

where $P$ is a sample, $Mean(P)$ is the Mean value of the sample $P$: $Mean(P) = 1/n \sum_{i=1}^n p_i$. $Var(P)$ is the Variance of the random variable $P$.

How can I Simplify this kind of equation with Mathematica?

I thought I could eventually define a sequence of $p_i$ of undefined length and work with it. Is something similar feasible?

Here is the Mathematica code:

Mean[P](1-Mean[P])( h + (1-2h)(1-(Variance[P])/(Mean[P](1-Mean[P])))*
(1-Mean[P]) + (Variance[P])/(Mean[P](1-Mean[P])) (1-h))

-
@Öskå Thanks for your comment. As I don't really know how to treat this kind of calculation I don't really know what code to write. But I added edited my question. Hope that makes sense. I realize that when running Simplify[..] on my expression I get a result! Maybe that was all I wanted. But if I run Simplify[Mean[X^2]-Mean[X]^2] Mathematica does not answer Var[X]. Thanks for your help –  Remi.b May 17 '14 at 11:46
I am confused by several aspects of the question. First, you say $Exp[P]$ is the expected value of $P$, but you then define it as the sample mean (which is not the same thing). Second, your sample mean is defined as the sum from 0 to $n$ (i.e. $n+1$ values), but you divide by $n$, not $n+1$. Doesn't make sense. Third, you then define $Var[P]$ as the population variance, but do you actually want it to be that, or the sample variance? And if the latter, which definition of sample variance: $1/n$ or $1/(n-1)$ ? –  wolfies May 17 '14 at 11:47
@wolfies Thanks your comment helps. Yes, I actually talk about sample mean and variance not about statistical moments, you're correct. And moreover I made the mistake in defining it! I edited the question to fix this mistake. –  Remi.b May 17 '14 at 11:50
@Remi.b If you define P = Symbol["p" <> ToString@#] & /@ Range@10, what result would you expect? –  Öskå May 17 '14 at 12:21
@Remi.b If my answer suits your needs please accept it. –  olliepower May 19 '14 at 14:17

Using FullSimplify we obtain

fun0[P_,h_] := Mean[P]*(1 - Mean[P])*(h + (1 - 2 h) (1 - (Variance[P])/(Mean[P] (1 - Mean[P])))*
(1 - Mean[P]) + (Variance[P])/(Mean[P](1 - Mean[P])) (1 - h))
fun[P_,h_] := FullSimplify@fun0[P]
fun[P,h]

-(-1 + Mean[P]) Mean[P] (1 - h + (-1 + 2 h) Mean[P]) +
(h + Mean[P] - 2 h Mean[P]) Variance[P]


Declare variables to see if the simplified equation is the same

P = {1, 2, 3};
h = 1;
fun0[P,h]

-5


as does

fun[P,h]

-5

-
Thanks for the edit Oska –  olliepower May 17 '14 at 22:13