# Probability Density Function (pdf) of Normal Distribution is
# f(x) = 1/(sqrt(2*pi)) * exp(-x^2/2)
plot(1/(sqrt(2*pi)) * exp(-x^2/2), x, -5,5)
N(integral(1/(sqrt(2*pi)) * exp(-x^2/2), (x, 0, 0.5)))
# prob that normally distributed random variable
# has value in (0,0.5)
# N(-) function gives numerical approximation
N(integral(1/(sqrt(2*pi)) * exp(-x^2/2), (x, -10, 10)), digits=30)
# prob value is in (-10,10)
# almost exactly 1
integral(1/(sqrt(2*pi)) * exp(-x^2/2), (x, -10, 10))
# the exactly value is not a nice number
integral(1/(sqrt(2*pi)) * exp(-x^2/2), (x, -oo, oo))
# prob that value is in (-oo, oo), which should be 1
# expected value E(X)
integral(x * 1/(sqrt(2*pi)) * exp(-x^2/2), (x,-oo,oo))
# variance
integral(x^2 * 1/(sqrt(2*pi)) * exp(-x^2/2), (x,-oo,oo))
#"standard normal distribution" has expected value = 1, variance = 1
# Code copied from last time
# Use uniform random numbers between -1 and 1
import matplotlib.pyplot as plt # python package that has histogram function
num_iters = 100000
n = 1
results = [] # will store the result for all the iterations
for j in range(num_iters):
# generate random numbers, and take average
X = [] # list of X_i
for i in range(n):
rand = 2*random()-1 # uniform rand number between -1 and 1
X.append(rand)
Sn = sum(X) # sum of all X_i
ave = Sn/n # average of the X_i
results.append(N(ave))
# print(results)
plt.hist(results,bins=31)
# above is num_iters instances of a single realization
# of a uniform (-1,1) random variable
# above should approximate pdf if num_iters is high
# The actual pdf would be a straight horizontal line above (-1,1)
# Use uniform random numbers between -1 and 1
import matplotlib.pyplot as plt # python package that has histogram
num_iters = 100000
n = 10 # number of random variables to sum up
results = [] # will store the result for all the iterations
for j in range(num_iters):
# generate random numbers, and take average
X = [] # list of X_i
for i in range(n):
rand = 2*random()-1
X.append(rand)
Sn = sum(X) # sum of all X_i
ave = Sn/n
results.append(N(ave))
# print(results)
plt.hist(results,bins=51)
# By Law of Large numbers, as n gets biggers,
# (1/n)(X_1+ ...+X_n) is very likely to be close to 0
# Instead of taking average by dividing by n,
# now we will divide by sqrt(n)
# Use uniform random numbers between -1 and 1
import matplotlib.pyplot as plt # python package that has histogram
num_iters = 100000
n = 15
results = [] # will store the result for all the iterations
for j in range(num_iters):
# generate random numbers, and take average
X = []
for i in range(n):
rand = 2*random()-1
X.append(rand)
Sn = sum(X) # sum of all X_i
rescaled_ave = Sn/sqrt(n) # now dividing by sqrt(n), rather than n
results.append(N(rescaled_ave))
# print(results)
plt.hist(results,bins=51)
# Central Limit Theorem
# For any X distribution (with E(X)=0, E(X^2)=1), we have that
# (1/sqrt(n))(X_1+X_2 + ... +X_n)
# converges to standard normal distribution (E(X)=0, E(X^2)=1)
# as n to infinity
# X_i are *independent* instances of X