\lab{Modelling the spread of an epidemic: SIR models}{Modelling the spread of an epidemic: SIR models}
\label{lab:SIRModels}

% Many industry grade ode solvers are similar to the RK4 method already described. A common, reliable method that is a good choice for initially studying most problems is the Dormand-Prince method. This method is implemented in Python's \li{scipy.integrate} module as \li{dopri5}, and in MatLab as \li{ode45}. A similar method is the Runge-Kutta-Fehlberg method (RKF45).
%
% The Dormand-Prince method is a Runge-Kutta method that computes a fourth order accurate solution, followed by a fifth order accurate solution. These solutions are used to estimate the error in the fourth order solution. In turn, the estimated error is used to help determine the size of each step $h_i = t_i-t_{i-1}$ used by the method, instead of used a fixed stepsize $h = (b-a)/n$.
%
% We will demonstrate how to solve the initial value problem
% \begin{align*}
% y'(t) &= 6+2t-y, \\
% y(0) &= 2,
% \end{align*}
% using \li{dopri5}. We start with importing several useful modules and defining the ode.
% The \li{ode} class is imported from the \li{scipy.integrate} module.
% This class functions as an interface to several numerical ode methods, one of which is \li{dopri5}.
% These other methods can be useful in certain situations; however, \li{dopri5} is a good solver to try on new problems.
%
%
%
% We create an instance of the \li{ode} class and initialize it using the \li{set_integrator} and \li{set_initial_value} methods.
% Useful parameters are \li{atol} and \li{rtol}, which set the maximum allowed absolute and relative tolerances for the solution.
% Other parameters and methods are explained in the documentation for \li{scipy}.
% Here the method solves for $y(1.6)$:
%
% \begin{lstlisting}
% from scipy.integrate import ode
% import numpy as np
% import matplotlib.pyplot as plt
%
% a, ya, b = 0., 2., 1.6
% def ode_f(t,y): return np.array([-1.*y+6.+2.*t])
%
% ode_object = ode(ode_f)
% ode_object.set_integrator('dopri5',atol=1e-5)
% ode_object.set_initial_value(ya,a)
% print ode_object.integrate(b)
% \end{lstlisting}
%
% Alternatively, let us solve for $y$ on a evenly spaced mesh, and then plot the results.
% %# The output of this function must have the shape (dim,), where dim
% %# is the dimension of the system.
% \begin{lstlisting}
% ode_object = ode(ode_f).set_integrator('dopri5',atol=1e-5)
% ode_object.set_initial_value(ya,a)
%
% dim, t = 1, np.linspace(a,b,51)
% Y = np.zeros((len(t),dim))
% Y[0,:] = ya
% for j in range(1,len(t)): Y[j,:] = ode_object.integrate(t[j])
%
% plt.plot(t,Y[:,0],'-k')
% plt.show()
% \end{lstlisting}
%
% \begin{figure}[ht]
% \centering
% \includegraphics[width=\textwidth]{Example1.pdf}
% \caption{The solution of $y'(t) = 6+2t-y$, $y(0) = 2$, on the interval $[0,1.6]$, using the solver \li{dopri5} from \li{scipy.integrate}.}
% \label{sir:example1}
% \end{figure}
%
%
% \begin{problem}
% Using \li{dopri5}, solve the IVP
% \begin{align*}
% 5y''' + y'+2y &= 0, \,\, 0 \leq x \leq 16,\\
% y(0) &=0,\\
% y'(0) &= 1,\\
% y''(0) &= -2.
% \end{align*}
% \end{problem}
%
% Another good ode solver to try is \li{odeint}, also in \li{scipy.integrate}. \li{odeint} is a Python wrapping for the function \li{lsoda} from the Fortran library \li{odepack}. One of the nice features of \li{lsoda} is that it automatically switches between stiff and nonstiff solvers depending on the behavior of the problem.
%
%
% \section*{The SIR Model}
The SIR model describes the spread of an epidemic throughout a large population.
It does this by describing the movement of the population through three phases of the disease: those individuals who are \emph{susceptible}, those who are \emph{infectious}, and those who have been \emph{removed} from the disease.
Those individuals in the removed class have either died, or have recovered from the disease and are now immune to it.
If the outbreak occurs over a short period of time, we may reasonably assume that the total population is fixed, so that $S'(t) + I'(t) + R'(t) = 0$.
We may also assume that $S(t) + I(t) + R(t) = 1$, so that $S(t)$ represents the \textit{fraction} of the population that is susceptible, etc.

Individuals may move from one class to another as described by the flow
\[S \to I \to R.\]
Let us consider the transition rate between $S$ and $I $.
Let $\beta$ represent the average number of contacts made per day that could spread the disease.
The proportion of these contacts that are with a susceptible individual is $S(t)$.
Thus, one infectious individual will on average infect $\beta S(t)$ others per day.
Let $N$ represent the total population size.
Then we obtain the differential equation
\[\frac{d}{dt}(S(t) N) = -\beta S(t) (I(t) N)\]

 Now consider the transition rate between $I$ and $R$.
We assume that there is a fixed proportion $\gamma$ of the infectious group who will recover on a given day, so that
\[\frac{d}{dt}R(t) = -\gamma I(t).\]
Note that $\gamma$ is the reciprocal of the average length of time spent in the infectious phase.

Since the derivatives sum to $0$, we have $I'(t) = - S'(t) - R'(t)$, so the  differential equations are given by
\begin{align*}
\frac{dS}{dt} &=-\beta IS ,\\
\frac{dI}{dt} &= \beta I S-\gamma I,\\
\frac{dR}{dt} &=\gamma I.
\end{align*}

\begin{problem}
Solve the IVP
\begin{align*}
\frac{dS}{dt} &=-\frac{1}{2} IS ,\\
\frac{dI}{dt} &= \frac{1}{2} I S-\frac{1}{4} I, \\
\frac{dR}{dt} &=\frac{1}{4} I,\\
S(0) &= 1-6.25\cdot10^{-7},\\
I(0) &= 6.25\cdot10^{-7},\\
R(0) &=0,
\end{align*}
on the interval $[0,100]$. \label{prob_sir1}
\end{problem}

\begin{figure}[ht]
\centering
\includegraphics[width=\textwidth]{SIR1.pdf}
\caption{Solution to Problem (\ref{prob_sir1})}
\label{sir1}
\end{figure}

\begin{problem}
Suppose that, in a city of approximately three million, five have recently entered the city carrying a certain disease.
(Suppose they have just entered the infectious state.)

Each of those individuals has a contact each day that could spread the disease, and an average of three days is spent in the infectious state.
Find the solution of the corresponding SIR equations for the next fifty days.

At the peak of the infection, how many in the city will still be able to work? (Assume for simplicity that those who are in the infectious state either cannot go to work or are unproductive, etc.)
Answer the same question if instead of three days, an average of seven days is spent in the infectious state.
\end{problem}

\begin{problem}
Suppose that, in a city of approximately three million, five have recently entered the city carrying a certain disease.
(Suppose they have just entered the infectious state.)

Each of those individuals will make three contacts every ten days that could spread the disease, and an average of four days is spent in the infectious state.
Find the solution of the corresponding SIR equations.
Choose an appropriate time interval and plot your results.
\end{problem}

\section*{Variations on the SIR Model}

SIS Models describe diseases where individuals who have recovered from the disease do not gain
any lasting immunity.
There are only two compartments in this model: those who are \emph{susceptible}, and those who are \emph{infectious}.

The basic equations are given by
\begin{align*}
\frac{dS}{dt} &=-\beta I S + \gamma I ,\\
\frac{dI}{dt} &= \beta I S-\gamma I
\end{align*}

If we add to our basic SIR model to account for the death rate and an equal birth rate, the equations become
\begin{align*}
\frac{dS}{dt} &=\mu(1 -S) - \beta I S,\\
\frac{dI}{dt} &= \beta I S - (\gamma + \mu)I, \\
\frac{dR}{dt} &= \gamma I - \mu R
\end{align*}

SIRS models take the previous model and allow the transfer of individuals from the recovered/removed class to rejoin the susceptible class.
\begin{align*}
\frac{dS}{dt} &= fR + \mu(1 -S) - \beta I S,\\
\frac{dI}{dt} &= \beta I S - (\gamma + \mu)I, \\
\frac{dR}{dt} &= -fR + \gamma I - \mu R.
\end{align*}

The next exercise uses a variation of the basic SIR model to describe the spread of measles.
It assumes that the rate at which measles is contracted depends on the season, i.e. the rate is periodic.
That allows us to formulate the yearly occurrence rate for measles as a boundary value problem.
To solve this problem we will use a full-featured BVP solver that is available as a Python package.
Several industrial-grade BVP solvers have been written in Fortran.
One of these, \li{bvp_solver}, has been wrapped for Python and is available as a scikit.
If you have not installed it, you can install it by running the command \li{pip install scikits.bvp_solver} in the command line.
The code below demonstrates how to use \li{bvp_solver} to solve the BVP
\begin{align*}
	\epsilon y'' + yy' - y &= 0, \quad y(-1) = 1, \quad y(1) = -1/3.
\end{align*}

\begin{lstlisting}
import numpy as np
from scikits import bvp_solver
import matplotlib.pyplot as plt

epsilon, lbc, rbc = .1, 1., - 1. / 3.

def ode(x , y):
    return np.array([y[1] , (1. / epsilon) * (y[0] - y[0] * y[1])])

# The BVP solver package expects you to pass it the boundary
# conditions as a callable function that computes the difference
# between a guess at the boundary conditions
# and the desired boundary conditions.
# When we use the BVP solver, we will tell it how many constraints
# there should be on each side of the domain so that it knows
# how many entries to expect in the tuples BCa and BCb.
# In this case, we have one boundary condition on either side.
# These constraints are expected to evaluate to 0 when the
# boundary condition is satisfied.
def bcs(ya, yb):
    BCa = np.array([ya[0] - lbc])   # 1 Boundary condition on the left
    BCb = np.array([yb[0] - rbc])   # 1 Boundary condition on the right
    return BCa, BCb

problem = bvp_solver.ProblemDefinition(num_ODE=2,
                                     num_parameters=0,
                                     num_left_boundary_conditions=1,
                                     boundary_points=(-1, 1),
                                     function=ode,
                                     boundary_conditions=bcs)

solution = bvp_solver.solve(problem, solution_guess=(- 1. / .3, - 4. / 3.))

A = np.linspace(-1., 1., 200)
T = solution(A)
plt.plot(A, T[0,:], '-k', linewidth=2.)
plt.show()
\end{lstlisting}

\begin{figure}[ht]
\centering
\includegraphics[width=\textwidth]{measles.pdf}
\caption{Solution to Problem (\ref{prob:sir_measles})}
\label{fig:sir4}
\end{figure}

\begin{problem}
SEIR models are another variation of the basic SIR model.
Basically they added another compartment, called the \emph{exposed} or \emph{latency} phase, to the basic compartments \emph{susceptible}, \emph{infectious}, and \emph{recovered}.

An SEIR model is used to describe the spread of measles (see \footnote{Numerical Solution of Boundary Value Problems for Ordinary Differential Equations, by Aescher, Mattheij, and Russell}).
The rate at which susceptible individuals may contract measles is seasonal, and corresponds to a periodic function $\beta(t) = \beta_0(1 + \beta_1 \cos{2\pi t})$.
Parameters $\mu$ and $\lambda$ represent the birth rate of the population and the latency period of measles, respectively.
$\eta$ represents the infectious period before an individual moves from the infectious class to the recovered class.
After recovery an individual remains immune.
The boundary value problem is given by
\begin{align*}
\left[\begin{array}{c}S \\ E \\ I\end{array}\right]' &= \left[\begin{array}{c}\mu - \beta(t) S I \\\beta(t) SI - E/\lambda \\E/\lambda - I/\eta\end{array}\right],\\
S(0) &= S(1),\\
E(0) &= E(1),\\
I(0) &= I(1)
% \left[\begin{array}{c}S \\E \\I\end{array}\right](0) &= \left[\begin{array}{c}S \\E \\I\end{array}\right](1).
\end{align*}
Solve this BVP with parameters $\beta_1 = 1,$ $\beta_0 = 1575,$ $\eta = 0.01,$ $\lambda = .0279,$ and $\mu = .02.$
Note: in this case, time is measured in years, so run the solution over the interval $\left[0, 1\right]$ to show a one-year cycle.
The boundary conditions are really just saying that the year will begin and end in the same state.

Hint: \li{bvp_solver} requires \emph{separated boundary conditions}.
In other words, each equation in the set of boundary conditions can only include values at one end of the interval.
To deal with this, let $C = [C_1, C_2, C_3]$, and add the equation
\[C' = 0\]
to the system of ODEs given above (for a total of 6 equations).
Then the boundary conditions can be separated using the following trick:
\begin{align*}
	\begin{pmatrix}C_1(0) \\C_2(0) \\ C_3(0) \end{pmatrix} &= \begin{pmatrix}S(0) \\E(0) \\ I(0) \end{pmatrix}, \quad 	\begin{pmatrix}C_1(1) \\C_2(1) \\ C_3(1) \end{pmatrix} = \begin{pmatrix}S(1) \\E(1) \\ I(1) \end{pmatrix}.
\end{align*}
Now $C_1,C_2,C_3$ become the 4th, 5th, and 6th rows of your solution matrix, so the 3 boundary conditions for the left are obtained by subtracting the last three entries of $y(0)$ from the first three entries. Similarly, your right boundary conditions will look like $yb[0:3]-yb[3:]$.

%This translates to 3 boundary conditions for the left and 3 for the right.  Now $C_1,C_2,C_3$ become the 4th, 5th, and 6th rows of your solution matrix.
%The formulae you will want to use for the boundary conditions can be formed by subtracting one side of these equations from the other. That is, subtract the last 3 entries of $y(a)$ from the %first 3 entries for the left boundary conditions, and similarly for the right.
When you code your boundary conditions, note that \li{bvp_solver} changes the initial conditions to force all the entries in the two arrays to be zero.
You can use the initial conditions from Fig.~\ref{fig:sir4} as your initial guess (which will be an array of 6 elements). Remember that the initial infected proportion is small, not 0.

\label{prob:sir_measles}
\end{problem}


% main.tex
\documentclass{report}
\usepackage{amsmath}
\usepackage{graphicx}
\graphicspath{{./figures/}}
\usepackage{subfigure}
\begin{document}
\chapter{Introduction}\label{ch:intro}
Hello
\section{Euler}\label{sec:euler}
\begin{equation}\label{eq:euler}
e^{i\pi}+1=0
\end{equation}
Equation~\eqref{eq:euler} is a beauty.

\begin{equation}\label{eq:einstein}
E = mc^2 \tag{*}
\end{equation}
Equation~\eqref{eq:einstein} is not bad either.

\begin{equation}\label{eq:gaslaw}
PV = nRT
\end{equation}
Equation~\eqref{eq:gaslaw} is a classic.

\begin{figure}[ht]
\centering
\subfigure[Union]{\includegraphics[scale=0.25]{AcupB}\label{fig:union}}
\subfigure[Intersection]{\includegraphics[scale=0.25]{AcapB}\label{fig:intersection}}
\subfigure[Complement]{\includegraphics[scale=0.25]{Acomp}\label{fig:complement}}
\caption{Three figures using \texttt{subfigure}.\label{fig:setops-subfig}}
\end{figure}

Goodbye
\end{document}


\chapter{Some applications}
With all this setup, we now take the time to develop some nice
results which are of independent interest.

\section{Frobenius divisibility}
\begin{theorem}
	[Frobenius divisibility]
	Let $V$ be a complex irrep of a finite group $G$.
	Then $\dim V$ divides $|G|$.
\end{theorem}
The proof of this will require algebraic integers
(developed in the algebraic number theory chapter).
Recall that an \emph{algebraic integer} is a complex number
which is the root of a polynomial with integer coefficients,
and that these algebraic integers form a ring $\ol\ZZ$
under addition and multiplication, and that $\ol\ZZ \cap \QQ = \ZZ$.

First, we prove:
\newcommand{\tempuuujbhtkx}{$\ZZ[G]$}
\begin{lemma}[Elements of \tempuuujbhtkx\ are integral]
	\label{lem:group_ring_integral}
	Let $\alpha \in \ZZ[G]$.
	Then there exists a monic polynomial $P$ with integer coefficients
	such that $P(\alpha) = 0$.
\end{lemma}
\begin{proof}
	Let $A_k$ be the $\ZZ$-span of $1, \alpha^1, \dots, \alpha^k$.
	Since $\ZZ[G]$ is Noetherian,
	the inclusions $A_0 \subseteq A_1 \subseteq A_2 \subseteq \dots$
	cannot all be strict, hence $A_k = A_{k+1}$ for some $k$,
	which means $\alpha^{k+1}$ can be expressed in terms of
	lower powers of $\alpha$.
\end{proof}

\begin{proof}
	[Proof of Frobenius divisibility]
	Let $C_1$, \dots, $C_m$ denote the conjugacy classes of $G$.
	Then consider the rational number \[ \frac{|G|}{\dim V}; \]
	we will show it is an algebraic integer, which will prove the theorem.
	Observe that we can rewrite it as
	\[
		\frac{|G|}{\dim V}
		= \frac{|G| \left< \chi_V, \chi_V \right>}{\dim V}
		= \sum_{g \in G} \frac{\chi_V(g) \ol{\chi_V(g)}}{\dim V}.
	\]
	We split the sum over conjugacy classes, so
	\[
		\frac{|G|}{\dim V}
		=
		\sum_{i=1}^m \ol{\chi_V(C_i)} \cdot \frac{|C_i| \chi_V(C_i)}{\dim V}.
	\]
	We claim that for every $i$,
	\[ \frac{|C_i| \chi_V(C_i)}{\dim V}
		= \frac{1}{\dim V} \Tr T_i \]
	is an algebraic integer,
	where \[ T_i \defeq \rho\left(\sum_{h \in C_i} h\right). \]
	To see this, note that $T_i$ commutes with elements of $G$,
	and hence is an intertwining operator $T_i : V \to V$.
	Thus by Schur's lemma, $T_i = \lambda_i \cdot \id_V$
	and $\Tr T = \lambda_i \dim V$.
	By \Cref{lem:group_ring_integral}, $\lambda_i \in \ol\ZZ$, as desired.

	Now we are done, since $\ol{\chi_V(C_i)} \in \ol\ZZ$ too
	(it is the sum of conjugates of roots of unity),
	so $\frac{|G|}{\dim V}$ is the sum of products of algebraic integers,
	hence itself an algebraic integer.
\end{proof}

\section{Burnside's theorem}
We now prove a group-theoretic result.
This is the famous poster child for representation theory
(in the same way that RSA is the poster child of number theory)
because the result is purely group theoretic.

Recall that a group is \vocab{simple} if it has no normal subgroups.
In fact, we will prove:
\begin{theorem}[Burnside]
	Let $G$ be a nonabelian group of order $p^a q^b$ (where $p,q$ are distinct primes and $a,b \ge 0$).
	Then $G$ is not simple.
\end{theorem}
In what follows $p$ and $q$ will always denote prime numbers.

\begin{lemma}[On $\gcd(|C|, \dim V) = 1$]
	\label{lem:burnside_ant_lemma}
	Let $V = (V, \rho)$ be an complex irrep of $G$.
	Assume $C$ is a conjugacy class of $G$ with $\gcd(|C|, \dim V) = 1$.
	Then for any $g \in C$, either
	\begin{itemize}
		\ii $\rho(g)$ is multiplication by a scalar, or
		\ii $\chi_V(g) = \Tr \rho(g) = 0$.
	\end{itemize}
\end{lemma}
\begin{proof}
	If $\eps_i$ are the $n$ eigenvalues of $\rho(g)$ (which are roots of unity),
	then from the proof of Frobenius divisibility we know
	$\frac{|C|}{n} \chi_V(g) \in \ol\ZZ$,
	thus from $\gcd(|C|, n) = 1$ we get
	\[ \frac1n \chi_V(g) = \frac1n(\eps_1 + \dots + \eps_n) \in \ol\ZZ. \]
	So this follows readily from a fact from algebraic number theory,
	namely \Cref{prob:rep_lemma}:
	either $\eps_1 = \dots = \eps_n$ (first case) or
	$\eps_1 + \dots + \eps_n = 0$ (second case).
\end{proof}

\begin{lemma}
	[Simple groups don't have prime power conjugacy classes]
	Let $G$ be a finite simple group.
	Then $G$ cannot have a conjugacy class of order $p^k$ (where $k > 0$).
\end{lemma}
\begin{proof}
	By contradiction.
	Assume $C$ is such a conjugacy class, and fix any $g \in C$.
	By the second orthogonality formula (\Cref{prob:second_orthog})
	applied $g$ and $1_G$ (which are not conjugate since $g \neq 1_G$) we have
	\[ \sum_{i=1}^r \dim V_i \chi_{V_i}(g) = 0 \]
	where $V_i$ are as usual all irreps of $G$.
	\begin{exercise}
		Show that there exists a nontrivial irrep $V$
		such that $p \nmid \dim V$ and $\chi_V(g) \neq 0$.
		(Proceed by contradiction to show that $-\frac1p \in \ol\ZZ$ if not.)
	\end{exercise}
	Let $V = (V, \rho)$ be the irrep mentioned.
	By the previous lemma, we now know that $\rho(g)$ acts as a scalar in $V$.

	Now consider the subgroup
	\[ H = \left< ab\inv \mid a,b \in C \right> \subseteq G. \]
	We claim this is a nontrivial normal subgroup of $G$.
	It is easy to check $H$ is normal,
	and since $|C| > 1$ we have that $H$ is nontrivial.
	As represented by $V$ each element of $H$ acts trivially in $G$,
	so since $V$ is nontrivial and irreducible, $H \neq G$.
	This contradicts the assumption that $G$ was simple.
\end{proof}

With this lemma, Burnside's theorem follows by partitioning
the $|G|$ elements of our group into conjugacy classes.
Assume for contradiction $G$ is simple.
Each conjugacy class must have order either $1$ (of which there are $|Z(G)|$ by \Cref{prob:class_eq})
or divisible by $pq$ (by the previous lemma), but on the other hand the sum equals $|G| = p^aq^b$.
Consequently, we must have $|Z(G)| > 1$.
But $G$ is not abelian, hence $Z(G) \neq G$,
thus the center $Z(G)$ is a nontrivial normal subgroup,
contradicting the assumption that $G$ was simple.

\section{Frobenius determinant}
We finish with the following result,
the problem that started the branch of representation theory.
Given a finite group $G$,
we create $n$ variables $\{x_g\}_{g \in G}$,
and an $n \times n$ matrix $M_G$ whose $(g,h)$th entry is $x_{gh}$.
\begin{example}[Frobenius determinants]
	\listhack
	\begin{enumerate}[(a)]
	\ii If $G = \Zc 2 = \left< T \mid T^2 = 1\right>$
	then the matrix would be \[ M_G =
	\begin{bmatrix} x_{\id} & x_T \\ x_T & x_{\id} \end{bmatrix}. \]
	Then $\det M_G = (x_{\id}-x_T)(x_{\id}+x_T)$.
	\ii If $G = S_3$, a long computation gives
	the irreducible factorization of $\det M_G$ is
	\[
		\left( \sum_{\sigma \in S_3} x_{\sigma} \right)
		\left( \sum_{\sigma \in S_3} \sign(\sigma)x_\sigma \right)
		\Big( F\left(x_\id, x_{(123)}, x_{(321)}\right)
		- F\left(x_{(12)}, x_{(23)}, x_{(31)}\right) \Big)^2 \]
	where $F(a,b,c) = a^2+b^2+c^2-ab-bc-ca$;
	the latter factor is irreducible.
	\end{enumerate}
\end{example}
\begin{theorem}
	[Frobenius determinant]
	The polynomial $\det M_G$ (in $|G|$ variables) factors
	into a product of irreducible polynomials such that
	\begin{enumerate}[(i)]
		\ii The number of polynomials equals the number
		of conjugacy classes of $G$, and
		\ii The multiplicity of each polynomial
		equals its degree.
	\end{enumerate}
\end{theorem}
You may already be able to guess how the ``sum of squares'' result
is related! (Indeed, look at $\deg\det M_G$.)

Legend has it that Dedekind observed this behavior first in 1896.
He didn't know how to prove it in general,
so he sent it in a letter to Frobenius,
who created representation theory to solve the problem.

With all the tools we've built, it is now fairly straightforward
to prove the result.

\begin{proof}
	Let $V = (V, \rho) = \Reg(\CC[G])$ and let $V_1$, \dots, $V_r$
	be the irreps of $G$.
	Let's consider the map $T \colon \CC[G] \to \CC[G]$
	which has matrix $M_G$ in the usual basis of $\CC[G]$, namely
	\[ T : T(\{x_g\}_{g \in G}) = \sum_{g \in G} x_g \rho(g) \in \Mat(V). \]
	Thus we want to examine $\det T$.

	But we know that $V = \bigoplus_{i=1}^r V_i^{\oplus \dim V_i}$
	as before, and so breaking down $T$ over its subspaces we know
	\[
		\det T
		= \prod_{i=1}^r \left( \det (T \restrict{V_i})  \right)^{\dim V_i}.
	\]
	So we only have to show two things:
	the polynomials $\det T_{V_i}$ are irreducible,
	and they are pairwise different for different $i$.

	Let $V_i = (V_i, \rho)$, and pick $k = \dim V_i$.
	\begin{itemize}
		\ii \emph{Irreducible}:
		By the density theorem, for any $M \in \Mat(V_i)$ there exists
		a \emph{particular} choice of complex numbers $x_g \in G$ such that
		\[
			M = \sum_{g \in G} x_g
			\cdot \rho_i(g)
			= (T \restrict{V_i})(\{x_g\}).
		\]
		View $\rho_i(g)$ as a $k \times k$ matrix with complex coefficients.
		Thus the ``generic'' $(T \restrict{V_i})(\{x_g\})$, viewed as a matrix with
		polynomial entries, must have linearly independent entries
		(or there would be some matrix in $\Mat(V_i)$ that we can't achieve).

		Then, the assertion follows (by a linear variable change)
		from the simple fact that the polynomial
		$\det (y_{ij})_{1 \le i, j \le m}$ in $m^2$ variables
		is always irreducible.

		\ii \emph{Pairwise distinct}:
		We show that from $\det T|_{V_i}(\{x_g\})$ we can read
		off the character $\chi_{V_i}$, which proves the claim.
		In fact
		\begin{exercise}
			Pick \emph{any} basis for $V_i$.
			If $\dim V_i = k$, and $1_G \neq g \in G$, then
			\[
				\chi_{V_i} (g)
				\text{ is the coefficient of } x_g x_{1_G}^{k-1}.
			\]
		\end{exercise}
		Thus, we are done. \qedhere
	\end{itemize}
\end{proof}


\subsection{Selection}

The selection operator describes the process by which individuals are chosen
from the current population to generate the next. Almost always, the likelihood
of an individual being selected is determined by their fitness. This is because
the purpose of selection is to preserve favourable qualities and encourage some
homogeneity within future generations~\cite{Back1994}.

\inputtikz[.8\imgwidth]{selection}{%
    The selection process with the inclusion of some lucky individuals.
}
\input{tex/algorithms/selection.tex}

In EDO, a modified truncation selection method is used~\cite{Jebari2013}, as can
be seen in Figure~\ref{fig:selection}. Truncation selection takes a fixed
number, \(n_b = \lceil bN\rceil\), of the fittest individuals in a population
and makes them the `parents' of the next. It has been observed that, despite
its efficiency as a selection operator, truncation selection can lead to
premature convergence at local optima~\cite{Jebari2013, Tatsuya2002}. The
modification for EDO is an optional stage after the best individuals have been
chosen: with some small \(l\), a number, \(n_l = \lceil lN\rceil\), of the
remaining individuals can be selected at random to be carried forward. Hence,
allowing for a small number of randomly selected individuals may encourage
diversity and further exploration throughout the run of the algorithm. It should
be noted that regardless of this step, an individual could potentially be
present throughout the entirety of the algorithm.

After the parents have been selected, there are two adjustments made to the
current search space. The first is that the subtypes for each family in
\(\mathcal{P}\) are updated to only those present in the parents. The second
adjustment is a process which acts on the distribution parameter limits for
each subtype in \(\mathcal{P}\). This adjustment gives the ability to `shrink'
the search space about the region observed in a given population. This method is
based on a power law described in~\cite{Amirjanov2016} that relies on a shrink
factor, \(s\). At each iteration, \(t\), every distribution subtype which is
present in the parents has its parameter's limits, \(\left(l_t, u_t\right)\),
adjusted. This adjustment is such that the new limits, \(\left(l_{t+1},
u_{t+1}\right)\) are centred about the mean observed value, \(\mu\), for that
parameter:
\begin{align}
    \label{eq:shrinking_lower}
    l_{t+1}&= \max \left\{l_t, \ \mu - \frac{1}{2} (u_t - l_t) s^t\right\}\\
    \label{eq:shrinking_upper}
    u_{t+1}&= \min \left\{u_t, \ \mu + \frac{1}{2} (u_t - l_t) s^t\right\}
\end{align}

The shrinking process is given explicitly in
Algorithm~\ref{algorithm:shrinking}. Note that the behaviour of this process can
produce reductive results for some use cases and is optional.

\input{tex/algorithms/shrinking.tex}


\subsection{Latent variable models for genomics}

With the exponential growth in the use of high-throughput genomics, biological datasets are increasingly high dimensional, both in terms of samples and features. A key principle of biological datasets is that variation between the features results from differences in underlying, often unobserved, processes. Such processes, whether driven by biological or technical effects, are manifested by coordinated changes in multiple features. This key assumption sets off an entire statistical framework of exploiting the redundancy encoded in the data set to learn the (latent) sources of variation in an unsupervised fashion. This is the aim of dimensionality reduction techniques, or latent variable models \cite{Komili2008, Stegle2012, Leek2007, Pournara2007, Dai2017, Genevieve2018, Meng2016}.

\subsubsection{Mathematical formulation}

Given a dataset $\bfY$ of $N$ samples and $D$ features, latent variable models attempt to exploit the dependencies between the features by reducing the dimensionality of the data to a potentially small set of $K$ latent variables, also called factors. The mapping between the low-dimensional space and the high-dimensional space is performed via a function $f(\bfX|\bTheta)$ that depends on some parameters $\bTheta$.\\
The choice of $f(\bfX|\bTheta)$ is essentially the field of dimensionality reduction. A trade-off exists between complexity and interpretation: while non-linear functions such as deep neural networks provide more explanatory power, this leads to considerable challenges in interpretation \cite{Zhang2018_NN}. Hence, for most applications where interpretability is essential, $f(\bfX|\bTheta)$ is assumed to be linear:
% \begin{equation}
% 	y_{nd} = \sum_{k=1}^{K} w_{dk}z_{n,k}
% \end{equation}
\begin{equation} \label{eq:linear_model}
	\mathbf{Y} = \mathbf{Z}\mathbf{W}^{T}
\end{equation}
where $\bfZ \in \R^{N \times K}$ is a matrix that contains the low-dimensional representation for each sample (i.e. the factors). The matrix $\bfW \in \R^{D \times K}$ contains the weights, which provide the linear mapping between the features and the factors.\\
Note that the aim of dimensionality reduction is to exploit the coordinated heterogeneity between features, and hence features can be assumed to be centered without loss of generality.

The inference procedure consists in learning the values of all unobserved variables, including factors and weights. As we shall demonstrate, different inference schemes and assumptions on the prior distributions lead to significantly different model outputs \cite{Rattray2009}.

\subsection{Principal Component Analysis} \label{section:pca}

Principal Component Analysis (PCA) is the most popular technique for dimensionality reduction \cite{Hotelling1933,Ringner2008}.Two formulations of PCA exist \cite{Bishop2006}: in the maximum variance formulation, the aim is to infer an orthogonal projection of the data onto a low-dimensional space such that variance explained by the projected data is maximised. Formally, the aim in PCA is to infer the matrix $\bfW$ such that the variance of $\bfZ$ (the projected data) is maximised. If we consider a single latent factor, the variance of the projected data is:
\begin{equation*}
	\sigma^2 = \frac{1}{N}\sum_{n=1}^{N} (\bfz_n - \hat{\bfz})^{2} = \frac{1}{N}\sum_{n=1}^{N} (\bfy_n^T \bfw - \hat{\bfy}^T\bfw)^{2}
\end{equation*}

where $\hat{\bfy}$ is a vector with the feature-wise means. If we assumed centered data this simplifies to:
\begin{equation*}
	\sigma^2 = \frac{1}{N}\sum_{n=1}^{N} (\bfy_n^T \bfw)^{2}
\end{equation*}
Some algebra allows us to define this equation in terms of the (centered) data covariance matrix: $\bfS = \frac{1}{N}\sum_{n=1}^{N} \bfy_n\bfy_n^T$:
\begin{align*}
	\sigma^2 &= \frac{1}{N}\sum_{n=1}^{N} (\bfy_n^T \bfw)^T (\bfy_n^T \bfw) \\
	=& (\bfw^T \bfy_n) (\bfy_n^T \bfw) \\
	=& \bfw^T (\bfy_n \bfy_n^T) \bfw \\
	=& \bfw^T \bfS \bfw
\end{align*}

Thus, for a single principal component, the optimisation problem is:
\begin{equation} \label{eq:pca}
	%\argmax_{\|\bfw\|=1} & \sum_{n=1}^{N} (\bfw_1^T \bfy_n)^2 = \\
	\argmax_{\|\bfw\|=1} = \bfw^T \bfS \bfw
\end{equation}

% where $\bfY^T \bfY=\bfS \in \R^{D \times D}$ is the data covariance matrix and $\bfw_1^T$ is the vector of weights. \\
The $k$-th principal component can be found by subtracting from $\bfY$ the reconstructed data by the previous $k-1$ principal components. If we define $\bfz_k=\bfw_k^T \bfY$ to be the $k$-th principal component:
\[
	\hat{\bfY} = \bfY - \sum_{k=1}^{K} (\bfz_k \bfw_k^T)
\]
Re-applying \Cref{eq:pca} defines the new optimisation problem.

In its minimum error formulation, the aim is to find an equivalent projection that minimises the mean squared error between the observations and the data reconstructed using the principal components:
\[
	\argmax_{\|\bfw\|=1} \Vert \bfY - \sum_{k=1}^{K} (\bfz_k \bfw_k^T) \Vert^{2}
\]
where $\Vert \cdot \Vert^{2}$ is the Frobenius norm.

Remarkably, in both cases, solving the optimisation problems via Lagrange multipliers leads to master eigenvalue-eigenvector equation:
\begin{equation}
	\bfS \bfw_k = \lambda_k \bfw_k
\end{equation}
where the weight vectors $\bfw_k$ can be calculated as the eigenvectors of the covariance matrix $\bfS$ \cite{Bishop2006}.

Interestingly, the reason why the maximum variance solution and the minimum reconstruction error solution are the same can be understood by applying Pythagoras theorem to the right triangle defined by the projection of a sample $\bfy_{n}$ to a weight vector $\bfw$ (\Cref{fig:pca2}).
Assuming again centered data, the variance of $\bfy_{n}$ is $\|\bfy_{n}\| = \bfy_{n}^T \bfy_{n}$. This variance decomposes as the sum of the variance in the latent space $\|\bfz_{n}\| = \bfz_{n}^T \bfz_{n}$ and the residual variance after reconstruction $\|\bfy_{n} - \bfz_{n} \bfw^T \|$:

\begin{figure}[H]
	\centering
	\includegraphics[width=0.8\linewidth]{pca2}
	\caption[Maximizing the variance in the principal component space is equivalent to minimizing the data reconstruction error]{In the maximum variance formulation the aim is to maximise the variance of the projected data (blue line), whereas in the minimum error formulation the aim is to minimise the residual variance (red line). Given a fixed total variance (black line), both strategies are equivalent}
	\label{fig:pca2}
\end{figure}

The main strength of PCA relies on its simplicity and closed form solution. Additionally, the linear mapping has the advantage of yielding interpretable feature weights, so that inspection of $\bfw_k$ reveals which features are jointly affected by the $k$-th principal component.\\
However, PCA suffers from serious drawbacks when applying it to real datasets \cite{Li2017b}. First, biological measurements are inherently noisy, and there is no explicit account of noise in PCA. In practice, high variance components are often asociated with signal whereas low-variance components are assumed to be noise, but an ideal model should explicitly disentangle the uncoordinated variability that is attributed to noise from the coordinated variability that is characterised as signal. Second, in its original formulation, no missing data is allowed \cite{Ilin2010}. Third, it does not offer a principled way of modelling prior information about the data.

\subsection{Probabilistic Principal Component Analysis and Factor Analysis} \label{section:probabilistic_pca}
A probabilistic version of PCA was initially proposed in \cite{Tipping1999}. It can be formulated by converting some (or all) fixed parameters into random variables and adding an explicit noise term to \Cref{eq:linear_model}:
\begin{equation}
\mathbf{Y} = \mathbf{Z}\mathbf{W}^{T}  + \bepsilon
\end{equation}
where the weights $\bfW$ are assumed to be non-probabilistic parameters, but the noise $\bepsilon$ and the latent variables $\bfZ$ (the principal components) are assumed to follow an isotropic normal distribution:
\begin{align*}
	p(\bfZ) &= \prod_{n=1}^{N} \prod_{k=1}^{K} \Ndist{z_{nk}}{0,1} \\
	p(\epsilon) &= \Ndist {\epsilon}{0,\tau^{-1}}
\end{align*}
where $\tau$ is the precision (inverse of the variance).
%The alternative strategy of treating $\bfZ$ as random variables and $\bfW$ as parameters has also been explored \cite{Lawrence2005}.\\

All together, this leads to a Gaussian likelihood:
\begin{equation} \label{eq:ppca_lik}
	% p(\bfY|\bfW,\bfZ,\sigma) = \Ndist{\bfY}{\bfW \bfZ,\sigma^2 \I}
	p(\bfY|\bfZ,\bfW,\tau) = \prod_{n=1}^{N} \prod_{d=1}^{D} \Ndist{y_{n,d}}{\bfz_{n,:} \bfw_{,:k},\tau^{-1} \I}
\end{equation}

The corresponding graphical model is:
\begin{figure}[H]
	\centering
	\input{graphical_models/pPCA}
	\caption{\textbf{Graphical model for probabilistic PCA}. The latent variables are modelled as random variables, whereas the weights and the noise are modelled as deterministic parameters.}
	\label{fig:pPCA}
\end{figure}

Importantly, the choice of the distribution for $\epsilon$ implies that the noise of each feature is independent but restricted to have the same precision $\tau$. In practice this is a limiting assumption, as different features are expected to show different degrees of noise, albeit this constraint can be relaxed and forms the basis of Factor Analysis \cite{Rubin1982,Bishop2006}.

The inference procedures involves learning the parameters $\bfW$, and $\tau$ and a posterior probability distribution for $\bfZ$. As the model depends on latent variables, inference can be performed using the iterative Expectation-Maximisation (EM) algorithm \cite{Rubin1982,Bishop2006}. In the expectation step, the posterior distribution for $\bfZ$ is computed in closed form (due to conjugacy between the likelihood and the prior), given current estimates for the parameters $\bfW$, and $\tau$. In the maximisation step, the parameters are calculated by maximising the expectation of the joint log likelihood under the posterior distribution of $\bfZ$ found in the E step \cite{Tipping1999}.\\
Interestingly, the EM solution of probabilistic PCA lies in the same subspace as the traditional PCA solution \cite{Tipping1999}, but the use of a probabilistic framework brings several benefits. First, model selection can be performed by comparing likelihoods across different settings of parameters. Second, missing data can naturally be accounted for by ignoring the missing observations from the likelihood. Finally, the probabilistic formulation sets the core framework for a Bayesian treatment of PCA, enabling a broad range of principled extensions tailored different types of datasets.

\subsection{Bayesian Principal Component Analysis and Bayesian Factor Analysis} \label{section:bayesian_pca}

The full Bayesian treatment of PCA requires the specification of prior probability distributions for all unobserved variables:
\begin{align*}
	p(\bfZ) &= \prod_{n=1}^{N} \prod_{k=1}^{K} \Ndist{z_{nk}}{0,1} \\
	p(\bfW) &= \prod_{d=1}^{D} \prod_{k=1}^{K} \Ndist{w_{dk}}{0,1} \\
	p(\epsilon) &= \Ndist {\epsilon}{0,\tau^{-1}} \\
	p(\tau) &= \Gdist{\tau}{a_0,b_0}
\end{align*}
A generalisation to Bayesian Factor Analysis follows by allowing a separate noise term per feature:
\begin{align*}
	p(\bepsilon) &= \prod_{d=1}^{D} \Ndist {\epsilon_d}{0,\tau_d^{-1}} \\
	p(\btau) &= \prod_{d=1}^{D} \Gdist{\tau_d}{a_0,b_0}
\end{align*}
where $a_0$ and $b_0$ are fixed hyperparameters. As in \Cref{eq:ppca_lik}, this results in a Normal likelihood:
\[
	p(\bfY|\bfW,\bfZ,\btau) = \prod_{n=1}^{N} \prod_{d=1}^{D} \Ndist{y_{nd}}{\bfw_{d}^T \bfz_{n},\tau_d}
\]

The corresponding graphical model is:

\begin{figure}[H]
	\centering
	\input{graphical_models/bayesianFA}
	\caption{\textbf{Graphical model for Bayesian Factor Analysis}. All unobserved variables are modelled as random variables.}
	\label{fig:bayesianFA}
\end{figure}

\subsection{Hierarchical priors}  \label{section:hierarchical_priors}

A key advantage of the full Bayesian treatment is that it explicitly captures uncertainity on the estimation of all unobserved variables, as opposed to the probabilistic PCA model \cite{Bishop1999a,Bishop1999b}. Yet, more importantly, the use of (hierarchical) prior distributions allow different modelling assumptions to be encoded, providing a flexible and principled approach to extend PCA to a myriad of modelling scenarios, including multi-view generalisations \cite{Klami2008,Virtanen2012,Klami2015,Bunte2016,Khan2014,Zhao2016}.

\subsubsection{Automatic relevance determination} \label{section:ard}

As an example, a major challenge in PCA is how to determine the dimensionality of the latent space (i.e. the number of principal components). As we will show, the use of hierarchical prior distributions allows the model to introduce sparsity assumptions on the weights in such a way that the model automatically learns the number of factors.\\
In the context of Factor Analysis, one of the first sparsity priors to be proposed was the Automatic Relevance determination (ARD) prior \cite{Neal1995,Mackay1996,Bishop1999a,Bishop1999b}.
\begin{equation*} \label{eq:ard}
	p(\bfW|\balpha) = \prod_{k=1}^{K} \Ndist{\bfw_{:,k}}{0,\frac{1}{\alpha_{k}}\I_{D}} \\
	\qquad\qquad
	p(\balpha) = \prod_{k=1}^{K} \Gdist{\alpha_k}{a_0^\alpha, b_0^\alpha}
\end{equation*}
The aim of this prior is two-fold. First, the zero-mean normal distribution specifies that, \textit{a priori}, no information is available and all features are \textit{inactive}. When exposed to some data, the posterior distribution for $\bfW$ will be estimated by weighting the contribution from the likelihood, potentially allowing features to escape from the zero-centered prior (\Cref{fig:ard}).\\
Second, performing inference on the variable $\balpha = \{ \alpha_1, \cdots, \alpha_k \}$ enables the model to discard inactive factors. To understand this, let us assume that only $K=5$ true factors exist, but the model is initialised with $K=20$ factors. In such case, inactive factors can be prunned out by driving the corresponding $\alpha_k$ to infinity. In turn, this causes the posterior $p(\bfw_{:,k}|\bfY)$ to be sharply peaked at zero, resulting in the inactivation of all its weights. %\Cref{fig:hinton}.

\begin{figure}[H] \begin{center}
	\includegraphics[width=0.7\textwidth]{Chapter2/Figs/ard}
	\caption{Visualisation of the sparsity-inducing Automatic Relevance Determination prior}
	\label{fig:ard}
\end{center} \end{figure}

% \begin{figure}[H] \begin{center}
% 	\includegraphics[width=0.8\textwidth]{Chapter2/Figs/hinton}
%             \caption[Hinton plot of the weight matrix for a Bayesian Factor Analysis model with an ARD prior]{Hinton plots display the values of the weight matrix, similar to a heatmap, where bigger squares depict larger weights. Shown are the Hinton plots for (a) the true weights, (b) the infered weights by a Factor Analysis model with no ARD prior (middle), and (c) the infered weights by a Factor Analysis model with ARD prior per factor. This figure was generated using simulated data with $N=100$ samples, $D=10$ features and $K=3$ factors.}
% 	\label{fig:hinton}
% \end{center} \end{figure}

\subsubsection{Spike-and-slab prior} \label{section_spikeslab}

Sparse extensions of the Bayesian factor analysis model have been proposed as a regularisation mechanism but also to model inherent assumptions regarding the sparse nature of biological data \cite{Stegle2012,Gao2013}.\\
The variability observed in biological data is driven both by technical factors and biological factors. Technical factors (i.e. batch effects) tend to be relatively strong and alter the expression of a large proportion of genes, whereas the biological factors are potentially weak effects driven by changes in small gene regulatory networks \cite{Gao2013}. Hence, a practical factor analysis model should be able to learn factors with different degrees of sparsity.\\
The ARD prior proposed in \Cref{eq:ard} allows entire factors to be dropped out from the model, but it provides a weak degree of regularisation when it comes to inactivating individual weights within the active factors.

A sparse generalisation of the Factor Analysis model proposed above can be achieved by combining the ARD prior with a spike-and-slab prior \cite{Mitchell1988,Titsias2011}. For every weight $w_{d,k}$:
\begin{align}
	p(w_{d,k} \mid \alpha_k,\theta_k) &= (1-\theta_k) \mathds{1}_0(w_{d,k}) + \theta_k \Ndist{w_{d,k}}{0, \alpha_k^{-1}} \\
	p(\theta_k) &= \Bdist{\theta_k}{a_0^\theta,b_0^\theta} \\
	p(\alpha_k) &= \Gdist{\alpha_k}{a_0^\alpha, b_0^\alpha}
\end{align}

The corresponding graphical model is:

\begin{figure}[H] \begin{center}
	\input{graphical_models/bayesian_sparseFA}
	\label{fig:bayesianFA}
	\caption{\textbf{Graphical model for Bayesian sparse Factor Analysis}. A double sparsity-inducing prior is used on the weights: an ARD prior to prune inactive factors and a spike-and-slab prior to inactive individual features within the active factors.}
\end{center} \end{figure}

The spike-and-slab prior is effectively a mixture model where features are sampled from a zero-inflated Gaussian distribution, where $\theta_k \in (0,1)$ dictates the level of sparsity per factor (i.e. how many active features). A value of $\theta_k$ close to $0$ implies that most of the weights of factor $k$ are shrunk to $0$ (i.e. a sparse factor), whereas a value of $\theta_k$ close to $1$ implies that most of the weights are non-zero (i.e. dense factors). By learning $\theta_k$ from the data, the model naturally accounts for combinations of sparse and dense factors.

\subsection{Multi-view factor analysis models}

Probabilistic PCA and Factor Analysis perform dimensionality reduction from a single input matrix. In some occasions data is collected from multiple data sources that exibit heterogeneous statistical properties, resulting in a structured data set where features are naturally partitioned into views \cite{Xu2013,Li2016,Zeng2018}. A clear biological example is multi-omics data, where, for the same set of samples, multiple molecular layers are profiled. Each of the data modalities can be analysed separately using conventional (single-view) methods, but in the ideal strategy a single model should be used to leverage information across all molecular layers using a flexible and principled approach. This is referred to as the multi-view learning problem \cite{Xu2013,Li2016}.\\
A tempting approach to circumvent the multi-view learning problem is to simply concatenate all datasets before applying conventional (single-view) latent variable models \cite{Ritchie2015}. However, this is prone to fail for several reasons. First, heterogeneous data modalities cannot always be modelled using the same likelihood function. For example, continuous measurements are often modelled using a normal distribution, but binary and count-based traits are not appropriately modelled by this distribution \cite{Pilling2018}. Second, even if all views are modelled with the same likelihood, differences in the scale and the magnitude of the variance can lead to some views being overrepresented in the latent space. Finally, in a multi-view data set we expect multiple sources of variation, some driven by a single view, whereas others could capture shared variability across multiple views. In other words, from a structured input space, one can also expect a structured latent representation. Not taking this behaviour into account can lead to challenges in the interpretability of the latent space.

A comprehensive review of multi-view machine learning methods can be found in \cite{Xu2013} and a more genomics-oriented perspective in \cite{Ritchie2015}. For the purpose of this thesis, I will describe only the use of latent variable models for multi-view data integration.

\subsection{Canonical Correlation Analysis} \label{cca}

Canonical Correlation Analysis (CCA) is a simple extension of PCA to find linear components that capture correlations between two datasets \cite{Hotteling1936,Hardle2007}.\\
Given two data matrices $\bfY_1 \in \R^{N \times D_1}$ and $\bfY_2 \in \R^{N \times D_2}$ CCA finds a set of linear combinations $\bfU \in \R^{D_1 \times K}$ and $\bfV \in \R^{D_2 \times K}$ with maximal cross-correlation.
For the first pair of canonical variables, the optimisation problem is:
\[
	(\hat{\bfu_1}, \hat{\bfv_1}) = \argmax_{\bfu_1,\bfv_1} corr(\bfu_{1}^T \bfY_1, \bfv_{1}^T \bfY_2)
\]
As in conventional PCA, the linear components are constraint to be orthogonal. Hence, the first pair of canonical variables $\bfu_1$ and $\bfv_1$ contain the linear combination of variables that have maximal correlation. Subsequently, the second pair of canonical variables $\bfu_2$ and $\bfv_2$ is found from the residuals of the first canonical variables.

Given the similarity with PCA, both methods share statistical properties, including the linear mapping between the low-dimensional space and the high-dimensional space, and the closed-form solution using singular value decomposition \cite{Hotteling1936,Hardle2007}.\\
Because of its simplicity and efficient computation, CCA has widespread use as a dimensionality reduction technique \cite{Hardle2007}. Yet, as expected, CCA suffers from the same pitfalls as PCA: difficulties in selecting the number of components, lack of sparsity in the solutions and absence of probabilistic formulation. In addition, CCA have been shown to overfit for datasets where $D>>N$ \cite{McCabe2018,Guo2016}. Hence, probabilistic versions with sparsity assumptions that reduce overfitting and improve interpretability followed.

\subsubsection{Probabilistic Canonical Correlation Analysis} \label{section_probabilisticCCA}

Following the derivation of probabilistic PCA \cite{Tipping1999}, a similar effort enabled a probabilistic formulation of CCA as a generative model \cite{Bach2005}.\\
In this model, the two matrix of observations $\bfY^{1}$ and $\bfY^{2}$ are decomposed in terms of two weight matrices $\bfW^{1}$ and $\bfW^{2}$ but a joint latent matrix $\bfZ$:
\begin{align*}
	\bfY^{1} &= \bfW^{1} \bfZ + \epsilon^{1} \\
	\bfY^{2} &= \bfW^{2} \bfZ + \epsilon^{2}
\end{align*}
With the following prior probability distributions:
\begin{align*}
	p(z_{nk}) &= \Ndist{z_{nk}}{0,1} \\
	p(\epsilon^{1}) &= \Ndist {\epsilon^{1}}{0,\tau_{1}^{-1}} \\
	p(\epsilon^{2}) &= \Ndist {\epsilon^{2}}{0,\tau_{2}^{-1}}
\end{align*}
As in \cite{Tipping1999}, the weights and the variance of the noise are assumed to be non-probabilistic parameters, whereas the factors are probabilistic unobserved variables. This yields the following likelihood functions:
\begin{align} \label{eq:probabilistic_cca_likelihood}
	p(\bfY^{1}|\bfW^{1},\bfZ,\tau_{1}) &= \prod_{n=1}^{N} \prod_{d=1}^{D_1} \Ndist{y^{1}_{n,d}}{(\bfw_{:,k}^{1})^T \bfz_{n},\tau_{1}^{-1}} \\
	p(\bfY^{2}|\bfW^{2},\bfZ,\tau_{2}) &= \prod_{n=1}^{N} \prod_{d=1}^{D_2} \Ndist{y^{2}_{n,d}}{(\bfw_{:,k}^{2})^T \bfz_{n},\tau_{2}^{-1}} \nonumber
\end{align}

The corresponding graphical model is:
\begin{figure}[H] \begin{center}
	\input{graphical_models/CCA}
	\label{fig:graphical_CCA}
	\caption{Graphical model for probabilistic Canonical Correlation Analysis}
\end{center} \end{figure}

Notice that the observations for both datasets are generated from the same set of latent variables $\bfZ$. This ensures that the model is focused on capturing the variation associated with cross-correlated groups of features.

Analogously to probabilistic PCA, the expected value of the posterior distribution $p(\bfZ|\bfY^{1},\bfY^{2})$ span the same subspace as standard CCA \cite{Bach2005}. Nonetheless, one of the many advantage of a probabilistic formulation is that it enables a broad range of principled extensions into larger graphical models.

\subsubsection{Bayesian Canonical Correlation Analysis} \label{section:bayesian_cca}

A fully Bayesian treatment of CCA followed based on exactly the same principle presented in \Cref{section:bayesian_pca} by introducing prior distributions to all unobserved variables \cite{Wang2007,Klami2013}:
\begin{align*}
	p(\bfZ) &= \prod_{n=1}^{N} \prod_{k=1}^{K} \Ndist{z_{nk}}{0,1} \\
	p(\epsilon^{1}) &= \Ndist {\epsilon^{1}}{\sigma_{1}^{2}} \\
	p(\epsilon^{2}) &= \Ndist {\epsilon^{2}}{\sigma_{2}^{2}} \\
	p(\bfW^1|\balpha) &= \prod_{k=1}^{K} \Ndist{\bfw^{1}_{:,k}}{0,\frac{1}{\alpha_{k}}\I_{D_1}} \\
	p(\bfW^2|\balpha) &= \prod_{k=1}^{K} \Ndist{\bfw^{2}_{:,k}}{0,\frac{1}{\alpha_{k}}\I_{D_2}} \\
	p(\balpha) &= \prod_{k=1}^{K} \Gdist{\alpha_k}{a_0^\alpha, b_0^\alpha}
\end{align*}
Resulting in the same likelihood model as in \Cref{eq:probabilistic_cca_likelihood}. Yet, notice that an ARD is introduced per factor, allowing an automatic inference of the dimensionality in the latent subspace.
Also, there is some flexibility in the definition of noise. Whereas an independent noise term can be defined per view, one can also model correlated noise by introducing a multivariate Gaussian distribution with full-rank covariance \cite{Wang2007,Klami2013}.

The corresponding graphical model is:
\begin{figure}[H] \begin{center}
	\input{graphical_models/bayesianCCA}
	\label{fig:graphical_bayesianCCA}
	\caption{Graphical model for Bayesian Canonical Correlation Analysis}
\end{center} \end{figure}

As expected, the sparsity priors yield a more sparse solution than traditional CCA, which is more appropriate for biological data analysis. However, this solution is still limited to $M=2$ views, which leads us to the next model extension.

% (\Cref{fig:hinton_cca}):
% \begin{figure}[H]
% 	\centering
% 	\includegraphics[width=0.85\linewidth]{hinton_cca}
% 	\caption{Comparison of the Hinton's diagram of $\bfW^{1}$ and $\bfW^{2}$ for the maximum likelihood CCA model (two left plots) and the variational Bayes CCA model (two right plots). Reprinted from \cite{Wang2007} with modifications.}
% 	\label{fig:hinton_cca}
% \end{figure}

\subsection{Group Factor Analysis} \label{section:gfa}

Group Factor Analysis (GFA) is the natural generalisation of Bayesian Canonical Correlation Analysis to an arbitrary number of views.
The original idea was originally presented in \cite{Virtanen2012} and a series of generalisations followed, tailored with specific assumptions for different applications \cite{Klami2015,Leppaaho2017,Bunte2016,Khan2014,Zhao2016,Remes2015}. In this section we will outline the core principle of GFA.

Given a data set of $M$ views $\bfY_1, \cdots, \bfY_M$, the task of GFA is to find $K$ factors that capture the variability \textit{within} as well as the variability \textit{between} views. In other words, we want to capture factors that not only explain variance that is shared across all views but we also want to capture factors that explain variance within a single view or between different subsets of views.\\
The starting point is to generalise the Bayesian CCA model (\Cref{section:bayesian_cca}) to $M$ views:
\begin{align*}
	\bfY^{1} &= \bfW^{2} \bfZ + \epsilon^{1} \\
	\bfY^{2} &= \bfW^{2} \bfZ + \epsilon^{2} \\
	& \cdots \\
	\bfY^{M} &= \bfW^{M} \bfZ + \epsilon^{M}
\end{align*}
Notice that there is a common factor space for all views, but there is a view-specific weight matrix. The key to disentangle the activity of each factor in each view lies on the sparsity structure imposed in the weights. Intuitively, if a factor $k$ is not driving any variation in a specific view $m$ we want all the individual weights to be pushed to zero. As shown before, this behaviour can be achieved using Automatic Relevance Determination (ARD) priors. However, if we were to use the same approach as in Bayesian CCA, where the ARD prior for factor $k$ is shared across all views, then factors would be restricted to have the same activity across all views.\\
In GFA this is generalised as follows:
\begin{align}
	p(\bfW) &= \prod_{m=1}^{M} \prod_{k=1}^{K} \Ndist{\bfw_{:,k}^m}{0,\frac{1}{\alpha_k^m}} \\
	p(\balpha) &= \prod_{m=1}^{M} \prod_{k=1}^{K} \Gdist{\alpha_k^m}{a_0^\alpha, b_0^\alpha}
\end{align}
This is effectively setting an ARD prior per factor $k$ and view $m$. The matrix $\balpha \in \R^{M \times K}$ defines four types of factors: (1) Inactive factors that do not explain variance in any view, which corresponds to all values $\balpha_k$ being large. (2) Fully shared factors that explain variance across all views, which corresponds to all values $\balpha_k$ being small. (3) Unique factors that explain variance in a single view, which corresponds to all values $\balpha_k$ being large, except for one entry. (4) Partially shared factors that explain variance in a subsets views, which corresponds to a mixture of small and large values for $\balpha_k$.\\

The corresponding graphical model is:

\begin{figure}[H] \begin{center}
	\input{graphical_models/GFA}
	\label{fig:graphical_GFA}
	\caption{Graphical model for Bayesian Group Factor Analysis}
\end{center} \end{figure}

Finally, notice that if $M=1$ the model reduces to Bayesian PCA (\Cref{section:bayesian_pca}), but when $M=2$ the model does \textit{not} reduce to Bayesian CCA because in the GFA setting factors are also allowed to capture both inter-specific variability (i.e. across views) and intra-specific variability (within a view). In Bayesian CCA, the views share a common ARD prior per factor to enforce the factors to explain variation in both views, at the expense of ignoring sources of variability that are specific to a single view.


\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{hyperref}
\usepackage{enumitem}

\newcommand{\fromlectures}{{\\ \color{blue} \hspace*{\fill}(from lecture slides)} \\}
\newcommand{\bydefn}{{\\ \color{blue} \hspace*{\fill}(by definition)} \\}
\newcommand{\given}{{\\ \color{blue} \hspace*{\fill}(given)} \\}
\newcommand{\rtp}{{\\ \color{blue} \hspace*{\fill}(required to prove)} \\}

\newcommand{\f}[1]{o_{#1}x_{#1}y_{#1}z_{#1}}
\newcommand{\rx}[1]{\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & cos(#1) & -sin(#1) & 0 \\ 0 & sin(#1) & cos(#1) & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}}
\newcommand{\rz}[1]{\begin{bmatrix} cos(#1) & -sin(#1) & 0 & 0 \\ sin(#1) & cos(#1) & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}}
\newcommand{\iden}{\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}}
\newcommand{\trans}[3]{\begin{bmatrix} 1 & 0 & 0 & #1 \\ 0 & 1 & 0 & #2 \\ 0 & 0 & 1 & #3 \\ 0 & 0 & 0 & 1 \end{bmatrix}}

\title{CSci 5551 - HW3}
\author{Yashasvi Sriram Patkuri\\patku001@umn.edu}

\begin{document}
\maketitle
\pagebreak

\section{}
Robot arm has 3 DOF as illustrated in the Figure \ref{fig:q1.1}.
I am assuming that length of link 3 is $L_3$ since it is not mentioned in the figure.
\given

\begin{figure}[h]
  \centering
  \includegraphics[width=0.6\textwidth]{q1.png}
  \caption{3DOF robot arm}
  \label{fig:q1.1}
\end{figure}

Assigning frames according to DH convention we have,
\fromlectures
\begin{figure}[h]
  \centering
  \begin{tikzpicture}[x=1cm, y=1cm, z=-0.6cm]
    % F1
    \draw [->]       (0, 0) -- (2, 0) node [right] {$x_1$};
    \draw [->]       (0, 0) -- (0, 2) node [left] {$z_1$};
    % F2
    \draw [->]       (4, 0) -- (6, 0) node [right] {$x_2$};
    \draw            (4, 0) circle (0.15cm);
    \draw [fill]     (4, 0) circle (0.07cm) node [left] {$z_2$};
    % F3
    \draw [->]       (6, 2) -- (7.414, 3.414) node [right] {$x_3$};
    \draw            (6, 2) circle (0.15cm);
    \draw [fill]     (6, 2) circle (0.07cm) node [left] {$z_3$};
    % F4
    \draw [->]       (5, 4) -- (3.656, 5.514) node [right] {$x_4$};
    \draw            (5, 4) circle (0.15cm);
    \draw [fill]     (5, 4) circle (0.07cm) node [right] {$z_4$};
  \end{tikzpicture}
  \caption{DH frame assignment}
  \label{fig:q1.2}
\end{figure}
\begin{enumerate}[nolistsep]
  \item The z-axes are chosen along the axes of rotation.
  \item The choice of $z_4$ is free, so it is chosen to be parallel to $z_3$ for simplicity.
  \item The choice of $x_1$ is free, so it chosen in the direction of $x_2$ for simplicity.
  \item $x_2$ lies along the common normal to $z_1$ and $z_2$ which is unique.
  \item There are many common normal to $z_2$ and $z_3$ as they are parallel, so $x_3$ is chosen to be in the plane of paper for simplicity.
  \item $x_4$ is chosen in a similar manner.
  \item Each y-axis (not shown) just form a right handed coordinate system with respective frame.
\end{enumerate}

\subsubsection*{DH parameters}
The DH parameters for the frames shown in Figure \ref{fig:q1.2} are as follows
\begin{center}
\begin{tabular}{ c | c c c c }
 \hline
 $F_i \to F_j$ & $\theta$ & d & r & $\alpha$ \\
 \hline
 $1 \to 2$ & $\theta_1$ & 0 & $L_1$ & $90^{\circ}$ \\
 $2 \to 3$ & $\theta_2$ & 0 & $L_2$ & $0^{\circ}$ \\
 $3 \to 4$ & $\theta_3$ & 0 & $L_3$ & $0^{\circ}$ \\
 \hline
\end{tabular}
\end{center}

\subsubsection*{Transformation from $F_1$ to $F_3$}
\[
  T_{13} \equiv T_{12} * T_{23}
\]
$T_{12}, T_{23}$ can be constructed using DH parameters.
Given frame i and frame j with DH parameters [ $\theta$, d, r, $\alpha$ ] the transformation matrix is
\[
  T_{i,j} \equiv Rot_{z,\theta} * Trans_{z, d} * Trans_{x, r} * Rot_{x, \alpha}
\]
\fromlectures
Consider $T_{12}$
\[
  T_{12} \equiv Rot_{z,\theta_1} * Trans_{z, 0} * Trans_{x, L_1} * Rot_{x, 90^{\circ}}
\]
\[
  T_{12} \equiv \rz{\theta_1} * \trans{0}{0}{0} * \trans{L_1}{0}{0} * \rx{90^{\circ}}
\]
\[
  T_{12} \equiv \rz{\theta_1} * \trans{L_1}{0}{0}
    * \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{12} \equiv \rz{\theta_1}
  * \begin{bmatrix} 1 & 0 & 0 & L_1 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{12} \equiv
  \begin{bmatrix} c\theta_1 & 0 & s\theta_1 & L_1c\theta_1 \\ s\theta_1 & 0 & -c\theta_1 & L_1s\theta_1 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]

Consider $T_{23}$
\[
  T_{23} \equiv Rot_{z,\theta_2} * Trans_{z, 0} * Trans_{x, L_2} * Rot_{x, 0^{\circ}}
\]
\[
  T_{23} \equiv \rz{\theta_2} * \trans{0}{0}{0} * \trans{L_2}{0}{0} * \rx{0^{\circ}}
\]
\[
  T_{23} \equiv \rz{\theta_2} * \trans{0}{0}{0} * \trans{L_2}{0}{0} * \iden
\]
\[
  T_{23} \equiv \rz{\theta_2} * \trans{L_2}{0}{0}
\]
\[
  T_{23} \equiv
  \begin{bmatrix} c\theta_2 & -s\theta_2 & 0 & L_2c\theta_2 \\ s\theta_2 & c\theta_2 & 0 & L_2s\theta_2 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]

\[
  T_{13} \equiv T_{12} * T_{23}
\]
Substituting $T_{12}, T_{23}$ we have,
\[
  T_{13} \equiv
  \begin{bmatrix} c\theta_1 & 0 & s\theta_1 & L_1c\theta_1 \\ s\theta_1 & 0 & -c\theta_1 & L_1s\theta_1 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  *
  \begin{bmatrix} c\theta_2 & -s\theta_2 & 0 & L_2c\theta_2 \\ s\theta_2 & c\theta_2 & 0 & L_2s\theta_2 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{13} \equiv
  \begin{bmatrix}
    c\theta_1c\theta_2 & -c\theta_1s\theta_2 & s\theta_1 & L_2c\theta_1c\theta_2 + L_1c\theta_1 \\
    s\theta_1c\theta_2 & -s\theta_1s\theta_2 & -c\theta_1 & L_2s\theta_1c\theta_2 + L_1s\theta_1 \\
    s\theta_2 & c\theta_2 & 0 & L_2s\theta_2 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]

\pagebreak

\section{}
Robot arm has 6 DOF as illustrated in the Figure \ref{fig:q2.1}.
\given

\begin{figure}[h]
  \centering
  \includegraphics[width=0.6\textwidth]{q2.png}
  \caption{6DOF robot arm}
  \label{fig:q2.1}
\end{figure}

Assigning frames according to DH convention we have,
\fromlectures
\begin{figure}[h]
  \centering
  \begin{tikzpicture}[x=1cm, y=1cm, z=-0.6cm]
    % F0
    \draw [->]       (0, 0) -- (1, 0) node [right] {$x_0$};
    \draw [->]       (0, 0) -- (0, 1) node [left] {$z_0$};
    % F1
    \draw [->]       (0, 2) -- (1, 2) node [right] {$x_1$};
    \draw [->]       (0, 2) -- (0, 3) node [left] {$z_1$};
    % F2
    \draw [->]       (0, 4) -- (1, 4) node [right] {$x_2$};
    \draw            (0, 4) circle (0.15cm);
    \draw [fill]     (0, 4) circle (0.07cm) node [left] {$z_2$};
    % F3
    \draw [->]       (3, 4) -- (4, 4) node [right] {$x_3$};
    \draw            (3, 4) circle (0.15cm);
    \draw [fill]     (3, 4) circle (0.07cm) node [left] {$z_3$};
    % F4
    \draw [->]       (6, 4) -- (7, 4) node [right] {$x_4$};
    \draw            (6, 4) circle (0.15cm);
    \draw [fill]     (6, 4) circle (0.07cm) node [left] {$z_4$};
    % F5
    \draw [->]       (9, 4) -- (10, 4) node [right] {$x_5, z_6$};
    \draw [->]       (9, 4) -- (9, 5) node [left] {$z_5$};
    \draw            (9, 4) circle (0.15cm);
    \draw [fill]     (9, 4) circle (0.07cm) node [left] {$x_6$};
    % F6
    \draw [->]       (15, 4) -- (16, 4) node [right] {$z_t$};
    \draw [->]       (15, 4) -- (15, 3) node [left] {$x_t$};
  \end{tikzpicture}
  \caption{DH frame assignment}
  \label{fig:q2.2}
\end{figure}
\begin{enumerate}[nolistsep]
  \item The z-axes are chosen along the axes of rotation or translation.
  \item $z_0$, $z_1$ are co-incident, therefore $x_1$ is chosen at the end of link 1 for simplicity.
  \item $z_1$, $z_2$ intersection is taken as origin of $F_2$ with $x_2$ perpendicular to plane formed by $z_2$ and $z_1$.
  \item $z_2$, $z_3$ are parallel so $x_3$ is chosen to be on plane of paper for simplicity.
  \item $x_4$ is chosen in the same ways as $x_3$.
  \item $z_4$, $z_5$ have a unique common normal, thus there is only one choice of $x_5$.
  \item $z_5$, $z_6$ intersection is taken as origin of $F_6$ with $x_6$ perpendicular to plane formed by $z_5$ and $z_6$.
  \item Note that this step is probably most non-trivial in this procedure as origins of $F_5$ and $F_6$ coincide.
\end{enumerate}

\subsubsection*{DH parameters}
I am assuming that the lengths $l_2$ and $l_7$ are constant link lengths and the prismatic joints $J_1$ and $J_6$ provide additional (signed) lengths of $q_1$ and $q_6$ respectively.
With this the DH parameters for the frames shown in Figure \ref{fig:q2.2} are as follows,
\begin{center}
\begin{tabular}{ c | c c c c }
 \hline
 $F_i \to F_j$ & $\theta$ & d & r & $\alpha$ \\
 \hline
 $0 \to 1$ & $0^{\circ}$    & $l_1$                      & $0$     &   $0^{\circ}$   \\
 $1 \to 2$ & $0^{\circ}$    & $l_2 + q_1$                & $0$     &   $90^{\circ}$  \\
 $2 \to 3$ & $q_2$          & $0$                        & $l_3$   &   $0^{\circ}$   \\
 $3 \to 4$ & $q_3$          & $0$                        & $l_4$   &   $0^{\circ}$   \\
 $4 \to 5$ & $q_4$          & $0$                        & $l_5$   &   $-90^{\circ}$ \\
 $5 \to 6$ & $q_5$          & $0$                        & $0$     &   $-90^{\circ}$ \\
 $6 \to t$ & $90^{\circ}$   & $l_6 + l_7 + q_6$          & $0$     &   $0^{\circ}$ \\
 \hline
\end{tabular}
\end{center}

\subsubsection*{Transformation from $F_0$ to $F_4$}
\[
  T_{04} \equiv T_{01} * T_{12} * T_{23} * T_{34}
\]
$T_{01}, T_{12}, T_{23}, T_{34}$ can be constructed using DH parameters.
Given frame i and frame j with DH parameters [ $\theta$, d, r, $\alpha$ ] the transformation matrix is
\[
  T_{i,j} \equiv Rot_{z,\theta} * Trans_{z, d} * Trans_{x, r} * Rot_{x, \alpha}
\]
\fromlectures
Consider $T_{01}$,
\[
  T_{01} \equiv Rot_{z, 0} * Trans_{z, l_1} * Trans_{x, 0} * Rot_{x, 0^{\circ}}
\]
\[
  T_{01} \equiv \rz{0} * \trans{0}{0}{l_1} * \trans{0}{0}{0} * \rx{0^{\circ}}
\]
Evaluating trigs,
\[
  T_{01} \equiv \iden * \trans{0}{0}{l_1} * \trans{0}{0}{0} * \iden
\]
Removing identities,
\[
  T_{01} \equiv \trans{0}{0}{l_1}
\]
Consider $T_{12}$,
\[
  T_{12} \equiv Rot_{z, 0} * Trans_{z, l_2 + q_1} * Trans_{x, 0} * Rot_{x, 90^{\circ}}
\]
\[
  T_{12} \equiv \rz{0} * \trans{0}{0}{l_2 + q_1} * \trans{0}{0}{0} * \rx{90^{\circ}}
\]
Evaluating trigs,
\[
  T_{12} \equiv \iden * \trans{0}{0}{l_2 + q_1} * \trans{0}{0}{0}
  * \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Removing identities,
\[
  T_{12} \equiv \trans{0}{0}{l_2 + q_1}
  * \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{12} \equiv
  \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & l_2 + q_1 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Consider $T_{23}$,
\[
  T_{23} \equiv Rot_{z, q_2} * Trans_{z, 0} * Trans_{x, l_3} * Rot_{x, 0^{\circ}}
\]
\[
  T_{23} \equiv \rz{q_2} * \trans{0}{0}{0} * \trans{l_3}{0}{0} * \rx{0^{\circ}}
\]
Evaluating trigs,
\[
  T_{23} \equiv \rz{q_2} * \trans{0}{0}{0} * \trans{l_3}{0}{0} * \iden
\]
Removing identities,
\[
  T_{23} \equiv \rz{q_2} * \trans{l_3}{0}{0}
\]
\[
  T_{23} \equiv
  \begin{bmatrix} cq_2 & -sq_2 & 0 & l_3cq_2 \\ sq_2 & cq_2 & 0 & l_3sq_2 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Consider $T_{34}$,
\[
  T_{34} \equiv Rot_{z, q_3} * Trans_{z, 0} * Trans_{x, l_4} * Rot_{x, 0^{\circ}}
\]
This is same as $T_{23}$ except for the variables, therefore we can use the final form of $T_{23}$ with replaced variables.
\[
  T_{34} \equiv
  \begin{bmatrix} cq_3 & -sq_3 & 0 & l_4cq_3 \\ sq_3 & cq_3 & 0 & l_4sq_3 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Finally,
\[
  T_{04} \equiv T_{01} * T_{12} * T_{23} * T_{34}
\]
\[
  T_{04} \equiv
  \trans{0}{0}{l_1}
  *
  \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & l_2 + q_1 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  *
  \begin{bmatrix} cq_2 & -sq_2 & 0 & l_3cq_2 \\ sq_2 & cq_2 & 0 & l_3sq_2 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  *
  \begin{bmatrix} cq_3 & -sq_3 & 0 & l_4cq_3 \\ sq_3 & cq_3 & 0 & l_4sq_3 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Multiplying first two,
\[
  T_{04} \equiv
  \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & l_1 + l_2 + q_1 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  *
  \begin{bmatrix} cq_2 & -sq_2 & 0 & l_3cq_2 \\ sq_2 & cq_2 & 0 & l_3sq_2 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  *
  \begin{bmatrix} cq_3 & -sq_3 & 0 & l_4cq_3 \\ sq_3 & cq_3 & 0 & l_4sq_3 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Multiplying last two,
\[
  T_{04} \equiv
  \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & l_1 + l_2 + q_1 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  *
  \begin{bmatrix}
    cq_2cq_3 - sq_2sq_3 & -(sq_2cq_3 + cq_2sq_3) & 0 & l_4(cq_2cq_3 - sq_2sq_3) + l_3cq_2 \\
    sq_2cq_3 + cq_2sq_3 & cq_2cq_3 - sq_2sq_3    & 0 & l_4(sq_2cq_3 + cq_2sq_3) + l_3sq_2 \\
    0                   & 0                      & 1 & 0 \\
    0                   & 0                      & 0 & 1
  \end{bmatrix}
\]
Using trig identity $s(a + b) \equiv s(a)c(b) + c(a)s(b), c(a + b) \equiv c(a)c(b) - s(a)s(b)$
\[
  T_{04} \equiv
  \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & l_1 + l_2 + q_1 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  *
  \begin{bmatrix}
    c(q_2 + q_3) & -s(q_2 + q_3) & 0 & l_4c(q_2 + q_3) + l_3cq_2 \\
    s(q_2 + q_3) & c(q_2 + q_3)  & 0 & l_4s(q_2 + q_3) + l_3sq_2 \\
    0            & 0             & 1 & 0 \\
    0            & 0             & 0 & 1
  \end{bmatrix}
\]
\[
  T_{04} \equiv
  \begin{bmatrix}
    c(q_2 + q_3) & -s(q_2 + q_3) & 0 & l_4c(q_2 + q_3) + l_3cq_2 \\
    0 & 0 & -1 & 0 \\
    s(q_2 + q_3) & c(q_2 + q_3)  & 0 & l_4s(q_2 + q_3) + l_3sq_2 + l_1 + l_2 + q_1 \\
    0 & 0 & 0 & 1 \\
  \end{bmatrix}
\]

\pagebreak

\section{}
MOM manipulator is illustrated in the Figure \ref{fig:q3.1}.
The lengths of various parts are labeled on the image in red ink.
Specifically the distance from base to joint 1 is $l_1$, distance b/w shoulder and forearm is $l_2$ and the distance b/w wrist and end-effector is $l_3$.
\given
\begin{figure}[h]
  \centering
  \includegraphics[width=1\textwidth]{q3.png}
  \caption{MOM manipulator}
  \label{fig:q3.1}
\end{figure}

\pagebreak
Assigning frames according to DH convention we have,
\fromlectures
\begin{figure}[h]
  \centering
  \begin{tikzpicture}[x=1cm, y=1cm, z=-0.6cm]
    \draw [dashed, red]       (-2, -3) -- (-2, 2) node [right] {$l_1$};
    \draw [dashed, red]       (5, 2) -- (5, 1) node [right] {$l_2$};
    % F0
    \draw [->]       (0, -3) -- (0, -2) node [right] {$z_0$};
    \draw            (0, -3) circle (0.15cm);
    \draw [fill]     (0, -3) circle (0.07cm) node [right] {$x_0$};
    % F1,2
    \draw [->]       (0, 2) -- (1, 2) node [right] {$z_1, z_2$};
    \draw            (0, 2) circle (0.15cm);
    \draw [fill]     (0, 2) circle (0.07cm) node [left] {$x_1, x_2$};
    % F3
    \draw [->]       (4, 1) -- (4, 0) node [right] {$x_3$};
    \draw            (4, 1) circle (0.15cm);
    \draw [fill]     (4, 1) circle (0.07cm) node [right] {$z_3$};
  \end{tikzpicture}
  \caption{DH frame assignment for joints 1-3: Front view}
  \label{fig:q3.2}
\end{figure}
\begin{figure}[h]
  \centering
  \begin{tikzpicture}[x=1cm, y=1cm, z=-0.6cm]
    \draw [dashed, red]       (3, -2) -- (-1, -2) node [left] {$l_3$};
    % F3
    \draw [->]       (7, 1) -- (7, 0) node [left] {$x_3$};
    \draw [->]       (7, 1) -- (6, 1) node [left] {$z_3$};
    % F4,5,6
    \draw [->]       (3, 1) -- (3, 0) node [left] {$x_4$};
    \draw [->]       (3, 1) -- (3, 2) node [left] {$z_5$};
    \draw [->]       (3, 1) -- (2, 1) node [left] {$z_4, z_6$};
    \draw            (3, 1) circle (0.15cm);
    \draw [fill]     (3, 1) circle (0.07cm) node [right] {$x_5, x_6$};
    % F7
    \draw [->]       (-1, 1) -- (-2, 1) node [left] {$z_7$};
    \draw            (-1, 1) circle (0.15cm);
    \draw [fill]     (-1, 1) circle (0.07cm) node [right] {$x_7$};
  \end{tikzpicture}
  \caption{DH frame assignment for joints 4-7: Side view}
  \label{fig:q3.3}
\end{figure}
\begin{enumerate}[nolistsep]
  \item The base frame z-axis is chosen to be upward and its x-axis in the direction of end-effector for the pose in Figure \ref{fig:q3.1}.
  \item The side in which $x_0$ comes out of paper and $z_0$ goes up is referred to as the front view in Figure \ref{fig:q3.2}. The side view formed by it is used in Figure \ref{fig:q3.3}.
  \item The end-effector frame z-axis is chosen as shown in Figure \ref{fig:q3.3}.
  \item The z-axes are chosen along the axes of rotation/translation.
  \item $z_0$ and $z_1$ intersection is taken as origin of $F_1$, $x_1$ is chosen to be normal to plane formed by $z_0$ and $z_1$.
  \item $z_1$ and $z_2$ coincide, so $x_2$ is chosen to be coincident to $x_1$ for simplicity.
  \item $z_2$ and $z_3$ have a unique common normal, so $x_3$ is chosen according to that.
  \item $F_1 \to F_2$ captures joint 1 effect. $F_2 \to F_3$ captures joint 2 effect.
  \item Spherical joint is made using three rotational joints as described in lecture slides captured by frames $F_4, F_5, F_6$.
  \item $z_3$ and $z_4$ are coincident, so $x_4$ is chosen to be on the end of forearm for simplicity.
  \item $z_4$ and $z_5$ intersection is taken as origin for $F_5$ and $x_5$ is chosen perpendicular to plane of $z_4$ and $z_5$.
  \item $x_6$ is chosen in the same way.
  \item $z_6$ and $z_7$ are coincident so $x_7$ is chosen on the end-effector for simplicity.
  \item Each y-axis (not shown) just form a right handed coordinate system with respective frame.
\end{enumerate}

\subsubsection*{DH parameters}
DH parameters for the frames shown in Figure \ref{fig:q3.2} and \ref{fig:q3.3} are as follows,
\begin{center}
\begin{tabular}{ c | c c c c }
 \hline
 $F_i \to F_j$ & $\theta$ & d & r & $\alpha$ \\
 \hline
 $0 \to 1$ & $0^{\circ}$    & $l_1$                      & $0$     &   $-90^{\circ}$    \\
 $1 \to 2$ & $q_1$          & $0$                        & $0$     &   $0^{\circ}$      \\
 $2 \to 3$ & $90^{\circ}$   & $q_2$                      & $l_2$   &   $90^{\circ}$     \\
 $3 \to 4$ & $0^{\circ}$    & $q_3$                      & $0$     &   $0^{\circ}$      \\
 $4 \to 5$ & $q_4$          & $0$                        & $0$     &   $-90^{\circ}$    \\
 $5 \to 6$ & $q_5$          & $0$                        & $0$     &   $90^{\circ}$     \\
 $6 \to 7$ & $q_6$          & $l_3$                      & $0$     &   $0^{\circ}$      \\
 \hline
\end{tabular}
\end{center}

\subsubsection*{Transformation from $F_0$ to $F_3$}
\[
  T_{03} \equiv
  T_{01}
  * T_{12}
  * T_{23}
\]
$T_{01}, T_{12}, T_{23}$ can be constructed using DH parameters.
Given frame i and frame j with DH parameters [ $\theta$, d, r, $\alpha$ ] the transformation matrix is
\[
  T_{i,j} \equiv Rot_{z,\theta} * Trans_{z, d} * Trans_{x, r} * Rot_{x, \alpha}
\]
\fromlectures
Also observe,
\[
  Rot_{z, 0^{\circ}} \equiv \rz{0^{\circ}} \equiv I
\]
\[
  Trans_{z, 0} \equiv \trans{0}{0}{0} \equiv I
\]
\[
  Trans_{x, 0} \equiv \trans{0}{0}{0} \equiv I
\]
\[
  Rot_{x, 0^{\circ}} \equiv \rx{0^{\circ}} \equiv I
\]
Consider $T_{01}$,
\[
  T_{0,1} \equiv Rot_{z, 0} * Trans_{z, l_1} * Trans_{x, 0} * Rot_{x, -90^{\circ}}
\]
\[
  T_{0,1} \equiv I * Trans_{z, l_1} * I * Rot_{x, -90^{\circ}}
\]
\[
  T_{0,1} \equiv Trans_{z, l_1} * Rot_{x, -90^{\circ}}
\]
\[
  T_{0,1} \equiv \trans{0}{0}{l_1} * \rx{-90^{\circ}}
\]
\[
  T_{0,1} \equiv \trans{0}{0}{l_1} *
  \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{0,1} \equiv
  \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & -1 & 0 & l_1 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Consider $T_{12}$,
\[
  T_{1,2} \equiv Rot_{z, q_1} * Trans_{z, 0} * Trans_{x, 0} * Rot_{x, 0^{\circ}}
\]
\[
  T_{1,2} \equiv Rot_{z, q_1} * I * I * I
\]
\[
  T_{1,2} \equiv Rot_{z, q_1}
\]
\[
  T_{1,2} \equiv \rz{q_1}
\]
Consider $T_{23}$,
\[
  T_{2,3} \equiv Rot_{z, 90^{\circ}} * Trans_{z, q_2} * Trans_{x, l_2} * Rot_{x, 90^{\circ}}
\]
\[
  T_{2,3} \equiv \rz{90^{\circ}} * \trans{0}{0}{q_2} * \trans{l_2}{0}{0} * \rx{90^{\circ}}
\]
\[
  T_{2,3}
  \equiv
  \begin{bmatrix} 0 & -1 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \trans{0}{0}{q_2}
  * \trans{l_2}{0}{0}
  * \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Multiplying first two,
\[
  T_{2,3}
  \equiv
  \begin{bmatrix} 0 & -1 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & q_2 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \trans{l_2}{0}{0}
  * \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Multiplying last two,
\[
  T_{2,3}
  \equiv
  \begin{bmatrix} 0 & -1 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & q_2 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \begin{bmatrix} 1 & 0 & 0 & l_2 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 &  & 1 \end{bmatrix}
\]
\[
  T_{2,3}
  \equiv
  \begin{bmatrix} 0 & 0 & 1 & 0 \\ 1 & 0 & 0 & l_2 \\ 0 & 1 & 0 & q_2 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Finally,
\[
  T_{03} \equiv
  T_{01}
  * T_{12}
  * T_{23}
\]
\[
  T_{03} \equiv
  \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & -1 & 0 & l_1 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \rz{q_1}
  * \begin{bmatrix} 0 & 0 & 1 & 0 \\ 1 & 0 & 0 & l_2 \\ 0 & 1 & 0 & q_2 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{03} \equiv
  \begin{bmatrix} cq_1 & -sq_1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ -sq_1 & -cq_1 & 0 & l_1 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \begin{bmatrix} 0 & 0 & 1 & 0 \\ 1 & 0 & 0 & l_2 \\ 0 & 1 & 0 & q_2 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{03} \equiv
  \begin{bmatrix} -sq_1 & 0 & cq_1 & -l_2sq_1 \\ 0 & 1 & 0 & q_2 \\ -cq_1 & 0 & -sq_1 & -l_2cq_1 + l_1 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]

\subsubsection*{Transformation from $F_0$ to $F_6$}
\[
  T_{06} \equiv
  T_{03}
  * T_{34}
  * T_{45}
  * T_{56}
\]
Consider $T_{34}$,
\[
  T_{3,4} \equiv Rot_{z, 0} * Trans_{z, q_3} * Trans_{x, 0} * Rot_{x, 0^{\circ}}
\]
\[
  T_{3,4} \equiv I * Trans_{z, q_3} I * I
\]
\[
  T_{3,4} \equiv Trans_{z, q_3}
\]
\[
  T_{3,4} \equiv \trans{0}{0}{q_3}
\]
Consider $T_{45}$,
\[
  T_{4,5} \equiv Rot_{z, q_4} * Trans_{z, 0} * Trans_{x, 0} * Rot_{x, -90^{\circ}}
\]
\[
  T_{4,5} \equiv Rot_{z, q_4} * I * I * Rot_{x, -90^{\circ}}
\]
\[
  T_{4,5} \equiv Rot_{z, q_4} * Rot_{x, -90^{\circ}}
\]
\[
  T_{4,5} \equiv \rz{q_4} * \rx{-90^{\circ}}
\]
\[
  T_{4,5} \equiv \rz{q_4}
  \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{4,5} \equiv
  \begin{bmatrix} cq_4 & 0 & -sq_4 & 0 \\ sq_4 & 0 & cq_4 & 0 \\ 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Consider $T_{56}$,
\[
  T_{5,6} \equiv Rot_{z, q_5} * Trans_{z, 0} * Trans_{x, 0} * Rot_{x, 90^{\circ}}
\]
\[
  T_{5,6} \equiv Rot_{z, q_5} * I * I * Rot_{x, 90^{\circ}}
\]
\[
  T_{5,6} \equiv Rot_{z, q_5} * Rot_{x, 90^{\circ}}
\]
\[
  T_{5,6} \equiv \rz{q_5} * \rx{90^{\circ}}
\]
\[
  T_{5,6} \equiv \rz{q_5} *
  \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{5,6} \equiv
  \begin{bmatrix} cq_5 & 0 & sq_5 & 0 \\ sq_5 & 0 & -cq_5 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Finally,
\[
  T_{06} \equiv
  T_{03}
  * T_{34}
  * T_{45}
  * T_{56}
\]
Substituting $T_{34}, T_{45}$,
\[
  T_{06} \equiv
  T_{03}
  * \trans{0}{0}{q_3}
  * \begin{bmatrix} cq_4 & 0 & -sq_4 & 0 \\ sq_4 & 0 & cq_4 & 0 \\ 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * T_{56}
\]
\[
  T_{06} \equiv
  T_{03}
  * \begin{bmatrix} cq_4 & 0 & -sq_4 & 0 \\ sq_4 & 0 & cq_4 & 0 \\ 0 & -1 & 0 & q_3 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * T_{56}
\]
Substituting $T_{56}$,
\[
  T_{06} \equiv
  T_{03}
  * \begin{bmatrix} cq_4 & 0 & -sq_4 & 0 \\ sq_4 & 0 & cq_4 & 0 \\ 0 & -1 & 0 & q_3 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \begin{bmatrix} cq_5 & 0 & sq_5 & 0 \\ sq_5 & 0 & -cq_5 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{06} \equiv
  T_{03}
  * \begin{bmatrix} cq_4cq_5 & -sq_4 & cq_4sq_5 & 0 \\ sq_4cq_5 & cq_4 & sq_4sq_5 & 0 \\ -sq_5 & 0 & cq_5 & q_3 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Substituting $T_{03}$,
\[
  T_{06} \equiv
  \begin{bmatrix} -sq_1 & 0 & cq_1 & -l_2sq_1 \\ 0 & 1 & 0 & q_2 \\ -cq_1 & 0 & -sq_1 & -l_2cq_1 + l_1 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \begin{bmatrix} cq_4cq_5 & -sq_4 & cq_4sq_5 & 0 \\ sq_4cq_5 & cq_4 & sq_4sq_5 & 0 \\ -sq_5 & 0 & cq_5 & q_3 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{06} \equiv
  \begin{bmatrix} -s_1c_4c_5 - c_1s_5 & s_1s_4 & -s_1c_4s_5 + c_1c_5 & q_3c_1 - l_2s_1 \\ s_4c_5 & c_4 & s_4s_5 & q_2 \\ -c_1c_4c_5 + s_1s_5 & c_1s_4 & -c_1c_4s_5 - s_1c_5 & -q_3s_1 + l_1 - l_2c_1 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
where $c_i \equiv cos(q_i), s_i \equiv sin(q_i)$.

\pagebreak

\section{}
A human hand is illustrated in the Figure \ref{fig:q4.1}.
The lengths of various parts are labeled on the image in red ink.
Specifically the distance from shoulder joint to elbow is $l_1$, distance from elbow and wrist is $l_2$ and the distance from wrist and tip of fingers is $l_3$.
The tip of the hand frame is drawn a bit incorrectly. It just needs to be translated to the tip of fingers, without changing the orientation.
The joints in the fingers are ignored.
\given
\begin{figure}[h]
  \centering
  \includegraphics[width=0.68\textwidth]{q4.png}
  \caption{Human hand}
  \label{fig:q4.1}
\end{figure}

\pagebreak
Assigning frames according to DH convention we have,
\fromlectures
\begin{figure}[h]
  \centering
  \begin{tikzpicture}[x=1cm, y=1cm, z=-0.6cm]
    \draw [dashed, red]       (-3, 0) -- (-3, -5) node [right] {$l_1$};
    \draw [dashed, red]       (3, -5) -- (3, -10) node [right] {$l_2$};
    \draw [dashed, red]       (-3, -10) -- (-3, -12) node [right] {$l_3$};
    % F1,2,3
    \draw [->]       (0, 0) -- (-1, 0) node [left] {$x_0, x_1$};
    \draw            (0, 0) circle (0.15cm);
    \draw [fill]     (0, 0) circle (0.07cm) node [below] {$z_0, x_2$};
    \draw [->]       (0, 0) -- (0, 1) node [left] {$z_1$};
    \draw [->]       (0, 0) -- (1, 0) node [right] {$z_2$};
    % F4
    \draw [->]       (0, -5) -- (0, -6) node [left] {$x_3$};
    \draw            (0, -5) circle (0.15cm);
    \draw [fill]     (0, -5) circle (0.07cm) node [above] {$z_3$};
    % F5,6,7
    \draw [->]       (0, -10) -- (0, -11) node [left] {$x_4, x_5, z_6$};
    \draw [->]       (0, -10) -- (1, -10) node [right] {$z_5$};
    \draw            (0, -10) circle (0.15cm);
    \draw [fill]     (0, -10) circle (0.07cm) node [left] {$z_4, x_6$};
    % F8
    \draw [->]       (0, -12) -- (0, -13) node [left] {$z_7 \equiv a$};
    \draw [->]       (0, -12) -- (1, -12) node [right] {$x_7 \equiv s$};
  \end{tikzpicture}
  \caption{DH frame assignment}
  \label{fig:q4.2}
\end{figure}
\begin{enumerate}[nolistsep]
  \item The spherical joint at the shoulder is modeled using 3 revolute joints captured by $F_0, F_1, F_2$, elbow by $F_3$ and wrist by $F_4, F_5, F_6$ and tip of hand by $F_7$. The lengths labeled in Figure \ref{fig:q4.1} are replicated in Figure \ref{fig:q4.2}.
    \begin{enumerate}[nolistsep]
      \item $q_1$ models sideways rotation.
      \item $q_2$ models hand twisting rotation.
      \item $q_3$ models forward/backward rotation.
      \item $q_4$ models elbow rotation
      \item $q_5$ models wrist yaw.
      \item $q_6$ models wrist pitch.
      \item $q_7$ models forearm axial roll.
    \end{enumerate}
  \item z-axes are chosen using joints.
  \item $z_0, z_1$ intersection point is chosen as origin for $F_1$, and $x_1$ is chosen perpendicular to $z_0, z_1$.
  \item $x_2$ is chosen similarly.
  \item $z_2, z_3$ have a unique common normal, so $x_3$ is chosen along that.
  \item $z_3, z_4$ are parallel therefore $x_4$ is chosen to be on the paper for simplicity.
  \item $z_4, z_5$ intersection point is chosen as origin for $F_5$, and $x_5$ is chosen perpendicular to $z_4, z_5$.
  \item $x_6$ is chosen similarly.
  \item $F_7$ is given, I assume that a $\equiv$ z-axis and s $\equiv$ x-axis.
\end{enumerate}

\paragraph{Note} The joints and frames are re-ordered to achieve the required `tip of the hand' transformation. Specifically joints 5 (axial roll), 6 (wrist yaw), 7 (wrist pitch) are mapped to DH parameters $q_7, q_5, q_6$ respectively. This re-ordering can be done because z-axis of joints 4 (elbow) and 6 (wrist yaw) are always parallel for a human hand and joints 5 (axial roll), 6 (wrist yaw), 7 (wrist pitch) together can form a spherical joint.

\subsubsection*{DH parameters}
DH parameters for the frames shown in Figure \ref{fig:q4.2} are as follows,
\begin{center}
\begin{tabular}{ c | c c c c }
 \hline
 $F_i \to F_j$ & $\theta$ & d & r & $\alpha$ \\
 \hline
 $0 \to 1$ & $q_1$    & $0$       & $0$     &   $90^{\circ}$   \\
 $1 \to 2$ & $q_2$    & $0$       & $0$     &   $-90^{\circ}$  \\
 $2 \to 3$ & $q_3$    & $0$       & $l_1$   &   $90^{\circ}$   \\
 $3 \to 4$ & $q_4$    & $0$       & $l_2$   &   $0^{\circ}$   \\
 $4 \to 5$ & $q_5$    & $0$       & $0$     &   $-90^{\circ}$  \\
 $5 \to 6$ & $q_6$    & $0$       & $0$     &   $-90^{\circ}$   \\
 $6 \to 7$ & $q_7$    & $l_3$     & $0$     &   $0^{\circ}$    \\
 \hline
\end{tabular}
\end{center}

\subsubsection*{Transformation from $F_0$ to $F_7$}
\[
  T_{07} \equiv
  T_{01}
  * T_{12}
  * T_{23}
  * T_{34}
  * T_{45}
  * T_{56}
  * T_{67}
\]
$T_{01}, T_{12}, T_{23}, T_{34}, T_{45}, T_{45}, T_{67}$ can be constructed using DH parameters.
Given frame i and frame j with DH parameters [ $\theta$, d, r, $\alpha$ ] the transformation matrix is
\[
  T_{i,j} \equiv Rot_{z,\theta} * Trans_{z, d} * Trans_{x, r} * Rot_{x, \alpha}
\]
\fromlectures
Also observe,
\[
  Rot_{z, 0^{\circ}} \equiv \rz{0^{\circ}} \equiv I
\]
\[
  Trans_{z, 0} \equiv \trans{0}{0}{0} \equiv I
\]
\[
  Trans_{x, 0} \equiv \trans{0}{0}{0} \equiv I
\]
\[
  Rot_{x, 0^{\circ}} \equiv \rx{0^{\circ}} \equiv I
\]
Consider $T_{01}$,
\[
  T_{0,1} \equiv Rot_{z,q_1} * Trans_{z, 0} * Trans_{x, 0} * Rot_{x, 90^{\circ}}
\]
\[
  T_{0,1} \equiv Rot_{z,q_1} * I * I * Rot_{x, 90^{\circ}}
\]
\[
  T_{0,1} \equiv Rot_{z,q_1} * Rot_{x, 90^{\circ}}
\]
\[
  T_{0,1} \equiv \rz{q_1} * \rx{90^{\circ}}
\]
\[
  T_{0,1} \equiv
  \rz{q_1}
  *
  \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{0,1} \equiv
  \begin{bmatrix} cq_1 & 0 & sq_1 & 0 \\ sq_1 & 0 & -cq_1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Consider $T_{12}$,
\[
  T_{1,2} \equiv Rot_{z,q_2} * Trans_{z, 0} * Trans_{x, 0} * Rot_{x, -90^{\circ}}
\]
\[
  T_{1,2} \equiv Rot_{z,q_2} * I * I * Rot_{x, -90^{\circ}}
\]
\[
  T_{1,2} \equiv Rot_{z,q_2} * Rot_{x, -90^{\circ}}
\]
\[
  T_{1,2} \equiv \rz{q_2} * \rx{-90^{\circ}}
\]
\[
  T_{1,2} \equiv
  \rz{q_2}
  *
  \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{1,2} \equiv
  \begin{bmatrix} cq_2 & 0 & -sq_2 & 0 \\ sq_2 & 0 & cq_2 & 0 \\ 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Consider $T_{23}$,
\[
  T_{2,3} \equiv Rot_{z,q_3} * Trans_{z, 0} * Trans_{x, l_1} * Rot_{x, 90^{\circ}}
\]
\[
  T_{2,3} \equiv Rot_{z,q_3} * I * Trans_{x, l_1} * Rot_{x, 90^{\circ}}
\]
\[
  T_{2,3} \equiv Rot_{z,q_3} * Trans_{x, l_1} * Rot_{x, 90^{\circ}}
\]
\[
  T_{2,3} \equiv \rz{q_3} * \trans{l_1}{0}{0} * \rx{90^{\circ}}
\]
\[
  T_{2,3} \equiv
  \rz{q_3}
  *
  \trans{l_1}{0}{0}
  *
  \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{2,3} \equiv
  \rz{q_3}
  *
  \begin{bmatrix} 1 & 0 & 0 & l_1 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  T_{2,3} \equiv
  \begin{bmatrix} cq_3 & 0 & sq_3 & l_1cq_3 \\ sq_3 & 0 & -cq_3 & l_1sq_3 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Consider $T_{34}$,
\[
  T_{3,4} \equiv Rot_{z,q_4} * Trans_{z, 0} * Trans_{x, l_2} * Rot_{x, 0^{\circ}}
\]
\[
  T_{3,4} \equiv Rot_{z,q_4} * I * Trans_{x, l_2} * I
\]
\[
  T_{3,4} \equiv Rot_{z,q_4} * Trans_{x, l_2}
\]
\[
  T_{3,4} \equiv \rz{q_4} * \trans{l_2}{0}{0}
\]
\[
  T_{3,4} \equiv
  \begin{bmatrix} cq_4 & -sq_4 & 0 & l_2cq_4 \\ sq_4 & cq_4 & 0 & l_2sq_4 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Consider $T_{45}$,
\[
  T_{4,5} \equiv Rot_{z,q_5} * Trans_{z, 0} * Trans_{x, 0} * Rot_{x, -90^{\circ}}
\]
This form is similar to $T_{12}$. So we can directly use the final form of $T_{12}$ with $q_1$ substituted by $q_5$.
\[
  T_{4,5} \equiv
  \begin{bmatrix} cq_5 & 0 & -sq_5 & 0 \\ sq_5 & 0 & cq_5 & 0 \\ 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Consider $T_{56}$,
\[
  T_{5,6} \equiv Rot_{z,q_6} * Trans_{z, 0} * Trans_{x, 0} * Rot_{x, -90^{\circ}}
\]
This form is similar to $T_{12}$. So we can directly use the final form of $T_{12}$ with $q_1$ substituted by $q_6$.
\[
  T_{5,6} \equiv
  \begin{bmatrix} cq_6 & 0 & -sq_6 & 0 \\ sq_6 & 0 & cq_6 & 0 \\ 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Consider $T_{67}$,
\[
  T_{6,7} \equiv Rot_{z,q_7} * Trans_{z, l_3} * Trans_{x, 0} * Rot_{x, 0^{\circ}}
\]
\[
  T_{6,7} \equiv Rot_{z,q_7} * Trans_{z, l_3} * I * I
\]
\[
  T_{6,7} \equiv Rot_{z,q_7} * Trans_{z, l_3}
\]
\[
  T_{6,7} \equiv \rz{q_7} * \trans{0}{0}{l_3}
\]
\[
  T_{6,7} \equiv
  \begin{bmatrix} cq_7 & -sq_7 & 0 & 0 \\ sq_7 & cq_7 & 0 & 0 \\ 0 & 0 & 1 & l_3 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Consider $T_{07}$,
\[
  T_{07} \equiv
  T_{01}
  * T_{12}
  * T_{23}
  * T_{34}
  * T_{45}
  * T_{56}
  * T_{67}
\]
Substituting $T_{01}, T_{12}$,
\[
  T_{07} \equiv
  \begin{bmatrix} cq_1 & 0 & sq_1 & 0 \\ sq_1 & 0 & -cq_1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \begin{bmatrix} cq_2 & 0 & -sq_2 & 0 \\ sq_2 & 0 & cq_2 & 0 \\ 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * T_{23}
  * T_{34}
  * T_{45}
  * T_{56}
  * T_{67}
\]
\[
  T_{07} \equiv
  \begin{bmatrix} cq_1cq_2 & -sq_1 & -cq_1sq_2 & 0 \\ sq_1cq_2 & cq_1 & -sq_1sq_2 & 0 \\ sq_2 & 0 & cq_2 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * T_{23}
  * T_{34}
  * T_{45}
  * T_{56}
  * T_{67}
\]
Substituting $T_{23}, T_{34}$,
\[
  T_{07} \equiv
  \begin{bmatrix} cq_1cq_2 & -sq_1 & -cq_1sq_2 & 0 \\ sq_1cq_2 & cq_1 & -sq_1sq_2 & 0 \\ sq_2 & 0 & cq_2 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \begin{bmatrix} cq_3 & 0 & sq_3 & l_1cq_3 \\ sq_3 & 0 & -cq_3 & l_1sq_3 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \begin{bmatrix} cq_4 & -sq_4 & 0 & l_2cq_4 \\ sq_4 & cq_4 & 0 & l_2sq_4 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * T_{45}
  * T_{56}
  * T_{67}
\]
Multiplying $T_{23}, T_{34}$,
\[
  T_{07} \equiv
  \begin{bmatrix} cq_1cq_2 & -sq_1 & -cq_1sq_2 & 0 \\ sq_1cq_2 & cq_1 & -sq_1sq_2 & 0 \\ sq_2 & 0 & cq_2 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \begin{bmatrix} cq_3cq_4 & -cq_3sq_4 & sq_3 & l_2cq_3cq_4 + l_1cq_3 \\ sq_3cq_4 & -sq_3sq_4 & -cq_3 & l_2sq_3cq_4 + l_1sq_3 \\ sq_4 & cq_4 & 0 & l_2sq_4 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * T_{45}
  * T_{56}
  * T_{67}
\]
Substituting $T_{45}, T_{56}$,
\[
  T_{07} \equiv
  A
  * \begin{bmatrix} cq_5 & 0 & -sq_5 & 0 \\ sq_5 & 0 & cq_5 & 0 \\ 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \begin{bmatrix} cq_6 & 0 & -sq_6 & 0 \\ sq_6 & 0 & cq_6 & 0 \\ 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * T_{67}
\]
\[
  A \equiv
  \begin{bmatrix} cq_1cq_2 & -sq_1 & -cq_1sq_2 & 0 \\ sq_1cq_2 & cq_1 & -sq_1sq_2 & 0 \\ sq_2 & 0 & cq_2 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \begin{bmatrix} cq_3cq_4 & -cq_3sq_4 & sq_3 & l_2cq_3cq_4 + l_1cq_3 \\ sq_3cq_4 & -sq_3sq_4 & -cq_3 & l_2sq_3cq_4 + l_1sq_3 \\ sq_4 & cq_4 & 0 & l_2sq_4 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Multiplying $T_{45}, T_{56}$,
\[
  T_{07} \equiv
  A
  * \begin{bmatrix} cq_5cq_6 & sq_5 & -cq_5sq_6 & 0 \\ sq_5cq_6 & -cq_5 & -sq_5sq_6 & 0 \\ -sq_6 & 0 & -cq_6 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * T_{67}
\]
\[
  A \equiv
  \begin{bmatrix} cq_1cq_2 & -sq_1 & -cq_1sq_2 & 0 \\ sq_1cq_2 & cq_1 & -sq_1sq_2 & 0 \\ sq_2 & 0 & cq_2 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \begin{bmatrix} cq_3cq_4 & -cq_3sq_4 & sq_3 & l_2cq_3cq_4 + l_1cq_3 \\ sq_3cq_4 & -sq_3sq_4 & -cq_3 & l_2sq_3cq_4 + l_1sq_3 \\ sq_4 & cq_4 & 0 & l_2sq_4 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Substituting $T_{67}$,
\[
  T_{07} \equiv
  A
  * \begin{bmatrix} cq_5cq_6 & sq_5 & -cq_5sq_6 & 0 \\ sq_5cq_6 & -cq_5 & -sq_5sq_6 & 0 \\ -sq_6 & 0 & -cq_6 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \begin{bmatrix} cq_7 & -sq_7 & 0 & 0 \\ sq_7 & cq_7 & 0 & 0 \\ 0 & 0 & 1 & l_3 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  A \equiv
  \begin{bmatrix} cq_1cq_2 & -sq_1 & -cq_1sq_2 & 0 \\ sq_1cq_2 & cq_1 & -sq_1sq_2 & 0 \\ sq_2 & 0 & cq_2 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  * \begin{bmatrix} cq_3cq_4 & -cq_3sq_4 & sq_3 & l_2cq_3cq_4 + l_1cq_3 \\ sq_3cq_4 & -sq_3sq_4 & -cq_3 & l_2sq_3cq_4 + l_1sq_3 \\ sq_4 & cq_4 & 0 & l_2sq_4 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
Multiplying,
\[
  T_{07} \equiv
  A
  \begin{bmatrix} c_5c_6c_7 + s_5s_7 & -c_5c_6s_7 + s_5c_7 & -c_5s_6 & -l_3c_5s_6 \\ s_5c_6c_7 - c_5s_7 & -s_5c_6s_7 - c_5c_7 & -s_5s_6 & -l_3s_5s_6 \\ -s_6c_7 & s_6s_7 & -c_6 & -l_3c_6 \\ 0 & 0 & 0 & 1 \end{bmatrix}
\]
\[
  A \equiv
  \begin{bmatrix}
    cq_1cq_2 & -sq_1 & -cq_1sq_2 & 0 \\
    sq_1cq_2 & cq_1 & -sq_1sq_2 & 0 \\
    sq_2 & 0 & cq_2 & 0 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
  *
  \begin{bmatrix}
    cq_3cq_4 & -cq_3sq_4 & sq_3 & l_2cq_3cq_4 + l_1cq_3 \\
    sq_3cq_4 & -sq_3sq_4 & -cq_3 & l_2sq_3cq_4 + l_1sq_3 \\
    sq_4 & cq_4 & 0 & l_2sq_4 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
Multiplying,
\[
  T_{07} \equiv
  A
  \begin{bmatrix}
    c_5c_6c_7 + s_5s_7 & -c_5c_6s_7 + s_5c_7 & -c_5s_6 & -l_3c_5s_6 \\
    s_5c_6c_7 - c_5s_7 & -s_5c_6s_7 - c_5c_7 & -s_5s_6 & -l_3s_5s_6 \\
    -s_6c_7 & s_6s_7 & -c_6 & -l_3c_6 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
\[
  A \equiv
  \begin{bmatrix}
    c_1c_2c_3c_4 - s_1s_3c_4 - c_1s_2s_4 & -c_1c_2c_3s_4 + s_1s_3s_4 - c_1s_2c_4 & c_1c_2s_3 + s_1c_3 & t_1 \\
    s_1c_2c_3c_4 + c_1s_3c_4 - s_1s_2s_4 & -s_1c_2c_3c_4 - c_1s_3s_4 - s_1s_2c_4 & s_1c_2s_3 - c_1c_3 & t_2 \\
    s_2c_3c_4 + c_2s_4 & -s_2c_3c_4 + c_2c_4 & s_2s_3 & s_2(l_2c_3c_4 + l_1c_3) + c_2(l_2s_4) \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
\[
  t_1 \equiv  c_1c_2(l_2c_3c_4 + l_1c_3) -s_1(l_2s_3s_4 + l_1s_3) -c_1s_2(l_2s_4)
\]
\[
  t_1 \equiv s_1c_2(l_2c_3c_4 + l_1c_3) + c_1(l_2s_3s_4 + l_1s_3) -s_1s_2(l_2s_4)
\]
Therefore $T_{07}$,
\[
  T_{07} \equiv A * B
\]
\[
  A \equiv
  \begin{bmatrix}
    c_1c_2c_3c_4 - s_1s_3c_4 - c_1s_2s_4 & -c_1c_2c_3s_4 + s_1s_3s_4 - c_1s_2c_4 & c_1c_2s_3 + s_1c_3 & t_1 \\
    s_1c_2c_3c_4 + c_1s_3c_4 - s_1s_2s_4 & -s_1c_2c_3c_4 - c_1s_3s_4 - s_1s_2c_4 & s_1c_2s_3 - c_1c_3 & t_2 \\
    s_2c_3c_4 + c_2s_4 & -s_2c_3c_4 + c_2c_4 & s_2s_3 & s_2(l_2c_3c_4 + l_1c_3) + c_2(l_2s_4) \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
\[
  t_1 \equiv  c_1c_2(l_2c_3c_4 + l_1c_3) -s_1(l_2s_3s_4 + l_1s_3) -c_1s_2(l_2s_4)
\]
\[
  t_1 \equiv s_1c_2(l_2c_3c_4 + l_1c_3) + c_1(l_2s_3s_4 + l_1s_3) -s_1s_2(l_2s_4)
\]
\[
  B \equiv
  \begin{bmatrix}
    c_5c_6c_7 + s_5s_7 & -c_5c_6s_7 + s_5c_7 & -c_5s_6 & -l_3c_5s_6 \\
    s_5c_6c_7 - c_5s_7 & -s_5c_6s_7 - c_5c_7 & -s_5s_6 & -l_3s_5s_6 \\
    -s_6c_7 & s_6s_7 & -c_6 & -l_3c_6 \\
    0 & 0 & 0 & 1
  \end{bmatrix}
\]
where $c_i \equiv cos(q_i), s_i \equiv sin(q_i)$.
\end{document}