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

CompFin_HW1_Fall2023

October 25, 2023

1 Computational Finance Fall 2023 Homework 1

1.1 Problem 1

In this problem, we will get familiar with Python, Jupyter, and your filesystem, i.e., the way you organize your files and folders on your computer.

You will import a dataset and make some plots.  Don’t worry – I know we haven’t yet discussed working with data or making plots.  I will give lots of guidance here.  The goal is to get you familiar with the tools we will be using throughout the course.

1.1.1 Part A: Setting Up Your Workspace

It’s possible to do many things on a computer these days without knowing exactly where your files are stored, e.g., through cloud services.  But for coding, it’s important to know how to navigate a filesystem.

1.  Create a folder on your computer where you will store everything related to this class.  You can call it CompFin (or anything else you like, as long as it’s meaningful to you). Where you create this folder is up to you, but here are my suggestions:

•  Windows:  C:\Users\[your username]\Documents\CompFin

•  Mac:  /Users/[your username]/Documents/CompFin

On Windows, you can use Explorer to navigate and create folders.  On Mac, you can use Finder. You can also use the command line (Command Prompt on Windows, or Terminal on Mac), if you prefer.

2.  Create a subfolder called hw1 to store materials related to this homework assignment.  hw1 should be inside the CompFin folder.

3.  Open Jupyter Notebook (or JupyterLab, if you prefer) and navigate to the hw1 folder you just created. Create a new notebook in this folder called hw1_question1.ipynb.  In the notebook, create a new coding cell and type:

import os

os.getcwd()

Using your favorite search engine or ChatGPT (but paraphrase in your own words!), explain what this code does and what the output means. Write your answer as a text/markdown cell below the code cell in the same notebook.

If you use Windows, do you notice anything strange about the output?  See if you can figure out why this is happening.

1.1.2 Part B: Importing Data

We  will  plot  inflation  in  the  U.S.  over  time  as  measured  by  the  annual  percent  change  in  the Consumer Price Index (CPI), which is published monthly by the Bureau of Labor Statistics (BLS). You can obtain the data from the FRED database maintained by the Federal Reserve Bank of St. Louis. Here is the direct link:

https://fred.stlouisfed.org/graph/?g=1auWG.

1.  Click on the “Download” button on this page and save the CSV file in the hw1 folder you created above. If the file is automatically saved in your Downloads folder, you can move it to the hw1 folder using Explorer or Finder. If you don’t rename it (which is fine), the data file will be called CPIAUCSL.csv.

2.  In the same notebook you created above, import the pandas package and use its read_csv function to import the data from the CSV file you just downloaded.  Assign the data to a variable called inflation. Use the documentation for read_csv to figure out how to do this. You can find the documentation here.

3.  Take a look at the first few rows of the data using the head method.  What does each row represent? What do the columns represent? Write your answer as a text/markdown cell below the code cell in the same notebook.

1.1.3 Part C: Plotting Data

1.  Create a line plot of the inflation data.  You can do this in several different ways, but the simplest is

inflation.plot(x='DATE', y='CPIAUCSL_PC1')

where DATE and CPIAUCSL_PC1 are the two column names in the data.   Plot the data in a new cell in the same notebook as above. Compare your plot with the plot you saw on FRED’s website from which you downloaded the data. In what ways do they look the same? In what ways do they look different?  Which plot do you think is prettier?  More informative?  Write your answer as a text/markdown cell below the code cell in the same notebook.

Use the documentation for the plot method available here to modify your code to add a y-axis label and a legend entry for your plot that matches FRED.

Congratulations! You’re done with Question 1. Export the notebook to HTML so you can submit it to Canvas. You can do this by clicking on File > Export Notebook As… > Export Notebook to HTML.

Reminder:  you must submit your work as an HTML file.  If you submit a notebook file, you will receive a zero. Do not just rename the file to give it an HTML extension.

1.2 Problem 2

You decide to create a Mortgage Real Estate Investment Trust, i.e. Mortgage REIT. Mortgage REITs are companies that  invest in mortgage-backed securities (MBS) using mostly borrowed funds (i.e., using a lot of leverage).

You decide to start your Mortgage REIT with $10 million in equity. You will borrow $90 million by issuing a 1-year bond with an annual coupon of 3%, and use the total $100 million to buy MBS. To make things simple, let’s represent the MBS portfolio as a 10-year zero-coupon bond that is trading at a yield of 5%.

1.2.1 Part A: Set Up A New Notebook

Create a new notebook in the hw1 folder and call it hw1_question2.ipynb. Define variables to represent the quantities defined in the problem above. Calculate answers to the questions below using Python code. Write your answers as text/markdown cells below the code cells in the same notebook. Since this is your first Python homework, you can use a calculator or Excel to check or plan your answer. But you should still write the code in Python.

1.2.2   Part B: Calculate the 1-year Return on Equity  (if rates don’t change)

1.  Use the future value formula to calculate the face value of the MBS you can buy with $100 million. Is it higher or lower than the price you paid? Why does this make sense?

2.  If interest rates don’t change, what will be the value (price) of the MBS portfolio in 1 year? Is it higher or lower than $100 million? Why?

3.  How much do you have to repay at the end of the 1 year on the $90 million bond that you issued?

4.  Suppose you sell the MBS at the price you calculated in (2) and repay the amount owed you calculated in (3). How much equity do you have left?

5.  What is your return on equity?  To calculate the ROE, divide the answer to (4) by the amount of equity you started with and subtract 1. How does the ROE compare to the yield on the MBS portfolio? Why does this make sense?

1.2.3   Part C: Calculate the 1-year Return on Equity for alternative rate scenarios

Changes in interest rates can substantially affect the value of your MBS portfolio and your return on equity. Let’s see how this works.

1.  Suppose MBS yields fall by 1% over the next year. So in 1 year, the yield on the MBS portfolio is 4%. What is the value of the MBS portfolio in 1 year?  What is your return on equity?

2.  We will try this again for a few more interest rate values.  To avoid duplicating your code over and over again, write a function that takes the yield on the MBS portfolio after 1 year as an argument and returns your ROE. Use this function to calculate the ROE for the following scenarios (hint: use a for loop). For each scenario, print an output message that says “Yield: XX% –> ROE: YY%.” Format and round the numbers so that the output looks nice.

•  MBS yield in 1 year:  3%

•  MBS yield in 1 year: 4%

•  MBS yield in 1 year:  5%

•  MBS yield in 1 year:  6%

•  MBS yield in 1 year:  7%

3.  How does your ROE change as the MBS yield changes? Why does this make sense?

4.  Do you ever get ROE that’s less than -100%? If you do, what does that mean econonomically? When companies have limited liability, is this possible?

5.  Modify your  function to  print a message that  says  “Yields  of XX%  lead to  bankruptcy!” (where XX is automatically replaced with value of the yield passed into the function) if the  ROE is less than -100%.