\documentclass[../OpenAppliedMusicTheory.tex]{subfiles}

\externaldocument{../OpenAppliedMusicTheory}
\externaldocument{chapter0}

\begin{document}

\sfchapter{Fundamental Ideas}{2}

    Now that we have explained basic notation, we can move onto a bit more substantial concepts. There are fundamental ideas which will be explained here. These ideas are something maybe akin to learning how to add and subtract numbers in math.

    \section{Notes and Rests (durations)}\label{ch2:duration}
        \subsection{Naked Note Durations}
        \paragraph{Picture of some notes here with labels}
        From left to right, the notes decrease in duration by a factor of two. If we look at the names then we can kind of think of them as fraction values and compare the relative magnitudes of these fractions. A whole note (\musWhole) would be like $1$, and a half note (\musHalf) would be like $\frac{1}{2}$. Therefore, a half note is half the duration of a whole note. Similarly, a quarter note (\musQuarter) is like $\frac{1}{4}$ and a sixteenth note (\musSixteenth) is like $\frac{1}{16}$. In this case $\frac{1}{16}$ is $4$ times smaller than $\frac{1}{4}$ and so a sixteenth note is four times shorter than a quarter note. Another way to think of this is that there are four sixteenth notes in a quarter note.

        Another way to think of it is visually. Each thing that is added on halves the duration of the note. This is most obvious with quarter notes and shorter. Each time you add a flag, the note halves in duration. This idea is true for shorter durations too. If you take a whole note and add a stem, it becomes a half note which is half the duration. If you take the half note and fill in the circle, it becomes a quarter note which is, again, half the duration.

        \subsection{Dotted Notes (with Augmentation Dots)}
        The first one on the left is actually not an accidental nor an articulation. It might actually be better fit above in the notes section, but is here with the other disconnected markings for now. It's simply called a dot. Just like it looks. The dot lengthens whatever it is after by a factor of $\frac{1}{2}$. Remember how there are two quarter notes in the space of a half note? %TODO: checkpoint here to test how to fill a half note with something dotted like that
        \begin{note}{Quick question}
            How do we fill a half note with a dotted note of some sort and another note?
        \end{note}

        In this case, there would be a dotted quarter note and a eighth note in the space of a half note. There are two eighth notes in the space of a quarter note and so if a dotted quarter note is 1.5 times the duration, it should last the equivalent duration of 3 eighth notes. There are two quarter notes in a half note which is 4 eighth notes and so that would leave us with $4-3=1$ eighth note in addition to the dotted quarter note to fill the space of a half note.

        Remember how I said that dots lengthen \emph{whatever} they are after by a factor of $\frac{1}{2}$? I deliberately used "whatever" instead of "note" because there can be notes with $2, 3, 4, \dots, \infty$ dots. Of course you won't really see more than 2 or maybe 3 in real world cases. In any case, though, they should be easy to understand if you already understand regular dots. They just multiply the duration of the thing before the dot by 1.5.

        \paragraph{figure of subdivision}

        If we have a double dotted half note, well let's just break it down. The first dot adds half of the half note. Half of a half note is a quarter note. Now for the second dot we add another half of that. Half of a quarter note is an eighth note. In total, we have one half note plus a quarter note plus an eighth note. An additional dot would add a sixteenth note and so on. \emph{Fun fact: if there were infinite dots after a given note, the resultant note would have a duration equal to precisely twice the original note.}

        %TODO: figure out why the sixteenth note is like this in musicology package
        \subsection{Subdivision}
        If we look above to the previous picture, we'll see what's called \textbf{subdivision} happening. Subdivision is the division of notes or beats into smaller quantities (hence the name \emph{subdivision}). So a subdivision of a quarter note may be into two eighth notes where each of those are subdivided into two sixteen notes. Either that or the quarter note can be subdivided directly into four sixteenth notes.

        \subsection{Rests}
        \paragraph{picture of rests} Above is a picture of some rests. Rests are periods of "silence" or rest. Another way to think about it is just the absence of a note. On the left is a whole rest and it goes to the right with the same names as notes. Whole rest, then half rest, then quarter rest, then eighth rest, and so forth.

        As a way to remember what is what, the whole rest is hanging down because it's heavy or more massive whereas the half rest is on top because it's light enough to do so. From there just remember quarter rest since you'll see it very often. Eighth rests are like a half of a quarter rest and going on from there the smaller rests add a little extra knob. %TODO: is there a word that represents this?

        \subsection{Ties}
        \paragraph{picture of ties} These are ties. Ties are ways of connecting notes of the same pitch together. In the first case, this would result in a note with a duration equal to the sum of the durations of its parts.

        Why would we do this?

        The first reason (in no particular order) is readability. We will explain meter and time signatures further in the next section, but you probably understand the idea that music has beats to it. For a performer reading music, knowing where beats are is useful to keep on track with everything. Yes we can display a quarter note and an eighth note as a dotted quarter note, but in certain cases it can be difficult to understand where the beat is and so it can be better to tie two notes together instead.

        The second reason is to tie across measures. We will also learn about measures further in the next section, but measures are a way of grouping notes together. When you want a note to be held between measures, ties can be useful.

    \section{Meter and Time Signatures}\label{ch2:meter}
        \subsection{Meter}
        \textbf{Meter} is something that describes how music is organized with respect to time. In other words, it is something that determines how we should group notes together, as well as how we can count to the music. By this second definition, it becomes evident that meter is not always clear cut. For a given piece of music, it is possible that meter can be argued, but in many circumstances it \emph{is}, in fact, clear.

        Moving on, there are are multitude of different meters. These can be split in two ways in western music. \textbf{Duple}, \textbf{triple}, and \textbf{quadruple} meter determine how we count the music. In other words, this determines the grouping of the beats. \textbf{Simple meter} and \textbf{compound meter} determine how we subdivide the beat.

        \paragraph{picture of subdivisions and groupings}As a result, duple meter would be counting in groups of two beats, triple in groups of three beats, and quadruple groups of four beats. Simple meters divide each beat into two, while compound meters divide each beat into three.

        These two classifications can be combined to give the meters we commonly use in western music. As an example, a simple quadruple music would be groups of four beats where each beat is divided into two. A compound duple meter would be groups two beats where each beat is divided into three.

        \paragraph{example of these meters}

        \subsection{Time Signatures}
        \paragraph{picture of time signatures on staff}
        We notate meter using time signatures. The bottom number determines the type of note that represents one beat (or a third of a beat in the case of compound meter) while the top number determines how many of the aforementioned notes should be grouped together.

        Let's start with the bottom number. The bottom number of a duple meter determines directly the type of note that represents a beat. Remember how we thought of note durations as fractions? This bottom number would be the denominator of the fraction above. An eighth note would then be the number 8, a half note would be the number 8, etc.

        For a compound meter, the bottom number represents a  subdivision of the beat. Since compound meters first divide by three, %TODO: explain subdivision for compound meters
        a meter where a dotted quarter note is the beat would have the denominator of an eighth note on the bottom. This is because there are three eighth notes inside of a dotted quarter note. Overall, however, you can remember this as a way to remember the note to use to count for the top number. %TODO: consider elaborating or rewriting this, it's confusing.

        %TODO: Explain top number and explain how this all fits together
        The top number is simple with respect to this. It simply represents how many of the notes (as denoted by the bottom number) should be grouped together. Let's look at a 3/4 time signature.

        \paragraph{3/4 time signature with a measure}

        The 4 on the bottom means to count the top number by quarter notes, and the 3 on the top number means that the meter groups notes 3 quarter notes as a time. This also is a good way to see the the type of meter since the numbers are unique in this way.\footnote{This is not necessarily true. There are situations where meter numbers may mean different things, but there are other ways to make sure this is counted right. In the future this may be elaborated on because we understand that the idea of time signatures is very confusing with respect to everything.} Numbers 2, 3, and 4 on the top often denote simple meters while numbers 6, 9, and 12 often denote compound meters. 2/4, 3/4, and 4/4 are likely the most common simple meters you'll see. 6/8, 9/8, and 12/8 are likely the most common compound meters you'll see. That being said, there are also simple meters like 2/2 and compound meters like 6/4 that you'll see around so having a denominator of 4 does not necessarily mean simple meter.

        \subsection{Measures}
        Remember the groupings that are denoted by the top number with respect to the bottom number? Each of those groups fit in what is called a \textbf{measure}.

        \paragraph{measure picture}
        A measure is denoted as a part of a score and is separated by bar lines. There are different types of bar lines like repeat bar lines, double bar lines, and ending bar lines. They mean different things and will be defined at the end of this chapter. Time signatures are a way to notate the meter of a piece of music where measures separate/hold groups of notes.

\end{document}


\subsection{Gradient tree boosting}

This applies gradient boosting to tree.


% !TEX root=../report.tex

\section{Properties}

\statefultrue

\subsection{Equality}

\begin{align*}
\forall t_1, t_2 .  t_1 \sim t_2 \iff &&\forall i . t_1 \handle{i} t_1' \implies& & t_2 \handle{i} t_2'\\
&&&\wedge & \Value{(t_1')} \equiv \Value{(t_2')}\\
&&&\wedge & t_1' \sim t_2'\\
&\wedge& \forall i . t_2 \handle{i} t_2' \implies& & t_1 \handle{i} t_1'\\
&&&\wedge & \Value{(t_1')} \equiv \Value{(t_2')}\\
&&&\wedge & t_1' \sim t_2'
\end{align*}

\subsection{Equivalence}

\begin{align*}
\forall t_1, t_2 .  t_1 \simeq t_2 \iff \forall i_1 .\exists i_2 . t_1 \handle{i_1} t_1'\implies& & t_2 \handle{i_2} t_2'\\
                                                    & \wedge & \Value{(t_1')} \equiv \Value{(t_2')}\\
                                                    & \wedge & t_1' \simeq t_2'\\
                                                    & \wedge & i_1 \bumpeq i_2\\
                                      \wedge\forall i_2.\exists i_1 . t_2 \handle{i_2} t_2'\implies && t_1 \handle{i_1} t_1'\\
                                      & \wedge & \Value{(t_1')} \equiv \Value{(t_2')}\\
                                      & \wedge & t_1' \simeq t_2'\\
                                      & \wedge & i_1 \bumpeq i_2\\
\end{align*}
\todo{What is missing still: $i_2$ can also be/contain empty/be a normalisation (???)}
\todo{Define the equivalence of i's $\bumpeq$}
\subsection{Progress}

\begin{align*}
  \forall e:\tau,i & e \handle{i} e' &\implies& \text{ either Fail}(e') \text{ or } \exists e'',i' \text{ such that } e'\handle{i'}e''&\\
\end{align*}

\subsection{Preservation}

\begin{theorem}[preservation under handling]
  $\forall e:\tau,i  e \handle{i} e' \implies e':\tau$
  \label{thmpreshandle}
\end{theorem}

\begin{theorem}[preservation under normalization]
    $\forall e:\tau  e \stride e' \implies e':\tau$
    \label{thmpresnorm}
\end{theorem}

\begin{theorem}[preservation under evaluation]
      $\forall e:\tau  e \evaluate e' \implies e':\tau$
      \label{thmpreseval}
\end{theorem}

\begin{proof}
  We prove Theorem~\ref{thmpreseval} by induction on $e$:\\
  \noindent\textbf{Case} $e=\lambda x:\tau.e, e_1 e_2, x, c, l, e_1 \star e_2,
                            \If{e_1}{e_2}{e_3},\tuple{e_1, e_2},\unit,\Ref e,!e,
                            e_1 := e_2,e_1; e_2$
      preservation has been proven for these cases by \todo{insert cite}\\

  \noindent\textbf{Case} $\userule{E-Edit}$
      By T-Edit we have $\Edit e:\Task \tau$ and $e:\tau$. The induction
      hypothesis gives us that $e\evaluate v$ also preserves, and thus $v:\tau$.
      Therefore $\Edit v:\Task\tau$.\\

  \noindent\textbf{Case} $\userule{E-Enter}$
      Evaluation does not alter the expression, therefore this case holds tivially.\\

  \noindent\textbf{Case} $\userule{E-Update}$
      By T-Update we have $\Edit e:\Task \tau$ and $e:\Ref \tau$. The induction
      hypothesis gives us that $e\evaluate l$ also preserves, and thus $l:\Ref\tau$.
      Therefore $\Update l:\Task\tau$\\

  \noindent\textbf{Case} $\userule{E-Fail}$
      Evaluation does not alter the expression, therefore this case holds tivially.\\

  \noindent\textbf{Case} $\userule{E-Then}$
      Given that $e_1\Then e_2:\Task \tau$, T-Then gives us that $e_1:\Task\tau_1$
      and $e_2:\tau_1 \to \Task \tau$. By the induction hypothesis, we know that
      $e_1\evaluate t_1$ preserves and thus $t_1:\Task\tau_1$. Therefore
      $t_1\Then e_2:\Task\tau$.\\

  \noindent\textbf{Case} $\userule{E-Next}$
      Given that $e_1\Next e_2:\Task \tau$, T-Then gives us that $e_1:\Task\tau_1$
      and $e_2:\tau_1 \to \Task \tau$. By the induction hypothesis, we know that
      $e_1\evaluate t_1$ preserves and thus $t_1:\Task\tau_1$. Therefore
      $t_1\Next e_2:\Task\tau$.\\

  \noindent\textbf{Case} $\userule{E-And}$
      Given that $e_1\And e_2:\Task(\tau_1\times\tau_2)$, T-And gives us that
      $e_1:\Task\tau_1$ and $e_2:\Task\tau_2$. By the induction hypothesis, we
      know that both $e_1\evaluate t_1$ and $e_2\evaluate t_2$ preserve and thus
      $t_1:\Task\tau_1$ and $t_2:\Task\tau_2$. Therfore
      $t_1\And t_2:\Task(\tau_1\times\tau_2)$\\

  \noindent\textbf{Case} $\userule{E-Or}$
      Given that $e_1\Or e_2:\Task\tau$, T-Or gives us that $e_1:\Task\tau$ and
      $e_2:\Task\tau$. By the induction hypothesis, we have that both
      $e_1\evaluate t_1$ and $e_2\evaluate t_2$ preserve and thus $t_1:\Task\tau$
      and $t_2:\Task\tau$. Therefore $t_1\Or t_2:\Task\tau$.\\

  \noindent\textbf{Case} $\userule{E-Xor}$
      Evaluation does not alter the expression, therefore this case holds tivially.
\end{proof}

Before we can show that normalisation also preserves, we need to show that the following lemma holds:\\

\begin{lemma}[Task value preserves]
  $\forall e:\Task\tau . \Value{(e)}=v \implies v:\tau$
  \label{lemmavaluepreserves}
\end{lemma}
\begin{proof}
  \noindent\textbf{Case} $\Value{(\Edit v,s)}=v$ By T-Edit, if $\Edit v:\Task\tau$, then $v:\tau$.\\

  \noindent\textbf{Case} $\Value{(\Enter \tau,s)}=\bot$ Since this case does not lead to a value, the lemma holds trivially.\\

  \noindent\textbf{Case} $\Value{(\Update l,s)}=s(l)$ \todo{Which rule do we use here to show that s(l) has the same type as l???}\\

  \noindent\textbf{Case} $\Value{(\Fail)}=\bot$ Since this case does not lead to a value, the lemma holds trivially.\\

  \noindent\textbf{Case} $\Value{(t_1\Then e_2)}=\bot$ Since this case does not lead to a value, the lemma holds trivially.\\

  \noindent\textbf{Case} $\Value{(t_2\Next e_2)}=\bot$ Since this case does not lead to a value, the lemma holds trivially.\\

  \noindent\textbf{Case} $\Value{(t_1\And t_2)}=\tuple{v_1, v_2}, \Value{(t_1)}=v_1\wedge\Value{(t_2)}=v_2$ By T-And we have that $t_1\And t_2:\Task(\tau_1\times\tau_2)$ and $t_1:\tau_1$ and $t_2:\tau_2$. By the induction hypothesis, $ \Value{(t_1)}=v_1$ and $\Value{(t_2)}=v_2$ preserve, and thus $v_1:\tau_1$ and $v_2:\tau_2$. This gives us that $\tuple{v_1, v_2}:\Task(\tau_1\times\tau_2)$ \\

  \noindent\textbf{Case} $\Value{(t_1\And t_2)}=bot, \not\Value{(t_1)}=v_1\wedge\Value{(t_2)}=v_2$ Since this case does not lead to a value, the lemma holds trivially.\\

  \noindent\textbf{Case} $\Value{(t_1\Or t_2)}=v_1, \Value{(t_1)}=v_1$ By T-Or we have that $t_1\Or t_2:\Task\tau$, and $t_1:\Task\tau$ and $t_2:\Task\tau$. By the induction hypothesis, we have that $v_1:\tau$.\\

  \noindent\textbf{Case} $\Value{(t_1\Or t_2)}=v_2, \Value{(t_1)}=\bot\wedge\Value{(t_2)}=v_2$ By T-Or we have that $t_1\Or t_2:\Task\tau$, and $t_1:\Task\tau$ and $t_2:\Task\tau$. By the induction hypothesis, we have that $v_2:\tau$.\\

  \noindent\textbf{Case} $\Value{(t_1\Or t_2)}=\bot, \Value{(t_1)}=\bot\wedge\Value{(t_2)}=\bot$ Since this case does not lead to a value, the lemma holds trivially.\\

  \noindent\textbf{Case} $\Value{(t_1\Xor t_2)}=\bot$ Since this case does not lead to a value, the lemma holds trivially.\\
\end{proof}

\begin{proof}
  We prove Theorem~\ref{thmpresnorm} by induction on $e$:\\

  \noindent\textbf{Case} $\userule{S-Fail}$ Since this case does not alter the
  expression, the theorem holds trivially.\\

  \noindent\textbf{Case} $\userule{S-Xor}$ Since this case does not alter the
  expression, the theorem holds trivially.\\

  \noindent\textbf{Case} $\userule{S-Update}$ Since this case does not alter
  the expression, the theorem holds trivially.\\

  \noindent\textbf{Case} $\userule{S-Fill}$ Since this case does not alter the
  expression, the theorem holds trivially.\\

  \noindent\textbf{Case} $\userule{S-Edit}$ Since this case does not alter the
  expression, the theorem holds trivially.\\

  \noindent\textbf{Case} $\userule{S-And}$ Given that $t_1\And t_2:\Task(\tau_1\times\tau_2)$, by T-And we have $t_1:\tau_1$ and $t_2:\tau_2$. By the induction hypothesis, we also have $t_1':\tau_1$ and $t_2':\tau_2$. This gives us that $t_1'\And t_2':\Task(\tau_1\times\tau_2)$.\\

  \noindent\textbf{Case} $\userule{S-Next}$ Given that $e_1\Next e_2:\Task \tau$, T-Then gives us that $t_1:\Task\tau_1$
  and $e_2:\tau_1 \to \Task \tau$. By the induction hypothesis, we know that
  $t_1\stride t_1'$ preserves and thus $t_1':\Task\tau_1$. Therefore
  $t_1'\Next e_2:\Task\tau$.\\\\

  \noindent\textbf{Case} $\userule{S-OrLeft}$ Given that $t_1\Or t_2:\Task\tau$,
  by T-Or we have $t_1:\Task\tau$. By the induction hypothesis, we know that
  $t_1\stride t_1'$ preserves and thus $t_1':\Task\tau$.\\

  \noindent\textbf{Case} $\userule{S-OrRight}$ Given that $t_1\Or t_2:\Task\tau$,
  by T-Or we have $t_2:\Task\tau$. By the induction hypothesis, we know that
  $t_2\stride t_2'$ preserves and thus $t_2':\Task\tau$. \todo{there might be
  more to be said here. t1 obviously can to stuff too, but we don't say anyting about this.}\\

  \noindent\textbf{Case} $\userule{S-OrNone}$ Given that $t_1\Or t_2:\Task\tau$,
  by T-Or we have $t_1:\Task\tau$ and $t_2:\Task\tau$. By the induction hypothesis,
  we know that $t_1\stride t_1'$ and $t_2\stride t_2'$ preserve, and thus
  $t_1'\Or t_2':\Task\tau$.\\

  \noindent\textbf{Case} $\userule{S-ThenStay}$ Given that $t_1\Then e_2:\Task\tau$,
  by T-Then we have $t_1:\Task\tau_1$ and $e_2:\tau_1\to\Task\tau$. By the induction
  hypothesis, we know that $t_1\stride t_1'$ preserves, and thus $t_1'\Then e_2:\Task\tau$.\\

  \noindent\textbf{Case} $\userule{S-ThenFail}$ Given that $t_1\Then e_2:\Task\tau$,
  by T-Then we have $t_1:\Task\tau_1$ and $e_2:\tau_1\to\Task\tau$. By the induction
  hypothesis, we know that $t_1\stride t_1'$ preserves, and thus $t_1'\Then e_2:\Task\tau$.\\

  \noindent\textbf{Case} $\userule{S-ThenCont}$Given that $t_1\Then e_2:\Task\tau$,
  by T-Then we have $t_1:\Task\tau_1$ and $e_2:\tau_1\to\Task\tau$. By the induction
  hypothesis, we know that $t_1\stride t_1'$ preserves. By
  Lemma~\ref{lemmavaluepreserves}, we know that $\Value{(t_1')}=v_1$ preserves.
  By Theorem~\ref{thmpreseval} we know that $e_2 v_1\evaluate t_2$ preserves. And
  finally by the induction hypothesis, we know that $t_2\stride t_2'$ preserves.
  Therefore $t_2':\Task\tau$.\\

\end{proof}

\begin{proof}
  We prove Theorem~\ref{thmpreshandle} by induction on $e$:

  \noindent\textbf{Case} $\userule{H-Change}$ Given that $\Edit v:\Task\beta$ and $v,v':\beta$, we have that $\Edit v':\Task\beta$\\

  \noindent\textbf{Case} $\userule{H-Empty}$ Given that $\Edit v:\Task\beta$, then by T-Enter we have $\Enter \beta:\Task\beta$ \\

  \noindent\textbf{Case} $\userule{H-Fill}$ \\

  \noindent\textbf{Case} $\userule{H-Up}$ \\

  \noindent\textbf{Case} $\userule{H-PickLeft}$ \\

  \noindent\textbf{Case} $\userule{H-PickRight}$ \\

  \noindent\textbf{Case} $\userule{H-Next}$ \\

  \noindent\textbf{Case} $\userule{H-PassThen}$ \\

  \noindent\textbf{Case} $\userule{H-PassNext}$ \\

  \noindent\textbf{Case} $\userule{H-FirstAnd}$ \\

  \noindent\textbf{Case} $\userule{H-SecondAnd}$ \\

  \noindent\textbf{Case} $\userule{H-FirstOr}$ \\

  \noindent\textbf{Case} $\userule{H-SecondOr}$
\end{proof}
\subsection{???}

\begin{align*}
  \forall e:\tau & e \stride e' &\implies& \text{ either Fail}(e') \text{ or } \exists e'',i' \text{ such that } e'\handle{i'}e''&\\
\end{align*}

\begin{theorem}
  $\forall e:\tau i\in\Inputs{(e)} \implies e\handle{i}e'$
\end{theorem}

\begin{proof}
  \noindent\textbf{Case} $\Edit v:\Task\tau$ By definition, $\Inputs{(\Edit v)}=\{v',\Empty\}$. For $i=v'$, we have that $\userule{H-Change}$ handles the input, and for $i=\Empty$, we have $\userule{H-Empty}$.\\

  \noindent\textbf{Case} $\Enter \tau$ By definition, $\Inputs{(\Enter \tau)}=\{v':\tau\}$. For $i=v'$, we have that $\userule{H-Fill}$ handles the input.\\

  \noindent\textbf{Case} $\Update l:\Task\tau$ By definition, $\Inputs{(\Update\tau)}=\{v'\}$. For $i=v'$, we have that $\userule{H-Update}$ handles the input.\\

  \noindent\textbf{Case} $\Fail$ By definition, $\Inputs{(\Fail)}=\{\}$. The theorem holds trivially.\\

  \noindent\textbf{Case} $t_1\Then e_2$ By definition, $\Inputs{(t_1\Then e_2)}=\Inputs{(t_1)}$. By the induction hypothesis, we have that $i\in\Inputs{(t_1)}$, then $t_1\handle{i}t_1'$. Therefore, we also have $\userule{H-PassThen}$.\\

  \noindent\textbf{Case} $t_1\Next e_2$ By definiton, $\Inputs(t_1 \Next e_2) = \Inputs(t_1) \cup \set{\Continue\mid \Value{(t_1)}\neq \bot \wedge \neg\Failing{(e_2 \Value{(t_1)}\stride)}}$. If $i=\Continue$, then we know that $\Value{(t_1)}\neq \bot \wedge \neg\Failing{(e_2 \Value{(t_1)}\stride)}$ holds, and therefore H-Next applies. Otherwise, we have by induction hypothesis that $i\in\Inputs{(t_1)}$, then $t_1\handle{i}t_1'$. Therefore, we also have $\userule{H-PassNext}$.\\

  \noindent\textbf{Case} $t_1\And t_2$ By definiiton, $\Inputs(t_1\And t_2) = \set{\First\ i \mid i \in \Inputs(t_1)} \cup \set{\Second\ i \mid i \in \Inputs(t_2)}$. By the induction hypothesis, we have both that if $i\in\Inputs(t_1)$ then $t_1\handle{i}t_1'$ and that if $i\in\Inputs(t_2)$ then $t_2\handle{i}t_2'$. Therefore, if $i\in\Inputs(t_1\And t_2)$, then either $\userule{H-FirstAnd}$ or $\userule{H-SecondAnd}$.\\

  \noindent\textbf{Case} $t_1\Or t_2$ By definition, $\Inputs(t_1\Or t_2) = \set{\First\ i \mid i \in \Inputs(t_1)} \cup \set{\Second\ i \mid i \in \Inputs(t_2)}$. By the induction hypothesis, we have both that if $i\in\Inputs(t_1)$ then $t_1\handle{i}t_1'$ and that if $i\in\Inputs(t_2)$ then $t_2\handle{i}t_2'$. Therefore, if $i\in\Inputs(t_1\Or t_2)$, then either $\userule{H-FirstOr}$ or $\userule{H-SecondOr}$.\\

  \noindent\textbf{Case} $t_1\Xor t_2$ By definition, $\Inputs(t_1\Xor t_2) = \set{\Pick \Left, \Pick \Right}$. In case $i=\Pick\Left$, H-PickLeft applies, in case $i=\Pick\Right$, H-PickRight applies.

\end{proof}

\begin{theorem}
  $\forall e:\tau, i e\handle{i}e' \implies i\in\Inputs{(e)}$
\end{theorem}

\begin{proof}
  \noindent\textbf{Case} $e=\Edit v:\Task\tau, i= v':\tau$\\
  \noindent\textbf{Case} $e=\Edit v:\Task\tau, i= \Empty$\\
  \noindent\textbf{Case} $e=\Enter \tau, i= v':\tau$\\
  \noindent\textbf{Case} $e=\Update l:\Task\tau, i= v':\tau$\\
  \noindent\textbf{Case} $e=t_1\Or t_2 , i= \Pick\Left r $\\
  \noindent\textbf{Case} $e=t_1\Or t_2 , i= \Pick\Right r $\\
  \noindent\textbf{Case} $e=t, i= \Pick\Here $\\
  \noindent\textbf{Case} $e=t_1\Next e_2 , i= \Continue $\\
  \noindent\textbf{Case} $e=t_1\Then e_2 , i$\\
  \noindent\textbf{Case} $e=t_1\Next e_2 , i\neq\Continue$\\
  \noindent\textbf{Case} $e=t_1\And t_2 , i=\First i$\\
  \noindent\textbf{Case} $e=t_1\And t_2 , i=\Second i$\\
  \noindent\textbf{Case} $e=t_1\Or t_2 , i=\First i$\\
  \noindent\textbf{Case} $e=t_1\Or t_2 , i=\Second i$\\
\end{proof}


\chapter{Scaling Behavior}

\section{Introduction}

     In the field of radioactive materials transportation cask design,
testing of scale models is very important.  In order to successfully
utilize the results of scale model tests, the appropriate parameters
and techniques for extrapolating the results to full-scale behavior
must be identified.  Because the accelerations of practical interest
are sufficiently high, the effects of a one G (gravity) field are
ignored in the following discussion.

\section{Simplified Analysis for Linear Systems}

     For linear behavior, a simplified engineering approach to scaling
relations can be applied.  For radioactive materials transportation
cask applications, the linear accelerations at various locations along
the cask length tend to be the most important parameters.  These
accelerations are used primarily to assess inertial loads on bolted
closures, cask contents and other components.  Therefore, the
discussion below centers on the scaling relations for acceleration
values.  A more complete description of scaling relations can be found
in Table 7.1.

Linear acceleration of the center-of-gravity is described
by the equation relating force ($F$), mass ($M$),
and acceleration ($a$):

\begin{equation}
                 F = M \cdot a.
\end{equation}

Angular acceleration about the center-of-gravity is similarly
described by the equation relating torque ($T$), moment of
inertia ($I$), and angular acceleration ($\alpha$):

\begin{equation}
                 T = I \cdot \alpha.
\end{equation}

     For linear acceleration, the forces are proportional to the
square of the linear dimensions of the body.  The mass is proportional
to the cube of the linear dimensions.  Therefore, the acceleration of
a full-scale object should be half that of a half-scale object.

     For angular acceleration, the torques are proportional to the
cube of the body scale
(T = force $\cdot$ length, where force $\propto$ length$^{2}$).
The moment of inertia is
proportional to the fifth power of the body scale
(I = mass $\cdot$ length$^{2}$, where mass $\propto$ length$^{3}$).
Therefore, the angular acceleration of a full-scale object
should be one fourth that of a half-scale object.  However, the linear
acceleration of the end of an object is equal to the angular
acceleration times a length dimension.  Thus, the linear acceleration
of the tip of a rotating full-scale object should again be half that
of a half-scale object.  Note, for simplicity of the argument and
because we are primarily concerned with the tangential component of
acceleration for shallow angle slapdown events, the inward component
of acceleration (centripetal acceleration) due to the square of the
angular velocity times the length has been neglected in this argument.
Inclusion of centripetal acceleration does not change the stated
conclusion.

\section{Numerical Confirmation Using \SLAP}

     Nonlinear behavior of the structure at the impact locations is
much more common than linear behavior.  Nonlinear behavior arises from
both geometric and material properties sources.  The nonlinear
behavior is easily incorporated into numeric solution procedures such
as those found in the slapdown analysis program, \SLAP , discussed in
this report.  \SLAP\ will therefore be used to address the effects
of nonlinear behavior on the scaling relations for slapdown impact
events.  Two sets of analyses will be performed.  First, an analysis
of linear behavior will be compared to the results of the simple
engineering analysis described above.  Then, an analysis of nonlinear
behavior will determine the effects of nonlinearity on the scaling
relations derived for linear behavior.

\subsection{Linear Springs}

     A simple solid cylinder was selected to demonstrate the
scaling relations.  No system of units was assumed, thus any
consistent units may be applied. In full-scale, the cylinder
was given
a mass of 80, a length of 120 units, and a radius of
30 units resulting in a
moment of inertia of 114,000.
An identical spring at each end of the
cylinder had a travel of 10 units with a spring rate of
600,000 and an identical
unloading modulus (that is the spring was linear on both loading and
unloading with no energy dissipation). This cylinder
was impacted onto a
rigid target at an initial angle of 10$^\circ$ with an initial velocity
of 527.45.  This velocity was selected,
based on the author's bias, because it is consistent, in units of
in/sec, with the NRC 30-foot free drop
hypothetical accident condition. A solid cylinder consistent
with the half-
scale of the cylinder described above has a mass of 10,
a length of 60 units,
and a radius of 15 units
for a moment of inertia of 3563.  In half-scale,
the spring travel was 5 units with a spring rate of
300,000 for both loading
and unloading.  The initial impact conditions were identical with
those of the
full-scale cylinder.  The model description parameters are listed in
Table 7.1 for easy comparison.

\begin{table}
\begin{center}
\caption{Comparison of Full- and Half-Scale Linear Spring Models}
\makeqnum
\begin{tabular}{||l|r|r|r||}
\hline
 &\multicolumn{1}{|c}{Full-Scale}
 &\multicolumn{1}{|c}{Half-Scale}
 &\multicolumn{1}{|c||}{Ratio}\\
 & & &\multicolumn{1}{|c||}{(Full/Half)}\\
Geometry and & & & \\
Initial Conditions: & & & \\
\quad Overall Length     &$???120.0$ &$??60.0$ &$?2.0?$\\
\quad Mass               &$????80.0$ &$??10.0$ &$?8.0?$\\
\quad Moment of Inertia  &$114000.0$ &$3563.0$ &$32.0?$\\
\quad Spring Constants: & & &\\
\quad \quad Loading             &$600000.$ &$300000.$ &$2.0?$\\
\quad \quad Unloading           &$600000.$ &$300000.$ &$2.0?$\\
\quad Initial Velocity  &$-527.5$ &$-527.5$ &$1.0?$\\
\quad Initial Angle     &$??10.0$ &$??10.0$ &$1.0?$\\
\hline
Results: & & & \\
Maximum Accelerations & & & \\
\quad    Nose            &$?85720.$ &$?171400.$ &$0.5?$\\
                    &$-67780.$ &$-135600.$ &$0.5?$\\
\quad    Tail            &$158400.$ &$316700.$ &$0.5?$\\
                    &$-36610.$ &$-73210.$ &$0.5?$\\
\quad    C.-G.            &$45300.$ &$90590.$ &$0.5?$\\
                     &$????0.$ &$????0.$ &$0.5?$\\
\quad    Angular          &$?1893.$ &$?7583.$ &$0.25$\\
                     &$-1023.$ &$-4090.$ &$0.25$\\
Maximum Velocities & & & \\
\quad    Nose              &$?536.4$ &$?536.4$ &$1.0?$\\
                      &$-527.5$ &$-527.5$ &$1.0?$\\
\quad    Tail          &$???1.7$ &$???1.7$ &$1.0?$\\
                  &$-983.2$ &$-983.2$ &$1.0?$\\
\quad    C.-G.            &$??56.7$ &$??56.7$  &$1.0?$\\
                     &$-527.5$ &$-527.5$  &$1.0?$\\
\quad    Angular            &$??0.0$ &$??0.0$ &$0.5?$\\
                       &$-12.7$ &$-25.3$ &$0.5?$\\
Maximum Displacements & & & \\
\quad    Nose            &$3.28$ &$1.64$ &$2.0?$\\
\quad    Tail            &$6.04$ &$3.02$ &$2.0?$\\
\hline
\end{tabular}
\end{center}
\end{table}

     \SLAP\ analysis results show that the sequence of
events occurs at twice the speed for the half-scale cylinder
as for the
full-scale cylinder.  The displacements for the half-scale cylinder
are half the full-scale cylinder's
displacements.
Linear velocities are the same for
both half- and full-scale cylinders, and linear accelerations of the
half-scale cylinder are double those of the full-scale cylinder.
The angular
velocities of the half-scale cylinder are double those of the full-scale
cylinder, and the angular accelerations for
the half-scale cylinder are four times those
for the full-scale cylinder.
Thus, if an object has a non-zero angular velocity
at impact, the half-scale object must have
double the initial angular velocity of the full-scale object,
for proper scaling.  These
results are shown in Figures 7.1-7.3.  The maximum values of
displacement, velocity, and acceleration are shown in Table 7.1.

\begin{figure}
\vspace{3.5 in}
\caption{Nose, Center-of-Gravity, and Tail Displacements versus Time
for Full- and Half-Scale Cylinders with Linear Springs}
\end{figure}

\begin{figure}
\vspace{3.5 in}
\caption{Nose, Center-of-Gravity, and Tail Velocities versus Time
for Full- and Half-Scale Cylinders with Linear Springs}
\end{figure}

\begin{figure}
\vspace{3.5 in}
\caption{Nose, Center-of-Gravity, and Tail Accelerations versus Time
for Full- and Half-Scale Cylinders with Linear Springs}
\end{figure}

\subsection{Nonlinear Springs}

     To investigate the nonlinear behavior, the cylinders described
above were again used.  The only differences were the springs.  For
the full-scale cylinder,
a spring with an initial spring rate of 750,000 for
the initial travel of 2 units
followed by a tangent spring rate of 0 for the
next 8 units
was selected.  Thus the force curve rose linearly to a value of
1,500,000
and then remained at that value for the remainder of the
compression.  An unloading modulus of 600,000 was again selected.
Since unloading occurs linearly from the maximum load reached, the
spring selected has the potential to absorb a considerable amount of
energy.  The half-scale spring has an initial spring rate of 375,000
over the first unit
of displacement. This force remains constant for the
next 4 units of displacement.  The unloading modulus is 300,000.  The
initial conditions for the impact event were identical with those
described above for the linear analysis.  The impact initial
conditions and model description are shown in Table 7.2.

\begin{table}
\begin{center}
\caption{Comparison of Full- and Half-Scale Nonlinear Spring Models}
\makeqnum
\begin{tabular}{||l|r|r|r||}
\hline
 &\multicolumn{1}{|c}{Full-Scale}
 &\multicolumn{1}{|c}{Half-Scale}
 &\multicolumn{1}{|c||}{Ratio}\\
 & & &\multicolumn{1}{|c||}{(Full/Half)}\\
Geometry and & & & \\
Initial Conditions: & & & \\
\quad Overall Length     &$???120.0$ &$??60.0$ &$?2.0?$\\
\quad Mass               &$????80.0$ &$??10.0$ &$?8.0?$\\
\quad Moment of Inertia  &$114000.0$ &$3563.0$ &$32.0?$\\
\quad Spring Constants: & & &\\
\quad \quad Loading section 1   &$750000.?$ &$375000.?$ &$2.0?$\\
\quad \quad Loading section 2   &$     0.?$ &$     0.?$ &$2.0?$\\
\quad \quad Unloading           &$600000.?$ &$300000.?$ &$2.0?$\\
\quad Initial Velocity  &$-527.5$ &$-527.5$ &$1.0?$\\
\quad Initial Angle     &$??10.0$ &$??10.0$ &$1.0?$\\
\hline
Results: & & & \\
Maximum Accelerations & & & \\
\quad    Nose    &$65630.?$ &$131300.?$ &$0.5?$\\
            &$-28760.?$ &$-57530.?$ &$0.5?$\\
\quad    Tail    &$66260.?$ &$132500.?$ &$0.5?$\\
            &$-28130.?$ &$-56260.?$ &$0.5?$\\
\quad    C.-G.   &$18750.?$ &$37500.?$ &$0.5?$\\
            &$0.?$    &$0.?$    &$0.5?$\\
\quad    Angular &$788.5$  &$3154.$  &$0.25$\\
            &$-781.5$ &$-3126.$ &$0.25$\\
Maximum Velocities & & & \\
\quad    Nose &$409.5???$ &$409.5???$ &$1.0?$\\
         &$-527.5???$ &$-527.5???$ &$1.0?$\\
\quad    Tail &$7.5???$ &$7.5???$ &$1.0?$\\
         &$-928.8???$ &$-928.8???$ &$1.0?$\\
\quad   C.-G. &$6.6???$ &$6.6???$ &$1.0?$\\
         &$-527.5???$ &$-527.5???$ &$1.0?$\\
\quad Angular &$0.0158$ &$???0.0316$ &$0.5?$\\
         &$-11.15??$ &$-22.3???$ &$0.5?$\\
Maximum Displacements & & & \\
\quad    Nose &$3.16$ &$1.58$ &$2.0?$\\
\quad    Tail &$7.53$ &$3.77$ &$2.0?$\\
\hline
\end{tabular}
\end{center}
\end{table}

     The results for the nonlinear springs showed exactly the same
behavior as described for the linear springs.  Nonlinear behavior is
shown in Figures 7.4-7.6 and the maximum values in Table 7.2.

\begin{figure}
\vspace{3.5 in}
\caption{Nose, Center-of-Gravity, and Tail Displacements versus Time
for Full- and Half-Scale Cylinders with Nonlinear Springs}
\end{figure}

\begin{figure}
\vspace{3.5 in}
\caption{Nose, Center-of-Gravity, and Tail Velocities versus Time
for Full- and Half-Scale Cylinders with Nonlinear Springs}
\end{figure}

\begin{figure}
\vspace{3.5 in}
\caption{Nose, Center-of-Gravity, and Tail Accelerations versus Time
for Full- and Half-Scale Cylinders with Nonlinear Springs}
\end{figure}

\section{Conclusions}

     In this section, we have shown that the results of impact testing
scale models can easily be related to the behavior of full-scale
objects.  Model displacements can be related to full-scale object
displacements by multiplying by the model scale.  Model linear
accelerations can be related to full-scale object linear accelerations
by dividing by the model scale.  Model angular accelerations can be
related to those of the full-scale object by dividing by the square of
the model scale. Linear velocities are identical between model and
full-scale objects regardless of scale.  Angular velocities scale in
the manner of linear accelerations.  The scaling relations for
velocities indicate that the initial conditions for a model impact
test should be identical to those expected for the full-scale event
except for initial angular velocity.  Initial angular velocity for the
model should be inversely proportional to the scale of the model used.
These scaling relations are summarized in Table 7.2.
\begin{table}
\begin{center}
\caption{Summary of Relationships for Scale Model Testing}
\makeqnum
\begin{tabular}{||l|c||}
\hline
\multicolumn{1}{|c}{Parameter}
 &\multicolumn{1}{|c||}{Scaling Relationships}\\
Geometry and & \\
Initial Conditions: &\\
\quad Overall Length     & $l_{sm} = l_{fs} \times (Scale)^{1}$\\
\quad Mass               & $M_{sm} = M_{fs} \times (Scale)^{3}$\\
\quad Moment of Inertia  & $I_{sm} = I_{fs} \times (Scale)^{5}$\\
\quad Spring Constants   & $K_{sm} = K_{fs} \times (Scale)^{1}$\\
\quad Initial Velocity   & $V_{sm} = V_{fs} \times (Scale)^{0}$\\
\quad Initial Angle      & $\theta _{sm} = \theta _{fs} \times
(Scale)^{0}$\\
\hline
Results: & \\
\quad    Linear Accelerations  & $a_{sm} = a_{fs} \times (Scale)^{-1}$\\
\quad    Angular Accelerations & $\alpha _{sm} = \alpha _{fs} \times
(Scale)^{-2}$\\
\quad    Linear Velocities     & $V_{sm} = V_{fs} \times (Scale)^{0}$\\
\quad    Angular Velocities    & $\omega _{sm} = \omega _{fs} \times
(Scale)^{-1}$\\
\quad    Linear Displacements  & $\Delta _{sm} = \Delta _{fs} \times
(Scale)^{1}$\\
\quad    Angular Displacements & $\theta _{sm} = \theta _{f} \times
(Scale)^{0}$\\
\hline
\end{tabular}
\end{center}
\end{table}


\chapter{Equilibrium}
\index{Equilibrium%
@\emph{Equilibrium}}%

%\pagestyle{empty}

This chapter defines the model equilibria.  We define both the stationary-steady-state equilibrium as well as the stationary non-steady state equilibrium.  Our solution method, described in the next chapter, uses both of these concepts.

 \section{Market clearing and stationary equilibrium}\label{SecMCEqlbm}

    Labor market clearing requires that aggregate demand for effective labor units $EL_t$  equal the sum of individual efficiency units of labor supplied, $e_{j,s}n_{j,s,t}$. Asset market clearing requires that aggregate asset holdings equal the total about of debt and equity asset outstanding. Aggregate consumption $C_t$ is defined as the sum of all individual consumption, and aggregate investment is defined by the resource constant, $Y = C + I$. In particular, we have market clearing in the labor, asset, and goods markets:
    \begin{align}
     & \sum_{m=1}^{M} EL_{m,c,t} +  \sum_{m=1}^{M} EL_{m,nc,t} + EL^{G}_{t} = \sum_{s=E+1}^{E+S}\sum_{j=1}^{J} \omega_{s,t}\lambda_j e_{j,s}n_{j,s,t} \quad \forall t \label{EqMktClrLab} \\
      & D_{t} +  \sum_{m=1}^{M} B_{m,c,t} +  \sum_{m=1}^{M} B_{m,nc,t} + \sum_{m=1}^{M} V_{m,c,t} +  \sum_{m=1}^{M} V_{m,nc,t}  = \sum_{s=E+2}^{E+S+1}\sum_{j=1}^{J}\omega_{s-1,t-1}\lambda_j b_{j,s,t}  \quad \forall t \label{EqMktClrAsset} \\
   &   \sum_{m=1}^{M} X_{m,c,t} +  \sum_{m=1}^{M} X_{m,nc,t}   = \sum_{s=E+1}^{E+S}\sum_{j=1}^{J}\sum_{i=1}^{I}\omega_{s,t}\lambda_j c_{i,j,s,t} + \sum_{m=1}^{M} I_{m,c,t} +  \sum_{m=1}^{M} I_{m,nc,t} + I^{G}_{t}  \quad\forall t \\
 \label{EqMktClrGoods}
    \end{align}

    The usual definition of equilibrium would be allocations and prices such that households optimize \eqref{Eqcfoc}, \eqref{Eqnfoc}, and \eqref{Eqbfoc}, firms optimize \eqref{eqn:foc_i} and \eqref{eqn:foc_l}, and markets clear \eqref{EqMktClrLab}, \eqref{EqMktClrAsset}, and \eqref{EqMktClrGoods}. However, the variables in these characterizing equations are potentially not stationary due to the possible growth rate in the total population $g_{n,t}$ each period coming from the cohort growth rates in \eqref{EqPopLawofmotion} and from the deterministic growth rate of labor augmenting technological change $g_y$ in \eqref{eqn:prod_fun}.

    \begin{table}[htbp] \centering \captionsetup{width=3.3in}
    \caption{\label{TabStatVars}\textbf{Stationary variable definitions}}
      \begin{threeparttable}
      \begin{tabular}{>{\small}c >{\small}c >{\small}c |>{\small}c}
        \hline\hline
        \multicolumn{3}{c}{Sources of growth} & Not \\
        & & & \\[-4mm]
        $e^{g_y t}$ & $\tilde{N}_t$ & $e^{g_y t}\tilde{N}_t$ & growing\tnote{a} \\
        \hline
        & & \\[-4mm]
        $\hat{c}_{j,s,t}\equiv\frac{\tilde{c}_{j,s,t}}{e^{g_y t}}$ & $\hat{\omega}_{s,t}\equiv\frac{\omega_{s,t}}{\tilde{N}_t}$ & $\hat{X}_t\equiv\frac{X_t}{e^{g_y t}\tilde{N}_t}$ & $n_{j,s,t}$ \\[2mm]
        $\hat{b}_{j,s,t}\equiv\frac{b_{j,s,t}}{e^{g_y t}}$ & $\hat{EL}_t\equiv\frac{EL_t}{\tilde{N}_t}$ & $\hat{K}_t\equiv\frac{K_t}{e^{g_y t}\tilde{N}_t}$ & $r_t$ \\[2mm]
        $\hat{w}_t\equiv\frac{w_t}{e^{g_y t}}$ &  & $\hat{BQ}_{j,t}\equiv\frac{BQ_{j,t}}{e^{g_y t}\tilde{N}_t}$ &  \\[2mm]
        $\hat{y}_{j,s,t}\equiv \frac{y_{j,s,t}}{e^{g_y t}}$ &  & $\hat{I}_{t}\equiv \frac{I_{t}}{e^{g_{y} t}\tilde{N}_t}$ &  \\[2mm]
        $\hat{T}_{j,s,t}\equiv\frac{T_{j,s,t}}{e^{g_y t}}$ &  &  &  \\[2mm]
        $\hat{p}_{s,t}\equiv\frac{\tilde{p}_{s,t}}{e^{g_y t}}$ &  &  &  \\[2mm]
        $\hat{p}_{i,t}\equiv\frac{p_{i,t}}{e^{g_y t}}$ &  &  &  \\[2mm]
        \hline\hline
      \end{tabular}
      \begin{tablenotes}
        \scriptsize{\item[a]The interest rate $r_t$ is already stationary because $X_t$ and $K_t$ grow at the same rate. Individual labor supply, $n_{j,s,t}$, is stationary.}
      \end{tablenotes}
      \end{threeparttable}
    \end{table}

%% Might need more variables in the above

    Table \ref{TabStatVars} characterizes the stationary versions of the variables of the model in terms of the variables that grow due to labor augmenting technological change, population growth, both, or none. With the definitions in Table \ref{TabStatVars}, it can be shown that the equilibrium characterizing equations can be written in stationary form in the following way. The static and intertemporal Euler equations from the individual's optimization problem corresponding to \\eqref{Eqcfoc}, \eqref{Eqnfoc}, and \eqref{Eqbfoc} are the following:

    NEED TO UPDATE EQUATIONS...need individual and firm conditions, stationarized...

%    \begin{equation}\label{EqEulerLabStat}
%      \begin{split}
%        &(\hat{c}_{j,s,t})^{-\sigma}\Biggl(\hat{w}_t e_{j,s} - \frac{\partial\hat{T}_{j,s,t}}{\partial n_{j,s,t}}\Biggr) = \chi^n_{s}\biggl(\frac{b}{\tilde{l}}\biggr)\biggl(\frac{n_{j,s,t}}{\tilde{l}}\biggr)^{v-1}\Biggl[1 - \biggl(\frac{n_{j,s,t}}{\tilde{l}}\biggr)\Biggr]^{\frac{1-v}{v}} \\
%        &\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\forall j,t, \quad\text{and}\quad E+1\leq s\leq E+S \\
%        &\qquad\text{where}\quad \hat{c}_{j,s,t} = \left(1 + r_t\right)\hat{b}_{j,s,t} + \hat{w}_t e_{j,s}n_{j,s,t} + \frac{\hat{BQ}_{j,t}}{\lambda_j} - e^{g_y}\hat{b}_{j,s+1,t+1} - \hat{T}_{j,s,t} \\
%        &\qquad\text{and}\quad \frac{\partial \hat{T}_{j,s,t}}{\partial n_{j,s,t}} = \hat{w}_t e_{j,s}\biggl[\tau^I\bigl(F\hat{a}_{j,s,t}\bigr) + \frac{F\hat{a}_{j,s,t}CD\bigl[2A(F\hat{a}_{j,s,t})+B\bigr]}{\bigl[A(F\hat{a}_{j,s,t})^2+B(F \hat{a}_{j,s,t})+C\bigr]^2} + \tau^P\Biggr] \\
%        &\qquad\text{and}\quad \hat{b}_{j,E+1,t} = 0 \quad\forall j,t
%      \end{split}
%    \end{equation}
%
%    \begin{equation}\label{EqEulerSavStat}
%      \begin{split}
%        &(\hat{c}_{j,s,t})^{-\sigma} = ... \\
%        &e^{-g_y\sigma}\Biggl(\rho_s\chi^b \bigl(\hat{b}_{j,s+1,t+1}\bigr)^{-\sigma} + \beta(1-\rho_s)(\hat{c}_{j,s+1,t+1})^{-\sigma}\Biggl[(1 + r_{t+1}) - \frac{\partial T_{j,s+1,t+1}}{\partial b_{j,s+1,t+1}}\Biggr]\Biggr) \\
%        &\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\forall j,t,\quad\text{and}\quad E+1\leq s \leq E+S-1 \\
%        &\qquad\text{where}\quad \frac{\partial T_{j,s+1,t+1}}{\partial b_{j,s+1,t+1}} = ...\\
%        &\qquad\qquad r_{t+1}\Biggl(\tau^I(F\hat{a}_{j,s+1,t+1}) + \frac{F\hat{a}_{j,s+1,t+1}CD\left[2A(F\hat{a}_{j,s+1,t+1}) + B\right]}{\left[A(F\hat{a}_{j,s+1,t+1})^2 + B(F\hat{a}_{j,s+1,t+1}) + C\right]^2}\Biggr) ... \\
%        &\qquad\qquad \tau^W(\hat{b}_{j,s+1,t+1}) + \frac{\hat{b}_{j,s+1,t+1}PHM}{\left(H\hat{b}_{j,s+1,t+1} + M\right)^2}
%      \end{split}
%    \end{equation}
%
%    \begin{equation}\label{EqEulerSavEpSstat}
%      (\hat{c}_{j,E+S,t})^{-\sigma} = \chi^b e^{-g_y\sigma}(\hat{b}_{j,E+S+1,t+1})^{-\sigma} \quad\forall j,t
%    \end{equation}
%
%    The stationary firm first order conditions for optimal labor and capital demand corresponding to \eqref{EqFOCwage} and \eqref{EqFOCrate} are the following.
%    \begin{equation}\label{EqFOCwageStat}
%       \hat{w}_t = (1-\alpha)\frac{\hat{Y}_t}{\hat{L}_t} \quad \forall t
%    \end{equation}
%    \begin{equation}\tag{\ref{EqFOCrate}}
%       r_t = \alpha\frac{\hat{Y}_t}{\hat{K}_t} - \delta = \alpha\frac{Y_t}{K_t} - \delta \quad \forall t
%    \end{equation}
%    And the two stationary market clearing conditions corresponding to \eqref{EqMktClrLab} and \eqref{EqMktClrCap}---with the goods market clearing by Walras' Law---are the following.
%    \begin{align}
%      \hat{L}_t &= \sum_{s=E+1}^{E+S}\sum_{j=1}^{J} \hat{\omega}_{s,t}\lambda_j e_{j,s}n_{j,s,t} \quad \forall t \label{EqMktClrLabStat} \\
%      \hat{K}_t &= \frac{1}{1 + \tilde{g}_{n,t}}\left(\sum_{s=E+2}^{E+S+1}\sum_{j=1}^{J}\hat{\omega}_{s-1,t-1}\lambda_j \hat{b}_{j,s,t}\right) \quad \forall t \label{EqMktClrCapStat}
%    \end{align}
%    where $\tilde{g}_{n,t}$ is the growth rate in the working age population between periods $t-1$ and $t$ described in \eqref{EqPopWkGrowth}.

    We can now define the stationary steady-state equilibrium for this economy in the following way.

   \ \\

    \hrule
    \begin{definition}[\textbf{Stationary steady-state equilibrium}]\label{DefEquilSS}
      A non-autarkic stationary steady-state equilibrium in the overlapping generations model with $S$-period lived agents and heterogeneous ability $e_{j,s}$ is defined as constant allocations $n_{j,s,t}=\bar{n}_{j,s}$ and $\hat{b}_{j,s+1,t+1}=\bar{b}_{j,s+1}$ and constant prices $\hat{w}_t=\bar{w}$ and $r_t=\bar{r}$ for all $j$, $s$, and $t$ such that the following conditions hold:
       \begin{enumerate}
          \item households optimize according to \eqref{EqEulerLabStat}, \eqref{EqEulerSavStat}, and \eqref{EqEulerSavEpSstat},
          \item firms optimize according to \eqref{EqFOCwageStat} and \eqref{EqFOCrate},
          \item markets clear according to\eqref{EqMktClrLab}, \eqref{EqMktClrAsset}, and \eqref{EqMktClrGoods}, and
          \item the population has reached its stationary steady state distribution $\bar{\omega}_s$ for all ages $s$, characterized in Chapter \ref{AppPopGrowth}.
       \end{enumerate}
    \end{definition}
    \hrule

\ \\

    The steady-state equilibrium is characterized by the system of $2JS+4M+...$ equations and $2JS+4M+...$ unknowns $\bar{n}_{j,s}$, $\bar{b}_{j,s+1}$, $EL_{m,c,t}$, $I_{m,c,t}$,... . Chapter \ref{chap:model_soln} details how to solve for the steady-state equilibrium.

%    \begin{table}[htbp] \centering \captionsetup{width=4.7in}
%    \caption{\label{TabExogVars}\textbf{List of exogenous variables and baseline calibration values}}
%      \begin{threeparttable}
%      \begin{tabular}{>{\footnotesize}c |>{\footnotesize}l |>{\footnotesize}c}
%        \hline\hline
%        Symbol & \quad\quad\quad\quad Description & Value \\
%        \hline
%        $\bm{\hat{\Gamma}}_1$ & Initial distribution of savings & $\bm{\bar{\Gamma}}$ \\
%        $N_0$ & Initial population & 1 \\
%        $\{\omega_{s,0}\}_{s=1}^S$ & Initial population by age & (see App. \ref{AppPopGrowth}) \\
%        $\{f_s\}_{s=1}^S$ & Fertility rates by age & (see App. \ref{AppPopGrowth}) \\
%        $\{i_s\}_{s=1}^S$ & Immigration rates by age & (see App. \ref{AppPopGrowth}) \\
%        $\{\rho_s\}_{s=1}^S$ & Mortality rates by age & (see App. \ref{AppPopGrowth}) \\
%        $\{e_{j,s}\}_{j,s=1}^{J,S}$ & Deterministic ability process & (see App. \ref{AppAbilCalib}) \\
%        $\{\lambda_j\}_{j=1}^J$ & Ability type bin percentages & (see App. \ref{AppAbilCalib}) \\
%        $J$ & Number of ability types & 7 \\
%        $S$ & Maximum periods in economically active & 80 \\[-2mm]
%        &\quad household life & \\
%        $E$ & Number of periods of youth economically & $\text{round}\left(\frac{S}{4}\right)$ \\[-2mm]
%        & \quad outside the model & \\
%        $R$ & Retirement age (period) & $\text{round}\left(\frac{9}{16}S\right)$ \\
%        \hline
%        $\tilde{l}$ & Maximum hours of labor supply & 1 \\
%        $\beta$ & Discount factor & $(0.96)^\frac{80}{S}$ \\
%        $\sigma$ & Coefficient of constant relative risk aversion & 3 \\
%        $b$ & Scale parameter in utility of leisure & (see App. \ref{AppEllipseUtil}) \\
%        $\upsilon$ & Shape parameter in utility of leisure & (see App. \ref{AppEllipseUtil}) \\
%        $k$ & constant parameter in utility of leisure & (see App. \ref{AppEllipseUtil}) \\
%        $\chi^n_s$ & Disutility of labor level parameter & (see App. \ref{AppChinsChib}) \\
%        $\chi^b$ & Utility of bequests level parameter & (see App. \ref{AppChinsChib}) \\
%        \hline
%        $Z$ & Level parameter in production function & 1 \\
%        $\alpha$ & Capital share of income & 0.35 \\
%        $\delta$ & Capital depreciation rate & $1-(1-0.05)^\frac{80}{S}$ \\
%        $g_y$ & Growth rate of labor augmenting & $(1+0.03)^\frac{80}{S}-1$ \\[-2mm]
%        & \quad technological progress & \\
%        \hline
%        $A$ & Coefficient on squared term in $\tau^I(\cdot)$ & (see App. \ref{AppIncTaxRate}) \\
%        $B$ & Coefficient on linear term in $\tau^I(\cdot)$ & (see App. \ref{AppIncTaxRate}) \\
%        $C$ & Constant coefficient in $\tau^I(\cdot)$ & (see App. \ref{AppIncTaxRate}) \\
%        $D$ & Level parameter for $\tau^I(\cdot)$ & (see App. \ref{AppIncTaxRate}) \\
%        $F$ & Income factor for $\tau^I(\cdot)$ & (see App. \ref{AppIncTaxRate}) \\
%        $\tau^P$ & Payroll tax rate & 0.15 \\
%        $\{\theta^j\}_{j=1}^J$ & Replacement rate by average income & (see App. \ref{AppReplRate}) \\
%        $\tau^{BQ}$ & Bequest (estate) tax rate & 0 \\
%        $P$ & Level parameter for $\tau^W(\cdot)$ & 0 \\
%        $H$ & Coefficient on linear term in $\tau^W(\cdot)$ & 0 \\
%        $M$ & Constant coefficient in $\tau^W(\cdot)$ & 0 \\
%        \hline
%        $T$ & Number of periods to steady state & 160 \\
%        $\nu$ & Dampening parameter for TPI & 0.2 \\
%        \hline\hline
%      \end{tabular}
%      % \begin{tablenotes}
%      %   \scriptsize{\item[]Note: Maybe put sources here.}
%      % \end{tablenotes}
%      \end{threeparttable}
%    \end{table}
%
%    \begin{figure}[htb]\centering \captionsetup{width=4.0in}
%      \caption{\label{FigLogSavSS}\textbf{Stationary steady-state distribution of savings $\log(\bar{\bm{\Gamma}})$ for $S=80$ and $J=7$}}
%      \fbox{\resizebox{4.0in}{3.0in}{\includegraphics{images/SavSS_log.png}}}
%    \end{figure}
%
%    \begin{figure}[htb]\centering \captionsetup{width=4.0in}
%      \caption{\label{FigLabSS}\textbf{Stationary steady-state distribution of individual labor supply $\bar{n}_{j,s}$ for $S=80$ and $J=7$}}
%      \fbox{\resizebox{4.0in}{3.0in}{\includegraphics{images/LabSS.png}}}
%    \end{figure}
%
%    \begin{figure}[htb]\centering \captionsetup{width=4.0in}
%      \caption{\label{FigLogConsSS}\textbf{Stationary steady-state distribution of consumption $\bar{c}_{j,s}$ for $S=80$ and $J=7$}}
%      \fbox{\resizebox{4.0in}{3.0in}{\includegraphics{images/ConsSS_log.png}}}
%    \end{figure}
%
%    Figure \ref{FigLogSavSS} shows the stationary steady-state distribution of individual savings $\bar{b}_{j,s}$ in logarithms, Figure \ref{FigLabSS} shows the stationary steady-state distribution of individual labor supply $\bar{n}_{j,s}$, and Figure \ref{FigLogConsSS} shows the steady-state distribution of consumption $\bar{c}_{j,s}$ in logarithms for a particular calibration of the model described in Table \ref{TabExogVars}. Notice from Figure \ref{FigLogConsSS} the hump-shaped pattern of consumption over the life cycle for each ability type, which is consistent with consumption data. Also note from the comparison of the distribution of savings in Figure \ref{FigLogSavSS} in comparison to the distribution of consumption in \ref{FigLogConsSS} that households use savings to smooth out consumption as much as possible, with a sharpe change in savings around retirement $s=R$ and only a small smooth lump in consumption at that age.

    The definition of the stationary non-steady-state equilibrium is similar to Definition \ref{DefEquilSS}, with the stationary steady-state equilibrium definition being a special case of the stationary non-steady-state equilibrium.

\ \\
WHAT FOLLOWS NEEDS UPDATING TO INCLUDE RICHER FIRM AND GOV'T, BUT IS HELPFUL IN SEEING THAT NON-SS EQ'M WILL LOOK LIKE...

\ \\

    \hrule
    \begin{definition}[\textbf{Stationary non-steady-state equilibrium}]\label{DefEquilNonSS}
      A non-autarkic stationary non-steady-state equilibrium in the overlapping generations model with $S$-period lived agents and heterogeneous ability $e_{j,s}$ is defined as allocations $n_{j,s,t}$ and $\hat{b}_{j,s+1,t+1}$ and prices $\hat{w}_t$ and $r_t$ for all $j$, $s$, and $t$ such that the following conditions hold:
       \begin{enumerate}
          \item households have symmetric beliefs $\Omega(\cdot)$ about the evolution of the distribution of savings, and those beliefs about the future distribution of savings equal the realized outcome (rational expectations),
            \begin{equation*}
              \bm{\hat{\Gamma}}_{t+u} = \bm{\hat{\Gamma}}^e_{t+u} = \Omega^u\left( \bm{\hat{\Gamma}}_t\right) \quad\forall t, \quad u\geq 1
            \end{equation*}
          \item households optimize according to \eqref{EqEulerLabStat}, \eqref{EqEulerSavStat}, and \eqref{EqEulerSavEpSstat}
          \item firms optimize according to \eqref{EqFOCwageStat} and \eqref{EqFOCrate}, and
          \item markets clear according to \eqref{EqMktClrLabStat} and \eqref{EqMktClrCapStat}.
       \end{enumerate}
    \end{definition}
    \hrule

   \ \\

    \noindent Taken together, the household labor-leisure and intended bequest decisions in the last period of life show that the optimal labor supply and optimal intended bequests for age $s=E+S$ are each functions of individual holdings of savings, total bequests received, and the prices in that period $n_{j,E+S,t}=\phi\bigl(\hat{b}_{j,E+S,t},\hat{BQ}_{j,t},\hat{w}_t,r_t\bigr)$ and $\hat{b}_{j,E+S+1,t+1}=\psi\bigl(\hat{b}_{j,E+S,t},\hat{BQ}_{j,t},\hat{w}_t,r_t\bigr)$. These two decisions are characterized by final-age version of the static labor supply Euler equation \eqref{EqEulerLabStat} and the static intended bequests Euler equation \eqref{EqEulerSavEpSstat}. Households in their second-to-last period of life in period $t$ have four decisions to make. They must choose how much to work this period $n_{j,E+S-1,t}$ and next period $n_{j,E+S,t+1}$, how much to save this period for next period $\hat{b}_{j,E+S,t+1}$, and how much to bequeath next period $\hat{b}_{j,E+S+1,t+2}$. The optimal responses for this individual are characterized by the $s=E+S-1$ and $s=E+S$ versions of the static Euler equations \eqref{EqEulerLabStat}, the $s=E+S-1$ version of the intertemporal Euler equation \eqref{EqEulerSavStat}, and the $s=E+S$ static bequest Euler equation \eqref{EqEulerSavEpSstat}, respectively.

    Optimal savings in the second-to-last period of life $s=E+S-1$ is a function of the current savings as well as the total bequests received and prices in the current period and in the next period $\hat{b}_{j,E+S,t+1} = \psi\bigl(\hat{b}_{j,E+S-1,t},\hat{BQ}_{j,t},\hat{w}_t,r_t,\hat{BQ}_{j,t+1},\hat{w}_{t+1},r_{t+1}|\Omega\bigr)$ given beliefs $\Omega$. As before, the optimal labor supply at age $s=E+S$ is a function of the next period's savings, bequests received, and prices $n_{j,E+S,t+1}=\phi\bigl(\hat{b}_{j,E+S,t+1},\hat{BQ}_{j,t+1},\hat{w}_{t+1},r_{t+1}\bigr)$. But the optimal labor supply at age $s=E+S-1$ is a function of the current savings, current bequests received, and the current prices as well as the future bequests received and future prices because of the dependence on the savings decision in that same period $n_{j,E+S-1,t}=\phi\bigl(\hat{b}_{j,E+S-1,t},\hat{BQ}_{j,t},\hat{w}_t,r_t,\hat{BQ}_{j,t+1},\hat{w}_{t+1},r_{t+1}|\Omega\bigr)$ given beliefs $\Omega$. By induction, we can show that the optimal labor supply, savings, and intended bequests functions for any individual with ability $j$, age $s$, and in period $t$ is a function of current holdings of savings and the lifetime path of total bequests received and prices given beliefs $\Omega$.
    \begin{align}
      n_{j,s,t} &= \phi\Bigl(\hat{b}_{j,s,t},\bigl(\hat{BQ}_{j,v},\hat{w}_v,r_v\bigr)_{v=t}^{t+S-s}|\Omega\Bigr) \quad\forall j,s,t \label{EqLabPolFuncGen} \\
      \hat{b}_{j,s+1,t+1} &= \psi\Bigl(\hat{b}_{j,s,t},\bigl(\hat{BQ}_{j,v},\hat{w}_v,r_v\bigr)_{v=t}^{t+S-s}|\Omega\Bigr) \quad\forall j,t \quad\text{and}\quad E+1\leq s\leq E+S \label{EqSavPolFuncGen}
    \end{align}

    If one knows the current distribution of households savings and intended bequests $\bm{\hat{\Gamma}}_t$ and has a beliefs function that predicts the law of motion over time for $\bm{\hat{\Gamma}}_t$, then one can predict time series for total bequests received $\hat{BQ}_{j,t}$, real wages $\hat{w}_t$ and real interest rates $r_t$ necessary for solving each household's optimal decisions. Characteristic (i) in equilibrium definition \ref{DefEquilNonSS} that individuals be able to forecast prices with perfect foresight over their lifetimes implies that each individual has correct information and beliefs about all the other individuals optimization problems and information. It also implies that the equilibrium allocations and prices are really just functions of the entire distribution of savings at a particular period, as well as a law of motion for that distribution of savings.

    In equilibrium, the steady-state household labor supplies $\bar{n}_{j,s}$ for all $j$ and $s$, the steady-state savings $\bar{b}_{j,E+S+1}$, the steady-state real wage $\bar{w}$, and the steady-state real rental rate $\bar{r}$ are simply functions of the steady-state distribution of savings $\bar{\Gamma}$. This is clear from the steady-state version of the capital market clearing condition \eqref{EqMktClrCapStat} and the fact that aggregate labor supply is a function of the sum of exogenous efficiency units of labor in the labor market clearing condition \eqref{EqMktClrLabStat}. And the two firm first order conditions for the real wage $\hat{w}_t$ \eqref{EqFOCwageStat} and real rental rate $r_t$ \eqref{EqFOCrate} are only functions of the stationary aggregate capital stock $\hat{K}_t$ and aggregate labor $\hat{L}_t$.

    \begin{figure}[htb]\centering \captionsetup{width=4.0in}
      \caption{\label{FigKpathTPI}\textbf{Equilibrium time path of $K_t$ for $S=80$ and $J=7$}}
      \fbox{\resizebox{4.0in}{3.0in}{\includegraphics{images/KpathTPI.png}}}
    \end{figure}

    \begin{figure}[htb]\centering \captionsetup{width=4.0in}
      \caption{\label{FigLpathTPI}\textbf{Equilibrium time path of $L_t$ for $S=80$ and $J=7$}}
      \fbox{\resizebox{4.0in}{3.0in}{\includegraphics{images/LpathTPI.png}}}
    \end{figure}


\subsection{Introduction}
The main goal of the analysis part has been discovering insights about what the customer did, behaved and liked. Those are key aspects to know what to change in our service and how to improve, from the customer journey to the App.
To do that we decided to use two different dataset, in order to get different kinds of insights.

\subsection{Clustering}
The first insight we wanted to address was "What customers did during their stay? What did they liked the most? And what can we propose them the next time they will come?".\\
In order to do that we used a Clustering analysis and a particular dataset from the UCI repository. The dataset\footnote{\url{https://archive.ics.uci.edu/ml/datasets/BuddyMove+Data+Set}} is a (249 x 7) .csv file describing the number of reviews each user did. Every row has a specific user ID (249 users), each column reflects  a different field.

\begin{itemize}
\item \textbf{Sports}: Number of reviews on stadiums, sports complex, etc.
\item \textbf{Religious}: Number of reviews on religious institutions.
\item \textbf{Nature}: Number of reviews on beach, lake, river, etc.
\item \textbf{Theatre}: Number of reviews on theatres, exhibitions, etc.
\item \textbf{Shopping}: Number of reviews on malls, shopping places, etc.
\item \textbf{Picnic}: Number of reviews on parks, picnic spots, etc.
\end{itemize}
\noindent By the end of the first two months of opening, we assume to have available a similar review dataset about our activities.

\begin{figure}[H]
\centering
\includegraphics[width=1\textwidth]{Img/Review_dataset.png}
\caption{Review Dataset}
\end{figure}

\clearpage
\subsubsection{KMeans}
In order to perform the clustering analysis we chose to set the number of clusters before the fit. K-means algorithm allowed us to follow this assumption. We applied the Elbow method to choose k. To get info about what activities customers did the most We followed two different paths:

\paragraph*{Nature - Picnic}:
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{Img/2_elbow.png}
\caption{Nature-Picnic Elbow method}
\end{figure}

\begin{figure}[H]
\centering
\hspace{-1cm}
\includegraphics[width=0.8\textwidth]{Img/2_cluster.png}
	\caption{Nature-Picnic Clustering}
\end{figure}
\noindent From the plot is clear that people did more picnics than nature activities, as the points are totally growing to the right. Recommending picnics on the app to natural people maybe would be a waste of time.
\clearpage

\paragraph*{Shopping - Nature}:
\begin{figure}[H]
\centering
\includegraphics[width=0.7\textwidth]{Img/3_elbow.png}
\caption{Shopping-Nature Elbow method}
\end{figure}
\begin{figure}[H]
\centering
\hspace{-1.8cm}
\includegraphics[width=0.8\textwidth]{Img/3_cluster.png}
\caption{Shopping-Nature clustering}
\end{figure}

\noindent The plot is very different from the previous one: it is reasonable to use three clusters as one of them is clearly apart. It is not easy to understand the correlation between the variables as people seem to behave very differently. Certainly shoppers don't normally go into the wild.
\clearpage

\subsubsection{Correlation Matrix}
In the end we realized that a clustering analysis was too much to be applied to this kind of dataset and it wasn't even giving us the kind of answers we were looking for. So we opted for a correlation matrix.

\begin{figure}[H]
\centering
\includegraphics[width=1\textwidth]{Img/correlation_review.png}
\caption{Reviews Correlation Matrix}
\end{figure}

\noindent This type of plot let us understand if features are actually correlated or not. As seen in the clustering analysis, we have a 0.52 between nature and picnic, while shopping and nature are absolutely not linked at all (-0.19).\\
It's remarkable who Religion and Shopping also seem to be correlated at 0.9 out of 1, which it's quite surprising and unexpected.

\clearpage
\subsection{Classification}
The second insight we wanted to address was "Are we able to predict if the customer will return to the watershed after his first stay?".\\
In order to do that we used a Classification analysis and a particular dataset from the UCI repository. The dataset\footnote{\url{https://archive.ics.uci.edu/ml/datasets/Bank+Marketing}} is a (45211 x k) .csv file describing data related to a direct marketing campaigns of a Portuguese banking institution, based on phone calls. The campaign is divided in 3 steps (calls); our goal was to predict the customer behaviour at the end of the first call, without any data from the next calls. The final dataset, indeed, will be a (45211 x 9) .csv file.

\begin{itemize}
\item \textbf{Age}: age.
\item \textbf{Job}: type of job.
\item \textbf{Marital}: marital status.
\item \textbf{Education}: level of education.
\item \textbf{Default}: has credit in default?
\item \textbf{Balance}: balance of the year.
\item \textbf{Housing}: has housing loan?
\item \textbf{Loan}: has personal loan?
\item \textbf{y}: has the client subscribed a term deposit?
\end{itemize}

\noindent After the first stay of a customer, we assume to have available a similar dataset comprehensive of age, job, marital, education, in order to know if that customer will return or not.

\begin{figure}[H]
\centering
\includegraphics[width=1\textwidth]{Img/bank_dataset.png}
\caption{Marketing Campaign Dataset}
\end{figure}

\clearpage
\subsubsection{Feature Engineering}
Dealing with the second dataset required more data cleaning and engineering. First, we deleted all the next calls features, in order to cut all those information we couldn't use. Second we analyzed each variable one by one and decided how to encode it:
\begin{figure}[H]
\centering
\includegraphics[width=0.8\textwidth]{Img/feature_recap.png}
\caption{Feature Engineering Strategy}
\end{figure}

\begin{itemize}[noitemsep]
\item \textbf{Education}: primary, secondary, tertiary (Label Encoded, since we want to keep track of the order. Tertiary brings higher value and score to the user).
\item \textbf{Marital}: married, single, divorced (binary modified, there's no need to introduce other categories).
\item \textbf{Default}: yes, no (binary).
\item \textbf{Housing}: yes, no (binary).
\item \textbf{Loan}: yes, no (binary).
\item \textbf{y}: yes, no (binary).

\begin{figure}[H]
\centering
\includegraphics[width=0.7\textwidth]{Img/clean_up.png}
\caption{Cleanup}
\end{figure}
\item \textbf{Job}:  management, technician, etc (Categorical variable encoded as Dummy).
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{Img/dummy.png}
\caption{Dummy Variables}
\end{figure}
\end{itemize}

\subsubsection{Correlation Matrix}
Before any kind of analysis, we tried to understand feature correlation, in order to understand apriori if there were any dependencies. The matrix showed us there were no correlations at all.
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{Img/correlation_bank.png}
\caption{Marketing Campaign Correlation Matrix}
\end{figure}
\vspace{-1cm}
\subsubsection{Metrics}
Since we were dealing with a classification problem, the first common solution we chose for metrics was confusion matrix, so that we could calculate accuracy and precision and use them to rate the models. During the process, however, we realized that we missed an important aspect of our dataset: the target, y, was imbalanced.
This event brought us to rethink our metrics and overall every consideration we were going to make. In the end we chose RECALL and F1 as main metrics, with a particular focus on RECALL as we want to identify the completeness of the classifier and lower as much as possible all the false negatives output, in order to focus on the customers who subscribed the service.

\begin{figure}[H]
\centering
\includegraphics[width=0.7\textwidth]{Img/imbalanced.png}
\caption{Target Plot}
\end{figure}
\clearpage
\subsubsection{Models}
The second best way to solve a machine learning problem in general is to try several models and compare their results, as each model is characterized by unique features that, depending on the problem, are able to make it perform better or worse. This is specifically true for imbalanced dataset. In our analysis we focused on trees as decision trees frequently perform well on imbalanced data.
\\They work by learning a hierarchy of if/else questions that normally can be forced to address both classes.

\begin{itemize}
\item \textbf{Decision Tree Classifier}: DT seemed learning quite well from the dataset and, even if the recall was not so high, averall the F1 score was very curious.

\begin{figure}[H]
\centering
\includegraphics[width=0.7\textwidth]{Img/normal_tree.png}
\caption{Basic DT Score}
\end{figure}
\item \textbf{Random Forest Classifier}: as a generalization of decision tree, RF should have been performing better. On the contrary, during the process it had a different behavior. We'll discuss more about that after more analysis.

\begin{figure}[H]
\centering
\includegraphics[width=0.7\textwidth]{Img/normal_forest.png}
\caption{Basic RF Score}
\end{figure}

\item \textbf{K-NN}: Besides others, the third best one was the K-NN. We decided to put it into the documentation to proof the fact we considered also other kind of models, even if the result was worse.

\begin{figure}[H]
\centering
\includegraphics[width=0.7\textwidth]{Img/normal_knn.png}
\caption{Basic K-NN Score}
\end{figure}
\end{itemize}

\noindent For any doubt, in the notebook each model has also it's confusion matrix, in order to make the reader understand better the overall trend.

\clearpage
\subsubsection{Under-Sampling}
Third way to address an imbalanced dataset is to re-sample specific features. In our case we lowered the number of rows of the biggest class. Under-sampling, in fact, can be defined as removing some observations from the majority class (the customer doesn't subscribe to the service or doesn't return to the watershed).
The selection was performed completely randomly. The only drawback was that we were removing information that may be valuable. For the sake of simplicity (and not to bore the reader with hundreds on analysis) we will focus only on trees.

\begin{itemize}
\item \textbf{Decision Tree Classifier}: like in the case before, the highest subscribed recall score has been performed by a decision tree. Remarkable is the fact that the value almost doubled (0.65).

\begin{figure}[H]
\centering
\includegraphics[width=0.7\textwidth]{Img/under_tree.png}
\caption{Under-Sampled DT Score}
\end{figure}
\item \textbf{Random Forest Classifier}: the greatest improvement, however, was performed by RF as from a very low 0.18 it reached 0.61 (lower than DT, but still very similar).

\begin{figure}[H]
\centering
\includegraphics[width=0.7\textwidth]{Img/under_forest.png}
\caption{Under-Sampled RF Score}
\end{figure}
\end{itemize}

\noindent F1 scores were slightly different from the normal case, but they were still keeping the small difference they had before. Between the two models, RF F1 did benefit the most from under-sampling.

\clearpage
\subsubsection{SMOTE}
In order to reach a complete overview of our analysis, we decided to apply another re-sampling technique, opposite from the first one, based on the concept of over sampling the minority dataset. Synthetic Minority Over-sampling Technique (SMOTE), in fact, is able to generate synthetic (fake) observations based on the real ones from the minor class, in order to reach a high similar volume of samples in both classes.

\begin{itemize}
\item \textbf{Decision Tree Classifier}:

\begin{figure}[H]
\centering
\includegraphics[width=0.7\textwidth]{Img/smote_tree.png}
\caption{SMOTE DT Score}
\end{figure}
\item \textbf{Random Forest Classifier}:

\begin{figure}[H]
\centering
\includegraphics[width=0.7\textwidth]{Img/smote_forest.png}
\caption{SMOTE RF Score}
\end{figure}
\end{itemize}

\noindent After the algorithm implementation, both models seemed to behave in a similar way. Precision and F1, in fact, were almost the same. RF, however, was the one which did benefit the most.
\clearpage
\subsection{Data Analysis Conclusion}
From a brute-force clustering attack we understood that a simple correlation matrix would have been more than fine to get the info we needed out of the dataset. It allowed us to understand connections we knew about (picnic - nature) and the ones we did not (shopping - religion).\\
The most challenging one has been the last analysis as we had to deal with an imbalanced dataset and decided to follow several paths. At first we studied the different metrics we could use to rate our results and recall was preferred. We then chose different models to use (decision tree and random forest were the best ones). At last we tried to re-sample our data and perform again each analysis.

\begin{itemize}
\item \textbf{Recall}

\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{Img/recall_recap.png}
\caption{Recall Recap}
\end{figure}
\item \textbf{F1 score}:

\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{Img/recap_f1.png}
\caption{F1-score Recap}
\end{figure}
\end{itemize}

\noindent From the beginning we decided to use RECALL as main metric. It is clear that Decision Tree is overall the best model to address this dataset, that combined with the Under-sampling technique provided the best results.\\
However, considering the F1 score, it is interesting to notice how random forest model improved across the analysis and almost reached the same value as the decision tree, which, on the other hand, seems to behave totally fine even from the beginning, without any kind of sampling. This is maybe the proof that DT is effectively our best choice.

Report - Latex Files/Files/DataAnalysis.tex


\section{Future work}
\label{sec:future_work}

In this section, we list some possible variations that could be applied to the
pipeline in the search for more performance.

\paragraph{Primary model} The primary model is using one of the many implementations
of momentum. Other alternatives can be tried using a relatively similar
scheme and might lead to better results without affecting anything in the pipeline.
Furthermore, other non-momentum based primary models could be tried as well, e.g. mean reversion.
Anyway, any change to the primary model will imply retraining the secondary model
with the new labels.

The primary model can also be applied to more than one specific asset, like other
cryptocurrencies. That would provide more resilience to the secondary model because
it will be trained with labels and metalables generated from different asset sources
and reduce the risk of overfitting.

\paragraph{Secondary model} As mentioned in \ref{sec:conclusion}, other models
could be evaluated. Not only tree based models, but also neural networks or
SVMs. Probably, further feature engineering would be required for the latter two
and that would not necessarily be useful for the tree based models. Special
attentions should be paid to overfitting though. A more comprehensive back
testing strategy would be required aiming to evaluate the strategy under
more scenarios and realize if testing data produced in excess model adjustment.

\paragraph{Feature importance} Also, as commented in \ref{sec:conclusion},
it is recommended to try feature orthogonalization to mitigate the effect of
feature substitution (the analog of multi-collinearity in linear models) in the
trees. Implementation of that technique was out of the scope of this research
project.

About interpretability, as Lopez de Prado points out in \cite{machine_learning_for_asset_managers}, only a theory can pin down
a cause-effect mechanism that allows you to generate excess returns. However,
most interpretability techniques are not suited for identifying causal
relationships unless additional assumptions are imposed (see \cite{causal_interpretations_of_bb_models}).

At this time, interpretability techniques in machine learning have become a widely used tool for practitioners. However, their outputs must be taken only as approximations of what models are doing, even if those interpretability exercises are sufficient to comply with current regulatory constraints. In this regard, how to do proper inference on the estimates of feature importance remains a current research area.  In this regard, techniques for proper inference on the estimates of feature importance remain a current research area.

In another context, this point was also mentioned by Swadroe \cite{your_complete_guide_to_factor_investing} when he warns us to be skeptical about the persistence of excess returns from technical trading rules. These rules rely solely on historical prices and lack risk-based explanations that cannot be arbitraged away.

\paragraph{Back testing} The implemented back testing strategy is not the only
one that could be used and probably it is not the most appropriate, although the
most common. Splitting the feature space into multiple folds and mixing them
to create different time lines that the model could face is one the many alternatives
that would help to create different scenarios to evaluate the model.

There are several ways to obtain rich scenarios under which to stress a given strategy. For example,  with access to large data sets, Wiese, Knobloch, Korn, and Kretschmer in \cite{quant_gans} implement generative models for these purposes.

It is possible to analyze if more sophisticated back testing techniques are warranted by calculating a metric called  Probability of Backtest Overfitting (PBO, \cite{lopez_de_prado}).  This metric measures the change in performance rankings for our strategies.  In particular, one can use them when assessing different trading rules.   Intuitively, an optimal trading rule overfits when it is expected to underperform the median of a set of alternative trading rules out of the sample.

\paragraph{Metrics} In Andrew Lo \cite{the_statistics_of_sharpe_ratio} it is
shown how the simplified scale of from monthly Sharpe Ratios to annual Sharpe
Ratios cannot be simply expanded by $\sqrt{12}$ and it depends on its
distribution. Application of the proposed equations to the benchmark would correct
the informed Sharpe Ratio even more.


\subsubsection{CMS Spread Leg Data}
\label{ss:cmsspreadlegdata}

Listing \ref{lst:cmsspreadlegdata} shows an example for a leg of type CMSSpread.

\begin{listing}[H]
%\hrule\medskip
\begin{minted}[fontsize=\footnotesize]{xml}
      <LegData>
        <LegType>CMSSpread</LegType>
        <Payer>false</Payer>
        <Currency>GBP</Currency>
        <Notionals>
          <Notional>10000000</Notional>
        </Notionals>
        <DayCounter>ACT/ACT</DayCounter>
        <PaymentConvention>Following</PaymentConvention>
        <ScheduleData>
          ...
        </ScheduleData>
        <CMSSpreadLegData>
          <Index1>EUR-CMS-10Y</Index1>
          <Index2>EUR-CMS-2Y</Index2>
          <Spreads>
            <Spread>0.0010</Spread>
          </Spreads>
          <Gearings>
            <Gearing>8.0</Gearing>
          </Gearings>
          <Caps>
            <Cap>0.05</Cap>
          </Caps>
          <Floors>
            <Floor>0.01</Floor>
          </Floors>
          <NakedOption>false</NakedOption>
        </CMSSpreadLegData>
      </LegData>
\end{minted}
\caption{CMS Spread leg data}
\label{lst:cmsspreadlegdata}
\end{listing}

The elements of the CMSSpreadLegData block are identical to those of the CMSLegData (see \ref{ss:cmslegdata}), except
for the index which is defined by two CMS indices as the difference between \verb+Index1+ and \verb+Index2+.