StanとRでベイズ統計モデリング(アヒル本)をPythonにしてみる - Chapter 6 練習問題


実行環境

インポート

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

(1)

np.random.seed(123)

bernoulli_rng = np.random.binomial(1, 0.2, size=10)

categorical_rng = np.random.randint(1, 6, size=10)

(2)

np.random.seed(123)

beta_rng = np.random.beta(2.0, 2.0, size=5)

dirichlet_rng = np.random.dirichlet((0.3, 1.0, 1.0), size=5)

gamma_rng = np.random.gamma(3.0, 1.0, size=5)

bivariate_normal_rng = np.random.multivariate_normal((0, 1), np.array((2, 1, 1, 3)).reshape((-1, 2)), size=5)

cauchy_rng = stats.cauchy.rvs(loc=1, scale=2.5, size=5)

(3)

np.random.seed(123)

y1 = np.random.normal(loc=50, scale=20, size=2000)
y2 = np.random.normal(loc=20, scale=15, size=2000)
y = y1 - y2

sns.kdeplot(y)
plt.show()