オーストラリアで勉強してきたMLデザイナーの口語自由詩

主に、データ分析・機械学習・ベイズ・統計について自由に書く。

pymc4のソースコード読んでみた - “Model”の動作確認, Initial Model Class, sampling and random variable [aafa32d]

f:id:yukinagae:20171122095115p:plain

コミット

2018/05/30のコミットです。

Initial Model Class, sampling and random variable · pymc-devs/pymc4@aafa32d · GitHub

主に、pymc4の根幹となる ModelRandomVariable クラスが作成されています。

Model クラスの動作を確認してみます。

Model - model.py

with でModelインスタンスを生成する度に、stackの末尾にそのmodelインスタンスが生成され、parent が設定されるテスト

print("before: ", Model.get_contexts())

with Model(name="model1") as model1:
    print("[1] inside with", ["{}:{}".format(x.name, getattr(x, 'parent', None)) for x in Model.get_contexts()])
    with Model(name="model2") as model2:
        print("[2] inside with", ["{}:{}".format(x.name, getattr(x, 'parent', None)) for x in Model.get_contexts()])
        print("model2's parent: ", model2.parent.name)
    
with Model(name="model3") as model3:
    print("[3] inside with", ["{}:{}".format(x.name, getattr(x, 'parent', None)) for x in Model.get_contexts()])

print("after: ", Model.get_contexts())

出力:

before:  []
[1] inside with ['model1:None']
[2] inside with ['model1:None', 'model2:<__main__.Model object at 0x1225e8048>']
model2's parent:  model1
[3] inside with ['model3:None']
after:  []

TODO

AttributeError: 'RandomVariable' object has no attribute 'sample'

References