473,473 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

long shot

3 New Member
hi guys this is a very big long shot but im new to all this and was hoping someone out there could assist me. i need to make a program that can determine what the minimum spanning tree of 8 stocks is i have all the correct formulas and figures that i have used excel to calculate but just don't know how to compute it and write the algorithm! if anyone can give help me i would be really grateful. xxx
Mar 25 '07 #1
2 1422
jenny22
3 New Member
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
Mar 25 '07 #2
Roonie
99 New Member
sure.

what part do you need help on again? if youve already got the formula in excel, coding it into a program shouldnt be much different . . . use functions to break down the algebra if its too complex to do in one line.

http://www.sparknotes.com/cs/c++fund.../section1.html
Mar 25 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: sam | last post by:
Hello Coders I have a question can we have a php script or function to take screen shot for webpage? for example , let the php script take a shot for www.google.com and convert the content...
2
by: Peter Kwan | last post by:
Hi, I believe I have discovered a bug in Python 2.3. Could anyone suggest a get around? When I tested my existing Python code with the newly released Python 2.3, I get the following warning: ...
9
by: Melissa | last post by:
Hi, I'm new to the group and haven't had a chance to read the FAQ, but I have a question that's driving me crazy. If anyone here can help me with this or can point me to another site or...
8
by: gregory_may | last post by:
Is there a way to grab a "Screen Shot" that includes "Tool Tips"? I saw this code someplace, cant remember where. But it doesnt grab "Tool Tips". Is there a better way to do this in .net?...
4
by: Anon | last post by:
Hello All! I have a long string that I need to make sense out of. I have the documentation about what info is between which characters, I just need to somehow parse each 94 character string into...
3
by: Simon | last post by:
Dear reader, I have a Snap Shot file located in a folder. Now I like to view the Snap Shot file. First I created a string with the full pathname and Snap Shot file.
16
by: anon.asdf | last post by:
Hi! On a machine of *given architecture* (in terms of endianness etc.), I want to access the individual bytes of a long (*once-off*) as fast as possible. Is version A, version B, or version...
5
by: bdy120602 | last post by:
Is it possible, when a user or viewer of your Web page, prints or takes a screen shot of a Web page with mousover (roll-over) text in it, to have that text printed or captures as part of the screen...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.