Sunday, 10 March 2013

Complexity theory

The class NP consists decision problems with solutions that can be "checked" in polynomial time. Equivalently, it can also be defined as a set of decision problems solvable in polynomial time via a "lucky" algorithm. This is a nondeterministic model where an algorithm makes guesses and then says YES or NO.  The name "NP" stands for "nondeterministic polynomial time".

A problem is in the class NP-complete, if it is in NP and is as "hard" as any problem in NP. So NP-complete problems are the hardest problems in NP.

Examples of NP-complete problems:

  • Knapsack (pseudopolynomial, O(nS) where n is number of items and S is the size of the knapsack)
  • Traveling salesman problem
  • SAT
  • Tetris
  • 3-coloring a given graph

MIT 6.006 notes: http://courses.csail.mit.edu/6.006/fall11/lectures/lecture23.pdf

TED talk: How movies teach manhood



I really enjoyed this talk. Colin Stokes is both eloquent and humorous. I agree with his observation that there are movies that teach girls to fight against patriarchy (like many Disney movies), but they are all tailored to girls and there are very few movies that teach boys to fight against patriarchy. As a result, there are no role models for boys to know how to respect women and treat them as equals. In a quest to achieve equality, we need work on both sides: encouraging girls to pursue their dreams without restrained by stereotypes and fostering the idea that female leadership is as normal as male leadership in boys. That's why Stokes asks for more movies that send positive messages to boys: that cooperation is heroic, and respecting women is as manly as defeating the villain.

In his talk, he mentioned the Bechdel test for movies. It is used to identify gender bias in fiction. I've heard this test before on a graduate women mailing list about showing movies that pass the test. The test has 3 requirements:

  1. Are there at least 2 named female characters?
  2. Did the two talk to each other?
  3. Did they talk about something other than men?
You have to agree that these these criteria are some what simplistic, but it can be a good start. Even with this these simple criteria, there are only about 50% of the movies in a year pass it.

Stokes also mentioned the movie Brave, which is an nontraditional princess movie. Unlike the usual princess movies which resolve around a male romantic attachment, this movie is purely about mother-daughter relationship. There is no point in the movie that the princess becomes romantic with a male character. This is indeed quite rare in Hollywood movies. It's good that Brave also wins the Oscar for the best animated film. 

Sunday, 3 March 2013

Matlab save path

Matlab allows you to add paths to the search path and you are supposed to be able to save it so that you don't need to add the paths again the next time. However when I tried to save the paths, the new paths didn't show up when I restarted Matlab. I'm running MATLAB R2012a and Ubuntu 12.04.

This post helps me solve the problem. After saving the new paths, a new file is created in ~/Documents/MATLAB because I do not have write permission in the default installation location. However the new pathdef.m file is not read when Matlab starts. To solve this, you can add startup.m in ~/Documents/MATLAB if that's your urserpath. Add the following line to the file:
path(pathdef)

On Windows 7, when you set path through the dialog box, it will add the new paths to the pathdef.m file in the installation folder. This will affect other users' path. To add the paths so only you will see it, you can do the similar thing before by adding a startup.m file in C:\Users\[username]\Documents\MATLAB if that is your statup path. On Windows, it won't ask you to specifiy a new file to save the paths, so you need to copy the pathdef.m file from the installation folder (default is C:\Program Files\MATLAB\R2013a\toolbox\local\pathdef.m) to the userpath.

However, one problem I encountered when I start from the userpath is that some functions in the parallel computing toolbox no longer work. Turns that there seem to be some conflicts between the toolbox and the Bayesian Network Toolbox (BNT) I'm using. After moving the BNT to the bottom of the search path, everything works again.

Wednesday, 27 February 2013

Ray Kurzweil



Ray Kurzweil is a prolific inventor and is involved in fields like OCR, text-to-speech synthesis, and speech recognition. He did his undergraduate at MIT. In this talk, he emphasized the exponential growth of information technology. I think everyone can see that information technology is growing very fast. Just in the short span of my life so far, my daily life has transitioned from one that is almost devoid of electronic devices to one in which computers, smart phones, tablets etc are indispensable. However, I haven't thought too much about the actual growth speed before this talk. Grasping the concept of exponential growth is almost eye-opening. This really makes me feel very excited because the range of possibilities that technology can achieve is kind of unfathomable. It's exciting to live in this age of the world and I also feel an urgency in my work. I have to work fast, otherwise the work will be outdated very soon.  

Sunday, 24 February 2013

Vim cheat sheet

Compound command
Compound commandEquivalent in longhand Effect
Cc$clear to the end of the line
sclclear to right
S^Cclear to the start of the line
I^iinsert from the start of the line
A$ainsert after the end of the line
oA<CR>insert to the new line below
Okoinsert to the new line above

Wednesday, 20 February 2013

Bayesian network

The posterior probability is the probability of the parameters given the evidence : \(P(\theta|X)\).
Likelihood is the probability of the evidence given the parameters: \(P(X|\theta)\).

maximum a posteriori probability (MAP) estimate is a mode of the posterior distribution. It can be used to obtain a point estimate of an unobserved quantity on the basis of empirical data. It is closely related to maximum likelihood (ML), but employs an augmented optimization objective which incorporates a prior distribution over the quantity one wants to estimate. MAP estimation can therefore be seen as a regularization of ML estimation.

Maximum likelihood estimate of \(\theta\) is $$\hat\theta_{ML}(x) = \underset{\theta}{\arg\max}f(x|\theta)$$
If \(g(\theta)\) is a prior distribution over \(\theta\), $$\hat{\theta}_{MAP} = \underset{\theta}{\arg\max}f(x|\theta)g(\theta)$$

Conjugate prior

