Consulte los códigos siguientes
Mi pregunta es sobre los parámetros de entrada (a, b y sigma) y su cálculo.
Para la media "b" a largo plazo, ¿utilizamos los tipos efectivos de los fondos federales o los T-bills a 3 millones?
Además, ¿cómo puedo calcular la velocidad de reversión media "a"?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
import timeit
start = timeit.default_timer()
def inst_to_ann(r):
return np.expm1(r)
def ann_to_inst(r):
return np.log1p(r)
def cir(n_years = 1, n_scenarios=1, a=0.05, b=0.03, sigma=0.05, steps_per_year=52, r_0=None):
if r_0 is None: r_0 = b
r_0 = ann_to_inst(r_0)
dt = 1/ steps_per_year
num_steps = int(n_years * steps_per_year) + 1 # because n_years might be a float
I = np.random.normal(0, scale=np.sqrt(dt), size=(num_steps, n_scenarios))
rates = np.empty_like(I)
rates[0] = r_0
## For Price Generation
h = math.sqrt(a ** 2 + 2 * sigma ** 2)
prices = np.empty_like(I)
def price(ttm, r):
_A = ((2 * h * math.exp((h + a) * ttm / 2)) / (2 * h + (h + a) * (math.exp(h * ttm) - 1))) ** (
2 * a * b / sigma ** 2)
_B = (2 * (math.exp(h * ttm) - 1)) / (2 * h + (h + a) * (math.exp(h * ttm) - 1))
_P = _A * np.exp(-_B * r)
return _P
prices[0] = price(n_years, r_0)
for step in range(1, num_steps):
r_t = rates[step - 1]
d_r_t = a * (b - r_t) * dt + sigma * np.sqrt(r_t) * I[step]
rates[step] = abs(r_t + d_r_t)
# generate prices at time t as well ...
prices[step] = price(n_years - step * dt, rates[step])
rates = pd.DataFrame(data=inst_to_ann(rates), index=range(num_steps))
### for prices
prices = pd.DataFrame(data=prices, index=range(num_steps))
return rates, prices
dfrates,dfprices = cir(n_scenarios=1000)
dfprices.plot()
plt.show()
print(dfprices)
stop = timeit.default_timer()
print('Time: ', stop - start)