thecuriouscat
thecuriouscat

Reputation: 59

How to fix: frozen and None value

The problem was to create a normal distribution with mean 32 and standard deviation 4.5, setting the random seed to 1, and create a random sample of 100 elements from the above defined distribution.Finally, compute the absolute difference between the sample mean and the distribution mean.

This is some of the beginner stats problems in the course. I have had experience in Python but not in stats.

x = stats.norm(loc=32,state=4.5)
y = np.random.seed(1)
mean1 = np.mean(x)
mean2 = np.mean(y)
diff = abs(mean1 - mean2)

The error I've been encountering is x has a frozen value and y has a value of None.

Upvotes: 1

Views: 2129

Answers (1)

Thierry Lathuille
Thierry Lathuille

Reputation: 24290

random.seed(1) sets the state of the pseudorandom numbers generator so that every run of this script will give the same output - and give identical results for all students...

You need to execute this before generating your random numbers. The seed function doesn't have anything to return, so it return None. This is the default return value in Python for functions that don't return anything specific.

Then you create your sample of size 100, and calculate its mean. As it is a sample, its mean will differ from the mean of the distribution (32): we calculate the absolute difference between these means.

You can experiment with different sample sizes, and see how the difference tends towards 0 when the size of the sample grows - you'll learn more about it in your course!

from scipy.stats import norm
import numpy as np

np.random.seed(1)

distribution_mean = 32

sample = norm.rvs(loc=distribution_mean, scale=4.5, size=100)
sample_mean = np.mean(sample)
print('sample:', sample)
print('sample mean:', sample_mean)
abs_diff = abs(sample_mean - distribution_mean)
print('absolute difference:', abs_diff)

Output:

sample: [39.30955414 29.24709614 29.62322711 27.1716412  35.89433433 21.64307586
 39.85165294 28.57456895 33.43567593 30.87783331 38.57948572 22.72936681
 30.54912258 30.2717554  37.10196249 27.0504893  31.22407307 28.04963712
 32.18996186 34.62266846 27.0472137  37.15125669 36.05715824 34.26122453
 36.05385177 28.92322463 31.44699399 27.78903755 30.79450364 34.3865996
 28.88752662 30.21460913 28.90772285 28.19657461 28.97939241 31.9430093
 26.97210343 33.05487064 39.4691098  35.33919872 31.13674001 28.00566966
 28.63778768 39.6160457  32.2286349  29.13351959 32.85911968 41.45114811
 32.54071529 34.77741399 33.35076644 30.41487569 26.85866811 30.42795775
 31.05997595 34.63980436 35.77542536 36.18995937 33.28514296 35.98313524
 28.60520927 37.6379067  34.30818419 30.65858224 34.19833166 31.65992729
 37.09233224 38.83917567 41.83508933 25.71576649 25.50148788 29.72990362
 32.72016681 35.94276015 33.42035726 22.90009453 30.62208194 35.72588589
 33.03542631 35.42905031 30.99952336 31.09658869 32.83952626 33.84523241
 32.89234874 32.53553891 28.98201971 33.69903704 32.54819572 37.08267759
 37.39513046 32.83320388 30.31121772 29.12571317 33.90572459 32.34803031
 30.45265846 32.19618586 29.2099962  35.14114415]
sample mean: 32.27262283434065
absolute difference: 0.2726228343406518

Upvotes: 4

Related Questions