-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.js
280 lines (257 loc) · 6.37 KB
/
model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
*
* LINEAR-REGRESSION: Model
*
*
* DESCRIPTION:
* - Defines a linear regression model class.
*
*
* NOTES:
* [1]
*
*
* TODO:
* [1]
*
*
* LICENSE:
* MIT
*
* Copyright (c) 2015. Athan Reines.
*
*
* AUTHOR:
* Athan Reines. kgryte@gmail.com. 2015.
*
*/
'use strict';
// MODULES //
var isArray = require( 'validate.io-array' ),
isObject = require( 'validate.io-object' ),
isFunction = require( 'validate.io-function' ),
isNumber = require( 'validate.io-number' ),
isBoolean = require( 'validate.io-boolean-primitive' );
// MODEL //
/**
* FUNCTION: createModel( x, y, slope, yint )
* Creates a new linear model.
*
* @param {Number[]} x - explanatory variable array
* @param {Number[]} y - response variable array
* @param {Number} slope - line slope
* @param {Number} yint - line y-intercept
* @returns {Object} model object
*/
function createModel( x, y, slope, yint ) {
var N = x.length, // number of observations
m = 2, // number of params
residuals,
summary,
ci,
model;
model = {};
/**
* ATTRIBUTE: params
* Model parameters and corresponding linear regression coefficients.
*
* @type {Number[]}
*/
Object.defineProperty( model, 'params', {
'configurable': false,
'enumerable': true,
get: function() {
// Define a getter so as to prevent corrupting the internal model state. Return a new parameter array each time the `params` property is accessed...
return [ yint, slope ];
}
});
/**
* ATTRIBUTE: residuals
* Model residuals; i.e., the difference between each observation `y_i` and its corresponding prediction `f(x_i) = y^{hat}_i`. Note: the residuals are lazily evaluated the first time they are accessed. Subsequent access returns a copy of the residual array.
*
* @type {Number[]}
*/
Object.defineProperty( model, 'residuals', {
'configurable': false,
'enumerable': true,
get: function() {
var arr = [],
yhat,
i;
if ( !residuals ) {
residuals = [];
for ( i = 0; i < N; i++ ) {
yhat = x[ i ]*slope + yint;
residuals.push( y[ i ] - yhat );
}
}
for ( i = 0; i < N; i++ ) {
arr.push( residuals[ i ] );
}
return arr;
}
});
/**
* ATTRIBUTE: ci
* Confidence intervals for the estimated model parameters. Note: the confidence intervals are lazily evaluated the first time they are accessed. Subsequent access returns a deep copy of the confidence interval array.
*
* @type {Array[]}
*/
Object.defineProperty( model, 'ci', {
'configurable': false,
'enumerable': true,
get: function() {
var arr,
tmp,
i;
if ( !ci ) {
ci = [];
// TODO: Slope confidence interval...
ci.push( [] );
// TODO: intercept confidence interval...
ci.push( [] );
}
arr = [];
for ( i = 0; i < m; i++ ) {
tmp = ci[ i ];
arr.push( [ tmp[0], tmp[1] ] );
}
return arr;
}
});
/**
* ATTRIBUTE: summary
* A model's statistical summary. Note: the summary is lazily evaluated the first time it is accessed. Subsequent access returns a deep copy.
*
* @type {Object}
*/
Object.defineProperty( model, 'summary', {
'configurable': false,
'enumerable': true,
get: function() {
if ( !summary ) {
// TODO: generate a summary
summary = {};
}
// TODO: copy. Possible deep copy depending on the summary structure. If so, use the util-copy module for deep cloning.
return {};
}
});
/**
* METHOD: predict( val[, opts] )
* Computes a predicted response `y^{hat}_i` for each `x_i`.
*
* @param {Number|Number[]} val - independent variable
* @param {Object} [opts] - method options
* @param {Function} [opts.accessor] - accessor function for accessing array values
* @param {Boolean} [opts.ci=false] - boolean indicating whether to compute confidence intervals for predicted responses
* @param {Boolean} [opts.copy=true] - boolean indicating whether to return a new array when computing predicted responses
* @returns {Number|Array|Array[]} prediction(s)
*/
model.predict = function( val, opts ) {
var isNum = isNumber( val ),
copy = true,
clbk,
yhat,
tmp,
len,
ci,
x,
i;
if ( !isNum && !isArray( val ) ) {
throw new TypeError( 'predict()::invalid input argument. Must provide either a single value or an an array of values. Value: `' + val + '`.' );
}
if ( arguments.length > 1 ) {
if ( !isObject( opts ) ) {
throw new TypeError( 'predict()::invalid input argument. Options must be an object. Value: `' + opts + '`.' );
}
if ( opts.hasOwnProperty( 'accessor' ) ) {
clbk = opts.accessor;
if ( !isFunction( clbk ) ) {
throw new TypeError( 'predict()::invalid option. Accessor must be a function. Option: `' + clbk + '`.' );
}
}
if ( opts.hasOwnProperty( 'ci' ) ) {
ci = opts.ci;
if ( !isBoolean( ci ) ) {
throw new TypeError( 'predict()::invalid option. CI option must be a boolean primitive. Option: `' + ci + '`.' );
}
}
if ( opts.hasOwnProperty( 'copy' ) ) {
copy = opts.copy;
if ( !isBoolean( copy ) ) {
throw new TypeError( 'predict()::invalid option. Copy option must be a boolean primitive. Option: `' + copy + '`.' );
}
}
}
if ( isNum ) {
if ( ci ) {
tmp = new Array( 3 );
tmp[ 0 ] = slope*val + yint;
// TODO: compute ci
tmp[ 1 ] = null;
tmp[ 2 ] = null;
yhat = [ tmp ];
} else {
yhat = slope*val + yint;
}
return yhat;
}
len = val.length;
if ( copy ) {
if ( clbk ) {
x = new Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = clbk( val[ i ] );
}
} else {
x = val;
}
yhat = new Array( len );
} else {
x = val;
if ( clbk ) {
for ( i = 0; i < len; i++ ) {
x[ i ] = clbk( x[ i ] );
}
}
yhat = x;
}
if ( ci ) {
// TODO: compute ci.
for ( i = 0; i < len; i++ ) {
tmp = new Array( 3 );
tmp[ 0 ] = slope*x[ i ] + yint;
tmp[ 1 ] = null;
tmp[ 2 ] = null;
yhat[ i ] = tmp;
}
} else {
for ( i = 0; i < len; i++ ) {
yhat[ i ] = slope*x[ i ] + yint;
}
}
return yhat;
}; // end METHOD predict()
/**
* METHOD: toString()
* Pretty prints a model.
*
* @returns {String} pretty printed model
*/
model.toString = function() {
var str = '',
line = '',
tmp;
if ( !summary ) {
// Generate the summary if not generated already...
tmp = model.summary;
}
// TODO: create the string lines.
str += line;
return str;
}; // end METHOD toString()
return model;
} // end FUNCTION createModel()
// EXPORTS //
module.exports = createModel;