我一直在尝试为自己的金融课程学习python,因此我必须从相关矩阵中找出并分析前5对股票(值从高到低排序)。 现在我无法执行此操作。
输入:
from string import ascii_letters
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
from pandas_datareader import data as pdr
import fix_yahoo_finance as yf
from pprint import pprint
from scipy.stats import linregress
import seaborn as sns
Utilities = ["NEE","DUK","D","SO","EXC","AEP","SRE","PEG","XEL","ED","WEC","PPL","DTE","ES","FE","EIX","AWK","ETR","AEE","CMS","CNP","EVRG","NRG","AES","PNW"]
Financial = ["JPM","GS","BLK","PNC","BK","ICE","COF","AIG","AFL","AON","AXP","BAC","C","CB","CME","MET","MMC","MS","PGR","PRU","SPGI","SCHW","USB","WFC"]
Financial = yf.download(Financial, start="2012-2-5", end="2013-2-5",action='only')
Utilities = yf.download(Utilities, start="2012-2-5", end="2013-2-5",action='only')
Utilities = Utilities[['Adj Close']]
Financial = Financial[['Adj Close']]
#Geometric returns
geo_Utilities = Utilities.pct_change()
geo_Financial = Financial.pct_change()
#log returns
log_Utilities = np.log(1+geo_Utilities)
log_Financial = np.log(1+geo_Financial)
#Volatility
std_Utilities = log_Utilities.rolling(2).std()
std_Financial = log_Financial.rolling(2).std()
#Concat + correlation
utifin = pd.concat([log_Utilities,log_Financial],axis=1).corr()
#Order Matrix only for different stock row, columns
s = utifin.iloc[0:24, 24:49]