\documentclass{beamer}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{wrapfig}
\usepackage{amsfonts}
\usepackage{csquotes}
\usepackage{epigraph}
\usepackage{float}
\usepackage{lipsum}
\usepackage{blindtext}
\usepackage{multirow}
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{bm}
\usepackage{xcolor}
\usepackage{upgreek}
\usepackage{calc}
\usepackage{lscape}
\usepackage[font={scriptsize}]{caption}
\usepackage{systeme}

% TYPESETTING SUGAR
\def\leq{\leqslant}
\def\geq{\geqslant}

\usetheme{Madrid}
\usecolortheme[rgb={0.0,0.4,0.5}]{structure}
%-----------------------------------------------%
%-----------------------------------------------%

%
% TITLE SLIDE
%

\makeatletter
\setbeamertemplate{footline}{%
  \leavevmode%
  \hbox{\begin{beamercolorbox}[wd=.4\paperwidth,ht=2.5ex,dp=1.125ex,leftskip=.20cm plus1fill,rightskip=.20cm, center]{author in head/foot}%
    \usebeamerfont{author in head/foot}\insertshortauthor%
  \end{beamercolorbox}%
  \begin{beamercolorbox}[wd=.6\paperwidth,ht=2.5ex,dp=1.125ex,leftskip=.20cm,rightskip=.20cm plus1fil, center]{title in head/foot}%
    \usebeamerfont{title in head/foot}\insertshorttitle \hspace*{2.5ex}
    \insertframenumber{}/\inserttotalframenumber
  \end{beamercolorbox}}%
  \vskip0pt%
}
\makeatother

\title[\textit{CoViD-19} daily positive-case count prediction for Northern India]{\textit{CoViD-19} daily positive-case count \\prediction for Northern India}

\author[E. Ballarin $|$ R. Corti $|$ A. Tasciotti]{Emanuele Ballarin \\ Roberto Corti \\ Arianna Tasciotti}
\institute[]{Department of Mathematics and Geosciences, University of Trieste}
\date[]{\textit{Statistical Methods for Data Science}\\ Final Project $\sim$ July 2020}

\titlegraphic{\includegraphics[width=1.25cm,height=1.3cm,keepaspectratio]{dssclogo.png}
	\hspace*{93.04mm}~% Horizontal space is tailored for 1.3cm square logos
	\includegraphics[width=1.3cm,height=1.25cm,keepaspectratio]{logouniv.png}
}

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

%
% ADAPTIVE TABLE OF CONTENTS
%

\AtBeginSection[]
{
	\begin{frame}
		\frametitle{\textit{We are here...}}
		\tableofcontents[currentsection]
	\end{frame}
}

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

%
% DOCUMENT
%

\begin{document}

% Insert title slide
\frame{\titlepage}

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

\section{\textit{Introductory Overview}}{

%% P.S. 01
\begin{frame}
\frametitle{\textit{CoViD-19} in India: What and Why}
\framesubtitle{A global pandemic hitting hard the World's most populous democracy}
\begin{itemize}
	\item{\textbf{CoViD-19}: clinical expression of the \textbf{human-host} infectious disease caused by \textit{SARS-CoV-2}. Probably resulted from \textit{animal spillover}.}
	\item{Identified during \textbf{Dec 2019}, in Wuhan (Hubei, China).\\As of Jul, 1, 2020: $> 10.4\ \text{mln}$ cases, $> 511\text{k}$ deaths worldwide.}
\end{itemize}
\hfill
\begin{itemize}
	\item{\textbf{India}:\\Outbreak: ongoing since January 2020;\\1st $\in$ Asia and 4th $\in$ World country w.r.t. number of confirmed cases.}
	\item{Further focus on \textbf{Northern India}, i.e. the $9$ regions of: \\
	\textit{Uttar Pradesh, Chandigarh, Haryana, Delhi, Himachal Pradesh, Jammu and Kashmir, Punjab, Rajasthan, Uttarakhand}.}
\end{itemize}
\end{frame}

%% P.S. 02
\begin{frame}
	\frametitle{Formal Problem Statement}
	\framesubtitle{Understanding aims and limits of present work}
		Predict the number of new \textbf{daily} confirmed cases of \textit{CoViD-19} for \textbf{Northern India}, on a \textit{per-state} basis.
		%\hfill
		\break
		\begin{itemize}
			\item{Up to \textbf{$\textbf{10}$ days} since the last known daily \textit{data-point};}
			\item{While also providing \textbf{predictive uncertainty} information;}
			\item{Trying to favor \textbf{parameter parsimony}, \textbf{theoretically-sound} methods, \textbf{explainability} and \textbf{interpretability} of the results;}
			\item{By using only publicly-available data from the Internet.}
		\end{itemize}
	\hfill

	\textit{Medical data $\rightarrow$ extra-care required!}
\end{frame}
}
\section{\textit{DataWorks}}{
%---------------------------------------------%
%% D.C. 01
\begin{frame}
	\frametitle{Data Gathering}
	\framesubtitle{Finding the source(s) of knowledge}
		\begin{itemize}
			\item{
				Kaggle (Sudalai Rajkumar):
				\begin{itemize}
					\item{Age grouping, early-outbreak ($<$ Mar 2020) [IMHW];}
					\item{Per-state/per-date individual counting data [IMHW];}
					\item{Confirmed individuals, early-outbreak [\textit{covid19india}, volunt.];}
					\item{Statewise geopolitical / hospital information [Indian Census 2011].}
				\end{itemize}
			}
		\hfill
		\item{
	\textit{covid19india}'s API:
	\begin{itemize}
		\item{Per-state/per-date individual counting data; additional [volunt.];}
		\item{Confirmed individuals, early-outbreak; additional [volunt.].}
	\end{itemize}
}
		\end{itemize}

	\hfill\break
	In a pipelined, incremental, \textbf{reproducible} manner (\textsf{bash}, \textsf{binutils}, \textsf{R}).

	\hfill\break

	Self-contained \textit{data arrangement}: \textbf{KF-CV} and \textbf{train/test} split.

\end{frame}

%% D.C. 02
\begin{frame}
	\frametitle{Data Cleaning}
	\framesubtitle{Making \textit{human world} more \textit{machine-friendly}}

	\textbf{Principled} drop of data:
	\begin{itemize}
		\item{Unnecessary data for the task considered (e.g. \textit{lat/long} of labs);}
		\item{Technical-only information (e.g. IDs, index keys);}
		\item{Non-easily-processable data (with \textit{permitted tools}; e.g. raw text).}
	\end{itemize}

	\hfill

	Removal of to-be-\textbf{duplicated} data:
	\begin{itemize}
		\item{To avoid \textit{by-design} duplication and variance inflation.\\ \textit{More on this in the forthcoming!}}
	\end{itemize}

	\hfill

	Dealing with \textsf{\textbf{NA}}s:
	\begin{itemize}
		\item{Simple removal ($\rightarrow$ upfront information loss);}
		\item{Data-carry-on ($\rightarrow$ information-theoretical justification for T.S.s).}
	\end{itemize}

\end{frame}

%---------------------------------------------%
%%Slide 1.3.1
\begin{frame}
	\frametitle{Data pre-processing}
	\framesubtitle{Making \textit{machine-friendly} more \textit{model-friendly}}

	Data transformations:
	\begin{itemize}
		\item Numerical encoding of categorical or string variables (e.g. date, sex);
		\item Daily averaging of patient information;
		\item Compressed, still informative quantities \\ (e.g. urban population, rural population $\rightarrow$ urbanization).
	\end{itemize}
	~\\
	Introduction of new variables:
	\begin{itemize}
		\item Daily metrics (e.g. daily positives, daily swabs);
		\item 3-, 4- lagged components (daily swabs), 10- lagged components (daily positive and average daily patient's age and sex).
	\end{itemize}

\end{frame}
%---------------------------------------------%
%---------------------------------------------%
%% SLIDE 1.4.1:
\begin{frame}
	\frametitle{Exploratory Data Analysis: Demographic features (1)}
	\framesubtitle{A country of contrasts}
	\center \includegraphics[scale = 0.095]{den.png}
\end{frame}
%---------------------------------------------%
%% SLIDE 1.4.2:
\begin{frame}
	\frametitle{Exploratory Data Analysis: Demographic features (2)}
	\framesubtitle{Large rural, small urban states}
	\center \includegraphics[scale = 0.1]{urb.png}
\end{frame}
%---------------------------------------------%
%% SLIDE 1.4.3:
\begin{frame}
	\frametitle{Exploratory Data Analysis: Health system organization (1)}
	\framesubtitle{The city is still the center of healthcare}
	\center \includegraphics[scale = 0.1]{urbanh.png}
\end{frame}
%---------------------------------------------%
%% SLIDE 1.4.4:
\begin{frame}
	\frametitle{Exploratory Data Analysis: Health system organization (2)}
	\framesubtitle{The city is still the center for healthcare}
	\center \includegraphics[scale = 0.1]{ruralh.png}
\end{frame}
%---------------------------------------------%
%% SLIDE 1.4.5:
\begin{frame}
	\frametitle{Exploratory Data Analysis: $\#$ positive cases per region}
	\framesubtitle{Not always a matter of population / landsize}
	\center \includegraphics[scale = 0.1]{pos.png}
\end{frame}
%---------------------------------------------%
%% SLIDE 1.4.6:
\begin{frame}
	\frametitle{Exploratory Data Analysis: Trend of positive cases}
	\framesubtitle{An exponential trend, maybe?}
	\center \includegraphics[scale = 0.085]{pos_date.png}
\end{frame}
%---------------------------------------------%
%% SLIDE 1.4.7:
\begin{frame}
	\frametitle{Exploratory Data Analysis: Trend of average age}
	\framesubtitle{The more vulnerable suffer the most}
	\center \includegraphics[scale = 0.085]{avg_age_pos.png}
\end{frame}
%---------------------------------------------%
%% SLIDE 1.4.8:
\begin{frame}
	\frametitle{Exploratory Data Analysis: Trend of average sex}
	\framesubtitle{No evident sex prevalence}
	\center \includegraphics[scale = 0.085]{avg_sex_pos.png}
\end{frame}
%---------------------------------------------%
}

\section{\textit{A world of models of the World}}{
%---------------------------------------------%
%% SLIDE 1.4:
\begin{frame}
	\frametitle{GLM: \textit{A recap}}
	\framesubtitle{Why not just linear?}

		\begin{itemize}
		\item{Difficult modeling of \textbf{epidemiological dynamics} with (mean) linear responses;}
		\item{Nonlinear trends confirmed by visual exploratory analysis.}
		\end{itemize}
		\hfill

		\pause \center A \textbf{Linear Model} for regression: $$\text{E}[\textbf{Y}] = \textbf{X} \beta$$

		\hfill

		\pause A \textbf{Generalized Linear Model} for regression: $$\text{E}[\textbf{Y}] = \text{g}^{-1}(\textbf{X} \beta)$$
		$$ \text{Var}[\textbf{Y}] = \phi V(\text{E}[\textbf{Y}])$$
\end{frame}

\begin{frame}
	\frametitle{GLM for count data:}
	\framesubtitle{Choice of family and link function}

	\textbf{Poisson}: $$\textbf{Y} \sim \text{Poisson}(\lambda)$$
				  $$f(\textbf{Y};\lambda)=\frac{e^{-\lambda}\lambda^{\textbf{Y}}}{\textbf{Y}!}$$

	\hfill

	\textbf{Negative Binomial}: $$\textbf{Y} \sim \text{NBi}(k,\alpha)$$
				  $$f(\textbf{Y};k,\alpha)=\binom{\textbf{Y}+k-1}{k-1} \frac{\alpha^{\textbf{Y}}}{(1+\alpha)^{\textbf{Y}+k}}$$

	\hfill

	\pause \textbf{Log link function} $\rightarrow$ \textit{exponential} dynamics.

\end{frame}

\begin{frame}
	\frametitle{GLM: \textit{Predictors and Interactions} (1)}
	\framesubtitle{A matter of choosing \textbf{what} to see}

	General criterion ($\leftarrow$ \textit{NB, P, QP}):
	\begin{itemize}
		\item{Add one/some predictor(s);}
		\item{\textit{5-fold CV} \textit{cumulative MAE}} and \textit{validation cumulative MAE;}
		\item{Significance analysis on the linear coefficients.}
	\end{itemize}

		\hfill

	Starting with no \textit{lagged} component, all \textit{non-interacting} predictors:\\
	$\rightarrow$ \textbf{sequential} predictor addition always produces $r\mathcal{D}ev$ decrease;\\
	$\rightarrow$ max. \textbf{overall} significance for all parameters, for every model family.
\end{frame}
%---------------------------------------------%

\begin{frame}
	\frametitle{GLM: \textit{Predictors and Interactions} (2)}
	\framesubtitle{A matter of choosing \textbf{how} to see}

	General criterion, but also:
\begin{itemize}
	\item{Max. $2^{nd}$-order interaction;}
	\item{\textit{Non sunt multiplicanda entia sine necessitate}.}
\end{itemize}

	\hfill

	\underline{Chosen interactions} ($\leftarrow$ \textit{P, QP}):
	\begin{itemize}
		\item{\textsf{Density:Urbanization} $\rightarrow$ the \textit{vertical city} hypothesis;}
		\item{\textsf{Males:Urbanization} $\rightarrow$ the \textit{male migration} hypothesis in India}.
	\end{itemize}

	\hfill

	The case of \textsf{Urbanization}: \textit{significance absorption} and \textit{predictive accuracy}.

\end{frame}
%---------------------------------------------%

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

\begin{frame}
	\frametitle{GLM: \textit{Predictors and Interactions} (3)}
	\framesubtitle{A matter of choosing \textbf{when} to see}

	Introduction of \textit{lagged} components:
	\begin{itemize}
		\item{Justified theoretically by phenomenon structure;}
		\item{\textit{But will they really work?}}
	\end{itemize}

	\hfill

	General criterion ($\leftarrow$ \textit{NB, P, QP}):
	\begin{itemize}
		\item{Add one/some \textit{lagged} component(s);}
		\item{\textit{Validation cumulative MAE};}
		\item{Significance analysis on the \textit{lagged}-component coefficients.}
	\end{itemize}

	\hfill

	\underline{Results}:\\
	$\rightarrow$ No \textit{cross-regressive lagged} predictor is useful;\\
	$\rightarrow$ Autoregressing on response (lagged at $10$ days) helps P/QP.

\end{frame}

\begin{frame}
	\frametitle{GLM: \textit{Predictors and Interactions} (4)}
	\framesubtitle{A picture is worth a thousand words: the final model (QP)}

	\begin{center}
		\center \includegraphics[scale = 0.3]{summary_qpoisson.png}
	\end{center}

\end{frame}
%---------------------------------------------%
%% SLIDE 1.5:
\begin{frame}
	\frametitle{GLM: \textit{Predictors and Interactions} (5)}
	\framesubtitle{A picture is worth a thousand words: the MLE model (P)}
		\center \includegraphics[scale = 0.3]{summary_poisson.png}
	\end{frame}
%---------------------------------------------%
%% SLIDE 1.5.1:
\begin{frame}
	\frametitle{GLM: \textit{Predictors and Interactions} (6)}
	\framesubtitle{A picture is worth a thousand words: the case of \textsf{Urbanization}}
	\center \includegraphics[scale = 0.38]{summary_qpoisson_urb.png}
\end{frame}
%---------------------------------------------%
%% SLIDE 1.5.2:
\begin{frame}[t]

	\frametitle{GLM: \textit{Determining prediction intervals}}
	\framesubtitle{A \textit{bootstrap} way }
	Generally, prediction intervals for GLMs are not easily computable...
	\\~\\
	Bootsrap-based solution:
	\begin{itemize}
		\item Fit the GLM, and collect the regression statistics $\hat{\beta}$ and $\text{Cov}(\hat{\beta})$:
		\item Simulate M draws of $\hat{\beta}^{*} \sim N(\hat{\beta}, \hat{\mathrm{Cov}}(\hat{\beta}))$;
		\item Simulate $y^*| x$ from response $g^{-1}(x \hat{\beta}^{*})$ and a variance determined by the response distribution;
		\item Determine the $\alpha/2$ and $1-\alpha/2$ empirical quantiles of the simulated response $y^*|x$ for each x.
	\end{itemize}
	~\\
	Possibility to reparametrize a Quasipoisson model into a Negative Binomial using $\mathrm{QP}(\mu, \theta) = \mathrm{NegBin}(\mu, \theta = \frac{\mu}{\phi - 1}).$
\end{frame}
%---------------------------------------------%

%---------------------------------------------%
%% SLIDE 1.6:
\begin{frame}[t]
	\frametitle{MARS: \textit{A recap} (1)}
	\framesubtitle{A \textit{nice} piecewise linear model (Friedman, 1991)}

	Multivariate Adaptive Regression Splines (MARS) model response as a weighted sum of basis functions
	$$ \widehat{f}(x) = \sum_{i=1}^k c_i B_i(x). $$

	The basis functions $B_i(x)$ takes one of the three forms:

	\begin{itemize}
		\item a constant 1;
		\item hinge function:  $\max(0, x - k)$ or $\max(0, k - x), ~ k: \ \text{``knot''}$;
		\item product of two or more hinge functions.
	\end{itemize}

\end{frame}

\begin{frame}[t]
	\frametitle{MARS: \textit{A recap} (2)}
	\framesubtitle{The model building process}
	Two-stage approach:
	\begin{itemize}
		\item \textit{Forward pass}: incrementally adding basis function that gives maximum reduction in sum-of-squares residual error;
		\item \textit{Backward pass}: delete least effective term using GCV criterion.
	\end{itemize}

	~\\ \textit{HP-tuning}: \\
	$\rightarrow$ grid-search, 5-fold CV minimizing the cumulative MAE:
	\begin{itemize}
		\item degree: 1 by 1, from 1.
	\end{itemize}

\end{frame}

\begin{frame}[t]
	\frametitle{MARS: \textit{A recap} (3)}
	\framesubtitle{\textit{Why that?}}
	A non parametric regression method.
	\\~\\
	Pros:
	\begin{itemize}
		\item More flexible than linear models;
		\item Simple to understand and interpret compared to other ML-based solutions;
		\item Automated modelling of interactions.
	\end{itemize}

	Cons:
	\begin{itemize}
		\item The resulting fitted function is not smooth (not differentiable along predictors);
		\item \textit{Greedy optimization vs. global optimization}.
	\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{MARS: \textit{Predictors and Interactions}}
	\framesubtitle{A summary}
	\center \includegraphics[scale = 0.26]{summary_mars.png}
\end{frame}

\begin{frame}
	\frametitle{MARS: \textit{Determining prediction intervals}}
	\framesubtitle{Residual model (Stephen Milborrow, 2019)}

	We do the following:

	\begin{itemize}
		\item Estimate the mean absolute error at each point using a \textit{residual model};
		\item Assuming normality, re-scale the error to an estimated standard deviation: $sd \simeq \sqrt{\frac{\pi}{2}} \text{E}[{|\epsilon|}]$;
		\item Convert the standard deviation to an estimated prediction interval for a given level $\alpha$:
		$$ [\hat{Y}-z_{\alpha/2}sd, \hat{Y}+z_{\alpha/2}sd]. $$
	\end{itemize}

\end{frame}
%---------------------------------------------%

\begin{frame}
	\frametitle{RF: \textit{A recap} (1)}
	\framesubtitle{Harnessing the \textit{wisdom of the crowd}}

	Based on the \textbf{decision tree} (regression) model and learning algorithm.

	\hfill

	Enhancements:
	\begin{itemize}
		\item{\textbf{Bootstrap} sampling on \textit{training set};}
		\item{\textbf{Random} feature selection;}
		\item{\textbf{Ensembling} of aggregates}.
	\end{itemize}

	\hfill

	\textit{HP}-tuning:\\
	$\rightarrow$ grid-search, \textit{5-fold CV} test \textit{cumulative MAE}:
	\begin{itemize}
		\item{\textsf{mtry}: $1$ by $1$, from $1$ $\rightarrow$ 5;}
		\item{\textsf{num.trees}: $10$ by $10$, from $500$ $\rightarrow$ 850.}
	\end{itemize}

\end{frame}

\begin{frame}
	\frametitle{RF: \textit{A recap} (2)}
	\framesubtitle{\textit{Why that?}}

	On the opposite side of the spectrum w.r.t. GLMs.

	\hfill

	Pros:
	\begin{itemize}
		\item{Typical \textit{fire and forget} \textbf{ML}-based solution;}
		\item{Often requires minimal \textit{HP-tuning};}
		\item{\textit{Automated} modeling of \textbf{interactions}.}
	\end{itemize}

	\hfill

	Cons:
	\begin{itemize}
		\item{Only averaged or modal \textbf{stepwise} regression capability;}
		\item{Difficult to interpret;}
		\item{May sometimes overfit or fit noise;}
		\item{\textbf{Non-deterministic} behavior and seed-dependence.}
	\end{itemize}

\end{frame}

\begin{frame}
	\frametitle{RF: \textit{Determining prediction intervals}}
	\framesubtitle{\textit{Always beware of absolute certainty!} (Zhang et al., 2019)}

	A well-acknowledged problem for \textit{tree-based} models...

	\hfill

	...and a new proposal:
	$$
	\left[ { \hat{Y} + D_{\left[ { n, \alpha/2 } \right]}, \hat{Y} + D_{\left[ { n, 1-\alpha/2 } \right]} } \right]
	$$

	\hfill

	\begin{itemize}
		\item{Uses the \textit{empirical quantile distribution} of \textbf{OOB prediction} errors;}
		\item{Well-grounded from the \textbf{asymptotic convergence} p.o.v.;}
		\item{Easy to compute, often partially pre-computed \textit{for free};}
		\item{\textsf{R} implementation already available!}
	\end{itemize}
\end{frame}

}

\section{\textit{Results}}{
	%---------------------------------------------%
	%% SLIDE 1.8:
	\begin{frame}
		\frametitle{QuasiPoisson model}
		\framesubtitle{Predictions and Prediction Intervals}
		\center \includegraphics[scale = 0.37]{qp_ppi.png}
	\end{frame}
	%---------------------------------------------%
	%% SLIDE 1.9:
	\begin{frame}
		\frametitle{MARS model}
		\framesubtitle{Predictions and Prediction Intervals}
		\center \includegraphics[scale = 0.37]{mars_ppi.png}
	\end{frame}
	%---------------------------------------------%
	%% SLIDE 1.10:
	\begin{frame}
		\frametitle{Random Forest model}
		\framesubtitle{Predictions and Prediction Intervals}
		\center \includegraphics[scale = 0.37]{rf_ppi.png}
	\end{frame}
	%-----------------------------------------------%
	%% SLIDE 1.11:
	\begin{frame}
		\frametitle{Accuracy measure: RMSE}
		\framesubtitle{Accuracy is not everything}
		\center \includegraphics[scale = 0.2]{acc_tab.png}
	\end{frame}
}

\section{\textit{Conclusions}}{
	%---------------------------------------------%
	%% SLIDE 1.12:
	\begin{frame}
		\frametitle{Final remarks}
		\framesubtitle{\textit{ML vs. Stats} doesn't belong to the real world}
		\hfill \break
		Parametric model:
		\begin{itemize}
			\item{QuasiPoisson model correctly captures the pandemic trend;}
			\item{On average, it has a higher RMSE than non parametric models, partly due to noisy data.}
		\end{itemize}
		\hfill \break
		Non parametric models:
		\begin{itemize}
			\item{Sometimes MARS and Random Forest favor local over global trends;}
			\item{Random Forest predictions are unstable.}
		\end{itemize}
		\hfill \break
		Trade-off between analyst's \textit{cognitive burden} and model ability to grasp deep inter-connections about reality.

	\end{frame}

}

%% THANKS!
\begin{frame}
	\frametitle{Greetings}
		\center Thank you for your attention!
		\center \includegraphics[scale = 0.5]{rexthor.png}
		\center [\url{https://github.com/emaballarin/northindia-covid-smds}]
\end{frame}

%-----------------------------------------------%
\end{document}


\documentclass[12pt]{cdblatex}
\usepackage{exercises}
\usepackage{fancyhdr}
\usepackage{footer}

\begin{document}

% --------------------------------------------------------------------------------------------
\section*{Exercise 6.6 The Kasner cosmology}

\begin{cadabra}
   {t, x, y, z}::Coordinate.
   {a,b,c,d,e,f,g,h#}::Indices(values={t, x, y, z}, position=independent).

   \partial{#}::PartialDerivative.

   p1::LaTeXForm("p_1").
   p2::LaTeXForm("p_2").
   p3::LaTeXForm("p_3").

   g^{a b}::InverseMetric.  # essential when using complete (gab, $g^{a b}$)

   Gamma := \Gamma^{a}_{b c} -> 1/2 g^{a d} (   \partial_{b}{g_{d c}}
                                              + \partial_{c}{g_{b d}}
                                              - \partial_{d}{g_{b c}}).

   Rabcd := R^{a}_{b c d} ->   \partial_{c}{\Gamma^{a}_{b d}}
                             - \partial_{d}{\Gamma^{a}_{b c}}
                             + \Gamma^{e}_{b d} \Gamma^{a}_{c e}
                             - \Gamma^{e}_{b c} \Gamma^{a}_{d e}.

   Rab := R_{a b} -> R^{c}_{a c b}.

   gab := { g_{t t} = -1,
            g_{x x} = t**(2*p1),
            g_{y y} = t**(2*p2),
            g_{z z} = t**(2*p3)}.                             # cdb(ex-0606.101,gab)

   complete   (gab, $g^{a b}$)                                # cdb(ex-0606.102,gab)

   substitute (Rabcd, Gamma)
   substitute (Rab, Rabcd)

   evaluate   (Gamma, gab, rhsonly=True)                      # cdb(ex-0606.103,Gamma)
   evaluate   (Rabcd, gab, rhsonly=True)                      # cdb(ex-0606.104,Rabcd)
   evaluate   (Rab,   gab, rhsonly=True)                      # cdb(ex-0606.105,Rab)
\end{cadabra}

\clearpage

\begin{dgroup*}
   \Dmath*{\Cdb*{ex-0606.101}}
   \Dmath*{\Cdb*{ex-0606.102}}
   \Dmath*{\Cdb*{ex-0606.103}}
   \Dmath*{\Cdb*{ex-0606.104}}
   \Dmath*{\Cdb*{ex-0606.105}}
\end{dgroup*}

\end{document}


\section{Data types}

\begin{frame}
  \frametitle{Introduction}

  So far, we have only considered two sorts of data types: $\enum{}$
  and $\estr{}$.
  %
  Most programming languages allow ``\emph{complex}'' types.

  \bigskip

  Examples?

\end{frame}

\note{This part is adapted from Chapters 10 and 11 of PFPL.}

\subsection{Binary product}

\begin{frame}
  \frametitle{Syntax}

  A language with binary \emph{product}.

  \[
  \begin{array}{llclll}
    \TYPES & \tau & \Coloneqq &  \ldots {\color{gray} (\text{as in } \lggeEF)}
    \\
    % &&& \tyunit  & \tyunit & \text{nullary product}
    % \\
           &&& \typrod{\tau_1}{\tau_2} & \tau_1 {\times} \tau_2 & \text{binary product}
    \\
    \\
    \EXPS & e & \Coloneqq &  \ldots {\color{gray} (\text{as in } \lggeEF)}
    \\
    % &&& \etriv & \ctriv & \text{null tuple}
    % \\
           &&& \epair{e_1}{e_2} & \cpair{e_1}{e_2}  & \text{ordered pair}
    \\
           &&& \eprl{e} & \cprl{e} & \text{left projection}
    \\
           &&& \eprr{e} & \cprr{e} & \text{right projection}
  \end{array}
  \]
\end{frame}

\begin{frame}
  \frametitle{Statics}
  % \[
  % \tyrule
  % {\etyrulename{u}}
  % {\,}
  % {\tyjudge{\Gamma}{\etriv}{\tyunit} }
  % \]
  \[
  \tyrule
  {\etyrulename{prod}}
  {\tyjudge{\Gamma}{e_1}{{\tau_1}}
    \and
    \tyjudge{\Gamma}{e_2}{{\tau_2}}
  }
  {\tyjudge{\Gamma}{\epair{e_1}{e_2}}{\typrod{\tau_1}{\tau_2}}}
  \]
  \pause
  \bigskip
  \[
  \tyrule
  {\etyrulename{pl}}
  {\tyjudge{\Gamma}{e}{\typrod{\tau_1}{\tau_2}}
  }
  {\tyjudge{\Gamma}{\eprl{e}}{\tau_1}}
  \qquad\qquad\quad
  \tyrule
  {\etyrulename{pr}}
  {\tyjudge{\Gamma}{e}{\typrod{\tau_1}{\tau_2}}
  }
  {\tyjudge{\Gamma}{\eprr{e}}{\tau_2}}
  \]
\end{frame}

\begin{frame}
  \frametitle{Examples}
  Find the type for the following terms, if possible.

  \begin{itemize}
  \item $\epair{\enum{2}}{\estr{\text{hello}}}$
  \item $\eprl{\epair{\enum{2}}{\estr{\text{hello}}}}$
  \item $\elam{\typrod{\enum{}}{\estr{}}}{x}{\epair{\eprr{x}}{\eprl{x}}}$
  \item $\eapp{\elam{\typrod{\enum{}}{\estr{}}}{x}{\epair{\eprr{x}}{\eprl{x}}}}{\etriv}$
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Dynamics (lazy)}

  \[
  % \semrule{}
  % {\,}
  % {\valjudge{\etriv}}
  % \qquad\qquad\quad
  \semrule{}
  {\,
    % \valjudge{e_1} \and \valjudge{e_2}
  }
  { \valjudge{\epair{e_1}{e_2}} }
  \]

  \[
  \semrule
  {\esemrulename{pll}}
  {\jtrans{e}{e'}}
  {\jtrans{\eprl{e}}{\eprl{e'}}}
  \qquad\qquad\quad
  \semrule
  {\esemrulename{plr}}
  {\jtrans{e}{e'}}
  {\jtrans{\eprr{e}}{\eprr{e'}}}
  \]

  \[
  \semrule
  {\esemrulename{pll}}
  {\,}
  {\jtrans{\eprl{\epair{e_1}{e_2}}}{{e_1}}}
 \qquad\qquad
  \semrule
  {\esemrulename{plr}}
  {\,}
  {\jtrans{\eprr{\epair{e_1}{e_2}}}{{e_2}}}
  \]
\end{frame}

\begin{frame}
  \frametitle{Examples}
  Evaluate the following terms, if they can be assigned a type.
  %
  \begin{itemize}
  \item $\epair{\enum{2}}{\estr{\text{hello}}}$
  \item $\eprl{\epair{\enum{2}}{\estr{\text{hello}}}}$
  \item $\elam{\typrod{\enum{}}{\estr{}}}{x}{\epair{\eprr{x}}{\eprl{x}}}$
  \item $\eapp{\elam{\typrod{\enum{}}{\estr{}}}{x}{\epair{\eprr{x}}{\eprl{x}}}}{\etriv}$
  \end{itemize}

\end{frame}

\subsection{Nullary and binary sum}

\begin{frame}
  \frametitle{Syntax}

  A language with nullary and binary \emph{sum}.

  \[
  \begin{array}{llclll}
    \TYPES & \tau & \Coloneqq &  \ldots {\color{gray} (\text{as in } \lggeEF)}
    % \\
    %        &&& \tyvoid  & \tyvoid & \text{nullary sum}
    \\
           &&& \tysum{\tau_1}{\tau_2} & \tau_1 {+} \tau_2 & \text{binary sum}
    \\
    \\
    \EXPS & e & \Coloneqq &  \ldots {\color{gray} (\text{as in } \lggeEF)}
    \\
    %        &&& \eabort{\tau}{e} & \cabort{e} & \text{abort}
    % \\
           &&& \einl{\tau_1}{\tau_2}{e} & \cinl{e}  & \text{left injection}
    \\
           &&&  \einr{\tau_1}{\tau_2}{e} & \cinr{e}  & \text{right injection}
    \\
           &&& \ecase{e}{x_1}{e_1}{x_2}{e_2} & \text{\tiny[see below]} & \text{case analysis}
  \end{array}
  \]

  Concrete form for case analysis: $\ccase{e}{x_1}{e_1}{x_2}{e_2}$
\end{frame}

\begin{frame}
  \frametitle{Statics}

  % \[
  % \tyrule
  % {\etyrulename{u}}
  % {\tyjudge{\Gamma}{e}{\tyvoid}}
  % {\tyjudge{\Gamma}{\eabort{\tau}{e}}{\tau}}
  % \]

  \[
  \tyrule
  {\etyrulename{inl}}
  {
    \tyjudge{\Gamma}{e_1}{{\tau_1}}
  }
  {\tyjudge{\Gamma}{\einl{\tau_1}{\tau_2}{e}}{\tysum{\tau_1}{\tau_2}}}
  \]

  \[
  \tyrule
  {\etyrulename{inr}}
  {
    \tyjudge{\Gamma}{e_2}{{\tau_2}}
  }
  {\tyjudge{\Gamma}{\einr{\tau_1}{\tau_2}{e}}{\tysum{\tau_1}{\tau_2}}}
  \]

  \[
  \tyrule
  {\etyrulename{cs}}
  {
    \tyjudge{\Gamma}{e}{\tysum{\tau_1}{\tau_2}}
    \and
    \tyjudge{\Gamma, \var{x_1}: {\tau_1}}{e_1}{\tau}
    \and
    \tyjudge{\Gamma, \var{x_2}: {\tau_2}}{e_2}{\tau}
  }
  {
    \tyjudge{\Gamma}
    {
      \ecase{e}{x_1}{e_1}{x_2}{e_2}
    }
    {\tau}
  }
  \]

  \bigskip

  Can we relax rule [\etyrulename{cs}] so that branches have
  different types?

\end{frame}

\begin{frame}
  \frametitle{Dynamics (eager)}

  \[
  \semrule{}
  {\valjudge{e}}
  {\valjudge{\einr{\tau_1}{\tau_2}{e}}}
  \qquad\qquad\quad
  \semrule{\etyrulename{ir}}
  {\jtrans{e}{e'}}
  {\jtrans{\einr{\tau_1}{\tau_2}{e}}{\einr{\tau_1}{\tau_2}{e'}}}
  \]

  \[
  \semrule{\etyrulename{inl}}
  {\valjudge{e}}
  {
    \jtrans{\ecase{\einl{\tau_1}{\tau_2}{e}}{x_1}{e_1}{x_2}{e_2}}
    {\subs{e_1}{e}{x_1}}
  }
  \]

  \[
  \semrule{\etyrulename{inr}}
  {\valjudge{e}}
  {
    \jtrans{\ecase{\einr{\tau_1}{\tau_2}{e}}{x_1}{e_1}{x_2}{e_2}}
    {\subs{e_2}{e}{x_2}}
  }
  \]

  \bigskip

  Anything missing?
\end{frame}

\subsection{Discussion}

\begin{frame}
  \frametitle{Constructors}
  We distinguish two sorts of type \emph{constructors}
  \begin{itemize}
  \item \emph{Introduction} forms construct a new term of the expected
    type, e.g., $\epair{e_1}{e_2}$ for binary products and
    $\einl{\tau_1}{\tau_2}{e_1}$ for binary sums.
    %
  \item \emph{Elimination} forms destruct a term of a give type into a
    ``smaller'' type, e.g., $\eprl{e}$ for binary sums and
    $\ecase{e}{x_1}{e_1}{x_2}{e_2}$ for binary sums.
  \end{itemize}

  \bigskip

  Any other example?

\end{frame}

\begin{frame}
  \frametitle{Exercises}
  Define the functions (in $\lggeEF$ with pairs and sums) that meet
  the following specification:
  \begin{itemize}
  \item A function that \emph{adds} the content of the pairs:
    $\text{addpair} \defi \clam{\typrod{\enum{}}{\enum{}}}{x}{\eplus{\cprl{x}}{\cprr{x}}}$. Observe that $\text{addpair}$ has type $\tyarr{\typrod{\enum{}}{\enum{}}}{\enum{}}$.
    %
  \item A function that \emph{flips} the content of a pair (e.g.,
    $\cpair{1}{2}$ becomes $\cpair{2}{1}$)
    %
  \item A function that takes \emph{either} a string or a number as a
    parameter and returns the length of the string if the argument is
    a string, or the square of the argument if it's a number.
    %
  \item A function of type
    $\tyarr{\tysum{\tau_1}{\tau_1}}{\tyarr{\tau_2}{\tyarr{\tau_2}{\tau_2}}}$,
    or, equivalently,
    $ (\tau_1 {+} \tau_1) \rightarrow \tau \rightarrow \tau
    \rightarrow \tau$
    (three parameters), which returns its second argument if its first
    is $\cinl{e}$ and returns its third argument if its first is
    $\cinr{e}$ (for some $e$).
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Applications and generalisations}
  \begin{itemize}
  \item Records
  \item Labelled variants
  \item Optional types with binary sums
  \item Booleans
  \item $n$-ary sums and products
  \end{itemize}
\end{frame}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "main"
%%% End:


\chapter{Landing Gear}

\section{Contact Point}

Landing gear contact point is considered to be an intersection of the ground plane and the line segment with the beginning at the strut attachment point and the end at the tire bottom.

\begin{figure}
  \centering
  \includegraphics[width=80mm]{eps/segment_plane_intersection.eps}
  \caption{Segment-plane intersection}
\end{figure}

Intersection of a line segment and a plane can be calculated using following expression: \cite{ORourke1998}
\begin{equation}
  \label{eq-lg-intersection}
  u =
  \frac
  { \vec n \cdot \left( {\vec r}_p - {\vec r}_b \right) }
  { \vec n \cdot \left( {\vec r}_e - {\vec r}_b \right) }
\end{equation}

Where:
\begin{description}[align=right,labelwidth=1cm]
  \item [$\vec n$]     --- unit vector normal to the plane
  \item [${\vec r}_b$] --- position vector of the line segment beginning
  \item [${\vec r}_e$] --- position vector of the line segment end
  \item [${\vec p}_b$] --- position vector of any point on the plane
  \item [$u$]          --- normalized coordinate of intersection point along line segment
\end{description}

If $0 \leq u \leq 1$ then intersection point is within line segment and its coordinates are given by the following formula:
\begin{equation}
  \vec r = {\vec r}_b = u \left( {\vec r}_e - {\vec r}_r \right)
\end{equation}

If denominator of expression (\ref{eq-lg-intersection}) is zero then the line segment is parallel to the plane. If both numerator and denominator are zero then the line segment lies on the plane.

\section{Forces and Moments}

Forces generated by the landing gear can be divided into:
\begin{itemize}
  \item[---] normal to the ground plane forces due to struts and tires deflection,
  \item[---] tangent to the ground plane forces due to friction between tires and the ground.
\end{itemize}

Normal forces are the sum of forces due to spring and damper while tangent force are caused by static or kinetic friction and optional rolling friction and are given as follows: \cite{ResnickHalliday2011}
\begin{align}
  F_N &= kx + c \dot x \\
  F_T &= \mu F_N
\end{align}

\begin{table}[h!]
  \begin{center}
    \begin{tabular}{ l | c | c }
      \toprule
      \textbf{Surface} & \textbf{Static friction coefficient} & \textbf{Kinetic friction coefficient} \\ \midrule
      Concrete (dry) &  0.8 - 1.0  &  0.7 - 0.8  \\
      Concrete (wet) &  0.6 - 0.8  &  0.5 - 0.6  \\
      Tarmac (dry)   &  0.7 - 0.8  &  0.6 - 0.7  \\
      Tarmac (wet)   &  0.4 - 0.5  &  0.3 - 0.4  \\
      Dirt (dry)     &  0.5 - 0.6  &  0.2 - 0.3  \\
      Dirt (wet)     &  0.3 - 0.4  &  0.2 - 0.3  \\
      Snow           &  0.1 - 0.4  &  0.2 - 0.3  \\
      Ice            & 0.05 - 0.15 & 0.05 - 0.10 \\
      \bottomrule
    \end{tabular}
    \caption{Static and kinetic friction coefficients \cite{Studzinski1980} }
  \end{center}
\end{table}

\begin{table}[h!]
  \begin{center}
    \begin{tabular}{ l | c }
      \toprule
      \textbf{Surface} & \textbf{Rolling friction coefficient} \\ \midrule
      Tarmac   &  0.010 - 0.012 \\
      Concrete &  0.012 - 0.015 \\
      Dirt     &  0.030 - 0.140 \\
      \bottomrule
    \end{tabular}
    \caption{Static and kinetic friction coefficients \cite{Studzinski1980} }
  \end{center}
\end{table}


\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
% maxwidth is the original width if it is less than linewidth
% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\let\hlipl\hlkwb

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}
\title{LAB 1}
\author{Richelle Tan Wei Qi - 0328371}
\date{}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}
\maketitle{}

\section*{Question 1}
Use R Studio as a calculator to compute the following values using Rnw file.
Add numbering in Word to identify each part of each problem.
\begin{enumerate}
\item 27(38-17)
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlnum{27}\hlopt{*}\hlstd{(}\hlnum{38}\hlopt{-}\hlnum{17}\hlstd{)}
\end{alltt}
\begin{verbatim}
## [1] 567
\end{verbatim}
\end{kframe}
\end{knitrout}
\item ln(14\textsuperscript{7})
\\
\\
In R, log function gives natural logarithm(ln), while log10 function gives the common logarithm in base 10.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{log}\hlstd{(}\hlnum{14}\hlopt{**}\hlnum{7}\hlstd{)}
\end{alltt}
\begin{verbatim}
## [1] 18.4734
\end{verbatim}
\end{kframe}
\end{knitrout}
\item sqrt(436/12)
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{sqrt}\hlstd{(}\hlnum{436}\hlopt{/}\hlnum{12}\hlstd{)}
\end{alltt}
\begin{verbatim}
## [1] 6.027714
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{enumerate}

\newpage
\section*{Question 2}
Create the following vectors in R.\\
\\a = (5, 10, 15, 20, ..., 160) \\
\\b = (87, 86, 85, ..., 56) \\
\\ Use vector arithmetic to multiply these vectors and call the result d. \\
\\ Select subsets of d to identify the following. \\

Vector a:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{a}\hlkwb{<-}\hlkwd{seq}\hlstd{(}\hlkwc{from}\hlstd{=}\hlnum{5} \hlstd{,}\hlkwc{to}\hlstd{=}\hlnum{160}\hlstd{,} \hlkwc{by}\hlstd{=}\hlnum{5}\hlstd{)}
\hlkwd{print}\hlstd{(a)}
\end{alltt}
\begin{verbatim}
##  [1]   5  10  15  20  25  30  35  40  45  50  55  60  65  70  75  80  85  90  95
## [20] 100 105 110 115 120 125 130 135 140 145 150 155 160
\end{verbatim}
\end{kframe}
\end{knitrout}

Vector b:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{b}\hlkwb{<-}\hlkwd{seq}\hlstd{(}\hlkwc{from}\hlstd{=}\hlnum{87}\hlstd{,}\hlkwc{to}\hlstd{=}\hlnum{56}\hlstd{,}\hlkwc{by}\hlstd{=}\hlopt{-}\hlnum{1}\hlstd{)}
\hlkwd{print}\hlstd{(b)}
\end{alltt}
\begin{verbatim}
##  [1] 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63
## [26] 62 61 60 59 58 57 56
\end{verbatim}
\end{kframe}
\end{knitrout}

Vector d:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{d}\hlkwb{<-}\hlstd{a}\hlopt{*}\hlstd{b}
\hlkwd{print}\hlstd{(d)}
\end{alltt}
\begin{verbatim}
##  [1]  435  860 1275 1680 2075 2460 2835 3200 3555 3900 4235 4560 4875 5180 5475
## [16] 5760 6035 6300 6555 6800 7035 7260 7475 7680 7875 8060 8235 8400 8555 8700
## [31] 8835 8960
\end{verbatim}
\end{kframe}
\end{knitrout}
\newpage
\begin{enumerate}
\item What are the 19\textsuperscript{th}, 20\textsuperscript{th}, and 21\textsuperscript{th} elements of d?
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{d[}\hlnum{19}\hlopt{:}\hlnum{21}\hlstd{]}
\end{alltt}
\begin{verbatim}
## [1] 6555 6800 7035
\end{verbatim}
\end{kframe}
\end{knitrout}
\item What are all of the elements of d which are less than 2000?
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{j}\hlkwb{<-}\hlstd{d}\hlopt{<}\hlnum{2000}
\end{alltt}
\end{kframe}
\end{knitrout}
\item How many elements of d are greater than 6000?
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{k}\hlkwb{<-}\hlstd{d}\hlopt{>}\hlnum{6000}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{enumerate}

\section*{Question 3}
Using d from problem 2, use R to compute the following statistics of d
\begin{enumerate}
\item sum
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{sum}\hlstd{(d)}
\end{alltt}
\begin{verbatim}
## [1] 175120
\end{verbatim}
\end{kframe}
\end{knitrout}

\item median
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{median}\hlstd{(d)}
\end{alltt}
\begin{verbatim}
## [1] 5897.5
\end{verbatim}
\end{kframe}
\end{knitrout}

\item standard deviation
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{sd}\hlstd{(d)}
\end{alltt}
\begin{verbatim}
## [1] 2608.563
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{enumerate}

\newpage
\section*{Question 4}
Use R to create the following two matrices and do the indicated matrix multiplication.

Setting first Matrix as matrixA:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{matrixA} \hlkwb{=} \hlkwd{matrix}\hlstd{(}\hlkwd{c}\hlstd{(}\hlnum{7}\hlstd{,} \hlnum{2}\hlstd{,} \hlnum{9}\hlstd{,} \hlnum{4}\hlstd{,} \hlnum{12}\hlstd{,} \hlnum{13}\hlstd{),} \hlkwc{nrow} \hlstd{=} \hlnum{2}\hlstd{,} \hlkwc{ncol} \hlstd{=} \hlnum{3}\hlstd{)}
\hlkwd{print}\hlstd{(matrixA)}
\end{alltt}
\begin{verbatim}
##      [,1] [,2] [,3]
## [1,]    7    9   12
## [2,]    2    4   13
\end{verbatim}
\end{kframe}
\end{knitrout}

Setting second Matrix as matrixB:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{matrixB} \hlkwb{=} \hlkwd{matrix}\hlstd{(}\hlkwd{c}\hlstd{(}\hlnum{1}\hlstd{,} \hlnum{2}\hlstd{,} \hlnum{3}\hlstd{,} \hlnum{7}\hlstd{,} \hlnum{8}\hlstd{,} \hlnum{9}\hlstd{,} \hlnum{12}\hlstd{,} \hlnum{13}\hlstd{,} \hlnum{14}\hlstd{,} \hlnum{19}\hlstd{,} \hlnum{20}\hlstd{,} \hlnum{21}\hlstd{),} \hlkwc{nrow} \hlstd{=} \hlnum{3}\hlstd{,} \hlkwc{ncol} \hlstd{=} \hlnum{4}\hlstd{)}
\hlkwd{print}\hlstd{(matrixB)}
\end{alltt}
\begin{verbatim}
##      [,1] [,2] [,3] [,4]
## [1,]    1    7   12   19
## [2,]    2    8   13   20
## [3,]    3    9   14   21
\end{verbatim}
\end{kframe}
\end{knitrout}

Resulting Matrix from Multiplication:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{print}\hlstd{(matrixA} \hlopt{%*%} \hlstd{matrixB)}
\end{alltt}
\begin{verbatim}
##      [,1] [,2] [,3] [,4]
## [1,]   61  229  369  565
## [2,]   49  163  258  391
\end{verbatim}
\end{kframe}
\end{knitrout}

\end{document}


% !TEX root = ../../main.tex
% !TEX spellcheck = en_GB

\section{Modelling}
\label{sec:PureSineModelling}

The Matlab code for pitch shifting an input sine of \SI{470}{\hertz} is shown in \cref{app:matlabmodel}. It is primarily based on the description from Matlab \cite{MatlabIF} and the open source implementation in the Octave Signal package \cite{OctaveHilbert}.

\Cref{code:matlabmodel} shows pseudo code describing the Matlab model.
The blockSize is set, and a Hamming window is generated.
The window can be used for each block and it is only necessary to generate it once.
The phase starts at zero, but is updated after each loop, allowing for the next block to start with the correct phase shift.
The loop loads the samples, applies the window, finds the frequency, finds the closest C-major frequency, generates a sine at the correct frequency and phase and updates the phase.

\begin{listing}
	\begin{minted}[linenos, breaklines, bgcolor=lightgray]{matlab}
	blockSize = 512;
	h = hammingWindow(blockSize);
	phase = 0;

	while(1)
	y = load(blockSize);
	yh = h .* y;
	freqIn = findIF(yh);
	newFreq = pianoFreq(freqIn);
	[out, phase] = genSine(blockSize, newFreq);
	\end{minted}
	\caption{Pseudo code for the Matlab model.}
	\label{code:matlabmodel}
\end{listing}

Finding the frequency is done in five steps, the output of some plotted in \cref{fig:IFexplained}:
\begin{enumerate}
	\item Hilbert Transform
	\begin{itemize}
		\item Shifts the input signal by \ang{90}.
	\end{itemize}
	\item Determine the phase, \cref{fig:IFsub1}.
	\item Unwrap the phase, \cref{fig:IFsub2}.
	\item Calculate the derivative in each point, \cref{fig:IFsub3}.
	\item Calculate the mean of the middle portion of the points (in our case the middle third).
\end{enumerate}

The IF and phase is used to create a new sine.
This is done for each new block as shown in \cref{fig:IFoutput}.
In the model each point is created using a call to \mintinline{matlab}{sin()}, this is due to the fact that the lowest frequency, \SI{261.626}{\hertz} will need only $(\frac{\SI{48000}{Hz}}{\SI{261.626}{\hertz}})=$\num{183} points without using interpolation, which does not seem excessive, and should be tried to make the Blackfin implementation simpler.

\begin{figure}
	\centering
	\begin{subfigure}[t]{.5\textwidth}
		\centering
		\includegraphics[width=.9\linewidth, clip, trim={2cm 7cm 2cm 7cm}]{gfx/Modelling/IFIP.pdf}
		\caption{Instantaneous Phase.}
		\label{fig:IFsub1}
	\end{subfigure}%
	\begin{subfigure}[t]{.5\textwidth}
		\centering
		\includegraphics[width=.9\linewidth, clip, trim={2cm 7cm 2cm 7cm}]{gfx/Modelling/IFunwrap.pdf}
		\caption{Wrapping Instantaneous Phase (blue), and unwrapped Instantaneous Phase (red).}
		\label{fig:IFsub2}
	\end{subfigure} \\
	\begin{subfigure}[t]{.5\textwidth}
		\centering
		\includegraphics[width=.9\linewidth, clip, trim={2cm 7cm 2cm 7cm}]{gfx/Modelling/IFIF.pdf}
		\caption{Instantaneous Frequency. Derivative of the unwrapped phase.}
		\label{fig:IFsub3}
	\end{subfigure}
	\caption{Different steps of determining the Instantaneous Frequency.}
	\label{fig:IFexplained}
\end{figure}

\begin{figure}
	\centering
	\begin{subfigure}[t]{.49\textwidth}
		\centering
		\includegraphics[width=.9\linewidth, clip, trim={2cm 7cm 2cm 7cm}]{gfx/Modelling/IF.pdf}
		\caption{Instantaneous Frequency of the input signal. Each color represents a new block.}
		\label{fig:IFout1}
	\end{subfigure} \hfill
	\begin{subfigure}[t]{.49\textwidth}
		\centering
		\includegraphics[width=.9\linewidth, clip, trim={2cm 7cm 2cm 7cm}]{gfx/Modelling/output.pdf}
		\caption{Output signal in the time domain. Each color represents a new block.}
		\label{fig:IFout2}
	\end{subfigure}
	\caption{Block processing.}
	\label{fig:IFoutput}
\end{figure}

\FloatBarrier


%2multibyte Version: 5.50.0.2953 CodePage: 1252
%% This document created by Scientific Word (R) Version 2.0
%\usepackage{sw20elba}
%\input tcilatex

\documentclass[thmsa,notitlepage,11pt]{article}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%TCIDATA{TCIstyle=Article/art1.lat,elba,article}

%TCIDATA{OutputFilter=LATEX.DLL}
%TCIDATA{Version=5.50.0.2953}
%TCIDATA{Codepage=1252}
%TCIDATA{<META NAME="SaveForMode" CONTENT="1">}
%TCIDATA{BibliographyScheme=Manual}
%TCIDATA{Created=Tue Nov 13 06:24:53 2001}
%TCIDATA{LastRevised=Tuesday, August 25, 2015 17:04:11}
%TCIDATA{<META NAME="GraphicsSave" CONTENT="32">}
%TCIDATA{Language=American English}

\input{tcilatex}
\begin{document}

\author{Richard Dennis\thanks{%
Email: richard.dennis@glasgow.ac.uk} \\
%EndAName
University of Glasgow}
\title{\textbf{Package Guide for SolveDSGE}\vspace{0.2in}}
\date{August 25, 2015}
\maketitle

\thispagestyle{empty}\newpage \setlength{\baselineskip}{18.95pt}%
\setcounter{page}{1}

\section{Overview}

SolveDSGE is a Julia package aimed at macroeconomics interested in solving
Dynamic Stochastic General Equilibrium (DSGE) models. \ The package provides
routines for solving rational expectations models and for solving optimal
policy problems. \ Using this package, DSGE models can be solved in logs or
levels to first- or second-order accuracy and optimal policy problems can be
solved under discretion, commitment, timeless-perspective commitment, and
quasi-commitment. \ Routines that solve robust-control versions of these
policy problems are in the works. \ Although there is much that this package
does not do, SolveDSGE offers a broad array of solution methods that can be
applied provided the DSGE model can be expressed in one of several standard
dynamic representations.

\section{Installation}

To install SolveDSGE you will need to type the following into the Julia REPL

\bigskip

\textbf{Pkg.add("SolveDSGE")}

\bigskip

\section{Solving rational expectations models}

\subsection{First-order-accurate solution methods}

SolveDSGE provides a range of solution methods for computing first-order
accurate solutions. \ Exploiting Julia's multiple dispatch all of these
solution methods are called via the single command \textbf{solve\_re}(). \
From this single command the particular solution method employed depends
principally on the model type that enters the \textbf{solve\_re()} function
call. \ Models are represented in various forms that are summarized by
types. \ The model types are

\begin{itemize}
\item \textbf{Blanchard\_Kahn\_Form}

\item \textbf{Klein\_Form}

\item \textbf{Sims\_Form}

\item \textbf{Binder\_Pesaran\_Form}
\end{itemize}

\subsubsection{Blanchard-Kahn form}

The Blanchard-Kahn Form is given by%
\[
\left[
\begin{array}{c}
\mathbf{x}_{t+1} \\
\text{E}_{t}\mathbf{y}_{t+1}%
\end{array}%
\right] =\mathbf{A}\left[
\begin{array}{c}
\mathbf{x}_{t} \\
\mathbf{y}_{t}%
\end{array}%
\right] +\mathbf{C}\left[ \mathbf{\epsilon }_{t+1}\right] ,
\]%
$\mathbf{\epsilon }_{t}\sim i.i.d.[\mathbf{0},\mathbf{\Sigma }]$, where $%
\mathbf{x}_{t}$ is an $n_{x}\times 1$ vector of predetermined variables and $%
\mathbf{y}_{t}$ is an $n_{y}\times 1$ vector of non-predetermined variables.
\ To solve models of this form we first create the \textbf{%
Blanchard\_Kahn\_Form} type for the model, then we use \textbf{solve\_re()}
to solve the model. \ The relevant lines of code would be something like

\bigskip

\textbf{cutoff = 1.0}

\textbf{model = Blanchard\_Kahn\_Form(nx,ny,a,c,sigma)}

\textbf{soln = solve\_re(model,cutoff)}

\bigskip

Here \textbf{soln} contains the solution, which is of the form%
\begin{eqnarray*}
\mathbf{x}_{t+1} &=&\mathbf{Px}_{t}+\mathbf{K\epsilon }_{t+1}, \\
\mathbf{y}_{t} &=&\mathbf{Fx}_{t},
\end{eqnarray*}%
and information about the number of eigenvalues with modulus greater than
cutoff and whether the \textquotedblleft solution\textquotedblright\
returned is determinate, indeterminate, or explosive.

Models of the \textbf{Blanchard\_Kahn\_Form} type can also be solved using
an iterative method to solve a non-symmetric, continuous, algebraic, Riccati
equation. \ In this case the relevant lines of code might look like

\bigskip

\textbf{tol = 1e-10}

\textbf{cutoff = 1.0}

\textbf{model = Blanchard\_Kahn\_Form(nx,ny,a,c,sigma)}

\textbf{soln = solve\_re(model,cutoff,tol)}

\bigskip

For this iterative method, the variable \textbf{cutoff} is still used to
establish determinacy, but is not used to order eigenvalues.

\subsubsection{Klein form}

The Klein Form for a model is given by%
\[
\mathbf{B}\left[
\begin{array}{c}
\mathbf{x}_{t+1} \\
\text{E}_{t}\mathbf{y}_{t+1}%
\end{array}%
\right] =\mathbf{A}\left[
\begin{array}{c}
\mathbf{x}_{t} \\
\mathbf{y}_{t}%
\end{array}%
\right] +\mathbf{C}\left[ \mathbf{\epsilon }_{t+1}\right] ,
\]%
$\mathbf{\epsilon }_{t}\sim i.i.d.[\mathbf{0},\mathbf{\Sigma }]$, where $%
\mathbf{x}_{t}$ is an $n_{x}\times 1$ vector of predetermined variables, $%
\mathbf{y}_{t}$ is an $n_{y}\times 1$ vector of non-predetermined variables,
and $\mathbf{B}$ need not have full rank. \ We can solve models of this form
using the code

\bigskip

\textbf{cutoff = 1.0}

\textbf{model = Klein\_Form(nx,ny,a,b,c,sigma)}

\textbf{soln = solve\_re(model,cutoff) }

\bigskip

The composite type \textbf{soln} contains the solution which is of the form%
\begin{eqnarray*}
\mathbf{x}_{t+1} &=&\mathbf{Px}_{t}+\mathbf{K\epsilon }_{t+1}, \\
\mathbf{y}_{t} &=&\mathbf{F}x_{t},
\end{eqnarray*}%
as well as information about the number of eigenvalues that have modulus
greater than cutoff and whether the \textquotedblleft
solution\textquotedblright\ returned is determinate, indeterminate, or
explosive.

\subsubsection{Sims form}

An alternative model form is used by Sims (2000) and is given by%
\[
\mathbf{\Gamma }_{0}\mathbf{z}_{t}=\mathbf{\Gamma }_{1}\mathbf{z}_{t-1}+%
\mathbf{C}+\mathbf{\Psi v}_{t}+\mathbf{\Pi \eta }_{t},
\]%
where $\mathbf{v}_{t}$ is a shock process, possibly serially correlated,
with mean-zero innovations whose variance-covarince matrix is given by $%
\mathbf{\Sigma }$. \ To solve models that are in this form we would do
something like the following

\bigskip

\textbf{cutoff = 1.0}

\textbf{model = Sims\_Form(gamma0,gamma1,c,psi,pi,sigma)}

\textbf{soln = solve\_re(model,cutoff)}

\bigskip

Here the solution, summarized by \textbf{soln}, is of the form%
\[
\mathbf{z}_{t}=\mathbf{G}_{1}\mathbf{z}_{t-1}+\mathbf{c}+\mathbf{impact}%
\times \mathbf{v}_{t}+\mathbf{ywt}\times \left[ \mathbf{I}-\mathbf{fmat}%
\times L^{-1}\right] ^{-1}\times \mathbf{fwt}\times \mathbf{v}_{t+1}.
\]

\subsubsection{Binder-Pesaran form}

A model is in \textquotedblleft structural form\textquotedblright\ if it is
written as%
\[
\mathbf{Az}_{t}=\mathbf{A}_{1}\mathbf{z}_{t-1}+\mathbf{B}\text{E}_{t}\mathbf{%
z}_{t+1}+\mathbf{C\epsilon }_{t},
\]%
where $\mathbf{\epsilon }_{t}\sim i.i.d.[\mathbf{0},\mathbf{\Sigma }]$. \ We
have two ways of solving structural form models. \ The first recasts them in
terms of the Klein form and here the relevant code would look something like

\bigskip

\textbf{cutoff = 1.0}

\textbf{model = Binder\_Pesaran\_Form(a,a1,b,c,sigma)}

\textbf{soln = solve\_re(model,cutoff)}

\bigskip

The second method is iterative, implementing Binder and Pesaran's
\textquotedblleft brute force\textquotedblright\ method; here the code would
be something like

\bigskip

\textbf{tol = 1e-10}

\textbf{cutoff = 1.0}

\textbf{model = Binder\_Pesaran\_Form(a,a1,b,c,sigma)}

\textbf{soln = solve\_re(model,cutoff,tol)}

\bigskip

Regardless of which of the two methods is used, the solution, summarized in
\textbf{soln}, has the form%
\[
\mathbf{z}_{t}=\mathbf{Pz}_{t-1}+\mathbf{K\epsilon }_{t}.
\]%
As earlier, \textbf{soln} is a composite type that in addition to the
solution itself also contains information about the number of eigenvalues
with modulus greater than cutoff and whether the solution returned is
determinate, indeterminate, or explosive.

\subsection{Second-order-accurate solution methods}

In addition to the first-order accurate solution methods documented above,
SolveDSGE also contains two methods form obtaining second-order-accurate
solutions to nonlinear DSGE models. \ As coded here, these two methods
employ the same model form but differ in how the solution is computed. \ To
employ either method the DSGE model is first expressed in the form%
\[
\text{E}_{t}\mathbf{G}\left( \mathbf{x}_{t},\mathbf{y}_{t},\mathbf{x}_{t+1},%
\mathbf{y}_{t+1}\right) =\mathbf{0}.
\]%
\ With $\mathbf{x}_{t}$ containing $n_{x}$ predetermined variables and $%
\mathbf{y}_{t}$ containing $n_{y}$ non-predetermined variables, $\mathbf{G}%
() $ is a vector containing $n_{x}+n_{y}$ functions. \ Bundling $x_{t}$ and $%
y_{t}$ into a new vector $\mathbf{z}_{t}=\left[
\begin{array}{cc}
\mathbf{x}_{t}^{^{\prime }} & \mathbf{y}_{t}^{^{\prime }}%
\end{array}%
\right] ^{^{\prime }}$ and bundling $\mathbf{z}_{t}$ and $\mathbf{z}_{t+1}$
into a new vector $\mathbf{p}_{t}=\left[
\begin{array}{cc}
\mathbf{z}_{t}^{^{\prime }} & \mathbf{z}_{t+1}^{^{\prime }}%
\end{array}%
\right] ^{^{\prime }}$ we get%
\[
\text{E}_{t}\mathbf{G}\left( \mathbf{p}_{t}\right) =\mathbf{0}.
\]%
We now approximate $\mathbf{G}\left( \mathbf{p}_{t}\right) $ around the
deterministic steady state, $\overline{\mathbf{p}}$, using a second-order
Taylor expansion giving%
\[
\mathbf{G}\left( \mathbf{p}_{t}\right) \simeq \mathbf{G}_{p}\left( \mathbf{p}%
_{t}-\overline{\mathbf{p}}\right) +\left[ \mathbf{I}\otimes \left( \mathbf{p}%
_{t}-\overline{\mathbf{p}}\right) \right] ^{^{\prime }}\mathbf{G}_{pp}\left[
\mathbf{I}\otimes \left( \mathbf{p}_{t}-\overline{\mathbf{p}}\right) \right]
=\mathbf{0},
\]%
where $\mathbf{G}_{p}$ is a matrix of first-derivatives and $\mathbf{G}_{pp}$
is a matrix of stacked Hessians, one Hessian for each of the $n_{x}+n_{y}$
equations.

We now recognize that some elements of $\mathbf{x}_{t}$ (usually the first $%
s $ elements) are shocks that have the form%
\[
\mathbf{s}_{t+1}=\mathbf{\Lambda s}_{t}+\mathbf{\eta \epsilon }_{t+1},
\]%
where $\mathbf{\epsilon }_{t}\sim i.i.d.[\mathbf{0},\mathbf{\Sigma }]$. \
The essential components required for a second-order-accurate solution are
now given by $n_{x}$, $n_{y}$, $\mathbf{G}_{p}$, $\mathbf{G}_{pp}$, $\mathbf{%
\eta }$, and $\mathbf{\Sigma }$.

The two model types that we consider for second-order-accurate solution
methods are

\begin{itemize}
\item \textbf{Gomme\_Klein\_Form}

\item \textbf{Lombardo\_Sutherland\_Form}
\end{itemize}

\subsubsection{Gomme-Klein form}

To compute a second-order accurate solution using the Gomme and Klein (2011)
method we summarize the model in the form of the \textbf{Gomme\_Klein\_Form}
composite type. \ Once this model type is constructed the model can be
solved. \ The code to compute the solution would be something like

\bigskip

\textbf{cutoff = 1.0}

\textbf{model = Gomme\_Klein\_Form(nx,ny,Gp,Gpp,eta,sigma)}

\textbf{soln = solve\_re(model,cutoff)}

\bigskip

Here \textbf{soln} is a composite type that contains the solution, which is
of the form%
\begin{eqnarray*}
\mathbf{x}_{t+1}-\overline{\mathbf{x}} &=&\frac{1}{2}\mathbf{ssh}+\mathbf{hx}%
\left( \mathbf{x}_{t}-\overline{\mathbf{x}}\right) +\frac{1}{2}\left[
\mathbf{I}\otimes \left( \mathbf{x}_{t}-\overline{\mathbf{x}}\right) \right]
^{^{\prime }}\mathbf{hxx}\left[ \mathbf{I}\otimes \left( \mathbf{x}_{t}-%
\overline{\mathbf{x}}\right) \right] +\mathbf{\eta \epsilon }_{t+1}, \\
\mathbf{y}_{t}-\overline{\mathbf{y}} &=&\frac{1}{2}\mathbf{ssg}+\mathbf{gx}%
\left( \mathbf{x}_{t}-\overline{\mathbf{x}}\right) +\frac{1}{2}\left[
\mathbf{I}\otimes \left( \mathbf{x}_{t}-\overline{\mathbf{x}}\right) \right]
^{^{\prime }}\mathbf{gxx}\left[ \mathbf{I}\otimes \left( \mathbf{x}_{t}-%
\overline{\mathbf{x}}\right) \right] ,
\end{eqnarray*}%
where $\mathbf{hxx}$ and $\mathbf{gxx}$ are stacked matrices containing the
second order coefficients for each of the $n_{x}$ and $n_{y}$ equations,
respectively. \ \textbf{soln} also contains information about the number of
eigenvalues with modulus greater than cutoff and the solution's determinacy
properties, where these properties are associated with the model first-order
dynamics.

\subsubsection{Lombardo-Sutherland form}

Implementing the Lombardo and Sutherland (2007) solution method is no
different than for Gomme and Klein (2010). \ The key difference is the form
in which the solution is presented. \ Ths code to implement the
Lombardo-Sutherland method would look something like

\bigskip

\textbf{cutoff = 1.0}

\textbf{model = Lombardo\_Sutherland\_Form(nx,ny,Gp,Gpp,eta,sigma)}

\textbf{soln = solve\_re(model,cutoff)}

\bigskip

where now the solution has the form%
\begin{eqnarray*}
\mathbf{x}_{t+1}-\overline{\mathbf{x}} &=&\frac{1}{2}\mathbf{ssh}+\mathbf{hx}%
\left( \mathbf{x}_{t}-\overline{\mathbf{x}}\right) +\frac{1}{2}\mathbf{hxx}%
\times \mathbf{v}_{t}+\mathbf{\eta \epsilon }_{t+1}, \\
\mathbf{y}_{t}-\overline{\mathbf{y}} &=&\frac{1}{2}\mathbf{ssg}+\mathbf{gx}%
\left( \mathbf{x}_{t}-\overline{\mathbf{x}}\right) +\frac{1}{2}\mathbf{gxx}%
\times \mathbf{v}_{t}, \\
\mathbf{v}_{t} &=&\mathbf{\Phi v}_{t-1}+\mathbf{\Gamma }vech\left( \mathbf{%
\epsilon }_{t}\mathbf{\epsilon }_{t}^{^{\prime }}\right) +\mathbf{\Psi }%
vec\left( \mathbf{x}_{t}\mathbf{\epsilon }_{t}^{^{\prime }}\right) ,
\end{eqnarray*}%
with $\mathbf{v}_{t}$ given by%
\[
\mathbf{v}_{t}=vech\left( \mathbf{x}_{t}\mathbf{x}_{t}^{\prime }\right) .
\]

The solution form produced by the Lombardo-Sutherland method can be
converted into that produced by the Gomme-Klein method by using the \textbf{%
convert\_second\_order} function as follows

\bigskip

\textbf{new\_soln = convert\_second\_order(soln)}

\bigskip

Where \textbf{soln} is of type \textbf{Lombardo\_Sutherland\_Soln}, \textbf{%
new\_soln} is of type \textbf{Gomme\_Klein\_Soln}.

\section{Solving optimal policy problems}

SolveDSGE provides routines for solving Linear-Quadratic (LQ) optimal policy
problems. \ These LQ problems allow policy to be conducted under:
discretion; commitment; quasi-commitment; and timeless-perspective
commitment. \ The solutions to these four policy problems are obtained using
the commands \textbf{solve\_disc()}, \textbf{solve\_commit()}, \textbf{%
solve\_quasi()}, and \textbf{solve\_timeless()}, respectively. \ The optimal
policy routines are based around four model types and two solution types. \
At this stage not all of these policies are supported for all model-types. \
The four model-types and the optimal policies that they support are
documented below.

\subsection{State space form}

The LQ optimal policy problem in \textbf{State\_Space\_Form} is described by
the quadratic objective function%
\[
Loss=\text{E}\left[ \dsum\limits_{t=0}^{\infty }\beta ^{t}\left( \mathbf{z}%
_{t}^{^{\prime }}\mathbf{Qz}_{t}+\mathbf{z}_{t}^{^{\prime }}\mathbf{Uu}_{t}+%
\mathbf{u}_{t}^{^{\prime }}\mathbf{U}^{^{\prime }}\mathbf{z}_{t}+\mathbf{u}%
_{t}^{^{\prime }}\mathbf{Ru}_{t}\right) \right] ,
\]%
and the linear constraints%
\[
\left[
\begin{array}{c}
\mathbf{x}_{t+1} \\
\text{E}_{t}\mathbf{y}_{t+1}%
\end{array}%
\right] =\mathbf{A}\left[
\begin{array}{c}
\mathbf{x}_{t} \\
\mathbf{y}_{t}%
\end{array}%
\right] +\mathbf{Bu}_{t}+\mathbf{C}\left[ \mathbf{\epsilon }_{t+1}\right] ,
\]%
where $\mathbf{x}_{t}$ contains $n_{x}$ predetermined variables, $\mathbf{y}%
_{t}$ contains $n_{y}$ non-predetermined variables, $\mathbf{u}_{t}$
contains $n_{p}$ policy instruments, and $\mathbf{\epsilon }_{t}$ contains $%
n_{s}$ stochastic innovations.

For this model form the following policies are supported:

\begin{itemize}
\item Discretion $\left( \mathbf{s}_{t}=\mathbf{x}_{t}\right) $

\item Commitment $\left( \mathbf{s}_{t}=\left[
\begin{array}{c}
\mathbf{x}_{t} \\
\mathbf{\lambda }_{t}%
\end{array}%
\right] \right) $

\item Quasi-commitment $\left( \mathbf{s}_{t}=\left[
\begin{array}{c}
\mathbf{x}_{t} \\
\mathbf{\lambda }_{t}%
\end{array}%
\right] \right) $

\item Timeless-perspective commitment $\left( \mathbf{s}_{t}=\left[
\begin{array}{c}
\mathbf{x}_{t} \\
\mathbf{x}_{t-1} \\
\mathbf{u}_{t-1}%
\end{array}%
\right] \right) $
\end{itemize}

For each policy the solution returned is of the form%
\begin{eqnarray*}
\mathbf{s}_{t+1} &=&\mathbf{Ps}_{t}+\mathbf{K\epsilon }_{t+1}, \\
\mathbf{z}_{t} &=&\mathbf{Hs}_{t}, \\
\mathbf{u}_{t} &=&\mathbf{Fs}_{t}.
\end{eqnarray*}%
with this information summarized in the solution type, \textbf{%
State\_Space\_Soln}.

To solve a model for each of the policies above we would use code like the
following

\bigskip

\textbf{obj = State\_Space\_Objective(beta,q,u,r)}

\textbf{model = State\_Space\_Form(nx,ny,a,b,c,sigma)}

\textbf{tol = 1e-10}

\textbf{maxiters = 100}

\textbf{commit\_prob = 0.75}

\textbf{soln\_disc = solve\_disc(model,obj,tol,maxiters)}

\textbf{soln\_commit = solve\_commit(model,obj,tol,maxiters)}

\textbf{soln\_quasi = solve\_quasi(model,obj,commit\_prob,tol,maxiters)}

\textbf{soln\_timeless = solve\_timeless(model,obj,tol,maxiters)}

\bigskip

\subsection{Generalized state space form}

The LQ optimal policy problem in \textbf{Generalized\_State\_Space\_Form} is
described by the quadratic objective function%
\[
Loss=\text{E}\left[ \dsum\limits_{t=0}^{\infty }\beta ^{t}\left( \mathbf{z}%
_{t}^{^{\prime }}\mathbf{Qz}_{t}+\mathbf{z}_{t}^{^{\prime }}\mathbf{Uu}_{t}+%
\mathbf{u}_{t}^{^{\prime }}\mathbf{U}^{^{\prime }}\mathbf{z}_{t}+\mathbf{u}%
_{t}^{^{\prime }}\mathbf{Ru}_{t}\right) \right] ,
\]%
and the linear constraints%
\[
\left[
\begin{array}{c}
\mathbf{x}_{t+1} \\
\mathbf{A}_{0}\text{E}_{t}\mathbf{y}_{t+1}%
\end{array}%
\right] =\mathbf{A}\left[
\begin{array}{c}
\mathbf{x}_{t} \\
\mathbf{y}_{t}%
\end{array}%
\right] +\mathbf{Bu}_{t}+\mathbf{C}\left[ \mathbf{\epsilon }_{t+1}\right] ,
\]%
where $\mathbf{x}_{t}$ contains $n_{x}$ predetermined variables, $\mathbf{y}%
_{t}$ contains $n_{y}$ non-predetermined variables, $\mathbf{u}_{t}$
contains $n_{p}$ policy instruments, and $\mathbf{\epsilon }_{t}$ contains $%
n_{s}$ stochastic innovations. \ The \textbf{Generalized\_State\_Space\_Form}
differs from the \textbf{State\_Space\_Form} above through the presence of
the (usually) singular leading matrix $\mathbf{A}_{0}$.

For this model form the following policies are supported:

\begin{itemize}
\item Discretion $\left( \mathbf{s}_{t}=\mathbf{x}_{t}\right) $

\item Commitment $\left( \mathbf{s}_{t}=\left[
\begin{array}{c}
\mathbf{x}_{t} \\
\mathbf{\lambda }_{t}%
\end{array}%
\right] \right) $

\item Quasi-commitment $\left( \mathbf{s}_{t}=\left[
\begin{array}{c}
\mathbf{x}_{t} \\
\mathbf{\lambda }_{t}%
\end{array}%
\right] \right) $

\item Timeless-perspective commitment $\left( \mathbf{s}_{t}=\left[
\begin{array}{c}
\mathbf{x}_{t} \\
\mathbf{x}_{t-1} \\
\mathbf{u}_{t-1}%
\end{array}%
\right] \right) $
\end{itemize}

For each policy the solution returned is of the form%
\begin{eqnarray*}
\mathbf{s}_{t+1} &=&\mathbf{Ps}_{t}+\mathbf{K\epsilon }_{t+1}, \\
\mathbf{z}_{t} &=&\mathbf{Hs}_{t}, \\
\mathbf{u}_{t} &=&\mathbf{Fs}_{t}.
\end{eqnarray*}%
with this information summarized in the solution type, \textbf{%
State\_Space\_Soln}.

To solve a model for each of the policies above we would use code like the
following

\bigskip

\textbf{obj = State\_Space\_Objective(beta,q,u,r)}

\textbf{model = Generalized\_State\_Space\_Form(nx,ny,a0,a,b,c,sigma)}

\textbf{tol = 1e-10}

\textbf{maxiters = 100}

\textbf{commit\_prob = 0.75}

\textbf{soln\_disc = solve\_disc(model,obj,tol,maxiters)}

\textbf{soln\_commit = solve\_commit(model,obj,tol,maxiters)}

\textbf{soln\_quasi = solve\_quasi(model,obj,commit\_prob,tol,maxiters)}

\textbf{soln\_timeless = solve\_timeless(model,obj,tol,maxiters)}

\bigskip

\subsection{Structural form}

The LQ optimal policy problem in \textbf{Structural\_Form} is described by
the quadratic objective function%
\[
Loss=\text{E}\left[ \dsum\limits_{t=0}^{\infty }\beta ^{t}\left( \mathbf{y}%
_{t}^{^{\prime }}\mathbf{Qy}_{t}+\mathbf{u}_{t}^{^{\prime }}\mathbf{Ru}%
_{t}\right) \right] ,
\]%
and the linear constraints%
\[
\mathbf{A}_{0}\mathbf{y}_{t}=\mathbf{A}_{1}\mathbf{y}_{t-1}+\mathbf{A}_{2}%
\text{E}_{t}\mathbf{y}_{t+1}+\mathbf{A}_{3}\mathbf{u}_{t}+\mathbf{A}_{5}%
\mathbf{\epsilon }_{t},
\]%
where $\mathbf{y}_{t}$ contains $n$ variables, $\mathbf{u}_{t}$ contains $%
n_{p}$ policy instruments, and $\mathbf{\epsilon }_{t}$ contains $n_{s}$
stochastic innovarions.

For this model form the following policies are supported:

\begin{itemize}
\item Discretion $\left( \mathbf{s}_{t-1}=\mathbf{y}_{t-1}\right) $

\item Commitment $\left( \mathbf{s}_{t-1}=\left[
\begin{array}{c}
\mathbf{y}_{t-1} \\
\mathbf{\lambda }_{t-1}%
\end{array}%
\right] \right) $
\end{itemize}

For each policy the returned solution is of the form%
\begin{eqnarray*}
\mathbf{s}_{t} &=&\mathbf{Ps}_{t-1}+\mathbf{K\epsilon }_{t}, \\
\mathbf{u}_{t} &=&\mathbf{Fs}_{t-1}.
\end{eqnarray*}%
with this information contained in the solution type, \textbf{%
Structural\_Soln}.

To solve a model for each of the policies above we would use code like the
following

\bigskip

\textbf{obj = Structural\_Objective(beta,q,r)}

\textbf{model = Structural\_Form(a0,a1,a2,a3,a5,sigma)}

\textbf{tol = 1e-10}

\textbf{maxiters = 100}

\textbf{soln\_disc = solve\_disc(model,obj,tol,maxiters)}

\textbf{soln\_commit = solve\_commit(model,obj,tol,maxiters)}

\bigskip

\subsection{Generalized structural form}

The LQ optimal policy problem in \textbf{Generalized\_Structural\_Form} is
described by the quadratic objective function%
\[
Loss=\text{E}\left[ \dsum\limits_{t=0}^{\infty }\beta ^{t}\left( \mathbf{y}%
_{t}^{^{\prime }}\mathbf{Qy}_{t}+\mathbf{u}_{t}^{^{\prime }}\mathbf{Ru}%
_{t}\right) \right] ,
\]%
and the linear constraints%
\[
\mathbf{A}_{0}\mathbf{y}_{t}=\mathbf{A}_{1}\mathbf{y}_{t-1}+\mathbf{A}_{2}%
\text{E}_{t}\mathbf{y}_{t+1}+\mathbf{A}_{3}\mathbf{u}_{t}+\mathbf{A}_{4}%
\text{E}_{t}\mathbf{u}_{t+1}+\mathbf{A}_{5}\mathbf{\epsilon }_{t},
\]%
where $\mathbf{y}_{t}$ contains $n$ variables, $\mathbf{u}_{t}$ contains $%
n_{p}$ policy instruments, and $\mathbf{\epsilon }_{t}$ contains $n_{s}$
stochastic innovarions.

For this model form the following policies are supported:

\begin{itemize}
\item Discretion $\left( \mathbf{s}_{t-1}=\mathbf{y}_{t-1}\right) $

\item Commitment $\left( \mathbf{s}_{t-1}=\left[
\begin{array}{c}
\mathbf{y}_{t-1} \\
\mathbf{\lambda }_{t-1}%
\end{array}%
\right] \right) $
\end{itemize}

For each policy the returned solution is of the form%
\begin{eqnarray*}
\mathbf{s}_{t} &=&\mathbf{Ps}_{t-1}+\mathbf{K\epsilon }_{t}, \\
\mathbf{u}_{t} &=&\mathbf{Fs}_{t-1}.
\end{eqnarray*}%
with this information contained in the solution type, \textbf{%
Structural\_Soln}.

To solve a model for each of the policies above we would use code like the
following

\bigskip

\textbf{obj = Structural\_Objective(beta,q,r)}

\textbf{model = Generalized\_Structural\_Form(a0,a1,a2,a3,a4,a5,sigma)}

\textbf{tol = 1e-10}

\textbf{maxiters = 100}

\textbf{soln\_disc = solve\_disc(model,obj,tol,maxiters)}

\textbf{soln\_commit = solve\_commit(model,obj,tol,maxiters)}

\bigskip

\setlength{\baselineskip}{10pt}

\begin{thebibliography}{9}
\bibitem{} Binder, M., and H. Pesaran, (1995), \textquotedblleft
Multivariate Rational Expectations Models and Macroeconomic Modeling: A
Review and Some New Results,\textquotedblright\ in: Pesaran, H., M. Wickens,
(Eds.), \textit{Handbook of Applied Econometrics}, Basil Blackwell, Oxford,
pp.139--187.

\bibitem{} Blanchard, O., and C. Kahn, (1980), \textquotedblleft The
Solution to Linear Difference Models under Rational
Expectations,\textquotedblright\ \textit{Econometrica}, 48, pp.1305--1311.

\bibitem{} Gomme, P., and P. Klein, (2011), \textquotedblleft Second-Order
Approximation of Dynamic Models Without the Use of
Tensors,\textquotedblright\ \textit{Journal of Economic Dynamics and Control}%
, 35, pp.604--615.

\bibitem{} Klein, P., (2000), \textquotedblleft Using the Generalized Schur
Form to Solve a Multivariate Linear Rational Expectations
Model,\textquotedblright\ \textit{Journal of Economic Dynamics and Control},
24, pp.1405--1423.

\bibitem{} Lombardo, G., and A. Sutherland, (2007), \textquotedblleft
Computing Second-Order-Accurate Solutions for Rational Expectations models
Using Linear Solution Methods,\textquotedblright\ \textit{Journal of
Economic Dynamics and Control}, 31, pp.515--530.

\bibitem{} Sims, C., (2001), \textquotedblleft Solving Linear Rational
Expectations Models,\textquotedblright\ \textit{Computational Economics},
20, pp.1--20.
\end{thebibliography}

\end{document}


\documentclass[a4paper,11pt]{article}%,twocolumn
\input{settings/packages}
\input{settings/page}
\input{settings/jupyter}
\usepackage{fontawesome}

\begin{document}
	\begin{center}
	{\large \textbf{Assignment A04: Neural Networks}}\\
	Thalagala B.P.\hspace{0.5cm} 180631J
\end{center}
\hrule

\hspace{10mm}

\textit{\textbf{Data set:} CIFAR10 50,000 32$\times$32$\times$3 training images and 10 classes. Accuracy greater than 0.1 shows learning.}
\section*{Part 1}

For the linear classifier in {\tt cell 2}, the score function is $f(x) = Wx +b$. But Keeping track of two sets of parameters \textbf{\textit{W}} and \textit{\textbf{b}} separately is not really efficient in computation. This cumbersomeness can be eliminated by combining both of them into one single matrix as coded in {\tt cell 1}. Additionally a column of ones must be added in front of training and testing images matrices({\tt x\_train,x\_test}) to enable matrix multiplication after this rearrangement.\\

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{1}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{std}\PY{o}{=}\PY{l+m+mf}{1e\PYZhy{}5}
\PY{n}{w1} \PY{o}{=} \PY{n}{std}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{random}\PY{o}{.}\PY{n}{randn}\PY{p}{(}\PY{n}{Din}\PY{p}{,} \PY{n}{K}\PY{p}{)} \PY{c+c1}{\PYZsh{} Initializing the weight matrix with random weights}
\PY{n}{b1} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{zeros}\PY{p}{(}\PY{n}{K}\PY{p}{)} \PY{c+c1}{\PYZsh{} Initializing the bias vector}
\PY{c+c1}{\PYZsh{} Rearranging train and test samples: (ra=rearranged)}
\PY{n}{x\PYZus{}train\PYZus{}ra} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{concatenate}\PY{p}{(}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{ones}\PY{p}{(}\PY{p}{(}\PY{n}{x\PYZus{}train}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{)}\PY{p}{)}\PY{p}{,}\PY{n}{x\PYZus{}train}\PY{p}{)}\PY{p}{,} \PY{n}{axis}\PY{o}{=}\PY{l+m+mi}{1}\PY{p}{)}
\PY{n}{x\PYZus{}test\PYZus{}ra}  \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{concatenate}\PY{p}{(}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{ones}\PY{p}{(}\PY{p}{(}\PY{n}{x\PYZus{}test}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{)}\PY{p}{)}\PY{p}{,}\PY{n}{x\PYZus{}test}\PY{p}{)}\PY{p}{,} \PY{n}{axis}\PY{o}{=}\PY{l+m+mi}{1}\PY{p}{)}
\PY{c+c1}{\PYZsh{} Rearranging weight matrix and bias matrix into single matrix}
\PY{n}{w1} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{concatenate}\PY{p}{(}\PY{p}{(}\PY{n}{b1}\PY{o}{.}\PY{n}{reshape}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,}\PY{n}{K}\PY{p}{)}\PY{p}{,} \PY{n}{w1}\PY{p}{)}\PY{p}{,} \PY{n}{axis}\PY{o}{=}\PY{l+m+mi}{0}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

After above described rearrangements of the matrices, gradient descent can be implemented as follows in {\tt cell 2}. Mean sum of squared errors function is used as the loss function and regularization is also used to eliminate unnecessary growth of parameters. And it was divided by two to make the rest of the coding easy.
{ \scriptsize\[
\therefore ~ Loss~ Function = \frac{1}{2m}.\sum\left[ \left( hypothesis - y_{train}\right)^2 + \lambda W^2 \right]~ where~\lambda = regularization~parameter
\]}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{2}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{m} \PY{o}{=} \PY{n}{x\PYZus{}train}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}  \PY{c+c1}{\PYZsh{} Number of training examples}
\PY{k}{for} \PY{n}{t} \PY{o+ow}{in} \PY{n+nb}{range}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,}\PY{n}{iterations}\PY{o}{+}\PY{l+m+mi}{1}\PY{p}{)}\PY{p}{:}
    \PY{c+c1}{\PYZsh{} Forward Propagation}
    \PY{n}{hypothesis} \PY{o}{=} \PY{n}{x\PYZus{}train\PYZus{}ra}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{w1}\PY{p}{)}
    \PY{n}{loss} \PY{o}{=} \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{p}{(}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{m}\PY{p}{)}\PY{p}{)}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{sum}\PY{p}{(}\PY{p}{(} \PY{n}{hypothesis} \PY{o}{\PYZhy{}} \PY{n}{y\PYZus{}train}\PY{p}{)}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)} \PY{o}{+} \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{p}{(}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{m}\PY{p}{)}\PY{p}{)}\PY{o}{*}\PY{n}{reg}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{sum}\PY{p}{(}\PY{n}{w1}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)}
    \PY{c+c1}{\PYZsh{} Backward Propagation}
    \PY{n}{dw1} \PY{o}{=} \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{n}{m}\PY{p}{)}\PY{o}{*}\PY{p}{(}\PY{n}{x\PYZus{}train\PYZus{}ra}\PY{o}{.}\PY{n}{T}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{hypothesis} \PY{o}{\PYZhy{}} \PY{n}{y\PYZus{}train}\PY{p}{)}\PY{p}{)}  \PY{o}{+} \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{n}{m}\PY{p}{)}\PY{o}{*}\PY{n}{reg}\PY{o}{*}\PY{n}{w1}
    \PY{n}{w1} \PY{o}{=} \PY{n}{w1} \PY{o}{\PYZhy{}} \PY{n}{lr}\PY{o}{*}\PY{n}{dw1}
    \PY{c+c1}{\PYZsh{} Decaying the learning rate}
    \PY{n}{lr} \PY{o}{=} \PY{n}{lr}\PY{o}{*}\PY{n}{lr\PYZus{}decay}
\end{Verbatim}
\end{tcolorbox}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.29]{figures/trainedWeightsp1}
	\caption{\footnotesize Weights matrix {\tt w1} as 10 images}
	\label{part1w}
\end{figure}

The learned weights at the end of learning for CIFAR-10 (after running the gradient descent 300 epochs), can be visualized as 10 figures in Fig. \ref{part1w}. Observe that, the weight matrix of the ``horse'' class slightly looks like a horse with two heads. This property is also visible in the ``automobile'' class too. Therefore theses templates will give maximum score for images of horses and automobiles respectively when the inner product is considered between the template and the image.\\

Hyperparameters of the  model, training and testing loss and accuracies corresponding to the first and last epochs are given below. Normalized losses and the accuracies are plot in the Fig. \ref{p1}.

\begin{verbatim}
	Initial Learning Rate = 1.4e-2, Learning Rate Decay = 0.999, Regularization Parameter = 5e-6
	Optimizer = Vanilla Gradient Descent(VGD), Epochs = 300

| Epoch 001 | Loss 0.5000 | accuracy: 0.0834 | val_loss: 0.4846 | val_accuracy: 0.2486 |
| Epoch 300 | Loss 0.3946 | accuracy: 0.4104 | val_loss: 0.3958 | val_accuracy: 0.3981 |
\end{verbatim}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.3]{figures/part1plots}
	\caption{\footnotesize Training Loss, Training Accuracy, Validation Loss and Validation Accuracy of the Linear Classifier with each iteration for 300 epochs}
	\label{p1}
\end{figure}

When observing the above figure following conclusions can be made. As the number of epochs increase, training and validation accuracies monotonically increase. But gap between the two curves also increases due to over-fitting of the model. Because parameters become more and more specific to the training set rather than become general to never seen images. The situation is the same for losses.\\

\hrule
\section*{Part 2}

As described in the part 1 weight matrices and bias vectors are concatenated into two matrices named {\tt w1} and {\tt w2}. The two layers are fully connected and  the {\tt sigmoid function} is used as the activation function for the hidden layer which consists of 200 hidden nodes. Layer 2, the output layer has no activation function.\\
\begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{3}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{H} \PY{o}{=} \PY{l+m+mi}{200} \PY{c+c1}{\PYZsh{} No of hidden nodes}
\PY{n}{std}\PY{o}{=}\PY{l+m+mf}{1e\PYZhy{}5}
\PY{c+c1}{\PYZsh{} Hidden Layer }
\PY{n}{w1} \PY{o}{=} \PY{n}{std}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{random}\PY{o}{.}\PY{n}{randn}\PY{p}{(}\PY{n}{Din}\PY{p}{,} \PY{n}{H}\PY{p}{)} \PY{c+c1}{\PYZsh{} Initializing the weight matrix with random weights}
\PY{n}{b1} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{zeros}\PY{p}{(}\PY{n}{H}\PY{p}{)} \PY{c+c1}{\PYZsh{} Initializing the bias vector}
\PY{c+c1}{\PYZsh{} Last Layer}
\PY{n}{w2} \PY{o}{=} \PY{n}{std}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{random}\PY{o}{.}\PY{n}{randn}\PY{p}{(}\PY{n}{H}\PY{p}{,} \PY{n}{K}\PY{p}{)} \PY{c+c1}{\PYZsh{} Initializing the weight matrix with random weights}
\PY{n}{b2} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{zeros}\PY{p}{(}\PY{n}{K}\PY{p}{)} \PY{c+c1}{\PYZsh{} Initializing the bias vector}
\PY{c+c1}{\PYZsh{} Rearranging train and test samples: (ra=rearranged)}
\PY{n}{x\PYZus{}train\PYZus{}ra} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{concatenate}\PY{p}{(}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{ones}\PY{p}{(}\PY{p}{(}\PY{n}{x\PYZus{}train}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{)}\PY{p}{)}\PY{p}{,}\PY{n}{x\PYZus{}train}\PY{p}{)}\PY{p}{,} \PY{n}{axis}\PY{o}{=}\PY{l+m+mi}{1}\PY{p}{)}
\PY{n}{x\PYZus{}test\PYZus{}ra}  \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{concatenate}\PY{p}{(}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{ones}\PY{p}{(}\PY{p}{(}\PY{n}{x\PYZus{}test}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{)}\PY{p}{)}\PY{p}{,}\PY{n}{x\PYZus{}test}\PY{p}{)}\PY{p}{,} \PY{n}{axis}\PY{o}{=}\PY{l+m+mi}{1}\PY{p}{)}
\PY{c+c1}{\PYZsh{} Rearranging weight matrices and bias vectors into single matrices}
\PY{n}{w1} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{concatenate}\PY{p}{(}\PY{p}{(}\PY{n}{b1}\PY{o}{.}\PY{n}{reshape}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,}\PY{n}{H}\PY{p}{)}\PY{p}{,} \PY{n}{w1}\PY{p}{)}\PY{p}{,} \PY{n}{axis}\PY{o}{=}\PY{l+m+mi}{0}\PY{p}{)}
\PY{n}{w2} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{concatenate}\PY{p}{(}\PY{p}{(}\PY{n}{b2}\PY{o}{.}\PY{n}{reshape}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,}\PY{n}{K}\PY{p}{)}\PY{p}{,} \PY{n}{w2}\PY{p}{)}\PY{p}{,} \PY{n}{axis}\PY{o}{=}\PY{l+m+mi}{0}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

\textbf{\textit{Normalization of image pixel values was removed from the data pre-processing stage}} as it was observed that model stops learning after few iterations. Because extremely small weights in the matrices, keeps the weight matrices almost the same and the change in loss function becomes negligible.

The loss function used in the part 1 is used here as well, with the additional term related to the {\tt w2} matrix.
{ \scriptsize\[
	\therefore ~ Loss~ Function = \frac{1}{2m}.\sum\left[ \left( hypothesis - y_{train}\right)^2 + \lambda W_1^2 + \lambda W_2^2 \right]~ where~\lambda = regularization~parameter
	\]}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{4}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{k}{for} \PY{n}{t} \PY{o+ow}{in} \PY{n+nb}{range}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,}\PY{n}{iterations}\PY{o}{+}\PY{l+m+mi}{1}\PY{p}{)}\PY{p}{:}
    \PY{c+c1}{\PYZsh{} Forward Propagation}
    \PY{n}{hypo} \PY{o}{=} \PY{n}{sigmoid}\PY{p}{(}\PY{n}{x\PYZus{}train\PYZus{}ra}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{w1}\PY{p}{)}\PY{p}{)} \PY{c+c1}{\PYZsh{} Layer 1 with sigmoid activation}
    \PY{n}{hypothesis} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{concatenate}\PY{p}{(}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{ones}\PY{p}{(}\PY{p}{(}\PY{n}{hypo}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{)}\PY{p}{)}\PY{p}{,}\PY{n}{hypo}\PY{p}{)}\PY{p}{,} \PY{n}{axis}\PY{o}{=}\PY{l+m+mi}{1}\PY{p}{)} \PY{c+c1}{\PYZsh{} Rearranging for layer 2}
    \PY{n}{predict} \PY{o}{=} \PY{n}{hypothesis}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{w2}\PY{p}{)} \PY{c+c1}{\PYZsh{} Layer 2     }
    \PY{n}{loss} \PY{o}{=} \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{p}{(}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{m}\PY{p}{)}\PY{p}{)}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{sum}\PY{p}{(}\PY{p}{(} \PY{n}{predict} \PY{o}{\PYZhy{}} \PY{n}{y\PYZus{}train}\PY{p}{)}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)}\PYZbs{}
         \PY{o}{+} \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{p}{(}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{m}\PY{p}{)}\PY{p}{)}\PY{o}{*}\PY{n}{reg}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{sum}\PY{p}{(}\PY{n}{w1}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)} \PY{o}{+} \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{p}{(}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{m}\PY{p}{)}\PY{p}{)}\PY{o}{*}\PY{n}{reg}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{sum}\PY{p}{(}\PY{n}{w2}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)}
    \PY{c+c1}{\PYZsh{} Back Propagation: partial dertivatives of Loss function}
    \PY{n}{dpredict} \PY{o}{=}  \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{n}{m}\PY{p}{)}\PY{o}{*}\PY{p}{(}\PY{n}{predict} \PY{o}{\PYZhy{}} \PY{n}{y\PYZus{}train}\PY{p}{)}
    \PY{n}{dw2} \PY{o}{=} \PY{n}{hypothesis}\PY{o}{.}\PY{n}{T}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{dpredict}\PY{p}{)} \PY{o}{+} \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{n}{m}\PY{p}{)}\PY{o}{*}\PY{n}{reg}\PY{o}{*}\PY{n}{w2}
    \PY{n}{dh} \PY{o}{=} \PY{n}{dpredict}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{w2}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{:}\PY{p}{,}\PY{p}{]}\PY{o}{.}\PY{n}{T}\PY{p}{)} \PY{c+c1}{\PYZsh{} Removing bias vector w2(201 X 10)\PYZhy{}\PYZhy{}\PYZgt{} 200 X 10}
    \PY{n}{dhdxw1} \PY{o}{=} \PY{n}{hypo}\PY{o}{*}\PY{p}{(}\PY{l+m+mi}{1} \PY{o}{\PYZhy{}} \PY{n}{hypo}\PY{p}{)} \PY{c+c1}{\PYZsh{}using the hypothesis(50000X200), the one before rearranging.}
    \PY{n}{dw1} \PY{o}{=} \PY{n}{x\PYZus{}train\PYZus{}ra}\PY{o}{.}\PY{n}{T}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{dh}\PY{o}{*}\PY{n}{dhdxw1}\PY{p}{)} \PY{o}{+} \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{n}{m}\PY{p}{)}\PY{o}{*}\PY{n}{reg}\PY{o}{*}\PY{n}{w1}
    \PY{c+c1}{\PYZsh{} Gradient Descent}
    \PY{n}{w1} \PY{o}{=} \PY{n}{w1} \PY{o}{\PYZhy{}} \PY{n}{lr}\PY{o}{*}\PY{n}{dw1}
    \PY{n}{w2} \PY{o}{=} \PY{n}{w2} \PY{o}{\PYZhy{}} \PY{n}{lr}\PY{o}{*}\PY{n}{dw2}
     \PY{c+c1}{\PYZsh{} Decaying learning rate}
    \PY{n}{lr} \PY{o}{=} \PY{n}{lr}\PY{o}{*}\PY{n}{lr\PYZus{}decay}
\end{Verbatim}
\end{tcolorbox}

\begin{verbatim}
	Initial Learning Rate = 1.4e-2, Learning Rate Decay = 0.999, Regularization Parameter = 5e-6
	Optimizer = Vanilla Gradient Descent(VGD), Epochs = 300

	| Epoch 001 | Loss 0.5000 | accuracy: 0.1000 | val_loss: 0.4541 | val_accuracy: 0.1339 |
	| Epoch 300 | Loss 0.3765 | accuracy: 0.4436 | val_loss: 0.3813 | val_accuracy: 0.4275 |
\end{verbatim}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.3]{figures/part2plots}
	\caption{\footnotesize Training Loss, Training Accuracy, Validation Loss and Validation Accuracy of the 2 Layer Classifier with each iteration:  for 300 epochs}
\end{figure}

As described in the part 1 the same reason applies for the behavior of the gaps between normalized losses and accuracies curves. In addition to that due to the non linearity introduced by the {\tt sigmoid function} at the hidden nodes, curves are not smooth as they were in the part 1 linear classifier.\\

\hrule
\section*{Part 3}

For the purpose of comparison hyperparameters were kept the same as in part 2. But the optimizer changes form Vanilla Gradient Descent to Mini-batch Gradient Descent(mentioned as stochastic GD-SGD).

\begin{verbatim}
	Initial Learning Rate = 1.4e-2, Learning Rate Decay = 0.999, Regularization Parameter = 5e-6
	Optimizer = Mini Batch Gradient Descent, Batch Size = 500, Epochs = 300

| Epoch 001 | Loss 0.4768 | accuracy: 0.1249 | val_loss: 0.4619 | val_accuracy: 0.1252 |
| Epoch 300 | Loss 0.3686 | accuracy: 0.4674 | val_loss: 0.3768 | val_accuracy: 0.4432 |
\end{verbatim}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.3]{figures/part3plots}
	\caption{\footnotesize Training Loss, Training Accuracy, Validation Loss and Validation Accuracy of the 2 Layer Classifier with each iteration for 300 epochs: Using Mini Batch Gradient Descent as the optimizer}
\end{figure}

consider the accuracies and losses related to the last epochs of the Part 2 and Part 3.
\begin{verbatim}
	| VGD | Epoch 300 | Loss 0.3765 | accuracy: 0.4436 | val_loss: 0.3813 | val_accuracy: 0.4275 |
	| SGD | Epoch 300 | Loss 0.3686 | accuracy: 0.4674 | val_loss: 0.3768 | val_accuracy: 0.4432 |
\end{verbatim}
In Part 2  \textbf{the same batch}(50,000) training samples were used \textbf{\textit{for every step of Gradient Descent}} and therefore the parameters become more specific to the training set as there is no any randomness in the data processed in each iteration. But with the Mini batch GD optimizer it is not the case. At each epoch the training set(50,000) is \textbf{\textit{shuffled}} and  \textbf{\textit{divided into}} number($n = \frac{Number~ of~ Training~ samples}{Batch~size}$) of \textit{\textbf{mini batches}}. After that gradient descent is carried out on each such mini batch and weights are updated accordingly. The advantage of this process is, since the batch is shuffled and only a small set of the batch is seen by the model at one gradient descent step, it performs well on the never seen test samples better than the algorithm in part 2.

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{5}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{batch\PYZus{}size} \PY{o}{=} \PY{l+m+mi}{500} \PY{c+c1}{\PYZsh{} define the batch size}
\PY{n}{seed} \PY{o}{=} \PY{l+m+mi}{0}\PY{p}{;} \PY{n}{rng} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{random}\PY{o}{.}\PY{n}{default\PYZus{}rng}\PY{p}{(}\PY{n}{seed}\PY{o}{=}\PY{n}{seed}\PY{p}{)}
\PY{k}{for} \PY{n}{t} \PY{o+ow}{in} \PY{n+nb}{range}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,}\PY{n}{iterations}\PY{o}{+}\PY{l+m+mi}{1}\PY{p}{)}\PY{p}{:}
    \PY{n}{indices} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{arange}\PY{p}{(}\PY{n}{Ntr}\PY{p}{)} \PY{c+c1}{\PYZsh{}Number of training samples}
    \PY{n}{rng}\PY{o}{.}\PY{n}{shuffle}\PY{p}{(}\PY{n}{indices}\PY{p}{)}
    \PY{n}{x\PYZus{}train\PYZus{}3} \PY{o}{=} \PY{n}{x\PYZus{}train\PYZus{}ra}\PY{p}{[}\PY{n}{indices}\PY{p}{]}
    \PY{n}{y\PYZus{}train\PYZus{}3} \PY{o}{=} \PY{n}{y\PYZus{}train}\PY{p}{[}\PY{n}{indices}\PY{p}{]}
    \PY{n}{batch\PYZus{}loss} \PY{o}{=} \PY{l+m+mi}{0} \PY{c+c1}{\PYZsh{} Loss for each batch}
    \PY{k}{for} \PY{n}{start} \PY{o+ow}{in} \PY{n+nb}{range}\PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,}\PY{n}{Ntr}\PY{p}{,}\PY{n}{batch\PYZus{}size}\PY{p}{)}\PY{p}{:}
        \PY{n}{stop} \PY{o}{=} \PY{n}{start} \PY{o}{+} \PY{n}{batch\PYZus{}size}
        \PY{c+c1}{\PYZsh{} Forward Propagation}
        \PY{n}{hypo} \PY{o}{=} \PY{n}{sigmoid}\PY{p}{(}\PY{n}{x\PYZus{}train\PYZus{}3}\PY{p}{[}\PY{n}{start}\PY{p}{:}\PY{n}{stop}\PY{p}{]}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{w1}\PY{p}{)}\PY{p}{)} \PY{c+c1}{\PYZsh{} Layer 1 with sigmoid activation}
        \PY{n}{hypothesis} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{concatenate}\PY{p}{(}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{ones}\PY{p}{(}\PY{p}{(}\PY{n}{hypo}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{)}\PY{p}{)}\PY{p}{,}\PY{n}{hypo}\PY{p}{)}\PY{p}{,} \PY{n}{axis}\PY{o}{=}\PY{l+m+mi}{1}\PY{p}{)}
        \PY{n}{predict} \PY{o}{=} \PY{n}{hypothesis}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{w2}\PY{p}{)} \PY{c+c1}{\PYZsh{} Layer 2 }
        \PY{n}{minibatch\PYZus{}loss} \PY{o}{=} \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{p}{(}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{m}\PY{p}{)}\PY{p}{)}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{sum}\PY{p}{(}\PY{p}{(} \PY{n}{predict} \PY{o}{\PYZhy{}} \PY{n}{y\PYZus{}train\PYZus{}3}\PY{p}{[}\PY{n}{start}\PY{p}{:}\PY{n}{stop}\PY{p}{]}\PY{p}{)}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)}\PYZbs{}
             \PY{o}{+} \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{p}{(}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{m}\PY{p}{)}\PY{p}{)}\PY{o}{*}\PY{n}{reg}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{sum}\PY{p}{(}\PY{n}{w1}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)} \PY{o}{+} \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{p}{(}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{m}\PY{p}{)}\PY{p}{)}\PY{o}{*}\PY{n}{reg}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{sum}\PY{p}{(}\PY{n}{w2}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)}
        \PY{n}{batch\PYZus{}loss}\PY{o}{+}\PY{o}{=} \PY{n}{minibatch\PYZus{}loss}
        \PY{c+c1}{\PYZsh{} Back Propagation partial dertivatives of Loss function}
        \PY{n}{dpredict} \PY{o}{=}  \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{n}{m}\PY{p}{)}\PY{o}{*}\PY{p}{(}\PY{n}{predict} \PY{o}{\PYZhy{}} \PY{n}{y\PYZus{}train\PYZus{}3}\PY{p}{[}\PY{n}{start}\PY{p}{:}\PY{n}{stop}\PY{p}{]}\PY{p}{)}
        \PY{n}{dw2} \PY{o}{=} \PY{n}{hypothesis}\PY{o}{.}\PY{n}{T}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{dpredict}\PY{p}{)} \PY{o}{+} \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{n}{m}\PY{p}{)}\PY{o}{*}\PY{n}{reg}\PY{o}{*}\PY{n}{w2}
        \PY{n}{dh} \PY{o}{=} \PY{n}{dpredict}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{w2}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{:}\PY{p}{,}\PY{p}{]}\PY{o}{.}\PY{n}{T}\PY{p}{)} \PY{c+c1}{\PYZsh{} Removing bias vector w2(201x10)\PYZhy{}\PYZhy{}\PYZgt{} 200x10}
        \PY{n}{dhdxw1} \PY{o}{=} \PY{n}{hypo}\PY{o}{*}\PY{p}{(}\PY{l+m+mi}{1} \PY{o}{\PYZhy{}} \PY{n}{hypo}\PY{p}{)} \PY{c+c1}{\PYZsh{}using hypothesis 50000*200, the one before rearranging.}
        \PY{n}{dw1} \PY{o}{=} \PY{n}{x\PYZus{}train\PYZus{}3}\PY{p}{[}\PY{n}{start}\PY{p}{:}\PY{n}{stop}\PY{p}{]}\PY{o}{.}\PY{n}{T}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{dh}\PY{o}{*}\PY{n}{dhdxw1}\PY{p}{)} \PY{o}{+} \PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{n}{m}\PY{p}{)}\PY{o}{*}\PY{n}{reg}\PY{o}{*}\PY{n}{w1}
        \PY{n}{w1} \PY{o}{=} \PY{n}{w1} \PY{o}{\PYZhy{}} \PY{n}{lr}\PY{o}{*}\PY{n}{dw1}  \PY{c+c1}{\PYZsh{} Gradient Descent}
        \PY{n}{w2} \PY{o}{=} \PY{n}{w2} \PY{o}{\PYZhy{}} \PY{n}{lr}\PY{o}{*}\PY{n}{dw2}  \PY{c+c1}{\PYZsh{} Gradient Descent}
    \PY{n}{lr} \PY{o}{=} \PY{n}{lr}\PY{o}{*}\PY{n}{lr\PYZus{}decay}      \PY{c+c1}{\PYZsh{} Decaying the learning rate}
\end{Verbatim}
\end{tcolorbox}

\section*{Part 4}

The CNN model declared below starts over-fitting around the $8^{th}$ epoch. Therefore the number of epochs was limited to 10. This fact is clearly visible, because at the $10^{th}$ epoch even though the training loss and accuracy are better than that of the $8^{th}$ epoch, validation loss and validation accuracy are worse. However, the model has performed absolutely better than any of the previous models in 8 epochs with a validation accuracy of $69.79\%$. Previously we were only able to reach up to $44.32\%$ of validation accuracy with the defined hyperparameters.

\begin{verbatim}
	Initial Learning Rate = 1.4e-2,  Learnable Parameters: 73,418
	Optimizer = SGD with Momentum = 0.9, Batch Size = 50, Epochs = 10

	| Epoch 001 | Loss: 1.8674 | accuracy: 0.3070 | val_loss: 1.2984 | val_accuracy: 0.5298 |
	| Epoch 008 | Loss: 0.6375 | accuracy: 0.7746 | val_loss: 0.8601 | val_accuracy: 0.7088 |
	| Epoch 010 | Loss: 0.5651 | accuracy: 0.8021 | val_loss: 0.9394 | val_accuracy: 0.6979 |
\end{verbatim}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.3]{figures/part4plots}
	\caption{\footnotesize Training Loss, Training Accuracy, Validation Loss and Validation Accuracy of the CNN model}
