sloped uniform distribution python

1.9k views Asked by At

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. I do know that for the first one, I could use 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. 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! standard uniform distribution plot

enter image description here

2

There are 2 answers

4
orange On

You could try to create your own pdf with stats.rv_continuous.

Here's a 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:

enter image description here

0
immerrr On

You could use inverse transform sampling for this problem.

Let's look at a simple slope distribution that will generate [0;1] numbers s.t. f(0) = 0 and f(1) = 2, 2 comes from normalization of F(x), i.e. F(1) = P(x <= 1) = 1 by definition of probability.

maths

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. Let's check that:

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

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

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

enter image description here