#!/usr/bin/env python3 from __future__ import annotations import json import math import os from typing import Iterable from seed_common import run_seed STATISTICAL_PRIORS_PATH = os.path.join( os.path.dirname(__file__), "datasets", "statistical_reasoning.json" ) def _load_priors() -> list[dict[str, object]]: try: with open(STATISTICAL_PRIORS_PATH, "r", encoding="utf-8") as handle: payload = json.load(handle) except (FileNotFoundError, json.JSONDecodeError): # pragma: no cover - offline fallback payload = [] if isinstance(payload, list): return [entry for entry in payload if isinstance(entry, dict)] return [] def gen_statistics(limit: int) -> Iterable[str]: priors = _load_priors() if not priors: priors = [ { "feature": "omni-channel", "feature_type": "channel", "mean_roi": 2.2, "std_roi": 0.5, "mean_ctr": 0.038, "std_ctr": 0.012, "sample_size": 150, "weight": 0.8, "baseline_budget": 20000, "notes": "Fallback statistical prior when curated dataset missing.", } ] scenarios = [ "baseline", "aggressive", "defensive", "innovation", "efficiency", "expansion", "stabilization", "premium", "retention", "accelerated", ] confidence_levels = [0.68, 0.8, 0.9, 0.95] budget_multipliers = [0.75, 0.9, 1.0, 1.1, 1.25] produced = 0 for prior in priors: feature = prior.get("feature", "unspecified") feature_type = str(prior.get("feature_type", "feature")) mean_roi = float(prior.get("mean_roi", 2.0)) std_roi = float(prior.get("std_roi", 0.5)) mean_ctr = float(prior.get("mean_ctr", 0.03)) std_ctr = float(prior.get("std_ctr", 0.01)) sample_size = int(prior.get("sample_size", 100)) weight = float(prior.get("weight", 0.75)) baseline_budget = float(prior.get("baseline_budget", 20000)) notes = str(prior.get("notes", "Statistical benchmark from curated marketing runs.")) for scenario in scenarios: for confidence in confidence_levels: z_score = { 0.68: 0.994, 0.8: 1.282, 0.9: 1.645, 0.95: 1.96, }.get(confidence, 1.96) margin_roi = round(z_score * std_roi / math.sqrt(max(sample_size, 1)), 3) margin_ctr = round(z_score * std_ctr / math.sqrt(max(sample_size, 1)), 4) for multiplier in budget_multipliers: scenario_budget = round(baseline_budget * multiplier, 2) projected_revenue = round(scenario_budget * mean_roi, 2) statement = ( "Statistical prior: {scenario} strategy for {feature_type} " "'{feature}' targets ROI {mean_roi:.3f}x ±{margin_roi:.3f} ({confidence:.0%} CI) " "and CTR {mean_ctr:.4f} ±{margin_ctr:.4f} from {sample_size} runs; " "allocate ${budget:,.2f} to unlock ${revenue:,.2f} with weights {weight:.2f}. Notes: {notes}" ).format( scenario=scenario.title(), feature_type=feature_type, feature=feature, mean_roi=mean_roi, margin_roi=margin_roi, confidence=confidence, mean_ctr=mean_ctr, margin_ctr=margin_ctr, sample_size=sample_size, budget=scenario_budget, revenue=projected_revenue, weight=weight, notes=notes, ) yield statement produced += 1 if produced >= limit: return if __name__ == "__main__": run_seed("statistics", gen_statistics)