In [385]:
import math
import numpy as np
import matplotlib.pyplot as plt
def norm(m,s,n):
return np.random.normal(m, s,n)
def exp(x):
return np.exp(x)
def dot(x,y):
return np.dot(x,y)

N=100 #size of data

In [386]:
def h(theta,x):
z = []
for d in range(0,len(theta)):
z.append(np.power(x,d))
return dot(theta,z)

def powers(x,D):
X=[]
for d in range(0,D):
X.append(np.power(x,d))
return np.array(X)

In [387]:
x=abs(norm(0., 2., N))
y=exp(-x+ norm(0.,0.1,N))

In [388]:
plt.plot(x, y, 'b.',t, np.exp(-1*t), 'g-')
plt.axis([0, 6, 0, 2])
plt.show()

In [394]:
D=1
theta=np.zeros(D)
alpha=0.001#np.power(0.01,D)
for t in range(0,1000):
for i, yi in enumerate(y):
X=powers(x[i],len(theta))
theta = theta - alpha*(h(theta,x[i]) - yi)*X
print(theta)

t = np.arange(0., 5., 0.2)
plt.plot(x, y, 'b.',t, h(theta,t), 'r-')
plt.axis([0, 6, 0, 2])
plt.show()

[ 0.35604351]

In [395]:
D=2
theta=np.array(theta)
theta=np.append(theta, 0)
alpha=0.001#np.power(0.01,D)
for t in range(0,1000):
for i, yi in enumerate(y):
X=powers(x[i],len(theta))
theta = theta - alpha*(h(theta,x[i]) - yi)*X
print(theta)

t = np.arange(0., 5., 0.2)
plt.plot(x, y, 'b.',t, h(theta,t), 'r-')
plt.axis([0, 6, 0, 2])
plt.show()

[ 0.68584989 -0.22254948]

In [396]:
D=3
theta=np.array(theta)
theta=np.append(theta, 0)
alpha=0.01#np.power(0.01,D)
for t in range(0,1000):
for i, yi in enumerate(y):
X=powers(x[i],len(theta))
theta = theta - alpha*(h(theta,x[i]) - yi)*X
print(theta)

t = np.arange(0., 5., 0.2)
plt.plot(x, y, 'b.',t, h(theta,t), 'r-')
plt.axis([0, 6, 0, 2])
plt.show()

[ 0.8745676  -0.55097359  0.08866483]

In [397]:
D=4
theta=np.array(theta)
theta=np.append(theta, 0)
alpha=0.0001#np.power(0.01,D)
for t in range(0,1000):
for i, yi in enumerate(y):
X=powers(x[i],len(theta))
theta = theta - alpha*(h(theta,x[i]) - yi)*X
print(theta)

t = np.arange(0., 5., 0.2)
plt.plot(x, y, 'b.',t, h(theta,t), 'r-')
plt.axis([0, 6, 0, 2])
plt.show()

[ 0.87495996 -0.58860182  0.12421817 -0.00738555]

In [436]:
import csv