Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@Labako

# pymc3でガウス過程

More than 1 year has passed since last update.

こちらのサイトで見つけたシャンプーの売上に関するデータを用いてGaussian Processによって推定してみようと思います。

ガウス過程はノンパラメトリックの代表的な回帰モデルです。

``````import os
import pandas as pd
shampoo_df
shampoo_df.plot.line(x='Month', y='Sales',c="k");
``````

``````
with pm.Model() as shampoo_model:
ρ=pm.HalfCauchy('ρ', 1) # これらはLength scale 大きい値ほど、近い動きに見える　<＝自己相関的な
η = pm.HalfCauchy('η', 1) # これらはLength scale 大きい値ほど、近い動きに見える　<＝自己相関的な

M = pm.gp.mean.Linear(coeffs=(shampoo_df.index/shampoo_df.Sales).mean()) # 初期値

σ = pm.HalfCauchy('σ', 1)
gp = pm.gp.Marginal(mean_func=M, cov_func=K)
gp.marginal_likelihood("recruits", X=shampoo_df.index.values.reshape(-1,1),
y=shampoo_df.Sales.values, noise=σ)
trace = pm.sample(1000)

pm.traceplot(trace);
``````

``````
with shampoo_model:
marginal_post = pm.find_MAP()
``````

{'ρlog': array(2.88298779),
'η_log
': array(5.63830367),
'σ_log
_': array(4.13876033),
'ρ': array(17.86757799),
'η': array(280.98567051),
'σ': array(62.72501496)}

``````
X_pred=np.array([r for r in range(X_pred.max()+10)]).reshape(-1,1)

with shampoo_model:
shampoo_pred = gp.conditional("shampoo_", X_pred)
samples=pm.sample_ppc([marginal_post], vars=[shampoo_pred] ,samples=100)

fig = plt.figure(figsize=(10,5));
ax = fig.gca()

from pymc3.gp.util import plot_gp_dist
plot_gp_dist(ax, samples["shampoo_"], X_pred);

ax.plot(shampoo_df.index, shampoo_df.Sales, 'dodgerblue', ms=5, alpha=0.5);
ax.plot(shampoo_df.index, shampoo_df.Sales, 'ok', ms=5, alpha=0.5);
plt.show()
``````

pm.sample_ppcではmarginal_post内のパラメータに基づく事後予測分布からサンプリングしています。

3
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away