\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath,amsfonts,amssymb,amsthm,dsfont}

\def\F{\mathcal{F}}
\def\G{\mathcal{G}}
\def\I{\mathcal{I}}
\def\y{\mathcal{\mu}}
\def\m{m}
\def\card{\mathrm{card}}

\def\diam{\mathrm{diam}}

\def\det{\mathrm{det}}

\def\tr{\mathrm{tr}}

\def\cov{\mathrm{cov}}
\begin{document}

\section{Entropy formulas}
\begin{description}
	$\G$ - family of all normal distributions \newline
	$\G_A$ - where $A$ proper matrix(square, symetric positvelly defined) is a subfamilly of $\G$ which covariance equals $A$
		\newline
	$\G_{(\cdot \I)} = \cup_{r \in \mathbb{R} - \{0\}} \G_{r\cdot \I}

\end{description}

\begin{table}\centering

\begin{tabular}{||l|l|l||} \hline \hline

$\F$ & $\Sigma_{\F}(\y)$ & $H^{\times}(\y\|\F)$ \\[0.5ex]

\hline \hline

$\G_{\Sigma}$ & $\Sigma$ & $\frac{N}{2} \ln(2\pi)+\frac{1}{2}\tr(\Sigma^{-1}\Sigma_{\y})+\frac{1}{2}\ln \det(\Sigma)$

\\[0.5ex] \hline

$\G_{r\I}$ & $r\I$ &

$\frac{N}{2}\ln(2\pi)+\frac{1}{2r}\tr(\Sigma_{\y})+\frac{N}{2}\ln r$ \\[0.5ex] \hline

$\G_{(\cdot\I)}$ & $\frac{\tr(\Sigma_{\y})}{N} \I$ & $\frac{N}{2}\ln(2\pi e/N)+\frac{N}{2}\ln (\tr \Sigma_\y)$ \\[0.5ex] \hline

$\G_{\mathrm{diag}}$ & $\mathrm{diag}(\Sigma_{\y})$ & $\frac{N}{2}\ln(2\pi e)+\frac{1}{2}\ln(\det(\mathrm{diag}(\Sigma_\y)))$ \\[0.5ex]

\hline

%$\G_{\det=A}$ & $(A/\det \Sigma_{\mu})^{1/N}

%\Sigma_{\mu}$ & $\frac{N}{2} \ln(2\pi)+\frac{N}{2}(\det \Sigma_{\mu}/A)^{1/N}+\frac{1}{2}\ln(A)$ \\[0.5ex] \hline

$\G$ & $\Sigma_{\y}$ & $\frac{N}{2}\ln(2\pi e)+\frac{1}{2}\ln \det(\Sigma_{\y})$ \\[0.5ex] \hline \hline

\end{tabular}

\caption{Table of cross-entropy formulas with respect to Gaussian subfamilies.}

\label{tab1:cec}

\end{table}

\section{Cluster formulas}
Assume we we have a cluster $A$ with parameters $\l,\m,\Sigma$ and we add to this cluster point $y$ we will get a new cluter $A_{+y}$ with paramter given by formulas:
$$

\begin{array}{rcl}

\l_{+y} & = & l+1, \\[1ex]

\m_{+y} & = & \frac{l\m+y}{l+1}, \\[1ex]

\Sigma_{+y} & = & \frac{l}{l+1}[\Sigma+\frac{1}{l+1}(\m-y)(\m-y)^T].

\end{array}

$$

Let's assume we'll substract point $ y $ from cluster $A$ our new cluster will have parameters given by formula :

$$

\begin{array}{rcl}

l_{-y} & = & l-1, \\[1ex]

\m_{-y} & = & \frac{l}{l-1}\m-\frac{1}{l-1} y, \\[1ex]

\Sigma_{-y} & = & \frac{l}{l-1}[\Sigma-\frac{1}{l-1}(\m-y)(\m-y)^T], .

\end{array}

$$

\end{document}


\documentclass[11pt]{etk-article}
\usepackage{pstool}
\usepackage{etk-bib}
\pdfmetadata{}{}{}{}
\externaldocument[FD:]{operator_discretization_finite_differences}

