cleverly.interventions.Rule#

class cleverly.interventions.Rule(rule: Callable[[Any], Any], name: str, rule_kind: Literal['known', 'estimated'] | None = None)[source]#

A dynamic regime: assign \(d(W)\), a deterministic function of the covariates.

rule is handed a dataframe of the covariates (see _covariate_frame()) in the backend the data arrived in, and returns the level – the user’s own label, not an internal code – to assign to each row.

Rule(lambda w: np.where(w["age"] > 65, 1, 0), name="treat the elderly", rule_kind="known")

Every returned level is checked against the declared support before it becomes a density, so a rule with a typo or an off-by-one fails naming the levels that exist rather than producing a regime nobody asked for.

rule_kind="known" declares that rule is a fixed rowwise function of the covariates, chosen independently of the analysis sample. A callable can close over any estimate, such as a threshold at a sample mean, and no code can inspect a closure, so the declaration is the check. refuse_regime_densities() refuses None and "estimated" when the rule is built, and TMLE refuses them again before any learner.

Parameters:
rulecallable()

Maps the covariate frame to the treatment level assigned to each row.

namestr

Label used in reported parameter names.

rule_kind{“known”, “estimated”} or None

The declaration that rule is a known function. "known" is the one value a fit accepts. None, the default, and "estimated" raise CapabilityError, and any other value raises DataError. It is the last field, so Rule(rule, name) keeps its positional order. dataclasses.replace() copies it, so a rule replaced with rule= keeps the old declaration.

Attributes:
density_kind{“known”, “estimated”} or None

The declaration rule_kind, under the name that the protocol reads.

Methods#

Rule.density(data: CausalData) → ndarray[tuple[Any, ...], dtype[float64]][source]#

Evaluate this regime’s arm probabilities for every row.

Parameters:
dataCausalData

Validated study data, which supplies the covariates and the arm order.

Returns:
ndarray

(n, K) density, columns in arm-code order.

Notes

It runs refuse_regime_densities() before it calls rule, because a modified rule can reach it directly with a declaration this version refuses.

Attributes#

Rule.density_kind#

The declaration rule_kind, under the name that the protocol reads.

Rule.rule_kind: Literal['known', 'estimated'] | None = None#
Rule.rule: Callable[[Any], Any]#
Rule.name: str#