# C++ Covariance function

This topic is 4405 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm translating some code to C++ from another language that has a built-in Cov function, but after looking into CMath and googling for a bit, I can't seem to find a C/C++ Covariant function that doesn't require a purchase. Can I get some help? Is there a free lib out there or a site that hosts a function? Any help would be appreciated, thanks.

##### Share on other sites
It shouldn't be that hard to implement on your own...

You want to compute the covariance between what and what? In other words, what does your data look like?

##### Share on other sites
Well, here's the line of code using covariance in the language I'm translating from:

Rcov = cov(Noise(p-mem:p,2:4));

Some obvious things that should be noted:

1) Rcov is a 3x3 matrix (and this is a typeless language.)
2) cov is obviously the covariance function
3) Noise is a 2D array that, I believe, stores floating point numbers
4) p stands for point and stores the loop count
5) mem is equal to 12
5a) this line is called if p > 12, thus it will be using p-mem or 13-12 through p or 13. So.. it will be using elements 1-13 of Noise's first dimension once it hits loop 13.
6) the 2:4 indicates that it wants to only use elements of index 2-4 of the second dimension per element of the first dimension.

Now this is actually the confusing part to me.. I'd hoped to get the cov function first and then start experimenting with the solution to my confusion, but what's a little strange to me is that it seems to want to take the covariance of 39 items (13 from dimension one * 3 from dimension two) and fill them up in the 9 positions of the 3x3 array. I'm pretty sure, from the purchasable functions I've seen, that the cov will just return a single floating point number.. That to me means I should only have 3 numbers returned to Rcov (i.e. I will have Rcov[0][0]=cov(Noise[1-13][2]) then Rcov[0][1]=cov(Noise[1-13][3]) and finally Rcov[0][2]=cov(Noise[1-13][4]).) But I'm pretty sure the 3x3 matrix is supposed to be full.

Hopefully this is more helpful than it is confusing.

Edit: One of the purchasable functions I've spoken of is here. Seems the way they're using theirs is taking two arrays and calculating them against each other to produce a floating point number.. which is a bit different than how I imagined it was supposed to be done.

##### Share on other sites
Hi,

it looks like you want to get the Covariance matrix. Hope this helps: Wikipedia.org: Covariance matrix

##### Share on other sites
Yes, knowing that it was instead a covariance matrix proved very helpful. Google produced this link with that new information. The code there should work fine, I think.

1. 1
2. 2
Rutin
30
3. 3
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633647
• Total Posts
3013108
×