\documentclass[]{beamer}
%
% Choose how your presentation looks.
%
% For more themes, color themes and font themes, see:
% http://deic.uab.es/~iblanes/beamer_gallery/index_by_theme.html
%
\mode<presentation>
{
  \definecolor{StanfordCardinal}{RGB}{140, 21, 21} % Stanford Cardinal (primary)
  \usetheme{Madrid}      % or try Darmstadt, Madrid, Warsaw, ...
  \usecolortheme[named=StanfordCardinal]{structure} % or try albatross, beaver, crane, ...
  \usefonttheme{default}  % or try serif, structurebold, ...
  \setbeamertemplate{navigation symbols}{}
  \setbeamertemplate{caption}[numbered]
  %\useinnertheme{circles}
  %\useoutertheme{miniframes} % Alternatively: miniframes, infolines, split
}
\usepackage{xcolor}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}

\title[GSE Math Camp: Day 2]{GSE Math Camp Day 2: \\ Calculus}
\author{Klint Kanopka}
\institute{kkanopka@stanford.edu}
\date{Wednesday, September 4, 2019}

\begin{document}

    \begin{frame}
      \titlepage
    \end{frame}

    \begin{frame}{Math Camp Materials}
        All materials for the first week are hosted at:
        \url{http://github.com/klintkanopka/gsemathcamp}
    \end{frame}

    \begin{frame}{Outline}
      \tableofcontents
    \end{frame}

\section{Motivation}

    \begin{frame}{Motivation}
        Calculus is the study of (infinitesimal) change, and will be used in education research most often for optimization during model fitting. Understanding these concepts can help make courses like 400B, 430A, and 430B more clear. These ideas are also a prerequisite for the Econ 102 sequence, the Polisci 450 sequence, and any study of machine learning.
    \end{frame}

\section{Differentiation}
    \begin{frame}{Rates of Change}
        Our goal will be to find the rate of change of one quantity compared to another. \\
        \textbf{Key definitions/notations:}
        \begin{itemize}
            \item<2-> Slope: the concept of slope as rate of change is important. For a straight line, the slope is \begin{center} {\LARGE $\frac{\text{rise}}{\text{run}} $} = {\LARGE $\frac{\text{change in y}}{\text{change in x}} $} = {\LARGE $\frac{\Delta{y}}{\Delta{x}}$ }\end{center}
            However, for other functions, the slope will not be constant.
        \item<3-> Derivative: can be thought of as the slope of a tangent line at a point on a function. The derivative tells you the rate of change at a point. It is denoted by {\LARGE $ \frac{\textit{dy}}{\textit{dx}}$} or f'(x) or y'. The value of the derivative may change for different values of x. In the graph below, we see that the derivative of function f(x) is positive, then 0, then negative (from left to right).
    \end{itemize}
    \end{frame}

    \subsection{Intuition}

    \begin{frame}{Intuition}
        \begin{center}
            \includegraphics[scale=0.4]{img/calculus_diffquotient.jpg}
        \end{center}
    \end{frame}

    \begin{frame}{Formal Definition}
        For a function, $f(x)$, we define the derivative, $f'(x)$, as follows:
        $$f'(x) = \lim_{\Delta h\rightarrow 0}\frac{f(x+h)-f(x)}{h}$$
    \end{frame}

\subsection{Differentiation Rules}
    \begin{frame}{Common Differentiation Rules}
        \begin{enumerate}
            \item<2-> $(af)'=a(f')$
            \item<3-> $(f+g)'=f'+ g'$
            \item<4-> $(f-g)'=f'- g'$
            \item<5-> Product rule for $h(x)=f(x)g(x):  h'(x)=f'(x)g(x) + f(x)g'(x)$
            \item<6-> Chain rule for $h(x)=f(g(x)): h'(x)=f'(g(x))g'(x)$
            \item<7-> Exponents: If $f(x)=x^n$ then $f'(x)=nx^{n-1}$ and when n=0, f'(x)=0
            \item<8-> Log rules: $\frac{d}{dx} ln\;x = \frac{1}{x}$ and $\frac{d}{dx}e^{cx}=ce^{cx}$
        \end{enumerate}
    \end{frame}

    \begin{frame}{Practice}
        Find the derivative of each function with respect to $x$.
        \begin{enumerate}
            \item $f(x) = 7x^3 -3$
            \item $g(x) = 4x(5-x^2)$
            \item $h(x) = \frac{1}{x^4}$
            \item $f(x) = \ln x^2$
            \item $g(x) = \ln (x^2 + 1)$
            \item $h(x) = 6x^6 - 5x^5 + 4x^4 - 3x^3 + 2x^2 - x + 1$
        \end{enumerate}
    \end{frame}

\section{Integration}
\subsection{Motivating Example}
    \begin{frame}{Integrals}
    \begin{itemize}
        \item Our goal will be to find the area of a region $S$ that lies under the curve $f(x)$ from points $a$ to $b$.
        \item<2-> Suppose our function of interest is $f(x) = x^2 + 1$ on $x\in[0,2]$. Let's define the shaded region under $f(x)$ as $S$. \\
        \begin{center}
            \onslide<3->{\includegraphics[scale=0.5]{img/integrals_mainfunction.pdf}}
        \end{center}
    \end{itemize}
    \end{frame}

    \begin{frame}{Estimating Areas}
    How can we find the area of $S$? We have a few options: \\
        \onslide<2->{Option 1: Rectangle method using right-hand endpoints: \\}
            \onslide<3->{\begin{center}
                \includegraphics[scale=0.4]{img/integrals_rectangleR.pdf}
            \end{center}}
    \onslide<4->{\begin{align*}
    Area(S) &= \frac{1}{2}f\left(\frac{1}{2}\right) + \frac{1}{2}f\left(1\right) + \frac{1}{2}f\left(\frac{3}{2}\right) + \frac{1}{2}f\left(2\right) \\
    &= \frac{1}{2}\left(\frac{5}{4}\right) + \frac{1}{2}\left(2\right) + \frac{1}{2}\left(\frac{13}{4}\right) + \frac{1}{2}\left(5\right) \\
    &= 5.75
    \end{align*}}
    \end{frame}

    \begin{frame}{Estimating Areas}
    How can we find the area of $S$? We have a few options: \\
        \onslide<2->{Option 2: Rectangle method using left-hand endpoints: \\}
        \onslide<3->{\begin{center}
            \includegraphics[scale=0.4]{img/integrals_rectangleL.pdf}\\
        \end{center}}
    \onslide<4->{Calculate $Area(S)$.}
    \end{frame}

    \begin{frame}{Estimating Areas}
        \onslide<2->{Rectangle method using midpoints: \\}
        \onslide<3->{\begin{center}
            \includegraphics[scale=0.4]{img/integrals_midpoint.pdf}\\
        \end{center}}
        \onslide<4->{Calculate $Area(S)$.\\}
        \onslide<5->{Which other shapes might we try?}
    \end{frame}

    \begin{frame}{Doing Better}
        While we can use shapes like rectangles, our estimate of the area will be imprecise. One thing we might want to do is divide up our region into more parts. For example, using rectangles:

        \begin{center}
            \onslide<2->{\includegraphics[scale=0.35]{img/integrals_morerectangleR.pdf}}
            \onslide<3->{\includegraphics[scale=0.35]{img/integrals_morerectangleL.pdf}}
            \onslide<4->{\includegraphics[scale=0.35]{img/integrals_moremidpoint.pdf}}
        \end{center}
        \onslide<5->{While dividing the region may get us closer to the true answer, it is still not exact!}
    \end{frame}

\subsection{Indefinite Integrals}

    \begin{frame}{Antiderivatives}
        Starting from a known function, $f$, we want to find a function $F$ for which $f$ is its derivative.
        \onslide<2->{Call $F$ the \textit{antiderivative} of $f$. We can write this as:}
            \onslide<3->{$$F' = f $$}
        \onslide<4->{\textbf{Definition}: If $F$ is the antiderivative of $f$, then $F$ is called the indefinite integral of $f$:}
            \onslide<5->{$$ F(x) = \int f(x) dx$$}
    \end{frame}

    \begin{frame}{Example}
        Let $f(x) = 6x$. We want to find $F(x) = \int 6x dx$.
        \onslide<2->{There are many potential solutions:}
        \begin{enumerate}
            \item<3-> $3x^2 + 1$
            \item<4-> $3x^2 + 2$
            \item<5-> $3x^2 + 100$, etc.
        \end{enumerate}
        \onslide<6->{\textbf{IMPORTANT}: The derivative of a function can have many antiderivatives. With any antiderivative, we need to add a constant $c$, unless we are told that the antiderivative passes through a particular point.

        The correct solution to our example problem, in the absence of any additional information, is $3x^2 + c$}
    \end{frame}

    \begin{frame}{Integration Rules}
        \begin{enumerate}
            \item<2-> $\int [(f(x) + g(x)] dx = \int f(x) dx + \int g(x) dx$
            \item<3-> $\int c f(x) dx = c \int f(x) dx$
            \item<4-> $\int x^n dx = \frac{x^{n+1}}{n+1} + c$
            \item<5-> $\int \frac{1}{x} dx = \ln|x| + c$
            \item<6-> $\int e^x dx = e^x + c$
            \item<7-> $\int e^{f(x)} f'(x) dx = e^{f(x)}+c$
            \item<8-> $\int [f(x)]^nf'(x) dx = \frac{1}{n+1}[f(x)]^{n+1}+c$
            \item<9-> $\int \frac{f'(x)}{f(x)} dx = \ln f(x) + c$
        \end{enumerate}
    \end{frame}
\subsection{Definite Integrals}
    \begin{frame}{Definite Integrals}
        We return to our initial problem of trying to calculate the area under a curve. Using an approximation method (i.e., one of the rectangle methods), we get close, but we want an exact answer.
        \begin{itemize}
            \item<2-> The rectangle approximation method is an application of a Riemann sum
                $$ Area(S) = \sum_{i=1}^n \underbrace{f(x_i)}_{height} \underbrace{\Delta x}_{base}$$
            \item<3-> How above making the base of the rectangles infinitesimally small? We can use limits:
                $$ Area(S) = \lim_{\Delta x\rightarrow 0} \sum_{i=1}^n \underbrace{f(x_i)}_{height} \underbrace{\Delta x}_{base}$$
        \end{itemize}
    \end{frame}

    \begin{frame}{Riemann Integral}
        The Riemann Integral is the solution to our problem!
            \onslide<2->{$$\int_a^b f(x) dx = \lim_{\Delta x\rightarrow 0} \sum_{i=1}^n f(x_i) \Delta x$$}
        \onslide<3->{The Riemann Integral is often referred to as the definite integral in calculus textbooks. Formally,  $\int_a^b f(x) dx$  denotes the integral of function $f$ from $a$ to $b$ (or the area under curve $f(x)$ from $x=a$ to $x=b$).}
    \end{frame}

    \begin{frame}{Fundamental Theorem of Calculus}
        Suppose $f$ is continuous on $[a,b]$,
            $$\int_a^b f(x) dx = F(b) - F(a)$$
        where $F$ is any antiderivative of $f$. That is, $F' = f$.

        \onslide<2->{FTC II provides a way to calculate a definite integral:
        \begin{enumerate}
            \item Find the indefinite integral $F(x)$
            \item Evaluate $F(b) -F(a)$
        \end{enumerate}}
    \end{frame}
\subsection{Properties of Integrals}
    \begin{frame}{Properties of the Definite Integral}
        \begin{enumerate}
            \item<2-> $\int_a^a f(x) dx = 0 \quad \text{No area under a point.}$
            \item<3-> $\int_a^b f(x) dx = -\int_b^a f(x) \quad \text{Switching limits changes sign of the integral}$
            \item<4-> $\int_a^b [\alpha f(x) + \beta g(x)] dx = \alpha \int_a^b f(x) dx + \beta \int_a^b g(x) dx$
            \item<5-> $\int_a^b f(x) dx + \int_b^c f(x) dx = \int_a^c f(x) dx$
        \end{enumerate}
    \end{frame}

\end{document}


% 9.5.07
% This is a sample documentation for Compass in the tex format.
% We restrict the use of tex to the following subset of commands:
%
% \section, \subsection, \subsubsection, \paragraph
% \begin{enumerate} (no-nesting), \begin{quote}, \item
% {\tt ... }, {\bf ...}, {\it ... }
% \htmladdnormallink{}{}
% \begin{verbatim}...\end{verbatim} is reserved for code segments
% ...''
%

\section{Prefer Algorithms}
\label{PreferAlgorithms::overview}

% write your introduction
Many people consider hand-written loops over STL containers inferior to calls
to STL algorithms for reasons of efficiency, correctness, and maintainability.

This checker is meant to highlight cases where a loop might be replaced by an
equivalent STL algorithm call. It reports for loops where the loop head
fulfills the following properties:
\begin{itemize}
\item The initialization part contains an assignment or variable declaration
with an initializer,
\item the condition part consists of an inequality comparison, and
\item the increment part consists of an increment or decrement operation.
\end{itemize}
For loops on integer or floating-point types are not reported as those cannot
be replaced by STL algorithms.

\subsection{Parameter Requirements}

This checker does not require any parameters.

\subsection{Non-Compliant Code Example}

\begin{verbatim}
#include <vector>

void add_x_to_each_element_noncompliant(int x, std::vector<int> &v)
{
    // not OK: loop to add x to each element
    std::vector<int>::iterator v_itr;
    for (v_itr = v.begin(); v_itr != v.end(); ++v_itr)
        *v_itr += x;
}
\end{verbatim}

\subsection{Compliant Solution}

\begin{verbatim}
#include <vector>
#include <algorithm>
#include <functional>

void add_x_to_each_element_compliant(int x, std::vector<int> &v)
{
    // OK: using an algorithm to add x to each element
    transform(v.begin(), v.end(), v.begin(),
              std::bind2nd(std::plus<int>(), x));
}
\end{verbatim}

\subsection{Mitigation Strategies}
\subsubsection{Static Analysis}

Compliance with this rule can be checked using structural static analysis checkers using the following algorithm:

\begin{enumerate}
\item For each for loop, check the criteria explained above, taking both
built-in and overloaded operators.
\item If the loop fulfills all criteria, generate a diagnostic.
\end{enumerate}

\subsection{References}

% Write some references
% ex. \htmladdnormallink{ISO/IEC 9899-1999:TC2}{https://www.securecoding.cert.org/confluence/display/seccode/AA.+C+References} Forward, Section 6.9.1, Function definitions''
A reference for this checker is: S.~Meyers: ``Effective STL'', Item~43:
``Prefer algorithm calls to hand-written loops''.


\documentclass[11pt, oneside]{article}   	% use "amsart" instead of "article" for AMSLaTeX format
\usepackage{geometry}                		% See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper}                   		% ... or a4paper or a5paper or ...
%\geometry{landscape}                		% Activate for rotated page geometry
%\usepackage[parfill]{parskip}    		% Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}				% Use pdf, png, jpg, or eps§ with pdflatex; use eps in DVI mode
								% TeX will automatically convert eps --> pdf in pdflatex
\usepackage{amssymb}
\usepackage{topcapt}
\usepackage{booktabs}

%SetFonts

%SetFonts

%\title{Brief Article}
%\author{The Author}
%\date{}							% Activate to display a given date or no date

\begin{document}
%\maketitle
%\section{}
%\subsection{}

\pagestyle{empty}  % turn of page numbering
\renewcommand{\thetable}{S\arabic{table}} % add "S" to table number

% Requires the booktabs if the memoir class is not being used
\begin{table}[htbp]
   \centering
   \topcaption{List of mathematical symbols.} % requires the topcapt package
   \begin{tabular}{clc} % Column formatting, @{} suppresses leading/trailing space
      \toprule
      Symbol    & Description & Dimensions \\
      \midrule
      $a$ & surface-roughness coefficient & - \\
      $d$ & disturbance transition frequency & T$^{-1}$ \\
      $d'$ & dimensionless disturbance frequency $=d\tau$ & - \\
      $D$ & characteristic disturbance rate $=\delta d$ & LT$^{-1}$ \\
      $D_s$ & soil-creep transport coefficient & L$^2$T$^{-1}$ \\
      $E_n$ & slope-normal erosion rate & LT$^{-1}$ \\
      $E_v$ & vertical erosion rate & LT$^{-1}$ \\
      $E'_v$ & dimensionless vertical erosion rate $=E_v/\delta d$ & - \\
      $q_s$ & sediment flux per unit width & L$^2$T$^{-1}$ \\
      $s$ & dissolution transition frequency & T$^{-1}$ \\
      $S'$ & dimensionless dissolution efficiency & - \\
      $S$ & slope gradient & - \\
      $S_c$ & critical slope gradient & - \\
      $V$ & fault slip rate $=\delta / \tau$ & LT$^{-1}$ \\
      $w$ & weathering transition frequency & T$^{-1}$ \\
      $w'$ & dimensionless weathering efficiency $=w\tau$ & - \\
      $W$ & characteristic weathering rate $=\delta w$ & LT$^{-1}$ \\
      $W_s$ & characteristic dissolution rate $=\delta s$ & LT$^{-1}$ \\
      $\alpha$      & fault dip angle & - \\
      $\beta$          & $\alpha - \theta$ &  - \\
      $\delta$ & cell width & L \\
      $\theta$ & facet dip angle & - \\
      $\tau$ & unit slip interval $=\tau_s / \sqrt{3}$ & T \\
      $\tau_s$ & lattice slip interval & T \\
      \bottomrule
   \end{tabular}
   \label{tab:symbols}
\end{table}

\end{document}


\documentclass[twoside]{article}

\usepackage[sc]{mathpazo} % Use the Palatino font
\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\linespread{1.05} % Line spacing - Palatino needs more space between lines
\usepackage{microtype} % Slightly tweak font spacing for aesthetics

\usepackage[hmarginratio=1:1,top=32mm,columnsep=20pt]{geometry} % Document margins
\usepackage{multicol} % Used for the two-column layout of the document
\usepackage[hang, small,labelfont=bf,up,textfont=it,up]{caption} % Custom captions under/above floats in tables or figures
\usepackage{booktabs} % Horizontal rules in tables
\usepackage{float} % Required for tables and figures in the multi-column environment - they need to be placed in specific locations with the [H] (e.g. \begin{table}[H])
\usepackage{hyperref} % For hyperlinks in the PDF

\usepackage{lettrine} % The lettrine is the first enlarged letter at the beginning of the text
\usepackage{paralist} % Used for the compactitem environment which makes bullet points with less space between them

\usepackage{abstract} % Allows abstract customization
\renewcommand{\abstractnamefont}{\normalfont\bfseries} % Set the "Abstract" text to bold
\renewcommand{\abstracttextfont}{\normalfont\small\itshape} % Set the abstract itself to small italic text

\usepackage{titlesec} % Allows customization of titles
\renewcommand\thesection{\Roman{section}} % Roman numerals for the sections
\renewcommand\thesubsection{\Roman{subsection}} % Roman numerals for subsections
\titleformat{\section}[block]{\large\scshape\centering}{\thesection.}{1em}{} % Change the look of the section titles
\titleformat{\subsection}[block]{\large}{\thesubsection.}{1em}{} % Change the look of the section titles

\usepackage{fancyhdr} % Headers and footers
\pagestyle{fancy} % All pages have headers and footers
\fancyhead{} % Blank out the default header
\fancyfoot{} % Blank out the default footer
\fancyhead[C]{Bitquant Research Laboratories Working Paper No. 1} % Custom header text
\fancyfoot[RO,LE]{\thepage} % Custom footer text

\title{\vspace{-15mm}\fontsize{24pt}{10pt}\selectfont\textbf{A simple macroeconomic model of bitcoin}} % Article title

\author{
\large
\textsc{Joseph C Wang}
\normalsize Bitquant Research Laboratories\\
\normalsize \href{http://www.bitquant.com.hk/}{http://www.bitquant.com.hk/} \\
\normalsize \href{mailto:joequant@gmail.com}{joequant@gmail.com} \\
\date{2014 February 11}
}

\usepackage{natbib}
\bibliographystyle{plainnat}
\usepackage{url}
\begin{document}
\maketitle
\thispagestyle{fancy}
\begin{abstract}
This working paper presents a simple model for the macroeconomic
behavior of bitcoin based on the economic equation of exchange.
According to this model, the value of bitcoin is determined largely by
the willingness of bitcoin holders to save bitcoin and not by its
transactional use.  This model therefore predicts that increased use
of bitcoin will not cause its value to rise, but that the value of
bitcoin in terms of fiat currency will be almost solely determined by
the willingness of bitcoin holders to pull bitcoin out of circulation.
This model suggests that bitcoin will not fall victim to a liquidity
trap as suggested by some economists.
\end{abstract}

\begin{multicols}{2}

\section{Introduction}

In this working paper, we present a model for the valuation of bitcoin
based on simple macroeconomic arguments.  We caution that our model is
intended primarily to stimulate discussion and that the truth of this
model or any economic model will be determined by empirical
observation of the bitcoin market.

\section{Macroeconomics of Bitcoin}

We begin our money with the equation of exchange where
\begin{equation}
M \cdot V = P \cdot Q
\end{equation}
where
\begin{eqnarray*}
& M & \mbox{is the nominal amount of money}\\
& V & \mbox{is the velocity of money}\\
& P & \mbox{is the price level}\\
& Q & \mbox{is the index of real expenditures}\\
\end{eqnarray*}

We now modify the equation in order to take into account the unique
aspects of bitcoin.  First we express all of our quantities in units
of fiat currency.  By expressing all of our quantities in terms of
fiat currency, we can set the $P$ to $1$.  Since we are expressing all
quantities in units of fiat currency, the value for $M$ is now the
value of bitcoin as measured in fiat currency units.

We next expand the quantity $M$ in terms of the number of bitcoin in
circulation $n_b$ and the price of a single bitcoin $p_b$.  The number
of bitcoin in circulation is externally determined and slowing and
predictable varying, whereas the price of a single bitcoin will
fluctuate.  Substituting $M=n_b p_b$ we and rearranging we get.

\begin{equation}
p_b = \frac{Q}{n_b V}
\end{equation}

We note that $n_b$ is an externally determined and slowing changing
variable.  The main determinant of the price of bitcoin is the
interaction between the level of bitcoin usage and the velocity of
bitcoin.

At this point we have made no assumptions concerning the dynamics of
bitcoin.  We shall now add some dynamical assumptions concerning the
relationship between the number of bitcoin expended $Q$ and the
velocity of an individual bitcoin $V$.

We decompose the velocity of bitcoin into a portion that is saved and
portion that is transacted.  We denote the likelihood that a
invididual bitcoin is saved by $l_s$ and the likelihood that a bitcoin
is transacted as $l_t$.  These two variables will sum to $1$.  These
portions have a corresponding set of velocities $v_s$ being the
velocity of a saved bitcoin and $v_t$ being the velocity of a
transacted bitcoin.

We now make several claims about the dynamics of bitcoin.  Because
$v_t \gg v_s$ we claim that we can set the velocity of saved bitcoins
to $0$.  We further claim that the velocity of transacted bitcoins can
be modeled as a linear function of $Q$.  We therefore have the
following expression for the total velocity of bitcoin.

\begin{equation}
V = l_t\alpha_t Q
\end{equation}.

Substituting back into the original equation we see that
\begin{equation}
p_b = \frac{1}{l_t \alpha_t}
\end{equation}.

We claim that $\alpha_t$ will remain roughly constant over time.  That
leaves only one term which impacts the price of bitcoin, which is the
$l_t$ the likelihood that a given bitcoin will be transacted rathr
than saved.

\section{Implications of our model}
Our model makes very strong claims concerning the price of bitcoins.
In particular, it states that the price of bitcoin is determined
almost solely by the likelihood that a given bitcoin will be saved. If
a user uses bitcoin for transaction purposes then this has no impact
on the value of bitcoin, while the value of bitcoin rises a given
bitcoin is more likely to be saved rather than transacted.

This model seems to agree with the changes in bitcoin pricing in 2013.
The two events that caused bitcoin prices to increase were the Cyprus
banking crisis in April 2013 and the rise of mainland Chinese bitcoin
exchanges in October 2013.  Both these events increased the likelihood
that a given bitcoin would be saved rather than transacted which
increased the price of bitcoin.  We note that the main drivers for
bitcoin prices have been news reports concerning the convertibility of
bitcoin, and that news reports concerning the increasing usage of
bitcoin has made very little impact on its price.

We also note that bitcoin has is prone to sudden changes in price due
to news events but that the price of bitcoin after a sudden shock has
returned to stable levels.  Our model explains this phenomenon by
asserting that although shocks can cause a sudden change in the price
of bitcoin, to the extent that it does not cause a change in how
likely a bitcoin is saved or transacted, it will not cause a change in
the long-run price level of bitcoin.

Our model also makes predictions concerning the future price of
bitcoin.  If our model is correct, then we should expect that
increased usage of bitcoin should manifest itself in larger volumes
rather than increased prices.  Prices in bitcoin would only be
effected by events which increase or decrease the likelihood that a
given bitcoin would be saved rather than transacted.

Some economists, notably Paul Krugman, are pessimistic about the
viability of bitcoin as a medium of exchange, because they believe
that bitcoin will fall into a liquidity trap caused by the fixed
supply of bitcoin \cite{krugman1}.  Krugman claims that as the price
of bitcoin rises, the amount of bitcoin in circulation will decrease
thereby increasing the price, ultimately creating a situation in
which bitcoin is completely hoarded.  This effect has been observed
in scrip economies such as the Capital Hill Babysitting Co-op
\cite{krugman2}.

However, our model provides a mechanism by which bitcoin can avoid a
liquidity trap and explains why bitcoin has not experienced such a
trap.  The mechanism which we claim this will occur involves the
interaction between bitcoin and fiat currency.  As the price of
bitcoin rises, we claim that people will be more likely to spend their
bitcoin rather than fiat currency, which will decrease saved bitcoin
thereby cause bitcoin prices to fall.  Conversely a fall in the price
of bitcoin will increase the likelihood that people will save bitcoin
as they will be more likely to spend fiat, and this will cause bitcoin
prices to rise.  Our models claims that long-run increases or
decreases in the price of bitcoin will not influenced by the
transactional use of bitcoin but rather by external factors which
change the likelihood that a given bitcoin will be saved.

Krugman's belief that bitcoin is destined for a liquidity trap ignores
the fact that bitcoin exists within an fiat-based economy, and that
the ease of convertibility to and from fiat prevents a liquidity trap
that exists when convertibility is difficult.

\section{Conclusion}
We caution that the correctness of the model will be determined by
future empirical observations of the bitcoin market.  However, we hope
to illustrate that the behavior of bitcoin can be modelled using basic
economic principles, and we hope that this working paper will
stimulate research and discussion in this area.

\bibliography{macro}
\end{multicols}

\end{document}


%%% ====================================================================
%%%  @LaTeX-file{
%%%     filename  = "subeqn.tex",
%%%     version   = "1.2c",
%%%     date      = "1999/11/29",
%%%     time      = "14:49:29 EST",
%%%     checksum  = "29866 202 668 6426",
%%%     author    = "American Mathematical Society",
%%%     copyright = "Copyright 1999 American Mathematical Society,
%%%                  all rights reserved.  Copying of this file is
%%%                  authorized only if either:
%%%                  (1) you make absolutely no changes to your copy,
%%%                  including name; OR
%%%                  (2) if you do make changes, you first rename it
%%%                  to some other name.",
%%%     address   = "American Mathematical Society,
%%%                  Technical Support,
%%%                  Electronic Products and Services,
%%%                  P. O. Box 6248,
%%%                  Providence, RI 02940,
%%%                  USA",
%%%     telephone = "401-455-4080 or (in the USA and Canada)
%%%                  800-321-4AMS (321-4267)",
%%%     FAX       = "401-331-3842",
%%%     email     = "tech-support@ams.org (Internet)",
%%%     supported = "yes",
%%%     keywords  = "latex, amslatex, ams-latex, amstex",
%%%     abstract  = "This is part of the AMS-\LaTeX{} distribution.
%%%                  It is a sample document illustrating the use of
%%%                  the subequations environment and the effect of
%%%                  the fleqn option.",
%%%     docstring = "The checksum field above contains a CRC-16
%%%                  checksum as the first value, followed by the
%%%                  equivalent of the standard UNIX wc (word
%%%                  count) utility output of lines, words, and
%%%                  characters.  This is produced by Robert
%%%                  Solovay's checksum utility.",
%%%  }
%%% ====================================================================
\documentclass[fleqn]{article}

\title{Test of subequations environment}
\author{mjd}

\usepackage{amsmath}
\numberwithin{equation}{section}

\newcommand{\env}[1]{{\normalfont\texttt{#1}}}

\AtEndDocument{\multipasswarning}
\newcommand{\multipasswarning}{%
  \clearpage
  \typeout{%
  **********************************************************************}
  \typeout{%
  Note: This document needs to run through LaTeX three times, instead of}
  \typeout{%
  the usual two, to resolve indirect cross-references.}
  \typeout{%
  **********************************************************************}
}

\makeatletter
%    Omit the warning message if three passes have been completed (on
%    first pass \ref{check} is undefined and it is set to 0; on second
%    pass \ref{check} is 0 and set to 1; on third pass it is 1).
\def\checkref{\begingroup
  \@ifundefined{r@check}{\let\r@check\relax\def\@currentlabel{0}}{\def\@currentlabel{1}}%
  \ifnum1=0\expandafter\@firstoftwo\r@check\relax
    \global\let\multipasswarning\relax
  \fi
  \label{check}%
  \endgroup}
\makeatother

\begin{document}
\section{Prime equation numbers}

First an equation.
\begin{equation}\label{e:previous}
A=B
\end{equation}
That was equation \eqref{e:previous}.

Then the same, with a prime on the number.\checkref
\begin{equation}
\tag{\ref{e:previous}$'$}\label{e:prevprime}
C=D
\end{equation}
And that was equation \eqref{e:prevprime}.

Notice, by the way, that when a \verb"\ref" occurs inside a \verb"\tag",
and that \verb"\tag" is then \verb"\label"'d, a \verb"\ref" for the the
second \verb"\label" requires \emph{three} runs of \LaTeX{} in order to
get the proper value. (If you run through the logic of \LaTeX{}'s
cross-referencing mechanisms as they apply in this case, you will see
that this is necessary.)

\section{Subnumbered equations}

Here is a,b,c sub-numbering.
\begin{subequations}
\begin{eqnarray}
A&=&B\\
D&=&C \label{e:middle}\\
E&=&F
\end{eqnarray}
\end{subequations}
That was produced with the \env{eqnarray} environment; the middle line
was labeled as \eqref{e:middle}.

An equation following the end of the \verb"subequations" environment
should revert to normal numbering:
\begin{equation}\label{e:check}
H<K
\end{equation}
A check on the labeling: that was equation~\eqref{e:check}.

The sub-numbered equations can be spread out through the text, like
this:
\begin{subequations}
\begin{equation}
A=B
\end{equation}
The \verb"subequations" environment can span arbitrary text between
subsidiary equations. The only restriction is that if there are any
numbered equations inside the \verb"subequations" environment that break
out of the subequation numbering sequence, they would have to be handled
specially.
\begin{equation}
D=C \label{e:newmiddle}
\end{equation}
More arbitrary text.
\begin{equation}
E=F
\end{equation}
\end{subequations}
Label check: the middle one was \eqref{e:newmiddle}

A final equation for a numbering check.
\begin{equation}\label{e:final}
G=H
\end{equation}
That equation was labeled as \eqref{e:final}.

\section{Tests of \env{align}, \env{gather}, and other
AMS-\protect\LaTeX{} environments}

The \env{align} environment:
\begin{subequations}
\begin{align}
\label{align:a}A+B&=B+A\\
\label{align:b}C&=D+E\\
\label{align:c}E&=F
\end{align}
\end{subequations}
Label check: that was \eqref{align:a}, \eqref{align:b}, and
\eqref{align:c}.

The \env{align} environment again:
\begin{subequations}
\begin{align}
\label{xalign:a}A+B&=B&     B&=B+A\\
\label{xalign:b}C&=D+E&     C\oplus D&=E\\
\label{xalign:c}E&=F&       E'&=F'
\end{align}
\end{subequations}
Label check: that was \eqref{xalign:a}, \eqref{xalign:b}, and
\eqref{xalign:c}.

The \env{gather} environment. For the third line we refer to one of the
numbers in the first \env{align} structure.
\begin{subequations}
\begin{gather}
\label{gather:a}A+B=B\\
\label{gather:b}C=D+E\\
\tag{\ref{align:c}$'$}\label{gather:c}E=F
\end{gather}
\end{subequations}
Label check: that was \eqref{gather:a},
\eqref{gather:b}, and
\eqref{gather:c}.

The next \env{subequations} environment encompasses two separate
equations. A \env{split} environment:
\begin{subequations}
\begin{equation}
\label{split:x}
\begin{split}
A&=B+C+F\\
&=G
\end{split}
\end{equation}
and a \env{multline} environment:
\begin{multline}\label{multline:x}
A[B]C[D]E[F]G[[H[I]J[K]L[M]N]]=\\
H[I]J[K]L[M]N[O]P[Q]R[S]T[U]V[W]X[Y]Z
\end{multline}
\end{subequations}
Label check: That was \eqref{split:x} and \eqref{multline:x}.

\end{document}


\documentclass[]{article}
\usepackage[margin=4cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{amsfonts}

\title{Robust Reinforcement Learning}
\author{Mathias Beguin}
\date{2018}

\begin{document}

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

\section{Formalization of the problem}
Reinforcement learning (abbreviated RL) is an area of machine learning in which agents learn how to interact in an environment so as to maximize a notion of reward. It differs from standard supervised learning in that correct input/output pairs need not be presented, and sub-optimal actions need not to be explicitly corrected. Instead it focuses on performance which involve finding a balance between exploration of uncharted state and exploitation of current knowledge.

\subsection{Markov Decision Process} \label{section:MDP}
In reinforcement learning, the problem is usually described as a Markov Decision Process. It is a mathematical framework used to model decision making problem in which outcomes are under the control of an agent. It is defined as a 5-tuple $(S, A, P_a, R_a, \gamma)$ where
\begin{itemize}
	\item $S$ is a set of states
	\item $A$ is a set of actions ($A_s$ is the set of available actions in state $s$)
	\item $Pr(s_{t+1} = s' | s_t = s, a_t = a)$ is the probability of transition from state $s$ to state $s'$ under the action $a$ at time $t$
	\item $R(s, a, s')$ is the immediate reward after a transition from $s$ to $s'$ by taking the action $a$
	\item $\gamma \in [0, 1)$ is the discount factor, which represents the difference in importance between future and present rewards
\end{itemize}

A reinforcement learning agent interacts with its environment in discrete time steps. At each time $t$, the agent receives an observation $o_{t}$. It then chooses an action $a_{t}$ from the set of available actions, which is subsequently sent to the environment. The environment moves to a new state $s_{t+1}$ and the reward $r_{t}$ associated with the transition $(s_{t},a_{t},s_{t+1})$ is determined.

\begin{figure}[h]
	\centering
	\includegraphics[scale=0.5]{rl-loop.jpg}
	\caption{Reinforcement learning interaction loop}
\end{figure}

The goal of a reinforcement learning agent is to learn a policy $\pi$ that specifies the action $\pi(s)$ to take in state $s$. This policy should maximize some cumulative function of the rewards. Typically the expected discounted sum of rewards $\sum_{t = 0}^{\infty} \gamma^t R(s, a_t, s')$ where $a_t = \pi(s)$ is taken.

\subsection{Policy Gradient}
Policy based models are methods that learn a parameterized policy $\pi (a | s; \theta)$ that can select actions without consulting a value function. A value function may still be used to learn the policy parameter, but is not required for action selection. They work by computing an estimator

\begin{equation}
\hat{g} = \hat{\mathbb{E}}_t [\nabla_\theta \ log\ \pi_\theta(a_t | s_t) \hat{A}_t]
\end{equation}

 using trajectories generated by the policy and plugging it into a stochastic gradient ascent algorithm. The term $\hat{A}_t$ corresponds to an estimator of the advantage function at time step $t$. It has for purpose to determine which action yields better or worse result than the average. When an estimator of the value function $V_\pi(s) = E[\sum_{t = 0}^{\infty} \gamma^tr_t | s_0 = s]$ is used, the methods become \textit{actor-critic methods}.

\subsection{Contribution}
The goal of this thesis is to augment the initial goal of learning a parametrized policy so as to make it robust to the value of nuisance parameters $z \in Z$.
Inspired by the work of Gilles Louppe, Michael Kagan and Kyle Cranmer in "Learning to Pivot with Adversarial Networks", our data generation process is a Markov Decision Process defined as in section \ref{section:MDP} where we modify the transition probability distribution to introduce the nuisance parameters and it becomes $Pr(s_{t+1} | s_t, a_t, z)$.

To learn such policy, we need it to be a pivot with respect to $Z$. A pivot is  a quantity whose distribution is invariant with the nuisance parameters. Therefore, we would like to learn policies such that

\begin{equation}
\pi (a | s, z; \theta) = \pi (a | s, z'; \theta)
\end{equation}

for all $z$, $z' \in Z$, all values of $a \in A$ and all values of $s \in S$. This equation implies that $\pi(a | s, \theta)$ and $Z$ are independent random variables.

\subsection{Method}
Following the method of "Learning to Pivot", we use a re-purposed adversarial networks as a means to constrain the policy $\pi$. We pit $\pi$ against an adversarial model $r$ that takes as input a sequence of tuples $\tau = (s_t, a_t, r_t, s_{t+1})$ where $t \in [0, T]$ which correspond to an episode and where the $a_t$ are chosen using $\pi(a | s;\theta)$ and  outputs a function modeling the posterior probability $z$ given $\tau$:

\begin{equation}
r := Pr(z | \tau \sim \pi(a | s;\theta))
\end{equation}

Intuitively, if $\pi (a | s, z; \theta)$ varies with $z$, then the corresponding correlation can be captured by $r$. By contrast, if $\pi (a | s, z; \theta)$ is invariant with $z$, then $r$ should perform poorly and be close to random guessing.

\end{document}


\documentclass[12pt]{article}

% Packages
\usepackage{float}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{listingsutf8}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[a4paper, total={16cm, 23.7cm}]{geometry}
\usepackage[colorlinks=true,urlcolor=blue]{hyperref}
\usepackage{color}

% Compiler/IDE directives
% !TeX spellcheck = en_US

% General
\fontfamily{phv}
\captionsetup{width=0.8\textwidth}

% Graphicx
\graphicspath{ {./images/} }

% Listings
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\fontsize{7}{9}\selectfont\ttfamily,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2
}
\lstset{style=mystyle}

% Geometry
\DeclareMathSizes{12}{13}{10}{8}
\setlength\parindent{0.7cm}

% Helper commands
\newcommand*\diff{\mathop{}\!\mathrm{d}}
\newcommand*\Diff[1]{\mathop{}\!\mathrm{d^#1}}
\newcommand\tab[1][.7cm]{\hspace*{#1}}
\renewcommand{\refname}{}
\newcommand{\code}[1]{\texttt{#1}}
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}

%----------------------------------------------------------------------------
% THE DOCUMENT
%----------------------------------------------------------------------------
\begin{document}

%----------------------------------------------------------------------------
% TITLE
%----------------------------------------------------------------------------
\begin{center}
	\Huge Advanced Numerical Methods\\
	\Large Assignment 1\\
	\vspace{1pc}
	\huge Péter Kardos \\
	\large 2019
\end{center}

Just uploading something.

\section{Task 1}

\subsection{Energy method}

The energy method on the continuous problem is used to derive the boundary terms.

\begin{equation}
    \begin{split}
        Cu_t = Au_x\\
        u =
        \begin{bmatrix}
            E\\
            H
        \end{bmatrix}
        A =
        \begin{bmatrix}
            0 & 1\\
            1 & 0
        \end{bmatrix}
        C =
        \begin{bmatrix}
            \epsilon & 0\\
            0 & \mu
        \end{bmatrix}
    \end{split}
\end{equation}

\noindent Multiply by $u^T$ from the left an integrate by parts:

\begin{equation}\label{eq_cont_energy_ibp}
    (u^T, Cu_t) = (u^T, Au_x) = -(u^T_x, Au) + [u^TAu]_{x_l}^{x_r}
\end{equation}

\noindent Calculate the transpose:

\begin{equation}\label{eq_cont_energy_transp}
    ((Cu_t)^T, u) = ((Au_x)^T, u) = (u_x^TA^T, u) = (u_x^TA, u) = (u_x^T, Au)
\end{equation}

\noindent Adding (\ref{eq_cont_energy_ibp}) and (\ref{eq_cont_energy_transp}):

\begin{equation}
    (u^T, Cu_t) + ((Cu_t)^T, u) = \frac{d}{dt}\norm{u}^2 = (u_x^T, Au) - (u^T_x, Au) + [u^TAu]_{x_l}^{x_r} = [u^TAu]_{x_l}^{x_r}
\end{equation}

\noindent We get the boundary terms:

\begin{equation}
    [u^TAu]_{x_l}^{x_r} = u^{(1)}u^{(2)}|_{x_r} - u^{(1)}u^{(2)}|_{x_l}
\end{equation}

\section{Task 2}

\section{Task 3}

\end{document}


\section{Data structures}

\subsection{Algebraic Datatypes (ADT)}

(ADT is also the usual accronym for Abstract DataType. However, I'll
never talk about abstract datatypes in this manual, so there's no
reason to get confused about it. ADT always refers to algebraic
datatypes).

Metalua's distinctive feature is its ability to easily work on program
source codes as trees, and this include a proper syntax for tree
manipulation. The generic table structure offered by Lua is
definitely good enough to represent trees, but since we're going to
manipulate them a lot, we give them a specific syntax which makes them
easier to read and write.

So, a tree is basically a node, with:
\begin{itemize}
\item a tag (a string, stored in the table field named ``{\tt tag}'')
\item some children, which are either sub-trees, or atomic values
  (generally strings, numbers or booleans). These children are stored
  in the array-part\footnote{Tables in Lua can be indexed by integers,
    as regular arrays, or by any other Lua data. Moreover, their
    internal representation is able to optimize both array-style and
    hashtable-style usage, and both kinds of keys can be used in the
    same table. In this manual, I'll refer to the integer-indexed part
    of a table as its array-part, and the other one as its hash-part.}
  of the table, i.e. with consecutive integers as keys.
\end{itemize}

\paragraph{Example 1}

The most canonical example of ADT is probably the inductive list. Such
a list is described either as the empty list \verb+Nil+, or a pair
(called a \verb+cons+ in Lisp) of the first element on one side
(\verb+car+ in Lisp), and the list of remaining elements on the other
side (\verb+cdr+ in Lisp). These will be represented in Lua as
\verb+{ tag = "Nil" }+ and {\tt\{ tag = "Cons", car, cdr \}}. The list
(1, 2, 3) will be represented as:

\begin{verbatim}
{ tag="Cons", 1,
  { tag="Cons", 2,
    { tag="Cons", 3,
      { tag="Nil" } } } }
\end{verbatim}

\paragraph{Example 2}

Here is a more programming language oriented example: imagine that we
are working on a symbolic calculator. We will have to work this:
\begin{itemize}
\item litteral numbers, represented as integers;
\item symbolic variables, represented by the string of their
  symbol;
\item formulae, i.e. numbers, variables an/or sub-formulae
  combined by operators. Such a formula is represented by the symbol
  of its operator, and the sub-formulae / numbers / variables it
  operates on.
\end{itemize}
Most operations, e.g. evaluation or simplification, will do different
things depending on whether it is applied on a number, a variable or a
formula. Moreover, the meaning of the fields in data structures depends
on that data type. The datatype is given by the name put in the
\verb+tag+ field. In this example, \verb+tag+ can be one of
\verb+Number+, \verb+Var+ or \verb+Formula+. The formula $e^{i\pi}+1$
would be encoded as:
\begin{verbatim}
{ tag="Formula", "Addition",
  { tag="Formula", "Exponent",
    { tag="Variable", "e" },
    { tag="Formula", "Multiplication",
      { tag="Variable", "i" },
      { tag="Variable", "pi" } } },
  { tag="Number", 1 } }
\end{verbatim}

\paragraph{Syntax}

The simple data above already has a quite ugly representation, so here
are the syntax extensions we provide to represent trees in a more
readable way:

\begin{itemize}
\item The tag can be put in front of the table, prefixed with a
  backquote. For instance, {\tt\{ tag = "Cons", car, cdr \}} can be
  abbreviated as {\tt`Cons\{ car, cdr \}}.
\item If the table contains nothing but a tag, the braces can be
  omitted. Therefore, \verb+{ tag = "Nil" }+ can be abbreviated as
  \verb+`Nil+ (although \verb|`Nil{ }| is also legal).
\item If there is only one element in the table besides the tag, and
  this element is a literal number or a literal string, braces can be
  omitted. Therefore {\tt\{ tag = "Foo", "Bar" \}} can be abbreviated
  as \verb+`Foo "bar"+.
\end{itemize}

With this syntax sugar, the $e^{i\pi}+1$ example above would read:
\begin{verbatim}
`Formula{ "Addition",
   `Formula"{ "Exponent",
      `Variable "e",
      `Formula{ "Multiplication",
                `Variable "i",
                `Variable "pi" } },
   `Number 1 }
\end{verbatim}

Notice that this is a valid description of some tree structure in metalua, but
it's not a representation of metalua code: metalua code is represented as tree
structures indeed, but a structure different from this example's one. In other
words, this is an ADT, but not an AST.

For the record, the metalua (AST) representation of the code {\tt"1+e\^\ (i*pi)"}
is:
\begin{verbatim}
`Op{ "add", `Number 1,
     `Op{ "pow", `Id "e",
          `Op{ "mul", `Id "i", `Id "pi" } } }
\end{verbatim}

After reading more about AST definition and manipulation tools, you'll hopefully
be convinced that the latter representation is more powerful.

\subsection{Abstract Syntax Trees (AST)}

An AST is an Abstract Syntax Tree, a data representation of source
code suitable for easy manipulation. AST are just a particular usage
of ADT, and we will represent them with the ADT syntax described
above.

\paragraph{Example} this is the tree representing the source code
\verb+print(foo, "bar")+:

\verb+`Call{ `Id "print", `Id "foo", `String "bar" }+

Metalua tries, as much as possible, to shield users from direct AST
manipulation, and a thorough knowledge of them is generally not
needed. Metaprogrammers should know their general form, but it is
reasonnable to rely on a cheat-sheet to remember the exact details of
AST structures. Such a summary is provided
in appendix of this tutorial, as a reference when dealing with them.

In the rest of this section, we will present the translation from Lua
source to their corresponding AST.

\subsection{AST  $\Longleftrightarrow$ Lua source translation}

This subsection explains how to translate a piece of lua source code
into the corresponding AST, and conversely. Most of time, users will
rely on a mechanism called quasi-quotes to produce the AST they will
work with, but it is sometimes necessary to directly deal with AST,
and therefore to have at least a superficial knowledge of their
structure.

\subsubsection{Expressions}

The expressions are pieces of Lua code which can be evaluated to give a
value. This includes constants, variable identifiers, table
constructors, expressions based on unary or binary operators, function
definitions, function calls, method invocations, and index selection
from a table.

Expressions should not be confused with statements: an expression has
a value with can be returned through evaluation, whereas statements
just execute themselves and change the computer state (mainly memory
and IO). For instance, \verb|2+2| is an expression which evaluates to
4, but \verb|four=2+2| is a statement, which sets the value of
variable \verb|four| but has no value itself.

\paragraph{Number constants}
A number is represented by an AST with tag \verb+Number+ and the
number value as its sole child. For instance, \verb+6+ is represented
by \verb+`Number 6+\footnote{As explained in the section about ADT,
  {\tt `Number 6} is exactly the same as {\tt `Number\{ 6 \}}, or
  plain Lua {\tt\{ tag="Number", 6\}} }.

\paragraph{String constants}
A string is represented by an AST with tag \verb+String+ and the
string as its sole child. For instance, \verb+"foobar"+ is
represented by:\\
\verb+`String "foobar"+.

\paragraph{Variable names}
A variable identifier is represented by an AST with tag \verb+Id+ and the
number value as its sole child. For instance, variable \verb+foobar+ is
represented by \verb+`Id "foobar"+.

\paragraph{Other atomic values}
Here are the translations of other keyword-based atomic values:
\begin{itemize}
\item \verb+nil+ is encoded as \verb+`Nil+\footnote{which is a
  short-hand for {\tt`Nil\{ \}}, or {\tt\{ tag="Nil" \}} in plain Lua.};
\item \verb+false+ is encoded as \verb+`False+;
\item \verb+true+ is encoded as \verb+`True+;
\item \verb+...+ is encoded as \verb+`Dots+.
\end{itemize}

\paragraph{Table constructors}
A table constructor is encoded as:

\verb+`Table{ ( `Pair{ expr expr } | expr )* }+

This is a list, tagged with \verb+Table+, whose elements are either:
\begin{itemize}
\item the AST of an expression, for array-part entries without an
  explicit associated key;
\item a pair of expression AST, tagged with \verb+Pair+: the first
  expression AST represents a key, and the second represents the value
  associated to this key.
\end{itemize}

\subparagraph{Examples}
\begin{itemize}

\item The empty table \verb+{ }+ is represented as \verb+`Table{ }+;

\item \verb+{1, 2, "a"}+ is represented as:\\
  \verb+`Table{ `Number 1, `Number 2, `String "a" }+;

\item \verb+{x=1, y=2}+ is syntax sugar for \verb+{["x"]=1, ["y"]=2}+,
  and is represented by {\tt`Table\{ `Pair\{ `String "x", `Number 1
    \}, `Pair\{ `String "y", `Number 2\} \}};

\item indexed and non-indexed entries can be mixed:
  \verb+{ 1, [100]="foo", 3}+ is represented as {\tt`Table\{ `Number
    1, `Pair\{ `Number 100, `String "foo"\}, `Number 3 \}};

\end{itemize}

\paragraph{Binary Operators}
Binary operations are represented by {\tt`Op\{ operator, left,
  right\}}, where \verb+operator+ is a the operator's name as one of
the strings below, \verb+left+ is the AST of the left operand, and
\verb+right+ the AST of the right operand.

The following table associates a Lua operator to its AST name:

\begin{center}
\begin{tabular}{|c|c||c|c||c|c||c|c|}
  \hline
  \bf Op. & \bf AST &
  \bf Op. & \bf AST &
  \bf Op. & \bf AST &
  \bf Op. & \bf AST \\

  \hline\hline %%%%%%%%%%%%%%%%%
  \verb|+|   & \verb+"add"+    &
  \verb+-+   & \verb+"sub"+    &
  \verb+*+   & \verb+"mul"+    &
  \verb+/+   & \verb+"div"+    \\
  \hline %%%%%%%%%%%%%%%%%%%%%%%
  \verb+%+   & \verb+"mod"+    &
  \verb+^+   & \verb+"pow"+    &
  \verb+..+  & \verb+"concat"+ &
  \verb+==+  & \verb+"eq"+     \\
  \hline %%%%%%%%%%%%%%%%%%%%%%%
  \verb+<+   & \verb+"lt"+     &
  \verb+<=+  & \verb+"le"+     &
  \verb+and+ & \verb+"and"+    &
  \verb+or+  & \verb+"or"+     \\
  \hline %%%%%%%%%%%%%%%%%%%%%%%
\end{tabular}
\end{center}

Operator names are the sames as the corresponding Lua metatable entry,
without the prefix {\tt"\_\,\_"}. There are no operator for operators
\verb+~=+, \verb+>=+ and \verb+>+: they can be simulated by swapping
the arguments of \verb+<=+ and \verb+<+, or adding a \verb+not+ to
operator \verb+==+.

\subparagraph{Examples}
\begin{itemize}
\item \verb|2+2| is represented as
  \verb|`Op{ 'add', `Number 2, `Number 2 }|;
\item \verb|1+2*3| is represented as:\\[-2em]
\begin{verbatim}
`Op{ 'add', `Number 1,
     `Op{ 'mul', `Number 2, `Number 3 } }
\end{verbatim}
\item \verb|(1+2)*3| is represented as:\\[-2em]
\begin{verbatim}
`Op{ 'mul, `Op{ 'add', `Number 1, `Number 2 },
     `Number 3 } }
\end{verbatim}

  \verb|`Op{ 'mul', `Op{ 'add', `Number 1, `Number 2 }, `Number 3 }|
\item \verb|x>=1 and x<42 | is represented as:\\[-2em]
\begin{verbatim}
`Op{ 'and', `Op{ 'le', `Number  1, `Id "x" },
            `Op{ 'lt', `Id "x", `Number 42 } }

\end{verbatim}
\end{itemize}

\paragraph{Unary Operators}
Unary operations are similar to binary operators, except that they
only take the AST of one subexression. The following table associates
a Lua unary operator to its AST:

\begin{center}
\begin{tabular}{|c|c||c|c||c|c|}
  \hline
  \bf Op. & \bf AST &
  \bf Op. & \bf AST &
  \bf Op. & \bf AST \\

  \hline\hline %%%%%%%%%%%%%%
  \verb+-+   & \verb+"unm"+ &
  \verb+#+   & \verb+"len"+ &
  \verb+not+ & \verb+"not"+ \\
  \hline %%%%%%%%%%%%%%%%%%%%
\end{tabular}
\end{center}

\subparagraph{Examples}
\begin{itemize}
\item \verb|-x| is represented as \verb|`Op{ 'unm', `Id "x" }|;
\item \verb|-(1+2)| is represented as:\\
  \verb|`Op{ 'unm', `Op{ 'add', `Number 1, `Number 2 } }|
\item \verb|#x| is represented as
  \verb|`Op{ 'len', `Id "x" }|
\end{itemize}

\paragraph{Indexed access}
They are represented by an AST with tag \verb+Index+, the table's AST
as first child, and the key's AST as second child.

\subparagraph{Examples}
\begin{itemize}
\item \verb+x[3]+ is represented as \verb+`Index{ `Id "x", `Number 3 }+;
\item \verb+x[3][5]+ is represented as:\\
  \verb+`Index{ `Index{ `Id "x", `Number 3 }, `Number 5 }+
\item \verb+x.y+ is syntax sugar for \verb+x["y"]+, and is represented as:\\
  \verb+`Index{ `Id "x", `String "y" }+
\end{itemize}

Notice that index AST can also appear as left-hand side of
assignments, as shall be shown in the subsection dedicated to
statements.

\paragraph{Function call}
Function call AST have the tag \verb+Call+, the called function's AST
as first child, and its arguments as remaining children.

\subparagraph{Examples}
\begin{itemize}
\item \verb+f()+ is represented as \verb+`Call{ `Id "f" }+;
\item \verb+f(x, 1)+ is represented as
  \verb+`Call{ `Id "f", `Id "x", `Number 1 }+;
\item \verb+f(x, ...)+ is represented as
  \verb+`Call{ `Id "f", `Id "x", `Dots }+.
\end{itemize}

Notice that function calls can be used as expressions, but also as statements.

\paragraph{Method invocation}
Method invocation AST have the tag \verb+Invoke+, the object's AST as
first child, the string name of the method as a second child, and
the arguments as remaining children.

\subparagraph{Examples}
\begin{itemize}
\item \verb+o:f()+ is represented as \verb+`Invoke{ `Id "o", String "f" }+;
\item \verb+o:f(x, 1)+ is represented as:\\
  \verb+`Invoke{ `Id "o", `String "f", `Id "x", `Number 1 }+;
\item \verb+o:f(x, ...)+ is represented as:\\
  \verb+`Invoke{ `Id "o", `String "f", `Id "x", `Dots }+;
\end{itemize}

Notice that method invocations can be used as expressions, but also as
statements.  Notice also that ``{\tt function o:m (x) return x end}'' is
not a method invocation, but syntax sugar for statement
``{\tt o["f"] = function (self, x) return x end}''. See the paragraph
about assignment in statements subsection for its AST representation.

\paragraph{Function definition}
A function definition consists of a list of parameters and a block of
statements. The parameter list, which can be empty, contains only
variable names, represented by their \verb+`Id{...}+ AST, except for
the last element of the list, which can also be a dots AST \verb+`Dots+
(to indicate that the function is a vararg function).

The block is a list of statement AST, optionnaly terminated with a
\verb+`Return{...}+ or \verb+`Break+ pseudo-statement. These
pseudo-statements will be described in the statements subsection.

FIXME: finally, return and break will be considered as regular
statements: it's useful for many macros.

The function definition is encoded as
\verb+`Function{ parameters block }+

\subparagraph{Examples}
\begin{itemize}

\item \verb+function (x) return x end+ is represented as:\\
  \verb+`Function{ { `Id x } { `Return{ `Id "x" } } }+;

\item \verb+function (x, y) foo(x); bar(y) end+ is represented as:
\begin{verbatim}
`Function{ { `Id x, `Id y }
           { `Call{ `Id "foo", `Id "x" },
             `Call{ `Id "bar", `Id "y" } } }
\end{verbatim}

\item \verb+function (fmt, ...) print (string.format (fmt, ...)) end+
  is represented as:
\begin{verbatim}
`Function{ { `Id "fmt", `Dots }
           { `Call{ `Id "print",
                    `Call{ `Index{ `Id "string",
                                   `String "format" },
                           `Id "fmt",
                           `Dots } } } }
\end{verbatim}

\item \verb+function f (x) return x end+ is not an expression, but a
  statement: it is actually syntax sugar for the assignment {\tt f =
    function (x) return x end}, and as such, is represented as:
\begin{verbatim}
`Let{ { `Id "f" },
      { `Function{ {`Id 'x'} {`Return{`Id 'x'} } } } }
\end{verbatim}
  (see assignment in the statements subsection for more details);

\end{itemize}

\paragraph{Parentheses}

In Lua, parentheses are sometimes semantically meaningful: when the
parenthesised expression returns multiple values, putting it between
parentheses foreces it to return only one value. For instance, ``{\tt
  local function f() return 1, 2, 3 end; return \{ f() \}}'' will
return ``{\tt\{1, 2, 3\}}'', whereas ``{\tt local function f() return
  1, 2, 3 end; return \{ (f()) \}}'' will return ``{\tt\{ 1 \}}''
(notice the parentheses around the function call).

Parentheses are represented in the AST as a node ``{\tt`Paren\{
  \}}''. The second example above has the following AST:

\begin{verbatim}
{ `Localrec{ { `Id "f" },
             { `Function{ { },
                          `Return{ `Number 1,
                                   `Number 2,
                                   `Number 3 } } } },
  `Return{ `Table{ `Paren{ `Call{ `Id "f" } } } } }
\end{verbatim}

\subsubsection{Statements}

Statements are instructions which modify the state of the
computer. There are simple statement, such as variable assignment,
local variable declaration, function calls and method invocation;
there are also control structure statements, which take simpler
statement and modify their action: these are if/then/else,
repeat/until, while/do/end, for/do/end and do/end statements.

\paragraph{Assignment}
Variable assignment \verb+a, b, c = foo, bar+ is represetned by AST
\verb+`Set{ lhs, rhs }+, with {\tt lhs} being a list of variables or
table indexes, and {\tt rhs} the list of values assigned to them.

\subparagraph{Examples}
\begin{itemize}

\item \verb+x[1]=2+ is represented as:\\
  \verb+`Set{ { `Index{ `Id "x", `Number 1 } }, { `Number 2 } }+;

\item \verb+a, b = 1, 2+ is represented as:\\
  \verb+`Set{ { `Id "a",`Id "b" }, { `Number 1, `Number 2 } }+;

\item \verb+a = 1, 2, 3+ is represented as:\\
  \verb+`Set{ { `Id "a" }, { `Number 1, `Number 2, `Number 3 } }+;

\item \verb+function f(x) return x end+ is syntax sugar for:\\
  \verb+f = function (x) return x end+. As such, is represented as:\\[-2em]
\begin{verbatim}
`Set{ { `Id "f" },
      { `Function{ {`Id 'x'}  {`Return{ `Id "x" } } } } }
\end{verbatim}

\item \verb+function o:m(x) return x end+ is syntax sugar for:\\
  \verb+o["f"] = function (self, x) return x end+, and as such, is
  represented as:\\[-2em]
\begin{verbatim}
`Set{ { `Index{ `Id "o", `String "f" } },
      { `Function{ { `Id "self, "`Id x }
                   { `Return{ `Id "x" } } } } }
\end{verbatim}

\end{itemize}

\paragraph{Local declaration}
Local declaration \verb+local a, b, c = foo, bar+ works just as
assignment, except that the tag is \verb+Local+, and it is allowed to
have an empty list as values.

\subparagraph{Examples}
\begin{itemize}

\item \verb+local x=2+ is represented as:\\
  \verb+`Local{ { `Id "x" }, { `Number 2 } }+;

\item \verb+local a, b+ is represented as:\\
  \verb+`Local{ { `Id "a",`Id "b" }, { } }+;

\end{itemize}

\paragraph{Recursive local declaration}
In a local declaration, the scope of local variables starts {\em
  after} the statement. Therefore, it is not possible to refer to a
variable inside the value it receives, and
``{\tt local function f(x) f(x) end}'' is not equivalent to
``{\tt local f = function (x) f(x) end}'': in the latter, the \verb|f|
call inside the function definition probably refers to some global
variable, whereas in the former, it refers to the local variable
currently being defined (f this therefore a forever looping function).

To handle this, the AST syntax defines a special \verb|`Localrec|
local declaration statement, in which the variables enter in scope
{\em before} their content is evaluated. Therefore, the AST
corresponding to {\tt local function f(x) f(x) end} is:
\begin{verbatim}
`Localrec{ { `Id "f" },
           { `Function{ { `Id x }
                        { `Call{ `Id "f", `Id "x" } } } } }
\end{verbatim}

\caveat{In the current implementation, both variable names list and
  values list have to be of lenght 1. This is enough to represent
  {\tt local function ... end}, but should be generalized in the
  final version of Metalua.}

\paragraph{Function calls and method invocations}
They are represented the same way as their expression counterparts,
see the subsection above for details.

\paragraph{Blocks and pseudo-statements}
Control statements generally take a block of instructions as
parameters, e.g. as the body of a \verb|for| loop. Such statement
blocks are represented as the list of the instructions they
contain. As a list, the block itself has no \verb|tag| field.

\subparagraph{Example}
\verb|foo(x); bar(y); return x,y| is represented as:
\begin{verbatim}
{ `Call{ `Id "foo", `Id "x" },
  `Call{ `Id "bar", `Id "y" },
  `Return{ `Id "x", `Id "y" } }
\end{verbatim}

\paragraph{Do statement}
These represent \verb|do ... end| statements, which limit local
variables scope. They are represented as
blocks with a \verb|Do| tag.

\subparagraph{Example}
\verb|do foo(x); bar(y); return x,y end| is represented as:
\begin{verbatim}
`Do{ `Call{ `Id "foo", `Id "x" },
     `Call{ `Id "bar", `Id "y" },
     `Return{ `Id "x", `Id "y" } }
\end{verbatim}

\paragraph{While statement}
\verb|while <foo> do <bar1>; <bar2>; ... end| is represented as \\
\verb|`While{ <foo>, { <bar1>, <bar2>, ... } }|.

\paragraph{Repeat statement}
\verb|repeat <bar1>; <bar2>; ... until <foo>| is represented as \\
\verb|`Repeat{ { <bar1>, <bar2>, ... }, <foo> }|.

\paragraph{For statements}

{\tt for x=<first>,<last>,<step> do <foo>; <bar>; ... end} is
represented as {\tt `Fornum\{ `Id "x", <first>, <last>, <step>, \{
  <foo>, <bar>, ... \} \}}.

The \verb|step| parameter can be omitted if equal to 1.

\begin{verbatim}
for x1, x2... in e1, e2... do
  <foo>;
  <bar>;
  ...
end
\end{verbatim}
isrepresented as:\\
{\tt `Forin\{ \{`Id "x1",`Id "x2",...\}, \{ <e1>, <e2>,... \} \{
 <foo>, <bar>, ... \} \}}.

\paragraph{If statements}
``If'' statements are composed of a series of (condition, block)
pairs, and optionnaly of a last default ``else'' block. The conditions
and blocks are simply listed in an \verb|`If{ ... }| ADT. Notice that
an ``if'' statement without a final ``else'' block will have an even
number of children, whereas a statement with a final ``else'' block
will have an odd number of children.

\subparagraph{Examples}
\begin{itemize}

\item \verb+if <foo> then <bar>; <baz> end+ is represented as:\\
  \verb+`If{ <foo>, { <bar>, <baz> } }+;

\item \verb+if <foo> then <bar1> else <bar2>; <baz2> end+
  is represented as:
  \verb+`If{ <foo>, { <bar1> }, { <bar2>, <baz2> } }+;

\item
  \verb+if <foo1> then <bar1>; <baz1> elseif <foo2> then <bar2>; <baz2> end+
  \\ is represented as: \\
  \verb+`If{ <foo1>, { <bar1>, <baz1> }, <foo2>,{ <bar2>, <baz2> } }+;

\item
\begin{verbatim}
if     <foo1> then <bar1>; <baz1>
elseif <foo2> then <bar2>; <baz2>
else               <bar3>; <baz3> end+
\end{verbatim}
is represented as:
\begin{verbatim}
`If{ <foo1>, { <bar1>, <baz1> },
     <foo2>, { <bar2>, <baz2> },
             { <bar3>, <baz3> } }
\end{verbatim}

\end{itemize}

\paragraph{Breaks and returns}
Breaks are represented by the childless \verb|`Break| AST. Returns are
retpresented by the (possibly empty) list of returned values.

\subparagraph{Example}
{\tt return 1, 2, 3} is represented as:\\
{\tt`Return\{ `Number 1, `Number 2, `Number 3 \}}.

\subsubsection{Extensions with no syntax}

A couple of AST nodes do not exist in Lua, nor in Metalua native
syntax, but are provided because they are particularly useful for
writing macros. They are presented here.

\paragraph{Goto and Labels}

Labels can be string AST, identifier AST, or simply string; they
indicate a target for goto statements.  A very common idiom is ``{\tt
  local x = mlp.gensym(); ... `Label\{ x \} }''. You just jump to that
label with ``{\tt `Goto\{ x \} }''.

Identifiers, string AST or plain strings are equivalent:
``{\tt`Label\{ `Id "foo"\}}'' is synonymous for ``{\tt`Label\{ `String
  "foo"\}}'' and ``{\tt`Label "foo"}''. The same equivalences apply
for gotos, of course.

Labels are local to a function; you can safely jump out of a block,
but if you jump {\em inside} a block, you're likely to get into unspecified
trouble, as local variables will be in a random state.

\paragraph{Statements in expressions}
A common need when writing a macro is to insert a statement in the
middle of an expression. It can be done by using an anonymous function
closure, but that would be expensive, so Metalua offers a better
solution. The \verb|`Stat| node evaluates a statement block in the
middle of an expression, then returns an arbitrary expression as its
result. Notice one important point: the expression is evaluated in
the block's context, i.e. if there are some local variables declared
in the block, the expression can use them.

For instance, {\tt `Stat\{ +\{local x=3\}, +\{x\}\}} evaluates to 3.

%\subsubsection{Formal translation defintion}

%\caveat{FIXME: here should go a formal, inductive definition of
%  AST/syntax translation, to serve as a reference}


\chapter{Semidefinite Programming}\label{ch3}
\epigraph{\textit{Science is knowledge which we understand so well that we can teach it to a computer; and if we don't fully understand something, it is an art to deal with it.}}{Donald E. Knuth}

The world of convex optimization is a rich and interesting world.

Please read \Vref{paper1}. Or simply \cref{paper1}.

% vim: spell spelllang=en syntax=tex tw=140


\section{The Microconnectome}
Here we introduce the Bio-M MC. This is one of the microcircuits that the BBP reconstructed. It represents the mean measurements of a set of 5 other varying instantiations of a rat's neocortical column.
\subsection{Bio-M MC}
\subsubsection{Origins of the Bio-M MC}
The Blue Brain Project have created multiple variants of a rat's neocortical column (a microconnectome). These are  comprised of 6 sets of 7 statistically varying instantiations. The first 5 of these sets were based on specific heights of the layers in the neocortex. These instantiations ultimately gave rise to a set of average instantiations of the neocortical column. The instance that focus on here is the final instance in this set of averages, the Bio-M MC. This instance of the neocortical column contains 6 layers, that are made up from a total of 55 different morphological neuron types. These can be further differentiated into excitatory and inhibitory neurons. The Bio-M MC representing the circuit itself contains approximately 7.8 million synaptic connections represented as directed edges that connect the 31,346 neurons represented as vertices.

\begin{figure}[H]
\begin{center}
\captionsetup{justification=centering}
\includegraphics[width=12cm]{BioM/matrix_BioM.png}
\caption{Adjacency Matrix for the Bio-M MC}
\end{center}
\end{figure}
%----------------------
\subsubsection{Block Layout}
\begin{figure}[H]%
    \centering
    \captionsetup{justification=centering}
    \subfloat[\centering Block-wise Edge Densities]{{\includegraphics[width=7cm]{BioM/heat_map_layer_BioM_dens_sci.png} }}%
    \qquad
    \subfloat[\centering Block-wise Edge Counts]{{\includegraphics[width=7cm]{BioM/heat_map_layer_BioM.png} }}%
    \caption{Connectivity by block of the Bio-M MC}%
    \label{fig:example}%
\end{figure}

To help visualise the MC, in Figure 5, we have a graphical representation of the connectivity of the neurons as they pass from the pre-synaptic neuron to the post-synaptic neuron in the form of an adjacency matrix. The rows represent the pre-synaptic neurons and the columns represent the post-synaptic neurons. As can be seen here, there is a large number of these connections in the leading diagonal which represents the connections within the layers as compared to off-diagonal blocks that represent connections between the layers, as can be seen in Figure 6. Furthermore, we see that there is a large number of connections that are contained within layer 6. The number of connections in this block amounts to approximately 27\% of all connections in the Bio-M MC.

The volume of the Bio-M MC is approximately $0.29$mm$^3$. The relative thickness is shown in Figure 7, along with statistical details of each layer shown in Table 2.

\begin{center}
\begin{table}[H]
    \centering
    \captionsetup{justification=centering}
     \begin{tabular}{||c | c | c | c||}
 \hline
 Layer & Thickness(\(\mu\)m) & Neurons & Density(neurons/mm\(^3\)) \\ [0.5ex]
 \hline\hline
 1 & 149 & 339 & 14706 \\
 \hline
 2/3 & 502 & 7519 & 31466 \\
 \hline
 4 & 190 & 4661 & 51074 \\
 \hline
 5 & 525 & 6106 & 24214 \\
 \hline
 6 & 700 & 12722 & 37838 \\
 \hline
 \hline
\end{tabular}
    \caption{Statistical details of the Bio-M MC}
    \label{tab:my_label}
\end{table}

\end{center}
\begin{figure}[H]
\begin{center}
\captionsetup{justification=centering}
\includegraphics[width=10cm]{BioM/connectome.png}
\caption[center]{Neocortical column scaled to account for layer thickness}
\end{center}
\end{figure}

\subsubsection{Distance Distribution}
To see further how the models compare to the original structure of the Bio-M MC, we also want to take a look at distances between the connected neurons to see how much of an impact distance has on the level of connectivity within the MC. Since our models are only dealing with the neurons and the observed synaptic connections that are present in the Bio-M MC, we look only at the functional graph, as shown in Figure 8.

\begin{figure}[H]
\begin{center}
\captionsetup{justification=centering}
\includegraphics[width=10cm]{BioM/BioM_dist_distr.png}
\caption{Distance distribution of connected neurons in the Bio-M MC}
\end{center}
\end{figure}

We have taken the bin sizes to be 75\(\mu\)m, to be consistent with the distances that were proposed in \cite{Reimann_2017}. This is due to 75$\mu$m being the maximum bin size that preserved the distribution of soma-distances of connected neurons in all sub-matrices of the adjacency matrix representation of the MC. A sub-matrix here, is defined by the connections between a morphological neuron to another morphological neuron type. By example, we have L1 DAC to L1 HAC. This is just one example of the 3025 sub-matrices that are contained in the MC. More details of this are given in the section on the General Biological model.

\subsubsection{Signed Degree of Neurons}
In Figure 9, we have the signed degree of the neurons. These are calculated by taking the neurons in-degree minus the out-degree. Therefore, a positive value as shown in Figure 9(a), indicates that the information flow into the neuron is greater than the flow out and vice versa for a negative degree. So, from Figure 9(a), we can see that more flow is directed towards the neurons that are located in layers 1, 2 and 3 and that more flow moves away from the neurons in layers 4, 5 and 6. This, theoretically, is down to the fact that L4 is the major thalamorecipient layer and therefore a starting point of cortical information processing. Subsequently, information is then propagated through the cortical column, firstly to L2/3, then from there to L5 and L6, which are the primary output layers of the cortex \cite{10.3389/fnana.2017.00091}.

To show that the graph representing the network is finite, we can take the sum of signed degree for each neuron over the set of neurons, $\sum_{v \in \mathcal{G}}\sd(\emph{v}) = 0 \enspace{.}$ Figure 9(b) shows the cumulative sum of the signed degree of each neuron throughout the MC. All models barring the \ER model follow this pattern as we aim to preserve this.

\begin{figure}[H]%
    \centering
    \captionsetup{justification=centering}
    \subfloat[\centering Signed Degree of each neuron in Bio-M MC]{{\includegraphics[width=7cm]{BioM/BioM_sd.png} }}%
    \qquad
    \subfloat[\centering Cumulative sum of signed degree to show graph $\mathcal{G}$ is finite]{{\includegraphics[width=7cm]{BioM/cumsum_degree_BioM.png} }}%
    \caption{Signed Degree statistics for the Bio-M MC}%
    \label{fig:example}%
\end{figure}


\documentclass[12pt,twoside]{article}

\newcommand{\reporttitle}{495 - Advanced Statistical Machine Learning and Pattern Recognition }
\newcommand{\reportauthor}{Jiahao Lin}
\newcommand{\reporttype}{Coursework 1}
\newcommand{\cid}{00837321}

% include files that load packages and define macros
\input{includes} % various packages needed for maths etc.
\input{notation} % short-hand notation and macros

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
% front page
\input{titlepage}

%%%%%%%%%%%%%%%%%%%%%%%%%%%% Main document

\section{Introduction}
This is a template for coursework submission. Many macros and definitions can be found in \texttt{notation.tex}. This document is not an introduction to LaTeX. General advice if get stuck: Use your favorite search engine. A great source is also \mbox{\url{https://en.wikibooks.org/wiki/LaTeX}}.

\section{Basics}

\subsection{Figures}
A figure can be included as follows:
\begin{figure}[tb]
\centering % this centers the figure
\includegraphics[width = 0.7\hsize]{./figures/imperial} % this includes the figure and specifies that it should span 0.7 times the horizontal size of the page
\caption{This is a figure.} % caption of the figure
\label{fig:imperial figure} % a label. When we refer to this label from the text, the figure number is included automatically
\end{figure}
Fig.~\ref{fig:imperial figure} shows the Imperial College logo.

Some guidelines:
\begin{itemize}
\item Always use vector graphics (scale free)
\item In graphs, label the axes
\item Make sure the font size (labels, axes) is sufficiently large
\item When using colors, avoid red and green together (color blindness)
\item Use different line styles (solid, dashed, dotted etc.) and different markers to make it easier to distinguish between lines
\end{itemize}

\subsection{Notation}
\begin{table}[tb]
\caption{Notation}
\label{tab:notation}
\centering
\begin{tabular}{ll}
Scalars & $x$\\
Vectors & $\vec x$\\
Matrices & $\mat X$\\
Transpose & $\T$\\
Inverse & $\inv$\\
Real numbers & $\R$\\
Expected values & $\E$\\
\end{tabular}
\end{table}
Table~\ref{tab:notation} lists some notation with some useful shortcuts (see latex source code).

\subsubsection{Equations}
Here are a few guidelines regarding equations
\begin{itemize}
\item Please use the \texttt{align} environment for equations (\texttt{eqnarray} is buggy)
\item Please number all equations: It will make things easier when we need to refer to equation numbers. If you always use the \texttt{align} environment, equations are numbered by default.
\item Vectors are by default column vectors, and we write
\begin{align}
\vec x &= \colvec{1,2}
\end{align}
\item Note that the same macro (\texttt{$\backslash$colvec}) can produce vectors of variable lengths, as
\begin{align}
\vec y &= \colvec{1,2,3,4}
\end{align}
\item Matrices can be created with the same command. The \& switches to the next column:
\begin{align}
\mat A = \begin{bmatrix}
1 & 2 & 3\\
3 & 4 & 5
\end{bmatrix}
\end{align}
\item Determinants. We provide a simple macro (\texttt{$\backslash$matdet}) whose argument is just a matrix array:
\begin{align}
\matdet{
1 & 2 & 3\\
3 & 4 & 5\\
2 & 2 & 2
}
\end{align}
\item If you do longer manipulations, please explain what you are doing: Try to avoid sequences of equations without text breaking up. Here is an example:
We consider
\begin{align}
U_1 = [\colvec{1,1,0,0},\, \colvec{0,1,1,0},\, \colvec{0,0,1,1}]
\subset\R^4, \quad
U_2 = [\colvec{-1,1,2,0},\, \colvec{0,1,0,0}]
\subset\R^4\,.
\end{align}
To find a basis of $U_1\cap U_2$, we need to find all $\vec x \in V$ that can be represented as linear combinations of the basis vectors of $U_1$ and $U_2$, i.e.,
\begin{align}
\sum_{i=1}^3 \lambda_i \vec b_i = \vec x = \sum_{j=1}^2 \psi_j \vec c_j\,,
\end{align}
where $\vec b_i$ and $\vec c_j$ are the basis vectors of $U_1$ and $U_2$, respectively.
%
The matrix $\mat A = [\vec b_1|\vec b_2|\vec b_3| -\vec c_1|-\vec
c_2]$ is given as
\begin{align}
\mat A =
\begin{bmatrix}
1 & 0 & 0 & 1 & 0\\
1 & 1 & 0 & -1 & -1\\
0 & 1 & 1 & -2 & 0\\
0 & 0 & 1 & 0 & 0
\end{bmatrix}\,.
\end{align}
By using Gaussian elimination, we determine the corresponding reduced row echelon form
\begin{align}
\begin{bmatrix}
1 & 0 & 0 & 1& 0\\
0 & 1 & 0 & -2 & 0\\
0 & 0 & 1 & 0 & 0\\
0 & 0 & 0 & 0 & 1
\end{bmatrix}
\,.
\end{align}
We keep in mind that we are interested in finding $\lambda_1,\lambda_2,\lambda_3\in\R$ and/or $\psi_1,\psi_2\in\R$ with
\begin{align}
\begin{bmatrix}
1 & 0 & 0 & 1& 0\\
0 & 1 & 0 & -2 & 0\\
0 & 0 & 1 & 0 & 0\\
0 & 0 & 0 & 0 & 1
\end{bmatrix}
\colvec{\lambda_1, \lambda_2, \lambda_3, \psi_1, \psi_2}
=\vec 0\,.
\end{align}
From here, we can immediately see that $\psi_2=0$ and $\psi_1\in\R$ is
a free variable since it corresponds to a non-pivot column, and our solution is
\begin{align}
U_1\cap U_2 = \psi_1\vec c_1 =  [ \colvec{-1,1,2,0} ]
\,, \quad \psi_1\in\R\,.
\end{align}
\end{itemize}

\section{Exercise I}
\renewcommand{\thesubsection}{\thesection.\alph{subsection}}
\subsection{}

\subsection{}
\begin{enumerate}[(ii)]
\item

random
init:
('init ', {'covars_1': array([[ 0.09596712,  0.18844958],
       [ 0.18844958,  0.41920324]]), 'covars_3': array([[ 0.57487025,  0.26014628],
       [ 0.26014628,  0.12539179]]), 'covars_2': array([[ 0.53174001,  0.51272331],
       [ 0.51272331,  0.62463347]]), 'covars_4': array([[ 0.08396639,  0.12883398],
       [ 0.12883398,  0.867649  ]]), 'mixCoeff_2': array([ 0.47532638]), 'mixCoeff_3': array([ 0.06450683]), 'mixCoeff_1': array([ 0.70248209]), 'mixCoeff_4': array([ 0.59740769]), 'means_4': array([ 0.62499559,  0.8922187 ]), 'means_1': array([ 0.67007467,  0.75286517]), 'means_2': array([ 0.83341803,  0.24653981]), 'means_3': array([ 0.07098854,  0.42394428])})

10:
{'covars_1': array([[ 0.40580311,  0.42837243],
       [ 0.42837243,  0.7671041 ]]), 'covars_3': array([[ 0.26527802, -0.04077459],
       [-0.04077459,  0.21586667]]), 'covars_2': array([[ 3.03794059,  1.65701141],
       [ 1.65701141,  9.85590337]]), 'covars_4': array([[ 0.46536999,  0.00949023],
       [ 0.00949023,  0.4514964 ]]), 'mixCoeff_2': array([ 0.70401117]), 'mixCoeff_3': array([ 0.04683536]), 'mixCoeff_1': array([ 0.02551497]), 'mixCoeff_4': array([ 0.22363849]), 'means_4': array([ 1.98668689,  1.98869637]), 'means_1': array([ 1.82697034,  2.73171692]), 'means_2': array([ 6.63879306,  5.53922754]), 'means_3': array([ 7.00971971,  2.11125597])}

k mean
init:
('init ', {'covars_1': array([[ 1.0247685 ,  0.00324863],
       [ 0.00324863,  1.04889345]]), 'covars_3': array([[ 1.0247685 ,  0.00324863],
       [ 0.00324863,  1.04889345]]), 'covars_2': array([[ 1.0247685 ,  0.00324863],
       [ 0.00324863,  1.04889345]]), 'covars_4': array([[ 1.0247685 ,  0.00324863],
       [ 0.00324863,  1.04889345]]), 'mixCoeff_2': 0.25600000000000001, 'mixCoeff_3': 0.25600000000000001, 'mixCoeff_1': 0.25600000000000001, 'mixCoeff_4': 0.25600000000000001, 'means_4': array([ 7.19484019,  1.62814244]), 'means_1': array([ 5.20891438,  4.99047893]), 'means_2': array([ 2.05777579,  2.1137154 ]), 'means_3': array([ 7.96714603,  9.02735559])})

10:
{'covars_1': array([[ 1.88086728,  0.47798092],
       [ 0.47798092,  0.99318458]]), 'covars_3': array([[ 1.03342452, -0.00648817],
       [-0.00648817,  0.97714424]]), 'covars_2': array([[ 0.4919996 ,  0.00617128],
       [ 0.00617128,  0.45976719]]), 'covars_4': array([[ 1.05385311, -0.48284976],
       [-0.48284976,  1.83194321]]), 'mixCoeff_2': array([ 0.25080839]), 'mixCoeff_3': array([ 0.25010134]), 'mixCoeff_1': array([ 0.25600076]), 'mixCoeff_4': array([ 0.24308951]), 'means_4': array([ 7.0506722 ,  1.87750963]), 'means_1': array([ 5.02277386,  5.01608402]), 'means_2': array([ 1.9873924 ,  2.00719055]), 'means_3': array([ 7.97584259,  9.07257758])}
\end{enumerate}

\section{Exercise II}
In Gaussian Mixture Models, assume given a set of $N$ unlablled data, there $K$ gaussian distributed clustered with different centers $\mu_1 ... \mu_k$ and same covariance matrix $\Sigma$. The probability of a data belong in cluster $l$ is $p(k = l) = \pi_l$.
So we have our parameters:\\
\begin{align}
  & \Sigma  \\
  & \mu_i ... \mu_k  \\
  & p(k=1) ... p(k=K)
\end{align}
And hidden variables:
\begin{align}
 \vec{Z}_n &= \colvec{Z_{n1},Z_{n2},...,Z_{nk}} = [\colvec{1,0,0,...,0},\, \colvec{0,1,0,...,0},\,\colvec{...},\,\colvec{0,0,0,...,1}]
\end{align}
where the probability of a sample in clusters is
\begin{align}
p(\vec{Z}_n) = \prod_{k=1}^{K} \pi_k^{Z_{nk}}
\end{align}
and the probability of a sample $x_n$ given a cluster $k$ is
\begin{align}
p(x_n | Z_{nk} =1, \theta) = N(X_n, \mu_k, \Sigma)
\end{align}
and the probability of a sample $x_n$ is
\begin{align}
p(x_n | \theta) = \sum_{k=1}^{K} p(Z_{nk} = 1)p(X_n | Z_{nk} = 1, \theta) = \sum_{k=1}^{K} \pi_k N(X_n | \mu_k, \Sigma)
\end{align}
Assume all data samples are independent, we first formulate the joint likelihood
\begin{align}
p(X, Z | \theta) = p(x_1, x_2, ..., x_n, z_1, z_2, ..., z_n | \theta)\\
 = \prod_{n=1}^{N} p(x_n|z_n, \theta_x) \prod_{n=1}^{N} p(z_n|\theta_z) \\
 =  \prod_{n=1}^{N} \prod_{k=1}^{K} N(x_n|\mu_k, \Sigma)^{z_{nk}} \prod_{n=1}^{N}  \prod_{k=1}^{K} \pi_k^{z_{nk}}
\end{align}
where the parameters are
\begin{align}
\theta_x &= {\Sigma, \mu_1, \mu_2, ..., \mu_k} \\
\theta_z &= {\pi_1, \pi_2, ..., \pi_k}
\end{align}
In the Expectation Step, we take the log of likelihood
\begin{align}
\ln p(X, Z|\theta) =  \sum_{n=1}^{N} \sum_{k=1}^{K} z_{nk} \left\lbrace\ln N(X_n | \mu_k, \Sigma) + \ln \pi_k \right\rbrace
\end{align}
And then apply the operator $E_{p(Z|X,\theta)}$ which is expectation of the posterior:
\begin{align}
E_{p(Z|X,\theta)} [\ln p(X, Z|\theta)] = \sum_{n=1}^{N} \sum_{k=1}^{K} E_{p(Z|X,\theta)} [Z_{nk}] \left\lbrace\ln N(X_n | \mu_k, \Sigma) + \ln \pi_k \right\rbrace
\end{align}
Now we need to compute $E_{p(Z|X,\theta)} [Z_{nk}]$:
\begin{align}
E_{p(Z|X,\theta)} [Z_{nk}] = E_{p(Z|X,\theta)} [Z_{nk}] \\
= \sum_{z_1}... \sum_{z_n} p(Z|X, \theta_{old}) = \sum_{z_n} z_{nk} p(z_n|x_n, \theta^{old})
\end{align}
where
\begin{align}
p(z_n|x_n, \theta^{old}) = \frac{p(x_n, z_n | \theta^{old})}{p(x_n|\theta^{old})} \\
=  \frac{p(x_n |z_n, \theta^{old})p(z_n|\theta^{old})}{p(x_n|\theta^{old})}\\
= \frac{\prod_{k=1}^{K}N(x_n|\mu_k, \Sigma)^{z_{nk}}{pi_k}^{z_{nk}}}{\sum_{z_n}\prod_{k=1}^{K} N(x_n|\mu_k, \Sigma)^{z_{nk}}\pi_k^{z_{nk}}}
\end{align}
which gives the expectation
\begin{align}
E_{p(Z|X,\theta)} [Z_{nk}] = \frac{\sum_{z_nk} z_{nk} \prod_{k=1}^{K}N(x_n|\mu_k, \Sigma)^{z_{nk}}{pi_k}^{z_{nk}}}{\sum_{z_n}\prod_{k=1}^{K} N(x_n|\mu_k, \Sigma)^{z_{nk}}\pi_k^{z_{nk}}} \\
= \frac{\pi_k N(x_n|\mu_k, \Sigma)}{\sum_{l=1}^{K} \pi_l N(x_n|\mu_l, \Sigma)}
\end{align}
now we see that the expectation is the posterior
\begin{align}
E_{p(Z|X,\theta)} [Z_{nk}] = p(z_{nk}|x_n)
\end{align}
now in the maximization step, we can define
\begin{align}
G(\theta) = E_{p(Z|X,\theta)} [\ln p(X, Z|\theta)] = \\
\sum_{n=1}^{N} \sum_{k=1}^{K} E_{p(Z|X,\theta)} [Z_{nk}] \left\lbrace\ln N(X_n | \mu_k, \Sigma) + \ln \pi_k \right\rbrace =\\
\sum_{n=1}^{N} \sum_{k=1}^{K} \gamma(z_{nk}) \left\lbrace -\frac{1}{2}(x_n - \mu_k)^T \Sigma^{-1}(x_n - \mu_k) -\frac{1}{2}(F\ln 2\pi + \ln |\Sigma |) + \ln \pi_k  \right\rbrace
\end{align}
take the derivative of our cost function with respect to our parameters, and set them to zero:
\begin{align}
\frac{d G(\theta)}{d \mu_k} = \sum_{n=1}^N  \gamma(z_{nk})\Sigma^{-1}(x_n - \mu_k) = 0 \\
\mu_k = \frac{\sum_{n=1}^N  \gamma(z_{nk}) x_n}{\sum_{n=1}^N  \gamma(z_{nk})}\\
\frac{d G(\theta)}{d \Sigma} = \sum_{n=1}^{N} \sum_{k=1}^{K} \gamma(z_{nk})\left\lbrace (x_n - \mu_k)^T (x_n - \mu_k) - \Sigma \right\rbrace = 0\\
\Sigma = \frac{\sum_{n=1}^{N} \sum_{k=1}^{K} \gamma(z_{nk})(x_n - \mu_k)^T (x_n - \mu_k) }{\sum_{n=1}^{N} \sum_{k=1}^{K} \gamma(z_{nk})} \\
= \sum_{n=1}^{N} \sum_{k=1}^{K} \gamma(z_{nk})(x_n - \mu_k)^T (x_n - \mu_k)
\end{align}
Since we have the constraint that
\begin{align}
\sum_{k=1}^K \pi_k = 1
\end{align}
We can define the laplacian function:
\begin{align}
L(\theta) = G(\theta) - \lambda (\sum_{k=1}^K \pi_k - 1) \\
\frac{d L(\theta)}{d \pi_k} = \sum_{n=1}^N \frac{\gamma(z_{nk})}{\pi_k} - \lambda = 0\\
\pi_k = \frac{\sum_{n-1}^N \gamma(z_{nk})}{\lambda}\\
\end{align}
Using the constraint, we can derive the value of $\lambda$
\begin{align}
\sum_{k=1}^K \pi_k = 1 = \frac{\sum_{n=1}^{N} \sum_{k=1}^{K} \gamma(z_{nk}) }{\lambda}\\
\lambda = \sum_{n=1}^{N} \sum_{k=1}^{K} \gamma(z_{nk}) = N\\
\pi_k = \frac{\sum_{n=1}^{N} \gamma(z_{nk})}{N}
\end{align}

\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\documentclass[12pt]{article}

\title{CSC 455 - Assignment 5}
\author{Robert Krency}
\date{\today}

\usepackage{tikz}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{multicol}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{setspace}

% Include some source code
% https://tex.stackexchange.com/questions/348651/c-code-to-add-in-the-document
\usepackage{xcolor}
\usepackage{listings}

\definecolor{mGreen}{rgb}{0,0.6,0}
\definecolor{mGray}{rgb}{0.5,0.5,0.5}
\definecolor{mPurple}{rgb}{0.58,0,0.82}
\definecolor{backgroundColour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{CStyle}{
    backgroundcolor=\color{backgroundColour},
    commentstyle=\color{mGreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{mGray},
    stringstyle=\color{mPurple},
    basicstyle=\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2,
    language=C
}

% Geometry
\usepackage{geometry}
\geometry{letterpaper, left=20mm, top=20mm, right=20mm, bottom=20mm}

% Add vertical spacing to tables
\renewcommand{\arraystretch}{1.4}

\onehalfspacing

% Macros
\newcommand{\definition}[1]{\underline{\textbf{#1}}}

\newenvironment{rcases}
  {\left.\begin{aligned}}
  {\end{aligned}\right\rbrace}

% Begin Document
\begin{document}

\maketitle

\section{Two-Way Parameters}

The following C code was ran on a Windows 10 computer, compiled with GCC 10.3 using the MinGW project.

\begin{lstlisting}[style=CStyle]
    int* fun(int* a) {
        *a += 10;
        return a;
    }

    int main() {
        int a, b;
        a = 10;
        b = a + *fun(&a);
        printf("With the function call on the right,");
        printf(" b is: %d\n", b);

        a = 10;
        b = *fun(&a) + a;
        printf("With the function call on the left,");
        printf(" b is: %d\n", b);
    }
\end{lstlisting}

The purpose here is to determine when the parameter $a$ has its value fetched for the addition operation, and if there
is any difference in calling a function that modifies its value before or after the initial reference.
The results are shown below:

\vspace{5mm}

\begin{center}
    \includegraphics{a5-params.png}
\end{center}

As can be seen, the function $fun$ is ran prior to any operations taken place in the assignment statement of $b$.
For this configuration, there was no difference.
The variable $a$ was modified prior to the values being fetched for the addition operation.

\pagebreak

\section{Bonus}

The following C code was ran on a Windows 10 computer, compiled with GCC 10.3 using the MinGW project.

\begin{lstlisting}[style=CStyle]
    int x = 0;
    if (x == 0 && x++==1);
    printf("X is %d\n",x);
    if (x == 3 && x++==2);
    printf("X is %d\n", x);
\end{lstlisting}

The purpose here is to determine if short circuiting is happening inside the conditional statements.
The results are shown below:

\vspace{5mm}

\begin{center}
    \includegraphics{a5-bonus.png}
\end{center}

As can be seen, the first conditional statement checked both input conditions, which resulted in the incrememnt of $x$ being executed in the first conditional.
However, as the $x == 3$ condition fails in the second conditional, the second increment of $x$ is never called.
This leads to the conclusion that short circuiting does indeed happen.

\end{document}