UrbanPro
true

Learn Python Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

A program to calculate Correlation Coefficient

Ashish K Sharma
23/06/2022 0 0

Task: Calculating the Correlation-coefficient using Python

 

We know that the correlation coefficient is calculated using the formula

             nΣxy- ΣxΣy / (√(nΣx^2-(Σx)^2) * (nΣy^2-(Σy)^2))

 

In the above formula, n is the total number of values present in each set

of numbers (the sets have to be of equal length). The two sets of numbers

are denoted by x and y (it doesn’t matter which one you denote as which).

The other terms are described as follows:

 

Σxy :Sum of the products of the individual elements of the two sets

of numbers, x and y

          Σx : Sum of the numbers in set x

          Σy:  Sum of the numbers in set y

          Σx^2:Square of the sum of the numbers in set x

          Σy^2:Square of the sum of the numbers in set y

          (Σx)^2:Sum of the squares of the numbers in set x

          (Σy)^2:Sum of the squares of the numbers in set y/

 

 

Let us now write a Python Program which calculates the correlation coefficient for us. We will be using the following two functions in the program:

 

  1. Sum(x) : Using this function on a list of numbers,x will sum up the numbers in the list.
  1. Zip(x,y): returns the list of corresponding numbers in lists x,y which  you can then use in a loop to perform other operations.

 

 

 

import os
import sys
#A Program to calculate the correlation coefficient



def find_corr_x_y(x,y):
    n = len(x)

    #Find the sum of the products
    prod = []
    for xi,yi in zip(x,y):
        prod.append(xi*yi)

    sum_prod_x_y = sum(prod)
    sum_x = sum(x)
    sum_y = sum(y)

    squared_sum_x = sum_x ** 2
    squared_sum_y = sum_y ** 2

    x_square = []

    for xi in x:
        x_square.append(xi**2)
        
    x_square_sum = sum(x_square)

    y_square = []
    for yi in y:
        y_square.append(yi**2)
        
    y_square_sum = sum(y_square)

    numerator = n * sum_prod_x_y - sum_x * sum_y
    dterm1 = n*x_square_sum - squared_sum_x
    dterm2 = n*y_square_sum - squared_sum_y
    denm = (dterm1 *dterm2) ** 0.5
    corr = numerator / denm
    return corr


crr = 0

X1 = [5.1,3.2,3,1.4,3.8,1.0,2.8,-0.3,6.9,2.5,6.2,4.6]
Y = [30,29,30,35,36,36,34,48,24,27,21,30]
 
if (len(X1) == len(Y)):        
    crr = find_corr_x_y(X1,Y)
    print("Pearson product-moment Correlation Coefficient = {0}".format(crr))
    
    if (crr >= 0.8):
     print("Strong Positive Correlation")
     
    elif (crr <= -0.8):
       print("Strong Negative Correlation")
else:
 print("Sorry,the data set lengths are not equal")

The find_corr_x_y() function accepts two arguments, x and y, which are the two sets of numbers we want to calculate the correlation for. Inside this function all terms used for calculating the Correlation coefficient are obtained. Also, correlation coefficient is only calculated when the list of numbers passed to the function are equal in length.

 

       

 

OUTPUT

>>>

Pearson product-moment Correlation Coefficient = -0.823545657378

Strong Negative Correlation

>>> 

 

Try writing this program,students, in your computer and see how it runs,with equal and unequal lists of numbers.

0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Two lists using Loops - Python Program 1
To generate a list of all possible combinations of two lists using loops, you can use nested loops to iterate through both lists and create a new list that contains all the combinations. list1 = list2...

Built-In Functions (Python)
Built-in Functions: The Python interpreter has a number of functions built into it that are always available. They are listed here in alphabetical order. Built-in Functions abs() divmod() input() open() staticmethod() all() enumerate() int() ord() str() any() eval() isinstance() pow() sum() basestring() execfile() issubclass() print() super() bin() file() iter() property() tuple() bool() filter() len() range() type() bytearray() float() list() raw_input() unichr() callable() format() locals() reduce() unicode() chr() frozenset() long() reload() vars() classmethod() getattr() map() repr() xrange() cmp() globals() max() reversed() zip() compile() hasattr() memoryview() round() __import__() complex() hash() min() set() ...


Datetime Module
#this examples demonstrate use of datetime module import datetimeob=datetime.datetime.now()print("-"*25)print(ob)print(ob.year)print(ob.month)print(ob.day)print(ob.hour)print(ob.minute)print(ob.second)str1=str(ob.hour)+':'+str(ob.minute)+':'+str(ob.second)print...

Python- Basic Concepts part 2
1. Other Numerical Operations a. Exponentiation Besides addition, subtraction, multiplication, and division, Python also supports exponentiation, which is the raising of one number to the power of...
X

Looking for Python Training Classes?

The best tutors for Python Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Python Training with the Best Tutors

The best Tutors for Python Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more