# Variance of a sum of a random number of iid random variables [duplicate]

I tried to get an expression for the variance of a sum of a random number of iid random variables. My question is whether it is correct and, if not, what is wrong or what additional assumptions might be missing.

Specifically, let:

$$S=\sum_1^N{X_i},$$ where $$N$$ is a non-negative integer-valued random variable.

Suppose that the distributions of both $$N$$ and $$X$$ are known (and $$X_i$$ are iid), I want to know the value of the variance of $$S$$.

Is the following solution correct?

$$V(S)=V(X)E(N)+E(X)^2V(N)$$

Use the definition of variance $$\sigma^{2} = \frac{1}{n-1}\sum_{i=1}^{n}(x_{i}-\bar{x})^{2}$$. For an R.V. $$X$$, we have $$\mathbb{V}(X)=\mathbb{E}[(X - \mathbb{E}[X])^{2}]$$.

From the Law of total variance we have the decomposition: $$\text{Var}(S) = \mathbb{E}[\text{Var}(S \mid N)] + \text{Var}[\mathbb{E}(S \mid N)]$$ Given $$N$$, $$S$$ is simply a sum of $$N$$ iid random variables so, $$\text{Var}(S \mid N) = N \times \text{Var}(X) \\ \mathbb{E}(S \mid N) = N \times \mathbb{E}(X)$$ The two quantities above are random variables but only as functions of $$N$$. Thus, since $$\text{Var(X)}$$ and $$\mathbb{E}(X)$$ are constants, we have:

$$\mathbb{E}[\text{Var}(S \mid N)] = \mathbb{E}(N) \text{Var}(X)$$ and $$\text{Var}[\mathbb{E}(S \mid N)]=\mathbb{E}(X)^2 \text{Var}(N)$$ Finally, $$\text{Var}(S) = \mathbb{E}(N) \text{Var}(X) + \mathbb{E}(X)^2 \text{Var}(N)$$

So yes, your solution is correct.

Here is some piece of R code where $$N$$ is Poisson distributed and the $$X_i$$ are $$\mathcal{N}(4,1)$$:

s<-sapply(1:10000,function(i){
N<-rpois(1,20)
S<-sum(rnorm(N,4,1))
return(S)
})
var(s) #should be close to 340
20*1 + 16*20