TITLE: Probability: Understanding Random Variables
QUESTION [0 upvotes]: I am using Ross' A First Course In Probability (4th). On page 113, Example 1d states the following:

Independent tirals consisting of the flipping of a coin having
  probability $p$ of coming up heads are continually performed until
  either a head occurs or a total of $n$ flips is made. If we let $X$
  denote the number of times the coin is flipped, then $X$ is a random
  variable taking on one of the values 1, 2, 3, ..., n with respective
  probabilities
$P\{X=1\} = P\{H\} = p$
$P\{X=2\} = P\{(T,H)\} = (1-p)p$
$P\{X=3\}=P\{(T,T,H)\} = (1-p)^2p$
.
.
.
$P\{X=n-1\}=P\{(T,T,...,T,H)\}=(1-p)^{n-2}p$
$P\{X=n\}=\{(T,T,...,T,T),(T,T,...,T,H)\}=(1-p)^{n-1}$

There are a couple of things that are confusing to me here. To my understanding, they are defining $X$ to be "the number of times the coin is flipped until a head occurs or a total of $n$ flips is made." Then, We would have $P\{X\}$ to be
$P\{X = n\} = \sum\limits_{i=1}^n (1-p)^{i-1}p^i + (1-p)^i$    , isn't it?
Also, wouldn't $P\{X=n\} =(1-p)^{n-1}p$? How does it turn out to be $(1-p)^{n-1}$?
Any help would be appreciated.

REPLY [0 votes]: We are running a series of Bernoulli trials, with a probability of success being $p$, and a probability of failure being $1-p$, until we encounter either a success or reach the $n^\text{th}$ trial.   $X$ is the count of trials we perform during a run.
For $0\leq k < n$ we have $\mathsf P\{X=k\} = (1-p)^{k-1}\color{silver}{\cdot}p$, which is the probability of $k-1$ consecutive failures followed by $1$ success.
However, for $k=n$ we want the probability of $n-1$ consecutive failures preceding the $n^\text{th}$ trial.   The last trial may either be a failure or a success because we'll terminate the run either way.   Thus: $\mathsf P\{X=n\}= (1-p)^{n-1}\color{silver}{\cdot 1}$
{"set_name": "stack_exchange", "score": 0, "question_id": 1265755}