본문으로 건너뛰기
라이브러리 문서 전체

완전한 모집단 진단으로 S&P 500 옵션 모델 비교

노트북 Machine Learning for Trading

요약

이 노트북은 불완전하거나 서로 맞지 않는 평가 표본이 결과를 왜곡하지 않도록 S&P 500 옵션 예측 모델을 비교하는 방법을 설명합니다. 각 모델을 계열, 설정, 체크포인트별로 식별하고, 예측이 등록된 적격성 계약을 충족하는지 확인하며, 모집단이 동일한 교차검증 식별자를 공유하는지도 검증합니다. 시퀀스 모델은 과거 관측 이력이 필요해 더 적은 행을 포함할 수 있습니다. 따라서 적격성 그룹이 다른 모델끼리 비교하면 모델 차이와 표본 차이가 뒤섞입니다.

폴드별로 집계한 정보계수(IC) 측정치를 설명하고, 폴드 기반 기술 통계량과 레이블 중첩을 고려해 일별 IC를 사용하는 HAC 보정 검정을 구분합니다. IC는 순위 연관성을 측정하며 보정된 크기나 트레이딩 수익률을 나타내지 않고 모델을 선택하지도 않습니다. 별도의 인과 DML 추정은 처치 효과를 연구하며, HAC 추론과 t 통계량에 기반한 위약 대조를 이용한 반증 검정을 포함합니다. 발췌문에는 수치 결과가 보고되지 않습니다. 예측 결과와 인과 결과는 모두 기술적 분석이며, 전략 선택은 검증 백테스트로 미룹니다. 이때 비용, 회전율, 포지션 규모가 순위에 영향을 줄 수 있습니다.

핵심 아이디어

  • 예측 비교에는 완전한 모집단과 공통 교차검증 식별자가 필요합니다.
  • 적격성 차이로 모델 계열 간 직접 비교에서 행 커버리지의 교란이 생길 수 있습니다.
  • IC는 횡단면 순위 연관성을 측정하며 수익 규모나 수익성을 입증하지 않습니다.
  • 일별 IC 추론은 레이블 중첩에 따른 의존성을 반영하기 위해 HAC 보정을 사용합니다.
  • 인과 DML는 처치 효과를 별도로 추정하며, 어느 진단표도 전략을 선택하지 않습니다.

태그

전문
# S&P 500 Options: Model Analysis


# S&P 500 Options: Model Analysis

This notebook describes the complete official validation prediction populations produced by the
model execution notebooks. Model identity always includes family, configuration, and checkpoint.
Every comparison requires identical expected prediction keys and the same cross-validation
identity. Information coefficient and related diagnostics do not select a model or checkpoint.

Causal DML is reported separately because it estimates a treatment effect rather than a
cross-sectional prediction configuration.

**Why completeness is established before any number is read.** Every table here is a comparison,
and a comparison across populations that do not cover the same rows is not a comparison at all:
a model scored on an easier subset of the panel looks better for a reason that has nothing to do
with the model. So the notebook refuses before it reports. It requires each prediction to be
complete against its own registered eligibility contract, and requires the four populations to
share one cross-validation identity, because two models cut on different folds have seen
different training data and their diagnostics are not on one scale.

**What a reader gets from this page.** A description of what was fitted and how the resulting
predictions behave, at the grain the pipeline actually decides on, which is the configuration
and its checkpoint together. What a reader does not get is a winner. Nothing here is ranked and
nothing here is chosen; that happens downstream, on backtests, and the separation is deliberate.

```python
"""Analyze complete S&P 500 options model populations."""

import plotly.graph_objects as go
import polars as pl

from case_studies.research import CausalResult, PredictionResult, Result
from case_studies.sp500_options.research_workflow import (
    official_prediction_catalog,
    open_study,
)
from case_studies.utils.registry.completeness import (
    key_digest_value,
    require_comparable_key_digests,
)

MODEL_POPULATIONS = (
    "sp500-options-linear-validation-v1",
    "sp500-options-gbm-validation-v1",
    "sp500-options-tabular-dl-validation-v1",
    "sp500-options-sequence-validation-v1",
)
```

