quantlab.runs.prediction_panel.PredictionPanel#
- class quantlab.runs.prediction_panel.PredictionPanel(predictions, labels)[source]#
Bases:
objectA run’s predictions together with the specs of the labels they predict.
Every backtest run with a model writes the predictions its rule read as a prediction panel (
quantlab.runs.backtest_run.BacktestRun.predictions): one variable per label on(timestamp, symbol), the store’sattrsholdingformat_version(FORMAT_VERSION) andlabels, a JSON list of the specs’ fields. An executor rebuilds the run’s decision inputs, the rule bound to these specs included, withquantlab.portfolio.decision_inputs.DecisionInputs.from_run.- Parameters:
- predictionsxr.Dataset
One variable per label, each on
(timestamp, symbol), named exactly aslabelsname them; NaN where a symbol has no prediction.- labelsSequence[LabelSpec]
The label specs, in the order of the prediction variables; stored as a tuple.
- Raises:
- ValueError
If the label names repeat, the variables are not exactly the label names, or a variable is not on
(timestamp, symbol).
Examples
>>> import numpy as np, pandas as pd, xarray as xr >>> from quantlab.runs.prediction_panel import LabelSpec >>> predictions = xr.Dataset( ... {"ret_5": (("timestamp", "symbol"), np.array([[0.1, -0.2], [0.3, np.nan]]))}, ... coords={"timestamp": pd.date_range("2024-01-02", periods=2), ... "symbol": np.array(["AAA", "BBB"], dtype=object)}, ... ) >>> panel = PredictionPanel(predictions, [LabelSpec("ret_5", "raw", 1, 5)]) >>> panel.labels (LabelSpec(name='ret_5', scale='raw', delay=1, span=5),)
- FORMAT_VERSION = 1#
The version of the store layout
writewrites andreadreads.
- classmethod read(path)[source]#
Read a panel
writestored, into memory.- Parameters:
- pathstr or os.PathLike
Directory of the store.
- Returns:
- PredictionPanel
The predictions, without the store’s
attrs, and the specs.
- Raises:
- FileNotFoundError
If
pathdoes not exist.- ValueError
If the store is not a prediction panel of
FORMAT_VERSION.
Examples
>>> PredictionPanel.read(path).predictions["ret_5"].dims ('timestamp', 'symbol')
- classmethod read_labels(path)[source]#
Read only the label specs of the panel stored at
path.The store is opened lazily and no prediction is loaded.
- Parameters:
- pathstr or os.PathLike
Directory of the store.
- Returns:
- tuple[LabelSpec, …]
The specs, in the order of the prediction variables.
- Raises:
- FileNotFoundError
If
pathdoes not exist.- ValueError
If the store is not a prediction panel of
FORMAT_VERSION.
Examples
>>> PredictionPanel.read_labels(path) (LabelSpec(name='ret_5', scale='raw', delay=1, span=5),)
- write(path)[source]#
Write the panel to the Zarr store
path, replacing any store there.The variables are written as they are; the store’s
attrsare exactlyformat_versionandlabels(the specs’ fields as a JSON list).- Parameters:
- pathstr or os.PathLike
Directory of the store,
<run_dir>/predictions.zarrin a run.
- Returns:
- Path
path.
Examples
>>> import tempfile >>> path = panel.write(Path(tempfile.mkdtemp()) / "panel.zarr") >>> PredictionPanel.read(path).labels == panel.labels True