Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added
sum
, mean
, stdev
computations and assertions
- Loading branch information
1 parent
76ae30a
commit 903d52e
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/*! | ||
* Chai Stats - calculation utilities | ||
* Copyright (c) 2012 Jake Luer <jake@alogicalparadox.com> | ||
* MIT Licenced | ||
*/ | ||
|
||
/** | ||
* # sum | ||
* | ||
* Returns the sum of a given array of numbers. | ||
* | ||
* @param {Array} numbers to sum | ||
* @returns sum | ||
*/ | ||
|
||
exports.sum = function (nums) { | ||
var res = 0; | ||
for (var i = 0; i < nums.length; i++) | ||
res += nums[i]; | ||
return res; | ||
}; | ||
|
||
/** | ||
* # mean | ||
* | ||
* Returns the mean (average) of a given array of numbers. | ||
* | ||
* @param {Array} numbers to average | ||
* @returs mean | ||
*/ | ||
|
||
exports.mean = function (nums) { | ||
var sum = exports.sum(nums); | ||
return sum / nums.length; | ||
}; | ||
|
||
/** | ||
* # sdeviation | ||
* | ||
* Returns the standard deviation of a given array of numbers. | ||
* | ||
* @param {Array} numbers for stdev | ||
* @return standard deviation | ||
*/ | ||
|
||
exports.sdeviation = function (nums) { | ||
var devs = [] | ||
, mean = exports.mean(nums); | ||
for (var i = 0; i < nums.length; i++) | ||
devs.push(nums[i] - mean); | ||
for (var d = 0; d < devs.length; d++) | ||
devs[d] = Math.pow(devs[d], 2); | ||
var davg = exports.sum(devs) / (devs.length - 1); | ||
return Math.sqrt(davg); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var calc = require('./calc'); | ||
|
||
module.exports = function (chai, _) { | ||
|
||
_.addProperty(chai.Assertion, 'sum', function () { | ||
var obj = _.flag(this, 'object'); | ||
new chai.Assertion(obj).to.be.instanceOf(Array); | ||
_.flag(this, 'object', calc.sum(obj)); | ||
}); | ||
|
||
_.addProperty(chai.Assertion, 'mean', function () { | ||
var obj = _.flag(this, 'object'); | ||
new chai.Assertion(obj).to.be.instanceOf(Array); | ||
_.flag(this, 'object', calc.mean(obj)); | ||
}); | ||
|
||
function deviation () { | ||
var obj = _.flag(this, 'object'); | ||
new chai.Assertion(obj).to.be.instanceOf(Array); | ||
_.flag(this, 'object', calc.sdeviation(obj)); | ||
} | ||
|
||
_.addProperty(chai.Assertion, 'stdev', deviation); | ||
_.addProperty(chai.Assertion, 'deviation', deviation); | ||
|
||
}; |