468,512 Members | 983 Online

# 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);
copy(y.begin(), y.end(), &b);

// 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 11043 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);
copy(y.begin(), y.end(), &b);

// 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 discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 1 post views Thread by tomerslider | last post: by 4 posts views Thread by Nospam | last post: by 16 posts views Thread by vizzz | last post: by 2 posts views Thread by brof777 | last post: by 3 posts views Thread by ajd335 | last post: by 2 posts views Thread by luftikus143 | last post: by 8 posts views Thread by bassemG | last post: by reply views Thread by bigmanutdfan7 | last post: by 2 posts views Thread by powerfulperl | last post: by reply views Thread by ravipankaj | last post: by reply views Thread by ravipankaj | last post: by reply views Thread by NPC403 | last post: by reply views Thread by slotstar | last post: by 3 posts views Thread by Davism4 | last post: by 1 post views Thread by sonnet | last post: by reply views Thread by jimatqsi | last post: by reply views Thread by ajay sahare | last post: by 1 post views Thread by hwsilver | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.