Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 26f7def

Browse files
committed
feat(benchmark): add statistical functions to calculate confidence interval
1 parent 460f6dc commit 26f7def

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

benchmark/web/bp.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
var bp = window.bp = {};
2+
bp.Statistics = {
3+
//Taken from z-table where confidence is 95%
4+
criticalValue: 1.96
5+
};
26
bp.steps = window.benchmarkSteps = [];
37
bp.runState = {
48
numSamples: 20,
@@ -9,6 +13,36 @@ bp.runState = {
913
timesPerAction: {}
1014
};
1115

16+
bp.Statistics.getConfidenceRange = function(mean, confidenceInterval) {
17+
return [
18+
mean - confidenceInterval,
19+
mean + confidenceInterval
20+
];
21+
};
22+
23+
bp.Statistics.calculateConfidenceInterval = function(standardDeviation, sampleSize) {
24+
var standardError = standardDeviation / Math.sqrt(sampleSize);
25+
var marginOfError = bp.Statistics.criticalValue * standardError;
26+
marginOfError = Math.round(marginOfError * 100) / 100;
27+
return marginOfError;
28+
};
29+
30+
bp.Statistics.calculateStandardDeviation = function(sample) {
31+
var mean = 0;
32+
var deviation = 0;
33+
sample.forEach(function(x) {
34+
mean += x;
35+
});
36+
mean = mean / sample.length;
37+
38+
sample.forEach(function(x) {
39+
deviation += Math.pow(x - mean, 2);
40+
});
41+
deviation = deviation / sample.length;
42+
deviation = Math.sqrt(deviation);
43+
return deviation;
44+
};
45+
1246
bp.setIterations = function (iterations) {
1347
bp.runState.iterations = iterations;
1448
};

benchmark/web/bp.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,30 @@ describe('bp', function() {
3434
};
3535
});
3636

37+
describe('.Statistics', function() {
38+
describe('.calculateConfidenceInterval()', function() {
39+
it('should provide the correct confidence interval', function() {
40+
expect(bp.Statistics.calculateConfidenceInterval(30, 1000)).toBe(1.86);
41+
});
42+
});
43+
44+
45+
describe('.calculateStandardDeviation()', function() {
46+
it('should provide the correct standardDeviation for the provided sample', function() {
47+
expect(bp.Statistics.calculateStandardDeviation([
48+
2,4,4,4,5,5,7,9
49+
])).toBe(2);
50+
});
51+
});
52+
53+
54+
describe('.getConfidenceRange()', function() {
55+
it('should return an array of low and high confidence range', function() {
56+
expect(bp.Statistics.getConfidenceRange(100, 1.5)).toEqual([98.5,101.5]);
57+
});
58+
});
59+
});
60+
3761
describe('.loopBenchmark()', function() {
3862
var runAllTestsSpy, btn;
3963
beforeEach(function() {

0 commit comments

Comments
 (0)