# Deriving the Distributions from a Coffee Shop, Part 3: The Promotion

[Part 1](dist-derived) asked how many croissants to bake and how many baristas to ask for, and [Part 2](revenue-derived) asked how much money the till would hold. Both took the shop as given. This part lets the owner change it.

Between two and four in the afternoon the shop is quiet: about twenty visitors an hour, 35% of them buying, one barista. The owner is thinking about taking 10% off for those two hours. Should the shop run the discount, how deep should it be, and does it need a second barista to cope?

A discount does not add a new family of distributions. It turns the two dials of Part 1, the clock and the coin, and the queue and the margin then react to the new settings. This part follows the change in three steps: what a discount does to the dials, what the extra traffic does to the queue and the margin, and what a short trial would tell the owner about how large the response really is.

The shop's numbers carry over: a drink takes three minutes, a barista finishes drinks at rate $\nu=20$ an hour, and a basket has a mean of 6 dollars. Five new assumptions come in.

1. **A discount of $d$ multiplies the visitor rate by $e^{\eta_\lambda d}$**, with $\eta_\lambda=2$ as a first guess. It is a guess to be tested, not a measurement.
2. **A discount multiplies the odds of buying by $e^{\eta_p d}$**, with $\eta_p=3$ as a first guess. The odds of an event with chance $p$ are $p/(1-p)$.
3. **A basket costs the shop 2.50 dollars to make**, so the shop keeps $6(1-d)-2.50$ dollars on an average basket: 3.50 dollars today, 2.90 at 10% off. The discount applies to every sale, including those the shop would have made anyway.
4. **A customer who would wait more than five minutes for a drink leaves.** A second barista costs 15 dollars an hour.
5. **The trial counts everyone who walks in and everyone who asks for a drink,** including those who give up in the line.

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 discount turns two dials

**Teaser.** The shop announces 10% off. How many more visitors come, and how much likelier is each to buy?

The coin and the clock of Part 1 cannot say. They describe what happens once the rates are fixed, not how a price moves them. So the response is an assumption, and the form it takes is chosen to keep each dial legal.

A rate must stay positive, so the discount multiplies it instead of adding to it: the visitor rate is $\lambda(d)=\lambda_0e^{\eta_\lambda d}$, with $\lambda_0=20$. At 10% off the rate is $20e^{0.2}=24.4$ visitors an hour.

A probability must stay below 1, so the discount cannot add to it either: a rise of 0.1 would break at a chance of 0.95. The odds $p/(1-p)$ have no ceiling, and a constant multiplier on them is the simplest legal response. Each additional point of discount multiplies the odds of buying by the same factor, and the chance of buying follows.

<details class="more"><summary>Derivation: the buying chance from a multiplier on the odds</summary>

The claim is that if the odds of buying are multiplied by $e^{\eta_p d}$ at discount $d$, then the chance of buying is $p(d)=1/\big(1+e^{-(\operatorname{logit}p_0+\eta_p d)}\big)$, where $\operatorname{logit}p_0=\ln\frac{p_0}{1-p_0}$.

**From odds to chance.** Write $\omega=p/(1-p)$ for the odds. Solving for $p$,

$$
p=\frac{\omega}{1+\omega}=\frac{1}{1+\omega^{-1}} .
$$

**The assumption.** Today the odds are $\omega_0=p_0/(1-p_0)$, and at discount $d$ they are

$$
\omega(d)=\omega_0\,e^{\eta_p d}.
$$

**Putting it in.** Then $\omega(d)^{-1}=\omega_0^{-1}e^{-\eta_p d}$. Since $\omega_0^{-1}=e^{-\operatorname{logit}p_0}$,

$$
\htmlClass{eq-der}{p(d)}=\frac{1}{1+e^{-\operatorname{logit}p_0}\,e^{-\eta_p d}}=\frac{1}{1+e^{-(\operatorname{logit}p_0+\eta_p d)}} .
$$

**It stays legal.** The denominator is always greater than 1, so $0<p(d)<1$ for every $d$, and it rises with $d$ when $\eta_p>0$. Its slope is $p(d)\,\big(1-p(d)\big)\,\eta_p$, which shrinks as $p$ approaches 1.

