My Blog Posts, in Reverse Chronological Order
subscribe via RSS
Stanford's Linear Algebra Review
I’ll take a break from the recent discussion on graphical models to go over some old linear algebra concepts. While there are many references on linear algebra designed to help a student relearn what he or she may have forgotten, I found the handout from Stanford’s CS 229 (Machine Learning) class to be one of the best when considering the content, size, and clarity together. And despite being advertised as a review, it actually did teach me a lot. In college, I only took one linear algebra class, a basic introductory and proofsbased course^{1}. While I found it difficult when I was taking it, in retrospect, we did not go over that much material. Two things that really tricked me up when I first arrived in Berkeley were
 Positive Definite Matrices (and related to that, Singular Value Decomposition)
 Matrix Calculus (with gradients, Hessians, etc.)
These two concepts are used all the time in AI, and I really wish I could know them better. Thus, in this post, I’ll quickly go over some of the basic review concepts of linear algebra as presented in the Stanford handout, and then spend a little more time on those previously mentioned topics.
Basic Concepts and Matrix Multiplication
A few years ago, I kept getting confused between column and row vectors when I was reading machine learning literature, but now I know that all vectors are assumed to be columns by default. Also, when denoting row vectors, be careful that the transpose sign does not necessarily mean we are taking the corresponding column and then transposing it (as the notes say, the definitions of and are ambiguous).
Matrix multiplication is at the heart of linear algebra and what we do in AI. I find it easiest to reason about matrix multiplication by always checking the number of rows and columns of the matrices involved, and making sure they align correctly. But this handout made it easier for me to think of other ways to view how multiplication works. They describe four interesting ways of looking at matrix multiplication, of which the most interesting to me is when the product is expressed as the sum of outer products (between vectors).
Operations and Properties
Some interesting operations and properties of matrices:

Any square matrix can be represented as a sum of a symmetric matrix and an antisymmetric matrix, since .

The trace is the sum of the diagonal elements of a matrix. Despite its seeming simplicity, the trace operator will actually be very useful in matrix calculus (the “trace trick”) and in the study of eigenvalues and eigenvectors, since the sum of the eigenvalues of a matrix equals the trace, even though the individual components being summed up generally have no relation.

The norm of a vector is like an informal measure of the distance. For some reason, these kept confusing me when I was trying to work on my undergraduate thesis, so it’s nice to see it formally treated here. There are several properties of norms, but in general, I tend to view norms of vectors as just the norm. Also, when working with norms, make sure I know the CauchySchwarz inequality: .

The rank of a matrix is equal to the number of its linearly independent columns (or rows). There are many implications of the rank; for me, the one I remember the best is that in order for an inverse to exist, a (square) matrix has to be full rank. If we generated a square matrix by filling in its values from a random number generator, then it will almost always have full rank.

A square matrix is orthogonal if . This requires the columns of to be orthonormal^{2}. A nice property: , which we can prove by squaring them, thus considering and .

The range, i.e., column space, of a matrix is . The null space is ^{3}. Vectors in the former set have length , those in the latter have length , and we can get the complement of each of those sets by considering and . For instance, considering the sets and , it turns out that the intersection of those two sets is empty, and that every vector in can be expressed as the sum of two vectors, one in each of those two sets. Thus, they are orthogonal complements. The two column spaces, combined with the two column spaces (using and ) form the four fundamental subspaces, a term popularized by Gilbert Strang. I’m not sure I really get it, though.

The determinant of a square matrix , denoted as or , is a mysterious function that maps to the real numbers. The three main properties it satisfies are:
 that the identity has determinant one
 that if we multiply a single row in by a scalar , the determinant of the new matrix is
 that if we exchange any two (different) rows, then the determinant of the new matrix is
All the other properties of determinants follow from this, including the cofactor expansion, which I will not list here. Determinants have an intuitive interpretation in terms of volume: if we take the span of the rows of , and restrict all the coefficients to be in , then the determinant is the volume of this set. Also, determinants are useful for checking if exists. Finally, the adjoint of a matrix, , is .

Given a square, symmetric matrix , a quadratic form is a function mapping and to the reals. Our variables are , so even if the dimensions of are large, the highest power of any (i.e., a component of ) that can exist in is two, hence the intuitive name.
Something that is related is the concept of a positive semidefinite matrix, which is a symmetric matrix such that for all . We can also define the obvious analogues for definite, negative definite, etc., matrices. Given any, not necessarily square matrix , the matrix is always positive semidefinite; prove this by using .
Note: quadratic forms like these are the start of an introduction to what really happens in machine learning and AI, where we have to understand matrix representations.

For determining eigenvalues and their corresponding eigenvectors (note: eigenvectors are not unique), refer to if we are going to be solving them by hand.
We tend to use (capital lambda) to represent the matrix of eigenvalues, so the matrix equation will result in , implying that , hence is diagonalizable.
Some interesting properties: the trace and determinant are the sum and product, respectively, of the eigenvalues. The eigenvalues of a triangular matrix are just the elements on the diagonal. If is nonsingular, then is an eigenvalue of with eigenvector .
If we have a symmetric matrix , then (1) all eigenvalues are real, and (2) its eigenvectors can be scaled to be orthogonal to each other, so that above can turn orthogonal, and hence we have , a good thing because transposes are easier computationally than inverses. Unfortunately, I don’t currently know how to prove these off the top of my head.
Singular Value Decomposition
With a symmetric matrix , we can set instead of using . It turns out we can generalize this to arbitrary (not even square!) matrices by expressing them in singular value decomposition form, . The and matrices are square and orthogonal, and represent eigenvectors of and , respectively.
The matrix is diagonal, possibly nonsquare, and contains something called the singular values (not the eigenvalues!) of matrix . They fill the first diagonal elements for a rank matrix , and the rest of the elements are zero. The singular values are the square roots of the nonzero eigenvalues of both and (which are symmetric, so they have real eigenvalues).
For a positive definite matrix, .
There are many applications of SVD, and the ones I’m familiar with happen to be in computer vision. Some of these applications have to do with the fact that and give orthonormal bases for all four fundamental subspaces. For instance, to use SVD to compute the null space of , then look at the last columns of .
Matrix Calculus
I really wish I had completely digested this section before taking CS 281A here because I had no idea how matrix calculus worked until I reviewed this document. Given a function , the gradient of with respect to is a matrix (i.e., it’s the same size as the original matrix input to ) of partial derivatives of .
The Hessian is the second derivative analogue to the gradient (well, almost). Here, we assume we are taking the Hessian with respect to a function , where we take a vector as input (not a general matrix, even though that’s possible).
The Trace Trick
Here’s a neat trick I learned from reading Mike Jordan’s notes. Since the trace is such that is the same no matter what the ordering of the three matrices, and since (verify this by explicit computation) then we can claim:
Intuitively, this makes sense since we just eliminated . In the past, when I tried computing derivatives like these, I would convert the entire expression to an enormous set of summations, and try to reason element by element. That is way too much work.
There is a related trick involving determinants: the derivative of with respect to is .
One can use these tactics to take the MLE of the covariance matrix of a multivariate Gaussian distribution, since the log likelihood of that term has and in it.

When I took the course (with Professor Elizabeth Beazley), it was called MATH 211. Now it’s called MATH 250 since the department put the core courses in the “50”s with their muchimproved numbering system. ↩

Watch out with the terminology! Some people might call such matrices as orthonormal matrices, but I call them orthogonal. ↩

This is technically called the right null space, or the kernel, since we may wish to refer to the left null space, which uses . ↩
Hidden Markov Models and Particle Filtering
In this post, we will continue our discussion of graphical models by going over a special kind known as a Hidden Markov Model (HMM). These are appropriate for modeling forms of sequential data, implying that we finally relax various forms of “independent identically distributed” data or variables. They are a piece of the broader class of models known as Dynamic Bayesian Networks, of which Kalman Filters – which we will discuss in a future blog post here – are their continuous analogue.
Hidden Markov Models
HMMs are one of the most popular graphical models in real use (indeed, probably the most popular). They are characterized by variables representing hidden states and variables representing observations (i.e., evidence). The subscript in and represents a discrete slice of time. There are three probability distributions in HMMs: a prior probability , a transition probability and an emission (sometimes called observation) probability . That the transition probability only depends on the previous state means we are effectively invoking the Markov assumption. Whatever information about the state in the last time slice is all we need to know to determine the likelihood of the current state. Our distribution is stationary, which means that we only need to specify those three probabilities and we are set, because the CPTs are “carried over” to all the time slices. This is an instance of parameter tying.
There are several welldefined inference tasks and algorithms that we can conduct on HMMs, to which we now turn. True, we could simply take an HMM and run variable elimination on it, but the structure of HMMs provides us with some recursion we can exploit.
Filtering, Smoothing, and Predicting
These three are all similar. Given a series of observations, we want to determine the distribution over states at some time stamp. Concretely, we want to determine . The task is called filtering if , smoothing if , and predicting if . Clearly, smoothing will give better estimates, and prediction the weakest (or most uncertain) estimates.
To compute filtering estimates, we require a recursive computation. The actual algorithm we use is the forward algorithm. We will talk more about it later; here is the update equation for the forward algorithm:
where capital variables denote unknown variables, and the lowercase ones denote known variables. One can see that this equation makes intuitive sense. We sum up over all possible state realizations in the previous time step by their transition probability to the current state, and for each of those, we weigh the probability by the likelihood of emission from the state (which doesn’t depend on so we can move it out of the sum). Furthermore, note that this is a recursive computation, a theme that will be common when we talk about inference in HMMs. Finally, the normalizing constant here is computed by summing up for all possible realizations of . The normalizing is needed only because of the addition of the emission model probability downscaling our values.
Given that we know filtering, prediction is not that much more work, because we have the value up to the latest evidence variable, then it’s like we are ignoring the evidence variable and only using the transition probability:
(Here, we have no proportion sign.)
As we predict further into the distribution, we will eventually end up at the stationary distribution of the underlying Markov chain governing the sequence of state realizations. We can compute this as , where is the matrix of transitions, and is the column vector of priors.
For smoothing, we actually need to compute something called a backward probability:
The first component is what we were able to compute earlier, and we can do this recursively from the starting state. The second component is new, but in fact, it is not too hard to compute recursively – we just have to start backwards from . Here is the update:
Notice now that we cannot move anything out of the summation, like we did with the forward update.
We can run something called the forwardbackward algorithm, which will compute the forward probabilities, then the backward probabilities. We will return to these “forward” and “backward” subroutines in the following subsections, with two helpful figures representing these two algorithms.
The Likelihood of Observations
In this section, we’ll talk about two other common tasks in HMMs. The first is computing the likelihood of an observation sequence, . At first, it might not seem obvious how to do this, because we don’t even know the state sequence that caused the observations! Following the laws of probability, we have to include the states in our joint sequence, and then sum up over them. To do this efficiently, we use a dynamic programming algorithm called the forward algorithm, which folds together the paths that could have generated the observations.
I find it easiest to think of this computation just by looking at a trellis, like the one in the following image from Jurafsky’s book^{1}:
This example was about determining the observation likelihood of seeing three, one, and then three ice creams eaten in a day, and the state variables represented the temperature of the day. In the trellis, the rows correspond to the possible realizations of each state, along with one other row to symbolically represent the emission/observation/evidence. The notation of represents the probability of the observation sequence and being at a particular state at time , so . The computation of is done recursively in an intuitive manner, as shown in the image:
Intuitively, the likelihood of an observation sequence should be the sum of all the possible state sequences that could have led to that observation. The fact that we are summing up and storing probabilities in the previous terms is an indication of the HMM’s independence assumptions, and is what lets us do inference efficiently. Oh, and to actually compute the observations, we do need to sum out from .
By the way, did you notice that these “updates” that we are doing are exactly what we did for the filtering step earlier? It’s just taking an earlier factor (representing whatever probability we want) and multiplying it by the transition and then the emission probability! In Russell and Norvig, the update equation is called , and can represent or . While and are clearly different, their updates are the same. And this FORWARD subroutine is what we generally mean when we talk about “the forward algorithm.”
The Viterbi Algorithm (Decoding)
This is another common inference task. Given a series of observations, the Viterbi algorithm helps us to determine the most likely sequence of states the system went to produce those observations. This is similar to the forward algorithm that we used to compute the likelihood of an observation sequence, but here we will now take maxes instead of summations, and we also have to keep a series of “backtrace pointers” so that we can reproduce our path.
Here’s the intuition: suppose we want to find the most likely path to some state . To do that, we need to find the most likely state path through , and then compare all the possible realizations of along with the transition (and emission, but those are the same) probabilities going to the current state. Then we pick the highest likelihood one, and add that particular to the path (note the lowercase here!). Mathematically, we express it as follows:
The “messages” here are not forward messages but instead
The time and space complexity is linear in terms of the length of the sequence, , but for filtering, we don’t have the space taking up linear time since there are no backpointers.
Training the Parameters with EM
The purpose of the BaumWelch algorithm (which is an example of the ExpectationMaximization algorithm), is to determine the parameters of an HMM given observed data. Somewhat confusingly, Jurafsky calls it the forwardbackward algorithm. Russell calls the forwardbackward algorithm as the algorithm that computes all the forward and backward probability messages defined earlier^{2}. But I guess they might be doing the same thing, because training HMMs will require us to compute the backward probabilities anyway. Jurafsky defines the backward probability to be exactly the same as what Russell defines it, i.e., as the probability of seeing the observations from time to the end, given that we are at some state in time , or . The recursion of the backwards probability is shown in the following image, where we now see that we had to keep separate emission probabilities for each state, unlike in the forward probability case.
To estimate the parameters of the transition probability matrix, for a given element , we must count the expected proportion of times that the system undergoes a transition from state realization to state realization . The expected counts are what EM computes, since those are the latent variables.
The following diagram shows the various things we have to compute to get the expected counts. Notice that the diagram is computing the probability of being in state then state , jointly with the observation sequence. Thus, after this step, we would divide by (or in their notation). We then sum over all times, and that gives us the expected number of transitions from state to state .
We follow a similar procedure for estimating the parameters of the emission probability.
The Mstep is simple here; it turns expected counts into MLE for the corresponding parameters.
Particle Filtering
I got confused about particle filtering earlier. I assumed it broadly meant any algorithm that sampled something, but in fact, it has a specific meaning in the context of HMMs and Dynamic Bayesian Networks more generally. It is indeed a sampling algorithm, but has some interesting properties that are worth discussing in its own right.
First, when do we want to do particle filtering? We want to do this if exact inference in HMMs is intractable, or if the speed of sampling (and computing its corresponding probabilities) is more important than exact answers.
A naive way of performing exact inference in an HMM is to “unroll” it into a normal Bayesian Network. Technically, HMMs are infinitely long, so what we would do is chop off the HMM after the last time step that has a query or evidence variable, since by the structure of an HMM, anything after is not relevant to the query^{3}. But if we did filtering after each observation, we would have to redo a lot of computations from the start to the set of variables at the current time step. To save on memory costs, we realize that if we do filtering for a given time , we only need the relevant probability distribution from the previous time , since the computation is done recursively. Unfortunately, when we do this “summing out” process ala variable elimination, that will iterate through the set of states, and this computation will grow exponentially due to the need to construct an exponentially large factor table^{4}.
Thus, we need to use approximate inference methods. In a previous post, I discussed likelihood weighting, which is a sampling algorithm that we can adapt for the HMM setting. We do, however, have to ensure that we do not unroll the entire HMM, and we have to make sure our weights are not being driven to zero.
Likelihood weighting (just like rejection sampling) suffers as the number of evidence variables increases, because the weight of each sample is based on the product of all the terms, which drives our values down to zero. But there is something that might be even worse than that. When we sample our nonevidence variables, ideally we would like those samples to depend on the evidence variables, so they can “guide” the sampling correctly. Unfortunately, if all the evidence variables tend to be downstream, i.e., they occur late in the variable ordering, then the sampling for the nonevidence variables will not depend on the evidence, even though the actual variables should depend on each other^{5}. Sadly, in an HMM, all state variables have the property such that all the evidence relevant to them is downstream! (The ones before it are not downstream, but they are not ancestors either!)
What does particle filtering do, then, keeping in mind the weaknesses of likelihood weighting? Here is how the algorithm works. We initialize a population of samples. The quantity determines the time complexity of the algorithm, not the number of states. Then for each time step, we perform the following steps in sequence:
 We transition the samples according to the transition probabilities . This means we do not have to unroll the HMM.
 We then observe the evidence and downscale (i.e., multiply by a value in ) each sample according to the evidence . This is where the likelihood weighting “adaption” is obvious because now each sample is a fraction of a sample with some weight, and the overall distribution over the state variable’s realizations is the fraction of weights there divided by the total fraction of weights across all realizations.
 Then (and this is important), we resample to renormalize the distribution, where the resampling process is sampled according to the weights of the samples.
Here is a nice diagram from the CS 188 slides that suddenly clarified particle filtering for me.
Here, we assume we have one state variable, with nine possible realizations. (Or we could have two state variables, each with three realizations, etc.) We have ten particles, representing our estimate of the distribution . Now the goal will be to determine the table of values. First, we transition, which means we move each of the ten particles over to their next realization and multiply accordingly. Then, we observe , and downscale the values, resulting in points of different sizes, which are a diagram notation for weights. Above, we assume that whatever was, the probability is high for that spot at coordinate , and lower as we gradually go away from that spot. The last step is to resample, so we are resampling based on the probability distribution of the weighted samples. So we get five samples in that block because divided by the sum of the weights (note: there’s a typo on the slides) gives a “high” probability. Note now that we have “normalsized” samples again.
At this point, we should ask ourselves why we even bother resampling. It’s not completely clear to me, but it has to do with how we constantly rescale the weights downwards. In general, it’s not a good idea to have such small weights, because then the more evidence we have, the more likely we will just have a bunch of zeroprobability particles. In other words, most particles end up in lowlikelihood places in the state space. Still, I don’t quite buy some of the textbook explanations on this because couldn’t we just perform the computations in log space?

I know it says the words “DRAFT” there, because I copied these images from an online version of the book. However, I did buy a hard copy of the book, so I think it is fine for me to do this (i.e., not illegal). If I did not have that online version, I would have taken a picture of the book with a cell phone, but then the figure would not have been perfectly flat. ↩

In the EM chapter, Russell says that his forwardbackward algorithm can be slightly modified for the EM case. ↩

In other words, the variable elimination algorithm would be able to push the summation sign for those variables and isolate them to the right. They would sum up to one and thus are irrelevant. ↩

I apologize if this is not clear. I think the easiest way to understand this is that this is still normal variable elimination, so it suffers from the sample computational complexity problems. What makes this confusing is that we tend to have one hidden variable , but that hidden variable can actually taken on different realizations. But what is happening here (and which Russel and Norvig discuss in their book) is that our variable represents a set of state variables. As the set increases in size, the process of summing out will require as many sums as there are state variables. At first, I thought the intractability came from how our one hidden variable could take on many values, but the intractability is actually when we have multiple hidden variables (and we would just use as shorthand to represent all of them). But both views can be made compatible if we have one megavariable that takes on values from each state variable into one realization. ↩

I hope I’m thinking about this correctly. I think the issue is that when we sample, we assume we only know the CPTs, which only express nodeparent relationships, so there is no notion of sampling in the “opposite direction” where we could sample a node based on the value of its children, even though those values do depend on each other according to the Bayes Ball algorithm. ↩
ExpectationMaximization
All right, I lied in the title of my last post. That wasn’t the final word on graphical models after all^{1}. In this post, we’ll discuss ExpectationMaximization, which is an incredibly useful and widespread algorithm in machine learning, though many in the field view it as “hacking” due to its lack of statistical guarantees^{2}. At first, I wasn’t sure what this had to do with graphical models. True, it’s not what I’d call core graphical models, but what happens is that a lot of problems that “factor” into nice Bayesian Network representations can use Expectation Maximization to determine the optimal value of their parameters (which are CPTs in the Bayesian Network case).
The Formalities of ExpectationMaximization
ExpectationMaximization (EM) is an extremely useful algorithm that applies in cases when we have a model with unobserved (i.e., latent) variables, and we need to train the model parameters despite the lack of that information. Why do we want latent variables? It helps to simplify our models. The canonical example is seeing a bunch of health measurements, then adding in a hidden node that represents the presence of heart disease.
We apply EM when we have some probabilistic model in which we are trying to find the “best” model parameters , which usually means taking the Maximum Likelihood Estimate (MLE). Crucially, is our observed variable (or a set of them), while represents the latent variables. Usually, we use logs when maximizing likelihood, so this is equivalent to finding , which Mike calls the incomplete log likelihood^{3}. This is hard; marginalization here (because it involves summing over ) tends to “make problems harder.” My intuition: having sums means we don’t usually have a closedform expression of the likelihood function, so how can we find the MLE for ?
We can define the expected complete log likelihood with some other distribution as , in other words, averaging out . A good choice of means that the expected log likelihood can be treated as the actual log likelihood.
EM is divided into two steps that repeat. Informally, the Expectation step assigns values to the hidden variables based on . (In most examples, these are indicator variables.) In the Maximization step, we find the MLE of , which is easy this time because we actually “know” what the variables are — we assigned them in the previous step!
There are two “formal perspectives” of EM, one relating to log likelihood, and the other relating to a concept known as the KullbackLeibler Divergence.
The Log Likelihood View
Let us define the following quantity:
We further know from some algebra that , i.e., that defines a lower bound of the incomplete log likelihood, for an arbitrary function.
We can now define our E and M steps more precisely:
 E step:
 M step:
What does this really mean? Note that in both cases we are trying to maximize a lower bound on the incomplete log likelihood, which makes intuitive sense: that is the point of MLE!

In the E step, we are finding the best , and this can actually be solved analytically. It’s right under our noses: . I mean, that’s just the true distribution of the hidden variable, given the known data and the current parameters! It sounds obvious.
There are two ways to show this. One is to plug in that choice of in the function , and we get equality with the incomplete log likelihood, . Hence, that’s the best we can get! The other is to take the KLDivergence view (spoiler for the next section!) and see that
which shows that to minimize the difference between the two distributions, we need to set the two distributions to be the same. Remember again that is a lower bound. I know I say this a lot, but it’s important.

In the M step, we maximize the expected complete log likelihood, which we recall we defined earlier as . This happens because breaks into two terms, one which is the expected complete log likelihood, and the other which does not depend on .
Taken together, what do these steps really do? Let’s look back at the incomplete log likelihood . This is hard to optimize, as we said earlier, but what the E and M steps do is that, during each iteration, they collectively keep maximizing this function! It’s a “hill climbing” algorithm that will keep increasing (or technically, never decrease) the incomplete log likelihood. Why is that the case? Here’s the intuition. The M step means that we are maximizing , a lower bound to the incomplete log likelihood. Hypothetically, suppose was actually equal to that incomplete log likelihood. Then the M step would necessarily result in a (highly desirable!) increase in . But this only works if there is equality … if there is a gap, the plan is thrown out the window.
Fortunately, the E step comes to rescue us, because we already know that for an appropriate choice of , we can get to be equal to . Note that the thetas here should really have a time superscript attached to them (as well as the s) but I left them out for simplicity.
In conclusion, EM will keep increasing each iteration. This, if you recall, was the whole point of this process anyway! And it does this by cleverly optimizing the function, which is much easier to handle than the incomplete log likelihood. Absolutely brilliant!
The KullbackLeibler Divergence View
We can alternatively bound the KLDivergence. Derive the bound
In words: the KLdivergence between the empirical distribution and the model is upper bounded by a “complete” KLdivergence. The intuition is that we are adding in the to “average over” the values of , just as we did earlier with the expected complete log likelihood term.
Our results carry over from the previous section because minimizing the right hand side of that expression above (with respect to and ) is equivalent to maximizing with respect to the same variables. Why? As part of the derivation for that bound above, we invoked , but that part was embedded inside an expression such that there was a negation attached, so maximizing means minimizing the overall problem.
Letting , the steps are:

E step:

M step:
Example on Gaussians
This example is from Russell and Norvig (2010). It is about clustering on a dataset presumed to be generated by three unknown multivariate Gaussian distributions. Here’s an example of a plot that could represent such data, with each data point highlighted by the color of its distribution (which we would not know — they are only there for ease of readability). Note: the figure here isn’t the exact one in the textbook, but it’s close.
I like this example because it makes it clear how to view the s as indicator variables.
To formalize the problem, we assume that each data point is generated by picking one of the three twodimensional, multivariate Gaussian distributions, then sampling from that distribution. Thus, the goal in using EM is to find the parameters of the model:
 the weight of each component (i.e., each multivariate Gaussian)
 the 2dimensional mean vector of each component
 the covariance matrix of each component
To start off with EM, we first initialize the three sets of parameters above to some random values^{4}. Then we iterate:

E step: this will determine the expected value of the hidden indicator variables in this model, which we define as , which is one if point was generated by Gaussian component and zero otherwise. From the earlier discussion, we know that we need to use the function. Assuming that the points are IID, this means for each point , we compute where .
We can transform this into an easier problem using Bayes’ rule, and claim that . The first quantity, , is a straightforward plug and chug into the formula for the density of a multivariate Gaussian, because we assume it came from the th one, and we have the parameters and from . The second quantity, is like computing the probability that a component generated a given point, but without any dependency on any point , we just probabilistically choose based on the component’s overall weight , which we get from .

M step: given the fact that we “know” which component generated which data point in a probabilistic sense, then we can find better values of the parameters using the following update equations:
 The weights:
 The means:
 The covariances:
where is the total number of data points and , the “number” of data points assigned to component (in a probabilistic sense).
Why does the M step work? The weight update is like that because that is the fraction of points we assigned to that cluster, and . The means and covariances is from the MLE for the parameters of multivariate Gaussians, except that we have to scale by an expectation because clusters are only assigned fractions of points.
For a more complicated example of EM, look at how EM is used to train the parameters of Hidden Markov Models.

I should have realized this a while back when I saw how many chapters Michael I. Jordan was able to write about graphical models. The last two posts I wrote about graphical models only covered chapters two through four. ↩

To future Ben Recht students, this is why he does not like ExpectationMaximization. Well, that, and because he (correctly) argues that it’s so good, it renders other attempts at approaching problems futile. ↩

When I was drafting this post, I often viewed the incomplete log likelihood as the “true” data log likelihood, which should be equivalent. I mean, we are summing out an arbitrary amount of s to result in a “true” distribution, I think? ↩

It would be better to initialize them sensibly in some way, which often requires assuming some extra knowledge about the problem. ↩
Closing Thoughts on Graphical Models
The Basics
There are two main types of graphical models: directed (Bayesian Networks) and undirected (Markov Random Fields). There are also chain graphs that mix the two, but I will ignore those for now. Bayesian Networks are interesting because they let us model generative processes. We can look at arrows in a diagram and think: oh, there’s an arrow from to , hence the former variable must have some kind of causal relationship with the latter. Then we can continue that line of thinking to “generate” each variable. For a Markov Random Field, we can think of there being a local structure to the nodes.
One of the main reasons why people like graphical models is that it helps us concisely express probability distributions. If we have a distribution where is in the hundreds (a typical realworld case) a naive tabular representation of the distribution is out. But with a Bayesian Network, what we can do is decompose the joint into nodeparent conditional probability tables (CPTs). Using the chain rule on a Bayesian Network, with nodes listed in a topological ordering^{1}, we can decompose the joint into distributions of each node, and then use the independence assumption to get rid of (hopefully lots of!) certain variables being conditioned on in . For a Markov Random Field, we can decompose the joint probability into a product of functions on maximal cliques, , where is the set of maximal cliques of the graph^{2}. One of the most confusing things about the clique functions is that they are not (generally) probability distributions! All we require is that they are an arbitrary, local nonnegative function. To enforce nonnegativity, it is common to exponentiate functions, thus we often see these expressed in terms of exponentials^{3}. Note: it’s important to understand the highlevel idea here. In both the directed and undirected cases, we have figured out a way to efficiently decompose the joint into the product of functions that act on a local set of nodes.
Given that we have decomposed the joint distribution in terms of products of local functions, it’s important to realize that, while we’ve obviously imposed some “constraints” on the graph, a graphical model still expresses a family of distributions. In a threenode “chain” Bayesian Network, which means , we are allowed to tweak the three CPTs of our graph, so long as the product still obeys a probability distribution (i.e., nonnegative and sums to one). For a Markov Random Field, we can tweak the potentials.
There is also an equivalent way of expressing the family of distributions inherent in a graphical model. We can do that by listing all the conditional independences enforced by the graph. This means that for all triplets of disjoint variable sets which are a subset of all the variables in the graph, we need to check whether . By taking the set of distributions that factor according to these independences, we find that they are equivalent to what we would get if we started with the nodeparent probabilities and “tweaked the CPTs.”
It’s easiest to determine whether in the undirected case, because to determine whether such a statement is true (and therefore should be listed in that list of independences) we delete the nodes of and check whether a path still exists from to .
The directed case is a little more complicated, but more intellectual. We run something called the Bayes Ball algorithm. To explain this, we first consider three canonical graphs: a chain , a wedge , and (the most interesting case) a vnode . The rules are as follows:
 In the chain, . No other assertions are possible.
 In the wedge, . No other assertions are possible.
 In the vnode, . No other assertions are possible.
To explain the third case in more detail is to understand the explaining away phenomenon. This happens when we have two or more competing clauses that attempt to explain the same thing. Those clauses are independent of each other, but once we observe the variable, then all of a sudden they are dependent on each other, because if one of them “caused” , it is likely that the other one did not. But without , we can’t say much. Kevin Murphy has a nice example on his tutorial page, where a college only admits smart students, or athletes, and those variables are independent in the population. Then if we see that a student is at the college, then we can “explain” this phenomenon in two ways: if the student is smart, or an athlete.
To run the Bayes Ball algorithm, we “shade in” the nodes of , which serve intuitively as “blocking” nodes. We start with balls in nodes from , and see if any one of them can reach any node in , subject to constraints on the movements of the balls due to the canonical graphs. Note that there are a few other special cases that we have to consider. If we have a chain but has no other children, then the ball is allowed to “return” in the opposite direction, so long as is shaded. It is as if there is a duplicate node, which would be like applying case three with the vnode, and balls only pass in that case if the node is shaded.
So, if you are ever given a Bayesian Network and have to determine whether certain conditional independences hold, just as in the Spring 2011 Berkeley AI final, just run the Bayes Ball algorithm.
By the way, it should be clear from the above that identifying conditional independences is easier for the undirected case, which is what tends to motivate their formulation, but for the directed case, it’s easier to think of them in terms of nodeparent probabilities. But we could try thinking of them in the opposite way.
The SumProduct Algorithm and Factor Graphs
In my last post, I described two ways of performing exact inference on general graphical models. That’s actually not the final word on exact inference: it is possible to develop a more useful version of variable elimination, under the assumption that the graphical model is a tree. This is not that restrictive, since we can express a lot of realworld problems in terms of trees. The key advantage of the algorithm known as the SumProduct Algorithm^{4} is that it lets us compute all marginals simultaneously. If you recall, in variable elimination, I assumed that we only had one query node, so that algorithm would have been useful for , but not . Fortunately, with trees, we can compute all of for each node. Including evidence isn’t a problem; we can also compute . Remember that we should consider marginals and conditionals as equivalent.
How does it work? Remember how in variable elimination, we would create intermediate values for node as it got eliminated? We will do a similar thing here; these are now technically called messages and we can index them as to describe a message from node to , where node was the one that got eliminated.
The SumProduct algorithm begins messagepassing at the leaf nodes of the graph by eliminating them and passing their intermediate messages (the functions) to their neighbors. Then the process repeats. The rule is that each node can only send a message to another node, as long as it has received messages from all its other neighbors. Since the graph is a tree, this immediately implies that messages have to start at the leaves (as stated earlier) and, furthermore, that no new edges will be created in the elimination process.
To be precise, the formula for a message from to (hence, eliminating ) is:
This equation is from Michael I. Jordan’s notes on graphical models. Don’t worry too much about the notation, but here is a little about it: the function is his way of including evidence variables^{5}. The is the potential function on the maximal clique of and ; in trees, the maximal cliques are always of size two. The interesting stuff comes when we consider the functions. These are all the other incoming messages from the neighbors of node , except . This should remind us of variable elimination. When we eliminate nodes, we pass intermediate factors back to whatever node has dependency on it. Since we are in a tree, that means the intermediate message will have a dependency on whatever neighbor is remaining. That is why the functions depend on , but the function depends on (not ).
Oh, by the way, we are dealing with undirected graphs here, not directed ones. We can treat these on equal footing because we have trees, so in the undirected case, , and the singleton potentials are either 1, or for the root^{6}.
At the root node, we can determine the marginal probability as proportional to the product of incoming messages, where the proportionality can be resolved by iterating over the possible states/realizations for the root node.
That’s nice, but how do we determine marginal probabilities for all nodes? When we did this for the root node, it was as if the messages started from the leaf nodes and propagated inwards towards the root node, due to the protocol that a node couldn’t send a message unless it got messages from all other neighbors. The clever insight is that we now propagate messages outwards from the root node, into all the leaves! What happens after this is that every edge in the tree now has two messages on it, in opposite directions. Then, for each node, take the product of its incoming messages. We now have marginal probabilities for all nodes! The amazing thing is that this only requires double the amount of work it took for the first step to send messages inwards to the root. Unfortunately, as mentioned earlier, this only works on trees. But it definitely works well.
Taking another perspective, suppose we were not interested in determining a distribution, but wanted to do MAP inference. That means figuring out the maximum probability possible in any configuration (or determining the actual configuration itself). To do that, just replace the “sum” operators with “max” operators in the formulas, and things will work from there^{7}.
Finally, let us consider factor graphs, briefly. Given an undirected graph, we can associate with it a set of factors , where each factor is a function on a set of nodes. The sets may not be unique among different factor functions, and they might not also correspond to maximal cliques. Actually, the one thing we really want them to obey is that there is a giant factor function of all the variables that can be “factorized” (hence the name) into the individual factors, and that we can evaluate for one factor efficiently (kind of reminds one of probabilities and potentials, huh?). Then when we draw the graph, the only edges that exist are those from normal nodes to factor nodes , and an edge exists between them if and only if the factor function takes node as input.
Some advantages of factor graphs are that:
 Sometimes, we want to express a family of probability distributions at a finer level than is possible with conditional independences. Factor functions let us be arbitrarily precise in how we want to model the interactions between variables, while potential functions are more limited. With the complete graph , no conditional independence assertions are possible but we might want to endow the sole potential with some structure: , but note that adding extra nodes can do the same thing^{8}.
 We can convert undirected trees that are “almost” like trees (perhaps they have just one clique of size three) into factor graphs that are trees, ignoring the distinction between factor and variable nodes.
 We can apply a similar version of the sumproduct algorithm to factor trees, which uses messages from nodes to factors and factors to nodes, even if the original tree (before the factorization was added) was not actually a tree.
There are also directed graphs that are almost like trees, e.g., polytrees, which are trees if we drop the orientation of edges, but they also have multiple parents to each node, which poses a problem if we do any moralization. We can convert these to factor trees (yes, trees) and directly apply the SumProduct algorithm.
Sampling for Graphical Models
In many cases, exact inference is intractable in graphical models, so we resort to approximate methods. Here, I’ll briefly review approximate inference in Bayesian Networks, with an emphasis on particlebased methods, which generate samples from the network.
One confusion I originally had when I first learned about this was that it was unclear what assumptions we were making about the information we possessed. To clarify, we’re going to assume that we know all the CPTs^{9} of the graph, but that’s it. The goal will be to generate a full set of samples from this CPT. That means if there are variables in the Bayesian Network, we want to obtain samples for large .
Using the CPTs, how do we sample? Here’s an almost trivial method, often called direct sampling: we iterate through the variables in a topological ordering, and for each one, sample its state^{10} from its CPT. The parents of the node in question (if any) must be set to the value that they were sampled at earlier, which we know happened due to the topological ordering. Once we’ve gone through all samples, we repeat.
In the general case, we’ll want to make use of whatever evidence variables we have in , which direct sampling fails to consider. To do so, we can use rejection sampling, which means that we do direct sampling, but only keep the full samples that are consistent with the evidence . Unfortunately, as the evidence increases, it gets increasingly unlikely that we will ever generate a compatible sample! To fix the problem of rejecting too many samples, we can use likelihood weighting which will force the evidence variables to be at their fixed values. That is to say, given a network of variables where we want to sample full elements that have , then we would fix that value and sample the other variables normally with the direct sampling method.
Unfortunately, even this doesn’t work out well, because we actually have to weigh the samples we get by the value of the evidence variables! It’s easiest to think of likelihood weighting as always generating compatible samples, but each sample is actually worth only a fraction of a sample, quantified by its weight . We compute a sample’s weight by multiplying the values of the evidence variables together. Intuitively, evidence variables that seem to be incompatible with the sampled variables should result in a smaller weight for the full sample.
Rejection sampling and likelihood weighting are two valid sampling methods, but they generate full samples independently of each other. The class of Markov Chain Monte Carlo methods assume that consecutive, full samples are (weakly) correlated with each other. Gibbs Sampling is the most well known of these samples. Given , it goes through each variable one by one and generates a sample for variable in the th element by using the conditional distribution
Thus, it relies on the newly generate samples for the first variables, but for the remaining variables, it uses the values of the previous sample. Hence the correlation between consecutive samples.
But wait, in a Bayesian Network, we can say more! The probability of a variable, conditioned on all the other variables, is simply that conditioned on the Markov blanket of a variable, which consists of itself, its parents, its children, and the parents of those children! We need to make an important distinction: when we say that in Bayesian Networks, that is only because we list variables in a topological ordering. If the variable is conditioned on all other variables in the network, we can only simplify by eliminating variables that are outside the Markov blanket of a node. Precisely, Gibbs sampling would sample from the following distribution:
where denotes the set of children nodes of node .
Note: with evidence variables, we just don’t sample them in Gibbs sampling (which also applies to other sampling methods).
Putting all this together, what does it really mean when we generate full samples? How do these actually help us with a query? An example would clarify. If we are making the query , but this is not a nodeparent probability (i.e., it is not listed in the CPTs anywhere) we need to sample to figure out the distribution of . Thus, we would generate full samples. We can then compute the desired distribution by looking at the value of generated in all those samples that have and (i.e., that are consistent). In other words, it’s a maximum likelihood estimate.
What I Would Study Next
If I had all the time in the world to study graphical models, here’s what I would study next: the junction tree algorithm. This is an extension of variable elimination and the sumproduct algorithm in that it performs exact inference on general graphical models as efficiently as possible. To do that, it has to transform the graph into what’s known as a junction tree (hence the algorithm name). Unfortunately, that’s about all I know about the algorithm.

When people discuss Bayesian Networks, they almost always assume that nodes have a topological ordering. ↩

Sometimes, we relax the maximal assumption on cliques, such as when we discuss the SumProduct algorithm. ↩

In fact, even though splitting up the joint distribution for Bayesian Networks in terms of the product of nodeparent conditional probabilities makes more sense than whatever the heck is going on with potentials, it’s not at all clear that we are allowed to do that! But as shown in Michael I. Jordan’s notes, the technical conditions we assume are that we can split up the joint in terms of a product of arbitrary, nonnegative functions that act on a local set of nodes, in which if we sum up over values of , . To actually get our nodeparent formalism, we assume that the functions can take the parents as “input,” and by showing that the sum of the entire product with respect to all the variables is one, we can prove that the functions must exactly correspond to the conditional probability functions of given its parents! Weird! Of course, things are different in the undirected case, in which case it is easiest for us to simply abandon conditional probabilities altogether when figuring out how to decompose the joint. ↩

Ben Recht: “You know what it’s called? The SumProduct Algorithm. [Laughs] I mean, come on, can’t we come up with better names here?” ↩

What happens is that he assumes we will always be summing over a variable, but that we will repeatedly multiply an indicator function to indicate evidence, so is really , where is a fixed evidence value of . All this is really formal trickery. In practice, if we know a value is fixed, we just take the appropriate slice from the CPT; we would not sum up over its values if we know that the indicators will be zero everywhere. But understanding the notation is helpful to understand the rest of his notes. ↩

In that sense, potentials here do loosely correspond to probability distributions. In general, if there is an easy way for us to map potentials to probabilities, we do that since it makes understanding them easier. ↩

The reason why replacing sums with maxes is fine is because both of those operations are commutative semirings. ↩

Interestingly enough, factor graphs do not provide additional representational power, because adding more nodes can simulate the effect of factor functions (by acting as indicator functions), but Mike argues that this approach is artificial, which makes sense. ↩

Again, we will continue the assumption that we have discrete random variables. ↩

Michael I. Jordan seems to prefer the term realization. ↩
Notes on Exact Inference in Graphical Models
In an effort to help me understand certain topics better for my prelim exam next month, I thought I’d briefly write about a topic that I sometimes never feel like I understand completely: exact inference in graphical models. Given a graphical model, inference is simply the task of computing the probability , where is a set of query variables, is a set of evidence variables, and if we include another disjoint set of the hidden variables, then represents the entire set of nodes in the graphical model. In general, inference is intractable, but for now we’ll pretend not to worry about that by discussing two ways of doing exact inference: by enumeration, and by variable elimination.
Inference by Enumeration
Here’s an example of a Bayesian Network, i.e., a directed graphical model with seven variables. This comes from the spring 2011 final for Berkeley’s undergraduate AI course, and the reason why there’s one but six s is because the question using this graphical model wanted to emphasize how the represented a class variable. But it’s still a variable, just like all the s, so the distinction is not important now.
Suppose we want to compute the exact quantity of^{1} . The first key point we need to know is that this computation immediately reduces to the computation of the joint , which directly follows from the definition of the conditional probability. Once we compute that joint, we can then divide by . Or, better yet, since the denominator does not depend on the value we choose for , we can avoid computing it by determining while substituting in the possible states of . These values will give us the normalizing constant we need, because we will be computing a vector of probabilities (one element per assignment to ) and the normalizing constant is the sum of all those elements. The point of all this previous discussion is this: the computation of conditional probabilities immediately reduces to the computation of joint probabilities.
Joint probabilities are easier for me to digest. In the current example, the joint can be reexpressed (using the simpler form of omitting the capital variables) as
Notice that we split up the joint into the conditionals described by the network, and then pushed the summations as far right as possible. The notation means that if , we are determining , i.e., we sum over the possible states of . So, in the above algebra, we sum over the possible set of states for the variables other than and , which are assumed fixed, but we still denote their values and with lowercase variables. Sorry. As my former professor Ben Recht would say, “the worst part about this is the notation.”
We need to be careful to make sure that we are rearranging the sums and probabilities correctly. For instance, is all the way to the left because it only depends on , but so is ! The reason is, again, that and are fixed to those values, so there is no dependency over any other summation.
At this point, exact inference by enumeration simply means computing the quantity above from left to right. So we would first fix to a state, then compute , then loop over the values for , and so on. It’s basically like how we have nested for loops in computer programming. This will give us the exact values. Then we have our desired conditional determined up to proportion: . To get the exact value, repeat the computation for all states of .
Variable Elimination
Variable elimination is the same as inference by enumeration, except for two things: (1) that we compute right to left and that (2) the exact order of our summations will change based on our variable ordering. That’s it! The key advantage of variable elimination over enumeration is that variable elimination is a dynamic programming algorithm and reuses computation.
I’m not going to go through the entire variable elimination algebra here, because the notation is tedious and we will do an example later, but here would be the first step. In the math above, we would transform into an intermediate factor , where the subscript 6 denotes the variable we just eliminated, and indicates the variable it depends on (it does not depend on the “variable” because it is fixed, but we are still summing over values of )^{2}. Then we repeat the process by creating more intermediate factors by going right to left. If we had to redo a lot of computations in enumeration, then variable elimination will save us lots of computation time.
Some notes:

It is possible to view variable elimination in a graphtheoretic way on undirected graphs. Here’s what happens: for a Markov Random Field, we use the graph directly, and for a Bayesian Network (like the one above), we would first moralize the graph by iterating through each node in the graph, connecting its parents, and then dropping the orientation of its edges. Great, so now we have an undirected graph – what now? First, we need a variable elimination ordering where the query variable ( in the above example) is listed last^{3}. Then, for each variable in the ordering, we eliminate it. For a variable , this means – graphically – that we would connect all neighbors of with an undirected edge (this forms a clique!), then eliminate from the graph. We repeat this process for all variables, until we get to our query variable, upon which time we will know the value of interest for the probability.

The order in which we eliminate variables is crucially important. This is beyond the scope of this post, but the run time of variable elimination is dominated by the size of the largest clique formed in the graphtheoretic version I just described. This is formalized as the treewidth of the graph, which is defined as one less than the size of the largest clique formed, in the best possible variable ordering for the graph. Unfortunately, it is intractable to know the optimal variable ordering, but we can have heuristics.
I need to add a word of caution to the first bullet point above. There are different ways of interpreting variable elimination^{4}. Some sources do not include the query variables in the elimination ordering, because it is assumed fixed (and it is always last so why bother). Similarly, some would also not include the evidence variables ( in our case) because we are not “eliminating” them. To me, it is a little unclear how this works graphically. What happens when we eliminate a variable that is connected to an evidence or query variable? Do we have to add edges between pairs of nodes where at least one is a query or evidence node? Do we eventually delete the evidence nodes? (We would never delete the query one because it is always last and by the time we finish, that node is the only thing left in the graph.) For now, my interpretation is that I’ll keep the query and evidence variables in the graph^{5}. If we eliminate a neighbor, we just don’t add edges to those nodes, because there is no “interaction” with those variables – again, the query and evidence values are fixed. If we end up with no edges towards one of the evidence variables at any point in the query, then I guess we can finally remove the node. My definition of elimination means that we can only “eliminate” a variable that corresponds to one of the summations in the above algebra.
Variable elimination – like inference by enumeration – takes exponential time in the worst case, based on the size of the conditional probability tables of the graphical model. In practice, as long as a graph of interest has a small enough treewidth (which usually means up to three), we can apply the algorithm. In a special class of graphs known as polytrees, inference takes (gasp) linear time!
With that said, we now turn to an example to apply our knowledge.
Example
Now let’s get back to that original question on the spring 2011 Berkeley AI final exam. I find practice exams (with solutions) to be among the gold mine for learning. Sometimes I learn more from practice finals than by reading an entire textbook! So it’s worth the time to go over the questions. The solutions key is already online, but for the variable elimination questions, I will go over the algebra and discuss the answers in more depth.
Here’s part (a):
Suppose we observe no variables as evidence in the TANB above. What is the classification rule for the TANB? Write the formula in terms of the CPTs (Conditional Probability Tables) and prior probabilities in the TANB
This isn’t really graphicalmodel related, because it’s simply . We could reexpress with the other six variables if we have the patience to write out six s.
Here’s part (b):
Assume we observe all the variables in the TANB above. What is the classification rule for the TANB? Write the formula in terms of the CPTs and prior probabilities in the TANB.
Now we really do have to write things out explicitly, because the classification rule is , with all variables fixed (except for , but we are fixing it here when we have to try different values of ). Here’s the joint I wrote earlier but will repeat here:
.
We will have to use this joint in part (c), which finally introduces us to variable elimination:
Specify an elimination order that is efficient for the query in the TANB above (the query variable should not be included your ordering). How many variables are in the biggest factor (there may be more than one; if so, list only one of the largest) induced by variable elimination with your ordering? Which variables are they?
Now things get interesting. How do we determine an efficient variable elimination? The idea is that we come up with an ordering, then see the s we get, and make sure that the largest function is “not too large.”
A useful first step that I use is that, after moralizing the graph, I see if there is any variable I can eliminate that will not create any additional edges. First, I notice that the variables and both have the property that their parents are connected in the moralized graph. Thus, removing those two variables should not add new edges, and so the largest clique will remain small. The clique will be of size two, as we are not counting the evidence or query variables.
It then remains to determine an ordering for . For that, I would look at the moralized graph with edges removed or added (none added in this case so far), and then try and see if I could eliminate nodes that would involve creating as few new edges as possible. The following picture shows my drawing of the variable elimination, because it would have taken forever to typeset this in LaTeX using the tikz library. Spoiler alert: I’ve already done the full elimination. I will describe why it makes sense to do this shortly.
Algebraically, this corresponds to the following^{6}, a righttoleft evaluation where we start with the full joint and then shift the summations and probabilities around:
Notice that indeed, it makes sense to get rid of and since they are simple cases that sum away to one. In fact, the solutions manual says that these variables should not even be included in a variable elimination ordering.
Next, we have to consider some combination of and to eliminate. The ordering here does matter! Here’s why: both and are connected to one other variable () and the two other nodes that represent fixed values. But is connected to two other variables! So if we tried to eliminate first, we would create an edge between and before removing from the graph, creating a factor of size three. Algebraically, this is:
Notice what happened! The function has two arguments, which is one more than anything we did in the optimal ordering above, which was .
Thus, the elimination ordering must have one of or . Then, we can do . In fact, after we pick the first of or , it doesn’t matter what order the last two are eliminated. Remember that we are not including here because it should technically be a fixed variable: we want to pick different values to run variable elimination.
The size of the largest factor here should be two, again, assuming that we do not count the fixed variables of or in these cliques.
Whew! Hopefully all the above analysis is clear.
To my delight, variable elimination continues in part (d):
Specify an elimination order that is efficient for the query in the TANB above (including in your ordering). How many variables are in the biggest factor (there may be more than one; if so, list only one of the largest) induced by variable elimination with your ordering? Which variables are they?
Unfortunately, the situation is not as easy as it was last time, because now we are forced to generate an function that will have two arguments, so the largest factor size is three. Also, note that now we want in the ordering. I’m not sure why this is the case since was not included in the last part, but it doesn’t make much of a difference because we know query variables should always be last, so will be last in the ordering. Now we will determine the other five variables (this excludes because it is fixed).
As usual, first we check if there is a way we can arrange the sums so that a variable can be summed out as one. Looking at the moralized graph, we can still remove as we did last time, for the same reasons. Thus, our probabilities would start out as:
Unfortunately, looking at the resulting (moralized) graph without (it’s the same as the first graph listed in my image above), there is just no way to eliminate one of without adding an edge. We can no longer treat as a fixed variable.
Here’s one possible ordering: . The algebra is as follows:
Notice that the largest factor size will be three, which here was created twice, once with , and the other with . Notice that if we had tried eliminating (or ) right after , then we would have created a size four factor/clique involving ! Again, all of these factor computations assume that fixed variables (query or evidence) do not count.
We gain a little more insight about variable elimination with part (e):
Does it make sense to run Gibbs sampling to do inference in a TANB? In two or fewer sentences, justify your answer.
Gibbs sampling is an approximate inference algorithm, which we use if exact inference is intractable. But here? Come on, we only have seven variables, and the treewidth of this graph is small; even for the query in part (d), our largest factor was of size three, so the clique would be size two. There is no reason to be approximate here.
The remaining parts do not have much of an emphasis on variable elimination, so I’ll just refer you to the exam and solutions key if you want to take a look.
Anyway, those are my thoughts on exact inference in graphical models, with an emphasis on enumeration and (especially) variable elimination.

As always, we need to be careful of how we write down probability notation. For now, I will attempt to use the notation that is common in Berkeley, where (or, even simpler, ) represents the probability that random variable takes on the value , and represents the entire probability vector for , i.e., is not a fixed quantity, unlike the lowercase version. We will assume that our random variables are discrete. ↩

Actually, by definition of a probability distribution, but let us pretend not to know that. ↩

A cautionary note: here, we are assuming that a query will only consist of a single variable, whereas in the general case, it can obviously be a set of variables. For now, just pretend that all queries involve a single variable. Fortunately, this is what happens in most textbook descriptions of variable elimination, for simplicity. ↩

I am following notes from Michael I. Jordan’s notes on probabilistic graphical models, and he does not seem to make special cases for evidence and query nodes, probably out of simplicity. ↩

An interesting side note: I think it may actually make sense to remove those nodes from the graph before elimination, because one might consider them “already eliminated.” It might be worth thinking about this more later. ↩

I hope you all love the underbraces there. Seeing that pretty math once again reminds me of the joys of LaTeX. ↩
The Impact of Same Sex Marriage Studies
In a continuation of my post regarding the same sex marriage ruling, I’ve been thinking about the potential impact of longterm same sex marriage studies. The kind I’m thinking of would be those that analyze the childhood and adult lives of people raised by two legally married adults of the same sex. While most people opposing same sex marriage do so on religious grounds, a substantial fraction instead argue that every child earns a mom and a dad. To strengthen that argument, these people could turn to longterm studies showing that children raised by two moms or two dads are “worse off” than children raised in traditional families. To precisely define “worse off,” we can look at factors such as overall happiness, overall health, future income, or a mix of these.
It is admirable to use research studies to back up one’s point (politicians, pay attention!), but with regards to the ones I’m thinking about now, I don’t believe such studies even exist. The issue of same sex marriage has only recently been at the forefront of public debate, so any longterm study would really be a shortterm study. Second, we also have to consider that gay adults who marry an oppositesex adult (perhaps under pressure) would likely not feel the same kind of affection as is common in parents of traditional families. I can’t imagine that a child could feel comfortable if his or her parents do not feel true affection towards each other.
Throwing those scenarios out the window, suppose we did have studies that showed negative impacts of same sex adults on their children. Even then, I’m not sure this should be a reason to invalidate same sex marriage. Here’s why: we get in a slippery slope of what we should allow for marriage. Without even doing any research, I can probably tell that children raised by parents who collectively earn $40,000 a year will be worse off than those raised by parents who earn $400,000 a year. Likewise, I can also probably find studies showing that children born to two black parents (of opposite sex) will be worse off than children born to two white parents (of opposite sex), even if income levels are the same. Then we would have to raise uncomfortable questions, such as whether we should have an income cutoff level for marriage. I’m not sure this is desirable, hence why even if studies show that it’s better for a child to be born in a traditional family, that’s not something we should use to prohibit same sex marriage.
The Same Sex Marriage Ruling, and a Paradox
Yesterday represented a historic moment for America as the Supreme Court legalized same sex marriage nationwide. I’m happy and proud at this result, because I supported same sex marriage and gay rights ever since I first learned about the issue. This was back in 2007, before prominent Democrats such as President Obama and the soontobe 2016 presidential candidate Hillary Clinton explicitly pledged their support.
As I ponder about the ruling and its various consequences, I’m noticing from Facebook and other sources (e.g., Scott Aaronson’s blog) that support for same sex marriage is practically universal among computer scientists in academia. To this day, I have yet to have one come to me stating that he/she opposed same sex marriage.
Yet this signals an interesting paradox.
What’s one of the biggest concerns regarding diversity in computer science? That there are too many Caucasian (and Asian) males^{1}!
And what’s one of the most notable characteristics regarding the Republican party — now infamously known as the antigay party^{2}? That there are too many Caucasian males^{3}!
So how come academics support same sex marriage?
I am a biracial, White and Asian male, and view myself as a moderate Democrat, so I am probably one of many examples of this paradox. Perhaps I can offer some opinions on why this is present:

Academia is liberal. This is not controversial, with notable political figures stating that universities are liberal bastions. As Michael Bloomberg commented in his Harvard 2014 commencement speech, ninetysix percent of all faculty campaign donations for the 2012 U.S. presidential election race went to Obama^{4}. Computer science just happens to be one subfield of academia, and there is no obvious reason why we should be more liberal or less liberal than other fields.

Computer science is also a subfield of, well, science, and being a welleducated scientist is inversely correlated with religious fervor (and positively correlated with athiesm), which is then positively correlated with support for same sex marriage. Richard Dawkins, in his thoughtprovoking book The God Delusion, eloquently dissects these observations and their subsequent consequences. By the way, I highly recommend his book.

Here’s a reason that’s specific to our field: one of the founders of computer science was Alan Turing, who was arguably one of the most important gay figures in history^{5}. His story — that of being the most important British codebreaker during World War II, one of the pioneers of comptuer science … and being prosecuted for displaying homosexual behavior in private (really?), and then committing suicide — is heart wrenching to digest. The Imitation Game, while not the most factually accurate account of his life, shows how our opinions of homosexuals has changed over the past few decades. I don’t think it’s a coincidence that some prominent Republicans who support same sex marriage have a gay relative^{6}. Perhaps computer scientists feel an obligation to respect the father of the field.
There’s a lot more that I’d like to cover, but for now I’m taking pleasure in the current ruling and thinking about the consequences. Hopefully we’ll see other countries continue to follow suit. I’m really wondering about what will happen to Japan, the country of origin for my father’s family. There’s a really nice map of LGBT rights by country or territory on Wikipedia, but there’s not a whole lot of dark blue (I think that’s the color for marriage … I’m colorblind) by Asia. I don’t know why Asian countries seem to lag behind the curve in gay rights. Still, as I look at how much attitudes have changed in recent times, perhaps it’s not too farfetched to suggest that within fifty years, same sex marriage will be legal in Japan (as well as China and South Korea). Actually, one might be able to make a reasonable argument for every country except North Korea and the Middle Eastern ones.
We’ll see what the future holds.

The issue of diversity in computer science and other STEM fields has been welldocumented, and a quick Google search will lead to articles such as this one. ↩

And yes, Governor Jindal, staying firm against gay marriage will only continue to damage the image of the Republican Party and deter young voters like me. Ironically, Governor Jindal is IndianAmerican, and Asians tend to vote Democratic. ↩

The Pew Research center is one possible source for learning about the breakdown of party affiliation among various demographic categories. ↩

By the way, Michael Bloomberg also delivered the 2014 Williams College commencement speech, so I saw him in person. He did not mention the issue of liberal academia; instead, he talked about cracking down on the illegal gun market. ↩

Seeing lists of important gay people like these perplexes me. These lists should only contain people whose sexuality is known without a doubt. Alan Turing fits this criteria; guys like Leonardo da Vinci (really?) do not. ↩

I can immediately think of several right off the bat: Rob Portman, Charlie Baker, and Dick Cheney. ↩
Reading Russell and Norvig
I had previously mentioned that the classic AI textbook by Russell and Norvig (2010) was fairly easy reading compared to most computer science textbooks. Now that I’ve recently gone through the first half of the book (which is about 500 pages) in the span of two weeks, I stand by my claim. Reading all these pages, however, does not necessarily mean that I would sufficiently absorb the material to the extent I wish, so in this post, I’ll give a brief overview of what’s covered in the first half of the book. The first two chapters serve as an introduction to AI, as a review of how the field came to be, and how we wish to design AI agents that are rational, which means that they make decisions that “make sense” according to some utility definition. There isn’t that much to see here.
Part II: Problem Solving
This part encompasses chapters 3 through 6 and is about problemsolving. Yes! Now we’re onto something that’s interesting, and something that’s also covered in every AI course. And every algorithms course, because what’s in chapter 3? Search algorithms on graphs!
Chapter 3: Solving Problems by Searching
The following list outlines the most important search algorithms to know:

BreadthFirst Search (BFS), a strategy where we start from a root node, expand it to generate its children, and then put those children in a queue (i.e, FIFO) to expand then later. This means all nodes at some depth level of the tree get expanded before any node at depth level gets expanded. The goal test is applied when nodes are immediately detected (i.e., before adding it to the queue) because there’s no benefit to continue checking nodes. BFS is complete and optimal, but it also suffers from horrible space and time complexity.

Uniform Cost Search (UCS) is like BFS, except that it orders the nodes to expanded in a priority queue based on some path cost function . One would want to use UCS rather than BFS in cases when a step cost (i.e., the cost of traversing from one node to another) is not uniform. Technically, this means without a uniform step function, BFS isn’t optimal, but usually we are smart enough to not apply BFS in those situations. UCS applies the goal test when a node is expanded, i.e., when it is pulled off the queue. This is later than when BFS would check. UCS is complete and optimal (so long as edge costs are nonzero, to prevent infinite loops) but can suffer from the same complexity issues as BFS.

Depth First Search (DFS) expands the deepest node in the search frontier, so it stores the frontier as a LIFO stack. Unfortunately, this means that DFS can traverse one really long path forever, without stopping to check back at other unexpanded noes near the beginning, so it’s clearly nonoptimal. The real savings for DFS comes with space complexity, because once a node has been expanded, it can be removed from memory once all its descendants have been fully explored.

DepthLimited Search is like DFS, except that nodes at a depth limit are treated as if they had no children. This can avoid DFS spiraling off in wild directions, but it also means that we will never reach the goal if the shallowest goal is beyond the depth limit.

Iterative Deepening Search (IDS) is another version of depthfirst search, and here we run depthlimited search multiple times, increasing the depth limit by one each time so as to gradually get close to the goal. It’s not as slow as one might think, because we would be repeating most of the initial node expansions, which have a lower branching factor.

Bidirectional Search means that we run two searches, one from the starting state and another from the ending state. The real challenge is how to combine the two search paths in the middle, and how to backtrack if necessary. In the queens problem, it’s not clear how to backtrack.

Greedy BestFirst Search uses a heuristic function (explained later) to choose which node to expand. This means that nodes are stored in a priority queue according to . This may seem a lot like UCS (and it is), but here, is the estimated cost of reaching the goal from node , not the overall path cost we have seen so far.

AStar Search fixes problems with greedy bestfirst search by supplementing with the path cost seen so far, . So here, the nodes would be stored in a priority queue based on , where the first term indicates a known cost so far, and the second indicates our estimate of the future cost. AStar Search is probably the most widely used form of bestfirst search.
For problems that use heuristic functions (i.e., in the above notation), one would like heuristics that are admissible and consistent, because then that would make AStar search complete and optimal. Admissible means that the function never overestimates the cost of reaching to the goal, and consistent means that the function obeys the triangle inequality^{1}. Every consistent heuristic is also admissible, but the reverse is not true. Probably the canonical example of a consistent heuristic in traveling salesmanlike problems is when we use the straightline distance from one city to another (well, assuming that our travel speed would be uniform across all possible routes).
If one has multiple admissible heuristics for a problem, and none dominates the other, then we should take the max of those for each node, . Do not take the sum if we want an admissible heuristic!
Chapter 4: Beyond Classical Search
Amusingly enough, this chapter is about trying to extend the previous one to bring it closer to the “real world.” Though admittedly, yes it is more like what most people would use. The first part of this chapter gives a very brief introduction to the field of optimization. Hillclimbing search is a search algorithm that attempts to move in the direction of increasing utility value, and is a more general version of the commonlyused gradientdescent algorithm, which is only applicable in continuous domains. The main problem with these local algorithms is that they can get stuck in local minima (or maxima, depending on whichever is most convenient for the problem description), so one should run the algorithm multiple times with random starting positions. Alternatively (or in addition), one can use simulated annealing. The way to think about how that works is to imagine a local minima problem where we have a small pingpong ball on a curvy, bumpy surface and are trying to get it to rest in the deepest crevice. The ball would obviously get stuck in a local minima easily due to gravity, so simulated annealing is like “shaking” the surface enough to shoot the ball out of a small valley, but not out of the actual global minima, which would be like a deep, giant pit. I find this analogy a lot easier to understand than most descriptions of simulated annealing, by the way.
The fact that we bring up gradient descent is interesting, because the search problems in Chapter 3 cannot handle continuous domains due to the infinite branching factor. Said another way, a human has an infinite number of ways to walk in a specific direction along the 360 degree circle; how would we design DFS to help a robot do that? One way to find optimal solutions in continuous spaces is to discretize the problem, so going back to my “human walking” example, we might limit the search directions to be anywhere from 0 up to 30 degrees, any where from 30 up to 60 degrees, etc. Another, of course, is to use the gradient and update the current state according to . We could also have constrained optimization problems, of which the best known and easily solvable problems are of the linear programming variety.
Remember that in Chapter 3, the environment is assumed to be fully deterministic and observable. It is also interesting to see how we would design an agent to search in spaces that have nondeterministic actions and if the agent can only have partial or even no (!) observations about the world. The key idea here is that we have to make use of an agent’s percepts that will help inform it what state it is in, so that we can say things like “if X happens, do Y, else do Z.” We can still design search trees and traverse them to reach the goal, but the trees have different flavors. In the nondeterministic environment case, we would need to have edges between a parent node and all its possible children nodes that could result. In the partial observation case, we would have the tree’s nodes be belief states, so each node is actually a set that contains the possible states the agent could be in. Gradually moving around in the search space might narrow down the set of possible belief states. The book’s vacuum cleaner example helps to explain how even an agent with no observations can still tell which state it is in given that it executes a specific sequence of actions and knows the consequences. In fact, having a sensorless agent can be advantageous in situations when it would be expensive to pin down an exact state, which is why doctors tend to prescribe a broad spectrum antibiotic rather than perform detailed analysis of a patient to decide on an incredibly specific drug.
As an interesting side note, I was reading sections 4.3 and 4.4 and noticed the similarities between the graphs provided here and the finite automata I learned from my undergraduate theoretical computer science course. We have notions of a state, a transition function, actions, and final (i.e., goal) states. Section 4.5, discussing online search, also uses these graphs. The nice thing about the book’s organization is that the search algorithms from Chapter 3 can be applied to the graphs on Chapter 4, along with additional problemspecific restrictions.
Chapter 5: Adversarial Search
We spent a lot of time in my undergraduate AI course on adversarial search. This is like what we consider in Chapters 3 and 4, except that the agent is no longer alone, and its actions are in conflict with other agents. The simplest abstraction of adversarial search is a twoplayer game involving one overall game score. The players are named MAX and MIN because they wish to maximize and minimize, respectively, the overall game score. In normal search algorithms, a player named MAX (who by convention tends to start these games) would just search for and form a sequence of moves that would reach a terminal state. Unfortunate, MIN can stop this in some cases. So the best algorithm for MAX to pursue is the minimax algorithm, because it minimizes the worstcase scenario^{2}. The easiest way to view this algorithm is to draw a graph of the game tree with various scores. Levels would alternate between a MAX player and a MIN player, so the MAX player should trace through the entire game tree and recursively backtrack to check and see the best score it can get on each node assuming that MIN plays optimally.
The problem with minimiax search is that nontrivial games have too many possible states; it’s actually exponential in the depth of the tree. By using alphabeta pruning to prune away branches that cannot possibly affect the final decision, we can cut the exponent in about half and still return the same solution used by minimiax. For any given state at any time in the search algorithm, alpha is the value of the best choice found along the path for MAX, and beta is the value of the best choice found along the path for MIN. Alpha starts as negative infinity and tries to go up, whereas beta starts as positive infinity and tries to go down. It’s easiest to see how this works by tracing through some game trees.
I remember these algorithms well because my undergraduate AI courses used the Berkeley Pacman assignments^{3}, which involved heavy use of minimiax search and alphabeta pruning. I remember that our problem involved four agents: Pacman (us) and three ghosts who wanted to eat him. With more than two players, we can associate a vector of values, and I think that’s what we did in the assignment, since the description says:
Now you will write an adversarial search agent in the provided MinimaxAgent class stub in multiAgents.py. Your minimax agent should work with any number of ghosts, so you’ll have to write an algorithm that is slightly more general than what you’ve previously seen in lecture. In particular, your minimax tree will have multiple min layers (one for each ghost) for every max layer.
If you’ve checked the AI project description, you’ll also see that we only run minimax algorithms to a limited depth, sometimes as small as just two layers. This is necessary due to the exponential explosion in the number of states in the Pacman maze. Another way to speed up the searching (but again, at the cost of optimality) is to treat nonterminal nodes at a given level as terminal nodes, and create a heuristic evaluation function for their values. (Yes, this is very similar to Chapter 3 material!) After all, this is what humans do when they play games. I can’t remember 20 moves ahead in a chess game, but I can reason that moving my queen to capture an opponent’s queen, while not threatening any of my pieces in the process, will have a higher utility for me.
One can also use minimax algorithms with games involving chance, which means that game nodes have chance nodes in addition to the normal MAX and MIN nodes. To make correct decisions here, we have to change our analysis to consider expected values.
Chapter 6: Constraint Satisfaction Problems
Now we’ll switch gears and focus on problems that have more sophisticated notions of a “state.” The reason for doing this is that algorithms like DFS, BFS, etc., assume that states are just black boxes. There is no domainspecific part of those search algorithm to those problems^{4}. With constraint satisfaction problems (CSPs), we represent each state as a set of variables , and a problem is solved when each variable has a value that satisfies all the constraints imposed from the states and problem formulation. The example used in the book is about coloring the seven regions of Australia so that no two bordering regions have the same color. To formulate it as a CSP, we
 define seven variables to be the seven regions
 define the domain for each variable, which consists of three colors for us
 define the constraints, which means listing all the color inequalities from bordering regions
Just to be clear, why do we want to use CSPs? Here are a few reasons:
 It is nice to have a single solver for a CSP. We can then solve a problem by converting it to a CSP, and then running our CSP solver. This is what a lot of theorists do when they reduce problems to known ones.
 There is no need to develop a detailed, problemspecific heuristic.
 CSPs can eliminate large portions of the search space all at once by quickly identifying variable assignments that violate constraints.
It’s worth discussing that last point in more detail. In the search problem of previous chapters, our search algorithm can search. With a CSP, we can perform inference called constraint propagation, which uses the constraints to reduce the number of legal values for variables. As the book delightfully points out, Sudoku is a problem that has “introduced millions of people to constraint satisfaction problems.” Constraint propagation is obvious here: if I see a column of variables that has all values other than 3 and 7 filled in (i.e., two empty squares), I can identify one of those spots that I want to fill in and constrain the number of possible variables from nine to two. If I then see that the square coincides with a row that already has 3 in it (but not 7, unless something went wacky), then that further constraints the choice of my variable to be 7 … and I will obviously put 7 there.
But while Sudoku problems can be solved by inference over constraints, sometimes we just have to search for a solution, and here is where backtracking search comes into play. This is a depthfirst search algorithm that goes down the tree assigning values to variables, and if it reaches a point where a variable has no legal values left to assign, then there is clearly no solution, so it backtracks to previous variables to try and perform different assignments. The order that we assign the variables does not matter, which helps to cut down on the branching factor.
To make backtracking search more efficient without using problemspecific knowledge, we should decide on solid heuristics for the following:
 What is the order in which we should choose to assign variables, and in what order should the possible assignments be done?
 What inferences should be performed at each step in the search?
 When the search arrives at an assignment that violates a constraint, how can we avoid repeating this failure?
For choosing the variable ordering, one way is the minimumremainingvalues (MRV) heuristic, or choosing the variable that has the fewest legal values, because then we can detect failure quicker. When we do choose a variable, but have to assign it from the list of possible values, it actually makes sense to follow leastconstrainingvalue heuristic, or choose the variable that rules out the fewest choices for the neighboring variables in the constraint graph (this is a graphical representation of the CSP) because it allows the possibility of more solutions down the road (ideally). So, most constrained when choosing a variable, and least constrained when assigning that chosen variable.
To perform inference, we can do forward checking after we assign a variable. This establishes arc consistency among adjacent variables in the constraint graph by iteratively updating constraints on those variables. However, this will fail in simple cases such as after we have assigned a variable a color in a problem where we have to twocolor the graph, because forward checking can’t reason about arcs that don’t directly include the currently assigned variable.
When we violate a constraint, we can backtrack one step up in the DFS tree like normal DFS, but that tends to work poorly because if we have an inconsistency, then we may have made a mistake much earlier in our sequence of variable assignments, so we want to backtrack far up in the tree beyond the most recent decision point. We can design a backjumping method by maintaining conflict sets for each variable, or in other words, a set of assignments that are in conflict with a variable assignment. Then the backtracking process would go back to the most recent variable assignment in the conflict set. However, forward checking already supplies the conflict set (check this yourself!), and so “simple” backjumping as previously described is redundant in a forward checking search or a search that utilizes arc consistency measures.
Instead, we can use the more sophisticated conflictdirected backjumping. Instead of backjumping once we detect a failure based on conflict sets, we should backtrack all the way before that to the point where the branch “gets doomed.” Clearly, this is a more challenging task, and we do this by redefining what it means to be a conflict set: for a variable, its conflict set is the set of preceding variables that caused this one, together with any subsequent variables, to have no consistent solution. These conflict sets are computed by an ingenious method of “absorbing” other nodes’ conflict sets.
Once we have our constraint graph, we can also apply some local search techniques from Chapter 4 (e.g., simulated annealing) to CSPs.
The previous stuff is very general, but honestly, if you look at the problem and can figure out something from it that is “obviously” going to make the problem easier, do it! In the Australian color mapping, Tasmania was not connected to the mainland, so it’s obvious that it shouldn’t have been part of the original coloring problem at all! Thus, splitting the graph into connected components would have been a smart tactic. Another way to make a problem easier is to reduce their constraint graphs to trees, because any treestructured CSP can be solved in time linear in the number of variables. We can assign values to variables so that the remaining ones form a tree, or we can do a more sophisticated treedecomposition, where nodes are now a set of variables, and variables can be part of multiple nodes. Each node represents its own subproblem.
Part III: Knowledge, Reasoning, and Planning
This part of the book is a little dry, and is about how one can design “languages” or various formalisms for agents to help them reason and plan about the world by extracting from a knowledge base. In my undergraduate AI course, we barely covered this part at all, and it was only towards the last week of class, when attendance was half the normal level because we didn’t have a final exam. I’m not sure how important this part is to AI research nowadays, since AI tends to be synonymous with machine learning these days. But maybe in some parts of robot motion planning?
Nevertheless, I still decided to read the entirety of it as there might be some important stuff here.
Chapter 7: Logical Agents
In which we design agents that can form representations of a complex world, use a process of inference to derive new representations about the world, and use these new representations to deduce what to do.
This somewhat boring chapter^{5} introduces the class of logic known as propositional logic, which lets agents represent the world through a series of statements and provides inference techniques to make conclusions. This is an upgrade over the agents in previous chapters. Why? When we tell Pacman to perform DFS to determine where it should move in the game, Pacman doesn’t really know anything about the game. A human can deduce a number of facts from the world, such as that Pacman should avoid going towards dead ends if a ghost is behind it and there is no powerup available there, but to the point of view of the DFS search agent, that knowledge is irrelevant. To say it another way, search agents only know the world in a very narrow, inflexible sense, and they can’t make real conclusions. They cannot reason. Constraint Satisfaction Problems alleviate this knowledge block by changing the representation of states from atomic to a set of variables, which allows for more efficient inference techniques (arc consistency, etc.), so there is a little bit of reasoning going on. Here, in Chapter 7, we take another step by representing the world not through a set of states and variables, but through logic. Remember that throughout this chapter (and the subsequent chapters), the overall theme is representation.
A few terms are in order to review:
 A knowledge base (KB) is the central component of our agents and will contain all the set of sentences (each represented with a specific syntax) that are known to the agent. A knowledge base is monotonic if the set of entailed sentences only increases as new information is added. Otherwise, it would be like the model is changing its mind.
 But wait, what is entailment? It is the idea that a sentence follows logically from another sentence. By writing , we state that entails , so that in every model in which is true, is also true. The relation implies that is a stronger assertion than (check this yourself).
 An inference algorithm is one that uses existing logical sentences and derives logically valid conclusions from them. If we have in our knowledge base that and then we should be able to make the conclusion somehow that . Algorithms that are sound derive only entailed sentences (this is a good thing), and algorithms that are complete can derive any sentence that is entailed from the KB, so complete algorithms are also sound. The slowest complete inference algorithm (assuming finite spaces) is modelchecking because it enumerates all models to check for entailment. This is not a scalable solution.
Propositional logic includes the following:
 atomic sentences, which consist of one symbol
 not connectives () to negate an expression
 and connectives () to join two expressions together (producing a conjunction)
 or connectives () to join two expressions together (producing a disjunction)
 implies relationships:
 if and only if relationships:
The semantics of these relationships are what one would expect, i.e., directly based on your discrete math or math logic class.
How do we use these facts to perform inference, and to do it efficiently^{6}? In other words, the ultimate question is that we want to decide if (logically equivalent to ) for some sentence . For this, we do some theoremproving. An important rule is Modus Ponens, which states that whenever is true, and if is true, then has to be true. (This makes sense because is only false if were true but false.) There are a few others, such as andelimination, which reduces to (without loss of generality) , but I generally apply these rules by directly appealing to what I remember about logic, rather than trying to remember rule names and their exact syntax. This must be why I hate resolving logic by hand.
A rule like Modus Ponens is sound, but incomplete. For a complete rule, we want to use resolution^{7}, which simplifies our problem by eliminating clauses that resolve with each other and don’t contribute to the resulting truth values. If we have , then we can simplify the sentence to be because are complimentary, so they cannot both force their respective clauses to be true. This is resolution. It applies to pairs of arbitrarily long clauses ANDed together. The key fact is that a resolutionbased theorem prover can, for any sentences and in propositional logic, decide whether . Why?
 Every sentence of propositional logic is logically equivalent to a conjunction of clauses, or said another way, every sentence can be converted into CNF form. This is important for resolution because it relies on there being a disjunction of literals. (Again, a sentence is in CNF form if it is an AND of ORs, and a clause is a disjunction of symbols.)
 Resolutionbased theorem provers work by using contradiction. To show that , we show is unsatisfiable. Starting with a sentence in CNF form, we apply the resolution rule to pairs of clauses to produce (potentially) new clauses. We continue with this until there are no new clauses that can be added ( does not entail ) or if any two clauses resolve to yield the empty clause ().
 Termination of the above algorithm follows due to the finite amount of symbols in the knowledge base, so long as useless literals such as are removed throughout the clause formation process. The proof of completeness for resolution is the ground resolution theorem.
While resolution is complete, sometimes we do not need its full power, or it is too slow. A Horn clause is a disjunction of literals of which at most one is positive, and if our clauses are of this form, we can perform more efficient inference using forwardchaining and backwardchaining algorithms. Forwardchaining means we start with the known facts and try to draw conclusions (e.g., using Modus Ponens) and propagate inference through the ANDOR graph. Backwardchaining does this in reverse. These algorithms decide entailment in linear time. And they are easy to describe to humans. Yay. Of course, we need Horn clauses for these to apply.
Moving away from resolution and back to model checking (remember how inefficient that is?), we can devise several heuristics to improve model checking, such as backtracking search and hillclimbing search. Backtracking search is a depthfirst enumeration of possible models with early termination, pure symbol heuristics, and unit clause heuristics. Hillclimbing search is a seemingly crazy way of doing inference. It randomly picks an unsatisfied clause and flips a symbol in it. Obviously, this may go on forever if we get unlucky in our draws, but if we do get a solution, then we know for sure that a solution actually exists!
Chapter 8: FirstOrder Logic
To design an agent based on propositional logic, as in the Wumpus world, one has to perform cumbersome steps to take care of variables representing the same world object, but at different times. The next upgrade of logic into what is known as firstorder logic will alleviate us of this nuisance because of existential () and universal () quantifiers. The following rule:
means that “For all , if is a king, then is a person.” More formally, firstorder logic assumes that the world now consists of facts, objects, relations, and functions, while propositional logic only assumes the world consists of facts (or propositions). The syntax terms are that constant symbols represent objects, predicate symbols represent relations, and function symbols stand for functions. Functions are a special type of relation where there is only one value for an input (which is the standard way we think of functions). In the above rule, is a variable; terms with no variables are called ground terms.
A model in first order logic consists of not only objects, but also various interpretations of each predicate and function. If our world consists of three terms and , and two objects, there are multiple ways we could map those terms: all of them could mean the first object, or could mean the first and and could both mean the second, and so on. Due to the amount of ways one could assign symbols to various objects or change the definition of a relation, modelchecking for entailment (which must apply to all possible models) in firstorder logic is much slower than it is for propositional logic.
Going back to universal quantification, we say that is true in a given model if is true in all possible extended interpretations constructed from the interpretation given in the model. This is a fancy way of saying that if our model has three objects (e.g., Richard the Lionheart, King John, and Richard’s Left Leg), then we better be able to plug in all three of those objects in as the variable in the rule and have those statements be true. For existential quantifiers, we just need at least one statement true in the extended interpretation for to be true.
Firstorder logic also includes equality. This is convenient when we need two variables to be unequal. Consider the following rule:
This is stating that Richard has at least two brothers. If we removed the part, then we could assign and to be the same person. But even if and referred to two different names (e.g., Daniel and Darius) then they could still refer to the same symbol/object. Thus, to make things easier for our brains, we will follow the uniquename assumption. We can also invoke the closedworld assumption in which atomic sentences not known to be true are false.
Chapter 9: Inference in FirstOrder Logic
One way to perform inference for firstorder logic is to convert a firstorder knowledge base to a propositional one, and then apply the propositional inference algorithms from Chapter 7. (Yes, I know you can tell that this will be crazily inefficient, but it might be useful to see how that works.) There are two techniques that help:
 Universal instantiation means we can substitute a ground term for a variable in a universally quantified rule. The rule is that if is true, then so is , where is the ground term^{8}.
 Existential instantiation means that in an existentially quantified rule, we can create a single new constant symbol that does not appear in the knowledge base. If is true, then so is , where is that new symbol. This new symbol is a Skolem constant and is part of a general process called skolemization.
These two methods help us to discard universal and existential quantifiers, respectively. (The former would require us to make many new rules, the latter requires only one new rule.) There’s more to this technique of propositionalization, but the point is that we can transform firstorder inference queries into propositional form while preserving entailment. Unfortunately, the question of entailment is semidecidable. We will be able to prove entailment for every entailed sentence, but we cannot refute entailment (in layman’s terms, “say no”) to every nonentailed sentence. The reason for this is that functions can construct infinitelymany groundterm substitutions, and we found out earlier that propositional inference algorithms (i.e., resolution) terminate precisely because we are guaranteed to have finitely many terms.
Despite this somewhat sorrowing news, there is better news to be had with regards to how efficiently we can “propositionalize.” For this, there are two techniques we can draw from: Generalized Modus Ponens and Unification.

Generalized Modus Ponens is an inference rule which states that for atomic sentences and , if there is a substitution such that , then if the following are true:
then the conclusion is that is true. So what does this mean in English? The conclusion is the sentence after we have applied the substitution that created equivalence between and . This is helpful in cases when the s are variable rules (e.g., ) and the s are knowledgebase sentences (e.g., ). By applying Generalized Modus Ponens with appropriate substitutions (like ), we can avoid the unnecessary extended interpretations of .
Before moving on, it’s worth connecting this rule to Modus Ponens from Chapter 7. It’s obvious that there are some similarities: we make the conclusion , which is the result of an implication . But why is this called the generalized version? It’s because we “generalize” this rule from propositional logic to firstorder logic by introducing variables and substitutions, which we know are not present in propositional logic. The book uses the term lifted for this, but it seems a bit arbitrary to me.

Our next rule relates to the previous one. Remember that we have to make sure that , but this will require a lot of comparisons. Unification is the hugelyimportant process of making different logical expressions have identical meanings. For two sentences and , unification returns , a set of substitutions for their variables to make them identical, if one exists.
Unification may require standardizing apart variables to avoid name clashes. Also, more than one unifications may be possible for a set of statements, so it is logical to pick the one that places fewest restrictions on the variables.
A naive (but sound) algorithm for unification recursively explores the expressions and builds up a unifier along the way, but has complexity quadratic in the size of the expressions being unified. More complicated unification algorithms can run in linear time.
Let us now briefly discuss three families of firstorder inference algorithms: forward chaining, backward chaining, and resolution. These should be familiar from propositional logic, since all we are doing here is extending them to fit in the framework of a firstorder logic system, but it is important to understand where exactly the extensions occur. We will clearly have to use rules like Generalized Modus Ponens and Unification here.
Forward chaining in firstorder logic applies Generalized Modus Ponens repeatedly to add more atomic sentences to the knowledge base until no further inferences are possible. This is similar to propositional logic, where forward chaining would repeatedly apply Modus Ponens. But remember how propositional forward chaining required Horn clauses (a generalized version of propositional definite clauses)? In firstorder logic, forward chaining requires firstorder definite clauses, which are disjunctions of literals of which exactly one is positive. Many (but all not all) knowledge bases can be converted into a set of definite clauses, which acts as a preprocessing step. Then, as stated earlier, we apply Generalized Modus Ponens, ideally until we’ve solved our query or reached a fixed point. Again, it’s similar to the propositional logic version, except here we include universally quantified atomic sentences. It is sound and complete, but entailment with definite clauses is semidecidable.
There are three sources of inefficiency in the naive forward chaining algorithm: (1) that unification involves searching through too many sets of facts on the knowledge base, (2) that the algorithm rechecks each rule on every iteration to see whether its premises are satisfied, and (3) that the algorithm generates facts that may be irrelevant to the goal.
Backward chaining in firstorder logic means we work backward from the goal, searching for substitutions and unifications to satisfy where the expression is already known. To find suitable substitutions for , which is a list of conjuncts which must all be positive, we may have to perform additional backtracking. The naive backward chaining algorithm is depthfirst search, so it suffers from some standard problems with DFS (e.g, lack of completeness) that forward chaining avoids.
Backward chaining is used in logic programming, a technology where systems are constructed and make conclusions using processes similar to what happens in firstorder logic. Prolog is an example of a logic programming language, but it is incomplete as a theorem prover for definite clauses. To avoid redundant computations, backward chaining should memoize solutions to subproblems.
We can extend resolution from propositional logic to create a complete inference procedure for firstorder logic. As before, the first step is to convert firstorder sentences into inferentially equivalent CNF sentences, which is always possible, and forms the basis for future proofsbycontradiction resolution procedures. This conversion process isn’t too difficult, though we need to eliminate existential quantifiers via Skolemization (briefly mentioned earlier). The process might involve creating Skolem functions to clarify variable dependencies.
The resolution inference rule is a generalization of the propositional reference rule to handle variables. Two clauses standardized apart (i.e., no shared variables) can be resolved (and therefore removed from proof as they don’t affect the outcome) if they contain complementary literals. In first order logic, complimentary literals are those in which one unifies with the negation of the other; remember that in propositional logic, we just had to worry about straightforward negations.
Resolution is refutationcomplete in the following sense: if is an unsatisfiable set of clauses, then the application of a finite number of resolution steps to will yield a contradiction. Resolution cannot generate all logical consequences of a set of sentences.
Chapter 10: Classical Planning
This chapter introduces a representation for planning problems in singleagent, deterministic, observable environments, which scales far better than the earlier search agents of Chapter 3 and the logical agents of Chapter 7. As a starting step to analyze and standardize language, AI researchers have introduced the PDDL language, the Planning Domain Definition Language. It can describe the things we need to define a search problem:
 the initial state, or states in general, which are conjunctions of ground (i.e., no variables or functions) atoms called fluents.
 the actions, which have variables, preconditions, and effects. They are only applicable in a state if the state satisfies the preconditions.
 the list of actions at each state, and the result of applying each action.
 the goal test, which is again a conjunction of literals, though these may contain variables. The goal is to find a sequence of actions that lead to a goal state.
There are several straightforward examples of PDDL in the book. They are intuitive descriptions of various problems written in a structured framework, though there is some trickery involved (e.g., with inequalities).
PDDL maps planning problems to search problems, and we can solve these with forward searching or (you must know what’s coming…) backward searching through states. Forward searching needs heuristics, because as stated earlier, the branching factor is too large to apply one of the Chapter 3 or 4 search algorithms directly. Backward searching avoids many irrelevant states, and PDDL makes it easy to represent the backtracking process, but requires sets of states and does not lend itself to easy heuristics.
To get an admissible heuristic, we can relax the problem to make it easier and apply the resulting solution to the original one. The corresponding search graph has nodes as states and actions as edges. Some ideas for heuristics:
 Ignore preconditions, so every action becomes applicable in every state.
 Ignore delete lists, applicable if all goals and preconditions are positive literals. This removes all negative literals from all actions, and the problem is easier now because no action will undo progress made by another action.
 To reduce the number of states, ignore some of the fluents.
 Assume subgoal independence, so if the goal is , take as the estimate the maximum cost over all , or sum up the estimated costs for each state (note: this is inadmissible).
A special data structure called a planning graph can provide better admissible heuristics than the ones previously suggested; we build the graph, and then search over it (this is called “GraphPlan” in the book). The planning graph is a directed graph of states, and is a polynomialsized approximation to the exponentialsized tree that consists of all the possible paths taken from the starting state, with the goal of finding a path to the goal state. It consists of alternating state levels and action levels . The state levels contain the literals that might be true (since it’s an approximation) at a given state^{9}, and the action levels contain the actions whose preconditions are satisfied by those literals in the previous state level. Literals that show up later in the tree (i.e., farther away from the starting state) are “harder” to achieve, so the true states that contain those literals are “harder” to reach.
A key property of state and action layers is that they contain mutual exclusion or mutex links between literals and actions, respectively. A mutex relation holds between two actions if they have inconsistent effects, interference, or competing needs. A mutex relation holds between two literals if one is the negation of the other, or if the pairs of actions that could lead to those literals are mutex.
Now that we’ve constructed a planning graph, how do we use it? As stated earlier, we can estimate the cost of achieving any goal literal as the level in which it first appears in the planning graph, i.e., the level cost. If the goal state is a conjunction of literals, which is the normal case anyway, then some ideas are:
 Take the maximum level cost over all literals. This is admissible.
 Take the sum over all literals. This is not admissible, though it might work if the individual literals are reasonably independent of each other.
 Take the level cost of the level in which all literals are in the planning graph, without there being any mutex relations between the two. This is admissible and also clearly dominates the maximum level cost heuristic because the level we return will have all the literals.
As an alternative, we can run the GraphPlan algorithm to search directly on the planning graph. The algorithm repeatedly adds levels to the planning graph, and finishes once all the goals show up as nonmutex in some possible state for a state layer. If the action and state levels do not increase, then the algorithm returns failure.
The downside of planning graphs is that they only work for propositional planning problems, though if we wanted, we could obviously convert a firstorder logic encoding of a plan into propositional logic. They also fail to detect unsolvable problems with threeway mutex relations but no twoway mutex relations.
There are a few other classical planning paradigms:
 We can treat this as a theoremproving problem by transforming the PDDL description into a form that can be processed by a SAT solver. This step involves propositionalizing the actions and the goal, as well as adding in more axioms to handle successor states and mutual exclusions.
 We can use firstorder logical deduction rather than PDDL. Rather than tie time directly to fluents, we can use situation calculus and create new rules to apply to our states. The downside of this approach is that it’s hard to get good heuristics.
 We can transform the problem of finding a plan of length as a constraint satisfaction problem (from Chapter 6), similar to the encoding for a SAT problem.
 We can also create partially ordered plans, which is useful with independent subproblems. We can create such plans by searching through the space of plans, rather than the state space. Unfortunately, it doesn’t represent states easily, and these fell out of favor (after the 1990s) in place of plans that search through states with strong heuristics.
Chapter 11: Planning and Acting in the Real World
This rather amusinglytitled chapter now brings us into the “real world” by requiring that our agent representation must handle not only planning, but also handle a changing environment. Here are some of the new assumptions we make in our world:
 We may have to deal with time and resource constraints.
 We may have to organize plans in a hierarchical fashion.
 We may have to handle nondeterminism and uncertainty in our environment.
 We may have to deal with multiple, competing agents.
Let’s briefly discuss how we design an agent to handle these four cases.
To deal with time and resource constraints, we augment the language of the problem formulation to include amounts for certain resources (e.g., means there are nine inspectors available in a car inspection problem), as well as Consume and Use keywords to indicate whether the resource is gone or available again after usage (e.g., inspectors would usually be available again after their usage). We can represent the problem with a directed graph that obeys the time/resource constraints, and then find the critical path through the graph, which is the path with the longest duration and thus is the “limiting factor” in the schedule. A heuristic to find the minimum cost path: for each iteration, choose the action with all predecessors satisfied, and which has the least amount of slack.
In order for AI systems to think like humans do, AI systems will need to make plans at a higher level of abstraction by forming hierarchies. The classic example is when we organize a plane trip to Hawaii. The highlevel abstraction is: take the BART to the airport, search for the gate, etc. Humans do not think: first, open the door carefully, then take seven paces to the right, then walk down this way for 500 steps, then put the ticket inside the BART gate, etc. That kind of detail would lead to too much combinatorial explosion in AI systems. So AI agents must use highlevel actions (HLA) that can possibly be refined later. One way to solve such hierarchy problems is to start with one or multiple HLAs that solve the problem, and then refine it (e.g., in a BFS fashion) until we get a sequence of primitive actions that accomplishes the goal. This can be substantially faster than normal BFS over the space of primitives. To get an even greater — potentially exponential — speedup in search, what we would like to do is only search through the space of HLAs, and once we find a sequence of HLAs that work, then we can refine that one plan into primitives, since we know it works. To do this, we set up preconditions and effects for each HLA, and the state space will be a set of fluents (as it was before in many areas of Chapter 10). We can define a search problem known as angelic search that utilizes reachable sets of HLAs. Pessimistic and optimistic reachable sets can prune away refinements that have no chance of reaching the goal.
Our agents will have to deal with nondeterminism and uncertainty in the environment. With no observations, we can perform sensorless planning. With partial observations, we can perform contingency planning, and for unknown environments, we can do online learning. Some material is similar to what was presented in Chapter 4, and the main difference here is that we have a far richer state representation (fluents rather than atomic) and thus we can represent belief states easily using length conjunctions (well, assuming that our belief states are 1CNF). It can be tricky to update belief states after an action. An example problem with fluents might be that we have to paint two chairs to be the same color, and a sensorless agent could solve the problem by just dumping a can of paint on both chairs, without knowing the color of the paint at all. A contingent agent can also solve the problem, and often does so more efficiently.
Finally, we can consider the multiagent case, either when one “super” agent controls multiple smaller agents, or when there are multiple, separate agents who only control themselves (and whose goals may be in competition with one another). If the agents are loosely coupled, it makes sense to decompose the transition model into independent subproblems to avoid an exponential branching factor.
Chapter 12: Knowledge Representation
In which we show how to use firstorder logic to represent the most important aspects of the real world, such as action, space, time, thoughts, and shopping.
Wait, shopping? All right all right, let’s see what’s in store here in the final chapter of the Knowledge, Reasoning, and Planning aspect of AI. The previous chapters have come up with the technology (e.g., firstorder logic and its various inference methods) for knowledgebased agents, but now, we have to figure out the content to put in an agent’s knowledge base.
I don’t think there is much material in this chapter that I need to know, and a lot of it is common sense. But here are the highlights regardless:
 Ontological engineering is the process of representing abstract concepts of events, time, physical objects, and beliefs in various domains. We clearly have to do something like this to design an agent! One way is to describe an upper ontology of the world by listing some general things first, and then moving to more specific items down the tree (this is what we do in object oriented programming).
 We need to represent the following: categories, objects, and events. We can represent categories by using straightforward predicates (e.g., the category of basketballs could be ) or by reification^{10}, a.k.a. thingification, which means representing it as an object . To reason about categories, we can use the graphically appealing semantic network framework, or appeal to formalism with description logic^{11}. Objects should be arranged in a hierarchy of categories with subclassing and inheritance, kind of like (again) how we do it in objectoriented programming. For events, rather than the situational calculus we saw earlier, we should use event calculus to deal with continuity. Event calculus reifies fluents and events.
 Sometimes, we may wish to represent mental beliefs by model logic rather than firstorder logic, because the former lets us take sentences as arguments, and allows us to represent a set of possible worlds of beliefs. The notation means that agent knows .
 At the end of the chapter, there is a shopping mall example, which I find to be amusing but not that educational.
Whew! Reading the above ten chapters, as well as Chapters 1 and 2 in the book, and Chapter 13 (which is about basic probability, nothing to see here!) brings me to the 500page mark out of 1000 pages. Now I only have 500 pages to go!

Said another way, it never overestimates the cost of reaching a given state. ↩

It is also possible that the MIN player is dumb and doesn’t play optimally, but the MAX player following the minimax strategy would perform even better if that were the case. ↩

These assignments are a great way to see more complicated applications of algorithms from the textbook. ↩

Various evaluation functions (e.g., heuristics) are of course domainspecific, but the point is that the search algorithms are not domainspecific. It’s worth thinking about this often. ↩

There’s also a nice Wumpus example in this chapter that makes it twice as much fun to read. That part is not dry. ↩

The book states that propositional entailment is coNPcomplete, so every known inference algorithm for propositional logic has a worstcase complexity exponential in the size of the input. Still, this is “only” worst case complexity. ↩

Technically, resoultion is complete if it is coupled with a complete search algorithm. ↩

The notation denotes the result of applying the substitution to sentence . The substitution rule should be of the form where is a variable and is a real term that we want to plug in. ↩

A literal cannot appear too late in a state level, because that would be like overestimating the cost of that literal and the states it belongs to, resulting in inadmissible heuristics. ↩

One of the advantages of reification is that by doing so, we represent what we want in terms of objects; then we can add an arbitrary amount of information about them. For instance, refiying events means we can add in as many descriptions about the event as we want by adding in more conjuncts. ↩

