-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMachine Learning-based pairs trading strategy_refractored.py
709 lines (534 loc) · 24.3 KB
/
Machine Learning-based pairs trading strategy_refractored.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
"""****************************
Pairs Trading Strategy
****************************"""
import numpy as np
import statsmodels.api as sm
import pandas as pd
from zipline.utils import tradingcalendar
import pytz
#from mpl_finance import candlestick_ohlc as quotes_historical_yahoo #
from yahoo_finance import Share
def initialize(context):
print("initialize----------------------------------------------------1")
# Quantopian backtester specific variables
set_symbol_lookup_date('2014-01-01')
set_slippage(slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1))
set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1))
context.stock_pairs = [(symbol('ABGB'), symbol('FSLR')),
(symbol('CSUN'), symbol('ASTI')),
(symbol('KO'), symbol('PEP')),
(symbol('AAPL'), symbol('IBM')),
(symbol('FB'), symbol('YHOO')),
(symbol('TWTR'), symbol('YHOO'))]
context.stocks = [symbol('ABGB'), symbol('FSLR'), symbol('CSUN'), symbol('ASTI'),\
symbol('KO'), symbol('PEP'), symbol('AAPL'), symbol('IBM'), symbol('FB'),\
symbol('YHOO'),symbol('TWTR')]
context.num_pairs = len(context.stock_pairs)
# strategy specific variables
context.lookback = 20 # used for regression
context.z_window = 20 # used for zscore calculation, must be <= lookback
context.spread = np.ndarray((context.num_pairs, 0))
# context.hedgeRatioTS = np.ndarray((context.num_pairs, 0))
context.inLong = [False] * context.num_pairs
context.inShort = [False] * context.num_pairs
schedule_function(func=check_pair_status, date_rule=date_rules.every_day(),\
time_rule=time_rules.market_close(minutes=90))
def check_pair_status(context, data):
print("check_pair_status----------------------------------------------------1")
if get_open_orders():
return
prices = data.history(context.stocks, fields='price', bar_count=35, frequency='1d').\
iloc[-context.lookback::]
new_spreads = np.ndarray((context.num_pairs, 1))
for i in range(context.num_pairs):
(stock_y, stock_x) = context.stock_pairs[i]
Y = prices[stock_y]
X = prices[stock_x]
try:
hedge = hedge_ratio(Y, X, add_const=True)
record(hedge=hedge)
except ValueError as e:
log.debug(e)
return
# context.hedgeRatioTS = np.append(context.hedgeRatioTS, hedge)
new_spreads[i, :] = Y[-1] - hedge * X[-1]
if context.spread.shape[1] > context.z_window:
# Keep only the z-score lookback period
spreads = context.spread[i, -context.z_window:]
zscore = (spreads[-1] - spreads.mean()) / spreads.std()
record(zscore=zscore)
if context.inShort[i] and zscore < 0.0:
order_target(stock_y, 0)
order_target(stock_x, 0)
context.inShort[i] = False
context.inLong[i] = False
record(X_pct=0, Y_pct=0)
return
if context.inLong[i] and zscore > 0.0:
order_target(stock_y, 0)
order_target(stock_x, 0)
context.inShort[i] = False
context.inLong[i] = False
record(X_pct=0, Y_pct=0)
return
if zscore < -1.0 and (not context.inLong[i]):
# Only trade if NOT already in a trade
y_target_shares = 1 #long y
X_target_shares = -hedge #short x
context.inLong[i] = True
context.inShort[i] = False
(y_target_pct, x_target_pct) = computeHoldingsPct( y_target_shares,X_target_shares, Y[-1], X[-1] )
order_target_percent( stock_y, y_target_pct * (1.0/context.num_pairs) )
order_target_percent( stock_x, x_target_pct * (1.0/context.num_pairs) )
record(Y_pct=y_target_pct, X_pct=x_target_pct)
return
if zscore > 1.0 and (not context.inShort[i]):
# Only trade if NOT already in a trade
y_target_shares = -1 #short y
X_target_shares = hedge #long x
context.inShort[i] = True
context.inLong[i] = False
(y_target_pct, x_target_pct) = computeHoldingsPct( y_target_shares, X_target_shares, Y[-1], X[-1] )
order_target_percent( stock_y, y_target_pct * (1.0/context.num_pairs))
order_target_percent( stock_x, x_target_pct * (1.0/context.num_pairs))
record(Y_pct=y_target_pct, X_pct=x_target_pct)
context.spread = np.hstack([context.spread, new_spreads])
def hedge_ratio(Y, X, add_const=True):
print("hedge_ratio----------------------------------------------------1")
if add_const:
X = sm.add_constant(X)
model = sm.OLS(Y, X).fit()
return model.params[1]
model = sm.OLS(Y, X).fit()
return model.params.values
def computeHoldingsPct(yShares, xShares, yPrice, xPrice):
print("computeHoldingsPct----------------------------------------------------1")
yDol = yShares * yPrice
xDol = xShares * xPrice
notionalDol = abs(yDol) + abs(xDol)
y_target_pct = yDol / notionalDol
x_target_pct = xDol / notionalDol
return (y_target_pct, x_target_pct)
def handle_data(context, data):
print("handle_data----------------------------------------------------1")
pass
"""****************************
Long Short Strategy
****************************"""
import numpy as np
import pandas as pd
from zipline.api import attach_pipeline, pipeline_output
import zipline.pipeline.pipeline as Pipeline #from quantopian.pipeline import Pipeline
from zipline.pipeline.factors.basic import CustomFactor, SimpleMovingAverage, AverageDollarVolume
from zipline.pipeline.data import USEquityPricing
#from quantopian.pipeline.data import morningstar
import zipline.pipeline.filters.filter#from quantopian.pipeline.filters import Q1500US
# Constraint Parameters
NUM_LONG_POSITIONS = 5
NUM_SHORT_POSITIONS = 5
class Momentum(CustomFactor):
print("Momentum----------------------------------------------------1")
inputs = [USEquityPricing.close]
window_length = 252
def compute(self, today, assets, out, prices):
out[:] = ((prices[-21] - prices[-252])/prices[-252] -
(prices[-1] - prices[-21])/prices[-21])
def make_pipeline():
print("make_pipeline----------------------------------------------------1")
# define alpha factors
momentum = Momentum()
growth = morningstar.operation_ratios.revenue_growth.latest
pe_ratio = morningstar.valuation_ratios.pe_ratio.latest
# Screen out non-desirable securities by defining our universe.
mkt_cap_filter = morningstar.valuation.market_cap.latest >= 500000000
price_filter = USEquityPricing.close.latest >= 5
universe = Q1500US() & price_filter & mkt_cap_filter & \
momentum.notnull() & growth.notnull() & pe_ratio.notnull()
combined_rank = (
momentum.rank(mask=universe).zscore() +
growth.rank(mask=universe).zscore() +
pe_ratio.rank(mask=universe).zscore()
)
longs = combined_rank.top(NUM_LONG_POSITIONS)
shorts = combined_rank.bottom(NUM_SHORT_POSITIONS)
long_short_screen = (longs | shorts)
# Create pipeline
pipe = Pipeline(columns = {
'longs':longs,
'shorts':shorts,
'combined_rank':combined_rank,
'momentum':momentum,
'growth':growth,
'pe_ratio':pe_ratio
},
screen = long_short_screen)
return pipe
def initialize(context):
print("initialize----------------------------------------------------1")
set_slippage(slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1))
set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1))
attach_pipeline(make_pipeline(), 'long_short_factors')
# Schedule my rebalance function
schedule_function(func=rebalance,
date_rule=date_rules.month_start(),
time_rule=time_rules.market_open(hours=1,minutes=30),
half_days=True)
# record my portfolio variables at the end of day
schedule_function(func=recording_statements,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_close(),
half_days=True)
def before_trading_start(context, data):
print("before_trading_start----------------------------------------------------1")
# Call pipeline_output to get the output
context.output = pipeline_output('long_short_factors')
context.longs = context.output[context.output['longs']].index.tolist()
context.shorts = context.output[context.output['shorts']].index.tolist()
context.long_weight, context.short_weight = assign_weights(context)
# These are the securities that we are interested in trading each day.
context.security_list = context.output.index
def assign_weights(context):
print("assign_weights----------------------------------------------------1")
"""
Assign weights to securities that we want to order.
"""
long_weight = 0.5 / len(context.longs)
short_weight = -0.5 / len(context.shorts)
return long_weight, short_weight
def rebalance(context, data):
print("rebalance----------------------------------------------------1")
for security in context.portfolio.positions:
if security not in context.longs and \
security not in context.shorts and data.can_trade(security):
order_target_percent(security, 0)
for security in context.longs:
if data.can_trade(security):
order_target_percent(security, context.long_weight)
for security in context.shorts:
if data.can_trade(security):
order_target_percent(security, context.short_weight)
def recording_statements(context, data):
print("recording_statements----------------------------------------------------1")
# Check how many long and short positions we have.
longs = shorts = 0
for position in context.portfolio.positions.itervalues():
if position.amount > 0:
longs += 1
elif position.amount < 0:
shorts += 1
# Record our variables.
record(leverage=context.account.leverage, long_count=longs, short_count=shorts)
"""****************************
Stochastic Volatility
****************************"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas_datareader import data
import pymc3 as pm
np.random.seed(0)
def main():
print("main----------------------------------------------------1")
#load data
returns = data.get_data_yahoo('SPY', start='2008-5-1', end='2009-12-1')['Close'].pct_change()
returns.plot()
plt.ylabel('daily returns in %');
with pm.Model() as sp500_model:
print("pm.Model----------------------------------------------------1")
nu = pm.Exponential('nu', 1./10, testval=5.0)
sigma = pm.Exponential('sigma', 1./0.02, testval=0.1)
s = pm.GaussianRandomWalk('s', sigma**-2, shape=len(returns))
r = pm.StudentT('r', nu, lam=pm.math.exp(-2*s), observed=returns)
with sp500_model:
print("sp500_model----------------------------------------------------1")
trace = pm.sample(2000)
pm.traceplot(trace, [nu, sigma]);
plt.show()
plt.figure()
returns.plot()
plt.plot(returns.index, np.exp(trace['s',::5].T), 'r', alpha=.03)
plt.legend(['S&P500', 'stochastic volatility process'])
plt.show()
if __name__ == "__main__":
main()
"""****************************
Recurrent Neural Network
****************************"""
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from pandas_datareader import data
from datetime import datetime
import pytz
import matplotlib.pyplot as plt
np.random.seed(0)
def create_dataset(dataset, look_back=1):
print("create_dataset----------------------------------------------------1")
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
dataX.append(dataset[i:(i+look_back),0])
dataY.append(dataset[i+look_back,0])
return np.array(dataX), np.array(dataY)
if __name__ == "__main__":
#load data
start = datetime(2015, 1, 1, 0, 0, 0, 0, pytz.utc)
end = datetime(2016, 1, 1, 0, 0, 0, 0, pytz.utc)
spy = data.DataReader("SPY", "yahoo", start, end)
dataset = np.array(spy['Close'].values).reshape(-1,1)
dataset = dataset.astype('float32')
scaler = MinMaxScaler(feature_range=(0,1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape for look_back
look_back = 10
X_train, y_train = create_dataset(train, look_back)
X_test, y_test = create_dataset(test, look_back)
# reshape for LSTM [samples, time steps, features]
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
# LSTM
model = Sequential()
model.add(LSTM(32, input_dim=1)) #look_back))
model.add(Dense(1))
print(str(X_train.shape)+"1")
print(str(y_train.shape)+"2")
print(str(X_test.shape)+"3")
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train, y_train, nb_epoch=100, batch_size=5, verbose=2)
train_pred = model.predict(X_train)
test_pred = model.predict(X_test)
# scale back
train_pred = scaler.inverse_transform(train_pred)
#y_train = scaler.inverse_transform(y_train)
test_pred = scaler.inverse_transform(test_pred)
#y_test = scaler.inverse_transform(y_test)
print("Yes")
# shift predictions for plotting
train_pred_plot = np.empty_like(dataset)
train_pred_plot[:,:] = np.nan
train_pred_plot[look_back:len(train_pred)+look_back,:] = train_pred
test_pred_plot = np.empty_like(dataset)
test_pred_plot[:,:] = np.nan
test_pred_plot[len(train_pred)+(look_back*2)+1:len(dataset)-1,:] = test_pred
print("Yes2")
f = plt.figure()
plt.plot(scaler.inverse_transform(dataset), color='b', lw=2.0, label='S&P 500')
plt.plot(train_pred_plot, color='g', lw=2.0, label='LSTM train')
plt.plot(test_pred_plot, color='r', lw=2.0, label='LSTM test')
plt.legend(loc=3)
plt.grid(True)
f.savefig('./lstm.png')
"""****************************
Stock Clusters
****************************"""
import numpy as np
import pandas as pd
from scipy import linalg
from datetime import datetime
import pytz
from sklearn.datasets import make_sparse_spd_matrix
#from sklearn.covariance import GraphicalLassoCV, ledoit_wolf
from sklearn.covariance import GraphLassoCV,ledoit_wolf
from sklearn.preprocessing import StandardScaler
from sklearn import cluster, manifold
import seaborn as sns
import matplotlib.pyplot as plt
import quandl
#from mpl_finance import
#from matplotlib.finance import quotes_historical_yahoo_ochl as quotes_historical_yahoo
from matplotlib.collections import LineCollection
np.random.seed(0)
if __name__ == "__main__":
print("475 main----------------------------------------------------1")
num_samples = 60
num_features = 20
#generate data (synthetic)
#prec = make_sparse_spd_matrix(num_features, alpha=0.95, smallest_coef=0.4, largest_coef=0.7)
#cov = linalg.inv(prec)
#X = np.random.multivariate_normal(np.zeros(num_features), cov, size=num_samples)
#X = StandardScaler().fit_transform(X)
#generate data (actual)
STOCKS = {
'SPY': 'S&P500',
'LQD': 'Bond_Corp',
'TIP': 'Bond_Treas',
'GLD': 'Gold',
'MSFT': 'Microsoft',
'XOM': 'Exxon',
'AMZN': 'Amazon',
'BAC': 'BofA',
'NVS': 'Novartis'}
symbols, names = np.array(list(STOCKS.items())).T
start = datetime(2014, 1, 1, 0, 0, 0, 0, pytz.utc)
end = datetime(2016, 1, 1, 0, 0, 0, 0, pytz.utc)
"""quotes = [quotes_historical_yahoo(symbol, start, end, asobject=True) for symbol in symbols]"""
"""yahoo = Share('YHOO')
print yahoo.get_open()
'36.60'
print yahoo.get_price()
'36.84'
"""
#mydata = quandl.get("FRED/GDP", start_date="2001-12-25", end_date="2005-12-31")
#mydata = quandl.get("BATS/BATS_"+symbol, start_date="2011-12-25", end_date="2015-12-31")
#quotes = [Share(symbol).get_historical(start, end) for symbol in symbols]
#quotes = [quandl.get("BATS/BATS_"+symbol, start_date=start, end_date=end,open) for symbol in symbols]
quandl.ApiConfig.api_key = "2bqtLaCsSnsM2XR3tCxU"
"""for symbol in sum
data = quandl.get_table(
'WIKI/PRICES', qopts = { 'columns': ['ticker', 'date', 'open'] },
ticker = ['AAPL', 'MSFT'], date = { 'gte': '2016-01-01', 'lte': '2016-12-31' })
print(data['open'])"""
data = pd.DataFrame()
data = quandl.get_table(
'WIKI/PRICES', qopts = { 'columns': ['ticker', 'date', 'open','close'] },
ticker = [symbols], date = { 'gte': start, 'lte': end })
#print(data.open.iloc[1:504])
data_open = pd.concat([data.open.iloc[:504].reset_index(),
data.open.iloc[504:1008].reset_index(),
data.open.iloc[1008:1512].reset_index(),
data.open.iloc[1512:2016].reset_index()],axis=1)
data_open = data_open.drop("None",axis=1)
data_close = pd.concat([data.close.iloc[:504].reset_index(),
data.close.iloc[504:1008].reset_index(),
data.close.iloc[1008:1512].reset_index(),
data.close.iloc[1512:2016].reset_index()],axis=1)
data_close = data_close.drop("None",axis=1)
#qopen = np.array([q.get_open() for q in data]).astype(np.float)
#qclose = np.array([q.get_prev_close() for q in quotes]).astype(np.float)
#qopen = np.array(data['open'],data['open']).astype(np.float)
#qclose = np.array(data['close'],data['close']).astype(np.float)
qopen = np.array(data_open).astype(np.float)
qclose = np.array(data_close).astype(np.float)
variation= qclose - qopen #per day variation in price for each symbol
X = variation.T
X /= X.std(axis=0) #standardize to use correlations rather than covariance
#X = np.concatenate([X,X],axis=1)
#B = np.reshape(A, (-1, 2))
#estimate inverse covariance
X = X.T
graph = GraphLassoCV()
graph.fit(X)
gl_cov = graph.covariance_
gl_prec = graph.precision_
gl_alphas =graph.cv_alphas_
gl_scores = np.mean(graph.grid_scores, axis=1)
plt.figure()
sns.heatmap(gl_prec)
plt.figure()
plt.plot(gl_alphas, gl_scores, marker='o', color='b', lw=2.0, label='GraphLassoCV')
plt.title("Graph Lasso Alpha Selection")
plt.xlabel("alpha")
plt.ylabel("score")
plt.legend()
#cluster using affinity propagation
_, labels = cluster.affinity_propagation(gl_cov)
num_labels = np.max(labels)
"""
for i in range(num_labels+1):
print("Cluster %i: %s" %((i+1), ', '.join(names[labels==i])))
#find a low dim embedding for visualization
node_model = manifold.LocallyLinearEmbedding(n_components=2, n_neighbors=6, eigen_solver='dense')
embedding = node_model.fit_transform(X.T).T
#generate plots
plt.figure()
plt.clf()
ax = plt.axes([0.,0.,1.,1.])
plt.axis('off')
partial_corr = gl_prec
d = 1 / np.sqrt(np.diag(partial_corr))
non_zero = (np.abs(np.triu(partial_corr, k=1)) > 0.02) #connectivity matrix
#plot the nodes
plt.scatter(embedding[0], embedding[1], s = 100*d**2, c = labels, cmap = plt.cm.spectral)
#plot the edges
start_idx, end_idx = np.where(non_zero)
segments = [[embedding[:,start], embedding[:,stop]] for start, stop in zip(start_idx, end_idx)]
values = np.abs(partial_corr[non_zero])
lc = LineCollection(segments, zorder=0, cmap=plt.cm.hot_r, norm=plt.Normalize(0,0.7*values.max()))
lc.set_array(values)
lc.set_linewidths(5*values)
ax.add_collection(lc)
#plot the labels
for index, (name, label, (x,y)) in enumerate(zip(names, labels, embedding.T)):
plt.text(x,y,name,size=12)
"""
"""****************************
Gaussian Process Regression
****************************"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas_datareader import data, wb
#from sklearn.gaussian_process import GaussianProcess
from sklearn.gaussian_process import GaussianProcess
#from sklearn.cross_validation import train_test_split
from sklearn.model_selection import train_test_split
from datetime import datetime
import pytz
np.random.seed(0)
def f(x): return x * np.sin(x)
if __name__ == "__main__":
print("639 main----------------------------------------------------1")
plt.close('all')
#example: fit a GP (with noisy observations)
X = np.array([1., 3., 5., 6., 7., 8.]).reshape(-1,1)
y = f(X).ravel()
dy = 0.5 + 1.0*np.random.random(y.shape) #in [0.5, 1.5] <- std deviation per point
y = y + np.random.normal(0, dy) #0-mean noise with variable std in [0.5, 1.5]
gp = GaussianProcess(corr='cubic', nugget = (dy / y)**2, theta0=1e-1, thetaL=1e-3, thetaU=1, random_start=100, verbose=True)
gp.fit(X, y) #ML est
gp.get_params()
Xt = np.array(np.linspace(np.min(X)-10,np.max(X)+10,1000)).reshape(-1,1)
y_pred, MSE = gp.predict(Xt, eval_MSE=True)
sigma = np.sqrt(MSE)
plt.figure()
plt.plot(Xt, f(Xt), color='k', lw=2.0, label = 'x sin(x) ground truth')
plt.plot(X, y, 'r+', markersize=20, lw=2.0, label = 'observations')
plt.errorbar(X.ravel(), y, dy, fmt='r.', markersize=10, label='Observations')
plt.plot(Xt, y_pred, color = 'g', linestyle = '--', lw=1.5, label = 'GP prediction')
plt.fill(np.concatenate([Xt, Xt[::-1]]), np.concatenate([y_pred-1.96*sigma, (y_pred+1.96*sigma)[::-1]]), alpha = 0.5, label = '95% conf interval')
plt.title('GP regression')
plt.xlabel('X1')
plt.ylabel('X2')
plt.grid(True)
plt.legend()
plt.show()
#fit a GP to market data
#load data
start = datetime(2015, 1, 1, 0, 0, 0, 0, pytz.utc)
end = datetime(2016, 1, 1, 0, 0, 0, 0, pytz.utc)
spy = data.DataReader("SPY", 'yahoo', start, end)
spy['Close'] = spy['Close'].dropna(inplace=True)
spy['Volume'] = spy['Volume'].dropna(inplace=True)
spy_price = np.array(spy['Close'].values).reshape(-1,1)
spy_volume = np.array(spy['Volume'].values).reshape(-1,1)
spy_obs = np.hstack([spy_price, spy_volume])
#X = np.random.rand(np.size(spy_price)).reshape(-1,1)
X = np.array(range(np.size(spy_price))).reshape(-1,1)
y = spy_price.ravel()
dy = 10*spy.Close.std()
"""
spy_gp = GaussianProcess(corr='cubic', nugget = (dy / y)**2, theta0=1e-1, thetaL=1e-3, thetaU=1e3, random_start=100, verbose=True)
spy_gp.fit(X,y)
spy_gp.get_params()
Xt = np.array(np.linspace(np.min(X)-10,np.max(X)+10,1000)).reshape(-1,1)
y_pred, MSE = spy_gp.predict(Xt, eval_MSE=True)
sigma = np.sqrt(MSE)
f = plt.figure()
plt.plot(X, y, 'r-', markersize=20, lw=2.0, label = 'SPY price, USD')
#plt.errorbar(X.ravel(), y, dy, fmt='r.', markersize=10, label='Observations')
plt.plot(Xt, y_pred, color = 'g', linestyle = '--', lw=1.5, label = 'GP prediction')
plt.fill(np.concatenate([Xt, Xt[::-1]]), np.concatenate([y_pred-1.96*sigma, (y_pred+1.96*sigma)[::-1]]), alpha = 0.5, label = '95% conf interval')
plt.title('GP regression')
plt.xlabel('time, days')
plt.ylabel('S&P500 price, USD')
plt.grid(True)
plt.legend()
plt.show()
"""