\documentclass[main.tex]{subfiles}
\begin{document}

\section{Dark Matter}

\marginpar{Wednesday\\ 2020-5-13, \\ compiled \\ \today}

% Yesterday we described the formation of the first nuclei.
% The amount of baryons in terms of energy density is of the order of 4 to \SI{5}{\percent}.
Let us start our discussion of dark matter with the \textbf{baryon budget}: how much do baryons contribute to the total energy density of the universe?

In the \emph{high-redshift universe} we have:
\begin{enumerate}
    \item at nucleosynthesis (\(z \sim \num{e9}\)) the baryonic matter fraction was \(\Omega_{B}^{BBN} = \num{.04(2)}\);
    \item at CMB emission (\(z \sim \num{e3}\)) the baryonic matter fraction was \(\Omega_{B}^{CMB} \approx \Omega_{B}^{BBN} \approx \SI{5}{\percent}\);
    \item at \(z \sim 3\) we have observations of the Lyman \(\alpha \) (``frest''?) of the intergalactic medium: this again yields \(\Omega_{B} \approx \SI{5}{\percent}\).
\end{enumerate}

\todo[inline]{what is written there?}

In the recent universe (\(z \sim 0\)) we have observations of galaxy clusters, in which we measure a matter fraction of \(\Omega_{M} \approx \num{.26}\), of which about \SI{15}{\percent} is made of baryons: so, we get an estimate of around \(\Omega_{B} \approx \SI{4}{\percent}\).

So, the fraction of luminous baryonic matter is \(\Omega_{B}^{\text{lumin}} \approx \SI{1}{\percent}\), with a corresponding density of \(\rho _{\text{lumin}} \approx \SI{9e-29}{kg m^{-3}}\).
\todo[inline]{Not really clear how we estimate this based on what is mentioned above.}

On the other hand, our estimate for the total baryonic mass fraction is \(\Omega_{B}^{\text{tot}} \approx \SI{5}{\percent}\), with a corresponding density of \(\rho_{b} \approx \SI{4e-28}{kg m^{-3}}\).

We know that the total value of \(\Omega _{\text{tot}}  \sim 1\).

The density fraction due to dark energy is around \SI{70}{\percent}, while \(\Omega _{\text{matter}} \sim \SI{30}{\percent}\): this can be deduced from the gravitational potential energy, which is inferred from galactic rotation curves, and the mass distribution in clusters.
The corresponding matter density is around \(\rho_{M} \approx \SI{2e-27}{kg m^{-3}}\).

So, most (something like \SI{80}{\percent}) of the matter is not made of baryons.
We are then looking for non-baryonic matter with \(\Omega \sim \SI{25}{\percent}\).
More precisely, the latest results give
%
\begin{align}
\Omega_{DM} \approx \SI{26.4}{\percent} = \SI{84.4}{\percent} \Omega_{M}
\,.
\end{align}

Accounting for our uncertainty in \(H_0 \), we can write \(\Omega_{B} = \num{.022}h^{-2} \approx 4 \divisionsymbol \SI{5}{\percent}\).

How sure are we that dark matter is indeed the best way to describe galactic rotation curves and such?

It makes other predictions beyond them: there were interesting studies about how the lensing of light is affected by Dark Matter.
All the evidence is based on our knowledge of gravitational interaction: Are we sure that Newton's law of gravitation is right on such large scales? There are MOND theories, in which it does not.
However, the hints about the existence of non-baryonic DM come from very different directions.\footnote{And I'd be remiss not to cite \url{https://xkcd.com/1758/}.}
We then take the standard view: Dark Matter exists.

Could the Higgs boson be the source of Dark Matter? No.
It decays too fast.
We have similar problems for the \(Z\) boson. So, in the end we must have some other neutral particle.

So, neutrinos? Maybe. They are light, of course, but there are many.

We get
%
\begin{align}
\Omega_{\nu } = \frac{\rho^{0}_{\nu }}{\rho _{\text{crit}}}
\,,
\end{align}
%
where we used the known neutrino number density today \(n^{0}_{\nu } \approx \SI{339.5}{cm^{-3}}\).
So, this comes out to be
%
\begin{align}
\Omega_{\nu } = \frac{ \sum _{i} m_{\nu_{i}}}{h^2 \SI{93.14}{eV}}
\,.
\end{align}

Now, \(h^2 \sim 1/2\), so this is very roughly
%
\begin{align}
\Omega_{\nu } \sim \frac{\sum m_\nu }{\SI{50}{eV}}
\,.
\end{align}

Recall that ``electron neutrino'' is a current eigenstate, not a mass eigenstate.
The bound we have is of the order \(m_{\nu_{e}} \lesssim \SI{1}{eV}\).

We also have bounds for the square of the mass differences.
In the end, this means that the term \(\sum m_{\nu }\) can be at most a few \SI{}{eV}.

Even if all the three neutrinos' masses were at the very top of their current experimental bounds at \( m_\nu \sim \SI{2}{eV}\) each we would have \(\Omega_{\nu } < \num{.1}\), which is too small to fit observations of \(\Omega_{DM} \sim \num{.25}\).

In spite of the fact that they are very numerous, neutrinos have too small a mass.

\subsection{Hot \& cold DM}

% Dark Matter is also crucial in the formation of Large Scale Structure.
% It must ``clump'' in order to do so: this means that it must be nonrelativistic.  But neutrinos are still relativistic today!

Let us call a generic DM particle \(X\). It will decouple at a certain temperature \(T^{D}_{X}\). Then, \textbf{cold} DM is such that \(M_X \gg T_X^{D}\), while for \textbf{hot} DM we have \(M_X \ll T_X^{D}\).
So, cold DM is \emph{nonrelativistic} when it decouples, while hot DM is \emph{relativistic} when it does.

We can also have \textbf{warm} DM, in the case where \(M_X \sim T_D\).

% We must ask that the mass of Dark Matter, \(M_X\), be larger than the temperature of decoupling. This is then called ``Cold Dark Matter''.

% Relativistic DM kills all the density fluctuations.

Neutrinos would be an example of hot dark matter, since they decouple around \SI{1}{eV} while their mass is smaller than that.

Whether DM is hot or cold\footnote{\url{https://www.youtube.com/watch?v=kTHNpusq654}} is relevant for the formation of structures: relativistic matter has a hard time clumping at small scales.
% If neutrinos were the source of DM, the first structures would form at a very large scale.

For HDM, in the period \(T_D > T > M_X\) the DM is free-streaming, so it quickly fills in under-dense regions, and the density perturbations are washed out.

The warm case is at around \(M_X \gtrsim \SI{1}{eV}\), while HDM has \(M_X < \SI{1}{eV}\).

There are simulations used in order to compare these models: what is seen is that for HDM the first structures to form are the very largest --- superclusters of galaxies ---, which then fragment into smaller pieces.
This type of evolution is in stark disagreement with our observations.
With this line of reasoning we have bounded the fraction of DM which may be hot: most of it will be cold.
So, neutrinos may be a part of DM, but they cannot be the dominant part of it.

% What does the Dark Matter density profile look like in a galaxy?
% CDM would have a ``cusp'' in density at the center, a really heavy region.

% Could we also have ``Warm'' Dark Matter? An intermediate situation between HDM and CDM.

Dark Matter around \SI{1}{keV} in mass would be a candidate for this.

Let us consider \textbf{Thermal Dark Matter}, which has been in equilibrium with the plasma for some time in the early universe, so it is cold when it decouples.
By assuming chemical equilibrium we can figure out the number density of those particles.
Consider a heavy particle \(\ce{X}\), with mass larger than, say, \SI{10}{GeV}, and which is stable or quasi-stable (that is, such that its lifetime is long compared to the age of the universe).

Then, its equilibrium number density will be given by
%
\begin{align}
n_X ^{\text{eq}} = g_X \qty(\frac{M_X T}{2 \pi })^{3/2} \exp(- \frac{M_X}{T})
\,.
\end{align}

The number of \(X\) in a certain comoving volume can only change by annihilation and creation: \(X \overline{X} \leftrightarrow \text{light particles}\).

If the rate \(\Gamma \) of this process is \(>H\) at a certain temperature \(T\), then the number density of \(X\) is indeed the equilibrium one.
However, as \(T\) decreases below \(M_X\) we will reach a point at which the light particles will not be energetic enough to form \(X\) pairs.
At this point, only the annihilation of \(X \overline{X}\) pairs will be able to occur, so their number will diminish.

After some more time, their encounters will not be likely anymore, so we say that the abundance of \(X\) \emph{freezes out}.
From that moment onward, \(n_X\) will not change in the comoving frame.

Note that this may still be before \emph{kinetic} equilibrium is reached: scatterings like \(X + A \to X + B\) can still happen.
The number of \(X\)s is governed by the Boltzmann equation:
%
\begin{align}
\dv{n_X}{t} + 3 H n_X  = - \expval{\sigma _{\text{annihilation}}v } \qty(n_X^2 - n_{X, \text{eq}}^2)
\,.
\end{align}

The cross section \(\sigma _{\text{annihilation}}\) is averaged over all the equilibrium momentum space distribution functions of the particle species, and summed over the possible annihilation channels.

% We have different kinds of equilibrium: kinematic and chemical equilibrium. If we have a scattering like
% %
% \begin{align}
% \ce{X} + \ce{A} \to \ce{X} + \ce{B}
% \,,
% \end{align}
% %
% this does not change the number of particles \(\ce{X}\). It only distributes the momenta.

% If the annihilation rate of \ce{X} is lower than the expansion of the universe, it stays as it is.
% Then, we have the Boltzmann suppression factor in the distribution, \(\exp(- m_X / T)\).

% The final number of \ce{X} is reduced by a factor \(\exp(- m_X / T _{\text{freezeout}})\).

\todo[inline]{Here start many calculations to get the density of \(X\) today\dots}

\subsection{WIMPs}

What is the window of the parameters of this particle which could give us the \(\Omega_{DM}\) we observe?

If the particle is a Weakly Interacting Massive Particle, we get the ``WIMP miracle'': with very simple assumptions, we get \(\Omega \) between 1 and \num{e-1}. Let us show this.

\begin{claim}
For a heavy relic particle the following holds:
%
\begin{align}
\Omega_{X} h^2 \approx \num{e-10}
\qty(\frac{\SI{}{GeV^{-2}}}{\expval{\sigma v}_{\text{eff}}})
\frac{1}{\sqrt{g_* (t_f)}}
\log(
    \frac{g_X M_X M_P \expval{\sigma v}}{(2 \pi )^{3/2} \sqrt{g_*}}
)
\,.
\end{align}
\end{claim}

We apply this to a particle with mass \(M_X \sim \SI{100}{GeV}\), which interacts through the weak interaction and therefore has
%
\begin{align}
\expval{\sigma v} \sim \frac{\alpha_{w}^2}{M_X^2} \sim \frac{\num{e-4}}{\num{e4}} \SI{}{GeV^{-2}}
\,.
\end{align}

The values of \(\sqrt{g_*}\) we need are \(\sqrt{g_*}\sim 10\) at \(T > \SI{200}{GeV}\) and \(\sqrt{g_*} \sim 3\) at \(\SI{100}{MeV}\).

Plugging these in we get
%
\begin{align}
\Omega_{X} h^2 &\approx 2 \times \num{e-10} \num{e8} \frac{1}{10} \log(\frac{\num{e2} \num{e19} \num{e-7}}{\num{e2}})  \\
\Omega_{X} &\approx 2 \times  2\times  \num{e-3} 12 \log 10 \approx \num{e-1}
\,,
\end{align}
%
since \(h^2 \sim 1/2\).

So, a particle with \(M_X \sim \SI{100}{GeV}\) which is weakly coupled, \(\alpha_{w} \sim \num{.01}\) could explain our dark matter observations!

So, the WIMP possibility looks good; alas, the LHC has not seen any new particles at the electroweak scale.

We have a result of \(\Omega_{X}\) between \(\num{.1}\) and \(\num{1}\), but the higher end of this range is not good for our purposes: we know that \(\Omega_{DM} < \num{.25}\).
So, we can put a lower limit on \(\sigma _{\text{annihilation}}\) in order to avoid the over-production of dark matter (or, more accurately, we want to avoid the fact that too much of it remains).
What we find is
%
\begin{align}
\abs{\sigma _{\text{ann}} v} >  \num{.3} \divisionsymbol \SI{10e-8}{GeV^{-2}}
\,.
\end{align}

Now, if the mass of \(X\) were large (like, more than \SI{10}{TeV}) then we would get \(\Omega_{X} > 10\), which would definitely over-close the universe. So, we expect DM not be super massive.

There are ways to get around this restriction: one is a strongly coupled theory, in which case \(X\) would be annihilated more; then it would be a Strongly Interacting Massive Particle but it would work.

Another possibility is for \(X\) not to have been in thermal equilibrium, so that we have non-thermal DM, so that their number is not linked to the abundance of other particle species.
\todo[inline]{But wait, what does thermal equilibrium have to do with it? We should only consider chemical equilibrium for the variation of the number of \(X\), right?}

For regular WIMPs, though, the bound \(M_X < \SI{100}{TeV}\) remains.

We can expect that there are more than one kind particle constituting DM.
A possibility is the presence of a ``Dark Sector'' in the SM.
It is generally assumed that there is a sort of ``portal'', which makes the ordinary matter sector allowing the sectors to communicate.

A very simple kind of dark sector would be constituted by right-handed neutrinos: they only interact with the Higgs boson, with terms like \(\overline{\nu}_{L} H^{0} \nu_{R}\).

How do we \textbf{search for WIMPs}? There are two ways, one is direct: we set up a target and wait for \(X\) to hit it.
If we set up a target with \SI{10}{kg} of nuclei with \(A \sim 100\), and have \(M_X \sim \SI{100}{GeV}\) and \(\sigma_0 \sim \SI{e-10}{GeV^{-2}}\), then we will have
%
\begin{align}
\text{event rate} &\approx
v_X n_X N_A \sigma_{AX}  \\
&\approx v_X \frac{\rho_{DM}^{\text{local}}}{M_X} N_A \sigma_{AX}
\,,
\end{align}
%
where \(N_A \approx \num{6e25}\) is the number of \(A\) nuclei, \(\sigma_{AX} \sim 10 A \sigma_0 \) is the cross section of the nucleus-\(X\) process, and \(\rho_{DM}^{\text{local}} \approx \SI{.3}{GeV / cm^3}\).

This then yields (after multiplying by \(\hbar^2 c^2\), and taking \(v_X \sim  \SI{2}{km/s}\))
%
\begin{align}
\text{event rate} \approx \SI{e-8}{s^{-1}}
\,,
\end{align}
%
or about 1 event a year.

% N_A= (10 * u.kg / (100 * ac.m_p)
% rho = .3 *u.GeV / u.cm**3M_X = 100 * u.GeV
% sigma = 10 * 1e-10 * u.GeV**-2
% (2 * u.km/u.s * rho / M_X * N_A * sigma * ac.c**2 * ac.hbar**2).decompose()

The other method is indirect, we look for the products of \(X \overline{X}\) annihilation in our astronomical observations: photons, protons, electrons, neutrinos or their antiparticles.

\end{document}


\subsection{Designing Guess that Number} % (fold)
\label{sub:designing_guess_that_number}

Table \ref{tbl:control-flow-prog} contains a description of the next program we are going to design. This program plays a small guessing game called \emph{Guess that Number}. In this game the computer will think of a target number between 1 and 100. The user then has 7 guesses to determine the target number's value. At each guess the computer outputs a message to indicate if the target number is greater than, less than, or equal to the user's guess.

\begin{table}[h]
\centering
\begin{tabular}{l|p{10cm}}
  \hline
  \multicolumn{2}{c}{\textbf{Program Description}} \\
  \hline
  \textbf{Name} & \emph{Guess that Number} \\
  \\
  \textbf{Description} & A simple guessing game where the computer `thinks' of a number between 1 and 100, and the user has seven guesses to guess it. With each guess the computer indicates if the number it is thinking of is less than, or greater than the user's guess. If the user runs out of guesses they are shown the answer, and that round ends. After each round the user has the option to play again, or to quit the program.\\
  \hline
\end{tabular}
\caption{Description of the Guess That Number program.}
\label{tbl:control-flow-prog}
\end{table}

As before, the process to design and implement this program will follow a number of steps:
\begin{enumerate}
  \item Understand the problem, and get some ideas on the tasks that need to be performed.
  \item Choose the artefacts we will create and use
  \item Design the control flow for the procedural\footnote{The program, and any Functions and Procedures.} artefacts
  \item Map these artefacts to code
  \item Compile and run the program
\end{enumerate}

The new step in this process will involve designing the flow of the instructions within the program, and the Functions and Procedures that need to be created. Previously each artefact had a sequential flow of instructions, the introduction of the control flow statements has made it possible to add branches and loops to this sequence. The order of these actions needs to be considered for each sequence of instructions in your code.

% subsection designing_guess_that_number (end)

\clearpage
\subsection{Understanding Guess that Number} % (fold)
\label{sub:understanding_guess_that_number}

Guess that Number is a relatively common game involving one player choosing a number, and the other trying to guess it. Each guess is followed by an indication of whether the guess was less than or larger than the target number. The game continues until the player guesses the target number.

The data for this program will be relatively simple. The code will need to keep track of the current \textbf{target number} which will be an integer. The player will be able to enter a \textbf{guess}, also an integer. These two numbers represent the data model for the game. It is a game that involves a \emph{target number} and \emph{guess}.

The actions within the program involve \textbf{playing the game}, and \textbf{performing a guess}. Playing the game is the process of performing guesses until the player finally guesses the target number. This version of the game will only allow the player to have seven guesses, so this will need to be a part of this code in the program. The process of performing a guess will involve getting the user's guess, and giving them feedback indicating if they guessed the target number, of if they were larger than, or less than the target.

% subsubsection understanding_guess_that_number (end)

\subsection{Choosing Artefacts for Guess that Number} % (fold)
\label{sub:choosing_artefacts_for_guess_that_number}

Software design is all about \textbf{abstraction}. This is the process of determining the essential features of the problem and modelling that in the artefacts that you create in your code. When designing the artefacts that will make up your code you need to think about problem, and try to create artefacts in your code that represent the actions and data you imagine when thinking about the problem.

Our understanding of the Guess that Number game indicates that there are two processes that need to be performed within the program: \textbf{play game} and \textbf{perform guess}. These two processes can be coded as either Functions or Procedures in the program's code. The \texttt{Play Game} code can be implemented as a \nameref{sub:procedure}. It will be responsible for running the process of the game, starting with telling the user it has `thought of a number', through to coordinating the guesses, ending only when the user gets the answer of runs out of guesses.

\texttt{Perform Guess}, on the other hand, will need to be a \nameref{sub:function} as it must return back a \nameref{sub:boolean_data} value indicating if the user has guessed the number. The code in \texttt{Perform Guess} will be responsible for asking the user to enter a guess, and then giving them the feedback on their guess. As this has the details of the guess, its result is needed to allow \texttt{Play Game} to determine if the user has guessed the number.

The \texttt{Perform Guess} code will also need to accept parameters to tell it what the current \texttt{target} value is. This data will exist within the \texttt{Play Game} code, so it will need a mechanism to pass that code to \texttt{Perform Guess}. A \nameref{sub:parameter} is needed in \texttt{Perform Guess} to accept \texttt{target} will enable this.

A nicety may be to allow tell the user which guess they are up to. Once again, this information is stored in \texttt{Play Game}, so a second parameter can be added to allow \texttt{Play Game} to pass in the \texttt{guess number} along with the \texttt{target} number.

In addition to these it has been decided to add a \texttt{Print Line} procedure to display a line of `-' characters. This will be displayed at the end of the game before the user is asked if they want to play again. A \texttt{length} parameter will enable the caller to indicate how many of these characters are printed on the line.

The Structure Chart showing these artefacts is shown in Figure \ref{fig:guess-game-structure}, and the Sequence Diagram is shown in Figure \ref{fig:guess-game-seq}. Notice that there is some relationship here between the artefacts that we are creating and the steps in the game itself. The earlier it is to see the relationship between these artefacts and the problem, the better job you have done abstracting your solution.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.6\textwidth]{./topics/control-flow/diagrams/GuessThatNumStructure}
   \caption{Structure Chart for the Guess that Number Program}
   \label{fig:guess-game-structure}
\end{figure}

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/control-flow/diagrams/GuessThatNumSeq}
   \caption{Sequence Diagram for the Guess that Number Program}
   \label{fig:guess-game-seq}
\end{figure}
% subsection choosing_artefacts_for_guess_that_number (end)

\clearpage
\subsection{Designing Control Flow for Perform Guess} % (fold)
\label{sub:designing_control_flow_for_perform_guess}

Having chosen the artefacts to build, the next step is to design the control flow that will enable these Functions and Procedures, and the program itself, to achieve their goals. Each has a responsibility that it must meet in order for the overall solution to work.

This section will go through designing the logic for the \texttt{Perform Guess} Function. It will cover the following points:
\begin{itemize}
  \item \nameref{ssub:flow_charts}
  \item \nameref{ssub:structured_programming}
  \item \nameref{ssub:structured_programming_blocks}
  \item \nameref{ssub:combining_blocks_for_the_guessing_game}
  \item \nameref{ssub:setting_the_result_using_an_expression}
  \item \nameref{ssub:the_pseudocode_for_perform_guess}
\end{itemize}

\subsubsection{Drawing control flow using a Flowchart} % (fold)
\label{ssub:flow_charts}

Before examining the control flows within the Functions and Procedures of the Guess that Number program, let us have a look at a means of visually representing flow of actions within code. A common means of doing this is to use a \textbf{flowchart}. This is a diagram that depicts a sequence of actions, and can be used to represent the sequence of action actions that occur within your code.

The flowchart has four basic symbols as shown in Figure \ref{fig:flow-symbols}.
\begin{itemize}
  \item \textbf{Start/Stop}: This represents the start and end of the process. Typically the start node would have the name of the Function or Procedure within it.
  \item \textbf{Flow}: The arrows between nodes represent control flow, indicating which action is to be performed next in the code.
  \item \textbf{Process}: This node represents a task being performed. In our case this will map to one, or more, simple statements. The text in the Process node should indicate what is being performed at this stage.
  \item \textbf{Decision}: This represents a point where the code needs to make a decision. This is used for the conditions in the \nameref{sub:branching} and \nameref{sub:looping} statements. A decision \textbf{must} have more than one flow coming out of it, and each flow should indicate the condition that triggers that path.
\end{itemize}

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.5\textwidth]{./topics/control-flow/diagrams/FlowParts}
   \caption{Flowchart Symbols}
   \label{fig:flow-symbols}
\end{figure}

% subsubsection flow_charts (end)

\clearpage
\subsubsection{Use Structured Programming Principles to guide the design of the flowchart} % (fold)
\label{ssub:structured_programming}

Flowcharts can be used to represent any sequence of actions, but not all sequences of actions will be easy to code. \fref{fig:unstructured-flow-example} is an example of a flowchart, but not one that can be coded using the structured statements covered in \cref{cha:control_flow}. \fref{fig:structured-flow-example} shows a structured version of this same algorithm. Notice that in this version there are identifiable blocks, each having a single entry and a single exit. This structure could easily be converted to code using structured statements.

Looking at these two flowcharts, the structured flowchart looks more complicated than the unstructured version. This reflects the fact that in some cases the structured version may be more complicated, but is also an indication that this process may be better broken down into more functions and procedures. For example, the repeated loop looking for someone else to blame could be placed in its own procedure. If you find it difficult to design the control flow using structured blocks try to see if you can identify additional Functions and Procedures to help you break the code down into smaller blocks.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/control-flow/diagrams/UnstructuredFlowSample}
   \caption{Unstructured Flowchart}
   \label{fig:unstructured-flow-example}
\end{figure}

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/control-flow/diagrams/StructuredFlowSample}
   \caption{Structured version of Figure \ref{fig:unstructured-flow-example}}
   \label{fig:structured-flow-example}
\end{figure}

The structured programming principles indicate that code within a Function or Procedure should be organised into \textbf{blocks}. These blocks match to the structured statements in modern programming languages: the \nameref{sub:branching} and \nameref{sub:looping} statements. Each of these blocks has a \textbf{single entry point} and a \textbf{single exit point}, allowing them to combined together.

The addition of \nameref{sub:jump} statements to the language allows the blocks to have \textbf{multiple exit points}; one at the end of the block's code, the other at a \nameref{sub:break}, \nameref{sub:exit}, or \nameref{sub:return_statement}. These statements give you extra flexibility, but still work within the structured programming principles.

\mynote{
Prior to the Structured Programming, programs had two control flow mechanisms: \texttt{if} and \nameref{sub:goto}. Control flow was not easy to picture or understand. Programs written in this way are now know as \textbf{spaghetti code}, as understanding this code is much like trying to untangle a bowl of spaghetti, though not nearly as tasty.
}
% subsubsection structured_programming (end)

\clearpage
\subsubsection{The Structured Programming blocks used to build the logic} % (fold)
\label{ssub:structured_programming_blocks}

In Structured Programming there are three kinds of blocks:
\begin{itemize}
  \item \textbf{Sequence}: one instruction follows the next in a sequence.
  \item \textbf{Selection}: the ability to branch the sequence.
  \item \textbf{Repetition}: repeat a block a number of times.
\end{itemize}

The flowchart snippets in the section on \nameref{sub:branching} and \nameref{sub:looping} showed how the flows work for \emph{selection} and \emph{repetition}. \fref{fig:selection-flow} shows the three alternatives for selection blocks, \fref{fig:repetition-flow} shows the two alternatives for repetition blocks, and \fref{fig:sequence-flow} shows the standard sequence flow. The design of any program's logic is a task in combining these blocks together to perform the desired effect.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.15\textwidth]{./topics/control-flow/diagrams/SequenceFlow}
   \caption{Flows for Sequence Blocks}
   \label{fig:sequence-flow}
\end{figure}

\begin{figure}[h]
   \centering
   \includegraphics[width=0.9\textwidth]{./topics/control-flow/diagrams/SelectionFlow}
   \caption{Flows for Selection Blocks}
   \label{fig:selection-flow}
\end{figure}

\begin{figure}[h]
   \centering
   \includegraphics[width=0.55\textwidth]{./topics/control-flow/diagrams/RepetitionFlow}
   \caption{Flows for Repetition Blocks}
   \label{fig:repetition-flow}
\end{figure}

% subsubsection structured_programming_blocks (end)

\subsubsection{Combining blocks for the Perform Guess} % (fold)
\label{ssub:combining_blocks_for_the_guessing_game}

With the basic theory at hand, we can now start to design the control flow for the Guess that Number program. This process will involve, once again, the idea of \textbf{abstraction}. When designing the flow for a program you first need to be able to perform the process yourself, even if its just on paper, and then work out the steps that you undertook so that you can code these within the program.

For the Guess that Number program we can start by designing the control flow within the \texttt{Perform Guess} function. The specification of this is shown in \tref{tbl:perform guess}. Think about the steps that need to be performed to achieve this. If you had been asked to do this what would you need to do?

\begin{table}[h]
  \centering
  \begin{tabular}{|c|p{9cm}|}
    \hline
    \multicolumn{2}{|c|}{\textbf{Function}} \\
    \hline
    \multicolumn{2}{|c|}{} \\
    \multicolumn{2}{|c|}{\texttt{Perform Guess}} \\
    \multicolumn{2}{|c|}{} \\
    \hline
    \multicolumn{2}{|c|}{\textbf{Returns}} \\
    \hline
    \texttt{Boolean} & True when the user has guessed the number, False otherwise. \\
    \hline
    \textbf{Parameter} & \textbf{Description} \\
    \hline
    \texttt{Guess Number} & The number of the current guess, used in the prompt asking for the user to enter their guess. \\
    & \\
    \texttt{Target}   & The number the user is aiming to guess. \\
    \hline
    \multicolumn{2}{|c|}{} \\
    \multicolumn{2}{|p{12cm}|}{Perform Guess is responsible for coordinating the actions needed to perform a single guess within a game of \emph{Guess that Number}. The user's \texttt{guess} is read, and the value checked against the \texttt{target} value. A message is then output telling the user if the target value is less than, larger than, or equal to their guess. This function returns True when the user's guess is equal to the target.} \\
    \multicolumn{2}{|c|}{} \\
    \hline
  \end{tabular}
  \caption{Specification for the \texttt{Perform Guess} Function.}
  \label{tbl:perform guess}
\end{table}

The first task the Function needs to perform is to get the guess from the user. This can be performed in a \textbf{sequence}: display a prompt, read the value from the user. This first sequence is shown in \fref{fig:perform-guess-seq-1}.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.25\textwidth]{./topics/control-flow/diagrams/PerformGuess1}
   \caption{Initial Sequence in \texttt{Perform Guess}}
   \label{fig:perform-guess-seq-1}
\end{figure}

\clearpage
The next step in this sequence is to give the user feedback based upon their guess and the target number. This code requires a the ability to \emph{select} a given branch. The computer needs to output different messages based upon the users guess. This can be achieved with a \textbf{selection} block. Looking back at \fref{fig:selection-flow} there are three possible alternatives for implementing this selection. The \texttt{if} with no \texttt{else} is not a valid option as there are three paths we need to take. The \texttt{case} block is also not valid as we are not matching a value, but comparing values to each other. The last option is the \texttt{if-else} block, but this only has two branches. It is not going to be possible to code all three options within one block, but it can be achieved using two \texttt{if-else} blocks.

The first \texttt{if-else} block will check if the \texttt{target} is greater than the user's \texttt{guess}. If this is true then the computer can take the first branch and output the message `The number is larger than ' and the value from the user's guess. The flow chart for this part is shown in \fref{fig:perform-guess-seq-2}. This block is the third task in the sequence, this if block has a single entry, causes a branch in the flow, and will have a single exit.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.6\textwidth]{./topics/control-flow/diagrams/PerformGuess2}
   \caption{First branch in \texttt{Perform Guess}}
   \label{fig:perform-guess-seq-2}
\end{figure}

\mynote{
\begin{itemize}
  \item The conditions within the \nameref{sub:if_statement} are Boolean Expressions.
  \item This condition is checking if \texttt{target > guess}.
  \item There are now two paths through this code, one when \texttt{target} is \texttt{> guess}, and another when it is not.
\end{itemize}
}

\clearpage

Following the false path from the first decision, and we have a new location into which to insert a block. At this point we know the target is \emph{not} larger than the user's guess. At this point you can include another \texttt{if-else} block to check if the target is \textbf{less than} the user's guess. This is shown in \fref{fig:perform-guess-seq-3}.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.7\textwidth]{./topics/control-flow/diagrams/PerformGuess3}
   \caption{Second branch tests if the Target is less than the guess}
   \label{fig:perform-guess-seq-3}
\end{figure}

\mynote{
\begin{itemize}
  \item The newly added block is another \nameref{sub:if_statement}.
  \item This is nested within the \texttt{else} branch of the first If Statement.
  \item This code checks \emph{if} \texttt{target < guess}.
  \item There are now three paths through this code.
\end{itemize}
}

\clearpage
Taking the false path again, and now we have a location at which the target value \emph{must be} equal to the user's guess. The first condition checked if the \texttt{target} was larger than the \texttt{guess}, which it was not. The second condition checked if the \texttt{target} was less than the \texttt{guess}, which it was not. So the only way this can be the case if is the \texttt{target} and the \texttt{guess} are equal. This path can then be used to output the `Well done\ldots' message. This is shown in \fref{fig:perform-guess-seq-4}.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.8\textwidth]{./topics/control-flow/diagrams/PerformGuess4}
   \caption{The `Well done\ldots' message can be output on the third path}
   \label{fig:perform-guess-seq-4}
\end{figure}

\mynote{
\begin{itemize}
  \item The newly added block is a sequence, outputting the `Well done\ldots' message.
  \item On this third path the \texttt{target} and \texttt{guess} must be equal.
  \item Notice the single exit out of the second branch, when then flows to the single exit out of the first branch.
\end{itemize}
}

\clearpage
The last action in the code is to return a Boolean result indicating if the user's guess is equal to the target number.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.74\textwidth]{./topics/control-flow/diagrams/PerformGuess5}
   \caption{A Boolean result is returned from the Function}
   \label{fig:perform-guess-seq-5}
\end{figure}

\mynote{
\begin{itemize}
  \item The newly added block is a single action, defining the result to be returned.
  \item This action returns the result True when the \texttt{target} and the \texttt{guess} are equal.
\end{itemize}
}

\clearpage

\fref{fig:perform-guess-seq-7} shows the flowchart with annotations highlighting the different blocks within the code. The function starts off with a \textbf{sequence} that contains all of the code in the Function. Within this there is the \textbf{selection}, that internally contains another \textbf{selection}.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.85\textwidth]{./topics/control-flow/diagrams/PerformGuess7}
   \caption{Blocks in the \texttt{Perform Guess} code}
   \label{fig:perform-guess-seq-7}
\end{figure}

\mynote{
\begin{itemize}
  \item Notice that each block has a single path going into it, and a single path coming out.
\end{itemize}
}

% subsubsection combining_blocks_for_the_guessing_game (end)

\clearpage
\subsubsection{Setting the result using an Expression} % (fold)
\label{ssub:setting_the_result_using_an_expression}

\fref{fig:perform-guess-seq-6} shows the \emph{trick} that is being performed at the end of \texttt{Perform Guess}'s code. \texttt{Perform Guess} needs to return a result indicating if the user has guessed the number of not. This will be a Boolean value, with True indicating they guessed the number. Initially it may seem that you need a \textbf{selection} block to enable this, as shown on the right of \fref{fig:perform-guess-seq-6}.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/control-flow/diagrams/PerformGuess6}
   \caption{Calculating \texttt{Perform Guess}'s result}
   \label{fig:perform-guess-seq-6}
\end{figure}

\mynote{
\begin{itemize}
  \item In \emph{most} cases it is better to have less code if possible, as long as this does not obscure the purpose of the code.
  \item This is an example of replacing \emph{actions} with \emph{data}. The more \emph{intelligence} you can build into the data in your programs the more flexible they will be.
\end{itemize}
}

\csection{This is achieved in C using \csnipet{return target == guess;}}

\passection{This is achieved in Pascal using \passnipet{result := target = guess;}}

% subsubsection setting_the_result_using_an_experssion (end)

\clearpage
\subsubsection{The Pseudocode for \texttt{Perform Guess}} % (fold)
\label{ssub:the_pseudocode_for_perform_guess}

Listing \ref{plst:perform_guess} contains the Pseudocode for the \texttt{Perform Guess} logic from the flowchart in \fref{fig:perform-guess-seq-5}. Notice how the indentation in this mirrors the block structures in the flowchart. It is good practice to indent your code in this way as it helps you, and any person who reads your code, to see the structure of the logic. You will be able to avoid many errors by making sure that you always indent your code so that it highlights the code's structure.

\pseudocode{plst:perform_guess}{Pseudocode for \texttt{Perform Guess}}{topics/control-flow/application/PerformGuess.txt}

\mynote{
\begin{itemize}
  \item Code indentation makes it easier to read, and helps locate many common issues.
  \item Tab you code in within a \textbf{structured statement}.
  \begin{itemize}
    \item Indent the code in the branches of an \nameref{sub:if_statement} and \nameref{sub:case_statement}.
    \item Indent the code within the body of the \textbf{While Loop} and the \textbf{Do While} or \textbf{Repeat Until} loops.
  \end{itemize}
  \item Make this a habit. When you code a \nameref{sub:branching} or \nameref{sub:looping} statement automatically indent the next line of code.
  \item Always keep you code neat, make it look good.
  \item The C code for \texttt{Perform Guess} is shown in Listing \ref{clst:perform_guess}.
  \item The Pascal code for \texttt{Perform Guess} is shown in Listing \ref{paslst:perform_guess}.
  \item Notice how the two code samples are laid out in a similar way. The indentation makes it easy to identify which statements are associated with each of the branches through the Function.
\end{itemize}
}

\clearpage

\csection{\ccode{clst:perform_guess}{C code for \texttt{Perform Guess}}{topics/control-flow/application/perform-guess.c}}

\passection{\pascode{paslst:perform_guess}{Pascal code for \texttt{Perform Guess}}{topics/control-flow/application/PerformGuess.pas}}

% subsubsection the_pseudocode_for_ (end)

% subsection designing_control_flow_for_guess_that_number (end)

\clearpage
\subsection{Designing Control Flow for Play Game} % (fold)
\label{sub:designing_control_flow_for_play_game}

The \texttt{Play Game} Procedure is another artefact that will require some some thought to design its logic. \tref{tbl:play game} contains the specification for this Procedure. It will be responsible for coordinating the actions of the game, while \texttt{Perform Guess} coordinates the actions for a \emph{single} guess.

\begin{table}[h]
  \centering
  \begin{tabular}{|c|p{9cm}|}
    \hline
    \multicolumn{2}{|c|}{\textbf{Procedure}} \\
    \hline
    \multicolumn{2}{|c|}{} \\
    \multicolumn{2}{|c|}{\texttt{Play Game}} \\
    \multicolumn{2}{|c|}{} \\
    \hline
    \multicolumn{2}{|c|}{} \\
    \multicolumn{2}{|p{12cm}|}{Play Game is responsible for coordinating the actions involved in playing a single game of \emph{Guess that Number}. Initially the computer will generate a random target value, and output starting text. Then it will repeatedly ask the user to guess the number, until either the user has guessed the value or they have run out of guesses. If the user does run out of guesses then the computer ends the game, and tell the user the target value.} \\
    \multicolumn{2}{|c|}{} \\
    \hline
  \end{tabular}
  \caption{Specification for the \texttt{Play Game} Procedure.}
  \label{tbl:play game}
\end{table}

The implementation of this Procedure will require us to store some data. The following \nameref{sub:local_variable}s will be needed to store data within this Procedure:
\begin{itemize}
  \item \texttt{\textbf{My Number}}: This will store the computer's randomly chosen number.
  \item \texttt{\textbf{Guess Number}}: This will store the current guess the user is at, allowing the computer to stop looping when the number of guesses is exceeds 7.
  \item \texttt{\textbf{Got It}}: A Boolean value to indicate if the user did guess the number, allowing the computer to stop looping when the user guesses the number.
\end{itemize}

The flowchart for this is shown in \fref{fig:play-game}, and again in \fref{fig:play-game-diag1} with its blocks highlighted. This code uses a \textbf{repetition} to ask the user to perform up to 7 guesses. The condition on this loop occurs \emph{after} the loop body as the user must have at least one guess.

There is also a \textbf{selection} after the loop to output the answer if the user ran out of guesses. This is only done when the user has not guessed it themselves. This does not need to perform any other actions when the user did guess the number, so the False branch has no additional actions. In code this would be implemented with an \nameref{sub:if_statement} without an \textbf{else} branch.

\mynote{
\begin{itemize}
  \item A loop may have its condition before or after its instructions.
  \item If the condition is after the loop, as it is with \texttt{Play Game}, then this gives a loop that runs at least once.
  \item The else branch on the If Statement is optional. If you have no processing for this branch you leave the \texttt{else} off in the code.
  \item The condition of the If Statement in \texttt{Play Game} check if the \texttt{Got It} is False using the \texttt{not} logical operator.
\end{itemize}
}

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.52\textwidth]{./topics/control-flow/diagrams/PlayGame}
   \caption{Logic for the \texttt{Play Game} Procedure}
   \label{fig:play-game}
\end{figure}

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.65\textwidth]{./topics/control-flow/diagrams/PlayGame1}
   \caption{Blocks in the \texttt{Play Game} code}
   \label{fig:play-game-diag1}
\end{figure}

\clearpage

\lref{plst:play_game} contains the Pseudocode for the \texttt{Play Game} Procedure. This uses Constants for \texttt{MAX\_NUMBER} and a \texttt{MAX\_GUESSES}, this will make it easier to change the range of the numbers and the associated number of guesses.

The flowchart for \texttt{Play Game} includes a \nameref{sub:post_test_loop}, which can be coded as either a \texttt{do...while} or a \texttt{repeat...until} loop. This two variations are shown in step 7 of \lref{plst:play_game}, with the while version appearing as a comment on the following line. Both of these versions have the same result, but do require different conditions. The two implementations of this are shown in \lref{clst:play_game} and \lref{paslst:play_game}. It is important to understand that the basic ideas of a \textbf{post-test loop} is the same regardless of whether it is coded using \texttt{do...while} or \texttt{repeat...until}.

\pseudocode{plst:play_game}{Pseudocode for \texttt{Play Game}}{topics/control-flow/application/PlayGame.txt}

\mynote{
\begin{itemize}
  \item Notice the indentation make it easier to see which instructions are within the loop and branches of this code.
  \item The C code for this is is \lref{clst:play_game}.
  \item The Pascal code for this is in \lref{paslst:play_game}.
  \item The C and Pascal libraries have different Functions for getting random numbers.
\end{itemize}
}

