Skip to content

Commit

Permalink
added normal generator
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanEpstein committed Mar 4, 2015
1 parent fb53320 commit 52e028e
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions lib/datakit.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ var sd = function(arr){
}
// return prod([s_sq,1/arr.length]) - Math.pow(prod([s,1/arr.length]),2);
}

var csv = function(path,callback){
fs.readFile(path,function(err,data){
var reg = new RegExp('\r','g');
Expand All @@ -92,6 +91,7 @@ var csv = function(path,callback){
callback(res);
});
}

//Given an array of objects (arr), get all values associated with a key (key).
var col = function(arr,key){
var res = [];
Expand All @@ -110,7 +110,33 @@ var uni = function(n){
return res;
}

//potential additions
// cor, order stats, rnorm, binom
// plotting
var norm = function(n,mu,sig){
mu = mu || 0;
sig = sig || 1;

//makes a pair of normals with specified parameters via Box-Muller
function box(mu_,sig_){
var u1 = Math.random();
var u2 = Math.random();
var z1 = Math.sqrt(-2*Math.log(u1))*Math.cos(2*u2*Math.PI);
var z2 = Math.sqrt(-2*Math.log(u1))*Math.sin(2*u2*Math.PI);
return [(mu_+(sig_*z1)),(mu_+(sig_*z2))];
}
var res = [];
if (n % 2 == 0){
var iter = n/2;
}
else{
var iter = (n-1)/2;
res.push(box(mu,sig).pop());
}
for (var i=0; i<iter;i++){
res = res.concat(box(mu,sig));
}
return res;
}

// additions
// replace col with sql style select
// cor, order stats
// plotting

0 comments on commit 52e028e

Please sign in to comment.