-
Notifications
You must be signed in to change notification settings - Fork 7
/
finite_difference.py
1364 lines (1249 loc) · 51.8 KB
/
finite_difference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Implement the explicit finite difference method to calculate the price of
various options
@author: ucaiado
Created on 07/03/2016
"""
# import libraries
import matplotlib.pylab as plt
import math
import numpy as np
import pandas as pd
from scipy import stats
import seaborn as sns
import time
from scipy.optimize import minimize
'''
Begin help functions
'''
class STABILITY_ERROR(Exception):
'''
STABILITY_ERROR is raised by the init method of the Grid class
'''
pass
class UNSUCCESSFUL_ERROR(Exception):
'''
UNSUCCESSFUL_ERROR is raised by the static hedhing minimization
'''
pass
def get_d1_and_d2(f_St, f_sigma, f_time, f_r, f_K):
'''
Calculate the d1 and d2 parameter used in Digital and call options
'''
f_d2 = (np.log(f_St/f_K) - (f_r - 0.5 * f_sigma ** 2)*f_time)
f_d2 /= (f_sigma * f_time**0.5)
f_d1 = f_d2 + f_sigma*f_time**0.5
return f_d1, f_d2
def bilinear_interpolation(f_S, f_time, df):
'''
Get information from simulations matrix using bilinear interpolation
:param f_S: float. asset price
:param f_time: float. time in years
:param df: dataframe. information to be interpolated
'''
# encontro linhas e colunas relevantes
f_col1 = df.columns[df.columns < f_time][-1]
f_col2 = df.columns[df.columns >= f_time][0]
f_row1 = df.index[df.index < f_S][-1]
f_row2 = df.index[df.index >= f_S][0]
# defino pontos e areas
l_V = [df.loc[f_row1, f_col1], df.loc[f_row1, f_col2],
df.loc[f_row2, f_col2], df.loc[f_row2, f_col1]]
l_A = [(f_row2 - f_S) * (f_col2 - f_time),
(f_row2 - f_S) * (f_time - f_col1),
(f_S - f_row1) * (f_time - f_col1),
(f_S - f_row1) * (f_col2 - f_time)]
# interpolo valores
return sum(np.array(l_V)*np.array(l_A))/sum(np.array(l_A))
'''
End help functions
'''
class GridNode(object):
'''
A representation of a Node of a Grid
'''
def __init__(self, i, k):
'''
Initialize a GridNode object
:param k: integer. the time index
:param i: integer. the asset index
'''
# inicia variaveis de controle
self.i = i # linhas sao os passos do asset
self.k = k # colunas sao os passos no tempo
self.node_idx = '{:.0f},{:.0f}'.format(i, k)
# inicia variaveis para precificacao
self.f_asset_value = 0
self.f_option_value = 0
self.f_delta = 0
self.f_gamma = 0
self.f_theta = 0
# inicia variaveis para guardar valores analiticos
self.f_option_value_anlt = 0
self.f_delta_anlt = 0
self.f_gamma_anlt = 0
def __str__(self):
'''
Return node_idx
'''
return self.node_idx
def __repr__(self):
'''
Return the node_idx
'''
return self.node_idx
def __eq__(self, other):
'''
Return if a node has different node_idx from the other
:param other: node object. Node to be compared
'''
return self.node_idx == other.node_idx
def __ne__(self, other):
'''
Return if a node has the same node_idx from the other
:param other: node object. Node to be compared
'''
return not self.__eq__(other)
def __hash__(self):
'''
Allow the node object be used as a key in a hash
table
'''
return self.node_idx.__hash__()
class Grid(object):
'''
A general representation of a Grid to be used by Derivative classes in the
discretization of their domains
'''
def __init__(self, f_vol, f_value, f_time, i_nas, i_nts=None):
'''
Initialize a Grid object. Save all parameters as attributes
:param f_vol: float. Volatility of the underlying instrument
:param f_val: float. The reference value to calculate the grid length
:param f_time: float. time to be used in the grid
:param i_nas: integer. Number of asset steps
:*param i_nts: integer. Number of time steps
'''
# inicia variaveis e usa vol para garantir estabilidade
self.f_nas = 1. * i_nas
# 'infinito' eh duas vezes o valor
self.dS = 2 * f_value / self.f_nas
# como o wilmott garantiu estabilidade
self.dt = 0.9 / f_vol**2. / self.f_nas**2.
self.i_nts = int(f_time/self.dt) + 1
if i_nts:
if i_nts <= self.i_nts:
self.i_nts = i_nts-1
else:
s_err = 'The maximum of time steps is {}'
raise STABILITY_ERROR(s_err.format(self.i_nts))
self.dt = f_time / (self.i_nts * 1.)
# inicia grid. O ponto do tempo inicial eh o final, na verdade
self.grid = {}
for k in xrange(int(self.i_nts) + 1):
for i in xrange(int(self.f_nas) + 1):
node = GridNode(i, k)
self.grid[node] = node
def __call__(self, i, k):
'''
Allow direct access to the nodes of the object
:param k: integer. the time index
:param i: integer. the asset index
'''
node_idx = GridNode(i, k)
return self.grid[node_idx]
def __str__(self):
'''
A string representation of the node
'''
s_aux = ''
df_rtn = pd.DataFrame(np.zeros([int(self.f_nas),
int(self.i_nts)]))
for k in xrange(int(self.i_nts) + 1):
for i in xrange(int(self.f_nas) + 1):
valid_node = self(i, k)
df_rtn.ix[i, k] = valid_node
return str(df_rtn)
class Derivative(object):
'''
A general representation of a Derivative contract.
'''
def __init__(self, f_St, f_sigma, f_time, f_r, i_nas, f_K=None,
i_nts=None, f_sigmam=None):
'''
Initialize a Derivative object. Save all parameters as attributes
:param f_St: float. The price of the underline asset
:param f_sigma: float. A non negative underline volatility
:param f_time: float. The time remain until the expiration
:param f_r: float. The free intereset rate
:param i_nas: integer. Number of asset steps
:*param f_K: float. The strike, if applyable
:*param i_nas: integer. Number of time steps
:*param f_sigmam: float. The minimum volatility observed. If it is set
fs_sigma is the maximum volatility observed
'''
# inicia variaveis
self.s_name = "General"
self.f_St = f_St
self.f_K = f_K
self.f_r = f_r
self.f_sigma = f_sigma
self.use_UV = False
if f_sigmam:
self.f_sigmaM = f_sigma
self.f_sigmam = f_sigmam
self.f_sigma = (f_sigma + f_sigmam)/2.
self.use_UV = True
self.f_time = f_time
# inica grid
self.grid = Grid(f_vol=f_sigma,
f_value=f_St,
f_time=f_time,
i_nas=i_nas,
i_nts=i_nts)
def get_information(self, f_S, f_time, s_info):
'''
:param f_S: float. asset price
:param f_time: float. time in years
:param s_info: string. information desired. delta, gamma, price,
delta_anlt, gamma_anlt, price_anlt
'''
# define dataframe desejado
if s_info == 'price':
df = self.df_opt_prices
elif s_info == 'price_anlt':
df = self.df_opt_prices_anlt
elif s_info == 'delta':
df = self.df_delta
elif s_info == 'delta_anlt':
df = self.df_delta_anlt
elif s_info == 'gamma':
df = self.df_gamma
elif s_info == 'gamma_anlt':
df = self.df_gamma_anlt
# interpola informacao
return bilinear_interpolation(f_S, f_time, df)
def compare_to_analytical_solutions(self, l_S, f_time):
'''
Plot charts comparing the price, delta and gamma measure by the finitte
difference and by the analytical solution
l_S: list. asset price list
f_time. float. the time step to measure the outputs
'''
d_price = {u'analítico': [], u'diferenças finitas': []}
d_delta = {u'analítico': [], u'diferenças finitas': []}
d_gamma = {u'analítico': [], u'diferenças finitas': []}
l_prices = l_S
for f_S in l_prices:
# calcula precos
f_aux = self.get_information(f_S, f_time, 'price_anlt')
d_price[u'analítico'].append(f_aux)
f_aux = self.get_information(f_S, f_time, 'price')
d_price[u'diferenças finitas'].append(f_aux)
# calcula delta
f_aux = self.get_information(f_S, f_time, 'delta_anlt')
d_delta[u'analítico'].append(f_aux)
f_aux = self.get_information(f_S, f_time, 'delta')
d_delta[u'diferenças finitas'].append(f_aux)
# calcula gamma
f_aux = self.get_information(f_S, f_time, 'gamma_anlt')
d_gamma[u'analítico'].append(f_aux)
f_aux = self.get_information(f_S, f_time, 'gamma')
d_gamma[u'diferenças finitas'].append(f_aux)
# plota resultados
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, sharex=True)
fig.set_size_inches(12, 4)
l_title = [u'Preços\n', u'$\Delta$\n', u'$\Gamma$\n']
for d_aux, ax, s_title in zip([d_price, d_delta, d_gamma],
[ax1, ax2, ax3], l_title):
s_col = u'diferenças finitas'
df_plot = pd.DataFrame(d_aux[s_col], index=l_prices)
df_plot.columns = [s_col]
df_plot.plot(ax=ax)
s_col = u'analítico'
df_plot = pd.DataFrame(d_aux[s_col], index=l_prices)
df_plot.columns = [s_col]
df_plot.plot(style='--', ax=ax)
# df_plot = pd.DataFrame(d_aux, index=l_prices)
# df_plot.plot(ax=ax)
ax.set_xlabel(u'Preço do Subjacente')
ax.set_title(s_title)
ax1.set_ylabel(u'Valor')
s_prep = u"Comparação de Resultados para {}\n"
fig.suptitle(s_prep.format(self.s_name), fontsize=16, y=1.03)
fig.tight_layout()
def _set_final_condition(self):
'''
Set up the final condition in the grid, the payoff
'''
# apenas o valor final do ativo eh necessario aqui
for i in xrange(int(self.grid.f_nas) + 1):
f_S = i * 1. * self.grid.dS
self.grid(i, 0).f_asset_value = f_S
self.grid(i, 0).f_option_value = self._get_payoff(f_S)
self.grid(i, 0).f_option_value_anlt = self._get_payoff(f_S)
# preencho ultimo valor de todas as colunas (tempo)
for j in xrange(int(self.grid.i_nts) + 1):
f_S = i * 1. * self.grid.dS
self.grid(i, j).f_asset_value = f_S
def _set_all_matrix(self):
'''
Create attributes to hold the get_matrix information
'''
d_rtn = self._get_matrix()
self.df_asset_prices = d_rtn['asset']
self.df_opt_prices = d_rtn['opt_prices']
self.df_delta = d_rtn['delta']
self.df_gamma = d_rtn['gamma']
self.df_theta = d_rtn['theta']
self.df_opt_prices_anlt = d_rtn['opt_prices_anlt']
self.df_delta_anlt = d_rtn['delta_anlt']
self.df_gamma_anlt = d_rtn['gamma_anlt']
def _go_backwards(self):
'''
work backwards in time to calculate the option value
'''
# inicia variaveis que serao utilizadas
dS = self.grid.dS
dt = self.grid.dt
f_r = self.f_r
f_vol = self.f_sigma
i_nas = int(self.grid.f_nas)
# seta condicao final
self._set_final_condition()
# comeco o loop depois do primeiro passo de cada dimensao
for k in xrange(1, int(self.grid.i_nts) + 1):
for i in xrange(1, int(self.grid.f_nas)):
# calcula valores auxiliares
f_S = i * 1. * dS
self(i, k).f_asset_value = f_S
f_V_ip1_km1 = self(i+1, k-1).f_option_value
f_V_im1_km1 = self(i-1, k-1).f_option_value
f_V_i_km1 = self(i, k-1).f_option_value
# calcula gregas por diferenca central
f_delta = (f_V_ip1_km1 - f_V_im1_km1) / (2. * dS)
f_gamma = (f_V_ip1_km1 - 2 * f_V_i_km1 + f_V_im1_km1) / (dS**2)
# treat UV if is set
f_vol = self.f_sigma
if self.use_UV:
if f_gamma < 0:
f_vol = self.f_sigmaM
elif f_gamma > 0:
f_vol = self.f_sigmam
# calcula theta Vki−Vk+1iδt
f_theta = f_r * f_V_i_km1 - f_r * f_S * f_delta
f_theta -= 0.5 * f_gamma * f_vol**2 * f_S**2
# guarda as gregas e novo preco
self(i, k).f_delta = f_delta
self(i, k).f_gamma = f_gamma
self(i, k).f_theta = f_theta
f_option_value = f_V_i_km1 - dt * f_theta
# aplica exercicio antecipado, se definido
f_option_value = self._early_exercise(f_option_value, f_S)
# guarda valor
self(i, k).f_option_value = f_option_value
# guarda valores analiticos
f_price_anlt = self._get_analytical_price(f_S, k*dt)
f_delta_anlt = self._get_analytical_delta(f_S, k*dt)
f_gamma_anlt = self._get_analytical_gamma(f_S, k*dt)
self(i, k).f_option_value_anlt = f_price_anlt
self(i, k).f_delta_anlt = f_delta_anlt
self(i, k).f_gamma_anlt = f_gamma_anlt
# aplica condicoes de contorno
f_aux1, f_aux2 = self._apply_boundary_conditions(k)
self(0, k).f_option_value = f_aux1
self(i_nas, k).f_option_value = f_aux2
# guarda valores analiticos
for i_step in [0, i_nas]:
f_S = i_step * 1. * dS
f_price_anlt = self._get_analytical_price(f_S, k*dt)
f_delta_anlt = self._get_analytical_delta(f_S, k*dt)
f_gamma_anlt = self._get_analytical_gamma(f_S, k*dt)
self(i_step, k).f_option_value_anlt = f_price_anlt
self(i_step, k).f_delta_anlt = f_delta_anlt
self(i_step, k).f_gamma_anlt = f_gamma_anlt
def _apply_boundary_conditions(self, k):
'''
Apply boundary conditions
:param k: integer. The time k step
'''
# para S = 0
dt = self.grid.dt
i_nas = int(self.grid.f_nas)
f_rtn1 = self(0, k - 1).f_option_value * (1 - self.f_r * dt)
# para S=inf
f_rtn2 = 2 * self(i_nas - 1, k).f_option_value
f_rtn2 -= self(i_nas - 2, k).f_option_value
return f_rtn1, f_rtn2
def _get_matrix(self):
'''
Return a matrix of prices, deltas, gammas and thetas calculated
'''
# cria listas de preco e tempo
# l_time = ['{:.3f}'.format(self.grid.dt * i)
# for i in xrange(int(self.grid.i_nts) + 1)]
l_time = [self.grid.dt * i for i in xrange(int(self.grid.i_nts) + 1)]
l_price = [self.grid.dS * i for i in xrange(int(self.grid.f_nas) + 1)]
# inicia dataframes
d_rtn = {}
l_col = ['asset', 'opt_prices', 'delta', 'gamma', 'theta',
'opt_prices_anlt', 'delta_anlt', 'gamma_anlt']
for s_key in l_col:
d_rtn[s_key] = pd.DataFrame(np.zeros([int(self.grid.f_nas),
int(self.grid.i_nts)]))
# extrain informacoes
for k in xrange(int(self.grid.i_nts) + 1):
for i in xrange(int(self.grid.f_nas) + 1):
valid_node = self(i, k)
d_rtn['asset'].ix[i, k] = valid_node.f_asset_value
d_rtn['opt_prices'].ix[i, k] = valid_node.f_option_value
d_rtn['delta'].ix[i, k] = valid_node.f_delta
d_rtn['gamma'].ix[i, k] = valid_node.f_gamma
d_rtn['theta'].ix[i, k] = valid_node.f_theta
f_aux = valid_node.f_option_value_anlt
d_rtn['opt_prices_anlt'].ix[i, k] = f_aux
d_rtn['delta_anlt'].ix[i, k] = valid_node.f_delta_anlt
d_rtn['gamma_anlt'].ix[i, k] = valid_node.f_gamma_anlt
# arruma index
for s_key in l_col:
d_rtn[s_key].index = l_price
d_rtn[s_key].columns = l_time
return d_rtn
def _early_exercise(self, f_value, f_S):
'''
Modify the derivative value if it is subject to early exercise
'''
return f_value
def _get_analytical_price(self, f_S, f_time):
'''
Return the price of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
raise NotImplementedError
def _get_analytical_delta(self, f_S, f_time):
'''
Return the delta of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
raise NotImplementedError
def _get_analytical_gamma(self, f_S, f_time):
'''
Return the gamma of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
raise NotImplementedError
def _get_payoff(self, f_asset_price):
'''
Get the payoff of the contract
:param f_asset_price: float. The base asset price
'''
raise NotImplementedError()
def __call__(self, i, k):
'''
Allow direct access to the nodes in the grid
:param k: integer. the time index
:param i: integer. the asset index
'''
node = self.grid(i, k)
return node
class EuropianCall(Derivative):
'''
A representation of a europian Call Option
'''
def __init__(self, f_St, f_sigma, f_time, f_r, i_nas, f_K, i_nts=None,
f_sigmam=None):
'''
Initialize a EuropianCall object. Save all parameters as attributes
:param f_St: float. The price of the underline asset
:param f_sigma: float. A non negative underline volatility
:param f_time: float. The time remain until the expiration
:param f_r: float. The free intereset rate
:param i_nas: integer. Number of asset steps
:param f_K: float. The strike
:*param i_nas: integer. Number of asset steps
'''
# inicia variaveis de Derivativo
super(EuropianCall, self).__init__(f_St=f_St,
f_sigma=f_sigma,
f_time=f_time,
f_r=f_r,
i_nas=i_nas,
f_K=f_K,
i_nts=i_nts,
f_sigmam=f_sigmam)
self.s_name = 'Call Europeia'
self._go_backwards()
self._set_all_matrix()
def _get_analytical_price(self, f_S, f_time):
'''
Return the price of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
f_d1, f_d2 = get_d1_and_d2(f_S, self.f_sigma, f_time, self.f_r,
self.f_K)
exp_r_t = np.exp(-self.f_r * self.f_time)
S_cdf_d1 = f_S * stats.norm.cdf(f_d1, 0., 1.)
K_cdf_d2 = self.f_K * stats.norm.cdf(f_d2, 0., 1.)
return S_cdf_d1 - K_cdf_d2 * exp_r_t
def _get_analytical_delta(self, f_S, f_time):
'''
Return the delta of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
f_d1, f_d2 = get_d1_and_d2(f_S, self.f_sigma, f_time, self.f_r,
self.f_K)
cdf_d1 = stats.norm.cdf(f_d1, 0., 1.)
return cdf_d1
def _get_analytical_gamma(self, f_S, f_time):
'''
Return the gamma of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
f_d1, f_d2 = get_d1_and_d2(f_S, self.f_sigma, f_time, self.f_r,
self.f_K)
pdf_d1 = stats.norm.pdf(f_d1, 0., 1.)
S_gima_sqrt_t = f_S * self.f_sigma * (f_time**0.5)
return pdf_d1/S_gima_sqrt_t
def _get_payoff(self, f_asset_price):
'''
Get the payoff of the contract
:param f_asset_price: float. The base asset price
'''
return max(0, f_asset_price - self.f_K)
class LogContract(Derivative):
'''
A representation of a Log Contract
'''
def __init__(self, f_St, f_sigma, f_time, f_r, i_nas, f_K=None, i_nts=None,
f_sigmam=None):
'''
Initialize a LogContract object. Save all parameters as attributes
:param f_St: float. The price of the underline asset
:param f_sigma: float. A non negative underline volatility
:param f_time: float. The time remain until the expiration
:param f_r: float. The free intereset rate
:param i_nas: integer. Number of asset steps
:param f_K: float. The strike
:*param i_nas: integer. Number of asset steps
'''
# inicia variaveis de Derivativo
super(LogContract, self).__init__(f_St=f_St,
f_sigma=f_sigma,
f_time=f_time,
f_r=f_r,
i_nas=i_nas,
f_K=f_K,
i_nts=i_nts,
f_sigmam=f_sigmam)
self.s_name = 'Contrato Log'
self._go_backwards()
self._set_all_matrix()
def _get_analytical_price(self, f_S, f_time):
'''
Return the price of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
exp_r_t = np.exp(-1 * self.f_r * f_time)
ln_S = np.log(f_S)
r_var_t = (self.f_r - (self.f_sigma**2.)/2.) * f_time
ln_S_r_var_t = ln_S + r_var_t
return exp_r_t * ln_S_r_var_t
def _get_analytical_delta(self, f_S, f_time):
'''
Return the delta of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
exp_r_t = np.exp(-1. * self.f_r * f_time)
return exp_r_t / f_S
def _get_analytical_gamma(self, f_S, f_time):
'''
Return the gamma of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
exp_r_t = np.exp(-1. * self.f_r * f_time)
return -1 * exp_r_t / f_S**2.
def _get_payoff(self, f_asset_price):
'''
Get the payoff of the contract
:param f_asset_price: float. The base asset price
'''
if f_asset_price == 0:
return 0.
return np.log(f_asset_price)
class SquaredLogContract(Derivative):
'''
A representation of a Squared Log Contract
'''
def __init__(self, f_St, f_sigma, f_time, f_r, i_nas, f_K=None, i_nts=None,
f_sigmam=None):
'''
Initialize a SquaredLogContract object. Save all parameters as
attributes
:param f_St: float. The price of the underline asset
:param f_sigma: float. A non negative underline volatility
:param f_time: float. The time remain until the expiration
:param f_r: float. The free intereset rate
:param i_nas: integer. Number of asset steps
:param f_K: float. The strike
:*param i_nas: integer. Number of asset steps
'''
# inicia variaveis de Derivativo
super(SquaredLogContract, self).__init__(f_St=f_St,
f_sigma=f_sigma,
f_time=f_time,
f_r=f_r,
i_nas=i_nas,
f_K=f_K,
i_nts=i_nts,
f_sigmam=f_sigmam)
self.s_name = 'Contrato Log Quadratico'
self._go_backwards()
self._set_all_matrix()
def _get_analytical_price(self, f_S, f_time):
'''
Return the price of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
exp_r_t = np.exp(-1*self.f_r*f_time)
ln_S_r_var_t_sq = (np.log(f_S) + (self.f_r -
(self.f_sigma**2.)/2.) * f_time)**2.
var_t = self.f_sigma**2. * f_time
return exp_r_t * (ln_S_r_var_t_sq + var_t)
def _get_analytical_delta(self, f_S, f_time):
'''
Return the delta of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
two_exp_r_t_over_S = 2 * np.exp(-1*self.f_r*f_time) / f_S
ln_S_r_var_t = (np.log(f_S) + (self.f_r -
(self.f_sigma**2)/2) * f_time)
return two_exp_r_t_over_S * ln_S_r_var_t
def _get_analytical_gamma(self, f_S, f_time):
'''
Return the gamma of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
ln_S = np.log(f_S)
r_t = self.f_r * f_time
if f_S == 0:
ln_S = 0
exp_r_t = np.exp(-1. * r_t)
sigma_sqr_t = self.f_sigma**2 * f_time
f_rtn = exp_r_t / f_S**2. * (2 + sigma_sqr_t - 2 * ln_S - 2 * r_t)
return f_rtn
def _get_payoff(self, f_asset_price):
'''
Get the payoff of the contract
:param f_asset_price: float. The base asset price
'''
if f_asset_price == 0:
return 0.
return np.log(f_asset_price) ** 2
class SquaredExotic(Derivative):
'''
A representation of a exotic suqared contract. The Strike is given
'''
def __init__(self, f_St, f_sigma, f_time, f_r, i_nas, f_K=None, i_nts=None,
f_sigmam=None):
'''
Initialize a SquaredExotic object. Save all parameters as attributes
:param f_St: float. The price of the underline asset
:param f_sigma: float. A non negative underline volatility
:param f_time: float. The time remain until the expiration
:param f_r: float. The free intereset rate
:param i_nas: integer. Number of asset steps
:param f_K: float. The strike
:*param i_nas: integer. Number of asset steps
'''
# inicia variaveis de Derivativo
super(SquaredExotic, self).__init__(f_St=f_St,
f_sigma=f_sigma,
f_time=f_time,
f_r=f_r,
i_nas=i_nas,
f_K=f_K,
i_nts=i_nts,
f_sigmam=f_sigmam)
self.s_name = 'Exotico Quadratico'
self._go_backwards()
self._set_all_matrix()
def _get_analytical_price(self, f_S, f_time):
'''
Return the price of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
exp_r_var_t = np.exp((self.f_r + self.f_sigma**2)*f_time)
S_sq_exp_r_var_t = f_S**2 * exp_r_var_t
K_sq_exp_r_t = self.f_K**2 * np.exp(-self.f_r * f_time)
two_S_K = 2 * f_S * self.f_K
return S_sq_exp_r_var_t - two_S_K + K_sq_exp_r_t
def _get_analytical_delta(self, f_S, f_time):
'''
Return the delta of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
exp_r_var_t = np.exp((self.f_r + self.f_sigma**2)*f_time)
two_K = 2. * self.f_K
return 2. * f_S * exp_r_var_t - two_K
def _get_analytical_gamma(self, f_S, f_time):
'''
Return the gamma of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
return 2 * np.exp((self.f_r + self.f_sigma**2)*f_time)
def _get_payoff(self, f_asset_price):
'''
Get the payoff of the contract
:param f_asset_price: float. The base asset price
'''
if f_asset_price == 0:
return 0.
return (f_asset_price - self.f_K) ** 2
def _apply_boundary_conditions(self, k):
'''
Apply boundary conditions
:param k: integer. The time k step
'''
# para S = 0
dt = self.grid.dt
dS = self.grid.dS
i_nas = int(self.grid.f_nas)
f_rtn1 = self(0, k - 1).f_option_value * (1 - self.f_r * dt)
# para S=inf
f_rtn2 = 2 * self(i_nas - 1, k).f_option_value
f_rtn2 -= self(i_nas - 2, k).f_option_value
# adaptando condicao ao contrato
f_rtn2 += (2 * dS**2)
return f_rtn1, f_rtn2
class DigitalOption(Derivative):
'''
A representation of a Digital Option.
'''
def __init__(self, f_St, f_sigma, f_time, f_r, i_nas, f_K, i_nts=None,
f_sigmam=None):
'''
Initialize a DigitalOption object. Save all parameters as attributes
:param f_St: float. The price of the underline asset
:param f_sigma: float. A non negative underline volatility
:param f_time: float. The time remain until the expiration
:param f_r: float. The free intereset rate
:param i_nas: integer. Number of asset steps
:param f_K: float. The strike
:*param i_nas: integer. Number of asset steps
'''
# inicia variaveis de Derivativo
super(DigitalOption, self).__init__(f_St=f_St,
f_sigma=f_sigma,
f_time=f_time,
f_r=f_r,
i_nas=i_nas,
f_K=f_K,
i_nts=i_nts,
f_sigmam=f_sigmam)
self.s_name = 'Opcao Digital'
self._go_backwards()
self._set_all_matrix()
def _get_analytical_price(self, f_S, f_time):
'''
Return the price of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
f_d1, f_d2 = get_d1_and_d2(f_S, self.f_sigma, f_time, self.f_r,
self.f_K)
exp_r_t = np.exp(-self.f_r * f_time)
cdf_d2 = stats.norm.cdf(f_d2, 0., 1.)
return exp_r_t * cdf_d2
def _get_analytical_delta(self, f_S, f_time):
'''
Return the delta of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
f_d1, f_d2 = get_d1_and_d2(f_S, self.f_sigma, f_time, self.f_r,
self.f_K)
exp_r_t = np.exp(-self.f_r * self.f_time)
pdf_d2 = stats.norm.pdf(f_d2, 0., 1.)
sig_S_sqtr_t = self.f_sigma * self.f_St * (self.f_time**0.5)
return exp_r_t * pdf_d2 / sig_S_sqtr_t
def _get_analytical_gamma(self, f_S, f_time):
'''
Return the gamma of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
f_d1, f_d2 = get_d1_and_d2(f_S, self.f_sigma, f_time, self.f_r,
self.f_K)
exp_r_t = np.exp(-self.f_r * f_time)
sig_S_sqtr_t = self.f_sigma * f_S * (f_time**0.5)
cdf_d2 = stats.norm.cdf(f_d2, 0., 1.)
S_sqr_var_t = f_S**2 * self.f_sigma**2 * f_time
return (-1. * exp_r_t * f_d1 * cdf_d2) / S_sqr_var_t
def _get_payoff(self, f_asset_price):
'''
Get the payoff of the contract
:param f_asset_price: float. The base asset price
'''
return 1. * (f_asset_price > self.f_K)
class EuropianCallButterfly(Derivative):
'''
A representation of a europian Call Butterfly strategy with legs expiring
at the same maturity
'''
def __init__(self, f_St, f_sigma, f_time, f_r, i_nas, l_K, l_Q,
f_sigmam=None):
'''
Initialize a EuropianCall object. Save all parameters as attributes
:param f_St: float. The price of the underline asset
:param f_sigma: float. A non negative underline volatility
:param f_time: float. The time remain until the expiration
:param f_r: float. The free intereset rate
:param i_nas: integer. Number of asset steps
:param l_K: list. The strikes of the strategy
:param l_Q: float. The normalized qtys of the strategy
:*param f_sigmam: float. The minimum volatility observed. If it is set
fs_sigma is the maximum volatility observed
'''
# inicia variaveis de Derivativo
self.l_K = l_K
self.l_Q = l_Q
super(EuropianCallButterfly, self).__init__(f_St=f_St,
f_sigma=f_sigma,
f_time=f_time,
f_r=f_r,
i_nas=i_nas,
f_K=max(l_K),
i_nts=None,
f_sigmam=f_sigmam)
self.s_name = 'Fly de Call Europeia'
self._go_backwards()
self._set_all_matrix()
def _get_analytical_price(self, f_S, f_time):
'''
Return the price of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
f_price = 0.
for f_q, f_K in zip(self.l_Q, self.l_K):
f_d1, f_d2 = get_d1_and_d2(f_S, self.f_sigma, f_time, self.f_r,
f_K)
exp_r_t = np.exp(-self.f_r * self.f_time)
S_cdf_d1 = f_S * stats.norm.cdf(f_d1, 0., 1.)
K_cdf_d2 = f_K * stats.norm.cdf(f_d2, 0., 1.)
f_aux = S_cdf_d1 - K_cdf_d2 * exp_r_t
f_price += (f_aux * f_q)
return f_price
def _get_analytical_delta(self, f_S, f_time):
'''
Return the delta of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
f_delta = 0.
for f_q, f_K in zip(self.l_Q, self.l_K):
f_d1, f_d2 = get_d1_and_d2(f_S, self.f_sigma, f_time, self.f_r,
f_K)
cdf_d1 = stats.norm.cdf(f_d1, 0., 1.)
f_aux = cdf_d1
f_delta += (f_aux * f_q)
return f_delta
def _get_analytical_gamma(self, f_S, f_time):
'''
Return the gamma of the instrument using its analytical solution
:param f_S: float. the asset price
:param f_time: float.time to expiration
'''
f_gamma = 0.
for f_q, f_K in zip(self.l_Q, self.l_K):
f_d1, f_d2 = get_d1_and_d2(f_S, self.f_sigma, f_time, self.f_r,
f_K)
pdf_d1 = stats.norm.pdf(f_d1, 0., 1.)
S_gima_sqrt_t = f_S * self.f_sigma * (f_time**0.5)
f_aux = pdf_d1/S_gima_sqrt_t
f_gamma += (f_aux * f_q)
return f_gamma
def _get_payoff(self, f_asset_price):
'''
Get the payoff of the contract
:param f_asset_price: float. The base asset price
'''
f_payoff = 0.
for f_q, f_K in zip(self.l_Q, self.l_K):
f_payoff += max(0, f_asset_price - f_K)*f_q
return f_payoff
class Derivative_UVM(object):
'''
A general representation of a Derivative contract precifed using UVM
'''
def __init__(self, f_volm, f_volM, f_intrate, s_ptype, l_strike, l_qty,
f_expiration, f_NAS, b_worstcase=True):
'''
Initialize a Derivative_UVM object. Save all parameters as attributes
:param f_volm: float. The minimum underline volatility to be observed
:param f_volM: float. The maximum underline volatility to be observed
:param f_intrate: float. The risk free intereset rate
:param s_ptype: string. Pur ot Call. 'P' or 'C'
:param l_strike: list. Strikes in a array (1,3)
:param l_qty: list. Normalized Qty in a array (1,3)
:param f_expiration: float. The time remain until the expiration
:param f_NAS: float. Number of asset steps
:*param b_worstcase: boolean. if should use the worst or best case
'''