This summer, during the third edition of Data Science Summit in Warsaw, Magdalena Wójcik (Senior Data Scientist at LogicAI) presented how we used Bayesian models in one of our projects.
2. Content of the presentation
1) What is the Bayesian approach?
2) Profits of going Bayesian
3) Recap of distributions
4) Toolbox for a Bayesian Hacker
5) Case Study #1 - Price-demand change analysis
6) Case Study #2 - Hierarchical modeling
4. What is the Bayesian approach?
Thomas Bayes - XVIII century mathematician who
interpreted probability as the degree of belief, and
not the simple frequency of events.
5. Bayes’ Theorem
Posterior probability
of A given evidence B
Prior
probability
of A
Likelihood of
collecting evidence B
when A is true
Probability of
collecting B under all
circumstances
6. Mandatory naive example 🤒
What is the probability of Bob having a relatively rare disease,
given that he received positive result from the medical test?
Disease occurs in 1 / 250 people. Test has True Positive Rate of 0.99.
7. Mandatory naive example 🤒
What is the probability of Bob having a relatively rare disease,
given that he received positive result from the medical test?
Disease occurs in 1 / 250 people. Test has True Positive Rate of 0.99.
0.99
8. Mandatory naive example 🤒
What is the probability of Bob having a relatively rare disease,
given that he received positive result from the medical test?
Disease occurs in 1 / 250 people. Test has True Positive Rate of 0.99.
0.004
0.99
9. Mandatory naive example 🤒
What is the probability of Bob having a relatively rare disease,
given that he received positive result from the medical test?
Disease occurs in 1 / 250 people. Test has True Positive Rate of 0.99.
0.004
0.99
0.99×1 + 0.01×249
250
10. Mandatory naive example 🤒
What is the probability of Bob having a relatively rare disease,
given that he received positive result from the medical test?
Disease occurs in 1 / 250 people. Test has True Positive Rate of 0.99.
0.004
0.99
0.28
0.99×1 + 0.01×249
250
11. Bayes’ Theorem in 🔍 of Data Scientist
Posterior distribution
Our updated belief
Prior belief as a
distribution
Collected data
Constant normalization term
12. Profits of going Bayesian
● Instead of one value, we get a distribution of likely values.
● We get information on certainty of model output.
● More ways to compare outcomes.
● Spot for expert knowledge already incorporated in the model.
● Easy way to include external knowledge when data set is small.
13. It’s where we put our beliefs. So we choose the distribution wisely:
1. Empirically - we have already done some experiments and have actual data,
2. With expertise - we have expert domain knowledge on the subject,
3. With intent - we have reasons to prefer some values over the others,
4. YOLO - we have no idea and wouldn’t like to affect the outcome.
Choosing prior distribution
14. Uniform distribution
all possible values fall between minimum and
maximum bounds and have equal likelihood.
Conditions:
1. The minimum value is fixed.
2. The maximum value is fixed.
3. All values between the minimum and
maximum occur with equal likelihood.
Recap of distributions
15. Normal distribution
The most common distribution, which has
3 properties:
1. Some value (mean of the
distribution) is the most likely.
2. The uncertain variable could as
likely be above the mean as it could
be below the mean (symmetrical
about the mean).
3. The uncertain variable is more
likely to be in the vicinity of the
mean than further away.
Recap of distributions
16. Poisson distribution
describes the number of times an event occurs in a given
interval.
Conditions:
1. The number of possible occurrences in any
interval is unlimited.
2. The occurrences are independent. The number of
occurrences in one interval does not affect the
number of occurrences in other intervals.
3. The average number of occurrences must remain
the same from interval to interval
Recap of distributions
17. Gamma distribution
The gamma distribution is most often used as the
distribution of the amount of time until the rth
occurrence of an event in a Poisson process.
Conditions:
1. The number of possible occurrences in any unit
of measurement is not limited to a fixed
number.
2. The occurrences are independent.
3. The average number of occurrences must
remain the same from unit to unit.
Recap of distributions
19. Bayesian Hacker’s Toolbox - sampling!
When there is no closed-form solution, we can approximate with sampling, using
technique named MCMC (Markov Chain Monte Carlo).
PyMC gives us also a set of pre-defined distributions.
20. Markov Chain Monte Carlo - easy explanation!
1. Generate random guesses (Monte Carlo part)
2. Generate next generation guesses based only on the guesses before that.
(Markov Chain). Fun fact: this property defines that those chains are memoryless.
3. Accept new generation guesses if they “moving in the right direction”,
otherwise reject them.
22. Case Study #1: Price-Demand change analysis
We know when we changed the price for a particular product.
What we don’t know are:
● Was this price change noticeable (enough to change the demand)?
● When the price change affected the demand change?
● Can models detect this from the data?
23. Price change effect on demand
Price $99.98 Price $79.98
Real-world examples are often noisy and concern thousands of products at once. Below is an example of the
noisy plot.
24. Price change effect on demand
Price $99.98 Price $79.98
We’ll use a simplistic example for this demonstration.
25. First steps: sales distribution
1. Define the distribution of the data:
We want to predict sales - what can be the distribution?
Normal Exponential Cauchy Zero Inflated Binomial
Uniform Gamma Geometric Flat
Binomial Beta Minimum extreme Half Flat
Poisson Lognormal Negative binomial Logistic
Weibull Student’s t Discrete uniform Negative Normal
26. First steps: sales distribution
1. Define the distribution of the data:
We want to predict sales - what can be the distribution?
Normal Exponential Cauchy Zero Inflated Binomial
Uniform Gamma Geometric Flat
Binomial Beta Minimum extreme Half Flat
Poisson Lognormal Negative binomial Logistic
Weibull Student’s t Discrete uniform Negative Normal
Way of thinking
● Discrete
27. First steps: sales distribution
1. Define the distribution of the data:
We want to predict sales - what can be the distribution?
Normal Exponential Cauchy Zero Inflated Binomial
Uniform Gamma Geometric Flat
Binomial Beta Minimum extreme Half Flat
Poisson Lognormal Negative binomial Logistic
Weibull Student’s t Discrete uniform Negative Normal
Way of thinking
● Discrete
● More than 3 unique values
28. First steps: sales distribution
1. Define the distribution of the data:
We want to predict sales - what can be the distribution?
Normal Exponential Cauchy Zero Inflated Binomial
Uniform Gamma Geometric Flat
Binomial Beta Minimum extreme Half Flat
Poisson Lognormal Negative binomial Logistic
Weibull Student’s t Discrete uniform Negative Normal
Way of thinking
● Discrete
● More than 3 unique values
Shows number of trials
until the success is
achieved
Number of times event is
occurred during the time
interval
29. First steps: λ distribution
1. We choose the Poisson distribution: Poisson (λ)
2. Now we must define the distribution of the λ
Normal Exponential Cauchy Zero Inflated Binomial
Uniform Gamma Geometric Flat
Binomial Beta Minimum extreme Half Flat
Poisson Lognormal Negative binomial Logistic
Weibull Student’s t Discrete uniform Negative Normal
30. First steps: λ distribution
1. We choose the Poisson distribution: Poisson (λ)
2. Now we must define the distribution of the λ
Normal Exponential Cauchy Zero Inflated Binomial
Uniform Gamma Geometric Flat
Binomial Beta Minimum extreme Half Flat
Poisson Lognormal Negative binomial Logistic
Weibull Student’s t Discrete uniform Negative Normal
Way of thinking
● Continuous
31. First steps: λ distribution
1. We choose the Poisson distribution: Poisson (λ)
2. Now we must define the distribution of the λ
Normal Exponential Cauchy Zero Inflated Binomial
Uniform Gamma Geometric Flat
Binomial Beta Minimum extreme Half Flat
Poisson Lognormal Negative binomial Logistic
Weibull Student’s t Discrete uniform Negative Normal
Way of thinking
● Continuous
● Positive
32. First steps: λ distribution
1. We choose the Poisson distribution: Poisson (λ)
2. Now we must define the distribution of the λ
Normal Exponential Cauchy Zero Inflated Binomial
Uniform Gamma Geometric Flat
Binomial Beta Minimum extreme Half Flat
Poisson Lognormal Negative binomial Logistic
Weibull Student’s t Discrete uniform Negative Normal
Way of thinking
● Continuous
● Positive
● Time related
33. First steps: λ distribution
1. We choose the Poisson distribution: Poisson (λ)
2. Now we must define the distribution of the λ
Normal Exponential Cauchy Zero Inflated Binomial
Uniform Gamma Geometric Flat
Binomial Beta Minimum extreme Half Flat
Poisson Lognormal Negative binomial Logistic
Weibull Student’s t Discrete uniform Negative Normal
Way of thinking
● Continuous
● Positive
● Time related
Event can occur at
any time randomly
Event is more/less
likely occur over
time
Event can occur at
any time not
necessarily
randomly
35. Now we build the model
2. Check the results!
On 16th day
the demand
changed
36. Conclusions 🤔 ?
1. For some products the change may not be
noticeable.
2. For some products the change can be easily spotted.
Why?
How much the price
changed?
Dig deeper!
37. Case Study #2: Price elasticity of demand 💸
Calculate Price Elasticity of Demand
for all products in store. Except some
of them are very new and for some
price almost never changed.
38. Case Study #2: Hierarchical model 🏔
Hierarchical models allows you to define the parameters taking to the account
group means, using the informations about products similarity.
Group level
Product level
39. Shrinkage
Products are “pulled” towards the group
mean.
Why is it important?
If you have few data points or a new
product, you can leverage this facts by using
knowledge from the group mean.
source
40. The simple model ⛰
1. Distribution of the target - sales
(Your target is λ = 𝑤*x +𝑤)
2. Distribution on the group’s level:
a. 𝑤′1
b. 𝑤′0
3. Distributions on products level:
a. 𝑤1
b. 𝑤0
1. Poisson (λ)
2.
a. Normal ( 𝛍, 𝛔)
b. Normal ( 𝛍, 𝛔)
3.
a. Group’s 𝛍, 𝛔
b. Group’s 𝛍, 𝛔
Let’s build the model!
43. Performance - explained
Mean - average of distribution
SD - standard deviation of distribution
mc_error - standard error of posterior sample mean as estimate of theoretical expectation for given
parameter. Rule of thumb: want MC error < 1 − 5% of posterior SD
hpd_2.5 & hpd_97.5 - 2.5% and 97.5% percentiles of the posterior samples for each parameter give a 95%
posterior credible interval
N_eff - number of effective samples. Rule of thumb: want N_eff ~= number of samples
Rhat - Gelman-Rubin convergence diagnostic, Rule of thumb: want Rhat ~= 1
44. More complex model 🏔
Add more levels
(category, subcategory, etc)
Add more
parameters
(𝑤2
, 𝑤3
,... 𝑤n
)
45. If smth wrong 🌋
1. Check if distribution matches the target formula
2. Plot prior distribution, maybe your prior believes are absurd
3. Plot posterior distribution, maybe your evaluations are too vague
4. Try to redefine the model with offset
5. Check possible suggestions here
6. If nothing works, reparametrize!
46. Useful links 👊
Library: PyMC3
Books:
Bayesian Methods for Hackers (free)
Statistical Rethinking
Good guide to distribution description (free)
Introduction to Bayesian Monte Carlo
To follow:
Thomas Wiecki (and his great blog)
Richard McElreath (also, check out his awesome lectures on )
48. 1
2
About us
We are a Boutique Data Science consulting, specialising in leading digital transformations.
We successfully realized custom projects and won Data Science competitions for startups to
Fortune 500 companies, some of them listed below.
We organize biggest Data Science community events called Kaggle Days in
cooperation with Google-owned Kaggle. Demand for AI experts is extremely
high nowadays - LogicAI is a company which has direct access to the top 3M
data scientists in the world.
49. Our customers have access to 2.8mln world’s top talent
We build world’s biggest offline Data Science community
We build Data Science teams for our customers
+ =
”Great community with lots of diverse talent
and skill sets.”
“You rock. Being nice and generous is the most
important thing in community events and you
were!”
50. 1st place
Allstate
[2016]
1st place
Mercari
[2018]
2nd place
GE
[2014]
Problem solved:
Which potential
customers are at
risk of not repaying
their loans?
3rd place
Am Express
[2015]
3rd place
Deloitte
[2015]
Problem solved:
Which of our current
customers will stay
insured with us for
an entire policy
term?
Problem solved:
What is the best
price for the
product I want to
sell?
Problem solved:
How to predict
flight delays over
The US?
Problem solved:
What will future
rental prices for
properties across
Western Australia
be?
Our experience