Week 2 Due Fri, midnight (Week 2)

PC Lab 1 — Applied Portfolio Theory

Your first week at an old-school asset manager: prove that Markowitz portfolio theory earns its keep by building — and stress-testing — a tangency portfolio on real stock data.

Week 2 materials
Solution
Published after the deadline

The setting

It’s Monday morning, your first week on the job. You’ve just been hired by Meridian Asset Management, a large, proudly old-school firm — mahogany desks, decades of relationships, and a healthy suspicion of anyone who says “machine learning” in a meeting. Your team manages a regulated mutual fund.

The fund manager stops by your desk. She has heard that you’re one of the new “big data people”, and she has a test for you:

“Everyone keeps telling me this Markowitz stuff is table stakes. Fine. Here’s a basket of stocks we follow. Show me the optimal portfolio — the weights I should hold, and why. Our broker charges us no transaction fees, so no excuses. And show your work: I want to see the data, not just the answer.”

Your goals for the week: manipulate and visualize stock market data, build portfolios and compute their basic statistics, draw the efficient frontier and find the best (i.e., tangency) portfolio — and then ask the uncomfortable question: does the theory actually work out of sample? Can you beat the market?

Background

Everything you need is in this week’s lecture: Portfolio Theory — mean-variance optimization, diversification, the efficient frontier, the Sharpe ratio, and the tangency portfolio. If any of those feel shaky, review the slides before you start coding.

The data

One CSV, daily data starting 2012-01-12 (≈ 2,160 trading days):

Column What it is
Date Trading day
AAPL, BA, T, MGM, AMZN, IBM, TSLA, GOOG Daily prices of 8 large-cap US stocks (Apple, Boeing, AT&T, MGM Resorts, Amazon, IBM, Tesla, Alphabet)
sp500 S&P 500 index level — your market benchmark

Download it with the button above and load it in a Jupyter notebook with pandas.

Your assignment

Work through the five parts in order. All five parts are required; bullets marked Optional are extras. Part 5 is where the case gets really interesting. (Numbers in parentheses map to the tasks in the assignment slides.)

Part 1 — Know your sample (Task 1)

Before touching any model, convince the fund manager you understand the data:

  • Import the data, sort it by date, and print the number of stocks.
  • Check for null values.
  • What is the average market value of the S&P 500 over the sample?
  • Which stock or index has the minimum dispersion in dollar value?
  • What is the maximum Amazon price over the sample period?

Part 2 — See the data (Tasks 2–3)

The manager wants pictures, not tables:

  • Write a function that plots the entire dataframe (it takes a dataframe as input and returns nothing).
  • Write a function that normalizes prices by their initial value (every series divided by its price on 2012-01-12), and plot the normalized data. Now the stocks are comparable — who really grew the most?

Part 3 — Returns and co-movement (Task 5)

Prices are for charts; portfolios are built on returns:

  • Write a function that computes daily returns for all stocks (loop through each stock, and through each row belonging to it), and plot them.
  • Compute the correlation matrix of daily returns and plot it as a heatmap.
  • Which are the top 2 stocks most positively correlated with the S&P 500?
  • What is the correlation between Amazon and Boeing? Between MGM and Boeing? Comment on both — what economic story explains the numbers?
  • Plot the histograms of daily returns and comment.
  • Optional: build fancy interactive histograms with Plotly’s figure factory module.

Part 4 — Build the fund’s portfolio (Task 6)

Now the question she actually asked. Write a function that:

  • Simulates 10,000 portfolios with random weights over the 8 stocks;
  • Computes the return, variance, and Sharpe ratio of each;
  • Returns the maximum Sharpe ratio and its weights — your candidate tangency portfolio.

Then answer, with charts:

  • What are the weights of your tangency portfolio?
  • What would its performance over the sample period have been? Plot it!
  • Plot the risk–return scatter of all 10,000 portfolios, colored by Sharpe ratio.
  • Optional: find, by minimizing variance at each level of return, the weights that trace the efficient frontier — and plot it on top of your scatter.

Part 5 — Does it survive reality? (Task 7)

The manager’s parting shot: “Optimal on paper is easy. Would it have made me money?”

  • Re-run Part 4 using only 2012–2016 and find the optimal weights for that period.
  • Simulate that portfolio on 2016-onward. How does it perform? Plot the expected (in-sample) vs. the realized (out-of-sample) return and volatility.
  • Optional: every year, re-balance the portfolio using only the previous year’s data (i.e., build a new Markowitz portfolio every year). What is your overall performance? Comment.

Deliverables

Grading

Your grade depends on:

  1. Submitting before the deadline;
  2. The quality of your code — comments, readability, use of functions;
  3. The structure of your notebook — well organized, explaining what you are doing and why;
  4. Your ability to complete the tasks.

Model solution

A complete model solution — code, output, charts, and commentary — will be published on this page after the submission deadline.

Stuck, or curious about something here? Ask on the course forum →