\csection{In C there is a \texttt{random()} function in the \texttt{stdlib.h} header file. This returns a random integer value. To get this between 1 and MAX\_NUMBER you can use the modulo operator (\%) that returns the remainder after division. The expression to use is \csnipet{random() \% MAX\_NUMBER + 1}}

\passection{In Pascal there is a \texttt{Random} function in the System unit. This takes a single parameter representing the number of random values to generate (0..n-1). The expression to use is \passnipet{Random(MAX\_NUMBER) + 1}}

\clearpage
\csection{\ccode{clst:play_game}{C code for \texttt{Play Game}}{topics/control-flow/application/play-game.c}}

\passection{\pascode{paslst:play_game}{Pascal code for \texttt{Play Game}}{topics/control-flow/application/PlayGame.pas}}

% subsection designing_control_flow_for_play_game (end)

\clearpage
\subsection{Designing Control Flow for Print Line} % (fold)
\label{sub:designing_control_flow_for_print_line}

\texttt{Print Line} is a short Procedure used to print a line of `-' characters to the Terminal. The flowchart for this Procedure is shown in \fref{fig:print-line}, and again with the blocks highlighted in \fref{fig:print-line-diag-1}.

\begin{table}[h]
  \centering
  \begin{tabular}{|c|p{9cm}|}
    \hline
    \multicolumn{2}{|c|}{\textbf{Procedure}} \\
    \hline
    \multicolumn{2}{|c|}{} \\
    \multicolumn{2}{|c|}{\texttt{Print Line}} \\
    \multicolumn{2}{|c|}{} \\
    \hline
    \textbf{Parameter} & \textbf{Description} \\
    \hline
    \texttt{Length} & The number of characters to print. Represents the length of the line. \\
    \hline
    \multicolumn{2}{|c|}{} \\
    \multicolumn{2}{|p{12cm}|}{Prints a number of `-' characters to the Terminal. The number of characters to print is specified in the \texttt{Length} parameter.} \\
    \multicolumn{2}{|c|}{} \\
    \hline
  \end{tabular}
  \caption{Specification for the \texttt{Print Line} Procedure.}
  \label{tbl:print line}
\end{table}

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.37\textwidth]{./topics/control-flow/diagrams/PrintLine}
   \caption{Flowchart for the logic in \texttt{Print Line} code}
   \label{fig:print-line}
\end{figure}

\clearpage

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.57\textwidth]{./topics/control-flow/diagrams/PrintLine1}
   \caption{Blocks in the \texttt{Print Line} code}
   \label{fig:print-line-diag-1}
\end{figure}

\pseudocode{plst:print_line}{Pseudocode for \texttt{Print Line}}{topics/control-flow/application/PrintLine.txt}

\mynote{
\begin{itemize}
  \item This code includes a \nameref{sub:local_variable} \texttt{i}.
  \item \texttt{i} is used to count the number of times the loop has executed, allowing it to stop when it has printed enough dashes.
  \item This uses a \nameref{sub:pre_test_loop}. This makes sure that no characters are printed if the length is less than or equal to zero.
  \item Notice that a sequence can be placed within a loop.
\end{itemize}
}

% subsection designing_control_flow_for_print_line (end)

\clearpage
\subsection{Designing the Control Flow for Main} % (fold)
\label{sub:designing_the_control_flow_for_main}

The last Procedure is \texttt{Main}. This is responsible for coordinating the actions of the program. It will call \texttt{Play Game} in a loop that repeatedly plays the game until the user decides to quit. \texttt{Main} will have one local variable called \texttt{again}. This will store a character, and will be used to store the value read from the user's response to the `\emph{play again}' prompt.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.31\textwidth]{./topics/control-flow/diagrams/Main}
   \caption{Flowchart for the \texttt{Main} Procedure}
   \label{fig:main}
