1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ベイズで変化点検知する(ガンマ-ポアソン分布)

Last updated at Posted at 2019-09-08

概要

ガンマ-ポアソン分布を使ったベイジアンモデリングを組みたかったので、やってみた。
良い感じのカウントデータを探したが(ポアソン分布使いたかったので)、なかなか見つけられなかったので、
今回は、google trendで「米津玄師」の直近90日間の検索数のcsvを出力した。
https://trends.google.co.jp/trends/?geo=JP
出力データに対して、ベイジアンモデリングを行い、変化点検出を行った。

直近90日の「米津玄師」検索数

2019年8月あたりから、検索数が増えている。
yonezu.png

結果

pymc3のサンプリング結果。
bayes.png
可視化。ちゃんとトレンド検知できているよう。
change.png

コード

import pandas as pd
import numpy as np
import pymc3 as pm
import matplotlib.pyplot as plt

# 前処理
df = pd.read_csv("../desktop/yonezu.csv")
df[""] = pd.to_datetime(df[""])
df.index = df[""]
df.drop([""], axis=1, inplace=True)

# モデリング
weeks = df.reset_index().index
with pm.Model() as model:
    switchpoint = pm.DiscreteUniform('sp', 
                                     lower=weeks.min(),
                                     upper=weeks.max())
    lambda1 = pm.Gamma('lambda1', alpha=1, beta=1)
    lambda2 = pm.Gamma('lambda2', alpha=1, beta=1)
    lambda_ = pm.math.switch(switchpoint>=weeks, lambda1, lambda2)
    k = pm.Poisson('k', lambda_, observed = df["米津玄師"].values)
with model:
    trace = pm.sample(10000)
burnin = 100
chain = trace[burnin:]

# 出力
pm.traceplot(chain)
df["bayes"] = np.hstack([np.array([chain.lambda1.mean()] * int(chain.sp.mean())), np.array([chain.lambda2.mean()] * (df.index.size - int(chain.sp.mean())))])
df.plot(subplots=False, sharex=True, figsize=(10, 8), title="ベイズによる米津玄師の変化点検知")

感想

変化点検知、実データ分析でかなり有用そうなので、もっと慣れたい。
もっと凝ったことをやりたい。

参考

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?