# Deriving the Distributions from a Coffee Shop, Part 2: The Money

[Part 1](dist-derived) ended with a shop that knows tomorrow's demand: about 70 purchases, anywhere from a few dozen to over a hundred, and enough croissants to disappoint no more than a few customers. The owner closes the till tonight with a different question. How much money will that be, and how far off could the forecast be?

Each purchase is a basket of a different size, so revenue has two sources of randomness: how many baskets arrive, and how big each one is. This part builds revenue from three pieces. A day is a **sum** of many baskets. A single basket is a **product** of many small effects. And a quarter hour holds a **random number** of random baskets. Put together, they give the range for tomorrow.

The shop's numbers carry over. The count of purchases tomorrow is negative binomial with mean 70 and shape $r=20$, as in Part 1. Two assumptions come in.

1. **Baskets are drawn independently of counts.** A basket's size does not depend on how many purchases there were.
2. **A basket is gamma:** shape $\alpha=2.25$ and rate $b=0.375$ per dollar, in the notation of Part 1, so the density is $b^\alpha x^{\alpha-1}e^{-bx}/\Gamma(\alpha)$. The mean is $\alpha/b=6$ dollars and the variance is $\alpha/b^2=16$, a standard deviation of 4 dollars. The gamma is chosen because sums of independent gammas with the same rate stay gamma, which Part 1 derives for any shape.

One colour code runs through the text and the figures: <span class="k-obs">blue</span> is what we observe, <span class="k-hid">copper</span> is a hidden quantity, and <span class="k-der">violet</span> is the distribution we derive.

## A sum forgets its parts: the normal

**Teaser.** Tomorrow's revenue is the sum of many separate baskets. What shape does a sum like that have?

The binomial of Part 1 was already one such sum, and its bars become a bell as $n$ grows. The reason is general: a sum of many independent contributions, none dominant, has a distribution that forgets the shape of its parts and keeps only their mean and variance. After centring and scaling it approaches the **normal** law, with density $e^{-x^2/2}/\sqrt{2\pi}$. This is a statement about aggregation, and it needs its own assumption: many small, independent terms.

<details class="more"><summary>Derivation: why only the mean and variance survive</summary>

The claim is that a standardised sum of $n$ independent parts, each with mean $m$ and variance $v$, has a moment generating function that tends to $e^{s^2/2}$, the one of the standard normal. The moment generating function of a variable $X$ is $M_X(u)=\mathbb E\,e^{uX}$. A distribution is determined by it, and if the functions converge near zero the distributions converge. I take that standard theorem as given.

**Its logarithm and the first two terms.** Expanding $e^{uX}=1+uX+u^2X^2/2+\cdots$ and taking expectations,

$$
M_X(u)=1+mu+\tfrac12\,\mathbb E[X^2]\,u^2+O(u^3).
$$

Using $\ln(1+x)=x-x^2/2+\cdots$, the logarithm is

$$
K(u)=\ln M_X(u)=mu+\tfrac12\big(\mathbb E[X^2]-m^2\big)u^2+O(u^3)=mu+\tfrac12\,v\,u^2+O(u^3).
$$

**Independent parts multiply.** For the sum $S_n=X_1+\dots+X_n$, $\mathbb E\,e^{uS_n}=\prod\mathbb E\,e^{uX_i}$ by independence, so $M_{S_n}(u)=M_X(u)^n$ and $\ln M_{S_n}(u)=nK(u)$.

**Centre and scale.** The standardised sum is $Z_n=(S_n-nm)/\sqrt{nv}$. Shifting multiplies the transform by a constant and scaling changes its argument, so

$$
\ln M_{Z_n}(s)=-s\,m\sqrt{n/v}+n\,K\!\Big(\frac{s}{\sqrt{nv}}\Big).
$$

**Put in the expansion.** With $u=s/\sqrt{nv}$ the first term of $nK(u)$ is $n\,m\,s/\sqrt{nv}=s\,m\sqrt{n/v}$, which cancels the shift exactly. The second is $n\cdot\frac12 v\cdot\frac{s^2}{nv}=\frac{s^2}{2}$. The error is $n\cdot O(n^{-3/2})=O(n^{-1/2})$:

