fake age range numbers via gaussian distribution
var gaussian = require('gaussian'); | |
// mean is the center of the distritubion | |
var mean = 34.5; | |
var variance = 300; | |
var distribution = gaussian(mean, variance); | |
// number of samples | |
var x = 1000; | |
var results = []; | |
// bins | |
var bins = ([{ | |
start: 16, | |
stop: 18 | |
}, { | |
start: 18, | |
stop: 25 | |
}, { | |
start: 25, | |
stop: 35 | |
}, { | |
start: 35, | |
stop: 45 | |
}, { | |
start: 45, | |
stop: 55 | |
}, { | |
start: 55, | |
stop: 65 | |
}, { | |
start: 65, | |
stop: 99 | |
}]).map(bin => { | |
bin.results = []; | |
return bin; | |
}); | |
// take a sample | |
for (let i = 0; i <= x; i++) { | |
results.push(distribution.ppf(Math.random())); | |
} | |
// sort samples to make bin-ing faster | |
results = results.sort((a,b) => a > b ? 1 : -1); | |
// put results in bins | |
var bindex = 0; | |
var bin = bins[0]; | |
results.forEach(function(result) { | |
if (result < 0) return; | |
// find the appropriate bin | |
while (result > bin.stop && bindex <= bins.length) { | |
bindex++; | |
bin = bins[bindex]; | |
} | |
if (result < bin.start) return; | |
bin.results.push(result); | |
}); | |
bins.forEach((bin, i) => { | |
var int = ~~(bin.results.length / 10); | |
console.log(bin.start, bin.stop, new Array(int).fill('x').join('')); | |
}); | |
/* | |
example results | |
16 18 'xx' | |
18 25 'xxxxxxxxxxx' | |
25 35 'xxxxxxxxxxxxxxxxxxxxxxx' | |
35 45 'xxxxxxxxxxxxxxxxxxxxx' | |
45 55 'xxxxxxxxxxxxxxx' | |
55 65 'xxxxxxx' | |
65 99 'xxx' | |
*/ |
{ | |
"name": "fake-age-bins", | |
"version": "1.0.0", | |
"description": "", | |
"main": "fake-age-bins.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"gaussian": "^1.1.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment