Examples / QPROG.RPF |
QPROG.RPF demonstrates the use of quadratic programming (LQPROG instruction) for constrained least squares. It estimates a regression subject to non-negativity constraints and an adding up constraint.
In matrix form, the sum of squared residuals can be written
\begin{equation} \left( {{\bf{y}} - {\bf{X}}\beta } \right)^\prime \left( {{\bf{y}} - {\bf{X}}\beta } \right) = \beta '{\kern 1pt} {\bf{X'}}{\kern 1pt} {\bf{X}}\beta - {\bf{2y'}}{\kern 1pt} {\bf{X}}\beta + {\bf{y'y}} \end{equation}
The last term doesn’t involve \(\beta\) and so can be ignored in finding the optimal coefficients. If we multiply this by 1/2 we can read off \({\bf{Q}} = {\bf{X'}}{\kern 1pt} {\bf{X}}\) and \({\bf{c}} = - {\bf{X'y}}\). For illustration, we will add to this the constraint that \({\bf{1}} \bullet \beta \le 1\). \({\bf{X'}}{\kern 1pt} {\bf{X}}\) and \({\bf{X'y}}\) can be obtained as submatrices of the cross-moment matrix of the regressors and \(y\). The CMOMENT instruction with the LASTREG option produces just such a matrix (regressors first, then dependent variable):
linreg longrate
# shortrate{0 to 12}
cmom(lastreg)
*
compute [symmetric] q = %xsubmat(%cmom,1,%ncmom-1,1,%ncmom-1)
compute [vector] c = -1.0*%xsubmat(%cmom,%ncmom,%ncmom,1,%ncmom-1)
This sets up the \(\bf{A}\) matrix:
compute a=%ones(1,%ncmom-1)
This does the optimization (which, with these options, will have all coefficients non-negative and summing to one):
lqprog(c=c,q=q,a=a,b=1.0) x
and this uses REPORT to display the OLS estimates (which are in %BETA) and the constrained estimates (in X) in parallel columns:
report(action=define,hlabels=||"Label","OLS","Constrained"||)
report(fillby=cols,atrow=1,atcol=1) %eqnreglabels(0)
report(fillby=cols,atrow=1,atcol=2) %beta
report(fillby=cols,atrow=1,atcol=3) x
report(action=show)
Full Program
open data haversample.rat
calendar(m) 1947
data(format=rats) 1947:1 2007:4 fltg ftb3
set shortrate = ftb3
set longrate = fltg
*
linreg longrate
# shortrate{0 to 12}
cmom(lastreg)
*
compute [symmetric] q = %xsubmat(%cmom,1,%ncmom-1,1,%ncmom-1)
compute [vector] c = -1.0*%xsubmat(%cmom,%ncmom,%ncmom,1,%ncmom-1)
*
compute a=%ones(1,%ncmom-1)
lqprog(c=c,q=q,a=a,b=1.0) x
*
report(action=define,hlabels=||"Label","OLS","Constrained"||)
report(fillby=cols,atrow=1,atcol=1) %eqnreglabels(0)
report(fillby=cols,atrow=1,atcol=2) %beta
report(fillby=cols,atrow=1,atcol=3) x
report(action=show)
Output
Linear Regression - Estimation by Least Squares
Dependent Variable LONGRATE
Monthly Data From 1948:01 To 2007:04
Usable Observations 712
Degrees of Freedom 699
Centered R^2 0.7128498
R-Bar^2 0.7079201
Uncentered R^2 0.9545383
Mean of Dependent Variable 6.0815870787
Std Error of Dependent Variable 2.6394706845
Standard Error of Estimate 1.4264864346
Sum of Squared Residuals 1422.3696202
Log Likelihood -1256.6368
Durbin-Watson Statistic 0.0526
Variable Coeff Std Error T-Stat Signif
************************************************************************************
1. SHORTRATE 0.705702074 0.134743997 5.23735 0.00000022
2. SHORTRATE{1} -0.215702353 0.225120830 -0.95816 0.33831207
3. SHORTRATE{2} 0.120288070 0.234432537 0.51310 0.60804130
4. SHORTRATE{3} -0.119402443 0.234465771 -0.50925 0.61073548
5. SHORTRATE{4} 0.013673999 0.234970463 0.05819 0.95361030
6. SHORTRATE{5} 0.031937149 0.236038455 0.13530 0.89240980
7. SHORTRATE{6} 0.180818041 0.240205942 0.75276 0.45184604
8. SHORTRATE{7} -0.038395627 0.235983733 -0.16270 0.87079809
9. SHORTRATE{8} 0.046579619 0.234967765 0.19824 0.84291623
10. SHORTRATE{9} -0.105761855 0.234483504 -0.45104 0.65209933
11. SHORTRATE{10} 0.141730720 0.234480817 0.60444 0.54574407
12. SHORTRATE{11} -0.180278995 0.225195680 -0.80054 0.42366782
13. SHORTRATE{12} 0.587076076 0.134843579 4.35376 0.00001538
QPROG converges at 9 iterations
minimum = -14621.17962725
Solution x =
0.4847369835 0.0000000000 0.0000000000 0.0000000000 0.0000000000
0.0267942351 0.0794425993 0.0196099240 0.0000000000 0.0000000000
0.0000000000 0.0000000000 0.3894162584
Lagrange Multipliers
3673.1565043
Label OLS Constrained
SHORTRATE 0.705702 0.484737
SHORTRATE{1} -0.215702 0.000000
SHORTRATE{2} 0.120288 0.000000
SHORTRATE{3} -0.119402 0.000000
SHORTRATE{4} 0.013674 0.000000
SHORTRATE{5} 0.031937 0.026794
SHORTRATE{6} 0.180818 0.079443
SHORTRATE{7} -0.038396 0.019610
SHORTRATE{8} 0.046580 0.000000
SHORTRATE{9} -0.105762 0.000000
SHORTRATE{10} 0.141731 0.000000
SHORTRATE{11} -0.180279 0.000000
SHORTRATE{12} 0.587076 0.389416
Copyright © 2025 Thomas A. Doan