\end{figure}

\clearpage

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.55\textwidth]{./topics/control-flow/diagrams/Main1}
   \caption{Blocks in the \texttt{Main} code}
   \label{fig:main-1}
\end{figure}

\mynote{
Computers cannot generate truly random numbers. Instead it uses a numeric sequence that appears to be random. Seeding the generator with the current time ensures that this random sequence starts at a different value each time the program is run.
}

% subsection designing_the_control_flow_for_main (end)

\clearpage

\subsection{Writing the Code for Guess That Number} % (fold)
\label{sub:writing_the_code_for_guess_that_number}

Flowcharts and Pseudocode communicate the same ideas. They describe the actions that need to be performed within your code. The following two sections, \sref{sec:control_flow_in_c} \nameref{sec:control_flow_in_c} and  \sref{sec:control_flow_in_pascal} \nameref{sec:control_flow_in_pascal}, contain a description of the syntax needed to code these control flow statements in the C and Pascal programming languages.

\mynote{
Remember the basic process for reading the Syntax Diagrams is to:
\begin{enumerate}
  \item Find the page with the Syntax rule you are interested in knowing about.
  \item Have a quick look at the Syntax Diagram and the rules it contains. Read each rule, and get a basic feel for how it is going to come together for your program.
  \item Read the example to see one way of using the Rule. The Syntax Diagram can be used to create any number of variations of the rule, the example gives you at least one way these rules can be coded.
  \item Return to the diagram and make sure you can match each part of the example back to the rule that created it.
  \item Look up any related rules that are not explained on this rule's page.
\end{enumerate}
}

% subsection writing_the_code_for_guess_that_number (end)

\subsection{Compiling and Running Guess that Number} % (fold)
\label{sub:compiling_and_running_guess_that_number}

Once you have completed the code for this program you need to compile and run it. As this uses random numbers you cannot generate standard test data in order to check the execution. Instead you should perform a number of executions and test the different paths through the program. The main condition you want to check are:
\begin{itemize}
  \item Test failing to get the number in seven guesses.
  \item Test getting the number correct within seven guesses.
  \item Check the output messages when your guess is less than the number (you can enter a guess below 0).
  \item Check the output message when your guess is larger than the number (you can enter a guess larger than 100).
  \item Check that the random sequence is different each time, if its not make sure you have seeded the random number generator.
\end{itemize}

% subsection compiling_and_running_guess_that_number (end)


\documentclass[12pt]{article}

\usepackage[margin=1.1in]{geometry}
\input{../../syllabi/preamble}

