%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS622: Theory of Formal Languages
% Copyright 2014 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 5}

Prove that the language $\{ a^n b^{n^2} \}$ is not context-free.

\subsection*{Solution}

Question is solved by proof of contradiction, taking advantage of \textit{The Pumping Lemma}.
Assume $L$ is a context-free language.
Based on the Pumping Lemma, there exists a number $n_G \in \mathbb{N}$ such that if $w \in L(G)$ and $|w| \geq n_G$, then we can write $w = xyzut$ such that $|y| \geq 1$ or $|u| \geq 1$, $|yzu| \leq n_G$ and $xy^nzu^nt \in L(G)$ for all $n \in \mathbb{N}$.
Let $w = a^nb^{n^2}$.
Let also $n_a$ and $n_b$ be defined as number of $a$s and $b$s in $w$ respectively, causing $n_b(w)$ = $n_a^2(w)$.
We show that no matter how we define $y$ and $u$, we break the shape of the language and therefore $L$ cannot be context-free.

\begin{enumerate}

	\item
	It is clear $y$ cannot contain both $a$s and $b$s simultaneously, for by pumping $y$, $a$s appear in the middle of $b$s and $w$ would no longer be in $L$.

	\item
	If $y$ contains only $b$s, it is inevitable that $u$ would contain only $b$s.
	In consequence, by pumping $y$ and $u$, $n_a$ increases while $n_b$ stays unchanged.
	Therefore, $n_a^2 \ge n_b$ thus shape of the language would break.
	Similarly, it can be shown that $y$ and $u$ cannot both contain only $a$s.

	\item
	If $y$ contains only $a$s and $u$ contains only $bs$, let $n_a(y) = s$ and $n_b(u) = s^\prime$.
	By pumping $y$ and $u$ to $y^j$ and $u^j$, it is possible that $(s^\prime j)^2 = (sj)$ by choosing proper $y$ and $u$.
	Yet, by re-pumping $y$ and $u$ once more to $y^{j+1}$ and $u^{j+1}$, $(s^\prime(j+1))^2 \neq (s(j+1))$ and this would break the form of the language.

\end{enumerate}

Therefore, assuming $L$ is context-free is shown to lead to contradicting the pumping lemma.
Hence, the assumption is invalid and $L$ cannot be context-free.


\documentclass{article}
\usepackage{color}
\usepackage{bigints}
\usepackage{amssymb}
\usepackage[italicdiff]{physics}
\color{white}
\definecolor{Blue}{RGB}{0,33,72}
\begin{document}
\pagecolor{Blue}
\title{Solution to Integral Fun\textsc{\char13}s Problem}
\author{Jose Bedoya}
\maketitle
\section{Question}
{\LARGE
$$I(\alpha,n)=\bigintss_{0}^{\infty} \frac{\cos \alpha t}{t^n}\,dt$$
with $\mid n \mid<1$
}
\section{Solution}
{\Large
Knowing,
$$\bigintsss_{0}^{\infty}f(t)g(t)\,dt=\bigintsss_{0}^{\infty}\mathcal{L}\{f(t)\}(s)\mathcal{L}^{-1}\{g(t)\}(s)\,ds$$

\vspace{5mm}
And by choosing
$$f(t)=\cos(\alpha t)$$
$$g(t)=\frac{1}{t^n}$$
we have
$$\mathcal{L}\{f(t)\}(s)=\frac{s}{s^2+\alpha^2}$$
$$\mathcal{L}^{-1}\{g(t)\}(s)=\frac{s^{n-1}}{\Gamma(n)}$$
\newpage
Substituting on our integral
$$I(\alpha,n)=\frac{1}{\Gamma(n)}\bigintsss_{0}^{\infty}\frac{s^n}{s^2+\alpha^2}\,ds$$

\vspace{3mm}
Let $s=\alpha\tan(\theta)$
$$I(\alpha,n)=\frac{\alpha^{n-1}}{\Gamma(n)}\bigintsss_{0}^{\frac{\pi}{2}}\tan^n(\theta)\,d\theta$$
$$=\frac{\alpha^{n-1}}{\Gamma(n)}\bigintsss_{0}^{\frac{\pi}{2}}\sin^n(\theta)\cos^{-n}(\theta)\,d\theta$$

\vspace{5mm}
Recall the definition of the Beta Function
$$B(x,y)=2\bigintsss_{0}^{\frac{\pi}{2}}\sin^{2x-1}(\theta)\cos^{2y-1}(\theta)\,d\theta$$
$$=\frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}$$
where $\Gamma(n)=(n-1)$!.

\vspace{5mm}
Notice, in our integral:
$$x=\frac{n+1}{2}$$
$$y=\frac{1-n}{2}$$

\vspace{3mm}
Therefore,
$$I(\alpha,n)=\frac{\alpha^{n-1}}{2\Gamma(n)}\Gamma\left(\frac{n+1}{2}\right)\Gamma\left(\frac{1-n}{2}\right)$$

\vspace{3mm}
Here we can simplify the expression using the reflection property of the Gamma Function
$$\Gamma(z)\Gamma(1-z)=\frac{\pi}{\sin(\pi z)}$$
for $z$ not integer.

\vspace{3mm}
If we let $z=\frac{n+1}{2}$
$$\Gamma\left(\frac{n+1}{2}\right)\Gamma\left(\frac{1-n}{2}\right)=\frac{\pi}{\sin\left(\frac{\pi(n+1)}{2}\right)}$$
$$=\pi\sec\left(\frac{\pi n}{2}\right)$$

\vspace{2mm}
we get
}
{\LARGE
$$I(\alpha,n)=\frac{\pi}{2}\frac{\alpha^{n-1}}{\Gamma(n)}\sec\left(\frac{\pi n}{2}\right)$$
}
\end{document}

Calculus/Jose Bedoya's questions/Integral with Beta Function (1).tex


\chapter{Basic Classification Models}
\newpage

\section{k Nearest Neighbours}
    kNN is a classical non-parametrized lazy-learning algorithm.

    \subsection{Distance}
        A distance function $d(x,y)$ is a function that satisfies
        \begin{itemize}
            \item Positive Definite. $d(x,y)>0$ and $d(x,y)=0$ iff $x=y$.
            \item Symmetric.
            \item Triangle Inequality. $d(x,y) \le d(x,z) + d(z,y)$.
        \end{itemize}
        Common distance functions include
        \begin{itemize}
            \item $l_2$ distance (Euclidean). $\|x-y\|_2 = \sqrt{\sum(x_i - y_i)^2}$.
            \item $l_1$ distance (Manhattan). $\|x-y\|_2 = \sum |x_i-y_i|$.
            \item $l_{\infty}$ distance. $\|x-y\|_{\infty} = \max\{|x_i-y_i|\}$.
        \end{itemize}

    \subsection{A general form of Euclidean Distance}
        Let $P$ be a projection matrix, we can generalize $l_2$ distance by
        \[\|Px-Py\|_2^2 = (P(x-y))^T(P(x-y)) = (x-y)^TP^TP(x-y)\]
        \[\Rightarrow Dist(x,y) = (x-y)^TA(x-y)\]
        \begin{remark}
            Actually $A$ can be learned.
        \end{remark}

    \subsection{kNN Algorithm}
        Given a dataset $\mathcal{D}$ and an input $x$, the kNN algorithm
        \begin{enumerate}
            \item Computes the distance between $x$ and each $y$ in $\mathcal{D}$.
            \item Finds the nearest $k$ neighbours of $x$ in $\mathcal{D}$.
            \item Determines the class of $x$ by the label of neighbours.
        \end{enumerate}

    \subsection{Choosing k}
        \begin{itemize}
            \item For a binary classification task, $k$ should be an odd number.
            \item $k$ should not be too large or too small.
            \item Parameter tuning techniques can be used to choose a proper $k$.
        \end{itemize}

    \subsection{More on kNN}
        \paragraph{Complexity.}
        kNN requires
        \begin{itemize}
            \item An $O(n)$ time to calculate distances.
            \item An $O(n\log n)$ time to sort and find nearest neighbours.
        \end{itemize}
        \paragraph{Can it be faster?}
        Yes if we use parallelism.

\section{Naive Bayes Classifer}

    \subsection{Posterior, Prior, Likelihood and Evidence}
        Recall Bayes' Rule
        \[ p(w_j|x) = \frac{p(x|w_j) \cdot p(w_j)}{p(x)} \]
        \begin{itemize}
            \item $p(w_j|x)$ is called the \textbf{posterior}.
            \item $p(w_j)$ is called the \textbf{prior}.
            \item $p(x|w_j)$ is called the \textbf{likelihood}.
            \item $p(x)$ is called the \textbf{evidence}.
        \end{itemize}
        \[ Posterior = (Likelihood \cdot Prior) / Evidence \]
        Optimizing w.r.t. $w$ is independent of $p(x)$,
        \[ Posterior \sim (Likelihood \cdot Prior) \]

    \subsection{Bayesian Decision Theory}
        Suppose there are $N$ classes $c_1,c_2,\dots,c_N$. Let $\lambda_{ij}$ be the cost of (mistankenly) classifying a sample in $c_i$ into $c_j$.
        \begin{remark}
            In a more general sense, $c_1, c_2, \dots, c_N$ is more than just a set of classes. It can be seen as a set of ``actions''.
        \end{remark}

        \begin{definition}[Conditional Risk]
            The \textbf{expected loss}, or the \textbf{conditional risk} is defined as
            \[ R(c_i|x) = \sum_{j=1}^N \lambda_{ij}p(c_j|x) \]
        \end{definition}
        Our goal is to find a minimizer $h(x)$ of the general conditional risk $R(h) = \mathbb{E}[R(h(x)|x)]$.

        For each $x$, if $h(x)$ minimizes $R(h(x)|x)$, then obviously $h(x)$ also minimizes $R(h)$. The minimizing $h^*(x)$ is called the \textbf{Bayes optimal classifier}, and $R(h^*)$ is called the \textbf{Bayes risk}.

        If
        \[
        \lambda_{ij} =
        \begin{cases}
            0 \quad & i = j\\
            1 \quad & otherwise
        \end{cases}
        \]
        Then
        \[ R(c|x) = 1 - p(c|x) \]
        minimizing this risk is equivalent to maximizing $p(c|x)$ and
        \[ h^*(x) = \arg \max_{c_i} p(c_i|x) = \arg \max_{c_i} \frac{p(x|c_i)p(c_i)}{p(x)} = \arg\max_{c_i} p(x|c_i)p(c_i)\]
        So we choose the $x$ that \emph{maximizes posterior}.

        \begin{remark}
            A \textbf{discriminative model} directly models likelihood $p(c|x)$, while a \textbf{generative model} models joint distribution $p(x,c)$ by modeling $p(c) \cdot p(x|c)$.
        \end{remark}

        \subsubsection{Example.}
        For a binary classification (decision-making), if
        \[ R(c_1|x) < R(c_2|x) \]
        then we take $c_1$.

        This is equivalent to taking $c_1$ if
        \[ (\lambda_{21}-\lambda_{11})p(x|c_1)p(c_1) > (\lambda_{12}-\lambda_{22})p(x|c_2)p(c_2) \]
        or
        \[ \frac{p(x|c_1)}{p(x|c_2)} > \frac{\lambda_{12} - \lambda_{22}}{\lambda_{21} - \lambda_{11}} \cdot \frac{p(c_2)}{p(c_1)} \]

        If we further assume that $p(x|C_1)$ and $p(x|c_2)$ have Gaussian distributions with the same variance, then we test whether
        \[ \exp\left\{ -\frac{1}{2}(x-\mu_1)^2/\sigma^2 + \frac{1}{2}(x-\mu_2)^2/\sigma^2 \right\} \ge \lambda \]
        where $\lambda$ is a threshold.
        Taking logarithm and simplifying
        \[ (\mu_1/\sigma^2 - \mu_2/\sigma^2)x + constant \ge \log\lambda \]
        i.e.
        \[ ax+b > c \]
        or in higher dimensions
        \[ w^Tx > b \]
        It is a linear classifier.

    \subsection{Parameter Estimation}
    \subsubsection{Maximum Likelihood Estimation}
    Let $D$ be a dataset with $n$ i.i.d. samples $x_i$, MLE maximizes
    \[ p(D|\theta) = \prod_{i=1}^n p(x_i|\theta) \]
    to avoid underflow of floating point operations, we take the log likelihood
    \[ l(\theta) = \log(p(D|\theta)) = \sum_{i=1}^n \log(p(x_i|\theta)) \]

    \subsubsection{Example of MLE}
    Consider the multivariate Gaussian distribution with dimension $p$
    \[ f(x) = \frac{1}{(2\pi)^{p/2}|\Sigma|^{1/2}}\cdot \exp \left\{ -\frac{1}{2}(x-\mu)^T\Sigma^{-1}(x-\mu) \right\} \]
    The log likelihood is
    \[ \log\left( p(x_i|\theta) = -\frac{1}{2}\log\left[(2\pi)^p|\Sigma|\right] - \frac{1}{2}(x_i - \mu)^T\Sigma^{-1}(x_i - \mu)  \right) \]
    Taking gradient w.r.t. $\mu$
    \[ \nabla_{\mu} = \Sigma^{-1}(x_i-\mu) \]
    And the MLE estimator is
    \[ \hat{\mu} = \frac{1}{n}\sum_{i=1}^n x_i \]
    The covariance matrix can be estimated similarly.

    \subsubsection{Maximum-A-Posterior}
    Beyond the scope.

    \subsection{Naive Bayes}
        \paragraph{Notations.}
            \begin{itemize}
                \item $X$: $N \times d$ training set.
                \item $x$: a $d$-dim data sample.
                \item $x^{(i)}$: the $i$-th sample in $X$.
                \item $y^{(i)}$: the label of $x^{(i)}$.
                \item $x_j$: the $j$-th feature in $x$.
                \item $c_i$'s: classes.
            \end{itemize}
        The naives Bayes classifier makes the attribute conditinoal independence assumption, aka. the naive Bayes assumption: \emph{all features of x are independent}.
        \[ p(x_1, x_2,\dots,x_d | y) = \prod_{i=1}^d p(x_i|y) \]
        Then we can compute the optimal classifier by
        \[ h_{nb}(x) = \arg\max_{c}p(c)\prod_{j=1}^d p(x_j|c) \]
        Therefore a naive Bayes classifier estimates $p(c)$ and $p(x_j|c)$.

        If we assume $p(x^{(i)}|c) \sim \mathcal{N}(\mu_{c,i}, \sigma^2_{c,i})$, then
        \[ p(c) = \frac{\sum_{i=1}^N\mathbb{I}[y^{(i)}=c]}{N} \]
        i.e. the frequency of $x$ with label $c$ in $X$.
        \[ p(x_j|c) = \frac{\sum_{i=1}^N\mathbb{I}[x^{(i)}_j = 1, y^{(i)}=c]}{\sum_{i=1}^N\mathbb{I}[y^{(i)}=c]} \]
        i.e. the frequency of those $x$ with feature $x_j$ in all $x$ with label $c$.

    \subsection{Laplace Smoothing}
        If an attribute value has never appeared in the training set, the output will either be $0$ or $\frac{0}{0}$.

        To fix this, we introduce \textbf{Laplace smoothing}.
        \[ p(y=i) = \frac{\sum_{j=1}^N\mathbb{I}[y^{(j)=i}] + 1}{N + k} \]
        where $k$ is the number of classes.

\section{Decision Trees and Ensembling}
    A decision tree is a tree-based classifier. It has a root, several leaf nodes and several internal nodes. Each leaf is a decision and in each internal node we perform a test on one or more attributes of input.

    \subsection{Entropy and Information Gain}
    \begin{definition}[Entropy]
        Given a dataset $D$ with $k$ classes, suppose the proportion of the $i$-th class in $D$ is $p_i$. The entropy is defined by
        \[ H(D) = -\sum_{i=1}^k p_i \log_2 p_i \]
    \end{definition}
    \begin{remark}
            The larger $H(D)$ is, the more information $D$ contains.
    \end{remark}

    \begin{definition}[Conditional Entropy]
        Suppose an attribute $a$ has $V$ possible values. If we split $D$ based on the value of $a$, we will have $V$ branches.
        \[ H(Y|X) = \sum_{v=1}^V \mathbb{P}[X=x_v]\sum_{i=1}^k\mathbb{P}[Y=y_i|X=x_v]\log_2\mathbb{P}[Y=y_i|X=x_v] \]
        or
        \[ H(Y|X) = \sum_{v=1}^V\frac{|D_v|}{|D|}H(D_v) \]
    \end{definition}
    \begin{remark}
        Conditional Entropy is a weighted sum.
    \end{remark}

    \begin{definition}[Information Gain]
        Given a dataset $D$ and an attribute $a$, the information gain of splitting $D$ by $a$ is defined by
        \[ IG(D,a) = H(D) - \sum_{v=1}^V\frac{|D_v|}{|D|}H(D_v) \]
    \end{definition}
    \begin{remark}
        Larger information gain usually means splitting $D$ by $a$ results in more ``pure'' subsets. So choosing $a_* = \arg\max IG(D,a)$ is a common way of choosing attributes when training decision trees.
    \end{remark}

    \begin{definition}[Gain Ratio]
        The \textbf{gain ratio} is defined by
        \[ GainRatio(D,a) = \frac{Gain(D,a)}{IV(a)} \]
        where $IV(a)$ is the \textbf{intrinstic value} of attribute $a$,
        \[ IV(a) = -\sum_{v=1}^V \frac{|D^v|}{|D|}\log_2\frac{|D^v|}{|D|} \]
    \end{definition}
    \begin{remark}
        Information gain prefers attributes with more values, and gain ratio is proposed to mitigate the negative effects. Gain ratio is used in the famous C4.5 algorithm.

        However, gain ratio prefers attributes with fewer possible values, and the C4.5 algorithm actually uses a heuristic approach: choose those attributes whose information gains are above the average, and then choose the one with the highest gain ratio.
    \end{remark}

    \subsection{Gini Index}
        \begin{definition}[Gini]
            \[ Gini(D) = \sum_{i=1}^{k}\sum_{i' \neq i} p_ip_{i'} = 1 - \sum_{i=1}^k p_i^2 \]
        \end{definition}

        \begin{definition}[Gini Index]
            The \textbf{Gini index} is defined by
            \[ GiniIndex(D, a) = \sum_{v=1}^V\frac{|D^v|}{|D|}Gini(D) \]
        \end{definition}

    \subsection{Generating a Decision Tree}
        A decision tree is generated recursively, and there are three return conditions
        \begin{enumerate}
            \item All samples in current data set belongs to the same class.
            \item No available attributes exist for splitting (the attribute set is empty, or all samples take the same value on all atrributes)
            \item No samples exist in current data set.
        \end{enumerate}

    \subsection{Ensemble Learning}
        Ensemble learning constructs multiple weak individual classifiers and combines their prediction outputs.

        Suppose the final prediction is determined by voting (the output will be $1$ if more than half classifiers output $1$), when there are $T$ base classifiers,
        \[ H(x) = \mathrm{sign}\left( \sum_{i=1}^T h_i(x) \right) \]

        Suppose the error rates of individual classifiers are independent, by Heoffding Inequality,
        \[ P(H(x) \neq f(x)) = \sum_{k=0}^{T/2}C_T^k(1-\epsilon)^k\epsilon^{T-k} \le \exp\left( -\frac{1}{2}T(1-2\epsilon)^2 \right) \]

        Therefore as we increase the number of base classifiers, the error rate drops exponentially. However, notice that this conclusion is based on the assumption that all classifiers are independent, which in practice is barely possible.

        \subsubsection{Bagging}
        While it is practically difficult to make each individual classifier independent of each other, it is still possbile to maximize their variances.
        \begin{enumerate}
            \item Construct $T$ datasets, each containing $m$ samples, with bootstrapping.
            \item Train $T$ models with the datasets.
            \item Ensemble by voting or taking means.
        \end{enumerate}
        \begin{remark}
            From the perspective of bias-variance decomposition, bagging reduces the variance term in the error.
        \end{remark}

        \subsubsection{Random Forests}
        Random forests uses decision trees in bagging. Furthermore, it adds randomness in attribute selection.

        For each node, instead of choosing the best attribute from all attributes, RF first sample a subset of $k$ attributes, and then choose the best attribute from this subset. This further increases the diversity of base classifiers.


