SlideShare a Scribd company logo
Probabilistic Programming in Quantitative FinanceProbabilistic Programming in Quantitative Finance
Thomas WieckiThomas Wiecki
@twiecki@twiecki
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
1 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
2 of 86 03/17/2015 08:47 PM
About meAbout me
Lead Data Scientist at : Building a
crowd sourced hedge fund.
PhD from Brown University -- research on computational neuroscience and machine
learning using Bayesian modeling.
Quantopian Inc (https://www.quantopian.com)
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
3 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
4 of 86 03/17/2015 08:47 PM
The problem we're gonna solveThe problem we're gonna solve
Two real-money strategies:
In [76]: plot_strats()
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
5 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
6 of 86 03/17/2015 08:47 PM
Types of risk
Systematic and Unsystematic Risk
Volatility
Tailrisk
Beta
D
raw
dow
n
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
7 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
8 of 86 03/17/2015 08:47 PM
Sharpe RatioSharpe Ratio
Sharpe =
mean returns
volatility
In [24]: print "Sharpe ratio strategy etrade =", data_0.mean() / data_0.std() * np.sqrt(252)
print "Sharpe ratio strategy IB =", data_1.mean() / data_1.std() * np.sqrt(252)
Sharpe ratio strategy etrade = 0.627893606355
Sharpe ratio strategy IB = 1.43720181575
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
9 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
10 of 86 03/17/2015 08:47 PM
Types of risk
Systematic and Unsystematic Risk
Model misspecification
Estimation Uncertainty
Programming errors
Data issuesModelRisk
Volatility
Tailrisk
Beta
D
raw
dow
n
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
11 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
12 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
13 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
14 of 86 03/17/2015 08:47 PM
Short primer on random variablesShort primer on random variables
Represents our beliefs about an unknown state.
Probability distribution assigns a probability to each possible state.
Not a single number (e.g. most likely state).
"When I bet on horses, I never lose. Why? I bet on all the horses." Tom Haverford
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
15 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
16 of 86 03/17/2015 08:47 PM
You already know what a variable is...You already know what a variable is...
In [8]: coin = 0 # 0 for tails
coin = 1 # 1 for heads
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
17 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
18 of 86 03/17/2015 08:47 PM
A random variable assigns all possible values a certainA random variable assigns all possible values a certain
probabilityprobability
In [ ]: coin = {0: 50%,
1: 50%}
Alternatively:Alternatively:
coin ~ Bernoulli(p=0.5)
coin is a random variable
Bernoulli is a probability distribution
~ reads as "is distributed as"
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
19 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
20 of 86 03/17/2015 08:47 PM
This was discrete (binary), what about the continuousThis was discrete (binary), what about the continuous
case?case?
returns ~ Normal( , )μ σ2
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
21 of 86 03/17/2015 08:47 PM
In [77]: from scipy import stats
sns.distplot(data_0, kde=False, fit=stats.norm)
plt.xlabel('returns')
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
22 of 86 03/17/2015 08:47 PM
How to estimateHow to estimate andand ??
Naive: point estimate
Set mu = mean(data) and sigma = std(data)
Maximum Likelihood Estimate
Correct answer as
μ σ
n → ∞
Bayesian analysisBayesian analysis
Most of the time ...
Uncertainty about and
Turn and into random variables
How to estimate?
n ≠ ∞
μ σ
μ σ
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
23 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
24 of 86 03/17/2015 08:47 PM
Bayes Formula!Bayes Formula!
BayesPrior
Data
Posterior
Use prior knowledge and data to update our beliefs.
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
25 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
26 of 86 03/17/2015 08:47 PM
In [78]: interactive(gen_plot, n=(0, 600), bayes=True)
Out[78]:
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
27 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
28 of 86 03/17/2015 08:47 PM
Probabilistic ProgrammingProbabilistic Programming
Model unknown causes (e.g. ) of a phenomenon as random variables.
Write a programmatic story of how unknown causes result in observable data.
Use Bayes formula to invert generative model to infer unknown causes.
μ
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
29 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
30 of 86 03/17/2015 08:47 PM
Approximating the posterior with MCMC samplingApproximating the posterior with MCMC sampling
In [81]: plot_want_get()
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
31 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
32 of 86 03/17/2015 08:47 PM
PyMC3PyMC3
Probabilistic Programming framework written in Python.
Allows for construction of probabilistic models using intuitive syntax.
Features advanced MCMC samplers.
Fast: Just-in-time compiled by Theano.
Extensible: easily incorporates custom MCMC algorithms and unusual probability
distributions.
Authors: John Salvatier, Chris Fonnesbeck, Thomas Wiecki
Upcoming beta release!
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
33 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
34 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
35 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
36 of 86 03/17/2015 08:47 PM
Model returns distribution: Specifying ourModel returns distribution: Specifying our
priorspriors
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
37 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
38 of 86 03/17/2015 08:47 PM
In [82]: x = np.linspace(-.3, .3, 500)
plt.plot(x, T.exp(pm.Normal.dist(mu=0, sd=.1).logp(x)).eval())
plt.title(u'Prior: mu ~ Normal(0, $.1^2$)'); plt.xlabel('mu'); plt.ylabel('Probabili
ty Density'); plt.xlim((-.3, .3));
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
39 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
40 of 86 03/17/2015 08:47 PM
In [83]: x = np.linspace(-.1, .5, 500)
plt.plot(x, T.exp(pm.HalfNormal.dist(sd=.1).logp(x)).eval())
plt.title(u'Prior: sigma ~ HalfNormal($.1^2$)'); plt.xlabel('sigma'); plt.ylabel('Pr
obability Density');
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
41 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
42 of 86 03/17/2015 08:47 PM
Bayesian Sharpe ratioBayesian Sharpe ratio
μ ∼ Normal(0, ).12
← Prior
σ ∼ HalfNormal( ).12
← Prior
returns ∼ Normal(μ, )σ2
← Observed!
Sharpe =
μ
σ
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
43 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
44 of 86 03/17/2015 08:47 PM
Graphical model of returnsGraphical model of returns
Bayes
PosteriorsPriors
Data
µ ~
σ ~
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
45 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
46 of 86 03/17/2015 08:47 PM
This is what the data looks likeThis is what the data looks like
In [9]: print data_0.head()
2013-12-31 21:00:00 0.002143
2014-01-02 21:00:00 -0.028532
2014-01-03 21:00:00 -0.001577
2014-01-06 21:00:00 -0.000531
2014-01-07 21:00:00 0.011310
Name: 0, dtype: float64
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
47 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
48 of 86 03/17/2015 08:47 PM
In [14]: import pymc as pm
with pm.Model() as model:
# Priors on Random Variables
mean_return = pm.Normal('mean return', mu=0, sd=.1)
volatility = pm.HalfNormal('volatility', sd=.1)
# Model returns as Normal
obs = pm.Normal('returns',
mu=mean_return,
sd=volatility,
observed=data_0)
sharpe = pm.Deterministic('sharpe ratio',
mean_return / volatility * np.sqrt(252))
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
49 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
50 of 86 03/17/2015 08:47 PM
In [15]: with model:
# Instantiate MCMC sampler
step = pm.NUTS()
# Draw 500 samples from the posterior
trace = pm.sample(500, step)
[-----------------100%-----------------] 500 of 500 complete in 0.4 sec
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
51 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
52 of 86 03/17/2015 08:47 PM
Analyzing the posteriorAnalyzing the posterior
In [84]: sns.distplot(results_normal[0][0]['mean returns'], hist=False, label='etrade')
sns.distplot(results_normal[1][0]['mean returns'], hist=False, label='IB')
plt.title('Posterior of the mean'); plt.xlabel('mean returns')
Out[84]: <matplotlib.text.Text at 0x7fde80cb5850>
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
53 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
54 of 86 03/17/2015 08:47 PM
In [85]: sns.distplot(results_normal[0][0]['volatility'], hist=False, label='etrade')
sns.distplot(results_normal[1][0]['volatility'], hist=False, label='IB')
plt.title('Posterior of the volatility')
plt.xlabel('volatility')
Out[85]: <matplotlib.text.Text at 0x7fde80e58310>
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
55 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
56 of 86 03/17/2015 08:47 PM
In [86]: sns.distplot(results_normal[0][0]['sharpe'], hist=False, label='etrade')
sns.distplot(results_normal[1][0]['sharpe'], hist=False, label='IB')
plt.title('Bayesian Sharpe ratio'); plt.xlabel('Sharpe ratio');
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
57 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
58 of 86 03/17/2015 08:47 PM
In [28]: print 'P(Sharpe ratio IB > 0) = %.2f%%' % 
(np.mean(results_normal[1][0]['sharpe'] > 0) * 100)
P(Sharpe ratio IB > 0) = 96.48%
In [29]: print 'P(Sharpe ratio IB > Sharpe ratio etrade) = %.2f%%' % 
(np.mean(results_normal[1][0]['sharpe'] > results_normal[0][0]['sharpe']) * 100)
P(Sharpe ratio IB > Sharpe ratio etrade) = 80.06%
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
59 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
60 of 86 03/17/2015 08:47 PM
Value at Risk with uncertaintyValue at Risk with uncertainty
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
61 of 86 03/17/2015 08:47 PM
In [88]: ppc_etrade = post_pred(var_cov_var_normal, results_normal[0][0], 1e6, .05, samples=8
00)
ppc_ib = post_pred(var_cov_var_normal, results_normal[1][0], 1e6, .05, samples=800)
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
62 of 86 03/17/2015 08:47 PM
Interim summaryInterim summary
Bayesian stats allows us to reformulate common risk metrics, use priors and
quantify uncertainty.
IB strategy seems better in almost every regard. Is it though?
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
63 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
64 of 86 03/17/2015 08:47 PM
So far, only added confidenceSo far, only added confidence
In [89]: sns.distplot(results_normal[0][0]['sharpe'], hist=False, label='etrade')
sns.distplot(results_normal[1][0]['sharpe'], hist=False, label='IB')
plt.title('Bayesian Sharpe ratio'); plt.xlabel('Sharpe ratio');
plt.axvline(data_0.mean() / data_0.std() * np.sqrt(252), color='b');
plt.axvline(data_1.mean() / data_1.std() * np.sqrt(252), color='g');
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
65 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
66 of 86 03/17/2015 08:47 PM
Is this a good model?Is this a good model?
In [93]: sns.distplot(data_1, label='data IB', kde=False, norm_hist=True, color='.5')
for p in ppc_dist_normal:
plt.plot(x, p, c='r', alpha=.1)
plt.plot(x, p, c='r', alpha=.5, label='Normal model')
plt.xlabel('Daily returns')
plt.legend();
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
67 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
68 of 86 03/17/2015 08:47 PM
Can it be improved? Yes!Can it be improved? Yes!
Identical model as before, but instead, use a heavy-tailed T distribution:
returns ∼ T(ν, μ, )σ2
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
69 of 86 03/17/2015 08:47 PM
In [94]: sns.distplot(data_1, label='data IB', kde=False, norm_hist=True, color='.5')
for p in ppc_dist_t:
plt.plot(x, p, c='y', alpha=.1)
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
70 of 86 03/17/2015 08:47 PM
Lets compare posteriors of the normal and TLets compare posteriors of the normal and T
modelmodel
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
71 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
72 of 86 03/17/2015 08:47 PM
Mean returnsMean returns
In [96]: sns.distplot(results_normal[1][0]['mean returns'], hist=False, color='r', label='nor
mal model')
sns.distplot(results_t[1][0]['mean returns'], hist=False, color='y', label='T model'
)
plt.xlabel('Posterior of the mean returns'); plt.ylabel('Probability Density');
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
73 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
74 of 86 03/17/2015 08:47 PM
Bayesian T-Sharpe ratioBayesian T-Sharpe ratio
In [97]: sns.distplot(results_normal[1][0]['sharpe'], hist=False, color='r', label='normal mo
del')
sns.distplot(results_t[1][0]['sharpe'], hist=False, color='y', label='T model')
plt.xlabel('Bayesian Sharpe ratio'); plt.ylabel('Probability Density');
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
75 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
76 of 86 03/17/2015 08:47 PM
But why? T distribution is more robust!But why? T distribution is more robust!
In [98]: sim_data = list(np.random.randn(75)*.01)
sim_data.append(-.2)
sns.distplot(sim_data, label='data', kde=False, norm_hist=True, color='.5'); sns.dis
tplot(sim_data, label='Normal', fit=stats.norm, kde=False, hist=False, fit_kws={'col
or': 'r', 'label': 'Normal'}); sns.distplot(sim_data, fit=stats.t, kde=False, hist=F
alse, fit_kws={'color': 'y', 'label': 'T'})
plt.xlabel('Daily returns'); plt.legend();
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
77 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
78 of 86 03/17/2015 08:47 PM
Estimating tail risk using VaREstimating tail risk using VaR
In [99]: ppc_normal = post_pred(var_cov_var_normal, trace_normal, 1e6, .05, samples=800)
ppc_t = post_pred(var_cov_var_t, trace_t, 1e6, .05, samples=800)
sns.distplot(ppc_normal, label='Normal', norm_hist=True, hist=False, color='r')
sns.distplot(ppc_t, label='T', norm_hist=True, hist=False, color='y')
plt.legend(loc=0); plt.xlabel('5% daily Value at Risk (VaR) with $1MM capital (in 
$)'); plt.ylabel('Probability density'); plt.xticks(rotation=15);
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
79 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
80 of 86 03/17/2015 08:47 PM
Comparing the Bayesian T-Sharpe ratiosComparing the Bayesian T-Sharpe ratios
In [101]: sns.distplot(results_t[0][0]['sharpe'], hist=False, label='etrade')
sns.distplot(results_t[1][0]['sharpe'], hist=False, label='IB')
plt.xlabel('Bayesian Sharpe ratio'); plt.ylabel('Probability Density');
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
81 of 86 03/17/2015 08:47 PM
In [42]: print 'P(Sharpe ratio IB > Sharpe ratio etrade) = %.2f%%' % 
(np.mean(results_t[1][0]['sharpe'] > results_t[0][0]['sharpe']) * 100)
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
82 of 86 03/17/2015 08:47 PM
ConclusionsConclusions
Bayesian statistics allows us to quantify uncertainty -- measure orthogonal sources
of risk.
Rich statistical framework to compare different models against each other.
Blackbox inference algorithms allow estimation of complex models.
PyMC3 puts advanced samplers at your fingertips.
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
83 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
84 of 86 03/17/2015 08:47 PM
Further readingFurther reading
-- Develop trading algorithms like this in
your browser.
-- IPython
Notebook book on Bayesian stats using PyMC2.
-- Great book by Kruschke.
Twitter:
Quantopian (https://www.quantopian.com)
My blog for Bayesian linear regression (financial alpha and beta)
(https://twiecki.github.io)
Probilistic Programming for Hackers (http://camdavidsonpilon.github.io
/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/)
Doing Bayesian Data Analysis (http://www.indiana.edu/~kruschke
/DoingBayesianDataAnalysis/)
PyMC3 repository (https://github.com/pymc-devs/pymc3)
@twiecki (https://twitter.com/twiecki)
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
85 of 86 03/17/2015 08:47 PM
bayesian_risk_perf_v3 slides http://twiecki.github.io/bayesian_risk_perf_v3.slides.html?print-pdf#/
86 of 86 03/17/2015 08:47 PM

More Related Content

PDF
Probabilistic Programming: Why, What, How, When?
PDF
Quantopian: Crowd-sourced Systematic Alpha
PDF
Self-Directed Investing by Akhil Lodha, Co-founder of Sliced Investing, and M...
PPTX
Crowd-sourced Alpha: The Search for the Holy Grail of Investing
PDF
Using Domain Expertise to Improve Text Analysis, Evan Schnidman, Founder and ...
PDF
Algorithmic trading and Machine Learning by Michael Kearns, Professor of Comp...
PDF
Welcome to QuantCon 2015 by John “Fawce” Fawcett, Founder and CEO of Quantopian
PDF
Beware of Low Frequency Data by Ernie Chan, Managing Member, QTS Capital Mana...
Probabilistic Programming: Why, What, How, When?
Quantopian: Crowd-sourced Systematic Alpha
Self-Directed Investing by Akhil Lodha, Co-founder of Sliced Investing, and M...
Crowd-sourced Alpha: The Search for the Holy Grail of Investing
Using Domain Expertise to Improve Text Analysis, Evan Schnidman, Founder and ...
Algorithmic trading and Machine Learning by Michael Kearns, Professor of Comp...
Welcome to QuantCon 2015 by John “Fawce” Fawcett, Founder and CEO of Quantopian
Beware of Low Frequency Data by Ernie Chan, Managing Member, QTS Capital Mana...

Viewers also liked (19)

PDF
The Genesis of an Order Type by Dan Aisen, Co-founder and Quantitative Develo...
PDF
A Guided Tour of Machine Learning for Traders by Tucker Balch at QuantCon 2016
PDF
Beyond Semantic Analysis Utilizing Social Finance Data Sets to Improve Quanti...
PDF
Leveraging Quandl
PDF
10 Ways Backtests Lie by Tucker Balch
PDF
Finding Alpha from Stock Buyback Announcements in the Quantopian Research Pla...
PDF
How Women are Conquering the S&P500 by Karen Rubin, Director of Product at Qu...
PDF
Market Timing, Big Data, and Machine Learning by Xiao Qiao at QuantCon 2016
PDF
The Mobile Revolution and the Future of Modern Data Collection by Joe Reising...
PDF
Careers in Systematic Investing: Advice and Perspective at the End by Matthew...
PPTX
Crowdsource Earnings Predictions and the Quantopian Research Platform
PPTX
Pairs Trading from NYC Algorithmic Trading Meetup November '13
PDF
Turkey or trader? by Jeremiah Lowin, Director of Risk Management at Kokino LLC
PDF
Finding, Exploring, and Refining Trading Strategies: A Case Study by Matthew ...
PDF
Combining the Best Stock Selection Factors by Patrick O'Shaughnessy at QuantC...
PDF
Market Outlook 2015: How to Spot Bubbles, Avoid Market Crashes and Earn Big ...
PDF
Case Studies in Creating Quant Models from Large Scale Unstructured Text by S...
PDF
Dual Momentum Investing by Gary Antonacci QuantCon 2016
PDF
Honey, I Deep-shrunk the Sample Covariance Matrix! by Erk Subasi at QuantCon ...
The Genesis of an Order Type by Dan Aisen, Co-founder and Quantitative Develo...
A Guided Tour of Machine Learning for Traders by Tucker Balch at QuantCon 2016
Beyond Semantic Analysis Utilizing Social Finance Data Sets to Improve Quanti...
Leveraging Quandl
10 Ways Backtests Lie by Tucker Balch
Finding Alpha from Stock Buyback Announcements in the Quantopian Research Pla...
How Women are Conquering the S&P500 by Karen Rubin, Director of Product at Qu...
Market Timing, Big Data, and Machine Learning by Xiao Qiao at QuantCon 2016
The Mobile Revolution and the Future of Modern Data Collection by Joe Reising...
Careers in Systematic Investing: Advice and Perspective at the End by Matthew...
Crowdsource Earnings Predictions and the Quantopian Research Platform
Pairs Trading from NYC Algorithmic Trading Meetup November '13
Turkey or trader? by Jeremiah Lowin, Director of Risk Management at Kokino LLC
Finding, Exploring, and Refining Trading Strategies: A Case Study by Matthew ...
Combining the Best Stock Selection Factors by Patrick O'Shaughnessy at QuantC...
Market Outlook 2015: How to Spot Bubbles, Avoid Market Crashes and Earn Big ...
Case Studies in Creating Quant Models from Large Scale Unstructured Text by S...
Dual Momentum Investing by Gary Antonacci QuantCon 2016
Honey, I Deep-shrunk the Sample Covariance Matrix! by Erk Subasi at QuantCon ...
Ad

Similar to Probabilistic Programming in Quantitative Finance by Thomas Wiecki, Lead Data Scientist at Quantopian (10)

PDF
Swift on Raspberry Pi
PDF
Openbar 7 - Leuven - OpenShift - The Enterprise Container Platform - Piros
PDF
Headache from using mathematical software
ODP
HPC For Bioinformatics
PDF
Better Documentation Through Automation: Creating docutils & Sphinx Extensions
PDF
08-15-2024 - AI Camp Meetup - Human Pose Estimation in Real-Time Utilizing Ed...
PDF
How do we detect malware? A step-by-step guide
PDF
Modern Web Security, Lazy but Mindful Like a Fox
ODP
2600 av evasion_deuce
PPTX
Security Awareness for Open Source Web Applications
Swift on Raspberry Pi
Openbar 7 - Leuven - OpenShift - The Enterprise Container Platform - Piros
Headache from using mathematical software
HPC For Bioinformatics
Better Documentation Through Automation: Creating docutils & Sphinx Extensions
08-15-2024 - AI Camp Meetup - Human Pose Estimation in Real-Time Utilizing Ed...
How do we detect malware? A step-by-step guide
Modern Web Security, Lazy but Mindful Like a Fox
2600 av evasion_deuce
Security Awareness for Open Source Web Applications
Ad

More from Quantopian (20)

PPTX
Being open (source) in the traditionally secretive field of quant finance.
PPTX
Stauth common pitfalls_stock_market_modeling_pqtc_fall2018
PPTX
Tearsheet feedback webinar 10.10.18
PDF
"Three Dimensional Time: Working with Alternative Data" by Kathryn Glowinski,...
PPTX
"Alpha from Alternative Data" by Emmett Kilduff, Founder and CEO of Eagle Alpha
PPTX
"Supply Chain Earnings Diffusion" by Josh Holcroft, Head of Quantitative Rese...
PDF
"Portfolio Optimisation When You Don’t Know the Future (or the Past)" by Rob...
PPTX
"Quant Trading for a Living – Lessons from a Life in the Trenches" by Andreas...
PDF
“Real Time Machine Learning Architecture and Sentiment Analysis Applied to Fi...
PDF
“Market Insights Through the Lens of a Risk Model” by Olivier d'Assier, Head ...
PDF
"Maximize Alpha with Systematic Factor Testing" by Cheng Peng, Software Engin...
PPTX
"How to Run a Quantitative Trading Business in China with Python" by Xiaoyou ...
PDF
"Fundamental Forecasts: Methods and Timing" by Vinesh Jha, CEO of ExtractAlpha
PPTX
"From Alpha Discovery to Portfolio Construction: Pitfalls and Solutions" by D...
PDF
"Deep Reinforcement Learning for Optimal Order Placement in a Limit Order Boo...
PPTX
"Making the Grade: A Look Inside the Algorithm Evaluation Process" by Dr. Jes...
PDF
"Building Diversified Portfolios that Outperform Out-of-Sample" by Dr. Marcos...
PPTX
"From Insufficient Economic data to Economic Big Data – How Trade Data is red...
PDF
"Machine Learning Approaches to Regime-aware Portfolio Management" by Michael...
PDF
"A Framework-Based Approach to Building Quantitative Trading Systems" by Dr. ...
Being open (source) in the traditionally secretive field of quant finance.
Stauth common pitfalls_stock_market_modeling_pqtc_fall2018
Tearsheet feedback webinar 10.10.18
"Three Dimensional Time: Working with Alternative Data" by Kathryn Glowinski,...
"Alpha from Alternative Data" by Emmett Kilduff, Founder and CEO of Eagle Alpha
"Supply Chain Earnings Diffusion" by Josh Holcroft, Head of Quantitative Rese...
"Portfolio Optimisation When You Don’t Know the Future (or the Past)" by Rob...
"Quant Trading for a Living – Lessons from a Life in the Trenches" by Andreas...
“Real Time Machine Learning Architecture and Sentiment Analysis Applied to Fi...
“Market Insights Through the Lens of a Risk Model” by Olivier d'Assier, Head ...
"Maximize Alpha with Systematic Factor Testing" by Cheng Peng, Software Engin...
"How to Run a Quantitative Trading Business in China with Python" by Xiaoyou ...
"Fundamental Forecasts: Methods and Timing" by Vinesh Jha, CEO of ExtractAlpha
"From Alpha Discovery to Portfolio Construction: Pitfalls and Solutions" by D...
"Deep Reinforcement Learning for Optimal Order Placement in a Limit Order Boo...
"Making the Grade: A Look Inside the Algorithm Evaluation Process" by Dr. Jes...
"Building Diversified Portfolios that Outperform Out-of-Sample" by Dr. Marcos...
"From Insufficient Economic data to Economic Big Data – How Trade Data is red...
"Machine Learning Approaches to Regime-aware Portfolio Management" by Michael...
"A Framework-Based Approach to Building Quantitative Trading Systems" by Dr. ...

Recently uploaded (20)

PDF
Buy Verified Stripe Accounts for Sale - Secure and.pdf
PDF
illuminati Uganda brotherhood agent in Kampala call 0756664682,0782561496
PDF
THE EFFECT OF FOREIGN AID ON ECONOMIC GROWTH IN ETHIOPIA
PPTX
social-studies-subject-for-high-school-globalization.pptx
PDF
Mathematical Economics 23lec03slides.pdf
PDF
Predicting Customer Bankruptcy Using Machine Learning Algorithm research pape...
PDF
Dr Tran Quoc Bao the first Vietnamese speaker at GITEX DigiHealth Conference ...
PDF
Circular Flow of Income by Dr. S. Malini
PDF
Understanding University Research Expenditures (1)_compressed.pdf
PDF
ssrn-3708.kefbkjbeakjfiuheioufh ioehoih134.pdf
PDF
1a In Search of the Numbers ssrn 1488130 Oct 2009.pdf
PDF
caregiving tools.pdf...........................
PPTX
Introduction to Managemeng Chapter 1..pptx
PDF
How to join illuminati agent in Uganda Kampala call 0782561496/0756664682
PDF
Copia de Minimal 3D Technology Consulting Presentation.pdf
PPTX
How best to drive Metrics, Ratios, and Key Performance Indicators
PPTX
Session 11-13. Working Capital Management and Cash Budget.pptx
PDF
ECONOMICS AND ENTREPRENEURS LESSONSS AND
PDF
Bitcoin Layer August 2025: Power Laws of Bitcoin: The Core and Bubbles
PDF
Lecture1.pdf buss1040 uses economics introduction
Buy Verified Stripe Accounts for Sale - Secure and.pdf
illuminati Uganda brotherhood agent in Kampala call 0756664682,0782561496
THE EFFECT OF FOREIGN AID ON ECONOMIC GROWTH IN ETHIOPIA
social-studies-subject-for-high-school-globalization.pptx
Mathematical Economics 23lec03slides.pdf
Predicting Customer Bankruptcy Using Machine Learning Algorithm research pape...
Dr Tran Quoc Bao the first Vietnamese speaker at GITEX DigiHealth Conference ...
Circular Flow of Income by Dr. S. Malini
Understanding University Research Expenditures (1)_compressed.pdf
ssrn-3708.kefbkjbeakjfiuheioufh ioehoih134.pdf
1a In Search of the Numbers ssrn 1488130 Oct 2009.pdf
caregiving tools.pdf...........................
Introduction to Managemeng Chapter 1..pptx
How to join illuminati agent in Uganda Kampala call 0782561496/0756664682
Copia de Minimal 3D Technology Consulting Presentation.pdf
How best to drive Metrics, Ratios, and Key Performance Indicators
Session 11-13. Working Capital Management and Cash Budget.pptx
ECONOMICS AND ENTREPRENEURS LESSONSS AND
Bitcoin Layer August 2025: Power Laws of Bitcoin: The Core and Bubbles
Lecture1.pdf buss1040 uses economics introduction

Probabilistic Programming in Quantitative Finance by Thomas Wiecki, Lead Data Scientist at Quantopian