import matplotlib.pyplot as plt
import numpy as np
%config InlineBackend.figure_format='retina'
lets create random points, in a linear way with noise
$ y = mx+b$, I am going to write it like $y = \beta x + \alpha $
b = np.random.normal(10,3)
a = np.random.normal(0,3)
def func(x):
noise = np.random.normal(0,1)
return b*x+a+noise
X = list(np.linspace(0,1,50))
Y =[]
for x in X:
Y.append(func(x))
plt.plot(X,Y,'x')
$$ y_i = \alpha + \beta x_i + \epsilon_i $$
The Least Squares estimates, slope
$$ \beta = \frac{\sum (y_i - \bar{y})(x_i - \bar{x})}{ \sum (x_i - \bar{x})^2 } $$
and intercept is
$$ \alpha = \bar{y} - \beta \bar{x} $$
Xmean=np.mean(X)
Ymean=np.mean(Y)
Ymean
Top=0
Bottom=0
for c in range(0,len(X)):
Top +=(Y[c]-Ymean)*(X[c]-Xmean)
Bottom += (X[c]-Xmean)**2
B = Top/Bottom
A = Ymean - B*Xmean
L=[]
for x in X:
L.append(x*B+A)
plt.plot(X,Y,'x')
plt.plot(X,L,c='r')
XX = np.linspace(0,1,50)
def func_line(x):
return x*B+A
plt.plot(X,Y,'x')
plt.plot(XX,func_line(XX),c='r')
plt.title('Linear Regression line')
plt.show()
print('we originally picked the value a =',a,': But our linear regression gave us A=',A)
print('we originally picked the value b =',b,': But our linear regression gave us B=',B)
b = np.random.normal(10,3)
a = np.random.normal(0,3)
n=500
noise_level = 0.5
def func(x):
noise = np.random.normal(0,noise_level)
return b*x+a+noise
X = list(np.linspace(0,1,n))
Y =[]
for x in X:
Y.append(func(x))
Xmean=np.mean(X)
Ymean=np.mean(Y)
Top=0
Bottom=0
for c in range(0,len(X)):
Top +=(Y[c]-Ymean)*(X[c]-Xmean)
Bottom += (X[c]-Xmean)**2
B = Top/Bottom
A = Ymean - B*Xmean
L=[]
for x in X:
L.append(x*B+A)
plt.plot(X,Y,'x',alpha = 0.2)
plt.plot(X,L,c='r')
plt.title('Linear Regression line')
plt.show()
print('we originally picked the value a =',a,': But our linear regression gave us A=',A)
print('we originally picked the value b =',b,': But our linear regression gave us B=',B)
Fitting = np.polyfit(X,Y,1)
Fitting
Fitting = np.polyfit
a = np.random.normal(10,3)
b = np.random.normal(0,3)
c = np.random.normal(5,1)
n=20
noise_level = 0.5
Lets create the parabola with noise
def func(x):
noise = np.random.normal(0,noise_level)
return a*x**2+b*x+c+noise
X = list(np.linspace(-1,1,n))
Y =[]
for x in X:
Y.append(func(x))
plt.plot(X,Y,'x')
my_fit= np.polyfit(X,Y,2)
my_fit
my_func_fit = np.poly1d(my_fit)
my_func_fit(-0.25)
plt.plot(X,Y,'x')
plt.plot(X,my_func_fit(X),color = 'r')
plt.title('curve fitting, parabola')
plt.show()
a = np.random.normal(10,3)
b = np.random.normal(0,3)
c = np.random.normal(5,1)
n=2000
noise_level = 2
X = list(np.linspace(-1,1,n))
Y =[]
for x in X:
Y.append(func(x))
my_fit= np.polyfit(X,Y,2)
my_func_fit = np.poly1d(my_fit)
plt.plot(X,Y,'x',alpha = 0.2)
plt.plot(X,my_func_fit(X),color = 'r')
plt.title('curve fitting, parabola')
plt.show()
print(my_func_fit.coef)
print(a,b,c)
dataX = np.random.random(10)
dataX
np.histogram(dataX)
my_histo = np.histogram(dataX)
my_histo[1]
plt.plot(my_histo[1][:-1],my_histo[0],'.')
dataX = np.random.random(100000)
my_histo = np.histogram(dataX,10)
plt.plot(my_histo[1][:-1],my_histo[0],'.-')
plt.ylim(0,(max(my_histo[0])*1.1))
plt.show()
dataX = np.random.normal(0,1,10000000)
my_histo = np.histogram(dataX,100)
plt.plot(my_histo[1][:-1],my_histo[0],'.-')
plt.ylim(0,(max(my_histo[0])*1.1))
plt.show()
plt.hist(dataX,100)
plt.show()
dataX = np.random.normal(0,1,1000000)
dataY = np.random.normal(1,2,1000000)
plt.hist(dataX,100,alpha=0.2)
plt.hist(dataY,100,alpha=0.2, color='r')
plt.show()
n= 10000
breaks = 100
stacks = np.zeros(2*breaks+1)
for ball in range(0,n):
position = breaks
for bounce in range(0,breaks):
if np.random.random()>=0.5:
position +=1
else:
position +=-1
stacks[position] +=1
X=list(range(0,2*breaks+1))
plt.plot(X,stacks,'.')
plt.show()
stacks
%%time
n= 100000
breaks = 10
stacks = np.zeros(2*breaks+1)
for ball in range(0,n):
position = breaks
for bounce in range(0,breaks):
position +=np.random.choice([-1,0,1])
stacks[position] +=1
X=list(range(0,2*breaks+1))
plt.plot(X,stacks,'.-')
plt.show()
%%time
n= 1000000
breaks = 10
stacks = np.zeros(2*breaks+1)
position = np.ones(n)*breaks
for bounce in range(0,breaks):
position +=np.random.choice([-1,0,1],n)
for x in position:
stacks[int(x)] +=1
X=list(range(0,2*breaks+1))
plt.plot(X,stacks,'.-')
plt.show()
stacks
np.random.choice([-1,0,1],n)