调整随机数列表的均值和标准差?

时间:2019-03-30 01:10:38

标签: python random mean python-3.7 normal-distribution

我必须创建一个介于-3和3之间的随机数列表(带小数)。问题是该列表的平均值必须为0,标准差必须为1。如何调整平均值和标准偏差参数?有可以使用的功能吗?

我已经能够创建一个介于-3和3之间的随机数列表。

import random


def lista_aleatorios(n):
    lista = [0] * n
    for i in range(n):
        lista[i] = random.uniform(-3, 3)
    return lista

print("\nHow many numbers do you want?: ")
n = int(input())

print (lista_aleatorios(n))

3 个答案:

答案 0 :(得分:1)

使用random.gauss,然后缩放:

import numpy as np

from random import gauss

def bounded_normal(n, mean, std, lower_bound, upper_bound):

    # generate numbers between lower_bound and upper_bound

    result = []
    for i in range(n):
        while True:
            value = gauss(mean, std)
            if lower_bound < value < upper_bound:
                break

        result.append(value)

    # modify the mean and standard deviation

    actual_mean = np.mean(result)
    actual_std = np.std(result)
    mean_difference = mean - actual_mean
    std_difference = std / actual_std
    new_result = [(element + mean_difference) * std_difference for element in result]

    return new_result

答案 1 :(得分:0)

函数random.normalvariate(mu, sigma)允许您为正态分布的随机变量指定均值和标准差。

答案 2 :(得分:0)

好的,这是解决问题的快速方法(如果您想使用截断的高斯)。设置边界和所需的stddev。我假设均值为0。然后使用粗鲁的代码对分布sigma进行二进制搜索,求解非线性根(在生产代码中应使用brentq())。所有公式均来自Truncated Normal的Wiki页面。由于以下事实,它(sigma)应大于所需的stddev:截断会删除会导致较大stddev的随机值。然后,我们进行快速采样测试-均值和标准差接近期望值,但从不完全等于它们。代码(Python-3.7,Anaconda,Win10 x64)

import numpy as np
from scipy.special import erf
from scipy.stats import truncnorm

def alpha(a, sigma):
    return a/sigma

def beta(b, sigma):
    return b/sigma

def xi(x, sigma):
    return x/sigma

def fi(xi):
    return 1.0/np.sqrt(2.0*np.pi) * np.exp(-0.5*xi*xi)

def Fi(x):
    return 0.5*(1.0 + erf(x/np.sqrt(2.0)))

def Z(al, be):
    return Fi(be) - Fi(al)

def Variance(sigma, a, b):
    al = alpha(a, sigma)
    be = beta(b, sigma)
    ZZ = Z(al, be)

    return sigma*sigma*(1.0 + (al*fi(al) - be*fi(be))/ZZ - ((fi(al)-fi(be))/ZZ)**2)

def stddev(sigma, a, b):
    return np.sqrt(Variance(sigma, a, b))

m = 0.0 # mean
s =  1.0 # this is what we want
a = -3.0 # left boundary
b =  3.0 # right boundary

#print(stddev(s , a, b))
#print(stddev(s + 0.1, a, b))

slo = 1.0
shi = 1.1

stdlo = stddev(slo, a, b)
stdhi = stddev(shi, a, b)

sigma = -1.0
while True: # binary search for sigma
    sme = (slo + shi) / 2.0
    stdme = stddev(sme, a, b)
    if stdme - s == 0.0:
        sigma = stdme
        break
    elif stdme - s < 0.0:
        slo = sme
    else:
        shi = sme

    if shi - slo < 0.0000001:
        sigma = (shi + slo) / 2.0
        break

print(sigma) # we got it, shall be slightly bigger than s, desired stddev

np.random.seed(73123457)

rvs = truncnorm.rvs(a, b, loc=m, scale=sigma, size=1000000) # quick sampling test

print(np.mean(rvs))
print(np.std(rvs))

对我来说是印刷的

sigma = 1.0153870105743408
mean = -0.000400729471992301
stddev = 1.0024267696681475

使用不同的种子或序列长度,您可能会得到类似

的输出
1.0153870105743408
-0.00015923177289006116
0.9999974266369461