This repository has been archived by the owner on Sep 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert all existing models to Literate (#59)
- Loading branch information
1 parent
a20059d
commit bc260b2
Showing
35 changed files
with
1,056 additions
and
963 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
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
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 |
---|---|---|
@@ -1,37 +1,6 @@ | ||
+++ | ||
title = "Admit or reject" | ||
showall = true | ||
+++ | ||
|
||
``` | ||
using TuringModels, StatsFuns | ||
delim = ';' | ||
d = CSV.read(joinpath(@__DIR__, "..", "..", "data", "UCBadmit.csv"), DataFrame; delim); | ||
size(d) # Should be 12x5 | ||
@model m_pois(admit, reject) = begin | ||
α₁ ~ Normal(0,100) | ||
α₂ ~ Normal(0,100) | ||
for i ∈ 1:length(admit) | ||
λₐ = exp(α₁) | ||
λᵣ = exp(α₂) | ||
admit[i] ~ Poisson(λₐ) | ||
reject[i] ~ Poisson(λᵣ) | ||
end | ||
end; | ||
chns = sample(m_pois(d[:, :admit], d[:, :reject]), Turing.NUTS(0.65), 1000); | ||
# Rethinking/CmdStan result | ||
m_10_yyt_result = " | ||
mean sd 5.5% 94.5% n_eff Rhat | ||
a1 4.99 0.02 4.95 5.02 2201 1 | ||
a2 5.44 0.02 5.41 5.47 2468 1 | ||
"; | ||
# Describe the draws | ||
chns |> display | ||
``` | ||
\literate{/scripts/admit-reject.jl} |
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 |
---|---|---|
@@ -1,39 +1,10 @@ | ||
+++ | ||
title = "Chimpanzees" | ||
showall = true | ||
+++ | ||
|
||
``` | ||
using TuringModels, StatsFuns | ||
This is the third Chimpanzees model (`m10.3`) in Statistical Rethinking Edition 1. | ||
|
||
delim=';' | ||
d = CSV.read(joinpath(@__DIR__, "..", "..", "data", "chimpanzees.csv"), DataFrame; delim); | ||
size(d) # Should be 504x8 | ||
\toc | ||
|
||
# pulled_left, condition, prosoc_left | ||
@model m10_3(y, x₁, x₂) = begin | ||
α ~ Normal(0, 10) | ||
βp ~ Normal(0, 10) | ||
βpC ~ Normal(0, 10) | ||
logits = α .+ (βp .+ βpC * x₁) .* x₂ | ||
y .~ BinomialLogit.(1, logits) | ||
end; | ||
chns = sample(m10_3(d[:,:pulled_left], d[:,:condition], d[:,:prosoc_left]), | ||
Turing.NUTS(0.65), 2000); | ||
# Rethinking result | ||
m_10_03t_result = " | ||
Mean StdDev lower 0.89 upper 0.89 n_eff Rhat | ||
a 0.05 0.13 -0.15 0.25 3284 1 | ||
bp 0.62 0.22 0.28 0.98 3032 1 | ||
bpC -0.11 0.26 -0.53 0.29 3184 1 | ||
"; | ||
# Describe the draws | ||
chns |> display | ||
# End of m10.03t.jl | ||
``` | ||
\literate{/scripts/chimpanzees.jl} |
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 |
---|---|---|
@@ -1,53 +1,11 @@ | ||
+++ | ||
title = "Estimate handedness for each Chimpanzee" | ||
showall = true | ||
+++ | ||
|
||
``` | ||
using TuringModels, StatsFuns | ||
This is model `m10.4` in Statistical Rethinking Edition 1. | ||
|
||
delim=';' | ||
d = CSV.read(joinpath(@__DIR__, "..", "..", "data", "chimpanzees.csv"), DataFrame; delim); | ||
size(d) # Should be 504x8 | ||
\toc | ||
|
||
# pulled_left, actors, condition, prosoc_left | ||
@model m10_4(y, actors, x₁, x₂) = begin | ||
# Number of unique actors in the data set | ||
N_actor = length(unique(actors)) | ||
\literate{/scripts/estimate-handedness-chimpanzees.jl} | ||
|
||
# Set an TArray for the priors/param | ||
α ~ filldist(Normal(0, 10), N_actor) | ||
βp ~ Normal(0, 10) | ||
βpC ~ Normal(0, 10) | ||
logits = α[actors] .+ (βp .+ βpC * x₁) .* x₂ | ||
y .~ BinomialLogit.(1, logits) | ||
end; | ||
chns = sample(m10_4(d[:,:pulled_left], d[:,:actor],d[:,:condition], | ||
d[:,:prosoc_left]), Turing.NUTS(0.65), 1000); | ||
# Rethinking/CmdStan results | ||
m_10_04s_result = " | ||
Iterations = 1:1000 | ||
Thinning interval = 1 | ||
Chains = 1,2,3,4 | ||
Samples per chain = 1000 | ||
Empirical Posterior Estimates: | ||
Mean SD Naive SE MCSE ESS | ||
a.1 -0.74503184 0.26613979 0.0042080396 0.0060183398 1000 | ||
a.2 10.77955494 5.32538998 0.0842018089 0.1269148045 1000 | ||
a.3 -1.04982353 0.28535997 0.0045119373 0.0049074219 1000 | ||
a.4 -1.04898135 0.28129307 0.0044476339 0.0056325117 1000 | ||
a.5 -0.74390933 0.26949936 0.0042611590 0.0052178124 1000 | ||
a.6 0.21599365 0.26307574 0.0041595927 0.0045153523 1000 | ||
a.7 1.81090866 0.39318577 0.0062168129 0.0071483527 1000 | ||
bp 0.83979926 0.26284676 0.0041559722 0.0059795826 1000 | ||
bpC -0.12913322 0.29935741 0.0047332562 0.0049519863 1000 | ||
"; | ||
# Describe the draws | ||
chns |> display | ||
``` |
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
+++ | ||
title = "Ignoring gender for admittance" | ||
showall = true | ||
reeval = true | ||
+++ | ||
|
||
\literate{/scripts/ignoring-gender-admit.jl} |
Oops, something went wrong.