0

I am newbie in C++ programming but also have a need to use C++ code to calibrate interest tree in Black-Derman-Toy model. In book "Modeling Derivatives with C++" have found needed source code. However it didn't work. I know that this error was somewhere here on forum but after some changes another error came to the surface. It's really problematic. Any clue would be helpful.

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

Line 12 Col 6[Error] expected unqualified-id before 'void'

Below full code

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);
}
}
} 
Ascorpio
  • 196
  • 1
  • 3
  • 14

3 Answers3

6

void is not a member of namespace std. Instead, try:

 void buildBDT(vector <double> yield_curve,
        vector <double> volatility_curve, int N, double T)
taocp
  • 23,276
  • 10
  • 49
  • 62
  • Tried a sec ago and the effect wast this `[Error] variable or field 'buildBDT' declared void` and more `12 23 [Error] expected primary-expression before 'double'; 12 43 [Error] 'vector' was not declared in this scope; 12 51 [Error] expected primary-expression before 'double'` – Ascorpio Apr 25 '13 at 00:24
  • @Ascorpio how did you use the buildBDT function? Did you assign it to something else in your code? It returns void so you should not expect that it will return anything. – taocp Apr 25 '13 at 00:26
  • @Ascorpio change std::void to void at both functino prototype declaration and definition – taocp Apr 25 '13 at 00:27
  • This function shouldn't return anything. It just estimate rates in `double r[20][20] = {0.0};// short rate at node i, j` but just as I as sad I am a newbie... – Ascorpio Apr 25 '13 at 00:37
  • @Ascorpio Just in case, make sure you have a semicolon at the end of the function prototype declaration. Does your code compile now? – taocp Apr 25 '13 at 00:40
  • I am trying but this code it has a lot of mminor mistakes. For now I think I can nail it on my own :) Thank you so much for help. – Ascorpio Apr 25 '13 at 00:59
  • @Ascorpio you are welcome. PS: be patience when you are trying to fix errors. – taocp Apr 25 '13 at 01:00
  • Hey I have little problem once again. Can you tell me in short what does it mean -1.#IND00E? It is something connected with minus infinity ?? – Ascorpio Apr 27 '13 at 07:06
  • @Ascorpio it means that you may have divided 0 somewhere in your code – taocp Apr 27 '13 at 13:08
2

void is native type in C++, so it can't be accessed through any particular namespace. std::void should just be void.

David G
  • 94,763
  • 41
  • 167
  • 253
0

void is a built-in type in C++. It is not in any namespace. Change the declaration and definition of buildBDT to:

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

Make sure you add a semi-colon at the end of the declaration (first line). That ends the declaration and should fix the variable or field declared void error. In addition, move the #include statements above the first line.

Also, to not get errors for using the vector type, add using std::vector; above the first line, but below the include statements, or if you want to access all the members in the std namespace without having to 'use' each one, you can use using namespace std; instead of using std::vector; (however, this is not a good idea to use all the time).

Your top of your new code should look like the following:

#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)
Community
  • 1
  • 1
JKor
  • 3,822
  • 3
  • 28
  • 36