How to calculate the expected value for a repeated probability? Let's say there's a 30% chance of some event happening, and if it happens then there's a 30% chance of it happening again (but it can only occur twice). I want to calculate the expected value for the number of times it happens. I think I can do this:
Chance of 0 occurences: 0.7
Chance of only 1 occurrence: 0.3 * (1 - 0.3) = 0.21
Chance of 2 occurrences: 0.3 * 0.3 = 0.09
So the expected value is (0 * 0.7) + (1 * 0.21) + (2 * 0.09) = 0.39
But I'm having trouble coming up with a general equation. If I have some event with a probability p, and if the event occurs then it can occur again up to a limit of N times, is there a general way to calculate the expected value for the number of occurrences?
 A: Problem statement:


*

*You have a biased coin. 

*If you flip the coin, then with probability $p$ the coin will come up
heads, and otherwise it'll come up tails.

*You're allowed to continue flipping the coin until it comes up tails, or you've flipped it $N$ times (whichever comes first). 


There are two approaches to this problem. The algebraic approach, and the pure probability approach. I'll cover the algebraic approach first, as it may have more familiar notation for a beginner, however the pure probability approach is simpler and cleaner. 
Let's look at an example case. If $N=3$, the possible results are: T (tails on first flip), HT (1 head, then 1 tail), HHT (2 heads, then 1 tail), and HHH (3 heads).
The probability of getting all heads is pretty straightforward: it's just $p^N$. Otherwise, the probability of getting $n$ heads (with $n<N$) is either $0$ (in the case of negative heads or more than $N$ heads), or it's $p^n(1-p)$. In formal probability notation, we can write this as follows.
Let $X$ represent the number of heads you get from carrying out this process. For a given number of heads $n$,
$$\Pr(X=n)=\begin{cases}
p^n(1-p)& \text{if}\; 0\leq n<N,\\
p^n&      \text{if}\; n=N,\\
0&        \text{otherwise}
\end{cases}$$
This means that the expected number of heads $\text E(X)$ is given by:
$$\text E(X)=N p^N+\sum_{n=0}^{N-1}n p^n(1-p)=N p^N+(1-p)\sum_{n=0}^{N-1}n p^n$$. 
Simplifying this formula, we obtain:
$$\text E(X) = \frac {p(1-p^N)} {(1-p)}$$
This formula gives the answer you calculated:
$$0.3 (1 - 0.3^2)\,/\,0.7=0.39$$
Pure probability approach: Let's generalize. What is the expected number of heads, assuming there's no limit to the number of tosses? (Your problem is easy to solve once we know this). 
When there's no limit on the number of flips, then the formula becomes $\text E(X)=\frac p {1-p}$. This fits our intuition: if $p$ is closer to $1$, then on average you'll have a lot more successes. 
Your problem asked 

What is the expected number of heads, given that we know we got $N$ heads or fewer?

And we can write this as $\text E(X\,|\,X\leq N)$. This means the expectation of $X$, given that the number of heads $X$ is less than or equal to our limit. 
Basically, we're removing all the cases where there were more than $N$ heads. To solve you're problem, just subtract out all cases where there were more than $N$ heads:
$$\text E(X\,|\,X\leq N)=\text E(X)-\text E(X\,|\,X>N)$$
This particular distribution is geometric. That means that $\text E(X\,|\,X>N+1)=p\, \text E(X\,|\,X>N)$. Through induction, we obtain $$\text E(X\,|\,X>N)=p^N\,\text E(X)$$
It follows
$$\text E(X\,|\,X\leq N)=\text E(X)\;-\;p^N\,\text E(X)$$
Which simplifies to
$$\text E(X\,|\,X\leq N)=(1-p^N)\,\text E(X)$$
We have that $\text E(X)=\frac p {1-p}$, so
$$\text E(X\,|\,X\leq N)=\frac {p\,(1-p^N)} {1-p}$$
A: Call $X$ the number of times your event occurs. Assume first that the number of occurrences is theoretically unlimited. Then $X+1$ follows a geometric distribution of parameter $1-p$: $$\Bbb P(X+1=n)=p^{n-1}(1-p)\qquad \text{for $n\geq 1$}.$$
Therefore $$E(X+1)=\frac1p,$$ which implies by linearity $$E(X)=\frac1p -1.$$
Now since the number of occurrences is limited to $N$, the expected value is \begin{align*}E(X)&=\left(\sum_{k=1}^{N}(k-1)\cdot\underbrace{p^{k-1}(1-p)}_{\text{probability that $X+1=k$, i.e. $X=k-1$}}\right)+N\underbrace{\left(1-\sum_{k=1}^{N}p^{k-1}(1-p)\right)}_{\text{probability that $X=N$}}\\&=N+(1-p)\sum_{k=1}^{N}(k-N-1)p^{k-1}.\end{align*}
Note that there are closed formulas for partial geometric sums $\sum_{k=1}^n x^k$ and for the derivative $\sum_{k=1}^n kx^{k-1}$ as well, so you can turn the above into a closed formula if you need to.
