在'void'之前预期的不合格ID

时间:2013-04-25 00:18:39

标签: c++ void

我是C ++编程的新手,但也需要使用C ++代码来校准Black-Derman-Toy模型中的兴趣树。在“使用C ++建模衍生物”一书中找到了所需的源代码。但它没有用。我知道这个错误在论坛的某个地方,但经过一些修改后,另一个错误浮出水面。这真的很成问题。任何线索都会有所帮助。

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

第12行第6行[错误]预期在'void'之前的非限定标识

完整代码

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

3 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:2)

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

答案 2 :(得分: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,或者如果要访问{{1}中的所有成员命名空间而不必“使用”每个命名空间,您可以使用using namespace std;而不是using std::vector;(但是,这是not a good idea一直使用的。)

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

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