-
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
-
Re-investment
One important aspect of investing with bonds is that as we get coupon payments, we want to track the total cash we have received. At the same time, we can also assume that we are putting in the cash to be compounded at the current interest rate of r. In this basic scenario, r is going to stay constant.
The computation of cash will be two steps, first, we compute the interest rate gain on the current cash we hold, then we will add in any payments to that cash variable. In the PSUBs variable definition you will see how we build out the two steps. The functions are defined below. First, reset the simulations, then we define the functions.
import pathos
pathos.helpers.shutdown()
configs.clear()
#New functions
#Compute cash
def cash_change(_params, substep, sH, s):
#Accumulate the cash
cash = s['cash'] * (1+s['r']) ** (1/12)
cash += s['payment']
return ({"cash": cash})
#Update cash
def update_cash(params, step, sL, s, _input):
return ('cash', _input['cash'])
We are going to run this simulation with two partial updates. First, one to compute payments, then one to compute the cash. You can see this in the
import pandas as pd
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
#Partial updates
PSUBs = [
{
"policies": {
"payment_policy": bond_payments,
},
"variables": {
"payment": update_payments,
}
},
{
"policies": {
"cash_policy": cash_change,
},
"variables": {
"cash": update_cash,
}
}
]
#Set simulation parameters
#The M parameter will let us test different combinations of parameters
sim_config_dict = {
'T': range(360),
'N': 5}
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)
Since we now have two substeps, we will take the mean cash value only where the results are equal to substep 2.
cash_ts = result[result['substep'] == 2].groupby('timestep')['cash'].mean()
print(cash_ts)
Finally, graph the resulting cash position.
cash_ts.plot(kind='line')
plt.title("Cash Position w/ Compounding")
plt.ylabel("Cash")
plt.show()