import numpy as np import matplotlib.pyplot as plt from scipy.stats import beta

Set parameters

a = 2 b = 3

Generate random variables from the beta distribution

rv = beta(a, b)

Generate some random samples

sample = rv.rvs(size=1000)

Plot the probability density function (PDF) of the beta distribution

x = np.linspace(0, 1, 100) pdf = rv.pdf(x) plt.plot(x, pdf, 'r-', lw=2, label='Beta({},{}) PDF'.format(a, b))

Plot a histogram of the random samples

plt.hist(sample, bins=30, density=True, alpha=0.5, label='Sampled Data')

plt.xlabel('x') plt.ylabel('Probability') plt.title('Beta Distribution') plt.legend() plt.show()


原文地址: https://www.cveoy.top/t/topic/xa1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录