Skip to content

Instantly share code, notes, and snippets.

@UMU18
Last active January 23, 2019 05:40
Show Gist options
  • Save UMU18/4f908e3fc88cfaeb72d858daa6a8b5dc to your computer and use it in GitHub Desktop.
Save UMU18/4f908e3fc88cfaeb72d858daa6a8b5dc to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
class StockPrices:
# param prices dict of string to list. A dictionary containing the tickers of the stocks, and each tickers daily prices.
# returns list of strings. A list containing the tickers of the two most correlated stocks.
@staticmethod
def most_corr(prices):
data=pd.DataFrame.from_dict(prices)
au_corr=data.corr()
listku=au_corr.values.flatten()
listku=np.unique(listku)
#dengan cara ini juga bisa sih
#arrayidx=[]
#arrayvalues=[]
#for idx, values in au_corr.stack().iteritems():
# arrayidx.append(idx)
# arrayvalues.append(values)
#for i in range(len(arrayvalues)):
# if (arrayvalues[i]==sorted(listku)[-2]):
# indices=arrayidx[i]
indices = np.where(au_corr > sorted(listku)[-3])
indices = [(au_corr.index[x], au_corr.columns[y]) for x, y in zip(*indices) if x != y and x < y]
return indices
#For example, with the parameters below the function should return ['FB', 'MSFT'].
prices = {
'GOOG' : [
742.66, 738.40, 738.22, 741.16,
739.98, 747.28, 746.22, 741.80,
745.33, 741.29, 742.83, 750.50
],
'FB' : [
108.40, 107.92, 109.64, 112.22,
109.57, 113.82, 114.03, 112.24,
114.68, 112.92, 113.28, 115.40
],
'MSFT' : [
55.40, 54.63, 54.98, 55.88,
54.12, 59.16, 58.14, 55.97,
61.20, 57.14, 56.62, 59.25
],
'AAPL' : [
106.00, 104.66, 104.87, 105.69,
104.22, 110.16, 109.84, 108.86,
110.14, 107.66, 108.08, 109.90
]
}
print(StockPrices.most_corr(prices))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment