-
Notifications
You must be signed in to change notification settings - Fork 74
Add support for graphical simulators #487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
daniel-habermann
wants to merge
18
commits into
bayesflow-org:dev
Choose a base branch
from
daniel-habermann:graphical-simulator
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
20b54ce
initial commit
daniel-habermann 02cc915
add networkx to project dependencies
daniel-habermann be9f390
initial implementation GraphicalSimulator
daniel-habermann 4194062
samples method of GraphicalSimulator now returns a dict of appropriat…
daniel-habermann 60e589a
add irt_simulator and threelevel_simulator
daniel-habermann d1624ee
enable sampling_fn with no arguments for non root nodes, change outpu…
daniel-habermann ae105f6
add one and three-level example simulators
daniel-habermann d8ac4fd
allow root node repetitions
daniel-habermann 56f7681
export GraphicalSimulator
daniel-habermann e59b2b2
rename sampling_fn argument to sample_fn in GraphicalSimulator.add_no…
daniel-habermann 9284333
move example simulators to own submodule
daniel-habermann b75c5f5
add unit tests for single level graphical model
daniel-habermann 1243f8c
add unit tests for two_level and irt graphical simulators
daniel-habermann 55b6dfd
rename GraphicalSimulator._call_sampling_fn to _call_sample_fn to ref…
daniel-habermann 4035c16
rename examples in graphical_simulator.example_simulators
daniel-habermann b5a653c
update description of **kwargs parameter in GraphicalSimulator.sample…
daniel-habermann a3c1fb6
remove unused is_root_node function
daniel-habermann c5044c1
use 0-based index for internal representation
daniel-habermann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .graphical_simulator import GraphicalSimulator | ||
from . import example_simulators |
3 changes: 3 additions & 0 deletions
3
bayesflow/experimental/graphical_simulator/example_simulators/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .single_level_simulator import single_level_simulator | ||
from .two_level_simulator import two_level_simulator | ||
from .crossed_design_irt_simulator import crossed_design_irt_simulator |
87 changes: 87 additions & 0 deletions
87
...sflow/experimental/graphical_simulator/example_simulators/crossed_design_irt_simulator.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import numpy as np | ||
|
||
from ..graphical_simulator import GraphicalSimulator | ||
|
||
|
||
def crossed_design_irt_simulator(): | ||
r""" | ||
Item Response Theory (IRT) model implemented as a graphical simulator. | ||
|
||
schools | ||
/ \ | ||
exams students | ||
| | | ||
questions | | ||
\ / | ||
observations | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neat! But I wonder what this looks like on the online sphinx docs |
||
""" | ||
|
||
# schools have different exam difficulties | ||
def sample_school(): | ||
mu_exam_mean = np.random.normal(loc=1.1, scale=0.2) | ||
sigma_exam_mean = abs(np.random.normal(loc=0, scale=1)) | ||
|
||
# hierarchical mu/sigma for the exam difficulty standard deviation (logscale) | ||
mu_exam_std = np.random.normal(loc=0.5, scale=0.3) | ||
sigma_exam_std = abs(np.random.normal(loc=0, scale=0.5)) | ||
|
||
return dict( | ||
mu_exam_mean=mu_exam_mean, | ||
sigma_exam_mean=sigma_exam_mean, | ||
mu_exam_std=mu_exam_std, | ||
sigma_exam_std=sigma_exam_std, | ||
) | ||
|
||
# exams have different question difficulties | ||
def sample_exam(mu_exam_mean, sigma_exam_mean, mu_exam_std, sigma_exam_std): | ||
# mean question difficulty for an exam | ||
exam_mean = np.random.normal(loc=mu_exam_mean, scale=sigma_exam_mean) | ||
|
||
# standard deviation of question difficulty | ||
log_exam_std = np.random.normal(loc=mu_exam_std, scale=sigma_exam_std) | ||
exam_std = float(np.exp(log_exam_std)) | ||
|
||
return dict(exam_mean=exam_mean, exam_std=exam_std) | ||
|
||
# realizations of individual question difficulties | ||
def sample_question(exam_mean, exam_std): | ||
question_difficulty = np.random.normal(loc=exam_mean, scale=exam_std) | ||
|
||
return dict(question_difficulty=question_difficulty) | ||
|
||
# realizations of individual student abilities | ||
def sample_student(): | ||
student_ability = np.random.normal(loc=0, scale=1) | ||
|
||
return dict(student_ability=student_ability) | ||
|
||
# realizations of individual observations | ||
def sample_observation(question_difficulty, student_ability): | ||
theta = np.exp(question_difficulty + student_ability) / (1 + np.exp(question_difficulty + student_ability)) | ||
|
||
obs = np.random.binomial(n=1, p=theta) | ||
|
||
return dict(obs=obs) | ||
|
||
def meta_fn(): | ||
return { | ||
"num_exams": np.random.randint(2, 4), | ||
"num_questions": np.random.randint(10, 21), | ||
"num_students": np.random.randint(100, 201), | ||
} | ||
|
||
simulator = GraphicalSimulator(meta_fn=meta_fn) | ||
|
||
simulator.add_node("schools", sample_fn=sample_school) | ||
simulator.add_node("exams", sample_fn=sample_exam, reps="num_exams") | ||
simulator.add_node("questions", sample_fn=sample_question, reps="num_questions") | ||
simulator.add_node("students", sample_fn=sample_student, reps="num_students") | ||
simulator.add_node("observations", sample_fn=sample_observation) | ||
|
||
simulator.add_edge("schools", "exams") | ||
simulator.add_edge("schools", "students") | ||
simulator.add_edge("exams", "questions") | ||
simulator.add_edge("questions", "observations") | ||
simulator.add_edge("students", "observations") | ||
|
||
return simulator |
36 changes: 36 additions & 0 deletions
36
bayesflow/experimental/graphical_simulator/example_simulators/single_level_simulator.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import numpy as np | ||
|
||
from ..graphical_simulator import GraphicalSimulator | ||
|
||
|
||
def single_level_simulator(): | ||
""" | ||
Simple single-level simulator that implements the same model as in | ||
https://bayesflow.org/main/_examples/Linear_Regression_Starter.html | ||
""" | ||
|
||
def prior(): | ||
beta = np.random.normal([2, 0], [3, 1]) | ||
sigma = np.random.gamma(1, 1) | ||
|
||
return {"beta": beta, "sigma": sigma} | ||
|
||
def likelihood(beta, sigma, N): | ||
x = np.random.normal(0, 1, size=N) | ||
y = np.random.normal(beta[0] + beta[1] * x, sigma, size=N) | ||
|
||
return {"x": x, "y": y} | ||
|
||
def meta(): | ||
N = np.random.randint(5, 15) | ||
|
||
return {"N": N} | ||
|
||
simulator = GraphicalSimulator(meta_fn=meta) | ||
|
||
simulator.add_node("prior", sample_fn=prior) | ||
simulator.add_node("likelihood", sample_fn=likelihood) | ||
|
||
simulator.add_edge("prior", "likelihood") | ||
|
||
return simulator |
65 changes: 65 additions & 0 deletions
65
bayesflow/experimental/graphical_simulator/example_simulators/two_level_simulator.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import numpy as np | ||
|
||
from ..graphical_simulator import GraphicalSimulator | ||
|
||
|
||
def two_level_simulator(repeated_roots=False): | ||
r""" | ||
Simple hierarchical model with two levels of parameters: hyperparameters | ||
and local parameters, along with a shared parameter: | ||
|
||
hypers | ||
| | ||
locals shared | ||
\ / | ||
\ / | ||
y | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above with respect to sphinx docs. |
||
|
||
Parameters | ||
---------- | ||
repeated_roots : bool, default false. | ||
|
||
""" | ||
|
||
def sample_hypers(): | ||
hyper_mean = np.random.normal() | ||
hyper_std = np.abs(np.random.normal()) | ||
|
||
return {"hyper_mean": hyper_mean, "hyper_std": hyper_std} | ||
|
||
def sample_locals(hyper_mean, hyper_std): | ||
local_mean = np.random.normal(hyper_mean, hyper_std) | ||
|
||
return {"local_mean": local_mean} | ||
|
||
def sample_shared(): | ||
shared_std = np.abs(np.random.normal()) | ||
|
||
return {"shared_std": shared_std} | ||
|
||
def sample_y(local_mean, shared_std): | ||
y = np.random.normal(local_mean, shared_std) | ||
|
||
return {"y": y} | ||
|
||
simulator = GraphicalSimulator() | ||
|
||
if not repeated_roots: | ||
simulator.add_node("hypers", sample_fn=sample_hypers) | ||
else: | ||
simulator.add_node("hypers", sample_fn=sample_hypers, reps=5) | ||
|
||
simulator.add_node( | ||
"locals", | ||
sample_fn=sample_locals, | ||
reps=6, | ||
) | ||
|
||
simulator.add_node("shared", sample_fn=sample_shared) | ||
simulator.add_node("y", sample_fn=sample_y, reps=10) | ||
|
||
simulator.add_edge("hypers", "locals") | ||
simulator.add_edge("locals", "y") | ||
simulator.add_edge("shared", "y") | ||
|
||
return simulator |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we should have these in the library itself, rather than, say, in the examples directory.