Homework 0#
Due date: None. This is an ungraded practice homework. Try to finish it during the first week, because HW 1 assumes that everything here works.
Repository: finm-32800/hw0
Goes with: Lecture 0
HW 0 has two purposes. It gets your computer and your accounts ready for the rest of the quarter. And it takes you once through the cycle that every later homework repeats: clone a repository, install its environment, run it, edit the code until the tests pass, and push.
Part 1: Set up your computing environment#
Follow Getting Set Up to install the software and create the accounts. Apply for your WRDS account first, because approval takes several days and HW 1 requires it.
Part 2: Get connected to the course#
Fill out the GitHub username survey on Canvas. It asks for your name, your UChicago email, your GitHub username, and the URL of your GitHub profile. I use your answer to create your private copy of each graded homework, starting with HW 1, so do this as soon as you have a GitHub account. Afterward, GitHub emails you an invitation to the hw-finm-32800 organization. Accept it within seven days, after which it expires.
“Watch” the course website repository so that you are notified of new posts on the course discussion board. The board is attached to that repository, so this is the only place to subscribe. Click Watch at the top right, choose Custom, check Discussions, and click Apply — the default setting of “Participating and @mentions” does not notify you of new threads. Then confirm the repository is listed at watching.
To get these as email rather than only in your GitHub inbox, go to settings/notifications and, under Subscriptions → Watching, check Email.
Consider posting an introduction in the Introductions thread on the discussion board. Say that you’re looking for people to work with on the homework and on the final project, which is done in groups of four.
Skim the tour of the course and open one of the past final projects.
Part 3: Clone and run the HW 0 repository#
Make your own copy of finm-32800/hw0. On that page, click Fork. (Once you are enrolled, I will create a private copy of each graded homework for you. HW 0 is public, so a fork works.)
Clone your fork and install its environment, as described in Clone and Run.
From the base directory of the repository, run
doit. This downloads a small extract of monthly stock returns from CRSP into the_datafolder, then executes the notebooks and saves HTML copies of them in_output. Once your WRDS account is approved, try pulling the same data directly from WRDS instead: copy.env.exampleto.env, fill in yourWRDS_USERNAME, setNO_CACHE=True, and rundoitagain.Launch the dashboard with
streamlit run src/app.py. In the “Two assets” tab, drag the correlation slider toward -1. In the “Many assets” tab, shorten the estimation window and watch what happens to the tangency portfolio weights. Then check the “No short sales” box and compare the two frontiers.Read through the portfolio selection notebook and its appendix, which derives the formulas. The sources for both are in the repository’s
srcfolder.
Part 4: Make the tests pass#
Which files should I edit?
In order to complete the homework, you need to adjust the source files so that the unit tests pass. The unit tests are implemented in the files that start with test_. In HW 0, you will need to edit src/port_opt.py.
NOTE: You should not make any edits to the test files. In the graded homeworks, if an edit is made to a test file, you will be required to edit the history of your commits to remove any trace of the edits to these files.
Portfolio optimization#
The notebook computes the global minimum variance portfolio and the tangency portfolio inline. Move that logic into the two functions marked TODO in src/port_opt.py. Then, from the base directory of the project, run
pytest
The five tests in test_port_opt.py should now pass. They use small made-up inputs with known answers, so they do not need the data. (The tests in test_mean_variance.py pass from the start. They check the frontier code that I wrote.)
Push, and watch the tests run on GitHub#
Commit your changes and push them to your fork. Then open the Actions tab of your fork on GitHub. (The first time, GitHub asks you to enable workflows on a fork. Click the button to do so, then push again or press “Run workflow.”) The same tests that you ran on your laptop now run on a fresh machine in the cloud. A green check mark next to your commit means that they passed. In the graded homeworks, this is how your work is scored.
Additional Notes about the HW#
Why are we doing it this way?#
The point of structuring the assignment this way is to give you experience with unit testing, CI/CD, and the concept of test-driven development. These are concepts that you should learn to level-up your software development skills. These are real-world development concepts and getting experience with them should move you beyond the over-simplified approach that you might find in a typical university problem set.
We discussed unit tests and test-driven development in class a little. If you’d like more in-depth information, please watch these YouTube videos:
“Software Testing Explained in 100 Seconds” (This short video is written from the perspective of a web developer, but the same concepts apply to us.)
“Test-Driven Development In Python // The Power of Red-Green-Refactor” (This video discussing unit testing with Python and PyTest.)
How can I check if my tests are passing?#
From the command-line, in the base directory of the project, run pytest. This is a local test. In the graded homeworks, your grade will depend on whether the tests pass in GitHub Actions.
More information about Git and GitHub#
Also, if you’re looking for more instruction about how to use Git, here are two videos that I might recommend: