Spearman Rank Correlation Coefficient
Simply, run the program and select the type of question you want to solve, enter the data ( data can be entered in space separated format or by entering the numbers one by one ) .
#include<bits/stdc++.h>
#define maax(a,b,c) (a>b && a>c)?a:(b>a && b>c)?b:(c>a && c>b)?c:(a>b)?a:(b>c)?b:(c>a)?c:(a>c)?a:(a==b && b==c)?a:-1
using namespace std;
/*
Sample Data: Class Dated 15th December 2022
x: 1 6 5 10 3 2 4 9 7 8
y: 3 5 8 4 7 10 2 1 6 9
z: 6 4 9 8 1 2 3 10 5 7
*/
vector<int> rankGiver(vector<int> arr){
int temp;
int arr_size=arr.size();
vector<int> array(arr);
for(int i=0;i<arr_size-1;i++){
for(int j=0;j<arr_size-1-i;j++){
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
vector<int> rank(arr_size);
rank[arr.size()-1]=1;
for(int i=arr_size-2;i>=0;i--){
rank[i]=rank[i+1]+1;
}
// cout<<endl;
for(int k=0;k<arr_size;k++){
/*In the below for loop I have made a change
*Instead of l=k+1, I have done l=k,because
*bcz it is possible that the first element of
*original "array" maybe equal to the first
*element of the "arr" array and then algo
*won't find the number
*/
for(int l=0;l<arr_size;l++){
if(array[k]==arr[l]){
if(array[k]==1){
}
array[k]=rank[l];
break;
}
}
}
return array;
}
int sq(int a){
return a*a;
}
//Below func is implemented to take the data, NOT the ranks as input
//For two data, that is x and y
double spearManCorelation1(vector<int> x,vector<int> y){
double ans;
int n=x.size();
vector<int> Rx(n),Ry(n);
Rx=rankGiver(x);
// cout<<"Rx: ";
// for(int z:Rx){
// cout<<z<<" ";
// }
// cout<<endl;
Ry=rankGiver(y);
// cout<<"Ry is: ";
// for(int z:Ry){
// cout<<z<<" ";
// }
// cout<<endl;
vector<int> d(n),d2(n);
int d2_sum=0;
for(int i=0;i<n;i++){
d[i]=Rx[i]-Ry[i];
d2[i]=sq(Rx[i]-Ry[i]);
d2_sum+=d2[i];
}
int n3=n*n*n;
cout<<"d list is: ";
for(int x:d){
cout<<x<<" ";
}
cout<<endl;
cout<<"d square list is: ";
for(int x:d2){
cout<<x<<" ";
}
cout<<endl;
// cout<<"d2_sum is "<<d2_sum<<endl;
ans=1-((6*d2_sum))/((double)(n3-n));
return ans;
}
// Overloaded spearManCorelation1 function
void spearManCorelation1(){
/* Function to ask for values of x and y */
double ans;
cout<<"Enter the value of n: ";
int n;
cin>>n;
vector<int> x(n),y(n);
cout<<"Enter the values of x: ";
for(int i=0;i<n;i++){
cin>>x[i];
}
cout<<endl;
cout<<"Enter the values of y: ";
for(int i=0;i<n;i++){
cin>>y[i];
}
cout<<endl;
vector<int> Rx(n),Ry(n);
Rx=rankGiver(x);
cout<<"Ranks of x: ";
for(int z:Rx){
cout<<z<<" ";
}
cout<<endl;
Ry=rankGiver(y);
cout<<"Ranks of y: ";
for(int z:Ry){
cout<<z<<" ";
}
cout<<endl;
vector<int> d(n),d2(n);
int d2_sum=0;
for(int i=0;i<n;i++){
d[i]=Rx[i]-Ry[i];
d2[i]=sq(Rx[i]-Ry[i]);
d2_sum+=d2[i];
}
int n3=n*n*n;
cout<<"d values: ";
for(int x:d){
cout<<x<<" ";
}
cout<<endl;
cout<<"d square values: ";
for(int x:d2){
cout<<x<<" ";
}
cout<<endl;
// cout<<"d2_sum is "<<d2_sum<<endl;
ans=1-((6*d2_sum))/((double)(n3-n));
cout<<"r: "<<ans<<endl;
// return ans;
}
//The below function tells the values of Rxy,Ryz,Rxz respectively
void spearManCorelation2(vector<int> x,vector<int> y,vector<int> z){
double rxy,ryz,rxz;
vector<int> ranks;
ranks=rankGiver(x);
cout<<"Ranks of x: ";
for(int x:ranks){
cout<<x<<" ";
}
cout<<endl;
ranks=rankGiver(y);
cout<<"Ranks of y: ";
for(int x:ranks){
cout<<x<<" ";
}
cout<<endl;
ranks=rankGiver(z);
cout<<"Ranks of z: ";
for(int x:ranks){
cout<<x<<" ";
}
cout<<endl;
rxy=spearManCorelation1(x,y);
ryz=spearManCorelation1(y,z);
rxz=spearManCorelation1(x,z);
cout<<"Rxy: "<<rxy<<"\nRyz:"
<<ryz<<"\nRxz: "<<rxz<<endl;
// cout<<
}
int main() {
int choice=1;
// Prompt the user for 2 or 3 term question option
do{
cout<<((choice<1 || choice>2)?"You need to make a VALID choice!!\n":"Please select an option :)\n");
cout<<"Question with x and y:\t\t1\n";
cout<<"Question with x,y and z:\t2\n";
cin>>choice;
}while(choice!=1 && choice!=2);
if(choice==2){
// Handle x,y and z containing questions
int n;
cout<<"Enter the number of terms(n): ";
cin>>n;
cout<<"Enter the data for x\n";
vector<int> x(n),y(n),z(n);
for(int i=0;i<n;i++){
cin>>x[i];
}
cout<<"Enter the data for y\n";
for(int i=0;i<n;i++){
cin>>y[i];
}
cout<<"Enter the data for z\n";
for(int i=0;i<n;i++){
cin>>z[i];
}
spearManCorelation2(x,y,z);
}
else{
// Handle x and y containing questions
spearManCorelation1();
}
/*
*
* This program takes the data of x and y(or even z) as input
* and returns the value of r(Coefficient something)
* so that we don't have to go through the tedious
* work :)
*
*/
// Sample data for x,y and z respectively.
// From question of Cases, Dated: 1-9-2022
// 42 63 70 12 18 44 93 40 60 71
// --
// 62 63 74 83 21 37 48 12 9 20
// --
// 8 42 34 63 12 19 47 18 70 95
// int num=maax(2,2,2);
// cout<<num<<endl;
// // cout<<(maax(1,2,3))<<endl;
/*************RankGiver*****************/
// cout<<"Using the rankGiver func:\n";
// vector<int> r=rankGiver(x);
// cout<<"The array returned by the rankGiver func:\n";
// for(int z:r){
// cout<<z<<" ";
// }
return 0;
}
Comments
Post a Comment