0% found this document useful (0 votes)
63 views1 page

Calculate Stock Beta in Python

This homework assignment involves calculating the beta coefficient of stocks using different methods in Python. Students are to read stock price data from a CSV file, then calculate beta using the mathematical function, matrix/vector method of ordinary least squares regression, and the linear regression package in SciPy. Students must also plot the returns of the stock against the S&P 500 returns on a time axis measured in years. Submissions will be graded based on a rubric for correctness, efficiency, and whether the script runs and produces output for a randomly selected stock data file.

Uploaded by

Victor Cabrejos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views1 page

Calculate Stock Beta in Python

This homework assignment involves calculating the beta coefficient of stocks using different methods in Python. Students are to read stock price data from a CSV file, then calculate beta using the mathematical function, matrix/vector method of ordinary least squares regression, and the linear regression package in SciPy. Students must also plot the returns of the stock against the S&P 500 returns on a time axis measured in years. Submissions will be graded based on a rubric for correctness, efficiency, and whether the script runs and produces output for a randomly selected stock data file.

Uploaded by

Victor Cabrejos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Homework 5

Calculating Beta in Python


Due September 29th by 11:59 PM
Submit assignments as last_first_hw5.py

Summary:

In this assignment you will calculate the beta coefficient of a stock(s) using different
methods in Python. You are free to use any library and data container.

Notation for financial mathematics


- Denote the spot price and logarithmic price as 𝑆𝑡 and 𝑋𝑡 = log⁡(𝑆𝑡 ), respectively
▪ Logarithmic return at time 𝑡 is then defined as 𝑟𝑡 = 𝑋𝑡 − 𝑋𝑡−1
- Denote continuous time as 𝑡 ∈ [0, 𝑇]
- Denote the discretization of 𝑇 into a grid of 𝑛 ∈ [0, 𝑁] steps (indexes) as 𝑡𝑛
𝑇
▪ The step size is then Δ𝑡 =
𝑁
▪ The time of the initial observation occurs at 𝑛 = 0, i.e. 𝑡 = 𝑡0.
▪ In our setting, 𝑇 = 23400, 𝑁 = 4680, and Δ𝑡 = 5

Homework Instructions:

Start with a fresh Python script:


1. Read the price series, 𝑆𝑡 , for the stocks in CRSP_1.csv or CRSP_5.csv:

2. Compute the beta coefficient for the stock(s) using the following methods:
𝐶𝑜𝑣(𝑟𝐹 ,𝑟𝑆𝑃⁡500 )
a. Using the mathematical function: 𝛽𝐹,𝑆𝑃500 =
𝑉𝑎𝑟(𝑟𝐹 )

b. Using the matrix/vector method of OLS (See eq. (12)): (𝑟𝐹𝑇 · 𝑟𝐹 )−1 · 𝑟𝐹𝑇 · 𝑟𝑆𝑃500
c. Using the linear regression package in SciPy
3. Plot 𝑟𝑡,𝑖 and 𝑟𝑡,𝑆𝑃500 , with the x-axis set to time and the x-label set to years.

Grading Process

I will award points according the rubric in the syllabus. To determine whether the script
is correct and efficient, the program will be run on a separate stock-day file. The script, if
done correctly, will report accurate results for a random stock day file.
If you are unable to complete all the tasks above, at a minimum make sure your script can
run and produce some output. A script that doesn’t run receives the least amount of points.

You might also like