【发布时间】:2018-02-20 09:51:50
【问题描述】:
我正在研究一个包含股票收盘价的数据集。
'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
]
这些是过去 12 天的收盘价。我需要确定给定公司的哪对股票的每日收盘价百分比变化的相关性最高,并将它们作为数组返回。
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):
return
#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))
我已经完成了 numpy 相关函数,但是如何使用该精确函数来确定以下两个向量中的哪一个具有最大相关性?
【问题讨论】:
标签: python python-3.x numpy vector correlation