To analyse given data using coeffificient of correlation and regression line
Python
Correlation describes the strength of an association between two variables, and is completely symmetrical, the correlation between A and B is the same as the correlation between B and A. However, if the two variables are related it means that when one changes by a certain amount the other changes on an average by a certain amount.
If y represents the dependent variable and x the independent variable, this relationship is described as the regression of y on x. The relationship can be represented by a simple equation called the regression equation. The regression equation representing how much y changes with any given change of x can be used to construct a regression line on a scatter diagram, and in the simplest case this is assumed to be a straight line.
import numpy as np
import math
import matplotlib.pyplot as plt
x=[int(i) for i in input().split()]
y=[int(i) for i in input().split()]
N=len(x)
sx=0
sy=0
sxy=0
sx2=0
sy2=0
for i in range(0,N):
sx=sx+x[i]
sy=sy+y[i]
sxy=sxy+x[i]*y[i]
sx2=sx2+x[i]**2
sy2=sy2+y[i]**2
r=(N*sxy-sx*sy)/(math.sqrt(N*sx2-sx**2)*math.sqrt(N*sy2-sy**2))
print("The correlation cofficient of %0.3f"%r)
byx=(N*sxy-sx*sy)/(N*sx2-sx**2)
xmean=sx/N
ymean=sy/N
print("The Regression line Y on X is ::: %0.3f + %0.3f (x-%0.3f)"%(ymean,byx,xmean))
plt.scatter(x,y)
def reg(x):
return ymean+byx*(x-xmean)
x=np.linspace(0,80,51)
y1=reg(x)
plt.plot(x,y1,'r')
plt.xlabel('x-data')
plt.ylabel('y-data')
plt.legend(['Regression Line','Data points'])
The Correlation and regression for data analysis of objects from feeder using probability distribution are calculated