quantlab.label.predefined.membership_mask.MembershipMaskedLabel#

class quantlab.label.predefined.membership_mask.MembershipMaskedLabel(label, membership)[source]#

Bases: Component

A label that is NaN where the symbol is not an index member at t.

read and compute return the wrapped label’s panel with a cell kept only when the membership panel’s is_member is true on that bar’s date for that symbol. The mask at bar t reads the membership of t’s date and nothing later, so a symbol that leaves the index inside the label’s horizon keeps its return at t, and a symbol that is not a member at t has no label whatever its later membership. Every other label method forwards to the wrapped label unchanged; the wrapper owns no store.

A symbol missing from the membership panel is not a member. A bar whose date the membership panel does not cover (before its first or after its last day) is refused, because unknown membership is not “not a member”.

Parameters:
labelForward

The label to mask, such as quantlab.label.predefined.fret.Return computed on the index’s unmasked prices.

membershipIndexConstituentDataset

The point-in-time membership, with a stored is_member panel on the label’s symbol axis (PERMNOs for CRSP stores).

Attributes:
labelForward

The wrapped label.

membershipIndexConstituentDataset

The membership dataset.

Examples

With prices a CrspStockDataset over the S&P 500 roster’s unmasked prices and membership the CrspSP500ConstituentDataset over the same roster:

>>> ret = Return(FactorConfig(dataset=prices, warmup_bars=0, data_columns=("adjOpen",),
...                           kwargs={"n_forward_periods": 20}))
>>> label = MembershipMaskedLabel(ret, membership)
>>> label.get_factor_names(), label.lookahead_bars(), label.span_bars()
(('ret_20',), 21, 20)
>>> model = XGBoostRegressor(ModelConfig(factors=[alpha158], labels=[label], ...))
build(start, end)[source]#

Build the wrapped label’s store; returns the wrapper.

The membership store is not built here; save it beforehand.

Examples

>>> label.build("2024-01-01", "2024-02-29") is label
True
property class_name#

Bare class name, used in log and error messages.

Examples

>>> label.class_name
'MembershipMaskedLabel'
compute(start, end)[source]#

Compute the wrapped label from start to end and mask it.

Parameters:
start, endstr, datetime.date or pd.Timestamp

The range to return; see Forward.compute.

Returns:
xr.Dataset

The masked label panel on (timestamp, symbol), in memory.

Examples

>>> label.compute("2024-02-01", "2024-02-10").equals(
...     membership.mask(ret.compute("2024-02-01", "2024-02-10")))
True
property config#

The wrapped label and the membership dataset; get_config() serialises it.

Examples

>>> sorted(label.get_config())
['label', 'membership', 'name']
config_cls#

The config dataclass the wrapper is serialised and rebuilt with.

alias of MembershipMaskedLabelConfig

delay_bars()[source]#

The wrapped label’s delay, which a model reports as its label_delays.

Examples

>>> label.delay_bars() == ret.delay_bars()
True
extend(end)[source]#

Extend the wrapped label’s store to end; returns the wrapper.

Examples

>>> label.extend("2024-03-29") is label
True
classmethod from_config(config, run_dir=None)[source]#

Rebuild the wrapper, its label and its membership dataset from get_config().

Both are rebuilt by the component rule, with run_dir passed down.

Examples

>>> MembershipMaskedLabel.from_config(label.get_config()) == label
True
get_factor_names()[source]#

The wrapped label’s variable names.

Examples

>>> label.get_factor_names() == ret.get_factor_names()
True
property kind#

The wrapped label’s kind; masking changes what is measured nowhere.

Examples

>>> label.kind
'return'
lookahead_bars()[source]#

The wrapped label’s lookahead; masking reads no later bar.

Examples

>>> label.lookahead_bars() == ret.lookahead_bars()
True
read(start, end)[source]#

Return the wrapped label’s stored panel from start to end, masked.

Parameters:
start, endstr, datetime.date or pd.Timestamp

The range to return; see Forward.read.

Returns:
xr.Dataset

The masked label panel on (timestamp, symbol).

Examples

>>> label.build("2024-01-01", "2024-02-29")
>>> label.read("2024-02-01", "2024-02-10").equals(
...     membership.mask(ret.read("2024-02-01", "2024-02-10")))
True
span_bars()[source]#

The wrapped label’s span, the horizon Factor.analyze reports.

Examples

>>> label.span_bars() == ret.span_bars()
True
store_range()[source]#

The wrapped label’s store_range().

Examples

>>> label.store_range() == ret.store_range()
True