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.
ruleis 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 thatruleis 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()refusesNoneand"estimated"when the rule is built, andTMLErefuses them again before any learner.- Parameters:
- rule
callable() Maps the covariate frame to the treatment level assigned to each row.
- name
str Label used in reported parameter names.
- rule_kind{“known”, “estimated”}
orNone The declaration that
ruleis a known function."known"is the one value a fit accepts.None, the default, and"estimated"raiseCapabilityError, and any other value raisesDataError. It is the last field, soRule(rule, name)keeps its positional order.dataclasses.replace()copies it, so a rule replaced withrule=keeps the old declaration.
- rule
- Attributes:
density_kind{“known”, “estimated”}orNoneThe 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:
- data
CausalData Validated study data, which supplies the covariates and the arm order.
- data
- Returns:
ndarray(n, K)density, columns in arm-code order.
Notes
It runs
refuse_regime_densities()before it callsrule, 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.