```python
EXECUTION_TIER = "canonical"
WORKSPACE: str = ""
```

## Complete population and its eligibility groups

Named immutable populations replace hash lists and registry-presence filters. The coverage table
comes from each prediction result's registered eligibility contract.

The four populations share one CV identity but not one eligibility set. A sequence model scores a
symbol only after its lookback window is available, so it is eligible on fewer symbols and fewer
rows than the cross-sectional families reading the same panel. The audit below reports one row per
eligibility group rather than asserting a single one, and the diagnostics that follow are read
within a group.

```python
study = open_study(execution_tier=EXECUTION_TIER, workspace=WORKSPACE or None)
catalog = official_prediction_catalog(study, MODEL_POPULATIONS)

coverage_rows = []
for prediction_hash in catalog.get_column("prediction_hash"):
    result = Result.open(study, prediction_hash)
    if not isinstance(result, PredictionResult):
        raise TypeError(f"{prediction_hash} is not a prediction result")
    coverage = result.coverage()
    if coverage is None or coverage["status"] != "complete":
        raise RuntimeError(f"prediction {prediction_hash} has incomplete coverage")
    coverage_rows.append(
        {
            "prediction_hash": prediction_hash,
            "expected_key_digest": coverage["expected_key_digest"],
            "n_expected": coverage["n_expected"],
            "n_actual": coverage["n_actual"],
            "n_folds": coverage["n_folds_actual"],
        }
    )
coverage = pl.DataFrame(coverage_rows)

cv_identities = catalog.get_column("cv_identity").drop_nulls()
if cv_identities.is_empty() or cv_identities.n_unique() != 1:
    raise RuntimeError("official model populations do not share one CV identity")
```

Eligibility is grouped, not assumed identical. A sequence model needs a lookback window before it
can score a symbol, so it is eligible on strictly fewer rows than a cross-sectional model reading
the same panel - which is a property of the model class, not a defect. Requiring one eligibility
digest across all four populations would fail here for a correct run. What must hold is that every
prediction sharing an eligibility contract agrees on its dimensions exactly.

**The grouping is what makes the diagnostics readable rather than misleading.** Two checkpoints
in the same group were scored on identical rows, so the difference between their numbers is the
models. Two checkpoints in different groups were not, and the difference between those numbers
is the models and the rows together, with no way to separate them from this table. The audit
below prints one row per group so that a reader can see which comparisons are available before
making one, rather than discovering afterwards that a sequence model was scored on the subset
of the panel where a lookback window existed.

```python
coverage = coverage.join(
    catalog.select("prediction_hash", "family"), on="prediction_hash", how="left"
)
# The grouping below is only meaningful within one key rendering. Two digests taken under
# different renderings are unequal whatever their key sets, so a mixed population reports more
# distinct eligibility contracts than exist and tells a reader that two checkpoints scored on
# identical rows are not comparable - which the dimension check on each group cannot catch,
# because the split halves agree on every dimension.
require_comparable_key_digests(
    coverage.get_column("expected_key_digest"), what="this notebook's model populations"
)
# Grouped on the digest value rather than the stored string. A row written before the rendering
# was stamped and a row written after it carry the same key set as `<value>` and `k2:<value>`,
# and grouping the strings would split one contract in two for no reason a reader could see -
# the same mis-grouping the check above refuses, arriving through the prefix instead.
coverage = coverage.with_columns(
    pl.col("expected_key_digest")
    .map_elements(key_digest_value, return_dtype=pl.String)
    .alias("eligibility")
)
for digest, group in coverage.group_by("eligibility"):
    if group.select("n_expected", "n_actual", "n_folds").n_unique() != 1:
        raise RuntimeError(
            f"predictions sharing eligibility {digest[0]} disagree on coverage dimensions"
        )
    if group.filter(pl.col("n_actual") != pl.col("n_expected")).height:
        raise RuntimeError(f"eligibility {digest[0]} has predictions short of their declaration")

population_audit = (
    coverage.group_by("eligibility")
    .agg(
        pl.col("family").unique().sort().str.join(", ").alias("families"),
        pl.len().alias("predictions"),
        pl.col("n_expected").first().alias("rows_per_prediction"),
        pl.col("n_folds").first().alias("folds"),
    )
    .with_columns(pl.lit(cv_identities[0]).alias("cv_identity"))
    .sort("rows_per_prediction", descending=True)
)
if population_audit.get_column("predictions").sum() != catalog.height:
    raise RuntimeError("the eligibility audit does not account for every declared prediction")
population_audit
```

## Predictive diagnostics

The table retains each checkpoint as a separate row. It supports descriptive comparison only;
strategy selection occurs after every row has an equal-weight validation backtest.

**What the four columns are, and the grain they are aggregated at.** The information coefficient
is the rank correlation between a model's predictions and the realised label across the symbols
priced at one decision time. Those are averaged within a fold to give the fold's IC, and the
columns here aggregate over *folds*, not over decision times: `ic_mean` is the mean of the fold
ICs, `ic_std` their dispersion across folds, and `pct_positive` the share of folds whose IC came
out above zero.

**`ic_t` is a fold-level diagnostic and is not the significance test.** It divides `ic_mean` by
the standard error implied by that fold-level dispersion, so with a handful of folds it rests on
a handful of numbers and is easily moved by one of them. The inferential statistic is `ic_t_hac`,
computed on the daily IC series with a HAC correction at the label's overlap, because overlapping
labels make neighbouring days dependent and an uncorrected error is too small. Read `ic_t` as a
description of how consistent the folds were, never as evidence that the mean is real.

**Rank correlation is the point of the choice.** It is invariant to any increasing transform of
the predictions, so a model whose values are badly scaled but correctly ordered scores the same
as one that is calibrated, and a squared-error fit is not rewarded for matching the magnitude of
a heavy tail it was never going to match. What that invariance costs is any information about
the size of the move, which is the reason the number below cannot stand in for a return.

**Why none of it selects.** A rank correlation says nothing about whether the ordering survives
position sizing, turnover and cost, and those are what decide whether a strategy makes money.
Selection is therefore by best validation backtest Sharpe, taken over configurations that each
already have an equal-weight backtest, with the checkpoint part of the configuration's identity
rather than a detail of how it was fitted. A high IC here is a reason to look, never a result.

Each IC is computed on its own prediction's eligible rows, so two rows are directly comparable
only when the same eligibility group above covers both. A sequence checkpoint and a linear
checkpoint are scored on different populations, and the difference between their IC values
therefore mixes model behaviour with the population each was scored on.

```python
analysis = (
    catalog.with_columns(
        pl.when(pl.col("checkpoint_value").is_null())
        .then(pl.col("checkpoint_kind").fill_null("final"))
        .otherwise(
            pl.concat_str(
                pl.col("checkpoint_kind"),
                pl.col("checkpoint_value").cast(pl.String),
                separator="=",
            )
        )
        .alias("checkpoint"),
    )
    .with_columns(
        pl.concat_str(
            "family",
            "config_name",
            "checkpoint",
            separator=" / ",
        ).alias("model_identity")
    )
    .select(
        "family",
        "config_name",
        "checkpoint",
        "model_identity",
        "ic_mean",
        "ic_std",
        "ic_t",
        "pct_positive",
        "prediction_hash",
    )
    .sort("family", "config_name", "checkpoint")
)
if analysis.select("ic_mean", "ic_std", "ic_t", "pct_positive").null_count().sum_horizontal().sum():
    raise RuntimeError("official model population has missing regression diagnostics")
analysis
```

```python
family_summary = (
    analysis.group_by("family")
    .agg(
        pl.len().alias("configuration_checkpoints"),
        pl.col("ic_mean").min().alias("ic_min"),
        pl.col("ic_mean").median().alias("ic_median"),
        pl.col("ic_mean").max().alias("ic_max"),
    )
    .sort("family")
)
family_summary
```

