-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPartial_Correlation_Coeff_XYZ.py
More file actions
235 lines (159 loc) · 13.6 KB
/
Partial_Correlation_Coeff_XYZ.py
File metadata and controls
235 lines (159 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Python Program to find correlation coefficient.
import math
import matplotlib.pyplot as plt
# function that returns correlation coefficient.(X, Y, n)
def correlationCoefficient(X, Y, n) :
sum_X = 0
sum_Y = 0
sum_XY = 0
squareSum_X = 0
squareSum_Y = 0
i = 0
while i < n :
# sum of elements of array X.
sum_X = sum_X + X[i]
# sum of elements of array Y.
sum_Y = sum_Y + Y[i]
# sum of X[i] * Y[i].
sum_XY = sum_XY + X[i] * Y[i]
# sum of square of array elements.
squareSum_X = squareSum_X + X[i] * X[i]
squareSum_Y = squareSum_Y + Y[i] * Y[i]
i = i + 1
# use formula for calculating correlation coefficient.
corr = (float)(n * sum_XY - sum_X * sum_Y)/(float)(math.sqrt((n * squareSum_X -
sum_X * sum_X)* (n * squareSum_Y -
sum_Y * sum_Y)))
return corr
# function that returns correlation coefficient.(X, Z, n)
def correlationCoefficientZ(X, Z, n) :
sum_X = 0
sum_Z = 0
sum_XZ = 0
squareSum_X = 0
squareSum_Z = 0
i = 0
while i < n :
sum_X = sum_X + X[i]
sum_Z = sum_Z + Z[i]
sum_XZ = sum_XZ + X[i] * Z[i]
# sum of square of array elements.
squareSum_X = squareSum_X + X[i] * X[i]
squareSum_Z = squareSum_Z + Z[i] * Z[i]
i = i + 1
# use formula for calculating correlation coefficient.
corr = (float)(n * sum_XZ - sum_X * sum_Z)/(float)(math.sqrt((n * squareSum_X -
sum_X * sum_X)* (n * squareSum_Z -
sum_Z * sum_Z)))
return corr
# function that returns correlation coefficient.(Y, Z, n)
def correlationCoefficientYZ(Y, Z, n) :
sum_Y = 0
sum_Z = 0
sum_YZ = 0
squareSum_Y = 0
squareSum_Z = 0
i = 0
while i < n :
sum_Y = sum_Y + Y[i]
sum_Z = sum_Z + Z[i]
sum_YZ = sum_YZ + Y[i] * Z[i]
# sum of square of array elements.
squareSum_Y = squareSum_Y + Y[i] * Y[i]
squareSum_Z = squareSum_Z + Z[i] * Z[i]
i = i + 1
# using formula for calculating correlation coefficient.
corr = (float)(n * sum_YZ - sum_Y * sum_Z)/(float)(math.sqrt((n * squareSum_Y -
sum_Y * sum_Y)* (n * squareSum_Z -
sum_Z * sum_Z)))
return corr
# Data set
X = [66,83,89,73,72,58,64,77,67,52,84,64,55,65,84,69,64,71,45,76,86,71,78,80,57,62,42,71,81,62,81,55,70,70,65,68,66,77,77,79,70,60,59,72,58,85,86,45,55,45,76,64,51,60,74,52,75,68,65,85,74,68,83,83,68,58,94,40,74,85,59,64,78,51,67,60,75,78,51,67,37,68,64,60,52,71,82,76,51,79,91,84,36,48,95,87,55,83,63,77,65,73,62,68,63,38,75,62,85,68,85,48,76,65,56,82,51,64,58,61,58,76,86,73,58,65,83,79,62,80,53,61,77,34,68,63,68,85,64,68,58,78,69,64,61,89,90,67,31,91,82,50,64,44,50,77,63,60,67,96,56,77,82,74,88,67,48,44,100,46,68,80,76,51,50,89,67,59,96,52,56,61,75,92,73,37,74,79,68,47,72,56,48,50,79,50,84,69,47,67,54,57,93,80,62,95,79,65,55,82,25,44,76,69,82,56,80,53,73,81,68,67,45,69,69,78,85,55,13,49,62,68,62,61,61,45,77,77,77,84,65,67,72,87,53,77,59,86,84,72,76,88,100,63,93,80,77,69,61,64,42,62,83,76,69,42,61,65,70,62,59,69,52,83,80,58,95,81,67,74,76,80,67,53,76,95,26,60,98,73,55,69,54,31,91,32,40,55,67,59,52,58,36,56,39,99,33,66,49,95,47,74,56,53,74,95,75,60,74,59,57,77,87,39,87,65,57,66,90,46,45,75,69,57,69,72,69,90,44,82,84,60,92,73,79,84,65,65,69,63,97,52,91,66,53,67,80,79,72,100,96,83,77,79,40,68,72,80,90,76,61,56,64,40,67,60,66,42,80,78,57,100,68,91,77,76,57,57,65,59,61,91,81,73,60,75,100,89,83,57,56,78,67,57,67,45,75,55,73,77,52,75,61,70,77,66,59,54,54,69,72,82,63,43,76,67,90,64,76,78,51,99,81,64,49,67,81,72,62,67,56,89,51,67,71,78,82,93,66,81,79,73,43,68,84,46,51,72,62,32,55,55,57,59,74,84,76,67,42,79,71,63,78,59,80,40,68,39,81,38,50,59,82,71,73,74,69,70,56,52,61,44,64,54,56,71,70,53,89,86,62,59,79,80,55,76,47,81,60,71,74,58,82,48,64,67,73,71,78,76,65,83,76,79,60,81,64,64,73,56,60,33,60,87,64,82,42,77,46,58,73,73,58,65,81,46,90,78,62,91,64,81,79,43,71,79,81,66,45,87,89,44,82,89,60,35,39,91,76,55,78,48,47,71,90,79,40,64,73,76,60,100,89,63,55,57,69,78,74,54,69,59,64,72,61,80,49,59,82,47,60,61,83,63,46,64,72,80,71,64,95,87,95,78,72,58,49,73,79,66,72,88,71,67,76,46,72,65,55,58,94,72,66,58,77,78,33,89,70,79,41,56,79,66,62,93,86,47,53,98,56,85,51,83,70,71,47,98,46,61,69,72,88,90,54,89,73,71,79,71,70,52,70,58,74,68,72,73,79,51,77,51,83,75,48,70,71,65,49,65,89,89,62,78,48,46,56,57,73,68,73,80,53,39,61,72,80,70,72,46,35,71,65,80,84,59,62,82,34,67,45,100,51,75,59,59,96,64,19,63,64,63,66,73,61,31,70,71,69,45,66,85,36,63,30,53,68,58,54,57,41,65,84,62,32,78,75,68,61,82,63,44,84,56,70,53,69,67,81,72,71,61,87,58,73,40,100,65,46,83,72,65,49,73,43,63,30,87,80,59,66,89,81,63,68,91,70,41,85,68,100,60,90,88,61,69,70,51,63,73,61,73,53,61,68,84,76,59,55,72,68,53,100,56,66,96,75,73,55,42,49,73,71,46,67,74,44,62,62,68,79,67,47,80,84,65,56,66,68,83,55,90,77,88,94,78,61,55,64,57,77,94,87,53,31,48,41,81,74,79,65,83,91,69,49,68,43,75,92,68,58,36,74,47,82,73,43,32,68,51,33,84,64,49,54,58,81,60,71,54,75,70,73,72,69,37,71,57,64,82,69,83,30,62,46,60,60,52,59,68,66,68,96,56,87,62,72,100,82,60,70,77,49,62,53,47,45,62,73,58,64,51,69,61,51,65,78,73,42,69,75,50,47,61,76,64,69,71,64,82,56,62,68,68,63,67,57,66,75,71,45,60,49,61,81,56,72,62,86,61,46,70,69,59,76,79,62,48]
Y = [66,82,87,86,74,52,64,98,50,59,92,64,64,66,75,61,60,72,54,85,83,67,81,81,65,72,58,78,72,70,93,66,72,67,67,70,66,70,83,77,78,56,67,64,62,86,93,66,57,43,87,55,44,60,89,71,66,72,71,82,84,59,73,81,75,47,94,37,81,94,67,47,85,59,76,84,79,85,64,65,53,68,62,63,38,65,78,87,58,70,77,93,49,43,98,97,51,85,75,74,56,82,71,67,65,47,69,71,82,58,90,46,74,67,65,86,69,53,43,63,73,89,93,73,60,74,81,75,59,75,58,65,70,46,69,68,64,80,76,60,54,80,69,64,81,93,82,59,40,87,78,52,73,46,50,74,68,75,83,86,65,80,86,58,83,69,57,61,100,59,61,79,72,67,71,98,60,75,89,56,70,60,80,85,58,59,77,87,71,48,64,59,58,52,81,58,70,61,63,63,51,70,82,81,68,86,72,65,57,74,32,48,60,86,74,61,90,46,72,78,61,74,65,61,69,74,93,61,19,52,76,68,78,79,77,56,89,61,60,82,69,74,73,81,65,67,72,93,74,56,67,81,97,63,100,84,81,62,54,77,37,77,90,86,91,43,55,79,72,77,65,95,61,86,83,61,86,77,66,87,70,78,73,61,84,89,43,64,98,78,61,87,62,41,92,43,51,50,81,65,59,65,21,41,60,89,44,81,47,89,53,69,73,53,84,81,76,70,72,60,63,79,85,39,100,49,64,57,92,73,49,75,70,65,59,81,61,78,62,76,98,58,82,70,91,81,70,80,66,70,78,66,97,62,53,63,85,72,69,97,85,97,85,83,55,87,83,80,99,79,71,53,80,32,71,71,73,36,82,70,51,75,74,94,79,69,66,64,68,72,63,94,71,85,74,65,98,90,82,65,64,65,51,62,68,42,76,71,70,85,57,67,61,89,67,69,56,68,68,76,73,65,66,54,100,63,83,57,84,78,40,90,78,71,45,63,70,77,62,69,69,93,58,73,82,83,99,76,93,77,86,61,43,59,82,47,56,89,70,28,86,62,47,54,70,74,77,59,53,75,58,69,82,53,83,47,60,42,77,50,57,55,68,74,79,89,71,66,58,66,59,37,70,55,51,89,68,81,84,99,58,74,81,71,51,80,48,78,79,70,71,59,81,56,62,52,64,85,75,60,82,75,66,89,65,76,71,86,64,47,48,53,63,65,75,88,46,85,46,60,72,66,52,59,67,58,81,80,74,95,52,83,88,39,70,97,87,75,57,79,72,39,72,88,84,41,53,98,63,47,77,65,66,73,96,80,41,55,62,80,66,92,92,62,68,47,75,81,66,64,75,65,67,88,76,75,57,55,83,45,76,61,84,70,57,73,65,71,82,71,79,85,84,83,72,65,62,70,77,69,66,87,69,67,74,48,61,58,41,54,82,85,79,60,85,74,34,90,63,97,31,80,77,64,71,97,77,61,71,92,60,80,56,92,61,71,67,96,56,65,70,84,79,76,73,82,76,64,69,70,64,54,65,69,78,74,75,77,86,61,92,57,85,80,45,67,56,68,46,63,91,94,59,78,42,50,54,54,69,66,77,66,64,58,44,74,87,75,64,51,56,71,68,67,80,59,60,80,44,67,48,96,63,72,71,80,84,64,19,49,64,65,63,74,76,41,80,87,85,53,62,94,44,63,33,48,82,65,48,72,33,75,80,71,43,82,88,81,76,76,62,55,74,65,73,68,79,83,78,63,74,53,84,62,94,46,95,67,56,75,59,57,66,74,50,59,49,86,67,64,71,88,75,76,68,87,75,55,68,62,89,58,85,81,87,78,83,70,63,65,58,66,65,78,62,85,92,74,60,64,76,59,95,70,74,86,73,70,77,40,69,83,73,65,74,90,48,61,57,71,85,61,62,77,66,85,67,69,71,99,66,84,90,80,79,80,55,56,68,81,76,83,67,56,39,48,49,65,84,80,70,75,86,76,56,84,48,91,86,82,70,48,78,44,75,79,39,56,76,53,31,88,63,62,70,66,70,72,73,51,82,79,79,87,64,46,77,72,81,82,65,76,47,67,53,65,63,48,59,61,58,60,83,68,92,43,83,98,92,55,81,69,50,55,49,50,51,81,89,74,74,53,72,68,49,62,77,87,44,74,77,53,61,41,69,74,90,77,82,78,75,66,90,86,52,72,66,58,75,90,48,71,44,70,78,44,88,69,78,78,48,84,84,64,83,80,81,68]
Z = [72,78,94,90,70,56,67,89,54,60,87,61,63,57,74,51,52,76,56,83,80,62,85,80,61,74,53,65,70,69,92,62,73,64,63,76,61,72,81,74,69,54,68,66,54,77,87,61,55,47,80,58,50,63,85,69,66,75,77,79,84,59,70,87,63,52,95,31,86,95,65,43,90,63,66,77,75,93,58,68,52,64,57,60,42,61,75,87,52,67,79,95,41,44,100,96,46,82,68,70,50,78,68,74,61,44,74,63,83,60,85,40,72,57,64,93,62,59,44,57,72,92,99,69,61,81,85,80,57,73,57,73,65,48,67,68,61,86,79,58,52,74,69,58,73,85,80,60,34,89,75,58,78,41,49,60,70,75,86,87,63,79,87,56,78,57,53,62,99,62,66,72,67,65,65,92,52,70,84,50,67,62,74,90,61,53,65,81,59,45,61,51,47,52,76,57,69,61,62,63,47,66,85,78,67,86,69,66,55,72,21,42,62,81,75,52,86,45,55,81,64,67,56,58,65,71,92,54,14,43,72,70,78,80,79,60,89,67,67,80,68,77,70,86,63,62,75,90,71,52,71,84,95,61,98,81,88,57,45,82,41,82,85,83,82,36,50,75,79,76,63,89,54,76,87,63,81,81,63,93,70,74,69,63,81,87,34,65,100,77,57,87,58,39,93,46,49,46,77,63,59,63,25,41,55,91,40,77,48,96,44,67,67,53,83,80,73,70,85,55,76,84,76,38,95,50,65,56,90,72,54,66,70,65,56,74,65,79,62,70,91,63,78,70,94,82,66,74,58,69,82,63,95,62,56,59,89,70,63,95,81,88,85,83,56,82,80,80,100,83,59,53,79,32,69,70,70,39,82,61,48,80,69,95,75,79,67,60,74,71,52,95,67,77,71,67,100,83,83,75,60,74,50,62,71,36,75,73,73,91,52,69,57,82,63,74,53,67,62,71,72,75,71,48,96,68,91,53,86,81,41,93,85,76,47,61,71,74,71,67,66,91,52,73,78,82,96,74,91,77,87,67,35,64,74,43,46,83,67,22,83,53,49,55,75,74,72,54,45,85,59,69,74,45,78,47,57,48,79,40,56,57,69,72,76,90,83,62,47,61,59,45,65,56,52,79,72,75,80,100,58,73,90,70,48,79,39,76,82,63,71,51,72,64,63,60,60,80,74,65,78,80,63,81,57,67,73,90,69,38,43,51,66,68,84,78,42,88,59,55,69,67,44,60,73,58,78,84,69,93,54,86,85,39,65,95,90,75,55,79,82,38,72,94,81,35,51,100,66,41,71,58,62,71,97,84,31,52,58,80,57,88,86,59,67,41,74,80,67,57,77,49,64,83,70,67,58,46,91,41,70,67,84,68,52,75,66,77,83,71,83,74,85,82,78,54,65,65,72,72,62,83,63,59,74,49,62,54,45,54,82,84,79,57,81,72,34,92,64,95,33,80,70,64,76,96,79,58,64,93,59,83,57,91,59,76,71,94,55,74,76,78,73,87,74,87,71,58,60,75,69,53,62,74,73,60,80,75,82,67,94,56,77,72,55,72,50,72,48,68,84,100,57,77,42,46,52,51,66,62,78,69,53,60,48,76,95,76,67,49,53,65,70,65,86,55,64,71,43,66,40,93,61,59,66,75,76,65,17,47,62,61,66,79,70,27,76,86,82,51,63,87,45,65,31,52,74,63,43,74,39,76,76,72,42,83,84,77,73,72,63,66,61,66,68,66,79,82,87,59,75,46,80,57,91,48,99,68,63,77,59,57,58,84,46,58,46,85,71,68,75,94,76,78,60,82,80,55,70,56,90,60,80,83,85,78,86,67,63,73,55,69,62,73,65,78,87,70,59,61,72,63,92,73,73,84,78,61,65,43,59,84,74,59,74,82,47,54,59,74,83,61,55,79,65,88,56,61,75,96,59,76,99,79,82,75,56,53,67,78,74,81,71,59,41,44,53,56,86,74,73,80,85,74,56,80,55,86,89,77,71,41,76,49,68,78,41,57,80,51,26,79,67,60,73,63,77,79,68,53,88,81,77,89,67,45,79,73,79,80,63,74,48,62,59,68,62,48,55,60,58,61,83,73,85,42,78,92,91,58,78,67,43,48,44,50,48,78,87,81,77,50,75,72,53,61,70,86,46,68,79,50,66,46,65,72,83,74,78,77,66,68,83,84,49,75,66,53,72,85,44,76,46,70,77,45,90,64,77,80,41,83,85,62,79,78,72,63]
# Find the size of array.
n = len(X)
# print correlationCoefficient.
print ("Simple Corr Coef between X & Y:",'{0:.6f}'.format(correlationCoefficient(X, Y, n)))
print ("Simple Corr Coef between Y & Z:",'{0:.6f}'.format(correlationCoefficient(Y, Z, n)))
print ("Simple Corr Coef between X & Z:",'{0:.6f}'.format(correlationCoefficient(X, Z, n)))
def main():
correlationCoefficient(X, Y, n)
correlationCoefficientZ(X, Z, n)
correlationCoefficientYZ(Y, Z, n)
main()
#Zero order/simple correlation Coefficient
r_XY=correlationCoefficient(X, Y, n)
r_XZ=correlationCoefficientZ(X, Z, n)
r_YZ=correlationCoefficientYZ(Y, Z, n)
#print(r_XY)
#print(r_XZ)
#print(r_YZ)
#partial correlation coeficient --------------------------------------------------------------------
def PartialcorrelationCoefficientXY_Z(X, Z, n) :
xy_z = (float)(r_XY - r_XZ * r_YZ)/(float)(math.sqrt((1 - r_XZ*r_XZ)* (1 - r_YZ * r_YZ)))
return xy_z
print ("Partial Corr Coef between XY & Z:",'{0:.6f}'.format(PartialcorrelationCoefficientXY_Z(X, Z, n)))
XY_Z=PartialcorrelationCoefficientXY_Z(X, Z, n)
print(XY_Z)
def PartialcorrelationCoefficientXZ_Y(X, Y, n) :
xz_y = (float)(r_XZ - r_XY * r_YZ)/(float)(math.sqrt((1 - r_XY*r_XY)* (1 - r_YZ * r_YZ)))
return xz_y
print ("Partial Corr Coef between XZ & Y:",'{0:.6f}'.format(PartialcorrelationCoefficientXZ_Y(X, Y, n)))
XZ_Y=PartialcorrelationCoefficientXZ_Y(X, Y, n)
print(XZ_Y)
def PartialcorrelationCoefficientYZ_X(Y, X, n) :
yz_x = (float)(r_YZ - r_XY * r_XZ)/(float)(math.sqrt((1 - r_XY*r_XY)* (1 - r_XZ * r_XZ)))
return yz_x
print ("Partial Corr Coef between YZ & X:",'{0:.6f}'.format(PartialcorrelationCoefficientYZ_X(Y, X, n)))
YZ_X=PartialcorrelationCoefficientYZ_X(Y, X, n)
print(YZ_X)
# x-axis values
x = X
# y-axis values
y = Y
# plotting points as a scatter plot
plt.scatter(x, y, label= "XY", color= "R",
marker= "*", s=30)
# x-axis label
plt.xlabel('x - axis')
# frequency label
plt.ylabel('y - axis')
# plot title
plt.title('Correlation X-Y')
# showing legend
plt.legend()
# function to show the plot
plt.show()
x = X
# y-axis values
y = Z
# plotting points as a scatter plot
plt.scatter(x, y, label= "XZ", color= "G",
marker= "*", s=30)
# x-axis label
plt.xlabel('x - axis')
# frequency label
plt.ylabel('y - axis')
# plot title
plt.title('Correlation X-Z')
# showing legend
plt.legend()
# function to show the plot
plt.show()
x = Y
# y-axis values
y = Z
# plotting points as a scatter plot
plt.scatter(x, y, label= "YZ", color= "B",
marker= "*", s=30)
# x-axis label
plt.xlabel('x - axis')
# frequency label
plt.ylabel('y - axis')
# plot title
plt.title('Correlation Y-Z')
# showing legend
plt.legend()
# function to show the plot
plt.show()