我是 C++ 编程的新手,但也需要使用 C++ 代码来校准 Black-Derman-Toy 模型中的兴趣树。在“Modeling Derivatives with C++”一书中找到了所需的源代码。然而它没有用。我知道这个错误出现在论坛的某个地方,但经过一些更改后,另一个错误浮出水面。这真的是有问题的。任何线索都会有所帮助。
std::void buildBDT(vector <double> yield_curve,vector <double> volatility_curve, int N, double T)
第 12 行第 6 列 [错误] 在 'void' 之前预期不合格 ID
下面是完整代码
std::void buildBDT(vector <double> yield_curve,vector <double> volatility_curve, int N, double T)
/**********************************************************************************
buildBDT : constructs a BDT tree calibrated to the yield curve and volatility curve
[in] vector<double> yield_curve: vector of yield curve
vector<double> volatility_curve : vector of volatility curves
int N: number of time steps
double T: time to maturity
[out]: void
*******************************************************************************/
#include<iostream>
#include <vector>
std::void buildBDT(vector <double> yield_curve,vector <double> volatility_curve, int N, double T)
{
double r[20][20] = {0.0};// short rate at node i, j
double U[20] = {0.0};// median of the (lognormal)
// distribution for r at time t
double dt = 0.0;// time step
double volR[20] = {0.0};// short rate volatiliy
double vol[20] = {0.0};// stores calibrated volatility
// parameter
double P[20] = {0.0};// discount bond prices
double Pu[20] = {0.0};// price of bond in up movement
double Pd[20] = {0.0};// price of bond in down movement
double Qu[20][20] = {0.0};// state securities (Arrow-Debreu)
// prices for an up movement
double Qd[20][20] = {0.0};// state securities (Arrow-Debreu)
// prices for a down movement
double R[20] = {0.0};// discount curve rates
const double epsilon = 0.0001;// error tolerance level
double error, error1, error2 = 0.0;// errors computed in numerical search
double error3, error4 = 0.0;// errors computed in numerical search
double sum1, sum2 = 0.0;// sums of ?rst derivatives
double sum3, sum4 = 0.0;// sums of second derivatives
double volSum1, volSum2 = 0.0;// sum of volatilities
double sigVal1 = 0.0;// calibrated volatility parameter
double sigVal2, sigVal3 = 0.0;// computed volatilities in numerical
// search
double alpha1 = 0.05;// calibrated U(i) parameter
double alpha2 = 0.0;// updated alpha1 (U(i)) parameter
double alpha3 = 0.10; // computed U(i) parameter in numerical
// search
int i,j;
// precompute constants assume one year time step
dt = 1;
// initialize yield and volatility curves
for (i = 1; i <= N; i++)
{
R[i] = yield_curve[i-1];
P[i] = 1/(pow((1 + R[i]*dt),i*dt));
volR[i] = volatility_curve[i-1];
}
// initialize nodes
U[0] = R[1];
r[0][0] = R[1];
d[0][0] = 1/(1 + r[0][0]*dt);
vol[0] = volR[1];
Qu[1][1] = 1;
Qd[1][-1] = 1;
// compute Pu[.] and Pd[.]
for (i = 2; i <= N; i++)
{
// solve the following for Pu[i]
sum1 = 0;
sum2 = 0;
error = 0;
alpha1 = 0.92;
do
{
sum1 = (alpha1 + pow(alpha1,exp(-2*volR[i]*sqrt(dt))))/(2*(1 + r[0][0]*dt));
sum2 = (1/(2*(1 + r[0][0]*dt)))*(1 + exp(-2*volR[i]*sqrt(dt))*
(pow(alpha1,exp(-2*volR[i]*sqrt(dt)) 1)));
alpha2 = alpha1 (sum1 P[i])/(sum2);
error = abs(alpha2 alpha);
alpha1 = alpha2;
}
while (error > epsilon);
Pu[i] = alpha1;
Pd[i] = pow(Pu[i],exp(-2*volR[i]*sqrt(dt)));
}
// evolve tree for the short rate
for (i = 1; i < N; i++)
{
// update pure security prices at time step i
if (i > 1)
{
for (j = -i+2; j <= i; j += 2)
{
Qu[i][j]= 0.5*Qu[i-1][j-1]*d[i-1][j-1] + 0.5*Qu[i-1][j+1]*d[i-1][j+1];
}
for (j = i-2; j >= -i; j -= 2)
{
Qd[i][j] =0.5*Qd[i-1][j-1]*d[i-1][j-1] + 0.5*Qd[i-1][j+1]*d[i-1][j+1];
}
}
// solve simultaneously for U[i] and sig[i]
// using 2 dimensional Newton-Raphson
// initial guess
alpha1 = 0.05;
sigVal1 = 0.092;
do
{
sum1 = 0;
sum2 = 0;
sum3 = 0;
sum4 = 0;
volSum1 = 0;
volSum2 = 0;
for (j = -i; j <= i; j += 2)
{
sum1 += Qu[i][j]*(1/(1 + alpha1*exp(sigVal1*j*sqrt(dt))*dt));
sum2 += Qu[i][j]*(pow((1+alpha1*exp(sigVal1*j*sqrt(dt))*dt),-
2)*exp(sigVal1*j*sqrt(dt))*dt));
volSum1 += Qu[i][j]*(pow((1+ alpha1*exp(sigVal1*j*sqrt(dt))*dt),-
2)*(alpha1*(j*sqrt(dt))*dt*exp(sigVal1*j*sqrt(dt))));
sum3 += Qd[i][j]*(1/(1 + alpha1*exp(sigVal1*j*sqrt(dt))*dt));
sum4 += Qd[i][j]*(pow((1+ alpha1*exp(sigVal1*j*sqrt(dt))*dt),-2)*(exp(sigVal1*j*sqrt (dt))*dt));
volSum2 += Qd[i][j]*(pow((1+ alpha1*exp(sigVal1*j*sqrt(dt))*dt),-2)*
(alpha1*(j*sqrt(dt))*dt*exp(sigVal1*j*sqrt(dt))));
}
alpha2 = alpha1 (sum1 Pu[i+1])/(-sum2);
error = abs(alpha2 alpha1);
alpha1 = alpha2;
sigVal2 = sigVal1 (sum1 Pu[i+1])/(-volSum1);
error1 = abs(sigVal2 sigVal1);
sigVal1 = sigVal2;
alpha3 = alpha1 (sum3 Pd[i+1])/(-sum4);
error3 = abs(alpha3 alpha1);
sigVal3 = sigVal1 (sum3 Pd[i+1])/(-volSum2);
error4 = abs(sigVal3 sigVal1);
sigVal1 = sigVal3;
}
while ((error > epsilon) || (error1 > epsilon) || (error3 > epsilon) ||
(error4 > epsilon));
U[i] = alpha;
vol[i] = sigVal1;
// set r[.] and d[.]
for (j = -i; j <= i; j += 2)
{r[i][j] = U[i]*exp(vol[i]*j*sqrt(dt));
d[i][j] = 1/(1 + r[i][j]*dt);
}
}
}