numeraire.CrossSectionView#
- class numeraire.CrossSectionView(panel: DataFrame, *, chars: Sequence[str], ret: str = 'ret', date_col: str = 'date', asset_col: str = 'asset', horizon: int = 1, char_blocks: Sequence[CharBlock] | None = None)[source]#
Bases:
objectA cross-sectional (panel) view: many assets with per-asset characteristics, ragged over time.
The sibling of
TimeSeriesViewfor the cross-sectional asset-pricing family (Fama-MacBeth, IPCA, characteristic sorts), where the predictorz_{i,t}varies by both date and asset — unlike the aggregate/shared predictors of a time-series view. Built from a tidy long panel[date, asset, <chars...>, ret]; the universe may enter/exit (ragged).Shapes (the reason this is a sibling, not a retrofit):
features_asof(t)returns the whole cross-section att—(ids, X)withXshaped(n_alive x K)— rather than one shared vector.target_asof(t, h)returns each alive asset’s return realized over(t, t+h](nanif the asset delists before the horizon closes).alignedstacks every(date, asset)observation into a panel design matrix(N_obs x K)with a(N_obs,)target — the shape Fama-MacBeth / IPCA consume.
Internally stored as a tidy panel sorted by
(date, asset)(date-outer, so a cross-section is a contiguous slice), with assets int-coded against a fixed label axis and a(T x N)row-index matrix (-1= absent) in place of any per-cell lookup: ragged forward-return resolution is a vectorized gather, and PITwindow/betweensub-views are zero-copy prefix slices (a date cutoff keeps a contiguous prefix of the date-sorted rows). Characteristics are taken as known at their row date (period-end info set att); any reporting/publication lag must be applied to the panel before it is handed in (PIT is the provider’s contract, mirroring a time-series block’slag=0).- __init__(panel: DataFrame, *, chars: Sequence[str], ret: str = 'ret', date_col: str = 'date', asset_col: str = 'asset', horizon: int = 1, char_blocks: Sequence[CharBlock] | None = None) None[source]#
Methods
__init__(panel, *, chars[, ret, date_col, ...])aligned([horizon])Panel design over the calendar: stacked
keys[(date,asset)], X (Nobs x K), y (Nobs,).between(start, end)Test-fold view: data truncated to
<= end, calendar restricted to(start, end].The cross-section known as of
t:(ids, X)withXshaped(n_alive x K).Tidy long eject:
[<chars>, ret]on a(date,asset)MultiIndex over calendar.target_asof(t[, horizon])Per-asset return over
(t, t+h]for thetcross-section;nanif it delists.Dense
(T x N x K)eject + an(T x N)presence mask (nan-padded; see PanelTensor).universe(t)Asset ids alive as of
t(empty before the first date).window(end)View restricted to info available up to
end(data and calendar both<= end).Attributes
Sorted union of every asset id appearing in this view (report / tensor column axis).
Rebalancing / observation timestamps (the prediction calendar).
Characteristic (per-asset predictor) column names.
- property calendar: DatetimeIndex#
Rebalancing / observation timestamps (the prediction calendar).
- property assets: list[str]#
Sorted union of every asset id appearing in this view (report / tensor column axis).
- features_asof(t: object) tuple[NDArray[object_], NDArray[float64]][source]#
The cross-section known as of
t:(ids, X)withXshaped(n_alive x K).
- target_asof(t: object, horizon: int | None = None) tuple[NDArray[object_], NDArray[float64]][source]#
Per-asset return over
(t, t+h]for thetcross-section;nanif it delists.
- window(end: object) CrossSectionView[source]#
View restricted to info available up to
end(data and calendar both<= end).
- between(start: object, end: object) CrossSectionView[source]#
Test-fold view: data truncated to
<= end, calendar restricted to(start, end].
- aligned(horizon: int | None = None) tuple[MultiIndex, NDArray[float64], NDArray[float64]][source]#
Panel design over the calendar: stacked
keys[(date,asset)], X (Nobs x K), y (Nobs,).Keeps only observations whose target is realized in-view (horizon purge / no delisting gap) and whose characteristics are all finite (missing-char imputation is the method’s job).
- panel_frame() DataFrame[source]#
Tidy long eject:
[<chars>, ret]on a(date,asset)MultiIndex over calendar.
- to_tensor() PanelTensor[source]#
Dense
(T x N x K)eject + an(T x N)presence mask (nan-padded; see PanelTensor).T= this view’s dates,N= the union asset axis (assets),K= chars.