-
Notifications
You must be signed in to change notification settings - Fork 0
/
kolm-and-ritter-2018.html
executable file
·813 lines (654 loc) · 104 KB
/
kolm-and-ritter-2018.html
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
<!DOCTYPE html>
<html lang="english">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="HandheldFriendly" content="True" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="robots" content="" />
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro:ital,wght@0,400;0,700;1,400&family=Source+Sans+Pro:ital,wght@0,300;0,400;0,700;1,400&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="/theme/stylesheet/style.min.css">
<link id="pygments-light-theme" rel="stylesheet" type="text/css"
href="/theme/pygments/github.min.css">
<link rel="stylesheet" type="text/css" href="/theme/font-awesome/css/fontawesome.css">
<link rel="stylesheet" type="text/css" href="/theme/font-awesome/css/brands.css">
<link rel="stylesheet" type="text/css" href="/theme/font-awesome/css/solid.css">
<!-- Google Analytics -->
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-148456981-1', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
<meta name="author" content="Indrayana Rustandi" />
<meta name="description" content="%reload_ext autoreload %autoreload 2 %matplotlib inline In an SSRN paper, Petter Kolm and Gordon Ritter present the application of reinforcement learning for model-free European call option hedging. Unfortunately, there does not seem to be any code made available to accompany this paper. Here we try to replicate the results of …" />
<meta name="keywords" content="">
<meta property="og:site_name" content="Indra's Technical Musings"/>
<meta property="og:title" content="Kolm and Ritter (2018)"/>
<meta property="og:description" content="%reload_ext autoreload %autoreload 2 %matplotlib inline In an SSRN paper, Petter Kolm and Gordon Ritter present the application of reinforcement learning for model-free European call option hedging. Unfortunately, there does not seem to be any code made available to accompany this paper. Here we try to replicate the results of …"/>
<meta property="og:locale" content="en_US"/>
<meta property="og:url" content="/kolm-and-ritter-2018.html"/>
<meta property="og:type" content="article"/>
<meta property="article:published_time" content="2019-10-12 09:00:00-04:00"/>
<meta property="article:modified_time" content=""/>
<meta property="article:author" content="/author/indrayana-rustandi.html">
<meta property="article:section" content="reinforcement learning"/>
<meta property="og:image" content="">
<title>Indra's Technical Musings – Kolm and Ritter (2018)</title>
</head>
<body class="light-theme">
<aside>
<div>
<a href="">
<img src="/theme/img/profile.png" alt="" title="">
</a>
<h1>
<a href=""></a>
</h1>
<ul class="social">
<li>
<a class="sc-linkedin" href="https://www.linkedin.com/in/indrayana-rustandi-a624494/" target="_blank">
<i class="fab fa-linkedin"></i>
</a>
</li>
<li>
<a class="sc-github" href="https://github.com/irustandi/" target="_blank">
<i class="fab fa-github"></i>
</a>
</li>
</ul>
</div>
</aside>
<main>
<article class="single">
<header>
<h1 id="kolm-and-ritter-2018">Kolm and Ritter (2018)</h1>
<p>
Posted on Sat 12 October 2019 in <a href="/category/reinforcement-learning.html">reinforcement learning</a>
</p>
</header>
<div>
<div class="highlight"><pre><span></span><code><span class="o">%</span><span class="n">reload_ext</span> <span class="n">autoreload</span>
<span class="o">%</span><span class="n">autoreload</span> <span class="mi">2</span>
<span class="o">%</span><span class="n">matplotlib</span> <span class="n">inline</span>
</code></pre></div>
<p>In an <a href="https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3281235">SSRN paper</a>, Petter Kolm and Gordon Ritter present the application of reinforcement learning for model-free European call option hedging. Unfortunately, there does not seem to be any code made available to accompany this paper. Here we try to replicate the results of the paper.</p>
<p>Let us first import the necessary packages.</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">scipy.stats</span> <span class="k">as</span> <span class="nn">si</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="kn">from</span> <span class="nn">catboost</span> <span class="kn">import</span> <span class="n">CatBoostRegressor</span>
<span class="kn">from</span> <span class="nn">tqdm</span> <span class="kn">import</span> <span class="n">tqdm</span><span class="p">,</span> <span class="n">tqdm_notebook</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">partial</span>
<span class="kn">from</span> <span class="nn">joblib</span> <span class="kn">import</span> <span class="n">Parallel</span><span class="p">,</span> <span class="n">delayed</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">gym</span>
<span class="n">plt</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">use</span><span class="p">(</span><span class="s1">'seaborn'</span><span class="p">)</span>
<span class="kn">import</span> <span class="nn">seaborn</span> <span class="k">as</span> <span class="nn">sns</span>
</code></pre></div>
<p>Next, we implement functions to calculate the price and delta for a European option (call/put, credit to <a href="https://aaronschlegel.me/black-scholes-formula-python.html">Aaron Schlegel</a>).</p>
<div class="highlight"><pre><span></span><code><span class="k">def</span> <span class="nf">euro_vanilla</span><span class="p">(</span><span class="n">S</span><span class="p">,</span> <span class="n">K</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">option</span> <span class="o">=</span> <span class="s1">'call'</span><span class="p">):</span>
<span class="c1">#S: spot price</span>
<span class="c1">#K: strike price</span>
<span class="c1">#T: time to maturity</span>
<span class="c1">#r: interest rate</span>
<span class="c1">#sigma: volatility of underlying asset</span>
<span class="k">if</span> <span class="n">T</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
<span class="n">d1</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">S</span> <span class="o">/</span> <span class="n">K</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">r</span> <span class="o">+</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">sigma</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">T</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">T</span><span class="p">))</span>
<span class="n">d2</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">S</span> <span class="o">/</span> <span class="n">K</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">r</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">sigma</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">T</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">T</span><span class="p">))</span>
<span class="k">if</span> <span class="n">option</span> <span class="o">==</span> <span class="s1">'call'</span><span class="p">:</span>
<span class="n">result</span> <span class="o">=</span> <span class="p">(</span><span class="n">S</span> <span class="o">*</span> <span class="n">si</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">d1</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">-</span> <span class="n">K</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">r</span> <span class="o">*</span> <span class="n">T</span><span class="p">)</span> <span class="o">*</span> <span class="n">si</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">d2</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">))</span>
<span class="k">if</span> <span class="n">option</span> <span class="o">==</span> <span class="s1">'put'</span><span class="p">:</span>
<span class="n">result</span> <span class="o">=</span> <span class="p">(</span><span class="n">K</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">r</span> <span class="o">*</span> <span class="n">T</span><span class="p">)</span> <span class="o">*</span> <span class="n">si</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="o">-</span><span class="n">d2</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">-</span> <span class="n">S</span> <span class="o">*</span> <span class="n">si</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="o">-</span><span class="n">d1</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">if</span> <span class="n">option</span> <span class="o">==</span> <span class="s1">'call'</span><span class="p">:</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">S</span> <span class="o">-</span> <span class="n">K</span> <span class="k">if</span> <span class="n">S</span> <span class="o">></span> <span class="n">K</span> <span class="k">else</span> <span class="mi">0</span>
<span class="k">if</span> <span class="n">option</span> <span class="o">==</span> <span class="s1">'put'</span><span class="p">:</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">K</span> <span class="o">-</span> <span class="n">S</span> <span class="k">if</span> <span class="n">K</span> <span class="o">></span> <span class="n">S</span> <span class="k">else</span> <span class="mi">0</span>
<span class="k">return</span> <span class="n">result</span>
<span class="k">def</span> <span class="nf">euro_vanilla_delta</span><span class="p">(</span><span class="n">S</span><span class="p">,</span> <span class="n">K</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">option</span> <span class="o">=</span> <span class="s1">'call'</span><span class="p">):</span>
<span class="c1">#S: spot price</span>
<span class="c1">#K: strike price</span>
<span class="c1">#T: time to maturity</span>
<span class="c1">#r: interest rate</span>
<span class="c1">#sigma: volatility of underlying asset</span>
<span class="k">if</span> <span class="n">T</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
<span class="n">d1</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">S</span> <span class="o">/</span> <span class="n">K</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">r</span> <span class="o">+</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">sigma</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">T</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">T</span><span class="p">))</span>
<span class="n">d2</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">S</span> <span class="o">/</span> <span class="n">K</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">r</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">sigma</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">T</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">T</span><span class="p">))</span>
<span class="n">delta</span> <span class="o">=</span> <span class="n">si</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">d1</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span>
<span class="k">if</span> <span class="n">option</span> <span class="o">==</span> <span class="s1">'call'</span><span class="p">:</span>
<span class="k">return</span> <span class="n">delta</span>
<span class="k">if</span> <span class="n">option</span> <span class="o">==</span> <span class="s1">'put'</span><span class="p">:</span>
<span class="k">return</span> <span class="n">delta</span> <span class="o">-</span> <span class="mf">1.0</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="mf">0.0</span>
</code></pre></div>
<p>We also need to simulate our underlying prices, which we assume to be a geometric Brownian motion (see <a href="https://stackoverflow.com/questions/13202799/python-code-geometric-brownian-motion-whats-wrong">this StackOverflow post</a> for details).</p>
<div class="highlight"><pre><span></span><code><span class="k">def</span> <span class="nf">simulate_GBM</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">dt</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">N</span><span class="p">,</span> <span class="n">numts</span><span class="p">):</span>
<span class="c1"># S0: starting level</span>
<span class="c1"># mu: drift</span>
<span class="c1"># sigma: volatility</span>
<span class="c1"># dt: the timestep unit</span>
<span class="c1"># T: end time</span>
<span class="c1"># N: the number of elements in the time series</span>
<span class="c1"># numts: number of time series to generate</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">N</span><span class="p">)</span>
<span class="n">W</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="n">numts</span><span class="p">,</span> <span class="n">N</span><span class="p">)</span>
<span class="n">W</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cumsum</span><span class="p">(</span><span class="n">W</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span> <span class="c1">### standard brownian motion ###</span>
<span class="n">X</span> <span class="o">=</span> <span class="p">(</span><span class="n">mu</span><span class="o">-</span><span class="mf">0.5</span><span class="o">*</span><span class="n">sigma</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="n">t</span> <span class="o">+</span> <span class="n">sigma</span><span class="o">*</span><span class="n">W</span>
<span class="n">S</span> <span class="o">=</span> <span class="n">S0</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">X</span><span class="p">)</span> <span class="c1">### geometric brownian motion ###</span>
<span class="k">return</span> <span class="n">S</span><span class="p">,</span> <span class="n">t</span>
<span class="k">def</span> <span class="nf">simulate_prices</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">numts</span><span class="p">):</span>
<span class="n">N</span> <span class="o">=</span> <span class="n">T</span> <span class="o">*</span> <span class="n">D</span> <span class="o">+</span> <span class="mi">1</span>
<span class="n">dt</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">/</span> <span class="n">D</span>
<span class="k">return</span> <span class="n">simulate_GBM</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">dt</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">N</span><span class="p">,</span> <span class="n">numts</span><span class="p">)</span>
</code></pre></div>
<p>Let us test our price simulation function by generating a sequence of prices and plotting them.</p>
<div class="highlight"><pre><span></span><code><span class="n">prices</span><span class="p">,</span> <span class="n">t</span> <span class="o">=</span> <span class="n">simulate_prices</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">prices</span><span class="p">[</span><span class="mi">5</span><span class="p">,:])</span>
</code></pre></div>
<p><img alt="png" src="/images/kolm_ritter/output_8_1.png"></p>
<p>Looks reasonable. Now we are ready to implement our simulation environment. We follow the interface of <a href="https://github.com/openai/gym/blob/master/docs/creating-environments.md">OpenAI gym's environment</a>. We also implement the transaction cost calculator, following the formula used in the paper:</p>
<div class="math">$$
\mathrm{cost}(n) = \mathrm{multiplier} \times \mathrm{TickSize} \times (|n| + 0.01 n^2)
$$</div>
<p>When this calculator is passed to our environment, the costs of our transactions will be included in the reward. The basic reward follows equation (10) in the paper:</p>
<div class="math">$$
R_t := \delta w_t - \frac{\kappa}{2}(\delta w_t)^2
$$</div>
<p>Our state space consists of </p>
<ul>
<li>stock price</li>
<li>time to expiry</li>
<li>number of shares held</li>
</ul>
<p>Our action space is a non-negative integer below 100, to reflect the number of shares to hold (short) for the next time step. Here, since we assume we are long the call option, the stock position is going to be always short.</p>
<p>In the environment, we keep track of the evolution of the following:</p>
<ul>
<li>stock prices</li>
<li>option prices</li>
<li>number of shares held short</li>
<li>PnL for stock and option</li>
<li>cash and transaction cost</li>
</ul>
<div class="highlight"><pre><span></span><code><span class="k">class</span> <span class="nc">BasicCostCalculator</span><span class="p">:</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tick_size</span><span class="p">,</span> <span class="n">multiplier</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">tick_size</span> <span class="o">=</span> <span class="n">tick_size</span>
<span class="bp">self</span><span class="o">.</span><span class="n">multiplier</span> <span class="o">=</span> <span class="n">multiplier</span>
<span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
<span class="n">n</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">multiplier</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">tick_size</span> <span class="o">*</span> <span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mf">0.01</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">BSMEnv</span><span class="p">(</span><span class="n">gym</span><span class="o">.</span><span class="n">Env</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">S0</span><span class="p">,</span> <span class="n">K</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">kappa</span><span class="p">,</span> <span class="n">cost_calculator</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="c1"># S0: starting level</span>
<span class="c1"># K: strike price of the option</span>
<span class="c1"># r: interest rate</span>
<span class="c1"># sigma: volatility</span>
<span class="c1"># T: number of days</span>
<span class="c1"># D: number of hedging periods in a day</span>
<span class="c1"># kappa: risk-aversion parameter</span>
<span class="c1"># cost_calculator (optional): calculator to use for transaction costs</span>
<span class="bp">self</span><span class="o">.</span><span class="n">S0</span> <span class="o">=</span> <span class="n">S0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">K</span> <span class="o">=</span> <span class="n">K</span>
<span class="bp">self</span><span class="o">.</span><span class="n">r</span> <span class="o">=</span> <span class="n">r</span>
<span class="bp">self</span><span class="o">.</span><span class="n">sigma</span> <span class="o">=</span> <span class="n">sigma</span>
<span class="bp">self</span><span class="o">.</span><span class="n">T</span> <span class="o">=</span> <span class="n">T</span>
<span class="bp">self</span><span class="o">.</span><span class="n">D</span> <span class="o">=</span> <span class="n">D</span>
<span class="bp">self</span><span class="o">.</span><span class="n">kappa</span> <span class="o">=</span> <span class="n">kappa</span>
<span class="bp">self</span><span class="o">.</span><span class="n">prices</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">t</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">idx</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">num_shares</span> <span class="o">=</span> <span class="mi">0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">opt_prices</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">cost_calculator</span> <span class="o">=</span> <span class="n">cost_calculator</span>
<span class="bp">self</span><span class="o">.</span><span class="n">action_space</span> <span class="o">=</span> <span class="n">gym</span><span class="o">.</span><span class="n">spaces</span><span class="o">.</span><span class="n">Discrete</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">observation_space</span> <span class="o">=</span> <span class="n">gym</span><span class="o">.</span><span class="n">spaces</span><span class="o">.</span><span class="n">Tuple</span><span class="p">((</span>
<span class="n">gym</span><span class="o">.</span><span class="n">spaces</span><span class="o">.</span><span class="n">Box</span><span class="p">(</span><span class="mf">0.</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">),</span>
<span class="n">gym</span><span class="o">.</span><span class="n">spaces</span><span class="o">.</span><span class="n">Box</span><span class="p">(</span><span class="mf">0.</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">),</span>
<span class="n">gym</span><span class="o">.</span><span class="n">spaces</span><span class="o">.</span><span class="n">Discrete</span><span class="p">(</span><span class="mi">100</span><span class="p">),</span>
<span class="p">))</span>
<span class="k">def</span> <span class="nf">reset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prices</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">time</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prices</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">prices</span><span class="p">,</span> <span class="n">time</span> <span class="o">=</span> <span class="n">simulate_prices</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">S0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sigma</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">D</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">prices</span> <span class="o">=</span> <span class="n">prices</span><span class="p">[</span><span class="mi">0</span><span class="p">,:]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">prices</span> <span class="o">=</span> <span class="n">prices</span>
<span class="bp">self</span><span class="o">.</span><span class="n">t</span> <span class="o">=</span> <span class="n">time</span>
<span class="bp">self</span><span class="o">.</span><span class="n">idx</span> <span class="o">=</span> <span class="mi">0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">num_shares</span> <span class="o">=</span> <span class="mi">0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">opt_prices</span> <span class="o">=</span> <span class="p">[</span><span class="n">euro_vanilla</span><span class="p">(</span><span class="n">price</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">K</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">T</span> <span class="o">-</span> <span class="n">tm</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">r</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sigma</span><span class="p">,</span> <span class="s1">'call'</span><span class="p">)</span> <span class="k">for</span> <span class="n">price</span><span class="p">,</span> <span class="n">tm</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">prices</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="p">)]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">deltas</span> <span class="o">=</span> <span class="p">[</span><span class="n">euro_vanilla_delta</span><span class="p">(</span><span class="n">price</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">K</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">T</span> <span class="o">-</span> <span class="n">tm</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">r</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sigma</span><span class="p">,</span> <span class="s1">'call'</span><span class="p">)</span> <span class="k">for</span> <span class="n">price</span><span class="p">,</span> <span class="n">tm</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">prices</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="p">)]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">opt_pnl</span> <span class="o">=</span> <span class="mi">100</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">opt_prices</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">stock_pnl</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">T</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">D</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">num_shares_hist</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">T</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">D</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">cash_hist</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">T</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">D</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">cost_hist</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">T</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">D</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">cash</span> <span class="o">=</span> <span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">opt_pnl</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">cash_hist</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cash</span>
<span class="n">state</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">prices</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_shares</span><span class="p">)</span>
<span class="k">return</span> <span class="n">state</span>
<span class="k">def</span> <span class="nf">step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">):</span>
<span class="c1"># buy back the short</span>
<span class="bp">self</span><span class="o">.</span><span class="n">cash</span> <span class="o">-=</span> <span class="bp">self</span><span class="o">.</span><span class="n">prices</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_shares</span>
<span class="n">num_shares_delta</span> <span class="o">=</span> <span class="n">action</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_shares</span>
<span class="bp">self</span><span class="o">.</span><span class="n">num_shares</span> <span class="o">=</span> <span class="n">action</span>
<span class="bp">self</span><span class="o">.</span><span class="n">cash</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">prices</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_shares</span>
<span class="bp">self</span><span class="o">.</span><span class="n">num_shares_hist</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_shares</span>
<span class="bp">self</span><span class="o">.</span><span class="n">cash_hist</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cash</span>
<span class="bp">self</span><span class="o">.</span><span class="n">stock_pnl</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">prices</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="p">]</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">prices</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="o">+</span><span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="n">action</span>
<span class="n">state_next</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">prices</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="o">+</span><span class="mi">1</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">T</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="o">+</span><span class="mi">1</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_shares</span><span class="p">)</span>
<span class="n">d_wealth</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt_pnl</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="p">]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">stock_pnl</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="p">]</span>
<span class="n">reward</span> <span class="o">=</span> <span class="n">d_wealth</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">kappa</span> <span class="o">*</span> <span class="n">d_wealth</span> <span class="o">*</span> <span class="n">d_wealth</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">cost_calculator</span><span class="p">:</span>
<span class="n">cost</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cost_calculator</span><span class="p">(</span><span class="n">num_shares_delta</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">cost_hist</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">cost</span>
<span class="n">reward</span> <span class="o">-=</span> <span class="n">cost</span>
<span class="n">info</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'opt_pnl'</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt_pnl</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="p">],</span>
<span class="s1">'stock_pnl'</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">stock_pnl</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="p">],</span>
<span class="s1">'cost'</span><span class="p">:</span> <span class="n">cost</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">cost_calculator</span> <span class="k">else</span> <span class="mf">0.</span><span class="p">,</span>
<span class="p">}</span>
<span class="bp">self</span><span class="o">.</span><span class="n">idx</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="n">done</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">idx</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">T</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">D</span>
<span class="k">return</span> <span class="n">state_next</span><span class="p">,</span> <span class="n">reward</span><span class="p">,</span> <span class="n">done</span><span class="p">,</span> <span class="n">info</span>
</code></pre></div>
<p>With the environment defined, we can implement our hedging agent. First we define an abstract class <code>Hedger</code> for our hedging agent. In this class, we define a collection of abstract functions that will be invoked by our simulator. The main hedging function is <code>hedge()</code>.</p>
<div class="highlight"><pre><span></span><code><span class="k">class</span> <span class="nc">Hedger</span><span class="p">:</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">eval</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">on_batch_start</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">on_batch_end</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">on_episode_start</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">on_episode_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">env</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">on_step_start</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">on_step_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state</span><span class="p">,</span> <span class="n">reward</span><span class="p">,</span> <span class="n">info</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">hedge</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
<span class="k">pass</span>
</code></pre></div>
<p>Next we have the hedger implementing the standard Black-Scholes-Merton hedging: <code>BSMHedger</code>. This hedger uses the option's delta to determine how many shares to use for hedging, calling <code>euro_vanilla_delta()</code> defined above to calculate the option's delta.</p>
<div class="highlight"><pre><span></span><code><span class="k">class</span> <span class="nc">BSMHedger</span><span class="p">(</span><span class="n">Hedger</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">K</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">opt_type</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">K</span> <span class="o">=</span> <span class="n">K</span>
<span class="bp">self</span><span class="o">.</span><span class="n">r</span> <span class="o">=</span> <span class="n">r</span>
<span class="bp">self</span><span class="o">.</span><span class="n">sigma</span> <span class="o">=</span> <span class="n">sigma</span>
<span class="bp">self</span><span class="o">.</span><span class="n">opt_type</span> <span class="o">=</span> <span class="n">opt_type</span>
<span class="k">def</span> <span class="nf">hedge</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
<span class="n">stock_price</span><span class="p">,</span> <span class="n">time_to_expire</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">state</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="mi">100</span> <span class="o">*</span> <span class="n">euro_vanilla_delta</span><span class="p">(</span><span class="n">stock_price</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">K</span><span class="p">,</span>
<span class="n">time_to_expire</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">r</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">sigma</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">opt_type</span><span class="p">))</span>
</code></pre></div>
<p>Last but not least, we have our reinforcement-learning hedger. The key member of the hedger is <code>model</code>; <code>model</code> is a model of the Q function, i.e. the mapping between a state-action pair to the corresponding value; it represents the value of taking the action given a particular state. In a given state, the policy then is to select the action with the maximal value given by the Q function when combined with the given state.</p>
<p>The hedger can be in training mode or evaluation mode. In the training mode, we try to learn <code>model</code> while in the evaluation mode, the hedger applies the trained <code>model</code> to decide the hedging action. Before <code>model</code> is learned (initialized), the hedger always takes random actions, while after initialization, the hedger can perform <span class="math">\(\epsilon\)</span>-greedy exploration of the actions during training. At the end of each batch, we fit <code>model</code> based on the rewards obtained in that batch.</p>
<p>In the paper, there is not much detail about what they use as <code>model</code>, besides the fact that it is a nonlinear regression model. Here, we use the catboost regressor from the <a href="https://catboost.ai/">catboost</a> package, but the hedger interface itself is agnostic to this detail as long as it can call the <a href="https://scikit-learn.org/stable/index.html">sklearn</a> API (<code>fit()</code> and <code>predict()</code>) on <code>model</code>.</p>
<p>We note that the procedure done here is somewhat similar to the one described in <a href="http://jmlr.csail.mit.edu/papers/volume6/ernst05a/ernst05a.pdf">Ernst, et al (2005)</a>.</p>
<div class="highlight"><pre><span></span><code><span class="k">class</span> <span class="nc">RLHedger</span><span class="p">(</span><span class="n">Hedger</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">eps</span><span class="p">,</span> <span class="n">gamma</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
<span class="bp">self</span><span class="o">.</span><span class="n">initialized</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">eps</span> <span class="o">=</span> <span class="n">eps</span>
<span class="bp">self</span><span class="o">.</span><span class="n">gamma</span> <span class="o">=</span> <span class="n">gamma</span>
<span class="bp">self</span><span class="o">.</span><span class="n">state_prev</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">action_prev</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">training</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">X_pred</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">101</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">101</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">X_pred</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span>
<span class="bp">self</span><span class="o">.</span><span class="n">X</span> <span class="o">=</span> <span class="p">[]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">def</span> <span class="nf">eval</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">training</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">training</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">def</span> <span class="nf">on_batch_start</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">training</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">X</span> <span class="o">=</span> <span class="p">[]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">def</span> <span class="nf">on_batch_end</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">training</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">X</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">initialized</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">def</span> <span class="nf">on_step_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state_next</span><span class="p">,</span> <span class="n">reward</span><span class="p">,</span> <span class="n">info</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">training</span><span class="p">:</span>
<span class="n">x</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">state_prev</span><span class="p">)</span> <span class="o">+</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">action_prev</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">reward</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">initialized</span><span class="p">:</span>
<span class="n">action</span><span class="p">,</span> <span class="n">q</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_action</span><span class="p">(</span><span class="n">state_next</span><span class="p">)</span>
<span class="n">y</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">gamma</span> <span class="o">*</span> <span class="n">q</span>
<span class="bp">self</span><span class="o">.</span><span class="n">X</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">X_pred</span><span class="p">[:,:</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">state</span><span class="p">))</span>
<span class="n">preds</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">X_pred</span><span class="p">)</span>
<span class="n">idx</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">preds</span><span class="p">)</span>
<span class="n">action</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">X_pred</span><span class="p">[</span><span class="n">idx</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">q</span> <span class="o">=</span> <span class="n">preds</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>
<span class="k">return</span> <span class="n">action</span><span class="p">,</span> <span class="n">q</span>
<span class="k">def</span> <span class="nf">hedge</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">state_prev</span> <span class="o">=</span> <span class="n">state</span>
<span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">initialized</span> <span class="ow">or</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">training</span> <span class="ow">and</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">()</span> <span class="o"><</span> <span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">eps_idx</span><span class="p">]):</span>
<span class="n">action</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">101</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">action</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_action</span><span class="p">(</span><span class="n">state</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">action_prev</span> <span class="o">=</span> <span class="n">action</span>
<span class="k">return</span> <span class="n">action</span>
</code></pre></div>
<p>We are now ready to implement functions for running our simulations. First, we implement the function to run a specific episode.</p>
<div class="highlight"><pre><span></span><code><span class="k">def</span> <span class="nf">run_episode</span><span class="p">(</span><span class="n">episode_idx</span><span class="p">,</span> <span class="n">env</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">eps</span><span class="p">,</span> <span class="n">gamma</span><span class="p">):</span>
<span class="n">hedger</span> <span class="o">=</span> <span class="n">RLHedger</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">eps</span><span class="p">,</span> <span class="n">gamma</span><span class="p">)</span>
<span class="n">hedger</span><span class="o">.</span><span class="n">on_episode_start</span><span class="p">()</span>
<span class="n">state</span> <span class="o">=</span> <span class="n">env</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
<span class="n">done</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">while</span> <span class="ow">not</span> <span class="n">done</span><span class="p">:</span>
<span class="n">hedger</span><span class="o">.</span><span class="n">on_step_start</span><span class="p">()</span>
<span class="n">action</span> <span class="o">=</span> <span class="n">hedger</span><span class="o">.</span><span class="n">hedge</span><span class="p">(</span><span class="n">state</span><span class="p">)</span>
<span class="n">state</span><span class="p">,</span> <span class="n">reward</span><span class="p">,</span> <span class="n">done</span><span class="p">,</span> <span class="n">info</span> <span class="o">=</span> <span class="n">env</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">action</span><span class="p">)</span>
<span class="n">hedger</span><span class="o">.</span><span class="n">on_step_end</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">reward</span><span class="p">,</span> <span class="n">info</span><span class="p">)</span>
<span class="n">hedger</span><span class="o">.</span><span class="n">on_episode_end</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">stack</span><span class="p">(</span><span class="n">hedger</span><span class="o">.</span><span class="n">X</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">stack</span><span class="p">(</span><span class="n">hedger</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>
</code></pre></div>
<p>Note that there are no dependencies between episodes. We can take advantage of this property and parallelize <code>run_episode()</code> to speed up training. This is done in <code>run_training()</code> below.</p>
<div class="highlight"><pre><span></span><code><span class="k">def</span> <span class="nf">run_training</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">hedger</span><span class="p">,</span> <span class="n">eps</span><span class="p">,</span> <span class="n">gamma</span><span class="p">,</span> <span class="n">nbatches</span><span class="p">,</span> <span class="n">nepisodes</span><span class="p">):</span>
<span class="k">for</span> <span class="n">batch_idx</span> <span class="ow">in</span> <span class="n">tqdm_notebook</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">nbatches</span><span class="p">)):</span>
<span class="n">hedger</span><span class="o">.</span><span class="n">on_batch_start</span><span class="p">()</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">Parallel</span><span class="p">(</span><span class="n">n_jobs</span><span class="o">=</span><span class="mi">8</span><span class="p">)(</span><span class="n">delayed</span><span class="p">(</span><span class="n">partial</span><span class="p">(</span><span class="n">run_episode</span><span class="p">,</span> <span class="n">env</span><span class="o">=</span><span class="n">env</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="n">hedger</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">eps</span><span class="o">=</span><span class="n">eps</span><span class="p">[</span><span class="n">batch_idx</span><span class="p">],</span> <span class="n">gamma</span><span class="o">=</span><span class="n">gamma</span><span class="p">))(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nepisodes</span><span class="p">))</span>
<span class="n">X</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">y</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nepisodes</span><span class="p">):</span>
<span class="n">X_curr</span><span class="p">,</span> <span class="n">y_curr</span> <span class="o">=</span> <span class="n">result</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>
<span class="n">X</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">X_curr</span><span class="p">)</span>
<span class="n">y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">y_curr</span><span class="p">)</span>
<span class="n">X_arr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="n">y_arr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="n">hedger</span><span class="o">.</span><span class="n">X</span> <span class="o">=</span> <span class="n">X_arr</span>
<span class="n">hedger</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y_arr</span>
<span class="n">hedger</span><span class="o">.</span><span class="n">on_batch_end</span><span class="p">()</span>
</code></pre></div>
<p>We also implement the out-of-sample testing function, <code>run_test()</code>. One difference here is that we run the simulation for both the trained reinforcement-learning hedger and the Black-Scholes-Merton hedger. In this function, we also collect the PnL and transaction-cost history of each episode. This function can potentially be parallelized, but we do not do that here.</p>
<div class="highlight"><pre><span></span><code><span class="k">def</span> <span class="nf">run_test</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">hedger</span><span class="p">,</span> <span class="n">env_ref</span><span class="p">,</span> <span class="n">hedger_ref</span><span class="p">,</span> <span class="n">nepisodes</span><span class="p">):</span>
<span class="n">pnls</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">pnls_ref</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">costs</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">costs_ref</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">pnls_info</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">pnls_info_ref</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">hedger</span><span class="o">.</span><span class="n">eval</span><span class="p">()</span>
<span class="n">hedger_ref</span><span class="o">.</span><span class="n">eval</span><span class="p">()</span>
<span class="k">for</span> <span class="n">episode_idx</span> <span class="ow">in</span> <span class="n">tqdm_notebook</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">nepisodes</span><span class="p">)):</span>
<span class="n">state</span> <span class="o">=</span> <span class="n">env</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
<span class="n">hedger</span><span class="o">.</span><span class="n">on_episode_start</span><span class="p">()</span>
<span class="n">done</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">pnl</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">cost</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">pnl_list</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">while</span> <span class="ow">not</span> <span class="n">done</span><span class="p">:</span>
<span class="n">hedger</span><span class="o">.</span><span class="n">on_step_start</span><span class="p">()</span>
<span class="n">action</span> <span class="o">=</span> <span class="n">hedger</span><span class="o">.</span><span class="n">hedge</span><span class="p">(</span><span class="n">state</span><span class="p">)</span>
<span class="n">state</span><span class="p">,</span> <span class="n">reward</span><span class="p">,</span> <span class="n">done</span><span class="p">,</span> <span class="n">info</span> <span class="o">=</span> <span class="n">env</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">action</span><span class="p">)</span>
<span class="n">pnl_curr</span> <span class="o">=</span> <span class="n">info</span><span class="p">[</span><span class="s1">'opt_pnl'</span><span class="p">]</span> <span class="o">+</span> <span class="n">info</span><span class="p">[</span><span class="s1">'stock_pnl'</span><span class="p">]</span> <span class="o">-</span> <span class="n">info</span><span class="p">[</span><span class="s1">'cost'</span><span class="p">]</span>
<span class="n">pnl_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pnl_curr</span><span class="p">)</span>
<span class="n">pnl</span> <span class="o">+=</span> <span class="n">pnl_curr</span>
<span class="n">cost</span> <span class="o">+=</span> <span class="n">info</span><span class="p">[</span><span class="s1">'cost'</span><span class="p">]</span>
<span class="n">hedger</span><span class="o">.</span><span class="n">on_step_end</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">reward</span><span class="p">,</span> <span class="n">info</span><span class="p">)</span>
<span class="n">hedger</span><span class="o">.</span><span class="n">on_episode_end</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
<span class="n">pnls</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pnl</span><span class="p">)</span>
<span class="n">costs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">cost</span><span class="p">)</span>
<span class="n">pnls_info</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pnl_list</span><span class="p">)</span>
<span class="n">state</span> <span class="o">=</span> <span class="n">env_ref</span><span class="o">.</span><span class="n">reset</span><span class="p">(</span><span class="n">env</span><span class="o">.</span><span class="n">prices</span><span class="p">,</span> <span class="n">env</span><span class="o">.</span><span class="n">t</span><span class="p">)</span>
<span class="n">hedger_ref</span><span class="o">.</span><span class="n">on_episode_start</span><span class="p">()</span>
<span class="n">done</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">pnl</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">cost</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">pnl_list</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">while</span> <span class="ow">not</span> <span class="n">done</span><span class="p">:</span>
<span class="n">hedger_ref</span><span class="o">.</span><span class="n">on_step_start</span><span class="p">()</span>
<span class="n">action</span> <span class="o">=</span> <span class="n">hedger_ref</span><span class="o">.</span><span class="n">hedge</span><span class="p">(</span><span class="n">state</span><span class="p">)</span>
<span class="n">state</span><span class="p">,</span> <span class="n">reward</span><span class="p">,</span> <span class="n">done</span><span class="p">,</span> <span class="n">info</span> <span class="o">=</span> <span class="n">env_ref</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">action</span><span class="p">)</span>
<span class="n">pnl_curr</span> <span class="o">=</span> <span class="n">info</span><span class="p">[</span><span class="s1">'opt_pnl'</span><span class="p">]</span> <span class="o">+</span> <span class="n">info</span><span class="p">[</span><span class="s1">'stock_pnl'</span><span class="p">]</span> <span class="o">-</span> <span class="n">info</span><span class="p">[</span><span class="s1">'cost'</span><span class="p">]</span>
<span class="n">pnl_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pnl_curr</span><span class="p">)</span>
<span class="n">pnl</span> <span class="o">+=</span> <span class="n">pnl_curr</span>
<span class="n">cost</span> <span class="o">+=</span> <span class="n">info</span><span class="p">[</span><span class="s1">'cost'</span><span class="p">]</span>
<span class="n">hedger_ref</span><span class="o">.</span><span class="n">on_step_end</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">reward</span><span class="p">,</span> <span class="n">info</span><span class="p">)</span>
<span class="n">hedger_ref</span><span class="o">.</span><span class="n">on_episode_end</span><span class="p">(</span><span class="n">env_ref</span><span class="p">)</span>
<span class="n">pnls_ref</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pnl</span><span class="p">)</span>
<span class="n">costs_ref</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">cost</span><span class="p">)</span>
<span class="n">pnls_info_ref</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pnl_list</span><span class="p">)</span>
<span class="k">return</span> <span class="n">pnls</span><span class="p">,</span> <span class="n">pnls_ref</span><span class="p">,</span> <span class="n">costs</span><span class="p">,</span> <span class="n">costs_ref</span><span class="p">,</span> <span class="n">pnls_info</span><span class="p">,</span> <span class="n">pnls_info_ref</span>
</code></pre></div>
<h3>Training</h3>
<p>Finally, we are ready to train our reinforcement-learning hedger. First we setup some constants, most of which follow what are used in the paper.</p>
<div class="highlight"><pre><span></span><code><span class="n">NBATCHES</span> <span class="o">=</span> <span class="mi">5</span>
<span class="n">NEPISODES</span> <span class="o">=</span> <span class="mi">15000</span>
<span class="n">NEPISODES_TEST</span> <span class="o">=</span> <span class="mi">10000</span>
<span class="n">T</span> <span class="o">=</span> <span class="mi">10</span>
<span class="n">D</span> <span class="o">=</span> <span class="mi">5</span>
<span class="n">S0</span> <span class="o">=</span> <span class="mi">100</span>
<span class="n">SIGMA</span> <span class="o">=</span> <span class="mf">0.01</span>
<span class="n">KAPPA</span> <span class="o">=</span> <span class="mf">0.1</span>
<span class="n">GAMMA</span> <span class="o">=</span> <span class="mf">0.9</span>
<span class="n">EPS</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.09</span><span class="p">,</span> <span class="mf">0.08</span><span class="p">,</span> <span class="mf">0.07</span><span class="p">,</span> <span class="mf">0.06</span><span class="p">]</span>
<span class="n">TICK_SIZE</span> <span class="o">=</span> <span class="mf">0.1</span>
<span class="n">MULTIPLIER</span> <span class="o">=</span> <span class="mf">5.</span>
</code></pre></div>
<p>We first train the hedger when there are no transaction costs.</p>
<div class="highlight"><pre><span></span><code><span class="n">env</span> <span class="o">=</span> <span class="n">BSMEnv</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="n">S0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">SIGMA</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">KAPPA</span><span class="p">)</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">CatBoostRegressor</span><span class="p">(</span><span class="n">thread_count</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="n">hedger</span> <span class="o">=</span> <span class="n">RLHedger</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">EPS</span><span class="p">,</span> <span class="n">GAMMA</span><span class="p">)</span>
<span class="n">run_training</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">hedger</span><span class="p">,</span> <span class="n">EPS</span><span class="p">,</span> <span class="n">GAMMA</span><span class="p">,</span> <span class="n">NBATCHES</span><span class="p">,</span> <span class="n">NEPISODES</span><span class="p">)</span>
</code></pre></div>
<p>Then we train the hedger with transaction costs.</p>
<div class="highlight"><pre><span></span><code><span class="n">env_cost</span> <span class="o">=</span> <span class="n">BSMEnv</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="n">S0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">SIGMA</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">KAPPA</span><span class="p">,</span> <span class="n">cost_calculator</span><span class="o">=</span><span class="n">BasicCostCalculator</span><span class="p">(</span><span class="n">tick_size</span><span class="o">=</span><span class="n">TICK_SIZE</span><span class="p">,</span> <span class="n">multiplier</span><span class="o">=</span><span class="n">MULTIPLIER</span><span class="p">))</span>
<span class="n">model_cost</span> <span class="o">=</span> <span class="n">CatBoostRegressor</span><span class="p">(</span><span class="n">thread_count</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="n">hedger_cost</span> <span class="o">=</span> <span class="n">RLHedger</span><span class="p">(</span><span class="n">model_cost</span><span class="p">,</span> <span class="n">EPS</span><span class="p">,</span> <span class="n">GAMMA</span><span class="p">)</span>
<span class="n">run_training</span><span class="p">(</span><span class="n">env_cost</span><span class="p">,</span> <span class="n">hedger_cost</span><span class="p">,</span> <span class="n">EPS</span><span class="p">,</span> <span class="n">GAMMA</span><span class="p">,</span> <span class="n">NBATCHES</span><span class="p">,</span> <span class="n">NEPISODES</span><span class="p">)</span>
</code></pre></div>
<h3>Out-of-sample testing</h3>
<p>Let us now evaluate the hedgers we trained with out-of-sample cases. First we setup the reference Black-Scholes-Merton hedger, and the environments for testing: one environment for our trained hedger and one environment for the reference hedger.</p>
<div class="highlight"><pre><span></span><code><span class="n">hedger_ref</span> <span class="o">=</span> <span class="n">BSMHedger</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">SIGMA</span><span class="p">,</span> <span class="s1">'call'</span><span class="p">)</span>
<span class="n">env</span> <span class="o">=</span> <span class="n">BSMEnv</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="n">S0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">SIGMA</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">KAPPA</span><span class="p">)</span>
<span class="n">env_ref</span> <span class="o">=</span> <span class="n">BSMEnv</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="n">S0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">SIGMA</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">KAPPA</span><span class="p">)</span>
<span class="n">env_cost</span> <span class="o">=</span> <span class="n">BSMEnv</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="n">S0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">SIGMA</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">KAPPA</span><span class="p">,</span> <span class="n">cost_calculator</span><span class="o">=</span><span class="n">BasicCostCalculator</span><span class="p">(</span><span class="n">tick_size</span><span class="o">=</span><span class="n">TICK_SIZE</span><span class="p">,</span> <span class="n">multiplier</span><span class="o">=</span><span class="n">MULTIPLIER</span><span class="p">))</span>
<span class="n">env_ref_cost</span> <span class="o">=</span> <span class="n">BSMEnv</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="n">S0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">SIGMA</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">KAPPA</span><span class="p">,</span> <span class="n">cost_calculator</span><span class="o">=</span><span class="n">BasicCostCalculator</span><span class="p">(</span><span class="n">tick_size</span><span class="o">=</span><span class="n">TICK_SIZE</span><span class="p">,</span> <span class="n">multiplier</span><span class="o">=</span><span class="n">MULTIPLIER</span><span class="p">))</span>
</code></pre></div>
<p>With those setup, we can run the out-of-sample simulations for both the no-transaction-cost and the with-transaction-cost cases.</p>
<div class="highlight"><pre><span></span><code><span class="n">pnls</span><span class="p">,</span> <span class="n">pnls_ref</span><span class="p">,</span> <span class="n">costs</span><span class="p">,</span> <span class="n">costs_ref</span><span class="p">,</span> <span class="n">pnls_info</span><span class="p">,</span> <span class="n">pnls_info_ref</span> <span class="o">=</span> <span class="n">run_test</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">hedger</span><span class="p">,</span> <span class="n">env_ref</span><span class="p">,</span> <span class="n">hedger_ref</span><span class="p">,</span> <span class="n">NEPISODES_TEST</span><span class="p">)</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="n">pnls_cost</span><span class="p">,</span> <span class="n">pnls_ref_cost</span><span class="p">,</span> <span class="n">costs_cost</span><span class="p">,</span> <span class="n">costs_ref_cost</span><span class="p">,</span> <span class="n">pnls_info_cost</span><span class="p">,</span> <span class="n">pnls_info_ref_cost</span> <span class="o">=</span> <span class="n">run_test</span><span class="p">(</span><span class="n">env_cost</span><span class="p">,</span> <span class="n">hedger_cost</span><span class="p">,</span> <span class="n">env_ref_cost</span><span class="p">,</span> <span class="n">hedger_ref</span><span class="p">,</span> <span class="n">NEPISODES_TEST</span><span class="p">)</span>
</code></pre></div>
<h4>No transaction costs</h4>
<p>We first consider the results in the no-transaction-costs case. We plot the density estimates of the total PnLs in both cases. Below we see that both are centered at zero, with the RLHedger case having slightly fatter tail. Performing the Welch two-sample t-test, we find that the means of the two cases are not statistically significantly different.</p>
<div class="highlight"><pre><span></span><code><span class="n">sns</span><span class="o">.</span><span class="n">kdeplot</span><span class="p">(</span><span class="n">pnls</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">'RLHedger'</span><span class="p">)</span>
<span class="n">sns</span><span class="o">.</span><span class="n">kdeplot</span><span class="p">(</span><span class="n">pnls_ref</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">'BSMHedger'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
</code></pre></div>
<p><img alt="png" src="/images/kolm_ritter/output_35_1.png"></p>
<div class="highlight"><pre><span></span><code><span class="n">si</span><span class="o">.</span><span class="n">ttest_ind</span><span class="p">(</span><span class="n">pnls</span><span class="p">,</span> <span class="n">pnls_ref</span><span class="p">,</span> <span class="n">equal_var</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code>Ttest_indResult(statistic=-0.005393764800999482, pvalue=0.9956964757476952)
</code></pre></div>
<p>Let us dig into a specific episode. We first plot the stock prices for this episode.</p>
<div class="highlight"><pre><span></span><code><span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">env</span><span class="o">.</span><span class="n">prices</span><span class="p">)</span>
</code></pre></div>
<p><img alt="png" src="/images/kolm_ritter/output_38_1.png"></p>
<p>Next we print out the total PnLs and plot the evolution of the PnLs for both RLHedger and BSMHedger. In both cases, there are some small residual total PnLs.</p>
<div class="highlight"><pre><span></span><code><span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">env</span><span class="o">.</span><span class="n">opt_pnl</span> <span class="o">+</span> <span class="n">env</span><span class="o">.</span><span class="n">stock_pnl</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">env</span><span class="o">.</span><span class="n">opt_pnl</span> <span class="o">+</span> <span class="n">env</span><span class="o">.</span><span class="n">stock_pnl</span><span class="p">))</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code>-1.0714312643296595
</code></pre></div>
<p><img alt="png" src="/images/kolm_ritter/output_40_1.png"></p>
<div class="highlight"><pre><span></span><code><span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">env_ref</span><span class="o">.</span><span class="n">opt_pnl</span> <span class="o">+</span> <span class="n">env_ref</span><span class="o">.</span><span class="n">stock_pnl</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">env_ref</span><span class="o">.</span><span class="n">opt_pnl</span> <span class="o">+</span> <span class="n">env_ref</span><span class="o">.</span><span class="n">stock_pnl</span><span class="p">))</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code>-11.22226031466913
</code></pre></div>
<p><img alt="png" src="/images/kolm_ritter/output_41_1.png"></p>
<p>Next we compare the evolution of the number of shares held short by the RLHedger and the delta of the option. Qualitatively, they look similar.</p>
<div class="highlight"><pre><span></span><code><span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">env</span><span class="o">.</span><span class="n">num_shares_hist</span><span class="p">)</span>
</code></pre></div>
<p><img alt="png" src="/images/kolm_ritter/output_43_1.png"></p>
<div class="highlight"><pre><span></span><code><span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">env</span><span class="o">.</span><span class="n">deltas</span><span class="p">)</span>
</code></pre></div>
<p><img alt="png" src="/images/kolm_ritter/output_44_1.png"></p>
<h4>With transaction costs</h4>
<p>Now we consider the case with transaction costs. Looking at the density estimates of the total PnLs, we clearly see a significant difference, and indeed this is also verified by the Welch two-sample t-test (p value 0.0).</p>
<div class="highlight"><pre><span></span><code><span class="n">sns</span><span class="o">.</span><span class="n">kdeplot</span><span class="p">(</span><span class="n">pnls_cost</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">'RLHedger'</span><span class="p">)</span>
<span class="n">sns</span><span class="o">.</span><span class="n">kdeplot</span><span class="p">(</span><span class="n">pnls_ref_cost</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">'BSMHedger'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
</code></pre></div>
<p><img alt="png" src="/images/kolm_ritter/output_46_1.png"></p>
<div class="highlight"><pre><span></span><code><span class="n">si</span><span class="o">.</span><span class="n">ttest_ind</span><span class="p">(</span><span class="n">pnls_cost</span><span class="p">,</span> <span class="n">pnls_ref_cost</span><span class="p">,</span> <span class="n">equal_var</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code>Ttest_indResult(statistic=147.77669475319246, pvalue=0.0)
</code></pre></div>
<p>Unlike in the paper, we do find the realized volatility of the PnLs are significantly different for the two cases.</p>
<div class="highlight"><pre><span></span><code><span class="n">vols_cost</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">pnls_info_cost</span><span class="p">]</span>
<span class="n">vols_ref_cost</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">pnls_info_ref_cost</span><span class="p">]</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="n">sns</span><span class="o">.</span><span class="n">kdeplot</span><span class="p">(</span><span class="n">vols_cost</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">'RLHedger'</span><span class="p">)</span>
<span class="n">sns</span><span class="o">.</span><span class="n">kdeplot</span><span class="p">(</span><span class="n">vols_ref_cost</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">'BSMHedger'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
</code></pre></div>
<p><img alt="png" src="/images/kolm_ritter/output_50_1.png"></p>
<div class="highlight"><pre><span></span><code><span class="n">si</span><span class="o">.</span><span class="n">ttest_ind</span><span class="p">(</span><span class="n">vols_cost</span><span class="p">,</span> <span class="n">vols_ref_cost</span><span class="p">,</span> <span class="n">equal_var</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code>Ttest_indResult(statistic=-31.544498387590423, pvalue=8.290086183138852e-213)
</code></pre></div>
<p>Next we show the density estimates of the total costs. It is clear that the costs for the BSMHedger is significantly higher compared to those for the RLHedger.</p>
<div class="highlight"><pre><span></span><code><span class="n">sns</span><span class="o">.</span><span class="n">kdeplot</span><span class="p">(</span><span class="n">costs_cost</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">'RLHedger'</span><span class="p">)</span>
<span class="n">sns</span><span class="o">.</span><span class="n">kdeplot</span><span class="p">(</span><span class="n">costs_ref_cost</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">'BSMHedger'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
</code></pre></div>
<p><img alt="png" src="/images/kolm_ritter/output_53_1.png"></p>
<p>Similar to the paper, we also compute and plot the density estimates of the t-statistics of the PnLs in both cases. The plot shows that the PnLs in the BSMHedger case are more significantly different from zero compared to the PnLs in the RLHedger case.</p>
<div class="highlight"><pre><span></span><code><span class="n">tstats_cost</span> <span class="o">=</span> <span class="n">si</span><span class="o">.</span><span class="n">ttest_1samp</span><span class="p">(</span><span class="n">pnls_info_cost</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
<span class="n">tstats_ref_cost</span> <span class="o">=</span> <span class="n">si</span><span class="o">.</span><span class="n">ttest_1samp</span><span class="p">(</span><span class="n">pnls_info_ref_cost</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="n">sns</span><span class="o">.</span><span class="n">kdeplot</span><span class="p">(</span><span class="n">tstats_cost</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">'RLHedger'</span><span class="p">)</span>
<span class="n">sns</span><span class="o">.</span><span class="n">kdeplot</span><span class="p">(</span><span class="n">tstats_ref_cost</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">'BSMHedger'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
</code></pre></div>
<p><img alt="png" src="/images/kolm_ritter/output_56_1.png"></p>
<p>Next we consider a particular episode, in particular, comparing the number of shares held short versus the delta for the RLHedger case. Here we see that the RLHedger case performs the hedging more gradually compared to what is suggested by the delta; for instance, in the beginning, even though delta is higher (around 0.5 since in the beginning the option is at the money), the RLHedger starts with shorting around 20 shares of stock.</p>
<div class="highlight"><pre><span></span><code><span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">env_cost</span><span class="o">.</span><span class="n">num_shares_hist</span><span class="p">)</span>
</code></pre></div>
<p><img alt="png" src="/images/kolm_ritter/output_58_1.png"></p>
<div class="highlight"><pre><span></span><code><span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">env_cost</span><span class="o">.</span><span class="n">deltas</span><span class="p">)</span>
</code></pre></div>
<p><img alt="png" src="/images/kolm_ritter/output_59_1.png"></p>
<h3>Saving models</h3>
<p>Let us now save the learned catboost models.</p>
<div class="highlight"><pre><span></span><code><span class="n">hedger</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">save_model</span><span class="p">(</span><span class="s1">'catboost_nocost'</span><span class="p">)</span>
<span class="n">hedger_cost</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">save_model</span><span class="p">(</span><span class="s1">'catboost_cost'</span><span class="p">)</span>
</code></pre></div>
<script type="text/javascript">if (!document.getElementById('mathjaxscript_pelican_#%@#$@#')) {
var align = "center",
indent = "0em",
linebreak = "false";
if (false) {
align = (screen.width < 768) ? "left" : align;
indent = (screen.width < 768) ? "0em" : indent;
linebreak = (screen.width < 768) ? 'true' : linebreak;
}
var mathjaxscript = document.createElement('script');
mathjaxscript.id = 'mathjaxscript_pelican_#%@#$@#';
mathjaxscript.type = 'text/javascript';
mathjaxscript.src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=TeX-AMS-MML_HTMLorMML';
var configscript = document.createElement('script');
configscript.type = 'text/x-mathjax-config';
configscript[(window.opera ? "innerHTML" : "text")] =
"MathJax.Hub.Config({" +
" config: ['MMLorHTML.js']," +
" TeX: { extensions: ['AMSmath.js','AMSsymbols.js','noErrors.js','noUndefined.js'], equationNumbers: { autoNumber: 'none' } }," +
" jax: ['input/TeX','input/MathML','output/HTML-CSS']," +
" extensions: ['tex2jax.js','mml2jax.js','MathMenu.js','MathZoom.js']," +
" displayAlign: '"+ align +"'," +
" displayIndent: '"+ indent +"'," +
" showMathMenu: true," +
" messageStyle: 'normal'," +
" tex2jax: { " +
" inlineMath: [ ['\\\\(','\\\\)'] ], " +
" displayMath: [ ['$$','$$'] ]," +
" processEscapes: true," +
" preview: 'TeX'," +
" }, " +
" 'HTML-CSS': { " +
" availableFonts: ['STIX', 'TeX']," +
" preferredFont: 'STIX'," +
" styles: { '.MathJax_Display, .MathJax .mo, .MathJax .mi, .MathJax .mn': {color: 'inherit ! important'} }," +
" linebreaks: { automatic: "+ linebreak +", width: '90% container' }," +
" }, " +
"}); " +
"if ('default' !== 'default') {" +
"MathJax.Hub.Register.StartupHook('HTML-CSS Jax Ready',function () {" +
"var VARIANT = MathJax.OutputJax['HTML-CSS'].FONTDATA.VARIANT;" +
"VARIANT['normal'].fonts.unshift('MathJax_default');" +
"VARIANT['bold'].fonts.unshift('MathJax_default-bold');" +
"VARIANT['italic'].fonts.unshift('MathJax_default-italic');" +
"VARIANT['-tex-mathit'].fonts.unshift('MathJax_default-italic');" +
"});" +
"MathJax.Hub.Register.StartupHook('SVG Jax Ready',function () {" +
"var VARIANT = MathJax.OutputJax.SVG.FONTDATA.VARIANT;" +
"VARIANT['normal'].fonts.unshift('MathJax_default');" +
"VARIANT['bold'].fonts.unshift('MathJax_default-bold');" +
"VARIANT['italic'].fonts.unshift('MathJax_default-italic');" +
"VARIANT['-tex-mathit'].fonts.unshift('MathJax_default-italic');" +
"});" +
"}";
(document.body || document.getElementsByTagName('head')[0]).appendChild(configscript);
(document.body || document.getElementsByTagName('head')[0]).appendChild(mathjaxscript);
}
</script>
</div>
<div class="tag-cloud">
<p>
</p>
</div>
<!-- Disqus -->
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'irustandi-github-io';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>
Please enable JavaScript to view comments.
</noscript>
<!-- End Disqus -->
</article>
<footer>
<p>© </p>
<p>
Built with <a href="http://getpelican.com" target="_blank">Pelican</a> using <a href="http://bit.ly/flex-pelican" target="_blank">Flex</a> theme
</p> </footer>
</main>
<script type="application/ld+json">
{
"@context" : "http://schema.org",
"@type" : "Blog",
"name": " Indra's Technical Musings ",
"url" : "",
"image": "",
"description": ""
}
</script>
</body>
</html>