0

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
  • 3
    You are misinterpreting these graphs. These are both of the same uniform distribution. The latter graph is just the cumulative distribution function of the uniform distribution, which is the integral of the probability distribution function. Commented Nov 30, 2014 at 9:41
  • 1
    @RobertKern is correct. There is no such thing as a sloped uniform distribution. There are triangular distributions (which immerrr has given an answer for) and trapezoidal distributions, is one of those what you want? Commented Nov 30, 2014 at 17:16

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

I don't thin this works, Im getting the following: >>> print your_random_values [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
I check it out later and get back to you.
Sorry, I quoted the discrete version, you have look at stats.rv_continuous.
After reading your question again, I realised that you're quoting the pdf and cdf of a uniform distribution. The above is not a uniform distribution. I'm not entirely sure this is what you're after...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.