Posts

Note on convolutions

Image
For simplicity, we consider a 1d convolution on 1d input array as an example. Most important parameters of a convolution are kernel size, stride and padding, denoted by $K, S, P$, respectively. Let $I$ be the size of 1d input array, the index of padded array can be viewed of value from $-P$ to $I-1+P$ (inclusive). The convolution can be interpreted in a sliding window picture: At the start (0 step), the first element in the window is aligned at the index $-P$ of the input array; In each step, the window slides $S$ elements along the input array. That is, in the $i$-th step, the first element in the window is aligned at the index $-P+i*S$ and the last element is thus aligned at the index $-P+i*S + K-1$, leading to a constraint $-P+i*S + K-1\leq I-1+P$. As a result, we have $0 \leq i \leq (I + 2P-K) / S$ and the size of output array is \begin{equation}\left\lfloor\frac{I + 2P - K}{S}\right\rfloor + 1\,.\end{equation} The above process can be summarized in the python code: 1 2 3...

Iterative Closest Point

Iterative closest point (ICP) algorithm is to align two point clouds iteratively: For each point $\mathbf{x}^{(i)}$ in the query point cloud, find its corresponding point $\mathbf{y}^{(\cal{C}_i)}$ in the reference point cloud using the current estimation of rotation matrix $\mathbf{R}$ and translation $\mathbf{t}$: \begin{equation}\cal{C}_i = \arg\min_{j} \left|\left|\mathbf{y}^{(j)}-\mathbf{R}\mathbf{x}^{(i)}-\mathbf{t}\right|\right|_2^2\,.\tag{1}\end{equation} Update the rotation matrix $\mathbf{R}$ and translation $\mathbf{t}$ by optimizing \begin{equation}\min_{\mathbf{R},\mathbf{t}} \sum_{i=1}^n\left|\left|\mathbf{y}^{(\cal{C}_i)}-\mathbf{R}\mathbf{x}^{(i)}-\mathbf{t}\right|\right|_2^2\,.\tag{2}\end{equation}  To solve (2), we first take derivative on $\mathbf{t}$ and obtain \begin{equation}\mathbf{t} = \frac{1}{n}\sum_{i=1}^n\mathbf{y}^{(\cal{C}_i)}-\mathbf{R}\,\frac{1}{n}\sum_{i=1}^n\mathbf{x}^{(i)}\,.\tag{3}\end{equation} As a result, we can remove $\mathbf{t}$ from (...

EPR paradox and Bell inequality

Popular science books or articles on quantum mechanics love the topic of EPR paradox. It relates to a legend of Albert Einstein and his debates with Neils Bohr. As well known in physics history, Einstein was not completely convinced by the quantum mechanics interpretation, and the so-called EPR paradox was proposed as an attempt to prove that quantum mechanics is "incomplete". Nowadays, we are used to quantum mechanics. EPR paradox is no longer a paradox. It is solved by Bell inequality and its various experimental verifications. Thanks to the simplification made by physicists David Bohm and Yakir Aharanov, we can describe EPR paradox in terms of a spin singlet state of two entangled electrons \begin{equation}|S=0\rangle=\frac{1}{\sqrt{2}}\left[|\uparrow\rangle_A |\downarrow\rangle_B -  |\downarrow\rangle_A |\uparrow\rangle_B\right]\,.\tag{1}\end{equation} Remarks: The spins of two electrons are alway along the exact opposite directions, with or without measurement...

Second quantization and quantum field theory

When I was an undergraduate and took the course of advanced quantum mechanics, I did not fully grasp the concept of second quantization. In my impression, second quantization was summarized as the following recipe: Expand the wave function in some basis: $\psi(x, t)=\sum_k a_k(t)\phi_k(x)$.  Impose the commutation relation like $[a_k, a^{\dagger}_{k'}]=\delta(k-k')$ (second quantization). Replace operators $\hat{\cal{O}}$ by their expectation values $\int dx \psi(x)^{\dagger} \hat{\cal{O}} \psi(x)$. I don't remember clearly whether the above summary was from my crash study for the final exam or exactly what I was taught on class. So I asked one of my best friends Joking for confirmation since we had that class together. Without any surprise, Joking told me that he never understand second quantization either.  Now let me try to explain what second quantization actually is in this post. Before start, I have to say second quantization is really a misleading terminology. As sho...

Interview problems on C++

1. Difference between pointer and reference: A pointer is a variable storing the memory address of the variable it points to. It is safe to think of a reference as an alias of the variable. (1) A pointer should be dereferenced with * while a reference can be used directly. (2) A pointer can be reassigned while a reference cannot once initialized. (3) A pointer can be assigned to point to nullptr directly while a reference cannot. (4) We can perform arithmetic operations on a pointer. (5) Pointer of pointer provides multiple levels of indirections. 2. Inheritance: 'is a' 3. Virtual: (1) A virtual function is a member function that is declared in the base class with keyword 'virtual' and can be overridden by derived classes. (2) A pure virtual function is a virtual function without implementation in the base class. It is specified by '=0' in the declaration. (3) A abstract class is a class that contains at least one pure virtual function. (4) An interface is a cla...

Interview problem: distance between median and mean

Problem: Provide a bound on the distance between median and mean. Solution: Let $X$ be a random variable and $M(X)$ be the median. Besides $\mathbb{E}(X)=\text{argmin}_c\mathbb{E}\left(X-c\right)^2$, we have \begin{equation}M(X)=\text{argmin}_c\mathbb{E}\left|X-c\right|\,.\end{equation} As a result,  we can prove using  Jensen's inequality : \begin{eqnarray} \left|\mathbb{E}(X)-M(X)\right| &\leq& \mathbb{E}\left|X-M(X)\right| \\ &\leq& \mathbb{E}\left|X-\mathbb{E}(X)\right| \\ &\leq& \sqrt{\mathbb{E}\left(X-\mathbb{E}(X)\right)^2}=\sqrt{\text{Var}(X)}\,.\end{eqnarray} 

Interview problem: Welford's online algorithm

Problem:  Provide an online algorithm to compute the sample mean and sample variance of stream data.  Solution: Let $x_1, x_2, \cdots, x_n, \cdots$ be the stream data, the sample mean $\bar{x}_n \equiv\sum_{i=1}^n x_i / n$ can be computed by \begin{equation}\bar{x}_n=\bar{x}_{n-1} +\frac{x_n - \bar{x}_{n-1}}{n}\,.\tag{1}\end{equation} For the sample variance $s^2_{n}\equiv \frac{1}{n-1}\sum_{i=1}^n\left(x_i-\bar{x}_n\right)^2$, a naive method is to compute \begin{equation} s^2_n = \frac{1}{n-1}\left(\sum_{i=1}^n x_i^2 - \frac{\left(\sum_{i=1}^n x_i\right)^2}{n}\right)\end{equation} by the statistics of the sum and sum of squared. However, such method is numerically unstable since it may involve the cancellation of two very similar numbers. Instead, as suggested in this wiki , we use Welford's algorithm: \begin{eqnarray} M_{2,n} &=& M_{2, n-1}+\left(x_n-\bar{x}_{n-1}\right)\left(x_n-\bar{x}_n\right)\,, \\ s_n^2 &=&\frac{M_{2, n}}{n-1}\,.\tag{2}\end{eqnarray} Pro...