Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. var egg_to_embryo = 0.75;
  4. var embryo_to_blastocyst = 0.50;
  5. var blastocyte_chance = 0.3; // egg_to_embryo * embryo_to_blastocyst;
  6. var debug = false;
  7.  
  8. function exponent(x, y) {
  9. var exp = 1;
  10. for (var i = 0; i < y; i++) {
  11. exp *= x;
  12. }
  13. return exp;
  14. }
  15.  
  16. function factorial(n) {
  17. var fact = n;
  18. for (var i = n - 1; i > 0; i--) {
  19. fact *= i;
  20. }
  21. return fact;
  22. }
  23.  
  24. function nCr(n, r) {
  25. if (r == 0 || r == n) { return 1; }
  26. return factorial(n) / (factorial(n - r) * factorial(r));
  27. }
  28.  
  29. function chance(pBlastocyte, eggs, blastocytes) {
  30. var success = exponent(pBlastocyte, blastocytes);
  31. var failure = exponent(1 - pBlastocyte, eggs - blastocytes);
  32. var chance = success * failure;
  33. var ways = nCr(eggs, blastocytes);
  34. var probability = chance * ways;
  35. if (debug) {
  36. console.log(eggs + ' eggs in, ' + blastocytes + ' blastocytes out');
  37. console.log(' success: ' + success);
  38. console.log(' failure: ' + failure);
  39. console.log(' chance: ' + chance);
  40. console.log(' ways: ' + ways);
  41. console.log(' probability: ' + probability);
  42. }
  43. return (exponent(pBlastocyte, blastocytes)
  44. * exponent(1 - pBlastocyte, eggs - blastocytes))
  45. * nCr(eggs, blastocytes);
  46. }
  47.  
  48. for (var eggs = 1; eggs <= 20; eggs++) {
  49. var sum = 0;
  50. for (var blastocytes = eggs; blastocytes >= 0; blastocytes--) {
  51. var prob = chance(blastocyte_chance, eggs, blastocytes);
  52. sum += prob;
  53. console.log(eggs + ' eggs in, ' + blastocytes + ' blastocytes out');
  54. console.log(' probability (exactly ' + blastocytes + '): ' + Math.round(prob * 100) + '%');
  55. console.log(' cumulative probability (at least ' + blastocytes + '): ' + Math.round(sum * 100) + '%');
  56. }
  57. console.log();
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement