% standard LaTeX packages
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[margin=3cm]{geometry}
\usepackage[hidelinks]{hyperref}

% math packages
\usepackage{mathtools,amsfonts,amssymb,mathdots}
\usepackage{siunitx}

% plotting and tables
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{caption}

% listings
\usepackage[euler]{textgreek}
\usepackage{xcolor}
\usepackage{listings}
\definecolor{background}{gray}{0.95}
\definecolor{comment}{rgb}{0,0.5,0}
\colorlet{keyword}{blue}
\colorlet{string}{red}
\lstset{numbers=left,
  numberstyle=\tiny,
  breaklines=true,
  tabsize=4,
  morekeywords={with,super,as},
  escapeinside={[$}{$]},
  escapebegin={\color{comment}\begin{math}},
  escapeend={\end{math}},
  frame=single,
  basicstyle=\footnotesize\tt,
  commentstyle=\color{comment},
  keywordstyle=\color{keyword},
  stringstyle=\color{string},
  backgroundcolor=\color{background},
  showstringspaces=false,
  numbers=left,
  numbersep=5pt,
  literate=
    % scandinavian vowels
    {æ}{{\ae}}1
    {å}{{\aa}}1
    {ø}{{\o}}1
    {Æ}{{\AE}}1
    {Å}{{\AA}}1
    {Ø}{{\O}}1
    {é}{{\'e}}1
    {É}{{\'E}}1
    % greek letters
    {α}{{\textalpha}}1
    {β}{{\textbeta}}1
    {γ}{{\textgamma}}1
    {δ}{{\textdelta}}1
    {ε}{{\textepsilon}}1
    {ζ}{{\textzeta}}1
    {η}{{\texteta}}1
    {θ}{{\texttheta}}1
    {ι}{{\textiota}}1
    {κ}{{\textkappa}}1
    {λ}{{\textlambda}}1
    {μ}{{\textmu}}1
    {ν}{{\textnu}}1
    {ξ}{{\textxi}}1
    {ο}{{o}}1
    {π}{{\textpi}}1
    {ρ}{{\textrho}}1
    {σ}{{\textsigma}}1
    {τ}{{\texttau}}1
    {υ}{{\textupsilon}}1
    {φ}{{\textphi}}1
    {ϕ}{{\ensuremath{\phi}}}1
    {χ}{{\textchi}}1
    {ψ}{{\textpsi}}1
    {ω}{{\textomega}}1
    {Α}{{A}}1
    {Β}{{B}}1
    {Γ}{{\textGamma}}1
    {Δ}{{\textDelta}}1
    {Ε}{{E}}1
    {Ζ}{{Z}}1
    {Η}{{H}}1
    {Θ}{{\textTheta}}1
    {Ι}{{I}}1
    {Κ}{{K}}1
    {Λ}{{\textLambda}}1
    {Μ}{{M}}1
    {Ν}{{N}}1
    {Ξ}{{\textXi}}1
    {Ο}{{O}}1
    {∏}{{\textPi}}1
    {Ρ}{{P}}1
    {Σ}{{\textSigma}}1
    {Τ}{{T}}1
    {Υ}{{Y}}1
    {Φ}{{\textPhi}}1
    {Χ}{{X}}1
    {Ψ}{{\textPsi}}1
    {Ω}{{\textOmega}}1
    % miscellaneous
    {°}{{\ensuremath{{}^\circ}}}1
    {²}{{\ensuremath{{}^2}}}1
  }

% bibliography packages
\usepackage[backend=bibtex8,style=authortitle,autocite=footnote,sorting=ynt,dashed=false]{biblatex}

% other packages
\usepackage{filecontents}

% bibliography
\begin{filecontents}{bibliography.bib}
  % ...
\end{filecontents}
\addbibresource{bibliography.bib}

% custom math commands
\newcommand\V[1]{\mathbf{#1}}                  % vector notation
\newcommand\M[1]{\begin{pmatrix} #1 \end{pmatrix}} % matrix shorthand

\begin{document}

\title{Project 1 - FYS3150 Computational Physics}
\author{Fredrik Østrem (\texttt{fredost})}
\date{\today}

\maketitle

\tableofcontents

\clearpage

\section{Introduction}

In this project, we solve the one-dimensional Possion equation
\begin{equation}
  - u''(x) = f(x)
\end{equation}
in the case where $u(0) = u(1) = 0$ and $f(x) = 100e^{-10x}$. We shall set up a matrix equation
\begin{equation}
  A \V{v} = \V{b}
\end{equation}
and generate efficient algorithms for solving this equation to get a numerical solution to the differential equaion. We shall also compare these algorithms to LU decomposition, and see how much more efficient the specialized algorithms are.

\section{Methods}

\subsection{Approximate solution to $-u''(x) = f(x)$}

We consider a differential equation on the form $-u''(x) = f(x)$ with $x \in (0,1)$ and $u(0) = u(1) = 0$, and where $f$ is a known function.

We consider the functions $u$ and $f$ at a sequence of equally spaces points $x_0,x_1,\ldots,x_n$ in the interval $[0,1]$, so that $x_i = ih$ where $h = \frac{1}{n+1}$. We let $v_i = \tilde{u}(x_i) \approx u(x_i)$ be the value of our approximate solution at $x_i$, and let $b_i = h^2 f(x_i)$. Since $u(0) = u(1)$, we have the boundary condition $v_0 = v_{n+1} = 0$.

We can approximate $u''(x)$ by using the Taylor's expansion of $u(x)$, which yields:
\begin{equation}
  u''(x_i) \approx \frac{u(x_i+h) + u(x_i-h) - 2 u(x_i)}{h^2} \approx \frac{v_{i+1} + v_{i-1} - 2 v_i}{h^2}
\end{equation}
which, when subsituted into our differential equation gives
\begin{equation}
  2v_i - v_{i+1} - v_{i-1} = b_i
\end{equation}

Since this is a linear equation with $v_{i-1},v_i,v_{i+1}$ as unknowns, we can write this as
\begin{equation}
  \underbrace{\M{ 0 & 0 & \cdots & -1 & 2 & -1 & \cdots & 0 & 0 }}_{\V{a}_i}
  \M{ v_0 \\ v_1 \\ \vdots \\ v_{i-1} \\ v_i \\ v_{i+1} \\ \vdots \\ v_{n-1} \\ v_n }
  = b_i
\end{equation}
We can do this for every index $i$, and $\V{a}_i$ is shifted a single step to the right compared to $\V{a}_{i-1}$. Therefore, when we
look at all values of $i = 1, \ldots, n$, we get the tridiagonal matrix $A$:
\begin{equation}
  A = \M{ \V{a}_0 \\ \vdots \\ \V{a}_{i-1} \\ \V{a}_i \\ \V{a}_{i+1} \\ \vdots \\ \V{a}_n }
    = \M{
      2  & -1 & 0 & \cdots &         & 0 \\
      -1 & 2  & -1 & 0 & \iddots & \\
      0 & -1 & 2  & \ddots & 0 & \vdots \\
      \vdots & 0 & \ddots & 2 & -1 & 0\\
        & \iddots & 0 & -1 & 2 & -1 \\
      0 & & \cdots & 0 & -1 & 2
    }
\end{equation}
such that $A \V{v} = \V{b}$.

\subsection{General algorithm for solving tridiagonal matrix equations}
\label{sec:b}

In general, we can write an $n \times n$ tridiagonal matrix $A$ as
\begin{equation}
  A = \M{
    b_1 & c_1 & 0 & \ldots & \ldots & \ldots \\
    a_1 & b_2 & c_2 & \ldots & \ldots & \ldots \\
    & a_2 & b_3 & c_3 & \ldots & \ldots \\
    & \ldots & \ldots & \ldots & \ldots & \ldots \\
    & & & a_{n-2} & b_{n-1} & c_{n-1} \\
    & & & & a_{n-1} & b_n
  }
\end{equation}

We can solve the matrix equation $A \V{v} = \V{b}$ in three steps:
\begin{enumerate}
  \item Eliminate the lower diagonal $a_1,a_2,\ldots,a_{n-1}$ through forward substitution.
    \begin{itemize}
      \item $a'_{i-1} = 0$; $b'_i = b_i - \dfrac{a_{i-1}}{b'_{i-1}} \cdot c_{i-1}$; $ f'_i = f_i - \dfrac{a_{i-1}}{b'_{i-1}} \cdot f'_{i-1}$
    \end{itemize}
  \item Eliminate the upper diagonal $c_1,c_2,\ldots,c_{n-1}$ through backward substitution.
    \begin{itemize}
      \item $c''_{i} = 0$; $f''_{i} = f'_{i} - \dfrac{c_i}{b'_{i+1}} \cdot f''_{i+1}$
    \end{itemize}
  \item Divide each row $i$ by $b_i$ to get only $1$ elements along the main diagonal.
    \begin{itemize}
      \item $v_i = \dfrac{f''_i}{b'_i}$
    \end{itemize}
\end{enumerate}
When doing these steps, we transform $A$ into the identity matrix, and transform $\V{b}$ into $\V{v}$. (This algorithm has been implemented in the file \href{https://github.com/frxstrem/fys3150/tree/master/project1/tridiagonal.hh}{\tt tridiagonal.hh}.) We can see the results of the approximation in figure \ref{fig:b}.

From the implementation, we can see that the first step uses 5 floating-point operations for every row except the first, the second step uses 5 for every row except the last, and the third step uses 2 for every row; in total, the number of floating-point operations is $5 (N - 1) + 5 (N - 1) + 2 N = 12N - 10$.

\section{Results}

We run the algorithm from section \ref{sec:b} from different values of $n$. In figure \ref{fig:b} we see that for small values of $N$, the curve keeps the right shape but is quite a bit off from the analytic solution; however, for $N = 1000$ it is almost indistinguishable from the analytic solution. In table \ref{tbl:c}, where we have listed the maximum value of
\begin{equation}
  \varepsilon_i = \log_{10}\left| \frac{v_i - u_i}{u_i} \right|
\end{equation}
for each $N$, as a function of $\log_{10}(h)$ where $h$ is the step length, we see that the relative error becomes decreases exponentially as $h$ decreases.

\begin{figure}[!ht]
  \begin{tikzpicture}
    \begin{axis}[
      width=\textwidth,
      height=0.5\textwidth,
      xmin=0, xmax=1,
      xlabel=$x$,
      ymin=0, ymax=2,
      ylabel=$u(x)$,
      legend cell align=left,
      ]
      \addplot[red,mark=o,mark size=1] table[x=x,y=v] {plots_b_10.dat};
      \addplot[red!50!blue,mark=o,mark size=1] table[x=x,y=v] {plots_b_100.dat};
      \addplot[blue,mark=o,mark size=1,mark repeat=7] table[x=x,y=v] {plots_b_1000.dat};
      \addplot[black,thick,domain=0:1,samples=100] {1 - (1 - exp(-10))*x - exp(-10*x)};

      \legend{$N = 10$,$N = 100$,$N = 1000$,Closed-form solution}
    \end{axis}
  \end{tikzpicture}

  \caption{Plot of approximate solutions based on the method described in section \ref{sec:b}}
  \label{fig:b}
\end{figure}

\begin{table}[!ht]
  \begin{center}
    \pgfplotstabletypeset[
      col sep=tab,
      every head row/.style={after row=\hline},
      columns/log h/.style={
        column name={$\log_{10}(h)$},
      },
      columns/eps/.style={
        column name={$\varepsilon_{max}$},
        sci, sci zerofill,
        precision=2,
      },
    ]{d.dat}
    \caption{}
  \end{center}

  \caption{$\varepsilon_{\text{max}}$ as a function of $\log_{10}(h)$.}
  \label{tbl:c}
\end{table}

If we compare the runtime of our tridiagonal algorithm to one using LU decomposition of the matrix (the implementation given in the course's code files), we see that the tridiagonal algorithm is very fast, even for large $N$, while the LU decomposition runs at about \SI{1}{s} for $N = 1000$:

\begin{table}[!ht]
  \begin{center}
    \pgfplotstabletypeset[
    col sep=tab,
    every head row/.style={after row=\hline},
    columns/n/.style={
    column name={$N$},
    },
    columns/ctime/.style={
    column name={Time (tridiagonal) / \si{\milli\second}},
    fixed, fixed zerofill,
    precision=3, 1000 sep={\,},
    },
    columns/lutime/.style={
    column name={Time (LU decomposition) / \si{\milli\second}},
    fixed, fixed zerofill,
    precision=3, 1000 sep={\,},
    },
    ]{e.dat}
  \end{center}

  \caption{Running times for our specialized algorithm, and a LU decomposition algorithm.}
\end{table}

There's a big difference here because our simple algorithm for tridiagonal matrices only needs on the order of $N$ float-point operations, so it run in less than a millisecond, even for very large matrices. However, LU decomposition is much more general, and needs on the order of $N^3$ different floating-point operations for each matrix. If $N$ is very large, the number of operations grows very fast.

\section{Conclusions}

From the results we got, we can conclude that numerically solving a differential equation by using a matrix equation can work well, but we a small enough step length ($N = 10$ is way off, for instance) and a sufficiently efficient algorithm for solving the matrix equation (LU decomposition is too slow for large $N$).

\clearpage
\appendix
\section{Appendix}

All files used in this project can be found at \url{https://github.com/frxstrem/fys3150/tree/master/project1}.
The following code files are used:
\begin{itemize}
  \item \href{https://github.com/frxstrem/fys3150/tree/master/project1/tridiagonal.hh}{\tt tridiagonal.hh}
  \item \href{https://github.com/frxstrem/fys3150/tree/master/project1/oppg_b.cc}{\tt oppg\_b.cc}
  \item \href{https://github.com/frxstrem/fys3150/tree/master/project1/oppg_d.cc}{\tt oppg\_d.cc}
  \item \href{https://github.com/frxstrem/fys3150/tree/master/project1/oppg_e.cc}{\tt oppg\_e.cc}
\end{itemize}

\clearpage

\printbibliography[heading=bibnumbered,title=Bibliography]

\end{document}


\documentclass[a4paper]{article}

\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{amsmath,amsthm,amsfonts,amssymb,euscript,stmaryrd,graphicx,appendix,enumitem,yhmath,mathrsfs,superbox}
\usepackage{fullpage}
\usepackage{color,psfrag}

\definecolor{ACORRIGER}{rgb}{0.9,0,0}

\def\myqed {{% set up
\parfillskip=0pt % so \par doesnt push \square to left
\widowpenalty=10000 % so we dont break the page before \square
\displaywidowpenalty=10000 % ditto
%\finalhyphendem*erits=0 % TeXbook exercise 14.32
%
% horizontal
\leavevmode % \nobreak means lines not pages
\unskip % remove previous space or glue
\nobreak % don't break lines
\hfil % ragged right if we spill over
\penalty50 % discouragement to do so
\hskip.2em % ensure some space
\null % anchor following \hfill
\hfill % push \square to right
$\square$% % the end-of-proof mark
%
% vertical
\par}} % build paragraph

\newcommand{\footnoteremember}[2]{
 \footnote{#2}
 \newcounter{#1}
 \setcounter{#1}{\value{footnote}}
}
\newcommand{\footnoterecall}[1]{
 \footnotemark[\value{#1}]
}
\def\andcr{%
 \end{tabular}%
 \\
 \begin{tabular}[t]{c}}

\author
{
}

\title{Cheapest Super-Replication of Vanilla Options}
\date{}

\newtheorem{theo}{Theorem}[section]
\newtheorem{theodef}{Theorem-Definition}[section]
\newtheorem{prop}[theo]{Proposition}
\newtheorem{coro}[theo]{Corollary}
\newtheorem{remark}{Remark}
\newtheorem{lemm}[theo]{Lemma}
\newtheorem{defi}{Definition}
\newtheorem*{propx}{Proposition}

\newcommand{\1}{\textbf{1}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\K}{\mathbb{K}}

\newcommand{\C}{\mathbb{C}}
\newcommand{\E}{\mathbb{E}}
\newcommand{\vega}{\upsilon}
\newcommand{\Vega}{\mathcal{V}}

\newcommand{\PP}{\mathbb{P}}
\newcommand{\cF}{\mathcal{F}}
\newcommand{\diag}{\operatorname{diag}}
\newcommand{\supp}{\operatorname{supp}}
\newcommand{\card}{\operatorname{card}}
\newcommand{\vect}{\operatorname{vect}}
\newcommand{\sspan}{\operatorname{span}}
\newcommand{\cov}{\operatorname{cov}}
\newcommand{\var}{\operatorname{Var}}
\newcommand{\Proj}{\operatorname{Proj}}
\newcommand{\cell}{\operatorname{cell}}
\newcommand{\LL}{\operatorname{L}}
\newcommand{\Prime}{\operatorname{Prime}}
\newcommand{\Call}{\operatorname{Call}}
\newcommand{\Put}{\operatorname{Put}}
\newcommand{\slab}{\operatorname{slab}}
\newcommand{\tanapprox}{\operatorname{tanapprox}}
\newcommand{\ui}{{\underline{i}}}
\newcommand{\uj}{{\underline{j}}}

\def\independent{{\perp\!\!\!\!\perp}}
\def\simdist{\stackrel{\mathcal{L}}{\sim}}
\def\h1{\hspace{0.1cm}}

% keywords
\def\keywordname{{\bf Keywords:}}

\newcommand{\keywords}[1]{\par\addvspace\baselineskip\noindent\keywordname\enspace\ignorespaces#1}

\bibliographystyle{plain}

\graphicspath{{./}{figures/}}

\begin{document}
\maketitle
\vspace{-1cm}

\begin{center}{\Large \emph{A Linear-Proramming Approach to Super Replication and Arbitrage Detection} }\end{center}

\par \emph{This project is slightly more ambitious than the two other proposals, and aimed at students already familiar with the C++ programming language. The added difficulty is that it requires students to make use of a third-party library.}

\section{Introduction}

\par Just like their underlyings, listed options are traded on exchanges with an order book mechanism, so that, in order to guarantee a transation, buyers must cross the bid-ask spreads of the considered instruments.

\par In this project, we consider the case of a trader interested in buying a European option with a given maturity $T$ with a piecewise linear payiff.

\par A piecewise-linear payoff may be written as a linear combination of Call and Put payoffs. A buyer willing to purchase the corresponding linear combination must trade Calls and Puts at the Ask price when the corresponding weight it positive, and the Bid price when the corresponding weight is negative. This can be prohibitive, especially when certain strikes are illiquid which results in large bid-ask spreads.

\par The goal of this project is to produce a routine that outputs the weights for a linear combination of calls and puts that super-replicates the target payoff while minimizing the cost.

\section{C++ Computing Project}

\par Produce a C++ routine outputing weights for the super replication of a piecewise linear function of compact suport with Call options.

\par \emph{This stepp should require no optimization whatsoever}.

\par Formulate the problem of the \emph{minimal super replication} of a piecewiese linear profile with combination of calls and puts as a linear programming problem.

\par Using a third-party linear programming solver, or with your own implementation of the simplex method, provide a routine computing the cheapest super-replication price for any given piecewise linear profile.

\par What would it mean if the cheapest replication of the null payoff had a negative price?

\bibliography{biblio}
\end{document}


\section{Conclusion}
This paper has explored finiteness in the setting of Cubical Type Theory.
Hopefully, it will serve as a reasonable introduction to dependent type theory
for functional programmers which is a little different from most other
introductions, and which gives a taste of topics like HoTT which are on the
cutting edge of the field.
For those more experienced with dependent types, hopefully the paper makes some
argument as to the usefulness and importance of univalence in dependently-typed
language, and demonstrates its power both theoretically and practically.

As for the theoretical contributions of the paper, we have provided a thorough
accounting of many of the ways to say something is ``finite'' in a
dependently-typed programming language, and grounded that account in topos
theory.
In the future we hope to see a similar exploration of the countably infinite
types, and a connection of those predicates to the finite predicates.

We would also like to see more uses for finite types in dependently typed
programming: termination checking seems one obvious area where they could be
used more extensively.

Automated proof search libraries based on finiteness are common in the
dependently-typed programming world.
In this paper, we have presented such a library which has the added power of
univalence: in the future we would like to see explorations of how to improve
the efficiency of the proof search, to make it more practical for larger
examples.
We would also like to see a similar library for the countably infinite types,
which can perform partial proof search.

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../paper"
%%% End:


\documentclass{article}

%%%%%% Include Packages %%%%%%
\usepackage{sectsty}
\usepackage{amsmath,amsfonts,amsthm,amssymb}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{setspace}
\usepackage{graphicx}
\usepackage{array}

%%%%%% Formatting Modifications %%%%%%

\usepackage[margin=2.5cm]{geometry} %% Set margins
\sectionfont{\sectionrule{0pt}{0pt}{-8pt}{0.8pt}} %% Underscore section headers
\setstretch{1.2} %% Set 1.2 spacing

%%%%%% Set Homework Variables %%%%%%

\newcommand{\hwkNum}{12}
\newcommand{\hwkAuthors}{Ben Drucker}

%%%%%% Set Header/Footer %%%%%%

\pagestyle{fancy}
\lhead{\hwkAuthors}
\rhead{Homework \#\hwkNum}
\rfoot{\textit{\footnotesize{\thepage /\pageref{LastPage}}}}
\cfoot{}
\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

%%%%%% Document %%%%%%

\begin{document}

\title{Homework \#\hwkNum}
\author{\hwkAuthors}
\date{}

\maketitle

%%%%%% Begin Content %%%%%%

\section*{8.1}
	\subsection*{14}
		\subsubsection*{a)}
			Given $\sigma_{\overline{x}} = \frac{.2}{\sqrt{25}} = .04$ and $\mu =10$:\\
			$P[Z \geq \phi (\frac{10.1004-10}{.04})] + P[Z \leq \Phi (\frac{9.8940-10}{.04})] = .006 + 004 = .01 = \alpha$
		\subsubsection*{b)}
			$\mu = 10.1: \beta (10.1) = P(9.8940 < \overline{x} < 10.1004) = P(-5.15 < Z .01) = .504\\
			\mu = 9.9: \beta(9.9) = P(-.15 < Z < 5.01) = .5596.$ \\\\
			This is not desirable. A difference from $\mu$ in either direction is equally important so we want $\beta(\mu + k) = \beta(\mu - k)$.
\section*{8.2}
	\subsection*{16}
		\subsubsection*{a)}
			For d.f. = 15, $\alpha = P(t\leq 3.73) = .001.$
		\subsubsection*{b)}
			For d.f. = 23, $\alpha = P(t \leq 2.5) = .01.$
		\subsubsection*{c)}
			For d.f. = 30, $\alpha = P(t \leq 1.697) + P(t \leq 1.697) = .05 * 2 =.1.$
	\subsection*{18}
		\subsubsection*{a)}
			$\left | \frac{72.3-75}{1.8} \right | = 1.5 $ SDs
		\subsubsection*{b)}
			$Z = -1.5 > 2.33$. Don't reject.
		\subsubsection*{c)}
			$\alpha = \Phi (-2.88) = .002$
		\subsubsection*{d)}
			$\Phi \left ( -2.88 + \frac{75-70}{\frac{9}{5}} \right ) = \Phi(-.1) = .46 \Rightarrow \beta(70) = .5398$
		\subsubsection*{e)}
			$n = \left( \frac{9(2.88+2.33)}{75-70} \right )^2 = 87.95 \Rightarrow n = 88$
		\subsubsection*{f)}
			$\alpha(76) = P(Z < -2.33) = P(\overline{X} < 72.9) = \phi \left ( \frac{72.9 - 76}{.9} \right ) = .0003$
	\subsection*{20}
		At $\alpha = .05$, $H_0$ is rejected if $z< 1.645. z = -2.14,$ so $H_0$ is rejected. At .01, $z < -2.33$ rejects and so we don't reject. Given the very favorable price, I'd choose .01.
	\subsection*{32}
		$H_0 : \mu = 15. H_a: \mu < 15; t = \frac{\overline{X} - 15}{s / \sqrt{115}}; t = -6.17\\
		t_{.005,114} =-1.66 > -6.17.\\$
		The data indicate that the observed daily intake is below the DRI.
\section*{8.3}
	\subsection*{38}
		\subsubsection*{a)}
			$H_0: p=.1; H_a: p\not =.1;$ Reject $H_0$ if $z \geq 1.96. \hat{p} =
			\frac{14}{100}, z= 1.33.$ Null hypothesis cannot be rejected.
		\subsubsection*{b)}
			??
		\subsubsection*{c)}
			??
	\subsection*{40}
		$H_0: p = \frac{2}{3}; H_a: p \not = \frac{2}{3}; \hat{p} = \frac{80}{124}; Z = \frac{\hat{p} - p_0}{\sqrt{\frac{p_0(1-p_0)}{n}}} = -.508; P(Z < -.508) = .306.$ This value rejects $H_0$.
	\subsection*{46}
		$p_0 = .035; \hat{p} = .03; Z = -.61; Z_{.01} = 2.33.$ Therefore the null hypothesis that humans and robots have the same defect rate cannot be rejected.

%%%%%% End Content %%%%%%
\end{document}

Spring 2013/STAT W1211 - Statistics/Homework/Homework 11/Homework 12/Homework 12.tex


\documentclass{article}

\usepackage{fancyhdr}
\usepackage{extramarks}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage[plain]{algorithm}
\usepackage{algpseudocode}
\usepackage{graphicx}
\graphicspath{ {./images/} }

\usetikzlibrary{automata,positioning}

%
% Basic Document Settings
%

\topmargin=-0.45in
\evensidemargin=0in
\oddsidemargin=0in
\textwidth=6.5in
\textheight=9.0in
\headsep=0.25in

\linespread{1.1}

\pagestyle{fancy}
\lhead{\hmwkAuthorName (\hmwkAuthorMatricNo)}
\chead{\hmwkClass\ (\hmwkClassTime): \hmwkTitle}
\rhead{\firstxmark}
\lfoot{\lastxmark}
\cfoot{\thepage}

\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\setlength\parindent{0pt}

%
% Create Problem Sections
%

\newcommand{\enterProblemHeader}[1]{
    \nobreak\extramarks{}{Problem \arabic{#1} continued on next page\ldots}\nobreak{}
    \nobreak\extramarks{Problem \arabic{#1} (continued)}{Problem \arabic{#1} continued on next page\ldots}\nobreak{}
}

\newcommand{\exitProblemHeader}[1]{
    \nobreak\extramarks{Problem \arabic{#1} (continued)}{Problem \arabic{#1} continued on next page\ldots}\nobreak{}
    \stepcounter{#1}
    \nobreak\extramarks{Problem \arabic{#1}}{}\nobreak{}
}

\setcounter{secnumdepth}{0}
\newcounter{partCounter}
\newcounter{homeworkProblemCounter}
\setcounter{homeworkProblemCounter}{1}
\nobreak\extramarks{Problem \arabic{homeworkProblemCounter}}{}\nobreak{}

%
% Homework Problem Environment
%
% This environment takes an optional argument. When given, it will adjust the
% problem counter. This is useful for when the problems given for your
% assignment aren't sequential. See the last 3 problems of this template for an
% example.
%
\newenvironment{homeworkProblem}[1][-1]{
    \ifnum#1>0
        \setcounter{homeworkProblemCounter}{#1}
    \fi
    \section{Problem \arabic{homeworkProblemCounter}}
    \setcounter{partCounter}{1}
    \enterProblemHeader{homeworkProblemCounter}
}{
    \exitProblemHeader{homeworkProblemCounter}
}

%
% Homework Details
%   - Title
%   - Due date
%   - Class
%   - Section/Time
%   - Instructor
%   - Author
%

\newcommand{\hmwkTitle}{Questions}
\newcommand{\hmwkDueDate}{February 12, 2014}
\newcommand{\hmwkClass}{}
\newcommand{\hmwkClassTime}{}
\newcommand{\hmwkClassInstructor}{Professor Isaac Newton}
\newcommand{\hmwkAuthorName}{\textbf{Larry Law}}
\newcommand{\hmwkAuthorMatricNo}{\textbf{A0189883A}}

%
% Title Page
%

\title{
    \vspace{2in}
    \textmd{\textbf{\hmwkClass:\ \hmwkTitle}}\\
    \normalsize\vspace{0.1in}\small{Due\ on\ \hmwkDueDate\ }\\
    \vspace{0.1in}\large{\textit{\hmwkClassInstructor\ }} \\
    \vspace{0.1in}\large{\textit{\hmwkClassTime\ }} \\
    \vspace{3in}
}

\author{\hmwkAuthorName \\
(\hmwkAuthorMatricNo)}
\date{}

\renewcommand{\part}[1]{\textbf{\large Part \Alph{partCounter}}\stepcounter{partCounter}\\}

%
% Various Helper Commands
%

% Useful for algorithms
\newcommand{\alg}[1]{\textsc{\bfseries \footnotesize #1}}

% For derivatives
\newcommand{\deriv}[1]{\frac{\mathrm{d}}{\mathrm{d}x} (#1)}

% For partial derivatives
\newcommand{\pderiv}[2]{\frac{\partial}{\partial #1} (#2)}

% Integral dx
\newcommand{\dx}{\mathrm{d}x}

% Alias for the Solution section header
\newcommand{\solution}{\textbf{\large Solution}}

% Probability commands: Expectation, Variance, Covariance, Bias
\newcommand{\E}{\mathrm{E}}
\newcommand{\Var}{\mathrm{Var}}
\newcommand{\Cov}{\mathrm{Cov}}
\newcommand{\Bias}{\mathrm{Bias}}

\begin{document}

\textbf{19/20 Mid Term, Part III, Q2} \\
\textbf{Claim:} Step 1(a) does not ensure that \( X_2, \cdots, X_n \) is a tree-structured CSP, thus you cannot run the tree-structured algo on it. \\
Let \( i, j \in \{1, 2, 3, 4\} \), \( D_{X_i} = \{0, 1\} \), Constraints := \( \langle (X_i, X_j), X_i \neq X_j \rangle \). \\
Set $ X_1 = 0 $. Running forward checking will cause \( D_{x2} = \{ 1 \}\). Since none of the domains are empty, the solution proceeds to run the algorithm for tree-structured CSPs, when the CSP is not a tree.

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{empheq}
\usepackage{siunitx}
\usepackage{pgfplots}
\usepackage{hyperref}
\title{Insert Title Here}
\author{Max Fan}
\date{\today}
\newcommand{\answer}[1]{\begin{center}\boxed{#1}\end{center}}
\newcommand{\vel}[1]{\SI[per-mode=fraction]{#1}{\meter\per\second}}
\newcommand{\acc}[1]{\SI[per-mode=fraction]{#1}{\meter\per\second^2}}
\newcommand{\accgrav}[1]{a_{due\hspace{0.5mm}to\hspace{0.5mm}gravity}}
\newcommand{\degree}{^{\circ}}
\newcommand{\problem}[1]{\section*{#1}}
\setlength{\parindent}{0cm}
\begin{document}
\begin{titlepage}
\maketitle

\center{}
\textbf{Choate Rosemary Hall}

\bigskip

\textbf{Problems assigned}

Problem numbers here

\vspace*{\fill}

\textbf{Honor Code}

On my honor, I have neither given nor received unauthorized aid.

\bigskip

\text{Made using \underline{\href{https://www.latex-project.org/}{LaTeX}} on
\underline{\href{https://www.texstudio.org/}{TeXstudio}}}
\text{Template at \underline{\href{https://github.com/InnovativeInventor/boilerplate/}{https://github.com/InnovativeInventor/boilerplate/}}}
\end{titlepage}

\problem{Example solution}
Given $a=b, b=c$:

$a=c$
\answer{$a=c$}

\end{document}


% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode

% This is a simple template for a LaTeX document using the "article" class.
% See "book", "report", "letter" for other types of document.

\documentclass[10pt,onecolumn]{article}

\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)
\usepackage{graphicx}
\usepackage{listings}
\usepackage{caption}
\usepackage{xcolor,colortbl}
\usepackage{mathtools}
\usepackage{wrapfig}
\usepackage{tabularx}
\usepackage{pdflscape}

\graphicspath{ {figures/} }

%%% Examples of Article customizations
% These packages are optional, depending whether you want the features they provide.
% See the LaTeX Companion or other references for full information.

%%% PAGE DIMENSIONS
\usepackage{geometry} % to change the page dimensions
\geometry{a4paper} % or letterpaper (US) or a5paper or....
\geometry{margin=1in} % for example, change the margins to 2 inches all round
% \geometry{landscape} % set up the page for landscape
%   read geometry.pdf for detailed page layout information
% \usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent

%%% PACKAGES
\usepackage{polski}
\usepackage{booktabs} % for much better looking tables
\usepackage{array} % for better arrays (eg matrices) in maths
\usepackage{paralist} % very flexible & customisable lists (eg. enumerate/itemize, etc.)
\usepackage{verbatim} % adds environment for commenting out blocks of text & for better verbatim
\usepackage{subfig} % make it possible to include more than one captioned figure/table in a single float
\usepackage{indentfirst}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}

% These packages are all incorporated in the memoir class to one degree or another...

%%% HEADERS & FOOTERS
\usepackage{fancyhdr} % This should be set AFTER setting up the page geometry
\pagestyle{fancy} % options: empty , plain , fancy
\renewcommand{\headrulewidth}{0pt} % customise the layout...
\lhead{}\chead{}\rhead{}
\lfoot{}\cfoot{\thepage}\rfoot{}

%%% SECTION TITLE APPEARANCE
\usepackage{sectsty}
\allsectionsfont{\sffamily\mdseries\upshape} % (See the fntguide.pdf for font help)
% (This matches ConTeXt defaults)

%%% ToC (table of contents) APPEARANCE
\usepackage[nottoc,notlof,notlot]{tocbibind} % Put the bibliography in the ToC
\usepackage[titles,subfigure]{tocloft} % Alter the style of the Table of Contents
\renewcommand{\cftsecfont}{\rmfamily\mdseries\upshape}
\renewcommand{\cftsecpagefont}{\rmfamily\mdseries\upshape} % No bold!
\setlength{\parindent}{0.5cm}

%%% END Article customizations

%%% The "real" document content comes below...

\definecolor{Gray}{gray}{0.85}
\definecolor{LightCyan}{rgb}{0.88,1,1}
\newcolumntype{a}{>{\columncolor{Gray}}c}
\newcolumntype{b}{>{\columncolor{white}}c}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}

\newcommand{\subsubfloat}[2]{%
  \begin{tabular}{@{}c@{}}#1\\#2\end{tabular}%
}

\title{Zadanie 808 z Klubu 44M}
\author{Marcin Barylski}
\date{\small{11 październik, 2020 \\ Gdańsk}}

\begin{document}
\maketitle

\section{Treść zadania}

Znaleźć wszystkie pary liczb wymiernych $x, y \textgreater 1$ spełniających równanie $x^y = xy$

\section{Rozwiązanie}

Przekształćmy równanie do wygodniejszej postaci: \\

\begin{enumerate}[(1)]
\item $x^y = xy$ \\
\item $x^y - xy = 0$ \\
\item $x(x^{y-1}-y) = 0$ \\
\end{enumerate}

Z założenia zadania $y \textgreater 1$, stąd wykładnik $y-1$ nadal pozostaje dodatni. Miejsca zerowe w (3) to: \\

\begin{enumerate}[(4)]
\item $x(x^{y-1}-y) = 0 \iff x = 0 \lor x^{y-1}-y = 0$ \\
\end{enumerate}

Pierwszy przypadek z (4) eliminujemy (bo w założeniach zadania $x \textgreater 1$). Kontynuujemy rozważania dla drugiego przypadku z (4):

\begin{enumerate}[(5)]
\item $x^{y-1}-y = 0$\\
\end{enumerate}
\begin{enumerate}[(6)]
\item $x^{y-1} = y$\\
\end{enumerate}

Podnosząc (6) obustronnie do potęgi $\frac{1}{y-1}$ (dozwolone, bo $y-1 \textgreater 0$) otrzymujemy:

\begin{enumerate}[(7)]
\item $x = y^{\frac{1}{y-1}}$\\
\end{enumerate}

$y \textgreater 1$, stąd $y-1 \textgreater 0$ oraz $\frac{1}{y-1} \textgreater 0$. Z własności potęgowania mamy wtedy: $y^{\frac{1}{y-1}} \textgreater 1$, czyli $x \textgreater 1$ \\

\textbf{Wniosek końcowy}: rozwiązaniem jest każda para liczb wymiernych $(x, y)$ postaci $(y^{\frac{1}{y-1}}, y), y \textgreater 1$. \\ \\ \\

\end{document}


\section {Experimental Results}

\subsection{AVL Tree}

For basic experiments, we used a simple Python AVL tree found on the
web \cite{avltree}, with 225 lines of code\footnote{All sizes
  non-comment, non-blank lines, by cloc \cite{cloc}.}.  We used MutPy
to generate 82 valid, non-equivalent (measured by applying random
testing for one hour, and assuming mutants with no failing tests were
equivalent) mutants of the AVL implementation.  The test harness and
reduction implementations used the TSTL \cite{tstl,ISSTA15,NFM15}
testing language for Python.  The AVL tree forms a good simple basis
for experimentation with slippage.  It has a strong oracle, letting us
ignore specification problems, but tests are all very similar, making
the construction of pattern-based slippage avoidance mechanisms
difficult.  AVL is also sufficiently complex to allow for many different
faults, some of which are fairly difficult to detect without effective
automated testing.

Combining two mutants that cover different lines of code yields a version
of AVL tree with two faults that are, we know, independently
detectable (that is, we can find each fault in isolation).  This
results in a program to test with the potential for test slippage.
There are 3,274 such combinations, which we used to explore slippage
and perform a simple evaluation of our slippage mitigation algorithms.

For the mutant pair $(m_1,m_2)$, let $m_1+m_2$ be the program
combining $m_1$ and $m_2$.  We randomly sampled mutant pairs, and
performed random testing on $m1+m2$ for 60 seconds. In all but a very
small number of cases, 60 seconds of random testing produces a failing
test, $t$.  Executing $t$ on both $m1$ and $m2$ yields a number,
$|f|_t$, between 0 and 2, the number of isolated faults $t$ detects (2
if each fault can be detected in isolation by $t$, 1 if only 1 of the
mutants is detected in isolation).  In a small number of cases, $t$
only detects the combined fault, and detects neither fault in
isolation.  We then applied delta debugging to produce a reduced test
$r$, and used the same process to produce $|f|_r$, which could,
compared to $|f|_t$, either be smaller (slippage with fault loss),
larger (beneficial slippage), or the same (either no slippage or
change of fault).  Finally, we applied the {\tt comb-block} algorithm
to produce a set of tests, $T_c$ and the {\tt multi-ddmin} algorithm
to produce a set of tests, $T_m$.  For parameters, we chose to run
{\tt ddmin} 10 times in {\tt multi-ddmin}, and constrained
{\tt comb-block} to approximately the same runtime by
limiting it to a search depth of 5 and a maximum of 1,000 attempted
combinations.  We computed $|f|_c$ as the count of isolated faults
detected by \emph{any} test in $T_c$; $|f|_m$ was defined in the same
way, except using $T_m$.

\begin{table}
\centering
{\scriptsize
\begin{tabular}{|c|c|c|c|}
\hline
Tests & Runtime(s) & $|f|_x$ (Faults) & \# Tests\\
\hline
\hline
Unreduced tests & N/A & 1.08 & 1 \\
\hline
{\tt ddmin} & 0.41 & 1.05 & 1 \\
\hline
{\tt comb-block} & 2.79 & 1.14 & 2.9 \\
\hline
{\tt multi-ddmin} & 4.24 & 1.11 & 1.9 \\
\hline
\end{tabular}
}
\caption{Average runtimes, faults detected in isolation, and number of
  tests for a user to examine for AVL
  tree mutant pairs.}
\label{tab1}
\end{table}

We produced 7,500 tests, sampling all 82 mutants numerous times, and
sampling 2,959 of the 3,274 mutant pairs.  Table \ref{tab1} shows the
results.  All differences between means are significant by Wilcoxon
test with $p < 10^{-12}$.  The {\tt ddmin} results show that using
only a single run of classic delta debugging results in fewer faults
per test than using un-reduced tests:  harmful slippage
results in a 2.8\% total reduction in fault detection, even with
beneficial slippage taken into account.  Slightly over 8\% of test
cases had slippage of some kind when reduced.

Both  approaches to slippage reduction produced a significantly higher
average number of faults detected than the unreduced tests.
Using {\tt comb-block} increased fault detection by about 5.5\%, and
using {\tt multi-ddmin} increased it by 2.7\% (the same margin,
interestingly, as a single reduction decreased fault detection).  The
cost of this improvement was close to a 6x increase in runtime
for {\tt comb-block} and over a 9x increase for {\tt multi-ddmin}.

One concern with slippage mitigation is that avoiding slippage may
produce a large number of additional tests a developer must examine to
see if they expose different faults \cite{PLDI13}.  However, at least
for our AVL example, the number of different tests produced is not
particularly larger than the number of faults discovered on average.
In most cases, we speculate that {\tt comb-block} and {\tt
  multi-ddmin} will not produce far more different tests than faults.

While only 8\% of all
tests showed slippage, we also ran some experiments with 30 runs for
every mutant pair (producing a total of 98,100 failing tests), but only running {\tt comb-block} and {\tt
  multi-ddmin} if reduction caused  loss-of-fault slippage, and
checking whether the two approaches managed to restore the lost fault
(without losing the other exposed fault).  These
results showed that 8\% of mutant pairs had harmful slippage, and that these
pairs had slippage rates up to 83\%, making some faults very
hard to detect in isolation without slippage mitigation.  The {\tt
  comb-block} approach avoided loss-of-fault slippage for 56\% of
slipped tests for each mutant, on average, while {\tt multi-ddmin}
only avoided it in 33\% of slipped tests, on average.  The difference
was significant with $p < 10^{-96}$.

\subsection{SpiderMonkey}

We also performed limited experiments with the Mozilla
SpiderMonkey JavaScript engine version 1.6 tests produced by {\tt jsfunfuzz}, used in the PLDI 2013 paper introducing
the concept of slippage \cite{PLDI13}.  SpiderMonkey is a large (about
70KLOC), complex, widely used program, and {\tt jsfunfuzz}
\cite{jsfunfuzz} has been used to discover over 1,700 bugs in
JavaScript engines.  For 113 randomly sampled
tests, using a single reduction produced slippage only about 2.7\% of the
time (in exactly 3 tests).  In these experiments we limited
{\tt comb-block} to a depth of 1, with no ``recursive'' exploration of
new tests.  For {\tt multi-ddmin} we used 10 reductions, as in the AVL experiments.

The small number of slippage cases
allowed us to analyze the results in detail.  The {\tt comb-block} approach did
not manage to avoid slippage in any of these three cases.  For two
of the three cases, no blocking combination produced a failing test.
In the third case, one combination did produce a failing test, but it
reduced to the same fault as the slipped {\tt ddmin} test.  The {\tt
  multi-ddmin} algorithm fared only slightly better, restoring the
originally detected fault in one of the three slippage cases.

Interestingly, the overall rate for slippage in the original paper is
estimated as 23\%, not under 3\%.  We attribute this to the use of a
far more aggressive and powerful reduction algorithm than our
line-based {\tt ddmin}.  The PLDI 13 reducer is a hand-crafted
modification of {\tt ddmin} that performs both character and line
based passes, and applies constant propagation and other specialized
techniques for JavaScript.  Note that both of our mitigation
techniques in theory could apply to this method, though {\tt comb-block} is
probably easier to apply as it does not require modifying the internals of the
more powerful reducer.

Slippage avoidance is not the only goal of our methods.  We also computed the total number of discovered faults for
{\tt comb-block} and {\tt multi-ddmin}, as with the AVL tree
experiments.  One-test {\tt ddmin}, in this setting, always produced a
single test, detecting one fault.  The {\tt comb-block} algorithm
discovered 1.34 different faults on average, and the {\tt multi-ddmin}
approach discovered 1.09 different faults on average.  These
differences are significant by Wilcoxon test with $p < 10^{-5}$.  Note
that the strong results for {\tt comb-block} are in spite of nearly
70\% of tests not having any failing blocked versions, and using a
search depth of only 1 (no attempt to block based on newly discovered
tests).  Blocking seems quite effective in producing tests due to a
different fault, when this is possible.  The total set
of distinct faults discovered by each method favored {\tt
  multi-ddmin}, which produced tests revealing 10 distinct faults,
while {\tt comb-block} discovered 8 distinct faults over all 113
tests.  The original set of unreduced tests contains 8 distinct
faults, and simple {\tt ddmin} reduced the count to 7 faults.

In part because many tests do not have any combinations that produce a failing
test when removed, the runtimes for {\tt comb-block} are
generally much lower than for {\tt multi-ddmin}, which takes about 10x
or more the time for a single-test {\tt ddmin} run (the average is
higher than 10x because of the structure of JavaScript tests, where
the first attempt in non-randomized {\tt ddmin} is very likely to
fail).

Given the (surprising to us) extreme differences in runtimes observed
for SpiderMonkey tests, and the smaller but real differences for AVL tree,
in future experiments we plan to exploit the ``best effort'' nature of
both mitigation approaches, and simply use a fixed timeout for each method.


\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{float}

% Basic figure setup, for now with no caption control since it's done
% automatically by Pandoc (which extracts ![](path) syntax from Markdown).
\usepackage{graphicx}
\usepackage{caption}
\captionsetup[figure]{labelfont=bf}

\usepackage{xcolor} % Allow colors to be defined
\usepackage{geometry} % Used to adjust the document margins
\usepackage{amsmath} % Equations
\usepackage{amssymb} % Equations
\usepackage{textcomp} % defines textquotesingle
\usepackage{upquote} % Upright quotes for verbatim code
\usepackage{eurosym} % defines \euro
\usepackage[mathletters]{ucs} % Extended unicode (utf-8) support
\usepackage[utf8x]{inputenc} % Allow utf-8 characters in the tex document
\usepackage{fancyvrb} % verbatim replacement that allows latex
\usepackage{grffile} % extends the file name processing of package graphics
% to support a larger range
% The hyperref package gives us a pdf with properly built
% internal navigation ('pdf bookmarks' for the table of contents,
% internal cross-reference links, web links for URLs, etc.)
\usepackage{hyperref}
\usepackage{longtable} % longtable support required by pandoc >1.10
\usepackage{booktabs}  % table support for pandoc > 1.12.2
\usepackage[inline]{enumitem} % IRkernel/repr support (it uses the enumerate* environment)
\usepackage[normalem]{ulem} % ulem is needed to support strikethroughs (\sout)
% normalem makes italics be italics, not underlines
\usepackage{color}

\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=\footnotesize,
	breakatwhitespace=false,
	breaklines=true,
	captionpos=b,
	keepspaces=true,
	numbers=left,
	numbersep=5pt,
	showspaces=false,
	showstringspaces=false,
	showtabs=false,
	tabsize=2,
	linewidth=1.2\textwidth
}
\lstset{style=mystyle}

\title{%
	Dirichlet Process Mixture Models\\
	\large A tutorial and overview
}
\author{Guilherme Grijó Pires}
\date{}

% Setup hyperref package
\hypersetup{
	breaklinks=true,  % so long urls are correctly broken across lines
	colorlinks=true
}
% Slightly bigger margins than the latex defaults
\usepackage[parfill]{parskip}

\begin{document}

\maketitle

\section{Introduction}\label{introduction}

In this work I'll try to present the concept of Dirichlet Process and to
show how they can be used to implement Infinite Mixture Models. I'll
start by introducing the Dirichlet Distribution, the Dirichlet Process,
and the application of the Dirichlet Process to Infinite Mixture Models.
I'll then apply an implementation of this model to the clustering of
data, with an unknown number of clusters.

\section{Dirichlet Distribution}\label{dirichlet-distribution}

\subsection{An introduction}\label{an-introduction}

The Dirichlet Distribution is commonly used as the conjugate prior for
the Multinomial distribution. This means that for a Multinomial
likelihood model, the most natural/simple way to encode our prior
beliefs about the nature of the observations is by using a Dirichlet
distribution. Not only that, if we use a Dirichlet prior with a
Multinomial likelihood, the posterior will turn out to be a Dirichlet
distribution as well (obtained by updating the \(\boldsymbol\alpha\)
parameter's entries with the corresponding counts given by the
Multinomial observations).

Let \begin{eqnarray}
\boldsymbol\theta &=& (\theta_1 , \theta_2, ..., \theta_m) \nonumber \\
\boldsymbol\alpha &=& (\alpha_1 , \alpha_2, ..., \alpha_m) \nonumber
\end{eqnarray} Then \[\boldsymbol\theta \sim Dir(\boldsymbol\alpha) :
P(\boldsymbol\theta)=\frac{\Gamma(\sum_{k}^{m} \alpha_k)}{\prod_{k}^{m}\Gamma(\alpha_k)}\prod_{k}^{m}\theta_{k}^{a_k -1}\]

Note that samples \(\boldsymbol\theta\) from the Dirichlet Distribution
belong to the probability simplex, which means
\(\sum_{k}^{m}\theta_k = 1, \theta_k \geq 0\)

The Dirichlet Distribution can be regarded as a distribution over
possible parameters for a Multinomial Distribution - which is the
intuitive reason to use the former as the latter's prior. Extending this
notion a bit further, we can regard the Dirichlet Distribution as a
distribution over (Multinomial) distributions.

\subsection{The $\boldsymbol{\alpha}$-effect}\label{the-boldsymbolalpha-effect}

Let's look at the effect of the \(\boldsymbol\alpha\) parameter on the
shape of the distribution. For simplicity, lets take \(m=3\). The
simplex of the corresponding space is a triangle and so it can be
projected to 2D and be easily plotted. See Figure 1 for plots of different
parameters and number of samples.

\begin{figure}
	\caption{The effect of the $\boldsymbol\alpha$ parameter on the Dirichlet Distribution}
	\makebox[\textwidth][c]{\includegraphics[width=1.2\textwidth]{output_8_0.png}}
\end{figure}

We see that \(\boldsymbol\alpha\) controls the nature of the probability
vectors sampled from the Dirichlet Distribution:

\begin{itemize}
	\item If the \(\alpha_i\) are all equal to \(\alpha\), the resulting samples
	have a symmetric spread on the space. Particularly:
	\begin{itemize}
		\item If \(\alpha = 1\), the samples spread uniformely on the space
		\item If \(\alpha > 1\), dense (as in opposed to \emph{sparse}) samples are more frequent
		\item If \(\alpha < 1\), sparse samples are more frequent
	\end{itemize}
	\item If the \(\alpha_i\) are not equal, there will be a concentration of samples on either one
	of the vertices or one of the edges of the space
\end{itemize}

\section{Dirichlet Process}\label{dirichlet-process}

\subsection{Introduction to the concept}\label{introduction-to-the-concept}

The Dirichlet Process can be regarded as a generalization of the the
Dirichlet Distribution to infinite dimensions. It too defines a
distribution over distributions. However, while the Dirichlet
Distribution defines a distribution over random probability measures of
defined dimension, the Dirichlet Process defines a distribution over
random probability measures, of random dimension.

Formally:

\begin{itemize}
	\item Consider the measure space defined by \((\Theta, \Sigma)\), where
	\(\Theta\) is some set and \(\Sigma\) is a \(\sigma\)-algebra on
	\(\Theta\)
	\item Take a \emph{measurable finite partition} of \(\Theta\) :
	\(A_1, A_2, ..., A_k\)
	\item A Dirichlet Process is a random probability measure \(G\) over a
	measure space \((\Theta, \Sigma)\), that respects a special property:

	\begin{itemize}
		\item \([G(A_1), G(A_2), ..., G(A_3)] \sim Dir(\alpha H(A_1), \alpha H(A_2), ..., \alpha H(A_k))\)
	\end{itemize}
	\item The Dirichlet Process is parametrized by:

		\begin{itemize}
			\item \(\alpha \in \mathbb{R}\) : The concentration parameter
			\item \(H\) (a probability distribution): The base distribution
		\end{itemize}
		\item
		Most common notation: \(G \sim DP(\alpha, H)\)
\end{itemize}

Intuitively, \(H\) is the "mean distribution" and \(\alpha\) can be
regarded as an "inverse variance". A sample from a Dirichlet Process
will be an infinite sum of Dirac deltas, with different heights, and
with locations sampled from \(H\).

A somewhat counter-intuitive fact is that a sample \(G\) from a
Dirichlet Process will be discrete with probability 1, even if the base
distribution is smooth. Even so, the base distribution will be the mean
distribution!

\subsection{Posterior Inference}\label{posterior-inference}

Now suppose we use a (random) sample \(G\) from a \(DP\) as a likelihood
model for some i.i.d data \(\theta_1, \theta_2, ..., \theta_N\) :
\(\theta_n | G \sim G\).

Convenientely, the conjugacy of the Dirichlet Distribution to the
Multinomial Distribution still applies to the Dirichlet Process, which
means the posterior on \(G\) is also a Dirichlet Process and is given
(after some rather cumbersome algebraic manipulation) by:
\[ G | \theta_1, \theta_2, ..., \theta_N \sim DP(\alpha+N , \frac{\alpha H + \sum_{n=1}^{N}\delta_{\theta_n}}{\alpha+N}) \]

Where \(\delta_{\theta_n}\) is the Dirac delta function. (Note that some
of the \(\theta_n\) will fall on the same value, which means we'll have
summed \(\delta\)'s. A reasonable and intuitive way of thinking about
these, is as the counts of sampled \(\theta\) that fell on each value
(which hints at the empirical distribution).

The posterior predictive distribution of a DP is given by its base
distribution. Taking that fact, we see that the posterior predictive
distribution for \(\theta_{N+1}\) is:
\[\theta_{N+1} | \theta_1, ..., \theta_N \sim \frac{\alpha H + \sum_{n=1}^{N}\delta_{\theta_n}}{\alpha+N}\]

If you look carefully at that distribution, you'll see it has a smooth
part and a discrete part. Figure 2 presents a possible configuration for such a distribution.

\begin{figure}
	\centering
	\caption{Example of a DP posterior predictive function}
	\includegraphics[width=0.5\textwidth]{output_19_0.png}
\end{figure}

That's rather unintuitive - how do you sample from such a distribution?
Hopefully the next section will make that clearer.

\subsubsection{Chinese Restaurant Process and Polya Urn Process}\label{chinese-restaurant-process-and-polya-urn-process}

Two very famous representations for the Dirichlet Process have been
devised, that evidence its clustering properties, in a \emph{rich get
richer} fashion. They are the Chinese Restaurant Process, and the Polya
Urn Process. Put simply, they provide a way to "implement" the posterior
predictive distribution I just presented, by either: - Assigning points
to an existing group, with some probability - Which corresponds to
assigning a person who just entered the restaurant to one of the
existing tables, in the Chinese Restaurant Process - And to add to the
urn a ball of the same color as some other ball sampled from the urn, in
the Polya Urn Process - Create a new group, based on the new point -
Which corresponds to assingning a person who just entered the restaurant
to an empty table, in the Chinese Restaurant Process - And to add a ball
of a new color to the urn, in the Polya Urn Model

This somewhat "dual" behaviour is evidenced by the posterior predictive
distribution, especially if we separate the expression in two terms:
\[\theta_{N+1} | \theta_1, ..., \theta_N \sim \frac{\alpha}{\alpha+N}H +
\frac{N}{\alpha+N}(\frac{1}{N}\sum_{n=1}^{N}\delta_{\theta_n})\]

This way of writing the equation evidences the fact that the posterior
predictive distribution is a weighted sum of the base distribution and
the empirical distribution. How does one sample from such a
distribution?
\begin{itemize}
	\item With probability \(\frac{\alpha}{N+\alpha}\) we sample
	the next \(\theta\) from the base distribution, \(H\)
	\item With probability
\(\frac{N}{N+\alpha}\) we sample the next \(\theta\) from the empirical
distribution
\end{itemize}

As it is easy to see, as N increases (i.e. we see more data), the weight
of the base distribution becomes proportionally smaller - proper
Bayesian behaviour! - but the probability of a new (as in
\emph{previously unseen}) value for \(\theta\) is never \(0\). We can
also see that the concentration parameter will have control over the
final number of clusters: the bigger \(\alpha\) is, the likelier the
predictive distribution is to sample a new \(\theta\) value from the
base distribution. That can easily be observed by the plots portraied
on Figure 3.

\begin{figure}[H]
	\caption{Effect of the concentration parameter and the number of samples
		on the DP posterior predictive distribution}
	\makebox[\textwidth][c]{\includegraphics[width=0.9\textwidth]{output_23_0.png}}
\end{figure}

We can see these plots are coherent with the intuitive notion I hinted
at: for a fixed number of observed samples, a bigger \(\alpha\)
increases the weight of the base distribution; for a fixed \(\alpha\), a
bigger number of samples decreases the weight of the base distribution.

\subsubsection{Stick Breaking Representation}\label{stick-breaking-representation}

A third, more generative view of the DP, called the Stick Breaking
Representation allows us to obtains samples \(G\) from a Dirichlet
Process. It's important to keep in mind that samples \(G\) from a DP are
themselves probability distributions! The Stick Breaking Representation
obtains samples \(G \sim DP(\alpha, G)\) by the following process:

	\begin{itemize}
		\item
		Take a stick of length 1\\
		\item
		While remaining\_stick has length \textgreater{} 0:

		\begin{itemize}
			\item
			Sample a \(\pi_k\) from \(Beta(1,\alpha)\)

			\begin{itemize}
				\item
				Note that \(\pi_k \in [0,1]\)
			\end{itemize}
			\item
			Break the remaining stick at \(\pi_k\) of it's length\\
			\item
			current\_stick = first part, remaining\_stick = second part\\
			\item
			Sample a \(\theta_k\) from \(H\)
			\item
			Place a \(\delta_{\theta_k}\) with height = current\_stick on point
			\(\theta_k\)
		\end{itemize}
	\end{itemize}

We see that in the end of this process, G will be equal to \(\sum_{k=1}^\infty \delta_k \pi_k\) .

\subsection{An intuitive bridge to (Infinite) Mixture Models}\label{an-intuitive-bridge-to-infinite-mixture-models}

The usefulness of the DP in mixture models now starts to become
apparent. We can easily use the random variables \(\theta_k\) as the
latent "indexing" variables on a mixture model, taking advantage of the
natural clustering behaviour of the DP and also of the fact that, at any
point, it allows the possibility of seeing a new value for \(\theta_k\)
- hence the proneness to model a Mixture Model with an unknown number of
components: a new one can appear at any time, if the data so suggests.

Systematizing the Dirichlet Process Mixture Model:
\begin{eqnarray}
	G|\alpha, H &\sim& DP(\alpha, H) \nonumber \\
	\theta_n|G &\sim& G \nonumber \\
	x_n|\theta_n &\sim& F(\theta_n) \nonumber
\end{eqnarray}

Where \(F(\theta_n)\) is a class conditional distribution, e.g., a
Gaussian in the case of a Gaussian Mixture Model.

One of the biggest motivations to use this kind of models lies in its
ability to directly attack the problem of model selection: there is no
initial assumption on the number of components, and the model itself
"searches" for the best possible.

\section{Inference in DPMM}\label{inference-in-dpmm}

Several ways of doing Inference on Dirichlet Process Mixture Models have
been proposed and shown. The most common ones involve Gibbs Sampling,
Collapsed Gibbs Sampling or other MCMC or simulation methods. More
recently some Variational methods have also been proposed. I will
present an overview of both approaches, also introducing the high-level
basics of Gibbs Sampling and Variational Inference.

Both of these approaches come from the need of computing complex (as in
\emph{complicated and hard}) integrals (usually on the denominators of
posterior distributions). Gibbs Sampling tackles this by sampling from
distributions that assymptotically approach the true ones, while
Variational methods work by converting the integral computation into an
optimization problem.

\subsection{Gibbs Sampling approach}\label{gibbs-sampling-approach}
\subsubsection{Gibbs Sampling}\label{gibbs-sampling}

As mentioned, Gibbs Sampling takes the approach of sampling from a
distribution that is assymptotically similar to the one of interest. It
is an instance of a broader class of sampling methods, called Markov
Chain Monte Carlo.

It's clear that if we had a black box from which we could take samples
of the distribution we care about, we could empirically estimate that
distribution. However, how can you build a black box for a distribution
you don't know yet?

First, there's the need to realize that what we actually want is to
compute something in the form of: \[E_{p(z)}[f(z)]=\int_{z}f(z)p(z)\]
Where \(p(z)\) governs the distribution of the values of \(z\), but the
integral we're actually interested is on the values of \(f(z)\).
Consider the previous expression in this form:
\[E_{p(z)}[f(z)]=\lim_{N \to \infty}\frac{1}{N}\sum_{N}f(z_{(i)})\]

Where \(z_{(i)}\) are observed values, taken from the \(p(z)\)
distribution. Here we're counting how many times \(f(z)\) landed on a
particular value and averaging it. So what we really want is to have a
way to "tell" how much time we spent on each "region" of the \(z\)-space
sow that we can accumulate \(f(z)\) values. A way to do so is to use a
Markov Chain that allows us to visit each \(z\)-value with a frequency
proportional to \(p(z)\) - hence the name Markov Chain Monte Carlo.

Gibbs Sampling is a way to implement such a Markov Chain. It's only
applicable when the \(z\)-space has at least 2 dimensions. It works by
getting each dimension of the next \(z\)-point individually, conditioned
on the remaining dimensions. For our models, these dimensions will be
parameters and variables.

Gibbs Samplers are derived on a per-model basis, because the way we
sample a new value for a dimension is determined by the way these
dimensions relate "interact" in the model.

\subsubsection{Collapsed Gibbs Sampling}\label{collapsed-gibbs-sampling}

Collapsed Gibbs Sampling takes the same principals from \emph{Vanilla}
Gibbs Sampling, but does the sampling of new dimension values with some
of the dimensions integrated out. This is made possible in some models
due to prior conjugacy and some algebraic tricks, and it makes the
sampler quicker because it reduces the number of variables per sampling
operation.

\subsubsection{Gibbs Sampling for DPMM}\label{gibbs-sampling-for-dpmm}

Several Gibbs Samplers have been devised for Dirichlet Process Mixture
Models. Although I'm not going to derive one here, I'll link some
references on that.

\subsection{Variational approach}\label{variational-approach}

\subsubsection{Variational Inference and Variational Bayes}\label{variational-inference-and-variational-bayes}

Variational Inference works by transforming the problem of integration
into one of optimization. It does so by fully replacing the distribution
we want to compute with an approximation which is chosen to live inside
of a distribution family. This family is commonly called a Variational
Family, and it doesn't necessarily include the real distribution
(actually, most likely it won't include the real distribution).
Variational Inference then proceeds by finding the parameters that
correspond to the optimal distribution in the Variational Family.

The question that should be ringing in your head now is: "Optimal
regarding what?". The answer is: We optimize the parameters so as to
minimize the Kullback-Leibler divergence between the true distribution
and the approximation. The KL divergence is a measure of how different
two distributions are. It's got its roots in Information Theory, and it
can be interpreted as the number of extra bits (if we work with base 2
logarithms) needed to encode an information source distributed according
to \(p\), if we use \(q\) to build our codebook.

A side note: the KL divergence is \textbf{not} symmetric, i.e.,
\(KL(p||q) \neq KL(q||p)\); in {[}{]} Murphy suggests that the reverse
version of the KL is statistically more sensible, but I won't go into
details on why that is. For the purpose of this overview, it suffices to
know that choosing to optimize for the forward KL divergence will yield
different results than choosing to optimize for the reverse KL
divergence.

Back on track. How does on go about computing the KL divergence between
two distributions without knowing one of them? The distribution we don't
know is precisely that which we want to estimate. It seems we got stuck
on an infinite loop. Alas! The whole trick of Variational Inference is
the way to break this loop. It turns out there's a way to leverage some
probability equalities and Jensen's inequality to come up with an
expression, called the Expectation Lower Bound, ELBO. Maximizing this
expression is equivalent to minimizing the KL divergence without needing
to know a closed form for \(p(x)\). Here's the derivation of that
result:

Consider Jensen's inequality (applied to Expectation):
\[f(E[X]) \geq E[f(X)]\]

Applying it to the log-probability of the observations:
\begin{eqnarray}
	log\ p(x) &=& log \int_z p(x,z) dz \nonumber \\
	&=& log \int_z p(x,z)\frac{q(z)}{q(z)} dz \nonumber \\
	&=& log E_q[\frac{p(x,Z)}{q(z)}] \nonumber \\
	&\geq& E_q[log\ p(x,Z)] - E_q[log\ q(Z)] \nonumber
\end{eqnarray}

Our goal is now to find the parameters that yield the \(q(Z)\)
distribution that makes this bound as tight as possible. One of the
advantages of Variational methods as compared to sampling methods is the
fact that this optimization yields deterministic results, and
Variational methods are faster in general. However there's usually an
accuracy trade-off.

You might have noticed that the title for this section includes
"Variational Bayes". This refers to the application of Mean-Field
Variational Inference, where the Variational distribution is of the form
\(\prod_i q_i(x_i)\)

\subsubsection{Streaming Variational Bayes and DPMM}\label{streaming-variational-bayes-and-dpmm}

Streaming Variational Bayes is a framework by \emph{Broderick, et al.}
that proposes a way to leverage the conjugacy of some distributions to
allow the fitting of the approximation to be computed in a streaming
fashion - which aligns with the current tendencies of big-data and
scalability. This framework has been leveraged by \emph{Huynh et al.} to
apply Variational Inference to Dirichlet Process Mixture Models.

\section{Experiments}\label{experiments}

I used the BayesianGaussianMixture implementation from
\href{scikit-learn.org}{scikit-learn} to find clusters of countries with
similar living standards.
\href{http://www.sharecsv.com/s/4165c9b03d9fffdef43a3226613ff37c/Countries.csv}{Here}
is the dataset I used. The code used to generate this figure can be found on the Appendix section.

\begin{figure}[H]
	\centering
	\caption{Clustering data about countries' living standards}
		\makebox[\textwidth][c]{\includegraphics[width=1.1\textwidth]{output_60_0.png}}
\end{figure}

\section{Suggestions for future research}\label{proposals-for-future-research}

One direction of research I feel tempted to follow is the application of
Neural Variational Inference as an alternative to classic Variational
Inference and MCMC methods for fitting Dirichlet Process Mixture Models.
Neural Variational Inference has been applied with particular success to
language modelling and other text processing tasks - which are areas
where DPMM are traditionally applied (for instance in Latent Dirichlet
Allocation) - and it aligns with the current trend of leveraging Deep
Learning. Adversarial Networks have also been used with good results to
model complex probabilistic distribution, so perhaps applying them to
DPMMs can also be an interesting research premise.

\nocite{*}
\bibliographystyle{abbrv}
\bibliography{dpmm}

\section*{Appendix}
\subsection{Code to generate Figure 1}
\begin{lstlisting}[language=Python]
import numpy as np
import matplotlib.pyplot as plt

from scipy.stats import dirichlet as Dir

ortog = (1/np.sqrt(3)) * np.ones(3)

x = np.array([1,0,0])
x = x - np.dot(x, ortog) * ortog
x /= np.linalg.norm(x)
y = np.cross(ortog, x)

def project3dsample(sample):
	return np.array([np.dot(sample, x), np.dot(sample, y)])

fig, axs = plt.subplots(1,5, figsize=(25,4))

alphas = [
	np.array([1,1,1]),
	np.array([10,10,10]),
	np.array([0.1,0.1,0.1]),
	np.array([10,1,1]),
	np.array([0.1,0.1,1])
]

for alpha, ax in zip(alphas, axs):
	samples = np.array(
	[project3dsample(sample) for sample in Dir.rvs(alpha, size=1000)])
	ax.set_title("alpha = {}".format(alpha))
	ax.scatter(samples[:,0], samples[:,1], s=3);

plt.show()
\end{lstlisting}

\subsection{Code to generate Figure 2}
\begin{lstlisting}[language=Python]
from scipy.stats import beta
from scipy.stats import norm

def predictive_posterior_plot(N, alpha, ax):
	stick = 1
	pis = []
	for i in range(N):
		pi = stick * beta.rvs(1, alpha)
		pis.append(pi)
		stick -= pi

	pis = np.array(pis)*N/(alpha+N)
	thetas = norm.rvs(size=N)
	x = np.array(range(-3000,3000))*0.001
	y = norm.pdf(x)*alpha/(alpha+N)

	ax.set_title("alpha = {} | N = {} ".format(alpha, N))
	ax.plot(x,y)
	ax.set_ylim((0,max([max(pis),max(y)])+0.01))
	ax.bar(thetas,pis,0.01)

fig, ax = plt.subplots()
predictive_posterior_plot(100,10,ax)
plt.show()
\end{lstlisting}

\subsection{Code to generate Figure 3}
\begin{lstlisting}[language=Python]
import itertools

fig, axs = plt.subplots(3,3, figsize=(20,20))

alphas = [0.5, 1, 10]
Ns = [10, 100, 1000]
axs = axs.flatten()

for (ax, (alpha, N)) in zip(axs, itertools.product(alphas, Ns)):
	predictive_posterior_plot(N, alpha, ax)

plt.suptitle("Several possible predictive functions after observing different numbers of samples,\n"+
"with different concentration parameters, and with a Normal base distribution", fontsize=22)

plt.show()
\end{lstlisting}

\subsection{Code to generate Figure 4}
\begin{lstlisting}[language=Python]
import pandas as pd
df = pd.read_csv("./Countries.csv")

cols_of_interest = ["GDPPC", "Literacy", "InfantMortality", "Agriculture", "Population", "NetMigration"]
y = df[cols_of_interest].values

from sklearn.mixture import BayesianGaussianMixture

m = BayesianGaussianMixture(
	n_components=5,
	weight_concentration_prior=1/5, #alpha
	weight_concentration_prior_type="dirichlet_process",
	max_iter=10000,
	init_params="random"
)

m.fit(y)
preds = m.predict(y)
print(np.bincount(preds))

grouped = dict(zip(range(0,100), [list() for _ in range(0,100)]))

for i in range(len(preds)):
	grouped[preds[i]].append(df.iloc[i]["Name"])

to_del = []
for key in grouped:
	if len(grouped[key]) == 0:
		to_del.append(key)

for key in to_del:
	del grouped[key]

from sklearn.preprocessing import MinMaxScaler

for col in cols_of_interest:
	if col != "NetMigration":
		df[col] = MinMaxScaler(feature_range=(0,1)).fit_transform(df[col].values.reshape(-1,1))
	else:
		df[col] = MinMaxScaler(feature_range=(-1,1)).fit_transform(df[col].values.reshape(-1,1))

def plot_country(ax, country, cluster_key=None):
	x = np.arange(len(cols_of_interest))

	if cluster_key != None:
		rows = df.loc[df["Name"].isin(grouped[cluster_key])][cols_of_interest].values
		y = np.mean(rows, axis=0)
		country = "Cluster {} average".format(cluster_key)
		ax.set_yticks(x)
		ax.set_yticklabels(cols_of_interest, fontsize=22)
		color="orange"
	else:
		y = df.loc[df["Name"] == country][cols_of_interest].values.flatten()
		ax.tick_params(axis="y", which="both", left="off", right="off", labelleft="off")
	color="blue"

	ax.tick_params(axis="x", which="both", bottom="off", top="off", labelbottom="off")
	ax.set_title(country, fontsize=22)
	ax.barh(x, y, height=0.3, alpha=0.65, color=color)

def plot_cluster(axs, key):
	countries = np.random.choice(grouped[key], size=3, replace=False)
	for ax, country in zip(axs[1:4], countries):
		plot_country(ax, country)

	plot_country(axs[0],"", key)

fig, axs_ = plt.subplots(5,4,figsize=(40,60))

top_5 = sorted(grouped.items(), key=lambda x: len(x[1]), reverse=True)[:5]

for (cluster,_), axs in zip(top_5, axs_):
	plot_cluster(axs, cluster)

plt.show()
\end{lstlisting}

\end{document}


\documentclass[11pt]{article}

% Packages for Latex document
\usepackage[utf8]{inputenc}

% Used for expanding text space
\usepackage[a4paper, margin=20mm]{geometry}

% Used for \mathbb{} function
\usepackage{amssymb}

% Used for \text{} function
\usepackage{amsmath}

% Used for writing algorithms
\usepackage{algorithm}
\usepackage{algorithmic}

% Additional formatting
\setlength{\parindent}{0em}
\setlength{\parskip}{0em}

% Begin the document
\begin{document}

% Title card
\begin{titlepage}

    % Implement a title
    \title{Trading Strategy Back-Tester and Bot Trader Creation}

    % Implement the author
    \author{Ivan Silajev}

    % Use date section for extra info
    \date{
    % State organisation
    Warwick Coding Society \\
    % State e-mail of authors
    \texttt{ivan.silajev@warwick.ac.uk}
    }

    % Create the title section in PDF
    \maketitle

    % Implement the contents section
    \tableofcontents

\end{titlepage}

% Start introduction section
\section{Introduction}

% Introduction:
% Sentence for stating what the document is about
This \textbf{Warwick Coding Society} report outlines the theory and the creation of a \textbf{trading strategy back-testing program and a bot trader} using the \textbf{Python} programming language.
\nolinebreak
% Sentence for stating how the document presents the information
This document's approach to presenting the theory involves giving a simple explanation of the relevant topics before introducing the mathematics for the topics.
\nolinebreak
% Sentence stating that the document is a programming tutorial as well
The reader can also follow the code shown throughout the report to practice building their own trading toolkit, which is designed to be flexible.\\

% List mentioning the most important topics the document
The topics covered in this report include:
\begin{itemize}
    \item What is \textbf{trading} and \textbf{arbitrage}?
    \item What is a \textbf{trading strategy} and how use it?
    \item What is \textbf{portfolio optimisation} and how it's done?
\end{itemize}

% Sentence for showing existing packages for back testing and portfolio optimisation
The report will also cover existing back testing and portfolio optimisation Python packages like \textbf{Backtrader} and \textbf{PyPortfolioOpt}.\\

% Sentence explaining the motivation for writing the document
The motivation for this report is to introduce programmers of any skill level to algorithmic trading and back testing in a clear, concise manner, in line with Warwick Coding Society's goal of making programming accessible.
\nolinebreak
% Sentence also mentioning collaborative intention behind document
This report is also part of a collaboration project with the WFS Quant Finance Team.
\nolinebreak
% Sentence mentioning how collaborative aspect stands out
This WCS report was made to perfectly complement the WFS report on 'trading strategy comparisons', as one can replicate the results in that report with the tools they learn in this one.

% Start trading basics section
\section{Trading Basics}

% Section on what is trading
\subsection{What is Trading?}

% A general definition of trading
\textbf{Trading} is the exchange of value between two entities.
\nolinebreak
% A general definition of a market
A \textbf{market} is a field where entities trade.
\nolinebreak
% Mention what the most active markets are with examples
The most active markets in the world today comprise of financial asset markets, including the \textbf{New York Stock Exchange (NYSE)} and the \textbf{National Association of Securities Dealers Automated Quotations (NASDAQ)}.\\

% Motivate the reason for focusing on financial markets only
\textbf{Financial asset markets} are ever-trending due to their increasing accessibility to the general public and the ease with which one can trade financial assets.
\nolinebreak
% State that reason restricts document to financial markets only, though information is applicable to any market
For this reason, this report will be focusing on financial asset markets only, though the methodology presented here is applicable to any market.\\

% Give extra insight on past of algorithmic trading
With the progress of computer technology and financial mathematics in the late 20th century, companies, like \textbf{Renaissance Technologies}, investigated the use of algorithmic trading strategies to automate and optimise their trading activity in financial asset markets.
\nolinebreak
% State that algorithmic trading is now accessible more than ever
Currently, anyone can access financial markets through the many internet brokers that exist, opening the opportunity for anyone to use algorithmic trading methods to automate their trading activity.\\

% State there are two ways of profiting from financial asset trading
There are two common ways to earn profit from trading in financial asset markets.

% List trading profiting techniques
\begin{itemize}

    % Explain long trading
    \item \textbf{Buying (Long Position)}: When one buys an asset on the market at an agreed price and time. Then, they sell the asset on the market when the it increases in price, thus profiting from the increase in the price.

    % Explain short trading
    \item \textbf{Selling (Short Position)}: When one loans assets from the broker and sells them on the market at an agreed price and time. Then, they buy back the asset when it decreases in price and return it to the broker, thus profiting from the decrease in the price.

\end{itemize}

% Point to extra resources for the curious
For more details on how brokers, financial markets and assets work, the reader can resort to the resources mentioned in this report's bibliography.

% Start section on arbitrage
\subsection{What is Arbitrage?}

% Give a definition of arbitrage
\textbf{Arbitrage} involves exploiting inefficiencies across markets to trade more profitably.\\

% Give a definition of arbitrage opportunity
An \textbf{arbitrage opportunity} is characterised by the potential of investing a net zero worth of capital into assets and profiting from them without risk.
\nolinebreak
% Give a simpler definition of arbitrage opportunity that highlights its significance
Simply put, an arbitrage opportunity can give something from nothing without loss.\\

% Explain how can arbitrage opportunities be found
Arbitrage opportunities are found using information that efficiently reflect the current demand for different assets, allowing one to essentially predict what asset will be profitable.
\nolinebreak
% State the generality of arbitrage for markets
Again, arbitrage is a general concept that is applicable to any set of markets.\\

% List basic sources of arbitrage opportunity info sources
The common methods of arbitrage opportunity detection include:
\begin{itemize}

    % State space models for trend analysis and using MPO for optimising portfolio
    \item Multivariate trend analysis

    % Sentiment analysis and other web scraping
    \item Internet social trend analysis

\end{itemize}

% State that there are more sources of arbitrage opportunity information
This is by no means an exhaustive list of arbitrage opportunity detection methods.

% Start section on trading strategy utilisation
\section{Trading Strategy Utilisation Theory}

% Give a general definition of a trading strategy
A \textbf{trading strategy} is a rule for when and how to trade an asset over time given a set of information.
\nolinebreak
% State that trading strategies have a standard
We start by outlining what we expect from a trading strategy.

% Start section explaining what a good trading strategy is
\subsection{The Characteristics of a Good Trading Strategy}

\subsubsection{General Description}

% State the goal of trading
The aim of trade is to achieve \textbf{profit}.
\nolinebreak
% Restate the different ways to trade on financial markets
One can trade in the financial market by either opening long or short positions on financial assets.\\

% How to trade profitably with long positions
\textbf{Long positions} are profitable when assets are bought at low prices and sold at higher prices.
\nolinebreak
% How to trade profitably with short positions
\textbf{Short positions} are profitable when assets are sold at high prices and bought at lower prices.\\

% Summarise the properties of a profitable trading strategy
Therefore, a good trading strategy:
\begin{itemize}
    \item Opens and holds long positions while the asset price increases
    \item Opens and holds short positions while the asset price decreases
    \item Closes long positions when the asset price starts decreasing
    \item Closes short positions when the asset price starts increasing
\end{itemize}

% Create section for initial mathematical concepts
\subsubsection{Mathematical Preliminaries}

% Start with defining the time domain
Define the set of times for which the price data of a given asset is observed as the countable set $T \subset \mathbb{R}$.\\

% Define the four price series
Brokers and financial websites provide data on the historical prices of assets, including the close, open, low and high prices of assets for different time intervals.
\nolinebreak
Define the close, open, low and high prices as follows:
\begin{itemize}
    \item Close price series: $P_{c}:T \mapsto \mathbb{R^{+}}$
    \item Open price series: $P_{o}:T \mapsto \mathbb{R^{+}}$
    \item Low price series: $P_{l}:T \mapsto \mathbb{R^{+}}$
    \item High price series: $P_{h}:T \mapsto \mathbb{R^{+}}$
\end{itemize}

These series can be used for the construction of \textbf{indicator series}.

% Introduce indicator series
\subsection{Indicator Series}

\subsubsection{General Description}

% State what type of information trading strategies require for being good
A trading strategy involves using information about the asset price trend.
\nolinebreak
% State the form in which this information exists in
This information is usually in the form of a time series, known as an \textbf{indicator series}, that shows when the price asset is increasing or decreasing.\\

% Summarise what an indicator series represents
An indicator series is:
\begin{itemize}
    \item Positive when the price is increasing
    \item Negative when the price is decreasing
    \item Zero when the price is stable
\end{itemize}

% Begin mathematical description of an indicator series
\subsubsection{Mathematical Description}

Define an indicator time series $I:T \mapsto \mathbb{R}$ as a function of time.\\

An example of a trivial indicator series is the difference between the close and open prices of the asset traded:
\begin{center}
    $I(t) = P_c(t) - P_o(t)$
\end{center}

Another example of a trivial indicator series involves using the first order difference of the closed prices:
\begin{center}
    $I(t) = P_c(t) - P_c(t - \Delta_t)$
\end{center}

Where $\Delta_t$ is the difference between time $t$ and the latest time before $t$ in set $T$, or, more precisely:
\begin{center}
    $\Delta_t = t - \sup \left ( T \cap (\infty, t) \right )$
\end{center}

Deriving reliable indicators is a separate science that quant traders specialise in. The WFS report on trading strategy comparisons gives some insight into the most commonly used indicators to date.

\subsection{Trade Signal Series}

\subsubsection{General Description}

A \textbf{trade signal series} gives information on when a given trade type should be open according to an underlying indicator series.\\

The long trade signal series is:
\begin{itemize}
    \item Equal to one when the indicator is strictly positive
    \item Equal to zero otherwise
\end{itemize}

The short trade signal series is:
\begin{itemize}
    \item Equal to one when the indicator is strictly negative
    \item Equal to zero otherwise
\end{itemize}

\subsubsection{Mathematical Description}

The \textbf{long trade signal series} $V_l:T \mapsto \{ 0,1 \}$ is defined as:
\begin{center}
    $V_l(t) = \text{sign}(\text{max}\{I(t),0 \})$
\end{center}

The \textbf{short trade signal series} $V_s(t)$ is trivially derived by changing the sign of the indicator.
\begin{center}
    $V_s(t) = \text{sign}(\text{max}\{-I(t),0 \})$
\end{center}

\subsection{Action Series}

\subsubsection{General Description}

An \textbf{action series} informs exactly when to open or close trades of a given type based on its underlying trade signal series.\\

The action series is:
\begin{itemize}
    \item Equal to one when the trade must be opened
    \item Equal to zero when the position must be held (inaction)
    \item Equal to minus one when the trade must be closed
\end{itemize}

\subsubsection{Mathematical Description}

The \textbf{action series} $A:T \mapsto \{ -1,0,1 \}$, for a given trade signal series $V(t)$, is defined as:
\begin{center}
    $A(t) = \begin{cases}
V(t) & \text{ if } t= \inf{T}\\
V(t) - V(t - \Delta_t) & \text{ if }  t > \inf{T}
\end{cases}$
\end{center}

\subsection{Alpha Series}

Finally, the \textbf{alpha series} shows the ratio of the value of the investment with its previous value at all times under the use of a trading strategy.\\

When the trade is inactive, the alpha series will take the value one, since no capital is invested in the asset. When the trade is active, the alpha series will output the ratio change in the investment according to the price series.\\

The alpha series of a given asset and trading strategy is what's used to evaluate the performance of the trading strategy for the asset.

\subsubsection{Mathematical Description}

The alpha series $\alpha : T \mapsto \mathbb{R}^{+}$, for a given trade signal series $V(t)$ and action series $A(t)$, is generated using the following algorithm:

\begin{algorithmic}[1]
\REQUIRE $T \wedge V(t) \wedge A(t) \wedge P_c(t) \wedge r$
\ENSURE $\alpha(t)$
\FOR{$t = \inf T$ \TO $\sup T$}
\IF{$A(t) = 1$}
\STATE $\alpha(t) \leftarrow 1$
\STATE $P_0 \leftarrow P_c(t)$
\ELSIF{$A(t) = 0$}
\IF{$V(t) = 0$}
\STATE $\alpha(t) \leftarrow 1$
\ELSIF{$V(t) = 1$}
\STATE $\alpha(t) \leftarrow (P_0 + r \cdot (P_c(t) - P_0))/(P_0 + r \cdot (P_c(t- \Delta_t) - P_0))$
\ENDIF
\ELSIF{$A(t) = -1$}
\STATE $\alpha(t) \leftarrow (P_0 + r \cdot (P_c(t) - P_0))/(P_0 + r \cdot (P_c(t- \Delta_t) - P_0))$
\ENDIF
\ENDFOR
\end{algorithmic}
The $r$ term is the proportion of invested capital dedicated to the trade. A positive $r$ generates an alpha series for a long position strategy and a negative $r$ generates an alpha series for a short position strategy.

\section{Portfolio Implementation Theory}

\subsection{Why are Portfolios Important?}
A \textbf{trading portfolio} is an allocation of capital dedicated to a set of assets for trade. Portfolios can significantly affect the efficiency of one's trading activity. Dedicating a more capital into assets with volatile prices may be irrational if there exist more stable assets with higher average returns.\\

The \textbf{volatility} of a random variable is its degree of variation. An asset price that deviate more from its trend that anther asset price is more volatile than the other. Usually, portfolios are optimised according to the volatility and average returns of the asset prices.

\subsection{Portfolio Distribution Series}

Since a portfolio is an allocation of capital, it is expressed as a vector function called a \textbf{portfolio distribution series} $\pi:T \mapsto [0,1]^{n}$ with entries summing to one. The $i$th entry of $\pi(t)$, is the proportion $\pi_{i}(t)$ of capital dedicated towards asset $i \in I$ for trade at time $t \in T$, where $I$ is the index set for the assets.\\

Setting $\alpha_{i}(t)$ to be the alpha series of the $i$th asset, define the portfolio alpha series as follows:
\begin{center}
    $\alpha_{\pi}(t)=\sum_{i \in I}\alpha_{i}(t)\pi_{i}(t)$
\end{center}

The \textbf{portfolio alpha series} is shows the ratio of the value of the total invested capital with its previous value at all times.

\subsection{The Equal Weight Portfolio}

The \textbf{equal weight portfolio}, or the \textbf{trivial portfolio}, is one that uniformly allocates capital across all assets that are being traded. Letting $V_i(t)$ be the trade signal series of asset $i \in I$, the trivial portfolio is such that:
\begin{center}
    $\pi_i(t) = \begin{cases}
    \dfrac{V_i(t)}{\sum_{i \in I}V_i(t)} & \text{ if } \sum_{i \in I}V_i(t) \neq 0 \\
    0 & \text{ if } \sum_{i \in I}V_i(t) = 0
    \end{cases}$
\end{center}

\subsection{Markowitz Modern Portfolio Theory}

\textbf{Markowitz MPT (Modern Portfolio Theory)} one of the most successful and useful portfolio optimisation methods created and used to date. MPT optimises a portfolio by maximising its average returns and minimising its variance, used as a measure of volatility in this case. It utilises information on the returns of individual assets, including their average returns and return covariances.\\

Let $\mu(t)$ be the \textbf{vector of average returns} of all assets in $I$ at time $t \in T$. So, $\mu_{i}(t)$ is the average returns of asset $i$ at time $t$.\\

Let $\Sigma(t)$ be the \textbf{matrix of covariances between returns} of all assets in $I$ at time $t \in T$. So, $\Sigma_{ij}(t)$ is the covariance between the returns of asset $i$ and $j$ at time $t$.\\

Let $d(t)$ be the \textbf{wealth distribution}, showing the proportion of wealth to be invested in each asset. The wealth distribution is different to the portfolio distribution $\pi(t)$, since the entries of $d(t)$ can be any real value all summing to one. $d_i(t)$ is the wealth distributed to asset $i$ at time $t$.\\

The entries of the portfolio distribution are derived from the wealth distribution as follows:
\begin{center}
    $\pi_i(t) = \dfrac{|d_i(t)|}{\sum_{i \in I}|d_i(t)|}$
\end{center}

\newpage

There exist two special wealth distributions.
\begin{itemize}
    \item The \textbf{minimal variance risky wealth distribution}:
    \begin{center}
        $d_M(t) = \dfrac{\Sigma^{-1}(t) \cdot \mathbf{1}}{\mathbf{1}^{T} \cdot \Sigma^{-1}(t) \cdot \mathbf{1}}$
    \end{center}
    With its corresponding average return $d_M(t) \cdot \mu(t)= \mu_M(t)$.
    \item The \textbf{zero rate tangency wealth distribution}:
    \begin{center}
        $d_T(t) = \dfrac{\Sigma^{-1}(t) \cdot \mu(t)}{\mathbf{1}^{T} \cdot \Sigma^{-1}(t) \cdot \mu(t)}$
    \end{center}
    With its corresponding average return $d_T(t) \cdot \mu(t)= \mu_T(t)$.
\end{itemize}

The \textbf{optimal wealth distribution} is derived by solving the following optimisation problem for some desired average return $\mu_0(t)$:
\begin{center}
        $\begin{matrix}
\text{minimise} & d(t)^T \cdot \Sigma(t) \cdot d(t), & d(t) \in \mathbb{R}^{n} \\
\text{subject to} & d(t) \cdot \mathbf{1}=1, & \\
 & d(t) \cdot \mu(t) \geq \mu_0(t) &
\end{matrix}$
    \end{center}

The problem involves deriving the wealth distribution that minimises the variance of the total returns with a set of necessary constraints. The unique solution to the problem is given by:
\begin{center}
    $d_0(t)= \dfrac{\mu_T(t) - \mu_0(t)}{\mu_T(t) - \mu_M(t)} d_M(t) + \dfrac{\mu_0(t) - \mu_M(t)}{\mu_T(t) - \mu_M(t)} d_T(t)$
\end{center}

Therefore, it is best to use $d_0(t)$ for a desired average returns of $\mu_0(t)$.

\section{Back Testing and Trading Bot Implementation}

\end{document}


\chapter{To verify truth table for NAND gate}
%\ref{sec:background}.

%\section{Aim}
%\label{sec:objectives}
%	To verify the truth table for AND Gate.

\section{Apparatus}
%\label{sec:objectives}
	\begin{itemize}
		\tightlist
		\item Kit for realization of gates
		\item Connecting Leads
	\end{itemize}

\section{Theory}
	This is a NOT-AND gate which is equal to an AND gate followed by a NOT gate. The outputs of all NAND gates are high if any of the inputs are low. The symbol is an AND gate with a small circle on the output. The small circle represents inversion.
	\begin{figure}[h]
		\centering
		\includegraphics{img/exp4/1}
		\caption{Symbol for NAND gate}
		\label{fig:4:1}
	\end{figure}
	\begin{figure}[h]
		\centering
		\includegraphics{img/exp4/2}
		\caption{Truth Table for NAND gate}
		\label{fig:4:2}
	\end{figure}

	A simple 2-input logic NAND gate can be constructed using RTL (Resistor-transistor-logic) switches connected together as shown below with the inputs connected directly to the transistor bases. Either transistor must be cut-off or “OFF” for an output at Q.

	\begin{figure}[h]
		\centering
		\includegraphics{img/exp4/3}
		\caption{Circut for making NAND gate}
		\label{fig:4:3}
	\end{figure}

\section{Procedure}
	\subsubsection{Simulator 1}
	\begin{itemize}
		\tightlist
		\item Connect the supply(+5V) to the circuit.
		\item Press the switches for inputs "A" and "B".
		\item The bulb glows if any one or both the switches are OFF else it won't glow.
		\item Repeat step-2 and step-3 for all state of inputs.
	\end{itemize}

	\subsubsection{Simulator 2}
	\begin{itemize}
		\tightlist
		\item Enter the Boolean input "A" and "B".
		\item Enter the Boolean output for your corresponding inputs.
		\item Click on "Check" Button to verify your output.
		\item Click "Print" if you want to get print out of Truth Table.
	\end{itemize}

\section{Observations}
	\begin{figure}[h]
		\centering
		\includegraphics[width=0.9\linewidth]{img/exp4/4}
		\caption{}
		\label{fig:4:4}
	\end{figure}
		\begin{figure}[h]
		\centering
		\includegraphics[width=0.9\linewidth]{img/exp4/5}
		\caption{}
		\label{fig:4:5}
	\end{figure}
		\begin{figure}[h]
		\centering
		\includegraphics[width=0.9\linewidth]{img/exp4/6}
		\caption{}
		\label{fig:4:6}
	\end{figure}
		\begin{figure}[h]
		\centering
		\includegraphics[width=0.9\linewidth]{img/exp4/7}
		\caption{}
		\label{fig:4:7}
	\end{figure}

\section{Conclusion}
NAND gate basically negates the results of AND gate . The output is low when both the inputs are high and the output is high when even one of the input is low.

\section{Precautions}
	\begin{enumerate}
		\tightlist
		\item Make the connections when power supply is OFF.
		\item Ensure that the connections are tight.
		\item Change the status of inputs only when power supply is OFF.
	\end{enumerate}