$$
\ln M_{Z_n}(s)=\frac{s^2}{2}+O\big(n^{-1/2}\big)\ \longrightarrow\ \frac{s^2}{2}.
$$

**The normal has that transform.** For the standard normal, $\int e^{sx}\,e^{-x^2/2}/\sqrt{2\pi}\,dx$ becomes, by completing the square $sx-x^2/2=-(x-s)^2/2+s^2/2$,

$$
e^{s^2/2}\int\frac{e^{-(x-s)^2/2}}{\sqrt{2\pi}}\,dx=e^{s^2/2}.
$$

So $M_{Z_n}(s)\to e^{s^2/2}$, and $Z_n$ tends to the standard normal. Only $m$ and $v$ entered.

**How slowly.** The next term of the expansion is $\kappa_3u^3/6$ with $\kappa_3$ the third cumulant, and it gives an error of $\frac{\kappa_3}{6\,v^{3/2}\sqrt n}\,s^3$, so the skewness of $Z_n$ is $\kappa_3/(v^{3/2}\sqrt n)$. For one exponential basket, $M_X(u)=\int e^{ux}\lambda e^{-\lambda x}dx=\lambda/(\lambda-u)$ and $K(u)=-\ln(1-u/\lambda)=u/\lambda+u^2/2\lambda^2+u^3/3\lambda^3+\cdots$. Matching $\kappa_3u^3/6$ gives $\kappa_3=2/\lambda^3$, and with $v=1/\lambda^2$ the skewness of one part is 2. For the sum of $n$ it is $2/\sqrt n$.

</details>

The shape of the parts is forgotten slowly. Baskets are skewed, like exponentials, and the skewness of the standardised sum of $n$ is $2/\sqrt n$: $2$ for one basket, $0.63$ for ten, $0.2$ for a hundred. A normal is symmetric, so the approximation is crude at ten baskets and decent at a hundred.

<figure class="mfig ds-figure" id="ds-clt" aria-label="Standardised sums of exponential baskets approaching a normal density as more are added">
<div class="ds-plot"></div>
<figcaption><b>Sums forget the shape of their parts.</b> The bars are standardised sums of $n$ skewed baskets, each drawn from an exponential law; the curve is the normal. Drag the handle to add more baskets.</figcaption>
</figure>

## A product remembers them: the lognormal

**Teaser.** Most baskets are a drink, and a few are a drink and a bag of beans. The average basket is worth more than the typical one. Why?

Many quantities grow by multiplication. A basket is the result of a series of factors: a size upgrade, a pastry, a loyalty discount. A product of positive factors becomes a sum on the log scale, $\ln(F_1\cdots F_n)=\sum\ln F_i$, and a sum of many small independent terms is approximately normal, as the previous section showed. So the product is approximately the exponential of a normal: the **lognormal**. It is skewed to the right and has a long tail. This is a new assumption, not a consequence of the coin and the clock: it needs a multiplicative mechanism.

<details class="more"><summary>Derivation: the lognormal density, its median and its mean</summary>

The claim is that if $\ln X$ is normal with mean $\mu$ and standard deviation $\sigma$, then $X$ has density $\frac{1}{x\sigma\sqrt{2\pi}}\exp\!\big(-\frac{(\ln x-\mu)^2}{2\sigma^2}\big)$, median $e^\mu$, mean $e^{\mu+\sigma^2/2}$ and standard deviation $e^{\mu+\sigma^2/2}\sqrt{e^{\sigma^2}-1}$.

**The density.** $X\le x$ exactly when $\ln X\le\ln x$, so $\Pr(X\le x)=\Phi\big((\ln x-\mu)/\sigma\big)$, where $\Phi$ is the standard normal cumulative distribution. The density is the derivative with respect to $x$. The chain rule gives a factor $\frac{d}{dx}\ln x=\frac1x$, so

$$
f(x)=\frac1x\cdot\frac{1}{\sigma\sqrt{2\pi}}\,e^{-(\ln x-\mu)^2/2\sigma^2}.
$$

