根据所需分布和经验抽样生成随机数 [英] Generating random numbers given required distribution and empirical sampling
问题描述
我有两组样本,一组呈指数分布,第二组是 Bernoli(我使用了 scipy.stats.expon 和 scipy.stats.bernoulli 以适合我的数据).
I have two sets of samplings, one distributes exponentially and the second- Bernoli (I used scipy.stats.expon and scipy.stats.bernoulli to fit my data).
基于这些采样,我想创建两个随机生成器,使我能够从两个分布中采样数字.
Based on these sampling, I want to create two random generators that will enable me to sample numbers from the two distributions.
这样做有哪些替代方案?
What alternatives are there for doing so?
如何找到创建随机生成器的正确参数?
How can I find the correct parameters for creating the random generators?
推荐答案
使用 rvs
方法使用估计参数生成样本.例如,假设 x
保存我的初始数据.
Use the rvs
method to generate a sample using the estimated parameters. For example, suppose x
holds my initial data.
In [56]: x
Out[56]:
array([ 0.366, 0.235, 0.286, 0.84 , 0.073, 0.108, 0.156, 0.029,
0.11 , 0.122, 0.227, 0.148, 0.095, 0.233, 0.317, 0.027])
使用 scipy.stats.expon
来拟合此数据的指数分布.我假设我们对位置参数为 0 的通常情况感兴趣,所以我在 fit
调用中使用了 floc=0
.
Use scipy.stats.expon
to fit the expononential distribution to this data. I assume we are interested in the usual case where the location parameter is 0, so I use floc=0
in the fit
call.
In [57]: from scipy.stats import expon
In [58]: loc, scale = expon.fit(x, floc=0)
In [59]: scale
Out[59]: 0.21076203455218898
现在使用这些参数生成随机样本.
Now use those parameters to generate a random sample.
In [60]: sample = expon.rvs(loc=0, scale=scale, size=8)
In [61]: sample
Out[61]:
array([ 0.21576877, 0.23415911, 0.6547364 , 0.44424148, 0.07870868,
0.10415167, 0.12905163, 0.23428833])
这篇关于根据所需分布和经验抽样生成随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!