\chapter{Proof of \texorpdfstring{\cref*{thm:access-control:abs-sec}}{Theorem~\ref{thm:access-control:abs-sec}}}%
\label{app:access-control-abs-sec}

\abssecuritytheorem*

The theorem is proved by showing our ABS scheme satisfies each security property.

\section{Proof of Perfect Privacy}

\begin{lemma}\label{lemma:access-control:abs-privacy}
  Our scheme has perfect privacy according to \cref{def:access-control:abs-privacy}.
\end{lemma}

\begin{proof}
  The first requirement of \cref{def:access-control:abs-privacy} is trivial considering the scheme is derived from~\cite{10.1007/978-3-642-19074-2_24}. It is also easy to verify that the distributions of the output signatures from \textsf{ABS.Sign} and \textsf{ABS.Relax} are identical owing to the re-randomization step in \textsf{ABS.Relax}, which proves the second requirement.
\end{proof}

\section{Proof of Unforgeability}

\begin{lemma}\label{lemma:access-control:abs-unf}
  Our scheme is unforgeable according to \cref{def:access-control:abs-unf} in the generic group model.
\end{lemma}

\newcommand{\Lin}{\textsf{Lin}}
\newcommand{\Hom}{\textsf{Hom}}

\begin{proof}
  The generic group model is designed to model the behavior of any algorithm that does not exploit the algebraic structure of the underlying groups. In other words, the lemma guarantees that our proposed scheme is unforgeable against any generic attacker, i.e., the attacker that does not exploit the underlying group structures. We remark that the scheme in~\cite{10.1007/978-3-642-19074-2_24} is also analyzed in this setting.

  The idea of the generic group model is briefly reviewed here. Let $p$ be a prime which represents the group order. A generic group $\mathbb{G}_i$ can be represented as the set $\{\xi_i(x)| x\in\mathbb{Z}_p\}$. The group operations are modeled by two oracles, namely, $\mathcal{O}_1, \mathcal{O}_2$. Specifically, on input two group elements $\xi_i(a), \xi_i(b)$, oracle $\mathcal{O}_i$ returns $\xi_i(a+b)$ and $\xi_i(a-b)$ respectively for multiplication and division. To model the bilinear pairing, another oracle $\mathcal{O}_E$ is defined. On input elements $\xi_1(a)$, $\xi_1(b)$, $\mathcal{O}_E$ returns $\xi_2(ab)$. Given $\xi_i(x)$ and scalar $s$, it is possible to obtain $\xi_i(sx)$ (via $O(\log s)$ calls to oracle $\mathcal{O}_i$).

  Since it only benefits the attacker, we assume the groups $\mathbb{G}$ and $\mathbb{H}$ are the same. The groups $\mathbb{G}$, $\mathbb{G}_T$ of the scheme are modeled as generic groups $\mathbb{G}_1$ and $\mathbb{G}_2$, respectively. The attacker works with the group elements and can only compute group operations by interacting with oracles $\mathcal{O}_i$ and $\mathcal{O}_E$. As the encoding functions $\xi_i$ are modeled as random functions, nothing about the element except equality can be inferred. The security proof is completed by showing that given the encodings of the public key and the signatures from the signing oracle, it is infeasible to output the encodings needed to satisfy the verification equation for the forgery with a bounded number of queries to oracles $\mathcal{O}_i$ and $\mathcal{O}_E$.

  We first define how signature queries are answered. For span program $\mathbf{M}\in \mathbb{Z}_p^{\ell \times t}$ with row labeling function $u$, pick random $\tau, y, s_1, \dots, s_\ell$, compute $p_j$ for $j=1$ to $t$, as follows:
  \begin{align*}
    p_j = \frac{1}{c + \hash}\big(\sum_{i=1}^\ell s_i (a+u(i)b)\mathbf{M}_{ij} - yz_j\big),
  \end{align*}
  where $(z_1, \dots, z_\ell) = (1, 0, \dots, 0)$ and $\hash = H(\tau$, $m)$.

  The signature is parsed as $\sigma=(\tau, g^y, g^{y/a_0}, g^{s_1}, \dots, g^{s_\ell}, h^{p_1}, \dots, h^{p_t})$. It is easy to show that the signature generated is of the same distribution as that of a signature created using the sign algorithm.

  The adversary is given $\xi_1(1), \xi_1(\Delta_0), \xi(a_0), \xi_1(\Delta)$, $\xi_1(a\Delta)$, $\xi_1(b\Delta)$, $\xi_1(c)$ as the public parameters. They represent group elements $(g$, $h_0$, $A_0$, $h$, $A$, $B$, $C)$, i.e., $mvk$, of the scheme.

  Let $Q$ be the number of signature queries made by the adversary, and $\ell_q, t_q$ be the length and width of the associated span program for the $q$-th query. In the $q$-th query, the adversary is given $\tau^{(q)}$ and the encodings of the following values: ${\{s_i^{(q)}\}}_{i=1}^{\ell_q}, {\{p_j^{(q)}\}}_{j=1}^{t_q}$, $w^{(q)}$, $y^{(q)}$, satisfying the conditions that $y^{(q)} \neq 1$, $w^{(q)} = y^{(q)}/a_0$ and for $j=1$ to $t_q$:
  \begin{align*}
    y^{(q)} z_j \Delta + (c + \hash^{(q)}) p^{(q)}_j = \sum_{i=1}^{\ell_q} s^{(q)}_i \mathbf{M}^{(q)}_{ij}(a+u^{(q)}(i)b)\Delta,
  \end{align*}
  where $\hash^{(q)}=H(\tau^{(q)}, m^{(q)})$ and $(z_1, \dots, z_{t_q}) = (1, 0, \dots, 0)$.

  Finally, the adversary outputs a forgery $\sigma^{\ast}:=(\tau^\ast$, $\xi_1(y^*)$, $\xi_1(w^\ast)$, ${\{\xi_1(s^{\ast}_i)\}}_{i=1}^{\ell^{\ast}}$, ${\{\xi_1(p^{\ast}_j)\}}_{j=1}^{t^\ast})$ on message $m^{\ast}$ with span program $\mathbf{M}\in\mathbb{Z}_p^{\ell^\ast \times t^\ast}$ and labeling function $u^\ast$.

  Denote by $\hash^\ast$ the value $H(\xi_1(y^*)$, $\xi_1(w^\ast)$, $\tau^\ast$, $m^\ast)$. To be a valid forgery, $y^\ast \neq 0$, $w^\ast = y^\ast/a_0$ and for $j=1$ to $t^\ast$, the following equation holds:
  \begin{align*}
    y^\ast z_j \Delta + (c + \hash^\ast)p^\ast_j = \sum_{i=1}^{\ell^\ast} s^\ast_i \mathbf{M}^\ast_{ij}(a+u^\ast(i)b)\Delta.
  \end{align*}

  For notational convenience, let $\Lin(S)$ denote the sets of functions that are linear in the terms in set $S$. Let $\Hom(S)$ be the subset of $\Lin(S)$ of homogeneous functions whose constant coefficient is zero. In the generic group model, a valid encoding can only be received from the oracles. Specifically, all encodings presented by the adversary in the generic group $\mathbb{G}_1$ must be linear combinations of the previously obtained element in $\mathbb{G}_1$. That is, $\sigma^\ast \setminus \{\tau^\ast\}  \subset \Lin(\Gamma)$, where $\Gamma$ is:
  \begin{align*}
  \{1, a_0, \Delta_0, \Delta, a\Delta, b\Delta\} \cup {\{{\{s_i^{(q)}\}}_{i=1}^{\ell_q}, {\{p_j^{(q)}\}}_{j=1}^{t_q}, w^{(q)}, y^{(q)}\}\}}_{q=1}^Q.
\end{align*}

The remaining part of the proof is to show that $\sigma^\ast\setminus\{\tau^\ast\}$ is not a subset of $\Lin(\Gamma)$ when we view the terms as functions in the random variables used in the security game. To complete the proof, we consider two cases.

\noindent\underline{Case 1: $(\tau^\ast, m^\ast) \neq (\tau^{(q)}, m^{(q)})$ for all $q$}. Firstly, $y^\ast =w^\ast a_0$. Thus,
\begin{align*}
  y^\ast \in \Hom(\{\Delta_0a_0, y^{(1)}, \dots, y^{(Q)}\}).
\end{align*}
Next, it can be seen that $\Delta | p_j^\ast$. Thus,
\begin{align*}
  p_j^\ast \in \Hom( \{\Delta, \Delta a, \Delta b\} \cup \{ {\{p_1^{(q)}, \dots, p_{t_q}^{(q)}  \}}_{q=1}^Q\} ).
\end{align*}

Since $\exists j$ s.t. $z_j \neq 0$, and given the form of $p_j^\ast$, it can be seen that $y^\ast$ cannot contain a term from $\Delta_0 a_0$. Thus,
\begin{align*}
  y^\ast \in \Hom(\{y^{(1)}, \dots, y^{(Q)}\}).
\end{align*}

Next, we argue that $p_j^\ast$ cannot contain a single term $\Delta$. For if it is the case, the left-hand side contributes monomials $\hash^\ast\Delta$ (since $y^\ast$ is homogeneous and has no constant term). On the other hand, the right-hand side cannot contribute monomials in $\Delta$ (everything in the right-hand side has either $a$ or $b$). Thus,
\begin{align*}
  p_j^\ast \in \Hom( \{\Delta a, \Delta b\} \cup \{ {\{p_1^{(q)}, \dots, p_{t_q}^{(q)}  \}}_{q=1}^Q\} ).
\end{align*}

Now, $p_j^\ast$ cannot contain a $p_n^{(q)}$ term for any $n, q$. Otherwise, it will produce a term with $\frac{c+\hash^\ast}{c+\hash^{(q)}}$ on the left and no setting of $y^\ast$, $s_i^\ast$ can come up with this rational term. In other words,
\begin{align*}
  p_j^\ast \in \Hom( \{\Delta a, \Delta b\}).
\end{align*}

Finally, we arrive at a contradiction since no combination of $(c+\hash^\ast)p_j^\ast$ and $\sum_{i=1}^{\ell^\ast} s^\ast_i \mathbf{M}^\ast_{ij}(a+u^\ast(i)b)\Delta$ could contribute to a term of the form $\Delta y^\ast$ for some $y\in \Hom(\{y^{(1)}, \dots, y^{(Q)}\})$.

\noindent\underline{Case 2: $(\tau^\ast, m^\ast) = (\tau^{(k)}, m^{(k)})$ for some $k$}. Following the analysis above, the following constraints can be reached:
\begin{gather*}
  y^\ast \in \Hom(\{y^{(1)}, \dots, y^{(Q)}\}), \\
  p_j^\ast \in \Hom( \{\Delta a, \Delta b\} \cup \{ {\{p_1^{(q)}, \dots, p_{t_q}^{(q)}  \}}_{q=1}^Q\} ).
\end{gather*}

Since $\tau^{(q)}$ is randomly chosen for each signature query, $\tau^{(k)} \neq \tau^{(q)}$ when $k\neq q$. Since $hash$ is collision-resistant, it means $\hash^\ast \neq \hash^{(q)}$ for all $q\neq k$. Thus, $p_j^\ast$ cannot contain a term from $p_n^{(q)}$ when $q\neq k$. Otherwise, there will be a term $\frac{c+\hash^\ast}{c+\hash^{(q)}}$ on the left and no setting of $y^\ast$, $s_i^\ast$ can come up with this rational term. Thus,
\begin{align*}
  p_j^\ast \in \Hom( \{\Delta a, \Delta b, p_1^{(k)}, \dots, p_{t_k}^{(k)}\} ).
\end{align*}

Next, $y^\ast$ cannot contain a term from $y^{(q)}$ for $q\neq k$ since all terms on the right-hand side contain either $a$ or $b$ and that $(c+\hash^\ast)p_j^\ast$ can only provide a term to cancel $y^{(k)}$. Thus,
\begin{align*}
  y^\ast \in \Hom(y^{(k)}).
\end{align*}

In this case, $m^\ast = m^{(k)}$ and thus the adversary wins if and only if $\Upsilon^\ast$ represents a more restricted policy compared with $\Upsilon^{(k)}$. Assume the DNF representation of policy $\Upsilon^{(k)}$ is $\mathcal{P}:=\vee_{x=1}^{n}(\mathcal{P}_x)$, where each $P_x$ is of the form $(\wedge_{y=1}^{n_x} (a_{xy}))$. In general, a more restricted policy can be formed by removing one clause from $\mathcal{P}$ or adding more attributes in one of $\mathcal{P}_x$.

In our threat model, the forgery must be of the form $(\lor_{a \in \mathcal{A}'} a)$ for some $\mathcal{A'} \subset \mathbb{A}$, where $\mathbb{A}$ represents the attribute universe. A successful forgery means that at least one clause, $\mathcal{P}_x$, is removed and that all attributes in $\mathcal{P}_x$ does not appear in $\mathcal{A}'$. The corresponding span program is $\mathbf{M} \in \mathbb{Z}_p^{|\mathcal{A}'| \times 1}$ and the labeling function maps each row to one attribute in $\mathcal{A}'$.

It means that the forgery in this case satisfies $t^\ast =1$, and that
\begin{align*}
  y^\ast z  \Delta + (c + \hash^\ast)p^\ast = \sum_{i=1}^{\ell^\ast} s^\ast_i (a+u^\ast(i)b)\Delta.
\end{align*}
Assume $\mathcal{P}_x$ is the clause that has been removed. We use $\bar{\mathcal{A}}$ to denote the attributes of $\mathcal{P}_x$. Thus, $u^\ast(i) \notin \bar{\mathcal{A}}$. However, each $p_{j}^{(k)}$ has at least one term with $a + u^{(k)}(i)b$ with $u^{(k)}(i) \in \bar{\mathcal{A}}$. WLOG, assume $\bar{\mathcal{A}}:=\{1,2, \dots, x\}$ and $\mathcal{P}_x$ is the first clause. Since $\mathcal{P}_x$ is a conjunctive clause, $p^{(k)}_1$ contains the term $(a+b)$, $p^{(k)}_2$ contains $(a+b)$ and $(a+2b)$, $p^{(k)}_3$ contains $(a+b)$ and $(a+3b)$, etc. If $p^\ast$ contains any of these $p^{(k)}_{j}$, there will be a term $(a+ub)$, $u\in\bar{\mathcal{A}}$, which appears on the left-hand side but not the right-hand side (note that $y^{\ast} = y^{(k)}$ and thus $y^\ast z \Delta$ cannot be used to cancel this $(a+ub)$ term. Further, note that linear combinations of $p^{(k)}_j$ cannot cancel all terms in the form of $(a+bu)$ for some $u \in \bar{\mathcal{A}}$). Thus,
\begin{align*}
  p^\ast \in \Hom( \{\Delta a, \Delta b\} ).
\end{align*}
Again, we arrive at a contradiction since no combination of $(c+\hash^\ast)p^\ast$ and $\sum_{i=1}^{\ell^\ast} s^\ast_i (a+u^\ast(i)b)\Delta$ could contribute to a term of the form $\Delta y^\ast$ for some $y\in \Hom(\{y^{(k)}\})$.
\end{proof}

\chapter{Proof of \texorpdfstring{\cref*{thm:access-control:sec}}{Theorem~\ref{thm:access-control:sec}}}%
\label{app:access-control-sec}

\querysecuritytheorem*

This theorem is proved by showing that our proposed query authentication algorithms satisfy each security property.

\section{Proof of Unforgeability}

\begin{lemma}\label{lemma:access-control:query-secure}
  Our proposed query authentication algorithms are unforgeable according to \cref{def:access-control:query-secure}.
\end{lemma}

\begin{proof}
  We prove this lemma by contradiction.

  \noindent\underline{Case 1:} The result set $RS$ contains a data record $\langle o^*, v^*, \Upsilon^*\rangle$, such that $\langle o^*, v^*, \Upsilon^*\rangle \neq \langle o_i, v_i, \Upsilon_i\rangle$, $\forall i$.

  Recall that in the result verification procedure, the verifier will run \textsf{ABS.Verify}$(mvk$, $H(o^*)|H(v^*)$, $\Upsilon^*$, $\sigma^*)$. Since it passes the verification and $\sigma^*$ can only be generated by the adversary, this means that the adversary is able to forge an ABS signature $\sigma^*$, which contradicts to \cref{thm:access-control:abs-sec}.

  \noindent\underline{Case 2:} The result set $RS$ contains a data record $\langle o^*, v^*, \Upsilon^*\rangle$, such that $o^*$ does not satisfy the query $q$ or $\Upsilon^*(\mathcal{A}) = 0$.

  It is trivial to see that such a case is impossible, as the verifier will check whether or not $o^*$ satisfies the query $q$ and $\Upsilon^*(\mathcal{A}) = 1$ during the result verification procedure.

  \noindent\underline{Case 3:} There exists a data record $\langle o_j, v_j, \Upsilon_j\rangle$ not in $RS$, such that $j \in \{i\}$, $o_j$ satisfies the query $q$, and $\Upsilon_j(\mathcal{A})= 1$.

  There are two possible subcases. The first subcase is that the index key of the missing record $j$ is not returned as part of the VO\@. This is impossible as the verifier will check whether or not the union of the indexing space for each entry in the VO covers the query range of $q$. The second subcase is that the index key of the missing record $j$ is returned as part of the VO\@. In this case, the record $j$ must fall in the space of an APS signature in the VO\@. Note that this APS signature can only be generated by the adversary, given its corresponding APP signature whose predicate is a more restricted policy compared with $\lor_{a \in \mathbb{A}\backslash\mathcal{A}} a$.
  This means that the adversary is able to forge an ABS signature,
  which contradicts to \cref{thm:access-control:abs-sec}.
\end{proof}

\section{Proof of Zero-Knowledge}

\begin{lemma}\label{lemma:access-control:query-zero-knowledge}
  Our proposed query authentication algorithms are zero-knowledge according to \cref{def:access-control:query-zero-knowledge}.
\end{lemma}

\begin{proof}
  It is easy to see that the messages output to the adversary from the ideal game will have the same distribution as those from the real game due to the following reasons:
  \begin{inlineenum}
  \item all the messages generated by the simulator are generated using the same algorithms as those by the real challenger;
  \item all ABS signatures achieve the prefect privacy (according to \cref{thm:access-control:abs-sec}); and
  \item the AP$^2$G-tree structure generated by the simulator is identical to the one by the real challenger.
  \end{inlineenum}
\end{proof}


\section{Quantifying Power Optimizations}
\label{sec:quantifying}

The practical component of our work addresses two related issues. We first establish the bounds within which CPU power draw can vary whilst running arbitrary code. This information allows us to put an upper limit on how much is possible to optimize a given code by. These bounds also help us tackle a second issue - namely to provide an objective appraisal of the power models proposed elsewhere in the literature.

As previously mentioned, the only property an engineer can influence while optimizing software for reduced CPU power consumption is processor activity factor. Although activity factor is defined as a scalar between zero and one, we know that in practice its range will be more limited. These limits also restrict the scope for optimization because of the relationship between activity factor and power consumption.

Modern processors are extremely complex and contain many logic elements dedicated to ancillary tasks like instruction fetching or clock signalling. These operations are necessary if a processor is to function, but do not depend on the software itself. This means there will always be some non-zero lower limit to activity factor which we label $\alpha$. Conversely, we know that there is no situation in which any processor will toggle the state of each transistor at every clock signal. Current processors are not physically able to keep all their functional units active simultaneously, which means our upper bound $\beta$ will be significantly lower than unity.

We define the range of values that activity factor can feasibly take whilst running a code as $[\alpha  .. \beta]$ where $0 < \alpha < \beta < 1$. From equations \ref{eq:totpwr}-\ref{eq:leakpwr} we know that, for a fixed platform, frequency and temperature, activity factors $\alpha$ and $\beta$ will be associated with constant power draws $P_{\alpha}$ and $P_{\beta}$ respectively, with $P_{\alpha} < P_{\beta}$.

We are now ready to derive a visual heuristic to guide optimization. We begin by plotting lines with gradients $P_{\alpha}$ and $P_{\beta}$ in \figurename~\ref{fig:modeldraw} to establish a feasible performance envelope. This represents the set of all $(E_\theta, D_\theta)$ pairs for which $P_{\alpha}~<~P_\theta~<~P_{\beta}$, with no bounds placed on runtime, $D_\theta$. It should be clear that all code executions, and by extension their maximally-optimized logical equivalents, must be represented somewhere within this envelope. We label the point which corresponds to our original run as $\theta$.

\begin{figure}[ht]
\centering
\lstset{basicstyle=\ttfamily\footnotesize\bfseries,
      frame=tb}
\lstinputlisting[]{Listings/nops.c}
\caption{Baseline Power Micro-Benchmark}
\label{fig:microbench}
\end{figure}

We approximate $P_{\alpha}$ by monitoring the power consumed whilst running an appropriate baseline benchmark. This code performs the minimum amount of work possible whilst keeping the system active. In particular, we wish to minimize our activity factor whilst matching the clock frequency and voltage figures we expect to see when code is running. We cannot simply halt the CPU because this would also stop many of the background tasks which contribute to $P_{\alpha}$.

We employ the code given in \figurename~\ref{fig:microbench} to derive our baseline.  It consists of a single instruction, performs no computation and places no demand on the memory subsystems. Any non-trivial computation will have a higher activity factor than this minimal micro-benchmark. If the application to be optimized blocks on IO this can be incorporated by measuring the power consumed when the CPU is inactive and adjusting $P_\alpha$ proportionately. We defer measurement of $P_{\beta}$ for now as its precise value is not relevant to the current discussion.

To constrain our search further we now consider the metric we wish to reduce. We know that for two logically equivalent codes $\theta$ and $\lambda$, the transformation $\theta \to \lambda$ is a valid optimization with respect to a cost metric $M$ if and only if $M(\lambda) < M(\theta)$. If we plot the curve linking all points having $M(\lambda) = M(\theta)$, then by definition any optimized versions of $\theta$ can only exist below this optimization bound. The exact equation of the curve depends on the chosen $E^mD^n$ metric as follows:

\begin{align}
E^mD^n(\theta) &= E^mD^n(\lambda) \nonumber \\
\implies {E_\lambda}^m &= \frac{{E_\theta}^m{D_\theta}^n}{{D_\lambda}^n} \nonumber \\
\implies E_\lambda &= (\frac{{E_\theta}^m{D_\theta}^n}{{D_\lambda}^n})^\frac{1}{m}
\end{align}

Our final bound considers what it means to optimize code for reduced power draw. We must avoid being too lenient; a large reduction in runtime associated with a negligible reduction in power draw should still be regarded as a classical optimization. On the other hand, our definition should include optimizations which deliver significant reductions in power draw with minuscule reductions in runtime.

The definition we have settled on is that an optimization $\theta \to \lambda$ is a power optimization with respect to metric $M$ if the change in power draw it delivers is responsible for the majority of the reduction in $M$. Conversely, if the primary benefit of an optimization comes from improved runtime then it is to be considered a runtime optimization. We plot a curve linking those points which have the same ratio of contributions from both power and runtime factors to $M$ as our original code. All valid power optimizations must lie below this so-called contribution bound. Again the equation for this bound depends on the metric chosen and is derived as follows:

\begin{align}
\frac{{P_{\theta}}^m}{{D_{\theta}}^{m+n}} &= \frac{{P_{\lambda}}^m}{{D_{\lambda}}^{m+n}} \nonumber \\
\implies {P_{\lambda}}^m &= \frac{{P_{\theta}}^m}{{D_{\theta}}^{m+n}} \times {D_\lambda}^{m+n} \nonumber \\
\implies {E_{\lambda}}^m &= \frac{{P_{\theta}}^m}{{D_{\theta}}^{m+n}} \times {D_\lambda}^{m+n+1} \nonumber \\
\implies E_{\lambda} &= (\frac{{P_{\theta}}^m}{{D_{\theta}}^{m+n}} \times {D_\lambda}^{m+n+1})^{\frac{1}{m}}
\end{align}

%\todo{Stick the actual derivations before a plot}
%\begin{equation}
% E_{\lambda} = \frac{P_{\theta} \times {D_{\theta}}^{3}}{{D_{\lambda}}^2}
%\end{equation}

%\todo{Put before plot 3}
%\begin{equation} E_{\lambda} = \frac{P_{\theta}}{{D_{\theta}}^3} \times {D_{\lambda}}^4
%\end{equation}

% All else being equal, energy to solution will reduce if a code can be made to run faster. That said, there is a clear difference between an optimization which specifically targets energy efficiency and this inevitable side-effect of classical optimization.
It may appear somewhat academic to base a bound on the definition of power optimization. That said, this is an important consideration in practice. Intuitively it makes sense to use the most appropriate tools while searching for optimizations. If code is sub-optimal and the penalty is felt more in one domain than the other then the tools and techniques developed for that domain are better suited to finding the optimization.

\begin{figure}
\input{./Plots/model_construction/plot_core}
\caption{$ED^2P$ Code Optimization Space}\label{fig:modeldraw}
\end{figure}

The bounds presented above allow us to identify the area in the Energy/Runtime plane where power-optimized versions of a given code may exist. For the purposes of illustration we add lines of fixed time and power draw based on our initial code measurement. This allows us to subdivide \figurename~\ref{fig:modeldraw} into those areas labelled:

\begin{enumerate}
\centering
\item Power-only optimizations
\item Power-mostly optimizations
\item Time-mostly optimizations
\item Time-only optimizations
\item Code degradation
\end{enumerate}

Only three measurements are required to build this plot; the system's baseline power draw, $P_\alpha$, and the time and energy to solution for the code to be optimized, $D_\theta$ and $E_\theta$ respectively. We can ignore the value of $P_\beta$ when optimizing for power draw as we need not consider any values greater than our initial $P_\theta$. We also need to consider which metric we are optimizing. \figurename~\ref{fig:modeldraw} is based on $ED^2P$, whilst \figurename~\ref{fig:multimetric} demonstrates how the optimization area varies with metric choice.

Despite its simplicity, this technique offers a surprising wealth of information. The vertical distance between $\theta$ and intercept $B$ places an upper limit on the absolute amount of energy which can be saved by power optimization alone. The value $M(\theta) / M(A)$ bounds the amount of improvement in our metric we can expect to see from power optimization. The difference in runtime between intersect $C$ and $\theta$ represents the amount of time we are able to trade off if we hope to achieve a slower yet more energy efficient code. Finally, the value $D(\theta) / D(A)$ represents the smallest speed-up which delivers more benefit than power optimization is capable of.

At this point it is worth stating explicitly that our heuristic is a general one. Its only requirements are that power consumption is proportional to activity factor and that energy and time figures can be obtained for the system baseline and the code under investigation. As such it can be applied equally well at scales ranging from a single core to an entire system. We focus on CPU power consumption simply because measurements are relatively easy to obtain at this level.

\begin{figure}
\input{./Plots/multimetric_construction/plot_core}
\caption{Multi-Metric Code Optimization Spaces}\label{fig:multimetric}
\end{figure}

We set out to apply our technique to a number of HPC benchmarks. Our first task was to measure the $P_\alpha$ baseline for our test system. This machine incorporates a commodity Intel Ivy Bridge processor which exposes the Running Average Power Limit interface \cite{david:2010aa}. For this reason we chose to use the techniques outlined in \cite{hackenberg:2013aa} to perform our measurements. This choice was made primarily out of convenience, as our method does not rely on any particular hardware or measurement configurations. The only prerequisites are that the system under test is CMOS-based and accurate power and time measurements are available.

\begin{table}
\centering
\small
\begin{tabular}{@{}ccccc@{}} \toprule
&\multicolumn{4}{c}{CPU Cores Active} \\ \cmidrule(r){2-5}
Frequency (GHz) & 1 & 2 & 3 & 4 \\ \midrule
1.60 & 9.180 & 10.970 & 12.832 & 14.555 \\
1.70 & 9.449 & 11.446 & 13.295 & 15.112 \\
1.80 & 9.592 & 11.654 & 13.617 & 15.682 \\
1.90 & 9.816 & 12.009 & 14.168 & 16.291 \\
2.10 & 10.272 & 12.709 & 15.161 & 17.605 \\
2.20 & 10.559 & 13.161 & 15.705 & 18.333 \\
2.30 & 10.812 & 13.551 & 16.419 & 19.070 \\
2.40 & 11.303 & 14.290 & 17.012 & 19.946 \\
2.50 & 11.680 & 14.784 & 18.000 & 20.837 \\
2.60 & 11.819 & 15.144 & 18.616 & 21.879 \\
2.70 & 12.205 & 15.830 & 19.379 & 22.940 \\
2.90 & 13.095 & 17.196 & 21.155 & 25.344 \\
3.00 & 13.547 & 18.160 & 22.210 & 26.759 \\
3.10 & 14.048 & 18.870 & 23.639 & 28.284 \\
3.20 & 14.504 & 19.726 & 24.940 & 29.857 \\
\bottomrule
\end{tabular}
   \vspace{0.5\baselineskip}
\caption{Test Platform Base CPU Power (W)}
\label{tab:baseline}
\end{table}

We measured CPU power draw whilst running our baseline micro-benchmark for each combination of core count and frequency which was supported by the hardware of our test platform. The results of this investigation are given in Table \ref{tab:baseline}. We then ran a range of codes chosen from common benchmarking suites, targeting various core counts and system configurations. A small number of results from the Rodinia suite is presented in Table \ref{tab:coderesults}. These results come from runs which were configured to use the maximum frequency and core count possible. Our baseline power draw for this configuration is 29.857 Watts.

\begin{table}
\centering
\small
\begin{tabular}{@{}ccccc@{}} \toprule
Code & Runtime (s) & Energy (J) & Power (W) & $ED^2P$ \\ \midrule
CFD & 29.72 & 933.32 & 31.40 & 824381  \\
Heartwall & 24.62 & 787.17 & 31.97 & 477139 \\
lavaMD & 66.18 & 2142.65 & 32.38 & 9384362\\
leukocyte &  38.92 & 1197.91 & 30.78 & 1814554 \\
streamcluster & 33.86 & 1086.77 & 32.10 & 1245981 \\
\bottomrule
\end{tabular}
   \vspace{0.5\baselineskip}
\caption{Rodinia Results @ 3.2GHz, 4 Cores}
\label{tab:coderesults}
\end{table}

Of the codes listed above, lavaMD is the strongest candidate for optimization as it has both the highest power draw at 32.38 Watts and the longest runtime of 66.18s. Applying our heuristic to this code for $ED^2P$ yields the optimization space shown in \figurename~\ref{fig:lavamd}. This construction allows us to derive a number of additional metrics, as described below.

\begin{figure}
\input{./Plots/lavamd_model/plot_core}
\caption{$ED^2P$ Heuristic for Rodinia lavaMD} \label{fig:lavamd}
\end{figure}

If we assume it is somehow possible to reduce the activity factor of lavaMD to match that of our  micro-benchmark which performs no work, then the corresponding energy savings at point B would amount to 166.71 Joules, a $7.8\%$ reduction. This corresponds to an improvement in $ED^2P$ of $1.08\times$ from 9384362 to 8654190. Point A represents the most extreme case; reducing activity factor to the minimum possible whilst also speeding up our code to a lesser extent. This scenario yields an $ED^2P$ of 7979856, an improvement of $1.17\times$.

For lavaMD, the longest runtime within our optimization envelope is 69.99s. This means that any power optimization which seeks to trade increased runtime for improved energy efficiency can slow lavaMD down by at most 1.81s before $ED^2P$ becomes strictly worse. Furthermore, we know the fastest out code can run whilst remaining in our envelope is 64.41s. This means that a classical optimization which reduces runtime by 1.77s or more is strictly better than all valid power optimizations at minimizing $ED^2P$. A modest $1.03\times$ speed-up from classical optimization yields a greater improvement in $ED^2P$ than any power optimization ever could.

The figures listed above are all upper bounds, and the benefits of power optimization will be more modest in practice. Even so, these figures are useful as they allow performance engineers to make informed decisions about where best to focus their efforts. If they consider a $1.03 \times$ speed up to be more achievable than up to the maximum $1.17\times$ reduction in activity factor then they can proceed to apply conventional optimizations safe in the knowledge that overall performance will improve despite any increases in activity factor.

If a performance engineer decides the benefits of power optimization are worth pursuing after applying the test outlined above, the question still remains as to how he should go about searching for those optimizations. Classical optimizations \todo{Something about extra stuff}

Conventional tools produce a breakdown of time spent by code path by halting the target application at timed intervals and recording its state. By extension, a sampling profiler which operated in intervals of energy instead of time it would show the contribution of different code sections to total energy use.

We first construct a power model in the vein of those proposed in the literature. We then try to link the predictions of our model to the power equations given previously. Essentially we are attempting to test against the null hypothesis - to show how much better or worse our regressed model is better than a naive attempt.

\fragment{Often power models are assessed by their performance relative to a measured baseline. Although this is important, this figure is somewhat meaningless without context. Our approach is one of quantifying

\todo{Make this bit less attacky:}
It is also worth noting that this model is effectively useless from a code optimization standpoint as it does not take any software features into account. This property is intentional, as it allows us once again to provide a baseline from which to assess any models. One can only realistically expect to optimize a code to the level at which any changes can be accurately measured. A model which claims to assist in the optimization of codes can only offer optimizations to the extent it shows divergence from this baseline.

}

\fragment{The unknown quantities in our simplified power equations can be empirically measured. \todo{Occam's razor - stronger than regression if not out performed by it}}

\fragment{Accuracy figures without context are notoriously unreliable. To compensate for this we compare the outputs of various models against a baseline we have devised. This baseline consists of what we regard as the simplest non-trivial power model conceivable. This model stands in as a sort of null hypothesis test, our justification being that a complex model only adds value to the extent with which it outperforms this toy model.}

\fragment{Our toy model is not the simplest model possible - It is well established and readily apparent that runtime is the largest contributory factor to power consumption. One could therefore imagine a simple power model}

\fragment{We consider this to be the absolute minimum power consumption possible.}

\fragment{Intentionally simplistic. Our decision to simplify activity factor to active cores is part of this. We assume that instruction pipe-lining does a reasonable job of keeping as much silicon active as possible, and we do not know how much area an individual instruction activates, and a large percentage of power use is from clock circuitry anyway}

\todo{Be nice, say that this does not invalidate other work, but simply shows that hardware has now reached \reword{convergence} and basically there's little traction left}

Having shown that

\fragment{This model is not supposed to be rigorous or precise. Rather we present it as the simplest possible non-trivial power model which accounts for the sources of variability in the power equations. In effect our model is functionally equivalent to the power equations
presented previously with appropriate constants substituted \todo{sampled}. We present this model as our null hypothesis - for a model to be useful it must outperform this one. It is necessarily an oversimplification - it ignores clock gating. It should also consistently underestimate the true value, as the benchmark selected intentionally exercises the minimum possible number of logic elements while still performing work.}

\todo{One limitation of this work stems from the nature of RAPL - it is an accumulative measurement of power taking into account all processes. On a loaded system it will measure all tasks }

\begin{figure}
\label{fig:dummy_model}
\includegraphics[width=0.9\linewidth]{./Plots/dummy_model/dummymodel-figure0.pdf}
\caption{Feasible Performance Envelope}
\end{figure}

\todo{Decompose the model - find baseline vs non-baseline components}
\todo{This kind of shows us that the baseline dominates}

\fragment{Limitations to optimizations - the superfluous and the logical equivalences. The first is a no-brainer and boils down to removing unnecessary pre-fetching}

\fragment{Either optimizations which are off the critical path, or else those which are on the critical path }

\todo{Do maths think - by what margin would power have to go down to justify longer runtime? ratio of cost per watt, amortized cost per second}

Nothing discussed so far precludes power optimization in practice.
\todo{imply limits thus far are theoretical}
Even these tight limits may still admit some benefits at extreme scale.
Our final argument however is strictly economic.
\reword{A great deal of attention is paid to the fact that power costs are approaching parity with machine construction costs.} The \choice{implicit, unspoken} \choice{consequence, corollary, implication} being that this has not yet happened. \todo{Ultimate point being here the price difference, machine vs power cost places a further limit on optimization utility. Even if we manage to find a slower, more power efficient method of computing a given result, the cost of energy saved has to be less than the added amortized runtime cost.}

\todo{Legitimate targets for optimization: removing redundant prefetch operations as per phi paper.}


\documentclass{article}
\usepackage[margin=1.25in]{geometry}
\usepackage{amsmath}
\usepackage{physics}
\usepackage{bm}

\usepackage{graphicx}
\begin{document}

\centerline{\sc \large Analytic derivatives of the trajectory with respect to forcefield parameters.}
\vspace{.5pc}
\centerline{An exact solution for fitting parameters to ensemble observables.}
\vspace{.5pc}
\centerline{Yutong Zhao (proteneer@gmail.com), unaffiliated}
\vspace{2pc}

\section{Introduction}

When we run molecular dynamics to generate conformations along a trajectory, we almost always generate observables derived from the ensemble. Example observables span from simple quantities such as pseudo-electron density in XRD/ED refinement, density, radii of gyration, Helmholtz free energies, distance information arising from NMR, etc. Nearly always, the experimentally measured observable and the predicted observable don't agree with each other. This results in the painstaking effort of identifying new forcefield functional forms and/or fitting their parameters. Note that this paper is not concerned with the former (discovery of functional forms), but rather, on how to fit the parameters used by these functional forms to condensed phase properties that arise from a simulation (which often span from simple fixed-charged models, all the way to more complicated polarizable forcefields).

In stark contrast to training techniques of modern machine learning, forcefield parameter fitting in MD has been sort of a black-art. At a high level, the parameter tuning procedure is typically separated into two parts: 1) fitting to gas phase QM energies across a varying level of theories either by energy or force-matching, which initializes parameters into a sane region of parameter space, 2) followed by a difficult procedure of fitting to condensed phase properties in a variety of solvent and/or crystallographic conditions. Typically, the second step requires repeated simulations to generate predicted observables from the ensemble. However, there lacks an analytic derivative of the observables to parameters partially owing to complexity of generating necessary derivatives.

Fortunately, with the advent of sophisticated auto differentiation systems in modern machine learning frameworks, it has become not only possible, but also practical to generate these derivatives. We introduce a new molecular dynamics tool with two primary aims:

First, the ability to quickly test new functional forms, as well as its derivatives. Ideally, the user should only need to specify the functional form of the energy function, and all subsequent higher order derivatives should be automatically generated. As an example, OpenMM uses symbolic differentiation (lepton) that's then JIT compiled into the respective kernels. However, symbolic differentiation has severe drawbacks in that it scales poorly with increased complexity of functional forms. In contrast, forward/reverse-mode autograd systems uses dual-algebra, which tends to scale better with complexity as well as naturally lending itself to higher order derivatives. This should enable us to rapidly prototype novel functional forms, ranging from polarizable models to neural-network potentials.

Second, the ability to analytically fit these parameters to observables derived from a simulation trajectory. We show that one can, without too much difficulty, implement analytic derivatives through Langevin dynamics provided one has access to analytic hessians and second-order mixed partial derivatives of the energy function. This allows for optimization of not only the location of the minimas, but also the curvature along the path to reach said minimias. This approach is in contrast to other approaches such as thermodynamic gradients (assuming the ergodic limit has been reached), which uses only the first order derivative of the energy with respect to the parameters. In practice, the implementation is realized in two parts through the chain rule, the first being the derivative of the observable with respect to each conformation in the ensemble, the second being the derivative of a conformation in the trajectory with respect to the forcefield parameters.

\section{Mathematical Formalism}

Define the loss function $L(O(\vec{X}_{label}), O(\vec{X}_{md}))$, where $O$ is a function that calculates some observable from an ensemble. $\vec{X}_{md}(\vec{x}_0; \theta)$ is a set of conformations derived from a molecular dynamics simulation, starting from some initial state $\vec{x}_0$ with some set of forcefield parameters $\theta$. We can also write $\vec{X}_{md}$ as a collection of individual states $\vec{x}_t$ at various times $t$. The goal is to analytically derive $\dfrac{\partial L}{\partial \theta}$ so we can train $\theta$ to some experimental observable $O(\vec{X}_{label})$ by using a standard MSE loss:

\begin{equation}
L(O(\vec{x}_{label}), O(\vec{x}_t)) = \lbrack O(\vec{x}_{label}) - O(\vec{X}(\vec{x}_0;\theta)) \rbrack ^2
\end{equation}
Training the parameters means we need the gradient of $L$ with respect to the parameters $\theta$, which we compute using the chain rule:
\begin{equation}
\dfrac {\partial L}{\partial \theta} = \dfrac{\partial L}{\partial \vec{X}_{md}} \dfrac{\partial \vec{X}_{md}}{\partial \theta}
\end{equation}
In an unbiased sample of conformations, this is equal to a sum of the derivatives of individual states from the trajectory:
\begin{equation}
\dfrac {\partial L}{\partial \theta} = \sum_{t}{\dfrac{\partial L}{\partial \vec{x}_t} \dfrac{\partial \vec{x}_t}{\partial \theta}}
\end{equation}
Each summand is a component-wise matrix-multiply, where both $\partial L / \partial \vec{x}_t$ and ${\partial \vec{x}_t}/{\partial \theta}$ are $(N, 3)$ matrices. The LHS is the derivative of the loss w.r.t. to a conformation, and the RHS is the derivative of a conformation w.r.t. to the model parameters. The LHS is trivial to compute, so the RHS is the main quantity of interest.

If we were to run Langevin dynamics, the velocity is $\vec{v}_t$ is updated according to:
\begin{equation}
\vec{v}_t = a \vec{v}_{t-1} - b \nabla E(\vec{x}_{t-1};\theta) + c \vec{n}_{t-1}
\end{equation}
Where $\nabla$ is the gradient operator (w.r.t. coordinates), and $a$, $b$, $c$ are the coefficents derived from the temperature and the friction, and $\vec{n}$ is noise sampled from an independent gaussian.
\begin{equation}
\vec{x}_{t} = \vec{x}_{t-1} + \vec{v}_{t} \delta t
\end{equation}
Expanding out the first few terms we get:
\begin{equation}
\begin{split}
\vec{x}_1  &= \vec{x}_0 + \big(a \vec{v}_0 - b \nabla E(\vec{x}_0) + c \vec{n}_0  \big) \delta t \\
\vec{x}_2  &= \vec{x}_1 + \bigg( a \Big( a \vec{v}_0 - b \nabla E(\vec{x}_0) + c \vec{n}_0 \Big) - b \nabla E(\vec{x}_1) + c \vec{n}_1 \bigg) \delta t \\
\vec{x}_3  &= \vec{x}_2 + \Bigg(a\bigg(a \Big( a \vec{v}_0 - b \nabla E(\vec{x}_0) + c \vec{n}_0 \Big) - b \nabla E(\vec{x}_1) + c \vec{n}_1 \bigg) - b \nabla E(\vec{x}_2) + c \vec{n}_2 \Bigg) \delta t \\
\end{split}
\end{equation}
For convenience, we define the $\zeta$ operator:
\begin{equation}
\zeta E(\vec{x}_i; \theta) \equiv \dfrac{\partial^2E}{\partial\vec{x}_i^2}\dfrac{\partial\vec{x}_i}{\partial\theta} +  \dfrac{\partial^2 E}{\partial \vec{x}_i \partial \theta}
\end{equation}
To demystify this expression, the first summand is a tensor contraction of a rank-4 hessian (N,3,N,3) with a rank-2 derivative (N,3). The second summand is a rank-2 mixed partial derivative (N,3). We can write out the first few terms of the derivative using the $\zeta$ operator:
\begin{equation}
\begin{split}
\dfrac{\partial\vec{x}_1}{\partial\theta}  &= - b\delta t \zeta E(\vec{x}_0) \\
\dfrac{\partial\vec{x}_2}{\partial\theta}  &= - b\delta t \Big(\zeta E(\vec{x}_0)(1+a)+\zeta E(\vec{x}_1)\Big)\\
\dfrac{\partial\vec{x}_3}{\partial\theta}  &= - b\delta t \Big(\zeta E(\vec{x}_0)(1+a+a^2)+\zeta E(\vec{x}_1)(1+a)+\zeta E(\vec{x}_2)\Big)\\
\end{split}
\end{equation}
Finally, we can tidy this up into the following expression:
\begin{equation}
\dfrac {\partial x_t}{\partial \theta} = -b  \delta t \Big( \zeta E(\vec{x}_0; \theta)S_{t-1}^a + \zeta E(\vec{x}_1;\theta)S_{t-2}^a + \ldots +  \zeta E(\vec{x}_{t-1};\theta) \Big)
\end{equation}
$S_n^a$ is the geometric series of $n$ terms of $a$. Observe that we only need to do a single forward pass, which can be implemented using forward-mode auto-differentiation. Naively, this procedure needs $O(t)$ memory and $O(t^2)$ runtime since we need to store $\partial\vec{x}_i / \partial\theta$ and traverse back in time as they're re-weighted after each step. But this would catastrophic for all practical purposes. Fortunately, we can immediately leverage the geometric series, whose rapid convergence implies we need to store the $\zeta$ terms for only a small numbers of the most recent steps, since for large $t$:
\begin{equation}
\zeta E(\vec{x}_0)(1+a+a^2+...+a^t) \approx \zeta E(\vec{x}_0)(1+a+a^2+...+a^{t-1})
\end{equation}

Suppose it takes $k$ steps for the geometric series converge to numerical accuracy. This allows us to maintain a rolling window of only size $O(k)$ for unconverged $\zeta$ values. The converged $\zeta$ terms can be reduced and thus require only $O(1)$ extra space. In practice, $k \approx 4000$, so the over all algorithm becomes becomes $O(1)$ in memory and $O(t)$ in time.

% With this machinery in place, we can now use some additional tricks to regularize the fitting process, such as enforcing that for all ergodic systems, the time averaged observable must be equal to the ensemble averaged observable. The exact method is that we use reservoir sampling to draw from the time average, and the last time slice of a replica simulation to generate the ensemble average.

\section{Issues and Caveats}

The first major issue has to do how training proceeds. The above procedure needs to be repeated multiple times for the parameters to converge. That is, each iteration would require us to re-run an entire MD simulation since we can't easily re-use the intermediate values. For some test systems it can take anywhere from 50 to 500 rounds of simulations for a single system to be fully optimized (internal data).

A second more practical issue deals with the computational complexity and speed. The analytically dense Hessian is an inherently $O(N^2)$, which is a major increase from the standard $O(N log N)$ in MD. While the bonded terms have sparse diagonal Hessians, it is unclear if the nonbonded hessians can be sparsified. Furthermore, because the derivatives are automatically generated, and that it's using tensorflow underneath the hood, the simulation itself is significantly slower than a highly optimized MD package. However, with recent advances in XLA-JIT and the flexibility of implementing custom ops, we hope to reconcile this to within an order of magnitude.

\section{Code}

The code has been implemented and will be open-sourced once the manuscript has been accepted. Please contact the author for access.

\end{document}


%%% solution 01 %%%
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath, amssymb, amsfonts, enumitem, fancyhdr, pgfplots, tikz}
% need to set pgfplots compatibility (choose newest)
\pgfplotsset{compat=newest}
% get rid of paragraph indent
\setlength{\parindent}{0 pt}
% allow section.equation numbering
\numberwithin{equation}{section}
% allows you to copy-paste code segments. requires pygments, which can be
% installed from PyPI with pip install Pygments.
% warning: minted does NOT work with Texmaker if you have TeX Live installed
% on WSL but Texmaker installed natively on Windows!
%\usepackage{minted}
% alternative to minted that does not require Python, LaTeX only. listings is
% however disgusting out of the box and some setup is required.
\usepackage{listings, xcolor}
% makes clickable links to sections
\usepackage{hyperref}
% make the link colors blue, as well as cite colors. urls are magenta
\hypersetup{
    colorlinks, linkcolor = blue, citecolor = blue, urlcolor = magenta
}
% fancy pagestyle so we can use fancyhdr for fancy headers/footers
\pagestyle{fancy}
% add logo in right of header. note that you will have to adjust logo path!
\fancyhead[R]{\includegraphics[scale = 0.15]{../bac_logo1.png}}
% don't show anything in the left and center header
\fancyhead[L, C]{}
% give enough space for logo by reducing top margin height, head separator,
% increasing headerheight. see Figure 1 in the fancyhdr documentation. if
% \topmargin + \headheight + \headsep = 0, original text margins unchanged.
\setlength{\topmargin}{-60 pt}
\setlength{\headheight}{50 pt}
\setlength{\headsep}{10 pt}
% remove decorative line in the fancy header
\renewcommand{\headrulewidth}{0 pt}

