\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage[pdftex]{graphicx}

\begin{document}

\renewcommand{\thesection}{Problem \arabic{section}:}
\renewcommand{\theenumi}{\alph{enumi}}
\addtolength{\belowcaptionskip}{-5mm}

\title{\underline{Problem Set 2}}
\author{MAS 622J/1.126J: Pattern Recognition and Analysis}
\date{Due: 5:00 p.m. on September 30}
\maketitle

\noindent [Note: All instructions to plot data or write a program
should be carried out using Matlab.  In order to maintain a reasonable level of consistency and simplicity
we ask that you do not use other software tools.]
\newline \newline
%If you discussed the problem set with other members of the class please write their names at the top of your solution.
If you collaborated with other members of the class, please write their names at the end of the assignment. Moreover, you will need to write and sign the following statement: ``In preparing my solutions, I did not look at any old homeworks, copy anybody's answers or let them copy mine."



\section{[10 Points]} 
In many pattern classification problems we have the option to either assign a pattern to one of $\textit{c}$ classes, or reject it as unrecognizable - if the cost to reject is not too high. Let the cost of classification be defined as:
\begin{equation*}
\lambda(\omega_{i} | \omega_{j}) = 
\begin{cases} 
0 & \text{if $\omega$ $_{i}$ = $\omega$ $_{j}$,    (i.e. Correct Classification)} \\
\lambda_{r} & \text{if $\omega$ $_{i}$ = $\omega$ $_{0}$,    (i.e. Rejection)} \\
\lambda_{s} & \text{Otherwise,     (i.e. Substitution Error)}
\end{cases}
\end{equation*}
Show that for the minimum risk classification, the decision rule should associate a test vector $\textbf{\textit{x}}$ with class $\omega$ $_{i}$, if P($\omega$ $_{i} | \textbf{\textit{x}}$) $\geq$ P($\omega$ $_{j} | \textbf{\textit{x}}$) for all $\textit{j}$ and and P($\omega$ $_{i}$ | $\textbf{\textit{x}}$)  $\geq$ 1 - $\frac{\lambda_{r}}{\lambda_{s}}$, and reject otherwise.
%%%%%%%%%% SOLUTION %%%%%%%%%%
\newline \newline
Solution:
\newline \newline
Average risk is choosing class $\omega_{i}$:
\begin{equation*}
R(\omega_{i} | \textbf{x}) = \sum_{j=1}^{c} \lambda(\omega_{i} | \omega_{j})P(\omega_{j} | \textbf{x}) = 0.P(\omega_{i} | \textbf{x}) + \sum_{j=1, j!=i}^{c} \lambda_{s}P(\omega_{j} | \textbf{x})
\end{equation*}
where $\lambda$($\omega_{i} | \omega_{j}$) is used to mean the cost of choosing class $\omega_{i}$ where the true class is $\omega{j}$.
\newline \newline
Hence:
\begin{equation*}
R(\omega_{i} | \textbf{x}) = \lambda_{s}(1 - P(\omega_{i} | \textbf{x}))
\end{equation*}
Associate $\textbf{x}$ with the class $\omega_{i}$ if highest posterior class probability and the average risk is less than the cost of rejection:
\begin{equation*}
\lambda_{s}(1 - P(\omega_{i} | \textbf{x})) \leq \lambda_{r}
\end{equation*}
\begin{equation*}
P(\omega_{i} | \textbf{x}) \geq 1 - \lambda_{r}/\lambda_{s}
\end{equation*}


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

\section{[16 Points]}
Use signal detection theory as well as the notation and basic Gaussian assumptions described in the text to address the following.
\begin{enumerate}
\item Prove that ${\bf P(x>x^* | x } \in w_2)$ and ${\bf P(x>x^* | x } \in w_1)$, taken together, uniquely determine the discriminability $\bf d'$

%%%%%%%%%SOLUTION%%%%%%%%%%%
Let ${\bf x} = x$ and  ${\bf x^*} = x^*$.  Based on the Gaussian assumption, we see that 
$ P(x>x^* | x  \in w_i) = P\left(\frac{x-\mu_i}{\sigma_i}>\frac{x^*-\mu_i}{\sigma_i} | x  \in w_i \right) \sim N(0,1)$ for $i=1,2$
Thus, we know $\left( \frac{x-\mu_2}{\sigma_2} \right)$ from the hit rate $ P(x>x^* | x \in w_2)$ and $\left( \frac{x-\mu_1}{\sigma_1} \right)$ from the false alarm rate $P(x>x^* | x \in w_1)$, and these let us calculate the discriminability.

