EDIT: or maybe to add, is the below way of calibration better than calculating as:
longer term mean b: average of interest rates
speed of reversion a: ln(1/drift)
volatility σ
I am trying to calibrate the CIR model on monthly interval 3 mon t bill rates, below is my code but I am not sure which approach is better the above one or the below coded approach? ( if the code is correct not sure)
import numpy as np
from scipy.optimize import minimize
# Define the CIR model
def cir_model(params, r, dt):
kappa, theta, sigma = params
dr = kappa * (theta - r) * dt + sigma * np.sqrt(r) * np.sqrt(dt)
return dr
# Define the objective function to minimize (sum of squared errors)
def objective_function(params, r):
dt = 1/12 # Monthly interval
error = 0
for n in range(len(r)):
error_ = r[n] - cir_model(params, r[n], dt)
error += error_**2
return error
# Initial parameter guess
initial_params = [0.1, 0.05, 0.1]
# List of interest rates
interest_rates = [0.04, 0.05, 0.04, 0.04, 0.02, 0.05, 0.07]
# Convert interest rates to a NumPy array
interest_rates = np.array(interest_rates)
# Perform parameter calibration
result = minimize(objective_function, initial_params, args=(interest_rates,), method='Nelder-Mead')
# Extract the calibrated parameters
calibrated_params = result.x
np.set_printoptions(suppress=True)
calibrated_params