使用scipy优化的Python Vasicek模型校准

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (213)

我正在尝试使用python设置Vasicek校准例程。我认为最好使用scipy.optimize,但我正在努力如何编码。我有以下整体表格。

谁在python中实现了Vasicek校准?下面的初始数据表。

tau = <0.25,0.50,1.0,1.50,2.0>和zeroBond = <0.975,0.949,0.900,0.8519,0.8056>

更新:如此给定,这个公式:B =(1 - np.exp(-kappa tau))/ kappa A = np.exp((theta-(sigma 2)/(2(kappa 2)))*(B- tau) - (sigma 2)/(4 * kappa)(B 2))Vasicek = A np.exp(-r0 * B)

有什么python函数,迭代求解'kappa',使得变量Vasicek达到某个值?

def py_exact_zcb_Vas_Table(theta, kappa, sigma, tau, zeroBond, r0 = 0):
  length = len(tau)
  B = np.zeros(length)
  A = np.zeros(length)

  Vasicek = np.zeros(length)
  kappa_calib = np.zeros(length)
  theta_calib = np.zeros(length)
  Vasci_calib = np.zeros(length)

  for i in range(0, length, 1):
    B[i] = (1 - np.exp(-kappa*tau[i])) / kappa
    A[i] = np.exp((theta-(sigma[i]**2)/(2*(kappa**2))) * (B[i]-tau[i]) - (sigma[i]**2)/(4*kappa)*(B[i]**2))
    Vasicek[i] = A[i]*np.exp(-r0 * B[i])

    #do while (zeroBond[i] - Vasci_calib[i]) <> 0:
        # change kappa[i] such that I match Vasci_calib[i] with zeroBond[i]



  return pd.DataFrame({'B':B, 'A':A, 'Vasicek':Vasicek, 'kappa':kapp_calib})
提问于
用户回答回答于

您可以使用该scipy.optimize.minimize_scalar函数,以便在给定参数的情况下找到kappa求解方程Vasicek(kappa) = target_value的函数(tau, sigma, theta, r0)

import numpy as np
from scipy.optimize import minimize_scalar

def compute_Vasicek(kappa, tau, sigma, theta, r0):
    B = (1 - np.exp(-kappa*tau)) / kappa
    A = np.exp((theta-(sigma**2)/(2*(kappa**2))) * (B-tau) - (sigma**2)/(4*kappa)*(B**2))
    vasicek = A*np.exp(-r0 * B)
    return vasicek

def objectif_function(kappa, *args):
    return (compute_Vasicek(kappa, *args[1:]) - args[0])**2

# Minimization:
targeted_Vasicek = 10
tau, sigma, theta, r0 = 4, 205, 5, 0
result = minimize_scalar(objectif_function, args=(targeted_Vasicek, tau, sigma, theta, r0), bounds=(0, 100), method='bounded')
print(result)

也许该Brent方法适用于您的情况,使用非随机值参数...

然后,你可以这样做:

kappa_calib = result.x
vasci_calib = compute_Vasicek(kappa_calib, tau, sigma, theta, r0)

并将最小化部分包装在另一个函数中,因此可以从循环内部调用它

扫码关注云+社区

领取腾讯云代金券