Random number generation from an arbitrary probability density function

I would like to be able to generate random numbers with a probability density function that comes from a drawn curve. The two below have the same area under the curve, but should create lists of random numbers with different characteristics.

enter image description here

My intuition is that one way would be to sample the curve, and then use the areas of these rectangles to feed np.random.choice, to select a range, to make a normal random case in the range of this range of rectangles.

enter image description here

This is not a very effective way to do this. Is there a more β€œright” way to do this?

I actually had a crack:

import matplotlib.pyplot as plt
import numpy as np

areas = [4.397498, 4.417111, 4.538467, 4.735034, 4.990129, 5.292455, 5.633938,
         6.008574, 6.41175, 5.888393, 2.861898, 2.347887, 2.459234, 2.494357,
         2.502986, 2.511614, 2.520243, 2.528872, 2.537501, 2.546129, 7.223747,
         7.223747, 2.448148, 1.978746, 1.750221, 1.659351, 1.669999]
divisons = [0.0, 0.037037, 0.074074, 0.111111, 0.148148, 0.185185, 0.222222,
            0.259259, 0.296296, 0.333333, 0.37037, 0.407407, 0.444444, 0.481481,
            0.518519, 0.555556, 0.592593, 0.62963, 0.666667, 0.703704, 0.740741,
            0.777778, 0.814815, 0.851852, 0.888889, 0.925926, 0.962963, 1.0]
weights = [a/sum(areas) for a in areas]
indexes = np.random.choice(range(len(areas)), 50000, p=weights)
samples = []
for i in indexes:
    samples.append(np.random.uniform(divisons[i], divisons[i+1]))

binwidth = 0.02
binSize = np.arange(min(samples), max(samples) + binwidth, binwidth)
plt.hist(samples, bins=binSize)
plt.xlim(xmax=1)
plt.show()

enter image description here

The method works, but a little harder!

+4
3

, , , , .

, y ( y- ) _ , .

from numpy.random import choice
pde = choice(list_of_candidates, number_of_items_to_pick, p=probability_distribution)

_ ( y y ) - , list_of_candidates ( x). replace = False, , .

. numpy docs

, pde, , pde.

EDIT: . pde, numba (http://numba.pydata.org) for.

+3

- rv_continuous scipy.stats. pdf rv_continuous. , , pdf, cdf .

+2

Another method is to reverse the CDF. Then you use a single random generator to generate p values ​​on the x axis of the inverse CDF to generate random accesses of your PDF. See This Article: http://matlabtricks.com/post-44/generate-random-numbers-with-a-given-distribution

0
source

Source: https://habr.com/ru/post/1666818/


All Articles