Skip to content

trading.analyzer.graphing.parameter_importance

trading.analyzer.graphing.parameter_importance

build_parameter_importance

build_parameter_importance(df: DataFrame, param_columns: list[str], target_metric: str = 'calmar_ratio') -> go.Figure

Builds the parameter importance bar chart.

Parameters:

Name Type Description Default
df DataFrame

Tidy experiment DataFrame.

required
param_columns list[str]

Parameter columns to score.

required
target_metric str

Metric to explain. Defaults to "calmar_ratio" (risk-adjusted).

'calmar_ratio'

Returns:

Type Description
Figure

A plotly Figure.

Source code in src\contango\trading\analyzer\graphing\parameter_importance.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def build_parameter_importance(
    df: pd.DataFrame,
    param_columns: list[str],
    target_metric: str = "calmar_ratio",
) -> go.Figure:
    """
    Builds the parameter importance bar chart.

    Args:
        df: Tidy experiment DataFrame.
        param_columns: Parameter columns to score.
        target_metric: Metric to explain. Defaults to "calmar_ratio" (risk-adjusted).

    Returns:
        A plotly Figure.
    """
    importance_df = compute_parameter_importance(df, target_metric, param_columns)

    fig = px.bar(   # type: ignore[unknownMemberType]
        importance_df,
        x="importance",
        y="parameter",
        orientation="h",
        title=f"Parameter Importance — Sensitivity of {target_metric} to Each Parameter",
        labels={"importance": "Relative Importance (eta²)", "parameter": "Parameter"},
    )
    fig.update_layout(yaxis={"categoryorder": "total ascending"})  # type: ignore[unknownMemberType]
    return fig

compute_parameter_importance

compute_parameter_importance(df: DataFrame, target_metric: str, param_columns: list[str]) -> pd.DataFrame

Scores each parameter's importance to target_metric using an eta-squared style measure: the share of total variance in the metric explained by grouping on that parameter's values.

Parameters:

Name Type Description Default
df DataFrame

Experiment DataFrame.

required
target_metric str

The metric column to explain (e.g. "calmar_ratio").

required
param_columns list[str]

Parameter columns to score, typically from data_prep.get_param_columns.

required

Returns:

Type Description
DataFrame

DataFrame with columns ["parameter", "importance"], sorted descending.

Source code in src\contango\trading\analyzer\graphing\parameter_importance.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def compute_parameter_importance(
    df: pd.DataFrame,
    target_metric: str,
    param_columns: list[str],
) -> pd.DataFrame:
    """
    Scores each parameter's importance to `target_metric` using an
    eta-squared style measure: the share of total variance in the metric
    explained by grouping on that parameter's values.

    Args:
        df: Experiment DataFrame.
        target_metric: The metric column to explain (e.g. "calmar_ratio").
        param_columns: Parameter columns to score, typically from
            `data_prep.get_param_columns`.

    Returns:
        DataFrame with columns ["parameter", "importance"], sorted descending.
    """
    plot_df = df.dropna(subset=[target_metric])
    grand_mean = plot_df[target_metric].mean()
    ss_total = ((plot_df[target_metric] - grand_mean) ** 2).sum()

    scores: list[dict[str, str | float]] = []
    for param in param_columns:
        group_stats = plot_df.groupby(param)[target_metric].agg(["mean", "count"])
        ss_between = (group_stats["count"] * (group_stats["mean"] - grand_mean) ** 2).sum()
        importance = float(ss_between / ss_total) if ss_total > 0 else 0.0
        scores.append({"parameter": param, "importance": importance})

    return pd.DataFrame(scores).sort_values("importance", ascending=False).reset_index(drop=True)