Skip to content

Regression

Helper classes for regression problems

AbstractRegressionPrediction

predict abstractmethod

predict()

Make a regression prediction

Source code in pyrenew/regression.py
18
19
20
21
22
23
@abstractmethod
def predict(self):
    """
    Make a regression prediction
    """
    pass

sample abstractmethod

sample(obs=None)

Observe or sample from the regression problem according to the specified distributions

Source code in pyrenew/regression.py
25
26
27
28
29
30
31
32
@abstractmethod
def sample(self, obs=None):
    """
    Observe or sample from the regression
    problem according to the specified
    distributions
    """
    pass

GLMPrediction

GLMPrediction(
    name: str,
    intercept_prior: Distribution,
    coefficient_priors: Distribution,
    transform: Transform = None,
    intercept_suffix="_intercept",
    coefficient_suffix="_coefficients",
)

Bases: AbstractRegressionPrediction

Generalized linear model regression predictions

Default class constructor for GLMPrediction

Parameters:

Name Type Description Default
name str

The name of the prediction process, which will be used to name the constituent sampled parameters in calls to numpyro.primitives.sample

required
intercept_prior Distribution

Prior distribution for the regression intercept value

required
coefficient_priors Distribution

Vectorized prior distribution for the regression coefficient values

required
transform Transform

Transform linking the scale of the regression to the scale of the observation. If None, use an identity transform. Default None.

None
intercept_suffix

Suffix for naming the intercept random variable in class to numpyro.sample(). Default "_intercept".

'_intercept'
coefficient_suffix

Suffix for naming the regression coefficient random variables in calls to numpyro.sample(). Default "_coefficients".

'_coefficients'
Source code in pyrenew/regression.py
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
def __init__(
    self,
    name: str,
    intercept_prior: dist.Distribution,
    coefficient_priors: dist.Distribution,
    transform: t.Transform = None,
    intercept_suffix="_intercept",
    coefficient_suffix="_coefficients",
) -> None:
    """
    Default class constructor for GLMPrediction

    Parameters
    ----------
    name
        The name of the prediction process,
        which will be used to name the constituent
        sampled parameters in calls to [`numpyro.primitives.sample`][]

    intercept_prior
        Prior distribution for the regression intercept
        value

    coefficient_priors
        Vectorized prior distribution for the regression
        coefficient values

    transform
        Transform linking the scale of the
        regression to the scale of the observation.
        If `None`, use an identity transform. Default
        `None`.

    intercept_suffix
        Suffix for naming the intercept random variable in
        class to numpyro.sample(). Default `"_intercept"`.

    coefficient_suffix
        Suffix for naming the regression coefficient
        random variables in calls to numpyro.sample().
        Default `"_coefficients"`.
    """
    if transform is None:
        transform = t.IdentityTransform()

    self.name = name
    self.transform = transform
    self.intercept_prior = intercept_prior
    self.coefficient_priors = coefficient_priors
    self.intercept_suffix = intercept_suffix
    self.coefficient_suffix = coefficient_suffix

__call__

__call__(*args, **kwargs)

Alias for sample().

Source code in pyrenew/regression.py
190
191
192
193
194
def __call__(self, *args, **kwargs):
    """
    Alias for `sample()`.
    """
    return self.sample(*args, **kwargs)

predict

predict(
    intercept: ArrayLike, coefficients: ArrayLike, predictor_values: ArrayLike
) -> ArrayLike

Generates a transformed prediction w/ intercept, coefficients, and predictor values

Parameters:

Name Type Description Default
intercept ArrayLike

Sampled numpyro distribution generated from intercept priors.

required
coefficients ArrayLike

Sampled prediction coefficients distribution generated from coefficients priors.

required
predictor_values ArrayLike

Matrix of predictor variables (covariates) for the regression problem. Each row should represent the predictor values corresponding to an observation; each column should represent a predictor variable. You do not include values of 1 for the intercept; these will be added automatically.

required

Returns:

Type Description
ArrayLike

Array of transformed predictions.

Source code in pyrenew/regression.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
def predict(
    self,
    intercept: ArrayLike,
    coefficients: ArrayLike,
    predictor_values: ArrayLike,
) -> ArrayLike:
    """
    Generates a transformed prediction w/ intercept, coefficients, and
    predictor values

    Parameters
    ----------
    intercept
        Sampled numpyro distribution generated from intercept priors.
    coefficients
        Sampled prediction coefficients distribution generated
        from coefficients priors.
    predictor_values
        Matrix of predictor variables (covariates) for the
        regression problem. Each row should represent the
        predictor values corresponding to an observation;
        each column should represent a predictor variable.
        You do not include values of 1 for the intercept;
        these will be added automatically.

    Returns
    -------
    ArrayLike
        Array of transformed predictions.
    """
    transformed_prediction = intercept + predictor_values @ coefficients
    return self.transform.inv(transformed_prediction)

sample

sample(predictor_values: ArrayLike) -> GLMPredictionSample

Sample generalized linear model

Parameters:

Name Type Description Default
predictor_values ArrayLike

Matrix of predictor variables (covariates) for the regression problem. Each row should represent the predictor values corresponding to an observation; each column should represent a predictor variable. Do not include values of 1 for the intercept; these will be added automatically. Passed as the predictor_values argument to pyrenew.regression.GLMPrediction.predict.

required

Returns:

Type Description
GLMPredictionSample
Source code in pyrenew/regression.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
def sample(self, predictor_values: ArrayLike) -> GLMPredictionSample:
    """
    Sample generalized linear model

    Parameters
    -----------
    predictor_values
        Matrix of predictor variables (covariates) for the
        regression problem. Each row should represent the
        predictor values corresponding to an observation;
        each column should represent a predictor variable.
        Do not include values of 1 for the intercept;
        these will be added automatically. Passed as the
        `predictor_values` argument to
        [`pyrenew.regression.GLMPrediction.predict`][].

    Returns
    -------
    GLMPredictionSample
    """

    intercept = numpyro.sample(
        self.name + self.intercept_suffix, self.intercept_prior
    )
    coefficients = numpyro.sample(
        self.name + self.coefficient_suffix, self.coefficient_priors
    )
    prediction = self.predict(intercept, coefficients, predictor_values)

    return GLMPredictionSample(
        prediction=prediction,
        intercept=intercept,
        coefficients=coefficients,
    )

GLMPredictionSample

Bases: NamedTuple

A container for holding the output from GLMPrediction.sample().

Attributes:

Name Type Description
prediction (ArrayLike | None, optional)

Transformed predictions. Defaults to None.

intercept (ArrayLike | None, optional)

Sampled intercept from intercept priors. Defaults to None.

coefficients (ArrayLike | None, optional)

Prediction coefficients generated from coefficients priors. Defaults to None.