-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from openpharma/2_meta_analytic
2: Add meta_analytic Turing model
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,12 @@ | ||
module BSSD | ||
|
||
using Turing | ||
using StatsPlots | ||
using Distributions | ||
|
||
export | ||
meta_analytic | ||
|
||
include("meta_analytic.jl") | ||
|
||
end |
This file contains 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,28 @@ | ||
""" | ||
Meta Analytic Prior Model | ||
This Turing model is used to generate posterior samples of the parameters `a` and `b`. | ||
""" | ||
@model function meta_analytic( | ||
y::Vector{Bool}, | ||
time::Vector{Float64}, | ||
trialindex::Vector{Int64}, | ||
prior_a::Distribution, | ||
prior_b::Distribution) | ||
|
||
n = length(y) | ||
n_trials = maximum(trialindex) | ||
|
||
a ~ prior_a | ||
b ~ prior_b | ||
pis ~ filldist(Beta(a * b * n, (1 - a) * b * n), n_trials) | ||
|
||
for i in 1:n | ||
pi = pis[trialindex[i]] | ||
mu = log(-log(1 - pi)) | ||
x = mu + log(time[i]) | ||
prob = 1 - exp(-exp(x)) | ||
y[i] ~ Bernoulli(prob) | ||
end | ||
|
||
end; |
This file contains 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,45 @@ | ||
using Test | ||
using StableRNGs | ||
using Random | ||
using Distributions | ||
using DataFrames | ||
using Turing | ||
using BSSD | ||
|
||
# Helper function for numerical tests. | ||
# Taken from https://github.com/TuringLang/Turing.jl/blob/master/test/test_utils/numerical_tests.jl#L41 for now. | ||
function check_numerical(chain, | ||
symbols::Vector, | ||
exact_vals::Vector; | ||
atol=0.2, | ||
rtol=0.0) | ||
for (sym, val) in zip(symbols, exact_vals) | ||
E = val isa Real ? | ||
mean(chain[sym]) : | ||
vec(mean(chain[sym], dims=1)) | ||
@info (symbol=sym, exact=val, evaluated=E) | ||
@test E ≈ val atol=atol rtol=rtol | ||
end | ||
end | ||
|
||
@testset "meta_analytic.jl" begin | ||
rng = StableRNG(123) | ||
|
||
n_trials = 5 | ||
n_patients = 50 | ||
df = DataFrame( | ||
y = rand(rng, Bernoulli(0.2), n_trials * n_patients), | ||
time = rand(rng, Exponential(1), n_trials * n_patients), | ||
trialindex = repeat(1:n_trials, n_patients) | ||
) | ||
|
||
chain = sample( | ||
rng, | ||
meta_analytic(df.y, df.time, df.trialindex, Beta(2, 8), Beta(9, 10)), | ||
HMC(0.05, 10), | ||
1000 | ||
) | ||
|
||
check_numerical(chain, [:a], [0.223], rtol=0.001) | ||
check_numerical(chain, [:b], [0.485], rtol=0.001) | ||
end |