**The median.** $\Pr(X\le e^\mu)=\Phi(0)=\tfrac12$, so the median is $e^\mu$.

**The mean.** Write $\ln X=\mu+\sigma Z$ with $Z$ standard normal. Then $\mathbb E[X^k]=e^{k\mu}\,\mathbb E\,e^{k\sigma Z}$, and the last factor is the transform of the standard normal at $s=k\sigma$, which the previous section found to be $e^{k^2\sigma^2/2}$:

$$
\mathbb E[X]=e^{\mu+\sigma^2/2},\qquad\mathbb E[X^2]=e^{2\mu+2\sigma^2}.
$$

**The spread.** The variance is $\mathbb E[X^2]-\mathbb E[X]^2=e^{2\mu+\sigma^2}\big(e^{\sigma^2}-1\big)$, which is the standard deviation stated above. The mean exceeds the median by the factor $e^{\sigma^2/2}$, the pull of the long right tail.

</details>

The mean and the typical value part ways. Take factors drawn evenly between 0.5 and 1.5: each has mean 1, so the product also has mean 1. But the log of one factor has mean $-0.045$ and variance $0.095$. The median of the product after 16 factors is $e^{16\times(-0.045)}=0.485$: a typical basket ends up at half the starting value, while the average stays at 1 because a few baskets end up far above.

<figure class="mfig ds-figure ds-wide" id="ds-lognormal" aria-label="Products of random factors against the lognormal density, on the original and the logarithmic scale">
<div class="ds-plot"></div>
<figcaption><b>Products are skewed, their logs are not.</b> Left: the products of $n$ random factors, on the original scale, with the lognormal density. Right: the same values on the log scale, with a normal density. Drag the handle to multiply more factors.</figcaption>
</figure>

<details class="answer"><summary>Worked example: typical against average basket</summary>

Let basket values be lognormal with a typical (median) basket of 5 dollars and a log-scale spread $\sigma=0.6$. The mean is $5\,e^{0.6^2/2}=6.0$ dollars, 1.2 times the median, pulled up by the tail, and the standard deviation is $3.9$ dollars. Baskets above 15 dollars have probability $0.034$, and the top 1% of baskets start at 20 dollars. The rest of this part uses a gamma basket with the same mean, 6 dollars, and standard deviation, 4, because sums of gamma baskets stay gamma; the lognormal is the right model when the product story holds. Reporting only the mean of 6 dollars would describe few actual baskets: report the median and a high percentile.

</details>

## A random number of random baskets: the compound Poisson

**Teaser.** In a quiet quarter hour the shop expects about one and three-quarters purchases. How likely is a quarter hour with no revenue at all, and what does the rest look like?

Now put the two operations together. A random number $N$ of purchases arrives, Poisson with mean $\lambda$, and each brings a basket $X_i$ drawn from the gamma of the assumptions, independent of the count. The total is $Y=X_1+\dots+X_N$. Two things follow.

If no purchase arrives, the total is exactly zero, which has probability $e^{-\lambda}$. If $n\ge1$ arrive, the sum of $n$ gamma baskets is gamma with shape $n\alpha$, so for $y>0$

$$
f(y)=\sum_{n=1}^\infty \frac{\lambda^ne^{-\lambda}}{n!}\ \htmlClass{eq-der}{\text{Gamma}(y;\,n\alpha,\ b)} .
$$

This is a **compound Poisson–gamma** law: a lump of probability at zero and a smooth density on the positive numbers. This family is known as the Tweedie family; I use only the compound form here and do not derive the Tweedie parametrisation.

<details class="more"><summary>Derivation: the density of a random number of baskets</summary>

The claim is that the total $Y$ has chance $e^{-\lambda}$ of being exactly 0 and, for $y>0$, density $\sum_{n\ge1}\Pr(N=n)\,\text{Gamma}(y;n\alpha,b)$.

**Condition on the count.** The count $N$ decides how many baskets there are, and the baskets do not depend on it. Given $N=n$ the total is a sum of $n$ independent baskets, so the density of $Y$ is the Poisson-weighted average of the densities given each $n$:

