CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Calculate the correlation coefficient (the least exciting part)


#include  "calcmarks.h"    // local header file provides declarations

void correlation(int nmarks)
{
//  MANY LOCAL VARIABLES REQUIRED TO CALCULATE THE CORRELATION
    double   sumx   = 0.0;
    double   sumy   = 0.0;
    double   sumxx  = 0.0;
    double   sumyy  = 0.0;
    double   sumxy  = 0.0;

    double   ssxx, ssyy, ssxy;
    double   r, m, b;

//  ITERATE OVER EACH MARK
    for(int n=0 ; n < nmarks ; ++n) {
        sumx    += projmarks[n];
        sumy    += exammarks[n];
        sumxx   += (projmarks[n] * projmarks[n]);
        sumyy   += (exammarks[n] * exammarks[n]);
        sumxy   += (projmarks[n] * exammarks[n]);
    }

    ssxx    = sumxx - (sumx*sumx) / nmarks;
    ssyy    = sumyy - (sumy*sumy) / nmarks;
    ssxy    = sumxy - (sumx*sumy) / nmarks;

//  CALCULATE THE CORRELATION COEFFICIENT, IF POSSIBLE
    if((ssxx * ssyy) == 0.0) {
        r   = 1.0;
    }
    else {
        r   = ssxy / sqrt(ssxx * ssyy);
    }
    printf("correlation is %.4f\n", r);

//  DETERMINE THE LINE OF BEST FIT, IT ONE EXISTS
    if(ssxx != 0.0) {
        m   = ssxy / ssxx;
        b   = (sumy / nmarks) - (m*(sumx / nmarks));
        printf("line of best fit is y = %.4fx + %.4f\n", m, b);
    }
}

 


CITS2002 Systems Programming, Lecture 17, p9, 26th September 2023.