\begin{document}
\title{Solving HJBE with Finite Differences}
\author{Jesse Perla\\UBC}
\date{\today}
\maketitle
 Here, we expand on details for how to discretize HJBE with a control of the drift.\footnote{See \url{operator_discretization_finite_differences.pdf} for more details on the discretization of a linear diffusion operator with finite-differences, and general notation (with equation numbers in that document prefaced by \textit{FD}). Thanks to Sari Wang for superb Research Assistance.}  In particular, this set of notes will focus on solving the neoclassical growth model first with a deterministic and then a stochastic TFP.

 \section{Neoclassical Growth}
 This solves the simple deterministic neoclassical growth model.
 \subsection{Value Function with Capital Reversibility}

 Take a standard neoclassical growth model with capital $k$, consumption $c$, production $f(k)$, utility $u(c)$, depreciation rate $\delta$, and discount rate $\rho$.\footnote{
 This builds on \url{http://www.princeton.edu/~moll/HACTproject/HACT_Additional_Codes.pdf} Section 2.2, with code in \url{http://www.princeton.edu/~moll/HACTproject/HJB_NGM_implicit.m}}
The law of motion for capital in this setup is,
\begin{align}
	\D[t] k(t) &= f(k) - \delta k - c\label{eq:lom-capital}\\
	\intertext{With this, the standard HJBE for the value of capital $k$ is,}
	\rho v(k) &= \max_{c}\set{u(c) + \left(f(k) - \delta k - c\right)v'(k)}\label{eq:HJBE-neoclassical-growth}
	\intertext{Assume an interior $c$ and envelope conditions, then taking the FOC of the choice is,}
	u'(c) &= v'(k)
	\intertext{Assume a functional form $u(c) = \frac{c^{1-\gamma}}{1-\gamma}$ for the utility and $f(k)=Ak^\alpha$ for production. Then the FOC can be inverted such that}
	c &= \left(v'(k) \right)^{-\frac{1}{\gamma}}\label{eq:c-neoclassical-growth}
	\intertext{And,}
	u(c) &= \frac{\left(v'(k)\right)^{-\frac{1-\gamma}{\gamma}}}{1-\gamma}\label{eq:u-c-neoclassical-growth}
\intertext{If \cref{eq:c-neoclassical-growth,eq:u-c-neoclassical-growth} were substituted back into \cref{eq:HJBE-neoclassical-growth}, we would have a nonlinear ODE in just $k$.  Define the following, which will be important in the finite difference scheme}
	\mu(k) &\equiv f(k) - \delta k - c\label{eq:mu-neoclassical-growth}
	\intertext{Then with \cref{eq:c-neoclassical-growth} this can be defined as a function of the $v(\cdot)$ function,}
	\mu(k;v) &\equiv f(k) - \delta k - \left(v'(k) \right)^{-\frac{1}{\gamma}}\label{eq:mu-v-neoclassical-growth}
	\intertext{Then with \cref{eq:HJBE-neoclassical-growth,eq:mu-v-neoclassical-growth,eq:u-c-neoclassical-growth}}
	\rho v(k) &= \frac{\left(v'(k)\right)^{-\frac{1-\gamma}{\gamma}}}{1-\gamma} + \mu(k;v) v'(k)\label{eq:HJBE-neoclassical-growth-mu}
\end{align}
The HJBE \cref{eq:HJBE-neoclassical-growth-mu} is a nonlinear ODE in $v(k)$.

\paragraph{Steady State}  The goal is for the finite-difference scheme to find the steady-state on its own.  However, in this example we have an equation to verify the solution:
\begin{align}
k^* &= \left(\frac{\alpha A}{\rho + \delta}\right)^{\frac{1}{1-\alpha}}\label{eq:steady-state-k}\\
c^* &= A \left(k^*\right)^{\alpha} - \delta  \left(k^*\right)^{\alpha}\label{eq:steady-state-c}
\end{align}
\subsection{Finite Differences and Discretization}
In order to solve this problem, we will need to use the appropriate ``upwind'' direction for the finite differences given the sign of the drift in \cref{eq:mu-v-neoclassical-growth}.
\paragraph {Two basic approaches:}
\begin{itemize}
	\item Fix the nonlinear $v'(k)$ function so that the operator becomes linear, solve it as a sparse linear system, and then iterate on the $v(k)$ solution.
	\item Solve it directly as a nonlinear system of equations.
\end{itemize}

\paragraph {Setup}
\begin{itemize}
\item Define a uniform grid with $I$ discrete points  $\set{k_i}_{i=1}^I$,  for some small $k_1 \in (0, k^*)$ and large $k_I > k^*$, with distance between grid points $\Delta \equiv k_i - k_{i-1}$ for all $i$. After discretizing, we will denote the grid with the variable name, i.e. $k \equiv \set{k_i}_{i=1}^I$.
Further define the notations $\underline{k} \equiv k_1$, $\overline{k} \equiv k_I$ and $v_i \equiv v(k_i)$ for simplicity.
\item When we discretize a function, use the function name without arguments to denote the vector.  i.e. $v(k)$ discretized on a grid $\set{k_i}_{i=1}^{I}$ is $v \equiv \set{v(k_i)}_{i=1}^I \in \R^I$.
\item When referring to a variable $\mu$, define the notation $\mu^{-} \equiv \min\set{\mu,0}$ and $\mu^{+} \equiv \max\set{\mu,0}$. This can apply to vectors as well. For example, $\mu_i^{-} = \mu_i$ if $\mu_i < 0$ and $0$ if $\mu_i > 0$, and $\mu^{-} \equiv \set{\mu^{-}_i}_{i=1}^{I}$.

\item To discretize the derivative at $k_i$, consider both forwards and backwards differences,
\begin{align}
	v'_F(k_i) &\approx \frac{v_{i+1} - v_i}{\Delta}\label{eq:forward-diff}\\
	v'_B(k_i) &\approx \frac{v_i - v_{i-1}}{\Delta}\label{eq:backward-diff}
	\intertext{subject to the state constraints}
		v'_{F}(\overline{k}) = (f(\overline{k}) - \delta \overline{k})^{-\gamma}\label{eq:forward-constraint}\\
		v'_{B}(\underline{k}) = (f(\underline{k}) - \delta \underline{k})^{-\gamma}\label{eq:backward-constraint}
	\intertext {Our finite difference approximation to the HJB equation \cref{eq:HJBE-neoclassical-growth-mu} is then}
	\rho v(k_i) &= \frac{\left(v'(k_i)\right)^{-\frac{1-\gamma}{\gamma}}}{1-\gamma} + \mu(k_i;v) v'(k_i)
\end{align}
\end{itemize}
 Following the ``upwind scheme'', we will use the forwards approximation whenever there is a positive drift above the steady state level of capital $k^*$, and the backwards approximation when below $k^*$.

\subsection{Iterating on a Linear System of Equations}
This section solves the problem through a series of iterations on a linear system of equations.
\paragraph{Iterative Method }Pick an $h$, which is used in the iterative process.\footnote{\textbf{TODO:} I believe this is the Howard algorithm.  In effect, I think that this is like using an explicit time step in a PDE, where we are forward iterating fixing the policy?  Because of this, there is a high likelihood that it is not unconditionally stable.}  We first make an initial guess $v^0 = (v_1^0, \dots v_I^0)$. Then for each consecutive iteration $n = 0, 1, 2, ...$,  update $v^n$ using the following equation
\begin{align}
\frac{v_i^{n+1}-v_i^{n}}{h} + \rho v_i^{n+1} = u(c_i^n) + (v_i^{n+1})^{'}  \underbrace{\left[f(k_i) - \delta k_i - c_i^n\right]}_{\equiv \mu_i}\label{eq:fd-approx-capital}
\end{align}
Using both the forwards and backwards difference approximations \cref{eq:forward-diff} and \cref{eq:backward-diff}, find consumption levels $c_F^n$ and $c_B^n$ as given by \cref{eq:c-neoclassical-growth} and compute savings,
\begin{align}
\mu^n_{i,F} = f(k_i) - \delta k_i - \left({v_F^n}'(k_i) \right)^{-\frac{1}{\gamma}}\\
\mu^n_{i,B} = f(k_i) - \delta k_i - \left({v_B^n}'(k_i) \right)^{-\frac{1}{\gamma}}
\end{align}

Depending upon the sign of the drift, a choice is made between using forward or backward differences. Let $\bold{1}_{\{ \cdot \}}$ be an indicator function\footnote{For now, assume that if the case $\mu_{F}>0$  and $\mu_{B}<0$ should arise, take $\bold{1}_{\{ \mu_{i,F} > 0\}} $ to be 1 and $\bold{1}_{ \{\mu_{i,B} < 0\}}\ $ to be 0.}and  $\bar{v_i}' $ be the derivative at the steady state, given in this example by $\bar{v_i}' = (f(k_i)-\delta k_i)^{-\gamma}$. Then the approximation of the derivative is
\begin{align}
{v_i^n}'={v_{i,F}^n}'\bold{1}_{\{ \mu_{i,F} > 0\}}+{v_{i,B}^n}\bold{1}_{\{ \mu_{i,B}<0 \}}+{\bar{v}_i^n}'\bold{1}_{\{ \mu_{i,F}<0<\mu_{i,B} \}}\label{eq:dv-upwind}
\end{align}

Define the vectors $X, Y, Z \in \R^{I} $ such that
\begin{align}
	X &= -\frac {\mu^{-} _B}{\Delta}\label{eq:X-delta} \\
	Y &= -\frac {\mu^{+} _F}{\Delta} + \frac {\mu^{-} _B}{\Delta}\label{eq:Y-delta} \\
	Z &= \frac {\mu^{+} _F}{\Delta}\label{eq:Z-delta}
\end{align}
%
%%For algebraic simplicity later on, multiply every term by  $\Delta$ and obtain
%\begin{align}
%	X &= -({\mu^{n} _B})^{-}\label{eq:X} \\
%	Y &= -({\mu^{n} _F})^{+} + ({\mu^{n} _B})^{-}\label{eq:Y} \\
%	Z &= ({\mu^{n} _F})^{+}\label{eq:Z}
%\end{align}

With these, construct the sparse matrix $A^n$
\begin{align}
A^n &\equiv \begin{bmatrix}
Y_1 & Z_1 & 0 & \cdots & \cdots & \cdots & 0 \\
X_2 & Y_2 & Z_2 & 0 & \ddots& & \vdots \\
0 & \ddots & \ddots & \ddots & \ddots &  & \vdots \\
\vdots & &\ddots & \ddots & \ddots & \ddots  & \vdots \\
\vdots & & & \ddots & X_{I-1} & Y_{I-1}  & Z_{I-1} \\
0 & \cdots & \cdots & \cdots & 0 & X_I & Y_I\\
\end{bmatrix}\in\R^{I\times I}\label{eq:A}
\end{align}

By substituting the approximation ${v^{n}}'$ found in \cref{eq:dv-upwind} into equation \cref{eq:c-neoclassical-growth}, define the utility vector
\begin{align}
u(c^n) &\equiv u\left(\left({v^n}'\right)^{-\frac{1}{\gamma}}\right)\\
\intertext{We can then write as a system of equations in matrix form and solve for $v^{n+1}$}
B^n &\equiv \left(\rho + \frac{1}{h}\right)I - A^n\\
b^n &\equiv u(c^n) + \frac{v^n}{h}\\
B^{n}v^{n+1} &= {b^n}
\end{align}

When $v^{n+1}$ is sufficiently close in value to $v^n$, the algorithm is complete. Otherwise, update the value of $v^n$ and repeat the previous steps for the next iteration. Keep in mind that the solution may not be unconditionally stable for an arbitrary $h$.

\subsection{Nonlinear System of Equations}
This section will focus on solving the problem directly as a nonlinear system of equations. Returning to equation \cref{eq:HJBE-neoclassical-growth-mu}, we have
\begin{align}
\rho v_i &= \frac{\left(v'_i\right)^{-\frac{1-\gamma}{\gamma}}}{1-\gamma} + \mu(k_i;v) v'_i
\end{align}
for every $k_i$, $i=1,2,...I$.

\bibliography{etk-references}

\end{document}


\subsubsection{Generalized NFW (GNFW) Model}
\label{sec:gnfw}
In the context of the pressure models of \cite{nagai2007}, hereafter N07, the generalized NFW profile is given by

\begin{equation}
p(x_g) = {p_0\over{(\c500 x_g)^\gamma\left[1 + (c_{\rm 500}x_g)^\alpha\right]^{(\beta-\gamma)/\alpha}}}
\label{eq:gnfw}
\end{equation}

where $c_{\rm 500}$ is a dimensionless concentration parameter and $x_g
= r/R_{\rm 500}$.

In \climax, the generalized NFW model is implemented as a
dimensionless shape function that can be used to fit cluster profiles
in any units:

\begin{equation}
p(x) = {1\over{x^\gamma\left[1 + x^\alpha\right]^{(\beta-\gamma)/\alpha}}}
\end{equation}

where $x = r/r_c$.  In the context of the GNFW pressure models,
therefore, the $r_c$ returned by \climax\ (actually $\theta_c$), is
equivalent to $r_c = \R500/\c500$.

The generalized NFW model can be used in \climax\ by
invoking \code{addmodel} with \code{type = gnfwmodel}.

From self-similarity arguments (see \S\ref{sec:ss}), the pressure
normalization (see Eq~\ref{eq:ytop}) of a cluster can be related to
its mass via Eq~\ref{eq:arnaud}.  In \climax\, \code{m500} can
therefore also be used with any GNFW model as the primary variable,
given a cosmology.

\subsubsection{Nagai07 GNFW Model}

N07 find that a good description of high-$T_X$ \chandra\
clusters can be fit with a model with $p_0 = 3.3$, $c_{\rm 500} = 1.8$
and $(\alpha,\beta,\gamma) = (1.3, 4.3, 0.7)$.

This specialization of the GNFW model can be used in \climax\ by
invoking \code{addmodel} with \code{type = nagai07model}.

\subsubsection{Arnaud GNFW Model}

\cite{arnaud2010}, hereafter A10, determine that $p_0 =
8.403\,h^{-3/2}_{70}$, $c_{\rm 500} = 1.17$ and $(\alpha,\beta,\gamma)
= (1.0510, 5.4905, 0.3081)$ yield the best fit to REXCESS clusters.

This specialization of the GNFW model can be used in \climax\ by
invoking \code{addmodel} with \code{type = arnaudmodel}.

A10 also determine the normalization of the pressure model fits to be

\begin{eqnarray}
P(r) &=&
1.65\times10^{-3}h(z)^{8/3}\left[{\M500\over{3\times10^{14}h^{-1}_{70}\Msolar}}\right]^{2/3+\alpha_P+\alpha^\prime_P(x)}\\\nonumber
&\times& p_{\rm A}(x_g) \,h^2_{70}\,{\rm keV\,cm^{-3}},
\end{eqnarray}

where $p_{\rm A}(x_g)$ is the GNFW profile with the Arnaud et al fit
parameters given above, $\alpha_P = 0.12$, and

\begin{equation}
\alpha^\prime_P(x_g) = 0.1 - (\alpha_P + 0.1){{(x_g/0.5)^3}\over{1 + (x_g/0.5)^3}}
\end{equation}

The small second-order $x_g$-dependent term represents a departure
from self-similarity, and also a significant increase in computational
overhead, and it is presently ignored in \climax.


\section{Development of data processing methods}
\label{data_processing}

High-quality spectra are needed for advanced multivariate statistic methods
used for the measurement analysis.
It is essential to have sufficiently sensitive apparatus which can provide a
reliable signal.
However, raw Raman spectra, which come from the measurement, usually contain
many components, and only one of them belongs to the analyte signal.
The following sections describe the process of transforming the raw spectrum
to the spectrum ready for the advanced result analysis, analysis
of spectra of multicomponent system, minimization algorithm used for the
data analysis, and band intensity estimation overview.

Among the major artifacts observed in Raman spectra is signal caused by cosmic
rays, which is characteristic by sharp lines usually impacting only a few
pixels of the CCD detector.
\emph{Spycor} program
\parencite{Spycor2018},
was created as part of this thesis and is based on a comparison of consecutive
spectra where it counts on the fact that during macroscopic Raman measurement,
the spectra change only slowly with time and that the consecutive spectra are
similar.

Raman spectra of liquid samples usually consist of many components, including
the signal of the analyte under investigation and the spectrum of the solvent.
In the case of absorbing analyte, the spectrum
is acquired from the near proximity of the sample cell wall, and so it
usually contains a signal of quartz.
The resulting spectrum can also contain unspecific background from different
sources like fluorescence from residuals from chemical synthesis and elastic
scattering of light from different light sources in the laboratory.

Therefore the background subtraction procedure took several steps.
Each spectrum was acquired as a series of frames, and all frames were stored
for further analysis.
The spectrum of solvent, buffer, and sample cell together with the intensity
normalization to the subtracted buffer signal was performed on each frame in
the first step using the \emph{Bgcor} program
\parencite{Bgcor2017}
(written in \cite{Matlab}),
developed as part of this thesis. The unspecific background was then removed
from each spectrum using background subtraction in frames decomposed to
spectral components by PCA
\parencite{Palacky2011}.
The thirds step consisted of extrapolation to zero time, where PCA decomposed
the frames to spectral components, and the spectrum at zero time
was reconstructed from significant ones.
The last optional step performed only when a series of spectra was
taken, for example, temperature-dependent measurement was to subtract the
background once more using PCA the same way as in step two but currently
applied to the whole series of the extrapolated spectra from the third step.
This section focuses mainly on the first step -- the subtraction of the
solvent, buffer, and sample cell, together with the intensity normalization.

Further, we adopted in this thesis \emph{Principal component analysis} (PCA,
\cite{%
	Wold1987,%
	Malinowski2002%
}),
which reduces the measured spectra to several spectral profiles (loadings) and
scores indicating each profile's portion in the measured spectra.
The scores can then be fitted by a function based on an underlying chemical
model, and for example, thermodynamic parameters for structural transitions
can be estimated in temperature-dependent measurement
\parencite{Nemecek2013}.
But the fit is usually not linear in the system parameters.
Nonlinear minimization is usually much more expensive because it utilizes
iterative methods of searching for the minimum.
They are also susceptible to finding only local minima.
On the other hand, linear least squares regression is “just” solution of a set
of normal equations
\parencite[p.~671]{NumericalRecipes}.
It means that the nonlinear iterative minimization algorithm can be applied
to $b_{k,m}$ values, the $a_{k,l_k}$ values are estimated by linear least
squares for the given $c_k(t_i)$ which are determined by the values of
$b_{k,m}$.
We made use of this fact and divided the fit to two parts where linear
parameters were estimated by linear fit in each step of iterative nonliner
fit which minimized only the nonlinear parameters.
This approach significantly reduced dimension of the nonlinear fit and
improved its numerical stability.

In this thesis, many data treatment approaches needed efficient nonlinear
minimization algorithm (background subtraction, estimation of band intensities,
estimation of parameters of chemical model from spectral series).
The Levenberg-Marquardt method
\parencite{Marquardt1963}
was used as a basis for the minimization algorithm used in this thesis, and
slight modifications were applied to improve its performance.

\label{band_intensities}

One of the important spectroscopic information is the integral intensity of
particular Raman bands from the spectrum.
The band's shape is usually modeled by Gaussian or Lorentzian curves or as
their combination.
The combination of the Lorentzian $\func{L}$ and Gaussian $\func{G}$ curve can
be expressed as
\begin{equation}
	\func{S}(\wn; I_\text{m}, \mu, \sigma) =
		c_\text{L} \cdot \func{L}(\wn; I_\text{m}, \mu, \sigma)
		+ (1 - c_\text{L}) \cdot \func{G}(\wn; I_\text{m}, \mu, \sigma),
	\label{\eqnlabel{band_intensities:single_shape}}
\end{equation}
where $c_\text{L}$ is the Lorentzian curve fraction coefficient, $I_m$ is the
height, $\mu$ is the band position, $\sigma$ is the Gaussian root mean square
width, and $\wn$ is the wavenumber.

Raman bands in a typical Raman spectrum of complex samples are overlapped.
We solved this problem by modeling the band as a combination of more
band-shape functions from
\eqnref{band_intensities:single_shape}
\begin{equation}
	\func{S}(\wn; I_{\text{m},1..n}, \mu_{1..n}, \sigma_{1..n}) =
		\sum_{i = 1}^n = 	\func{S}_i(\wn; I_{\text{m},i}, \mu_i, \sigma_i),
	\label{\eqnlabel{band_intensities:shape}}
\end{equation}
where $n$ is the number of the overlapping bands.


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{parskip}
\usepackage{lscape}
\usepackage{multicol}

\begin{document}
\begin{landscape}
% lose the page number on this page
\thispagestyle{empty}
% start a 2 column page
\begin{multicols}{2}

\section*{(Batch) Gradient Descent}
\begin{equation*}
    J_{train}(\theta) = \frac{1}{2m} \sum_{i=1}^m(h_{\theta}(x^{(i)} - y^{(i)}))^2
\end{equation*}

Perform gradient descent by updating $\theta$ using the derivative of the cost function.

Repeat for every  $j=0,...,n$ \{
\begin{equation*}
    \theta_j := \theta_j - \alpha\frac{1}{m}\sum_{i=1}^m(h_{\theta}(x^{(i)} - y^{(i)}))x_j^{(i)}
\end{equation*}
\}

In large data sets, this summation would have to occur over every training example $m$ at every iteration of the for loop.

% This code block below starts the new column
\vfill
\columnbreak
% This code block above starts the new column

\section*{Stochastic Gradient Descent}
In stochastic gradient descent, the cost is rewritten as the cost of one training example:
\begin{equation*}
    cost(\theta, (x^{(i)}, y^{(i)})) = \frac{1}{2}(h_\theta(x^{(i)})-y^{(i)})^2
\end{equation*}
And thus the cost function is:
\begin{equation*}
    J_{train}(\theta) = \frac{1}{m}\sum_{i=1}^m cost(\theta, (x^{(i)}, y^{(i)}))
\end{equation*}

\begin{enumerate}
    \item Randomly shuffle the dataset (randomly reorder the training examples
    \item Repeat \{
    \begin{description}
    \item for $i=1,...,m$ \{
    \item$\theta_j := \theta_j - \alpha h_\theta(x^{(i)}- y^{(i)})x^{(i)}_j$ \quad \textbf{note:} this is $\frac{\partial}{\partial\theta_j}cost$

    (for every $j=0,...,n$)
    \item\}
    \end{description}
    \item[]\}
\end{enumerate}
In essence, stochastic gradient descent is updating the parameters $\Theta$ (the matrix of $\theta_{0,...,j}$) by stepping through each individual training example rather than summing across all training examples. The outer repeat loop is meant to symbolize that this process may have to be repeated over several iterations (e.g., 1-10 times) until pseudo-convergence.
\end{multicols}
\end{landscape}
\end{document}

tex files/Batch v. Stochastic Gradient Descent.tex


\chapter{Database}
Database questions
\newline

\section{Combine Two Tables} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:combine-two-tables}

\subsubsection{描述}
SQL Schema

Table: Person
\begin{Code}
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| PersonId    | int     |
| FirstName   | varchar |
| LastName    | varchar |
+-------------+---------+
PersonId is the primary key column for this table.
\end{Code}

Table: Address
\begin{Code}
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| AddressId   | int     |
| PersonId    | int     |
| City        | varchar |
| State       | varchar |
+-------------+---------+
AddressId is the primary key column for this table.
\end{Code}

Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:

\subsubsection{outer join}
\begin{Code}
  // Note: Using where clause to filter the records will fail if there is
  // no address information for a person because it will not display the name information.
SELECT
    FirstName, LastName, City, State
FROM
    Person LEFT JOIN Address
ON
    Person.PersonID = Address.PersonID
;
\end{Code}

\section{Second Highest Salary} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:second-highest-salary}

\subsubsection{描述}
SQL Schema

Table: Employee
\begin{Code}
+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+
\end{Code}

For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.
\begin{Code}
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+
\end{Code}

Write a SQL query to get the second highest salary from the Employee table.

\subsubsection{sub-query and LIMIT clause}
\begin{Code}
  // Note: Use a sub-query to handle only one record in this table
SELECT
    (SELECT DISTINCT
        Salary
     FROM
         Employee
     ORDER BY Salary DESC
     LIMIT 1 OFFSET 1) AS SecondHighestSalary
;
\end{Code}

\subsubsection{IFNULL and LIMIT}
\begin{Code}
  // Note: Use a sub-query to handle only one record in this table
SELECT
    IFNULL(
      (SELECT DISTINCT
           Salary
       FROM
           Employee
       ORDER BY Salary DESC
       LIMIT 1 OFFSET 1),
    NULL) AS SecondHighestSalary
;
\end{Code}

\section{Nth Highest Salary} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:nth-highest-salary}

\subsubsection{描述}
SQL Schema

Table: Employee
\begin{Code}
+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+
\end{Code}

For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null.
\begin{Code}
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200                    |
+------------------------+
\end{Code}

Write a SQL query to get the nth highest salary from the Employee table.

\subsubsection{IFNULL and LIMIT}
\begin{Code}
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN

SET N=N-1;
  RETURN (
      # Write your MySQL query statement below.
      select
          ifnull(
              (select distinct
                   salary
               from
                   employee
               order by  salary desc
               limit N,1),
          null)
  );
END
\end{Code}

\section{Rank Scores} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:rank-scores}

\subsubsection{描述}
SQL Schema

\begin{Code}
Create table If Not Exists Scores (Id int, Score DECIMAL(3,2))
Truncate table Scores
insert into Scores (Id, Score) values ('1', '3.5')
insert into Scores (Id, Score) values ('2', '3.65')
insert into Scores (Id, Score) values ('3', '4.0')
insert into Scores (Id, Score) values ('4', '3.85')
insert into Scores (Id, Score) values ('5', '4.0')
insert into Scores (Id, Score) values ('6', '3.65')
\end{Code}

Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks.

Table: Scores
\begin{Code}
+----+-------+
| Id | Score |
+----+-------+
| 1  | 3.50  |
| 2  | 3.65  |
| 3  | 4.00  |
| 4  | 3.85  |
| 5  | 4.00  |
| 6  | 3.65  |
+----+-------+
\end{Code}

For example, given the above Scores table, your query should generate the following report (order by highest score):
\begin{Code}
+-------+---------+
| score | Rank    |
+-------+---------+
| 4.00  | 1       |
| 4.00  | 1       |
| 3.85  | 2       |
| 3.65  | 3       |
| 3.65  | 3       |
| 3.50  | 4       |
+-------+---------+
\end{Code}

\textbf{Important Note}: For MySQL solutions, to escape reserved words used as column names, you can use an apostrophe before and after the keyword. For example `Rank`.

\subsubsection{Dense_Rank}
\begin{Code}
SELECT Score, dense_rank() OVER(ORDER BY Score DESC) AS `Rank` FROM Scores;
\end{Code}

\section{Consecutive Numbers} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:consecutive-numbers}

\subsubsection{描述}
SQL Schema

\begin{Code}
Create table If Not Exists Logs (Id int, Num int)
Truncate table Logs
insert into Logs (Id, Num) values ('1', '1')
insert into Logs (Id, Num) values ('2', '1')
insert into Logs (Id, Num) values ('3', '1')
insert into Logs (Id, Num) values ('4', '2')
insert into Logs (Id, Num) values ('5', '1')
insert into Logs (Id, Num) values ('6', '2')
insert into Logs (Id, Num) values ('7', '2')
\end{Code}

Write an SQL query to find all numbers that appear at least three times consecutively.
Return the result table in any order.
The query result format is in the following example:

Table: Logs
\begin{Code}
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| num         | varchar |
+-------------+---------+
id is the primary key for this table.
\end{Code}

\begin{Code}
Logs table:
+----+-----+
| Id | Num |
+----+-----+
| 1  | 1   |
| 2  | 1   |
| 3  | 1   |
| 4  | 2   |
| 5  | 1   |
| 6  | 2   |
| 7  | 2   |
+----+-----+

Result table:
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1               |
+-----------------+
1 is the only number that appears consecutively for at least three times.
\end{Code}

\subsubsection{multiple table}
\begin{Code}
SELECT DISTINCT
    l1.Num AS ConsecutiveNums
FROM
    Logs l1,
    Logs l2,
    Logs l3
WHERE
    l1.Id = l2.Id - 1
    AND l2.Id = l3.Id - 1
    AND l1.Num = l2.Num
    AND l2.Num = l3.Num
;
\end{Code}

\section{Employees Earning More Than Their Managers} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:employees-earning-more-than-their-managers}

\subsubsection{描述}
SQL Schema

\begin{Code}
Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, ManagerId int)
Truncate table Employee
insert into Employee (Id, Name, Salary, ManagerId) values ('1', 'Joe', '70000', '3')
insert into Employee (Id, Name, Salary, ManagerId) values ('2', 'Henry', '80000', '4')
insert into Employee (Id, Name, Salary, ManagerId) values ('3', 'Sam', '60000', 'None')
insert into Employee (Id, Name, Salary, ManagerId) values ('4', 'Max', '90000', 'None')
\end{Code}

The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

Table: Employee
\begin{Code}
+----+-------+--------+-----------+
| Id | Name  | Salary | ManagerId |
+----+-------+--------+-----------+
| 1  | Joe   | 70000  | 3         |
| 2  | Henry | 80000  | 4         |
| 3  | Sam   | 60000  | NULL      |
| 4  | Max   | 90000  | NULL      |
+----+-------+--------+-----------+
\end{Code}

Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.

\begin{Code}
+----------+
| Employee |
+----------+
| Joe      |
+----------+
\end{Code}

\subsubsection{multiple table}
\begin{Code}
SELECT
    a.Name AS 'Employee'
FROM
    Employee AS a,
    Employee AS b
WHERE
    a.ManagerId = b.Id
        AND a.Salary > b.Salary
;
\end{Code}

\subsubsection{join}
\begin{Code}
SELECT
     a.NAME AS Employee
FROM Employee AS a JOIN Employee AS b
     ON a.ManagerId = b.Id
     AND a.Salary > b.Salary
;
\end{Code}

\section{Duplicate Emails} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:duplicate-emails}

\subsubsection{描述}
SQL Schema

\begin{Code}
Create table If Not Exists Person (Id int, Email varchar(255))
Truncate table Person
insert into Person (Id, Email) values ('1', 'a@b.com')
insert into Person (Id, Email) values ('2', 'c@d.com')
insert into Person (Id, Email) values ('3', 'a@b.com')
\end{Code}

Write a SQL query to find all duplicate emails in a table named Person.

Table: Person
\begin{Code}
+----+---------+
| Id | Email   |
+----+---------+
| 1  | a@b.com |
| 2  | c@d.com |
| 3  | a@b.com |
+----+---------+
\end{Code}

For example, your query should return the following for the above table:

\begin{Code}
+---------+
| Email   |
+---------+
| a@b.com |
+---------+
\end{Code}

\subsubsection{Using GROUP BY and a temporary table}
\begin{Code}
select Email from
(
  select Email, count(Email) as num
  from Person
  group by Email
) as statistic
where num > 1
;
\end{Code}

\subsubsection{Using GROUP BY and HAVING condition}
\begin{Code}
select Email
from Person
group by Email
having count(Email) > 1;
\end{Code}

\section{Customers Who Never Order} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:customers-who-never-order}

\subsubsection{描述}
SQL Schema

\begin{Code}
Create table If Not Exists Customers (Id int, Name varchar(255))
Create table If Not Exists Orders (Id int, CustomerId int)
Truncate table Customers
insert into Customers (Id, Name) values ('1', 'Joe')
insert into Customers (Id, Name) values ('2', 'Henry')
insert into Customers (Id, Name) values ('3', 'Sam')
insert into Customers (Id, Name) values ('4', 'Max')
Truncate table Orders
insert into Orders (Id, CustomerId) values ('1', '3')
insert into Orders (Id, CustomerId) values ('2', '1')
\end{Code}

Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.

Table: Customers.
\begin{Code}
+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+
\end{Code}

Table: Orders.

\begin{Code}
+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+
\end{Code}

Using the above tables as example, return the following:

\begin{Code}
+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+
\end{Code}

\subsubsection{Not In}
\begin{Code}
select customers.name as 'Customers'
from customers
where customers.id not in
(
    select customerid from orders
);
\end{Code}

\subsubsection{Left Join}
\begin{Code}
SELECT Name AS 'Customers'
FROM Customers c
LEFT JOIN Orders o
ON c.Id = o.CustomerId
WHERE o.CustomerId IS NULL;
\end{Code}

\section{Department Highest Salary} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:department-highest-salary}

\subsubsection{描述}
SQL Schema

\begin{Code}
Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, DepartmentId int)
Create table If Not Exists Department (Id int, Name varchar(255))
Truncate table Employee
insert into Employee (Id, Name, Salary, DepartmentId) values ('1', 'Joe', '70000', '1')
insert into Employee (Id, Name, Salary, DepartmentId) values ('2', 'Jim', '90000', '1')
insert into Employee (Id, Name, Salary, DepartmentId) values ('3', 'Henry', '80000', '2')
insert into Employee (Id, Name, Salary, DepartmentId) values ('4', 'Sam', '60000', '2')
insert into Employee (Id, Name, Salary, DepartmentId) values ('5', 'Max', '90000', '1')
Truncate table Department
insert into Department (Id, Name) values ('1', 'IT')
insert into Department (Id, Name) values ('2', 'Sales')
\end{Code}

The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.

\begin{Code}
+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Jim   | 90000  | 1            |
| 3  | Henry | 80000  | 2            |
| 4  | Sam   | 60000  | 2            |
| 5  | Max   | 90000  | 1            |
+----+-------+--------+--------------+
\end{Code}

The Department table holds all departments of the company.
\begin{Code}
+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+
\end{Code}

Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, your SQL query should return the following rows (order of rows does not matter).
\begin{Code}
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Jim      | 90000  |
| Sales      | Henry    | 80000  |
+------------+----------+--------+
\end{Code}

\subsubsection{Join, In}
\begin{Code}
# This example shows that IN can match multiple fields together
SELECT
    Department.name AS 'Department',
    Employee.name AS 'Employee',
    Salary
FROM
    Employee
        JOIN
    Department ON Employee.DepartmentId = Department.Id
WHERE
    (Employee.DepartmentId , Salary) IN
    (   SELECT
            DepartmentId, MAX(Salary)
        FROM
            Employee
        GROUP BY DepartmentId
    )
;
\end{Code}

\section{Department Top Three Salaries} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:department-top-three-salaries}

\subsubsection{描述}
SQL Schema

\begin{Code}
Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, DepartmentId int)
Create table If Not Exists Department (Id int, Name varchar(255))
Truncate table Employee
insert into Employee (Id, Name, Salary, DepartmentId) values ('1', 'Joe', '85000', '1')
insert into Employee (Id, Name, Salary, DepartmentId) values ('2', 'Henry', '80000', '2')
insert into Employee (Id, Name, Salary, DepartmentId) values ('3', 'Sam', '60000', '2')
insert into Employee (Id, Name, Salary, DepartmentId) values ('4', 'Max', '90000', '1')
insert into Employee (Id, Name, Salary, DepartmentId) values ('5', 'Janet', '69000', '1')
insert into Employee (Id, Name, Salary, DepartmentId) values ('6', 'Randy', '85000', '1')
insert into Employee (Id, Name, Salary, DepartmentId) values ('7', 'Will', '70000', '1')
Truncate table Department
insert into Department (Id, Name) values ('1', 'IT')
insert into Department (Id, Name) values ('2', 'Sales')
\end{Code}

Table: Employee

\begin{Code}
+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| Id           | int     |
| Name         | varchar |
| Salary       | int     |
| DepartmentId | int     |
+--------------+---------+
Id is the primary key for this table.
Each row contains the ID, name, salary, and department of one employee.
\end{Code}

Table: Department
\begin{Code}
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| Id          | int     |
| Name        | varchar |
+-------------+---------+
Id is the primary key for this table.
Each row contains the ID and the name of one department.
\end{Code}

A company's executives are interested in seeing who earns the most money in each of the company's departments. A high earner in a department is an employee who has a salary in the top three unique salaries for that department.

Write an SQL query to find the employees who are high earners in each of the departments.

Return the result table in any order.

The query result format is in the following example:
\begin{Code}
Employee table:
+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 85000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
| 5  | Janet | 69000  | 1            |
| 6  | Randy | 85000  | 1            |
| 7  | Will  | 70000  | 1            |
+----+-------+--------+--------------+

Department table:
+----+-------+
| Id | Name  |
+----+-------+
| 1  | IT    |
| 2  | Sales |
+----+-------+

Result table:
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Joe      | 85000  |
| IT         | Randy    | 85000  |
| IT         | Will     | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+

In the IT department:
- Max earns the highest unique salary
- Both Randy and Joe earn the second-highest unique salary
- Will earns the third-highest unique salary

In the Sales department:
- Henry earns the highest salary
- Sam earns the second-highest salary
- There is no third-highest salary as there are only two employees
\end{Code}

\subsubsection{Join, sub-query}
\begin{Code}
SELECT
    d.Name AS 'Department', e1.Name AS 'Employee', e1.Salary
FROM
    Employee e1
        JOIN
    Department d ON e1.DepartmentId = d.Id
WHERE
    3 > (SELECT
            COUNT(DISTINCT e2.Salary)
        FROM
            Employee e2
        WHERE
            e2.Salary > e1.Salary
                AND e1.DepartmentId = e2.DepartmentId
        )
;
\end{Code}

\subsubsection{Join, dense_rank}
\begin{Code}
SELECT d.NAME  AS Department,
       a. NAME AS Employee,
       a. salary
FROM   (SELECT e.*,
               Dense_rank()
                 OVER (
                   partition BY departmentid
                   ORDER BY salary DESC) AS DeptPayRank
        FROM   employee e) a
       JOIN department d
         ON a. departmentid = d. id
WHERE  deptpayrank <= 3;
\end{Code}

\section{Delete Duplicate Emails} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:delete-duplicate-emails}

\subsubsection{描述}
SQL Schema

\begin{Code}
Truncate table Person
insert into Person (Id, Email) values ('1', 'john@example.com')
insert into Person (Id, Email) values ('2', 'bob@example.com')
insert into Person (Id, Email) values ('3', 'john@example.com')
\end{Code}

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.

\begin{Code}
+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
| 3  | john@example.com |
+----+------------------+
Id is the primary key column for this table.
\end{Code}

For example, after running your query, the above Person table should have the following rows:
\begin{Code}
+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
+----+------------------+
\end{Code}

Note:

Your output is the whole Person table after executing your sql. Use delete statement.

\subsubsection{Delete, Where}
\begin{Code}
DELETE p1 FROM Person p1,
    Person p2
WHERE
    p1.Email = p2.Email AND p1.Id > p2.Id;
\end{Code}

\section{Rising Temperature} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:rising-temperature}

\subsubsection{描述}
SQL Schema

\begin{Code}
Create table If Not Exists Weather (Id int, RecordDate date, Temperature int)
Truncate table Weather
insert into Weather (Id, RecordDate, Temperature) values ('1', '2015-01-01', '10')
insert into Weather (Id, RecordDate, Temperature) values ('2', '2015-01-02', '25')
insert into Weather (Id, RecordDate, Temperature) values ('3', '2015-01-03', '20')
insert into Weather (Id, RecordDate, Temperature) values ('4', '2015-01-04', '30')
\end{Code}

Table: Weather

\begin{Code}
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| recordDate    | date    |
| temperature   | int     |
+---------------+---------+
id is the primary key for this table.
This table contains information about the temperature in a certain day.
\end{Code}

Write an SQL query to find all dates' id with higher temperature compared to its previous dates (yesterday).

Return the result table in any order.

The query result format is in the following example:
\begin{Code}
Weather
+----+------------+-------------+
| id | recordDate | Temperature |
+----+------------+-------------+
| 1  | 2015-01-01 | 10          |
| 2  | 2015-01-02 | 25          |
| 3  | 2015-01-03 | 20          |
| 4  | 2015-01-04 | 30          |
+----+------------+-------------+

Result table:
+----+
| id |
+----+
| 2  |
| 4  |
+----+
In 2015-01-02, temperature was higher than the previous day (10 -> 25).
In 2015-01-04, temperature was higher than the previous day (20 -> 30).
\end{Code}

\subsubsection{Join, Datediff}
\begin{Code}
SELECT
    weather.id AS 'Id'
FROM
    weather
        JOIN
    weather w ON DATEDIFF(weather.recordDate, w.recordDate) = 1
        AND weather.Temperature > w.Temperature
;
\end{Code}

\subsubsection{Join, Subdate}
\begin{Code}
select
    w1.id
from
    Weather w1
join
    Weather w2
on
    subdate(w1.recordDate, 1) = w2.recordDate
    and w1.Temperature > w2.Temperature;
\end{Code}

\section{Trips and Users} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:trips-and-users}

\subsubsection{描述}
SQL Schema

\begin{Code}
Create table If Not Exists Trips (Id int, Client_Id int, Driver_Id int, City_Id int, Status ENUM('completed', 'cancelled_by_driver', 'cancelled_by_client'), Request_at varchar(50))
Create table If Not Exists Users (Users_Id int, Banned varchar(50), Role ENUM('client', 'driver', 'partner'))
Truncate table Trips
insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('1', '1', '10', '1', 'completed', '2013-10-01')
insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('2', '2', '11', '1', 'cancelled_by_driver', '2013-10-01')
insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('3', '3', '12', '6', 'completed', '2013-10-01')
insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('4', '4', '13', '6', 'cancelled_by_client', '2013-10-01')
insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('5', '1', '10', '1', 'completed', '2013-10-02')
insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('6', '2', '11', '6', 'completed', '2013-10-02')
insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('7', '3', '12', '6', 'completed', '2013-10-02')
insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('8', '2', '12', '12', 'completed', '2013-10-03')
insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('9', '3', '10', '12', 'completed', '2013-10-03')
insert into Trips (Id, Client_Id, Driver_Id, City_Id, Status, Request_at) values ('10', '4', '13', '12', 'cancelled_by_driver', '2013-10-03')
Truncate table Users
insert into Users (Users_Id, Banned, Role) values ('1', 'No', 'client')
insert into Users (Users_Id, Banned, Role) values ('2', 'Yes', 'client')
insert into Users (Users_Id, Banned, Role) values ('3', 'No', 'client')
insert into Users (Users_Id, Banned, Role) values ('4', 'No', 'client')
insert into Users (Users_Id, Banned, Role) values ('10', 'No', 'driver')
insert into Users (Users_Id, Banned, Role) values ('11', 'No', 'driver')
insert into Users (Users_Id, Banned, Role) values ('12', 'No', 'driver')
insert into Users (Users_Id, Banned, Role) values ('13', 'No
\end{Code}

Table: Trips

\begin{Code}
+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| Id          | int      |
| Client_Id   | int      |
| Driver_Id   | int      |
| City_Id     | int      |
| Status      | enum     |
| Request_at  | date     |
+-------------+----------+
Id is the primary key for this table.
The table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are foreign keys to the Users_Id at the Users table.
Status is an ENUM type of (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’).
\end{Code}

Table: Users

\begin{Code}
+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| Users_Id    | int      |
| Banned      | enum     |
| Role        | enum     |
+-------------+----------+
Users_Id is the primary key for this table.
The table holds all users. Each user has a unique Users_Id, and Role is an ENUM type of (‘client’, ‘driver’, ‘partner’).
Status is an ENUM type of (‘Yes’, ‘No’).
\end{Code}

Write a SQL query to find the cancellation rate of requests with unbanned users (both client and driver must not be banned) each day between "2013-10-01" and "2013-10-03".

The cancellation rate is computed by dividing the number of canceled (by client or driver) requests with unbanned users by the total number of requests with unbanned users on that day.

Return the result table in any order. Round Cancellation Rate to two decimal points.

The query result format is in the following example:

\begin{Code}
Trips table:
+----+-----------+-----------+---------+---------------------+------------+
| Id | Client_Id | Driver_Id | City_Id | Status              | Request_at |
+----+-----------+-----------+---------+---------------------+------------+
| 1  | 1         | 10        | 1       | completed           | 2013-10-01 |
| 2  | 2         | 11        | 1       | cancelled_by_driver | 2013-10-01 |
| 3  | 3         | 12        | 6       | completed           | 2013-10-01 |
| 4  | 4         | 13        | 6       | cancelled_by_client | 2013-10-01 |
| 5  | 1         | 10        | 1       | completed           | 2013-10-02 |
| 6  | 2         | 11        | 6       | completed           | 2013-10-02 |
| 7  | 3         | 12        | 6       | completed           | 2013-10-02 |
| 8  | 2         | 12        | 12      | completed           | 2013-10-03 |
| 9  | 3         | 10        | 12      | completed           | 2013-10-03 |
| 10 | 4         | 13        | 12      | cancelled_by_driver | 2013-10-03 |
+----+-----------+-----------+---------+---------------------+------------+

Users table:
+----------+--------+--------+
| Users_Id | Banned | Role   |
+----------+--------+--------+
| 1        | No     | client |
| 2        | Yes    | client |
| 3        | No     | client |
| 4        | No     | client |
| 10       | No     | driver |
| 11       | No     | driver |
| 12       | No     | driver |
| 13       | No     | driver |
+----------+--------+--------+

Result table:
+------------+-------------------+
| Day        | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 | 0.33              |
| 2013-10-02 | 0.00              |
| 2013-10-03 | 0.50              |
+------------+-------------------+

On 2013-10-01:
  - There were 4 requests in total, 2 of which were canceled.
  - However, the request with Id=2 was made by a banned client (User_Id=2), so it is ignored in the calculation.
  - Hence there are 3 unbanned requests in total, 1 of which was canceled.
  - The Cancellation Rate is (1 / 3) = 0.33
On 2013-10-02:
  - There were 3 requests in total, 0 of which were canceled.
  - The request with Id=6 was made by a banned client, so it is ignored.
  - Hence there are 2 unbanned requests in total, 0 of which were canceled.
  - The Cancellation Rate is (0 / 2) = 0.00
On 2013-10-03:
  - There were 3 requests in total, 1 of which was canceled.
  - The request with Id=8 was made by a banned client, so it is ignored.
  - Hence there are 2 unbanned request in total, 1 of which were canceled.
  - The Cancellation Rate is (1 / 2) = 0.50
\end{Code}

\subsubsection{CASE WHEN, Extra Column}
\begin{Code}
SELECT
    t.Request_at AS Day
    , ROUND(SUM(t.flag) / COUNT(t.Request_at), 2) AS "Cancellation Rate"
FROM
    (
        SELECT
            *
            , CASE WHEN Status!='completed' THEN 1 ELSE 0 END AS flag
        FROM
            Trips
        WHERE
		    Request_at >= '2013-10-01' AND Request_at <= '2013-10-03'
            AND Client_Id NOT IN (SELECT Users_Id FROM Users WHERE Banned='Yes')
            AND Driver_Id NOT IN (SELECT Users_Id FROM Users WHERE Banned='Yes')
    ) AS t
GROUP BY
    t.Request_at
;
\end{Code}

\subsubsection{Extra Table}
\begin{Code}
WITH cte AS
(
SELECT
    Client_Id
    , Driver_Id
    , Status
    , Request_at
    , CASE WHEN Status!='completed' THEN 1 ELSE 0 END AS flag
FROM
    Trips
WHERE
    Request_at >= '2013-10-01' AND Request_at <= '2013-10-03'
    AND Client_Id NOT IN (SELECT Users_Id FROM Users WHERE Banned='Yes')
    AND Driver_Id NOT IN (SELECT Users_Id FROM Users WHERE Banned='Yes')
)

SELECT
    Request_at AS Day
    , ROUND(SUM(flag) / COUNT(*), 2) AS 'Cancellation Rate'
FROM
    cte
GROUP BY
    Request_at
;
\end{Code}

\subsubsection{Extra Table, Faster}
\begin{Code}
WITH cte AS
(
SELECT
    *
    , CASE WHEN Status!='completed' THEN 1 ELSE 0 END AS flag
FROM
    Trips
WHERE
    Request_at >= '2013-10-01' AND Request_at <= '2013-10-03'
    AND Client_Id NOT IN (SELECT Users_Id FROM Users WHERE Banned='Yes')
    AND Driver_Id NOT IN (SELECT Users_Id FROM Users WHERE Banned='Yes')
)

SELECT
    Request_at AS Day
    , ROUND(SUM(flag) / COUNT(*), 2) AS 'Cancellation Rate'
FROM
    cte
GROUP BY
    Request_at
;
\end{Code}

\section{Game Play Analysis I} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:game-play-analysis-i}

\subsubsection{描述}
SQL Schema

\begin{Code}
Create table If Not Exists Activity (player_id int, device_id int, event_date date, games_played int)
Truncate table Activity
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-01', '5')
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-05-02', '6')
insert into Activity (player_id, device_id, event_date, games_played) values ('2', '3', '2017-06-25', '1')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '1', '2016-03-02', '0')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '4', '2018-07-03', '5')
\end{Code}

Table: Activity

\begin{Code}
+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| player_id    | int     |
| device_id    | int     |
| event_date   | date    |
| games_played | int     |
+--------------+---------+
(player_id, event_date) is the primary key of this table.
This table shows the activity of players of some game.
Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on some day using some device.
\end{Code}

Write an SQL query that reports the first login date for each player.

The query result format is in the following example:

\begin{Code}
Activity table:
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1         | 2         | 2016-03-01 | 5            |
| 1         | 2         | 2016-05-02 | 6            |
| 2         | 3         | 2017-06-25 | 1            |
| 3         | 1         | 2016-03-02 | 0            |
| 3         | 4         | 2018-07-03 | 5            |
+-----------+-----------+------------+--------------+

Result table:
+-----------+-------------+
| player_id | first_login |
+-----------+-------------+
| 1         | 2016-03-01  |
| 2         | 2017-06-25  |
| 3         | 2016-03-02  |
+-----------+-------------+
\end{Code}

\subsubsection{GROUP BY, MIN}
\begin{Code}
SELECT
    player_id,
    MIN(event_date) AS first_login
FROM
    activity
GROUP BY
    player_id
;
\end{Code}

\section{Game Play Analysis II} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:game-play-analysis-ii}

\subsubsection{描述}
SQL Schema

\begin{Code}
Create table If Not Exists Activity (player_id int, device_id int, event_date date, games_played int)
Truncate table Activity
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-01', '5')
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-05-02', '6')
insert into Activity (player_id, device_id, event_date, games_played) values ('2', '3', '2017-06-25', '1')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '1', '2016-03-02', '0')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '4', '2018-07-03', '5')
\end{Code}

Table: Activity

\begin{Code}
+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| player_id    | int     |
| device_id    | int     |
| event_date   | date    |
| games_played | int     |
+--------------+---------+
(player_id, event_date) is the primary key of this table.
This table shows the activity of players of some game.
Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on some day using some device.
\end{Code}

Write an SQL query that reports the first login date for each player.

The query result format is in the following example:

\begin{Code}
Activity table:
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1         | 2         | 2016-03-01 | 5            |
| 1         | 2         | 2016-05-02 | 6            |
| 2         | 3         | 2017-06-25 | 1            |
| 3         | 1         | 2016-03-02 | 0            |
| 3         | 4         | 2018-07-03 | 5            |
+-----------+-----------+------------+--------------+

Result table:
+-----------+-----------+
| player_id | device_id |
+-----------+-----------+
| 1         | 2         |
| 2         | 3         |
| 3         | 1         |
+-----------+-----------+
\end{Code}

\subsubsection{GROUP BY, MIN}
\begin{Code}
SELECT
    player_id
    , device_id
FROM
    activity
WHERE
    (player_id
     , event_date)
IN
    (SELECT
         player_id
         , MIN(event_date) AS event_date
     FROM
         activity
     GROUP BY player_id)
;
\end{Code}

\subsubsection{GROUP BY, Dense_Rank}
\begin{Code}
SELECT
    player_id
    , device_id
FROM
    (SELECT
         player_id
         , device_id
         , DENSE_RANK() OVER (PARTITION BY player_id ORDER BY event_date) AS num
    FROM
         activity) AS a
    WHERE
        num = 1
;
\end{Code}

\section{Game Play Analysis III} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:game-play-analysis-iii}

\subsubsection{描述}
SQL Schema

\begin{Code}
Create table If Not Exists Activity (player_id int, device_id int, event_date date, games_played int)
Truncate table Activity
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-01', '5')
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-05-02', '6')
insert into Activity (player_id, device_id, event_date, games_played) values ('2', '3', '2017-06-25', '1')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '1', '2016-03-02', '0')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '4', '2018-07-03', '5')
\end{Code}

Table: Activity

\begin{Code}
+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| player_id    | int     |
| device_id    | int     |
| event_date   | date    |
| games_played | int     |
+--------------+---------+
(player_id, event_date) is the primary key of this table.
This table shows the activity of players of some game.
Each row is a record of a player who logged in and played a number of games (possibly 0)
before logging out on some day using some device.
\end{Code}

Write an SQL query that reports for each player and date, how many games played so far by the player. That is, the total number of games played by the player until that date. Check the example for clarity.

The query result format is in the following example:

\begin{Code}
Activity table:
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1         | 2         | 2016-03-01 | 5            |
| 1         | 2         | 2016-05-02 | 6            |
| 1         | 3         | 2017-06-25 | 1            |
| 3         | 1         | 2016-03-02 | 0            |
| 3         | 4         | 2018-07-03 | 5            |
+-----------+-----------+------------+--------------+

Result table:
+-----------+------------+---------------------+
| player_id | event_date | games_played_so_far |
+-----------+------------+---------------------+
| 1         | 2016-03-01 | 5                   |
| 1         | 2016-05-02 | 11                  |
| 1         | 2017-06-25 | 12                  |
| 3         | 2016-03-02 | 0                   |
| 3         | 2018-07-03 | 5                   |
+-----------+------------+---------------------+
For the player with id 1, 5 + 6 = 11 games played by 2016-05-02,
and 5 + 6 + 1 = 12 games played by 2017-06-25.
For the player with id 3, 0 + 5 = 5 games played by 2018-07-03.
Note that for each player we only care about the days when the player logged in.
\end{Code}

\subsubsection{SUM, Window Function}
\begin{Code}
SELECT
    player_id
    , event_date
    , SUM(games_played) OVER
        (PARTITION BY player_id ORDER BY event_date) AS games_played_so_far
FROM
    Activity;
\end{Code}


\section{Refined Strings}\label{sec:strings}

Refined Strings provides a wrapping over a string manipulations library,
here `ByteString`
\begin{code}
data RString = S BS.ByteString
\end{code}

String operators wrap the library operators
and moreover they are assumed to be reflected into the logic

\begin{code}
assume concatString :: x:LHString -> y:LHString
      -> {v:LHString | v == concatString x y }
concatString (S s1) (S s2) = S (s1 `BS.append` s2)

assume stringEmp :: {v:RString | v == stringEmp  && stringLen v == 0 }
stringEmp = S (BS.empty)

assume stringLen :: x:RString -> {v:Nat | v == stringLen x}
stringLen (S s) = BS.length s

assume subString  :: s:RString -> offset:Int -> ln:Int -> {v:RString | v == subString s offset ln }
subString (S s) o l = S (BS.take l (BS.drop o s))

assume takeString :: i:Nat -> xs:{RString | i <= stringLen xs }
  -> {v:RString | stringLen v == i && v == takeString i xs }
takeString i (S s) = S (BS.take i s)

assume dropString :: i:Nat -> xs:{RString | i <= stringLen xs }
  -> {v:RString | stringLen v == stringLen xs - i && v == dropString i xs }
dropString i (S s) = S (BS.drop i s)

assume fromString :: i:String -> {o:RString | i == o && o == fromString i}
fromString = S . ST.fromString

assume isNullString :: i:RString -> {b:Bool | Prop b <=> stringLen i == 0 }
isNullString (S s) = BS.length s == 0
\end{code}