$$
f(y)=\sum_{n}\Pr(N=n)\,f_{Y\mid N=n}(y).
$$

**No purchases.** For $n=0$ the sum is empty, so $Y=0$ with chance $\Pr(N=0)=e^{-\lambda}$. This is a lump, not a density.

**One or more purchases.** Part 1 derives that independent gammas with the same rate add their shapes. The density of a sum of $n$ gamma baskets with shape $\alpha$ and rate $b$ is therefore $\text{Gamma}(y;n\alpha,b)$, which gives the sum above.

</details>

<figure class="mfig ds-figure ds-wide" id="ds-compound" aria-label="The compound Poisson-gamma density of revenue in a window and its probability of exactly zero, for a chosen number of expected purchases">
<div class="ds-plot"></div>
<figcaption><b>A lump at zero and a curve beyond it.</b> The bar at the left is the chance that no purchase arrived; the curve is the density of the revenue when some did. Baskets have mean 6 dollars and standard deviation 4. Drag the handle to change how many purchases the window expects: a busy window leaves the lump behind.</figcaption>
</figure>

The mean and the variance of the total need only the mean and variance of the count and of a basket.

<details class="more"><summary>Derivation: the mean and variance of a random number of baskets</summary>

The claim is that $Y=X_1+\dots+X_N$, with $N$ having mean $\mathbb E[N]$ and variance $\operatorname{Var}N$, and the $X_i$ independent of $N$ with mean $\mu_X$ and variance $\sigma_X^2$, has mean $\mathbb E[N]\mu_X$ and variance $\mathbb E[N]\,\sigma_X^2+\operatorname{Var}(N)\,\mu_X^2$.

**The mean.** Given $N=n$ the total has mean $n\mu_X$. Averaging over $n$ gives $\mathbb E[Y]=\mathbb E[N]\mu_X$.

**Splitting a variance.** For any variables, $\mathbb E[Y^2]=\mathbb E\big[\mathbb E[Y^2\mid N]\big]$ and $\mathbb E[Y^2\mid N]=\operatorname{Var}(Y\mid N)+\big(\mathbb E[Y\mid N]\big)^2$. Subtracting $\mathbb E[Y]^2=\big(\mathbb E\,\mathbb E[Y\mid N]\big)^2$ leaves

$$
\operatorname{Var}Y=\mathbb E\big[\operatorname{Var}(Y\mid N)\big]+\Big(\mathbb E\big[(\mathbb E[Y\mid N])^2\big]-\big(\mathbb E\,\mathbb E[Y\mid N]\big)^2\Big)=\mathbb E\big[\operatorname{Var}(Y\mid N)\big]+\operatorname{Var}\big(\mathbb E[Y\mid N]\big).
$$

**The first term.** Given $N=n$ the baskets are independent, so $\operatorname{Var}(Y\mid N=n)=n\sigma_X^2$, and averaging over $n$ gives $\mathbb E[N]\sigma_X^2$.

**The second term.** Given $N=n$ the mean is $n\mu_X$, so the second term is $\operatorname{Var}(N)\,\mu_X^2$.

**Adding them.** Therefore

$$
\operatorname{Var}Y=\mathbb E[N]\,\sigma_X^2+\operatorname{Var}(N)\,\mu_X^2 .
$$

For a Poisson count both $\mathbb E[N]$ and $\operatorname{Var}N$ equal $\lambda$, so $\operatorname{Var}Y=\lambda(\sigma_X^2+\mu_X^2)$. For the gamma basket, $\mu_X=\alpha/b$ and $\sigma_X^2=\alpha/b^2$, so this is $\lambda\alpha(\alpha+1)/b^2$. The next section uses the formula with a general count variance.

</details>

<details class="answer"><summary>Worked example: a quiet quarter hour</summary>

A quarter hour expects 1.75 purchases (twenty visitors an hour, 35% buying), and baskets have shape $2.25$ and rate $0.375$. The chance of no revenue is $e^{-1.75}=0.174$. The mean revenue is $1.75\times6=10.50$ dollars, with a standard deviation of $9.5$ dollars, nearly as large as the mean. The chance of more than 20 dollars in the window is $0.154$. Compare a busy window that expects 7 purchases: the chance of no revenue is $0.0009$, the mean is 42 dollars, and the chance of more than 60 dollars is $0.169$.

