TExcelTAD
Home › Online Algorithms & Competitive Analysis › Randomized Ski Rental: Deriving the Optimal Distribution

Randomized Ski Rental: Deriving the Optimal Distribution

The deterministic lower bound of the previous lesson works because the adversary knows the buying day and ends the season on it. A skier who buys on a random day, drawn from a distribution the adversary knows but whose outcome it does not, cannot be ambushed that way. Part I stated the optimal distribution and its ratio ee−1≈1.582\frac{e}{e-1} \approx 1.582 (Ski Rental: Rent or Buy?). This lesson derives both, in discrete and continuous time, and then proves with Yao's principle that no randomized strategy does better. The result is due to Karlin, Manasse, McGeoch and Owicki (1990); all bounds here are strict ratios against an oblivious adversary.

The equalizing idea

A randomized strategy buys on day JJ with Pr⁡[J=i]=pi\Pr[J = i] = p_i. Buying after day BB never helps (buying on day BB instead costs less on every long season and the same on short ones), so assume pp lives on {1,…,B}\{1, \ldots, B\}. For a season of DD days the expected cost is

E(D)=∑i≤Dpi (i−1+B)+D∑i>Dpi,E(D) = \sum_{i \le D} p_i \,(i - 1 + B) + D \sum_{i > D} p_i ,

and for D≥BD \ge B it no longer changes, while OPT(D)=min⁡(D,B)\mathrm{OPT}(D) = \min(D, B). The adversary picks the DD with the worst ratio E(D)/OPT(D)E(D)/\mathrm{OPT}(D). A natural guess for the best pp is the one that makes the adversary indifferent: E(D)=c⋅DE(D) = c \cdot D for every D=1,…,BD = 1, \ldots, B. If any DD had a smaller ratio, weight could be shifted to lower the others.

Deriving the discrete distribution

Subtract consecutive equations. Going from D−1D - 1 to DD days, the outcome J=DJ = D changes from "rented D−1D - 1 days" to "rented D−1D - 1 days and bought", adding BB, and every outcome J>DJ > D pays one more day of rent:

E(D)−E(D−1)=B pD+Pr⁡[J>D]=cfor D=1,…,BB pD+Pr⁡[J>D]−B pD+1−Pr⁡[J>D+1]=0subtract the next equationB pD−(B−1) pD+1=0since Pr⁡[J>D]−Pr⁡[J>D+1]=pD+1\begin{aligned} E(D) - E(D - 1) &= B\,p_D + \Pr[J > D] = c && \text{for } D = 1, \ldots, B \\ B\,p_D + \Pr[J > D] - B\,p_{D+1} - \Pr[J > D + 1] &= 0 && \text{subtract the next equation} \\ B\,p_D - (B - 1)\,p_{D+1} &= 0 && \text{since } \Pr[J > D] - \Pr[J > D+1] = p_{D+1} \end{aligned}

So pD=(1−1B) pD+1p_D = (1 - \frac{1}{B})\,p_{D+1}: each day is BB−1\frac{B}{B-1} times as likely as the previous one, pi=pB (1−1/B)B−ip_i = p_B\,(1 - 1/B)^{B - i}. The last equation (D=BD = B, where Pr⁡[J>B]=0\Pr[J > B] = 0) gives c=B pBc = B\,p_B, and normalization gives pB=1/(B(1−(1−1/B)B))p_B = 1/\bigl(B(1 - (1 - 1/B)^B)\bigr). Hence

pi=(1−1/B)B−iB(1−(1−1/B)B),cB=11−(1−1/B)B→B→∞11−1/e=ee−1.p_i = \frac{(1 - 1/B)^{B - i}}{B\bigl(1 - (1 - 1/B)^B\bigr)}, \qquad c_B = \frac{1}{1 - (1 - 1/B)^B} \xrightarrow{B \to \infty} \frac{1}{1 - 1/e} = \frac{e}{e - 1}.

The program builds pp from the recurrence with exact fractions, checks that every season length gives the same ratio, and samples the strategy with a seeded generator to confirm the expectation empirically.

python · try it yourself
import random
from fractions import Fraction

def optimal_distribution(B):
    r = Fraction(B - 1, B)
    w = [r ** (B - i) for i in range(1, B + 1)]       # p_i proportional to (1 - 1/B)^(B - i)
    s = sum(w)
    return [x / s for x in w]

def expected_cost(p, B, D):
    return sum(pi * ((i - 1 + B) if D >= i else D) for i, pi in enumerate(p, start=1))

