Guest User

Untitled

a guest
Apr 20th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. from numpy import corrcoef
  2. import numpy as np
  3. # (a)
  4. # Implement my_corrcoef and compare with numpy.corrcoef
  5. # use the same signature as numpy.corrcoef
  6. def my_corrcoef(x, y):
  7. x_mean = np.mean(x)
  8. y_mean = np.mean(y)
  9.  
  10. upper_sum = 0
  11.  
  12. for i in range(0, len(x)):
  13. upper_sum += (x[i] - x_mean) * (y[i] - y_mean)
  14.  
  15. lower_sum1 = 0
  16. for i in range(0, len(x)):
  17. lower_sum1 += (x[i] - x_mean) ** 2
  18.  
  19. lower_sum2 = 0
  20. for i in range(0, len(y)):
  21. lower_sum2 += (y[i] - y_mean) ** 2
  22.  
  23. lower_product = np.sqrt(lower_sum1) * np.sqrt(lower_sum2)
  24.  
  25. return upper_sum / lower_product
  26.  
  27. # (b)
  28. # Calculate the correlation coefficient for all pairs.
  29. coefficients = np.zeros(109)
  30.  
  31. for i in range(1, 109):
  32. xy = load_pair(i)
  33. coefficients[i] = my_corrcoef(xy[:,0], xy[:,1])
  34.  
  35. hist(coefficients, bins=50);
  36.  
  37. from hsic import hsic
  38. # (c)
  39. # Calculate the HSIC criterion for all pairs.
  40.  
  41. hsics = np.zeros(10)
  42.  
  43. for i in range(1, 9):
  44. xy = load_pair(i)
  45. #coefficients[i] = my_corrcoef(xy[:,0], xy[:,1])
  46. hsics[i] = hsic(xy[:,0], xy[:,1])[0]
  47.  
  48. hist(hsics, bins=50);
  49.  
  50. # (d)
  51. # Make a scatter plot where each point is a pairs dataset and the axes show my_corrcoef vs hsic.
  52.  
  53. scatter(coefficients[:10], hsics)
Add Comment
Please, Sign In to add comment