Onecanthenworkwithmaybe orevenonly principalcomponent forboththegermandaxindexandallstocksthatmakeuptheindex toderiveprincipalcomponents machinelearninglibrary importnumpyasnp importpandasaspd importpandas fromsklearn stocksthefollowinglistobjectcontainsthe symbolsforthestockscontainedinthegerman daxindex symbols toretrievestockdatawithpandas time data forsyminsymbols data cputimes
���������plt.grid(True)
���������plt.axhline(0,�color=‘k’,�ls=‘—’,�lw=2.0)���������plt.axvline(0,�color=‘k’,�ls=‘—’,�lw=2.0)���������plt.xlabel(‘expected�volatility’)
���������plt.ylabel(‘expected�return’)
���������plt.colorbar(label=‘Sharpe�ratio’)
Out[76]:�array([�0.684,��0.059,��0.257,�-0.���,��0.���])
Principal�Component�Analysis
The�example�of�this�section�illustrates�the�use�of�PCA�in�such�a�context.�We�retrieve�data for�both�the�German�DAX�index�and�all�stocks�that�make�up�the�index.�We�then�use�PCA to�derive�principal�components,�which�we�use�to�construct�what�we�call�a�pca_index.
First,�some�imports.�In�particular,�we�use�the�KernelPCA�function�of�the�scikit-learn machine�learning�library�(cf.�the�):
We�work�only�with�the�closing�values�of�each�data�set�that�we�retrieve�(for�details�on�how to�retrieve�stock�data�with�pandas,�see�Chapter�6):
In�[3]:�%%time
��������data�=�pd.DataFrame()
��������for�sym�in�symbols:
������������data[sym]�=�web.DataReader(sym,�data_source=‘yahoo’)[‘Close’]��������data�=�data.dropna()