Python for Finance
Table of Contents
For students taking the course: Each week you will submit your Colab notebook with the exercise completed. To make sure you don't lose your work, save the Colab notebook you are modifying to your Google Drive. This will create a unique URL for your Colab notebook. When you have completed the exercise, submit this link to the Brightspace dropbox. If you do not save your notebook to your Drive you may not get a unique URL, and then you will submit my own URL which will retain none of your modifications. For your first couple submissions you may want to double-check that your URL is different from my notebook.
A couple other notes:
- When watching the videos, be sure to follow along in your copy of the Colab Notebooks.
- When you modify a Colab notebook it automatically creates a new notebook. This means you can always go back to the original Colab Notebooks using the links below. So don't hesitate to modify your copy—you learn best by experimenting.
- Most courses in using Python in Finance use stock data and create trading algorithms. This is a very niche skill. Here we are going to focus on core financial analyst tasks, e.g. what is the average PPP loan size in Texas? These are common tasks, and are increasingly being answered in Python rather than Excel.
- There are exercises at the end of each session. Be sure to complete them.
- You should take the time to learn how to use Colab (Jupyter) notebooks. Increasingly companies are using them for internal reporting.
Module 0: Introduction and Setup
Week 0
Topics covered:
- Course Overview:
- What we will cover in the course.
- The use of Python in finance.
- Python Relative to other tools.
- Excel
- Other Programming Languages
- Comparison with R
- Using Javascript for Visualization
Week 1
Session Video (Part 1) Colab Notebooks and Basic Arithmetic
Session Video (Part 2) Defining Functions and Data Types
Topics covered:
- Interacting with Python
- Google's Colab and Jupyter
- Introduction to markdown
- Sharing notebooks
- Google's Colab and Jupyter
- Intro to the Linux Server
- Creating Functions and Time-Value-of-Money calculations
- Basic control flow
- The importance of indentation
Week 2
Topics covered:
- Sharing code and the Python ecosystem.
- Python Packages
- Extending the core language
- Our most used packages.
- Installing and Loading.
- How to call methods/functions from a package.
- Package security.
Module 1: Pandas and Spreadsheet (tabular) Data
This module focuses on corporate finance related tasks. Accordingly we read data from spreadsheet and Dataframe structures.
Week 3
Exercise: Is there a correlation between industry institutional ownership and return on equity?
Topics covered:
- An intro to Pandas
- Reading csv and excel files over http.
- Data cleaning.
- Inspecting the objects
- Determining dataframe size and the column types.
- Adding new columns
- A note of functions vs methods.
Week 4
Exercise: What is the average on and off-peak real-time fuel mix?
Topics covered:
- Dates in Pandas.
- Interacting with the operating system from Python.
- Summarizing by creating group means, etc.
Week 5
Exercise: Where were the PPP loans made?
Topics covered:
- Filtering DataFrames
- Pivot Tables
Week 6
The previous sections have used data available online through APIs and flat files. Commonly the financial analyst will have to query relational (SQL) databases. In this section we'll cover the basics of connecting to a SQL database in Python, and executing SQL queries, and returning the data as a Pandas DataFrame.
Module 2: Numpy/Scipy and APIs
This module focuses on using scientific computing libraries such as Numpy Scipy to complete investments-focused tasks. We also retrieve JSON data from APIs.
Week 7
- An introduction to Application Programming Interfaces (APIs) and online
data. Above notebook uses the yfinance package. Additional APIs we can use.
- Example: Coinbase API
- Example: Interactive Brokers API
- EIA API.
- Connecting to databases.
Exercise: Pull stock data via API and estimate a Markowitz mean-variance optimal portfolio.
Alternative: Calculate futures prices given options (but we cant get this from API data).
Week 8
Old video and notebook. Coinbase API Packages are now broken.
- An overview of scipy and basic statistics in Python.
- Extracting coefficients from an estimated regression model.
Exercise: Calculate market depth via Kyle's lambda. Use Coinbase API to get trades.
Week 9
Exercise: Deposit insurance valuation using (Merton's) option pricing method. (chapter 19, Merton's CTF)
Week 10
Calculate Performance Measures for Various ETFs (Sharpe, Information ratios, etc)
Module 3: AI/ML
Week 11
Predict Small Business Loans with a Deep Neural Network in Tensorflow
Week 12:
Using Pytorch to Calculate Greeks for the Margrabe Model
Module 4: Sharing Your Work
This module focuses on showcasing your work via visualizations, sharing code via git and GitHub. We also cover reproducible environments.
Week 13
- Visualization:
- Matplotlib
- Seaborn
- Plotly
- ggplot (using plotnine library)
- Interactive Web Apps via Plotly's Dash
Week 14
- Git/Github and IDE Integration
- Why use version control?
- Collaborate with others more easily.
- Show others your work.
- Why use version control?
Week 15
- The Integrated Development Environment (IDE)
- What does an IDE provide?
- Code Completion
- Linter
- Git/Github and IDE Integration
- What does an IDE provide?
- Virtual Environments
- Ensuring compatible python and package versions.
- Anaconda Python