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

Commit

Permalink
feat(benchmark): add statistical functions to calculate confidence in…
Browse files Browse the repository at this point in the history
…terval
  • Loading branch information
jeffbcross committed Jun 17, 2014
1 parent 460f6dc commit 26f7def
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
34 changes: 34 additions & 0 deletions benchmark/web/bp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
var bp = window.bp = {};
bp.Statistics = {
//Taken from z-table where confidence is 95%
criticalValue: 1.96
};
bp.steps = window.benchmarkSteps = [];
bp.runState = {
numSamples: 20,
Expand All @@ -9,6 +13,36 @@ bp.runState = {
timesPerAction: {}
};

bp.Statistics.getConfidenceRange = function(mean, confidenceInterval) {
return [
mean - confidenceInterval,
mean + confidenceInterval
];
};

bp.Statistics.calculateConfidenceInterval = function(standardDeviation, sampleSize) {
var standardError = standardDeviation / Math.sqrt(sampleSize);
var marginOfError = bp.Statistics.criticalValue * standardError;
marginOfError = Math.round(marginOfError * 100) / 100;
return marginOfError;
};

bp.Statistics.calculateStandardDeviation = function(sample) {
var mean = 0;
var deviation = 0;
sample.forEach(function(x) {
mean += x;
});
mean = mean / sample.length;

sample.forEach(function(x) {
deviation += Math.pow(x - mean, 2);
});
deviation = deviation / sample.length;
deviation = Math.sqrt(deviation);
return deviation;
};

bp.setIterations = function (iterations) {
bp.runState.iterations = iterations;
};
Expand Down
24 changes: 24 additions & 0 deletions benchmark/web/bp.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ describe('bp', function() {
};
});

describe('.Statistics', function() {
describe('.calculateConfidenceInterval()', function() {
it('should provide the correct confidence interval', function() {
expect(bp.Statistics.calculateConfidenceInterval(30, 1000)).toBe(1.86);
});
});


describe('.calculateStandardDeviation()', function() {
it('should provide the correct standardDeviation for the provided sample', function() {
expect(bp.Statistics.calculateStandardDeviation([
2,4,4,4,5,5,7,9
])).toBe(2);
});
});


describe('.getConfidenceRange()', function() {
it('should return an array of low and high confidence range', function() {
expect(bp.Statistics.getConfidenceRange(100, 1.5)).toEqual([98.5,101.5]);
});
});
});

describe('.loopBenchmark()', function() {
var runAllTestsSpy, btn;
beforeEach(function() {
Expand Down

0 comments on commit 26f7def

Please sign in to comment.