# Sum of Random Variables — An Example with R [closed]

So I was trying to simulate these formulas from my lecture with R. $$E[\bar{X_n}] = \mu_x\text{, where X_i \sim i.i.d}\\Var(\bar{X_n}) = \frac{1}{n}*\sigma^2_x$$

My code in R:

set.seed(42)
a <- rnorm(100000, mean=10, sd=2)
b <- rnorm(100000, mean=10, sd=2)
c <- rnorm(100000, mean=10, sd=2)
d <- rnorm(100000, mean=10, sd=2)

mean((a+b+c+d)) # 39.99

var((a+b+c+d)) # 15.95


My question is. Why do I not get the result of the formulas above($$\mu_{sum} = 10, \sigma^2_{sum}=1$$)?

Possibility: Something is missing about my understanding of what I am doing here with the random number generation, which I though of as generating RV's. The question is what?

• $\bar{X}$ usually signifies the mean but here you're taking the sum. What do you mean? For what purpose are you making 4 different vectors with $n$ elements, instead of 1 vector with $4n$ elements? Also, you're using sd=2 but you say you expect to have sd=1. Which is it?
– Sycorax
Mar 3, 2019 at 20:24
• My task says $X_i$ are i.i.d distributed RV's and then formula above as a fact. I thought this is what they wanted me to do. Why would the want to show $E[i] = i$, where i is any number. Of course the expectation of a number is the number itself... What did they actually want to show? Regarding the variance: Well, sd=2 means var = 4 thus the formulal above says 4/4=1... no? I feel so stupid, because I seem to not get this at all, even though this is after I have acceptably well passed stats101 last semester... Mar 3, 2019 at 20:27
• a to d each contain 100000 realistations of $X_1,X_2,\dots,X_4$. Using vectorization in R, (a+b+c+d)/4 is going to contain 100000 corresponding values of $\bar X$. Then you take the mean and var of that to get the an estimate of the theoretical mean and variance of $\bar X$. Mar 3, 2019 at 20:35
• @JarleTufto Ah. Thank you! That does fix the problem. So yes. As Sycorax suggested, I was not taking a mean, which was the problem. Mar 3, 2019 at 20:38
• @thebilly: See stats.meta.stackexchange.com/questions/4378/… Mar 4, 2019 at 21:44