Skip to content

Commit ae105f6

Browse files
add one and three-level example simulators
1 parent d1624ee commit ae105f6

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

bayesflow/experimental/graphical_simulator/example_simulators.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def sample_school():
1010

1111
# hierarchical mu/sigma for the exam difficulty standard deviation (logscale)
1212
mu_exam_std = np.random.normal(loc=0.5, scale=0.3)
13-
sigma_exam_std = abs(np.random.normal(loc=0, scale=1))
13+
sigma_exam_std = abs(np.random.normal(loc=0, scale=0.5))
1414

1515
return dict(
1616
mu_exam_mean=mu_exam_mean,
@@ -37,7 +37,7 @@ def sample_question(exam_mean, exam_std):
3737
return dict(question_difficulty=question_difficulty)
3838

3939
# realizations of individual student abilities
40-
def sample_student(**kwargs):
40+
def sample_student():
4141
student_ability = np.random.normal(loc=0, scale=1)
4242

4343
return dict(student_ability=student_ability)
@@ -89,6 +89,34 @@ def meta_fn():
8989
return simulator
9090

9191

92+
def onelevel_simulator():
93+
def prior():
94+
beta = np.random.normal([2, 0], [3, 1])
95+
sigma = np.random.gamma(1, 1)
96+
97+
return {"beta": beta, "sigma": sigma}
98+
99+
def likelihood(beta, sigma, N):
100+
x = np.random.normal(0, 1, size=N)
101+
y = np.random.normal(beta[0] + beta[1] * x, sigma, size=N)
102+
103+
return {"x": x, "y": y}
104+
105+
def meta():
106+
N = np.random.randint(5, 15)
107+
108+
return {"N": N}
109+
110+
simulator = GraphicalSimulator(meta_fn=meta)
111+
112+
simulator.add_node("prior", sampling_fn=prior)
113+
simulator.add_node("likelihood", sampling_fn=likelihood)
114+
115+
simulator.add_edge("prior", "likelihood")
116+
117+
return simulator
118+
119+
92120
def twolevel_simulator():
93121
def sample_hypers():
94122
hyper_mean = np.random.normal()

0 commit comments

Comments
 (0)