**The shop's numbers.** Today $\omega_0=0.35/0.65=0.538$. At 10% off the odds are $0.538\,e^{0.3}=0.727$ and the chance is $0.727/1.727=0.421$.

</details>

At 10% off, then, the shop expects 24.4 visitors an hour and 42% of them buy. Buyers form a thinned Poisson stream, as in Part 1, with rate $\lambda(d)\,p(d)=24.4\times0.421=10.3$ an hour against 7 today, which is 47% more orders.

<figure class="mfig ds-figure ds-wide" id="ds-response" aria-label="Visitors per hour and the chance that a visitor buys, against the size of the discount">
<div class="ds-plot"></div>
<figcaption><b>A discount turns two dials.</b> Left: the visitor rate, which multiplies. Right: the chance that a visitor buys, which follows the odds and stays below 1. The dot marks the chosen discount. Drag the handle to deepen it.</figcaption>
</figure>

## Cheaper sales: how much traffic pays for the discount?

**Teaser.** At 10% off, each basket leaves the shop 2.90 dollars instead of 3.50. How many more orders make up for that?

Profit an hour is orders times what each order leaves. Today that is $7\times3.50=24.50$ dollars before the queue. At discount $d$ it is $o(d)\times m(d)$, where $o(d)=\lambda(d)\,p(d)$ is the order rate and $m(d)=6(1-d)-2.50$ is the margin. To match today, orders must rise by the factor $m(0)/m(d)$: 1.21 at 10% off, 1.35 at 15%, 1.75 at 25%. The assumed response raises orders by 1.47 at 10% off and 1.77 at 15%, so it clears both.

If the margin were the whole story, the best discount would be deep.

<details class="more"><summary>Derivation: the discount that earns the most before the queue</summary>

The claim is that the profit $o(d)\,m(d)$ is largest where $\eta_\lambda+\big(1-p(d)\big)\,\eta_p=6/m(d)$.

**Take logarithms.** Profit is a product, so its logarithm is a sum, and its slope is a sum of slopes:

$$
\frac{d}{dd}\ln\big(o\,m\big)=\frac{d}{dd}\ln\lambda+\frac{d}{dd}\ln p+\frac{d}{dd}\ln m .
$$

**The three slopes.** Since $\ln\lambda=\ln\lambda_0+\eta_\lambda d$, the first slope is $\eta_\lambda$. The chance has slope $p(1-p)\eta_p$ from the derivation above, so $\frac{d}{dd}\ln p=(1-p)\,\eta_p$. The margin $m=6(1-d)-2.50$ falls by 6 for each unit of $d$, so $\frac{d}{dd}\ln m=-6/m$.

**Where the slope is zero.** Adding the three and setting the sum to zero,

$$
\eta_\lambda+\big(1-p(d)\big)\,\eta_p=\frac{6}{m(d)} .
$$

**One crossing.** The left side falls as $d$ grows, because $p$ rises. The right side rises, because $m$ falls. So they cross once, profit rises before the crossing and falls after it.

**The shop's numbers.** At 10% off the left side is $2+0.579\times3=3.74$ and the right side is $6/2.90=2.07$, so profit is still rising. The sides meet at a discount of 28.3%.

</details>

At that discount the order rate is 19.6 an hour and profit is 35.4 dollars an hour, 44% more than today. That should make a reader suspicious: the baristas have not appeared yet. One barista finishes 20 orders an hour, and 19.6 arrive.

## Traffic feeds the queue

**Teaser.** At 28% off, orders arrive almost as fast as one barista can finish them. What does the line look like?

Part 1 derived the queue. With orders arriving at rate $o$ and $c$ baristas, each finishing at rate $\nu$, a customer who has to wait waits an exponential time with rate $c\nu-o$, and

$$
\Pr(W>t)=\Pr(\text{wait})\,e^{-(c\nu-o)\,t},
$$

where $\Pr(\text{wait})$ is the chance an order finds every barista busy. With $a=o/\nu$ and $\rho=a/c$, Part 1 derived it as

$$
\Pr(\text{wait})=\frac{\frac{a^c}{c!}\cdot\frac1{1-\rho}}{\sum_{k=0}^{c-1}\frac{a^k}{k!}+\frac{a^c}{c!}\cdot\frac1{1-\rho}} ,
$$

which for one barista is just $\rho$. Part 1 reported waits of more than two minutes at the rush; an afternoon customer with time to spare is assumed to give up only after five. The share of orders lost to the line is then $L(o,c)=\Pr(W>\tfrac1{12}\text{ hour})$. Even today, with 7 orders an hour and one barista, 12% of orders wait longer than five minutes, because drinks take three minutes and orders arrive at random.

Profit an hour is now the orders that stay, times the margin, less the second barista if the shop hires one:

$$
\text{profit}(d,c)=o(d)\,\big[1-L\big(o(d),c\big)\big]\,m(d)-15\,(c-1).
$$

<figure class="mfig ds-figure ds-wide" id="ds-promo" aria-label="Profit per hour against the size of the discount, with one barista and with two">
<div class="ds-plot"></div>
<figcaption><b>The queue turns the curve over.</b> Profit an hour against the discount: solid with one barista, dashed with two, the second costing 15 dollars an hour. The horizontal line is today's profit, no discount, one barista. Drag the handle to deepen the discount and read the orders, the share lost to the line and the margin above the curve.</figcaption>
</figure>

With one barista, profit rises from 21.6 dollars an hour to a peak of 23.1 at 8.5% off, then falls. At 15% off it is back to 21.6, the same as no discount, and at 20% off it is 17.7. At 25% off the line is so long that profit is 9.9. A second barista removes the line but costs 15 dollars an hour, and its best profit, 18.7 at 25% off, never reaches today's 21.6. On these numbers the second barista never pays.

<details class="answer"><summary>Worked example: 15% off with one barista or two</summary>

At 15% off the shop expects $20e^{0.3}=27.0$ visitors an hour, a buying chance of $0.458$ and so 12.36 orders an hour, each leaving $6\times0.85-2.50=2.60$ dollars. With one barista, $\Pr(\text{wait})=\rho=0.62$, the wait has rate $20-12.36=7.64$ an hour, and the share lost to the line is $0.62\,e^{-7.64/12}=0.327$: a third of the orders leave. Profit is $12.36\times0.673\times2.60=21.6$ dollars an hour, the same as no discount. The discount brought 5.4 more orders an hour and earned nothing. With two baristas $a=0.618$, $\rho=0.309$ and $\Pr(\text{wait})=0.146$, the wait has rate $40-12.36=27.64$ an hour, so $0.146\,e^{-27.64/12}=0.015$ of the orders leave, and profit is $12.36\times0.985\times2.60-15=16.7$ dollars an hour.

</details>

A 10% discount earns 23.0 dollars an hour against 21.6, a gain of 1.4 dollars an hour, or 2.80 dollars an afternoon. The gain is small. It is also fragile, because it rests on two guessed numbers. If the true response were half as large, with $\eta_\lambda=1$ and $\eta_p=1.5$, then 10% off would earn 20.6 dollars an hour, a loss of one dollar. The sign of the answer depends on numbers nobody has measured.

## Learning the response from a trial

**Teaser.** The owner runs 10% off for two afternoons, four hours in all. What would the shop know afterwards?

Part 1 has the tools. The visitor rate is a hidden quantity with a gamma belief, which each visitor updates, and the chance of buying is a hidden quantity with a beta belief, which each buyer updates. Before the trial the owner expects no lift: the rate is $\text{Gamma}(4,\ 0.2)$ per hour, the belief of Part 1 with mean 20 and standard deviation 10, and the buying chance is $\text{Beta}(7,\ 13)$, centred on today's 0.35 and worth twenty visitors of experience. After $n$ visitors and $x$ buyers in $t$ hours,

$$
\lambda\mid\text{data}\ \sim\ \htmlClass{eq-der}{\text{Gamma}(4+n,\ 0.2+t)},\qquad p\mid\text{data}\ \sim\ \htmlClass{eq-der}{\text{Beta}(7+x,\ 13+n-x)}.
$$

