"""Reproduce the illustrative weekly trend scan with Python 3 (standard library).""" import json import math import random WEEKS = 48 MISSING = (8, 19, 33, 42) # Calendar week numbers, starting at 1. WINDOW = 12 RHO = 0.5 SIMULATIONS = 9_999 EXAMPLE_SEED = 20261110 SIMULATION_SEED = 20261111 def null_series(rng): """Draw a stationary, zero-mean Gaussian AR(1) series of unit variance.""" values = [rng.gauss(0, 1)] innovation_sd = math.sqrt(1 - RHO ** 2) for _ in range(1, WEEKS): values.append(RHO * values[-1] + innovation_sd * rng.gauss(0, 1)) return values def window_slope(first_week): """Return the OLS slope coefficients and their slope's SD under the null.""" weeks = [w for w in range(first_week, first_week + WINDOW) if w not in MISSING] mean_week = sum(weeks) / len(weeks) offsets = [w - mean_week for w in weeks] sum_squares = sum(offset ** 2 for offset in offsets) weights = [(week - 1, offset / sum_squares) for week, offset in zip(weeks, offsets)] # For slope b = w'Y, Var(b) = w'Cov(Y)w. Indices retain the calendar gaps. variance = sum(a * b * RHO ** abs(i - j) for i, a in weights for j, b in weights) return weights, math.sqrt(variance) def scan(values, windows): scores = [] for weights, slope_sd in windows: slope = sum(values[i] * weight for i, weight in weights) scores.append(slope / slope_sd) return scores def histogram(peaks, observed): """Half-unit bins with an upper edge beyond every simulated and observed peak.""" width = 0.5 low = math.floor(min(min(peaks), observed) / width) high = math.floor(max(max(peaks), observed) / width) + 1 counts = [0] * (high - low) for peak in peaks: counts[math.floor(peak / width) - low] += 1 return {"edges": [i * width for i in range(low, high + 1)], "counts": counts} def exceedance_interval(count, trials): """Two-sided 95% Clopper–Pearson interval for the model tail probability.""" def solve_cdf(k, target): # Invert P(X <= k) for X ~ Binomial(trials, p). Log probabilities keep # the small tail accurate even when the midpoint is far from the root. log_choose = [math.lgamma(trials + 1) - math.lgamma(i + 1) - math.lgamma(trials - i + 1) for i in range(k + 1)] low, high = 0.0, 1.0 for _ in range(52): p = (low + high) / 2 log_p, log_q = math.log(p), math.log1p(-p) cdf = math.fsum(math.exp(c + i * log_p + (trials - i) * log_q) for i, c in enumerate(log_choose)) if cdf > target: low = p else: high = p return (low + high) / 2 lower = 0.0 if count == 0 else solve_cdf(count - 1, 0.975) upper = 1.0 if count == trials else solve_cdf(count, 0.025) return [lower, upper] def main(): windows = [window_slope(w) for w in range(1, WEEKS - WINDOW + 2)] series = null_series(random.Random(EXAMPLE_SEED)) scores = scan(series, windows) selected = max(range(len(scores)), key=scores.__getitem__) observed = scores[selected] selected_weights, selected_sd = windows[selected] selected_slope = sum(series[i] * weight for i, weight in selected_weights) rng = random.Random(SIMULATION_SEED) null_peaks = [] for _ in range(SIMULATIONS): null_scores = scan(null_series(rng), windows) null_peaks.append(max(null_scores)) global_exceedances = sum(peak >= observed for peak in null_peaks) ranked = sorted(null_peaks) # Nearest-rank quantiles describe the simulated distribution of maxima. quantiles = {str(q): ranked[math.ceil(q * SIMULATIONS) - 1] for q in (0.5, 0.9, 0.95, 0.99)} report = { "example_seed": EXAMPLE_SEED, "simulation_seed": SIMULATION_SEED, "calendar_weeks": WEEKS, "missing_weeks": MISSING, "lag_one_correlation": RHO, "window_length_weeks": WINDOW, "windows_scanned": len(windows), "example_readings": [None if w in MISSING else series[w - 1] for w in range(1, WEEKS + 1)], "selected_weeks_inclusive": [selected + 1, selected + WINDOW], "selected_slope_baseline_sd_per_week": selected_slope, "selected_null_slope_sd_baseline_sd_per_week": selected_sd, "observed_standardized_slope": observed, "window_scores": [{"first_week": week, "standardized_slope": score} for week, score in enumerate(scores, start=1)], # A single pre-specified window has a standard normal score under this model. "fixed_window_one_sided_p": 0.5 * math.erfc(observed / math.sqrt(2)), "null_series": SIMULATIONS, "global_exceedances": global_exceedances, "global_monte_carlo_p": (global_exceedances + 1) / (SIMULATIONS + 1), # Conditional on the observed score; only the simulated records count here. "model_tail_probability_95pct_interval": exceedance_interval( global_exceedances, SIMULATIONS), "null_peak_quantiles": quantiles, "null_peak_histogram": histogram(null_peaks, observed), } print(json.dumps(report, indent=2)) if __name__ == "__main__": main()