\newpage
\section{Matrix Normal Forms}

\begin{Operator}[smithex]{SMITHEX}
The operator \name{smithex} computes the Smith normal form S of a
\nameref{matrix} A (say). It returns \{S,P,$P^-1$\} where $P*S*P^-1 = A$.

\begin{Syntax}
\name{smithex}\(\meta{matrix},\meta{variable}\)

\meta{matrix} :- a rectangular \nameref{matrix} of univariate polynomials in
                 \meta{variable}.
\meta{variable} :- the variable.
\end{Syntax}

\begin{Examples}
 a := mat((x,x+1),(0,3*x^2)); &
\begin{multilineoutput}{6cm}
      [x  x + 1]
      [        ]
 a := [      2 ]
      [0  3*x  ]
\end{multilineoutput}\\

 smithex(a,x); &
\begin{multilineoutput}{6cm}
   [1  0 ]    [1    0]    [x   x + 1]
\{  [     ],   [      ],   [         ]  \}
   [    3]    [   2  ]    [         ]
   [0  x ]    [3*x  1]    [-3    -3 ]
\end{multilineoutput}\\
\end{Examples}
\end{Operator}

\begin{Operator}[smithexint]{SMITHEX_INT}
The operator \name{smithex\_int} performs the same task as \name{smithex}
but on matrices containing only integer entries. Namely,
\name{smithex\_int} returns \{S,P,$P^-1$\} where S is the smith normal
form of the input \nameref{matrix} (A say), and $P*S*P^-1 = A$.

\begin{Syntax}
\name{smithex\_int}\(\meta{matrix}\)

\meta{matrix} :- a rectangular \nameref{matrix} of integer entries.
\end{Syntax}

\begin{Examples}
 a := mat((9,-36,30),(-36,192,-180),(30,-180,180)); &
\begin{multilineoutput}{6cm}
     [ 9   -36    30 ]
     [               ]
a := [-36  192   -180]
     [               ]
     [30   -180  180 ]
\end{multilineoutput}\\

 smithex_int(a); &
\begin{multilineoutput}{6cm}
  [3  0   0 ]    [-17  -5   -4 ]    [1   -24  30 ]
  [         ]    [             ]    [            ]
\{ [0  12  0 ],   [64   19   15 ],   [-1  25   -30] \}
  [         ]    [             ]    [            ]
  [0  0   60]    [-50  -15  -12]    [0   -1    1 ]
\end{multilineoutput}\\
\end{Examples}
\end{Operator}

\begin{Operator}[frobenius]{FROBENIUS}
The operator \name{frobenius} computes the \name{frobenius} normal form F of a
\nameref{matrix} (A say). It returns \{F,P,$P^-1$\} where $P*F*P^-1 = A$.

\begin{Syntax}
\name{frobenius}\(\meta{matrix}\)

\meta{matrix} :- a square \nameref{matrix}.
\end{Syntax}

Field Extensions:

By default, calculations are performed in the rational numbers. To
extend this field the \nameref{arnum} package can be used. The package must
first be loaded by load\_package arnum;. The field can now be extended
by using the defpoly command. For example, defpoly sqrt2**2-2; will
extend the field to include the square root of 2 (now defined by sqrt2).

Modular Arithmetic:

\name{Frobenius} can also be calculated in a modular base. To do this
first type on  modular;. Then setmod p; (where p is a prime) will set
the modular base of calculation to p. By further typing on balanced\_mod
the answer will appear using a symmetric modular representation. See
\nameref{ratjordan} for an example.

\begin{Examples}
 a := mat((x,x^2),(3,5*x)); &
\begin{multilineoutput}{6cm}
     [    2 ]
     [x  x  ]
a := [      ]
     [3  5*x]
\end{multilineoutput}\\
 frobenius(a);&
\begin{multilineoutput}{6cm}
   [         2]    [1  x]    [       - x ]
\{  [0   - 2*x ],   [    ],   [1     -----]  \}
   [          ]    [0  3]    [        3  ]
   [1    6*x  ]              [           ]
                             [        1  ]
                             [0      --- ]
                             [        3  ]

\end{multilineoutput}\\
 load\_package arnum;\\
 defpoly sqrt2**2-2;\\
 a := mat((sqrt2,5),(7*sqrt2,sqrt2));&
\begin{multilineoutput}{6cm}
     [ sqrt2     5  ]
a := [              ]
     [7*sqrt2  sqrt2]
\end{multilineoutput}\\

 frobenius(a); &
\begin{multilineoutput}{6cm}
  [0  35*sqrt2 - 2]    [1   sqrt2 ]    [           1  ]
\{ [               ],   [          ],   [1       - --- ]  \}
  [1    2*sqrt2   ]    [1  7*sqrt2]    [           7  ]
                                       [              ]
                                       [     1        ]
                                       [0   ----*sqrt2]
                                       [     14       ]

\end{multilineoutput}\\
\end{Examples}
\end{Operator}

\begin{Operator}[ratjordan]{RATJORDAN}
The operator \name{ratjordan} computes the rational Jordan normal form R
of a \nameref{matrix} (A say). It returns \{R,P,$P^-1$\} where $P*R*P^-1 = A$.

\begin{Syntax}
\name{ratjordan}\(\meta{matrix}\)

\meta{matrix} :- a square \nameref{matrix}.
\end{Syntax}

Field Extensions:

By default, calculations are performed in the rational numbers. To
extend this field the \name{arnum} package can be used. The package must
first be loaded by  load\_package arnum;. The field can now be extended
by using the defpoly command. For example, defpoly sqrt2**2-2; will
extend the field to include the square root of 2 (now defined by sqrt2).
See \nameref{frobenius} for an example.

Modular Arithmetic:

\name{ratjordan} can also be calculated in a modular base. To do this
first type on  modular;. Then setmod p; (where p is a prime) will set
the modular base of calculation to p. By further typing on balanced\_mod
the answer will appear using a symmetric modular representation.

\begin{Examples}
 a := mat((5,4*x),(2,x^2));&
\begin{multilineoutput}{6cm}
     [5  4*x]
     [      ]
a := [    2 ]
     [2  x  ]
\end{multilineoutput}\\

 ratjordan(a); &
\begin{multilineoutput}{6cm}
  [0  x*( - 5*x + 8)]   [1  5]    [        -5 ]
\{ [                 ],  [    ],   [1     -----] \}
  [        2        ]   [0  2]    [        2  ]
  [1      x  + 5    ]             [           ]
                                  [        1  ]
                                  [0     -----]
                                  [        2  ]
\end{multilineoutput}\\
 on modular; \\
 setmod 23; \\
 a := mat((12,34),(56,78)); &
\begin{multilineoutput}{6cm}
     [12  11]
a := [      ]
     [10  9 ]
\end{multilineoutput}\\

 ratjordan(a); &
\begin{multilineoutput}{6cm}
  [15  0]   [16  8]   [1  21]
\{ [     ],  [     ],  [     ]  \}
  [0   6]   [19  4]   [1  4 ]
\end{multilineoutput}\\

 on balanced\_mod;\\
 ratjordan(a);&
\begin{multilineoutput}{6cm}
  [- 8  0]   [ - 7  8]   [1  - 2]
\{ [      ],  [       ],  [      ]  \}
  [ 0   6]   [ - 4  4]   [1   4 ]
\end{multilineoutput}\\

\end{Examples}
\end{Operator}

\begin{Operator}[jordansymbolic]{JORDANSYMBOLIC}
The operator \name{jordansymbolic} computes the Jordan normal form J
of a \nameref{matrix} (A say). It returns \{J,L,P,$P^-1$\} where
$P*J*P^-1 = A$. L = \{ll,mm\} where mm is a name and ll is a list of
irreducible factors of p(mm).

\begin{Syntax}
\name{jordansymbolic}\(\meta{matrix}\)

\meta{matrix} :- a square \nameref{matrix}.
\end{Syntax}

Field Extensions:

By default, calculations are performed in the rational numbers. To
extend this field the \nameref{arnum} package can be used. The package must
first be loaded by  load\_package arnum;. The field can now be extended
by using the defpoly command. For example, defpoly sqrt2**2-2; will
extend the field to include the square root of 2 (now defined by sqrt2).
See \nameref{frobenius} for an example.

Modular Arithmetic:

\name{jordansymbolic} can also be calculated in a modular base. To do this
first type on  modular;. Then setmod p; (where p is a prime) will set
the modular base of calculation to p. By further typing on balanced\_mod
the answer will appear using a symmetric modular representation. See
\nameref{ratjordan} for an example.

% Extras:

% If using \name{xr}, the X interface for REDUCE, then the appearance of the
% output can be improved by switching on looking\_good. This
% converts any lambdas to a greek font.

\begin{Examples}

 a := mat((1,y),(2,5*y)); &
\begin{multilineoutput}{6cm}
     [1   y ]
a := [      ]
     [2  5*y]
\end{multilineoutput}\\

 jordansymbolic(a); &
\begin{multilineoutput}{6cm}
\{

 [lambda11     0    ]
 [                  ]
 [   0      lambda12]

 ,

         2
 {{lambda  - 5*lambda*y - lambda + 3*y},lambda},

 [lambda11 - 5*y  lambda12 - 5*y]
 [                              ]
 [      2               2       ]

 ,

 [ 2*lambda11 - 5*y - 1    5*lambda11*y - lambda11 - y + 1 ]
 [----------------------  ---------------------------------]
 [       2                              2                  ]
 [   25*y  - 2*y + 1             2*(25*y  - 2*y + 1)       ]
 [                                                         ]
 [ 2*lambda12 - 5*y - 1    5*lambda12*y - lambda12 - y + 1 ]
 [----------------------  ---------------------------------]
 [       2                              2                  ]
 [   25*y  - 2*y + 1             2*(25*y  - 2*y + 1)       ]

 \}

\end{multilineoutput}\\
\end{Examples}
\end{Operator}

\begin{Operator}[jordan]{JORDAN}
The operator \name{jordan} computes the Jordan normal form J
of a \nameref{matrix} (A say). It returns \{J,P,$P^-1$\} where $P*J*P^-1 = A$.

\begin{Syntax}
\name{jordan}\(\meta{matrix}\)

\meta{matrix} :- a square \nameref{matrix}.
\end{Syntax}

Field Extensions:
By default, calculations are performed in the rational numbers. To
extend this field the \name{arnum} package can be used. The package must
first be loaded by  load\_package arnum;. The field can now be extended
by using the defpoly command. For example, defpoly sqrt2**2-2; will
extend the field to include the square root of 2 (now defined by sqrt2).
See \nameref{frobenius} for an example.

Modular Arithmetic:
\name{Jordan} can also be calculated in a modular base. To do this
first type on  modular;. Then setmod p; (where p is a prime) will set
the modular base of calculation to p. By further typing on balanced\_mod
the answer will appear using a symmetric modular representation. See
\nameref{ratjordan} for an example.

\begin{Examples}

 a := mat((1,x),(0,x)); &
\begin{multilineoutput}{6cm}
     [1  x]
a := [    ]
     [0  x]
\end{multilineoutput}\\

 jordan(a);&
\begin{multilineoutput}{6cm}
\{

 [1  0]
 [    ]
 [0  x]

 ,

 [   1           x       ]
 [-------  --------------]
 [ x - 1     2           ]
 [          x  - 2*x + 1 ]
 [                       ]
 [               1       ]
 [   0        -------    ]
 [             x - 1     ]

 ,

 [x - 1   - x ]
 [            ]
 [  0    x - 1]

 \}
\end{multilineoutput}\\
\end{Examples}

\end{Operator}


    \filetitle{movsum}{Moving sum pseudofunction}{modellang/movsum}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
movsum(Expr)
movsum(Expr,K)
\end{verbatim}

\paragraph{Description}\label{description}

If the second input argument, \texttt{K}, is negative, this function
expands to the moving sum of the last K periods (including the current
period), i.e.

\begin{verbatim}
((Expr)+(Expr{-1})+ ... +(Expr{-(K-1)})
\end{verbatim}

where \texttt{Expr\{-N\}} derives from \texttt{Expr} and has all its
time subscripts shifted by \texttt{-N} (if specified).

If the second input argument, \texttt{K}, is positive, this function
expands to the moving sum of the next K periods ahead (including the
current period), i.e.

\begin{verbatim}
((Expr)+(Expr{1})+ ... +(Expr{K-1})
\end{verbatim}

If the second input argument, \texttt{K}, is not specified, the default
value -4 is used (based on the fact that most of the macroeconomic
models are quarterly).

\paragraph{Example}\label{example}

The following three lines

\begin{verbatim}
movsum(Z)
movsum(Z,-3)
movsum(X+Y{-1},2)
\end{verbatim}

will expand to

\begin{verbatim}
((Z)+(Z{-1})+(Z{-2})+(Z{-3}))
((Z)+(Z{-1})+(Z{-2}))
((X+Y{-1})+(X{1}+Y))
\end{verbatim}


\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsthm,amssymb,amsfonts}
\usepackage{enumitem}
\usepackage{tabu}
\usepackage{xcolor}
 \usepackage{mathtools}
 \usepackage{gensymb}

\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}

\newcommand{\nspace}{\vspace*{.5cm}}
\newcommand{\nline}{\nspace \noindent}

\newcommand{\expected}[1]{\text{E}(#1)}

\newenvironment{nscenter}
 {\parskip=0pt\par\nopagebreak\centering}
 {\par\noindent\ignorespacesafterend}

\def\SPSB#1#2{\rlap{\textsuperscript{\textcolor{black}{#1}}}\SB{#2}}

 % 6.1 4,17,18,21
 % 6.2 2,4,5,9,28,29

\begin{document}
\title{Math 487 Homework 5}
\author{Jacob Shiohira}
\maketitle

\subsection*{Section 6.1}

% In Las Vegas the roulette wheel has a $0$ and a $00$ and then the numbers $1$ to $36$ marked on equal slots; the wheel is spun and a ball stops randomly in one slot. When a player bets $1$ dollar on a number, he receives $36$ dollars if the ball stops on this number, for a net gain of $35$ dollars; otherwise, he loses his dollar bet. Find the expected value for his winnings.

\noindent
\textbf{Ch 6.1 Q4} With $38$ possible spots to place a bet on, we know that the probability of winning $35$ dollars is $1/38$, and the probability of losing a dollar is $37/38$. Let $X$ be the random variable which denotes your winnings on a $1$ dollar bet in Las Vegas roulette. Then, the distribution of $X$ is given by

\begin{equation*}
m_X = {-1 \qquad 35 \choose 37/38 \quad 1/38}.
\end{equation*}

\nline
Thus, we see then that $-1 \cdot (37/38) + 35 \cdot (1/38) = - 1/19$.

% Let $X$ be the first time that a failure occurs in an infinite sequence of Bernoulli trials with probability $p$ for success. Let $p_k = P(X = k)$ for $k = 1, 2, \ldots$. Show that $p_k = p^{k - 1}q$ where $q = 1 - p$. Show that $\sum_{k} p_k = 1$. Show that $\text{E}(X) = 1/q$. What is the expected number of tosses of a coin required to obtain the first tail?

\nline
\textbf{Ch 6.1 Q17} Since $X$ is the first time that a failure occurs in an infinite sequence, let's assume that the failure occurs after $k$ trials. Then, the probability of a failure is $k-1$ multiplications of $p$ and $1$ multiplication of $1-p$. This is valid because $k-1+1=k$ for our $k$ trials. Thus, for $q=1-p$, we can formally see that

\begin{equation*}
p_k = p^{k-1} (1-p) = p^{k-1} q.
\end{equation*}

\nline
Then, we can show $\sum_{k} p_k = 1$. Since $k=1,2, \ldots$, the limits of the summation are originally $k=1$ to $\infty$.

\begin{align*}
\sum_{k} p_k &= \sum_{k=1}^{\infty} p^{k-1} q \\
&= q \sum_{k=0}^{\infty} p^{k} \\
&= q \Big ( \frac{1}{1-p} \Big ) \\
&= q \frac{1}{q} \\
&= 1.
\end{align*}

\nline
In this case, $p_k = p^{k-1} q$ for $k = 1,2,\ldots$. So,

\begin{align*}
\text{E}(X) &= 1q + 2pq + 3p^2q + \ldots \\
&= q (1 + 2p + 3p^2 + \ldots).
\end{align*}

\nline
Then, we can see that the infinite series $1 + p + p^2 + \ldots$, which is equivalent to $\frac{1}{1-p}$, is embedded in the previous equation. By differentiating this, we get $1 + 2p + 3p^2 + \ldots$, which is the term we have above and is equivalent to $\frac{1}{(1-p)^2}$. By substituting into the expanded equation of $\text{E}(X)$, we get

\begin{align*}
\text{E}(X) &= q (1 + 2p + 3p^2 + \ldots) \\
&= q \frac{1}{(1-p)^2} \\
&= q \Big ( \frac{1}{q^2} \Big ) \\
&= \frac{1}{q}.
\end{align*}

\nline
Thus, the expected value of the first tail of a fair coin is $\frac{1}{q} = \frac{1}{\frac{1}{2}} = 2$.

%  Exactly one of six similar keys opens a certain door. If you try the keys, one after another, what is the expected number of keys that you will  have to try before success?

\nline
\textbf{Ch 6.1 Q18} To compute the expected value, $\text{E}(X)$ for some random variable $X$, we can use the definition of $\text{E}(X)$ using a sum and a distribution function,

\begin{align*}
\text{E}(X) &= \sum_{k=1}^{6} k \cdot \frac{1}{6} \\
&= \frac{1}{6} + \frac{2}{6} + \frac{3}{6} + \frac{4}{6} + \frac{5}{6} + \frac{6}{6} \\
&= \frac{7}{2}.
\end{align*}

% Let $X$ be a random variable which is Poisson distributed with parameter $\lambda$. Show that E$(X) = \lambda$. Hint: Recall that

% \begin{equation*}
% e^{x} = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \cdots .
% \end{equation*}

\nline
\textbf{Ch 6.1 Q21} To compute the expected value of $X$, we need to note that the distribution function of $X$ is $p(x) = \frac{ e^{- \lambda} \lambda^x }{x!}$. Then,

\begin{align*}
\text{E}(X) = \sum_{x=0}^{\infty} x \cdot p(x) &= \sum_{x=1}^{\infty} x \cdot  \frac{ e^{- \lambda} \lambda^x }{x!} \\
&= \sum_{x=1}^{\infty} \frac{ e^{- \lambda} \lambda^x }{(x-1)!} \\
&= e^{- \lambda} \sum_{x=1}^{\infty} \frac{ \lambda^x }{(x-1)!} \\
\end{align*}

\nline
Now, we see that the denominator yields $0$ when $x=1$, so we can set $k=x-1$ and adjust the summation,

\begin{align*}
\text{E}(X) &= \lambda e^{- \lambda} \sum_{k=1}^{\infty} \frac{ \lambda^k }{k!} \\
\end{align*}

\nline
Now, we see that $\sum_{k=1}^{\infty} \frac{ \lambda^k }{k!} = 1 + x + \frac{\lambda^2}{2!} + \frac{\lambda^3}{3!} + \cdots$, which is equivalent to $e^{\lambda}$. By substituting into $\text{E}(X)$, we get

\begin{equation*}
\text{E}(X) = \lambda e^{- \lambda} e^{\lambda} = \lambda.
\end{equation*}

\subsection*{Section 6.2}

\nline
\textbf{Ch 6.2 Q2} A random variable $X$ has the distribution

\begin{equation*}
p_x = {0 \qquad 1 \qquad 2 \qquad 4 \choose 1/3 \quad 1/3 \quad 1/6 \quad 1/6}
\end{equation*}

\nline
Find the expected value, variance, and standard deviation of $X$.

\begin{align*}
\text{E}(X) &= 0 \cdot \frac{1}{3} + 1 \cdot \frac{1}{3} + 2 \cdot \frac{1}{6} + 4 \cdot \frac{1}{6} \\
&= \frac{1}{3} + \frac{2}{6} + \frac{4}{6} \\
&= \frac{4}{3}.
\end{align*}

\begin{align*}
\text{V}(X) &= \text{E}(X^2) - \Big ( \frac{4}{3} \Big )^2 \\
&= \Big [ 0^2 \cdot \frac{1}{3} + 1^2 \cdot \frac{1}{3} + 2^2 \cdot \frac{1}{6} + 4^2 \cdot \frac{1}{6} \Big ] - \frac{16}{9} \\
&= \Big [ \frac{11}{3} \Big ] - \frac{16}{9} \\
&= \frac{17}{9}.
\end{align*}

\begin{align*}
\text{D}(X) = \sqrt{\text{V}(X)} = \sqrt{\frac{17}{9}} = \frac{\sqrt{17}}{3}.
\end{align*}

\nline
\textbf{Ch 6.2 Q4} $X$ is a random variable with $\text{E}(X) = 100$ and $\text{V}(X) = 15$. Find

\begin{enumerate}[label=(\alph*)]
\item $\expected{X^2}$

\nline
We can rearrange $\text{V}(X) = \text{E}(X^2) - \text{E}(X)^2$ for $\text{E}(X^2)$, which yields $\text{V}(X) + \text{E}(X)^2$. Plugging in values, we see that $\expected{X^2} = 10015$.

\item $\expected{3X + 10}$

\nline
We can use the fact that $\expected{cX} = c \expected{X}$. So, $\expected{3X + 10}$ is equivalent to $3 \expected{X} + 10$. Thus, $\expected{3X + 10} = 310$.

\item $\expected{-X}$

\nline
We can use the fact that $\expected{cX} = c \expected{X}$. So, $\expected{-X}$ is equivalent to $-1 \expected{X}$. Thus, $\expected{-X} = -100$.

\item $\text{V}(-X)$

\nline
We can use the fact that $V(cX) = c^2 V(X)$. So, $\text{V}(-X) = -1^2 \text{V}(X)$. Thus,  $\text{V}(-X) = 15$.

\item $\text{D}(-X)$

\nline
We can use the previous result that said $\text{V}(-X) = 15$ since $\text{D}(-X) = \sqrt{\text{V}(-X)}$. Thus, $\text{D}(-X) = \sqrt{15}$.

\end{enumerate}

\nline
\textbf{Ch 6.2 Q5} In a certain manufacturing process, the (Fahrenheit) temperature never varies by more than $2^{\circ}$ from $62^{\circ}$. The temperature is, in fact, a random variable $F$ with distribution

\begin{equation*}
P_F = { 60 \qquad 61 \qquad 62 \qquad  63 \qquad 64 \choose 1/10 \quad 2/10 \quad 4/10 \quad 2/10 \quad 1/10}.
\end{equation*}

\begin{enumerate}[label=(\alph*)]
\item Find $\expected{F}$ and $\text{V}(F)$.

\begin{equation*}
\expected{F} = 60 \cdot \frac{1}{10} + 61 \cdot \frac{2}{10} + 62 \cdot \frac{4}{10} + 63 \cdot \frac{2}{10} + 64 \cdot \frac{1}{10} = 62.
\end{equation*}

\begin{equation*}
\text{V}(F) = \Big [ 60^2 \cdot \frac{1}{10} + 61^2 \cdot \frac{2}{10} + 62^2 \cdot \frac{4}{10} + 63^2 \cdot \frac{2}{10} + 64^2 \cdot \frac{1}{10} \Big ] - 62^2 = 1.2.
\end{equation*}

\item Define $T = F - 62$. Find $\expected{T}$ and $\text{V}(T)$, and compare these answers with those in part $(a)$.

\nline
We know that $\expected{X + a} = \expected{X} + a$, so $\expected{T} = \expected{F} - 62$. Thus, $\expected{T} = 0$. Additionally, we know that $\text{V}(X + a) = \text{V}(X)$, so $\text{V}(T) = \text{V}(F)$. Thus, $\text{V}(T) = 1.2$.

\item It is decided to report the temperature readings on a Celsius scale, that
is, $C = (5/9) (F - 32)$. What is the expected value and variance for the
readings now?

\nline
We know that $\expected{X + a} = \expected{X} + a$, so $\expected{C} = \frac{5}{9} \Big ( \expected{F} - 32 \Big )$. Thus, $\expected{C} = \frac{50}{3}$. Additionally, we know that $\text{V}(cX + a) = c^2 \text{V}(X)$, so $\text{V}(C) = \frac{5}{9}^2 \cdot \text{V}(F)$. Thus, $\text{V}(C) = \frac{5}{9}^2 \cdot \frac{6}{5}= \frac{10}{27}$.
\end{enumerate}

\nline
\textbf{Ch 6.2 Q9} A die is loaded so that the probability of a face coming up is proportional to the number on that face. The die is rolled with outcome $X$. Find $\text{V}(X)$ and $\text{D}(T)$.

\nline
The total of all faces on a die is $21$. Thus, the probability of rolling a $1$ is $1/21$, rolling a $2$ is $2/21$, and so on. Thus, we can compute expected value using the summation definition.

\begin{equation*}
\sum_{x=1}^{6} x \cdot \frac{x}{21} = \frac{13}{3}.
\end{equation*}

\nline
Then, $\text{V}(X)  = \expected{X^2} - \expected{X}^2$. So,

\begin{align*}
\text{V}(X) &= \Big ( \sum_{x=1}^{6} x^2 \cdot \frac{x}{21} \Big ) - \frac{13}{3}^2 \\
&= 21 - \frac{13}{3}^2 = \frac{20}{9}.
\end{align*}

\nline
Lastly, $\text{D}(X) = \sqrt{\text{V}(X)} = \sqrt{ \frac{20}{9}} = \frac{\sqrt{20}}{3}$.

% TODO: finish this by getting help?
\nline
\textbf{Ch 6.2 Q28} In Example $5.3$, assume that the book in question has $1000$ pages. Let $X$ be the number of pages with no mistakes. Show that $\expected{X} = 905$ and $\text{V}(X) = 86$. Using these results, show that the probability is $\leq .05$ that there will be more than $924$ pages without errors or fewer than $866$ pages without errors.

\nline
The likelihood that there are no mistakes on a page can is given by $\frac{ e^{- 0.1} 0.1^0 }{0!} = e^{-.1} = 0.9048374$. Since the likelihood of a mistake (or lack thereof) on a page is independent of other pages, we can multiply this probability by $1000$, which represents the number of pages in the book, to find the expected value of pages without mistakes. So, $\expected{X} = 1000 \cdot e^{-.1} = 905$ pages.

\nline
Per the usual definition, we know $\text{V}(X) = \expected{X^2} - \expected{X}^2$. However, since we know that a mistake on the $i^{\text{th}}$ page does not affect a mistake on the $i+1^{\text{th}}$ page, we can reduce the equation to

\begin{align*}
\text{V}(X) &= \expected{X} - \expected{X}^2 \\
&= \Bigg [ \frac{ e^{- 0.1} 0.1^0 }{0!} - \Bigg ( \frac{ e^{- 0.1} 0.1^0 }{0!} \Bigg )^2 \Bigg ] \\
&= \frac{ e^{- 0.1} 0.1^0 }{0!} \Bigg ( 1- \frac{ e^{- 0.1} 0.1^0 }{0!} \Bigg ) \\
&= e^{- 0.1} \Bigg ( 1- e^{- 0.1} \Bigg ) \\
& \sim 0.086107
\end{align*}

\nline
So, for total variance see that $\text{V}(X) = 1000 \cdot 0.086107 = 86$.

\nline
We can use the cumulative distribution function for a binomial distribution to show $\mathbb{P}(X > 924) \leq .05$ and $\mathbb{P}(X < 866) \leq .05$. We see that $F(924; 0.9048374, 1000) \sim 0.985$, so the probability of more than $924$ pages is approximately $.015$. Additionally, $F(866; 0.9048374, 1000) \sim 4.209053 \times 10^{-5}$, and this is obviously less than $.05$.

%http://www.chegg.com/homework-help/questions-and-answers/book-200-pages-number-mistakes-page-apoisson-random-variable-mean-001-independent-thenumbe-q228411
\nline
\textbf{Ch 6.2 Q29} Let $X$ be Poisson distributed with parameter $\lambda$. Show that $\text{V}(X) = \lambda$.

\nline
To find $\text{V}(X)$ of the Poisson distributed random variable $X$ with parameter $\lambda$, we can use the equation

\begin{equation*}
\text{V}(X) = \expected{X^2} - \expected{X}^2.
\end{equation*}

\nline
To find $\expected{X^2}$, we need a second order value of $X$, but there is no $i,j$ where $i \neq j$ such that $X=x_i=x_j$. So, $\expected{X^2} = \expected{X(X-1) + X}$.

\begin{align*}
\expected{X(X-1) + X} &= \sum_{x=0}^{\infty} x(x-1) \frac{ e^{- \lambda} \lambda^x }{x!} \\
&= \sum_{x=0}^{\infty} \frac{ e^{- \lambda} \lambda^x }{(x-2)!} \\
&= \lambda^2  e^{- \lambda} \sum_{x=2}^{\infty} \frac{ \lambda^{x-2} }{(x-2)!} \\
&= \lambda^2  e^{- \lambda} e^{- \lambda} =  \lambda^2\\
\end{align*}

\nline
Thus, we can substitute values and we see that $\text{V}(X) = \lambda^2 + \lambda - \lambda^2 = \lambda$.
\end{document}


\documentclass[a4paper,11pt]{scrartcl}

\usepackage{amsmath,amssymb}
\usepackage[utf8]{inputenc}

\title{Informed priors and quasi-simultaneous inference for psychometric functions}
\author{Ingo Fründ}
\date{\today}

\def\prob{\mathbb{P}}

\begin{document}

\maketitle
% Your text here
When fitting psychometric functions, we are often interested in summarizing the performance of observers in as few numbers as possible.
Of particular interest is a summary of the performance that consists of a single parameter, the threshold.
Unfortunately, comparisons of psychophysical thresholds are only meaningful if other parameters of the psychometric functions --- such as slope and lapse rate --- are constant across conditions.
Here, I demonstrate how priors can be derived that incorporate the information from other conditions.
If these priors are used in bayesian inference of psychometric functions, this allows for quasi-simultaneous fits that constrain parameters to be constant across conditions.

\section{A simple example}

We start with a simple example to illustrate the basic idea behind the approach.
Assume, we colleted data $X_i$ under two different conditions $i\in\{1,2\}$.
We want to model these data depending on a parameter $\theta$.
In principle, $\theta$ might differ between the two conditions, but we will assume here, that $\theta_1=\theta_2$.
In this particular case, we can write down the posterior distribution of $\theta$:
%
$$
\prob(\theta|X_1,X_2) \propto \prob(X_1,X_2|\theta) \prob(\theta) = \prob(X_1|\theta)\prob(X_2|\theta)\prob(\theta),
$$
%
Where the second equality holds under the assumption that the two conditions are independent.

In practical applications, we will typically not be able to write down $\prob(\theta|X_1,X_2)$ directly.
Thus, we will have to resort to sampling strategies to explore $\prob(\theta|X_1,X_2)$.
The strategy in this case is therefore to perform two sampling stages.
In the first sampling run, we derive proper priors for the parameter $\theta$ and in the second sampling run, we use these priors to perform the true analysis.

For the first sampling run, we employ an improper uniform prior, that is $\prob(\theta)=\kappa$ for all $\theta$ and a fixed $\kappa$.
We then use MCMC to generate a sample $(\theta_{1i})_{i=1}^m$ of size $m$ from $\prob(\theta|X_1)\propto\prob(X_1|\theta)\cdot\kappa$ and another sample
$(\theta_{2i})_{i=1}^m$ from $\prob(\theta|X_2)\propto\prob(X_2|\theta)\cdot\kappa$.
We now fit a parametric model to each sample and use these model as priors for the second sampling run.
Denote the parametric model fitted to $(\theta_{1i})$ by $f_1$ and the parametric model fitted to $(\theta_{2i})$ by $f_2$.
Then approximate samples from the simultaneous model can be obtained by sampling either
%
$$
\prob(\theta|X_1,X_2) \propto\prob(X_1|\theta)f_2(\theta),
$$
%
or
%
$$
\prob(\theta|X_1,X_2) \propto\prob(X_2|\theta)f_1(\theta).
$$
%

\section{Constant parameters}

I will now expand the above strategy to the case in which the model has more than a single parameter.
Let us think about a model with three parameters, $\theta$, $\vartheta_1$, $\vartheta_2$.
As the notation already suggests,  $\theta$ shall be assumed fixed across conditions, while $\vartheta_1$ and $\vartheta_2$ depend on the
experimental condition.
Again, we write down the posterior distribution
%
\begin{align*}
\prob(\theta,\vartheta_1,\vartheta_2|X_1,X_2) &=\prob(\theta|X_1,X_2)\prob(\vartheta_1,\vartheta_2|X_1,X_2)\\
&= \prob(\theta|X_1,X_2)\prob(\vartheta_1|X_1)\prob(\vartheta_2|X_2),
\end{align*}
%
Where the first equality requires $\theta$ to be independent of $\vartheta_1$ and $\vartheta_2$.
That means, the parameter we want constraint to be equal across conditions necessarily has to be independent of the other parameters!
The second equality is valid if $\vartheta_1$ depends only on the data $X_1$ and $\vartheta_2$ depends only on the data $X_2$.
This is a rather plausible condition.

We can now use the same trick as above to derive priors for $\theta$ by drawing samples $(\theta_{1i})_{i=1}^m$ from $\prob(\theta,\vartheta_1|X_1)\cdot\kappa$
and $(\theta_{2i})_{i=1}^m$ from $\prob(\theta,\vartheta_2|X_2)\cdot\kappa$.
Then, we fit the marginal distributions of $(\theta_{1i})$ and $(\theta_{2i})$ to obtain expressions for $f_1$ and $f_2$.
In a second sampling run, we can then perform quasi simultaneous sampling using
%
$$
\prob(\theta,\vartheta_1|X_1,X_2) \propto\prob(X_1|\theta,\vartheta_1)f_2(\theta)\prob(\vartheta_1),
$$
%
and
%
$$
\prob(\theta,\vartheta_2|X_1,X_2) \propto\prob(X_2|\theta,\vartheta_2)f_1(\theta)\prob(\vartheta_2).
$$
%

\section{More than two conditions}

The extension of these ideas to cases with more than two conditions is straight forward:
In the first sampling run, we determine the prior densities $f_j, j=1,\dots,n$ from each dataset $X_j$ in isolation.
We then combine the $f_j$ in order to sample
%
\begin{equation}
    \label{eq:allcombined}
\prob(\theta,\vartheta_j|X_j, j=1,\dots,n) \propto\prob(X_j|\vartheta_j,\theta) \prob(\vartheta_j) \prod_{\underset{\ell\neq j}{\ell=1}}^n f_\ell ( \theta ),
\end{equation}
%
in isolation for each $j=1,\dots,n$.
Thus, the product $\prod_{\underset{\ell\neq j}{\ell=1}}^n f_\ell ( \theta )$ can be taken as a prior that incorporates prior knowledge about the other psychometric functions in the design.

\section{Specific choices of fitted posteriors}

Here, we will highlight reasonable choices to fit the posterior distributions and we will demonstrate, how these choices imply a closed functional form for the product prior in equation \eqref{eq:allcombined}.

\subsection{Fitting posterior distributions for scale parameters}

In order to compare, thresholds obtained from different conditions, it is typically required that the slopes across all these conditions are equal.
Although threshold comparisons are possible with unequal slopes, too, it is far more complicated to arrive at valid conclusions from these comparisons.
In psignifit, many parameterizations of the psychometric functions do not explicitely specify the slope of the psychometric function but instead have a parameter that is somehow related to the inverse slope.
Posterior distributions for this parameter often resemble a Gamma distribution, i.e. the seem to have a density of the form
%
\begin{equation}
    \label{eq:GammaDensity}
    f ( x | k, \theta ) = x^{k-1} \frac{\exp(-x/\theta)}{\Gamma(k)\theta^k}.
\end{equation}
%
We now assume that we have fitted this density (using for instance maximum likelihood) to a set of $n$ sampled posteriors from different conditions and we want to derive the product prior for the $n+1$-th condition.
The product prior has the density
%
\begin{align*}
    \bar{f}(x) &= \prod_{j=1}^n f_j(x) = \prod_{j=1}^n x^{k_j-1} \frac{\exp(-x/\theta_j)}{\Gamma(k_j)\theta_j^{k_j}} \\
    &\propto \prod_{j=1}^n x^{k_j-1} \exp(-x/\theta_j) \\
    &= x^{\sum_{j=1}^n k_j-1} \exp(-x\sum_{j=1}^n \frac{1}{\theta_j}) \\
    &= x^{\bar{k}-1} \exp(-x/\bar{\theta}),
\end{align*}
%
where we set
%
$$
\bar{k} := 1 + \sum_{j=1}^n k_j-1,
$$
%
and
%
$$
\bar{\theta} := \Big( \sum_{j=1}^n \frac{1}{\theta_j} \Big)^{-1}.
$$
%
Thus, the product prior is again a Gamma distribution with parameters $\bar{k}$ and $\bar{\theta}$!

\subsection{Fitting posterior distributions for rates}

The psychometric function model includes two rate parameters, the guessing rate $\gamma$ that describes the lower asymptote and the lapse rate $\lambda$ that describes the upper asymptote.
A very reasonable model for rates is the Beta distribution (cite Ferrari?).
The Beta distribution lives on the unit interval $(0,1)$ and takes the density
%
\begin{equation}
    f ( x | \alpha, \beta ) = \frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha)\Gamma(\beta)} x^{\alpha-1} (1-x)^{\beta-1}.
    \label{eq:BetaDensity}
\end{equation}
%
Here, we can use the same trick as with the Gamma distribution to derive a sensible product prior:
We assume that we fitted $n$ sets of parameters $(\alpha_j,\beta_j)_{j=1}^n$ and want to combine these to obtain a prior for the $n+1$-th condition.
The product prior in this case is
%
\begin{align*}
    \bar{f}(x) &= \prod_{j=1}^n f_j(x) = \prod_{j=1}^n \frac{\Gamma(\alpha_j+\beta_j)}{\Gamma(\alpha_j)\Gamma(\beta_j)} x^{\alpha_j-1} (1-x)^{\beta_j-1}\\
    &\propto \prod_{j=1}^n x^{\alpha_j-1} (1-x)^{\beta_j-1} \\
    &= x^{\sum_{j=1}^n \alpha_j-1} (1-x)^{\sum_{j=1}^n\beta_j-1} \\
    &= x^{\bar{\alpha}-1} (1-x)^{\bar{\beta}-1},
\end{align*}
%
where $\bar{\alpha}:= 1+ \sum_{j=1}^n\alpha_j-1$ and $\bar{\beta}:=1+\sum_{j=1}^n\beta_j-1$.

Thus, the product prior in this case, is again a Beta distribution, with parameters $\bar{\alpha}$ and $\bar{\beta}$.

\subsection{Fitting posterior distributions for thresholds}

In some, very rare situations, we might also be interested in having equal posterior distributions for thresholds.
A reasonable approximation for posterior threshold distributions is often the Normal distribution.
Interestingly, the procedure used to derive the product prior in the preceeding paragraphs does not work well for the Normal distribution.
Assume, we fitted the posteriors in conditions $j=1,\dots,n$ with Normal densities $(f_j)_{j=1}^n$ with parameters $(\mu_j,\sigma_j)_{j=1}^n$.
The product is then
%
\begin{align*}
    \bar{f}(x) &= \prod_{j=1}^n f_j(x) = \prod_{j=1}^n \frac{1}{\sqrt{2\pi}\sigma_j} \exp ( - \frac{(x-\mu_j)^2}{2\sigma_j^2})\\
    &\propto \prod_{j=1}^n \exp ( - \frac{(x-\mu_j)^2}{2\sigma_j^2})\\
    &= \exp ( - \sum_{j=1}^n \frac{(x-\mu_j)^2}{2\sigma_j^2} ) \\
    &= \exp ( - \frac{1}{2} \big( \sum_{j=1}^n \frac{x^2}{\sigma_j^2} - \sum_{j=1}^n 2\frac{x\mu_j}{\sigma_j^2} + \sum_{j=1}^n \frac{\mu_j^2}{\sigma_j^2} \big) )\\
    &\propto\exp ( - \frac{1}{2} \big( \sum_{j=1}^n \frac{x^2}{\sigma_j^2} - \sum_{j=1}^n 2\frac{x\mu_j}{\sigma_j^2}\big) ) \\
    &= \exp ( - \frac{1}{2} \big( x^2\sum_{j=1}^n \frac{1}{\sigma_j^2} - 2x\sum_{j=1}^n 2\frac{\mu_j}{\sigma_j^2} \big) ) \\
    &= \exp ( - \frac{1}{2} \underbrace{\sum_{j=1}^n \frac{1}{\sigma_j^2}}_{=:\bar{\sigma}^{-2}} \big( x^2 - 2x\underbrace{\frac{\sum_{j=1}^n \frac{\mu_j}{\sigma_j^2}}{\sum_{j=1}^n \frac{1}{\sigma_j^2}}}_{=:\bar{\mu}}\big) ) \\
    &\propto \frac{1}{\sqrt{2\pi}\bar{\sigma}} \exp( - \frac{(x-\bar{\mu})^2}{2\bar{\sigma}^2}).
\end{align*}
%
Thus, comparing coefficients with the Normal density, we end up with a new Normal distribution with parameters
%
$$
\bar{\sigma}^2 := \big(\sum_{j=1}^n \frac{1}{\sigma_j^2}\big)^{-1},
$$
%
$$
\bar{\mu} := \frac{\sum_{j=1}^n\frac{\mu_j}{\sigma_j^2}}{\sum_{j=1}^n\frac{1}{\sigma_j^2}}.
$$
%
\end{document}


\subsection{The dual space forms a vector space}

The dual space forms a vector space. We can define addition and scalar multiplication on members of the dual space.

The dimension of the dual space is the same as the underlying space.

We have defined the dual space. A vector in dual space will have also have components and a basis.

\(\mathbf w=\sum_i w_i f^j\)

So how we describe the components will depend on the choice of basis.

We choose the dual basis, the basis for \(V^*\) as:

\(\mathbf e_i \mathbf f^j =\delta_i^j\)

If the basis changes, so does the dual basis.

We write the dual basis as \(e^j\)


\chapter{Constructive Heuristics}

\section{Greedy}\label{sec:greedy}
Greedy heuristic for TSP start by choosing a random element and add always the closer node, one node at a time, making the local optimal choice. This create good path in the first part of the tour, however the last added edges create different crosses. Therefore this heuristic can be applied in combination with refining heuristics such as: \texttt{local\_branching}, \texttt{hard\_fixing}, \texttt{best\_two\_opt} and other ones that try to improve a tour.\\
The algorithm computational time is $ O(n^2) $ where $ n = |V| $, therefore is pretty fast, however depending on the first selected node, the cost of the tour can differ more than 10 \% one another. The implemented algorithm (from now only \texttt{Greedy} or \texttt{heuristic\_greedy}), at cost of execution time, perform the described greedy using all the nodes as starting node, returning the shortest tour. The time complexity for the implemented algorithm raise to $ O(n^3) $.\\
In fig \ref{fig:a280_10_} an example of tour calculated with the implemented algorithm.

\begin{figure}[h]
	\centering
	\centering
	\includegraphics[width=0.7\columnwidth]{../res/a280_10.png}
	\caption{The shortest greedy tours.}
	\label{fig:a280_10_}
\end{figure}

\subsection{Greedy with CGAL Library}
To take a look at the potential of the CGAL library, we decided to implement the greedy algorithm. It follows the same idea as the one previously described (Section \ref{sec:greedy}) but using completely different data structures. Thanks to the potential of the C++ code and therefore the use of objects, the CGAL library offers a very wide range of elements.\\
The installation of CGAL within the Windows environment with Visual Studio 2019 is reported in the appendix \ref{sec:cagalOne}.\\
First of all, the objects used for this algorithm are briefly explained:
\begin{enumerate}
\item \texttt{CGAL::Simple\_cartesian <double> K}: The layer of geometry kernels provides basic geometric entities of constant size and primitive operations on them. Each entity is provided as both a stand-alone class, which is parameterized by a kernel class, and as a type in the kernel class. CGAL provides different kernels, they can differ by internal representation of objects (e.g. cartesian versus homogeneous) or provide different functionalities. When creating a new package, the authors have to specify clearly the requirements needed by the kernel used and may specify a targeted kernel in the list of predefined kernels. In this project \texttt{Exact\_predicates\_inexact\_constructions\_kernel} is used. Thus, a \texttt{Simple\_cartesian} object is chosen to represent a model for a kernel using Cartesian coordinates to represent the geometric objects.
\item \texttt{CGAL::Search\_traits\_2 <K> TreeTraits}: The \texttt{Simple\_cartesian} allows to define a dD Spatial Searching which is, in this kind of problems, a 2D space.
\item \texttt{CGAL::Orthogonal\_k\_nemap.com\_search <TreeTraits> Neighbor\_search}: Thanks to the aforementioned objects, with this one a neighborhood search is created, based on the Euclidean distance in a Cartesian plane.
\item \texttt{Neighbor\_search::Tree Tree}: In our implementation was decided to use a Tree representation in which to apply the \texttt{Neighbor\_search}.
\end{enumerate}
Once the model and structure have been defined, the search is simple. Each node is created via the \texttt{Point\_2} object of the kernel defined above. The various points are assigned to the \texttt{Tree}. As a last step just pass: the \texttt{Tree}, the starting node and the number of total nodes to the \texttt{Neighbor\_search} and it return an list of nodes in ascending order of distance from the starting one.\\
By repeating this procedure for each node, a matrix of nodes sorted by distance from the starting point is obtained and with an ad hoc searching function:

\begin{lstlisting}
vector<int>::iterator it = find_if(neigh_sol_idx[succ].begin(), neigh_sol_idx[succ].end(),
					 [&](int neig) {
	return find(var->sol[i].begin(), var->sol[i].end(), neig) == var->sol[i].end();
});
\end{lstlisting}

the resulting matrix will have a number of lines equal to the number of nodes and each of them will be a tour built starting from a node and always choosing the closest from the current node, exactly as \texttt{Greedy}. \\
The algorithm, as shown in fig. \ref{fig:Lconstructives_time} , takes longer to run, as it uses higher level objects and methods than \texttt {Greedy}. In fact, its potential lies in its application in complex problems, in which these data structures offer powerful and effective tools for geometric problems.

\section{Greedy Randomize Adaptive Search Path (GRASP)}\label{sec:grasp}
GRASP heuristic, as the name anticipate, is a Greedy constructive heuristic with some randomization. Indeed, for the TSP problem, instead of bringing the closer isolated node as next, it's chosen a random one between the first $ n $ closest isolated nodes, where $ n = 3 $ in the tested implementation.
The idea is that in TSP it's not always the closer to be the next node on the optimum tour. Moreover, in other metaheuristics that start from a solution and optimize it, a percentage of randomness could appreciated.

\begin{figure}[!h]
	\centering
	\begin{subfigure}{.75\textwidth}
		\includegraphics[width=\columnwidth]{../res/Lgrasp_greedy_time.png}
		\caption{Solution time domain.}
		\label{fig:Lgrasp_greedy_time}
	\end{subfigure}
	\begin{subfigure}{.75\textwidth}
		\includegraphics[width=\columnwidth]{../res/Lgrasp_greedy_lb.png}
		\caption{Solution cost domain.}
		\label{fig:Lgrasp_greedy_lb}
	\end{subfigure}
\caption{The first parameter in the name of \texttt{n\_greedy} and \texttt{n\_grasp} is the number of different tour evaluated. In \texttt{n\_grasp} name the other parameter represent the probability to chose in order the first, the second and the third closer node.  }
\label{fig:Lgrasp_greedy}
\end{figure}
In fig. \ref{fig:Lgrasp_greedy} the result of the test with different $ n $ values for \texttt{n\_greedy} and \texttt{n\_grasp} and for different probability of choice of the next node in \texttt{n\_grasp}. There can be see the differences in execution time of \texttt{heuristic\_greedy} from the other and the gradual decrease of the cost of the \texttt{n\_grasp} by increasing the probability to select the closer node until \texttt{n\_greedy\_10}

In figure \ref{fig:att48_diff} can be compared of the optimum tour, \texttt{Greedy}, 3 different instances of \texttt{GRASP} (\texttt{n\_grasp\_1 \\ \_33\_33\_33} version) and \texttt{heuristic\_insertion}. It's nice to see that the \texttt{Greedy} (fig \ref{fig:att48_GREEDY}) is pretty close to the optimal solution and also some instance of \texttt{GRASP} like \ref{fig:att48_GRASP2}. \texttt{heuristic\_insertion} in this example has no cross edges and as will be show in fig. \ref{fig:Lgrasp_insertion_refining_LA_lb} the refining phase of this solutions does not improve so much the tour cost.
\begin{figure}[!h]
	\begin{subfigure}{.49\textwidth}
		\centering
		\includegraphics[width=\columnwidth]{../res/att48_0.png}
		\caption{Optimal subtour: cost=10628, time=0.76s}
		\label{fig:att48_best}
	\end{subfigure}
	\begin{subfigure}{.49\textwidth}
		\centering
		\includegraphics[width=\columnwidth]{../res/att48_10.png}
		\caption{\texttt{Greedy}: cost=12012, time=0.005s}
		\label{fig:att48_GREEDY}
	\end{subfigure}
	\begin{subfigure}{.49\textwidth}
	\centering
	\includegraphics[width=\columnwidth]{../res/att48_12_1.png}
	\caption{\texttt{GRASP}: cost=21824, time=0.001s}
	\label{fig:att48_GRASP1}
	\end{subfigure}
	\begin{subfigure}{.49\textwidth}
	\centering
	\includegraphics[width=\columnwidth]{../res/att48_12_2.png}
	\caption{\texttt{GRASP}: cost=12576, time=0.001s}
	\label{fig:att48_GRASP2}
	\end{subfigure}
	\begin{subfigure}{.49\textwidth}
	\centering
	\includegraphics[width=\columnwidth]{../res/att48_12_3.png}
	\caption{\texttt{GRASP}: cost=22179, time=0.001s}
	\label{fig:att48_GRASP3}
	\end{subfigure}
	\begin{subfigure}{.49\textwidth}
	\centering
	\includegraphics[width=\columnwidth]{../res/att48_13.png}
	\caption{\texttt{heuristic\_insertion}: cost=11197, time=0.0028s}
	\label{fig:att48_insertion}
	\end{subfigure}
	\caption{Differences of \texttt{Greedy} and \texttt{GRASP} tour for \textit{att48.tsp} instace.}
	\label{fig:att48_diff}
\end{figure}

\section{Insertion Heuristic}
The idea of this constructive heuristic is to create an initial simple tour of 3 nodes randomly and insert one node at a time, breaking the edge that minimize the final tour cost.\\
The complexity of the algorithm is $ O(n^2) $.

% versione 2
\textbf{Construction heuristic comparison.} The performance profile in fig \ref{fig:pp_Lconstructives} show the three constructive presented in this chapter. In particular \texttt{heristic\_insertion} is comparable with \texttt{n\_greedy} and \texttt{n\_grasp} in execution time, however it find shorter tours of all the other constructive.\\
Other consideration about the constructive heuristics in chapter \ref{chapter:refining}.

\begin{figure}[!h]
	\centering
	\begin{subfigure}{.9\textwidth}
		\centering
		\includegraphics[width=\columnwidth]{../res/Lconstructives_LA_time.png}
		\caption{Solution time domain.}
		\label{fig:Lconstructives_time}
	\end{subfigure}
	\begin{subfigure}{.9\textwidth}
	\centering
	\includegraphics[width=\columnwidth]{../res/Lconstructives_LA_lb.png}
	\caption{Solution cost domain.}
	\label{fig:Lconstructives_lb}
	\end{subfigure}
	\caption{Performance profile of constructive heuristic.}
	\label{fig:pp_Lconstructives}
\end{figure}