import osif"KERAS_BACKEND"notin os.environ:# set this to "torch", "tensorflow", or "jax" os.environ["KERAS_BACKEND"] ="jax"import matplotlib.pyplot as pltimport numpy as npimport kerasimport bayesflow as bfimport pandas as pd
In this example we will evaluate the evidence that the results of the 9 studies presented by Bem et al. (2011) are contaminated by optional stopping.
Simulator
The generative model is same as in the example on Pearson’s correlation, though in this case we make two variants: null and alternative models. Under the null model, \(\rho\) is held fixed at zero, whereas under the alternative model, \(\rho \sim \text{Uniform}(-1, 1)\).
To generalize the procedure to different sample sizes, we will vary \(n\) between 3 and 20.
Note that we change the prior model probability of the null model to 0.8 (and correspondingly of the alternative model to 0.2). The null model doesn’t generate many extreme correlations (i.e., correlations close to -1 and 1). Increasing the prior model probability ensures that the trainer has enough examples of the sampling distribution under the null model in the tails.
INFO:matplotlib.mathtext:Substituting symbol M from STIXNonUnicode
INFO:matplotlib.mathtext:Substituting symbol M from STIXNonUnicode
INFO:matplotlib.mathtext:Substituting symbol M from STIXNonUnicode
INFO:matplotlib.mathtext:Substituting symbol M from STIXNonUnicode
INFO:matplotlib.mathtext:Substituting symbol M from STIXNonUnicode
INFO:matplotlib.mathtext:Substituting symbol M from STIXNonUnicode
INFO:matplotlib.mathtext:Substituting symbol M from STIXNonUnicode
INFO:matplotlib.mathtext:Substituting symbol M from STIXNonUnicode
Inference
Now that we have trained the network, we can compute the Bayes factor in favuor of the model that posits that there is a correlation between the effect size and sample size.
Recall that we increased the prior model probability of the null model to 0.8. This means that in order to calculate the Bayes factor in favour of the alternative model, we must divide the posterior odds with the prior odds.
Code
inference_data =dict( n = np.array([[9]]), r = np.array([[-0.8717038]]))posterior_model_probs = approximator.predict(conditions=inference_data)[0]posterior_model_odds_10 = posterior_model_probs[1] / posterior_model_probs[0]prior_model_odds_10 =0.2/0.8bf_10 = posterior_model_odds_10 / prior_model_odds_10bf_10
59.72677230834961
\(\text{BF}_{10} \approx 60\) provides evidence for optional stopping.
Note: The results here are only approximate and differ from the original analysis by about a factor of 3. This is because the classification network cannot extrapolate well for such an extreme correlation (from the perspective of the null model).
References
Lee, M. D., & Wagenmakers, E.-J. (2013). Bayesian CognitiveModeling: APracticalCourse. Cambridge University Press.