Product of two beta distributions

牧云@^-^@ 提交于 2019-12-06 06:27:16
Sven Hohenstein

For an analytical solution, have a look at this paper and this answer.

A numerical approach in R

set.seed(1) # for reproducability

n <- 100000 # number of random variables

# first beta distribution
a1 <- 0.5
b1 <- 0.9
X <- rbeta(n, a1, b1)

# second beta distribution
a2 <- 0.9
b2 <- 0.5
Y <- rbeta(n, a2, b2)

# calculate product
Z <- X * Y

# Have a look at the distributions
plot(density(Z), col = "red", main = "Distributions")
lines(density(X), lty = 2)
lines(density(Y), lty = 2)

FWIW, same in Python

from scipy import stats
import statsmodels.api as sm
import matplotlib.pyplot as plt

N = 100000

y = stats.beta(.5, .9).rvs(N)
x = stats.beta(.9, .5).rvs(N)
z = x*y
dens_z = sm.nonparametric.KDEUnivariate(z)
dens_z.fit()

dens_x = sm.nonparametric.KDEUnivariate(x)
dens_x.fit()

dens_y = sm.nonparametric.KDEUnivariate(y)
dens_y.fit()

fig, ax = plt.subplots()
ax.plot(dens_z.support, dens_z.density, label='z')
ax.plot(dens_x.support, dens_x.density, label='x')
ax.plot(dens_y.support, dens_y.density, label='y')
ax.legend()
plt.draw_if_interactive()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!