B = 10
p = optimal_distribution(B)
ratios = {D: expected_cost(p, B, D) / min(D, B) for D in range(1, 2 * B)}
print("p_1..p_B:", " ".join(f"{float(x):.4f}" for x in p))
print("distinct ratios over D = 1..19:", sorted({str(r) for r in ratios.values()}))
print(f"c_B = {float(ratios[1]):.6f}, formula {1 / (1 - (1 - 1 / B) ** B):.6f}")

rng = random.Random(7)
days = list(range(1, B + 1))
weights = [float(x) for x in p]
for D in [3, 10, 25]:
    runs = [rng.choices(days, weights)[0] for _ in range(20000)]
    avg = sum((j - 1 + B) if D >= j else D for j in runs) / len(runs)
    print(f"season {D:2}: sampled E[cost]/OPT = {avg / min(D, B):.4f}")
p_1..p_B: 0.0595 0.0661 0.0734 0.0816 0.0907 0.1007 0.1119 0.1244 0.1382 0.1535
distinct ratios over D = 1..19: ['10000000000/6513215599']
c_B = 1.535340, formula 1.535340
season  3: sampled E[cost]/OPT = 1.5363
season 10: sampled E[cost]/OPT = 1.5367
season 25: sampled E[cost]/OPT = 1.5338

The distinct-ratios line contains a single fraction: the adversary is indeed indifferent. Sampling agrees with the exact value up to the usual noise of 20000 runs.

The continuous version

In continuous time, rent accrues at rate 1 and the skier buys at a random time TT with density ff on [0,B][0, B]. The same equalization, E[cost(D)]=c D\mathbb{E}[\text{cost}(D)] = c\,D for all D∈[0,B]D \in [0, B], reads

∫0D(t+B)f(t) dt+D∫DBf(t) dt=c D.\int_0^D (t + B) f(t)\,dt + D \int_D^B f(t)\,dt = c\,D .

Differentiating once in DD gives Bf(D)+∫DBf(t) dt=cB f(D) + \int_D^B f(t)\,dt = c, and once more gives Bf′(D)−f(D)=0B f'(D) - f(D) = 0. So f(t)=a et/Bf(t) = a\,e^{t/B}, normalization gives a=1B(e−1)a = \frac{1}{B(e - 1)}, and putting D=0D = 0 into the first derivative gives c=B a+1=ee−1c = B\,a + 1 = \frac{e}{e-1}. The skier buys at time

T=Bln⁡(1+U(e−1)),U uniform on [0,1],T = B \ln\bigl(1 + U(e - 1)\bigr), \qquad U \text{ uniform on } [0, 1],

by inverting the distribution function F(t)=(et/B−1)/(e−1)F(t) = (e^{t/B} - 1)/(e - 1).

In C++

Sampling the continuous strategy by inverse transform with a seeded std::mt19937_64, and measuring the expected ratio for several season lengths. The uniform UU is built from the top 53 bits of one 64-bit draw, so the sequence is the same on every standard library.

cpp · try it yourself
#include <cmath>
#include <cstdio>
#include <random>

int main() {
    const double B = 10.0, e = std::exp(1.0);
    std::mt19937_64 rng(42);
    auto uniform = [&]() { return (rng() >> 11) * (1.0 / 9007199254740992.0); };  // [0, 1)
    const int runs = 200000;
    std::printf("e/(e - 1) = %.4f\n", e / (e - 1));
    for (double D : {1.0, 4.0, 10.0, 30.0}) {
        double total = 0;
        for (int i = 0; i < runs; ++i) {
            double T = B * std::log(1 + uniform() * (e - 1));   // buying time
            total += (D <= T) ? D : T + B;                        // rented until D, or until T then bought
        }
        double opt = std::fmin(D, B);
        std::printf("season %4.1f: E[cost] = %7.4f, OPT = %4.1f, ratio %.4f\n", D, total / runs, opt, total / runs / opt);
    }
}
e/(e - 1) = 1.5820
season  1.0: E[cost] =  1.5811, OPT =  1.0, ratio 1.5811
season  4.0: E[cost] =  6.3161, OPT =  4.0, ratio 1.5790
season 10.0: E[cost] = 15.8231, OPT = 10.0, ratio 1.5823
season 30.0: E[cost] = 15.8227, OPT = 10.0, ratio 1.5823

Every season length gives a ratio near 1.5821.582; short seasons as well as long ones.

Optimality: Yao's principle

Theorem (Karlin, Manasse, McGeoch and Owicki, 1990). No randomized ski-rental strategy has strict ratio below cB=1/(1−(1−1/B)B)c_B = 1/(1 - (1 - 1/B)^B) against an oblivious adversary in the discrete problem, and below ee−1\frac{e}{e-1} in the continuous problem.

