10
$\begingroup$

I am reading a book, and there is an example there that doesn't work for me, I get a different result each and every time, and I can't figure out why. Gemini claims that I am correct. The book is respectable, so I am hesitating about this.

In a random experiment a coin is flipped until $H$ comes up. Let $Y$ be the number of flips before the $H$ comes up.

The probability function is $Y$ is then:

$$p(Y=y)= \left(\frac{1}{2}\right)^{y+1}$$

Now we define a new random variable:

$$Z=e^{-Y}$$

And I am interested in $Z$'s expected value.

What I did was:

$$E(Z)=\sum_0^\infty e^{-y} \left(\frac{1}{2} \right)^{y+1} = \frac{1}{2} \sum_0^\infty \left(\frac{1}{2} e^{-1}\right)^y$$

Which led me into this geometric series (or sum). Using the formula for the sum, and since the first element ($y=0$) is $1/2,$ I got:

$$E(Z)=\frac{\frac{1}{2}}{1-\frac{1}{2e}}=\frac{\frac{1}{2}2e}{2e(1-\frac{1}{2e})}=\frac{e}{2e-1}$$

In the book however, the answer is:

$$E(Z)=\frac{2e^2}{2e-1}$$

which is not equivalent.

Help please...

$\endgroup$
4
  • 2
    $\begingroup$ Welcome to CV. It is customary to provide explicit references if one has encountered a problem concerning the contents of a textbook or paper, as it can help with answering your question. Please edit your question to include author, book, and page number. $\endgroup$ Commented Apr 11 at 21:19
  • 2
    $\begingroup$ Assuming I didn't miss something, your answer looks correct to me $\endgroup$ Commented Apr 11 at 23:36
  • 2
    $\begingroup$ Which book is it? $\endgroup$ Commented Apr 12 at 14:08
  • 2
    $\begingroup$ Maybe it's just a coincidence but summing from -1 instead of 0 gives the result in the book:$$\sum _{y=-1}^{\infty } e^{-y}\left(\frac{1}{2}\right)^{y+1} =\frac{2 e^2}{2 e-1}$$ $\endgroup$ Commented Apr 13 at 21:43

5 Answers 5

14
$\begingroup$

When confronted with an apparent contradiction in a mathematical derivation, consider developing the answer in a completely different fashion. With statistical problems, one method that is often available is to simulate the problem.

This R code flips the coin as many times as you specify--I use a million (1e7) flips here (which keeps the run time under one second)--identifies the heads, computes the numbers of flips $Y$ preceding each head, and estimates the expectation by using the average of $\exp(-Y).$ It prints out the "z-score," defined as the difference between that and your estimator as expressed as a multiple of the standard error of estimate. Typically, a z-score between $-2$ and $2$ would be taken as evidence your formula is correct.

set.seed(17)
k <- which(rbinom(1e6, 1, 1/2) == 1)
Y <- diff(c(0, k)) - 1
Z <- exp(-Y)
s <- sqrt(var(Z) / length(Z))              # Standard error of estimate
(mean(Z) - exp(1) / (2 * exp(1) - 1)) / s  # The Z-score

The output is

> [1] 0.2558145

This example is not unusual insofar as (a) it's quick and easy to code and (b) takes almost no computational time. As such, it illustrates why it's a good policy always to check your probability calculations with a simulation.

