0

我是 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);
}
}
} 
4

3 回答 3

6

void不是命名空间的成员std。相反,请尝试:

 void buildBDT(vector <double> yield_curve,
        vector <double> volatility_curve, int N, double T)
于 2013-04-25T00:20:48.123 回答
2

void是 C++ 中的本机类型,因此不能通过任何特定的命名空间访问它。std::void应该只是void

于 2013-04-25T00:20:26.507 回答
0

void是 C++ 中的内置类型。它不在任何命名空间中。将声明和定义更改buildBDT为:

void buildBDT(vector <double> yield_curve,vector <double> volatility_curve, int N, double T)

确保在声明的末尾(第一行)添加分号。这结束了声明并应该修复variable or field declared void错误。此外,将#include语句移到第一行上方。

此外,为了不因使用该vector类型而出现错误,请using std::vector;在第一行上方但在 include 语句下方添加,或者如果您想访问std命名空间中的所有成员而不必“使用”每个成员,您可以使用using namespace std;而不是using std::vector;(但是,这不是一直使用的好主意)。

您的新代码顶部应如下所示:

#include <vector>
using std::vector; //or using namespace std; depending on your preference
void buildBDT(vector <double> yield_curve,vector <double> volatility_curve, int N, double T)
/********** (start of the documentation)
于 2013-04-25T00:35:20.590 回答