How can I generate data which will show inverted bell curve for normal distribution

瘦欲@ 提交于 2021-01-27 11:22:41

问题


I have generated random data which follows normal distribution using the below code:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

rng = np.random.default_rng()
number_of_rows = 10000
mu = 0
sigma = 1
data = rng.normal(loc=mu, scale=sigma, size=number_of_rows)

dist_plot_data = sns.distplot(data, hist=False)
plt.show()

The above code generates the below distribution plot as expected:

If I want to create a distribution plot that is exactly an inverse curve like below then how can I generate the random normal distribution data?

I want the data for which the distribution plot will show the inverse curve. How can I generate this normal distribution data?


回答1:


not sure how useful this is, but it's easy to do with rejection sampling. Borrowing the API from Peter O's previous solution but working with blocks for performance gives me:

import numpy as np

def invNormal(low, high, mu=0, sd=1, *, size=1, block_size=1024):
    remain = size
    result = []
    
    mul = -0.5 * sd**-2

    while remain:
        # draw next block of uniform variates within interval
        x = np.random.uniform(low, high, size=min((remain+5)*2, block_size))
        
        # reject proportional to normal density
        x = x[np.exp(mul*(x-mu)**2) < np.random.rand(*x.shape)]
        
        # make sure we don't add too much
        if remain < len(x):
            x = x[:remain]

        result.append(x)
        remain -= len(x)

    return np.concatenate(result)

can be used as sns.histplot(invNormal(-4, 4, size=100_000), bins=51), giving me:

note that probability densities have to integrate to 1, so the "wider" you make it the smaller the densities will be (i.e. you can't have a density of 0.4 on the y-axis if the range on the x-axis is [-4, +4]). also, it feels less useful to generate a KDE because it'll struggle with the discontinuity at the edges



来源:https://stackoverflow.com/questions/64878103/how-can-i-generate-data-which-will-show-inverted-bell-curve-for-normal-distribut

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!