---
title: "Simple_Implication"
author: "Peiyuan Zhu"
date: "2024-05-03"
output: rmarkdown::html_vignette
# output: word_document
vignette: >
  %\VignetteIndexEntry{Simple_Implication}
  %\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)
```

We use a simple implication relation to familiar ourselves with the DS-ECP syntax. Suppose we are sure about an implication and with a certain probability the premise is true and we don't know the rest. There are two SSMs. One is the premise and the other the conclusion. There are two DSMs. One is the probability on the first SSM and the other is the structural equation between the two SSMs. Besides the ttmatrix, m vector, it's necessary to give it varnames and idvar or else the extension would've failed. For the premise in the syntax of DS-ECP, we have $\text{SSM1}: A=\{a\text{ is }T, a\text{ is }F\}$ and $\text{DSM1}: \mathcal{P}(A) \rightarrow [0,1]$ where $$\mathcal{P}(A) = \{\emptyset, \{a\text{ is }T\}, \{a\text{ is }F\}, \{a\text{ is }T\text{ or }a\text{ is }F\}\}$$ and $$\text{DSM1}(\{a\text{ is }T\}) = p$$ and $$\text{DSM1}(\{a\text{ is }F\}) = 1 - p$$ This can be written in the syntax of dst as:

```{r}
DSM1_tt <- matrix(c(1,0,0,1,1,1), nrow = 2 + 1, ncol = 2, byrow = TRUE)
DSM1_m <- matrix(c(0.3,0.7,0), nrow = 2 + 1, ncol = 1)
DSM1_cnames <- c("a is True", "a is False")
DSM1_varnames <- c("a")
DSM1_idvar <- 1
DSM1 <- bca(DSM1_tt, DSM1_m, cnames = DSM1_cnames, idvar = DSM1_idvar, varnames = DSM1_varnames)
bcaPrint(DSM1)
```

Technically we have three overall DSMs with the addition of the conclusion DSM that we're interested in inferring. Similar to DSM1, we have:

```{r}
DSM3_tt <- matrix(c(1,1), nrow = 1, ncol = 2, byrow = TRUE)
DSM3_m <- matrix(c(1), nrow = 1, ncol = 1)
DSM3_cnames <- c("b is True", "b is False")
DSM3_varnames <- c("b")
DSM3_idvar <- 3
DSM3 <- bca(DSM3_tt, DSM3_m, cnames = DSM3_cnames, idvar = DSM3_idvar, varnames = DSM3_varnames)
bcaPrint(DSM3)
```

For the structural relation we can obtain the implication relation from https://web.stanford.edu/class/cs103/tools/truth-table-tool/.  Besides the ttmatrix, m vector, it's necesssary to give row names of the tt matrix, infovar, and varnames, or else the bcaRel argument checks would fail. In the syntax of DS-ECP, we define $\text{SSM2}: A \times B$ and $\text{DSM2}: \mathcal{P}(A \times B) \rightarrow [0,1]$ where $$\mathcal{P}(A \times B) = \{\emptyset, \{a\text{ is }T\text{ and }b\text{ is }T\}, ..., \\ \{a\text{ is }T\text{ and }b\text{ is }T\text{ or }a\text{ is }T\text{ and }b\text{ is }F\}, ..., \\ \{a\text{ is }T\text{ and }b\text{ is }T\text{ or }a\text{ is }T\text{ and }b\text{ is }F\text{ or }a\text{ is }F\text{ and }b\text{ is }T\}, ..., \\ \{a\text{ is }T\text{ and }b\text{ is }T\text{ or }a\text{ is }T\text{ and }b\text{ is }F\text{ or }a\text{ is }F\text{ and }b\text{ is }T\text{ or }a\text{ is }F\text{ and }b\text{ is }F\}\}$$ and $$\text{DSM2}(\{a\text{ is }T\text{ and }b\text{ is }T\text{ or }a\text{ is }F\text{ and }b\text{ is }T\text{ or }a\text{ is }F\text{ and }b\text{ is }F\}) = 1$$

```{r}
DSM2_tt <- 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("a is True", "a is False", "b is True", "b is False")))
DSM2_m <- matrix(c(1,1,1,2,
                   1,1,1,0), nrow = 3 + 1, ncol = 2)
DSM2_infovar <- matrix(c(1,3,2,2), nrow = 2, ncol = 2)
DSM2_varnames <- c("a","b")
DSM2 <- bcaRel(DSM2_tt, DSM2_m, DSM2_infovar, DSM2_varnames)
bcaPrint(DSM2)
```

Here we need to extend DSM1 to the joint space so it can be combined with DSM2. In the syntax of DS-ECP, we obtain $\text{uporj_DSM1}: \mathcal{P}(A \times B) \rightarrow [0,1]$ where $\mathcal{P}(A \times B)$ is as previously mentioned and $$\text{uporj_DSM1}(\{a\text{ is }T\text{ and }b\text{ is }T\text{ or }a\text{ is }T\text{ and }b\text{ is }F\}) = p$$ and $$\text{uporj_DSM1}(\{a\text{ is }F\text{ and }b\text{ is }T\text{ or }a\text{ is }F\text{ and }b\text{ is }F\}) = 1 - p$$ In the syntax of dst, this is:

```{r}
uporj_DSM1 <- extmin(DSM1, DSM2)
bcaPrint(uporj_DSM1)
```

Now we can combine the two DSMs over the joint space. In the syntax of DS-ECP, we have $\text{uporj_DSM1_xDSC_DSM2}: \mathcal{P}(A \times B) \rightarrow [0,1]$ and $$\text{uporj_DSM1_xDSC_DSM2}(\{a\text{ is }T\text{ and }b\text{ is }T\}) = p$$ and $$\text{uporj_DSM1_xDSC_DSM2}(a\text{ is }F\text{ and }b\text{ is }T\text{ or }a\text{ is }F\text{ and }b\text{ is }F\}) = 1 - p$$ In the syntax of dst, we have:

```{r}
uporj_DSM1_xDSC_DSM2 <- dsrwon(uporj_DSM1, DSM2)
bcaPrint(uporj_DSM1_xDSC_DSM2)
```

After obtaining the DSM over the joint space, we can project it down by marginalizing out the premise. In the syntax of DS-ECP, we obtained $\text{uproj_DSM1_xDSC_DSM2_dproj}: \mathcal{P}(B) \rightarrow [0,1]$ and $$\text{uporj_DSM1_xDSC_DSM2_dproj}(\{b\text{ is }T\}) = p$$ and $$\text{uporj_DSM1_xDSC_DSM2_dproj}(\{b\text{ is }T\text{ or }b\text{ is }F\}) = 1 - p$$

```{r}
uporj_DSM1_xDSC_DSM2_dproj <- elim(uporj_DSM1_xDSC_DSM2, 1)
bcaPrint(uporj_DSM1_xDSC_DSM2_dproj)
```

Now we can evaluate the belief and plausibility of this DSM. Here the $\text{DSA}:(p,q,r)$ triplet corresponds to $(\text{bel},\text{disbel},\text{unc})$ in the belplau table.

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

