\documentclass{llncs}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage[utf8]{inputenc}
\usepackage{mathpartir}

\input{macros}

\begin{document}
\section{First Attempt: Uniqueness and Linearity}
\label{sec:first-attempt:-uniq}

Syntax
\begin{align*}
  e &::= x \mid \LAM xte \mid \APP ee \mid \LET xee \mid \REC{ \overline{a=e}} \mid \GET ea \mid \UPD eae \\
  \ell &::= \LIN \mid \UN & \UN & \sqsubset \LIN \\
  u &::= \READ \mid \WRITE & \WRITE & \sqsubset \READ \\
  t &::= b\,\ell\, u \\
  b &::= t \to t \mid \REC{ \overline{a : t}}
\end{align*}
Every type has two attributes, linearity $\ell$ and uniqueness $u$.

An environment $A$ is a partial mapping from variables to types. Environment overriding $A=A'\OVERRIDE A''$ is defined as the pointwise extension of a ternary relation on types. We write $x:t\,-$ to indicate that no binding for $x$ exists.
\begin{mathpar}
  \inferrule{}{b\,\UN\,\READ  =  b\,\UN\,\READ   \OVERRIDE' b\,\UN\,\READ}

  \inferrule{}{b\,\UN\,\WRITE  =  b\,\UN\,\WRITE  \OVERRIDE b\,-}
  \\
  \inferrule{b = b' \OVERRIDE' b''}
  {b\,\UN\,\WRITE  =  b'\,\UN\,\READ   \OVERRIDE' b''\, \UN\,\WRITE}

  \inferrule{t = t' \OVERRIDE' t''}{t = t' \OVERRIDE t''}
  \\
  \inferrule{}{t\,\LIN\,u           =  t\,\LIN\,u           \OVERRIDE t\,-}

  \inferrule{}{t\,\LIN\,u           =  t\,-                     \OVERRIDE t\, \LIN\,u}
  \\
  \inferrule{
    t_i = t_i' \OVERRIDE' t_i''
  }{
    \REC{ a_1:t_1, \dots } =     \REC{ a_1:t_1', \dots } \OVERRIDE'     \REC{ a_1:t_1'', \dots }
  }

  \inferrule{}{t_1\to t_2 = t_1 \to t_2 \OVERRIDE' t_1 \to t_2}
\end{mathpar}
For $n>2$ define $A= A_1 \OVERRIDE A_2 \OVERRIDE \dots \OVERRIDE A_n$ by $A= A_1 \OVERRIDE A_1'$ and $A_1' = A_2 \OVERRIDE \dots \OVERRIDE A_n$.

\newpage
If any record component is linear, then the record must be linear, too.
If any of the components has write premission, then the record must have it, too.
\begin{mathpar}
  \inferrule{
    A = A_1 \OVERRIDE \dots \OVERRIDE A_n \\
    A_i \vdash e_i : t_i\,\ell_i\,u_i \\
    \ell_i \sqsubseteq \ell \\
    u \sqsubseteq u_i
  }{
    A \vdash \REC{ \overline{a_i=e_i}} : \REC{ \overline{a_i : t_i}}\,\ell\, u
  }
\end{mathpar}
Reading from a record is a use of the record: If the record is linear, then there can be only one use. The fields that are dropped  must not be linear. Thus, a linear record can have at most a single linear field and no other field can be accessed. (That seems overly restrictive, but could be amended by a record elimination that reads more than one field at a time.)

Uniqueness of the record does not matter for reading. The attributes of the field's type remain as they are, if the record has write permission. (If the record has only read permission, then none of the fields can have write permission, so the attributes remain the same, too.)
\begin{mathpar}
  \inferrule{
    A \vdash e : \REC{ a:t \mid r}\,\ell\,u \\
    \UN (r)
  }{
    A \vdash \GET ea : t
  }
\end{mathpar}
To write to a record, we need write permission. The type of the overwritten field must be unrestricted because the old contents is dropped. (It would be good to have an update operation that works on linear fields.)
\begin{mathpar}
  \inferrule{
    A_0 \vdash e_0 : \REC{ a:t \mid r}\,\ell\,\WRITE \\
    A_1 \vdash e_1 : t \\
    \UN (t) \\
    A = A_0 \OVERRIDE A_1
  }{
    A \vdash \UPD {e_0}a{e_1} : \REC{ a:t \mid r}\,\ell\,u
  }
\end{mathpar}

\begin{mathpar}
  \inferrule{
  }{
    A, x: t \vdash x:t
  }
\end{mathpar}
Uniqueness does not matter for a function. However, a function that captures a linear variable or a variable with write permission must be linear. A function that captures a record with read permission can be influenced by subsequent writes to that record. Thus the following combinations are possible.
\begin{itemize}
\item $y:t \in A$ is readonly, which means that no future writes through $y$ are possible. Readonly variables impose no restriction on the function type, but currently we have no notion of readonly.
\item $y:t \in A$ needs write permission to typecheck $e$. This requires $\ell = \LIN$, so that writes are only executed at  most once. If $\ell \ne\LIN$, then $y$ needs to be copied each time the closure is invoked.
\item $y:t \in A$ is linear requires that $\ell = \LIN$, so that the linear stuff is used exactly once.
\end{itemize}
\begin{mathpar}
  \inferrule{
    A, x:t \vdash e : t'
  }{
    A \vdash \LAM x t e : (t \to t')\,\ell\,u
  }
\end{mathpar}
Function application has no particular restrictions, it seems.
\begin{mathpar}
  \inferrule{
    A_0 \vdash e_0 : (t_1 \to t_2)\, \ell\,u \\
    A_1 \vdash e_1 : t_1 \\
    A = A_0 \OVERRIDE A_1
  }{
    A \vdash \APP{e_0}e_1 : t_2
  }
\end{mathpar}

\clearpage{}

\section{Second Attempt: Uniqueness Only}
\label{sec:second-attempt:-uniq}

Syntax
\begin{align*}
  e &::= x \mid \LAM xte \mid \APP ee \mid \LET xee  \mid c \mid e+e \mid \IF eee\\
  &\mid \REC{ \overline{a=e}} \mid \GET ea \mid \UPD[u] eae \\
  u,v &::= \READ \mid \WRITE & \WRITE & \sqsubset \READ \\
  s,t &::= \NUM \mid s \to t \mid \REC[u]{ \overline{a : t}}
\end{align*}
Only record types have a uniqueness attribute $u$.
The $u$ attribute on the update operation determines whether it is destructive ($u=\WRITE$) or copying ($u=\READ$).

\begin{mathpar}
  \inferrule{
    A_2 \vdash e_0 : \REC[u]{ a_1:t \mid r} \dashv A_1 \\
    A_1 \vdash e_1 : t \dashv A_0
  }{
    A_2 \vdash
    \UPD[u] {e_0}{a_1}{e_1} : \REC[v]{ a_1:t \mid r}
    \dashv A_0
  }

  \inferrule{
    A_1 \vdash e : \REC[\READ]{ a:t \mid r}
    \dashv A_0
  }{
    A_1 \vdash \GET ea : t
    \dashv A_0
  }

  \inferrule{
    A_{i+1} \vdash e_i : t_i \dashv A_i \\
  }{
    A_{n+1} \vdash \REC{ \overline{a_i=e_i}} : \REC[u]{ \overline{a_i : t_i}}
    \dashv A_0
  }

  \inferrule{
    t_0 = t_1 \OVERRIDE t
  }{
    A, x: t_1 \vdash x : t \dashv A, x:t_0
  }

    \inferrule{
    A_2 \vdash e_0 : (t_1 \to t_2) \dashv A_1 \\
    A_1 \vdash e_1 : t_1 \dashv A_0
  }{
    A_2 \vdash
    \APP{e_0}e_1 : t_2
    \dashv A_0
  }

    \inferrule{
      A_1, x:t_1 \vdash
      e : t
      \dashv A_0^{X:\READ}, x:t_0 \\
      X = \FREE (\LAM x {t_0} e)
  }{
    A_0^{X:\READ} \vdash
    \LAM x {t_0} e : (t_0 \to t)
    \dashv A_0
  }

  \inferrule{
    A_4 \vdash e_0 : \NUM \dashv A_3 \\
    A_1 \vdash e_1 : t_1 \dashv A_0 \\
    A_2 \vdash e_2 : t_2 \dashv A_0 \\
    t_1, t_2 \sqsubseteq t_3 \\
    A_1, A_2 \sqsubseteq A_3
  }{
    A_4 \vdash \IF {e_0}{e_1}{e_2} : t_3 \dashv A_0
  }
\end{mathpar}

Operationally, the lambda takes a deep copy of all free variables.
Thus, it must not propagate write permission to the left.
As the lambda may be invoked multiple times, the free variables must
not have write permission.
The notation $A^{X:\READ}$ means that all permissions in the types of the
variables in $X$ are set to $\READ$.

\clearpage{}

\section{Third Attempt: Field Effects}
\label{sec:third-attempt:-field}

\begin{figure}[tp]
  \begin{align*}
    e,f,g,r
    &::= x \mid \LAM xte \mid \APP fe \mid \FIX xtf \\
    & \mid \LET xef  \mid c \mid f+g \mid \IF efg\\
    &\mid \REC{ \overline{a=e}} \mid \GET ra \mid \UPD rae \\
    O,P,Q &::= \PVAR \mid \NOTHING \mid \READ \mid \WRITE & \NOTHING & \sqsubseteq\WRITE\sqsubset \READ \\
    s,t &::= \TVAR \mid \NUM \mid s \to t \mid \REC{ \overline{a :^P t}} \\
    S, T& ::= \forall \TVAR. T \mid \forall \PVAR. T \mid t
  \end{align*}
  \caption{Syntax}
  \label{fig:syntax-3}
\end{figure}
In the syntax in Figure~\ref{fig:syntax-3}, effects are attached to single record labels, intuitively $P, Q \subseteq
\{ \READ, \WRITE \}$ but it does not make sense to distinguish
$\{\WRITE, \READ\}$ from $\{\WRITE\}$, so we take the three element lattice as indicated.

\begin{figure}[tp]
  \begin{mathpar}
    \inferrule{
      A = A_1 \OVERRIDE \dots \OVERRIDE A_n \\
      A_i \vdash e_i : t_i }{ A \vdash \REC{ \overline{a_i=e_i}} :
      \REC{ \overline{a_i :^{P_i} t_i}} }

    \inferrule { A \vdash e : \REC{ a:^\READ t \mid r}
      % \\ \READ \in P
    } { A \vdash \GET ea : t }

    \inferrule{
      A_0 \vdash e_0 : \REC{ a:^\WRITE t \mid r} \\
      A_1 \vdash e_1 : t \\
      % \WRITE \in P \\
      A = A_0 \OVERRIDE A_1 }{ A \vdash \UPD {e_0}a{e_1} : \REC{ a:^P
        t \mid r} }
    \\
    \inferrule{ }{ A, x: t \vdash x: t }

    \inferrule{
      A_0, x:t \vdash e : t' \\
      A = A_0^* }{ A \vdash \LAM x t e : (t \to t') }

  \inferrule[PT: Subtyping needed?]
  { A, x:s \vdash f :t  \\ s \SUB t}
  { A \vdash \FIX xtf : t}

  \inferrule{
    A_0 \vdash e_0 : (t_1 \to t_2) \\
    A_1 \vdash e_1 : t_1 \\
    A = A_0 \OVERRIDE A_1
  }{
    A \vdash \APP{e_0}e_1 : t_2
  }

  \inferrule
  { A_1 \vdash e_1 : t_1 \\
    A_2, x : t_1 \vdash e_2 : t_2 \\
    A = A_1 \OVERRIDE A_2
  }
  { A \vdash \LET x{e_1} e_2 : t}

  \inferrule
  {
    A = A_0 \OVERRIDE A_1 \\
    \JOIN{A_1}{A_2}{ A_3} \\
    \SPLIT{t_1}{  t_2}{ t_3} \\\\
    A_0 \vdash e : \NUM \\
    A_2 \vdash f : t_2 \\
    A_3 \vdash g : t_3
  }
  {
    A \vdash \IF e f g : t_1
  }
  % PT: is subtyping really needed? We are propagating demand information *backwards*, so (e.g.) the demand
  % on the branches of a conditional is the same as the demand on the result of the conditional.
  % \inferrule[Sub]
  % {A \vdash e : s \\ s \SUB t}
  % {A \vdash e : t}
  \end{mathpar}
  \caption{Typing rules.
  It seems like the subtyping in the FIX rule can alway be eliminated by unfolding the fixed point
  once.
  If the demand on $t$ is stronger or weaker than $s$, then unfolding will mediate the demand.
  NB the directionality of subtyping is wrong if interpolated from the lattice ordering (as it is presently).
}
  \label{fig:field-effects}
\end{figure}
% \begin{figure}[tp]
%   \begin{mathpar}
%     \inferrule{}{\NUM \SUB \NUM}
%
%     \inferrule{
%       s' \SUB s \\ t \SUB t'
%     }{
%       s \to t \SUB s' \to t'
%     }
%
%     \inferrule{
%       \overline{s_i \SUB t_i} \\
%       \overline{P_i \sqsubseteq Q_i}
%     }{
%       \REC{ \overline{a_i :^{P_i} s_i}}
%       \SUB
%       \REC{ \overline{a_i :^{Q_i} t_i}}
%     }
%   \end{mathpar}
%   \caption{Subtyping}
%   \label{fig:subtyping-3}
% \end{figure}
\begin{figure}[tp]
  % \begin{align*}
  %   \NUM \sqcup \NUM
  %   & = \NUM
  %   \\
  %   (s \to t) \sqcup (s' \to t')
  %   & = (s \sqcap s') \to (t \sqcup t') \qquad ???
  %   \\
  %   \REC{ \overline{a_i :^{P_i} s_i}} \sqcup \REC{ \overline{a_i :^{Q_i} t_i}}
  %   &= \REC{ \overline{a_i :^{P_i \sqcup Q_i} (s_i \sqcup t_i)}}
  % \end{align*}
  \begin{mathpar}
    \inferrule{}{
      \SPLIT \NUM \NUM \NUM
    }

    \inferrule{}{
      \JOIN \NUM \NUM \NUM
    }
    \\
    \inferrule{
      \JOIN{s}{ s_1}{ s_2} \\
      \SPLIT{t}{t_1}{t_2}
    }{
      \SPLIT{s \to t}{s_1 \to t_1}{s_2 \to t_2}
    }

    \inferrule{
      \SPLIT{s}{ s_1}{ s_2} \\
      \JOIN{t}{t_1}{t_2}
    }{
      \JOIN{s \to t}{s_1 \to t_1}{s_2 \to t_2}
    }
    \\
    \inferrule
    {\overline{\SPLIT{s_i}{s_i'}{s_i''}}}
    {\SPLIT{\REC{ \overline{a_i :^{P_i} s_i}}}{\REC{ \overline{a_i :^{P_i} s_i'}}}{\REC{ \overline{a_i :^{P_i} s_i''}}}}

    \inferrule
    {\overline{\JOIN{s_i}{s_i'}{s_i''}} \\
    \overline{O_I = P_i \sqcup Q_i}}
    {\JOIN{\REC{ \overline{a_i :^{O_i} s_i}}}{\REC{ \overline{a_i :^{P_i} s_i'}}}{\REC{ \overline{a_i :^{Q_i} s_i''}}}}
  \end{mathpar}
  \caption{Bounding and splitting operations on types. The intuition is that \SPLITK{} propagates
    information in the positive positions of a type downwards towards the subexpressions. At the
    same time, it joins information in the negative positions and propagates the join upwards. The
    \JOINK{} operation works dually.}
  \label{fig:bounding-3}
\end{figure}
The novel ingredient in the typing rules are the propagation and splitting rules for the effect annotations on
record labels indicated by the ternary override relation $\OVERRIDE$.
Function closures have ``infinite'' splitting $(a :^P t)^*$ into $(a :^Q t)$.
\begin{itemize}
\item $P = \NOTHING$ propagate $Q=\NOTHING$.
\item $P = \READ$ propagate $Q = \WRITE$, no action required inside closure.
\item $P = \WRITE$ propagate $Q = \WRITE$, perform copy inside of closure.
\end{itemize}
 Propagating $\READ$ is not safe in both cases because that enables a write operation on the same
 record after the closure has been constructed. Alternatively, we could have another READONLY mode,
 which prevents subsequent write operations.

Function application has no particular restrictions, it seems.
Let improves on lambda combined with app because infinite splitting is avoided.

Look at combining $a :^O t \OVERRIDE a:^P t$ upwards to $a : ^Q t$:

$Q= O \OVERRIDE P$ is defined by:
\begin{itemize}
\item if $O=\NOTHING$ or $P=\NOTHING$ (field only used in one branch), then $Q = O \sqcup P$, no
  action required
\item if $O\sqsubseteq \READ$: $Q = O \sqcup P$,
  no further action required as potential writing in $P$ happens after reading
\item if $O = \WRITE$, $P = \READ$, then there are two choices
  \begin{itemize}
  \item $Q = \READ$ insert copy, propagate left to the write operation
  \item $Q = \WRITE$ insert copy, propagate right to the read operation
  \end{itemize}
  First option is preferred because it keeps writes local and prevents
  further copies downstream.
\item if $O=\WRITE$, $P =\WRITE$: $Q = \WRITE$ insert copy,
  propagate left (or right)
\end{itemize}
We need an operation that returns $\READ$ if a copy is required and
$\NOTHING$ otherwise.
\begin{align*}
  \COPY (O, P) &=
                 \begin{cases}
                   \READ & O=\WRITE \wedge \READ \sqsubseteq P \\
                   \NOTHING & \text{otherwise}
                 \end{cases}
  \\
  \COPY (\overline{O}, \overline{P})
               &= \bigsqcup_i \COPY (O_i, P_i)
  \\
  \COPY (\NUM, \NUM)
               & = \NOTHING
  \\
  \COPY (t_1 \to t_2, t_1 \to t_2)
               & = \NOTHING
  \\
  \COPY (\REC{ \overline{a_i :^{O_i} t_i'}}, \REC{ \overline{a_i :^{P_i} t_i''}})
  &=\COPY (\overline{O_i}, \overline{P_i})
    \sqcup \bigsqcup_i \overline{\COPY (t_i', t_i'')}
\end{align*}

Combining environments: pointwise

Combining types and records
\begin{mathpar}
  \inferrule{}{ \NUM = \NUM \OVERRIDE \NUM}

  \inferrule{}{t_1 \to t_2 = t_1 \to t_2 \OVERRIDE t_1 \to t_2}
  \\
  \inferrule{
    Q_i = (O_i \OVERRIDE P_i)
    \sqcup \COPY (\overline{O_i}, \overline{P_i})
    \sqcup \bigsqcup_i \overline{\COPY (t_i', t_i'')} \\
    t_i = t_i' \OVERRIDE t_i''
  }{
    \REC{ \overline{a_i :^{Q_i} t_i}}
    =
    \REC{ \overline{a_i :^{O_i} t_i'}}
    \OVERRIDE
    \REC{ \overline{a_i :^{P_i} t_i''}}
  }
\end{mathpar}

\begin{mathpar}
  \inferrule
  {}
  {
    \REC{ {a :^{Q_a} t_a}, {b :^{Q_b} t_b}}
    =
    \REC{ {a :^{O_a} t_a}, {b :^{O_b} t_b}}
    \OVERRIDE
    \REC{ {a :^{P_a} t_a}, {b :^{P_b} t_b}}
  }
\end{mathpar}

\clearpage
\begin{mathpar}
  \begin{array}{|c|c||c|c||c|c|l}
    Q_a&Q_b&O_a&O_b&P_a&P_b& \\
    \hline
    P_a &\NOTHING& \NOTHING & \NOTHING & P_a & \NOTHING & \\
    O_a & \NOTHING& O_a & \NOTHING & \NOTHING & \NOTHING & \\
    P_a\sqcup\READ   &\NOTHING& \READ & \NOTHING & P_a & \NOTHING & \\
    \READ&\READ& \WRITE & \NOTHING & \READ & \NOTHING
                       &  \text{copy, propagate left to write}\\
    \WRITE &\READ& \WRITE & \NOTHING & \WRITE & \NOTHING
                       &\text{copy, propagate left to write} \\
  \end{array}

  \begin{array}{|c|c||c|c||c|c|l}
    Q_a&Q_b&O_a&O_b&P_a&P_b& \\
    \hline
    P_a & P_b & \NOTHING & \NOTHING & P_a & P_b & \\
    O_a & P_b & O_a & \NOTHING & \NOTHING & P_b & \\
    P_a\sqcup\READ   &P_b& \READ & \NOTHING & P_a & P_b & \\
    \READ&P_b\sqcup\READ& \WRITE & \NOTHING & \READ & P_b
                       &  \text{copy, propagate left to write}\\
    \WRITE & P_b\sqcup\READ & \WRITE & \NOTHING & \WRITE & P_b
                       &\text{copy, propagate left to write} \\
  \end{array}

  \begin{array}{|c|c||c|c||c|c|l}
    Q_a&Q_b&O_a&O_b&P_a&P_b& \\
    \hline
    P_a & O_b & \NOTHING & O_b & P_a & \NOTHING & \\
    O_a & O_b & O_a & O_b & \NOTHING & \NOTHING & \\
    P_a\sqcup\READ  & O_b & \READ & O_b & P_a & \NOTHING & \\
    \READ&O_b \sqcup\READ& \WRITE & O_b & \READ & \NOTHING
                       &  \text{copy, propagate left to write}\\
    \WRITE &O_b\sqcup\READ& \WRITE & O_b & \WRITE & \NOTHING
                           &\text{copy, propagate left to write} \\
  \end{array}

\begin{array}{|c|c||c|c||c|c|l}
    Q_a&Q_b&O_a&O_b&P_a&P_b& \\
    \hline
    P_a &P_b\sqcup\READ& \NOTHING & \READ & P_a & P_b & \\
    O_a & P_b\sqcup\READ& O_a & \READ & \NOTHING & P_b & \\
    P_a\sqcup\READ   &P_b\sqcup\READ& \READ & \READ & P_a & P_b & \\
    \READ&P_b\sqcup\READ& \WRITE & \READ & \READ & P_b
                       &  \text{copy, propagate left to write}\\
    \WRITE &P_b\sqcup\READ& \WRITE & \READ & \WRITE & P_b
                       &\text{copy, propagate left to write} \\
  \end{array}

  \begin{array}{|c|c||c|c||c|c|l}
    Q_a&Q_b&O_a&O_b&P_a&P_b& \\
    \hline
    P_a\sqcup\READ &\READ& \NOTHING & \WRITE & P_a & \READ
                           & \text{copy, propagate left to write} \\
    \READ & \READ& O_a & \WRITE & \NOTHING & \READ
                           & \text{copy, propagate left to write} \\
    P_a\sqcup\READ   &\READ& \READ & \WRITE & P_a & \READ
                           &  \text{copy, propagate left to write}\\
    \READ&\READ& \WRITE & \WRITE & \READ & \READ
                       &  \text{copy, propagate left to write}\\
    \WRITE &\READ& \WRITE & \WRITE & \WRITE & \READ
                       &\text{copy, propagate left to write} \\
  \end{array}

  \begin{array}{|c|c||c|c||c|c|l}
    Q_a&Q_b&O_a&O_b&P_a&P_b& \\
    \hline
    P_a\sqcup\READ &\WRITE& \NOTHING & \WRITE & P_a & \WRITE
                           & \text{copy, propagate left to write} \\
    \READ & \WRITE& O_a & \WRITE & \NOTHING & \WRITE
                           & \text{copy, propagate left to write} \\
    P_a\sqcup\READ   &\WRITE& \READ & \WRITE & P_a & \WRITE
                           &  \text{copy, propagate left to write}\\
    \READ&\WRITE& \WRITE & \WRITE & \READ & \WRITE
                       &  \text{copy, propagate left to write}\\
    \WRITE &\WRITE& \WRITE & \WRITE & \WRITE & \WRITE
                       &\text{copy, propagate left to write} \\
  \end{array}
\end{mathpar}
A copy operation reads \textbf{every} field. Hence, a copy operation
caused by one field implies a read effect on all fields.
\clearpage{}
Consider the typing for
\begin{align*}
  f
  &=(\LAM r{\REC{ a:^\WRITE \NUM}} \UPD r a{42})
  &&: \REC{ a:^\WRITE \NUM} \to \REC{ a:^\PVAR \NUM}
  \\
  g
  &=(\LAM r{\REC{a :^\NOTHING \NUM}} \REC{a = 0})
  &&: \REC{a :^\NOTHING \NUM} \to \REC{ a:^\PVAR \NUM}
  \\
  &\IF e{f}{g}
  &&: \REC{ a:^\WRITE \NUM} \to \REC{ a:^\PVAR \NUM}
\end{align*}
This propagation using $\WRITE\sqcup\NOTHING = \WRITE$ is correct,
\textbf{but} the propagation of $\WRITE$  may cause a useless copy if $e$ is false.

The typing is analogous to pushing the conditional into the lamdas.
\begin{align*}
  r : \REC{ a:^\WRITE \NUM} & \vdash \UPD r a{42} &&: \REC{ a:^\PVAR \NUM}
  \\
  r : \REC{ a:^\NOTHING \NUM} & \vdash \REC{a = 0} &&: \REC{ a:^\PVAR \NUM}
  \\
  r : \REC{ a:^\WRITE \NUM}& \vdash \IF e{( \UPD r a{42})}{(\REC{a = 0})} &&: \REC{ a:^\PVAR \NUM}
  \\
  \vdash \LAM r {\REC{ a:^\WRITE \NUM}} & \IF e{( \UPD r a{42})}{(\REC{a = 0})}
                                                  &&: {\REC{ a:^\WRITE \NUM}} \to \REC{ a:^\PVAR \NUM}
\end{align*}

\textbf{Let's finally get down to polymorphism. }
\begin{align*}
  \mathit{id} =
  \LAM x\TVAR x & : \forall\TVAR.\TVAR\to\TVAR
  &&\text{seems fine, just instantiate}
  \\
  \mathit{dup} =
  \LAM x\TVAR x & : \forall\TVAR.\TVAR\to(\TVAR,\TVAR)
\end{align*}
\begin{verbatim}
lambda r.
  let (r1, r2) = dup r
  in  (r1 { a = 42 }, r2.a)
-- should behave like "write before read"
lambda r.
      (r  { a = 42 }, r .a)
\end{verbatim}
\begin{align*}
  r : \REC{ a:^\WRITE \NUM} & \vdash \UPD r a{42} &&: \REC{ a:^\PVAR \NUM}
  \\
  r : \REC{ a:^\READ \NUM} & \vdash  r. a &&: \NUM
  \\
  r : \REC{ a:^\READ \NUM} & \vdash (\UPD r a{42}, r. a)  &&: \REC{ a:^\PVAR \NUM} \times \NUM
\end{align*}
Using \textit{dup}
\begin{align*}
  r_1 : \REC{ a:^\WRITE \NUM} & \vdash \UPD r a{42} &&: \REC{ a:^\PVAR \NUM}
  \\
  r_2 : \REC{ a:^\READ \NUM} & \vdash  r. a &&: \NUM
  \\
  r_1 : \REC{ a:^\WRITE \NUM},
  r_2 : \REC{ a:^\READ \NUM} & \vdash  (\UPD r a{42} , r. a) &&: \REC{ a:^\PVAR \NUM} \times \NUM
\end{align*}
\textbf{Suggestion}
Distiguish positive and negative occurrences of a type variable.
\begin{itemize}
\item If a negative occurrence of a type variable is instantiated with a record type, then its fields are all set to
  \READ.
\item Every instantiation of a positive occurrence of a type variable with a record type is allowed
  to have \textbf{different field permissions}. If a record type ends up with permission $\WRITE$,
  then the corresponding record is copied after instantiation.
\item If there is only one positive occurrence under an affine type constructor, then its permission
  is pushed through.
\end{itemize}
That does not quite solve the problem:
\begin{verbatim}
let (r1, r2) = (r, r)
in  (r1 { a = 42 }, r2.a)
\end{verbatim}
Here the reads and writes to \texttt{r1} and \texttt{r2} get out of order due to aliasing in the
pair.

Practical instance: the state monad. Turns out that the bind cannot be handled satisfactorily by the
suggestion on the table because we don't know that \texttt{s} is linear. Seems like linearity is
useful knowledge for polymorphic arguments.
\begin{verbatim}
type ST s a = s -> (s,a)

-- a: -, +
-- s: -, +
returnST :: a -> ST s a
returnST a = \s -> (s, a)

-- b: -, +
-- a: -, +
-- s: +, -, +, -, -, +
bindST :: ST s a -> (a -> ST s b) -> ST s b
bindST ma f = \s -> let (s1,a) = ma s
                    in  f s1 a
\end{verbatim}
\end{document}


\chapterimage{head2.png} % Chapter heading image
\chapter{D and log-likelihood}
\section{Basic Equations}

\begin{definition}[log-likelihood function]
\begin{equation}
    \norm{\alpha_{t_{\tau}}} = \prod_{i=1}^{\tau} \norm{\left < \hat{\alpha}_{t_{i-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_i}
\end{equation}
Therefore,
\begin{equation}
    p(\textbf{y}) = p(y_1,\cdots,y_T) = \norm{\alpha_{t_{T}}}^2 = \prod_{i=1}^{T} \norm{\left < \hat{\alpha}_{t_{i-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_i}^2
\end{equation}
and log-likelihood is
\begin{equation}
    l(\theta) = \ln{(p(\textbf{y}))} = \ln{(\prod_{i=1}^{T} \norm{\left < \hat{\alpha}_{t_{i-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_i}^2)} = 2\sum_{i=1}^{T} \ln{\norm{\left < \hat{\alpha}_{t_{i-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_i}}
\end{equation}
Other details are explained in Section(\ref{sec:scalefactor}).
\end{definition}
As you can see, $l(\theta)$ is proportional to $\ln{\norm{\left < \hat{\alpha}_{t_{i-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_i}}$. And the information of diffusion coefficient is buried in $e^{-\textbf{H}\Delta t}$.

\section{Simple example}
We will use the following as our model to discuss:
\begin{center}
    \includegraphics[scale=0.8]{ch2/four_states_ex.png}
\end{center}

\begin{definition}[Alpha-0]
Given $x_0=0$, and we use an Gaussian($\sigma=2.0$) to approximate delta-function
\begin{align}
    \left< \alpha_{t_0} | x_0 \right> = \frac{p(x_0)}{\sqrt{p_{\rm{eq}}(x_0)}} = \frac{p(x_0)}{\rho_{\rm{eq}}(x_0)}
\end{align}
and
\begin{align}
    p(x_0) = \rho_{eq}(x_0)[\left< \alpha_{t_0} | \psi_1 \right> \psi_1(x_0) + \left< \alpha_{t_0} | \psi_2 \right>\psi_2(x_0)+\cdots+\left< \alpha_{t_0} | \psi_{72} \right>\psi_{72}(x_0)]
\end{align}
where
\begin{align*}
    \left< \alpha_{t_0} | \psi_i \right> = \int \frac{p(x_0)}{\rho_{\rm{eq}}(x_0)} \psi_i(x_0) dx_0
\end{align*}
Notice that
\begin{align*}
    \left< \alpha_{t_0} | \psi_1 \right> = \int \frac{p(x_0)}{\rho_{\rm{eq}}(x_0)} \psi_1(x_0) dx_0 = \pm 1
\end{align*}
because
\begin{align*}
    \psi_1(x) = \rho_{\rm{eq}}(x) ~~\text{or}~~\psi_1(x) = -\rho_{\rm{eq}}(x)
\end{align*}
\begin{center}
    \includegraphics[scale=0.45]{ch7/alpha_t0.pdf}
\end{center}
Furthermore, we let
\begin{align}
    \left< \hat{\alpha}_{t_0} \right|  = \left< \alpha_{t_0} \right|
\end{align}
and we know
\begin{align*}
    \left< \hat{\alpha}_{t_0} \right|  =
    \begin{bmatrix}
        \left< \alpha_{t_0} | \psi_1 \right> & \left< \alpha_{t_0} | \psi_2 \right> & \cdots & \left< \alpha_{t_0} | \psi_{N_v} \right>
    \end{bmatrix}
\end{align*}
\end{definition}

\begin{definition}[Alpha-0 after relaxation]
Then, let us see
\begin{align*}
    \left< \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} &=
    \begin{bmatrix}
        \left< \alpha_{t_0} | \psi_1 \right> e^{-\lambda_1 \Delta t} & \left< \alpha_{t_0} | \psi_2 \right> e^{-\lambda_2 \Delta t} & \cdots & \left< \alpha_{t_0} | \psi_{N_v} \right>e^{-\lambda_{N_v} \Delta t}
    \end{bmatrix} \\
    &=
    \begin{bmatrix}
        \pm 1 & \left< \alpha_{t_0} | \psi_2 \right> e^{-\lambda_2 \Delta t} & \cdots & \left< \alpha_{t_0} | \psi_{N_v} \right>e^{-\lambda_{N_v} \Delta t}
    \end{bmatrix} \\
\end{align*}
and we know that
\begin{align*}
    \left< \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \left| x_1 \right> &= \frac{\int p(x_0)p(x_1|x_0) dx_0}{\rho_{\rm eq}(x_1)} = \frac{p(x_1)}{\rho_{\rm eq}(x_1)}
\end{align*}
so
\begin{align*}
    p(x_1)&=\rho_{\rm{eq}}(x_1)\left[ \left< \alpha_{t_0} | \psi_1 \right>\psi_1(x_1) +  \left< \alpha_{t_0} | \psi_2 \right> e^{-\lambda_2 \Delta t} \psi_2(x_1) + \cdots + \left< \alpha_{t_0} | \psi_{N_v} \right>e^{-\lambda_{N_v} \Delta t}  \psi_{N_v}(x_1) \right] \\
    &= \rho_{\rm{eq}}(x_1)\left[ \rho_{\rm{eq}}(x_1) +  \left< \alpha_{t_0} | \psi_2 \right> e^{-\lambda_2 \Delta t} \psi_2(x_1) + \cdots + \left< \alpha_{t_0} | \psi_{N_v} \right>e^{-\lambda_{N_v} \Delta t}  \psi_{N_v}(x_1) \right]
\end{align*}
\begin{center}
    \includegraphics[scale=0.45]{ch7/alpha_t0_e_dt.pdf}
\end{center}
From the integration of probability
\begin{align*}
    \int p(x_1) dx_1 = \int p_{\rm{eq}}(x_1) dx_1 + \sum_{i=2}^{N_v} \int  \left< \alpha_{t_0} | \psi_{i} \right>e^{-\lambda_{i} \Delta t} \rho_{\rm{eq}}(x_1) \psi_{i}(x_1) dx_1 = 1
\end{align*}
Since $\int p_{\rm{eq}}(x) dx=1$, we get
\begin{align*}
    \sum_{i=2}^{N_v} \int  \left< \alpha_{t_0} | \psi_{i} \right>e^{-\lambda_{i} \Delta t} \rho_{\rm{eq}}(x_1) \psi_{i}(x_1) dx_1 = 0
\end{align*}
and we get the consistent result numerically.
\end{definition}

\begin{definition}[Alpha-1]
\begin{equation}
        \left< \alpha_{t_1} \right| = \left< \hat{\alpha}_{t_0} \right| e^{-\textbf{H} \Delta t} \textbf{y}_1
\end{equation}
$\left< \alpha_{t_1} \right|$ carries "the joint probability amplitude" of observing all the photon data during $[0, t_1]$ and the system state at $t_1$, that is
\begin{align*}
    \left< \alpha_{t_1} | x_1 \right> = \frac{p(x_1,y_1)}{\rho_{\rm{eq}}(x_1)}
\end{align*}
\begin{center}
    \includegraphics[scale=0.45]{ch7/alpha_t1.pdf}
\end{center}
Then, we can do normalization based on $p(x_1,y_1)$
\begin{align*}
    p(x_1,y_1) &=  \rho_{\rm{eq}}(x) \left< \alpha_{t_1} | x_1 \right> \\
    p(x_1|y_1) &= \frac{p(x_1,y_1)}{p(y_1)}
\end{align*}
and we define $\left< \hat{\alpha}_{t_1}|x_1 \right>$ is
\begin{equation}
    \left< \hat{\alpha}_{t_1}|x_1 \right> = \frac{p(x_1|y_1)}{ \rho_{\rm{eq}}(x_1) }
\end{equation}
therefore, $\left< \hat{\alpha}_{t_1} \right|$ is the vector in eigenspace
\begin{equation}
        \left< \hat{\alpha}_{t_1} \right| =
        \begin{bmatrix}
            \left< \hat{\alpha}_{t_1} | \psi_1 \right> & \left< \hat{\alpha}_{t_1} | \psi_2 \right> & \cdots & \left< \hat{\alpha}_{t_1} | \psi_{N_v} \right>
        \end{bmatrix}
\end{equation}
Notice that
\begin{align*}
    \left< \hat{\alpha}_{t_1} | \psi_1 \right> = \int \frac{p(x_1|y_1)}{\rho_{\rm{eq}}(x_1)} \psi_1(x_1) dx_1= \pm 1
\end{align*}
\begin{center}
    \includegraphics[scale=0.45]{ch7/alpha_t1_hat.pdf}
\end{center}
\end{definition}

\begin{definition}[Alpha-1 after relaxation]
\begin{align*}
    \left< \hat{\alpha}_{t_{1}} \right| e^{-\textbf{H}\Delta t} \left| x_2 \right> &= \frac{\int p(x_1,x_2|y_1) dx_1}{\rho_{\rm eq}(x_2)} = \frac{p(x_2|y_1)}{\rho_{\rm eq}(x_2)}
\end{align*}
\begin{center}
    \includegraphics[scale=0.45]{ch7/alpha_t1_e_dt.pdf}
\end{center}
\end{definition}

\begin{definition}[Alpha-2]
\begin{align*}
    \left< \hat{\alpha}_{t_1} \right| e^{-\textbf{H} \Delta t} \textbf{y}_2 \left| x_2 \right> = \frac{p(x_2, y_2|y_1)}{\rho_{\rm eq}(x_2)}
\end{align*}
\begin{center}
    \includegraphics[scale=0.45]{ch7/alpha_t1_hat_e_dt_y2.pdf}
\end{center}
\begin{equation}
    \left< \hat{\alpha}_{t_2}|x_2 \right> = \frac{1}{ \rho_{\rm{eq}}(x_2) }\frac{p(x_2,y_2|y_1)}{p(y_2|y_1)} = \frac{p(x_2|y_1,y_2)}{ \rho_{\rm{eq}}(x_2)}
\end{equation}
\begin{center}
    \includegraphics[scale=0.45]{ch7/alpha_t2_hat.pdf}
\end{center}
\end{definition}

\begin{definition}[Alpha-2 after relaxation]
\begin{align*}
    \left< \hat{\alpha}_{t_{2}} \right| e^{-\textbf{H}\Delta t} \left| x_3 \right> &= \frac{\int p(x_2,x_3|y_1,y_2) dx_2}{\rho_{\rm eq}(x_3)} = \frac{p(x_3|y_1,y_2)}{\rho_{\rm eq}(x_3)}
\end{align*}
\begin{center}
    \includegraphics[scale=0.45]{ch7/alpha_t2_e_dt.pdf}
\end{center}
\end{definition}

\begin{definition}[Alpha-3]
\begin{align*}
    \left< \hat{\alpha}_{t_2} \right| e^{-\textbf{H} \Delta t} \textbf{y}_3 \left| x_3 \right> = \frac{p(x_3, y_3|y_1, y_2)}{\rho_{\rm eq}(x_3)}
\end{align*}
\begin{center}
    \includegraphics[scale=0.45]{ch7/alpha_t2_hat_e_dt_y3.pdf}
\end{center}
\begin{equation}
    \left< \hat{\alpha}_{t_3}|x_3 \right> = \frac{1}{ \rho_{\rm{eq}}(x_3) }\frac{p(x_3,y_3|y_1,y_2)}{p(y_3|y_1,y_2)} = \frac{p(x_3|y_1,y_2,y_3)}{ \rho_{\rm{eq}}(x_3)}
\end{equation}
\begin{center}
    \includegraphics[scale=0.45]{ch7/alpha_t3_hat.pdf}
\end{center}
\end{definition}

\begin{definition}[Alpha-3 after relaxation]
\begin{align*}
    \left< \hat{\alpha}_{t_{3}} \right| e^{-\textbf{H}\Delta t} \left| x_4 \right> &= \frac{\int p(x_3,x_4|y_1,y_2,y_3) dx_3}{\rho_{\rm eq}(x_4)} = \frac{p(x_4|y_1,y_2,y_3)}{\rho_{\rm eq}(x_4)}
\end{align*}
\begin{center}
    \includegraphics[scale=0.45]{ch7/alpha_t3_e_dt.pdf}
\end{center}
\end{definition}

\begin{definition}[Alpha-4]
\begin{align*}
    \left< \hat{\alpha}_{t_3} \right| e^{-\textbf{H} \Delta t} \textbf{y}_4 \left| x_4 \right> = \frac{p(x_4, y_4|y_1, y_2, y_3)}{\rho_{\rm eq}(x_4)}
\end{align*}
\begin{center}
    \includegraphics[scale=0.45]{ch7/alpha_t3_hat_e_dt_y4.pdf}
\end{center}
\begin{equation}
    \left< \hat{\alpha}_{t_4}|x_4 \right> = \frac{1}{ \rho_{\rm{eq}}(x_4) }\frac{p(x_4,y_4|y_1,y_2,y_3)}{p(y_4|y_1,y_2,y_3)} = \frac{p(x_4|y_1,y_2,y_3,y_4)}{ \rho_{\rm{eq}}(x_4)}
\end{equation}
\begin{center}
    \includegraphics[scale=0.45]{ch7/alpha_t4_hat.pdf}
\end{center}
\end{definition}

\begin{definition}[Likelihood and log-likelihood]
Likelihood function can be written as the product of normalization factors
\begin{align}
    p(y_1,y_2,y_3,y_4) = p(y_1) p(y_2|y_1) p(y_3|y_1,y_2) p(y_4|y_1,y_2,y_3)
\end{align}
and the log-likelihood is the sum of them
\begin{align}
    \ln{(p(y_1,y_2,y_3,y_4))} = \ln{(p(y_1))} + \ln{(p(y_2|y_1))} + \ln{(p(y_3|y_1,y_2))} + \ln{(p(y_4|y_1,y_2,y_3))}
\end{align}
\end{definition}

\begin{definition}[Diffusity and log-likelihood]
Let us reexamine how we calculate $p(y_1)$. First,
\begin{align*}
    p(y_1) = \int p(x_1,y_1) dx_1 = \int \left< \hat{\alpha}_{t_0} \right| e^{-\textbf{H} \Delta t} \textbf{y}_1 \left| x_1 \right> \rho_{\rm{eq}}(x_1) dx_1
\end{align*}
and recall that
\begin{align*}
    \textbf{H} = -D \frac{\partial^2}{\partial x^2} + \frac{1}{2}D\frac{d F(x)}{dx} + \frac{1}{4} D F^{2}(x)
\end{align*}
and we define $\textbf{H}'=\frac{\textbf{H}}{D}$ , that is
\begin{equation}
    \textbf{H}' = -\frac{\partial^2}{\partial x^2} + \frac{1}{2}\frac{d F(x)}{dx} + \frac{1}{4} F^{2}(x)
\end{equation}
Therefore,
\begin{align*}
    e^{-\textbf{H} \Delta t} = e^{-D\textbf{H}' \Delta t} = \left(e^{-\textbf{H}' \Delta t}\right)^D
\end{align*}
then
\begin{align*}
    \ln{(p(y_1))} = \ln{\left( \int \left< \hat{\alpha}_{t_0} \right| \left(e^{-\textbf{H}' \Delta t}\right)^D \textbf{y}_1 \left| x_1 \right> \rho_{\rm{eq}}(x_1) dx_1 \right)}
\end{align*}
We can write down the general form for normalization factors
\begin{align}
    p(y_n|y_1,...,y_{n-1}) = \int \left< \hat{\alpha}_{t_{n-1}} \right| \left(e^{-\textbf{H}' \Delta t}\right)^D \textbf{y}_n \left| x_n \right> \rho_{\rm{eq}}(x_n) dx_n
\end{align}
Finally, the log-likelihood can be express as
\begin{align}
    \ln{(p(y_1,...,y_n))} = \sum_{i=1}^{n} \ln{\left( \int \left< \hat{\alpha}_{t_{n-1}} \right| \left(e^{-\textbf{H}' \Delta t}\right)^D \textbf{y}_n \left| x_n \right> \rho_{\rm{eq}}(x_n) dx_n \right)}
\end{align}
\end{definition}

\begin{definition}[Maximizing log-likelihood along D: Mathematical perspective]
According to
\begin{align*}
    \ln{(p(y_1,...,y_n))} = \sum_{i=1}^{n} \ln{\left( \int \left< \hat{\alpha}_{t_{n-1}} \right| \left(e^{-\textbf{H}' \Delta t}\right)^D \textbf{y}_n \left| x_n \right> \rho_{\rm{eq}}(x_n) dx_n \right)}
\end{align*}
The problem of maximizing is to find how many times of doing operation $e^{-\textbf{H}' \Delta t}$ which can maximize log-likelihood. Especially in MD case, because $\Delta t$ is given, so our mission is to find best $D$.

In other cases, we may formulate the log-likelihood as
\begin{align*}
    \ln{(p(y_1,...,y_n))} = \sum_{i=1}^{n} \ln{\left( \int \left< \hat{\alpha}_{t_{n-1}} \right| e^{-\textbf{H}' \Delta \tau} \textbf{y}_n \left| x_n \right> \rho_{\rm{eq}}(x_n) dx_n \right)}
\end{align*}
where $\Delta \tau = D \Delta t$. In this case, the problem is to find best $\Delta \tau$ which can maximize log-likelihood.
\end{definition}
\newpage
\begin{definition}[Maximizing log-likelihood along D: Probability perspective]
\begin{center}
    \includegraphics[scale=0.45]{ch7/py1_Dtest.pdf}
\end{center}
\end{definition}

\section{One photon example: The derivative of log-likelihood with respect to D}
\begin{definition}[Graphical model]
\begin{center}
    \includegraphics[scale=1.0]{ch7/alpha_example.pdf}
\end{center}
\end{definition}

\begin{definition}[Likelihood function]
\begin{equation}
    p(y_1) = \norm{\left < \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1}^2
\end{equation}
where
\begin{equation}
    \norm{\left < \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1} = \left(
        \left( \left< \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1 \left| \psi_1 \right> \right)^2 +
        \cdots +
        \left( \left< \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1 \left| \psi_{72} \right> \right)^2
        \right)^{1/2}
\end{equation}
\end{definition}

\begin{definition}[log-likelihood]
\begin{equation}
    \ln{(p(y_1))} = 2 \ln{\norm{\left < \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1}}
\end{equation}
Since basic rules for logarithms, we get
\begin{equation}
    \ln{(p(y_1))} = \ln{\left(\left( \left< \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1 \left| \psi_1 \right> \right)^2 + \cdots +
    \left( \left< \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1 \left| \psi_{72} \right> \right)^2\right)}
\end{equation}
\end{definition}

\begin{definition}[log-likelihood: Another way]
\begin{align}
    \left< \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} =
    \begin{bmatrix}
        \left< \hat{\alpha}_{t_{0}} | \psi_1 \right> & \left< \hat{\alpha}_{t_{0}} | \psi_2 \right> e^{-\lambda_2\Delta t} & \cdots & \left< \hat{\alpha}_{t_{0}} | \psi_{72} \right> e^{-\lambda_{72}\Delta t}
    \end{bmatrix}
\end{align}
The element of $\textbf{y}_1$ is
\begin{align}
    [\textbf{y}_1]_{ij} = \left<\psi_i \right| \hat{\textbf{y}} \left|\psi_j\right>
\end{align}
Therefore,
\begin{align*}
    \left< \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1 \left| \psi_j \right> &= \sum_{i=1}^{72} \left< \hat{\alpha}_{t_{0}} | \psi_i \right> e^{-\lambda_i\Delta t} \left<\psi_i \right| \hat{\textbf{y}} \left|\psi_j\right> \\
    &= \left< \hat{\alpha}_{t_{0}} | \psi_1 \right> \left<\psi_1 \right| \hat{\textbf{y}} \left|\psi_j\right> + \sum_{i=2}^{72} \left< \hat{\alpha}_{t_{0}} | \psi_i \right> e^{-\lambda_i\Delta t} \left<\psi_i \right| \hat{\textbf{y}} \left|\psi_j\right>
\end{align*}
As you can see, we can express log-likelihood as
\begin{equation}
    \ln{(p(y_1))} = \ln{\left( \sum_{j=1}^{N_v} \left[ \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B_{ij} \right]^2  \right)}
\end{equation}
where
\begin{align*}
        B_{ij} &= \left< \hat{\alpha}_{t_{0}} | \psi_i \right> \left<\psi_i \right| \hat{\textbf{y}} \left|\psi_j\right>
\end{align*}
\end{definition}

\begin{definition}[Likelihood as a function of D]
\begin{align}
    g(D) &= (p(y_1)) = \sum_{j=1}^{N_v} \left[ \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B_{ij} \right]^2 \\
    g(D) &=\left( \left< \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1 \left| \psi_1 \right> \right)^2 + \cdots +\left( \left< \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1 \left| \psi_{N_v} \right> \right)^2
\end{align}
\end{definition}

\begin{definition}[The derivative of log-likelihood with respect to D]
\begin{align*}
    \frac{d \ln{(g(D))}}{dD} = \frac{1}{g(D)}\frac{d g(D)}{dD}
\end{align*}
We wish to find $D_{\rm{max}}$ which can maximize the log-likelihood, so we want to solve the following equation
\begin{equation}
    \frac{d g(D)}{dD} = \frac{d \left( \left< \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1 \left| \psi_1 \right> \right)^2}{d D} + \cdots + \frac{d \left( \left< \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1 \left| \psi_{72} \right> \right)^2}{d D} = 0
\end{equation}
\end{definition}

\begin{definition}[The derivative of single term]
\begin{align*}
    f_j^2(D) = \left( \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B_{ij} \right)^2
\end{align*}
and from the chain rule
\begin{align*}
    \frac{d f_j^2(D)}{dD} = 2 f_j(D) \frac{d f_j(D)}{dD} = 2 \left( \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B_{ij} \right) \frac{d \left( \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B_{ij} \right) }{dD}
\end{align*}
and because $\lambda_i=D \lambda'_i$
\begin{align*}
    \frac{d \left( \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B_{ij} \right) }{dD} &= \sum_{i=1}^{N_v} -\lambda'_i \Delta t  e^{-\lambda_i\Delta t} \left< \hat{\alpha}_{t_{0}} | \psi_i \right>  \left<\psi_i \right| \hat{\textbf{y}} \left|\psi_j\right> \\
    &= \sum_{i=1}^{N_v} -\lambda'_i \Delta t  e^{-\lambda_i\Delta t} B_{ij}
\end{align*}
As you can see, when $\Delta t \gg 1$, it leads to $\frac{d \left( \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B_{ij} \right) }{dD} \rightarrow 0$, and
\begin{align*}
    \frac{d f_j^2(D)}{dD} \approxeq 0
\end{align*}
\end{definition}

\begin{definition}[The derivative of all terms]
\begin{equation}
    \frac{d g(D)}{dD} = \sum_{j=1}^{72}\frac{d f_j^2(D)}{dD} = 2 \sum_{j=1}^{72} \left[ \left( \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B_{ij} \right) \frac{d \left( \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B_{ij} \right) }{dD} \right] = 0
\end{equation}
Therefore,
\begin{equation}
    \frac{d g(D)}{dD} = 2 \sum_{j=1}^{72} \left[ \left( \sum_{i=1}^{N_v}e^{-D\lambda'_i\Delta t}B_{ij} \right) \left(  \sum_{i=1}^{N_v} -\lambda'_i \Delta t  e^{-D\lambda'_i\Delta t} B_{ij}  \right) \right]
\label{loglikeexpansion}
\end{equation}
The goal is to find $D_{\rm{max}}$ which can make eq.(\ref{loglikeexpansion}) become 0.
\end{definition}

\begin{definition}[Taylor expansion for $g(D)$]
\begin{align*}
    g(D) = \sum_{j=1}^{N_v} \left[ \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B_{ij} \right]^2
\end{align*}
substitute $e^{-D\lambda'_i\Delta t} \approxeq 1 - \lambda'_i\Delta t D$ into the above equation
\begin{align*}
    g(D) &= \sum_{j=1}^{N_v} \left[\left(\sum_{i=1}^{N_v}B_{ij}\right) - \left( D\Delta t \sum_{i=1}^{N_v} \lambda'_i B_{ij} \right) \right]^2 = \sum_{j=1}^{N_v} \left(b_j - D\Delta t c_j \right)^2 \\
    &= \sum_{j=1}^{N_v} b^2_j - 2 D\Delta t b_j c_j + D^2 (\Delta t)^2 c_j^2
\end{align*}
\end{definition}

\begin{definition}[Taylor expansion for $\frac{dg(D)}{dD}$]
\begin{equation}
    e^{-D\lambda'_i\Delta t} \approxeq 1 - \lambda'_i\Delta t D
\end{equation}
Therefore,
\begin{align*}
    \sum_{i=1}^{N_v}e^{-D\lambda'_i\Delta t}B_{ij} &= \sum_{i=1}^{N_v} \left( 1 - \lambda'_i\Delta t D \right) B_{ij} = \sum_{i=1}^{N_v} B_{ij} - D \Delta t \sum_{i=1}^{N_v} \lambda'_i B_{ij} \\
    \sum_{i=1}^{N_v} -\lambda'_i \Delta t  e^{-D\lambda'_i\Delta t} B_{ij} &= \sum_{i=1}^{N_v} \left(-\lambda'_i \Delta t   + (\lambda'_i \Delta t)^2 D \right)B_{ij}
    = D (\Delta t)^2 \sum_{i=1}^{N_v} (\lambda'_i)^2 B_{ij} - \Delta t \sum_{i=1}^{N_v} \lambda'_i B_{ij}
\end{align*}
and then
\begin{align*}
    &\left( \sum_{i=1}^{N_v}e^{-D\lambda'_i\Delta t}B_{ij} \right) \left( \sum_{i=1}^{N_v} -\lambda'_i \Delta t  e^{-D\lambda'_i\Delta t} B_{ij} \right) = \\
    &\left( \sum_{i=1}^{N_v} B_{ij} - D \Delta t \sum_{i=1}^{N_v} \lambda'_i B_{ij} \right)  \left(  D (\Delta t)^2 \sum_{i=1}^{N_v} (\lambda'_i)^2 B_{ij} - \Delta t \sum_{i=1}^{N_v} \lambda'_i B_{ij} \right) = \\
    & D (\Delta t)^2 (\sum_{i=1}^{N_v} B_{ij}) (\sum_{i=1}^{N_v} (\lambda'_i)^2 B_{ij})
    - \Delta t  (\sum_{i=1}^{N_v} B_{ij}) (\sum_{i=1}^{N_v} \lambda'_i B_{ij})
    - D^2 (\Delta t)^3 (\sum_{i=1}^{N_v} \lambda'_i B_{ij}) (\sum_{i=1}^{N_v} (\lambda'_i)^2 B_{ij}) \\
    & + D  (\Delta t)^2 (\sum_{i=1}^{N_v} \lambda'_i B_{ij})^2
\end{align*}
Here, I let
\begin{align*}
    a_j &= \sum_{i=1}^{N_v} (\lambda'_i)^2 B_{ij} \\
    b_j &= \sum_{i=1}^{N_v} B_{ij} \\
    c_j &= \sum_{i=1}^{N_v} \lambda'_i B_{ij}
\end{align*}
and we simplify the above equation
\begin{align*}
    \left( \sum_{i=1}^{N_v}e^{-D\lambda'_i\Delta t}B_{ij} \right) \left( \sum_{i=1}^{N_v} -\lambda'_i \Delta t  e^{-D\lambda'_i\Delta t} B_{ij} \right) = -\Delta t b_j c_j + D (\Delta t)^2 \left( a_j b_j - c^2_j \right) - D^2 (\Delta t)^3 a_j c_j
\end{align*}
and
\begin{equation}
    \frac{dg(D)}{dD} \approxeq \frac{d\tilde{g}_{\rm{v0}}(D)}{dD} = 2 \sum_{j=1}^{N_v} \left[ -\Delta t b_j c_j + D (\Delta t)^2 \left( a_j b_j - c^2_j \right) - D^2 (\Delta t)^3 a_j c_j  \right]
\end{equation}
If we discard the term of $D^2$, we get
\begin{equation}
    \frac{dg(D)}{dD} \approxeq \frac{d\tilde{g}_{\rm{v1}}(D)}{dD} = 2 \sum_{j=1}^{N_v} \left[ -\Delta t b_j c_j + D (\Delta t)^2 \left( a_j b_j - c^2_j \right) \right]
\end{equation}
Then, we approximate the derivative of $\ln{p(y_1)}$ by
\begin{align*}
    \rm{Taylor-v0:}&~~\frac{d\ln{p(y_1)}}{dD} = \frac{1}{g(D)}\frac{d\tilde{g}_{\rm{v0}}(D)}{dD}    \\
    \rm{Taylor-v1:}&~~\frac{d\ln{p(y_1)}}{dD} = \frac{1}{g(D)}\frac{d\tilde{g}_{\rm{v1}}(D)}{dD}
\end{align*}
\begin{center}
    \includegraphics[scale=0.8]{ch7/taylor_try.pdf}
\end{center}
\end{definition}

\section{Two photons}
\begin{definition}[Likelihood function]
\begin{equation}
        p(y_1,y_2) = p(y_1) p(y_2|y_1) = \norm{\left < \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1}^2 \norm{\left < \hat{\alpha}_{t_{1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_2}^2
\end{equation}
\end{definition}

\begin{definition}[log-likelihood]
\begin{align*}
        \ln{p(y_1,y_2)} &= \ln{p(y_1)} + \ln{p(y_2|y_1)}  \\
        &=2 \ln{\norm{\left < \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1}} + 2 \ln{\norm{\left < \hat{\alpha}_{t_{1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_2}}
\end{align*}
\end{definition}

\begin{definition}[Derivative]
\begin{align*}
    \frac{d\ln{p(y_1,y_2)}}{dD} = \frac{d \ln{p(y_1)}}{dD} + \frac{d \ln{p(y_2|y_1)}}{dD}
\end{align*}
Let us focus on
\begin{align*}
    \frac{d \ln{p(y_2|y_1)}}{dD} = 2\frac{d \ln{\norm{\left < \hat{\alpha}_{t_{1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_2}}}{dD}
\end{align*}
\end{definition}

\begin{definition}[$\norm{\left < \hat{\alpha}_{t_{1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_2}$]
\begin{align*}
    \norm{\left < \hat{\alpha}_{t_{1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_2} = \left(
        \left( \left< \hat{\alpha}_{t_{1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_2 \left| \psi_1 \right> \right)^2 +
        \cdots +
        \left( \left< \hat{\alpha}_{t_{1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_2 \left| \psi_{Nv} \right> \right)^2
        \right)^{1/2}
\end{align*}
Then,
\begin{align*}
    \ln{\norm{\left < \hat{\alpha}_{t_{1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_2}} = \frac{1}{2}\ln{\left( \sum_{j=1}^{N_v} \left[ \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t} \left< \hat{\alpha}_{t_{1}} | \psi_i \right> \left<\psi_i \right| \hat{\textbf{y}}_{2} \left|\psi_j\right>\right]^2  \right)}
\end{align*}
There are two terms which are the function of $D$ in the above equation. The first is $e^{-\lambda_i\Delta t}$, and the second is $\left< \hat{\alpha}_{t_{1}} | \psi_i \right>$. Because
\begin{align*}
    \left< \hat{\alpha}_{t_{1}} | \psi_i \right> = \frac{ \left < \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t}\textbf{y}_1 \left| \psi_i \right> }{\norm{ \left < \hat{\alpha}_{t_{0}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1 }}
\end{align*}
\end{definition}

\begin{definition}[Complicated in multiple photons]
\begin{align*}
    \ln{\norm{\left < \hat{\alpha}_{t_{\tau-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_{\tau}}} = \frac{1}{2}\ln{\left( \sum_{j=1}^{N_v} \left[ \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t} \left< \hat{\alpha}_{t_{\tau-1}} | \psi_i \right> \left<\psi_i \right| \hat{\textbf{y}}_{\tau} \left|\psi_j\right>\right]^2  \right)}
\end{align*}
In the above equation, there is a nested dependence to $D$ in $\left< \hat{\alpha}_{t_{\tau-1}} | \psi_i \right>$
\begin{align*}
    \left< \hat{\alpha}_{t_{\tau-1}} | \psi_i \right> = \frac{ \left < \hat{\alpha}_{t_{\tau-2}} \right| e^{-\textbf{H}\Delta t}\textbf{y}_{\tau-1} \left| \psi_i \right> }{\norm{ \left < \hat{\alpha}_{t_{\tau-2}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_{\tau-1} }}
\end{align*}
This causes the diffculty to write down the analytical expression for $\frac{d\ln{p(y_1,...,y_{N_p})}}{dD}$
\end{definition}

\section{The general model for multiple photons}
\begin{definition}[Graphical model]
The model is shown as
\begin{center}
    \includegraphics[scale=1.0]{ch7/multiple_photons_graphical.pdf}
\end{center}
\end{definition}

\begin{definition}[Likelihood function]
\begin{equation}
    p(y_1,...,y_{N_p}) = p(y_1) p(y_2|y_1)\cdots p(y_{N_p}|y_1,...,y_{N_p -1}) =\prod_{i=1}^{N_p} \norm{\left < \hat{\alpha}_{t_{i-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_i}^2
\end{equation}
where
\begin{equation}
    \norm{\left < \hat{\alpha}_{t_{i-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_i} = \left(
        \left( \left< \hat{\alpha}_{t_{i-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1 \left| \psi_1 \right> \right)^2 +
        \cdots +
        \left( \left< \hat{\alpha}_{t_{i-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_1 \left| \psi_{72} \right> \right)^2
        \right)^{1/2}
\end{equation}
\end{definition}

\begin{definition}[log-likelihood]
\begin{align*}
    \ln{(p(y_1,...,y_{N_p}))} &= \ln{p(y_1)} + \ln{p(y_2|y_1)} + \cdots + \ln{p(y_{N_p}|y_1,...,y_{N_p -1})} \\
    &=2\sum_{i=1}^{N_p} \ln{\norm{\left < \hat{\alpha}_{t_{i-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_i}}
\end{align*}
Since basic rules for logarithms, we get
\begin{align*}
    \ln{p(y_{\tau}|y_1,...,y_{\tau-1})} = \ln{\left(\left( \left< \hat{\alpha}_{t_{\tau-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_{\tau} \left| \psi_1 \right> \right)^2 + \cdots +
    \left( \left< \hat{\alpha}_{t_{\tau-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_{\tau} \left| \psi_{72} \right> \right)^2\right)}
\end{align*}
\begin{center}
    \includegraphics[scale=0.3]{ch7/l_numerical.pdf}
\end{center}
\end{definition}

\begin{definition}[log-likelihood: Another way]
\begin{align*}
    \left< \hat{\alpha}_{t_{\tau - 1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_{\tau} \left| \psi_j \right> = \sum_{i=1}^{N_v} \left< \hat{\alpha}_{t_{0}} | \psi_i \right> e^{-\lambda_i\Delta t} \left<\psi_i \right| \hat{\textbf{y}}_{\tau} \left|\psi_j\right>
\end{align*}
As you can see, we can express log-likelihood as
\begin{equation}
    \ln{p(y_{\tau}|y_1,...,y_{\tau-1})} = \ln{\left( \sum_{j=1}^{N_v} \left[ \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B^{(\tau)}_{ij} \right]^2  \right)}
\end{equation}
where
\begin{align*}
    B^{(\tau)}_{ij} &= \left< \hat{\alpha}_{t_{\tau-1}} | \psi_i \right> \left<\psi_i \right| \hat{\textbf{y}}_{\tau} \left|\psi_j\right>
\end{align*}
\end{definition}

\begin{definition}[Likelihood as a function of D]
\begin{align}
    g_{\tau}(D) &= p(y_{\tau}|y_1,...,y_{\tau-1}) = \sum_{j=1}^{N_v} \left[ \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B^{(\tau)}_{ij} \right]^2 \\
    g_{\tau}(D) &=\left( \left< \hat{\alpha}_{t_{\tau-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_{\tau} \left| \psi_1 \right> \right)^2 + \cdots +\left( \left< \hat{\alpha}_{t_{\tau-1}} \right| e^{-\textbf{H}\Delta t} \textbf{y}_{\tau} \left| \psi_{N_v} \right> \right)^2
\end{align}
Therefore, the likelihood function is
\begin{equation}
    p(y_1,...,y_{N_p}) = p(y_1) p(y_2|y_1)\cdots p(y_{N_p}|y_1,...,y_{N_p -1}) =\prod_{\tau=1}^{N_p} g_{\tau}(D)
\end{equation}
and the log-likelihood function is
\begin{equation}
    \ln{p(y_1,...,y_{N_p})} = \sum_{\tau=1}^{N_p} \ln{g_{\tau}(D)}
\end{equation}
\begin{center}
    \includegraphics[scale=0.6]{ch7/l_analytical.pdf}
\end{center}
\end{definition}

\begin{definition}[The derivative of log-likelihood with respect to D]
\begin{align*}
    \frac{d \ln{p(y_1,...,y_{N_p})}}{dD} = \sum_{\tau=1}^{N_p} \frac{d \ln{g_{\tau}(D)}}{dD} = \sum_{\tau=1}^{N_p}\frac{1}{g_{\tau}(D)}\frac{d g_{\tau}(D)}{dD}
\end{align*}
where
\begin{align*}
    \frac{d g_{\tau}(D)}{dD} = 2\sum_{j=1}^{N_v} f_{\tau,j}(D) \frac{d f_{\tau,j}(D)}{dD}
\end{align*}
so
\begin{align*}
    \frac{d \ln{p(y_1,...,y_{N_p})}}{dD} = \sum_{\tau=1}^{N_p} \frac{1}{g_{\tau}(D)} \left( 2\sum_{j=1}^{N_v} f_{\tau,j}(D) \frac{d f_{\tau,j}(D)}{dD} \right)
\end{align*}
\begin{center}
    \includegraphics[scale=0.4]{ch7/dldD_compare.pdf}
\end{center}
As you can see, there is something inconsistent between the numerical derivative and the analytical derivative.
\end{definition}

\begin{definition}[The derivative of single term]
Recall that
\begin{align*}
    g_{\tau}(D) = \sum_{j=1}^{N_v} \left[ \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B^{(\tau)}_{ij} \right]^2 = \sum_{j=1}^{N_v} f_{\tau,j}^2(D)
\end{align*}
where
\begin{align*}
        f_{\tau,j}^2(D) = \left(\sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B^{(\tau)}_{ij}\right)^2
\end{align*}
and from the chain rule
\begin{align*}
    \frac{d f_{\tau,j}^2(D)}{dD} = 2 f_{\tau,j}(D) \frac{d f_{\tau,j}(D)}{dD} = 2 \left( \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B^{(\tau)}_{ij} \right) \frac{d \left( \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B^{(\tau)}_{ij} \right)}{dD}
\end{align*}
and because $\lambda_i=D \lambda'_i$
\begin{align*}
    \frac{d f_{\tau,j}(D)}{dD} = \frac{d \left( \sum_{i=1}^{N_v}e^{-\lambda_i\Delta t}B^{(\tau)}_{ij} \right) }{dD} = \sum_{i=1}^{N_v} -\lambda'_i \Delta t  e^{-D\lambda'_i\Delta t} B^{(\tau)}_{ij}
\end{align*}
\end{definition}

\begin{definition}[Taylor expansion for $\frac{dg_{\tau}(D)}{dD}$]
\begin{align*}
    e^{-D\lambda'_i\Delta t} \approxeq 1 - \lambda'_i\Delta t D
\end{align*}
We get
\begin{align*}
    \frac{dg_{\tau}(D)}{dD} &\approxeq \frac{d \tilde{g}_{\tau}(D)}{dD} \\
    &= 2 \sum_{j=1}^{N_v} \left[ -\Delta t b^{(\tau)}_j c^{(\tau)}_j + D (\Delta t)^2 \left( a^{(\tau)}_j b^{(\tau)}_j - \left(c^{(\tau)}_j\right)^2 \right) \right]
\end{align*}
where
\begin{align*}
    a^{(\tau)}_j &= \sum_{i=1}^{N_v} (\lambda'_i)^2 B^{(\tau)}_{ij} \\
    b^{(\tau)}_j &= \sum_{i=1}^{N_v} B^{(\tau)}_{ij} \\
    c^{(\tau)}_j &= \sum_{i=1}^{N_v} \lambda'_i B^{(\tau)}_{ij}
\end{align*}
\end{definition}

\begin{definition}[The derivative of log-likelihood]
Recall that
\begin{align*}
    \frac{d \ln{p(y_1,...,y_{N_p})}}{dD} = \sum_{\tau=1}^{N_p} \frac{d \ln{g_{\tau}(D)}}{dD} = \sum_{\tau=1}^{N_p}\frac{1}{g_{\tau}(D)}\frac{d g_{\tau}(D)}{dD}
\end{align*}
and we use $\frac{d \tilde{g}_{\tau}(D)}{dD}$ to approximate $\frac{dg_{\tau}(D)}{dD}$, we get
\begin{align*}
    \frac{d \ln{p(y_1,...,y_{N_p})}}{dD} &\approxeq \sum_{\tau=1}^{N_p}\frac{1}{g_{\tau}(D)} \frac{d \tilde{g}_{\tau}(D)}{dD}\\
    &= 2\sum_{\tau=1}^{N_p}\frac{1}{g_{\tau}(D)} \left[ \sum_{j=1}^{N_v} -\Delta t b^{(\tau)}_j c^{(\tau)}_j + D (\Delta t)^2 \left( a^{(\tau)}_j b^{(\tau)}_j - \left(c^{(\tau)}_j\right)^2 \right)  \right]
\end{align*}
where $g_{\tau}(D)=\sum_{j=1}^{N_v} f_{\tau,j}^2(D)$. Then, we try to solve $\frac{d \ln{p(y_1,...,y_{N_p})}}{dD}=0$,
\begin{align*}
    \Delta t \left( \sum_{\tau=1}^{N_p} \frac{1}{g_{\tau}(D)} \left(\sum_{j=1}^{N_v}  b^{(\tau)}_j c^{(\tau)}_j \right)\right) = D (\Delta t)^2 \left( \sum_{\tau=1}^{N_p} \frac{1}{g_{\tau}(D)} \left(\sum_{j=1}^{N_v} a^{(\tau)}_j b^{(\tau)}_j - \left(c^{(\tau)}_j\right)^2 \right) \right)
\end{align*}
Finally, we get
\begin{equation}
    D = \frac{  \sum_{\tau=1}^{N_p} \frac{1}{g_{\tau}(D)} \left(\sum_{j=1}^{N_v}  b^{(\tau)}_j c^{(\tau)}_j \right)  }{ \Delta t \left( \sum_{\tau=1}^{N_p} \frac{1}{g_{\tau}(D)} \left(\sum_{j=1}^{N_v} a^{(\tau)}_j b^{(\tau)}_j - \left(c^{(\tau)}_j\right)^2 \right) \right) }
\end{equation}
\begin{center}
    \includegraphics[scale=0.6]{ch7/D_EM_first_try.pdf}
\end{center}
\end{definition}

\section{Appendix}
\begin{definition}[Taylor series of $\exp{(x)}$]
\begin{equation}
    \exp{(x)} = \sum_{n=0}^{\infty}\frac{x^n}{n!} = 1+\frac{x}{1!}+\frac{x^2}{2!}+\frac{x^3}{3!}+\cdots
\end{equation}
\end{definition}

\begin{definition}[Eigenvalue and D]
\begin{align*}
    \textbf{H}' \left| \psi'_i \right> &= \lambda'_i \left| \psi'_i \right> \\
    \textbf{H} \left| \psi_i \right> &= \lambda_i \left| \psi_i \right>
\end{align*}
The eigenvectors is fully determined by $p_{\rm{eq}}(x)$, so
\begin{equation}
    \left| \psi'_i \right> = \left| \psi_i \right>
\end{equation}
and the eigenvalue
\begin{equation}
    \lambda_i = D \lambda'_i
\end{equation}
The exception is $\lambda_1=0$
\end{definition}


%!TEX root = ../../dissertation.tex

\section{Solving the recursive Pareto problem}

We compute a competitive equilibrium indirectly as a Pareto optimum,
a standard approach in this literature.
The equilibirum prices then show up as Lagrange multipliers on the resource constraints.
Similar models have been studied by \citet{Colacito2013-yq},
\citet{Kollmann2015-sy}, and \citet{Tretvoll2011-lo}.

\subsection*{Pareto problem}

We solve the Pareto problem recursively with a slight change in notation:
We represent utility of agent 1 by $J$, the value function,
and the utility of agent 2 by $U$, without the ``2'' subscript.
The state at date $t$ is then the exogenous variables $s_t = ( z_{1t}, z_{2t}, v_t ) $
plus the utility promise $U_{t}$ made to agent 2.
The Bellman equation is
\begin{eqnarray*}
    J(U_t, s_t) &=& \max_{\{c_{1t}, U_{t+1} \}}  V \big\{ c_{1t}, \mu_t [J(U_{t+1}, s_{t+1})] \big\} \\
    \mbox{s.t.} && V \big\{ c_{2t}, \mu_t (U_{t+1}) \big\} \;\geq\; U_{t} \\
            && \mbox{plus resource constraints and shocks} .
\end{eqnarray*}
The resource constraints include (\ref{eq:resource-a},\ref{eq:resource-b}) for intermediate goods
and (\ref{eq:armington-1},\ref{eq:armington-2}) for final goods.
The shocks follow (\ref{eq:lom-z},\ref{eq:lom-v}).

We use Lagrange multipliers $\lambda_t$ on promised utility
and $(q_{1t}, q_{2t}, p_{1t}, p_{2t})$ on the resource constraints.
The first-order conditions are then
\begin{eqnarray}
    {c}_{1t}: &&  p_{1t} \;\;=\;\; {J}_t^{1-\rho} (1-\beta) {c}_{1t}^{\rho-1}
            \label{eq:foc-c1} \\
    {c}_{2t}: &&  p_{2t} \;\;=\;\; \lambda_t {U}_t^{1-\rho} (1-\beta) {c}_{2t}^{\rho-1}
            \label{eq:foc-c2} \\
    {a}_{1t}: &&  q_{1t} \;\;=\;\; p_{1t} {c}_{1t}^{1-\sigma} (1-\omega) {a}_{1t}^{\sigma-1}
            \label{eq:foc-a1} \\
    {b}_{1t}: &&  q_{2t} \;\;=\;\; p_{1t} {c}_{1t}^{1-\sigma} \omega {b}_{1t}^{\sigma-1}
            \label{eq:foc-b1} \\
    {a}_{2t}: &&  q_{1t} \;\;=\;\; p_{2t} {c}_{2t}^{1-\sigma} \omega {a}_{2t}^{\sigma-1}
            \label{eq:foc-a2} \\
    {b}_{2t}: &&  q_{2t} \;\;=\;\; p_{2t} {c}_{2t}^{1-\sigma} (1-\omega) {b}_{2t}^{\sigma-1}
            \label{eq:foc-b2} \\
    {U}_{t+1}:&&  {J}_t^{1-\rho} \beta \mu_t ({J}_{t+1})^{\rho-\alpha} {J}_{t+1}^{\alpha-1} {J}_{Ut+1}
            \;\;=\;\; - \lambda_t {U}_t^{1-\rho} \beta \mu_t ({U}_{t+1})^{\rho-\alpha} {U}_{t+1}^{\alpha-1} .
            \label{eq:foc-u}
\end{eqnarray}
Note, in particular, that equation (\ref{eq:foc-u}) applies to promises $U_{t+1}$
in every state at $t+1$.  It's many equations, not just one.

The envelope condition for ${U}_t$ is
\begin{eqnarray*}
    {J}_{Ut} &=& - \lambda_t ,
\end{eqnarray*}
which we use to replace $J_{Ut+1}$ with $\lambda_{t+1}$ in  (\ref{eq:foc-u}).

{\textit Transforming the Pareto weight.\/}
We can simplify the solution by transforming the Pareto weight $\lambda_t$.
A Pareto weight is defined for specific a utility function;
if we transform the utility function, we transform the Pareto weight along with it.
The natural benchmark for the Pareto weight is the additive case, in which it's constant.
Additive preferences are traditionally expressed using utility $U_{t}^* = U_{t}^\rho/\rho$ and $J_{t}^* = J_{t}^\rho/\rho$.
The associated Pareto weight is
\begin{eqnarray*}
    \lambda_t^* &=& \lambda_t {U}_t^{1-\rho}/ {J}_t^{1-\rho} .
\end{eqnarray*}
We refer to $\lambda^*_t$ as the additive Pareto weight ---
or simply the Pareto weight.

With this substitution, we can clearly see the impact of recursive preferences.
The first-order condition (\ref{eq:foc-u}) becomes
\begin{eqnarray}
    \beta \left( \frac{{J}_{t+1}} {\mu_t({J}_{t+1})} \right)^{\alpha-\rho}
%                  \left( \frac{e_{t+1}}{e_t} \right)
                  \lambda^*_{t+1}
            &=&  \lambda^*_t \
            \beta \left( \frac{{U}_{t+1}} {\mu_t({U}_{t+1})} \right)^{\alpha-\rho} .
            \label{eq:foc-u-additive}
\end{eqnarray}
In the additive case, $\alpha=\rho$, this reduces to
$ \lambda^*_{t+1}  = \lambda^*_{t} $ and  the Pareto weight is constant.
This is, of course, well known, but it's nice to know we're on the right track.

%*** Show one-good case reduces to constant weight. ??

With recursive preferences, the Pareto weight need not be constant, although it's an open
question how important this is quantitatively.

{\textit Consumption and the exchange rate.\/}
The same transformation of the Pareto weight changes equations (\ref{eq:foc-c1},\ref{eq:foc-c2}) to
\begin{eqnarray*}
    (1-\beta) c_{1t}^{\rho-1} / p_{1t} &=& \lambda_t^* (1-\beta) c_{2t}^{\rho-1} / p_{2t}
\end{eqnarray*}
or
\begin{eqnarray}
    p_{2t} / p_{1t} &=& \lambda_t^* (c_{2t}/c_{1t})^{\rho-1} .
    \label{eq:cons-rer}
\end{eqnarray}
If the prices of final goods are equal, as they are in a one-good world,
the first equation tells us to equate weighted marginal utilities across agents.

In the second equation, the left side is the real exchange rate $e_t$.
The right side is the product of the Pareto weight
and the consumption ratio.
In the additive case, the Pareto weight is constant and we have a linear relation between
the logs of the real exchange rate and the consumption ratio.
In the data, there is little sign of such a relation.
See, among many others, \citet{Backus1993-ul,Chari2002-ll,Corsetti2008-lx,Colacito2013-yq,Kollmann1995-fe}, and \citet{Tretvoll2011-lo}.
We might say that the data suggests a wedge between the price and consumption ratios
that is represented in the recursive model by the Pareto weight.

If we further simplify the model to have a single good,
then the real exchange rate is one in all states.
In the additive case,
$\lambda_t^*$ is constant
and equation (\ref{eq:cons-rer}) tells us that the ratio of consumptions is also constant.
Any variation in consumption by one agent is exactly mirrored by the other.
This is, of course, counterfactual, and one of the standard anomalies
of international business cycle models.

{\textit Marginal rates of substitution.\/}
The Pareto problem equates marginal rates of substitution,
but since the agents consume different goods this involves the relative price $ e_t = p_{2t} / p_{1t} $.
The ratio of equation (\ref{eq:cons-rer})
at dates $t$ and $t+1$ is
\begin{eqnarray*}
        \beta \left( \frac{c_{1t+1}}{c_{1t}}\right)^{\rho-1}
              \left( \frac{e_{t+1}}{e_t} \right)
              &=&
              \left( \frac{\lambda^*_{t+1}}{\lambda^*_t} \right)
              \beta \left( \frac{c_{2t+1}}{c_{2t}}\right)^{\rho-1} .
\end{eqnarray*}
Combining this with (\ref{eq:foc-u-additive}) gives us
\begin{eqnarray*}
        \beta \left( \frac{c_{1t+1}}{c_{1t}}\right)^{\rho-1}
              \left( \frac{J_{t+1}}{\mu_{t}(J_{t+1})} \right)^{\alpha-\rho}
              \left( \frac{e_{t+1}}{e_t} \right)
              &=&
              \beta \left( \frac{c_{2t+1}}{c_{2t}}\right)^{\rho-1}
              \left( \frac{U_{t+1}}{\mu_{t}(U_{t+1})} \right)^{\alpha-\rho}
\end{eqnarray*}
as noted earlier.
Note the role of  $\alpha - \rho$.
If the difference is zero, the recursive term drops out.
Otherwise the sign of the impact of future utility depends on the sign of $\alpha-\rho$.

%[*** Talk about relation between promises and weights?]  %??


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

\appendix
\section*{Appendix 9 --- Discriminant Optimization Criteria}
\label{appendix-optimization}

Since the goal of the analysis is to validate the production of single
top quarks, any optimization criteria should be based on affirming the
signal+background hypothesis convincingly. We, therefore, define an
optimal search as one that maximizes the probability of the
signal+background hypothesis relative to that of the background
hypothesis. Since we use a Bayesian approach for all our statistical
analyses, a natural way to do this would be to compute the Bayes
factor which is the ratio of the posterior probability densities of
the signal+background hypothesis to the background-only
hypothesis. The description of the method we use to obtain a posterior
probability density can be found in Appendix 13 of
Ref.~\cite{run2-d0-230} and in Ref.~\cite{d0note5123}.

A fundamental principle of Bayesian reasoning is that one should
integrate over all that is unknown.  In designing an analysis, we do
not yet know the event count $n$, nor do we know the background and
signal means $b$ and $s$, respectively. Therefore, in accordance with
the basic Bayesian principle, we must integrate over $n$, $s$, and $b$,
constrained by whatever knowledge we have of them.

We are working to develop the methodology to compute Bayes factor as
discussed above by averaging over all possible value of $n$, $s$ and
$b$~\cite{d0note5260}. For now we have used an approximation to the
Bayes factor by considering only one value of $n$ which is set to the
estimated values of the signal and background yields. The factor is
then computed as illustrated in Fig.~\ref{fig:bayesfactor} using the
values of the posterior probability densities at peak and at zero
signal cross section.

\begin{figure}[!h!tbp]
\includegraphics[width=0.5\textwidth]{figures/bayesfactor.eps}
\vspace{-0.1in}
\begin{minipage}{4in}
\caption[bayesfactor]{A plot of the posterior probability density
versus signal cross section that illustrates the calculation of the
Bayes factor.}
\label{fig:bayesfactor}
\end{minipage}
\end{figure}

We have chosen to use the Bayes ratio to optimize the discriminants'
performance. It may be noted however that this approximate definition
may yield unreasonably high values as the posterior density at zero
signal cross section becomes infinitesimally small. In such a case the
distribution of the posterior probability density is expected to be a
near-perfect Gaussian, and a more meaningful quantity for any
optimization would be a ratio of the signal cross section at the peak
of the posterior to the width of the posterior as shown in
Fig.~\ref{fig:measurement}. This essentially defines how significant a
measurement of the single top cross section would be if the data were
consistent with the signal+background hypothesis.

\begin{figure}[!h!tbp]
\includegraphics[width=0.5\textwidth]{figures/measurement.eps}
\vspace{-0.1in}
\begin{minipage}{4in}
\caption[measurement]{A plot of the posterior probability density
versus signal cross section that illustrates the measurement of the
cross section and its uncertainty.}
\label{fig:measurement}
\end{minipage}
\end{figure}


\chapter{Operations}

Intentio provides three types of oparators:
\begin{itemize}
  \item simple math operators
  \item relational operators
  \item logical operators
\end{itemize}

\begin{example}[Simple math operators]
\begin{lstlisting}[language=intentio]
  1 + 2       # succ 3
  1 - 2       # succ -1
  1 * 2       # succ 2
  1 / 2       # succ 0
  1 / 2.0     # succ 0.5
\end{lstlisting}
\end{example}

\begin{example}[Usage of math operators]
\begin{lstlisting}[language=intentio]
  (2 + 7) * 3 - 8 / 4 # succ 25
\end{lstlisting}
\end{example}

\clearpage
\begin{example}[Relational operators]
\begin{lstlisting}[language=intentio]
  1 < 2       # succ 2
  1 > 2       # fail 2
  1 <= 2      # succ 2
  1 >= 2      # fail 2
  1 == 2      # fail 2
  # (below) authomatic conversion from Int to Float
  1 == 1.0    # succ 1.0
  1 =! 2      # succ 2
  1 === 1     # succ 1
  1 === 1.0   # fail 1.0
  1 ==! 1     # fail 1
\end{lstlisting}
\end{example}

\begin{example}[Usage of relational operators]
\begin{lstlisting}[language=intentio]
  a = 3;
  b = 7;
  1 < a < b < 20 # succ 20
\end{lstlisting}
\end{example}

\begin{example}[Logical operators]
\begin{lstlisting}[language=intentio]
  a = succ "Ala"
  b = fail 8

  a or b            # succ 8
  a or succ none    # succ none
  a and b           # fail 8
  succ none and a   # succ "Ala"
  a xor b           # succ 8
  a xor fail none   # succ none
  not a             # fail "Ala"
\end{lstlisting}
\end{example}


\chapter{Introduction to Triangles}

Connecting any three points with three line segments will get you a
triangle. Here is the triangle $ABC$ created by connecting three points $A$, $B$, and $C$:\index{triangle}

\begin{tikzpicture}[scale=1.5]
  \coordinate [circle, fill, inner sep=2pt] (a) at (0,0) ;
  \coordinate [circle, fill, inner sep=2pt] (b) at (1, 2) ;
  \coordinate [circle, fill, inner sep=2pt] (c) at (3,0) ;
  \draw (a)--(b) node [outer sep=3pt, above]{$B$};
  \draw (b)--(c) node[outer sep=3pt, right]{$C$};
  \draw (c)--(a) node[outer sep=3pt, left]{$A$};
\end{tikzpicture}

\section{Equilateral and Isosceles Triangles}

We talk a lot about the length of the sides of triangles. If all three sides of the triangle are the same length, we say it is an \emph{equilateral triangle}:\index{equilateral triangle}

\begin{tikzpicture}[scale=1.5]
  \coordinate [circle, fill, inner sep=2pt] (a) at (0,0) ;
  \coordinate [circle, fill, inner sep=2pt] (b) at (1.5, 2.6) ;
  \coordinate [circle, fill, inner sep=2pt] (c) at (3,0) ;
  \draw (a)--(b) node [outer sep=3pt, above]{$B$};
  \draw (b)--(c) node[outer sep=3pt, right]{$C$};
  \draw (c)--(a) node[outer sep=3pt, left]{$A$};
  \tkzMarkSegment[pos=.5,mark=||](a,b)
  \tkzMarkSegment[pos=.5,mark=||](b,c)
  \tkzMarkSegment[pos=.5,mark=||](c,a)
\end{tikzpicture}

If only two sides of the triangle are the same length, we say it is an \emph{isosceles triangle}:\index{isoscelese triangle}

\begin{tikzpicture}[scale=1.3]
  \coordinate [circle, fill, inner sep=2pt] (a) at (0,0) ;
  \coordinate [circle, fill, inner sep=2pt] (b) at (1.5, 4) ;
  \coordinate [circle, fill, inner sep=2pt] (c) at (3,0) ;
  \draw (a)--(b) node [outer sep=3pt, above]{$B$};
  \draw (b)--(c) node[outer sep=3pt, right]{$C$};
  \draw (c)--(a) node[outer sep=3pt, left]{$A$};
  \tkzMarkSegment[pos=.5,mark=||](a,b)
  \tkzMarkSegment[pos=.5,mark=||](c,b)
\end{tikzpicture}

The shortest distance between two points is always the straight line
between them. Thus, you can be certain that the length of one side
will \emph{always} be less than the sum of the lengths of the
remaining two sides. This is known as the \emph{triangle inequality}.\index{triangle inequality}

For example, in this diagram $c$ must be less than $a + b$.

\begin{tikzpicture}[scale=1.2]
  \coordinate [circle, fill, inner sep=1pt] (a) at (1.5,0) ;
  \coordinate [circle, fill, inner sep=1pt] (b) at (0,2) ;
  \coordinate [circle, fill, inner sep=1pt] (c) at (3.5,0) ;
  \draw (a)--node[outer sep=3pt, left]{length $b$}(b) ;
  \draw (b)--node[outer sep=3pt, right]{length $c$}(c) ;
  \draw (c)--node[outer sep=3pt, below]{length $a$}(a) ;
\end{tikzpicture}

\section{Interior Angles of a Triangle}

We also talk a lot about the interior angles of a triangle:

\begin{tikzpicture}[scale=2]
  \coordinate [circle, fill, inner sep=2pt] (a) at (0,0) ;
  \coordinate [circle, fill, inner sep=2pt] (b) at (1, 2) ;
  \coordinate [circle, fill, inner sep=2pt] (c) at (3,0) ;
  \draw (a)--(b) node [outer sep=3pt, above]{$B$};
  \draw (b)--(c) node[outer sep=3pt, right]{$C$};
  \draw (c)--(a) node[outer sep=3pt, left]{$A$};
  \pic [draw, <->, "$a$", angle eccentricity=1.5] {angle = c--a--b};
  \pic [draw, <->, "$b$", angle eccentricity=1.5] {angle = a--b--c};
  \pic [draw, <->, "$c$", angle eccentricity=1.5] {angle = b--c--a};
\end{tikzpicture}

A triangle where one of the interior angles is a right angle is said to be a \emph{right triangle}:\index{right triangle}

\begin{tikzpicture}[scale=1.2]
  \coordinate [circle, fill, inner sep=2pt] (a) at (0,0) ;
  \coordinate [circle, fill, inner sep=2pt] (b) at (0,4) ;
  \coordinate [circle, fill, inner sep=2pt] (c) at (3,0) ;
  \draw (a)--(b) node [outer sep=3pt, above]{$B$};
  \draw (b)--(c) node[outer sep=3pt, right]{$C$};
  \draw (c)--(a) node[outer sep=3pt, left]{$A$};
  \pic [draw,angle eccentricity=1.5] {right angle = c--a--b};
\end{tikzpicture}

If a triangle has an obtuse interior angle, it is said to be an \emph{obtuse triangle}:\index{obtuse triange}

\begin{tikzpicture}[scale=1.2]
  \coordinate [circle, fill, inner sep=2pt] (a) at (1.5,0) ;
  \coordinate [circle, fill, inner sep=2pt] (b) at (0,2) ;
  \coordinate [circle, fill, inner sep=2pt] (c) at (3.5,0) ;
  \draw (a)--(b) node [outer sep=3pt, above]{$B$};
  \draw (b)--(c) node[outer sep=3pt, right]{$C$};
  \draw (c)--(a) node[outer sep=3pt, left]{$A$};
  \pic [draw, <->, "obtuse", angle eccentricity=1.7] {angle = c--a--b};
\end{tikzpicture}

If all three interior angles of a triangle are less than $90^\circ$, it is said to be an \emph{acute triangle}.\index{acute triangle}

The measures of the interior angles of a triangle always add up to
$180^\circ$. For example, if you tell me that a triangle has interior
angles of $37^\circ$ and $56^\circ$, I can tell you that the third
interior angle is $87^\circ$.

\begin{Exercise}[title={Missing Angle}, label=missing_angle]
One interior angle of a triange is $92^\circ$. A second angle is $42^\circ$. What is the measure of the third interior angle?
\end{Exercise}
\begin{Answer}[ref=missing_angle]
$180^\circ - (92^\circ + 42^\circ) = 46^\circ$
\end{Answer}

How can you know that the sum of the interior angles is $180^\circ$?
Imagine that you started on the edge of a triangle and walked all the
way around to where you started. (Let's assume you are going
counter-clockwise.) You would turn three times to the left:

\begin{tikzpicture}[scale=1.7]
  \coordinate [circle, fill, inner sep=2pt] (a) at (1.5,0) ;
   \node at (a) [outer sep=3pt, left]{$A$};
  \coordinate [circle, fill, inner sep=2pt] (b) at (0.5,2) ;
  \node at (b) [outer sep=3pt, above]{$B$} ;
  \coordinate [circle, fill, inner sep=2pt] (c) at (3.5,0) ;
    \node at (c)[outer sep=3pt, below]{$C$};

    \coordinate [circle, fill, inner sep=2pt](start) at (2.75,0);
    \node at (start)[outer sep=3pt, below]{Start};

   \coordinate (aa) at (1.75,-0.5) ;
  \coordinate (bb) at (-0.25,2.5) ;
  \coordinate (cc) at (4.2,0) ;
  \draw [->](a)--(cc);
  \draw [->](b)--(aa);
  \draw [->](c)--(bb);
  \pic [draw, ->, "$T_C$", angle eccentricity=1.7] {angle = cc--c--b};
  \pic [draw, ->, "$T_B$", angle eccentricity=2] {angle = bb--b--a};
  \pic [draw, ->, "$T_A$", angle eccentricity=2] {angle = aa--a--c};
\end{tikzpicture}

After these three turns, you would be facing the same direction that
you started in. Thus $T_A + T_B + T_C = 360^\circ$. Let's call the
measure of the interior angles $a$, $b$, and $c$.  Notice that $a$ and
$T_A$ are supplementary. So we know that:
\begin{itemize}
\item $T_A = 180 - a$
\item $T_B = 180 - b$
\item $T_C = 180 - c$
\end{itemize}
So we can rewrite the equation above as
\begin{equation*}
  (180 - a) + (180 - b) + (180 - c) = 360^\circ
\end{equation*}
Which is equivalent to
\begin{equation*}
  a + b + c = 360^\circ
\end{equation*}

\begin{Exercise}[title={Interior Angles of a Quadrilateral}, label=interior_of_quad]
  Any four-sided polygon is a \emph{quadrilateral}.  Using the same
  ``walk around the edge'' logic, what is the sum of the interior
  angles of any quadrilateral?
\end{Exercise}
\begin{Answer}[ref=interior_of_quad]
$360^\circ$
\end{Answer}


\documentclass[11pt, a4paper, twoside]{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage[margin=0.8in]{geometry}
\usepackage{listings}
\usepackage{float}
\usepackage{fancyhdr}
\usepackage{indentfirst}
\usepackage[inline]{enumitem}
\usepackage{tabularx}
\usepackage{xcolor}
\usepackage{array}
\usepackage{minted}
\usemintedstyle{tango}
\usepackage[belowskip=0pt,aboveskip=0pt,font=small,labelfont=small]{caption}
\captionsetup{width=\linewidth}
\setlength\intextsep{0pt}
\graphicspath{{Plots/}}
\setlist[itemize]{noitemsep, topsep=0pt}
\fancyhead[RO,LE]{EE2703: Assignment 8}
\fancyhead[LO,RE]{Akilesh Kannan}
\cfoot{\thepage}

\title{EE2703: Assignment 8}
\author{Akilesh Kannan (EE18B122)}
\date{\today}

\pagestyle{fancy}

\begin{document}
\maketitle

\section{Introduction}
In this assignment, we shall look at DFTs, using the \texttt{numpy.fft} toolbox.

\section{The Accuracy of the \texttt{numpy.fft} Package}
    To check how powerful and accurate the package is, we shall take the DFT of a sequence of random numbers, then take it's IDFT and compare the two, as to how close they are. This is done using the commands \mintinline{python}{np.fft.fft} and \mintinline{python}{np.fft.ifft}. We also have to use \mintinline{python}{np.fft.fftshift} to shift the $[\pi, 2\pi]$ portion of the DFT to the left as it represents negative frequency, i.e., $[-\pi, 0]$.

    The following piece of code accomplishes this task:
    \inputminted[breaklines, linenos]{python}{Code/1.py}

    The error came out to be of the order of $10^{-15}$. When the two sequences \mintinline{python}{xOriginal} and \mintinline{python}{xComputed} are plotted together, this is the result:

    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.53]{Fig0.png}
        \caption{Comparison of the true and computed values of the random sequence}
        \label{fig:fftRandom}
    \end{figure}

    The two sequences overlap and cannot be differentiated !! This shows that the \texttt{numpy.fft} package is very accurate.

\section{Spectrum of $sin(5t)$}
    We calculate the DFT of $f(t)$ by the method mentioned in the above section. Then, we plot the phase and magnitude of the DFT and the following is obtained for $sin(5t)$:
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.53]{Fig1.png}
        \caption{Spectrum of $sin(5t)$}
        \label{fig:sin5t}
    \end{figure}

    This is expected, because:
    \begin{equation}
        sin(5t) = \frac{1}{2j}(e^{5jt}-e^{-5jt})
    \end{equation}

    So, the frequencies present in the DFT of $sin(5t)$ are $\omega = \pm5\ rad/sec$, and the phase associated with them is $\phi = \pm \frac{\pi}{2}\ rad/sec$ respectively. This is exactly what is shown in the above plot.

\section{Amplitude Modulation with $(1+0.1cos(t))cos(10t)$}
    We have,
    \begin{equation}
        (1+0.1cos(t))cos(10t) = \frac{1}{2}(e^{10jt}+e^{-10jt}) + 0.1\cdot\frac{1}{2}\cdot\frac{1}{2}(e^{11jt} + e^{-11jt} + e^{9jt} + e^{-9jt})
        \label{eqModAmp}
    \end{equation}

    Writing $(1+0.1cos(t))cos(10t)$ in a different form as shown in \eqref{eqModAmp}, we observe that the frequencies present in the signal are $\omega = \pm 10\ rad/sec$, $\omega = \pm 11\ rad/sec$ and $\omega = \pm 9\ rad/sec$. Thus we expect the DFT also to have non-zero magnitudes only at these frequencies.

    Plotting the DFT using the \texttt{numpy.fft} package, we get:
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.55]{Fig2.png}
        \caption{DFT of $(1+0.1cos(t))cos(10t)$}
        \label{fig:ampMod}
    \end{figure}

    Figure \eqref{fig:ampMod} is in line with the theory we have discussed above.

\section{Spectra of $sin^3(t)$ and $cos^3(t)$}
DFT Spectrum of $sin^3(t)$:
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.5]{Fig3.png}
    \caption{Spectrum of $sin^3(t)$}
    \label{fig:sin^3t}
\end{figure}

DFT Spectrum of $cos^3(t)$:
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.5]{Fig4.png}
    \caption{Spectrum of $cos^3(t)$}
    \label{fig:cos^3t}
\end{figure}

The above 2 figures are expected because:
\begin{gather}
    sin^3(t) = \frac{3}{4}sin(t) - \frac{1}{4}sin(3t)\\
    cos^3(t) = \frac{3}{4}cos(t) + \frac{1}{4}cos(3t)
\end{gather}

So, we expect peaks $\omega = \pm 1\ rad/sec$ and $\omega = \pm 3\ rad/sec$.

\section{Frequency Modulation with $cos(20t + 5cos(t))$}
The DFT of $cos(20t + 5cos(t))$ can be seen below:
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.41]{Fig5.png}
    \caption{DFT of $cos(20t + 5cos(t))$}
    \label{fig:freqMod}
\end{figure}

When we compare this result with that of the Amplitude Modulation as seen in Fig \eqref{fig:ampMod}, we see that there are more side bands, and some of them have even higher energy than $\omega = \pm 20 \ rad/sec$.\footnote{J. M. Chowning, \textit{The synthesis of complex audio spectra by means of frequency modulation}, Journal of the Audio Engineering Society, Vol. 21, No. 7, pp. 526-534, \textit{1973}}

\section{DFT of a Gaussian}
The DFT of a gaussian is also a gaussian, as shown below:
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.5]{Fig6.png}
    \caption{Gaussian Spectrum}
    \label{fig:gauss}
\end{figure}

\section{Conclusion}
We have thus found out the DFT's of various signals using the \texttt{numpy.fft} package. We need to use the \mintinline{python}{numpy.fft.fftshift()} and \mintinline{python}{numpy.fft.ifftshift()} methods to fix distortions in the phase response.
\end{document}


\documentclass{article}
\author{Lin Jiaying}
\title{Chap.4 Exercise}
\usepackage{algorithm}
\usepackage{xcolor}
\usepackage{graphicx}

\usepackage{color}
\usepackage{alltt}
\usepackage[T1]{fontenc}
\usepackage{marvosym}

% highlight theme: vim bclear
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.2,0.2,0.2}{#1}}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.06,0.58,0.63}{#1}}
\newcommand{\hlesc}[1]{\textcolor[rgb]{0.86,0.41,0.09}{#1}}
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.06,0.58,0.63}{#1}}
\newcommand{\hlpps}[1]{\textcolor[rgb]{0.06,0.58,0.63}{#1}}
\newcommand{\hlslc}[1]{\textcolor[rgb]{0.59,0.59,0.59}{#1}}
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.59,0.59,0.59}{#1}}
\newcommand{\hlppc}[1]{\textcolor[rgb]{0.41,0.78,0.23}{#1}}
\newcommand{\hlopt}[1]{\textcolor[rgb]{0.2,0.2,0.2}{#1}}
\newcommand{\hlipl}[1]{\textcolor[rgb]{0.45,0.46,0.77}{#1}}
\newcommand{\hllin}[1]{\textcolor[rgb]{0.59,0.59,0.59}{#1}}
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.23,0.42,0.78}{#1}}
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.63,0,0.31}{#1}}
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0,0.63,0.31}{#1}}
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.78,0.23,0.41}{#1}}
\definecolor{bgcolor}{rgb}{1,1,1}

\usepackage{algorithmic}
%listings
\usepackage{listings}

\usepackage{amsmath}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}

\begin{document}
	\maketitle
\section{Exercise 4.5}
\textbf{Implement the rank finding algorithm based upon the divide-and-conquer approach. Compare it with the straightforward approach.}

\section{Solution: }

	The divide-and-conquer method to solve this problem is similar to the maxima finding problem showed in PPT.

	\paragraph{Step 1:}
	If the set S contains only one points, return this point rank as 0.\\
	Else, split the points along the y-axis line L in median x-axis position into Set L and Set R. The x positions of the points in $S_L$ is all smaller than the median in x, and the points in $S_R$ is on contrary.
	\paragraph{Step 2:}
	Recursively find the ranks of the points in $S_L$ and $S_R$.
	\paragraph{Step 3:}
	Sort the points in $S_L$ and $S_R$ by their y-value increasingly, and for each points in $S_R$, find their rank by plusing the number of points in $S_L$ whose y-value are smaller than the point.\\

	The psesudocode of the divide-and-conquer approach to solve rank finding algorithm as follow:\\

	\newpage

	\begin{algorithm}
		\caption{findRank(S, L, R)}
	\begin{algorithmic}[1]
		\REQUIRE S[n]: A set S of n planar points.\\
				$L: 0$\\
				$R: n$\\
		\ENSURE rank[n]: The rank of all points in S.\\

		\IF {$length(S) \gets 1$}
		\RETURN $rank(S[0]) \gets 0$
		\ENDIF
		\STATE $mid \gets (L + R)/ 2$\\
		\STATE Split($S$) $\gets$ $S_L \bigcup S_R$, which\\
			$S_L \gets $all points x-value $\leq$ mid,\\
			$S_R \gets $all points x-value $>$ mid\\
		\STATE findRank(S, L, mid)\\
		\STATE findRank(S, mid + 1, R)\\

		\STATE $S_L$ $\gets$ sort($S_L$, 'increasing', 'xValue')\\
		\STATE $S_R$ $\gets$ ($S_R$, 'increasing', 'xValue')\\
		\FOR{one point A to all points in $S_R$}
			\STATE $rank(A) \gets rank(A) + length$(points in $S_L$ have less y-value than A)\\
		\ENDFOR
	\end{algorithmic}
	\end{algorithm}

	The time complexity analysis of the divide-and-conquer method to find these ranks is as follows:\\

	$$ T(n)=\left\{
	\begin{aligned}
		&2T(n/2) + c_{1}nlogn + c_{2}n, n > 1\\
		&1, n = 1
	\end{aligned}
	\right.
	$$
	Let $n = 2^k$. It's easy to prove that\\
	\begin{align*}
	T(n) &= O(n log n) + O(n log^{2} n)\\
		&= O(n log^{2} n)
	\end{align*}
	, while the straightforward method needs $O(n^2)$.\\
	The time complexity comparsion of two methods is shown in the chart as follows.\\
	\begin{figure}
	\centering
	\caption{The time complexity comparsion of two methods.}
	\includegraphics[scale = .8]{time.png}
	\end{figure}

	\newpage
	The source code of the divide-and-conquer method in C++ as follows:\\

	\begin{lstlisting}[language={C++},breaklines=true]
	void divide_finding(Point *points, int N, int l, int r)
	{
	    if(l == r) return;
	    int mid = (r + l) / 2;

	    divide_finding(points, N, l, mid);
	    divide_finding(points, N, mid + 1, r);

	    sort(points + l, points + mid + 1, comp_y);
	    //sort(points + mid, points + r, comp_y);
	    sort(points + mid + 1, points + r, comp_y);

	    int scan_a = l;
	    //int scan_b = mid;
	    int scan_b = mid + 1;

	    while(scan_a <= mid && scan_b <= r)
	    {
	        if(points[scan_a].y < points[scan_b].y && points[scan_a].x < points[scan_b].x)
	        {
	            while(scan_a++ < mid)
	            {
	                if(points[scan_a].y > points[scan_b].y) break;
	            }
	            points[scan_b].rank += scan_a - l;
	            scan_a = l; //*
	            scan_b++;

	        } else {
	            scan_b++;
	        }

	    }
	    while(scan_b <= r)
	    {
	        scan_b++;
	    }
	    sort(points + l, points + r + 1, comp_x);
	}

	\end{lstlisting}
	\newpage
	The source code of the straightforward method in C++ as follows.It's easy to notice that this method time complexity is $O(n^2)$:\\
	\begin{lstlisting}[language={C++},breaklines=true]
	void straight_finding(Point *points, int N)
	{
	    for(int i = 0; i < N; ++i)
	    {
	        for(int j = 0; j < N; ++j)
	        {
	            if(points[i].x > points[j].x && points[i].y > points[j].y)
	            {
	                points[i].rank++;
	            }
	        }
	    }
	}
	\end{lstlisting}

	\section{Appendix:}
	The full version of source code in C++ is as follows:
	\newcommand{\dq}{"}

	\pagecolor{bgcolor}
	\newsavebox{\hlboxopenbrace}
	\newsavebox{\hlboxclosebrace}
	\newsavebox{\hlboxlessthan}
	\newsavebox{\hlboxgreaterthan}
	\newsavebox{\hlboxdollar}
	\newsavebox{\hlboxunderscore}
	\newsavebox{\hlboxand}
	\newsavebox{\hlboxhash}
	\newsavebox{\hlboxat}
	\newsavebox{\hlboxbackslash}
	\newsavebox{\hlboxpercent}
	\newsavebox{\hlboxhat}
	\setbox\hlboxopenbrace=\hbox{\verb.{.}
	\setbox\hlboxclosebrace=\hbox{\verb.}.}
	\setbox\hlboxlessthan=\hbox{\verb.<.}
	\setbox\hlboxgreaterthan=\hbox{\verb.>.}
	\setbox\hlboxdollar=\hbox{\verb.$.}
	\setbox\hlboxunderscore=\hbox{\verb._.}
	\setbox\hlboxand=\hbox{\verb.&.}
	\setbox\hlboxhash=\hbox{\verb.#.}
	\setbox\hlboxat=\hbox{\verb.@.}
	\setbox\hlboxbackslash=\hbox{\verb.\.}
	\setbox\hlboxpercent=\hbox{\verb.\%.}
	\setbox\hlboxhat=\hbox{\verb.^.}
	\def\urltilda{\kern -.15em\lower .7ex\hbox{\~{}}\kern .04em}
	\noindent
	\ttfamily
	%\shorthandoff{"}
	\hlstd{}\hllin{\ 1\ }\hlstd{}\hlppc{\#include\ \usebox{\hlboxlessthan}iostream\usebox{\hlboxgreaterthan}}\\
	\hllin{\ 2\ }\hlstd{}\hlppc{\#include\ \usebox{\hlboxlessthan}algorithm\usebox{\hlboxgreaterthan}}\\
	\hllin{\ 3\ }\hlstd{}\\
	\hllin{\ 4\ }\hlstd{}\hlppc{\#define\ MAX\textunderscore N\ 100}\\
	\hllin{\ 5\ }\hlstd{}\\
	\hllin{\ 6\ }\hlstd{}\hlkwa{using\ namespace\ }\hlstd{std}\hlopt{;}\\
	\hllin{\ 7\ }\hlstd{}\\
	\hllin{\ 8\ }\hlstd{}\hlkwb{struct\ }\hlstd{Point}\hlopt{\usebox{\hlboxopenbrace}}\\
	\hllin{\ 9\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{int\ }\hlstd{x}\hlopt{;}\\
	\hllin{10\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{int\ }\hlstd{y}\hlopt{;}\\
	\hllin{11\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{int\ }\hlstd{rank}\hlopt{;}\\
	\hllin{12\ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\hlstd{points}\hlopt{{[}}\hlstd{MAX\textunderscore N}\hlopt{{]};}\\
	\hllin{13\ }\hlstd{}\\
	\hllin{14\ }\hlstd{}\hlkwb{bool\ }\hlstd{}\hlkwd{comp\textunderscore x}\hlstd{}\hlopt{(}\hlstd{}\hlkwb{const\ }\hlstd{Point}\hlopt{\&}\hlstd{a}\hlopt{,\ }\hlstd{}\hlkwb{const\ }\hlstd{Point\ }\hlopt{\&}\hlstd{b}\hlopt{)\usebox{\hlboxopenbrace}}\\
	\hllin{15\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{return\ }\hlstd{a}\hlopt{.}\hlstd{x\ }\hlopt{\usebox{\hlboxlessthan}\ }\hlstd{b}\hlopt{.}\hlstd{x}\hlopt{;}\\
	\hllin{16\ }\hlstd{}\\
	\hllin{17\ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\\
	\hllin{18\ }\hlstd{}\\
	\hllin{19\ }\hlstd{}\hlkwb{bool\ }\hlstd{}\hlkwd{comp\textunderscore y}\hlstd{}\hlopt{(}\hlstd{}\hlkwb{const\ }\hlstd{Point\ }\hlopt{\&}\hlstd{a}\hlopt{,\ }\hlstd{}\hlkwb{const\ }\hlstd{Point\ }\hlopt{\&}\hlstd{b}\hlopt{)\usebox{\hlboxopenbrace}}\\
	\hllin{20\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{return\ }\hlstd{a}\hlopt{.}\hlstd{y\ }\hlopt{\usebox{\hlboxlessthan}\ }\hlstd{b}\hlopt{.}\hlstd{y}\hlopt{;}\\
	\hllin{21\ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\\
	\hllin{22\ }\hlstd{}\\
	\hllin{23\ }\hlstd{}\hlkwb{void\ }\hlstd{}\hlkwd{straight\textunderscore finding}\hlstd{}\hlopt{(}\hlstd{Point\ }\hlopt{{*}}\hlstd{points}\hlopt{,\ }\hlstd{}\hlkwb{int\ }\hlstd{N}\hlopt{)\usebox{\hlboxopenbrace}}\\
	\hllin{24\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{for}\hlstd{}\hlopt{(}\hlstd{}\hlkwb{int\ }\hlstd{i\ }\hlopt{=\ }\hlstd{}\hlnum{0}\hlstd{}\hlopt{;\ }\hlstd{i\ }\hlopt{\usebox{\hlboxlessthan}\ }\hlstd{N}\hlopt{;\ ++}\hlstd{i}\hlopt{)\usebox{\hlboxopenbrace}}\\
	\hllin{25\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{for}\hlstd{}\hlopt{(}\hlstd{}\hlkwb{int\ }\hlstd{j\ }\hlopt{=\ }\hlstd{}\hlnum{0}\hlstd{}\hlopt{;\ }\hlstd{j\ }\hlopt{\usebox{\hlboxlessthan}\ }\hlstd{N}\hlopt{;\ ++}\hlstd{j}\hlopt{)\usebox{\hlboxopenbrace}}\\
	\hllin{26\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{if}\hlstd{}\hlopt{(}\hlstd{points}\hlopt{{[}}\hlstd{i}\hlopt{{]}.}\hlstd{x\ }\hlopt{\usebox{\hlboxgreaterthan}\ }\hlstd{points}\hlopt{{[}}\hlstd{j}\hlopt{{]}.}\hlstd{x\ }\hlopt{\&\&\ }\hlstd{points}\hlopt{{[}}\hlstd{i}\hlopt{{]}.}\hlstd{y\ }\hlopt{\usebox{\hlboxgreaterthan}\ }\\
	\hllin{\ \ \ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{points}\hlopt{{[}}\hlstd{j}\hlopt{{]}.}\hlstd{y}\hlopt{)\usebox{\hlboxopenbrace}}\Righttorque\\
	\hllin{27\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{points}\hlopt{{[}}\hlstd{i}\hlopt{{]}.}\hlstd{rank}\hlopt{++;}\\
	\hllin{28\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\\
	\hllin{29\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\\
	\hllin{30\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\\
	\hllin{31\ }\hlstd{}\\
	\hllin{32\ }\hlstd{}\\
	\hllin{33\ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\\
	\hllin{34\ }\hlstd{}\\
	\hllin{35\ }\hlstd{}\hlkwb{void\ }\hlstd{}\hlkwd{divide\textunderscore finding}\hlstd{}\hlopt{(}\hlstd{Point\ }\hlopt{{*}}\hlstd{points}\hlopt{,\ }\hlstd{}\hlkwb{int\ }\hlstd{N}\hlopt{,\ }\hlstd{}\hlkwb{int\ }\hlstd{l}\hlopt{,\ }\hlstd{}\hlkwb{int\ }\hlstd{r}\hlopt{)\usebox{\hlboxopenbrace}}\\
	\hllin{36\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{if}\hlstd{}\hlopt{(}\hlstd{l\ }\hlopt{==\ }\hlstd{r}\hlopt{)\ }\hlstd{}\hlkwa{return}\hlstd{}\hlopt{;}\\
	\hllin{37\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{int\ }\hlstd{mid\ }\hlopt{=\ (}\hlstd{r\ }\hlopt{+\ }\hlstd{l}\hlopt{)\ /\ }\hlstd{}\hlnum{2}\hlstd{}\hlopt{;}\\
	\hllin{38\ }\hlstd{}\\
	\hllin{39\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwd{divide\textunderscore finding}\hlstd{}\hlopt{(}\hlstd{points}\hlopt{,\ }\hlstd{N}\hlopt{,\ }\hlstd{l}\hlopt{,\ }\hlstd{mid}\hlopt{);}\\
	\hllin{40\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwd{divide\textunderscore finding}\hlstd{}\hlopt{(}\hlstd{points}\hlopt{,\ }\hlstd{N}\hlopt{,\ }\hlstd{mid\ }\hlopt{+\ }\hlstd{}\hlnum{1}\hlstd{}\hlopt{,\ }\hlstd{r}\hlopt{);}\\
	\hllin{41\ }\hlstd{}\\
	\hllin{42\ }\hlstd{}\\
	\hllin{43\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwd{sort}\hlstd{}\hlopt{(}\hlstd{points\ }\hlopt{+\ }\hlstd{l}\hlopt{,\ }\hlstd{points\ }\hlopt{+\ }\hlstd{mid\ }\hlopt{+\ }\hlstd{}\hlnum{1}\hlstd{}\hlopt{,\ }\hlstd{comp\textunderscore y}\hlopt{);}\\
	\hllin{44\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlslc{//sort(points\ +\ mid,\ points\ +\ r,\ comp\textunderscore y);}\\
	\hllin{45\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwd{sort}\hlstd{}\hlopt{(}\hlstd{points\ }\hlopt{+\ }\hlstd{mid\ }\hlopt{+\ }\hlstd{}\hlnum{1}\hlstd{}\hlopt{,\ }\hlstd{points\ }\hlopt{+\ }\hlstd{r}\hlopt{,\ }\hlstd{comp\textunderscore y}\hlopt{);}\\
	\hllin{46\ }\hlstd{}\\
	\hllin{47\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{int\ }\hlstd{scan\textunderscore a\ }\hlopt{=\ }\hlstd{l}\hlopt{;}\\
	\hllin{48\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlslc{//int\ scan\textunderscore b\ =\ mid;}\\
	\hllin{49\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{int\ }\hlstd{scan\textunderscore b\ }\hlopt{=\ }\hlstd{mid\ }\hlopt{+\ }\hlstd{}\hlnum{1}\hlstd{}\hlopt{;}\\
	\hllin{50\ }\hlstd{}\\
	\hllin{51\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{while}\hlstd{}\hlopt{(}\hlstd{scan\textunderscore a\ }\hlopt{\usebox{\hlboxlessthan}=\ }\hlstd{mid\ }\hlopt{\&\&\ }\hlstd{scan\textunderscore b\ }\hlopt{\usebox{\hlboxlessthan}=\ }\hlstd{r}\hlopt{)\usebox{\hlboxopenbrace}}\\
	\hllin{52\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{if}\hlstd{}\hlopt{(}\hlstd{points}\hlopt{{[}}\hlstd{scan\textunderscore a}\hlopt{{]}.}\hlstd{y\ }\hlopt{\usebox{\hlboxlessthan}\ }\hlstd{points}\hlopt{{[}}\hlstd{scan\textunderscore b}\hlopt{{]}.}\hlstd{y\ }\hlopt{\&\&\ }\hlstd{points}\hlopt{{[}}\\
	\hllin{\ \ \ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ }\hlstd{scan\textunderscore a}\hlopt{{]}.}\hlstd{x\ }\hlopt{\usebox{\hlboxlessthan}\ }\hlstd{points}\hlopt{{[}}\hlstd{scan\textunderscore b}\hlopt{{]}.}\hlstd{x}\hlopt{)\usebox{\hlboxopenbrace}}\\
	\hllin{53\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{while}\hlstd{}\hlopt{(}\hlstd{scan\textunderscore a}\hlopt{++\ \usebox{\hlboxlessthan}\ }\hlstd{mid}\hlopt{)\usebox{\hlboxopenbrace}}\Righttorque\\
	\hllin{54\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{if}\hlstd{}\hlopt{(}\hlstd{points}\hlopt{{[}}\hlstd{scan\textunderscore a}\hlopt{{]}.}\hlstd{y\ }\hlopt{\usebox{\hlboxgreaterthan}\ }\hlstd{points}\hlopt{{[}}\hlstd{scan\textunderscore b}\hlopt{{]}.}\hlstd{y}\hlopt{)\ }\Righttorque\\
	\hllin{\ \ \ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{break}\hlstd{}\hlopt{;}\\
	\hllin{55\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\\
	\hllin{56\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{points}\hlopt{{[}}\hlstd{scan\textunderscore b}\hlopt{{]}.}\hlstd{rank\ }\hlopt{+=\ }\hlstd{scan\textunderscore a\ }\hlopt{{-}\ }\hlstd{l}\hlopt{;}\Righttorque\\
	\hllin{57\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{scan\textunderscore a\ }\hlopt{=\ }\hlstd{l}\hlopt{;\ }\hlstd{}\hlslc{//{*}}\\
	\hllin{58\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{scan\textunderscore b}\hlopt{++;}\\
	\hllin{59\ }\hlstd{}\\
	\hllin{60\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\hlstd{}\hlkwa{else}\hlstd{}\hlopt{\usebox{\hlboxopenbrace}}\\
	\hllin{61\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlslc{//scan\textunderscore a++;}\\
	\hllin{62\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{scan\textunderscore b}\hlopt{++;}\\
	\hllin{63\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\\
	\hllin{64\ }\hlstd{}\\
	\hllin{65\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\\
	\hllin{66\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{while}\hlstd{}\hlopt{(}\hlstd{scan\textunderscore b\ }\hlopt{\usebox{\hlboxlessthan}=\ }\hlstd{r}\hlopt{)\ \usebox{\hlboxopenbrace}}\\
	\hllin{67\ }\hlstd{}\\
	\hllin{68\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlslc{//points{[}scan\textunderscore b{]}.rank\ +=\ scan\textunderscore a\ {-}\ l;}\\
	\hllin{69\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{scan\textunderscore b}\hlopt{++;}\\
	\hllin{70\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\\
	\hllin{71\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwd{sort}\hlstd{}\hlopt{(}\hlstd{points\ }\hlopt{+\ }\hlstd{l}\hlopt{,\ }\hlstd{points\ }\hlopt{+\ }\hlstd{r\ }\hlopt{+\ }\hlstd{}\hlnum{1}\hlstd{}\hlopt{,\ }\hlstd{comp\textunderscore x}\hlopt{);}\\
	\hllin{72\ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\\
	\hllin{73\ }\hlstd{}\\
	\hllin{74\ }\hlstd{}\hlkwb{int\ }\hlstd{}\hlkwd{main}\hlstd{}\hlopt{()\ \usebox{\hlboxopenbrace}}\\
	\hllin{75\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{int\ }\hlstd{N}\hlopt{;}\\
	\hllin{76\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{while}\hlstd{}\hlopt{(}\hlstd{cin\ }\hlopt{\usebox{\hlboxgreaterthan}\usebox{\hlboxgreaterthan}\ }\hlstd{N}\hlopt{)\usebox{\hlboxopenbrace}}\\
	\hllin{77\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{for}\hlstd{}\hlopt{(}\hlstd{}\hlkwb{int\ }\hlstd{i\ }\hlopt{=\ }\hlstd{}\hlnum{0}\hlstd{}\hlopt{;\ }\hlstd{i\ }\hlopt{\usebox{\hlboxlessthan}\ }\hlstd{N}\hlopt{;\ ++}\hlstd{i}\hlopt{)\usebox{\hlboxopenbrace}}\\
	\hllin{78\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{cin\ }\hlopt{\usebox{\hlboxgreaterthan}\usebox{\hlboxgreaterthan}\ }\hlstd{points}\hlopt{{[}}\hlstd{i}\hlopt{{]}.}\hlstd{x\ }\hlopt{\usebox{\hlboxgreaterthan}\usebox{\hlboxgreaterthan}\ }\hlstd{points}\hlopt{{[}}\hlstd{i}\hlopt{{]}.}\hlstd{y}\hlopt{;}\\
	\hllin{79\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\\
	\hllin{80\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwd{sort}\hlstd{}\hlopt{(}\hlstd{points}\hlopt{,\ }\hlstd{points\ }\hlopt{+\ }\hlstd{N}\hlopt{,\ }\hlstd{comp\textunderscore x}\hlopt{);}\\
	\hllin{81\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwd{sort}\hlstd{}\hlopt{(}\hlstd{points\ }\hlopt{+\ }\hlstd{}\hlnum{0}\hlstd{}\hlopt{,\ }\hlstd{points\ }\hlopt{+\ }\hlstd{}\hlnum{1}\hlstd{}\hlopt{,\ }\hlstd{comp\textunderscore y}\hlopt{);}\\
	\hllin{82\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwd{divide\textunderscore finding}\hlstd{}\hlopt{(}\hlstd{points}\hlopt{,\ }\hlstd{N}\hlopt{,\ }\hlstd{}\hlnum{0}\hlstd{}\hlopt{,\ }\hlstd{N\ }\hlopt{{-}\ }\hlstd{}\hlnum{1}\hlstd{}\hlopt{);}\\
	\hllin{83\ }\hlstd{}\\
	\hllin{84\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{for}\hlstd{}\hlopt{(}\hlstd{}\hlkwb{int\ }\hlstd{i\ }\hlopt{=\ }\hlstd{}\hlnum{0}\hlstd{}\hlopt{;\ }\hlstd{i\ }\hlopt{\usebox{\hlboxlessthan}\ }\hlstd{N}\hlopt{;\ }\hlstd{i}\hlopt{++)\usebox{\hlboxopenbrace}}\\
	\hllin{85\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{cout\ }\hlopt{\usebox{\hlboxlessthan}\usebox{\hlboxlessthan}\ }\hlstd{points}\hlopt{{[}}\hlstd{i}\hlopt{{]}.}\hlstd{x\ }\hlopt{\usebox{\hlboxlessthan}\usebox{\hlboxlessthan}}\hlstd{}\hlstr{\dq{}\ \dq{}}\hlstd{\ }\hlopt{\usebox{\hlboxlessthan}\usebox{\hlboxlessthan}}\hlstd{\ \ }\hlopt{}\hlstd{points}\hlopt{{[}}\hlstd{i}\hlopt{{]}.}\hlstd{y}\hlstd{\ \ }\hlstd{}\hlopt{\usebox{\hlboxlessthan}\usebox{\hlboxlessthan}\ }\hlstd{}\hlstr{\dq{}\ }\\
	\hllin{\ \ \ }\hlstr{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstr{\dq{}}\hlstd{\ }\hlopt{\usebox{\hlboxlessthan}\usebox{\hlboxlessthan}\ }\hlstd{points}\hlopt{{[}}\hlstd{i}\hlopt{{]}.}\hlstd{rank\ }\hlopt{\usebox{\hlboxlessthan}\usebox{\hlboxlessthan}\ }\hlstd{endl}\hlopt{;}\\
	\hllin{86\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\\
	\hllin{87\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\\
	\hllin{88\ }\hlstd{}\Righttorque\\
	\hllin{89\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{return\ }\hlstd{}\hlnum{0}\hlstd{}\hlopt{;}\\
	\hllin{90\ }\hlstd{}\hlopt{\usebox{\hlboxclosebrace}}\hlstd{}\\
	\mbox{}
	\normalfont
	\normalsize
	%\shorthandon{"}

	\newpage
	Result:\\
	\begin{figure}
		\centering
		\includegraphics[scale = 1.5]{result.png}
	\end{figure}

\end{document}