Source code for biogeme.likelihood.linear_regression
"""Calculates the contribution to the likelihood function of an observation in a linear regression context.Michel BierlaireSun Aug 17 2025, 18:18:23"""frombiogeme.distributionsimportlognormalpdf,normal_logpdf,normalpdffrombiogeme.expressionsimport(Beta,Expression,LinearTermTuple,LinearUtility,Variable,)
[docs]defbuild_linear_terms(independent_variables:list[Variable],coefficients:list[Beta])->LinearUtility:iflen(independent_variables)!=len(coefficients):raiseValueError(f'There are {len(independent_variables)} variables and {len(coefficients)} coefficients. Thi sis inconsistent.')returnLinearUtility([LinearTermTuple(beta=beta,x=x)forbeta,xinzip(coefficients,independent_variables)])
[docs]defbuild_normalized_formula(dependent_variable:Expression,linear_terms:Expression,scale_parameter:Expression,)->Expression:""" Constructs the standardized residual expression used in the likelihood and loglikelihood. :param dependent_variable: The dependent variable expression. :param linear_terms: Expression for the linear terms. :param scale_parameter: The scale parameter expression (e.g., standard deviation). :return: An Expression representing the standardized residual (dependent_variable minus linear predictor, divided by scale). """return(dependent_variable-linear_terms)/scale_parameter
[docs]defregression_likelihood(dependent_variable:Expression,linear_terms:Expression,scale_parameter:Expression,)->Expression:""" Calculates the contribution of one observation to the likelihood under a normal regression model. :param dependent_variable: The dependent variable expression. :param linear_terms: Expression for the linear terms. :param scale_parameter: The scale parameter expression (e.g., standard deviation). :return: An Expression representing the likelihood contribution of the observation. """argument=build_normalized_formula(dependent_variable=dependent_variable,linear_terms=linear_terms,scale_parameter=scale_parameter,)returnnormalpdf(argument)
[docs]defregression_loglikelihood(dependent_variable:Expression,linear_terms:Expression,scale_parameter:Expression,)->Expression:""" Calculates the log-likelihood contribution of one observation under a normal regression model. :param dependent_variable: The dependent variable expression. :param linear_terms: Expression for the linear terms. :param scale_parameter: The scale parameter expression (e.g., standard deviation). :return: An Expression representing the log-likelihood contribution of the observation. """argument=build_normalized_formula(dependent_variable=dependent_variable,linear_terms=linear_terms,scale_parameter=scale_parameter,)returnnormal_logpdf(argument)