</details>

## How far off could tomorrow's revenue be?

A day is the same construction with a bigger count. The number of purchases $N$ has the negative binomial law of Part 1, the baskets add as before, and the density of the day's revenue is

$$
f(y)=\sum_{n\ge1}\Pr(N=n)\ \text{Gamma}(y;\,n\alpha,\ b),
$$

with a lump of $\Pr(N=0)$ at zero that is negligible at a mean of 70. The mean is $70\times6=420$ dollars. The variance is $\mathbb E[N]\,\sigma_X^2+\operatorname{Var}(N)\,\mu_X^2$, so how wide the range is depends on how the count behaves.

- **A count fixed at 70.** The variance is $70\times16$, a standard deviation of 33 dollars, and the 95% range is 357 to 488 dollars.
- **A Poisson count.** The variance is $70\,(16+36)=3640$, a standard deviation of 60 dollars, and the range is 307 to 543 dollars.
- **The wandering rate of Part 1.** The count is negative binomial with $r=20$, so $\operatorname{Var}N=70+70^2/20=315$. The variance is $70\times16+315\times36=12{,}460$, a standard deviation of 112 dollars, and the range is 227 to 662 dollars.

The ranges are the 2.5th and 97.5th percentiles of the density above, found by adding up the mixture. The spread in the number of purchases, not the spread in baskets, dominates: a forecast that treats the count as fixed misses most of the risk, and one that treats it as Poisson gives a range about half as wide as the real one.

The last range is not symmetric. The median day is 411 dollars, below the mean of 420, and the range reaches 251 dollars above the median but only 185 below it. A normal curve with the same mean and standard deviation would give 201 to 639 as its middle 95%, and would be wrong at both ends: 1.1% of days fall below 201 and 3.6% above 639, instead of 2.5% each.

The reason is the one the first section gave. The normal needs many small independent terms. The baskets are, but the count is not a sum of small terms: the whole day shares one hidden rate, so a high rate lifts every purchase of the day together. That is a single draw from a skewed gamma belief, and it passes its skew on to the revenue. Averaging cannot remove it.

<figure class="mfig ds-figure ds-wide" id="ds-range" aria-label="The density of a day's revenue for several models of the count of purchases, against a normal curve with the same mean and spread">
<div class="ds-plot"></div>
<figcaption><b>One hidden rate skews the day.</b> The filled curve is the density of the day's revenue; the dashed curve is a normal with the same mean and standard deviation. The bars underneath are the middle 95% of each. Drag the handle from a fixed count to a wandering rate with a small $r$.</figcaption>
</figure>

With hundreds of baskets and a rate that does not wander, the normal is reasonable: for the Poisson count it gives 302 to 538, close to the exact 307 to 543. On a quiet day with ten baskets, the skew from the first section would show.

## What the story cannot tell you

Baskets are assumed independent of the count, and a busy afternoon may well fill with bigger group orders. The gamma basket is chosen because it adds up neatly, not because it was measured. The normal approximation needs many small, nearly independent terms, and a revenue day dominated by one catering order is not that. The range above is only as good as $r=20$, which in practice is estimated from a few weeks of days and is itself uncertain. The test to run is to take a few weeks of daily takings and compare their spread and their skew with the numbers above.

The opening asked how much the till would hold. On these numbers: a typical day of about 410 dollars with a mean of 420, and between about 230 and 660 on 95% of days. What a discount would do to those numbers is in [Part 3](promo-derived).

## Further reading

- [SAS — The GENMOD procedure, Tweedie distribution](https://support.sas.com/documentation/cdl/en/statug/68162/HTML/default/statug_genmod_details28.htm). The map from the Tweedie parameters to the Poisson rate, gamma shape and gamma scale. The page resolves; I did not read it in full.
- Smyth — *Regression analysis of quantity data with exact zeros*, 1996. I confirmed that it exists and where it was published but did not read it.
