API Reference
The two main classes in glum are GeneralizedLinearRegressor and GeneralizedLinearRegressorCV. Most users will use fit() and predict().
BinomialDistribution
Bases: ExponentialDispersionModel
A class for the Binomial distribution.
The Binomial distribution models outcomes y in [0, 1].
See the documentation of the superclass,
ExponentialDispersionModel, for details.
lower_bound
class-attribute
instance-attribute
lower_bound = 0
Get the lower bound of values for the EDM.
upper_bound
class-attribute
instance-attribute
upper_bound = 1
Get the upper bound of values for the EDM.
include_lower_bound
class-attribute
instance-attribute
include_lower_bound = True
Return whether lower_bound is allowed as a value of y.
include_upper_bound
class-attribute
instance-attribute
include_upper_bound = True
Return whether upper_bound is allowed as a value of y.
in_y_range
in_y_range(x) -> np.ndarray
Return True if x is in the valid range of the EDM.
Source code in src/glum/_distribution.py
88 89 90 91 92 93 94 95 96 97 98 99 100 | |
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a distribution if it exists.
Source code in src/glum/_distribution.py
102 103 104 105 106 107 108 | |
variance
variance(mu, dispersion=1, sample_weight=1) -> np.ndarray
Compute the variance function.
The variance of \(Y_i \sim \mathrm{EDM}(\mu_i, \phi / w_i)\) takes the form \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(w_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
variance_derivative
variance_derivative(mu, dispersion=1, sample_weight=1)
Compute the derivative of the variance with respect to mu.
The derivative of the variance is equal to \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(ws_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
unit_deviance_derivative
unit_deviance_derivative(y, mu)
Compute the derivative of the unit deviance with respect to mu.
The derivative of the unit deviance is given by \(2 \times (\mu - y) / v(\mu)\), where \(v(\mu)\) is the unit variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
deviance
deviance(y, mu, sample_weight=1) -> float
Compute the deviance.
The deviance is a weighted sum of the unit deviances. In terms of the unit log likelihood \(\ell\), it equals \(2 \sum_i [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which the variance is inversely proportional. |
1
|
Source code in src/glum/_distribution.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
deviance_derivative
deviance_derivative(y, mu, sample_weight=1)
Compute the derivative of the deviance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples)(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
eta_mu_deviance
eta_mu_deviance(link: Link, factor: float, cur_eta, X_dot_d, y, sample_weight) -> tuple[np.ndarray, np.ndarray, float]
Compute eta, mu and the deviance.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The linear predictor, |
(ndarray, shape(shape[0]))
|
The link-function-transformed prediction, |
float
|
The deviance. |
Source code in src/glum/_distribution.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
rowwise_gradient_hessian
rowwise_gradient_hessian(link: Link, coef, dispersion, X: Union[MatrixBase, StandardizedMatrix], y, sample_weight, eta, mu, offset=None)
Compute the gradient and negative Hessian of the log likelihood row-wise.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The gradient of the log likelihood, row-wise. |
(ndarray, shape(shape[0]))
|
The negative Hessian of the log likelihood, row-wise. |
Source code in src/glum/_distribution.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
unit_variance
unit_variance(mu)
Compute the unit variance.
The unit variance, \(v(\mu) \equiv b''((b')^{-1} (\mu))\), determines the variance as a function of the mean \(\mu\) by \(\mathrm{var}(y_i) = v(\mu_i) \times \phi / w_i\). It can also be derived from the unit deviance \(d(y, \mu)\) as
See also variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1269 1270 | |
unit_variance_derivative
unit_variance_derivative(mu)
Compute the derivative of the unit variance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1272 1273 | |
unit_deviance
unit_deviance(y, mu)
Compute the unit deviance.
In terms of the unit log likelihood \(\ell\), the unit deviance is \(2 [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1275 1276 1277 | |
log_likelihood
log_likelihood(y, mu, sample_weight=None, dispersion=1) -> float
Compute the log likelihood.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Sample weights. |
None
|
dispersion
|
(float, optional(default=1))
|
Ignored. |
1
|
Source code in src/glum/_distribution.py
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 | |
dispersion
dispersion(y, mu, sample_weight=None, ddof=1, method='pearson') -> float
Estimate the dispersion parameter \(\phi\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inversely proportional. |
None
|
ddof
|
(int, optional(default=1))
|
Degrees of freedom consumed by the model for |
1
|
method
|
Whether to base the estimate on the Pearson residuals or the deviance. |
'pearson'
|
Source code in src/glum/_distribution.py
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 | |
ExponentialDispersionModel
Base class for reproductive Exponential Dispersion Models (EDM).
The PDF of \(Y \sim \mathrm{EDM}(\theta, \phi)\) is given by
where \(\theta\) is the scale parameter, \(\phi\) is the dispersion parameter, \(w\) is a given weight, \(b\) is the cumulant function and \(c\) is a normalization term.
It can be shown that \(\mathrm{E}(Y) = b'(\theta)\) and \(\mathrm{var}(Y) = b''(\theta) \times \phi / w\).
References
< https://en.wikipedia.org/wiki/Exponential_dispersion_model >.
lower_bound
abstractmethod
property
lower_bound: float
Get the lower bound of values for the EDM.
upper_bound
abstractmethod
property
upper_bound: float
Get the upper bound of values for the EDM.
include_lower_bound
abstractmethod
property
include_lower_bound: bool
Return whether lower_bound is allowed as a value of y.
include_upper_bound
abstractmethod
property
include_upper_bound: bool
Return whether upper_bound is allowed as a value of y.
in_y_range
in_y_range(x) -> np.ndarray
Return True if x is in the valid range of the EDM.
Source code in src/glum/_distribution.py
88 89 90 91 92 93 94 95 96 97 98 99 100 | |
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a distribution if it exists.
Source code in src/glum/_distribution.py
102 103 104 105 106 107 108 | |
unit_variance
abstractmethod
unit_variance(mu)
Compute the unit variance.
The unit variance, \(v(\mu) \equiv b''((b')^{-1} (\mu))\), determines the variance as a function of the mean \(\mu\) by \(\mathrm{var}(y_i) = v(\mu_i) \times \phi / w_i\). It can also be derived from the unit deviance \(d(y, \mu)\) as
See also variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
unit_variance_derivative
abstractmethod
unit_variance_derivative(mu)
Compute the derivative of the unit variance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
132 133 134 135 136 137 138 139 140 141 | |
variance
variance(mu, dispersion=1, sample_weight=1) -> np.ndarray
Compute the variance function.
The variance of \(Y_i \sim \mathrm{EDM}(\mu_i, \phi / w_i)\) takes the form \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(w_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
variance_derivative
variance_derivative(mu, dispersion=1, sample_weight=1)
Compute the derivative of the variance with respect to mu.
The derivative of the variance is equal to \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(ws_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
unit_deviance
abstractmethod
unit_deviance(y, mu)
Compute the unit deviance.
In terms of the unit log likelihood \(\ell\), the unit deviance is \(2 [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
unit_deviance_derivative
unit_deviance_derivative(y, mu)
Compute the derivative of the unit deviance with respect to mu.
The derivative of the unit deviance is given by \(2 \times (\mu - y) / v(\mu)\), where \(v(\mu)\) is the unit variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
deviance
deviance(y, mu, sample_weight=1) -> float
Compute the deviance.
The deviance is a weighted sum of the unit deviances. In terms of the unit log likelihood \(\ell\), it equals \(2 \sum_i [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which the variance is inversely proportional. |
1
|
Source code in src/glum/_distribution.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
deviance_derivative
deviance_derivative(y, mu, sample_weight=1)
Compute the derivative of the deviance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples)(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
eta_mu_deviance
eta_mu_deviance(link: Link, factor: float, cur_eta, X_dot_d, y, sample_weight) -> tuple[np.ndarray, np.ndarray, float]
Compute eta, mu and the deviance.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The linear predictor, |
(ndarray, shape(shape[0]))
|
The link-function-transformed prediction, |
float
|
The deviance. |
Source code in src/glum/_distribution.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
rowwise_gradient_hessian
rowwise_gradient_hessian(link: Link, coef, dispersion, X: Union[MatrixBase, StandardizedMatrix], y, sample_weight, eta, mu, offset=None)
Compute the gradient and negative Hessian of the log likelihood row-wise.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The gradient of the log likelihood, row-wise. |
(ndarray, shape(shape[0]))
|
The negative Hessian of the log likelihood, row-wise. |
Source code in src/glum/_distribution.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
dispersion
dispersion(y, mu, sample_weight=None, ddof=1, method='pearson') -> float
Estimate the dispersion parameter \(\phi\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inversely proportional. |
None
|
ddof
|
(int, optional(default=1))
|
Degrees of freedom consumed by the model for |
1
|
method
|
Whether to base the estimate on the Pearson residuals or the deviance. |
'pearson'
|
Source code in src/glum/_distribution.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
GammaDistribution
Bases: ExponentialDispersionModel
Class for the gamma distribution.
The gamma distribution models outcomes y in (0, +∞).
See the documentation of the superclass,
ExponentialDispersionModel, for details.
lower_bound
class-attribute
instance-attribute
lower_bound = 0
Get the lower bound of values for the EDM.
upper_bound
class-attribute
instance-attribute
upper_bound = np.inf
Get the upper bound of values for the EDM.
include_lower_bound
class-attribute
instance-attribute
include_lower_bound = False
Return whether lower_bound is allowed as a value of y.
include_upper_bound
class-attribute
instance-attribute
include_upper_bound = False
Return whether upper_bound is allowed as a value of y.
in_y_range
in_y_range(x) -> np.ndarray
Return True if x is in the valid range of the EDM.
Source code in src/glum/_distribution.py
88 89 90 91 92 93 94 95 96 97 98 99 100 | |
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a distribution if it exists.
Source code in src/glum/_distribution.py
102 103 104 105 106 107 108 | |
variance
variance(mu, dispersion=1, sample_weight=1) -> np.ndarray
Compute the variance function.
The variance of \(Y_i \sim \mathrm{EDM}(\mu_i, \phi / w_i)\) takes the form \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(w_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
variance_derivative
variance_derivative(mu, dispersion=1, sample_weight=1)
Compute the derivative of the variance with respect to mu.
The derivative of the variance is equal to \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(ws_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
unit_deviance_derivative
unit_deviance_derivative(y, mu)
Compute the derivative of the unit deviance with respect to mu.
The derivative of the unit deviance is given by \(2 \times (\mu - y) / v(\mu)\), where \(v(\mu)\) is the unit variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
deviance_derivative
deviance_derivative(y, mu, sample_weight=1)
Compute the derivative of the deviance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples)(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
eta_mu_deviance
eta_mu_deviance(link: Link, factor: float, cur_eta, X_dot_d, y, sample_weight) -> tuple[np.ndarray, np.ndarray, float]
Compute eta, mu and the deviance.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The linear predictor, |
(ndarray, shape(shape[0]))
|
The link-function-transformed prediction, |
float
|
The deviance. |
Source code in src/glum/_distribution.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
rowwise_gradient_hessian
rowwise_gradient_hessian(link: Link, coef, dispersion, X: Union[MatrixBase, StandardizedMatrix], y, sample_weight, eta, mu, offset=None)
Compute the gradient and negative Hessian of the log likelihood row-wise.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The gradient of the log likelihood, row-wise. |
(ndarray, shape(shape[0]))
|
The negative Hessian of the log likelihood, row-wise. |
Source code in src/glum/_distribution.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
unit_variance
unit_variance(mu) -> np.ndarray
Compute the unit variance.
The unit variance, \(v(\mu) \equiv b''((b')^{-1} (\mu))\), determines the variance as a function of the mean \(\mu\) by \(\mathrm{var}(y_i) = v(\mu_i) \times \phi / w_i\). It can also be derived from the unit deviance \(d(y, \mu)\) as
See also variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1022 1023 | |
unit_variance_derivative
unit_variance_derivative(mu) -> np.ndarray
Compute the derivative of the unit variance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1025 1026 | |
deviance
deviance(y, mu, sample_weight=None) -> float
Compute the deviance.
The deviance is a weighted sum of the unit deviances. In terms of the unit log likelihood \(\ell\), it equals \(2 \sum_i [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which the variance is inversely proportional. |
None
|
Source code in src/glum/_distribution.py
1028 1029 1030 1031 1032 1033 1034 1035 | |
unit_deviance
unit_deviance(y, mu)
Compute the unit deviance.
In terms of the unit log likelihood \(\ell\), the unit deviance is \(2 [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1037 1038 | |
log_likelihood
log_likelihood(y, mu, sample_weight=None, dispersion=None) -> float
Compute the log likelihood.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Sample weights. |
None
|
dispersion
|
(float, optional(default=None))
|
Dispersion parameter \(\phi\). Estimated if |
None
|
Source code in src/glum/_distribution.py
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 | |
dispersion
dispersion(y, mu, sample_weight=None, ddof=1, method='pearson') -> float
Estimate the dispersion parameter \(\phi\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inversely proportional. |
None
|
ddof
|
(int, optional(default=1))
|
Degrees of freedom consumed by the model for |
1
|
method
|
Whether to base the estimate on the Pearson residuals or the deviance. |
'pearson'
|
Source code in src/glum/_distribution.py
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 | |
GeneralizedHyperbolicSecant
Bases: ExponentialDispersionModel
A class for the Generalized Hyperbolic Secant (GHS) distribution.
The GHS distribution models outcomes y in (-∞, +∞).
See the documentation of the superclass,
ExponentialDispersionModel, for details.
lower_bound
class-attribute
instance-attribute
lower_bound = -np.inf
Get the lower bound of values for the EDM.
upper_bound
class-attribute
instance-attribute
upper_bound = np.inf
Get the upper bound of values for the EDM.
include_lower_bound
class-attribute
instance-attribute
include_lower_bound = False
Return whether lower_bound is allowed as a value of y.
include_upper_bound
class-attribute
instance-attribute
include_upper_bound = False
Return whether upper_bound is allowed as a value of y.
in_y_range
in_y_range(x) -> np.ndarray
Return True if x is in the valid range of the EDM.
Source code in src/glum/_distribution.py
88 89 90 91 92 93 94 95 96 97 98 99 100 | |
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a distribution if it exists.
Source code in src/glum/_distribution.py
102 103 104 105 106 107 108 | |
variance
variance(mu, dispersion=1, sample_weight=1) -> np.ndarray
Compute the variance function.
The variance of \(Y_i \sim \mathrm{EDM}(\mu_i, \phi / w_i)\) takes the form \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(w_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
variance_derivative
variance_derivative(mu, dispersion=1, sample_weight=1)
Compute the derivative of the variance with respect to mu.
The derivative of the variance is equal to \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(ws_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
unit_deviance_derivative
unit_deviance_derivative(y, mu)
Compute the derivative of the unit deviance with respect to mu.
The derivative of the unit deviance is given by \(2 \times (\mu - y) / v(\mu)\), where \(v(\mu)\) is the unit variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
deviance
deviance(y, mu, sample_weight=1) -> float
Compute the deviance.
The deviance is a weighted sum of the unit deviances. In terms of the unit log likelihood \(\ell\), it equals \(2 \sum_i [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which the variance is inversely proportional. |
1
|
Source code in src/glum/_distribution.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
deviance_derivative
deviance_derivative(y, mu, sample_weight=1)
Compute the derivative of the deviance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples)(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
eta_mu_deviance
eta_mu_deviance(link: Link, factor: float, cur_eta, X_dot_d, y, sample_weight) -> tuple[np.ndarray, np.ndarray, float]
Compute eta, mu and the deviance.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The linear predictor, |
(ndarray, shape(shape[0]))
|
The link-function-transformed prediction, |
float
|
The deviance. |
Source code in src/glum/_distribution.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
rowwise_gradient_hessian
rowwise_gradient_hessian(link: Link, coef, dispersion, X: Union[MatrixBase, StandardizedMatrix], y, sample_weight, eta, mu, offset=None)
Compute the gradient and negative Hessian of the log likelihood row-wise.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The gradient of the log likelihood, row-wise. |
(ndarray, shape(shape[0]))
|
The negative Hessian of the log likelihood, row-wise. |
Source code in src/glum/_distribution.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
dispersion
dispersion(y, mu, sample_weight=None, ddof=1, method='pearson') -> float
Estimate the dispersion parameter \(\phi\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inversely proportional. |
None
|
ddof
|
(int, optional(default=1))
|
Degrees of freedom consumed by the model for |
1
|
method
|
Whether to base the estimate on the Pearson residuals or the deviance. |
'pearson'
|
Source code in src/glum/_distribution.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
unit_variance
unit_variance(mu) -> np.ndarray
Compute the unit variance.
The unit variance, \(v(\mu) \equiv b''((b')^{-1} (\mu))\), determines the variance as a function of the mean \(\mu\) by \(\mathrm{var}(y_i) = v(\mu_i) \times \phi / w_i\). It can also be derived from the unit deviance \(d(y, \mu)\) as
See also variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1242 1243 | |
unit_variance_derivative
unit_variance_derivative(mu) -> np.ndarray
Compute the derivative of the unit variance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1245 1246 | |
unit_deviance
unit_deviance(y, mu) -> np.ndarray
Compute the unit deviance.
In terms of the unit log likelihood \(\ell\), the unit deviance is \(2 [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1248 1249 | |
InverseGaussianDistribution
Bases: ExponentialDispersionModel
Class for the inverse Gaussian distribution.
The inverse Gaussian distribution models outcomes y in (0, +∞).
See the documentation of the superclass,
ExponentialDispersionModel, for details.
lower_bound
class-attribute
instance-attribute
lower_bound = 0
Get the lower bound of values for the EDM.
upper_bound
class-attribute
instance-attribute
upper_bound = np.inf
Get the upper bound of values for the EDM.
include_lower_bound
class-attribute
instance-attribute
include_lower_bound = False
Return whether lower_bound is allowed as a value of y.
include_upper_bound
class-attribute
instance-attribute
include_upper_bound = False
Return whether upper_bound is allowed as a value of y.
in_y_range
in_y_range(x) -> np.ndarray
Return True if x is in the valid range of the EDM.
Source code in src/glum/_distribution.py
88 89 90 91 92 93 94 95 96 97 98 99 100 | |
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a distribution if it exists.
Source code in src/glum/_distribution.py
102 103 104 105 106 107 108 | |
variance
variance(mu, dispersion=1, sample_weight=1) -> np.ndarray
Compute the variance function.
The variance of \(Y_i \sim \mathrm{EDM}(\mu_i, \phi / w_i)\) takes the form \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(w_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
variance_derivative
variance_derivative(mu, dispersion=1, sample_weight=1)
Compute the derivative of the variance with respect to mu.
The derivative of the variance is equal to \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(ws_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
unit_deviance_derivative
unit_deviance_derivative(y, mu)
Compute the derivative of the unit deviance with respect to mu.
The derivative of the unit deviance is given by \(2 \times (\mu - y) / v(\mu)\), where \(v(\mu)\) is the unit variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
deviance_derivative
deviance_derivative(y, mu, sample_weight=1)
Compute the derivative of the deviance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples)(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
eta_mu_deviance
eta_mu_deviance(link: Link, factor: float, cur_eta, X_dot_d, y, sample_weight) -> tuple[np.ndarray, np.ndarray, float]
Compute eta, mu and the deviance.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The linear predictor, |
(ndarray, shape(shape[0]))
|
The link-function-transformed prediction, |
float
|
The deviance. |
Source code in src/glum/_distribution.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
rowwise_gradient_hessian
rowwise_gradient_hessian(link: Link, coef, dispersion, X: Union[MatrixBase, StandardizedMatrix], y, sample_weight, eta, mu, offset=None)
Compute the gradient and negative Hessian of the log likelihood row-wise.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The gradient of the log likelihood, row-wise. |
(ndarray, shape(shape[0]))
|
The negative Hessian of the log likelihood, row-wise. |
Source code in src/glum/_distribution.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
unit_variance
unit_variance(mu) -> np.ndarray
Compute the unit variance.
The unit variance, \(v(\mu) \equiv b''((b')^{-1} (\mu))\), determines the variance as a function of the mean \(\mu\) by \(\mathrm{var}(y_i) = v(\mu_i) \times \phi / w_i\). It can also be derived from the unit deviance \(d(y, \mu)\) as
See also variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1135 1136 | |
unit_variance_derivative
unit_variance_derivative(mu) -> np.ndarray
Compute the derivative of the unit variance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1138 1139 | |
deviance
deviance(y, mu, sample_weight=None) -> float
Compute the deviance.
The deviance is a weighted sum of the unit deviances. In terms of the unit log likelihood \(\ell\), it equals \(2 \sum_i [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which the variance is inversely proportional. |
None
|
Source code in src/glum/_distribution.py
1141 1142 1143 1144 1145 | |
unit_deviance
unit_deviance(y, mu)
Compute the unit deviance.
In terms of the unit log likelihood \(\ell\), the unit deviance is \(2 [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1147 1148 | |
log_likelihood
log_likelihood(y, mu, sample_weight=None, dispersion=None) -> float
Compute the log likelihood.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Sample weights. |
None
|
dispersion
|
(float, optional(default=None))
|
Dispersion parameter \(\phi\). Estimated if |
None
|
Source code in src/glum/_distribution.py
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 | |
dispersion
dispersion(y, mu, sample_weight=None, ddof=1, method='pearson') -> float
Estimate the dispersion parameter \(\phi\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inversely proportional. |
None
|
ddof
|
(int, optional(default=1))
|
Degrees of freedom consumed by the model for |
1
|
method
|
Whether to base the estimate on the Pearson residuals or the deviance. |
'pearson'
|
Source code in src/glum/_distribution.py
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 | |
NegativeBinomialDistribution
NegativeBinomialDistribution(theta=1.0)
Bases: ExponentialDispersionModel
A class for the Negative Binomial distribution.
A negative binomial distribution with mean \(\mu = \mathrm{E}(Y)\) is uniquely defined by its mean-variance relationship \(\mathrm{var}(Y) \propto \mu + \theta * \mu^2\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
(float, optional(default=1.0))
|
The dispersion parameter from the |
1.0
|
References
For the log-likelihood and deviance:
- M. L. Zwilling Negative Binomial Regression, The Mathematica Journal 2013. https://www.mathematica-journal.com/2013/06/27/negative-binomial-regression/
Source code in src/glum/_distribution.py
1375 1376 1377 | |
lower_bound
class-attribute
instance-attribute
lower_bound = 0
Get the lower bound of values for the EDM.
upper_bound
class-attribute
instance-attribute
upper_bound = np.inf
Get the upper bound of values for the EDM.
include_lower_bound
class-attribute
instance-attribute
include_lower_bound = True
Return whether lower_bound is allowed as a value of y.
include_upper_bound
class-attribute
instance-attribute
include_upper_bound = False
Return whether upper_bound is allowed as a value of y.
theta
property
writable
theta
Return the negative binomial theta parameter.
in_y_range
in_y_range(x) -> np.ndarray
Return True if x is in the valid range of the EDM.
Source code in src/glum/_distribution.py
88 89 90 91 92 93 94 95 96 97 98 99 100 | |
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a distribution if it exists.
Source code in src/glum/_distribution.py
102 103 104 105 106 107 108 | |
variance
variance(mu, dispersion=1, sample_weight=1) -> np.ndarray
Compute the variance function.
The variance of \(Y_i \sim \mathrm{EDM}(\mu_i, \phi / w_i)\) takes the form \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(w_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
variance_derivative
variance_derivative(mu, dispersion=1, sample_weight=1)
Compute the derivative of the variance with respect to mu.
The derivative of the variance is equal to \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(ws_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
unit_deviance_derivative
unit_deviance_derivative(y, mu)
Compute the derivative of the unit deviance with respect to mu.
The derivative of the unit deviance is given by \(2 \times (\mu - y) / v(\mu)\), where \(v(\mu)\) is the unit variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
deviance_derivative
deviance_derivative(y, mu, sample_weight=1)
Compute the derivative of the deviance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples)(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
eta_mu_deviance
eta_mu_deviance(link: Link, factor: float, cur_eta, X_dot_d, y, sample_weight) -> tuple[np.ndarray, np.ndarray, float]
Compute eta, mu and the deviance.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The linear predictor, |
(ndarray, shape(shape[0]))
|
The link-function-transformed prediction, |
float
|
The deviance. |
Source code in src/glum/_distribution.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
rowwise_gradient_hessian
rowwise_gradient_hessian(link: Link, coef, dispersion, X: Union[MatrixBase, StandardizedMatrix], y, sample_weight, eta, mu, offset=None)
Compute the gradient and negative Hessian of the log likelihood row-wise.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The gradient of the log likelihood, row-wise. |
(ndarray, shape(shape[0]))
|
The negative Hessian of the log likelihood, row-wise. |
Source code in src/glum/_distribution.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
unit_variance
unit_variance(mu)
Compute the unit variance.
The unit variance, \(v(\mu) \equiv b''((b')^{-1} (\mu))\), determines the variance as a function of the mean \(\mu\) by \(\mathrm{var}(y_i) = v(\mu_i) \times \phi / w_i\). It can also be derived from the unit deviance \(d(y, \mu)\) as
See also variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1399 1400 | |
unit_variance_derivative
unit_variance_derivative(mu)
Compute the derivative of the unit variance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1402 1403 | |
deviance
deviance(y, mu, sample_weight=None) -> float
Compute the deviance.
The deviance is a weighted sum of the unit deviances. In terms of the unit log likelihood \(\ell\), it equals \(2 \sum_i [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which the variance is inversely proportional. |
None
|
Source code in src/glum/_distribution.py
1405 1406 1407 1408 1409 1410 | |
unit_deviance
unit_deviance(y, mu)
Compute the unit deviance.
In terms of the unit log likelihood \(\ell\), the unit deviance is \(2 [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
1412 1413 1414 1415 1416 1417 | |
log_likelihood
log_likelihood(y, mu, sample_weight=None, dispersion=1) -> float
Compute the log likelihood.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Sample weights. |
None
|
dispersion
|
(float, optional(default=1.0))
|
Ignored. |
1
|
Source code in src/glum/_distribution.py
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 | |
dispersion
dispersion(y, mu, sample_weight=None, ddof=1, method='pearson') -> float
Estimate the dispersion parameter \(\phi\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inversely proportional. |
None
|
ddof
|
(int, optional(default=1))
|
Degrees of freedom consumed by the model for |
1
|
method
|
Whether to base the estimate on the Pearson residuals or the deviance. |
'pearson'
|
Source code in src/glum/_distribution.py
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 | |
NormalDistribution
Bases: ExponentialDispersionModel
Class for the normal (a.k.a. Gaussian) distribution.
The normal distribution models outcomes y in (-∞, +∞).
See the documentation of the superclass,
ExponentialDispersionModel, for details.
lower_bound
class-attribute
instance-attribute
lower_bound = -np.inf
Get the lower bound of values for the EDM.
upper_bound
class-attribute
instance-attribute
upper_bound = np.inf
Get the upper bound of values for the EDM.
include_lower_bound
class-attribute
instance-attribute
include_lower_bound = False
Return whether lower_bound is allowed as a value of y.
include_upper_bound
class-attribute
instance-attribute
include_upper_bound = False
Return whether upper_bound is allowed as a value of y.
in_y_range
in_y_range(x) -> np.ndarray
Return True if x is in the valid range of the EDM.
Source code in src/glum/_distribution.py
88 89 90 91 92 93 94 95 96 97 98 99 100 | |
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a distribution if it exists.
Source code in src/glum/_distribution.py
102 103 104 105 106 107 108 | |
variance
variance(mu, dispersion=1, sample_weight=1) -> np.ndarray
Compute the variance function.
The variance of \(Y_i \sim \mathrm{EDM}(\mu_i, \phi / w_i)\) takes the form \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(w_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
variance_derivative
variance_derivative(mu, dispersion=1, sample_weight=1)
Compute the derivative of the variance with respect to mu.
The derivative of the variance is equal to \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(ws_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
unit_deviance_derivative
unit_deviance_derivative(y, mu)
Compute the derivative of the unit deviance with respect to mu.
The derivative of the unit deviance is given by \(2 \times (\mu - y) / v(\mu)\), where \(v(\mu)\) is the unit variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
deviance_derivative
deviance_derivative(y, mu, sample_weight=1)
Compute the derivative of the deviance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples)(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
eta_mu_deviance
eta_mu_deviance(link: Link, factor: float, cur_eta, X_dot_d, y, sample_weight) -> tuple[np.ndarray, np.ndarray, float]
Compute eta, mu and the deviance.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The linear predictor, |
(ndarray, shape(shape[0]))
|
The link-function-transformed prediction, |
float
|
The deviance. |
Source code in src/glum/_distribution.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
rowwise_gradient_hessian
rowwise_gradient_hessian(link: Link, coef, dispersion, X: Union[MatrixBase, StandardizedMatrix], y, sample_weight, eta, mu, offset=None)
Compute the gradient and negative Hessian of the log likelihood row-wise.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The gradient of the log likelihood, row-wise. |
(ndarray, shape(shape[0]))
|
The negative Hessian of the log likelihood, row-wise. |
Source code in src/glum/_distribution.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
unit_variance
unit_variance(mu) -> np.ndarray
Compute the unit variance.
The unit variance, \(v(\mu) \equiv b''((b')^{-1} (\mu))\), determines the variance as a function of the mean \(\mu\) by \(\mathrm{var}(y_i) = v(\mu_i) \times \phi / w_i\). It can also be derived from the unit deviance \(d(y, \mu)\) as
See also variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
796 797 | |
unit_variance_derivative
unit_variance_derivative(mu) -> np.ndarray
Compute the derivative of the unit variance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
799 800 | |
deviance
deviance(y, mu, sample_weight=None) -> float
Compute the deviance.
The deviance is a weighted sum of the unit deviances. In terms of the unit log likelihood \(\ell\), it equals \(2 \sum_i [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which the variance is inversely proportional. |
None
|
Source code in src/glum/_distribution.py
802 803 804 805 806 807 808 809 | |
unit_deviance
unit_deviance(y, mu)
Compute the unit deviance.
In terms of the unit log likelihood \(\ell\), the unit deviance is \(2 [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
811 812 | |
log_likelihood
log_likelihood(y, mu, sample_weight=None, dispersion=None) -> float
Compute the log likelihood.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Sample weights. |
None
|
dispersion
|
(float, optional(default=None))
|
Dispersion parameter \(\phi\). Estimated if |
None
|
Source code in src/glum/_distribution.py
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 | |
dispersion
dispersion(y, mu, sample_weight=None, ddof=1, method='pearson') -> float
Estimate the dispersion parameter \(\phi\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inversely proportional. |
None
|
ddof
|
(int, optional(default=1))
|
Degrees of freedom consumed by the model for |
1
|
method
|
Whether to base the estimate on the Pearson residuals or the deviance. |
'pearson'
|
Source code in src/glum/_distribution.py
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 | |
PoissonDistribution
Bases: ExponentialDispersionModel
Class for the scaled Poisson distribution.
The Poisson distribution models discrete outcomes y in [0, +∞).
See the documentation of the superclass,
ExponentialDispersionModel, for details.
lower_bound
class-attribute
instance-attribute
lower_bound = 0
Get the lower bound of values for the EDM.
upper_bound
class-attribute
instance-attribute
upper_bound = np.inf
Get the upper bound of values for the EDM.
include_lower_bound
class-attribute
instance-attribute
include_lower_bound = True
Return whether lower_bound is allowed as a value of y.
include_upper_bound
class-attribute
instance-attribute
include_upper_bound = False
Return whether upper_bound is allowed as a value of y.
in_y_range
in_y_range(x) -> np.ndarray
Return True if x is in the valid range of the EDM.
Source code in src/glum/_distribution.py
88 89 90 91 92 93 94 95 96 97 98 99 100 | |
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a distribution if it exists.
Source code in src/glum/_distribution.py
102 103 104 105 106 107 108 | |
variance
variance(mu, dispersion=1, sample_weight=1) -> np.ndarray
Compute the variance function.
The variance of \(Y_i \sim \mathrm{EDM}(\mu_i, \phi / w_i)\) takes the form \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(w_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
variance_derivative
variance_derivative(mu, dispersion=1, sample_weight=1)
Compute the derivative of the variance with respect to mu.
The derivative of the variance is equal to \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(ws_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
unit_deviance_derivative
unit_deviance_derivative(y, mu)
Compute the derivative of the unit deviance with respect to mu.
The derivative of the unit deviance is given by \(2 \times (\mu - y) / v(\mu)\), where \(v(\mu)\) is the unit variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
deviance_derivative
deviance_derivative(y, mu, sample_weight=1)
Compute the derivative of the deviance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples)(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
eta_mu_deviance
eta_mu_deviance(link: Link, factor: float, cur_eta, X_dot_d, y, sample_weight) -> tuple[np.ndarray, np.ndarray, float]
Compute eta, mu and the deviance.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The linear predictor, |
(ndarray, shape(shape[0]))
|
The link-function-transformed prediction, |
float
|
The deviance. |
Source code in src/glum/_distribution.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
rowwise_gradient_hessian
rowwise_gradient_hessian(link: Link, coef, dispersion, X: Union[MatrixBase, StandardizedMatrix], y, sample_weight, eta, mu, offset=None)
Compute the gradient and negative Hessian of the log likelihood row-wise.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The gradient of the log likelihood, row-wise. |
(ndarray, shape(shape[0]))
|
The negative Hessian of the log likelihood, row-wise. |
Source code in src/glum/_distribution.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
unit_variance
unit_variance(mu) -> np.ndarray
Compute the unit variance.
The unit variance, \(v(\mu) \equiv b''((b')^{-1} (\mu))\), determines the variance as a function of the mean \(\mu\) by \(\mathrm{var}(y_i) = v(\mu_i) \times \phi / w_i\). It can also be derived from the unit deviance \(d(y, \mu)\) as
See also variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
909 910 | |
unit_variance_derivative
unit_variance_derivative(mu) -> np.ndarray
Compute the derivative of the unit variance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
912 913 | |
deviance
deviance(y, mu, sample_weight=None) -> float
Compute the deviance.
The deviance is a weighted sum of the unit deviances. In terms of the unit log likelihood \(\ell\), it equals \(2 \sum_i [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which the variance is inversely proportional. |
None
|
Source code in src/glum/_distribution.py
915 916 917 918 919 920 921 922 | |
unit_deviance
unit_deviance(y, mu)
Compute the unit deviance.
In terms of the unit log likelihood \(\ell\), the unit deviance is \(2 [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
924 925 | |
log_likelihood
log_likelihood(y, mu, sample_weight=None, dispersion=None) -> float
Compute the log likelihood.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Sample weights. |
None
|
dispersion
|
(float, optional(default=None))
|
Dispersion parameter \(\phi\). Estimated if |
None
|
Source code in src/glum/_distribution.py
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 | |
dispersion
dispersion(y, mu, sample_weight=None, ddof=1, method='pearson') -> float
Estimate the dispersion parameter \(\phi\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inversely proportional. |
None
|
ddof
|
(int, optional(default=1))
|
Degrees of freedom consumed by the model for |
1
|
method
|
Whether to base the estimate on the Pearson residuals or the deviance. |
'pearson'
|
Source code in src/glum/_distribution.py
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 | |
TweedieDistribution
TweedieDistribution(power=0)
Bases: ExponentialDispersionModel
A class for the Tweedie distribution.
A Tweedie distribution with mean \(\mu = \mathrm{E}(Y)\) is uniquely defined by its mean-variance relationship \(\mathrm{var}(Y) \propto \mu^{\mathrm{p}}\).
Special cases are:
| Power | Distribution | Support |
|---|---|---|
| 0 | Normal | (-∞, +∞) |
| 1 | Poisson | [0, +∞) |
| (1, 2) | Compound Poisson | [0, +∞) |
| 2 | Gamma | (0, +∞) |
| 3 | Inverse Gaussian | (0, +∞) |
See the documentation of the superclass,
ExponentialDispersionModel, for details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
power
|
(float, optional(default=0))
|
The variance power of the |
0
|
Source code in src/glum/_distribution.py
572 573 574 | |
upper_bound
class-attribute
instance-attribute
upper_bound = np.inf
Get the upper bound of values for the EDM.
include_upper_bound
class-attribute
instance-attribute
include_upper_bound = False
Return whether upper_bound is allowed as a value of y.
power
property
writable
power
Return the Tweedie power parameter.
lower_bound
property
lower_bound: float
Get the lower bound of values for the EDM.
include_lower_bound
property
include_lower_bound: bool
Return whether lower_bound is allowed as a value of y.
in_y_range
in_y_range(x) -> np.ndarray
Return True if x is in the valid range of the EDM.
Source code in src/glum/_distribution.py
88 89 90 91 92 93 94 95 96 97 98 99 100 | |
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a distribution if it exists.
Source code in src/glum/_distribution.py
102 103 104 105 106 107 108 | |
variance
variance(mu, dispersion=1, sample_weight=1) -> np.ndarray
Compute the variance function.
The variance of \(Y_i \sim \mathrm{EDM}(\mu_i, \phi / w_i)\) takes the form \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(w_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
variance_derivative
variance_derivative(mu, dispersion=1, sample_weight=1)
Compute the derivative of the variance with respect to mu.
The derivative of the variance is equal to \(v(\mu_i) \times \phi / w_i\), where \(v(\mu)\) is the unit variance and \(ws_i\) are weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
dispersion
|
(float, optional(default=1))
|
Dispersion parameter \(\phi\). |
1
|
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
unit_deviance_derivative
unit_deviance_derivative(y, mu)
Compute the derivative of the unit deviance with respect to mu.
The derivative of the unit deviance is given by \(2 \times (\mu - y) / v(\mu)\), where \(v(\mu)\) is the unit variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
deviance_derivative
deviance_derivative(y, mu, sample_weight=1)
Compute the derivative of the deviance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples)(default=1))
|
Weights or exposure to which variance is inverse proportional. |
1
|
Returns:
| Type | Description |
|---|---|
(array - like, shape(n_samples))
|
|
Source code in src/glum/_distribution.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
eta_mu_deviance
eta_mu_deviance(link: Link, factor: float, cur_eta, X_dot_d, y, sample_weight) -> tuple[np.ndarray, np.ndarray, float]
Compute eta, mu and the deviance.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The linear predictor, |
(ndarray, shape(shape[0]))
|
The link-function-transformed prediction, |
float
|
The deviance. |
Source code in src/glum/_distribution.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
rowwise_gradient_hessian
rowwise_gradient_hessian(link: Link, coef, dispersion, X: Union[MatrixBase, StandardizedMatrix], y, sample_weight, eta, mu, offset=None)
Compute the gradient and negative Hessian of the log likelihood row-wise.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(shape[0]))
|
The gradient of the log likelihood, row-wise. |
(ndarray, shape(shape[0]))
|
The negative Hessian of the log likelihood, row-wise. |
Source code in src/glum/_distribution.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
unit_variance
unit_variance(mu)
Compute the unit variance.
The unit variance, \(v(\mu) \equiv b''((b')^{-1} (\mu))\), determines the variance as a function of the mean \(\mu\) by \(\mathrm{var}(y_i) = v(\mu_i) \times \phi / w_i\). It can also be derived from the unit deviance \(d(y, \mu)\) as
See also variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
615 616 617 | |
unit_variance_derivative
unit_variance_derivative(mu)
Compute the derivative of the unit variance with respect to mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
619 620 621 | |
deviance
deviance(y, mu, sample_weight=None) -> float
Compute the deviance.
The deviance is a weighted sum of the unit deviances. In terms of the unit log likelihood \(\ell\), it equals \(2 \sum_i [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which the variance is inversely proportional. |
None
|
Source code in src/glum/_distribution.py
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 | |
unit_deviance
unit_deviance(y, mu)
Compute the unit deviance.
In terms of the unit log likelihood \(\ell\), the unit deviance is \(2 [\ell(y_i, y_i, \phi) - \ell(y_i, \mu, \phi)]\), i.e. twice the difference between the log likelihood of a saturated model (with one parameter per observation) and the model at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
Source code in src/glum/_distribution.py
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 | |
log_likelihood
log_likelihood(y, mu, sample_weight=None, dispersion=None) -> float
Compute the log likelihood.
For 1 < p < 2, we use the series approximation by Dunn and Smyth
(2005) to compute the normalization term.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Sample weights. |
None
|
dispersion
|
(float, optional(default=None))
|
Dispersion parameter \(\phi\). Estimated if |
None
|
Source code in src/glum/_distribution.py
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 | |
dispersion
dispersion(y, mu, sample_weight=None, ddof=1, method='pearson') -> float
Estimate the dispersion parameter \(\phi\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array - like, shape(n_samples))
|
Target values. |
required |
mu
|
(array - like, shape(n_samples))
|
Predicted mean. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=1))
|
Weights or exposure to which variance is inversely proportional. |
None
|
ddof
|
(int, optional(default=1))
|
Degrees of freedom consumed by the model for |
1
|
method
|
Whether to base the estimate on the Pearson residuals or the deviance. |
'pearson'
|
Source code in src/glum/_distribution.py
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 | |
GeneralizedLinearRegressor
GeneralizedLinearRegressor(*, alpha=None, l1_ratio=0, P1: Optional[Union[str, ndarray]] = 'identity', P2: Optional[Union[str, ndarray, spmatrix]] = 'identity', fit_intercept=True, family: Union[str, ExponentialDispersionModel] = 'normal', link: Union[str, Link] = 'auto', solver: str = 'auto', max_iter=100, max_inner_iter=100000, gradient_tol: Optional[float] = None, step_size_tol: Optional[float] = None, hessian_approx: float = 0.0, warm_start: bool = False, alpha_search: bool = False, alphas: Optional[ndarray] = None, n_alphas: int = 100, min_alpha_ratio: Optional[float] = None, min_alpha: Optional[float] = None, start_params: Optional[ndarray] = None, selection: str = 'cyclic', random_state=None, copy_X: Optional[bool] = None, check_input: bool = True, verbose=0, scale_predictors: bool = False, lower_bounds: Optional[ndarray] = None, upper_bounds: Optional[ndarray] = None, A_ineq: Optional[ndarray] = None, b_ineq: Optional[ndarray] = None, monotonic_constraints: Optional[Mapping[str, str]] = None, force_all_finite: bool = True, drop_first: bool = False, robust: bool = True, expected_information: bool = False, formula: Optional[FormulaSpec] = None, interaction_separator: str = ':', categorical_format: str = '{name}[{category}]', cat_missing_method: str = 'fail', cat_missing_name: str = '(MISSING)')
Bases: GeneralizedLinearRegressorBase
Regression via a Generalized Linear Model (GLM) with penalties.
GLMs based on a reproductive Exponential Dispersion Model (EDM) aimed at
fitting and predicting the mean of the target y as mu=h(X*w).
Therefore, the fit minimizes the following objective function with combined
L1 and L2 priors as regularizer:
1/(2*sum(s)) * deviance(y, h(X*w); s)
+ alpha * l1_ratio * ||P1*w||_1
+ 1/2 * alpha * (1 - l1_ratio) * w*P2*w
with inverse link function h and s=sample_weight.
Note that, for alpha=0 the unregularized GLM is recovered.
This is not the default behavior (see alpha parameter description for details).
Additionally, for sample_weight=None, one has s_i=1 and
sum(s)=n_samples. For P1=P2='identity', the penalty is the elastic net:
alpha * l1_ratio * ||w||_1 + 1/2 * alpha * (1 - l1_ratio) * ||w||_2^2.
If you are interested in controlling the L1 and L2 penalties separately, keep in mind that this is equivalent to:
a * L1 + b * L2,
where:
alpha = a + b and l1_ratio = a / (a + b).
The parameter l1_ratio corresponds to alpha in the R package glmnet,
while alpha corresponds to the lambda parameter in glmnet.
Specifically, l1_ratio = 1 is the lasso penalty.
Read more in background.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
(float, array - like)
|
Constant that multiplies the penalty terms and thus determines the
regularization strength. If |
float
|
l1_ratio
|
(float, optional(default=0))
|
The elastic net mixing parameter, with |
0
|
P1
|
(identity, array - like, None)
|
(default='identity')
This array controls the strength of the regularization for each coefficient
independently. A high value will lead to higher regularization while a value of
zero will remove the regularization on this parameter.
Note that |
'identity'
|
P2
|
'identity', array-like, sparse matrix, None
|
With this option, you can set the P2 matrix in the L2 penalty
|
'identity'
|
fit_intercept
|
(bool, optional(default=True))
|
Specifies if a constant (a.k.a. bias or intercept) should be
added to the linear predictor ( |
True
|
family
|
(str or ExponentialDispersionModel, optional(default=normal))
|
The distributional assumption of the GLM, i.e. the loss function to
minimize. If a string, one of: |
'normal'
|
link
|
(auto, identity, log, logit, cloglog)
|
The link function of the GLM, i.e. mapping from linear
predictor (
|
'auto'
|
solver
|
(auto, closed - form, irls - cd, irls - ls, lbfgs, trust - constr)
|
Algorithm to use in the optimization problem:
|
'auto'
|
max_iter
|
(int, optional(default=100))
|
The maximal number of iterations for solver algorithms. |
100
|
max_inner_iter
|
The maximal number of iterations for the inner solver in the IRLS-CD
algorithm. This parameter is only used when |
100000
|
|
gradient_tol
|
(float, optional(default=None))
|
Stopping criterion. If For the IRLS-LS, L-BFGS and trust-constr solvers, the iteration
will stop when If you wish to only use a step-size tolerance, set |
None
|
step_size_tol
|
Optional[float]
|
Alternative stopping criterion. For the IRLS-LS and IRLS-CD solvers, the
iteration will stop when the L2 norm of the step size is less than
|
None
|
hessian_approx
|
float
|
The threshold below which data matrix rows will be ignored for updating the Hessian. See the algorithm documentation for the IRLS algorithm for further details. |
0.0
|
warm_start
|
(bool, optional(default=False))
|
Whether to reuse the solution of the previous call to |
False
|
alpha_search
|
(bool, optional(default=False))
|
Whether to search along the regularization path for the best alpha.
When set to
|
False
|
alphas
|
DEPRECATED. Use `alpha` instead.
|
|
None
|
n_alphas
|
(int, optional(default=100))
|
Number of alphas along the regularization path |
100
|
min_alpha_ratio
|
(float, optional(default=None))
|
Length of the path. |
None
|
min_alpha
|
(float, optional(default=None))
|
Minimum alpha to estimate the model with. The grid will then be created
over |
None
|
start_params
|
array-like, shape (n_features*,), optional (default=None)
|
Relevant only if |
None
|
selection
|
(str, optional(default=cyclic))
|
For the CD solver 'cd', the coordinates (features) can be updated in
either cyclic or random order. If set to |
'cyclic'
|
random_state
|
(int or RandomState, optional(default=None))
|
The seed of the pseudo random number generator that selects a random
feature to be updated for the CD solver. If an integer, |
None
|
copy_X
|
(bool, optional(default=None))
|
Whether to copy |
None
|
check_input
|
(bool, optional(default=True))
|
Whether to bypass several checks on input: |
True
|
verbose
|
(int, optional(default=0))
|
For the IRLS solver, any positive number will result in a pretty
progress bar showing convergence. This features requires having the
tqdm package installed. For the L-BFGS and |
0
|
scale_predictors
|
bool
|
If Reported coefficient estimates are always at the original scale. Advanced developer note: Internally, predictors are always rescaled for
computational reasons, but this only affects results if
|
False
|
lower_bounds
|
(array - like, shape(n_features), optional(default=None))
|
Set a lower bound for the coefficients. Setting bounds forces the use
of the coordinate descent solver ( |
None
|
upper_bounds
|
array-like, shape=(n_features,), optional (default=None)
|
See |
None
|
A_ineq
|
array-like, shape=(n_constraints, n_features), optional (default=None)
|
Constraint matrix for linear inequality constraints of the form
|
None
|
b_ineq
|
array-like, shape=(n_constraints,), optional (default=None)
|
Constraint vector for linear inequality constraints of the form
|
None
|
monotonic_constraints
|
(dict, optional(default=None))
|
Mapping from variable names in |
None
|
drop_first
|
(bool, optional(default=False))
|
If |
False
|
robust
|
(bool, optional(default=False))
|
If true, then robust standard errors are computed by default. |
True
|
expected_information
|
(bool, optional(default=False))
|
If true, then the expected information matrix is computed by default. Only relevant when computing robust standard errors. |
False
|
formula
|
FormulaSpec
|
A formula accepted by formulaic. It can either be a one-sided formula, in
which case |
None
|
interaction_separator
|
str
|
The separator between the names of interacted variables. |
':'
|
categorical_format
|
str
|
Format string for categorical features. The format string should
contain the placeholder |
'{name}[{category}]'
|
cat_missing_method
|
str
|
How to handle missing values in categorical columns. Only used if
|
'fail'
|
cat_missing_name
|
str
|
Name of the category to which missing values will be converted if
|
'(MISSING)'
|
Attributes:
| Name | Type | Description |
|---|---|---|
coef_ |
(array, shape(n_features))
|
Estimated coefficients for the linear predictor (X*coef_+intercept_) in the GLM. |
intercept_ |
float
|
Intercept (a.k.a. bias) added to linear predictor. |
n_iter_ |
int
|
Actual number of iterations used in solver. |
col_means_ |
(array, shape(n_features))
|
The means of the columns of the design matrix |
col_stds_ |
(array, shape(n_features))
|
The standard deviations of the columns of the design matrix |
feature_names_ |
list of str
|
The names of the coefficients. |
Notes
The fit itself does not need outcomes to be from an EDM, but only assumes the first two moments to be \(\mu_i \equiv \mathrm{E}(y_i) = h(x_i' w)\) and \(\mathrm{var}(y_i) = (\phi / s_i) v(\mu_i)\). The unit variance function \(v(\mu_i)\) is a property of and given by the specific EDM; see background.
The parameters \(w\) (coef_ and intercept_) are estimated by
minimizing the deviance plus penalty term, which is equivalent to
(penalized) maximum likelihood estimation.
If the target y is a ratio, appropriate sample weights s should be
provided. As an example, consider Poisson distributed counts z
(integers) and weights s = exposure (time, money, persons years, ...).
Then you fit y ≡ z/s, i.e.
GeneralizedLinearModel(family='poisson').fit(X, y, sample_weight=s). The
weights are necessary for the right (finite sample) mean. Consider
\(\bar{y} = \sum_i s_i y_i / \sum_i s_i\): in this case, one might
say that \(y\) follows a 'scaled' Poisson distribution. The same holds
for other distributions.
References
For the coordinate descent implementation:
- Guo-Xun Yuan, Chia-Hua Ho, Chih-Jen Lin An Improved GLMNET for L1-regularized Logistic Regression, Journal of Machine Learning Research 13 (2012) 1999-2030 https://www.csie.ntu.edu.tw/~cjlin/papers/l1_glmnet/long-glmnet.pdf
Source code in src/glum/_glm.py
2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 | |
family_instance
property
family_instance: ExponentialDispersionModel
Return an ExponentialDispersionModel.
report_diagnostics
report_diagnostics(*, full_report: bool = False, custom_columns: Optional[Sequence] = None) -> None
Print diagnostics to stdout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
full_report
|
(bool, optional(default=False))
|
Print all available information. When |
False
|
custom_columns
|
(iterable, optional(default=None))
|
Print only the specified columns. |
None
|
Source code in src/glum/_glm.py
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 | |
get_formatted_diagnostics
get_formatted_diagnostics(*, full_report: bool = False, custom_columns: Optional[Sequence] = None) -> Union[str, pd.DataFrame]
Get formatted diagnostics which can be printed with report_diagnostics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
full_report
|
(bool, optional(default=False))
|
Print all available information. When |
False
|
custom_columns
|
(iterable, optional(default=None))
|
Print only the specified columns. |
None
|
Source code in src/glum/_glm.py
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 | |
linear_predictor
linear_predictor(X: ArrayLike, offset: Optional[ArrayLike] = None, *, alpha_index: Optional[Union[int, Sequence[int]]] = None, alpha: Optional[Union[float, Sequence[float]]] = None, context: Optional[Union[int, Mapping[str, Any]]] = None)
Compute the linear predictor, X * coef_ + intercept_.
If alpha_search is True, but alpha_index and alpha are both
None, the predictions are for the last alpha value self._alphas[-1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
(array - like, shape(n_samples, n_features))
|
Observations. |
required |
offset
|
(array - like, shape(n_samples), optional(default=None))
|
Offset added to the linear predictor. |
None
|
alpha_index
|
int or sequence of int, optional (default=None)
|
Index (or indices) into the fitted alpha path. Only valid when
|
None
|
alpha
|
float or sequence of float, optional (default=None)
|
Alpha value(s) to predict at, resolved to the closest index on
the fitted alpha path. Only valid when |
None
|
context
|
(int or mapping, optional(default=None))
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Shape |
Source code in src/glum/_glm.py
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 | |
predict
predict(X: ShapedArrayLike, sample_weight: Optional[ArrayLike] = None, offset: Optional[ArrayLike] = None, *, alpha_index: Optional[Union[int, Sequence[int]]] = None, alpha: Optional[Union[float, Sequence[float]]] = None, context: Optional[Union[int, Mapping[str, Any]]] = None)
Predict using GLM with feature matrix X.
If alpha_search is True, but alpha_index and alpha are
both None, we use the last alpha value self._alphas[-1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
(array - like, shape(n_samples, n_features))
|
Observations. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=None))
|
Sample weights to multiply predictions by. |
None
|
offset
|
(array - like, shape(n_samples), optional(default=None))
|
|
None
|
alpha_index
|
(int or list[int], optional(default=None))
|
Sets the index of the alpha(s) to use in case |
None
|
alpha
|
(float or list[float], optional(default=None))
|
Sets the alpha(s) to use in case |
None
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Shape |
Source code in src/glum/_glm.py
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 | |
coef_table
coef_table(X=None, y=None, sample_weight=None, offset=None, *, confidence_level=0.95, mu=None, dispersion=None, robust=None, clusters: ndarray = None, expected_information=None, context: Optional[Union[int, Mapping[str, Any]]] = None)
Get a table of of the regression coefficients.
Includes coefficient estimates, standard errors, t-values, p-values and confidence intervals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
confidence_level
|
float
|
The confidence level for the confidence intervals. |
0.95
|
X
|
array-like, sparse matrix
|
Training data. Can be omitted if a covariance matrix has already been computed or if standard errors, etc. are not desired. |
array-like
|
y
|
(array - like, shape(n_samples))
|
Target values. Can be omitted if a covariance matrix has already been computed. |
None
|
mu
|
array - like
|
Array with predictions. Estimated if absent. |
None
|
offset
|
array - like
|
Array with additive offsets. |
None
|
sample_weight
|
(array - like, shape(n_samples))
|
Individual weights for each sample. |
None
|
dispersion
|
float
|
The dispersion parameter. Estimated if absent. |
None
|
robust
|
boolean
|
Whether to compute robust standard errors instead of normal ones.
If not specified, the model's |
None
|
clusters
|
array - like
|
Array with cluster membership. Clustered standard errors are computed if clusters is not None. |
None
|
expected_information
|
boolean
|
Whether to use the expected or observed information matrix.
Only relevant when computing robust standard errors.
If not specified, the model's |
None
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A table of the regression results. |
Source code in src/glum/_glm.py
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 | |
wald_test
wald_test(X=None, y=None, sample_weight=None, offset=None, *, R: Optional[ndarray] = None, features: Optional[Union[str, list[str]]] = None, terms: Optional[Union[str, list[str]]] = None, formula: Optional[str] = None, r: Optional[Sequence] = None, mu=None, dispersion=None, robust=None, clusters: ndarray = None, expected_information=None, context: Optional[Union[int, Mapping[str, Any]]] = None) -> WaldTestResult
Compute the Wald test statistic and p-value for a linear hypothesis.
The left hand side of the hypothesis may be specified in the following ways:
R: The restriction matrix representing the linear combination of coefficients to test.features: The name of a feature or a list of features to test.terms: The name of a term or a list of terms to test.formula: A formula string specifying the hypothesis to test.
The right hand side of the tested hypothesis is specified by r. In the
case of a terms-based test, the null hypothesis is that each coefficient
relating to a term equals the corresponding value in r.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Training data. Can be omitted if a covariance matrix has already been computed. |
array-like
|
y
|
(array - like, shape(n_samples))
|
Target values. Can be omitted if a covariance matrix has already been computed. |
None
|
sample_weight
|
(array - like, shape(n_samples))
|
Individual weights for each sample. |
None
|
offset
|
array - like
|
Array with additive offsets. |
None
|
R
|
ndarray
|
The restriction matrix representing the linear combination of coefficients to test. |
None
|
features
|
Union[str, list[str]]
|
The name of a feature or a list of features to test. |
None
|
terms
|
Union[str, list[str]]
|
The name of a term or a list of terms to test. It can cover one or more
coefficients. In the case of a model based on a formula, a term is one
of the expressions separated by |
None
|
r
|
Sequence
|
The vector representing the values of the linear combination. If None, the test is for whether the linear combinations of the coefficients are zero. |
None
|
mu
|
array - like
|
Array with predictions. Estimated if absent. |
None
|
dispersion
|
float
|
The dispersion parameter. Estimated if absent. |
None
|
robust
|
boolean
|
Whether to compute robust standard errors instead of normal ones.
If not specified, the model's |
None
|
clusters
|
array - like
|
Array with cluster membership. Clustered standard errors are computed if clusters is not None. |
None
|
expected_information
|
boolean
|
Whether to use the expected or observed information matrix.
Only relevant when computing robust standard errors.
If not specified, the model's |
None
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Returns:
| Type | Description |
|---|---|
WaldTestResult
|
NamedTuple with test statistic, p-value, and degrees of freedom. |
Source code in src/glum/_glm.py
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 | |
std_errors
std_errors(X=None, y=None, sample_weight=None, offset=None, *, mu=None, dispersion=None, robust=None, clusters: ndarray = None, expected_information=None, store_covariance_matrix=False, context: Optional[Union[int, Mapping[str, Any]]] = None)
Calculate standard errors for generalized linear models.
See covariance_matrix for an in-depth explanation of how the
standard errors are computed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Training data. Can be omitted if a covariance matrix has already been computed. |
array-like
|
y
|
(array - like, shape(n_samples))
|
Target values. Can be omitted if a covariance matrix has already been computed. |
None
|
sample_weight
|
(array - like, shape(n_samples))
|
Individual weights for each sample. |
None
|
offset
|
array - like
|
Array with additive offsets. |
None
|
mu
|
array - like
|
Array with predictions. Estimated if absent. |
None
|
dispersion
|
float
|
The dispersion parameter. Estimated if absent. |
None
|
robust
|
boolean
|
Whether to compute robust standard errors instead of normal ones.
If not specified, the model's |
None
|
clusters
|
array - like
|
Array with cluster membership. Clustered standard errors are computed if clusters is not None. |
None
|
expected_information
|
boolean
|
Whether to use the expected or observed information matrix.
Only relevant when computing robust standard errors.
If not specified, the model's |
None
|
store_covariance_matrix
|
boolean
|
Whether to store the covariance matrix in the model instance. If a covariance matrix has already been stored, it will be overwritten. |
False
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Source code in src/glum/_glm.py
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 | |
covariance_matrix
covariance_matrix(X=None, y=None, sample_weight=None, offset=None, *, mu=None, dispersion=None, robust=None, clusters: Optional[ndarray] = None, expected_information=None, store_covariance_matrix=False, skip_checks=False, context: Optional[Union[int, Mapping[str, Any]]] = None)
Calculate the covariance matrix for generalized linear models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Training data. Can be omitted if a covariance matrix has already been computed. |
array-like
|
y
|
(array - like, shape(n_samples))
|
Target values. Can be omitted if a covariance matrix has already been computed. |
None
|
mu
|
array - like
|
Array with predictions. Estimated if absent. |
None
|
offset
|
array - like
|
Array with additive offsets. |
None
|
sample_weight
|
(array - like, shape(n_samples))
|
Individual weights for each sample. |
None
|
dispersion
|
float
|
The dispersion parameter. Estimated if absent. |
None
|
robust
|
boolean
|
Whether to compute robust standard errors instead of normal ones.
If not specified, the model's |
None
|
clusters
|
array - like
|
Array with cluster membership. Clustered standard errors are computed if clusters is not None. |
None
|
expected_information
|
boolean
|
Whether to use the expected or observed information matrix.
Only relevant when computing robust standard errors.
If not specified, the model's |
None
|
store_covariance_matrix
|
boolean
|
Whether to store the covariance matrix in the model instance. If a covariance matrix has already been stored, it will be overwritten. |
False
|
skip_checks
|
boolean
|
Whether to skip input validation. For internal use only. |
False
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Notes
We support three types of covariance matrices:
- non-robust
- robust (HC-1)
- clustered
For maximum-likelihood estimator, the covariance matrix takes the form \(\mathcal{H}^{-1}(\theta_0)\mathcal{I}(\theta_0) \mathcal{H}^{-1}(\theta_0)\) where \(\mathcal{H}^{-1}\) is the inverse Hessian and \(\mathcal{I}\) is the Information matrix. The different types of covariance matrices use different approximation of these quantities.
The non-robust covariance matrix is computed as the inverse of the Fisher information matrix. This assumes that the information matrix equality holds.
The robust (HC-1) covariance matrix takes the form \(\mathbf{H}^{−1} (\hat{\theta})\mathbf{G}^{T}(\hat{\theta})\mathbf{G}(\hat{\theta}) \mathbf{H}^{−1}(\hat{\theta})\) where \(\mathbf{H}\) is the empirical Hessian and \(\mathbf{G}\) is the gradient. We apply a finite-sample correction of \(\frac{N}{N-p}\).
The clustered covariance matrix uses a similar approach to the robust (HC-1) covariance matrix. However, instead of using \(\mathbf{G}^{T}( \hat{\theta}\mathbf{G}(\hat{\theta})\) directly, we first sum over all the groups first. The finite-sample correction is affected as well, becoming \(\frac{M}{M-1}\frac{N}{N-p}\) where \(M\) is the number of groups.
References
- Davidson, Russell & MacKinnon, James G. (1993). "Estimation and Inference in Econometrics," OUP Catalogue, Oxford University Press
- Cameron, A. C., & Trivedi, P. K. (2005). "Microeconometrics: methods and applications," Cambridge university press
Source code in src/glum/_glm.py
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 | |
score
score(X: ShapedArrayLike, y: VectorLike, sample_weight: Optional[VectorLike] = None, offset: Optional[ArrayLike] = None, *, context: Optional[Union[int, Mapping[str, Any]]] = None)
Compute \(D^2\), the percentage of deviance explained.
\(D^2\) is a generalization of the coefficient of determination
\(R^2\). The \(R^2\) uses the squared error and the \(D^2\),
the deviance. Note that those two are equal for family='normal'.
\(D^2\) is defined as \(D^2 = 1 - \frac{D(y_{\mathrm{true}}, y_{\mathrm{pred}})} {D_{\mathrm{null}}}\), \(D_{\mathrm{null}}\) is the null deviance, i.e. the deviance of a model with intercept alone. The best possible score is one and it can be negative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Test samples. |
array-like
|
y
|
(array - like, shape(n_samples))
|
True values of target. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=None))
|
Sample weights. |
None
|
offset
|
(array - like, shape(n_samples), optional(default=None))
|
|
None
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Returns:
| Type | Description |
|---|---|
float
|
D^2 of self.predict(X) w.r.t. y. |
Source code in src/glum/_glm.py
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 | |
fit
fit(X: ArrayLike, y: Optional[ArrayLike] = None, sample_weight: Optional[ArrayLike] = None, offset: Optional[ArrayLike] = None, *, store_covariance_matrix: bool = False, clusters: Optional[ndarray] = None, weights_sum: Optional[float] = None, context: Optional[Union[int, Mapping[str, Any]]] = None)
Fit a Generalized Linear Model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Training data. Note that a |
array-like
|
y
|
(array - like, shape(n_samples))
|
Target values. |
None
|
sample_weight
|
(array - like, shape(n_samples), optional(default=None))
|
Individual weights w_i for each sample. Note that, for an
Exponential Dispersion Model (EDM), one has
\(\mathrm{var}(y_i) = \phi \times v(mu) / w_i\).
If \(y_i \sim EDM(\mu, \phi / w_i)\), then
\(\sum w_i y_i / \sum w_i \sim EDM(\mu, \phi / \sum w_i)\),
i.e. the mean of \(y\) is a weighted average with weights equal
to |
None
|
offset
|
Optional[ArrayLike]
|
Added to linear predictor. An offset of 3 will increase expected
|
None
|
store_covariance_matrix
|
(bool, optional(default=False))
|
Whether to estimate and store the covariance matrix of the parameter
estimates. If |
False
|
clusters
|
array - like
|
Array with cluster membership. Clustered standard errors are computed if clusters is not None. |
None
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
weights_sum
|
Optional[float]
|
|
None
|
Returns:
| Type | Description |
|---|---|
self
|
|
Source code in src/glum/_glm.py
2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 | |
aic
aic(X: ArrayLike, y: VectorLike, sample_weight: Optional[ArrayLike] = None, *, context: Optional[Union[int, Mapping[str, Any]]] = None)
Akaike's information criteria. Computed as:
\(-2\log\hat{\mathcal{L}} + 2\hat{k}\) where
\(\hat{\mathcal{L}}\) is the maximum likelihood estimate of the
model, and \(\hat{k}\) is the effective number of parameters. See
_compute_information_criteria for more information on the computation
of \(\hat{k}\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Same data as used in 'fit' |
array-like
|
y
|
(array - like, shape(n_samples))
|
Same data as used in 'fit' |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=None))
|
Same data as used in 'fit' |
None
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Source code in src/glum/_glm.py
3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 | |
aicc
aicc(X: ArrayLike, y: VectorLike, sample_weight: Optional[ArrayLike] = None, *, context: Optional[Union[int, Mapping[str, Any]]] = None)
Second-order Akaike's information criteria (or small sample AIC).
Computed as:
\(-2\log\hat{\mathcal{L}} + 2\hat{k} + \frac{2k(k+1)}{n-k-1}\)
where \(\hat{\mathcal{L}}\) is the maximum likelihood estimate of
the model, \(n\) is the number of training instances, and
\(\hat{k}\) is the effective number of parameters. See
_compute_information_criteria for more information on the computation
of \(\hat{k}\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Same data as used in 'fit' |
array-like
|
y
|
(array - like, shape(n_samples))
|
Same data as used in 'fit' |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=None))
|
Same data as used in 'fit' |
None
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Source code in src/glum/_glm.py
3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 | |
bic
bic(X: ArrayLike, y: VectorLike, sample_weight: Optional[ArrayLike] = None, *, context: Optional[Union[int, Mapping[str, Any]]] = None)
Bayesian information criterion. Computed as:
\(-2\log\hat{\mathcal{L}} + k\log(n)\) where
\(\hat{\mathcal{L}}\) is the maximum likelihood estimate of the
model, \(n\) is the number of training instances, and
\(\hat{k}\) is the effective number of parameters. See
_compute_information_criteria for more information on the computation
of \(\hat{k}\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Same data as used in 'fit' |
array-like
|
y
|
(array - like, shape(n_samples))
|
Same data as used in 'fit' |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=None))
|
Same data as used in 'fit' |
None
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Source code in src/glum/_glm.py
3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 | |
GeneralizedLinearRegressorCV
GeneralizedLinearRegressorCV(*, l1_ratio=0, P1='identity', P2='identity', fit_intercept=True, family: Union[str, ExponentialDispersionModel] = 'normal', link: Union[str, Link] = 'auto', solver: str = 'auto', max_iter=100, max_inner_iter=100000, gradient_tol: Optional[float] = None, step_size_tol: Optional[float] = None, hessian_approx: float = 0.0, warm_start: bool = False, n_alphas: int = 100, alphas: Optional[ndarray] = None, min_alpha_ratio: Optional[float] = None, min_alpha: Optional[float] = None, start_params: Optional[ndarray] = None, selection: str = 'cyclic', random_state=None, copy_X: Optional[bool] = None, check_input: bool = True, verbose=0, scale_predictors: bool = False, lower_bounds: Optional[ndarray] = None, upper_bounds: Optional[ndarray] = None, A_ineq: Optional[ndarray] = None, b_ineq: Optional[ndarray] = None, monotonic_constraints: Optional[Mapping[str, str]] = None, force_all_finite: bool = True, cv=None, n_jobs: Optional[int] = None, drop_first: bool = False, robust: bool = True, expected_information: bool = False, formula: Optional[FormulaSpec] = None, interaction_separator: str = ':', categorical_format: str = '{name}[{category}]', cat_missing_method: str = 'fail', cat_missing_name: str = '(MISSING)')
Bases: GeneralizedLinearRegressorBase
Generalized linear model with iterative fitting along a regularization path.
The best model is selected by cross-validation.
Cross-validated regression via a Generalized Linear Model (GLM) with
penalties. For more on GLMs and on these parameters, see the documentation
for GeneralizedLinearRegressor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
l1_ratio
|
float or array of floats, optional (default=0)
|
If you pass |
0
|
P1
|
(identity, array - like, None)
|
(default='identity')
This array controls the strength of the regularization for each coefficient
independently. A high value will lead to higher regularization while a value of
zero will remove the regularization on this parameter.
Note that |
'identity'
|
P2
|
'identity', array-like, sparse matrix, None
|
With this option, you can set the P2 matrix in the L2 penalty
|
'identity'
|
fit_intercept
|
(bool, optional(default=True))
|
Specifies if a constant (a.k.a. bias or intercept) should be
added to the linear predictor ( |
True
|
family
|
(str or ExponentialDispersionModel, optional(default=normal))
|
The distributional assumption of the GLM, i.e. the loss function to
minimize. If a string, one of: |
'normal'
|
link
|
(auto, identity, log, logit, cloglog)
|
The link function of the GLM, i.e. mapping from linear
predictor (
|
'auto'
|
solver
|
(auto, closed - form, irls - cd, irls - ls, lbfgs, trust - constr)
|
Algorithm to use in the optimization problem:
|
'auto'
|
max_iter
|
(int, optional(default=100))
|
The maximal number of iterations for solver algorithms. |
100
|
max_inner_iter
|
The maximal number of iterations for the inner solver in the IRLS-CD
algorithm. This parameter is only used when |
100000
|
|
gradient_tol
|
(float, optional(default=None))
|
Stopping criterion. If For the IRLS-LS, L-BFGS and trust-constr solvers, the iteration
will stop when If you wish to only use a step-size tolerance, set |
None
|
step_size_tol
|
Optional[float]
|
Alternative stopping criterion. For the IRLS-LS and IRLS-CD solvers, the
iteration will stop when the L2 norm of the step size is less than
|
None
|
hessian_approx
|
float
|
The threshold below which data matrix rows will be ignored for updating the Hessian. See the algorithm documentation for the IRLS algorithm for further details. |
0.0
|
warm_start
|
(bool, optional(default=False))
|
Whether to reuse the solution of the previous call to |
False
|
n_alphas
|
(int, optional(default=100))
|
Number of alphas along the regularization path |
100
|
alphas
|
(array - like, optional(default=None))
|
List of alphas for which to compute the models. If |
None
|
min_alpha_ratio
|
(float, optional(default=None))
|
Length of the path. |
None
|
min_alpha
|
(float, optional(default=None))
|
Minimum alpha to estimate the model with. The grid will then be created
over |
None
|
start_params
|
array-like, shape (n_features*,), optional (default=None)
|
Relevant only if |
None
|
selection
|
(str, optional(default=cyclic))
|
For the CD solver 'cd', the coordinates (features) can be updated in
either cyclic or random order. If set to |
'cyclic'
|
random_state
|
(int or RandomState, optional(default=None))
|
The seed of the pseudo random number generator that selects a random
feature to be updated for the CD solver. If an integer, |
None
|
copy_X
|
(bool, optional(default=None))
|
Whether to copy |
None
|
check_input
|
(bool, optional(default=True))
|
Whether to bypass several checks on input: |
True
|
verbose
|
(int, optional(default=0))
|
For the IRLS solver, any positive number will result in a pretty
progress bar showing convergence. This features requires having the
tqdm package installed. For the L-BFGS solver, set |
0
|
scale_predictors
|
bool
|
If Advanced developer note: Internally, predictors are always rescaled for
computational reasons, but this only affects results if
|
False
|
lower_bounds
|
(array - like, shape(n_features), optional(default=None))
|
Set a lower bound for the coefficients. Setting bounds forces the use
of the coordinate descent solver ( |
None
|
upper_bounds
|
array-like, shape=(n_features), optional (default=None)
|
See |
None
|
A_ineq
|
array-like, shape=(n_constraints, n_features), optional (default=None)
|
Constraint matrix for linear inequality constraints of the form
|
None
|
b_ineq
|
array-like, shape=(n_constraints,), optional (default=None)
|
Constraint vector for linear inequality constraints of the form
|
None
|
monotonic_constraints
|
(dict, optional(default=None))
|
Mapping from variable names in |
None
|
cv
|
int, cross-validation generator or Iterable, optional (default=None)
|
Determines the cross-validation splitting strategy. One of:
For integer/ |
None
|
n_jobs
|
(int, optional(default=None))
|
The maximum number of concurrently running jobs. The number of jobs that
are needed is |
None
|
drop_first
|
(bool, optional(default=False))
|
If |
False
|
formula
|
FormulaSpec
|
A formula accepted by formulaic. It can either be a one-sided formula, in
which case |
None
|
interaction_separator
|
str
|
The separator between the names of interacted variables. |
':'
|
categorical_format
|
str
|
The format string used to generate the names of categorical variables.
Has to include the placeholders |
'{name}[{category}]'
|
Attributes:
| Name | Type | Description |
|---|---|---|
alpha_ |
float
|
The amount of regularization chosen by cross validation. |
alphas_ |
(array, shape(n_l1_ratios, n_alphas))
|
Alphas used by the model. |
l1_ratio_ |
float
|
The compromise between L1 and L2 regularization chosen by cross validation. |
coef_ |
(array, shape(n_features))
|
Estimated coefficients for the linear predictor in the GLM at the
optimal ( |
intercept_ |
float
|
Intercept (a.k.a. bias) added to linear predictor. |
n_iter_ |
int
|
The number of iterations run by the CD solver to reach the specified tolerance for the optimal alpha. |
coef_path_ |
(array, shape(n_folds, n_l1_ratios, n_alphas, n_features))
|
Estimated coefficients for the linear predictor in the GLM at every point along the regularization path, per fold and l1_ratio. |
intercept_path_ |
(array, shape(n_folds, n_l1_ratios, n_alphas, 1))
|
Estimated intercepts at every point along the regularization path, per fold and l1_ratio. |
deviance_path_ |
(array, shape(n_folds, n_l1_ratios, n_alphas))
|
Deviance for the test set on each fold, varying alpha. |
train_deviance_path_ |
(array, shape(n_folds, n_l1_ratios, n_alphas))
|
Deviance for the training set on each fold, varying alpha. |
robust |
(bool, optional(default=False))
|
If true, then robust standard errors are computed by default. |
expected_information |
(bool, optional(default=False))
|
If true, then the expected information matrix is computed by default. Only relevant when computing robust standard errors. |
categorical_format |
(str, optional(default={name}[{category}]))
|
Format string for categorical features. The format string should
contain the placeholder |
cat_missing_method |
str {'fail'|'zero'|'convert'}, default='fail'
|
How to handle missing values in categorical columns. Only used if
|
cat_missing_name |
str, default='(MISSING)'
|
Name of the category to which missing values will be converted if
|
col_means_ |
(array, shape(n_features))
|
The means of the columns of the design matrix |
col_stds_ |
(array, shape(n_features))
|
The standard deviations of the columns of the design matrix |
feature_names_ |
list of str
|
The names of the coefficients. |
Source code in src/glum/_glm_cv.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 | |
family_instance
property
family_instance: ExponentialDispersionModel
Return an ExponentialDispersionModel.
report_diagnostics
report_diagnostics(*, full_report: bool = False, custom_columns: Optional[Sequence] = None) -> None
Print diagnostics to stdout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
full_report
|
(bool, optional(default=False))
|
Print all available information. When |
False
|
custom_columns
|
(iterable, optional(default=None))
|
Print only the specified columns. |
None
|
Source code in src/glum/_glm.py
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 | |
get_formatted_diagnostics
get_formatted_diagnostics(*, full_report: bool = False, custom_columns: Optional[Sequence] = None) -> Union[str, pd.DataFrame]
Get formatted diagnostics which can be printed with report_diagnostics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
full_report
|
(bool, optional(default=False))
|
Print all available information. When |
False
|
custom_columns
|
(iterable, optional(default=None))
|
Print only the specified columns. |
None
|
Source code in src/glum/_glm.py
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 | |
predict
predict(X: ShapedArrayLike, sample_weight: Optional[ArrayLike] = None, offset: Optional[ArrayLike] = None, *, alpha_index: Optional[Union[int, Sequence[int]]] = None, alpha: Optional[Union[float, Sequence[float]]] = None, context: Optional[Union[int, Mapping[str, Any]]] = None)
Predict using GLM with feature matrix X.
If alpha_search is True, but alpha_index and alpha are
both None, we use the last alpha value self._alphas[-1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
(array - like, shape(n_samples, n_features))
|
Observations. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=None))
|
Sample weights to multiply predictions by. |
None
|
offset
|
(array - like, shape(n_samples), optional(default=None))
|
|
None
|
alpha_index
|
(int or list[int], optional(default=None))
|
Sets the index of the alpha(s) to use in case |
None
|
alpha
|
(float or list[float], optional(default=None))
|
Sets the alpha(s) to use in case |
None
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Shape |
Source code in src/glum/_glm.py
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 | |
coef_table
coef_table(X=None, y=None, sample_weight=None, offset=None, *, confidence_level=0.95, mu=None, dispersion=None, robust=None, clusters: ndarray = None, expected_information=None, context: Optional[Union[int, Mapping[str, Any]]] = None)
Get a table of of the regression coefficients.
Includes coefficient estimates, standard errors, t-values, p-values and confidence intervals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
confidence_level
|
float
|
The confidence level for the confidence intervals. |
0.95
|
X
|
array-like, sparse matrix
|
Training data. Can be omitted if a covariance matrix has already been computed or if standard errors, etc. are not desired. |
array-like
|
y
|
(array - like, shape(n_samples))
|
Target values. Can be omitted if a covariance matrix has already been computed. |
None
|
mu
|
array - like
|
Array with predictions. Estimated if absent. |
None
|
offset
|
array - like
|
Array with additive offsets. |
None
|
sample_weight
|
(array - like, shape(n_samples))
|
Individual weights for each sample. |
None
|
dispersion
|
float
|
The dispersion parameter. Estimated if absent. |
None
|
robust
|
boolean
|
Whether to compute robust standard errors instead of normal ones.
If not specified, the model's |
None
|
clusters
|
array - like
|
Array with cluster membership. Clustered standard errors are computed if clusters is not None. |
None
|
expected_information
|
boolean
|
Whether to use the expected or observed information matrix.
Only relevant when computing robust standard errors.
If not specified, the model's |
None
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A table of the regression results. |
Source code in src/glum/_glm.py
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 | |
wald_test
wald_test(X=None, y=None, sample_weight=None, offset=None, *, R: Optional[ndarray] = None, features: Optional[Union[str, list[str]]] = None, terms: Optional[Union[str, list[str]]] = None, formula: Optional[str] = None, r: Optional[Sequence] = None, mu=None, dispersion=None, robust=None, clusters: ndarray = None, expected_information=None, context: Optional[Union[int, Mapping[str, Any]]] = None) -> WaldTestResult
Compute the Wald test statistic and p-value for a linear hypothesis.
The left hand side of the hypothesis may be specified in the following ways:
R: The restriction matrix representing the linear combination of coefficients to test.features: The name of a feature or a list of features to test.terms: The name of a term or a list of terms to test.formula: A formula string specifying the hypothesis to test.
The right hand side of the tested hypothesis is specified by r. In the
case of a terms-based test, the null hypothesis is that each coefficient
relating to a term equals the corresponding value in r.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Training data. Can be omitted if a covariance matrix has already been computed. |
array-like
|
y
|
(array - like, shape(n_samples))
|
Target values. Can be omitted if a covariance matrix has already been computed. |
None
|
sample_weight
|
(array - like, shape(n_samples))
|
Individual weights for each sample. |
None
|
offset
|
array - like
|
Array with additive offsets. |
None
|
R
|
ndarray
|
The restriction matrix representing the linear combination of coefficients to test. |
None
|
features
|
Union[str, list[str]]
|
The name of a feature or a list of features to test. |
None
|
terms
|
Union[str, list[str]]
|
The name of a term or a list of terms to test. It can cover one or more
coefficients. In the case of a model based on a formula, a term is one
of the expressions separated by |
None
|
r
|
Sequence
|
The vector representing the values of the linear combination. If None, the test is for whether the linear combinations of the coefficients are zero. |
None
|
mu
|
array - like
|
Array with predictions. Estimated if absent. |
None
|
dispersion
|
float
|
The dispersion parameter. Estimated if absent. |
None
|
robust
|
boolean
|
Whether to compute robust standard errors instead of normal ones.
If not specified, the model's |
None
|
clusters
|
array - like
|
Array with cluster membership. Clustered standard errors are computed if clusters is not None. |
None
|
expected_information
|
boolean
|
Whether to use the expected or observed information matrix.
Only relevant when computing robust standard errors.
If not specified, the model's |
None
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Returns:
| Type | Description |
|---|---|
WaldTestResult
|
NamedTuple with test statistic, p-value, and degrees of freedom. |
Source code in src/glum/_glm.py
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 | |
std_errors
std_errors(X=None, y=None, sample_weight=None, offset=None, *, mu=None, dispersion=None, robust=None, clusters: ndarray = None, expected_information=None, store_covariance_matrix=False, context: Optional[Union[int, Mapping[str, Any]]] = None)
Calculate standard errors for generalized linear models.
See covariance_matrix for an in-depth explanation of how the
standard errors are computed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Training data. Can be omitted if a covariance matrix has already been computed. |
array-like
|
y
|
(array - like, shape(n_samples))
|
Target values. Can be omitted if a covariance matrix has already been computed. |
None
|
sample_weight
|
(array - like, shape(n_samples))
|
Individual weights for each sample. |
None
|
offset
|
array - like
|
Array with additive offsets. |
None
|
mu
|
array - like
|
Array with predictions. Estimated if absent. |
None
|
dispersion
|
float
|
The dispersion parameter. Estimated if absent. |
None
|
robust
|
boolean
|
Whether to compute robust standard errors instead of normal ones.
If not specified, the model's |
None
|
clusters
|
array - like
|
Array with cluster membership. Clustered standard errors are computed if clusters is not None. |
None
|
expected_information
|
boolean
|
Whether to use the expected or observed information matrix.
Only relevant when computing robust standard errors.
If not specified, the model's |
None
|
store_covariance_matrix
|
boolean
|
Whether to store the covariance matrix in the model instance. If a covariance matrix has already been stored, it will be overwritten. |
False
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Source code in src/glum/_glm.py
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 | |
covariance_matrix
covariance_matrix(X=None, y=None, sample_weight=None, offset=None, *, mu=None, dispersion=None, robust=None, clusters: Optional[ndarray] = None, expected_information=None, store_covariance_matrix=False, skip_checks=False, context: Optional[Union[int, Mapping[str, Any]]] = None)
Calculate the covariance matrix for generalized linear models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Training data. Can be omitted if a covariance matrix has already been computed. |
array-like
|
y
|
(array - like, shape(n_samples))
|
Target values. Can be omitted if a covariance matrix has already been computed. |
None
|
mu
|
array - like
|
Array with predictions. Estimated if absent. |
None
|
offset
|
array - like
|
Array with additive offsets. |
None
|
sample_weight
|
(array - like, shape(n_samples))
|
Individual weights for each sample. |
None
|
dispersion
|
float
|
The dispersion parameter. Estimated if absent. |
None
|
robust
|
boolean
|
Whether to compute robust standard errors instead of normal ones.
If not specified, the model's |
None
|
clusters
|
array - like
|
Array with cluster membership. Clustered standard errors are computed if clusters is not None. |
None
|
expected_information
|
boolean
|
Whether to use the expected or observed information matrix.
Only relevant when computing robust standard errors.
If not specified, the model's |
None
|
store_covariance_matrix
|
boolean
|
Whether to store the covariance matrix in the model instance. If a covariance matrix has already been stored, it will be overwritten. |
False
|
skip_checks
|
boolean
|
Whether to skip input validation. For internal use only. |
False
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Notes
We support three types of covariance matrices:
- non-robust
- robust (HC-1)
- clustered
For maximum-likelihood estimator, the covariance matrix takes the form \(\mathcal{H}^{-1}(\theta_0)\mathcal{I}(\theta_0) \mathcal{H}^{-1}(\theta_0)\) where \(\mathcal{H}^{-1}\) is the inverse Hessian and \(\mathcal{I}\) is the Information matrix. The different types of covariance matrices use different approximation of these quantities.
The non-robust covariance matrix is computed as the inverse of the Fisher information matrix. This assumes that the information matrix equality holds.
The robust (HC-1) covariance matrix takes the form \(\mathbf{H}^{−1} (\hat{\theta})\mathbf{G}^{T}(\hat{\theta})\mathbf{G}(\hat{\theta}) \mathbf{H}^{−1}(\hat{\theta})\) where \(\mathbf{H}\) is the empirical Hessian and \(\mathbf{G}\) is the gradient. We apply a finite-sample correction of \(\frac{N}{N-p}\).
The clustered covariance matrix uses a similar approach to the robust (HC-1) covariance matrix. However, instead of using \(\mathbf{G}^{T}( \hat{\theta}\mathbf{G}(\hat{\theta})\) directly, we first sum over all the groups first. The finite-sample correction is affected as well, becoming \(\frac{M}{M-1}\frac{N}{N-p}\) where \(M\) is the number of groups.
References
- Davidson, Russell & MacKinnon, James G. (1993). "Estimation and Inference in Econometrics," OUP Catalogue, Oxford University Press
- Cameron, A. C., & Trivedi, P. K. (2005). "Microeconometrics: methods and applications," Cambridge university press
Source code in src/glum/_glm.py
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 | |
score
score(X: ShapedArrayLike, y: VectorLike, sample_weight: Optional[VectorLike] = None, offset: Optional[ArrayLike] = None, *, context: Optional[Union[int, Mapping[str, Any]]] = None)
Compute \(D^2\), the percentage of deviance explained.
\(D^2\) is a generalization of the coefficient of determination
\(R^2\). The \(R^2\) uses the squared error and the \(D^2\),
the deviance. Note that those two are equal for family='normal'.
\(D^2\) is defined as \(D^2 = 1 - \frac{D(y_{\mathrm{true}}, y_{\mathrm{pred}})} {D_{\mathrm{null}}}\), \(D_{\mathrm{null}}\) is the null deviance, i.e. the deviance of a model with intercept alone. The best possible score is one and it can be negative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Test samples. |
array-like
|
y
|
(array - like, shape(n_samples))
|
True values of target. |
required |
sample_weight
|
(array - like, shape(n_samples), optional(default=None))
|
Sample weights. |
None
|
offset
|
(array - like, shape(n_samples), optional(default=None))
|
|
None
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Returns:
| Type | Description |
|---|---|
float
|
D^2 of self.predict(X) w.r.t. y. |
Source code in src/glum/_glm.py
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 | |
linear_predictor
linear_predictor(X: ArrayLike, offset: Optional[ArrayLike] = None, *, alpha_index: Optional[Union[int, Sequence[int]]] = None, alpha: Optional[Union[float, Sequence[float]]] = None, context: Optional[Union[int, Mapping[str, Any]]] = None)
Compute the linear predictor, X * coef_ + intercept_.
If alpha_search is True, but alpha_index and alpha are both
None, the predictions are for the last alpha value self._alphas[-1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
(array - like, shape(n_samples, n_features))
|
Observations. |
required |
offset
|
(array - like, shape(n_samples), optional(default=None))
|
Offset added to the linear predictor. |
None
|
alpha_index
|
int or sequence of int, optional (default=None)
|
Index (or indices) into the fitted alpha path. Only valid when
|
None
|
alpha
|
float or sequence of float, optional (default=None)
|
Alpha value(s) to predict at, resolved to the closest index on
the fitted alpha path. Only valid when |
None
|
context
|
(int or mapping, optional(default=None))
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Shape |
Compute the linear predictor, `X * coef_ + intercept_`.
|
|
When neither `alpha_index` nor `alpha` are given, predictions are for the
|
|
best CV-selected `(l1_ratio_, alpha_)`.
|
|
When either `alpha_index` or `alpha` are specified, predictions are for the
|
|
corresponding alpha values on the full-data refit path for the best
|
|
`l1_ratio_`.
|
|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
(array - like, shape(n_samples, n_features))
|
Observations. |
required |
offset
|
(array - like, shape(n_samples), optional(default=None))
|
Offset added to the linear predictor. |
None
|
alpha_index
|
int or sequence of int, optional (default=None)
|
Index (or indices) into the alpha path from the full-data refit.
Incompatible with |
None
|
alpha
|
float or sequence of float, optional (default=None)
|
Alpha value(s) to predict at, resolved to the closest index on
the refit alpha path. Incompatible with |
None
|
context
|
(int or mapping, optional(default=None))
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Shape |
Source code in src/glum/_glm_cv.py
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | |
fit
fit(X: ArrayLike, y: Optional[ArrayLike] = None, sample_weight: Optional[ArrayLike] = None, offset: Optional[ArrayLike] = None, *, store_covariance_matrix: bool = False, clusters: Optional[ndarray] = None, context: Optional[Union[int, Mapping[str, Any]]] = None)
Choose the best model along a 'regularization path' by cross-validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Training data. Note that a |
array-like
|
y
|
(array - like, shape(n_samples))
|
Target values. |
None
|
sample_weight
|
(array - like, shape(n_samples), optional(default=None))
|
Individual weights w_i for each sample. Note that, for an
Exponential Dispersion Model (EDM), one has
\(\mathrm{var}(y_i) = \phi \times v(mu) / w_i\). If
\(y_i \sim EDM(\mu, \phi / w_i)\), then
\(\sum w_i y_i / \sum w_i \sim EDM(\mu, \phi / \sum w_i)\),
i.e. the mean of \(y\) is a weighted average with weights equal
to |
None
|
offset
|
Optional[ArrayLike]
|
Added to linear predictor. An offset of 3 will increase expected
|
None
|
store_covariance_matrix
|
(bool, optional(default=False))
|
Whether to store the covariance matrix of the parameter estimates corresponding to the best best model. |
False
|
clusters
|
array - like
|
Array with cluster membership. Clustered standard errors are computed if clusters is not None. |
None
|
context
|
Optional[Union[int, Mapping[str, Any]]]
|
The context to add to the evaluation context of the formula with,
e.g., custom transforms. If an integer, the context is taken from
the stack frame of the caller at the given depth. Otherwise, a
mapping from variable names to values is expected. By default,
no context is added. Set |
None
|
Source code in src/glum/_glm_cv.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 | |
CloglogLink
Bases: Link
The complementary log-log link function log(-log(1-mu)).
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a link function if it exists.
Source code in src/glum/_link.py
96 97 98 99 100 101 102 | |
link
link(mu)
Compute the link function.
The link function g links the mean, mu ≡ E(Y), to the linear
predictor, X * w, so that g(mu) is equal to the linear predictor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Usually the (predicted) mean. |
required |
Source code in src/glum/_link.py
281 282 | |
derivative
derivative(mu)
Compute the derivative of the link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Usually the (predicted) mean. |
required |
Source code in src/glum/_link.py
284 285 | |
inverse
inverse(lin_pred)
Compute the inverse link function.
The inverse link function h gives the inverse relationship between
the linear predictor, X * w, and the mean, mu ≡ E(Y), so that
h(X * w) = mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
287 288 289 290 | |
is_saturated
is_saturated(lin_pred) -> np.ndarray
Whether inverse(lin_pred) is numerically at the boundary of its range.
Only links that clip their inverse at the boundary of the unit interval, such as the logit and cloglog links, can be saturated.
Source code in src/glum/_link.py
292 293 294 295 296 297 | |
inverse_derivative
inverse_derivative(lin_pred)
Compute the derivative of the inverse link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
299 300 | |
inverse_derivative2
inverse_derivative2(lin_pred)
Compute second derivative of the inverse link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
302 303 | |
IdentityLink
Bases: Link
The identity link function.
is_saturated
is_saturated(lin_pred) -> np.ndarray
Whether inverse(lin_pred) is numerically at the boundary of its range.
Only links that clip their inverse at the boundary of the unit interval, such as the logit and cloglog links, can be saturated.
Source code in src/glum/_link.py
26 27 28 29 30 31 32 | |
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a link function if it exists.
Source code in src/glum/_link.py
96 97 98 99 100 101 102 | |
link
link(mu)
Compute the link function.
The link function g links the mean, mu ≡ E(Y), to the linear
predictor, X * w, so that g(mu) is equal to the linear predictor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Usually the (predicted) mean. |
required |
Source code in src/glum/_link.py
205 206 | |
derivative
derivative(mu)
Compute the derivative of the link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Usually the (predicted) mean. |
required |
Source code in src/glum/_link.py
208 209 | |
inverse
inverse(lin_pred)
Compute the inverse link function.
The inverse link function h gives the inverse relationship between
the linear predictor, X * w, and the mean, mu ≡ E(Y), so that
h(X * w) = mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
211 212 | |
inverse_derivative
inverse_derivative(lin_pred)
Compute the derivative of the inverse link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
214 215 | |
inverse_derivative2
inverse_derivative2(lin_pred)
Compute second derivative of the inverse link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
217 218 | |
Link
Abstract base class for link functions.
is_saturated
is_saturated(lin_pred) -> np.ndarray
Whether inverse(lin_pred) is numerically at the boundary of its range.
Only links that clip their inverse at the boundary of the unit interval, such as the logit and cloglog links, can be saturated.
Source code in src/glum/_link.py
26 27 28 29 30 31 32 | |
link
abstractmethod
link(mu)
Compute the link function.
The link function g links the mean, mu ≡ E(Y), to the linear
predictor, X * w, so that g(mu) is equal to the linear predictor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Usually the (predicted) mean. |
required |
Source code in src/glum/_link.py
34 35 36 37 38 39 40 41 42 43 44 45 46 | |
derivative
abstractmethod
derivative(mu)
Compute the derivative of the link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Usually the (predicted) mean. |
required |
Source code in src/glum/_link.py
48 49 50 51 52 53 54 55 56 57 | |
inverse
abstractmethod
inverse(lin_pred)
Compute the inverse link function.
The inverse link function h gives the inverse relationship between
the linear predictor, X * w, and the mean, mu ≡ E(Y), so that
h(X * w) = mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
inverse_derivative
abstractmethod
inverse_derivative(lin_pred)
Compute the derivative of the inverse link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
74 75 76 77 78 79 80 81 82 83 | |
inverse_derivative2
abstractmethod
inverse_derivative2(lin_pred)
Compute second derivative of the inverse link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
85 86 87 88 89 90 91 92 93 94 | |
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a link function if it exists.
Source code in src/glum/_link.py
96 97 98 99 100 101 102 | |
LogitLink
Bases: Link
The logit link function logit(x).
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a link function if it exists.
Source code in src/glum/_link.py
96 97 98 99 100 101 102 | |
link
link(mu)
Compute the link function.
The link function g links the mean, mu ≡ E(Y), to the linear
predictor, X * w, so that g(mu) is equal to the linear predictor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Usually the (predicted) mean. |
required |
Source code in src/glum/_link.py
252 253 | |
derivative
derivative(mu)
Compute the derivative of the link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Usually the (predicted) mean. |
required |
Source code in src/glum/_link.py
255 256 | |
inverse
inverse(lin_pred)
Compute the inverse link function.
The inverse link function h gives the inverse relationship between
the linear predictor, X * w, and the mean, mu ≡ E(Y), so that
h(X * w) = mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
258 259 260 261 | |
is_saturated
is_saturated(lin_pred) -> np.ndarray
Whether inverse(lin_pred) is numerically at the boundary of its range.
Only links that clip their inverse at the boundary of the unit interval, such as the logit and cloglog links, can be saturated.
Source code in src/glum/_link.py
263 264 | |
inverse_derivative
inverse_derivative(lin_pred)
Compute the derivative of the inverse link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
266 267 268 | |
inverse_derivative2
inverse_derivative2(lin_pred)
Compute second derivative of the inverse link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
270 271 272 | |
LogLink
Bases: Link
The log link function log(x).
is_saturated
is_saturated(lin_pred) -> np.ndarray
Whether inverse(lin_pred) is numerically at the boundary of its range.
Only links that clip their inverse at the boundary of the unit interval, such as the logit and cloglog links, can be saturated.
Source code in src/glum/_link.py
26 27 28 29 30 31 32 | |
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a link function if it exists.
Source code in src/glum/_link.py
96 97 98 99 100 101 102 | |
link
link(mu)
Compute the link function.
The link function g links the mean, mu ≡ E(Y), to the linear
predictor, X * w, so that g(mu) is equal to the linear predictor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Usually the (predicted) mean. |
required |
Source code in src/glum/_link.py
230 231 | |
derivative
derivative(mu)
Compute the derivative of the link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Usually the (predicted) mean. |
required |
Source code in src/glum/_link.py
233 234 | |
inverse
inverse(lin_pred)
Compute the inverse link function.
The inverse link function h gives the inverse relationship between
the linear predictor, X * w, and the mean, mu ≡ E(Y), so that
h(X * w) = mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
236 237 | |
inverse_derivative
inverse_derivative(lin_pred)
Compute the derivative of the inverse link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
239 240 | |
inverse_derivative2
inverse_derivative2(lin_pred)
Compute second derivative of the inverse link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
242 243 | |
TweedieLink
TweedieLink(power)
Bases: Link
The Tweedie link function x^(1-p) if p≠1 and log(x) if p=1.
See the documentation of the superclass, Link, for details.
Source code in src/glum/_link.py
129 130 | |
is_saturated
is_saturated(lin_pred) -> np.ndarray
Whether inverse(lin_pred) is numerically at the boundary of its range.
Only links that clip their inverse at the boundary of the unit interval, such as the logit and cloglog links, can be saturated.
Source code in src/glum/_link.py
26 27 28 29 30 31 32 | |
to_tweedie
to_tweedie(safe=True)
Return the Tweedie representation of a link function if it exists.
Source code in src/glum/_link.py
96 97 98 99 100 101 102 | |
link
link(mu)
Compute the link function.
The link function g links the mean, mu ≡ E(Y), to the linear
predictor, X * w, so that g(mu) is equal to the linear predictor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Usually the (predicted) mean. |
required |
Source code in src/glum/_link.py
152 153 154 155 156 157 | |
derivative
derivative(mu)
Compute the derivative of the link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
(array - like, shape(n_samples))
|
Usually the (predicted) mean. |
required |
Source code in src/glum/_link.py
159 160 161 162 163 164 | |
inverse
inverse(lin_pred)
Compute the inverse link function.
The inverse link function h gives the inverse relationship between
the linear predictor, X * w, and the mean, mu ≡ E(Y), so that
h(X * w) = mu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
166 167 168 169 170 171 172 | |
inverse_derivative
inverse_derivative(lin_pred)
Compute the derivative of the inverse link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
174 175 176 177 178 179 180 181 | |
inverse_derivative2
inverse_derivative2(lin_pred)
Compute second derivative of the inverse link function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lin_pred
|
(array - like, shape(n_samples))
|
Usually the (fitted) linear predictor. |
required |
Source code in src/glum/_link.py
183 184 185 186 187 188 189 190 191 192 193 | |
get_link
get_link(link: Union[str, Link], family: ExponentialDispersionModel) -> Link
For the Tweedie distribution, this code follows actuarial best practices regarding link functions. Note that these links are sometimes not canonical:
- identity for normal (
p = 0); - no convention for
p < 0, so let's leave it as identity; - log otherwise.
Source code in src/glum/_glm.py
3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 | |