{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Graphical Models"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Textbook Question\n",
"\n",
"- Q8.20: Induction on graph structure (recall from MATH1005/6005) (Difficulty $\\star$)\n",
"- Q8.21: Note typo: it should be $f_s(x_s)$ (Difficulty $\\star\\star$)\n",
"- Q8.27: Construct example showing greedy method not working (Difficulty $\\star\\star$)\n",
"- Q8.29: Induction on tree structure (recall from MATH1005/6005) (Difficulty $\\star\\star$)\n",
"- Extra: Derive eq 8.74 to 8.85 w.r.t Fig 8.51\n",
"\n",
"- Q10.2: Solving simulataneous equations (Difficulty $\\star$)\n",
"- Q10.3: Use lagrangian to enforce normalisation of q (Difficulty $\\star\\star$)\n",
"- Q10.6: Hint, how to introduce log term for both p and q? (Difficulty $\\star\\star$)\n",
"\n",
"- Q2.44: Manipulation with a more complex conjugate to derive the posterior (Difficulty $\\star\\star$)\n",
"\n",
"- Q10.7: Rewritting to the form of the respective distributions. Mostly algebra. (Difficulty $\\star\\star$)\n",
"- Q10.8: What will $b_n$ be approximated as? (Difficulty $\\star$)\n",
"- Q10.9: Essentially, deriving 10.31 and 10.32 (Difficulty $\\star\\star$)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### COMP4670/8600 - Statistical Machine Learning - Tutorial\n",
"\n",
"### Assumed knowledge\n",
"- Directed and undirected graphical models (lectures)\n",
"\n",
"### After this lab, you should be comfortable with:\n",
"- Basic operations and definitions of graphical models"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Setting up the environment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from IPython.display import display, Image"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This tutorial mainly includes three parts: Bayesian Networks (BN), Markov Random Field (MRF) and Sum Product Algorithm (Factor Graph). Before diving into the graphical models, we will first review some basic probability concepts. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reviewing Probability\n",
"\n",
"### Discrete Probability\n",
"\n",
"Recall the meaning of the following terms:\n",
"* Joint probability distribution\n",
"* Marginal distribution\n",
"* Conditional distribution"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Answer\n",
"*--- replace this with your solution, add and remove code and markdown cells as appropriate ---*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider the following table defining the joint probability distribution of two variables $A$ and $B$.\n",
"\n",
"| | A=$\\square$ | A=$\\bigcirc$ | A = $\\clubsuit$ | A = $\\heartsuit$ | A = $\\triangle$ |\n",
"|--|:--:|:--:|:--:|:--:|:--:|\n",
"|**B**=$p$|0.01|0.01|0.12|0.01|0.14|\n",
"|**B**=$q$|0.03|0.15|0.01|0.01|0.01|\n",
"|**B**=$r$|0.13|0.11|0.07|0.18|0.01|\n",
"\n",
"Compute the following distributions:\n",
"* $p(B=p | A = \\bigcirc)$\n",
"* $p(B | A = \\bigcirc)$\n",
"* $p(B)$\n",
"\n",
"You may do this calculation by hand or using python.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# replace this with your solution, add and remove code and markdown cells as appropriate"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Bayes Rule\n",
"\n",
"Recall that there are only two rules of probability, the sum rule and the product rule. Using these two rules, prove Bayes rule.\n",
"\n",
"$$p(Y|X) = \\frac{p(X|Y)p(Y)}{\\sum_Y p(X,Y)}$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Answer\n",
"*--- replace this with your solution, add and remove code and markdown cells as appropriate ---*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Empirical verification of Bayes rule\n",
"\n",
"Using the distribution $p(A,B)$ above, verify the Bayes rule i.e. \n",
"\n",
"$$p(A|B) = \\frac{p(B|A)p(A)}{\\sum_A p(A,B)}$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# replace this with your solution, add and remove code and markdown cells as appropriate"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dependent random variables\n",
"\n",
"Consider the following problem with 5 random variables.\n",
"* **A**ches with states (False, True)\n",
"* **B**ronchitis with states (none, mild, severe)\n",
"* **C**ough with states (False, True)\n",
"* **D**isease with states (healthy, carrier, sick, recovering)\n",
"* **E**mergency with states (False, True)\n",
"\n",
"How much memory is needed to store the joint probability distribution if:\n",
"* All variables are dependent?\n",
"* All variables are independent?\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Answer\n",
"*--- replace this with your solution, add and remove code and markdown cells as appropriate ---*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Bayesian Network\n",
"\n",
"Bayesian Network is directed graphical model expressing causal relationship between variables.\n",
"\n",
"Consider the following graphical model. Anwser the following questions:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Image(url=\"https://machlearn.gitlab.io/sml2020/tutorials/graphical_model.png\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"No1. Write down the joint factorisation for the above graph. \n",
"\n",
"No2. How much memory is needed to store the joint probability distribution?\n",
"\n",
"\n",
"#### Conditional Independence (D-seperation)\n",
"\n",
"If all paths are blocked between nodes $X$, $Y$ when a set of nodes $Z$ is observed, then $X$ is $d$-separated from $Y$ by $Z$ and $X \\perp Y | Z$. A path is blocked if it includes a node such that:\n",
"- the arrows on the path meet either head-to-tail or tail-to-tail at the node, and the node is in $Z$\n",
"- the arrows meet head-to-head at the node, and neither the node, nor any of its descendants, is in $Z$.\n",
"\n",
"No3. Identify and prove whether the conditional independences holds for the following cases: \n",
"\n",
"* A and D, when B is observed.\n",
"* B and C, when none of the variables are observed.\n",
"* B and C, when E is observed.\n",
"* A and C, when none of the variables are observed.\n",
"* A and C, when B is observed.\n",
"* A and E, when D is observed."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Answer\n",
"*--- replace this with your solution, add and remove code and markdown cells as appropriate ---*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Calculate distributions for BN\n",
"\n",
"Consider the following tables.\n",
"\n",
"|$p(B)$ | B=n | B=m | B=s |\n",
"|:-----:|:--:|:--:|:--:|\n",
"|marginal| 0.97 | 0.01 | 0.02 |\n",
"\n",
"|$p(C)$ | C=False | C=True |\n",
"|:-----:|:--:|:--:|\n",
"|marginal| 0.7 | 0.3 |\n",
"\n",
"| $p(A|B)$ | B=n | B=m | B=s |\n",
"|:-----:|:--:|:--:|:--:|\n",
"|**A**=False |0.9|0.8|0.3|\n",
"|**A**=True |0.1|0.2|0.7|\n",
"\n",
"| $p(D|B,C)$ | B=n, C=F | B=m, C=F | B=s, C=F | B=n, C=T | B=m, C=T | B=s, C=T |\n",
"|:-----:|:--:|:--:|:--:|:--:|:--:|:--:|\n",
"|**D**=healthy |0.9 |0.8 |0.1 | 0.3 |0.4 |0.01|\n",
"|**D**=carrier |0.08|0.17|0.01| 0.05|0.05|0.01|\n",
"|**D**=sick |0.01|0.01|0.87| 0.05|0.15|0.97|\n",
"|**D**=recovering|0.01|0.02|0.02| 0.6 |0.4 |0.01|\n",
"\n",
"| $p(E|D)$ | D=h | D=c | D=s | D=r |\n",
"|:-----:|:--:|:--:|:--:|\n",
"|**E**=False |0.99|0.99|0.4|0.9|\n",
"|**E**=True |0.01|0.01|0.6|0.1|\n",
"\n",
"\n",
"Compute the following:\n",
"* p(A,B,C,D,E)\n",
"* p(E)\n",
"* p(E|B=s)\n",
"* p(E|B=s, C=T)\n",
"\n",
"Note that there are two ways of arriving at the distributions:\n",
"1. By computing p(A,B,C,D,E) and marginalising and conditioning appropriately\n",
"2. By only computing the required distributions directly using the graphical model structure."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Answer\n",
"*--- replace this with your solution, add and remove code and markdown cells as appropriate ---*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Sampling a Discrete Probability Mass Function (PMF)\n",
"1. Code a class which represents a PMF, and has a ``sample`` method which draws a sample from the PMF.\n",
"2. Use it to draw samples from $p(C)$ and empirically compute and print the probability that your sampler returns the value ``False``."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# replace this with your solution, add and remove code and markdown cells as appropriate"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Markov Random Field\n",
"\n",
"For Bayesian Networks, create the cliques of a MRF by adding undirected edges bewteen all pairs of parents for each node in the graph. This process of 'marrying the parents' is called moralisation. But note that the resulting MRF may represent different conditional independence statements than the original BN. \n",
"\n",
"Convert the Bayesian Network above to a Markov Random Field. Draw the resulting network."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# replace this with your solution, add and remove code and markdown cells as appropriate"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Identify the maximal cliques in the graph and write down the corresponding clique potentials. Compare the conditional independence statements of the MRF with the BN."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Answer\n",
"*--- replace this with your solution, add and remove code and markdown cells as appropriate ---*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sum Product Algorithm\n",
"\n",
"The aim of this exercise is to implement the sum product algorithm on a chain graph."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Distributive law\n",
"\n",
"The [distributive property](http://en.wikipedia.org/wiki/Distributive_property) can be used to save computation, and is the basis of message passing and dynamic programming. See an [anecdote](http://bibiserv.techfak.uni-bielefeld.de/dynprog/node3_mn.html) about camels.\n",
"\n",
"Consider the following equation:\n",
"$$\n",
"2*3 + 2*5 = 2 * (3+5)\n",
"$$\n",
"\n",
"* How many mathematical operations (multiplications and additions) are on the left hand side?\n",
"* How many mathematical operations are on the right hand side?\n",
"\n",
"Construct a larger example where there is even more computational savings."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Answer\n",
"*--- replace this with your solution, add and remove code and markdown cells as appropriate ---*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Message passing\n",
"\n",
"Consider the following factor graph. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Image(url=\"https://machlearn.gitlab.io/sml2020/tutorials/message_passing.png\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The factors are given by the following tables:\n",
"\n",
"|f(A,B) | A=$\\square$ | A=$\\bigcirc$ | A = $\\clubsuit$ | A = $\\heartsuit$ | A = $\\triangle$ |\n",
"|--|:--:|:--:|:--:|:--:|:--:|\n",
"|**B**=$p$|0.01|0.01|0.12|0.01|0.14|\n",
"|**B**=$q$|0.03|0.15|0.01|0.01|0.01|\n",
"|**B**=$r$|0.13|0.11|0.07|0.18|0.01|\n",
"\n",
"|g(B,C) | B=$p$ | B=$q$ | B=$r$ |\n",
"|--|:--:|:--:|:--:|\n",
"|**C**=$w$|0.05|0.06|0.07|\n",
"|**C**=$x$|0.1|0.3|0.2|\n",
"|**C**=$y$|0.03|0.02|0.1|\n",
"|**C**=$z$|0.11|0.15|0.08|\n",
"\n",
"| | h(C) |\n",
"|--|:--:|\n",
"|**C**=$w$|1.2|\n",
"|**C**=$x$|3.2|\n",
"|**C**=$y$|1.8|\n",
"|**C**=$z$|2.3|\n",
"\n",
"Using the sum product algorithm, compute the marginal distribution of the random variable $B$.\n",
"\n",
"*Hint: Note that the factors are not normalised.*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Answer\n",
"*--- replace this with your solution, add and remove code and markdown cells as appropriate ---*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# replace this with your solution, add and remove code and markdown cells as appropriate"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 1
}