Published
Edited
May 25, 2018
4 forks
Importers
7 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function randb(alpha, beta) {
const u = randg(alpha);
return u / (u + randg(beta));
}
Insert cell
// Returns a gamma deviate by the method of Marsaglia and Tsang.
function randg(shape) {
let oalph = shape, a1, a2, u, v, x, mat;
if (!shape) shape = 1;
if (shape < 1) shape += 1;
a1 = shape - 1 / 3;
a2 = 1 / Math.sqrt(9 * a1);
do {
do {
x = randn();
v = 1 + a2 * x;
} while (v <= 0);
v = v * v * v;
u = Math.random();
} while (
u > 1 - 0.331 * Math.pow(x, 4) &&
Math.log(u) > 0.5 * x * x + a1 * (1 - v + Math.log(v))
);
if (shape === oalph) return a1 * v; // alpha > 1
do u = Math.random(); while (u === 0); // alpha < 1
return Math.pow(u, 1 / oalph) * a1 * v;
}
Insert cell
// Returns a normal deviate (mu=0, sigma=1).
function randn() {
let u, v, x, y, q;
do {
u = Math.random();
v = 1.7156 * (Math.random() - 0.5);
x = u - 0.449871;
y = Math.abs(v) + 0.386595;
q = x * x + y * (0.19600 * y - 0.25472 * x);
} while (q > 0.27597 && (q > 0.27846 || v * v > -4 * Math.log(u) * u * u));
return v / u;
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more