In Bayesian probability theory, if the posterior distributions \(p(\theta|x)\) are in the same family as the prior probability distribution \(p(\theta)\), the prior and posterior are then called conjugate distribution, and the prior is called a conjugate prior for the likelihood. For example, the Gaussian family is conjugate to itself with respect to a Gaussian likelihood function: if the likelihood function is Gaussian, choosing a Gaussian prior over the mean will ensure that the posterior distribution is also Gaussian.

The posterior distribution of a parameter \(\theta\) given some data x is $$p(\theta|x) = \frac{p(x|\theta)p(\theta)}{\int{p(x|\theta)p(\theta)d\theta}}$$ A conjugate prior is an algebraic convenience, giving a closed-form expression for the posterior.

Dirichlet distribution is the conjugate prior of the categorical distribution and multinomial distribution. The Dirichlet distribution with parameters \(\alpha_1, \dots, \alpha_K > 0\) has a probability density function given by $$f(x_1, \dots, x_{K-1};\alpha_1, \dots, \alpha_K) \propto \prod_{i = 1}^Kx_i^{\alpha_i-1}$$ for \(x_i > 0\) and \(\sum x_i = 1\). This makes it suitable to be a prior distribution for a model parameter \(\boldsymbol{\theta}\) for a multinomial distribution where \(\boldsymbol{\theta}_i = x_i\).

Linear algebra reivew

Homogeneous systems

$$b_{11}x_1 + b_{12}x_2 + \dots + b_{1n}x_n = 0 \\ b_{21}x_1 + b_{22}x_2 + \dots + b_{2n}x_n = 0 \\ \dots \\ b_{p1}x_1 + b_{p2}x_2 + \dots + b_{pn}x_n = 0$$

Properties:
  • Has at least one 1 solution [0, 0, ..., 0]
  • If it has a non-zero solution, then it has infinite number of solutions.

Inverse of a matrix

A matrix \(A\) has an inverse if one of the following holds:
  • \(det(A)\neq 0\)
  • The reduced form of A is the identity matrix.
  • \(A\) has full rank.
  • The homogeneous equation \(Ax=0\) has a unique solution.
  • The equation \(Ax = b\) has a unique solution for every b.

Eigenvalues and eigenvectors

An eigenvector is a non-zero vector which is transformed to a scalar multiple of itself.
The eigenvalue equation for a matrix \(A\) is \(Av-\lambda v = 0\), which is equivalent to \((A - \lambda I)v = 0\).
This equation only has non-zero solutions if and only if \(det(A - \lambda I) = 0\), i.e.\((A - \lambda I)\) is singular and not invertible.

Showing that an eigenbasis makes for good coordinate systems

Theorems 

Number of eigenvalues of a matrix

Suppose that A is a square matrix of size n with distinct eigenvalues \(\lambda_1, \lambda_2, \lambda_3,\dots,\lambda_k\). Then \(\sum_{i=1}^k\alpha_A(\lambda_i) = n\), where \(\alpha_A(\lambda_i)\) is the algebraic multiplicity of \(\lambda_i\).

Maximum number of eigenvalues of a matrix

Suppose that A is a square matrix of size n. Then A cannot have more than n distinct eigenvalues.

Spectral theorem

Consider a Hermitian map A on a finite-dimensional real or complex inner product space V endowed with a positive definite Hermitian inner product. The Hermitian condition means
 (\forall x,y\in V): \langle A x ,\, y \rangle =  \langle x ,\, A y \rangle .
An equivalent condition is that A* = A where A* is the hermitian conjugate of A. In the case that A is identified with an Hermitian matrix (one which is equal to its own conjugate transpose), the matrix of A* can be identified with its conjugate transpose. If A is a real matrix, this is equivalent to AT = A (that is, A is a symmetric matrix).
Theorem. There exists an orthonormal basis of V consisting of eigenvectors of A. Each eigenvalue is real.

So in less precise terms, and considering only real numbers: if A is a symmetric matrix, its eigenvectors form an orthonormal basis.

Principal component analysis

One of the applications involving eigenvalues and eigenvectors is PCA. It transforms a set of observations of possibly correlated variables into a set of values of linearly uncorrelated variables called principal components. The first principal component has the largest possible variance (that is, accounts for as much as of the variability in the data as possible).

PCA is used in the eigenface technique for face recognition.

One question one would ask is why the eigenvectors of the covariance matrix \(\textbf{V}\) of the data are the principal component. So by definition, the first principal component \(\textbf{w}\) has the largest possible variance. If we project all the data in to this direction. The variance of the resultant data is \(\textbf{w}^T\textbf{Vw}\). So we want to choose a unit vector \(\textbf{w}\) to maximize the variance. Note that we need to constrain the maximization otherwise there is no maximum point of the objective function. The constraint is \(\textbf{w}\) is a unit vector so that \(\textbf{w}^T\textbf{w} = 1\). To do constrained optimization, we need to use Lagrange multiplier. The Lagrange function is thus
\begin{align}L(\textbf{w}, \lambda) &= \textbf{w}^T\textbf{w} - \lambda(\textbf{w}^T\textbf{w} - 1)\\
\frac{\partial u}{\partial \textbf{w}} &= 2\textbf{Vw} - 2\lambda\textbf{w} \\
\textbf{Vw} &= \lambda\textbf{w} \\
\textbf{AA}^T\textbf{w} &= \lambda\textbf{w}
\end{align}
This means the maximizing vector will be the eigenvector with the largest eigenvalue. The principal component vector is also a linear combination of the original variables.

Singular value decomposition

Singular value decomposition can be expressed as
$$M = U\Sigma V^*$$

The column of V are eigenvectors of \(M^*M\). If M is positive semi-definite, the eigenvalue decomposition of M is the same as singular value decomposition. However, the eigenvalue decomposition and the singular value decomposition differ for all other matrices M.