Means of samples taken from a population is normal distributed.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from numpy import random
import math
#6000 random samples of dimension 6 from 1 to 40
#same probability
samples = np.random.randint(1, 40 + 1, size=(6000,6))
#means of samples
means = np.mean(samples,axis=1)
unique_elements, counts_elements = np.unique(means, return_counts=True)
fv = np.asarray((unique_elements, counts_elements))
x = fv[0]
y = fv[1]
plt.scatter(x,y)
plt.show()
mu = np.mean(means)
sigma = np.std(means)
print("miu-mean",mu,"sigma-stdev", sigma)
#normal distribution function
def normal(x):
return ( 1/(sigma * np.sqrt(2*np.pi)) ) * np.exp(-1/2 * np.power((x-mu)/sigma,2) )
x = np.arange(1, 41)
y = normal(x)
plt.plot(x, y)
plt.show()
miu-mean 20.49169444444444 sigma-stdev 4.674894581814679
miu-mean 20.49169444444444 sigma-stdev 4.674894581814679
#generating normal distribution samples of size...
s = random.normal(loc=mu, scale=sigma, size=(3, 6))
print(s)
s = random.normal(loc=mu, scale=sigma, size=(6))
print(s)
for i in range(len(s)):
s[i] = int(round(s[i]))
print(sorted(s),"-->",np.mean(s))
[[20.55804827 22.65994093 17.529906 19.81501237 28.92938079 28.61580378] [10.06561594 16.70940515 21.69396461 21.92751652 18.65745382 28.06123269] [22.76194981 23.29725248 14.2024344 16.76480874 14.45765157 20.24139894]] [21.30721342 19.54410009 19.85705537 22.34844031 22.95314155 18.85774149] [19.0, 20.0, 20.0, 21.0, 22.0, 23.0] --> 20.833333333333332
#6000 random samples of dimension 4 from 1 to 10
#different probabilities
samples2 = np.random.choice(10, p=[0.11,0.03,0.085,0.2,0.05,0.083,0.07,0.141,0.220,0.011],size=(6000,4))
#means
means = np.mean(samples2,axis=1)
unique_elements, counts_elements = np.unique(means, return_counts=True)
plt.scatter(unique_elements, counts_elements)
plt.show()
1 to 40 random numbers from database
import pandas as pd
df=pd.read_csv('arhiva.txt', sep='\t',header=None)
#print(df.head(2))
arr = df.to_numpy()
#remove 1st column - data
arr = arr[:,[1,2,3,4,5,6]]
records = arr.shape[0]
#remove header
arr = arr[1:records,:]
#strings to int
df = pd.DataFrame(arr)
df = df.astype(int)
arr = df.to_numpy()
arr.shape
(1942, 6)
means = np.mean(samples,axis=1)
unique_elements, counts_elements = np.unique(means, return_counts=True)
plt.scatter(unique_elements, counts_elements)
plt.show()
mu = np.mean(means)
sigma = np.std(means)
print("miu-mean",mu,"sigma-stdev", sigma)
#normal distribution function
def normal(x):
return ( 1/(sigma * np.sqrt(2*np.pi)) ) * np.exp(-1/2 * np.power((x-mu)/sigma,2) )
x = np.arange(1, 41)
y = normal(x)
plt.plot(x, y)
plt.show()
miu-mean 20.49169444444444 sigma-stdev 4.674894581814679
unique_elements2, counts_elements2 = np.unique(arr, return_counts=True)
plt.scatter(unique_elements2, counts_elements2)
plt.show()
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(unique_elements2, counts_elements2)
plt.show()