C++ program to implement correlation analysis on nominal and numeric data type.
Simple C++ program to implement correlation analysis on nominal and numeric data type.
Code:
#include<iostream>
#include<math.h>
using namespace std;
int main() {
int n1, i, sum=0, sum1=0;
floatmean_A, mean_B, std_A, std_B, corr;
cout<< "Enter the number of data: " <<endl;
cin>> n1;
int x[n1];
cout<< "Enter the data for A: " <<endl;
for(i=0; i<n1; i++) {
cin>> x[i]; }
int y[n1];
cout<< "Enter the data for B: " <<endl;
for(i=0; i<n1; i++) {
cin>> y[i]; }
for(i=0; i<n1; i++) {
sum = sum + x[i];
sum1 = sum1 + y[i]; }
mean_A = (float)sum / n1;
mean_B = (float)sum1 / n1;
sum=0;
sum1=0;
for(i=0; i<n1; i++) {
sum = sum + pow(x[i] - mean_A, 2);
sum1 = sum1 + pow(y[i] - mean_B, 2); }
std_A = sqrt(sum / n1);
std_B = sqrt(sum1 / n1);
sum=0;
for(i=0; i<n1; i++) {
sum = sum + x[i] * y[i]; }
corr = ((sum - (n1 * mean_A * mean_B))/(n1 * std_A * std_B));
if(corr> 0) {
cout<< "A and B are positively correlated." <<endl; }
else if(corr< 0) {
cout<< "A and B are negatively correlated." <<endl; }
else if(std_A == 0 || std_B == 0) {
cout<< "Correlation is undefined." <<endl; }
else {
cout<< "A and B are independent." <<endl; }
return 0;
}
Comments
Post a Comment