tlo.lm module

class Predictor(property_name: str = None, external: bool = False, conditions_are_mutually_exclusive: bool | None = None, conditions_are_exhaustive: bool | None = False)[source]

Bases: object

when(condition: str | float | bool, value: float) Predictor[source]
otherwise(value: float) Predictor[source]
apply(callback: Callable[[Any], float]) Predictor[source]
class LinearModelType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

The type of model specifies how the results from the predictor are combined: ‘additive’ -> adds the effect_sizes from the predictors ‘logisitic’ -> multiples the effect_sizes from the predictors and applies the transform x/(1+x) [Thus, the intercept can be taken to be an Odds and effect_sizes Odds Ratios, and the prediction is a probability.] ‘multiplicative’ -> multiplies the effect_sizes from the predictors

ADDITIVE = 1
LOGISTIC = 2
MULTIPLICATIVE = 3
CUSTOM = 4
class LinearModel(lm_type: LinearModelType, intercept: float | int, *predictors: Predictor)[source]

Bases: object

property lm_type: LinearModelType

The model type.

property intercept: float | int

The intercept value for the model.

property predictors: Tuple[Predictor]

The predictors used in calculating the model output.

static multiplicative(*predictors: Predictor)[source]

Returns a multplicative LinearModel with intercept=1.0

Parameters:

predictors – One or more Predictor objects defining the model

static custom(predict_function, **kwargs)[source]

Define a linear model using the supplied function

The function acts as a drop-in replacement to the predict function and must implement the interface:

(

self: LinearModel, df: Union[pd.DataFrame, pd.Series], rng: Optional[np.random.RandomState] = None, **kwargs

) -> pd.Series

It is the responsibility of the caller of predict to ensure they pass either a dataframe or an individual record as expected by the custom function.

See test_custom() in test_lm.py for a couple of examples.

predict(df: DataFrame, rng: RandomState | None = None, squeeze_single_row_output=True, **kwargs) Series[source]

Evaluate linear model output for a given set of input data.

Parameters:
  • df – The input DataFrame containing the input data to evaluate the model with.

  • rng – If set to a NumPy RandomState instance, returned output will be boolean Series corresponding to Bernoulli random variables sampled according to probabilities specified by model output. Otherwise model output directly returned.

  • squeeze_single_row_output – If rng argument is not None and this argument is set to True, the output for a df input with a single-row will be a scalar boolean value rather than a boolean Series.

  • **kwargs

    Values for any external variables included in model predictors.