# Calculate the Variance

This is what the question that I'm having trouble with:

I know how to calculate the variance in general but I'm not sure how they got this:

-

By definition of the variance, you have that $$\mathrm{Var}(X_i)=E\left[(X_i-E[X_i])^2\right]=E\left[(X_i-p)^2\right].$$ Let us call $Y_i=(X_i-p)^2$. Then $Y_i$ is a random variable given by $$Y_i= \begin{cases} (1-p)^2\quad&\text{when }X_i=1,\\ p^2&\text{when }X_i=0, \end{cases}$$ from which we see that $P(Y_i=(1-p)^2)=P(X_i=1)=p$ and $P(Y_i=p^2)=P(X_i=0)=1-p$, and thus its expectation is $$E[Y_i]=(1-p)^2\times p+p^2\times (1-p)=p-p^2.$$

-
Why is it the other way round from the question though? I.e. P{X_i=0]=p and P{X_i=1}=p –  Mathlete Jan 22 '13 at 10:45
What? The other way around how? In both your question and this answer we have: $P(X_i=0)=1-p$ and $P(X_i=1)=p$. –  Stefan Hansen Jan 22 '13 at 10:52
Sorry, my mistake - I misread your line of working. Thanks for clarifying. –  Mathlete Jan 22 '13 at 10:55

we know var($X_{i}$)=$E[\,(X_{i}-E(X_{i}))^{2} ]=E[\,(X_{i}-p))^{2} ]$ (because $E(X_{i})=P\{X_{i}=0\}*0+P\{X_{i}=1\}*1=p$), a more useful formula is $var(X_{i})=E(X_{i}^{2})-E(X_{i})^{2}$ which can be obtained by expanding var($X_{i}$)=$E[\,(X_{i}-E(X_{i}))^{2} ]$

-
OK but why would you multiply by (1-p)^2 and p^2? –  Mathlete Jan 22 '13 at 9:30

You have that $Var(X_i)=E((X_i-E(X_i))^2)=E(X_i^2-2X_iE(X_i)+(E(X_i))^2)=E(X_i^2)-(E(X_i))^2.$ Now $X_i^2$ is a RV that equals $1^2=1$ with probability $p$ and $0^2=0$ with probability $1-p$. As such, its expected value is $1\cdot p + 0\cdot (1-p) = p$. On the other hand, since you $E(X_i)=p$, you get that $(E(X_i))^2=p^2$. Combining these two results gives you that $Var(X_i)=E(X_i^2)-(E(X_i))^2=p-p^2$.

-
I got that answer too doing it that way - I was just wondering how they went about their method? –  Mathlete Jan 22 '13 at 10:32