2016-08-03 18:00:07 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2015-02-11 13:13:49 -05:00
|
|
|
export class Statistic {
|
2016-08-26 19:34:08 -04:00
|
|
|
static calculateCoefficientOfVariation(sample: number[], mean: number) {
|
2015-02-11 13:13:49 -05:00
|
|
|
return Statistic.calculateStandardDeviation(sample, mean) / mean * 100;
|
|
|
|
}
|
|
|
|
|
2015-10-09 01:44:58 -04:00
|
|
|
static calculateMean(samples: number[]) {
|
2016-11-12 08:08:58 -05:00
|
|
|
let total = 0;
|
2015-10-07 12:09:43 -04:00
|
|
|
// TODO: use reduce
|
|
|
|
samples.forEach(x => total += x);
|
|
|
|
return total / samples.length;
|
2015-02-11 13:13:49 -05:00
|
|
|
}
|
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
static calculateStandardDeviation(samples: number[], mean: number) {
|
2016-11-12 08:08:58 -05:00
|
|
|
let deviation = 0;
|
2015-10-07 12:09:43 -04:00
|
|
|
// TODO: use reduce
|
|
|
|
samples.forEach(x => deviation += Math.pow(x - mean, 2));
|
|
|
|
deviation = deviation / (samples.length);
|
2015-02-11 13:13:49 -05:00
|
|
|
deviation = Math.sqrt(deviation);
|
|
|
|
return deviation;
|
|
|
|
}
|
|
|
|
|
2016-08-03 18:00:07 -04:00
|
|
|
static calculateRegressionSlope(
|
|
|
|
xValues: number[], xMean: number, yValues: number[], yMean: number) {
|
2015-02-11 13:13:49 -05:00
|
|
|
// See http://en.wikipedia.org/wiki/Simple_linear_regression
|
2016-11-12 08:08:58 -05:00
|
|
|
let dividendSum = 0;
|
|
|
|
let divisorSum = 0;
|
|
|
|
for (let i = 0; i < xValues.length; i++) {
|
2015-02-11 13:13:49 -05:00
|
|
|
dividendSum += (xValues[i] - xMean) * (yValues[i] - yMean);
|
|
|
|
divisorSum += Math.pow(xValues[i] - xMean, 2);
|
|
|
|
}
|
|
|
|
return dividendSum / divisorSum;
|
|
|
|
}
|
|
|
|
}
|