
Correlation
import math
SPY = [170.66, 170.95, 170.70, 169.73, 169.18, 169.80, 169.31, 169.11, 169.61, 168.74, 166.38, 165.83]
JPM = [56.54, 56.40, 56.10, 55.49, 55.30, 54.83, 54.52, 54.09, 54.29, 54.15, 53.29, 51.83]
SPY2 = []
JPM2 = []
SPY_JPM = []
digit1 = 0
digit2 = 0
digit3 = 0
digit4 = 0
digit5 = 0
digit6 = 0
digit7 = 0
#1. First, every period needs to be squared for both securities.
for x in range(0, len(SPY), 1):
digit = SPY[x]
digit = digit * digit
#print(digit)
SPY2.append(digit)
for x in range(0, len(JPM), 1):
digit = JPM[x]
digit = digit * digit
#print(digit)
JPM2.append(digit)
#2. Multiply the each period value of SPY by each period of JPM.
for x in range(0, len(JPM), 1):
digit = JPM[x] * SPY[x]
#print(digit)
SPY_JPM.append(digit)
#3. Find the Average Value for each column.
SPY_AVG = sum(SPY)/len(SPY)
#print(SPY_AVG)
JPM_AVG = sum(JPM)/len(JPM)
#print(JPM_AVG)
SPY2_AVG = sum(SPY2)/len(SPY2)
#print(SPY2_AVG)
JPM2_AVG = sum(JPM2)/len(JPM2)
#print(JPM2_AVG)
SPY_JPM_AVG = sum(SPY_JPM)/len(SPY_JPM)
#print(SPY_JPM_AVG)
#Variance
SPY_Variance = SPY2_AVG - (SPY_AVG * SPY_AVG)
#print(SPY_Variance)
JPM_Variance = JPM2_AVG - (JPM_AVG * JPM_AVG)
#print(JPM_Variance)
#Covariance
covariance = SPY_JPM_AVG - (SPY_AVG * JPM_AVG)
#print(covariance)
#CorrelationCoefficient
correlationCoefficient = covariance/math.sqrt(SPY_Variance * JPM_Variance)
print(correlationCoefficient)