viewof alpha = Inputs.range([0.1, 10], {
step: 0.1,
value: 1,
label: tex`\alpha`,
width: 200
})
viewof beta = Inputs.range([0.1, 10], {
step: 0.1,
value: 1,
label: tex`\beta`,
width: 200
})
// Gamma function approximation using Lanczos approximation
function gamma(z) {
const p = [676.5203681218851, -1259.1392167224028, 771.32342877765313,
-176.61502916214059, 12.507343278686905, -0.13857109526572012,
9.9843695780195716e-6, 1.5056327351493116e-7];
if (z < 0.5) {
return Math.PI / (Math.sin(Math.PI * z) * gamma(1 - z));
}
z -= 1;
let x = 0.99999999999980993;
for (let i = 0; i < p.length; i++) {
x += p[i] / (z + i + 1);
}
const t = z + p.length - 0.5;
return Math.sqrt(2 * Math.PI) * Math.pow(t, z + 0.5) * Math.exp(-t) * x;
}
// Beta function using gamma function
function betaFunc(x, y) {
return (gamma(x) * gamma(y)) / gamma(x + y);
}
// Beta probability density function
function betaPDF(x, a, b) {
if (x <= 0 || x >= 1) return 0;
return Math.pow(x, a - 1) * Math.pow(1 - x, b - 1) / betaFunc(a, b);
}
// Generate points for the beta distribution curve
points = Array.from({length: 100}, (_, i) => {
let x = 0.001 + i * 0.01;
return { x, y: betaPDF(x, alpha, beta) };
});
Plot.plot({
style: "overflow: visible; display: block; margin: 0 auto;",
width: 600,
height: 400,
y: {
grid: true,
label: "Density"
},
x: {
label: "x",
domain: [0, 1]
},
marks: [
Plot.line(points, {x: "x", y: "y", stroke: "steelblue"}),
Plot.ruleY([0])
]
})Beta Distribution
f_{X}(x) = {\frac {\Gamma (\alpha +\beta )}{\Gamma (\alpha )\Gamma (\beta )}}\,x^{\alpha -1}(1-x)^{\beta -1}
where \Gamma is the gamma function defined as:
\Gamma (z)=\int _{0}^{\infty}t^{z-1}e^{-t}\,dt
- Gamma functions are used to model factorial functions of complex numbers z.
- Beta functions are used to model behavior of random variables in intervals of finite length.
- E[X] = \frac{\alpha}{\alpha+\beta}
- Var[X] = \frac{\alpha\beta}{(\alpha+\beta)^2(\alpha+\beta+1)}
Python Code: Beta Distribution
To create Beta distributed data using numpy:
import numpy as np
alpha = 0.5
beta = 0.5
size = (1000,1)
data = np.random.beta(alpha, beta, size)