This repository was archived by the owner on Feb 22, 2018. It is now read-only.
File tree 2 files changed +58
-0
lines changed 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 1
1
var bp = window . bp = { } ;
2
+ bp . Statistics = {
3
+ //Taken from z-table where confidence is 95%
4
+ criticalValue : 1.96
5
+ } ;
2
6
bp . steps = window . benchmarkSteps = [ ] ;
3
7
bp . runState = {
4
8
numSamples : 20 ,
@@ -9,6 +13,36 @@ bp.runState = {
9
13
timesPerAction : { }
10
14
} ;
11
15
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
+
12
46
bp . setIterations = function ( iterations ) {
13
47
bp . runState . iterations = iterations ;
14
48
} ;
Original file line number Diff line number Diff line change @@ -34,6 +34,30 @@ describe('bp', function() {
34
34
} ;
35
35
} ) ;
36
36
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
+
37
61
describe ( '.loopBenchmark()' , function ( ) {
38
62
var runAllTestsSpy , btn ;
39
63
beforeEach ( function ( ) {
You can’t perform that action at this time.
0 commit comments