\section{DFS and BFS}

%%%%%%%%%%%%%%%
\begin{frame}{Classifying edges}
  \begin{definition}[Classifying edges]
    Given a DFS/BFS traversal $\Rightarrow$ DFS/BFS tree:
    \begin{itemize}
      \item Tree edge: $\to$ child
      \item Back edge: $\to$ ancestor
      \item Forward edge: $\to$ \emph{nonchild} descendant
      \item Cross edge: $\to$ neither ancestor nor descendant
    \end{itemize}
  \end{definition}

  \begin{alertblock}{Remarks:}
    \begin{itemize}
      \item applicable to both DFS and BFS
      \item w.r.t. DFS/BFS trees
    \end{itemize}
  \end{alertblock}
\end{frame}
%%%%%%%%%%%%%%%
\begin{frame}{Classifying edges}
  \begin{exampleblock}{Classifying edges \pno{3.4.1}}
    \begin{figure}
      \begin{subfigure}{0.50\linewidth}
	\centering
	\includegraphics[width=0.50\textwidth]{figures/dfs-digraph.png}
	\caption{DFS on directed graph.}
      \end{subfigure}%
      \begin{subfigure}{0.50\linewidth}
	\centering
	\includegraphics[width=0.50\textwidth]{figures/dfs-undirected.png}
	\caption{DFS on undirected graph.}
      \end{subfigure}

      \begin{subfigure}{0.50\linewidth}
	\centering
	\includegraphics[width=0.50\textwidth]{figures/bfs-digraph.png}
	\caption{BFS on directed graph.}
      \end{subfigure}%
      \begin{subfigure}{0.50\linewidth}
	\centering
	\includegraphics[width=0.60\textwidth]{figures/bfs-undirected.png}
	\caption{BFS on undirected graph.}
      \end{subfigure}
    \end{figure}
  \end{exampleblock}
\end{frame}
%%%%%%%%%%%%%%%
\begin{frame}{Classifying edges}
  \begin{exampleblock}{DFS tree and BFS tree coincide \pno{3.4.30}}
    $G = (V,E), v \in V$. DFS tree $T$ = BFS tree $T'$.

    \begin{itemize}
      \item $G$ is an undirected graph $\Rightarrow$ $G = T$.
      \item $G$ is a digraph $\Rightarrow^{?}$ $G = T$.
    \end{itemize}
  \end{exampleblock}

  \begin{block}{Solution.}
    \begin{itemize}
      \item $T$: tree + back; $T'$: tree + cross
      \item $T$: tree + back + forward + cross; $T'$: tree + back + cross
    \end{itemize}
  \end{block}
\end{frame}
%%%%%%%%%%%%%%%
\begin{frame}{Distance constraints for BFS}
  \begin{exampleblock}{Distance constraints for BFS \pno{3.4.4}}
    \begin{columns}[t]
      \column{0.40\textwidth}
        BFS on digraph:
	\begin{description}
	  \item[TE:] $d[v] = d[u] + 1$
	  \item[BE:] $0 \le d[v] \le d[u]$
	  \item[CE:] $d[v] \le d[u] + 1$
	\end{description}
      \column{0.60\textwidth}
        BFS on undirected graph:
	\begin{description}
	  \item[TE:] $d[v] = d[u] + 1$
	  \item[CE:] $d[v] = d[u] \lor d[v] = d[u] + 1$
	\end{description}
    \end{columns}
  \end{exampleblock}

  \begin{block}{Solution to ``\emph{CE} in BFS on \emph{undirected} graph''.}
    \begin{itemize}
      \item $d[v] = d[u], d[v] = d[u] + 1$
      \item $d[v] < d[u], d[v] > d[u] + 1$
    \end{itemize}
  \end{block}

  \begin{alertblock}{Remark.}
    \begin{itemize}
      \item BFS tree defines a \emph{shortest-path} from its root to every other node.
      \item Layers in BFS on \emph{undirected} graph; c.f. bipartite testing \pno{3.4.26}
    \end{itemize}
  \end{alertblock}
\end{frame}
%%%%%%%%%%%%%%%
\begin{frame}{Lifetime of vertices in DFS}
  \begin{exampleblock}{Lifetime of vertices in DFS \pno{3.4.5}}
    $\forall u,v$:
    \begin{itemize}
      \item $u$ is an ancestor of $v$: $[\text{d}[v], \text{f}[v]] \subset [\text{d}[u], \text{f}[u]]; [_{u}\; [_{v}\; ]_{v}\; ]_{u}$
      \item $u,v$ has no ancestor/descendant relation: $[\text{d}[v], \text{f}[v]] || [\text{d}[u], \text{f}[u]]$
    \end{itemize}
  \end{exampleblock}

  \begin{block}{Solution.}
    Assume $u, v \in $ DFS tree.

    \begin{itemize}
      \item $c$: least common ancestor of $u,v$ \pno{3.4.17}
      \item $c \to u' \leadsto u; c \to v' \leadsto v$
      \item $u', v'$ disjoint; $u \subset u' \land v \subset v'$
    \end{itemize}
  \end{block}

  \begin{alertblock}{Remark.}
    $\forall u,v: [_{u} \; ]_{u}, [_{v} \; ]_{v}$ \emph{disjoint} $\lor$ \emph{contained} $\Leftarrow$ a ``stack'' view.
  \end{alertblock}
\end{frame}
%%%%%%%%%%%%%%%
\begin{frame}{Preprocessing for ancestor/descendant relation}
  \begin{exampleblock}{Preprocessing for ancestor/descendant relation \pno{3.4.14}}
    \begin{itemize}
      \item binary tree $\Rightarrow$ tree $T$
      \item $r \in T$
    \end{itemize}
  \end{exampleblock}

  \begin{block}{Solution.}
    $v: \text{d}[v], \text{f}[v]$
  \end{block}

  \begin{alertblock}{Remark.}
    $\forall v$: how many descendants?

    $(\text{f}[v] - \text{d}[v] - 1) / 2$
  \end{alertblock}
\end{frame}
%%%%%%%%%%%%%%%
\begin{frame}{Edge types and lifetime of vertices in DFS}
  \begin{exampleblock}{Edge types and lifetime of vertices in DFS \pno{3.4.3}}
    $\forall u \to v$:
    \begin{itemize}
      \item tree/forward edge: $[_{u}\; [_{v}\; ]_{v}\; ]_{u}$
      \item back edge: $[_{v}\; [_{u}\; ]_{u}\; ]_{v}$
      \item cross edge: $[_{v}\; ]_{v}\; [_{u}\; ]_{u}$
    \end{itemize}
  \end{exampleblock}

  \begin{alertblock}{Remark.}
    \begin{itemize}
      \item $\text{f}[v] < \text{d}[u]$: cross edge
      \item $\text{f}[u] < \text{f}[v]$: back edge
    \end{itemize}
  \end{alertblock}
\end{frame}
%%%%%%%%%%%%%%%


\section*{Appendix}

This section provides a detailed description of the {\tt TESS3} algorithm.  The first step of the algorithm builds a nearest-neighbor graph based on the geographic coordinates of the sampling sites. The number of neighbors in the graph was set to represent $5\%$ of total connections. Then, the program runs a least-squares minimization algorithm.  In this approach, the estimates of $Q$ and $G$ are obtained after solving the following constrained least-squares problem~\citep{cai2011graph}

$$
(\hat Q, \hat G) = \arg \min {\rm LS}(Q, G) \, ,
$$

\noindent where

\begin{equation}\label{eq:LS}
{\rm LS}(Q, G) =  \|  \tilde{X} - QG \|^2_{\rm F}   +   \alpha   \sum_{s_i \sim s_j}   w_{ij} \|  Q_{i.}  - Q_{j.} \|^2   \, ,
\end{equation}

\noindent and $Q$ and $G$ are non-negative matrices such that, for all $i$ and $\ell$, we have

$$
\sum_{k=1}^K Q_{ik} = 1  \qquad \sum_{j=0}^{p} G_{i\ell}(j) = 1 \, .
$$

\noindent In this equation, $\| M \|_{\rm F}$ denotes the Frobenius norm of a matrix $M$,  $\| V \|$ is the Euclidean norm of a vector $V$, $\alpha$ is a non-negative {\it regularization parameter}. The summation on the right-hand side of the second term runs over all pairs of sites, $s_i \sim s_j$ , sharing an edge in the nearest-neighbor graph. The quantity $w_{ij}$ is a weight that decreases with geographic distance between sampling sites as follows

\begin{equation}
w_{ij} =\exp( -  d(s_i, s_j)^2 / \bar{d^2}  ) \, ,
\end{equation}

\noindent where $d$ is the Euclidean distance, and $\bar{d}$ is the average distance computed over the neighboring sites in the sample. More specifically, the weight of an edge in the nearest-neighbor graph is related to the Laplace-Beltrami operator on a manifold~\citep{belkin2003laplacian}. In the algorithm, the regularization parameter $\alpha$ is equal to $c \times nL(p+1) / \sum w_{ij}$. The default value of $c$ is $0.1\%$.

Least squares minimization is performed using the Alternating Non-negativity-constrained Least Squares (ANLS) algorithm with the active set (AS) method following the approach used in the computer program {\tt sNMF}~\citep{frichot2014fast,kim2011fast}. The ANLS-AS algorithm starts with the initialization of the $Q$ matrix, and then computes a non-negative matrix $G$ that minimizes the quantity

$${\rm LS}_1(G) = \|X - QG \|_F^2 \, .
$$

\noindent The obtained solution is normalized so that its entries satisfy the probabilistic constraints for genotypic frequencies. Given $G$, the $Q$-matrix is computed after minimizing the following quantity

$${\rm LS}_2(Q) =
\left|\left|
\left(
    \begin{array}{c}
      {\rm Vec}(\tilde{X}^T) \\
      0
    \end{array}
  \right)
   -
   \left(
   \begin{array}{c}
      {\rm Id} \otimes G^T \\
      \sqrt{\alpha} ~ \Gamma \otimes {\rm Id}
    \end{array}
  \right) {\rm Vec}(Q^T)
   \right|\right|_F^2
   \, ,
$$

\noindent where ${\rm Vec}(\tilde{X})$ denotes the vectorization of the matrix $\tilde{X}$ formed by stacking the columns of $\tilde{X}$ into a single column vector, $\Gamma$ is the Cholesky decomposition of the graph Laplacian associated with the weights of the graph~\citep{chung1997spectral}, Id is the identity matrix, and $\otimes$ is a symbol for the Kronecker product. Iterations are stopped when the relative difference between two successive values of ${\rm LS}(Q,G)$  is lower than a tolerance threshold of $\epsilon$. The default value for $\epsilon$ equals $10^{-7}$.


\newpage\section{Calculus}

	\lem{}{ \begin{align*}
			(f(x)+g(x))' = f'(x)+g'(x) \\ \\
			(f(x)g(x)) = f'(x)g(x) + f(x)g'(x) \\ \\
			(\frac{f(x)}{g(x)})' = \frac{f'(x)g(x) - f(x)g'(x)}{g(x)^2} \\ \\
			(f(g(x)))' = f'(g(x)) + g'(x)
	\end{align*} }

	\lem{The Derivative of an Odd function is always an Even function, and vice versa.}


\section{Hoare Triple Validity}

\subsection{Showing the validity of a Hoare triple using Midnight-Formula}

Proving the validity of the following hoare triple: $\{x^2 + 4x + 3 = 0\} x := x - 1 \{ x = -4 \lor x = -2 \}$

\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{figures/hoareTripleMidnight.png}
\caption{Verification Condition - Midnight Formula}
\end{figure}


\section{Introduction}
\par
If the ultimate goal is to solve linear systems of the form
$AX = B$, one must compute an $A = LDU$, $A = U^TDU$ or
$A = U^HDU$ factorization, depending on whether the matrix $A$
is nonsymmetric, symmetric or Hermitian.
$D$ is a diagonal or block diagonal matrix,
$L$ is unit lower triangular,
and $U$ is unit upper triangular.
$A$ is sparse, but the sparsity structure of $L$ and $U$ will
likely be much larger than that of $A$,
i.e., they will suffer fill-in.
It is crucial to find a permutation matrix such that the factors of
$PAP^T$ have as moderate fill-in as can be reasonably expected.
\par
To illustrate, consider a 27-point finite difference operator defined
on an $n \times n \times n$ grid.
The number of rows and columns in $A$ is $n^3$, as is the number of
nonzero entries in $A$.
Using the natural ordering, the numbers of entries in $L$ and $U$
are $O(n^5)$, and it takes $O(n^7)$ operations to compute the
factorization.
The banded and profile orderings \cite{geo81-book}
have the same complexity.
\par
Using the nested dissection ordering,
\cite{geo73-nested},
the factor storage is reduced to $O(n^4)$ and factor operations to
$O(n^6)$.
In practice, the minimum degree ordering has this same low-fill
nature, although topological counterexamples exist
\cite{ber90-mindeg}.
A unit cube is the worst case comparison between banded and profile
orderings and the minimum degree and nested dissection orderings.
But, there is still a lot to be gained by using a good permutation
when solving most sparse linear systems, and the relative gain
becomes larger as the problem size increases.
\par
This short paper is a gentle introduction to the ordering methods
--- the background as well as the specific function calls.
But finding a good ordering is not enough.
The ``choreography'' of the factorization and solves, i.e., what
data structures and computations exist, and in a parallel
environment, which thread or processor does what and when,
is as crucial.
The structure of the factor matrices, as well as the structure of the
computations is controlled by a ``front tree''.
This object is constructed directly by the {\bf SPOOLES} ordering
software, or can be created from the graph of the matrix and an
outside permutation.
Various transformations on the front tree can make a large
difference in performance.
Some knowledge of the linear system, (e.g., does it come from a 2-D
or 3-D problem? is it small or large?), coupled with some knowledge
of how to tailor a front tree, can be important to getting the best
performance from the library.
\par
Section~\ref{section:ordering} introduces some background on sparse
matrix orderings and describes the {\bf SPOOLES} ordering software.
Section~\ref{section:front-trees} presents the front tree object
that controls the factorization, and its various transformations
to improve performance.


%!TEX root = dsp_2nd_program_hw.tex

\section{Code}

\subsection{DTMF\_1.cpp --- Main function for problem 1}\label{dtmf1}
\begin{lstlisting}
// Work by Lin, Tzu-Heng
// W42, 2014011054
// Dept. of Electronic Engineering, Tsinghua University
// DSP Course Work

# include <fstream>
# include <string>
# include <stdio.h>
# include <stdlib.h>
# include <cmath>
# include <ctime>
# include <sstream>
# include <iostream>
# include <cstdlib>
# include <unistd.h>
# include <dirent.h>
# include <sys/stat.h>
# define PI 3.1415926
int const MAX_STR_LEN = 200;

# include "complex.h"
# include "dif_fft.h"
# include "find_dtmf_symbol.h"

using namespace std;

// FFT Algorithm
int main()
{
	char dir_name[100] = "./txtData1/";

    struct dirent * filename;    // return value for readdir()
    DIR * dir;                   // return value for opendir()
    dir = opendir( dir_name );
    /* read all the files in the dir ~ */
    while( ( filename = readdir(dir) ) != NULL )
    {
        // get rid of "." and ".."
        if( strcmp( filename->d_name , "." ) == 0 ||
            strcmp( filename->d_name , "..") == 0 )
            continue;
        cout << filename ->d_name <<endl;

    	char hhh[MAX_STR_LEN];
    	strcpy(hhh, dir_name);
    	strcat(hhh, (filename->d_name));
		ifstream in( hhh );
		string filename;
		string line;

		if(in) // 有该文件
		{
			double audio[60000];
			int count = 0;
			while (getline (in, line)) // line中不包括每行的换行符
			{
				// audio[count] = atof(const_cast<const char *>(line.c_str()));

				double d;
				stringstream ss(line);
				ss >> d;
				audio[count] = d;

				count = count + 1;
				// cout << audio[count-1] << endl;
			}

			// is 2^k or not, if not add zero
			int add_zero_count = 0;
			for (int i = 1; i < 100; ++i)
			{
				if ( pow(2,i) < count ) {
					continue;
				} else if ( pow(2,i) == count ) {
					break;
				} else {
					add_zero_count = pow(2,i) - count;
					break;
				}
			}

			// add value to input_seq[]
			int total_length = count + add_zero_count;
			complex* input_seq = new complex[total_length];
			for (int i = 0; i < count; i++)
			{
				input_seq[i].re = audio[i];
				input_seq[i].im = 0;
			}
			for (int i = 0; i < add_zero_count; i++)
			{
				input_seq[count + i].re = 0;
				input_seq[count + i].im = 0;
			}

			// FFT
			complex* output_seq = DIF_FFT_reordered(input_seq, total_length);

			// Amp, Find max 1,2 and their positions
			int amp;
			int max1 = 0;
			int max2 = 0;
			int max1_pos = 0;
			int max2_pos = 0;
			for (int i = 0; i < total_length/2; ++i)
			{
				amp = pow(output_seq[i].re,2) + pow(output_seq[i].im,2);
				if (amp > max2)
				{
					if (amp > max1) {
						// max2 = max1
						max2 = max1;
						max2_pos = max1_pos;
						// max1 = new
						max1 = amp;
						max1_pos = i;
					} else {
						max2 = amp;
						max2_pos = i;
					}
				}
			}

			// x-axis
				// 0:f_s/(N-1):f_s
			int fs = 8000;
			int N = total_length;
			double step = double(fs)/(N-1);
			double* x_axis = new double[N];
			for (int i = 0; i < N; ++i)
			{
				x_axis[i] = i * step;
			}

			// Get max1, max2 freq
			cout << "Max1 pos:" << x_axis[max1_pos] << endl;
			cout << "Max2 pos:" << x_axis[max2_pos] << endl;

			// decipher
			char output_symbol = find_dtmf_symbol(x_axis[max1_pos], x_axis[max2_pos]);
			cout << "The symbol for this sound: " << output_symbol << endl;

			cout << endl;

		} else { // fail reading file
			cout << "no such file" << endl;
		}
    }
	return 0;
}
\end{lstlisting}

\subsection{DTMF\_2.cpp --- Main function for problem 2}\label{dtmf2}
\begin{lstlisting}
// Work by Lin, Tzu-Heng
// W42, 2014011054
// Dept. of Electronic Engineering, Tsinghua University
// DSP Course Work

# include <fstream>
# include <string>
# include <stdio.h>
# include <stdlib.h>
# include <cmath>
# include <ctime>
# include <sstream>
# include <iostream>
# include <cstdlib>
# include <unistd.h>
# include <dirent.h>
# include <sys/stat.h>
# define PI 3.1415926
int const MAX_STR_LEN = 200;

# include "complex.h"
# include "goertzel.h"
# include "find_dtmf_symbol.h"

using namespace std;

// Goertzel Algorithm
int main()
{
	cout << "Running Prob2, using Goertzel Algorithm ... " << endl << endl;

	char dir_name[100] = "./txtData1/";

    struct dirent * filename;    // return value for readdir()
    DIR * dir;                   // return value for opendir()
    dir = opendir( dir_name );
    /* read all the files in the dir ~ */
    while( ( filename = readdir(dir) ) != NULL )
    {
        // get rid of "." and ".."
        if( strcmp( filename->d_name , "." ) == 0 ||
            strcmp( filename->d_name , "..") == 0 )
            continue;
        cout << filename ->d_name <<endl;

    	char hhh[MAX_STR_LEN];
    	strcpy(hhh, dir_name);
    	strcat(hhh, (filename->d_name));
		ifstream in( hhh );
		string filename;
		string line;

		if(in) // 有该文件
		{
			double audio[60000];
			int count = 0;
			while (getline (in, line)) // line中不包括每行的换行符
			{
				// audio[count] = atof(const_cast<const char *>(line.c_str()));

				double d;
				stringstream ss(line);
				ss >> d;
				audio[count] = d;

				count = count + 1;
				// cout << audio[count-1] << endl;
			}

			// is 2^k or not, if not add zero
			int add_zero_count = 0;
			for (int i = 1; i < 100; ++i)
			{
				if ( pow(2,i) < count ) {
					continue;
				} else if ( pow(2,i) == count ) {
					break;
				} else {
					add_zero_count = pow(2,i) - count;
					break;
				}
			}

			// add value to input_seq[]
			int total_length = count + add_zero_count;
			complex* input_seq = new complex[total_length];
			for (int i = 0; i < count; i++)
			{
				input_seq[i].re = audio[i];
				input_seq[i].im = 0;
			}
			for (int i = 0; i < add_zero_count; i++)
			{
				input_seq[count + i].re = 0;
				input_seq[count + i].im = 0;
			}

			// Goertzel, return amp^2
			double* targeted_amp = Goertzel(input_seq, total_length);

			// Amp, Find max 1,2 and their positions
			int amp;
			int max1 = 0;
			int max2 = 0;
			int max1_pos = 0;
			int max2_pos = 0;
			for (int i = 0; i < 8; ++i)
			{
				amp = targeted_amp[i];
				if (amp > max2)
				{
					if (amp > max1) {
						// max2 = max1
						max2 = max1;
						max2_pos = max1_pos;
						// max1 = new
						max1 = amp;
						max1_pos = i;
					} else {
						max2 = amp;
						max2_pos = i;
					}
				}
			}

			// Get max1, max2 freq
			double x_axis[] = 				 		{697,
											  		 770,
											  		 852,
											  		 941,
								1209,1336,1477,1633};

			cout << "Max1 pos:" << x_axis[max1_pos] << endl;
			cout << "Max2 pos:" << x_axis[max2_pos] << endl;

			// decipher
			char output_symbol = find_dtmf_symbol(x_axis[max1_pos], x_axis[max2_pos]);
			cout << "The symbol for this sound: " << output_symbol << endl;

			cout << endl;

		} else { // fail reading file
			cout << "no such file" << endl;
		}

    }
	return 0;
}
\end{lstlisting}

\subsection{DTMF\_3.cpp --- Main function for problem 3}\label{dtmf3}
\begin{lstlisting}
// Work by Lin, Tzu-Heng
// W42, 2014011054
// Dept. of Electronic Engineering, Tsinghua University
// DSP Course Work

# include <fstream>
# include <string>
# include <stdio.h>
# include <stdlib.h>
# include <cmath>
# include <ctime>
# include <sstream>
# include <iostream>
# include <cstdlib>
# include <unistd.h>
# include <dirent.h>
# include <sys/stat.h>
# define PI 3.1415926
int const MAX_STR_LEN = 200;

# include "complex.h"
# include "goertzel.h"
# include "find_dtmf_symbol.h"

using namespace std;

// Goertzel Algorithm to identify 3
int main()
{

		cout << "Identifying Prob3, a long audio, using Goertzel Algorithm ..." << endl << endl;

		ifstream in("./txtData2/data.txt");
		string filename;
		string line;

		if(in) // 有该文件
		{
			double audio[60000];
			int count = 0;
			while (getline (in, line)) // line中不包括每行的换行符
			{
				// audio[count] = atof(const_cast<const char *>(line.c_str()));

				double d;
				stringstream ss(line);
				ss >> d;
				audio[count] = d;

				count = count + 1;
				// cout << audio[count-1] << endl;
			}

			// Stops (in seconds)
			double stops[] = 	{0.4459, 0.6633,
							     0.8642, 1.072,
							     1.27,   1.473,
							     1.7,    1.89,
							     2.09,   2.29,
							     2.5,    2.722,
							     2.915,  3.1,
							     3.34,   3.549,
							     3.782,  4,
							     4.182,  4.398,
							     4.581,  4.794,
							     5.013,  5.222,
							     5.431,  5.641,
							     5.869,  6.059,
							     6.258,  6.447};

		    int len_data2 = count;
			double seconds_len_data2 = len_data2/8000.0;

			for (int d = 0; d < 15; ++d)
			{

				int start_index = stops[d*2]/seconds_len_data2*len_data2;
				int end_index = stops[d*2+1]/seconds_len_data2*len_data2;

				// Get this audio
				count = end_index - start_index + 1;
				double* this_audio = new double[count];
				for (int i = 0; i < count; ++i)
				{
					this_audio[i] = audio[i+start_index];
				}

				// is 2^k or not, if not add zero
				int add_zero_count = 0;
				for (int i = 1; i < 100; ++i)
				{
					if ( pow(2,i) < count ) {
						continue;
					} else if ( pow(2,i) == count ) {
						break;
					} else {
						add_zero_count = pow(2,i) - count;
						break;
					}
				}

				// add value to input_seq[]
				int total_length = count + add_zero_count;
				complex* input_seq = new complex[total_length];
				for (int i = 0; i < count; i++)
				{
					input_seq[i].re = this_audio[i];
					input_seq[i].im = 0;
				}
				for (int i = 0; i < add_zero_count; i++)
				{
					input_seq[count + i].re = 0;
					input_seq[count + i].im = 0;
				}

				// Goertzel, return amp^2
				double* targeted_amp = Goertzel(input_seq, total_length);

				// amp indicator
				// for (int ii = 0; ii < 8; ++ii)
				// {
				// 	cout << targeted_amp[ii] << endl;
				// }

				// Amp, Find max 1,2 and their positions
				int amp;
				int max1 = 0;
				int max2 = 0;
				int max1_pos = 0;
				int max2_pos = 0;
				for (int i = 0; i < 8; ++i)
				{
					amp = targeted_amp[i];
					if (amp > max2)
					{
						if (amp > max1) {
							// max2 = max1
							max2 = max1;
							max2_pos = max1_pos;
							// max1 = new
							max1 = amp;
							max1_pos = i;
						} else {
							max2 = amp;
							max2_pos = i;
						}
					}
				}

				// Get max1, max2 freq
				double x_axis[] = 				 		{697,
												  		 770,
												  		 852,
												  		 941,
									1209,1336,1477,1633};

				cout << "Max1 pos:" << x_axis[max1_pos] << endl;
				cout << "Max2 pos:" << x_axis[max2_pos] << endl;

				// decipher
				char output_symbol = find_dtmf_symbol(x_axis[max1_pos], x_axis[max2_pos]);
				cout << "The symbol for this sound: " << output_symbol << endl;

				cout << endl;

			}

		} else { // fail reading file
			cout << "no such file" << endl;
		}

	return 0;
}
\end{lstlisting}

\subsection{complex.h}\label{code:complex}
\begin{lstlisting}
// Work by Lin, Tzu-Heng
// W42, 2014011054
// Dept. of Electronic Engineering, Tsinghua University
// DSP Course Work

/********************************************************************/
// Complex Struct & Some Basic func

using namespace std;

typedef struct Complex
{
	double re;
	double im;
	Complex() {
		re = 0;
		im = 0;
	};
	Complex(double a,double b) {
		re = a;
		im = b;
	};
} complex;

complex* append_seq(complex seq_1[], complex seq_2[], int N);
complex* reorder_seq(complex input_seq[], int N);
complex* Calc_WN(int N);
int reverse_bit(int value, int N);

// Multiplier
complex ComplexMul(complex c1, complex c2)
{
	complex r;

	r.re = c1.re*c2.re - c1.im*c2.im;
	r.im = c1.re*c2.im + c1.im*c2.re;

	return r;
}

// Adder
complex ComplexAdd(complex c1, complex c2)
{
	complex r;

	r.re = c1.re + c2.re;
	r.im = c1.im + c2.im;

	return r;
}

// -c
complex ReverseComplex(complex c)
{
	c.re = -c.re;
	c.im = -c.im;

	return c;
}

// scalar mul
complex ComplexScalarMul(complex cc, double con)
{
	complex r;

	r.re = cc.re * con;
	r.im = cc.im * con;

	return r;
}

// Other func

/********************************************************************/
// Append [seq_1] & [seq_2] to [seq_1,seq_2]
complex* append_seq(complex seq_1[], complex seq_2[], int N) {
	complex* total_seq = new complex[N*2];
	for (int i = 0; i < N; i++) {
		total_seq[i] = seq_1[i];
	}
	for (int i = N; i < 2*N; i++) {
		total_seq[i] = seq_2[i-N];
	}
	return total_seq;
}

/********************************************************************/
// Reorder the input_seq to an order
complex* reorder_seq(complex input_seq[], int N) {

	cout << "Reorder the sequence ..." << endl;

	complex* reordered_seq = new complex[N];
	for (int i = 0; i < N; ++i)
	{
		int k = reverse_bit(i, log2(N));
		reordered_seq[k] = input_seq[i];
	}

	return reordered_seq;
}

/********************************************************************/
// Reverse Bit
	// input:
		// a decimal num,
		// N-based reverse method
	// output: a decimal num
int reverse_bit(int value, int N) {

	int ret = 0;
	int i = 0;

	while (i < N) {
		ret <<= 1;
		ret |= (value>>i) & 1;
		i++;
	}

	return ret;
}

/********************************************************************/
// Calc WN[], with N = input_N
complex* Calc_WN(int N) {

	cout << "Calculating WN[] of N = " << N << " ..." << endl;
	complex* WN = new complex[N];

	complex WN_unit; WN_unit.re = cos(2*PI/N); WN_unit.im = -sin(2*PI/N);
	WN[0].re=1; WN[0].im=0;

	for (int i = 1; i < N; ++i)
	{
		WN[i] = ComplexMul(WN[i-1], WN_unit);
	}

	return WN;
}
\end{lstlisting}

\subsection{dif\_fft.h --- FFT implementation}\label{code:fft}
\begin{lstlisting}
// Work by Lin, Tzu-Heng
// W42, 2014011054
// Dept. of Electronic Engineering, Tsinghua University
// DSP Course Work

complex* DIF_FFT_reordered(complex input_seq[], int N);
complex* DIF_FFT(complex input_seq[], int N, complex WN[], int recur_time_count);

/********************************************************************/
// DIF-FFT
	// input_seq[]:
	// N: size of input_seq
		// Must be a 2^k integer
complex* DIF_FFT_reordered(complex input_seq[], int N) {

	// Initialize
	complex* reordered_seq = new complex[N];

	// Calc WN
	complex* WN = new complex[N];
	WN = Calc_WN(N);

	// Calc DIF-FFT
	reordered_seq = DIF_FFT(input_seq, N, WN, 0);
	// Reorder
	reordered_seq = reorder_seq(reordered_seq, N);

	return reordered_seq;
}
complex* DIF_FFT(complex input_seq[], int N, complex WN[], int recur_time_count) {

	// cout << "\tDIF_FFT executed!\n"; // for validation
	// output seq
	complex* return_seq = new complex[N];

	if ( N != 2 ) {

		complex* first_half_seq = new complex[N/2];
		complex* second_half_seq = new complex[N/2];

		int k = pow(2,recur_time_count);

		// Calc
		for (int i = 0; i < N/2; ++i) {
			first_half_seq[i] = ComplexAdd(input_seq[i], input_seq[i+N/2]) ;
		}
		for (int i = 0; i < N/2; ++i) {
			second_half_seq[i] = ComplexMul( ComplexAdd(input_seq[i], ReverseComplex(input_seq[i+N/2])), WN[i*k] ) ;
		}

		// DFT
		complex* DFTed_first_half_seq = new complex[N/2];
		DFTed_first_half_seq = DIF_FFT(first_half_seq, N/2, WN, recur_time_count+1);
		complex* DFTed_second_half_seq = new complex[N/2];
		DFTed_second_half_seq = DIF_FFT(second_half_seq, N/2, WN, recur_time_count+1);

		// Append [DFTed_first_half_seq] & [DFTed_second_half_seq]
		return_seq = append_seq(DFTed_first_half_seq, DFTed_second_half_seq, N/2);
		return return_seq;

	} else if ( N == 2 ) { // Smallest Butterfly Unit

		// cout << "\tDIF_FFT N==2 triggered!\n"; // for validation
		return_seq[0] = ComplexAdd(input_seq[0], input_seq[1]);
		return_seq[1] = ComplexMul( ComplexAdd(input_seq[0], ReverseComplex(input_seq[1])), WN[0] );
		return return_seq;

	}

	// return [return_seq] # unordered
	return return_seq;
}
/********************************************************************/
\end{lstlisting}

\subsection{goertzel.h --- Goertzel implementation}\label{code:goertzel}
\begin{lstlisting}
// Work by Lin, Tzu-Heng
// W42, 2014011054
// Dept. of Electronic Engineering, Tsinghua University
// DSP Course Work

// input x[n], N
// output: amp of the targeted 8 freq (freqs)

double* Goertzel(complex input_seq[], int N) {

	cout << "Calculating Goertzel ..." << endl;
	// targeted freqs
	double freqs[] = 				 		{697,
									  		 770,
									  		 852,
									  		 941,
						1209,1336,1477,1633};

	// Calc WN
	complex* WN = new complex[N];
	WN = Calc_WN(N);

	int sampling_rate = 8000;

	// Calc DFT of targeted 8 freqs
	complex targeted_X[8];
	complex* v = new complex[N];

	for (int i = 0; i < 8; ++i)
	{
		int k = ( N * freqs[i] ) / sampling_rate;
		double w_k = 2 * PI * k / N;

		// init
		v[0] = input_seq[0];
		v[1] = ComplexAdd(input_seq[1], ComplexScalarMul(v[0], 2*cos(w_k)));

		for (int j = 2; j < N; ++j)
		{
			v[j] = ComplexAdd(ComplexAdd(input_seq[j], ComplexScalarMul(v[j-1], 2*cos(w_k))), ReverseComplex(v[j-2]));
		}

		targeted_X[i] = ComplexAdd(v[N-1], ReverseComplex(ComplexMul(WN[k], v[N-2])));
	}

	// Calc amp
	double* amp_targeted_X = new double[8];
	for (int i = 0; i < 8; ++i)
	{
		amp_targeted_X[i] = pow(targeted_X[i].re,2) + pow(targeted_X[i].im,2);
		// cout << amp_targeted_X[i] << endl; // indicator
	}

	return amp_targeted_X;
}
\end{lstlisting}

\subsection{find\_dtmf\_symbol.h --- judge signals}\label{code:finddtmf}
\begin{lstlisting}
// Work by Lin, Tzu-Heng
// W42, 2014011054
// Dept. of Electronic Engineering, Tsinghua University
// DSP Course Work

// Decipher
char find_dtmf_symbol(double a, double b)
{
	char symbol[]	 =  {'1','2','3','A',
						 '4','5','6','B',
						 '7','8','9','C',
						 '*','0','#','D'};
	double freqs1[] = 				 		{697,
									  		 770,
									  		 852,
									  		 941};
	double freqs2[] = {1209,1336,1477,1633};

	// switch if a > b, making sure a < b
	if (a > b) {
		int c = a;
		a = b;
		b = c;
	}

	// get a pos: x
	int x = 100;
	for (int i = 0; i < 4; ++i)
	{
		if ( abs(freqs1[i] - a) <= 10 )
		{
			x = i;
			break;
		}
	}

	// get b pos: y
	int y = 100;
	for (int i = 0; i < 4; ++i)
	{
		if ( abs(freqs2[i] - b) <= 10 )
		{
			y = i;
			break;
		}
	}

	if (x == 100 || y == 100)
	{
		char error_char = 'x';
		return error_char;
	}
	// get return symbol
	char return_symbol = symbol[ x*4 + y ];

	return return_symbol;
}
\end{lstlisting}

\subsection{read\_wav.m --- Convert .wav files to .txt}\label{code:readwav}
\begin{lstlisting}
// Work by Lin, Tzu-Heng
// W42, 2014011054
// Dept. of Electronic Engineering, Tsinghua University
// DSP Course Work

close all;clc;clear;

%% Data1
dirpath = './Data1/';
writepath = './txtData1/';
dat = dir( dirpath );
for j = 1 : length( dat )
    if( ~isequal( dat( j ).name, '.' ) &...
        ~isequal( dat( j ).name, '..') )
        datpath = [dirpath dat( j ).name];
        [y, Fs] = audioread(datpath);
        f = fopen([writepath dat( j ).name(1:8) '.txt'],'w');
        fprintf(f, '%f\n', y);
        fclose(f);
    end
end

%% Data2
datpath = './Data2/data.wav';
[y, Fs] = audioread(datpath);
f = fopen('./txtData2/data.txt','w');
fprintf(f, '%f\n', y);
fclose(f);
\end{lstlisting}


% Created 2021-11-23 Tue 09:03
% Intended LaTeX compiler: pdflatex
\documentclass[presentation,aspectratio=1610]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{khpreamble, euscript}
\DeclareMathOperator{\atantwo}{atan2}
\newcommand*{\ctrb}{\EuScript{C}}
\newcommand*{\obsv}{\EuScript{O}}
\usetheme{default}
\author{Kjartan Halvorsen}
\date{\today}
\title{Discrete-time State feedback}
\hypersetup{
 pdfauthor={Kjartan Halvorsen},
 pdftitle={Discrete-time State feedback},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 26.3 (Org mode 9.4.6)},
 pdflang={English}}
\begin{document}

\maketitle

\section{Discret-time state space model}
\label{sec:org94bda5c}
\begin{frame}[label={sec:org2a875e6}]{The discrete-time state-space model}
\end{frame}

\begin{frame}[label={sec:orgd0007cb}]{The discrete-time state-space model}
\begin{center}
  \begin{tikzpicture}[node distance=42mm, block/.style={inner sep=6pt, rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]
    \node[coordinate] (input) {};
    \node[block, right of=input, align=center] (plant)  {$x(k+1) = \Phi x(k) + \Gamma u(k)$\\$y(k) = C x(k)$};
    \node[coordinate, right of=plant] (output) {};

    \draw[->] (input) -- node[above, pos=0.3] {$u(k)$} (plant);
    \draw[->] (plant) -- node[above, near end] {$y(k)$} (output);
  \end{tikzpicture}
\end{center}
\end{frame}

\section{PMSM - sysid}
\label{sec:org8f4c529}

\begin{frame}[label={sec:org4fc96f8}]{Obtain state-space model from discrete-time pulse-transfer function}
\end{frame}

\begin{frame}[label={sec:orgb5bf028}]{The permanent magnet synchronous motor}
\begin{center}
\includegraphics[width=0.9\linewidth]{../../figures/permanent-motor.jpg}
\end{center}
\end{frame}

\begin{frame}[label={sec:orgb21b305}]{The PMSM}
\begin{center}
\includegraphics[width=0.8\linewidth]{../../figures/pmsm_control_block_diag.png}
\end{center}
{\footnotesize Liu and Li  ``Speed control for PMSM servo system'', IEEE Transactions on Industrial Electronics, 2012.}
\end{frame}
\begin{frame}[label={sec:org4b44d9c}]{Identified model}
Two poles, two zeros, one delay
\begin{center}
  \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=10mm}, sumnode/.style={circle, draw, inner sep=2pt}]

    \node[coordinate] (input) {};
    \node[block, right of=input] (delay1)  {$\frac{1}{z}$};
    \node[block, right of=delay1, node distance=30mm] (plant)  {$\frac{b_0z^2 + b_1z + b_2}{z^2 + a_1 z + a_2}$};
    \node[coordinate, right of=plant] (output) {};

    \draw[->] (input) -- node[above, pos=0.3] {$u(k)$} (delay1);
    \draw[->] (delay1) -- node[above, pos=0.3] {} (plant);
    \draw[->] (plant) -- node[above, near end] {$y(k)$} (output);

    \begin{scope}[yshift=-1cm, xshift = 3cm]
    \node {$\Updownarrow$};
    \end{scope}
    \begin{scope}[yshift=-3cm, xshift = 3cm]
    \node {$\Updownarrow$};
    \end{scope}

    \node[coordinate, below of=input, node distance=2cm] (input2) {};
    \node[block, right of=input2, node distance=30mm] (plant)  {$\frac{b_0z^2 + b_1z + b_2}{z^2 + a_1 z + a_2}$};
    \node[block, right of=plant] (delay2)  {$\frac{1}{z}$};
    \node[coordinate, right of=delay2] (output) {};

    \draw[->] (input2) -- node[above, pos=0.3] {$u(k)$} (plant);
    \draw[->] (plant) -- node[above, pos=0.3] {} (delay2);
    \draw[->] (delay2) -- node[above, near end] {$y(k)$} (output);

    \node[coordinate, below of=input2, node distance=2cm] (input3) {};
    \node[block, right of=input3, node distance=30mm] (plant)  {$\frac{b_0z^2 + b_1z + b_2}{z(z^2 + a_1 z + a_2})$};
    \node[coordinate, right of=plant, node distance=30mm] (output) {};

    \draw[->] (input3) -- node[above, pos=0.3] {$u(k)$} (plant);
    \draw[->] (plant) -- node[above, near end] {$y(k)$} (output);

  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[label={sec:org50003c7}]{Identified model}
\[ H(z) = \frac{6.91z^2 + 16.48z -17.87}{z(z^2 - 1.766z + 0.7665)} = \frac{6.91(z+3.19)(z-0.81)}{z(z-0.998)(z-0.768)}\]
\end{frame}

\begin{frame}[label={sec:orge0a0d61}]{From pulse-transfer function to state space model}
\begin{center}
  \begin{tikzpicture}[node distance=32mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

    \node[coordinate] (input) {};
    \node[block, right of=input] (plant)  {$H(z) = \frac{b_0z^2 + b_1z + b_2}{z(z^2 + a_1 z + a_2)}$};
    \node[coordinate, right of=plant] (output) {};

    \draw[->] (input) -- node[above, pos=0.3] {$u(k)$} (plant);
    \draw[->] (plant) -- node[above, near end] {$y(k)$} (output);

    \begin{scope}[yshift=-2cm, xshift = 3cm]
    \node {$\Updownarrow$};
    \end{scope}

    \begin{scope}[yshift=-4cm, node distance=50mm, xshift=-2cm]
    \node[coordinate] (input) {};
    \node[block, right of=input, align=center] (plant)  {$x(k+1) = \Phi x(k) + \Gamma u(k)$\\$y(k) = C x(k)$};
    \node[coordinate, right of=plant] (output) {};

    \draw[->] (input) -- node[above, pos=0.3] {$u(k)$} (plant);
    \draw[->] (plant) -- node[above, near end] {$y(k)$} (output);
    \end{scope}

  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[label={sec:org82965d6}]{Canonical forms}
Given pulse-transfer function
\[ H(z) = \frac{b_1 z^2 + b_2 z + b_3}{z^3 + a_1z^2 + a_2z + a_3}.\]
Find a representation in state space form
\begin{align*}
 x(k+1) &= \Phi x(k) + \Gamma u(k) \\
 y(k) &= C x(k)
 \end{align*}

\pause

\begin{itemize}
\item Controlable canonical form
\item Observable canonical form
\end{itemize}
\end{frame}

\begin{frame}[label={sec:org11e0a2a}]{Controllable canonical form}
Given pulse-transfer function
\[ H(z) = \frac{b_1 z^2 + b_2 z + b_3}{z^3 + a_1z^2 + a_2z + a_3}.\]

\begin{align*}
 x(k+1) &= \begin{bmatrix} -a_1 & -a_2 & -a_3\\1 & 0 & 0\\0 & 1 & 0\end{bmatrix} x(k) + \begin{bmatrix}1\\0\\0\end{bmatrix} u(k) \\
 y(k) &= \begin{bmatrix} b_1 & b_2 & b_3 \end{bmatrix} x(k)
 \end{align*}
\end{frame}

\begin{frame}[label={sec:orgc102ae9}]{Observable canonical form}
Given pulse-transfer function
\[ H(z) = \frac{b_1 z^2 + b_2 z + b_3}{z^3 + a_1z^2 + a_2z + a_3}.\]

\begin{align*}
 x(k+1) &= \begin{bmatrix} -a_1 & 1 & 0\\-a_2 & 0 & 1\\-a_3 & 0 & 0\end{bmatrix} x(k) + \begin{bmatrix}b_1\\b_2\\b_3\end{bmatrix} u(k) \\
 y(k) &= \begin{bmatrix} 1 & 0 & 0 \end{bmatrix} x(k)
 \end{align*}
\end{frame}

\begin{frame}[label={sec:org29c6710}]{Canonical forms}
\alert{Activity} Find the controllable canonical form for the pulse-transfer function of the motor (needed for question 2 on the exercises).

\[ H(z) = \frac{6.91z^2 + 16.48z -17.87}{z(z^2 - 1.766z + 0.7665)} = \frac{6.91(z+3.19)(z-0.81)}{z(z-0.998)(z-0.768)}\]

\pause

\begin{center}
  \includegraphics[width=.6\linewidth]{../../figures/discrete-controllable.png}
\end{center}
\end{frame}

\section{Apollo moon lander}
\label{sec:org5cca4ce}
\begin{frame}[label={sec:org9930f8c}]{Discretizing a continuous-time state-space model}
\end{frame}

\begin{frame}[label={sec:orgd570eaa}]{Example - The Apollo lunar module}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/fig-apollo}
\end{center}
\end{frame}

\begin{frame}[label={sec:orgd51ff9a}]{Example - The Apollo lunar module}
State variables: \(x = \begin{bmatrix} x_1 & x_2 & x_3 \end{bmatrix}^T = \begin{bmatrix} \dot{\theta} & \theta & \dot{z} \end{bmatrix}^T\). With dynamics
\[ \begin{cases} \dot{x}_1 =  \ddot{\theta} = \frac{1}{J} u\\ \dot{x}_2 = \dot{\theta} = x_1\\ \dot{x}_3 = \ddot{z} = g\theta = gx_2 \end{cases} \]

\[ \dot{x} = \begin{bmatrix} \dot{x}_1\\\dot{x}_2\\\dot{x}_3\end{bmatrix} = \underbrace{\begin{bmatrix} \textcolor{red!60!black}{0} & \textcolor{red!60!black}{0} &\textcolor{red!60!black}{0} \\\textcolor{red!60!black}{1} & \textcolor{red!60!black}{0}& \textcolor{red!60!black}{0}\\ \textcolor{red!60!black}{0}& \textcolor{red!60!black}{g} &\textcolor{red!60!black}{0} \end{bmatrix}}_{A} \begin{bmatrix} x_1\\x_2\\x_3\end{bmatrix} + \underbrace{\begin{bmatrix} \textcolor{red!60!black}{\frac{1}{J}} \\ \textcolor{red!60!black}{0} \\\textcolor{red!60!black}{0}  \end{bmatrix}}_{B} u \]
\end{frame}

\section{Discretization}
\label{sec:org1f5260c}

\begin{frame}[label={sec:org818e46f}]{Discretization}
The general solution to a linear, continuous-time state-space system
\begin{align*}
x(t_k+\tau)& = \mathrm{e}^{A(\tau)} x(t_k) + \int_{0}^\tau \mathrm{e}^{As} B u\big((t_k+\tau)-s) ds
\end{align*}

\pause

\begin{center}
  \begin{tikzpicture}
    \draw[->] (-3,0) -- (6,0) node[below] {$t$};
    \draw (-2, 0.2) -- ( -2, 0) node[below] {$t_k=kh$};
    \draw (1, 0.2) -- ( 1, 0) node[below] {$t_{k+1}=kh+h$};
    \draw (4, 0.2) -- ( 4, 0) node[below] {$kh+2h$};
    \draw[thick, orange!90!black] (-3,0.3) -- (-2, 0.3) -- (-2,1) -- (1, 1) -- (1,0.8) -- (4, 0.8) --(4, 0.5) --(5.5, 0.5) node[pos=0.1, coordinate, pin=30:{$u(t)$}] {} ;
    \draw[->] (-2, -0.7) -- (0, -0.7) node[below] {$\tau$};
  \end{tikzpicture}
\end{center}

\pause

 \begin{align*}
  x(kh+h) &= \mathrm{e}^{Ah} x(kh) + \int_{0}^{h} \mathrm{e}^{As} B u(kh+h-s) ds\\
   &= \underbrace{\mathrm{e}^{Ah}}_{\Phi(h)} x(kh) + \underbrace{\left(\int_{0}^h \mathrm{e}^{As} B ds \right)}_{\Gamma(h)} u(kh)
\end{align*}
\end{frame}

\begin{frame}[label={sec:org8a9a709}]{Discretization - The matrix exponential}
\begin{center}
  \includegraphics[width=.7\linewidth]{../../figures/dubious.png}
\end{center}
\end{frame}

\begin{frame}[label={sec:orgf9cef9f}]{Discretization - The matrix exponential}
Square matrix \(A\). Scalar variable \(t\).
\[ \mathrm{e}^{At} = I + At + \frac{t^2}{2!}A^2 + \frac{t^3}{3!} A^3 + \cdots\]
Laplace transform
\[ \laplace{\mathrm{e}^{At}} = (sI - A)^{-1}\]
\end{frame}

\begin{frame}[label={sec:org03ba997}]{Discretization - example}
\small

 \begin{align*}
  x(kh+h) &= \mathrm{e}^{Ah} x(kh) + \int_{0}^{h} \mathrm{e}^{As} B u(kh+h-s) ds\\
   &= \underbrace{\mathrm{e}^{Ah}}_{\Phi(h)} x(kh) + \underbrace{\left(\int_{0}^h \mathrm{e}^{As} B ds \right)}_{\Gamma(h)} u(kh)
\end{align*}
\[ A = \begin{bmatrix} 0 & 0 & 0\\1 & 0 & 0\\0 & g & 0\end{bmatrix}, \quad A^2 = \begin{bmatrix} 0 & 0 & 0\\1 & 0 & 0\\0 & g & 0\end{bmatrix}\begin{bmatrix} 0 & 0 & 0\\1 & 0 & 0\\0 & g & 0\end{bmatrix}= \begin{bmatrix} 0 & 0 & 0\\0 & 0 & 0\\g & 0  & 0\end{bmatrix}, \quad A^3 = 0\]
So,
\begin{align*}
 \Phi(h) &= \mathrm{e}^{Ah} = I + Ah + A^2 h^2/2  + \cdots \\
\end{align*}

\pause

\begin{align*}
 \Phi(h) &= \begin{bmatrix} 1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{bmatrix} + \begin{bmatrix} 0 & 0 & 0\\1 & 0 & 0\\0 & g & 0\end{bmatrix}h + \begin{bmatrix} 0 & 0 & 0\\0 & 0 & 0\\g & 0 & 0\end{bmatrix}\frac{h^ 2}{2}= \begin{bmatrix} 1 & 0 & 0\\h & 1 & 0\\\frac{h^2g}{2} & hg & 1\end{bmatrix}
 \end{align*}
\end{frame}

\begin{frame}[label={sec:org568062b}]{Discretization - example}
 \begin{align*}
  x(kh+h) &= \mathrm{e}^{Ah} x(kh) + \int_{0}^{h} \mathrm{e}^{As} B u(kh+h-s) ds\\
   &= \underbrace{\mathrm{e}^{Ah}}_{\Phi(h)} x(kh) + \underbrace{\left(\int_{0}^h \mathrm{e}^{As} B ds \right)}_{\Gamma(h)} u(kh)
\end{align*}
\[\mathrm{e}^{As}B &=  \begin{bmatrix} 1 & 0 & 0\\s & 1 & 0\\\frac{s^2g}{2} & sg & 1\end{bmatrix} \begin{bmatrix} \frac{1}{J}\\0\\0 \end{bmatrix} = \frac{1}{J} \begin{bmatrix} 1\\s\\\frac{gs^2}{2} \end{bmatrix}
  \]
\begin{align*}
\Gamma (h) &= \int_0^h \mathrm{e}^{As}B ds = \frac{1}{J} \int_0^h \begin{bmatrix} 1\\s\\\frac{gs^2}{2} \end{bmatrix}ds = \frac{1}{J}\begin{bmatrix} h\\ \frac{h^2}{2} \\ \frac{g h^3}{6} \end{bmatrix}
\end{align*}
\end{frame}

\begin{frame}[label={sec:orgb414f0f}]{Discretization - example}
 \begin{align*}
  x(kh+h) &= \mathrm{e}^{Ah} x(kh) + \int_{0}^{h} \mathrm{e}^{As} B u(kh+h-s) ds\\
   &= \underbrace{\mathrm{e}^{Ah}}_{\Phi(h)} x(kh) + \underbrace{\left(\int_{0}^h \mathrm{e}^{As} B ds \right)}_{\Gamma(h)} u(kh)\\
   &= \begin{bmatrix} 1 & 0 & 0\\h & 1 & 0\\\frac{h^2g}{2} & hg & 1\end{bmatrix} x(kh) + \frac{1}{J} \begin{bmatrix} h\\ \frac{h^2}{2} \\ \frac{g h^3}{6} \end{bmatrix} u(kh)
\end{align*}
\end{frame}

\section{Stability}
\label{sec:org3e7227a}
\begin{frame}[label={sec:org68f96c3}]{Stability}
\end{frame}
\begin{frame}[label={sec:org6d21b1f}]{Eigenvalues and eigenvectors}
\alert{Definition} The eigenvalues \(\lambda_i  \in \mathbb{R}\) and eigenvectors \(v_i \in \mathbb{R}^n\) of a matrix \(\Phi \in \mathbb{R}^{n\times{}n}\) are the \(n\) pairs \((\lambda_i, v_i \neq 0 ), \; i=1,2,\ldots,n\) that satisfy
\[ \Phi v_i = \lambda_i v_i \]
\end{frame}

\begin{frame}[label={sec:orgd9a48bb}]{Stability}
The system
\begin{equation*}
x(k+1)=\Phi x(k), \ \ x(0)=x_0
\end{equation*}
is \alert{stable} if  \(\underset{t\to\infty}{\lim}x(kh)=0, \quad \forall\;  x_0\in\Bbb{R}^n\).

A necessary and sufficient requirement for stability is that \alert{all the eigenvalues of \(\Phi\) are inside the unit circle.}

The \alert{eigenvalues} of \(\Phi\) are the  \alert{poles} of the system.
\end{frame}

\section{State feedback}
\label{sec:org75d4b43}
\begin{frame}[label={sec:orgc029191}]{State feedback control}
\end{frame}
\begin{frame}[label={sec:orgb5b6f23}]{State feedback control}
Given
 \begin{equation}
 \begin{split}
  x(k+1) &= \Phi x(k) + \Gamma u(k)\\
  y(k) &= C x(k)
 \end{split}
 \label{eq:ssmodel}
\end{equation}
and measurements (or an estimate) of the state vector \(x(k)\).

\alert{Linear state feedback} is the control law
\begin{equation*}
\begin{split}
 u(k) &= f\big((x(k), u_c(k)\big) = -\textcolor{morange}{l_1}x_1(k) - \textcolor{morange}{l_2}x_2(k) - \cdots - \textcolor{morange}{l_n} x_n(k) + \textcolor{mbluegreen}{l_0}u_c(k)\\
      &= -\textcolor{morange}{L}x(k) + \textcolor{mbluegreen}{l_0}u_c(k),
\end{split}
\end{equation*}
where \[ \textcolor{morange}{L} = \bbm \textcolor{morange}{l_1} & \textcolor{morange}{l_2} & \cdots & \textcolor{morange}{l_n} \ebm. \]
Substituting this in the state-space model \eqref{eq:ssmodel} gives
 \begin{equation}
 \begin{split}
  x(k+1) &= \left(\Phi -\Gamma \textcolor{morange}{L} \right) x(k) + \textcolor{mbluegreen}{l_0}\Gamma u_c(k)\\
  y(k) &= C x(k)
 \end{split}
 \label{eq:closedloop}
\end{equation}
\end{frame}

\begin{frame}[label={sec:org1f6fa3e}]{Pole placement by state feedback}
Given (or choosing) a desired placement of the closed-loop poles \(p_1, p_2, \ldots, p_n\), being roots of the desired characteristic polynomial
\begin{equation}
a_c(z) = (z-p_1)(z-p_2)\cdots(z-p_n) = z^n + \alpha_1 z^{n-1} + \cdots \alpha_n.
\label{eq:desiredpoles}
\end{equation}

\pause

Linear state feedback gives the system
 \begin{equation}
 \begin{split}
  x(k+1) &= \left(\Phi -\Gamma \textcolor{morange}{L} \right) x(k) + \textcolor{mbluegreen}{l_0}\Gamma u_c(k)
 \end{split}
 \label{eq:closedloop}
\end{equation}
with characteristic polynomial
\begin{equation}
\det\left(zI - (\Phi - \Gamma \textcolor{morange}{L})\right) = z^n + \beta_1(\textcolor{morange}{l_1},\ldots,\textcolor{morange}{l_n}) z^{n-1} + \cdots \beta_n(\textcolor{morange}{l_1}, \ldots, \textcolor{morange}{l_n}).
\label{eq:poles}
\end{equation}

\pause

Set the coefficients of the desired characteristic polynomial \eqref{eq:desiredpoles} equal to the coefficients of \eqref{eq:poles} to obtain the system of equations
\begin{equation*}
\begin{split}
\beta_1(\textcolor{morange}{l_1}, \ldots, \textcolor{morange}{l_n}) &= \alpha_1\\
\beta_2(\textcolor{morange}{l_1}, \ldots, \textcolor{morange}{l_n}) &= \alpha_2\\
&\vdots\\
\beta_n(\textcolor{morange}{l_1}, \ldots, \textcolor{morange}{l_n}) &= \alpha_n
\end{split}
\label{eq:coeffs}
\end{equation*}
\end{frame}

\begin{frame}[label={sec:orgbacb088}]{Pole placement by state feedback}
The system of equations
\begin{equation*}
\begin{split}
\beta_1(\textcolor{morange}{l_1}, \ldots, \textcolor{morange}{l_n}) &= \alpha_1\\
\beta_2(\textcolor{morange}{l_1}, \ldots, \textcolor{morange}{l_n}) &= \alpha_2\\
&\vdots\\
\beta_n(\textcolor{morange}{l_1}, \ldots, \textcolor{morange}{l_n}) &= \alpha_n
\end{split}
\label{eq:coeffs}
\end{equation*}

is always linear in the parameters of the controller, hence
\begin{equation*}
M \textcolor{morange}{L}\transp = \alpha,
\end{equation*}
where \(\alpha\transp = \bbm \alpha_1 & \alpha_2 & \cdots & \alpha_n \ebm.\)
\end{frame}

\begin{frame}[label={sec:org86c6a79},fragile]{Pole placement by state feedback}
 Given a desired placement of the closed-loop poles \(p_1, p_2, \ldots, p_n\), being roots of the desired characteristic polynomial
\begin{equation*}
a_c(z) = (z-p_1)(z-p_2)\cdots(z-p_n) = z^n + \alpha_1 z^{n-1} + \cdots \alpha_n.
\label{eq:desiredpoles}
\end{equation*}
and closed-loop system
 \begin{equation*}
 \begin{split}
  x(k+1) &= \left(\Phi -\Gamma \textcolor{morange}{L} \right) x(k) + \textcolor{mbluegreen}{l_0}\Gamma u_c(k)\\
  y(k) &= C x(k)
 \end{split}
 \label{eq:closedloop}
\end{equation*}

The Matlab (\emph{control systems toolbox}) has methods for computing the gain vector \(L\)

\begin{enumerate}
\item \alert{Ackerman's method}
\begin{verbatim}
L = acker(Phi, Gamma, pd)
\end{verbatim}
\item \alert{Numerically more stable method}
\begin{verbatim}
L = place(Phi, Gamma, pd)
\end{verbatim}
\end{enumerate}
\end{frame}

\begin{frame}[label={sec:org41f1c99}]{The reference input gain \(l_0\)}
The closed-loop state space system
\begin{equation*}
\begin{split}
 x(k+1) &= \underbrace{\left(\Phi -\Gamma \textcolor{morange}{L} \right)}_{\Phi_c} x(k) + \textcolor{mbluegreen}{l_0}\Gamma u_c(k)\\
 y(k) &= C x(k)
\end{split}
\end{equation*}
with constant reference signal \(u_c(k) = u_{c,f}\) has the steady-state solution (\(x(k+1)=x(k)\))
\pause
\[ x_f =  \textcolor{mbluegreen}{l_0} (I - \Phi_c)^{-1}\Gamma u_{c,f}\]
\[ y_f = Cx_f = \textcolor{mbluegreen}{l_0} C(I - \Phi_c)^{-1}\Gamma u_{c,f}.\]
We want \(y_f =  u_{c,f}\),
\[ \Rightarrow \qquad \textcolor{mbluegreen}{l_0} = \frac{1}{C(I-\Phi_c)^{-1}\Gamma}\]
\end{frame}
\end{document}


\documentclass{beamer}
\usepackage{beamerthemesplit}
\usepackage{coqdoc}

\title{Coq: it has an unfortunate name}
\author{Dan Colish}
\date{\today}

\begin{document}
\frame{\titlepage}

\section{Intro}
\subsection{What is Coq?}
\frame
{
  \frametitle{Why Prove anything at all?}

  \begin{itemize}
    \item Formally state mathematical theories
    \item Define predicate functions that can be evaluated systematically
    \item Verifies that a program does what we expect it to.
  \end{itemize}
}

\frame {
  \frametitle{Computer Checking Proofs}
  \begin{itemize}
    \item Hand-written proofs are subject to mistakes
    \item Considered to be informal since there is no formal method of
      checking
    \item Coq allows us to systematically verify proofs using the
      Calculus of Inductive Constructions
    \item All decisions about Coq proofs are resolved using very
      strong type checking
   \end{itemize}
}

\frame {
  \frametitle{So What's it look like?}
    Here's something simple.

  \begin{coqdoccode}
\scriptsize{   \coqdockw{Theorem} \coqlemma{acm.sillyprop}{silly\_prop} :forall (\coqdocvar{A} \coqdocvar{B} \coqdocvar{C}: \coqdockw{Prop}), \coqdoceol
\coqdocindent{1.00em}
(\coqdocvar{A} \ensuremath{\rightarrow} (\coqdocvar{B} \ensuremath{\rightarrow} \coqdocvar{C})) \ensuremath{\rightarrow} (\coqdocvar{A} \ensuremath{\rightarrow} \coqdocvar{B}) \ensuremath{\rightarrow} (\coqdocvar{A} \ensuremath{\rightarrow} \coqdocvar{C}).\coqdoceol
\coqdocnoindent
\coqdockw{Proof}.\coqdoceol
\coqdocindent{1.00em}
\coqdoctac{intros} \coqdocvar{A} \coqdocvar{B} \coqdocvar{C} \coqdocvar{H1} \coqdocvar{HA} \coqdocvar{H2}.\coqdoceol
\coqdocindent{1.00em}
\coqdoctac{apply} \coqdocvar{H1}.\coqdoceol
\coqdocindent{1.00em}
\coqdoctac{apply} \coqdocvar{H2}.\coqdoceol
\coqdocindent{1.00em}
\coqdoctac{apply} \coqdocvar{HA}.\coqdoceol
\coqdocindent{1.00em}
\coqdoctac{assumption}.\coqdoceol
\coqdocnoindent
\coqdockw{Qed}.\coqdoceol
}\end{coqdoccode}

And a little more complex...

\begin{coqdoccode}
\scriptsize{
\coqdocnoindent
\coqdockw{Theorem} \coqdocvar{ex1} : \ensuremath{\forall} (\coqdocvar{A} \coqdocvar{B} \coqdocvar{C} \coqdocvar{D}:Prop),\coqdoceol
\coqdocindent{1.00em}
(\coqdocvar{A} \ensuremath{\lor} \coqdocvar{B}) \ensuremath{\rightarrow} (\coqdocvar{B} \ensuremath{\land} \coqdocvar{C}) \ensuremath{\rightarrow} (\coqdocvar{B} \ensuremath{\rightarrow} \coqdocvar{C}) \ensuremath{\lor} \coqdocvar{D}.\coqdoceol
\coqdocnoindent
\coqdockw{Proof}.\coqdoceol
\coqdocnoindent
\coqdoctac{intros}.\coqdoceol
\coqdocnoindent
\coqdoctac{destruct} \coqdocvar{H0}.\coqdoceol
\coqdocnoindent
\coqdocvar{left}.\coqdoceol
\coqdocnoindent
\coqdoctac{intros}.\coqdoceol
\coqdocnoindent
\coqdoctac{apply} \coqdocvar{H1}.\coqdoceol
\coqdocnoindent
\coqdockw{Qed}.\coqdoceol
}\end{coqdoccode}
}

\frame {
  \frametitle{That's cool, now what?}
  Now we can start reasoning about programs. Lets say we have the following functions on lists.

First we need to tell Coq how to construct a list \begin{coqdoccode}
\scriptsize{
\coqdocnoindent
\coqdockw{Inductive} \coqinductive{simplelists.natlist}{natlist} : \coqdockw{Type} :=\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqconstructor{simplelists.nil}{nil} : \coqinductiveref{simplelists.natlist}{natlist}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqconstructor{simplelists.cons}{cons} : \coqinductiveref{Coq.Init.Datatypes.nat}{nat} \ensuremath{\rightarrow} \coqinductiveref{simplelists.natlist}{natlist} \ensuremath{\rightarrow} \coqinductiveref{simplelists.natlist}{natlist}.\coqdoceol
\coqdocemptyline
}\end{coqdoccode}
Using this constructor we can then define lists \begin{coqdoccode} \scriptsize{
\coqdocemptyline
\coqdocnoindent
\coqdockw{Definition} \coqdefinition{simplelists.l123}{l\_123} := \coqconstructorref{simplelists.cons}{cons} 1 (\coqconstructorref{simplelists.cons}{cons} 2 (\coqconstructorref{simplelists.cons}{cons} 3 \coqconstructorref{simplelists.nil}{nil})).\coqdoceol
\coqdocemptyline
}\end{coqdoccode}
Is is trivial to add notation for easier use of the list constructor \begin{coqdoccode}
\scriptsize {
\coqdocnoindent
\coqdockw{Notation} "x :: l" := (\coqconstructorref{simplelists.cons}{cons} \coqdocvar{x} \coqdocvar{l}) (\coqdoctac{at} \coqdocvar{level} 60, \coqdocvar{right} \coqdocvar{associativity}).\coqdoceol
\coqdocnoindent
\coqdockw{Notation} "[ ]" := \coqconstructorref{simplelists.nil}{nil}.\coqdoceol
\coqdocnoindent
\coqdockw{Notation} "[ x , .. , y ]" := (\coqconstructorref{simplelists.cons}{cons} \coqdocvar{x} .. (\coqconstructorref{simplelists.cons}{cons} \coqdocvar{y} \coqconstructorref{simplelists.nil}{nil}) ..).\coqdoceol
\coqdocemptyline
}\end{coqdoccode}
}

\frame {
  \frametitle{More Lists}

  Now we can define a few functions over lists \begin{coqdoccode}
\scriptsize{
    \coqdocemptyline
    \coqdocnoindent
    \coqdockw{Fixpoint} \coqdefinition{simplelists.repeat}{repeat} (\coqdocvar{n} \coqdocvar{count} : \coqinductiveref{Coq.Init.Datatypes.nat}{nat}) \{\coqdockw{struct} \coqdocvar{count}\} : \coqinductiveref{simplelists.natlist}{natlist} := \coqdoceol
    \coqdocindent{1.00em}
    \coqdockw{match} \coqdocvar{count} \coqdockw{with}\coqdoceol
    \coqdocindent{1.00em}
    \ensuremath{|} \coqconstructorref{Coq.Init.Datatypes.O}{O} \ensuremath{\Rightarrow} \coqconstructorref{simplelists.nil}{nil}\coqdoceol
    \coqdocindent{1.00em}
    \ensuremath{|} \coqconstructorref{Coq.Init.Datatypes.S}{S} \coqdocvar{count'} \ensuremath{\Rightarrow} \coqdocvar{n} :: (\coqdefinitionref{simplelists.repeat}{repeat} \coqdocvar{n} \coqdocvar{count'})\coqdoceol
    \coqdocindent{1.00em}
    \coqdockw{end}.\coqdoceol
    \coqdocemptyline}
  \end{coqdoccode}
  The \coqdocvar{length} function calculates the length of a
  list. \begin{coqdoccode}
\scriptsize {
    \coqdocemptyline
    \coqdocnoindent
    \coqdockw{Fixpoint} \coqdefinition{simplelists.length}{length} (\coqdocvar{l}:natlist) \{\coqdockw{struct} \coqdocvar{l}\} : \coqinductiveref{Coq.Init.Datatypes.nat}{nat} := \coqdoceol
    \coqdocindent{1.00em}
    \coqdockw{match} \coqdocvar{l} \coqdockw{with}\coqdoceol
    \coqdocindent{1.00em}
    \ensuremath{|} \coqconstructorref{simplelists.nil}{nil} \ensuremath{\Rightarrow} \coqconstructorref{Coq.Init.Datatypes.O}{O}\coqdoceol
    \coqdocindent{1.00em}
    \ensuremath{|} \coqdocvar{h} :: \coqdocvar{t} \ensuremath{\Rightarrow} \coqconstructorref{Coq.Init.Datatypes.S}{S} (\coqdefinitionref{simplelists.length}{length} \coqdocvar{t})\coqdoceol
    \coqdocindent{1.00em}
    \coqdockw{end}.\coqdoceol
    \coqdocemptyline}
  \end{coqdoccode}
  The \coqdocvar{app} function concatenates two
  lists. \begin{coqdoccode}
\scriptsize{
    \coqdocemptyline
    \coqdocnoindent
    \coqdockw{Fixpoint} \coqdefinition{simplelists.app}{app} (\coqdocvar{l1} \coqdocvar{l2} : \coqinductiveref{simplelists.natlist}{natlist}) \{\coqdockw{struct} \coqdocvar{l1}\} : \coqinductiveref{simplelists.natlist}{natlist} := \coqdoceol
    \coqdocindent{1.00em}
    \coqdockw{match} \coqdocvar{l1} \coqdockw{with}\coqdoceol
    \coqdocindent{1.00em}
    \ensuremath{|} \coqconstructorref{simplelists.nil}{nil}    \ensuremath{\Rightarrow} \coqdocvar{l2}\coqdoceol
    \coqdocindent{1.00em}
    \ensuremath{|} \coqdocvar{h} :: \coqdocvar{t} \ensuremath{\Rightarrow} \coqdocvar{h} :: (\coqdefinitionref{simplelists.app}{app} \coqdocvar{t} \coqdocvar{l2})\coqdoceol
    \coqdocindent{1.00em}
    \coqdockw{end}.\coqdoceol
    \coqdocemptyline
    \coqdocemptyline
    \coqdocnoindent
    \coqdockw{Notation} "x ++ y" := (\coqdefinitionref{simplelists.app}{app} \coqdocvar{x} \coqdocvar{y}) \coqdoceol
    \coqdocindent{10.50em}
    (\coqdocvar{right} \coqdocvar{associativity}, \coqdoctac{at} \coqdocvar{level} 60).\coqdoceol
    \coqdocemptyline}
  \end{coqdoccode}
}
\frame{
  \frametitle{List Theorems}
  One trivial theorem we can show is the indentity of a list
  using

\begin{coqdoccode}
\scriptsize{
    \coqdocnoindent
    \coqdockw{Theorem} \coqlemma{simplelists.nilapp}{nil\_app} : \ensuremath{\forall} \coqdocvar{l}:natlist,\coqdoceol
    \coqdocindent{1.00em}
    [] ++ \coqdocvar{l} = \coqdocvar{l}.\coqdoceol
    \coqdocnoindent
    \coqdockw{Proof}.\coqdoceol
    \coqdocindent{1.50em}
    \coqdoctac{reflexivity}. \coqdockw{Qed}.\coqdoceol}
\end{coqdoccode}
A more complex theorem involving induction

 \begin{coqdoccode}
\scriptsize{
\coqdocnoindent
\coqdockw{Theorem} \coqdocvar{assoc\_app} : \ensuremath{\forall} \coqdocvar{l1} \coqdocvar{l2} \coqdocvar{l3} : \coqdocvar{natlist}, \coqdoceol
\coqdocindent{1.00em}
\coqdocvar{l1} ++ (\coqdocvar{l2} ++ \coqdocvar{l3}) = (\coqdocvar{l1} ++ \coqdocvar{l2}) ++ \coqdocvar{l3}.\coqdoceol
\coqdocnoindent
\coqdockw{Proof}.\coqdoceol
\coqdocindent{1.00em}
\coqdoctac{intros} \coqdocvar{l1} \coqdocvar{l2} \coqdocvar{l3}. \coqdoctac{induction} \coqdocvar{l1} \coqdockw{as} [\ensuremath{|} \coqdocvar{n} \coqdocvar{l1'}].\coqdoceol
\coqdocindent{1.00em}
\coqdocvar{Case} "l1 = nil".\coqdoceol
\coqdocindent{2.00em}
\coqdoctac{reflexivity}.\coqdoceol
\coqdocindent{1.00em}
\coqdocvar{Case} "l1 = cons n l1'".\coqdoceol
\coqdocindent{2.00em}
\coqdoctac{simpl}. \coqdoctac{rewrite} \ensuremath{\rightarrow} \coqdocvar{IHl1'}. \coqdoctac{reflexivity}. \coqdockw{Qed}.\coqdoceol
}
\end{coqdoccode}
}

\frame {
  \frametitle{Resources}
  \begin{itemize}
    \item Coq: http://coq.inria.fr/
    \item Software Foundations http://www.cis.upenn.edu/~bcpierce/sf/
      (from which i heavily borrowed for these slides)
\end{itemize}
}

\end{document}


\chapter{}

\begin{ex}
    Let \(\left\{\mathbb{P}_{\theta}\right\}_{\theta \in \Theta}, \Theta \subseteq \mathbb{R}\), be an identifiable parametric family of distributions with common support, where card \((\Theta) \geq 2\). Consider the family of estimators \(\Delta=\left\{\delta(\mathbf{X}): \mathbb{E}_{\theta} \delta^{2}<\infty, \theta \in\right.\) \(\Theta\}\) and the loss function \(L(\theta, a)=(\theta-a)^{2}\). Prove that there does not exist an estimator \(\delta(\mathbf{X})\) for which \(R(\theta, \delta)=0, \theta \in \Theta\).
\end{ex}

\begin{proof}
    \[\begin{aligned}
            R(\theta, \delta)&=EL(\theta, \delta)\\
            &=E(\theta^2-2\theta\delta(X)+\delta^2(X))\\
            &=\theta^2-2\theta E(\delta(X))+E(\delta(X))^2\\
            &=\theta^2-2\theta E(\delta(X))+Var(\delta(X))+(E(\delta(X)))^2\\
            &=(\theta-E(\delta(X)))^2+Var(\delta(X))\\
            &\geqslant Var(\delta(X))>0.
        \end{aligned}
    \]
    So, $\forall \theta \in \Theta$, $R(\theta, \delta)>0$.
\end{proof}

\begin{ex}
    If \(X_{1}, \ldots, X_{n} \stackrel{\text { i.i.d. }}{\sim} \mathcal{N}\left(\mu, \sigma_{0}^{2}\right), \mu \in \mathbb{R}\), where \(\sigma_{0}^{2}\) is known, let \(\delta(\mathbf{X})=\sum_{i=1}^{n} c_{i} X_{i}\) be any linear estimator of \(\mu\). Show that, if \(\delta\) is biased, then the quadratic risk \(\mathbb{E}_{\mu}(\delta-\mu)^{2}\) is unbounded.
\end{ex}

\begin{proof}
    For $\delta(X)$,
    \[
        E(\delta(X))=\sum_{i=1}^nc_i E(X_i)=\mu\sum_{i=1}^nc_i.
    \]
    Hence, if $\sum c_i\neq 1$, $\delta(X)$ is biased. Then,
    \[
        \begin{aligned}
            E(\delta-\mu)^2&=E\left(\sum c_iX_i\right)^2-2\mu^2\sum c_i+\mu^2\\
            &=Var\left(\sum c_iX_i\right)+\left(\mu\sum c_i\right)^2-2\mu^2\sum c_i+\mu^2\\
            &=\sigma_0^2\sum c_i+\mu^2\left(\sum c_i-1\right)^2.
        \end{aligned}
    \]
    If $\sum c_i\neq 1$, then $\left(\sum c_i-1\right)^2>0$. And $E(\delta-\mu)^2\to\infty$, when $\left(\sum c_i-1\right)^2\to \infty$.
\end{proof}

\begin{ex}
    Let \(\mathcal{P}=\left\{\mathbb{P}_{\theta}\right\}_{\theta \in \Theta}\) be a parametric family and let \(g\) be a measurable function. Suppose \(g(\theta)\) is \(U\)-estimable, i.e., there exists an estimator \(\delta_{0}(\mathbf{X})\) such that \(\mathbb{E}_{\theta} \delta_{0}(\mathbf{X})=g(\theta), \theta \in \Theta\). Show that the class of unbiased estimators of \(g(\theta)\) is given by
    \(\left\{\delta(\mathbf{X}): \delta(\mathbf{X})=\delta_{0}(\mathbf{X})-U_{\delta}(\mathbf{X})\right.\) for some unbiased estimator \(U_{\delta}(\mathbf{X})\) of zero \(\}\).
\end{ex}

\begin{proof}
    What we need to prove is $E(\delta(X))=g(\theta)$. Then,
    \[
        \begin{aligned}
            E(\delta(X))&=E(\delta_0(X))-E(U_\delta(X))\\
            &=g(\theta).
        \end{aligned}
    \]
    ???
\end{proof}

\begin{ex}
    Let \(G(d \mathbf{x})\) be a \(\sigma\)-finite measure, and let \(\mathcal{P}=\{f(\mathbf{x} \mid \theta) G(d \mathbf{x})\}_{\theta \in \Theta}\) be a family of distributions with associated measurements \(\mathbf{X}\). Recall that the likelihood function is given by
    \[
    \mathcal{L}(\theta \mid \mathbf{X}):=f(\mathbf{X} \mid \theta)
    \]
    In particular, for any given \(\theta, \mathcal{L}(\theta \mid \mathbf{X})\) is a random variable. Note that the definition of likelihood function simply entails a change of perspective with respect to the generalized joint density: instead of fixing \(\theta\), we now fix the sample \(\mathbf{X}\). So, starting from a sample \(\mathbf{X}\), further recall that the maximum likelihood estimator is defined by
    \[
        \widehat{\theta}_{\mathrm{ML}}=\operatorname{argmax}_{\theta \in \Theta} \mathcal{L}(\theta \mid \mathbf{X}) .
    \]
    Find the maximum likelihood estimator of the following parameters under each parametric family.
    \begin{enumerate}[(a)]
        \item \(\widehat{\lambda}_{\mathrm{ML}}\) when \(X_{1}, \ldots, X_{n} \stackrel{\text { i.i.d. }}{\sim} \operatorname{Poi}(\lambda), \lambda>0\).
        \item \(\widehat{\lambda}_{\mathrm{ML}}\) when \(X_{1}, \ldots, X_{n} \stackrel{\text { i.i.d. }}{\sim} \exp (\lambda), \lambda>0\).
        \item \(\left(\widehat{\mu}_{\mathrm{ML}}, \widehat{\sigma^{2}}_\mathrm{ML}\right)\) when \(X_{1}, \ldots, X_{n} \stackrel{\text { i.i.d. }}{\sim} \mathcal{N}\left(\mu, \sigma^{2}\right), \mu, \sigma^{2} \in \mathbb{R} \times(0, \infty)\) (hint: first solve for \(\widehat{\mu}_{\mathrm{ML}}\).
        \item \(\widehat{\theta}_{\mathrm{ML}}\) when \(X_{1}, \ldots, X_{n} \stackrel{\text { i.i.d. }}{\sim} U[0, \theta], \theta>0\).
        \item \(\widehat{\mu^{2}}{ }_{\mathrm{ML}}\) when \(X_{1}, \ldots, X_{n} \stackrel{\text { i.i.d. }}{\sim} \mathcal{N}(\mu, 1), \mu \in \mathbb{R}\).
    \end{enumerate}
\end{ex}

\begin{solution}
    \begin{enumerate}[(a)]
        \item The likelihood function is
        \[
            f(\lambda|X)=\frac{\exp(-n\lambda)\lambda^{\sum x_i}}{\prod x_i!},
        \]
        Then,
        \[
            \mathcal{L}(\lambda)=\log(\lambda)\sum x_i-n\lambda-\sum \log(x_i!).
        \]
        \[
            \mathcal{L}'(\lambda)=\frac{\sum x_i}{\lambda}-n=0,
        \]
        Therefore,
        \[
            \hat{\lambda}_{ML}=\bar{X}.
        \]
        \item The likelihood function is
        \[
            f(\lambda|X)=\lambda^n\exp\left(-\lambda\sum x_i\right),
        \]
        Then,
        \[
            \mathcal{L}(\lambda)=n\log(\lambda)-\lambda\sum x_i,
        \]
        \[
            \mathcal{L}'(\lambda)=\frac{n}{\lambda}-\sum x_i=0,
        \]
        Therefore,
        \[
            \hat{\lambda}_{ML}=\frac{1}{\bar{X}}.
        \]
        \item The likelihood function is
        \[
            f(\mu, \sigma^2|X)=(2\pi\sigma^2)^{-n/2}\exp\left(-\frac{1}{2\sigma^2}\sum(x_i-\mu)^2\right)
        \]
        Then,
        \[
            \mathcal{L}(\mu, \sigma^2) = -\frac{n\log(2\pi)}{2}-\frac{n}{2}\log(\sigma^2)-\frac{\sum x_i^2}{2\sigma^2}+\frac{\mu\sum x_i}{\sigma^2}-\frac{n\mu^2}{2\sigma^2}.
        \]
        \[
            \mathcal{L}'(\mu)=\frac{\sum x_i}{\sigma^2}-\frac{n\mu}{\sigma^2}=0, \qquad \hat{\mu}_{ML}=\bar{X}.
        \]
        \[
            \mathcal{L}'(\sigma^2)=-\frac{n}{2}\frac{1}{\sigma^2}+\frac{1}{(\sigma^2)^2}\left(\frac{\sum x_i^2}{2}-\mu\sum x_i+\frac{n\mu^2}{2}\right)=0,
        \]
        \[
            \widehat{\sigma^2}_{ML}=\frac{\sum x_i^2-2\mu\sum x_i+n\mu^2}{n}=\frac{\sum x_i^2}{n}-\bar{X}^2.
        \]
        \item The likelihood function is
        \[
            f(\theta|X)=\frac{1}{\theta^n}\mathbf{1}_{0\leqslant x_{(1)}, x_{(n)}\leqslant\theta},
        \]
        Then,
        \[
            \mathcal{L}(\theta)=-n\log(\theta),
        \]
        \[
            \mathcal{L}'(\lambda)=\frac{-n}{\theta}<0,
        \]
        Therefore,
        \[
            \hat{\theta}_{ML}=X_{(n)}.
        \]
        \item Maximum likelihood estimator has unvari
        \[
            \mathcal{L}(\mu) = -\frac{n\log(2\pi)}{2}-\frac{\sum x_i^2}{2}+\mu\sum x_i-\frac{n\mu^2}{2}.
        \]
        Let $t=\mu^2$, $\mu=\pm\sqrt{t}$. When $\mu>0$,
        \[
            \mathcal{L}'(t)=\frac{1}{2}t^{-1/2}\sum x_i-\frac{n}{2}=0, \qquad \widehat{\mu^2}_{ML}=\bar{X}^2.
        \]
        When $\mu<0$,
        \[
            \mathcal{L}'(t)=-\frac{1}{2}t^{-1/2}\sum x_i-\frac{n}{2}=0, \qquad \widehat{\mu^2}_{ML}=\bar{X}^2.
        \]
        Always positive?
        \item Because the invariability of ML, \[
            \widehat{\sigma}_{ML}=\sqrt{\widehat{\sigma^2}_{ML}}=\sqrt{Var(X)}.
        \]
    \end{enumerate}
\end{solution}

\begin{ex}
    Let \(X_{1}, \ldots, X_{n} \stackrel{\text { i.i.d. }}{\sim} \mathcal{N}\left(\mu, \sigma_{0}^{2}\right), \mu \in \mathbb{R}\).
    \begin{enumerate}[(a)]
        \item If \(\sigma_{0}^{2}\) is known, find the UMVU estimators of \(\mu^{2}, \mu^{3}\) and \(\mu^{4}\) (suggestion: consider \(\bar{X}=Y+\mu, Y \sim \mathcal{N}\left(0, \sigma^{2} / n\right)\), and expand \(\left.\mathbb{E}(Y+\mu)^{k}, k \in \mathbb{N}\right)\).
        \item Solve the preceding problem when \(\sigma\) is unknown.
    \end{enumerate}
\end{ex}

\begin{solution}
    \begin{enumerate}[(a)]
        \item \begin{itemize}
            \item $\mu^2$:
            \[
                \begin{aligned}
                    E(\bar{X})^2&=E(Y)^2+2\mu E(Y)+\mu^2\\
                    &=Var(Y)+(E(Y))^2+\mu^2\\
                    &=\frac{\sigma_0^2}{n}+\mu^2,
                \end{aligned}
            \]
            So, $E(\bar{X}^2-\sigma_0^2/n)=\mu^2$. And $T(X)=\bar{X}^2-\sigma_0^2/n$ is UMVU for $\mu^2$.
            \item $\mu^3$:
            \[
                \begin{aligned}
                    E(\bar{X}^3)&=E(Y^3)+3\mu E(Y^2)+3\mu^2E(Y)+\mu^3\\
                    &=3\mu\frac{\sigma^2_0}{n}+\mu^3,
                \end{aligned}
            \]
            So, $E(\bar{X}^3-3\bar{X}\sigma_0^2/n)=\mu^3$. $T(X)=\bar{X}^3-3\bar{X}\sigma_0^2/n$ is UMVU for $\mu^3$.
            \item $\mu^4$:
            \[
                \begin{aligned}
                    E(\bar{X}^4)&=E(Y^4)+4\mu E(Y^3)+6\mu^2E(Y^2)+4\mu^3E(Y)+\mu^4\\
                    &=3\left(\frac{\sigma_0^2}{n}\right)^4+6\mu^2\left(\frac{\sigma_0^2}{n}\right)+\mu^4,
                \end{aligned}
            \]
            So, $E(\bar{X}^4-6(\bar{X}^2-\sigma_0^2/n)\sigma_0^2/n-3(\sigma_0^2/n)^4)=\mu^4$. $T(X)=\bar{X}^4-6(\bar{X}^2-\sigma_0^2/n)\sigma_0^2/n-3(\sigma_0^2/n)^4$ is UMVU for $\mu^4$.
        \end{itemize}
        \item Because $S^2$ is UMVU for $\sigma_0^2$, replacing $\sigma_0^2$ with $S^2$ can get the results.
    \end{enumerate}
\end{solution}

\begin{ex}
    If \(X_{1}, \ldots, X_{n} \stackrel{\text { i.i.d. }}{\sim} \mathcal{N}\left(\mu, \sigma^{2}\right)\), where one of the parameters is known, and the estimator is a polynomial in \(\mu\) or \(\sigma\). Then, clearly, the UMVU estimator is a polynomial in \(\bar{X}\) or \(S_{*}^{2}:=\sum_{i=1}^{n}\left(X_{i}-\mu\right)^{2}\). The variance of any such polynomial can be estimated if one knows the moments \(\mathbb{E}\left(\bar{X}^{k}\right)\) and \(\mathbb{E}\left(S_{*}^{k}\right), k \in \mathbb{N}\). To determine \(\mathbb{E}\left(\bar{X}^{k}\right)\), write \(\bar{X}=Y+\mu\), where \(Y \sim \mathcal{N}\left(0, \sigma^{2} / n\right) .\)
    \begin{enumerate}[(a)]
        \item Show that
        \[
        \mathbb{E}\left(\bar{X}^{k}\right)=\sum_{r=0}^{k}\left(\begin{array}{l}
        k \\
        r
        \end{array}\right) \mu^{k-r} E\left(Y^{r}\right)
        \]
        with
        \[
        \mathbb{E}\left(Y^{r}\right)=\left\{\begin{array}{cc}
        (r-1)(r-3) \ldots(3)(1)\left(\sigma^{2} / n\right)^{r / 2}, & r \geq 2 \text { is even } \\
        0, & r \text { is odd. }
        \end{array}\right.
        \]
        \item As an example, consider the UMVU estimator \(S_{*}^{2} / n\) of \(\sigma^{2}\). Show that \(\mathbb{E}\left(S_{*}^{4}\right)=\) \(n(n+2) \sigma^{4}\) and \(\operatorname{Var}\left(S_{*}^{2} / n\right)=2 \sigma^{4} / n\), and that the UMVU estimator of this variance is \(2 S_{*}^{4} / n^{2}(n+2)\).
    \end{enumerate}
\end{ex}

\begin{solution}

\end{solution}

\begin{ex}
    Let \(X_{1}, \ldots, X_{n} \stackrel{\text { i.i.d. }}{\sim} \mathcal{N}\left(\mu, \sigma^{2}\right)\), where both parameters are unknown.
    \begin{enumerate}[(a)]
        \item Show that the UMVU estimator of \(\mu^{2}\) is given by
        \[
        \delta(\mathbf{X})=\bar{X}^{2}-\frac{S^{2}}{n};
        \]
        \item Determine \(Var\delta(\mathbf{X})\);
        \item Find the UMVU estimator of \(Var \delta(\mathbf{X})\).
    \end{enumerate}
\end{ex}

\begin{ex}
    If \(X \sim \mathcal{N}\left(\mu, \sigma^{2}\right.\)) (i.e., \(n=1\)), show that no unbiased estimator \(\delta\) of \(\sigma^{2}\) exists when \(\mu\) in unknown (suggestion: for fixed \(\sigma=\sigma_{0}, X\) is a complete and sufficient statistic for \(\mu\). Now consider \(\mathbb{E} \delta(X)=\sigma_{0}^{2}\), and conclude that \(\delta(X)\) is a constant a.s.)
\end{ex}

\begin{ex}
    Let \(X \sim \mathcal{N}\left(\mu, \sigma^{2}\right)\) (i.e., \(n=1\)), where both parameters are unknown. Fix \(u \in \mathbb{R}\). Show that the UMVU estimator of
    \[
    p:=\mathbb{P}(X \leq u)
    \]
    is \(1_{\{X \leq u\}}\).
\end{ex}

\begin{ex}
    Consider two independent samples \(X_{1}, \ldots, X_{m} \stackrel{\text { i.i.d. }}{\sim} \mathcal{N}\left(\mu_{1}, \sigma_{1}^{2}\right), Y_{1}, \ldots, Y_{n} \stackrel{\text { i.i.d. }}{\sim} \mathcal{N}\left(\mu_{2}, \sigma_{2}^{2}\right)\).
    \begin{enumerate}[(a)]
        \item Assume \(\sigma_{1}^{2}=\sigma_{2}^{2}=: \sigma^{2}\) (unknown). Determine the UMVU estimators of \(\sigma^{2}\) and \(\left(\mu_{2}-\mu_{1}\right) / \sigma\).
        \item Now assume \(\mu_{1}=\mu_{2}\) (unknown), and \(\sigma_{1}^{2} / \sigma_{2}^{2}=\gamma\), where \(\gamma\) is known. Prove that:
        \begin{enumerate}[(i)]
            \item \(T^{\prime}(\mathbf{X}, \mathbf{Y})=\left(\sum X_{i}^{2}+\gamma \sum Y_{j}^{2}, \sum X_{i}+\gamma \sum Y_{j}\right)\) is a complete and sufficient statistic.
            \item The estimator
            \[
                \delta_{\gamma}(\mathbf{X}, \mathbf{Y})=\alpha \bar{X}+(1-\alpha) \bar{Y}, \quad \alpha=\frac{\sigma_{2}^{2} / n}{\sigma_{1}^{2} / m+\sigma_{2}^{2} / n}
            \]
            is UMVU for \(\mu\).
        \end{enumerate}
        \item Now assume \(\mu_{1}=\mu_{2}\) (unknown), and \(\sigma_{1}^{2} / \sigma_{2}^{2}=\gamma\) is also unknown. We want to prove that the UMVU estimator of \(\mu\) does not exist.
        \begin{enumerate}[(i)]
            \item Fix two parameter values \(\gamma_{1}\) and \(\gamma_{2}\). Define the estimators
            \[
            \delta_{\gamma_{i}}(\mathbf{X}, \mathbf{Y})=\alpha_{i} \bar{X}+\left(1-\alpha_{i}\right) \bar{Y}, \quad \alpha_{i}=\frac{1 / n}{\gamma_{i} / m+1 / n}, \quad i=1,2,
            \]
            where \(\alpha_{1}\) and \(\alpha_{2}\) are regarded as fixed constants. Let \(\xi_{1}(\mathbf{X}, \mathbf{Y}), \xi_{2}(\mathbf{X}, \mathbf{Y})\) be unbiased estimators of \(\mu\) which are not a.s.-equal to \(\delta_{\gamma_{1}}\) and \(\delta_{\gamma_{2}}\), respectively. Conclude that
            \[
            \begin{array}{ll}
            \operatorname{Var}_{\mu_{1}, \gamma_{1}}\left(\delta_{\gamma_{1}}\right)<\operatorname{Var}_{\mu_{1}, \gamma_{1}}\left(\xi_{1}\right), & \exists \mu_{1} \in \mathbb{R} \\
            \operatorname{Var}_{\mu_{2}, \gamma_{2}}\left(\delta_{\gamma_{2}}\right)<\operatorname{Var}_{\mu_{2}, \gamma_{2}}\left(\xi_{2}\right), & \exists \mu_{2} \in \mathbb{R}.
            \end{array}
            \]
            \item Prove that \(\delta_{\gamma_{1}}\) and \(\delta_{\gamma_{2}}\) are not a.s.-equal estimators for every pair \(\mu, \gamma\). Conclude that the UMVU estimator of \(\mu\) does not exist.
        \end{enumerate}
        \item Still assuming that \(\mu_{1}=\mu_{2}\) (unknown), and that \(\sigma_{1}^{2} / \sigma_{2}^{2}=\gamma\) is also unknown, prove that the estimator \(\widehat{\mu}:=\widehat{\alpha} \bar{X}+(1-\widehat{\alpha}) \bar{Y}\), where \(\widehat{\alpha}\) is a function only of \(S_{X}^{2}\) and \(S_{Y}^{2}\), is unbiased for \(\mu\).
    \end{enumerate}
\end{ex}

\begin{ex}
    Let \(\left(X_{1}, Y_{1}\right), \ldots,\left(X_{n}, Y_{n}\right) \stackrel{\text { i.i.d. }}{\sim} N_{2}(\mu, \Sigma)\), i.e.,
    \[
    \begin{gathered}
    f(\mathbf{x}, \mathbf{y} \mid \theta)=f(\mathbf{x}, \mathbf{y} \mid \mu, \Sigma)=\left(\frac{1}{2 \pi \sigma_{1} \sigma_{2} \sqrt{1-\rho^{2}}}\right)^{n} \\
    \exp \left(-\frac{1}{2\left(1-\rho^{2}\right)}\left(\frac{1}{\sigma_{1}^{2}} \sum_{i=1}^{n}\left(X_{i}-\mu_{1}\right)^{2}+\frac{1}{\sigma_{2}^{2}} \sum_{i=1}^{n}\left(Y_{i}-\mu_{2}\right)^{2}-\frac{2 \rho}{\sigma_{1} \sigma_{2}} \sum_{i=1}^{n}\left(X_{i}-\mu_{1}\right)\left(Y_{i}-\mu_{2}\right)\right)\right), \\
    \left(\mu, \sigma^{2}, \rho\right) \in \mathbb{R} \times(0, \infty) \times(-1,1) .
    \end{gathered}
    \]
    \begin{enumerate}
        \item Conclude that \(T(\mathbf{X}, \mathbf{Y})=\left(\bar{X}, \bar{Y}, S_{X}^{2}, S_{Y}^{2}, S_{X Y}\right)\) is a complete and sufficient statistic.
        \item Conclude that \(S_{X Y}\) is UMVU for \(\operatorname{Cov}\left(X_{1}, Y_{1}\right)\).
    \end{enumerate}
\end{ex}

\begin{ex}
    Let \(X_{1}, \ldots, X_{n}\) be an i.i.d. sample from the (nonparametric) family \(\mathcal{P}\) which encompasses all a.c. distributions in \(\mathbb{R}\). Show that the order statistics are complete. (hint: establish the following facts.
    \begin{enumerate}[(i)]
        \item Let \(\mathcal{P}_{0} \subseteq \mathcal{P}_{1}\) be two families of distributions such that, for a Borel set \(B\),
        \[
        \mathbb{P}_{\theta}(B)=0, \quad \theta \in \mathcal{P}_{0} \Rightarrow \mathbb{P}_{\theta}(B)=0, \quad \theta \in \mathcal{P}_{1} .
        \]
        If a statistic \(T\) is complete for \(\mathcal{P}_{0}\), then it is also complete for \(\mathcal{P}_{1}\);
        \item note that \(\mathcal{P}\) in the statement of the problem includes the a.c. canonical exponential family
        \[
        q(\mathbf{x} \mid \eta)=h(\mathbf{x}) \exp \left\{\eta_{1} \sum_{i=1}^{n} x_{i}+\eta_{2} \sum_{i=1}^{n} x_{i}^{2}+\ldots+\eta_{n} \sum_{i=1}^{n} x_{i}^{n}-A(\boldsymbol{\eta})\right\}
        \]
        where \(h(\mathbf{x})>0\) has fast enough decay at \(\|\mathbf{x}\| \rightarrow \infty\). Now show that
        \[
        \left(\sum_{i=1}^{n} x_{i}, \sum_{i=1}^{n} x_{i}^{2}, \ldots, \sum_{i=1}^{n} x_{i}^{n}\right)
        \]
        is equivalent to the vector of order statistics).
    \end{enumerate}
\end{ex}

\begin{ex}
    We say that an estimator \(\delta(\mathbf{X})=\delta\left(X_{1}, \ldots, X_{n}\right)\) is symmetric in its \(n\) arguments if
    \[
    \delta\left(X_{1}, \ldots, X_{n}\right)=\delta\left(X_{\pi_{1}}, \ldots, X_{\pi_{n}}\right)
    \]
    for any permutation \(\pi=\left(\pi_{1}, \ldots, \pi_{n}\right)\) of its indices \(1, \ldots, n(\) e.g., \(\delta(\mathbf{X})=\bar{X})\).

    Let \(\mathcal{P}=\{F\}\) be a (nonparametric) family of distributions for which the order statistics \(X_{(1)} \leq \ldots \leq X_{(n)}\) are complete and sufficient. Let \(g(F)\) be a \(U\)-estimable quantity and assume that the symmetric estimator \(\delta(\mathbf{X})\) is such that \(\mathbb{E}_{F} \delta(\mathbf{X})=g(F)\). Prove that \(\delta(\mathbf{X})\) is UMVU for \(g(F)\).
\end{ex}

\begin{ex}
    Let \(\mathcal{P}=\{F\}\) be a (nonparametric) family of distributions in \(\mathbb{R}\) which includes all a.c. distributions, and consider \(g(F)=\mathbb{P}(X \leq a)=F(a)\), \(a\) being a known constant. Let \(X_{1}, \ldots, X_{n}\) be an i.i.d. sample, where \(X_{1} \sim F\), and define the estimator
    \[
    \widehat{F}_{n}(a):=\frac{\#\left\{i: X_{i} \leq a\right\}}{n}
    \]
    (as a function of \(a \in \mathbb{R}, \widehat{F}_{n}(a)\) is called the empirical distribution function).
    \begin{enumerate}[(a)]
        \item Show that \(\widehat{F}_{n}(a)\) is unbiased for \(\mathbb{P}(X \leq a)\).
        \item Show that \(\widehat{F}_{n}(a)\) is a symmetric estimator.
        \item Conclude that \(\widehat{F}_{n}(a)\) is UMVU for \(\mathbb{P}(X \leq a)\).
    \end{enumerate}
\end{ex}

\begin{ex}
    Consider the (nonparametric) family
    \(\mathcal{P}=\left\{\right.\) i.i.d. sample \(X_{1}, \ldots, X_{n}\) from a d.f. \(F\) with a.c. density \(f\) and finite fourth moment \(\} .\)
    Let
    \[
    g(F)=\mathbb{E} X_{1}, \quad h(F)=\operatorname{Var} X_{1}
    \]
    be two estimators.
    \begin{enumerate}
        \item Prove that \(\mathbb{E}\left(X_{1} \mid X_{(1)}, \ldots, X_{(n)}\right)\) is UMVU for \(g(F)\).
        \item Prove that \(\bar{X}\) is UMVU for \(g(F)\).
        \item Conclude that \(\mathbb{E}\left(X_{1} \mid X_{(1)}, \ldots, X_{(n)}\right)=\bar{X} \mathcal{P}\)-a.s.
        \item Prove that \(S^{2}\) is UMVU for \(h(F)\).
    \end{enumerate}
\end{ex}

\begin{ex}
    Let \(\mathcal{P}\) be the nonparametric family \((0.1)\), and let \(g(F)\) be an estimator.
    \begin{enumerate}
        \item For \(r \leq n\), let \(\delta\left(X_{i_{1}}, \ldots, X_{i_{r}}\right) \in \Delta\) be an unbiased estimator for \(g(F)\). Show that there exists a symmetric, unbiased estimator for \(g(F)\). Conclude that we can assume without loss of generality that \(\delta\left(X_{i_{1}}, \ldots, X_{i_{r}}\right)\) is symmetric.
        \item The random variable
        \[
        U(\mathbf{X})=\left(\begin{array}{l}
        n \\
        r
        \end{array}\right)^{-1} \sum_{1 \leq i_{1}<\ldots<i_{r} \leq n} \delta\left(X_{i_{1}}, \ldots, X_{i_{r}}\right)
        \]
        is called an \(r\)-degree \(U\)-statistic for \(g(F)\). Prove that \(U(\mathbf{X})\) is UMVU for \(g(F)\).
    \end{enumerate}
\end{ex}

\begin{ex}
    Let \(\mathcal{P}\) be the nonparametric family \((0.1)\), and let \(g(F)=\mu^{2}\) be the estimator, where \(\mathbb{E} X_{1}=\mu\).
    \begin{enumerate}
        \item Prove that \(\mathbb{E}\left(X_{1} X_{2} \mid X_{(1)}, \ldots, X_{(n)}\right)\) is UMVU for \(\mu^{2}\).
        \item Compute the conditional probability
        \[
        \mathbb{P}\left(X_{1} X_{2}=x_{(i)} x_{(j)} \mid X_{(1)}=x_{(1)}, \ldots, X_{(n)}=x_{(n)}\right), \quad i \neq j
        \]
        and conclude that
        \[
        \widehat{\mu^{2}} \mathrm{UMVU}=\frac{1}{n(n-1)} \sum_{i \neq j} X_{i} X_{j}.
        \]
        \item Conclude that the \(\widehat{\mu^{2}}\) UMVU is a \(U\)-statistic and find its degree.
    \end{enumerate}
\end{ex}