\lab{Algorithms}{Eigenvalue Solvers}{Eigenvalue Solvers}
\label{Ch:EigSolve}

\objective{Implement the $QR$ algorithm for finding eigenvalues.}

\section*{Eigenvalues are hard to find}

Finding the eigenvalues of $n \times n$ matrix $A$ means solving the following equation, where $x$ is a nonzero vector and $\lambda$ is a scalar.
\begin{align}
 A x                       &=  \lambda x  \notag \\
A x - \lambda x &= 0 \notag \\
(A - \lambda I)x  &= 0 \label{eq:singularity}
\end{align}
Since $x$ is nonzero, \eqref{eq:singularity} means $A-\lambda I$ must be singular. Thus $det(A-\lambda I) = 0$.  This determinant is often notated $det(A-\lambda I) = p(\lambda)$ and is called the \emph{characteristic polynomial} of $A$. The roots of the characteristic polynomial are the eigenvalues of $A$.

If $A$ is $n \times n$, the degree of $p(\lambda) = n$.   Finding the roots is easy for small $n$, but it becomes difficult or impossible as $n$ increases.  Abel's Theorem  outlines the problem.

\begin{theorem}
\label{Theorem:Abel}
{\bf Abel's Impossibility Theorem:} There is no general algebraic solution for solving a polynomial equation of degree $n>4$.
\end{theorem}

Therefore, there is no method that will exactly find the eigenvalues of an arbitrary matrix. This is a significant result. In practice it means that we often rely on iterative methods, which converge to the eigenvalues.

\section*{The $QR$ algorithm}

There are many such iterative methods for finding eigenvalues. We will explore one of the simplest: the $QR$ algorithm. The following recurrence describes the $QR$ Algorithm in its most basic form.

\begin{equation*}
A_0 = A, \quad A_k = Q_k R_k, \quad A_{k+1} = R_k Q_k
\end{equation*}

where $Q_k, R_k$ is the $QR$ decomposition of $A_k$. Yes, it's as easy as it looks. All this algorithm does at each step is find the $QR$ decomposition of $A_k$ and multiply $Q_k$ and $R_k$ together again but in the opposite order. How does this simple algorithm find the eigenvalues of $A$?

\begin{comment}
\begin{problem}
\label{problem:similarity proof}
Prove that $A_{k+1} \sim A_k$ (where $\sim$ denotes matrix similarity). Then prove that $A_n \sim A$ for all $n$.
\end{problem}
\end{comment}

$A_{k+1} \sim A_k$ (where $\sim$ denotes matrix similarity). Then $A_n \sim A$ for all $n$. This statement shows that $A_n$ has the same eigenvalues as $A$. Preservation of eigenvalues is the first important feature that makes the algorithm work. The other important feature is that each iteration of the algorithm effectively transfers some of the``mass" from the lower to the upper triangle. Under very general conditions, $A_n$ will converge to a matrix of the form

\begin{equation*}
\label{eq:Schur form}
S =
     \begin{pmatrix}
          S_1 &* & \cdots & * \\
           0     &S_2  &  \ddots & \vdots \\
           \vdots  & \ddots & \ddots & *  \\
           0 & \cdots & 0 & S_m
    \end{pmatrix}
\end{equation*}
where $S_i$ is either a $1 \times 1$ or a $2 \times 2$ matrix. For most matrices $A$, all the $S_i$ will be $1 \times 1$, so $S$ will be an upper triangular matrix. In this case, $S$ is called the \emph{Schur form} of $A$. The eigenvalues of $A$ are on the main diagonal of $S$.

The only case where $S$ is not upper triangular is when $A$ is a real but not symmetric matrix. In this case, though $A$ is real, it may have complex eigenvalues. These eigenvalues occur in complex conjugate pairs. Each of these pairs corresponds to a $2 \times 2$ block in $S$, where the eigenvalues of the $2 \times 2$ block are the complex conjugate pair of eigenvalues of $A$. In this case, $S$ is called the \emph{real Schur form} of $A$.

\subsection*{Hessenberg preconditioning}

Recall from Lab \ref{Ch:Householder reflections} that an upper Hessenberg matrix looks like
\[
\begin{pmatrix}
*           & * & \ast & \cdots   & *\\
*           & * & * & \cdots   & * \\
0          & * & * &  \cdots&* \\
\vdots &  & \ddots    & \ddots & \vdots \\
0 & \cdots & 0 & * & *\\
\end{pmatrix}
\]
and that every matrix is similar to an upper Hessenberg matrix. Hessenberg reduction also preserves eigenvalues. It is a good idea to reduce to Hessenberg form before continuing with the $QR$ algorithm. You'll converge to the Schur form faster this way, since Hessenberg matrices are already close to upper triangular.

\begin{problem}
Code the $QR$ algorithm. Have your function accept an $n \times n$ matrix $A$ and a number of iterations, and return all the eigenvalues of $A$. Note that you will need to find the eigenvalues of the $2 \times 2$ $S_i$ directly, if there are any. Since your own implementations of $QR$ decomposition and Hessenberg reduction may not handle complex matrices, you should use the ones in \li{scipy.linalg}.
\end{problem}

\begin{comment}

\begin{problem}
If $A$ is normal, its Schur form is diagonal. For normal $A$, have your function additionally output the eigenvector corresponding to each eigenvalue. Hint 1: Test your function on Hermitian and real symmetric matrices; they are both normal. Hint 2: Your work in Problem \ref{problem:similarity proof} will help. You have already made all the necessary calculations, you just need to store the information correctly.
\end{problem}

\end{comment}

\begin{problem}
Test your implementation with random matrices. Try real, complex, symmetric, and Hermitian matrices. Compare your output to the output from the eigenvalue solver. How many iterations are necessary? How large can $A$ be?
\end{problem}

The $QR$ algorithm is not the only iterative method used to find eigenvalues. Arnoldi iteration is similar to the $QR$ algorithm but exploits sparsity. Other methods include the Jacobi method and the Rayleigh quotient method.

It is important to remember that eigenvalue solvers can be wrong, particularly for matrices that are ill-conditioned.


\chapter{Monte Carlo Estimation}\label{S:MCEstim}
Various problems can be solved by taking advantage of randomness and random number generators.  Such an approach yields a Monte Carlo solution.  %We focus on Monte Carlo integration here.

\section{Monte Carlo Integral Estimation}\label{S:BMC}
Suppose we want to estimate the integral
\[
\vartheta^* = \int_{A} h(x) \, dx \ , \text{where, } h(x): \Rz^k \to \Rz, A \subset \Rz^k \ .
\]
If the integrand function $h$ is simple enough, eg.~polynomial, simple transcendal or trigonometric, then we can evaluate $\vartheta^*$ {\bf analytically by hand}.  When this is not the case, then a closed form expression for $\vartheta^*$ may not exist and we may approximate it by {\bf numerical quadrature}, eg.~Mid-point rectangles rule ({\tt QuadMR}), or adaptive Simpson's quadrature rule ({\tt quad}) or adaptive Lobatto's quadrature rule ({\tt quadl}).  Numerical quadratures are inefficient in higher dimensions since the number of samples or grid-points grow exponentially with the dimension $k$ of the domain $A$.

{\bf Basic Monte Carlo (BMC) integral estimation} or {\bf Basic Monte Carlo integration} is a stochastic method to estimate the integral $\vartheta^*$ by its point estimate $\widehat{\varTheta}_n$ based on $n$ samples drawn at random from the domain $A$.  BMC integration is renowned for its {\bf simplicity}, {\bf generality} (class of integrands) and {\bf scalability} (dimension of the  integration domain).

Let the domain be a $k$-dimensional box or hyper-cuboid $A=([\underline{a}_1,\overline{a}_1], [\underline{a}_2,\overline{a}_2] , \ldots, [\underline{a}_k,\overline{a}_k] )$.  We can rewrite the integral as:
\begin{equation}\label{E:BMCI}
\vartheta^* = \int_A h(x)\, dx = \int_A w(x) f(x)\, dx \ ,
\end{equation}
where the functions $w(x)$ and $f(x)$ are:
\[
w(x)= h(x) \prod_{j=1}^k (\overline{a}_j - \underline{a}_j) , \qquad f(x)=\frac{1}{\prod_{j=1}^k (\overline{a}_j - \underline{a}_j) } \ .
\]
The PDF of the RV $X \sim \uniform(A) = \uniform(\underline{a}_1,\overline{a}_1) \times  \uniform(\underline{a}_2,\overline{a}_2) \times \cdots \times  \uniform(\underline{a}_k,\overline{a}_k)$ is $f(x)$ over the box $A$.  Therefore:
\[
\vartheta^* =  \int_A w(x) f(x)\, dx = \E_f(w(X)) = \E(w(X)) \ .
\]
We subscript the expectation $E$ by $f$ to emphasize the PDF with respect to which we integrate.  Thus, if we generate $X_1,X_2,\ldots,X_n \overset{\IID}{\sim} \uniform(A)$, then by the WLLN:
\[
\widehat{\varTheta}_n := \frac{1}{n} \sum_{i=1}^n w(X_i) \overset{P}{\longrightarrow} \E(w(X_1)) = \vartheta^* \ .
\]
The standard error:
\[
\mathsf{se}_n := \sqrt{\V(\widehat{\varTheta}_n)} = \sqrt{\V(\frac{1}{n} \sum_{i=1}^n w(X_i))} = \sqrt{\frac{1}{n^2} \sum_{i=1}^n \V( w(X_i))} = \sqrt{\frac{1}{n^2} n \V( w(X_1))} = \frac{1}{\sqrt{n}} \sqrt{\V( w(X_1))} \ .
\]
We can get the estimated standard error $\widehat{\mathsf{se}}_n$ of the integral estimate $\widehat{\varTheta}_n$ from the sample standard deviation $s_n$:
\[
\widehat{\mathsf{se}}_n = \frac{s_n}{\sqrt{n}}, \qquad
s_n^2 = S_n^2((y_1,y_2,\ldots,y_n)) = \frac{1}{n-1} \sum_{i=1}^n \left( y_i - \widehat{\varTheta}_n \right)^2 \ ,
\]
where, $Y_i = w(X_i)$.  A Normal-based $1-\alpha$ confidence interval (CI) for $\vartheta^*$ is $\widehat{\varTheta}_n \pm z_{\alpha/2} \widehat{\mathsf{se}}_n$.  Therefore, we can take $n$ as large as we want and thereby shrink the width of the confidence interval as small as we want.  Our recipe for estimating $\vartheta^*$ is simply summarized in \hyperref[A:BMCI]{Algorithm \ref*{A:BMCI}}.
\begin{algorithm}
\caption{Basic Monte Carlo Integral Estimation for $\vartheta^*= \int_A h(x) dx$\label{A:BMCI}}
\begin{algorithmic}[1]
\STATE{
{\it input:}
\begin{enumerate}
\item $n \gets$ the number of samples.
\item $h(x) \gets$ the integrand function over $\Rz$
\item $[\underline{a}_j,\overline{a}_j] \gets$ lower and upper bounds of integration for each $j=1,2,\ldots,k$
\item capability to draw $nk$ IID samples from $\uniform(0,1)$ RV
\end{enumerate}
}
\STATE{{\it output:} a point estimate $\widehat{\vartheta}_n$ of $\vartheta^*$ and the estimated standard error $\widehat{\mathsf{se}}_n$}
\STATE{{\it initialize:} $y \gets (0,0,\ldots,0)$, initialize $y$ as a zero vector of length $n$}
\WHILE{$i \leq n$}
\STATE{
\begin{enumerate}
\item $i \gets i+1$,
\item $x_i = (x_{i,1},x_{i,2},\ldots,x_{i,k})$, with $x_{i,j} \gets u_j$, $u_j \sim \uniform(\underline{a}_j,\overline{a}_j)$, for $j=1,2,\ldots,k$,
\item $y_i \gets w(x_i)= h(x_i) \prod_{j=1}^k (\overline{a}_j-\underline{a}_j) $
\end{enumerate}
}
\ENDWHILE
\STATE{{\it compute:}
\begin{enumerate}
\item $\widehat{\vartheta}_n \gets \overline{y}_n$, the sample mean of $y=(y_1,y_2,\ldots,y_n)$
\item $\widehat{\mathsf{se}}_n = s_n(y) / \sqrt{n}$, where $s_n(y)$ is the sample standard deviation of $y$
\end{enumerate}
}
\STATE{{\it return:} $\widehat{\vartheta}_n$ and $\widehat{\mathsf{se}}_n$}
\end{algorithmic}
\end{algorithm}

\begin{labwork}[1D integral over an interval]\label{LW:1DintegralOverInterval}
Estimate $\vartheta^* := \int^{0.5}_0x(1-x^2)^{3/2}dx$ using \hyperref[A:BMCI]{Algorithm \ref*{A:BMCI}}.  We use IID samples $x_1,x_2,\ldots,x_n$ from $\uniform(0,0.5)$ RV to estimate $\vartheta^*$ as follows:
\[
\widehat{\vartheta}_n = \frac{1}{n} \sum_{i=1}^n w(x_i) = \frac{1}{n} \sum_{i=1}^n h(x_i) (0.5-0), \quad \text{where, } h(x_i)= x_i(1-x_i^2)^{3/2} \enspace .
\]
The estimation procedure may be implemented as follows:
\begin{VrbM}
>> rand('twister',189783)% initialise the fundamental sampler
>> N=1000000; % estimate from 1000000 samples
>> Xs=0.5*rand(1,N); % save 1 million samples from Uniform(0,0.5) in array Xs
>> Ws=(Xs .* ((1 - (Xs .^ 2)) .^ 1.5)) * 0.5; % h(Xs)* 1/f, f is density of Uniform(0,0.5)
>> MCEst=mean(Ws(1:N)) % point estimate of the integral
MCEst =    0.1026
>> StdErr=std(Ws(1:N))/sqrt(N) % estimated standard error
StdErr =   5.0026e-05
>> % approximate 95% confiednce interval of the estimate
>> [MCEst-2*StdErr MCEst+2*StdErr]
ans =
    0.1025    0.1027
\end{VrbM}

The estimates and the associated confidence intervals for different sample sizes  are:
$$
\begin{array}{ccc}
\hline
n		&\widehat{\vartheta}_n		&\approx 95\%\textrm{~C.I.}\\ \hline
10^2&		0.1087			&	(0.0985, 0.1189)\\
10^3	&		0.1055			&	(0.1023, 0.1086)\\
10^4	&		0.1028			&	(0.1018, 0.1038)\\
10^5	&		0.1023			&	(0.1020, 0.1027)\\
10^6	&		0.1026			&	(0.1025, 0.1027)\\ \hline
\end{array}
$$
The exact answer is:
$$\int^{0.5}_0x(1-x^2)^{3/2}dx=\left( -\frac{(1-x^2)^{5/2}}{5} \right]^{0.5}_0=0.10257.$$
\end{labwork}

\begin{labwork}[2D integral over a rectangle]\label{LW:2DintegralOverRectangle}
Let us estimate the area of a circle $C:=\{(x,y): \sqrt{x^2+y^2} \leq 1\}$ centred at the origin with unit radius using IID uniform samples from a unit square $[-1,1]^2 := [-1,1] \times [-1,1]$ that contains $C$.  Therefore, the integral of interest:
$$
\vartheta^*:=\int_{-1}^{1} \int_{-1}^1 \BB{1}_C (u_1,u_2) d u_1 d u_2 =\int_{-1}^{1} \int_{-1}^1 \left( 4 \ \BB{1}_C (u_1,u_2) \right) \frac{1}{4} \ d u_1 d u_2 \ ,
$$ where $ \frac{1}{4} \ \BB{1}_{[-1,1]^2} (u_1,u_2)$ is the joint PDF of $\uniform([-1,1]^2)$ RV, and the Monte Carlo estimate of $\vartheta^*$ based on $n$ samples from $\uniform([-1,1]^2)$ is:
\[
\widehat{\vartheta}_n = \frac{1}{n} \sum_{i=1}^n 4 \ \BB{1}_{C} (u_1,u_2)
\]
We know that our integral, namely the area of the unit circle, is $\pi ({\rm radius})^2=\pi 1^2=\pi=3.1416\ldots$.
\begin{VrbM}
>> rand('twister',188);%Initialise the fundamental sampler
>> N=10000; % sample size of ten thousand
>> Ws=zeros(1,N); % initialise a vector of zeros
>> % produce N pairs of Uniform(0,1) numbers and set to 4 if they fall inside a unit circle
>> Ws(find( sqrt(sum(rand(2,N) .^ 2)) <= 1.0 ))=4;
>> MCEst=mean(Ws(1:N)); % MC estimate
>> StdErr=std(Ws(1:N))/sqrt(N); % estimated standard error
>> disp([MCEst StdErr MCEst-2*StdErr MCEst+2*StdErr])% display estimate, std error, and approx 95% CI
    3.1476    0.0164    3.1148    3.1804

>> % with N=10^7 samples we get a better approximation
>> rand('twister',188); N=10000000; Ws=zeros(1,N); Ws(find( sqrt(sum(rand(2,N) .^ 2)) <= 1.0 ))=4;
>> MCEst=mean(Ws(1:N)); StdErr=std(Ws(1:N))/sqrt(N);
>> disp([MCEst StdErr MCEst-2*StdErr MCEst+2*StdErr])
    3.1420    0.0005    3.1409    3.1430
>> pi % correct value of pi
ans =    3.1416
\end{VrbM}
\end{labwork}

We can also estimate the integral of functions over unbounded domain by first transforming them to one over a bounded domain.
For the integral:
$$\vartheta^* :=\int^{\infty}_0h(x)dx \ ,$$
the substitution $y=1/(x+1)$ will change the interval of integration to a bounded one:
$$\vartheta^*=\int^{\infty}_0h(x)dx=\int^0_1 h \left( \frac{1}{y}-1 \right) \frac{-1}{y^2}dy=\int^1_0 h \left( \frac{1}{y}-1 \right) \frac{1}{y^2}dy.$$
Therefore, with $y_1,\ldots,y_n \overset{IID}{\sim} \uniform(0,1)$, the integral can be estimated by:
$$
\widehat{\vartheta}_n=\frac{1}{n}\sum^n_{i=1} h \left(\frac{1}{y_i}-1\right) \frac{1}{y_i^2} \ .
$$
Similarly, we can estimate the integral:
$$
\vartheta^* := \int^{\infty}_{-\infty}h(x)dx=\int^{0}_{-\infty}h(x)dx+\int^{\infty}_{0}h(x)dx=\int^{\infty}_{0} \left( h(-x)+h(x) \right) dx \ ,
$$
can be estimated by:
$$\widehat{\vartheta}_n=\frac{1}{n}\sum^n_{i=1} \left( h \left(1-\frac{1}{y_i} \right)+h\left( \frac{1}{y_i}-1 \right) \right) \frac{1}{y_i^2} \ .
$$

\begin{labwork}[1D Integral with Unbounded Domain]\label{LW:1DintegralOverUnboundedDomain}
Estimate $\vartheta^* = \int^{\infty}_{-\infty}e^{-x^2}dx$ by the Monte Carlo estimate $\widehat{\vartheta}_n = \frac{1}{n}\sum_{i=1}^n e^{-x_i}, \ x_i \overset{IID}{\sim} \uniform(0,1)$
\Matlab code:
\begin{VrbM}
>> y=rand(1,1000000);
>> Hy = (exp(- ((1 - (1./y)).^2)) + exp(- (( (1./y) - 1).^2))) ./ (y .* y);
>> mean(Hy)
ans =    1.7722
>> disp([mean(Hy)- 1.96*std(Hy)/sqrt(1000000), mean(Hy)+ 1.96*std(Hy)/sqrt(1000000)])
    1.7694    1.7749
\end{VrbM}
The Monte Carlo estimates $\widehat{\vartheta}_n$ of $\vartheta^*$ for different sample sizes $n$ are:
$$\begin{array}{ccc}
\hline
n	&\widehat{\vartheta}_n	&	\approx~ 95\% \textrm{~C.I.}\\ \hline
		10^2		&  1.7062			&(1.4, 2.0)\\
		10^4		&1.7849  			&(1.75, 1.81)\\
		10^6		& 1.7717			&(1.769, 1.775)\\ \hline
\end{array}$$
The exact answer is:
$$\int^{\infty}_{-\infty}e^{-x^2}dx=\sqrt{\pi}=1.7725  \textrm{(  4 decimal places)},$$
which can be obtained by noting that $e^{-x^2}$ is the un-normalised $\normal(0,0.5^2)$ density.
\end{labwork}

\section{Variance Reduction via Importance Sampling}\label{S:IS}

Consider the problem of estimating the integral $\vartheta^* = \int h(x) f(x) dx$, where $f(x)$ is a PDF.  In basic Monte Carlo method, we can simulate from the RV $X$ with PDF $f$.  However, there are situations where we may not be able to draw samples from $X$.  {\bf Importance sampling} overcomes this problem by generalising the basic Monte Carlo method.   Suppose $g(x)$ is a PDF from which we can draw samples, then
\[
\vartheta^* = \int h(x) f(x) dx = \int \frac{h(x) f(x)}{g(x)} g(x) dx = \E_{g}(Y) \ ,
\]
where, $Y=h(X)f(X)/g(X)$ and the expectation $\E_g(Y)$ is taken with respect to the PDF $g$.  Therefore, we can simulate $x_1,x_2,\ldots,x_n \overset{IID}{\sim} g$ and estimate $\vartheta^*$ by:
\[
\widehat{\vartheta}_n = \frac{1}{n} \sum_{i=1}^n y_i =  \frac{1}{n} \sum_{i=1}^n \frac{h(x_i) f(x_i)}{g(x_i)} \ .
\]
By the law of large numbers, $\widehat{\varTheta}_n \overset{P}{\to} \vartheta^*$, however $\widehat{\varTheta}_n$ may have an infinite standard error if $g(x)$ is chosen poorly.  Since the estimator $\widehat{\varTheta}_n$ of $\vartheta^*$ is the mean of $w(X) = h(X)f(X)/g(X)$ and the second moment of $W(X)$, given by:
\[
\E_g(w^2(X)) = \int \left(w(x)\right)^2 g(x) dx = \int \left( \frac{h(x) f(x)}{g(x)} \right)^2 g(x) dx = \int \frac{h^2(x) f^2(x)}{g(x)} dx
\]
may be infinite if $g$ has thinner tails than $f$.  Moreover, $\E_g(w^2(X))$ may be large if $g(x)$ is small over some set $A$ where $f(x)$ is large, since the ratio $f/g$ over $A$ could become large.  Therefore, we want the {\bf importance sampling density} $g$ to have thicker tails than $f$ and also be of similar shape to $f$ to minimise the ratio $f/g$.  In fact, the optimal choice of the importance sampling density $g$ is given by the following proposition.

\begin{prop}[Optimal Importance Sampling Density]
The optimal choice for the importance sampling density $g$ that minimises the variance of the importance sampling estimator
$$
\widehat{\varTheta}_n = \frac{1}{n} \sum_{i=1}^n \frac{h(X_i) f(X_i)}{g(X_i)}, \quad X_1,X_2,\ldots,X_n \overset{IID}{\sim} g
$$
of the integral:
\[
\vartheta^* = \int h(x) f(x) dx \ ,
\]
is
\[
g^*(x) = \frac{|h(x)| f(x)}{\int |h(t)| f(t) dt} \ .
\]
\begin{proof}
Let $w(X) := \frac{f(X)h(X)}{g(X)}$.  The variance of $w(X)$ is:
\begin{flalign*}
\V_g(w(X))
&= \E_g(w^2(X)) - (\E_g(w(X)))^2 \\
&= \int w^2(x) g(x) dx  - \left( \int w(x) g(x) dx \right)^2 \\
&= \int \frac{h^2(x) f^2(x)}{g^2(x)} g(x) dx  - \left( \int \frac{h(x)f(x)}{g(x)} g(x) dx \right)^2 \\
&= \int \frac{h^2(x) f^2(x)}{g^2(x)} g(x) dx  - \left( \int h(x)f(x) dx \right)^2 \\
\end{flalign*}
Note that $\left( \int h(x)f(x) dx \right)^2$ does not depend on $g$, therefore, minimisation of
\[
\V_g(\widehat{\varTheta}_n) = \V_g \left( \frac{1}{n} \sum_{i=1}^n \frac{h(X_i) f(X_i)}{g(X_i)} \right) = \V_g \left( \sum_{i=1}^n  \frac{1}{n} w(X_i) \right) =  n \frac{1}{n^2} \V_g(w(X_1)) = \frac{1}{n} \V_g(w(X_1))
\]
over all possible densities $g$, is equivalent to minimisation of $\E_g(w^2(X)) = \int \frac{h^2(x) f^2(x)}{g^2(x)} g(x) dx$, where $X \sim g$.  Due to Jensen's inequality:
\begin{flalign*}
\E_g(w^2(X))
& \geq \left( \E_g(|w(X)|) \right)^2 \\
& = \left( \int \frac{|h(x) f(x)|}{|g(x)|} g(x) dx \right)^2
= \left( \int \frac{|h(x)| f(x)}{g(x)} g(x) dx \right)^2
= \left( \int |h(x)| f(x) dx \right)^2
\end{flalign*}
This establishes a lower bound on $\E_g(w^2(X))$ and thereby on $\V_g(\widehat{\varTheta}_n)$.  This lower bound is achieved when $g(x)=g^*(x)=\frac{|h(x)|f(x)}{\int |h(t)| f(t) dt}$:
\begin{flalign*}
\E_{g^*}(w^2(X))
&= \int \frac{h^2(x)f^2(x)}{{g^*}^2(x)} g^*(x) dx
= \int \frac{|h(x)|^2f^2(x)}{\frac{|h(x)|^2f^2(x)}{\left(\int |h(t)| f(t) dt \right)^2}} \frac{|h(x)|f(x)}{\int |h(t)| f(t) dt} dx \\
&= \int \left(\int |h(t)| f(t) dt \right)^2 \frac{|h(x)|f(x)}{\int |h(t)| f(t) dt} dx
= \left(\int |h(t)| f(t) dt \right) \int |h(x)|f(x) dx \\
&= \left( \int |h(x)| f(x) dx \right)^2
\end{flalign*}
\end{proof}
\end{prop}
There is no free lunch.  If we can't sample from $f(x)$, then we probably can't sample from the more complicated optimal importance sampling density $g(x)^*=|h(x)|f(x)/\int |h(t)| f(t) dt$ either.  In practice, we sample from a thick-tailed density $g$ that is as close a possible to $g^*=|h|f$.

\begin{labwork}[Estimating $\P(Z>\pi)$]\label{LW:GaussianTailProbs}
Compare the estimates and standard errors of the following estimators of the Gaussian tail probability:
$$
\vartheta^* = \P(Z>\pi) = \int_{-\infty}^{\infty} \BB{1}_{(\pi,\infty)}(x) \phi(x) dx, \quad \text{where, $Z \sim \normal(0,1)$, $\phi(x)$ is PDF of $Z$} \ ,
$$
for different sample sizes $n=\{10^2,10^4, 10^6\}$, based on four different importance sampling densities:
\begin{enumerate}
\item $g^{(1)}=\phi$, the PDF of $\normal(0,1)$ (simulate $x_1,x_2,\ldots,x_n$ from $\normal(0,1)$ via {\tt randn(1,n)}),
\item $g^{(2)}$, the PDF of $\normal(\mu=4.4,\sigma^2=1)$ (simulate $x_1,x_2,\ldots,x_n$ from $\normal(\mu,\sigma^2)$ by $x_i \gets \mu + \sigma z_i, z_i \sim \normal(0,1)$, i.e.~via {\tt 4.4 + 1.*randn(1,n);}),
\item $g^{(3)} = \exp(\pi - x)$, the PDF of a $\pi$-translated $\exponential(\lambda=1)$ RV $X$ with support $[\pi,\infty)$ (simulate $x_1,x_2,\ldots,x_n \sim X$ by $x_i \gets \pi + -\log(u_i), u_i \sim \uniform(0,1)$) and
\item $g^{(4)}$, the PDF of $|X|$, where $X \sim \normal(\pi,1)$.
\end{enumerate}

The Monte Carlo estimate based on $g^{(1)}=\phi \sim \normal(0,1)$:
\[
\widehat{\vartheta}^{(1)}_n
= \frac{1}{n} \sum_{i=1}^n \frac{\BB{1}_{(\pi,\infty)}(x_i) \phi(x_i)}{g^{(1)}(x_i)}
= \frac{1}{n} \sum_{i=1}^n \BB{1}_{(\pi,\infty)}(x_i), \qquad x_1,\ldots,x_n \overset{IID}{\sim} g^{(1)} = \phi \sim \normal(0,1) \ .
\]

The Monte Carlo estimate based on $g^{(2)} \sim \normal(4.4,1)$:
\[
\widehat{\vartheta}^{(1)}_n
= \frac{1}{n} \sum_{i=1}^n \frac{\BB{1}_{(\pi,\infty)}(x_i) \phi(x_i)}{g^{(2)}(x_i)}
= \frac{1}{n} \sum_{i=1}^n \BB{1}_{(\pi,\infty)}(x_i)\frac{\phi(x_i)}{g^{(2)}(x_i)}, \qquad x_1,\ldots,x_n \overset{IID}{\sim} g^{(2)} \sim \normal(4.4,1) \ .
\]

The Monte Carlo estimate based on $g^{(3)}$:
\[
\widehat{\vartheta}^{(1)}_n
= \frac{1}{n} \sum_{i=1}^n \frac{\BB{1}_{(\pi,\infty)}(x_i) \phi(x_i)}{g^{(3)}(x_i)}
= \frac{1}{n} \sum_{i=1}^n \BB{1}_{(\pi,\infty)}(x_i)\frac{\phi(x_i)}{g^{(3)}(x_i)}, \qquad x_1,\ldots,x_n \overset{IID}{\sim} g^{(3)} \ .
\]

The Monte Carlo estimate based on $g^{(4)}$:
\[
\widehat{\vartheta}^{(1)}_n
= \frac{1}{n} \sum_{i=1}^n \frac{\BB{1}_{(\pi,\infty)}(x_i) \phi(x_i)}{g^{(4)}(x_i)}
= \frac{1}{n} \sum_{i=1}^n \BB{1}_{(\pi,\infty)}(x_i)\frac{\phi(x_i)}{g^{(4)}(x_i)}, \qquad x_1,\ldots,x_n \overset{IID}{\sim} g^{(4)} \ .
\]

\end{labwork}

\section{Sequential Monte Carlo Methods}
\work

\subsection{Sequential Importance Sampling}

\subsection{Population MCMC}

\subsection{Genetic Monte Carlo Algorithms}

\section{Monte Carlo Optimisation}
\work