\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{hyperref}
\usepackage[pdftex]{graphicx}
%\usepackage{physymb}
\usepackage{wrapfig}
\usepackage{subcaption}
\title{EP 222: Assignment 1}

\author{Manish Goregaokar (120260006)}
\date{July 28, 2013}
\begin{document}
\maketitle
\section{Atwood's Machine}
\begin{figure}[h]
\centering
\includegraphics[scale=0.3]{AtD}
\caption{Diagram for Problem 1}

\label{fig:atd}
\end{figure}
 ~\\
\begin{wrapfigure}{r}{0.4\textwidth}
  \begin{center}
    \includegraphics[width=0.38\textwidth]{atFBDs}
  \end{center}

  \caption{\footnotesize Free body diagrams: (a) For leftmost weight. (b) for middle weight. (c) For rightmost weight. (d) For middle pulley.}
  \label{fig:atfbds}
\end{wrapfigure}

I have defined the variables in Figure~\ref{fig:atd}.

Now, assuming that the pulleys are massless, and writing Newton's laws of motion for the central pulley, with FBD in Figure~\ref{fig:atfbds} (d), we get:

$$2T_1-T_2=0\times a_2=0$$ $$ \therefore T_2=2T_1$$

Now, if we assume that each block $m_i$ moves $x_i$ (downward positive) in a given time, the net work done by the strings will be $-(T_1x_1 + T_2x_2 + T_1x_3)$. This must be 0, as strings as a whole cannot do work. Differentiating twice with respect to time,\\~\\~\\ $$0=T_1\ddot x_1 + T_2\ddot x_2 + T_1\ddot x_3$$ $$\therefore T_1a_1 +2T_1a_2+T_1a_3=0$$ $$\therefore a_1 +2a_2 + a_3 =0$$

Now, applying Newton's second law on the other three FBDs, we get:

\begin{align}
T_1-m_1g &= m_1a_1\notag\\
2T_1-m_2g &= m_2a_2\notag\\
T_1-m_3g &= m_3a_3\notag
\end{align}

Firstly, let us substitute $a_i'=a_i+g $

This reduces our equations to
\begin{align}
4g &= a_1' +2a_2' + a_3'\notag\\
T_1 &= m_1a_1'\notag\\
2T_1 &= m_2a_2'\notag\\
T_1 &= m_3a_3'\notag
\end{align}

Substituting values for $a_1',a_2',a_3'$ into the first equation, we get:

$$4g=\frac{T_1}{m_1} + 4\frac{T_1}{m_2} + \frac{T_1}{m_3}$$

$$\therefore T_1=\frac{4g}{\frac1{m_1}+\frac4{m_2}+\frac1{m_3}}=\frac{4gm_1m_2m_3}{m_1m_2+m_2m_3 + 4m_1m_3}$$

Substituting these values for $T_1$ back, we get:

\fbox{
 \addtolength{\linewidth}{-2\fboxsep}%
 \addtolength{\linewidth}{-2\fboxrule}%
\begin{minipage}{\linewidth}
\begin{align}
a_1 &=\frac{4gm_2m_3}{m_1m_2+m_2m_3 + 4m_1m_3} -g\notag\\
a_2 &=2\frac{4gm_1m_3}{m_1m_2+m_2m_3 + 4m_1m_3} -g\notag\\
a_3 &=\frac{4gm_1m_2}{m_1m_2+m_2m_3 + 4m_1m_3} -g\notag
\end{align}
\end{minipage}
}
\newpage
\section{Block system}
\begin{figure}[h]
\centering
\includegraphics[scale=0.6]{2D}
\caption{Diagram for Problem 2}

\label{fig:2d}
\end{figure}

 ~\\
\begin{wrapfigure}{r}{0.4\textwidth}
  \begin{center}
    \includegraphics[width=0.38\textwidth]{2F}
  \end{center}

  \caption{\footnotesize Free body diagrams: (a) For large block $M$ (b) For small block $m$}
  \label{fig:2f}
\end{wrapfigure}

Let $a_x,a_y$ be the components of acceleration of the small block, and let $A$ be the acceleration of the large block.

Let $x$ denote the extension of the vertical string  and $X$ denote the contraction of the horizontal string where marked.

Now, if the block $M$ moves forward by $X$, the horizontal motion will "contract" these portions of the string by $2X$ (net change in length). This must be compensated for with the  expansion of the vertical portion of the string $x$, so $x=2X$ (as the total length of the string does not change, it is just redistributed). Differentiating, $\ddot x=2\ddot X\implies a_y=2A$.

Now, as the system is in motion, $f=\mu N$. Analyzing the horizontal direction with Newton's laws in Figure \ref{fig:2f} (a), we get: $$2T-N=MA$$

Analyzing Figure \ref{fig:2f} (b), we get: $$N=mA$$ $$mg-f-T=ma_y\implies mg-\mu N -T =ma_y$$

Substituting $N=mA$ and $a_y=2A$, we get:

$$2T=(m+M)A$$

$$mg-\mu mA -T=2mA$$

Solving, we get $$A=\frac{2g}{5+2\mu+\frac Mm}$$

This implies  $$a_y=\frac{4g}{5+2\mu+\frac Mm}$$

Substituting $\frac Mm=10, \mu=0.5$, we get:

$$A=a_x=\frac14 g, a_y=\frac12 g$$

Thus vector acceleration of $M$ is \fbox{$\frac14g \hat i $}, acceleration of $m$ is \fbox{$\frac14g(\hat i -2\hat j) $}

As initual downward velocity of $m$ is 0, time taken to cover a distance of $d=2.45 ~\mathrm m$ is $\sqrt{\frac{2d}{a_y}}$ (from $s=ut+\frac12 at^2$), which is $\sqrt{\frac{4.9}{9.8\times \frac{1}{4}}} = \sqrt{2}~\mathrm s$

Thus the block takes \fbox{$1.414$ seconds} to reach the bottom.
\newpage
\section{Rod on belt}
\begin{figure}[h]
\centering
\includegraphics[scale=0.6]{3D}
\caption{Diagram for Problem 3}

\label{fig:3d}
\end{figure}
 ~\\
\begin{wrapfigure}{r}{0.3\textwidth}
  \begin{center}
    \includegraphics[width=0.38\textwidth]{3D2}
  \end{center}

  \caption{\footnotesize Close up of small element of rope}
  \label{fig:3D2}
\end{wrapfigure}

Let us analyze a small portion of the belt, as tension on that belt is changing. We assume that friction acts to the right.

Now, the horizontal component of tension is $(T+dT-T)\cos{\frac12 d\theta}=dT$. The vertical component is $(T+T+dT)\sin{\frac12 d\theta}=Td\theta$.

Balancing forces, we get $N=Td\theta$, and $f=dT$. Now, $f\leq \mu N \implies dT\leq \mu Td\theta$.

Integrating, $\int\limits_{T_0}^T \frac{dT}{T}=\int\limits_0^\theta \mu d\theta$. From here, we get that

$T(\theta)\leq T_0e^{\mu\theta}\implies T_{wall}\leq T_0e^{\mu\theta_0}$

If we assume that friction acts to the left, we get $T_{wall}\geq T_0e^{-\mu\theta_0}$

Now, the macroscopic variable $T_{wall}$ depends on the microscopic distribution of elongation in the string, which in turn depends on the process used to create this system. This is an unknown, which gives us a range (instead of an absolute answer) for $T_{wall}$ However, Newton's laws allow all values of $T_{wall}$ between $T_0e^{-\mu\theta_0}$ and $T_0e^{\mu\theta_0}$. Thus, we can claim that a state can be constructed where \fbox{$T_{wall}=T_0e^{\mu\theta_0}$}, and this is the maximum tension possible.

\section{Leaning ladder}
\begin{figure}[h]
\centering
\includegraphics[scale=0.6]{4D}
\caption{Diagram for Problem 4}

\label{fig:4d}
\end{figure}

Firstly, let us assume that the ladder has a length $2L$ and mass $m$.

Now, note that the smallest angle in equilibrium will be  when the static friction attains its maximum value ($\mu N$). So, $f=\mu N$.

Now, first writing translational equilibrium equations:

$$mg=N$$
$$\mu N=f = N_w$$

The net torque on the ladder is $\tau=N_wL\sin\theta+fL\sin\theta - NL\cos\theta$. The ladder is in rotational equilibrium, so $\tau=0$. So, $$N_w L\sin\theta +fL\sin\theta =NL\cos\theta$$

Making substitutions from previously derived equations, we get $$2\mu NL\sin\theta=\cos\theta$$

or $$\boxed{\theta=\cot^{-1}2\mu}$$

\section{Cylinders}

\begin{figure}[h]
\centering
\includegraphics[scale=0.4]{5D}
\caption{Diagram for Problem 5}

\label{fig:5d}
\end{figure}

Here, we assume that there is friction between the small cylinders and the large one, as otherwise we get that the system does not stay in rotational equilibrium. This is easily verified, as the only forces providing torque to the smaller cylinders are the friction from the ground and the friction between them and the larger cylinder.\\  As the grund friction is nonzero, there must be friction between the smaller cylinders and the larger one otherwise the system does not stay at rest. Note that the friction between the cylinders must be zero due to the symmetry of the system.\\

The free body diagrams have been labelled taking into account the symmetry of the system. Figure \ref{fig:5d} indicates the contact forces at various points (direction is not shown as it  depends on which body we are considering, refer Figure \ref{fig:5f} for details on the direction of the forces).
\begin{wrapfigure}{l}{0.5\textwidth}
  \begin{center}
    \includegraphics[width=0.38\textwidth]{5F}
  \end{center}

  \caption{\footnotesize Free body diagrams: (a) For small cylinders $(m,R)$ (b) For large cylinder $(4m,2R)$}
  \label{fig:5f}
\end{wrapfigure}
Now, we first write the equation for torational equilibrium for the small cylinders, $\tau=rf_g-rf=0 \implies f_g=f$. We can thus conveniently replace all $f_g$ with $f$.

Writing the equations for translational equilibrium of the small cylinders, we get

\newcommand{\ccc}{\cos\theta}
\newcommand{\sss}{\sin\theta}

$$mg + N\ccc +f\sss = N_g$$

$$N\sss =f+f\ccc$$

Writing equilibrium equations in the vertical direction for the large cylinder:

$$2N\ccc+2f\sss=4mg\implies N\ccc +f\sss=2mg$$

Note that $\theta$ is known, with $\sss=\frac13, \ccc=\frac{\sqrt8}3$

$$\therefore N=f(1+\sqrt8)$$
$$N\sqrt 8 +f=6mg$$
$$\therefore f(1+\sqrt 8) + f =6mg\implies f=\frac{6mg}{2+\sqrt 8}$$

$$\therefore N=6mg\frac{1+\sqrt8}{2+\sqrt8}$$

As $$Ng=mg + N\ccc +f\sss $$
\begin{align}
N_g &= mg + 6mg\ccc\frac{1+\sqrt8}{2+\sqrt8} + \sss\frac{6mg}{2+\sqrt 8}\notag\\
 &= mg \left(1 + \frac{6}{3(2+\sqrt 8)}\left(\sqrt8(1+\sqrt8) + (2+\sqrt8)\right) \right)\notag\\
 &= mg\left(1+\frac{1}{1+\sqrt 2}\left(10+4\sqrt2\right)\right )\notag\\
 &=mg\frac{11+5\sqrt 2}{1+\sqrt 2}\notag
\end{align}

$$\therefore \frac{N_g}{f_g}=\frac{\left(mg\frac{11+5\sqrt 2}{1+\sqrt 2}\right)}{\left(\frac{3mg}{1+\sqrt 2}\right)}=\frac{11+5\sqrt 2}{3}$$

Now, $\mu N_g\geq f_g$. In this case, the minimum $\mu$ is during equality, so \fbox{$\mu=\frac{N_g}{f_g}= \frac{11+5\sqrt 2}{3}$}

\section{Mass on wedge}
\begin{figure}[h]
\centering
\includegraphics[scale=0.4]{6D}
\caption{Diagram for Problem 6}

\label{fig:6d}
\end{figure}

Here, I am assuming that the values of final velocity are required just before the mass $m$ reaches the table (as to calculate this value at the exact time will require knowledge of the nature of the collision). For the purposes of this problem I shall take the symbols $v_w,v_x,v_y$ to mean the velocities at the final stage. Initial velocities are zero.

Now, we can conserve momentum in the horizontal direction here and overall energy as well. Conserving momentum in the horizontal direction, we get $mv_x=Mv_w \implies v_x=\frac Mm v_w$.

Conserving energy, $\frac12(Mv_w^2+mv_x^2+mv_y^2)=mgh$

Finally, in the wedge frame, the mass must move along the wedge surface. Thus, $\frac{v_y}{v_x+v_w}=\tan\theta \implies v_y=\tan\theta (v_x+v_w)$

Substituting the values for $v_x$ and $v_y$ in the energy conservation equation, we get

$$2mgh= Mv_w^2+ m\left(\frac{M}m v_w\right)^2+m\tan^2\theta (v_x+v_w)^2=Mv_w^2+ m\left(\frac{M}m v_w\right)^2+m\tan^2\theta v_w^2 \left(1+\frac{M}{m}\right)^2$$

$$\therefore v_w^2 = \frac{2mgh}{M+\frac{M^2}{m} + m\tan^2\theta \left(1+\frac{M}m\right)^2}$$

$$\therefore \boxed{v_w=\sqrt{\frac{2mgh}{M+\frac{M^2}{m} + m\tan^2\theta \left(1+\frac{M}m\right)^2}}}$$

\end{document}

Course material/EP 222 - Classical Mechanics/Assignment 1/assign1.tex


\documentclass[12pt,notitlepage]{article}

\usepackage{hyperref}

\title{Project Euler: Problem 2
\\\small{\url{https://projecteuler.net/problem=1}}\\
\small{\url{https://github.com/oltdaniel/euler}}}

\begin{document}
  \maketitle

  \begin{center}
    \textit{"Each new term in the Fibonocci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:\\1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...\\By considering the terms in the Fibonocci sequence whose values do not exceed four million, find the sum of the even-valued terms."}
  \end{center}

  The general idea to solve this, is starting an endless loop, that will break if the current fibonacii number exceeds
  $4,000,000$. During each iteration the numbers before will be added and stored, as well as moving the last Fibonocci
  number to the memory for the next iteration. THe current Fibonoccie will be then tested wether they are even or not
  and added to the sum if they are.\par

  As we already know, that only even fibonacii numbers will be added to the sum, this routine can be optimized by using
  some existing formulas.

  \subsection{Basic Idea}
  The basic idea is to start an endless loop, that calculates the next Fibonocci number, by adding the two previous
  ones. Followed by that, the new number, and the last one need to be stored for the next operation. After the current
  Fibonocci number has been proven to be even, it can be added to the final sum.\par
  \textbf{Complexity: $O(\log (\sqrt{5} * n) \div \log (\frac{1 + \sqrt{5}}{2}))$}

  \subsection{Memory Optimization}
  As the above solution requires the use of many memory operations, by moving the numbers around, this step
  is something that can be optimized with the \textit{Binet's Formula}. This allows us to calculate the $n$-th
  Fibonocci number without moving any values in the memory nor remembering any values calculated before. The formula
  is defined as follows:

  $$F_{n} = \frac{\varphi^{n} - \psi^{n}}{\sqrt{5}} = \frac{
    \frac{
      (1 + \sqrt{5})
    }
    {2}^{n} -
    \frac{
      (1 - \sqrt{5})
    }
    {2}^{n}
  }{
    \sqrt{5}
  }$$\par

  \textbf{Complexity: $O(\log (\sqrt{5} * n) \div \log (\frac{1 + \sqrt{5}}{2}))$}

  \subsection{Reduce Calculation}
  Based on the optimizations made above, we can now simplify the sequence of numers we will add in a loop, into
  a geometric series. This will allow us to calculate the sum with the following formula:

  $$S(n) = (F_{n + 2} - 1) \div 2$$

  However, as we only want even numbers we need to extend this. We know, that a Fibonocci number is only even, if the
  index of it is a multiple of $3$. We also know, that $n$ is the number of Fibonocci numbers under the set limit $4,000,000$.
  In order to calculate $n$ for $S(n \div 3)$ to find $s$, we can use the following formula based on \textit{Binet's Formula}:

  $$n = \log(\sqrt{5} * n) \div \log(\frac{1 + \sqrt{5}}{2})$$

  Followed by that, we can say, that the sum of all even Fibonocci numbers under $4,000,000$ is $s = S(n / 3)$.

  \textbf{Complexity: $O(1)$}
\end{document}


\vsssub
\subsubsection{~$S_{db}$: Battjes and  Janssen 1978} \label{sec:DB1}
\vsssub

\opthead{DB1 / MLIM}{Pre-WAM}{J. H. G. M. Alves}

\noindent
The implementation in \ws\ of depth-induced breaking algorithms is intended to
extend the applicability of the model to within shallow water environments,
where wave breaking, among other depth-induced transformation processes,
becomes important.

For this reason the approach of \citet[][henceforth denoted as
BJ78]{pro:BJ78}, which is based on the assumption that all waves in a random
field exceeding a threshold height, defined as a function of bottom topography
parameters, will break. For a random wave field, the fraction of waves
satisfying this criterion is determined by a statistical description of
surf-zone wave heights (i.e., a Rayleigh-type distribution, truncated at a
depth-dependent wave-height maximum).

The bulk rate $\delta$ of spectral energy density dissipation of the fraction
of breaking waves, as proposed by BJ78, is estimated using an analogy with
dissipation in turbulent bores as

%-------------------------------%
% Battjes Janssen surf breaking %
%-------------------------------%
% eq:BJ78_base

\begin{equation}
\delta = 0.25 \: Q_b \: f_m \: H_{\max}^2  , \label{eq:BJ78_base}
\end{equation}

\noindent
where $Q_b$ is the fraction of breaking waves in the random field, $f_m$ is
the mean frequency and $H_{\max}$ is the maximum individual height a component
in the random wave field can reach without breaking (conversely, above which
all waves would break). In BJ78 the maximum wave height $H_{\max}$ is defined
using a Miche-type criterion \citep{art:Miche44},

% eq:BJ78_Miche

\begin{equation}
\bar{k} H_{\max} = \gamma_M \tanh ( \bar{k} d )
 , \label{eq:BJ78_Miche}
\end{equation}

\noindent
where $\gamma_M$ is a constant factor. This approach also removes energy in
deep-water waves exceeding a limiting steepness. This can potentially result
in double counting of dissipation in deep-water waves. Alternatively,
$H_{\max}$ can be defined using a McCowan-type criterion, which consists of
simple constant ratio

% eq:BJ78_McC

\begin{equation}
H_{\max} = \gamma \: d  , \label{eq:BJ78_McC}
\end{equation}

\noindent
where $d$ is the local water depth and $\gamma$ is a constant derived from
field and laboratory observation of breaking waves. This approach will
exclusively represent depth-induced breaking.  Although more general breaking
criteria for $H_{\max}$ as a simple function of local depth exist
\citep[e.g.,][]{art:TG83}, it should be noted that the coefficient $\gamma$
refers to the maximum height of an individual breaking wave within the random
field. \cite{art:M1894} calculated the limiting wave-height-to-depth ratio for
a solitary wave propagating on a flat bottom to be 0.78, which is still used
presently as a conservative criteria in engineering applications. The average
value found by \cite{pro:BJ78} was $\gamma = 0.73$. More recent analyses of
waves propagating over reefs by \cite{art:Nel94, art:Nel97} suggest a ratio of
0.55.

The fraction of breaking waves $Q_b$ is determined in terms of a Rayleigh-type
distribution truncated at $H_{\max}$ (i.e., all broken waves have a height
equal to $H_{max}$), which results in the following expression:

% eq:BJ78_Qb

\begin{equation}
\frac{1 - Q_b}{-\ln Q_b} = \left ( \frac{H_{rms}}{H_{\max}} \right ) ^{2}
 , \label{eq:BJ78_Qb}
\end{equation}

\noindent
where $H_{rms}$ is the root-mean-square wave height. In the current
implementation, the implicit equation (\ref{eq:BJ78_Qb}) is solved for $Q_b$
iteratively. With the assumption that the total spectral energy dissipation
$\delta$ is distributed over the entire spectrum so that it does not change
the spectral shape \citep{art:EB96} the following depth-induced breaking
dissipation source function is obtained

% eq:BJ78

\begin{equation}
\cS_{db} (k,\theta) = - \alpha \frac{\delta}{E} F(k,\theta)
       = - 0.25 \: \alpha \: Q_b \: f_m \frac{H_{\max}^2}{E} F(k,\theta)
 , \label{eq:BJ78}
\end{equation}

\noindent
where $E$ is the total spectral energy, and $\alpha = 1.0$ is a tunable
parameter. The user can select between Eqs.~(\ref{eq:BJ78_Miche}) and
(\ref{eq:BJ78_McC}), and adjust $\gamma$ and $\alpha$. Defaults are
Eq.~(\ref{eq:BJ78_McC}), $\gamma = 0.73$ and $\alpha = 1.0$.


%% SECTION HEADER /////////////////////////////////////////////////////////////////////////////////////
\section{Temperature Effect on the GW Propagation}
\label{sec:temp}

%% SECTION CONTENT ////////////////////////////////////////////////////////////////////////////////////

%% SUBSECTION HEADER //////////////////////////////////////////////////////////////////////////////////
In order to carry out the temperature dependent SE simulation and semi-analytical analysis of Lamb wave propagation in the \acp{hsc}, the elastic modulus of \acp{hsc} layers are calculated as per the methodology described in \cite{chamis1983simplified,salamone2009guided}. The calculation is done for a range of temperatures (+50\(^{\circ}\)C to -50\(^{\circ}\)C) generally occurred in practical operating scenarios. The obtained temperature-dependent elastic properties (E11 = E22, E33, G12, G13 = G23) for composite laminate and adhesive (E, G) are presented in Figure 7 and Figure 8, respectively.

In this model \cite{salamone2009guided}, reduction of Young’s modulus of the resin \(E_m\) with variation in temperature is assumed as:
\begin{eqnarray}
	E_m(T)=F_m E_{rm},
	\label{eq:factor_temp}
\end{eqnarray}
where \(E_{rm}\) is the Young’s modulus of resin at the reference temperature and \(F_m\) is the temperature degradation factor as proposed in \cite{chamis1983simplified}:
\begin{eqnarray}
F_m=\sqrt{\frac{T_{g0}-T}{T_{g0}-T_r}},
\label{eq:em_temp}
\end{eqnarray}
where \(T_{g0}\) is the glass transition temperature and \(T_r\) is the reference temperature. In the study, the value of \(T_{g0}=215^{\circ}\)C and \(T_r=20^{\circ}\)C is selected from Table 2 in \cite{chamis1983simplified}.


% Copyright and Usage Information
% ===============================

% This file is provided solely for the personal and private use of students
% taking CSC110 at the University of Toronto St. George campus. All forms of
% distribution of this code, whether as given or with any changes, are
% expressly prohibited. For more information on copyright for CSC110 materials,
% please consult our Course Syllabus.

% This file is Copyright (c) 2021 Mario Badr and Tom Fairgrieve.
\documentclass{article}

\setlength{\parindent}{0pt}
\setlength{\parskip}{5pt}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{amsfonts}

\usepackage[margin=1in]{geometry}

\title{CSC110 Fall 2021: Term Test 2\\
       Question 2 (Analyzing Algorithm Running Time)}
\author{TODO: INSERT YOUR NAME HERE}
\date{Wednesday December 8, 2021}

% Some useful LaTeX commands. You are free to use these or not, and also add your own.
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\cO}{\mathcal{O}}
\newcommand{\floor}[1]{\left\lfloor #1 \right\rfloor}
\newcommand{\ceil}[1]{\left\lceil #1 \right\rceil}
\newcommand{\C}{\texttt}

\begin{document}
\maketitle

\subsection*{Question 2, Part 1}

\noindent
We define the function $g: \N \to \R^{\geq 0}$ as $g(n) = 7n(n-1)^2$.
Consider the following statement:

\[
g(n) \in \cO(n^4)
\]

\begin{enumerate}

\item[(a)]
Rewrite the statement $g(n) \in \cO(n^4)$ by expanding the definition of Big-O.

\bigskip

\textbf{Solution}:

$\exists c, n_0 \in \R^+ \text{ s.t. } \forall n \in \N, n \ge n_0 \Rightarrow 7n(n-1)^2 \le c \cdot n^4$

\item[(b)]
Write the \emph{negation} of the statement from (a), using negation rules to simplify the statement as much as possible.

\bigskip

\textbf{Solution}:

$\forall c, n_0 \in \R^+, \exists n \in \N \text{ s.t. } n \ge n_0 \land 7n(n-1)^2 > c \cdot n^4$

\item[(c)]
Which of statements (a) and (b) is true? Provide a complete proof that justifies your choice.

In your proof, you may not use any properties or theorems about Big-O/Omega/Theta.  Work from the expanded statement from (a) or (b).

\bigskip

\textbf{Solution}:

I think statement (a) is true.

\begin{proof}
Want to show: $\exists c, n_0 \in \R^+ \text{ s.t. } \forall n \in \N, n \ge n_0 \Rightarrow 7n(n-1)^2 \le c \cdot n^4$ \\
Prove using Induction. \\
Take $c = 7, n_0 = 1$ \\
Let $n$ be an arbitrary natural number such that $n \ge (n_0 = 1)$ \\
What we want to prove becomes: $\forall n \in \N, n \ge 1 \Rightarrow 7n(n-1)^2 \le 7n^4$ \\
\\
Since $n \ge 1$, \\
Multiply both sides by $n^2$, we get $n^3 \ge n^2$ \\
From this, we also know that $(n - 1)^2 \le n^3$ \\
Also, since $n \ge 1$, \\
Multiply the inequality by $-2$, we get $-2n \le -2$ \\
Adding $1$ to both sides, we get $-2n + 1 \le -1$ \\
Putting the two inequalities together, we have $n^2 - 2n + 1 \le n^3 - 1 \le n^3$ \\
Factoring the polynomial on the left, we have $(n - 1)^2 \le n^3$ \\
Multiply both sides by $7n$, we get $7n(n - 1)^2 \le 7n^4$\\
Which is what we want to prove.

\end{proof}

\end{enumerate}

\subsection*{Question 2, Part 2}

\noindent
Consider the function below.

\begin{verbatim}
def f(nums: list[int]) -> list[int]:           # Line 1
    n = len(nums)                              # Line 2
    i = 1                                      # Line 3
    new_list = []                              # Line 4
    while i < n:                               # Line 5
        if nums[i] % 2 == 0:                   # Line 6
            list.append(new_list, i)           # Line 7
        else:                                  # Line 8
            new_list = [i * j for j in nums]   # Line 9
        i = i * 3                              # Line 10
    return new_list                            # Line 11
\end{verbatim}

\begin{enumerate}

\item[(a)]
Perform an \emph{upper bound analysis} on the worst-case running time of \texttt{f}.
The Big-O expression that you conclude should be \emph{tight}, meaning that the worst-case running time should be Theta of this expression, but you are not required to show that here.

\textbf{To simplify your analysis}, you may omit all floors and ceilings in your calculations (if applicable).
Use ``at most'' or $\leq$ to be explicit about where a step count expression is an upper bound.

\textbf{Solution}:

Let $n$ be the length of the input list \C{nums}

There is one loop in the function which loops through \C{nums} with $i$ increasing exponentially, which will run $\ceil{log_3(n)}$ times. Inside the loop, if then number is even, it takes $\cO(1)$ to append the item at the end of \C{new\_list}. If the number is odd, it sets \C{new\_list} to a list comprehension which iterates through all number in \C{nums}, performing an $\cO(1)$ multiplication every iteration, which takes exactly $n$ steps, which is a larger running time than if the number is even. Therefore, the inside of the loop will take at most $n$ steps, if all numbers \C{nums[i]} iterated are odd.

Since there are only constant-time operations outside the loop, the worst-case running time would be $\ceil{log_3(n)}$ iterations multiplied by at most $n$ steps per iteration, which is $n\ceil{log_3(n)}$ steps.

Since $n\ceil{log_3(n)} \in \cO(n\ceil{log_3(n)})$, we can conclude that $WC_{f}(n) \in \cO(n\ceil{log_3(n)})$

\item[(b)]
Perform a \emph{lower bound analysis} on the worst-case running time of \texttt{f}.
The Omega expression you find should match your Big-O expression from part (a).

\textbf{Hint}: you don't need to try to find an ``exact maximum running-time'' input. \emph{Any} input family whose running time is Omega of (``at least'') the bound you found in part (a) will yield a correct analysis for this part.

\textbf{Solution}:

Let $n$ be the length of the input list \C{nums}, let \C{nums} be the list of length $n$ which every number is 1.

In this case, the if statement inside the loop always runs line 9 that takes $n$ steps, and then the $i = i * 3$ statement, which is 1 step, which is a total of $n + 1$ steps. The loop still iterates $\ceil{log_3(n)}$ times. Since there are only constant-time operations outside the loop, the total number of steps for this input is $(n + 1)\ceil{log_3(n)} + c$ which $c \in \N$ is a constant, which is $WC_{f}(n) \in \Omega(n\ceil{log_3(n)})$

\end{enumerate}

\begin{center}
    \textbf{SUBMIT THIS FILE AND THE GENERATED PDF q2.pdf FOR GRADING}
\end{center}
\end{document}


\chapter{First Examples}
We will now look at a simple example to demonstrate the basics of how Bayesian
statistics works. We start with some probabilities at the beginning of the problem (these are
called {\it prior probabilities}), and how exactly these get updated
when we get more information (these updated probabilities are called
{\it posterior probabilities}). To help make things more clear, we will
use a table that we will call a {\it Bayes' Box} to help us calculate the
posterior probabilities easily.

Suppose there are two balls in a bag. We know in advance
that at least one of them is black, but we're not sure whether they're both
black, or whether one is black and one is white. These are the only two
possibilities we will consider.
To keep things concise, we
can label our two competing hypotheses. We could call them whatever we want,
but I will call them \bb~and \bw. So, at the beginning of the problem, we know
that {\it one and only one} of the following statements/hypotheses is true:\\
\begin{framed}
\bb: Both balls are black\\
\bw: One ball is black and the other is white.
\end{framed}
Suppose an experiment is performed to help us determine
which of these two hypotheses is
true. The experimenter reaches into the bag, pulls out one of the balls, and
observes its colour. The result of this experiment is (drumroll please!):
\begin{framed}
$D$: The ball that was removed from the bag was black.
\end{framed}
We will now do a Bayesian analysis of this result.

\section{The Bayes' Box}
A Bayesian analysis starts by choosing some values for the prior probabilities.
We have our two competing hypotheses \bb~and \bw, and we need to choose some
probability values to describe how sure we are that each of these is true.
Since we are talking about two hypotheses, there will be two prior probabilities,
one for \bb~and one for \bw.
For simplicity, we will assume that we don't have much of an idea which is true,
and so we will use the following prior probabilities:
\begin{eqnarray}
P(\bb) &=& 0.5\\
P(\bw) &=& 0.5.
\end{eqnarray}
Pay attention to the notation. The upper case $P$ stands for probability, and if we just
write $P(\texttt{whatever})$, that means we are talking about the
prior probability of {\tt whatever}. We will see the notation for the posterior probability
shortly. Note also that since the two hypotheses are mutually exclusive
(they can't both be true) and exhaustive (one of these is true, it can't be
some undefined third option).
We will almost always consider mutually exclusive and exhaustive hypotheses in
this course\footnote{If this does not appear to be true in a particular problem,
it is usually possible to redefine the various hypotheses into a set that of
hypotheses that {\it are}
mutually exclusive and exhaustive.}.

The choice of 0.5 for the two prior probabilities describes the fact that,
before we did the experiment, we were very uncertain about which of the two
hypotheses was true.
I will now present a {\it Bayes' Box}, which lists all the hypotheses (in this
case
two) that might be true, and the prior probabilities. There are some extra
columns which we haven't discussed yet, and will be needed in order to
figure out the posterior probabilities in the final column.
\begin{table}[!ht]
\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\hline
{\bf Hypotheses} & {\tt prior} & {\tt likelihood} &
{\tt prior $\times$ likelihood} & {\tt posterior}\\
\hline
\bb & 0.5 &   &  & \\
\bw & 0.5 &   &  & \\
\hline
Totals: & 1 & & & \\
\hline
\end{tabular}
\end{center}
\end{table}
The first column of a Bayes' Box is just the list of hypotheses we are
considering. In this case there are just two. If you need to construct a Bayes' box for a new problem, just think
about what the possible answers to the problem are, and list them in the first
column. The second column lists the prior probabilities for each of the
hypotheses.
Above, before we did the experiment, we decided to say that there was a 50\%
probability that \bb~is true and a 50\% probability that \bw~is true, hence the 0.5 values in this column.
The prior column should always sum to 1. Remember, the prior probabilities
only describe our initial uncertainty, before taking the data into account. Hopefully the data will help by changing these probabilities to
something a bit more decisive.

\subsection{Likelihood}
The third column is called {\it likelihood}, and this is a really important
column where the action happens. The likelihood is a quantity
that will be used for calculating the posterior
probabilities.
In colloquial language, likelihood is synonymous with
probability. It means the same thing. However, in statistics, likelihood is a
very
specific kind of probability. To fill in the third column of the Bayes' Box,
we need to calculate two likelihoods, so you can tell from this that the
likelihood is something different for each hypothesis. But what is it
exactly?
\begin{framed}
{\bf The likelihood for a hypothesis is the probability that you would have
observed the data, if that hypothesis were true. The values can be found by
going through each hypothesis in turn, imagining it is true, and asking,
``What is the probability of getting the data that I observed?''.}
\end{framed}

Here is the Bayes' Box with the likelihood column filled in. I will explain
how these numbers were calculated in a bit more detail in the next subsection.
\begin{table}[!ht]
\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\hline
{\bf Hypotheses} & {\tt prior} & {\tt likelihood} &
{\tt h = prior $\times$ likelihood} & {\tt posterior}\\
\hline
{\tt BB} & 0.5 & 1 &  & \\
{\tt BW} & 0.5 & 0.5 &  & \\
\hline
Totals: & 1 & & & \\
\hline
\end{tabular}
\end{center}
\end{table}
If you have taken STATS 210 and used the maximum likelihood method, where you
find the value of a parameter that maximises the likelihood function, that is
the same as the likelihood we use in this course! So you have a head start
in understanding this concept.

\subsection{Finding the Likelihood Values}
We will first calculate the value of the likelihood for the \bb~hypothesis.
Remember, the data we are analysing here is that we chose one of the balls in
the bag ``at random'', and it was black. The likelihood for the \bb~hypothesis is
therefore the probability that we would get a black ball if \bb~is true.

Imagine that \bb~is true. That means both balls are black. What is the probability that
the experiment would result in a black ball? That's easy -- it's 100\%! So we
put the number 1 in the Bayes Box as the likelihood for the \bb~hypothesis.

Now imagine instead that \bw~is true. That would mean one ball is black and the
other is white. If this were the case and we did the experiment, what would be
the probability of getting the black ball in the experiment? Since one of the
two balls is black, the chance of choosing this one is 50\%. Therefore, the
likelihood for the \bw~hypothesis is 0.5, and that's why I put 0.5 in the Bayes'
Box for the likelihood for \bw.

In general, the likelihood is the {\it probability of the data that you actually
got, assuming a particular hypothesis is true}. In this example it was
fairly easy to get the likelihoods directly by asking ``if this hypothesis
is true, what is the probability of getting the black ball when we do the
experiment?''. Sometimes this is not so easy, and it can be helpful to think about
ALL possible experimental outcomes/data you might have seen -- even though
ultimately, you just need to select the one that actually occurred.
Table~\ref{tab:all_data} shows an example of this process.

\begin{table}[!ht]
\begin{center}
\begin{tabular}{|c|c|c|}
\hline
{\bf Hypotheses} & {\bf Possible Data} & {\bf Probability} \\
\hline
{\tt BB} & {\color{blue} Black Ball} & {\color{blue} 1}\\
         & White Ball & 0 \\
\hline
{\tt BW} & {\color{blue} Black Ball} & {\color{blue} 0.5} \\
         & White Ball & 0.5 \\
\hline
\end{tabular}
\caption{\it This table demonstrates a method for calculating the likelihood
values, by considering not just the data that actually occurred, but all
data that might have occurred. Ultimately, it is only the probability of the
data which actually occurred that matters, so this is highlighted in blue.
\label{tab:all_data}}
\end{center}
\end{table}
The fact that only the blue probabilities in Table~\ref{tab:all_data} enter the
Bayes' Box calculation is related to the {\it likelihood principle}, which we
will
discuss in lectures. Note also that in Table~\ref{tab:all_data}, the
probabilities for the different possible data sets add to 1 within each
hypothesis, but the sum of the blue ``selected'' likelihood values is not 1
(it is, in fact, meaningless).

When we come to parameter estimation in later chapters, we will usually set up
our problems in this way, by considering what data sets are possible, and
assigning probabilities to them.

\subsection{The Mechanical Part}
The third column of the Bayes' Box is the product of the prior probabilities
and the likelihoods, calculated by simple multiplication. The result will be
called ``prior times likelihood'', but occasionally we will use the letter $h$
for these quantities. This is the {\it unnormalised} posterior. It does not sum
to 1 as the posterior probabilities should, but it is at least proportional to
the actual posterior probabilities.

To find the posterior probabilities, we take the {\tt prior $\times$ likelihood}
column and divide it by its sum, producing numbers that do sum to 1. This
gives us the final
posterior probabilities, which were the goal all along. The completed Bayes'
Box is shown below:

\begin{table}[!ht]
\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\hline
{\bf Hypotheses} & {\tt prior} & {\tt likelihood} &
{\tt h = prior $\times$ likelihood} & {\tt posterior}\\
\hline
{\tt BB} & 0.5 & 1 & 0.5 & 0.667\\
{\tt BW} & 0.5 & 0.5 & 0.25  & 0.333\\
\hline
Totals: & 1 & & 0.75 & 1\\
\hline
\end{tabular}
\end{center}
\end{table}
We can see that the posterior probabilities are not the same as the prior
probabilities, because we have more information now! The experimental result
made \bb~a little bit more plausible than it was before. Its probability has
increased from 1/2 to 2/3.

\subsection{Interpretation}
The posterior probabilities of the hypotheses are proportional to the prior probabilies
and the likelihoods. A high prior probability will help a hypothesis have a high
posterior probability. A high likelihood value also helps.
To understand what this means about reasoning, consider
the meanings of the prior and the likelihood. There are two things that can
contribute to a hypothesis being plausible:
\begin{itemize}
\item If the prior probability is high. That is, the hypothesis was {\it already}
plausible, before we got the data.
\item If the hypothesis {\it predicted the data} well. That is, the data was
what we would have expected to occur if the hypothesis had been true.
\end{itemize}
I hope you agree that this is all very sensible.

In class we will also study
variations on this problem, considering different assumptions about the prior
probabilities and how they affect the results, and also considering what happens
when we get more and/or different data.

\section{Bayes' Rule}
Bayes' rule is an equation from probability theory, shown in
Figure~\ref{fig:bayes_neon}. The various terms in Bayes' rule are all
probabilities, but notice that there are conditional probabilities in there.
For example, the left hand side of the equation is $P(A|B)$ and that means
the probability of $A$ {\bf given} $B$. That is, it's the probability of $A$
after taking into account the information $B$. In other words,
$P(A|B)$ is a posterior probability, and Bayes' rule tells us how to calculate
it from other probabilities.
\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.4]{Figures/bayes_neon.jpg}
\caption{\it A blue neon sign displaying Bayes' rule.
You can use it to calculate the probability of $A$ {\it given} $B$,
if you know the values of some other probabilities on the right hand side.
Image credit: Matt Buck. Obtained from Wikimedia Commons.
\label{fig:bayes_neon}}
\end{center}
\end{figure}
Bayes' rule is true for {\it any} statements $A$ and $B$. If you took the
equation in Figure~\ref{fig:bayes_neon} and replaced $A$ with
``K\={a}k\={a}p\={o} will survive beyond 2050'' and $B$ with
``I had coffee this morning'', the
resulting equation would still be true\footnote{It would still be true, but
it would not very interesting,
because
whether or not I had coffee doesn't tell you much about the survival prospects
of endangered New Zealand parrots.}.

It is helpful to relabel $A$ and $B$ in Bayes' rule to give a more clear
interpretation of how the equation is to be used. In this version of Bayes'
rule (which is one you should commit to memory), $A$ has been replaced by $H$,
and $B$ has been replaced by $D$. The reason for these letters is that you should
interpret $H$ as {\it hypothesis} and $D$ as {\it data}. Then you can interpret
Bayes' rule as telling you the probability of a hypothesis given some data, in
other words, a posterior probability.
\begin{eqnarray}
P(H|D) = \frac{P(H)P(D|H)}{P(D)}
\end{eqnarray}
In Bayesian statistics, most of the terms in Bayes' rule have special names.
Some of them even have more than one name, with different scientific
communities preferring different terminology. Here is a list of the
various terms and the names we will use for them:
\begin{itemize}
\item $P(H|D)$ is the {\bf posterior probability}. It describes how certain
or confident we are that
hypothesis $H$ is true, given that we have observed data $D$. Calculating
posterior probabilities is the main goal of Bayesian statistics!
\item $P(H)$ is the {\bf prior probability}, which describes how sure we were
that $H$ was true, before we observed the data $D$.
\item $P(D|H)$ is the {\bf likelihood}. If you were to assume that $H$ is true,
this is the probability that you would have observed data $D$.
\item $P(D)$ is the {\bf marginal likelihood}. This is the probability that you
would have observed data $D$, {\it whether $H$ is true or not}.
\end{itemize}
Since you may encounter Bayesian methods outside of STATS 331, I have included
an Appendix called ``Rosetta Stone'' that lists some common alternative
terminology.

In the above example, we did some calculations to work out the numbers in the
Bayes' Box, particularly the posterior probabilities, which are the ultimate
goal of the calculation. {\it What we were actually doing in these calculations
was applying Bayes' rule}. We actually applied Bayes' rule twice, once to
compute $P(\bb | D)$ and a second time to calculate $P(\bw | D)$.

\begin{framed}
{\bf When you use a Bayes' Box to calculate posterior probabilities,
you are really just applying Bayes' rule a lot of times:
once for each hypothesis listed in the first column.}
\end{framed}

\section{Phone Example}
This example is based on Question 1 from the 2012 final exam. I got the
idea for this question from an example in David MacKay's wonderful book
``Information Theory, Inference and Learning Algorithms''
(available online as a free PDF download. You're welcome to check it out, but
it is a large book and only about 20\% of the content is relevant to this course!).

You move into a new house which has a phone
installed. You can't remember the phone number, but you suspect it
might be {\tt 555-3226} (some of you may recognise
this as being the phone number for Homer Simpson's ``Mr Plow'' business).
To test this hypothesis, you carry out an experiment
by picking up the phone and dialing {\tt 555-3226}.

If you are correct about
the phone number, you will definitely hear a busy signal because you are calling
yourself.
If you are incorrect, the probability of hearing a busy signal is $1/100$.
However, all of that is only true if you assume the phone is working, and it
might be broken! If the phone is broken, it will always give a busy signal.

When you do the experiment, the outcome (the data) is that you do actually get the busy signal.
The question asked us to consider the following four hypotheses, and to calculate
their posterior probabilities:
\begin{table}[!ht]
\begin{center}
\begin{tabular}{|c|c|c|c|}
\hline
Hypothesis & Description & Prior Probability\\
\hline
$H_1$ & Phone is working and {\tt 555-3226} is correct & 0.4\\
$H_2$ & Phone is working and {\tt 555-3226} is incorrect & 0.4\\
$H_3$ & Phone is broken and {\tt 555-3226} is correct & 0.1\\
$H_4$ & Phone is broken and {\tt 555-3226} is incorrect & 0.1\\
\hline
\end{tabular}
\caption{\it The four hypotheses about the state of the phone and the phone
number. The prior probabilities are also given.
\label{tab:phone}}
\end{center}
\end{table}
Note that the four hypotheses are mutually exclusive and exhaustive. If you were
to come up with hypotheses yourself, ``phone is working'' and ``{\tt 555-3226} is correct''
might spring to mind. They wouldn't be mutually exclusive so you couldn't do a
Bayes' Box with just those two, but it is possible to put these together (using
``{\bf and}'') to make the four mutually exclusive options in the table.

\subsection{Solution}
We will go through the solution using a Bayes' Box. The four hypotheses listed
in Table~\ref{tab:phone} and their prior probabilities are given, so we can fill
out the first two columns of a Bayes' Box right away:
\begin{table}[!ht]
\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\hline
{\bf Hypotheses} & {\tt prior} & {\tt likelihood} &
{\tt prior $\times$ likelihood} & {\tt posterior}\\
\hline
$H_1$ & 0.4 &  &  & \\
$H_2$ & 0.4 &  &  & \\
$H_3$ & 0.1 &  &  & \\
$H_4$ & 0.1 &  &  & \\
\hline
Totals: & 1 & & & \\
\hline
\end{tabular}
\end{center}
\end{table}
The next thing we need is the likelihoods. The outcome of the experiment (the
data) was the busy signal, so we need to work out $P(\textnormal{busy signal} | H)$ for each $H$
in the problem (there are four of them). Let's start (naturally!) with $H_1$.

If we assume $H_1$ is true, then the phone is working and {\tt 555-3226} is the
correct phone number. In that case, we would definitely get a busy signal
because we are calling ourselves. Therefore
$P(\textnormal{busy signal} | H_1) = 1$ is our first likelihood value.

Next, let's imagine that $H_2$ is true, so the phone is working, but
{\tt 555-3226} is not the right phone number. In this case, it is given in the
question that the probability of getting a busy signal is $1/100$ or 0.01 (in
reality, this would be based on some other data, or perhaps be a totally
subjective judgement).
Therefore $P(\textnormal{busy signal} | H_2) = 0.01$, and that's our second
likelihood value.

The likelihoods for $H_3$ and $H_4$ are quite straightforward
because they both imply the phone is broken, and that means a busy signal is certain.
Therefore $P(\textnormal{busy signal} | H_3) = P(\textnormal{busy signal} | H_4) = 1$.
We have our four likelihoods, and can proceed to work out everything in the
Bayes' Box, including the main goal -- the posterior probabilities! Here it is:
\begin{table}[!ht]
\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\hline
{\bf Hypotheses} & {\tt prior} & {\tt likelihood} &
{\tt prior $\times$ likelihood} & {\tt posterior}\\
\hline
$H_1$ & 0.4 & 1 &  0.4 & 0.662\\
$H_2$ & 0.4 & 0.01 & 0.004 & 0.00662\\
$H_3$ & 0.1 & 1 & 0.1 & 0.166\\
$H_4$ & 0.1 & 1 & 0.1 & 0.166\\
\hline
Totals: & 1 & & 0.604 & 1\\
\hline
\end{tabular}
\end{center}
\end{table}

To conclude this phone problem, I should admit that
I actually calculated the numbers in the Bayes' Box using R. My code is shown
below. A lot of the code we write in labs will look like this. Obviously in the
2012 exam the students had to use their calculators instead.
\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
prior = c(0.4, 0.4, 0.1, 0.1) # Vector of prior probs
lik = c(1, 0.01, 1, 1)        # Vector of likelihoods
h = prior*lik
Z = sum(h)                    # Sum of prior times likelihood
post = prior*lik/Z            # Normalise to get posterior
# Look at all the results
print(prior)
print(lik)
print(h)
print(Z)
print(post)
\end{minted}

Now let's try to see if this makes sense. There are many things we could think
about, but let's just consider the question of whether
the phone is working or not. The first two hypotheses correspond to the phone
being in a working state. If you want to calculate the probability of $A$
{\bf or} $B$, then you can just add the probabilities if they are mutually
exclusive. The prior probability that the phone is working is
therefore:
\begin{eqnarray}
P(\textnormal{phone working}) &=& P(H_1 \vee H_2)\\
&=& P(H_1) + P(H_2)\\
&=& 0.4 + 0.4\\
&=& 0.8.
\end{eqnarray}
Here, I have introduced the notation $\vee$, meaning ``logical or'': For
any two propositions $A$, $B$, the proposition $(A \vee B)$ is true if
either one of $A$ or $B$ is true (or both).

The posterior probability is worked out in a similar way, but using the posterior
probabilities instead of the prior ones:
\begin{eqnarray}
P(\textnormal{phone working}|\textnormal{busy signal}) &=& P(H_1 \vee H_2 | \textnormal{busy signal})\\
&=& P(H_1|\textnormal{busy signal}) + P(H_2|\textnormal{busy signal})\\
&=& 0.662 + 0.00662\\
&=& 0.6689.
\end{eqnarray}
Our probability that the phone is working has gone down a little bit as a result of this
evidence! That makes sense to me. A busy signal is what you would expect to
happen if the phone was broken. This data doesn't {\it prove} the phone is
broken, but it does point in that direction a little bit, and hence the
probability that the phone is working has been reduced from 0.8 to 0.6689.

\section{Important Equations}
Posterior probabilities are calculated using Bayes' rule. For a single
hypothesis $H$ given data $D$, Bayes' rule is:
\begin{eqnarray}
P(H|D) = \frac{P(H)P(D|H)}{P(D)}\label{eq:bayes1}
\end{eqnarray}
This gives the posterior probability $P(H|D)$ in terms of the prior probability
$P(H)$, the likelihood $P(D|H)$ and the marginal likelihood $P(D)$ in the
denominator. To obtain $P(H)$, think about your prior beliefs (which may
indicate a large amount of uncertainty, or may already be well informed based
on previous data sets). To obtain $P(D|H)$, think about what the experiment is
doing: If $H$ is true, what data would you expect to see and with what
probabilities?

The denominator is the probability of obtaining the data $D$ but without
assuming that $H$ is either true or false. This is obtained using the sum rule.
There are two ways that the data $D$ could occur, either via the route of $H$
being true (this has probability $P(H)P(D|H)$), or via the route of $H$ being
false (this has probability $P(\bar{H})P(D|\bar{H})$). These two ways are
mutually exclusive, so we can add their probabilities:
\begin{eqnarray}
P(D) = P(H)P(D|H) + P(\bar{H})P(D|\bar{H}).
\end{eqnarray}

Bayes' rule can be applied to a whole set
of hypotheses (that are mutually exclusive and exhaustive) simultaneously.
This is a more common way of using it, and it is the way we use it when we use
a Bayes' Box. If we applied Equation~\ref{eq:bayes1} to $N$ hypotheses
$H_1, H_2, ..., H_N$, given data $D$, we would get the following for the posterior
probability of each hypothesis $H_i$ (for $i=1, 2, ..., N$):
\begin{eqnarray}
P(H_i|D) &=& \frac{P(H_i)P(D|H_i)}{P(D)}
\end{eqnarray}
The denominator $P(D)$ is a single number. It does not depend on the index $i$.
It can again be obtained using the sum rule. There are $N$
mutually exclusive ways that the data $D$ could have occurred: via $H_1$ being
true, or via $H_2$ being true, etc. Adding the probabilities of these gives:
\begin{eqnarray}
P(D) &=& \sum_{i=1}^N P(H_i)P(D|H_i).
\end{eqnarray}
which just happens to be the sum of the prior times likelihood values.
If you don't find equations particularly easy to read, just remember that
following the steps for making a Bayes' Box is equivalent to applying
Bayes' rule in this form! The $P(H_i)$ values are the prior probability column, the
$P(D|H_i)$ values are the likelihood column, and the denominator is the
sum of the prior times likelihood column. For example, the posterior probability
for $H_1$ (the top right entry in a Bayes' Box) is given by the prior probability
for $H_1$ times the likelihood for $H_1$, divided by the sum of prior times
likelihood values. That is, $P(H_1|D) = P(H_1)P(D|H_1)/P(D)$.
The correspondence between the probabilities that go in a Bayes'
Box (in general) and the terms in the Equations are given in
Table~\ref{tab:general_bayes_box}.

\begin{table}[!ht]
\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\hline
{\bf Hypotheses} & {\tt prior} & {\tt likelihood} &
{\tt prior $\times$ likelihood} & {\tt posterior}\\
\hline
$H_1$ & $P(H_1)$ & $P(D|H_1)$ & $P(H_1)\times P(D|H_1)$ & $P(H_1|D)$\\
$H_2$ & $P(H_2)$ & $P(D|H_2)$ & $P(H_2)\times P(D|H_2)$ & $P(H_2|D)$\\
\ldots & \ldots & \ldots & \ldots & \ldots\\
\hline
Totals: & 1 & & $P(D)$ & 1\\
\hline
\end{tabular}
\caption{\it A general Bayes' Box. Using Bayes' rule or making a Bayes' Box are
actually the same thing, and this table can be used to identify the
terms.\label{tab:general_bayes_box}}
\end{center}
\end{table}


% When using TeXShop on the Mac, let it know the root document.
% The following must be one of the first 20 lines.
% !TEX root = ../design.tex

\chapter[Linear-chain Conditional Random Field]{Linear-chain Conditional Random Field}
% Motivation. Why do we want to have this abstract layer?
Conditional random field(CRF) \cite{DBLP:conf/icml/LaffertyMP01} is a type of discriminative undirected probabilistic graphical model.
Linear-chain CRFs are special CRFs which assume that the next state depends only on the current state.
Linear-chain CRFs achieve state of the art accuracy in some real world natural language processing tasks such
as part of inforamtion extraction\cite{chen2012optimizing}, speech tagging(POS) and named entity resolution(NER).

\section{Linear-chain CRF Learning}

\subsection{Mathematical Notations}
\begin{itemize}
\item $p(\boldsymbol Y | \boldsymbol X)$: conditional probability distributions of label sequence $\boldsymbol Y$ given input sequence $\boldsymbol X$.
\item $M$: total number of unique features.
\item $I$: the position of last token in a sentence.
\item $N$: number of sentences in the training data set.
\item $\lambda$: the coefficients (feature weights).
\item $\ell_{\lambda}$: log-likelihood summed over all training sentences.
\item $\nabla \ell_{\lambda}$: gradient vector summed over all training sentences.
\item $\ell_{\lambda}^\prime$: adjusted log-likelihood to avoid overfitting using spherical Gaussian weight prior.
\item $\nabla \ell_{\lambda}^\prime$: adjusted gradient vector to avoid overfitting using spherical Gaussian weight prior.

\end{itemize}

\subsection{Formulation}
A linear-chain CRF \cite{DBLP:conf/naacl/ShaP03} is a distribution
    \[p(\boldsymbol Y | \boldsymbol X) = \frac{\exp{\sum_{m=1}^M \sum_{i=0}^{I} \lambda_m f_m(y_i,y_{i-1},x_i)}}{Z(X)},\]

where Z(X) is an instance specific normalizer
\[Z(X) = \sum_{y} \exp{\sum_{m=1}^M \sum_{i=0}^{I} \lambda_m f_m(y_i,y_{i-1},x_i)}.\]

Train a CRF by maximizing the log-likelihood of a given training set $ T=\{(\vec{x^{(k)}},\vec{y^{(k)}})\}_{k=1}^N$.
Seek the zero of the gradient.\\
    \[\ell_{\lambda}=\sum_k \log p_\lambda(\vec{y^{(k)}}|\vec{x^{(k)}}) =\sum_k[\lambda F(\vec{y^{(k)}},\vec{x^{(k)}})-\log Z_\lambda(\vec{x^{(k)}})]\]
    \[\nabla \ell_{\lambda}=\sum_k[F(\vec{y^{(k)}},\vec{x^{(k)}})-E_{p_\lambda(Y|\vec{x^{(k)}})}F(Y,\vec{x^{(k)}})]\]

To avoid overfitting, we penalize the likelihood with a spherical Gaussian weight prior:\\
    \[\ell_{\lambda}^\prime=\sum_k[\lambda F(\vec{y^{(k)}},\vec{x^{(k)}})-\log Z_\lambda(\vec{x^{(k)}})]-\frac{\lVert \lambda \rVert^2}{2\sigma ^2}\]
    \[\nabla \ell_{\lambda}^\prime=\sum_k[F(\vec{y^{(k)}},\vec{x^{(k)}})-E_{p_\lambda(Y|\vec{x^{(k)}})}F(Y,\vec{x^{(k)}})]-\frac{\lambda}{\sigma ^2}\]
Note:We hard code $\sigma$ as $100$ in the implementation which follows other CRF packages in the literature.

\subsection{Forward-backward Algorithm}
$E_{p_\lambda(Y|x)}F(Y,x)$ is computed using a variant of the forward-backward algorithm:

    \[E_{p_\lambda(Y|x)}F(Y,x) = \sum_y p_\lambda(y|x)F(y,x) = \sum_i\frac{\alpha_{i-1}(f_i*M_i)\beta_i^T}{Z_\lambda(x)}\]
    \[Z_\lambda(x) = \alpha_I.1^T\]
    where $\alpha_i$ and $\beta_i$ the forward and backward state cost vectors defined by\\
  \[\alpha_i =
    \begin{cases}
    \alpha_{i-1}M_i, & 0<i<=n\\
    1, & i=0
    \end{cases}
    ,
    \beta_i^T =
    \begin{cases}
    M_{i+1}\lambda_{i+1}^T, & 1<=i<I\\
    1, & i=n
    \end{cases}
  \]

\subsection{L-BFGS Convex Solver}

The limited-memory BFGS(L-BFGS) \cite{DBLP:journals/siamjo/MoralesN00} is the
limited memory variation of the Broyden-Fletcher-Goldfarb-Shanno(BFGS) algorithm
which is the state of art of large scale non constraint convex optimization
method. We translate the in-memory Java implementation to C++ in-database
implementation using Eigen support. Eigen vector and Eigen matrix are used
instead of the plain one dimentional and two dimentional arrays. In the Java in-
memory implementation, it defines many static variables defined and shared
between the interations. However, in the MADlib implementation, we define these
variables in the state object. Before each iteration of L-BFGS optimization, we
need to initialize the L-BFGS with the current state object.  At the end of each
iteration, we need to dump the updated variables to the database state for next
iteration.

\subsection{Parallel CRF Training}
\begin{algorithm}[CRF training$(z_{1:M})$] \label{alg:CRF training}
\alginput{Observation set $z_{1:M}$,\\
convergence criterion $\mathit{Convergence}()$,\\
start strategy $\mathit{Start}()$,\\
initialization strategy $\mathit{Initialization}()$,\\
transition strategy $\mathit{Transition}()$,\\
finalization strategy $\mathit{Finalization}()$}
\algoutput{Coefficients $w \in \mathbb{R}^N$}
\algprecond{$iteration = 0, diag = \boldsymbol 1$}
\begin{algorithmic}[1]
	\State $w_\text{new} \set \mathit{Start}(z_{1:M})$
	\Repeat
        \State $w_\text{old} \set w_\text{new}$
        \State $\mathit{state} \set \mathit{Initialization}(w_\text{new})$
		\For{$m \in 1..M$} \Comment{Single entry in the observation set}
			\State $\mathit{state} \set \mathit{Transition}(\mathit{state}, z_m)$
                \Comment{Computing gradient and log-likelihood.}
		\EndFor
		\State $w_\text{new} \set Finalization(\mathit{state})$ \Comment{Mainly invoke L-BFGS convex solver}
	\Until{$Convergence(w_\text{new}, g_\text{new}, \mathit{iteration})$}
    \State \Return $w_\text{new}$
\end{algorithmic}
\end{algorithm}

\paragraph{Programming Model.}
We provide above the algorithm of parallel CRF training strategy, in the fashion of the selected programming model supported by MADlib (mainly user-defined aggregate).

\paragraph{Parallelism.}
The outer loop is inherently sequential over multiple iterations.
The iteration $n+1$ takes the output of iteration $n$ as input, so on so forth until the stop criterion is satisfied.
The inner loop which calculates the gradient and log-likelihood for each document is data-parallel.
Simple model averaging are used to merge two states.
A merge function is not explicitly added to the pseudocode for simplicity.
The finalization function invokes the L-BFGS convex solver to get a new solution. L-BFGS is sequential, but very fast.
Experiments show that the speed-up ration approaches the number of segments configured in the Greenplum database.

\paragraph{Convergence criterion.}
Usually, the following conditions are combined by AND, OR, or NOT.
\begin{enumerate}
    \item The norm of gradient divided by the norm of coefficient drops below a given threshold.
    \item The maximum number of iterations is reached.
    \item There could be more.
\end{enumerate}

\paragraph{Start strategy.}
In most cases, zeros are used unless otherwise specified.

\paragraph{Transition strategies.}
This function contains the logic of computing the gradient and log-likelihood for each tuple using the forward-backward
algorithm. The algorithms will be discussed in the following sections.

\begin{algorithm}[transition-lbfgs$(\mathit{state}, z_m)$] \label{alg:transition-lbfgs}
\alginput{Transition state $\mathit{state}$,\\
observation entry $z_m$,\\
gradient function $\mathit{Gradient}()$}
\algoutput{Transition state $\mathit{state}$}
\begin{algorithmic}[1]
    \State $\{state.g,state.loglikelihood\}  \set \mathit{Gradient}(\mathit{state}, z_m)$
        \Comment{using forward-backward algorithm to calculate gradient and loglikelihood}
    \State $\mathit{state}.num\_rows \set \mathit{state}.num\_rows + 1$
    \State \Return $\mathit{state}$
\end{algorithmic}
\end{algorithm}

\paragraph{Merge strategies.}
The merge function simply sums the gradient and log-likelihood over all training documents
\begin{algorithm}[merge-lbfgs$(\mathit{state_1}, \mathit{state_2})$] \label{alg:merge-lbfgs}
\alginput{Transition state $\mathit{state_1}$,\\
Transition state $\mathit{state_2}$}
\algoutput{Transition state $\mathit{state_{new}}$}
\begin{algorithmic}[1]
    \State $\mathit{state_{new}}.g \set \mathit{state_1}.g + \mathit{state_2}.g$
    \State $\mathit{state_{new}}.loglikelihood \set \mathit{state_1}.loglikelihood + \mathit{state_2}.loglikelihood$
    \State \Return $\mathit{state_{new}}$
\end{algorithmic}
\end{algorithm}

\paragraph{Finalization strategy.}
The finalization function invokes the L-BFGS convex solver to get a new coefficent vector.\\

\begin{algorithm}[finalization-lbfgs$(state)$] \label{alg:CRF training}
\alginput{Transition state $state$,\\
LBFGS $\mathit{lbfgs}()$}
\algoutput{Transition state  $state$}
\begin{algorithmic}[1]
        \State $\{state.g,state.loglikelihood\} \set penalty(state.g,state.loglikelihood)$ \Comment{To avoid overfitting, add penalization}
        \State $\{state.g,state.loglikelihood\}\set-\{state.g,state.loglikelihood\}$ \Comment{negation for maximization}
        \State LBFGS instance($state)$ \Comment{initialize the L-BFGS instance with previous state}
        \State instance.$lbfgs()$ \Comment{invoke the L-BFGS convex solver}
        \State instance.save\_state$(state)$ \Comment{save updated variables to the state for next iteration}
        \State \Return $state$
\end{algorithmic}
\end{algorithm}

Feeding with current solution, gradient, log-likelihood, etc., the L-BFGS will output a new solution.
To avoid overfitting, a penalization function is needed. We choose to penalize the log-likelihood with a spherical Gaussian weight prior.
Also, L-BFGS is to maximum objective, so we need to negate the gradient vector and log-likelihood to fit our needs in order minimize the log-likehood.

\section{Linear-chain CRF Applications}
Linear-chain CRF can be used in various applications such as  part of speech tagging and named entity resolution.
All the following sections assume that the application is part of speech tagging. It can be fitted to named entity resolution with minimal effort.

\subsection{Part of Speech Tagging}
Part-of-speech tagging, also called grammatical tagging or word-category disambiguation \cite{DBLP:journals/coling/DeRose88}, is the process of assigning
a part of speech to each word in a sentence. POS has been widely used in information retrieval and text to speech. There are two distinct methods for
POS task: rule-based and stochastic.
In rule-based method, large collection of rules are defined to indentify the tag. Stochastic method is based on probabilistic
graphic models such as hidden markov models and conditional random fields. In practice, conditional random fields are approved
to achieve the state of art accuracy.
\subsection{Tag Set}
There are various tag set used in the literature. The Pennsylvania Treebank tag-set \cite{DBLP:journals/coling/MarcusSM94} is the commonly used tag set and contains
45 tags. The following table shows part of tags in the tag set.

\begin {table}[h]
\caption {Pen Treebank III Tag Set} \label{tab:tagset}
\[\begin{tabular}{lll||lll}
  Tag & Description         & Example       & Tag & Description         & Example\\
  \hline
  CC  & Coordin,Conjunction & and,but,or    & SYM & Symbol              & +,\%,\&\\
  CD  & Cardinal number     & one,two,three & TO  & 'to'                & to\\
  DT  & Determiner          & a,the         & UH  & Interjection        & ah,oops\\
  EX  & Existential         & there         & VB  & Verb,base form      & eat\\
  ... & ...                 & ...           & ... & ...                 & ...\\
  RBR & Adverb,comparative  & faster        & .   & Sentence-final      & (.!?)\\
  RBS & Adverb,superlative  & fastest       & :   & Mid-sentence punc   & (:;...-)\\
  RP  & Particle            & up,off        &     &                     &\\
  \hline
\end{tabular}\]
\end{table}

\subsection{Regular Expression Table}
  Regex feature captures the relationship between the morphology of a token and it's corresponding tag.
For example, a token ending with 's' is mostly likely to be a plural noun whereas a token ending with 'ly' is more likely to be an
adverb. One can define his/her own regular expressions to capture the intrinsic characteristics of the given training data.
\begin {table}[h]
\caption {Regular expression table} \label{tab:regex}
\begin{center}
\begin{tabular}{ll||ll}
  pattern & name             & pattern & name\\
  \hline
  $\wedge[A-Z][a-z]+\$$    & InitCapital       & $\wedge[A-Z]+\$$  & isAllCapital \\
  $\wedge.*[0-9]+.*\$$     & containsDigit     & $\wedge.+[.]\$$   & endsWithDot\\
  $\wedge.+[,]\$$          & endsWithComma     & $\wedge.+er\$$    & endsWithEr\\
  $\wedge.+est\$$	   & endsWithEst       & $\wedge.+ed\$$    & endsWithEd\\
  $\wedge.+s\$$	           & endsWithS         & $\wedge.+ing\$$   & endsWithIng\\
  $\wedge.+ly\$$	   & endsWithly        & $\wedge.+-.+\$$   & isDashSeparatedWords\\
  $\wedge.*@.*\$$	   & isEmailId         &           & \\
  \hline
\end{tabular}
\end{center}
\end{table}

\section{Feature Extraction}
The Feature Extraction module provides functionality for basic text-analysis
tasks such as part-of-speech (POS) tagging and named-entity resolution.
At present, six feature types are implemented.
    \begin{itemize}
    \item Edge Feature: transition feature that encodes the transition feature weight from current label to next label.
    \item Start Feature: fired when the current token is the first token in a sentence.
    \item End Feature: fired when the current token is the last token in a sentence.
    \item Word Feature: fired when the current token is observed in the trained dictionary.
    \item Unknown Feature: fired when the current token is not observed in the trained dictionary for at least certain times.
    \item Regex Feature: fired when the current token can be matched by the regular expression.
    \end{itemize}

Advantages of extracting features using SQL statements:
\begin{itemize}
\item [$\star$] Decoupling the feature extraction and other code.
\item [$\star$] Compared with procedure language, SQL is much more easier to understand.
\item [$\star$] Storing all the features in tables avoids recomputing of features over iterations. It also boosts the performance.
\item [$\star$] SQL is naivelly paralleled.
\end{itemize}

\subsection{Column Names Convention and Table Schema}
\subsubsection{Column Names Convention}
The following column names are commonly used in the tables of the following sections.
\begin{itemize}
\item doc\_id: Unique integer indentifier of a document.
\item start\_pos: Position of the token in the document starting from 0.
\item seg\_text: Text token itself.
\item prev\_label: Label of previous token.
\item label: Label of current token.
\item max\_pos: End positon of the document.
\item weight: Feature weight associated with certain feature
\item f\_index: Unique integer identifier of a feature
\item f\_name: Feature name.
\end{itemize}

\subsubsection{Training and Testing Data Schema}
The text data has to been tokenized before it can be stored in the database. One of the commonly used tokenization program for part-of-speech
tagging is the Treebank tokenization script.
The following table dipicts how the training/testing data is stored in the database table.

\begin {table}[h]
\caption {Training or Testing data} \label{tab:trainingdata}
\begin{center}
    \footnotesize\tt
\begin{tabular}{llll||llll}
  start\_pos & doc\_id & seg\_text & label & start\_pos & doc\_id & seg\_text & label\\
  \hline
        0&1&'confidence'&11& 1&1&'in'&5\\
        2&1&'the'&2& 3&1&'pound'&11\\
        4&1&'is'&31& 5&1&'widely'&19\\
        6&1&'expected'&29& 7&1&'to'&24\\
        8&1&'take'&26& 9&1&'another'&2\\
        10&1&'sharp'&6& 11&1&'dive'&11\\
        12&1&'if'&5& 13&1&'trade'&11\\
        14&1&'figures'&12& 15&1&'for'&5\\
        16&1&'september'&13& 17&1&','&42\\
        18&1&'due'&6& 19&1&'for'&5\\\hline
\end{tabular}
\end{center}
\end{table}

\subsection{Design Challanges and Work-arounds}
As far as I know, the MADlib C++ abstraction layer doesn't support array of self-defined composite data types or multi-dimentional arrays.
But we do have the need of these complex data types in the implemenation of this module. For example, the
$viterbi\_mtbl$ table is indeed a two dimentional arrays. Due to the limitations of current C++ abstraction layer, we have to convert the matrix
to an array and later index the data with  $M[i*n+j]$ instead of the normal way $M[i][j]$. Another example is the data types to represent the
features. A single feature cannot be represented by a single DOUBLE varialbe but by a unit of struct : $[prev\_label, label, f\_index, start\_pos, exist]$
But there is no arrays of struct type, we had to represent it with one dimentional array.
Also we have to store the features for a document using  array of doubles instead of array of struct.

\subsection{Training Data Feature Extraction}
Given training data, SQLs are writen to extract all the features. It happened that any type of features mentioned above can be extracted out by one single SQL clause which makes the code succinct. We illustrate the training data feature extraction by SQL clause examples.

\paragraph{Sample Feature Extraction SQLs for edge features and regex features}
\begin{itemize}
\item $SQL_1$:\\
\begin{lstlisting}[language=SQL,gobble=4]
    SELECT doc2.start_pos, doc2.doc_id, 'E.', ARRAY[doc1.label, doc2.label]
    FROM   segmenttbl doc1, segmenttbl doc2
    WHERE  doc1.doc_id = doc2.doc_id AND doc1.start_pos+1 = doc2.start_pos
\end{lstlisting}

\item $SQL_2$:\\
\begin{lstlisting}[language=SQL,gobble=4]
    SELECT start_pos, doc_id, 'R_' || name, ARRAY[-1, label]
    FROM  regextbl, segmenttbl
    WHERE seg_text ~ pattern
\end{lstlisting}
\end{itemize}

\paragraph{Build the feature dictionary and assign each feature with a unique feature id}
\begin{itemize}
\item $SQL_3$\\
\begin{lstlisting}[language=SQL,gobble=4]
    INSERT INTO tmp_featureset(f_name, feature)
    SELECT DISTINCT f_name, feature
    FROM   tmp1_feature;
    INSERT INTO featureset(f_index, f_name, feature)
    SELECT nextval('seq')-1, f_name, feature
    FROM   tmp_featureset;
\end{lstlisting}
\end{itemize}

\paragraph{Generate sparse\_r table}
\begin{itemize}
\item $SQL_3$\\
\begin{lstlisting}[language=SQL,gobble=4]
    INSERT INTO rtbl(start_pos,doc_id,feature)
    SELECT start_pos, doc_id, array_cat(fset.feature,
			ARRAY[f_index,start_pos,
			CASE WHEN tmp1_feature.feature = fset.feature THEN 1
			ELSE 0 END] )
    FROM   tmp1_feature, featureset fset
    WHERE  tmp1_feature.f_name = fset.f_name AND fset.f_name <> 'E.';
\end{lstlisting}
\end{itemize}

The final input table schema which contains all the feature data for the crf learning algorithm is as follows:
\begin{center}
    \begin{tabular}{ | l | l | l | l |}
    \hline
    doc\_id & sparse\_r & dense\_m & sparse\_m \\
    \hline
    \end{tabular}
\end{center}

\begin{itemize}

\item
sparse r feature(single state feature):(prev\_label, label, f\_index, start\_pos, exist)

\begin{center}
    \begin{tabular}{ | l | l |}
    \hline
    label & Description \\ \hline
    prev\_label & the label of previous token, it is always 0 in r table.\\
    label       & the label of the single state feature\\
    f\_index    & the index of the feature in the feature table\\
    start\_pos  & the postion of the token(starting from 0)\\
    exist       & indicate whether the token exists or not in the acutal training data set\\
    \hline
    \end{tabular}
\end{center}

\item
dense m feature:
(prev\_label, label, f\_index, start\_pos, exist)
\begin{center}
    \begin{tabular}{ | l | l |}
    \hline
    label & Description \\ \hline
    prev\_label & the label of previous token.\\
    label       & the label of current token\\
    f\_index    & the index of the feature in the feature table\\
    start\_pos  & the postion of the token in a sentence(starting from 0)\\
    exist       & indicate whether the token exists or not in the acutal training data set\\
    \hline
    \end{tabular}
\end{center}

\item
sparse m feature:(f\_index, prev\_label, label)
\begin{center}
    \begin{tabular}{ | l | l |}
    \hline
    label & Description \\ \hline
    f\_index    &  the index of the feature in the feature table\\
    prev\_label &  the label of previous token \\
    label       &  the label of current token\\
    \hline
    \end{tabular}
\end{center}

\end{itemize}
For performance consideraton, we split the m feature to $dense\_m$ feature and $sparse\_m$ feature\\
The acutal $spare\_r$ table is array union of individal r features ordered by the start positon of tokens.
So the function to compute the gradient vector and log-likelihood can scan the feature arrays from beginning to end.

\subsection{Learned Model}
The CRF learning algorithm will generate two tables: feature table and dictionary table
Feature table stores all the features and their corresponding feature weight.
The dictionary constains all the tokens and the number of times they appear in the training data.\\
\begin {table}[h]
\caption {Feature table} \label{tab:featuretbl}
\begin{center}
    \scriptsize\tt
    \begin{tabular}{ | l | l | l | l | l || l | l | l | l | l | }
    \hline
    f\_index & f\_name & prev\_label & label & weight & f\_index & f\_name & prev\_label & label & weight\\
    \hline
    0&'U'&-1&6&2.037& 1&'E.'&2&11&2.746   \\
    2&'W\_exchequer'&-1&13&1.821& 3&'W\_is'&-1&31&1.802 \\
    4&'E.'&11&31&2.469& 5&'W\_in'&-1&5&3.252 \\
    6&'E.'&11&12&1.305& 7&'U'&-1&2&-0.385 \\
    8&'E.'&31&29&1.958& 9&'U'&-1&29&1.422 \\
    10&'R\_endsWithIng'&-1&11&1.061&11&'W\_of'&-1&5&3.652 \\
    12&'S.'&-1&13&1.829& 13&'E.'&24&26&3.282 \\
    14&'W\_helped'&-1&29&1.214& 15&'E.'&11&24&1.556 \\
    \hline
    \end{tabular}
\end{center}
\end {table}

\begin {table}[h]
\caption {Dictionary table} \label{tab:title}
\begin{center}
    \scriptsize\tt
    \begin{tabular}{ | l | l || l | l || l | l || l | l | }
    \hline
    token     & total & token   & total & token     & total & token       & total\\
    'freefall'& 1     & 'policy'& 2     & 'measures'&1      & 'commitment'&1\\
    'new'&1& 'speech'&1& '''s'&2& 'reckon'&1\\
    'underlying'&1&'week'&1& 'prevent'&1& 'has'&2\\
    'failure'&1& 'restated'&1&'announce'&1& 'thursday'&1\\
    'but'&1& 'lawson'&1& 'last'&1& 'firm'&1\\
    'exchequer'&1& 'helped'&1& 'sterling'&2& $\ldots$ & $\ldots$\\
    \hline
    \end{tabular}
\end{center}
\end {table}

\subsection{Testing Data Feature Extraction}
  This component extracts features from the testing data based on the learned models.
  It will produce two factor tables
  $viterbi\_mtbl$ and  $viterbi\_rtbl$. The $viterbi\_mtbl$
  table and a $viterbi\_rtbl$ table are used to calculate the best label
  sequence for each sentence.

\paragraph{Sample Feature Extraction SQLs}
\begin{itemize}
\item $SQL_1$: Extracting unique tokens:\\
              \begin{lstlisting}[language=SQL,gobble=4]
                        INSERT INTO segment_hashtbl
                        SELECT DISTINCT seg_text
                        FROM   segmenttbl
              \end{lstlisting}

\item $SQL_2$: Summerize over all single state features with respect to specific tokens and labels :\\
              \begin{lstlisting}[language=SQL,gobble=4]
                        INSERT INTO viterbi_rtbl
                        SELECT seg_text, label, SUM(value)
                        FROM   rtbl
                        GROUP BY seg_text,label
              \end{lstlisting}
\end{itemize}
\begin{center}
    \begin{tabular}{ | l | l | l | l |}
    \hline
    doc\_id & $viterbi\_mtbl$ & $viterbi\_rtbl$ \\
    \hline
    \end{tabular}
\end{center}

  \begin{itemize}
  \item
  $viterbi\_mtbl$ table
  encodes the edge features which are solely dependent on upon current label and
  previous y value. The m table has three columns which are prev\_label, label,
  and value respectively.
  If the number of labels is $n$, then the m factor table will have $n^2$
  rows. Each row encodes the transition feature weight value from the previous label
  to the current label.

  startFeature is considered as a special edge feature which is from the
  beginning to the first token. Likewise, endFeature can be considered
  as a special edge feature which is from the last token to the very end.
  So m table encodes the edgeFeature, startFeature, and endFeature.
  If the total number of labels in the label space is 45 from 0 to 44,
  then the m factor array is as follows:
  \item
  $viterbi\_r$ table
  is related to specific tokens. It encodes the single state features,
  e.g., wordFeature, RegexFeature for all tokens. The r table is represented as
  shown in the table.\\

\begin {table}[h]
\caption {viterbi\_mtbl table} \label{tab:viterbimtbl}
 \begin{center}
%\tiny\tt
  \begin{tabular}{l*{6}{c}r}
   token             & 0   & 1   & 2   & 3   & ... & 43 &  44 \\
   \hline
  -1                 & 2.1 & 1.1 & 1.0 & 1.1 & 1.1 & 2.1 & 1.1  \\
   0                 & 1.1 & 3.9 & 1.2 & 2.1 & 2.8 & 1.8 & 0.8  \\
   1                 & 0.7 & 1.7 & 2.9 & 3.8 & 0.6 & 3.2 & 0.2  \\
   2                 & 0.2 & 3.2 & 3.8 & 2.9 & 0.2 & 0.1 & 0.2  \\
   3                 & 1.2 & 6.9 & 7.8 & 8.0 & 0.1 & 1.9 & 1.7  \\
   ...               & ... & ... & ... & ... & ... & ... & ...  \\
   44                & 8.2 & 1.8 & 3.7 & 2.1 & 7.2 & 1.3 & 7.2  \\
   45                & 1.8 & 7.8 & 5.6 & 9.8 & 2.3 & 9.4 & 1.1
  \end{tabular}
 \end{center}
\end{table}
\begin {table}[h]
\caption {viterbi\_rtbl table} \label{tab:viterbirtbl}
 \begin{center}
%\tiny\tt
  \begin{tabular}{l*{6}{c}r}
   token             & 0   & 1   & 2   & 3   & ... & 43 &  44 \\
   \hline
   madlib            & 0.2 & 4.1 & 0.0 & 2.1 & 0.1 & 2.5 & 1.2  \\
   is                & 1.3 & 3.0 & 0.2 & 3.1 & 0.8 & 1.9 & 0.9  \\
   an                & 0.9 & 1.1 & 1.9 & 3.8 & 0.7 & 3.8 & 0.7  \\
   open-source       & 0.8 & 0.2 & 1.8 & 2.7 & 0.5 & 0.8 & 0.1  \\
   library           & 1.8 & 1.9 & 1.8 & 8.7 & 0.2 & 1.8 & 1.1  \\
   ...               & ... & ... & ... & ... & ... & ... & ...  \\
  \end{tabular}
 \end{center}
\end{table}
\end{itemize}

\section{Linear-chain CRF Inference}
The Viterbi algorithm \cite{DBLP:journals/scholarpedia/Viterbi09} is the popular algorithm to find the top-k most likely labelings of a document for CRF models.
For the tasks in natural language processing domain, it is sufficient to only generate the best label sequence.
We chose to use a SQL clause to drive the inference over all documents.
In Greenplum, Viterbi can be run in parallel over different subsets of the document on a multi-core machine.

\subsection{Parallel CRF Inference}
The $vcrf\_top\_label$ is implemented sequentially and each function call will finish labeling of one document.
The inference is paralledl in the level of document. We use a SQL statment to drive the inference of all documents.
So, the CRF inference is naivelly parallel.
\begin{lstlisting}[language=SQL,gobble=4]
        SELECT doc_id, vcrf_top1_label(mfactors.score, rfactors.score)
        FROM   mfactors,rfactors
\end{lstlisting}

\subsection{Viterbi Inference Algorithm}
\[
V(i,y) =
\begin{cases}
 \max_{y^\prime}(V(i-1,y^\prime)) + \textstyle \sum_{k=1}^K \lambda_kf_k(y,y^\prime,x_i), & \text{if } i\ge0 \\
 0, & \text{if } i=-1.
\end{cases}
\]

\subsection{Viterbi Inference output}
The final inference output will produce the the best label sequence for each document and also the conditional probability given the
observed input sequence.
\begin {table}[h]
\caption {Viterbi inference output}
\begin{center}
    \scriptsize\tt
    \begin{tabular}{ | l | l | l | l | l | }
    \hline
    doc\_id & start\_pos & token & label & probability     \\\hline
    1   & 0    & madlib        & proper noun, singular &0.6\\
    1   & 1    & is            & Verb, base form       &0.6 \\
    1   & 2    & an            & determiner            &0.6 \\
    1   & 2    & open-source   & adjective             &0.6 \\
    1   & 4    & library       & noun                  &0.6 \\
    1   & 5    & for           & preposition           &0.6 \\
    1   & 6    & scalable      & adjective             &0.6 \\
    1   & 7    & in-dababase   & adverb                &0.6 \\
    1   & 8    & analytics     & noun, singular        &0.6 \\
    1   & 9    & .             & sentence-final punc   &0.6 \\
    2   & 0    & it            & personal pronoun      &0.4 \\
    2   & 1    & provides      & verb, base form       &0.4 \\
    2   & 2    & data-parallel & noun                  &0.4 \\
    2   & 2    & $\ldots$      & $\ldots$                    &0.4 \\
    \hline
    \end{tabular}
\end{center}
\end{table}


\documentclass[letterpaper]{article}

\usepackage{amsmath, amsfonts, amssymb, amsthm}

\usepackage{enumerate,hyperref}
\usepackage[margin=1in]{geometry}
\usepackage[section]{placeins}

\theoremstyle{definition}
\newtheorem{problem}{Problem}
\newtheorem*{lemma}{Lemma}
\newtheorem*{corollary}{Corollary}

\providecommand{\equationref}[1]{Equation \eqref{eq:#1}}
\providecommand{\needscite}{[\textit{Citation Needed}]}

\setcounter{secnumdepth}{0}

\title{Spell Damage Analysis and Stat Weights}
\author{Balor - Anathema}
\date{Status: DRAFT. Last updated \today}

\begin{document}
\maketitle
This analysis was motivated by determining stat weights for a Balance druid casting Starfire. Wherever possible, however, things were kept general so as to be applicable to other spells and classes.
\section{Assumptions}
We use the following assumptions about how damage works.
\begin{itemize}
	\item Spells have a base chance to hit that is purely a function of player level, target level, and Hit gear. Resistance does not affect a spell's chance to hit. For raid bosses, the base spell hit is 83, and thus a spell's percent chance to hit is $(83 + H)$ where $H$ is your total hit bonus from gear or talents.\needscite
	\item Whether a spell lands as a criticial hit is determined after a spell is known to land. That is, a 10\% chance to crit means that 10\% of all spells \textit{that hit} will be critical hits, not that 10\% of all spells that are cast will crit. \needscite This is in contrast to melee attacks, which use a different system to determine hit and crit chance.
	\item Critical hits provide a fixed multiplicative boost to the damage of a spell. This is usually a 1.5 multiplier, but can vary depending on talents. \needscite For Balance Druids, the Vengeance talent gives a 2.0 damage multiplier on critical hits.
	\item Spellpower increases the damage of a spell by increasing the damage of a non-resisted, non-critical hit by $c$ times your total spellpower, where $c$ is a fixed constant for a given spell. Usually, this constant is given by the default cast time for that spell divided by 3.5. \needscite
\end{itemize}

\section{The Damage Formula}
Let $B$ be the base damage of a spell, $c$ be that spell's corresponding spell coefficient, $H \in [0, 16]$ be a player's current total hit bonus (as a percentage, so +12\% hit is $H = 12$. Note that player hit chance can not be increased to 100, so only the first 16 are useful \needscite), $P$ be a player's total spellpower that applies to that spell, and $R \in [0, 100]$ be the player's spell crit, also as a percentage. Finally, let $x$ be the crit bonus, or one minus the crit multiplier (for example, if spell crits do 1.5 times damage in the default case, $x = 0.5$). Then the expected damage from one spell cast on a raid boss is given by the following.
\begin{equation}
\left(0.83 + \frac{H}{100}\right)\left(B + cP\right)\left(1 + x\frac{R}{100}\right)
\label{eq:damage}
\end{equation}
To get DPS, we can simiply divide this by $T$, the total casting time of the spell. There is one complication here for druids, however. The Nature's Grace talent decreases the cast time of your next spell by 0.5 seconds whenever a spell lands a critical hit. Using assumption 2 above, we know that the probability of one spell resulting in a critical hit is $(0.83 + \frac{H}{100})(\frac{R}{100})$. Therefore, we can calculate an average cast time for the spell over a sufficiently long encounter as the following. Note that $t$ here is the casting time reduction that a critical hit yields. In the case of having Nature's Grace, $t=0.5$. If one does not have Nature's Grace, then $t=0$.
\begin{equation}
T - t\left(0.83 + \frac{H}{100}\right)\frac{R}{100}
\label{eq:time}
\end{equation}

Note that this is somewhat inaccurate, as the first spell in a fight is guaranteed to take $T$ time to cast, and so this is truly only the expected cast time for all subsequent spells. Factoring in the additional time from the first cast would require making assumptions on the total encounter length, which we hope to avoid here. Over sufficiently long encounters, these will converge to the same, so the effect of thsi is ignored in the following analysis.

Dividing the expected damage in \equationref{damage} by the expected cast time in \equationref{time} yields our expected total DPS, $D$.

\begin{equation}
D = d\frac{\left(0.83 + \frac{H}{100}\right)\left(mB + cP\right)\left(1 + x\frac{R}{100}\right)}{T - t\left(0.83 + \frac{H}{100}\right)\frac{R}{100}}
\end{equation}

For completeness, we have added in two additional factors, $d$, and $m$. $m$ is any multiplicative modifier on the base damage of a spell that might arise from talents or set bonuses. For example, the Druid talent Moonfury sets $m=1.1$. $d$ is any multiplicative damage modifer on total damage of the spell, including things like Curse of Shadows and the target's resistance. (TODO: add argument for why we can treat resistance, which really determines a probability distrubution of multiplicative damage reductions, as one simple average damage reduction. Also verify that either resistance cannot cause full 100\% damage reductions, or, that if it does, a spell can still be a crit while being 100\% resisted. If this is untrue, resistance will have an effect on Nature's Grace proc rates.).

\section{Stat Weightings}
To determine how we should value each stat ($H$, $P$, $R$), we have to examine how DPS varies as you change each stat. To do so, we will use derivatives, which measure the rate of change of the function with respect to a given parameter. The partial derivatives of DPS with respect to $H$, $P$, and $R$ are given below.

\begin{equation}
\frac{\partial D}{\partial P} = d\frac{c\left(83+H\right)\left(100 + xR\right)}{100^2T - t(83 + H)R}
\end{equation}

\begin{equation}
\frac{\partial D}{\partial H} = d\left(mB + cP\right)\left(100+xR\right) \left(\frac{100^2T}{\left(100^2T - t\left(83+H\right)R\right)^2}\right)
\end{equation}

\begin{equation}
\frac{\partial D}{\partial R} = d\left(mB+cP\right)\left(83+H\right) \left(\frac{xT + t\left(0.83 + \frac{H}{100}\right)}{\left(100T - t\left(0.83 + \frac{H}{100}\right)R\right)^2}\right)
\end{equation}

$\frac{\partial D}{\partial P}$ says that, when adding a very small amount of $P$, we expect the function value to change by $\frac{\partial D}{\partial P}$ \textit{per point of $P$ we varied}. It is the limiting value for very small changes of $P$, which gives a sense of how relevant $P$ is to the output function at a given point in the parameter space.

Since we are concerned with stat weights, what we care most about is how these derivatives relate to each other. If we set the value of one spellpower to be 1 by convention, then taking ratios of derivatives will give us values for the other stats, $R$ and $H$. These equations are as follows.

\begin{equation}
\textrm{HitWeight} = \frac{\frac{\partial D}{\partial H}}{\frac{\partial D}{\partial P}} = \frac{\frac{mB}{c} + P}{83 + H} \left(\frac{100^2 T}{100^2T - t(83 + H)R}\right)
\end{equation}

\begin{equation}
\textrm{CritWeight} = \frac{\frac{\partial D}{\partial R}}{\frac{\partial D}{\partial P}} = x\frac{\frac{mB}{c} + P}{100+xR} \left(\frac{T + \frac{t}{x}\left(0.83+\frac{H}{100}\right)}{T - t\left(0.83 + \frac{H}{100}\right)\frac{R}{100}}\right)
\end{equation}

\subsection{No Nature's Grace}
To slightly generalize these to other classes, we can remove Nature's Grace from the equations by setting the casting time reduction from a crit to zero. That is, by setting $t=0$. Note that the equations were already factorized to make the impact of Nature's Grace apparent. Upon doing so, we get the following stat weights, which should be applicable to other classes.

\begin{equation}
\nonumber
\textrm{SpellpowerWeight} = 1
\end{equation}
\begin{equation}
\nonumber
\textrm{HitWeight} = \frac{\frac{mB}{c} + P}{83 + H}
\end{equation}
\begin{equation}
\nonumber
\textrm{CritWeight} = x\frac{\frac{mB}{c} + P}{100 + xR}
\end{equation}

\end{document}