\end{figure}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{6}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{p}{(}\PY{n}{x\PYZus{}train}\PY{p}{,} \PY{n}{y\PYZus{}train}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{n}{x\PYZus{}test}\PY{p}{,} \PY{n}{y\PYZus{}test}\PY{p}{)} \PY{o}{=} \PY{n}{datasets}\PY{o}{.}\PY{n}{cifar10}\PY{o}{.}\PY{n}{load\PYZus{}data}\PY{p}{(}\PY{p}{)}
\PY{n}{K} \PY{o}{=} \PY{n+nb}{len}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{unique}\PY{p}{(}\PY{n}{y\PYZus{}train}\PY{p}{)}\PY{p}{)} \PY{c+c1}{\PYZsh{} Number of Classes}
\PY{c+c1}{\PYZsh{} Normalize pixel values: Image data preprocessing}
\PY{n}{x\PYZus{}train}\PY{p}{,} \PY{n}{x\PYZus{}test} \PY{o}{=} \PY{n}{x\PYZus{}train} \PY{o}{/} \PY{l+m+mf}{255.0}\PY{p}{,} \PY{n}{x\PYZus{}test} \PY{o}{/} \PY{l+m+mf}{255.0}
\PY{n}{mean\PYZus{}image} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{mean}\PY{p}{(}\PY{n}{x\PYZus{}train}\PY{p}{,} \PY{n}{axis}\PY{o}{=}\PY{l+m+mi}{0}\PY{p}{)} \PY{c+c1}{\PYZsh{} axis=0: mean of a column; Mean of each pixel}
\PY{n}{x\PYZus{}train} \PY{o}{=} \PY{n}{x\PYZus{}train} \PY{o}{\PYZhy{}} \PY{n}{mean\PYZus{}image}
\PY{n}{x\PYZus{}test} \PY{o}{=} \PY{n}{x\PYZus{}test} \PY{o}{\PYZhy{}} \PY{n}{mean\PYZus{}image}
\PY{c+c1}{\PYZsh{} Convert class vectors to binary class matrices.}
\PY{n}{y\PYZus{}train} \PY{o}{=} \PY{n}{tf}\PY{o}{.}\PY{n}{keras}\PY{o}{.}\PY{n}{utils}\PY{o}{.}\PY{n}{to\PYZus{}categorical}\PY{p}{(}\PY{n}{y\PYZus{}train}\PY{p}{,} \PY{n}{num\PYZus{}classes}\PY{o}{=}\PY{n}{K}\PY{p}{)}
\PY{n}{y\PYZus{}test} \PY{o}{=} \PY{n}{tf}\PY{o}{.}\PY{n}{keras}\PY{o}{.}\PY{n}{utils}\PY{o}{.}\PY{n}{to\PYZus{}categorical}\PY{p}{(}\PY{n}{y\PYZus{}test}\PY{p}{,} \PY{n}{num\PYZus{}classes}\PY{o}{=}\PY{n}{K}\PY{p}{)}
\PY{n}{model} \PY{o}{=} \PY{n}{models}\PY{o}{.}\PY{n}{Sequential}\PY{p}{(}\PY{p}{)} \PY{c+c1}{\PYZsh{} Declaring the CNN}
\PY{n}{model}\PY{o}{.}\PY{n}{add}\PY{p}{(}\PY{n}{layers}\PY{o}{.}\PY{n}{Conv2D}\PY{p}{(}\PY{l+m+mi}{32}\PY{p}{,} \PY{p}{(}\PY{l+m+mi}{3}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{)}\PY{p}{,} \PY{n}{activation}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{relu}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{input\PYZus{}shape}\PY{o}{=}\PY{p}{(}\PY{l+m+mi}{32}\PY{p}{,} \PY{l+m+mi}{32}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{)}\PY{p}{,} \PY{n}{name}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{C32}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{)}
\PY{n}{model}\PY{o}{.}\PY{n}{add}\PY{p}{(}\PY{n}{layers}\PY{o}{.}\PY{n}{MaxPooling2D}\PY{p}{(}\PY{p}{(}\PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)}\PY{p}{)}\PY{p}{)}
\PY{n}{model}\PY{o}{.}\PY{n}{add}\PY{p}{(}\PY{n}{layers}\PY{o}{.}\PY{n}{Conv2D}\PY{p}{(}\PY{l+m+mi}{64}\PY{p}{,} \PY{p}{(}\PY{l+m+mi}{3}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{)}\PY{p}{,} \PY{n}{activation}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{relu}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{name}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{C64\PYZus{}1}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{)} \PY{c+c1}{\PYZsh{} 64, 3x3 convolutions}
\PY{n}{model}\PY{o}{.}\PY{n}{add}\PY{p}{(}\PY{n}{layers}\PY{o}{.}\PY{n}{MaxPooling2D}\PY{p}{(}\PY{p}{(}\PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)}\PY{p}{)}\PY{p}{)}
\PY{n}{model}\PY{o}{.}\PY{n}{add}\PY{p}{(}\PY{n}{layers}\PY{o}{.}\PY{n}{Conv2D}\PY{p}{(}\PY{l+m+mi}{64}\PY{p}{,} \PY{p}{(}\PY{l+m+mi}{3}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{)}\PY{p}{,} \PY{n}{activation}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{relu}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{name}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{C64\PYZus{}2}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{)} \PY{c+c1}{\PYZsh{} 64, 3x3 convolutions}
\PY{n}{model}\PY{o}{.}\PY{n}{add}\PY{p}{(}\PY{n}{layers}\PY{o}{.}\PY{n}{MaxPooling2D}\PY{p}{(}\PY{p}{(}\PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)}\PY{p}{)}\PY{p}{)}
\PY{n}{model}\PY{o}{.}\PY{n}{add}\PY{p}{(}\PY{n}{layers}\PY{o}{.}\PY{n}{Flatten}\PY{p}{(}\PY{p}{)}\PY{p}{)} \PY{c+c1}{\PYZsh{} Make the (None, 2, 2, 64) tensor flat}
\PY{n}{model}\PY{o}{.}\PY{n}{add}\PY{p}{(}\PY{n}{layers}\PY{o}{.}\PY{n}{Dense}\PY{p}{(}\PY{l+m+mi}{64}\PY{p}{,} \PY{n}{activation}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{relu}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{name}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{F64}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{)} \PY{c+c1}{\PYZsh{} Dense Layer 1}
\PY{n}{model}\PY{o}{.}\PY{n}{add}\PY{p}{(}\PY{n}{layers}\PY{o}{.}\PY{n}{Dense}\PY{p}{(}\PY{l+m+mi}{10}\PY{p}{,} \PY{n}{name}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{F10}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{)} \PY{c+c1}{\PYZsh{} Because CIFAR has 10 output classes}
\PY{n}{model}\PY{o}{.}\PY{n}{summary}\PY{p}{(}\PY{p}{)} \PY{c+c1}{\PYZsh{} Complete architecture of the model}
\PY{n}{model}\PY{o}{.}\PY{n}{compile}\PY{p}{(}\PY{n}{optimizer}\PY{o}{=}\PY{n}{tf}\PY{o}{.}\PY{n}{keras}\PY{o}{.}\PY{n}{optimizers}\PY{o}{.}\PY{n}{SGD}\PY{p}{(}\PY{n}{learning\PYZus{}rate}\PY{o}{=}\PY{l+m+mf}{1.4e\PYZhy{}2}\PY{p}{,} \PY{n}{momentum}\PY{o}{=}\PY{l+m+mf}{0.9}\PY{p}{)}\PY{p}{,}
              \PY{n}{loss}\PY{o}{=}\PY{n}{tf}\PY{o}{.}\PY{n}{keras}\PY{o}{.}\PY{n}{losses}\PY{o}{.}\PY{n}{CategoricalCrossentropy}\PY{p}{(}\PY{n}{from\PYZus{}logits}\PY{o}{=}\PY{k+kc}{True}\PY{p}{)}\PY{p}{,}
              \PY{n}{metrics}\PY{o}{=}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{accuracy}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}\PY{p}{)}
\PY{n}{history} \PY{o}{=} \PY{n}{model}\PY{o}{.}\PY{n}{fit}\PY{p}{(}\PY{n}{x\PYZus{}train}\PY{p}{,} \PY{n}{y\PYZus{}train}\PY{p}{,}\PY{n}{batch\PYZus{}size}\PY{o}{=}\PY{l+m+mi}{50}\PY{p}{,} \PY{n}{epochs}\PY{o}{=}\PY{l+m+mi}{10}\PY{p}{,}
                    \PY{n}{validation\PYZus{}data}\PY{o}{=}\PY{p}{(}\PY{n}{x\PYZus{}test}\PY{p}{,} \PY{n}{y\PYZus{}test}\PY{p}{)}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

 \vfill

 \begin{center}
 	\textit{ \textbf{-- Executable code for this assignment can be found on}} \href{https://github.com/bimalka98/Computer-Vision-and-Image-Processing/blob/main/EN2550Assignments/A4/180631J_a04.ipynb}{\faGithub} \textbf{\textit{--}}
 \end{center}

\end{document}