spateo.tools.CCI_effects_modeling.distributions

Defining the types of distributions that the dependent variable can be assumed to take.

Attributes

Classes

Link

Parent class for transformations of the dependent variable. The link function is used to transform the mean of

Logit

The logit link function to transform the probability of a binary response variable to the scale of a linear

Power

Transform by raising to a power to transform the mean parameter to the scale of the linear predictor.

identity

Identity transform.

inverse_power

Inverse power transform.

sqrt

Square root transform.

Log

Transform by taking the logarithm.

VarianceFunction

Relates the variance of a random variable to its mean.

Power_Variance

Variance function that is a power of the mean.

Binomial_Variance

Variance function for binomial distribution.

Negative_Binomial_Variance

Variance function for the negative binomial distribution.

Distribution

Parent class for one-parameter exponential distributions that can be used with Spateo's modeling core. Some of

Poisson

Poisson distribution for modeling count data.

Gaussian

Gaussian distribution for modeling continuous data.

Gamma

Gamma distribution for modeling continuous data.

Binomial

Binomial distribution for modeling binary data.

NegativeBinomial

Negative binomial distribution for modeling count data.

Module Contents

Bases: object

Parent class for transformations of the dependent variable. The link function is used to transform the mean of the response variable, which may have a different distributional form than the linear predictor, to the scale of the linear predictor.

For example, the probability returned by a logistic regressor and the mean parameters are related through this link function.

Does nothing, but includes the expected methods for transformations.

inverse(z: numpy.ndarray) numpy.ndarray[source]

Inverse of the transformation.

Parameters:
z

The prediction of the transformed dependent variable from the IRLS algorithm as applied with a generalized linear model

Returns:

The value of the inverse transform function (converting from values to probabilities)

Return type:

g^(-1)(z)

deriv(fitted: numpy.ndarray) numpy.ndarray[source]

Derivative of the logit transformation evaluated at the fitted mean response variable and with respect to the linear predictor.

Parameters:
fitted

The fitted mean response variable

Returns:

The value of the derivative of the logit function evaluated at the fitted mean response variable

Return type:

deriv

second_deriv(p: numpy.ndarray) numpy.ndarray[source]

Second derivative of the transformation.

Parameters:
p

Logits to model the probability of an event, given predictor variables with specified values

Returns:

The value of the second derivative of the link function

Return type:

g’’(p)

inverse_deriv(z: numpy.ndarray)[source]

Derivative of the inverse transformation g^(-1)(z).

Parameters:
z

The prediction of the transformed dependent variable from the IRLS algorithm as applied with a generalized linear model

Returns:

