|
5 | 5 | public abstract class LinearRegression {
|
6 | 6 |
|
7 | 7 | static public JsonObject run( ValueArray ary, int colA, int colB ) {
|
| 8 | + return( exec(ary,colA,colB).toJson() ); |
| 9 | + } |
| 10 | + static public LRResult exec( ValueArray ary, int colA, int colB ) { |
8 | 11 | // Pass 1: compute sums & sums-of-squares
|
9 | 12 | long start = System.currentTimeMillis();
|
10 | 13 | CalcSumsTask lr1 = new CalcSumsTask();
|
@@ -42,23 +45,22 @@ static public JsonObject run( ValueArray ary, int colA, int colB ) {
|
42 | 45 | double svar1 = svar / lr2._XXbar;
|
43 | 46 | double svar0 = svar/n + lr2._Xbar*lr2._Xbar*svar1;
|
44 | 47 |
|
45 |
| - JsonObject res = new JsonObject(); |
46 |
| - res.addProperty("Key", ary._key.toString()); |
47 |
| - res.addProperty("ColA", ary._cols[colA]._name); |
48 |
| - res.addProperty("ColB", ary._cols[colB]._name); |
49 |
| - res.addProperty("Pass1Msecs", pass1 - start); |
50 |
| - res.addProperty("Pass2Msecs", pass2-pass1); |
51 |
| - res.addProperty("Pass3Msecs", pass3-pass2); |
52 |
| - res.addProperty("Rows", n); |
53 |
| - res.addProperty("Beta0", lr3._beta0); |
54 |
| - res.addProperty("Beta1", lr3._beta1); |
55 |
| - res.addProperty("RSquared", R2); |
56 |
| - res.addProperty("Beta0StdErr", Math.sqrt(svar0)); |
57 |
| - res.addProperty("Beta1StdErr", Math.sqrt(svar1)); |
58 |
| - res.addProperty("SSTO", lr2._YYbar); |
59 |
| - res.addProperty("SSE", lr3._rss); |
60 |
| - res.addProperty("SSR", lr3._ssr); |
61 |
| - return res; |
| 48 | + LRResult result = new LRResult(ary._key.toString(), |
| 49 | + ary._cols[colA]._name, |
| 50 | + ary._cols[colB]._name, |
| 51 | + pass1-start, |
| 52 | + pass2-pass1, |
| 53 | + pass3-pass2, |
| 54 | + n, |
| 55 | + lr3._beta0, |
| 56 | + lr3._beta1, |
| 57 | + R2, |
| 58 | + Math.sqrt(svar0), |
| 59 | + Math.sqrt(svar1), |
| 60 | + lr2._YYbar, |
| 61 | + lr3._rss, |
| 62 | + lr3._ssr ); |
| 63 | + return( result ); |
62 | 64 | }
|
63 | 65 |
|
64 | 66 | public static class CalcSumsTask extends MRTask {
|
@@ -220,4 +222,94 @@ public void reduce( DRemoteTask rt ) {
|
220 | 222 | _ssr += lr3._ssr;
|
221 | 223 | }
|
222 | 224 | }
|
| 225 | + public static class LRResult{ |
| 226 | + public final String key; |
| 227 | + public final String colA; |
| 228 | + public final String colB; |
| 229 | + public final long pass1Msecs; |
| 230 | + public final long pass2Msecs; |
| 231 | + public final long pass3Msecs; |
| 232 | + public final long rows; |
| 233 | + public final double beta0; |
| 234 | + public final double beta1; |
| 235 | + public final double rSquared; |
| 236 | + public final double beta0StdErr; |
| 237 | + public final double beta1StdErr; |
| 238 | + public final double ssto; |
| 239 | + public final double sse; |
| 240 | + public final double ssr; |
| 241 | + |
| 242 | + public LRResult (String key, |
| 243 | + String colA, |
| 244 | + String colB, |
| 245 | + long pass1Msecs, |
| 246 | + long pass2Msecs, |
| 247 | + long pass3Msecs, |
| 248 | + long rows, |
| 249 | + double beta0, |
| 250 | + double beta1, |
| 251 | + double rSquared, |
| 252 | + double beta0StdErr, |
| 253 | + double beta1StdErr, |
| 254 | + double ssto, |
| 255 | + double sse, |
| 256 | + double ssr) { |
| 257 | + this.key = key; |
| 258 | + this.colA = colA; |
| 259 | + this.colB = colB; |
| 260 | + this.pass1Msecs = pass1Msecs; |
| 261 | + this.pass2Msecs = pass2Msecs; |
| 262 | + this.pass3Msecs = pass3Msecs; |
| 263 | + this.rows = rows; |
| 264 | + this.beta0 = beta0; |
| 265 | + this.beta1 = beta1; |
| 266 | + this.rSquared = rSquared; |
| 267 | + this.beta0StdErr = beta0StdErr; |
| 268 | + this.beta1StdErr = beta1StdErr; |
| 269 | + this.ssto = ssto; |
| 270 | + this.sse = sse; |
| 271 | + this.ssr = ssr; |
| 272 | + } |
| 273 | + |
| 274 | + @Override |
| 275 | + public String toString () { |
| 276 | + return "LRResult{" + |
| 277 | + "key='" + key + '\'' + |
| 278 | + ", colA='" + colA + '\'' + |
| 279 | + ", colB='" + colB + '\'' + |
| 280 | + ", pass1Msecs=" + pass1Msecs + |
| 281 | + ", pass2Msecs=" + pass2Msecs + |
| 282 | + ", pass3Msecs=" + pass3Msecs + |
| 283 | + ", rows=" + rows + |
| 284 | + ", beta0=" + beta0 + |
| 285 | + ", beta1=" + beta1 + |
| 286 | + ", rSquared=" + rSquared + |
| 287 | + ", beta0StdErr=" + beta0StdErr + |
| 288 | + ", beta1StdErr=" + beta1StdErr + |
| 289 | + ", ssto=" + ssto + |
| 290 | + ", sse=" + sse + |
| 291 | + ", ssr=" + ssr + |
| 292 | + '}'; |
| 293 | + } |
| 294 | + |
| 295 | + public JsonObject toJson() { |
| 296 | + JsonObject res = new JsonObject(); |
| 297 | + res.addProperty("Key", key); |
| 298 | + res.addProperty("ColA", colA); |
| 299 | + res.addProperty("ColB", colB); |
| 300 | + res.addProperty("Pass1Msecs", pass1Msecs); |
| 301 | + res.addProperty("Pass2Msecs", pass2Msecs); |
| 302 | + res.addProperty("Pass3Msecs", pass3Msecs); |
| 303 | + res.addProperty("Rows", rows); |
| 304 | + res.addProperty("Beta0", beta0); |
| 305 | + res.addProperty("Beta1", beta1); |
| 306 | + res.addProperty("RSquared", rSquared); |
| 307 | + res.addProperty("Beta0StdErr", beta0StdErr); |
| 308 | + res.addProperty("Beta1StdErr", beta1StdErr); |
| 309 | + res.addProperty("SSTO", ssto); |
| 310 | + res.addProperty("SSE", sse); |
| 311 | + res.addProperty("SSR", ssr); |
| 312 | + return( res ); |
| 313 | + } |
| 314 | + } |
223 | 315 | }
|
0 commit comments