Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.
Permalink
Newer
Older
100644 1192 lines (1004 sloc) 32.5 KB
June 25, 2014 00:08
1
// This file was generated by modules-webmake (modules for web) project.
2
// See: https://github.com/medikoo/modules-webmake
3
4
(function (modules) {
5
'use strict';
6
7
var resolve, getRequire, wmRequire, notFoundError, findFile
8
, extensions = {".js":[],".json":[],".css":[],".html":[]}
March 4, 2015 18:49
9
, envRequire = typeof require === 'function' ? require : null;
June 25, 2014 00:08
10
11
notFoundError = function (path) {
12
var error = new Error("Could not find module '" + path + "'");
13
error.code = 'MODULE_NOT_FOUND';
14
return error;
15
};
16
findFile = function (scope, name, extName) {
17
var i, ext;
18
if (typeof scope[name + extName] === 'function') return name + extName;
19
for (i = 0; (ext = extensions[extName][i]); ++i) {
20
if (typeof scope[name + ext] === 'function') return name + ext;
21
}
22
return null;
23
};
24
resolve = function (scope, tree, path, fullPath, state, id) {
25
var name, dir, exports, module, fn, found, i, ext;
26
path = path.split('/');
27
name = path.pop();
28
if ((name === '.') || (name === '..')) {
29
path.push(name);
30
name = '';
31
}
32
while ((dir = path.shift()) != null) {
33
if (!dir || (dir === '.')) continue;
34
if (dir === '..') {
35
scope = tree.pop();
36
id = id.slice(0, id.lastIndexOf('/'));
37
} else {
38
tree.push(scope);
39
scope = scope[dir];
40
id += '/' + dir;
41
}
42
if (!scope) throw notFoundError(fullPath);
43
}
44
if (name && (typeof scope[name] !== 'function')) {
45
found = findFile(scope, name, '.js');
46
if (!found) found = findFile(scope, name, '.json');
47
if (!found) found = findFile(scope, name, '.css');
48
if (!found) found = findFile(scope, name, '.html');
49
if (found) {
50
name = found;
51
} else if ((state !== 2) && (typeof scope[name] === 'object')) {
52
tree.push(scope);
53
scope = scope[name];
54
id += '/' + name;
55
name = '';
56
}
57
}
58
if (!name) {
59
if ((state !== 1) && scope[':mainpath:']) {
60
return resolve(scope, tree, scope[':mainpath:'], fullPath, 1, id);
61
}
62
return resolve(scope, tree, 'index', fullPath, 2, id);
63
}
64
fn = scope[name];
65
if (!fn) throw notFoundError(fullPath);
66
if (fn.hasOwnProperty('module')) return fn.module.exports;
67
exports = {};
68
fn.module = module = { exports: exports, id: id + '/' + name };
69
fn.call(exports, exports, module, getRequire(scope, tree, id));
70
return module.exports;
71
};
72
wmRequire = function (scope, tree, fullPath, id) {
73
var name, path = fullPath, t = fullPath.charAt(0), state = 0;
74
if (t === '/') {
75
path = path.slice(1);
76
scope = modules['/'];
77
if (!scope) {
78
if (envRequire) return envRequire(fullPath);
79
throw notFoundError(fullPath);
80
}
81
id = '/';
82
tree = [];
83
} else if (t !== '.') {
84
name = path.split('/', 1)[0];
85
scope = modules[name];
86
if (!scope) {
87
if (envRequire) return envRequire(fullPath);
88
throw notFoundError(fullPath);
89
}
90
id = name;
91
tree = [];
92
path = path.slice(name.length + 1);
93
if (!path) {
94
path = scope[':mainpath:'];
95
if (path) {
96
state = 1;
97
} else {
98
path = 'index';
99
state = 2;
100
}
101
}
102
}
103
return resolve(scope, tree, path, fullPath, state, id);
104
};
105
getRequire = function (scope, tree, id) {
106
return function (path) {
107
return wmRequire(scope, [].concat(tree), path, id);
108
};
109
};
110
return getRequire(modules, [], '');
111
})({
112
"jerzy": {
113
"jerzy.js": function (exports, module, require) {
114
var vector = require('./lib/vector');
July 23, 2014 23:32
115
var factor = require('./lib/factor');
July 30, 2014 19:50
116
var matrix = require('./lib/matrix');
June 25, 2014 00:08
117
var t = require('./lib/t');
118
var misc = require('./lib/misc');
119
var distributions = require('./lib/distributions');
120
var regression = require('./lib/regression');
June 29, 2014 00:34
121
var correlation = require('./lib/correlation');
June 25, 2014 00:08
122
var numeric = require('./lib/numeric');
July 23, 2014 23:32
123
var anova = require('./lib/anova');
July 30, 2014 19:50
124
var normality = require('./lib/normality');
125
var confidence = require('./lib/confidence');
October 22, 2014 07:28
126
var power = require('./lib/power');
October 31, 2014 11:02
127
var nonparametric = require('./lib/nonparametric');
June 25, 2014 00:08
128
129
module.exports.Vector = vector.Vector;
July 23, 2014 23:32
130
module.exports.Factor = factor.Factor;
July 30, 2014 19:50
131
module.exports.Matrix = factor.Matrix;
June 25, 2014 00:08
132
module.exports.Sequence = vector.Sequence;
133
module.exports.StudentT = t.StudentT;
134
module.exports.Misc = misc.Misc;
135
module.exports.Numeric = numeric.Numeric;
136
module.exports.Normal = distributions.Normal;
137
module.exports.StandardNormal = distributions.StandardNormal;
138
module.exports.T = distributions.T;
July 23, 2014 23:32
139
module.exports.F = distributions.F;
October 31, 2014 11:02
140
module.exports.Kolmogorov = distributions.Kolmogorov;
June 29, 2014 00:34
141
module.exports.Regression = regression.Regression;
July 23, 2014 23:32
142
module.exports.Correlation = correlation.Correlation;
July 30, 2014 19:50
143
module.exports.Anova = anova.Anova;
144
module.exports.Normality = normality.Normality;
October 22, 2014 07:28
145
module.exports.Confidence = confidence.Confidence;
October 31, 2014 11:02
146
module.exports.Power = power.Power;
147
module.exports.Nonparametric = nonparametric.Nonparametric;
June 25, 2014 00:08
148
},
149
"lib": {
July 23, 2014 23:32
150
"anova.js": function (exports, module, require) {
151
var vector = require('./vector');
152
var distributions = require('./distributions');
153
154
Anova = function() {};
155
156
/*
157
* One-way ANOVA
158
*/
159
160
Anova.oneway = function(x, y) {
161
var result = {};
162
163
var vectors = [];
164
for (var i = 0; i < x.groups(); i++) {
165
var v = new vector.Vector([]);
166
var indices = x.group(i);
167
for (var j = 0; j < indices.length; j++) {
168
v.push(y.elements[indices[j]]);
169
}
170
vectors.push(v);
171
}
172
173
var mean = new vector.Vector([]);
174
var n = new vector.Vector([]);
175
var v = new vector.Vector([]);
176
for (var i = 0; i < vectors.length; i++) {
177
mean.push(vectors[i].mean());
178
n.push(vectors[i].length());
179
v.push(vectors[i].variance());
180
}
181
182
result.tdf = x.groups() - 1;
183
result.tss = mean.add(-y.mean()).pow(2).multiply(n).sum();
184
result.tms = result.tss / result.tdf;
185
186
result.edf = x.length() - x.groups();
187
result.ess = v.multiply(n.add(-1)).sum();
188
result.ems = result.ess / result.edf;
189
190
result.f = result.tms / result.ems;
191
192
var fdistr = new distributions.F(result.tdf, result.edf);
193
result.p = 1 - fdistr.distr(Math.abs(result.f));
194
195
return result;
196
}
197
198
module.exports.Anova = Anova;
199
},
200
"confidence.js": function (exports, module, require) {
201
var distributions = require('./distributions');
202
203
Confidence = function() {};
204
August 2, 2014 22:35
205
Confidence.normal = function(x, c) {
206
var alpha = 1 - c;
207
var t = new distributions.T(x.length() - 1);
August 2, 2014 22:35
208
var lower = x.mean() - t.inverse(1 - alpha / 2) * x.sem();
209
var upper = x.mean() + t.inverse(1 - alpha / 2) * x.sem();
210
return [lower, upper];
211
};
212
August 2, 2014 22:35
213
Confidence.normalUpper = function(x, c) {
214
var alpha = 1 - c;
215
var t = new distributions.T(x.length() - 1);
216
return(x.mean() + t.inverse(1 - alpha) * x.sem());
217
};
218
219
Confidence.normalLower = function(x, c) {
220
var alpha = 1 - c;
221
var t = new distributions.T(x.length() - 1);
222
return(x.mean() - t.inverse(1 - alpha) * x.sem());
223
};
224
225
module.exports.Confidence = Confidence;
226
},
June 29, 2014 00:34
227
"correlation.js": function (exports, module, require) {
June 30, 2014 20:59
228
var distributions = require('./distributions');
229
June 29, 2014 00:34
230
Correlation = function() {};
231
232
/*
233
* Pearson correlation
234
*/
235
236
Correlation.pearson = function(x, y) {
June 30, 2014 20:59
237
var result = {};
238
var n = x.length();
June 29, 2014 00:34
239
var mx = x.mean();
240
var my = y.mean();
June 30, 2014 20:59
241
result.r = x.add(-mx).multiply(y.add(-my)).sum() /
June 29, 2014 00:34
242
Math.sqrt(x.add(-mx).pow(2).sum() * y.add(-my).pow(2).sum());
June 30, 2014 20:59
243
result.t = result.r * Math.sqrt((n - 2) / (1 - Math.pow(result.r, 2)));
244
result.df = n - 2;
245
var tdistr = new distributions.T(result.df);
246
result.p = 2 * (1 - tdistr.distr(Math.abs(result.t)));
247
return result;
June 29, 2014 00:34
248
};
249
250
module.exports.Correlation = Correlation;
251
},
June 25, 2014 00:08
252
"distributions.js": function (exports, module, require) {
253
var vector = require('./vector');
254
var misc = require('./misc');
255
var numeric = require('./numeric');
June 25, 2014 00:08
256
257
/*
258
* Normal distribution
259
*/
260
261
Normal = function(mean, variance) {
262
this.mean = mean;
263
this.variance = variance;
June 29, 2014 00:34
264
};
June 25, 2014 00:08
265
266
Normal.prototype._de = function(x) {
267
return (1 / (Math.sqrt(this.variance) * (Math.sqrt(2 * Math.PI))))
268
* Math.exp(-(Math.pow(x - this.mean, 2)) / (2 * this.variance))
269
};
270
July 30, 2014 19:50
271
Normal.prototype._di = function(x) {
272
return 0.5 * (1 + misc.Misc.erf((x - this.mean) / (Math.sqrt(this.variance) * Math.sqrt(2))));
273
};
274
June 25, 2014 00:08
275
Normal.prototype.dens = function(arg) {
276
if (arg instanceof vector.Vector) {
277
result = new vector.Vector([]);
278
for (var i = 0; i < arg.length(); ++i) {
279
result.push(this._de(arg.elements[i]));
280
}
281
return result;
282
} else {
283
return this._de(arg);
284
}
June 29, 2014 00:34
285
};
June 25, 2014 00:08
286
July 30, 2014 19:50
287
Normal.prototype.distr = function(arg) {
288
if (arg instanceof vector.Vector) {
289
result = new vector.Vector([]);
290
for (var i = 0; i < arg.length(); ++i) {
291
result.push(this._di(arg.elements[i]));
292
}
293
return result;
294
} else {
295
return this._di(arg);
296
}
297
};
298
299
Normal.prototype.inverse = function(x) {
300
var a1 = -3.969683028665376e+1;
301
var a2 = 2.209460984245205e+2;
302
var a3 = -2.759285104469687e+2;
303
var a4 = 1.383577518672690e+2;
304
var a5 = -3.066479806614716e+1;
305
var a6 = 2.506628277459239e+0;
306
307
var b1 = -5.447609879822406e+1;
308
var b2 = 1.615858368580409e+2;
309
var b3 = -1.556989798598866e+2;
310
var b4 = 6.680131188771972e+1;
311
var b5 = -1.328068155288572e+1;
312
313
var c1 = -7.784894002430293e-3;
314
var c2 = -3.223964580411365e-1;
315
var c3 = -2.400758277161838e+0;
316
var c4 = -2.549732539343734e+0;
317
var c5 = 4.374664141464968e+0;
318
var c6 = 2.938163982698783e+0;
319
320
var d1 = 7.784695709041462e-3;
321
var d2 = 3.224671290700398e-1;
322
var d3 = 2.445134137142996e+0;
323
var d4 = 3.754408661907416e+0;
324
325
var q, r, y;
326
327
if (x < 0.02425) {
328
q = Math.sqrt(-2 * Math.log(x));
329
y = (((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6) / ((((d1 * q + d2) * q + d3) * q + d4) * q + 1);
330
} else if (x < 1 - 0.02425) {
331
q = x - 0.5;
332
r = q * q;
333
y = (((((a1 * r + a2) * r + a3) * r + a4) * r + a5) * r + a6) * q / (((((b1 * r + b2) * r + b3) * r + b4) * r + b5) * r + 1);
334
} else {
335
q = Math.sqrt(-2 * Math.log(1 - x));
336
y = -(((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6) / ((((d1 * q + d2) * q + d3) * q + d4) * q + 1);
337
}
338
339
return y * this.variance + this.mean;
340
};
341
June 25, 2014 00:08
342
/*
343
* Standard Normal distribution
344
*/
345
346
StandardNormal.prototype = new Normal();
347
348
StandardNormal.prototype.constructor = StandardNormal;
349
350
function StandardNormal() {
351
this.mean = 0;
352
this.variance = 1;
353
};
354
355
/*
356
* T distribution
357
*/
358
359
T = function(df) {
360
this.df = df;
361
};
362
363
T.prototype._de = function(x) {
364
return (misc.Misc.gamma((this.df + 1) / 2) / (Math.sqrt(this.df * Math.PI) * misc.Misc.gamma(this.df / 2)))
365
* Math.pow((1 + Math.pow(x, 2) / this.df), -(this.df + 1) / 2);
366
};
367
368
T.prototype._di = function(x) {
369
if (x < 0) {
370
return 0.5 * misc.Misc.rbeta(this.df / (Math.pow(x, 2) + this.df), this.df / 2, 0.5);
371
} else {
372
return 1 - 0.5 * misc.Misc.rbeta(this.df / (Math.pow(x, 2) + this.df), this.df / 2, 0.5);
373
}
374
};
375
376
T.prototype.dens = function(arg) {
377
if (arg instanceof vector.Vector) {
378
result = new vector.Vector([]);
379
for (var i = 0; i < arg.length(); ++i) {
380
result.push(this._de(arg.elements[i]));
381
}
382
return result;
383
} else {
384
return this._de(arg);
385
}
386
};
387
388
T.prototype.distr = function(arg) {
389
if (arg instanceof vector.Vector) {
390
result = new vector.Vector([]);
391
for (var i = 0; i < arg.length(); ++i) {
392
result.push(this._di(arg.elements[i]));
393
}
394
return result;
395
} else {
396
return this._di(arg);
397
}
398
};
399
400
T.prototype.inverse = function(x) {
401
return (function (o, x) {
402
var t = numeric.Numeric.bisection(function(y) {
403
return o._di(y) - x;
404
}, -10.1, 10);
405
return t;
406
})(this, x);
407
};
408
October 31, 2014 11:02
409
/*
410
* Kolmogorov distribution
411
*/
412
413
Kolmogorov = function() {};
414
415
Kolmogorov.prototype._di = function(x) {
416
var term;
417
var sum = 0;
418
var k = 1;
419
do {
420
term = Math.exp(-Math.pow(2 * k - 1, 2) * Math.pow(Math.PI, 2) / (8 * Math.pow(x, 2)));
421
sum = sum + term;
422
k++;
423
} while (Math.abs(term) > 0.000000000001);
424
return Math.sqrt(2 * Math.PI) * sum / x;
425
};
426
427
Kolmogorov.prototype.distr = function(arg) {
428
if (arg instanceof vector.Vector) {
429
result = new vector.Vector([]);
430
for (var i = 0; i < arg.length(); ++i) {
431
result.push(this._di(arg.elements[i]));
432
}
433
return result;
434
} else {
435
return this._di(arg);
436
}
437
};
438
439
Kolmogorov.prototype.inverse = function(x) {
440
return (function (o, x) {
441
var t = numeric.Numeric.bisection(function(y) {
442
return o._di(y) - x;
443
}, 0, 1);
444
return t;
445
})(this, x);
446
};
447
July 23, 2014 23:32
448
/*
449
* F distribution
450
*/
451
452
F = function(df1, df2) {
453
this.df1 = df1;
454
this.df2 = df2;
455
};
456
457
F.prototype._di = function(x) {
458
return misc.Misc.rbeta((this.df1 * x) / (this.df1 * x + this.df2), this.df1 / 2, this.df2 / 2);
459
};
460
461
F.prototype.distr = function(arg) {
462
if (arg instanceof vector.Vector) {
463
result = new vector.Vector([]);
464
for (var i = 0; i < arg.length(); ++i) {
465
result.push(this._di(arg.elements[i]));
466
}
467
return result;
468
} else {
469
return this._di(arg);
470
}
471
};
472
June 25, 2014 00:08
473
module.exports.Normal = Normal;
474
module.exports.StandardNormal = StandardNormal;
July 23, 2014 23:32
475
module.exports.T = T;
October 31, 2014 11:02
476
module.exports.F = F;
January 24, 2015 11:23
477
module.exports.Kolmogorov = Kolmogorov;
July 23, 2014 23:32
478
},
479
"factor.js": function (exports, module, require) {
480
Factor = function(elements) {
481
this.levels = [];
482
this.factors = [];
483
for (var i = 0; i < elements.length; i++) {
484
if ((index = this.levels.indexOf(elements[i])) != -1) {
485
this.factors.push(index);
486
} else {
487
this.factors.push(this.levels.length);
488
this.levels.push(elements[i]);
489
}
490
}
491
};
492
493
Factor.prototype.group = function(g) {
494
var indices = [];
495
var i = -1;
496
while ((i = this.factors.indexOf(g, i + 1)) != -1) {
497
indices.push(i);
498
}
499
return indices;
500
};
501
502
Factor.prototype.length = function() {
503
return this.factors.length;
504
};
505
506
Factor.prototype.groups = function() {
507
return this.levels.length;
508
};
509
510
module.exports.Factor = Factor;
June 25, 2014 00:08
511
},
July 30, 2014 19:50
512
"matrix.js": function (exports, module, require) {
513
Matrix = function(elements) {
514
this.elements = elements;
515
};
516
517
Matrix.prototype.rows = function() {
518
return this.elements.length;
519
};
520
521
Matrix.prototype.cols = function() {
522
return this.elements[0].length;
523
};
524
525
Matrix.prototype.dot = function(m) {
526
var result = [];
527
for (var i = 0; i < this.rows(); i++) {
528
result[i] = [];
529
for (var j = 0; j < m.cols(); j++) {
530
var sum = 0;
531
for (var k = 0; k < this.cols(); k++) {
532
sum += this.elements[i][k] * m.elements[k][j];
533
}
534
result[i][j] = sum;
535
}
536
}
537
return new Matrix(result);
538
};
539
540
module.exports.Matrix = Matrix;
541
},
June 25, 2014 00:08
542
"misc.js": function (exports, module, require) {
543
var numeric = require('./numeric');
544
545
Misc = function() {};
546
July 30, 2014 19:50
547
/*
548
* error function
549
*/
550
551
Misc.erf = function(z) {
552
var term;
553
var sum = 0;
554
var n = 0;
555
do {
556
term = Math.pow(-1, n) * Math.pow(z, 2 * n + 1) / this.fac(n) / (2 * n + 1);
557
sum = sum + term;
558
n++;
559
} while (Math.abs(term) > 0.000000000001);
560
return sum * 2 / Math.sqrt(Math.PI);
561
};
562
June 25, 2014 00:08
563
/*
564
* gamma function
565
*/
566
June 30, 2014 20:59
567
Misc.gamma = function (n) {
June 25, 2014 00:08
568
var p = [
July 30, 2014 19:50
569
0.99999999999980993,
570
676.5203681218851,
571
-1259.1392167224028,
572
771.32342877765313,
573
-176.61502916214059,
574
12.507343278686905,
575
-0.13857109526572012,
576
9.9843695780195716e-6,
577
1.5056327351493116e-7
578
];
June 25, 2014 00:08
579
var g = 7;
580
if (n < 0.5) {
581
return Math.PI / (Math.sin(Math.PI * n) * this.gamma(1 - n));
582
}
583
n -= 1;
584
var a = p[0];
585
var t = n + g + 0.5;
586
for (var i = 1; i < p.length; i++) {
587
a += p[i] / (n + i);
588
}
589
return Math.sqrt(2 * Math.PI) * Math.pow(t, n + 0.5) * Math.exp(-t) * a;
590
};
591
592
/*
593
* beta function
594
*/
595
June 30, 2014 20:59
596
Misc.beta = function (x, y) {
June 25, 2014 00:08
597
return this.gamma(x) * this.gamma(y) / this.gamma(x + y);
598
};
599
600
/*
601
* incomplete beta function
602
*/
603
June 30, 2014 20:59
604
Misc.ibeta = function (x, a, b) {
June 25, 2014 00:08
605
return numeric.Numeric.adaptiveSimpson(function(y) {
606
return Math.pow(y, a - 1) * Math.pow(1 - y, b - 1);
June 29, 2014 00:34
607
}, 0, x, 0.000000000001, 10);
June 25, 2014 00:08
608
};
609
610
/*
611
* regularized incomplete beta function
612
*/
613
614
June 30, 2014 20:59
615
Misc.rbeta = function (x, a, b) {
June 25, 2014 00:08
616
return this.ibeta(x, a, b) / this.beta(a, b);
617
};
618
619
/*
620
* factorial
621
*/
622
June 30, 2014 20:59
623
Misc.fac = function(n) {
June 25, 2014 00:08
624
var result = 1;
625
for (var i = 2; i <= n; i++) {
626
result = result * i;
627
}
628
return result;
629
}
630
631
module.exports.Misc = Misc;
632
},
October 31, 2014 11:02
633
"nonparametric.js": function (exports, module, require) {
634
var vector = require('./vector');
635
var distributions = require('./distributions');
636
637
Nonparametric = function() {};
638
639
/*
640
* Two-sample Kolmogorov-Smirnov test
641
*/
642
643
Nonparametric.kolmogorovSmirnov = function(x, y) {
644
var all = new vector.Vector(x.elements.concat(y.elements)).sort();
645
var ecdfx = x.ecdf(all);
646
var ecdfy = y.ecdf(all);
647
var d = ecdfy.subtract(ecdfx).abs().max();
January 24, 2015 11:23
648
var n = (x.length() * y.length()) / (x.length() + y.length());
649
var ks = Math.sqrt(n) * d;
650
var p = 1 - new distributions.Kolmogorov().distr(ks);
651
652
return {
653
"d": d,
654
"ks": ks,
655
"p": p
656
};
October 31, 2014 11:02
657
}
658
659
module.exports.Nonparametric = Nonparametric;
660
},
July 30, 2014 19:50
661
"normality.js": function (exports, module, require) {
662
var matrix = require('./matrix');
663
var vector = require('./vector');
664
var distributions = require('./distributions');
665
666
Normality = function() {};
667
668
Normality.shapiroWilk = function(x) {
669
result = {};
670
var xx = x.sort();
671
var mean = x.mean();
672
var n = x.length();
673
var u = 1 / Math.sqrt(n);
674
675
// m
676
677
var sn = new distributions.StandardNormal();
678
var m = new vector.Vector([]);
679
for (var i = 1; i <= n; i++) {
680
m.push(sn.inverse((i - 3/8) / (n + 1/4)));
681
}
682
683
// c
684
685
var md = m.dot(m);
686
var c = m.multiply(1 / Math.sqrt(md));
687
688
// a
689
690
var an = -2.706056 * Math.pow(u, 5) + 4.434685 * Math.pow(u, 4) - 2.071190 * Math.pow(u, 3) - 0.147981 * Math.pow(u, 2) + 0.221157 * u + c.elements[n - 1];
691
var ann = -3.582633 * Math.pow(u, 5) + 5.682633 * Math.pow(u, 4) - 1.752461 * Math.pow(u, 3) - 0.293762 * Math.pow(u, 2) + 0.042981 * u + c.elements[n - 2];
692
693
var phi;
694
695
if (n > 5) {
696
phi = (md - 2 * Math.pow(m.elements[n - 1], 2) - 2 * Math.pow(m.elements[n - 2], 2)) / (1 - 2 * Math.pow(an, 2) - 2 * Math.pow(ann, 2));
697
} else {
698
phi = (md - 2 * Math.pow(m.elements[n - 1], 2)) / (1 - 2 * Math.pow(an, 2));
699
}
700
701
var a = new vector.Vector([]);
702
if (n > 5) {
703
a.push(-an);
704
a.push(-ann);
705
for (var i = 2; i < n - 2; i++) {
706
a.push(m.elements[i] * Math.pow(phi, -1/2));
707
}
708
a.push(ann);
709
a.push(an);
710
} else {
711
a.push(-an);
712
for (var i = 1; i < n - 1; i++) {
713
a.push(m.elements[i] * Math.pow(phi, -1/2));
714
}
715
a.push(an);
716
}
717
718
// w
719
720
result.w = Math.pow(a.multiply(xx).sum(), 2) / xx.ss();
721
722
// p
723
724
var g, mu, sigma;
725
726
if (n < 12) {
727
var gamma = 0.459 * n - 2.273;
728
g = - Math.log(gamma - Math.log(1 - result.w));
729
mu = -0.0006714 * Math.pow(n, 3) + 0.025054 * Math.pow(n, 2) - 0.39978 * n + 0.5440;
730
sigma = Math.exp(-0.0020322 * Math.pow(n, 3) + 0.062767 * Math.pow(n, 2) - 0.77857 * n + 1.3822);
731
} else {
732
var u = Math.log(n);
733
g = Math.log(1 - result.w);
734
mu = 0.0038915 * Math.pow(u, 3) - 0.083751 * Math.pow(u, 2) - 0.31082 * u - 1.5851;
735
sigma = Math.exp(0.0030302 * Math.pow(u, 2) - 0.082676 * u - 0.4803);
736
}
737
738
var z = (g - mu) / sigma;
739
var norm = new distributions.StandardNormal();
740
result.p = 1 - norm.distr(z);
741
742
return result;
743
};
744
745
module.exports.Normality = Normality;
746
},
June 25, 2014 00:08
747
"numeric.js": function (exports, module, require) {
748
Numeric = function() {};
749
750
/*
751
* adaptive Simpson
752
*/
753
June 30, 2014 20:59
754
Numeric._adaptive = function(f, a, b, eps, s, fa, fb, fc, depth) {
June 25, 2014 00:08
755
var c = (a + b) / 2;
756
var h = b - a;
757
var d = (a + c) / 2;
758
var e = (c + b) / 2;
759
var fd = f(d);
760
var fe = f(e);
761
var left = (h / 12) * (fa + 4 * fd + fc);
762
var right = (h / 12) * (fc + 4* fe + fb);
763
var s2 = left + right;
764
if (depth <= 0 || Math.abs(s2 - s) <= 15 * eps) {
765
return s2 + (s2 - s) / 15;
766
} else {
767
return this._adaptive(f, a, c, eps / 2, left, fa, fc, fd, depth - 1)
768
+ this._adaptive(f, c, b, eps / 2, right, fc, fb, fe, depth - 1);
769
}
770
}
771
June 30, 2014 20:59
772
Numeric.adaptiveSimpson = function(f, a, b, eps, depth) {
June 25, 2014 00:08
773
var c = (a + b) / 2;
774
var h = b - a;
775
var fa = f(a);
776
var fb = f(b);
777
var fc = f(c);
778
var s = (h / 6) * (fa + 4 * fc + fb);
779
return this._adaptive(f, a, b, eps, s, fa, fb, fc, depth);
780
}
781
June 30, 2014 20:59
782
/*
783
* root finding: bisection
784
*/
785
January 24, 2015 11:23
786
Numeric.bisection = function(f, a, b, eps) {
787
eps = typeof eps !== "undefined" ? eps : 1e-9;
788
while (Math.abs(a - b) > eps) {
June 30, 2014 20:59
789
if (f(a) * f((a + b) / 2) < 0) {
790
b = (a + b) / 2;
791
} else {
792
a = (a + b) / 2;
793
}
794
}
795
return (a + b) / 2;
796
}
797
798
/*
799
* root finding: secant
800
*/
801
January 24, 2015 11:23
802
Numeric.secant = function(f, a, b, eps) {
803
eps = typeof eps !== "undefined" ? eps : 1e-9;
June 30, 2014 20:59
804
var q = [a, b];
January 24, 2015 11:23
805
while (Math.abs(q[0] - q[1]) > eps) {
June 30, 2014 20:59
806
q.push((q[0] * f(q[1]) - q[1] * f(q[0])) / (f(q[1]) - f(q[0])));
807
q.shift();
808
}
809
return (q[0] + q[1]) / 2;
810
}
June 25, 2014 00:08
811
812
module.exports.Numeric = Numeric;
813
},
October 22, 2014 07:28
814
"power.js": function (exports, module, require) {
815
var distributions = require('./distributions');
816
817
Power = function() {};
818
January 24, 2015 11:23
819
/*
820
* Sample size calculation
821
*/
822
October 22, 2014 07:28
823
Power.sampleSize = function(a, power, sd, effect) {
824
var n = new distributions.Normal(0, 1);
825
return (2 * Math.pow(n.inverse(1 - a / 2) + n.inverse(power), 2) * Math.pow(sd, 2)) / Math.pow(effect, 2);
826
};
827
828
module.exports.Power = Power;
829
},
June 25, 2014 00:08
830
"regression.js": function (exports, module, require) {
June 30, 2014 20:59
831
var distributions = require('./distributions');
832
June 25, 2014 00:08
833
Regression = function() {};
834
835
/*
836
* simple linear regression
837
*/
838
839
Regression.linear = function(x, y) {
June 30, 2014 20:59
840
var result = {};
841
result.n = x.length();
June 25, 2014 00:08
842
843
// means
844
June 30, 2014 20:59
845
var mx = x.mean();
846
var my = y.mean();
June 25, 2014 00:08
847
848
// parameters
849
June 30, 2014 20:59
850
var rx = x.add(-mx);
851
var ry = y.add(-my);
June 25, 2014 00:08
852
853
var ssxx = rx.pow(2).sum();
854
var ssyy = ry.pow(2).sum();
855
var ssxy = rx.multiply(ry).sum();
856
June 30, 2014 20:59
857
result.slope = ssxy / ssxx;
858
result.intercept = my - result.slope * mx;
June 25, 2014 00:08
859
860
// sum of squared residuals
861
June 30, 2014 20:59
862
var ssr = y.add(x.multiply(result.slope).add(result.intercept).multiply(-1)).pow(2).sum();
June 25, 2014 00:08
863
864
// residual standard error
865
June 30, 2014 20:59
866
result.rse = Math.sqrt(ssr / (result.n - 2))
June 25, 2014 00:08
867
868
// slope
869
June 30, 2014 20:59
870
var tdistr = new distributions.T(result.n - 2);
June 25, 2014 00:08
871
June 30, 2014 20:59
872
result.slope_se = result.rse / Math.sqrt(ssxx);
873
result.slope_t = result.slope / result.slope_se;
874
result.slope_p = 2 * (1 - tdistr.distr(Math.abs(result.slope_t)));
June 25, 2014 00:08
875
876
// intercept
877
June 30, 2014 20:59
878
result.intercept_se = result.rse / Math.sqrt(ssxx) / Math.sqrt(result.n) * Math.sqrt(x.pow(2).sum());
879
result.intercept_t = result.intercept / result.intercept_se;
880
result.intercept_p = 2 * (1 - tdistr.distr(Math.abs(result.intercept_t)));
June 25, 2014 00:08
881
882
// R-squared
883
June 30, 2014 20:59
884
result.rs = Math.pow(ssxy, 2) / (ssxx * ssyy);
June 25, 2014 00:08
885
June 30, 2014 20:59
886
return result;
June 25, 2014 00:08
887
};
888
889
module.exports.Regression = Regression;
890
},
891
"t.js": function (exports, module, require) {
892
var vector = require('./vector');
June 30, 2014 20:59
893
var distributions = require('./distributions');
894
895
StudentT = function(){};
June 25, 2014 00:08
896
June 30, 2014 20:59
897
StudentT.test = function(first, second) {
June 25, 2014 00:08
898
if (second instanceof vector.Vector) {
June 30, 2014 20:59
899
return this._twosample(first, second);
June 25, 2014 00:08
900
} else {
June 30, 2014 20:59
901
return this._onesample(first, second);
June 25, 2014 00:08
902
}
June 30, 2014 20:59
903
};
June 25, 2014 00:08
904
905
/*
906
* two-sample Student's t-test
907
*/
908
June 30, 2014 20:59
909
StudentT._twosample = function(first, second) {
910
var result = {};
July 23, 2014 23:32
911
result.se = Math.sqrt((first.variance() / first.length()) + (second.variance() / second.length()));
912
result.t = (first.mean() - second.mean()) / result.se;
913
result.df = first.length() + second.length() - 2;
June 30, 2014 20:59
914
var tdistr = new distributions.T(result.df);
915
result.p = 2 * (1 - tdistr.distr(Math.abs(result.t)));
916
return result;
June 25, 2014 00:08
917
};
918
919
/*
920
* one-sample Student's t-test
921
*/
922
June 30, 2014 20:59
923
StudentT._onesample = function(sample, mu) {
924
var result = {};
925
result.sample = sample;
926
result.mu = mu;
927
result.se = Math.sqrt(result.sample.variance()) / Math.sqrt(result.sample.length());
928
result.t = (result.sample.mean() - result.mu) / result.se;
929
result.df = result.sample.length() - 1;
930
var tdistr = new distributions.T(result.df);
931
result.p = 2 * (1 - tdistr.distr(Math.abs(result.t)));
932
return result;
June 25, 2014 00:08
933
};
934
935
module.exports.StudentT = StudentT;
936
},
937
"vector.js": function (exports, module, require) {
938
Vector = function(elements) {
939
this.elements = elements;
940
};
941
942
Vector.prototype.push = function(value) {
943
this.elements.push(value);
944
};
945
January 24, 2015 11:23
946
Vector.prototype.map = function(fun) {
947
return new Vector(this.elements.map(fun));
948
};
949
June 25, 2014 00:08
950
Vector.prototype.length = function() {
951
return this.elements.length;
952
};
953
October 31, 2014 11:02
954
Vector.prototype.concat = function(x) {
955
return new Vector(this.elements.slice(0).concat(x.elements.slice(0)));
956
};
957
958
Vector.prototype.abs = function() {
959
var values = [];
960
for (var i = 0; i < this.elements.length; i++) {
961
values.push(Math.abs(this.elements[i]));
962
}
963
return new Vector(values);
964
};
965
July 30, 2014 19:50
966
Vector.prototype.dot = function(v) {
967
var result = 0;
968
for (var i = 0; i < this.length(); i++) {
969
result = result + this.elements[i] * v.elements[i];
970
}
971
return result;
972
};
973
June 25, 2014 00:08
974
Vector.prototype.sum = function() {
975
var sum = 0;
976
for (var i = 0, n = this.elements.length; i < n; ++i) {
977
sum += this.elements[i];
978
}
979
return sum;
980
};
981
August 3, 2014 16:32
982
Vector.prototype.log = function() {
983
var result = new Vector(this.elements.slice(0));
984
for (var i = 0, n = this.elements.length; i < n; ++i) {
985
result.elements[i] = Math.log(result.elements[i]);
986
}
987
return result;
988
};
989
June 25, 2014 00:08
990
Vector.prototype.add = function(term) {
991
var result = new Vector(this.elements.slice(0));
992
if (term instanceof Vector) {
993
for (var i = 0, n = result.elements.length; i < n; ++i) {
994
result.elements[i] += term.elements[i];
995
}
996
} else {
997
for (var i = 0, n = result.elements.length; i < n; ++i) {
998
result.elements[i] += term;
999
}
1000
}
1001
return result;
1002
};
1003
October 31, 2014 11:02
1004
Vector.prototype.subtract = function(term) {
1005
return this.add(term.multiply(-1));
1006
};
1007
June 25, 2014 00:08
1008
Vector.prototype.multiply = function(factor) {
1009
var result = new Vector(this.elements.slice(0));
1010
if (factor instanceof Vector) {
1011
for (var i = 0, n = result.elements.length; i < n; ++i) {
1012
result.elements[i] = result.elements[i] * factor.elements[i];
1013
}
1014
} else {
1015
for (var i = 0, n = result.elements.length; i < n; ++i) {
1016
result.elements[i] = result.elements[i] * factor;
1017
}
1018
}
1019
return result;
1020
};
1021
1022
Vector.prototype.pow = function(p) {
1023
var result = new Vector(this.elements.slice(0));
1024
if (p instanceof Vector) {
1025
for (var i = 0, n = result.elements.length; i < n; ++i) {
1026
result.elements[i] = Math.pow(result.elements[i], p.elements[i]);
1027
}
1028
} else {
1029
for (var i = 0, n = result.elements.length; i < n; ++i) {
1030
result.elements[i] = Math.pow(result.elements[i], p);
1031
}
1032
}
1033
return result;
1034
};
1035
1036
Vector.prototype.mean = function() {
1037
var sum = 0;
1038
for (var i = 0, n = this.elements.length; i < n; ++i) {
1039
sum += this.elements[i];
1040
}
1041
return sum / this.elements.length;
1042
};
1043
March 4, 2015 18:49
1044
Vector.prototype.median = function() {
1045
var sorted = this.sort();
1046
var middle = Math.floor(sorted.length() / 2);
1047
if (sorted.length() % 2) {
1048
return sorted.elements[middle];
1049
} else {
1050
return (sorted.elements[middle - 1] + sorted.elements[middle]) / 2;
1051
}
1052
};
1053
August 3, 2014 16:32
1054
Vector.prototype.geomean = function() {
1055
return Math.exp(this.log().sum() / this.elements.length);
1056
};
1057
June 25, 2014 00:08
1058
Vector.prototype.sortElements = function() {
1059
var sorted = this.elements.slice(0);
1060
for (var i = 0, j, tmp; i < sorted.length; ++i) {
1061
tmp = sorted[i];
1062
for (j = i - 1; j >= 0 && sorted[j] > tmp; --j) {
1063
sorted[j + 1] = sorted[j];
1064
}
1065
sorted[j + 1] = tmp;
1066
}
1067
return sorted;
1068
};
1069
October 31, 2014 11:02
1070
Vector.prototype._ecdf = function(x) {
1071
var sorted = this.sortElements();
1072
var count = 0;
1073
for (var i = 0; i < sorted.length && sorted[i] <= x; i++) {
1074
count++;
1075
}
1076
return count / sorted.length;
1077
};
1078
1079
Vector.prototype.ecdf = function(arg) {
1080
if (arg instanceof Vector) {
1081
var result = new Vector([]);
1082
for (var i = 0; i < arg.length(); i++) {
1083
result.push(this._ecdf(arg.elements[i]));
1084
}
1085
return result;
1086
} else {
1087
return this._ecdf(arg);
1088
}
1089
};
1090
June 25, 2014 00:08
1091
Vector.prototype.sort = function() {
1092
return new Vector(this.sortElements());
1093
};
1094
1095
Vector.prototype.min = function() {
1096
return this.sortElements()[0];
1097
};
1098
1099
Vector.prototype.max = function() {
1100
return this.sortElements().pop();
1101
};
1102
1103
Vector.prototype.toString = function() {
1104
return "[" + this.elements.join(", ") + "]";
1105
};
1106
1107
/*
1108
* unbiased sample variance
1109
*/
1110
June 25, 2014 00:08
1111
Vector.prototype.variance = function() {
July 23, 2014 23:32
1112
return this.ss() / (this.elements.length - 1);
1113
};
1114
1115
/*
1116
* biased sample variance
1117
*/
1118
1119
Vector.prototype.biasedVariance = function() {
1120
return this.ss() / this.elements.length;
1121
};
1122
1123
/*
1124
* corrected sample standard deviation
1125
*/
1126
1127
Vector.prototype.sd = function() {
1128
return Math.sqrt(this.variance());
1129
};
1130
1131
/*
1132
* uncorrected sample standard deviation
1133
*/
1134
1135
Vector.prototype.uncorrectedSd = function() {
1136
return Math.sqrt(this.biasedVariance());
1137
};
1138
1139
/*
1140
* standard error of the mean
1141
*/
1142
1143
Vector.prototype.sem = function() {
1144
return this.sd() / Math.sqrt(this.elements.length);
1145
};
1146
1147
/*
1148
* total sum of squares
1149
*/
1150
July 23, 2014 23:32
1151
Vector.prototype.ss = function() {
June 25, 2014 00:08
1152
var m = this.mean();
1153
var sum = 0;
1154
for (var i = 0, n = this.elements.length; i < n; ++i) {
1155
sum += Math.pow(this.elements[i] - m, 2);
1156
}
July 23, 2014 23:32
1157
return sum;
1158
};
1159
1160
/*
1161
* residuals
1162
*/
1163
July 23, 2014 23:32
1164
Vector.prototype.res = function() {
July 30, 2014 19:50
1165
return this.add(-this.mean());
June 25, 2014 00:08
1166
};
1167
July 30, 2014 19:50
1168
Vector.prototype.kurtosis = function() {
1169
return this.res().pow(4).mean() / Math.pow(this.res().pow(2).mean(), 2);
1170
};
1171
1172
Vector.prototype.skewness = function() {
1173
return this.res().pow(3).mean() / Math.pow(this.res().pow(2).mean(), 3 / 2);
1174
};
1175
June 25, 2014 00:08
1176
Sequence.prototype = new Vector();
1177
1178
Sequence.prototype.constructor = Sequence;
1179
1180
function Sequence(min, max, step) {
1181
this.elements = [];
1182
for (var i = min; i <= max; i = i + step) {
1183
this.elements.push(i);
1184
}
1185
};
1186
1187
module.exports.Vector = Vector;
October 31, 2014 11:02
1188
module.exports.Sequence = Sequence;
June 25, 2014 00:08
1189
}
1190
}
1191
}
1192
})("jerzy/jerzy");