The Beta distribution models probabilities themselves — perfect for random variables bounded between 0 and 1. It’s commonly used in Bayesian inference as a prior for binomial proportions due to its flexibility in shape. 🔧
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 approximationfunctiongamma(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) {returnMath.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;returnMath.sqrt(2*Math.PI) *Math.pow(t, z +0.5) *Math.exp(-t) * x;}// Beta function using gamma functionfunctionbetaFunc(x, y) {return (gamma(x) *gamma(y)) /gamma(x + y);}// Beta probability density functionfunctionbetaPDF(x, a, b) {if (x <=0|| x >=1) return0;returnMath.pow(x, a -1) *Math.pow(1- x, b -1) /betaFunc(a, b);}// Generate points for the beta distribution curvepoints =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]) ]})