\begin{align*}
\left|\frac{x^*-\mu_1}{\sigma_1} - \frac{x^*-\mu_2}{\sigma_2}\right| 
									&= \left|\frac{x^*-\mu_1}{\sigma} - \frac{x^*-\mu_2}{\sigma}\right| \\
									&= \left|\frac{\mu_2-\mu_1}{\sigma}\right| \\
									&= \frac{|\mu_2-\mu_1|}{\sigma} \\
									&= d'
\end{align*}

Therefore $d'$ is uniquely determined by the hit and false alarm rates.
%%%%%%%%%%%%%%%%%%%%%%%%%%

\item Use error functions $erf(*)$ to express $\bf d'$ in terms of the hit and false alarm rates.  Estimate $\bf d'$ if ${\bf P(x>x^* | x } \in w_1) = .7$ and ${\bf P(x>x^* | x } \in w_2) = .5$.  Repeat for $\bf d'$ if ${\bf P(x>x^* | x } \in w_1) = .85$ and ${\bf P(x>x^* | x } \in w_2) = .15$.

%%%%%%%%%SOLUTION%%%%%%%%%%%
There are a couple of ways in which you can proceed.  This document will detail one of them.

Let y be a random variable such that $P(y>y^*|y \in w_i) \sim N(0,1)$ for $i = 1,2$.


\begin{align*}
\frac{1}{\sqrt{2\pi}} \int_0^{y^*} e^{\frac{t^2}{2}}dt 
			&= \frac{1}{2}\left(\frac{1}{\sqrt{2\pi}} \int_{-y^*}^{y^*} e^{\frac{t^2}{2}}dt\right) \\
			& \text{let  } u= \frac{t^2}{2};  du = \frac{dt}{\sqrt{2}} \\
			& = \frac{1}{2} \left( \frac{1}{\sqrt{\pi}} \int_{\frac{-y^*}{\sqrt{2}}}^{\frac{y^*}{\sqrt{2}}} e^{-u}du \right) \\
			& = \frac{1}{2} \left( \frac{2}{\sqrt{\pi}} \int_{0}^{\frac{y^*}{\sqrt{2}}} e^{-u}du \right) \\
			& = \frac{1}{2} erf\left( \frac{y^*}{\sqrt{2}} \right) 
\end{align*}

Then, given that $ P(x>x^* | x  \in w_i) = P\left(\frac{x-\mu_i}{\sigma_i}>\frac{x^*-\mu_i}{\sigma_i} | x  \in w_i \right) \sim N(0,1)$ for $i=1,2$. 

\begin{align*}
P\left(\frac{x-\mu_i}{\sigma_i} > \frac{x^*-\mu_i}{\sigma_i} | x  \in w_i \right) 
&= 1 - P\left(\frac{x-\mu_i}{\sigma_i} < \frac{x^*-\mu_i}{\sigma_i} | x  \in w_i \right) \\
&= \begin{cases}
	1 - \left( \frac{1}{2} + \frac{1}{2}erf\left( \frac{x^*-\mu_i}{\sqrt{2}\sigma_i} \right)\right) & \text{if } \frac{x^*-\mu_i}{\sigma_i}  > 0\\
	1 - \left( \frac{1}{2} - \frac{1}{2}erf\left( \frac{\mu_i-x^*}{\sqrt{2}\sigma_i} \right)\right) & \text{if } \frac{x^*-\mu_i}{\sigma_i}  < 0 
	\end{cases}\\
&= \begin{cases}
	\frac{1}{2} - \frac{1}{2}erf\left( \frac{x^*-\mu_i}{\sqrt{2}\sigma_i} \right) & \text{if } \frac{x^*-\mu_i}{\sigma_i}  > 0\\
	\frac{1}{2} + \frac{1}{2}erf\left( \frac{\mu_i-x^*}{\sqrt{2}\sigma_i} \right) & \text{if } \frac{x^*-\mu_i}{\sigma_i}  < 0 
	\end{cases} \\
&= \begin{cases}
	\frac{1}{2} \left(1- erf\left( \frac{x^*-\mu_i}{\sqrt{2}\sigma_i} \right) \right) & \text{if } \frac{x^*-\mu_i}{\sigma_i}  > 0\\
	\frac{1}{2} \left(1 + erf\left( \frac{\mu_i-x^*}{\sqrt{2}\sigma_i} \right) \right) & \text{if } \frac{x^*-\mu_i}{\sigma_i}  < 0 
	\end{cases} \\
\end{align*}

Therefore:
\begin{align*}
\frac{x^*-\mu_i}{\sigma_i}  &= \sqrt{2}\left(erf^{-1}[1-2P(x > x^*|x \in w_i)]\right) \text{if } P(x > x^*|x \in w_i) < .5 \\
\frac{\mu_i-x^*}{\sigma_i}  &= \sqrt{2}\left(erf^{-1}[2P(x > x^*|x \in w_i) - 1]\right) \text{if } P(x > x^*|x \in w_i) > .5
\end{align*}

Then to find the values of $erf^-1()$ use a table of $erf$, a table of the cumulative normal distribution, or use Matlab $erf$ and $erfinv$ functions.

\begin{align*}
d_1^* &= \left| -\frac{\mu_1-x^*}{\sigma} - \frac{x^*-\mu_2}{\sigma} \right| \\
   & = \left| -\sqrt{2}\left(erf^{-1}[2(.7) -1]\right) - \sqrt{2}\left(erf^{-1}[2(.5) -1]\right) \right| \\
   & = \left| -\sqrt{2}\left(erf^{-1}[.4]\right) - \sqrt{2}\left(erf^{-1}[0]\right) \right| \\
   & = .??\\
\end{align*}

\begin{align*}
d_2^* &= \left| -\frac{\mu_1-x^*}{\sigma} - \frac{x^*-\mu_2}{\sigma} \right| \\
   & = \left| -\sqrt{2}\left(erf^{-1}[2(.85) -1]\right) - \sqrt{2}\left(erf^{-1}[1- 2(.15)]\right) \right| \\
   & = \left| -\sqrt{2}\left(erf^{-1}[.7]\right) - \sqrt{2}\left(erf^{-1}[.7]\right) \right| \\
   & = 0
\end{align*}
%%%%%%%%%%%%%%%%%%%%%%%%%%

\item Given that the Gaussian assumption is valid, calculate the Bayes error for both the cases in (b).

%%%%%%%%%SOLUTION%%%%%%%%%%%
According to Problem 3 
$$ \mbox{Pr[error]}= {\bf P}(w_1)\epsilon_1 + {\bf P}(w_2)\epsilon_2 $$
$$ \mbox{ where } \epsilon_1 = \int_{\chi_2} {\bf p}({\bf x}|w_1){\bf dx} 
\mbox{ and } \epsilon_2 = \int_{\chi_1} {\bf p}({\bf x}|w_2){\bf dx} 
$$

Since the regions $\chi_1$ and $\chi_2$ are defined by our decision boundary $x^*$.  We can see that 
\begin{align*}
\epsilon_1 = \int_{\chi_2} {\bf p}({\bf x}|w_1){\bf dx}  &= P(x < x^* | x \in w_1) 
					&= 1 - P(x > x^* | x \in w_1) \\
					& Similarly \\
\epsilon_2 = \int_{\chi_1} {\bf p}({\bf x}|w_2){\bf dx}  &= P(x > x^* | x \in w_2) 
\end{align*}

Therefore,
 
\begin{align*}
\text{Pr[error1]} &= (1-.7)P(w_1) +  (.5)P(w_2)  \\
\text{Pr[error2]} &= (1-.85)P(w_1) +  (.15)P(w_2) 
\end{align*}

And if the priors are equally likely, 

\begin{align*}
\text{Pr[error1]} &= .5 \\
\text{Pr[error2]} &= .15
\end{align*}
%%%%%%%%%%%%%%%%%%%%%%%%%%

\item Using a trivial one-line computation or a graph determine which case has the higher $\bf d'$, and explain your logic:
$$
{\bf \mbox{\bf Case A:  }  P(x>x^* | x } \in w_1) = .75 \mbox{ and }  {\bf P(x>x^* | x } \in w_2)=.45.
$$
$$
{\bf \mbox{\bf Case B:  }  P(x>x^* | x } \in w_1) = .2 \mbox{ and }  {\bf P(x>x^* | x } \in w_2)=.9.
$$
\end{enumerate}

%%%%%%%%%SOLUTION%%%%%%%%%%%
From part (b) we can calculate the discriminability given the hit and false alarm rates, and see that Case B has a higher discriminability.

\begin{align*}
d_1^* &= \left| -\frac{\mu_1 - x^*}{\sigma} - \frac{x^*-\mu_2}{\sigma} \right| \\
	   & = \left| -\sqrt{2}\left(erf^{-1}[2(.7) -1]\right) - \sqrt{2}\left(erf^{-1}[1 - 2(.4)]\right) \right| \\
	   & = \left| -\sqrt{2}\left(erf^{-1}[.4]\right) - \sqrt{2}\left(erf^{-1}[.2]\right) \right| \\
	   & =  .777	\\
\end{align*}

\begin{align*}
d_2^* &= \left| \frac{x^*-\mu_1}{\sigma} + \frac{\mu_2 - x^*}{\sigma} \right| \\
	   & = \left| \sqrt{2}\left(erf^{-1}[1 - 2(.2)]\right) + \sqrt{2}\left(erf^{-1}[2(.8) - 1]\right) \right| \\
	   & = \left| \sqrt{2}\left(erf^{-1}[.4]\right) + \sqrt{2}\left(erf^{-1}[.6]\right) \right| \\
	   & =  1.366	\\
\end{align*}

Key point of this problem:  The error function erf is related to the cumulative normal distribution.  Assuming that the probability of $x$ belonging to one of two classes is Gaussian, then knowing the values of the false alarm and hit rates for an arbitrary $x^*$ is enough to compute the discriminability $d'$.  Moreover, if the Gaussian assumption holds, a determination of the discriminability allows us to calculate the Bayes error rate.
%%%%%%%%%%%%%%%%%%%%%%%%%%


\section{[16 Points]}

\begin{enumerate}
\item Show that the maximum likelihood (ML) estimation of the mean for a Gaussian is unbiased but the ML estimate of variance is biased (i.e., slightly wrong).  Show how to correct this variance estimate so that it is unbiased.   


%Sample mean is unbiased:
%\begin{align*}
%E[\hat{\mu}] &= E[ \frac{1}{n} \sum_{i=1}^n x_i] \\
%			&= \frac{1}{n} \sum_{i=1}^n E[ x_i] \\
%			&= \frac{1}{n} \sum_{i=1}^n \mu \\
%			&= \frac{1}{n} n\mu \\
%			&= \mu
%\end{align*}
%
%This document will show two ways to prove that the sample variance is biased.  Proof 1:
%\begin{align*}
%E[ \sum_{i=1}^n (x_i - \hat{\mu})^2] &= E[ \sum_{i=1}^n (x_i)^2] - nE[\hat{\mu}^2] \\
%				&= nE[(x_i)^2] - \frac{1}{n}E[(\sum_{i=1}^n x_i)^2] \\
%				&= n(var(x_i) + (E[x_i])^2) - \frac{1}{n}E[(\sum_{i=1}^n x_i)^2] \\
%				&= n\sigma^2 + \frac{1}{n}(nE[x_i])^2) - \frac{1}{n}E[(\sum_{i=1}^n x_i)^2] \\
%				&= n\sigma^2 - \frac{1}{n}[ E[(\sum_{i=1}^n x_i)^2] - (E[\sum_{i=1}^n x_i])^2] \\
%				&= n\sigma^2 - \frac{1}{n}var(\sum_{i=1}^n x_i) \\
%				&= n\sigma^2 - \frac{1}{n}n\sigma^2 \\
%				&= (n-1)\sigma^2 \\
%\end{align*}
%
%Proof 2:
%\begin{align*}
%E[ \hat{\sigma_{ML}}^2] &= E[ \frac{1}{n} \sum_{i=1}^n (x_i - \hat{\mu})^2] \\
%				&= \frac{1}{n}  \sum_{i=1}^n  E[x_i^2 - 2x_i\hat{\mu} + \hat{\mu}^2] \\
%				&= \frac{1}{n}  \sum_{i=1}^n  E[x_i^2]  - 2\hat{\mu}E[\sum_{i=1}^n x_i] + \sum_{i=1}^n E[\hat{\mu}^2] \\
%				&= \frac{1}{n} [ n(\sigma^2 + \mu^2)  - 2nE[\hat{\mu}^2] + nE[\hat{\mu}^2] ] \\
%				&= \frac{1}{n} [ n\sigma^2 + n\mu^2  - nE[\hat{\mu}^2] ]\\
%				&= \frac{1}{n} [ n\sigma^2 + n\mu^2  - n (frac{\sigma^2}{n} + \mu^2) ]\\
%				&= \frac{n-1}{n} \sigma^2 \\
%				& Therefore \\
%\text{unbiased: } \hat{\sigma}^2 &= \frac{n}{n-1} \hat{\sigma_{ML}}^2 \\
%				&= \frac{1}{n-1} \sum_{i=1}^n (x_i - \hat{\mu})^2 \\
%\end{align*}

