Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please follow the instructions carefully. Write all your code in a Code cell, and your explanations in a Markdown cell. Make sure that your code

Please follow the instructions carefully. Write all your code in a Code cell, and your explanations in a Markdown cell. Make sure that your code compiles correctly either by selecting a given cell and clicking the Run button, or by hitting shift+enter or shift+return.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Please Write in Python Please follow the instructions carefully. Write all your code in a Code cell, and your explanations in a Markdown cell. Make sure that your code compiles correctly either by selecting a given cell and clicking the Run button, or by hitting shift+enter or shift+return. 1. Import numpy, matplotlib.pyplot, and pandas 2. Load the data from the file named data_stock_returns.csv into a DatFrame called returns. The file data_stock_returns.csv contains daily returns of a number of stocks selected from the S&P 500 universe. The rows of the csv file represent the returns over a number of days, and the columns represent individual stocks labeled by their NYSE ticker symbol, e.g., Apple stock is labeled AAPL. 3.View the head of the returns DataFrame 4. View the tail of the returns DataFrame 5. How many stocks are in this DataFrame? 6. Type Markdown and LaTeX: a2a2 Over how many days are these stock returns reported? 7. Type Markdown and LaTeX: a2a2 Extract the returns of the Amazon stock only, which has a ticker symbol AMZN. Save it in a Series called amzn_returns. 8. Plot the Amazon stock returns extracted in the above cell. 9. Plot the cumulative sum of the Amazon stock returns using the method .cumsum() which acts directly on the amzn_returns Series. 10. # the module below will allow us to perform linear regression import statsmodels.api as sm The function lin_reg(x,y) given below performs ordinary least squares (OLS) linear regression using sm.OLS from the statsmodels.apimodule. The code enclosed in " '" is the docstring of the function lin_reg. x in the above function is a matrix that contains the regressors, and y represents the vector containing the dependent variable. Note that x might contain one vector or multiple vectors. In the case that x contains one vector xx, the regression gives: y=BO+B1xy=BO+B1x In the case that x contains multiple vectors x1,...,xkx1,...,xk, the regression becomes: y=BO+B1x1+...+Bkxky=BO+B1x1+---+Bkxk The BB's are the regression coefficients obtained using least squares. Note that sm.add_constant is used in the function below to make x look like the matrix AA we use in least squares, whose first column contains all ones. def lin_reg(x, y): oridinary linear regression using least-squares Parameters x: regressors (numpy array) y: dependent variable (numpy array) Returns betas: regression coefficients (pandas Series) residuals: regression residuals (numpy array) r_squared: correlation coefficient (float) x = sm.add_constant(x) model = sm.OLS(y, x).fit() coefficients = model.params residuals = model.resid r_squared = model.rsquared return coefficients, residuals, r_squared Let's now try to use the above function. Extract (as numpy array) the stock returns of: . Apple (ticker symbol AAPL) and call it aapl Intel (ticker symbol INTC) and call it into Microsoft (ticker symbol MSFT) and call it msft IBM (ticker symbol IBM) and call it ibm . Let y be the Apple stock returns, and x be the Intel stock returns. Use the lin_reg function defined above to find y=BO+B1xy=BO+B1x. 11. Plot the cumulative sum of the Apple returns prediction from least squares on top of the actual Apple returns. How well do the Intel stock returns describe the Apple stock returns? 12. Now, let y be the Apple stock returns, and x be the Intel, Microsoft, and IBM stock returns. Use the lin_reg function defined above to find y=B0+31x1+B2x2+33x3y=BO+B1x1+B2x2+B3x3, where x1x1 represents Intel returns, x2x2 represents Microsoft returns, and x3x3 represents IBM returns. 13. Plot the cumulative sum of the Apple returns prediction from least squares on top of the actual Apple returns. How well do the Intel stock returns describe the Apple stock returns? 14. The file SPY.csv contains the prices of SPDR S&P 500 ETF Trust. This Exchage Traded Fund (ETF) contains a collection of assets currently present in the S&P 500 index. Load SPY.csv into a DataFrame called spy_prices using the read_csv method in pandas. Make sure to make the 'Date' column to be your index column. To do that, read the docstring for read_csv. Once you have downloaded the file into the DataFrame, observe all the available prices and dates. Show the head of the DataFrame, and then answer the following questions: 1. Which prices are reported? 2. From which date to which date are these prices reported? 15. Type Markdown and LaTeX: a2a2 Retain only the Adjusted Close price in the spy_prices DataFrame. Call the resulting Series spy_adjclose. 16. Now, using the pct_change method in pandas, compute the returns on the Adjusted Close prices of SPY, and only retain the returns from '2019-01-01 to 2020-01-01. Call the Series obtained spy_returns. 17. Perform SVD on returns data that contain assets from the S&P 500. Retain the left singular vector corresponding to the largest singular value and call is u_sigmal. 18. u_sigmal is thought to track the market. To test that, we will perform a regression of spy_returns against this first left singular vector by letting y=spy_returns and x=u_sigmal and computing y=BO+B1xy=O+B1x using least squares regression. 19.Plot the cumulative sum of the result from the regression on top of the cumulative sum of spy_returns. What do you notice? Please Write in Python Please follow the instructions carefully. Write all your code in a Code cell, and your explanations in a Markdown cell. Make sure that your code compiles correctly either by selecting a given cell and clicking the Run button, or by hitting shift+enter or shift+return. 1. Import numpy, matplotlib.pyplot, and pandas 2. Load the data from the file named data_stock_returns.csv into a DatFrame called returns. The file data_stock_returns.csv contains daily returns of a number of stocks selected from the S&P 500 universe. The rows of the csv file represent the returns over a number of days, and the columns represent individual stocks labeled by their NYSE ticker symbol, e.g., Apple stock is labeled AAPL. 3.View the head of the returns DataFrame 4. View the tail of the returns DataFrame 5. How many stocks are in this DataFrame? 6. Type Markdown and LaTeX: a2a2 Over how many days are these stock returns reported? 7. Type Markdown and LaTeX: a2a2 Extract the returns of the Amazon stock only, which has a ticker symbol AMZN. Save it in a Series called amzn_returns. 8. Plot the Amazon stock returns extracted in the above cell. 9. Plot the cumulative sum of the Amazon stock returns using the method .cumsum() which acts directly on the amzn_returns Series. 10. # the module below will allow us to perform linear regression import statsmodels.api as sm The function lin_reg(x,y) given below performs ordinary least squares (OLS) linear regression using sm.OLS from the statsmodels.apimodule. The code enclosed in " '" is the docstring of the function lin_reg. x in the above function is a matrix that contains the regressors, and y represents the vector containing the dependent variable. Note that x might contain one vector or multiple vectors. In the case that x contains one vector xx, the regression gives: y=BO+B1xy=BO+B1x In the case that x contains multiple vectors x1,...,xkx1,...,xk, the regression becomes: y=BO+B1x1+...+Bkxky=BO+B1x1+---+Bkxk The BB's are the regression coefficients obtained using least squares. Note that sm.add_constant is used in the function below to make x look like the matrix AA we use in least squares, whose first column contains all ones. def lin_reg(x, y): oridinary linear regression using least-squares Parameters x: regressors (numpy array) y: dependent variable (numpy array) Returns betas: regression coefficients (pandas Series) residuals: regression residuals (numpy array) r_squared: correlation coefficient (float) x = sm.add_constant(x) model = sm.OLS(y, x).fit() coefficients = model.params residuals = model.resid r_squared = model.rsquared return coefficients, residuals, r_squared Let's now try to use the above function. Extract (as numpy array) the stock returns of: . Apple (ticker symbol AAPL) and call it aapl Intel (ticker symbol INTC) and call it into Microsoft (ticker symbol MSFT) and call it msft IBM (ticker symbol IBM) and call it ibm . Let y be the Apple stock returns, and x be the Intel stock returns. Use the lin_reg function defined above to find y=BO+B1xy=BO+B1x. 11. Plot the cumulative sum of the Apple returns prediction from least squares on top of the actual Apple returns. How well do the Intel stock returns describe the Apple stock returns? 12. Now, let y be the Apple stock returns, and x be the Intel, Microsoft, and IBM stock returns. Use the lin_reg function defined above to find y=B0+31x1+B2x2+33x3y=BO+B1x1+B2x2+B3x3, where x1x1 represents Intel returns, x2x2 represents Microsoft returns, and x3x3 represents IBM returns. 13. Plot the cumulative sum of the Apple returns prediction from least squares on top of the actual Apple returns. How well do the Intel stock returns describe the Apple stock returns? 14. The file SPY.csv contains the prices of SPDR S&P 500 ETF Trust. This Exchage Traded Fund (ETF) contains a collection of assets currently present in the S&P 500 index. Load SPY.csv into a DataFrame called spy_prices using the read_csv method in pandas. Make sure to make the 'Date' column to be your index column. To do that, read the docstring for read_csv. Once you have downloaded the file into the DataFrame, observe all the available prices and dates. Show the head of the DataFrame, and then answer the following questions: 1. Which prices are reported? 2. From which date to which date are these prices reported? 15. Type Markdown and LaTeX: a2a2 Retain only the Adjusted Close price in the spy_prices DataFrame. Call the resulting Series spy_adjclose. 16. Now, using the pct_change method in pandas, compute the returns on the Adjusted Close prices of SPY, and only retain the returns from '2019-01-01 to 2020-01-01. Call the Series obtained spy_returns. 17. Perform SVD on returns data that contain assets from the S&P 500. Retain the left singular vector corresponding to the largest singular value and call is u_sigmal. 18. u_sigmal is thought to track the market. To test that, we will perform a regression of spy_returns against this first left singular vector by letting y=spy_returns and x=u_sigmal and computing y=BO+B1xy=O+B1x using least squares regression. 19.Plot the cumulative sum of the result from the regression on top of the cumulative sum of spy_returns. What do you notice

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial And Managerial Accounting

Authors: Carl S. Warren, James M. Reeve, Jonathan Duchac

14th Edition

1337119202, 978-1337119207

Students also viewed these Accounting questions