Guest User

Untitled

a guest
Jan 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3.  
  4. class StockPrices:
  5. # param prices dict of string to list. A dictionary containing the tickers of the stocks, and each tickers daily prices.
  6. # returns list of strings. A list containing the tickers of the two most correlated stocks.
  7. @staticmethod
  8. def most_corr(prices):
  9.  
  10. return None
  11.  
  12. #For example, with the parameters below the function should return ['FB', 'MSFT'].
  13. prices = {
  14. 'GOOG' : [
  15. 742.66, 738.40, 738.22, 741.16,
  16. 739.98, 747.28, 746.22, 741.80,
  17. 745.33, 741.29, 742.83, 750.50
  18. ],
  19. 'FB' : [
  20. 108.40, 107.92, 109.64, 112.22,
  21. 109.57, 113.82, 114.03, 112.24,
  22. 114.68, 112.92, 113.28, 115.40
  23. ],
  24. 'MSFT' : [
  25. 55.40, 54.63, 54.98, 55.88,
  26. 54.12, 59.16, 58.14, 55.97,
  27. 61.20, 57.14, 56.62, 59.25
  28. ],
  29. 'AAPL' : [
  30. 106.00, 104.66, 104.87, 105.69,
  31. 104.22, 110.16, 109.84, 108.86,
  32. 110.14, 107.66, 108.08, 109.90
  33. ]
  34. }
  35.  
  36. df = pd.DataFrame.from_dict(prices)
  37. def get_redundant_pairs(df):
  38.  
  39. pairs_to_drop = set()
  40. cols = df.columns
  41. for i in range(0, df.shape[1]):
  42. for j in range(0, i+1):
  43. pairs_to_drop.add((cols[i], cols[j]))
  44. return pairs_to_drop
  45.  
  46. def get_top_abs_correlations(df, n=12):
  47. au_corr = df.corr().abs().unstack()
  48. labels_to_drop = get_redundant_pairs(df)
  49. au_corr = au_corr.drop(labels=labels_to_drop).sort_values(ascending=False)
  50. return au_corr[0:n]
  51.  
  52. print(get_top_abs_correlations(df, 1))
Add Comment
Please, Sign In to add comment