\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{aeguill}
\usepackage{hyperref}
\usepackage{verbatim}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{tikz}
\usepackage{a4wide}

\newcommand{\strid}{\textsl{strid}}
\newcommand{\example}[1]{\begin{center}\fbox{\begin{minipage}{11cm}#1\end{minipage}}\end{center}}
\newcommand{\cinput}[1]{\vcenter{\hbox{\input{#1}}}}

\title{strid -- A string diagrams generator}
\author{Samuel Mimram}
\begin{document}
\maketitle
\tableofcontents
\newpage

\strid{} is a string diagrams generator for inclusion into \LaTeX{} files. It is
still in very $\alpha$ stage but already quite useable. It is entirely
programmed in OCaml\footnote{OCaml can be downloaded at
  \url{http://caml.inria.fr/}.}. Feel free to drop me a line at
\url{samuel.mimram@pps.jussieu.fr} if you have some comments, bug reports or
feature requests about it.

\section{Presentation of \strid{}}
\subsection{A first example}
%\begin{figure}[ht!]
\example{ Suppose that $(\mathcal{C},\otimes,I)$ is a strict monoidal
  category. A \emph{monoid} in $\mathcal{C}$ is an object $M$ of $\mathcal{C}$
  together with two maps $\mu:M\otimes M\to M$, called \emph{multiplication},
  and $\eta:I\to M$, called \emph{unit}, respectively drawn as
\[
\input{monoid_mult.tex}
\]
and
\[
\input{monoid_unit.tex}
\]
such that the equalities
\[
\cinput{monoid_assoc_l.tex}
=
\cinput{monoid_assoc_r.tex}
\]
and
\[
\cinput{monoid_neutral_l.tex}
=
\cinput{monoid_neutral.tex}
=
\cinput{monoid_neutral_r.tex}
\]
hold.
}
%\caption{Definition of monoid objects.}
%\end{figure}

Let's have a look at how we typeset the left member of the associativity
equation:
\[
\input{monoid_assoc_l.tex}
\]
The \strid{} code for this figure is
\verbatiminput{monoid_assoc_l.strid}

Despite it's apparent complexity, this code is very simple! Like every \strid{}
diagram, this code starts with ``\verb+matrix {+'' and ends with
  ``\verb+}+''. Between those lines comes the actual description of the
diagram. It is structured as a matrix whose colums are separated by ``\verb+&+''
and whose lines are separated by ``\verb+\\+''.

The rightmost multiplication is typeset by
\begin{center}
\verb+mult(ull,dl,r)+
\end{center}
Here, ``\verb+mult+'' is the kind of the operator (a multiplication-shaped one)
and its arguments specify that it should be linked to the relative positions
$(-2,1)$ (\verb+ull+ means up-left-left), $(-1,-1)$ (\verb+dl+ means down-left)
and $(1,0)$. The order in which the links should be specified is indicated on
the figure below:
\[
\input{order_mult.tex}
\]
As for other operators, links are specified inputs first and then outputs.

The labels are specified similarly by instructions like
\begin{center}
\verb+text(r)[l,t=#$M$#]+
\end{center}
This create a ``\verb+text+'' operator from here to the relative position
$(1,0)$. The brackets ``\verb+[l,t=#$M$#]+'' are here to specify optional
parameters related to this operator. The ``\verb+l+'' indicates that we are
going to add a label and the ``\verb+t=#$M$#+'' means that the label's text
should be ``\verb+$M$+''. The text between \verb+#+ is quoted uninterpreted.

Suppose that we have put the text of this figure in a file named
\verb+monoid_assoc_l.strid+. Compiling this file can be simply done by typing
\begin{center}
\verb+strid monoid_assoc_l.strid+
\end{center}
This generates a file \verb+monoid_assoc_l.tex+ which can be used in a \LaTeX{}
file like:
\begin{verbatim}
\documentclass{article}

\usepackage{tikz}

\begin{document}
\input{monoid_assoc_l.tex}
\end{document}
\end{verbatim}
You will need the \texttt{TikZ} package which can be downloaded at
\url{http://sourceforge.net/projects/pgf/}.

\bigskip

Similarly, the right member of the equation is generated in a file
\verb+monoid_assoc_r.tex+. To have the equality sign between the two diagrams
centered vertically you need to center the two diagrams. This can be done using
the \verb+\vcenter+ and \verb+\hbox+ \LaTeX{} commands as shown in the following
example:
\begin{verbatim}
\[
\vcenter{\hbox{\input{monoid_assoc_l.tex}}}
=
\vcenter{\hbox{\input{monoid_assoc_r.tex}}}
\]
\end{verbatim}

\subsection{Visualizing your diagram}
Making a nice diagram is sometimes hard and \LaTeX{} compilation of the diagrams
usually takes some time to complete. If you want to quickly see the diagram
generated by \strid{} on a file \texttt{toto.strid}, type the command
\begin{center}
  \texttt{strid -g toto.strid}
\end{center}
This will open a window in which the output diagram is displayed, which is
refreshed every time the file \texttt{toto.strid} is changed.

\section{The operators}
\subsection{Line: \texttt{line}}
\[
\input{order_line.tex}
\]
\subsection{Multiplication: \texttt{mult}}
\[
\input{order_mult.tex}
\]
\subsection{Unit: \texttt{unit}}
\[
\input{order_unit.tex}
\]
\subsection{Adjunction: \texttt{adj}}
\[
\input{order_adj.tex}
\]
\subsection{Symmetry: \texttt{sym}}
\[
\input{order_sym.tex}
\]
\subsection{Braiding: \texttt{braid}}
\[
\input{order_braid.tex}
\]
\subsection{$m,n$-ary box: \texttt{$m$box$n$}}
\[
\input{order_nboxm.tex}
\]
\subsection{Vertical box: \texttt{vbox}}
\[
\input{order_vbox.tex}
\]

\subsection{Region: \texttt{region}}
Regions can be delimited:
\[
\input{pl_composition.tex}
\]
is typeset by
\verbatiminput{pl_composition.strid}

\section{Parameters of operators}
\subsection{Labels}
Labels can be added to operators. For example the diagram
\[
\input{mult_label.tex}
\]
can be typeset by \verbatiminput{mult_label.strid} If you don't like the size of
the ellipse surrounding the label, this can of course be changed. For example,
\[
\input{mult_small_label.tex}
\]
can be typeset by
\verbatiminput{mult_small_label.strid}

Various shapes are available for labels:
\subsubsection{Triangles: \texttt{triangle} / \texttt{t}}
For example,
\[
\input{bang_maybe_cut.tex}
\]
can be typeset by
\verbatiminput{bang_maybe_cut.strid}

Here, the \texttt{s} parameter is the shape (currently, only \texttt{ellipse}
and \texttt{triangle} are available), the \texttt{d} parameter is the direction
of the triangle and the \texttt{c} parameter specifies the color of the
triangle.

\subsubsection{Rectangles: \texttt{rectangle} / \texttt{r}}
\[
\cinput{monoidal_bifunct_l.tex}
\quad=\quad
\cinput{monoidal_bifunct_r.tex}
\]
The left member is typeset by
\verbatiminput{monoidal_bifunct_l.strid}

\subsection{Arrows}
Lines can be oriented using the \texttt{a} attribute. For example,
\[
\input{mult_a.tex}
\]
can be typeset by \verbatiminput{mult_a.strid} To specify that the direction
should be backwards use the \texttt{d=b} subattribute. For example,
\[
\input{mult_ab.tex}
\]
can by typeset by \verbatiminput{mult_ab.strid}

\section{Configuration files}
All parameters can be saved in a configuration file named
\texttt{strid.conf}. To generate a configuration file, type
\begin{verbatim}
strid --dump-conf
\end{verbatim}
You can then edit \texttt{strid.conf}.

Some of the options that can be set are:
\begin{itemize}
\item \verb+line_width+: default width of a line
\item \verb+label_width+: default width of a label
\item \verb+label_height+: default height of a label
\item \verb+no_tex_environment+: do not output \verb+\begin{tikz}+ and
    \verb+\end{tikz}+
\item \verb+scaling_factor+: scale the diagrams
\item \verb+label_triangle_height+: default height of a triangular label
\item \verb+label_rectangle_width+: default width of a rectangular label
\item \verb+label_rectangle_height+: default height of a rectangular label
\item \verb+interpolation+: interpolation method for drawing lines (possible
  values are \verb+cspline+ and \verb+linear+)
\item \verb+small_circle_ray+: ray of small circles (used to tweak the drawing
  of multiplications)
\end{itemize}

\section{Examples}
\subsection{Yang-Baxter equality for braids}
\[
\cinput{yang_baxter_l.tex}
\qquad=\qquad
\cinput{yang_baxter_r.tex}
\]
Left member is typeset by
\verbatiminput{yang_baxter_l.strid}
and right member by
\verbatiminput{yang_baxter_r.strid}

\subsection{Hopf law for bialgebras}
\[
\cinput{bialgebra_hopf_l.tex}
\quad=\quad
\cinput{bialgebra_hopf_r.tex}
\]
Left member is typeset by
\verbatiminput{bialgebra_hopf_l.strid}
and right member by
\verbatiminput{bialgebra_hopf_r.strid}

\newcommand{\A}{\mathbb{A}}
\newcommand{\B}{\mathbb{B}}
\newcommand{\C}{\mathbb{C}}
\subsection{Naturality condition for natural transformations between two lax functors between bicategories}

\begin{center}
\begin{tabular}{ccc}
{\scriptsize $\input{bicat_nat_transfo_l.tex}$}
&
\qquad
\raisebox{5cm}{$=$}
\qquad
&
{\scriptsize $\input{bicat_nat_transfo_r.tex}$}
\end{tabular}
\end{center}

\noindent The code for the left-hand side of the equation is
{\scriptsize \verbatiminput{bicat_nat_transfo_l.strid}}
\noindent and the code for the right-hand side is
{\scriptsize \verbatiminput{bicat_nat_transfo_r.strid}}

\end{document}

Higher category models of the pi-calculus hctm/strid-0.1.0/doc/stridman.tex


\documentclass[a4paper]{ltxdoc}
\usepackage[a4paper,left=2.25cm,right=2.25cm,top=2.5cm,bottom=2.5cm,nohead]{geometry}
\usepackage{amsmath}
\usepackage{makeidx}
\usepackage{tikz}
\usepackage[T1]{fontenc}
\usepackage{trace}

% These are required to typeset the manual.
\usepackage{calc}
\usepackage{hyperref}

% We can't use \usetikzlibrary{spline} because TeX won't find the correct
% file one directory up. So fake it.
%\usetikzlibrary{spline}
\makeatletter
\input{../tikzlibraryspline.code}
\let\tikz@library@spline@loaded\empty
\makeatother

\input{pgfmanual-en-macros}
\makeindex

\title{\tikzname\ spline library}
\author{Stephen Checkoway}
\date{2018-03-13}

\begin{document}
\maketitle

\section{Constructing splines through points}

\begin{tikzlibrary}{spline}
This library enables drawing cubic B\'ezier splines through specified points.
\end{tikzlibrary}

When you load this library, the following options become available:

\begin{keylist}{%
    /tikz/spline through=\meta{coordinates or nodes},
    /tikz/closed spline through=\meta{coordinates or nodes}}
These options must be given to a |to| or |edge| path command. The
\meta{coordinates or nodes} should be a sequence of \tikzname\ coordinates or
node names, one directly after the other without commas (like with the
|plot coordinates| path operation). Examples are |(1,0) (2,2)| or
|(a) (1,0) (b)|, where |a| and |b| are nodes. For |closed spline through|, the
\meta{coordinates or nodes} must contain at least two coordinates or node
names.

A spline is constructed from the current position through each of the
\meta{coordinates or nodes} and ending either at the target of the |to| or |edge|
path command for |spline through| or at the current position for
|closed spline through|.
\begin{codeexample}[]
\begin{tikzpicture}[thick]
  \draw[help lines] (0,0) grid (3,2);
  \node[draw=red, circle] (A) at (1,0) {A};
  \node[draw=blue, rectangle] (B) at (2,2) {B};
  \node[draw=green!60!black, circle] (C) at (3,1) {C};

  \draw (A) to[spline through={(0,1)(B)(2,1)}] (C);
\end{tikzpicture}
\end{codeexample}

\begin{codeexample}[]
\begin{tikzpicture}[thick]
  \draw[help lines] (0,0) grid (3,2);
  \node[draw=red, circle] (A) at (1,0) {A};
  \node[draw=blue, rectangle] (B) at (2,2) {B};
  \node[draw=green!60!black, circle] (C) at (3,1) {C};

  \draw (A) to[closed spline through={(0,1)(B)(2,1)(C)}] (A);
\end{tikzpicture}
\end{codeexample}

If you want the curve to not stop at the border of a node, the |center| anchor
(or any other node anchor) can be given.
\begin{codeexample}[]
\begin{tikzpicture}[thick]
  \draw[help lines] (0,0) grid (3,2);
  \node[draw=red, circle] (A) at (1,0) {A};
  \node[draw=blue, rectangle] (B) at (2,2) {B};
  \node[draw=green!60!black, circle] (C) at (3,1) {C};

  \draw (A.135) to[spline through={(0,1)(B.south)(2,1)}] (C.center);
\end{tikzpicture}
\end{codeexample}

\begin{codeexample}[]
\begin{tikzpicture}[thick]
  \draw[help lines] (0,0) grid (3,2);
  \node[draw=red, circle] (A) at (1,0) {A};
  \node[draw=blue, rectangle] (B) at (2,2) {B};
  \node[draw=green!60!black, circle] (C) at (3,1) {C};

  \draw (A.135) to[closed spline through={(0,1)(B.south)(2,1)(C.center)}] (A);
\end{tikzpicture}
\end{codeexample}
\end{keylist}

\begin{key}{/tikz/spline coordinates=\meta{name prefix} (default spline)}
This option must be given to a |to| or |edge| path command that is also given
one of |spline through| or |closed spline through|.

The spline's ``knot'' points---the specified points of the spline---and
control points are made available as coordinates. If the spline has $n$
segments, then the knot points $K_1,K_2,\dotsc,K_{n+1}$ have names \meta{name prefix}
|K-1|, \meta{name prefix} |K-2|, and so on.
(For a closed spline, the $(n+1)$th knot point is the starting
point of the spline.) The $2n$ control points $P_1,P_2,\dotsc,P_n$ and
$Q_1,Q_2,\dotsc,Q_n$
are available with names \meta{name prefix} |P-1|, \meta{name prefix} |Q-1|,
\meta{name prefix} |P-2|, \meta{name prefix} |Q-2|, and so on.

The $i$th segment of the spline is a cubic B\`ezier curve from $K_i$ to
$K_{i+1}$ with control points $P_i$ and $Q_i$. In \tikzname, the third segment
can be expressed as\\[.5\baselineskip]
\begingroup
\frenchspacing
\null\quad |(spline K-3) .. controls (spline P-3) and (spline Q-3) .. (spline K-4)|
\endgroup

The number of segments is available as the \TeX\ macro
\cs{tikzsplinesegments}.

\begin{codeexample}[]
\begin{tikzpicture}[thick]
  \draw[help lines] (0,0) grid (3,2);
  \coordinate (A) at (1,0);
  \coordinate (B) at (2,2);
  \coordinate (C) at (3,1);

  \draw (A) to[spline coordinates=S,
               closed spline through={(0,1)(B)(2,1)(C)}] (A);
  \foreach \i [evaluate=\i as \j using \i+1] in {1,2,...,\tikzsplinesegments} {
    \draw[semithick]  (S K-\i) -- (S P-\i)
                      (S K-\j) -- (S Q-\i);
    \path[fill=green!60!black] (S P-\i) circle (2pt);
    \path[fill=red] (S Q-\i) circle (2pt);
  }
\end{tikzpicture}
\end{codeexample}
\end{key}

\section{Intersection with shapes}
When \tikzname\ creates a B\'ezier curve where one or both of the end points
is a node, it constructs the line through the center of the node tangent to
the curve at that point. \tikzname\ then computes the intersection of the line
with the node's background path. Finally, it adjusts the end point of the
curve to be the intersection point. This happens without changing the curve's
control points. The result is a completely different curve. Compare the red
and black curves.
\begin{codeexample}[]
\begin{tikzpicture}[thick]
  \draw[help lines] (0,0) grid (3,2);
  \node[draw, circle] (A) at (0,1) {A};
  \node[draw] (B) at (3,1) {B};
  \draw[red] (A.center) .. controls +(0,1) and +(0,1) .. (B.center);
  \draw (A) .. controls +(0,1) and +(0,1) .. (B);
\end{tikzpicture}
\end{codeexample}

Normally, this difference is not a big deal and it's fast for \pgfname\ to
compute this border point. Unfortunately, when the same procedure is applied
to a B\`ezier spline, the result is slightly unsettling.

In the example below, the red line has been drawn explicitly from the
|closed spline through|'s knot and control points whereas the black line uses
the same control points but the start and end points have been modified
according to \tikzname's normal procedure.
\begin{codeexample}[]
\begin{tikzpicture}[thick]
  \draw[help lines] (0,0) grid (3,3);
  \node[draw,circle]    (A) at (0,1)   {A};
  \node[draw]           (B) at (3,1)   {B};
  \node[draw,circle]    (C) at (2,3)   {C};
  \node[draw,circle]    (D) at (1.5,0) {D};

  \path
    (A.center) to[spline coordinates=S,
                  closed spline through=(B.center)(C.center)(D.center)] (A);
  \draw[red]
    (A.center) .. controls (S P-1) and (S Q-1) .. (B.center)
               .. controls (S P-2) and (S Q-2) .. (C.center)
               .. controls (S P-3) and (S Q-3) .. (D.center)
               .. controls (S P-4) and (S Q-4) .. (A.center);
  \draw (A) .. controls (S P-1) and (S Q-1) .. (B)
            .. controls (S P-2) and (S Q-2) .. (C)
            .. controls (S P-3) and (S Q-3) .. (D)
            .. controls (S P-4) and (S Q-4) .. (A);
\end{tikzpicture}
\end{codeexample}
None of the black line's intersections with the node borders look quite right.

The |spline| library uses the \pgfname\ |intersections| library to compute the
intersection of the spline with node background shapes. This is slower, but
the result is significantly better.

\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,3);
  \node[draw,circle]    (A) at (0,1)   {A};
  \node[draw]           (B) at (3,1)   {B};
  \node[draw,circle]    (C) at (2,3)   {C};
  \node[draw,circle]    (D) at (1.5,0) {D};

  \path
    (A.center) to[spline coordinates=S,
                  closed spline through=(B.center)(C.center)(D.center)] (A);
  \draw[red]
    (A.center) .. controls (S P-1) and (S Q-1) .. (B.center)
               .. controls (S P-2) and (S Q-2) .. (C.center)
               .. controls (S P-3) and (S Q-3) .. (D.center)
               .. controls (S P-4) and (S Q-4) .. (A.center);
  \draw[thick] (A) to[closed spline through=(B)(C)(D)] (A);
\end{tikzpicture}
\end{codeexample}

\section{Placing nodes along a spline}
Nodes can be placed along a spline as with any other standard path by giving a
|pos=|\meta{time} option to the node.
\begin{codeexample}[]
\begin{tikzpicture}[thick]
  \draw[help lines] (0,0) grid (3,2);
  \node[draw=red, circle] (A) at (1,0) {A};
  \node[draw=blue, rectangle] (B) at (2,2) {B};
  \node[draw=green!60!black, circle] (C) at (3,1) {C};

  \draw (A) to[spline through={(0,1)(B)(2,1)}, node font=\footnotesize]
    node foreach \pos in {0, .25, .5, .75, 1}
      [pos=\pos, fill=blue, circle, inner sep=1pt] {}
    node[pos=0, auto] {$0$}
    node[pos=.25, auto] {$.25$}
    node[pos=.5, auto] {$.5$}
    node[pos=.75, auto, swap] {$.75$}
    node[pos=1, auto] {$1$}
    (C);
\end{tikzpicture}
\end{codeexample}
Note that the distance between |pos=.25| and |pos=.5| is much greater than the
distance between |pos=.75| and |pos=1|.

The position along the curve is computed by first dividing $[0,1]$ into $n$
equal-size intervals for a spline with $n$ segments. After selecting the
segment, the position along the segment is computed in the normal fashion for
curves with one exception. If the start of the spline is a node with a
background shape, |pos=0| is set to the intersection of the curve and the
shape. Similarly, if the end of the spline is a node with a background shape,
|pos=1| is set to the intersection of the curve with the shape.

Just as with normal \tikzname\ curves, positioning nodes requires a bit of a
trial-and-error.

\section{Debugging constructed splines}
\begin{key}{/tikz/spline debug=\meta{boolean} (default true, initially false)}
When given as an option to a |to| or |edge| path command, this causes
debugging output to be written to the log and the terminal. It is not intended
to be useful to end users but the author found it invaluable for debugging the
code.
\end{key}

\printindex
\end{document}
% vim: set sw=2 sts=2 ts=8 et:


\documentclass{beamer}

\usepackage{kotex}
\usepackage{graphicx,psfrag,amsfonts,amsmath,amssymb}
\usepackage{multicol}
\usepackage{algorithm2e}

\usetheme{metropolis}
\usefonttheme[onlymath]{serif}	% 수식 설정!!
\setbeamertemplate{section in toc}[sections numbered]

\title{Principal Component Models for Sparse Functional Data}
%\date{\today}
\date[Short Occasion]{August 9, 2019}
\author{Hyunsung Kim}
\institute{Department of Statistics\\ Chung-Ang University}

% A subtitle is optional and this may be deleted
\subtitle{}

%\author{F.~Author\inst{1} \and S.~Another\inst{2}}
% - Give the names in the same order as the appear in the paper.
% - Use the \inst{?} command only if the authors have different
%   affiliation.

%\institute[Universities of Somewhere and Elsewhere] % (optional, but mostly needed)
%{
%  \inst{1}%
%  Department of Computer Science\\
%  University of Somewhere
%  \and
%  \inst{2}%
%  Department of Theoretical Philosophy\\
%  University of Elsewhere}
% - Use the \inst command only if there are several affiliations.
% - Keep it simple, no one is interested in your street address.

%\date{Conference Name, 2013}
% - Either use conference name or its abbreviation.
% - Not really informative to the audience, more for people (including
%   yourself) who are reading the slides online

\subject{Functional Data Analysis}
% This is only inserted into the PDF information catalog. Can be left
% out.

% If you have a file called "university-logo-filename.xxx", where xxx
% is a graphic format that can be processed by latex or pdflatex,
% resp., then you can add a logo as follows:

% \pgfdeclareimage[height=0.5cm]{university-logo}{university-logo-filename}
% \logo{\pgfuseimage{university-logo}}

% Delete this, if you do not want the table of contents to pop up at
% the beginning of each subsection:
\AtBeginSubsection[]
{
  \begin{frame}<beamer>{Outline}
    \tableofcontents[currentsection,currentsubsection]
  \end{frame}
}

\def \bY {\mathbf{Y}}
\def \by {\mathbf{y}}
\def \bX {\mathbf{X}}
\def \bB {\mathbf{B}}
\def \bD {\mathbf{D}}
\def \bbeta {\boldsymbol{\beta}}
\def \btheta {\boldsymbol{\theta}}
\def \bTheta {\boldsymbol{\Theta}}
\def \bepsilon {\boldsymbol{\epsilon}}
\def \balpha {\boldsymbol{\alpha}}
\def \bgamma {\boldsymbol{\gamma}}
\def \bGamma {\boldsymbol{\Gamma}}
\def \bR {\boldsymbol{R}}
\def \bZ {\boldsymbol{Z}}
\def \bG {\boldsymbol{G}}
\def \bu {\boldsymbol{u}}
\def \bV {\boldsymbol{V}}

% Let's get started
\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\begin{frame}{Outline}
  \tableofcontents
  % You might wish to add the option [pausesections]
\end{frame}

% Section and subsections will appear in the presentation overview
% and table of contents.
\section{Reduced rank model}
\begin{frame}{Reduced rank model}
	\begin{block}{Reduced rank model}
	\vspace{0.1cm}
	$$ \bY_i=\mathbf{B}_i\btheta_{\mu}+\mathbf{B}_i\bTheta\balpha_i + \bepsilon_i $$
	where $\btheta_{\mu}$ is $q \times 1$ vector, $\bTheta$ is $ q \times k$ matrix, $\balpha_i$ is $k \times 1$ vector,
	$$ \bepsilon_i \sim (\mathbf{0}, \sigma^2 \mathbf{I}), \ \balpha_i \sim (\mathbf{0}, \mathbf{D}) $$
	subject to
	$$ \bTheta^T \bTheta=\mathbf{I}, \ \int \mathbf{b}(t)^T\mathbf{b}(t)dt=1, \ \int\int \mathbf{b}(t)^T\mathbf{b}(s)dt=0 $$
	\end{block}

\end{frame}

\begin{frame}{Reduced rank model}
	\begin{block}{How to find functional PCs}
		\vspace{0.1cm}
		\begin{itemize}
			\item {
				mean function\\
				$\mu(t)=\mathbf{b}(t)^T\btheta_{\mu}$
			}
			\item {
				PC function\\
				$f(t)^T=\mathbf{b}(t)^T\bTheta$
			}
		\end{itemize}
		where $\mathbf{b}(t)$ is the orthonormal spline basis and it can be computed by Gram–Schmidt orthonormalization.(Zhou et al.(2008))
	\end{block}

	To find the PC curves, We should estimate $\btheta_{\mu}, \bTheta$.
\end{frame}

\section{EM Algorithm}
\begin{frame}{EM Algorithm}
	\begin{block}{Maximum likelihood (or Penalized least squares)}
		\vspace{0.5cm}
		In the reduced rank model, we should minimized $L(\btheta_{\mu}, \bTheta, \mathbf{D}, \sigma^2)$,
		$$
		\sum_{i=1}^N \bigg \{ (\bY_i-\mathbf{B}_i\btheta_{\mu}-\mathbf{B}_i\bTheta\balpha_i)^T(\bY_i-\mathbf{B}_i\btheta_{\mu}-\mathbf{B}_i\bTheta\balpha_i) + \sigma^2 \balpha_i^T \bD^{-1}\balpha_i \bigg \}
		$$
	\end{block}
	We can minimize this equation using the \texttt{EM} algorithm.
\end{frame}

\begin{frame}{EM Algorithm}
	\begin{block}{Complete data}
		\vspace{0.2cm}
		Let $\balpha_i$ is the latent variable(unobserved), then we can define the complete data $ \mathbf{Z} = (\bY, \balpha)$ and employ the \texttt{EM} algorithm.
	\end{block}
	\vspace{1cm}
	Let $\mathbf\Omega = (\btheta_{\mu}, \bTheta)$ and $L(\mathbf\Omega|\mathbf{Z}) = -L(\btheta_{\mu}, \bTheta, \mathbf{D}, \sigma^2)$, then the minimization problem will be equivalent to maximize $L(\mathbf\Omega|\mathbf{Z})$.
\end{frame}

\begin{frame}{EM Algorithm}
	\begin{block}{E-step}
		\vspace{0.1cm}
		Compute the expectation of the objective function($L(\mathbf\Omega|\mathbf{Z})$) for complete data $\mathbf{Z}$,
		$$
		Q(\mathbf\Omega|\mathbf\Omega^{(t)}) = E\left\{ L(\mathbf\Omega|\mathbf{Z})|\bY,\mathbf\Omega^{(t)} \right\}
		$$
		where $\mathbf\Omega^{(t)} = (\btheta_{\mu}^{(t)}, \bTheta^{(t)})$.\\
		Also we can predict $\balpha_i$ on the E-step.
	\end{block}
\end{frame}

\begin{frame}{EM Algorithm}
	\begin{block}{M-step}
		\vspace{0.1cm}
		$$
		\mathbf\Omega^{(t+1)} = \arg\max_\mathbf\Omega Q(\mathbf\Omega|\mathbf\Omega^{(t)})
		$$
		Also we can estimate $\mathbf{D}, \sigma^2$ on the M-step.
	\end{block}
	\vspace{0.5cm}
	With many \texttt{EM} iterations, it is known to converge to true parameters.
\end{frame}

\begin{frame}{EM Algorithm}
	\begin{algorithm}[H]
		\KwResult{ The procedure to fit the reduced rank model }
		initialization $( \widehat\btheta^{(1)}_\mu, \widehat\bTheta^{(1)}, \widehat{\balpha}^{(1)}_i, \widehat\bD^{(1)}, \widehat{\sigma^2}^{(1)} )$\;
		\For{$i=2$ to $iterations$}
		 {\vspace{0.2cm}
		 	$$
		 	\begin{aligned}
		 		\text{(M-step)}\\
		 		\widehat{\sigma^2}^{(t+1)} \ &\leftarrow \ ( \widehat\btheta^{(t)}_\mu, \widehat\bTheta^{(t)}, \widehat{\balpha}^{(t)}_i, \widehat\bD^{(t)}, \widehat{\sigma^2}^{(t)} )\\
		 		\widehat\bD^{(t+1)}_{jj} \ &\leftarrow \ ( \widehat\bTheta^{(t)}, \widehat{\balpha}^{(t)}_i, \widehat\bD^{(t)}, \widehat{\sigma^2}^{(t+1)} )\\
		 		\widehat\btheta^{(t+1)}_{\mu} \ &\leftarrow \ ( \widehat\bTheta^{(t)}, \widehat{\balpha}^{(t)}_i )\\
		 		\widehat\bTheta^{(t+1)} \ &\leftarrow \ ( \widehat\btheta^{(t)}_\mu, \widehat\bTheta^{(t)}, \widehat{\balpha}^{(t)}_i, \widehat\bD^{(t+1)}, \widehat{\sigma^2}^{(t+1)} )\\
		 		\text{(E-step)}\\
		 		\widehat\balpha_i^{(t+1)} \ &\leftarrow \ ( \widehat\bTheta^{(t+1)}, \widehat{\balpha}^{(t+1)}_i, \widehat\bD^{(t+1)}, \widehat{\sigma^2}^{(t+1)} )
		 	\end{aligned}
		 	$$
	 	 }
	\end{algorithm}
\end{frame}

\section{Applications}
\begin{frame}{Applications}
	\begin{block}{Bone Mineral Density data}
		\begin{itemize}
			\item {
				48 white females
			}
			\item {
				160 observations
			}
			\item {
				It was measured at the different time points and sparsely observed.
			}
		\end{itemize}
	\end{block}
\end{frame}

\begin{frame}{Applications}
	\begin{figure}[h] %%% t: top, b: bottom, h: here
		\begin{center}
			\includegraphics[width=0.8\linewidth]{img/curve.png}
		\end{center}
		\caption{The bone mineral density of 48 females}
		\label{fig:long}
		\label{fig:onecol}
	\end{figure}
	%\end{block}
\end{frame}

\begin{frame}{Applications}
	\begin{itemize}
		\item {
			Fit the reduced rank model using \texttt{EM} algorithm
		}
		\item {
			Initial values $=0.1 \ (\btheta_{\mu}, \bTheta, \mathbf{D}, \sigma^2, \balpha_i)$
		}
		\item {
			The number of PCs $=2$
		}
		\item {
			$100$ \texttt{EM} iterations
		}
	\end{itemize}
\end{frame}

\begin{frame}{Applications}
	\begin{block}{4 knots}
		\begin{figure}[h] %%% t: top, b: bottom, h: here
			\begin{center}
				\includegraphics[width=0.8\linewidth]{img/4knots_true.png}
				\includegraphics[width=0.8\linewidth]{img/4knots.png}
			\end{center}
			\label{fig:long}
			\label{fig:onecol}
		\end{figure}
	\end{block}
\end{frame}

\begin{frame}{Applications}
	\begin{block}{9 knots}
		\begin{figure}[h] %%% t: top, b: bottom, h: here
			\begin{center}
				\includegraphics[width=0.8\linewidth]{img/9knots_true.png}
				\includegraphics[width=0.8\linewidth]{img/9knots.png}
			\end{center}
			\label{fig:long}
			\label{fig:onecol}
		\end{figure}
	\end{block}
\end{frame}

\begin{frame}{Applications}
	\begin{block}{14 knots}
		\begin{figure}[h] %%% t: top, b: bottom, h: here
			\begin{center}
				\includegraphics[width=0.8\linewidth]{img/14knots_true.png}
				\includegraphics[width=0.8\linewidth]{img/14knots.png}
			\end{center}
			\label{fig:long}
			\label{fig:onecol}
		\end{figure}
	\end{block}
\end{frame}

\begin{frame}{Applications}
	\begin{block}{Loglikelihoods for the reduced rank fits}
		\begin{table}[ht]
			\centering
			\begin{tabular}{ccc}
				\hline
				Number \\of knots& Paper & Coding \\
				\hline
				$4$ & 389.22 & 263.22 \\
				$9$ & 409.81 & 296.23 \\
				$14$ & 411.36 & 308.98 \\
				\hline
			\end{tabular}
		\end{table}
	\end{block}
\end{frame}

\appendix
\section{Reference}
\begin{frame}
  \frametitle<presentation>{Reference}

  \begin{thebibliography}{10}
  	\beamertemplatearticlebibitems
	% Followed by interesting articles. Keep the list short.
	 \bibitem{Someone2000}
		James G.M., Hastie T.J., Sugar C.A.
		\newblock Principal component models for sparse functional data
		\newblock {\em Biometrika}, 87(3):587--602,
		2000.

  \beamertemplatebookbibitems
  % Start with overview books.
	\bibitem{Author1990}
		J.O. Ramsay, B.W. Silverman.
		\newblock {\em Functional Data Analysis 2nd edition}.
		\newblock Springer, 2005.

   	\beamertemplatearticlebibitems
		\bibitem{Someone2008}
		 Zhou L., Huang J.Z., Carroll R.J.
		 \newblock Joint modeling of paired sparse functional data using principal components
		 \newblock {\em Biometrika}, 95(3):601--619,
		 2008.

  \end{thebibliography}
\end{frame}

% All of the following is optional and typically not needed.
%\appendix
%\section<presentation>*{\appendixname}
%\subsection<presentation>*{For Further Reading}
%
%\begin{frame}[allowframebreaks]
%  \frametitle<presentation>{For Further Reading}
%
%  \begin{thebibliography}{10}
%
%  \beamertemplatebookbibitems
%  % Start with overview books.
%
%  \bibitem{Author1990}
%    A.~Author.
%    \newblock {\em Handbook of Everything}.
%    \newblock Some Press, 1990.
%
%
%  \beamertemplatearticlebibitems
%  % Followed by interesting articles. Keep the list short.
%
%  \bibitem{Someone2000}
%    S.~Someone.
%    \newblock On this and that.
%    \newblock {\em Journal of This and That}, 2(1):50--100,
%    2000.
%  \end{thebibliography}
%\end{frame}

\end{document}

Sparse FPCA/Application/Reduced rank model.tex


\subsubsection{Notation}
In the following sections, the concepts of the language of mbeddr are iteratively extended with new elements. For this purpose, based on the notation presented by Pierce \cite{TypesAndProgrammingLanguages}, the syntax of new concepts is described by notations like the following:
\begin{equation}
e ::= ...| e'
\end{equation}
This exemplary line extends the set of expressions of mbeddr by the expression \CODE{e'} which may contain arbitrary meta-variables like \CODE{e}. In order to fit into the type system of mbeddr, these concepts are equipped with \textit{type inference} rules, each of which, given a list of premises, derives the type of some concept in the conclusion:
\begin{equation}
\inference*[NewConcept]{\mathit{premise_1}, ..., \mathit{premise_n}}{\qquad\quad e |- t \qquad\quad}
\end{equation}
The premises of the concepts are kept minimal but not exhaustive, which means that complex premises are given as informal explanations and are mostly explained in section \ref{safetyMeasures}. This way, the type inference rules are kept comprehensible and the discussion of the safety measures of ParallelMbeddr are kept closely together. Additionally, this separation reflects MPS' separation of type inference rules and non-typesystem rules.

The translation of a new concept in ParallelMbeddr to a base concept of mbeddr will be shown informally by listing the resulting code that is generated by the IDE after it was given some input code. To this end, if not expressed in the text, the symbol $\Longrightarrow$ will denote the translation of code $c_1$ to some other code $c_2$ in $c_1 \Longrightarrow c_2$. The translation to basic concepts will often entail the generation of additional code somewhere else in the program as a side effect, e.g. the translation of an expression to a function call may force the generator of mbeddr to generate the declaration of the called function first. These side effects will also be demonstrated by listings of the generated code.


\subsection{Aibolit Index}

In addition to the recommendation functionality, we proppose \textit{Aibolit Index},
a score that measures the overall quality of a given software project.
It is a single number, with the following properties:
\begin{itemize}
\item[(i)] the more patterns are suggested to fix, the higher Aibolit Index;
\item[(ii)] the higher negative impact factor (see Section \ref{sec:recommendation_algorithm})
of detected patterns the higher Aibolit Index.
\end{itemize}
Therefore, the lower Abolit Index the better the project's code from the point
of view of Aibolit.

Let $P(C)$ be a set of all patterns Aibolit recommends to fix for Java class $C$.
% As we know from the section ~\ref{sec:recommendation_algorithm}, each
% recommended anti-pattern $p \in P(C)$ has an associated impact factor $I_{p}$ and a count $count(p, C)$.
We define the Aibolit Index $A(C)$ of Java class $C$ as a sum of the products of
impact factors $I_{p}$ and scaled counts $count(p, C)$ for all of the
patterns occurring in the class (Eq.~\ref{eq:aibolit_index}). We use log-scaling for smoothing purposes, because some patterns are a lot more common than others.

\begin{equation}
    A(C) = \sum_{p \in P(C)} { I_{p}(C) \cdot \ln{(count(p, C) + 1)} } \label{eq:aibolit_index}
\end{equation}

The Aibolit Index of a project is defined as average Aibolit Index
across all Java classes in the project. In Table~\ref{table:aibolit_index_repos}
you can see a calculated Aibolit Index of some GitHub Java repositories with
more than 4500 stars. Aibolit Index is supposed to be a convenient instrument
to get a first estimate of the project code's quality.

\begin{table}[t]
\footnotesize
    \begin{tabular}{|l|l|l|l|l|l|l|}
    \hline
    Repository & Aibolit Index & Total files & Total NCSS & GitHub stars \\
    \hline
    ReactiveX\textbackslash RxJava& 6.66& 1493& 25270 & 42972  \\
    bumptech\textbackslash glide& 5.81& 465& 5078 & 29364 \\
    JakeWharton\textbackslash butterknife& 6.31& 74& 935 & 25347  \\
    greenrobot\textbackslash EventBus& 7.48& 51& 651 & 22637  \\
    skylot\textbackslash jadx& 6.69& 602& 12658 & 22602 \\
    alibaba\textbackslash fastjson & 9.97& 144& 20175 & 21891 \\
    alibaba\textbackslash druid & 6.36& 822& 28852 & 21581 \\
    Netflix\textbackslash Hystrix& 6.74& 292& 2920 & 19888 \\
    ReactiveX\textbackslash RxAndroid& 5.47& 9& 59 & 19010 \\
    google\textbackslash gson& 6.87& 160& 2941 & 18084 \\
    square\textbackslash picasso& 6.21& 26& 687 & 17514 \\
    libgdx\textbackslash libgdx & 5.02 & 1981 & 46409 & 17105 \\
    nostra13\textbackslash Android-Universal-Image-Loader& 7.84& 62& 1059 & 16722  \\
    qiurunze123\textbackslash miaosha& 3.57& 197& 841 & 16224 \\
    wuyouzhuguli\textbackslash SpringAll& 13.41& 467& 463 & 15221 \\
    justauth\textbackslash JustAuth& 4.36& 46& 241 & 8916 \\
    heibaiying\textbackslash BigData-Notes& 11.17& 54& 204 & 7166  \\
    crossoverJie\textbackslash cim & 6.70& 96& 574 & 5841 \\
    wildfirechat\textbackslash server& 6.51& 285& 4671  & 5140 \\
    febsteam\textbackslash FEBS-Shiro & 5.66& 90& 453 & 4777 \\
    \hline
    \end{tabular}
    \centering
\caption{Aibolit Index of some popular Java repositories. \label{table:aibolit_index_repos}}
\end{table}


% Preamble.
\documentclass[12pt]{article}
\usepackage[margin=1.25in]{geometry}
\usepackage[fleqn]{amsmath}
\usepackage{textcomp}
\usepackage{gensymb}
\usepackage{amsfonts}
\usepackage{enumitem}
%\usepackage{tikz}  % Include for figures.
%\usepackage{subfiles}  % Include for subfiles.

%% Title macros.
\newcommand{\EXAMNUM}{1}
\newcommand{\NAME}{D. Choi}
\newcommand{\DATE}{2020-06-15}

\title{\vspace{-2\baselineskip}MATH 225 - Exam \#\EXAMNUM}
\author{\NAME}
\date{\DATE}

%% Formatting options.
%\pagenumbering{gobble}  % Include for single-page document.

% Macros.
%% Norm.
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}
%% Rotation and reflection matrices.
\newcommand{\rota}[1]{\text{Rot}(#1 \degree)}
\newcommand{\refl}[1]{\text{Ref}(#1 \degree)}
%% Augmented matrix.
% https://tex.stackexchange.com/a/2238
\newenvironment{amatrix}[1]{%
  \left(\begin{array}{@{}*{#1}{c}|c@{}}
}{%
  \end{array}\right)
}

% Document.
\begin{document}
\maketitle

\section*{1.}
\textit{Let $A$ be the matrix associated with the orthogonal projection onto
the plane $x + y - 3z = 0$.}
\begin{enumerate}[label=(\alph*)]
	\item \textit{Find $A$.} \\[\baselineskip]
	The equation for the plane $x + y - 3z = 0$ can be rewritten as
	\begin{equation*}
		\begin{pmatrix} 1 \\ 1 \\ -3 \end{pmatrix}
		\cdot
		\begin{pmatrix} x \\ y \\ z \end{pmatrix}
		= \vec{n} \cdot \vec{v}
		= 0
		.
	\end{equation*}
	In this form, it is apparent that $\vec{n}$ is a normal vector to the plane.
	\\
	For any vector $\vec{v}$, the vector rejection of $\vec{v}$ from $\vec{n}$
	(which is $\vec{v} - \text{proj}_{\vec{n}}\vec{v}$) will be the orthographic
	projection of $\vec{v}$ onto the plane. Thus, for the standard basis unit
	vectors,
	\begin{gather*}
		\begin{pmatrix} 1 \\ 0 \\ 0 \end{pmatrix}
		\overset{A}{\longrightarrow}
		\begin{pmatrix} 1 \\ 0 \\ 0 \end{pmatrix}
		-
		\frac{
			\begin{pmatrix} 1 \\ 0 \\ 0 \end{pmatrix}
			\cdot
			\begin{pmatrix} 1 \\ 1 \\ -3 \end{pmatrix}
		}{
			\norm{\begin{pmatrix} 1 \\ 1 \\ -3 \end{pmatrix}}^2
		}
		\begin{pmatrix} 1 \\ 1 \\ -3 \end{pmatrix}
		=
		\begin{pmatrix} 1 \\ 0 \\ 0 \end{pmatrix}
		-
		\frac{1}{11}
		\begin{pmatrix} 1 \\ 1 \\ -3 \end{pmatrix}
		=
		\begin{pmatrix}
			\frac{10}{11} \\ -\frac{1}{11} \\ \frac{3}{11}
		\end{pmatrix}
		\\
		\begin{pmatrix} 0 \\ 1 \\ 0 \end{pmatrix}
		\overset{A}{\longrightarrow}
		\ldots
		=
		\begin{pmatrix} 0 \\ 1 \\ 0 \end{pmatrix}
		-
		\frac{1}{11}
		\begin{pmatrix} 1 \\ 1 \\ -3 \end{pmatrix}
		=
		\begin{pmatrix}
			-\frac{1}{11} \\ \frac{10}{11} \\ \frac{3}{11}
		\end{pmatrix}
		\\
		\begin{pmatrix} 0 \\ 0 \\ 1 \end{pmatrix}
		\overset{A}{\longrightarrow}
		\ldots
		=
		\begin{pmatrix} 0 \\ 0 \\ 1 \end{pmatrix}
		+
		\frac{3}{11}
		\begin{pmatrix} 1 \\ 1 \\ -3 \end{pmatrix}
		=
		\begin{pmatrix}
			\frac{3}{11} \\ \frac{3}{11} \\ \frac{2}{11}
		\end{pmatrix}
	\end{gather*}
	And so,
	\begin{equation*}
		\boxed{
			A =
			\frac{1}{11}
			\begin{pmatrix}
				10 & -1 & 3 \\
				-1 & 10 & 3 \\
				-3 & 3 & 2
			\end{pmatrix}
		}
		.
	\end{equation*}

	\item \textit{Let $\{\vec{v}_1, \vec{v}_2, \vec{v}_3\}$ be the columns of
	$A$. Are $\{\vec{v}_1, \vec{v}_2, \vec{v}_3\}$ linearly independent? Why
	or why not?}\\[\baselineskip]
	$\{\vec{v}_1, \vec{v}_2, \vec{v}_3\}$ cannot be linearly independent
	because $A$ is a projection matrix from $\mathbb{R}^3$ onto a plane. The
	range of such a projection matrix does not include all of $\mathbb{R}^3$,
	meaning that the column space of $A$ does not include all of
	$\mathbb{R}^3$. The linear dependence of
	$\{\vec{v}_1, \vec{v}_2, \vec{v}_3\}$ can also be verified by computing
	the determinant of $A$, which should be $0$.
\end{enumerate}

\section*{2.}
\textit{Let $B$ be the matrix associated with the reflection across the plane
$x = 0$.}
\begin{enumerate}[label=(\alph*)]
	\item \textit{Let $\{\vec{w}_1, \vec{w}_2, \vec{w}_3\}$ be the columns of
	$B$. What is span$\{\vec{w}_1, \vec{w}_2, \vec{w}_3\}$? Explain your
	reasoning.} \\[\baselineskip]
	The span of $\{\vec{w}_1, \vec{w}_2, \vec{w}_3\}$ is $\mathbb{R}^3$.
	Such a reflection matrix $B$ is invertible. An invertible $n \times n$
	matrix over $\mathbb{R}$ has column space $\mathbb{R}^n$ because such a
	matrix must be bijective. Thus, the span of
	$\{\vec{w}_1, \vec{w}_2, \vec{w}_3\}$ is $\mathbb{R}^3$.

	\item \textit{Find all solutions to the system $AB\vec{x} = (1, 2, 1)$.}
	\\[\baselineskip]
	Solutions to $A\vec{v} = (1, 2, 1)$ are
	\begin{equation*}
		\vec{v} =
		\begin{pmatrix} 1 \\ 2 \\ 1 \end{pmatrix}
		+ s \begin{pmatrix} 1 \\ 1 \\ -3 \end{pmatrix}
	\end{equation*}
	Thus,
	\begin{equation*}
		B\vec{x} = \vec{v} =
		\begin{pmatrix} 1 \\ 2 \\ 1 \end{pmatrix}
		+ s \begin{pmatrix} 1 \\ 1 \\ -3 \end{pmatrix}
		.
	\end{equation*}
	Because $B$ is a reflection matrix, $B = B^{-1}$, and so
	\begin{align*}
		\vec{x} = B(
		\begin{pmatrix} 1 \\ 2 \\ 1 \end{pmatrix}
		+ s \begin{pmatrix} 1 \\ 1 \\ -3 \end{pmatrix}
		)
		=
		\boxed{
			\begin{pmatrix} -1 \\ 2 \\ 1 \end{pmatrix}
			+ B (s \begin{pmatrix} 1 \\ 1 \\ -3 \end{pmatrix})
		}
		.
	\end{align*}
\end{enumerate}

\section*{3.}
\textit{The reduced row echelon form of the left matrix $M$ is the right
matrix.}
\begin{equation*}
	\begin{pmatrix}
		1 & 1 & -1 & 1 \\
		2 & 1 & -1 & 3 \\
		1 & 4 & -4 & -2 \\
		2 & 0 & 1 & 2
	\end{pmatrix}
	\xrightarrow{\text{RREF}}
	\begin{pmatrix}
		1 & 0 & 0 & 2 \\
		0 & 1 & 0 & -3 \\
		0 & 0 & 1 & -2 \\
		0 & 0 & 0 & 0
	\end{pmatrix}
\end{equation*}
\begin{enumerate}[label=(\alph*)]
	\item \textit{Find the solution(s) to $M\vec{x} = 0$.}
	\begin{equation*}
		\begin{amatrix}{4}
			1 & 0 & 0 & 2 & 0 \\
			0 & 1 & 0 & -3 & 0 \\
			0 & 0 & 1 & -2 & 0 \\
			0 & 0 & 0 & 0 & 0
		\end{amatrix}
	\end{equation*}
	\begin{gather*}
		x + 2w = 0 \\
		y - 3w = 0 \\
		z - 2w = 0
	\end{gather*}
	Solutions to the system are of the form
	\begin{equation*}
		\begin{pmatrix}
			-2w \\ 3w \\ 2w \\ w
		\end{pmatrix}
		=
		\boxed{
			\begin{pmatrix}
				-2 \\ 3 \\ 2 \\ 1
			\end{pmatrix}
			w
		}
		.
	\end{equation*}

	\item \textit{Would it be easy to solve $M\vec{x} = (1, 1, 1, 0)$ without
	any further information?} \\[\baselineskip]
	Yes.
	\begin{gather*}
		x + 2w = 1 \\
		y - 3w = 1 \\
		z - 2w = 1
	\end{gather*}
	Solutions to the system are of the form
	\begin{equation*}
		\begin{pmatrix}
			1 -2w \\ 1 + 3w \\ 1 + 2w \\ w
		\end{pmatrix}
		=
		\boxed{
			\begin{pmatrix}
				1 \\ 1 \\ 1 \\ 0
			\end{pmatrix}
			+
			\begin{pmatrix}
				-2 \\ 3 \\ 2 \\ 1
			\end{pmatrix}
			w
		}
		.
	\end{equation*}

	\item \textit{Write the first column of $M$ as a linear combination of
	the last $3$ columns.}
	\begin{equation*}
		\begin{pmatrix}
			1 & -1 & 1 \\
			1 & -1 & 3 \\
			4 & -4 & -2 \\
			0 & 1 & 2
		\end{pmatrix}
		\begin{pmatrix} x \\ y \\ z \end{pmatrix}
		=
		\begin{pmatrix}
			1 \\ 2 \\ 1 \\ 2
		\end{pmatrix}
	\end{equation*}
	Plugging this into GNU Octave, a solution is
	\begin{equation*}
		\begin{pmatrix} x \\ y \\ z \end{pmatrix}
		=
		\begin{pmatrix} 1.5 \\ 1 \\ 0.5 \end{pmatrix}
		.
	\end{equation*}
	To verify,
	\begin{equation*}
		\boxed{
			1.5 \begin{pmatrix} 1 \\ 1 \\ 4 \\ 0 \end{pmatrix}
			+
			\begin{pmatrix} -1 \\ -1 \\ -4 \\ 1 \end{pmatrix}
			+
			0.5 \begin{pmatrix} 1 \\ 3 \\ -2 \\ 2 \end{pmatrix}
			=
			\begin{pmatrix} 1 \\ 2 \\ 1 \\ 2 \end{pmatrix}
		}
		.
	\end{equation*}
\end{enumerate}

\section*{4.}
\textit{Let $N$ be a $100 \times 100$ matrix with the following pattern: \\
\indent Everything on and above the diagonal is 1. \\
\indent Everything below the diagonal is 0.}
\begin{enumerate}[label=(\alph*)]
	\item \textit{What is the first column of $N^{-1}$? \\
	Hint: Can you solve $N\vec{x} = (1, 0, 0, ..., 0)$?}
	\begin{equation*}
		\boxed{\vec{x} = (1, 0, 0, ..., 0)}
	\end{equation*}

	\item \textit{What is the third column of $N^{-1}$? \\
	Hint: Can you guess what a solution to
	$N\vec{x} = (0, 0, 1, 0, 0, ..., 0)$ is by playing with linear
	combinations of columns of $N$?}
	\begin{equation*}
		\boxed{\vec{x} = (0, -1, 1, 0, 0, ..., 0)}
	\end{equation*}

	\item \textit{What is $N N^{-1}$?} \\[\baselineskip]
	is this a trick question i'm scared
	\begin{equation*}
		\boxed{N N^{-1} = I_{100}}
	\end{equation*}
\end{enumerate}

\section*{5.}
\textit{Let $C$, $D$, and $E$ be $2 \times 2$ matrices that do the
following: \\
\indent C: $\rota{45}$ \\
\indent D: $\rota{-60}$ \\
\indent E: $\refl{30}$}
\begin{enumerate}[label=(\alph*)]
	\item \textit{Find $DE^7C$.}
	\begin{align*}
		DE^7C &= DEC = \rota{-60} (\refl{30} (\rota{45})) \\
		&= \rota{-60} (\refl{7.5}) = \boxed{\refl{-22.5}}
	\end{align*}

	\item \textit{Find $(CE^8D)^{-1}$.}
	\begin{equation*}
		(CE^8D)^{1} = (CD)^{1} =
		(\rota{45} \rota{-60})^{-1} =
		(\rota{-15})^{-1} =
		\boxed{\rota{15}}
	\end{equation*}
\end{enumerate}

\section*{6.}
\textit{Let the $2 \times 2$ matrix $U$ have columns $\vec{v}$ and $\vec{w}$:
$U = (\vec{v} | \vec{w})$. \\ {[Diagram omitted.]}}
\begin{equation*}
	\vec{v} = \begin{pmatrix} 3 \\ 1 \end{pmatrix}
	,
	\vec{w} = \begin{pmatrix} 1 \\ 3 \end{pmatrix}
	,
	\vec{b} = \begin{pmatrix} 8 \\ 8 \end{pmatrix}
\end{equation*}
\begin{enumerate}[label=(\alph*)]
	\item \textit{Solve $U\vec{x} = \vec{b}$.}
	\begin{gather*}
		\begin{pmatrix}
			3 & 1 \\
			1 & 3
		\end{pmatrix}
		\begin{pmatrix} x \\ y \end{pmatrix}
		=
		\begin{pmatrix} 8 \\ 8 \end{pmatrix}
		\\
		\boxed{
			\begin{pmatrix} x \\ y \end{pmatrix}
			=
			\begin{pmatrix} 2 \\ 2 \end{pmatrix}
		}
	\end{gather*}

	\item \textit{Find $U^{-1}$.} \\[\baselineskip]
	Plugging this into GNU Octave,
	\begin{equation*}
		U^{-1} =
		\boxed{
			\frac{1}{8}
			\begin{pmatrix}
				3 & -1 \\
				-1 & 3
			\end{pmatrix}
		}
		.
	\end{equation*}
\end{enumerate}

\end{document}


% \documentclass[CEJM,DVI]{cej} % use DVI command to enable LaTeX driver
\documentclass[ITESM,PDF]{cej} % use PDF command to enable PDFLaTeX driver
\usepackage{layout}

\begin{document}
\input{title_page}
%\maketitle
%\baselinestretch{2}
\section{Introduction }

Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text. Some introductory text.

\section{Sample section}

Sample theorem with citation:

\begin{theorem}[Pythagoras \cite{euclid00}]\label{Pythagoras-famous}
Let $a,b,c$ denote the sides of a triangle. If the angle between $a,b$ equals $90^\circ$ then
$$|a|^2 + |b|^2 = |c|^2.$$
Moreover, regardless of the assumption on the angle, it holds that $|a|+|b|\ge |c|$.
\end{theorem}

See References for the bibliography style in CEJM.
Below is a proposition with a proof.

\begin{proposition}\label{Standard-stuff}
The only possible real solutions of the quadratic equation $ax^2+bx+c=0$ are
$$x_1 = \frac{-b-\sqrt{\Delta}}{2a} \qquad\text{and}\qquad x_2 = \frac{-b+\sqrt{\Delta}}{2a},$$
where $\Delta = b^2-4ac$.
\end{proposition}

\begin{proof}
Observe that
\begin{equation}
(x-x_1)\,(x-x_2) = -{\frac{\Delta}{4\,a^2}}+x^2+{\frac{b\,x}{a}}+{\frac{b^2}{4\,a^2}}
\label{eq-somelabel}
\end{equation}
Taking into account that $\Delta = b^2-4ac$, equation (\ref{eq-somelabel}) leads to
\begin{equation}
(x-x_1)\,(x-x_2) = \frac{c}{a}+x^2 + \frac{b\,x}{a}
\label{eq-anotherlabel}
\end{equation}
Finally, (\ref{eq-anotherlabel}) shows that $a\,(x-x_1)\,(x-x_2)$ equals the original equation.
A polynomial of degree two cannot have more than two roots, therefore $x_1,x_2$ are the only possible solutions of our equation.
\end{proof}

\begin{corollary}
If $b^2<4ac$ then the quadratic equation $ax^2+bx+c=0$ has no real solutions.
\end{corollary}

\subsection{Subsections and sample formulas}

This is a subsection.

Typical aligned list of formulas with labels, displayed by using the \verb align environment.
\begin{align}
\label{eq:first}
\left(b+a\right)\,\left(2\,b-3\,a\right) &= 2\,b^2-a\,b-3\,a^2\\
\label{eq:second}
123+321 &= 444
\end{align}
The same list without labels.
\begin{align*}
\left(b+a\right)\,\left(2\,b-3\,a\right) &= 2\,b^2-a\,b-3\,a^2\\
123+321&=444
\end{align*}
Now the same list with custom tags.
\begin{align*}
\label{eq:smallstars}
\left(b+a\right)\,\left(2\,b-3\,a\right) &= 2\,b^2-a\,b-3\,a^2
\tag*{$(**)$}\\ %<-- \tag* does not add parentheses
\label{eq:doubledagger}
123+321&=444
\tag{$\ddagger$} %<-- \tag adds parentheses
\end{align*}
Another example of aligned formulas
\begin{align*}
  1+1&=2    &    1+2&=3    &    1+3&=4   \\
\intertext{with some text inserted between,}
 10+1&=11   &   10+2&=12   &   10+3&=13  \\
100+1&=101  &  100+2&=102  &  100+3&=103
\end{align*}
by using the \verb \intertext command.

\subsubsection{Polynomials of degree three}

This is a ``subsubsection".

Below is a complicated formula, which must be divided into several lines using, for instance, the
\verb align* \ environment:
\begin{align*}
x_1 &= \left(-{{\sqrt{3}\,i}\over{2}}-{{1}\over{2}}\right)\, \left({{3^ {- {{3}\over{2}} }\,\sqrt{12\,a^4-12\,a^3+188\,a^2-432\,a +247}}\over{2}}-{{2\,a^3+72\,a-81}\over{54}}\right)^{{{1}\over{3}}}\\
&+ {{\left({{\sqrt{3}\,i}\over{2}}-{{1}\over{2}}\right)\,\left(a^2-3 \right)}\over{9\,\left({{3^ {- {{3}\over{2}} }\,\sqrt{12\,a^4-12\,a^3 +188\,a^2-432\,a+247}}\over{2}}-{{2\,a^3+72\,a-81}\over{54}}\right) ^{{{1}\over{3}}}}}-{{a}\over{3}},\\
x_2 &= \left({{\sqrt{3}\,i}\over{2}}- {{1}\over{2}}\right)\,\left({{3^ {- {{3}\over{2}} }\,\sqrt{12\,a^4-12 \,a^3+188\,a^2-432\,a+247}}\over{2}}-{{2\,a^3+72\,a-81}\over{54}} \right)^{{{1}\over{3}}}\\
&+{{\left(-{{\sqrt{3}\,i}\over{2}}-{{1}\over{2 }}\right)\,\left(a^2-3\right)}\over{9\,\left({{3^ {- {{3}\over{2}} } \,\sqrt{12\,a^4-12\,a^3+188\,a^2-432\,a+247}}\over{2}}-{{2\,a^3+72\, a-81}\over{54}}\right)^{{{1}\over{3}}}}}-{{a}\over{3}},\\
x_3 &= \left({{3 ^ {- {{3}\over{2}} }\,\sqrt{12\,a^4-12\,a^3+188\,a^2-432\,a+247} }\over{2}}-{{2\,a^3+72\,a-81}\over{54}}\right)^{{{1}\over{3}}}\\
&+{{a^2 -3}\over{9\,\left({{3^ {- {{3}\over{2}} }\,\sqrt{12\,a^4-12\,a^3+188 \,a^2-432\,a+247}}\over{2}}-{{2\,a^3+72\,a-81}\over{54}}\right)^{{{1 }\over{3}}}}}-{{a}\over{3}}
\end{align*}

\begin{theorem}\label{th_poly3}
The numbers $x_1,x_2,x_3$ defined above are the only complex solutions of the equation
$$x^3+a\,x^2+x+3\,\left(a-1\right)=0.$$
\end{theorem}

\begin{proof}
Easy calculations, using \href{http://maxima.sourceforge.net/}{Maxima} or some other free computer algebra system.
\end{proof}

\paragraph{Other polynomials}

This is a paragraph with title.

One should admit that there is no general formula for the solutions of general polynomial equations and therefore sometimes we must use numerical methods.

\begin{example}\label{examp-123}
Consider the polynomial $v(t)=t^3+t+1$ of degree three. It turns out that its only real root is
$$t=\left({{3^ {- {{3 }\over{2}} }\,\sqrt{31}}\over{2}}-{{1}\over{2}}\right)^{{{1}\over{3 }}}-{{1}\over{3\,\left({{3^ {- {{3}\over{2}} }\,\sqrt{31}}\over{2}}- {{1}\over{2}}\right)^{{{1}\over{3}}}}}$$
There are also other two complex roots\footnote{Since the degree of $v$ is $3$, we know that there may be at most $3$ roots.}, which we ignore.
\end{example}

\begin{remark}\label{rem_CAS}
The proof of Theorem \ref{th_poly3} uses some advanced technology, including a computer algebra system. The same applies to Example \ref{examp-123}. On the other hand, Proposition \ref{Standard-stuff} is well known and its proof is rather elementary.
\end{remark}

\section{Tables and figures}

Table \ref{tab-01} shows how to show some data using the \verb table environment.

\begin{table}
\def~{\phantom0}
\catcode`\@=13
\def@{\phantom.}
\caption{Some caption text.\label{tab-01}}
\medskip
\begin{center}
\begin{tabular}{l|ccc}\hline
\multicolumn1l{\it Some title}\\\hline\hline
row 1, column 1         &   row 1, column 2  \\
row 2, column 1    &   row 2, column 2   \\
row 3, column 1    &   row 3, column 2   \\
\hline
\multicolumn1l{\it Another title} & Value 1 &   Value 2 &   Value 3 \\
\hline\hline
row 1                    & ~130 & 30 & 30 \\
row 2                    & 1025 & ~1 & 15 \\
row 3                   & ~100 & ~1 & 10 \\
row 4        & 2925 & ~1 & ~4 \\
row 5            & 2950 & ~1 & ~2 \\\hline
\end{tabular}
\end{center}
\end{table}

Figure \ref{fig_abc} shows how to use the \verb figure environment for displaying graphics, etc.

\begin{figure}
\caption{The graph of $y=x\,sin x$ in the interval [-50,50], created by wxMaxima 0.7.4. \label{fig_abc}}
%\vskip3cm
\includegraphics{xsinx-49.png}
\end{figure}

\section*{Acknowledgements}

The author(s) would like to thank some institutions for support and so on.

\begin{thebibliography}{9}

\bibitem{annis-etal} Anninis K., Crabi T.J., Sunday T.J., New methods for parallel computing, In: Lyonvenson S. (Ed.), Proceedings of Computer Science Conference (1-�10 Jul. 2007 Haifa Israel), University Press, 2007, 13�-179

\bibitem{p1} Author N., Coauthor M., Title of article, J. Some Math., 2007, 56, 243--256

\bibitem{katish} Katish A., The inconsistency of ZFC, preprint available at
http://arxiv.org/abs/1234.1234

\bibitem{kittel_etal} Kittel S.J., Maria G., Tuke M., Sepran D.J., Smith J., Tadeuszewicz K., et al., New class of measurable functions, J. Real Anal., 1997, 999, 234-�255

\bibitem{nowak1} Nowak P., New axioms for planar geometry, Eastern J. Math., 1999, 1, 324�-334, (in Polish)

\bibitem{nowak} Nowak P., Even better axioms for planar geometry, Eastern J. Math., (in press, in Polish), DOI: 33.1122/321

\bibitem{euclid00} Pythagoras S., On the squares of sides of certain triangles, J. Ancient Math., 2003, 4, 1--30, (in Greek)

\bibitem{sambrook} Sambrook J., Uncountable abelian groups, In: Sambrook J., Russell D.W. (Eds.), Contributions to Abelian groups, 3rd ed., Nauka, Moscow, 2001

\bibitem{sambrook-russell} Sambrook J., Russell D.W., Abelian groups, 3rd ed., Nauka, Moscow, 2001

\end{thebibliography}

\end{document}


% !TEX program = pdflatex
% !TEX options = --shell-escape -synctex=1 -interaction=nonstopmode -file-line-error "%DOC%"
\documentclass[notitlepage]{article}
\usepackage{float}
\usepackage{verbatim}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{bm}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{color,xcolor}
\usepackage{enumerate}
\usepackage{minted}
\usepackage{caption}
\usepackage{subcaption}

\title{Poisson-FBMP Note}
\date{}

\input{format.tex}

\begin{document}
\newcommand{\ud}{\mathrm{d}}

\maketitle
\tableofcontents

\section{P-FBMP with magic time bin}

\begin{lstlisting}
mu_t = abs(wave.sum() / gmu)
if Tau > 10:
    n = min(math.ceil(20 / mu_t), 5)
else:
    n = min(math.ceil(4 / mu_t), 3)
A, wave_r, tlist, t0_t, t0_delta, cha, left_wave, right_wave = wff.initial_params(wave[::wff.nshannon], spe_pre[ent[i]['ChannelID']], Mu, Tau, Sigma, gmu, Thres['lucyddm'], p, nsp, nstd, is_t0=True, is_delta=False, n=n, nshannon=1)
mu_t = abs(wave_r.sum() / gmu)
def optit0mu(t0, mu, n, xmmse_star, psy_star, c_star, la):
    ys = np.log(psy_star) - np.log(poisson.pmf(c_star, la)).sum(axis=1)
    ys = np.exp(ys - ys.max()) / np.sum(np.exp(ys - ys.max()))
    t0list = np.arange(t0 - 3 * Sigma, t0 + 3 * Sigma + 1e-6, 0.2)
    mulist = np.arange(max(1e-8, mu - 3 * np.sqrt(mu)), mu + 3 * np.sqrt(mu), 0.1)
    b_mu = [max(1e-8, mu - 5 * np.sqrt(mu)), mu + 5 * np.sqrt(mu)]
    tlist_pan = np.sort(np.unique(np.hstack(np.arange(0, window)[:, None] + np.arange(0, 1, 1 / n))))
    As = np.zeros((len(xmmse_star), len(tlist_pan)))
    As[:, np.isin(tlist_pan, tlist)] = c_star
    assert sum(np.sum(As, axis=0) > 0) > 0

    def likelihood(x):
        a = x[0] * wff.convolve_exp_norm(tlist_pan - x[1], Tau, Sigma) / n + 1e-8 # use tlist_pan not tlist
        li = -special.logsumexp(np.log(poisson.pmf(As, a)).sum(axis=1), b=ys)
        return li

    likemu = np.array([likelihood([mulist[j], t0]) for j in range(len(mulist))])
    liket0 = np.array([likelihood([mu, t0list[j]]) for j in range(len(t0list))])
    mu, t0 = opti.fmin_l_bfgs_b(likelihood, x0=[mulist[likemu.argmin()], t0list[liket0.argmin()]], approx_grad=True, bounds=[b_mu, b_t0], maxfun=50000)[0]
    return mu, t0

truth = pelist[pelist['TriggerNo'] == ent[i]['TriggerNo']]
time_fbmp_start = time.time()
factor = np.sqrt(np.diag(np.matmul(A.T, A)).mean())
A = np.matmul(A, np.diag(1. / np.sqrt(np.diag(np.matmul(A.T, A)))))
la = mu_t * wff.convolve_exp_norm(tlist - t0_t, Tau, Sigma) / n + 1e-8
xmmse, xmmse_star, psy_star, nu_star, nu_star_bk, T_star, d_tot_i, d_max_i, num_i = wff.fbmpr_fxn_reduced(wave_r, A, la, spe_pre[cid]['std'] ** 2, (gsigma * factor / gmu) ** 2, factor, len(la), stop=5, truth=truth, i=i, left=left_wave, right=right_wave, tlist=tlist, gmu=gmu, para=p)
time_fbmp = time_fbmp + time.time() - time_fbmp_start
c_star = np.zeros_like(xmmse_star).astype(int)
for k in range(len(T_star)):
    t, c = np.unique(T_star[k][xmmse_star[k][T_star[k]] > 0], return_counts=True)
    c_star[k, t] = c
maxindex = 0

xmmse_most = xmmse_star[maxindex]
pet = np.repeat(tlist[xmmse_most > 0], c_star[maxindex][xmmse_most > 0])
cha = np.repeat(xmmse_most[xmmse_most > 0] / factor / c_star[maxindex][xmmse_most > 0], c_star[maxindex][xmmse_most > 0])

mu, t0 = optit0mu(t0_t, mu_t, n, xmmse_star, psy_star, c_star, la)
mu_i, t0_i = optit0mu(t0_t, mu_t, n, xmmse_most[None, :], np.array([1]), c_star[maxindex][None, :], la)
\end{lstlisting}

Motivation: demonstrate magic time binning on $\mu$ bias

Progress: magic time bin is
\begin{lstlisting}
mu_t = abs(wave.sum() / gmu)
if Tau > 10:
    n = min(math.ceil(20 / mu_t), 5)
else:
    n = min(math.ceil(4 / mu_t), 3)
\end{lstlisting}

Result: relative $\mu$ bias are less than $2.5\%$

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{vs-biasmu-magic.png}
\end{figure}

\section{P-FBMP only fitting}

\begin{lstlisting}
def optit0mu(t0, mu, n, xmmse_star, psy_star, c_star, la):
    ys = np.log(psy_star) - np.log(poisson.pmf(c_star, la)).sum(axis=1)
    ys = np.exp(ys - ys.max()) / np.sum(np.exp(ys - ys.max()))
    t0list = np.arange(t0 - 3 * Sigma, t0 + 3 * Sigma + 1e-6, 0.2)
    mulist = np.arange(max(1e-8, mu - 3 * np.sqrt(mu)), mu + 3 * np.sqrt(mu), 0.1)
    b_mu = [max(1e-8, mu - 5 * np.sqrt(mu)), mu + 5 * np.sqrt(mu)]
    tlist_pan = np.sort(np.unique(np.concatenate([np.unique(np.hstack(np.arange(1, window-1)[:, None] + np.arange(0, 1, 1 / n))), tlist])))
    As = np.zeros((len(xmmse_star), len(tlist_pan)))
    As[:, np.isin(tlist_pan, tlist)] = c_star
    tlist_edge = np.concatenate([[tlist_pan[0] - np.diff(tlist_pan)[0] / 2], (tlist_pan[1:] + tlist_pan[:-1]) / 2, [tlist_pan[-1] + np.diff(tlist_pan)[-1] / 2]])
    diff = np.diff(tlist_edge)
    assert As.sum() == c_star.sum()
    assert sum(np.sum(As, axis=0) > 0) > 0

    def likelihood(x):
        a = x[0] * wff.convolve_exp_norm(tlist_pan - x[1], Tau, Sigma) * diff + 1e-8 # use tlist_pan not tlist
        li = -special.logsumexp(np.log(poisson.pmf(As, a)).sum(axis=1), b=ys)
        return li

    likemu = np.array([likelihood([mulist[j], t0]) for j in range(len(mulist))])
    liket0 = np.array([likelihood([mu, t0list[j]]) for j in range(len(t0list))])
    mu, t0 = opti.fmin_l_bfgs_b(likelihood, x0=[mulist[likemu.argmin()], t0list[liket0.argmin()]], approx_grad=True, bounds=[b_mu, b_t0], maxfun=50000)[0]
    return mu, t0

truth = pelist[pelist['TriggerNo'] == ent[i]['TriggerNo']]

tlist = truth['HitPosInWindow'][truth['HitPosInWindow'] < right_wave - 1]
if len(tlist) == 1:
    tlist_edge = np.array([tlist[0] - 0.5, tlist[0] + 0.5])
else:
    tlist_edge = np.concatenate([[tlist[0] - np.diff(tlist)[0] / 2], (tlist[1:] + tlist[:-1]) / 2, [tlist[-1] + np.diff(tlist)[-1] / 2]])
t_auto = (np.arange(left_wave, right_wave) / wff.nshannon)[:, None] - tlist
A = p[2] * np.exp(-1 / 2 * (np.log((t_auto + np.abs(t_auto)) / p[0] / 2) / p[1]) ** 2)

t0_t = t0_truth[i]['T0']
mu_t = len(truth)
la = mu_t * np.array([integrate.quad(lambda t : wff.convolve_exp_norm(t - t0_t, Tau, Sigma), tlist_edge[i], tlist_edge[i+1])[0] for i in range(len(tlist))]) + 1e-8

c_star_truth = np.ones_like(tlist)
wav_ans = np.sum([np.where(pp > truth['HitPosInWindow'][j], wff.spe(pp - truth['HitPosInWindow'][j], tau=p[0], sigma=p[1], A=p[2]) * truth['Charge'][j] / gmu, 0) for j in range(len(truth))], axis=0)

mu, t0 = optit0mu(t0_t, mu_t, n, np.empty(len(la))[None, :], np.array([1]), c_star_truth[None, :], la)
\end{lstlisting}

Motivation: to validate fitting process

Progress: Use true \texttt{HitPosInWindow} as \texttt{tlist}, true \texttt{T0} as \texttt{t0\_t}, length of \texttt{HitPosInWindow} as \texttt{mu\_t}. Without FBMP RGS, directly \textbf{fit} $\mu$ and $t_0$.

Result: fitting process is not related to bias of $\mu$.

\begin{figure}[H]
    \includegraphics[width=\textwidth]{vs-biasmu-trufit.png}
\end{figure}

\section{P-FBMP with truth prior}

\begin{lstlisting}
tlist = truth['HitPosInWindow'][truth['HitPosInWindow'] < right_wave - 1]
if len(tlist) == 1:
    tlist_edge = np.array([tlist[0] - 0.5, tlist[0] + 0.5])
else:
    tlist_edge = np.concatenate([[tlist[0] - np.diff(tlist)[0] / 2], (tlist[1:] + tlist[:-1]) / 2, [tlist[-1] + np.diff(tlist)[-1] / 2]])
t_auto = (np.arange(left_wave, right_wave) / wff.nshannon)[:, None] - tlist
A = p[2] * np.exp(-1 / 2 * (np.log((t_auto + np.abs(t_auto)) / p[0] / 2) / p[1]) ** 2)
t0_t = t0_truth[i]['T0']
mu_t = len(truth)
factor = np.sqrt(np.diag(np.matmul(A.T, A)))
A = np.matmul(A, np.diag(1. / np.sqrt(np.diag(np.matmul(A.T, A)))))
la = mu_t * np.array([integrate.quad(lambda t : wff.convolve_exp_norm(t - t0_t, Tau, Sigma), tlist_edge[i], tlist_edge[i+1])[0] for i in range(len(tlist))]) + 1e-8
xmmse, xmmse_star, psy_star, nu_star, nu_star_bk, T_star, d_tot_i, d_max_i, num_i = wff.fbmpr_fxn_reduced(wave_r, A, la, spe_pre[cid]['std'] ** 2, (gsigma * factor / gmu) ** 2, factor, len(la), stop=5, truth=truth, i=i, left=left_wave, right=right_wave, tlist=tlist, gmu=gmu, para=p)
c_star = np.zeros_like(xmmse_star).astype(int)
for k in range(len(T_star)):
    t, c = np.unique(T_star[k][xmmse_star[k][T_star[k]] > 0], return_counts=True)
    c_star[k, t] = c
mu = np.average(c_star.sum(axis=1), weights=psy_star)
t0 = t0_t
\end{lstlisting}

Motivation: to prove prior used in FBMP is not the origin of $\mu$ bias.

Progress: Use true \texttt{HitPosInWindow} as \texttt{tlist}, true \texttt{T0} as \texttt{t0\_t}, length of \texttt{HitPosInWindow} as \texttt{mu\_t}. \texttt{la} is vector of integral in each time bins.

Result: prior used in FBMP is not the origin of $\mu$ bias.

\begin{figure}[H]
    \includegraphics[width=\textwidth]{vs-biasmu-truprior.png}
\end{figure}

\section{P-FBMP using different bin width}

\begin{lstlisting}
mu_t = abs(wave.sum() / gmu)
A, wave_r, tlist, t0_t, t0_delta, cha, left_wave, right_wave = wff.initial_params(wave[::wff.nshannon], spe_pre[ent[i]['ChannelID']], Tau, Sigma, gmu, Thres['lucyddm'], p, nsp, nstd, is_t0=True, is_delta=False, n=n, nshannon=1)
mu_t = abs(wave_r.sum() / gmu)
def optit0mu(t0, mu, n, xmmse_star, psy_star, c_star, la):
    ys = np.log(psy_star) - np.log(poisson.pmf(c_star, la)).sum(axis=1)
    ys = np.exp(ys - ys.max()) / np.sum(np.exp(ys - ys.max()))
    t0list = np.arange(t0 - 3 * Sigma, t0 + 3 * Sigma + 1e-6, 0.2)
    mulist = np.arange(max(1e-8, mu - 3 * np.sqrt(mu)), mu + 3 * np.sqrt(mu), 0.1)
    b_mu = [max(1e-8, mu - 5 * np.sqrt(mu)), mu + 5 * np.sqrt(mu)]
    tlist_pan = np.sort(np.unique(np.hstack(np.arange(0, window)[:, None] + np.arange(0, 1, 1 / n))))
    As = np.zeros((len(xmmse_star), len(tlist_pan)))
    As[:, np.isin(tlist_pan, tlist)] = c_star
    assert sum(np.sum(As, axis=0) > 0) > 0

    def likelihood(x):
        a = x[0] * wff.convolve_exp_norm(tlist_pan - x[1], Tau, Sigma) / n + 1e-8 # use tlist_pan not tlist
        li = -special.logsumexp(np.log(poisson.pmf(As, a)).sum(axis=1), b=ys)
        return li

    likemu = np.array([likelihood([mulist[j], t0]) for j in range(len(mulist))])
    liket0 = np.array([likelihood([mu, t0list[j]]) for j in range(len(t0list))])
    mu, t0 = opti.fmin_l_bfgs_b(likelihood, x0=[mulist[likemu.argmin()], t0list[liket0.argmin()]], approx_grad=True, bounds=[b_mu, b_t0], maxfun=50000)[0]
    return mu, t0

truth = pelist[pelist['TriggerNo'] == ent[i]['TriggerNo']]

factor = np.sqrt(np.diag(np.matmul(A.T, A)))
A = np.matmul(A, np.diag(1. / np.sqrt(np.diag(np.matmul(A.T, A)))))
la = mu_t * wff.convolve_exp_norm(tlist - t0_t, Tau, Sigma) / n + 1e-8
xmmse, xmmse_star, psy_star, nu_star, nu_star_bk, T_star, d_tot_i, d_max_i, num_i = wff.fbmpr_fxn_reduced(wave_r, A, la, spe_pre[cid]['std'] ** 2, (gsigma * factor / gmu) ** 2, factor, len(la), stop=5, truth=truth, i=i, left=left_wave, right=right_wave, tlist=tlist, gmu=gmu, para=p)
time_fbmp = time_fbmp + time.time() - time_fbmp_start
c_star = np.zeros_like(xmmse_star).astype(int)
for k in range(len(T_star)):
    t, c = np.unique(T_star[k][xmmse_star[k][T_star[k]] > 0], return_counts=True)
    c_star[k, t] = c
maxindex = 0

xmmse_most = xmmse_star[maxindex]
pet = np.repeat(tlist[xmmse_most > 0], c_star[maxindex][xmmse_most > 0])
cha = np.repeat(xmmse_most[xmmse_most > 0] / factor[xmmse_most > 0] / c_star[maxindex][xmmse_most > 0], c_star[maxindex][xmmse_most > 0])

mu, t0 = optit0mu(t0_t, mu_t, n, xmmse_star, psy_star, c_star, la)
mu_i, t0_i = optit0mu(t0_t, mu_t, n, xmmse_most[None, :], np.array([1]), c_star[maxindex][None, :], la)
\end{lstlisting}

Motivation: to demonstrate the bin width's influence on $\mu$ bias.

Progress: $1/n$ is the bin width, $n=\{1,2,5\}$

\begin{figure}[H]
    \includegraphics[width=\textwidth]{vs-biasmu-bin.png}
\end{figure}

\section{$N_{PE}$ estimation in one bin}

\begin{lstlisting}
def getm(i, p):
    N = 10000
    bias = np.empty(N)
    np.random.seed(i)
    Mu = 10
    gmu = 160
    gsigma = 40
    npan = np.arange(1, 50)
    for i in range(N):
        n = poisson.ppf(1 - uniform.rvs(scale=1-poisson.cdf(0, Mu), size=1), Mu).astype(int)
        cha = norm.ppf(1 - uniform.rvs(scale=1-norm.cdf(0, loc=gmu, scale=gsigma), size=n), loc=gmu, scale=gsigma)
        cha = cha.sum()
        if p:
            weight = poisson.pmf(npan, Mu) * norm.pdf(cha, loc=gmu * npan, scale=gsigma * np.sqrt(npan))
        else:
            weight = norm.pdf(cha, loc=gmu * npan, scale=gsigma * np.sqrt(npan))
        weight = weight / weight.sum()
        bias[i] = np.sum(npan * weight) - n
    m = bias.mean()
    return m
slices = np.arange(1000).T.tolist()
with Pool(100) as pool:
    resultpoisson = np.hstack([pool.starmap(getm, zip(slices, [True] * len(slices)))])
with Pool(100) as pool:
    resultflat = np.hstack([pool.starmap(getm, zip(slices, [False] * len(slices)))])
\end{lstlisting}

Motivation: to estimate bias of $\mu$ based on toy simulation in one bin

Progress: set $\mu$. Sampling: $n\sim\mathrm{Poisson}(n|\mu)$, $q_i\sim\mathrm{Gaussian}(q_i|1,\sigma_q^2),i=\{1,\cdots,n\}$, $Q=\sum_i q_i$. Estimation: $p(n|Q) = \frac{\mathrm{Poisson}(n|\mu) \cdot \mathrm{Gaussian}(Q|n,n\sigma_q^2)}{\sum_{j=1}^n \mathrm{Poisson}(j|\mu) \cdot \mathrm{Gaussian}(Q|j,j\sigma_q^2)}$ or $p(n|Q) = \frac{\mathrm{Gaussian}(Q|n,n\sigma_q^2)}{\sum_{j=1}^n \mathrm{Gaussian}(Q|j,j\sigma_q^2)}$

Result: when using Poisson prior, there is \textbf{no} bias, when using flat prior, there is bias.

\begin{figure}[H]
    \centering
    \includegraphics[width=0.5\textwidth]{onebinbias.png}
\end{figure}

\section{$N_{PE}$ estimation based on toy simulation}

\begin{lstlisting}
n = 10000
npe = 30
noi = True
if noi:
    std = 1.
else:
    std = 1e-4
window = 200
np.random.seed(0)
wdtp = np.dtype([('No', np.uint32), ('Waveform', np.float, window)])
waves = np.empty(n).astype(wdtp)
tlist = np.repeat(np.array([0, 1, 1]), npe / 3)
sams = [np.vstack((tlist, wff.charge(npe, gmu=gmu, gsigma=gsigma, thres=0))).T for i in range(n)]
pan = np.arange(0, window)
t0 = 0.5
for i in tqdm(range(n)):
    wave = np.sum([np.where(pan > sams[i][j, 0], wff.spe(pan - sams[i][j, 0], tau=p[0], sigma=p[1], A=p[2]) * sams[i][j, 1] / gmu, 0) for j in range(len(sams[i]))], axis=0)
    if noi:
        wave = wave + np.random.normal(0, std, size=window)
    waves[i]['Waveform'] = wave
waves['No'] = np.arange(n).astype(np.uint32)
sdtp = np.dtype([('No', np.uint32), ('HitPosInWindow', np.float64), ('Charge', np.float64)])
pelist = np.empty(sum([len(sams[i]) for i in range(n)])).astype(sdtp)
pelist['No'] = np.repeat(np.arange(n), [len(sams[i]) for i in range(n)]).astype(np.uint32)
pelist['HitPosInWindow'] = np.hstack([sams[i][:, 0] for i in range(n)])
pelist['Charge'] = np.hstack([sams[i][:, 1] for i in range(n)])
mu = np.empty(n)
for i in tqdm(range(n)):
    wave = waves[i]['Waveform'].astype(np.float64) * spe_pre[0]['epulse']
    truth = pelist[pelist['No'] == waves[i]['No']]
    tlist = np.unique(truth['HitPosInWindow'])

    t_auto = np.arange(0, window)[:, None] - tlist
    A = p[2] * np.exp(-1 / 2 * (np.log((t_auto + np.abs(t_auto)) / p[0] / 2) / p[1]) ** 2)
    mu_t = npe
    factor = np.sqrt(np.diag(np.matmul(A.T, A)))
    A = np.matmul(A, np.diag(1. / np.sqrt(np.diag(np.matmul(A.T, A)))))
    la = mu_t * np.array([1, 2]) / 3

    xmmse, xmmse_star, psy_star, nu_star, nu_star_bk, T_star, d_tot_i, d_max_i, num_i = wff.fbmpr_fxn_reduced(wave, A, la, std ** 2, (gsigma * factor / gmu) ** 2, factor, len(la), stop=5)

    c_star = np.zeros_like(xmmse_star).astype(int)
    for k in range(len(T_star)):
        t, c = np.unique(T_star[k][xmmse_star[k][T_star[k]] > 0], return_counts=True)
        c_star[k, t] = c

    mu[i] = np.average(c_star.sum(axis=1), weights=psy_star)
\end{lstlisting}

Motivation: try to understand the relation between convolution process and bias

Progress: there is no $\mu$, set $N_{PE}$ as 30, set \textit{HitPosInWindow} to $[0, 1]$, set time profile prior as $[1/3, 2/3]$, sample \textit{Charge}. Use FBMP to generate posterior distribution of $N_{PE}$.

Result: there is \textbf{no} bias on this configuration

\begin{figure}[H]
    \centering
    \includegraphics[width=0.5\textwidth]{toybias.png}
\end{figure}

\section{Influence of lucyddm \texttt{tlist} initialization on $\mu$ bias}

\begin{lstlisting}
n = 2
A, wave_r, tlist, t0_t, t0_delta, cha, left_wave, right_wave = wff.initial_params(wave[::wff.nshannon], spe_pre[ent[i]['ChannelID']], Tau, Sigma, gmu, Thres['lucyddm'], p, nsp, nstd, is_t0=True, is_delta=False, n=n, nshannon=1)

truth = pelist[pelist['TriggerNo'] == ent[i]['TriggerNo']]

c_star_truth = np.sum([np.where(tlist - 0.5 / n < truth['HitPosInWindow'][j], 1, 0) * np.where(tlist + 0.5 / n > truth['HitPosInWindow'][j], 1, 0) for j in range(len(truth))], axis=0)

mu = c_star_truth.sum()
t0 = t0_t
\end{lstlisting}

Motivation: to test the influence of lucyddm time bin initialization on $\mu$ bias

Progress: lucyddm initialization, estimate $\mu$ as number of truth \textit{HitPosInWindow} in \texttt{tlist}

Result: there is \textbf{no} bias from lucyddm \texttt{tlist} initialization

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{vs-biasmu-lucyddmtlist.png}
\end{figure}

\section{Influence of lucyddm \texttt{wave\_r} initialization on $\mu$ bias}

\begin{lstlisting}
mu_t = abs(wave.sum() / gmu)
n = 2
A, wave_r, tlist, t0_t, t0_delta, cha, left_wave, right_wave = wff.initial_params(wave[::wff.nshannon], spe_pre[ent[i]['ChannelID']], Tau, Sigma, gmu, Thres['lucyddm'], p, nsp, nstd, is_t0=True, is_delta=False, n=n, nshannon=1)
mu_t = abs(wave_r.sum() / gmu)
mu = mu_t
t0 = t0_t
\end{lstlisting}

Motivation: to test the influence of waveform cut in lucyddm initialization on $\mu$ bias

Progress: lucyddm initialization, estimate $\mu$ as number of integral of truncated waveform \texttt{wave\_r}

Result: there is \textbf{no} bias from lucyddm waveform cut initialization

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{vs-biasmu-lucyddmwave.png}
\end{figure}

\section{Iterative P-FBMP}

\begin{lstlisting}
mu_t = abs(wave.sum() / gmu)
if Tau > 10:
    n = min(math.ceil(20 / mu_t), 5)
else:
    n = min(math.ceil(4 / mu_t), 3)
A, wave_r, tlist, t0_t, t0_delta, cha, left_wave, right_wave = wff.initial_params(wave[::wff.nshannon], spe_pre[ent[i]['ChannelID']], Tau, Sigma, gmu, Thres['lucyddm'], p, nsp, nstd, is_t0=True, is_delta=False, n=n, nshannon=1)
mu_t = abs(wave_r.sum() / gmu)
def optit0mu(t0, mu, n, xmmse_star, psy_star, c_star, la):
    ys = np.log(psy_star) - np.log(poisson.pmf(c_star, la)).sum(axis=1)
    ys = np.exp(ys - ys.max()) / np.sum(np.exp(ys - ys.max()))
    t0list = np.arange(t0 - 3 * Sigma, t0 + 3 * Sigma + 1e-6, 0.2)
    mulist = np.arange(max(1e-8, mu - 3 * np.sqrt(mu)), mu + 3 * np.sqrt(mu), 0.1)
    b_mu = [max(1e-8, mu - 5 * np.sqrt(mu)), mu + 5 * np.sqrt(mu)]
    tlist_pan = np.sort(np.unique(np.hstack(np.arange(0, window)[:, None] + np.arange(0, 1, 1 / n))))
    As = np.zeros((len(xmmse_star), len(tlist_pan)))
    As[:, np.isin(tlist_pan, tlist)] = c_star
    assert sum(np.sum(As, axis=0) > 0) > 0

    def likelihood(x):
        a = x[0] * wff.convolve_exp_norm(tlist_pan - x[1], Tau, Sigma) / n + 1e-8 # use tlist_pan not tlist
        li = -special.logsumexp(np.log(poisson.pmf(As, a)).sum(axis=1), b=ys)
        return li

    likemu = np.array([likelihood([mulist[j], t0]) for j in range(len(mulist))])
    liket0 = np.array([likelihood([mu, t0list[j]]) for j in range(len(t0list))])
    mu, t0 = opti.fmin_l_bfgs_b(likelihood, x0=[mulist[likemu.argmin()], t0list[liket0.argmin()]], approx_grad=True, bounds=[b_mu, b_t0], maxfun=50000)[0]
    return mu, t0

truth = pelist[pelist['TriggerNo'] == ent[i]['TriggerNo']]
time_fbmp_start = time.time()
factor = np.sqrt(np.diag(np.matmul(A.T, A)).mean())
A = np.matmul(A, np.diag(1. / np.sqrt(np.diag(np.matmul(A.T, A)))))

t0_bk = np.inf
mu_bk = np.inf
iterc_i = 0
while abs(mu_bk - mu_t) > 1e-3 or abs(t0_bk - t0_t) > 1e-3:
    iterc_i += 1
    t0_bk = t0_t
    mu_bk = mu_t
    la = mu_t * wff.convolve_exp_norm(tlist - t0_t, Tau, Sigma) / n + 1e-8
    xmmse, xmmse_star, psy_star, nu_star, nu_star_bk, T_star, d_tot_i, d_max_i, num_i = wff.fbmpr_fxn_reduced(wave_r, A, la, spe_pre[cid]['std'] ** 2, (gsigma * factor / gmu) ** 2, factor, len(la), stop=5, truth=truth, i=i, left=left_wave, right=right_wave, tlist=tlist, gmu=gmu, para=p)
    time_fbmp = time_fbmp + time.time() - time_fbmp_start
    c_star = np.zeros_like(xmmse_star).astype(int)
    for k in range(len(T_star)):
        t, c = np.unique(T_star[k][xmmse_star[k][T_star[k]] > 0], return_counts=True)
        c_star[k, t] = c

    mu_t, t0_t = optit0mu(t0_t, mu_t, n, xmmse_star, psy_star, c_star, la)
    if iterc_i >= 10:
        break

mu = mu_t
t0 = t0_t
mu_i, t0_i = optit0mu(t0_t, mu_t, n, xmmse_most[None, :], np.array([1]), c_star[maxindex][None, :], la)
\end{lstlisting}

Motivation: demonstrate whether iteration can reduce $\mu$ bias

Progress: set $\mu=10$ to save time, iteration after FBMP sampling and fitting until $\mu$ and $t_0$ are stable

Result: iteration can \textbf{not} reduce $\mu$ bias

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{vs-biasmu-iter.png}
\end{figure}

\end{document}


\chapter{Phylogenetic Measures}
\pagenumbering{arabic} \setcounter{page}{16}

Bacterial lineages determine how the microbial communities are formed as the phylogenetic relatedness points towards functional relatedness. Traits of bacteria influencing ecological functions are phylogenetically conserved. High ecosystem functioning is directly related to the co-existence of functionally distinct lineages or from the existence of productive lineages that outperform the rest. Functional differences allow coexistence through niche segregation events and deliver different functions to the ecosystem. A study performed by \emph{Goberna et al} detected that the abundance of divergent lineages in the community increases the ecological processes \cite{goberna_2018_phylogeneticscale}. Therefore to elucidate the ecological functioning of the community, incorporation of phylogenetic measures is very crucial. This section discusses the four most widely used approaches that are used in designing the phylogenetic trees. The approaches are classified distance based-approaches and character-based approaches.

\section{Distance-based approaches}

As the name suggests, the distance-based algorithms utilise the matrices containing pairwise distances. Pairwise distances aid in the construction of trees via Bayesian and likelihood methods. The two most widely used methods which utilise the pairwise distance are the Unweighted Pair Group Method with Arithmetic Mean (UPGMA) and Neighbour Joining (NJ) method \cite{munjal_2019_phylogenetics}. UPGMA is an agglomerative hierarchical clustering method that builds a rooted phylogenetic tree by assuming equal rates of evolution. The NJ is an iterative clustering method that produces an unrooted tree by considering different rates of evolution. Given that both the methods use a distance matrix, they lower computing time for large datasets compared to character-based methods. NJ is preferred over UPGMA for almost all cases as the previous assumes the same evolution rate for all of its lineages \cite{munjal_2019_phylogenetics}.

\section{Character-based approaches}

The character-based approaches use actual sequence alignments similarities to calculate the distances. The character-based methods are more greedy for computational resources and time as compared to the distance-based techniques \cite{munjal_2019_phylogenetics}. However, they generate exact phylogenetic trees. The character-based methods include Maximum-Likelihood (ML) and Maximum-Parsimony (MP). The MP is based on the assumption that the simplicity extends that the most parsimonious tree would be the one that reflects the slightest evolutionary changes \cite{munjal_2019_phylogenetics}. The ML, on the other hand, utilises the probabilistic modelling based on Markov chains to derive the trees.

\section*{Bootstrapping}

Once the trees are constructed, the inference of their reliability poses a challenge. This is known as bootstrapping. Bootstrapping in phylogenetic analysis is a greedy approach that picks deviated/pseudo samples from the original dataset to design the tree \cite{lemoine_2018_renewing}. Essentially, running any tree constructing algorithms on a sample data more than 100 times generates a bootstrapping value which is a measure of reliability as it gives the probability of a branch.