Skip to content

Instantly share code, notes, and snippets.

@CnrLwlss
Created January 24, 2022 17:04
Show Gist options
  • Save CnrLwlss/fc4392e1e7b6336aa94b8bc1a9590f0e to your computer and use it in GitHub Desktop.
Save CnrLwlss/fc4392e1e7b6336aa94b8bc1a9590f0e to your computer and use it in GitHub Desktop.
Trying to visualise Beta prior predictives.
prior1 = function(n=1) {
runif(n,min=0.0,max=2.0)
}
prior2 = function(n=1) {
15
}
sampleprior = function(n=1){
return(replicate(n,rbeta(n=1,shape1=prior1(),shape2=prior2())))
}
plotprior = function(){
return(dbeta(seq(0,1,0.01),prior1(),prior2()))
}
op = par(mfrow=c(1,2))
plot(NULL,xlim=c(0,1),ylim=c(0,5),xlab="Value",ylab="Density",cex.lab=1.55,main="PDFs sampled from priors")
for(i in 1:2000) points(seq(0,1,0.01),plotprior(),type="l",lwd=3,col=rgb(0,0,0,0.005))
priorpred = sampleprior(100000)
d = density(priorpred)
h=hist(priorpred,breaks=seq(0,1,0.001),plot=FALSE)
plot(h$mids,h$density,xlim=c(0,1),lwd=3,type="l",xlab="Value",ylab="Density",main="Prior predictive distribution",cex.lab=1.55,ylim=c(0,5))
par(op)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment