How I Plan for the program to work:
In the excel sheet there will be a selection of 8 different stocks over a particular time series the program will then calculate the correlation between each pair of stock as follows
For the sake of the example they will be known as
A B C D E F G H
Using the formula
You will get a range of results similar to this
AB = 0.7 BC = 1.2 CD = 0.4 DE = 0.1 EF = 1.3 FG = 1.1 GH = 1.0
AC = 0.42 BD = 1.12 CE = 0.94 DF = 0.04 EG = 0.7 FH = 1.4
AD = 0.75 BE = 0.9 CF = 1.37 DG = 0.02 EH = 0.84
AE = 1.26 BF = 0.65 CG =1.01 DH = 0.5
AF = 0.39 BG = 0.43 CH =1.28
AG = 1.45 BH = 1.56
AH = 0.31
Then in order to achieve the calculation of the metric distance between each 28 pairs of stocks the following formula needs to be applied
formula goes here
Where if the correlation is
= 0 then it means it is completely correlated
= then is not correlated
= 2 then it is completely anti correlated
Then using these metric distances starting from the determined distance that is closest to zero and is the most closely correlated we can start to map out the minimum spanning tree.
Each number represents the metric distance
Between the pair of stocks vertices that appear at the end of that edge.
Then in order to get the minimum spanning tree I plan to
Add the lowest edge into the category I will refer to a “ T ”:
T = {AD}
Then find the next edge that is the most closely correlated and add that to my “T” category unless it forms a cycle with edges that is already present in “ T ” .
In order to determine that this rule is followed the program will also have to have another category I will refer to as “ Z ” which will contain all the 28 pairs of stocks and then as one enters the “ T ” (Minimum spanning tree) the software will delete this from the “Z ” category as well as all the edges that form a cycle with “ T ”
So for instance
i put example here
At this point this is what you should have in your categories
T { }
Z { }
The next point although it seems would be BE as is the most closely correlated is not and is infact as BE would form a cycle which is against the rule so therefore even though it did not form part of the spanning tree is eliminated from “ Z ” for the very same reason.
Your category should look something like this
This procedure has to be followed until E is empty and then the program should be able to output “ T ” (The minimum spanning tree)
so far i have this
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double mean(double rcplc[], int n); //functions to use
double mean(double rposn[], int n); //functions to use
double mean(double rabp[], int n); //functions to use
double mean(double rfp[], int n); //functions to use
double mean(double rjfs[], int n); //functions to use
double mean(double rsg[], int n); //functions to use
double mean(double rsh[], int n); //functions to use
double mean(double rsf[], int n); //functions to use
double cov(double x[], double y[], double meanx, double meany,int n);
// ============================================
const int DIM = 8;
class vector;
class matrix;
class vector
{
public :
// Constructor
vector() {;}
vector(double value)
{for (int i=0; i<DIM; i++) v[i] = value;}
// Accessor
double get(int i){return v[i-1];}
void print();
// Mutator
void set(int i, double value){ v[i-1] = value;}
//vector friend matvec(matrix &mat1, vector &v1);
private :
double v[DIM];
};
// =============================================
class matrix
{
public :
// Constructor
matrix(double value){int i,j; for(i=0; i<DIM; i++)
for(j=0;j<DIM;j++) Cov[i][j] = value;}
// Accessor
double get(int i,int j){return Cov[i-1][j-1];}
void print();
// Mutator
void set(int i, int j, double value){ Cov[i-1][j-1] = value;}
void hilbert(){int i,j;for(i=0; i<DIM; i++) for(j=0; j<DIM; j++)
Cov[i][j] = 1.0/(i+j+1);}
matrix Multiply (matrix &mat1,matrix &mat2);
vector friend matvec(matrix &mat1, vector &v1);
private :
double Cov[DIM][DIM]; };
// ================================================== =======================
void vector::print()
//this prints the vector
{
cout.precision(3);
cout.setf(ios::fixed);
for (int i=0; i<DIM; i++)
{
switch(i)
{
case 0 : cout << "/" << v[i] << "\\" << endl; break;
case DIM-1: cout << "\\" << v[i] << "/" << endl; break;
default : cout << "|" << v[i] << "|" << endl;
}
}
}
//================================================== ========================
void matrix::print()
{
// this prints the matrix
cout.precision(2);
cout.setf(ios::fixed);
for (int i=0; i<DIM; i++)
{
switch(i)
{
case 0 : cout << "/"; break;
case DIM-1: cout << "\\"; break;
default : cout << "|";
}
for (int j=0; j<DIM; j++) cout << Cov[i][j] << "\t";
switch(i)
{
case 0 : cout << "\\" << endl; break;
case DIM-1: cout << "/" << endl; break;
default : cout << "|" << endl;
}
}
}
//================================================== =============================
double multv(vector &vec1, vector &vec2)
{
int i;
double multv=0;
for(i=1; i<=DIM;i++)
multv+=(vec1.get(i)* vec1.get(i));
return multv;
}
//================================================== ==============================
matrix matrix::Multiply (matrix &mat1,matrix &mat2)
//multiplies a matrix by a matrix
{
int i,j,k;
double temp=0;
matrix tempmat(0);
for (i=1;i<DIM;i++)
{
for (j=1;j<DIM;j++)
{
for (k=1;k<DIM;k++)
temp +=(mat1.get(i,k) * mat2.get(k,j));
tempmat.set(i,j,temp);
}
}
return tempmat;
}
//================================================== =============================
vector matvec(matrix &mat1,vector &v)
//multiplies a matrix by a vector
{
int i,k;
double temp=0;
vector tempvec(0);
for (i=1;i<DIM;i++)
{
for (k=1;k<DIM;k++)
temp +=(mat1.get(i,k) * v.get(k));
tempvec.set(i,temp);
}
return tempvec;
}
//================================================== ==============================
//Function To Calculate The correlation coefficients
double mean(double r[], int n)
{
int i;
double sum=0;
for (i=0;i<n;i++)
sum += r[i];
return ????????//; //sum using my formula that goes here
}
//================================================== ================================
//Function To Calculate metric distance again using my formula
please any help