Rank Corelation/Spearman Rank Corelation

 The below mentioned code has two functions, for getting the corelation. The first one takes two vectors and provides the value of r(corelation) and the second one uses the first function to get the respective values of r. The code can also be manipulated to get the value of r when ranks are provided instead of the data of x,y and z. By just removing the part where rankGiver is being used to find the ranks.

Also the further part of the questions where there is repetition of elements in the data is a bit dubious, but the implementation is possible by changes in the rankGiver function.

#include<bits/stdc++.h>
using namespace std;
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:\n";
    for(int z:Rx){
        cout<<z<<" ";
    }
    cout<<endl;
    
    Ry=rankGiver(y);
    cout<<"Ry is:\n";
    for(int z:Ry){
        cout<<z<<" ";
    }
    
    vector<int> d2(n);
    int d2_sum=0;
    for(int i=0;i<n;i++){
        d2[i]=sq(Rx[i]-Ry[i]);
        d2_sum+=d2[i];
    }
    int n3=n*n*n;
    cout<<"d2_sum is "<<d2_sum<<endl;

    ans=1-((6*d2_sum))/((double)(n3-n));
    
    return ans;
}


//The below function tells the values of Rxy,Ryz,Rxz respectively
void spearManCorelation2(vector<int> x,vector<int> y,vector<int> z){
    cout<<"Rxy: "<<spearManCorelation1(x,y)<<"\nRyz: 
"<<spearManCorelation1(y,z)<<"\nRxz: "<<spearManCorelation1(x,z)<<endl;
}
int main() {
    /*
     *
     * 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 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);
    
    /*************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

Popular Posts