## 使用scipy优化的Python Vasicek模型校准内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

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

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

``````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})
``````

### 1 个回答

``````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)
``````

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