├── .DS_Store ├── hw ├── .DS_Store ├── homework1.pdf ├── homework2.pdf ├── homework3.pdf ├── homework4.pdf ├── source │ ├── .DS_Store │ ├── graph.png │ ├── fig_hw3.png │ ├── figure.png │ ├── brown_hw1.pdf │ ├── brown_hw2.png │ ├── homework2.tex │ ├── homework1.tex │ └── homework3.tex ├── instructions.txt ├── make_debug_graph.ipynb ├── .ipynb_checkpoints │ └── make_debug_graph-checkpoint.ipynb └── sequence.txt ├── materials ├── .DS_Store ├── Nov12.PNG ├── Nov19.PNG ├── Nov5.PNG ├── Oct8.PNG ├── Sep10.PNG ├── Sep17.PNG ├── Sep24.PNG ├── Oct1-1.PNG ├── Oct1-2.PNG ├── review.pdf └── example_posterior_computation.ipynb ├── final_project_proposal_inf.pdf └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/.DS_Store -------------------------------------------------------------------------------- /hw/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/hw/.DS_Store -------------------------------------------------------------------------------- /hw/homework1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/hw/homework1.pdf -------------------------------------------------------------------------------- /hw/homework2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/hw/homework2.pdf -------------------------------------------------------------------------------- /hw/homework3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/hw/homework3.pdf -------------------------------------------------------------------------------- /hw/homework4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/hw/homework4.pdf -------------------------------------------------------------------------------- /hw/source/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/hw/source/.DS_Store -------------------------------------------------------------------------------- /hw/source/graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/hw/source/graph.png -------------------------------------------------------------------------------- /materials/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/materials/.DS_Store -------------------------------------------------------------------------------- /materials/Nov12.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/materials/Nov12.PNG -------------------------------------------------------------------------------- /materials/Nov19.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/materials/Nov19.PNG -------------------------------------------------------------------------------- /materials/Nov5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/materials/Nov5.PNG -------------------------------------------------------------------------------- /materials/Oct8.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/materials/Oct8.PNG -------------------------------------------------------------------------------- /materials/Sep10.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/materials/Sep10.PNG -------------------------------------------------------------------------------- /materials/Sep17.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/materials/Sep17.PNG -------------------------------------------------------------------------------- /materials/Sep24.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/materials/Sep24.PNG -------------------------------------------------------------------------------- /hw/source/fig_hw3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/hw/source/fig_hw3.png -------------------------------------------------------------------------------- /hw/source/figure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/hw/source/figure.png -------------------------------------------------------------------------------- /materials/Oct1-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/materials/Oct1-1.PNG -------------------------------------------------------------------------------- /materials/Oct1-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/materials/Oct1-2.PNG -------------------------------------------------------------------------------- /materials/review.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/materials/review.pdf -------------------------------------------------------------------------------- /hw/source/brown_hw1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/hw/source/brown_hw1.pdf -------------------------------------------------------------------------------- /hw/source/brown_hw2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/hw/source/brown_hw2.png -------------------------------------------------------------------------------- /final_project_proposal_inf.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solevillar/Inference_and_Representation/HEAD/final_project_proposal_inf.pdf -------------------------------------------------------------------------------- /hw/instructions.txt: -------------------------------------------------------------------------------- 1 | The homework should be uploaded to NYU classes as pdf. 2 | Collaboration is allowed but each student should write their own solutions independently. 3 | -------------------------------------------------------------------------------- /materials/example_posterior_computation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import scipy.special as sp" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "Example from class: we have 5 urns numbered ui i=0,1,2,3,4, each of urns has i black balls and 4-i white balls. I perform the following experiment: I choose an urn and draw 5 balls with replacement from it. I don't tell you which urn I chose, but I tell you that in the experiment I drew 2 black balls and 3 white ones. \n", 17 | "Question: what is the probability that I chose the urn ui?" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 7, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "#b: total black balls in the urn\n", 27 | "#k: total balls in the urn (4 in our example)\n", 28 | "#nb: number of black balls drawn in the experiment\n", 29 | "#N: total number of balls drawn in the experiment\n", 30 | "\n", 31 | "#forward probability P(nb|u,N)\n", 32 | "def P(b, k, nb, N):\n", 33 | " return sp.binom(N, nb)*(b/k)**nb*(1-b/k)**(N-nb)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 17, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "data": { 43 | "text/plain": [ 44 | "0.1328125" 45 | ] 46 | }, 47 | "execution_count": 17, 48 | "metadata": {}, 49 | "output_type": "execute_result" 50 | } 51 | ], 52 | "source": [ 53 | "#P(nb|N) \n", 54 | "norm_constant=sum([P(i,4,2,5)*(1/5) for i in [0,1,2,3,4]])\n", 55 | "norm_constant" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 18, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "#P(u|nb, N)=P(nb|u, N)*P(u)/P(nb|b)\n", 65 | "#we assume uniform prior (each of the urns is chosen with same probability)\n", 66 | "#u=1\n", 67 | "u0=P(0,4,2,5)*(1/5)/norm_constant\n", 68 | "u1=P(1,4,2,5)*(1/5)/norm_constant\n", 69 | "u2=P(2,4,2,5)*(1/5)/norm_constant\n", 70 | "u3=P(3,4,2,5)*(1/5)/norm_constant\n", 71 | "u4=P(4,4,2,5)*(1/5)/norm_constant" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 21, 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "name": "stdout", 81 | "output_type": "stream", 82 | "text": [ 83 | "0.0 0.39705882352941174 0.47058823529411764 0.1323529411764706 0.0\n" 84 | ] 85 | } 86 | ], 87 | "source": [ 88 | "print(u0,u1,u2,u3, u4)\n", 89 | "#observe that u0 and u4 have probability 0, since the outcome of the experiment is impossible in this setting" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 20, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "data": { 99 | "text/plain": [ 100 | "1.0" 101 | ] 102 | }, 103 | "execution_count": 20, 104 | "metadata": {}, 105 | "output_type": "execute_result" 106 | } 107 | ], 108 | "source": [ 109 | "sum([u1,u2,u3])" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 22, 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "data": { 119 | "text/plain": [ 120 | "0.4338235294117647" 121 | ] 122 | }, 123 | "execution_count": 22, 124 | "metadata": {}, 125 | "output_type": "execute_result" 126 | } 127 | ], 128 | "source": [ 129 | "# I draw another ball from the same urn. What is the probability that it's black?\n", 130 | "1/4*u1+2/4*u2+3/4*u3" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [] 139 | } 140 | ], 141 | "metadata": { 142 | "kernelspec": { 143 | "display_name": "Python 3", 144 | "language": "python", 145 | "name": "python3" 146 | }, 147 | "language_info": { 148 | "codemirror_mode": { 149 | "name": "ipython", 150 | "version": 3 151 | }, 152 | "file_extension": ".py", 153 | "mimetype": "text/x-python", 154 | "name": "python", 155 | "nbconvert_exporter": "python", 156 | "pygments_lexer": "ipython3", 157 | "version": "3.7.3" 158 | } 159 | }, 160 | "nbformat": 4, 161 | "nbformat_minor": 2 162 | } 163 | -------------------------------------------------------------------------------- /hw/make_debug_graph.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "PS 3 - Question 2
\n", 8 | "Inference and Representation
\n", 9 | "NYU Center for Data Science
\n", 10 | "October 3, 2017\n", 11 | "\n", 12 | "It is a Python adaptation of the Matlab code provided in Brown University CS242 Homework 1:\n", 13 | "http://cs.brown.edu/courses/cs242/assignments/\n", 14 | "The factor graph library (fglib) is a Python 3 package to simulate message passing on factor graphs: https://github.com/danbar/fglib" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 210, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "import numpy as np \n", 24 | "import networkx as nx\n", 25 | "from fglib import graphs, nodes, rv\n", 26 | "\n", 27 | "def make_debug_graph():\n", 28 | "\n", 29 | " # Create factor graph\n", 30 | " fg = graphs.FactorGraph()\n", 31 | "\n", 32 | " # Create variable nodes\n", 33 | " x1 = nodes.VNode(\"x1\")\n", 34 | " x2 = nodes.VNode(\"x2\")\n", 35 | " x3 = nodes.VNode(\"x3\")\n", 36 | " x4 = nodes.VNode(\"x4\")\n", 37 | "\n", 38 | " # Create factor nodes\n", 39 | " f12 = nodes.FNode(\"f12\")\n", 40 | " f234 = nodes.FNode(\"f234\")\n", 41 | " f3 = nodes.FNode(\"f3\")\n", 42 | " f4 = nodes.FNode(\"f4\")\n", 43 | "\n", 44 | " # Add nodes to factor graph\n", 45 | " fg.set_nodes([x1, x2, x3, x4])\n", 46 | " fg.set_nodes([f12, f234, f3,f4 ])\n", 47 | "\n", 48 | " # Add edges to factor graph\n", 49 | " fg.set_edge(x1, f12)\n", 50 | " fg.set_edge(f12, x2)\n", 51 | " fg.set_edge(x2, f234)\n", 52 | " fg.set_edge(f234, x3)\n", 53 | " fg.set_edge(f234, x4)\n", 54 | " fg.set_edge(x3, f3)\n", 55 | " fg.set_edge(x4, f4)\n", 56 | "\n", 57 | " #add potential for f_3: p(x3)\n", 58 | " dist_f3 = [0.5, 0.5]\n", 59 | " f3.factor = rv.Discrete(dist_f3,x3)\n", 60 | " \n", 61 | " #add potential for f_4: p(x4)\n", 62 | " dist_f4 = [0.4,0.6]\n", 63 | " f4.factor = rv.Discrete(dist_f4,x4)\n", 64 | " \n", 65 | " # add potential for f_{234}: p(x2, x3, x4) = p(x2|x3,x4) p(x3,x4)\n", 66 | " px3x4=np.outer(dist_f3,dist_f4)\n", 67 | " px3x4=np.reshape(px3x4, np.shape(px3x4)+(1,))\n", 68 | " px2_conditioned_x3x4=[[[0.2,0.8],\n", 69 | " [0.25,0.75],],\n", 70 | " [[0.7,0.3],\n", 71 | " [0.3,0.7]]]\n", 72 | " \n", 73 | " dist_f234 =px3x4*px2_conditioned_x3x4\n", 74 | " f234.factor = rv.Discrete(dist_f234,x3,x4,x2)\n", 75 | " \n", 76 | " # add potential for f_{12}: p (x1,x2) = p(x1 | x2) p(x2)\n", 77 | " px1_conditioned_x2 = [[0.5,0.5],\n", 78 | " [0.7,0.3]]\n", 79 | " px2= np.sum(dist_f234, axis=(0,1))\n", 80 | " dist_f12 =px2[:,np.newaxis]*px1_conditioned_x2\n", 81 | " f12.factor = rv.Discrete(dist_f12,x2,x1)\n", 82 | " # Perform sum-product algorithm on factor graph\n", 83 | " # and request belief of variable node x1\n", 84 | " belief = inference.sum_product(fg, x3)\n", 85 | " return (fg)\n" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": { 92 | "collapsed": true 93 | }, 94 | "outputs": [], 95 | "source": [ 96 | "def get_beliefs(fg)\n", 97 | " ##TO DO\n", 98 | "\n", 99 | "fg=make_debug_graph()\n", 100 | "beliefs = get_beliefs(fg)\n", 101 | "# Print belief of variable nodes\n", 102 | "print(\"Belief of variable nodes \")\n", 103 | "print(beliefs)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | " " 111 | ] 112 | } 113 | ], 114 | "metadata": { 115 | "anaconda-cloud": {}, 116 | "celltoolbar": "Raw Cell Format", 117 | "kernelspec": { 118 | "display_name": "Python 3", 119 | "language": "python", 120 | "name": "python3" 121 | }, 122 | "language_info": { 123 | "codemirror_mode": { 124 | "name": "ipython", 125 | "version": 3 126 | }, 127 | "file_extension": ".py", 128 | "mimetype": "text/x-python", 129 | "name": "python", 130 | "nbconvert_exporter": "python", 131 | "pygments_lexer": "ipython3", 132 | "version": "3.7.3" 133 | } 134 | }, 135 | "nbformat": 4, 136 | "nbformat_minor": 1 137 | } 138 | -------------------------------------------------------------------------------- /hw/.ipynb_checkpoints/make_debug_graph-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "PS 3 - Question 2
\n", 8 | "Inference and Representation
\n", 9 | "NYU Center for Data Science
\n", 10 | "October 3, 2017\n", 11 | "\n", 12 | "It is a Python adaptation of the Matlab code provided in Brown University CS242 Homework 1:\n", 13 | "http://cs.brown.edu/courses/cs242/assignments/\n", 14 | "The factor graph library (fglib) is a Python 3 package to simulate message passing on factor graphs: https://github.com/danbar/fglib" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 210, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "import numpy as np \n", 24 | "import networkx as nx\n", 25 | "from fglib import graphs, nodes, rv\n", 26 | "\n", 27 | "def make_debug_graph():\n", 28 | "\n", 29 | " # Create factor graph\n", 30 | " fg = graphs.FactorGraph()\n", 31 | "\n", 32 | " # Create variable nodes\n", 33 | " x1 = nodes.VNode(\"x1\")\n", 34 | " x2 = nodes.VNode(\"x2\")\n", 35 | " x3 = nodes.VNode(\"x3\")\n", 36 | " x4 = nodes.VNode(\"x4\")\n", 37 | "\n", 38 | " # Create factor nodes\n", 39 | " f12 = nodes.FNode(\"f12\")\n", 40 | " f234 = nodes.FNode(\"f234\")\n", 41 | " f3 = nodes.FNode(\"f3\")\n", 42 | " f4 = nodes.FNode(\"f4\")\n", 43 | "\n", 44 | " # Add nodes to factor graph\n", 45 | " fg.set_nodes([x1, x2, x3, x4])\n", 46 | " fg.set_nodes([f12, f234, f3,f4 ])\n", 47 | "\n", 48 | " # Add edges to factor graph\n", 49 | " fg.set_edge(x1, f12)\n", 50 | " fg.set_edge(f12, x2)\n", 51 | " fg.set_edge(x2, f234)\n", 52 | " fg.set_edge(f234, x3)\n", 53 | " fg.set_edge(f234, x4)\n", 54 | " fg.set_edge(x3, f3)\n", 55 | " fg.set_edge(x4, f4)\n", 56 | "\n", 57 | " #add potential for f_3: p(x3)\n", 58 | " dist_f3 = [0.5, 0.5]\n", 59 | " f3.factor = rv.Discrete(dist_f3,x3)\n", 60 | " \n", 61 | " #add potential for f_4: p(x4)\n", 62 | " dist_f4 = [0.4,0.6]\n", 63 | " f4.factor = rv.Discrete(dist_f4,x4)\n", 64 | " \n", 65 | " # add potential for f_{234}: p(x2, x3, x4) = p(x2|x3,x4) p(x3,x4)\n", 66 | " px3x4=np.outer(dist_f3,dist_f4)\n", 67 | " px3x4=np.reshape(px3x4, np.shape(px3x4)+(1,))\n", 68 | " px2_conditioned_x3x4=[[[0.2,0.8],\n", 69 | " [0.25,0.75],],\n", 70 | " [[0.7,0.3],\n", 71 | " [0.3,0.7]]]\n", 72 | " \n", 73 | " dist_f234 =px3x4*px2_conditioned_x3x4\n", 74 | " f234.factor = rv.Discrete(dist_f234,x3,x4,x2)\n", 75 | " \n", 76 | " # add potential for f_{12}: p (x1,x2) = p(x1 | x2) p(x2)\n", 77 | " px1_conditioned_x2 = [[0.5,0.5],\n", 78 | " [0.7,0.3]]\n", 79 | " px2= np.sum(dist_f234, axis=(0,1))\n", 80 | " dist_f12 =px2[:,np.newaxis]*px1_conditioned_x2\n", 81 | " f12.factor = rv.Discrete(dist_f12,x2,x1)\n", 82 | " # Perform sum-product algorithm on factor graph\n", 83 | " # and request belief of variable node x1\n", 84 | " belief = inference.sum_product(fg, x3)\n", 85 | " return (fg)\n" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": { 92 | "collapsed": true 93 | }, 94 | "outputs": [], 95 | "source": [ 96 | "def get_beliefs(fg)\n", 97 | " ##TO DO\n", 98 | "\n", 99 | "fg=make_debug_graph()\n", 100 | "beliefs = get_beliefs(fg)\n", 101 | "# Print belief of variable nodes\n", 102 | "print(\"Belief of variable nodes \")\n", 103 | "print(beliefs)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | " " 111 | ] 112 | } 113 | ], 114 | "metadata": { 115 | "anaconda-cloud": {}, 116 | "celltoolbar": "Raw Cell Format", 117 | "kernelspec": { 118 | "display_name": "Python 3", 119 | "language": "python", 120 | "name": "python3" 121 | }, 122 | "language_info": { 123 | "codemirror_mode": { 124 | "name": "ipython", 125 | "version": 3 126 | }, 127 | "file_extension": ".py", 128 | "mimetype": "text/x-python", 129 | "name": "python", 130 | "nbconvert_exporter": "python", 131 | "pygments_lexer": "ipython3", 132 | "version": "3.7.3" 133 | } 134 | }, 135 | "nbformat": 4, 136 | "nbformat_minor": 1 137 | } 138 | -------------------------------------------------------------------------------- /hw/source/homework2.tex: -------------------------------------------------------------------------------- 1 | \documentclass[11pt]{article} 2 | \usepackage{amsmath} 3 | \usepackage{geometry} % See geometry.pdf to learn the layout options. There are lots. 4 | \geometry{letterpaper} % ... or a4paper or a5paper or ... 5 | %\geometry{landscape} % Activate for for rotated page geometry 6 | %\usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent 7 | \usepackage{graphicx} 8 | \usepackage{fullpage} 9 | \usepackage{amssymb} 10 | \usepackage{epstopdf} 11 | \usepackage{url} 12 | \DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png} 13 | \date{} 14 | \title{Homework 2} 15 | \author{(Inference and Representation) 16 | \\ Due October 2 on NYU Classes. } 17 | %\date{} % Activate to display a given date or no date 18 | 19 | \begin{document} 20 | \maketitle 21 | 22 | \begin{enumerate} 23 | \item Consider the Bayesian Network below: 24 | 25 | \begin{center} 26 | \includegraphics[width=.3\textwidth]{graph} 27 | \end{center} 28 | \begin{enumerate} 29 | \item For what pairs $(i,j)$ does the statement $X_i \perp X_j$ hold? (Do not assume any conditioning for this part). 30 | \item Suppose that we condition on $\{X_2, X_9\}$, shown shaded in the graph. What is the largest set $A$ for which the statement $X_1 \perp X_A | \{X_2, X_9\}$ holds? 31 | \end{enumerate} 32 | 33 | \item Consider the following distribution over 3 binary variables $X,Y,Z:$ 34 | $$ 35 | \operatorname{Pr}(x,y,z)=\left\{ 36 | \begin{matrix} 37 | 1/12 & x \oplus y \oplus z =0 \\ 38 | 1/6 & x \oplus y \oplus z =1 39 | \end{matrix} 40 | \right. 41 | $$ 42 | where $\oplus$ denotes the XOR function. Show that there is no directed acyclic graph $G$ such that $I_{d-sep}(G)= I(Pr)$. 43 | 44 | 45 | \item The Ising model, from statistical physics, consists of discrete variables that represent magnetic spins that can be in one of two states (+1 or -1). The spins are arranged in a graph, usually a lattice, allowing each spin to interact with its neighbors. It satisfies the distribution 46 | \begin{equation} \label{ising} 47 | \operatorname{Pr}(x_1,\ldots, x_n)= \frac{1}{Z} \exp\left(\sum_{(i,j)\in E} w_{i,j}x_i x_j - \sum_{i\in V} u_i x_i \right), 48 | \end{equation} 49 | where the random variables $X_{i}\in \{+1, -1\}$. Related to the Ising model is the Boltzmann machine, which is an early model for neural networks. Boltzmann machines are parameterized the same way (i.e., using \eqref{ising}), but it has variables $X_i \in\{ 0, 1\}$. Here we get a non-zero contribution to the energy (i.e. the quantity in the parentheses in \eqref{ising}) from an edge $(i, j)$ only when $X_i = X_j = 1$. 50 | Show that a Boltzmann machine distribution can be rewritten as an Ising model. More specifically, given parameters $\mathbf w, \mathbf u$ corresponding to a Boltzmann machine, specify new parameters $\mathbf{w'}, \mathbf {u'}$ for an Ising model and prove that they give the same distribution Pr$(X)$ (assuming the state space $\{0, 1\}$ is mapped to $\{-1, +1\}$). 51 | 52 | \item Let $T$ denote the edges of a tree-structured pairwise Markov random field with vertices $V$ . For the special case of trees, prove that any distribution $p_T(x)$ corresponding to a Markov random field over $T$ admits a factorization of the form: 53 | $$p_T(x)=\Pi_{(i,j)\in T} \frac{p_T(x_i,x_j)}{p_T(x_i)p_T(x_j)} \Pi_{j\in V} p_T(x_j), $$ 54 | where $p_T(x_i,x_j)$ and $p_T(x_i)$ denote pairwise and singleton marginals of the distribution $p_T$ , respectively. 55 | 56 | Hint: consider the Bayesian network where you choose an arbitrary node to be a root and direct all edges away from the root. Show that this is equivalent to the MRF. Then, looking at the BN’s factorization, reshape it into the required form. 57 | 58 | \item Recall the occasionally dishonest casino example we saw in class (from Durbin et al. 1998, see chapter 17 of Murphy). A casino has two dice, one is fair (every number in $\{1,2,3,4,5,6\}$ is cast with the same probability) and the other one is loaded but we don't know the exact distribution of its outputs. The casino starts using a fair die but with some unknown probability it switches to the loaded die back and forth. The goal of this problem is to learn the HMM model from the dataset \texttt{sequence.txt}. In particular the transition probabilities between the hidden states (loaded to fair and fair to loaded) and the probability distribution of the output of the loaded dice. 59 | 60 | Someone suggested that the casino had actually three different dice, one fair and two loaded with different probabilities. How would the Hidden Markov Model look in this case? Which one do you think is more likely? 61 | 62 | Hint: The Baum-Welch algorithm may be useful. If you use a software package you need to explain what it's doing. 63 | 64 | \end{enumerate} 65 | \end{document} -------------------------------------------------------------------------------- /hw/source/homework1.tex: -------------------------------------------------------------------------------- 1 | \documentclass[11pt]{article} 2 | \usepackage{amsmath} 3 | \usepackage{geometry} % See geometry.pdf to learn the layout options. There are lots. 4 | \geometry{letterpaper} % ... or a4paper or a5paper or ... 5 | %\geometry{landscape} % Activate for for rotated page geometry 6 | %\usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent 7 | \usepackage{graphicx} 8 | \usepackage{fullpage} 9 | \usepackage{amssymb} 10 | \usepackage{epstopdf} 11 | \usepackage{url} 12 | \DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png} 13 | \date{} 14 | \title{Homework 1} 15 | \author{(Inference and Representation) 16 | \\ Due September 16 on NYU Classes. } 17 | %\date{} % Activate to display a given date or no date 18 | 19 | \begin{document} 20 | \maketitle 21 | 22 | Problems 1 and 2 are from Murphy, chapter 2. Problems 3 and 4 are from Joan Bruna's course. 23 | \begin{enumerate} 24 | \item Probabilities are sensitive to the form of the question that was used to generate the answer. My neighbor has two children. Assuming that the gender of a child is like a coin flip, it is most likely, a priori, that my neighbor has one boy and one girl, with probability 1/2. The other possibilities—two boys or two girls—have probabilities 1/4 and 1/4. 25 | 26 | \begin{enumerate} 27 | \item Suppose I ask him whether he has any boys, and he says yes. What is the probability that one child is a girl? 28 | \item Suppose instead that I happen to see one of his children run by, and it is a boy. What is the probability that the other child is a girl? 29 | \end{enumerate} 30 | \item Legal reasoning. Suppose a crime has been committed. Blood is found at the scene for which there is 31 | no innocent explanation. It is of a type which is present in 1\% of the population. 32 | \begin{enumerate} 33 | \item The prosecutor claims: “There is a 1\% chance that the defendant would have the crime blood type if he were innocent. Thus there is a 99\% chance that he guilty”. This is known as the prosecutor’s fallacy. What is wrong with this argument? 34 | \item The defender claims: “The crime occurred in a city of 800,000 people. The blood type would be found in approximately 8000 people. The evidence has provided a probability of just 1 in 8000 that the defendant is guilty, and thus has no relevance.” This is known as the defender’s fallacy. What is wrong with this argument? 35 | \end{enumerate} 36 | 37 | \item \textbf{Hidden Markov models}. Harry lives a simple life. Some days he is Angry and some days he is Happy. But he hides his emotional state, and so all we can observe is whether he smiles, frowns, laughs, or yells. Harry’s best friend is utterly confused about whether Harry is actually happy or angry and decides to model his emotional state using a hidden Markov model. 38 | Let $X_d \in \{\text{Happy, Angry}\}$ denote Harry’s emotional state on day $d$, and let $Y_d\in \{ \text{smile, frown, laugh, yell}\}$ denote the observation made about Harry on day $d$. Assume that on day 1 Harry is in the Happy state, i.e. $X_1 = $Happy. Furthermore, assume that Harry transitions between states exactly once per day (staying in the same state is an option) according to the following distribution: 39 | 40 | $p(X_{d+1} = \text{Happy} | X_d = \text{Angry}) = 0.1$, 41 | 42 | $p(X_{d+1} = \text{Angry} | X_d = \text{Happy}) = 0.1$, 43 | 44 | $p(X_{d+1} = \text{Angry} | X_d =\text{Angry}) = 0.9$, and 45 | 46 | $p(X_{d+1} = \text{Happy} | X_d = \text{Happy}) = 0.9$. 47 | 48 | \noindent The observation distribution for Harry’s Happy state is given by 49 | 50 | $p(Y_d = \text{smile} | Xd = \text{Happy}) = 0.6$, 51 | 52 | $p(Y_d = \text{frown} | Xd = \text{Happy}) = 0.1$, 53 | 54 | $p(Y_d = \text{laugh} | Xd = \text{Happy}) = 0.2$, and 55 | 56 | $p(Y_d = \text{yell} | Xd = \text{Happy}) = 0.1$. 57 | 58 | \noindent The observation distribution for Harry’s Angry state is 59 | 60 | $p(Yd = \text{smile} | Xd = \text{Angry}) = 0.1$, 61 | 62 | $p(Yd =\text{frown} | Xd = \text{Angry}) = 0.6$, 63 | 64 | $p(Yd = laugh | Xd =\text{Angry})=0.1,$ and 65 | 66 | $p(Yd =\text{yell} | Xd =\text{Angry})=0.2.$ 67 | 68 | \noindent All of this is summarized in the following figure: 69 | 70 | \begin{center} 71 | \includegraphics[width=0.7\textwidth]{figure} 72 | \end{center} 73 | 74 | \begin{enumerate} 75 | \item What is $p(X_2 = \text{Happy})$? 76 | \item What is $p(Y_2 = \text{frown})$? 77 | \item What is $p(X_2 = \text{Happy} | Y2 = \text{frown})$? 78 | \item What is $p(Y_{60} = \text{yell})$? 79 | \item Assume that $Y_1 =Y_2 =Y_3 =Y_4 =Y_5 =\text{frown}$. What is the most likely sequence of the states? That is, compute the MAP assignment $$\arg \max_{x1,\ldots,x5} p(X_1 = x_1,...,X_5 =x_5 |Y_1 =Y_2 =Y_3 =Y_4 =Y_5 =\text{frown})$$ 80 | \end{enumerate} 81 | 82 | \item \textbf{Naive Bayes classifier} 83 | \begin{enumerate} 84 | \item The dataset we will be using is a subset of 2005 TREC Public Spam Corpus, containing 9000 training examples and 1000 test examples. You can download it here 85 | 86 | \url{https://courses.cs.washington.edu/courses/csep546/12sp/psetwww/3/NDA.htm} 87 | 88 | Each line in the train/test files represents a single email with the following space-delimited properties: the first is the email ID (in the form /xxx/yyy), the second is whether it is ‘\emph{spam}’ or ‘\emph{ham}’ (non-spam), and the rest are words followed by their occurrence numbers. (Note that numbers may be words, so don’t worry if a line contains multiple numbers in a row). The data has been pre-processed to remove non-word characters (e.g. ‘!’) and to select features similar to what Mehran Sahami did in his original paper \url{http://robotics.stanford.edu/users/sahami/papers-dir/spam.pdf} 89 | though with larger cut-offs since our corpus is larger. 90 | 91 | \item Using the training data, compute the prior probabilities $P(spam)$ and $P(ham)$. What 92 | is $P(spam)$? 93 | \item Determine the vocabulary and compute the conditional probabilities $P(w_i |spam)$ and $P(w_i |ham)$. 94 | 95 | In this context, it is possible that none of emails labeled as say spam contain a 96 | particular word $w_j$ . Then $P(w_j |spam) = 0$ and $P(spam) \pi_i P (w_i |spam) = 0$. To 97 | address this use a so-called $m$-estimate given by $P(w_j|spam) = \frac{n_c+mp}{n+m}$ where 98 | \begin{itemize} 99 | \item $n$ is the number of training examples which are spam 100 | \item $n_c$ is the number of examples, which are spam and which contain $w_j$ 101 | \item $p$ is prior estimate for $P(w_i |spam)$, e.g., $p = 1/|Vocabulary|$) 102 | \item $m$ is weight given to the prior, i.e., number of deemed training examples, e.g., 103 | $m = |Vocabulary|$. 104 | \end{itemize} 105 | In this context we consider each word as a training example, so $n$ is the total number of words (in either ham or spam documents) and $n_c$ is the number of times $w_j$ appeared in those documents (including multiple occurrences in the same email). 106 | 107 | What are the 5 most likely words given that a document is spam? What are the 5 most likely words given that a document is ham? 108 | 109 | \item Use these probabilities to classify the test data and report the accuracy (i.e. the percentage of correct classifications). Note that directly computing $P(spam|w_1,\ldots,w_n)$ and $P(ham|w_1,...,w_n)$ can cause numerical precision issues, since the unnormalized probabilities are very small (i.e. the numerator in Bayes’ theorem). Instead, you should compare the log-probabilities of being ham/spam. 110 | \item Vary the m parameter, using $m = |Vocabulary| \times [1, 10, 100, 1000, 10000]$ and plot the accuracies vs. $m$. What assumptions are we making when the value of $m$ is very large vs. very small? How does this affect the test accuracy? 111 | \item If you were a spammer, how would you modify your emails to beat the classifiers we have learned above? 112 | 113 | With your answers, please include your Python code and a report containing: 114 | \begin{itemize} 115 | \item A high-level description on how your code works. 116 | \item The accuracies you obtain. 117 | \item If all your accuracies are low, tell us what you have tried to improve and what you suspect is failing. 118 | \end{itemize} 119 | 120 | \end{enumerate} 121 | \end{enumerate} 122 | \end{document} -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Inference and Representation Fall 2019 2 | 3 | ## Course staff 4 | 5 | | Soledad Villar | soledad.villar@nyu.edu | Instructor | 6 | |----------------|------------------------|------------| 7 | | Zhengdao Chen | zc1216@nyu.edu | TA | 8 | | Efe Onaran | eonaran@nyu.edu | Grader | 9 | 10 | ## Syllabus 11 | 12 | This is a graduate level course that presents fundamental tools of statistical inference, probabilistic graphical models and generative models for machine learning. 13 | 14 | Some of the covered topics include latent graphical models (Latent Dirichlet Allocation, Gaussian Processes), state-space models (Kalman Filter, Hidden Markov Models), Gibbs Models and Deep generative models (Variational autoencoders, GANs). 15 | 16 | ## Lecture (required) 17 | Tuesdays 4:55pm-6:35pm, in 60 5th Ave, FA 110. 18 | 19 | ## Recitation (required) 20 | Mondays 4:55pm-5:45pm, in 60 5th Ave, FA 110. 21 | 22 | ## Office hours 23 | SV: Tuesdays, 3:00pm-4:45pm. Location: 60 5th ave, 6th floor, room 617. 24 | 25 | ## Grading 26 | Homework 40%, midterm exam 25%, final project 30%, participation 5%. 27 | 28 | ## Materials 29 | There is no required book. Assigned readings will come from freely-available online material. 30 | 31 | ### Core Materials 32 | - David MacKay, [Information Theory, Inference and Algorithms](https://www.inference.org.uk/itprnn/book.pdf), Cambridge Press, 2003. 33 | - Kevin Murphy, [Machine Learning: a Probabilistic Perspective](http://www.cs.ubc.ca/%7Emurphyk/MLbook/index.html), MIT Press, 2012. You can read this online for free from [NYU Libraries](http://site.ebrary.com/lib/nyulibrary/detail.action?docID=10597102). We recommend the latest (4th) printing, as earlier editions had many typos. You can tell which printing you have as follows: check the inside cover, below the "Library of Congress" information. If it says "10 9 8 ... 4" you've got the (correct) fourth print. 34 | - Daphne Koller and Nir Friedman, [Probabilistic Graphical Models: Principles and Techniques](http://pgm.stanford.edu/), MIT Press, 2009. 35 | - Mike Jordan's notes on [Probabilistic Graphical Models](https://people.eecs.berkeley.edu/~jordan/prelims/) 36 | - [MIT lecture notes](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-438-algorithms-for-inference-fall-2014/lecture-notes/) on algorithms for inference. 37 | - [Probabilistic Programming and Bayesian Methods for Hackers](https://camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/) by Cam Davidson Pilon 38 | - Trevor Hastie, Rob Tibshirani, and Jerry Friedman, [Elements of Statistical Learning](http://statweb.stanford.edu/~tibs/ElemStatLearn/), Second Edition, Springer, 2009. (Can be downloaded as PDF file.)6 39 | - David Barber, [Bayesian Reasoning and Machine Learning](http://web4.cs.ucl.ac.uk/staff/D.Barber/pmwiki/pmwiki.php?n=Brml.Online) , Cambridge University Press, 2012. (Can be downloaded as PDF file.) 40 | 41 | ### Background on Probability and Optimization 42 | - [Review notes from Stanford's machine learning class](http://cs229.stanford.edu/section/cs229-prob.pdf) 43 | - Sam Roweis's [probability review](http://cs.nyu.edu/%7Edsontag/courses/ml12/notes/probx.pdf) 44 | - [Convex Optimization](http://www.stanford.edu/%7Eboyd/cvxbook/) by Stephen Boyd and Lieven Vandenberghe. 45 | - [Carlos Ferndandez's notes on Statistics and Probability for Data Science DS-GA 1002](http://www.cims.nyu.edu/~cfgranda/pages/stuff/probability_stats_for_DS.pdf) 46 | 47 | ### Further Reading 48 | - Mike Jordan and Martin Wainwright, [Graphical Models, Exponential Families, and Variational Inference](https://people.eecs.berkeley.edu/~wainwrig/Papers/WaiJor08_FTML.pdf) 49 | 50 | ### Additional Readings for the Recitation 51 | - Christopher Bishop, [Pattern Recognition and Machine Learning](http://users.isr.ist.utl.pt/~wurmd/Livros/school/Bishop%20-%20Pattern%20Recognition%20And%20Machine%20Learning%20-%20Springer%20%202006.pdf) (PRML) 52 | - Miranda Holmes-Cerfon, [Lecture notes on Markov Chains](https://cims.nyu.edu/~holmes/teaching/asa19/handout_Lecture2_2019.pdf) 53 | - Daniel Jurafsky and James Martin, [Book chapter on hidden Markov models](https://web.stanford.edu/~jurafsky/slp3/A.pdf) 54 | - Bill Freeman and Antonio Torralba, [Lecture notes on belief propagation](http://6.869.csail.mit.edu/fa13/lectures/slideNotesCh7rev.pdf) 55 | - Zoubin Ghahramani, [Slide on belief propagation](http://mlg.eng.cam.ac.uk/mlss09/mlss_slides/Ghahramani_2.pdf) 56 | - Miranda Holmes-Cerfon, [Lecture notes on MCMC](https://cims.nyu.edu/~holmes/teaching/asa19/handout_Lecture3_2019.pdf) 57 | - Michael I. Jordan, [Notes on EM](http://www.robots.ox.ac.uk/~cvrg/trinity2005/jordan_bishop.ps) 58 | 59 | 60 | ## Important dates 61 | - October 15. No class and no office hours (legislative Monday). 62 | - October 29. Midterm 63 | - December 3, 4, 6. Final project presentations (see schedule). 64 | - December 12. Final project due. 65 | 66 | 67 | 68 | | Date | Topic | References | Homework | 69 | |----------|-------------------------------------------------------------------------------------------|-------------------------------------------------------------------|----------| 70 | | Sept 3rd | Introduction to inference and graphical models. Priors, likelihood functions, posteriors. | MacKay chapters 2 and 21. Section 2.1 of Jordan and Wainwright. [Example seen in class](./materials/example_posterior_computation.ipynb). | HW 1 due 9/16. 71 | | Sept 9th (recitation) | Basics of probability; data fitting and maximum likelihood inference. | PRML chapter 1. 72 | |Sept 10th | Bayesian networks, naive bayes, hidden markov models | Murphy chapters 10, 17, 3. 73 | | Sept 16th (recitation)| Markov chains and PageRank. | Murphy sections 17.2, 17.4; Lecture notes on Markov chains 74 | | Sept 17th | Bayesian networks (cont.) Bayes Ball algorithm, Undirected graphical models | Murphy chapter 10 and sections 19.1-19.4 | HW2 due 10/2 | 75 | | Sept 23rd (recitation) | Hidden Markov models, Viterbi algorithm | [Book chapter on HMMs](https://web.stanford.edu/~jurafsky/slp3/A.pdf) | | 76 | | Sept 24th | EM for mixtures of Gaussians and training HMMs | Bishop chapter 9 and https://web.stanford.edu/~jurafsky/slp3/A.pdf | | 77 | | Sept 30th (recitation) | Baum-Welch algorithm (EM algorithm) for HMMs | [Book chapter on HMMs](https://web.stanford.edu/~jurafsky/slp3/A.pdf) | | 78 | | Oct 1st | Belief propagation and stochastic block model | Murphy chapter 20 and https://arxiv.org/pdf/1702.00467.pdf . See Zhengdao's paper about [community detection using GNNs](https://openreview.net/pdf?id=H1g0Z3A9Fm) . | | 79 | | Oct 7th (recitation) | Belief propagation | [Lecture notes on BP](http://6.869.csail.mit.edu/fa13/lectures/slideNotesCh7rev.pdf) and [slides on BP](http://mlg.eng.cam.ac.uk/mlss09/mlss_slides/Ghahramani_2.pdf)| | 80 | | Oct 8th | Introduction to error correcting codes. Introduction to sampling methods | Chapters 1 and 47 of MacKay. Bishop chapter 11 | HW 3 due October 22 (noon) | 81 | | Oct 21st (recitation)| Markov Chain Monte Carlo (MCMC) | [Lecture notes on MCMC](https://cims.nyu.edu/~holmes/teaching/asa19/handout_Lecture3_2019.pdf) | | 82 | | Nov 4th (recitation)| MCMC cont'd; General EM algorithm | [Notes on EM](http://www.robots.ox.ac.uk/~cvrg/trinity2005/jordan_bishop.ps) | | 83 | | Nov 5th | MCMC techniques for detecting Gerrymandering, Variational Autoencoders | [Gerrymandering](https://sites.duke.edu/quantifyinggerrymandering/) and papers [1](https://arxiv.org/pdf/1801.03783.pdf), [2](https://www.pnas.org/content/114/11/2860). VAEs [tutorial](https://arxiv.org/pdf/1606.05908.pdf) and [code example](https://github.com/nitarshan/variational-autoencoder) | Project proposal due 11/13| 84 | | Nov 11th (recitation)| EM cont'd; basics of neural networks | || 85 | | Nov 12th | Variational inference | https://arxiv.org/abs/1601.00670|| 86 | | Nov 18th | GANs |https://papers.nips.cc/paper/5423-generative-adversarial-nets.pdf || 87 | | Nov 19th | Wasserstein GAN, GLO | https://arxiv.org/pdf/1701.07875.pdf and https://arxiv.org/pdf/1707.05776.pdf|| 88 | | Nov 25th (recitation) | Community detection and GNN | [Community detection](https://arxiv.org/abs/1702.00467), [GCN (Kipf & Welling)](https://arxiv.org/abs/1609.02907), [MPNN (Gilmer et al.)](https://arxiv.org/abs/1704.01212) || 89 | | Nov 26th | Gaussian processes | Chapter 2 of http://www.gaussianprocess.org/gpml/chapters/ || 90 | | Dec 2nd (recitation) | Representation learning with autoencoders and predictive coding | [Autoencoders](https://www.deeplearningbook.org/contents/autoencoders.html), [Contrastive Predictive Coding](https://arxiv.org/pdf/1807.03748.pdf), [Noise Contrastive Estimation](http://demo.clab.cs.cmu.edu/cdyer/nce_notes.pdf) | 91 | 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /hw/source/homework3.tex: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | 3 | \usepackage{graphicx} 4 | \usepackage{amsfonts,amsmath,amssymb,amsthm} 5 | \usepackage{url} 6 | \usepackage[usenames]{color} 7 | 8 | 9 | 10 | 11 | 12 | 13 | \newcommand{\G}{\mathcal{G}} 14 | 15 | 16 | \newcommand{\ww}{{\bf w}} 17 | \newcommand{\xx}{{\bf x}} 18 | \newcommand{\yy}{{\bf y}} 19 | \newcommand{\real}{\ensuremath{\mathbb{R}}} 20 | \date{} 21 | \title{Homework 3} 22 | \author{(Inference and Representation) 23 | \\ Due October 22 on NYU Classes. } 24 | \usepackage{fullpage} 25 | \begin{document} 26 | 27 | \maketitle 28 | 29 | \begin{enumerate} 30 | 31 | 32 | \item {\bf Sum-product algorithm. From Brown University CS 242.} 33 | 34 | We implement the sum-product variant of the belief propagation algorithm to compute marginals. To understand the details of the sum-product algorithm, we recommend Chapter 5 of Barber's ``Bayesian Reasoning and Machine Learning", as well as ``Factor Graphs and the Sum-Product Algorithm" by Kschischang, Frey, \& Loeliger, IEEE Trans. Information Theory 47, pp. 498-519, 2001. 35 | 36 | You must write your own implementation of the sum-product algorithm in Python. 37 | 38 | We provided code implementing a data structure to store the graph adjacency structure, and numeric potential tables, defining any discrete factor graph. We also provided code that explicitly builds a table containing the probabilities of all joint configurations of the variables in a factor graph, and sums these probabilities to compute the marginal 39 | distribution of each variable. Such ``brute force" inference code is of course inefficient, and will only be computationally tractable for small models. 40 | 41 | We recommend (but do not require) that you use these same data structures for your own implementation of the sum-product algorithm, by implementing \path{get_beliefs}. To gain intuition for the graph structure, examine the output of \path{make_debug_graph.ipynb}. Think of the code we provide as a starting point: you are welcome to create additional functions or data structures as needed. 42 | 43 | \begin{enumerate} 44 | \item Implement the sum-product algorithm. Your code should support an arbitrary factor graph linking a collection of discrete random variables. Use a parallel message update schedule, 45 | in which all factor-to-variable messages are updated given the current variable-to-factor messages, and then all variable-to-factor messages given the current factor-to-variable 46 | messages. Initialize by setting the variable-to-factor messages to equal 1 for all states. 47 | Be careful to normalize messages to avoid numerical underflow. 48 | 49 | 50 | \item Consider the four-node, tree-structured factor graph illustrated in Figure \ref{fig:tree} with binary variables. Numeric values for the potential functions are defined in \path{make_debug_graph.ipynb}. Run your implementation of the sum-product algorithm on this graph, and report the marginal distributions it computes. 51 | \begin{figure}[h] 52 | \centering 53 | \includegraphics[width=2in]{brown_hw1} 54 | \caption{A tree-structured factor graph in which four factors link four binary random variable.} 55 | \label{fig:tree} 56 | \end{figure} 57 | \end {enumerate} 58 | 59 | 60 | \item {\bf LDPC, (appeared in Joan Bruna's homework set).} 61 | 62 | We begin by designing algorithms for reliable communication in the presence of noise. We 63 | focus on error correcting codes based on highly sparse, low density parity check (LDPC) matrices, and use the sum-product variant of the loopy belief propagation (BP) algorithm 64 | to estimate partially corrupted message bits. For background information on LDPC codes, see Chap. 47 of MacKay's Information Theory, Inference, and Learning Algorithms, which 65 | is freely available online: \url{http://www.inference.phy.cam.ac.uk/mackay/itila/}. 66 | 67 | We consider rate 1/2 error correcting codes, which encode N message bits using a 2N-bit codeword. LDPC codes are specified by a $N \times 2N$ binary parity check matrix H, 68 | whose columns correspond to codeword bits, and rows to parity check constraints. We define $H_{ij} = 1$ if parity check $i$ depends on codeword bit $j$, and $H_{ij} = 0$ otherwise. Valid codewords are those for which the sum of the bits connected to each parity check, as indicated by H, equals zero in modulo-2 addition (i.e., the number of "active" bits must be even). Equivalently, the modulo-2 product of the parity check matrix with the 2N-bit codeword vector must equal a N-bit vector of zeros. As illustrated in Figure \ref{fig:factor}, we can visualize these 69 | parity check constraints via a corresponding factor graph. The parity check matrix H can then be thought of as an adjacency matrix, where rows correspond to factor (parity) nodes, 70 | columns to variable (codeword bit) nodes, and ones to edges linking actors to variables. 71 | 72 | \begin{figure}[t] 73 | \centering 74 | \includegraphics[width=2in]{brown_hw2} 75 | \caption{A factor graph representation of a LDPC code linking four factor (parity constraint) 76 | nodes to eight variable (message bit) nodes. The unary factors encode noisy observations of the 77 | message bits from the output of some communications channel.} 78 | \label{fig:factor} 79 | \end{figure} 80 | 81 | \begin{enumerate} 82 | \item Implement code that, given an arbitrary parity check matrix H, constructs a corresponding factor graph. The parity check factors should evaluate to 1 if an even number of adjacent bits are active (equal 1), and 0 otherwise. Your factor graph representation should interface with your implementation of the sum-product algorithm from the previous problem. Define a small test case, and verify that your graphical model assigns zero probability to invalid codewords. 83 | \item Load the $N = 128$-bit LDPC code using the Python library \path{pyldpc} (for a tutorial see \url {github.com/hichamjanati/pyldpc-tutos}). To evaluate decoding 84 | performance, we assume that the all-zeros codeword is sent, which always satisfies any set of parity checks. Using the random module, simulate the output of a binary symmetric 85 | channel: each transmitted bit is flipped to its complement with error probability $\epsilon =0.05$, and equal to the transmitted bit otherwise. Define unary factors for each variable node 86 | which equal $1-\epsilon$ if that bit equals the "received" bit at the channel output, and $\epsilon$ otherwise. Run the sum-product algorithm for 50 iterations of a parallel message update schedule, initializing by setting all variable-to-factor messages to be constant. After the final iteration, plot the estimated posterior probability that each codeword bit equals one. 87 | If we decode by setting each bit to the maximum of its corresponding marginal, would we find the right codeword? 88 | \item Repeat the experiment from part (b) for 10 random channel noise realizations with error probability $\epsilon= 0.06$. For each trial, run sum-product for 50 iterations. After each iteration, estimate the codeword by taking the maximum of each bit's marginal distribution, and evaluate the Hamming distance (number of differing bits) between the estimated and true (all-zeros) codeword. On a single plot, display 10 curves showing Hamming distance versus iteration for each random trial. Is BP a reliable decoding algorithm? 89 | \item Repeat part (c) with two higher error probabilities, $\epsilon = 0.08$ and $\epsilon = 0.10$. Discuss any qualitative differences in the behavior of the loopy BP decoder. 90 | \item For the LDPC codes we consider, we also define a corresponding $2N\times N$ generator matrix G. To encode an N-bit message vector we would like to transmit, we take the modulo-2 matrix product of the generator matrix with the message. The generator matrix has been constructed (via linear algebra over the finite field GF(2)) such that this product always produces a valid 2N-bit codeword. Geometrically, its columns are chosen to span the null space of H. We use a systematic encoding, in which the first N codeword bits are simply copies of the message bits. The problems below use precomputed (G;H) pairs using the relevant functions in \path{pyldpc}. Generate the $N = 1600$-bit LDPC code. Using this, we will replicate the visual decoding demonstration from MacKay's Fig. 47.5. Start by converting a $40\times 40$ binary image to a 1600-bit message vector. Encode the message using the generator matrix $G$, 91 | and add noise with error probability $\epsilon = 0.06$. For this input, plot images showing the output of the sum-product decoder after 0, 1, 2, 3, 5, 10, 20, and 30 iterations. The \% 92 | operator may be useful for computing modulo-2 sums. You can use the numpy reshape function to easily convert between images and rasterized message vectors. 93 | \item Repeat the previous part with a higher error probability of $\epsilon =0.10$, and discuss differences. 94 | \end{enumerate} 95 | 96 | \section*{Optional problems} 97 | 98 | \item {\bf HMM with mixture model emissions, (appeared in Joan Bruna's homework set). \newline [10 points of extra credit]} 99 | % From Berkeley, A_hw5_Fa11.tex 100 | %\begin{center}{\bf \large HMM with mixture model emissions}\end{center} 101 | 102 | A common modification of the hidden Markov model involves using mixture models for the 103 | emission probabilities $p(\yy_t | q_t)$, where $q_t$ refers to the state for time $t$ and $\yy_t$ to the observation for time $t$. 104 | 105 | Suppose that ${\bf y}_t \in \real^n$ and that the emission distribution is given by a mixture of Gaussians for each value of the state. To be concrete, suppose that the $q_t$ can take $K$ discrete states and each mixture has $M$ components. Then, 106 | \[ 107 | p({\bf y}_t \mid q_t) = \sum_{j=1}^M b_{q_t j} 108 | \left(\frac{1}{(2\pi)^{\frac{n}{2}}|\Sigma_{q_t j}|^{\frac{1}{2}}} 109 | \exp\left\{-\frac{1}{2}({\bf y}_t-\mu_{q_t j})^T\Sigma_{q_t j}^{-1}({\bf y}_t-\mu_{q_t j}) 110 | \right\}\right) 111 | \] 112 | where ${\bf b}_i \in [0,1]^M$ denotes the mixing weights for state $i$ ($\sum_{j=1}^M b_{ij} = 1$ for $i=1,\ldots K$), $\mu_{ij} \in \real^n$ and $\Sigma_{ij} \in \real^{n\times n}$. 113 | 114 | Let $\pi \in \real^K$ be the probability distribution for the initial state $q_0$, 115 | and $A \in \real^{K \times K}$ be the transition matrix of the $q_t$'s. 116 | In this problem you will derive an EM algorithm for learning the parameters $\{ b_{ij}, \mu_{ij}, \Sigma_{ij} \}$ and $A, \pi$. 117 | 118 | \begin{enumerate} 119 | \item The EM algorithm is substantially simpler if you introduce auxiliary variables $z_t\in \{1, \ldots, M\}$ denoting which mixture component the $t$'th observation is drawn from. 120 | 121 | Draw the graphical model for this modified HMM, identifying 122 | clearly the additional latent variables that are needed. 123 | \item Write the expected complete log likelihood for the model 124 | and identify the expectations that you need to compute in the E 125 | step. {\em Show all steps of your derivation.} 126 | 127 | \item Give an algorithm for computing the E step. 128 | 129 | {\em Hint: Reduce the inference problem to something you know how to do, such as sum-product belief propagation in tree-structured pairwise MRFs.} 130 | 131 | \item Write down the equations that implement the M step. 132 | \end{enumerate} 133 | 134 | 135 | 136 | 137 | 138 | 139 | \item {\bf Message passing on a tree. (From Murphy chapter 20)\newline [10 points of extra credit]} 140 | 141 | Consider the DGM below which represents the following fictitious biological model. Each $G_i$ represents the genotype of a person: $G_i = 1$ if they have a healthy gene and $G_i = 2$ if they have an unhealthy gene. $G_2$ and $G_3$ may inherit the unhealthy gene from their parent $G_1$. $X_i\in \mathbb R$ is a continuous measure of blood pressure, which is low if you are healthy and high if you are unhealthy. We define the CPDs as follows 142 | 143 | \begin{minipage}{0.6\textwidth} 144 | \vspace{-30pt} 145 | \begin{eqnarray*} 146 | p(G_1) &=& [0.5, 0.5] \\ 147 | p(G_2|G_1) &=& \left( \begin{matrix} 0.9 & 0.1 \\ 0.1 & 0.9 \end{matrix} \right) \\ 148 | p(G_3|G_1) &=& \left( \begin{matrix} 0.9 & 0.1 \\ 0.1 & 0.9 \end{matrix} \right) \\ 149 | p(X_i | G_i =1) &=& \mathcal N (X_i | \mu=50, \sigma^2 =10) \\ 150 | p(X_i | G_i =2) &=& \mathcal N (X_i | \mu=60, \sigma^2 =10) 151 | \end{eqnarray*} 152 | \end{minipage} 153 | \begin{minipage}{0.7\textwidth} 154 | \includegraphics[width=0.22\textwidth]{fig_hw3} 155 | \end{minipage} 156 | 157 | \begin{enumerate} 158 | \item Suppose you observe $X_2=50$ and $X_1$ is unobserved. What is the posterior belief on $G_1$ (i.e. $p(G_1|X_2=50)$?) 159 | \item Now suppose you observe $X_2=50$ and $X_3=50$. What is $p(G_1| X_2, X_3)$? Explain your answer intuitively. 160 | \item Now suppose $X_2=60$ and $X_3=60$. What is $p(G_1| X_2, X_3)$? Explain your answer intuitively. 161 | \item Now suppose $X_2=50$ and $X_3=60$. What is $p(G_1| X_2, X_3)$? Explain your answer intuitively. 162 | \end{enumerate} 163 | 164 | \end{enumerate} 165 | 166 | \end{document} 167 | -------------------------------------------------------------------------------- /hw/sequence.txt: -------------------------------------------------------------------------------- 1 | [4, 1, 4, 4, 2, 1, 6, 4, 4, 2, 4, 5, 1, 1, 2, 5, 1, 1, 1, 6, 1, 4, 5, 1, 6, 1, 3, 3, 6, 4, 6, 1, 1, 6, 6, 1, 5, 5, 5, 3, 2, 1, 3, 1, 6, 4, 5, 1, 5, 5, 3, 6, 2, 4, 6, 4, 5, 1, 1, 6, 4, 5, 6, 6, 3, 1, 5, 6, 4, 2, 4, 1, 1, 6, 6, 2, 5, 3, 6, 6, 6, 4, 6, 5, 1, 1, 2, 6, 4, 1, 4, 1, 1, 3, 2, 4, 5, 6, 5, 4, 6, 1, 4, 5, 5, 1, 4, 1, 2, 5, 4, 1, 6, 5, 6, 2, 2, 1, 6, 4, 6, 3, 2, 6, 4, 3, 6, 1, 4, 5, 6, 2, 4, 2, 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