You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FAQ: Wrong value of BIC, AIC for linear regressions and OLS #1802
where k is the number of parameters of the full statistical model. When assuming a model with Gaussian errors,
y = f(x) + e, e~N(0, s)
k is the number of parameters of f plus 1 for the (unobserved) variance s of the Gaussian errors. BIC methods in statsmodels always ignore this +1, which results in BICs that are systematically shifted by -log(n). Although this does not have any effect on model selection, it is confusing and, strictly speaking, incorrect. IMHO it should be fixed.
Update
statsmodels follows the least squares and quasi-likelihood tradition and only takes mean parameters into account in the number of parameters for information criteria like AIC and BIC. Scale parameters and other auxiliary parameters that are not directly estimated as part of the main optimization are not included. This default does not change.
Starting with statsmodels version 0.13 (PR #7693), a new results method info_criteria is included for GLM and for linear regression that allows users to adjust the number of parameters in the information criteria calculation, using dk_params (for "delta k_params").
Full Maximum Likelihood method such as those used in discrete models estimate scale or dispersion parameters jointly with mean parameters and both types of parameters are counted in the information criteria computation.
(Generalize Poisson and NegativeBinomialP have an extra power parameter that is not estimated and is not taken into account for information criteria.)
@seeslab Do you have a specific application, reference or know of other packages that makes this confusing.
I would prefer a consistent definition across models, and wouldn't mind changing this for the linear and other models.
As far as I know. the main argument against counting the variance for linear models is tradition. No other package (R, Stata, SAS, SPSS) is counting it either, as far as I remember.
in issue #1624 I tried to get a bit of an overview, e.g. #1624 (comment)
Starting at the following comment is the difference in treatment of variance for aic between linear models and ARMA #1262 (comment)
@josef-pkt Thanks for the quick reply. I was comparing the results to R and not getting the same--that's how I noticed. So, unless I am doing something wrong, R does include the variance as a parameter in any least-squares model fit (linear or otherwise). Other than that, the variance is a parameter in the model, right? :)
I can confirm that R does count the scale parameter when computing BIC for linear models, both when fit by lm and when fit by glm(... family="gaussian").
I tried to find the code where R computes the BIC, but was not successful.
Good, I didn't know this about R. That makes it easier to justify switching.
Our unit tests for the linear models are a mix of Stata and R results.
I tried out a few examples in R for lm and nmle/mle
calling logLik(model) shows a different degrees of freedom from df for the inference/summary and includes the variance, or covariance parameters in mle, which is used in AIC, BIC.
(There is also an extractAIC function that uses model specific definition and drops some constants.)
To change this we need a way to keep track of variance/covariance parameters even when they are not counted in df_resid and df_model.
Other than that, the variance is a parameter in the model, right? :)
Yes and no, The parameter estimates in OLS are just least squares and are independent of the variance/scale estimate.
However, they are part of the loglike, the full parametric likelihood model
(AIC, BIC also have a least squares version that only depends on the residual sum of squares instead of the loglike.)
I am not sure if this discussion is ended already but in general AIC/BIC are almost never comparable between software packages, but this is really not an issue since they are normally used to find the optimal model given a set of choices in models/parameters - the actual numbers are irrelevant as long as the concept behind the AIC/BIC implementation is sound (i.e panalizing unsupported model parameters).
For selecting a model with information criterion it should be irrelevant if you get (given 4 model choices) results of (1,2,-2,4) or (16543, 17002, 14283, 18234) as long as you are aware to pick the lowest of these values as your best parametric fit - in this case model number 3.
Again my apologies if i am beating a dead horse here - i just stumbled across this one and thought i'd add something
I don't care very much about comparison with other packages in this, since those also don't agree.
(Although we do have unit tests for most cases to compare and verify the loglikelihood value, llf.)
But it will be important to have a consistent definition so we can compare the information criteria across models within statsmodels.
From a maximum likelihood perspective it would be an easy decision, count every parameter that is in the likelihood function and define df_model and similar attributes correspondingly.
However, OLS is not really a MLE, GLSAR and similar are also not estimated by MLE.
There are several other open issues, that are related and try to figure out consistent definitions for these attributes across models.
Essentially, I'm not sure yet how to treat the distinction between MLE and Quasi-MLE or M-estimators within the same model.
(google just found this, I never looked at it:
Andrews, Donald WK. "Consistent moment selection procedures for generalized method of moments estimation." Econometrica 67.3 (1999): 543-563.
)
My current thinking is to treat llf, aic, bic and other ic as pure MLE concepts and warn or raise when they are used with a non-robust cov_type.
Which doesn't answer the question what to do with models that are not MLE, especially generalized least squares GLSxxx, nor whether to adjust df_model also.
You raise some very good questions there. I always considers OLS (and GLS to a lower degree) to be sufficiently MLE like to just use information criteria and call it a day (there are some rather soft technical conditions for OLS to be considered a MLE - if i remember my econometrics classes correctly something along the lines of "error distribution is in the exponential family" or somesuch).
Personally i have not looked into the statsmodel code deeply (altho that is a bit shameful since i use it frequently) and did only stumble by this issue - is it very problematic to use a standard implementation for OLS/GLS for AIC/BIC and just call it a day? It should not really cause problems as long as it fits the MLE estimated enough to be comparable. What i mean by this: What is the core issue to implement it for those models separately? Seem to make sense if you can't treat them like MLE.
Oh and regarding literature: Wooldridge, Introductory Econometrics has something to say on this topic too - but i would need to dig the section in question out :)
A historical note. For the most part, we follow the Stata conventions for
information criteria rather than R. There are some notes in the tests and
in the results files about the definitions of the information criteria,
which package we agree with (i.e., usually stata with the R value commented
out), and why. I don't have a strong opinion on this though. I'm not
entirely convinced that the 'right' answer isn't just a matter of
perspective though. Perhaps it would be better to allows for degrees of
freedom choice in the model instantiation?
I agree that having keyword arguments for df would be a good way to deal
with this (like the numpy/scipy df options for variance and covariance
estimation). I don't have an opinion about what the default should be.
On Sat, Nov 8, 2014 at 11:35 AM, Skipper Seabold [email protected]
wrote:
A historical note. For the most part, we follow the Stata conventions for
information criteria rather than R. There are some notes in the tests and
in the results files about the definitions of the information criteria,
which package we agree with (i.e., usually stata with the R value commented
out), and why. I don't have a strong opinion on this though. I'm not
entirely convinced that the 'right' answer isn't just a matter of
perspective though. Perhaps it would be better to allows for degrees of
freedom choice in the model instantiation?
—
Reply to this email directly or view it on GitHub #1802 (comment)
.
about the degrees of freedom: large part of this is added now through the cov_typecov_kwds arguments in fit. For cluster robust standard errors we have now a separate df_resid_inference (IIR the name correctly)
This affects currently only the Wald based inference. Robust score tests are in preparation.
If there is demand, I think this can or will be extended to cover more.
Robust likelihood ratio inference doesn't exist except in a few cases, which leaves the information criteria belonging to the full MLE interpretation, (unless we find robust information criteria).
Putting df adjustments into the model.__init__ is more appropriate if the user has already pre-estimated something, like removing fixed effects in constant be demeaning (longitudinal/panel data).
Essentially, with the choice of cov_type we have now QMLE and MLE in the same models in all the GLM/linear exponential family models, and we have models that are mainly or exclusively QMLE, and will have models that only make sense as MLE (those that are not in GLM/LEF).
changed the title [-]Wrong value of BIC, AIC for linear regressions and OLS[/-][+]FAQ: Wrong value of BIC, AIC for linear regressions and OLS[/+]on Dec 1, 2022
It's a FAQ because the way it is implemented is wrong. s is a parameter of the probabilistic model being assumed, so it should be counted. This is very obvious if one bothers to consider where the expression of the BIC comes from, namely, marginalizing over parameter values to get the posterior over models (not sure whether this is so obvious from the derivation of the AIC). The fact that some statistics traditions have historically not counted s as a parameter does not make it better, IMHO.
s is a parameter of the probabilistic model being assumed, so it should be counted.
It is somewhat naive (or dogmatic) to assume there is anything resembling a complete probabilistic model when when estimating parameters using OLS. There are many justifications for using OLS that do not require a complete probabilistic model specification, e.g,. moment-restrictions, semi-parametric efficient estimator, or QMLE.
OLS is not a fully probabilistic model. It does NOT require any distributional assumption.
The parameter estimates are independent of variance estimate.
statsmodels is treating GLM and OLS/WLS/... as moment estimators and not as full MLE, even if we use llf in info criteria.
What you are both saying is true in general, but not in the context of calculating the BIC or other model selection criteria. In this context, there is a probabilistic model underlying OLS (whether you acknowledge it explicitly or not), the plausibility of which you want to quantify---and s is a parameter of that model.
We will hopefully get in 0.15, a gaussian MLE model that estimates mean and variance parameters at the same time.
That will count all parameters for the information criteria.
aside:
for ols, the number of effective parameters is often defined as trace of hatmatrix/projection matrix which is equal to the number of explanatory variables (under full rank design matrix.)
You penalize this parameter for the same reason that you penalize any other parameter: because there is uncertainty about their true value, and when you estimate the goodness of a model you must take into account all possible values of all the uncertain parameters, not only their most plausible values. More formally, the penalty term in the BIC comes from a first order approximation to the integral of the likelihood over all possible parameter values. Thus, the "penalty" is not a heuristic term (as typically, but wrongly, seen in some statistics "traditions"), and one is not free to count parameters as they wish---you formulate the (probabilistic) model, integrate over all parameters, and (to first order) you get the BIC.
Activity
josef-pkt commentedon Jul 4, 2014
@seeslab Do you have a specific application, reference or know of other packages that makes this confusing.
I would prefer a consistent definition across models, and wouldn't mind changing this for the linear and other models.
As far as I know. the main argument against counting the variance for linear models is tradition. No other package (R, Stata, SAS, SPSS) is counting it either, as far as I remember.
in issue #1624 I tried to get a bit of an overview, e.g.
#1624 (comment)
Starting at the following comment is the difference in treatment of variance for aic between linear models and ARMA #1262 (comment)
see also #1624 (comment) for GLM
seeslab commentedon Jul 4, 2014
@josef-pkt Thanks for the quick reply. I was comparing the results to R and not getting the same--that's how I noticed. So, unless I am doing something wrong, R does include the variance as a parameter in any least-squares model fit (linear or otherwise). Other than that, the variance is a parameter in the model, right? :)
Thanks again.
kshedden commentedon Jul 5, 2014
I can confirm that R does count the scale parameter when computing BIC for linear models, both when fit by lm and when fit by glm(... family="gaussian").
I tried to find the code where R computes the BIC, but was not successful.
josef-pkt commentedon Jul 5, 2014
Good, I didn't know this about R. That makes it easier to justify switching.
Our unit tests for the linear models are a mix of Stata and R results.
I tried out a few examples in R for lm and nmle/mle
calling
logLik(model)shows a different degrees of freedom from df for the inference/summary and includes the variance, or covariance parameters in mle, which is used in AIC, BIC.(There is also an
extractAICfunction that uses model specific definition and drops some constants.)To change this we need a way to keep track of variance/covariance parameters even when they are not counted in df_resid and df_model.
Yes and no, The parameter estimates in OLS are just least squares and are independent of the variance/scale estimate.
However, they are part of the loglike, the full parametric likelihood model
(AIC, BIC also have a least squares version that only depends on the residual sum of squares instead of the loglike.)
andreas-esders-kopecky commentedon Nov 8, 2014
I am not sure if this discussion is ended already but in general AIC/BIC are almost never comparable between software packages, but this is really not an issue since they are normally used to find the optimal model given a set of choices in models/parameters - the actual numbers are irrelevant as long as the concept behind the AIC/BIC implementation is sound (i.e panalizing unsupported model parameters).
For selecting a model with information criterion it should be irrelevant if you get (given 4 model choices) results of (1,2,-2,4) or (16543, 17002, 14283, 18234) as long as you are aware to pick the lowest of these values as your best parametric fit - in this case model number 3.
Again my apologies if i am beating a dead horse here - i just stumbled across this one and thought i'd add something
josef-pkt commentedon Nov 8, 2014
The horse is not completely dead yet.
I don't care very much about comparison with other packages in this, since those also don't agree.
(Although we do have unit tests for most cases to compare and verify the loglikelihood value,
llf.)But it will be important to have a consistent definition so we can compare the information criteria across models within statsmodels.
From a maximum likelihood perspective it would be an easy decision, count every parameter that is in the likelihood function and define df_model and similar attributes correspondingly.
However, OLS is not really a MLE, GLSAR and similar are also not estimated by MLE.
There are several other open issues, that are related and try to figure out consistent definitions for these attributes across models.
Essentially, I'm not sure yet how to treat the distinction between MLE and Quasi-MLE or M-estimators within the same model.
(google just found this, I never looked at it:
Andrews, Donald WK. "Consistent moment selection procedures for generalized method of moments estimation." Econometrica 67.3 (1999): 543-563.
)
My current thinking is to treat
llf,aic,bicand othericas pure MLE concepts and warn or raise when they are used with a non-robustcov_type.Which doesn't answer the question what to do with models that are not MLE, especially generalized least squares GLSxxx, nor whether to adjust
df_modelalso.andreas-esders-kopecky commentedon Nov 8, 2014
You raise some very good questions there. I always considers OLS (and GLS to a lower degree) to be sufficiently MLE like to just use information criteria and call it a day (there are some rather soft technical conditions for OLS to be considered a MLE - if i remember my econometrics classes correctly something along the lines of "error distribution is in the exponential family" or somesuch).
Personally i have not looked into the statsmodel code deeply (altho that is a bit shameful since i use it frequently) and did only stumble by this issue - is it very problematic to use a standard implementation for OLS/GLS for AIC/BIC and just call it a day? It should not really cause problems as long as it fits the MLE estimated enough to be comparable. What i mean by this: What is the core issue to implement it for those models separately? Seem to make sense if you can't treat them like MLE.
Oh and regarding literature: Wooldridge, Introductory Econometrics has something to say on this topic too - but i would need to dig the section in question out :)
jseabold commentedon Nov 8, 2014
A historical note. For the most part, we follow the Stata conventions for
information criteria rather than R. There are some notes in the tests and
in the results files about the definitions of the information criteria,
which package we agree with (i.e., usually stata with the R value commented
out), and why. I don't have a strong opinion on this though. I'm not
entirely convinced that the 'right' answer isn't just a matter of
perspective though. Perhaps it would be better to allows for degrees of
freedom choice in the model instantiation?
kshedden commentedon Nov 8, 2014
I agree that having keyword arguments for df would be a good way to deal
with this (like the numpy/scipy df options for variance and covariance
estimation). I don't have an opinion about what the default should be.
On Sat, Nov 8, 2014 at 11:35 AM, Skipper Seabold [email protected]
wrote:
josef-pkt commentedon Nov 8, 2014
about the degrees of freedom: large part of this is added now through the
cov_typecov_kwdsarguments infit. For cluster robust standard errors we have now a separatedf_resid_inference(IIR the name correctly)This affects currently only the Wald based inference. Robust score tests are in preparation.
If there is demand, I think this can or will be extended to cover more.
Robust likelihood ratio inference doesn't exist except in a few cases, which leaves the information criteria belonging to the full MLE interpretation, (unless we find robust information criteria).
Putting df adjustments into the
model.__init__is more appropriate if the user has already pre-estimated something, like removing fixed effects in constant be demeaning (longitudinal/panel data).Essentially, with the choice of
cov_typewe have now QMLE and MLE in the same models in all the GLM/linear exponential family models, and we have models that are mainly or exclusively QMLE, and will have models that only make sense as MLE (those that are not in GLM/LEF).josef-pkt commentedon Jan 11, 2015
status quo wins for 0.7, revisit for 0.8
11 remaining items
[-]Wrong value of BIC for linear regressions and OLS[/-][+]Wrong value of BIC, AIC for linear regressions and OLS[/+]josef-pkt commentedon Dec 1, 2022
closing this
we don't change the default, but now have an option for the user in separate results method
josef-pkt commentedon Dec 1, 2022
reopening, it's a FAQ issue
[-]Wrong value of BIC, AIC for linear regressions and OLS[/-][+]FAQ: Wrong value of BIC, AIC for linear regressions and OLS[/+]seeslab commentedon Dec 1, 2022
It's a FAQ because the way it is implemented is wrong. s is a parameter of the probabilistic model being assumed, so it should be counted. This is very obvious if one bothers to consider where the expression of the BIC comes from, namely, marginalizing over parameter values to get the posterior over models (not sure whether this is so obvious from the derivation of the AIC). The fact that some statistics traditions have historically not counted s as a parameter does not make it better, IMHO.
bashtage commentedon Dec 1, 2022
It is somewhat naive (or dogmatic) to assume there is anything resembling a complete probabilistic model when when estimating parameters using OLS. There are many justifications for using OLS that do not require a complete probabilistic model specification, e.g,. moment-restrictions, semi-parametric efficient estimator, or QMLE.
josef-pkt commentedon Dec 1, 2022
OLS is not a fully probabilistic model. It does NOT require any distributional assumption.
The parameter estimates are independent of variance estimate.
statsmodels is treating GLM and OLS/WLS/... as moment estimators and not as full MLE, even if we use llf in info criteria.
Based on unit tests, our aic matches Stata
seeslab commentedon Dec 1, 2022
What you are both saying is true in general, but not in the context of calculating the BIC or other model selection criteria. In this context, there is a probabilistic model underlying OLS (whether you acknowledge it explicitly or not), the plausibility of which you want to quantify---and s is a parameter of that model.
josef-pkt commentedon Dec 1, 2022
But why would you want to penalize for a parameter that does not affect the estimation of params and resid?
josef-pkt commentedon Dec 1, 2022
We will hopefully get in 0.15, a gaussian MLE model that estimates mean and variance parameters at the same time.
That will count all parameters for the information criteria.
for now there is https://www.statsmodels.org/dev/generated/statsmodels.regression.linear_model.OLSResults.info_criteria.html
(seems to be not in regression base class)
aside:
for ols, the number of effective parameters is often defined as trace of hatmatrix/projection matrix which is equal to the number of explanatory variables (under full rank design matrix.)
seeslab commentedon Dec 1, 2022
You penalize this parameter for the same reason that you penalize any other parameter: because there is uncertainty about their true value, and when you estimate the goodness of a model you must take into account all possible values of all the uncertain parameters, not only their most plausible values. More formally, the penalty term in the BIC comes from a first order approximation to the integral of the likelihood over all possible parameter values. Thus, the "penalty" is not a heuristic term (as typically, but wrongly, seen in some statistics "traditions"), and one is not free to count parameters as they wish---you formulate the (probabilistic) model, integrate over all parameters, and (to first order) you get the BIC.
You may want to read the original BIC paper by Schwarz to understand where the terms come from, or this paper on model selection by us, which is less dense (although less detailed in this specific regard): https://www.science.org/doi/10.1126/sciadv.aav6971
PS: The value of s does not affect the residuals but it affects the likelihood of (or associated to) those residuals.