Skip to content

Instantly share code, notes, and snippets.

@tpapp
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpapp/66efec47df7710062096 to your computer and use it in GitHub Desktop.
Save tpapp/66efec47df7710062096 to your computer and use it in GitHub Desktop.
example of truncated data inference, posterior mean vs posterior
N <- 100
w <- rlnorm(N,0,1)
a <- runif(N,0,1)
m <- 2
keep <- w < a*m
w_kept <- w[keep]
a_kept <- a[keep]
N_kept <- sum(keep)
data {
int<lower=1> N_kept;
real m;
}
parameters {
vector<lower=0,upper=1>[N_kept] a_kept;
vector<lower=0,upper=1>[N_kept] b;
}
model {
vector[N_kept] w_kept;
for (i in 1:N_kept) {
w_kept[i] <- b[i]*a_kept[i]*m;
}
increment_log_prob(log(a_kept));
a_kept ~ uniform(0,1);
w_kept ~ lognormal(0,1);
}
layout(1:2)
posterior_a_kept <- extract(fit_appendix_1,"a_kept")$a_kept
plot(a_kept, colMeans(posterior_a_kept),xlim=c(0,1),ylim=c(0,1))
plot(density(posterior_a_kept), xlim=c(0,1), lwd=2, col="red",
main="a_kept (data vs posterior)")
hist(a_kept, probability=TRUE, add=TRUE, breaks=10, col="#80ffff60",
border=NA)
legend("topleft", inset=0.1, col=c("black","#80ffff"), lty=1, lwd=2,
c("posterior", "data"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment