\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[USenglish]{babel}
\usepackage[nodayofweek,level]{datetime}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{intersections,arrows.meta}
\usepackage{pgfplots}
\usepackage[scr]{rsfso}
\usepackage{array}
\usepackage{stackengine}
\hbadness=10001 %gets rid of "\hfill underfull" warning
\stackMath

\usepackage{tikz,pgfplots}

\pgfplotsset{compat=1.10}
% Uncomment to use "fillbetween" function
% otherwise leave commented because
% the red highlighting is annoying
%\usepgfplotslibrary{fillbetween}

% Change for each new hw week
\newcommand{\dueDate}{\formatdate{22}{11}{2017}} % day/month/year
\newcommand{\hwNum}{8}

\begin{document}

	\selectlanguage{USenglish}
	%------------------------ Title Code ------------------------
	\title{Homework: Week \hwNum}
	\author{Joseph Ismailyan}
	\date{}
	\maketitle
	\begin{flushleft}
		Math 100 \\
		Due: \dueDate \\
		Professor Boltje \\
		MWF 9:20a-10:25a
	\end{flushleft}

	%------------------------ Begin Page 1 ------------------------
	\begin{minipage}[t]{0.40\textwidth}

		\section*{Chapter 10. }
		\subsection*{2.}
		\textbf{Proposition}: For every integer $ n\in\mathbb{N} $, it follows that $ 1^2+2^2+3^2+\ldots+n^2=\frac{n(n+1)(2n+1)}{6} $.
		\newline\textit{Proof.} Observe that if $ n=1 $ the statement $ 1^2=\frac{1((1)+1)(2(1)+1)}{6}=\frac{1(2)(3)}{6}=1 $, so the statement is true. Now let $ k\geq  1$, so $ 1^2+2^2+3^2+\ldots+(k+1)^2 = 1^2+2^2+3^2+\ldots+k^2+(k+1)^2 = \frac{k(k+1)(2k+1)}{6} + (k+1)^2 = (k+1)\frac{k(2k+1)}{6} + (k+1) = (k+1)(\frac{k(2k+1+ 6k+6)}{6}) = (k+1)(\frac{2k^2+k+6k+6}{6}) = (k+1)(\frac{2k^2+7k+6}{6}) = (k+1)(\frac{2k^2+4k+3k+6}{6}) = (k+1)(\frac{2k(k+1)+3(k+2)}{6})=(k+1)(\frac{(k+2)(2k+3)}{6})=\frac{(k+1)(k+1+1)(2k+2+1)}{6} = \frac{(k+1)((k+1)+1)(2(k+1)+1)}{6} $. Therefore $ 1^2+2^2+3^2+\ldots+(k+1)^2 = \frac{(k+1)((k+1)+1)(2(k+1)+1)}{6} $. It follows by induction that $ 1^2+2^2+3^2+\ldots+n^2=\frac{n(n+1)(2n+1)}{6} $ for every natural number $ n $.

	\end{minipage}
	% Creates vertical line
	\hfill\vline\hfill
	\begin{minipage}[t]{0.45\textwidth}

		\subsection*{4.}
		\textbf{Proposition}: If $ n\in\mathbb{N} $, then $ 1\cdot2+2\cdot3+3\cdot4+4\cdot5+\dots+n(n+1)=\frac{n(n+1)(n+2)}{3} $.
		\newline\textit{Proof.}  Observe that if $ n=1 $ the statement $ 1(1+1)=\frac{1(1+1)(1+2)}{3}\rightarrow 2=\frac{1(2)(3)}{3}=2 $ so the statement is true for $ n=1 $. Now let $ k\geq 1 $, so  $ 1\cdot2+2\cdot3+3\cdot4+4\cdot5+\dots+(k+1)((k+1)+1) = 1\cdot2+2\cdot3+3\cdot4+4\cdot5+\dots+k(k+1)+(k+1)((k+1)+1) = \frac{k(k+1)(k+2)}{3} + (k+1)((k+1)+1) = \frac{k(k+1)(k+2)}{3} + (k+1)(k+2) = \frac{k(k+1)(k+2) +3(k+1)(k+2)}{3} = \frac{(k+1)(k+2)(k+3)}{3} = \frac{(k+1)((k+1)+1)((k+1)+2)}{3} $. It follows by induction that $ 1\cdot2+2\cdot3+3\cdot4+4\cdot5+\dots+n(n+1)=\frac{n(n+1)(n+2)}{3} $ for every natural number $ n $.

	\end{minipage}
	\pagebreak

	%------------------------ End Page 1 ------------------------

	%------------------------ Begin Page 2 ------------------------

	\begin{minipage}[t]{0.40\textwidth}

		\subsection*{8.}
		\textbf{Proposition}: If $ n\in\mathbb{N} $, then $ \frac{1}{2!}+\frac{2}{3!}+\frac{3}{4!}+\dots+\frac{n}{(n+1)!}=1-\frac{1}{(n+1)!}$.
		\newline\textit{Proof.} Observe that if $ n=1 $, then $ \frac{1}{(1+1)!}=1-\frac{1}{(1+1)!}\rightarrow \frac{1}{2}=1-\frac{1}{2}=\frac{1}{2} $, so the statement is true for $ n=1 $. Now let $ k\geq 1 $, so $ \frac{1}{2!}+\frac{2}{3!}+\frac{3}{4!}+\dots+\frac{(k+1)}{((k+1)+1)!} = \frac{1}{2!}+\frac{2}{3!}+\frac{3}{4!}+\dots+\frac{k}{(k+1)!}+\frac{(k+1)}{((k+1)+1)!} = 1-\frac{1}{(k+1)!} + \frac{(k+1)}{((k+1)+1)!} = 1-\frac{1}{(k+1)!} + \frac{(k+1)}{(k+2)!} = 1-(\frac{1}{(k+1)!} - \frac{(k+1)}{(k+2)!}) = 1-(\frac{1}{(k+1)!} - \frac{(k+1)}{(k+2)(k+1)!}) = 1-\frac{k+1-(k+1)}{(k+2)(k+1)!} = 1-\frac{1}{(k+2)(k+1)!} = 1-\frac{1}{(k+2)!} =  1-\frac{1}{((k+1)+1)!} $. It follows by induction that $ \frac{1}{2!}+\frac{2}{3!}+\frac{3}{4!}+\dots+\frac{n}{(n+1)!}=1-\frac{1}{(n+1)!}$ for every natural number $ n $.

		\subsection*{20.}
		\textbf{Proposition}: $ (1+2+3+\dots+n)^2=1^3+2^3+3^3+\dots+n^3 $ for every $ n\in\mathbb{N} $.
		\newline\textit{Proof.} Observe that if $ n=1 $, then $ 1^2=1^3 $, which is true. Now let $ k\geq1 $, so then $ (1+2+3+\dots+(k+1))^2 = (1+2+3+\dots+k+(k+1))^2. $. If we say $ a=(1+2+3+\dots+k) $ and $ b=(k+1) $ then $ (a+b)^2 = a^2+b^2+2ab $, then substituting back in for $ a $ and $ b $ we get $ (1+2+3+\dots+k)^2 + (k+1)^2+2(1+2+3+\dots+k)(k+1)$. Note that $ (1+2+3+\dots+k) = \frac{k(k+1)}{2} $ and $ (1+2+3+\dots+k)^2 = 1^3+2^3+3^3+\dots+n^3 $. So $ 1^3+2^3+3^3+\dots+n^3 + (k+1)^2+2\frac{k(k+1)}{2}(k+1) = 1^3+2^3+3^3+\dots+n^3 + (k+1)^2+k(k+1) = 1^3+2^3+3^3+\dots+n^3 + (k+1)^2(k+1) = 1^3+2^3+3^3+\dots+n^3 + (k+1)^3 $. It follows by induction that $ (1+2+3+\dots+n)^2=1^3+2^3+3^3+\dots+n^3 $ for every $ n\in\mathbb{N} $.

	\end{minipage}
	% Creates verticle line
	\hfill\vline\hfill
	\begin{minipage}[t]{0.45\textwidth}

		\subsection*{30.}
		\textbf{Proposition}: $F_n$ is the $n$th Fibonacci number. Show that $ F_n=\frac{(\frac{1+\sqrt{5}}{2})^n-(\frac{1-\sqrt{5}}{2})^n}{\sqrt{5}}  $
		\textit{Proof.} Observe that if $ n=1 $, then $ F_1=\frac{(\frac{1+\sqrt{5}}{2})^1-(\frac{1-\sqrt{5}}{2})^1}{\sqrt{5}} = \frac{\frac{2\sqrt{5}}{2}}{\sqrt{5}} = 1 $. And if $ n=2 $, then $F_2=\frac{(\frac{1+\sqrt{5}}{2})^2-(\frac{1-\sqrt{5}}{2})^2}{\sqrt{5}} = \frac{(\frac{3+\sqrt{5}}{2})-(\frac{3-\sqrt{5}}{2})}{\sqrt{5}} = \frac{2\sqrt{5}}{2\sqrt{5}} = 1 $. Now let $ k\geq 1 $. Note that $ F_{k+2}=F_k+F_{k+1} $. So then
		$ \frac{(\frac{1+\sqrt{5}}{2})^k-(\frac{1-\sqrt{5}}{2})^k}{\sqrt{5}}+\frac{(\frac{1+\sqrt{5}}{2})^{k+1}-(\frac{1-\sqrt{5}}{2})^{k+1}}{\sqrt{5}} = $
		$ \frac{(\frac{1+\sqrt{5}}{2})^k - (\frac{1-\sqrt{5}}{2})^k + (\frac{1+\sqrt{5}}{2})(\frac{1+\sqrt{5}}{2})^k - (\frac{1-\sqrt{5}}{2})(\frac{1-\sqrt{5}}{2})^k}{\sqrt{5}} = $
		$ \frac{(\frac{1+\sqrt{5}}{2})^k + (\frac{1+\sqrt{5}}{2})(\frac{1+\sqrt{5}}{2})^k -[(\frac{1-\sqrt{5}}{2})^k+(\frac{1-\sqrt{5}}{2})(\frac{1-\sqrt{5}}{2})^k]}{\sqrt{5}} =$
		$ \frac{(\frac{1+\sqrt{5}}{2})^k(1+(\frac{1+\sqrt{5}}{2}))-[(\frac{1-\sqrt{5}}{2})^k(1+(\frac{1-\sqrt{5}}{2}))]}{\sqrt{5}} = $
		$ \frac{(\frac{1+\sqrt{5}}{2})^k(\frac{3+\sqrt{5}}{2}) - (\frac{1-\sqrt{5}}{2})^k(\frac{3-\sqrt{5}}{2})}{\sqrt{5}} =$\\
		Note that $ \frac{3+\sqrt{5}}{2} = (\frac{1+\sqrt{5}}{2})^2 $ \\and $ \frac{3-\sqrt{5}}{2} = (\frac{1-\sqrt{5}}{2})^2 $. Continuing...\\
		$ \frac{(\frac{1+\sqrt{5}}{2})^k(\frac{1+\sqrt{5}}{2})^2 - (\frac{1-\sqrt{5}}{2})^k(\frac{1-\sqrt{5}}{2})^2}{\sqrt{5}} =$\\
		$ \frac{(\frac{1+\sqrt{5}}{2})^{k+2}-(\frac{1-\sqrt{5}}{2})^{k+2}}{\sqrt{5}}$.
		It follows by induction that $ F_n=\frac{(\frac{1+\sqrt{5}}{2})^n-(\frac{1-\sqrt{5}}{2})^n}{\sqrt{5}}  $ for every $ n\in\mathbb{N} $.

		\subsection*{32.}
		\textbf{Proposition}: Show that the number of $ n $-digit binary numbers that have no consecutive
		1's is the Fibonacci number $  F_{n+2} $\\
		\textit{Proof.} Let $ n=1 $, then $ a_1=2\rightarrow F_{1+2}=F_3=2 $. Assume that the given statement is true for $ n=k $. So $ a_k=F_{k+2} $. Observe that the sequence of $ a_n's $ satisfy $ a_{n+1}=a_n+a_{n-1} $. So $ a_{n+1}=a_n+a_{n-1} = F_{n+2}+F_{(n-1)+2} = F_{n+2}+F_{n+1}= F_3 = F_{(n+1)+2}$ thus the result is true for $ n=k+1 $. Therefore by induction, the number of $ n $-digit binary numbers that have no consecutive
		1's is the Fibonacci number $  F_{n+2} $, where $ n\in\mathbb{N} $.

	\end{minipage}
	\pagebreak

	%------------------------ End Page 4 ------------------------

\end{document}


\chapter{Mna}
Simple tool for calculating response of a linear circuit using
Modified Nodal Analysis.
\section{mna}\hypertarget{mna}
Class constructor.
\subsection{Syntax}
\begin{verbatim}
    Y = mna
    Y = mna(n)
\end{verbatim}

\subsection{Description}
\verb"Y = mna" returns a default mna object with 1 node.\\
\verb"Y = mna(n)", where \verb"n" is a positive scalar, returns a mna object with \verb"n" nodes.\\

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{stamp}\hypertarget{stamp}
Inserts the MNA footprint of a circuit element into the
mna-object.
\subsection{Syntax}
\begin{verbatim}
    Y = stamp(Y,type,element,conn)
\end{verbatim}
\subsection{Description}
\verb"Y = stamp(Y,type,element,conn)" returns a mna-object with
the circuit element \verb"type" with name \verb"element" between
nodes \verb"conn".

\verb"type" can be a reciprocal two port element
\verb"'R','G','L','C'" or a non reciprocal four port
\verb"'VCCS','GY'". \verb"element" can be any string describing
the element name. For two ports \verb"conn" is a two element
vector containing the nodes of the circuit element, for four ports
it is a four element vector.

\subsection{Example}
Produces a indefinite MNA description of a standard transistor
$\pi$-pad.
\begin{verbatim}
    >> Y = stamp(mna(3),'C','Cpg',[1 3]);
    Y = stamp(Y,'L','Lg',[1 2]);
    Y = stamp(Y,'C','Cpg',[2 3]);
    >> Y
    ans =
        '+s.*Cpg+1./(s*Lg)'    '-1./(s*Lg)'           '-s.*Cpg'
        '-1./(s*Lg)'           '+1./(s*Lg)+s.*Cpg'    '-s.*Cpg'
        '-s.*Cpg'              '-s.*Cpg'              '+s.*Cpg+s.*Cpg'
    ans =
        'Cpg'    'Lg'
\end{verbatim}

Example of a voltage controlled current source.
\begin{verbatim}
    >> Y=stamp(mna(4),'VCCS','gm',[1 2 3 4])
    ans =
           []     []       []     []
        '+gm'     []    '-gm'     []
           []     []       []     []
        '-gm'     []    '+gm'     []
    ans =
        'gm'
\end{verbatim}

Example of a gyrator.
\begin{verbatim}
    >> Y=stamp(mna(4),'GY','g',[1 2 3 4]) ans =
      []    '+g'      []    '-g'
    '-g'      []    '+g'      []
      []    '-g'      []    '+g'
    '+g'      []    '-g'      []
    ans =
        'g'
\end{verbatim}

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

\vspace{3mm} \hrule
\section{freq}\hypertarget{freq}
Sets the frequencies for subsequent calculation of a numeric
MNA-matrix.
\subsection{Syntax}
\begin{verbatim}
    Y = freq(Y,frequencies)
\end{verbatim}
\subsection{Description}
\verb"Y = freq(Y,frequencies)" returns the mna object with
calculation frequencies set to \verb"frequencies".

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

\vspace{3mm} \hrule
\section{params}\hypertarget{params}
Returns a cell vector containing the circuit elements in the
circuit.
\subsection{Syntax}
\begin{verbatim}
    x = params(Y)
\end{verbatim}
\subsection{Description}
\verb"x = params(Y)" returns a cell vector with the circuit
elements.

\subsection{Examples}
\begin{verbatim}
    >> Y = read_netlist(mna,'test/test.nl')
    ans =
        '+s.*Cpg+1./(s*Lg)'    '-1./(s*Lg)'                 []
        '-1./(s*Lg)'           '+1./(s*Lg)+s.*Cpg'          []
                         []                     []    '+1./Rs'
    ans =
        'Cpg'    'Lg'    'Rs'
    >> params(Y)
    ans =
        'Cpg'    'Lg'    'Rs'
    >>
\end{verbatim}
%%%%%%%%%%%%%%%%%%%%%%%%%

\vspace{3mm} \hrule
\section{calc}\hypertarget{calc}
Calculates a numeric MNA-matrix.
\subsection{Syntax}
\begin{verbatim}
    X = calc(Y,parameters)
\end{verbatim}
\subsection{Description}
\verb"X = calc(Y,parameters)" returns a \verb"xparam"-object
containing the MNA-matrix calculated at the frequencies set by
\verb"freq". \verb"parameters" is a numeric vector containing the
values of the circuit elements. This vector must have the same
ordering as the elements were inserted into the mna-object.

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{gnd}\hypertarget{gnd}
Connects specified nodes to ground and thereby reduces the
dimension of the MNA-matrix.
\subsection{Syntax}
\begin{verbatim}
    Y = gnd(Y,conn)
\end{verbatim}
\subsection{Description}
\verb"Y = gnd(Y,conn)", where \verb"conn" is a vector containing
the nodes to be connected to ground.

\subsection{Examples}
Example of a gyrator with nodes 3 and 4 grounded.
\begin{verbatim}
    >> Y=stamp(mna(4),'GY','g',[1 2 3 4])
    ans =
          []    '+g'      []    '-g'
        '-g'      []    '+g'      []
          []    '-g'      []    '+g'
        '+g'      []    '-g'      []
    ans =
        'g'
    >> Y=gnd(Y,[3 4])
    ans =
          []    '+g'
        '-g'      []
    ans =
        'g'
\end{verbatim}

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{read\_netlist}\hypertarget{readnetlist}
Reads a netlist and builds a corresponding mna-object.
\subsection{Syntax}
\begin{verbatim}
    Y = read_netlist(Y,file)
\end{verbatim}
\subsection{Description}
\verb"Y = read_netlist(Y,file)", where \verb"file" is a string
containing the path to the netlist file.

The netlist should be a text-file of the following form:
\begin{verbatim}
    C Cpg 1 3
    L Lg  1 2
    C Cpg 2 5
    R Rs  3 4
    GND 3 5
\end{verbatim}
The circuit elements could be given in any order.

\subsection{Examples}
The file \verb"test.nl" contains the netlist above.
\begin{verbatim}
    >> Y = read_netlist(mna,'test/test.nl')
    ans =
        '+s.*Cpg+1./(s*Lg)'    '-1./(s*Lg)'                 []
        '-1./(s*Lg)'           '+1./(s*Lg)+s.*Cpg'          []
                     []                     []    '+1./Rs'
    ans =
        'Cpg'    'Lg'    'Rs'
\end{verbatim}

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{display}\hypertarget{display}
Displays the MNA-matrix and a list of circuit variables of a
mna-object.
\subsection{Syntax}
\begin{verbatim}
    display(Y)
\end{verbatim}

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


\documentclass{article}
\usepackage{amsmath, amssymb, amsthm, enumerate, framed, graphicx}
\usepackage[usenames,dvipsnames]{color}
\usepackage{bm}
\usepackage[colorlinks=true,urlcolor=blue]{hyperref}
\usepackage{geometry}
\geometry{margin=1in}
\usepackage{float}
\setlength{\marginparwidth}{2.15cm}
\usepackage{booktabs}
\usepackage{enumitem}
\usepackage{epsfig}
\usepackage{setspace}
\usepackage{parskip}
\usepackage{hyperref}
\usepackage[normalem]{ulem}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage[font=scriptsize]{subcaption}
\usepackage{float}
\usepackage[]{algorithm2e}
\usepackage{environ}
\usepackage{bbm}
\usepackage[normalem]{ulem}
\usepackage{color}
\usepackage{tcolorbox}

%% To HIDE SOLUTIONS (to post at the website for students), set this value to 0: \def\issoln{0}
\def\issoln{1}
% Some commands to allow solutions to be embedded in the assignment file.
\ifcsname issoln\endcsname \else \def\issoln{1} \fi
% Default to an empty solutions environ.
\NewEnviron{soln}{}{}
\if\issoln 1
% Otherwise, include solutions as below.
\RenewEnviron{soln}{
    \leavevmode\color{red}\ignorespaces
    \textbf{Solution} \BODY
}{}
\fi

%\newcommand{\norm}[1]{\lVert #1 \rVert}
%\newcommand{\st}{\mathrm{s.t.}}

\makeatletter
\newcommand{\removelatexerror}{\let\@latex@error\@gobble}
\makeatother

\begin{document}
\section*{Part 1: Multiple Choice and Short Answer Questions [38 points]}
\begin{enumerate}
    \item \textbf{[1 pt]} \textbf{True or False}: One reason that the MAP might be preferred over the MLE is that MLE can have a tendancy to overfit small amounts of data.

    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[2 pt]} Let $X$ be the result of a coin toss, where $X=1$ if it comes up heads and $X=0$ otherwise.  The coin has an unknown probability $p_1$ of coming up heads.
    \\ Suppose that we observe the following sequence of of coin toss outcomes:
    $$
    (1,0,1,1,0,0,1,0,1,0,1)
    $$
    What is the maximum likelihood estimate for $p_1$?
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[2 pt]} Now suppose that someone else observes the coin flip (still denoted by $X$) and tells you $Y$, the outcome of the flip, but this person only reports the correct result with probability $p_2$.  Suppose we have the following dataset:
    $X$-the sequence of actual coin toss outcomes- is: \\
    $$
    (1,0,1,1,0,0,1,0,1,0,1)
    $$
    $Y$- the sequence of coin toss outcomes we were told by the other person-is:\\
    $$
    (1,0,1,0,1,0,1,0,1,0,1)
    $$
    What is the maximum likelihood estimate for $p_2$?

    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[2 pt]} Another person is observing the coin toss too, but the probability for this person to report the correct result depends on the actual outcome of the coin toss. Let  $p_{a,b}$ be the probability for that person to report outcome $b$ given that the actual outcome of the coin toss is $a$ , where  $a,b\in\{0,1\}$. Consider the same  $X$  and  $Y$  values as given in previous question, what is the maximum likelihood estimate for  $p_{0,0}$?
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[4 pt]} Let $\theta$ be a random variable with the probability density function:
    \[
    f(\theta) =
    \begin{cases}
    2\theta, &\text{ if } 0 \leq \theta \leq 1 \\
    0, &\text{ otherwise}.
    \end{cases}
    \]
    Suppose that another random variable $Y$, conditioning on $\theta$, follows an exponential distribution with $\lambda=3\theta$. Note that the exponential distribution with parameter $\lambda$ has a probability density function
    \[
    f(y) =
    \begin{cases}
    \lambda e^{-\lambda y}, &\text{ if } y \geq 0, \\
    0, &\text{otherwise}.
    \end{cases}
    \]
    Find the MAP estimate of $\theta$ given $Y=4$ is observed.
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[1 pt]} \textbf{True or False}: If we choose an incorrect set of parameters for the beta prior of Bernoulli distribution, then the MAP estimate will not converge (as the number of training examples grows toward infinity) to the true value.  (here, when we say an 'incorrect' set of parameters for the beta prior, we mean a set of parameters for which the most probable value is different from the true value of the parameter we are trying to estimate.)
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}
    \item \textbf{[1 pt]} \textbf{True or False}: In case we choose Beta parameters that correspond to a uniform prior, the value of the MAP estimate will be identical to that of the MLE.
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}
    \item \textbf{[4 pt]} The next two questions refer to the following scenario: suppose that $0.5\%$ people have cancer. Someone decided to take a medical test for cancer. The outcome of the test can either be positive (cancer) or negative (no cancer). The test is not perfect - among people who have cancer, the test comes back positive $96\%$ of the time. Among people who don't have cancer, the test comes back positive $2\%$ of the time. For the following questions, you should assume that the test results are independent of each other, given the true state (\textit{cancer} or \textit{no cancer}).

    What is the probability of a test subject having cancer, given that the subject's test result is positive?
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[4 pt]} In the same scenario as the previous question, a test subject's first test returned positive, and the subject decided to do a second independent test. The second test returned negative. What is the probability that this subject has cancer?
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}
    \newpage
    \item \textbf{[1 pt]} \textbf{True or False}: Gaussian Naive Bayes can be used to perfectly classify the training data shown below.
    \begin{center}
    \vspace{2em}
        \textbf{Please refer to the pdf for image of this question}.
    \vspace{2em}
    \end{center}
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[1 pt]} \textbf{Note}: This question is based on material discussed in Part 2 - Implementing Na{\"i}ve Bayes of the homework assignment. Please complete Part 2 of this assignment before attempting these questions.

    How many parameters will the model need under the Na{\"i}ve Bayes assumption, assuming that $P(X_w = x_w \vert Y = y)$ is a Bernoulli distribution for each $w$ and $P(Y=y)$ is also a Bernoulli distribution? All answers are shown as a function of the vocabulary size $V$.
    \begin{itemize}
        \item[A.] $V$
        \item[B.] $2V$
        \item[C.] $V+1$
        \item[D.] $2V+1$
    \end{itemize}
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[1 pt]} \textbf{Note}: This question is based on material discussed in Part 2 - Implementing Na{\"i}ve Bayes of the homework assignment. Please complete Part 2 of this assignment before attempting these questions.

    How many parameters (also as a function of $V$) will the model need if we \textbf{do not} make the NB assumption, assuming $P(Y=y)$ is Bernoulli again and all of the features in $X$ have binary labels?
    \begin{itemize}
        \item[A.] $2V$
        \item[B.] $2^V$
        \item[C.] $2(2^V-1)+1$
        \item[D.] $2^{2V+1}$
    \end{itemize}
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}
    \newpage

    \item \textbf{[1 pt]} \textbf{Note}: This question is based on material discussed in Part 2 - Implementing Na{\"i}ve Bayes of the homework assignment. Please complete Part 2 of this assignment before attempting these questions.

    Does the Na{\"i}ve Bayes assumption hold true for our dataset? Select a valid explanation for your answer.
    \begin{itemize}
        \item[A.] True. The appearances of each pair of words are not related regardless of review class.
        \item[B.] False. The appearances of some common stopwords (say, pronoun \textit{he} and \textit{she}) are dependent in both classes of movie reviews.
        \item[C.] True. The number of occurrences for words are not conditionally independent, but the appearances certainly do.
        \item[D.] False. For example, \textit{Darth} and \textit{Vader} are unlikely to be independent in both positive and negative reviews.
    \end{itemize}
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[1 pt]} \textbf{Note: only one of the answers is correct.}  This question is based on material discussed in Part 2 - Implementing Na{\"i}ve Bayes of the homework assignment. Please complete Part 2 of this assignment before attempting these questions.

    Which of the following statement(s) is/are correct with respect to using stopwords as features?
    \begin{itemize}
        \item[A.] We can keep stopwords as features. They have no effect on the accuracy of classifier.
        \item[B.] Stopwords add value to the dataset which is useful for correctly classifying the document.
        \item[C.] Removing stopwords helps in reducing noise/false positives.
        \item[D.] All of the above.
        \item[E.] None of the above.
    \end{itemize}
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[1 pt]} \textbf{Note: only one of the answers is correct.}  this is a single choice question. This question is based on material discussed in Part 2 - Implementing Na{\"i}ve Bayes of the homework assignment. Please complete Part 2 of this assignment before attempting these questions.

    We will experiment with two different parameter settings for our prior over $\theta_{yw}$:
    \begin{enumerate}
        \item $\beta_0=5$ and $\beta_1=7$, and
        \item $\beta_0=7$ and $\beta_1=5$.
    \end{enumerate}
    Train your classifier with 2 sets of data (\texttt{XTrainSmall},\texttt{yTrainSmall}) and (\texttt{XTrain},\texttt{yTrain}) with the first parameter setting. Then, use the learned classifiers to classify whether the reviews \texttt{XTest} are positive or negative. How do the classification errors compare?
    \begin{itemize}
        \item[A.] Error is smaller when using \texttt{XTrain},\texttt{yTrain}.
        \item[B.] Error is smaller when using \texttt{XTrainSmall},\texttt{yTrainSmall}.
        \item[C.] Errors are equal.
    \end{itemize}
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}
    \newpage
    \item \textbf{[4 pt]} \textbf{Note}: This question is based on material discussed in Part 2 - Implementing Na{\"i}ve Bayes of the homework assignment. Please complete Part 2 of this assignment before attempting these questions.

    Train your classifier on the data contained in
    \texttt{XTrain} and \texttt{yTrain} with the second parameter setting in the previous problem. Then, use the learned classifier to classify whether the reviews \texttt{XTest} are positive or negative. After comparing classification errors produced by classifiers trained by \texttt{XTrain} and \texttt{yTrain} with 2 parameter settings, which parameter setting was a better choice for the prior on $\theta_{yw}$?
    \begin{itemize}
        \item[A.] $\beta_0=5$ and $\beta_1=7$
        \item[B.] $\beta_0=7$ and $\beta_1=5$
    \end{itemize}
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[4 pt]} \textbf{Note}: This question is based on material discussed in Part 2 - Implementing Na{\"i}ve Bayes of the homework assignment. Please complete Part 2 of this assignment before attempting these questions.

    Consider again the Na{\"i}ve Bayes classifiers trained with \texttt{XTrain} and \texttt{yTrain} for both parameter settings. Which of the settings of $\beta_0$ and $\beta_1$ make more sense if we strongly believe the true value of $\theta_{yw}$ lies in the interval $[0.1, 0.3]$?
    \begin{itemize}
        \item[A.] $\beta_0=5$ and $\beta_1=7$
        \item[B.] $\beta_0=7$ and $\beta_1=5$
    \end{itemize}

    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[0.5 pt]} \textbf{Collaboration Policy Question}: Did you receive any help whatsoever from anyone in solving this assignment? Please answer \textit{yes} or \textit{no}.
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[0.5 pt]} \textbf{Collaboration Policy Question}: If you answered \textit{yes} on the previous question, please give full details below (e.g., \textit{Christopher Nolan} explained to me what is asked in Question 3.4).
    \begin{tcolorbox}[height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[0.5 pt]} \textbf{Collaboration Policy Question}: Did you give any help whatsoever to anyone in solving this assignment? Please answer \textit{yes} or \textit{no}.
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}
    \newpage
    \item \textbf{[0.5 pt]} \textbf{Collaboration Policy Question}: If you answered \textit{yes} on the previous question, please give full details below (e.g., I pointed \textit{Michael Bay} to section 2.3 since he didn't know how to proceed with Question 2).
    \begin{tcolorbox}[height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[0.5 pt]} \textbf{Collaboration Policy Question}: Did you find or come across code that implements any part of this assignment? Please answer \textit{yes} or \textit{no}.
    \begin{tcolorbox}[width=\linewidth/3,height=1.5cm]
    %Your solution here
    \end{tcolorbox}

    \item \textbf{[0.5 pt]} \textbf{Collaboration Policy Question}: If you answered \textit{yes} on the previous question, please give full details below (book \& page, URL \& location, movies \& scene, etc).
    \begin{tcolorbox}[height=1.5cm]
    %Your solution here
    \end{tcolorbox}

\end{enumerate}

\end{document}


\documentclass[10pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\author{Daniel Frederico Lins Leite}
\title{Asymptotic Statistics}
\begin{document}
\maketitle
\tableofcontents

\chapter {Introduction}

The idea of this crash course on asymptotic Statistics is to have all proofs and ideas related with the one of the three most important aspects of statistics, being estimation. The other two being confidence interval and hypothesis testing, off course.\\

The idea is to present as gentle as possible the needed concepts to follow a more thorough course. Here one will find the concepts of "asymptotic", "estimators", Markov and Chebyshevs inequalities, convergnce in the sense for Statistics, characteristic functions, the weak law of large numbers and the Central Limit Theorem.

\chapter{Asymptotic Behaviour of Estimators}
\section{Introduction}

As "Asymptotic Statistics" we understand the study of Probabilities and Statistics as one of its parameters go to infinity. We will study for example:

\begin{align}
	\lim_{n->\infty}{P(X_n \in A)}
\end{align}

or

\begin{align}
	\lim_{n->\infty}{P((X_n - X) \in A)}
\end{align}

Here $P(x \in A) = \int_{A}{f(x)dx}$. The lower-case $f$ has a especial meaning because that is how we label the PDF of a distribution. We use upper-case $F$ to mean de CDF. We also know that to be a valid "random variable":

\begin{align}
	\int_{-\infty}^{\infty}{f(x)dx} = 1 && f(x) \ge 0
\end{align}

Where $X_n$ means off course a "random variable", pure in the sense that is obeys a known distribution or a "transformed random variable". We will be interested to know if a "random variable" converge to a constant, to another "random variable" of if it diverge. Throughout this book we will see that we can conclude different things if any of these cases are proved as true.

One of the most interesting, or one the most useful, "random variables" is the "estimator". We call "estimator" any "statistic" that does not depend on any of the "population parameters" an possible estimator. If on top of that we define that this "statistic" will be used as an "estimation" of the "population parameter", we have a "estimator".

A famous statistic is the "sample mean" that we will define as:

\begin{align*}
	X_i \sim P_{\theta}\\
	S_n = \sum_{\forall i}{X_i}\\
	\bar{X_{n}} = \frac{S_n}{n}\\
\end{align*}

We understand the expressions above as:
\begin{itemize}
	\item {$X_i$ is a "random variable" that follows a distribution described with a parameter $\theta$, that can be a number or various numbers;}
	\item {The set of all $X_i$ are i.i.d.}
	\item {$S_n$ is another "random variable". Being the sum of $n$ "random variables" allows us to know the distribution of $S_n$ in some cases}
	\item {$\bar{X_{n}}$ is another "random variable" that applies the "transformation" $1/x$ to the "random variable" $S_n$}
\end{itemize}

Being random variables, $X_i$, $S_n$, $\bar{X_{n}}$, allow them to have "expected values", "variations" and any other statistics themselves. This is a powerfull source of confusion. So we can have:

\begin{align*}
	E[X_i] = a \text{ or } f_a(i)\\
	E[S_n] = b \text{ or } f_b(n)\\
	E[\bar{X_{n}}] = c \text{ or } f_c(n)\\
\end{align*}

In these cases, $a$, $b$, $c$ are not "random variables". They are constants, or functions that depend on the "random variables" "parameter". The same can be said about their "variance" and other statistics.

Remember that "Expected Value" is:

\begin{align}
	E[X] = \int_{-\infty}^{\infty}{x*f(x)dx}
\end{align}

Actually this is just the "First Moment" of $X$. We can the n-th "moment" as:

\begin{align}
	\int_{-\infty}^{\infty}{x^n*f(x)dx}
\end{align}

or

\begin{align*}
	E[X^1] = \int_{-\infty}^{\infty}{x^1*f(x)dx}\\
	E[X^2] = \int_{-\infty}^{\infty}{x^2*f(x)dx}\\
	...\\
	E[X^n] = \int_{-\infty}^{\infty}{x^n*f(x)dx}\\
\end{align*}

And this allows us to calculate the variance as, the "second moment" minus the square of the "first moment":

\begin{align}
	\text{VAR}[X] = E[X^2] - E[X]^2\\
\end{align}

or

\begin{align}
\text{VAR}[X] = E[(X-E[X])^2]
\end{align}

And we also have the "standard deviation".

\begin{align}
	\text{SD}[X] = \sqrt{\text{VAR}[X]}
\end{align}

\section{Markov's Inequality Intuition}

What can you say about a "Probbility distribution" if you only have its mean? Probably not much. But not much is more than nothing. If we imagine a standard dice with six faces, but not necessarily fair, each face having the same probability. The question is, knoning just the mean, can we affirm with a 100\% certainity that the dice is not fair?

For example, if someone tell us that the $E[X_i]$, $X_i$ being a roll of the dice is $6$. We know that the only way of this happening is when $P(X = 6) = 1$ and $P(X < 6) = 0$ because of the "Expected Value" definition.

\begin{align*}
	E[X] &= \sum_{i=1}^{6}{i*P(X=i)} = 6\\
	&= 6*P(X=6) = 6\\
	& P(X=6) = \frac{6}{6}\\
	& P(X=6) = 1\\
\end{align*}

So it seems that knowing only the $E[X]$ allows us to take some conclusions of the distribution. But this is a extreme case. Can we take conclusions if for example the $E[X]$ is the same as the "expected value" when the dice is fair? Let us see.

We know that the $E[X]$ is:

\begin{align*}
	E[X] &= \sum_{i=1}^{6}{i*P(X=i)}\\
\end{align*}

And we know that $i \in {1,2,3,4,5,6}$ or $i > 0$. We know that $i$ is positive. We also know that $P(X = i) \in [0,1]$. It is also positive. The multiplication of two positive values is also positive. And the sum of two positive values is also positive.

\begin{align*}
	i &> 0\\
	P(X = i) &> 0\\
	a * b &> 0 \text{, if } a > 0 \text{ and } b > 0\\
	a + b &> 0 \text{, if } a > 0 \text{ and } b > 0
\end{align*}

Which give us that

\begin{align*}
	E[X] &= \sum_{i=1}^{6}{i*P(X=i)}\\
	&= \sum_{i=1}^{5}{i*P(X=i)} + 6*P(X=6)
\end{align*}

We know that this summuation is going to be greater than zero. Whick make the $E[X]$ greater than or equal $6*P(X=6)$. In the extreme case above, it was equal because the summation was zero. But the summation can be greater than zero (never lower).

\begin{align*}
	E[X] &= \sum_{i=1}^{6}{i*P(X=i)}\\
	E[X] &= \sum_{i=1}^{5}{i*P(X=i)} + 6*P(X=6)\\
	E[X] &\ge 6*P(X=6)\\
	\frac{E[X]}{6} &\ge P(X=6)\\
	P(X=6) &\le \frac{E[X]}{6}
\end{align*}

Which is quite surprising if you think. But looking from another perspective, it is quite obvious that there is a relationship with the $P(X=6)$ and the "expected value".

\section{Markov's Inequality}

This relationship was discovered by two mathematicians:\\
Andrey Markov\\
\includegraphics[width=2cm]{AAMarkov}\\

and Pafnuty Chebyshev\\
\includegraphics[width=2cm]{Chebyshev}\\

And it is generalized as:
\begin{itemize}
	\item {$X$ is a nonnegative random variable;}
	\item {$a > 0$}
\end{itemize}

We now that:

\begin{align*}
	E[X] &= \int_{-\infty}^{\infty}{x*f(x)dx}\\
	&= \int_{0}^{\infty}{x*f(x)dx} && \text{because of } X > 0\\
	&= \int_{0}^{a}{x*f(x)dx} + \int_{a}^{\infty}{x*f(x)dx} && \text{because of } a > 0\\
\end{align*}

We know that $\int_{0}^{a}{x*f(x)dx} > 0$, so

\begin{align*}
	E[X] &= \int_{0}^{a}{x*f(x)dx} + \int_{a}^{\infty}{x*f(x)dx}\\
	E[X] &= b + \int_{a}^{\infty}{x*f(x)dx} && b > 0\\
	E[X] - b &= \int_{a}^{\infty}{x*f(x)dx}\\
	E[X] &\ge \int_{a}^{\infty}{x*f(x)dx} && b > 0\\
\end{align*}

Before the next step, we must realize that:

\begin{align*}
	\int_{a}^{\infty}{x*f(x)dx}&\\
	&=\int_{a}^{\infty}{(a+(x-a))*f(x)dx}\\
	&=\int_{a}^{\infty}{a*f(x)dx}+\int_{a}^{\infty}{(x-a)*f(x)dx}\\
\end{align*}

We must realize the in the second integration, $x \ge a$ and $f(x) > 0$ which makes the second integration greater than or equal to zero. This allow us to:

\begin{align*}
	\int_{a}^{\infty}{x*f(x)dx} &=\int_{a}^{\infty}{a*f(x)dx}+\int_{a}^{\infty}{(x-a)*f(x)dx}\\
	\int_{a}^{\infty}{x*f(x)dx}	&=\int_{a}^{\infty}{a*f(x)dx}+c\\
	\int_{a}^{\infty}{x*f(x)dx}	- c &=\int_{a}^{\infty}{a*f(x)dx}\\
	\int_{a}^{\infty}{x*f(x)dx}	&\ge \int_{a}^{\infty}{a*f(x)dx}\\
\end{align*}

We can use this fact with the fact that if $a>b$ and $b>c$ then $a>c$, and finish our proof

\begin{align*}
	E[X] &\ge \int_{a}^{\infty}{x*f(x)dx} && b > 0\\
	E[X] &\ge \int_{a}^{\infty}{a*f(x)dx} && \text{see above}\\
	E[X] &\ge a*\int_{a}^{\infty}{f(x)dx}\\
	E[X] &\ge a*P(X \ge a)\\
	\frac{E[X]}{a} &\ge P(X \ge a)\\
\end{align*}
\begin{align}
	P(X \ge a) &\le \frac{E[X]}{a}
\end{align}

Which is the obivious generalization we did in the intuition section. But remember that we are studying "Asymptotic Statistics", this is useful for us, because in some cases, the right side will depend on $n$, and in the limit the right side will converge or diverge. This will give even more information about the "random variable". All of this with just its "expected value".

\section{Chebyshev's Inequality}

One of the interesting aspects of the "Markov Inequality" is that it only contains two assumptions and both are very generic. It only depends on X being non-negative and a being nonnegative. We know that a nonnegative function, when sifted on the X-Axis is still nonnegative. So $X-a$ is still non-negative and the "Markov Inequality" still apllies.

\begin{align*}
P((X-b) \ge a) &\le \frac{E[X-b]}{a}
\end{align*}

Another "transformation" that keeps the non-negativiness of the function is squaring its value.

\begin{align*}
P((X-b)^2 \ge a^2) &\le \frac{E[(X-b)^2]}{a^2}
\end{align*}

But we do not have any restriction on $b$ here, and we can easily choose the "expected value" of $X$.

\begin{align*}
P((X-E[X])^2 \ge a^2) &\le \frac{E[(X-E[X])^2]}{a^2}
\end{align*}

Which gives us the $\text{VAR}[X]$. Although this inequality is often writen in a different form.

\begin{align*}
P((X-E[X])^2 \ge a^2) &\le \frac{\text{VAR}[X]}{a^2}\\
\end{align*}

But this is still not the most "famous" way this formula is written, and the reason is going to be appear in future chapters. To derive the most famous form we are going to apply some transformations only to the left side, because they will not change the boundness of the inequality.

\begin{align*}
P(\sqrt{(X-E[X])^2}\ge\sqrt{a}) &\le \frac{\text{VAR}[X]}{a^2}\\
P(|X-E[X]| \ge a) &\le \frac{\text{VAR}[X]}{a^2} && \text{ no modulus on a because } a>0
\end{align*}

\begin{align}
	P(|X-E[X]| \ge a) &\le \frac{\text{VAR}[X]}{a^2}
\end{align}

Which is quite fascinating, because it gives you a know bound when you know the distribution "expected value" and "variance". This formula will demonstrate itself very powerfull and useful in the study of sampling and convergence.

We can also interpret that it is "harder", less probable, of being $n$ "standard deviations" from the "expected value". What is very intuitive, if you stop to think about it. What you can not do, is forget the "standard deviation" and say that it is "harder" to be far from the "expected value". This is not true. Without the "standard deviation", or "variance" this phrase, does not make any send. Stop to think of a distribution with "infinite" "variance". It is very "easy" to be "infinitilly" far from the "expected value".

\section{Markov's and Chebyshev's Inequalities}

If you search you will probably find another formula for both inequalities. They are all equivalent, off course.

\begin{align*}
	P(X \ge a) \le \frac{E[X]}{a}\\
	P(X \ge a*E[X]) \le \frac{E[X]}{a*E[X]} && E[X] > 0
\end{align*}
\begin{align}
	P(X \ge a*E[X]) \le \frac{1}{a}
\end{align}

\begin{align*}
P((X-E[X])^2 \ge a^2) &\le \frac{E[(X-E[X])^2]}{a^2}\\
P((X-E[X])^2 \ge a^2*\text{VAR}[X]) &\le \frac{E[(X-E[X])^2]}{a^2*\text{VAR}[X]}\\
P((X-E[X])^2 \ge a^2*\text{VAR}[X]) &\le \frac{\text{VAR}[X]}{a^2*\text{VAR}[X]}\\
P((X-E[X])^2 \ge a^2*\text{VAR}[X]) &\le \frac{1}{a^2}\\
P(\sqrt{(X-E[X])^2}\ge\sqrt{a^2*\text{VAR}[X]}) &\le \frac{1}{a^2}\\
\end{align*}
\begin{align}
P(|(X-E[X])| \ge a*\text{SD}[X]) &\le \frac{1}{a^2}
\end{align}

\section{68-95-99.7 rule}

\section{Summary}

\subsection{Markov's Inequality}

\begin{align}
P(X \ge a) &\le \frac{E[X]}{a}
\end{align}

or

\begin{align}
P(X \ge aE[X]) &\le \frac{1}{a}
\end{align}

\subsection{Chebyshev's Inequality}

\begin{align}
P(|X - E[X] \ge a) &\le \frac{\text{VAR}[X]}{a^2}
\end{align}

or

\begin{align}
P(|X - E[X] \ge a*\text{SD}[X]) &\le \frac{1}{a^2}
\end{align}

	\chapter{Convergence}
	\section{Convergence in probability of a random variable}
	\section{Convergence in probability to a random variable}
	\section{Convergence in probability of a random variable to a constant}
	\section{Convergence in distribution of a random variable}
	\section{Slutsky's Theorem}

	Eugen Slutsky\\
	\includegraphics[width=2cm]{EugenSlutsky}\\

	and Harald Cramér\\
	\includegraphics[width=2cm]{HaraldCramer}\\

	If:
	\begin{align}
	X \sim P\\
	c \in \rm I\!R\\
	X_n \xrightarrow[]{d} X\\
	Y_n \xrightarrow[]{d} c\\
	\end{align}
	then:
	\begin{align}
	X_n + Y_n \xrightarrow[]{d} X + c\\
	X_n * Y_n \xrightarrow[]{d} X * c\\
	\end{align}

	\chapter{Characteristic Functions}
	\section{Introduction}

	\chapter{Weak Law of Large Numbers}
	\section{Introduction}
	\section{Proof of the weak law of large numbers}
	\section{Proof using characteristic functions}

	\chapter{Central Limit Theorems}
	\section{Introduction}
	\section{Proof}
	\section{The Characteristic Function of a Normal Random Variable}

	\chapter{Applications}
	\section{Two-sided, Two-sample Tests}

	In the scenario:
	\begin{align}
		X_1,...,X_n \sim P_1\\
		E[X_i] = \mu_1\\
		VAR[X_i] = \sigma^2_1\\
		\\
		Y_1,...,Y_n \sim P_2\\
		E[Y_i] = \mu_2\\
		VAR[Y_i] = \sigma^2_2\\
	\end{align}
	the test statistic $T_n$.
	\begin{align}
		\sqrt{n}\frac{(\overline{X}_n - \overline{Y}_n)}{\sqrt{\sigma^2_1+\sigma^2_2}} \sim N_{\mu,\sigma^2}(0,1)
	\end{align}
	If both variances $\sigma^2_1$ and $\sigma^2_2$ are unknown we can use the "Plug-In" method using "Slutsky Theorem here".\\
	Given that $T_n$ converges to $N(0,1)$. We can use the fact that:
	\begin{align}
		\frac{\sqrt{\sigma^2_1+\sigma^2_2}}{\sqrt{\hat{\sigma}^2_1+\hat{\sigma}^2_2}} \xrightarrow[]{d} 1
	\end{align}
	and
	\begin{align}
	\sqrt{n}\frac{(\overline{X}_n - \overline{Y}_n)}{\sqrt{\sigma^2_1+\sigma^2_2}} * \frac{\sqrt{\sigma^2_1+\sigma^2_2}}{\sqrt{\hat{\sigma}^2_1+\hat{\sigma}^2_2}} \sim N_{\mu,\sigma^2}(0,1) * 1\\
	\sqrt{n}\frac{(\overline{X}_n - \overline{Y}_n)}{\sqrt{\hat{\sigma}^2_1+\hat{\sigma}^2_2}} * \frac{\sqrt{\sigma^2_1+\sigma^2_2}}{\sqrt{\sigma^2_1+\sigma^2_2}} \sim N_{\mu,\sigma^2}(0,1) * 1\\
	\sqrt{n}\frac{(\overline{X}_n - \overline{Y}_n)}{\sqrt{\hat{\sigma}^2_1+\hat{\sigma}^2_2}} * 1 \sim N_{\mu,\sigma^2}(0,1) * 1\\
	\sqrt{n}\frac{(\overline{X}_n - \overline{Y}_n)}{\sqrt{\hat{\sigma}^2_1+\hat{\sigma}^2_2}} \sim N_{\mu,\sigma^2}(0,1)
	\end{align}
\end{document}


% !TEX root = atlas_iros_16.tex
\subsection{Rigid Body and Friction Identification}
\label{sec:advance_ident}
%

%
In our previous work \cite{SpVdTh14} both, rigid body model and friction were simultaneously identified as the parameter vector $\bm{\beta}_{\mathrm{I}}$.
The results still contained a relevant error regarding model fitting and plausibility of the friction parameters compared to single-joint experiments.
Therefore, in this work a sequential procedure was applied, where pre-identified Coulomb and viscous friction parameters $\bm{d}_\mathrm{v,p}$, $\bm{\mu}_\mathrm{C,p}$ were included into the identification procedure in order to reduce the parameter space from $59$ to $45$
unknowns\footnote{
The 70 dynamic parameters (10 per joint) are reduced to $n_\mathrm{b}=45$ in the minimal regressor form.
Additionally, the model has $2n_\mathrm{j}=14$ friction parameters (2 per joint) resulting in $n_\beta=59$ parameters overall, see \cite{SpVdTh14}.}.
\subsubsection{Identification Model and Approach}
%
For identification purposes the robot dynamics (\ref{eqn:invdyn}), including a suitable friction model, can be written in regressor form as
\begin{equation}
\bm{\tau}_{\mathrm{m}} = \bm{\Phi} \bm{\beta} - \bm{\tau}_{\mathrm{ext}},
\label{regressor}
\end{equation}
where the regressor matrix $\bm{\Phi}$ contains distinct base and friction parameter related columns $\bm{\Phi} = \begin{pmatrix} \bm{\Phi}_\mathrm{b} & \bm{\Phi}_\mathrm{f} \end{pmatrix}$.
The elements of the parameter vector $\bm{\beta} = \begin{pmatrix} \bm{\beta}_\mathrm{b} \ \bm{d}_\mathrm{v} \ \bm{\mu}_\mathrm{c} \end{pmatrix}^{\mathrm{T}}$ denote the base, viscous friction and Coulomb friction parameter vectors.
The regressor matrix of the friction model can be allocated as
\begin{equation}
\bm{\Phi}_\mathrm{f}(\dot{\bm{q}}) =
\begin{pmatrix}
\mathrm{diag}\{\dot{\bm{q}}\}&
\mathrm{diag}\{\mathrm{sgn}(\dot{\bm{q}})\}\\
\end{pmatrix}.
\label{friction_mdl}
\end{equation}
Assuming $\bm{\tau}_{\mathrm{ext}} = \bm{0}$ during identification procedure, the influence of (\ref{friction_mdl}) can be incorporated by subtracting (\ref{friction_mdl}) from both sides in (\ref{regressor}).
This leads to the friction-corrected motor torque
\begin{align}
\bm{\tau}_{\mathrm{m},\mathrm {f}} &=
\bm{\tau}_{\mathrm{m}} -  \bm{\Phi}_{\mathrm{f}} ( \dot{\bm{q}})(\bm{d}_{\mathrm{v}}, \bm{\mu}_{\mathrm{c}})^{\mathrm{T}}\\
&=
\bm{\Phi} \bm{\beta} - \bm{\Phi}_{\mathrm{f}} ( \dot{\bm{q}})(\bm{d}_{\mathrm{v}}, \bm{\mu}_{\mathrm{c}})^{\mathrm{T}}=\bm{\Phi}_\mathrm{b}\bm{\beta}_\mathrm{b}.
\end{align}
%TODO QUELLE
%The influence of friction to motor torque $\bm{\tau}_{\mathrm{m}}$ can now be corrected by $\bm{\tau}_{\mathrm{m},\mathrm {f}} = \bm{\tau}_{\mathrm{m}} - \bm{\Phi}_{\mathrm{f}} ( \dot{\bm{q}})(\bm{d}_{\mathrm{v}}, \bm{\mu}_{\mathrm{c}})^{\mathrm{T}}$ using prior knowledge of friction parameters $\bm{d}_\mathrm{v,p}$ and $\bm{\mu}_\mathrm{c,p}$.
Identifying the numerical values of $\bm{\beta}_{\mathrm{b}}$ is done using a Moore-Penrose pseudoinverse
%
\begin{equation}
\hat{\bm{\beta}}_{\mathrm{b}} = {\left( \bm{F}^{\mathrm{T}} \bm{\Sigma}^{-1} \bm{F} \right)}^{-1}  \bm{F}^{\mathrm{T}} \bm{\Sigma}^{-1}\bm{b}
\label{MoorePenrose}
\end{equation}
%
filled with experimentally gained optimized Fourier-based joint angle trajectories of duration $t_{\mathrm{f}}$ \cite{park2006fourier}.
The information matrix $\bm{F}$ and the measurement vector $\bm{b}$ are defined as
%
\begin{equation}
\bm{F} = \begin{pmatrix}
\bm{\Phi}_{\mathrm{b}}\left( \bm{q}(t_1),\dot{\bm{q}}(t_1),\ddot{\bm{q}}(t_1) \right) \\
\bm{\Phi}_{\mathrm{b}}\left( \bm{q}(t_2),\dot{\bm{q}}(t_2),\ddot{\bm{q}}(t_2) \right) \\
\vdots \\
\bm{\Phi}_{\mathrm{b}}(\bm{q}(t_{\mathrm{f}}),\dot{\bm{q}}(t_{\mathrm{f}}),\ddot{\bm{q}}(t_{\mathrm{f}}))\end{pmatrix},~~
\bm{b} = \begin{pmatrix}
\bm{\tau}_\mathrm{m,f}(t_1) \\
\bm{\tau}_\mathrm{m,f}(t_2) \\
\vdots \\
\bm{\tau}_\mathrm{m,f}(t_{\mathrm{f}})
\end{pmatrix},
\end{equation}
%
where $\bm{\tau}_\mathrm{m,f}(t_i)$ are friction-corrected torque measurements using prior knowledge of friction parameters $\bm{d}_\mathrm{v,p}$ and $\bm{\mu}_\mathrm{c,p}$.
The covariance matrix $\bm{\Sigma}$ is composed of actuator noise variances.
Torque measurements $\bm{\tau}_{\mathrm{m}}$ are determined based on chamber pressures for the hydraulic joints and electric currents for the electromechanic joints.
Gear ratios and motor constants are provided by the manufacturer.
The joint angle $\bm{q}$ is measured by position encoders.
The resulting parameter vector $\bm{\beta}_{\mathrm{II}}$ of the sequential method, which is used to parameterize (\ref{regressor}), consists of the elements $\bm{\beta}_{\mathrm{II}} = \begin{pmatrix} \bm{\beta}_\mathrm{b} \ \bm{d}_\mathrm{v,p} \ \bm{\mu}_\mathrm{c,p} \end{pmatrix}^{\mathrm{T}}$.

\subsubsection{Single Joint Friction Identification}

The identification of joint friction parameters $\bm{d}_{\mathrm{v,p}}$ and $\bm{\mu}_{\mathrm{c,p}}$ is done by running a set of different constant velocities $\dot{q}_i$ in positive and negative direction and measuring the resulting torque $\bm{\tau}_{\mathrm{m}}$ for every joint.
Mean velocity and torque are calculated using intervals of constant speed.
Figure~\ref{fig:ident_friction_char} depicts the joint friction characteristics for the hydraulic joints which show significant Coulomb and viscous friction.

% figure generated with MATLAB in
% drc_paper/Atlas_IROS_16/figures/Identification/FrictionCharacteristics_resultfigures_IROS.m
\begin{figure}
\centering
\includegraphics[]{./figures/Identification/FrictionCharacteristics_left}
\caption{Viscous and Coulomb friction for the hydraulic joints of the left arm. Each marker represents the mean value of one constant velocity single joint experiment, see Fig.~\ref{fig:velocity_tracking_friction}. Dynamics effects from $\bm{\Phi}_{\mathrm{b}}$ were removed and calculated with dynamics parameters $\hat{\bm{\beta}}_{\mathrm{b,I}}$ from the combined identification approach \cite{SpVdTh14} and with assumed fixed and upright upper body orientation.}
\label{fig:ident_friction_char}
\SkipBeforePicture
\end{figure}

The identification results could be further improved by using the impedance controller to execute the identification trajectory, as it shows significantly improved velocity tracking compared to an extensively tuned PD controller implemented by the manufacturer, see Fig.~\ref{fig:velocity_tracking_friction}.
The identified friction parameters are given in Table~\ref{tab:errors_tracking_left}.

%
% figure generated with MATLAB in
%./figures/Identification/ConstVel_AssemblyFigure_middle_plot.m
\begin{figure}
\centering
\includegraphics[width=\linewidth]{./figures/Identification/SI_E036_Joint4_ConstVel_Summary_medium_speed}
\caption{Single joint friction experiment exemplified for joint 4 reveals improved velocity tracking of the impedance controller compared to PD position control with controller gains from \cite{2014:JFR-ViGIR-DRC-Trials, ConnerKohRomStu2015}.}
\label{fig:velocity_tracking_friction}
\SkipBeforeText
\end{figure}

% MSE-Werte werden auch in atlas5_plot_torque_ident_MPV_left_DRC_IROS.m ausgegeben.
%
\setlength\tabcolsep{5pt}
\begin{table}
   \caption{Mean square errors (MSE) for arm identification using different base parameter vectors $\hat{\bm{\beta}}_{\mathrm{I}}$, $\hat{\bm{\beta}}_{\mathrm{II}}$ and data from Fig.~\ref{fig:ident_torque_compare} and identified friction parameters from single-joint experiments}
  \begin{center}
   \begin{tabular}{c|c|c|c|c}
joint  & $\mathrm{MSE}(\bm{\tau}_{\mathrm{m}}-\bm{\tau}_{\mathrm{I}})$   & $\mathrm{MSE}(\bm{\tau}_{\mathrm{m}}-\bm{\tau}_{\mathrm{II}})$   & $\hat{{\mu}}_{\mathrm{C,p},i}$ & $\hat{{d}}_{\mathrm{v,p},i}$ \\
 & [(Nm)$^2$] & [(Nm)$^2$] & [Nm] & [Nms/rad] \\
\hline
1 (shz) &  97.04  & 13.03 & 2.0 & 1.3 \\
2 (shx) &  52.64  & 20.78 & 6.7 & 0.9 \\
3 (ely) &  22.58  & 16.62 & 10.3 & 1.6 \\
4 (elx) &  25.22  & 18.66 & 6.1 & 2.7 \\
\hline
5 (wry) &  2.82 & 6.50 & 0.1 & 0.5 \\
6 (wrx) &  8.11 & 3.46 & 0.1 & 0.2 \\
7 (wry2) &  0.26 & 18.28 & 3.1 & 0.2 \\
   \end{tabular}
  \end{center}
\label{tab:errors_tracking_left}
\SkipBeforePicture
\end{table}
\setlength\tabcolsep{6pt}

\subsubsection{Results of the Sequential Identification}

A comparison between the base parameter vector $\hat{\bm{\beta}}_{\mathrm{I}} = \begin{pmatrix} \hat{\bm{\beta}}_\mathrm{b,I} \ \hat{\bm{d}}_\mathrm{v} \ \hat{\bm{\mu}}_\mathrm{C} \end{pmatrix}^{\mathrm{T}}$, where friction was identified as part of the combined least squares optimization \cite{SpVdTh14}, and the base parameter vector $\hat{\bm{\beta}}_{\mathrm{II}}$ of the sequential method can be found in Fig.~\ref{fig:ident_torque_compare}.
Good model consistency is indicated by low distance between model and measurement.
To avoid the problem of overfitting, the trajectory of this experiment was different from the one used for identification.
When using the parameter vector $\hat{\bm{\beta}}_{\mathrm{II}}$, significantly improved results could be achieved in the hydraulic joints, indicated by the lower mean square error between measured and modeled torques in Table~\ref{tab:errors_tracking_left}.

% Bildquelle: atlas5_plot_torque_ident_MPV_left_DRC_IROS.m
\begin{figure}
\centering
\includegraphics{./figures/Identification/atlas5_plot_torque_ident_MPV_left_IROS}
\caption{Measured ($\bm{\tau}_\mathrm{m}$) and simulated torques of the left arm joints comparing the sequential method $\bm{\tau}_{\mathrm{\mathrm{II}}}$ with the combined method $\bm{\tau}_{\mathrm{\mathrm{I}}}$.}
\label{fig:ident_torque_compare}
\SkipBeforeText
\end{figure}

As already mentioned in \cite{SpVdTh14}, the electromechanic wrist joints (wry, wrx, wry2) do not seem to be identifiable for the Atlas system without working joint torque sensors.
Although friction was identified in single axis experiments, the predicted
torques have essentially no correlation with the measured torques.
Presumably, this is due to the current-based torque measurement on actuator side, which decreases the quality of the measured information significantly.

In the next section, the results for the experimental collision handling performance with the Atlas system are outlined.


 \documentclass [12pt]{article}

\usepackage {amsmath}
\usepackage {amsthm}
\usepackage {amssymb}
\usepackage {graphicx}
\usepackage {float}
\usepackage {multirow}
\usepackage {xcolor}
\usepackage {algorithmic}
\usepackage [ruled,vlined,commentsnumbered,titlenotnumbered]{algorithm2e} \usepackage {array}
\usepackage {booktabs}
\usepackage {url}
\usepackage {parskip}
\usepackage [margin=1in]{geometry}
\usepackage [T1]{fontenc}
\usepackage {cmbright}
\usepackage [many]{tcolorbox}
\usepackage [colorlinks = true,
            linkcolor = blue,
            urlcolor  = blue,
            citecolor = blue,
            anchorcolor = blue]{hyperref}
\usepackage {enumitem}
\usepackage {xparse}
\usepackage {verbatim}
\usepackage{listings}
\usepackage{xcolor}
\lstset { %
    language=C++,
    backgroundcolor=\color{black!5}, % set backgroundcolor
    basicstyle=\footnotesize,% basic font setting
}
\newtheorem{theorem}{Theorem}
\newtheorem{remark}{Remark}

\DeclareTColorBox {Solution}{}{breakable, title={Solution}} \DeclareTColorBox {Solution*}{}{breakable, title={Solution (provided)}} \DeclareTColorBox {Instruction}{}{boxrule=0pt, boxsep=0pt, left=0.5em, right=0.5em, top=0.5em, bottom=0.5em, arc=0pt, toprule=1pt, bottomrule=1pt} \DeclareDocumentCommand {\Expecting }{+m}{\textbf {[We are expecting:} #1\textbf {]}} \DeclareDocumentCommand {\Points }{m}{\textbf {(#1 pt.)}}

\begin {document}

\vspace {1em}
\begin {Instruction}
Adapted From Virginia Williams' lecture notes.
\end {Instruction}

{\LARGE \textbf {COMP 285 (NC A\&T, Spr `22)}\hfill \textbf {Lecture 8} }

\begin{centering}
\section*{Median of Medians}
\end{centering}

\section{Introduction}
We ended last lecture with a few ideas on how we could solve the kSelect problem. The only difference between the ideas involved how we chose the `pivot'! In this lecture, we'll cover a deterministic mechanism for chosing a pivot. As a refresher, here is the pseudo-code for the divide-and-conquer approach to the kSelect problem.

\begin{algorithm}
\caption{Select(A, n, k)}\label{alg:select}
\begin{algorithmic}
\IF {$n=1$}
    \RETURN $A[1]$
\ENDIF
\STATE $p \gets \texttt{ChoosePivot}(A, n)$
\STATE $A_< \gets \{A[i] \mid A[i] < p \}$
\STATE $A_> \gets \{A[i] \mid A[i] > p \}$
\IF {$|A_<| = k - 1$}
    \RETURN $p$
\ELSIF {$|A_<| > k - 1$}
    \RETURN \texttt{Select}$(A_<, |A_<|, k)$
\ELSIF {$|A_<| < k - 1$}
    \RETURN \texttt{Select}$(A_>, |A_>|, k - |A_<| - 1)$
\ENDIF
\end{algorithmic}
\end{algorithm}

\section{Choose a pivot ``close enough'' to the median}
Given a linear-time median algorithm, we can solve the selection problem in linear time (and vice versa). Although ideally we would want to find the median, notice that as far as correctness goes, there was nothing special about partitioning around the median. We could use this same idea of partitioning and recursing on a smaller problem even if we partition around an arbitrary element. To get a good runtime, however, we need to guarantee that the subproblems get smaller quickly. In 1973, Blum, Floyd, Pratt, Rivest, and Tarjan came up with the Median of Medians algorithm. It is similar to the algorithm we covered in the last lecture, but rather than partitioning around the exact median, uses a surrogate ``median of medians''. We update \texttt{ChoosePivot}accordingly.

\begin{algorithm}
\caption{ChoosePivot(A, n)}\label{alg:choose_pivot}
\begin{algorithmic}
\STATE Split $A$ into $g = \lceil n / 5 \rceil$ groups $p_1, \cdots, p_g$
\FOR {$i=1$ to $g$}
    \STATE $p_i \gets \texttt{MergeSort}(p_i)$
\ENDFOR
\STATE $C \gets \{ \text{median of } p_i \mid i = 1, \cdots, g \}$
\STATE $g \gets \texttt{Select}(C, g, g/2)$
\RETURN p
\end{algorithmic}
\end{algorithm}

What is this algorithm doing? First it divides $A$ into segments of size $5$. Within each group, it finds the median by first sorting the elements with \texttt{MergeSort}. Recall that \texttt{MergeSort} sorts in $O(n \log n)$ time. However, since each group has a constant number of elements, it takes constant time to sort. Then it makes a recursive call to \texttt{Select} to find the median of $C$, the median of medians. Intuitively, by partitioning around this value, we are able to find something that is close to the true median for partitioning, yet is `easier' to compute, because it is the median of $g = \lceil n/5 \rceil$ elements rather than $n$. The last part is as before: once we have our pivot element $p$, we split the array and recurse on the proper subproblem, or halt if we found our answer.

We have devised a slightly complicated method to determine which element to partition
around, but the algorithm remains correct for the same reasons as before. So what is its running time? As before, we're going to show this by examining the size of the recursive subproblems. As it turns out, by taking the median of medians approach, we have a guarantee on how much smaller the problem gets each iteration. The guarantee is good enough to achieve $O(n)$ runtime.

\subsection{Running Time}

\textbf{Lemma.} $|A_<| \leq 7n/10 + 5$ and $|A_>| \leq 7n/10 + 5$.

\begin{proof}
$p$ is the median of $p_1, \cdots , p_g$. Because $p$ is the median of $g = \lceil n/5 \rceil$ elements, the medians of $\lceil g/2 \rceil-1$ groups $p_i$ are smaller than $p$. If $p$ is larger than a group median, it is larger than at least three elements in that group (the median and the smaller two numbers). This
applies to all groups except the remainder group, which might have fewer than 5 elements. Accounting for the remainder group, $p$ is greater than at least $3 \cdot (\lceil g/2 \rceil - 2)$ elements of $A$.
By symmetry, $p$ is less than at least the same number of elements.

Now,
\begin{align*}
|A_>| &= \# \text{ of elements greater than } p \\
\leq (n-1) - 3 \cdots (\lceil g / 2 \rceil - 2) \\
= n + 5 - 3 \cdots \lceil g / 2 \rceil \\
\leq n - 3n/10 + 5 \\
\leq 7n/10 + 5
\end{align*}

By symmetry, $|A_<| \leq 7n/10 + 5$ as well.

Intuitively, we know that 60\% of half of the groups are less than the pivot, which is 30\% of the total number of elements, $n$. Therefore, at most 70\% of the elements are greater than the pivot. Hence, $|A_>| \approx 7n/10$. We can make the same argument for $|A_<|$.
\end{proof}

The recursive call used to find the median of medians has input of size $\lceil n/5 \rceil \leq n/5 + 1$. The other work in the algorithm takes linear time: constant time on each of $\lceil n/5 \rceil$ groups for \texttt{MergeSort} (linear time total for that part), $O(n)$ time scanning $A$ to make $A_<$ and $A_>$.

Thus, we can write the full recurrence for the runtime,

$$
T(n) \leq \begin{cases}
    c_1n + T(n/5 + 1) + T(7n/10 + 5) & \text{if } n > 5 \\
    c_2 & \text{if } n \leq 5
\end{cases}
$$

How do we prove that $T(n) = O(n)$? The master theorem does not apply here. Instead, we will prove this using the substitution method.

\subsection{Solving the Recurrence of Select Using the Substitution Method}

For simplicity, we consider the recurrence $T(n) \leq T(n/5) + T(7n/10) + cn $instead of the exact recurrence of \texttt{Select}.

To prove that $T(n) = O(n)$, we guess:

$$
T(n) \leq \begin{cases}
    d\cdot n_0 & \text{if } n = n_0 \\
    d \cdot n & \text{if } n > n_0
\end{cases}
$$

For the base case, we pick $n_0 = 1$ and use the standard assumption that $T(1) = 1 \leq d$. For the inductive hypothesis, we assume that our guess is correct for any $n < k$, and we prove our guess for $k$. That is, consider $d$ such that for all $n_0 \leq n < k, T(n) \leq dn$.

To prove for $n = k$, we solve the following equation:
\begin{align*}
T(k) &\leq T(k/5) + T(7k/10) + ck \\
&\leq  dk/5 + 7dk/10 + ck \\
\implies 9/10d + c &\leq dk \\
\implies c &\leq d/10 \\
\implies d &\geq 10c
\end{align*}
Therefore, we can choose $d = \max(1, 10c)$, which is a constant factor. The induction is completed. By the definition of big-Oh, the recurrence runs in $O(n)$ time.

\subsection{Isssues When Using the Substitution Method}

Now we will try out an example where our guess is incorrect. Consider the recurrence $T(n) = 2T(n/2) + n$ (similar to \texttt{MergeSort}). We will guess that the algorithm is linear
$$
T(n) \leq \begin{cases}
    d\cdot n_0 & \text{if } n = n_0 \\
    d \cdot n & \text{if } n > n_0
\end{cases}
$$
We try the inductive step. We try to pick some $d$ such that for all $n \geq n_0$,
\begin{align*}
n + \Sigma_{i=1}^{k} dg(n_i) &\leq d \cdot g(n) \\
n + 2\cdot d \cdot \frac{n}{2} &\leq dn \\
n(1 + d) \leq dn \\
n + dn \leq dn \\
n < 0
\end{align*}
However, the above can never be true, and there is no choice of d that works! Thus our guess was incorrect.

This time the guess was incorrect since \texttt{MergeSort} takes superlinear time. Sometimes, however, the guess can be asymptotically correct but the induction might not work out. Consider for instance $T(n) \leq 2T(n/2) + 1$.

We know that the runtime is $O(n)$ so let's try to prove it with the substitution method. Let's guess that $T(n) \leq cn$ for all $n \geq n_0$.

First we do the induction step: We assume that $T(n/2) \leq cn/2$ and consider $T(n)$. We
want that $2 \cdot cn/2 + 1 \leq cn$, that is, $cn + 1 \leq cn$. However, this is impossible.

This doesn't mean that $T(n)$ is not $O(n)$, but in this case we chose the wrong linear function. We could guess instead that $T(n) \leq cn-1$. Now for the induction we get $2\cdot(cn/2-1)+1 = cn - 1$ which is true for all $c$. We can then choose the base case $T(1) = 1$.

\subsection{Correctness of the Algorithm}
Recall that the choice of pivot only affects the runtime, and not the correctness of the algorithm. Here, we prove formally, by induction, that \texttt{Select} is correct. We will use strong induction. That is, our inductive step will assume that the inductive hypothesis holds for all $n$ between $1$ and $i -1$, and then we’ll show that it holds for $n = i$.

\textit{Remark.} You can also do this using regular induction with a slightly more complicated inductive hypothesis; either way is fine.

\textbf{Inductive Hypothesis (for $n$)}. When run on an array $A$ of size $n$ and an integer $k \in \{1, \cdots , n\}$, Select returns the $k$-th smallest element of A.

\textbf{Base Case ($n = 1$)}. When $n = 1$, the requirement $k \in \{1, \cdots , n\}$ means that $k = 1$; that is, \texttt{Select}$(A, k)$ is supposed to return the smallest element of A. This is precisely what the pseudocode above does when $|A| = 1$, so this establishes the Inductive Hypothesis for $n = 1$.

\textbf{Inductive Step.} Let $i \geq 2$, and suppose that the inductive hypothesis holds for all $n$ with $1 \leq n < i$. Our goal is to show that it holds for $n = i$. That is, we would like to show that

    \textit{When run on an array $A$ of size $i$ and an integer $k \in \{1, \cdots, i\}$, Select$(A, k)$ returns the $k$-th smallest element of $A$}.

Informally, we want to show that assuming that \texttt{Select} ``works'' on smaller arrays, then it ``works'' on an array of length n.

We do this below:

Suppose that $1 \leq k \leq i$, and that $A$ is an array of length $i$. There are three cases to consider,
depending on $p = \texttt{ChoosePivot}(A, i)$. Notice that in the pseudocode above, $p$ is a value from A, not an index. Let $A_<, A_>, p$ be as in the pseudocode above.

\begin{itemize}
\item \textbf{Case 1.} Suppose that $|A_<| = k -1$. Then by the definition of $A_<$, there are $k -1$ elements of $A$ that are smaller than $p$, so $p$ must be the $k$-th smallest. In this case, we return $p$, which is indeed the $k$-th smallest.
\item \textbf{Case 2.} Suppose that $|A_<| > k -1$. Then there are more than $k -1$ elements of $A$ that are smaller than $p$, and so in particular the $k$-th smallest element of $A$ is the same as the $k$-th smallest element of $L$. Next we will use the inductive hypothesis for $n = |A_<|,$ which holds since $|A_<| < i$. Since $1 \leq k \leq |A_<|,$ the inductive hypothesis implies that \texttt{Select}($A_<, k$) returns the $k$-th smallest element of $A_<$. Thus, by returning this we are also returning the $k$-th smallest element of $A$, as desired.
\item \textbf{Case 3.} Suppose that $|A_<| $< $k -1$. Then there are fewer than $k -1$ elements that are less than $p$, which means that the $k$-th smallest element of $A$ must be greater than $p$; that is, it shows up in $A_>$. Now, the $k$-th smallest element in $A$ is the same as the $(k -|A_<| -1)$-st element in $A_>$. To see this, notice that there are $|A_<| + 1$ elements smaller than the $k$-th that do not show up in $A_>$. Thus there are $k -(|A_<| + 1) = k -|A_<| -1$ elements in $A_>$ that are smaller than or equal to the $k$-th element. Now we want to apply the inductive hypothesis for $n = |A_>|$, which we can do since $|A_>| < i$. Notice that we have $1 \leq k -|A_<| -1 \leq |A_>|$; the first inequality holds because $k > |A_<| + 1$ by the definition of Case 3, and the second inequality holds because it is the same as $k \leq |A_<| + |A_>| + 1 = n$, which is true by assumption. Thus, the inductive hypothesis implies that \texttt{Select}($A_>, k -|A_<| -1$) returns the $(k -|A_<| -1)$-st element of $A_>$. Thus, by returning this we are also returning the $k$-th smallest element of A, as desired.

Thus, in each of the three cases, \texttt{Select}($A, k$) returns the $k$-th smallest element of $A$. This establishes the inductive hypothesis for $n = i$.
\end{itemize}

\textbf{Conclusion.} By induction, the inductive hypothesis holds for all $n \geq 1$. Thus, we conclude that Select($A, k$) returns the $k$-th smallest element of A on any array $A$, provided that $k \in \{1, \cdots , |A|\}$. That is, \texttt{Select} is correct, which is what we wanted to show.

\end{document}


%!TEX root = ../notes.tex
\section{April 7, 2022}
\subsection{Quadratic Fields \emph{continued}}

\begin{definition*}
    A quadratic field is a number field $K$ of degree $2$ over $\QQ$.

    Thus $K = \QQ(\theta)$ for $\theta$ a zero of some
    \[x^2 + ax + b\]
    for $a, b\in\ZZ$.
\end{definition*}
Hence,
\[\theta = \frac{-a\pm \sqrt{a^2 - 4b}}{2}.\]
Thus from which it follows
\begin{proposition*}
    The quadratic fields are of the form $\QQ(\sqrt{d})$ where $d\in\ZZ$ is squarefree.
\end{proposition*}

\begin{theorem*}[p.64 of \cite{stewart2015algebraic}]
    Let $d\in\ZZ$ be a squarefree integer and let $K = \QQ(\sqrt{d})$. Then $\riO_K$ equals
    \begin{enumerate}[a)]
        \item $\ZZ\left[\sqrt{d}\right]$ if $d\not\equiv 1\pmod{4}$.
        \item $\ZZ\left[\frac{1+\sqrt{d}}{2}\right]$ if $d\equiv 1\pmod{4}$.
    \end{enumerate}
\end{theorem*}
\begin{proof}
    Every $\alpha\in\QQ(\sqrt{d})$ is of the form
    \[\alpha = \frac{a\pm b\sqrt{d}}{c}\]
    with $a, b, c\in\ZZ$ and $\gcd(a, b, c) = 1$. Now $\alpha\in\riO_K$ iff the coefficients of
    \[\left( x - \frac{a + b\sqrt{d}}{c} \right)\left( x - \frac{a - b\sqrt{d}}{c} \right)\]
    are in $\ZZ$. This holds iff
    \[\frac{a^2 - b^2d}{c^2}\in\ZZ\qquad\text{and}\qquad \frac{2a}{c}\in\ZZ\]

    If $(a, c)\neq 1$, then in our first expression, the fact that $d$ is squarefree forces our common factor must also be shared with $b$. So $\gcd(a, b, c)\neq 1$. So $(a, c) = 1$. Looking at our second expression, $c$ is forced to be $1$ or $2$.

    If $c = 1$, then $\alpha\in\riO_K$ anyway, so assume that $c = 2$. We have that $\gcd(b, c) = 1$ by the same reasoning as before, so $c=2$ implies that $a$ and $b$ are both odd.

    Moreover, $\alpha\in\riO_K$ with these assumptions iff
    \[\frac{a^2 - b^2d}{c^2} = \frac{a^2 - b^2d}{4}\in\ZZ\]
    which happens iff $a^2 - b^2d\equiv 0\pmod{4}$. Then $a, b$ odd implies $a^2 \equiv b^2\equiv 1\pmod{4}$ so we get that this is equivalent to $d\equiv 1\pmod{4}$.

    Thus $c = 2$ and $\alpha\in\riO_K$ implies that $d\equiv 1\pmod{4}$.

    In sum, if $d\not\equiv 1\pmod{4}$, then $c = 1$, so we've shown that $\riO_K = \ZZ[\sqrt{d}]$. If $d\equiv 1\pmod{4}$, then we can have $c = 2$ and $a, b$ odd. Hence $\riO_K = \ZZ\left[ \frac{1+\sqrt{d}}{2} \right]$.
\end{proof}

\begin{theorem}[p.65 \cite{stewart2015algebraic}]
    We then have the following:
    \begin{enumerate}[a)]
        \item If $d\not\equiv 1\pmod{4}$, then $\{1, \sqrt{d}\}$ is an integral basis. If $d\equiv 1\pmod{4}$, then $\left\{1, \frac{1+\sqrt{d}}{2}\right\}$ is an integral basis.
        \item If $d\not\equiv 1\pmod{4}$, then $\disc(K) = 4d$. If $d\equiv 1\pmod{4}$, then $\disc{K} = d$.
    \end{enumerate}
\end{theorem}

\subsection{Cyclotomic Extensions}
\begin{definition}
    A \ul{cyclotomic field/extension} is a number field of the form
    \[K = \QQ(\zeta_n), \quad \zeta_n = e^{2\pi i / n}.\]
    That is, $\zeta_n$ is the primitive $n$-th root of unity. We could just as easily take $\zeta_n = e^{2\pi i k / n}$ where $\gcd(k, n) = 1$.
\end{definition}
\begin{example}
    $n = 1$ is boring. $n = 2$ is boring. $n = 2$ gives a quadratic field.
\end{example}
\begin{example}
    $K = \QQ(i)$ for $n=4$, $\zeta_4 = i$.

    $K = \QQ(\sqrt{-3}) = \QQ\left(\frac{1 + \sqrt{-3}}{2}\right) = \QQ(\zeta_3)$.
\end{example}

We note:
\begin{itemize}
    \item
          Any embedding of $\QQ(\zeta_n)\hookrightarrow \CC$ has image contained in $\QQ(\zeta_n)$. (In other words, these extensions are Galois over $\QQ$.)
    \item
          We care about extensions of the form $\QQ(\sqrt[n]{a})$ where $a\in\QQ$. But these are not Galois in general.

          The solution is to ``repair'' the base field. Take $K = \QQ(\zeta_n)$ and $L = K(\sqrt[n]{a})$. Then $L/K$ is Galois. This is to say that the embeddings $L\hookrightarrow \CC$ that fix $K$ stabilize $L$ (send $L$ to itself).
    \item \emph{Kronecker-Weber theorem} that every finite Abelian extension of $\QQ$ is contained in some cyclotomic extension.
\end{itemize}

We have some \ul{key facts about cyclotomic extensions}:
\begin{enumerate}[1)]
    \item $[\QQ(\zeta_n) : \QQ] = \phi(n)$.
    \item The field automorphisms $\sigma: \QQ(\zeta_n)\to \QQ(\zeta_n)$ form a cyclic group under composition, of order $\phi(n)$. (Our automorphisms send $\zeta_n$ to some other primitive $n$-th root of unity $\zeta_n'$.)

          From now on, let $K = \QQ(\zeta_n)$.
    \item Then $\riO_K = \ZZ[\zeta_n]$. The case where $n = p$ is in the textbook.
    \item We have
          \[\disc(K) = (-1)^{\phi(n)/2}\frac{n^{\phi(n)}}{\displaystyle\prod_{\substack{p\mid n \\ p\text{ prime}}} p^{\phi(n)/(p-1)} }\]

          For $n = p$ prime, we get
          \begin{align*}
              \disc(\QQ(\zeta_p)) & = (-1)^{(p-1)/2}\cdot \frac{p^{p-1}}{p} \\
                                  & = (-1)^{(p-1)/2}\cdot p^{p-2}
          \end{align*}
          In particular, if $p\in\ZZ$ is a prime with $p\nmid n$, then $p\nmid \disc(K)$.
\end{enumerate}

\subsection{Prime Factorization in Number Fields}
Useful to note that this is section 5.1 in \cite{stewart2015algebraic}.

\recall the examples of non-UFDs given previously in Math 1530.
\begin{example}
    In $\ZZ[\sqrt{-5}]$, we have $6 = 2\cdot 3 = (1 + \sqrt{-5})(1 - \sqrt{-5})$. And we check that each term here is irreducible.

    (2, for example, is not an associate of $1+\sqrt{-5}$ or $(1 - \sqrt{-5})$. Simply reason by norms.)
\end{example}

\begin{example}\label{example:q-sqrt-15}
    What about $\QQ(\sqrt{15})$?
    \[2\cdot 5 = (5 + \sqrt{15})(5 - \sqrt{15})\]
    in $\ZZ[\sqrt{15}]$.
\end{example}
\begin{example}
    In $\QQ(\sqrt{30})$,
    \[2\cdot 3 = (6 + \sqrt{30})(6 - \sqrt{30})\]

    In $\QQ(\sqrt{-10})$,
    \[2\cdot 7 = (2 + \sqrt{-10})(2 - \sqrt{-10})\]
\end{example}
\begin{ques*}
    What's going wrong?
\end{ques*}
In \cref{example:q-sqrt-15}, we notice
\begin{align*}
    5 + \sqrt{15} = \sqrt{5}(\sqrt{5} + \sqrt{3}) \\
    5 - \sqrt{15} = \sqrt{5}(\sqrt{5} - \sqrt{3})
\end{align*}
Multiplying these together, we get
\[25 - 15 = 10 = 5\cdot (\sqrt{5} + \sqrt{3})\cdot (\sqrt{5} - \sqrt{3})\]
so the factors in
\[\sqrt{5}\qquad \sqrt{5} + \sqrt{3} \qquad \sqrt{5} - \sqrt{3}\]
are being grouped in $2$ ways:
\[(a_1^2)(a_2a_3) = (a_1a_2)(a_1a_3)\]
In other words, the problem goes away in $\riO_L$ for $L = \QQ(\sqrt{15}, \sqrt{5}) = \QQ(\sqrt{3}, \sqrt{5})$ (we extend to get some other things in it).

We can check that the same thing underlies the other two examples.

\begin{theorem}[Principal Ideal Theorem]
    Let $K$ be a number field. Then there is a finite extension $L/K$ such that every nonzero $\alpha\in\riO_K$ has a unique factorization into irreducibles in $\riO_L$.
\end{theorem}

\textbf{Caution!} This does \emph{not} say that $\riO_L$ is a UFD. So it is not true that every number field $K$ has a finite extension $L/K$ such that $\riO_L$ is a UFD.


% !TEX options=--shell-escape
\documentclass [12pt]{article}
\usepackage {amsmath}
\usepackage {amsthm}
\usepackage {amssymb}
\usepackage {graphicx}
\usepackage {float}
\usepackage {multirow}
\usepackage {xcolor}
\usepackage {algorithmic}
\usepackage [ruled,vlined,commentsnumbered,titlenotnumbered]{algorithm2e} \usepackage {array}
\usepackage {booktabs}
\usepackage {url}
\usepackage {parskip}
\usepackage [margin=1in]{geometry}
\usepackage [T1]{fontenc}
\usepackage {cmbright}
\usepackage [many]{tcolorbox}
\usepackage [colorlinks = true,
            linkcolor = blue,
            urlcolor  = blue,
            citecolor = blue,
            anchorcolor = blue]{hyperref}
\usepackage {enumitem}
\usepackage {xparse}
\usepackage {verbatim}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{csquotes}
\usepackage[cache=false]{minted}
\usepackage{mdframed}
\usepackage{tikz}
\usetikzlibrary{shapes.symbols}
\newtheorem{theorem}{Theorem}

\DeclareTColorBox {Solution}{}{breakable, title={Solution}}
\DeclareTColorBox {Solution*}{}{breakable, title={Solution (provided)}}
\DeclareTColorBox {Instruction}{}{boxrule=0pt, boxsep=0pt, left=0.5em, right=0.5em, top=0.5em, bottom=0.5em, arc=0pt, toprule=1pt, bottomrule=1pt}
\DeclareDocumentCommand {\Expecting }{+m}{\textbf {[We are expecting:} #1\textbf {]}}
\DeclareDocumentCommand {\Points }{m}{\textbf {(#1 pt.)}}
\newcommand {\hint }[1]{\noindent {[\textbf {HINT:} \em #1 \em ]}} \newcommand {\pts }[1]{\textbf {(#1 pt.)}}

\begin{document}

{\LARGE \textbf {COMP 285 (NC A\&T, Spr `22)}\hfill \textbf {Weekly Quiz 5} }

\begin{Instruction}

\paragraph{Reporting Issues} If you find any issues with the solutions, reach out to Chi Wang (author) or Luis Perez (reviewer).

\end{Instruction}

\section{} In order to conclude an expected runtime of $O(1)$ for hash table operations, we assumed the followingtwo happen in some specific order:
\begin{enumerate}
    \item The adversary picks elements $x_1, \cdots , x_n$ for the hash table
    \item The algorithm picks a hash function from the hash family.
\end{enumerate}

\begin{Solution}
(1) Adversary first, then (2) algorithm.
\paragraph{}

Bad guys may lower our running time by picking the worst-case element if they know the hash function algorithm before picking elements.
\end{Solution}

\section{} Math review: What is $285 \mod 5$?

\begin{Solution}
0
\end{Solution}

\section{} Math review: What is the meaning of the ``$a \mod b$'' operation?

\begin{Solution}
You divide $a$ by $b$ and take the remainder.
\end{Solution}

\section{} Suppose that we have a universe of size $M$, and our hash table size is $n$. If $n \geq M$, what is the minimum size of a universal hash family?

\begin{Solution}
1
\paragraph{}
Since our hash table is bigger than our universe, a single hash function will be sufficient for the family to be universal.
\end{Solution}

\section{} You start your DFS algoritmh at node $0$, and assume that the vertices' numbers are used as tie breakers (you visit vertices with smaller numbers first). In what order do you enter the vertices.
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.5]{1.png}
    \label{fig:my_label}
\end{figure}

\begin{Solution}
0, 2, 3, 4, 1, 5
\end{Solution}

\section{} You start your DFS algoritmh at node 0, and assume that the vertices' numbers are used as tie breakers (you visit vertices with larger numbers first). In what order do you enter the vertices.
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.5]{1.png}
    \label{fig:my_label}
\end{figure}

\begin{Solution}
0, 5, 4, 2, 3, 1
\end{Solution}

\section{} What is the degree of vertex 4?
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.5]{1.png}
    \label{fig:my_label}
\end{figure}

\begin{Solution}
3
\paragraph{}
There are 3 edges coming out from the vertex 4.
\end{Solution}

\section{} Two options for representing graphs are the (1) adjacency list representations and the (2) matrix representation. For edge (v, w), what is the running time of checking if it exists in the matrix representation, where $n$ is the number of nodes in the graph? There may be multiple correct answers. Select 1.

\begin{Solution}
$O(1)$
\paragraph{}
Simply check $matrix[v][w]$, 1 or 0 means there exists such edge or not.
\end{Solution}

\section{} Two options for representing graphs are the (1) adjacency list representations and the (2) matrix representation. For edge (v, w), what is the running time of checking if it exists in the adjacency list representation, where n is the number of nodes in the graph? There may be multiple correct answers. Select 1.

\begin{Solution}
$O(\text{degree}(v)) / O(\text{degree}(w))$
\paragraph{}
To find edge(v,w), we may need to iterate the whole list whose size is $\text{degree}(v)/\text{degree}(w)$.
\end{Solution}

\section{} Two options for representing graphs are the (1) adjacency list representations and the (2) matrix representation. How much space is required for each, where n is the number of nodes and m is the number of edges.

\begin{Solution}
Adjacency: $O(n+m)$, Matrix: $O(n^2)$
\end{Solution}

\end{document}


\documentclass{report}[11pt]

\usepackage{amsmath} % provides numberwithin (and lots more)
\usepackage{graphicx}
\usepackage[backend=bibtex]{biblatex}
\usepackage{listings}
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
  language=MATLAB,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

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

\newcommand{\ds}{\displaystyle}

\begin{document}

\begin{titlepage}
\begin{center}
 {\huge\bfseries MATLAB\\ Patterns and Practices\\}
 % ----------------------------------------------------------------
 \vspace{1.5cm}
 {\bfseries Pete Benson}\\[5pt]
% pbenson@umich.edu\\[14pt]
  % ----------------------------------------------------------------
 \vspace{10cm}
 % ----------------------------------------------------------------
\includegraphics{QFRM_rgb}\\[5pt]
{Department of Mathematics}\\[5pt]
{530 Church Street}\\[5pt]
{Ann Arbor, MI 48109-1043,
 USA}\\
 \vfill

\end{center}
\end{titlepage}

%----------------------
% review
%----------------------
\chapter{Patterns}

Each pattern is in a subsection, and the name of the pattern is the name of the subsection.

\section{Repeating a task $n$ times}
If you know you need to repeat something a specific number of times, use the for loop:

\subsection{\code{for} loop}
\begin{lstlisting}
% for loop to sum numbers 1 to 5
sum_ = 0;
n = 5;
for iter = [1:n]
    sum_ = sum_ + iter;
end
fprintf('sum_ of 1 to %d is %d\n',n,sum_);
\end{lstlisting}
\pagebreak

\section{Repeating a task when you don't know in advance when you will be done}
In short, if you need a loop and a  \code{for} loop won't work, use \code{while}.
\subsection{\code{while true} with \code{break}}
A commonly used approach is to use an infinite loop structure (\code{while true}) with a \code{break} statement to get out of the infinite loop.
\begin{lstlisting}
% add integers until the sum is greater than 100
sum_ = 0;
n = 0;
while true
    n = n + 1;
    sum_ = sum_ + n;
    if sum_ > 100
        break
    end
end
fprintf('sum of 1 to %d is %d\n',n,sum_);
\end{lstlisting}

\subsection{\code{while <boolean expression>}}
Alternatively, the \code{while} can execute conditionally, as long as the provided boolean expression evaluates to \code{true}.
\begin{lstlisting}
% add integers until the sum is greater than 100
sum_ = 0;
n = 0;
while sum_ < 100
    n = n + 1;
    sum_ = sum_ + n;
end
fprintf('sum of 1 to %d is %d\n',n,sum_);
\end{lstlisting}

\pagebreak
\subsection{Validating user input}
Getting correct input from the user is a perfect application of the  \code{while true} with \code{break} pattern.
\begin{lstlisting}
% get user input between 1 and 100
n = -1;
while true
    n = input('Enter a number from 1 to 100: ');
    if n >=1 && n <=100
        break
    end
end
fprintf('n = %d\n',n);
\end{lstlisting}

\section{\code{if}, \code{elseif}, \code{else}, }

\subsection{\code{if}}
If you want to do something only if some condition is met, and there is nothing special that needs to be done if the condition is not met, then you want the \code{if} statement.
\begin{lstlisting}
% get age, and warn user if they need adult approval
age = input('Enter your age: ');
if age < 18
    fprintf('You will need adult approval.\n',n);
end
\end{lstlisting}

\subsection{\code{if}-\code{else}}
If you want to do a task if some condition is met, and you must do a different task if the condition is not met, use the \code{if}-\code{else} pattern.  Note that \code{else} {\em does not} have a boolean expression attached to it.
\begin{lstlisting}
% get age, and assign one of two categories
age = input('Enter your age: ');
age_category = '';
if age < 18
    age_category = 'minor';
else
    age_category = 'adult';
end
fprintf('For age = %d, category = %s\n',age, age_category);
\end{lstlisting}

\subsection{\code{if}-\code{elseif}}
If you must do one task from several possible tasks, use the \code{if}-\code{elseif}. Note that \code{elseif} {\em does } have a boolean expression attached to it.
\begin{lstlisting}
% get age, and assign one of more than two categories
age = input('Enter your age: ');
age_category = '';
if age < 12
    age_category = 'child';
elseif age < 18
    age_category = 'youth';
else
    age_category = 'adult';
end
fprintf('For age = %d, category = %s\n',age, age_category);
\end{lstlisting}
Also, note that the last case (the default case) used \code{else} rather than \code{elseif}. Usually, this is what your last case will do.

\chapter{Practices}

\section{Habits}
A list of good habits:
\begin{enumerate}
	\item{When you start writing a program, begin with comments describing (in sufficient detail) how your program will work. }
	\item{Practice running your program in your head as you write. It may seem slow, but it is the fastest way to write working code. }
	\item{Reformat your code frequently. This will help you detect missing end statements, and makes your code easier to understand.}
	\item{When assigning value to a variable (e.g. \code{x = ...}), end the line with a semi-colon, unless you are debugging the value of \code{x}. This makes your output easier to read. }
	\item{Typically, when using \code{fprintf}, insert \textbackslash n. E.g. \code{fprintf('Hi  \textbackslash n')}}.
\end{enumerate}

\end{document}


\documentclass[fleqn]{article}

\usepackage[no-math]{fontspec}
\usepackage[OT1]{eulervm}
\usepackage{microtype,ifthen}
\usepackage{amsmath,amssymb,amsfonts,amsthm,braket,cancel}
\usepackage{titlesec}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[style=numeric-comp,backend=biber,doi=false,isbn=false,url=false,date=year]{biblatex}
\usepackage{hyperref}

% Fonts
\defaultfontfeatures{%
    RawFeature={%
        +calt   % *Contextual alternates
        ,+clig  % *contextual ligatures
        ,+ccmp  % *composition & decomposition
        ,+tlig  % 'tex-ligatures': `` '' -- --- !` ?` << >>
        ,+cv06  % narrow guillemets
    }%
}
\setmainfont{EB Garamond}
\newfontfamily{\smallcaps}[RawFeature={+c2sc,+scmp}]{EB Garamond}
\newfontfamily{\swash}[RawFeature={+swsh}]{EB Garamond}

% Sections
\titleformat{\section}{\normalfont \Large \scshape}{\thesection}{1em}{}
\titleformat{\subsection}{\normalfont \large \scshape}{\thesubsection}{1em}{}
\titleformat{\subsubsection}{\normalfont \scshape}{\thesubsubsection}{0.5em}{}
\pagestyle{headings}
\renewcommand{\thesection}{\roman{section}}
\renewcommand{\thesubsection}{\thesection.\roman{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\roman{subsubsection}}

% Theorems
\newtheoremstyle{definition}{}{}{\itshape}{\parindent}{\scshape}{.}{1em}{\thmname{#1}\thmnumber{ #2}:}
\newtheoremstyle{theorem}{}{}{}{\parindent}{\scshape}{}{1em}{\thmname{#1}\thmnumber{ #2}:}
\theoremstyle{theorem}
\newtheorem{lemma}{Lemma}[section]
\newtheorem{corollary}[lemma]{Corollary}
\theoremstyle{definition}
\newtheorem{definition}{Definition}

% Math operators
\DeclareMathOperator*{\argmin}{argmin}
\DeclareMathOperator*{\argmax}{argmax}
\DeclareMathOperator{\logm}{Log}
\newcommand{\norm}[2][]{\left\Vert#2\right\Vert_{#1}}
\newcommand{\abs}[1]{\left\vert#1\right\vert}
\newcommand{\SN}[2][]{\mathcal{SN}_{#1}\left(#2\right)}

% Metadata
\title{Skewnormal}
\newcommand{\myName}{Jacopo Schiavon}
\newcommand{\myMail}{\href{mailto:jschiavon@stat.unipd.it}{\ttfamily jschiavon@stat.unipd.it}}
\newcommand{\myDept}{Department of Statistical Sciences, University of Padova}
\author{\myName\thanks{\myDept. Contact: \myMail}}
\hypersetup{pdfauthor={\myName},
    pdfcreator={\myName},
    breaklinks=True,
    colorlinks=true,       	% false: boxed links; true: colored links
    linkcolor=MidnightBlue, % color of internal links
    citecolor=ForestGreen,	% color of links to bibliography
    filecolor=Plum,			% color of file links
    urlcolor=Sepia			% color of url link
}

% Bibliography
\setlength\bibitemsep{1.5\itemsep}
\setlength\bibhang{1.5\parindent}
\renewcommand*{\mkbibnamefamily}[1]{\textsc{#1}}
\renewcommand*{\mkbibnamegiven}[1]{\textsc{#1}}
\renewcommand*{\mkbibnameprefix}[1]{\textsc{#1}}
\renewcommand*{\mkbibnamesuffix}[1]{\textsc{#1}}
\renewcommand*{\labelnamepunct}{\par}
\addbibresource{biblio.bib}

\setlength{\parindent}{0pt}

\begin{document}
    \maketitle

    \section{Parametrization}
    Let $y\in\mathbb{R}^d$, we say that $y\sim\SN[d]{\xi, \bar\Sigma, \delta}$ if
    \begin{equation}\label{eq:first}
        p(y\mid \xi, \bar\Sigma, \delta) = \int_0^\infty 2\phi_{d+1}\left(\left[y^\top, z\right]^\top\mid \mu, \Omega\right)dz
    \end{equation}
    with $\mu=\left[\xi^\top, 0\right]^\top$ and $\Omega=\begin{pmatrix}
        \omega\Sigma\omega  &   \omega\delta\\
        \delta^\top\omega   &   1
    \end{pmatrix}$ and $\bar\Sigma = \omega\Sigma\omega$ is the decomposition of the covariance matrix in correlation matrix and the diagonal matrix with variances.

    By defining $\theta= \omega\delta$ and $\Psi = \bar\Sigma - \theta\theta^\top$, we can rewrite the previous density as
    \begin{equation}\label{eq:second}
        p(y\mid \xi, \bar\Sigma, \delta) \propto \int_0^\infty 2\phi_1(z)\phi_d\!\left(y\mid \xi+\theta z, \Psi\right)dz.
    \end{equation}
    Note that $\abs{\Omega} = \abs{\omega\Sigma\omega - \omega\delta\delta^\top\omega} = \abs{\omega\left(\Sigma-\delta\delta^\top\right)\omega} = \abs{\Psi}$ and
    \begin{equation*}
        \Omega^{-1} = \begin{pmatrix}
            \Psi^{-1}   &   -\Psi^{-1}\theta\\
            - \theta^\top\Psi^{-1}  &   1 + \theta^\top\Psi^{-1}\theta
        \end{pmatrix}
    \end{equation*}

    Moreover, by rearranging the terms from equation~\eqref{eq:second} we can write:
    \begin{align*}
        p(y\mid \xi, \bar\Sigma, \delta) &\propto \int_0^\infty 2\phi_1(z\mid\bar\mu,\bar\sigma^2) \phi_d\!\left(y\mid \xi, \Psi\right) \exp\left[\frac{\bar\mu^2}{2\bar\sigma^2}\right]dz\\
        &= \phi_d\!\left(y\mid \xi, \Psi\right) \exp\left[\frac{\bar\mu^2}{2\bar\sigma^2}\right] 2 \int_{-\bar\mu/\bar\sigma}^\infty\phi_1(z)dz\\
        &= 2\phi_d\!\left(y\mid \xi, \Psi\right) \exp\left[\frac{\bar\mu^2}{2\bar\sigma^2}\right]\Phi_1\!\left(\frac{\bar\mu}{\bar\sigma}\right)\\
        &= 2\phi_d\!\left(y\mid \xi, \Psi\right) \exp\left[\frac{1}{2}(y-\xi)^\top\alpha\alpha^\top(y-\xi)\right]\Phi_1\!\left(\alpha^\top(y-\xi)\right)\\
        &= 2\phi_d\!\left(y\mid \xi, \Psi - \alpha\alpha^\top\right) \Phi_1\!\left(\alpha^\top(y-\xi)\right)
    \end{align*}
    where we have used
    \begin{align*}
        \bar\mu &= \frac{(y-\xi)^\top\Psi^{-1}\theta}{1 + \theta^\top\Psi^{-1}\theta}        &       \bar\sigma^2 &= \left(1 + \theta^\top\Psi^{-1}\theta\right)^{-1}
    \end{align*}
    and we defined
    \begin{equation*}
        \alpha = \frac{\Psi^{-1}\theta}{\sqrt{1 + \theta^\top\Psi^{-1}\theta}}
    \end{equation*}

    \section{Constraints}
    In order for $\Psi$ (and thus $\Omega$) to be positive definite, a constrain should be put on $\delta$ and $\bar\Sigma$.  First of all, recall that the matrix $\theta\theta^\top$ has only one strictly positive eigenvalue, equal to $\norm{\theta}^2$, while all the others are 0. As it can be proven that $\Psi$ is SPD if and only if the smallest eigenvalue of $\bar\Sigma$ is larger than $\norm{\theta}^2$, we can require that
    \begin{equation*}
        \norm{\theta}^2 = \delta^\top\omega\omega\delta \leq \min_i\lambda_i(\bar\Sigma)
    \end{equation*}

    \section{Data generation mechanism}
    To generate samples from a skewnormal we exploit equation~\eqref{eq:first} and we proceed in the following way:
    \begin{itemize}
        \item We compute $\mu$ and $\Omega$ from the parameters $\xi$, $\bar\Sigma$ and $\delta$
        \item We generate a sample from a $(d+1)$-variate normal distribution: $Z \sim \mathcal{N}_{d+1}(\mu,\Omega)$
        \item if $Z[d+1] \geq 0$ then $y = Z[:d]$, else $y = - Z[:d]$.
    \end{itemize}

    \printbibliography

\end{document}