Each visitor adds one to the gamma's shape, each hour adds one to its rate parameter, and each buyer and each non-buyer add one to the two beta parameters. The two beliefs are held separately: what the shop learns about the clock tells it nothing about the coin.

<details class="more"><summary>Derivation: the two updates, and why the beliefs are independent</summary>

The claim is that after $n$ visitors and $x$ buyers in $t$ hours, the beliefs about $\lambda$ and $p$ are the gamma and the beta above, and are independent.

**The likelihood factors.** The visitors in the window are Poisson, $\Pr(n\mid\lambda)=(\lambda t)^ne^{-\lambda t}/n!$. Given the $n$ visitors, the buyers are binomial, $\Pr(x\mid n,p)=\binom nx p^x(1-p)^{n-x}$. The chance of the whole record is the product, and as a function of the two unknowns it separates:

$$
\Pr(n,x\mid\lambda,p)\ \propto\ \underbrace{\lambda^{n}e^{-\lambda t}}_{\text{only }\lambda}\ \cdot\ \underbrace{p^{x}(1-p)^{n-x}}_{\text{only }p}.
$$

**The prior factors too.** The two priors are chosen independently, $\lambda^{3}e^{-0.2\lambda}$ for $\text{Gamma}(4,0.2)$ and $p^{6}(1-p)^{12}$ for $\text{Beta}(7,13)$, each up to a constant.

**Multiply.** Belief is prior times likelihood, so the joint belief is a product of a function of $\lambda$ and a function of $p$, which means the two are independent:

$$
\lambda^{4+n-1}e^{-(0.2+t)\lambda}\quad\text{and}\quad p^{7+x-1}(1-p)^{13+n-x-1}.
$$

**Read off the kernels.** The first is a $\text{Gamma}(4+n,\ 0.2+t)$ kernel, the second a $\text{Beta}(7+x,\ 13+n-x)$ kernel, as in Part 1. Their means and standard deviations come from the formulas of Part 1: $a/b$ and $\sqrt a/b$ for the gamma, $a/(a+b)$ and $\sqrt{ab/((a+b)^2(a+b+1))}$ for the beta.

</details>

The owner's question is whether the discount pays. For any pair of true values $(\lambda,p)$, the profit follows from the previous section. The beliefs say how likely each pair is, so the chance that the discount pays is the belief mass of the pairs that earn more than today's 21.6 dollars an hour.

<details class="more"><summary>Derivation: the chance that the promotion pays</summary>

The claim is that the chance that 10% off beats today is an integral over the two beliefs, which has no closed form and is computed on a grid.

**A known pair gives a known profit.** If the true visitor rate is $\lambda$ and the true buying chance is $p$, the order rate is $o=\lambda p$ by thinning, and the profit at discount $d$ with $c$ baristas is $\text{profit}(o;d,c)$ as in the previous section. It depends on the pair only through the product $\lambda p$.

**The beliefs are independent.** The posterior density of the pair is the product of the gamma density in $\lambda$ and the beta density in $p$, because the visitor counts and the buyer counts enter the likelihood separately.

**The chance.** The chance that the discount beats today is

$$
\Pr(\text{pays})=\int_0^1\!\!\int_0^\infty \mathbf 1\Big[\text{profit}(\lambda p;\,d,\,c)>\text{profit}_{\text{today}}\Big]\ \text{Gamma}(\lambda;\,a,\,b)\ \text{Beta}(p;\,a',\,b')\ d\lambda\,dp ,
$$

where $\mathbf 1[\cdot]$ is 1 when the profit beats today's and 0 otherwise.

**Why no closed form.** The profit passes through the queue, which is not a simple function of $\lambda p$, so the region where it beats today's is not a simple shape. The integral is computed by adding up the belief mass on a fine grid over $(\lambda,p)$.

</details>

<figure class="mfig ds-figure ds-wide" id="ds-trial" aria-label="Beliefs about the visitor rate and the buying chance after some hours of a trial, and the chance that the discount pays">
<div class="ds-plot"></div>
<figcaption><b>The beliefs sharpen, and the answer firms up.</b> Left: the belief about visitors an hour. Right: the belief about the chance a visitor buys. Dashed is the belief before the trial, solid the belief after the chosen hours, and the thin vertical line marks today's value. Drag the handle to watch more hours of a simulated trial.</figcaption>
</figure>