% color definitions for listings syntax highlighting. uses colors borrowed
% from the VS Code Dark+ and Abyss standard themes.
\definecolor{KwColor}{RGB}{153, 102, 184}     % keyword color
\definecolor{VarColor}{RGB}{86, 156, 214}     % variables/identifier color
\definecolor{StrColor}{RGB}{209, 105, 105}    % string color
\definecolor{CmtColor}{RGB}{106, 153, 85}     % comment color

% general listings configuration for all languages
\lstset{
    % change keyword, identifier, comment, string colors
    keywordstyle = \color{KwColor},
    commentstyle = \color{CmtColor},
    identifierstyle = \color{VarColor},
    stringstyle = \color{StrColor},
    % no spaces in strings
    showstringspaces = false,
    % monospace font by default
    basicstyle = \ttfamily,
    % tabsize 8 by default, this is not the 1960s
    tabsize = 4,
    % add line numbers to the left with gray typewriter font
    numbers = left,
    numberstyle = \color{gray}\ttfamily,
    % change distance from code block from 10 pt to 5 pt
    numbersep = 5 pt
}

% colors to use for Figure 1 in section 1.4. these are the RGB values returned
% by the matplotlib viridis colormap, produced using the following Python code:
%
% import matplotlib.cm as cm
% cm.viridis([0.1, 0.5, 0.9])
%
% a numpy.ndarray shape (3, 4) is returned, each row the RGBA values.
\definecolor{viridis_ichi}{rgb}{0.282623, 0.140926, 0.457517}
\definecolor{viridis_ni}{rgb}{0.127568, 0.566949, 0.550556}
\definecolor{viridis_san}{rgb}{0.741388, 0.873449, 0.149561}

% title, author, date
\title{Solution 1}
\author{Derek Huang\thanks{NYU Stern 2021, BAC Advanced Team.}}
\date{May 20, 2021}

\begin{document}

\maketitle
% need to include this after making title to undo the automatic
% \thispagestyle{plain} command that is issued.
\thispagestyle{fancy}

\section{Set theory}

\subsection{Partition of $ \mathbb{R} $}

\textit{Solution 1.} Consider the infinite sequence
$ \left\{1 - \frac{1}{k}\right\}_{k \in \mathbb{N}} $. Note that
$ \lim_{k \rightarrow \infty}\left(1 - \frac{1}{k}\right) = 1 $ and that the
sequence is strictly monotone increasing, as
$ 0 < \frac{1}{2} < \frac{2}{3} < \ldots < 1 $. Now let us define $ A_k $,
$ \forall k \in \mathbb{N} $, such that
\begin{equation*}
    A_k \triangleq \left[1 - \frac{1}{k}, 1 - \frac{1}{k + 1}\right)
\end{equation*}

It's clear that $ A_1, A_2 \ldots $ are mutually disjoint and that
$ \bigcup_{k \in \mathbb{N}}A_k = \left[0, \frac{1}{2}\right) \cup
\left[\frac{1}{2}, \frac{2}{3}\right) \cup \ldots = [0, 1) $ . Therefore,
\begin{equation*}
    P = \bigcup_{k \in \mathbb{N}}\{A_k\}
\end{equation*}

\medskip

\textit{Solution 2.} Consider the infinite geometric series
$ \sum_{k = 0}^\infty r^k $, where $ r \in (0, 1) $. Since $ |r| < 1 $, we
know that $ \sum_{k = 0}^\infty r^k $ converges. In particular, by the
formula for the convergent geometric series, we see that
\begin{equation*}
    (1 - r)\sum_{k = 0}^\infty r^k = (1 - r)\frac{1}{1 - r} = 1
\end{equation*}

Let $ S_{k; r} \triangleq (1 - r)\sum_{j = 0}^kr^j $,
$ \forall k \in \mathbb{N} \cup\{0\} $. Since the terms of
$ \{S_{k; r}\}_{k \in \mathbb{N}} $ are strictly monotone increasing, as
$ 1 - r < (1 - r)(1 + r) < (1 - r)(1 + r + r^2) < \ldots $, let us define
$ A_{k; r} $, $ \forall k \in \mathbb{N} \cup \{0\} $, such that
\begin{equation*}
    A_{k; r} \triangleq [S_{k; r}, S_{k + 1; r})
\end{equation*}

It's clear that $ A_{0; r}, A_{1; r}, \ldots $ are mutually disjoint and that
$ [0, 1 - r) \cup \bigcup_{k \in \mathbb{N} \cup\{0\}}A_{k; r} = [0, 1) $.
Therefore,
\begin{equation*}
    P_r \triangleq \{[0, 1 - r)\} \cup
    \bigcup_{k \in \mathbb{N}\cup\{0\}}\{A_{k; r}\}
\end{equation*}

$ \forall r \in (0, 1) $, $ P_r $ is a partition of $ [0, 1) $ into an
infinite number of half-open intervals.

\subsection{Slicing the unit circle}

\textit{Solution.} Note that $ \tilde{U} \triangleq \{\mathbf{x} \in U :
x_1 \ne 0, x_2 \ne 0\} $, as no point in $ \tilde{U} $ lies on the $ x_1 $ or
$ x_2 $ axes. Noting that $ A \subset U $, we see that $ A = U \setminus
\tilde{U} $, and can therefore write $ A \triangleq
\{\mathbf{x} \in U : x_1 = 0\} \cup \{\mathbf{x} \in U : x_2 = 0\} $.

\subsection{Countably infinite intersection}

\textit{Solution.} Note that $ -\lim_{k \rightarrow \infty}e^{-k} = 0 $ and
that $ 1 + \lim_{k \rightarrow \infty}e^{-k} = 1 $. However, note that
$ \forall k \in \mathbb{N} $, $ -e^{-k} < 0 $, $ 1 + e^{-k} > 1 $. Therefore,
$ \bigcap_{k \in \mathbb{N}}A_k = [0, 1] $. This result can be seen by
noting that $ [0, 1] $ is a subset of each open interval in
$ \{A_k\}_{k \in \mathbb{N}} $, where $ A_k \subset A_{k + 1} $,
$ \forall k \in \mathbb{N} $. The intervals therefore more and more tightly
wrap $ [0, 1] $, but since they do not contain their endpoints, in the limit
the left endpoint converges to $ 0 $ and the right endpoint to $ 1 $, which
are included since the endpoints never attain these values.

\subsection{$ \ell^p $ norm balls}

\textit{Solution.} Consider Figure \ref{fig:p_norm_balls}, which plots the
boundaries of $ \bar{B}_{2, \ell^1}(\mathbf{0}, 1) $,
$ \bar{B}_{2, \ell^2}(\mathbf{0}, 1) $, and
$ \bar{B}_{2, \ell^\infty}(\mathbf{0}, 1) $.

\begin{figure}[h!]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            % where to draw the axis lnes
	        axis lines = left,
	        xmin = -1.2, xmax = 1.2, ymin = -1.2, ymax = 1.2,
	        % make the axes equal length
	        axis equal,
	        xlabel = $ x_1 $,
	        ylabel = {$ x_2 $},
	        % set x and y ticks
	        xtick = {-1, 0, 1},
	        ytick = {-1, 0, 1},
	        % put legend in middle of plot, anchored to north, no border
	        legend style = {
	            at = {(0.5, 0.64)}, anchor = north, draw = none
	        }
	    ]
	        %% plotting the 1-norm ball %%
	        \addplot[
	            domain = 0:1,
	            color = viridis_ichi,
	            % don't track plot when creating legend
	            forget plot
	        ]{1 - x};
	        \addplot[
	            domain = 0:1,
	            color = viridis_ichi,
	            % don't track plot when creating legend
	            forget plot
	        ]{x - 1};
	        \addplot[
	            domain = -1:0,
	            color = viridis_ichi,
	            % don't track plot when creating legend
	            forget plot
	        ]{x + 1};
	        \addplot[
	            domain = -1:0,
	            color = viridis_ichi
	        ]{-1 - x};
	        \addlegendentry{$ \partial\bar{B}_{2, \ell^1}(\mathbf{0}, 1) $}
	        %% plotting the 2-norm ball %%
	        \addplot[
	            % note that the domain is in degrees
	            domain = 0:360,
	            % need more samples because we are plotting a curve
	            samples = 100,
	            color = viridis_ni
	        ]({cos(x)}, {sin(x)});
	        \addlegendentry{$ \partial\bar{B}_{2, \ell^2}(\mathbf{0}, 1) $}
	        %% plotting the max-norm ball %%
	        \addplot[
	            domain = -1:1,
	            color = viridis_san,
	            % don't track plot when creating legend
	            forget plot
	        ]({-1}, {x});
	        \addplot[
	            domain = -1:1,
	            color = viridis_san,
	            % don't track plot when creating legend
	            forget plot
	        ]({1}, {x});
	        \addplot[
	            domain = -1:1,
	            color = viridis_san,
	            % don't track plot when creating legend
	            forget plot
	        ]({x}, {-1});
	        \addplot[
	            domain = -1:1,
	            color = viridis_san
	        ]({x}, {1});
	        %\addlegendentry{$ \bar{B}_{2, \ell^\infty}(\mathbf{0}, 1) $}
	        \addlegendentry{$ \partial\bar{B}_{2, \ell^\infty}(\mathbf{0}, 1) $}
	    \end{axis}
    \end{tikzpicture}
    \caption{
        The boundaries of $ \bar{B}_{2, \ell^1}(\mathbf{0}, 1) $,
        $ \bar{B}_{2, \ell^2}(\mathbf{0}, 1) $, and
        $ \bar{B}_{2, \ell^\infty}(\mathbf{0}, 1) $.
    }
    \label{fig:p_norm_balls}
\end{figure}

It's clear that $ \bar{B}_{2, \ell^\infty}(\mathbf{0}, 1) $ contains both
$ \bar{B}_{2, \ell^1}(\mathbf{0}, 1) $ and
$ \bar{B}_{2, \ell^2}(\mathbf{0}, 1) $. In fact, it is true in general that
$ \forall n \in \mathbb{N} + 1 $, $ \forall \mathbf{x} \in \mathbb{R}^n $,
$ \forall r \in (0, \infty) $, that $ \bar{B}_{n, \ell^1}(\mathbf{x}, r)
\subset \bar{B}_{n, \ell^2}(\mathbf{x}, r) \subset
\bar{B}_{n, \ell^\infty}(\mathbf{x}, r) $, which agrees with intuition.

\subsection{Complex numbers}

\textit{Proof.} $ \forall r \in \mathbb{R} $, $ r = r + 0i \in \mathbb{C}
\Rightarrow \mathbb{R} \subseteq \mathbb{C} $. Since $ \forall c \in
\mathbb{R} \setminus \{0\} $, $ r + ci \notin \mathbb{R} $, then
$ \mathbb{R} \ne \mathbb{C} \Rightarrow \mathbb{R} \subset \mathbb{C} $.

\section{Probability}

\subsection{Event unions}

\textit{Proof.} Note that $ A \cup B = A \cup (B \setminus A) $, a disjoint
union. Furthermore, note that $ B = (B \setminus A) \cup (A \cap B) $, also a
disjoint union. By the 3\textsuperscript{rd} Kolmogorov axiom, we thus know
that $ \mathbb{P}(A \cup B) = \mathbb{P}(A) + \mathbb{P}(B \setminus A) $ and
that $ \mathbb{P}(B) = \mathbb{P}(B \setminus A) + \mathbb{P}(A \cap B) $.
Therefore, $ \mathbb{P}(A \cup B) = \mathbb{P}(A) + \mathbb{P}(B) -
\mathbb{P}(A \cap B) $.

\subsection{Sequences of coin flips}

Consider sequences of $ n \in \mathbb{N} $ independent flips of a fair coin
and note that the sample space $ \Omega \triangleq \{\text{H}, \text{T}\}^n $.
\begin{enumerate}[label = \alph*.]
    \item
    \textit{Solution.} Since the coin is fair and the coin flips are
    independent, the probability of getting a particular sequence of flips is
    the product of the probabilities of each flip, and thus
    $ \mathbb{P}\{\omega\} = \left(\frac{1}{2}\right)^n $.

    \item
    \textit{Solution.} Since each of the $ n $ flips can only result in either
    heads or tails, there are two possible outcomes per flip, so for $ n $
    flips, there are $ |\Omega| = 2^n $ possible sequences of flips.

    \item
    \textit{Solution.} Note that we may write $ H_n = \sum_{k = 1}^nX_k $,
    where $ X_1, \ldots X_n $ are mutually independent and
    $ \forall k \in \{1, \ldots n\} $, $ X_k $ is $ 1 $ if the $ k $th flip
    results in heads and is zero otherwise. Since the probability of heads is
    $ \frac{1}{2} $, then, $ \mathbb{E}[X_k] = \frac{1}{2} $ and
    $ \operatorname{Var}(X_k) = \mathbb{E}\big[X_k^2\big] - \mathbb{E}[X_k]^2 = \frac{1}{2} - \frac{1}{4} = \frac{1}{4} $. Therefore,
    \begin{equation*}
        \begin{split}
        \mathbb{E}[H_n] & = \sum_{k = 1}^n\mathbb{E}[X_k] = \frac{1}{2}n \\
        \operatorname{Var}(H_n) & = \sum_{k = 1}^n\operatorname{Var}(X_k) =
            \frac{1}{4}n
        \end{split}
    \end{equation*}

    Here we use the linearity of expectation and properties of variance.
\end{enumerate}

\subsection{More sequences of coin flips}

Consider sequences of $ n \in \mathbb{N} $ independent flips of an
\textit{unfair} coin, where the probability of getting heads is
$ p \in (0, 1) $. Note the sample space $ \Omega $ is the same as the
$ \Omega $ in the previous question.
\begin{enumerate}[label = \alph*.]
    \item
    \textit{Proof.} Let $ \omega_1 \in \Omega $ be the sequence where all
    flips except the last are heads and $ \omega_2 \in \Omega $ be the
    sequence where all flips except the last two are heads. Since the flips
    are independent, it's clear that $ \mathbb{P}\{\omega_1\} =
    p^{n - 1}(1 - p) \ne p^{n - 2}(1 - p)^2 = \mathbb{P}\{\omega_2\} $ since
    $ p \ne 1 / 2 $.

    \item
    \textit{Solution.} Note that $ \mathbb{E}[X_k] = M_1p - M_2(1 - p) $.
    Therefore, by linearity of expectation,
    \begin{equation} \label{eq:unfair_coin_mean}
        \mathbb{E}[U_n] = \sum_{k = 1}^n\mathbb{E}[X_k] = n(M_1p - M_2(1 - p))
    \end{equation}

    \item
    \textit{Solution.} From (\ref{eq:unfair_coin_mean}), we know that
    $ n(M_1p - M_2(1 - p)) = 0 $. Dividing out $ n $, we see that
    \begin{equation} \label{eq:unfair_breakeven}
        M_1 = \frac{M_2(1 - p)}{p}
    \end{equation}

    Supposing $ U_n $ represented the payoff of a betting game, given a fixed
    value of $ p $, (\ref{eq:unfair_breakeven}) gives the relationship between
    $ M_1, M_2 $ that would make the game fair, i.e. expected profit is zero.
\end{enumerate}

\subsection{The Black-Scholes underlying}

\begin{enumerate}[label = \alph*.]
    \item
    \textit{Solution.} By the law of the unconscious statistician, since
    $ Z \sim \mathcal{N}(0, 1) $, we can compute $ \mathbb{E}[S_T \mid S_t] $
    by integrating over a standard normal density. Therefore, we may write
    \begin{equation*}
        \begin{split}
        \mathbb{E}[S_T \mid S_t] & = \int_\mathbb{R}S_te^{
            \left(r - \frac{1}{2}\sigma^2\right)(T - t) + \sigma\sqrt{T - t}z
        }\frac{1}{\sqrt{2\pi}}e^{-\frac{1}{2}z^2}\,dz \\
        & = S_te^{\left(r - \frac{1}{2}\sigma^2\right)(T - t)}
        \int_\mathbb{R}\frac{1}{\sqrt{2\pi}}e^{
            -\frac{1}{2}z^2 + \sigma\sqrt{T - t}z
        }\,dz
        \end{split}
    \end{equation*}
    Completing the square in the exponent, as $ z^2 - 2\sigma\sqrt{T - t}z =
    \big(z - \sigma\sqrt{T - t}\big)^2 - \sigma^2(T - t) $, we have
    \begin{equation*}
        \begin{split}
        \mathbb{E}[S_T \mid S_t] & = S_te^{
            \left(r - \frac{1}{2}\sigma^2\right)(T - t) +
            \frac{1}{2}\sigma^2(T - t)
        }
        \int_\mathbb{R}\frac{1}{\sqrt{2\pi}}e^{
            -\frac{1}{2}(z - \sigma\sqrt{T - t})^2
        }\,dz = S_te^{r(T - t)}
        \end{split}
    \end{equation*}
    The last step follows from the fact that the function being integrated is
    a Gaussian density, so by properties of density functions, its integral
    over $ \mathbb{R} $ must be $ 1 $.

    \item
    \textit{Solution.} Since $ \mathbb{Q}\{S_t \in (P_1, P_2) \mid S_t\} =
    \mathbb{Q}\{S_T < P_2 \mid S_t\} - \mathbb{Q}\{S_T \le P_1 \mid S_t\} $,
    we seek an expression for $ \mathbb{Q}\{S_T \le P_k \mid S_t\} $,
    $ k \in \{1, 2\} $. Recall that for $ Z \sim N(0, 1) $,
    $ S_t \in (0, \infty) $, $ T > t $, we have
    \begin{equation*}
	    S_T \mid S_t \triangleq S_te^{
	        \left(r - \frac{1}{2}\sigma^2\right)(T - t) + \sigma\sqrt{T - t}Z
	    }
	\end{equation*}
    Since inequalities are preserved under monotone transforms, translations,
    and positive scaling, we have
    \begin{equation*}
        \begin{split}
        \mathbb{Q}\{S_T \le P_k \mid S_t\} & = \mathbb{Q}\left\{
            S_te^{
                \left(r - \frac{1}{2}\sigma^2\right)(T - t) +
                \sigma\sqrt{T - t}Z
            } \le P_k \ \middle\vert \ S_t
        \right\} \\
        & = \mathbb{Q}\left\{
            Z \le \frac{
                \log(P_k / S_t) - \left(r - \frac{1}{2}\sigma^2\right)(T - t)
            }{\sigma\sqrt{T - t}} \ \middle\vert \ S_t
        \right\} \\
        & = \Phi(\zeta_k)
	    \end{split}
    \end{equation*}
    Here we define $ \forall k \in \{1, 2\} $ the term $ \zeta_k $, which is
    such that
    \begin{equation*}
        \zeta_k \triangleq \frac{
            \log(P_k / S_t) - \left(r - \frac{1}{2}\sigma^2\right)(T - t)
        }{\sigma\sqrt{T - t}}
    \end{equation*}
    Therefore, we have $ \mathbb{Q}\{S_T \in (P_1, P_2) \mid S_t\} =
    \Phi(\zeta_2) - \Phi(\zeta_1) $.
\end{enumerate}

\end{document}


\chapter{``L'' Standard Extension for Decimal Floating-Point, Version 0.0}

{\bf This chapter is a draft proposal that has not been ratified by
  the Foundation.}

This chapter is a placeholder for the specification of a standard
extension named ``L'' designed to support decimal floating-point
arithmetic as defined in the IEEE 754-2008 standard.

\section{Decimal Floating-Point Registers}

Existing floating-point registers are used to hold 64-bit and 128-bit
decimal floating-point values, and the existing floating-point load
and store instructions are used to move values to and from memory.

\begin{commentary}
Due to the large opcode space required by the fused multiply-add
instructions, the decimal floating-point instruction extension will
require five 25-bit major opcodes in a 30-bit encoding space.
\end{commentary}


\section{The \find algorithm}
\Label{sec:find}

The \find algorithm in the \cxx Standard Library \cite[\S 28.5.5]{cxx-17-draft}
implements \emph{sequential search} for general sequences.
We have modified the generic implementation,
which relies heavily on \cxx templates, to that of a range of
type \valuetype.
The signature now reads:

\begin{lstlisting}[style=acsl-block]

       size_type find(const value_type* a, size_type n, value_type v);
\end{lstlisting}

The function \find returns the least \emph{valid} index \inl{i} of \inl{a}
where the condition \inl{a[i] == v} holds.
If no such index exists then \find returns the length \inl{n} of the array.

As an example, we consider in Figure~\ref{fig:find} an array.
The arrows indicate which indices will be returned by \find for a given value.
Note that the index~9 points \emph{one past end} of the array.
Values that are not contained in the array are colored in gray.

\begin{figure}[hbt]
\centering
\includegraphics[width=0.60\textwidth]{Figures/find.pdf}
\caption{\Label{fig:find}Some simple examples for \find}
\end{figure}

\FloatBarrier

\subsection{Formal specification of \find}

The following listing shows our first attempt specify \specref{find}.

\input{Listings/find.h.tex}

The \inl{requires}-clause indicates that \inl{n} is non-negative and
that the pointer \inl{a} points to $n$~contiguously allocated objects of type
\valuetype (see~\S\ref{sec:frequentPattern}).
%
The \inl{assigns}-clause indicates that \find (as a non-mutating algorithm),
does not modify any memory location outside its scope (see~Page~\pageref{assigns-clause}).

Generally, we only know that \find returns a non-negative index that is less or
equal the length of the array.
However, once we assume more specific situations,
we can also make more precise statements about the returned valued.
This is the reason why we have subdivided the specification of
\find into two behaviors (named \inl{some} and \inl{none}).

\begin{itemize}
\item
The behavior \inl{some} applies if the sought-after value is contained in the array.
We express this condition by using the \inl{assumes}-clause.
The next line expresses that if the assumptions of the behavior are satisfied then
\find will return a valid index.
The algorithm also ensures that  the returned (valid) index \inl{i},
\inl{a[i] == v} holds.
Therefore we define this property in the second postcondition of behavior \inl{some}.
Finally, it is important to express that \find returns the smallest index~\inl{i}
for which \inl{a[i] == v} holds (see last postcondition of behavior \inl{some}).

\item
The behavior \inl{none} covers the case that the sought-after value
is \emph{not} contained in the array (see \inl{assumes}-clause of behavior \inl{none} in
in the contract of\specref{find}.
In this case, \find must return the length \inl{n} of the range \inl{a}.
\end{itemize}

Note that the formula in the \inl{assumes}-clause of the behavior \inl{some}
is the negation of the \inl{assumes}-clause of the behavior \inl{none}.
Therefore, we can express  that these two behaviors are
\emph{complete} and \emph{disjoint}.

\subsection{Implementation of \find}

The noteworthy elements of our implementation of \implref{find} are the
\emph{loop annotations}.
%
The first loop \emph{invariant} is needed to prove that accesses
to~\inl{a} only occur with valid indices. The second  loop \emph{invariant} is needed
for the proof of the postconditions of the
behavior~\inl{some} in the contract of \specref{find}.
It expresses that for each iteration the sought-after value is not
yet found up to that iteration step.
Finally, the  loop \emph{variant} \inl{n-i} is needed to generate correct verification
conditions for the termination of the loop.

\input{Listings/find.c.tex}

\clearpage


\documentclass[letterpaper, 10 pt, conference]{ieeeconf}
\IEEEoverridecommandlockouts
\usepackage{graphicx}
\usepackage{hyperref}

\overrideIEEEmargins

\title{\Huge Dense Optic Flow}
\author{Jiyu Tian}

\begin{document}

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

%-------------------------------------------------------------------------

\section{INTRODUCTION}
In this project we explore the implementation of the Lucas-Kanade method for estimating dense optic flow from a pair of images. We take a pair of greyscale images taken from a video sequence as the input, and output two matrices containing the x and y components of the flow vector at each pixel.
%-------------------------------------------------------------------------
\section{ALGORITHMS DESCRIPTION}

\subsection{Grayscale Conversion}
After reading in two images, we first make them grayscale. We apply the conversion as the following equation:
\begin{equation}
Gray = 0.299 \times R + 0.587 \times G + 0.114 \times B
\end{equation}

If, in the worst case, input images do not have all the three $RGB$ channels, we simply regard the first channel as its grayscale value.

\subsection{Image Gradient}
Before taking the derivative, we smooth the images using Gaussian filter. The spatial intensity gradients $I_x$ and $I_y$ of second image is computed using horizontal and vertical components of Prewitt/Sobel mask. Compute the temporal gradient $I_t$ by subtracting a smoothed version of first image from a smoothed version of the second image.

\subsection{Lucas-Kanade Method}
For a given $N\times N$ window, a system of linear equations can be formed at each pixel by summing over products of gradients in its neighborhood. That is, at each pixel, we have a set of equations:
\begin{equation}
\left[ \begin{array}{cc}
\sum I^2_x & \sum I_xI_y\\
\sum I_xI_y & \sum I^2_y
\end{array} \right] \left[ \begin{array}{c}
V_x\\
V_y
\end{array} \right]= -\left[ \begin{array}{c}
\sum I_xI_t\\
\sum I_yI_t
\end{array} \right]
\end{equation}

The flow vector $[V_x, \ V_y]$ can then be solved at each pixel. $[V_x, \ V_y]$ will be $[0, \ 0]$ if the left matrix do not have full rank.

\subsection{Scaling}
One main assumption for LK method is that the motion is small. If the motion is large and violates this assumption, we need first reduce the resolution of images and then apply the Lucas-Kanade method.
%-------------------------------------------------------------------------
\section{EXPERIMENTAL RESULTS}
Before applying LK method, the input images are converted into grayscale, and smoothed by $7\times7$ Gaussian filter with $\sigma = 1.4$.
\begin{figure}[thpb]
\centering
\includegraphics[width=0.4\textwidth]{raw.png}
\caption{Raw Images}
\label{mosaic}
\end{figure}

\subsection{Scale}

\begin{figure}[thpb]
\centering
\includegraphics[width=0.42\textwidth]{scale5.png}
\caption{Different Scale}
\label{scale}
\end{figure}

\clearpage

\begin{figure}[thpb]
\centering
\includegraphics[width=0.4\textwidth]{window2.png}
\caption{Different Size}
\label{window}
\end{figure}

As we down-sampling the image, the pixel spacing is also reduced. We can see from Figure \ref{scale} that the optical flow tends to be uniform, and the algorithm is performing better. But still we notice that there are several vectors with extreme large magnitude.

\subsection{Window Size}
As we can see from Figure \ref{window}, larger window size also results in better performance given a fixed scaling factor.
\newpage
The window size must be large enough to have sufficient intensity variation for detection, yet a too large one may contain pixels with different disparity.

\subsection{Limitation}
One big limitation of Lucas-Kanade method is that it may fail on large motion. Although this can be slightly modified by reducing image resolution, yet poor resolution can also be a great problem for the method.

Also as we can see from Figure \ref{limit}, the method also highly depends on brightness constancy and camera calibration.

\begin{figure}[thpb]
\centering
\includegraphics[width=0.415\textwidth]{limit.png}
\caption{Limitation}
\label{limit}
\end{figure}
%-------------------------------------------------------------------------
\section{CONCLUSION}
In this project, we implemented Lucas-Kanade method for optical flow estimation. We also explored influence from various parameters, as well as the limitation of the method.
\vfill

\end{document}


\documentclass{article}

\usepackage{mathrsfs,amssymb,amsmath}

\begin{document}

\section{Prove that the given set of functions converges weakly but not strongly}

Consider $\mathscr{X} = C[0,1]$, the Banach Space of continuous functions on $[0,1]$ with the supremum norm.

Consider sequence of functions $(f_n)_{n \ge 2}$, where

$$
f_n(x) = \left\{
        \begin{array}{ll}
            xn & \quad 0 \leq x \leq \frac{1}{n} \\
            2 - nx & \quad \frac{1}{n} < x \leq \frac{2}{n} \\
            0 & \quad \frac{2}{n} < x \le 1
        \end{array}
    \right.
 $$

 $D_n = \{ \alpha \in \mathbb{F}: | \alpha | \le 1 \}$

(Starts at 0, goes up to 1 at 1/n, down to 0 at 2/n, and stays flat from there).

Denote $0 \in \mathscr{X}$ as function $z(x) = 0$ for $0 \le x \le 1$.

Claim is that $f_n \rightharpoonup z$, but that $f_n \not\rightarrow z$.

First, $|| f_n - 0 || = || f_n || = 1$ for all $n$, so $f_n$ does not converge to $0$ (TODO: Prove it doesn't converge at all?)

Suppose $L$ is a continuous linear functional such that $\lim_{n \to \infty} L(f_n) \ne 0$. Therefore there is some subsequence $b_n$ and $\varepsilon$ such that $L(b_n) > \varepsilon$. Choose a further subsequence of $b_n$, called $c_n$ where the $c_n$ have disjoint support

$d_n = \sum_{i=1}^n c_n$

\end{document}