SlideShare a Scribd company logo
PyMC3 – Bayesian Statistical Modelling in Python
Max Kochurov
22 June, 2019
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 1 / 29
About me
Max Kochurov tg,slack:@ferres / github:ferrine
Geoopt
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 2 / 29
Bayesian Statistics
Figure: Updating prior p(λ)
p(λ | D) =
p(D | λ)p(λ)
p(D)
• p(λ) – Prior, base knowledge
• p(D | λ) – Likelihood, new information
• p(λ | D) – Posterior, updated knowledge
• p(D) – Evidence, surprise in data
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 3 / 29
Bayesian Statistics
p(λ | D) =
p(D | λ)p(λ)
p(D)
• p(λ) – Prior, base knowledge
• p(D | λ) – Likelihood, new information
• p(λ | D) – Posterior, updated knowledge
• p(D) – Evidence, surprise in data
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 3 / 29
Bayesian Statistics
Compared to Frequentist
• Elegant way to put assumptions in the
model
• Can work with few Data!
• No need for p-values,
all you need is p(λ | D)
• . . .
p(λ | D) =
p(D | λ)p(λ)
p(D)
• p(λ) – Prior, base knowledge
• p(D | λ) – Likelihood, new information
• p(λ | D) – Posterior, updated knowledge
• p(D) – Evidence, surprise in data
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 3 / 29
Where do people use Bayesian inference?
• Finance (Quantopian): estimating the performance of trading algorithms
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
Where do people use Bayesian inference?
• Finance (Quantopian): estimating the performance of trading algorithms
• Music Streaming (Sounds): A/B testing, churn prediction, lifetime value, defining user
session
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
Where do people use Bayesian inference?
• Finance (Quantopian): estimating the performance of trading algorithms
• Music Streaming (Sounds): A/B testing, churn prediction, lifetime value, defining user
session
• E-Commerce (Salesforce): A/B tests, combining disparate sources of information,
hierarchical models
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
Where do people use Bayesian inference?
• Finance (Quantopian): estimating the performance of trading algorithms
• Music Streaming (Sounds): A/B testing, churn prediction, lifetime value, defining user
session
• E-Commerce (Salesforce): A/B tests, combining disparate sources of information,
hierarchical models
• Astronomy: estimating orbits of space objects, getting an image of a black hole
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
Where do people use Bayesian inference?
• Finance (Quantopian): estimating the performance of trading algorithms
• Music Streaming (Sounds): A/B testing, churn prediction, lifetime value, defining user
session
• E-Commerce (Salesforce): A/B tests, combining disparate sources of information,
hierarchical models
• Astronomy: estimating orbits of space objects, getting an image of a black hole
• Life science: epidemic analysis
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
Where do people use Bayesian inference?
• Finance (Quantopian): estimating the performance of trading algorithms
• Music Streaming (Sounds): A/B testing, churn prediction, lifetime value, defining user
session
• E-Commerce (Salesforce): A/B tests, combining disparate sources of information,
hierarchical models
• Astronomy: estimating orbits of space objects, getting an image of a black hole
• Life science: epidemic analysis
• Medicine: calculating effect sizes for drugs
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
Where do people use Bayesian inference?
• Finance (Quantopian): estimating the performance of trading algorithms
• Music Streaming (Sounds): A/B testing, churn prediction, lifetime value, defining user
session
• E-Commerce (Salesforce): A/B tests, combining disparate sources of information,
hierarchical models
• Astronomy: estimating orbits of space objects, getting an image of a black hole
• Life science: epidemic analysis
• Medicine: calculating effect sizes for drugs
• . . .
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
Where do people use Bayesian inference?
• Finance (Quantopian): estimating the performance of trading algorithms
• Music Streaming (Sounds): A/B testing, churn prediction, lifetime value, defining user
session
• E-Commerce (Salesforce): A/B tests, combining disparate sources of information,
hierarchical models
• Astronomy: estimating orbits of space objects, getting an image of a black hole
• Life science: epidemic analysis
• Medicine: calculating effect sizes for drugs
• . . .
Some will be covered later
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
Coin Example
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 5 / 29
Fair Coin Flips – expect fair
p ∼ Beta(3, 3)
flips ∼ Binomial(N, p)
Data: 12 out of 20 flips
What is p?
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 6 / 29
Fair Coin Flips – expect fair
p ∼ Beta(3, 3)
flips ∼ Binomial(N, p)
Data: 12 out of 20 flips
What is p?
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 6 / 29
Fair Coin Flips – expect fair
p ∼ Beta(3, 3)
flips ∼ Binomial(N, p)
Data: 12 out of 20 flips
What is p?
We expect an angel and see an angel, we are confident
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 6 / 29
Fair Coin Flips – expect unfair
What is p?
p ∼ Beta(1, 5)
flips ∼ Binomial(N, p)
Data: 12 out of 20 flips
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 7 / 29
Fair Coin Flips – expect unfair
What is p?
p ∼ Beta(1, 5)
flips ∼ Binomial(N, p)
Data: 12 out of 20 flips
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 7 / 29
Fair Coin Flips – expect unfair
What is p?
p ∼ Beta(1, 5)
flips ∼ Binomial(N, p)
Data: 12 out of 20 flips
We expect a devil but see an angel, we are less confident
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 7 / 29
Hierarchical Models
What if we have more diverse data? We can estimate an amount of devils + uncertainty!
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 8 / 29
Hierarchical Models
What if we have more diverse data? We can estimate an amount of devils + uncertainty!
Data: [8, 2, 2, 10, 0,
0, 2, 5, 6, 10] out of 20 flips
λDevil ∼ Exponential(1)
λAngel ∼ Exponential(1)
pi ∼ Beta(λAngel , λAngel + λDevil )
flipsi ∼ Binomial(pi , Ni )
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 8 / 29
Hierarchical Models
What if we have more diverse data? We can estimate an amount of devils + uncertainty!
Data: [8, 2, 2, 10, 0,
0, 2, 5, 6, 10] out of 20 flips
λDevil ∼ Exponential(1)
λAngel ∼ Exponential(1)
pi ∼ Beta(λAngel , λAngel + λDevil )
flipsi ∼ Binomial(pi , Ni )
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 8 / 29
Markov Chain Monte Carlo (MCMC) & Variational Inference
Few simple rules
• Try MCMC first
• If it is slow: wait a bit
• Got tired: try VI
• Didn’t work: feel sad
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 9 / 29
Markov Chain Monte Carlo (MCMC) & Variational Inference
Few simple rules
• Try MCMC first
• If it is slow: wait a bit
• Got tired: try VI
• Didn’t work: feel sad
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 9 / 29
Markov Chain Monte Carlo (MCMC) & Variational Inference
Few simple rules
• Try MCMC first
• If it is slow: wait a bit
• Got tired: try VI
• Didn’t work: feel sad
Did not work for you?
goto https://discourse.pymc.io
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 9 / 29
Inspecting Your Model
What is an amount of devils? (MCMC used) pm.traceplot(trace)
0 1 2 3 4 5 6 7 8
0.0
0.2
0.4
Frequency
devils
0 100 200 300 400 500
0.0
2.5
5.0
7.5
Samplevalue
devils
0.5 1.0 1.5 2.0 2.5
0.0
0.5
1.0
Frequency
angels
0 100 200 300 400 500
1
2
Samplevalue
angels
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
0
10
20
Frequency
p
0 100 200 300 400 500
0.00
0.25
0.50
0.75
Samplevalue
p
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 10 / 29
Inspecting Your Model
plt.hist(trace["angels"],
alpha=.8, label='Alpha')
plt.hist(trace["angels"]+trace["devils"],
alpha=.8, label='Beta')
plt.legend(fontsize=20)
0 1 2 3 4 5 6 7 8
0
50
100
150
200
250
300
Alpha
Beta
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 11 / 29
Inspecting Your Model
0.0 0.2 0.4 0.6 0.8 1.0
0
1
2
3
4
5
6
7
Prob Distribution
for a, b in zip(
trace["angels"],
trace["angels"]+trace["devils"]
):
plt.plot(
np.linspace(0, 1),
st.beta(a, b).pdf(np.linspace(0, 1)),
color="b", alpha=.025
)
a_mean = trace["angels"].mean()
b_mean = (trace["angels"]+trace["devils"]).mean()
plt.plot(
np.linspace(0, 1),
st.beta(a_mean, b_mean).pdf(np.linspace(0, 1)),
color="black", linewidth=4.0
)
plt.axvline(0.5)
plt.title("Prob Distribution", fontsize=20)
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 12 / 29
Case Studies
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 13 / 29
A/B testing
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 14 / 29
A/B testing, why Bayes
We would accept that
result
0.0 0.2 0.4 0.6 0.8 1.0
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
p-val=0.2398diff=0.1
diff=0.1
pval=.24
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 15 / 29
A/B testing, why Bayes
We would accept that
result
0.0 0.2 0.4 0.6 0.8 1.0
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
p-val=0.2398diff=0.1
diff=0.1
pval=.24
We wait too long to
check small diff
0.0 0.2 0.4 0.6 0.8 1.0
0
5
10
15
20
25
30
35
40
p-val=0.2398diff=0.01
diff=0.01
pval=.24
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 15 / 29
A/B testing, why Bayes
We would accept that
result
0.0 0.2 0.4 0.6 0.8 1.0
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
p-val=0.2398diff=0.1
diff=0.1
pval=.24
We wait too long to
check small diff
0.0 0.2 0.4 0.6 0.8 1.0
0
5
10
15
20
25
30
35
40
p-val=0.2398diff=0.01
diff=0.01
pval=.24
WTF?
0.0 0.2 0.4 0.6 0.8 1.0
0
5
10
15
20
25
30
35
40
p-val=0.0786diff=0.02
diff=0.02
pval=0.07
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 15 / 29
Expected Loss Framework
p-values are useless. What about calculating a loss of ignoring a better model?
• We now decide to use model A or B
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 16 / 29
Expected Loss Framework
p-values are useless. What about calculating a loss of ignoring a better model?
• We now decide to use model A or B
• A has some α ctr, B has some β ctr
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 16 / 29
Expected Loss Framework
p-values are useless. What about calculating a loss of ignoring a better model?
• We now decide to use model A or B
• A has some α ctr, B has some β ctr
• Say a loss (can be any) to ignore a better model given we use x
Loss(α, β, x) =
max(β − α, 0), x = A
max(α − β, 0), x = B
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 16 / 29
Expected Loss Framework
p-values are useless. What about calculating a loss of ignoring a better model?
• We now decide to use model A or B
• A has some α ctr, B has some β ctr
• Say a loss (can be any) to ignore a better model given we use x
Loss(α, β, x) =
max(β − α, 0), x = A
max(α − β, 0), x = B
• Loss depends on the magnitude of difference (p-values do not)
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 16 / 29
Expected Loss Framework
L(x) = Ep(α,β)Loss(α, β, x) → min
x
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 17 / 29
Expected Loss Framework
L(x) = Ep(α,β)Loss(α, β, x) → min
x
0.0 0.2 0.4 0.6 0.8 1.0
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
p-val=0.2398diff=0.1
L(A) = 0.122
L(B) = 0.019
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 17 / 29
Expected Loss Framework
L(x) = Ep(α,β)Loss(α, β, x) → min
x
0.0 0.2 0.4 0.6 0.8 1.0
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
p-val=0.2398diff=0.1
L(A) = 0.122
L(B) = 0.019
0.0 0.2 0.4 0.6 0.8 1.0
0
5
10
15
20
25
30
35
40
p-val=0.2398diff=0.01
L(A) = 0.012
L(B) = 0.002
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 17 / 29
Expected Loss Framework
L(x) = Ep(α,β)Loss(α, β, x) → min
x
0.0 0.2 0.4 0.6 0.8 1.0
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
p-val=0.2398diff=0.1
L(A) = 0.122
L(B) = 0.019
0.0 0.2 0.4 0.6 0.8 1.0
0
5
10
15
20
25
30
35
40
p-val=0.2398diff=0.01
L(A) = 0.012
L(B) = 0.002
0.0 0.2 0.4 0.6 0.8 1.0
0
5
10
15
20
25
30
35
40
p-val=0.0786diff=0.02
L(A) = 0.02
L(B) = 0.0005
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 17 / 29
Expected Loss Framework
L(x) = Ep(α,β)Loss(α, β, x) → min
x
0.0 0.2 0.4 0.6 0.8 1.0
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
p-val=0.2398diff=0.1
L(A) = 0.122
L(B) = 0.019
0.0 0.2 0.4 0.6 0.8 1.0
0
5
10
15
20
25
30
35
40
p-val=0.2398diff=0.01
L(A) = 0.012
L(B) = 0.002
0.0 0.2 0.4 0.6 0.8 1.0
0
5
10
15
20
25
30
35
40
p-val=0.0786diff=0.02
L(A) = 0.02
L(B) = 0.0005
Without p-values we stop first experiment earlier as B is a clear winner
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 17 / 29
Real(!) Impact of prior
Do you believe in ctr > 0.9 ? Or > 0.8? Or 0?
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 18 / 29
Real(!) Impact of prior
Do you believe in ctr > 0.9 ? Or > 0.8? Or 0?
You have your p(ctr) in mind, use it!
Informative prior
Non-Informative
prior
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 18 / 29
Real(!) Impact of prior
Do you believe in ctr > 0.9 ? Or > 0.8? Or 0?
You have your p(ctr) in mind, use it!
• You iterate quicker
• Accept small improvements
• Fewer problems of continuous monitoring
Informative prior
Non-Informative
prior
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 18 / 29
Real(!) Impact of prior
Do you believe in ctr > 0.9 ? Or > 0.8? Or 0?
You have your p(ctr) in mind, use it!
• You iterate quicker
• Accept small improvements
• Fewer problems of continuous monitoring
Do not use overconfident prior, choose slightly pessimistic one
Informative prior
Non-Informative
prior
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 18 / 29
Real(!) Impact of prior
Do you believe in ctr > 0.9 ? Or > 0.8? Or 0?
You have your p(ctr) in mind, use it!
• You iterate quicker
• Accept small improvements
• Fewer problems of continuous monitoring
Do not use overconfident prior, choose slightly pessimistic one
Choose smarter loss (”accept improvement at least δ”):
Loss(α, β, δ, x) =
max((β − δ) − α, 0), x = A
max(α − (β − δ), 0), x = B
Informative prior
Non-Informative
prior
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 18 / 29
Real(!) Impact of prior
Do you believe in ctr > 0.9 ? Or > 0.8? Or 0?
You have your p(ctr) in mind, use it!
• You iterate quicker
• Accept small improvements
• Fewer problems of continuous monitoring
Do not use overconfident prior, choose slightly pessimistic one
Choose smarter loss (”accept improvement at least δ”):
Loss(α, β, δ, x) =
max((β − δ) − α, 0), x = A
max(α − (β − δ), 0), x = B
Convert loss into $$$
Informative prior
Non-Informative
prior
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 18 / 29
Portfolio
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 19 / 29
Finance (Quantopian) case
Hedge-Fund in Boston
• Crowd sourcing for trade strategies, more that 700k
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 20 / 29
Finance (Quantopian) case
Hedge-Fund in Boston
• Crowd sourcing for trade strategies, more that 700k
• People tend to overfit the leaderboard, that’s ok
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 20 / 29
Finance (Quantopian) case
Hedge-Fund in Boston
• Crowd sourcing for trade strategies, more that 700k
• People tend to overfit the leaderboard, that’s ok
• But they do not have out of sample data
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 20 / 29
Finance (Quantopian) case
Hedge-Fund in Boston
• Crowd sourcing for trade strategies, more that 700k
• People tend to overfit the leaderboard, that’s ok
• But they do not have out of sample data
• Data changes over time, we should take care of it
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 20 / 29
Finance (Quantopian) case
Hedge-Fund in Boston
• Crowd sourcing for trade strategies, more that 700k
• People tend to overfit the leaderboard, that’s ok
• But they do not have out of sample data
• Data changes over time, we should take care of it
How to link 2 periods of algorithm evaluation to use more data?
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 20 / 29
Finance (Quantopian) case
Core ideas
• Use Gaussian process to capture changing
volatility and return in time
Figure: Gaussian Process
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 21 / 29
Finance (Quantopian) case
Core ideas
• Use Gaussian process to capture changing
volatility and return in time
• Allow structural changes in the model
• mean returns
• but not volatility
Figure: Gaussian Process
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 21 / 29
Finance (Quantopian) case
Core ideas
• Use Gaussian process to capture changing
volatility and return in time
• Allow structural changes in the model
• mean returns
• but not volatility
• Use posterior model returns to optimize
expected risk-return objective
Figure: Risk vs Returns
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 21 / 29
Finance (Quantopian) case
Figure: Sharpe Ratio
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 22 / 29
Supply chains
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 23 / 29
year 3019, PyMC X
Supply PyMC X Demand Mars
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 24 / 29
Supply
Business Effect / Observed
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 25 / 29
Demand
∼100 /month
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 26 / 29
Costs and Profits
Profit ∼ Stock | Demand Profit ∼ Demand | Stock
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 27 / 29
Costs and Profits
Profit ∼ Stock | Demand Profit ∼ Demand | Stock
How to combine them?
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 27 / 29
Expected Loss Framework
Profits: Frequentist vs Bayesian
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 28 / 29
Take outs
Bayesian Framework allows:
• effectively use your prior knowledge
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 29 / 29
Take outs
Bayesian Framework allows:
• effectively use your prior knowledge
• take uncertainty in account
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 29 / 29
Take outs
Bayesian Framework allows:
• effectively use your prior knowledge
• take uncertainty in account
• understand your data / problem better
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 29 / 29
Take outs
Bayesian Framework allows:
• effectively use your prior knowledge
• take uncertainty in account
• understand your data / problem better
BUT, you should really understand your problem
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 29 / 29
Take outs
Bayesian Framework allows:
• effectively use your prior knowledge
• take uncertainty in account
• understand your data / problem better
BUT, you should really understand your problem
Tutorials/MISC:
pymc3 docs: https://docs.pymc.io/nb_tutorials/index.html
case studies: https://twiecki.io
Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 29 / 29

More Related Content

PDF
AIC x PyLadies TW Python Data Vis - 2: Plot packages
PDF
Mapping Lo Dto Proton Revised [Compatibility Mode]
PDF
How elephants survive in big data environments
PDF
Why-Not Provenance through Game Semantics
PDF
MULDER: Querying the Linked Data Web by Bridging RDF Molecule Templates
PPTX
Learning to assess Linked Data relationships using Genetic Programming
PDF
私は如何にして心配するのを止めてPyTorchを愛するようになったか
PDF
Introduction to Bayesian Inference
AIC x PyLadies TW Python Data Vis - 2: Plot packages
Mapping Lo Dto Proton Revised [Compatibility Mode]
How elephants survive in big data environments
Why-Not Provenance through Game Semantics
MULDER: Querying the Linked Data Web by Bridging RDF Molecule Templates
Learning to assess Linked Data relationships using Genetic Programming
私は如何にして心配するのを止めてPyTorchを愛するようになったか
Introduction to Bayesian Inference

Similar to PyMC3 — Bayesian Statistical Modelling in Python, Максим Кочуров. 22 июня, 2019 (20)

ODP
Gentle Introduction: Bayesian Modelling and Probabilistic Programming in R
PDF
Maxwell W Libbrecht - pomegranate: fast and flexible probabilistic modeling i...
PDF
[P.D.F] Bayesian Methods for Hackers: Probabilistic Programming and Bayesian ...
PDF
Introduction to Bayesian Analysis in Python
PDF
Striving to Demystify Bayesian Computational Modelling
PDF
Probably, Definitely, Maybe
PPT
Introduction to Bayesian Statistics.ppt
PPTX
Into to prob_prog_hari (2)
PDF
Likelihood free computational statistics
PDF
Pittsburgh and Toronto "Halloween US trip" seminars
PDF
estaditica aplicar
PDF
Bayesian networks
PPT
PDF
naive bayes example.pdf
PDF
naive bayes example.pdf
ODP
Introduction to Bayesian Statistics
PDF
QMC Program: Trends and Advances in Monte Carlo Sampling Algorithms Workshop,...
PDF
data-microscopes
PPTX
Bayesian Reasoning and Learning
PDF
Complex models in ecology: challenges and solutions
Gentle Introduction: Bayesian Modelling and Probabilistic Programming in R
Maxwell W Libbrecht - pomegranate: fast and flexible probabilistic modeling i...
[P.D.F] Bayesian Methods for Hackers: Probabilistic Programming and Bayesian ...
Introduction to Bayesian Analysis in Python
Striving to Demystify Bayesian Computational Modelling
Probably, Definitely, Maybe
Introduction to Bayesian Statistics.ppt
Into to prob_prog_hari (2)
Likelihood free computational statistics
Pittsburgh and Toronto "Halloween US trip" seminars
estaditica aplicar
Bayesian networks
naive bayes example.pdf
naive bayes example.pdf
Introduction to Bayesian Statistics
QMC Program: Trends and Advances in Monte Carlo Sampling Algorithms Workshop,...
data-microscopes
Bayesian Reasoning and Learning
Complex models in ecology: challenges and solutions
Ad

More from Mail.ru Group (20)

PDF
Автоматизация без тест-инженеров по автоматизации, Мария Терехина и Владислав...
PDF
BDD для фронтенда. Автоматизация тестирования с Cucumber, Cypress и Jenkins, ...
PDF
Другая сторона баг-баунти-программ: как это выглядит изнутри, Владимир Дубровин
PDF
Использование Fiddler и Charles при тестировании фронтенда проекта pulse.mail...
PDF
Управление инцидентами в Почте Mail.ru, Антон Викторов
PDF
DAST в CI/CD, Ольга Свиридова
PDF
Почему вам стоит использовать свой велосипед и почему не стоит Александр Бел...
PDF
CV в пайплайне распознавания ценников товаров: трюки и хитрости Николай Масл...
PDF
RAPIDS: ускоряем Pandas и scikit-learn на GPU Павел Клеменков, NVidia
PDF
WebAuthn в реальной жизни, Анатолий Остапенко
PDF
AMP для электронной почты, Сергей Пешков
PDF
Как мы захотели TWA и сделали его без мобильных разработчиков, Данила Стрелков
PDF
Кейсы использования PWA для партнерских предложений в Delivery Club, Никита Б...
PDF
Метапрограммирование: строим конечный автомат, Сергей Федоров, Яндекс.Такси
PDF
Как не сделать врагами архитектуру и оптимизацию, Кирилл Березин, Mail.ru Group
PDF
Этика искусственного интеллекта, Александр Кармаев (AI Journey)
PDF
Нейро-машинный перевод в вопросно-ответных системах, Федор Федоренко (AI Jour...
PDF
Конвергенция технологий как тренд развития искусственного интеллекта, Владими...
PDF
Обзор трендов рекомендательных систем от Пульса, Андрей Мурашев (AI Journey)
PDF
Мир глазами нейросетей, Данила Байгушев, Александр Сноркин ()
Автоматизация без тест-инженеров по автоматизации, Мария Терехина и Владислав...
BDD для фронтенда. Автоматизация тестирования с Cucumber, Cypress и Jenkins, ...
Другая сторона баг-баунти-программ: как это выглядит изнутри, Владимир Дубровин
Использование Fiddler и Charles при тестировании фронтенда проекта pulse.mail...
Управление инцидентами в Почте Mail.ru, Антон Викторов
DAST в CI/CD, Ольга Свиридова
Почему вам стоит использовать свой велосипед и почему не стоит Александр Бел...
CV в пайплайне распознавания ценников товаров: трюки и хитрости Николай Масл...
RAPIDS: ускоряем Pandas и scikit-learn на GPU Павел Клеменков, NVidia
WebAuthn в реальной жизни, Анатолий Остапенко
AMP для электронной почты, Сергей Пешков
Как мы захотели TWA и сделали его без мобильных разработчиков, Данила Стрелков
Кейсы использования PWA для партнерских предложений в Delivery Club, Никита Б...
Метапрограммирование: строим конечный автомат, Сергей Федоров, Яндекс.Такси
Как не сделать врагами архитектуру и оптимизацию, Кирилл Березин, Mail.ru Group
Этика искусственного интеллекта, Александр Кармаев (AI Journey)
Нейро-машинный перевод в вопросно-ответных системах, Федор Федоренко (AI Jour...
Конвергенция технологий как тренд развития искусственного интеллекта, Владими...
Обзор трендов рекомендательных систем от Пульса, Андрей Мурашев (AI Journey)
Мир глазами нейросетей, Данила Байгушев, Александр Сноркин ()
Ad

Recently uploaded (20)

PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
history of c programming in notes for students .pptx
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
medical staffing services at VALiNTRY
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
top salesforce developer skills in 2025.pdf
PPTX
L1 - Introduction to python Backend.pptx
PPTX
assetexplorer- product-overview - presentation
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Introduction to Artificial Intelligence
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Designing Intelligence for the Shop Floor.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
Operating system designcfffgfgggggggvggggggggg
history of c programming in notes for students .pptx
Design an Analysis of Algorithms I-SECS-1021-03
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
medical staffing services at VALiNTRY
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
top salesforce developer skills in 2025.pdf
L1 - Introduction to python Backend.pptx
assetexplorer- product-overview - presentation
Understanding Forklifts - TECH EHS Solution
Introduction to Artificial Intelligence
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Digital Systems & Binary Numbers (comprehensive )
Which alternative to Crystal Reports is best for small or large businesses.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Nekopoi APK 2025 free lastest update
2025 Textile ERP Trends: SAP, Odoo & Oracle
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Designing Intelligence for the Shop Floor.pdf

PyMC3 — Bayesian Statistical Modelling in Python, Максим Кочуров. 22 июня, 2019

  • 1. PyMC3 – Bayesian Statistical Modelling in Python Max Kochurov 22 June, 2019 Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 1 / 29
  • 2. About me Max Kochurov tg,slack:@ferres / github:ferrine Geoopt Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 2 / 29
  • 3. Bayesian Statistics Figure: Updating prior p(λ) p(λ | D) = p(D | λ)p(λ) p(D) • p(λ) – Prior, base knowledge • p(D | λ) – Likelihood, new information • p(λ | D) – Posterior, updated knowledge • p(D) – Evidence, surprise in data Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 3 / 29
  • 4. Bayesian Statistics p(λ | D) = p(D | λ)p(λ) p(D) • p(λ) – Prior, base knowledge • p(D | λ) – Likelihood, new information • p(λ | D) – Posterior, updated knowledge • p(D) – Evidence, surprise in data Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 3 / 29
  • 5. Bayesian Statistics Compared to Frequentist • Elegant way to put assumptions in the model • Can work with few Data! • No need for p-values, all you need is p(λ | D) • . . . p(λ | D) = p(D | λ)p(λ) p(D) • p(λ) – Prior, base knowledge • p(D | λ) – Likelihood, new information • p(λ | D) – Posterior, updated knowledge • p(D) – Evidence, surprise in data Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 3 / 29
  • 6. Where do people use Bayesian inference? • Finance (Quantopian): estimating the performance of trading algorithms Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
  • 7. Where do people use Bayesian inference? • Finance (Quantopian): estimating the performance of trading algorithms • Music Streaming (Sounds): A/B testing, churn prediction, lifetime value, defining user session Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
  • 8. Where do people use Bayesian inference? • Finance (Quantopian): estimating the performance of trading algorithms • Music Streaming (Sounds): A/B testing, churn prediction, lifetime value, defining user session • E-Commerce (Salesforce): A/B tests, combining disparate sources of information, hierarchical models Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
  • 9. Where do people use Bayesian inference? • Finance (Quantopian): estimating the performance of trading algorithms • Music Streaming (Sounds): A/B testing, churn prediction, lifetime value, defining user session • E-Commerce (Salesforce): A/B tests, combining disparate sources of information, hierarchical models • Astronomy: estimating orbits of space objects, getting an image of a black hole Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
  • 10. Where do people use Bayesian inference? • Finance (Quantopian): estimating the performance of trading algorithms • Music Streaming (Sounds): A/B testing, churn prediction, lifetime value, defining user session • E-Commerce (Salesforce): A/B tests, combining disparate sources of information, hierarchical models • Astronomy: estimating orbits of space objects, getting an image of a black hole • Life science: epidemic analysis Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
  • 11. Where do people use Bayesian inference? • Finance (Quantopian): estimating the performance of trading algorithms • Music Streaming (Sounds): A/B testing, churn prediction, lifetime value, defining user session • E-Commerce (Salesforce): A/B tests, combining disparate sources of information, hierarchical models • Astronomy: estimating orbits of space objects, getting an image of a black hole • Life science: epidemic analysis • Medicine: calculating effect sizes for drugs Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
  • 12. Where do people use Bayesian inference? • Finance (Quantopian): estimating the performance of trading algorithms • Music Streaming (Sounds): A/B testing, churn prediction, lifetime value, defining user session • E-Commerce (Salesforce): A/B tests, combining disparate sources of information, hierarchical models • Astronomy: estimating orbits of space objects, getting an image of a black hole • Life science: epidemic analysis • Medicine: calculating effect sizes for drugs • . . . Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
  • 13. Where do people use Bayesian inference? • Finance (Quantopian): estimating the performance of trading algorithms • Music Streaming (Sounds): A/B testing, churn prediction, lifetime value, defining user session • E-Commerce (Salesforce): A/B tests, combining disparate sources of information, hierarchical models • Astronomy: estimating orbits of space objects, getting an image of a black hole • Life science: epidemic analysis • Medicine: calculating effect sizes for drugs • . . . Some will be covered later Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 4 / 29
  • 14. Coin Example Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 5 / 29
  • 15. Fair Coin Flips – expect fair p ∼ Beta(3, 3) flips ∼ Binomial(N, p) Data: 12 out of 20 flips What is p? Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 6 / 29
  • 16. Fair Coin Flips – expect fair p ∼ Beta(3, 3) flips ∼ Binomial(N, p) Data: 12 out of 20 flips What is p? Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 6 / 29
  • 17. Fair Coin Flips – expect fair p ∼ Beta(3, 3) flips ∼ Binomial(N, p) Data: 12 out of 20 flips What is p? We expect an angel and see an angel, we are confident Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 6 / 29
  • 18. Fair Coin Flips – expect unfair What is p? p ∼ Beta(1, 5) flips ∼ Binomial(N, p) Data: 12 out of 20 flips Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 7 / 29
  • 19. Fair Coin Flips – expect unfair What is p? p ∼ Beta(1, 5) flips ∼ Binomial(N, p) Data: 12 out of 20 flips Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 7 / 29
  • 20. Fair Coin Flips – expect unfair What is p? p ∼ Beta(1, 5) flips ∼ Binomial(N, p) Data: 12 out of 20 flips We expect a devil but see an angel, we are less confident Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 7 / 29
  • 21. Hierarchical Models What if we have more diverse data? We can estimate an amount of devils + uncertainty! Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 8 / 29
  • 22. Hierarchical Models What if we have more diverse data? We can estimate an amount of devils + uncertainty! Data: [8, 2, 2, 10, 0, 0, 2, 5, 6, 10] out of 20 flips λDevil ∼ Exponential(1) λAngel ∼ Exponential(1) pi ∼ Beta(λAngel , λAngel + λDevil ) flipsi ∼ Binomial(pi , Ni ) Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 8 / 29
  • 23. Hierarchical Models What if we have more diverse data? We can estimate an amount of devils + uncertainty! Data: [8, 2, 2, 10, 0, 0, 2, 5, 6, 10] out of 20 flips λDevil ∼ Exponential(1) λAngel ∼ Exponential(1) pi ∼ Beta(λAngel , λAngel + λDevil ) flipsi ∼ Binomial(pi , Ni ) Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 8 / 29
  • 24. Markov Chain Monte Carlo (MCMC) & Variational Inference Few simple rules • Try MCMC first • If it is slow: wait a bit • Got tired: try VI • Didn’t work: feel sad Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 9 / 29
  • 25. Markov Chain Monte Carlo (MCMC) & Variational Inference Few simple rules • Try MCMC first • If it is slow: wait a bit • Got tired: try VI • Didn’t work: feel sad Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 9 / 29
  • 26. Markov Chain Monte Carlo (MCMC) & Variational Inference Few simple rules • Try MCMC first • If it is slow: wait a bit • Got tired: try VI • Didn’t work: feel sad Did not work for you? goto https://discourse.pymc.io Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 9 / 29
  • 27. Inspecting Your Model What is an amount of devils? (MCMC used) pm.traceplot(trace) 0 1 2 3 4 5 6 7 8 0.0 0.2 0.4 Frequency devils 0 100 200 300 400 500 0.0 2.5 5.0 7.5 Samplevalue devils 0.5 1.0 1.5 2.0 2.5 0.0 0.5 1.0 Frequency angels 0 100 200 300 400 500 1 2 Samplevalue angels 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0 10 20 Frequency p 0 100 200 300 400 500 0.00 0.25 0.50 0.75 Samplevalue p Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 10 / 29
  • 28. Inspecting Your Model plt.hist(trace["angels"], alpha=.8, label='Alpha') plt.hist(trace["angels"]+trace["devils"], alpha=.8, label='Beta') plt.legend(fontsize=20) 0 1 2 3 4 5 6 7 8 0 50 100 150 200 250 300 Alpha Beta Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 11 / 29
  • 29. Inspecting Your Model 0.0 0.2 0.4 0.6 0.8 1.0 0 1 2 3 4 5 6 7 Prob Distribution for a, b in zip( trace["angels"], trace["angels"]+trace["devils"] ): plt.plot( np.linspace(0, 1), st.beta(a, b).pdf(np.linspace(0, 1)), color="b", alpha=.025 ) a_mean = trace["angels"].mean() b_mean = (trace["angels"]+trace["devils"]).mean() plt.plot( np.linspace(0, 1), st.beta(a_mean, b_mean).pdf(np.linspace(0, 1)), color="black", linewidth=4.0 ) plt.axvline(0.5) plt.title("Prob Distribution", fontsize=20) Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 12 / 29
  • 30. Case Studies Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 13 / 29
  • 31. A/B testing Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 14 / 29
  • 32. A/B testing, why Bayes We would accept that result 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 p-val=0.2398diff=0.1 diff=0.1 pval=.24 Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 15 / 29
  • 33. A/B testing, why Bayes We would accept that result 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 p-val=0.2398diff=0.1 diff=0.1 pval=.24 We wait too long to check small diff 0.0 0.2 0.4 0.6 0.8 1.0 0 5 10 15 20 25 30 35 40 p-val=0.2398diff=0.01 diff=0.01 pval=.24 Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 15 / 29
  • 34. A/B testing, why Bayes We would accept that result 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 p-val=0.2398diff=0.1 diff=0.1 pval=.24 We wait too long to check small diff 0.0 0.2 0.4 0.6 0.8 1.0 0 5 10 15 20 25 30 35 40 p-val=0.2398diff=0.01 diff=0.01 pval=.24 WTF? 0.0 0.2 0.4 0.6 0.8 1.0 0 5 10 15 20 25 30 35 40 p-val=0.0786diff=0.02 diff=0.02 pval=0.07 Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 15 / 29
  • 35. Expected Loss Framework p-values are useless. What about calculating a loss of ignoring a better model? • We now decide to use model A or B Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 16 / 29
  • 36. Expected Loss Framework p-values are useless. What about calculating a loss of ignoring a better model? • We now decide to use model A or B • A has some α ctr, B has some β ctr Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 16 / 29
  • 37. Expected Loss Framework p-values are useless. What about calculating a loss of ignoring a better model? • We now decide to use model A or B • A has some α ctr, B has some β ctr • Say a loss (can be any) to ignore a better model given we use x Loss(α, β, x) = max(β − α, 0), x = A max(α − β, 0), x = B Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 16 / 29
  • 38. Expected Loss Framework p-values are useless. What about calculating a loss of ignoring a better model? • We now decide to use model A or B • A has some α ctr, B has some β ctr • Say a loss (can be any) to ignore a better model given we use x Loss(α, β, x) = max(β − α, 0), x = A max(α − β, 0), x = B • Loss depends on the magnitude of difference (p-values do not) Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 16 / 29
  • 39. Expected Loss Framework L(x) = Ep(α,β)Loss(α, β, x) → min x Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 17 / 29
  • 40. Expected Loss Framework L(x) = Ep(α,β)Loss(α, β, x) → min x 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 p-val=0.2398diff=0.1 L(A) = 0.122 L(B) = 0.019 Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 17 / 29
  • 41. Expected Loss Framework L(x) = Ep(α,β)Loss(α, β, x) → min x 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 p-val=0.2398diff=0.1 L(A) = 0.122 L(B) = 0.019 0.0 0.2 0.4 0.6 0.8 1.0 0 5 10 15 20 25 30 35 40 p-val=0.2398diff=0.01 L(A) = 0.012 L(B) = 0.002 Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 17 / 29
  • 42. Expected Loss Framework L(x) = Ep(α,β)Loss(α, β, x) → min x 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 p-val=0.2398diff=0.1 L(A) = 0.122 L(B) = 0.019 0.0 0.2 0.4 0.6 0.8 1.0 0 5 10 15 20 25 30 35 40 p-val=0.2398diff=0.01 L(A) = 0.012 L(B) = 0.002 0.0 0.2 0.4 0.6 0.8 1.0 0 5 10 15 20 25 30 35 40 p-val=0.0786diff=0.02 L(A) = 0.02 L(B) = 0.0005 Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 17 / 29
  • 43. Expected Loss Framework L(x) = Ep(α,β)Loss(α, β, x) → min x 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 p-val=0.2398diff=0.1 L(A) = 0.122 L(B) = 0.019 0.0 0.2 0.4 0.6 0.8 1.0 0 5 10 15 20 25 30 35 40 p-val=0.2398diff=0.01 L(A) = 0.012 L(B) = 0.002 0.0 0.2 0.4 0.6 0.8 1.0 0 5 10 15 20 25 30 35 40 p-val=0.0786diff=0.02 L(A) = 0.02 L(B) = 0.0005 Without p-values we stop first experiment earlier as B is a clear winner Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 17 / 29
  • 44. Real(!) Impact of prior Do you believe in ctr > 0.9 ? Or > 0.8? Or 0? Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 18 / 29
  • 45. Real(!) Impact of prior Do you believe in ctr > 0.9 ? Or > 0.8? Or 0? You have your p(ctr) in mind, use it! Informative prior Non-Informative prior Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 18 / 29
  • 46. Real(!) Impact of prior Do you believe in ctr > 0.9 ? Or > 0.8? Or 0? You have your p(ctr) in mind, use it! • You iterate quicker • Accept small improvements • Fewer problems of continuous monitoring Informative prior Non-Informative prior Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 18 / 29
  • 47. Real(!) Impact of prior Do you believe in ctr > 0.9 ? Or > 0.8? Or 0? You have your p(ctr) in mind, use it! • You iterate quicker • Accept small improvements • Fewer problems of continuous monitoring Do not use overconfident prior, choose slightly pessimistic one Informative prior Non-Informative prior Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 18 / 29
  • 48. Real(!) Impact of prior Do you believe in ctr > 0.9 ? Or > 0.8? Or 0? You have your p(ctr) in mind, use it! • You iterate quicker • Accept small improvements • Fewer problems of continuous monitoring Do not use overconfident prior, choose slightly pessimistic one Choose smarter loss (”accept improvement at least δ”): Loss(α, β, δ, x) = max((β − δ) − α, 0), x = A max(α − (β − δ), 0), x = B Informative prior Non-Informative prior Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 18 / 29
  • 49. Real(!) Impact of prior Do you believe in ctr > 0.9 ? Or > 0.8? Or 0? You have your p(ctr) in mind, use it! • You iterate quicker • Accept small improvements • Fewer problems of continuous monitoring Do not use overconfident prior, choose slightly pessimistic one Choose smarter loss (”accept improvement at least δ”): Loss(α, β, δ, x) = max((β − δ) − α, 0), x = A max(α − (β − δ), 0), x = B Convert loss into $$$ Informative prior Non-Informative prior Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 18 / 29
  • 50. Portfolio Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 19 / 29
  • 51. Finance (Quantopian) case Hedge-Fund in Boston • Crowd sourcing for trade strategies, more that 700k Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 20 / 29
  • 52. Finance (Quantopian) case Hedge-Fund in Boston • Crowd sourcing for trade strategies, more that 700k • People tend to overfit the leaderboard, that’s ok Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 20 / 29
  • 53. Finance (Quantopian) case Hedge-Fund in Boston • Crowd sourcing for trade strategies, more that 700k • People tend to overfit the leaderboard, that’s ok • But they do not have out of sample data Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 20 / 29
  • 54. Finance (Quantopian) case Hedge-Fund in Boston • Crowd sourcing for trade strategies, more that 700k • People tend to overfit the leaderboard, that’s ok • But they do not have out of sample data • Data changes over time, we should take care of it Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 20 / 29
  • 55. Finance (Quantopian) case Hedge-Fund in Boston • Crowd sourcing for trade strategies, more that 700k • People tend to overfit the leaderboard, that’s ok • But they do not have out of sample data • Data changes over time, we should take care of it How to link 2 periods of algorithm evaluation to use more data? Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 20 / 29
  • 56. Finance (Quantopian) case Core ideas • Use Gaussian process to capture changing volatility and return in time Figure: Gaussian Process Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 21 / 29
  • 57. Finance (Quantopian) case Core ideas • Use Gaussian process to capture changing volatility and return in time • Allow structural changes in the model • mean returns • but not volatility Figure: Gaussian Process Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 21 / 29
  • 58. Finance (Quantopian) case Core ideas • Use Gaussian process to capture changing volatility and return in time • Allow structural changes in the model • mean returns • but not volatility • Use posterior model returns to optimize expected risk-return objective Figure: Risk vs Returns Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 21 / 29
  • 59. Finance (Quantopian) case Figure: Sharpe Ratio Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 22 / 29
  • 60. Supply chains Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 23 / 29
  • 61. year 3019, PyMC X Supply PyMC X Demand Mars Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 24 / 29
  • 62. Supply Business Effect / Observed Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 25 / 29
  • 63. Demand ∼100 /month Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 26 / 29
  • 64. Costs and Profits Profit ∼ Stock | Demand Profit ∼ Demand | Stock Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 27 / 29
  • 65. Costs and Profits Profit ∼ Stock | Demand Profit ∼ Demand | Stock How to combine them? Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 27 / 29
  • 66. Expected Loss Framework Profits: Frequentist vs Bayesian Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 28 / 29
  • 67. Take outs Bayesian Framework allows: • effectively use your prior knowledge Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 29 / 29
  • 68. Take outs Bayesian Framework allows: • effectively use your prior knowledge • take uncertainty in account Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 29 / 29
  • 69. Take outs Bayesian Framework allows: • effectively use your prior knowledge • take uncertainty in account • understand your data / problem better Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 29 / 29
  • 70. Take outs Bayesian Framework allows: • effectively use your prior knowledge • take uncertainty in account • understand your data / problem better BUT, you should really understand your problem Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 29 / 29
  • 71. Take outs Bayesian Framework allows: • effectively use your prior knowledge • take uncertainty in account • understand your data / problem better BUT, you should really understand your problem Tutorials/MISC: pymc3 docs: https://docs.pymc.io/nb_tutorials/index.html case studies: https://twiecki.io Max Kochurov PyMC3 – Bayesian Statistical Modelling in Python PyData – Moscow 29 / 29