05 · how I'd run it on real data
swap synthetic draws for telemetry
Illustrative table names only. The idea: fit the size and reuse distributions from Preview telemetry, then run the same importance-sampling estimate on every candidate budget before choosing the GA default.
SQL · per-session cache demand from Preview sessions
-- illustrative schema: one row per cache() call during the Preview
WITH per_session AS (
SELECT session_id,
COUNT(*) AS dfs_cached,
SUM(cached_bytes) / POW(1024, 3) AS demand_gb,
AVG(reuse_count) AS avg_reuse,
SUM(recompute_ms_avoided) / 6e4 AS minutes_saved
FROM preview_telemetry.cache_events
WHERE event_date >= current_date() - INTERVAL 28 DAYS
GROUP BY session_id
)
SELECT percentile_approx(demand_gb, array(0.5, 0.9, 0.99, 0.999)) AS demand_q,
AVG(LN(demand_gb / dfs_cached)) AS mu_log_size,
STDDEV(LN(demand_gb / dfs_cached)) AS sigma_log_size,
AVG(avg_reuse) AS mean_reuse,
AVG(minutes_saved) AS avg_minutes_saved
FROM per_session;
Python · importance-sampled crash rate for each candidate budget
import numpy as np
rng = np.random.default_rng(2026)
def crash_rate_is(mu, sigma, lam, budget_gb, delta=1.2, n=200_000):
k = 1 + rng.poisson(lam - 1, n) # DataFrames per session
logs = rng.normal(mu + delta, sigma, k.sum()) # proposal g: shifted log-sizes
sid = np.repeat(np.arange(n), k)
demand = np.bincount(sid, weights=np.exp(logs))
# log w = sum over DFs of [(x-mu-delta)^2 - (x-mu)^2] / (2 sigma^2)
logw = np.bincount(sid, weights=((logs-mu-delta)**2 - (logs-mu)**2) / (2*sigma**2))
w = np.exp(logw)
hit = w * (demand > 3 * budget_gb)
return hit.mean(), hit.std() / np.sqrt(n), w.mean() # estimate, SE, sanity (≈1)
fit = spark.table("pm_sandbox.cache_fit").first() # mu, sigma, lam from the SQL above
for b in [4, 8, 16, 32]:
p, se, wbar = crash_rate_is(fit.mu, fit.sigma, fit.lam, b)
print(f"{b:>3} GB crash/10k = {1e4*p:.3f} ± {1e4*se:.3f} mean w = {wbar:.2f}")