Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python numpy.random.normal

Tags:

python

numpy

I generated random 20 numbers with mean 0 and variance 1 (np.random.normal). I calculated the variance twice ddof = 1 and 0.

My question is i am trying to add (mean 0 and variance 1) to (np.random.normal), However on there website is no mention for the variance 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

So can i just do it like this

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

Because i have to perform the estimation in 90 times and 20 numbers each time and recount again

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

Here is the full code

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 )
like image 471
Star_89 Avatar asked Dec 05 '16 16:12

Star_89


People also ask

What is NumPy random normal?

The NumPy random normal() function is used to gets the random samples from a normal distribution. The NumPy random normal() function is a built-in function in NumPy package of python.

How do you generate a random normal number in Python?

An array of random Gaussian values can be generated using the randn() NumPy function. This function takes a single argument to specify the size of the resulting array. The Gaussian values are drawn from a standard Gaussian distribution; this is a distribution that has a mean of 0.0 and a standard deviation of 1.0.


1 Answers

the definition: variance = (standard deviation)^2, then standard deviation = sqrt(variance), in consequence:

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)

Output:

Unbiased_estimator :  1.08318083742
Biased_estimator   :  1.02902179555
like image 189
eyllanesc Avatar answered Oct 24 '22 13:10

eyllanesc