Ski Rental: Rent or Buy?
You are on a ski holiday of unknown length. Renting skis costs 1 per day; buying costs . Each morning you choose: rent once more, or buy and never pay again. If you knew the season would last 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 days, the cheapest plan is to rent every day when and to buy on day 1 otherwise:
An online strategy does not know ; 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 on which it buys if the season is still going. Its cost is
The lab plays a season day by day. Try the break-even strategy (buy on day ) and then the two extremes, buying at once and never buying; the chart shows the ratio for every season length.
The break-even strategy
Rent until the rent paid would reach the price, then buy: rent on days and buy on day . If the season ends before day , this strategy pays exactly what OPT pays. Otherwise it pays while OPT pays , so
The program computes, for , the worst ratio of every buying day over all season lengths.
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 balances the two dangers.
No deterministic strategy is better
Theorem. Every deterministic ski-rental strategy has competitive ratio at least .
Proof
The adversary ends the season on the day the strategy buys: . The strategy pays , and OPT pays .
- If : the ratio is .
- If : the ratio is .
A strategy that never buys is beaten by a long season: its ratio grows without bound. In every case the ratio is at least .
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 at random from a distribution on , and the adversary, who knows the distribution but not the outcome, picks . The best distribution makes the expected ratio the same for every ; solving those equations gives
later days being more likely. Its expected cost is at most times OPT for every , and as grows this tends to
The program computes the exact expected ratio for every season length with fractions, finds the worst one and compares it with the formula.
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 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 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 .
#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 ). Spinning for time 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 -server problem and online learning.
Test yourself
Buying costs and renting 1 per day. What does the break-even strategy pay if the season lasts 20 days?
- 8
- 15
- 16
- 20
In the lower-bound proof for deterministic strategies, which season length does the adversary choose?
- One day
- days
- Exactly the day the strategy buys
- An infinite season
What is the best competitive ratio of a randomized ski-rental strategy against an oblivious adversary, as ?
What is the worst ratio of the strategy that buys on day 1, with ?
- 1
- 2
- 5.5
- 10