Skip to content

Commit 1410af4

Browse files
committed
add NumberConcept models
1 parent 5de6427 commit 1410af4

File tree

4 files changed

+762
-0
lines changed

4 files changed

+762
-0
lines changed
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
# clears workspace:
2+
rm(list=ls())
3+
library(rstan)
4+
5+
model <- "
6+
// Knower Level Model Applied to Give-N Data
7+
data {
8+
int<lower=1> ns;
9+
int<lower=1> nz;
10+
int<lower=1> gn;
11+
int gnq[ns];
12+
int gq[ns,21]; // no. columns = max(gnq)
13+
int ga[ns,21]; // no. columns = max(gnq)
14+
}
15+
parameters {
16+
vector<lower=0,upper=1>[gn] pitmp;
17+
real<lower=1,upper=1000> v;
18+
}
19+
transformed parameters {
20+
simplex[gn] pi;
21+
simplex[gn] npiprime[nz,gn];
22+
vector[nz] lp_parts[ns];
23+
24+
// Base rate
25+
pi <- pitmp / sum(pitmp);
26+
27+
// Model
28+
for (i in 1:nz) {
29+
for (j in 1:gn) {
30+
vector[gn] piprime;
31+
for (k in 1:gn) {
32+
real ind1;
33+
real ind2;
34+
real ind3;
35+
real ind4;
36+
real ind5;
37+
38+
// Will be 1 if Knower-Level (i.e, i-1) is Same or Greater than Answer
39+
ind1 <- step((i - 1) - k);
40+
// Will be 1 for the Possible Answer that Matches the Question
41+
ind2 <- k == j;
42+
// Will be 1 for 0-Knowers
43+
ind3 <- i == 1;
44+
// Will be 1 for HN-Knowers
45+
ind4 <- i == nz;
46+
ind5 <- ind3 + ind4 * (2 + ind2)
47+
+ (1 - ind4) * (1 - ind3) * (ind1 * ind2 + ind1 + 1);
48+
49+
if (ind5 == 1)
50+
piprime[k] <- pi[k];
51+
else if (ind5 == 2)
52+
piprime[k] <- 1 / v * pi[k];
53+
else if (ind5 == 3)
54+
piprime[k] <- v * pi[k];
55+
}
56+
for (k in 1:gn)
57+
npiprime[i,j,k] <- piprime[k] / sum(piprime);
58+
}
59+
}
60+
61+
for (i in 1:ns) {
62+
for (m in 1:nz) {
63+
real lp_parts_tmp;
64+
lp_parts_tmp <- 0;
65+
66+
// Probability a z[i]-Knower Will Answer ga[i,j] to Question gq[i,j]
67+
// is a Categorical Draw From Their Distribution over the 1:gn Toys
68+
for (j in 1:gnq[i])
69+
lp_parts_tmp <- lp_parts_tmp
70+
+ categorical_log(ga[i,j], npiprime[m,gq[i,j]]);
71+
72+
lp_parts[i,m] <- log(1.0 / nz) + lp_parts_tmp;
73+
}
74+
}
75+
}
76+
model {
77+
for (i in 1:ns)
78+
increment_log_prob(log_sum_exp(lp_parts[i]));
79+
}
80+
generated quantities {
81+
vector[nz] prob;
82+
int z[ns];
83+
int predga[ns,gn];
84+
int predz[nz,gn];
85+
int predpi;
86+
87+
for (i in 1:ns) {
88+
prob <- softmax(lp_parts[i]);
89+
z[i] <- categorical_rng(prob);
90+
}
91+
92+
// Posterior Predictive
93+
for (i in 1:ns)
94+
for (j in 1:gn)
95+
predga[i,j] <- categorical_rng(npiprime[z[i],j]);
96+
97+
// Posterior Prediction For Knower Levels
98+
for (i in 1:nz)
99+
for (j in 1:gn)
100+
predz[i,j] <- categorical_rng(npiprime[i,j]);
101+
102+
predpi <- categorical_rng(pi);
103+
}"
104+
105+
load("fc_given.RData") # Load all data for the model
106+
107+
# to be passed on to Stan
108+
data <- c("ns", "gnq", "gn", "ga", "gq", "nz")
109+
110+
myinits <- list(
111+
list(v=2, pitmp=rep(1 / gn, gn)),
112+
list(v=2, pitmp=rep(1 / gn, gn)))
113+
114+
# parameters to be monitored:
115+
parameters <- c("predga", "predz", "predpi", "v", "z")
116+
117+
# The following command calls Stan with specific options.
118+
# For a detailed description type "stan".
119+
samples <- stan(model_code=model,
120+
data=data,
121+
init=myinits, # If not specified, gives random inits
122+
pars=parameters,
123+
iter=600,
124+
chains=2,
125+
thin=1,
126+
warmup = 100, # Stands for burn-in; Default = iter/2
127+
# seed = 123 # Setting seed; Default is random seed
128+
)
129+
130+
zSamples <- extract(samples)$z
131+
predpi <- extract(samples)$predpi
132+
predz <- extract(samples)$predz
133+
predga <- extract(samples)$predga
134+
135+
#### Figure 19.2 ####
136+
windows(7,4)
137+
par(mar=c(3, 2, 1, 1) + .1, mgp=c(1.3, 0.2, 0), cex.lab=1.2)
138+
barplot(table(predpi), ylim=c(0, max(table(predpi)) * 1.2), col="black",
139+
yaxt="n", xlab="Number", ylab="")
140+
box()
141+
title(ylab="Probability", line=.2)
142+
axis(3, at=seq(.7, 17.5, by=1.2), label=FALSE, tck = 0.02)
143+
144+
#### Figure 19.3 ####
145+
windows(9, 6)
146+
par(mfrow=c(4, 5), mar=c(1, 0, 2, 2) + .1, oma=c(2.6, 2.8, 1, 0),
147+
mgp=c(1.5, 0.15, 0))
148+
for (i in 1:ns) {
149+
zTable <- table(factor(zSamples[, i], levels=as.character(1:6)))
150+
151+
barplot(zTable, col="black", xaxt="n", yaxt="n", main=paste("Child", i),
152+
ylim=c(0, length(zSamples[, 1])))
153+
154+
if (i == 16)
155+
axis(1, at=seq(.7, 7.5, by=1.2), label=c("P", "1", "2", "3", "4", "C"), tck = 0.1)
156+
else
157+
axis(1, at=seq(.7, 7.5, by=1.2), label=FALSE, tck = 0.1)
158+
159+
axis(3, at=seq(.7, 7.5, by=1.2), label=FALSE, tck = 0.1)
160+
box()
161+
}
162+
mtext("Knower", side=1, line=0.2, at=.055, adj=0, outer=TRUE)
163+
mtext("Level", side=1, line=1.2, at=.065, adj=0, outer=TRUE)
164+
mtext("Posterior", side=2, line=1.2, at=.07, adj=0, outer=TRUE)
165+
mtext("Mass", side=2, line=0.2, at=.09, adj=0, outer=TRUE)
166+
167+
#### Figure 19.4 ####
168+
subjlist = c(15, 2, 4, 3, 10, 20)
169+
sc=2
170+
sc2=1
171+
sc3=2
172+
cm=-.4
173+
cb=.99
174+
shadedSize <- 2
175+
176+
windows(9, 6)
177+
par(mfrow=c(2, 3), mar=c(2, 2, 2, 1) + .1, oma=c(2, 3, 0, 0), mgp=c(.2, .2, 0),
178+
cex.lab=1)
179+
for (s in subjlist) {
180+
plot(NA, xlim=c(.5, 15.5), ylim=c(.5, 15.5), axes=FALSE, cex.main=1, ylab="",
181+
main=paste("Child", s), xlab="")
182+
axis(1, at=c(1, 2, 3, 4, 5, 8, 10), tck=0.01)
183+
axis(2, at=1:15, las=1, tck=0.01)
184+
axis(3, at=c(1, 2, 3, 4, 5, 8, 10), labels=FALSE, tck=0.01)
185+
axis(4, at=1:15, labels=FALSE, tck=0.01)
186+
box()
187+
188+
for (i in 1:gn) {
189+
count <- hist(predga[, s, i], plot=FALSE, breaks=seq(0.5, 15.5))$counts
190+
count <- count / max(count)
191+
192+
for (j in 1:gn)
193+
points(i, j, pch=15, col=gray(min(1, max(0, (cm * count[j] + cb)))),
194+
cex=shadedSize)
195+
196+
tmp <- gq[s, ]
197+
match <- which(tmp == i)
198+
if (length(match) != 0) {
199+
for (j in 1:gn) {
200+
count <- sum(ga[s, match] == j)
201+
count <- count / length(match)
202+
if (count > 0)
203+
points(i, j, pch=22, cex=sc * sqrt(count), lwd=sc3 * count)
204+
}
205+
}
206+
}
207+
}
208+
mtext("Question", side=1, line=0.2, outer=TRUE)
209+
mtext("Answer", side=2, line=0.2, outer=TRUE)
210+
211+
#### Figure 19.5 ####
212+
mainTitle <- c("PN-Knower", "One-Knower", "Two-Knower", "Three-Knower",
213+
"Four-Knower", "CP-Knower")
214+
dm <- array(0, dim=c(gn, gn, nz))
215+
mv <- c()
216+
for (i in 1:ns) { # computing mode of z for each child
217+
uz <- unique(zSamples[, i])
218+
mv[i] <- uz[which.max(tabulate(match(zSamples[, i], uz)))]
219+
}
220+
221+
windows(9, 6)
222+
par(mfrow=c(2, 3), mar=c(2, 2, 2, 1) + .1, oma=c(2, 3, 0, 0), mgp=c(.2, .2, 0),
223+
cex.lab=1)
224+
for (z in 1:nz) {
225+
plot(NA, xlim=c(.5, 15.5), ylim=c(.5, 15.5), axes=FALSE, cex.main=1, ylab="",
226+
main=mainTitle[z], xlab="")
227+
axis(1, at=c(1, 2, 3, 4, 5, 8, 10), tck=0.01)
228+
axis(2, at=1:15, las=1, tck=0.01)
229+
axis(3, at=c(1, 2, 3, 4, 5, 8, 10), labels=FALSE, tck=0.01)
230+
axis(4, at=1:15, labels=FALSE, tck=0.01)
231+
box()
232+
233+
for (i in 1:gn) {
234+
count <- hist(predz[, z, i], plot=FALSE, breaks=seq(0.5, 15.5))$counts
235+
count <- count / max(count)
236+
237+
for (j in 1:gn)
238+
points(i, j, pch=15, col=gray(min(1, max(0, (cm * count[j] + cb)))),
239+
cex=shadedSize)
240+
}
241+
match <- which(mv == z)
242+
243+
for (i in 1:length(match))
244+
for (j in 1:gnq[match[i]])
245+
dm[gq[match[i], j], ga[match[i], j], z] <- dm[gq[match[i], j], ga[match[i], j], z] + 1
246+
247+
for (i in 1:gn) {
248+
if (sum(dm[i, , z]) == 0)
249+
count <- rep(0, 15)
250+
else
251+
count <- dm[i, , z] / sum(dm[i, , z])
252+
for (j in 1:gn)
253+
if (count[j] > 0)
254+
points(i, j, pch=22, cex=sc * sqrt(count[j]), lwd=sc3 * count[j])
255+
}
256+
}
257+
mtext("Question", side=1, line=0.2, outer=TRUE)
258+
mtext("Answer", side=2, line=0.2, outer=TRUE)
259+

0 commit comments

Comments
 (0)