quantlab.model.ensemble.average_predictions#

quantlab.model.ensemble.average_predictions(panels)[source]#

Return the mean of the panels’ per-bar cross-sectional z-scores.

Every panel is an xr.Dataset on (timestamp, symbol) with one variable per label, and all panels carry the same variables. The panels are outer-joined on both coordinates. Then, per panel, per variable and per bar, the finite values are z-scored over symbols with ddof=1; a bar with fewer than two finite values or a constant cross-section makes that panel NaN on that bar. Each cell of the result is the mean of the z-scores the panels have there, with equal weights; a cell no panel has a z-score for is NaN.

Parameters:
panelssequence of xr.Dataset

The prediction panels, for example one per ensemble member.

Returns:
xr.Dataset

One float64 variable per label, in the first panel’s variable order, on (timestamp, symbol) over the union of the coordinates.

Raises:
ValueError

If panels is empty or two panels carry different variable sets.

Examples

>>> import numpy as np
>>> import pandas as pd
>>> import xarray as xr
>>> def panel(values):
...     return xr.Dataset(
...         {"ret": (("timestamp", "symbol"), np.array(values))},
...         coords={"timestamp": pd.date_range("2024-01-01", periods=2),
...                 "symbol": ["A", "B", "C"]},
...     )
>>> small = panel([[1.0, 2.0, 3.0], [1.0, 1.0, 1.0]])
>>> large = panel([[300.0, 100.0, 200.0], [10.0, 30.0, 20.0]])
>>> average_predictions([small, large])["ret"].values
array([[ 0. , -0.5,  0.5],
       [-1. ,  1. ,  0. ]])