<details class="answer"><summary>Worked example: two afternoons, then four</summary>

Before any data the chance that the discount pays is $0.20$, and it rests on the prior alone. In a simulated trial, 99 visitors came in the first four hours and 44 bought. The rate belief is $\text{Gamma}(103,\ 4.2)$, with mean $103/4.2=24.5$ visitors an hour and standard deviation $\sqrt{103}/4.2=2.4$. The buying belief is $\text{Beta}(51,\ 68)$, with mean $0.429$ and standard deviation $0.045$. The chance that 10% off with one barista beats today is $0.81$, and with two baristas it is $0.07$. After eight hours, 198 visitors and 91 buyers, the rate belief has mean 24.6 and the buying belief has mean $0.450$, and the chance is $0.96$.

</details>

Four hours do not settle it. A chance of 0.81 means the discount still loses one time in five, and the chance does not climb smoothly: it jumps from 0.81 at four hours to 0.96 at five, when a busy hour of 36 visitors comes in, and slips from 0.98 at seven hours to 0.96 at eight, when a quiet one does. A shop that wants to be sure waits for more hours.

The same trial can say the opposite. Suppose the response is half as large as assumed, so a visitor arrives at 22 an hour and buys with chance 0.385. Then 10% off loses a dollar an hour, 20.6 against 21.6. The next figure replays a simulated trial of that shop. The chance that the discount pays starts at 0.20 from the prior alone, rises to 0.41 after two hours because the first buyers come quickly, 19 of 44 visitors, and then falls as the evidence arrives: 0.18 after four hours, 0.13 after six and 0.22 after eight. After four hours the beliefs are $\text{Gamma}(81,\ 4.2)$ with mean 19.3 visitors an hour and $\text{Beta}(40,\ 57)$ with mean 0.412, so the belief about visitors sits below today's 20 while the belief about buying has risen to 0.41. The visitors are what the discount failed to bring, and the owner who ran the trial keeps the shop as it was.

<figure class="mfig ds-figure ds-wide" id="ds-trial-weak" aria-label="Beliefs about the visitor rate and the buying chance after some hours of a trial in a shop where the discount works less well than hoped">
<div class="ds-plot"></div>
<figcaption><b>The same trial where the response is weaker.</b> Same layout as before, a different simulated shop: the discount brings fewer visitors and a smaller rise in buying. Drag the handle to watch more hours.</figcaption>
</figure>

## What the story cannot tell you

The dials move because the model says so. The exponential for visitors and the multiplier on the odds were chosen because they stay legal, not because anyone measured them, and the trial measures the response at one depth, 10%. The claim that the best discount is 8.5% extrapolates the assumed form to other depths, and a shop that wants the shape has to try two or three depths.

The trial can fool the owner. Two afternoons are two draws of weather and weekday, and the discount days should be alternated with ordinary days so the comparison is fair. Counting only the drinks sold would hide the customers who left the line, which is why the count includes everyone who asked: a count of completed sales is censored by the queue, and it would make the discount look less popular than it was.

The queue is simplified. A customer who leaves does not join the line, so the share lost is overstated when the line is long. Drinks are taken to be exponential, as in Part 1, and patience a fixed five minutes. Baskets are assumed to stay at 6 dollars, though people who come for a discount may buy less, and regulars may learn to wait for the next one.

The gain is small beside the noise of a day. The 2.80 dollars an afternoon of the best case is far below the 112 dollars of day-to-day spread in Part 2's revenue. Daily takings will never show whether the promotion worked, and only counts of visitors and buyers during the window can.

The opening asked whether to run the discount, how deep, and with how many baristas. On these numbers: about 10% off, one barista, and a gain of about 1.4 dollars an hour, which is positive with probability 0.81 after two afternoons of trial and 0.96 after four. A second barista does not pay, and neither does going deeper than about 15%, where the line eats the gain.
