{
  "schema_version": "1.0.0",
  "ledger_id": "signal-quest-lecture-masterclass-worked-examples",
  "canonical_manuscript": "docs/textbook/btc-polymarket-ml/full-textbook/lecture-masterclass-manuscript.md",
  "epistemic_contract": "Every entry is illustrative. No entry is a measured market, model, latency, fill, calibration, or performance result.",
  "examples": [
    {
      "id": "SQ-EX-001",
      "title": "Binary settlement label under an equality-inclusive contract",
      "inputs": {
        "start_value": 100,
        "end_value": 101,
        "equality_rule": "end_value >= start_value",
        "equality_case_end_value": 100
      },
      "formula_or_algorithm": "label = 1 if end_value >= start_value else 0",
      "exact_expected_outputs": {
        "primary_label": 1,
        "equality_case_label": 1,
        "strict_greater_than_equality_case_label": 0
      },
      "epistemic_status": "illustrative",
      "manuscript_anchor": "#sq-ex-001",
      "notes": "A unit-test fixture demonstrating that the equality convention is part of the estimand; values are not market observations."
    },
    {
      "id": "SQ-EX-002",
      "title": "Bernoulli log loss with L2 regularization",
      "inputs": {
        "labels": [1, 0, 1, 0],
        "probabilities": [0.8, 0.3, 0.6, 0.2],
        "parameter_vector": [0.4, -0.2],
        "regularization_lambda": 0.1,
        "regularizer": "sum(theta_j^2)"
      },
      "formula_or_algorithm": [
        "mean_log_loss = -mean(y_i * ln(p_i) + (1-y_i) * ln(1-p_i))",
        "l2_squared_norm = sum(theta_j^2)",
        "penalty = regularization_lambda * l2_squared_norm",
        "regularized_objective = mean_log_loss + penalty"
      ],
      "exact_expected_outputs": {
        "mean_log_loss_expression": "-(ln(0.8) + ln(0.7) + ln(0.6) + ln(0.8)) / 4",
        "mean_log_loss_decimal": 0.32844691758328565,
        "l2_squared_norm": 0.2,
        "regularization_penalty": 0.02,
        "regularized_objective_expression": "-(ln(0.8) + ln(0.7) + ln(0.6) + ln(0.8)) / 4 + 0.02",
        "regularized_objective_decimal": 0.34844691758328566
      },
      "epistemic_status": "illustrative",
      "manuscript_anchor": "#sq-ex-002",
      "notes": "Demonstrates the fit-plus-complexity objective; it does not recommend a regularization setting."
    },
    {
      "id": "SQ-EX-003",
      "title": "Temporal purge and embargo derived from support windows",
      "inputs": {
        "lookback_minutes": 3,
        "label_horizon_minutes": 2,
        "candidate_training_decision_minutes": [5, 6, 7, 8, 9],
        "nominal_evaluation_boundary_minute": 10,
        "embargo_minutes": 3,
        "purge_rule": "purge training decision t when t + label_horizon_minutes >= nominal_evaluation_boundary_minute"
      },
      "formula_or_algorithm": [
        "label_support_end(t) = t + 2",
        "surviving training decisions satisfy label_support_end(t) < 10",
        "first eligible evaluation decision = 10 + 3"
      ],
      "exact_expected_outputs": {
        "label_support_ends": [7, 8, 9, 10, 11],
        "surviving_training_decision_minutes": [5, 6, 7],
        "purged_training_decision_minutes": [8, 9],
        "first_eligible_evaluation_decision_minute": 13
      },
      "epistemic_status": "illustrative",
      "manuscript_anchor": "#sq-ex-003",
      "notes": "Durations are teaching values. A real purge and embargo must follow measured lookbacks, horizons, delays, and overlap semantics."
    },
    {
      "id": "SQ-EX-004",
      "title": "Confusion-matrix metrics including MCC",
      "inputs": {
        "true_positives": 30,
        "false_positives": 10,
        "true_negatives": 50,
        "false_negatives": 10
      },
      "formula_or_algorithm": [
        "accuracy = (TP + TN) / (TP + FP + TN + FN)",
        "precision = TP / (TP + FP)",
        "recall = TP / (TP + FN)",
        "specificity = TN / (TN + FP)",
        "false_positive_rate = FP / (FP + TN)",
        "f1 = 2 * TP / (2 * TP + FP + FN)",
        "mcc = (TP * TN - FP * FN) / sqrt((TP + FP)(TP + FN)(TN + FP)(TN + FN))"
      ],
      "exact_expected_outputs": {
        "sample_size": 100,
        "accuracy": 0.8,
        "precision": 0.75,
        "recall": 0.75,
        "specificity_fraction": "5/6",
        "specificity_decimal": 0.8333333333333334,
        "false_positive_rate_fraction": "1/6",
        "false_positive_rate_decimal": 0.16666666666666666,
        "f1": 0.75,
        "mcc_numerator": 1400,
        "mcc_denominator": 2400,
        "mcc_fraction": "7/12",
        "mcc_decimal": 0.5833333333333334
      },
      "epistemic_status": "illustrative",
      "manuscript_anchor": "#sq-ex-004",
      "notes": "One hard-prediction matrix supports multiple questions but says nothing by itself about calibration, temporal validity, or decision value."
    },
    {
      "id": "SQ-EX-005",
      "title": "Threshold sweep, ROC-AUC, and stepwise average precision",
      "inputs": {
        "score_label_pairs_descending": [
          {"score": 0.9, "label": 1},
          {"score": 0.8, "label": 0},
          {"score": 0.7, "label": 1},
          {"score": 0.4, "label": 0},
          {"score": 0.3, "label": 1},
          {"score": 0.1, "label": 0}
        ],
        "thresholds": [0.85, 0.65],
        "positive_rule": "score >= threshold",
        "pr_summary": "stepwise average precision"
      },
      "formula_or_algorithm": [
        "At each threshold, construct the confusion matrix from score >= threshold.",
        "ROC-AUC = positive-negative score wins / all positive-negative pairs; there are no ties.",
        "average_precision = mean(precision at each positive-ranked position)."
      ],
      "exact_expected_outputs": {
        "threshold_0_85": {
          "TP": 1,
          "FP": 0,
          "TN": 3,
          "FN": 2,
          "precision": 1,
          "recall_fraction": "1/3",
          "recall_decimal": 0.3333333333333333,
          "f1": 0.5,
          "false_positive_rate": 0,
          "accuracy_fraction": "2/3",
          "accuracy_decimal": 0.6666666666666666
        },
        "threshold_0_65": {
          "TP": 2,
          "FP": 1,
          "TN": 2,
          "FN": 1,
          "precision_fraction": "2/3",
          "precision_decimal": 0.6666666666666666,
          "recall_fraction": "2/3",
          "recall_decimal": 0.6666666666666666,
          "f1_fraction": "2/3",
          "f1_decimal": 0.6666666666666666,
          "false_positive_rate_fraction": "1/3",
          "false_positive_rate_decimal": 0.3333333333333333,
          "accuracy_fraction": "2/3",
          "accuracy_decimal": 0.6666666666666666
        },
        "positive_negative_pair_wins": 6,
        "positive_negative_pair_count": 9,
        "roc_auc_fraction": "2/3",
        "roc_auc_decimal": 0.6666666666666666,
        "positive_rank_precisions": [1, "2/3", "3/5"],
        "average_precision_expression": "(1 + 2/3 + 3/5) / 3",
        "average_precision_fraction": "34/45",
        "average_precision_decimal": 0.7555555555555555
      },
      "epistemic_status": "illustrative",
      "manuscript_anchor": "#sq-ex-005",
      "notes": "Demonstrates that equal accuracy can hide different operating behavior and that PR integration convention must be named."
    },
    {
      "id": "SQ-EX-006",
      "title": "Reliability-bin gap, Brier score, and log loss",
      "inputs": {
        "probabilities": [0.9, 0.8, 0.4, 0.2],
        "labels": [1, 0, 1, 0],
        "high_probability_bin_indices_zero_based": [0, 1],
        "calibration_gap_sign": "mean prediction minus observed positive frequency"
      },
      "formula_or_algorithm": [
        "brier = mean((p_i - y_i)^2)",
        "log_loss = -mean(y_i * ln(p_i) + (1-y_i) * ln(1-p_i))",
        "bin_gap = mean(p_i in bin) - mean(y_i in bin)"
      ],
      "exact_expected_outputs": {
        "squared_errors": [0.01, 0.64, 0.36, 0.04],
        "squared_error_sum": 1.05,
        "brier_score": 0.2625,
        "log_loss_expression": "-(ln(0.9) + ln(0.2) + ln(0.4) + ln(0.8)) / 4",
        "log_loss_decimal": 0.7135581778200728,
        "high_bin_mean_prediction": 0.85,
        "high_bin_observed_frequency": 0.5,
        "high_bin_gap": 0.35
      },
      "epistemic_status": "illustrative",
      "manuscript_anchor": "#sq-ex-006",
      "notes": "The sample is intentionally too small for an empirical calibration claim; it is arithmetic instruction only."
    },
    {
      "id": "SQ-EX-007",
      "title": "Conservative edge trigger and abstention",
      "inputs": {
        "case_pass": {
          "lower_probability_bound": 0.62,
          "executable_ask": 0.55,
          "fee_allowance": 0.02,
          "slippage_allowance": 0.01,
          "latency_allowance": 0.015,
          "required_margin": 0.01
        },
        "case_abstain": {
          "lower_probability_bound": 0.59,
          "all_other_inputs": "same as case_pass"
        }
      },
      "formula_or_algorithm": [
        "total_cost_allowance = fee_allowance + slippage_allowance + latency_allowance",
        "conservative_edge = lower_probability_bound - executable_ask - total_cost_allowance",
        "numerical_gate_passes when conservative_edge >= required_margin",
        "win_net = 1 - executable_ask - total_cost_allowance",
        "loss_net = -executable_ask - total_cost_allowance",
        "expected_value = p_lower * win_net + (1-p_lower) * loss_net"
      ],
      "exact_expected_outputs": {
        "total_cost_allowance": 0.045,
        "case_pass": {
          "conservative_edge": 0.025,
          "win_net": 0.405,
          "loss_net": -0.595,
          "expected_value": 0.025,
          "numerical_edge_gate": "pass"
        },
        "case_abstain": {
          "conservative_edge": -0.005,
          "numerical_edge_gate": "abstain"
        }
      },
      "epistemic_status": "illustrative",
      "manuscript_anchor": "#sq-ex-007",
      "notes": "Passing the arithmetic gate does not override data, artifact, calibration, liquidity, risk, or authority gates."
    },
    {
      "id": "SQ-EX-008",
      "title": "Additive boosted-tree logit",
      "inputs": {
        "base_logit": 0,
        "tree_leaf_values": [0.8, -0.3],
        "learning_rate": 0.1
      },
      "formula_or_algorithm": [
        "final_logit = base_logit + learning_rate * sum(tree_leaf_values)",
        "probability = 1 / (1 + exp(-final_logit))"
      ],
      "exact_expected_outputs": {
        "tree_contributions": [0.08, -0.03],
        "final_logit": 0.05,
        "probability_expression": "1 / (1 + exp(-0.05))",
        "probability_decimal": 0.5124973964842103
      },
      "epistemic_status": "illustrative",
      "manuscript_anchor": "#sq-ex-008",
      "notes": "Explains additive-logit mechanics without claiming to reproduce a library's full optimizer or calibration."
    },
    {
      "id": "SQ-EX-009",
      "title": "Simplified ordered categorical target statistic",
      "inputs": {
        "category_sequence": ["A", "A", "A"],
        "label_sequence": [1, 0, 1],
        "prior_probability": 0.5,
        "smoothing_weight": 2,
        "ordering": "left to right; current and later labels excluded"
      },
      "formula_or_algorithm": "statistic_i = (sum of earlier same-category labels + smoothing_weight * prior_probability) / (count of earlier same-category labels + smoothing_weight)",
      "exact_expected_outputs": {
        "row_1_expression": "(0 + 2 * 0.5) / (0 + 2)",
        "row_1_statistic": 0.5,
        "row_2_expression": "(1 + 2 * 0.5) / (1 + 2)",
        "row_2_statistic_fraction": "2/3",
        "row_2_statistic_decimal": 0.6666666666666666,
        "row_3_expression": "(1 + 0 + 2 * 0.5) / (2 + 2)",
        "row_3_statistic": 0.5
      },
      "epistemic_status": "illustrative",
      "manuscript_anchor": "#sq-ex-009",
      "notes": "A teaching simplification of ordered target-statistic intuition, not a complete reproduction of CatBoost internals."
    },
    {
      "id": "SQ-EX-010",
      "title": "Additive SHAP-style attribution on the logit scale",
      "inputs": {
        "baseline_logit": -0.2,
        "feature_contributions": [0.5, -0.1, 0.2],
        "output_space": "logit"
      },
      "formula_or_algorithm": [
        "explained_logit = baseline_logit + sum(feature_contributions)",
        "probability = 1 / (1 + exp(-explained_logit))"
      ],
      "exact_expected_outputs": {
        "contribution_sum": 0.6,
        "explained_logit": 0.4,
        "probability_expression": "1 / (1 + exp(-0.4))",
        "probability_decimal": 0.598687660112452
      },
      "epistemic_status": "illustrative",
      "manuscript_anchor": "#sq-ex-010",
      "notes": "An additive accounting example; it makes no causal claim about feature intervention."
    },
    {
      "id": "SQ-EX-011",
      "title": "Masked self-supervised reconstruction loss",
      "inputs": {
        "scalar_token_sequence": [2, 4, 6, 8],
        "masked_position_one_based": 3,
        "masked_target": 6,
        "prediction": 5.5,
        "loss": "squared error at the masked position"
      },
      "formula_or_algorithm": "masked_loss = (prediction - masked_target)^2",
      "exact_expected_outputs": {
        "error": -0.5,
        "squared_reconstruction_loss": 0.25
      },
      "epistemic_status": "illustrative",
      "manuscript_anchor": "#sq-ex-011",
      "notes": "Pretext-task arithmetic only; low reconstruction loss would not establish downstream usefulness."
    },
    {
      "id": "SQ-EX-012",
      "title": "Causal masking and row-wise softmax",
      "inputs": {
        "unmasked_attention_logits": [
          [1, 2, 3],
          [0, 1, 2],
          [2, 1, 0]
        ],
        "mask": "lower triangular; logits above the diagonal become negative infinity"
      },
      "formula_or_algorithm": [
        "masked_logits[t,j] = logits[t,j] when j <= t else -infinity",
        "attention_weights[t] = softmax(masked_logits[t])"
      ],
      "exact_expected_outputs": {
        "masked_logits_symbolic": [
          [1, "-Infinity", "-Infinity"],
          [0, 1, "-Infinity"],
          [2, 1, 0]
        ],
        "attention_weights": [
          [1, 0, 0],
          [0.2689414213699951, 0.7310585786300049, 0],
          [0.6652409557748219, 0.24472847105479764, 0.09003057317038046]
        ],
        "each_row_sum": [1, 1, 1]
      },
      "epistemic_status": "illustrative",
      "manuscript_anchor": "#sq-ex-012",
      "notes": "Verifies masking arithmetic only. It cannot make a future-derived token causally admissible."
    },
    {
      "id": "SQ-EX-013",
      "title": "Replay latency, depth consumption, partial fill, and fee",
      "inputs": {
        "intent_units": 120,
        "ask_levels_before_latency": [
          {"price": 0.54, "units": 40},
          {"price": 0.56, "units": 35},
          {"price": 0.6, "units": 50}
        ],
        "units_removed_from_first_level_during_latency": 20,
        "fee_rate_on_gross_acquisition_cost": 0.01,
        "settlement_payout_per_filled_unit": 1
      },
      "formula_or_algorithm": [
        "Consume remaining ask depth from best to worse until intent is filled or visible depth is exhausted.",
        "gross_cost = sum(filled_units_at_level * level_price)",
        "vwap = gross_cost / total_filled_units",
        "fee = fee_rate * gross_cost",
        "net_settlement_result = payout_per_unit * total_filled_units - gross_cost - fee"
      ],
      "exact_expected_outputs": {
        "post_latency_ask_levels": [
          {"price": 0.54, "units": 20},
          {"price": 0.56, "units": 35},
          {"price": 0.6, "units": 50}
        ],
        "fills": [
          {"price": 0.54, "units": 20, "cost": 10.8},
          {"price": 0.56, "units": 35, "cost": 19.6},
          {"price": 0.6, "units": 50, "cost": 30}
        ],
        "total_filled_units": 105,
        "unfilled_units": 15,
        "gross_acquisition_cost": 60.4,
        "vwap_fraction": "60.4/105",
        "vwap_decimal": 0.5752380952380952,
        "fee": 0.604,
        "total_acquisition_cost": 61.004,
        "gross_settlement_payout": 105,
        "net_settlement_result": 43.996
      },
      "epistemic_status": "illustrative",
      "manuscript_anchor": "#sq-ex-013",
      "notes": "A paper-replay fixture, not an observed fill or profitability result. Queue priority, impact, and production mechanics are not claimed."
    },
    {
      "id": "SQ-EX-014",
      "title": "Monitoring freshness predicate and bounded containment",
      "inputs": {
        "freshness_budget_seconds": 2,
        "last_valid_receive_time": "10:00:00.0",
        "monitor_evaluation_time": "10:00:03.2",
        "stale_predicate": "evaluation_time - last_valid_receive_time > freshness_budget_seconds"
      },
      "formula_or_algorithm": [
        "observed_staleness_seconds = evaluation_time - last_valid_receive_time",
        "budget_excess_seconds = observed_staleness_seconds - freshness_budget_seconds",
        "If stale, execute the allowlisted reversible containment path and escalate."
      ],
      "exact_expected_outputs": {
        "observed_staleness_seconds": 3.2,
        "budget_excess_seconds": 1.2,
        "stale_predicate_result": true,
        "policy_result": "abstain",
        "containment_actions": [
          "stop affected data consumer",
          "mark dependent policy evaluations ineligible",
          "record predicate evidence",
          "verify containment",
          "escalate to data-contract owner"
        ]
      },
      "epistemic_status": "illustrative",
      "manuscript_anchor": "#sq-ex-014",
      "notes": "A runbook teaching trace, not a production freshness budget or observed incident. No remediation or execution authority is granted."
    }
  ]
}
