FIN 683 / Fall 2018 Python Extra Credit Assignment 1
Extra credit: Up to 6 points will be added to your exam score.
Using the PyPortfolioOpt library, Jupyter notebook, and the data set given in the Canvas instructions to this assignment, perform the set of tasks below. Submit a pdf file of your code and your outputs to Canvas.
You may find the pandas documention useful (https://pandas.pydata.org/pandas-docs/ version/0.23.4/api.html#api-dataframe-stats. The PyPortfolioOpt library is avail- able at https://pypi.org/project/PyPortfolioOpt/. The GitHub page for this library is https://github.com/robertmartin8/PyPortfolioOpt.
1. Read the data from the .csv file into a dataframe.
2. Assign the mean monthly returns multiplied by 12 to a new variable, called mu.
3. Assign the monthly covariance matrix of returns multiplied by 12 to a new variable called S.
4. Contrain the sum of the portfolio weights to be one and each weight to be −1.0 ≤ wi ≤ 1.0.
5. You will work with three sets of assets as your investment opportunities.
Case 1: The investable assets are the 4 ETFs below. [Note that you must set the riskless rate to zero when using the functions in this library.]
i. (SPYG) S&P 500 Growth
ii. (SPYV) S&P 500 Value
iii. (SLYG) S&P 600 Growth
iv. (SLYV) S&P 600 Value
Case 2: The investable assets are the 9 S&P sector ETFs given in the data file.
• XLB, XLV, XLP, XLY, XLF, XLI, XLE, XLK, XLU
Case 3: The investable assets are the 9 S&P sector ETFs given in the data file plus a riskless rate of 4%.
• XLB, XLV, XLP, XLY, XLF, XLI, XLE, XLK, XLU, and rf
6. For each case, identify the mean and standard deviation of return for the optimal port- folio that achieves an expected return of 6%. Print the weights in each case’s optimal portfolio.
7. For each case, identify the mean and standard deviation of return for the optimal port- folio that achieves an expected return of 10.5%. Print the weights in each case’s optimal portfolio.
8. For each case, use the plot frontier function to produce plots that include:
https://pandas.pydata.org/pandas-docs/version/0.23.4/api.html#api-dataframe-stats
https://pandas.pydata.org/pandas-docs/version/0.23.4/api.html#api-dataframe-stats
https://pypi.org/project/PyPortfolioOpt/
https://github.com/robertmartin8/PyPortfolioOpt
FIN 683 / Fall 2018 Python Extra Credit Assignment 2
• the frontier of the risky opportunity set • 100 random portfolios of the risky basis assets • the basis assets used in each case • for case 3, the tangency line from rf to the risky opportunity set
Note that the text file “Template-plotting-functions.txt” provides a template of code to achieve these outcomes.
9. Adjust your plot for case 1 to remove the dot marking the “Tangency Portfolio”. You may find this page of the matplotlib documentation useful: https://matplotlib.org/ api/_as_gen/matplotlib.axes.Axes.plot.html.
10. Adjust the previous plot so that the random portfolios are plotted with magenta circles instead of blue crosses.
https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.plot.html
https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.plot.html