By Yao's principle, it suffices to find a distribution over seasons on which every deterministic strategy jj pays at least cBc_B times the expected optimum. Equalize again, now on the adversary's side. The adversary uses seasons D=1,…,B−1D = 1, \ldots, B - 1 and one long season LL (any length at least 2B2B), with probabilities q1,…,qB−1,qLq_1, \ldots, q_{B-1}, q_L. Strategies j>Bj > B are worse than j=Bj = B on this support, so only j=1,…,Bj = 1, \ldots, B matter. Moving the buying day from jj to j+1j + 1 saves B−1B - 1 on the season D=jD = j and costs one more day of rent on every longer season, so the deterministic strategies all cost the same exactly when

(B−1) qj=Pr⁡[D>j],j=1,…,B−1.(B - 1)\,q_j = \Pr[D > j], \qquad j = 1, \ldots, B - 1 .
python · try it yourself
from fractions import Fraction

B = 10
# solve backwards: Pr[D > B-1] = q_L; q_j = Pr[D > j] / (B - 1)
q = {"L": Fraction(1)}
tail = Fraction(1)                                # Pr[D > j], unnormalized
for j in range(B - 1, 0, -1):
    q[j] = tail / (B - 1)
    tail += q[j]
total = sum(q.values())
q = {d: x / total for d, x in q.items()}

def det_cost(j, D):                               # D = "L" is a season longer than every j <= B
    if D == "L" or D >= j:
        return (j - 1) + B
    return D

e_opt = sum(x * (B if d == "L" else d) for d, x in q.items())
costs = [sum(x * det_cost(j, d) for d, x in q.items()) for j in range(1, B + 1)]
print("E[ALG_j] for j = 1..B all equal:", len(set(costs)) == 1)
print(f"E[ALG]/E[OPT] = {float(costs[0] / e_opt):.6f}")
print(f"c_B           = {1 / (1 - (1 - 1 / B) ** B):.6f}")
print(f"Pr[long season] = {float(q['L']):.4f}, Pr[D = 1] = {float(q[1]):.4f}")
E[ALG_j] for j = 1..B all equal: True
E[ALG]/E[OPT] = 1.535340
c_B           = 1.535340
Pr[long season] = 0.3874, Pr[D = 1] = 0.1000

The adversary's distribution makes every deterministic strategy pay exactly cB⋅E[OPT]c_B \cdot \mathbb{E}[\mathrm{OPT}], so by Yao's principle no randomized strategy beats cBc_B, and the distribution pp derived above is optimal. In continuous time the adversary's distribution becomes a density ∝e−t/B\propto e^{-t/B} on [0,B)[0, B) plus an atom at a long season, and the bound is exactly ee−1\frac{e}{e - 1}.

Watching it in the lab

The lab's strategy 3 is the randomized discrete strategy. Its chart shows the expected cost over OPT for every season length: flat at cBc_B up to D=BD = B, then falling, because long seasons only add to OPT's side. Compare with strategy 0 (break-even), whose curve peaks at 2−1/B2 - 1/B.

Season of 10 days, buying costs 10rent (1)buy (10)own (0)randomized: costs are expected values12341B = 1040season length Dcost / OPT

Test yourself

In the optimal discrete strategy, how do pi+1p_{i+1} and pip_i compare?

  1. pi+1=pip_{i+1} = p_i
  2. pi+1=BB−1 pip_{i+1} = \frac{B}{B-1}\,p_i
  3. pi+1=12 pip_{i+1} = \frac{1}{2}\,p_i
  4. pi+1=e pip_{i+1} = e\,p_i

What density does the optimal continuous strategy use for its buying time on [0,B][0, B]?

  1. Uniform
  2. Proportional to et/Be^{t/B}
  3. Proportional to e−t/Be^{-t/B}
  4. A point mass at BB

Why does the randomized strategy not contradict the deterministic lower bound 2−1/B2 - 1/B?

  1. It does contradict it
  2. That bound is for deterministic strategies, whose buying day the adversary knows; the oblivious adversary only knows the distribution
  3. Randomized strategies are measured with additive constants
  4. The randomized strategy rents forever

How is the optimality of ee−1\frac{e}{e-1} proved?

  1. By an adaptive adversary
  2. By Yao's principle with a season distribution on which every deterministic strategy pays ee−1\frac{e}{e-1} times the expected optimum
  3. By the minimax theorem applied to running times
  4. It is an open problem