What is the sample space of such random variable? I see a question in Chinese senior high schools books:

Throwing a fair coin until either there is one Head or four Tails.
  Find the expectation of times of throwing.
  (You start throwing a coin, if you see Head, then the game suddenly over; and if you see four Tail, the game is over too. Only these two situation can the game be over.)

(The answer is $1\times\frac{1}{2}+2\times\frac{1}{4}+\cdots=\frac{15}{8}$)
I know that, if we want to calculate the expectation, we, of course, need to find the random variable first. In order to find the random variable, we need to know the sample space of the experiment. However, how can we say about this sample space? The throwing times are varing, not a constant like 3. If the times we throw is 3, the sample space is $\{(a_1,a_2,a_3)\mid \forall 1\le i\le 3,~a_i\in\{H,T\}\}$. But the sample space like this question, is not like this one. What is its sample space?
 A: You could take $\Omega=\{T,H\}^4$ as sample space where all outcomes are equiprobable, and prescribe random variable $X$ as the function $\Omega\to\mathbb R$ determined by:


*

*$X(\omega)=1$ if $\omega_1=H$

*$X(\omega)=2$ if $\omega_1=T$ and $\omega_2=H$

*$X(\omega)=3$ if $\omega_1=\omega_2=T$ and $\omega_3=H$

*$X(\omega)=4$ otherwise


This gives the probabilities:


*

*$P(X=1)=\frac12$

*$P(X=2)=\frac14$

*$P(X=3)=\frac18$

*$P(X=4)=\frac18$ 


And expectation: $$\mathbb EX=\sum_{k=1}^4kP(X=k)=1\cdot\frac12+2\cdot\frac14+3\cdot\frac18+4\cdot\frac18=\frac{15}8$$
The probability space is $\langle\Omega,\wp(\Omega),P)$ where $\Omega=\{T,H\}^4$ and probability measure $P$ is defined by: $$P(S)=\frac{|S|}{16}$$

Fortunately in situations like this it is not necessary at all to construct a suitable sample space. We can restrict to finding the values of $P(X=k)$ by logical thinking. In many cases even that is not needed when it comes to calculating expectations. 
It is a good thing however to know about the construction of sample spaces, and for that it is good practice to construct one now and then.
A: You can use as a sample space the set
$$
    \Omega=\left\{H,TH,TTH,TTTH,TTTT\right\}
$$
The probabilities of these outcomes are $\frac{1}{2}$, $\frac{1}{4}$, $\frac{1}{8}$, $\frac{1}{16}$, and $\frac{1}{16}$ respectively.
[You asked how to show this rigorously.  Each throw is independent from every other throw, and on each throw the probability of $H$ or $T$ is $\frac{1}{2}$.  The probability of independent events is the product of the probabilities of each of those events.]
Let $X$ be the number of throws in each outcome.  That's just the length of the word: 
\begin{align*}
    X(H) &= 1 \\
    X(TH) &= 2 \\
    X(TTH) &= 3 \\
    X(TTTH) &= 4 \\
    X(TTTT) &= 4
\end{align*}
So the expected value is:
$$
    E(X) = 1 \left(\frac{1}{2}\right)
          +2 \left(\frac{1}{4}\right)
          +3 \left(\frac{1}{8}\right)
          +4 \left(\frac{1}{16}\right)
          +4 \left(\frac{1}{16}\right)
         = \frac{15}{8}
$$
