UNPKG

1.13 kBPlain TextView Raw
1import {Math} from 'angular2/src/facade/math';
2
3export class Statistic {
4 static calculateCoefficientOfVariation(sample, mean) {
5 return Statistic.calculateStandardDeviation(sample, mean) / mean * 100;
6 }
7
8 static calculateMean(samples: number[]) {
9 var total = 0;
10 // TODO: use reduce
11 samples.forEach(x => total += x);
12 return total / samples.length;
13 }
14
15 static calculateStandardDeviation(samples: number[], mean) {
16 var deviation = 0;
17 // TODO: use reduce
18 samples.forEach(x => deviation += Math.pow(x - mean, 2));
19 deviation = deviation / (samples.length);
20 deviation = Math.sqrt(deviation);
21 return deviation;
22 }
23
24 static calculateRegressionSlope(xValues: number[], xMean: number, yValues: number[],
25 yMean: number) {
26 // See http://en.wikipedia.org/wiki/Simple_linear_regression
27 var dividendSum = 0;
28 var divisorSum = 0;
29 for (var i = 0; i < xValues.length; i++) {
30 dividendSum += (xValues[i] - xMean) * (yValues[i] - yMean);
31 divisorSum += Math.pow(xValues[i] - xMean, 2);
32 }
33 return dividendSum / divisorSum;
34 }
35}