- Page 2 and 3:
ContentsI Introduction 11 Introduct
- Page 4 and 5:
38.2 Correlation . . . . . . . . .
- Page 6 and 7:
513.2 The Kalman Filter . . . . . .
- Page 8 and 9:
719 Support Vector Machines . . . .
- Page 10 and 11:
928 Kalman Filter-Based Pairs Tradi
- Page 12 and 13:
Limit of Liability/Disclaimer ofWar
- Page 14:
Part IIntroduction1
- Page 17 and 18:
4The aim of this book is to provide
- Page 19 and 20:
61.3 How Is The Book Laid Out?The b
- Page 21 and 22:
81.5 How Does This Book Differ From
- Page 23 and 24:
101.7.1 AlternativesThere are many
- Page 26 and 27:
Chapter 2Introduction to Bayesian S
- Page 28 and 29:
15Table 2.1: Comparison of Frequent
- Page 30 and 31:
172.2 Applying Bayes’ Rule for Ba
- Page 32 and 33:
19the next chapter. At this stage,
- Page 34 and 35:
21# Create and plot a Beta distribu
- Page 36 and 37:
Chapter 3Bayesian Inference of a Bi
- Page 38 and 39:
25• The fairness of the coin does
- Page 40 and 41:
273.4.3 Multiple Flips of the CoinN
- Page 42 and 43:
29x = np.linspace(0, 1, 100)params
- Page 44 and 45:
31Hence, all we need to do is re-ar
- Page 46 and 47:
33Figure 3.3: The prior and posteri
- Page 48 and 49:
Chapter 4Markov Chain Monte CarloIn
- Page 50 and 51:
374.2.1 Markov Chain Monte Carlo Al
- Page 52 and 53:
39We discussed the fact that we cou
- Page 54 and 55:
41Finally we specify the Metropolis
- Page 56 and 57:
43we do not need to compute 100,000
- Page 58 and 59:
45x, stats.beta.pdf(x, alpha, beta)
- Page 60 and 61:
Chapter 5Bayesian Linear Regression
- Page 62 and 63:
49non-informative priors.• Poster
- Page 64 and 65:
51# Use a linear model (y ~ beta_0
- Page 66 and 67:
53# Use the No-U-Turn Samplerstep =
- Page 68 and 69:
55plt.show()We can see the sampled
- Page 70 and 71:
57"""Calculates the Markov Chain Mo
- Page 72 and 73:
Chapter 6Bayesian Stochastic Volati
- Page 74 and 75:
61plt.show()Figure 6.1: Different r
- Page 76 and 77:
63( )yilog ∼ t(ν, 0, exp(−2s i
- Page 78 and 79:
656.3.2 Model Specification in PyMC
- Page 80 and 81:
67Figure 6.5: Overlay of samples fr
- Page 82 and 83:
69plt.xlabel(’Time’)plt.ylabel(
- Page 84:
Part IIITime Series Analysishttps:/
- Page 87 and 88:
74• Seasonal Variation - Many tim
- Page 89 and 90:
767.5 How Does This Relate to Other
- Page 91 and 92:
78Definition 8.1.1. Expectation. Th
- Page 93 and 94:
808.2 CorrelationCorrelation is a d
- Page 95 and 96:
82Definition 8.3.4. Stationary in t
- Page 97 and 98:
84Figure 8.2: Correlogram plotted i
- Page 99 and 100:
868.6 Next StepsNow that we’ve di
- Page 101 and 102:
88• Use our knowledge of time ser
- Page 103 and 104:
909.3.2 CorrelogramWe can also plot
- Page 105 and 106:
929.4.2 CorrelogramThe autocorrelat
- Page 107 and 108:
94created the difference series, we
- Page 109 and 110:
96there are a few that are marginal
- Page 111 and 112:
9810.1 How Will We Proceed?In this
- Page 113 and 114:
10010.4.1 RationaleIn the previous
- Page 115 and 116:
10210.4.4 Simulations and Correlogr
- Page 117 and 118:
104Figure 10.2: Realisation of AR(1
- Page 119 and 120:
10610.4.5 Financial DataAmazon Inc.
- Page 121 and 122:
108Figure 10.6: Correlogram of Firs
- Page 123 and 124:
110> plot(gspcrt)Figure 10.8: First
- Page 125 and 126:
112model, as well as the conditiona
- Page 127 and 128:
114Figure 10.10: Realisation of MA(
- Page 129 and 130:
116Coefficients:ma1 intercept-0.729
- Page 131 and 132:
118> require(quantmod)> getSymbols(
- Page 133 and 134:
120> amznrt.maCall:arima(x = amznrt
- Page 135 and 136:
122Figure 10.16: Residuals of MA(1)
- Page 137 and 138:
124Figure 10.18: Residuals of MA(3)
- Page 139 and 140:
12610.6.3 RationaleTo date we have
- Page 141 and 142:
128Figure 10.20: Correlogram of an
- Page 143 and 144:
130Figure 10.22: Correlogram of an
- Page 145 and 146:
132Figure 10.23: Correlogram of the
- Page 147 and 148:
134Notice that there are some signi
- Page 149 and 150:
136I would like to thank you for be
- Page 151 and 152:
138Figure 11.1: Plot of simulated A
- Page 153 and 154:
140> amzn = diff(log(Cl(AMZN)))As i
- Page 155 and 156:
142We fit an ARIMA model by looping
- Page 157 and 158:
144being able to create strategy in
- Page 159 and 160:
14611.5.2 Why Does This Model Volat
- Page 161 and 162:
148ɛ t = σ t w t (11.17)σ 2 t =
- Page 163 and 164:
1502.5 % 97.5 %a0 0.1786255 0.21726
- Page 165 and 166:
152Figure 11.10: Residuals of an AR
- Page 167 and 168:
Figure 11.13: Squared residuals of
- Page 169 and 170:
156mean, but due to its stationarit
- Page 171 and 172:
158x t = pz t + w x,t (12.1)y t = q
- Page 173 and 174:
160Figure 12.3: Plot of x t and y t
- Page 175 and 176:
162> layout(1:2)> plot(badcomb, typ
- Page 177 and 178:
164We then created two subsequent s
- Page 179 and 180: 166Figure 12.6: Backward-adjusted c
- Page 181 and 182: 168Coefficients:(Intercept) ewaAdj3
- Page 183 and 184: 170> plot(rdsaAdj, rdsbAdj,xlab="RD
- Page 185 and 186: 172set.seed(123)## SIMULATED DATA##
- Page 187 and 188: 174plot(comb1$residuals, type="l",x
- Page 189 and 190: 176> q <- 0.6*z + rnorm(10000)> r <
- Page 191 and 192: 178Figure 12.12: Plot of s t , the
- Page 193 and 194: 180EWA.Adjusted.l2 EWC.Adjusted.l2
- Page 195 and 196: 182library("quantmod")library("tser
- Page 197 and 198: 184
- Page 199 and 200: 186• Smoothing - Estimating the p
- Page 201 and 202: 18813.2.1 A Bayesian ApproachRecall
- Page 203 and 204: 190E[y t+1 |D t ] = E[Ft+1θ T t +
- Page 205 and 206: 192of our linear regression. The ne
- Page 207 and 208: 194Figure 13.1: Scatterplot of the
- Page 209 and 210: 196Figure 13.2: Time-varying slope
- Page 211 and 212: 198delta = 1e-5trans_cov = delta /
- Page 213 and 214: 200
- Page 215 and 216: 202"risk manager". They will be use
- Page 217 and 218: 204Figure 14.1: Two-state Markov Ch
- Page 219 and 220: 206Figure 14.2: Hidden Markov Model
- Page 221 and 222: 208> bull_var <- 0.1> bear_mean <-
- Page 223 and 224: 210historical financial data.14.3.3
- Page 225 and 226: 212The same process will now be car
- Page 227 and 228: 214install.packages(’quantmod’)
- Page 229: 216
- Page 233 and 234: https://sanet.cd/blogs/polatebooks/
- Page 235 and 236: 22215.4 Machine Learning Applicatio
- Page 237 and 238: 224disadvantages are the need to ha
- Page 239 and 240: 226to machine learning, which is th
- Page 241 and 242: 228However, a significant concern a
- Page 243 and 244: 230variance σ 2 . ɛ represents th
- Page 245 and 246: 232Figure 17.1:(2012)[71]Plot of p(
- Page 247 and 248: 234N∑NLL(θ) = − log p(y i | x
- Page 249 and 250: 236The goal of the exercise will be
- Page 251 and 252: 238lr_model.predict(X_test),color=
- Page 253 and 254: 240from sklearn import linear_model
- Page 255 and 256: 242
- Page 257 and 258: 244Where w m is the mean response i
- Page 259 and 260: 246The first task is to sum across
- Page 261 and 262: 24818.4 Advantages and Disadvantage
- Page 263 and 264: 250Unfortunately this gain in predi
- Page 265 and 266: 252import datetimeimport matplotlib
- Page 267 and 268: 254X = amzn[["Lag1", "Lag2", "Lag3"
- Page 269 and 270: 256plt.plot(estimators, bagging_mse
- Page 271 and 272: 258"""# Obtain stock information fr
- Page 273 and 274: 260)rf = RandomForestRegressor(n_es
- Page 275 and 276: 262between categories.Formally, in
- Page 277 and 278: 264b 0 + b 1 x 1 + ... + b p x p =
- Page 279 and 280: 266andb · x i + b 0 < 0, if y i =
- Page 281 and 282:
268Maximise M ∈ R, by varying b 1
- Page 283 and 284:
270andy i (b · x + b 0 ) ≥ M(1
- Page 285 and 286:
272p∑〈u, v〉 = u j v j (19.14)
- Page 287 and 288:
274
- Page 289 and 290:
276y = f(x) + ɛ (20.1)This states
- Page 291 and 292:
278the following day, we are only c
- Page 293 and 294:
280However if we plot the test MSE,
- Page 295 and 296:
28220.2.1 Overview of Cross-Validat
- Page 297 and 298:
284Secondly, note that in the 50-50
- Page 299 and 300:
286tslag["Volume"] = ts["Volume"]#
- Page 301 and 302:
288# Increase degree of linear# reg
- Page 303 and 304:
290Figure 20.4: Test MSE curves for
- Page 305 and 306:
292..We can plot these curves with
- Page 307 and 308:
294Trading volume, as well as the D
- Page 309 and 310:
296sample_dict["seed_%s" % i] = np.
- Page 311 and 312:
298label=’Avg Test MSE’)ax.lege
- Page 313 and 314:
300
- Page 315 and 316:
30221.1 High Dimensional DataQuanti
- Page 317 and 318:
304The canonical algorithm for clus
- Page 319 and 320:
306Figure 22.1: K-Means Clustering
- Page 321 and 322:
30822.1.3 Simulated DataIn this sec
- Page 323 and 324:
310Figure 22.2: K-Means Algorithm o
- Page 325 and 326:
312["Open", "High", "Low","Close",
- Page 327 and 328:
314alldays = DayLocator()weekFormat
- Page 329 and 330:
316# Plot the full OHLC candles re-
- Page 331 and 332:
318changes. Clearly this motivates
- Page 333 and 334:
320Obtains a pandas DataFrame conta
- Page 335 and 336:
322ax.xaxis.set_major_locator(monda
- Page 337 and 338:
https://sanet.cd/blogs/polatebooks/
- Page 339 and 340:
326• Continually monitor the syst
- Page 341 and 342:
328... 1049566 Dec 4 1996 reut2-020
- Page 343 and 344:
330of a quant researchers day is sp
- Page 345 and 346:
332import pprintimport retry:from h
- Page 347 and 348:
334self.docs.append( (self.topics,
- Page 349 and 350:
336’90,000 yen per 15 tonne lot f
- Page 351 and 352:
33823.4 VectorisationAt this stage
- Page 353 and 354:
340# Open the first Reuters data se
- Page 355 and 356:
34223.7 Performance MetricsThe two
- Page 357 and 358:
344print(svm.score(X_test, y_test))
- Page 359 and 360:
346def handle_endtag(self, tag):"""
- Page 361 and 362:
348def train_svm(X, y):"""Create an
- Page 364 and 365:
Chapter 24Introduction to QSTraderI
- Page 366 and 367:
353The design calls for the infrast
- Page 368 and 369:
Chapter 25Introductory Portfolio St
- Page 370 and 371:
357Ticker Name Period LinkSPY SPDR
- Page 372 and 373:
359):ticker = event.tickerif self.t
- Page 374 and 375:
361from qstrader import settingsfro
- Page 376 and 377:
363Figure 25.2: "Strategic" Weight
- Page 378 and 379:
36525.6 Full Code# monthly_rebalanc
- Page 380 and 381:
367import datetimefrom qstrader imp
- Page 382 and 383:
Chapter 26ARIMA+GARCH Trading Strat
- Page 384 and 385:
371We use the same procedure as in
- Page 386 and 387:
373value would represent data not k
- Page 388 and 389:
375Figure 26.1: Equity curve of ARI
- Page 390 and 391:
377}final.aic <- current.aicfinal.o
- Page 392 and 393:
379new_str = "%s,%s\n" % (strpf, ol
- Page 394 and 395:
Chapter 27Cointegration-Based Pairs
- Page 396 and 397:
383Figure 27.1: Backward-adjusted p
- Page 398 and 399:
385By definition a mean-reverting s
- Page 400 and 401:
387ues of the market value of a "un
- Page 402 and 403:
389commands, so that the positions
- Page 404 and 405:
391thresholds are set to 1.5 and 0.
- Page 406 and 407:
393surprising since no statisticall
- Page 408 and 409:
395self.bars_elapsed = 0def _set_co
- Page 410 and 411:
397self.invested = Nonedef calculat
- Page 412 and 413:
399# Use the ExampleCompliance comp
- Page 414 and 415:
Chapter 28Kalman Filter-Based Pairs
- Page 416 and 417:
403• TLT - For the period 3rd Aug
- Page 418 and 419:
405self.time = event.time# Set the
- Page 420 and 421:
407print("LONG: %s" % event.time)se
- Page 422 and 423:
409# Use the Naive Position Sizer (
- Page 424 and 425:
411Figure 28.1: Kalman Filter-Based
- Page 426 and 427:
413"""Sets the correct price and ev
- Page 428 and 429:
415))self.invested = "long"elif et
- Page 430 and 431:
417strategy = Strategies(strategy,
- Page 432 and 433:
Chapter 29Supervised Learning for I
- Page 434 and 435:
42129.2 Building a Prediction Model
- Page 436 and 437:
423The next steps consist of filter
- Page 438 and 439:
425if __name__ == "__main__":random
- Page 440 and 441:
427from sklearn.externals import jo
- Page 442 and 443:
429SignalEvent(self.tickers[0], "BO
- Page 444 and 445:
431)# Use the ExampleCompliance com
- Page 446 and 447:
433Figure 29.1: Tearsheet for Linea
- Page 448 and 449:
435total return. The Sharpe Ratio i
- Page 450 and 451:
437)# Create the lookback and lookf
- Page 452 and 453:
439#model = LinearDiscriminantAnaly
- Page 454 and 455:
441self.minutes += 1# Allow enough
- Page 456 and 457:
443# Use the Tearsheet Statisticsst
- Page 458 and 459:
Chapter 30Sentiment Analysis via Se
- Page 460 and 461:
447sentiment").This sample file for
- Page 462 and 463:
449Ticker Name Period LinkXOM Exxon
- Page 464 and 465:
451tuples, it is useful to create a
- Page 466 and 467:
453if event.type == EventType.TICK
- Page 468 and 469:
455threshold, then it closes the po
- Page 470 and 471:
457Figure 30.1:is mostly down or fl
- Page 472 and 473:
459Figure 30.3:Defence stocks provi
- Page 474 and 475:
461if (self.invested[ticker] is Fal
- Page 476 and 477:
https://sanet.cd/blogs/polatebooks/
- Page 478 and 479:
Chapter 31Market Regime Detection w
- Page 480 and 481:
467Ticker Name Period LinkSPY SPDR
- Page 482 and 483:
469def obtain_prices_df(csv_filepat
- Page 484 and 485:
471adjusted closing prices are plot
- Page 486 and 487:
473# short and long window barspric
- Page 488 and 489:
475# Determine the HMM predicted re
- Page 490 and 491:
477pickle_path = "/path/to/your/mod
- Page 492 and 493:
479The underlying strategy is desig
- Page 494 and 495:
481Obtain the prices DataFrame from
- Page 496 and 497:
483Requires:tickers - The list of t
- Page 498 and 499:
485)hidden_state = self.hmm_model.p
- Page 500 and 501:
487)csv_dir, events_queue, tickers,
- Page 502 and 503:
Chapter 32Strategy DecayIn this cha
- Page 504 and 505:
491It should be well remembered tha
- Page 506 and 507:
493)label=’Backtest’, ax=ax, **
- Page 508 and 509:
495Figure 32.1: Kalman Filter Pairs
- Page 510 and 511:
497Figure 32.3: Sentiment Sentdex S
- Page 512 and 513:
Bibliography[1] Wikipedia: Standard
- Page 514 and 515:
501[39] Efron, B. Bootstrap methods
- Page 516 and 517:
503[74] O’Mahony, A. Online linea