```python
fig = go.Figure()
for family in analysis.get_column("family").unique(maintain_order=True):
    rows = analysis.filter(pl.col("family") == family)
    fig.add_trace(
        go.Box(
            name=family,
            y=rows.get_column("ic_mean").to_list(),
            text=rows.get_column("model_identity").to_list(),
            boxpoints="all",
            jitter=0.35,
            pointpos=0,
            hovertemplate="%{text}<br>validation IC %{y:+.4f}<extra></extra>",
        )
    )
fig.add_hline(y=0, line_width=1, line_dash="dot", line_color="#666666")
fig.update_layout(
    title="Validation IC across declared configurations and checkpoints",
    xaxis_title="Model family",
    yaxis_title="Mean daily rank IC",
    showlegend=False,
)
fig.show()
```

## Causal DML artifact

The causal result is not mixed into the predictive population or its checkpoint summaries.

**It answers a different question from everything above.** The models above rank symbols against
each other at a decision time; the estimate below asks what happens to the outcome when the
treatment moves, holding the controls fixed. Double machine learning gets there by fitting two
nuisance models - one predicting the outcome from the controls, one predicting the treatment
from them - and regressing the parts neither explains against each other, so that the effect is
estimated on what is left after the controls are accounted for rather than on the raw series.

**Two standard errors are reported and they are not interchangeable.** The HAC standard error
corrects for the serial correlation that overlapping labels induce, which is what makes the
conventional error too small on this data. The placebo p-value is a permutation test: the
treatment is shuffled in blocks long enough to preserve that serial dependence, the estimate is
recomputed, and the reported value is the share of shuffles whose HAC t-statistic reaches the
observed one. The comparison is on the t-statistic rather than the effect because a permuted
treatment is no longer predictable from the controls, so its residual keeps nearly all its
variance - and that variance is the denominator of the second-stage effect. On the effect scale
every placebo draw is divided by a larger number than the observed one, which narrows the null in
the one direction that makes a refutation read as passed. The t-statistic carries the same
denominator and cancels it. The first asks whether the estimate is distinguishable from zero given
the dependence; the second asks whether the procedure would have produced it from a treatment that
carries no signal.

It is resolved as canonical whatever tier this notebook runs at, because the populations above
are canonical whatever tier this notebook runs at. Asking a preview run for a preview causal
artifact would make the notebook fail unless `10_causal_dml` happened to have run in the same
workspace first, and would pair a preview estimate with canonical predictions if it had.

```python
causal = CausalResult.one(study, label="ret_to_expiry", execution_tier="canonical")
if not causal.complete:
    raise RuntimeError("the causal DML artifact is incomplete")
causal_summary = pl.DataFrame(
    {
        "causal_hash": [causal.hash],
        "treatment": [causal.spec["computation"]["estimand"]["treatment"]],
        "outcome": [causal.spec["computation"]["estimand"]["outcome"]],
        "observations": [causal.metrics["n_obs"]],
        "effect": [causal.metrics["dml_effect"]],
        "hac_standard_error": [causal.metrics["dml_se_hac"]],
        "hac_p_value": [causal.metrics["p_value_hac"]],
        "placebo_p_value": [causal.metrics["refutation_p"]],
    }
)
causal_summary
```

Predictive diagnostics and the causal estimate are now available for reader inspection. Neither
table changes the complete prediction population or selects a strategy.

**Reading the two tables together, and the trap in doing so.** They describe the same data from
two directions, and neither confirms the other. A family can rank symbols well and carry no
causal effect on the treatment studied here, because ranking exploits any stable association
while the estimate is restricted to what survives the controls. The reverse also happens: a
treatment effect that is real and small can be invisible to a rank correlation computed across
a cross-section it barely moves. Agreement between the two is worth noticing and is not
evidence, and disagreement is not a defect in either.

**What carries forward.** Only the population itself. The diagnostics are read and left here;
the next stage takes every configuration in the population, gives each an equal-weight backtest,
and selects on that. Anything a reader concludes from the numbers above should be held until
those backtests exist, because the ordering above and the ordering there routinely differ.
![notebook output](figures/p1_1.png)

출처의 라이선스에 따라 출처를 표시하고 전문을 공개합니다. 라이선스: MIT

이 요약은 원문을 바탕으로 Stratmill의 리서치 에이전트가 작성했으며, 원문을 복사한 것이 아닙니다.