473,761 Members | 5,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<double a(c);
valarray<double b(d);
copy(x.begin(), x.end(), &a[0]);
copy(y.begin(), y.end(), &b[0]);

// expected values "mean"
double Ea = a.sum() / static_cast<dou ble(a.size());
double Eb = b.sum() / static_cast<dou ble(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 11475
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<double a(c);
valarray<double b(d);
copy(x.begin(), x.end(), &a[0]);
copy(y.begin(), y.end(), &b[0]);

// expected values "mean"
double Ea = a.sum() / static_cast<dou ble(a.size());
double Eb = b.sum() / static_cast<dou ble(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
1717
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 and 31/11/05 and it will be universal code for all the month not a specific month
4
1282
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
8950
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 performances. Is there any stl method for a fast search? Andrea
2
1263
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, pDw);
3
2624
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 have the below code, But seems not working.. <?php $title = " Form"; echo "Hello"; if(isset($_POST)) { $f1 = $_POST; }
2
2811
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 an array: // find max and min value foreach( $value as $__vals )
8
3498
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 entered in the txtbox and click the button I get the value of the string. The problem I am facing is that the value I am getting is something like: John-William-Henry-Jack. This format is already in the database. What I need is to split the string so that...
0
1033
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 deviation, however the correlation is wrong. Is there somethin wrong with my formula or the way I am coding things? The following code is Module 3 of the code for correlation: Module 1 finds the mean, variance, and std deviation of X-values, and Module...
2
2339
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 word '--Begin'. So in order to locate the string from only 100 line, I think copying all the file content into any array is not a good idea. I want the grep command to exist in the script. I have the following code. I need this to be modified...
0
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9336
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10111
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9948
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5215
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.