1 | import {Math} from 'angular2/src/facade/math';
|
2 |
|
3 | export 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 |
|
11 | samples.forEach(x => total += x);
|
12 | return total / samples.length;
|
13 | }
|
14 |
|
15 | static calculateStandardDeviation(samples: number[], mean) {
|
16 | var deviation = 0;
|
17 |
|
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 |
|
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 | }
|