Sum of a random number of r.v.'s A fair coin is flipped independently until the first Heads is observed. Let the random variable  K  be the number of tosses until the first Heads is observed plus 1. For example, if we see TTTHTH, then K=5. For  k=1,2,…,K , let  $X_k$   be a continuous random variable that is uniform over the interval  [0,5]. The  $X_k$  are independent of one another and of the coin flips.
Let 
X=$\sum_{k=1}^k X_k$
 . Find the mean and variance of  X . You may use the fact that the mean and variance of a geometric random variable with parameter  p  are  $\frac 1p$and  ${(1−p)\over(p^2)}$ respectively.


*

*What is $E[X]$?

*What is $Var[X]$?
My solution:
$E[N] = \frac{1}{p} = 2$
$E[X_k] =\frac{1}{2}\times 5=\frac{5}{2}$
$E[X] = E[N]\times E[X_k]=2\times\frac{5}{2} = 5$ 
$Var[N]=\frac{(1−p)}{p^2} = 2$
$Var[X_k] = \frac{1}{12}\times 5^2 = \frac{25}{12}$
 A: Firstly, I assume $N=K$ in your solutions. The expected value and variance of $X$ can be found via Law of Iterated Expectation (LIE) and Law of Total Variance (LTV):
$$E[X]=E[E[X|K]], \ \ \ \ \operatorname{var}(X)=E[\operatorname{var(X|K)}]+\operatorname{var}(E[X|K])$$
For the expectation, your approach is correct, but it can be found via LIE:
$$E[X|K]=KE[X_k]\rightarrow E[KE[X_k]]=E[K]E[X_k]$$
You just need to correct your expectation for $K$: $E[K]=1/p+1$, since it is of the form $1+Y$, where $Y$ is a geometric RV with parameter $p$. Also, note that $\operatorname{var}(K)=\operatorname{var}(1+Y)=\operatorname{var}(Y)=(1-p)/p^2$ as yours.
For the variance, we need $\operatorname{var}(X|K)=\operatorname{var}(\sum X_k|K)=K \operatorname{var}(X_k)$, and by LTV:
$$\begin{align}\operatorname{var}(X)&=E[K\operatorname{var}(X_k)]+\operatorname{var}(KE[X_k])\\ &=\operatorname{var}(X_k)E[K]+E[X_k]^2\operatorname{var}(K)\end{align}$$
The rest is substitution.
A: Formulas for the mean and variance of $X$ are stated and derived here.
Notice that there are two components to $V(X).$
If you're interested in distribution of $X$ you can do a simulation. In R, the a geometric random variable counts the number of failures before the first success;
this is the second version in Wikipedia. With a million iterations one can expect
about 3 significant digits of accuracy.
set.seed(712)
x = replicate(10^6,  sum(runif(rgeom(1,.5)+2,0,5)))    
mean(x);  var(x)
[1] 7.497338     # aprx E(X) = 15/2
[1] 18.73797     # aprx V(X) = 75/4
mean(x < 10)
[1] 0.787989     # aprx P(X<10) = 0.788 +/- 0.001 
2*sd(x < 10)/10^3
[1] 0.0008174656 # aprx 95% marg of sim err

hist(x, prob=T, br=30, col="skyblue2", main="")


