-
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
-
Parameters
This will be a very quick lesson on how to use parameters. First, we are going to need to bring back some of the functions we defined in the last lesson.
import matplotlib.pyplot as plt
from scipy.stats import norm
import numpy as np
import pandas as pd
#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
For our policy, we are going to make a change. With the _params argument we can grab the parameters for the simulation. Notice the changes below.
#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}
Now we can set the other pieces.
#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,
}
}
]
Now define parameters.
#Define parameters to test
a = [.5,.25]*2
print(a)
print()
b = [.06]
print(b)
print()
sigma = [.02]*2 + [.04]*2
print(sigma)
print()
t = [1/12]
print(t)
print()
You will notice that a and sigma are defined as four values (each related to simulations to 1-4). The other two are just one value which means that they are kept as the same value for all simulations. Plug these in as the M in the simulation configurations.
#Set simulation parameters
from cadCAD.configuration.utils import config_sim
#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)
from cadCAD.configuration import Experiment
exp = Experiment()
exp.append_configs(
initial_state = initial_state,
partial_state_update_blocks = PSUBs,
sim_configs = c
)
Now run!
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
exec_mode = ExecutionMode()
local_mode_ctx = ExecutionContext(context=exec_mode.single_mode)
from cadCAD import configs
simulation = Executor(exec_context=local_mode_ctx, configs=configs)
raw_result, field, sessions = simulation.execute()
result = pd.DataFrame(raw_result)
print(result)
You can now see that there are 4 simulations each with 100 runs. Finally, we can plot the average rate for the four different simulations.
ts = result.groupby(['timestep', 'simulation'])['r'].mean().unstack()
ts.columns = ['a=.5 sigma=.02',
'a=.25 sigma=.02',
'a=.5 sigma=.04',
'a=.25 sigma=.04']
ts.plot(kind='line')
plt.show()