Finally the refined string library, exposes theorems that hold (but are assumed)
for the internal string representation

\begin{code}
assume stringEmpProp :: x:RString  -> { stringLen x == 0 <=> x == stringEmp }
stringEmpProp _ = trivial

assume concatStringNeutralLeft :: x:RString -> {concatString x stringEmp == x}
concatStringNeutralLeft _ = trivial

assume concatStringNeutralRight :: x:RString -> {concatString stringEmp x == x}
concatStringNeutralRight _ = trivial

assume concatTakeDrop :: i:Nat -> xs:{RString | i <= stringLen xs}
    -> {xs == concatString (takeString i xs) (dropString i xs) }
concatTakeDrop _ _ = trivial

assume concatLen :: x:RString -> y:RString -> { stringLen (concatString x y) == stringLen x + stringLen y }
concatLen _ _ = trivial

assume concatStringAssoc :: x:RString -> y:RString -> z:RString
     -> {concatString (concatString x y) z == concatString x (concatString y z) }
concatStringAssoc _ _ _ = trivial

-- | Substrings

assume subStringConcatBack :: input:RString -> input':RString -> j:Int -> i:{Int | i + j <= stringLen input }
  -> { (subString input i j == subString (concatString input input') i j) }
subStringConcatBack _ _ _ _ = trivial

assume subStringConcatFront
  :: input:RString -> input':RString -> j:Int -> i:Int
  -> { (subString input i j == subString (concatString input' input) (stringLen input' + i) j) }
subStringConcatFront _ _ _ _ = trivial
\end{code}


\subsection{Variance of the 2SOLS estimator}


\subsection{gammapy.stats}
\label{ssec:gammapy-stats}
\todo{Regis Terrier}

The gammapy.stats subpackage contains the fit statistics and associated
statistical estimators that are commonly used in gamma-ray astronomy. In
general, gamma-ray observations count Poisson-distributed events at various sky
positions, and contain both signal and background events. Estimation of the
number of signal events is done through likelihood maximization. In Gammapy,
the fit statistics are Poisson log-likelihood functions normalized like
chi-squares, i.e., they follow the expression $2 \times log L$, where $L$ is
the likelihood function used. The statistic function used when the expected
number of background events is known is the \emph{Cash} statistic
~\citep{Cash}. It is used by datasets using background templates such as the
MapDataset. When the number of background events is unknown and an off
measurement where only background events are expected is used, the statistic
function is WStat. It is a profile log-likelihood statistic where background
counts are marginalized parameters. It is used by datasets containing off
counts measurements such as the SpectrumDatasetOnOff, used for classical
spectral analysis.

To perform simple statistical estimations on counts measurements,
CountsStatistic classes encapsulate the aforementioned statistic functions to
measure excess counts and estimate the associated statistical significance,
errors and upper limits. They perform maximum likelihood ratio tests to
estimate significance (the square root of the statistic difference) and compute
likelihood profiles to measure errors and upper limits. The code example
\ref{codeexample:stats} shows how to compute the Li \& Ma
significance~\citep{LiMa} of a set of measurements.

\begin{figure}
	\import{code-examples/generated/}{gp_stats}
	\caption{}
	\label{codeexample:stats} \end{figure}


\section{Algorithm}
Although the algorithm of~\cite{AGHKT12} is statistically consistent and more computationally efficient than EM, directly applying it to our setting has a few drawbacks: First, if we covert our observations $(c_t, \mu_t)$ to categorical observations, then since $(c_t, \mu_t) \in \cbr{(x,y) \in \N^2: x\leq y \leq N}$, the observation space scale as $O(N^2)$.  In practice, $N$ can be as large as 2000, making $n$, the size of the observation space, as large as $10^6$, which can be prohibitive. Moreover, we are given the prior information that $\mu_t$ is drawn from a binomial distribution $\bin(c_t, p_{h_t})$, which a direct aplication of~\cite{AGHKT12} will not utilize. Instead, it suffices to estimate vector $p$ for our application. Estimating the joint distribution of $c_t$ and $\mu_t$ given $h_t$ is not necessary for our purposes, and direct estimation of $p$ may result in better statistical efficiency.

To overcome these drawbacks, we propose a feature-map based algorithm for learning the binomial hidden Markov model. The algorithm relies on the tensor decomposition algorithm of~\cite{AGHKT12}, but has several novel modifications. We introduce them in the following subsections.

\subsection{Feature Map} We use a feature map
$\phi(x)$ to map the observations to a vector. By redefining $C_i = \E[\phi(x_i)|h_2]$,
and $P_{i,j} := \E[\phi(x_i) \otimes \phi(x_j)]$ (where $i,j$ are distinct elements from $\cbr{1,2,3}$)
and $T := \E[\phi(x_1) \otimes \phi(x_2) \otimes \phi(x_3)]$, it can be shown that same as in Section 2.3, relationship
\[ P_{i,j} = C_i \diag(w) C_j^T = \sum_{l=1}^m w_l (C_i)_l \otimes (C_j)_l \]
and
\[ T = \sum_{l=1}^m w_l (C_1)_l \otimes (C_2)_l \otimes (C_3)_l \]
still holds, and Steps 2-5 in Section 2.3 provably recovers matrix $C_2$, matrix $T$ and vector $\pi$
modulo column permutation and scaling.

%\paragraph{Binning mapping} Given observation $(c, \mu)$, $\phi_{\bin, n}(c, \mu)$ is a $n$-dimensional vector, with its entries as follows:
%\[ (\phi_{\bin, n}(c, \mu))_i = \begin{cases} I(\frac{\mu}{c} \in (\frac{i-1}{n}, \frac{i}{n}]) & c \neq 0 \\ 0 & c = 0 \end{cases} \]
%where $n$ is a hyperparameter controlling the number of the bins, and the width of the bins is $\frac{1}{n}$.

\paragraph{Beta Mapping} We propose a novel feature map, namely Beta mapping, to map the observations to vectors. Since our goal is to recover $p_h$ for all $h$, we would like a function of $c_t$ and $m_t$ that reveals information on the underlying $p_{h_t}$. We map the observation $(m_t, c_t)$ to a (discretized) distribution with its mean around $\frac{c_t}{m_t}$, which is $p_{h_t}$ in expectation; moreover, if $c_t$ is large, then the mapped distribution is more concentrated, implying that we have a higher confidence on the value of $p_{h_t}$.

Formally, given observation $(c, \mu)$, $\phi_{\bet, n}(c, \mu)$ is a $n$-dimensional vector, with its entries defined as:
\[ (\phi_{\bet, n}(c, \mu))_i = \frac{1}{B(\mu+1, c-\mu+1)} (\frac{i}{n})^\mu (1-\frac{i}{n})^{c-\mu} \]
We also denote by the above quantity $\varphi_{\bet}((c,\mu), \frac{i}{n})$, where $\varphi_{\bet, n}(x, t) = \frac{1}{B(\mu+1, c-\mu+1)} t^\mu (1-t)^{c-\mu}$.
We will run Algorithm $\TD$ with feature map $\phi_{\bet, n}$.

%\paragraph{Recovery of $p_h$ from Expected  Binning feature mapping}
%Recall that
%\[ \int_0^1 t \E[\phi_{\bin}(x,t) | h] dt = \E[\frac{m}{c} | h] = p_h \]
%Thus,
%\[ p_h = \int_0^1 t \E[\phi_{\bin}(x,t) | h] dt. \]

\subsection{Recovery of Methylation Probabilities}
Notice that running the Algorithm $\TD$ with feature map $\phi$, we will recover $C_2 = \E[\phi(x)|h]$, which is not the methylation probability. To recover the model parameters $p$,we need a recovery procedure that extracts the value of $p_h$ for each $h$ from $\E[\phi(x)|h]$ estimated from tensor decomposition.

\paragraph{Recovery of $p_h$ from Expected Beta Feature Mapping} If the feature map $\phi$ is the Beta feature mappping $\phi_{\bet, n}$, we use the following formula to recover $p_h$ from $\E[\phi(x)|h]$:
\[ \hat{p}_h := \frac{\frac{1}{n} \sum_{i=1}^n \frac{i}{n} \E[(\phi_{\bet}(x))_i | h] - a}{1 - 2 a}, \]
where $a = \E[\frac{1}{c+2}]$. We justify the recovery formula as follows.

Recall that
\[ \int_0^1 \frac{t \cdot t^{m} (1-t)^{c-m}}{B(m+1,c-m+1)} dt = \int_0^1 \frac{t \cdot t^{(m+1)-1} (1-t)^{(c-m+1)-1}}{B(m+1,c-m+1)} dt = \frac{m+1}{c+2} \]
Therefore,
\[ \int_0^1 t \E[\varphi_{\bet}(x,t) | h] dt = \E\sbr{\frac{m+1}{c+2} | h} = \E\sbr{\frac{cp_h + 1}{c + 2} | h} = \E\sbr{\frac{c}{c+2}} p_h + \E\sbr{\frac{1}{c+2}} \]
assuming independence between $h$ and $c$.
Let $a = \E[\frac{1}{c+2}]$. Then,
\[ \int_0^1 t\E[\varphi_\bet(x,t) | h] dt = (1 - 2a) p_h + a. \]
Hence,
\[ p_h = \frac{\int_0^1 t\E[\varphi_{\bet}(x,t) | h] dt - a}{1 - 2 a}. \]
In practice, since we only have finite dimensions of the feature map, we do discrete
summation as opposed to integration. Using the relationship that $\phi_{\bet, n}(x)_i = \varphi_{\bet}(x, \frac{i}{n})$, we get
\[ p_h \approx \frac{\frac{1}{n} \sum_{i=1}^n \frac{i}{n} \E[(\phi_{\bet}(x))_i | h] - a}{1 - 2 a}. \]

\subsection{Stablization Procedure}
In practice, the binomial hidden Markov model may not perfectly characterize the data distribution. Therefore, the methylation probability estimation provided by the algorithm may not fully capture the data distribution and causes estimation errors on other parameters, such as initial probablity and transition matrix. To address this issue, we propose a least squares formulation for recovering the transition matrix and initial probability distribution. Given $\hat{C}_2$ as an estimatior of $\E[\phi(x_2)|h_2]$, we propose to solve the following optimization problem:
\[ \min_{H_{2,1}: \forall i,j (H_{2,1})_{i,j} \geq 0, \sum_{i,j} (H_{2,1})_{i,j} = 1} \| P_{2,1} - \hat{C}_2 H_{2,1} \hat{C}_2^T \|_F^2 \]
Here, $H_{2,1}$ is and estimator of $\P[x_2 = i, x_1 = j]$, and we can recover the transition matrix and intial probability by applying the formulae $\pi =\one^T H_{2,1}$ and $T = H_{2,1} \diag(\pi)^{-1}$.
Empirically, this can be shown to have superior performance compared to direct applying the formula
\[ H_{2,1} := \hat{C}_2^{\dagger} P_{2,1} \hat{C}_2^{\dagger T}\]
which will get an estimator of $H_{2,1}$ with large negative entries.

\subsection{Multiple Cell Types}
For experiments with differential methylation between two cell types, we observe two
coverage methylation pairs, one for each cell types. That is, $x = ((c^1, \mu^1), (c^2, \mu^2))$.
Our goal is to extract hidden states represented by a pair of methylation probabilities $(p^1_h, p^2_h)$.
To this end, we construct a concatenated feature map from feature maps on each cell type:
\[ \phi(x) = \begin{bmatrix} \phi(c^1, \mu^1) \\ \phi(c^2, \mu^2) \end{bmatrix} \]
Following the tensor decomposition algorithm in Section 2, we can recover the expected feature map
given hidden states:
\[ C_2 = \E[\phi(x)|h] = \begin{bmatrix} \E[\phi(c^1, \mu^1)|h] \\ \E[\phi(c^2, \mu^2)|h] \end{bmatrix}\]
Now, applying the recovery procedure of $p_h$, we can now recover
a pair $(p^1_h, p^2_h)$ for each hidden state $h$. For $h$, if we see a large difference between
$p^1_h$ and $p^2_h$, then we identify state $h$ as differntial methylation state.
We follow the same stable recovery procedure as in the last subsection.

\subsection{Decoding}
Parameter recovery gives us estimates on model parameters $\pi, T, p$. We perform decoding, i.e. inference
over the hidden states, using two algorithms: posterior decoding and Viterbi decoding. In posterior decoding,
we compute $\P(h_t=i|x_1, \ldots, x_T)$ and pick the $i$ achieving the maximum for each position $t$. In Viterbi decoding,
we use a dynamic program to get a combination of $h_1, \ldots, h_T$ that maximizes
$\P(h_1, \ldots, h_T|x_1,\ldots,x_T)$. Both algorithm runs in time $O(l m^2)$. In the experiments, we see the decoding results of both methods are roughly the same, therefore, we focus on the results of posterior decoding in this paper.

When we have multiple cell types, we compute the observation probabilities given hidden states under the following assumption. Given positition $t$,
we assume conditional independence among different observations in different cell types given the hidden state and the coverage in its cell type. Formally, we have
\[ \P(c_1, c_2 | \mu_1, \mu_2, h) = \P(c_1 | \mu_1, h) \cdot \P(c_2 | \mu_2, h) \]


\chapter{EVOLUTIONARY ALGORITHMS}

\section{Introduction to EA}In artificial intelligence, an evolutionary algorithm (EA) is a subset of evolutionary computation, a generic population-based optimization algorithm. An EA uses mechanisms inspired by biological evolution, such as reproduction, mutation, recombination, and selection. Candidate solutions to the optimization problem play the role of individuals in a population, and the fitness function determines the quality of the solutions (see also loss function). Evolution of the population then takes place after the repeated application of the above operators.In details, the underlying idea behind the algorithm is:\\
Given a population of individuals the environmental pressure causes natural selection (survival of the fittest) and this causes a rise in the fitness of the population.Given a quality function to be maximised we can randomly create a set of candidate solutions, i.e., elements of the function’s domain, and apply the quality function as an abstract fitness measure- the higher the better. Based on this fitness, some of the better candidates are chosen to seed the next generation by applying recombination and/or mutation to them.\\
Recombination is an operator applied to two or more selected candidates (the so-called parents) and results one or more new candidates (the children). Mutation is applied to one candidate and results in one new candidate.Executing recombination and mutation leads to a set of new candidates date (the 	offspring) that compete-based on their fitness – with the old ones for a place in the 	next generation. This process can be iterated until a candidate with sufficient quality (a solution) is found or a previously set computational limit is reached.\\
\section{General algorithm of EA.}BEGIN\\
        INITIALISE population with random candidate solutions;\\
		EVALUATE each candidate’s fitness;\\
		REPEAT UNTIL (TERMINATION CONDITION is satisfied) DO\\\
           1.	SELECT parents;\\
           2.	RECOMBINE pairs of parents;\\
           3.	MUTATE the resulting offspring;\\
           4.	EVALUATE new candidates;\\
           5.	SELECT individuals for the next generation;\\
        END\\\\
\includegraphics[width=1\textwidth]{./ge}\\[1cm]
\section{Some evolutionary algorithms.}
\subsection{Ant colony optimization.}Ant colony optimization algorithm(ACO) is a probabilistic technique for solving computational problems which can be reduced to finding good paths through graphs.This algorithm is a member of the ant colony algorithms family, in swarm intelligence methods, and it constitutes some optimizations. Initially proposed by Marco Dorigo in 1992 in his PhD thesis, the first algorithm was aiming to search for an optimal path in a graph, based on the behavior of ants seeking a path between their colony and a source of food. The original idea has since diversified to solve a wider class of numerical problems, and as a result, several problems have emerged, drawing on various aspects of the behavior of ants.\\
\includegraphics[width=0.5\textwidth]{./ant}\\[1cm]
\subsection{Genetic algorithm.}Genetic algorithm (GA) is a search heuristic that mimics the process of natural selection. Genetic algorithms belong to the larger class of evolutionary algorithms (EA), which generate solutions to optimization problems using techniques inspired by natural evolution, such as inheritance, mutation, selection, and crossover.Next step is to generate a second generation population of solutions from those selected through a combination of genetic operators: crossover (also called recombination), and mutation.\\
For each new solution to be produced, a pair of "parent" solutions is selected for 	breeding from the pool selected previously.By producing a "child" solution using 	the above methods of crossover and mutation, a new solution is created which typically shares many of the characteristics of its "parents". New parents are selected for each new child, and the process continues until a new population of solutions of appropriate size is generated. Although reproduction methods that are based on the use of two parents are more "biology inspired",quality chromosomes.\\
These processes ultimately result in the next generation population of 	chromosomes that is different from the initial generation.Generally the average fitness will have increased by this procedure for the population, since only the best organisms from the first generation are selected for breeding, along with a small proportion of less fit solutions. These less fit solutions ensure genetic diversity within the genetic pool of the parents and therefore ensure the genetic diversity of the subsequent generation of children.\\
\includegraphics[width=0.5\textwidth]{./ee}\\[1cm]
\subsection{Particle swarm optimization.}Particle swarm optimization is a heuristic global optimization method and also an optimization algorithm, which is based on swarm intelligence. It comes from the research on the bird and fish flock movement behaviour.These particles are moved around in the search-space according to a few simple formulae. The movements of the particles are guided by their own best known position in the search-space as well as the entire swarm's best known position. When improved positions are being discovered these will then come to guide the movements of the swarm. The process is repeated and by doing so it is hoped, but not guaranteed, that a satisfactory solution will eventually be discovered.\\\\
\includegraphics[width=0.5\textwidth]{./pso}\\[1cm]


\documentclass{article}
\usepackage{etoolbox}
\usepackage{mdwlist}
\usepackage{mathtools}
\makeatletter
\patchcmd{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi}{}{}{}
\makeatother

\usepackage{thumbpdf}
\usepackage[pdftex,
        colorlinks=true,
        urlcolor=rltblue,       % \href{...}{...} external (URL)
        filecolor=rltgreen,     % \href{...} local file
        linkcolor=rltred,       % \ref{...} and \pageref{...}
        pdftitle={Untitled},
        pdfauthor={Your Name},
        pdfsubject={Just a test},
        pdfkeywords={mathematics},
        pdfproducer={pdfLaTeX},
        %pdfadjustspacing=1,
        pagebackref,
        pdfpagemode=None,
        bookmarksopen=true]{hyperref}
\usepackage{color}
\definecolor{rltred}{rgb}{0.75,0,0}
\definecolor{rltgreen}{rgb}{0,0.5,0}
\definecolor{rltblue}{rgb}{0,0,0.75}

\title{Optimist Racing - Math and Physics}
\author{Claude Richard}
\date{\today}

\begin{document}\label{start}

\maketitle
This document contains all the mathematics used (except the easy math LOL) for the program.

\subsection{Conventions}
This section describes the notational conventions used throughout this document.
If \(A\) is a matrix (or vector), then we denote the i\textsuperscript{th} row of A by \(A_i\).
We also denote the i\textsuperscript{th} column of A by \(A^i\).

\newpage

\section{Electric Colours}
There are 3 RGB colours (red, green, blue). There are 8 electric colours.
The electric colours can be thought of as subsets of \( \{ red, green, blue \} \).
The electric colours are:
\begin{basedescript}{\desclabelstyle{\pushlabel}\desclabelwidth{10em}}
\item [ElectricBlack] \(= \emptyset\)
\item [ElectricRed] \(= \{ red \}\)
\item [ElectricGreen] \(= \{ green \}\)
\item [ElectricBlue] \(= \{ blue \}\)
\item [ElectricYellow] \(= \{ red, green \}\)
\item [ElectricMagenta] \(= \{ red, blue \}\)
\item [ElectricCyan] \(= \{ green, blue \}\)
\item [ElectricWhite] \(= \{ red, green, blue \}\)
\end{basedescript}
In the physics engine, 2 objects of Electric colours X and Y respectively can interact with each other if and only if
\( X \bigcap Y \neq \emptyset \)

\newpage

\section{Smooth Floor Grid}
File: Bezier.hpp
You can create a floor which is differentiable n times for an arbitrary n.
Note that the complexity is at least O(n). (I didn't calculate it exactly)
Specify f, fx, fy, etc. for each vertex in a grid.
Then the whole grid will be n-times differentiable everywhere.

\newpage

\section{Lagrangian Mechanics}
Let \(\overrightarrow{x}\) represent the vector of generalized coordinates, of length \(n\).
Let \(\overrightarrow{Q}\) represent the vector of generalized forces, also of length \(n\).
For a given system, let \(T\) be the kinetic energy and \(V\) be the potential energy of the system.
Express \(L = T - V\) as a function of the generalized coordinates and their derivatives with respect to time.
\[
T = T(x_1, \dotsc, x_n, \dot{x_1}, \dotsc, \dot{x_n})
\]\[
V= V(x_1, \dotsc, x_n)
\]\[
L = L(x_1, \dotsc, x_n, \dot{x_1}, \dotsc, \dot{x_n})
\]
Then there are \(n\) Lagrange Equations which together govern the motion of the generalized coordinates.
\[
\frac{d}{dt}\frac{\partial L}{\partial \dot{x_i}} - \frac{\partial L}{\partial x_i} = Q_i
\]

\subsection{Matrix Form}
You should be able to express \(L\) in matrix form:
\[
L = \frac{1}{2} \dot{\overrightarrow{x}}^T A \dot{\overrightarrow{x}} - V
\]
Where \(A\) is a symmetric \(n x n\) matrix dependent on the generalized coordinates but not their derivatives wrt time.
\[
A = A(x_1, \dotsc, x_n)
\]\[
A = A^T
\]
We will now derive a vector equation to replace the \(n\) Lagrange equations. We have the following:
\[
\frac{\partial L}{\partial x_i} = \frac{1}{2}\dot{\overrightarrow{x}}^T \frac{\partial A}{\partial x_i} \overrightarrow{x} - \frac{\partial V}{\partial x_i}
\]\[
\frac{\partial L}{\partial \dot{x_i}} = A_i \dot{\overrightarrow{x}}
\]\[
\frac{d}{dt}\frac{\partial L}{\partial \dot{x_i}} = A_i \ddot{\overrightarrow{x}} +
\sum_{j=1}^{n} \dot{x_j}(\frac{\partial A}{\partial x_j})_i \dot{\overrightarrow{x}}
\]
The i\textsuperscript{th} Lagrange equation then becomes:
\[
A_i \ddot{\overrightarrow{x}} + \sum_{j=1}^{n} \dot{x_j}(\frac{\partial A}{\partial x_j})_i \dot{\overrightarrow{x}}
- \frac{1}{2}\dot{\overrightarrow{x}}^T \frac{\partial A}{\partial x_i} \overrightarrow{x} + \frac{\partial V}{\partial x_i}
= Q_{x_i}
\]
Now for each \(i\), let \(M_i\) be a matrix such that \( (M_i)_j = (\frac{\partial A}{\partial x_j})_i\).
All equations combined become the following vector equation:
\[
A \ddot{\overrightarrow{x}} + \sum_{j=1}^{n} \dot{x_j}(\frac{\partial A}{\partial x_j} - \frac{1}{2}M_j) \dot{\overrightarrow{x}}  + \nabla V = \overrightarrow{Q}
\]

\subsection{Kinetic energy relating to the center-of-mass}
Let the system be composed of one object of mass \(m\), which does not rotate, and whose position as a function of the generalized coordinates is:
\[
position = \begin{bmatrix}
  f(\overrightarrow{x}) \\
  g(\overrightarrow{x}) \\
  h(\overrightarrow{x})
 \end{bmatrix}
\]
We will now derive the kinetic energy as a function of \(\overrightarrow{x}\), \(\dot{\overrightarrow{x}}\), and the three functions \(f\), \(g\), and \(h\).
The velocity of the object is:
\[
velocity = \begin{bmatrix}
  (\nabla f)^T \dot{\overrightarrow{x}} \\
  (\nabla g)^T \dot{\overrightarrow{x}} \\
  (\nabla h)^T \dot{\overrightarrow{x}}
 \end{bmatrix}
\]
So its kinetic energy is:
\[
T = \frac{1}{2} m [\dot{\overrightarrow{x}}^T [(\nabla f)(\nabla f)^T + (\nabla g)(\nabla g)^T + (\nabla h)(\nabla h)^T] \dot{\overrightarrow{x}} ]
\]
Assume the potential energy is zero. Then \(L = T\).
We will now derive the Lagrange equations for this system.
\[
\frac{\partial L}{\partial x_i} = \frac{1}{2} m \dot{\overrightarrow{x}}^T [ H(f)^i(\nabla f)^T + H(g)^i(\nabla g)^T + H(h)^i(\nabla h)^T + (\nabla f)H(f)_i + (\nabla g)H(g)_i + (\nabla h)H(h)_i ] \dot{\overrightarrow{x}}
\]\[
\frac{\partial L}{\partial \dot{x_i}} = m [ \frac{\partial f}{\partial x_i}(\nabla f)^T + \frac{\partial g}{\partial x_i}(\nabla g)^T +\frac{\partial h}{\partial x_i}(\nabla h)^T ] \dot{\overrightarrow{x}}
\]\[
\begin{array}{lcl}
\frac{d}{dt} \frac{\partial L}{\partial \dot{x_i}} & = & m [ \frac{\partial f}{\partial x_i}(\nabla f)^T + \frac{\partial g}{\partial x_i}(\nabla g)^T +\frac{\partial h}{\partial x_i}(\nabla h)^T ] \ddot{\overrightarrow{x}} \\
& + & m \dot{\overrightarrow{x}}^T [H(f)^i(\nabla f)^T + H(g)^i(\nabla g)^T + H(h)^i(\nabla h)^T] \dot{\overrightarrow{x}} \\
& + & m \dot{\overrightarrow{x}}^T [\frac{\partial f}{\partial x_i}H(f) + \frac{\partial g}{\partial x_i}H(g) + \frac{\partial h}{\partial x_i}H(h)] \dot{\overrightarrow{x}}
\end{array}
\]
The matrix form of the Lagrange equations is:
\[
\begin{array}{lcl}
m[(\nabla f)(\nabla f)^T + (\nabla g)(\nabla g)^T + (\nabla h)(\nabla h)^T]\ddot{\overrightarrow{x}} + \\
m[(\nabla f)\dot{\overrightarrow{x}}^TH(f) + (\nabla g)\dot{\overrightarrow{x}}^TH(g) + (\nabla h)\dot{\overrightarrow{x}}^TH(h)]\dot{\overrightarrow{x}} = Q
\end{array}
\]

\subsection{Kinetic energy relating to the Euler angles}
Let the system be composed of one object, whose center-of-mass does not move, and whose Euler angles as a function of the generalized coordinates are \(\alpha\), \(\beta\) and \(\gamma\).
This means that to go from an orientation parallel to the world axes to the object's orientation, you must turn left (around the z-axis) by \(\alpha\), then tilt back (around the new model x-axis) by \(\beta\), then turn left around the model z-axis by \(\gamma\).
Let the object have a moment of inertia tensor
\[
I = \begin{bmatrix}
I_x & I_{xy} & I_{xz} \\
I_{xy} & I_y & I_{yz} \\
I_{xz} & I_{yz} & I_z
\end{bmatrix}
\]
Also denote
\[
\alpha_1 = \alpha, \alpha_2 = \beta, \alpha_3 = \gamma
\]\[
G = \begin{bmatrix}
\nabla \alpha & \nabla \beta & \nabla \gamma
\end{bmatrix}
\]
\[
E = \begin{bmatrix}
I_x \sin^2 \beta \cos^2 \gamma & (I_y-I_x)\sin\beta\sin\gamma\cos\gamma & I_z\cos\beta \\
(I_y-I_x)\sin\beta\sin\gamma\cos\gamma & I_x\sin^2\gamma+I_y\cos^2\gamma & 0 \\
I_z\cos\beta & 0 & I_z
\end{bmatrix}
\]\[
\nabla_0 L = \begin{bmatrix} \frac{\partial L}{\partial x_1} \\ \vdots \\ \frac{\partial L}{\partial x_n} \end{bmatrix}
\]\[
\nabla_1 L = \begin{bmatrix} \frac{\partial L}{\partial \dot{x_1}} \\ \vdots \\ \frac{\partial L}{\partial \dot{x_n}} \end{bmatrix}
\]
We will now derive the matrix form of the Lagrange equations for this system.
Let \(T\) represent the kinetic energy, and assume the potential energy is zero.
We can derive the following lemmas:
\[
\dot{\alpha_i} = \dot{\overrightarrow{x}}^T \nabla\alpha_i
\]\[
\dot{(\nabla\alpha_i)} = H(\alpha_i)\dot{\overrightarrow{x}}
\]\[
T = L
\]
\[
T = \begin{bmatrix}\dot \alpha & \dot \beta & \dot \gamma \end{bmatrix}
E \begin{bmatrix}\dot \alpha \\ \dot \beta \\ \dot \gamma \end{bmatrix}
\]
Therefore:
\[
L = \frac{1}{2} \dot{\overrightarrow{x}}^T G E G^T \dot{\overrightarrow{x}}
\]\[
\nabla_0 L = \sum_{i=1}^3 \frac{1}{2} \nabla \alpha_i \dot{\overrightarrow{x}}^T G \frac{\partial E}{\partial \alpha_i} G^T \dot{\overrightarrow{x}}
+ H(\alpha_i) \dot{\overrightarrow{x}} E_i G^T \dot{\overrightarrow{x}}
\]\[
\nabla_1 L = G E G^T \dot{\overrightarrow{x}}
\]
Anyway, after a while we find the final vector equation:
\[
G E G^T \ddot{\overrightarrow{x}} + \sum_{i=1}^{3} G E^i \dot{\overrightarrow{x}}^T H(\alpha_i) \dot{\overrightarrow{x}} +
G \frac{\partial E}{\partial \alpha_i} G^T \dot{\overrightarrow{x}} \dot{\overrightarrow{x}}^T \nabla \alpha_i -
\frac{1}{2} \nabla \alpha_i \dot{\overrightarrow{x}}^T G \frac{\partial E}{\partial \alpha_i} G^T \dot{\overrightarrow{x}}
= \overrightarrow{Q}
\]

\subsection{Combining center-of-mass movement and rotation, and several objects}
In the previous two sections we focused on center-mass movement, then on rotation, for only one object.
What if we have several objects that have both rotation and moving centers-of-mass?
Let the system have \(k\) objects, and \(n\) generalized coordinates. Let the position of object \(i\) be
\[ position_i = \begin{bmatrix} f_i(\overrightarrow{x}) \\ g_i(\overrightarrow{x}) \\ h_i(\overrightarrow{x}) \end{bmatrix} \]
and let the Euler angles of object \(i\) be
\[ Euler_i = \begin{bmatrix} \alpha_i(\overrightarrow{x}) \\ \beta_i(\overrightarrow{x}) \\ \gamma_i(\overrightarrow{x}) \end{bmatrix} \]
If we use the center-of-mass section above for object \(i\), we would get the equation
\[ C_i \ddot{\overrightarrow{x}} = \overrightarrow{c_i} \]
And if we use the rotation section above for object \(i\), we would get
\[ R_i \ddot{\overrightarrow{x}} = \overrightarrow{r_i} \]
Where \(C_i\), \(R_i\), \(\overrightarrow{c_i}\) and \(\overrightarrow{r_i}\) depend on \(\overrightarrow{x}\) and \(\dot{\overrightarrow{x}}\).
For the combined system, the equation is simply
\[ A \ddot{\overrightarrow{x}} = \overrightarrow{Q} - \overrightarrow{b} \]
where
\[ A = \sum_{i=1}^{n} (C_i + R_i) \]
\[ b = \sum_{i=1}^{n} (\overrightarrow{c_i} + \overrightarrow{r_i}) \]
and \(\overrightarrow{Q}\) is the generalized force on the system, which will be covered in the next sections.

\subsection{Transforming forces and torques between model and world coordinate systems}
This is just a preliminary section to clarify the transformations in the next sections.
Say you apply a force or torque \(\overrightarrow{F_w}\) in world coordinates.
There is a force/torque \(\overrightarrow{F_m}\) in model coordinates, which is equivalent to \(\overrightarrow{F_w}\) in world coordinates.
Define the following matrices:
\[ R_\alpha = \begin{bmatrix} \cos\alpha & -\sin\alpha & 0 \\ \sin\alpha & \cos\alpha & 0 \\ 0 & 0 & 1 \end{bmatrix} \]
\[ R_\beta = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos\beta & -\sin\beta \\ 0 & \sin\beta & \cos\beta \end{bmatrix} \]
\[ R_\gamma = \begin{bmatrix} \cos\gamma & -\sin\gamma & 0 \\ \sin\gamma & \cos\gamma & 0 \\ 0 & 0 & 1 \end{bmatrix} \]

\subsection{Generalized force from 3D force on center-of-mass}
You are given a system consisting of one object, which may or may not rotate. Its position is as above,
\[ position = \begin{bmatrix} f(\overrightarrow{x}) \\ g(\overrightarrow{x}) \\ h(\overrightarrow{x}) \end{bmatrix} \]
You apply an external force \(\overrightarrow{F_w}\) (in 3D world coordinates) on the object at its center-of-mass.
The generalized force on the coordinate \(x_i\) is
\[ Q_{x_i} = \frac{\partial position}{\partial x_i}^T \overrightarrow{F_w} \]
So the generalized force vector must be
\[ Q = \begin{bmatrix} \nabla f & \nabla g & \nabla h \end{bmatrix} \overrightarrow{F_w} \]

\subsection{Generalized force from 3D torque}
Now instead of applying a force, you apply a torque \(\overrightarrow{T_w}\) about the center-of-mass, in world coordinates.
Alternatively, apply a torque \(\overrightarrow{T_m}\) in model coordinates (i.e. rotated according to current Euler angles).
\newpage

\section{Lagrangian Models}
The racer includes 2 objects.
The bottom has position x,y,z, rotated by theta.
The top is attached to a massless rigid rod, of which the other end is attached to the bottom.
To get to the top: start with (x,y,z), rotate left by theta, tilt right by phi, go up by length.
When you're gliding on the floor, z = f(x,y) where f describes the floor.
When you're falling, z is an additional generalized coordinate.

\newpage

\section{Camera}
TODO: write out this section to describe good camera movement around the lagrangian models

\label{end}\end{document}


\documentclass{tufte-handout}

\usepackage{xcolor}
\usepackage{graphicx}

% set hyperlink attributes
\hypersetup{colorlinks}

\usepackage{amsmath}

% set image attributes:
\usepackage{graphicx}
\graphicspath{ {images/} }

% create environment for bottom paragraph:
\newenvironment{bottompar}{\par\vspace*{\fill}}{\clearpage}

% ============================================================

% define the title
\title{SOC 4930/5050: Week 04 Equations Quick \\Reference}
\author{Christopher Prener, Ph.D.}
\date{September 18\textsuperscript{th}, 2017}
% ============================================================
\begin{document}
% ============================================================
\maketitle % generates the title
% ============================================================

\vspace{5mm}
\section{Additive Law}
\begin{fullwidth}
\begin{equation}
\scalebox{2} {$ P(A \cup B)= P(A)+P(B)-P(A \cap B) $}
\end{equation}
\end{fullwidth}

\vspace{5mm}
\section{Conditional Probability}
\begin{subequations}
\begin{equation}
\scalebox{2} {$ P(A | B)= \frac{P(A \cap B)}{P(B)} $}
\end{equation}
\vspace{3mm}
\par \noindent \begin{equation}
\scalebox{2} {$ P(B | A)= \frac{P(A \cap B)}{P(A)} $}
\end{equation}
\end{subequations}

\vspace{5mm}
\section{Multiplicative Law}
\begin{equation}
\scalebox{2} {$ P(A \cap B)= P(A)*P(B|A) $}
\end{equation}

\vspace{5mm}
\section{Independence}
\begin{equation}
\scalebox{2} {$ P(A \cap B)= P(A)*P(B) $}
\end{equation}

\vspace{5mm}
\section{Bayes' Theorem}
The posterior probability can be calculated using this simplified formula:
\begin{equation}
\scalebox{2} {$ \frac { xy }{ xy+z(1-x) } $}
\end{equation}

% ============================================================
\end{document}