1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.stat.regression;
18
19 import org.apache.commons.math.MathRuntimeException;
20 import org.apache.commons.math.linear.RealMatrix;
21 import org.apache.commons.math.linear.Array2DRowRealMatrix;
22 import org.apache.commons.math.linear.RealVector;
23 import org.apache.commons.math.linear.ArrayRealVector;
24
25
26
27
28
29
30 public abstract class AbstractMultipleLinearRegression implements
31 MultipleLinearRegression {
32
33
34 protected RealMatrix X;
35
36
37 protected RealVector Y;
38
39
40
41
42
43
44
45
46
47 public void newSampleData(double[] data, int nobs, int nvars) {
48 double[] y = new double[nobs];
49 double[][] x = new double[nobs][nvars + 1];
50 int pointer = 0;
51 for (int i = 0; i < nobs; i++) {
52 y[i] = data[pointer++];
53 x[i][0] = 1.0d;
54 for (int j = 1; j < nvars + 1; j++) {
55 x[i][j] = data[pointer++];
56 }
57 }
58 this.X = new Array2DRowRealMatrix(x);
59 this.Y = new ArrayRealVector(y);
60 }
61
62
63
64
65
66
67 protected void newYSampleData(double[] y) {
68 this.Y = new ArrayRealVector(y);
69 }
70
71
72
73
74
75
76 protected void newXSampleData(double[][] x) {
77 this.X = new Array2DRowRealMatrix(x);
78 }
79
80
81
82
83
84
85
86
87
88 protected void validateSampleData(double[][] x, double[] y) {
89 if ((x == null) || (y == null) || (x.length != y.length)) {
90 throw MathRuntimeException.createIllegalArgumentException(
91 "dimension mismatch {0} != {1}",
92 (x == null) ? 0 : x.length,
93 (y == null) ? 0 : y.length);
94 } else if ((x.length > 0) && (x[0].length > x.length)) {
95 throw MathRuntimeException.createIllegalArgumentException(
96 "not enough data ({0} rows) for this many predictors ({1} predictors)",
97 x.length, x[0].length);
98 }
99 }
100
101
102
103
104
105
106
107
108
109 protected void validateCovarianceData(double[][] x, double[][] covariance) {
110 if (x.length != covariance.length) {
111 throw MathRuntimeException.createIllegalArgumentException(
112 "dimension mismatch {0} != {1}", x.length, covariance.length);
113 }
114 if (covariance.length > 0 && covariance.length != covariance[0].length) {
115 throw MathRuntimeException.createIllegalArgumentException(
116 "a {0}x{1} matrix was provided instead of a square matrix",
117 covariance.length, covariance[0].length);
118 }
119 }
120
121
122
123
124 public double[] estimateRegressionParameters() {
125 RealVector b = calculateBeta();
126 return b.getData();
127 }
128
129
130
131
132 public double[] estimateResiduals() {
133 RealVector b = calculateBeta();
134 RealVector e = Y.subtract(X.operate(b));
135 return e.getData();
136 }
137
138
139
140
141 public double[][] estimateRegressionParametersVariance() {
142 return calculateBetaVariance().getData();
143 }
144
145
146
147
148 public double[] estimateRegressionParametersStandardErrors() {
149 double[][] betaVariance = estimateRegressionParametersVariance();
150 double sigma = calculateYVariance();
151 int length = betaVariance[0].length;
152 double[] result = new double[length];
153 for (int i = 0; i < length; i++) {
154 result[i] = Math.sqrt(sigma * betaVariance[i][i]);
155 }
156 return result;
157 }
158
159
160
161
162 public double estimateRegressandVariance() {
163 return calculateYVariance();
164 }
165
166
167
168
169
170
171 protected abstract RealVector calculateBeta();
172
173
174
175
176
177
178
179 protected abstract RealMatrix calculateBetaVariance();
180
181
182
183
184
185
186 protected abstract double calculateYVariance();
187
188
189
190
191
192
193
194
195
196
197
198 protected RealVector calculateResiduals() {
199 RealVector b = calculateBeta();
200 return Y.subtract(X.operate(b));
201 }
202
203 }