{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Yee Whye Teh et al](http://www.cs.berkeley.edu/~jordan/papers/hdp.pdf)'s 2005 paper _Hierarchical Dirichlet Processes_ describes a nonparametric prior for grouped clustering problems. For example, the HDP helps in generalizing the [latent Dirichlet allocation](https://en.wikipedia.org/wiki/Latent_Dirichlet_allocation) model to the case the number of topics in the data are discovered by the inference algorithm instead of being specified as a parameter of the model.\n",
"\n",
"The authors describe three MCMC-based algorithms for fitting HDP based models. Unfortunately, the algorithms are described somewhat vaguely and in general terms. A fair bit of mathematical leg work is required before the HDP algorithms can be applied to the specific case of nonparametric latent Dirichlet allocation.\n",
"\n",
"Here are some notes I've compiled in my effort to understand these algorithms.\n",
"\n",
"## HDP-LDA Generative Model\n",
"\n",
"The generative model for Hierarchical Dirichlet Process Latent Dirichlet Allocation is as follows:\n",
"\n",
"\n",
"\\begin{equation}\n",
"\\begin{aligned}\n",
" H & \\sim \\text{Dirichlet}(\\beta) \\\\\n",
" G_0 \\,|\\, \\gamma, H & \\sim \\text{DP}(\\gamma, H) \\\\\n",
" G_j \\,|\\, \\alpha_0, G_0 & \\sim \\text{DP}(\\alpha_0, G_0) \\\\\n",
" \\theta_{ji} \\,|\\, G_j & \\sim G_j \\\\\n",
" x_{ij} \\,|\\, \\theta_{ji} & \\sim \\text{Categorical}(\\theta_{ji}) \\\\\n",
"\\end{aligned}\n",
"\\end{equation}\n",
"\n",
"* $H$ is Dirichlet distribution whose dimension is the size of the vocabulary, i.e. it is distribution over an uncountably-number of term distributions (topics).\n",
"* $G_0$ is a distribution over a countably-infinite number of categorical term distributions, i.e. topics.\n",
"* For each document $j$, $G_j$ is a is a distribution over a countably-infinite number of categorical term distributions, i.e. topics.\n",
"* $\\theta_{ji}$ is a categorical distribution over terms, i.e. a topic.\n",
"* $x_{ij}$ is a term.\n",
"\n",
"To see code for sampling from this generative model, see [this post](/nonparametric-lda/).\n",
"\n",
"### Chinese Restaurant Franchise Approach\n",
"\n",
"Instead of the above Dirichlet process model, we can think of an identical \"Chinese Restaurant Franchise\" model.\n",
"\n",
"Each $\\theta_{ji}$ is a customer in restaurant $j$. Each customer is sitting at a table, and each table has multiple customers.\n",
"\n",
"There is a global menu of $K$ dishes that the restaurants serve, $\\phi_1,\\ldots,\\phi_K$.\n",
"\n",
"Some other definitions:\n",
"\n",
"* $\\psi_{jt}$ is the dish served at table $t$ in restaurant $j$; i.e. each $\\psi_{jt}$ corresponds to some $\\phi_k$.\n",
"* $t_{ji}$ is the index of the $\\psi_{jt}$ associated with $\\theta_{ji}$.\n",
"* $k_{jt}$ is the index of $\\phi_k$ associated with $\\psi_{jt}$.\n",
"\n",
"*Customer $i$ in restaurant $j$ sits at table $t_{ji}$ while table $t$ in restaurant $j$ serves dish $k_{jt}$.*\n",
"\n",
"There are two arrays of count variables we will want to track:\n",
"\n",
"* $n_{jtk}$ is the number of customers in restaurant $j$ at table $t$ eating dish $k$.\n",
"* $m_{jk}$ is the number of tables in restaurant $j$ serving dish $k$ (multiple tables may serve the same dish).\n",
"\n",
"To summarize:\n",
"\n",
"$x_{ij}$ are observed data (words). We assume $x_{ij}\\sim F(\\theta_{ij})$. Further, we assume $\\theta_{ji}$ is associated with table $t_{ji}$, that is $\\theta_{ji}=\\psi_{jt_{ji}}$. Further, we assume the topic for table $j$ is indexed by $k_{jt}$, i.e. $\\psi_{jt}=\\phi_{k_{jt}}$. Thus, if we know $t_{ji}$ (the table assignment for $x_{ij}$) and $k_{jt}$ (the dish assignment for table $t$) for all $i, j, t$, we could determine the remaining parameters by sampling.\n",
"\n",
"## Gibbs Sampling\n",
"\n",
"[Teh et al](http://www.cs.berkeley.edu/~jordan/papers/hdp.pdf) describe three Gibbs samplers for this model. The first and third are most applicable to the LDA application. The section helps with more complication applications of the LDA algorithm (e.g. the hidden Markov model).\n",
"\n",
"### 5.3 Posterior sampling by direct assignment\n",
"\n",
"Section 5.3 describes a direct assignment Gibbs sampler that directly assigns words to topics by augmenting the model with an assignment variable $z_{ji}$ that is equivalent to $k_{jt_{ji}}$. This also requires a count variable $m_{jk}$: the number of tables in document/franchise $j$ assigned to dish/topic $k$. This sampler requires less \"bookkeeping\" than the algorithm from 5.1, however it requires expensive simulation or computation of recursively computed Stirling numbers of the first kind.\n",
"\n",
"My notes below are derive the necessary equations for Gibbs sampling for the algorithm in section 5.1. However, they also provide most of the derivations needed for 5.3.\n",
"\n",
"### 5.1 Posterior sampling in the Chinese restaurant franchise\n",
"\n",
"Section 5.1 describes \"Posterior sampling in the Chinese restaurant franchise\". Given observed data $\\mathbf{x}$ (i.e. documents), we sample over the index variables $t_{ji}$ (associating tables with customers/words) and $k_{jt}$ (associating tables with dishes/topics). Given these variables, we can reconstruct the distribution over topics for each document and distribution over words for each topic.\n",
"\n",
"#### Notes on Implementing Algorithm 5.1\n",
"\n",
"Teh et al's original HDP paper is sparse on details with regard to applying these samplers to the specific case of nonparametric LDA. For example, both samplers require computing the conditional distribution of word $x_{ji}$ under topic $k$ given all data items except $x_{ji}$: $f_k^{x_{ji}}(x_{ji})$) (eq. 30).\n",
"\n",
"[A Blogger Formerly Known As Shuyo](https://shuyo.wordpress.com/2012/08/15/hdp-lda-updates/) has a brief post where he states (with little-to-no derivation) the equations specific to the LDA case. Below I attempt provide some of those derivations in pedantic detail.\n",
"\n",
"As stated above, in the case of topic modeling, $H$ is a Dirichlet distribution over terms distributions and $F$ is a multinomial distribution over terms.\n",
"\n",
"By definition,\n",
"\n",
"\\begin{equation} h(\\phi_k)=\\frac{1}{Z}\\prod_v[\\phi_k]_v^{\\beta-1} \\end{equation}\n",
"\n",
"and\n",
"\n",
"\\begin{equation}\n",
" f(x_{ji}=v \\,|\\, \\phi_k)=\\phi_{kv}.\n",
"\\end{equation}\n",
"\n",
"\n",
"##### Equation (30): $f_k^{x_{ji}}(x_{ji})$\n",
"\n",
"For convenience, define $v=x_{ji}$ (word $i$ in document $j$), define $k=k_{jt_{ji}}$ (topic assignment for the table in document $j$ containing word $i$), and\n",
"\n",
"\\begin{equation}\n",
" n_{kv}^{-ji}=\\left|\\left\\{\n",
" x_{mn} \\,|\\, k_{mt_{mn}}=k,\\, x_{mn}=v,\\, (m,n)\\neq(j,i)\n",
" \\right\\}\\right|\n",
"\\end{equation}\n",
"\n",
"(the number of times the term $x_{ji}$, besides $x_{ji}$ itself, is generated by the same topic as was $x_{ji}$).\n",
"\n",
"First look at the term (for fixed $k$):\n",
"\n",
"\\begin{equation}\n",
" \\prod_{j'i'\\neq ji, z_{j'i'=k}}\n",
" f(x_{j'i'} \\,|\\, \\phi_k)=\n",
" \\prod_{j'}\n",
" \\prod_{i'\\neq i, z_{j'i'}=k}\n",
" [\\phi_{k}]_{x_{j'i'}}\n",
"\\end{equation}\n",
"\n",
"\n",
"\n",
"$[\\phi_k]_v$ is the probability that term $v$ is generated by topic $k$. The double sums run over every word generated by topic $k$ in every document. Since $[\\phi_{k}]_{x_{j'i'}}$ is fixed for a given word $w$, we could instead do a product over the each word of the vocabulary:\n",
"\n",
"\n",
"\\begin{equation}\n",
"\\prod_{j'i'\\neq ji, z_{j'i'=k}}\n",
" f(x_{j'i'} \\,|\\, \\phi_k)\n",
" =\\prod_{w\\in\\mathcal{V}}[\\phi_k]_w^{n_{kw}^{-ji}}\n",
"\\end{equation}\n",
"\n",
"\n",
"We use this early on in the big derivation below.\n",
"\n",
"Also, note that\n",
"\n",
"\\begin{equation}\n",
" \\int\n",
" \\phi_{kv}^{n_{kv}^{-ji}+\\beta}\n",
" \\prod_{w\\neq v}\n",
" \\phi_{kw}^{n_{kw}^{-ji}+\\beta-1}\n",
" d\\phi_k \\text{ and }\n",
" \\int\n",
" \\prod_w\n",
" \\phi_{kw}^{n_{kw}^{-ji}+\\beta-1}\n",
" d\\phi_k\n",
"\\end{equation}\n",
"\n",
"are the normalizing coefficients for Dirichlet distributions.\n",
"\n",
"Equation (30) in Teh's paper is:\n",
"\n",
"\\begin{equation}\n",
"\\begin{aligned}\n",
" f_k^{-x_{ji}}(x_{ji})\n",
" &=\\frac{\n",
" \\int\n",
" f(x_{ji} \\,|\\, \\phi_k)\n",
" \\left[\n",
" \\prod_{j'i'\\neq ji, z_{j'i'}=k}\n",
" f(x_{j'i'} \\,|\\, \\phi_k)\n",
" \\right]\n",
" h(\\phi_k)\n",
" d\\phi_k\n",
" }\n",
" {\n",
" \\int\n",
" \\left[\n",
" \\prod_{j'i'\\neq ji, z_{j'i'}=k}\n",
" f(x_{j'i'}|\\phi_k)\n",
" \\right]\n",
" h(\\phi_k)d\\phi_k\n",
" } \\\\\n",
" &=\\frac{\n",
" \\int\n",
" f(x_{ji} \\,|\\, \\phi_k)\n",
" \\left[\n",
" \\prod_{j'}\n",
" \\prod_{i'\\neq i, z_{j'i'}=k}\n",
" \\phi_{kv}\n",
" \\right]\\cdot\n",
" h(\\phi_k)\n",
" d\\phi_k\n",
" }\n",
" {\n",
" \\int\n",
" \\left[\n",
" \\prod_{j'i'\\neq ji, z_{j'i'}=k}\n",
" f(x_{j'i'}|\\phi_k)\n",
" \\right]h(\\phi_k)d\\phi_k\n",
" } \\\\\n",
" &\\propto\\frac{\n",
" \\int\n",
" \\phi_{kv}\n",
" \\prod_w\n",
" \\phi_{kw}^{n_{kw}^{-ji}}\n",
" \\prod_{w}\n",
" \\phi_{kw}^{\\beta-1}\n",
" d\\phi_k\n",
" }{\n",
" \\int\n",
" \\prod_w\n",
" \\phi_{kw}^{n_{kw}^{-ji}}\n",
" \\prod_w\n",
" \\phi_{kw}^{\\beta-1}\n",
" d\\phi_k\n",
" }\\\\\n",
" &=\\frac{\n",
" \\int\n",
" \\phi_{kv}\\cdot\n",
" \\phi_{kv}^{n_{kv}^{-ji}}\\cdot\n",
" \\prod_{w\\neq v}\n",
" \\phi_{kw}^{n_{kw}^{-ji}}\\cdot\n",
" \\phi_{kv}^{\\beta-1}\\cdot\n",
" \\prod_{w\\neq v}\n",
" \\phi_{kw}^{\\beta-1}\n",
" d\\phi_k\n",
" }{\n",
" \\int\n",
" \\prod_w\n",
" \\phi_{kw}^{n_{kw}^{-ji}}\n",
" \\prod_w\n",
" \\phi_{kw}^{\\beta-1}\n",
" d\\phi_k\n",
" }\\\\\n",
" &=\n",
" \\int\n",
" \\phi_{kv}^{n_{kv}^{-ji}+\\beta}\n",
" \\prod_{w\\neq v}\n",
" \\phi_{kw}^{n_{kw}^{-ji}+\\beta-1}\n",
" d\\phi_k\n",
" \\cdot\n",
" \\frac{\n",
" 1\n",
" }{\n",
" \\int\n",
" \\prod_w\n",
" \\phi_{kw}^{n_{kw}^{-ji}+\\beta-1}\n",
" d\\phi_k.\n",
" }\n",
"\\end{aligned}\n",
"\\end{equation}\n",
"\n",
"Recognizing these integrals as those that occur in the Dirichlet distribution, we have,\n",
"\n",
"\\begin{equation}\n",
"\\begin{aligned}\n",
" f_k^{-x_{ji}}(x_{ji})\n",
" &=\\frac{\n",
" \\Gamma\\left(\\beta+n_{kv}^{-ji}+1\\right)\n",
" \\prod_{w\\neq v} \\Gamma\\left(\\beta+n_{kw}^{-ji}\\right)\n",
" }{\n",
" \\Gamma\\left(\n",
" \\sum_{w\\neq v}\n",
" \\left[\n",
" \\beta+n_{kw}^{-ji}\n",
" \\right]+\n",
" (\\beta+n_{kv}^{-ji}+1)\\right)\n",
" }\\cdot\n",
" \\frac{\n",
" 1\n",
" }{\n",
" \\int\\prod_w\n",
" \\left(\\phi_{kw}\\right)^{n_{kw}^{-ji}+\\beta-1}\n",
" d\\phi_k\n",
" }\\\\\n",
" &=\\frac{\n",
" \\Gamma\\left(\\beta+n_{kv}^{-ji}+1\\right)\n",
" \\prod_{w\\neq v} \\Gamma\\left(\\beta+n_{kw}^{-ji}\\right)\n",
" }{\n",
" \\Gamma\\left(\n",
" \\sum_{w\\in\\mathcal{V}}\n",
" \\left[\n",
" \\beta+n_{kw}^{-ji}\n",
" \\right]\n",
" +1\\right)\n",
" }\\cdot\n",
" \\frac{\n",
" 1\n",
" }{\n",
" \\int\\prod_w\n",
" \\left(\\phi_{kw}\\right)^{n_{kw}^{-ji}+\\beta-1}\n",
" d\\phi_k\n",
" }\\\\\n",
" &=\\frac{\n",
" \\Gamma\\left(\\beta+n_{kv}^{-ji}+1\\right)\n",
" \\prod_{w\\neq v} \\Gamma\\left(\\beta+n_{kw}^{-ji}\\right)\n",
" }{\n",
" \\Gamma\\left(\n",
" \\sum_{w\\in\\mathcal{V}}\n",
" \\left[\n",
" \\beta+n_{kw}^{-ji}\n",
" \\right]\n",
" +1\\right)\n",
" }\\cdot\n",
" \\frac{\n",
" \\Gamma\\left(\n",
" \\sum_{w\\in\\mathcal{V}}\n",
" \\left[\n",
" \\beta+n_{kw}^{-ji}\n",
" \\right]\n",
" \\right)\n",
" }{\n",
" \\prod_{w}\\Gamma\\left(\\beta+n_{kw}^{-ji}\\right)\n",
" }\\\\\n",
" &=\\frac{\n",
" \\Gamma\\left(\\beta+n_{kv}^{-ji}+1\\right)\n",
" \\prod_{w\\neq v} \\Gamma\\left(\\beta+n_{kw}^{-ji}\\right)\n",
" }{\n",
" \\Gamma\\left(V\\beta+n_{k\\cdot}^{-ji}+1\\right)\n",
" }\\cdot\n",
" \\frac{\n",
" \\Gamma\\left(V\\beta+n_{k\\cdot}^{-ji}\\right)\n",
" }{\n",
" \\prod_{w}\\Gamma\\left(\\beta+n_{kw}^{-ji}\\right)\n",
" }\\\\\n",
" &=\\frac{\n",
" \\Gamma\\left(\\beta+n_{kv}^{-ji}+1\\right)\n",
" \\Gamma\\left(V\\beta+n_{k\\cdot}^{-ji}\\right)\n",
" }{\n",
" \\Gamma\\left(V\\beta+n_{k\\cdot}^{-ji}+1\\right)\n",
" }\\cdot\n",
" \\frac{\n",
" \\prod_{w\\neq v} \\Gamma\\left(\\beta+n_{kw}^{-ji}\\right)\n",
" }{\n",
" \\prod_{w}\\Gamma\\left(\\beta+n_{kw}^{-ji}\\right)\n",
" }\\\\\n",
" &=\\frac{\n",
" \\Gamma\\left(\\beta+n_{kn}^{-ji}+1\\right)\n",
" \\Gamma\\left(V\\beta+n_{k\\cdot}^{-ji}\\right)\n",
" }{\n",
" \\Gamma\\left(V\\beta+n_{k\\cdot}^{-ji} + 1\\right)\n",
" \\Gamma\\left(\\beta+n_{kv}^{-ji}\\right)\n",
" }\\\\\n",
" &=\\frac{\n",
" \\Gamma\\left(\\beta+n_{kn}^{-ji}+1\\right)\n",
" }{\n",
" \\Gamma\\left(\\beta+n_{kv}^{-ji}\\right)\n",
" }\\cdot\n",
" \\frac{\n",
" \\Gamma\\left(V\\beta+n_{k\\cdot}^{-ji}\\right)\n",
" }{\n",
" \\Gamma\\left(V\\beta+n_{k\\cdot}^{-ji} + 1\\right)\n",
" }\\\\\n",
" &=\\frac{\\beta+n_{kv}^{-ji}}{V\\beta+n_{k\\cdot}^{-ji}}\n",
"\\end{aligned}\n",
"\\end{equation}\n",
"\n",
"This is validated in the [appendix of this paper](http://arxiv.org/pdf/1201.1657.pdf).\n",
"\n",
"##### Equation: $f_{k^\\text{new}}^{x_{ji}}(x_{ji})$\n",
"\n",
"We also need the prior density of $x_{ji}$ to compute the likelihood that $x_{ji}$ will be seated at a new table.\n",
"\n",
"\\begin{equation}\n",
"\\begin{aligned}\n",
" f_{k^{\\text{new}}}^{-x_{ji}}(x_{ji})\n",
" &=\n",
" \\int\n",
" f(x_{ji} \\,|\\, \\phi)\n",
" h(\\phi)d\\phi_k\n",
" \\\\\n",
" &=\\int\n",
" \\phi_v \\cdot\n",
" \\frac{\n",
" \\Gamma(V\\beta)\n",
" }{\n",
" \\prod_w \\Gamma(\\beta)\n",
" }\n",
" \\prod_w \\phi_w^{\\beta-1}\n",
" d\\phi \\\\\n",
" &=\\frac{\n",
" \\Gamma(V\\beta)\n",
" }{\n",
" \\prod_w \\Gamma(\\beta)\n",
" }\n",
" \\int\n",
" \\phi_v \\cdot\n",
" \\prod_w \\phi_w^{\\beta-1}\n",
" d\\phi\\\\\n",
" &=\\frac{\n",
" \\Gamma(V\\beta)\n",
" }{\n",
" \\prod_w \\Gamma(\\beta)\n",
" }\n",
" \\int\n",
" \\phi_v^\\beta \\cdot\n",
" \\prod_{w\\neq v} \\phi_w^{\\beta-1}\n",
" d\\phi\\\\\n",
" &=\\frac{\n",
" \\Gamma(V\\beta)\n",
" }{\n",
" \\prod_w \\Gamma(\\beta)\n",
" }\\cdot\n",
" \\frac{\n",
" \\Gamma(\\beta+1)\\prod_{w\\neq v}\\Gamma(\\beta)\n",
" }{\n",
" \\Gamma(V\\beta+1)\n",
" }\\\\\n",
" &=\\frac{\n",
" \\Gamma(V\\beta)\n",
" }{\n",
" \\Gamma(V \\beta+1)\n",
" }\\cdot\n",
" \\frac{\n",
" \\beta\\prod_{w}\\Gamma(\\beta)\n",
" }{\n",
" \\prod_w \\Gamma(\\beta)\n",
" }\\\\\n",
" &=\\frac{\n",
" 1\n",
" }{\n",
" V \\beta\n",
" }\\cdot\n",
" \\beta\\\\\n",
" &= \\frac{1}{V}\n",
"\\end{aligned}\n",
"\\end{equation}\n",
"\n",
"##### Equation (31): $p(x_{ji} \\,|\\, {\\bf t}^{-ji}, t_{ji}=t^{new}, {\\bf k})$\n",
"\n",
"These last two derivations give us Equation (31), the likelihood that $t_{ji}=t^{new}$:\n",
"\n",
"\\begin{equation}\n",
"\\begin{aligned}\n",
" p(x_{ji} \\,|\\, {\\bf t}^{-ji}, t_{ji}=t^{new}, {\\bf k})\n",
" &=\\sum_{k=1}^K \\left[\n",
" \\frac{\n",
" m_{\\cdot k}\n",
" }{\n",
" m_{\\cdot\\cdot}+\\gamma\n",
" }\\cdot\n",
" \\frac{\\beta+n_{kv}^{-ji}}{V\\beta+n_{k\\cdot}^{-ji}}\n",
" \\right] \\\\\n",
" &\\phantom{=}+ \\frac{\n",
" \\gamma\n",
" }{\n",
" m_{\\cdot\\cdot}+\\gamma\n",
" }\n",
" \\cdot \\frac{1}{V}\n",
"\\end{aligned}\n",
"\\end{equation}\n",
"\n",
"##### Equation (32): $p(t_{ji}=t \\,|\\, {\\bf t}^{-ji}, {\\bf k})$\n",
"\n",
"From this, we know the conditional distribution of $t_{ji}$ is:\n",
"\n",
"\\begin{equation}\n",
" p(t_{ji}=t \\,|\\, {\\bf t}^{-ji}, {\\bf k}) \\propto\n",
" \\begin{cases}\n",
" n_{jt\\cdot}^{-ji} \\cdot \\frac{\\beta+n_{k_{jt}v}^{-ji}}{V\\beta+n_{k_{jt}\\cdot}^{-ji}}\n",
" & {\\tiny \\text{if } t \\text{ previously used,}}\\\\\n",
" {\\tiny\n",
" \\alpha_0 \\cdot\n",
" p(x_{ji} \\,|\\, {\\bf t}^{-ji}, t_{ji}=t^{new}, {\\bf k})}\n",
" & {\\tiny \\text{if } t=t^{\\text{new}}}.\n",
" \\end{cases}\n",
"\\end{equation}\n",
"\n",
"##### Equation (33): $p(k_{jt^\\text{new}}=k \\,|\\, {\\bf t}, {\\bf k}^{-jt^\\text{new}})$\n",
"\n",
"If the sampled value of $t_{ji}$ is $t^{\\text{new}}$, we sample a dish $k_{jt^\\text{new}}$ for the table with:\n",
"\n",
"\\begin{equation}\n",
" p(k_{jt^\\text{new}}=k \\,|\\, {\\bf t}, {\\bf k}^{-jt^\\text{new}}) \\propto\n",
" \\begin{cases}\n",
" m_{\\cdot k}\\cdot\\frac{\\beta+n_{kv}^{-ji}}{V\\beta+n_{k\\cdot}^{-ji}}\n",
" & {\\tiny \\text{if } k \\text{ previously used,}}\\\\\n",
" \\frac{\n",
" \\gamma\n",
" }{V}\n",
" & {\\tiny \\text{if } t=k^{\\text{new}}}.\n",
" \\end{cases}\n",
"\\end{equation}\n",
"\n",
"##### Equation (34): $p(k_{jt}=k \\,|\\, {\\bf t}, {\\bf k}^{-jt})$\n",
"\n",
"We need to sample $k_{jt}$ (the dish/topic for table $t$ in restaurant $j$):\n",
"\n",
"\\begin{equation}\\displaystyle\n",
" p(k_{jt}=k \\,|\\, {\\bf t}, {\\bf k}^{-jt}) \\propto\n",
" \\begin{cases}\n",
" m_{\\cdot k}^{-jt}\\cdot f_k^{-{\\bf x}_{jt}}({\\bf x}_{jt})\n",
" & {\\tiny \\text{if } k \\text{ previously used,}}\\\\\n",
" \\gamma\\cdot f_{k^\\text{new}}^{-{\\bf x}_{jt}}({\\bf x}_{jt})\n",
" & {\\tiny \\text{if } t=k^{\\text{new}}}.\n",
" \\end{cases}\n",
"\\end{equation}\n",
"\n",
"where $f_k^{-{\\bf x}_{jt}}({\\bf x}_{jt})$ is the \"conditional density of ${\\bf x}_{jt}$ given all data items associated with mixture component $k$ leaving out ${\\bf x}_{jt}$\" (Teh, et al). (${\\bf x}_{jt}$ is every customer in restaurant $j$ seated at table $t$). $m_{\\cdot k}^{-jt}$ is the number of tables (in all franchises) serving dish $k$ when we remove table $jt$.\n",
"\n",
"This requires $f_k^{-{\\bf x}_{jt}}({\\bf x}_{jt})$; this is different from Equation (30), though they look quite similar.\n",
"\n",
"\\begin{equation}\n",
"\\begin{aligned}\n",
" f_k^{-{\\bf x}_{jt}}({\\bf x}_{jt})\n",
" &=\\frac{\\displaystyle\n",
" \\int\n",
" {\\prod_{x_{ji}\\in {\\bf x}_{jt}}}\n",
" f(x_{ji} \\,|\\, \\phi_k)\n",
" \\left[\n",
" \\prod_{x_{j'i'}\\not\\in {\\bf x}_{jt}, z_{j'i'}=k}\n",
" f(x_{j'i'} \\,|\\, \\phi_k)\n",
" \\right]\n",
" h(\\phi_k)\n",
" d\\phi_k\n",
" }\n",
" {\\displaystyle\n",
" \\int\n",
" \\left[\n",
" \\displaystyle\\prod_{x_{j'i'}\\not\\in {\\bf x}_{jt}, z_{j'i'}=k}\n",
" f(x_{j'i'}|\\phi_k)\n",
" \\right]\n",
" h(\\phi_k)d\\phi_k\n",
" } \\\\\n",
" &=\\frac{\\displaystyle\n",
" \\int\n",
" {\\prod_{x_{ji}\\in {\\bf x}_{jt}}}\n",
" \\phi_{k x_{ji}}\n",
" \\left[\n",
" \\prod_{x_{j'i'}\\not\\in {\\bf x}_{jt}, z_{j'i'}=k}\n",
" \\phi_{k x_{j'i'}}\n",
" \\right]\n",
" \\prod_{w}\n",
" \\phi_{kw}^{\\beta-1}\n",
" d\\phi_k\n",
" }\n",
" {\\displaystyle\n",
" \\int\n",
" \\left[\n",
" \\displaystyle\\prod_{x_{j'i'}\\not\\in {\\bf x}_{jt}, z_{j'i'}=k}\n",
" f(x_{j'i'}|\\phi_k)\n",
" \\right]\n",
" \\prod_{w}\n",
" \\phi_{kw}^{\\beta-1}\n",
" d\\phi_k\n",
" } \\\\\n",
" &=\\frac{\\displaystyle\n",
" \\int\n",
" {\\prod_{x_{ji}\\in {\\bf x}_{jt}}}\n",
" \\phi_{k x_{ji}}\n",
" \\left[\n",
" \\prod_{x_{j'i'}\\not\\in {\\bf x}_{jt}, z_{j'i'}=k}\n",
" \\phi_{k x_{j'i'}}\n",
" \\right]\n",
" \\prod_{w}\n",
" \\phi_{kw}^{\\beta-1}\n",
" d\\phi_k\n",
" }\n",
" {\\displaystyle\n",
" \\int\n",
" \\left[\n",
" \\displaystyle\\prod_{x_{j'i'}\\not\\in {\\bf x_{jt}}, z_{j'i'}=k}\n",
" \\phi_{k x_{j'i'}}\n",
" \\right]\n",
" \\prod_{w}\n",
" \\phi_{kw}^{\\beta-1}\n",
" d\\phi_k\n",
" }\n",
"\\end{aligned}\n",
"\\end{equation}\n",
"\n",
"The denominator is\n",
"\n",
"\\begin{equation}\n",
"\\begin{aligned}\n",
" \\text{denominator}&=\n",
" \\int\\left[\n",
" \\prod_{x_{j'i'}\\not\\in {\\bf x_{jt}}, z_{j'i'}=k}\n",
" \\phi_{k x_{j'i'}}\n",
" \\right]\n",
" \\prod_{w}\n",
" \\phi_{kw}^{\\beta-1}\n",
" d\\phi_k \\\\\n",
" &=\\int\\left[\n",
" \\prod_w \\phi_{kw}^{n_{kw}^{-jt}} \\prod_w \\phi_{kw}^{\\beta-1}\n",
" \\right]\n",
" d\\phi_k \\\\\n",
" &=\\int\\left[\n",
" \\prod_w \\phi_{kw}^{n_{kw}^{-jt}+\\beta-1}\n",
" \\right]\n",
" d\\phi_k \\\\\n",
" &=\\frac{\n",
" \\prod_w \\Gamma\\left(\n",
" n_{kw}^{-jt} + \\beta\n",
" \\right)\n",
" }{\n",
" \\Gamma\\left( \\sum_w\n",
" n_{kw}^{-jt}+\\beta\n",
" \\right)\n",
" } \\\\\n",
" &=\\frac{\n",
" \\prod_w \\Gamma\\left(\n",
" n_{kw}^{-jt} + \\beta\n",
" \\right)\n",
" }{\n",
" \\Gamma\\left(\n",
" n_{k\\cdot}^{-jt}+V\\beta\n",
" \\right)\n",
" }\n",
"\\end{aligned}\n",
"\\end{equation}\n",
"\n",
"The numerator is\n",
"\n",
"\\begin{equation}\n",
"\\begin{aligned}\n",
" \\text{numerator}\n",
" &=\\int\n",
" {\\prod_{x_{ji}\\in {\\bf x}_{jt}}}\n",
" \\phi_{k x_{ji}}\n",
" \\left[\n",
" \\prod_{x_{j'i'}\\not\\in {\\bf x}_{jt}, z_{j'i'}=k}\n",
" \\phi_{k x_{j'i'}}\n",
" \\right]\n",
" \\prod_{w}\n",
" \\phi_{kw}^{\\beta-1}\n",
" d\\phi_k \\\\\n",
" &=\\int\n",
" \\prod_{w}\n",
" \\phi_{kw}^{\n",
" n_{kw}^{-jt} +\n",
" n_{\\cdot w}^{jt} +\n",
" \\beta + 1\n",
" }\n",
" d\\phi_k \\\\\n",
" &=\\frac{\n",
" \\prod_w \\Gamma\\left(\n",
" n_{kw}^{-jt} + n_{\\cdot w}^{jt} + \\beta\n",
" \\right)\n",
" }{\n",
" \\Gamma \\left(\n",
" \\sum_w\n",
" n_{kw}^{-jt} + n_{\\cdot w}^{jt} + \\beta\n",
" \\right)\n",
" }\\\\\n",
" &=\\frac{\n",
" \\prod_w \\Gamma\\left(\n",
" n_{kw}^{-jt} + n_{\\cdot w}^{jt} + \\beta\n",
" \\right)\n",
" }{\n",
" \\Gamma \\left(\n",
" n_{k\\cdot}^{-jt} + n_{\\cdot \\cdot}^{jt} + \\beta\n",
" \\right)\n",
" }\n",
"\\end{aligned}\n",
"\\end{equation}\n",
"\n",
"This gives us a closed form version of this conditional distribution:\n",
"\n",
"\\begin{equation}\n",
"\\begin{aligned}\n",
" f_k^{-{\\bf x_{jt}}}({\\bf x}_{jt})\n",
" &= \\displaystyle\\frac{\n",
" \\prod_w \\Gamma\\left(\n",
" n_{kw}^{-jt} + n_{\\cdot w}^{jt} + \\beta\n",
" \\right)\n",
" }{\n",
" \\prod_w \\Gamma\\left(\n",
" n_{kw}^{-jt} + \\beta\n",
" \\right)\n",
" }\n",
" \\frac{\n",
" \\Gamma\\left(\n",
" n_{k\\cdot}^{-jt}+V\\beta\n",
" \\right)\n",
" }{\n",
" \\Gamma \\left(\n",
" n_{k\\cdot}^{-jt} + n_{\\cdot \\cdot}^{jt} + \\beta\n",
" \\right)\n",
" }.\n",
"\\end{aligned}\n",
"\\end{equation}\n",
"\n",
"We also need the conditional distribution of $k$ is a new dish: $f_{k^\\text{new}}^{-{\\bf x}_{jt}}({\\bf x}_{jt})$. Shuyo provides without derivation:\n",
"\n",
"\\begin{equation}\n",
"\\begin{aligned}\n",
"f_{k^\\text{new}}^{-{\\bf x}_{jt}}({\\bf x}_{jt})\n",
" &=\\int\n",
" \\left[\n",
" \\prod_{x_{ji}\\in \\mathbf{x_{jt}}}\n",
" f(x_{ji} \\,|\\, \\phi)\n",
" \\right]\n",
" h(\\phi)d\\phi_k\n",
" \\\\\n",
" &=\\int\n",
" \\prod_{x_{ji}\\in \\mathbf{x_{jt}}}\n",
" \\phi_{x_{ji}}\n",
" \\cdot\n",
" \\frac{\n",
" \\Gamma(V\\beta)\n",
" }{\n",
" \\prod_w \\Gamma(\\beta)\n",
" }\n",
" \\prod_w \\phi_w^{\\beta-1}\n",
" d\\phi \\\\\n",
" &=\\frac{\n",
" \\Gamma(V\\beta)\n",
" }{\n",
" \\prod_w \\Gamma(\\beta)\n",
" }\n",
" \\int\n",
" \\prod_{x_{ji}\\in \\mathbf{x_{jt}}}\n",
" \\phi_{x_{ji}}\n",
" \\cdot\n",
" \\prod_w \\phi_w^{\\beta-1}\n",
" d\\phi\\\\\n",
" &=\\frac{\n",
" \\Gamma(V\\beta)\n",
" }{\n",
" \\prod_w \\Gamma(\\beta)\n",
" }\n",
" \\int\n",
" \\prod_{x_{ji}\\in \\mathbf{x_{jt}}}\n",
" \\phi_{x_{ji}}^{\\left(\\beta+1\\right)-1}\n",
" \\cdot\n",
" \\prod_{x_{jt}\\not\\in \\mathbf{x_{jt}}} \\phi_{x_{jt}}^{\\beta-1}\n",
" d\\phi\\\\\n",
" &=\\frac{\n",
" \\Gamma(V\\beta)\n",
" }{\n",
" \\prod_w \\Gamma(\\beta)\n",
" }\\cdot\n",
" \\frac{\n",
" \\prod_{x_{ji}\\in \\mathbf{x_{jt}}}\n",
" \\Gamma(\\beta+1)\n",
" \\prod_{x_{ji}\\not\\in \\mathbf{x_{jt}}}\\Gamma(\\beta)\n",
" }{\n",
" \\Gamma(V\\beta+\\sum_{x_{ji}\\in \\mathbf{x_{jt}}} 1)\n",
" }\\\\\n",
" &=\\frac{\n",
" \\Gamma(V\\beta)\n",
" \\prod_w \\Gamma(\\beta+n_{\\cdot w}^{jt})\n",
" }{\n",
" \\Gamma(V\\beta+n_{\\cdot\\cdot}^{jt})\n",
" \\prod_w \\Gamma(\\beta)\n",
" }.\n",
"\\end{aligned}\n",
"\\end{equation}\n",
"\n",
"Given these equations for $f_{k}^{-{\\bf x}_{jt}}({\\bf x}_{jt})$ and $f_{k^\\text{new}}^{-{\\bf x}_{jt}}({\\bf x}_{jt})$, we can draw samples from $p(k_{jt}=k \\,|\\, {\\bf t}, {\\bf k}^{-jt})$ by enumeration over topics. We now have a complete Gibbs sampler for the [Posterior sampling in the Chinese restaurant franchise in Teh, et al.](http://www.cs.berkeley.edu/~jordan/papers/hdp.pdf)\n",
"\n"
]
}
],
"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.4"
},
"nikola": {
"date": "2015-09-21",
"slug": "hdp-lda",
"title": "Notes on Hierarchical Dirichlet Process for Topic Models"
}
},
"nbformat": 4,
"nbformat_minor": 2
}