\item For this part you'll write a program with Matlab to explore the biased and unbiased ML estimations of variance for a Gaussian distribution.  Find the data for this problem on the class webpage as ps2.dat.  This file contains n=5000 samples from a 1-dimensional Gaussian distribution.  
\begin{enumerate} 
	\item Write a program to calculate the ML estimate of the mean, and report the output.
	\item Write a program to calculate both the biased and unbiased ML estimate of the variance of this distribution.  For n=1 to 5000, plot the biased and unbiased estimates of the variance of this Gaussian.  This is as if you are being given these samples sequentially, and each time you get a new sample you are asked to re-evaluate your estimate of the variance.  Give some interpretation of your plot.


%The dataset was generated from a Gaussian normal distribution $\sim N(6, 1)$.  The following Matlab code calculates the ML estimate of the mean = 6, and produces Figure \ref{fig:mlbias} indicating that the variance = 1.0.  
%
%\begin{verbatim}
%data = load('ps2.dat');
%n = length(data)
%%ML estimate of mean:
%MLmean = sum(data) / n
%MLvar = [];
%unMLvar = [];
%%ML estimate of variance:
%for(i=2:n)
%    varML = 0;
%    tempMean = sum(data(1:i)) / i;   
%    for (j=1:i)
%        varML = varML + (data(j) - tempMean)^2;
%    end
%    
%    MLvar(i) = varML/i;
%    x = data(1:i);
%    unMLvar(i) = cov(data(1:i));
%end
%figure
%plot(1:1:n, MLvar(1:n), 'g', 1:1:n, unMLvar(1:n), 'r')
%\end{verbatim}

\end{enumerate}

%\begin{figure}
%\center
%\includegraphics[width=4in]{MLBias.jpg}
%\caption{Though the two estimators have quite different estimates in the first few samples, the bias looses significance as $n$ is large, and both estimators give the true variance of the data $\sigma = 1.0$.}
%\label{fig:mlbias}
%\end{figure}

\end{enumerate}


\section{[16 Points]}
Suppose $\textbf{x}$ and $\textbf{y}$ are random variables.  Their joint density, depicted below, is constant in the shaded area and 0 elsewhere,

\begin{figure}[h]
\center
\includegraphics[width=4in]{P7pic.jpg}
\caption{The joint distribution of $\textbf{x}$ and $\textbf{y}$.}
\label{fig:mlbias}
\end{figure}


\begin{enumerate}
\item Let $\omega_{1}$ be the case when $\textbf{x}$ $\leq$ 0, and $\omega_{2}$ be the case when $\textbf{x}$ $>$ 0.  Determine the $\textit{a priori}$ probabilities of the two classes P($\omega_{1}$) and P($\omega_{2}$). $\textbf{y}$ is the observation. from which we infer whether $\omega_{1}$ or $\omega_{2}$ happens. Find the likelihood functions, namely, the two conditional distributions $\textit{p(y$|$$\omega_{1}$)}$ and $\textit{p(y$|$$\omega_{2}$)}$.

\item Find the decision rule that minimizes the probability of error, and calculate what the probability of error is.  Please note that there will be ambiguities at decision boundaries, but how you classify when $\textit{y}$ falls on the decision boundary doesn't affect the probability of error. 

\end{enumerate}

\end{document}
