-
-
Save southwolf/ed9e455a08bfd1e0ebef1336ce69ffc0 to your computer and use it in GitHub Desktop.
Revisions
-
izaac revised this gist
Dec 30, 2015 . 1 changed file with 15 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ #!/usr/bin/env node /* jshint node: true */ 'use strict'; var program = require('commander'); @@ -92,27 +93,23 @@ function getZPercent(z) return sum; } function example(n){ n = typeof n !== 'undefined' ? n : 100; var x = Math.round(Math.random() * 1000) + 1; var rnd_arr = randomArrayThousand(n); var std = stdDev(rnd_arr); var zscr = zScore(x, average(rnd_arr), std); var zp = getZPercent(zscr); console.log("The Standard Deviation of a random " +n+ " elements Array [Values 1:1000] is: " + std ); console.log("zScore of random X="+ x +" is: " + zscr); console.log("Percentile: " + Math.floor(zp * 100) + "%" ); console.log("Median: " + median(rnd_arr)); } program .version('0.0.1') .description('An Application to Calculate Descriptive Statistics') .option('-e, --example <n>', 'Execute Example Code') .parse(process.argv); if (program.example) example(program.example); -
izaac revised this gist
Dec 30, 2015 . 1 changed file with 12 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ /* jshint node: true */ 'use strict'; var program = require('commander'); @@ -15,29 +14,29 @@ function numSquare(a) { return Math.pow(a, 2); } function randomArrayThousand(numElems) { var arr = []; for (var i=0; i<numElems; i++){ arr.push(Math.round(Math.random() * 1000) + 1); } return arr; } function median(arr) { arr.sort(numericSort); if (arr.length % 2 === 0){ return arr[arr.length/2]; } else{ return (arr[Math.floor(arr.length/2 - 1)] + arr[Math.floor(arr.length/2)]) / 2; } } function average(arr){ var sum = arr.reduce(add); return sum / arr.length; } function deviations(arr, mean){ return arr.map(function(a, mean) { return a - mean}); } @@ -97,12 +96,14 @@ function example(){ var i = 100; do{ var x = Math.round(Math.random() * 1000) + 1; var rnd_arr = randomArrayThousand(99); var std = stdDev(rnd_arr); var zscr = zScore(x, average(rnd_arr), std); var zp = getZPercent(zscr); console.log("The Standard Deviation of a random 100 elements Array [Values 1:1000] is: " + std ); console.log("zScore of random X="+ x +" is: " + zscr); console.log("Percentile: " + Math.floor(zp * 100) + "%" ); console.log("Median: " + median(rnd_arr)); i--; } while (i > 0); -
izaac revised this gist
Dec 29, 2015 . 1 changed file with 17 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ #!/usr/bin/env node /* jshint node: true */ 'use strict'; var program = require('commander'); @@ -6,6 +7,10 @@ function add(a, b){ return a + b; } function numericSort(a,b) { return a - b; } function numSquare(a) { return Math.pow(a, 2); } @@ -23,6 +28,16 @@ function average(arr){ return sum / arr.length; } function median(arr) { arr.sort(numericSort); if (arr.length % 2 === 0){ return arr[arr.length/2]; } else{ return (arr[arr.length/2 - 1] + arr[arr.length/2]) / 2; } } function deviations(arr, mean){ return arr.map(function(a, mean) { return a - mean}); } @@ -32,7 +47,7 @@ function squaredDeviations(arr){ // return arr.map(function(a){ return Math.pow(a, 2)}); } function variance(arr){ var avg = average(arr); return squaredDeviations(deviations(arr, avg)).reduce(add) / arr.length; @@ -41,7 +56,7 @@ function varianceExample(arr){ function stdDev(arr, sample){ // default value of sample is false. If sample is defined then we apply Bessel's Correction. sample = typeof sample !== 'undefined' ? sample : false; return sample ? Math.sqrt(variance(arr)) - 1 : Math.sqrt(variance(arr)); } function zScore(x, m, std){ -
izaac created this gist
Dec 28, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,102 @@ /* jshint node: true */ 'use strict'; var program = require('commander'); function add(a, b){ return a + b; } function numSquare(a) { return Math.pow(a, 2); } function randomArrayThousand(numElems){ var arr = []; for (var i=0; i<numElems; i++){ arr.push(Math.round(Math.random() * 1000) + 1); } return arr; } function average(arr){ var sum = arr.reduce(add); return sum / arr.length; } function deviations(arr, mean){ return arr.map(function(a, mean) { return a - mean}); } function squaredDeviations(arr){ return arr.map(numSquare); // return arr.map(function(a){ return Math.pow(a, 2)}); } function varianceExample(arr){ var avg = average(arr); return squaredDeviations(deviations(arr, avg)).reduce(add) / arr.length; } function stdDev(arr, sample){ // default value of sample is false. If sample is defined then we apply Bessel's Correction. sample = typeof sample !== 'undefined' ? sample : false; return sample ? Math.sqrt(varianceExample(arr)) - 1 : Math.sqrt(varianceExample(arr)); } function zScore(x, m, std){ return Math.abs(x - m) / std; } function getZPercent(z) { // http://stackoverflow.com/questions/16194730/seeking-a-statistical-javascript-function-to-return-p-value-from-a-z-score //z == number of standard deviations from the mean //if z is greater than 6.5 standard deviations from the mean //the number of significant digits will be outside of a reasonable //range if ( z < -6.5) return 0.0; if( z > 6.5) return 1.0; var factK = 1; var sum = 0; var term = 1; var k = 0; var loopStop = Math.exp(-23); while(Math.abs(term) > loopStop) { term = 0.3989422804 * Math.pow(-1,k) * Math.pow(z,k) / (2 * k + 1) / Math.pow(2,k) * Math.pow(z,k+1) / factK; sum += term; k++; factK *= k; } sum += 0.5; return sum; } function example(){ var i = 100; do{ var x = Math.round(Math.random() * 1000) + 1; var rnd_arr = randomArrayThousand(100); var std = stdDev(rnd_arr); var zscr = zScore(x, average(rnd_arr), std); var zp = getZPercent(zscr); console.log(i + " - The Standard Deviation of a random 1000 Array [1 - 100] is: " + std + " zScore of X="+ x + ": " + zscr + " Percentile: " + Math.floor(zp * 100) + "%" ); i--; } while (i > 0); } program .version('0.0.1') .description('An Application to Calculate Descriptive Statistics') .option('-e, --example', 'Execute Example Code') .parse(process.argv); if (program.example) example();