简体   繁体   English

使用 beta 分布生成特定范围内的随机数

[英]Generate random numbers in a specific range using beta distribution

I want to generate 1 Million and 10 Million data points in the range (0.0001,0.03) using a beta distribution with a=2.2 and b=1.我想使用 a=2.2 和 b=1 的 beta 分布在 (0.0001,0.03) 范围内生成 100 万和 1000 万个数据点。 Thanks in advance!提前致谢!

I tried this:我试过这个:

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

fig, ax = plt.subplots(1, 1)
a, b = 2.2, 1
#Generated pdf of required range
x = np.linspace(0.0001, 0.03, 100)

ax.plot(x, beta.pdf(x, a, b),
       'k-', lw=5, alpha=0.6, label='beta pdf')

r = beta.rvs(a, b, size=1000) #Generating values
print(r)

The values in 'r' are not in the range (0.0001,0.03). “r”中的值不在范围 (0.0001,0.03) 内。

For for alpha and beta parameters you are using, the beta distribution is a fairly straight line from (0, 0) to (1, 2.2) .对于您使用的 alpha 和 beta 参数,beta 分布是一条从(0, 0)(1, 2.2)的直线。 The range you are interested in (0.0001, 0.03) is both a very thin slice of the 0 to 1 range, but also has a very small probability for the parameters you selected.您感兴趣的范围(0.0001, 0.03)既是 0 到 1 范围的一小部分,但对于您选择的参数来说也有很小的概率。

To actually generate 1M or 10M points, you will need to keep generating points and accumulating them to an array.要实际生成 100 万或 1000 万个点,您将需要不断生成点并将它们累加到一个数组中。

from scipy.stats import beta
import numpy as np

b_dist = beta(a=2.2, b=1)
target_length = 1_000_000
points = np.array([])

while points.size < target_length:
    # generate new points
    x = b_dist.rvs(1_000_000)
    # find the indices that match criteria
    ix = x < 0.03
    ix = ix & (0.0001 < x)
    points = np.hstack([points, x[ix]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中使用帕累托分布生成特定范围内的随机数 - How to generate random numbers in specyfic range using pareto distribution in Python 为非标准 beta 分布生成随机数 - Generate random numbers for a non-standard beta distribution Python删除Beta分布的随机数 - Python drop random numbers of a beta distribution 使用python从指数分布和模型生成随机数 - Generate random numbers from exponential distribution and model using python 使用 Pandas 在特定范围内生成与其他列值相关的随机数 - Generate random numbers in a specific range that correlates to other column values using Pandas 生成除python中的特定数字以外的范围内的随机非重复数字 - Generate random non repeating numbers in a range except a specific number in python 从特定范围生成一组排序的随机数 - Generate a set of sorted random numbers from a specific range 生成具有给定(数字)分布的随机数 - Generate random numbers with a given (numerical) distribution 如何生成具有预定义概率分布的随机数? - How to generate random numbers with predefined probability distribution? 生成在给定区间内具有数值分布的随机数 - Generate random numbers with a numerical distribution in given intervals
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM