numeraire.core.data.CrossSectionView#

class numeraire.core.data.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: object

A cross-sectional (panel) view: many assets with per-asset characteristics, ragged over time.

The sibling of TimeSeriesView for the cross-sectional asset-pricing family (Fama-MacBeth, IPCA, characteristic sorts), where the predictor z_{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 at t(ids, X) with X shaped (n_alive x K) — rather than one shared vector.

  • target_asof(t, h) returns each alive asset’s return realized over (t, t+h] (nan if the asset delists before the horizon closes).

  • aligned stacks 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 PIT window/between sub-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 at t); 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’s lag=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].

features_asof(t)

The cross-section known as of t: (ids, X) with X shaped (n_alive x K).

panel_frame()

Tidy long eject: [<chars>, ret] on a (date,asset) MultiIndex over calendar.

target_asof(t[, horizon])

Per-asset return over (t, t+h] for the t cross-section; nan if it delists.

to_tensor()

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

assets

Sorted union of every asset id appearing in this view (report / tensor column axis).

calendar

Rebalancing / observation timestamps (the prediction calendar).

char_names

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).

property char_names: list[str]#

Characteristic (per-asset predictor) column names.

universe(t: object) list[str][source]#

Asset ids alive as of t (empty before the first date).

features_asof(t: object) tuple[NDArray[object_], NDArray[float64]][source]#

The cross-section known as of t: (ids, X) with X shaped (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 the t cross-section; nan if 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.