\documentclass{article}

\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{fullpage}
\usepackage{parskip}
\usepackage{tikz}
\usetikzlibrary{calc, shapes, patterns}

\begin{document}

\section*{\centering{Moran process}}
\subsection*{\centering{Fitness}}

\[
N=3
\text{ and }
A =
\begin{pmatrix}
    0 & 3 \\
    1 & 2
\end{pmatrix}
\]

\vspace{1cm}

\begin{center}
    \begin{tabular}{r|c|c}
        \toprule
                         & \(f(\text{Hawk})\)           & \(f(\text{Dove})\) \\
        \midrule
                         &                              & \\
        1 Hawk, 2 Doves  & \(0\times 0  + 3\times 2\)=6 & \phantom{\(0\times 0 + 3\times 2\)=6} \\
                         &                              & \\
        \midrule
                         &                              & \\
        2 Hawks, 1 Dove  &                              & \\
                         &                              & \\
        \bottomrule
    \end{tabular}
\end{center}

\subsection*{\centering{Probabilities}}

\begin{center}
    \begin{tabular}{r|r|c|c}
        \toprule
        & Select       & Selection: Birth   & Selection: Death   \\
        \midrule
        &             &                                                                          & \\
        &Hawk         & \(\frac{f(\text{Hawk})}{f(\text{Hawk}) + 2f(\text{Dove})}=\frac{6}{12}\) & \(\frac{1}{3}\)\\
        &             &                                                                          & \\
1 Hawk, 2 Doves        &             &                                                                          & \\
        &             &                                                                          & \\
        &Dove         &                                                                          & \\
        &             &                                                                          & \\
        \midrule
        &             &                                                                          & \\
        &Hawk         &                                                                          & \\
        &             &                                                                          & \\
2 Hawks, 1 Dove        &             &                                                                          & \\
        &             &                                                                          & \\
        &Dove         &                                                                          & \\
        &             &                                                                          & \\
        \bottomrule
    \end{tabular}
\end{center}

\newpage
\section*{\centering{Simulation}}

Use the appropriate dice to simulate 1 Hawk taking over a population of Doves.

Decide what dice you will use to sample birth/death selection at all possible
states:

\begin{center}
    \begin{tabular}{r|c|c|c|c}
        \toprule
        State & Birth: dice used & Select Hawk values   & Death: dice used &
        Select Hawk values \\
        \midrule
                        &   &                 &   & \\
        1 Hawk          & 6 & \(\{1, 2, 3\}\) & 6 & \(\{1, 2\}\) \\
                        &   &                 &   & \\
        \midrule
                        &   &                 &   & \\
        2 Hawks         &   &                 &   & \\
                        &   &                 &   & \\
        \bottomrule
    \end{tabular}
\end{center}

\subsection*{\centering{Example}}

\begin{center}
    \begin{tabular}{r|c|c|c|c|c}
        \toprule
        State & Birth: dice used & Birth: value rolled & Death: dice used & Death: value rolled & Next state   \\
        \midrule
                      &                  &              &                  &              &              \\
        1 Hawk        & 6                &     2        & 6                &             1& 1 Hawk       \\
                      &                  & (Select Hawk)&                  & (Select Hawk)&              \\

                      &                  &              &                  &              &              \\
        1 Hawk        & 6                &     3        & 6                &             5& 2 Hawks      \\
                      &                  & (Select Hawk)&                  & (Select Dove)&              \\
                      &                  &              &                  &              &              \\
        2 Hawks       & 4                &     4        & 6                &             2& 1 Hawk       \\
                      &                  & (Select Dove)&                  & (Select Hawk)&              \\
                      &                  &              &                  &              &              \\
        1 Hawk        & 6                &     4        & 6                & 1& \framebox{0 Hawks}      \\
                      &                  & (Select Dove)&                  & (Select Hawk)&              \\
        \bottomrule
    \end{tabular}
\end{center}

\subsection*{\centering{Activity}}

Every time you arrive at 0 \textbf{or} 3 Hawks:

\begin{enumerate}
    \item Stop;
    \item Circle your final state
    \item Draw a line in the table (next page);
    \item Start again.
\end{enumerate}

\newpage

\begin{center}
    \begin{tabular}{r|c|c|c|c|c}
        \toprule
        Current state & Birth: dice used & Birth: value rolled & Death: dice used & Death: value rolled & Next state   \\
        \midrule
        1 Hawk        & 6                &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
                      &                  &              &                  &              &              \\
    \end{tabular}
\end{center}

\section*{\centering{Computation}}

\begin{center}
    \begin{tikzpicture}[
        dove/.style={circle, pattern=north west lines, pattern color=blue!70, draw=blue},
        hawk/.style={circle, pattern=north east lines, pattern color=red!70, draw=red},
        ]

	\node (N1) at (1, 1) [dove] {D};
	\node (N2) at (0, 0) [dove] {D};
	\node (N3) at (2, 0) [dove] {D};

    \draw [thick, <-] ($(N1)!0.5!(N2) + (2.5, -.25)$) -- node [below] {\(p_{10}\)} ++(1, 0);

    \node (N1) at ($(N1) + (5, 0)$) [dove] {D};
    \node (N2) at ($(N2) + (5, 0)$) [dove] {D};
    \node (N3) at ($(N3) + (5, 0)$) [hawk] {H};

    \draw [thick, ->] ($(N1)!0.5!(N2) + (2.5, -.25)$) -- node [below] {\(p_{12}\)} ++(1, 0);
    \draw [thick, <-] ($(N1)!0.5!(N2) + (2.5, .25)$) -- node [above] {\(p_{21}\)} ++(1, 0);

    \node (N1) at ($(N1) + (5, 0)$) [dove] {D};
    \node (N2) at ($(N2) + (5, 0)$) [hawk] {H};
    \node (N3) at ($(N3) + (5, 0)$) [hawk] {H};

    \draw [thick, ->] ($(N1)!0.5!(N2) + (2.5, .25)$) -- node [above] {\(p_{23}\)} ++(1, 0);

    \node (N1) at ($(N1) + (5, 0)$) [hawk] {H};
    \node (N2) at ($(N2) + (5, 0)$) [hawk] {H};
    \node (N3) at ($(N3) + (5, 0)$) [hawk] {H};
    \end{tikzpicture}
\end{center}

\vspace{1cm}

Which gives:

\[
    p_{10}=\frac{6}{12}\frac{1}{3}=\frac{1}{6}\qquad
    p_{12}=\phantom{\frac{6}{12}\frac{2}{3}=\frac{1}{3}}\qquad
    p_{21}=\phantom{\frac{2}{8}\frac{2}{3}=\frac{1}{6}}\qquad
    p_{23}=\phantom{\frac{6}{8}\frac{1}{3}=\frac{1}{4}}
\]

\end{document}


\section{On voting}
We want to tap into the rich pool of established knowledge on  voting systems, to inform about our options and guide our decisions in designing suitable algorithms.

\subsection{On existing voting systems}
%TODO: specify, for which elections (2 candidates etc) the following holds
In this section we recall the basic established definitions and then reflect upon our needs. For the dApp-voting system, the term \textit{voters} does refer to the users of the dApp-store and \textit{candidates} does refer to the dApps available there. \\

\subsubsection{Commonly demanded properties}
Commonly, the three properties that a voting system should fulfill in order to be fair are anonymity, neutrality and monotonicity. For the election of one out of two possible candidates, these properties are formalized as follows:
\begin{itemize}
\item {\textbf{Anonymity:}\footnote{This notion of anonymity is unrelated to the anonymity of a user using an app.}}:	A voting system is said to be anonymous if it treats all voters equally. In other words, if any two voters trade ballots, this shouldn't change the election's outcome. \\
Concerning a dApp-voting-system, it is up for discussion in what way this property is wanted and how it should be implemented. For example, it could be sensible to have not total anonymity, but to give users who have a high reputation, which indicates their knowledge, or users who hold a large stake and therefore are likely to want the best for the platform, more voting power than others. Certainly it would establish an unwanted great inequality between users if the relation between voting power and reputation or stake was a linear one. We discuss this topic further in \textcolor{red}{TODO}. %TODO \ref{label}
\item {\textbf{Neutrality:}} A voting system is said to be neutral if it treats all candidates equally. This means that if every voter switched their vote from one candidate to another, the outcome should change accordingly. \\
This point is not about the voters. For dApp-rating, the neutrality property is about whether the voting-evaluation algorithm treats all the dApps that are applicable to partake in the voting round equally.

\item {\textbf{Monotonicity:}} A voting system is said to be monotone if it is impossible for a candidate to change from winning to losing by gaining additional votes and to change from losing to winning by losing votes without gaining others.
\end{itemize}
(see \cite{voting}, p. 3-4)

\subsubsection{Classical voting systems and their properties}
\begin{itemize}[leftmargin = 0pt]
\item {\textbf{Majority rule:}} This voting system elects the candidate who receives more than half of the votes, if such a candidate exists. If there is no such candidate, then majority rule results in a tie, with no winner elected.
\end{itemize}