Representing categories these ways also helps us to establish default values for categories. Unlike in previous forms of knowledge representation, semantic networks and description logics make it easy for us to have exceptions for objects. For example, most tomatoes are red, but some can be purple. Thus, the category of tomatoes should have a default color attribute of red, with individual objects potentially overriding those values. The connections between this and programming languages is once again obvious. Also, note that if we allow overriding, then this violates monotonicity of the logic. Monotonicity means that if , then for any , . ↩
Advocate For Yourself
Advocate For Yourself
I remember hearing those words fifteen years ago when I was in elementary school. I was in a classroom where the few Deaf and Hard of Hearing (DHH) students of the school were bunched up to get tailored advice from staff members who knew sign language. The speech teacher who said those words was reminding us that as we get older, we would need to take the initiative to secure accommodations.
Fifteen years later, my mind consistently replays that phrase, and I am amazed at the importance of advocating for myself now that I successfully (by some standards) went through college and now have a “real” job. (I know being a graduate student doesn’t count, but please pretend it does.)
If I had to evaluate myself on my ability to advocate for myself on a scale of one to ten, where a one means that I’m so shy that I need my parents to conduct every form of communication, and a ten indicates that I’m so good that my inbox is stuffed with other DHH students begging me to advocate on their behalf … I’d rate myself a five. I’ll explain why.
Part of advocating for myself is, really, to state that I am deaf. This is obviously priority number one, since I have to clear that hurdle before getting additional benefits such as interpreting services. I don’t generally have a problem telling people that I’m deaf, because unlike some people with hidden disabilities who have great incentives to hide them, there is almost no reason for me to hide my deafness. Not revealing it puts me at an immediate disadvantage.
At the same time, I don’t want those words to be the first sentence that I’m telling people. This raises the key question:
When is the optimal time for me to tell people that I’m deaf?
I think the optimal time can be captured in a curve, and it would bear a shape like the hypothetical email productivity curve, with the xaxis indicating the time when I tell people I’m deaf (and make the usual requests, e.g., please talk to me clearly, turn off that blaring music, and please do not make me attend a seminar right away), and the yaxis indicating the overall joint utility that me and the other party gain.
Here’s why. At time = 0, equivalent to me telling someone I’m deaf immediately, I’ll have made things clear from the start, or at least more clear than it would have been had I not made that proclamation. That results in some utility for me.
But when two hearing people meet for the first time (e.g., during graduate student orientation), I can’t imagine that they talk about such personal things right away. They probably begin with their names, where they are from, their interests, and other smalltalk fodder. Plunging right into deafrelated topics means that we would talk about something deeply personal to me, but that other person would have no knowledge about it, and may be struggling to determine if his or her immediate questions are offensive.
This is why I generally like to start conversations about “normal” things. Then when the time is right, I’ll be at the top of the joint utility curve. That is when I will tell the other person that I’m deaf. He or she may or may not gain much utility as compared to time = 0, but I know that I will have much more utility at that time, which explains the rise in the curve.
The problem with my approach is that I really have to tell the person early, because the curve quickly levels off (or can dip below zero, indicating negative (!) utility). If I keep hiding my deafness, and only reveal it after the 347th meeting with that person, then I’ll be angry at myself for not advocating for myself early, and the other person will be incensed that I didn’t tell them why I missed some information during earlier, wasted meetings. Oops.
I wish I could say that I conveyed the information regarding my deafness at optimal times to everyone important in my life. Unfortunately, I do not, and there have been several unsatisfactory events in college and in Berkeley that I probably could have avoided had I made things clearer earlier. The classic example is when I show up to meetings with at least two other people. These are a problem for me even without background noise, since it is difficult to understand two people when they are talking to each other, rather than directly to me.
Hence, the five I rate myself.
To work on my internal advocacy rating, in the future, I will no longer agree to take part in a group meeting without me making it clear that I will need some assistance. It makes things so much easier in the long run, at the cost of a little initial awkwardness that I have to learn to ignore.
I Did Not Request ASL. I Requested Transliteration.
Before the start of the spring 2015 semester, I had a meeting with Randy Jordan, who works in Berkeley’s Disabled Students’ Program and specializes in providing services for Deaf and Hard of Hearing students. After a terrible first semester, I talked to him about having American Sign Language (ASL) interpreting accommodations for my classes, rather than captioning.
Randy smiled and agreed. In fact, he had already made the appropriate requests for the semester. But to my surprise, he also mentioned that he requested transliteration sign language interpreting, which I assume he meant to be Signed Exact English (SEE). He said:
I did not request for ASL. I requested sign language for transliteration. That’s based on my professional opinion of you. I don’t want you to be in a class and not understand what’s going on.
I did not respond, and our meeting concluded shortly after. It’s clear that Randy thought my sign language skills with regards to American Sign Language were not up to speed.
I’ve thought a lot about what he said to me. I’m not angry at all — Randy is a great guy. In fact, I’m happy he brought this up, because to me it’s reminder that my ASL skills are raw and unrefined. I am far better at reading, writing, and speaking English as compared to ASL. When Randy and I speak in our meetings, we usually sign. (He is the child of deaf adults.) During our conversations, Randy must have observed a tendency for me to lean towards SEE over ASL style.
Looking at my background, this shouldn’t be a surprise. While I learned sign language when I was two or three years old, I don’t know how much of it was formal ASL, or how much of it was just a sequence of memorizethissignthenmemorizethissign. I don’t remember formally studying ASL grammar, such as how one should manage facial expressions when signing particular questions.
As I progressed through my education, I also distanced myself from other deaf students (I’m excluding the hard of hearing ones now, since they usually had very limited skills). At the start of elementary school, I might have had multiple classes a day where all the students were deaf or hard of hearing. But as I got older, both (1) the frequency of such classes and (2) the ratio of deaftohearing students would jointly decrease. Then I went to college, where I was the only deaf student for four years, and now I’m in graduate school.
Who would I sign with? My brother, sometimes, but we don’t see each other that often now, and we were always sloppy signing to each other. It’s one of the interesting things about knowing someone for so long: even if he communicates so poorly, I can still understand what he’s said due to years of practice.
Throughout college, I did have interpreters who used ASL, and I think I understood them reasonably well (and my academic performance might offer supporting evidence). It’s clear that I am better at reading ASL than speaking it. Unfortunately, the only way I’ll be able to get better at my ASL is if I can practice with others.
Any volunteers?
Give Video Relay Services a Try
Video Relay Services is a federallysubsidized service that allows deaf and hard of hearing users to have conversations over the phone with the assistance of a sign language interpreter. If the deaf person uses video relay to call another person, then he or she will be calling using a computer or a television, and there will be a sign language interpreter shown on the screen who follows the conversation. The sign language interpreter signs what the hearing person says, and the deaf person can see the translation and then respond by communicating in English or sign language. In the former case, no additional action is needed from the interpreter, as the callee should hear the deaf person. In the latter case, the interpreter watches what the deaf person says, and then verbally relays the information to the other party. To the callee, it is like having a normal phone call.
This sounds like a tremendously beneficial service for deaf people, and I admit it: I should have been using Video Relay Services a long time ago. I signed up to use Video Relay in February, and have used it four times so far. Since it’s June now, that might not sound like a lot, but for someone who used to make about one phone call a year to a nonfamily member, it’s significant.
The reason why I took so long to embrace Video Relay had to do with initial perceptions. I learned about Video Relay in 2008 when a staff member from Sorenson VRS gave a presentation about it to the deaf and hard of hearing students in my high school. I soon had it installed at home, but probably made only three calls. At that time, I had to use a specialized television screen with a specialized camera and a complicated remote, and the video quality left much to be desired.
Nowadays, it’s much easier because there are applications that allow me to use Video Relay from my laptop. I use Video Relay like I use Skype: I open the software on my computer, sign in, make sure my camera is working, and dial the desired number. The software will not call the number immediately, because it first has to connect to an available sign language interpreter, who will then make the actual call to the callee. It should not take more than five minutes to connect to an interpreter.
As I explained earlier, one can use video relay by signing or speaking to the interpreter. I use the latter case, which is technically called Voice Carry Over. This is my preferred communication mechanism, because my English is better than my ASL, and signs can turn choppy and distorted across a monitor.
To get started with using Video Relay, I signed up with Z5 Desktop and downloaded their free application for my Macbook Pro laptop. I had to first verify my address and related information with a staff member via video relay. We talked for a little while (in sign language) and then he officially gave me the goahead to start using the service. The whole setup was much easier than I expected. Surprisingly, he never asked me for documentation regarding my hearing loss, and as far as I know, only deaf and hard of hearing people are allowed to use Video Relay.
I suppose the lack of accessible software was a valid reason for avoiding Video Relay Services, but again, I should have used it once I went to college and had my own laptop. I can remember many cases when a simple phone call might have made things so much clearer for me. Yet, I would often settle for wading though a mountain of documentation and sending emails with long turnaround times. As far as technical difficulties are concerned, I have not had any problems so far. My one concern with accommodating the communication needs for deaf people is that there are some who do not know sign language. They would still struggle in cases when people only give out a phone number for contact information. But for someone with my background, Video Relay Services takes care of a lot of my needs, and I am thankful for that.
Review of Computer Vision (CS 280) at Berkeley
Last semester, I took Berkeley’s graduatelevel computer vision class (CS 280) as part of my course requirements for the Ph.D. program. My reaction to this class in three words: it was great.
Compared to what happened in classes I took last semester, there were a lot fewer cases of headbashing, mental struggles, and nagging doubts in CS 280. One reason for this favorable outcome is that I eschewed captioning in favor of sign language interpreting, which is the accommodation type I’m most used to experiencing. What may have played an even bigger role than that, however, was the professor himself. The one who taught my class was Professor Jitendra Malik, a senior faculty member who’s been at Berkeley since 1986 and was recently elected to the National Academy of Sciences (congratulations). I realized after the first few classes that he really takes his time when lecturing. He talks relatively slowly, explains things at a high level, and repeatedly asks “Does this make sense?” He is slowest when dissecting math he’s scribbled on the whiteboard. In fact, I was often hoping he would speed up when he was strolling through basic linear algebra review that should have been a prerequisite for taking CS 280. If someone like me wants a faster class pace, that means everyone else must have wanted the same thing!
For obvious reasons, the slow lecture pace was wellsuited for the two sign language interpreters who worked during lectures. They sat near where the power point slides were displayed, which made it relatively easy to move my eyes back and forth across the front of the room. As usual, I hope no one else got distracted by the interpreters. Unfortunately, the seat next to mine (I sat on the edge of the first row, no surprise) would remain suspiciously empty for a long time, and would only fill up once all the other seats were occupied save for a few hardtoreach center ones.
I should warn future CS 280 students: this is a popular graduatelevel course, for reasons that will be clear shortly. The auditorium we had was designed to seat about 8090 people, and we probably had over 100 when the class began. It did eventually drop to 80 — aided by some forced undergraduate drops — but before those drops, the tardy students had to sit on the floor. This is a real problem with Berkeley EECS courses, but I guess the university is short on funds?
Anyway, let’s discuss the work. While the lectures were informative, they did not go in great detail on any topic. Professor Malik, when faced with an esoteric concept he didn’t want to explain, would say “but I won’t explain this because you can read the [academic] paper for yourself, or Wikipedia.” Well, I did use Wikipedia a lot, but the thing that really helped me understand computer vision concepts were the homework assignments. We only had three of them:

Homework 1 was a jackofalltrades assignment which asked us questions on a wide variety of subjects. The questions were related to perspective projection, rotation analysis (e.g., Rodriguez’s formula), systems of equations, optical flow, and the Canny edge detector, which we had to implement. I think it is too hard to implement the Canny edge detector from the original 1986 paper, and I’m pretty sure most of the students relied on a combination of Wikipedia and other sources to get the algorithm pseudocode. Overall, this homework took a really long time for me to finish (I blame the perspective projection questions), but we did have two weeks to do this, and we all got an extension of a few days.

Homework 2 was a smaller assignment that focused on classifying handwritten digits using SVMs (part 1) and neural networks (part 2). The first part was not too bad, as we just had to write some short MATLAB scripts, but the second part, which required us to use the opensource, Berkeleydeveloped caffe software, probably took everyone a longer time to finish. To put it politely, research software is rarely easy to use^{1}, and caffe is no exception. I could tell that there were a lot of headaches based on the complaints in Piazza! Oh, and I should also mention that caffe had a critical update happen a few days before the deadline, which broke some of the older data format. Be warned, everyone. To no one’s surprise, we all got an extension for this homework after that surprise update.

Homework 3 was another small assignment, and it was about reconstructing a 3D scene using points measured from multiple cameras with different centers. We had to implement a matching algorithm that would match the different coordinate systems to determine the true coordinates of a point. Our online textbook went through the algorithm in detail, so it wasn’t too bad to read the textbook and apply what was there (and possibly supplement with external sources). Unfortunately, this homework’s due date was originally set to be on the same day as the midterm! After some more Piazza complaints (none from me!), we all got yet another extension^{2}.
One thing I didn’t quite like was that Homework 1 took significantly longer than the second one, which took significantly longer than the third one. There wasn’t quite much balance, it seems. On the other hand, we could work in groups of two, which made it easier.
Aside from the homeworks, the other two aspects of our grade were the midterm and the final project. The midterm was inclass, set to be eighty minutes, but the administrative assistant was a little late in printing the tests, so we actually started ten minutes late. Fortunately, Professor Malik gave us five extra minutes, and told us we didn’t have to answer one of the questions (unfortunately, it was one that I could have answered easily). As for the midterm itself, I didn’t like it that much. I felt like it had too many subjective multiplechoice questions (there were some “select the best answer out of the following…”). I don’t mind a few of those, but it’s a little annoying to see thirty percent of the points based on that and to see that you lost points because you didn’t interpret the question correctly. The average score appeared to be about 60 percent.
With regards to my final project, I did enjoy it, even if what I produce almost always falls under initial expectations. I paired up with another student to focus on extracting information from videos, but we basically did two separate projects in parallel and tried to convince the course staff that they could be combined in “future work” in our fivepage report. What I did was take YouTube video frames from Eclipse, Excel, Photoshop, or SketchUp videos and trained a neural network (using caffe, of course) to recognize, for a given frame, to which movie it belonged. Thus, the neural network had to solve a fourway classification problem for each frame. The results were impressive: my network got over 95 percent accuracy!
The final project was also enjoyable because each group had to give a five minute presentation to the class. This is better than a poster session because I don’t have to go through the hassle of going to people and saying “Hi, I’m Daniel, can I look at what you did?” One amusing benefit of these class presentations is that I get to learn the names of people in this class; it’s definitely nothing like Williams where we are expected to know our classmates’ names. When I see names that are familiar from Piazza, I think: whoa, that was the person who kept criticizing me!
To wrap it up, I’d like to mention the increasing importance of computer vision as a research field, which is one of the reasons why I took this class. Computer vision is starting to have some lifechanging impacts to real life. It has long been used for digit recognition, but with recent improvements, we’ve been able to do better object detection and scene reconstruction. In the future, we will actually have automated cars that use computer vision to track their progress. It’s exciting! (One of my interpreters was terrified at this thought, though, so there will be an “old guard” that tries to stop this.) A lot of progress, as Professor Andrew Ng mentions in this article, is due to the power of combining the agesold technique of neural networks with the massive amounts of data we have. One of the things that motivated this line of thinking was a 2012 paper published at NIPS, called ImageNet Classification with Deep Convolutional Neural Networks, that broke the ImageNet classification record. It spawned a huge interest in the application of neural networks to solving object detection and classification problems, and hopefully we may end up seeing neural networks become a household name in the coming years.

Moses, for statistical machine translation, is also not an exception. It is extremely hard to install and use, as I discussed last November. ↩

If there had been a fourth homework assignment, I would have been tempted to say the following on Piazza: “To the TAs/GSIs: I would like to ask in advance how long the incoming homework extension will be for homework four?” ↩
The Joy of Talking To Others
I had a vision of what I wanted to be like before I entered graduate school. Some things have worked out, and others haven’t. One thing that hasn’t — and not necessarily in a bad way — is my changing opinion of how I want to structure my schedule so as to talk to others.
Originally, I wanted to be someone who could hunker down at his desk for sixteen hours a day and tenaciously blast his way through a pesky math or programming problem. I wanted to possess lasersharp, Andrew Wileslevel focus, and channel it to work on computer science all day without a need to have others hinder my progress with meetings and various requests.
That vision has not become reality. The key factor? I really want to talk to people.
For most of my life, I never viewed myself as “normal.” This was largely a consequence of being deaf and being isolated in social settings. But I am normal in the sense that I thrive on talking and socialization.
My isolation in recent years has made me hungrier and hungrier to socialize, and when I don’t get that opportunity and see people my age establishing new friendships on a regular basis, I relentlessly beat myself up for failing to take the necessary initiative. Is there something they do that I should be doing? Am I not painting the correct impression of myself?
As a result, sometimes those “uninterrupted hours” that I’ve gone through during work have really been “interrupted” by my brain^{1}, which is constantly telling me that I should socialize. Somehow.
My brain will often go further than that, in a peculiar way. I don’t know how common this is with people, but my brain is constantly creating and envisioning fictional social situations involving me. A typical scene will be me and a few other people socializing. Interestingly enough, I will be participating in these conversations much more often than is typical for me, and the other people will be more engaging towards me than usual. That’s it — those are the key commonalities in these scenes. I don’t know … is my brain trying to form what my hope would be for a normal social situation? Is it trying to compensate for some reallife deficiency? This kind of hypothetical scene formation, for lack of a better way to describe it, happens more often when I am in bed and trying to sleep. I will usually go through cycles of social scenes, with an intriguing rotation of settings and conversationalists^{2}.
During the day, I find that if I go too long without talking to someone, these thoughts may appear in a similar form as those that occur in the evening. Recently, they seem to begin when I think about how I’m missing out because I don’t know many people and don’t always have the courage to talk to others. Unfortunately, there’s a paradox: most of the time, when I have tried attending social events, I tend to feel worse. Huh.
Oneonone meetings, of course, are the main exception to this rule. Even if such meetings are not strictly for social purposes (e.g., a studentadvisor relationship), I usually feel like they have served a social purpose, and that they fulfill my minimum socialization goal for the day. It’s no surprise that after meetings, my mood improves regardless of the outcome, and I can get back to my work in a saner state.
I think it’s more important for someone like me to have meetings and to talk to others while at work. The rationale is that I don’t get to talk to many people, so any small conversation in which I do participate provides more utility to me as compared to that other person, because he or she will have had more social opportunities throughout the day.
As a result, I now try to stagger my schedule so that, instead of having three days completely free and one day with four meetings, I’ll have one meeting per day. Having just one halfhour meeting can completely change the course of a day by refreshing my “focus” and “motivation” meters so that I can finish up whatever task I need to finish.
You know, for someone who doesn’t socialize much, I sure do think about socialization a lot! Case in point: this short essay! The reason why I am just now writing this post is due to a recent visit at the Rochester Institute of Technology (RIT) to discuss some research with several colleagues. While I was at RIT, I had an incredibly easy time talking with my colleagues in sign language.
More than ever, I appreciate the enormous social benefit of RIT to deaf students. While I may have issues with how RIT handles academic accommodations, it has one thing that no other university can boast: a thriving, intelligent deaf population of computer scientists and engineers. Wouldn’t that be my ideal kind of situation?
My visit to RIT made me again wonder how my life would have been different had I decided to pick RIT for undergrad. Life, however, is full of tradeoffs, and whatever social benefit from going to RIT would have been countered by a possible reduction in my future opportunities. If I had attended RIT, I doubt I would have gotten in the Berkeley Ph.D. program, because the reputation of one’s undergraduate institution plays a huge (possibly unfair) role in determining admission.
I don’t want to suggest that all deaf people can follow my footsteps. I know that the only reason why I had that kind of “choice privilege” to decide between a hearingoriented versus a hearinganddeaforiented school is that my level of hearing (with hearing aids) and speech are just good enough so that I can thrive in a hearingdominated setting. By thrive, I mean academically (in most cases); I am always at the bottom of the social totem pole.
When I think about my social situation, I sometimes get angry. Then I react by reminding myself of how lucky I am in other ways. With the exception of hearing, I have a completely functional body with excellent mobility. My brain appears to be working fine and can efficiently process through various computer science problems that I face in my daily work. I live in a reasonably nice apartment in Berkeley, in an area that is reasonably safe. I have a loving family that provides an incredible amount of support to me.
In fact, almost every day since entering college five years ago, I’ve reminded myself of how lucky I am in these (and other) regards. I wish I could say I did this every day, but I’ve forgotten a few times. Shame on me.
I make no illusions. I am really lucky to be able to have conversations with hearing people, and I treasure these moments to what may seem like a ridiculous extent. Not all deaf people can do this on a regular basis. I do get frustrated when I communicate with others and don’t always get the full information.
But it could be a lot worse.
Finished the WordpresstoJekyll Migration
In my last post, I talked about the process of migrating my Wordpress.com blog into this Jekyll blog. I finally finished this process — at least, to the extent where nothing is left but touchups — so in this post, I’d like to explain the final migration steps.
The Manual Stuff
Let’s start with the stuff that I put off as long as possible: checking every single post to fix every single blogtoblog post link and every single image. With 150 posts, this was not the most fun activity in the world.
Due to the transition to Jekyll, the old blogtoblog links^{1} that I had in my blog posts
were no longer valid. To fix those up, I went through every single blog post, checked if it
referenced any other blog posts as links, and changed the link by replacing seitad.wordpress.com
with danieltakeshi.github.io
. Remember, this is only valid because I changed the Wordpress.org way
of permalinks to match Jekyll’s style. If I had not done that, then this process would have
involved more editing of the links.
I also fixed the images for each post. For each post that uses images, I had to copy the
corresponding file location in my old folder that contained my Wordpress.com images, and paste them
into the assets
folder here. Then, I fixed the image HTML in the Markdown file so that they looked
like this:
<img src="https://danieltakeshi.github.io/assets/image_name" alt="some_alt_text">
By keeping all images in the same directory, they have a common “skeleton” which makes life easier,
and image_name
is the file name, including any .png
, .jpg
, etc. stuff. The some_alt_text
is
in case the image does not load, so an alternative text will appear in place of the image.
The Interesting Stuff
Incorporating LaTeX into my posts turned out to be easier than expected. Unlike in Wordpress, where
I had to do a clumsy $latex ... $
to get math, in Jekyll + MathJax (which is the tool to
get LaTeX to appear in browsers) I can do $$ ... $$
. For example: $$\int_0^1 x^2dx$$
results in
. It does require two extra dollar signs than usual, but nothing is perfect.
Note: to get MathJax, I pasted the following code at the end of my index.html
file:
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeXAMSMML_HTMLorMML">
</script>
There were a few other things I wanted to do after incorporating math, and they also required
modifying index.html
:
 To have all the post content appear on the front page rather than only the titles. The latter case forces the user to click on the title to see any of the content.
 To add pages on the home page so that the first page (the default one) would display the 20 most recent posts, then the next page would list the next 20, etc.
