Mastering Python for Finance by 2015

Mastering Python for Finance by 2015

Author:2015
Language: eng
Format: epub
Publisher: Packt Publishing


With these methods defined, we can now run our code and price a callable zero-coupon bond by the Vasicek model.

The implementation of the VasicekCZCB class in Python is given as follows:

""" Price a callable zero coupon bond by the Vasicek model """ import math import numpy as np import scipy.stats as st class VasicekCZCB: def __init__(self): self.norminv = st.distributions.norm.ppf self.norm = st.distributions.norm.cdf def vasicek_czcb_values(self, r0, R, ratio, T, sigma, kappa, theta, M, prob=1e-6, max_policy_iter=10, grid_struct_const=0.25, rs=None): r_min, dr, N, dtau = \ self.vasicek_params(r0, M, sigma, kappa, theta, T, prob, grid_struct_const, rs) r = np.r_[0:N]*dr + r_min v_mplus1 = np.ones(N) for i in range(1, M+1): K = self.exercise_call_price(R, ratio, i*dtau) eex = np.ones(N)*K subdiagonal, diagonal, superdiagonal = \ self.vasicek_diagonals(sigma, kappa, theta, r_min, dr, N, dtau) v_mplus1, iterations = \ self.iterate(subdiagonal, diagonal, superdiagonal, v_mplus1, eex, max_policy_iter) return r, v_mplus1 def vasicek_params(self, r0, M, sigma, kappa, theta, T, prob, grid_struct_const=0.25, rs=None): (r_min, r_max) = (rs[0], rs[-1]) if not rs is None \ else self.vasicek_limits(r0, sigma, kappa, theta, T, prob) dt = T/float(M) N = self.calculate_N(grid_struct_const, dt, sigma, r_max, r_min) dr = (r_max-r_min)/(N-1) return r_min, dr, N, dt def calculate_N(self, max_structure_const, dt, sigma, r_max, r_min): N = 0 while True: N += 1 grid_structure_interval = dt*(sigma**2)/( ((r_max-r_min)/float(N))**2) if grid_structure_interval > max_structure_const: break return N def vasicek_limits(self, r0, sigma, kappa, theta, T, prob=1e-6): er = theta+(r0-theta)*math.exp(-kappa*T) variance = (sigma**2)*T if kappa==0 else \ (sigma**2)/(2*kappa)*(1-math.exp(-2*kappa*T)) stdev = math.sqrt(variance) r_min = self.norminv(prob, er, stdev) r_max = self.norminv(1-prob, er, stdev) return r_min, r_max def vasicek_diagonals(self, sigma, kappa, theta, r_min, dr, N, dtau): rn = np.r_[0:N]*dr + r_min subdiagonals = kappa*(theta-rn)*dtau/(2*dr) - \ 0.5*(sigma**2)*dtau/(dr**2) diagonals = 1 + rn*dtau + sigma**2*dtau/(dr**2) superdiagonals = -kappa*(theta-rn)*dtau/(2*dr) - \ 0.5*(sigma**2)*dtau/(dr**2) # Implement boundary conditions. if N > 0: v_subd0 = subdiagonals[0] superdiagonals[0] = superdiagonals[0] - \ subdiagonals[0] diagonals[0] += 2*v_subd0 subdiagonals[0] = 0 if N > 1: v_superd_last = superdiagonals[-1] superdiagonals[-1] = superdiagonals[-1] - \ subdiagonals[-1] diagonals[-1] += 2*v_superd_last superdiagonals[-1] = 0 return subdiagonals, diagonals, superdiagonals def check_exercise(self, V, eex): return V > eex def exercise_call_price(self, R, ratio, tau): K = ratio*np.exp(-R*tau) return K def vasicek_policy_diagonals(self, subdiagonal, diagonal, superdiagonal, v_old, v_new, eex): has_early_exercise = self.check_exercise(v_new, eex) subdiagonal[has_early_exercise] = 0 superdiagonal[has_early_exercise] = 0 policy = v_old/eex policy_values = policy[has_early_exercise] diagonal[has_early_exercise] = policy_values return subdiagonal, diagonal, superdiagonal def iterate(self, subdiagonal, diagonal, superdiagonal, v_old, eex, max_policy_iter=10): v_mplus1 = v_old v_m = v_old change = np.zeros(len(v_old)) prev_changes = np.zeros(len(v_old)) iterations = 0 while iterations <= max_policy_iter: iterations += 1 v_mplus1 = self.tridiagonal_solve(subdiagonal, diagonal, superdiagonal, v_old) subdiagonal, diagonal, superdiagonal = \ self.vasicek_policy_diagonals(subdiagonal, diagonal, superdiagonal, v_old, v_mplus1, eex) is_eex = self.check_exercise(v_mplus1, eex) change[is_eex] = 1 if iterations > 1: change[v_mplus1 != v_m] = 1 is_no_more_eex = False if True in is_eex else True if is_no_more_eex: break v_mplus1[is_eex] = eex[is_eex] changes = (change == prev_changes) is_no_further_changes = all((x == 1) for x in changes) if is_no_further_changes: break prev_changes = change v_m = v_mplus1 return v_mplus1, (iterations-1) def tridiagonal_solve(self, a, b, c, d): nf = len(a) # Number of equations ac, bc, cc, dc = map(np.array, (a, b, c, d))



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Personalized inhaled bacteriophage therapy for treatment of multidrug-resistant Pseudomonas aeruginosa in cystic fibrosis by unknow(151159)
Eco-friendly approach of bio-indigo synthesis and developing purification methods towards isolation of indigo from indirubin and bacterial fragments by Ramalingam Manivannan & Kaliyan Prabakaran & Young-A Son(149858)
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(74269)
CONSORT 2025 statement: updated guideline for reporting randomized trials by unknow(66072)
Critical evaluation of the ProfiLER-02 study design and outcomes by Vivek Subbiah & Razelle Kurzrock(65822)
Cardiac gene therapy makes a comeback by Oliver J. Müller & Susanne Hille & Anca Kliesow Remes(65257)
Unveiling the design rules for tunable emission in graphene quantum dots: A high-throughput TDDFT and machine learning perspective by Şener Özönder & Mustafa Coşkun Özdemir & Caner Ünlü(50857)
A yeast-based oral therapeutic delivers immune checkpoint inhibitors to reduce intestinal tumor burden by unknow(33518)
Covalent hitchhikers guide proteins to the nucleus by Alexander F. Russell & Madeline F. Currie & Champak Chatterjee(33397)
Meet the Authors: Christopher R. Mansfield and Emily R. Derbyshire by Christopher R. Mansfield & Emily R. Derbyshire(33141)
What's Done in Darkness by Kayla Perrin(27102)
Topological analysis of non-conjugated ethylene oxide cored dendrimers decorated with tetraphenylethylene: Insights from degree-based descriptors using the polynomial approach by A Theertha Nair & D Antony Xavier & Annmaria Baby & S Akhila(26482)
Investigation of mechanical and self-healing properties of hydroxyl-terminated polybutadiene functionalized with 2-ureido-4-pyrimidinone by Mohsen Kazazi & Mehran Hayaty & Ali Mousaviazar(26435)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(21011)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(20773)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(20647)
The Fifty Shades Trilogy & Grey by E L James(19604)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19486)
Shot Through the Heart by Mercy Celeste(19345)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17490)