The value of the derivative of the fitted mean response variable (link function

Return type:

g^(-1)’(p)

class spateo.tools.CCI_effects_modeling.distributions.Logit[source]

Bases: Link

The logit link function to transform the probability of a binary response variable to the scale of a linear predictor.

clip(vals: numpy.ndarray) numpy.ndarray[source]

Clips values to avoid numerical issues.

Parameters:
vals

Values to clip

Returns:

The clipped values

Return type:

vals

__call__(p: numpy.ndarray)[source]

Transforms the probabilities to logits.

Parameters:
p

Probabilities of an event, given predictor variables with specified values

Returns:

The transformed logits

Return type:

z

inverse(z: numpy.ndarray) numpy.ndarray[source]

Inverse of the transformation, transforms the linear predictor to the scale of the response variable.

Parameters:
z

The prediction of the logit-transformed dependent variable from the IRLS algorithm as applied with a generalized linear model

Returns:

Transformed linear predictor

Return type:

inv

inverse_deriv(z: numpy.ndarray)[source]

Derivative of the inverse transformation g^(-1)(z).

Parameters:
z

The prediction of the transformed dependent variable from the IRLS algorithm as applied with a generalized linear model

Returns:

The value of the derivative of the inverse of the logit function

Return type:

inv_deriv

deriv(fitted: numpy.ndarray) numpy.ndarray[source]

Derivative of the logit transformation evaluated at the fitted mean response variable and with respect to the linear predictor.

Parameters:
fitted

The fitted mean response variable

Returns:

The value of the derivative of the logit function evaluated at the fitted mean response variable

Return type:

deriv

second_deriv(p: numpy.ndarray) numpy.ndarray[source]

Second derivative of the logit transformation.

Parameters:
p

Logits to model the probability of an event, given predictor variables with specified values

Returns:

The value of the second derivative of the logit function at “p”

Return type:

second_deriv

class spateo.tools.CCI_effects_modeling.distributions.Power(power: float)[source]

Bases: Link

Transform by raising to a power to transform the mean parameter to the scale of the linear predictor.

Aliases of Power:

identity = Power(power=1) squared = Power(power=2) sqrt = Power(power=0.5) inverse = Power(power=-1) inverse_squared = Power(power=-2)

Parameters:
power

The exponent of the power transform

power[source]
__call__(fitted: numpy.ndarray)[source]

Raises parameters to power.

Parameters:
fitted

Mean parameter values

Returns:

The transformed logits

Return type:

z

inverse(z: numpy.ndarray) numpy.ndarray[source]

Inverse of the transformation, transforms the linear predictor to the scale of the response variable.

Parameters:
z

The prediction of the power-transformed dependent variable from the IRLS algorithm as applied with a generalized linear model

Returns:

Transformed linear predictor

Return type:

inv

inverse_deriv(z: numpy.ndarray)[source]

Derivative of the inverse transformation g^(-1)(z).

Parameters:
z

The prediction of the transformed dependent variable from the IRLS algorithm as applied with a generalized linear model

Returns:

The value of the derivative of the inverse of the power transform

Return type:

inv_deriv

deriv(fitted: numpy.ndarray) numpy.ndarray[source]

Derivative of the logit transformation evaluated at the fitted mean response variable and with respect to the linear predictor.

Parameters:
fitted

The fitted mean response variable

Returns:

The value of the derivative of the logit function evaluated at the fitted mean response variable

Return type:

deriv

second_deriv(p: numpy.ndarray) numpy.ndarray[source]

Second derivative of the power transformation.

Parameters:
p

(non-transformed) logits to model the probability of an event, given predictor variables with specified values

Returns:

The value of the second derivative of the logit function at “p”

Return type:

second_deriv

class spateo.tools.CCI_effects_modeling.distributions.identity[source]

Bases: Power

Identity transform.

g(p) = p

Alias of Power(power=1.)

class spateo.tools.CCI_effects_modeling.distributions.inverse_power[source]

Bases: Power

Inverse power transform.

g(p) = 1 / p

Alias of Power(power=-1.)

class spateo.tools.CCI_effects_modeling.distributions.sqrt[source]

Bases: Power

Square root transform.

g(p) = sqrt(p)

Alias of Power(power=0.5)

class spateo.tools.CCI_effects_modeling.distributions.Log[source]

Bases: Link

Transform by taking the logarithm.

g(p) = log(p)

clip(vals: numpy.ndarray) numpy.ndarray[source]

Clips values to avoid numerical issues.

Parameters:
vals

Values to clip

Returns:

The clipped values

Return type:

vals

__call__(p: numpy.ndarray)[source]

Transforms the probabilities to logits.

Parameters:
p

Probabilities of an event, given predictor variables with specified values

Returns:

The transformed logits

Return type:

z

inverse(z: numpy.ndarray) numpy.ndarray[source]

Inverse of the transformation, transforms the linear predictor to the scale of the response variable.

Parameters:
z

The prediction of the log-transformed dependent variable from the IRLS algorithm as applied with a generalized linear model

Returns:

Transformed linear predictor

Return type:

inv

inverse_deriv(z: numpy.ndarray)[source]

Derivative of the inverse transformation g^(-1)(z).

Parameters:
z

The prediction of the transformed dependent variable from the IRLS algorithm as applied with a generalized linear model

Returns:

The value of the derivative of the inverse of the power transform

Return type:

inv_deriv

deriv(fitted: numpy.ndarray) numpy.ndarray[source]

Derivative of the log transformation evaluated at the fitted mean response variable and with respect to the linear predictor.

Parameters:
fitted

The fitted mean response variable

Returns:

The value of the derivative of the logit function evaluated at the fitted mean response variable

Return type:

deriv

second_deriv(y: numpy.ndarray) numpy.ndarray[source]

Second derivative of the logit transformation evaluated at the mean response variable and with respect to the linear predictor.

Parameters:
y

The mean response variable

Returns:

The value of the second derivative of the logit function at “p”

Return type:

second_deriv

class spateo.tools.CCI_effects_modeling.distributions.VarianceFunction[source]

Bases: object

Relates the variance of a random variable to its mean.

__call__(fitted)[source]

Default variance function, assumes variance of each parameter is 1.

Parameters:
fitted

Mean parameter values

Returns:

Variance

Return type:

var

deriv(fitted)[source]

Returns the derivative of the variance function.

Parameters:
fitted

Mean parameter values

Returns:

Derivative of the variance function

Return type:

deriv

spateo.tools.CCI_effects_modeling.distributions.constant_var[source]
class spateo.tools.CCI_effects_modeling.distributions.Power_Variance(power=1.0)[source]

Bases: object

Variance function that is a power of the mean.

Alias for Power_Variance:

fitted = Power_Variance() fitted_squared = Power_Variance(power=2) fitted_cubed = Power_Variance(power=3)

Parameters:
power

Exponent used in the variance function

power[source]
__call__(fitted)[source]

Computes variance by raising mean parameters to a power.

Parameters:
fitted

Mean parameter values

Returns:

Variance

Return type:

var

deriv(fitted)[source]

Returns the derivative of the variance function.

Parameters:
fitted

Mean parameter values

Returns:

Derivative of the variance function

Return type:

deriv

spateo.tools.CCI_effects_modeling.distributions.fitted[source]
spateo.tools.CCI_effects_modeling.distributions.fitted_squared[source]
spateo.tools.CCI_effects_modeling.distributions.fitted_cubed[source]
class spateo.tools.CCI_effects_modeling.distributions.Binomial_Variance(n=1)[source]

Bases: object

Variance function for binomial distribution.

Equations:

V(fitted) = p * (1 - p) * n, where p = mu / n

Parameters:
n

The number of trials. The default is 1, under which the assumption is that each observation is an independent trial with a binary outcome.

n[source]
clip(vals: numpy.ndarray) numpy.ndarray[source]

Clips values to avoid numerical issues.

Parameters:
vals

Values to clip

Returns:

The clipped values

Return type:

vals

__call__(fitted: numpy.ndarray)[source]

Computes variance for the mean parameters by modeling the output probabilities as a binomial distribution.

Parameters:
fitted

Mean parameter values

Returns:

Variance

Return type:

var

deriv(fitted: numpy.ndarray) numpy.ndarray[source]

Returns the derivative of the variance function.

Parameters:
fitted

Mean parameter values

Returns:

Derivative of the variance function

Return type:

deriv

spateo.tools.CCI_effects_modeling.distributions.binom_variance[source]
class spateo.tools.CCI_effects_modeling.distributions.Negative_Binomial_Variance(disp: float = 0.5)[source]

Bases: object

Variance function for the negative binomial distribution.

Equations:

V(fitted) = fitted + disp * fitted ** 2

Parameters:
disp

The dispersion parameter for the negative binomial. Assumed to be nonstochastic, defaults to 0.5.

disp[source]
clip(vals: numpy.ndarray) numpy.ndarray[source]

Clips values to avoid numerical issues.

Parameters:
vals

Values to clip

Returns:

The clipped values

Return type:

vals

__call__(fitted: numpy.ndarray)[source]

Computes variance for the mean parameters by modeling the output probabilities as a negative binomial distribution.

Parameters:
fitted

Mean parameter values

Returns:

Variance, given by fitted + disp * fitted ** 2

Return type:

var

deriv(fitted)[source]

Returns the derivative of the variance function.

Parameters:
fitted

Mean parameter values

Returns:

Derivative of the variance function

Return type:

deriv

spateo.tools.CCI_effects_modeling.distributions.nbinom_variance[source]
class spateo.tools.CCI_effects_modeling.distributions.Distribution(link, variance)[source]

Bases: object

Parent class for one-parameter exponential distributions that can be used with Spateo’s modeling core. Some of the methods do nothing, but provide skeletons for the expected methods for the distributions themselves .

Parameters:
link

The link function to use for the distribution, for performing transformation of the linear outputs. See the individual distributions for the default link.

variance

Measures the variance as a function of the mean probabilities. See the individual families that inherit from this class for the default variance function.

Sets the link function for the distribution.

If the chosen link function is not valid for a particular distribution family, a ValueError exception will be raised.

variance[source]
clip(vals: numpy.ndarray) numpy.ndarray[source]

Clips values to avoid numerical issues.

Parameters:
vals

Values to clip

Returns:

The clipped values

Return type:

vals

initial_predictions(y: numpy.ndarray) numpy.ndarray[source]

Starting value for linear predictions in the IRLS algorithm.

Parameters:
y

The untransformed dependent variable

Returns:

Array of shape [n_samples,]; the initial linear predictors.

Return type:

y_hat_0

weights(fitted: numpy.ndarray) numpy.ndarray[source]

Weights for the IRLS algorithm.

Parameters:
fitted

Array of shape [n_samples,]; transformed mean response variable

Returns:

Weights for the IRLS steps

Return type:

w

predict(fitted: numpy.ndarray) numpy.ndarray[source]

Given the linear predictors, map back to the scale of the dependent variable.

Parameters:
fitted

Linear predictors

Returns:

The predicted dependent variable values

Return type:

y_hat

get_predictors(outputs: numpy.ndarray) numpy.ndarray[source]

Given model fit (outputs obtained from applying the link function), map back to the scale of the linear predictors.

Parameters:
outputs

The predicted dependent variable values

Returns:

The linear predictors

Return type:

predictor

abstract deviance(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) float[source]

Deviance function to measure goodness-of-fit of model fitting. Defined as twice the log-likelihood ratio.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

freq_weights

Array of shape [n_samples, ]; 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response variable

Returns:

The value of the deviance function

Return type:

dev

abstract deviance_residuals(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) numpy.ndarray[source]

Deviance residuals for the model, representing the difference between the observed and expected values of the dependent variable.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

freq_weights

Array of shape [n_samples, ]; 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response variable- residuals will be divided by the scale

Returns:

The deviance residuals

Return type:

dev_res

abstract log_likelihood(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) numpy.ndarray[source]

Log-likelihood function for the model.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

freq_weights

Array of shape [n_samples, ]; 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response variable

Returns:

The value of the log-likelihood function

Return type:

ll

class spateo.tools.CCI_effects_modeling.distributions.Poisson(link=Log)[source]

Bases: Distribution

Poisson distribution for modeling count data.

Parameters:
link

The link function to use for the distribution, for performing transformation of the linear outputs. The default link is the log link, but available links are “log”, “identity”, and “sqrt”.

variance[source]
valid[source]
logger[source]
clip(vals: numpy.ndarray) numpy.ndarray[source]

Clips values to avoid numerical issues.

Parameters:
vals

Values to clip

Returns:

The clipped values

Return type:

vals

deviance(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) float[source]

Poisson deviance function.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

freq_weights

Array of shape [n_samples, ]; 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response variable

Returns:

Array of shape [n_samples, ]; the value of the deviance function evaluated for each sample.

Return type:

dev

deviance_residuals(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) numpy.ndarray[source]

Poisson deviance residuals.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

scale

Optional scale of the response variable- residuals will be divided by the scale

Returns:

The deviance residuals

Return type:

dev_resid

log_likelihood(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) numpy.ndarray[source]

Poisson log likelihood of the fitted mean response.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

freq_weights

Array of shape [n_samples, ]; 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response variable

Returns:

The value of the log-likelihood function

Return type:

ll

class spateo.tools.CCI_effects_modeling.distributions.Gaussian(link=identity)[source]

Bases: Distribution

Gaussian distribution for modeling continuous data.

Parameters:
link

The link function to use for the distribution, for performing transformation of the linear outputs. The default link is the identity link, but available links are “log”, “identity”, and “inverse”.

variance[source]
logger[source]
deviance(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) float[source]

Gaussian deviance function.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

freq_weights

Array of shape [n_samples, ]; 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response variable

Returns:

The value of the deviance function

Return type:

dev

deviance_residuals(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) numpy.ndarray[source]

Gaussian deviance residuals.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

freq_weights

Array of shape [n_samples, ]; 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response- residuals will be divided by the scale

Returns:

The deviance residuals

Return type:

dev_resid

log_likelihood(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0)[source]

Gaussian log likelihood of the fitted mean response.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

freq_weights

Array of shape [n_samples, ]; 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response variable

Returns:

The value of the log-likelihood function

Return type:

ll

class spateo.tools.CCI_effects_modeling.distributions.Gamma(link=Log)[source]

Bases: Distribution

Gamma distribution for modeling continuous data.

Parameters:
link

The link function to use for the distribution, for performing transformation of the linear outputs. The default link is the inverse link, but available links are “log”, “identity”, and “inverse”.

variance[source]
logger[source]
clip(vals: numpy.ndarray) numpy.ndarray[source]

Clips values to avoid numerical issues.

Parameters:
vals

Values to clip

Returns:

The clipped values

Return type:

vals

deviance(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) float[source]

Gamma deviance function.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

freq_weights

Array of shape [n_samples, ]; 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response variable

Returns:

The value of the deviance function

Return type:

dev

deviance_residuals(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) numpy.ndarray[source]

Gamma deviance residuals.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

freq_weights

Array of shape [n_samples, ]; 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response variable- residuals will be divided by the scale

Returns:

The deviance residuals

Return type:

dev_resid

log_likelihood(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) numpy.ndarray[source]

Gamma log likelihood of the fitted mean response.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

freq_weights

Array of shape [n_samples, ]; 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response variable

Returns:

The value of the log-likelihood function

Return type:

ll

class spateo.tools.CCI_effects_modeling.distributions.Binomial(link=Logit)[source]

Bases: Distribution

Binomial distribution for modeling binary data.

Parameters:
link

The link function to use for the distribution, for performing transformation of the linear outputs. The default link is the logit link, but available links are “logit” and “log”.

variance[source]
logger[source]
n = 1[source]
initial_predictions(y: numpy.ndarray) numpy.ndarray[source]

Initial predictions for the IRLS algorithm.

Parameters:
y

Array of shape [n_samples, ]; untransformed dependent variable

Returns:

Array of shape [n_samples,]; the initial linear predictors.

Return type:

y_hat_0

deviance(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0, axis: int | None = None) float[source]

Binomial deviance function.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

freq_weights

Array of shape [n_samples, ]; 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response variable

axis

Axis along which the deviance is calculated

Returns:

The value of the deviance function

Return type:

dev

deviance_residuals(endog: numpy.ndarray, fitted: numpy.ndarray, scale: float = 1.0) numpy.ndarray[source]

Binomial deviance residuals.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

scale

Optional scale of the response variable- residuals will be divided by the scale

Returns:

The deviance residuals

Return type:

dev_resid

log_likelihood(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) numpy.ndarray[source]

Binomial log likelihood of the fitted mean response.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated

values) at the linear predicted

freq_weights

Array of shape [n_samples, ]; optional 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response variable

Returns:

The value of the log-likelihood function

Return type:

ll

class spateo.tools.CCI_effects_modeling.distributions.NegativeBinomial(link=Log, disp: float | None = None)[source]

Bases: Distribution

Negative binomial distribution for modeling count data.

Parameters:
link

The link function to use for the distribution, for performing transformation of the linear outputs. The default link is the inverse link, but available links are “log”, “identity”, and “inverse”.

variance[source]
logger[source]
clip(vals: numpy.ndarray) numpy.ndarray[source]

Clips values to avoid numerical issues.

Parameters:
vals

Values to clip

Returns:

The clipped values

Return type:

vals

deviance(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) float[source]

Negative binomial deviance function.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated at the linear predicted values)

freq_weights

Array of shape [n_samples, ]; 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response variable

Returns:

The value of the deviance function

Return type:

dev

deviance_residuals(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) numpy.ndarray[source]

Negative binomial deviance residuals.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated

values) at the linear predicted

scale

Optional scale of the response variable- residuals will be divided by the scale

Returns:

The deviance residuals

Return type:

dev_resid

log_likelihood(endog: numpy.ndarray, fitted: numpy.ndarray, freq_weights: numpy.ndarray | None = None, scale: float = 1.0) numpy.ndarray[source]

Negative binomial log likelihood of the fitted mean response.

Parameters:
endog

Array of shape [n_samples, ]; untransformed dependent variable

fitted

Array of shape [n_samples, ]; fitted mean response variable (link function evaluated

values) at the linear predicted

freq_weights

Array of shape [n_samples, ]; optional 1D array of frequency weights, used to e.g. adjust for unequal sampling frequencies

scale

Optional scale of the response variable

Returns:

The value of the log-likelihood function

Return type:

ll