-
Basics Part 1 2
-
Lecture1.1
-
Lecture1.2
-
-
Basics Part 2 1
-
Lecture2.1
-
-
Credit Migration 2
-
Lecture3.1
-
Lecture3.2
-
-
Vasicek Model 2
-
Lecture4.1
-
Lecture4.2
-
-
Payments 2
-
Lecture5.1
-
Lecture5.2
-
-
Complete System 2
-
Lecture6.1
-
Lecture6.2
-
Building the Model
We saw the Vasicek model prior, but did not go into detail. We will now explain what it is and how we are going to be using it. The Vasicek model is one that allows us to simulate interest rate paths. The assumption is that there are two components, a mean reversion component that pulls the current rate to the long run average, and a random shock component that will move the rate either up or down. The mathematical formula for the Vasicek model is as so:
$$dr_{t}\text{ = Change in rate at time t}$$
$$dW_{t}\text{ = Increment from Standard Brownian Motion}$$
$$dt\text{ = The time increment (1/12 would mean a month or 1/12 year)}$$
$$r_{t}\text{ = The rate at time t}$$
$$a\text{ = Mean Reversion Speed}$$
$$b\text{ = Long Run Average of r}$$
$$\sigma \text{ = Random shock volatility}$$
For those not familiar with brownian motion, the dW term essentially means that we will pick a number from the standard normal distribution multiplied by the square root of the time increment.
In this lesson we want to see how different parameters can change path behavior. Our first step is to pick the different options for each parameter we want.
#Set different parameter choices
sigma = [.005, .01, .02]
b = [.04, .06]
t = [1/12]
a = [1, .5, .25]
By using itertools.product we can get all combinations possible, like so:
import itertools
import numpy as np
#By using itertools.product we can get all combinations possible
params = np.array(list(itertools.product(*(sigma, b, t, a))))
print(params)
Now, let’s assign each column to a variable.
#Each column is given to one of the variables
sigma = list(params[:,0])
b = list(params[:,1])
t = list(params[:,2])
a = list(params[:,3])
Now, run everything as we have been before with the new parameter options.
import matplotlib.pyplot as plt
from scipy.stats import norm
import numpy as np
import pandas as pd
from cadCAD.configuration.utils import config_sim
from cadCAD.configuration import Experiment
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
from cadCAD import configs
#Set an initial interest rate
initial_state = {'r': .08}
#Basic vasicek rate change model
def rate_change(r, a=.5, b=.06, sigma=.02, t=1/12):
delta = a* (b-r) * t + sigma * t ** .5 * norm.ppf(np.random.RandomState().rand())
return delta + r
#Policy for updating
def vasicek_policy(_params, substep, sH, s):
r = rate_change(s['r'], _params['a'], _params['b'], _params['sigma'], _params['t'])
return {'r': r}
#Simple state update
def update_rate(_params, substep, sH, s, _input):
return ('r', _input['r'])
#Partial updates
PSUBs = [
{
"policies": {
"rate_policy": vasicek_policy,
},
"variables": {
"r": update_rate,
}
}
]
#Set simulation parameters
#The M parameter will let us test different combinations of parameters
sim_config_dict = {
'T': range(360),
'N': 100,
"M": {'a': a, 'b':b, 'sigma':sigma, 't':t}}
c = config_sim(sim_config_dict)
exp = Experiment()
exp.append_configs(
initial_state = initial_state,
partial_state_update_blocks = PSUBs,
sim_configs = c
)
exec_mode = ExecutionMode()
local_mode_ctx = ExecutionContext(context=exec_mode.single_mode)
simulation = Executor(exec_context=local_mode_ctx, configs=configs)
raw_result, field, sessions = simulation.execute()
result = pd.DataFrame(raw_result)