Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

ISE 529 Predictive Analytics

Homework 2

Submit on Sep 13 by 4 p.m. PST

The file contituents.csv, on blackboard, has the name, ticker symbol, and sector of companies that make up the S&P 500 stock index. Read this file into a pandas DataFrame called dfnames.

The file prices.csv has daily Adjusted Close prices of these stocks. Read this file into a pandas DataFrame called dfprices. Then use dfprices.set_index(’Date’,inplace=True) to let the column Date be the index of dfprices. For this homework the library pandas_datareader is not needed.

Collect a random sample of 6 companies using df=dfnames.sample(n=6,random_state=12)

1. (10 pts) Display the names, symbols and Sectors of these 6 companies.

2. (10 pts) Plot their stock prices over time. Use the following steps.

❼ create a list with the companies symbols using

Symbol = df[’Symbol’]

list3 = list(Symbol)

❼ Use the list to create a dataframe with the prices of these companies

dfprices8 = dfprices[list3]

dfprices8.index = pd.to_datetime(dfprices8.index)

❼ Make the plot placing the legend out of the plotting box using

dfprices8.plot(grid = True)

plt.legend(bbox_to_anchor = (1,1));

3. (10 pts) Report the names of the 2 most profitable stocks (those having the largest price increase)

df6 = dfprices8.iloc[-1]/dfprices8.iloc[0]

df6.sort_values(ascending = False)

4. (20 pts) Which stock was the most risky (with the largest std deviation in net returns)? Use

net_returns = dfprices / dfprices.shift(1) - 1

net_returns8 = net_returns[list3]

net_returns8.std()

Report the company name, symbol, and sector.

5. (10 pts) For this stock, create a single chart with 2 plots. One showing the time-evolution of the net returns, and another showing the histogram of the net returns. What can you see from this chart?

6. (20 pts) Plot the cumulative gross returns of these six stocks over time. Use the following

gross_returns8 = net_returns8 + 1

cum_gross_returns8 = gross_returns8.cumprod()

cum_gross_returns8.plot(figsize=(12,6),grid=True)

Make sure the legend does not overlap the plot.

Why would you say that this plot agrees with the answer of question 3?

7. (10 pts) Display a dataframe showing the correlation matrix of these 6 stocks.

8. (10 pts) Report the names of the two companies with the largest correlation of net returns. Make a scatterplot of the net returns of these two companies.

Submit your report with your name and USC ID as a pdf file online (no screen captures).