Use of "typedef" to Define a Matrix

Jun 19, 2012 at 1:58pm
I am trying to grasp the use of the object-oriented features of C++ in financial models and decided to practice by copying and running the source codes in a forthcoming book. One of the source codes includes a header file where "typedef" is used as follows:

#include <vector>
using namespace std;

typedef vector<double> Vector;
typedef vector<Vector> Matrix;

When I attempt to compile the source code, I get an error message which I interpret as saying that the second use of typedef is incorrect. The message refers to the allocator for the vector template.

My question is "Is the second use of typedef invalid?". If so, what ought to be used?

Thank you.
Jun 19, 2012 at 2:04pm
Yeah, what's a Vector? I only heard of a vector before...

Oh right yeah C++... hehe *double-take*
Last edited on Jun 19, 2012 at 2:07pm
Jun 19, 2012 at 2:06pm
Compiles happily for me. What are you using to compile it?

Yeah, what's a Vector?

One of these :)
typedef vector<double> Vector;
Last edited on Jun 19, 2012 at 2:07pm
Jun 19, 2012 at 2:08pm
Maybe we could have the actual error message?

Is it complaining about that line specifically or is another line coming up in the error message?
Jun 19, 2012 at 4:23pm
I'm copying the error message below; I've edited out some portions which I believe was unnecessary. The references to other header files are part of the project. The message seems to refer to a problem with the use of vector<double> in the typedef of Matrix. The C++ IDE that I'm using is wxDev-C++.

Thank you once again.

Here is the error message:

In file included from c:\program files (x86)\dev-cpp\mingw32\bin\../lib/gcc/mingw32/4.6.1/include/c++/vector:65:0, from Matrix.h
from BSModel02.h from Matrix.h:4, from BSModel02.h
from BSModel02.h from Matrix.h:4, from BSModel02.h
from PathDepOption05.h from BSModel02.h:4, from PathDepOption05.h
from main23.cpp from PathDepOption05.h:4, from main23.cpp
from main23.cpp

