-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcdemo.py
97 lines (82 loc) · 2.59 KB
/
tcdemo.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import numpy as np
import econlearn.tilecode as tc
import matplotlib.pyplot as plt
import pylab
import results.chartbuilder as cb
import sklearn.linear_model as sk
"""
x = np.random.rand(150)
y = np.maximum(-0.5 + x, 0) + np.random.rand(150)*.2
X = x.reshape([150, 1])
minx = min(X)
maxx = max(X)
XS = np.linspace(minx, maxx, 200).reshape([200,1])
home = '/home/nealbob'
out = '/Dropbox/Thesis/IMG/chapter8/'
img_ext = '.pdf'
print 'The noise of a high resolution grid'
test = tc.Tilecode(1, [60], 1, min_sample=1, offset='uniform', lin_spline=True, linT=5)
test.fit(X, y)
yhat = test.predict(XS)
pylab.figure()
pylab.plot(x, y, '+')
pylab.plot(XS, yhat)
pylab.savefig(home + out + 'noisytile' + img_ext, bbox_inches='tight')
pylab.show()
print 'The bias of a low resolution grid'
test = tc.Tilecode(1, [5], 1, min_sample=1, offset='uniform', lin_spline=True, linT=5)
test.fit(X, y)
yhat = test.predict(XS)
plt.plot(x, y, '+')
plt.plot(XS, yhat)
pylab.savefig(home + out + 'biasedtile' + img_ext, bbox_inches='tight')
plt.show()
print 'High resolution tilecoding'
test = tc.Tilecode(1, [3], 40, min_sample=1, offset='uniform', lin_spline=True, linT=5)
test.fit(X, y, sgd=True, asgd=False, eta=0.3, n_iters=10, scale=0)
yhat = test.predict(XS)
#test2 = tc.Tilecode(1, [5], 30, min_sample=1, offset='uniform', lin_spline=True, linT=5)
#test2.fit(X, y) #sgd=True, eta=0.5, n_iters=10, scale=0)
#yhat2 = test2.predict(XS)
plt.plot(x, y, '+')
plt.plot(XS, yhat)
pylab.savefig(home + out + 'goodtile' + img_ext, bbox_inches='tight')
#plt.plot(XS, yhat2)
plt.show()
print 'The bias of averaging'
test = tc.Tilecode(1, [2], 50, min_sample=1, offset='uniform', lin_spline=True, linT=5)
test.fit(X, y)
yhat = test.predict(XS)
plt.plot(x, y, '+')
plt.plot(XS, yhat)
pylab.savefig(home + out + 'averagetile' + img_ext, bbox_inches='tight')
plt.show()
print 'A linear model'
test = sk.LinearRegression()
test.fit(X, y)
yhat = test.predict(XS)
plt.plot(x, y, '+')
plt.plot(XS, yhat)
plt.ylim(0, 0.7)
pylab.savefig(home + out + 'lineartile' + img_ext, bbox_inches='tight')
plt.show()
"""
from econlearn import TilecodeRegressor as TR
tr = TR(2, [10,10], 15, offset='uniform')
X = np.array(tr.tile.offset)
x1 = X[:, 0]
x2 = X[:, 1]
pylab.scatter(x1, x2)
pylab.xlim(0, 1)
pylab.ylim(0, 1)
pylab.savefig('/home/nealbob/Dropbox/Thesis/IMG/appendixB/uniform.pdf', bbox_inches='tight')
pylab.show()
tr = TR(2, [10,10], 15, offset='optimal')
X = np.array(tr.tile.offset)
x1 = X[:, 0]
x2 = X[:, 1]
pylab.scatter(x1, x2)
pylab.xlim(0, 1)
pylab.ylim(0, 1)
pylab.savefig('/home/nealbob/Dropbox/Thesis/IMG/appendixB/optimal.pdf', bbox_inches='tight')
pylab.show()