如何从python/scipy/numpy等步骤函数中采样? [英] How to sample from a step function in python/scipy/numpy etc?

查看:106
本文介绍了如何从python/scipy/numpy等步骤函数中采样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有bin边并为每个bin计数,那么有没有一种很好的简洁方法可以从概率密度函数中采样呢?

这是我的意思的一个例子.

  bin_edges = [0,2.1,6.3,23.5]计数= [5,2,10] 

概率密度函数是一个阶跃函数,其步骤为:

  [0,2.1,6.3,23.5] 

,第一步的概率密度(步长)为5/(17 * 2.1).第二个步骤/步骤的概率密度为2/(17 * 4.2),第三个步骤/步骤的概率密度为10/(17 * 17.2).

如果您落在垃圾箱/阶梯中,则从阶梯的x值中均匀采样所采样的值.因此,如果您在第一步中落下,则它在 0 和 2.1 之间是统一的.

是否有使用Python模块执行此采样的简洁方法?例如使用scipy/numpy/etc?

解决方案

如果我具有bin边缘和每个bin的计数,是否有一种简洁的方法可以从这暗示的概率密度函数中采样?"/em>

If I have bin edges and counts for each bin, is there a nice succinct way to sample from the probability density function this implies?

Here is an example of what I mean.

bin_edges = [0,2.1,6.3,23.5]
counts = [5,2,10]

The probability density function is a step function with steps at:

[0,2.1,6.3,23.5]

and the probability density (height of the step) for the first step is 5/(17* 2.1). The probability density for the second bin/step is 2/(17*4.2), the probability density the third step/bin is 10/(17*17.2).

If you fall in a bin/step the value you sample is uniformly sampled from the x-values of the step. So if you fall in the first step it is uniform between 0 and 2.1.

Is there a succinct way of performing this sampling using a Python module? For example using scipy/numpy/etc?

解决方案

"If I have bin edges and counts for each bin, is there a nice succinct way to sample from the probability density function this implies?"

This is exactly the case for scipy.stats.rv_histogram.

Here's an example.

First, generate some histogram data that we can use for the example.

In [150]: sample = np.random.gamma(6, size=2000)                                                                                                                

In [151]: hist, edges = np.histogram(sample, bins=5)                                                                                                            

In [152]: hist                                                                                                                                                  
Out[152]: array([490, 949, 438, 100,  23])

In [153]: edges                                                                                                                                                 
Out[153]: 
array([ 1.23006474,  4.19769156,  7.16531838, 10.13294519, 13.10057201,
       16.06819883])

Create an instance of rv_histogram with that data.

In [154]: from scipy.stats import rv_histogram                                                                                                                  

In [155]: rv = rv_histogram((hist, edges))                                                                                                                      

Generate a random sample from rv, and plot its histogram.

In [156]: rv_sample = rv.rvs(size=100000)                                                                                                                       

In [157]: plt.hist(rv_sample, bins=50, alpha=0.5, edgecolor='k')

这篇关于如何从python/scipy/numpy等步骤函数中采样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