Reputation:
This is a very simple problem, but for some reason I'm going wrong somewhere and I can't figure it out. I need to show that the Mean Square Deviation is biased by drawing n = 10 values from a normal distribution and repeating N = 10,000 times. sigma = 1 and mu = 0. I want to plot a histogram of the 10,000 values. This is the equation I'm trying to model in Python:
(S_b)**2 = (1/n)*(∑,i=1,n, (X_i - X_bar)**2)
Here is the Python script I have so far:
import numpy as np
from matplotlib import pyplot as plt
N = 10000
sigma = 1
mu = 0
MSD = list() # Mean Square Deviation
n = 10
x = np.random.normal(n)*sigma+mu
for i in range(N):
MSD.append((1/n)*(x[i] - np.mean(x))**2)
plt.hist(MSD,bins=50)
plt.title(r'$\overline {X}$', fontsize = 20)
plt.grid()
Right now I'm getting an error that says the float object is non-subscriptable, referring to the x[i] where I append the MSD. If I remove the [i], then I get a plot of 1 bin at 0, which is definitely not a distribution.
Where am I going wrong?
Upvotes: 0
Views: 516
Reputation: 196
After the comments in the question, I believe I have understand the problem. So you want to get n
random values and calculate the MSD of this sample N
times. After that, you want to plot the result, right?
Here is the adaptation of your code
import numpy as np
from matplotlib import pyplot as plt
N = 10000
sigma = 1
mu = 0
MSD = list() # Mean Square Deviation
n = 10
for i in range(N):
x = np.random.normal(size=n) * sigma + mu
MSD.append((1 / n) * sum((x - np.mean(x))**2))
plt.hist(MSD, bins=50)
plt.title(r'$\overline {X}$', fontsize=20)
plt.grid()
Here is the result of one execution:
Upvotes: 1