Skip to content

Instantly share code, notes, and snippets.

@aedoran
Created June 5, 2012 16:00
Show Gist options
  • Save aedoran/2875932 to your computer and use it in GitHub Desktop.
Save aedoran/2875932 to your computer and use it in GitHub Desktop.
normal distribution thrown into scores from 0 - 100
var scores = [0,2,3,2,24,24,2,2];
//get the mean
var avg = 0;
scores.forEach(function(a) {
avg+= a;
});
avg = avg / scores.length;
//get the standard_deviation
var devs = 0;
scores.forEach(function(a) {
devs += Math.pow(avg-a,2);
});
var standard_deviation = Math.sqrt(devs/(scores.length-1));
//normal distribution function
var normal = function(num) {
return (1/(standard_deviation * Math.sqrt(2*Math.PI))) *
Math.exp(-(Math.pow(num-avg,2)) / (2 * (Math.pow(standard_deviation,2))));
}
//calculate the size of integral
var integral = 0;
var bar_width = standard_deviation/100;
for (var a=1; a<400; a++) {
integral += bar_width*normal(a*bar_width);
}
integral = integral*2;
//make the mappings from 0-100 based on the proportion of the integral
var mapping = {};
var start = integral/2;
for (var a=1; a<400; a++) {
var proportion = Math.floor(100*(bar_width*normal(a*bar_width)+start/integral));
if (!mapping[proportion]) { mapping[proportion] = a*bar_width };
start += bar_width*normal(a*bar_width);
}
//bucket divisions
var divisions = {};
for (var a in mapping) {
if (a == 50) { divisions[avg] = 50 }
else {
divisions[avg+mapping[a]] = parseInt(a);
divisions[avg-mapping[a]] = 50-(a-50);
}
}
var buckets= {};
//now put them into buckets of 0-100
var testsum = 0;
scores.forEach(function(a) {
var closest = 100000;
var bucket = 0;
for (var d in divisions) {
if (closest > Math.abs(a-d)) {
bucket = divisions[d]
closest = Math.abs(a-d);
}
}
console.log(a, bucket);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment