Skip to content

Instantly share code, notes, and snippets.

@a-mitani
Created May 6, 2021 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-mitani/630ef831e8f2238da003a23f25d75e2e to your computer and use it in GitHub Desktop.
Save a-mitani/630ef831e8f2238da003a23f25d75e2e to your computer and use it in GitHub Desktop.
If文を用いない赤玉白玉モデルの実装例
# x=1: 袋a, x=0: 袋b
# y=1: 赤玉, y=0: 白玉
def ball_model_no_if():
x = pyro.sample("x", dist.Bernoulli(0.5))
# xの値に応じてyの割合を変更
y = pyro.sample("y", dist.Bernoulli(2.0/3.0)) * x + pyro.sample("y", dist.Bernoulli(1.0/4.0)) * (1 - x)
return y
# plateを用いてi.i.dサンプリング
with pyro.plate("plate", size=10000):
samples = ball_model_no_if()
print("var_type is {}, shape = {}".format(type(samples), samples.shape))
plt.hist(samples, bins=20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment