\documentclass[aspectratio=149]{beamer}

\input{../../shared_slides.tex}

% reference: https://yuxinchen2020.github.io/ele522_optimization/lectures/variance_reduction.pdf
% or
% https://ieeexplore-ieee-org.uaccess.univie.ac.at/stamp/stamp.jsp?tp=&arnumber=9226504

\usepackage{booktabs}

\title{Duality, Gradient-free, in Application}
\date{\today}

\begin{document}
\maketitle
\frame{\tableofcontents}

\section{Duality}%

\begin{frame}
  \frametitle{Duality}
  \begin{center}
    Establishes some relation between two classes of objects.
  \end{center}
  \begin{definition}[``Legendre transform'' or ``Fenchel conjugate'']
    Given a function $f: \R^d \to \R \cup \{+\infty\}$, we define its \textcolor{blue}{\textbf{conjugate}} $f^*:\R^d \to \R \cup\{+\infty\}$ by
    \begin{equation}
      f^*(y) = \sup_x \{\langle y,x \rangle - f(x)\}
    \end{equation}
  \end{definition}
\end{frame}

\begin{frame}
  \frametitle{Convex conjugate}

  \begin{block}{}
    \begin{equation}
      f^*(y) = \sup_x \{\langle y,x \rangle - f(x)\}
    \end{equation}
  \end{block}
  \begin{minipage}{0.48\textwidth}
  \begin{figure}[ht]
    \centering
    \includegraphics[width=\textwidth,keepaspectratio]{conjugate}
  \end{figure}
  \end{minipage}
  \begin{minipage}{0.48\textwidth}
    \vspace{2cm}
    \textcolor{blue}{Figure:} maximum gap between linear function $x \mapsto \langle y, x \rangle$ and $f$
  \end{minipage}
\end{frame}

\begin{frame}
  \frametitle{Properties}

  \begin{itemize}
    \item $f^*$ is always convex. \hfill{\textcolor{gray}{point-wise max of affine function}}
    \item \textcolor{blue}{Fenchel's inequality:}
          \begin{equation}
            f(x) + f^*(y) \ge \langle x, y \rangle.
          \end{equation}
    \item Hence the biconjugate $f^{**}:={(f^*)}^*$ satisfies $f^{**}\le f$.
    \item If f is convex an lsc. then $f^{**}=f$.
    \item etc.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Examples}
  \begin{itemize}
    \item \textcolor{blue}{Norm:} If $f(x) = \Vert x \Vert$, then
          \begin{equation}
            f^*(y) = \mathbbm{1}(\Vert y \Vert_* \le 1),
          \end{equation}
          i.e.\ the indicator of the dual norm ball.
          Recall the definition of the dual norm:
          \begin{equation}
            \Vert y \Vert_* :=  \max_{\Vert x \Vert \le 1} \{\langle y, x \rangle\}.
          \end{equation}
          In particular: $\Vert \cdot \Vert_1 \leftrightarrow \Vert \cdot \Vert_\infty$
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{More examples}
    \begin{equation}
      \hspace{-4cm}\text{\textcolor{blue}{Generalized linear models:}}  \qquad \min_{x\in \R^d} \, f(Ax) + g(x).
    \end{equation}
    Two approaches to reformulate:
    \begin{equation}
      \min_x \underbrace{\max_y \, \langle y, Ax \rangle - f^*(y)}_{= f(Ax)} + g(x).
    \end{equation}
    Switch min and max
    \begin{equation}
      \min_x \max_y \, - f^*(y) + \langle y, Ax \rangle + g(x).
    \end{equation}
    Change sign to go from max to min
    \begin{equation}
      \min_x \, - f^*(y) - \min_y \underbrace{-\langle y, Ax \rangle - g(x)}_{= g^*(-A^T y)}.
    \end{equation}
\end{frame}

\begin{frame}
  \frametitle{Generalized linear models continued}
  Or reformulate
  \begin{equation}
    \min_{x\in \R^d} \, f(Ax) + g(x)
  \end{equation}
  as
  \begin{equation}
    \min_{x\in\R^d, w\in \R^m} \, f(w)+ g(x) \quad \text{s.t.} w = Ax
  \end{equation}
  Use Lagrange function
  \begin{equation}
    \mathcal{L}(x,w, u) :=  \, f(w)+ g(x) \langle u, w - Ax \rangle,
  \end{equation}
  then the dual function is given by
  \begin{equation}
    \varphi(u) = \min_{x\in\R^d, w\in \R^m} \mathcal{L}(x,w,u).
  \end{equation}
  Dual problem
  \begin{equation}
    \max_{u \in \R^m} [\varphi(u) = - f^*(-u) - g^*(A^T u)].
  \end{equation}
\end{frame}

\begin{frame}
  \frametitle{Example: Lasso}
  $\ell_1$ regularized regression
  \begin{equation}
    \min_{x\in \R^d}\, \frac12 \Vert Ax-b \Vert^2 + \lambda \Vert x \Vert_1
  \end{equation}
  fits this template with
  \begin{equation}
    f(w) = \frac12 \Vert w-b \Vert^2 \quad \text{and} \quad g(x) = \lambda \Vert x \Vert_1
  \end{equation}
  Computation gives
  \begin{equation}
    f^*(u) = \frac12 \Vert b \Vert^2 - \frac12 \Vert b-u \Vert^2 \quad \text{and} \quad g^*(v)= \mathbbm{1}(\Vert v/\lambda \Vert_\infty \le 1).
  \end{equation}
\end{frame}

\begin{frame}
  \frametitle{Dual of Lasso}
  We had
  \begin{equation}
    f^*(u) = \frac12 \Vert b \Vert^2 - \frac12 \Vert b-u \Vert^2 \quad \text{and} \quad g^*(v)= \mathbbm{1}(\Vert v/\lambda \Vert_\infty \le 1).
  \end{equation}
  So the dual is
  \begin{equation}
    \begin{aligned}
      &\max_{u\in \R^m} - f^*(-u) - g^*(A^T u) \\
      \Leftrightarrow& \min_{u\in\R^m} \Vert b+u \Vert^2 \quad \text{s.t.} \quad \Vert A^T u \Vert_{\infty} \le \lambda
    \end{aligned}
  \end{equation}

  Similarly, for least squares, ridge/logistic regression, SVM, etc.
\end{frame}

\begin{frame}
  \frametitle{But why?}
  \begin{itemize}
    \item Duality gap gives a \textcolor{blue}{certificate} of current optimization quality
          \begin{equation}
            \begin{aligned}
              f(A \bar{x}) + g(\bar{x}) &\ge \min_{x\in\R^d} \, f(Ax) + g(x) \\
              & \ge \max_{u\in\R^m} - f^*(-u) - g^*(A^T u) \\
              &\ge -f^*(-\bar{u}) - g^*(A^T \bar{u})
            \end{aligned}
          \end{equation}
    \item Stopping criterion
    \item Dual problem is sometimes easier to solve
  \end{itemize}
\end{frame}

\section{Derivative free}%
\label{sec:}

\begin{frame}
  \frametitle{}

  \begin{center}
    \Huge\textcolor{blue}{Zero-order Optimization}\\
    $\Leftrightarrow$ \textcolor{blue}{Derivative-Free}\\
    $\Leftrightarrow$ \textcolor{blue}{Blackbox}
  \end{center}

\end{frame}

\begin{frame}
  \frametitle{Look, no gradients!}
  Can we solve $\min_{x\in \R^d}\, f(x)$ without access to gradients?

  \begin{algorithm}[H]
    \caption{Random search}
    \begin{algorithmic}[1]
      \For{$k = 1,2, \dots$}
      \For{pick a random direction $d_k \in \R^d$}
      \State{$\gamma_k:= \argmin_{\gamma\in \R} f(x_k+ \gamma d_k)$}
      \State{$x_{k+1} := x_k+ \gamma_k d_k$}
      \EndFor{}
    \end{algorithmic}
  \end{algorithm}

\end{frame}

\begin{frame}
  \frametitle{Convergence rate for derivative-free random search}
  Converges same as gradient descent - up to a slow-down factor $d$.

  \textcolor{blue}{Proof.}
  \begin{equation}
    f(x_k+ \gamma d_k ) \le f(x_k) + \gamma \langle d_k, \nabla f(x_k) \rangle + \frac{\gamma^2L}{2} \Vert d_k \Vert^2
  \end{equation}
  Minimizing the upper bound (RHS), there exists a step $\bar{\gamma}$ for which
  \begin{equation}
    f(x_k + \bar{\gamma}d_k) \le f(x_k) - \frac{1}{L} \left\langle \frac{d_k}{\Vert d_k \Vert^2}, \nabla f(x_k)\right\rangle.
  \end{equation}
  So our (exact line-search) step can only be better
  \begin{equation}
    f(x_k + \gamma_k d_k) \le f(x_k + \bar{\gamma}d_k)
  \end{equation}
  Taking expectation and using $\E_r \langle r, g \rangle^2 = 1/d \Vert g \Vert^2$ for $r \sim$ sphere, gives
  \begin{equation}
    \E [f(x_k+ \gamma_k d_k)] \le E[f(x_k)] - \frac{1}{L d} \E\left[ \Vert \nabla f(x_k) \Vert^2\right].
  \end{equation}

\end{frame}

\begin{frame}
  \frametitle{Convergence rate for derivative-free random search}
  Same as what we obtained for \textcolor{blue}{gradient descent},\\
  now with an \textcolor{blue}{extra factor of $d$}. $d$ can be huge!!!\\
  \bigskip
  Similarly for other function classes
  \begin{itemize}
    \item For convex functions, we get a rate of $\mathcal{O}(\frac{d L}{\epsilon})$.
    \item For $\mu$-strongly convex functions, we get a rate of $\mathcal{O}(d \kappa \log(1/\epsilon))$.
  \end{itemize}
  Always $d$ times the complexity of gradient descent on the function class.\\
  \bigskip
  But assumed differentiability. Can also approximate the gradient.

\end{frame}

\begin{frame}
  \frametitle{Applications for derivative-free random search}

  \textcolor{blue}{Applications}
  \begin{itemize}
    \item competitive method for \textcolor{blue}{reinforcement learning}
    \item No need to store a gradient
    \item hyperparameter optimization, and other difficult e.g. discrete optimization
problems, black-box, noisy
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Reinforcement learning}
  \begin{equation}
    s_{k+1} = f(s_k, a_k, e_k),
  \end{equation}
  where $s_k$ is the \textcolor{blue}{state} of the system, $a_k$ is the control \textcolor{blue}{action}, and $e_t$ is some random \textcolor{blue}{noise}. We assume existence of $f$, but it is unknown.\\
  \medskip
  We search for a ``policy''
  \begin{equation}
    a_k := \pi(a_1, \dots, a_{k-1}, s_0, \dots, s_k)
  \end{equation}

  \medskip
  \textcolor{blue}{Goal:} Maximize reward
  \begin{equation}
    \begin{aligned}
       \max_{a_k}\, &\E_{e_k} \left[ \sum_{k=1}^N R_k(s_k, a_k)\right]\\
       \text{s.t.} & s_{k+1}=f(s_k, a_k, e_k)
    \end{aligned}
  \end{equation}
\end{frame}

\section{methods in practice}%
\label{sec:}

\begin{frame}
  \frametitle{}
  \begin{center}
    \Huge\textcolor{blue}{Adaptive \& other SGD methods}\\
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Adagrad}
  An adaptive variant of SGD
  \begin{algorithm}[H]
    \caption{Adagrad}\label{label:}
    \begin{algorithmic}[1]
      \For{$k = 1, \dots$}
      \State{pick stochastic gradient $g_k$}
      \State{update $[G_k]_i = \sum_{l=}^{k}([g_k]_i)^2, \quad \forall i$}
      \State{update $[x_{k+1}]_i =[x_{k}]_i - \frac{\gamma}{\sqrt{[G_k]_i}}[g_k]_i,  \quad \forall i$}
      \Endfor
    \end{algorithmic}
  \end{algorithm}
  \begin{itemize}
    \item chooses an \textcolor{blue}{adaptive, coordinate-wise} learning rate
    \item strong performance in practice
    \item many variants: Adadelta, Adam, RMSprop
  \end{itemize}

\end{frame}

\end{document}


\documentclass[a4paper,12pt]{article}
\usepackage{amsmath,amssymb}
\usepackage[margin=1in]{geometry}

\begin{document}

\paragraph{Problem 4 Hints}
So for this problem just assume that $X = \mathbb{R}^n$.
Then $a\cdot x = a_1 x_1 + \cdots + a_n x_n$.
Given two vectors $x,y \in \mathbb{R}^n$ and scalars $\alpha, \beta \in \mathbb{R}$ we have
\begin{align}
	a \cdot (\alpha x + \beta y) &= a\cdot (\alpha x) + a\cdot (\beta y) \nonumber \\
	&= \alpha(a\cdot x) + \beta (a\cdot y) \nonumber
\end{align}
therefore its a linear map.

For part (ii), this is basically the proof that linear transformations between real vector spaces can be represented by real valued matrices.
In this case the matrices will all be $n\times 1$.
Consider a function $f\in X^*$.
By definition $f$ is a linear map from $X = \mathbb{R}^n$ to $\mathbb{R}$.
Because $X$ is a linear subspace and $\mathbb{R}$ is a linear subspace and $f$ is a linear map we know that $f(X)$ is a linear subspace in $\mathbb{R}$.
Because $X$ and $f(X)$ are subspaces they have a basis.
Let $(\mathbf{e}_1,\dots, \mathbf{e}_n)$ be the coordinate vectors for $\mathbb{R}^n$ (which are a basis for $X$).
Then for any $x\in X$ we can write $\mathbf{x} = x_1 \mathbf{e}_1 + \cdots + x_n \mathbf{e}_n$ (a linear combination of the basis vectors).
Applying our function $f$ to $x$ gives us
\begin{align}
	f(\mathbf{x}) &= f( x_1 \mathbf{e}_1 + \cdots + x_n \mathbf{e}_n ) \nonumber \\
	&= x_1 f(\mathbf{e}_1) + \cdots + x_n f(\mathbf{e}_n) \nonumber
\end{align}
Since $\mathbf{e}_i$, for $i=1,\dots,n$ is a vector we know $f(\mathbf{e}_i) \in \mathbb{R}$.
For all $i = 1,\dots, n$ define $f(\mathbf{e}_i) = a_i$.
Then
\begin{align}
	f(\mathbf{x}) &= x_1 a_1 + \cdots + x_n a_n \nonumber \\
	&= a_1 x_n + \cdots + a_n x_n \nonumber \\
	&= \mathbf{a} \cdot \mathbf{x} \nonumber
\end{align}
so we can define the $\mathbf{a} = (f(\mathbf{e}_1), \dots, f(\mathbf{e}_n))$.

For part (iii), you should use the defined operations to show that the vector space axioms hold for this set so that its a vector space.
The key operations are that if $f$ and $g$ are linear functions in the set, then these will be our vectors.
So $f + g$ is defined as $f(x) + g(x)$ where $x$ is a vector in $X$.
If $\alpha$ is a real number, then we scale the vectors in $X^*$ as $\alpha f = \alpha f(x)$.
All the axioms should be tested.

For part (iv) the idea is to use what we just learned in part (ii), that every linear functional $f \in X^*$ can be uniquely determined by a vector $\mathbf{a} = (f(\mathbf{e}_1),\dots,f(\mathbf{e}_n))$.
We can think about finding a set of such $\mathbf{a}$ vectors.
For example suppose we have a set of $n$ vectors, say the coordinate vectors again, $(\mathbf{e}_1,\dots, \mathbf{e}_n)$, do each of these represent their own linear functional? Yes.
Now that we are calling them functionals, lets represent the set now as $\{f_1,\dots, f_n\}$.
At this point we establish that any linear functional is just a linear combination of these $n$ linear functionals and that the set is linearly independent.
Once you've done that you will have shown that a set of $n$ vectors is a basis for $X^*$ and therefore $\dim\left(X^*\right) = n$.

