nutterb / HydeNet

Hybrid Decision Networks in R
Other
23 stars 3 forks source link

Inaccurate HydeSim() predictions with discrete nodes #108

Open derekpowell opened 5 years ago

derekpowell commented 5 years ago

I'm finding that HydeNet is giving pretty inaccurate predictions for a very simple completely discrete network. I'm wondering if this is a bug.

Here's a reproducible example for a two node network. The network a single parent and a single "evidence" node child:

node1 --> evid

Where p(node1) = .50, p(evid|node1) = .80 and p(evid|~node1) = .50.

make_custom_cpt <- function(ps, dims, dim_names, data) {
  # makes custom cpt in HydeNet format
  output <- array(ps, dim=dims, dimnames=dim_names)
  attr(output, "model") <- data
  attr(output, "class") <- c("cpt","array")

  return(output)
}

set.seed(42)
# first create some easily-calculated CPTs
n1 <- xtabs(~ node1, data = data.frame(node1 = c(rep("Yes",500), rep("No",500))))
n2 <- make_custom_cpt(c(.80, .5, .2, .5), c(2,2), list(node1=c("Yes","No"), evid = c("Yes","No") ), NULL)

mynet <- HydeNetwork(list(n1, n2))

# predict conditioning on no evidence, and observing the evidence
model1 <- compileJagsModel(mynet, data= data.frame(evid=2))

post1 <- HydeSim(model1,
                 variable.names = c("node1"),
                 n.iter = 1e6
)

p_obs_evidence <- mean(as.numeric(post1$node1)) - 1

p_obs_evidence

The result is:

> p_obs_evidence
[1] 0.714207

But this is easy enough to solve analytically by hand, and it should be .615. So HydeSim() is getting P(node1|evidence) pretty seriously wrong. Is this a bug?