# More Complex Cases: Hybrid Bayesian Networks

## Introduction

These are answers and solutions to the exercises at the end of Part 3 in Bayesian Networks with Examples in R by M. Scutari and J.-B. Denis. Much of my inspiration for these solutions, where necessary, by consulting the solutions provided by the authors themselves as in the appendix.

## R Environment

For today’s exercise, I load the following packages:

library(bnlearn)


## Exercise 3.1

Explain why it is logical to get a three-step function for the discretised approach in Figure 3.2.

Figure 3.2 shows this discretisation:

the reason we obtain a three-step function here is down to the intervals that were chosen to bin the continuous diametre data into discrete categories:

$$<6.16$$ $$[6.16; 6.19]$$ $$>6.19$$

Owing to these intervals, we fit all of our continuous data into three categories which mirror the three-step function portrayed in the above figure.

## Exercise 3.2

Starting from the BUGS model in Section 3.1.1, write another BUGSmodel for the discretised model proposed in Section 3.1.2. The functions required for this task are described in the JAGS manual.

The model for the hybrid case (which we are to adapt to the discretised approach) reads as such:

model{
csup ~ dcat(sp);
cdiam ~ dnorm(mu[csup], 1/sigma^2);
}


To adapt it for discretised data, I simply change the outcome distribution for cdiam to also be categorical:

model{
csup ~ dcat(sp);
cdiam ~ dcat(Diams[, csup]);
}


where Diams is a matrix that contains probabilities for the different diameters (rows) for suppliers (columns).

## Exercise 3.3

Let d = 6.0, 6.1, 6.2, 6.4.

1. Using the BUGS model proposed in Section 3.1.1, write the R script to estimate $P(S = s1 | D = d)$ for the continuous approach demonstrated in the same section.
1. Using the BUGS model obtained in Exercise 3.2, write the R script to estimate $P(S = s1 | D = d)$ for the discretised approach suggested in Section 3.1.2.

And check the results with Figure 3.2.

## Exercise 3.4

In Section 3.1.1, the probability that the supplier is s1 knowing that the diameter is 6.2 was estimated to be 0.1824 which is not identical to the value obtained with JAGS.

1. Explain why the calculation with the R function dnorm is right and why the value 0.1824 is correct. Can you explain why the JAGS result is not exact? Propose a way to improve it.
2. Would this value be different if we modify the marginal distribution for the two suppliers?

## Exercise 3.5

Revisiting the discretisation in Section 3.1.2, compute the conditional probability tables for $D | S$ and $S | D$ when the interval boundaries are set to $(6.10, 6.18)$ instead of $(6.16, 6.19)$ . Compared to the results presented in Section 3.1.2, what is your conclusion?

## Session Info

sessionInfo()

## R version 4.0.5 (2021-03-31)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19043)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
## [5] LC_TIME=English_United Kingdom.1252
##
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base
##
## other attached packages:
## [1] bnlearn_4.6.1
##
## loaded via a namespace (and not attached):
##  [1] knitr_1.33        magrittr_2.0.1    R.cache_0.14.0    R6_2.5.0          rlang_0.4.11      fansi_0.4.2       stringr_1.4.0     styler_1.4.1      tools_4.0.5       parallel_4.0.5
## [11] xfun_0.22         R.oo_1.24.0       utf8_1.2.1        jquerylib_0.1.4   htmltools_0.5.1.1 ellipsis_0.3.2    yaml_2.2.1        digest_0.6.27     tibble_3.1.1      lifecycle_1.0.0
## [21] crayon_1.4.1      bookdown_0.22     purrr_0.3.4       R.utils_2.10.1    vctrs_0.3.7       sass_0.3.1        evaluate_0.14     rmarkdown_2.7     blogdown_1.3      stringi_1.5.3
## [31] compiler_4.0.5    bslib_0.2.4       pillar_1.6.0      R.methodsS3_1.8.1 backports_1.2.1   jsonlite_1.7.2    pkgconfig_2.0.3