\noindent $\bm{\ast}$ The so-called \textbf{May's Theorem} states that majority rule %TODO: see \ref{}
is the only voting system for two-candidate-elections that is anonymous, neutral, and monotone, and that avoids the possibility of ties. For more than two candidates this is not true, since it is not guaranteed that one of the candidates is voted for by more than half of all voters. \\
Because on the dApp-platform there will be more than two candidates, and not only a winner but a whole ranking is needed, majority rule isn't the voting system to go for.

%\cite{Handsonapproach} The Mathematics of Voting and Elections: A Hands-On Approach. Chapter One; maybe TODO: quota systems
\begin{itemize}[leftmargin = 0pt, nosep]
\item {\textbf{Plurality rule:}} One class of methods, which majority rule can be viewed as a special case of, is plurality rule. It is the voting system that elects the candidate who receives the highest number of votes, even if that number is less than half of the total number of votes cast. Here, only when two or more candidates receive the same number of votes (and more than the number of votes received by any of the other candidates), plurality method does result in a tie.
\end{itemize}
(see \cite{voting}, p. 5-20)\\ \\

\noindent Regarding a voting system for the dApp-ranking, the possibility of ties is no downside as there is no need to determine a single winner.\\ %S. 19
Rather, what is needed is a system that leads to some sort of preference order of all dApps. \\
%\noindent $\bm{\ast}$ Such preference order produced by the voting system is called {\textbf{societal preference order}} since it can be thought of as the ranking of the candidates that, according to the voting system being used, reflects the voters' will the best. \\
%n protip: avoid " in TeX
There are various systems that can be used to determine the societal preference order. One property of such systems that might sound sensible at first, is the following: \\
%\noindent $\bm{\ast}$ A voting system is said to satisfy the {\textit{majority criterion}} if whenever a candidate is ranked first by a majority of the voters, that candidate will also be ranked first in the corresponding societal preference order.
\\In the following, we give an example for why this would be no legitimate property for the dApp-voting-system. \textcolor{red}{TODO?}
\begin{itemize}[leftmargin = 0pt, nosep]
%\item \textbf{Borda count:} A voting system that does not fulfill the majority criterion is the so-called Borda count, which uses a non-trivial {\textsf{point system}} to determine the overall rankings and is often used in collegiate sports polls, for example. In an election with $n$ candidates it works as follows:
Firstly, each voter submits a ballot that contains his or her individual preference order of all the candidates.
Then points are awarded to each candidate for each ballot cast, according to the following rule:
An $m$-place rank is worth $n-m$ points (where $1\leq m \leq n$). In other words, a first-place rank is worth $n-1$ points, a second-place rank is worth $n-2$ points, and so on.
\item
Finally the candidate whose total number of points from all of the ballots is the largest is declared the winner and the corresponding societal preference order is determined by the number of points each candidate has got from largest to smallest. If there is more than one candidate with the largest number of points, a tie occurs. Also some sort of tie occurs in the societal preference order whenever candidates receive the same number of total points. These candidates then occupy indistinguishable positions in the preference order and are listed consecutively.
\end{itemize}
%(see \cite{voting}, p. 20-26) \newline

Intuitively, Borda count appears to be quite fair. However, it violates the majority criterion. \\
But for the purpose of the dApp-voting-system, using some sort of variation of the Borda count is more appropriate than demanding that the majority criterion is fulfilled:
\\For example, if an extreme situation is considered, in which a small majority of users ranks a specific dApp at the top position place while all of the other voters rank it last on their personal preference it would obviously make no sense to automatically rank this dApp first in the resulting societal preference order. Determining its rank according to the Borda count clearly seems much more legitimate. \\
Thus, a voting system for ranking dApps does not have to fulfill the majority criterion and will not use plurality method, but rather use a variant of Borda count to guarantee a sensible societal preference order. \\

Taking account of personal and societal preference orders, the three properties of fair voting systems have to be redefined for elections with more than two candidates:
\begin{itemize}
\item \textbf{Anonymity:} This property stays the same.
\item \textbf{Neutrality:} A voting system is said to be neutral if it treats all candidates equally. This means that if every voter switched the positions of two specific candidates on their personal preference orders, the positions of these two candidates in the resulting societal preference order would be switched accordingly.
\item\textbf{Monotonicity:} A voting system is said to be monotone if it is impossible for a candidate to go from winning to losing or to experience a decrease in rank on the resulting societal preference order whenever changes in favor of that candidate, but no changes in disadvantage of that candidate, occur on individual preference ballots. \\
%(see \cite{voting}, p. 26-27)
\end{itemize}
In this definition, the second and third property now are no longer about winning or losing, but about the ranks of the candidates in the societal preference order resulting from the personal preference orders. \\
\\Two additional properties that seem sensible to be demanded to be fulfilled by a voting system, is to belong to both kinds of voting systems that are characterized by the following definitions, for which the terms {\textbf{Condorcet winner} and \textbf{Condorcet loser}} are needed: \\
A candidate who would defeat every other candidate in a head-to-head election (under majority rule) is called a Condorcet winner. If a candidate would lose to every other candidate in a head-to-head contest (under majority rule), he is called a Condorcet loser.
\begin{itemize}
\item \textbf{CWC:} A voting system is said to satisfy the {\textbf{Condorcet winner criterion}} (CWC), if it always elects the Condorcet winner whenever one exists.
\item \textbf{CLC:} A voting system is said to satisfy the {\textbf{Condorcet loser criterion }} (CLC), if it always elects the Condorcet loser whenever one exists.
\end{itemize}
These definitions involve two-candidate-elections. It can be challenging to design a voting system with more than two candidates in a way that it fulfills at least one of the Condorcet criterions. The Borda count, for example, does not (since it violates majority rule). \\
%But since the Condorcet properties clearly are valuable requirements concerning a voting system, the dApp-store-voting system should fulfil them. The so-called sequential pairwise voting, which is explained in the following lines, does. A voting system we propose below, a variation of this system, turns out to do so, too.\\ %später zeigen, dass es das Kriterium erfüllt
%Sequential pairwise voting: \\
%To determine the winner and the societal preference order according to sequential pairwise voting, at first, the voters have to choose between two candidates. Majority rule is used to decide the winner. Next, the voters have to choose between the winner of the preceding contest and the next candidate. Then they have to choose between the winner of the latter contest and the next candidate and so on, whereby the winner always is decided according to majority rule. Finally, the winner from the last two-candidate-contest is declared the winner of the whole election. \\
% Though sequential pairwise voting has the benefit of always electing a Condorcet winner if one exists, it comes with great downsides. First of all, it would produce a societal preference order that doesn't have the mathematical property of transitivity and therefore would not be sensible. Furthermore, in sequential pairwise voting, an order in which new candidates are to be introduced into the head-to-head comparisons has to be specified beforehand. In elections where no Condorcet winner exists, exactly this order, called agenda, has an enormous influence on who wins the election. In other words, if there is no Condorcet winner, this system is highly manipulable and violates the fundamental property of neutrality, which is why sequential pairwise voting clearly can't be said to be a fair method. \\
Another criterion that can be considered related to the assessment of the utility and sensibility of a voting system is the so-called "Independence of Irrelevant Alternatives" - criterion, defined as follows: \\
A voting system is said to satisfy the irrelevant alternatives criterion (IIA) if the societal preference between any two candidates is only dependent on the individual voters' preferences between those two candidates (and not affected by the candidacy of any other candidate). \\
However, none of the voting systems discussed so far satisfies this criterion. \\
In 1951, Kenneth arrow in the context of his Ph.D.-thesis published the commonly called "Arrow's impossibility theorem" which states that no voting system thathas the five desired qualities mentioned above does exist. In 1972, this discovery even earned him the Nobel Prize in economic science. Various versions of the theorem exist, but what it states, more precisely, is the following: \\
You can consider a voting system to be a rule that assigns to each possible collection of preference orders for a poll some kind of societal preference order, or more concretely, \\
we define it as a function that takes as input a collection of transitive preference orders of all the voters and produces as output a transitive societal preference order that represents the will of the electorate. \\
This definition doesn't rule out the possibility of ties, neither ties in individual preference ballots nor ties in the resulting societal preference orders. So if one defines the societal preference order of pairwise sequential voting as the winner followed by a tie of all the other candidates, even pairwise sequential voting fits in with that defintion of a voting system. \\
The mathematical conditions Arrow had in mind can be interpreted as follows:
\begin{enumerate} % from the voting book, page 68; andere Kriterien auf Wikipedia: https://en.wikipedia.org/wiki/Kenneth_Arrow, https://en.wikipedia.org/wiki/Arrow%27s_impossibility_theorem, https://plato.stanford.edu/entries/arrows-theorem/,
%TODO: Arrow's theorem http://pi.math.cornell.edu/~mec/Summer2008/anema/maystheorem.html
\item Universality: Voting systems should place no restrictions apart from transitivity on how voters can rank the candidates in an election. There should be no kind of dictatorship presetting that only specific preference orders are acceptable or are not. Every possible collection of transitive preference ballots must yield a transitive societal preference order.
\item Positive Association of Social and Individual Values: Voting systems should be monotone.
\item Independence of Irrelevant Alternatives: Voting systems should satisfy the IIA - criterion.
\item Citizen Sovereignty: Voting systems should not be imposed in any way. I.e., there should never be a pair of candidates in an election such that one of these candidates is preferred over or tied with the other in the resulting societal preference order in defiance of any vote.
\item Nondictatorship: Voting systems should not be dictatorial. I.e., there should never be a voter such that, for any pair of candidates, if that voter prefers one of the candidates over the other, then society will also have the same preference order regarding these two candidates.
\end{enumerate}
The second condition had been called monotonicity so far. The third one is the one we introduced before. The fourth condition is close to neutrality and the fifth is similar to anonymity.
The first of these conditions is one that we haven't stated before, but which we have been assuming anyways. However, according to this condition a voting system must always output a transitive societal preference order, but some potential voting systems - e.g. sequential pairwise voting - yield cyclic societal preference orders when receiving certain inputs. So one could postulate that only systems which output transitive societal preference orders are called voting systems by definition or one could handle the problem with systems of this kind by excluding inputs that yield preferential orders which are not transitive through restrictions. The latter option then in some way would violate universality. \\
What Kenneth arrow discovered is the following: For an eleciton with three or more candidates, it is impossible for a voting system to satisfy all five of the conditions above. \\
In other words, there is no "perfect" voting system for polls with more than two candidates, at least one sensible condition always will be violated. But there is a great variety of interpretations of Arrow's Theorem, what it really means and how it should be viewed in light of the search for a voting system that is truly fair.
One of these is Pareto's Unanimity condition, which replaces the conditions of Positive Association of Social and Individual Values and Citizen Sovereignty. It postulates that if there is a pair of candidates in an election such that every voter prefers the same one over the other, then that one should be ranked higher than the other in the resulting societal preference order.
Since the holding true of Unanimity implies that Positive Association of Social and Individual Values and Citizen Sovereignty also are fulfilled, one can formulate a stronger form aof Arrow's Theorem: \\
For an election with more than two candidates, a voting system can't satisfy Universality, Independence of Irrelevant Alternatives, Nondictatorship and Unanimity all at once. \\

This stronger version is easier to prove. In order to do so, the following Lemma is needed: \\

Assume having an electin with three or more candidates and a voting system that satisfies universality, IIA and unanimity. Suppose that A is a candidate in the election and that every voter ranks A either first or last on their individual preference order (whereby some can rank him first and some last, not all have to rank him the same; furthermore this assumption excludes ties between A and any other candidate). Then the societal preference order the voting system yields, must also rank A either first or last. \\

Proof: % TODO: rethink it
\begin{enumerate}
\item If A was neither ranked alone first nor last in the societal preference order, there would have to be some other candidates B and C, so that B was ranked higher than or tied with A, and C was ranked lower than or tied with A in the societal preference order.
Due to universality, transitivity holds and so B would be ranked higher than C or would be tied with him. \\

\item Furthermore, if the assumption of the Lemma holds and every voter changed the position of C above B in their personal preference order, the societal preference between A and B as well as A and C wouldn't be affected since A was ranked highest or lowest and thus the relation between A and B as well as A and C wouldn't change. Due to unanimity, we know that C would be ranked higher than B in the societal preference order then, too.

\item So we get the contradiction that (1) says that B was ranked higher than or tied with C and (2) says that C would be ranked higher than B. Clearly, both relations can't be true at once, so it's not possible that the voting system yields a societal preference order in which A is ranked neither first nor last. So the Lemma is proven.
\end{enumerate}

We will prove the strong form of Arrow's theorem by assuming that universality, IIA and unanimity hold and showing that the system can't be non-dictatorial under this assumption by finding a dictator. To do so, we assume without loss of generality that A is ranked last by all of the voters (and therefore is ranked last in the societal preference order, too). The voters are labeled $v_1, \cdots, v_n$. \\
If some of the voters changed A from last place to first place on their individual preference orders, according to the Lemma above A then is either still ranked last or ranked first. But if all of the voters changed A from last place to first place, A would definitely be ranked first in the corresponding societal preference order. \\
Therefore we know that if the voters changed their ranking of A from last to first one by one, there has to be one voter, $v_k, 1\leq k \leq n$, whose change in the individual preference order causes A to switch from last place to first place in the societal preference order. Thus, this voter is a potential dictator. \\
We show know $v_k$ controls the societal preference between any pair of candidates not including B.
Let B and C be any two candidates other than A and assume that $v_k$ prefers B over C.
If $v_k$ moved A between B and C on his individual preference order, this would change nothing in the societal preference between B and C. Neither would a movement of A to the top of their individual preference orders by $v_1, \cdots, v_{k-1}$ and a movement of A to the bottom of their individual preference orders by $v_k, \cdots, v_n$ would not either. \\
Assume that all of these three changes occur. Then B would be ranked lower than A by $v_1, \cdots, v_{k-1}$ and higher than A by $v_k, \cdots, v_n$. Since we already know that $v_k$ would have yet to change his ranking of A from bottom to top in order to yield a societal preference order in which A were ranked first instead of last, clearly B would be preferred over A in the societal preference order resulting after the three changes. With a similar argumentation, it becomes obvious that A would be preferred over C in that societal preference order. Due to transitivity, B would be preferred over C in the societal preference order. \\
Since we observed before that the three changes yield no change in the corresponding societal preference order, we now also know, that B would be preferred over C in the societal preference order corresponding to the individual rankings before the three changes. \\
 % S. 86, Q 5.14

% maybe add instant runoff (p 48-50) though it violates monotonicity and CWC
% TODO: majority criterion ergänzen

%https://en.wikipedia.org/wiki/Social_Choice_and_Individual_Values

%TODO: May's theorem

%%@Alex
%%
%% plz don't use German in this text (also comments).
%%It is harder to read for outsiders and also a problem when you want to to grammar correction.
%%
%% And I propose to do line breaks after sentences to make the diff be easier to read.
%% (At the very latest after the dot, possibly every 80 chars, even.)
%% If you do a line break without \\, it will be ignored in the formatted text anyway.


\section{Constraint Satisfaction Problems}

Outline:
\begin{itemize}
    \item A special subset of search problems
    \item State is defined by variables Xi with values from a domain D (sometimes D depends on i)
    \item Goal test is a set of constraints specifying allowable combinations of values for subsets of variables
\end{itemize}

\paragraph{Use cases}
\begin{itemize}
    \item Assignment
    \item Timetabling
    \item Hardware configuration
    \item Transportation scheduling
    \item Factory scheduling
    \item Circuit layout
    \item Fault diagnosis
\end{itemize}

\subsection{Solving constraint satisfaction problems}

\paragraph{Backtracking}

\begin{itemize}
    \item Fix an ordering for variables, and select values for variables in this order. Because assignments are commutative (e.g. assigning WA = Red, NT = Green is identical to NT = Green, WA = Red), this is valid.
    \item When selecting values for a variable, only select values that don’t conflict with any previously assigned values. If no such values exist, backtrack and return to the previous variable, changing its value.
\end{itemize}

\subsection{Filtering}

An arc $X \rightarrow Y$ if consistent if and only if for every value $x$ in X there is some $y$ in $Y$  which could be assigned without violating a constraint.

\subsubsection{Arc consistency}
\begin{itemize}
    \item Begin by storing all arcs in the constraint graph for the CSP in a queue Q. For a binary constraint $(X,Y)$, there are two arcs to add to the queue - $X \rightarrow Y$ and $Y \rightarrow X$
    \item Check the arcs in the queue for consistency:
    \begin{itemize}
        \item If one arc $X \rightarrow Y$ is not consistent for a given value $x$, remove $x$ from the domain of $X$
        \item If at least one value is removed for a variable $X_i$, add arcs of the form $X_k \rightarrow X_i$ to the queue, for all unassigned variables $X_k$ (skip duplicate arcs already in the queue.
        \item Continue until Q is empty, or the domain of some variable is empty and triggers a backtrack.
    \end{itemize}
\end{itemize}

\subsubsection{Forward checking}: whenever a value is assigned to a variable $X$, prune the domains of unassigned variables that share a constraint with $X$ that would violate the constraint
if assigned.

Forward checking is a special type of enforcing arc consistency, in which we only enforce the arcs pointing into the newly assigned variable.

\subsection{Ordering}

\paragraph{Minimum remaining values (MRV)} Choose the variable with the fewest legal options left in its domain

\paragraph{Iterative improvement}
\begin{itemize}
    \item Take an assignment with unsatisfied constraints
    \item While a goal has not been reached:
    \begin{itemize}
        \item Reassign one variable to another value. With  min-conflict heuristic: choose a value that violates the fewest constraints
    \end{itemize}
\end{itemize}

\subsection{K-Consistency}

\textbf{K-Consistency}: For each $k$ nodes, any consistent assignment to $k-1$ ca be extended to the $k^{th}$ node.

\textbf{Strong k-consistency}: also $k-1$, $k-2$, $\cdots$ $1$ consistent

strong n-consistency means we can solve without backtracking!

\subsection{Tree-Structured CSPs}

Theorem: if the constraint graph has no loops, the CSP can be solved in $O(n d^2)$ time, with $n$ the number of nodes and $d$.

\begin{itemize}
    \item Pick an arbitrary node in the constraint graph for the CSP to serve as the root of the tree (it doesn’t matter which one because basic graph theory tells us any node of a tree can serve as a root).
    \item Convert all undirected edges in the tree to directed edges that point away from the root. Then linearize (or topologically sort) the resulting directed acyclic graph
    \item Perform a backwards pass of arc consistency. Iterating from i = n down to i = 2, enforce arc consistency for all arcs $Parent(Xi) \rightarrow Xi$
    \item For the linearized CSP from above, this domain pruning will eliminate a few values, leaving us with the following
    \item Perform a forward assignment. Starting from X1 and going to Xn, assign each Xi a value consistent with that of its parent. Because we’ve enforced arc consistency on all of these arcs, no matter what value we select for any node, we know that its children will each all have at least one consistent value. Hence, this iterative assignment guarantees a correct solution, a fact which can be proven inductively without difficulty.
\end{itemize}

The tree structured algorithm can be extended to CSPs that are reasonably close to being tree-structured
with cutset conditioning. Cutset conditioning involves first finding the smallest subset of variables in a constraint graph such that their removal results in a tree (such a subset is known as a cutset for the graph).


% !TEX TS-program = pdflatexmk
\documentclass{article} % For LaTeX2e
\usepackage{nips15submit_e,times}
\usepackage{hyperref}
\usepackage{url}
%\documentstyle[nips14submit_09,times,art10]{article} % For LaTeX 2.09

\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}

\usepackage{algpseudocode,algorithm,algorithmicx}

\newtheorem{theorem}{Theorem}
\newtheorem{corollary}{Corollary}[theorem]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{example}{Example}[theorem]

\theoremstyle{definition}
\newtheorem{definition}{Definition}

\newcommand\myeq{\stackrel{\mathclap{\tiny\mbox{d}}}{=}}

\newcommand\mc{\mathcal} %calligraphic
\newcommand\ts{\mathcal} %tensor
\newcommand\mt{} %matrix
\newcommand\vt{\mathbf} %vector
\newcommand\fn{} %function
\newcommand\triple[3]{(#1 \stackrel{#2}\rightarrow #3)}
%\newcommand\triple[3]{(#1, #2, #3)}

\title{Multiple Matrices Factorisation\\ via Matching Gradients}

\author{
Dongwoo Kim
}

% The \author macro works with any number of authors. There are two commands
% used to separate the names and addresses of multiple authors: \And and \AND.
%
% Using \And between authors leaves it to \LaTeX{} to determine where to break
% the lines. Using \AND forces a linebreak at that point. So, if \LaTeX{}
% puts 3 of 4 authors names on the first line, and the last on the second
% line, try using \AND instead of \And before the third author name.

\newcommand{\fix}{\marginpar{FIX}}
\newcommand{\new}{\marginpar{NEW}}

\nipsfinalcopy % Uncomment for camera-ready version

\begin{document}

\maketitle

%\begin{abstract}
%\end{abstract}

\section{Joint Matrix Factorisation}
Let $X \in \mathbb{R}^{n_1 \times n_2}$ and $Y \in \mathbb{R}^{n_1 \times n_3}$ be two matrices where the first dimension represents the same object, i.e. user-rating matrix $X$ and user-attribute matrix $Y$. A typical collaborative filtering approach to predict the unknown entry of the first matrix is to factorise the matrix into a low-rank representation $X \approx f(UV^\top)$ where $U\in\mathbb{R}^{n_1\times k}$, $V\in \mathbb{R}^{n_2 \times k}$ and $f:\mathbb{R}^{n_1 \times n_2} \rightarrow \mathbb{R} ^{n_1 \times n_2}$ is a link function. Joint (or co-) factorisation approach tries to factorise two related matrices into some shared low-rank representations in hope that there is a common latent representation shared across different domains. For example, $X$ and $Y$ can be factorised into $X \approx UV^\top$ and $Y \approx UW^\top$ where $W \in \mathbb{R}^{n_3 \times k}$ with the identity link function. Therefore, both matrices share the same latent representation $U$ on the first dimension.
Many of the joint factorisation approaches attempts to minimise the following objective:
\begin{equation}
\label{eqn:joint_obj}
L_{XY}(X,Y,U,V,W) = L_X(X, UV^\top) + \lambda L_Y(Y, UW^\top),
\end{equation}
where $L_X$ and $L_Y$ are appropriate loss functions for matrix $X$ and $Y$, and $\lambda$ controls the relative importance of $Y$ with respect to $X$. Various gradient based algorithms are developed to minimise the objective function.

\subsection{Gradient Matching}
What I and Lexing initially thought about gradient matching is that we match the direction of gradient w.r.t $U$ on both side of losses $L_X$ and $L_Y$, so that the both side of losses are reduced together in the same direction. In the worse case without matching gradients, two gradients could be cancelled out, which makes convergence slow. Formally, we want to ensure that the two gradients are similar during the optimisation
\begin{align}
\frac{\partial L_X}{\partial U_i} \stackrel{\angle}{\approx} \frac{\partial L_Y}{\partial U_i},
\end{align}
where $\stackrel{\angle}{\approx}$ represents the similarity between angles of two vectors. Given $X$,$Y$,$V$ and $W$, however, there is no way to match these two gradients because the gradients are decided once the other quantities and losses are fixed (btw, this is why I said it's impossible). Having said that, it might be possible to adjust another quantities such as $V$ and/or $W$ in order to match these gradients. If we think about SVD, there are no unique solution for $U$ and $V$ except the singular values, therefore it might be possible to find the new $W$ that reduces the divergence in gradients while preserving the approximation $Y \approx UW^\top$ relatively the same or reduced further. Let's assume that we adjust $W$ to match the gradients, then the optimal(?)  $W^*$ would be the solution of the following objective for some positive $\epsilon$:
\begin{align}
\label{eqn:matching_obj}
\arg\min_{W^*}L_{\angle}(\frac{\partial L_X}{\partial U_i}, \frac{\partial L_Y(W^*)}{\partial U_i}) \leq \epsilon \quad \text{subject to} \quad L_Y(Y, UW^{*\top}) \leq L_Y(Y, UW_{\text{old}}^\top)
\end{align}
where $L_{\angle}$ measures the difference between two vectors, i.e. ($1 - \frac{1}{2}(\text{cosine similarity}+1)$), and $W_{\text{old}}$ is the previously estimated matrix. Or we could relax the constraint and minimise the following objective to find $W^*$:
\begin{align}
L_{\angle Y}(W^*) = \lambda_\angle L_{\angle}(\frac{\partial L_X}{\partial U_i}, \frac{\partial L_Y(W^*)}{\partial U_i}) + |L_Y(Y, UW^{*\top}) - L_Y(Y, UW_{\text{old}}^\top)|_{+},
\end{align}
where $\lambda_\angle$ represents the relative importance of the angle difference, and $|x|_+ = \max(x, 0)$ in order to ensure that the reconstruction loss will not increase. Algorithm \ref{alg:jmfgm} summarises the gradient matching approach. We first compute the gradients of $U$ and then find an optimal $W^*$ that matches the gradients. After we update $U$ and repeat this process until converge.

\subsection{Discussion}

\begin{itemize}
\item Can we proof that the matching gradients approach leads to a better solution? or converge?
\item How can we find $W^*$ in Equation \ref{eqn:matching_obj}?
\item What if we cannot find $W^*$ in Equation \ref{eqn:matching_obj}? Do we stop the optimisation?
\item Would it be better to jointly optimising $W^*$ and $V^*$ together while matching gradients?
\item How can we generalise this framework to incorporate a tensor?
\end{itemize}

\begin{algorithm}
  \caption{Joint Matrix Factorisation via Matching Gradients \label{alg:jmfgm}}
  \begin{algorithmic}
  	\Require{$X \in \mathbb{R}^{n \times n_2}$, $Y \in \mathbb{R}^{n \times n_3}$, $L_X$, $L_Y$, $L_\angle$}
    \Statex
    \Function{MatchingGradient}{$U, V, W$}
    \State Randomly initialise $U, V, W$
    \For{$t \gets 1 \textrm{ to } T$}
      \For{$i \gets 1 \textrm{ to } n$}
      	\State Compute gradient of ${\partial L_X}/{\partial U_i}$ and ${\partial L_Y}/{\partial U_i}$
        \While{$L_{\angle}({\partial L_X}/{\partial U_i}, {\partial L_Y}/{\partial U_i}) \leq \epsilon$}
        	\State update $W$ from Equation \ref{eqn:matching_obj}
        \EndWhile
        \State update $U_i = U_i - \gamma_t \nabla_{U_i} L_{XY}$
        \State update $V_i = V_i - \gamma_t \nabla_{V_i} L_{XY}$
      \EndFor
    \EndFor
      \State \Return{$U$, $V$, $W$}
    \EndFunction
  \end{algorithmic}
\end{algorithm}

\begin{algorithm}
\end{algorithm}

\section{Concrete Example}
Let us examine the framework with more concrete example. For every matrix reconstruction loss we use squared Frobenious norm, and for the angle loss we use $1 - \frac{1}{2}(\text{cosine similarity}+1)$ between two vectors.

\begin{align}
L_{XY}(X,Y,U,V,W) =& L_X(X, UV^\top) + \lambda L_Y(Y, UW^\top)\\
=& \sum_{ij} \frac{1}{2} (X_{ij} - U_i^\top V_j)^2 + \lambda \sum_{ij} \frac{1}{2} (Y_{ij} - U_i^\top W_j)^2
\end{align}
Take the gradient with respect to $U_i$
\begin{align}
\frac{\partial L_X(X, UV^\top)}{\partial U_i} &= -\sum_j^{n_2} V_j(X_{ij} - U_i^\top V_j)\\
&= - V^\top(X_i - VU_i)\\
\frac{\partial L_Y(Y, UW^\top)}{\partial U_i} &= -\sum_j^{n_3} W_j(Y_{ij} - U_i^\top W_j)\\
&= - W^\top(Y_i - WU_i)
\end{align}
Cosine similarity between two gradient vectors can be computed as follow
\begin{align}
\frac{(X_i - VU_i)^\top V W^\top(Y_i - WU_i)}{||V^\top(X_i - VU_i)||\cdot||W^\top(Y_i - WU_i)||}
\end{align}

\bibliographystyle{apalike}
\bibliography{ref}

\end{document}


\section{Preliminary Results}
\label{sec:preliminary}
\subsection{Test statistic and quantifying sensitivity}
\label{sec:sensitivity}
The CP mixing angle, \PhiTau, acting as the parameter of interest, is measured in a binned maximum likelihood (ML) fit for each CP hypothesis, using the likelihood function [arxiv:1503.07622]:
\begin{equation}
	\label{eq:binnedprofileLH}
	\mathcal{L}(\operatorname{\textbf{x}};\phi_{\tau},\boldsymbol{\theta}) =
	\prod_{i\in \operatorname{bins}} \mathcal{P}(\operatorname{\textbf{x}_i}|\mu\cdot S_i (\phi_{\tau},\boldsymbol{\theta})+B_i (\boldsymbol{\theta}))\times\prod_{j\in syst} \mathcal{G}(\theta_j^0)|\theta_j,\Delta\theta_j)
\end{equation}
$\textbf{x}$ is a special, artificial data set called the “Asimov data set” explained below[arxiv:1007.1727]. $\mu$ is the normalization factor of Higgs signal (NF H), $S_i$ and $B_i$ indicate the expectation value of signal and background events in bin $i$ respectively, therefore $\mu\cdot S_i (\phi_{\tau},\boldsymbol{\theta})+B_i (\boldsymbol{\theta})$ is the signal + background prediction. Moreover, $\boldsymbol{\theta}$ is a set of the nuisance parameter (NP). NPs following Gaussian distribution for each systematic $j$ with a standard derivation $\pm1\sigma$ and expectation value of $\theta_j^0$ are obtained from Combined Performance working groups or data-driven methods.

To quantify the disagreement between our tested CP hypothesis and the data, we consider the profile likelihood ratio:
\begin{equation}
	\label{}
	\lambda(\phi_{\tau}) = \frac{\mathcal{L}(\phi_{\tau},\hat{\hat{\boldsymbol{\theta}}})}{\mathcal{L}(\hat{\phi_{\tau}},\hat{\boldsymbol{\theta}})}
\end{equation}
where the denominator indicates the best estimator of $\phi_{\tau}$ and $\boldsymbol{\theta}$ for which the $\mathcal{L}$ is maximized (unconditional maximum-likelihood estimator), while $\hat{\hat{\boldsymbol{\theta}}}$ in the numerator denotes the value of $\theta$ that maximizes $\mathcal{L}$ given the specified \PhiTau (conditional maximum-likelihood estimator). One can see $0 < \lambda < 1$ with $\lambda \rightarrow 1$ implying good agreement between data and CP hypothesis. Conveniently, the negative log-likelihood (NLL) is defined as the test statistic $q$:
\begin{equation}
	\label{}
	q = -2 \ln \lambda(\phi_{\tau}) =-2 \ln \frac{\mathcal{L}(\phi_{\tau},\hat{\hat{\boldsymbol{\theta}}})}{\mathcal{L}(\hat{\phi_{\tau}},\hat{\boldsymbol{\theta}})} =(\frac{\phi_{\tau}-\hat{\phi}_{\tau}}{\sigma_{\hat{\phi}_{\tau}}})^2
\end{equation}
The NLL adopted in the analysis is equivalent to $q/2$. The frequentist central confidence interval (C.I.) $\phi_{\tau}\in[\hat{\phi}_{\tau} -N\sigma_{\hat{\phi}_{\tau}},\hat{\phi}_{\tau} +N\sigma_{\hat{\phi}_{\tau}}]$ can be directly read off the NLL function using Neyman constructions [10.2307/91337]:
\begin{equation}
	\label{eq:DNLL}
	\Delta \operatorname{NLL} = \operatorname{NLL} - \operatorname{NLL_{min}} = \ln \mathcal{L}(\hat{\phi}_{\tau}) -\ln \mathcal{L}(\hat{\phi}_{\tau}\pm N\sigma_{\hat{\phi}_{\tau}}) = (\frac{\pm N\sigma_{\hat{\phi}_{\tau}}}{\sigma_{\hat{\phi}_{\tau}}})^2 = \frac{N^2}{2}
\end{equation}
The 68\% central confidence interval corresponds to 1 $\sigma_{\hat{\phi}_{\tau}}$ Gaussian standard deviation, which indicates that 68\% of all the test templates are containing true value of \PhiTau within this C.I.. To determine this C.I., or to estimate the standard deviation $\sigma_{\hat{\phi}_{\tau}}$ namely, the Asimov data set is defined as its signal and background events are equal to their expectation values. In this analysis, Standard Model based Asimov data set is used, which contains the pure \CP-even signal plus the expected background, so the NLL curve minimizes at the best estimator $\hat{\phi}_\tau= 0^\circ$ (SM hypothesis). Within the fitting region \PhiTau $\in [-90^\circ, 90^\circ]$, a set of 19 signal templates with the \PhiTau interval of $10^\circ$ are created by reweighting the \CP-flat VBFH, ggH, VH, and ttH signal samples produced through ATLAS simulations with TAUSPINNER [Przedzinski:2014pla].

%\begin{figure}[h!]
%	\begin{center}
%		\includegraphics[width=0.7\linewidth]{figures/results/Fit_model.png}
%	\end{center}
%	\caption{Schematic summary of the fit models used in the analysis. They are grouped by topologies (Boosted (green) and VBF (red)) and by decay channels (HH for \tauhh channel and LH for \taulh channel). }
%	\label{fig:fit_model}
%\end{figure}

In this analysis, 16 signal regions (SRs) and 2 control regions (CRs) are taken as the fit models, (8 SR, 1 CR in \taulh and the same in \tauhh). The normalization factors (NF) are unconstrained by Poisson or Gaussian distribution in Eq.~\ref{eq:binnedprofileLH}, where NF H is the NF of \htt signal samples while  NF Ztt is the NF of \ztt background samples.

\subsection{Expected Asimov fit results}
\label{sec:Asimovfit}
The $\Delta$NLL curves and the expected \CP-odd exclusion limits for \tauhh + \taulh combined fit with only statistical uncertainties of the Asimov data set (stat-only) and with all statistical and systematic uncertainties (stat+syst) are shown in Fig.~\ref{fig:expected_sensitivity} and Table.~\ref{tab:expected_sigma}.

As a result, according to the \CP-odd exclusion limits with statistical and systematic uncertainties, one have the confidence of 1.90 $\sigma$ to reject \CP-odd hypothesis within SM Asimov data set.
\begin{figure}[h!]
	\centering
	\includegraphics[width=0.75\linewidth]{figures/results/NLL_comparison.pdf}
	\caption{Expected $\Delta$NLL curves for the \tauhh + \taulh combined fit with stat-only (black dashed curve) and stat+syst (red dotted curve).
%		The expected value for \PhiTau is $0\pm35^{\circ}$ ($0\pm32^{\circ}$ for stat-only) at one standard deviation.
	}
	\label{fig:expected_sensitivity}
\end{figure}

\begin{table}[h!]
	\centering
	\caption{Expected CP-odd exclusion limits for the \tauhh + \taulh combined fit with stat-only and stat+syst}
	\begin{tabular}{lc}
		\hline
		\hline
		Fit settings & \CP-odd exclusion limits\\
		\hline
		stat-only & 2.07$\sigma$ \\
		stat+syst & 1.90$\sigma$ \\
		\hline
		\hline
	\end{tabular}
	\label{tab:expected_sigma}
\end{table}

\clearpage


%
%
Let the assumption with inputs $I_i$ and challenge $C$ be
  defined as in Chapter~\ref{assumption_def}.
Since we require $\lambda = k$ for decisional problems, the adversary
  cannot win unless he computes $C$ from $I_1,\ldots,I_n$ for both kinds of problems.
It is not hard to see that addition is useless to compute a monomial from monomials
  and we only have to consider multiplications.

More precisely, the adversary can win iff there are $\delta_i \in \mathbb{N}$ for $i \in [n]$
  such that holds that\footnote{
    Defining $S_1 * S_2 := \{ s_1 * s_2 \mid s_1 \in S_1 \land s_2 \in S_2\}$
    and $S^\delta = \{ \Pi_{i=1}^{\delta} s_i \mid s_1 \in S \land \ldots \land s_{\delta} \in S\}$
    as usual.
  }
$
  C \in (I_1^{\delta_1} * \cdots * I_n^{\delta_n})
$
  and the group setting allows for the computation of the product on the right-hand-side, i.e.,
$
  \Sigma_{i=1}^n\, \delta_i * \lambda_i = \lambda \text{.}
$
%
It is therefore sufficient to perform the following tasks to analyze such assumptions:
\begin{enumerate}
\item Compute a range expression $J$ that characterizes the set $I_1 * I_2$.
\item Check if $C \in I$ for a a monomial $C$ and a range expression $I$.
\item Compute a range expression $J$ that characterizes the set $I^\delta$ for a variable $\delta$.
\end{enumerate}
We will now describe our approach to handle these tasks.

\paragraph{1.}%
We can rename all range indices apart and then perform the following computation
$$
  (\forall \vec{r} \in \vec{R}:\, \vec{X}^{\vec{f}})
  *  (\forall \vec{r'} \in \vec{R'}:\, \vec{X}^{\vec{h}})
=
    (\forall \vec{r} \in \vec{R}, \vec{r'} \in \vec{R'}:\,
         \vec{X}^{\vec{f} + \vec{h}}) \text{.}
$$

\paragraph{2.}%
Let $C := \vec{X}^{\vec{g}}$ and
$I := \forall \range{1}, \ldots, \range{w}:\, \vec{X}^{\vec{f}}$.
We can then define a translation into the following system of polynomial constraints:
\begin{align}
 \alpha_i \leq \beta_i \\
 0 \leq{}& \delta_i \\
 \Sigma_{i=1}^n\, \delta_i * \lambda_i ={}& \lambda \\
 \alpha_j \leq r_j \leq{}& \beta_j \\
 k >{}& i \quad \text{for all levels $k - i$} \\
 f_i ={}& g_i
\end{align}
The constraint system is over the integer variables $k$, $\vec{l}$,
  $r_{j}$, and $\delta_i$.
%Using Z3 we can prove unsatisfiability or find models.
%Note that there are classes of inputs for which
%  the satisfiability problem is decidable.
%\begin{framed}
%  \noindent {\bf FIXME:} We probably have to allow for user-defined constraints
%  (such as $k > 2$) in the problem definition. We might also have to check
%  well-formedness of the problem, e.g., exponents of $X_i$ always positive.
%\end{framed}

\paragraph{3.}%
Let $I$ be defined as above.
If $f$ is linear in $\vec{r}$ (considering $k$ and $\vec{l}$
  as constants), then we can write $\vec{f}(k,\vec{l},\vec{r})$
  as $r_1*\phi_1(k,\vec{l}) + \ldots + r_k*\phi_k(k,\vec{l}) + \psi(k,\vec{l})$.
In this case, $I^{\delta}$ is characterized by the following range expression:
$$
    \forall r_1 \in \brack{\delta\alpha_1, \delta\beta_1},
            \ldots,
            r_k \in \brack{\delta\alpha_k, \delta\beta_k}:
            \vec{X}^{r_1*\vec{\phi}_1(k,\vec{l}) + \ldots + r_w*\vec{\phi}_w(k,\vec{l})
                     + \delta*\vec{\psi}(k,\vec{l})} \text{.}
$$

\input{range_expr_pow_proof}


% This is part of the TFTB Reference Manual.
% Copyright (C) 1996 CNRS (France) and Rice University (US).
% See the file refguide.tex for copying conditions.

\markright{fmt}
\section*{\hspace*{-1.6cm} fmt}

\vspace*{-.4cm}
\hspace*{-1.6cm}\rule[0in]{16.5cm}{.02cm}
\vspace*{.2cm}

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Fast Mellin Transform.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
[mellin,beta] = fmt(x)
[mellin,beta] = fmt(x,fmin,fmax)
[mellin,beta] = fmt(x,fmin,fmax,N)
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty fmt} computes the Fast Mellin Transform of signal {\ty x}.\\

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{8.5cm} c}
Name & Description & Default value\\
\hline
        {\ty x} & signal in time\\
        {\ty fmin, fmax} & respectively lower and upper frequency bounds of
         the analyzed signal. These parameters fix the equivalent
         frequency bandwidth (expressed in Hz). When unspecified, you
         have to enter them at the command line from the plot of the
         spectrum. {\ty fmin} and {\ty fmax} must be between 0 and 0.5\\
        {\ty N} & number of analyzed voices. {\ty N} must be even &
	auto\footnote{This value, determined from {\ty fmin} and {\ty fmax}, is the
	next-power-of-two of the minimum value checking the non-overlapping
	condition in the fast Mellin transform.}\\
\hline  {\ty mellin} & the {\ty N}-points Mellin transform of signal {\ty x}\\
        {\ty beta} & the {\ty N}-points Mellin variable\\
\hline
\end{tabular*}
\vspace*{.5cm}

The Mellin transform is invariant in modulus to dilations, and decomposes
the signal on a basis of hyperbolic signals. This transform can be defined
as\,:
\[M_x(\beta)=\int_0^{+\infty} x(\nu)\ \nu^{j2\pi \beta-1}\ d\nu\]
where $x(\nu)$ is the Fourier transform of the analytic signal
corresponding to $x(t)$. The $\beta$-parameter can be interpreted as a {\it
hyperbolic modulation rate}, and has no dimension\,; it is called the {\it
Mellin's scale}.

In the discrete case, the Mellin transform can be calculated rapidly using
a fast Fourier transform ({\ty fft}). The fast Mellin transform is  used,
for example, in the computation of the affine time-frequency distributions.
\end{minipage}

%\newpage

{\bf \large \sf Example}
\begin{verbatim}
         sig=altes(128,0.05,0.45);
         [mellin,beta]=fmt(sig,0.05,0.5,128);
         plot(beta,real(mellin));
\end{verbatim}
\vspace*{.5cm}

{\bf \large \sf See Also}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
ifmt, fft, ifft.
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf References}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
[1] J. Bertrand, P. Bertrand, J-P. Ovarlez ``Discrete Mellin Transform for
Signal Analysis'' Proc IEEE-ICASSP, Albuquerque, NM USA, 1990.\\

[2] J-P. Ovarlez, J. Bertrand, P. Bertrand ``Computation of Affine
Time-Frequency Representations Using the Fast Mellin Transform'' Proc
IEEE-ICASSP, San Fransisco, CA USA, 1992.
\end{minipage}


% This is part of the TFTB Reference Manual.
% Copyright (C) 1996 CNRS (France) and Rice University (US).
% See the file refguide.tex for copying conditions.

\markright{fmodany}
\section*{\hspace*{-1.6cm} fmodany}

\vspace*{-.4cm}
\hspace*{-1.6cm}\rule[0in]{16.5cm}{.02cm}
\vspace*{.2cm}

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Signal with arbitrary frequency modulation.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
[y,iflaw] = fmodany(iflaw)
[y,iflaw] = fmodany(iflaw,t0)
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty fmodany} generates a frequency modulated signal whose
        instantaneous frequency law is approximately given by the vector
        {\ty iflaw} (the integral is approximated by {\ty cumsum}).  The
        phase of this modulation is such that {\ty y(t0)=1}.\\

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{8.5cm} c}
Name & Description & Default value\\
\hline
        {\ty iflaw} & vector of the instantaneous frequency law samples\\
        {\ty t0}    & time reference          & {\ty 1}\\
  \hline {\ty y}     & output signal\\

\hline
\end{tabular*}

\end{minipage}
\vspace*{1cm}

{\bf \large \sf Example}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
         [y1,ifl1]=fmlin(100); [y2,ifl2]=fmsin(100);
         iflaw=[ifl1;ifl2]; sig=fmodany(iflaw);
         subplot(211); plot(real(sig))
         subplot(212); plot(iflaw);
\end{verbatim}
This example shows a signal composed of two successive frequency
modulations\,: a linear FM followed by a sinusoidal FM.\\
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf See Also}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
fmconst, fmlin, fmsin, fmpar, fmhyp, fmpower.
\end{verbatim}
\end{minipage}


\documentclass[10pt, reqno]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
%american mathematical society - AMS

\usepackage{hyperref}
\usepackage{float}
\usepackage{enumerate}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{subfig}

\oddsidemargin = 0in
\textwidth=6.5in

\numberwithin{equation}{section}

\numberwithin{figure}{section}

\title{Calculating Volume from an Implicit Function }
\author{Johnny Minor}
\date{\today}

\begin{document}

\maketitle

\section{Introduction}
In everyday life we may never notice the intricacies of something so simple as a volume. When at the grocery store we can pick up a can of soup and be greeted immediately with the volume of the can in our hand. However, a clever mathematician or engineer had to calculate the volume to make our life easier. Otherwise one might find many seasoned professors looking contemplative in isle seven at Safeway as their mind churns the numbers to find the volume of the can of soup. In this particular paper we will concern ourselves with describing the method of finding the volume of an egg shaped wine fermentation tank.

\section{Background}
\label{sec:background}

The particular tank that we are interested in calculating the volume of is a tank manufactured by the Sonoma Stone company.
According to their website the tank has a volume of 476 gallons. \cite{ref:sonoma}

This value is very helpful to guide us as we calculate the volume. It's generally good practice to have a reference value or expression when doing computations. So you're not just blindly clutching at straws. Really it's nice because often times when programming numerical methods you're first attempt probably isn't correct. Without a value to compare to it's very difficult to decipher if you're program is working correctly.

The equation which describes a cross section of the tank is given to be
\begin{equation}
0.0002017404542x^2 + \frac{0.0001303290910y}{20.9520444748+\alpha y} = 1.
\end{equation}

\noindent Where $x$ and $y$ are their usual $x$ and $y$ coordinates(measure in centimeters) and $\alpha$ is a constant that changes the overall roundness of the cross section. A lower value of $\alpha$ corresponds to a rounder cross section. A high value corresponds to a less circular cross section, or a more "smushed" shape. For our example the $\alpha$ is equal to $0.005$.

A natural progression would be to wonder what this unfamiliar equation looks like when plotted. It can be seen that it looks very similar to an egg. %TODO: include cross section of function HERE!!!

\begin{figure}[H] % 'h' means put it here and 'ht' means that put it at top the and here. 'ht!' put it here now!
\centering
\includegraphics[width=0.5\textwidth]{cross_section.png}
\caption{What a cross section of our function looks like.}
%\label{fig:cross-seciton}
\end{figure}

\noindent And this egg shaped equation spun around the $y$ axis what we hope to solve for.

\section{Mathematical Method}

It seems like an insurmountable task to a compute a volume integral at first glance of this function. And it would be quite a formidable task without a little help from our friends in form of numpy and scipy. Well, as Professor Cooper says frequently: "We're mathematicians, we know how to do this(paraphrased)." Let's get on it with it then.

As we learned in our calculus courses. We can use the disc method to integrate our function. The general form of the disc method is to form a shape that resembles a circle. Since we know the area of a circle to be $\pi R^2$ where $R$ is the radius of the circle. If we form an infinite number of discs this will become an integral. This integral will take the form
\begin{equation}
\label{eq:formula}
\mathrm{Volume}=\pi\int_a^b [R(y)]^2\ \mathrm{d}y.
\end{equation}

\noindent Where $R(y)$ is our function and $a$ and $b$ are our limits of integration. Although we run into a problem quickly. the equation \ref{eq:formula} is an implicit function. So we first need to solve for $x$ as a function of $y$. This can be done easily with Sympy, but it is an important step in the process nevertheless.

It is often nice to have a visualization of what's going on. A visual representation of the disc method is like this

\begin{figure}[H] % 'h' means put it here and 'ht' means that put it at top the and here. 'ht!' put it here now!
\centering
\includegraphics[width=0.4\textwidth]{Disc_integration.png}
\caption{What a cross section integration looks like.\cite{ref:cross_section}}
\label{fig:cross-seciton}
\end{figure}

In figure \ref*{fig:cross-seciton} the function is being rotated around the $y$ axis.
Now, once the function is solved for $x$ as a function of $y$ then we can use Sympy to carry out the integration for us. This is an easy task for the computer and yields the volume that we're interested in.

\section{Conclusion}
Using Sympy and Numpy we were easily able to calculate the volume of the egg shaped barrel to be 475.82 gallons which is slightly less than the advertised 476 gallons. In this volume we would expect 2401.59027585 bottles that are 750mL. This isn't a perfectly even value of bottles of wine, but I would speculate there is some quality control testing that takes place before the barrel is bottled for consumption.

Further in our analysis we varied the value of $\alpha$ and then calculated the corresponding volume of the tank with the given cross section equation.

\begin{figure}[H] % 'h' means put it here and 'ht' means that put it at top the and here. 'ht!' put it here now!
\centering
\includegraphics[width=0.8\textwidth]{alpha_volume_plot.png}
\caption{Varying $\alpha$ and calculating the volume}
\label{fig:alpha_volume}
\end{figure}

This is an interesting plot, and it behooves us to do a simple analysis. Figure \ref{fig:alpha_volume} shows that there are actually values of $\alpha$ that would yield a greater volume--interesting. The engineering intentions come to the surface and we might speculate that the egg shape was chosen to minimize floor space while maximizing volume. Perhaps this shape was chosen to help aid in the fermentation process as well.

Speculation aside, it is an extraordinary feat of mathematics to be able to carry out a calculation via a computer that even the most clever mathematician could only hope to be able to do a mere 400 years ago.

\begin{thebibliography}{XX}
%TODO: actually fill out the biography.

\bibitem{ref:cross_section} %when we refer to it by name we use this name.
Petteri Aimonen (\url{https://en.wikipedia.org/wiki/File:Disc_integration.svg})[Public Domain], via Wikimedia Commons

\bibitem{ref:sonoma} %when we refer to it by name we use this name.
Sonoma Cast Stone (\url{http://www.concretewinetanks.com/concrete-egg-tank.html}) \today

\end{thebibliography}

\end{document}


\documentclass[main.tex]{subfiles}

\begin{document}

\subsection{Training}

We trained classifiers to solve the problem with machine learning algorithms: Logistic Regression, Support Vector Machine, Fisher Discriminant Analysis, and Neural Networks.
For each algorithm, we check the speed of training, inference speed, and translation invariance under the same condition.
In the following sub-sections, we will describe how we used each algorithm.

\subsubsection{Logistic Regression}
Logistic Regression is designed for binary classification tasks. Given an input vector, it outputs yes or no.
Therefore, we need to adopt a heuristic, one-vs-rest, because our task is multi-class classification.
It divides multi-class dataset into several binary classification problems.
A logistic regression model is then optimized on each binary classification problem.
Finally, we use the Limited Memory BFGS (LBFGS) algorithm to optimize the model by minimizing the objective function following.

\begin{equation}
	L(\mathbf{w}, b) = \sum_{i=1}^{N} \log \left(\exp\left(-y_{i} \left(\mathbf{x}_i^{\top}\mathbf{w} + b\right)\right) + 1\right) + \frac{1}{2} \mathbf{w}^{\top}\mathbf{w}
\end{equation}

\subsubsection{Support Vector Machine}
Support Vector Machine is also a binary classifier.
Hence, we used one-vs-rest heuristic for the same reason as in logistic regression.
And we experimented with a few different kernel functions: Linear kernel, Gaussian kernel, Sigmoid kernel, and Polynomial kernel with degree 2 and 3.

\subsubsection{Fisher Discriminant Analysis}
Fisher Discriminant Analysis, unlike the previous two algorithms, allows for multi-class classification.
And it has closed-form solution rather than using iterative method.
We used least square solver to fit the model.

\subsubsection{Multi Layer Perceptron}
We design a network with the architecture following:
\begin{enumerate}[(1)]
	\item A fully connected layer to a hidden layer of size 64
	\item A rectifier non-linearity
	\item A fully connected layer to a hidden layer of size 32
	\item A rectifier non-linearity
	\item A fully connected layer that outputs a vector of size 10, corresponding to logit probabilities for each class
\end{enumerate}

The model parameters $\theta$ are updated by gradient descent using Adam optimizer on the loss function following:

\begin{equation} \label{crossentropy}
	L(\theta) = - \mathbf{t}^{\top} \log \mathbf{p} + 10^{-4} \times \|\theta\| ^2
\end{equation}

\subsubsection{Convolutional Neural Network}
Unlike other algorithms above, convolutional neural network gets the grayscale of $8 \times 8 \times 1$ itself as input without flattening.
The input data are processed by the feature extractor.

The feature extractor applies the following modules:
\begin{enumerate}[(1)]
	\item A convolutional of $16$ filters of kernel size $3 \times 3$ with stride 1 and zero-padding.
	\item A rectifier non-linearity
	\item A max-pooling of kernel size $2 \times 2$ with stride 2
	\item A convolutional of $32$ filters of kernel size $3 \times 3$ with stride 1 and zero-padding.
	\item A rectifier non-linearity
	\item A max-pooling of kernel size $2 \times 2$ with stride 2
\end{enumerate}

The output of the feature extractor is passed into a fully connected layer that outputs a vector of size 10, corresponding to logit probabilities for each class.
We optimized the model in the same way as in the case of the multi layer perceptron.

\subsection{Validation}
To measure the accuracy of each model and check whether the model is in overfitting or not, we first split the MNIST dataset into two subsets.
One is the training set. It is used to update the parameters of the models.
The other is the validation set, which consists of 200 data pairs (20 samples per class, about 11\% of the total data). It is not used to train the models directly.
Two datasets do not have any overlapped samples.

\begin{figure}[H]
	\centering
	\includegraphics[scale=0.5]{img/experiment/translated_img.png}

	\caption{Mean image of images in translated validation set. Compared to Fig. \ref*{edafig}-(c), they are skewed to the left or right.}
	\label{translation}
\end{figure}

To test translation robustness, we also created a new validation set, translated validation dataset.
It consists of images of the validation dataset translated left or right.
It consists of a total of 400 image sets, with 200 left-biased and 200 right-biased.
However, we do not include this kind of data in the training set.
In other words, the models experience only center-centered images in the training phase.

\end{document}


\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{enumerate}
\usepackage{amsmath}

\setlength{\oddsidemargin}{.1in}
\setlength{\textwidth}{6.3in}
\setlength{\textheight}{8.9in}
\setlength{\topmargin}{-.5in}

\title{Simulating the dynamics of the pendulum and cart}
\date{}

\begin{document}

\maketitle

\section{Kinematics}

Consider a cart of mass $M$, motorized and free to move along the x-axis upon a frictionless track. A pendulum bob of mass $m$ is attached to the cart by a rigid, massless, freely swinging rod of length $l$. Let's start by writing down the kinematics---that is, the position, velocity, and acceleration---for each of the cart and bob.

For the cart,

\begin{align}
\mbox{position:} &\quad x \mathbf{\hat{x}} \nonumber\\[5pt]
\mbox{velocity:} &\quad x' \mathbf{\hat{x}} \nonumber\\[5pt]
\mbox{acceleration:} &\quad x'' \mathbf{\hat{x}}. \nonumber
\end{align}

And for the bob,

\begin{align}
\mbox{position:} &\quad (x + l \sin{\theta}) \mathbf{\hat{x}} + l \cos{\theta} \mathbf{\hat{y}} \nonumber\\[5pt]
\mbox{velocity:} &\quad (x' + l \theta' \cos{\theta}) \mathbf{\hat{x}} - l \theta' \sin{\theta} \mathbf{\hat{y}} \nonumber\\[5pt]
\mbox{acceleration:} &\quad (x'' + l \theta'' \cos{\theta} - l \theta'^2 \sin{\theta}) \mathbf{\hat{x}} - (l \theta'' \sin{\theta} + l \theta'^2 \cos{\theta}) \mathbf{\hat{y}}. \nonumber
\end{align}

\section{Forces}

We can list the forces acting on each of the cart and bob as well (see Fig. \ref{fig:free_body}).

\begin{figure}
\center
\includegraphics[width=0.9\linewidth]{free_body.png}
\caption{Free body diagram for our setting of the cart and pendulum.}
\label{fig:free_body}
\end{figure}

The cart experiences

\begin{align}
\mbox{force due to gravity:} &\quad {-M} g \mathbf{\hat{y}} \nonumber\\[5pt]
\mbox{rod tension:} &\quad T \sin{\theta} \mathbf{\hat{x}} + T \cos{\theta} \mathbf{\hat{y}} \nonumber\\[5pt]
\mbox{normal force on track:} &\quad N \mathbf{\hat{y}} \nonumber\\[5pt]
\mbox{motor force:} &\quad f \mathbf{\hat{x}}. \nonumber
\end{align}

And the bob experiences

\begin{align}
\mbox{force due to gravity:} &\quad -m g \mathbf{\hat{y}} \nonumber\\[5pt]
\mbox{rod tension:} &\quad {-T} \sin{\theta} \mathbf{\hat{x}} - T \cos{\theta} \mathbf{\hat{y}}. \nonumber
\end{align}

\section{Equations of motion}

Using Newton's law $\mathbf{F_{net}} = m \mathbf{x''}$ to relate forces with kinematics, we get the two expressions which follow.

For the cart,

\begin{equation}
T \sin{\theta} \mathbf{\hat{x}} + f \mathbf{\hat{x}} - M g \mathbf{\hat{y}} + T \cos{\theta} \mathbf{\hat{y}} + N \mathbf{\hat{y}} = M x'' \mathbf{\hat{x}}.
\end{equation}

And for the bob,

\begin{equation}
-T \sin{\theta} \mathbf{\hat{x}} - m g \mathbf{\hat{y}} - T \cos{\theta} \mathbf{\hat{y}} = m x'' \mathbf{\hat{x}} + m l \theta'' \cos{\theta} \mathbf{\hat{x}} - m l \theta'^2 \sin{\theta} \mathbf{\hat{x}} - m l \theta'' \sin{\theta} \mathbf{\hat{y}} - m l \theta'^2 \cos{\theta} \mathbf{\hat{y}}.
\end{equation}

Separating these two equations into vector components gives four equations.

\begin{align}
\label{eq:a} \mbox{cart x:} &\quad T \sin{\theta} + f = M x'' \\[5pt]
\mbox{cart y:} &\quad {-M} g + T \cos{\theta} + N = 0 \\[5pt]
\label{eq:b} \mbox{bob x:} &\quad {-T} \sin{\theta} = m x'' + m l \theta'' \cos{\theta} - m l \theta'^2 \sin{\theta} \\[5pt]
\label{eq:c} \mbox{box y:} &\quad {-m} g - T \cos{\theta} = -m l \theta'' \sin{\theta} - m l \theta'^2 \cos{\theta}.
\end{align}

Adding together equations (\ref{eq:a}) and (\ref{eq:b}) to eliminate $T$ gives

\begin{equation}
\label{eq:d} f = (M + m) x'' + m l \theta'' \cos{\theta} - m l \theta'^2 \sin{\theta}.
\end{equation}

Multiplying (\ref{eq:b}) through by $\cos{\theta}$ and multiplying (\ref{eq:c}) through by $-\sin{\theta}$ and then adding these together gives

\begin{equation}
\label{eq:e} m g \sin{\theta} = m x'' \cos{\theta} + m l \theta''.
\end{equation}

Our two equations of motion are (\ref{eq:d}) and (\ref{eq:e}). However, these must be rewritten to isolate $x''$ and $\theta''$ in each equation. This can be done algebraically or using \textit{Mathematica}, yielding

\begin{align}
x'' &= \frac{f + m l \theta'^2 \sin{\theta} - m g \sin{\theta} \cos{\theta}}{M - m - m \cos^2{\theta}} \\[5pt]
\theta'' &= \frac{-f \cos{\theta} + (M + m) g \sin{\theta} - m l \theta'^2 \sin{\theta} \cos{\theta}}{l (M + m - m \cos^2{\theta})}.
\end{align}

We can turn these two second-order ODEs into four first-order ODEs, yielding a form which will be suitable for numerical simulation via the Runge-Kutta method:

\begin{align}
x' &= v \\[5pt]
\theta' &= \omega \\[5pt]
v' &= \frac{f + m l \omega^2 \sin{\theta} - m g \sin{\theta} \cos{\theta}}{M + m - m \cos^2{\theta}} \\[5pt]
\omega' &= \frac{-f \cos{\theta} + (M + m) g \sin{\theta} - m l \omega^2 \sin{\theta} \cos{\theta}}{l (M + m - m \cos^2{\theta})}.
\end{align}

These are the equations of motion we will use for numerical simulation.

\section{Runge-Kutta update rule}
Now we will work out the update rules for simulating via the Runge-Kutta method. We can represent the system with a 5-dimensional state vector

\begin{align}
\mathbf{x} = (t, x, \theta, v, \omega) \in {\rm I\!R}^5, \quad \mbox{where, equivalently,} \\[5pt]
x_1 = t \nonumber\\[5pt]
x_2 = x \nonumber\\[5pt]
x_3 = \theta \nonumber\\[5pt]
x_4 = v \nonumber\\[5pt]
x_5 = \omega \nonumber.
\end{align}

So, our equations of motion become

\begin{align}
\frac{d}{dt} x_1 &= f_1(\mathbf{x}) = 1 \tag*{(passage of time; $dt = 1$)} \nonumber\\[5pt]
\frac{d}{dt} x_2 &= f_2(\mathbf{x}) = x_4 \tag*{(change in cart position; $\frac{dx}{dt} = v$)} \\[5pt]
\frac{d}{dt} x_3 &= f_3(\mathbf{x}) = x_5 \tag*{(change in pendulum angle; $\frac{d\theta}{dt} = \omega = 1$)} \\[5pt]
\frac{d}{dt} x_4 &= f_4(\mathbf{x}) = \frac{f + m l {x_5}^2 \sin{x_3} - m g \sin{x_3} \cos{x_3}}{M + m - m \cos^2{x_3}} \tag*{(change in cart velocity; $\frac{dv}{dt} = a$)} \\[5pt]
\frac{d}{dt} x_5 &= f_5(\mathbf{x}) = \frac{-f \cos{x_3} + (M + m) g \sin{x_3} - m l {x_5}^2 \sin{x_3} cos{x_3}}{l (M + m - m \cos^2{x_3})} \tag*{(change in pendulum angular velocity; $\frac{d\omega}{dt} = \alpha$).}
\end{align}

This can be expressed even more generally as

\begin{equation}
\frac{d}{dt} \mathbf{x} = \mathbf{f}(\mathbf{x})
\end{equation}

where $\mathbf{f}$ is a loosely defined ``vector of functions'' $\mathbf{f} = (f_1, f_2, f_3, f_4, f_5)$, each accepting the system state $\mathbf{x}$ as an argument and returning the rate of change of a particular state variable.

The Runge-Kutta method, using a step of size $h$, numerically approximates four intermediate values of the state as it advances from $\mathbf{x}_i$ to $\mathbf{x}_{i+1}$

\begin{align}
\mathbf{k}_1 &= \mathbf{f}(\mathbf{x}) \\[5pt]
\mathbf{k}_2 &= \mathbf{f}(\mathbf{x} + \frac{h}{2} \mathbf{k}_1) \\[5pt]
\mathbf{k}_3 &= \mathbf{f}(\mathbf{x} + \frac{h}{2} \mathbf{k}_2) \\[5pt]
\mathbf{k}_4 &= \mathbf{f}(\mathbf{x} + h \mathbf{k}_3).
\end{align}

and then returns a weighted average as the result:

\begin{equation}
\mathbf{x}_{i+1} = \mathbf{x}_i + \frac{h}{6}(\mathbf{k}_1 + 2 \mathbf{k}_2 + 2 \mathbf{k}_3 + \mathbf{k}_4).
\end{equation}

This is the update rule for advancing the simulation by a step $h$.

\end{document}