In member function 'void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_Integer, _Integer, std::__true_type) [with _Integer = int, _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >]':
instantiated from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = int, _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >, std::vector<_Tp, _Alloc>::allocator_type = s
instantiated from here no matching function for call to 'std::vector<std::vector<double> >::_M_fill_initialize(std::vector<std::vector<double> >::size_type, int&)'
candidate is:
void std::vector<_Tp, _Alloc>::_M_fill_initialize(std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >, std::vector<_Tp, _Alloc>::size_type = unsigned int, std::vector<_Tp,
no known conversion for argument 2 from 'int' to 'const value_type& {aka const std::vector<double>&}'

Jun 19, 2012 at 4:54pm
can you plz show us the complete code? I think I know what the error might be about but i'm not sure, and I don't even know if that's possible, so please show us the COMPLETE code and the COMPLETE error message
Jun 19, 2012 at 5:42pm
As suggested, I'm including to this reply the complete code that contains the header files, along with the corresponding cpp files.
Also the complete error message follows. The message is in the format of a csv file so that it is easier to read in a spreadsheet program. There are 3 headings: Line, File, Message.

Thank you.

// main23.cpp

#include <cstdlib>
#include <iostream>
#include "PathDepOption05.h"

using namespace std;

int main()
{
int d = 3;
Vector S0(d);
S0[0] = 40.0; S0[1] = 60.0; S0[2] = 100.0;
double r = 0.03;
Matrix C(d,d);
C[0][0] = 0.1; C[0][1] = -0.1; C[0][2] = 0.0;
C[1][0] = -0.1; C[1][1] = 0.2; C[1][2] = 0.0;
C[2][0] = 0.0; C[2][1] = 0.0; C[2][2] = 0.3;
BSModel Model(S0, r, C);

double T = 1.0/12.0, K = 200.0;
int m = 30;
ArthmAsianCall Option(T, K, m);

long N = 30000;
cout << "Arithmetic Basket Call Price = "
<< Option.PriceByMC( Model, N) << endl;

cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}

//PathDepOption05.h

#ifndef PathDepOption05_h
#define PathDepOption05_h

#include "BSModel02.h"

class PathDepOption
{
public:
double T;
int m;
double PriceByMC(BSModel Model, long N);
virtual double Payoff(SamplePath &S) = 0;
};

class ArthmAsianCall: public PathDepOption
{
public:
double K;
ArthmAsianCall(double T_, double K_, int m_)
{T = T_; K = K_; m = m_;}
double Payoff(SamplePath& S);
};

#endif

//PathDepOption05.cpp

#include "PathDepOption05.h"
#include <cmath>

double PathDepOption::PriceByMC(BSModel Model, long N)
{
double H = 0.0;
SamplePath S(m);
for( long i = 0; i < N; i++)
{
Model.GenerateSamplePath(T, m, S);
H = (i*H + Payoff(S))/(i + 1.0);
}
return exp(-Model.r*T)*H;
}

double ArthmAsianCall::Payoff(SamplePath &S)
{
double Ave = 0.0;
int d = S[0].size();
Vector one(d);
for( int i = 0; i < d; i++) one[i] = 1.0;
for( int k = 0; k < m; k++)
{
Ave = (k*Ave + (one^S[k]))/(k + 1.0);
}
if (Ave < K) return 0.0;
return Ave - K;
}

//BSModel02.h

#ifndef BSModel02_h
#define BSModel02_h

#include "Matrix.h"

typedef vector<Vector> SamplePath;

class BSModel
{
public:
Vector S0, sigma;
Matrix C;
double r;
BSModel(Vector S0_, double r_, Matrix C_);
void GenerateSamplePath(double T, int m, SamplePath& S);
};

#endif


//BSModel02.cpp

#include "BSModel02.h"
#include <cmath>

#include <ctime>
#include <cstdlib>

const double pi = 4.0*atan(1.0);

double Gauss( )
{
double U1 = (rand() + 1.0)/(RAND_MAX + 1.0);
double U2 = (rand() + 1.0)/(RAND_MAX + 1.0);
return sqrt(-2.0*log(U1))* cos(2.0*pi*U2);
}

Vector Gauss(int d)
{
Vector Z(d);
for (int j = 0; j < d; j++) Z[j] = Gauss( );
return Z;
}

BSModel::BSModel(Vector S0_, double r_, Matrix C_)
{
S0 = S0_; r = r_; C = C_; srand(time(NULL));
int d = S0.size();
sigma.resize(d);
for (int j = 0; j < d; j++) sigma[j] = sqrt(C[j]^C[j]);
}

void BSModel::GenerateSamplePath( double T, int m, SamplePath &S)
{
Vector St = S0;
int d = S0.size();
for( int k = 0; k < m; k++)
{
S[k] = St*exp((T/m)*(r + (-0.5)*sigma*sigma) + sqrt(T/m)*(C*Gauss(d)));
St = S[k];
}
}

//Matrix.h

#ifndef Matrix_h
#define Matrix_h

#include <vector>
using namespace std;

typedef vector<double> Vector;
typedef vector<Vector> Matrix;

Vector operator*(const Matrix &C, const Vector &V);
Vector operator*(const double &a, const Vector &V);
Vector operator+(const double &a, const Vector &V);
Vector operator+(const Vector &V, const Vector &W);
Vector operator*(const Vector &V, const Vector &W);
Vector exp(const Vector &V);
double operator^(const Vector &V, const Vector &W);

#endif

//Matrix.cpp

#include "Matrix.h"
#include <cmath>


Vector operator*(const Matrix &C, const Vector &V)
{
int d = C.size();
Vector W(d);
for(int j = 0; j < d ; j++)
{
W[j] = 0.0;
for(int l = 0; l < d; l++) W[j] = W[j] + C[j][l]*V[l];
}
return W;
}

double operator^(const Vector &V, const Vector &W)
{
double sum = 0.0;
int d = V.size();
for (int j = 0; j < d; j++) sum = sum + V[j]*W[j];
return sum;
}


Error Message in csv File

Line,File,Message
,,,,,,,,,,,,,,
4 ,"c:\program files (x86)\dev-cpp\mingw32\lib\gcc\mingw32\4.6.1\include\c++\vector:65:0, from Matrix.h ","In file included from c:\program files (x86)\dev-cpp\mingw32\bin\../lib/gcc/mingw32/4.6.1/include/c++/vector:65:0, from Matrix.h",,,,,,,,,,,,
4 ,"E:\Dev-CPP_Projects\NMiFwCPP\main23\Matrix.h:4, from BSModel02.h ","from Matrix.h:4, from BSModel02.h",,,,,,,,,,,,
4 ,"E:\Dev-CPP_Projects\NMiFwCPP\main23\BSModel02.h:4, from PathDepOption05.h ","from BSModel02.h:4, from PathDepOption05.h",,,,,,,,,,,,
3 ,"E:\Dev-CPP_Projects\NMiFwCPP\main23\PathDepOption05.h:4, from main23.cpp ","from PathDepOption05.h:4, from main23.cpp",,,,,,,,,,,,
3 ,E:\Dev-CPP_Projects\NMiFwCPP\main23\main23.cpp from main23.cpp,,,,,,,,,,,,,
,c:\program files (x86)\dev-cpp\mingw32\lib\gcc\mingw32\4.6.1\include\c++\bits\stl_vector.h ,"In member function 'void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_Integer, _Integer, std::__true_type) [with _Integer = int, _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >]':",,,,,,,,,,,,
340:4 ,c:\program files (x86)\dev-cpp\mingw32\lib\gcc\mingw32\4.6.1\include\c++\bits\stl_vector.h ,"instantiated from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = int, _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >, std::vector<_Tp, _Alloc>::allocator_type = s",,,,,,,,,,,,
13:17 ,E:\Dev-CPP_Projects\NMiFwCPP\main23\main23.cpp , instantiated from here,,,,,,,,,,,,
4 ,c:\program files (x86)\dev-cpp\mingw32\lib\gcc\mingw32\4.6.1\include\c++\bits\stl_vector.h:1080 ,"no matching function for call to 'std::vector<std::vector<double> >::_M_fill_initialize(std::vector<std::vector<double> >::size_type, int&)'",,,,,,,,,,,,
,,candidate is:,,,,,,,,,,,,
,,"void std::vector<_Tp, _Alloc>::_M_fill_initialize(std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >, std::vector<_Tp, _Alloc>::size_type = unsigned int, std::vector<_Tp,",,,,,,,,,,,,
,,no known conversion for argument 2 from 'int' to 'const value_type& {aka const std::vector<double>&}',,,,,,,,,,,,
,E:\Dev-CPP_Projects\NMiFwCPP\main23\Makefile.win [Build Error] ,[Objects/MingW/main23.o] Error 1,,,,,,,,,,,,
Jun 19, 2012 at 6:01pm
The problem is this line:
Matrix C(d,d);

Matrix is just a typedef of vector<vector<double>> so that line is equivalent to
vector<vector<double>> C(d,d);

vector<vector<double>> doesn't have a constructor that takes 2 integers as argument. I think this is what you tried to do:
vector<vector<double>> C(d,vector<double>(d));
==>
Matrix C(d,Vector(d));
Last edited on Jun 19, 2012 at 6:02pm
Jun 19, 2012 at 6:16pm
Yeeees, that's what I tought. As peter said, you used an int where a Vector(aka vector<double>) was expected! just do what peter said in the last line of his reply
Last edited on Jun 19, 2012 at 6:16pm
Jun 19, 2012 at 8:03pm
I thank you all for all your time and effort to resolve this programming error. I'm surprised that the authors of the textbook were not aware of this flaw in their code.
I again thank you, the experts.
Jun 19, 2012 at 10:33pm
This is valid code

Matrix C(d,d);


But I see the following invalid statement in the BSModel constructor definitiom

for (int j = 0; j < d; j++) sigma[j] = sqrt(C[j]^C[j]);


C[j] - is a vector. There is no such an operation as ^ for vectors.
Jun 20, 2012 at 5:59am
swong01 wrote:
1
2
3
4
5
6
7
double operator^(const Vector &V, const Vector &W)
{
double sum = 0.0;
int d = V.size();
for (int j = 0; j < d; j++) sum = sum + V[j]*W[j];
return sum;
}

Please, vlad from moscow, read the tutorial section about overloading operators!
Last edited on Jun 20, 2012 at 6:03am
Jun 20, 2012 at 11:15am
This code is so bad that I did not even try to look all it through. The code demonstartes how programs shall not be written.
Last edited on Jun 20, 2012 at 11:15am
Jun 20, 2012 at 4:04pm
In defense of the authors, they do say that they have taken some liberty with the source codes so that they would be easier to follow for the student. They admit that there ought to be more use of "private", but again for the sake of clarity and shorter source codes, they've omitted the proper use of "private" and of "protected".
Topic archived. No new replies allowed.