Created
February 5, 2023 17:28
-
-
Save fasiha/fe0a79d294f222d060fa6aeff1424db2 to your computer and use it in GitHub Desktop.
Density of the max of independent Beta random variables implementing https://math.stackexchange.com/a/2965587
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pylab as plt | |
from math import prod | |
import numpy as np | |
from scipy.stats import beta as betarv | |
from scipy.special import betainc, beta as betafn | |
plt.ion() | |
plt.style.use('ggplot') | |
# %matplotlib qt | |
params = [(3, 4), (2, 3), (5, 2)] | |
betas = [betarv(a, b) for a, b in params] | |
size = 1_000_000 | |
rvs = np.max(np.vstack([b.rvs(size) for b in betas]), axis=0) | |
xs = np.linspace(0, 1, 1001) | |
def pdf(x: float | int): | |
return sum(b.pdf(x) * prod([betainc(*b2.args, x) for b2 in betas if b2 != b]) for b in betas) | |
plt.figure() | |
plt.hist(rvs, bins=50, density=True, label='histogram') | |
plt.plot(xs, [pdf(x) for x in xs], label='JimB PDF') | |
plt.xlabel('x') | |
plt.ylabel('P(x)') | |
plt.legend() | |
plt.savefig('hist-fit.png', dpi=300) | |
plt.savefig('hist-fit.svg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment