\documentclass{article}

\usepackage{lipsum}
\usepackage[margin=1.2in]{geometry}
\usepackage{titlesec}
\usepackage{graphicx}
\usepackage{amsmath}

\newcommand{\code}{\texttt}
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}

\usepackage{siunitx} % Required for alignment

\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

% Specify images directory
\graphicspath{ {./report-images/} }

% Header and Footer stuff
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\fancyfoot[R]{ \thepage\ }
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\newcommand{\sectionbreak}{\clearpage}
\setlength{\parindent}{0pt}

%

\begin{document}

%----------------------------------------------------------------------------------------
%	TITLE PAGE
%----------------------------------------------------------------------------------------

\begin{titlepage} % Suppresses displaying the page number on the title page and the subsequent page counts as page 1
	\newcommand{\HRule}{\rule{\linewidth}{0.5mm}}% Defines a new command for horizontal lines, change thickness here

	\center % Centre everything on the page

	%------------------------------------------------
	%	Headings
	%------------------------------------------------

	\textsc{\Large Systems of Linear Equations}\\[0.5cm] % Major heading such as course name

	\textsc{\large Exercise 7}\\[0.5cm] % Minor heading such as course title

	%------------------------------------------------
	%	Title
	%------------------------------------------------

	\HRule\\[0.6cm]

	{\huge\bfseries Solving a Linear System with LU Decomposition}\\[0.25cm] % Title of your document

	\HRule\\[1.5cm]

	%------------------------------------------------
	%	Author(s)
	%------------------------------------------------

	\begin{minipage}{0.4\textwidth}
		\begin{flushleft}
			\large
			\textit{Author}\\
			\textsc{Cesare De Cal} % Your name
		\end{flushleft}
	\end{minipage}
	~
	\begin{minipage}{0.4\textwidth}
		\begin{flushright}
			\large
			\textit{Professor}\\
			\textsc{Annie Cuyt}\\ % Supervisor's name
			[0.25cm]
			\textit{Assistant Professor}\\
			\textsc{Ferre Knaepkens} % Supervisor's name

		\end{flushright}
	\end{minipage}

	\vfill\vfill\vfill

	{\large\today}

	\vfill

\end{titlepage}

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

\section{Introduction}\label{sec:intro}
This exercise asks to build a tridiagonal matrix with the value $-1$ on the adjacent upper diagonal, the value $+1$ on the adjacent lower diagonal, and the value $b_{i}$ on the main diagonal, with $i = 1, \ldots, n$ given by
$$b_i =\frac{2(i+1)}{3},\quad i + 1= 3, 6, 9,\ldots$$
$$b_i =1,\quad i + 1 = 2, 4, 5, 7, 8, \ldots$$

This matrix should then be used as the coefficients matrix in the $A\vec{x}=\vec{y}$ linear system. The exercise asks to solve the system using \code{GEPP} (Gaussian Elimination with Partial Pivoting) and then give $x_1$, an approximation of $e-2$.\\

As we've seen in class, there are multiple ways of solving a linear system. For example, I could compute the inverse of $A$ and find $\vec{x}=A^{-1}\vec{y}$. We've seen that this approach, however, requires more computations than necessary and returns a less accurate result. Therefore in this exercise I am going to use solve a linear system using LU decomposition which is numerically stable. I'll also calculate the condition number and the error to verify if this is an ill-conditioned system and to verify how precise the computed solution is.

\section{Tools}
The following programming language and libraries have been used in this exercise:
\begin{itemize}
  \item C
  \item C Math Library
  \item GSL (GNU Scientific Library)
  \item Python 3 (for plotting)
  \item NumPy (for plotting)
\end{itemize}
The following double-precision GSL data types have been used in the exercise:
\begin{itemize}
  \item \code{gsl\_vector}
  \item \code{gsl\_matrix}
  \item \code{gsl\_permutation}
\end{itemize}
The following GSL methods have been used in the exercise:
\begin{itemize}
  \item \code{gsl\_matrix\_alloc(size1, size2)}
  \item \code{gsl\_matrix\_set\_zero(matrix)}
  \item \code{gsl\_matrix\_set(matrix, row, column, value)}
  \item \code{gsl\_matrix\_get(matrix, row, column)}
  \item \code{gsl\_vector\_alloc(size)}
  \item \code{gsl\_vector\_set\_zero(vector)}
  \item \code{gsl\_vector\_set(vector, index, value)}
  \item \code{gsl\_vector\_get(vector, index)}
  \item \code{gsl\_matrix\_memcpy(matrixToCopyFrom, matrix)}
  \item \code{gsl\_linalg\_SV\_decomp(A, V, S, workspaceVector)}
  \item \code{gsl\_vector\_minmax(vector, minInVector, maxInVector)}
\end{itemize}
In order to factorize a matrix into the LU decomposition, and then solve the square system $Ax=y$ using the decomposition of A, I've used the following methods:
\begin{itemize}
  \item \code{gsl\_linalg\_LU\_decomp(A, permutation, signum)}
  \item \code{gsl\_linalg\_LU\_solve(LU, permutation, b, x)}
  \item \code{gsl\_permutation\_alloc(size)}
\end{itemize}
The following method from the C Math library was used in this exercise to calculate the absolute value of a number:
\begin{itemize}
  \item \code{fabs(x)}
\end{itemize}

\section{Solving the Linear System}
By looking closely at the first rule, we see that the $i+1$ are all multiples of 3 ($i+1 = 3*k$, for some $k$). Hence the $i$ are of the form $i = 3*k-1$, for some $k$. For $n = 5$, for example, this is what the coefficient matrix looks like:
$$
\begin{bmatrix}
1.000000000e+00 & -1.000000000e+00 & 0.000000000e+00 & 0.000000000e+00 & 0.000000000e+00 \\
1.000000000e+00 & 2.000000000e+00 & -1.000000000e+00 & 0.000000000e+00 & 0.000000000e+00 \\
0.000000000e+00 & 1.000000000e+00 & 1.000000000e+00 & -1.000000000e+00 & 0.000000000e+00 \\
0.000000000e+00 & 0.000000000e+00 & 1.000000000e+00 & 1.000000000e+00 & -1.000000000e+00 \\
0.000000000e+00 & 0.000000000e+00 & 0.000000000e+00 & 1.000000000e+00 & 4.000000000e+00 \\
\end{bmatrix}
$$

The coefficients matrix A is first allocated by using the \code{gsl\_matrix\_alloc} method, then I set all the elements to zero with \code{gsl\_matrix\_set\_zero} and finally nested \code{for} loops fill the diagonal values by checking the indexes.\\

I used the \code{gsl\_vector\_alloc} method to create an instance of the vector. All of its elements were set to zero by using \code{gsl\_vector\_set\_zero(vector)}. The exercise asks us to set the first element of the $y$ vector to one, so I used \code{gsl\_vector\_set(vector, 0, 1)} to assign the value 1 to index 0. For $n=5$, we have:
$$
\vec{y}=
\begin{bmatrix}
1.000000000e+00 \\
0.000000000e+00 \\
0.000000000e+00 \\
0.000000000e+00 \\
0.000000000e+00 \\
\end{bmatrix}
$$

Then, I calculate the condition number of the matrix $A$ of order $n$ which will give me a better idea if this is a well-conditioned or an ill-conditioned linear system. In GSL there is no direct function that calculates the condition number, but it's possible to use the ratio of the largest singular value of matrix A, $\sigma_n (A)$, to the smallest $\sigma_1 (A)$:

$$\kappa(A) := \frac{\sigma_n (A)}{\sigma_1 (A)}= \frac{\norm{A}}{\norm{A^{-1}}^{-1}}$$

I proceed to factorize $A$ into its singular value decomposition $SVD$ using the \code{gsl\_linalg\_SV\_decomp} method, and then use $\code{gsl\_vector\_minmax}$ to extract the minimum and maximum singular values out of the vector $S$ that contains the diagonal elements of the singular value matrix. \\

For $n=5$, the condition number is

$$\kappa(A) = \frac{\sigma_n (A)}{\sigma_1 (A)}= \frac{4.205100611e+00}{1.142643287e+00}=3.680151678e+00$$

Given the $Ax=y$ system, my goal is now to find the vector of the unknowns $\vec{x}$. To do so, I first factorize $A$ into its LU decomposition by allocating a new matrix (so that the matrix which represents $A$ doesn't get overridden) using \code{gsl\_matrix\_memcpy} and then by calling \code{gsl\_linalg\_LU\_decomp}. This method utilizes Gaussian Elimination with partial pivoting to compute the decomposition. The following is the $LU$ matrix for $n=5$:
$$
\begin{bmatrix}
1.000000000e+00 & -1.000000000e+00 & 0.000000000e+00 & 0.000000000e+00 & 0.000000000e+00 \\
1.000000000e+00 & 3.000000000e+00 & -1.000000000e+00 & 0.000000000e+00 & 0.000000000e+00 \\
0.000000000e+00 & 3.333333333e-01 & 1.333333333e+00 & -1.000000000e+00 & 0.000000000e+00 \\
0.000000000e+00 & 0.000000000e+00 & 7.500000000e-01 & 1.750000000e+00 & -1.000000000e+00 \\
0.000000000e+00 & 0.000000000e+00 & 0.000000000e+00 & 5.714285714e-01 & 4.571428571e+00 \\
\end{bmatrix}
$$

I can now use the $LU$ matrix to solve the system by passing $LU$, $\vec{x}$, a permutation structure \code{gsl\_permutation} (it contains the order of the indexes of the equations in the system to keep track of pivoting) and $\vec{y}$ to \code{gsl\_linalg\_LU\_solve}. This method uses forward and back-substitution to modify the contents of the $\vec{x}$ vector given in input, which now looks like this (for $n=5$):

$$
\vec{x}=
\begin{bmatrix}
7.187500000e-01\\
-2.812500000e-01\\
1.562500000e-01\\
-1.250000000e-01\\
3.125000000e-02\\
\end{bmatrix}
$$

I calculate the error by subtracting the computed solution $x_{1}^{\ast}$ from the exact mathematical solution $\widetilde{x}$ (an approximated value can be obtained by using the $\code{M\_E}$ GSL constant minus 2).\\

\begin{table*}[htb]
\centering % used for centering table
\begin{tabular}{c c c c} % centered columns (4 columns)
$n$ & $\widetilde{x}_1$ & $x_1^{\ast}- \widetilde{x}_1$ & $\kappa(A_n)$ \\ [0.65ex] % inserts table
%heading
\hline % inserts single horizontal line
1 & 1.000000000e+00 & -2.817181715e-01 & 1.000000000e+00 \\
2 & 6.666666667e-01 & 5.161516179e-02 & 1.767591879e+00 \\
3 & 7.500000000e-01 & -3.171817154e-02 & 2.561552813e+00 \\
4 & 7.142857143e-01 & 3.996114173e-03 & 2.258696038e+00 \\
5 & 7.187500000e-01 & -4.681715410e-04 & 3.680151678e+00 \\
6 & 7.179487179e-01 & 3.331105103e-04 & 3.953864002e+00 \\
7 & 7.183098592e-01 & -2.803069588e-05 & 3.847674609e+00 \\
8 & 7.182795699e-01 & 2.258566572e-06 & 5.377037588e+00 \\
9 & 7.182835821e-01 & -1.753630507e-06 & 5.727581839e+00 \\
10 & 7.182817183e-01 & 1.101773268e-07 & 5.498872833e+00 \\
11 & 7.182818352e-01 & -6.746947445e-09 & 7.100335770e+00 \\
12 & 7.182818229e-01 & 5.515095380e-09 & 7.582164638e+00 \\
13 & 7.182818287e-01 & -2.766507023e-10 & 7.195531702e+00 \\
14 & 7.182818284e-01 & 1.364375279e-11 & 8.833149892e+00 \\
15 & 7.182818285e-01 & -1.153854789e-11 & 9.488074730e+00 \\
16 & 7.182818285e-01 & 4.816147481e-13 & 8.911558696e+00 \\
17 & 7.182818285e-01 & -1.998401444e-14 & 1.057152285e+01 \\
18 & 7.182818285e-01 & 1.709743458e-14 & 1.142018246e+01 \\
19 & 7.182818285e-01 & -6.661338148e-16 & 1.063813407e+01 \\
20 & 7.182818285e-01 & -1.110223025e-16 & 1.231319966e+01 \\
21 & 7.182818285e-01 & -2.220446049e-16 & 1.336883104e+01 \\
22 & 7.182818285e-01 & -2.220446049e-16 & 1.237107821e+01 \\
23 & 7.182818285e-01 & -2.220446049e-16 & 1.405700479e+01 \\
24 & 7.182818285e-01 & -2.220446049e-16 & 1.532862983e+01 \\
25 & 7.182818285e-01 & -2.220446049e-16 & 1.410816377e+01 \\
26 & 7.182818285e-01 & -2.220446049e-16 & 1.580226249e+01 \\
27 & 7.182818285e-01 & -2.220446049e-16 & 1.729630706e+01 \\
28 & 7.182818285e-01 & -2.220446049e-16 & 1.584809348e+01 \\
29 & 7.182818285e-01 & -2.220446049e-16 & 1.754855617e+01 \\
30 & 7.182818285e-01 & -2.220446049e-16 & 1.926975724e+01 \\
31 & 7.182818285e-01 & -2.220446049e-16 & 1.759006043e+01 \\
32 & 7.182818285e-01 & -2.220446049e-16 & 1.929561485e+01 \\
33 & 7.182818285e-01 & -2.220446049e-16 & 2.124756325e+01 \\
34 & 7.182818285e-01 & -2.220446049e-16 & 1.933353645e+01 \\
35 & 7.182818285e-01 & -2.220446049e-16 & 2.104325456e+01 \\
36 & 7.182818285e-01 & -2.220446049e-16 & 2.322873622e+01 \\
37 & 7.182818285e-01 & -2.220446049e-16 & 2.107816128e+01 \\
38 & 7.182818285e-01 & -2.220446049e-16 & 2.279134599e+01 \\
39 & 7.182818285e-01 & -2.220446049e-16 & 2.521256520e+01 \\
40 & 7.182818285e-01 & -2.220446049e-16 & 2.282368084e+01 \\
41 & 7.182818285e-01 & -2.220446049e-16 & 2.453979556e+01 \\
42 & 7.182818285e-01 & -2.220446049e-16 & 2.719852600e+01 \\
43 & 7.182818285e-01 & -2.220446049e-16 & 2.456991077e+01 \\
44 & 7.182818285e-01 & -2.220446049e-16 & 2.628853390e+01 \\
45 & 7.182818285e-01 & -2.220446049e-16 & 2.918622370e+01 \\
46 & 7.182818285e-01 & -2.220446049e-16 & 2.631671410e+01 \\
47 & 7.182818285e-01 & -2.220446049e-16 & 2.803750846e+01 \\
48 & 7.182818285e-01 & -2.220446049e-16 & 3.117535515e+01 \\
49 & 7.182818285e-01 & -2.220446049e-16 & 2.806398692e+01 \\
50 & 7.182818285e-01 & -2.220446049e-16 & 2.978667872e+01 \\\hline %inserts single line
\end{tabular}
\end{table*}

\section{Plot}
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{cond_number.png}
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{error.png}

\section{Observations}
The linear system presented in this exercise gets increasingly ill-conditioned as $n$ grows (since $\kappa(A_n)>> 1$ for most $n$). From the plot, it can be observed that the condition number grows linearly. It can be noticed, however, that a large condition number doesn’t necessarily mean that the error will be large in all cases, just that it is possible to have a large error. In fact, the error which represents how well the computed solution $\widetilde{x}_1$ approximates the true solution $x_1^{\ast}$ gets incrementally smaller. This computation presents round-off errors because computers cannot work with infinite numbers. It can be noted that the Gaussian elimination with partial pivoting doesn't introduce any additional truncation errors and therefore it is numerically stable.\\

\end{document}


\documentclass{article}

\usepackage{mathrsfs,amssymb,amsmath}

\begin{document}

Page whatever
\section{Show that wk is the smallest topology on $\mathscr{X}$ such that each $x^*$ in $\mathscr{X}^*$}

Must show that an arbitrary open set in wk can be generated by some collection of sets of the form $x^{*-1}(V)$.

Start with an arbitrary open set $U$. Since $X$ with wk is a locally convex set, $\bigcap^n_{i=1}\{x\in \mathscr{X} : p_j( x-x_0) < \varepsilon_j\} \subseteq U$ for some finite list of $\varepsilon$ and $p$, where $p_{x^*}=|\langle x,x^*\rangle|$.

 $\bigcap^n_{i=1}\{x\in \mathscr{X} : p_j( x-x_0) < \varepsilon_j\}$ is open and the intersection of pre-images of open sets from $\mathbb{F}$, which are simply open balls of radius $\varepsilon_j$ around $x_0$.

Since $U$ is generated by the subbase of preimages of the collection of $x^*$ and $U$ is arbitrary, all open sets of wk are generated in this way, thus is the smallest possible topology.

\section{Show that wk* is the smallest topology on $\mathscr{X}^*$ such that for each $x$ in $\mathscr{X}$, $x^* \mapsto \langle x, x^* \rangle$}

This purported topology $\sigma$ is generated by open sets that are preimages of open sets of functions of the form $x^* \mapsto \langle x, x^* \rangle$. By continuity of composition, they are also the preimages of open sets of functions of the form $x^* \mapsto | \langle x, x^* \rangle | $. Open sets of $\mathbb{R}$ are generated by open balls, so the pre-images are generated by sets of the form $| \langle x, x^* \rangle | < \varepsilon $.

A set $U$ of $\mathscr{X}^*$ is weakly open if and only if for every $x^*_0$ in $U$ there is an $\varepsilon$ and there are $x_1, ... x_n$ in $\mathscr{X}$ such that

$$\bigcap^n_{i=1}\{x^*\in \mathscr{X}^* : |\langle x_k, x^* - x^*_0 \rangle| < \varepsilon_j\} \subseteq U$$

Every such set $U$ can be generated as part of $\sigma$ and therefore is the smallest topology

\section{Prove Theorem 1.3}

Theorem 1.3 is proven in the text!

\section{Let $\mathscr{X}$ be a complex LCS and let $\mathscr{X}^*_{\mathbb{R}}$ denote the collection of all continous real linear functionals on $\mathscr{X}$. Use the elements of $\mathscr{X}^*_{\mathbb{R}}$  to define seminorms on $\mathscr{X}$ and let $\sigma(\mathscr{X}, \mathscr{X}^*_{\mathbb{R}})$ be the corresponding topology. Show that $\sigma(\mathscr{X}, \mathscr{X}^*) = \sigma(\mathscr{X}, \mathscr{X}^*_{\mathbb{R}})$}

Since every complex linear function is also real linear, $\mathscr{X}^*$ is a subset of $\mathscr{X}^*_{\mathbb{R}}$,  hence  $\sigma(\mathscr{X}, \mathscr{X}^*) \subset \sigma(\mathscr{X}, \mathscr{X}^*_{\mathbb{R}})$

Thus we need to show that $\sigma(\mathscr{X}, \mathscr{X}^*_{\mathbb{R}}) \subset \sigma(\mathscr{X}, \mathscr{X}^*)$.

An arbitrary open sets around $x_0$ in $\sigma(\mathscr{X}, \mathscr{X}^*_{\mathbb{R}})$ is generated by sets that are pre-images of open sets of a finite set of real-linear functions. Take one such real-linear function $f$ which takes an open neighborhood $U_0$ to some open neighborhood $V_0$, and consider some open rectangle. If we can show that $U_0$ is open in $\sigma(\mathscr{X}, \mathscr{X}^*)$, this will allow us to conclude that any arbitrary in $\sigma(\mathscr{X}, \mathscr{X}^*_{\mathbb{R}})$ is open in $\sigma(\mathscr{X}, \mathscr{X}^*)$.

If $f$ is real linear, then define $\hat{f}(z) \equiv f(z) - i f(iz)$. The image of $U_0$ under $\hat{f}$ is $U_0$ plus $ (-i) W_0$, where $W_0$ is the image of $U_0$ times $i$. Since an open set plus an arbitrary set in a $LCS$ is open, it doesn't matter what exactly $W_0$, the image will be open in $\mathbb{C}$ (is this sufficient?). Hence if $\hat{f}$ is shown to be complex linear, then $U_0$ is open in $\sigma(\mathscr{X}, \mathscr{X}^*)$ and the proof is concluded.

\begin{align}
\hat{f}( \alpha z) &= f(\alpha z) - i f (\alpha i z) \\
&= f( (a+bi) z) - i f((a+bi) i z) \\
&= f(az + biz) - i f(aiz - bz) \\
&= f(az) + f(biz) - if(aiz) + if(bz) \\
&= af(z) + bf(iz) - iaf(iz) + bif(z) \\
&= (a+bi) (f(z) - i f(iz)) \\
&= \alpha (f(z) - i f(iz)) \\
&= \alpha \hat{f}(z)
\end{align}

5) because $f$ is real linear and $a$ and $b$ are real. 6) is just collecting terms. This shows that $\hat{f}$ is complex linear.

\section{If $A \subseteq  \mathscr{X}$...}

\subsection{$A^{\circ}$ is convex and balanced}

$A^{\circ} \equiv \{x^*\in \mathscr{X}^*: | \langle a, x^*\rangle | \le 1 \text{ for all } a \text{ in } A\}$

To show that $A^{\circ}$ is balanced, given $x^*$ in $A^{\circ}$ and $|\alpha| \le 1$, we have to show that $\alpha x^*$ is also in $A^{\circ}$.

If $|\langle a, x^*\rangle | \le 1$, then $|\langle a, \alpha x^*\rangle | = |\alpha |  | \langle a, x^* \rangle | \le 1$, hence $\alpha x^*$ is in $A^{\circ}$

To show that $A^{\circ}$ is convex, we have to show that given arbitrary $x_1^*$ and $x_2^*$ in $A^{\circ}$, then an arbitrarily chosen $x_3^*$ in $\{t x_1^* + (1-t) x_2^* :0\le t \le 1\}$ is also in $A^{\circ}$, say $t_3 x_1^* + (1-t_3) x_2^*$ (where $t_3$ was chosen arbitrarily in $0\le t \le 1\}$).

Hence we need to prove that, for all $a$ in $A$, $|\langle a, t_3 x_1^* + (1-t_3) x_2^* \rangle | \le 1$

If $|\langle a, x_1^*\rangle | \le 1$ and $|\langle a, x_2^*\rangle | \le 1$, then simply by multiplication of both sides $t_3|\langle a, x_1^*\rangle | \le t_3$ and $(1-t_3)|\langle a, x_2^*\rangle | \le 1-t_3$ since $0 \le t_3 \le 1$ (to not flip the signs). Adding the inequalities we get:

\begin{align}
t_3|\langle a, x_1^*\rangle | +(1-t_3)|\langle a, x_2^*\rangle | \le 1 \\
|\langle a, t_3 x_1^*\rangle | +|\langle a, (1-t_3) x_2^*\rangle | \le 1 \\
|\langle a, t_3 x_1^* + (1-t_3) x_2^*\rangle | \le 1
\end{align}

Both 10) and 11) by linearity. Note that (9) could also have been obtained geometrically, by noting that the unit disk is convex, so any point between any two points would also be in the unit disk.

\subsection{If $A_1 \subseteq A$, then $A^{\circ} \subseteq A^{\circ}_1$}

If $x^* \in A^{\circ}$, then $| \langle a,x^* \rangle | \le 1$ for all $a$ in $A$. Since $A_1 \subseteq A$,  $| \langle a,x^* \rangle | \le 1$ for all $a$ in $A_1$ as well. Therefore $x^* \in A^{\circ}$. Since $x^*$ was chosen arbitrarily,  $A^{\circ} \subseteq A^{\circ}_1$.

\section{If $A\subseteq\mathscr{X}$, show that $A$ is weakly bounded if and only if $A^{\circ}$ is absorbing in $\mathscr{X}^*$}

This works equally well at any point $x$ in $\mathscr{X}$ so assume that $0 \in A$ and  $A^{\circ}$ is absorbing in $\mathscr{X}^*$ at $0$.

Assume $A$ is weakly bounded. Thus for every $x^*$ in $\mathscr{X}$, $x^*(A)$ is bounded in $\mathbb{C}$. Take an arbitrary $x^*_0$ such that $x^*_0(A)$ is bounded by $M_0$. Take $\varepsilon_0 = 1 / M_0$. We must show that for $0 \le t < \varepsilon_0$, $tx^* \in A^{\circ}$. Indeed, for all $a$ in $A$,

\begin{align}
|\langle a, x^* \rangle | \le M_0 \\
|\langle a, t x^* \rangle | \le \varepsilon_0
\end{align}

13) by linearity. Thus all of $t x^*$ is in $A^{\circ}$, so $A^{\circ}$ is absorbing.

Assume $A^{\circ}$ is absorbing in $\mathscr{X}$. Thus for each $x^*$ in $\mathscr{X}$ there is an $\varepsilon > 0$ such that $tx^* \in A^{\circ}$ for $0 \le t < \varepsilon$, i.e. $| \langle a, t x^* \rangle | \le 1$. Thus, by linearity, $x^*$ cannot be larger than $1/\varepsilon$, and is thus bounded.

\end{document}


\section{Currency Input IO}
The purpose of this section is to Provide extra detail on the specification of IO streams and how they interface with other devices.

\subsection{Input Details}
\label{selector}
\begin{itemize}
\item \textbf{Clock:} The clock signal tells the circuit to add the value of an accepted bill to the value that is currently stored in memory. In a real vending machine, the clock pulse would be generated when the bill reader accepted an inserted bill as valid. We simulate this using a manually operated two-button clock pulse generator.
\item $\mathbf{B_{0..5}}$\textbf{:} Together these inputs are a binary representation of the value of the bill with $B_0$ being the least significant bit. In a real vending machine, this number would be generated by the bill reader. We simulate this in our circuit using 6 switches.
\item \textbf{Selector:} The selector bit switches the ALUs between addition and subtraction while adjusting their $C_n$ bits accordingly. Addition is used for currency input and subtraction is used for vending and making change. The value of this bit is set by the item selection and change making circuits.
\end{itemize}

\subsection{Output Details}
\label{dollars-in-machine}
\begin{itemize}
\item \textbf{Dollars In Machine:} There are six D flip flops that store the amount of money currently in the machine. On the board, they are numbered as such where FF represents a 74LS74: \\\\
\begin{tabular}{|c|c|c|}
\hline
4 & FF & 1 \\
\hline
5 & FF & 2 \\
\hline
6 & FF & 3 \\
\hline
\end{tabular}
\end{itemize}


\documentclass[main.tex]{subfiles}
\begin{document}

\marginpar{Tuesday\\ 2020-11-17, \\ compiled \\ \today}

The papers \textcite[]{verdeFirstYearWilkinson2003} and \textcite[]{spergelFirstYearWilkinson2003} are discussed.

\section{Bayesian Hierarchical Modeling}

An example from the lectures by A.\ Heavens, in the ICIC data analysis workshop.

Suppose we want to solve a linear regression problem for a line passing through the origin, and we have one data pair \((\hat{x}, \hat{y})\), with error on both.

We want the angular coefficient \(m\):
%
\begin{align}
\mathcal{P} (m | \hat{x}, \hat{y}) \propto \mathcal{L} (\hat{x}, \hat{y} | m) \mathbb{P}(m)
\,.
\end{align}

We know that the true \(x\) and \(y\) are related by \(y = mx\).
We can introduce these two as extra variables in the problem: applying \(\mathbb{P}(a, b) = \mathbb{P}(a | b) \mathbb{P}(b)\) repeatedly we find
%
\begin{align}
\mathbb{P}(\hat{x}, \hat{y}, x, y | m) \propto \mathbb{P}(\hat{x}, \hat{y} | x, y, m) \mathbb{P}(x, y | m) \propto \mathbb{P}(\hat{x}, \hat{y} | x, y) \underbrace{\mathbb{P}(y | x, m)}_{ = \delta (y - mx)} \mathbb{P}(x | m)
\,.
\end{align}

Then, we can get the final posterior by
%
\begin{align}
\mathcal{P} (m | \hat{x}, \hat{y}) &\propto \int \dd{x} \dd{y} \mathbb{P}(\hat{x}, \hat{y}, x, y | m) \mathbb{P}(m)  \\
&\propto \int \dd{x} \dd{y} \underbrace{\mathbb{P}(\hat{x}, \hat{y} | x, y)}_{\text{error}} \underbrace{\mathbb{P}(y | x, m)}_{\text{theory}} \underbrace{\mathbb{P}(x | m) \mathbb{P}(m)}_{\text{prior}} \\
&\propto \int \dd{x} \mathbb{P}(\hat{x}, \hat{y} | x, mx)  \mathbb{P}(x | m) \mathbb{P}(m)
\,,
\end{align}
%
and assuming \(\sigma = 1\) independent Gaussians for the error distribution, we get
%
\begin{align}
\mathbb{P}(m | \hat{x}, \hat{y}) &\propto
\int \dd{x} \exp(- \frac{1}{2} (\hat{y} - mx)^2) \exp( - \frac{1}{2} (\hat{x} - x)^2)  \\
&\propto \frac{1}{\sqrt{1 + m^2}} \exp( - \frac{1}{2 (1+m^2)} (- m \hat{x} + \hat{y})^2 )
\,.
\end{align}

Alternatively, we can marginalize with Monte Carlo integration.

This method works well for Gibbs sampling: it is easy to find the conditional distributions.

This can be applied to SN Ia data.

\end{document}


\documentclass[8pt]{article}

\usepackage{fullpage}
\usepackage[margin=.7in]{geometry}
\usepackage{epic}
\usepackage{eepic}
\usepackage{graphicx}
\usepackage{mathtools}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{ragged2e}
\usepackage[parfill]{parskip}

\newcommand{\proof}[1]{
{\noindent {\it Proof.} {#1} \rule{2mm}{2mm} \vskip \belowdisplayskip}
}

\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}

\newtheorem{lemma}{Lemma}[section]
\newtheorem{theorem}[lemma]{Theorem}
\newtheorem{claim}[lemma]{Claim}
\newtheorem{definition}[lemma]{Definition}
\newtheorem{corollary}[lemma]{Corollary}

%\setlength{\oddsidemargin}{0in}
%\setlength{\topmargin}{0in}
%\setlength{\textwidth}{6.5in}
%\setlength{\textheight}{8.5in}

\begin{document}

\hfill \small{\today} \\
\setlength{\fboxrule}{.5mm}\setlength{\fboxsep}{1.2mm}
\newlength{\boxlength}\setlength{\boxlength}{\textwidth}
\addtolength{\boxlength}{-4mm}
\begin{center}\framebox{\parbox{\boxlength}{\bf
\center{CS 577 - Homework 3}
\center{Sejal Chauhan, Vinothkumar Siddharth, Mihir Shete}
}}\end{center}
\vspace{5mm}

\section{Graded written problem}

\textbf{Input:} A sequence of n real numbers $a_1$, $a_2$,..., $a_n$ and a corresponding sequence of weights $w_1$, $w_2$, . . . , $w_n$. The weights are nonnegative reals that add up to 1, i.e. $\sum_n^{i=1} w_i = 1$. \\
\\
\textbf{Output:} The weighted median of the sequence is the number $a_k$ such that
$\sum_{a_i \textless a_k} w_i \textless 1/2$ and $\sum_{a_i \textless= a_k} w_i \geq 1/2$

\begin{algorithm}
\caption{Algorithm to find weighted median}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Weighted-Median}{$A_w^a$}

\If{$length(A_w^a) = 1$}
    \State \Return{A[0]}
\EndIf

\State $Median \leftarrow \Call{Selection}{A_w^a, \ceil{length(A_w^a)/2}}$ \Comment{The Selection will happen over A}
    \State $Pivot \leftarrow Median$
    \State $L_{w}^{a},R_{w}^{a} \leftarrow \Call{Partition}{A_w^a, Pivot}$

    \If{$\sum w_L$ \textless $1/2$}
        \State $w_{Pivot} \leftarrow \sum w_L + w_{Pivot}$
        \If{$w_{Pivot} \geq 1/2$}
            \State \Return ${Pivot}$
        \EndIf
        \State $\Call{Weighted-Median}{Pivot|R_w^a}$
    \Else
        \State $\Call{Weighted-Median}{L_w^a}$
    \EndIf

\EndProcedure
\end{algorithmic}
\end{algorithm}

\begin{flushleft}
Our Algorithm uses the Selection() procedure discussed in section 6 of the Lecture
notes on Divide and Conquer. When \textit{Weighted-Median} is called it will first
find the $\ceil{n/2}$ smallest element in A using the Selection() procedure in linear
time, let's call this value \textit{Median}(because it is actually the median of
sorted elements in A). We will use this value to \textit{Partition} the Array A into
2 arrays L and R such that the Value $a_i^L$ of all elements in L is less than or equal
to value of \textit{Median} and Value $a_i^R$ of all elements in R is greater than \textit{Median}.

If the weight of all elements in L is less than 1/2 and on adding the weight of the \textit{Pivot}
it becomes greater than or equal to 1/2 then \textit{Pivot} is our Weighted Median and
we return the value of the \textit{Pviot}. If the weight of all elements in L is greater
than or equal to 1/2 then we can say that the Weighted Median is present in the array L
and we recursively call \textit{Weighted-Median} on L.

If the aggregate of weight of elements in L and the weight of \textit{Pivot} is less than 1/2
then we know the Weighted Median is in R. So we change the weight of the
\textit{Pivot} as: $w_{Pivot} \leftarrow w_{Pivot} + \sum L_w$. So \textit{Pivot's} weight
now also includes the weight of elements to its Left(i.e all elements less than equal to pivot).

After changing the weight of \textit{Pivot} we will recursively call \textit{Weighted-Median}
procedure on array $\textit{Pivot}|R$. Prepending \textit{Pivot} with added weight to R
assures that we consider the weight of all elements which are lesser than all elements in R
while calculating the Weighted Median from the new sub-array, so the Weighted Median returned by the recursive call is actually the Weighted Median of original Array A.

\newpage
\subsection{Correctness}
We will proceed to prove the correctness of our algorithm by proving partial correctness
and termination.

\subsubsection{Partial Correctness}
The recursive calls to \textit{Weighted-Median} will be made on sub-array of the input.
It is trivial to see that the length of the sub-array will never be negative or 0. So
the recursive calls to \textit{Weighted-Median} will always have a valid array as input.

The algorithm returns from 2 places. The first case is trivially true because if the length
of the array is 1 then the only element in the array is the Weighted-Median. In the second
return we see that the sum of all elements to the Left of the \textit{Pivot} in the partitioned
array is less than 1/2 and when we add the weight of the \textit{Pivot} the aggregated weight
becomes $\geq{1/2}$ then we say that the \textit{Pivot} is the Weighted Median which is true
as per definition because all the elements to the left of \textit{Pivot} in the partitioned
array are less than or equal to the \textit{Pivot}.

\subsubsection{Termination}
Let $\mu(length(A))$ be the potential function then we can see that in the recursive calls

\begin{center}
    $\mu(length(A_{recursive)}) = \mu(length(A[0...\ceil{n/2}]))$ OR $\mu(length(A_{recursive)}) = \mu(length(A[\ceil{n/2} + 1 ... n - 1])) \leq \mu(length(A))$
\end{center}

So the potential function will decrease each recursive call and our program is guaranteed to terminate.

\subsection{Complexity Analysis}
Its trivial to see that our recursive calls operate on an input of size $\ceil{n/2} \pm 1$ and in
worst case our algorithm will go through log(n) recursive calls.

All the steps in our procedure take constant time except \textit{Selection} and \textit{Partition}
which take linear time. So total running time of our algorithm in worst case will be:
\begin{center}
$c*(n/2^0) + c*(n/2^1) + c*(n/2^2) ... + c*(n/2^{log(n)}) \le c*n*(1 + 1/2 + 1/4 ....)$
\end{center}
The above is a geometric series with sum limiting to 2. Hence, our algorithm can take c*2*n time in worst case and so it is linear in n i.e O(n)
\end{flushleft}

\end{document}


\documentclass[letterpaper,12pt,leqno]{article}
\usepackage{paper,math,notes}
\available{https://www.pascalmichaillat.org/t3.html}
\hypersetup{pdftitle={Optimal Control}}

\begin{document}

\title{Optimal Control}
\author{Pascal Michaillat}
\date{}

\begin{titlepage}
\maketitle
\tableofcontents
\end{titlepage}

\section{Preliminary Results}

Before presenting the techniques of optimal control, we need to review two mathematical results: L'Hopital's rule and Leibniz's rule. These results are used in section~\ref{sec:HEURISTIC} for the derivation of some of the results.

\subsection{L'Hopital's rule}

L'Hopital's rule states that if
\begin{equation*}
\lim_{x\to x_{0}}f(x) =\lim_{x\to x_{0}}g(x) =0,
\end{equation*}
then
\begin{equation*}
\lim_{x\to x_{0}}\frac{f(x)}{g(x)}=\lim_{x\to x_{0}}\frac{f'(x) }{g'(x)}.
\end{equation*}

\subsection{Leibniz's rule}

Leibniz's rule states that if
\begin{equation*}
I(z) \equiv \int_{a(z) }^{b(z)}f\bp{x,z} dx
\end{equation*}
where $x$ is the integration variable, $z$ is a parameter and $f\bp{ x,z} $
is assumed to have a continuous derivative $\pdx{f(x,z)}{z}$ in the
interval $\bs{a(z),b(z)}$, then the effect of change in $z$ on the integral is given by
\begin{equation*}
\od{I}{z}(z) =\int_{a(z)}^{b(z)} \pd{f}{z}(x,z) dx+\od{b}{z}(z) \cdot f( b(z),z) -\od{a}{z}(z) \cdot f(a(z) ,z).
\end{equation*}

\section{Consumption-Saving Problem}

\subsection{Description of the Problem}

We consider the same consumption-saving problem as in the notes on dynamic programming, except that the problem is now in continuous time. Taking initial wealth $a_{0}$ as given, the problem is to choose the consumption path $\bc{c(t)}_{t\geq 0}$ to maximize the lifetime utility
\begin{align}
\int_{0}^{\infty}e^{-\r \cdot t}\cdot u(c(t)) dt \label{eq:cont},
\end{align}
subject to the law of motion
\begin{equation}
\dot{a}(t) = r\cdot a(t)-c(t).\label{eq:LAW}
\end{equation}
The parameter $r>0$ is the constant interest rate at which wealth is invested. The parameter $\r>0$ is the discount factor. The notation $\dot{a}$ denotes the derivative of wealth $a$ with respect to time $t$:
\[\dot{a}(t)\equiv \pd{a(t)}{t}.\]

\subsection{Optimal-Control Approach}

To solve this problem, it is inconvenient to use the Lagrangian technique or dynamic programming technique because they are designed for discrete-time optimization problems. Instead, we use a technique called \textit{optimal control}. We will see in section~\ref{sec:HEURISTIC} that optimal control is related both to the Lagrangian technique and the dynamic programming technique. But optimal control is designed for continuous-time optimization problems.

There are two ways to apply the optimal-control approach: one by forming a \textit{present-value Hamiltonian}, the other by forming a \textit{current-value Hamiltonian}. These two ways are roughly equivalent, but using the current-value Hamiltonian is usually simpler.

\section{Solution with Present-Value Hamiltonian}

We start by solving the consumption-saving problem with a present-value Hamiltonian.

\subsection{State and Control Variables}

First, identify the \textit{state variables} and \textit{control variables}. A control variable can be adjusted at any time $t$ whereas the evolution of a state variable follows a law of motion such as~\eqref{eq:LAW}. Here, the control variable is consumption $c(t)$ and the state variable is wealth $a(t)$.

\subsection{Present-Value Hamiltonian}

Second, write down the present-value Hamiltonian
\begin{equation*}
\Hc(t) = e^{-\r \cdot t}\cdot u(c(t)) +\l(t) \cdot \bp{r\cdot a(t) - c(t)}.
\end{equation*}
To form the Hamiltonian, we introduce a new variable $\l(t)$, which we call the \textit{costate variable} associated with the state variable $a(t)$. In general, we introduce as many costate variables as there are state variables. As a consequence, we introduce as many costate variables as there are laws of motion, because each state variable is associated with one law of motion.

\subsection{Optimality Conditions}

Write down the two optimality conditions, which are derived from the Maximum Principle:
\begin{align}
\pd{\Hc(t)}{c(t)}=& 0 \label{eq:foc1}\\
\pd{\Hc(t)}{a(t)}=&-\dot{\l}(t)\label{eq:foc2}.
\end{align}

The optimality conditions can be rewritten as
\begin{align}
0 &= e^{-\r \cdot t}\cdot u'(c(t)) -\l(t)\label{eq:focc} \\
-\dot{\l}(t)&= \l(t)\cdot  r  \label{eq:focs}.
\end{align}

\subsection{Transversality Condition}

Impose a \textit{transversality condition}:
\begin{equation}
\lim_{t\to+\infty} \l(t)\cdot a(t)=0.\label{eq:trans1}
\end{equation}
The transversality condition prevents the state variable to grow without bounds. It makes sure that the discounted present value of consumption does not exceed the discounted present value of the income from investment.

\subsection{Euler Equation}

We can solve explicitly for the optimal consumption path by eliminating the costate variable $\l(t)$ using~\eqref{eq:focc} and~\eqref{eq:focs}.

To eliminate $\l(t)$, we first take log of \eqref{eq:focc}:
\begin{align*}
-\r \cdot t+ \ln{u'(c(t))} =\ln{\l(t)}.
\end{align*}
We then take time derivatives in this equation:
\begin{equation*}
\r+\bs{ \frac{-u^{''}(c(t)) \cdot c(t)}{u'(c(t))}}\cdot \bs{\frac{\dot{c}(t)}{c(t)}} =-\frac{\dot{\l}(t)}{\l(t)}.
\end{equation*}
Equation~\eqref{eq:focs} can be rewritten as
\begin{align*}
-\frac{\dot{\l}(t)}{\l(t)}&=  r.
\end{align*}
Combining these two equations, we obtain the Euler equation for optimal consumption:
\begin{equation}
\frac{\dot{c}(t)}{c(t)}\cdot \bs{\frac{-u^{''}(c(t)) \cdot c(t)}{u'(c(t)) }} =r-\r.
\label{eq:EULER}\end{equation}
The term \[\frac{-u^{''}(c(t))\cdot  c(t)}{u'(c(t))}\] measures relative risk aversion. The coefficient of relative risk aversion also corresponds to the inverse of the intertemporal elasticity of substitution.

\subsection{CRRA Utility}

Consider the following class of utility function:
\begin{equation*}
u(c) =\frac{c^{1-\g }-1}{1-\g }.
\end{equation*}
This class of utility function is know as Constant Relative Risk Aversion utility, or CRRA utility. This class of function is characterized by a constant coefficient of relative risk aversion $\g$ as
\[\frac{-u^{''}(c) \cdot  c}{u^{'}(c)}=\g.\]
With CRRA utility, the Euler equation simplifies to
\begin{equation*}
\frac{\dot{c}(t)}{c(t)}=\frac{r-\r}{\g}.
\end{equation*}

\section{Solution with Current-Value Hamiltonian}

Next we solve the consumption-saving problem with a current-value Hamiltonian.

\subsection{Current-Value Hamiltonian}

The present-value Hamiltonian $\Hc$ depends on $t$ because of the discounting $e^{-\r \cdot t}$, which might create some difficulties in deriving and analyzing solutions to the problem. Multiplying $\Hc$ by $e^{\r \cdot t}$ addresses this problem.

We denote the resulting Hamiltonian $\Hc^{*}$ as current-value Hamiltonian. The current-value Hamiltonian is given by
\begin{equation*}
\Hc^{*}(t)\equiv e^{\r \cdot t}\cdot  \Hc(t)=u(c(t)) +e^{\r \cdot t} \cdot \l(t) \cdot \bp{r\cdot a(t)-c(t)}.
\end{equation*}
We define a new costate variable $q(t)$ as
\begin{equation*}
q(t)\equiv  e^{\r \cdot t} \cdot \l(t).
\end{equation*}
The current-value Hamiltonian becomes
\begin{equation*}
\Hc^{*}(t)= u(c(t)) +q(t) \cdot \bp{r\cdot a(t)-c(t)}.
\end{equation*}
This is the expression of the current-value Hamiltonian that we use in practice.

\subsection{Optimality Conditions}

The optimality conditions are slightly different. The optimality condition become
\begin{align*}
\pd{\Hc^{*}(t)}{c(t)}=& 0\\
\pd{\Hc(t)^{*}}{a(t)}=&\r\cdot q(t)-\dot{q}(t).
\end{align*}

Compared to the optimality conditions~\eqref{eq:foc1} and~\eqref{eq:foc2} with the present-value Hamiltonian, there is an extra term $+\r\cdot q(t)$ in the second condition. The extra term arises because the costate variable $q(t)$ is defined differently form the costate variable $\l(t)$ that we used in the present-value Hamiltonian.

The optimality conditions can be rewritten as
\begin{align}
0 &= u'(c(t)) -q(t)\label{eq:auto1}\\
\r\cdot q(t)-\dot{q}(t)&= q(t)\cdot  r .\label{eq:auto2}
\end{align}

\subsection{Transversality Conditions}

We also impose a transversality condition:
\[\lim_{t\to+\infty}e^{-\r \cdot t} \cdot  q(t)\cdot a(t)=0.\]
Compared to the transversality condition~\eqref{eq:trans1} with the present-value Hamiltonian, there is an extra factor $e^{-\r \cdot t}$ in this condition. The extra factor arises because the costate variable $q(t)$ is defined differently form the costate variable $\l(t)$ that we used in the present-value Hamiltonian.

\subsection{Euler Equation}

By combining equations~\eqref{eq:auto1} and~\eqref{eq:auto2}, we obtain exactly the same condition~\eqref{eq:EULER} as with the present-value Hamiltonian. We take the log of equation~\eqref{eq:auto1} to obtain
\begin{align*}
\ln{u'(c(t))} = \ln{q(t)}.
\end{align*}
We then take time derivatives in this equation:
\begin{equation*}
\bs{ \frac{-u^{''}(c(t)) \cdot c(t)}{u'(c(t))}}\cdot \bs{\frac{\dot{c}(t)}{c(t)}} =-\frac{\dot{q}(t)}{q(t)}.
\end{equation*}
Equation~\eqref{eq:auto2} can be rewritten
\begin{align*}
-\frac{\dot{q}(t)}{q(t)}&= r-\r.
\end{align*}
Combining these two equations, we obtain the same Euler equation for optimal consumption as the one we obtained with the present-value Hamiltonian:
\begin{equation*}
\frac{\dot{c}(t)}{c(t)}\cdot \bs{\frac{-u^{''}(c(t)) \cdot c(t)}{u'(c(t)) }} =r-\r.
\end{equation*}

\subsection{Comparison of the Two Solutions}

The two approaches---the approach with the present-value Hamiltonian and the approach with the current-value Hamiltonian---are equivalent. They lead to the same Euler equation. However, it is often more convenient to work with the current-value Hamiltonian.

\section{Theory of Optimal Control}

\subsection{General Optimization Problem}

Optimal control is used to solve any continuous-time optimization problem. The general problem is to choose $\bc{c(t)}_{t\geq 0}$ to maximize
\begin{align}
\int_{0}^{\infty}e^{-\r\cdot  t}\cdot  u\bp{ a(t),c(t)}\label{dpc}
\end{align}
given the constraint that for all $t$
\begin{align}
\dot{a}(t) = g(a(t),c(t)),\label{eq:thelaw}
\end{align}
and taking $a_{0}$ as given. The parameter $\r >0$ is the discount rate. The functions $u$ and $g$ are concave and twice differentiable.

\subsection{Present-Value Hamiltonian}

An important result in optimal control theory is the Maximum Principle. It is due to Pontryagin. In addition to the control and state variables, we introduce a costate variable $\l (t)$  associated with the state variable. The costate variable measures the shadow price of the associated state variable. The costate variable enters the optimal control problem through the present-value Hamiltonian, defined as
\begin{equation}
\Hc(t)=e^{-\r\cdot  t}\cdot  u(a(t),c(t)) +\l(t)\cdot g(a(t),c(t)).
\label{eq:HAMILDEF}\end{equation}

\subsection{Present-Value Optimality Conditions}

The Maximum Principle gives necessary conditions for optimality. There are three conditions. The first two conditions are
\begin{align}
\pd{\Hc(t)}{c(t)} &=0  \label{eq:hcontrol} \\
\pd{\Hc(t)}{a(t)} &=-\dot{\l}(t)  \label{eq:hstate}
\end{align}
Condition \eqref{eq:hcontrol} implies that the Hamiltonian must be maximized
with respect to the control variable at any point in time. Condition \eqref{eq:hstate} says that the marginal change of the Hamiltonian associated with a unit change of the state variable is equal to  minus the rate of
change of the costate variable.

The optimal solution must also satisfy a third condition, which we call transversality condition:
\begin{equation}
\lim_{t\to \infty }\l(t)\cdot a(t)=0.  \label{eq:tvc}
\end{equation}
The transversality condition implies the product of costate and state must be converging to zero as time goes to infinity.

\subsection{Current-Value Hamiltonian}

We can reformulate the results from the Maximum Principle with the current-value Hamiltonian which is often easier to manipulate. The current-value Hamiltonian is defined as
\begin{equation*}
\Hc^{*}(t)=  u\bp{a(t),c(t)} +q(t)\cdot g(a(t),c(t)),
\end{equation*}
where $q(t)$ is the costate variable associated with the state variable $a(t)$.

\subsection{Current-Value Optimality Conditions}

With the current-value Hamiltonian, the three necessary conditions~\eqref{eq:hcontrol},~\eqref{eq:hstate}, and ~\eqref{eq:tvc} for optimality become
\begin{align*}
\pd{\Hc^{*}(t)}{c(t)} &=0\\
\pd{\Hc^{*}(t)}{a(t)} &=\r\cdot q(t)-\dot{q}(t)\\
\lim_{t\to \infty }e^{-\r\cdot  t}\cdot q(t)\cdot a(t)&=0.
\end{align*}

\section{Heuristic Derivation of the Maximum Principle}\label{sec:HEURISTIC}

In this section, we provide an heuristic derivation of the necessary conditions for optimality provided by the Maximum Principle. One way to derive the optimality conditions \eqref{eq:hcontrol} and \eqref{eq:hstate} is to apply informally the results from dynamic programming. Formally, many of the claims below are imprecise, but they will serve the purpose of providing intuition for the Maximum Principle.

\subsection{Value Function for the Discretized Optimization Problem}

We being by defining the value function of the problem, which the maximized value of the objective function as a function of the
state variable $a(t)$ and time $t$:
\begin{equation*}
V\bp{a(t),t} =\underset{\bc{c(s)}_{s\geq t}}{\max} \int_{t}^{\infty }e^{-\r \cdot \bp{s-t}}\cdot u\bp{a(s),c(s)} ds,
\end{equation*}
where the maximization is subject for all $s\geq t$ to the law of motion of the state variable
\begin{equation}
\dot{a}(s)=g\bp{a(s),c(s)}.\label{eq:lmh}
\end{equation}

\subsection{Bellman Equation}

Since the problem has a recursive structure, we can apply the Principle of Optimality and write the value function as the solution to a Bellman equation:
\begin{align*}
V\bp{ a(t),t} =\underset{\bc{c(s)}_{t\leq s\leq t+\D t}}{\max }\bc{\int_{t}^{t+\D t}e^{-\r \cdot \bp{ s-t}} \cdot u\bp{a(s),c(s)} ds+e^{-\r  \cdot \D t} \cdot V\bp{ a(t+\D t),t+\D t} },
\end{align*}
where the maximization is subject for all $t\leq s\leq t+\D t$ to~\eqref{eq:lmh}.

Subtract $V\bp{a(t),t} $ from both side and divide by $\D t$:
\begin{align}
0=\underset{\bc{c(s)}_{t\leq s\leq t+\D t}}{\max }\bs{\frac{\int_{t}^{t+\D t}e^{-\r \cdot \bp{ s-t}} \cdot u\bp{
a(s),c(s)} ds}{\D t}+ \frac{e^{-\r  \cdot \D t} \cdot V\bp{ a(t+\D t),t+\D t}-V\bp{ a(t),t}}{\D t} },\label{eq:BIG}
\end{align}
where the maximization is subject for all $t\leq s\leq t+\D t$ to~\eqref{eq:lmh}.

\subsection{Hamilton-Jacobi-Bellman Equation}

We now take the limit of \eqref{eq:BIG} as $\D t\to 0$ to obtain the Hamilton-Jacobi-Bellman equation.

\paragraph{Limit of First Term} We start with the first term in the curly brackets. Since numerator and denominator of the first term approach zero as $\D t\to 0$,  we apply L'Hopital's rule. The derivative of the denominator with respect to $\D t$ is $1$. We apply Leibniz's rule to determine the derivative with respect to $\D t$ of the integral in the numerator. Leibniz's rule tells us that the derivative of the integral with respect to $\D t$ is
\[e^{-\r \cdot  \D t} \cdot u\bp{a(t+\D t),c(t+\D t)}.\]
Therefore, the limit as $\D t\to 0$ for the first term in the bracket is
\begin{equation}
u\bp{ a(t),c(t)}.\label{eq:BIG1}
\end{equation}

\paragraph{Limit of Second Term}  We move on to the second term. Since
\[\lim_{\D t\to 0} e^{-\r \cdot \D t}=1\]
and
\[\lim_{\D t\to 0} V\bp{ a(t+\D t),t+\D t} =V\bp{a(t),t}, \]
both numerator and denominator approach zero as $\D t\to 0$. Therefore we apply L'Hopital's rule. The derivative of the denominator with respect to $\D t$ is $1$. The derivative of the numerator with respect to $\D t$ is
\begin{align*}
&-\r \cdot e^{-\r\cdot \D t}\cdot V\bp{ a(t+\D t),t+\D t}+e^{-\r\cdot \D t}\cdot \pd{V}{a}\bp{ a(t+\D t),t+\D t} \cdot \dot{a}(t+\D t)\\
&+e^{-\r\cdot \D t}\cdot  \pd{V}{t}\bp{a(t+\D t),t+\D t}.
\end{align*}
We have the following limits:
\begin{align*}
\lim_{\D t\to 0} \r \cdot e^{-\r\cdot \D t}\cdot V\bp{ a(t+\D t),t+\D t}&=\r \cdot V\bp{ a(t),t}\\
\lim_{\D t\to 0} e^{-\r\cdot \D t}\cdot  \pd{V}{t}\bp{a(t+\D t),t+\D t} &= \pd{V}{t}\bp{a(t),t}\\
\lim_{\D t\to 0} e^{-\r\cdot \D t}\cdot \pd{V}{a}\bp{ a(t+\D t),t+\D t}\cdot\dot{a}(t+\D t)&=\pd{V}{a}\bp{ a(t),t} \cdot \dot{a}(t)=\pd{V}{a}\bp{ a(t),t} \cdot g(a(t),c(t)),
\end{align*}
where the last equality results from the law of motion~\eqref{eq:thelaw} of state variable $a(t)$.

Therefore, the limit as $\D t\to 0$ for the first term in the bracket is
\begin{equation}
-\r \cdot V\bp{ a(t),t}+\pd{ V}{a}\bp{ a(t),t} \cdot g(a(t),c(t))+\pd{V}{t}\bp{a(t),t}.\label{eq:BIG2}
\end{equation}

\paragraph{Derivation of the Hamilton-Jacobi-Bellman Equation} Combining equations~\eqref{eq:BIG},~\eqref{eq:BIG1}, and~\eqref{eq:BIG2}, we obtain a version of the Bellman equation for the continuous-time optimization problem. This equation is called the \textit{Hamilton-Jacobi-Bellman equation}. The equation is
\begin{equation}
\r V\bp{ a(t),t} =\max_{c(t)}\bs{ u\bp{a(t),c(t)} +\pd{V}{a(t)}\bp{a(t),t}\cdot
g(a(t),c(t)) +\pd{V}{t}\bp{a(t),t}}, \label{eq:HJB}
\end{equation}
where $a(t)$ is given. We define \[\l (t)\equiv e^{-\r \cdot t}\cdot \pd{V}{a(t)}\bp{a(t),t}.\]
We can rewrite the Hamilton-Jacobi-Bellman equation as
\begin{equation}
\r V\bp{ a(t),t} =\max_{c(t)}\bs{ u\bp{a(t),c(t)} +e^{\r \cdot t}\cdot\l (t)  \cdot
g(a(t),c(t)) +\pd{V}{t}\bp{a(t),t}}. \label{eq:HJB2}
\end{equation}

\subsection{Derivation of the Optimality Conditions}

Taking the first-order condition with respect to $c(t)$ in the Hamilton-Jacobi-Bellman equation~\eqref{eq:HJB2} implies
\begin{equation*}
\pd{u}{c(t)}\bp{a(t),c(t)}+e^{\r \cdot t}\cdot\l (t) \cdot \pd{g}{c(t)}\bp{a(t),c(t)}=0.
\end{equation*}
Furthermore, the envelope theorem implies
\begin{align*}
\r \pd{V}{a(t)}\bp{a(t),t}&=\pd{u}{a(t)}\bp{a(t),c(t)}+e^{\r \cdot t}\cdot\l (t)\cdot \pd{g}{a(t)}\bp{ a(t),c(t)}+\frac{\partial^{2}V}{\partial t\partial a(t)}\bp{a(t),t}.
\end{align*}

The last two equations become equivalent to optimality conditions~\eqref{eq:hcontrol} and~\eqref{eq:hstate}. This is the case because, using the definition of the present-value Hamiltonian, equation~\eqref{eq:hcontrol} can be written
\[\pd{H(t)}{c(t)}\bp{a(t),c(t)} =0\]
and equation~\eqref{eq:hstate} can be written
\[\pd{u}{a(t)}\bp{a(t),c(t)} +e^{\r\cdot  t}\cdot \l(t)\cdot \pd{g}{a(t)}\bp{a(t),c(t)}=-e^{\r\cdot  t}\cdot \pd{\l(t)}{t},\]
which implies
\[\pd{H(t)}{a(t)}\bp{a(t),c(t)} =-\pd{\l(t)}{t}.\]
Note that we consider that the Hamiltonian is a function of $a_{t}$, $t$, and $\l_{t}$, and we only take the partial derivative with respect to $a_{t}$, thus keeping $\l_{t}$ constant.

\section{Applications of the Hamilton-Jacobi-Bellman Equation}

The Hamilton-Jacobi-Bellman equation~\eqref{eq:HJB} is an optimality condition that equates flow costs with flow benefits. In practice, we write it down without going through all the algebra relating to $\D t.$

This equation is commonly used in macroeconomics. For instance, it is frequently used in search-and-matching models of the labor market. In a search-and-matching model, a vacant job costs $c$ per unit time and becomes occupied according to a Poisson process with arrival rate $q.$ In the labor market, the occupied job yields net returns $p-w,$ where $p$ is real output and $w$ is the cost of labor. The job runs a risk $\l $ of being destroyed.

Let $V$ be the value of the vacant job and $J$ be the value of occupied job. Let $r$ be the discount factor. In steady state, the Hamilton-Jacobi-Bellman equations are
\begin{align*}
r\cdot V &=-c+q \cdot  \bp{J-V} \\
r\cdot J &=p-w+ \l \cdot \bp{0-J}=p-w-\l \cdot J
\end{align*}
There is no maximization on the right-hand-side in this particular example. These equations simply describe the relationship between the equilibrium values $V,J$ and $w$.

\end{document}


\subsection{Sorting}

\begin{center}
\begin{longtable}{|l|l|l|p{7cm}|}
\hline
\textbf{Name} & \textbf{Average complexity} & \textbf{Worst Complexity} & \textbf{Description} \\
\hline
Quick sort & O(nlog(n)) & O(n$^{2}$)
           & \begin{enumerate}
                \item Pick a random pivot value
                \item Partition into items smaller than the value (left) and
                      items larger (right)
                \item Recursively apply this to the left and right subsections
                      until they contain one value
              \end{enumerate}\\
\hline

Merge sort & O(nlog(n)) & O(nlog(n)))
           & \begin{enumerate}
                \item Divide unsorted list into n subsets
                \item Compare each element with adjacent list to sort and merge
                      the two
                \item Recursively do this until only one list is left
              \end{enumerate}\\

\hline

Heap sort & O(nlog(n)) & O(nlog(n))
          & \begin{enumerate}
                \item First build a max heap from the data [O(n) time]
                \item Repeatedly remove the largest element from the heap,
                      it takes log(n) time for each removal

              \end{enumerate}\\
\hline

\end{longtable}
\end{center}


\documentclass{article}

% the purpose of this particular document is to serve as a staging area potential blog posts, for the purpose of editing, revision, and review.

% packages
	\usepackage{amsmath, amsthm, mathtools}
	\usepackage{mdframed}
	\usepackage{geometry, enumerate}
	\usepackage{parskip}
	\usepackage{graphicx}
	\usepackage{dsfont} % for math bf text

% theorems and such
  	\newtheorem{theorem}{Theorem}
  	\newtheorem{corollary}{Corollary}
  	\newtheorem{lemma}[theorem]{Lemma}
  	\newtheorem*{remark}{Remark}
  	\newtheorem*{exe}{Exercise}
  	\newtheorem{prop}{Proposition}
  	\newtheorem{example}{Example}
  	\newtheorem{definition}{Definition}

% custom commands
	\newcommand{\ceil}[1]{\left \lceil #1 \right \rceil}
	\newcommand{\floor}[1]{\lfloor #1 \rfloor}
	\newcommand{\X}[1]{\, \text{mod} \, #1}
	\newcommand{\divv}{\,|\,}
	\newcommand{\ndiv}{\,\not|\,}
	\newcommand{\GCD}[2]{GCD\,(#1, #2)}
	\newcommand{\LCM}[2]{LCM\,(#1, #2)}

\begin{document}

\title{Elementary Number Theory : \S6 }
\author{Henry Slayer $|$ University of California, Santa Cruz}
\date{}
\maketitle

\section*{The Chinese Remainder Theorem}
Often, we will be faced with problems or situations in which it is helpful to break congruence down into a system of congruences. Conversely, it can be useful to assemble a single congruence that uniquely describes a system of smaller congruences. Our tool for doing so is the Chinese Remainder Theorem, which allows us to relate congruence across different modulii. Speaking loosely, if our modulus $m$ can be decomposed into coprime factors, say $d$ and $e$, every congruence modulo $m$ is equivalent to a compound congruence $[a, b]\X{[d, e]}$. The bracket notation means \textit{congruent to \textbf{a} modulo \textbf{d}, and \textbf{b} modulo \textbf{e}}. More formally, we can say the following
\begin{mdframed}
\begin{theorem}[Chinese Remainder Theorem]
If $d$ and $e$ are a pair of coprime numbers, then there exists a bijective correspondence between the set of pairs $[a, b]$, where $0\leq a < d$, and $0\leq b < e$, and the set of integers $n$ for which $0\leq n < de$. \\
In other words, the pairs of residues $[a, b]\X{[d, e]}$ map uniquely to the residues $n\X{(de)}$.
\end{theorem}
\begin{proof}
The sets that we're working with are the same size. So, if we can find an injective map from the set of pairs $[a, b]$ to the set of integers from 0 to $de - 1$, we've found our bijection. So how do we map $n\mapsto [a, b]$? There's a natural choice. \\
Let $n\mapsto [a, b]$ be defined by the rule that $n\mapsto [a, b]$ if and only if $n\equiv[a, b]\X{de}$. \\
We need to show that this map is injective, so suppose that we have a pair of values $0\leq m < de$ and $0\leq n < de$, such that $n\equiv[a, b]\X{de}$ and $m\equiv[a, b]\X{[d, e]}$. Naturally, then, $n - m \equiv [0, 0]\X{[d, e]} $. In turn, this implies that $n - m$ is a multiple of both $d$ and $e$. As $d$ and $e$ are coprime, this forces that $n - m$ is a multiple of the least common multiple of $d$ and $e$. In this case, this means that $n - m$ is a multiple of $de$. Yet, both $m$ and $n$ are strictly less than $de$, so their difference $n - m$ is surely less than $de$. The only multiple of $de$ less than $de$ is 0, so $m - n = 0$, and $m = n$. \\
Having shown that the map is injective, the fact that our two sets (the set of pairs, and the set of residue classes modulo $de$) are the same size implies that the map is bijective. \\
As the map is bijective, this implies that every congruence modulo $de$ corresponds to a unique congruence $[a, b]\X{[d, e]}$, \textbf{provided that d and e are coprime}.
\end{proof}
\end{mdframed}

\subsubsection*{Disassembling and Reassembling Congruence}
The extreme utility of the Chinese Remainder Theorem is that it enables us to reduce complex congruences to systems of simpler congruences, and vice versa.
\begin{mdframed}
\begin{example}
Break $x\equiv 45\X{56}$ into a system of simpler congruences. \\\\
Breaking down congruence couldn't be simpler. All we need to do is find two (or more) coprime factors of 56. 7 and 8 seem like a good choice. $45\equiv 3\X{7}$, and $45\equiv5\X{8}$, so $45\equiv[3, 5]\X{[7, 8]}$. Brilliant.
\end{example}
\end{mdframed}
\begin{mdframed}
\begin{example}
What is $x$, if... \\
x is no more than 100.
Counting by 3, we have one left over. \\
Counting by 22, we have three left over. \\
Counting by 7, we have 4 left over.\\\\
We'll start the problem, and leave the reader to finish it. \\
The statement of the problem implies that $x\equiv 1\X{3}$, $3\X{22}$, and $4\X{7}$. Each one of these linear congruences is really hiding a linear diophantine equation, which gives us the following system of equations:
\begin{align*}
x - 3y &= 1 \\
x - 22z &= 3 \\
x - 7w &= 4
\end{align*}
You do the rest! Hint: use the first two equations to come up with a congruence modulo 66. This can be translated back into the language of linear diophantine equations, and re-combined with the last equation to construct a congruence in terms of 462.
\end{example}
\end{mdframed}
\begin{mdframed}
\begin{example}
Find all solutions to $x^2\equiv 1\X{21}$. \\\\
By the CRT, we can decompose this into two congruences, $x^2\equiv 1\X{7}$ and $x^2\equiv1\X{3}$. Modulo 7, this means that we have two solutions: $x\equiv 6$, or $x\equiv 1$. Modulo 3, we also have two solutions, $x\equiv 1$ or $x\equiv 2$. Two options for each smaller congruence gives us four possible combinations:
\begin{align*}
x\equiv [1, 1]\X{[3, 7]}\\
x\equiv [1, 6]\X{[3, 7]}\\
x\equiv [2, 1]\X{[3, 7]}\\
x\equiv [2, 6]\X{[3, 7]}
\end{align*}
Respectively, these simpler congruences resolve to $x\equiv 1,\, 8,\, 13,\, 20\X{21}$, which characterizes all solutions to $x^2\equiv 1\X{21}$.
\end{example}
\end{mdframed}
The last example here characterizes a useful generalization. Solutions to the congruence $x^2\equiv a\X{de}$ are in one-to-one correspondence with the pairs of solutions $(u, v)$ to the congruences $u^2\equiv a\X{d}$ and $v^2\equiv a\X{e}$. In other words, $a$ is a square mod $de$ (meaning $a\equiv x^2$ for some $x\X{de}$) if and only if $a$ is a square modulo $d$ and modulo $e$. We'll dive into squares (the \textit{quadratic residues}) a bit more further down the road.

\end{document}


\subsection{Example \#6: generate and analyze synthetic data }
\label{S:EXAMPLESYNTHETICDATA}

\subsubsection{Purpose}
This example illustrates how to create a synthetic time series using OpenBDLM.
The objective is to create a 4-years long time series with an acceleration stationary baseline, and a yearly periodic pattern as well as an autoregressive process superimposed into it.
The timestep is 1 day.
This example corresponds to the OpenBDLM demo presented in Section~\ref{S:OPENBDLMGETTINGSTARTED}.

\begin{figure*}[h!]
\centering
\begin{subfigure}{\linewidth}\centering
\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/optim_param_optim_initialhiddenstate/ALL_AMPLITUDES.pdf}
\caption{Amplitude}
\end{subfigure}
\begin{subfigure}{\linewidth}\centering
\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/raw/ALL_TIMESTEPS.pdf}
\caption{Timestep}
\end{subfigure}
\begin{subfigure}{\linewidth}\centering
\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/raw/AVAILABILITY.pdf}
\caption{Availability}
\end{subfigure}
\caption{Data used in example \#5}.
\label{fig:DataSummaryRawSynthetic}
\end{figure*}

\subsubsection{Model description}
The model includes one model class, and the block components are
\begin{gather*}
\textbf{x}=[x^{\mathtt{L}}, x^{\mathtt{T}}, x^{\mathtt{LA}}, x^{\mathtt{P1}\text{,yearly}}, x^{\mathtt{P2}\text{,yearly}}, x^{\mathtt{AR}}].
\end{gather*}
The associated model parameters are
\begin{gather*}
\bm\theta=[\sigma_{w}^{\mathtt{LA}}, p^{\mathtt{PD}, \text{yearly}}, \sigma_{w}^{\mathtt{PD}, \text{yearly}}, \phi^{\mathtt{AR}}, \sigma_{w}^{\mathtt{AR}}, \sigma_{v,\text{D}}].
 \end{gather*}
The model parameters values assigned by default from OpenBDLM are
\begin{gather*}
\bm\theta^{\text{defaut}}=[ 1\times10^{-8}, 365.24, 0, 0.75, 1, 0.01].
\end{gather*}
The default initial hidden states mean, covariance, and model probability are
\begin{align*}
\bm \mu^{\text{defaut}}_{0} & = [	 10  , -1\times10^{-5}  ,	-0.001	,	10  ,  	10    ,	0  ]^{\intercal}, \text{and} \\
\bm\Sigma^{\text{defaut}}_{0} & = \text{diag}[ 0.01  ,	0.01  ,	0.01  	,0.04  ,	0.04  ,	0.01 ], \\
 \pi_{0}^{1,\text{default}} & = 1.
 \end{align*}
The synthetic data generated from this model structure, model parameters values and initial hidden states values are presented in Figure~\ref{fig:DataSummaryRawSynthetic}.
The hidden states computed using the same (i.e. true) model structure, model parameters values and initial hidden states values are presented in Figure~\ref{fig:SYNTHETICOptimizedOptimized}.

\subsubsection{Run the example from command line interaction}

This section explains how to run the example \#6, that is, how to generate the synthetic data presented in Figure~\ref{fig:DataSummaryRawSynthetic}, and estimate the hidden states as presented in Figure~\ref{fig:SYNTHETICOptimizedOptimized}.

\begin{enumerate}
\item Start OpenBDLM. Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!OpenBDLM_main;!}.
\item Choose the interactive tool. Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!0!}.
\item Enter the project name. Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!Example_SYNTHETIC!}.
\item Generate synthetic data. Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!yes!}.
\item Provide the number of time series. Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!1!}.
\item Provide  the date corresponding of the first data sample. Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!2000-01-01!}.
\item Provide  the date corresponding of the last data sample. Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!2005-01-01!}.
\item Provide  the timestep in day. Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!1!}.
\item Select the number of model classes. Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!1!}.
\item Select the model block components. Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]![13 31 41]!}. The Figure~\ref{fig:DataSummaryRawSynthetic} should popup on screen.
\item Access hidden states estimation menu. Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!3!}.
\item Estimate the smoothed hidden states. Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!2!}. The estimation should correspond to the results presented in Figure~\ref{fig:SYNTHETICOptimizedOptimized}.
\item Save and quit OpenBDLM. Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!Q!}.
\end{enumerate}

%First, choose the interactive tool by typing \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!0!}.
%Secondly, provide a project name (i.e. \lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!Example_SYNTHETIC!).
%Then, answer \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!yes!} to indicate that you would like to create synthetic data.
%Finally, type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!1!} to create one single synthetic time series.
%
%\subsection{Step 2: define the time step vector}
%
%Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!2000-01-01!}, \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!2005-01-01!} and \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!1!}, to provide the date corresponding of the first and last data sample of the synthetic data, as well as the timestep in days.

%\subsection{Step 3: configure the model}
%
%First, the program requests the number of model class.
%In this example, we would like to create synthetic data with no anomaly.
%In such case, we type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!1!} to choose a single model class.
%Then, OpenBDLM asks for the type of block component.
%As mentionned earlier, the objective is to create synthetic time series data with an acceleration stationary baseline, and a yearly periodic pattern superimposed into it.
%%The presence of a daily periodic pattern is unclear.
%Therefore, we choose \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]![13 31 41]!}.
%This model considers a trend stationary model with a yearly periodic pattern, and an autoregressive process to be more realistic.
%At this time, three figures  that represent the amplitude, timestep and availability of the newly created synthetic data (as shown in Figure~\ref{fig:DataSummaryRawSynthetic}) should popup on the screen.
%Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!Q!} to save and quit.
%
%\subsection{Step 4: explore the model}
%From the main menu, type  \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!11!} to see what are the values of model parameters which have been assigned to create the synthetic data.
%The model totalizes 6 model parameters, that is
%\begin{gather*}
%\bm\theta=\{\sigma_{w, \text{D}}^{LA}, p^{\text{PD1}}_{\text{D}}, \sigma_{w,\text{D}}^{\text{PD1}}, \phi^{AR}_{\text{D}}, \sigma_{w,\text{D}}^{AR}, \sigma_{v,\text{D}}\}
% \end{gather*}
%The default model parameters values are
%\begin{gather*}
%\bm\theta^{\text{default}}=\{1\times10^{-8}, 365.24, 0, 0.75, 0.01, 0.01\},
%\end{gather*}
%in agreement with the values indicated in Table~\ref{table:defaultsynthetic}.
%Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!R!} to return to the main menu.
%Then, type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!12!} the see that the default initial hidden states mean, covariance  and model probability values are
%\begin{align*}
% \bm \mu^{1,\text{default}}_{0} & = [	10  , -1\times10^{-5}  ,	-0.001	,	10  ,  	10    ,	0         ]^{\intercal}, \text{and} \\
% \text{diag}(\bm\Sigma^{1,\text{default}}_{0})  & = [	0.01  ,	0.01  ,	0.01  	,0.04  ,	0.04  ,	0.01     ], \text{and} \\
% \pi_{0}^{1,\text{default}} & = 1.
%\end{align*}
%respectively, in agreement with the values indicated in Table~\ref{table:defaultsynthetic}.
%Type \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!R!} to return to the main menu.

%\begin{figure*}[h!]
%\centering
%\begin{subfigure}{\linewidth}
%\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/raw/ALL_AMPLITUDES.pdf}
%\caption{Amplitude}
%\end{subfigure}
%\begin{subfigure}{\linewidth}
%\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/raw/ALL_TIMESTEPS.pdf}
%\caption{Timestep}
%\end{subfigure}
%\begin{subfigure}{\linewidth}
%\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/raw/AVAILABILITY.pdf}
%\caption{Availability}
%\end{subfigure}
%\caption{Data used in example \#4}.
%\label{fig:DataSummaryRawSynthetic}
%\end{figure*}

%\subsection{Step 5: estimate the hidden states}
%
%From the main menu, type  \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!3!}, then \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!1!} to estimate the filtered hidden states using the default model parameters and default initial hidden states values.
%The value of the log-likelihood is $4976$, and the estimated hidden states are presented in Figure~\ref{fig:SYNTHETICDefaultDefaultExample4}.
%For each figure, the red dashed line represents the true known values of the hidden states.
%
%\begin{figure*}[h!]
%\centering
%\begin{subfigure}{\linewidth}
%\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/default/TS01_ObservedPredicted.pdf}
%\caption{Observed and estimated displacement data}
%\end{subfigure}
%\begin{subfigure}{\linewidth}
%\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/default/TS01_LL_1.pdf}
%\caption{Estimated displacement local level component}
%\end{subfigure}
%\begin{subfigure}{\linewidth}
%\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/default/TS01_LT_2.pdf}
%\caption{Estimated displacement local trend component.}
%\end{subfigure}
%\begin{subfigure}{\linewidth}
%\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/default/TS01_LA_3.pdf}
%\caption{Estimated displacement local acceleration component.}
%\end{subfigure}
%\end{figure*}
%\begin{figure*}[h!]
%\ContinuedFloat
%\begin{subfigure}{\linewidth}
%\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/default/TS01_S1_4.pdf}
%\caption{Estimated displacement yearly periodic component (first hidden state)}
%\end{subfigure}
%\begin{subfigure}{\linewidth}
%\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/default/TS01_AR_6.pdf}
%\caption{Estimated displacement autoregressive component}
%\end{subfigure}
%\caption{Estimated results using OpenBDLM default model parameters and default initial hidden states. The hidden states are estimated from the data presented in Figure~\ref{fig:DataSummaryRawSynthetic}a. The solid line and shaded area represent the mean and standard deviation of the estimated hidden states, respectively. The red dashed line represent the true the hidden state value.}
%\label{fig:SYNTHETICDefaultDefaultExample4}
%\end{figure*}

%\subsection{Step 6: estimate the initial hidden states}
%
%From the main menu, type  \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!2!}, to optimize the initial hidden states value.
%The estimated initial hidden states mean and covariance values are
%\begin{align*}
%\bm \mu^{*}_{0} & = [	10 ,   	-0.00103,	-9.68\times10^{-6},	10   , 	10    ,	-0.0106  ]^{\intercal}, \text{and} \\
% \text{diag}(\bm\Sigma^{*}_{0}) & = [	2.35\times10^{-5}	, 1.33\times10^{-9},	3.3\times10^{-14}	, 1.9\times10^{-6}	, 2.03\times10^{-6}	,0.000353    ],
% \end{align*}
% respectively.
%Once it is done, type  \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!3!}, and then  \colorbox{light-gray}{\lstinline[basicstyle = \mlttfamily \small, backgroundcolor = \color{light-gray}]!1!} to compute the filtered hidden states using the optimized model parameters and optimized initial hidden states.
%The value of the log-likelihood is $5011$.
%The estimated hidden states are presented in Figure~\ref{fig:SYNTHETICOptimizedOptimized}.

\begin{figure*}[h!]
\centering
\begin{subfigure}{\linewidth}
\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/optim_param_optim_initialhiddenstate/TS01_ObservedPredicted.pdf}
\caption{Observed and estimated displacement data}
\end{subfigure}
\begin{subfigure}{\linewidth}
\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/optim_param_optim_initialhiddenstate/TS01_LL_1.pdf}
\caption{Estimated displacement level component}
\end{subfigure}
\begin{subfigure}{\linewidth}
\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/optim_param_optim_initialhiddenstate/TS01_LT_2.pdf}
\caption{Estimated displacement trend component.}
\end{subfigure}
\begin{subfigure}{\linewidth}
\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/optim_param_optim_initialhiddenstate/TS01_LA_3.pdf}
\caption{Estimated displacement local acceleration component.}
\end{subfigure}
\end{figure*}
\begin{figure*}[h!]
\ContinuedFloat
\begin{subfigure}{\linewidth}
\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/optim_param_optim_initialhiddenstate/TS01_S1_4.pdf}
\caption{Estimated displacement yearly periodic component (first hidden state)}
\end{subfigure}
\begin{subfigure}{\linewidth}
\includegraphics[width=0.9\linewidth]{./docfigs/Example_SYNTHETIC/optim_param_optim_initialhiddenstate/TS01_AR_6.pdf}
\caption{Estimated displacement autoregressive component}
\end{subfigure}
\caption{Estimated results using OpenBDLM with the default model parameters and optimized initial hidden states. The hidden states are estimated from the data presented in Figure~\ref{fig:DataSummaryRawSynthetic}a. The solid line and shaded area represent the mean and standard deviation of the estimated hidden states, respectively. The red dashed line represent the true the hidden state value.}
\label{fig:SYNTHETICOptimizedOptimized}
\end{figure*}


\documentclass{article}
\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\allowdisplaybreaks

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

\begin{document}
\title{Notes}
\date{February 10, 2014}
\maketitle
\section*{Lesson 10}
\subsection*{use of integral transforms}
\begin{align*}
  \intertext{sine or fourier transform?}
  \mathcal{F}_s[f]&=\frac{2}{\pi}\int_0^\infty{f(t)\sin(\omega t)\,\mathrm{d}t}=F(\omega)\\
  {\mathcal{F}_s}^{-1}[F]&=\int_0^\infty{F(\omega)\sin(\omega t)\,\mathrm{d}\omega}=f(t)\\
  \intertext{Laplace}
  \mathcal{L}[f]&=F(s)=\int_0^\infty{e^{-st}f(t)\,\mathrm{d}t}
\end{align*}
Start with a problem in terms of (say) time t. Original solutioon $f(t)$. Transform it to an equation (in s). Solve for $F(s)$. Inversion $F(s)\to f(t)$. Simplest way of inverstion: Use a table of transforms.
\begin{align*}
  &\text{Laplace}&&f(t)&&F(s)\\
  &&&t^p&&\frac{T(p+1)}{S^{p+}}\\
  &&&\cos(at)&&\frac{s}{s^2+a^2}\\
  &&&\vdots&&\vdots\\
