1. Portfolio and Risk Analytics in Python with pyfolio
PyData NYC 2015
Jessica Stauth
VP Quant Strategy
Justin Lent, Thomas Weicki PhD, Andrew Campbell
#PyData #PyDataNYC 1
2. Why use Python for Quant Finance?
• Python is a general purpose language
• No hodge-podge of perl, bash, matlab, R, excel
fortran.
• Very easy to learn.
#PyData #PyDataNYC 2
3. The Quant Finance PyData Stack
• Source: [Jake VanderPlas: State of the Tools]
– (https://www.youtube.com/watch?v=5GlNDD7qbP4)#PyData #PyDataNYC 3
4. Python in Quantitative Finance
• When Quantopian started in 2011, we needed a backtester
– Open-sourced Zipline in 2012
• When we started to build a crowd-source hedge fund, we
needed a better way to evaluate algorithms
– Open-sourced pyfolio in 2015
#PyData #PyDataNYC 4
5. pyfolio
• State-of-the-art portfolio and risk analytics
http://quantopian.github.io/pyfolio/
• Open source and free: Apache v2 license
• Can be used:
– stand alone
– with Zipline
– on Quantopian in a hosted Research Environment
– with PyThalesians
#PyData #PyDataNYC 5
6. Using pyfolio stand-alone
• Installation
• Use Anaconda to get a Python system with the full
PyData ecosystem. Then:
• pip install pyfolio
• Import it in your project
#PyData #PyDataNYC 6
7. Tearsheets analysis package
Visualizations
• Daily returns of a stock, or trading strategy
• Positions
• Transactions
• Periods of market stress
• Bayesian risk analyses
#PyData #PyDataNYC 7
16. Zipline + pyfolio, locally or via quantopian.com
• Zipline: open-source backtester by Quantopian
• Powers quantopian.com
– 12 years of stock market data for US Equities (minute-bar
prices, corporate fundamentals, sentiment, events, etc.)
– Various models for transaction costs and slippage.
– Web based IDE for creating and deploying trading algorithms
• Hosted ipython notebook research server
– Ad-hoc data analysis. We provide market data.
– Pull in strategy backtest results from the Web IDE and use
pyfolio
#PyData #PyDataNYC 16
17. Bayesian analysis in pyfolio
• Sneak-peek into ongoing research.
• Can a backtest (in-sample data) be used to predict the future results
(out of sample data)?
• Sophisticated statistical modeling takes uncertainty into account.
• Uses T-distribution to model returns (instead of normal).
– Addresses ‘fat-tail’ nature of financial returns
• Relies on PyMC3.
– Python module for Bayesian statistical modeling and model fitting which
focuses on advanced Markov chain Monte Carlo fitting algorithms.
#PyData #PyDataNYC 17
18. Modeling Trading Strategy Uncertainty with Bayesian Analysis
How do I know my trading
strategy is “working” after I’ve
put real $ into it?
How many Out-of-Sample trading
days must be observed for me to
be certain?
Calculate: P(mean > 0)
(Probability of out-of-sample means > 0%)
Re-compute model as new data is sampled.
#PyData #PyDataNYC 18
22. Bayesian analysis – real world example
#PyData #PyDataNYC 22
June2015
Nov2015
Backtest – “in-sample”
23. More Info on Bayesian Analysis
Accompanying blog post:
http://blog.quantopian.com/bayesian-cone/
Bayesian Methods for Hackers:
http://camdavidsonpilon.github.io/Probabilistic-Programming-and-
Bayesian-Methods-for-Hackers/
PyMC3: http://pymc-devs.github.io/pymc3
#PyData #PyDataNYC 23
24. Summary
• Pyfolio bundles various useful analyses and includes
advanced statistical modeling.
• “Using pyfolio” webinar tutorial:
https://www.youtube.com/watch?v=-VmZAlBWUko
• Still young -- please contribute:
https://github.com/quantopian/pyfolio/labels/help%2
0wanted
• Bugs: https://github.com/quantopian/pyfolio/issues
#PyData #PyDataNYC 24
25. Up next right here: Andrew Campbell - Bootstrapping
Applications and Dashboards with IPython Widgets
Tomorrow 4:25pm Room A: Scott Sandersen –
Developing an Expression Language for Quantitative
Financial Modeling
jstauth@quantopian.com
@jstauth
www.quantopian.com/fund
Thank you.
Questions?
#PyData #PyDataNYC 25