Evidence_v2, Financial Modeling and Programming

Author

Alberto Dorantes, Ph.D.

Published

November 30, 2026

Abstract
This is the in-class evidence (practical exam) of the course. Follow the directions below.

1 GENERAL DIRECTIONS

Work in Google Colab. You can use any material (your workshops, notes) and you can use Gemini or another LLM to generate code.

  • For each code chunk you MUST EXPLAIN WITH YOUR OWN WORDS WHAT THE CODE DOES AND WHY YOU RUN IT. If an LLM writes explanations, delete them and write your OWN.

If you used a prompt, paste it in quotes.

  • ONLY KEEP THE PYTHON CODE YOU NEED FOR THIS EVIDENCE. Extra code can be penalized.
  • Submit your .ipynb file in Canvas (File → Download → Download .ipynb).

You are a portfolio manager on March 31, 2024. You will select stocks with a fundamental filter (screen) and the market model, build an optimal portfolio, and test it with data after your decision date.

2 DATA

Run the following code to import the 4 datasets:

import os, io, requests
import numpy as np
import pandas as pd

def import_csv(file, base_url="https://www.apradie.com/datos/", **kwargs):
    """Imports a csv file from base_url; downloads it only once and keeps a local copy."""
    if not os.path.exists(file):
        headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
                                 "(KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"}
        response = requests.get(base_url + file, headers=headers)
        response.raise_for_status()
        with open(file, "wb") as f:
            f.write(response.content)
    return pd.read_csv(file, **kwargs)

uspanel = import_csv("findata_s2.csv", encoding="utf-8-sig")
usfirms = import_csv("usfirms_s2.csv", encoding="utf-8-sig")
prices = import_csv("fz2024_prices_monthly.csv", index_col="date", parse_dates=True)
rf = import_csv("fz2024_rf_monthly.csv", index_col="date", parse_dates=True)
  • uspanel: quarterly financial data (2000q1–2024q2) of a sample of 104 S&P 500 firms. Same columns as the dataset of Workshop 1. Income-statement variables are fiscal Year-to-Date amounts; fiscalmonth identifies the fiscal quarter (3, 6, 9, 12). The annual report of a fiscal year is the row with fiscalmonth = 12; blanks and other values are not annual reports.
  • usfirms: catalog of the firms of the sample (same columns as in Workshop 1). It is only for reference: you do not need it to answer any part.
  • prices: month-end adjusted prices of S&P 500 firms and SPY (Workshop 2).
  • rf: monthly risk-free rate (rf, in decimals) and the annual T-Bill rate (tb3ms, in %).

Remember: ebit = revenue - cogs - sgae - depreciation, and netincome = ebit + otherincome + extraordinaryitems - finexp - incometax.

3 PART 1 (20 pts) – Fundamental screen

Using uspanel, calculate the annual Operating Return on Assets of each firm:

OperatingROA=\frac{EBIT_{fiscal\ year}}{TotalAssets_{end\ of\ previous\ fiscal\ year}}

Use the last annual report of each firm with a fiscal year that ended between 2023q1 and 2023q4.

Select the 30 firms with the highest Operating ROA.

4 PART 2 (30 pts) – Market model

For each of the 30 firms, estimate the market model (CAPM) with monthly simple excess returns (stock return minus rf, and SPY return minus rf) from April 2021 to March 2024.

Show a table with alpha, beta, their p-values and the p-value of the test \beta=1. Do the following screening (filter):

  • Select the 20 firms with the highest alpha.

  • Sort these 20 firms by the p-value of alpha, from smallest to largest, and select the first 10 firms (the most statistically significant alphas), even if some of them are not significant at 5%.

Interpret the alpha (including its statistical significance) and the beta of the firm with the smallest alpha p-value in your final list of 10 firms.

5 PART 3 (10 pts) – Market risk filter

From the 10 firms of Part 2, select the 5 stocks with the lowest market beta (the slope coefficient of the capm model, beta).

6 PART 4 (30 pts) – Optimal portfolio

With the 5 stocks of Part 3 and the window April 2021 – March 2024, calculate the long-only tangency (maximum Sharpe ratio) portfolio (no short sales). Use an annual risk-free rate of 1% for this optimization. Use the following inputs:

  • Annual expected returns e^{12\bar r}-1, where \bar r is the mean monthly cc return of each stock (raw returns, not excess returns).
  • The annual covariance matrix of monthly cc returns (12 times the monthly covariance matrix).

Report and explain the weights (including how concentrated the portfolio is), the expected annual return and the expected annual volatility.

7 PART 5 (10 pts) – Out-of-sample backtest

Calculate the monthly simple returns of your Optimal portfolio (buy and hold) from April 2024 to the last month available in prices, and compare it with the equally weighted portfolio (buy and hold) of the same 5 stocks and with SPY. For each one report:

  • Holding-period return (HPR).
  • Annualized return: (1+HPR)^{12/n}-1, where n is the number of months of the test period.
  • Annualized volatility: standard deviation of the monthly cc returns, \ln(1+R_t), times \sqrt{12}.
  • Sharpe ratio: \frac{\text{annualized return}-R_f}{\text{annualized volatility}}, where R_f is the average T-Bill rate of the test period (average of tb3ms divided by 100).

Write a short conclusion: did your process work? That is, did your Optimal portfolio beat SPY and the equally weighted portfolio in return and in Sharpe ratio? Mention one limitation.

Hint: You can use functions from the Workshop Solutions