\end{align*}
\subsubsection*{note}
when you first see laplace transforms you do not see an inversion formula written down. This is because it turns out that the inversion formula requires complex analysis. Even to write down.
\[f(t)=\frac{1}{2\pi i}\int_{c-i\infty}^{c+i\infty}{F(s)e^t\,\mathrm{d}s}\]
This is covered in chapter 13.
\emph{extra credit for using this inversion formula. Talk to him about it}
\subsection*{some properties of sine transform}
\begin{align*}
  \mathcal{F}_s[f']&=\frac{2}{\pi}\int_0^\infty{f'(t)\sin(\omega t)\,\mathrm{d}t}\\
  &=\frac{2}{\pi}\left(\left.f(t)\sin(\omega t)\right|_0^\infty-\int_0^\infty{f(t)\cdot \omega\cos(\omega t)\,\mathrm{d}t}\right)\\
  &=\frac{2}{\pi}\left(0-0-\omega\int_0^\infty{f(t)\cdot \cos(\omega t)\,\mathrm{d}t}\right)\\
  &=-\omega\mathcal{F}_c[f]\\
  \mathcal{F}_s[f'']&=-\omega\mathcal{F}_c[f']=-\omega^2\mathcal{F}_s[f]-\frac{2\omega}{\pi}f(0)\\
  \mathcal{F}_c[f']&=+\omega\mathcal{F}_s[f]-\frac{2}{\pi}f(0)
\end{align*}
\subsection*{example p. 77}
\begin{align*}
  \text{PDE}&&u_t&=\alpha^2u_{xx}&0&<x<\infty &0&<t<\infty\\
  \text{BC}&&u(0,t)&=A&&&0&<t<\infty\\
  \text{IC}&&u(x,0)&=0&0&<x<\infty
\end{align*}
indefinite length rod. starts at zero temp.

use integral transforms to solve this. specifically the sine transform.
\begin{align*}
  \mathcal{F}_s[f]&=\frac{2}{\pi}\int_0^\infty{f(t)\sin(\omega t)\,\mathrm{d}t}=F(\omega)\\
  \mathcal{F}_s[u_t]&=\alpha^2\mathcal{F}_s[u_{xx}]\\
  &=\alpha^2(-\omega^2U(\omega,t)+\frac{2}{\pi}\omega Au(0.t))\\
  \intertext{note:}
  \frac{2}{\pi}\int_0^\infty{\sin(\omega x)\frac{\partial u}{\partial t}(x,t)\,\mathrm{d}x}&=\frac{\partial}{\partial t}\left[\frac{2}{\pi}\int_0^\infty{]sin(\omega x)u(x,t)\,\mathrm{d}x}\right]\\
  U(\omega,t)&=C(\omega)e^{-\alpha^2\omega^2t}+\frac{2}{\pi}\frac{A}{\omega}\\
  \intertext{as $t\to0^+$}
  U(\omega,t)&\to\frac{2}{\pi}\int_0^\infty{\sin(\omega t)\cdot0\,\mathrm{d}t}\text{ initial condition}\\
  0&=C(\omega)+\frac{2}{\pi}\frac{A}{\omega}\\
  U(\omega,t)&=\frac{2}{\pi}\frac{A}{\omega}\left(1-e^{-\alpha^2\omega^2t}\right)\text{ sine transform of the solution $u(x,t)$}
\end{align*}
\end{document}


\subsection{Curvature tensor}


\documentclass[12pt]{article}

\usepackage[margin=0.3in]{geometry}
\usepackage{amssymb}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{float}
\usepackage{hyperref}
\usepackage{pgfplots}
\usepackage{enumitem}
\usepackage{bm}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{titling}
\newtheorem{theorem}{Theorem}
\newtheorem{definition}{Definition}
\newcommand{\subtitle}[1]{\posttitle{\par\end{center}\begin{center}\large#1\end{center}\vskip0.5em}}
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.4,0.4,0.4}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.91,0.91,0.91}
\lstdefinestyle{mystyle}{backgroundcolor=\color{backcolour}, commentstyle=\color{codegreen}, keywordstyle=\color{magenta}, numberstyle=\footnotesize\color{codegray}, stringstyle=\color{codepurple}, basicstyle=\ttfamily\fontsize{12}{12}, breakatwhitespace=false, breaklines=true, captionpos=b, keepspaces=true, numbers=left, numbersep=5pt, showspaces=false, showstringspaces=false, showtabs=false, tabsize=2}
\lstset{style=mystyle}
\setlength\parindent{0pt}

\newcommand{\code}[1]{\colorbox{backcolour}{\texttt{#1}}}

\setlength{\abovecaptionskip}{2pt plus 3pt minus 2pt}

\begin{document}

\title{CTA200H Assignment 2}
\author{Anatoly Zavyalov}
\date{\today}
\maketitle

\section*{Question 1}

\begin{figure}[H]
    \begin{center}
        \includegraphics[scale=0.8]{img_q1.pdf}
    \end{center}
    \caption{Comparison of errors of two ways of estimating derivative on a loglog plot, based on varying stepsize.}
\end{figure}

\subsection*{Methods}

We first wrote Python functions to represent the two ways of approximating the derivative, and then created a function to determine the error between the numerical and analytical ways of finding the derivative. We then iterated through different values of stepsize \(h\) (with a certain step between the values of the stepsize), and plotted the values on a loglog plot using the \code{matplotlib} Python module.

\subsection*{Analysis}

We see that the absolute error of the two methods of approximations have the same approximate innacuracy when the stepsize is high, but the second method of approximation is more accurate by orders of magnitude when the stepsize is decreased. The slope of the error vs. stepsize plot represents how rapidly the method becomes more innacurate as the stepsize is increased.

\newpage

\section*{Question 2}

\begin{figure}[H]
    \begin{center}
        \includegraphics[scale=0.55]{img_q2_1.pdf}
        \includegraphics[scale=0.55]{img_q2_2.pdf}
        \caption{Left: Mandelbrot set with two colors; Right: Mandelbrot set with coloring based on number of iterations before diverging.}
    \end{center}
\end{figure}

\subsection*{Methods}

In both illustrations, for \(c \in \mathbb{C}\), we developed a function to check the number of iterations of the function \(z(n) = z(n-1)^2 + c\) with \(z(0) = 0\) before \(z(n)\) was greater than some threshold, up to a maximum number of iterations. This function was vectorized using \code{numpy}'s \code{vectorize} method, and was then applied to a \code{numpy.meshgrid} in order to get a two-dimensional mapping of the function. Then, \code{matplotlib}'s \code{imshow} method was used to display the graphics. For the illustration that required two colors, the function just returned \code{1.0} if the point was bounded, and \code{0.0} otherwise, instead of returning the number of iterations, thus resulting in a two-colored output.

\subsection*{Analysis}

We see that the output is the famous Mandelbrot set! This is a fractal (as can be seen by increasing resolution and zooming in on the \code{matplotlib} output in the Jupyter notebook), and a very cool one at that. We see that the large middle region is completely bounded upon repeatedly applying the function, and it ``branches out'' into a fractal when heading toward regions that become unbounded.

\newpage

\section*{Question 3}

\begin{figure}[H]
    \begin{center}
        \includegraphics[scale=0.55]{img_q3_1.pdf}
        \includegraphics[scale=0.55]{img_q3_2.pdf}
    \end{center}
    \caption{Left: SIR Model with different values of $\beta$, $\gamma$. Right: SIRD Model with different values of $\beta$, $\gamma$, $\kappa$.}
\end{figure}

\section*{Methods}

Similar to lecture, we create a function to return the right hand of the ODEs (that have the derivative in terms of time on the left hand side). We then use \code{scipy}'s \code{integrate.ode} function to numerically integrate the ODEs, then convert the data into \code{numpy} arrays, and finally draw the plots using \code{matplotlib}.

For the SIRD model, we added a death parameter and death coefficient \(\kappa\), which represents how much of the infected population dies every unit of time due to the disease. We then modify our code from the SIR model to accept this fourth parameter, and recreate the graphs.

\section*{Analysis}

We see that, for the SIR model, when \(\beta > \gamma\), the size of the infected population rises in a single wave before slowly dropping, with the size of the recovered population increasing shortly after, and with the size of the susceptible population dropping quite rapidly. This makes sense, as if the recovery rate is lower than the infection rate, the disease will have the chance to infect some people before the infected population recovers. However, when \(\gamma > \beta\) (such as in the bottom left), the chance of recovery of an infected person is greater than the possibility of a susceptible person becoming infected, hence almost no one gets infected.

\medspace

For the SIRD model, we see that if $\kappa \geq \beta$ (such as in the top left example), then it is more likely for infected people to die than it is for susceptible people to get infected. Hence, the disease basically does nothing, the small infected population dies, and that's the end of it. We also see that, in the bottom right example, if the infection coefficient is high, recovery coefficient is low, and death coefficient is relatively low (so disease can kill people slowly enough without just completely killing every infected person quickly), we have quite a deadly pandemic on our hands.

\end{document}


\section{Language and axiom system}

\begin{defn} (Basic vocabulary)

	\begin{itemize}
		\item $P, Q, P\p, Q\p, \dots$ (\textit{relation symbols});
		\item $x, y, z, \dots$ (\textit{individual variables});
		\item $\impli, \bot$ (\textit{boolean connectives});
		\item $\todo$ (\textit{universal quantifier});
		\item $p_{0}, p_{1}, p_{2}, \dots$(\textit{justification variables});
		\item $c_{0}, c_{1}, c_{2}, \dots$ (\textit{justification constants});
		\item $+$, $\cdot$, $!$, $?$, $gen_{x}$ (\textit{justification operators - for every individual variable $x$, there is an operator $gen_{x}$});
		\item $(\cdot):_{X} (\cdot)$,(for every finite set of individual variables $X$);
		\item $),($ (\textit{parentheses}).
	\end{itemize}
\end{defn}

\begin{defn} (Justification terms)
\begin{center}
$ t : = p_{i}$   $|$ $c$ $|$  $(t_{1} \cdot t_{2})$ $|$ $(t_{1} + t_{2})$ $|$  $!t$ $|$ $?t$ $|$ $gen_{x}(t)$
\end{center}
\end{defn}

\begin{defn} (Justification formulas)
\begin{center}
$ \varphi : = Q(x_1, \dots, x_n)$   $|$ $\bot$ $|$  $\varphi \impli \psi$ $|$ $\todo x \varphi$ $|$  $t$$:_{X}$$\varphi$
\end{center}
\end{defn}

\qquad The set of all formulas is denoted by $L$. We are assuming that the set of relational symbols, individual variables, justification variables and justification constants are all countable sets. Thus, it is easy to check that $L$ itself is a countable set.

\begin{defn}
We define the notion of free variables of $\varphi$, $fv(\varphi)$, inductively as follows:

\begin{itemize}
	\item If $\varphi$ is atomic, then $fv(\varphi)$ is the set of all variables occurring in $\varphi$.
	\item If $\varphi$ is $(\psi \impli \theta)$, then $fv(\varphi)$ is $fv(\psi) \cup fv(\theta)$.
	\item If $\varphi$ is $\todo x \psi$, then $fv(\varphi)$ is $fv(\psi) \backslash \{x\}$.
	\item If $\varphi$ is $t$$:_{X}$$\psi$, then  $fv(\varphi)$ is $X$.
\end{itemize}

\qquad Similarly as in the classical case, we must define the notion of an individual variable $y$ being free for $x$ in the formula $\varphi$. The definition is the same as in the classical case, we only add the following clause: $y$ is free for $x$ in $t$$:_{X}$$\varphi$ if two conditions are met, i) $y$ is free for $x$ in $\varphi$, ii) if $y \in fv(\varphi)$, then $y \in X$.
\end{defn}

\qquad We write $\varphi(x_{1}, \dots, x_{n})$ to denote that the free variables of $\varphi$ are among $\{x_{1}, \dots, x_{n}\}$. Let $y_{1}, \dots, y_{n}$ be variables, we write $\varphi(y_{1}/x_{1}, \dots, y_{n}/x_{n})$ to denote the formula obtained by substitution of $y_{1}, \dots, y_{n}$ for all the free occurrences of $x_{1}, \dots, x_{n}$ in $\varphi$,  respectively. When it is clear from the context which variables are free in $\varphi$ we simply write $\varphi(y_{1}, \dots, y_{n})$ instead of $\varphi(y_{1}/x_{1}, \dots, y_{n}/x_{n})$. We use $\vec{x},\vec{y}, \dots$ for sequence of variables; and we write $\todo \vec{x} \varphi(\vec{x})$ in the place of $\todo x_1, \dots ,\todo x_n\varphi(x_1, \dots ,x_n)$

\qquad We write $Xy$ instead of $X \cup \{y\}$, in this case it is assumed that $y \notin X$. And we use $t$$:$$\varphi$ as an abbreviation for $t$$:_{\vazio}$$\varphi$

\qquad  The first-order JT45, FOJT45, is axiomatized by the following axiom schemes and inference rules:\\

\textbf{A1} classical axioms of first-order logic\\

\textbf{A2} $t$$:_{Xy}$$\varphi \impli$ $t$$:_{X}$$\varphi$, provided $y$ does not occur free in $\varphi$\\

\textbf{A3} $t$$:_{X}$$\varphi \impli$ $t$$:_{Xy}$$\varphi$ \\

\textbf{B1} $t$$:_{X}$$\varphi \impli \varphi$\\

\textbf{B2} $t$$:_{X}$$(\varphi \impli \psi) \impli$ $(s$$:_{X}$$\varphi \impli$ $[t\cdot s]$$:_{X}$$\psi)$\\

\textbf{B3} $t$$:_{X}$$\varphi \impli$ $[t+s]$$:_{X}$$\varphi$, $s$$:_{X}$$\varphi \impli$ $[t+s]$$:_{X}$$\varphi$\\

\textbf{B4} $t$$:_{X}$$\varphi \impli$ $!t$$:_{X}$$t$$:_{X}$$\varphi$\\

\textbf{B5} $\nao t$$:_{X}$$\varphi \impli$ $?t$$:_{X}$$\nao t$$:_{X}$$\varphi$\\

\textbf{B6} $t$$:_{X}$$\varphi \impli$ $gen_{x}(t)$$:_{X}$$ \todo x \varphi$, provided $x \notin X$\\

\textbf{R1} (\textit{Modus Ponens}) $\teo \varphi$, $\teo \varphi\impli\psi$ $\Rightarrow$ $\teo \psi$ \\

\textbf{R2} (\textit{generalization})  $\teo \varphi$ $\Rightarrow$ $\teo \todo x \varphi$ \\

\textbf{R3} (\textit{axiom necessitation})  $\teo c$$:$$\varphi$, where $\varphi$ is an axiom and $c$ is a justification constant.\\

\begin{defn}
Let $\C$ be a constant specification. We say that $\C$ is \textit{schematic} if all instances of an axiom scheme are assigned the same constants.
\end{defn}

\qquad We use $\Gamma, \Delta, \Theta, \dots$ as variables for sets of formulas. The notion of $\Gamma \teo \varphi$ is define as usual. The only thing that should be noted is that, if $\Gamma$ deduces $\varphi$ using the generalization rule, then this rule was not applied to a variable which occur free in the formulas of $\Gamma$.

\qquad Since derivations depends on the constant specification being considered, we sometimes write $\teo_{\C} \varphi$ to point out that the proof of $\varphi$ meets the constant specification $\C$.

\begin{defn}
A substitution $\sigma$ is a mapping from the set of justification variables to the set of justification terms. For a justification term $t$ the result of applying a substitution $\sigma$ is denoted $t\sigma$; similarly, for a formula $\varphi$ we write $\varphi\sigma$.
\end{defn}

\begin{lema}
(\textit{Substitution}) Let $\varphi$ be a formula, $\sigma$ a substitution and $\C$ a schematic constant specification. If  $\teo_{\C} \varphi$, then  $\teo_{\C} \varphi\sigma$.
\end{lema}

\begin{lema}
(\textit{Deduction})  $\Gamma,\varphi \teo \psi$ iff  $\Gamma \teo \varphi \impli \psi$
\end{lema}

\begin{teor}
	(\textit{Internalization}) Let $\C$ be an axiomatic appropriate constant specification; $p_{0}, \dots, p_{k}$ be justification variables; $X_{0}, \dots, X_{k}$ be finite sets of individual variables, and $X =X_{0} \cup \dots \cup X_{k}$. In these conditions, if  $p_{0}$$:_{X_{0}}$$\varphi_{0}, \dots, p_{k}$$:_{X_{k}}$$\varphi_{k} \teo_{\C} \psi$, then there is a justification term $t(p_{0}, \dots, p_{k})$ such that

	\begin{center}
		$p_{0}$$:_{X_{0}}$$\varphi_{0}, \dots,
		p_{k}$$:_{X_{k}}$$\varphi_{k} \teo_{\C} t$$:_{X}$$\psi$.
	\end{center}

\end{teor}

\begin{proof}
The same proof as presented in \cite[p. 7]{Artemov11}.
\end{proof}

\pagebreak

\begin{pro}
(\textit{Explicit counterpart of the Barcan Formula and its converse}) For every formula $\varphi(x)$ and every justification term $t$, there are justification terms $CB(t)$ and $B(t)$ such that:
\begin{center}
$\teo t$$:$$\todo x \varphi(x) \impli \todo x CB(t)$$:_{\{x\}}$$\varphi(x)$\\

$\teo \todo x t$$:_{\{x\}}$$\varphi(x) \impli B(t)$$:$$\todo x \varphi(x)$
\end{center}
\end{pro}

\begin{proof}
\qquad In Appendix.
\end{proof}

\begin{pro}
AAAA For every formula $\varphi(x)$ and every justification term $t$, there are justification terms $CB(t)$ and $B(t)$ such that:
	\begin{center}
		$\teo t$$:$$\todo x \varphi(x) \impli \todo x CB(t)$$:_{\{x\}}$$\varphi(x)$\\

		$\teo \todo x t$$:_{\{x\}}$$\varphi(x) \impli B(t)$$:$$\todo x \varphi(x)$
	\end{center}
\end{pro}

\begin{proof}
	\qquad In Appendix.
\end{proof}

\section{Semantics: basic definitions}

\begin{defn}
An S5 \textit{skeleton} is a structure $\bl \W, \R, \D \br$ where: $\W \ne \vazio$; $\R \subseteq \W \times \W$ such that $\R$ is an equivalence relation, and $\D \ne \vazio$.
\end{defn}

\qquad For any non-empty set $\D$ we are going to use the elements of $\D$ as constants. And  we are going to use $\vec{a}, \vec{b},  \dots$ to denote sequences of constants.

\begin{defn}
Let $\D$ be a non-empty set. The set of all $\D$-formulas, $L_{\D}$, is defined as follows:

\begin{center}
	$L_{\D} = \{\varphi (\vec{a})$ $|$  $\varphi(\vec{x}) \in L$ and $\vec{a} \in \D\}$
\end{center}

\end{defn}

\qquad As usual, for a $\D$-formula $\varphi$, we say that $\varphi$ is closed if  $\varphi$ has no free-variables.

\begin{defn}
A \textit{Fitting model} is a structure $M = \model$ where $\bl \W, \R, \D \br$ is an S5 skeleton; and:

\begin{itemize}
\item $\I$ is an \textit{interpretation function}, i.e.,  $\I$ is a function assigning to each $n$-ary relational symbol $Q$ and each $w \in \W$ an $n$-ary relation $\I(Q,w)$ on $\D$.

\item $\E$ is an \textit{evidence function}, i.e., for any justification term $t$ and $\D$-formula $\varphi$, $\E(t,\varphi) \subseteq \W$.
\end{itemize}

\end{defn}

\begin{defn}
\textit{Evidence Function Conditions}. Let $M = \model$ be a Fitting model. We require the evidence function to meet the following conditions:

\begin{itemize}
	\item[] \textbf{$\cdot$ Condition} $\E (t, \varphi \impli \psi) \cap \E(s, \varphi) \subseteq \E([t\cdot s], \psi).$
	\item[] \textbf{$+$ Condition} $\E (s, \varphi) \cup \E(t, \varphi) \subseteq \E([s+t], \varphi).$
	\item[] \textbf{$!$ Condition} $\E (t, \varphi) \subseteq \E(!t, t$$:_{X}\varphi)$, where $X$ is the set of constant occurring in $\varphi$.
    \item[] \textbf{$?$ Condition} $\W  \backslash \E (t, \varphi) \subseteq \E(?t,\nao t$$:_{X}\varphi)$, where $X$ is the set of constant occurring in $\varphi$.
	\item[] \textbf{$\R$ Closure Condition} If $w \in \E (t, \varphi)$ and $w \R w\p$, then $w\p \in \E (t, \varphi)$.
	\item[] \textbf{Instantiation Condition} If $w \in \E (t, \varphi(x))$ and $a \in \D$, then $w \in \E (t, \varphi(a))$.
	\item[] \textbf{$gen_{x}$ Condition} $\E (t, \varphi) \subseteq \E(gen_{x}(t),\todo x\varphi)$.
\end{itemize}
\end{defn}

\qquad We say that a model $M = \model$ \textit{meets constant specification $\C$} whenever $c$$:$$\varphi \in \C$, then $\E (c, \varphi) = \W$.

\begin{defn}
Let $M = \model$ be a Fitting model, $\varphi$ a closed $\D$-formula and $w \in \W$. The notion that \textit{$\varphi$ is true at world $w$ of $M$}, in symbols $M,w \models \varphi$, is defined as usual by induction on $\varphi$:
\begin{itemize}
	\item $M,w \models Q(\vec{a})$ iff $\bl \vec{a}\br \in \I(Q,w)$.
	\item $M,w \nmodels \bot$.
	\item $M,w \models \psi \impli \theta$ iff $\M,w \nmodels \psi$ or $M,w \models \theta$.
	\item $M,w \models \todo x \psi(x)$ iff for every $a \in \D$, $M,w \models \psi(a)$.

\pagebreak

	\item Assume $t$$:_{X}$$\psi(\vec{x})$ is closed and $\vec{x}$ are all the free variables of $\psi$. Then, $M,w \models t$$:_{X}$$\psi(\vec{x})$ iff
	\begin{enumerate}[(a)]
		\item $w \in \E (t, \psi(\vec{x}))$ and
		\item for every $w\p \in \W$ such that $w\R w\p$, $\M,w\p \models \psi(\vec{a})$ for every $\vec{a} \in \D$.
	\end{enumerate}

\end{itemize}

\end{defn}

\begin{defn}
Let $\varphi \in L$ be a closed formula. We say that $\varphi$ is \textit{valid in the Fitting model} $M = \model$ provided for every $w \in W$, $M,w \models \varphi$. A formula with free individual variables is valid if its universal closure is valid.
\end{defn}

\begin{defn}
A \textit{Fitting model for FOJT45} is a Fitting model $M = \model$ where $\E$ is a \textit{strong evidence function}, i.e., for every term $t$ and $\D$-formula $\varphi$, $\E(t,\varphi) \subseteq \{w \in \W$ $|$ $ M,w \models t$$:_{X}$$\varphi\}$ where $X$ is the set of constant occurring in $\varphi$.

\qquad For a formula $\varphi$ and constant specification $\C$, we write $\models_{\C}\varphi$ if for every Fitting model for FOJT45 $M$ meeting $\C$, $\varphi$ is valid in $M$.
\end{defn}

\section{Semantics: non-validity}

\qquad Before we deal with soundness and completeness, it is useful to know some examples of non-validity in order to see that the provisions of some axioms make sense. There is only a minor problem, we require that Fitting models for FOJT45 have a strong evidence function, and it is not so easy to construct models with that property. The following proposition helps us to circumnavigate this issue.

\begin{pro}
If $M = \model$ is a Fitting model such that for every justification term $t$ and D-formula $\varphi$, $\E(t,\varphi) = \W$, then there is a Fitting model for FOJT45 $M^{*} = \bl\W,\R,\D,\I,\E^{*} \br$ such that for every $w \in \W$ and every formula $\varphi$, $M,w \models \varphi$ iff $M^{*},w \models \varphi$.
\end{pro}

\begin{proof}
\qquad Let $M^{*} =\bl\W,\R,\D,\I,\E^{*} \br$ where for every justification term and D-formula $\varphi$,

\begin{center}
$\E^{*}(t,\varphi) = \{w \in \W$ $|$ $ M,w \models t$$:_{X}$$\varphi\}$
\end{center}

where $X$ is the set of constants occurring in $\varphi$.

\qquad It is straightforward to check that $M^{*}$ is indeed a Fitting model. Now consider the following:\\

 (*) For every $w \in \W$ and every closed D-formula $\varphi$, $M,w \models \varphi$ iff $M^{*},w \models \varphi$.\\

 (Proof of (*)) Induction on the complexity of $\varphi$. Crucial case, $\varphi = t$$:_{X}$$\psi$. For simplicity, let's assume that $\varphi$ is $t$$:_{\{a\}}$$\psi(a,y)$.

\qquad ($\Rightarrow$) If $M,w \models t$$:_{\{a\}}$$\psi(a,y)$, then by definition $w \in \E^{*}(t, \psi(a,y))$ and for every $w\p \in \W$, if $w\R w\p$, then $M,w\p \models \psi(a,b)$ for every $b \in \D$. By the induction hypothesis, for every $w\p \in \W$, if $w\R w\p$, then $M^{*},w\p \models \psi(a,b)$ for every $b \in \D$. Thus, $M^{*},w \models t$$:_{\{a\}}$$\psi(a,y)$.

\qquad ($\Leftarrow$) If $M^{*},w \models t$$:_{\{a\}}$$\psi(a,y)$, then $w \in \E^{*}(t, \psi(a,y))$. By definition, $M,w \models t$$:_{\{a\}}$$\psi(a,y)$. $\Box$\\

\qquad By (*) we have that,

\begin{center}
	$\E^{*}(t,\varphi) = \{w \in \W$ $|$ $ M,w \models t$$:_{X}$$\varphi\} = \{w \in \W$ $|$ $ M^{*},w \models t$$:_{X}$$\varphi\}$
\end{center}

\qquad Hence, $\E^{*}$ is a strong evidence function and $M$ and $M^{*}$ agree on all D-formulas. Therefore, $M^{*}$ is a Fitting model for FOJT45 and $M$ and $M^{*}$ agree on all formulas.
\end{proof}

\qquad With this proposition we can construct non-validity examples similar as presented in \cite{Fitting14}.

\qquad \textbf{Example 1:} the restriction on axiom \textbf{A2} is needed. Take, for example, the formula $t$$:_{\{x,y\}}$$Q(x,y) \impli t$$:_{\{x\}}$$Q(x,y)$; let $m =\model$ be a Fitting model where:
\begin{itemize}
\item $\W = \{w_0, w_1\}$;
\item $\R = \W \times \W$;
\item $\D = \{a, b\}$;
\item $\I(w_{0},Q) = \I(w_{1},Q) = \{\bl a,b \br\}$;
\item $\E(t,\varphi) = \W$, for every term $t$ and formula $\varphi$.
\end{itemize}

\qquad Clearly, $M,w_0 \models t$$:_{\{a,b\}}$$Q(a,b)$ and $M,w_0 \nmodels t$$:_{\{a\}}$$Q(a,y)$. Hence, $M,w_0 \nmodels t$$:_{\{x,y\}}$$Q(x,y) \impli t$$:_{\{x\}}$$Q(x,y)$. By Proposition 2, $t$$:_{\{x,y\}}$$Q(x,y) \impli t$$:_{\{x\}}$$Q(x,y)$ is not valid in every Fitting model for FOJT45.

\qquad \textbf{Example 2:} The proviso of axiom \textbf{B6} is necessary. Take, for example, the formula $t$$:_{\{x\}}$$Q(x) \impli gen(t)$$:_{\{x\}}$$\todo x Q(x)$; let $M =\model$ be a Fitting model where:
\begin{itemize}
\item $\W = \{w_0\}$;
\item $\R = \W \times \W$;
\item $\D = \{a,b\}$;
\item $\I(w_{0},Q) =  \{a\}$;
\item $\E(t,\varphi) = \W$, for every term $t$ and formula $\W$.
\end{itemize}

\qquad Clearly, $M,w_0 \models t$$:_{\{a\}}$$Q(a)$ and since $M,w_0 \nmodels Q(b)$, then $M,w_0 \nmodels \todo x Q(x)$, and so $M,w_0 \nmodels gen(t)$$:_{\{a\}}$$\todo x Q(x)$. Hence, $M,w \nmodels t$$:_{\{x\}}$$Q(x) \impli gen(t)$$:_{\{x\}}$$\todo x Q(x)$. Again by Proposition 2, $t$$:_{\{x\}}$$Q(x) \impli gen(t)$$:_{\{x\}}$$\todo x Q(x)$ is not valid in every Fitting model for FOJT45.

\section{Soundness and completeness}

\begin{teor}
(\textit{Soundness}) Let $\C$ be a constant specification. For every formula $\varphi \in L$, if $\teo_{\C} \varphi$, then $\models_{\C}\varphi$.
\end{teor}

\begin{proof}
The proof is by induction on the theorems of the axiom system using the constant specification $\C$. The argument is exactly the same as presented in \cite[p.9-10]{Fitting14}. We are going to show validity for the specific axiom of FOJT45.

\textbf{B5} $\nao t$$:_{X}$$\psi \impli$ $?t$$:_{X}$$\nao t$$:_{X}$$\psi$. For simplicity, assume $X= \{x\}$ and $\psi = \psi(x,y)$. So, we have that $\teo_{\C}\nao t$$:_{\{x\}}$$\psi(x,y) \impli$ $?t$$:_{\{x\}}$$\nao t$$:_{\{x\}}$$\psi(x,y)$.

\qquad Let $\M = \model$ be a Fitting model for FOJT45 meeting $\C$, $w \in \W$ and $a \in \D$. Suppose $M, w \models \nao t$$:_{\{a\}}$$\psi(a,y)$. Then, $M, w \nmodels t$$:_{\{a\}}$$\psi(a,y)$. By the definition of the strong evidence function, $w \notin \E (t, \psi(a,y))$. By the ? condition, $w \in \E(?t,\nao t_{\{a\}}$$:\psi(a,y))$. Again, by the strong evidence function $M, w \models ?t$$:_{\{a\}}$$\nao t$$:_{\{a\}}$$\psi(a,y)$.

\end{proof}

\subsection{Language extension}

\subsection{Templates}

\subsection{Using templates for Henkin and Lindenbaum theorems}

\subsection{Completeness}