Skip to content

Instantly share code, notes, and snippets.

@yefremov
Last active April 16, 2017 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yefremov/72e1da689aac177ce0acae346757a7f6 to your computer and use it in GitHub Desktop.
Save yefremov/72e1da689aac177ce0acae346757a7f6 to your computer and use it in GitHub Desktop.
Data Science Math Skills
// 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