\paragraph{Problem 10 Hints}
Define the profit function $\pi(p) = \max\{p\cdot y : y\in Y\}$.
This function tells you the maximum profit over technology $Y$ when prices are $p$.
Lets suppose we have some fixed $p_0 \in \mathbb{R}^n$.
Then what do we know about $\pi(p_0)$?
Well we know that for all $y\in Y$ it must be the case that $p_0\cdot y \leq \pi(p_0)$, right?
By definition of the maximum, either $y$ achieves the maximum and $p_0\cdot y = \pi(p_0)$ or else it must be that $p_0\cdot y < \pi(p_0)$.
So what if we look at all the vectors $y \in \mathbb{R}^n$ (notice not just in $Y$) that when you take the dot product of it with $p_0$ it equals $\pi(p_0)$?
\[
  \{ y \in \mathbb{R}^n: p_0\cdot y = \pi(p_0) \}
\]
You should notice from previous exercises that this is a hyperplane in $\mathbb{R}^n$ and we have associated with it two half-spaces.
\[
  H^{\leq}_{p_0}(\pi(p_0)) = \{ y \in \mathbb{R}^n : p_0 \cdot y \leq \pi(p_0) \} \qquad H_{p_0}^{\geq}(\pi(p_0)) = \{ y \in \mathbb{R}^n : p_0 \cdot y \geq \pi(p_0)
\]
The left half-space could be referred to as the ``lower'' half-space and the right one as the ``upper'' half-space.
Of course we get the hyperplane by simply taking the intersection $H_{p_0}^{\leq}(\pi(p_0)) \cap H_{p_0}^{\geq}(\pi(p_0))$.

If its the case that $Y \subseteq H_{p_0}(\pi(p_0))$ what should we be able to show?
As you know from the proofs material the subset relation is an implication of the form $y\in Y \implies y \in H_{p_0}(\pi(p_0))$.
As discussed we know that for any $y \in Y$ it must be the case that $p_0\cdot y \leq \pi(p_0)$ so that rules out the ``upper'' half-space, right?
So we focus on the ``lower'' half-space.
Let $y \in Y$ then by definition of $\pi(p_0)$ we know that $p_0 \cdot y \leq \pi(p_0)$ and since $y \in Y \implies y \in \mathbb{R}^n$ we know that $y \in \{ y \in \mathbb{R}^n : p_0 \cdot y \leq \pi(p_0) \} = H_{p_0}^{\leq}(\pi(p_0))$.
Since our choice of $y \in Y$ was arbitrary, it holds for all $y \in Y$ and thus $Y\subseteq H_{p_0}^{\leq}(\pi(p_0))$.

Remember that the dot product in this case is a function mapping $\mathbb{R}^n$ into $\mathbb{R}$.
Hence, $p\cdot y$ and $\pi(p)$ are both real numbers.
The ``normed distance'' or metric distance in $\mathbb{R}$ is the absolute value, like we used in the calculus proofs on assignment 2.
So we will look at the difference $|p\cdot y - \pi(p)|$ over the points in a hyperplane and in $Y$.
As previously discussed we know that it is always true for elements of $Y$ that $p_0 \cdot y \leq \pi(p_0)$ implying that $p_0\cdot y  - \pi(p_0) \leq 0$.
The only vectors in $Y$ for which the difference will actually be zero are those vectors $y^*$ that are the profit maximizing production plans.
Okay so now consider again the hyperplane $\{ y \in \mathbb{R}^n: p_0 \cdot y = \pi(p_0)\}$.
If any point in the hyperplane is also an element of $Y$, then would it be a profit maximizing production plan? It would be.
So saying the hyperplane ``touches'' $Y$ is essentially saying they have an element in common (technically there are topological arguments that slightly loosen this, but we haven't covered those yet, so...).
In our case, as long as $\exists y^* \in Y$ such that $p_0\cdot y^* = \pi(p_0)$ that same point $y^*$ is also in the hyperplane and $|p_0 \cdot y^* - \pi(p_0)|=0$.
As for the question about $F(y)$, just ignore it I realize know I was asking too much on this.  The point here is that all points on the ``boundary'' of $Y$ will satisfy $F(y)=0$ and that the profit maximizing production plans, if any, will be on the boundary.
\textbf{So what}? Well we have now established that $Y$ is contained in $H_{p_0}^{\leq}(\pi(p_0))$ and that the hyperplane ``touches'' the boundary of $Y$ and all this essentially makes the hyperplane \emph{tangent} to the set $Y$ and we call it a \textbf{supporting hyperplane}.

For part (iii) remember what you learned about what it means for two sets to be equal
\[
  A = B \iff [A \subseteq B \wedge B \subseteq A]
\]
Now for all the previous discussion we were working with $p_0$, was there anything special about $p_0$?
No, we just picked it arbitrarily from the set $\mathbb{R}^n$ which means that the vector doesn't have any zeros in it -- no negative or zero prices -- sorry for a typo in the assignment.
Anyways, since we picked $p_0$ arbitrarily all our previous results should hold for all elements of the set that we drew $p_0$ from.
So for any $p \in \mathbb{R}^n$ we have a supporting hyperplane that contains $Y$ in its ``lower'' half-space and ``touches'' $Y$ on its boundary.
Remember that for any $p$ we can define $\pi(p) = \max\{p\cdot y: y \in Y\}$ and we could also create the hyperplane $H(p) = \{ y \in \mathbb{R}^n: p\cdot y = \pi(p)\}$ which means we could also create the ``lower`` half space that we know will contain $Y$, or $H_{p}^{\leq}(\pi(p))=\{ y \in \mathbb{R}^n : p\cdot y \leq \pi(p) \}$
Hopefully you remember from your reading on sets that we can represent \emph{indexed collections} of sets (set of sets) so that the set of all ``lower'' half-spaces could be represented as $\{ H_{p}^{\leq}(\pi(p)): p \in \mathbb{R}^n \}$ -- which is the same as $\{ A(p) : p \in \mathbb{R}^n\}$ from the problem.

Okay so we want to show the following
\[
	Y \subseteq \cap_{p \in \mathbb{R}^n} A(p) \text{ and } \cap_{p \in \mathbb{R}^n} A(p) \subseteq Y
\]
Let $y \in Y$. Then since $Y$ is contained in \text{every} half-space of the form $A(p)$ we know that it is in the intersection of all half-spaces of the form $A(p)$, right? why?
This means that $y$ is also in $\cap_p A(p)$.

The other direction is little more involved.
One way to go about this would be proof by contradiction.
Essentially, we assume, to the contrary, that $y \notin Y$.
If we can show that this implies that $\exists p$ such that $p\cdot y > \pi(p)$ then this means there exists some half-space $A(p)$ that doesn't contain $y$, which contradicts our primary assumption.

Let $y \in \cap_p A(p)$ and to the contrary let $y \notin Y$; what do we know?
Well this means that $y$ is in every half space of the form $\{ x \in \mathbb{R}^n : p\cdot x \leq \pi(p)\}$ so we know that $p\cdot y \leq \pi(p)$ for all $p$.
Even though $y \notin Y$ we can a pick a point $z\in Y$ that is ``closest'' to $y$ in the sense that $z$ minimizes the normed distance $\| y - z \|$.
Now the basic idea is that we can form a price vector, $p = y - z$ and think about value $\pi(p)$.
Lets create a value $c = p\cdot z$.
From this we can think of a hyperplane $H_{p}(c) = \{ x \in \mathbb{R}^n : p\cdot x = c \}$.
Because $p\cdot z = c$ and $z \in Y$ we know $z \in H_p(c)$ and that $H_p(c)$ touches $Y$.

Without going into all the details this ends up being the point.
You end up establishing that our vector $z \in Y$ that is closest to $y \notin Y$ ends up achieving the maximum $\pi(p)$ and that for all other vectors $v \in Y$, we have $p\cdot v \leq \pi(p)$ so that $Y$ is contained by the ``lower'' half-space of the hyperplane we created.
However, we find the following holds for the vector $y\in Y$.
\begin{align}
	p\cdot y - c &= p\cdot y - p\cdot z \nonumber \\
	&= p\cdot (y - z) \nonumber \\
	&= (y - z)\cdot (y - z) \nonumber \\
	&= \| y - z \|^2  \nonumber \\
	&> 0 \text{ since } y \neq z \nonumber
\end{align}
What this implies is that $p\cdot y > \pi(p)$.
So essentially what you'll end up showing, after more work than I now expect you to do, is that we created a price $p$ with a hyperplane $\{ x \in \mathbb{R}^n : p\cdot x = \pi(p)\}$ that \textbf{separates} the set $Y$ from the point $y\notin Y$.
This implies that $y$ is not in the lower half-space and hence couldn't be in the intersection $\cap_p A(p)$ which is where we get our contradiction.

\textbf{Duality}  The idea of duality is that given technology $Y$ you can use the space of prices to create the profit (value) function $\pi(p)$ which tells you the profit of the profit maximizing firm with technology $Y$ and prices $p$.
If instead we know $\pi(p)$, but not $Y$, we can construct $Y$ (or at least the convex hull of it) using our knowledge of $\pi(p)$ and the half-spaces it generates for us.

\textbf{You'll encounter hyperplanes, half-spaces, convex sets, duality and so on several times in consumer theory, producer theory and general equilibrium as it plays a large role in convex optimization problems}

\end{document}


\documentclass{standalone}
\begin{document}
	\chapter{Complex Numbers}
	\section{Loci}
	$\quad$ Let $z=x+yi$ where the complex number $z$ is represented on the Argand diagram by the line $OP$ where $P$ is the point $(x,y)$. In general, as $x$ and $y$ are variable $P$ can be anywhere on the Argand diagram. Suppose however that a condition is imposed on $z$. Consider the case where $\abs{z} = 4$. In this case the position of $P$ is restricted such that the line $OP$ is of a constant length of 4 units. Thus, $P$ can lie anywhere on the circumference of a circle centre $(0,0)$ with radius 4.\\\\
	\quad Thus, the locus of $P$ is the circle with equation:
	\begin{center}
		\begin{tcolorbox}[before = \centering, center title,hbox,    %%<<---- here
			lifted shadow={1mm}{-2mm}{3mm}{0.1mm}%
			{black!50!white}]
			\begin{varwidth}{300pt}
				\begin{align*}	&&x^2+y^2 &= 4^2 \qquad\text{Cartesian form}\\
					&   & \abs{z} & = 4 \qquad\text{Complex form}
				\end{align*}
			\end{varwidth}
		\end{tcolorbox}
	\end{center}

	Alternatively, we can say that if $\abs{z} = r^2$

	\begin{example}
		If $z=x+yi$ and $P$ is the point $(x,y)$ , find the locus of $P$ such that:\\
		$\qquad\qquad$a) $\abs{x-4} = 5$\\
		$\qquad\qquad$ b) $\abs{x+2-i} = 7$
	\end{example}
	\begin{example}
		Find the locus of $z$ if $\abs{z-1} = k\abs{z+4}$ when $k=1, k=3$
	\end{example}
	Let $z=x+yi$
	\begin{multicols}{2}
		\begin{center}
			\begin{alignat*}{2}
				&                  & \abs{x+yi-1}         & = \abs{x+yi+4}         \\
				& \implies\quad    & \abs{(x-1) + yi}     & = \abs{(x+4) + yi}     \\
				& \implies\quad    & \sqrt{(x-1)^2 + y^2} & = \sqrt{(x+4)^2 + y^2} \\
				& \implies\quad    & (x-1)^2 + y^2        & = (x+4)^2 + y^2        \\
				& \implies \quad   & x^2 - 2x + 1         & = x^2+8x+16            \\
				& \therefore \quad & x                    & = \frac{-3}{2}
			\end{alignat*}
		\end{center}

		\begin{center}
			\includegraphics[scale=0.7]{ex1a complex loci}
		\end{center}
	\end{multicols}

	\begin{example}
		Given $\abs{\frac{z-1}{z+4i}} = 2 $ , find the locus of $z$.
	\end{example}
	Remembering: $\abs{\frac{z_1}{z_2}} = \frac{\abs{z_1}}{\abs{z_2}}$

	Let $z = x+yi$
	\begin{alignat*}{2}
		&                & \abs{\frac{z-1}{z+4i}} & = 2                   \\
		& \implies \quad & \abs{z-1}              & = 2\abs{z+4i}         \\
		& \implies \quad & \abs{(x-1)+yi}         & = 2\abs{x+(y+4)i}     \\
		& \implies \quad & \sqrt{(x-1)^2+y^2}     & = 2\sqrt{x^2+(y+4)^2} \\
		& \implies \quad & \sqrt{(x-1)^2+y^2}     & = 2\sqrt{x^2+(y+4)^2} \\
	\end{alignat*}
	\hrulefill
	\begin{example}
		Given that $z_A = \frac{1}{10} (-1+i)$ and $z_B = -\frac{1}{500} (11+127i)$ , find in the form a+bi the complex numbers $\frac{z_A}{z_B}$.
		If $P(x,y)$ is the point on the Argand diagram representing $z=x+yi$, determine the equation of the locus of $P$ where $\abs{z-z_A} = \abs{z-z_B}$.
	\end{example}
	$$\frac{z_A}{z_B}  =  \frac{1}{10} (-1+i) \div -\frac{1}{500} (11+127i)$$
	\begin{alignat*}{2}
		&   &                                     & =-5\cdot \frac{-1+i}{11+127i}                         \\
		&   &                                     & = \frac{5-5i}{11+127i} \cdot  \frac{11-127i}{11-127i} \\
		&   &                                     & = a+bi                                                \\
		&   & \therefore \quad a=-\frac{58}{1625} & ,\quad  b=-\frac{69}{1625}                            \\
		&   & \abs{z-z_A}                         & = \abs{z-z_B}
	\end{alignat*}
	\newpage
	\begin{example}
		If the real part of $\dfrac{z+2}{z+2i}$ is equal to 1, show that the point $z$ lies on a straight line. Hence find the point $z_0$ on this line such that  $\abs{z_0} = \sqrt{2}$. Find also the quadratic equation with real coefficients which has $z_0$ as one of the roots.
	\end{example}

	Let $z = x+yi$\\
	\textbf{Showing that $\boldsymbol{z}$ lies on a straight line: }
	\begin{align*}
		&   & Re(\frac{z+2}{z+2i})           & = 1                                                                       \\
		&   & \implies \quad 1               & = Re(\frac{x+2+yi}{x+2i+yi})                                              \\
		&   & \implies \quad 1               & = Re\left(\frac{(x+2)+yi}{x+(y+1)i}\cdot \frac{x-(y+1)i}{x-(y+1)i}\right) \\
		&   & \implies \quad 1               & = Re\left(\frac{x(x+2) -i(y+2)(x+2) + iyx + y(2+y}{x^2 + (y+2)^2}\right)  \\
		&   & \implies \quad 1               & = \frac{x(x+2) + y(y+2)}{x^2 + (y+2)^2}                                   \\
		&   & \implies \quad x(x+2) + y(y+2) & = x^2  + y^2+4y+4                                                         \\
		&   & \implies \quad 2y              & = 2x-4                                                                    \\
	\end{align*}
	$$\therefore \quad y = x-2 \qed$$
	$$\text{Point } z \text{ lies on the straight line } y = x-2$$

	\begin{example}
		Shade on an Argand diagram the area represented by $\abs{z+i} < 4$.
	\end{example}~\\
	\setlength{\columnseprule}{0pt}
	\textbf{Finding the loci: }
	\begin{multicols}{2}
		\begin{center}
			\begin{align*}
				&   & \abs{z+i} & = \abs{x+(y+1)i}                                   \\
				&   &           & =\sqrt{x^2  + (y+1)^2}                             \\
				&   &           & =\sqrt{(x-0)^2 + (y-(-1))^2}                       \\
				&   &           & \text{ is the distance from point (0,-1) to (x,y)}
			\end{align*}
		\end{center}
		\begin{center}
			\includegraphics[scale=0.4]{complex_loci_ex5_circle}
		\end{center}
	\end{multicols}

	\textbf{Finding $\boldsymbol{z_0= (a,b) = a+bi\colon}$}
	\reqnomode

	\begin{alignat*}{2}
		&               & z_0           & = a+bi           \\
		& \implies      & \abs{z_0}     & = \sqrt{a^2+b^2} \\
		\intertext{Given:  $\abs{z_0}= \sqrt{2}$}
		& \implies\quad & \sqrt{a^+b^2} & = \sqrt{2}       \\
		& \implies\quad & a^2+b^2       & = 2\tag{1}       \\
		\intertext{We also know $z_0$ is on the line $y=x-2$}
		\therefore    b  = a-2\tag{2}
		\intertext{Substituting 2 in 1: }
		&               & a^2 + (a-2)^2 & = 2              \\
		& \implies\quad & 2a^2 - 4a+4   & = 2              \\
		& \implies\quad & a^2-2a+1      & =0               \\
	\end{alignat*}
	$$	\therefore\quad z_0 = 1-i \qed$$\\
	\textbf{Finding quadratic equation with roots: $\boldsymbol{z_0, \bar{z_0}}$}
	\begin{center}
		$$x^2 - (\text{sum of roots})x + (\text{product of roots}) = 0$$
		$$\implies x^2 - (1-i + 1+i)x + (1-i)(1+i)=0$$
		$$\implies x^2-2x+2 = 0\qed$$
	\end{center}

\end{document}


\section{Notes of The Hundred Page Machine Learning Book}
\label{section:100PageNotes}

This book was chosen as it is a very popular read, and is described as a good introduction to machine learning. Its concise nature allows a general understanding, and was selected to be my first read in my deeper endeavour of ML\\

Machine learning algorithms are often described as "supervised", or "unsupervised". There are also "semi-supervised" and "reinforcement" machine learning algorithms.\\
\newpage
\begin{itemize}
	\item \textbf{Supervised Models} uses labeled sets of data to train an algorithm, and by iterating can predict more accurately. They often take feature vectors in, and output information accordingly. It is the most commonly used learning method.  The data is supervised to (input, output). \\
	\item \textbf{Unsupervised Models} are in charge of identifying patterns of unlabeled data. There is also a feature vector given into this learning alternative. \\
	\item \textbf{Semi-Supervised Models} is a mix of labelled and unlabelled data. The end goal is to build a strong algorithm. \\
	\item \textbf{Reinforcement Learning} is interpreted as a state, that can execute actions at every state. Depending on actions, a reward system is engaged accordingly. This helps a computer algorithm decipher policy. This is measured through excepted average reward. \\
\end{itemize}

Classification and Regression are often mixed terms. In our terms, \textbf{classification} entails automatically assigning labels to unlabeled data, where the then-labeled data can help develop a model. For example spam detection. For \textbf{regression} is the pursuit of predicting a label based off an unlabeled example. For example, judging a house price, based off location, number of bedrooms, area etc. \\

Machine Learning algorithms can be manually developed, however the industry standard is known to leverage libraries to source their work. \\

A \textbf{Neural Network} is a nested function,

\begin{equation}
    y = f_N_N(x) = f_3(f_2(f_1(x)))
\end{equation}
\\
hence the common statement of "layers" within a neural network. The vector functions follow the form,

\begin{equation}
    f_l(z)=g_l(W_lz + b_l)
\end{equation}

where $l$ is the the layer index (spans from 1 to any number of layers), and $g_l$ is the activation function. The parameters $W_l$ is a matrix, $b_l$ is a vector.


\documentclass{article}
\usepackage{amsmath, amsfonts}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\begin{document}
\section{Spring Loaded Inverted Pendulum Dynamics}
For a \textit{spring loaded inverted pendulum} (SLIP) model, it has two modes, the flight phase and the stance phase. Its equation of motion is
\begin{itemize}
	\item Flight phase, state being $\mathbf{x}_{fl} = [x, z, \dot{x}, \dot{z}]^T$.
		\begin{align}
			\frac{d}{dt}
			\begin{bmatrix} x\\z\\\dot{x}\\\dot{z} \end{bmatrix} = f_{fl}(\mathbf{x}_{fl})
			=\begin{bmatrix}\dot{x}\\\dot{z}\\0\\-g\end{bmatrix}
		\end{align}
	\item Stance phase, state being $\mathbf{x}_{st}=[r, \theta, \dot{r}, \dot{\theta}, x_{foot}]^T$.
		\begin{align}
			\frac{d}{dt}\begin{bmatrix}r\\\theta\\\dot{r}\\\dot{\theta}\\\dot{x}_{foot}\end{bmatrix} = f_{st}(\mathbf{x}_{st})
			=\begin{bmatrix}\dot{r}\\\dot{\theta}\\k(l_0-r)/m - g\cos\theta+r\dot{\theta}^2\\g/r\sin\theta - 2\dot{r}\dot{\theta}/r\\0\end{bmatrix}
		\end{align}
	\item touch down guard
		\begin{align}
			g_{TD}(\mathbf{x}, \mathbf{u}) = y -l_0\cos\theta \ge 0
		\end{align}
	\item lift off guard
		\begin{align}
			g_{LO}(\mathbf{x}) = l_0 - r \ge 0
		\end{align}
	\item touch down transition maps a pre impact state $\mathbf{x}_{TD}^-$ to a post-impact state $\mathbf{x}_{TD}^+$
		\begin{align}
			\mathbf{x}_{TD}^+ = \Delta_{TD}(\mathbf{x}_{TD}^-, \mathbf{u}) = \begin{bmatrix} l_0\\\theta\\ -\dot{x}\sin\theta+\dot{y}\cos\theta\\(-\dot{x}\cos\theta-\dot{y}\sin\theta)/l_0\\x+l_0\sin\theta\end{bmatrix}
		\end{align}
	\item lift off transition maps a pre-lift-off state $\mathbf{x}_{LO}^-$ to a post-lift-off state $\mathbf{x}_{LO}^+$
		\begin{align}
			\mathbf{x}_{LO}^+ = \Delta_{LO}(\mathbf{x}_{LO}^-) = \begin{bmatrix}x_{foot} - l_0\sin\theta\\ l_0\cos\theta\\-l_0\cos\theta\dot{\theta}-\dot{r}\sin\theta \\ -l_0\sin\theta\dot{\theta}+\dot{r}\cos\theta\end{bmatrix}
		\end{align}
\end{itemize}

\section{Return map}
We will build the apex-to-apex Poincar\'e return map. We denote $\mathbf{x}_{apex}[n]$ as the n'th state at the apex of the flight phase, and the apex map as $\mathbf{x}_{apex}[n+1] = \phi(\mathbf{x}_{apex}[n], \mathbf{u}[n])$. This return map is nonlinear, and we will linearize this return map around a nominal apex state $\mathbf{x}^*_{apex}$ and leg angle $\mathbf{u}^*$ to get a linear dynamic model. We will then build the linearized model at many linearization points, and approximate the nonlinear return map as hybrid linear systems.

In order to linearize the return map, we will use the following lemma
\begin{lemma}
	\label{lemma:gradient_x0}
	For a dynamical system $\dot{\mathbf{x}} = f(\mathbf{x})$ with initial state $\mathbf{x}_0$, at a fixed time $T$, the gradient of $\partial{\mathbf{x}(t)}/\partial \mathbf{x}_0$ can be computed through integrating the following ODE on the matrix $\partial\mathbf{x}/\partial\mathbf{x}_0$
	\begin{align}
		\frac{d}{dt}\frac{\partial\mathbf{x}}{\partial \mathbf{x}_0} = \frac{\partial f}{\partial\mathbf{x}}\frac{\partial \mathbf{x}}{\partial \mathbf{x}_0}
	\end{align}
	with the initial condition $\partial \mathbf{x}(0)/\partial\mathbf{x}_0 = I$.
\end{lemma}
\subsection{Linearization of return map}
In order to compute $\frac{\partial\phi}{\partial{\mathbf{x}}_{apex}}$ and $\frac{\partial\phi}{\partial \mathbf{u}}$, we would first compute the gradient of the pre-touch-down state. The pre-touchdown state $\mathbf{x}_{TD}^-$ is computed as
\begin{align}
	\mathbf{x}_{TD}^- = \int_0^{t_{TD}} f_{fl}(\mathbf{x}_{fl}) d\tau
\end{align}
Using Lemma \ref{lemma:gradient_x0}, we know
\begin{align}
	\frac{\mathbf{x}_{TD}^-}{\partial \mathbf{x}_{apex}} = \underbrace{I + \int_0^{t_{TD}} \frac{\partial f_{fl}}{\partial \mathbf{x}}\frac{\partial \mathbf{x}}{\partial \mathbf{x}_{apex}} d\tau}_{\left.\frac{\partial \mathbf{x}(t)}{\partial x_{apex}}\right|_{t=t_{TD}}} + f_{fl}(\mathbf{x}_{TD}^-)\frac{\partial t_{TD}}{\partial \mathbf{x}_{apex}}\\
	\frac{\mathbf{x}_{TD}^-}{\partial \mathbf{u}} = f_{fl}(\mathbf{x}_{TD}^-)\frac{\partial t_{TD}}{\partial \mathbf{u}}
\end{align}
Note that we have the term $f_{fl}(\mathbf{x}_{TD}^-)\frac{\partial t_{TD}}{\partial \mathbf{x}_{apex}}$ because the touch down time $t_{TD}$ is also a function of the initial apex state (and the control).

To compute the term $\frac{\partial t_{TD}}{\partial \mathbf{x}_{apex}}$ and $\frac{\partial t_{TD}}{\partial \mathbf{u}}$, we take a full derivative of the guard function $g_{TD}$. Since the touchdown always happens at $g_{TD} = 0$, we have
\begin{align}
	dg_{TD} = \frac{\partial g_{TD}}{\partial \mathbf{x}}\left.\frac{\partial \mathbf{x}(t)}{\partial \mathbf{x}_{apex}}\right|_{t=t_{TD}}d\mathbf{x}_{apex} + \frac{\partial g_{TD}}{\partial\mathbf{x}}f_{fl}(x^-_{TD})dt_{TD} + \frac{\partial g_{TD}}{\partial\mathbf{u}}d\mathbf{u} = 0
\end{align}
Hence we can compute $\frac{\partial t_{TD}}{\partial \mathbf{x}_{apex}}$ as
\begin{align}
	\frac{\partial t_{TD}}{\partial \mathbf{x}_{apex}} =-\left(\frac{\partial{g}_{TD}}{\partial \mathbf{x}}f_{fl}(\mathbf{x}_{TD}^-)\right)^{-1}\frac{\partial g_{TD}}{\partial \mathbf{x}}\left.\frac{\partial \mathbf{x}(t)}{\partial \mathbf{x}_{apex}}\right|_{t = t_{TD}}
\end{align}
and  $\frac{\partial t_{TD}}{\partial \mathbf{u}}$ as
\begin{align}
	\frac{\partial t_{TD}}{\partial \mathbf{u}} = -\left(\frac{\partial{g}_{TD}}{\partial \mathbf{x}}f_{fl}(\mathbf{x}_{TD}^-)\right)^{-1}\frac{\partial g_{TD}}{\partial \mathbf{u}}
\end{align}

We can then compute the gradient of the post-touch-down state $\mathbf{x}_{TD}^+$ w.r.t the initial apex state using chain rule as
\begin{align}
	\frac{\partial \mathbf{x}_{TD}^+}{\partial \mathbf{x}_{apex}} = \frac{\partial \Delta_{TD}}{\partial x}\frac{\partial \mathbf{x}_{TD}^-}{\partial \mathbf{x}_{apex}}\\
	\frac{\partial \mathbf{x}_{TD}^+}{\partial \mathbf{u}} = \frac{\partial \Delta_{TD}}{\partial x}\frac{\partial \mathbf{x}_{TD}^-}{\partial \mathbf{u}} + \frac{\partial \Delta_{TD}}{\partial \mathbf{u}}
\end{align}

\end{document}


\documentclass{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
\usepackage{parskip}
\usepackage{graphicx}

% Margins
\usepackage[top=2.5cm, left=3cm, right=3cm, bottom=4.0cm]{geometry}
% Colour table cells
\usepackage[table]{xcolor}

% Get larger line spacing in table
\newcommand{\tablespace}{\\[1.25mm]}
\newcommand\Tstrut{\rule{0pt}{2.6ex}}         % = `top' strut
\newcommand\tstrut{\rule{0pt}{2.0ex}}         % = `top' strut
\newcommand\Bstrut{\rule[-0.9ex]{0pt}{0pt}}   % = `bottom' strut

%%%%%%%%%%%%%%%%%
%     Title     %
%%%%%%%%%%%%%%%%%
\title{Research Assistant Crypto Response}
\author{Haile Lagi}
\date{September 26, 2020}

\begin{document}
\maketitle

%%%%%%%%%%%%%
% Finance   %
%%%%%%%%%%%%%

\section{Finance}
Yara Inc is listed on the NYSE with a stock price of 40 - the company is not known
to pay dividends. We need to price a call option with a strike of \$45 maturing in
4 months.
The continuously-compounded risk-free rate is 3\%/year, the mean return on the
stock is 7\%/year, and the standard deviation of the stock return is 40\%/year.
What is the Black-Scholes call price?

Parameters given are (assuming the stock doesn't pay dividend it is assumed an American call option is equivalent to an European call option):

Converting all parameters w.r.t. to 1 year:

Given that:
\begin{itemize}
\item Black-Scholes call price = $(C)$
\item  stock price$(S)$ = \$40
\item call option strike$(X)$ = \$45
\item  maturity$(T)$ = 4 months / 12 = 1/3
\item continuously-compounded risk-free rate$(r_{f})$ = 3/100 = 0.03
\item stock mean return$(\mu)$ = 7/100 = 0.07
\item standard deviation($\sigma$) = 40/100 = 0.4
\end{itemize}

the linear black-scholes general form for an European style call option is:
\begin{align}
    \label{eq:call_option}
    C(S, T) = SN(x_{1}) - BN(x_{2})
\end{align}

Where:
\begin{align}
   Bond price (B) = Xe^-r_{f} * T
\end{align}

and $x_{1}$ is given by:

\begin{align}
  x_{1} = \frac{log(S/B)}{\sigma \sqrt{T}} + \frac{1}{2}\sigma \sqrt{T}
\end{align}

and to find $x_{2}$ from the value of $x_{1}$:

\begin{align}
  x_{2} = x_{1} - \sigma * \sqrt{T}
\end{align}

To find the value of $N(x)$ which is the cumulative normal distribution we use the integral form:

\begin{align}
    \label{eq:gaussian}
    N(x) = \frac{1}{\sqrt(2\pi)}\int_{-\infty }^{x}e^{\frac{-u^2}{2}}du
\end{align}

First we find $B = Xe^-r_{f} * T = 40e^-0.03 * \frac{1}{3} $
$B = 44.552243$

Using this we find $x_{1}$

\begin{align}
  x_{1} = \frac{log(40/44.552243)}{0.4 \sqrt{1/3}} + \frac{1}{2} * 0.4 * \sqrt{1/3}
\end{align}

\begin{align}
  = \frac{-0.10778304645914898}{0.2309401076758503} + (0.5 * 0.4 * 0.2309401076758503)
\end{align}

\begin{align}
  x_{1}  \approx
  -0.3512439362402078046572242 \approx -0.3512439
\end{align}

\begin{align}
  x_{2}  = -0.3512439362402078046572242 - 0.4 * \sqrt{1/3} \approx
  -0.5821840439160581104608837 \approx -0.5821840
\end{align}

Now we must find the Gaussian probability distribution functions of $N(x_{1})$ and $N(x_{2})$

\begin{align}
    \label{eq:gaussian_x_one}
    N(x_{1}) = \frac{1}{\sqrt(2\pi)}\int_{-\infty }^{-0.3512439}e^{\frac{-u^2}{2}}du
\end{align}

\begin{align}
    \label{eq:gaussian_x_two}
    N(x_{1}) = \frac{1}{\sqrt(2\pi)}\int_{-\infty }^{-0.5821840}e^{\frac{-u^2}{2}}du
\end{align}

Sadly the cumulative difference function of a normal distribution does not have
a closed form and CANNOT BE SOLVED analytically. The accuracy of the values produced by each estimation methods varies in accuracy. I'll be using a $Z-table$ or $Standard Normal Talbe$

\begin{align}
  N(x_{1}) = 0.3527026 N(x_{2}) = 0.2802213
\end{align}

Then finally substitution into the general form:
\begin{align}
    C(S, T) = 40 * 0.3627026 - 44.55224 * 0.2802213
\end{align}

The Black-Scholes call price is \approx \$2.02361738929
\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%2345678901234567890123456789012345678901234567890123456789012345678901234567890
%        1         2         3         4         5         6         7         8

\documentclass[letterpaper, 10 pt, conference]{ieeeconf}  % Comment this line out if you need a4paper

%\documentclass[a4paper, 10pt, conference]{ieeeconf}      % Use this line for a4 paper

\IEEEoverridecommandlockouts                              % This command is only needed if
                                                          % you want to use the \thanks command

\overrideIEEEmargins                                      % Needed to meet printer requirements.

% See the \addtolength command later in the file to balance the column lengths
% on the last page of the document

% The following packages can be found on http:\\www.ctan.org
\usepackage{graphics} % for pdf, bitmapped graphics files
\usepackage{epsfig} % for postscript graphics files
\usepackage{mathptmx} % assumes new font selection scheme installed
\usepackage{times} % assumes new font selection scheme installed
\usepackage{amsmath} % assumes amsmath package installed
\usepackage{amssymb}  % assumes amsmath package installed
\usepackage{amsthm}

\newtheorem{myTheo}{Theorem}

\theoremstyle{remark}
\newtheorem{myrem}{Remark}

\title{\LARGE \bf
Geometric Part*
}

\author{Xiao Ming Xiu$^{1}$  \\% <-this % stops a space
\\
\footnotesize\textit{\today}
\thanks{*This work was supported by Standard-Robots Ltd.,co.}% <-this % stops a space
\thanks{$^{1}$Xiao Ming Xiu is with Visual SLAM Algorithm Group, Standard-Robots Ltd.,co. Shenzhen, China.
        {\tt\small stevenxiu@aliyun.com}}%
}

\begin{document}

\maketitle
\thispagestyle{empty}
\pagestyle{empty}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{abstract}
Given 3 2-D points' coordinates in two different Reference Systems, Barcode System (BS) and Camera System (CS), output two things:  $\vec{t}$ and $\theta$, defined as the CS origin's coordinates in BS and the rotated angle of CS relative to BS.

\end{abstract}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Problem Setting}
\subsection{Given}
Notation: $$ BS --- S $$  $$ CS --- S' $$

Three Points' coordinates in both systems are known:
$$ P1:  (x_1,y_1), (x_1',y_1') $$
$$ P2:  (x_2,y_2), (x_2',y_2') $$
$$ P3:  (x_3,y_3), (x_3',y_3') $$

\subsection{Want}
$$\vec{t} = (t_x, t_y)$$
$$ \theta $$

where, \\$\vec{t}$ is defined by S' origin in S.\\
$\theta$ is defined by

\begin{equation}
\begin{aligned}
(\epsilon_x',\epsilon_y')&= (\epsilon_x,\epsilon_y)*R\\
						&= (\epsilon_x,\epsilon_y)*
\begin{bmatrix}
\cos{\theta}&-\sin{\theta}\\
\sin{\theta}&cos{\theta}
\end{bmatrix}
\end{aligned}
\end{equation}

($\epsilon_x, \epsilon_y$ denotes the basis vector in S.  $\epsilon_x', \epsilon_y'$ denotes the basis vector in S'.)

\subsection{Relationship Equation}

Denote $\vec{P}$ (column vector)as vector coordinate representation in S and $\vec{P}'$ in S'. We have

\begin{equation}
\vec{P} = \vec{t} + R*\vec{P}'
\end{equation}

Write in explicit form:

\begin{equation}
\begin{bmatrix}
x\\
y
\end{bmatrix}
=
\begin{bmatrix}
t_x \\
t_y
\end{bmatrix}
+
\begin{bmatrix}
\cos{\theta}&-\sin{\theta}\\
\sin{\theta}&cos{\theta}
\end{bmatrix}
*
\begin{bmatrix}
x' \\
y'
\end{bmatrix}
\end{equation}

Eq. (3) is necessary and sufficient.\\
You have 3 unknown variables, $t_x, t_y, \theta$. Given 1 pair coordinates $\vec{P}$ and $\vec{P}'$, you will get 2 independent equations. At least,  2 points are needed to solve all the 3 variables, though 1 equation is over determined. \\

\section{Question: what is minimal set of input sufficient to solve?}
\begin{myTheo}
Given 6 Rits of $x_1,y_1,x_1',y_1',x_2,x_2'$ is sufficient.
\end{myTheo}
3 independent equations have already been prepared up from Eq.(3). Only left with a Two-fold ambiguity.\\

\begin{myrem}
The set of 1.5 points, not 2,  is the minimum input.
\end{myrem}

\begin{myrem}
Do there exist the following case: the input given format cannot exactly fit the sufficient and necessary solution of Problem, at least have some input as overdetermined condition and called unavoidable waste input?
\end{myrem}

\section{Solution}
Eliminate $\theta$ first from Eq. (3) by Left Multiply (based on $R^T*R=I$) :
( Eq. (3) represents 2 equations.)
\begin{equation}
(x-t_x)^2 + (y-t_y)^2 = {x'}^2 + {y'}^2
\end{equation}

Rely on Point P1 and P2.  Substitute Eq.(4) as:
\begin{equation}
(x_1-t_x)^2 + (y_1-t_y)^2 = {x_1'}^2 + {y_1'}^2
\end{equation}
\begin{equation}
(x_2-t_x)^2 + (y_2-t_y)^2 = {x_2'}^2 + {y_2'}^2
\end{equation}

Use Eq.(5)- Eq.(6), we get

\begin{equation}
t_y=k t_x + b
\end{equation}

where
\begin{equation}
k=-\frac{x_1 - x_2}{y_1 - y_2}
\end{equation}
\begin{equation}
\begin{aligned}
b=&{\frac{1}{2}}*\{ y_1 + y_2 \\
&- {\frac{1}{y_1 - y_2}}[x'_1^2 + y'_1^2 - x'_2^2 -y'_2^2 -x_1^2 + x_2^2]\}
\end{aligned}
\end{equation}

Substitute $t_y$ in Eq. (5)
\begin{equation}
At_x^2 + Bt_x + C =0
\end{equation}
Where,
\begin{equation}
\begin{aligned}
A&=1+k^2 \\
B&=-2[x_1 + k(y_1 -b)]\\
C&= x_1^2 + (y_1 -b)^2 - x_1'^2 - y_1'^2
\end{aligned}
\end{equation}

Solve this quadratic equation
$$\Delta = B^2 - 4AC$$

\begin{equation}
t_x= \frac{-B \pm \sqrt{\Delta} }{2A}
\end{equation}

The corresponding $t_y$ can be derived from Eq. (7).

Here is a two-fold ambiguity. Only one of the roots are correct. Check them by the third point P3:

\begin{equation}
(x_3-t_x)^2 + (y_3 - t_y)^2 = x'_3^2 + y'_3^2
\end{equation}

Define
\begin{equation}
\delta_{check} = |(x_3-t_x)^2 + (y_3 - t_y)^2  - ( x'_3^2 + y'_3^2 )| ?=? 0
\end{equation}

The right root will satisfy Eq. (13). In  practice we can set the threshold of $\delta_{check}$.

\section{Experiments of Oct.17}
In the evening of Oct.17, a 2cm translation experiment was conducted.

    \begin{figure}
    \begin{minipage}[t]{0.5\linewidth}
    \centering
    \includegraphics[width=1.6in]{0cm1017.jpg}
    \caption{0cm Case No Lean}
    \label{fig:side:a}
    \end{minipage}%
    \begin{minipage}[t]{0.5\linewidth}
    \centering
    \includegraphics[width=1.6in]{2cm1017.jpg}
    \caption{2cm Case No Lean}
    \label{fig:side:b}
    \end{minipage}
    \end{figure}

\subsection{2cm case data}

Notice you need to transform the S' pixel unit to cm.

\begin{table}[h]
\caption{2cm1017.jpg's P1,P2,P3 }
\label{table_example}
\begin{center}
\begin{tabular}{|c|c|c|}

\hline
2cmNoLean & BS (cm)& CS(pixel)\\
\hline

P1 & (0,0)& (267,455) \\
\hline

P2 & $(3*cos(30^\circ),3*sin(30^\circ))$ & (271,256) \\

\hline
P3 & $(-3*cos(30^\circ),3*sin(30^\circ))$ & (466,460)\\
\hline
\end{tabular}
\end{center}
\end{table}

Output:

$$t_x=7.94855$$
$$t_y=-0.215983$$
$$\theta=120^\circ$$

\subsection{0cm case data}

\begin{table}[h]
\caption{0cm1017.jpg's P1,P2,P3 }
\label{table_example}
\begin{center}
\begin{tabular}{|c|c|c|}

\hline
0cmNoLean & BS (cm)& CS(pixel)\\
\hline

P1 & (0,0)& (399,458) \\
\hline

P2 & $(3*cos(30^\circ),3*sin(30^\circ))$ & (406,257) \\

\hline
P3 & $(-3*cos(30^\circ),3*sin(30^\circ))$ & (599,463)\\
\hline
\end{tabular}
\end{center}
\end{table}

Output:

$$t_x=8.82632$$
$$t_y=-2.04685$$
$$\theta=118.005^\circ$$

\subsection{Translation Calculation}
\begin{equation}
\begin{aligned}
Translation&=\sqrt{ (7.94855-8.82632)^2 +(-0.215983 + 2.04685)^2}\\
		   &=2.03 cm
\end{aligned}
\end{equation}

The real translation is just 2 cm. So the precision has attained to 0.3mm.

\section{Experiments of Oct.19}
In the evening of Oct.19, just use the data collected in Sep., to check the algorithm again in Lean case.

    \begin{figure}
    \begin{minipage}[t]{0.5\linewidth}
    \centering
    \includegraphics[width=1.6in]{0cm96Lean.jpg}
    \caption{0cm Case No Lean}
    \label{fig:side:a}
    \end{minipage}%
    \begin{minipage}[t]{0.5\linewidth}
    \centering
    \includegraphics[width=1.6in]{1cm96Lean.jpg}
    \caption{2cm Case No Lean}
    \label{fig:side:b}
    \end{minipage}
    \end{figure}

\end{document}


\section{Result}

\section{Results}

\subsection{Measurement for natural angular frequency}
We calculate the angular frequency from Table~\ref{data_omega} by the formula
\[
\omega_0=\frac{2\pi}{T}.
\]
\begin{table}[H]
\centering
\begin{tabular}{|c|c|}
\hline
& $10T[s] \pm 0.001[s]$\\\hline
1 & 15.790  \\\hline
2 & 15.801  \\\hline
3 & 15.778  \\\hline
4 & 15.800  \\\hline
\end{tabular}
\caption{Measurement of ten periods for the natural frequency}
\label{data_omega}
\end{table}

Hence the average value of $10T$ should be calculated as

\[
\overline{10T}=\frac{1}{4}\sum_{i=1}^{4}(10T)_i=15.79225 \pm 0.0107 \ s, \quad
u_{10T,r}=0.07\%.
\]

The value of $\omega_0$ is

\[
\overline{\omega_0}=\frac{20\pi}{10T}=\frac{20\times3.1416}{15.79225}= 3.9787
\pm 0.006 \  rad/s,\quad u_{\omega_0,r}=0.15\%.
\]

\subsection{Measurement for damping coefficient}

The damping coefficient can be calculated by the following formula.

\[
\beta=\frac{1}{5T}\ln\frac{\theta_i}{\theta_{i+5}}.
\]

In the experiment, we choose Damping Selection 2.

\begin{table}[H]
\centering

\begin{tabular}{|c|c|c|c|c|}
\hline

\multicolumn{2}{|c}{Amplitude[$\degree$]$\pm$1[$\degree$]} &
\multicolumn{2}{|c|}{Amplitude[$\degree$]$\pm$1[$\degree$]} &
$\ln(\theta_i/\theta_{i+5})$\\\hline

$\theta_0$ & 149 & $\theta_5$ & 95 & 0.4501 \\\hline
$\theta_1$ & 136 & $\theta_6$ & 86 & 0.4583 \\\hline
$\theta_2$ & 124 & $\theta_7$ & 79 & 0.4508 \\\hline
$\theta_3$ & 114 & $\theta_8$ & 72 & 0.4595 \\\hline
$\theta_4$ & 103 & $\theta_9$ & 65 & 0.4603 \\\hline
\multicolumn{4}{|c|}{The average value of $\ln(\theta_i/\theta_{i+5})$}
    & 0.4558 \\\hline
\end{tabular}

\caption{Measurement of the damping coefficient for Damping Selection 2}
\label{data_damping}
\end{table}

The experimental value of $\ln(\theta_i/\theta_{i+5})$ is shown below

\[
\ln(\theta_i/\theta_{i+5})= 0.4558 \pm 0.0050 , \quad u_r=1.09\%
\]

Here, $T= 15.79225 /10= 1.5792 \pm 0.0001s$. Then we can easily obtain $\beta$
as well,

\[
\beta=\frac{1}{5\times1.5792}\times0.4558=  0.0577 \pm 0.003s^{-1}, \quad
u_{\beta,r}=5.2\%
\]

\subsection{Measurement for $\theta_{st}$ vs. $\omega$ and $\varphi$ vs.
  $\omega$}

To study the relation between $\varphi$ and $\omega/\omega_0$,

first we get the raw data of 10T, $\phi$ and $\theta$,
we then process the raw data and list them in Table~\ref{data_phi2} and
Table~\ref{data_phi3}.

\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|c|}
\hline
& $10T [s] \pm 0.001 [s]$ &  $\varphi  [\degree]  \pm 1 [\degree]$ & $ \theta [\degree] \pm 1 [\degree]$ \\ \hline

1  & 15.098 & -162  &  38   \\ \hline
2  & 15.123 & -163  &  39   \\ \hline
3  & 15.542 & -143  &  87   \\ \hline
4  & 15.672 & -118  &  130  \\ \hline
5  & 15.707 & -109  &  138  \\ \hline
6  & 15.724 & -103  &  141  \\ \hline
7  & 15.736 & -100  &  143  \\ \hline
8  & 15.755 & -94   &  145  \\ \hline
9  & 15.763 & -92   &  146  \\ \hline
10 & 15.774 & -89   &  144  \\ \hline
11 & 15.788 & -86   &  144  \\ \hline
12 & 15.797 & -84   &  144  \\ \hline
13 & 15.810 & -82   &  144  \\ \hline
14 & 15.820 & -79   &  142  \\ \hline
15 & 15.841 & -75   &  140  \\ \hline
16 & 15.907 & -64   &  130  \\ \hline
17 & 15.946 & -57   &  124  \\ \hline
18 & 16.050 & -46   &  105  \\ \hline
19 & 16.131 & -40   &  92   \\ \hline
20 & 16.237 & -33   &  78   \\ \hline
21 & 16.472 & -22   &  54   \\ \hline
22 & 16.603 & -18   &  46   \\ \hline
\end{tabular}
\caption{$\theta$ vs. $10T$ and $\varphi$ vs. $10T$ raw data for Damping
  selection 2}
\end{table}

\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|c|}
\hline
& $10T [s] \pm 0.001 [s]$ &  $\varphi  [\degree]  \pm 1 [\degree]$ & $ \theta [\degree] \pm 1 [\degree]$ \\ \hline

1  & 15.057   & -163 &  35   \\ \hline
2  & 15.302   & -155 &  50   \\ \hline
3  & 15.526   & -142 &  79   \\ \hline
4  & 15.657   & -123 &  108  \\ \hline
5  & 15.708   & -111 &  120  \\ \hline
6  & 15.745   & -104 &  125  \\ \hline
7  & 15.760   & -97  &  127  \\ \hline
8  & 15.794   & -93  &  128  \\ \hline
9  & 15.789   & -92  &  128  \\ \hline
10 & 15.803   & -89  &  128  \\ \hline
11 & 15.814   & -86  &  128  \\ \hline
12 & 15.830   & -83  &  128  \\ \hline
13 & 15.849   & -80  &  126  \\ \hline
14 & 15.883   & -74  &  124  \\ \hline
15 & 15.903   & -69  &  120  \\ \hline
16 & 15.933   & -62  &  114  \\ \hline
17 & 16.013   & -55  &  106  \\ \hline
18 & 16.102   & -47  &  93   \\ \hline
19 & 16.168   & -41  &  83   \\ \hline
20 & 16.290   & -33  &  70   \\ \hline
21 & 16.452   & -25  &  55   \\ \hline
22 & 16.585   & -18  &  46   \\ \hline
\end{tabular}
\caption{$\theta$ vs. $10T$ and $\varphi$ vs. $10T$ raw data for Damping
  selection 3}
\end{table}

Then We know that $ \omega / \omega_0 = T_0 / T $,
thus we can have the following processed data.

\begin{figure}
  \centering
\begin{minipage}{0.45\textwidth}
\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|}
\hline
& $\omega/\omega_0$ &  $\varphi \pm 1 [\degree] $ \\ \hline
1  & 1.0460  & -162  \\ \hline
2  & 1.0443  & -163  \\ \hline
3  & 1.0161  & -143  \\ \hline
4  & 1.0077  & -118  \\ \hline
5  & 1.0054  & -109  \\ \hline
6  & 1.0043  & -103  \\ \hline
7  & 1.0036  & -100  \\ \hline
8  & 1.0024  & -94   \\ \hline
9  & 1.0019  & -92   \\ \hline
10 & 1.0012  & -89   \\ \hline
11 & 1.0003  & -86   \\ \hline
12 & 0.9997  & -84   \\ \hline
13 & 0.9989  & -82   \\ \hline
14 & 0.9982  & -79   \\ \hline
15 & 0.9969  & -75   \\ \hline
16 & 0.9928  & -64   \\ \hline
17 & 0.9904  & -57   \\ \hline
18 & 0.9839  & -46   \\ \hline
19 & 0.9790  & -40   \\ \hline
20 & 0.9726  & -33   \\ \hline
21 & 0.9587  & -22   \\ \hline
22 & 0.9512  & -18   \\ \hline
\end{tabular}
\caption{$\varphi$ vs. $\omega/\omega_0$，\\ Damping selection
  2}\label{data_phi2}
\end{table}
\end{minipage}
%
\begin{minipage}{0.45\textwidth}
\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|}
\hline
& $\omega/\omega_0$ &  $\varphi \pm 1 [\degree] $ \\ \hline
1  & 1.0488   & -163 \\ \hline
2  & 1.0320   & -155 \\ \hline
3  & 1.0171   & -142 \\ \hline
4  & 1.0086   & -123 \\ \hline
5  & 1.0054   & -111 \\ \hline
6  & 1.0030   & -104 \\ \hline
7  & 1.0020   &  -97 \\ \hline
8  & 0.9999   &  -93 \\ \hline
9  & 1.0002   &  -92 \\ \hline
10 & 0.9993   &  -89 \\ \hline
11 & 0.9986   &  -86 \\ \hline
12 & 0.9976   &  -83 \\ \hline
13 & 0.9964   &  -80 \\ \hline
14 & 0.9943   &  -74 \\ \hline
15 & 0.9930   &  -69 \\ \hline
16 & 0.9912   &  -62 \\ \hline
17 & 0.9862   &  -55 \\ \hline
18 & 0.9808   &  -47 \\ \hline
19 & 0.9768   &  -41 \\ \hline
20 & 0.9694   &  -33 \\ \hline
21 & 0.9599   &  -25 \\ \hline
22 & 0.9522   &  -18 \\ \hline
\end{tabular}
\caption{$\varphi$ vs. $\omega/\omega_0$，\\ Damping selection
  3}\label{data_phi3}
\end{table}
\end{minipage}
\vspace*{5cm}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=1\textwidth]{matlab/p1}
\caption{Phase shift $\varphi$ vs. $\omega/\omega_0$}
\label{phi}
\end{figure}

To study the relation between $\theta_{st}$ and $\omega/\omega_0$, we process the raw data and list them in Table~\ref{data_t2} and Table~\ref{data_t3}.

% TODO:

\begin{figure}
  \centering
\begin{minipage}{0.45\textwidth}
\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|}
\hline
& $\omega/\omega_0$ &  $\theta \pm 1 [\degree] $ \\ \hline
1  & 1.0460  & 38   \\ \hline
2  & 1.0443  & 39   \\ \hline
3  & 1.0161  & 87   \\ \hline
4  & 1.0077  & 130  \\ \hline
5  & 1.0054  & 138  \\ \hline
6  & 1.0043  & 141  \\ \hline
7  & 1.0036  & 143  \\ \hline
8  & 1.0024  & 145  \\ \hline
9  & 1.0019  & 146  \\ \hline
10 & 1.0012  & 144  \\ \hline
11 & 1.0003  & 144  \\ \hline
12 & 0.9997  & 144  \\ \hline
13 & 0.9989  & 144  \\ \hline
14 & 0.9982  & 142  \\ \hline
15 & 0.9969  & 140  \\ \hline
16 & 0.9928  & 130  \\ \hline
17 & 0.9904  & 124  \\ \hline
18 & 0.9839  & 105  \\ \hline
19 & 0.9790  & 92   \\ \hline
20 & 0.9726  & 78   \\ \hline
21 & 0.9587  & 54   \\ \hline
22 & 0.9512  & 46   \\ \hline
\end{tabular}
\caption{$\theta$ vs. $\omega/\omega_0$，\\ Damping selection 2}\label{data_t2}
\end{table}
\end{minipage}
%
\begin{minipage}{0.45\textwidth}
\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|}
\hline
& $\omega/\omega_0$ &  $\theta \pm 1 [\degree] $ \\ \hline
1  & 1.0488   & 35   \\ \hline
2  & 1.0320   & 50   \\ \hline
3  & 1.0171   & 79   \\ \hline
4  & 1.0086   & 108  \\ \hline
5  & 1.0054   & 120  \\ \hline
6  & 1.0030   & 125  \\ \hline
7  & 1.0020   & 127  \\ \hline
8  & 0.9999   & 128  \\ \hline
9  & 1.0002   & 128  \\ \hline
10 & 0.9993   & 128  \\ \hline
11 & 0.9986   & 128  \\ \hline
12 & 0.9976   & 128  \\ \hline
13 & 0.9964   & 126  \\ \hline
14 & 0.9943   & 124  \\ \hline
15 & 0.9930   & 120  \\ \hline
16 & 0.9912   & 114  \\ \hline
17 & 0.9862   & 106  \\ \hline
18 & 0.9808   & 93   \\ \hline
19 & 0.9768   & 83   \\ \hline
20 & 0.9694   & 70   \\ \hline
21 & 0.9599   & 55   \\ \hline
22 & 0.9522   & 46   \\ \hline
\end{tabular}
\caption{$\theta$ vs. $\omega/\omega_0$，\\ Damping selection 3}\label{data_t3}
\end{table}
\end{minipage}
\vspace*{5cm}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=1\textwidth]{matlab/p2}
\caption{Amplitude $\theta_{st}$ vs. $\omega/\omega_0$}
\label{theta}
\end{figure}


\documentclass{article}
\usepackage{theorem}

\DeclareMathAlphabet{\mathsc}{OT1}{cmr}{m}{sc}

\newcommand{\implies}{\Rightarrow}
\newcommand{\have}{{.~}}
\newcommand{\bstate}{{\mathcal{S}}}
\newcommand{\blocks}{{\mathcal{B}}}
\newcommand{\tabtop}{{\mathsc{t}}}
\newcommand{\tblocks}{{\blocks_\tabtop}}
\newcommand{\bclear}{{\mathsc{clear}}}
\newcommand{\bon}{{\mathsc{on}}}
\newcommand{\babove}{{\mathsc{above}}}
\newcommand{\bstack}{{\mathsc{stack}}}
\newcommand{\bbase}{{\mathsc{base}}}
\newcommand{\bflexed}{{\mathsc{flexed}}}
\newcommand{\st}{~|}

\begin{document}

\section{Definitions}

\begin{itemize}
\item Let
  $$ \blocks = \{ 1, \ldots, N \} $$
represent a set of $N$ blocks in a blocks-world problem.
We represent the table top with the symbol $\tabtop$, and
define $$ \tblocks \equiv \blocks \cup \{ \tabtop \} $$

\item Call any
  $$ \bstate \subseteq \blocks \times \tblocks $$
a {\em part-state} of the blocks-world problem. The pairs of blocks in
$\bstate$ represent {\em on} relations.

\item A part-state $\bstate$ is {\em legal} iff
  \begin{itemize}
  \item No block is on two things. $$
    \forall i \in \blocks \have
      \langle i , j \rangle \in \bstate \implies
      \not\exists j' \in \tblocks \have
	j \neq j' \wedge \langle i , j' \rangle \in \bstate
  $$
  \item No two blocks are on the same block. $$
    \forall j \in \blocks \have
      \langle i , j \rangle \in \bstate \implies
      \not\exists i' \in \blocks \have
	i \neq i' \wedge \langle i' , j \rangle \in \bstate
  $$
  \end{itemize}

\item Define the partial functions
$\bon_i(\bstate)$ mapping legal part-states to
blocks such that
$\bon_i(\bstate) = j$ when $\langle i, j \rangle \in \bstate$;
$\bon_i$ is undefined elsewhere.
(In other words, $\bon_i$ says
which block block $i$ is on in a given state.)
By the definition of a legal part-state, these functions are well-defined.

\item A part-state is a {\em total state} or just a {\em state}
iff it is legal and every block is on something. $$
    \forall i \in \blocks \have
      \exists j \in \tblocks \have
	\langle i , j \rangle \in \bstate
$$

\item The {\em above} relation is the transitive closure of
the {\em on} relation: A block $i$ is above a block $j$ in a
legal part-state $\bstate$ if either $i$ is on $j$, or $i$ is on
a block which is above $j$.  $$
  \babove_i(\bstate) = \left \{ \begin{array}{ll}
    \{ j \} \cup \babove_j(\bstate) &
      j = \bon_i(\bstate) \wedge j \neq \tabtop \\
    \emptyset & \mbox{otherwise}
  \end{array} \right .
$$

\item A block $j$ is {\em clear} in a legal part-state $\bstate$ iff no block
is above it. $$
  \bclear(\bstate) = \{ j \in \bstate \st
  \not\exists i \have \langle i , j \rangle \in \bstate \}
$$

\item A legal part-state $\bstate$ is {\em realizable} iff
no block is above itself. $$
  \not\exists i \in \blocks \have
    i \in \babove_i(\bstate)
$$

\item {\em Moves} are functions $m_{ij}$ which take a realizable part-state in
which blocks $i$ and $j$ are clear to a
realizable part-state in which $i$ is on $j$.

Let $i \in \blocks$ and $j \in \tblocks$, and let $\mathcal{C}_{ij}$ be the set
of realizable part-states
in which $i$ is clear, and either $j = \tabtop$ or $j$ is clear.
Then the domain of $m_{ij}$ is
$\mathcal{C}_{ij}$, and the range is the set of all realizable part-states.

We define the partial function $m_{ij}$ by $$
  m_{ij}(\bstate) \equiv
    \left ( \bstate - \{ \langle i, \bon_i(\bstate) \rangle \} \right )
    \cup \{ \langle i, j \rangle \}
$$
when $i$ and $j$ are clear in $\bstate$; $m_{ij}$ is undefined
elsewhere.  A move is {\em legal} iff $m_{ij}$ is defined.

\item A {\em move sequence} $M$
of length $k$ from state $\bstate^1$ to state $\bstate^2$ consists of
a sequence of pairs $$
  \langle i_1, j_1 \rangle , \langle i_2 , j_2 \rangle , \ldots ,
  \langle i_k , j_k \rangle \in \left ( \blocks \times \tblocks \right ) ^ \ast
$$
such that $$
  \left ( m_{i_k j_k} \circ \cdots \circ m_{i_2 j_2} \circ
  m_{i_1 j_1} \right ) (\bstate^1) = \bstate^2
$$
We allow ourselves the liberty of writing $M(\bstate^1) = \bstate^2$.
A move sequence $M$ is {\em legal} for a state $\bstate$ iff
$M(\bstate)$ is defined.

\item A {\em primitive-blocks-world} (PBW) {\em problem} consists of two
realizable states: an
initial state $I$ and a goal state $G$.
A {\em solution} to the problem is a move sequence $M$ such that $M(I) = G$.
We say that a solution is {\em optimal} if there is no solution
of shorter length.

\item A {\em stack} of blocks starting at $i$ in a
legal part-state $\bstate$ is just
$i$ plus the set of blocks $i$ is above. $$
  \bstack_i(\bstate) = \{ i \} \cup \babove_i(\bstate)
$$
We call $i$ the {\em top} of the stack.
If $\bstate$ is a realizable part-state, the {\em base}
of the stack is the block in the stack which is
above no other blocks in the stack. $$
  \bbase_i(\bstate) = j \in \bstack_i(\bstate) \have
    \babove_j(\bstate) \subseteq \{ \tabtop \}
$$
(The lemma that every stack in any realizable part state
has a base is proved by tracing through the definitions.)

\item A {\em tower} is a stack which is a subset of no other
stack.

\end{itemize}

\section{Basics}

We first prove that two states are the same exactly when
all the blocks have the same $\babove$ relation.
\begin{lemma}{same-above}{$\babove$ As A State Characterization}
\given
States $\bstate^1$ and $\bstate^2$.
\prove
$\bstate^1 = \bstate^2$ iff $$
  \forall i \in \blocks \have
    \babove_i(\bstate^1) = \babove_i(\bstate^2)
$$
\proof  By forward and backward implication.

\forwardcase
By definition, $\babove$ is a function.  Thus,
$\babove_i$ applied to any identical pair of states
must yield identical answers.

\backwardcase
We prove this case by contradiction.
Assume that $\bstate^1 = \bstate^2$, but there is some $i$ such that
$\babove_i(\bstate^1) \not= \babove_i(\bstate^2)$.
WLOG, assume that
there is some $j \in \tblocks$
such that $j \in \babove_i(\bstate^1)$ but not $j \in \babove_i(\bstate^2)$.
Then by definition of $\babove$, there must exist some
$k \in i \cup \babove_i(\bstate^1)$
such that $\bon_k(\bstate^1) = j$ but $\bon_k(\bstate^2) \not= j$.
But by definition of $\bon$, this implies that $\bstate^1 \not= \bstate^2$,
contradicting our original assumption.
\end{lemma}

\section{Flexibility}

A legal part-state $\bstate^2$ is {\em as flexible} as
a legal part-state $\bstate^1$ iff no block in $\bstate^2$
is above some block in $\bstate^2$ which it is not above in
$\bstate^1$, i.e. $$
  \babove_i (\bstate^2) \subseteq \babove_i(\bstate^1)
$$
We notate this by writing $\bstate^2 \sqsubseteq \bstate^1$.

An observation about states related by flexibility is in order.
\begin{lemma}{flexible-clear}{Flexibility And Clear Blocks}
\given
States $\bstate^1$ and $\bstate^2$ with $\bstate^2 \sqsubseteq \bstate^1$.
\prove
Any block clear in $\bstate^1$ is clear in $\bstate^2$: $$
  \bclear(\bstate^2) \supseteq \bclear(\bstate^1)
$$
\proof
Consider some block $j$ in $\bstate^2$.  If it is not clear,
then there is a block $i$ above it in $\bstate^2$.  But,
by definition of flexibility, this means that $i$ is above $j$
in $\bstate^1$, so $j$ is not clear in $\bstate^1$, either.
\end{lemma}

We now justify the terminology ``as flexible as''
by means of Theorem \ref{flexible-move}:
\begin{theorem}{flexible-move}{Flexibility And Move Sequences}
\given
States $\bstate^1$ and $\bstate^2$ with $\bstate^2 \sqsubseteq \bstate^1$.
A legal move sequence $M$ starting at $\bstate^1$.
\prove
\begin{description}
\item[i)~] $M$ is also a legal move sequence starting at $\bstate^2$.
\item[ii)~] $M(\bstate^2) \sqsubseteq M(\bstate^1)$.
\end{description}
\proof
By induction on prefixes of $M$.

\basecase
The theorem is true for the empty prefix of $M$.

\indcase
Decompose $M$ such that the prefix $M^1$ before
some move $m = \langle i, j \rangle$ satisfies the lemma $$
  M = M^1 \cdot \langle i, j \rangle \cdot M^2
$$
and let
\begin{eqnarray*}
  \bstate^1_1 &=& M^1(\bstate^1) \\
  \bstate^1_2 &=& \langle i, j \rangle(\bstate^1_1) \\
  \bstate^2_1 &=& M^1(\bstate^2) \\
  \bstate^2_2 &=& \langle i, j \rangle(\bstate^2_1)
\end{eqnarray*}

By hypothesis we have $\bstate^2_1 \sqsubseteq \bstate^1_1$.
By Lemma \ref{flexible-clear}, this means that $$
  \bclear(\bstate^1_1) \subseteq \bclear(\bstate^2_1)
$$
This means that since $\langle i, j \rangle$ is a legal
move in $\bstate^1_1$, it must also be a legal move in
$\bstate^2_1$.  Thus, condition (i) of the lemma holds
for the prefix $M^1 \cdot \langle i, j \rangle$ of $M$.

To see that condition (ii) of the Lemma holds on the extended
prefix, we note that the only block possibly above other blocks in
$\bstate^2_2$ but not in $\bstate^2_1$ is $i$.  We then note that
by definition of flexibility
\begin{eqnarray*}
  \babove_i(\bstate^2_2) &=& \babove_j(\bstate^2_1) \cup \{j\} \\
  &\subseteq& \babove_j(\bstate^1_1) \cup \{j\} \\
  &=& \babove_i(\bstate^1_2)
\end{eqnarray*}
\end{theorem}

If two states are related by flexibility, legal sequences of
moves tend to make the two states the same.  The next theorem
quantifies this observation.

Given a block $i$ and states $\bstate$ and $\bstate'$ such
that $\bstate' \sqsubseteq \bstate$, we will say that block
$i$ is {\em flexed} iff $i$ is above fewer blocks in
$\bstate'$ than in $bstate$. $$
  \babove_i(\bstate') \subset \babove_i(\bstate)
$$
We will write $\bflexed_{\bstate}(\bstate')$ for the
set of blocks in $\bstate'$ flexed with respect to $\bstate$.

\begin{theorem}{flexed-blocks}{Flexed Blocks}
\given States $\bstate^1_1$ and $\bstate^2_1$ such that
$\bstate^1_1 \sqsubseteq \bstate^2_1$.  A
move sequence $M$ such that $M(\bstate^1_1) = \bstate^1_2$
and $M(\bstate^2_1) = M(\bstate^2_2)$.
\prove
The final states are the same whenever all the flexed blocks
in the initial state have moved, i.e.,
$\bstate^2_1 = \bstate^2_2$ iff $$
  \forall i \in \bflexed_{\bstate^2_1}(\bstate^1_1) \have
    \exists j \in \tblocks \have
      \langle i, j \rangle \in M
$$
\proof  By forward and backward implication.

\forwardcase
We first prove that when the final states are the same, all
flexed blocks have moved.
By Lemma \ref{same-above} two states are the same exactly when the
$\babove$ relations of all of their blocks are the same.
Now consider any block $i$
which is flexed in a state $\bstate^1$ WRT a state $\bstate^1_1$. $$
  i \in \bflexed_{\bstate^1_1}(\bstate^1)
$$
Assume that a sequence $M$ of moves takes both
initial states $\bstate^1$ and $\bstate^1_1$
to the same final state $\bstate^2$. $$
  \bstate^2 = M(\bstate^1) = M(\bstate^1_1)
$$
By the definition of flexed
block, the $\babove$ relation of $i$ in the
more flexible initial state $\bstate^1$ was a subset
of that in the less flexible initial state $\bstate^1_1$. $$
   \babove_i(\bstate^1) \subset \babove_i(\bstate^1_1)
$$
Since the final
states are the same at the end of the sequence of moves, then
the sequence of moves must have changed the $\babove_i$ relation
WRT at least one of the initial states $\bstate^1$ and $\bstate^1_1$.
But, by definition of $\babove_i$, this can happen only if block $i$ moves.

\backwardcase
We next prove that when all flexed blocks have moved, the states are
identical.  We prove this case by induction.

\end{theorem}

\section{Solution Optimization}

\end{document}


\documentclass[11pt,a4paper]{article}

% \usepackage[TDBbrev]{UUbrev}
\usepackage[latin1]{inputenc}
\usepackage[swedish]{babel}
\usepackage{graphicx}
%\usepackage[dvips]{color}
\usepackage{float}
\usepackage{enumitem}
\usepackage{hyperref}

%\beteckning{Mini project}
%\mottagare{Scientific Computing, Bridging Course}

\begin{document}
\section*{Mini project 1: Water Pipe Network\footnote{Part~1 is based on
Problem~5.1 in A.~Quarteroni \& F.~Saleri, \emph{Scientific Computing with MATLAB},
Springer-Verlag Berlin Heidelberg, 2003}
}

This mini project is inspired by a research article:
\begin{quotation}\noindent
H. Jia, W. Wei, K. Xin, \emph{Hydraulic model for multi-sources reclaimed water pipe network based on EPANET and its applications in Beijing, China}, Front. Environ. Sci. Engin. China 2008, 2(1):57--62 (doi:10.1007/s117783-008-0013-0)
\end{quotation}
The article is about the design of a water pipe network in Beijing, for distribution of reclaimed water. Several issues are discussed, one of which is the water pressure at the network nodes.
\\
\\
In this mini project you will do a similar computation as is discussed in the article, but you will focus on the computation of the node pressure. You will, however, not use the real network data from Beijing, but a simpler model network. To simplify the task, it has been subdivided into three parts.
\bigskip \bigskip
\begin{figure}[hp]
\centering
\includegraphics[width=0.9\textwidth]{./network.pdf}
\caption{The water pipe system in part 1}
 \end{figure}

\newpage

\subsection*{Part~1}
To understand the principle we will start off with a very small water pipe network with six nodes only, see the figure on first page. The network nodes are enumerated 1, 2, $\ldots$, 6.  A water reservoir is connected to node 1, and water taps to nodes 5 and 6, and the problem here is to find the pressure in the inner nodes (2, 3 \& 4). The pressure value for node $k$ is denoted by $p_k$. The pressure is given as the difference between the water pressure and the surrounding atmospheric pressure. Consequently, the pressure value 0 corresponds to the atmospheric pressure.
\\
\\
The following relations are used to compute the pressure:
\begin{enumerate}
 \item In pipe $j$ the water flow speed $Q_j$ (m$^3$/s) can be expressed as:
\begin{equation}
 Q_j = k L \left(p_{in}-p_{out}\right)  \label{eq:Qj}
\end{equation}
where $1/k$ is the hydraulic resistance in the pipe, $L$ is the pipe length, $p_{in}$ is the pressure at inflow to the pipe and $p_{out}$ is the pressure at outflow from the same pipe. The inverse hydraulic resistance $k$ is measured in m$^2$/(bar\ s), the pressure in bar and the length in meter. In this case $k=0.001$ and constant.
\item The total inflow to a node equals the total outflow from the same node.
\end{enumerate}

\noindent The table contains values of $L$ for the pipes in our network:
\par
\begin{center}
\begin{tabular}{|cl || cl |}\hline
pipe  & $L$   & pipe & $L$    \\ \hline
1       & 300   & 4      & 600   \\ \hline
2       & 500   & 5      & 500    \\ \hline
3       & 500   & 6      & 500    \\ \hline
\end{tabular}
\end{center}
\par
\noindent
Moreover, the pressure in the water reservoir is 10~bar and the pressure  $p$ at the water outlets is ca. 0~bar.
\\
\noindent Relation~2 above can be expressed as follows for the inner nodes:
\begin{eqnarray*}
\rm{Node\ 2:} &\ & Q_1 = Q_2 + Q_{3}\\
\rm{Node\ 3:} &\ & Q_3 = Q_4 + Q_6\\
\rm{Node\ 4:} &\ & Q_2 + Q_4  = Q_5
\end{eqnarray*}

\noindent Inserting relationship (1) into these formulas and using the values from the table yields the following system of equations for the four pressure values  $p_1,\ldots, p_6$:

\begin{equation}
\left(\begin{array}{cccccc}
    1.0   &0        & 0      &   0      &   0    & 0     \\
    0.3   & -1.3  &  0.5  &   0.5   &   0    & 0     \\
    0      &   0.5  & -1.6  &  0.6   &   0    & 0.5  \\
    0      &   0.5  &   0.6 & -1.6   &   0.5 & 0     \\
  0        &   0     &   0     &   0      &  1.0  & 0     \\
  0        &   0     &   0     &   0     &   0     & 1.0
 \end{array}\right)
\left(\begin{array}{c}
p_1  \\
p_2  \\
p_3  \\
p_4  \\
p_5   \\
p_6
\end{array}\right) =
\left(\begin{array}{c}
10 \\
0    \\
0    \\
0    \\
0    \\
0
\end{array}\right)
\label{eq:matsys}
\end{equation}\\
\noindent All equations except the first one have right-hand side equal to 0, and the right-hand-side of the first equation is the pressure in the water reservoir $p_1$.
\\

\noindent Your task is to write a Matlab/Python script that solves this system and displays the result. Also, generate a bar graph showing the pressures of different nodes. It's important that output and figures are easy to understand even for someone not very involved in the project. Finally, change the program so that the user can input the pressure in the water reservoir, $p_1$, when running the program.

\subsection*{Part~2}
On the course website you can download the files \verb@water4720.mat@ and \verb@water6.mat@. The former contains data for a network with 4720 nodes, and the latter is data for the network i Part 1. Due to the size of the 4720 node network it has several water reservoirs. The data stored in the \verb@.mat@-files are two variables: the coefficient matrix $A$, and a vector called $sources$. The vector $sources$ contains the node indices corresponding to the water reservoirs.

As in Part 1 the elements in the right-hand-side $b$ are equal to the pressure in the water reservoir when there is a reservoir in that node, and 0 elsewhere. Thus, you simply assign the pressure in the reservoir nodes into the corresponding entries in $b$, and the index number of these entries can be found in the vector $sources$.
\\
Your task now is to write a program, a Matlab-function, that have the following structure:
\begin{enumerate}[label=(\roman*)]
\item Input the  water pipe network (i.e. load the mat-file, see information below)
\item Input pressure in the water reservoir nodes and create $b$
\item Compute the pressures in the network
\item Calculate and display the average pressure
\item Ask if the user want to try again (choosing other reservoir pressure-values). If the user type \textless Return\textgreater \thinspace on keyboard the program will repeat from item (ii) above,  and the user can choose new pressure values. If any other key on keyboard is chosen, the program will exit.
\end{enumerate}
It is important that the same program works for different pipeline networks, i.e. for different \verb@.mat@-files. Thus, the same program should work for both \verb@water4720.mat@ and \verb@water6.mat@. \\
To solve this in a good way, let the name of the \verb@.mat@-file be input-parameter to your function. To solve point (v) above, you can use a while-loop:
\begin{verbatim}
  loop = ' ' ;  % Assign empty string to enter while-loop
  while isempty(loop)
         ...
  end
\end{verbatim}

\noindent Implement the algorithm above in a Matlab/Python function and test that the program works the way it is supposed to. Through trial and error, try to find as low pressure as possible in the water reservoirs, but with a average pressure larger than 20 bar. \\

\medskip
\noindent {\bf The \verb@load@ command}\\
If the name of the input file (the .mat-file) is stored as a text string in the variable \verb@namein@, the file can be read via the command \verb@load(namein)@. The suffix .mat is implicit in calls to \verb@load@, so there is no need to include the suffix in the file name text strings. See the Matlab help for more information.

\medskip
\noindent {\bf The \verb@scipy.io.loadmat()@ function}\\
For Python user, you can load the \verb@.mat@ file with \verb@scipy.io.loadmat()@.
More detailed examples and documents are given in the official tutorial of  \verb@scipy@ at \url{https://docs.scipy.org/doc/scipy/reference/tutorial/io.html}.

\subsection*{Part~3}
It takes a little bit of time to solve the problem in Part 2 when the network size gets big. When you run the 4720 node problem, there is a slight waiting time when the equation system is solved. As the matrix is the same all the time, there is some room there for improvements that would make the program more efficient. Implement these improvements.

\end{document}


\documentclass[letterpaper]{article}

\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\usepackage{amssymb}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\title{Notes}
\date{November 7, 2014}
\maketitle
\section*{4.2 5a,6}
\subsection*{5a}
$\gcd(f(x),f'(x))=d(x)\ne 1$
assume $f(x)$ does not have repeatable factors

consider $p(x)$ to be an irreduceable factor of $d(x)$.

then we can say $f(x)=a(x)p(x), f'(x)=b(x)p(x)$ where $g(x),b(x)\in F[x]$.

$f'(x)=a'(x)p(x)+a(x)p'(x)$ and the following holds. $a(x)p'(x)=b(x)p(x)-a'(x)p(x)$

$p(x)|a(x)p'(x)\to p(x)|a(x) $ because $p(x)|h_1(x)h_2(x)\to p(x)|h_1(x)$ or $p(x)|h_2(x)$

$\exists cc(x)\in F[x]$ where $a(x)=c(x)p(x)$ and $f(x)=a(x)p(x)=c(x)p(x)p(x)=c(x)p(x)^2$ which is a contradiction

note that $p(x)|p'(x)$ is possible. $p(x)\in \mathbb{Z}_p[x], p(x)=x^{p^2}-x^{p}-1$ and $p'(x)=0$.

\section*{4.3 existence of roots}
$p(x)\in K[x]\setminus\{0\}$.

construction $m$ $K[x]$. for $f(x),g(x)\in K[x]$ $f\equiv g(x)\mod p(x)\Leftrightarrow p(x)|[f(x)-g(x)]$. this is an equivalence relation on $K[x].$

now we examine the equivalence class of $f(x)$. It consists of all polynomials in $K[x]$ such that $[f(x)]=\{g(x)|g(x)\in K[x], f(x)\equiv g(x)\mod p(x)\}$. we write $f(x)=p(x)q(x)+r(x)$ where $r(x)=0$ or $\deg r<\deg p$. and then $[f(x)]=[r(x)]$. the set of equivalence classes is denoted $K[x]/\langle p(x)\rangle$

\subsection*{properties}
\begin{enumerate}
\item
$f(x)\equiv g(x)\mod p(x)$ and $h(x)\equiv l(x)\mod p(x)$ then $f(x)+h(x)\equiv g(x)+l(x)\mod p(x)$ and $f(x)h(x)\equiv g(x)l(x)\mod p(x)$
\item
$f(x)h(x)\equiv g(x)h(x)\mod p(x)$ and $\gcd(p(x),h(x))=1$ then $f(x)\equiv g(x)\mod p(x)$.
\end{enumerate}
\subsubsection*{on $K[x]/\langle p(x)\rangle$}
define
\begin{enumerate}
\item
$[f(a)]+[g(x)]=[f(a)+g(a)]$
\item
$[f(x)]\cdot [g(x)]=[f(x)\cdot g(x)]$
\end{enumerate}
these are well defined operations, check all details

$[f(x)+g(x)]=[f'(x)+g'(x)]$?

\subsubsection*{example}
$K=\mathbb{R}, p(x)=x^2+1\in \mathbb{R}[x]$.

$\mathbb{R}[x]/\langle p\rangle =\mathbb{R}[x]/\langle x^2+1\rangle$

$[x]^2=-[1]$.
\end{document}


\section{What does the first principal component capture?}
We have described the score in the first principal component as a proxy for diversity of the export basket, where products are weighted by their complexity or sophistication. We explore here more in detail what this first component captures.

From the definition of what PCA does, we know that the first principal component $\scoreFirstPC$ captures the largest amount of variability across export baskets (the latter transformed using our measure $R_{cpt}$ of scaled absolute advantage). As shown before, the first principal component explains almost sixty percent of the variance, and the loadings of the vector are positive and approximately of the same magnitude. This necessarily means that the scores in this $\scoreFirstPC$ must capture an \emph{average scaled absolute advantage}. Given the formula of scaled absolute advantage, we therefore expect $\scoreFirstPC$ to be highly correlated with the logarithm of exports per capita.

But, for similar reasons, we also expect $\scoreFirstPC$ to be correlated with the diversification of the export basket. To see this let us drop the index $t$ for clarity of exposition. From the definition of Principal Component Analysis as a Singular Value Decomposition, we get that the matrix can be factored as $\mathrm{R}=\mathrm{H}\mathrm{V}^T$. Here, $H_{ck}$ is the score in the $k$-th principal component for country $c$ (i.e., the $c$-th element in the vector $\scorePC{k}$ in our notation), and $V_{pk}$ is how much product $p$ weights (or loads) on component $k$. The matrix $\mathrm{V}$ is orthogonal, and thus $\mathrm{V}^T\mathrm{V}=\mathrm{I}$ is the identity matrix. Given this, the 2-norm length of the export vector of country $c$ is
\begin{align*}
	\left\|R_c\right\|^2 &= [\mathrm{R}\mathrm{R}^T]_{cc}, \\
	&= [\mathrm{H}\mathrm{H}^T]_{cc}, \\
	&= \left\|H_c\right\|^2, \\
	&= \sum_k \scorePC{k}^2_c.
\end{align*}
In other words, the norm of the export basket vector of country $c$ is equal to the norm of the $c$'s vector in the space of principal components.

Now, the norm of country's $c$ export basket is related to its diversification, since diversity is the norm of the export basket if we \emph{discretize} the elements of the vector:
\begin{align*}
	M_{cpt}=
	\begin{cases}
		1,\quad\text{if $R_{cpt}>0$}\\
		0,\quad\text{if $R_{cpt}\leq 0$}
	\end{cases}.
\end{align*}
Having $R_{cpt}>0$ would mean that the country $c$ has an absolute advantage larger than the mean absolute advantage that countries have in that product $p$ in that year $t$ (we could discretize the matrix in other ways, but the result is qualitatively the same). Given this, diversity is $d_{c} = \sum_p M_{cp}$, but it is also $d_c = [\mathrm{M}\mathrm{M}^T]_{cc}$. All together, we can conclude that when there is a first principal component that explains most of the variation, then
\begin{align*}
	d_c \approx \left\|R_c\right\|^2\approx \scoreFirstPC(c)^2 + \scorePC{1}(c)^2.
\end{align*}
Thus, we would expect diversity to be correlated with the square of the first principal component score.

But beyond the observation that because (i) the first component explains a large share of the variance and (ii) that the vector of loadings is positive and weights approximately the same across products, then $\scoreFirstPC$ has to be a measure of both log-exports per capita and diversification, we would like to know whether it captures information beyond these two quantities.

We use the following datasets:
\begin{description}
	\item[Worldwide Governance Indicators (WGI)] from \url{http://info.worldbank.org/governance/wgi/index.aspx#home}. According to the source these report ``aggregate and individual governance indicators for over 200 countries and territories over the period 1996–2016, for six dimensions of governance: Voice and Accountability, Political Stability and Absence of Violence, Government Effectiveness, Regulatory Quality, Rule of Law, and Control of Corruption''.
	\item[Barro-Lee Educational Attainment Data] from \url{http://barrolee.com/data/Lee_Lee_v1.0/LeeLee_v1.dta} or \url{http://www.barrolee.com/data/BL_v2.2/BL2013_MF1599_v2.2.csv}, which reports ``educational attainment data for 146 countries in 5-year intervals from 1950 to 2010. It also provides information about the distribution of educational attainment of the adult population over age 15 and over age 25 by sex at seven levels of schooling: no formal education, incomplete primary, complete primary, lower secondary, upper secondary, incomplete tertiary, and complete tertiary. Average years of schooling at all levels---primary, secondary, and tertiary---are also measured for each country and for regions in the world.
	\item[International Data on Cognitive Skills] from \url{http://hanushek.stanford.edu/sites/default/files/publications/hanushek\%2Bwoessmann.cognitive.xls}. [Do Better Schools Lead to More Growth? Cognitive Skills, Economic Outcomes, and Causation, Eric A. Hanushek, Ludger Woessmann, Journal of Economic Growth]
\end{description}
The question is, how much do the quantities and indicators in these datasets explain $\scoreFirstPC$?

Figures\ \ref{fig:regcoefs_univariate}, \ref{fig:regcoefs_smallmultivariate}, \ref{fig:regcoefs_multivariate}, and \ref{fig:regcoefs_acrossyears} show the results of the standardized coefficients for different univariate and multivariate regressions.
\begin{figure}[htb]
\begin{center}
\includegraphics[width=0.7\columnwidth]{C:/Users/agomez/Dropbox/Identify laws and dynamical systems in socioeconomic data/machine_learned_patterns_in_economic_development/notebooks/regression_figures/PC0_regressions_coefficients_univariate.pdf}
\caption{
\textbf{Coefficients of the predictors from univariate regressions.}
 All regressions included year-specific fixed-effects, errors are clustered by country and error bars reflect $95\%$ confidence intervals. The estimates are for standardized coefficients.}
\label{fig:regcoefs_univariate}
\end{center}
\end{figure}

\begin{figure}[htb]
\begin{center}
\includegraphics[width=0.7\columnwidth]{C:/Users/agomez/Dropbox/Identify laws and dynamical systems in socioeconomic data/machine_learned_patterns_in_economic_development/notebooks/regression_figures/PC0_regressions_coefficients_smallmultivariate.pdf}
\caption{
\textbf{Coefficients of the predictors from a multivariate regression only including exports per capita, diversity and economic complexity index.}
 All regressions included year-specific fixed-effects, errors are clustered by country and error bars reflect $95\%$ confidence intervals. The estimates are for standardized coefficients.}
\label{fig:regcoefs_smallmultivariate}
\end{center}
\end{figure}

\begin{figure}[htb]
\begin{center}
\includegraphics[width=0.7\columnwidth]{C:/Users/agomez/Dropbox/Identify laws and dynamical systems in socioeconomic data/machine_learned_patterns_in_economic_development/notebooks/regression_figures/PC0_regressions_coefficients_multivariate.pdf}
\caption{
\textbf{Coefficients of the predictors from a multivariate regression including.}
 All regressions included year-specific fixed-effects, errors are clustered by country and error bars reflect $95\%$ confidence intervals. The estimates are for standardized coefficients.}
\label{fig:regcoefs_multivariate}
\end{center}
\end{figure}

\begin{figure}[htb]
\begin{center}
\includegraphics[width=\columnwidth]{C:/Users/agomez/Dropbox/Identify laws and dynamical systems in socioeconomic data/machine_learned_patterns_in_economic_development/notebooks/regression_figures/PC0_acrossyears_regressions_coefficients_subset_multivariate.pdf}
\caption{
\textbf{Coefficients of the predictors from multivariate regressions carried separately by year.}
 The estimates are for standardized coefficients.}
\label{fig:regcoefs_acrossyears}
\end{center}
\end{figure}

As can be seen, while all regressors predict to some extent $\scoreFirstPC$ when we carry out univariate regressions, when all are put together only exports per capita, diversity, government effectiveness, and rule of law survive. In the multivariate regressions done per year, the coefficients for exports per capita and diversity are consistently significant and positive, and have similar magnitudes.