TExcelTAD

Ski Rental: Rent or Buy?

You are on a ski holiday of unknown length. Renting skis costs 1 per day; buying costs BB. Each morning you choose: rent once more, or buy and never pay again. If you knew the season would last DD days, the choice would be easy, but the snow may melt tomorrow or last all winter. This ski rental problem is the cleanest example of an online decision, and the same rent-or-buy shape appears in systems everywhere: spin on a lock or block the thread, keep a disk spinning or power it down, keep a TCP connection open or close it. This lesson finds the best deterministic strategy, proves that nothing deterministic is better, and shows how randomness improves the ratio from 2 to about 1.58.

The offline optimum

If the season lasts DD days, the cheapest plan is to rent every day when D<BD < B and to buy on day 1 otherwise:

OPT(D)=min⁡(D,B).\mathrm{OPT}(D) = \min(D, B).

An online strategy does not know DD; the season ends at some point and it learns only then. Since every morning looks the same until the season ends, a deterministic strategy is fully described by one number: the day jj on which it buys if the season is still going. Its cost is

ALGj(D)={Dif D<j(the season ended while renting),(j−1)+Bif D≥j(rented j−1 days, then bought).\mathrm{ALG}_j(D) = \begin{cases} D & \text{if } D < j \quad \text{(the season ended while renting)}, \\ (j - 1) + B & \text{if } D \ge j \quad \text{(rented } j - 1 \text{ days, then bought)}. \end{cases}

The lab plays a season day by day. Try the break-even strategy (buy on day BB) and then the two extremes, buying at once and never buying; the chart shows the ratio ALG/OPT\mathrm{ALG}/\mathrm{OPT} for every season length.

Season of 15 days, buying costs 10rent (1)buy (10)own (0)12341B = 1040season length Dcost / OPT

The break-even strategy

Rent until the rent paid would reach the price, then buy: rent on days 1,…,B−11, \ldots, B - 1 and buy on day BB. If the season ends before day BB, this strategy pays exactly what OPT pays. Otherwise it pays (B−1)+B=2B−1(B - 1) + B = 2B - 1 while OPT pays BB, so

ALGB(D)OPT(D)≤2B−1B=2−1B.\frac{\mathrm{ALG}_B(D)}{\mathrm{OPT}(D)} \le \frac{2B - 1}{B} = 2 - \frac{1}{B}.

The program computes, for B=10B = 10, the worst ratio of every buying day jj over all season lengths.

python · try it yourself
from fractions import Fraction

def alg(j, B, D):
    return D if D < j else (j - 1) + B

def opt(B, D):
    return min(D, B)

B = 10
print(" buy on day   worst ratio   worst season")
for j in [1, 3, 5, 8, 9, 10, 11, 12, 15, 20]:
    ratios = [(Fraction(alg(j, B, D), opt(B, D)), D) for D in range(1, 4 * B)]
    worst, D = max(ratios, key=lambda r: (r[0], -r[1]))   # the shortest worst season
    print(f"{j:11} {float(worst):13.3f} {D:14}")
print("2 - 1/B =", 2 - 1 / B)
 buy on day   worst ratio   worst season
          1        10.000              1
          3         4.000              3
          5         2.800              5
          8         2.125              8
          9         2.000              9
         10         1.900             10
         11         2.000             11
         12         2.100             12
         15         2.400             15
         20         2.900             20
2 - 1/B = 1.9

Buying on day 1 is ruinous if the season lasts a single day (ratio 10), and buying late is ruinous if the season ends the day after the purchase. Day BB balances the two dangers.

No deterministic strategy is better

Theorem. Every deterministic ski-rental strategy has competitive ratio at least 2−1/B2 - 1/B.

Proof

The adversary ends the season on the day the strategy buys: D=jD = j. The strategy pays (j−1)+B(j - 1) + B, and OPT pays min⁡(j,B)\min(j, B).

  • If j≤Bj \le B: the ratio is (j−1+B)/j=1+(B−1)/j≥1+(B−1)/B=2−1/B(j - 1 + B)/j = 1 + (B - 1)/j \ge 1 + (B - 1)/B = 2 - 1/B.
  • If j>Bj > B: the ratio is (j−1+B)/B≥(B+B)/B=2(j - 1 + B)/B \ge (B + B)/B = 2.

A strategy that never buys is beaten by a long season: its ratio D/BD/B grows without bound. In every case the ratio is at least 2−1/B2 - 1/B.

The proof works because a deterministic player is predictable: the adversary knows the buying day and stops the season exactly then. A randomized player can hide its buying day.

Randomized ski rental

Suppose the strategy picks its buying day JJ at random from a distribution on 1,…,B1, \ldots, B, and the adversary, who knows the distribution but not the outcome, picks DD. The best distribution makes the expected ratio the same for every D≤BD \le B; solving those equations gives

Pr⁡[J=i]=(1−1/B)B−iB(1−(1−1/B)B),i=1,…,B,\Pr[J = i] = \frac{(1 - 1/B)^{B - i}}{B\left(1 - (1 - 1/B)^{B}\right)}, \qquad i = 1, \ldots, B,

later days being more likely. Its expected cost is at most 11−(1−1/B)B\frac{1}{1 - (1 - 1/B)^B} times OPT for every DD, and as BB grows this tends to

11−1/e=ee−1≈1.582.\frac{1}{1 - 1/e} = \frac{e}{e - 1} \approx 1.582 .

The program computes the exact expected ratio for every season length with fractions, finds the worst one and compares it with the formula.

python · try it yourself
from fractions import Fraction
from math import e

def buy_distribution(B):
    q = Fraction(B - 1, B)
    weights = [q ** (B - i) for i in range(1, B + 1)]
    total = sum(weights)
    return [w / total for w in weights]           # P(J = 1), ..., P(J = B)

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

for B in [2, 4, 10, 30]:
    dist = buy_distribution(B)
    worst = max(expected_cost(dist, B, D) / min(D, B) for D in range(1, 3 * B))
    formula = 1 / (1 - (1 - 1 / B) ** B)
    print(f"B = {B:2}: worst expected ratio {float(worst):.4f}, formula {formula:.4f}, "
          f"deterministic {2 - 1 / B:.4f}")
print(f"limit e/(e - 1) = {e / (e - 1):.4f}")
dist = buy_distribution(10)
print("B = 10, P(buy on day i):", " ".join(f"{float(p):.3f}" for p in dist))
B =  2: worst expected ratio 1.3333, formula 1.3333, deterministic 1.5000
B =  4: worst expected ratio 1.4629, formula 1.4629, deterministic 1.7500
B = 10: worst expected ratio 1.5353, formula 1.5353, deterministic 1.9000
B = 30: worst expected ratio 1.5666, formula 1.5666, deterministic 1.9667
limit e/(e - 1) = 1.5820
B = 10, P(buy on day i): 0.059 0.066 0.073 0.082 0.091 0.101 0.112 0.124 0.138 0.154

For every BB the worst expected ratio equals the formula exactly: the distribution is designed so that the adversary gains nothing by choosing one season length over another. Yao's minimax principle shows that no randomized strategy beats this against an oblivious adversary, so e/(e−1)e/(e-1) is the true randomized competitive ratio in the limit. The lab's fourth strategy plays this distribution.

In C++

The worst-case ratios of the break-even strategy and of the randomized strategy, for several prices, computed in floating point over every season length up to 3B3B.

cpp · try it yourself
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>

int main() {
    std::printf("  B   break-even   randomized   1/(1-(1-1/B)^B)\n");
    for (int B : {2, 5, 10, 20, 100}) {
        double q = 1.0 - 1.0 / B, norm = B * (1 - std::pow(q, B));
        std::vector<double> p(B + 1);
        for (int i = 1; i <= B; i++) p[i] = std::pow(q, B - i) / norm;
        double worst_det = 0, worst_rand = 0;
        for (int D = 1; D < 3 * B; D++) {
            double opt = std::min(D, B);
            double det = D < B ? D : (B - 1) + B;
            double exp_cost = 0;
            for (int i = 1; i <= B; i++) exp_cost += p[i] * (D < i ? D : (i - 1) + B);
            worst_det = std::max(worst_det, det / opt);
            worst_rand = std::max(worst_rand, exp_cost / opt);
        }
        std::printf("%3d %12.4f %12.4f %17.4f\n", B, worst_det, worst_rand, 1 / (1 - std::pow(q, B)));
    }
}
  B   break-even   randomized   1/(1-(1-1/B)^B)
  2       1.5000       1.3333            1.3333
  5       1.8000       1.4874            1.4874
 10       1.9000       1.5353            1.5353
 20       1.9500       1.5588            1.5588
100       1.9900       1.5774            1.5774

Rent or buy in real systems

  • Spin or block. A thread waiting for a lock can spin (cost: time spent) or block (cost: a fixed context-switch cost CC). Spinning for time CC and then blocking is 2-competitive, which is why many mutex implementations spin briefly before sleeping.
  • Power management. A disk or radio idles at some cost per second, while switching it off and on again has a fixed cost; staying on for the break-even time first is the same rule.
  • Caching decisions with a fixed fetch cost, and TCP acknowledgement delays, have the same structure.

Ski rental, paging and list update (List Update and Move-to-Front) are the three opening examples of Online Algorithms & Competitive Analysis, which extends them to rent-or-buy with many items, the kk-server problem and online learning.

Test yourself

Buying costs B=8B = 8 and renting 1 per day. What does the break-even strategy pay if the season lasts 20 days?

  1. 8
  2. 15
  3. 16
  4. 20

In the lower-bound proof for deterministic strategies, which season length does the adversary choose?

  1. One day
  2. BB days
  3. Exactly the day the strategy buys
  4. An infinite season

What is the best competitive ratio of a randomized ski-rental strategy against an oblivious adversary, as B→∞B \to \infty?

  1. 11
  2. ee−1≈1.58\frac{e}{e - 1} \approx 1.58
  3. 22
  4. e≈2.72e \approx 2.72

What is the worst ratio of the strategy that buys on day 1, with B=10B = 10?

  1. 1
  2. 2
  3. 5.5
  4. 10