-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpricing_models.py
1180 lines (869 loc) · 28.6 KB
/
pricing_models.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
import pandas as pd
import numpy as np
import risk_kit as rk
from abc import ABC
from collections import Iterable
from scipy.optimize import broyden1
class BinomialTree(ABC):
"""
Implements an abstract class for the mulit-period binomial pricing models.
Uses the Lattice objects for the nodes of the tree structure.
Parameters:
----------
n: int
The number of periods for which the tree is to be made.
Also equivalent to the depth of the tree.
q: float
The probability of the security going upwards.
"""
@property
def n(self):
"""
Gets the the number of periods in the model.
"""
return self._n
@n.setter
def n(self, val):
self._n = val
@property
def q(self):
"""
Gets the probability of a security going up.
"""
return self._q
@q.setter
def q(self, val):
self._q = val
@property
def tree(self):
"""
Gets the binomial tree with node as Lattice objects
The tree for a n period model is returned in the form of a matrix as -
[[S0],
[[dS0, uS0],
[[d2S0, duS0, u2S0],
.
.
[[dnS0, d(n-1)u1S0, ..., unS0]]
"""
return self._tree
@tree.setter
def tree(self, tree):
self._tree = tree
def printtree(self):
"""
Prints the prices of the binomial pricing tree.
"""
for i in range(self.n + 1):
print("Period = " + str(i))
print(self.tree[i][: i + 1].round(3))
def __init__(self, n, q=0.5):
"""
Initializes the data descriptors from the given parameters.
"""
self.n = n
self.q = q
self.tree = np.zeros([self.n + 1, self.n + 1])
class StockPricing(BinomialTree):
"""
Implements the binomial stock pricing model.
Inherits the BinomialTree class.
Parameters:
----------
n: int
Number of periods
S0: float
The initial price of the security
u: float
The upward drift of the security
d: float
The downward drift of the security
c: float
The dividend paid by the security
"""
__doc__ += BinomialTree.__doc__
@property
def S0(self):
return self._S0
@S0.setter
def S0(self, val):
self._S0 = val
@property
def u(self):
return self._u
@u.setter
def u(self, val):
self._u = val
@property
def d(self):
return self._d
@d.setter
def d(self, val):
self._d = val
@property
def c(self):
return self._c
@c.setter
def c(self, val):
self._c = val
def _constructTree(self):
"""
Constructs the pricing of the binomial model for n periods.
"""
for i in range(self.n + 1):
for j in range(i + 1):
price = self.S0 * (self.u ** j) * (self.d ** (i - j))
self.tree[i, j] = price
def __init__(self, n, S0, u, d, c=0.0):
"""
Initializes the binomail model for the corresponding parameters.
"""
super().__init__(n)
self.S0 = S0
self.u = u
self.d = d
self.c = c
self._constructTree()
class FuturesPricing(BinomialTree):
"""
Implements a futures pricing model based on the binomial model.
Inherits the BinomialTree class.
Parameters:
----------
n: int
The period of the futures contract
model: BinomialTree
The underlying security pricing from which the futures contract is derived.
q: float
The probability of an upward move.
unpaid_coupon: float
The amount which the underlying security earns at the end of the contract but is not
paid to the long position holder in the contract.
The contract is executed immeditately after the dividend/coupon is paid.
"""
__doc__ += BinomialTree.__doc__
@property
def price(self):
return self.tree[0, 0]
def _constructTree(self, model, coupon):
"""
Recomputes the prices from the given model's spot prices for futures pricing.
"""
for i in range(self.n, -1, -1):
if i == self.n:
self.tree[i] = model.tree[i, : (i + 1)] - coupon
else:
for j in range(i + 1):
childd = self.tree[i + 1, j]
childu = self.tree[i + 1, j + 1]
self.tree[i, j] = self.q * childu + (1 - self.q) * childd
def __init__(self, n, model, q, unpaid_coupon=0.0):
super().__init__(n, q)
self._constructTree(model, unpaid_coupon)
class OptionsPricing(BinomialTree):
"""
Implements a binomial tree based option pricing model.
Inherits the BinomialTree class.
Parameters
----------
n: int
Number of periods
model: BinomialTree
The underlying security model from which the options contract is derived.
r: float / BinomialTree
The rate of interest to be used. Should be a scalar if fixed and a binomial model otherwise.
q: float
The probability of price going up in the binomial model
K: float
The strike price of the option contract.
is_call: bool
Sets to True if the option is call and False if the option is put. Defaults to True,
is_american: bool
Sets to True if the option is American and False if the option is European. Defaults to False.
"""
__doc__ += BinomialTree.__doc__
@property
def K(self):
"""
Represents the strike price of the options contract.
"""
return self._K
@K.setter
def K(self, val):
self._K = val
@property
def multiplier(self):
"""
The multiplier to be used for call and put option pricing.
Sets to 1 for call options and -1 for put options.
"""
return self._multiplier
@multiplier.setter
def multiplier(self, val):
self._multiplier = val
@property
def is_american(self):
"""
Represents if the option security is american or european.
"""
return self._is_american
@is_american.setter
def is_american(self, val):
self._is_american = val
@property
def price(self):
"""
Returns the current price of the option.
"""
return self.tree[0, 0]
@property
def early_exercise(self):
"""
Gets the details of early exercise of options.
Returns a list of dictionaries sorted by time consisting of all the possible times
when early exercise of options can be more beneficial.
"""
result = []
for time, no, early_ex, hold in sorted(self._early_exercise):
data = {
"Time": time,
"Current Premium": early_ex,
"Hold": hold,
}
result.append(data)
return result
def _constructTree(self, model, r):
"""
Computes the option prices from the given pricing model and rate of interest.
"""
if isinstance(r, int) or isinstance(r, float):
rate = np.empty([self.n + 1, self.n + 1])
rate.fill(r)
else:
rate = r.tree
for i in range(self.n, -1, -1):
if i == self.n:
for j in range(i + 1):
self.tree[i, j] = max(
0, self.multiplier * (model.tree[i, j] - self.K)
)
else:
for j in range(i + 1):
childu = self.tree[i + 1, j + 1]
childd = self.tree[i + 1, j]
# Expected call option permium if portfolio is held
hold = (self.q * childu + (1 - self.q) * childd) / (1 + rate[i, j])
# Call option premium if portfolio is exercised
# Can be done only in the case of american options
early_ex = max(0, self.multiplier * (model.tree[i, j] - self.K))
if early_ex > hold:
self._early_exercise.append((i, j, early_ex, hold))
self.tree[i, j] = max(hold, early_ex) if self.is_american else hold
def __init__(self, n, model, r, q, K, is_call=True, is_american=False):
"""
Initializes the black scholes model and other parameters from the given parameters.
"""
super().__init__(n, q)
self.K = K
self.multiplier = 1 if is_call else -1
self.is_american = is_american
self._early_exercise = []
self._constructTree(model, r)
class BondPricing(BinomialTree):
"""
Implements the binomial bond pricing model.
Inherits the BinomialTree class.
Parameters:
----------
n: int
The number of periods.
F: float
The face value of the bond.
q: float
The probability of the price going upward in the binomial model.
u: float
The factor by which the bond price goes up.
d: float
The factor by which the bond price goes down.
c: float
The coupon rate of the bond. Defaults to zero assuming zero coupon bond.
hazard: dict
If set to None, the bond is assumed to be non-defaultable. Otherwise should contain
a dictionary of following params:
a: float
The speed of hazard escalation
b: float
The exponential parameter of hazard
recovery_rate: float
The amount of interest paid back if a default occurs
default_probability[i, j] = a * b(i - j/2)
"""
__doc__ += BinomialTree.__doc__
@property
def F(self):
return self._F
@F.setter
def F(self, val):
self._F = val
@property
def c(self):
return self._c
@c.setter
def c(self, val):
self._c = val
@property
def price(self):
return self.tree[0, 0]
def _compute_defaults(self, hazard):
"""
Computes the probability of default at each node.
h[i, j] = a * b^(i - j/2)
Returns a tuple of hazard rates, recovery rate
"""
h = np.zeros([self.n + 1, self.n + 1])
r = 1
if hazard is not None:
a = hazard["a"]
b = hazard["b"]
r = hazard["recovery_rate"]
for i in range(self.n + 1):
for j in range(i + 1):
h[i, j] = a * b ** (j - i / 2)
return (h, r)
def _constructTree(self, r, h, recovery_rate):
"""
Constructs the tree for bond pricing for n periods.
"""
if isinstance(r, int) or isinstance(r, float):
rate = np.empty([self.n + 1, self.n + 1])
rate.fill(r)
else:
rate = r.tree
coupon = self.F * self.c
self.tree[self.n] = np.repeat(self.F + coupon, self.n + 1)
for i in range(self.n - 1, -1, -1):
for j in range(i + 1):
childd = self.tree[i + 1, j]
childu = self.tree[i + 1, j + 1]
non_hazard_price = (
coupon + (self.q * childu + (1 - self.q) * childd)
) * (1 - h[i, j])
hazard_price = h[i, j] * recovery_rate * self.F
self.tree[i, j] = (non_hazard_price + hazard_price) / (1 + rate[i, j])
def __init__(self, n, F, q, r, c=0.0, hazard=None):
"""
Initializes the bond pricing model from the given parameters.
"""
super().__init__(n, q)
self.F = F
self.c = c
self.r = r
h, recovery = self._compute_defaults(hazard)
self._constructTree(r, h, recovery)
class ForwardsPricing(BinomialTree):
"""
Implements a forwards pricing model based on the binomial model.
Inherits the BinomialTree class.
Parameters:
----------
n: int
The period of the futures contract
model: BinomialTree
The underlying security pricing from which the futures contract is derived.
q: float
The probability of an upward move.
r: float / BinomialTree
The rate of interest to be used. Should be a scalar if fixed and a binomial model otherwise.
unpaid_coupon: float
The amount which the underlying security earns at the end of the contract but is not
paid to the long position holder in the contract.
The contract is executed immeditately after the dividend/coupon is paid.
"""
__doc__ += BinomialTree.__doc__
@property
def r(self):
"""
The rate of interest.
"""
return self._r
@r.setter
def r(self, val):
self._r = val
@property
def price(self):
"""
Gets the price of the forward contract on the underlying security.
"""
zcb_n = BondPricing(self.n, 1, self.q, self.r).price
return self.tree[0, 0] / zcb_n
def _constructTree(self, model, r, coupon):
"""
Recomputes the prices from the given model's spot prices for futures pricing.
"""
if isinstance(r, int) or isinstance(r, float):
rate = np.empty([self.n + 1, self.n + 1])
rate.fill(r)
else:
rate = r.tree
for i in range(self.n, -1, -1):
if i == self.n:
self.tree[i] = model.tree[i, : (i + 1)] - coupon
else:
for j in range(i + 1):
childd = self.tree[i + 1, j]
childu = self.tree[i + 1, j + 1]
self.tree[i, j] = (self.q * childu + (1 - self.q) * childd) / (
1 + rate[i, j]
)
def __init__(self, n, model, q, r, unpaid_coupon=0.0):
super().__init__(n, q)
self.r = r
self._constructTree(model, r, unpaid_coupon)
class SwapsPricing(BinomialTree):
"""
Implements a swap pricing model based on the binomial model.
Inherits the BinomialTree class.
The model assumes the last exchange is executed at n + 1 period.
Parameters:
----------
n: int
The number of periods. Here n denotes the period at which the last payment occured.
q: float
The probability of the price of security going upward.
fixed_rate: float
The fixed rate of interest to be paid/recieved in the swap contract
start_time: int
The period from which the exchange starts
is_long: bool
The type of position to be modeled, long or short.
Long position refers to paying the fixed interest rate
while short refers to paying the floating rates.
r: BinomialTree
The rate model for varying interest rates
"""
__doc__ += BinomialTree.__doc__
@property
def fixed_rate(self):
return self._fixed_rate
@fixed_rate.setter
def fixed_rate(self, val):
self._fixed_rate = val
@property
def start_time(self):
return self._start_time
@start_time.setter
def start_time(self, val):
self._start_time = val
@property
def multiplier(self):
return self._multiplier
@multiplier.setter
def multiplier(self, val):
self._multiplier = val
@property
def r(self):
return self._r
@r.setter
def r(self, val):
self._r = val
@property
def price(self):
return self.tree[0, 0]
def _constructTree(self, r):
"""
Constructs the binomial tree for pricing the swaps.
"""
rate = r.tree
for i in range(self.n, -1, -1):
if i == self.n:
self.tree[i] = (
(rate[i, : (i + 1)] - self.fixed_rate)
* self.multiplier
/ (rate[i, : (i + 1)] + 1)
)
else:
for j in range(i + 1):
childd = self.tree[i + 1, j]
childu = self.tree[i + 1, j + 1]
value = (self.q * childu + (1 - self.q) * childd) / (1 + rate[i, j])
if i >= self.start_time - 1:
payment = ((rate[i, j] - self.fixed_rate) * self.multiplier) / (
1 + rate[i, j]
)
value += payment
self.tree[i, j] = value
def __init__(self, n, q, fixed_rate, start_time, is_long, r):
"""
Initializes the model based on the given parameters.
"""
super().__init__(n - 1, q)
self.fixed_rate = fixed_rate
self.start_time = start_time
self.multiplier = 1 if is_long else -1
self.r = r
self._constructTree(r)
class BDTRate(BinomialTree):
"""
Implements a black-derman-toy short rate model over the binomial tree model.
Inherits the BinomialTree class.
Assumes the number of periods is equal to the length of the dirft vector - 1.
rate[i, j] = a[i] * exp(b[i] * j), where
rate[i, j] - Rate of interest at period i and state j
a[i] - Drift at period i
b[i] - volatility at period i
Parameters:
----------
n: int
The number of periods.
drift: scalar / np.array
The list of a[i] in the black-derman-toy model
vol: scalar / np.array
The list of b[i] in the black-derman-toy model
"""
__doc__ += BinomialTree.__doc__
@property
def a(self):
return self._a
@a.setter
def a(self, val):
if isinstance(val, int) or isinstance(val, float):
val = np.repeat(val, self.n)
self._a = val
@property
def b(self):
return self._b
@b.setter
def b(self, val):
if isinstance(val, int) or isinstance(val, float):
val = np.repeat(val, self.n + 1)
self._b = val
def _constructTree(self):
"""
Constructs the binomial tree model for interest rates based on
the BDT equation.
"""
for i in range(self.n + 1):
for j in range(i + 1):
self.tree[i, j] = self.a[i] * np.exp(self.b[j] * j)
def __init__(self, n, drift, vol):
"""
Initializes the model based on the given parameters.
"""
super().__init__(n - 1)
self.a = drift
self.b = vol
self._constructTree()
@classmethod
def calibrate(cls, n, q, vol, market_spot_rates, iterations=200):
"""
Calibrates the optimal drift for the given market spot rates
Initializes the model from the corresponding optimal drift and vol
Parameters:
----------
n: int
The number of periods
q: float
The probability of rates going upward in the binomial model
vol: scalar / np.array
The volatility for the model
market_spot_rates: np.array
The current spot rates for n periods to be used for optimization
max_iter: int
The number of iterations for which the optimization function should run
Returns:
-------
(BDTRate, error): Returns a tuple of BDTRate instance calibrated from the given parameters and
the squared error in the result.
"""
def error(drift):
rates = BDTRate(n, drift, vol)
spot_rates = CashPricing(n, q, rates).get_spot_rates()
error = spot_rates - market_spot_rates
return error
initial_guess = np.repeat(0.05, n)
drift = broyden1(error, initial_guess, iter=iterations)
exp_error = (error(drift) ** 2).sum()
return cls(n, drift, vol), exp_error
class CashPricing(BinomialTree):
"""
Implements the binomial model for pricing 1 unit of cash.
Inherits the BinomialTree class.
Each node of the binomial tree denotes the value of 1 unit of cash at that node.
For example, tree[i, j] denotes the value of 1($) at period i and state j.
Parameters:
----------
n: int
The number of periods.
q: float
The probability of price going up in the binomial model
r: BinomialTree
The rates of interest
"""
def _constructTree(self, r):
"""
Constructs the binomial tree for pricing a unit of cash.
The i, j node of the tree denotes the price of a unit of cash at period i and state j.
"""
rate = r.tree
self.tree[0, 0] = 1
for i in range(1, self.n + 1):
# The bottom most nodes
self.tree[i, 0] = (1 - self.q) * self.tree[i - 1, 0] / (1 + rate[i - 1, 0])
# The top most nodes
self.tree[i, i] = (
self.q * self.tree[i - 1, i - 1] / (1 + rate[i - 1, i - 1])
)
for j in range(1, i):
par_d = self.tree[i - 1, j - 1] / (1 + rate[i - 1, j - 1])
par_u = self.tree[i - 1, j] / (1 + rate[i - 1, j])
self.tree[i, j] = self.q * par_d + (1 - self.q) * par_u
def get_zcb_prices(self):
"""
Returns the prices of zero coupon bonds for the corresponding interest rates.
"""
return self.tree.sum(axis=1)
def get_spot_rates(self):
"""
Returns the spot rates for the corresponding interest rates.
"""
zcb_prices = self.get_zcb_prices()[1:]
spot_rates = zcb_prices ** -(1 / (np.arange(self.n) + 1)) - 1
return spot_rates
def __init__(self, n, q, r):
super().__init__(n, q)
self._constructTree(r)
class LevelPaymentMortgage(object):
"""
Implements the class of a single fixed-rate level payment mortgage structure.
Assumes no pre-payment.
Parameters:
----------
P: float
The total principal amount of the mortgage.
r: float
The annual rate of interest of the mortgage.
T: int
The total number of years for which the payment is to be made.
"""
@property
def P(self):
return self._P
@P.setter
def P(self, val):
self._P = val
@property
def r(self):
return self._r
@r.setter
def r(self, val):
self._r = val
@property
def T(self):
return self._T
@T.setter
def T(self, val):
self._T = val
@property
def periods(self):
return self._periods
@periods.setter
def periods(self, val):
self._periods = val
@property
def monthly_payment(self):
"""
The monthly payment which needs to be given
"""
c = self.r
n = self.T * self.periods
M0 = self.P
payment = (M0 * c * (1 + c) ** n) / ((1 + c) ** n - 1)
return payment
@property
def annualized_rate(self):
"""
The effective annualized rate of interest after compounding periodically
"""
return (1 + self.r) ** self.periods - 1
def get_value(self, rate):
"""
The effective value of the mortgage
Parameters:
----------
rate: scalar/np.array
The risk free rate of interest to be used for discounting cash flows.
If the rates vary over time, it should be a numpy array of expected rates
of interest of size periods.
"""
B = self.monthly_payment
n = self.T * self.periods
if isinstance(rate, int) or isinstance(rate, float):
rate = np.repeat(rate, n)
t = np.arange(1, n + 1)
value = (1 + rate) ** (-t) * B
return value
def __init__(self, P, r, T, periods_per_year=12):
"""
Initializes the class instance with the given parameters.
"""
self.P = P
self.periods = periods_per_year
self.r = r / periods_per_year
self.T = T
class PassThroughMBS(object):
"""
Implements a basic pass through mortgage backed securitization which
consists of only single type of mortgages and a constant prepayment factor
in terms of the PSA.
Parameters:
----------
P: float
The principal payment of the pool of mortgages.
T: int
The number of years
loan_r: float
The rate of interest of lending
pass_r: float
The rate of interest given to investors
PSA: float
The rate of prepayment in terms of PSA multiplier
age: int
The age of the pool. Defaults to 0
periods_per_year: int