{{ message }}

Instantly share code, notes, and snippets.

# chiral/spline_bayes.R

Created Dec 12, 2013
Eayes inference test with general multivariate distribution using spline interpolation.
 spline <- function(points, df) { smooth.spline(points[,1],points[,2],df=df) } general_sample <- function(weight, size) { weight\$y[which(weight\$y<0)] <- 0 r<-rmultinom(1,size,weight\$y) rr<-c() while (any(r>0)) { i<-which(r>0); rr<-c(rr,i); r[i]<-r[i]-1; } return(weight\$x[rr]) } like_base <- rbind( c(0,10), c(20,100), c(50,50), c(80,30), c(100,10) ) like_sp <- function(di) { spline(cbind(di*like_base[,1]/100,like_base[,2]),4) } pre_sp <- spline(rbind( c(1000,15), c(10000,25), c(30000,30), c(50000,20), c(100000,10) ),5) pre_sp2 <- spline(rbind( c(1000,10), c(10000,10), c(30000,10), c(50000,10), c(100000,10) ),5) pre_di <- predict(pre_sp,seq(1000,100000,1000)) post_di <- function(paid) { d <- pre_di for (i in 1:length(d\$x)) { x <- d\$x[i] y <- d\$y[i] w <- y*predict(like_sp(x),paid)\$y w[which(w<1)] <- 1 d\$y[i] <- exp(sum(log(w))) # multiply for all w } return(d) } d <- post_di(c(3000,2000,1000,2500)) plot(d\$x,d\$y) lines(d\$x,d\$y) #hist(general_sample(d,100000))