source: node_modules/d3-random/src/gamma.js@ e4c61dd

Last change on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 1.1 KB
Line 
1import defaultSource from "./defaultSource.js";
2import normal from "./normal.js";
3
4export default (function sourceRandomGamma(source) {
5 var randomNormal = normal.source(source)();
6
7 function randomGamma(k, theta) {
8 if ((k = +k) < 0) throw new RangeError("invalid k");
9 // degenerate distribution if k === 0
10 if (k === 0) return () => 0;
11 theta = theta == null ? 1 : +theta;
12 // exponential distribution if k === 1
13 if (k === 1) return () => -Math.log1p(-source()) * theta;
14
15 var d = (k < 1 ? k + 1 : k) - 1 / 3,
16 c = 1 / (3 * Math.sqrt(d)),
17 multiplier = k < 1 ? () => Math.pow(source(), 1 / k) : () => 1;
18 return function() {
19 do {
20 do {
21 var x = randomNormal(),
22 v = 1 + c * x;
23 } while (v <= 0);
24 v *= v * v;
25 var u = 1 - source();
26 } while (u >= 1 - 0.0331 * x * x * x * x && Math.log(u) >= 0.5 * x * x + d * (1 - v + Math.log(v)));
27 return d * v * multiplier() * theta;
28 };
29 }
30
31 randomGamma.source = sourceRandomGamma;
32
33 return randomGamma;
34})(defaultSource);
Note: See TracBrowser for help on using the repository browser.