\documentclass[11pt]{scrartcl} % Font size
\input{structure.tex} % Include the file specifying the document structure and custom commands

%----------------------------------------------------------------------------------------
%	TITLE SECTION
%----------------------------------------------------------------------------------------

\title{
	\normalfont\normalsize
	\textsc{Harvard Privacy Tools Project}\\ % Your university, school and/or department name(s)
	\vspace{25pt} % Whitespace
	\rule{\linewidth}{0.5pt}\\ % Thin top horizontal rule
	\vspace{20pt} % Whitespace
	{\huge The Exponential Mechanism for Medians}\\ % The assignment title
	\vspace{12pt} % Whitespace
	\rule{\linewidth}{2pt}\\ % Thick bottom horizontal rule
	\vspace{12pt} % Whitespace
}

% \author{\LARGE} % Your name

\date{\normalsize\today} % Today's date (\today) or a custom date

\begin{document}
\maketitle

\section{The Exponential Mechanism}

Sometimes, the global sensitivity of a function is too great, so the Laplace mechanism will not produce meaningful results. The median is one such function. In many cases, the \textit{Exponential mechanism} is an alternate approach that gives reasonable utility.\footnote{This is not the \textit{only} advantage of the exponential mechanism. It is a way to compute differentially private queries on non-numeric data, unlike the Laplace mechanism it does not assume that the probability of outputting a response ought to be symmetric about the true response, etc.} Introduced in 2007 by McSherry and Talwar, the exponential mechanism posits that for a given database, users prefer some outputs over others. That those preferences may be encapsulated with a utility score, where a high utility score indicates a higher preference for that output. The exponential mechanism releases outputs with probability proportional (in the exponent) to the utility score and the sensitivity of the utility function.

\begin{definition}
Let $\mathcal{X}$ be a space of databases and let $[m,M]$ be an arbitrary range. Let $u: \mathcal{X} \times [m,M] \rightarrow \mathbb{R}$ be a utility function, which maps pairs of databases and outputs to a utility score. Let $\Delta u$ be the sensitivity of $u$ with respect to the database argument. The exponential mechanism outputs $r \in [m,M]$ with probability proportional to $\exp\left(\frac{\varepsilon u(x,r)}{2 \Delta u}\right)$ \cite{mcsherry2007mechanism, dwork2014algorithmic}.\footnote{The original definition is from \cite{mcsherry2007mechanism}, but here we state the version rewritten in \cite{dwork2014algorithmic} as it is slightly clearer.}
\end{definition}

\begin{theorem}
The exponential mechanism preserves $(\varepsilon,0)$-differential privacy \cite{mcsherry2007mechanism, dwork2014algorithmic}.\footnote{As written in \cite{mcsherry2007mechanism}, the mechanism actually preserves $(2\varepsilon\Delta u,0)$-differential privacy; the main difference in the $\cite{dwork2014algorithmic}$ version is that it has the extra factor of $2\Delta u$ to avoid these extra terms.}
\end{theorem}

Note that the exponential mechanism may not be tractable in many cases, as it assumes the existence of a utility function, and even if one exists it may not be tractable to compute it efficiently.

\section{An Exponential Mechanism for a quantile}

\subsection{Defining a sensible utility function}

Note that a user will prefer an output that is closer to the true quantile over one that is further away. Let $x$ be an (ordered) data set, let $r$ be a possible output, and let $N$ be the size of the data set. Let $\#(Z>r)$ refer to the number of points in $x$ above $r$. Then, the following is a reasonable utility function for a release $r$ for the $\alpha$-quantile of $x$.

\begin{equation}
u(x,r) = \max(\alpha, (1-\alpha))N - \vert (1-\alpha)\#(Z<r) - \alpha\#(Z>r)\vert.
\end{equation}

\subsection{Sensitivity of the utility function}

\subsubsection{Neighboring Definition: Change One}
\begin{lemma}
The above utility function $u$ has $\ell_11$ sensitivity bounded above by 1 in the change one model.
\end{lemma}

\begin{proof}
Let $c_1 = \#(Z<r)$ and $c_2 = \#(Z>r)$. In one worst case, $c_1$ increases by 1 and $c_2$ decreases by 1.
Then,
\begin{align*}
\Delta u &= \vert (1-\alpha) (c_1 + 1) - \alpha (c_2-1) \vert - \vert (1-\alpha) c_1 - \alpha c_2 \vert  \\
 &\le \vert (1-\alpha) (c_1 + 1) - \alpha (c_2-1) - (1-\alpha) c_1 + \alpha c_2 \vert\\
 & \le \vert c_1 + 1 - \alpha c_1 - \alpha - \alpha c_2 + \alpha - c_1 + \alpha c_1 + \alpha c_2 \vert\\
&= 1
\end{align*}
If instead $c_2$ decreases by 1 and $c_1$ increases by 1, the same thing will happen except with a negative sign that will not impact the final result due to the absolute values.
\end{proof}
\subsubsection{Neighboring Definition: Add/Drop One}

\begin{lemma}
The above utility function $u$ has $\ell_11$ sensitivity bounded above by $\max(1-\alpha, \alpha)$ in the add/drop one model.
\end{lemma}

\begin{proof}
Let $c_1 = \#(Z<r)$ and $c_2 = \#(Z>r)$.  Consider what happens if one point is added. There are two cases that would impact the utility function:
\begin{enumerate}
\item $c_1$ increases by one and nothing happens to $c_2$.
\item $c_2$ increases by one and nothing happens to $c_1$.
\end{enumerate}

Say the first case occurs. Then,

\begin{align*}
\Delta u &= | (1-\alpha) (c_1 + 1) - \alpha (c_2) | - | (1-\alpha) c_1 - \alpha c_2 |  \\
 	&\le | (1-\alpha) (c_1 + 1) - \alpha (c_2) - (1-\alpha) c_1 + \alpha c_2 | \\
	&= 1 - \alpha
\end{align*}

In the second case,

\begin{align*}
\delta u &= \vert (1-\alpha) (c_1) - \alpha (c_2 + 1) \vert - \vert (1-\alpha) c_1 - \alpha c_2 \vert \\
	&\le \vert c_1 -\alpha c_1 - \alpha c_2 - \alpha - c_1 + \alpha c_1 + \alpha c_2  \vert\\
	&= \alpha
\end{align*}
\end{proof}

Subtracting a point leads to the same results.

\bibliographystyle{alpha}
\nocite{*}
\bibliography{expMechMedian}
\end{document}


The following text is reprinted from \href{https://doi.org/10.21105/joss.00797}{Whalley, L. D. (2018). effmass: An effective mass package. \textit{Journal of Open Source Software}, 3(28), p.797.} © 2018 CC-BY

\subsection*{Summary}
\label{sec:summary}

Many semiconductor properties depend on the response of electrons to an external perturbation. This perturbation could take the form of an electric field, change in temperature or an applied lattice stress. In a crystal, this response depends on the interaction of the electrons with a periodic potential. The effective mass approximation assumes that the response of an electron in a periodic potential is equivalent to that of a free electron with a renormalised mass (called the `effective mass'). This makes the effective mass a critical parameter in models for the optical and transport properties of a semiconductor.

The effective mass has a number of definitions, depending on the perturbation under consideration. The conventional definition of effective mass is inversely proportional to the second derivative of electron energy with respect to electron momentum.\autocite[p.~227]{Ashcroft1976} This allows the effective mass to be easily calculated from ab-initio band structures, and there are existing codes which have implemented this (see the `Related packages' Section below).

We must approximate the band structure with a parabola for the previous definition to be valid.\autocite{Ariel2012} However, this approximation breaks down when there is a high concentration of electrons in the material - when, for example, the material is doped or excited under a laser. Instead, we can then approximate the band structure with the Kane quasi-linear dispersion,\autocite{Kane1957} and the definition of effective mass is adapted accordingly.

\textsc{effmass} is a Python 3 package for calculating various definitions of effective mass from the electronic bandstructure of a semiconducting material. It contains a core class that calculates the effective mass and other associated properties of selected band structure segments. effmass also contains functions for locating band structure extrema, calculating the Kane quasi-linear dispersion parameters and plotting approximations to the true dispersion. Parsing of electronic structure data is facilitated by the \textsc{vasppy} package.\autocite{Morgan2018}

\textsc{effmass} is aimed towards theoretical solid state physicists and chemists who have a basic familiarity with Python. Depending on the functionality and level of approximation you are looking for, it may be that one of the packages listed below will suit your needs better.

\subsection*{Related packages}
\label{sec:related}

Effective mass calculations are implemented in a number of other packages:
\begin{itemize}
    \item \textsc{vasppy} \cite{Morgan2018}: This is installed as a dependancy of effmass. Calculates the effective mass using a least-squares quadratic fit for parabolic dispersions.
    \item \textsc{sumo} \cite{Ganose2018}: Calculates the effective mass using a least-squares fit for parabolic and non-parabolic dispersions.
    \item \textsc{emc} \cite{Fornari2012}: Calculates the effective mass tensor using a finite-difference method for parabolic dispersions.
    \item \textsc{pymatgen} \cite{Ong2013}: This is installed as a dependancy of effmass. Calculates an average effective mass tensor for non-parabolic dispersions with multiple bands and extrema. Also calculates the Seebeck effective mass as defined here.
\end{itemize}

\subsection*{Unique features of \textsc{effmass}}
\label{sec:unique}

To our knowledge, the following features are unique to this package:
\begin{itemize}
    \item Easily compare the values of curvature effective mass calculated using multiple numerical techniques (least-squares and polynomial fitting)
    \item Tailor the polynomial fitting used to approximate the DFT calculated dispersion: by choosing the order of the polynomial and the energy range to fit over.
    \item Visualise the dispersions used to approximate the DFT calculated dispersion
    \item Quantify non-parabolicity through the Kane dispersion parameters: effective mass at band-edge and alpha
    \item Calculate the optical effective mass assuming a Kane dispersion.
\end{itemize}

\textbf{Acknowledgements}

LW would like to thank Aron Walsh, Benjamin Morgan and Jarvist Moore Frost for their guidance during this project. This package was written during a PhD funded by the EPSRC through the Centre for Doctoral Training in New and Sustainable Photovoltaics (grant no. EP/L01551X/1). The input data used for developing and testing this package was generated using the ARCHER UK National Supercomputing Service. We have access to Archer via our membership of the UK's HEC Materials Chemistry Consortium, which is funded by EPSRC (EP/L000202).


    \documentclass{article}
    \usepackage{amsfonts}
    \usepackage{amsmath,multicol,eso-pic}
    \begin{document}
    \title{Algebra 101 worksheet 1}
 \date{\vspace{-5ex}}
 \maketitle

        \section{Linear equations}
        Solve the following equations for the specified variable.
        \begin{multicols}{1}
        \begin{enumerate}
        \item Solve for $a$ : $$H a + 5 = 14 a + g$$
\item Solve for $N$ : $$N S + 5 = N w - 11$$
\item Solve for $A$ : $$A E - 4 = 6 A + h$$
\item Solve for $w$ : $$6 w + z = 21 w + 23$$
\item Solve for $b$ : $$K + 23 b = Y b + 5$$
\item Solve for $M$ : $$M S - 4 = 4 M + 6$$
\item Solve for $y$ : $$X y + z = T y + V$$
\item Solve for $p$ : $$- 2 p + 18 = Q p + 12$$
\item Solve for $p$ : $$g - 5 p = V + p t$$
\item Solve for $H$ : $$E H + 20 = H u + y$$
\item Solve for $M$ : $$M R - 10 = M k + R$$
\item Solve for $M$ : $$- 26 M + 9 = M j + b$$
\item Solve for $V$ : $$P + S V = Q V + X$$
\item Solve for $P$ : $$- 10 P + e = - 23 P + 1$$
\item Solve for $g$ : $$- 14 g + 16 = - 21 g - 21$$
\item Solve for $K$ : $$- 23 K + S = K T - 6$$
\item Solve for $d$ : $$Q - 10 d = N d - 24$$
\item Solve for $D$ : $$8 D - 5 = D m + 22$$
\item Solve for $E$ : $$E q + y = - 5 E + 2$$
\item Solve for $r$ : $$r x + 10 = c - 5 r$$
        \end{enumerate}
        \end{multicols}

        \section{Quadratic equations}
        Solve the following quadratic equations.
        \begin{multicols}{1}
        \begin{enumerate}
        \item $$4 y^{2} = - 15 y - 10$$
\item $$9 x^{2} = 19 x + 11$$
\item $$x^{2} - 13 x - 230 = 0$$
\item $$2 y^{2} - 8 = y + 14$$
\item $$2 x^{2} + 22 x = 3 x^{2}$$
\item $$- 16 x^{2} + 16 x - 23 = - 3 x^{2} + 2$$
\item $$x^{2} - 8 x - 20 = 0$$
\item $$- 7 x^{2} - 1 = 15 x^{2} + 2 x - 15$$
\item $$- 8 x^{2} - 24 x = - 13 x^{2}$$
\item $$- 7 y^{2} - 25 y = 3 y^{2} - 14 y + 23$$
\item $$- 3 x^{2} = 12 x^{2} - 4 x$$
\item $$x^{2} - 27 x + 180 = 0$$
\item $$y^{2} - 8 y + 12 = 0$$
\item $$x^{2} - 6 x - 391 = 0$$
\item $$x^{2} + 8 x + 7 = 0$$
\item $$x^{2} + 18 x + 65 = 0$$
\item $$- 20 x^{2} + 5 x - 21 = - 24 x^{2}$$
\item $$- 21 x^{2} + 20 x + 15 = 10 x$$
\item $$- 11 y^{2} = - 24 y^{2} + 25 y + 8$$
\item $$4 x^{2} + 10 x = 14 x$$
        \end{enumerate}
        \end{multicols}

        \section{Compute the derivative}
        ['x', 'y', 'z']
        \begin{multicols}{1}
        \begin{enumerate}
        \item $$\frac{d}{d x}\left(\frac{1}{x} \left(7 x^{2} + 12 x - 24\right)\right)$$
\item $$\frac{d}{d x}\left(\frac{2 \sqrt{x}}{- 9 x^{3} + 18 x + 7}\right)$$
\item $$\frac{d}{d x}\left(\frac{\log{\left (x \right )} + \tan{\left (x \right )}}{16 x^{3} - 23 x^{2} + 5 x}\right)$$
\item $$\frac{d}{d x}\left(\left(e^{x} + \tan{\left (x \right )}\right) e^{- x}\right)$$
\item $$\frac{d}{d x}\left(\left(19 x + e^{x}\right) e^{- x}\right)$$
\item $$\frac{d}{d x}\left(\frac{1}{x} \left(24 x^{2} + 7 x + \cos{\left (x \right )}\right)\right)$$
\item $$\frac{d}{d x}\left(\frac{1}{\tan{\left (x \right )}} \left(\log{\left (x \right )} + \sin{\left (x \right )}\right)\right)$$
\item $$\frac{d}{d x}\left(\frac{1}{x} \left(4 x^{3} + \log{\left (x \right )} - 17\right)\right)$$
\item $$\frac{d}{d x}\left(\frac{1}{\sin{\left (x \right )}} \left(- 17 x^{3} + 24 x^{2} + 14 x + \log{\left (x \right )} - 14\right)\right)$$
\item $$\frac{d}{d x}\left(\left(\sqrt{x} + \log{\left (x \right )}\right) e^{- x}\right)$$
        \end{enumerate}
        \end{multicols}

    \end{document}


\documentclass[twoside]{MATH77}
\usepackage{multicol}
\usepackage[fleqn,reqno,centertags]{amsmath}
\begin{document}
\begmath 19.1 System Parameters

\silentfootnote{$^\copyright$1997 Calif. Inst. of Technology, \thisyear \ Math \`a la Carte, Inc.}

\subsection{Purpose}

These subprograms provide values of various system parameters that
are needed in library subprograms and typically have different values
on different computer systems.  ``F. Supporting Information'' below
has instructions for customizing these programs for your system.

\subsection{Usage}

\subsubsection{Program Prototype}

\begin{description}
\item[REAL]  \ {\bf R1MACH, S}

\item[DOUBLE PRECISION]  \ {\bf D1MACH, D}

\item[INTEGER]  \ {\bf I1MACH, I, J}
\end{description}

Set J in the range 1 $\leq $ J $\leq $ 5 for R1MACH or D1MACH or 1 $\leq $ J
$\leq $ 16 for I1MACH. Then, use the appropriate one of the following
statements:
$$
\fbox{{\bf S = R1MACH(J)}}
$$
$$
\fbox{{\bf D = D1MACH(J)}}
$$
$$
\fbox{{\bf I = I1MACH(J)}}
$$
The results, S, D, or I are set as described in Section D.

\subsubsection{Argument Definitions}

\begin{description}
\item[J]  \ [in] Integer argument selecting the desired system parameter as
described in Section D.
\end{description}

\subsection{Examples and Remarks}

The program DRMACH lists all the values obtainable from R1MACH, D1MACH, and
I1MACH on the host computer system. Output is shown from several different
host systems.

\subsection{Functional Description}

For the purpose of this package a model of the Fortran~77 INTEGER, REAL, and
DOUBLE PRECISION number sets is characterized by a total of nine fundamental
parameters.

The model of Fortran~77 numbers of type INTEGER is parameterized by two
numbers, $a$ and $s$, where $a$ denotes the base (radix) of the number
system and $s$ denotes the maximum number of base $a$ digits available to
represent a Fortran integer. Thus the integers range from $-(a^s -1)$ to $%
a^s -1.$

The model of Fortran~77 numbers of type REAL is characterized by four
parameters, $b$, $t$, $emin$, and $emax$, where $b$ is the base of the
fraction part, $t$ is the number of base $b$ digits in the fraction part, $%
emin $ is the minimum exponent and $emax$ is the maximum exponent. The
magnitude of a floating-point number is thus of the form%
\begin{equation*}
b^e\left( c_1b^{-1}+c_2b^{-2}+...+c_tb^{-t}\right) ,
\end{equation*}
where, $emin\leq e\leq emax$, and the digits $c_i$ satisfy $0\leq c_i\leq
b-1.$ A nonzero floating-point number is normalized if and only if the digit
$c_1$ is nonzero. We shall consider only normalized floating-point numbers,
although numeric processors based on the IEEE standard also support a
range of unnormalized numbers.

Fortran~77 numbers of type DOUBLE PRECISION are modeled in the same form as
REAL numbers and are assumed to have the same base, $b$, but generally
different values of $t$, $emin$, and $emax$.

For some computer systems no setting of these parameters will make the model
system coincide exactly with the actual computer's number set. In such cases
the model parameters are selected so the model system will be as large a
subset of the actual number set as possible. In particular, for Cray
X/MP and Y/MP (but not T3D) systems the parameters $t$ and $emax$ are set
smaller and $emin$ is set larger than one might expect from the structure
of floating-point numbers on these systems. The reasons for this are
described in \cite{Schryer:1981:ATO}.

The values returned by this package are either prestored constants or are
computed at compile-time from prestored constants by use of expressions in
PARAMETER statements. Thus correct values must be determined and edited into
the package whenever this package is moved to a new computer system. Correct
values for many systems are present as comments in the source code.

On some systems, the Fortran compiler may not be able to compute
numbers in the full range of the model, in PARAMETER statements (this
disease usually strikes computation of the overflow limit).  On such
systems it may be necessary to restrict the model, $e.g.$ by reducing
$emax$.

{\bf Specification of Values Returned}
\begin{itemize}
\item[\bf J]  {\hspace{.5in} {\bf I1MACH (J)}}
\item[1]  Standard input unit number.
\item[2]  Standard output unit number.
\item[3]  Standard punch unit number.
\item[4]  Standard error message unit number.
\item[5]  Number of bits per Fortran integer storage unit.
\item[6]  Number of characters per Fortran integer storage unit.
\item[7]  $a$, the base for integers.
\item[8]  $s$, the number of base $a$ digits in an integer.
\item[9]  $a^s-1$, the largest integer magnitude.
\item[10]  $b$, the base for floating-point numbers. Assumed the same for
REAL and DOUBLE PRECISION arithmetic.
\item[11]  $t$, the number of base $b$ digits for REAL arithmetic.
\item[12]  $emin$, minimum exponent for REAL arithmetic.
\item[13]  $emax$, maximum exponent for REAL arithmetic.
\item[14]  $t$, the number of base $b$ digits for DOUBLE PRECISION
arithmetic.
\item[15]  $emin$, minimum exponent for DOUBLE PRECISION arithmetic.
\item[16]  $emax$, maximum exponent for DOUBLE PRECISION arithmetic.
\end{itemize}
\begin{itemize}
\item[\bf J]  {\hspace{.5in} {\bf R1MACH(J)}}
\item[1]  $b^{emin-1}$ smallest positive normalized REAL number,
(underflow limit).
\item[2]  $b^{emax}(1-b^{-t})$, largest REAL number, (overflow limit).
\item[3]  $b^{-t}$, smallest relative difference between two successive
nonzero REAL numbers. This is also the difference between~1.0 and the next
smaller REAL number.
\item[4]  $b^{-(t-1)}$ largest relative difference between two successive
nonzero REAL numbers. This is also the difference between~1.0 and the next
larger REAL number.
\item[5]  $\log _{10}b$, useful in certain conversions between base $b$ and
base~10.
\end{itemize}
The values returned by D1MACH are as described above for R1MACH with REAL
replaced by DOUBLE PRECISION.

\subparagraph{Historical perspective and relations to other languages}

The specifications of R1MACH, D1MACH, and I1MACH and the original
implementation were developed at the AT\&T Bell Laboratories, Murray Hill,
New Jersey, in the~1970's to support the development of portable
mathematical software, and specifically the PORT library,
\cite{Fox:1978:PMS}, which is a proprietary AT\&T Bell Laboratories
product.  These three subprograms were published as a subset of
Algorithm~528 in TOMS,~\cite{Fox:1978:AFP}, and are not proprietary.  The
MATH77 version has the same specification but is substantially different
in its implementation from the original versions.

The attributes associated with J = 1, 2, 3, 4, and~6 in I1MACH are less
relevant in Fortran~77 in the~90's than they were in Fortran~66 in the~70's.
In particular, only the DNLxxx and SNLxxx subroutines of
Chapter~9.3 access I1MACH(2).

Other MATH77 library subprograms use PRINT or WRITE(*,...) for printing.

Languages developed more recently than Fortran~77, such as Ada, ANSI C, and
Fortran~90, provide methods within the language to obtain certain
environmental parameters. Consider, for example, the underflow and overflow
limits, and precision for floating-point arithmetic. Using the present
package, these can be obtained for DOUBLE PRECISION arithmetic by
referencing D1MACH(1), D1MACH(2), and D1MACH(4), respectively, and for REAL
arithmetic by referencing R1MACH(1), R1MACH(2), and R1MACH(4). In Fortran~90
these parameters can be obtained by referencing the generic inquiry
functions TINY(X), HUGE(X), and EPSILON(X), where X may be any DOUBLE
PRECISION entity to obtain the values for DOUBLE PRECISION arithmetic, and
any REAL entity to obtain the values for REAL arithmetic. In ANSI C these
values for arithmetic of type $double$ are given by the macro names DBL\_MIN,
DBL\_MAX, and DBL\_EPSILON, for type $float$ there are FLT\_MIN, FLT\_MAX,
and FLT\_EPSILON, and for type $long\ double$ LDBL\_MIN, LDBL\_MAX, and
LDBL\_EPSILON.  All of these are defined in the standard
header file $float.h$.

\bibliography{math77}
\bibliographystyle{math77}

\subsection{Error Procedures and Restrictions}

If the argument is outside the range 1\ $\leq $ J $\leq $ 16 for I1MACH or
outside 1 $\leq $ J $\leq $ 5 for R1MACH or D1MACH, an error message is
printed and execution is terminated.

This package contains a partial protection against the inadvertent use of
the wrong version of one of these subroutines; say using the PC version on a
VAX. On the first call to any one of these subprograms, tests are done to
verify that two of the stored parameter values are not grossly wrong for the
current environment. These tests depend on assumptions about hardware,
compilers, and linkers that may be invalidated by technological changes.
Subroutines AMTEST and AMSUB1 are used to support these tests and are not
intended for any other usage.

\subsection{Supporting Information}

The source language is ANSI Fortran~77. All the program units are
grouped into a single file, AMACH.FOR. The filename may be different
on different systems, $e.g.$, ``amach.f" on UNIX systems.

One can either customize AMACH for a new system by commenting out
lines defining the parameters required for the system as it is
currently configured, and uncommenting lines required for the desired
system, or one can use the program {\tt m77con} described in
Chapter~19.4.  This requires making up a small control file {\tt
m77job}, and compiling, linking, and running {\tt m77con}, which is
self contained.  To make up {\tt m77job} for a VAX running UNIX, the
control file would contain the following.

{\tt SET SYS = VAX\newline
FILE amach.f}

Running {\tt m77con} with this control file and amach in the current
directory will generate a file {\tt amach.f} for the VAX.  If one
wants the extension ``{\tt .for}'' change the {\tt h.f} to {\tt
h.for}.  If one wants a machine other than the VAX, choose a value
for SYS (without any parenthetical remark) from the following table.
{\tt SYS=IEEE} covers any machine that uses the IEEE binary standard
for floating point arithmetic.  If your machine is not included in
this list, either pick a machine with the same parameters for the
floating point arithmetic as for a machine on this list, or enter
parameters for your machine as a new option into AMACH.

\begin{tabbing}
{\tt SYS = IEEE}\\
{\tt SYS = AMDAHL}\\
{\tt SYS = APOLLO\_10000}\\
{\tt SYS = BUR1700}\\
{\tt SYS = BUR5700}\\
{\tt SYS = BUR67\_7700}\\
{\tt SYS = CDC60\_7000}\\
{\tt SYS = CONVEXC\_1}\\
{\tt SYS = CRAY1}\\
{\tt SYS = CRAY1\_SD}\hspace{30pt}\=(Sngl prec.arith. used for dble.)\\
{\tt SYS = CRAY1\_64}\>(64 bit integers)\\
{\tt SYS = CRAY1\_SD\_64}\>(64 bit int, SP used for DP)\\
{\tt SYS = CRAY\_J90}\\
{\tt SYS = CRAY\_J90\_SD}\>(Sngl prec. used for dble.)\\
{\tt SYS = DG\_S2000}\\
{\tt SYS = HARRIS220}\\
{\tt SYS = HON600\_6000}\\
{\tt SYS = HON\_DPS\_8\_70}\\
{\tt SYS = HP700Q}\>(Q Precision on HP700 series)\\
{\tt SYS = IBM360\_370}\\
{\tt SYS = INTERDATA\_8\_32}\\
{\tt SYS = PDP10\_KA}\\
{\tt SYS = PDP10\_KB}\\
{\tt SYS = PDP11}\\
{\tt SYS = PRIME50}\\
{\tt SYS = SEQ\_BAL\_8000}\\
{\tt SYS = UNIVAC}\\
{\tt SYS = VAX}\\
{\tt SYS = VAX\_G}\\
{\tt SYS = ALPHA\_D3}
\end{tabbing}

Designed and programmed by P.A. Fox, A.D. Hall, and N.L. Schryer, AT\&T Bell
Laboratories, 1978. Adapted to the JPL MATH77 library, 1984 and~1987.

\begin{tabular}{@{\bf}l@{\hspace{5pt}}l}
\bf Entry & \hspace{.2in} {\bf Required Files}\vspace{2pt} \\
D1MACH & \hspace{.35in} AMACH\\
I1MACH & \hspace{.35in} AMACH\\
R1MACH & \hspace{.35in} AMACH\\\end{tabular}

\begcode

\medskip
\lstset{language=[77]Fortran,showstringspaces=false}
\lstset{xleftmargin=.8in}

\centerline{\bf \large DRMACH}\vspace{0pt}
\lstinputlisting{\codeloc{mach}}

\centerline{{\bf \large Results from Various Machines\hspace{1in}}}

\begin{lstlisting}{}

              MACHINE CONSTANTS for IEEE Arithmetic
              -----------------------------------------
 J      I1MACH(J)       R1MACH(J)             D1MACH(J)

 1              5    0.11754944E-37    0.222507385850720100-307
 2              6    0.34028235E+39    0.179769313486231600+309
 3              7    0.59604645E-07    0.111022302462515700E-15
 4              6    0.11920929E-06    0.222044604925031300E-15
 5             32    0.30103001        0.301029995663981200
 6              4
 7              2
 8             31
 9     2147483647
10              2
11             24
12           -125
13            128
14             53
15          -1021
16           1024
\end{lstlisting}

\begin{lstlisting}{}

              MACHINE CONSTANTS for VAX
              -----------------------------------------
 J      I1MACH(J)       R1MACH(J)             D1MACH(J)
 1              5    0.29387359E-38    0.293873587705571877E-38
 2              6    0.17014117E+39    0.170141183460469229E+39
 3              7    0.59604645E-07    0.138777878078144568E-16
 4              6    0.11920929E-06    0.277555756156289135E-16
 5             32    0.30103001        0.301029995663981198
 6              4
 7              2
 8             31
 9     2147483647
10              2
11             24
12           -127
13            127
14             56
15           -127
16            127
\end{lstlisting}

\newpage
\begin{lstlisting}{}

              MACHINE CONSTANTS for CRAY J90
              -----------------------------------------
 J      I1MACH(J)       R1MACH(J)             D1MACH(J)
 1              5    0.73344155-2465   0.733441547021938866-2465
 2              6    0.13634352+2466   0.136343516952426991+2466
 3            102    0.71054274E-14    0.504870979341447555E-28
 4              6    0.14210855E-13    0.100974195868289511E-27
 5             64    0.30103000        0.301029995663981195
 6              8
 7              2
 8             46
 9 70368744177663
10              2
11             47
12          -8188
13           8189
14             94
15          -8188
16           8189

              MACHINE CONSTANTS for UNISYS 1100
              -----------------------------------------
 J      I1MACH(J)       R1MACH(J)             D1MACH(J)
 1              5     .14693679-038     .278134232313400172-308
 2              6     .17014118+039     .898846567431157951+308
 3              7     .74505806-008     .867361737988403547-018
 4              6     .14901161-007     .173472347597680709-017
 5             36     .30103000         .301029995663981194
 6              4
 7              2
 8             35
 9    34359738367
10              2
11             27
12           -128
13            127
14             60
15          -1024
16           1023
\end{lstlisting}
\end{document}


\graphicspath{{Chapter2/Figs/}}

\section{Model validation with simulated data} \label{section:mofa_simulated}

We used simulated data from the generative model to systematically test the technical capabilities of MOFA.

\subsection{Recovery of simulated factors}

First, we tested the ability of MOFA to recover simulated factors under varying number of views, features, factors and with different amounts of missing values.\\
For every simulation scenario we initialised a model with a high number of factors ($K=100$), and inactive factors were automatically dropped during model training by the ARD prior. In addition, to test the robustness under different random initialisations, 10 model instances were trained for every simulation scenario.\\
We observe that in most settings the model accurately recovers the correct number of factors (\Cref{fig:MOFA_learnK}). Exceptions occur when the dimensionality of the latent space is too large (more than 50 factors) or when an excessive amount of missing values (more than 80\%) is present in the data.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.9\textwidth]{MOFA_learnK}
	\caption{\textbf{Assessing the ability to recover simulated factors}.\\
	In all plots the y-axis displays the number of infered factors. (a) x-axis displays the number of true factors, and boxplots summarise the distribution of inferred factors across 10 model instances. For (b-d) the true number of factors was set to $K=10$ and each bar corresponds to a different model instance. (b) x-axis displays the number of features, (c) x-axis displays the number of views, (d) x-axis displays fraction of missing values. }
	\label{fig:MOFA_learnK}
\end{figure}

\subsubsection{View-wise sparsity on the weights}

One of the essential features of MOFA is the use of an ARD prior aimed at disentangling the activity of factors across views (see \Cref{section:ard} and \Cref{mofa:model_description}).\\
We simulated data from the generative model such that the factors were set to be active or inactive in specific views by sampling $\alpha_{k}^{m}$ from a discrete distribution with values $\{ 1, 1e3\}$. We compared the performance with a popular integrative clustering method (iCluster) that is also formulated as a latent variable model \cite{Mo2013}. In iCluster each factor shares the same sparsity constraint across all views, and hence the model is less accurate at detecting factors that show differential activity across different views:

\begin{figure}[H]
	\centering
	\includegraphics[width=0.9\textwidth]{MOFA_group_sparsity}
	\caption{\textbf{Evaluating the ability to recover differential factor activity across views}.\\
	(a) The true activity pattern, with factors sampled to display differential activity across views.
	(b) Percentage of variance explained for each factor in each view, for MOFA and iCluster \cite{Mo2013}.}
	\label{fig:MOFA_group_sparsity}
\end{figure}

\subsubsection{Feature-wise sparsity on the weights} \label{section:spike_slab}

In MOFA we implemented a spike-and-slab prior prior to enforce feature-wise sparsity on the weights with the aim of delivering a more interpretable solution (see \Cref{section:mofa_weights}).\\
To assess the effect of the spike-and-slab prior we trained a group of models with and without the spike-and-slab prior. Importantly, both models contain the ARD prior, which should provide some degree of regularisation. To compare both options to a non-sparse method, we also fit a Principal Component Analysis on the concatenated data set. As expected, we observe that the spike-and-slab prior induces more zero-inflated weights, although the ARD prior provided a moderate degree of regularisation. The PCA solution was notably more dense than both Bayesian models (\Cref{fig:MOFA_sparsity}).

\begin{figure}[H]
	\centering
	\includegraphics[width=0.7\textwidth]{MOFA_sparsity}
	\caption{\textbf{Assessing the sparsity priors on the weights}.\\
	The plot shows the empirical cumulative density function of the weights for an arbitrary factor in a single view. The weights were simulated with a sparsity level of $\theta_k^m=0.5$ (50\% of active features.)
	}
	\label{fig:MOFA_sparsity}
\end{figure}

\subsection{Non-Gaussian likelihoods}  \label{section:mofa_nongaussian_results}

A key improvement of MOFA with respect to previous methods is the use of non-Gaussian likelihoods to integrate data modalities with different types of readouts. In particular, as described in \Cref{section:mofa_ngaussian}, we implemented a Bernoulli likelihood to model binary data and a Poisson likelihood to model count data.\\
To validate both likelihood models, we simulated binary and count data using the generative model and we fit two sets of models for each data type: a group of models with a Gaussian likelihood and a group of models with a Bernoulli or Poisson likelihood, respectively.\\
Reassuringly, we observe that although the Gaussian likelihood is also able to recover the true number of factors, the models with the non-Gaussian likelihoods result in a better fit to the data:

\begin{figure}[H]
	\centering
	\includegraphics[width=0.8\textwidth]{MOFA_nongaussian}
	\caption{\textbf{Validation of the non-Gaussian likelihood models using simulated data}.\\
	(a-d) Comparison of Poisson and Gaussian likelihood models applied to count data.\\
	(e-h) Comparison of Bernoulli and Gaussian likelihood models applied to binary data.\\
	(a,e) The y-axis displays the ELBO for each model instance (x-axis). (b,f) The y-axis displays the mean reconstruction error for each model instance (x-axis). (c,g) The y-axis displays the number of estimated factrors for each model instance (x-axis). The horizontal dashed line marks the true number of factors $K=10$. (d,h) Distribution of reconstructed data. Plotted are the expected values of the inferred posterior distributions, not samples from the corresponding posteriors. This is why reconstructed measurements are continuous and not discrete.
	}
	\label{fig:MOFA_nongaussian}
\end{figure}

\subsection{Scalability}

Finally, we evaluated the scalability of the model when varying each of its dimensions independently, and we compared the speed with an implementation of GFA that uses Gibbs Sampling \cite{Leppaaho2017} and the popular Cluster+\cite{Mo2013}, which adopts a maximum-likelihood approach with grid search to optimise the hyperparameters. Overall, we observe that MOFA scales linear with respect to all dimensions and is significantly faster than any of the three evaluated techniques (\Cref{fig:MOFA_scalability}).

\begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{MOFA_scalability}
	\caption{\textbf{Evaluation of scalability in MOFA}.\\
	Shown is the time required for convergence (y-axis, in minutes). The x-axis displays the value of the dimension that was tested, either number of factors ($K$), number of features ($D$), number of samples ($N$) and number of views ($M$). Baseline parameters were $M=3, K=10, D=1000, N=100$. Each line represents a different model, GFA (red), MOFA (blue) and iCluster (green). Default convergence criteria where used for all methods. Each dot displays the average time across 10 trials with error bars denoting the standard deviation. iCluster is only shown for one value as all other settings required more than 200min for convergence.
	}
	\label{fig:MOFA_scalability}
\end{figure}

As a real application showcase, the training on the CLL cohort that is described below (\Cref{fig:MOFA_CLL_overview}) required 25 minutes using MOFA, 34 hours with GFA and 5-6 days with iCluster.

% \subsection{Class imbalance}  \label{section:class_imbalance}
% TO-DO.
%The objective function (the evidence lower bound, ELBO) does not weight the different data modalities according to the number of features. Hence, in general the larger the number of features
%Having said this, we find the model to be very robust to differences in the dimensionality of the feature space.

\newpage

\section{Application to a cohort of Chronic Lymphocytic Leukaemia patients} \label{section:mofa_cll}

Personalised medicine is an attractive field for the use of multi-omics, as dissecting heterogeneity across patients is a major challenge in complex diseases, and requires data integration from multiple biological layers \cite{Chen2013,Costello2014,Alyass2015}.

To demonstrate the potential of MOFA, we applied it to a publicly available study of 200 patient samples of Chronic Lymphocytic Leukaemia (CLL) profiled for somatic mutations, RNA expression, DNA methylation and \textit{ex vivo} drug responses \cite{Dietrich2018}, all of them at the bulk level. We selected this data set for three main reasons: (1) The complex missing data structure, with nearly 40\% samples having incomplete assays (\Cref{fig:MOFA_CLL_overview}). As described in \Cref{section:mofa_missing_values}, the inference framework implemented in MOFA should cope with large amounts of missing values, including missing assays. (2) After data processing, three assays had continuous observations whereas for the somatic mutations the observations were binary. As described in \Cref{section:mofa_ngaussian}, MOFA can combine different likelihood models. (3) The existence of clinical covariates provide an excellent test to evaluate whether the MOFA factors can capture the molecular variation that underlies clinically-relevant phenotypes.

\subsection{Data overview and processing}

Data processing and normalisation is essential for the model to work and it requires a few considerations. First, in the case of count-based assays such as RNA-seq one needs to remove differences in library size between samples. If not done correctly, the signal in the data will be dominated by this (undesired) source of variation, and more subtle heterogeneity will be harder to identify. Similarly, batch effects and other undesired technical sources of variation should be regressed out \textit{a priori}, although this was not the case for this particular data set. Second, feature selection must be performed by selecting highly variable features. A proper feature selection will increase the signal-to-noise-ratio, it will simplify model selection and it will speed up the training procedure.  Finally, as discussed above, the total number of features can influence the contribution of a data modality to the latent space. To mitigate this problem it is recommended to keep the number of features per view within the same order of magnitude, when possible.

Here we proceed to briefly describe the different data modalities and outline the basic data processing steps that we performed before applying MOFA:
\begin{itemize}
	\item \textbf{RNA expression} was profiled using bulk RNA-seq. Genes with low counts were filtered out and the data was subsequently normalized using DESeq2 \cite{Love2014}. Feature selection was performed by considering the top 5,000 most variable genes.
	\item \textbf{DNA methylation} was profiled using Illumina 450K arrays. We converted the beta-values to M-values, as it has better statistical properties when modelled with a Gaussian distribution \cite{Du2010}. Feature selection was performed by considering the top 1\% most variable CpG sites.
	\item \textbf{\textit{Ex vivo} Drug response} was screened using the ATP-based CellTiter-Glo assay. Briefly, the asay includes a panel of 62 drugs at 5 different concentrations each, for a total of 310 measurements. The readout is a number proportional to the fraction of viable cells in culture based on quantitation of the ATP present, which signals the presence of metabolically active cells.
	\item \textbf{Somatic mutations} were profiled using a combination of targeted and whole exome sequencing. Feature selection was performed by considering only mutations that were present in at least three samples, which resulted in a total of 69 mutations.
\end{itemize}

% For more details on the data generation steps we refer the reader to \cite{Dietrich2018}.

\subsection{Model overview}

In this data set, MOFA recovered $K=10$ factors, each one explaining a minimum of 3\% of variance in at least one assay. Interestingly, MOFA detected factors which are shared across several data modalities (Factors 1 and 2, sorted by variance explained). Some factors captured sources of covariation between two data modalities (Factor 3 and 5, active in the RNA expression and drug response). In addition, some factors captured variation that is unique to a single data modality (Factor 4, active in the RNA expression data).\\
All together, the 10 MOFA factors explained 41\% of variance in the drug response data, 38\% in the mRNA expression, 24\% in the DNA methylation and 24\% in somatic mutations.

\begin{figure}[H]
	\centering
	\includegraphics[width=1.0\textwidth]{MOFA_CLL_overview}
	\caption{\textbf{Application of MOFA to a study of chronic lymphocytic leukaemia. Model overview.}\\
	(a) Data overview. Assays are shown in different rows ($D$ = number of features) and samples ($N$) in columns, with missing samples shown using grey bars. Notice that some samples are missing entire assays.\\
	(b) Variance explained (\%) by each Factor in each assay.\\
	(c) Total variance explained (\%) for each assay by all factors.
	}
	\label{fig:MOFA_CLL_overview}
\end{figure}

The first two factors are the most interesting from a molecular perspective, as they capture a phenotypic effect that is manifested across multiple molecular layers. To annotate Factors 1 and 2 we proceeded to visualise the feature weights, starting by the (binary) somatic mutation data, as it is the simplest data modality to interpret. Inspection of the top weights revealed that Factor 1 was associated with the mutation status of the immunoglobulin heavy-chain variable (IGHV) region, while Factor 2 was aligned with trisomy of chromosome 12 (\Cref{fig:MOFA_CLL_factors12}).\\
Remarkably, in a completely unsupervised fashion, MOFA recovered the two most important clinical markers in CLL as the two major axes of molecular disease heterogeneity \cite{Fabbri2016,Bulian2017,Crombie2017}.

Next, we visualised the samples in the latent space spanned by Factors 1 and 2. A scatterplot based on these factors shows a clear separation of patients by their IGHV status on the first Factor and presence or absence of trisomy 12 on the second Factor (\Cref{fig:MOFA_CLL_factors12}). Interestingly, 24 patients lacked IGHV status measurements (grey crosses) due to quality control filtering in the DNA sequencing assay. Nonetheless, MOFA was able to pool information from the other molecular layers to map those samples to the latent space, and could be classified to the corresponding molecular subgroup.

\begin{figure}[H]
	\centering
	\includegraphics[width=1.0\textwidth]{MOFA_CLL_factors12}
	\caption{\textbf{Visualisation of the genetic signature underlying Factor 1 and 2}\\
	(a) Weights of the top somatic mutations for Factors 1 and 2.
	(b) Scatterplots of Factors 1 and 2. Each dot corresponds to one sample and the colours denote the IGHV status of the tumours samples; symbol shape indicate chromosome 12 trisomy status.
	}
	\label{fig:MOFA_CLL_factors12}
\end{figure}

IGHV status is currently the most important prognostic marker in CLL and has routinely been used to distinguish between two distinct subtypes of the disease\cite{Fabbri2016}. Molecularly, it is a surrogate of the level of activation of the B-cell receptor, which is in turn related to the differentiation state of the tumoral cells. Multiple studies have associated mutated IGHV with a better response to chemotherapy, whereas unmutated IGHV patients have a worse prognosis \cite{Fabbri2016,Bulian2017,Crombie2017}.\\
In clinical practice, the IGHV status has been considered binary. Our results suggest that this is a fairly good approximation, but a more complex structure with at least three groups or a potential underlying continuum is supported (\Cref{fig:MOFA_CLL_factors12,fig:MOFA_CLL_Factor1}), as also suggested in \cite{Queiros2015}.

% \subsection{Detection of outlier samples}

% Interestingly, there is some discrepancy between the IGHV status predicted by MOFA and the IGHV status reported in the clinical data. Out the 200 patients, MOFA classifies 176 in accordance with the clinical label, it classifies 12 patients that lacked the clinical marker and it re-classifies 12 patients to the opposite group.\\
% To validate the MOFA-based classification, we proceeded to inspect the molecular data in more detail.

% sample-to-sample correlation matrices for the individual layers suggest that for 3 of the cases where the inferred factor disagrees with the clinical label, the molecular data supports the predicted label. The other 9 cases showed intermediate molecular sgnatures now well captured by the binary classification.
% %Based on these results, we hypothesize that a multi-omics approach based on several molecular signatures could be a more precise and robust approach to predict clinical phenotypes than the use of single features such as mutations or expression of marker genes.

% \begin{figure}[H]
% 	\centering
% 	\includegraphics[width=1.0\textwidth]{MOFA_IGHV_outlier}
% 	\caption{XX}
% 	\label{fig:MOFA_IGHV_outlier}
% \end{figure}

\subsection{Molecular characterisation of Factor 1}
% \tabularnewline

An important step in the MOFA pipeline is the characterisation of the molecular signatures underlying each Factor. I will demonstrate this for Factor 1, although a similar strategy can be applied to Factor 2.

On the RNA expression, inspection of the top weights pinpoint genes that have been previously associated to IGHV status, some of which have been proposed as clinical markers\cite{Vasconcelos2005,Morabito2015}. Heatmaps of the RNA expression levels for these genes reveals clear differences between samples when ordered according to the Factor 1 values.

On the drug response data the weights highlight kinase inhibitors targeting the B-cell receptor pathway. Splitting the patients into three groups based on k-means clustering shows clear separation in the drug response curves.

% Copied
\begin{figure}[H]
	\centering
	\includegraphics[width=0.90\textwidth]{MOFA_CLL_Factor1}
	\caption{
	\textbf{Characterization of MOFA Factor 1 as IGHV status.}\\
	(a) Beeswarm plot of Factor 1 values, where each dot corresponds to a patient sample. Colours denote three groups found by applying 3-means clustering on the Factor values.\\
	(b) Genes with the largest weights (in absolute values) in the mRNA data. Plus or minus symbols on the right indicate the sign of the weight.\\
	(c) Heatmap of gene expression values for the genes with the largest weights displayed in (b).\\
	(d) Drugs with the largest weights (in absolute values) in the Drug response data, coloured by the drug's target category.\\
	(e) Drug response curves for two of the drugs with top weights, stratified by the clusters displayed in (a).
	}
	\label{fig:MOFA_CLL_Factor1}
\end{figure}

\subsection{Molecular characterisation of other factors}

Despite their clinical importance, Factor 1 (IGHV status) and Factor 2 (chr12 trisomy) they explain less than 20\% variability in each data modality, suggesting the existence of more subtle sources of variation. As an example, we will also characterise Factor 5, which explains 2\% of the variance in the mRNA and 6\% of variance in the drug response.\\
As mentioned in \Cref{mofa:downstream}, instead of exploring the feature weights individually, factors can be annotated using gene set annotations. This procedure is particularly appealing for RNA expression data, as a rich amount of resources exist that have categorised genes into ontologies in terms of biological pathways, molecular function and cellular components  \cite{Fabregat2015,Ashburner2000}.

Briefly, the idea is to aggregate the weights using prior information to obtain a single statistic for each gene set, which can be tested against a competitive null hypothesis. Inspired from \cite{Frost2015}, in MOFA we implemented several scoring schemes and a variety of parametric and unparametric statistical tests. By default we use the weights as feature statistics and the average difference in the weight values as the feature set statistic. P-values are then obtained per feature set and factor via a simple t-test.

Gene Set Enrichment Analysis on the RNA weights using the Reactome annotations \cite{Fabregat2015} reveals that Factor 2 is strongly enriched for oxidative stress and senescence pathways. Inspection of the top features highlights the importance of heat shock proteins (HSPs), a group of proteins that are essential for protein stability which are up-regulated upon stress conditions like high temperatures, pH shift or oxidative stress. Importantly, HSPs can be elevated in tumour cells and potentially contribute to prolonged tumour cell survival\cite{Dempsey2010}. In agreement with the findings from the mRNA view, the drugs with largest weights on Factor 5 belong to clinical categories associated with stress response, such as target reactive oxygen species and DNA damage response (\Cref{fig:MOFA_CLL_Factor5})

\begin{figure}[H]
	\centering
	\includegraphics[width=0.95\textwidth]{MOFA_CLL_Factor5}
	\caption{
	\textbf{Characterization of Factor 5 in the CLL cohort as oxidative stress response.}\\
	(a) Beeswarm  plot of Factor 5, where each dot corresponds to a patient sample. Colours represent the expression of TNF, an inflammatory stress marker that is present among the top mRNA weights.\\
	(b) Gene set enrichment analysis results using Reactome pathways. Displayed are the top pathways with the strongest enrichment.\\
	(c) Heatmap of mRNA expression values for representative genes among the top weights. Samples are ordered by their Factor 5 values.\\
	(d) Weights for the top drugs, annotated by target category.\\
	(e) Heatmap of drug response values for the top three drugs. Samples are ordered by their Factor 5 values, as in (c).
	}
	\label{fig:MOFA_CLL_Factor5}
\end{figure}

\subsection{Prediction of clinical outcomes}

We conjectured that the integration of multiple molecular layers could allow an improved prediction of the patients' clinical outcome. To evaluate the utility of the MOFA factors as predictors of clinical outcomes we fit Cox regression models \cite{Cox1972} using the patients' time to next treatment (TTT) as a response variable. Two types of analysis were performed: a univariate analysis where each Factor was independently associated with TTT, and a multivariate analysis where the combination of all factors were used to predict TTT (\Cref{fig:MOFA_CLL_Cox}). In the univariate Cox models, we observe  that Factor 1 (IGHV status), Factor 7 (associated with chemo-immunotherapy treatment prior to sample collection) and Factor 8 (enriched for Wnt signalling) were significant predictors of TTT. Accordingly, when splitting patients into binary groups based on the corresponding Factor values, we observe clear differences in the survival curves. In the multivariate Cox model, MOFA (Harrell's C-Index C=0.78) outperformed all other input settings, including PCA on single-omic data (C=0.68-0.72), individual genetic markers (C=0.66) as well PCA applied to the concatenated data matrix (C=0.74).

% Caption copied
\begin{figure}[H]
	\centering
	\includegraphics[width=0.9\textwidth]{MOFA_CLL_Cox}
	\caption{
	\textbf{Association analysis between MOFA factors and clinical putcome.}\\
	(a) Association of MOFA factors to time to next treatment using a univariate Cox regression model Error bars denote 95\% confidence intervals. Numbers on the right show p-values for each Factor.\\
	(b) Kaplan-Meier plots for the three MOFA factors that show a significant association with time to next treatment.\\
	(c) Prediction accuracy of time to treatment using multivariate Cox regression trained with the first 10 principal components applied to single data modalities, the full data set or the 10 MOFA factors. Shown are average values of Harrell's C-index from fivefold cross-validation. Error bars denote standard error of the mean.
	}
	\label{fig:MOFA_CLL_Cox}
\end{figure}

\subsection{Imputation of missing values}

A promising application of MOFA is the imputation of missing values, including the potential to impute of entire assays.\\
The principle of imputation in MOFA follows the same logic as simulating from the generative model: if the factors and weights are known, the input data can be reconstructed by a simple matrix multiplication:
\[
	\hat{\bfY} = \E[\bfZ] \E[\bfW]^T
\]
where $\E[\bfZ]$ and $\E[\bfW]$ denote the expected values of the variational distributions for the factors and the weights, respectively. Notice that, when using the expectations of the posterior distributions, the noise $\epsilon$ (\Cref{mofa_master_equation}) has a mean of zero and does not contribute to the predictions.\\
The equation above results in point estimates, but it ignores the uncertainity on $\bfZ$ and $\bfW$. Instead of relying in point estimates, one could adopt a more Bayesian approach and calculate the posterior predictive distribution by propagating the uncertainity \cite{Gelman2013}. Nonetheless, due to the nature of the optimisation problem in variational inference, the variance of the posterior distributions can be underestimated (see \Cref{section:expectation_propagation}). In addition, this would be substantially more complex to implement and would result in a significant increase in computational complexity, hence we did not implement this strategy.

To assess the imputation performance, we trained MOFA models using a data set of complete measurements (a total of N=121 samples) after masking parts of the drug response measurements. In a first experiment, we masked values at random, and in a second experiment we masked the entire drug response measurements. We compared the imputation accuracy of MOFA to some established imputation strategies, including imputation by feature-wise mean, SoftImpute \cite{Mazumder2010}, and a k-nearest neighbour method \cite{Troyanskaya2001}. For both imputation tasks, MOFA consistently yielded more accurate predictions, albeit the differences are less pronounced in the imputation of full assays, a significantly more challenging task.\\

\begin{figure}[H]
	\centering
	\includegraphics[width=0.9\textwidth]{MOFA_imputation}
	\caption{\textbf{Evaluation of imputation performance in the drug response assay.}\\
	The y-axis shows the mean-squared error (MSE) across 15 trials for increasing fractions of missing data (x-axis). Two experiments were considered: (a) values missing at random and (b) entire assays missing at random. Each point displays the mean across all trials and the error bars depict the corresponding standard deviations.}
	\label{fig:MOFA_imputation}
\end{figure}

\newpage

\section{Application to single-cell multi-omics} \label{section:mofa_scmt}

The emergence of single-cell multi-modal techniques has created opportunities for the development of novel computational strategies \cite{Stuart2019,Colome-Tatche2018,Chappell2018}.\\
To show case how MOFA can be used to integrate single-cell multi-omics data, we considered a simple data set that consists of 87 ESCs where RNA expression and DNA methylation were simultaneously measured using scM\&T-seq\cite{Angermueller2016}. Two populations of ESCs were profiled: the first one contains 16 cells grown in 2i media, which is known to induce a naive pluripotency state associated with genome-wide DNA hypomethylation \cite{Ficz2013}. The second population contains 71 cells grown in serum media, which contain stimuli that trigger a primed pluripotency state poised for differentiation \cite{Tosolini2016}.

\subsection{Data processing}

The RNA expression data was processed using \textit{scran}\cite{Lun2016b} to obtain log normalised counts adjusted by library size. Feature selection was performed by selecting the top 5,000 most overdispersed genes\cite{Lun2016a}. A Gaussian likelihod was used for this data modality. \\
The DNA methylation data was processed as described in Chapter 2. Briefly, for each CpG site, we calculated a binary methylation rate from the ratio of methylated read counts to total read counts. Next, CpG sites were classified by overlapping with genomic contexts, namely promoters, CpG islands and enhancers (distal H3K27ac peaks). Finally, for each annotation we selected the top 5,000 most variable CpG sites with a minimum coverage of 10\% across cells. Each of the resulting matrices was defined as a separate view for MOFA. A Bernoulli likelihod was used for this data modality.

\subsection{Model overview}

In this data set, MOFA inferred 3 factors with a minimum explained variance of 1\% (\Cref{fig:mofa_scMT}). Factor 1 captured the transition from naive to primed pluripotent states, which MOFA links to widespread coordinated changes between DNA methylation and RNA expression. Inspection of the gene weights for Factor 1 pinpoints important pluripotency markers including  \textit{Rex1/Zpf42} or \textit{Essrb} \cite{Mohammed2017}. As previously described both \textit{in vitro} \cite{Angermueller2016} and \textit{in vivo} \cite{Auclair2014}, the transition from naive to primed pluripotency state is concomitant with a genome-wide increase in DNA methylation levels. Factor 2 captured a second dimension of heterogeneity driven by the transition from a primed pluripotency state to a differentiated state, with RNA weights enriched with canonical differentiation markers including keratins and annexins \cite{Fuchs1988}.\\
Jointly, the combination of Factors 1 and 2 reconstruct the coordinated changes between the transcriptome and the epigenome along the differentiation trajectory from naive pluripotent cells to differentiated cells.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.9\textwidth]{MOFA_scMT}
	\caption{\textbf{MOFA recovers a differentiation process from a single-cell multi-omics data set.} \\
	(a) Overview of the data modalities. Rows indicate number of features ($D$) and columns indicate number of samples ($N$). Grey bars denote missing samples.\\
	(b) Fraction of variance explained per factor (column) and view (row).\\
	(c) Cumulative fraction of variance explained per view (across all factors).\\
	(d) mRNA weights of Factor 1 (bottom) and Factor 2 (top). The genes that are labelled are known markers of pluripotency (for Factor 1) or differentiation (for Factor 2). \\
	(e) Scatter plot of Factor 1 (x-axis) against Factor 2 (y-axis). Cells are colored based on the culture condition. Grey arrow illustrates the differentiation trajectory from a naive pluripotency state to a differentiated state.
	}
	\label{fig:mofa_scMT}
\end{figure}

% \begin{figure}[H]
% 	\centering
% 	\includegraphics[width=0.8\textwidth]{MOFA_scMT2}
% 	\caption{XX}
% 	\label{fig:MOFA_scMT2}
% \end{figure}

% \subsubsection{Comparison with clustering strategies}

% To illustrate the importance of learning continuous latent spaces before clustering samples, we applied popular integrative clustering algorithms \cite{Wang2014,Shen2009,Mo2013} to the data set. As expected, two clusters can be recovered that broadly match the culture conditions. However, no trajectory is recovered, illustrating the importance of

% \begin{figure}[H]
% 	\centering
% 	\includegraphics[width=1.0\textwidth]{MOFA_scMT_clustering}
% 	\caption{\textbf{Multi-omics clustering applied to scMT data set.}\\
% 	(a) Similarity matrix and dendogram obtained using Similarity Network Fusion\cite{Wang2014}\\
% 	(b) Dendrogram obtained using iClusterPlus\cite{Mo2013} with two clusters.
% 	}
% 	\label{fig:MOFA_scMT_clustering}
% \end{figure}


\chapter{String}

\section{Palindrome}
\subsection{Palindrome anagram}
\runinhead{Test palindrome anagram.} Char counter, number of odd count should $\leq 0$.
\runinhead{Count palindrome anagram.} See Section-\ref{N_objects_K_types}.
\runinhead{Construct palindrome anagram.} Construct all palindrome anagrams given a string \pyinline{s}.
\\
Clues:
\begin{enumerate}
\item dfs, grow the counter map of \pyinline{s}.
\item jump parent char
\end{enumerate}
Code:
\begin{python}
def grow(self, s, count_map, pi, cur, ret):
  if len(cur) == len(s):
    ret.append(cur)
    return

  for k in count_map.keys():
    if k != pi and count_map[k] > 0:
      # jump the parent
      for i in xrange(1, count_map[k]/2+1):
        count_map[k] -= i*2
        self.grow(s, count_map, k, k*i+cur+k*i, ret)
        count_map[k] += i*2
\end{python}

Jump within the looping to avoid repetition.

\section{KMP}
Find string $W$ in string $S$ within complexity of $O(|W|+|S|)$. KMP - reflect upon yourself before judging others.
\subsection{Prefix suffix table}
Partial match table (also known as "failure function"). After a failure matching, you know that the matched suffix before the failure point is already matched; therefore when you shift the $W$, you only need to shift the prefix onto the position of the previous suffix. The prefix and suffix must be proper prefix and suffix.

\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[scale=1.30]{kmp_table}}
\caption{Prefix-suffix table}
\label{fig:kmp_table}
\end{figure}
In table-building algorithm, similar to dp, let $T[i]$ store the length of matched prefix suffix for $needle[:i]$\\
\\
Clues:
\begin{enumerate}
\item dummy at $T[0]=-1$.
\item three parts
\begin{enumerate}
\item matched
\item fall back (consider $ABABC...ABABA$)
\item restart
\end{enumerate}
\end{enumerate}
Table-building code:
\begin{python}
# construct T
T = [0 for _ in xrange(len(needle)+1)]
T[0] = -1
T[1] = 0

cnd = 0  # candidate
i = 2  # table index
while i < len(needle)+1:
    if needle[i-1] == needle[cnd]:  # matched
        T[i] = cnd+1
        cnd += 1
        i += 1
    elif T[cnd] != -1:  # fall back
        cnd = T[cnd]
    else:  # restart
        T[i] = 0
        cnd = 0
        i += 1
\end{python}

\pythoninline{T[cnd]} is the length, thus just the next index to be processed in the next loop.
\subsection{Searching algorithm}
\begin{figure}[F]
\centering
\subfloat{\includegraphics[scale=1.30]{kmp_presuffix}}
\caption{KMP example}
\label{fig:kmp_presuffix}
\end{figure}

Notice:
\begin{enumerate}
\item index $i$ and $j$.
\item $T[i-1+1]$ for corresponding previous index in $T$ for current scanning index $i$.
\item When falling back, the next scanning index is \pyinline{len(prefix)}
\item three parts:
\begin{enumerate}
\item matched
\item aggressive move and fall back
\item restart
\end{enumerate}
\end{enumerate}
Search code:
\begin{python}
# search
i = 0  # index for needle
j = 0  # index for haystack
while j+i < len(haystack):
    if needle[i] == haystack[j+i]:  # matched
        i += 1
        if i == len(needle):
            return haystack[j:]
    else:
        if T[i] != -1:  # move and fall back j
            j = j+i-T[i]
            i = T[i]
        else:  # restart
            j += 1
            i = 0

return None
\end{python}
\subsection{Applications}
\begin{enumerate}
\item Find needle in haystack.
\item Shortest palindrome
\end{enumerate}


\section{Free selective functors}\label{sec-free}

Free construction with examples

The methodology of building effectful computations with free constructions such
as free~\cite{free-monads} and freer~\cite{freer-monads} monads and free
applicatives~\cite{free-applicatives} is a widespread in the functional programming community.
It allows to focus on the internal aspects of the effect under consideration and receive the
desired \hs{Applicative} of \hs{Monadic} structure of the computation~\emph{for free},
i.e. without the need to construct instances or prove laws.

In the ``free structures'' methodology, the essence of an effect is a datatype which encodes
the ``commands'' which the effect provides, acting as a deep embedding of the effect's
interface. This datatype must only have enough structure to be a~\hs{Functor}. The purpose of
the free constructions is then to build on top of this functor a richer structure,
which would have the instances of \hs{Applicative}/\hs{Selective}/\hs{Monad}.

\subsection{Free construction}\label{sec-free-construction}

...

\subsection{Ping-pong, freely}\label{sec-free-ping-pong}

To illustrate how the free selective construction can be used, we implement the
classical example of the Teletype DSL.

The \hs{TeletypeF} datatype has two constructors, representing the commands of the
Teletype interface:

\begin{minted}[xleftmargin=10pt]{haskell}
data Teletype a = GetLine (String -> a)
                | PutStrLn String a
    deriving Functor
\end{minted}

We embed these commands into the free selective with the following two combinators,
mimicking Haskell Prelude's IO API:

\begin{minted}[xleftmargin=10pt]{haskell}
getLine :: Select Teletype String
getLine = liftSelect (GetLine id)

putStrLn:: Select Teletype ()
putStrLn s = liftSelect (PutStrLn s ())
\end{minted}

By reimplement the \hs{pingPongS} example from the section \ref{sec-intro}
in terms of the free selective construction:

\begin{minted}[xleftmargin=10pt]{haskell}
pingPongS :: Select Teletype ()
pingPongS = whenS (fmap (=="ping") getLine) (putStrLn "pong")
\end{minted}

Once we have embedded the \hs{pingPongS} program into the free selective datatype,
we have access to the machinery allowing for static analysis of its effects:

\begin{minted}[xleftmargin=10pt]{haskell}
ghci> getEffects pingPong
[GetLine,PutStrLn pong]
\end{minted}

The \hs{getEffects} function of type \hs{Functor f => Select f a -> [f ()]}
returns a list of all effects of a free selective computations. In the specific case of
the Teletype functor, we get a list of all commands that a computations has called.
Internally, the \hs{getEffects} function interprets a free selective computation
in the \hs{Over} functor (see section~\ref{sec-instances}).

We can interpret Teletype programs in any other \hs{Selective} by means of the
\hs{runSelect} function, by providing a \emph{natural transformation} \hs{forall a. f a -> g a}, which gives an interpretation of the commands of
\hs{f} (specifically, \hs{Teletype}) in terms of \hs{g}. A natural example of such a
transformation would be an interpretation in the \hs{IO} monad:

\begin{minted}[xleftmargin=10pt]{haskell}
inIO :: Teletype a -> IO a
inIO (GetLine t)    = t <$> Prelude.getLine
inIO (PutStrLn s x) = Prelude.putStrLn s *> pure x
\end{minted}

\subsection{Build systems, freely}\label{sec-free-build}

...

\subsection{Analysis and simulation of processor instructions}\label{sec-free-isa}

In this section, we demonstrate how we can use free selective functors to construct an
effect which can be used for effectively describing the semantics of a hypothetical
instruction set architecture. The features of free selective functors will allow for
multiple distinct interpretations of the same semantics, such as~\emph{static} dependency
analysis and~\emph{dynamic} simulation.

\subsubsection{Embedding}

We will represent the semantics of instruction in terms of the following datatype:

\begin{minted}[xleftmargin=10pt]{haskell}
type ISA a = Select RW a
\end{minted}

Here, \hs{Select} is the free selective functor defined earlier in this section.
We apply the \hs{Select} type constructor to the \hs{RW} datatype, which is the
functor we build our free construction on:

\begin{minted}[xleftmargin=10pt]{haskell}
data RW k v a = R k             (v -> a)
              | W k (ISA k v v) (v -> a)
    deriving Functor
\end{minted}

The effect we require comprises two commands. We need to have an ability to (1)
\emph{read} a value associated with a key and, (2) given a computation which produces a value,
\emph{write} its result into the store. Here, the second argument of the \hs{W} constructor
This exact structure of the definition is required for accommodating a pattern that
frequently occurs in instruction semantics: often we read a value from a location
(register/memory), do something with the value and then write it into a different location.
If we had the type of \hs{W} to be \hs{k -> v -> (v -> a)}, i.e. required the value to be pure,
we would not be able to get away from using monadic bind/join. Additionally, we want the write
operation to not just write the value and return \hs{()}, but to return the just written value
back, so it somehow used in the context; such a generosity of the write command not consuming
its arguments will be useful to avoid creating more data dependencies than necessary.

We introduce two convenience combinators, which \emph{lift} the data constructors
of the \hs{RW} datatype into the free selective, thus making them directly usable in
the definitions of instruction semantics:

\begin{minted}[xleftmargin=10pt]{haskell}
read :: Location -> ISA Value
read k = liftSelect (R k id)

write :: Location -> ISA Value -> ISA Value
write k p = p *> liftSelect (W k p id)
\end{minted}

Whereas the \hs{read} combinator is exactly the lifted \hs{R} data constructor, the \hs{write}'s implementation deserves attention, since it deviates from the trivial lifting of
the \hs{W} data constructor. It evaluates its second argument, thus executing its
associated effects.

\subsubsection{Example 1. Addition}

To get acquainted with the proposed methodology, we start with a simple semantics for
the addition instruction, which will read the summands from the two locations, add them,
write the result into the third location and also update the state of the \hs{zero}
flag to indicate if the sum was zero:

\begin{minted}[xleftmargin=10pt]{haskell}
add :: Location -> Location -> Location -> ISA Value
add var1 var2 dest =
    let arg1     = read var1
        arg2     = read var2
        sum      = (+)  <$> arg1   <*> arg2
        isZero   = (==) <$> pure 0 <*> write dest sum
        overflow = willOverflowPure <$> arg1 <*> arg2
    in write "zero"     (fromBool <$> isZero) *>
       write "overflow" (fromBool <$> overflow)
\end{minted}

Here, we get two effectful values from the two locations and calculate three intermediate
results. To calculate the sum we just lift \hs{+} into the free selective using the applicative
combinators. We calculate the state of the \hs{"zero"} flag in the similar way, but here we
exploit the fact that the \hs{write} combinator returns the value it has just written, thus we
can reuse the value of the sum without recalculating it and triggering its associated effects
again. We detect integer overflow by means of a pure function, thus there is not much difference
with calculating the sum (\todo{discuss effects of \hs{willOverflow}?}).

The free selective functor construction shines in the static analysis. By executing
the analysis of the \hs{add} semantics, we can find obtain the list of all its effects:
\begin{minted}[xleftmargin=10pt]{haskell}
> analyse (add "x" "y" "z")
([],Left (W "overflow" :| [R "y",R "x",W "zero",W "z",R "y",R "x"]))
\end{minted}
If we read the list from right to left, we could see that \hs{add} read the values of the
arguments than written the destination variable then then something else \todo{write this}.

The addition instruction semantics has only used the applicative combinators and thus
the same analysis capabilities could have been implemented with free applicative functors.
However, there are important instructions whose semantics cannot be implemented in terms
of the \hs{Applicative} interface, but still do not require such heavy artillery as monads.

\subsubsection{Example 2. Conditional jump}

Selective functors allow to introduce limited dependencies between effectful computations.
It turns out, that they give just enough power to implement the semantics of conditional
jump instructions. A relative conditional jump offsets the program counter in case if a
certain condition, materialised in a microarchitectural flag, holds. For instance, some instruction set might have a jump triggered by the fact that the result of the last addition was zero

\begin{minted}[xleftmargin=10pt]{haskell}
jumpZero :: Value -> ISA ()
jumpZero offset =
    let pc       = read PC
        zeroSet  = (/=) <$> pure 0 <*> read (Flag Zero)
        modifyPC = void $ write PC (fmap (+ offset) pc)
    in whenS zeroSet modifyPC
\end{minted}

Here we use the aforementioned \hs{whenS} combinator to only execute the effect, i.e.
to modify the program counter, if the flag is set. By implementing this semantics in terms of
\hs{Selective} we achieve both the ability to implement an adequate simulator for the ISA and
to retain the possibilities for the static analysis of programs by means of the \hs{analyse} function:

\begin{minted}[xleftmargin=10pt]{haskell}
> analyse (jumpZero 42)
([],Left (Write PC :| [Read PC,Read Zero]))
\end{minted}

The \hs{analyse} function informs us of all necessary effects of the computation, thus
effectively giving us an over-approximated list of dependencies. Note that it does not matter
what argument we supply, since it will never get evaluated, e.g. the analysis will succeed and give us the same result even if we supply \hs{undefined}.

\subsubsection{Blocks of instructions}


\documentclass{beamer}
\usetheme{Warsaw}
\usepackage{nhtvslides}
\usepackage{graphicx}
\usepackage{listings}
\lstset{language=CAML,
basicstyle=\ttfamily\footnotesize,
frame=shadowbox,
breaklines=true}
\usepackage[utf8]{inputenc}
\DeclareMathOperator{\sign}{sign}
\DeclareMathOperator{\LookupCurve}{LookupCurve}

\title{Building a physics engine - part 5b: cars}

\author{Dr. Giuseppe Maggiore}

\institute{NHTV University of Applied Sciences \\
Breda, Netherlands}

\date{}

\begin{document}
\maketitle

\begin{frame}{Table of contents}
\tableofcontents
\end{frame}

\section{Basic linear dynamics}
\begin{slide}{Basic linear dynamics}{Basic linear dynamics}{
\item Some easy assumptions for starters
\item No gears, lateral forces, etc.
\item Sports car with rear traction
}\end{slide}

\begin{slide}{Basic linear dynamics}{Basic linear dynamics}{
\item Longitudinal force
\begin{eqnarray}
F_{\text{traction}} &=& u F_{\text{engine}} \\
F_{\text{drag}} &=& - C_{\text{drag}} v |v| \\
F_{\text{rr}} &=& - C_{\text{rr}} v \\
F_{\text{long}} &=& F_{\text{traction}} + F_{\text{drag}} + F_{\text{rr}}
\end{eqnarray}
\begin{itemize}
\item $u$ is forward direction
\item $C_{\text{drag}} = 0.4257$
\item $C_{\text{rr}} = 12.8$
\end{itemize}
}\end{slide}

\begin{slide}{Basic linear dynamics}{Basic linear dynamics}{
\item Braking force
\begin{eqnarray}
F_{\text{brake}} &=& -u C_{\text{brake}} \\
F_{\text{long}} &=& F_{\text{brake}} + F_{\text{drag}} + F_{\text{rr}}
\end{eqnarray}
\begin{itemize}
\item $C_{\text{brake}}$ is a constant that \textit{just feels good}
\end{itemize}
}\end{slide}

\section{Weight transfer}
\begin{slide}{Weight transfer}{Weight transfer}{
\item Acceleration causes a pitch of the car
\item It shuffles weight between front and rear
\item Tires with more or less friction, and thus capacity to support acceleration
\item $F_{\text{max}} = \mu W_w$ for a wheel carrying weight $W_w$
\item $\mu \in [1 \dots 1.5]$
}\end{slide}

\begin{frame}{Weight transfer}
\center
\includegraphics[height=5cm]{Pics/WeightTransfer.png}
\end{frame}

\begin{slide}{Weight transfer}{Weight transfer}{
\item When the car is at rest
\begin{eqnarray}
W_f &=& \frac{c}{l}W \\
W_r &=& \frac{b}{l}W
\end{eqnarray}
\item When the car is accelerating
\begin{eqnarray}
W_f &=& \frac{c}{l}W - \frac{h}{l}ma \\
W_r &=& \frac{b}{l}W + \frac{h}{l}ma
\end{eqnarray}
}\end{slide}

\begin{slide}{Weight transfer}{Weight transfer}{
\item When accelerating, pitch the car
\item If the force applied by the engine to the wheels is bigger than $F_{\text{max}}$, reduce $\mu$ and apply $F_{\text{max}}$, and draw smoke/spinning wheels
\item If the force applied by the engine to the wheels is less than $F_{\text{max}}$, apply the engine force directly
}\end{slide}

\section{Engine force}
\begin{slide}{Engine force}{Engine force}{
\item The engine is not directly connected to the wheels
\item Gears apply the engine torque to different values of max wheel $\tau$ and max wheel $\omega$
\item Lower gears have higher $\tau$
\item Higher gears have higher $\omega$
}\end{slide}

\begin{slide}{Engine force}{Engine force}{
\item $F_{\text{drive}} = u \frac{\tau_{\text{drive}}}{R_w}$ is the force applied to the rear axle
\item $\tau_{\text{drive}} = \tau_{\text{engine}} x_g x_d n$ is the torque applied to the rear axle
\item $\tau_{\text{engine}}$ is the torque coming from the engine given the current RPM
\item $x_g$ is the gear ratio, $x_d$ is the differential ratio
\item $m=1500kg$ is the car mass
\item $n=0.7$ is the transmission efficiency
\item $r_w = 0.34m$ is the wheels radius
}\end{slide}

\begin{slide}{Engine force}{Gear ratios}{
\item $x_g = 2.66\ 1.78\ 1.3\ 1.0\ 0.74\ 0.5$
\item reverse gear $ = 2.9$
\item $x_d = 3.42$
}\end{slide}

\begin{slide}{Engine force}{Torque and RPM}{
\item $rpm$ determines the current maximum torque
\item torque accelerates the wheels
\item wheels determine the next $rpm$
\item $rpm$ is capped; after a while (\textit{red-line}) the engine breaks
\item $\tau_{\text{max}}$ is capped as well; one cap per gear
}\end{slide}

\begin{slide}{Engine force}{Torque and RPM}{
\item Torque and RPM recurrences
\begin{eqnarray}
\tau_{\text{max}} &=& \LookupCurve(rpm) \\
\tau_{\text{engine}} &=& \tau_{\text{max}} \alpha_{\text{throttle}} \\
rpm &=& \max(1000, \frac{\omega_w x_g x_d}{2 \pi})
\end{eqnarray}
}\end{slide}

\begin{slide}{Engine force}{Wheel angular velocity}{
\item For $\LookupCurve$, any reasonable bell-shaped curve (different for each gear) will do
\item Or, copy from the sources of \textit{Marco Monster's - Car Physics for Games} tutorial; they contain some data
}\end{slide}

\begin{frame}{Gear plot}
\center
\includegraphics[height=5cm]{Pics/GearPlot.png}
\end{frame}

\begin{slide}{Engine force}{Shifting gears}{
\item RPM changes suddenly when changing gear $rpm' = rpm \frac{x_g'}{x_g}$
}\end{slide}

\begin{slide}{Engine force}{Wheel angular velocity}{
\item Simple solution vs hard solution
\item Simple solution: wheels rotating as car is moving
$$\omega_w \approx \frac{|v|}{r_w}$$
\item Hard solution: track wheel angular velocities separately
}\end{slide}

\section{Slip ratio}
\begin{slide}{Slip ratio}{Slip ratio}{
\item The amount of acceleration of the car depends on the friction between tires and road
\item Rolling tires do not have friction; friction is given by tires rotating faster than they are moving
\item Rear tires roll faster than front tires
}\end{slide}

\begin{frame}{Longitudinal force}
\center
\includegraphics[height=3cm]{Pics/SlipRatioCurve.png}
\end{frame}

\begin{slide}{Slip ratio}{Slip ratio}{
\item Slip ratio determines the force given by the wheel to the car
$\sigma = \frac{\omega_w r_w - v_{\text{long}}}{|v_{\text{long}}|}$
\item The traction force given by the wheel at a certain slip ratio is
$F_{\text{traction}} = \max(6000, C_t \sigma)$
$\tau_{\text{traction}} = F_{\text{traction}} \times R_w$
}\end{slide}

\begin{frame}{Longitudinal force simplified}
\center
\includegraphics[height=3cm]{Pics/SlipRatioCurveApprox.png}
\end{frame}

\begin{slide}{Slip ratio}{Slip ratio}{
\item We track $\omega_w$ for each wheel
\item We compute the slip ratio and the corresponding torque on the axle
$\tau_{\text{total}} = \tau_{\text{drive}} + \underbrace{\tau_{\text{traction}}}_{\text{two wheels}} + \tau_{\text{brake}}$
\item We compute the angular acceleration of this force on the wheel
$\alpha = \frac{\tau_{\text{total}}}{I_w}$
\item The wheel rotating around its central axis has moment of inertia
$I_w = \frac{m r_w^2}{2}$
}\end{slide}

\section{Curves at a low speed}
\begin{slide}{Curves at a low speed}{Curves at a low speed}{
\item When travelling at low speed
\item We just find the radius of the circle the car describes, depending on the wheel angle
\item $\delta$ is the wheel turn angle
\item $\sin \delta = \frac{L}{R}$
\item From the radius we can determine the angular velocity and just rotate the car by that
$\omega = \frac{v}{R} = \frac{v \sin \delta}{L}$
}\end{slide}

\begin{frame}{Rotation radius}
\center
\includegraphics[height=3cm]{Pics/RotationRadius.png}
\end{frame}

\begin{frame}{Delta angle}
\center
\includegraphics[height=3cm]{Pics/DeltaAngle.png}
\end{frame}

\section{Curves at a high speed}
\begin{slide}{Curves at a high speed}{Curves at a high speed}{
\item Turning the front wheels causes a change in their lateral forces
\item We add new state information to our system
\begin{itemize}
\item $\alpha$ is the side-slip angle of the wheel, which changes as we turn
\item $C_a$ is the cornering stiffness, a pleasant, and utterly fake, constant
\end{itemize}
}\end{slide}

\begin{slide}{Curves at a high speed}{Curves at a high speed}{
\item We compute the lateral and longitudinal speed at a given side-slip angle, for each wheel
\begin{eqnarray}
v_{\text{lat}} &=& |v| \sin \alpha \\
v_{\text{long}} &=& |v| \cos \alpha
\end{eqnarray}
}\end{slide}

\begin{slide}{Curves at a high speed}{Curves at a high speed}{
\item We also compute the side-slip angles given the current angular velocity (started up from low-speed turning) and lateral and longitudinal velocities
\begin{eqnarray}
\alpha_{\text{front}} &=& \frac{v_{\text{lat}} + \omega b}{v_{\text{long}}} - \delta \sign(v_{\text{long}}) \\
\alpha_{\text{rear}} &=& \frac{v_{\text{lat}} - \omega b}{v_{\text{long}}}
\end{eqnarray}
}\end{slide}

\begin{slide}{Curves at a high speed}{Lateral forces}{
\item Lateral force also depends on the current weight distribution
\item $F_{\text{lateral}} = \max(6000, C_a \alpha) W_w$
\item $\tau_{\text{lateral}} = F_{\text{lateral}} \times b$
\item Each wheel has a different lateral force; we compute torque from lateral forces and use it as usual to further integrate $\omega$
}\end{slide}

\section{Assignment}
\begin{slide}{Assignment}{Assignment}{
\item Before the end of next week
\item Group-work archive/video on Natschool or uploaded somewhere else and linked in your report
\item Individual report by each of you on Natschool
\item Add a personalized selection of forces to your simulator
}\end{slide}

\begin{frame}{That's it}
\center
\fontsize{18pt}{7.2}\selectfont
Thank you!
\end{frame}

\end{document}


\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{amsmath, amsthm, amssymb, latexsym}
\usepackage{enumerate}
\usepackage{color}
\newtheorem*{defn}{Definition}
\begin{document}
\title{Mid-semester report\\
Interactive Visualizations in Mathematica
\\
Spring 2021}
\author{Faculty Mentor: A.J. Hildebrand \\
    Project Leader: Efstathios Konstantinos Chrontsios Garitsis \\
	IGL Scholars: Xiaojun Jia, Adithya Swaminathan, \\
	Dimitrios Tambakos, Troy Yang, Sarah Zimmerman}
	\date{March 22, 2021}
\maketitle
\section{Project Goals}
We split into two subgroups in order to create fractal-like visualizations derived by different aspects of math. The first subgroup concentrates on  Julia sets of complex cubic \mbox{polynomials} and tries to identify patterns within their fractal-like visualizations while the coefficients of the polynomials change. The second subgroup is investigating the behavior of fractals constructed from sum-of-digit functions.

\section{Julia Sets}
Let $P: \mathbb{C} \rightarrow \mathbb{C}$ be a complex polynomial and ${P^k}$ denote the k-th iterate of ${P}$, i.e., $P^k = P \circ P \circ \dots \circ P$  k times.

\begin{defn}
The \textbf{Julia Set} is defined to be the boundary of the set of all points ${z \in \mathbb{C}}$ for which the set $\{P^{k}(z) : k \in \mathbb{N}\}$ is bounded.
\end{defn}

Our research is focused on how the Julia Sets of polynomials change while the \mbox{coefficients} change. This has been extensively studied for polynomials of the form ${z^2 + c}$ and many \mbox{visualizations} of those Julia sets have been created. We focus on polynomials of the form ${z^3 + az^2 + \lambda z}$ where ${a}$ is a complex number and ${\lambda = e^{2 \pi i \theta}}$ where ${\theta}$ is an irrational number.\\

When $a$ varies among integers and $\theta$ is constant, we noticed that graphs of Julia sets have central symmetry when $a_1=-a_2$. For example, when $\theta = \sqrt{2}$ and $a\in [-2,2] \cap \mathbb{Z}$, the graphs for $a_1 = -1$ and $a_2 = 1$ are central symmetric.
\newpage
\begin{center}
      	~\\$a_1 = -1$ \hspace{2.15in}$a_2 = 1$
        ~\\\includegraphics[
        height=1in,
        width=2in]{sqrt_2_a=-1.PNG}
        \hspace{0.6in}
        \includegraphics[
        height=1in,
        width=2in]{sqrt_2_a=1.PNG}
\end{center}

However, we also came across instances where the graphs of the Julia set do not have central symmetry. For example, fix $\theta = \pi^\pi$ and let $a \in \{0.59+1.5xi \mid x \in \mathbb{Z}\}$. The graphs for $x=-1$ and $x=1$  lose central symmetry, as shown below:
\begin{center}
    ~\\$a=0.59-1.5i$ \hspace{1.25in}$a=0.59+1.5i$
    ~\\\includegraphics[width=1.25in,height=1.25in]{pi2pi1.png}
    \hspace{1in}
    \includegraphics[width=1.25in,height=1.25in]{pi2pi3.png}
\end{center}

Last but not least, we are studying the possibility of patterns of the Julia set when ${a}$ is fixed and ${\theta}$ changes among irrational numbers.

\section{Sum-of-Digit Fractals}
Let  $s_{b}(n)$ denote the sum of the digits of a number $n$ in base $b$.
For example, in base $b=10$ we have
$s_{10}(15) = 1 + 5 = 6$, while in base $b=2$,
 $s_{2}(15) =  1 + 1 + 1+ 1 = 4$ since $15$ has base $2$ representation $1111$.
 We consider the exponential sum
\[ S_{b, p}(N) = \sum_{n=1}^{N}{e^{2\pi i\, s_{b}(n)/p}},
\]
where $s_b(n)$ is the sum-of-digit function and $p$ is a parameter.
Since the terms $e^{2\pi i s_b(n)/p}$ in $S_{b,p}(n)$ are complex numbers, we can interpret them as vectors in the $xy$-plane given by:
\[
e^{2\pi i s_b(n)/p}=(\cos(2\pi  s_{b}(n)/p), \sin(2\pi s_{b}(n)/p))
\]

\begin{defn}Given a base $b$ and a parameter $p$, we define the associated \textbf{sum-of-digit fractal} to be the  curve whose steps are given by the terms of $S_{b,p}(N)$.
\end{defn}

%Figure \ref{fig:five-steps} shows the first 5 steps of the sum-of-digit fractal with $b=2$ and $p=7$.
In the case where $b \equiv 1 \pmod{p}$, we can show that the corresponding fractal is given by a regular $p$-gon, as illustrated in Figure \ref{fig:p-gons}.

For the case where $b \equiv 0 \pmod{p}$, we can obtain a set of $p$ overlapping $p$-gons that share a common vertex in the center, as seen in Figure \ref{fig:multiple-p-gons}.
% , given by Figure \ref{fig:multiple-p-gons}.

% \begin{figure}[htb!]
%     \begin{center}
%         \includegraphics[
%         height=0.25\textheight,
%         width=0.5\textwidth]{arrows_w_axes.png}
%         \end{center}

%         \caption{The first 5 steps of the sum-of-digit fractal $S_{2,7}(N)$.}
%         \label{fig:five-steps}
% \end{figure}

\begin{figure}[htb!]

    \begin{center}
    \includegraphics[width=2in,height=1.75in]{pentagon.png}
    \hspace{0.2in} \includegraphics[width=2in,height=1.75in]{hexagon.png}
    \end{center}
    \caption{Sum-of-digit fractals for $p = 5, b = 6$ (left), and $p = 6, b = 7$ (right).}
    \label{fig:p-gons}
    \end{figure}

\begin{figure}[htb!]
    \begin{center}
    \includegraphics[width=2in,height=1.75in]{bequalsn3.png}
    \hspace{0.2in} \includegraphics[width=2in,height=1.75in]{bequalsn4.png}
    \\
    \includegraphics[width=2in,height=1.75in]{bequalsn5.png}
    \hspace{0.2in} \includegraphics[width=2in,height=1.75in]{bequalsn6.png}
    \end{center}
    \caption{Sum-of-digit fractals for $p = 3, b = 3$ (top left), $p = 4, b = 4$ (top right),
    $p = 5, b = 5$ (lower left), and $p = 6, b = 6$ (lower right).}
    \label{fig:multiple-p-gons}
\end{figure}

\clearpage
For irrational values of $p$, the sum-of-digit fractals can take a great (and largely unpredictable) variety of different shapes  as illustrated in Figure \ref{fig:irrational}.
\begin{figure}[htb!]
    \begin{center}
    \includegraphics[width=2in,height=1.75in]{sqrt7_irrational.png}
    \hspace{0.2in} \includegraphics[width=2in,height=1.75in]{logpi.png}
    \\
    \includegraphics[width=2in,height=1.75in]{base5_8piover3.png}
    \hspace{0.2in} \includegraphics[width=2in,height=1.75in]{logsqrtpi.png}
    \end{center}
    \caption{Sum-of-digit fractals irrational for $p$ values: $n = 1000$, $p = 2\sqrt{6}$, $b = 7$  (top left), $n = 1000$, $p = \ln(\pi)$, $b = 8$ (top right), $n = 500$, $p = 8\pi/3$, $b = 5$ (bottom left), $n = 1000$, $p = \ln(\sqrt{\pi})$, $b = 10$ (bottom right).}
    \label{fig:irrational}
\end{figure}

\section{Next Steps}

In the coming weeks, we plan to refine the code for these visualizations, add more functionality, and create interactive animations. We also hope to prove or explain some of the observed behavior.

\end{document}