#This file is designed to contain the code for running Monte Carlo simulations in generating zero-coupon bond price and the yield curve import math import random from array import array face = 1 #change this later def r_generator (a,b,r0,sigma,num_its): #Creates an array of r_t (discrete) i = 0 r = [r0] for i in range(num_its): #goes from i = 0 to (numb_its - 1) brownian = random.normalvariate(0, 1) #Brownian motion variable r_new = r[i] + a*(b-r[i]) + sigma*brownian #calculates each r_t r.append(r_new) return r def monte_carlo_ZCB (a,b,sigma,r0,dt,t0,tf,numb_sims,face): #Returns a matrix that is [numb_sims]by[numb_iterations] number_iterations = int((tf-t0)/dt) #number of iterations per simulation i = 0 #delete? ZCB_price = [] tau = tf-t0 for i in range(numb_sims): #iterates through the simulations ZCB_one_round = [] #initializes disposable array r_one_round = r_generator(a,b,r0,sigma,number_iterations) #calculates the r_t # B = (1-math.exp(-a*tau))/a #calculates B (equation 18b) A = float(face*math.exp((B-tau)*(b-(sigma**2)/(2*(a**2)))-((sigma**2)*(B**2))/(4*a))) #calculates equation 18a for j in range(number_iterations): #iterates through time ZCB_one_round.append(A*math.exp(-B*r_one_round[j])) ZCB_price.append(ZCB_one_round) #MAKE SURE THIS APPENDS THE VALUES TO A NEW LIST IN THIS ARRAY return ZCB_price def yield_curve (maturities,a,b,sigma,r0,dt,t0,tf,numb_sims): #calculates the yield curve #maturities is an array of the times-to-maturity yield_sims = [] #Will hold numb_sims of yield yield_time_sim = [] #Will hold [time_iterations]by[numb_sims] yield_all = [] #Will be final [len(maturities)]by[time_iterations]by[numb_sims] for j in range(len(maturities)): #iterates through each maturity term ZCB_prices = monte_carlo_ZCB(a,b,sigma,r0,dt,t0,tf,numb_sims,face) time_iterations = int((tf-t0)/dt) for i in range(time_iterations): for k in range(numb_sims): yield_sims.append(-1*math.log(ZCB_prices[k][i])/maturities[j]) #This calculates and appends the yield at simulation k with maturity j, at time i (I hope) yield_time_sim.append(yield_sims) yield_all.append(yield_time_sim) return yield_all maturities = [1,2,3] print(yield_curve(maturities,.1,0,.1,0,.05,0,1,1)) #def hull_white (a,b,r0,sigma,dt,t0,tf,face,in_yield,N) #calculates ZCB price #in_yield is an array (N by M) of the starting values on the yield curve, with N points with M maturities #print(r_generator(.1,0,0,1,.05,0,2))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question