Last active
April 16, 2017 04:45
-
-
Save yefremov/72e1da689aac177ce0acae346757a7f6 to your computer and use it in GitHub Desktop.
Data Science Math Skills
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
// Data Science Math Skills | |
// https://www.coursera.org/learn/datasciencemathskills/home/welcome | |
// Sigma summation | |
function sigma(n, i, f) { | |
var sum = 0; | |
while (i <= n) { | |
sum += f(i++); | |
} | |
return sum; | |
} | |
// sigma(100, 1, i => i); | |
// => 1050 | |
// Maen value | |
function mean(x) { | |
var n = x.length; | |
return 1 / n * sigma(n, 1, function (i) { | |
// decrement index by one since array index in | |
// JavaScript starts from zero | |
return x[i-1]; | |
}); | |
} | |
// mean([5, 6, 7]); | |
// => 6 | |
// Variance population | |
function variance(x) { | |
var n = x.length; | |
return 1 / n * sigma(n, 1, function (i) { | |
// decrement index by one since array index in | |
// JavaScript starts from zero | |
var res = x[i-1] - mean(x); | |
// square result | |
return res * res; | |
}); | |
} | |
// variance([1, 5, 12]) | |
// => 20.666666666666664 | |
// Standart deviation | |
function deviation(x) { | |
return Math.sqrt(variance(x)); | |
} | |
// deviation([2,4,4,4,5,5,7,9]); | |
// => 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment