pydata nyc 2015

25
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

Upload: jess-stauth

Post on 21-Jan-2017

1.437 views

Category:

Software


2 download

TRANSCRIPT

Page 1: PyData NYC 2015

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

Page 2: PyData NYC 2015

#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.

Page 3: PyData NYC 2015

3

The Quant Finance PyData Stack

• Source: [Jake VanderPlas: State of the Tools]– (https://www.youtube.com/watch?v=5GlNDD7qbP4)

#PyData #PyDataNYC

Page 4: PyData NYC 2015

#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

Page 5: PyData NYC 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

Page 6: PyData NYC 2015

#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

Page 7: PyData NYC 2015

#PyData #PyDataNYC 7

Tearsheets analysis packageVisualizations

• Daily returns of a stock, or trading strategy• Positions• Transactions• Periods of market stress• Bayesian risk analyses

Page 8: PyData NYC 2015

#PyData #PyDataNYC 8

Tearsheet Components

Page 9: PyData NYC 2015

#PyData #PyDataNYC 9

Page 10: PyData NYC 2015

#PyData #PyDataNYC 10

Page 11: PyData NYC 2015

#PyData #PyDataNYC 11

Page 12: PyData NYC 2015

#PyData #PyDataNYC 12

Page 13: PyData NYC 2015

#PyData #PyDataNYC 13

Long/Short Exposure over Time

Page 14: PyData NYC 2015

#PyData #PyDataNYC 14

Sector Exposure over Time

Page 15: PyData NYC 2015

#PyData #PyDataNYC 15

Slippage and Transaction Cost Sensitivity

Page 16: PyData NYC 2015

#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

Page 17: PyData NYC 2015

#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.

Page 18: PyData NYC 2015

#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.

Page 19: PyData NYC 2015

#PyData #PyDataNYC 19

Modeling Trading Strategy Uncertainty with Bayesian Analysis

Page 20: PyData NYC 2015

#PyData #PyDataNYC 20

Bayesian analysis – real world example

paper trading

Page 21: PyData NYC 2015

#PyData #PyDataNYC 21

Bayesian analysis – real world example

!

Page 22: PyData NYC 2015

#PyData #PyDataNYC 22

Bayesian analysis – real world example

June 2015

Nov 2015Backtest – “in-sample”

Page 23: PyData NYC 2015

#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

Page 24: PyData NYC 2015

#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

Page 25: PyData NYC 2015

#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?