Skip to content

Commit

Permalink
Added standard deviation and variance
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanvamsi3 committed Oct 5, 2017
1 parent c6124ba commit b9a356b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var moreNumbers = [1, 2, 1, 3];
var mean = stats.mean(numbers); // returns 2
var median = stats.median(numbers); // returns 2
var modes = stats.mode(moreNumbers); // returns [1]
var standardDeviation = stats.standardDeviation(numbers); // returns 0.816496580927726
var variance = stats.variance(numbers); // returns 0.6666666666666666
```

### Tests
Expand All @@ -26,10 +28,7 @@ var modes = stats.mode(moreNumbers); // returns [1]
`npm test`

### To do
- [ ] Standard Deviation


![alt text](https://www.mathsisfun.com/data/images/standard-deviation-formula.gif "Standard Deviation")
- [ ] :thinking:

### Like it? :see_no_evil:

Expand Down
27 changes: 26 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,33 @@ var mode = numbers => {
return modes;
}

var standardDeviation = numbers => {
if (!Array.isArray(numbers)) {
return null;
}

return Math.sqrt(variance(numbers));
}

var variance = numbers => {
if (!Array.isArray(numbers)) {
return null;
}
var initialMean = mean(numbers);
var varianceNumbersArray = [], i;
for(i = 0; i < numbers.length; i += 1) {
var varianceNumber = numbers[i] - initialMean;
varianceNumbersArray.push(varianceNumber * varianceNumber);
}
var variance = mean(varianceNumbersArray);

return variance;
}

module.exports = {
mean: mean,
median: median,
mode: mode
mode: mode,
standardDeviation: standardDeviation,
variance: variance
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "math-stats",
"version": "0.1.4",
"version": "0.2.0",
"description": "Mean, Median, Mode",
"main": "index.js",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@ describe('#stats', function() {
var result = stats.mean([1, 2, 3]);
expect(result).to.equal(2);
});
it('should return 0.816', function() {
var result = stats.standardDeviation([1, 2, 3]);
expect(result).to.equal(0.816496580927726);
})
it('should return 0.666', function() {
var result = stats.variance([1, 2, 3]);
expect(result).to.equal(0.6666666666666666);
})
});

0 comments on commit b9a356b

Please sign in to comment.