Question: I've been trying to find the correlation between the two lists using the exactly code and function below. I can get it to run. #
I've been trying to find the correlation between the two lists using the exactly code and function below. I can get it to run.
# Quarterly change from a year earlier in spending during visits to the UK (ONS)
spending = [3.6,5.7,-4.8,5.7,4.1,4.1,8.7,8.3,14.1,9.5,22.5,-1.9,2.3,-3,-16.7,-1.8,-7.5,-0.6,9.4,26.5,-9.6]
# Quarterly change from a year earlier in visits to the UK from overseas residents (O
visits = [6.9,1.6,2,7.4,8.8,1.5,3.6,13.1,7.9,8.6,9.2,-5.8,-3.4,-4.5,-3,3.8,-2.5,-1.5,2.8,6.4,-16.1]
def corr_coef(list_x, list_y): """ Return correlation between values in list_x and list_y.
Lists must be of equal length. """ x_bar = mean(list_x) y_bar = mean(list_y) sxy = 0 sxx = 0 syy = 0 for index in range(len(list_x)): x = list_x[index] y = list_y[index] sxy = sxy + (x - x_bar) * (y - y_bar) sxx = sxx + (x - x_bar) * (x - x_bar) syy = syy + (y - y_bar) * (y - y_bar) return sxy / math.sqrt(sxx * syy)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