%%edit these!!
\newcommand{\coursedept}{Math}
\newcommand{\coursenumber}{341}
\newcommand{\coursenumbercrosslisted}{/ 650.03~}
\newcommand{\semester}{Spring}
\newcommand{\numcredits}{3}
\newcommand{\lectimeandloc}{Mon and Wed 3:10 -- 4:25PM / on zoom}
\newcommand{\requiredlabtimeandloc}{}
\newcommand{\tataofficehourtimeandloc}{TA / TA Office Hours / Loc & Abhinav Patil / TBD week-week / on zoom}
\newcommand{\numtheoryhws}{7--10}
\newcommand{\extrahwzero}{}
\newcommand{\hwzerodue}{Wednesday, Feb 3 11:59PM}
\newcommand{\lastdatetimetohandinhomeworks}{May 18 at noon}
\newcommand{\midtermonedateandlocation}{Wednesday, March 3 on zoom during class time}
\newcommand{\midtermtwodateandlocation}{Wednesday, April 21 on zoom during class time}
\newcommand{\finaldateandlocation}{TBD but on zoom}

%%%unchanged
\newcommand{\coursewebpageurl}{https://github.com/kapelner/QC_\coursedept_\coursenumber_\semester_\the\year}
\newcommand{\coursewebpagelink}{\href{\coursewebpageurl}{course homepage}}
\newcommand{\slackurl}{https://QC\coursedept\coursenumber\semester\the\year.slack.com/}
\newcommand{\slacklink}{\href{\slackurl}{slack}}

\input{../../syllabi/_header}

\section*{Course Overview}

MATH 341. Bayesian Modeling. 3 hr.; 3 cr. Prereq.: MATH 241. A review of frequentist methods followed by a survey of statistical modeling using the Bayesian framework: prior distribution design, including Jeffrey’s priors; likelihood models; posterior probabilities; hypothesis tests; Bayesian linear regression; Gibbs sampling; Metropolis-Hastings (basic Bayesian computing).  Emphasis on real-world applications, including those in finance and applied probability. The goal is to be fluent enough to understand how industry uses Bayesian modeling and computation by the end of the course.

Statistics has historically been taught from the frequentist perspective. Recently, the Bayesian perspective has become popular (1) due to their models' performance on previously intractable problems and the recent availability of inexpensive computational power and (2) it solves many philosophical quandaries in the Frequentist perspective. Further, many scientific journals are moving away from Frequentist p-values and confidence intervals in favor of the Bayesian analogues. It is imperative to learn this perspective as you will see these models in industry and this mode of thinking is becoming mainstream in science at large. \pagebreak

Here are the main topics to be covered:

\begin{itemize}
\itemsep -0.0em
\item Data modeling with parametric families
\item Bayes Rule as it applies to parameters
\item prior distribution design
\item Jeffrey's priors
\item likelihood models and maximum likelihood estimators
\item posterior probabilities
\item Bayesian inference: credibility intervals
\item Bayesian inference: p-values for hypothesis tests
\item mixture priors
\item mixture models
\item Newton-Raphson and Expectation-Maximization Algorithms
\item basic computing for Bayesian models including Gibbs sampling and Metropolis-Hastings Sampling
\end{itemize}

\noindent The only prerequisite is Math 241 or equivalent. You should be familiar with the following:

\begin{itemize}
\itemsep -0.0em
\item Basic Set Theory
\item Counting Methods | permutations and combinations
\item Basic Probability Theory | axioms, conditional probability, in/dependence
\item Modeling with Discrete Random Variables: Bernoulli, Hypergeometric, Binomial, Pois-
son, Geometric, Negative Binomial, Uniform Discrete and others
\item Expectation and Variance
\item Modeling with Continuous Random Variables: Exponential, Uniform and Normal
\item Frequentist Confidence Intervals and Hypothesis Testing for one-sample proportions
\end{itemize}

\noindent We will review the above \textit{throughout the semester} when needed and we will do so rapidly. \\

\textbf{This is not your typical mathematics course.} This course develops ideas and concepts for helping to make decisions based on randomness and we will do lots of modeling of real-world situations. The course does not dwell on theory nor details of computation but will make use of computation especially using the \texttt{R} statistical language.

\section*{Course Materials}

\paragraph{Textbook:} Introduction to Bayesian Statistics by William M. Bolstad First Edition. It can be purchased used on \href{http://www.amazon.com/gp/offer-listing/0471270202/ref=sr_1_2_twi_1_har_olp?ie=UTF8&qid=1433515305&sr=8-2&keywords=introduction+to+bayesian+statistics+bolstad}{Amazon}. This is \textit{recommended}. It is a way to get ``another take'' on the material. However, most of the material in the class comes from the lecture notes.

\paragraph{Popular Book:} We will also be reading the non-fiction novel \qu{The Theory that Would not Die: How Bayes' Rule Cracked the Enigma Code, Hunted Down Russian Submarines, and Emerged Triumphant from Two Centuries of Controversy} by Sharon Bertsch McGrayne which can also be purchased on \href{http://www.amazon.com/Theory-That-Would-Not-Die/dp/0300188226/ref=sr_1_1?ie=UTF8&qid=1454261896&sr=8-1&keywords=The+Theory+that+Would+not+Die}{Amazon}. This is \textit{required} --- you will have homework questions directly from this book.

\paragraph{Computer Software:} We will also be using \texttt{R} which is a free, open source statistical programming language and console. You can download it from: \url{http://cran.mirrors.hoobly.com/}. I do not expect you to do \textit{any} programming. I will be giving you \texttt{R} code to run and expect you to interpret the results based on concepts explained during the course.

\paragraph{Calculator:} You can use a TI-84, 85, 89 or any calculator which you wish. I strongly suggest you use \href{http://www.wolframalpha.com/}{Wolfram Alpha} and its smartphone app.

\input{../../syllabi/_the650section}

\input{../../syllabi/_announcements_on_slack}

\input{../../syllabi/_use_of_slack}

\input{../../syllabi/_standard_class_meetings}

I am \inred{canceling} Monday, May 17 (the last meeting) due to a Jewish holiday. This meeting would have been a review session for the final exam. We will decide when to have this session during finals week that fits the majority's schedule.

\input{../../syllabi/_zoom_policies}

\input{_lecture_schedule}

\input{../../syllabi/_lecture_upload}

\section*{Homework}

\input{../../syllabi/_theory_hws_text}

\input{../../syllabi/_theory_hws_submission_text}
\input{../../syllabi/_philosophy_hws}

\input{../../syllabi/_time_spent_hws}

\input{../../syllabi/_late_hw_policy}

\input{../../syllabi/_latex_hw_bonus_policy}

\input{../../syllabi/_hw_ec_policy}

\input{../../syllabi/_hw_0}

\section*{Examinations}

\input{../../syllabi/_examination_text}

\input{../../syllabi/_standard_exam_schedule}

\subsection*{Exam Policies and Materials}

\input{../../syllabi/_examination_policies}

\input{../../syllabi/_standard_cheat_sheet_policy}

\input{../../syllabi/_cheating_on_exams_and_missing_exams}
\input{../../syllabi/_special_services}

\input{../../syllabi/_class_participation}

\input{../../syllabi/_zoom_attendance}

\input{../../syllabi/_standard_grading_and_grading_policy}

\input{../../syllabi/_advanced_course_grade_distribution}

\input{../../syllabi/_grade_checking_on_gradesly}

\input{../../syllabi/_auditing_policy}

\end{document}