简体   繁体   English

倾斜均匀分布python

[英]sloped uniform distribution python

I would like to acquire values from a uniform distribution that is sloped instead of a standard uniform distribution which is drawing out values from a straight flat line of slope = 0. To be more specific, I'd like to get values from the function of the slope distribution, FIGURE 2 BELOW. 我想从倾斜的均匀分布中获取值,而不是从斜率= 0的直线绘制出值的标准均匀分布中获取值。更具体地说,我想从的函数中获取值斜率分布,如图2所示。 I do know that for the first one, I could use numpy.random.uniform(initial,final). 我确实知道,对于第一个,我可以使用numpy.random.uniform(initial,final)。 How can I do this for a sloped distribution? 如何针对倾斜分布进行此操作? I know that multiplying a 'slope' or scaling factor to the values from the numpy.random.uniform does not mathematically mean that values are being drawn out from a sloped distribution. 我知道将“坡度”或比例因子与numpy.random.uniform中的值相乘并不表示从倾斜分布中提取值。 I do realize this might have something to do with changing the way each drawn out value is weighted. 我确实意识到这可能与更改每个提取值的加权方式有关。 source: http://www.itl.nist.gov/div898/handbook/eda/section3/eda3662.htm Please help! 来源: http : //www.itl.nist.gov/div898/handbook/eda/section3/eda3662.htm请帮助! 标准均匀分布图

在此处输入图片说明

You could use inverse transform sampling for this problem. 您可以使用逆变换采样来解决此问题。

Let's look at a simple slope distribution that will generate [0;1] numbers st f(0) = 0 and f(1) = 2 , 2 comes from normalization of F(x) , ie F(1) = P(x <= 1) = 1 by definition of probability. 让我们看一个简单的斜率分布,该分布将生成[0; 1]数字st f(0) = 0f(1) = 2 2来自于F(x)归一化,即F(1) = P(x <= 1) = 1通过概率的定义。

数学

According to the inverse transform sampling method, to get a random variable with necessary distribution you need to plug in a uniformly distributed random variable instead of Y into the last equation. 根据逆变换采样方法,要获得具有必要分布的随机变量,您需要将均匀分布的随机变量而不是Y插入到最后一个方程中。 Let's check that: 让我们检查一下:

In [61]: y = np.random.rand(10000)

In [62]: x = np.sqrt(y)

In [63]: plt.hist(x, bins=100)

在此处输入图片说明

You could try to create your own pdf with stats.rv_continuous . 您可以尝试使用stats.rv_continuous创建自己的pdf。

Here 'sa SO answer that could help you. 是一个可以帮助您的答案。

Some code: 一些代码:

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

class linear(scipy.stats.rv_continuous):
    def _cdf(self, x):
        return x**2

distrib = linear(a=0, b=1.0)
d = distrib.rvs(size=10000)

fig, ax = plt.subplots(1, 1)
ax.hist(d, normed=True, histtype='stepfilled', alpha=0.2, bins=100)
plt.show()

A histogram of random samples of the distribution: 分布的随机样本的直方图:

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM