Skip to content

Commit 85c9995

Browse files
committed
Rel 7.0.0
1 parent 3164281 commit 85c9995

39 files changed

+925
-1585
lines changed

Examples/Basic_intros/chimpanzees.jl

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# A walk-through example (using StanSample.jl)
2+
3+
using StanSample, Distributions
4+
5+
N = 100
6+
df = DataFrame(
7+
height = rand(Normal(10, 2), N),
8+
leg_prop = rand(Uniform(0.4, 0.5), N),
9+
)
10+
df.leg_left = df.leg_prop .* df.height + rand(Normal(0, 0.02), N)
11+
df.leg_right = df.leg_prop .* df.height + rand(Normal(0, 0.03), N)
12+
13+
stan6_1 = "
14+
data {
15+
int <lower=1> N;
16+
vector[N] H;
17+
vector[N] LL;
18+
vector[N] LR;
19+
}
20+
parameters {
21+
real a;
22+
vector[2] b;
23+
real <lower=0> sigma;
24+
}
25+
model {
26+
vector[N] mu;
27+
mu = a + b[1] * LL + b[2] * LR;
28+
a ~ normal(10, 100);
29+
b ~ normal(2, 10);
30+
sigma ~ exponential(1);
31+
H ~ normal(mu, sigma);
32+
}
33+
";
34+
35+
m6_1s = SampleModel("m6.1s", stan6_1);
36+
data = (H = df.height, LL = df.leg_left, LR = df.leg_right, N = size(df, 1))
37+
rc6_1s = stan_sample(m6_1s; data);
38+
39+
if success(rc6_1s)
40+
chns = read_samples(m6_1s)
41+
42+
# Display the chns
43+
44+
chns |> display
45+
println()
46+
47+
# Display the keys
48+
49+
axiskeys(chns) |> display
50+
println()
51+
52+
chns(chain=1) |> display
53+
println()
54+
55+
chns[:, 1, 1] |> display
56+
println()
57+
58+
chns(chain=1, param=:a) |> display
59+
println()
60+
61+
chns(chain=[1, 3], param=[:a, :sigma]) |> display
62+
println()
63+
64+
# Select all elements starting with 'a'
65+
66+
chns_b = matrix(chns, :b)
67+
chns_b |> display
68+
println()
69+
70+
mean(chns_b, dims=1) |> display
71+
println()
72+
73+
typeof(chns_b.data) |> display
74+
println()
75+
76+
ndraws_b, nchains_b, nparams_b = size(chns_b)
77+
chn_b = reshape(chns_b, ndraws_b*nchains_b, nparams_b)
78+
println()
79+
80+
for row in eachrow(chn_b)
81+
# ...
82+
end
83+
84+
# Or use read_samples to only use chains 2 and 4 using the chains kwarg.
85+
86+
chns2 = read_samples(m6_1s; chains=[2, 4])
87+
chns2_b = matrix(chns2, :b)
88+
ndraws2_b, nchains2_b, nparams2_b = size(chns2_b)
89+
chn2_b = reshape(chns2_b, ndraws2_b*nchains2_b, nparams2_b)
90+
mean(chns2_b, dims=1) |> display
91+
92+
end
93+
94+
# Some more options:
95+
96+
init = (a = 2.0, b = [1.0, 2.0], sigma = 1.0)
97+
rc6_2s = stan_sample(m6_1s; data, init);
98+
99+
if success(rc6_2s)
100+
101+
read_summary(m6_1s, true)
102+
println()
103+
104+
post6_1s_df = read_samples(m6_1s, :dataframe)
105+
post6_1s_df |> display
106+
println()
107+
108+
part6_1s = read_samples(m6_1s, :particles)
109+
part6_1s |> display
110+
println()
111+
112+
nt6_1s = read_samples(m6_1s, :namedtuple)
113+
nt6_1s.b |> display
114+
println()
115+
116+
mean(nt6_1s.b, dims=2) |> display
117+
println()
118+
119+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Load Julia packages (libraries)
2+
3+
using Pkg, DrWatson
4+
5+
using StanSample
6+
7+
df = CSV.read(joinpath(@__DIR__, "..", "..", "data", "chimpanzees.csv"), DataFrame);
8+
9+
# Define the Stan language model
10+
11+
stan10_4 = "
12+
data{
13+
int N;
14+
int N_actors;
15+
int pulled_left[N];
16+
int prosoc_left[N];
17+
int condition[N];
18+
int actor[N];
19+
}
20+
parameters{
21+
vector[N_actors] a;
22+
real bp;
23+
real bpC;
24+
}
25+
model{
26+
vector[N] p;
27+
bpC ~ normal( 0 , 10 );
28+
bp ~ normal( 0 , 10 );
29+
a ~ normal( 0 , 10 );
30+
for ( i in 1:504 ) {
31+
p[i] = a[actor[i]] + (bp + bpC * condition[i]) * prosoc_left[i];
32+
p[i] = inv_logit(p[i]);
33+
}
34+
pulled_left ~ binomial( 1 , p );
35+
}
36+
";
37+
38+
data = (N = size(df, 1), N_actors = length(unique(df.actor)),
39+
actor = df.actor, pulled_left = df.pulled_left,
40+
prosoc_left = df.prosoc_left, condition = df.condition);
41+
42+
# Sample using cmdstan
43+
44+
m10_4s = SampleModel("m10.4s", stan10_4)
45+
rc10_4s = stan_sample(m10_4s; data);
46+
47+
if success(rc10_4s)
48+
chns = read_samples(m10_4s)
49+
50+
# Display the chns
51+
52+
chns |> display
53+
println()
54+
55+
# Display the keys
56+
57+
axiskeys(chns) |> display
58+
println()
59+
60+
mean(chns_a, dims=1) |> display
61+
println()
62+
63+
chns(chain=1) |> display
64+
println()
65+
66+
chns[:, 1, 8] |> display
67+
println()
68+
69+
chns(chain=1, param=:bp) |> display
70+
println()
71+
72+
chns(chain=[1, 3], param=[:bp, :bpC]) |> display
73+
println()
74+
75+
# Select all elements starting with 'a'
76+
77+
chns_a = matrix(chns, :a)
78+
chns_a |> display
79+
println()
80+
81+
typeof(chns_a.data) |> display
82+
println()
83+
84+
ndraws_a, nchains_a, nparams_a = size(chns_a)
85+
chn_a = reshape(chns_a, ndraws_a*nchains_a, nparams_a)
86+
println()
87+
88+
for row in eachrow(chn_a)
89+
# ...
90+
end
91+
92+
# Or use read_samples to only use chains 2 and 4 using the chains kwarg.
93+
94+
chns2 = read_samples(m10_4s; chains=[2, 4])
95+
chns2_a = matrix(chns2, :a)
96+
ndraws2_a, nchains2_a, nparams2_a = size(chns2_a)
97+
chn2_a = reshape(chns2_a, ndraws2_a*nchains2_a, nparams2_a)
98+
mean(chns2_a, dims=1) |> display
99+
100+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using StatsBase, NamedTupleTools
2+
using StanSample
3+
4+
df = CSV.read(joinpath(@__DIR__, "..", "..", "data", "WaffleDivorce.csv"), DataFrame);
5+
df.D = zscore(df.Divorce)
6+
df.M = zscore(df.Marriage)
7+
df.A = zscore(df.MedianAgeMarriage)
8+
data = (N=size(df, 1), D=df.D, A=df.A, M= df.M)
9+
10+
stan5_1 = "
11+
data {
12+
int < lower = 1 > N; // Sample size
13+
vector[N] D; // Outcome
14+
vector[N] A; // Predictor
15+
}
16+
parameters {
17+
real a; // Intercept
18+
real bA; // Slope (regression coefficients)
19+
real < lower = 0 > sigma; // Error SD
20+
}
21+
transformed parameters {
22+
vector[N] mu; // mu is a vector
23+
for (i in 1:N)
24+
mu[i] = a + bA * A[i];
25+
}
26+
model {
27+
a ~ normal(0, 0.2); //Priors
28+
bA ~ normal(0, 0.5);
29+
sigma ~ exponential(1);
30+
D ~ normal(mu , sigma); // Likelihood
31+
}
32+
generated quantities {
33+
vector[N] log_lik;
34+
for (i in 1:N)
35+
log_lik[i] = normal_lpdf(D[i] | mu[i], sigma);
36+
}
37+
";
38+
39+
m5_1s = SampleModel("m5.1s", stan5_1)
40+
rc5_1s = stan_sample(m5_1s; data)
41+
42+
if success(rc5_1s)
43+
44+
nt5_1s = read_samples(m5_1s, :particles)
45+
NamedTupleTools.select(nt5_1s, (:a, :bA, :sigma)) |> display
46+
47+
chns = read_samples(m5_1s)
48+
chns |> display
49+
end

Examples/Walkthrough2/walkthrough2.jl

-63
This file was deleted.

Examples_DiffEqBayes/Readme.md

-17
This file was deleted.

0 commit comments

Comments
 (0)