To get the post content to appear, in index.html
where it loops through the posts in the site,
there is a place where one can post the content by using the post.content
variable^{2}. To
get the pages, see this blog post which explains that one needs to add the paginage: X
line
in the _config.yml
file, where X
is clearly the amount of posts per page. Then, one can loop
through the paginator.posts
variable to loop through the posts. To get a page counter at the
bottom that lets you jump from page to page, you need to use the paginator.previous_page
,
paginator.page
, and paginator.next_page
variables. Again, see the linked blog post, which
explains it clearly.
The Future
There are certainly a bevy of things I could do to further customize this blog. I’ll probably add some Google Analytics stuff later to track my blog statistics, but I don’t view that as a high priority. Instead, I would like to start writing about more computer science topics.

Just to be clear: these are when blog posts link to another blog posts in the text, such as by saying “click HERE to see my earlier statement”, where “HERE” is replaced with the link. ↩

It’s also possible to use
post.excerpt
which will only display part of a post on the home page, but I found that it messed up the links within the post. ↩
Seita's Place has Migrated from Wordpress to Jekyll!
A New Era
This post marks the beginning of a new era for my blog. For almost four years (!), which saw 151 posts published (this post is #152), Seita’s Place was hosted by Wordpress.com. Over the past few months, though, I’ve realized that this isn’t quite what I want for the long run. I have now made the decision to switch the hosting platform to Jekyll.
Many people others (e.g., Vito Botta and Tomomi Imura) have provided reasons why they migrated from Wordpress to Jekyll, so you can read their posts to get additional perspectives.
In my case, I:
 wanted to feel like I had more control over my site, rather than writing some stuff and then handing it over to a blackbox database to do all the work.
 wanted to write more in Markdown and use git/GitHub more often, which will be useful for me as I continue to work in computer science.
 wanted to more easily be able to write code and math in my posts.
 wanted to use my own personal text editor (vim is my current favorite) rather than Wordpress’s WYSIWYG editor.
 was tired of various advertisements underneath my posts.
 wanted to be more of a hacker and less like the “ignorant masses,” no offense intended =).
Jekyll, which was created by GitHub founder Tom
PrestonWerner^{1}, offers
a splendid blogging platform with minimalism and simplicity in mind. It allows users like me to
write posts in plain text files using Markdown syntax. These are all stored in a _posts
file in
the overall blog directory. To actually get the site to appear online, I can host it on my GitHub
account; here is the GitHub repository
for this site^{2}. By default, such sites are set to have a URL at username.github.io
, which for me
would be danieltakeshi.github.io
. That I can use GitHub to back up my blog was a huge factor in my
decision to switch over to Jekyll for my blog.
There’s definitely a learning curve to using Jekyll (and Markdown), so I wouldn’t recommend it for those who don’t have much experience with commandline shenanigans. But for me, I think it will be just right, and I’m happy that I switched.
How Did I Migrate?
Oh boy. The migration process did not go as planned. I was hoping to get that done in about three hours, but it took me much longer than that, and the process spanned about four days (and it’s still not done, for reasons I will explain later). Fortunately, since the spring semester is over, there is no better time for me to work on this stuff.
Here’s a highlevel overview of the steps:
 Migrate from Wordpress.com to Wordpress.org.
 Migrate from Wordpress.org to Jekyll
 Migrate comments using Disqus
 Proofread and check existing posts
The first step to do is one that took a surprisingly long time for me: I had to migrate from Wordpress.com to Wordpress.org. It took me a while to realize that there even was a distinction: Wordpress.com is hosted by Wordpress and they handle everything (including the price of hosting, so it’s free for us), but we don’t have as much control over the site, and the extensions they offer are absurdly overpriced. Wordpress.org, on the other hand, means we have more control over the site and can choose a domain name to get rid of that ugly “wordpress” text in the URL. Needless to say, this makes Wordpress.org extremely common among many professional bloggers and organizations.
In my case, I had been using Wordpress.com for seitad.wordpress.com
, so what I had to do was go to
Bluehost, pay to create a Wordpress.org site, which I named
seitad.com
, and then I could migrate. The migration process itself is pretty easy once you’ve
got a Wordpress.org site up, so I won’t go into detail on that. The reason why I used Bluehost is
because it’s a recommended Wordpress provider, and on their website there’s a menu option that you
can click to create a Wordpress.org site. Unfortunately, that’s about it for my praise, because I
otherwise really hate Bluehost. Did anyone else feel like Bluehost does almost nothing but shove
various “upgrade feature XXX for $YZ” messages down our throats? I was even misled by their pricing
situation and instead of paying $5 to “host” seitad.com
for a month, I accidentally paid $71 to
host that site for a year. I did notice that they had a 30day money back guarantee, so hopefully
I can hastily finish up this migration and request my money back so so I won’t have to deal with
Bluehost again^{3}.
To clarify, the only reason why I am migrating to Wordpress.org is because the next step, using a WordpresstoJekyll exporter plugin, only works on Wordpress.org sites, because Wordpress.com sites don’t allow external plugins to be installed. (Remember what I said earlier about how we don’t have much control over Wordpress.com sites? Case in point!) But before we do that, there’s a critical step we’ll want to do: change the permalinks for Wordpress to conform to Jekyll’s default style.
A permalink is the link extension given to a blog post after the end of the site URL. For
instance, suppose a site has address http://www.address.com
. It might have a page called “News”
that one can click on, and that could have address http://www.address.com/news
, and news
would
be the permalink.
Modifying permalinks is not strictly necessary, but it will make importing comments later easy.
The default Wordpress.org scheme seems like it appends a “p” followed by an integer, and then a
question mark. We want to change it to match Jekyll’s default naming scheme, which is
/year/month/day/title
, and we can do that by modifying the “Permalinks” section in the Wordpress
dashboard.
Now let’s discuss that WordpresstoJekyll exporter I recently mentioned. This plugin, created by GitHub staff member Ben Balter, can be found (you guessed it) on GitHub. What you need to do is go to the “Releases” tab and download a .zip file of the code; I downloaded version 2.0.1. Then unzip it and follow the instructions that I’ve taken from the current README file:
 Place plugin in
/wpcontent/plugins/
folder  Activate plugin in WordPress dashboard
 Select Export to Jekyll from the Tools menu
Steps (2) and (3) shouldn’t need much explanation, but step (1) is the trickiest. The easiest way to
do this is to establish what’s known as an FTP connection to the Wordpress.org server, with the
“host name” field specified by the URL of the old site (in my case, seitad.com
). What I did
was download FileZilla, a free FTP provider, and used its
graphical user interface to connect to my Wordpress.org site.
Note that to connect to the site, one does not generally use his or her Wordpress.org’s login, but
instead, one needs to use the login information from Bluehost^{4}! Once I got over my initial
confusion, I was able to “drag and drop” the wordpresstojekyll exporter plugin to the Wordpress
site. You can see in the above image (of Filezilla) that I have the plugin in the correct directory
on the remote site. Executing steps (2) and (3) should then result in a jekyllexport.zip
file
that contains the converted HTMLtoMarkdown information about blog entries, as well as other
metadata such as the categories, tags, etc.
All right, now that we have our zip file, it’s time to create a Jekyll directory with the jekyll new
danieltakeshi.github.io
command, where danieltakeshi
should be replaced with whatever GitHub
username you have. Then take that jekyllexport.zip
file and unzip it in this directory. This
should mean that all your old Wordpress posts are now in the _posts
directory, and that they
are converted to Markdown, and that they contain some metadata. The importer will ask if you want
to override the default _config.yml
file; I chose to decline that option, so _config.yml
was
still set to be what jekyll new ...
created for me.
The official Jekyll documentation contains a tool that you can use to convert from Wordpress (or Wordpress.com) to Jekyll. The problem with the Wordpress.com tool is that the original Wordpress.com posts are not converted to Markdown, but instead to plain HTML. Jekyll can handle HTML files, but to really get it to look good, you need to use Markdown. I tried using the Wordpress.org (not Wordpress.com) tool on the Jekyll docs, but I couldn’t get it to work due to missing some Ruby libraries that later caused a series of dependency headaches. Ugh. I think the simplicity and how the posts actually get converted to Markdown automatically are the two reasons why Ben’s external jekyll plugin is so popular among migrators.
At this point, it makes sense to try and commit everything to GitHub to see if the GitHub pages will
look good. The way that the username.github.io
site works is that it gets automatically refreshed
each time you push to the master branch. Thus, in your blog directory, assuming you’ve already
initialized a git repository there, just do something like
$ git add .
$ git commit m "First commit, praying this works..."
$ git push origin master
These commands^{5} will update the github repository, which automatically updates
username.github.io
, so you can refresh the website to see your blog.
One thing you’ll notice, however, is that comments by default are not enabled. Moreover, old comments made on Wordpress.org are not present even with the use of Ben’s WordpresstoJekyll tool. Why this occurs can be summarized as follows: Jekyll generates static pages, but comments are dynamic. So it is necessary to use an external system, which is where Disqus comes into play.
Unfortunately, it took me a really long time to figure out how to import comments correctly. I’ll summarize the ministeps as follows:
 In the Admin panel for Disqus, create a new website and give it a “shortname” that we will need
later. (For this one, I used the shortname
seitasplace
.)  In the Wordpress.org site, install the Disqus comment plugin^{6} and make sure your comments are “registered” with Disqus. What this means is that you should be able to view all comments in your blog from the Disqus Admin panel.
 Now comes the part that I originally missed, which took me hours to figure out: I had to import the comments with Disqus! It seems a little confusing to me (I mean, don’t I already have comments registered?), but I guess we have to do it. On Disqus, there is a “Discussions” panel, and then there’s a submenu option for “Import” (see the following image for clarification). There, we need to upload the .xml file of the Wordpress.org site that contains all the comments, which one can obtain without a plugin by going to Tools > Export in the Wordpress dashboard.
 You will also likely need to do some URL mapping. Comments in Disqus are stored relative to a URL, and the default URL is obviously the source from where it was imported! But if we’re migrating from source A to source B, doesn’t it make sense to have the comments default to source B’s URL instead of source A? In my case, I used a mapper in the “Tools” menu (in the above image) to convert all the comment links to be based on the current site’s URL. That way, if the original source (i.e., the Wordpress site) gets deleted, we still retain the comments^{7}. If you made sure the permalinks match, then this process should be pretty easy.
 Finally, the last thing to do is to actually install Disqus comments in the code for wordpress.
For that, I went to the “Universal Code” option for Disqus, and pasted the HTML code there into
the
_layouts/post.html
file.
After several restarts due to some random issues with Disqus/Wordpress having problems with deleted material, I was finally able to get comments imported correctly, and they had the same names assigned to the commentators! Excellent! The traceback comments, which are created by Wordpress when one blog post links to another blog post, did not get copied over here, but I guess that’s OK with me. I mostly wanted the human comments, for obvious reasons.
Whew! So we are done, right? Oh, never mind – we have to proofread each post! Since I
had 151 posts from Wordpress to import, that meant I had to proofread every single one of them.
Ben’s importer tool is good but not perfect, and code or mathheavy posts are especially difficult
to convert correctly. Even disregarding code and math, a common issue was that italicized text
wouldn’t get parsed correctly. Sometimes the Markdown asterisks were “one space too ahead”, e.g., if
the word code
needs to be italicized, the Markdown syntax for that is *code*
, but sometimes the
importer created *code *
, and that danging space can create some ugly asterisks visible in the
resulting HTML.
Even after basic proofreading, there are still additional steps one needs to take in order to ensure a crisp blog. One needs to
 fix the links for the images, since the images by default are set to the original Wordpress
address. The WordpresstoJekyll plugin will put the images in the
wpcontent
folder, but I (and the official Jekyll documentation) recommend copying those images over to anassets
folder. The defaultwpcontent
folder contains too many folders and subdirectories for my liking, but I guess it’s useful if a blog contains thousands of images.  fix the posttopost hyperlinks in each post to refer to the current Jekyll version. In vim, this should be easy as I can do a bunch of findandreplace calls to each file. Ensuring that the Wordpress permalinks follow Jekyllstyle permalinks makes this task easier.
 incorporate extra tools to get LaTeX formatting.
I haven’t been able to do all these steps yet, but I’m working on it^{8}.
Whew! The best part about the migration is that you only have to do it once. Part of the problem is that I had to rely on a motley collection of blog posts to help me out. The Jekyll documentation itself was not very helpful^{9}.
PostMigration Plan
In addition to the actual migration, there are some sensible steps that users should take to ensure that they can extract maximal utility from Jekyll. For me, I plan to
 learn more Markdown^{10}! And in addition, it makes sense to use a text editor that can
handle Markdown well. I’m using vim since it’s my default, but it’s actually not that useful to
me, because I set the syntax mode off (using
:syntax off
) and by default vim does not have a Markdown highlighter. I’m sure someone has created a Markdown syntax addon to vim, so I’ll search for that.  actually make the site look better! I don’t mind the simplicity of default Jekyll, but a little more “piazza” wouldn’t hurt. I’d like to at least get a basic “theme” up and running, and to include excerpts from each post on the front page.
 make a site redirect from my old Wordpress.com site, so that it redirects users to this site. I’d rather not delete the old site all of a sudden (even though I will delete it eventually). But I will get rid of that Wordpress.org site that I had to pay to create, all just to help me migrate to Jekyll.
Incidentally, now that we’ve covered the migration pipeline, I thought I should make it clear how
one would go about using Jekyll. To add new posts, one simply adds a file in the _posts
directory
that follows the convention YYYYMMDDnameofpost.ext
and includes the necessary front matter,
which contains the title, the date, etc. Looking at the raw Markdown code of sample posts is
probably the easiest way to learn.
One could update the site with each edit by adding, committing, and pushing to GitHub, but probably
a better way is to update locally by running jekyll build; jekyll serve
. This will create a local
copy of Jekyll that one can have open in a web browser even if one doesn’t have Internet access.
Each time one saves a post, the server will update, so by refreshing, we can see the edit. It won’t
catch all edits — I had to push to GitHub and then update the local copy to get images to show up
correctly — but it is useful enough that I thought I’d share (and suggest) it. Furthermore, if
the website is public, it’s best to update/push polished versions of posts rather than
worksinprogress.
Hopefully these personal notes prove useful to future Wordpress.{com,org}toJekyll migrators. In the meantime, I’m going to fix up the rest of this site and prepare some new entries that accentuate some of Jekyll’s neat features.

By the way, saying something like “GitHub cofounder Tom …” is the computer programming equivalent of the law school saying “Yale Law School graduate Bob …”. The fact that he founded GitHub immediately hightens my opinion of him. Oh, by the way, do you like how Jekyll does footnotes? Just click the little arrow here and you’ll be back up to where you were in the article! ↩

If you have experience using GitHub, then you can even fork my repository on GitHub to serve as a launching point for your own site or to test out Jekyll. ↩

Just to be clear, if you host a site on a public GitHub repository, then it’s free. That’s yet another reason to use Jekyll/GitHub! ↩

This username information should be included in the first email (or one of the first, I think) you got from Bluehost. The password should be the password you use to log into Bluehost to get to your user control panel. ↩

If you’re wondering how I was able to get a code block highlighted like that, I wrap the commands with three tildas (
~~~
) before and after the text. This is with thekramdown
Markdown scheme. ↩ 
Fortunately, you can find this plugin by searching in Wordpress directly; there’s no need to engage in fancy FTP stuff. ↩

Actually, I haven’t tested this yet. I hope this works. ↩

If you see things like dead links or any “weird” looking text here, please contact me. ↩

Interestingly enough, the Jekyll docs for migrating from Wordpress.com to Jekyll currently link to an external blog post from March 2011. I found that blog post to be reasonably helpful, but it didn’t really do what I needed, which tends to be a problem when following such guides. ↩

To add to the complexity, there are several different versions of Markdown. My site is currently using the
kramdown
style, but another popular one (that GitHub pages use) isredcarpet
, but that style messed up my footnotes, so I eschewed from using it. ↩