---
title: "Evidential_Modelling"
author: "Peiyuan Zhu"
date: "2024-04-16"
output: rmarkdown::html_vignette
# output: word_document
vignette: >
%\VignetteIndexEntry{Evidential_Modelling}
%\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 write examples from section 1.2 Mathematical Theory of Hints in the syntax of DS-ECP and translate that to the syntax of dst. Here, what's known as causal relations, often not unambiguously interpreted as "sufficient but not necessary cause", "sufficient and necessary cause", are more fundamentally evidential / functional relations / structural equations, as constraints between spaces in place describing interactions between the "cause", the "effect", while intentionally ignoring all other interactions with all others factors that are involved (interestingly, such contention exists in sociology / biology as the functionalist / structuralist view versus the more reductive views). If a probabilistically contingent relation between a cause and an effect is deemed fundamental, then one can in the name of pragmatism, model this contingent relation itself, without the unrealistic need to enumerate all causal relations as incoming arrows to the cause as one has to do in do-calculus, so that an inference on the effect is regarded as unconfounded.
Suppose a cause h of an effect e is necessary but insufficient. Let a be all that cannot be controlled but occurs with probability p. We can write out the constraining relations as $DSM1: H \times A \times E \rightarrow [0,1]$ on $SSM: H \times A \times E$which represents insufficiency, $DSM2: H \times E \rightarrow [0,1]$ on SSM $H \times E$ which represents necessity, and $DSM_a: A \rightarrow [0,1]$ where represents the uncontrolability of this relation. Besides, we also need $DSM_h: H \rightarrow [0,1]$ and $DSM_e: E \rightarrow [0,1]$ because that's what we condition on and want to infer, depending on whether it's a diagnostic evaluation or predictive evaluation. In a diagnostic evaluation we combine $DSM_e$ with the rest and infer $DSM_h$. In a predictive evaluation we combine $DSM_h$ with the rest and infer $DSM_e$.
First, we define the margins:
```{r}
tt_DSMa <- matrix(c(1,0,0,1,1,1), nrow = 2 + 1, ncol = 2, byrow = TRUE)
m_DSMa <- matrix(c(0.3,0.7,0), nrow = 2 + 1, ncol = 1)
cnames_DSMa <- c("a is T", "a is F")
varnames_DSMa <- "a"
idvar_DSMa <- 1
DSMa <- bca(tt_DSMa, m_DSMa, cnames = cnames_DSMa, idvar = idvar_DSMa, varnames = varnames_DSMa)
```
```{r}
tt_DSMh <- matrix(c(1,0,1,1), nrow = 1 + 1, ncol = 2, byrow = TRUE)
m_DSMh <- matrix(c(1,0), nrow = 1 + 1, ncol = 1)
cnames_DSMh <- c("h is T", "h is F")
varnames_DSMh <- "h"
idvar_DSMh <- 2
DSMh <- bca(tt_DSMh, m_DSMh, cnames = cnames_DSMh, idvar = idvar_DSMh, varnames = varnames_DSMh)
```
```{r}
tt_DSMe <- matrix(c(1,0,1,1), nrow = 1 + 1, ncol = 2, byrow = TRUE)
m_DSMe <- matrix(c(1,0), nrow = 1 + 1, ncol = 1)
cnames_DSMe <- c("e is T", "e is F")
varnames_DSMe <- "e"
idvar_DSMe <- 3
DSMe <- bca(tt_DSMe, m_DSMe, cnames = cnames_DSMe, idvar = idvar_DSMe, varnames = varnames_DSMe)
```
A truth table can be generated from https://web.stanford.edu/class/cs103/tools/truth-table-tool/ with argument "h && a -> e" and collecting only the rows where the implication is "true".
```{r}
tt_DSM1 <- matrix(c(0,1,0,1,0,1,
0,1,1,0,0,1,
0,1,0,1,1,0,
0,1,1,0,1,0,
1,0,0,1,0,1,
1,0,0,1,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("a is T", "a is F", "h is T", "h is F", "e is T", "e is F")))
spec_DSM1 <- matrix(c(1,1,1,1,1,1,1,2,
1,1,1,1,1,1,1,0), nrow = 7 + 1, ncol = 2)
infovar_DSM1 <- matrix(c(1,2,3,2,2,2), nrow = 3, ncol = 2)
varnames_DSM1 <- c("a", "h", "e")
relnb_DSM1 <- 1
DSM1 <- bcaRel(tt_DSM1, spec_DSM1, infovar_DSM1, varnames_DSM1, valuenames_DSM1, relnb_DSM1)
```
Now with the argument "not h -> not e", collecting the values where the implication is true gets:
```{r}
tt_DSM2 <- matrix(c(0,1,0,1,
1,0,0,1,
1,0,1,0,
1,1,1,1), nrow = 3 + 1, ncol = 4, byrow = TRUE, dimnames = list(NULL, c("h is T", "h is F", "e is T", "e is F")))
spec_DSM2 <- matrix(c(1,1,1,2,
1,1,1,0), nrow = 3 + 1, ncol = 2)
infovar_DSM2 <- matrix(c(2,3,2,2), nrow = 2, ncol = 2)
varnames_DSM2 <- c("h","e")
relnb_DSM2 <- 2
DSM2 <- bcaRel(tt_DSM2, spec_DSM2, infovar_DSM2, varnames_DSM2, valuenames_DSM2, relnb_DSM2)
```
The above definitions give us a bipartite graph where R1 links to h, a, e and R2 links to h, e. Consider predictive inference where h is known to be true. There are 2 ways of arranging a path of combination and elimination to arrive at e. One is a -> h -> e and the other is h -> a -> e.
Suppose we choose the first order of elimination a -> h -> e which is simpler than the h -> a -> e because the former only combines three times on spaces of two varaibles while the latter combines five times on the spaces of three variables, then:
```{r}
DSMa_uproj <- extmin(DSMa, DSM1)
DSMa_uproj_xDSC1_DSM1 <- dsrwon(DSMa_uproj, DSM1)
DSMa_uproj_xDSC1_DSM1_dproj <- elim(DSMa_uproj_xDSC1_DSM1, 1)
DSMh_uproj <- extmin(DSMh, DSM2)
DSMh_uproj_xDSC2_DSM2 <- dsrwon(DSMh_uproj, DSM2)
DSMa_uproj_xDSC1_DSM1_dproj_xDSC2_DSMh_uproj <- dsrwon(DSMa_uproj_xDSC1_DSM1_dproj, DSMh_uproj_xDSC2_DSM2)
DSMa_uproj_xDSC1_DSM1_dproj_xDSC2_DSMh_uproj_dproj <- elim(DSMa_uproj_xDSC1_DSM1_dproj_xDSC2_DSMh_uproj, 2)
DSMa_uproj_xDSC1_DSM1_dproj_xDSC2_DSMh_uproj_dproj_DSA <- belplau(DSMa_uproj_xDSC1_DSM1_dproj_xDSC2_DSMh_uproj_dproj)
DSMa_uproj_xDSC1_DSM1_dproj_xDSC2_DSMh_uproj_dproj_DSA
```
This is consistent with the result cred(e) = p and pl(e) = 1 on page 8 of Mathematical Theory of Hints.