Python Workshop March 2018
Python Workshop March 2018
March 2018
Why learn to code?
I It’s fun
I Saves you time
I Produce better research
I Looks good on your CV
I Likely to help your career down the road
Why learn to code?
Automated trading programs have taken over the rest of the work,
supported by 200 computer engineers.”
NumPy
The main package for numerical analysis in Python. Includes
functions to deal with arrays of data, linear algebra, random
number generation, and much more.
Main scientific Python libraries
NumPy
The main package for numerical analysis in Python. Includes
functions to deal with arrays of data, linear algebra, random
number generation, and much more.
pandas
Package for panel data analysis, built on top of NumPy. Great for
importing/exporting, merging, cleaning and analysing data.
Written by Wes McKinney while working at AQR (now at 2σ
Investments), so very good for dealing with financial data.
Main scientific Python libraries
SciPy
A collection of packages that add a lot of nice features such as
more advanced linear algebra (above NumPy), numerical
integration and optimization.
Main scientific Python libraries
SciPy
A collection of packages that add a lot of nice features such as
more advanced linear algebra (above NumPy), numerical
integration and optimization.
statsmodels
Statistical analysis, including OLS regressions with support for
fixed effects and clustering.
Main scientific Python libraries
SciPy
A collection of packages that add a lot of nice features such as
more advanced linear algebra (above NumPy), numerical
integration and optimization.
statsmodels
Statistical analysis, including OLS regressions with support for
fixed effects and clustering.
matplotlib
Main package for visualization in Python. Can produce many type
of graphs, highly customizable.
Sample Matplotlib plots
17.5
Intercept: 0.712***
Adj. R2 : 0.386
15.0 N. Obs.: 882
Regression Coefficient βδ,j
12.5
10.0
7.5
5.0
2.5
0.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
j
Source: Boguth, Gregoire and Martineau, Shaping Expectations and Coordinating Attention: The Unintended
Consequences of FOMC Press Conferences, 2018.
9,000,000
Group 1
8,000,000 Group 2
Group 3
7,000,000 Control
6,000,000
Volume ($)
5,000,000
4,000,000
3,000,000
2,000,000
1,000,000
6 6
016 6 016 016 6
201 201 2 201 42 82 201
09 23 t 07 t 21 v0 v1 c 02
Sep Sep Oc Oc No No De
Date
Source: Comerton-Forde, Gregoire and Zhong, Inverted Fee Venues and Market Quality, 2018.
0.09
0.06
Order imbalance
0.03
0.00
-0.03
-0.05
-0.08
-0.11
-0.14
-0.17
ile
5
uint
4 s q
2 ise
4 3
r pr
Trade 6 2 s su
ing
bins 8
10 1 a rn
E
Source: Gregoire and Martineau, How is Earnings News Transmitted to Stock Prices?, 2018.
0.25
Panel A: Log(Fund Size) 0.30
Panel B: Log(Fund Family Size)
Traditional Funds Traditional Funds
Performance Fee Funds 0.25 Performance Fee Funds
0.20
0.20
0.15
0.15
0.10
0.10
0.05
0.05
0.00 0.00
−6 −4 −2 0 2 4 6 8 10 −2 0 2 4 6 8 10 12
Log(Fund Size) Log(Fund Family Size)
0.10
Panel C: Fund Age 0.6
Panel D: Domicile
Traditional Funds Traditional Funds
Performance Fee Funds 0.5 Performance Fee Funds
0.08
0.4
0.06
0.3
0.04
0.2
0.02
0.1
0.00 0.0
0 10 20 30 40 50 60 70 80
UK
France
Germany
Ireland
Italy
Spain
Luxembourg
Switzerland
Fund Age
Source: Gregoire and Sotes-Paladino, Double Bonus? Implicit incentives in Mutual Funds with Explicit
Performance Fee, 2017. (Work in progress)
Additional useful libraries
Credit: http://xkcd.com
Online resources
I Tutorial:
https://docs.python.org/3/tutorial/index.html
I LearnPython.org http://www.learnpython.org/
I Package documentation
I pandas:
http://pandas.pydata.org/pandas-docs/stable/
I matplotlib: http://matplotlib.org/contents.html
I numpy: https://docs.scipy.org/doc/numpy/index.html
Online resources
I Kaggle https://www.kaggle.com/
I Quantopian lectures
https://www.quantopian.com/lectures
4. Other topics
In this block, you will be introduced briefly to other Python
packages that can be helpful for research. We will look at an
example of web scraping with textual analysis.
Let’s get started!