Let us now consider how to implement mean variance analysis in C++. As shown using Matlab, the
calculations are relatively simple matrix expressions. To implement the calculations in C++the best way
of doing it is to use a linear algebra class to deal with the calculations. For illustrative purposes we will show usage of two dierent matrix classes, Newmat and IT++. These classes are described in more detail in an appendix, with some references to how to obtain and install them.
In C++Code 6.1 and C++Code 6.2 we show how to do the basic mean variance calculations, calculating
means and standard deviations, using the two classes. Note the similarity to using Matlab in the way the matrix and vector multiplications are carried out. Note also an important dierence between the two classes. In IT++ the indexing of elements start at zero, the usual C++way. Hence, when adressing
element tmp(0,0) in the IT++ example we are pulling the rst element. In Newmat the default indexing starts at one, the Matlab way. Therefore, in adressing tmp(1,1) in the Newmat example we are also pulling the rst element. This serves as a warning to read the documentation carefully, the o by one error is a very common occurrence when mixing libraries like this.
#include <cmath> using namespace std; #include <itpp/itbase.h> using namespace itpp;
double mv calculate mean(const vec& e, const vec& w){ vec tmp = e.transpose()*w;
return tmp(0); };
double mv calculate variance(const mat& V, const vec& w){ mat tmp = w.transpose()*V*w;
return tmp(0,0); };
double mv calculate st dev(const mat& V, const vec& w){ double var = mv calculate variance(V,w);
return sqrt(var); };
C++Code 6.1: Mean variance calculations using IT++
Example
Mean variance calculations.
e= 0:05 0:1 V= 1:0 0:0 0:0 1:0
Calculate mean, variance and stdev for portfolio
w=
0:5 0:5
#include <cmath> using namespace std; #include "newmat.h" using namespace NEWMAT;
double mv calculate mean(const Matrix& e, const Matrix& w){ Matrix tmp = e.t()*w;
return tmp(1,1); };
double mv calculate variance(const Matrix& V, const Matrix& w){ Matrix tmp = w.t()*V*w;
return tmp(1,1); };
double mv calculate st dev(const Matrix& V, const Matrix& w){ double var = mv calculate variance(V,w);
return sqrt(var); };
C++Code 6.2: Mean variance calculations using Newmat
C++program:
cout << "Simple example of mean variance calculations " << endl; Matrix e(2,1); e(1,1)=0.05; e(2,1)=0.1; Matrix V(2,2); V(1,1)=1.0; V(2,1)=0.0; V(1,2)=0.0; V(2,2)=1.0; Matrix w(2,1); w(1,1)=0.5; w(2,1)=0.5;
cout << " mean " << mv calculate mean(e,w) << endl; cout << " variance " << mv calculate variance(V,w) << endl; cout << " stdev " << mv calculate st dev(V,w) << endl;
Output from C++program:
Simple example of mean variance calculations mean 0.075
variance 0.5 stdev 0.707107
In C++Code 6.4 and C++Code 6.3 we show how to calculate the mean variance optimal portfolio for a given
required return. This is the case where there are no constraints on the weight, and we use the analytical solution directly.
#include "newmat.h" using namespace NEWMAT;
ReturnMatrix mv calculate portfolio given mean unconstrained(const Matrix& e, const Matrix& V, const double& r){ int no assets=e.Nrows();
Matrix ones = Matrix(no assets,1); for (int i=0;i<no assets;++i){ ones.element(i,0) = 1; }; Matrix Vinv = V.i(); // inverse of V
Matrix A = (ones.t()*Vinv*e); double a = A.element(0,0); Matrix B = e.t()*Vinv*e; double b = B.element(0,0); Matrix C = ones.t()*Vinv*ones; double c = C.element(0,0); double d = b*c a*a;
Matrix Vinv1=Vinv*ones; Matrix Vinve=Vinv*e;
Matrix g = (Vinv1*b Vinve*a)*(1.0/d); Matrix h = (Vinve*c Vinv1*a)*(1.0/d); Matrix w = g + h*r;
w.Release(); return w; };
C++Code 6.3: Calculating the unconstrained frontier portfolio given an expected return using Newmat
Example
Mean variance calculations.
e= 0:05 0:1 V= 1:0 0:0 0:0 1:0
Find the optmal minimum variance portfolio with return r = 0:075. C++program:
cout << "Testing portfolio calculation " << endl; Matrix e(2,1); e(1,1)=0.05; e(2,1)=0.1; Matrix V(2,2); V(1,1)=1.0; V(2,1)=0.0; V(1,2)=0.0; V(2,2)=1.0; double r=0.075;
Matrix w = mv calculate portfolio given mean unconstrained(e,V,r); cout << " suggested portfolio: ";
cout << " w1 = " << w(1,1) << " w2 = " << w(2,1) << endl;
Output from C++program:
Testing portfolio calculation
#include <itpp/itbase.h> using namespace itpp;
mat mv calculate portfolio given mean unconstrained(const vec& e, const mat& V, const double& r){ int no assets=e.size();
vec one = ones(no assets);
mat Vinv = inv(V); // inverse of V mat A = one.transpose()*Vinv*e; double a = A(0,0); mat B = e.transpose()*Vinv*e; double b = B(0,0); mat C = one.transpose()*Vinv*one; double c = C(0,0); double d = b*c a*a; mat Vinv1=Vinv*one; mat Vinve=Vinv*e;
mat g = (Vinv1*b Vinve*a)*(1.0/d); mat h = (Vinve*c Vinv1*a)*(1.0/d); mat w = g + h*r;
return w; };
Chapter 7
Futures algoritms.
Contents
7.1 Pricing of futures contract. . . 81
In this we discuss algoritms used in valuing futures contracts.
7.1 Pricing of futures contract.
The futures price of an asset without payouts is the future value of the current price of the assset. ft= er(T t)St
#include <cmath> using namespace std;
double futures price(const double& S, // current price of underlying asset const double& r, // risk free interest rate
const double& time to maturity) { return exp(r*time to maturity)*S;
};
C++Code 7.1: Futures price
Example
Let S = 100 and r = 10%. What is the futures price for a contract with time to maturity of half a year? C++program:
double S=100; double r=0.10; double time=0.5;
cout << " futures price = " << futures price(S,r, time) << endl;
Output from C++program:
Chapter 8
Binomial option pricing
Contents
8.1 Options . . . 82