-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPandRegressionProblem2.py
More file actions
73 lines (59 loc) · 1.87 KB
/
GPandRegressionProblem2.py
File metadata and controls
73 lines (59 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt
n = 50
a = 0.2
z1 = np.random.normal(loc=0,scale=4/3,size=1) # Munch's Randomness
# z1 = np.random.normal(loc=0,scale=1/30,size=1)
THESYSTEM = [z1]
for i in range(0,n-1):
zn = THESYSTEM[-1]
n1 = np.random.normal(loc=0,scale=1,size=1)
znp1 = a * zn + n1
THESYSTEM.append(znp1)
x = np.random.uniform(low=-2,high=2,size=50)
print(x, " Shape ", np.shape(x))
y = np.array(list(map(lambda z, x: z + x + 1, THESYSTEM, x)))
y = np.transpose(y)[0]
print(y," Shape ", np.shape(y))
""" Linear Solution """
xavg = np.average(x) # shitty covar calculation
yavg = np.average(y)
cov = np.array([],dtype=np.float64)
for (xi,yi) in zip(x,y):
cov = np.append(cov,[(xi-xavg)*(yi-yavg)])
cov = np.average(cov)
cov2 = np.cov(np.vstack((y,x)))
var = np.var(x) # determine a and b
b = cov / var
A = np.average(y)-b*np.average(x)
print("b = ",b)
print("a = ", A)
""" General Least Squares """
# X = np.vstack((x,y))
# Y = np.delete(X,(0),axis=1)
# X = np.delete(X, (-1), axis=1)
# sigma = np.cov(X) # use numpy's covar matrix before Steve's
x = np.reshape(x,(1,50))
y = np.reshape(y,(1,50))
sigma = np.fromfunction(lambda i, j: (pow(a,abs(i-j)))/(1-pow(a,2)), (50,50), dtype=np.float64)
print("x trans shape ", np.shape(np.transpose(x)), ' sigma shape ', np.shape(sigma), " x shape ", np.shape(x))
Bhat = la.inv(x @ la.inv(sigma) @ np.transpose(x) ) @ (x @ la.inv(sigma) @ np.transpose(y))
print("Bhat shape ", np.shape(Bhat))
print("Bhat ",Bhat)
Ahat = np.average(y) - Bhat*np.average(x)
# Graph it!
x = np.transpose(x)
fig1 = plt.figure(1)
plt.plot(THESYSTEM)
plt.title("Nonlinear Dynamics")
fig2 = plt.figure(2)
plt.plot(x)
plt.title("Truly Random")
fig3 = plt.figure(3)
plt.scatter(x,y)
plt.plot(x, A + b*x,"-")
plt.plot(x, Ahat + Bhat*x,"-")
plt.legend(["Naive Regression","General Regression"])
plt.title("Y")
plt.show()