牛油果多少钱?

Python 中的贝叶斯数据分析

Michal Oleszak

Machine Learning Engineer

牛油果公司

牛油果。

Python 中的贝叶斯数据分析

案例研究:估计价格弹性

目标:估计牛油果的价格弹性并优化定价

(价格弹性 = 价格变化对销量的影响)

  1. 拟合贝叶斯回归模型。
  2. 检查模型以验证其正确性。
  3. 预测不同价格下的销量。
  4. 给出使利润最大化的价格及其不确定性。
Python 中的贝叶斯数据分析

牛油果数据

print(avocado)
           date  price      volume  type_organic
0    2015-01-04   0.95  313.242777             0
1    2015-01-11   1.01  290.635427             0
2    2015-01-18   1.03  290.434588             0
3    2015-01-25   1.04  284.703108             0
..          ...    ...         ...           ...
334  2018-03-04   1.52   16.344308             1
335  2018-03-11   1.52   16.642349             1
336  2018-03-18   1.54   16.758042             1
337  2018-03-25   1.55   15.599672             1
1 数据来源:https://www.kaggle.com/neuromusic/avocado-prices
Python 中的贝叶斯数据分析

pymc3 中的先验

formula = "num_bikes ~ temp + work_day + wind_speed"

with pm.Model() as model:

    pm.GLM.from_formula(formula, data=bikes)
    trace = pm.sample(draws=1000, tune=500)
Python 中的贝叶斯数据分析

pymc3 中的先验

formula = "num_bikes ~ temp + work_day + wind_speed"

with pm.Model() as model:
    priors = {"wind_speed": pm.Normal.dist(mu=-5)}
    pm.GLM.from_formula(formula, data=bikes, priors=priors)
    trace = pm.sample(draws=1000, tune=500)
Python 中的贝叶斯数据分析

从 trace 提取抽样结果

temp_draws = trace.get_values("temp")

print(temp_draws)
array([6.8705346, 6.7421152, 6.7393061, ..., 5.966574 , 6.1274128, 6.7149277])
Python 中的贝叶斯数据分析

你将用到的内容

模型拟合:

  • pm.Model()
  • pm.GLM.from_formula()
  • pm.sample()
  • pm.Normal()

可视化:

  • pm.forestplot()
  • pm.traceplot()

 

预测:

  • pm.fast_sample_posterior_predictive()

 

推断:

  • az.hdi()
Python 中的贝叶斯数据分析

让我们动手练习!

Python 中的贝叶斯数据分析

Preparing Video For Download...