Upload
jess-stauth
View
1.437
Download
2
Embed Size (px)
Citation preview
Portfolio and Risk Analytics in Python with pyfolio
PyData NYC 2015
Jessica StauthVP Quant Strategy
Justin Lent, Thomas Weicki PhD, Andrew Campbell
#PyData #PyDataNYC 1
#PyData #PyDataNYC 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.
3
The Quant Finance PyData Stack
• Source: [Jake VanderPlas: State of the Tools]– (https://www.youtube.com/watch?v=5GlNDD7qbP4)
#PyData #PyDataNYC
#PyData #PyDataNYC 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 5
pyfolio
• State-of-the-art portfolio and risk analyticshttp://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 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 7
Tearsheets analysis packageVisualizations
• Daily returns of a stock, or trading strategy• Positions• Transactions• Periods of market stress• Bayesian risk analyses
#PyData #PyDataNYC 8
Tearsheet Components
#PyData #PyDataNYC 9
#PyData #PyDataNYC 10
#PyData #PyDataNYC 11
#PyData #PyDataNYC 12
#PyData #PyDataNYC 13
Long/Short Exposure over Time
#PyData #PyDataNYC 14
Sector Exposure over Time
#PyData #PyDataNYC 15
Slippage and Transaction Cost Sensitivity
#PyData #PyDataNYC 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 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 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 19
Modeling Trading Strategy Uncertainty with Bayesian Analysis
#PyData #PyDataNYC 20
Bayesian analysis – real world example
paper trading
#PyData #PyDataNYC 21
Bayesian analysis – real world example
!
#PyData #PyDataNYC 22
Bayesian analysis – real world example
June 2015
Nov 2015Backtest – “in-sample”
#PyData #PyDataNYC 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 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%20wanted
• Bugs: https://github.com/quantopian/pyfolio/issues
#PyData #PyDataNYC 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
[email protected]@jstauth
www.quantopian.com/fund
Thank you.Questions?