如何生成将显示正态分布的倒钟形曲线的数据

如何生成将显示正态分布的倒钟形曲线的数据

本文介绍了如何生成将显示正态分布的倒钟形曲线的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码生成了遵循正态分布的随机数据:

将 numpy 导入为 np导入 matplotlib.pyplot 作为 plt将 seaborn 作为 sns 导入rng = np.random.default_rng()number_of_rows = 10000亩 = 0西格玛 = 1数据 = rng.normal(loc=mu, scale=sigma, size=number_of_rows)dist_plot_data = sns.distplot(data, hist=False)plt.show()

以上代码按预期生成以下分布图:

如果我想创建一个完全像下面这样的反向曲线的分布图,那么我如何生成随机正态分布数据?

我想要分布图将显示反向曲线的数据.如何生成此正态分布数据?

解决方案

不确定这有多大用处,但使用拒绝抽样很容易做到.从

注意概率密度必须积分为 1,所以更宽"你让它的密度越小(即,如果 x 轴上的范围是 [-4, +4],则 y 轴上的密度不能为 0.4).此外,生成 KDE 感觉不太有用,因为它会在边缘处遇到不连续性

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?

解决方案

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

这篇关于如何生成将显示正态分布的倒钟形曲线的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:53