---
title: "Holmes_Burglary"
author: "Peiyuan Zhu"
date: "2024-04-16"
output: rmarkdown::html_vignette
# output: word_document
vignette: >
  %\VignetteIndexEntry{Holmes_Burglary}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
#devtools::load_all(".") # only used in place of dst when testing with R-devel
library(dst) 
knitr::opts_chunk$set(echo = TRUE)
```

Now we move on to section P13, 1.3 of Mathematical Theory of Hints. There are three SSMs that can be read from the model SSMa1: {a1 y, a1 n}, SSMa2: {a2 y, a2 n}, SSMR1: {burglary y, burglary n} x {a1 y, a1 n} x {alarm y, alarm n}, SSMR2: {a2 y, a2 n} x {alarm y, alarm n}, SSMR3: {burglary y, burglary n} x {a2 y, a2 n} x {alarm y, alarm n}. On top of which four DSMs DSMa1: P(SSMa1) -> [0,1], DSMa2: P(SSMa2) -> [0,1], DSM1: P(SSM1) -> [0,1], DSM2: P(SSM2) -> [0,1], DSM3: P(SSM3) -> [0,1] where P denotes "power set of ...". In dst in addition to SSMa1, SSMa2 we also need placeholder DSMalarm: P(SSMalarm) -> [0,1] on SSMalarm: {alarm y, alarm n} and DSMburglary: P(SSMburglary) -> [0,1] on SSMburglary: {burglary y, burglary n} for input and output and we don't need to define SSM1, SSM2, so overall three bcas and three bcaRels need to be defined. Hopefully we will remake dst so the new syntax allows only DSM, SSM.

```{r}
tt_SSMa1 <- matrix(c(1,0,0,1,1,1), nrow = 2 + 1, ncol = 2, byrow = TRUE)
m_DSMa1 <- matrix(c(0.95,0.05,0), nrow = 2 + 1, ncol = 1)
cnames_SSMa1 <- c("a1 is y", "a1 is n")
varnames_SSMa1 <- "a1"
idvar_SSMa1 <- 1
DSMa1 <- bca(tt_SSMa1, m_DSMa1, cnames = cnames_SSMa1, idvar = idvar_SSMa1, varnames = varnames_SSMa1)
bcaPrint(DSMa1)
```

```{r}
tt_SSMa2 <- matrix(c(1,0,0,1,1,1), nrow = 2 + 1, ncol = 2, byrow = TRUE)
m_DSMa2 <- matrix(c(0.01,0.99,0), nrow = 2 + 1, ncol = 1)
cnames_SSMa2 <- c("a2 is y", "a2 is n") 
varnames_SSMa2 <- "a2"
idvar_SSMa2 <- 2
DSMa2 <- bca(tt_SSMa2, m_DSMa2, cnames = cnames_SSMa2, idvar = idvar_SSMa2, varnames = varnames_SSMa2)
bcaPrint(DSMa2)
```

```{r}
tt_SSMalarm <- matrix(c(1,0,1,1), nrow = 1 + 1, ncol = 2, byrow = TRUE)
m_DSMalarm <- matrix(c(1,0), nrow = 1 + 1, ncol = 1)
cnames_SSMalarm <- c("alarm is y", "alarm is n") 
varnames_SSMalarm <- "alarm"
idvar_SSMalarm <- 3
DSMalarm <- bca(tt_SSMalarm, m_DSMalarm, cnames = cnames_SSMalarm, idvar = idvar_SSMalarm, varnames = varnames_SSMalarm)
bcaPrint(DSMalarm)
```

```{r}
tt_SSMburglary <- matrix(c(1,1), nrow = 1, ncol = 2, byrow = TRUE)
m_DSMburglary <- matrix(c(1), nrow = 1, ncol = 1)
cnames_SSMburglary <- c("burglary is y", "burglary is n") 
varnames_SSMburglary <- "burglary"
idvar_SSMburglary <- 4
DSMburglary <- bca(tt_SSMburglary, m_DSMburglary, cnames_SSMburglary, idvar = idvar_SSMburglary, varnames = varnames_SSMburglary)
bcaPrint(DSMburglary)
```

```{r}
tt_SSMR1 <- matrix(c(0,1,0,1,0,1,
                     1,0,0,1,0,1,
                     0,1,0,1,1,0,
                     1,0,0,1,1,0,
                     0,1,1,0,0,1,
                     0,1,1,0,1,0,
                     1,0,1,0,1,0,
                     1,1,1,1,1,1), nrow = 7 + 1, ncol = 6, byrow = TRUE, dimnames = list(NULL, c("burglary is y", "burglary is n", "a1 is y", "a1 is n", "alarm is y", "alarm is n")))
