beta distribution

beta distribution

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import beta
import d2l.torch as d2l
d2l.use_svg_display()
def plot(alist, blist):
    for a in alist:
        for b in blist:
            x = np.linspace(0, 1, 100) # 后面这个个数的选取只会影响ab小于1的时候的图形
            y = beta.pdf(x, a, b)
            plt.plot(x, y, label=f'a={a}, b={b}')
    plt.legend()

U 形是没有意义的…因为 α \alpha α β \beta β 的选择都太小了, 一般选不到这么小

alist = [0.5, 1]
blist = [0.5, 1]
plot(alist, blist)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-G9gOlwTz-1672731565119)(output_3_0.svg)]
可以把 α \alpha α 看作成功的概率, β \beta β 看作失败的概率

alist = [36, 78]
blist = [13, 79]
plot(alist, blist)

在这里插入图片描述

alist = [10, 100]
blist = [10, 100]
plot(alist, blist)

在这里插入图片描述

参考文献:
推荐算法之Thompson(汤普森)采样
如何通俗理解 beta 分布?

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Sure, I can help you with that. The Metropolis-Hastings algorithm is a Markov Chain Monte Carlo (MCMC) method used to generate samples from a target distribution that is difficult to sample from directly. In this case, we want to generate samples from the beta distribution using the uniform distribution as a candidate distribution. The beta distribution is given by: $$p(x) \propto x^{\alpha-1}(1-x)^{\beta-1}$$ where $x \in [0, 1]$ and $\alpha, \beta > 0$ are the shape parameters. To apply the Metropolis-Hastings algorithm, we need to follow these steps: 1. Initialize $x_0$ to some value in $[0, 1]$. 2. For each iteration $t$: - Generate a candidate sample $y$ from the uniform distribution $U(x_t - \epsilon, x_t + \epsilon)$, where $\epsilon$ is a small positive constant. - Compute the acceptance ratio $r = \frac{p(y)}{p(x_t)}$. - Generate a random number $u$ from the uniform distribution $U(0, 1)$. - If $u < r$, set $x_{t+1} = y$, otherwise set $x_{t+1} = x_t$. 3. Repeat step 2 until enough samples have been generated. Note that we can simplify the acceptance ratio by canceling constants: $$r = \frac{p(y)}{p(x_t)} = \frac{y^{\alpha-1}(1-y)^{\beta-1}}{x_t^{\alpha-1}(1-x_t)^{\beta-1}}$$ Here's some Python code that implements the Metropolis-Hastings algorithm to generate samples from the beta distribution: ```python import numpy as np def beta_mh(alpha, beta, epsilon, n_samples): # Initialize x = np.random.rand() samples = np.zeros(n_samples) # Generate samples for i in range(n_samples): # Generate candidate sample y = np.random.uniform(x - epsilon, x + epsilon) # Compute acceptance ratio r = (y**(alpha-1) * (1-y)**(beta-1)) / (x**(alpha-1) * (1-x)**(beta-1)) # Generate random number u = np.random.rand() # Accept or reject candidate sample if u < r: x = y # Save sample samples[i] = x return samples ``` You can call this function with the desired shape parameters $\alpha$ and $\beta$, the step size $\epsilon$, and the number of samples to generate. For example: ```python alpha = 2 beta = 5 epsilon = 0.1 n_samples = 1000 samples = beta_mh(alpha, beta, epsilon, n_samples) ``` This will generate 1000 samples from the beta distribution with shape parameters $\alpha=2$ and $\beta=5$, using the uniform distribution with step size 0.1 as the candidate distribution.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

d3ac

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值