- 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 and 230:
216
- Page 232 and 233:
Chapter 15Introduction to Machine L
- Page 234 and 235:
22115.3.1 Linear RegressionAn eleme
- Page 236 and 237:
223While seemingly not a traditiona
- Page 238 and 239:
Chapter 16Supervised LearningSuperv
- Page 240 and 241:
227ŷ = ˆf(x) = argmax z∈R p(y =
- Page 242 and 243:
Chapter 17Linear RegressionIn this
- Page 244 and 245:
231if __name__ == "__main__":# Set
- Page 246 and 247:
23317.3 Maximum Likelihood Estimati
- Page 248 and 249:
235To simplify the notation the lat
- Page 250 and 251:
237Once the full dataset is created
- Page 252 and 253:
239if __name__ == "__main__":# Set
- Page 254 and 255:
241))lr_model.coef_[0]# Create a sc
- Page 256 and 257:
Chapter 18Tree-Based MethodsIn this
- Page 258 and 259:
245Figure 18.2: The resulting parti
- Page 260 and 261:
24718.3 Decision Trees for Classifi
- Page 262 and 263:
249In quantitative finance applicat
- Page 264 and 265:
251(a) Grow a tree ˆf b with k spl
- Page 266 and 267:
253tslag["Lag%s" % str(i+1)] = ts["
- Page 268 and 269:
255The same approach is carried out
- Page 270 and 271:
257seen whether it is feasible to p
- Page 272 and 273:
259amzn = create_lagged_series("AMZ
- Page 274 and 275:
Chapter 19Support Vector MachinesIn
- Page 276 and 277:
263• Non-Probabilistic - Since th
- Page 278 and 279:
265The key point here is that it is
- Page 280 and 281:
267classification. Overfitting mean
- Page 282 and 283:
269Figure 19.6: Addition of a singl
- Page 284 and 285:
27119.8 Support Vector MachinesThe
- Page 286 and 287:
273p∑K(x i , x k ) = (1 + x ij x
- Page 288 and 289:
Chapter 20Model Selection andCross-
- Page 290 and 291:
277This motivates the concept of a
- Page 292 and 293:
279Figure 20.1: Various estimates o
- Page 294 and 295:
281error for a particular model est
- Page 296 and 297:
283Figure 20.3: Amazon, Inc. - Pric
- Page 298 and 299:
28520.2.5 Python ImplementationWe a
- Page 300 and 301:
287At this stage we have the necess
- Page 302 and 303:
289..import pylab as plt....def plo
- Page 304 and 305:
291fold = 0# Loop over the k-foldsf
- Page 306 and 307:
293Figure 20.5: Test MSE curves for
- Page 308 and 309:
295return tsretdef validation_set_p
- Page 310 and 311:
297model = Pipeline([("polynomial_f
- Page 312 and 313: 299X = lags[["Lag1", "Lag2", "Lag3"
- Page 314 and 315: Chapter 21Unsupervised LearningThe
- Page 316 and 317: 303parameters of the model, θ.Conv
- Page 318 and 319: Chapter 22Clustering MethodsIn this
- Page 320 and 321: 3072. Iterate the following until c
- Page 322 and 323: 309]# Generate a list of the 2D clu
- Page 324 and 325: 311clustered using K-Means and then
- Page 326 and 327: 313ax.set_axis_bgcolor((1,1,0.9))ax
- Page 328 and 329: 315ClusterTomorrow to contain tomor
- Page 330 and 331: 317Figure 22.4: 3D scatterplot of n
- Page 332 and 333: 319# Apply the K-Means Algorithm fo
- Page 334 and 335: 321# up days and red for down daysc
- Page 336 and 337: 323sp500["ClusterMatrix"] = list(zi
- Page 338 and 339: Chapter 23Natural Language Processi
- Page 340 and 341: 327The Reuters 21578 dataset can be
- Page 342 and 343: 329for export as shippers are now e
- Page 344 and 345: 331tungtung-oilveg-oilwheatwoolwpiy
- Page 346 and 347: 333in order to minimise memory usag
- Page 348 and 349: 335’The Tokyo Grain Exchange said
- Page 350 and 351: 337for t in d[0]:if t in topics:d_t
- Page 352 and 353: 339This would mean that we are givi
- Page 354 and 355: 341partitions into the training and
- Page 356 and 357: 3430.660194174757[[21 0 0 0 2 3 0 0
- Page 358 and 359: 345self.encoding = encodingdef _res
- Page 360 and 361: 347"""topics = open("data/all-topic
- 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