C language code for Covariance Calculation

Source: Internet
Author: User

Covariance plays an important role in Signal Analysis:

1. It indicates whether X is positively correlated or negatively correlated with Y. If X is negative, the covariance is positive and the covariance is positive. 2. Covariance shows the correlation degree of X and Y. When the correlation points are scattered in the four quadrants, the correlation degree is very low. When the correlation points are distributed on the average line of X and Y, indicates unrelated. When the correlation point is close to the always-on line, it indicates that the correlation relationship is close. When the correlation point falls to the always-on line, it indicates completely related.

The computer formula for covariance is sxy = (Σ (Xi-x ^) (Yi-y ^)/(n-1), I =, 2 ..., n-1. ^ indicates the average value.

Void Cox (int x [], int y [], int N, double * P)
{
Int I;
Double averx, Avery, sumx = 0, Sumy = 0;
Double sum = 0;
For (I = 0; I <n; I ++)
{
Sumx + = x [I];
Sumy + = Y [I];
}
Averx = sumx/N;
Avery = Sumy/N;
For (I = 0; I <n; I ++)
Sum + = (X [I]-averx) * (Y [I]-Avery );
* P = sum/(n-1 );
}

Void output (INT array [], int N)
{
Int I;
For (I = 0; I <n; I ++)
Printf ("% 4D", array [I]);
}

Void main ()
{
Int X [5] = {1, 2, 3, 4, 5 };
Int y [5] = {5, 4, 3, 2, 1 };
Double Co;
Cox (X, Y, 5, & Co );
Printf ("/NX:/N ");
Output (x, 5 );
Printf ("/NY:/N ");
Output (Y, 5 );
Printf ("/nthe Cox of X and Y is: % lf/N", CO );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.