$\endgroup$
7
  • $\begingroup$ Just for fun(?), I reran your code, but got a z-score of 0.2558145, which corresponds to a 2-sided p-value of .7981. A z-score of 1.739844 gives p=0.0819, not significant, but not a strong evidence. Fwiw, mean(Z) returns 0.61285 vs. the OP's (correct) formula of 0.61270, which is good evidence that his derivation is correct (the book's formula gives 3.331, which the simulation clearly rejects). So something seems off with 1.739... $\endgroup$ Commented Apr 12 at 19:27
  • $\begingroup$ @jginestet There's absolutely nothing the matter with being within 2 ses of the expection! If you like, loop the simulation a few hundred times and examine the distribution of Z-scores: they will be close to normal. $\endgroup$ Commented Apr 12 at 19:56
  • $\begingroup$ That is not what I was saying. Running your simulation, as shown, with the same seed, as many times as I care (resetting the seed each time!), always gives me z=.2558145, not z=1.739844, as you got... That last value does not come from running the code shown; probably after several itterations, w/o resetting the seed? $\endgroup$ Commented Apr 12 at 21:11
  • 1
    $\begingroup$ Different R version? I also get $z=0.2558145$. (Perhaps we are using different values of 17?) $\endgroup$ Commented Apr 12 at 22:11
  • 1
    $\begingroup$ +1 for simulation, its a very good tool for a quick check on reasonableness of answers. I also got 0.2558 with seed 17 on both my (oldish) R install and on rdrr.io/snippets. Rerunning with a dozen other seeds the Z scores do look consistent with standard normal values, consistent with OP being correct (or - in some strange manner - only incorrect by a very small amount) $\endgroup$ Commented Apr 12 at 23:46
8
$\begingroup$

Assuming the description of the question is accurate, the book's claimed result for the expectation cannot possibly be correct because $$\frac{2e^2}{2e-1} > \frac{2e^2}{2e} = e > 2.7,$$ whereas $$\begin{align} \operatorname{E}[Z] &= \operatorname{E}[e^{-Y}] \\ &= e^0 \Pr[Y = 0] + e^{-1} \Pr[Y = 1] + \cdots \\ &< \Pr[Y = 0] + \Pr[Y = 1] + \cdots \\ &= 1. \end{align}$$

There is no need for explicit calculation of the expectation--the above is easy to mentally deduce.

$\endgroup$
4
$\begingroup$

The answer by whuber gives excellent general advice for how to deal with situations of this kind. For completeness, I will show you a derivation of the expected value at issue, which is a particular value of the moment generating function of the geometric distribution. Taking $Y \sim \text{Geom}(p)$ and applying the law of the unconscious statistician allows us to derive the moment generating function:

$$\begin{align} m_Y(t) \equiv \mathbb{E}(e^{tY}) &= \sum_y e^{ty} \cdot \text{Geom}(y|p) \\[6pt] &= \sum_{y=0}^\infty e^{ty} \cdot p (1-p)^y \\[6pt] &= p \sum_{y=0}^\infty (e^t (1-p))^y \\[6pt] &= \frac{p}{1-(1-p)e^t}. \\[6pt] \end{align}$$

In this particular case you have $p=\tfrac{1}{2}$ and $t=-1$ which gives:

$$\mathbb{E}(e^{-Y}) = \frac{\tfrac{1}{2}}{1-\tfrac{1}{2} e^{-1}} = \frac{1}{2-e^{-1}} = \frac{e}{2e-1} = 0.6126998.$$

$\endgroup$
1
  • $\begingroup$ Going over the moment generating function is a standard trick. $\endgroup$ Commented Apr 17 at 11:44
3
$\begingroup$

The book’s answer,

$$\frac{2 e^2}{2 e-1},$$

cannot be right here, because $\mathbb Z = e^{-\mathbb Y}$ always satisfies $0 < \mathbb Z \le 1$, so its expectation must also be at most $1$.

Your calculation is correct. We have, by definition of expectation,

$$E[\mathbb Z] = E\left[e^{- \mathbb Y}\right] = \displaystyle \sum_{y=0}^\infty e^{-y} \left(\frac{1}2 \right)^{y+1} = \frac{1}2 \displaystyle \sum_{y=0}^\infty \left(\frac{1}{2 e} \right)^y.$$

This is a geometric series with ratio $1/(2e)$, so

$$E[\mathbb Z] = \frac{1}2 \cdot \frac{1}{1 - \frac{1}{2 e}} = \frac{e}{2 e-1}.$$

$\endgroup$
1
$\begingroup$

The random variable $\exp(-Y)$ is never more than $1$ and half the time is less than $1,$ so its average value cannot be $2e^2/(2e-1)\approx 3.33098.$

The way you did it is correct.

To check that, I used the following R commands:

e <- exp(1)
n <- seq(0,30)
sum(exp(-n)/2^(n+1))

This gave me $0.6126998.$

The command

e/(2*e-1)

gave me the same number.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.