cleverly.interventions.Stochastic#
- class cleverly.interventions.Stochastic(density_fn: Callable[[Any], Any], name: str, density_kind: Literal['known', 'estimated'] | None = None)[source]#
A known stochastic regime: assign arm \(a\) with probability \(g^\star(a \mid W)\).
density_fnis handed the covariate frame and returns an(n, K)array whose columns are inarm_codesorder and whose rows sum to one.The same class is a node of a longitudinal plan, inside a
DynamicRegimen. There the unit draws its arm at that node with probability \(q_t(a \mid H_t)\).Known is the load-bearing word, and
density_kind="known"declares it. \(g^\star\) must be a fixed function of \(W\), chosen independently of the analysis sample. For a population-mechanism-indexed odds tilt, the regime influence curve omits the pathwise derivative through the mechanism. A realized learned density defines a different, data-adaptive target; inference for it needs conditions this API does not check. A callable can close over any estimate and no code can inspect a closure, so the declaration is the check.refuse_regime_densities()refusesNoneand"estimated"when the regime is built, andTMLErefuses them again before any learner. For the population odds-tilt target, useIncremental, whose curve carries the mechanism term.- Parameters:
- density_fn
callable() Maps the covariate frame to an
(n, K)array of arm probabilities. At a longitudinal node it is handed the policy frame (policy_frame()): the history \([W, L_1, \ldots, L_t]\) and the earlier treatments, as labels. It returns an(n, K_t)array whose columns followtreatment_levels[t - 1], or a dataframe whose columns are exactly those levels.- name
str Label used in reported parameter names. At a longitudinal node the settings report prints it as
q:name.- density_kind{“known”, “estimated”}
orNone The declaration that
density_fnis a known function of the covariates, or at a longitudinal node of the node history."known"is the one value a fit accepts.None, the default, and"estimated"raiseCapabilityError, and any other value raisesDataError. It is the last field, soStochastic(density_fn, name)keeps its positional order.
- density_fn
Methods#
- Stochastic.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 callsdensity_fn, because a modified regime can reach it directly with a declaration this version refuses.