Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env node
- var egg_to_embryo = 0.75;
- var embryo_to_blastocyst = 0.50;
- var blastocyte_chance = 0.3; // egg_to_embryo * embryo_to_blastocyst;
- var debug = false;
- function exponent(x, y) {
- var exp = 1;
- for (var i = 0; i < y; i++) {
- exp *= x;
- }
- return exp;
- }
- function factorial(n) {
- var fact = n;
- for (var i = n - 1; i > 0; i--) {
- fact *= i;
- }
- return fact;
- }
- function nCr(n, r) {
- if (r == 0 || r == n) { return 1; }
- return factorial(n) / (factorial(n - r) * factorial(r));
- }
- function chance(pBlastocyte, eggs, blastocytes) {
- var success = exponent(pBlastocyte, blastocytes);
- var failure = exponent(1 - pBlastocyte, eggs - blastocytes);
- var chance = success * failure;
- var ways = nCr(eggs, blastocytes);
- var probability = chance * ways;
- if (debug) {
- console.log(eggs + ' eggs in, ' + blastocytes + ' blastocytes out');
- console.log(' success: ' + success);
- console.log(' failure: ' + failure);
- console.log(' chance: ' + chance);
- console.log(' ways: ' + ways);
- console.log(' probability: ' + probability);
- }
- return (exponent(pBlastocyte, blastocytes)
- * exponent(1 - pBlastocyte, eggs - blastocytes))
- * nCr(eggs, blastocytes);
- }
- for (var eggs = 1; eggs <= 20; eggs++) {
- var sum = 0;
- for (var blastocytes = eggs; blastocytes >= 0; blastocytes--) {
- var prob = chance(blastocyte_chance, eggs, blastocytes);
- sum += prob;
- console.log(eggs + ' eggs in, ' + blastocytes + ' blastocytes out');
- console.log(' probability (exactly ' + blastocytes + '): ' + Math.round(prob * 100) + '%');
- console.log(' cumulative probability (at least ' + blastocytes + '): ' + Math.round(sum * 100) + '%');
- }
- console.log();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement