library(gRain)
my.forensic.bn.2 <- function(n.p.type,N.p,n.g.type,Freq.g,frac.s,typing.prec.mat,L.prob,mot.levels,M.prob,Cr.prob,rs){
ps <- Gs <- list()
yn <- c("1","0")
p.type <- paste("p",1:n.p.type,sep="")
for(i in 1:n.p.type){
ps[[i]] <- cptable(~p,values=c(N.p[i],sum(N.p)-N.p[i]),levels=yn)
ps[[i]]$vpa[1] <- paste("p",i,sep="")
}
G.type <- paste("Gn",1:n.g.type,sep="")
for(i in 1:n.p.type){
Gs[[i]] <- cptable(~G,values=Freq.g,levels=G.type)
Gs[[i]]$vpa[1] <- paste("G",i,sep="")
}
tmp.exp <- expand.grid(rep(list(1:n.g.type),n.p.type))
tmp.val <- matrix(0,length(tmp.exp[,1]),n.g.type)
for(i in 1:length(tmp.val[,1])){
tmp.val[i,tmp.exp[i,1]] <- tmp.val[i,tmp.exp[i,1]]+frac.s
for(j in 2:n.p.type){
tmp.val[i,tmp.exp[i,j]] <- tmp.val[i,tmp.exp[i,j]]+(1-frac.s)/(n.p.type-1)
}
}
Sx <- cptable(~Sx,values=c(t(tmp.val)),levels=G.type)
for(i in 1:n.p.type){
Sx$vpa <- c(Sx$vpa,Gs[[i]]$vpa[1])
}
SGx <- cptable(~SGx|Sx,values=c(t(typing.prec.mat)),levels=G.type)
tmplist <- list(Sx,SGx)
tmplist1 <- list(Sx,SGx)
for(i in 1:length(ps)){
tmp.n <- length(tmplist)
tmplist[[tmp.n+1]] <- ps[[i]]
tmplist[[tmp.n+2]] <- Gs[[i]]
tmplist1[[length(tmplist1)+1]] <- Gs[[i]]
}
Ls <- list()
for(i in 1:n.p.type){
Ls[[i]] <- cptable(~l,values=c(L.prob[i,],0,1),levels=yn)
Ls[[i]]$vpa[1] <- paste("L",i,sep="")
Ls[[i]]$vpa <- c(Ls[[i]]$vpa,ps[[i]]$vpa[1])
tmplist[[length(tmplist)+1]] <- Ls[[i]]
}
Ms <- list()
for(i in 1:n.p.type){
Ms[[i]] <- cptable(~m,values=M.prob[i,],levels=mot.levels)
Ms[[i]]$vpa[1] <- paste("M",i,sep="")
Ms[[i]]$vpa <- c(Ms[[i]]$vpa,Ls[[i]]$vpa[1])
tmplist[[length(tmplist)+1]] <- Ms[[i]]
}
Cs <- list()
for(i in 1:n.p.type){
tmp <- cbind(Cr.prob,1-Cr.prob)
Cs[[i]] <- cptable(~cr,values=c(t(tmp),rep(0:1,length(mot.levels))),levels=yn)
Cs[[i]]$vpa[1] <- paste("C",i,sep="")
Cs[[i]]$vpa <- c(Cs[[i]]$vpa,Ms[[i]]$vpa[1],Ls[[i]]$vpa[1])
tmplist[[length(tmplist)+1]] <- Cs[[i]]
}
tmp.val <- expand.grid(rep(list(1:0),n.p.type))
ss <- apply(tmp.val,1,sum)
ari <- which(ss==1)
tmp.val2 <- rep(0,length(ss))
tmp.val2[ari] <- 1
tmp.val3 <- 1-tmp.val2
Cr <- cptable(~Cr,values=c(t(cbind(tmp.val2,tmp.val3))),levels=yn)
for(i in 1:n.p.type){
Cr$vpa <- c(Cr$vpa,Cs[[i]]$vpa[1])
}
tmplist[[length(tmplist)+1]] <- Cr
tmp.mat <- expand.grid(rep(list(1:0),n.p.type))
sum.tmp.mat <- apply(tmp.mat,1,sum)
tmp.mat[which(sum.tmp.mat==0),] <- 1
sum.tmp.mat <- apply(tmp.mat,1,sum)
tmp.mat2 <- tmp.mat/sum.tmp.mat
E <- cptable(~E,values=c(t(tmp.mat2)),levels=p.type)
for(i in 1:n.p.type){
E$vpa <- c(E$vpa,Cs[[i]]$vpa[1])
}
tmplist[[length(tmplist)+1]] <- E
Rs <- list()
for(i in 1:n.p.type){
tmp.val <- c(rep(c(rs[2],1-rs[2]),n.p.type),rep(c(0,1),n.p.type))
tmp.val[c((i-1)*2+1,i*2)] <- c(rs[1],1-rs[1])
Rs[[i]] <- cptable(~r,values=tmp.val,levels=yn)
Rs[[i]]$vpa[1] <- paste("R",i,sep="")
Rs[[i]]$vpa <- c(Rs[[i]]$vpa,E$vpa[1],Ls[[i]]$vpa[1])
tmplist[[length(tmplist)+1]] <- Rs[[i]]
}
Rr <- cptable(~Rr,values=c(rep(1:0,2^n.p.type-1),0,1),levels=yn)
for(i in 1:n.p.type){
Rr$vpa <- c(Rr$vpa,Rs[[i]]$vpa[1])
}
tmplist[[length(tmplist)+1]] <- Rr
A <- cptable(~A,values=c(t(tmp.mat2)),levels=p.type)
for(i in 1:n.p.type){
A$vpa <- c(A$vpa,Rs[[i]]$vpa[1])
}
tmplist[[length(tmplist)+1]] <- A
exx <- expand.grid(rep(list(1:n.g.type),n.p.type+1))
exx.1 <- exx[,1:n.p.type]
exx.2 <- exx[,n.p.type+1]
tmp.mat <- matrix(0,length(exx.2),n.g.type)
for(i in 1:length(exx.2)){
tmp.mat[i,exx.1[i,exx.2[i]]] <- 1
}
Ax <- cptable(~Ax,values=c(t(tmp.mat)),levels=G.type)
for(i in 1:n.p.type){
Ax$vpa <- c(Ax$vpa,Gs[[i]]$vpa)
}
Ax$vpa <- c(Ax$vpa,A$vpa[1])
tmplist[[length(tmplist)+1]] <- Ax
AGx <- cptable(~AGx|Ax,values=c(t(typing.prec.mat)),levels=G.type)
tmplist[[length(tmplist)+1]] <- AGx
plist <- compileCPT(tmplist)
plist1 <- compileCPT(tmplist1)
return(list(full.list=plist,sample.list=plist1))
}
n.p.type <- 2
N.p <- c(1,1000000)
n.g.type <- 2
Freq.g <- c(10^(-10),0)
Freq.g[2] <- 1-Freq.g[1]
frac.s <- 1
typing.prec.mat <- matrix(c(1,0,0,1),byrow=TRUE,2,2)
L.prob <- matrix(c(0.1,0.9,10000,990000),byrow=TRUE,ncol=2)
mot.levels <- c("1","0")
M.prob <- rbind(c(1,0,1,0),c(0.01,0.99,0,1))
Cr.prob <- c(1,0)
rs <- c(1,1)
yn <- c("1","0")
yn <- c("1","0")
p.type <- paste("p",1:n.p.type,sep="")
G.type <- paste("Gn",1:n.g.type,sep="")
my.bn <- my.forensic.bn.2(n.p.type,N.p,n.g.type,Freq.g,frac.s,typing.prec.mat,L.prob,mot.levels,M.prob,Cr.prob,rs)
my.net.full <- grain(my.bn$full.list)
querygrain(my.net.full,nodes=c("Cr"))
querygrain(my.net.full,nodes=c("C1","C2"),type="joint")
querygrain(my.net.full,nodes=c("E"))