Skip to content Skip to sidebar Skip to footer

How Can I Generate Data Which Will Show Inverted Bell Curve For Normal Distribution

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.defau

Solution 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

definvNormal(low, high, mu=0, sd=1, *, size=1, block_size=1024):
    remain = size
    result = []
    
    mul = -0.5 * sd**-2while 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 muchif 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:

histogram

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

Post a Comment for "How Can I Generate Data Which Will Show Inverted Bell Curve For Normal Distribution"