Python numpy.random.normal

时间:2016-12-05 16:47:46

标签: python numpy

我生成随机20个数字,均值为0,方差为1(np.random.normal)。我计算了两次ddof = 1和0的方差。

我的问题是我正在尝试将(均值0和方差1)添加到(np.random.normal),但是在那里网站上没有提到方差https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.normal.html

loc : float Mean (“centre”) of the distribution.
scale : float Standard deviation (spread or “width”) of the distribution.
size : int or tuple of ints, optional

我可以这样做吗

 mu, sigma = 0, math.sqrt(1) 
 x = np.random.normal(mu, sigma, 20)

因为我必须每次执行90次估计和20次数字再次重新计算

a = np.random.rand(90, x)

这是完整的代码

import math
import numpy as np
import pandas as pd
mu, sigma = 0, math.sqrt(1) 
x = np.random.normal(mu, sigma, 20)


#caluclateing the unbiased_estimator and the biased_estimator
unbiased_estimator = np.var(x, ddof=1)
biased_estimator = np.var(x, ddof=0)


print ("Unbiased_estimator : ",unbiased_estimator)
print ("Biased_estimator   : ", biased_estimator)

a = np.random.rand(90, x)
#caluclateing the unbiased_estimator and the biased_estimator
unbiased_estimator_for_each_20 = np.var(a, ddof=1, axis=1)
biased_estimator_for_each_20 = np.var(a, ddof=0, axis=1)

print (unbiased_estimator_for_each_20 )
print(" ")
print (biased_estimator_for_each_20 )

1 个答案:

答案 0 :(得分:4)

定义:variance = (standard deviation)^2,然后standard deviation = sqrt(variance),结果:

import numpy as np

mean = 0, 
variance = 1,
np.random.normal(loc = mean, scale= np.sqrt(variance), 20)

#caluclateing the unbiased_estimator and the biased_estimator
unbiased_estimator = np.var(x, ddof=1)
biased_estimator = np.var(x, ddof=0)


print ("Unbiased_estimator : ",unbiased_estimator)
print ("Biased_estimator   : ", biased_estimator)

输出:

Unbiased_estimator :  1.08318083742
Biased_estimator   :  1.02902179555