cleverly.msm.MSM#

class cleverly.msm.MSM(design: Callable[[Any, Any], Any], terms: tuple[str, ...], weights: Callable[[Any, Any], Any] | None = None, link: Literal['identity', 'log', 'logit'] = 'identity', from_linear: bool = False, doses: tuple[float, ...] = (), weights_kind: Literal['known', 'estimated'] | None = None, design_kind: Literal['known', 'estimated'] | None = None)[source]#

A working model, declared as a design function and the names of its terms.

design is handed one treatment level – the user’s own label, not an internal code – and a dataframe of the covariates in the backend the data arrived in, and returns the (n, p) design \(\varphi(a, V)\) for that arm. It is called once per arm. A written design declares that it is a fixed function with design_kind="known".

MSM(
    design=lambda a, w: np.column_stack([np.ones(len(w)), np.full(len(w), a)]),
    terms=("(intercept)", "a"),
    design_kind="known",
)

Reading the covariate frame rather than the whole dataset is the same restriction cleverly.interventions.Rule works under and for the same reason: a working model whose design read Y would not be a model for a counterfactual mean, and one that read the observed A would be summarising the arms nobody was assigned to differently from the ones they were. The columns are the encoded covariates, so a categorical column appears as the indicators the data layer expanded it into; data.covariate_names is the list to write a design against.

Parameters:
designcallable()

(arm_label, covariate_frame) -> (n, p) for a point treatment, and (regimen_label, horizon, baseline_frame) -> (n, p) for a longitudinal regimen MSM. It must be a known function, and design_kind declares that it is.

termstuple of str

One name per design column, used verbatim in the reported parameter names: msm[a:W1].

weightscallable() or None

h(a, V), as (arm_label, covariate_frame) -> (n,). None means uniform, which weights every arm and every unit equally. It must be a known function, and weights_kind declares that it is.

link{“identity”, “log”, “logit”}

"identity", "log" or "logit". The link also chooses the projection’s loss: least squares for the identity, the quasi-Poisson loss for the log and the quasi-binomial loss for the logit, each convex in \(\beta\) (Link). Every link’s fit is a single fluctuation. What the coefficients mean changes with it – under "log" a coefficient is a log risk ratio and under "logit" a log odds ratio, which is what coefficients() exponentiates.

from_linearbool

Set by linear() and by nothing else. That shorthand reads the label it is handed as a dose, which a treatment arm can be and a regimen cannot, so a working model over regimens refuses it (cleverly.longitudinal.msm). _numeric_level already refuses a string label, but a regimen legitimately called "0" would be read as a dose of zero and reported without complaint – a flag on the declaration is what makes that structural rather than lucky. It declares nothing about the design: the design declaration is design_kind.

dosestuple of float

Increasing dose grid used to integrate a continuous-treatment projection. Empty on the ordinary finite-arm path. The grid is part of the estimand, not a random Monte Carlo tuning parameter.

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

The declaration that weights is a known function. A callable weights needs "known": a fixed function of the arm and the covariates, chosen without reading the data. None with a callable, and "estimated", are refused by refuse_msm_functions(), because a weight computed from the sample makes h a functional of \(P\) and the reported influence curve omits its pathwise derivative. It is unrelated to cleverly.data.weighting.WeightKind, which describes observation weights. It follows doses, so link stays the fourth positional argument.

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

The declaration that design is a known function: a fixed function of the arm (or the regimen and horizon) and the covariates, chosen without reading the data. None and "estimated" are refused by refuse_msm_functions(). A population-law target using a sample-computed design, such as a covariate centred at its sample mean, can need a pathwise derivative through that statistic, which the reported curve lacks. None reads as "known" only when the design has the exact type that linear() builds. That design is known by construction, so linear() leaves this field None, and a design swapped into its model with dataclasses.replace() needs its own declaration.

Methods#

classmethod MSM.linear(*, modifiers: Sequence[str] = (), interaction: bool = True, weights: Callable[[Any, Any], Any] | None = None, weights_kind: Literal['known', 'estimated'] | None = None, link: Literal['identity', 'log', 'logit'] = 'identity', doses: Sequence[float] = ()) → MSM[source]#

The usual dose-response model, without writing the design out by hand.

MSM.linear(modifiers=("W1",)) is \(m(a, V) = \beta_0 + \beta_1 a + \beta_2 W_1 + \beta_3 a W_1\), and interaction=False drops the last term. With no modifiers it is a plain dose-response line, whose slope is the average change in counterfactual mean per unit of treatment under the \(h\)-weighted projection. link= puts that linear predictor inside a link, so link="log" makes \(\beta_1\) a log risk ratio per unit of treatment rather than a risk difference.

The arm enters as a number, so this requires numeric treatment levels: it reads the arms as a dose to be extrapolated between. A string-labelled treatment is refused rather than coded silently, because {"low", "medium", "high"} sorts alphabetically and the resulting slope would be per-step in an order nobody chose. Pass design= with design_kind='known' and code the arms yourself where that is what you want.

The design this builds is a fixed function of the arm and the named covariates, so it needs no declaration, and the model leaves design_kind None. refuse_msm_functions() reads the exact type of this design as known. A design swapped in with dataclasses.replace() has another type, so it needs design_kind="known" of its own. weights= and weights_kind= are forwarded unchanged, so a callable weight needs weights_kind="known" here as it does on MSM.

Parameters:
modifierssequence of str

The encoded covariates that enter the model as main effects.

interactionbool

Whether each modifier also enters as a product with the dose, named a:<modifier>.

weightscallable() or None

The projection weight h(a, V), forwarded to MSM. None means uniform.

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

The declaration of weights, forwarded to MSM.

link{“identity”, “log”, “logit”}

The link, forwarded to MSM.

dosessequence of float

The increasing dose grid of a continuous-treatment projection. Empty on the finite-arm path.

Returns:
MSM

A working model whose design is linear in the dose and the modifiers, with from_linear=True and design_kind=None.

Attributes#

MSM.design_kind: Literal['known', 'estimated'] | None = None#
MSM.doses: tuple[float, ...] = ()#
MSM.from_linear: bool = False#
MSM.weights: Callable[[Any, Any], Any] | None = None#
MSM.weights_kind: Literal['known', 'estimated'] | None = None#
MSM.design: Callable[[Any, Any], Any]#
MSM.terms: tuple[str, ...]#