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
Below is mostly for inequality restricted least squares, non-negative least squares is a special case might be a good starting point
3 stages to implement this
subclass base.Model and only calculate parameters, returning a special Results instance that only has params and some information about which constraints are binding.
This includes a wrapper about nnls (*1) or slsqp plus the interface adjustments to let user define constraints.
conditional results: Under the (incorrect) assumption that we know which constraints are binding, we can just return an OLSResults instance or an RLSResults instance depending on the constraints that are binding. (RLSResults doesn't exist yet, but should be mostly a subclass of current RegressionResults. For nnls, the restricted results would just be OLSResults with some explanatory variables dropped.) see references (*2)
Get "correct" Resutls: This is more involved since we need to figure out what this is supposed to do
see references (*3)
(*2) Liew, Chong Kiew. 1976. “Inequality Constrained Least-Squares Estimation.” Journal of the American Statistical Association 71 (355) (September 1): 746–751. doi:10.2307/2285614.
Geweke, John. 1986. “Exact Inference in the Inequality Constrained Normal Linear Regression Model.” Journal of Applied Econometrics 1 (2): 127–141. doi:10.1002/jae.3950010203.
I don't see any problem including just the basic version (the first stage above) in statsmodels.
Besides params, we can also add predict, resid and fittedvalues (and maybe some other things). This would already make it possible to use the model for prediction, and to add bootstrap for the inference.
In this case I would be happy enough to get started with prediction because inference is tough. (Although bootstrap might not turn out to be difficult.)
The other advantage of statsmodels compared to sklearn is the integration with pandas and formulas from patsy.
On the other hand, sklearn is much better if you just want cross-validated prediction and don't care about your "explanatory" variables.
@josef-pkt I recently added an ActiveSet solver class to my repo which may be of interest. I assume it is similar to the FORTRAN algo referenced in the scipy.optimize.nnls link above but I have not read the ref. Derived classes only need to define functions for objective and Hessian calcs. One of the provided examples is a constrained least squares solver similar to (and verified against) Matlab lsqlin command. One of the unit tests shows how to run a NNLS reg. Two.KKT solvers are provided: a fast direct matrix solver and a more computationally stable null space solver. A “commercial” version would certainly use the null space solver with O(n^2) QR updating but I have not implemented the update part. As a side note, I will be adding DFGLS unit root test in the next week or so and PP unit root test a few weeks later. I added the Leybourne-McCabe stationarity test a few months ago. Also added an ARFIMA sim with Lo’s modified R/S test for long memory to be added soon. https://github.com/JimVaranelli/ActiveSet
@JimVaranelli sounds good.
Are you planning to try to add this to scipy.optimize?
So far I have been using SLSQP for this case (quadratic objective, linear constraints), but quadratic programming equivalents in scipy are still missing, AFAICS.
AFAIR, the last time I used this I had a simplex constraint on the parameters x: 0 <= x <= 1, sum(x) = 1
@JimVaranelli
followup question because I was looking at linalg recently
The objective function uses the moment matrices in LS: (x'x b = x'y)
can we replace the moment matrices by the results from a QR
@josef-pkt I wasn’t planning on offering it to scipy but I would be more than happy to integrate it for them if there’s a need.
With respect to your question Re: QR, that is pretty much exactly what the null space method does. Of course it is 1-2 OoM slower than direct KKT matrix solve. You can use Givens or Householder updates to the QR matrices rather than full decomposition at each iteration but I have not implemented it. The code is currently set up for direct KKT solver with a backup to null space method in case of failure.
Would your QR nullspace method use the QR(X'X) in OLS and not AR of the original data QR(X)?
AFAIU, for general quadratic optimization problems like the portfolio choice, we only have the A matrix for the objective function. In OLS (similar to scipy.optimize nonlinear least squares) we have the matrix of explanatory variables X that includes all observations, so we can avoid directly computing A = X'X.
I was thinking of using QR(X) just to be able to use e.g. direct KKT in the optimization problem itself.
@JimVaranelli For something relatively generic like a non-neg lst square solver, it would probably be a better fit with SciPy. It would also reach a wider audience if that matters to you.
@josef-pkt The Hessian is only calculated once at the beginning of the run but is used in both the null space method (if number of active constraints is less than the number of variables) and direct KKT solve. Null space performs a QR on the (active) constraint matrix as opposed to the Hessian as in OLS. The KKT matrix is square symmetric so direct solution is quite fast using numpy.linalg.solve.
@bashtage I agree it is more of a scientific computing piece of code. The main contribution is the generic active set algo which AFAIK does not exist in python. The primary motivation for adding the repo is to support a research paper with the constrained least squares used for ensemble scorer weight optimization which was refactored from our C++ portfolio optimizer. I just abstracted out the active set method as a base class in the python version.
plain bootstrap doesn't help. It is not consistent on the boundary of the parameter space.
If true value is away from the boundary of the parameter space, then asymptotic normality holds and bootstrap is consistent, but not if the true value is at a boundary, e.g. b=0 under non-negativity constraint.
Bootstrap is not consistent if parameter goes to zero asymptotically. 1st page of Andrews.
Small sample properties for parameters close to the boundary will not be close to asymptotic normality.
Andrews, Donald WK. "Inconsistency of the bootstrap when a parameter is on the boundary of the parameter space." Econometrica (2000): 399-405.
Hurwitz has a section in his overview article with more recent references (e.g. for inequality moment conditions)
Horowitz, Joel L. "Bootstrap methods in econometrics." Annual Review of Economics 11 (2019): 193-224.
an idea that might not work. I didn't find references for it in google scholar
score confidence interval with boundary of parameter space or with inequality constraints.
(references found refer mostly to proportions)
assume parameter space is m >= 0
Find confidence interval upper bound m_upp that is consistent with the data at level alpha under assumption that m = m_upp.
This would avoid a null hypothesis that parameter is at boundary m=0.
That's what we use to construct score confidence intervals for proportions, where we have [0, 1] parameter space with boundaries.
related: confint if we observe all 0 (or 1) in binary proportion case. (wald ci collapses to boundary point or nan, score ci has positive length)
I don't really care about "literal" null hypotheses. All models are wrong and no point null hypothesis ever holds exactly.
Even if we don't have wald inference, we can still do useful things, like compute goodness-of-fit statistics and compare predictive accuracy or accuracy of predictive distributions. (e.g. what I did for predictive count probabilities, hosmer-lemeshow type, PIT).
When we have monotonicity in a test statistic, then we can just look at equivalence testing, e.g. anything smaller than delta is the essentially equivalent to boundary value of zero.
This way we can compare models without having to compute inference for individual statistics.
i.e. it's not relevant, even though it might be statistically significant (even when we don't know whether statistical significance holds or not)
One question are information criteria. Does ranking by them still choose the "right" model?
Activity
josef-pkt commentedon Dec 2, 2013
Problem is to get the standard errors for the parameter estimates and the statistical tests correct
https://groups.google.com/d/msg/pystatsmodels/JAyUngazjJ0/ThlBIwo72dgJ
Below is mostly for inequality restricted least squares, non-negative least squares is a special case might be a good starting point
3 stages to implement this
This includes a wrapper about nnls (*1) or slsqp plus the interface adjustments to let user define constraints.
see references (*3)
(*1) Vlas was benchmarking different ways to solve non-negative least squares scikit learn
http://sourceforge.net/mailarchive/forum.php?thread_name=CAFJw_eGFWt%3DLLeseS%2B5FqQa%2BqQn3t_jHbMWF%2BSOsOY9Tv7hb2Q%40mail.gmail.com&forum_name=scikit-learn-general
(*2) Liew, Chong Kiew. 1976. “Inequality Constrained Least-Squares Estimation.” Journal of the American Statistical Association 71 (355) (September 1): 746–751. doi:10.2307/2285614.
Geweke, John. 1986. “Exact Inference in the Inequality Constrained Normal Linear Regression Model.” Journal of Applied Econometrics 1 (2): 127–141. doi:10.1002/jae.3950010203.
(*3) R package http://cran.r-project.org/web/packages/ic.infer/vignettes/ic.infer.pdf
there should be some documentation for GAUSS
I don't have an overview, and I didn't read any of the journal articles or any book.
For example http://www.amazon.com/Constrained-Statistical-Inference-Constraints-Probability/dp/0471208272
josef-pkt commentedon Dec 2, 2013
I don't see any problem including just the basic version (the first stage above) in statsmodels.
Besides params, we can also add predict, resid and fittedvalues (and maybe some other things). This would already make it possible to use the model for prediction, and to add bootstrap for the inference.
cancan101 commentedon Dec 2, 2013
Of of curiosity, would something like NNLS belong in statmodels or in scikit-learn? There seem to be a number of other linear models in scikit: http://scikit-learn.org/stable/modules/linear_model.html ?
jseabold commentedon Dec 2, 2013
If you care about doing things like causal inference and hypothesis testing with the results, then statsmodels.
josef-pkt commentedon Dec 2, 2013
Here is a more expanded version on the difference
http://stats.stackexchange.com/questions/47913/pandas-statsmodel-scikits-learn/48578#48578
In this case I would be happy enough to get started with prediction because inference is tough. (Although bootstrap might not turn out to be difficult.)
The other advantage of statsmodels compared to sklearn is the integration with pandas and formulas from patsy.
On the other hand, sklearn is much better if you just want cross-validated prediction and don't care about your "explanatory" variables.
jseabold commentedon Dec 2, 2013
(Candidate for an FAQ....)
jseabold commentedon Apr 8, 2014
Added FAQ tag for statsmodels vs. scikit-learn.
diegojromerolopez commentedon Apr 14, 2017
Is there anybody working on integrating scipy.nnls in statsmodels?
josef-pkt commentedon Apr 14, 2017
@diegojromerolopez No, not that I know of.
JimVaranelli commentedon Jan 16, 2020
@josef-pkt I recently added an ActiveSet solver class to my repo which may be of interest. I assume it is similar to the FORTRAN algo referenced in the scipy.optimize.nnls link above but I have not read the ref. Derived classes only need to define functions for objective and Hessian calcs. One of the provided examples is a constrained least squares solver similar to (and verified against) Matlab lsqlin command. One of the unit tests shows how to run a NNLS reg. Two.KKT solvers are provided: a fast direct matrix solver and a more computationally stable null space solver. A “commercial” version would certainly use the null space solver with O(n^2) QR updating but I have not implemented the update part. As a side note, I will be adding DFGLS unit root test in the next week or so and PP unit root test a few weeks later. I added the Leybourne-McCabe stationarity test a few months ago. Also added an ARFIMA sim with Lo’s modified R/S test for long memory to be added soon. https://github.com/JimVaranelli/ActiveSet
josef-pkt commentedon Jan 16, 2020
@JimVaranelli sounds good.
Are you planning to try to add this to scipy.optimize?
So far I have been using SLSQP for this case (quadratic objective, linear constraints), but quadratic programming equivalents in scipy are still missing, AFAICS.
AFAIR, the last time I used this I had a simplex constraint on the parameters x: 0 <= x <= 1, sum(x) = 1
josef-pkt commentedon Jan 16, 2020
@JimVaranelli
followup question because I was looking at linalg recently
The objective function uses the moment matrices in LS: (
x'x b = x'y)can we replace the moment matrices by the results from a QR
current OLS/WLS code using QR has
can we use
Randeffectsin the quadratic optimization? e.g.A=Randb=effectsThat would be numerically more stable than using the moment matrices.
JimVaranelli commentedon Jan 17, 2020
@josef-pkt I wasn’t planning on offering it to scipy but I would be more than happy to integrate it for them if there’s a need.
With respect to your question Re: QR, that is pretty much exactly what the null space method does. Of course it is 1-2 OoM slower than direct KKT matrix solve. You can use Givens or Householder updates to the QR matrices rather than full decomposition at each iteration but I have not implemented it. The code is currently set up for direct KKT solver with a backup to null space method in case of failure.
josef-pkt commentedon Jan 17, 2020
to see if I understand:
Would your QR nullspace method use the QR(X'X) in OLS and not AR of the original data QR(X)?
AFAIU, for general quadratic optimization problems like the portfolio choice, we only have the
Amatrix for the objective function. In OLS (similar to scipy.optimize nonlinear least squares) we have the matrix of explanatory variables X that includes all observations, so we can avoid directly computing A = X'X.I was thinking of using QR(X) just to be able to use e.g. direct KKT in the optimization problem itself.
bashtage commentedon Jan 17, 2020
@JimVaranelli For something relatively generic like a non-neg lst square solver, it would probably be a better fit with SciPy. It would also reach a wider audience if that matters to you.
JimVaranelli commentedon Jan 17, 2020
@josef-pkt The Hessian is only calculated once at the beginning of the run but is used in both the null space method (if number of active constraints is less than the number of variables) and direct KKT solve. Null space performs a QR on the (active) constraint matrix as opposed to the Hessian as in OLS. The KKT matrix is square symmetric so direct solution is quite fast using numpy.linalg.solve.
@bashtage I agree it is more of a scientific computing piece of code. The main contribution is the generic active set algo which AFAIK does not exist in python. The primary motivation for adding the repo is to support a research paper with the constrained least squares used for ensemble scorer weight optimization which was refactored from our C++ portfolio optimizer. I just abstracted out the active set method as a base class in the python version.
josef-pkt commentedon Aug 16, 2021
plain bootstrap doesn't help. It is not consistent on the boundary of the parameter space.
If true value is away from the boundary of the parameter space, then asymptotic normality holds and bootstrap is consistent, but not if the true value is at a boundary, e.g. b=0 under non-negativity constraint.
Bootstrap is not consistent if parameter goes to zero asymptotically. 1st page of Andrews.
Small sample properties for parameters close to the boundary will not be close to asymptotic normality.
Andrews, Donald WK. "Inconsistency of the bootstrap when a parameter is on the boundary of the parameter space." Econometrica (2000): 399-405.
Hurwitz has a section in his overview article with more recent references (e.g. for inequality moment conditions)
Horowitz, Joel L. "Bootstrap methods in econometrics." Annual Review of Economics 11 (2019): 193-224.
another stackoverflow question: https://stackoverflow.com/questions/68793818/how-to-use-statsmodels-linear-regression-with-positive-fit-values
josef-pkt commentedon Aug 16, 2021
an idea that might not work. I didn't find references for it in google scholar
score confidence interval with boundary of parameter space or with inequality constraints.
(references found refer mostly to proportions)
assume parameter space is m >= 0
Find confidence interval upper bound m_upp that is consistent with the data at level alpha under assumption that m = m_upp.
This would avoid a null hypothesis that parameter is at boundary m=0.
That's what we use to construct score confidence intervals for proportions, where we have [0, 1] parameter space with boundaries.
related: confint if we observe all 0 (or 1) in binary proportion case. (wald ci collapses to boundary point or nan, score ci has positive length)
josef-pkt commentedon Aug 16, 2021
And now for something completely different:
I don't really care about "literal" null hypotheses. All models are wrong and no point null hypothesis ever holds exactly.
Even if we don't have wald inference, we can still do useful things, like compute goodness-of-fit statistics and compare predictive accuracy or accuracy of predictive distributions. (e.g. what I did for predictive count probabilities, hosmer-lemeshow type, PIT).
When we have monotonicity in a test statistic, then we can just look at equivalence testing, e.g. anything smaller than delta is the essentially equivalent to boundary value of zero.
This way we can compare models without having to compute inference for individual statistics.
i.e. it's not relevant, even though it might be statistically significant (even when we don't know whether statistical significance holds or not)
One question are information criteria. Does ranking by them still choose the "right" model?