Skip to content

contango.trading.analysis.graphing.metric_distribution

contango.trading.analysis.graphing.metric_distribution

MetricDistribution

MetricDistribution(config: C)

Bases: GraphType[MetricDistributionConfig]

A graph type that shows the distribution of a metric grouped by a parameter.

Source code in src\contango\trading\analysis\graph_type.py
26
27
28
29
30
31
32
33
def __init__(self, config: C) -> None:
    """
    Initializes `GraphType`.

    Args:
        config: The matching configuration for the graph type.
    """
    self._config = config

build

build(df: DataFrame) -> go.Figure

Builds a distribution plot of target_metric grouped by group_by.

Parameters:

Name Type Description Default
df DataFrame

The experiment DataFrame.

required

Returns:

Type Description
Figure

A plotly Figure.

Source code in src\contango\trading\analysis\graphing\metric_distribution.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def build(self, df: pd.DataFrame) -> go.Figure:
    """
    Builds a distribution plot of target_metric grouped by group_by.

    Args:
        df: The experiment DataFrame.

    Returns:
        A plotly Figure.
    """
    plot_df = df.dropna(subset=[self.config.group_by, self.config.target_metric]).copy()
    plot_df[self.config.group_by] = plot_df[self.config.group_by].astype(str)

    fig_fn = px.box if self.config.plot_type == "box" else px.violin  # type: ignore[unknownMemberType]
    fig = fig_fn(
        plot_df,
        x=self.config.group_by,
        y=self.config.target_metric,
        points="all",
        title=f"Distribution of {self.config.target_metric} by {self.config.group_by}",
        labels={self.config.group_by: self.config.group_by, self.config.target_metric: self.config.target_metric},
    )
    fig.update_layout()  # type: ignore[unknownMemberType]
    return fig

MetricDistributionConfig dataclass

MetricDistributionConfig(group_by: str, target_metric: str = 'total_return', plot_type: str = 'box')

Bases: GraphConfig

A graph configuration specific to the metric distribution graph.

Attributes:

Name Type Description
group_by str

The column to group by, including a parameter column, or a version of one for continuous parameters with various distinct values.

target_metric str

The name for the targeted metric to graph.

plot_type str

The distribution plot style, either "box" or "violin".