473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

code to find correlation


Hi

I wrote a code to calculate the correlation between 2 sequence of
double numbers presented as vectors.
did I go about this the right way?

thanks

#include <vector>
using std::vector;
#include <valarray>
using std::valarray;
cor::cor(vector<double>& x, vector<double>& y)
{
int c = x.size();
int d = y.size();
if(c != d)
cout << "to calculate correlation, variables"
" must be of the same number of elements" << endl;

// converts the input to valarray for spead and ellegance.
valarray<doublea(c);
valarray<doubleb(d);
copy(x.begin(), x.end(), &a[0]);
copy(y.begin(), y.end(), &b[0]);

// expected values "mean"
double Ea = a.sum() / static_cast<double(a.size());
double Eb = b.sum() / static_cast<double(b.size());
// standard diviation
double Sa = sqrt( pow((a-Ea),2).sum()/c );
double Sb = sqrt( pow((b-Eb),2).sum()/d );
// double Sb = sqrt( ((a*a).sum()/c)- pow(Ea, 2) ); // faster
// but more round-offs

// correlation
cof = ( ((a-Ea)/Sa)*((b-Eb)/Sb) ).sum()/(c-1);
}
Aug 26 '06 #1
1 11434
Gary Wessle wrote:
I wrote a code to calculate the correlation between 2 sequence of
double numbers presented as vectors.
did I go about this the right way?
I would never use 'cout' in a constructor unless it's a debug output
(in that case it should go to 'cerr' and be #ifdef'ed). You might
think of throwing an exception instead.
>
thanks

#include <vector>
using std::vector;
#include <valarray>
using std::valarray;
cor::cor(vector<double>& x, vector<double>& y)
{
int c = x.size();
int d = y.size();
if(c != d)
cout << "to calculate correlation, variables"
" must be of the same number of elements" << endl;

// converts the input to valarray for spead and ellegance.
valarray<doublea(c);
valarray<doubleb(d);
copy(x.begin(), x.end(), &a[0]);
copy(y.begin(), y.end(), &b[0]);

// expected values "mean"
double Ea = a.sum() / static_cast<double(a.size());
double Eb = b.sum() / static_cast<double(b.size());
// standard diviation
Isn't it "deviation"?
double Sa = sqrt( pow((a-Ea),2).sum()/c );
double Sb = sqrt( pow((b-Eb),2).sum()/d );
What are the extra parentheses aroud 'x-Ex' for?
// double Sb = sqrt( ((a*a).sum()/c)- pow(Ea, 2) ); // faster
// but more round-offs

// correlation
cof = ( ((a-Ea)/Sa)*((b-Eb)/Sb) ).sum()/(c-1);
}
Well, I see 'cor' is an object. Why? Does it have states? Does
it have a life on its own? What's the reason for it to be an object?
Shouldn't you simply have a function, like

double cor(vector..., vector...)
{
...
return cof;
}

?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 27 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: tomerslider | last post by:
i need some code that show me all the expired product that i have in every month example: the date today is 4/11/05 i need that it will show me all the proudct that there dates betwenn 1/11/05...
4
by: Nospam | last post by:
<SCRIPT type='text/javascript' language='javascript'> document.write('<img src="http://www.example.com/image1.jpg" width="60" height="60">'); for some strange reason it is not showing the picture
16
by: vizzz | last post by:
Hi there, i need to find an hex pattern like 0x650A1010 in a binary file. i can make a small algorithm that fetch all the file for the match, but this file is huge, and i'm scared about...
2
by: brof777 | last post by:
here is the code: DWORD SomeFunction(DWORD Dw_0, DWORD Dw_1, DWORD Dw_2) { return Dw_0 + Dw_1 + Dw_2; } void main() { DWORD Dw = 0; DWORD pDw = {1,2,3}; DWORD Dw_Sum = SomeFunction(pDw, pDw,...
3
by: ajd335 | last post by:
Hi All, I have a page in which user can enter first name,last name , date and according to that My script should search for the records which matches the above last n first names and print it. I...
2
by: luftikus143 | last post by:
Hi there, I have an array, which stores in the first dimension a counter for the number of sets, and in the second the value: $value I found somewhere how to find min and max values in such...
8
by: bassemG | last post by:
Hello every body I created a simple program usine web development 2008. The program consists of a datalist control that is bound to an access database, a textbox and a button. When a string is...
0
by: bigmanutdfan7 | last post by:
I have to find the correlation by coding in the actual formula in the visual basic editor and not by calling it using the excel correlation function. I am getting the proper mean and standard...
2
by: powerfulperl | last post by:
I want to locate a string 'Local=IN' from a file and I am sure that this string is located within 100 lines(assumption) from the beginning of the file out of 5000 lines. The 100th line start with the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.