spec_DSMR1 <- matrix(c(1,1,1,1,1,1,1,2,
                       1,1,1,1,1,1,1,0), nrow = 7 + 1, ncol = 2)
infovar_SSMR1 <- matrix(c(4,1,3,2,2,2), nrow = 3, ncol = 2)
varnames_SSMR1 <- c("burglary", "a1", "alarm")
relnb_SSMR1 <- 1
DSMR1 <- bcaRel(tt_SSMR1, spec_DSMR1, infovar_SSMR1, varnames_SSMR1, relnb_SSMR1)
bcaPrint(DSMR1)
```

```{r}
tt_SSMR2 <- matrix(c(0,1,0,1,
                     0,1,1,0,
                     1,0,1,0,
                     1,1,1,1), nrow = 3 + 1, ncol = 4, byrow = TRUE, dimnames = list(NULL, c("a2 is y", "a2 is n", "alarm is y", "alarm is n")))
spec_DSMR2 <- matrix(c(1,1,1,2,1,1,1,0), nrow = 3 + 1, ncol = 2)
infovar_SSMR2 <- matrix(c(2,3,2,2), nrow = 2, ncol = 2)
varnames_SSMR2 <- c("a2", "alarm")
relnb_SSMR2 <- 2
DSMR2 <- bcaRel(tt_SSMR2, spec_DSMR2, infovar_SSMR2, varnames_SSMR2, relnb_SSMR2)
bcaPrint(DSMR2)
```

```{r}
tt_SSMR3 <- matrix(c(0,1,0,1,0,1,
                     1,0,0,1,0,1,
                     0,1,0,1,1,0,
                     1,0,0,1,1,0,
                     0,1,1,0,0,1,
                     0,1,1,0,1,0,
                     1,0,1,0,1,0,
                     1,1,1,1,1,1), nrow = 7 + 1, ncol = 6, byrow = TRUE, dimnames = list(NULL, c("burglary is y", "burglary is n", "a2 is y", "a2 is n", "alarm is y", "alarm is n")))
spec_DSMR3 <- matrix(c(1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,0), nrow = 7 + 1, ncol = 2)
infovar_SSMR3 <- matrix(c(4,2,3,2,2,2), nrow = 3, ncol = 2)
varnames_SSMR3 <- c("burglary", "a2", "alarm")
relnb_SSMR3 <- 3
DSMR3 <- bcaRel(tt_SSMR3, spec_DSMR3, infovar_SSMR3, varnames_SSMR3, relnb_SSMR3)
bcaPrint(DSMR3)
```

Next we traverse the graph in syntax of DS-ECP. If we project up DSMa1, DSMalarm from their own SSMs to SSMR1 we can combine resulting DSM with DSMR1 and project down onto DSM1 on SSM1: burgalry x alarm by eliminating a1. If we project up DSMa2, DSMalarm from their own SSMs to SSMR2 we can combine the resulting DSM with DSMR2, which can then be extended to combine with DSMR3 and then eliminate a2. Having gotten that one can combine the two DSMs between burglary x alarm and eliminate alarm.

```{r}
# combine DSMa1, DSMalarm with DSMR1 to eliminate a1 and get DSM1
DSMa1_uproj <- extmin(DSMa1, DSMR1)
DSMalarm_uproj <- extmin(DSMalarm, DSMR1)
DSM1 <- dsrwon(DSMa1_uproj, DSMR1)
DSM1 <- dsrwon(DSMalarm_uproj, DSM1)
DSM1_dproj <- elim(DSM1, 1)
bcaPrint(DSM1_dproj)
```

```{r}
# combine DSMa2, DSMalarm with DSMR2 to get DSM2
DSMa2_uproj <- extmin(DSMa2, DSMR2)
DSMalarm_uproj <- extmin(DSMalarm, DSMR2)
DSM2 <- dsrwon(DSMa2_uproj, DSMR2)
bcaPrint(DSM2)
```

```{r}
# combine DSM2 and DSMR3 to eliminate to eliminate a2 and get DSM4
DSM2_uproj <- extmin(DSM2, DSMR3)
DSM4 <- dsrwon(DSM2_uproj, DSMR3)
DSM4_dproj <- elim(DSM4, 2)
bcaPrint(DSM4_dproj)
```

```{r}
# combine DSM4 and DSM1 to eliminate alarm and get DSM5
DSM5 <- dsrwon(DSM4_dproj, DSM1_dproj)
bcaPrint(DSM4_dproj)
bcaPrint(DSM1_dproj)
bcaPrint(DSM5)
DSM5_dproj <- elim(DSM5, 3)
```

```{r}
DSA <- belplau(DSM5_dproj)
DSA
```

