% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode

% This is a simple template for a LaTeX document using the "article" class.
% See "book", "report", "letter" for other types of document.

\documentclass[10pt,twocolumn]{article}

\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)
\usepackage{graphicx}
\usepackage{listings}
\usepackage{xcolor,colortbl}
\usepackage[section]{placeins}
\usepackage{amsthm}
\usepackage{mathtools}
\graphicspath{ {images/} }

%%% Examples of Article customizations
% These packages are optional, depending whether you want the features they provide.
% See the LaTeX Companion or other references for full information.

%%% PAGE DIMENSIONS
\usepackage{geometry} % to change the page dimensions
\geometry{a4paper} % or letterpaper (US) or a5paper or....
\geometry{margin=0.55in} % for example, change the margins to 2 inches all round
% \geometry{landscape} % set up the page for landscape
%   read geometry.pdf for detailed page layout information

\usepackage{graphicx} % support the \includegraphics command and options

% \usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent

%%% PACKAGES
\usepackage{booktabs} % for much better looking tables
\usepackage{array} % for better arrays (eg matrices) in maths
\usepackage{paralist} % very flexible & customisable lists (eg. enumerate/itemize, etc.)
\usepackage{verbatim} % adds environment for commenting out blocks of text & for better verbatim
\usepackage{subfig} % make it possible to include more than one captioned figure/table in a single float
\usepackage{indentfirst}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{tabularx}
% These packages are all incorporated in the memoir class to one degree or another...

%%% HEADERS & FOOTERS
\usepackage{fancyhdr} % This should be set AFTER setting up the page geometry
\pagestyle{fancy} % options: empty , plain , fancy
\renewcommand{\headrulewidth}{0pt} % customise the layout...
\lhead{}\chead{}\rhead{}
\lfoot{}\cfoot{\thepage}\rfoot{}

%%% SECTION TITLE APPEARANCE
\usepackage{sectsty}
\allsectionsfont{\sffamily\mdseries\upshape} % (See the fntguide.pdf for font help)
% (This matches ConTeXt defaults)

%%% ToC (table of contents) APPEARANCE
\usepackage[nottoc,notlof,notlot]{tocbibind} % Put the bibliography in the ToC
\usepackage[titles,subfigure]{tocloft} % Alter the style of the Table of Contents
\renewcommand{\cftsecfont}{\rmfamily\mdseries\upshape}
\renewcommand{\cftsecpagefont}{\rmfamily\mdseries\upshape} % No bold!
\setlength{\parindent}{0.5cm}

%%% END Article customizations

%%% The "real" document content comes below...

\title{Redundant Primes In Lemoine Conjecture}
\author{Marcin Barylski}
\date{\small{Published: August 20, 2020 \\ The last update: August 26, 2020}}

\definecolor{Gray}{gray}{0.85}
\definecolor{LightCyan}{rgb}{0.88,1,1}
\newcolumntype{a}{>{\columncolor{Gray}}c}
\newcolumntype{b}{>{\columncolor{white}}c}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}

\newcommand\bigforall{\mbox{\huge $\mathsurround0pt\forall$}}
\newcommand\bigexists{\mbox{\huge $\mathsurround0pt\exists$}}

\begin{document}
\maketitle

\begin{abstract}
Lemoine's conjecture ($LC$), still unsolved, states that all positive odd integer $\geq 7$ can be expressed as the sum of a prime and an even semiprime. But do we need all primes to satisfy this conjecture? This work is devoted to selection of must-have primes and formulation of stronger version of $LC$ with reduced set of primes.
\end{abstract}

\section{Problem statement}

Lemoine's conjecture ($LC$) \cite{Lemoine1896} asserts that all positive odd integer $n \geq$ 7 can be expressed as the sum of a prime $p$ and an even semiprime $2q$: $n = 2m + 1 = p + 2q$ and let's call such pair $(p, q)$ a Lemoine's Partition ($LP$) of $n$. Let's denote this relation as $LC (2m+1, p, 2q)$. Then $LC$ can be written as (1):

\begin{equation}
\displaystyle\mathop{\bigforall}_{x \textgreater 2, x \in \mathbb{N}} \displaystyle\mathop{\bigexists}_{p_1, p_2 \in \mathbb{P}} LC (2x+1, p_1, 2 \times p_2)
\end{equation}

But maybe we can formulate much stronger version of (1)? The set of prime numbers $\mathbb{P}$ is dense, number of $LP(n)$ is increasing with increasing $n$, thus question if stronger version $LC$ is possible is legitimate (2):

\begin{equation}
\displaystyle\mathop{\bigforall}_{x \textgreater 2, x \in \mathbb{N}} \displaystyle\mathop{\bigexists}_{p_1, p_2 \in \mathbb{R}} LC (2x+1, p_1, 2 \times p_2)
\end{equation}

where $\left\vert \mathbb{R} \right\vert \textless \left\vert \mathbb{P} \right\vert$. By design $\mathbb{R}$ contains primes only.

\section{Algorithm}

Elimination of primes must start from the fact that every prime is potentially required (Lemma 1).

\begin{lemma}
Every prime is present is some Lemoine's  partition of odd number.
\end{lemma}
\begin{proof}
TBD
\end{proof}

Further elimination of primes from $\mathbb{P}$ to build $\mathbb{R}$ requires appropriate algorithm $A$ which is able to resign from a given prime, even if it is present in some $LPs$. Such algorithm $A$ could look as follows:

\begin{enumerate}
\item  Let $\mathbb{R}$ is empty, a set of $\mathbb{R}_i$ is empty and $n = 5$. Let's assume that we break calculations at $n = n_{max} \textgreater 5$.
\item  \textbf{New turn}: $n = n + 2$.
\item Break calculations if $n \textgreater n_{max}$.
\item  If  $\mathbb{R}$ is sufficient to fulfill LC for all odd numbers $q$ ($ 7 \leq q \leq n$), then go to \textbf{New turn}.
\item  If  we have a set of  $\mathbb{R}_i$ and any $\mathbb{R}_j$ belonging to this set is sufficient to fulfill LC for all odd numbers $q$ ($ 7 \leq q \leq n$), then $\mathbb{R} = \mathbb{R}_j$, we forget all $\mathbb{R}_i$ and go to \textbf{New turn}.
\item  If  not, find all $LP(n)$ and build as many candidates for $\mathbb{R}$ (let's call them $\mathbb{R}_i$) as required. As a base use either (as a first choice) $\mathbb{R}$ (if $\mathbb{R}_i$ does not exist) or all previous $\mathbb{R}_i$ (if they are present).
\item Go to \textbf{New turn}.
\end{enumerate}

\section{Results}

Table 1 presents the very first rounds of algorithm eliminating primes required to satisfy $LC$ - it is depicting current values of both $\mathbb{R}$ and $\mathbb{R}_i$, plus additional set $\mathbb{E}$ which contains primes that were present in $LPs$ so far but can be eliminated without hurting $LC$.

\begin{table}[ht]
\centering
\captionsetup{justification=centering}
\caption{Results of the first rounds of $A$}
\label{tablealgostart}
\begin{tabular}{lalalalal}
  \rowcolor{LightCyan}
  $n$ & $LP(n)$ & $\mathbb{R}$ & $\mathbb{R}_i$ & $\mathbb{E}$ \\
  \hline
7  & 2$\times$2+3 & \{2,3\}& $\emptyset$ & $\emptyset$\\
  \hline
9  & \begin{tabular}[c]{@{}l@{}}2$\times$2+5\\2$\times$3+3\end{tabular} & \{2,3\} & $\emptyset$ & \{5\}\\
  \hline
11  & \begin{tabular}[c]{@{}l@{}}2$\times$2+7\\2$\times$3+5\end{tabular} & $\emptyset$  & \begin{tabular}[c]{@{}l@{}}\{2,3,7\}\\\{2,3,5\}\end{tabular} & $\emptyset$ \\
  \hline
13  & \begin{tabular}[c]{@{}l@{}}2$\times$3+7\\2$\times$5+3\end{tabular} & $\emptyset$  & \begin{tabular}[c]{@{}l@{}}\{2,3,7\}\\\{2,3,5\}\end{tabular} & $\emptyset$ \\
  \hline
15  & \begin{tabular}[c]{@{}l@{}}2$\times$2+11\\2$\times$5+5\end{tabular} & \{2,3,5\} & $\emptyset$ & \{7,11\}\\
\end{tabular}
\end{table}

For instance, if we take into account all positive odd numbers $5 \textless n \textless 16$, then we need a set of primes $\{2,3,5\}$ to satisfy $LC$ for all $n$ checked so far and $\{7,11\}$ ($LC(11, 2, 7)$, $LC(13, 3, 7)$, $LC(15, 2, 11)$) are our candidates for elimination.

\begin{figure}[!ht]
\centering
\captionsetup{justification=centering}
\includegraphics[width=9cm]{f_required_primes}
\caption[caption]{Number of required primes for LC vs. other functions ($7 \leq n \leq 2.5 \times 10^4 +1$)}
\label{fig:requiredprimes}
\end{figure}

Figures 1, 2 and 3 are depicting findings after analyzing odd numbers $7 \leq n \leq 2.5 \times 10^4 + 1$. First of all, function of required primes increases but slower than $\Pi(n)$.

\begin{figure}[!ht]
\centering
\captionsetup{justification=centering}
\includegraphics[width=9cm]{f_number_of_possible_sets}
\caption[caption]{Number of $\mathbb{R}_i$ ($7 \leq n \leq 2.5 \times 10^4 + 1$)}
\label{fig:numberofsets}
\end{figure}

It is also interesting that algorithm $A$ has sporadic congestions in terms of number of $\mathbb{R}_i$ - generally its number at a given time is low but there are quite frequent situation when it is high, even exceeding 200000 (this means that we have 200000+ subsets containing candidates for $\mathbb{R}$) - it happens when there are still some $\mathbb{R}_i$ and new $n$ requires new prime to be used which is multiplying number of $\mathbb{R}_i$ in the next round of $A$. Surprisingly, shortly after number of $\mathbb{R}_i$ is decreasing to much smaller values. Congestions could be better handled (from memory utilization standpoint) if in $A$ instead of seperate lists (paths) we have a tree (where branches/leaves represent variable part added on top of a common base).

\begin{figure}[!ht]
\centering
\captionsetup{justification=centering}
\includegraphics[width=9cm]{f_number_of_eliminated_primes}
\caption[caption]{Number of eliminated primes ($7 \leq n \leq 2.5 \times 10^4 + 1$)}
\label{fig:numberofeliminatedprimes}
\end{figure}

\begin{figure}[!ht]
\centering
\captionsetup{justification=centering}
\includegraphics[width=9cm]{f_eliminated_primes_to_partitions}
\caption[caption]{Ratio of number of eliminated primes to number of LPs, including average value ($7 \leq n \leq 2.5 \times 10^4 + 1$)}
\label{fig:numberofeliminatedprimestopartitions}
\end{figure}

\begin{figure}[!ht]
\centering
\captionsetup{justification=centering}
\includegraphics[width=9cm]{f_eliminated_primes_to_req_primes}
\caption[caption]{Ratio of number of eliminated primes to number of required primes, including average value ($7 \leq n \leq 2.5 \times 10^4 + 1$)}
\label{fig:numberofeliminatedprimestorequiredprimes}
\end{figure}

\begin{figure}[!ht]
\centering
\captionsetup{justification=centering}
\includegraphics[width=9cm]{f_required_eliminated_primes_percentage}
\caption[caption]{Percentages of required and eliminated primes to all primes possible at a given point ($7 \leq n \leq 2.5 \times 10^4 + 1$)}
\label{fig:requiredeliminatedprimespercentage}
\end{figure}

\section{Summary and next steps}

Executed experiments, run for all even $n \leq 2.5 \times 10^4$, confirmed that we do not need entire set of primes to satisfy $LC$. Appendix A lists eliminated primes after checking all odd numbers $7 \ leq n \leq 2.5 \times 10^4 + 1$ - the smallest eliminated prime is $11$. Of course, exercised cases do not proof that eventually such set exists for all even $n$ but observed trends (Figure $\ref{fig:numberofeliminatedprimes}$, Figure $\ref{fig:numberofeliminatedprimestopartitions}$,  Figure $\ref{fig:numberofeliminatedprimestorequiredprimes}$,  Figure $\ref{fig:requiredeliminatedprimespercentage}$) give strong foundation that such set exists indeed and conjecture (2) is true. \par

\begin{thebibliography}{9}
\bibitem{Lemoine1896}
  Emile Lemoine, \emph{ L'intermédiare des mathématiciens} 1 (1894), 179; ibid 3 (1896), 151.

\appendix
\section{List of redundant primes}

Based on empirical verification done for all odd numbers $7 \leq n \leq 2.5 \times 10^4 + 1$:

[11, 17, 19, 41, 43, 61, 83, 89, 101, 103, 109, 113, 127, 139, 149, 151, 157, 167, 191, 197, 227, 233, 257, 263, 269, 271, 277, 283, 307, 313, 347, 373, 383, 397, 409, 419, 421, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 509, 541, 557, 563, 571, 577, 587, 599, 601, 617, 619, 631, 641, 653, 659, 673, 677, 691, 701, 719, 727, 733, 739, 743, 751, 769, 773, 787, 811, 827, 839, 857, 859, 863, 877, 881, 887, 907, 911, 919, 941, 953, 967, 971, 977, 983, 991, 997, 1013, 1021, 1031, 1033, 1039, 1051, 1063, 1069, 1087, 1093, 1097, 1103, 1109, 1123, 1129, 1151, 1181, 1187, 1193, 1213, 1217, 1223, 1229, 1231, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1303, 1307, 1319, 1321, 1327, 1367, 1373, 1409, 1427, 1429, 1439, 1447, 1451, 1471, 1481, 1483, 1487, 1489, 1493, 1511, 1531, 1553, 1559, 1567, 1571, 1579, 1597, 1601, 1607, 1609, 1613, 1619, 1627, 1637, 1657, 1663, 1667, 1693, 1697, 1699, 1709, 1721, 1723, 1741, 1747, 1777, 1783, 1787, 1823, 1847, 1861, 1867, 1871, 1873, 1877, 1889, 1901, 1907, 1913, 1931, 1933, 1951, 1973, 1979, 1987, 1993, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2137, 2141, 2143, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2281, 2293, 2297, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2383, 2389, 2393, 2411, 2417, 2423, 2437, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2633, 2647, 2657, 2659, 2663, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2713, 2731, 2741, 2749, 2753, 2767, 2789, 2797, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2999, 3001, 3011, 3037, 3041, 3049, 3061, 3067, 3083, 3089, 3109, 3137, 3163, 3169, 3181, 3187, 3191, 3203, 3217, 3221, 3229, 3257, 3259, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3373, 3391, 3407, 3413, 3433, 3449, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3533, 3539, 3541, 3559, 3571, 3581, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3671, 3673, 3677, 3691, 3701, 3709, 3719, 3727, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3911, 3917, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4091, 4099, 4111, 4127, 4129, 4133, 4157, 4159, 4201, 4211, 4217, 4229, 4231, 4241, 4253, 4259, 4261, 4271, 4273, 4283, 4297, 4327, 4337, 4349, 4357, 4363, 4391, 4397, 4421, 4423, 4441, 4447, 4451, 4457, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4621, 4637, 4639, 4643, 4649, 4657, 4673, 4679, 4703, 4721, 4723, 4751, 4759, 4783, 4789, 4793, 4799, 4801, 4813, 4817, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4933, 4937, 4943, 4951, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5077, 5081, 5087, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5209, 5227, 5231, 5233, 5261, 5273, 5279, 5281, 5297, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5669, 5683, 5689, 5693, 5701, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5861, 5867, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6203, 6211, 6217, 6229, 6257, 6263, 6271, 6277, 6299, 6301, 6311, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6451, 6473, 6491, 6529, 6547, 6553, 6569, 6571, 6577, 6581, 6607, 6619, 6637, 6653, 6659, 6661, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6761, 6763, 6779, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6997, 7001, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7283, 7297, 7309, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7643, 7649, 7669, 7673, 7687, 7691, 7699, 7703, 7717, 7723, 7741, 7757, 7759, 7789, 7793, 7817, 7829, 7841, 7853, 7867, 7879, 7883, 7901, 7919, 7927, 7933, 7937, 7949, 7951, 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8167, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8353, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8467, 8513, 8521, 8527, 8537, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8969, 8971, 8999, 9001, 9007, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, 9521, 9533, 9547, 9551, 9601, 9613, 9619, 9623, 9631, 9643, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9907, 9923, 9931, 9941, 9949, 9967, 10007, 10009, 10069, 10079, 10091, 10093, 10099, 10103, 10111, 10133, 10139, 10151, 10159, 10163, 10169, 10177, 10181, 10193, 10211, 10223, 10243, 10247, 10253, 10267, 10271, 10273, 10289, 10301, 10303, 10313, 10321, 10331, 10333, 10337, 10343, 10357, 10369, 10391, 10427, 10429, 10433, 10459, 10463, 10477, 10487, 10499, 10501, 10513, 10531, 10559, 10567, 10589, 10597, 10601, 10607, 10613, 10627, 10631, 10639, 10657, 10663, 10667, 10687, 10691, 10709, 10711, 10723, 10729, 10733, 10739, 10753, 10781, 10789, 10799, 10831, 10837, 10847, 10853, 10859, 10861, 10867, 10883, 10889, 10903, 10909, 10937, 10949, 10973, 10979, 10987, 10993, 11003, 11027, 11047, 11059, 11069, 11071, 11083, 11087, 11093, 11113, 11117, 11119, 11149, 11159, 11161, 11171, 11173, 11177, 11197, 11213, 11239, 11251, 11257, 11261, 11273, 11279, 11287, 11299, 11311, 11317, 11321, 11329, 11351, 11353, 11383, 11393, 11399, 11411, 11423, 11437, 11443, 11447, 11467, 11471, 11491, 11503, 11519, 11527, 11549, 11551, 11579, 11587, 11593, 11597, 11617, 11621, 11633, 11657, 11677, 11681, 11717, 11719, 11731, 11743, 11777, 11779, 11783, 11789, 11801, 11807, 11821, 11831, 11833, 11839, 11863, 11867, 11887, 11897, 11903, 11909, 11927, 11933, 11939, 11941, 11953, 11959, 11969, 11971, 11981, 11987, 12007, 12011, 12037, 12041, 12043, 12049, 12071, 12073, 12097, 12101, 12107, 12113, 12143, 12157, 12161, 12163, 12197, 12211, 12227, 12239, 12241, 12251, 12253, 12263, 12269, 12277, 12281, 12289, 12301, 12323, 12329, 12343, 12347, 12373, 12377, 12379, 12391, 12401, 12409, 12413, 12433, 12437, 12451, 12457, 12473, 12479, 12487, 12491, 12497, 12511, 12517, 12527, 12539, 12541, 12547, 12553, 12569, 12577, 12583, 12589, 12601, 12611, 12613, 12619, 12637, 12641, 12647, 12653, 12659, 12689, 12697, 12703, 12713, 12721, 12739, 12757, 12763, 12781, 12791, 12799, 12809, 12821, 12823, 12829, 12841, 12853, 12889, 12893, 12899, 12907, 12911, 12917, 12919, 12923, 12941, 12953, 12959, 12967, 12973, 12979, 12983, 13001, 13003, 13007, 13009, 13033, 13037, 13043, 13049, 13063, 13093, 13099, 13103, 13109, 13121, 13127, 13147, 13151, 13159, 13171, 13177, 13183, 13187, 13217, 13219, 13241, 13249, 13259, 13267, 13297, 13309, 13313, 13327, 13331, 13337, 13339, 13367, 13381, 13397, 13399, 13411, 13417, 13421, 13441, 13451, 13457, 13463, 13469, 13477, 13487, 13499, 13513, 13523, 13537, 13553, 13567, 13577, 13591, 13597, 13613, 13619, 13627, 13633, 13649, 13669, 13679, 13681, 13687, 13691, 13693, 13697, 13709, 13711, 13721, 13723, 13729, 13751, 13757, 13759, 13763, 13781, 13789, 13799, 13807, 13829, 13831, 13841, 13859, 13873, 13877, 13879, 13883, 13901, 13903, 13907, 13913, 13921, 13931, 13933, 13963, 13967, 13997, 13999, 14009, 14011, 14029, 14033, 14051, 14057, 14071, 14081, 14083, 14087, 14107, 14143, 14149, 14153, 14159, 14173, 14177, 14197, 14207, 14221, 14243, 14249, 14251, 14281, 14293, 14303, 14321, 14327, 14341, 14347, 14369, 14387, 14389, 14401, 14407, 14411, 14419, 14423, 14431, 14437, 14447, 14449, 14461, 14479, 14489, 14503, 14519, 14533, 14537, 14543, 14549, 14551, 14557, 14561, 14563, 14591, 14593, 14621, 14627, 14629, 14633, 14639, 14653, 14657, 14669, 14683, 14713, 14717, 14723, 14731, 14737, 14741, 14747, 14753, 14759, 14767, 14771, 14779, 14783, 14797, 14813, 14821, 14827, 14831, 14843, 14851, 14867, 14869, 14879, 14887, 14891, 14897, 14923, 14929, 14939, 14947, 14951, 14957, 14969, 15013, 15031, 15053, 15061, 15073, 15077, 15083, 15091, 15101, 15107, 15121, 15131, 15137, 15139, 15149, 15173, 15187, 15193, 15199, 15217, 15227, 15233, 15241, 15259, 15263, 15269, 15271, 15277, 15287, 15289, 15299, 15307, 15313, 15319, 15329, 15331, 15349, 15359, 15361, 15373, 15377, 15383, 15391, 15401, 15413, 15427, 15439, 15451, 15461, 15467, 15473, 15493, 15497, 15511, 15527, 15541, 15551, 15559, 15569, 15581, 15583, 15601, 15607, 15619, 15629, 15641, 15643, 15647, 15649, 15661, 15667, 15671, 15679, 15683, 15727, 15731, 15733, 15737, 15739, 15749, 15761, 15767, 15773, 15787, 15791, 15797, 15803, 15809, 15817, 15823, 15859, 15877, 15887, 15889, 15901, 15907, 15913, 15919, 15923, 15937, 15959, 15971, 15973, 15991, 16001, 16007, 16033, 16057, 16061, 16063, 16067, 16069, 16073, 16087, 16091, 16097, 16103, 16111, 16127, 16139, 16141, 16183, 16187, 16189, 16193, 16217, 16223, 16229, 16231, 16249, 16253, 16267, 16273, 16301, 16319, 16333, 16339, 16349, 16361, 16363, 16369, 16381, 16411, 16417, 16421, 16427, 16433, 16447, 16451, 16453, 16477, 16481, 16487, 16493, 16519, 16529, 16553, 16561, 16567, 16573, 16603, 16607, 16619, 16631, 16633, 16649, 16651, 16657, 16661, 16673, 16691, 16693, 16699, 16703, 16729, 16741, 16747, 16759, 16763, 16787, 16811, 16823, 16829, 16831, 16843, 16871, 16879, 16883, 16889, 16901, 16903, 16921, 16927, 16931, 16937, 16943, 16963, 16979, 16981, 16987, 16993, 17011, 17021, 17027, 17029, 17033, 17041, 17047, 17053, 17077, 17093, 17099, 17107, 17117, 17137, 17159, 17167, 17183, 17189, 17191, 17203, 17207, 17209, 17231, 17239, 17257, 17291, 17293, 17299, 17317, 17321, 17327, 17333, 17341, 17351, 17359, 17377, 17383, 17387, 17389, 17393, 17401, 17417, 17419, 17431, 17443, 17449, 17467, 17471, 17477, 17483, 17489, 17491, 17497, 17509, 17519, 17539, 17551, 17569, 17573, 17579, 17581, 17597, 17599, 17609, 17623, 17627, 17657, 17659, 17669, 17681, 17683, 17707, 17713, 17729, 17737, 17747, 17749, 17761, 17783, 17791, 17807, 17827, 17837, 17839, 17851, 17863, 17881, 17891, 17903, 17909, 17911, 17921, 17923, 17929, 17939, 17957, 17959, 17971, 17977, 17981, 17989, 18013, 18041, 18043, 18047, 18049, 18059, 18061, 18077, 18089, 18097, 18119, 18121, 18127, 18131, 18133, 18143, 18149, 18169, 18181, 18191, 18211, 18217, 18223, 18229, 18233, 18251, 18253, 18257, 18269, 18287, 18289, 18301, 18307, 18313, 18329, 18341, 18353, 18367, 18371, 18379, 18397, 18401, 18413, 18427, 18433, 18439, 18443, 18451, 18457, 18461, 18493, 18503, 18517, 18521, 18523, 18539, 18541, 18553, 18583, 18587, 18593, 18617, 18637, 18661, 18671, 18679, 18691, 18701, 18713, 18719, 18731, 18743, 18749, 18757, 18773, 18787, 18793, 18797, 18803, 18839, 18859, 18869, 18899, 18911, 18913, 18917, 18919, 18947, 18973, 18979, 19001, 19009, 19013, 19031, 19037, 19051, 19069, 19073, 19079, 19081, 19121, 19139, 19141, 19157, 19163, 19181, 19183, 19207, 19211, 19213, 19219, 19231, 19237, 19249, 19259, 19267, 19273, 19289, 19301, 19309, 19333, 19373, 19379, 19381, 19387, 19391, 19403, 19417, 19421, 19423, 19427, 19429, 19433, 19441, 19447, 19457, 19463, 19469, 19471, 19477, 19483, 19489, 19501, 19507, 19531, 19541, 19543, 19553, 19571, 19577, 19583, 19597, 19603, 19609, 19661, 19681, 19687, 19697, 19699, 19709, 19717, 19727, 19739, 19751, 19753, 19759, 19763, 19777, 19793, 19801, 19813, 19819, 19841, 19843, 19853, 19861, 19867, 19889, 19891, 19919, 19927, 19937, 19949, 19961, 19963, 19973, 19979, 19991, 19993, 19997, 20011, 20021, 20023, 20029, 20047, 20051, 20063, 20071, 20089, 20101, 20107, 20113, 20117, 20123, 20129, 20143, 20147, 20149, 20161, 20173, 20177, 20183, 20201, 20219, 20231, 20233, 20249, 20261, 20269, 20287, 20297, 20323, 20327, 20333, 20341, 20347, 20353, 20357, 20359, 20369, 20389, 20393, 20407, 20411, 20431, 20441, 20443, 20477, 20479, 20483, 20507, 20509, 20521, 20533, 20543, 20549, 20551, 20563, 20593, 20599, 20611, 20627, 20641, 20663, 20681, 20693, 20707, 20717, 20719, 20731, 20743, 20747, 20749, 20753, 20759, 20771, 20773, 20789, 20807, 20809, 20849, 20857, 20873, 20879, 20887, 20897, 20899, 20921, 20929, 20939, 20947, 20959, 20963, 20981, 20983, 21001, 21011, 21013, 21017, 21019, 21023, 21031, 21059, 21061, 21067, 21089, 21101, 21107, 21121, 21139, 21143, 21149, 21157, 21163, 21169, 21179, 21187, 21193, 21211, 21221, 21227, 21247, 21269, 21277, 21283, 21313, 21317, 21319, 21341, 21347, 21377, 21379, 21391, 21397, 21401, 21407, 21419, 21433, 21467, 21481, 21487, 21491, 21493, 21499, 21503, 21517, 21521, 21523, 21529, 21557, 21559, 21563, 21569, 21577, 21587, 21589, 21599, 21601, 21611, 21613, 21617, 21647, 21649, 21673, 21683, 21701, 21713, 21727, 21737, 21739, 21751, 21757, 21767, 21773, 21787, 21799, 21817, 21821, 21839, 21841, 21851, 21859, 21863, 21871, 21881, 21893, 21911, 21929, 21937, 21943, 21961, 21977, 21991, 21997, 22003, 22013, 22027, 22031, 22037, 22039, 22051, 22063, 22067, 22073, 22079, 22091, 22093, 22109, 22111, 22123, 22129, 22133, 22147, 22153, 22157, 22159, 22171, 22189, 22193, 22229, 22247, 22259, 22271, 22273, 22279, 22283, 22291, 22303, 22307, 22343, 22349, 22367, 22369, 22381, 22391, 22397, 22409, 22433, 22441, 22447, 22453, 22469, 22481, 22483, 22501, 22511, 22531, 22541, 22543, 22549, 22571, 22573, 22613, 22619, 22621, 22637, 22639, 22643, 22651, 22669, 22679, 22691, 22697, 22699, 22709, 22717, 22721, 22727, 22739, 22741, 22751, 22769, 22777, 22783, 22787, 22807, 22811, 22817, 22853, 22859, 22861, 22871, 22877, 22901, 22907, 22921, 22937, 22943, 22961, 22963, 22973, 22993, 23003, 23011, 23017, 23021, 23029, 23039, 23041, 23053, 23057, 23059, 23063, 23071, 23081, 23087, 23099, 23117, 23131, 23143, 23159, 23167, 23173, 23189, 23197, 23201, 23203, 23209, 23227, 23251, 23269, 23279, 23291, 23293, 23297, 23311, 23321, 23327, 23333, 23339, 23357, 23369, 23371, 23399, 23417, 23431, 23447, 23459, 23473, 23497, 23509, 23531, 23537, 23539, 23549, 23557, 23561, 23563, 23567, 23581, 23593, 23599, 23603, 23609, 23623, 23627, 23629, 23633, 23663, 23669, 23671, 23677, 23689, 23719, 23741, 23743, 23747, 23753, 23761, 23767, 23773, 23789, 23801, 23813, 23819, 23827, 23831, 23833, 23857, 23869, 23873, 23879, 23887, 23893, 23899, 23909, 23911, 23917, 23929, 23957, 23971, 23977, 23981, 23993, 24001, 24007, 24019, 24023, 24029, 24043, 24049, 24061, 24071, 24077, 24083, 24091, 24097, 24103, 24107, 24109, 24113, 24121, 24133, 24137, 24151, 24169, 24179, 24181, 24197, 24203, 24223, 24229, 24247, 24251, 24281, 24317, 24329, 24337, 24359, 24371, 24373, 24379, 24391, 24407, 24413, 24419, 24421, 24439, 24469, 24473, 24481, 24499, 24509, 24517, 24527, 24533, 24547, 24551, 24571, 24593, 24611, 24623, 24631, 24659, 24671, 24677, 24683, 24691, 24697, 24709, 24733, 24749, 24763, 24781, 24799, 24809, 24821, 24841, 24847, 24851, 24859, 24877, 24889, 24907, 24917, 24919, 24923, 24943, 24953, 24967, 24971, 24977, 24979, 24989]

\end{thebibliography}

\end{document}


% A (minimal) template for problem sets and solutions using the exam document class

% Organization:
%% Define new commands, macros, etc. in macros.tex
%% Anything that you would put before \begin{document} should go in prelude.tex

%% For multiple psets, each should get its own file to \input into main with a \section{}
\input{prelude}
\begin{document}

\title{Real Analysis Homework 12}
\author{Francisco Jose Castillo Carrasco}
\date{\today}
\maketitle

\input{macros}

%% Content goes here

\section{Problem 5.2.4.}
\input{problem1}

\section{Problem 6.1.2.}
\input{problem2}

\section{Problem 6.2.2.}
\input{problem3}

\newpage
\section{Problem 6.3.4.}
\input{problem4}

\section*{Acknowledgements}
\input{Acknowledgements}

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
	\DeclareGraphicsExtensions{.png, .jpeg}
\usepackage{caption}
% \usepackage{subcaption}
\usepackage{csvsimple}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}

\title{PA04: Vector Reduce Revisited\\CS791v: Parallel Computing}
\author{Terence Henriod}
\date{\today}

\begin{document}

\clearpage
\maketitle
\thispagestyle{empty} % removes the page number from the title page

\begin{abstract}
In this assignment, we revisit vector reduction techniques on the GPU, this time attempting an array of techniques instead of just one. The first is vector reduction via kernel calls from the CPU; the second is reduction using a in itial kernel to pre-sum the elements followed by a final reduction kernel launch from within the original kernel; third is using the GPU to initially reduce the vector to an intermediate vector, and then complete the summation on the CPU; the final version is to use the \texttt{threadfence} instruction to have one block perform the final summation of the intermediate vector.
\end{abstract}

\newpage
\section{Introduction}
Vector reduction is a common, highly parallelizable task. It also provides ample flexibility to explore different GPU techniques in a simplified way. Even though reduction is typically summarized as simply ``summing all of the elements of a vector," the addition operation can be substituted to adapt reduction to a variety of tasks. Since elements can be operated on in any order, and only need to be operated only once,

\section{Theory}
Here we discuss the methodologies of the various methods.

\subsection{Reduction Through Multiple Kernel Launches}
The most ``naive" way to reduce a vector. A kernel is used to task blocks with reducing the original vector into an intermediate vector that has as many elements as there were blocks in the kernel launch. In order to complete the reduction, the intermediate vector needs to be reduced once all of the blocks have completed their task. Since inter-block synchronization is difficult and costly to implement on hardware, the programmer can effect the inter-block synchronization by simply making a second kernel call that uses a single block to reduce the intermediate vector to a single element.

\subsection{Reduction Through Kernels That Launch Kernels}

\subsection{Reduction With a Kernel Launch and Final Reduction on the CPU}
While the GPU can provide large speedups through its massive parallelism, GPU kernels have high start-up and tear down costs, not to mention the need for memory transfers. Because of this, it can be beneficial to have smaller tasks be completed on the CPU. This way the CPU's quickness can be applied without large startup costs or memory transfers. In vector reduction, this takes the form of having the CPU perform the final reduce on the intermediate vector.

\subsection{Reduction With a Single Kernel and Inter-Block Synchronization}
As mentioned previously, inter-block synchronization is costly to implement, and so it is not done. Mostly. There is a special thread synchronization function known as \texttt{threadfence}. This instruction allows GPU code to make guarantees about the order of some operations. Using the \texttt{threadfence}, a vector can be reduced to an intermediate vector, then the fence can synchronize operations, and then the intermediate vector can be passed to a single block for final reduction.

\newpage
\section{Results}
The performance results of the various vector reduction methods are displayed here.

\subsection{Information on the GPU device used}
\csvautotabular{gpu_properties.csv}

\newpage
\subsection{Performance Graphs}
  % \begin{figure}[h!]
  %   \centering
  %   \includegraphics[width=.9\linewidth]{runtime}
  %   \caption{A comparison of the runtimes for computing a 2000x2000 Mandelbrot image using 1024 iterations at most for each pixel.}
  %   \label{fig:runtime}
  % \end{figure}

  % \begin{figure}[h!]
  %   \centering
  %   \includegraphics[width=.9\linewidth]{throughput}
  %   \caption{A comparison of the throughput in integer pixel values produced per second.}
  %   \label{fig:throughput}
  % \end{figure}

  % \begin{figure}[h!]
  %   \centering
  %   \includegraphics[width=.9\linewidth]{compute_speedup}
  %   \caption{The speedup achieved. Note the peak in the surface at 64 threads for all block sizes.}
  %   \label{fig:compute_speedup}
  % \end{figure}

  % \begin{figure}[h!]
  %   \centering
  %   \includegraphics[width=.9\linewidth]{total_speedup}
  %   \caption{The speedup achieved when also considering the time to copy the computed data from the GPU to the Host.}
  %   \label{fig:total_speedup}
  % \end{figure}

  % \begin{figure}[h!]
  %   \centering
  %   \includegraphics[width=.7\linewidth]{testimage}
  %   \caption{A Mandelbrot image for verification.}
  %   \label{fig:total_speedup}
  % \end{figure}

\newpage
\section{Discussion}

\section{Issues}

\end{document}


\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{listings}
\usepackage{algorithmic}
\usepackage[ruled]{algorithm2e} % For algorithms
\usepackage[colorinlistoftodos]{todonotes}
\usepackage[margin=1in]{geometry}
\usepackage{mathtools}
\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}

\title{High Performance Computing and Big Data - OpenMP and MPI}

\author{Federico Tavella, Student number 11343605}

\date{\today}

\begin{document}
\maketitle

\section{Experimenting with OpenMP}

One of the main advantages of OpenMP is that it is not strictly necessary to change the source code to parallelize the program. In fact, we can improve the performances adding pragrams. Taking a look at the \texttt{multimult} function in Section~\ref{subsec:ausiliary}, we can see that the logic of the program is the same of the \texttt{multimult\_seq} function, which is the sequential version. We used the following macros to make the inner \texttt{for} loop parallel: \texttt{pragma omp parallel}, \texttt{private}, \texttt{shared} and \texttt{schedule}. By using \texttt{pragma omp parallel}, we are saying that different iterations of the loop can be executed in different threads. The \texttt{private} and \texttt{shared} macros define which are the variables shared among the different threads and which ones are not. In our case, the only private variable is \texttt{c}, which is used to temporarily store the value of the vector product between \texttt{a} and \texttt{b}. Finally, with the \texttt{schedule} macro we explicitly say in which order the different threads have to be executed; \texttt{static} means that the order is decided a priori of the execution, while the second parameter for the \texttt{schedule} macro indicates the chunk size. Through different runs, we found out that the best results are obtained with $size_{chunk} = \dfrac{size_{array}}{n_{threads}}$.

One further improvement might be parallelizing the external \texttt{for} loop, however this is not feasible without modifying the code because for each different iteration of this loop, the value of \texttt{a[i]} depends on the previous iterations. However, by switching the inner loop with the outermost one, we could make both of them run in parallel.

\subsection{Auxiliary functions}\label{subsec:ausiliary}

\begin{verbatim}
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "omp.h"

/*
  Function used to extract the number of seconds spent
*/

time_t extract_time_secs(struct timespec start, struct timespec stop){
  if(stop.tv_sec - start.tv_sec > 0 && stop.tv_nsec - start.tv_nsec < 0){
    return (stop.tv_sec - start.tv_sec - 1);
  }
  else{
    return (stop.tv_sec - start.tv_sec);
  }
}

/*
  Function used to extract the number of nanoseconds spent
*/

time_t extract_time_nsecs(struct timespec start, struct timespec stop){
  if(stop.tv_sec - start.tv_sec > 0 && stop.tv_nsec - start.tv_nsec < 0){
    return (1000000000 - (start.tv_nsec - stop.tv_nsec));
  }
  else{
    return (stop.tv_nsec - start.tv_nsec);
  }
}

/*
  Function used to start/stop the timer passed with clock_ref
*/

void get_time(struct timespec* clock_ref){
  if( clock_gettime(CLOCK_REALTIME, clock_ref) == -1 ) {
      perror( "clock gettime" );
      exit(-1);
  }
}

/*
  Function used to initialise the vectors
*/

void vectors_init(double* a, double* a_seq, double* b, int len){

  int dummy_value = 2;

  for(int i = 0; i < len; i++){
    a[i] = dummy_value;
    b[i] = dummy_value;
    a_seq[i] = a[i];
  }
}

/*
  Recursive function to calculate base^(expo)
*/

int powr(int base, int expo){
  if(expo == 0){
    return 1;
  }
  return base*powr(base, expo-1);
}

/*
  Sequential version of the multimult function
*/

void multimult_seq(double *a, double *b, int len, int steps){
  double c;
  for (int t=0; t < steps; t++){
    for (int i=0; i < len; i++){
        c = a [i] * b [i];
        a[i] = c * (double) t;
    }
  }
}

/*
  Parallel version of the multimult function
*/

void multimult(double *a, double *b, int len, int steps, int n_threads){
  double c;

  for (int t=0; t < steps; t++){
      #pragma omp parallel for private(c) shared(a,b,len,steps,t) \
      schedule(static, len/n_threads)
      for (int i=0; i < len; i++){
          c = a [i] * b [i];
          a[i] = c * (double) t;
      }
  }
}
\end{verbatim}

\subsection{Main function}

\begin{verbatim}
int main(){

  int step_iters = 10;    // number of iterations for the step variable
  int delta_steps = 10;   // delta at each iterations for the step variable
  int len_iters = 10;     // number of iterations for the length variable
  int delta_len = 50000;  // delta at each iterations for the length variable
  int thread_iters = 5;   // number of iterations for the threads variable

  // variable used to store the time of each iteration
  time_t results_seq_s[len_iters][step_iters];
  time_t results_seq_ns[len_iters][step_iters];
  time_t results_s[len_iters][step_iters][thread_iters];
  time_t results_ns[len_iters][step_iters][thread_iters];

    for(int k = 0; k < len_iters; k++){

      int len = delta_len*(k+1);
      double a[len], b[len], a_seq[len];

      for(int m = 0; m < step_iters; m++){

        int steps = delta_steps*(m+1);
        vectors_init(a,a_seq,b,len);
        // timers for the sequential/parallel programs
        struct timespec start_seq, stop_seq, start, stop;

        get_time(&start_seq);
        multimult_seq(a_seq,b,len,steps);
        get_time(&stop_seq);

        results_seq_s[k][m] = extract_time_secs(start_seq,stop_seq);
        results_seq_ns[k][m] = extract_time_nsecs(start_seq,stop_seq);

        for(int n = 0; n < thread_iters; n++){

          int n_threads = powr(2,n+1);
          omp_set_num_threads(n_threads);

          get_time(&start);
          multimult(a,b,len,steps,n_threads);
          get_time(&stop);

          results_s[k][m][n] = extract_time_secs(start,stop);
          results_ns[k][m][n] = extract_time_nsecs(start,stop);
        }
    }
  }

  /* storing the results into two different csv files */
  FILE *fp;     // file for the sequential results
  FILE *fp_par; // file for the parallel results

  fp=fopen("res_seq.csv", "w");
  if(fp == NULL)
      exit(-1);
  fprintf(fp, "length,steps,seconds,nanoseconds\n");

  fp_par=fopen("res.csv", "w");
  if(fp_par == NULL)
      exit(-1);
  fprintf(fp_par, "length,steps,threads,seconds,nanoseconds\n");

  for(int k = 0; k < len_iters; k++){
    for(int m=0; m < step_iters; m++){
      fprintf(fp,"%d,%d,%ld,%ld\n",delta_len*(k+1),delta_steps*(m+1),
              results_seq_s[k][m],results_seq_ns[k][m]);
      for(int n=0; n < thread_iters; n++){
        fprintf(fp_par,"%d,%d,%d,%ld,%ld\n",delta_len*(k+1),delta_steps*(m+1),
                powr(2,n+1),results_s[k][m][n],results_ns[k][m][n]);
      }
    }

  }

  fclose(fp)
  fclose(fp_par);

  return 0;
}
\end{verbatim}

\newpage

\subsection{Results}

%Specify characteristics of the processor
In this section, we describe the results obtained from different simulation runs on both laptop and Cartesius supercomputer. The computer on which the tests are executed is equipped with \texttt{Intel Core i7-3630QM CPU @ 2.40GHz x 8} processor, which means that it can run up to 8 threads simultaneously (4 physical processors and 8 virtual ones). In the following graphs, we use a logarithmic scale for the execution time in order to improve the readability.

In Figure~\ref{fig:seq}, we can see how the number of elements in the array and the number of steps affect the execution time. There is a clear positive correlation between these two factor and the time required from the program to terminate. Moreover, we can see how the results on the local machine are more chaotic (i.e., with a less linear behaviour) than the ones obtained from Cartesius. We suppose this is due to interference of other processes during the simulation on the normal computer.

Furthermore, there is no significant difference between using the array length or the number of steps as abscissa. Consequently, from now on, we will always consider as x-axis the number of steps. In this way, we should get an indication of how much the parallelization is improving the performance by looking at the distance between the different lines - i.e., how much the time is increasing with different vectors length.

\begin{figure}[htbp]
\centering
\begin{subfigure}{.45\textwidth}
  \centering
  \includegraphics[width=\linewidth]{res/seq/array_length_res_seq.png}
  \caption{Run on local machine}
  \label{subfig:length_seq}
\end{subfigure}%
\begin{subfigure}{.45\textwidth}
  \centering
  \includegraphics[width=\linewidth]{res/seq/array_length_res_seq_cartesius.png}
  \caption{Run on Cartesius}
  \label{subfig:length_seq_cart}
\end{subfigure}
\begin{subfigure}{.45\textwidth}
  \centering
  \includegraphics[width=\linewidth]{res/seq/array_steps_res_seq.png}
  \caption{Run on local machine}
  \label{subfig:steps_seq}
\end{subfigure}%
\begin{subfigure}{.45\textwidth}
  \centering
  \includegraphics[width=\linewidth]{res/seq/array_steps_res_seq_cartesius.png}
  \caption{Run on Cartesius}
  \label{subfig:steps_seq_cart}
\end{subfigure}
\caption{Correlation between array length, number of steps and execution time - sequential version}
\label{fig:seq}
\end{figure}

Secondly, we want to verify that there is an improvement in the performance of the program when we increase the number of threads. In general, we can assert that the number of threads decreases the execution time of our program. However, this enhancement has an upper bound dictated by the number of cores of the machine. For example, we can see that on a machine with 8 cores, it is better to use 8 threads (Figure~\ref{subfig:steps_thread2_parall}) instead of 16 (Figure~\ref{subfig:steps_thread16_parall}).

We can see how - in Figure~\ref{subfig:steps_thread2_parall_cart}, \ref{subfig:steps_thread4_parall_cart}, \ref{subfig:steps_thread8_parall_cart} and \ref{subfig:steps_thread16_parall_cart} - the lines indicating the execution time are getting lower and closer, indicating respectively a reduction of the time required and the improvement provided from the parallelization to bigger arrays. Moreover, as for the sequential version, even in this case the behaviour on the local machine is not as much linear as on the supercomputer.

\begin{figure}[htbp]
\centering
\begin{subfigure}{.42\textwidth}
  \centering
  \includegraphics[width=\linewidth]{res/parallel/array_thread_2_steps_res.png}
  \caption{Local machine - 2 threads}
  \label{subfig:steps_thread2_parall}
\end{subfigure}%
\begin{subfigure}{.42\textwidth}
  \centering
  \includegraphics[width=\linewidth]{res/parallel/array_thread_2_steps_res_cartesius.png}
  \caption{Cartesius - 2 threads}
  \label{subfig:steps_thread2_parall_cart}
\end{subfigure}
\begin{subfigure}{.42\textwidth}
  \centering
  \includegraphics[width=\linewidth]{res/parallel/array_thread_4_steps_res.png}
  \caption{Local machine - 4 threads}
  \label{subfig:steps_thread4_parall}
\end{subfigure}%
\begin{subfigure}{.42\textwidth}
  \centering
  \includegraphics[width=\linewidth]{res/parallel/array_thread_4_steps_res_cartesius.png}
  \caption{Cartesius - 4 threads}
  \label{subfig:steps_thread4_parall_cart}
\end{subfigure}
\begin{subfigure}{.42\textwidth}
  \centering
  \includegraphics[width=\linewidth]{res/parallel/array_thread_8_steps_res.png}
  \caption{Local machine - 8 threads}
  \label{subfig:steps_thread8_parall}
\end{subfigure}%
\begin{subfigure}{.42\textwidth}
  \centering
  \includegraphics[width=\linewidth]{res/parallel/array_thread_8_steps_res_cartesius.png}
  \caption{Cartesius - 8 threads}
  \label{subfig:steps_thread8_parall_cart}
\end{subfigure}
\begin{subfigure}{.42\textwidth}
  \centering
  \includegraphics[width=\linewidth]{res/parallel/array_thread_16_steps_res.png}
  \caption{Local machine - 16 threads}
  \label{subfig:steps_thread16_parall}
\end{subfigure}%
\begin{subfigure}{.42\textwidth}
  \centering
  \includegraphics[width=\linewidth]{res/parallel/array_thread_16_steps_res_cartesius.png}
  \caption{Cartesius - 32 threads}
  \label{subfig:steps_thread16_parall_cart}
\end{subfigure}
\caption{Correlation between number of threads and execution time - parallel version}
\label{fig:steps_parall}
\end{figure}

Finally, in order to verify the upper bound previously defined, we execute one more simulation using 32 threads, knowing that the machine in Cartesius on which is executed has 16 cores. As a result, comparing Figure~\ref{subfig:steps_thread16_parall_cart} and Figure~\ref{fig:steps_thread32_parall_cart}, it is evident that the execution time increases due to the overhead created by generating a number of threads greater than the number of cores.

\begin{figure}[htbp]
\centering
\includegraphics[width=0.7\textwidth]{res/parallel/array_thread_32_steps_res_cartesius.png}
\caption{Execution of the program with 32 threads on the Cartesius supercomputer}
\label{fig:steps_thread32_parall_cart}
\end{figure}
\newpage

\section{Collective communication}

In order to implement the collective communication, we can use the \texttt{MPI\_Send} and \texttt{MPI\_Recv} methods, First of all, we need to retrieve the communication \texttt{rank} and \texttt{size} (i.e., current process id and number of process respectively). To do so, we use the \texttt{MPI\_Comm\_rank} and \texttt{MPI\_Comm\_size} methods. Finally, if the current process is the root one, we broadcast the message to all the other process with the \texttt{MPI\_Send} method; otherwise, the current process is one of the receivers, so we start to listen awaiting for a message from the root process. The \texttt{tag} variable is used to filter messages from different process.

\begin{verbatim}
int MPI_Broadcast(  void *buffer,           /* INOUT : buffer address            */
                    int count,              /* IN    : buffer size               */
                    MPI_Datatype datatype,  /* IN    : datatype of entry address */
                    int root,               /* IN    : root process (sender)     */
                    MPI_Comm communicator)  /* IN    : communicator              */
{
  int rank, size, tag;
  MPI_Status status;
  MPI_Comm_rank(communicator, rank);
  MPI_Comm_size(communicator, size);

  //initialise tag variable

  if (rank == root) { // I am the root process
    for (int i = 0; i < size; i++) {
      if (i != rank) { //Avoid to send the message to myself
        MPI_Send(buffer, count, datatype, i, tag, communicator);
      }
    }
  }
  else { // I am a receiver process
    MPI_Recv(buffer, count, datatype, root, tag, communicator, &status);
    /* do something with the data */
  }
}

\end{verbatim}

\newpage

\section{Collective communication with topology adaptation}

Topology adaptation improves the performance of the broadcast communication, delegating the re-trasmission of the signal to other nodes. In the case of 1-dimensional ring topology, each node communicates with only two different nodes, one that has a greater MPI rank and one that has a smaller MPI rank than the node itself. Consequently, the root node send the message to its two neighboors, which propagates the message throught the ring without sending it back. In the end, there might be one or two nodes that are the last ones to receive the message, depending on the number of node of the network - i.e., if the number is even, there is one final node; otherwise, there are two.

If we assume $N$ is the number of nodes in the ring, there will be $N-1$ communication events (while without topology adaptation we would have $N$ events). However, since the broadcasting operation is distributed amoung the nodes, the time required to complete the broadcast is much smaller. If we consider as $k$ the time for a single \texttt{MPI\_Send} operation, without topology adaptation we need $kN$ time units to complete the broadcasting, but with this new feature the time is reduced to $k \cdot \floor*{\frac{N}{2}}$.

\begin{verbatim}
int MPI_Circular_Broadcast(  void *buffer,          /* INOUT : buffer address            */
                             int count,             /* IN    : buffer size               */
                             MPI_Datatype datatype, /* IN    : datatype of entry address */
                             int root,              /* IN    : root process (sender)     */
                             MPI_Comm communicator) /* IN    : communicator              */
{
  int rank, size, tag;
  MPI_Status status;
  MPI_Comm_rank(communicator, rank);
  MPI_Comm_size(communicator, size);

  //initialise tag variable

  int neighboor_lh, neighboor_rh;
  neighboor_rh = (rank + 1) % size;
  neighboor_lh = (rank - 1) % size;
  if (rank == root) { //Root process sends to neighboors
    MPI_Send(buffer, count, datatype, neighboor_lh, tag, communicator);
    MPI_Send(buffer, count, datatype, neighboor_rh, tag, communicator);
  }
  else if (rank < size/2){ // Circularly increasing MPI ranks
    MPI_Recv(buffer, count, datatype, neighboor_lh, tag, communicator, &status);
    // Send message to the neighboor that has bigger MPI rank
    MPI_Send(buffer, count, datatype, neighboor_rh, tag, communicator);
  }
  else if (rank > size/2){  // Circularly decreasing MPI ranks
    MPI_Recv(buffer, count, datatype, neighboor_rh, tag, communicator, &status);
    // Send message to the neighboor that has smaller MPI rank
    MPI_Send(buffer, count, datatype, neighboor_lh, tag, communicator);
  }
  else{ // Last nodes in the ring to reach

    /*
      By default, last node receive the message from the neighboor on its right
      in both cases size is even (one more node to reach) or odd (two nodes
      remaining)
    */

    MPI_Recv(buffer, count, datatype, neighboor_rh, tag, communicator, &status);
  }

  return 0;
}
\end{verbatim}

\end{document}


\documentclass[11pt]{article}
\usepackage{uvatoc} % replace this line with the one below for your submission
%\usepackage[response]{uvatoc}

\begin{document}

\makeheader

\makemytitle{Problem Set 4: The Countess of Functions}

\submitter{TODO: replace this with your name (and computing id)}
\due{4:19pm Friday, 18 October 2019}

\directions{
The purpose of this assignment is to continue to develop your understanding of the asymptotic operators, and to understand deeply the results in \href{https://uvatoc.github.io/docs/tcs-chapter5.pdf}{Chapter 5} of the textbook.

\collaboration{You should do this assignment by yourself and submit your own answers. You may discuss the problems with anyone you want and it is also fine to get help from anyone on problems with LaTeX or Jupyter/Python. You are permitted to use any resources you find for this assignment. You should note in the {\em Collaborators and Resources} box below the people you collaborated with and any external resources you used (you may exclude resources you used exclusively for help with LaTeX or Jupyter/Python.}
}

\collaborators{TODO: replace this with your collaborators and resources (if you did not have any, replace this with {\em None})}

\directions{
This problem set includes both PDF and Jupyter notebook components. You should complete the answers to the PDF part by writing your answers in \texttt{ps4.tex}, and submitting your generated PDF file in collab.

As with previous problem sets, the first thing you should do in \keyword{ps4.tex} is set up your name as the author of the submission by replacing the line, \texttt{\textbackslash submitter\{TODO: your name\}}, with your name and UVA id, e.g., \texttt{\textbackslash submitter\{Margaret Hamilton (mhh01z)\}}. Before submitting your PDF, also remember to (1) list your collaborators and resources, replacing the TODO in {\texttt{\textbackslash collaborators\{TODO: replace ...\}}}, and (2) replace the second line in \keyword{ps4.tex}, \texttt{\textbackslash usepackage\{uvatoc\}} with \texttt{\textbackslash usepackage[response]\{uvatoc\}} so the directions do not appear in your final PDF.
}

\subsection*{Asymptotic Operators}

For all of these questions (and throughout cs3102), you should use the formal definitions of $O$, $\Theta$, and $\Omega$ from Class 9.

\begin{problem}
\rm
For each sub-problem, indicate if the statement is \emph{true} or \emph{false} and support your answer with a convincing argument.
\end{problem}

\begin{enumerate}[(a)]
    \item $17n \in O(723n + \log n)$

    \item $\min(n^n, 3012) \in O(1)$

    \item $n^2 \in \Theta(n^3)$

    \item $2.0001^n \in O(2^n)$

    \item $log_n 10 \in \Theta(log_{2n} 17)$
\end{enumerate}

\directions{\clearpage}

\begin{problem}
\rm
Are there any two functions, $f, g: \mathbb{N} \rightarrow \mathbb{R}$ such that $f \notin \Omega(g)$ and $g \notin \Omega(f)$?
\end{problem}

\directions{
A good answer will either show a specific counter-example, or prove that no such two functions exist. A solid argument will need to use the formal definition of $\Omega$, instead of relying on intuition (which is often counter-intuitive).
}

\subsection*{Soft-$O$ and Little-$o$}

\directions{
Logarithms grow so slowly, they are practically ``constants'' --- $\log_2 \mathrm{1 Trillion} < 30$. So, for any size problem we could compute on a real machine, theoreticians (and students who don't like to worry about manipulating logarithms) shouldn't waste their time worrying about logarithmic factors. Indeed, even polynomials on logarithms (i.e., $a_k (\log n)^k$ for any constant $k$) grow so slowly to usually be irrelevant. For this reason, we often use the ``Soft-$O$'' notation, $\widetilde{O}$:

\begin{definition}[$\widetilde{O}$]
A function $f(n): \N \rightarrow \mathbb{R}$ is in $\widetilde{O}(g(n))$ for any function $g(n): \N \rightarrow \mathbb{R}$ if and only if $f(n) \in O(g(n) \cdot \log^k g(n))$ for some $k \in \N$.
\end{definition}

(Note: for convenience, we write $\log^k x$ to mean $(\log x)^k$. Also, we have seen the (constant) base of a $\log$ doesn't matter within our asymptotic operators, but if it is disturbing to have a $\log$ with uncertain base, it is fine to assume it is base 2.)
}

\begin{problem}
\rm For each sub-problem, indicate if the statement is \emph{true} or \emph{false} and support your answer with a convincing argument. \end{problem}

\begin{enumerate}[(a)]
    \item $n^2\log n^3 \in \widetilde{O}(n^2)$
    \item $2.0001^n \in \widetilde{O}(2^n)$
    \item maximum number of comparison operations needed to sort a list of $n$ items $\in \widetilde{O}(n)$
\end{enumerate}

(Hint: by understanding the definition of $\widetilde{O}$ above, you should realize that one way to prove a function is in a $\widetilde{O}$ set is to choose a value for $k$ used in the definition, but to disprove inclusion in $\widetilde{O}$ you need to show that there is no $k$ that works.)

\directions{
Another useful notation is ``little-$o$'' which is designed to capture the notion that a function $g$ grows much faster than $f$:

\begin{definition}[$o$]
A function $f(n): \N \rightarrow \mathbb{R}$ is in $o(g(n))$ for any function $g(n): \N \rightarrow \mathbb{R}$ if and only if for every positive constant $c$, there exists an $n_0 \in \N$ such that:
$$ \forall n > n_0 . f(n) \le cg(n).$$
\end{definition}
}

\begin{problem}
Goldilocks and the Three $O$s
\end{problem}

\begin{enumerate}[(a)]
    \item Prove that for any function $f$, $f \notin o(f)$.
    \item Prove that $n \in o(n \log n)$.
    \item ($\star$) Prove that for any $k \in \N$, $\log^k n \in o(n^\epsilon)$ for any $\epsilon > 0$.
    \item ($\star$) Prove that for any two functions $f, g$ and constant $\alpha>0$, $f \in \widetilde{O}(g) \implies g^{1+\alpha} \in \Omega(f)$.
\end{enumerate}

\subsection*{Counting Circuits and Functions}

\begin{problem}
Equal to Constant Function {\rm (TCS Exercise 5.3)}
\end{problem}

\directions{
For every $k \in \N$ and $x' \in \{0,1\}^k$, show that there is an $O(k)$ line \emph{NAND-CIRC} program that computes the function $EQUALS_{x'}: \{0,1\}^k \rightarrow \{0,1\}$ that on input $x\in \{0,1\}^k$ outputs $1$ if and only if $x=x'$.
}

\begin{problem}
Counting lower bound for multibit functions {\rm (TCS Exercise 5.4)}
\end{problem}
\directions{
 Prove that there exists a number $\delta>0$ such that for every $n,m$ there exists a function $f:\{0,1\}^n \rightarrow \{0,1\}^m$ that requires at least $\delta m \cdot 2^n / n$ NAND gates to compute.  (If you are stuck, see this exercise in the book for a hint.)
}

\begin{problem}
Random Functions are Hard  {\rm (TCS Exercise 5.8)}
\end{problem}

\directions{

Suppose $n>1000$ and that we choose a function $F:\{0,1\}^n \rightarrow \{0,1\}$ at random, choosing for every $x\in \{0,1\}^n$ the value $F(x)$ to be the result of tossing an independent unbiased coin. Prove that the probability that there is a $2^n/(1000n)$ line program that computes $F$ is at most $2^{-100}$.   (If you are stuck, see this exercise in the book for a hint.)
}

\begin{center}
{\bf End of Problem Set 4 (PDF part)} \\
Don't forget to also complete and submit the Jupyter notebook!
\end{center}

\end{document}


\documentclass[jou]{apa6}

\usepackage[american]{babel}

\usepackage{csquotes}
\usepackage[style=apa,sortcites=true,sorting=nyt,backend=biber]{biblatex}
\DeclareLanguageMapping{american}{american-apa}
\addbibresource{bibliography.bib}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Discrete Structures
%% The start of RBS stuff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Working internal and external links in PDF
\usepackage{hyperref}
% Extra math symbols in LaTeX
\usepackage{amsmath}
\usepackage{gensymb}
\usepackage{amssymb}
% Enumerations with (a), (b), etc.
\usepackage{enumerate}

\let\OLDitemize\itemize
\renewcommand\itemize{\OLDitemize\addtolength{\itemsep}{-6pt}}

\usepackage{etoolbox}
\makeatletter
\preto{\@verbatim}{\topsep=3pt \partopsep=3pt }
\makeatother

% These sizes redefine APA for A4 paper size
\oddsidemargin 0.0in
\evensidemargin 0.0in
\textwidth 6.27in
\headheight 1.0in
\topmargin -24pt
\headheight 12pt
\headsep 12pt
\textheight 9.19in

\title{Sample Quiz 8}
\author{Discrete Structures, Spring 2020}
\affiliation{RBS}

\leftheader{Discrete Sample Quiz 8}

\abstract{%
}

%\keywords{}

\setlength\parindent{0pt}

\begin{document}

%\thispagestyle{empty}

\twocolumn
\section{Worksheet 8: Probabilities}

\vspace{10pt}
{\bf Question 1}
What is the probability that the sum of the numbers on two dice is even when they are rolled?\\
(Express your answer as $P/Q$.)

\vspace{10pt}
{\bf Question 2}\\
{\bf (A)} What is the probability to get sequence ``{\tt Heads}, {\tt Heads}, {\tt Heads},
{\tt Heads}, {\tt Heads}, {\tt Heads}'' when
tossing a coin?\\
{\bf (B)} What is the probability to get sequence ``{\tt Heads, Tails, Heads, Tails, Heads, Tails, Heads, Tails}'' when
tossing a coin?\\
{\bf (C)} What is the probability to get even number of "Heads" when tossing a coin 6 times?

\vspace{10pt}
{\bf Question 3}
Assume that there is the following gambling game involving dice (cube with six outcomes: $1,2,3,4,5,6$ with
equal probabilities). Player $A$ guesses a number $n$ between $1$ and $6$ (for example $n = 3$). Then he rolls a dice three times.
We have the following four outcomes (events):\\
$E_1$: If one of the dice rolls equals Player's $A$ number $n$, then Player $A$ wins 1 euro.\\
$E_2$: If two of the dice rolls equal Player's $A$ number $n$, then Player $A$ wins 2 euros.\\
$E_3$: If all three dice rolls equal Player's $A$ number $n$, then Player $A$ wins 3 euros.\\
$E_4$: If none of the dice rolls equals $n$, then Player $A$ loses 1 euro.\\
{\bf (A)} Find the probability for each event $E_1,E_2,E_3,E_4$.\\
{\bf (B)} Find the expected value of the money that Player $A$ wins or loses during a single round of this game. (One round = three
dice rolls as described above). Use this formula \textendash{} a weighted sum of the euros multiplied by the respective probabilities:
$$p(E_1)\cdot 1 + p(E_2)\cdot 2 + p(E_3)\cdot 3 + p(E_4)\cdot (-1).$$

\vspace{10pt}
{\bf Question 4} Find a probability that choosing a random month during a leap year, it would have exactly $5$ Sundays.

\vspace{10pt}
{\bf Question 5} Suppose you and a friend each choose at random an integer between $1$ and $8$, inclusive. For example, some
possibilities are $(3,7)$, $(7,3)$, $(4,4)$, $(8,1)$, where your number is written first and your friend’s number second.
Find the following probabilities:\\
{\bf (A)} p(you pick 5 and your friend picks 8).\\
{\bf (B)} p(sum of the two numbers picked is < 4).\\
{\bf (C)} p(both numbers match).\\
{\bf (D)} p(the sum of the two numbers is a prime).\\
{\bf (E)} p(your number is greater than your friend’s number).

\vspace{10pt}
{\bf Question 6} In a card-game "Zol\={\i}te" there are $26$ cards: four Aces (worth 11 points each);
four Kings (worth 4 points each); four Queens (worth 3 points each); four Jacks (worth 2 points each);
four cards "10" (worth 10 points each); and also four "9", one "8" and one "7" (they are worth 0 points).\\
A single trick ({\em sti\c{k}is}) in this game consists of any three cards (you can assume that all the
$C_{26}^3$ tricks have equal probabilities.)\\
{\bf (A)} Let $X$ be the random variable that expresses the number of points of a single card out
of the 26 cards.  Find the expected value $E(X)$.\\
{\bf (B)} Find the variance $V(X)$.\\
{\bf (C)} Let $Y$ be the random variable that expresses the total number of points for all three
cards $c_1,c_2,c_3$ in a random trick. Find the expected value $E(Y)$.\\
{\bf (D)} Is $E(Y)$ exactly three times larger than $E(X)$?

\vspace{10pt}
{\bf Question 7} You create a random bit string of length five (all $32$ bit strings are equally probable). Consider
these events:\\
$E_1$: the bit string chosen begins with $1$;\\
$E_2$: the bit string chosen ends with $1$;\\
$E_3$: the bit string chosen has exactly three $1$’s.\\
{\bf (A)} Find $p(E_1 \,\mid\, E_3)$.\\
{\bf (B)} Find $p(E_3 \,\mid\, E_2)$.\\
{\bf (C)} Find $p(E_2 \,\mid\, E_3)$.

\vspace{10pt}
{\bf Question 8} A tiny ant travels in 3D space. It starts in the point $(0;0;0)$ and
makes 9 steps altogether: With equal probability
each step can be parallel to either $x$, $y$ or $z$ axes (incrementing the corresponding coordinate).\\
{\bf (A)} What is the probability that the
ant has taken at least one step in the direction of each coordinate axis?\\
{\bf (B)} What is the probability that the ant has reached the point $(3;3;3)$?

\vspace{10pt}
{\bf Question 9} There were 5 people who left their hats in a lobby as they entered some building.
After that there was an electricity blackout; in the total darkness everybody exited the building and grabbed a random hat.
All $5!=120$ permutations of hats are considered equally likely. Find the probability that nobody got his/her
own hat.\\
You may want to define five events: $E_1$ means that the 1st person
got his/her hat; $E_2$ \textendash{} the 2nd person got his/her hat, etc.
Then you can apply the inclusion/exclusion principle:
\begin{align}
 & p(E_1 \cup E_2 \cup E_3 \cup E_3 \cup E_4 \cup E_5) = \nonumber \\
= & p(E_1) + p(E_2) + p(E_3) + p(E_4) + p(E_5) - \nonumber \\
- & p(E_1 \cap E_2) - p(E_1 \cap E_3) - \ldots - p(E_4 \cap E_5) + \nonumber \\
+ & p(E_1 \cap E_2 \cap E_3) + \ldots + p(E_3 \cap E_4 \cap E_5) - \nonumber \\
- & p(E_1 \cap E_2 \cap E_3 \cap E_4) - \ldots - p(E_2 \cap E_3 \cap E_4 \cap E_5) + \nonumber \\
+ & p(E_1 \cap E_2 \cap E_3 \cap E_4 \cap E_5). \nonumber
\end{align}

\newpage
\subsection{Answers}

\vspace{10pt}
{\bf Question 1} Answer: {\tt 1/2}\\
The sum of two dice rolls is even in $18$ cases out of $36$ (can build a table
$6 \times 6$ and paint it as a checkerboard: exactly $18$ cells will be black,
the remaining ones will be white). Since we assume that
all $36$ combinations have equal probabilities, we get $18/36 = 1/2$.

\vspace{10pt}
{\bf Question 2} Answer: TBD\\

\vspace{10pt}
{\bf Question 3} Answer: TBD\\

\vspace{10pt}
{\bf Question 4} Answer: TBD\\

\vspace{10pt}
{\bf Question 5} Answer: TBD\\

\vspace{10pt}
{\bf Question 6} Answer: TBD\\

\vspace{10pt}
{\bf Question 7} Answer: TBD\\

\vspace{10pt}
{\bf Question 8} Answer: TBD\\

\vspace{10pt}
{\bf Question 9} Answer: TBD\\

\end{document}


\documentclass[a4paper, article, oneside, UKenglish]{memoir}

%% Encoding
\usepackage[utf8]{inputenx} % Source code
\usepackage[T1]{fontenc}    % PDF

%% Fonts and typography
\usepackage{lmodern}           % Latin Modern Roman
\usepackage[scaled]{beramono}  % Bera Mono (Bitstream Vera Sans Mono)
\renewcommand{\sfdefault}{phv} % Helvetica
\usepackage[final]{microtype}  % Improved typography
\pretitle{\begin{center}\LARGE\sffamily\bfseries}                    % Title
\renewcommand{\abstractnamefont}{\sffamily\bfseries}                 % Abstract
\renewcommand*{\chaptitlefont}{\Large\bfseries\sffamily\raggedright} % Chapter
\setsecheadstyle{\large\bfseries\sffamily\raggedright}               % Section
\setsubsecheadstyle{\large\bfseries\sffamily\raggedright}            % Subsection
\setsubsubsecheadstyle{\normalsize\bfseries\sffamily\raggedright}    % Subsubsection
\setparaheadstyle{\normalsize\bfseries\sffamily\raggedright}         % Paragraph
\setsubparaheadstyle{\normalsize\bfseries\sffamily\raggedright}      % Subparagraph

%% Mathematics
\usepackage{amssymb}   % Extra symbols
\usepackage{amsthm}    % Theorem-like environments
\usepackage{thmtools}  % Theorem-like environments
\usepackage{mathtools} % Fonts and environments for mathematical formuale
\usepackage{mathrsfs}  % Script font with \mathscr{}

%% Miscellanous
\usepackage{graphicx}  % Tool for images
\usepackage{babel}     % Automatic translations
\usepackage{csquotes}  % Quotes
\usepackage{textcomp}  % Extra symbols
\usepackage{listings}  % Typesetting code
\lstset{basicstyle = \ttfamily, frame = tb}

%% Bibliography
\usepackage{mathscinet}
\usepackage[backend    = biber,
            sortcites  = true,
            giveninits = true,
            doi        = false,
            isbn       = false,
            url        = false,
            style      = alphabetic]{biblatex}
\DeclareNameAlias{sortname}{family-given}
\DeclareNameAlias{default}{family-given}
\DeclareFieldFormat[article]{volume}{\bibstring{jourvol}\addnbspace#1}
\DeclareFieldFormat[article]{number}{\bibstring{number}\addnbspace#1}
\renewbibmacro*{volume+number+eid}
{
    \printfield{volume}
    \setunit{\addcomma\space}
    \printfield{number}
    \setunit{\addcomma\space}
    \printfield{eid}
}
%\addbibresource{<NAME OF BIBLIOGRAPY FILE>.bib}

%% Cross references
\usepackage{varioref}
\usepackage[pdfusetitle]{hyperref}
\urlstyle{sf}
\usepackage[nameinlink, capitalize, noabbrev]{cleveref}
\crefname{chapter}{Section}{Sections}

%% Theorem-like environments
\declaretheorem[style = plain, numberwithin = chapter]{theorem}
\declaretheorem[style = plain,      sibling = theorem]{corollary}
\declaretheorem[style = plain,      sibling = theorem]{lemma}
\declaretheorem[style = plain,      sibling = theorem]{proposition}
\declaretheorem[style = definition, sibling = theorem]{definition}
\declaretheorem[style = definition, sibling = theorem]{example}
\declaretheorem[style = remark,    numbered = no]{remark}

%% Delimiters
\DeclarePairedDelimiter{\p}{\lparen}{\rparen}   % Parenthesis
\DeclarePairedDelimiter{\set}{\lbrace}{\rbrace} % Set
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert}   % Absolute value
\DeclarePairedDelimiter{\norm}{\lVert}{\rVert}  % Norm

%% Operators
\newcommand{\diff}{\mathop{}\!\mathrm{d}}
\DeclareMathOperator{\im}{im}
\DeclareMathOperator{\rank}{rank}
\DeclareMathOperator{\E}{E}
\DeclareMathOperator{\Var}{Var}
\DeclareMathOperator{\Cov}{Cov}

%% New commands for sets
\newcommand{\N}{\mathbb{N}}   % Natural numbers
\newcommand{\Z}{\mathbb{Z}}   % Integers
\newcommand{\Q}{\mathbb{Q}}   % Rational numbers
\newcommand{\R}{\mathbb{R}}   % Real numbers
\newcommand{\C}{\mathbb{C}}   % Complex numbers
\newcommand{\A}{\mathbb{A}}   % Affine space
\renewcommand{\P}{\mathbb{P}} % Projective space

%% New commands for vectors
\renewcommand{\a}{\mathbf{a}}
\renewcommand{\b}{\mathbf{b}}
\renewcommand{\c}{\mathbf{c}}
\renewcommand{\v}{\mathbf{v}}
\newcommand{\w}{\mathbf{w}}
\newcommand{\x}{\mathbf{x}}
\newcommand{\y}{\mathbf{y}}
\newcommand{\z}{\mathbf{z}}
\newcommand{\0}{\mathbf{0}}
\newcommand{\1}{\mathbf{1}}

%% Miscellanous
\renewcommand{\qedsymbol}{\(\blacksquare\)}

\title{Some Snappy Title}
\author{First Author}

\begin{document}

\maketitle

\begin{abstract}
    \noindent
    Brief summary of the paper.
\end{abstract}

\chapter{Introduction}

Purpose of the paper, historical context, necessary background information and notation.

\chapter{Body of the Work}

Full proofs, numerical implementations.

\begin{theorem}[Pythagoras]
    \label{thm:pythagoras}
    In a right triangle, the square of the hypotenuse is equal to the sum of the squares of the other two sides. That is,
    \begin{align}
        \label{eq:pythagoras}
            a^2 + b^2 = c^2,
    \end{align}
    where \(c\) is the length of the hypotenuse and \(a\) and \(b\) are the lengths of the two other sides.
\end{theorem}

\begin{proof}
    Draw a figure.
\end{proof}

\chapter{Conclusions}

Optional. Results, consequences, future work.

\cref{tab:numbers} lists some integers satisfying \cref{eq:pythagoras} of \cref{thm:pythagoras}.

\begin{table}[htbp]
    \centering
    \begin{tabular}{@{}ccc@{}}
        \toprule
        \(\boldsymbol{a}\) & \(\boldsymbol{b}\) & \(\boldsymbol{c}\)
        \\
        \midrule
        3 & 4 & 5
        \\
        65 & 72 & 97
        \\
        \bottomrule
    \end{tabular}
    \caption{Some interesting numbers}
    \label{tab:numbers}
\end{table}

\printbibliography

\end{document}


\documentclass{ximera}
\input{../preamble}
\title{Alternating Series}
%%%%%\author{Pat Smith}

\begin{document}
\begin{abstract}
We study the notion of alternating series and related concepts.
\end{abstract}
\maketitle

\section*{(Video) Calculus: Single Variable}
\youtube{E122NOGM2p4}

\section*{Online Texts}
\begin{itemize}
\item \link[OpenStax II 5.5: Alternating Series]{https://openstax.org/books/calculus-volume-2/pages/5-5-alternating-series}
\item \link[Ximera OSU: Alternating Series]{https://ximera.osu.edu/mooculus/calculus2/alternatingSeries/titlePage} and \link[Remainders for Alternating Series]{https://ximera.osu.edu/mooculus/calculus2/remainders/digInAlternatingSeriesTestRemainders} and \link[Absolute and Conditional Convergence]{https://ximera.osu.edu/mooculus/calculus2/absoluteAndConditionalConvergence/titlePage}
\item \link[Community Calculus 11.4: Alternating Series]{https://www.whitman.edu/mathematics/calculus_online/section11.04.html} and \link[11.6: Absolute Convergence]{https://www.whitman.edu/mathematics/calculus_online/section11.06.html}
\end{itemize}

\section*{Examples}

\begin{example}

\end{example}

\begin{example}

\end{example}

\end{document}


{%- if cookiecutter.template == "Homework" -%}

% {{cookiecutter.first_name}} {{cookiecutter.last_name}} - {{cookiecutter.title}}

\documentclass[11pt]{article}

%
% Packages
%

% Page setup
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{fancyhdr}

% Characters
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

% Math
\usepackage{amsmath}
\usepackage{amssymb}

% Algorithm pseudocode
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

% Images and captions
\usepackage{graphicx}
\usepackage{caption}
\usepackage{float}

% Lists
\usepackage[shortlabels]{enumitem}

% Hyperlinks
\usepackage{hyperref}
\hypersetup {
  % colorlinks=true,
  % urlcolor=blue,
}

% Code font
\usepackage{courier}

% Text
\usepackage{setspace}
\usepackage[export]{adjustbox}
\usepackage{textcomp}

%
% Math commands
%

\DeclareMathOperator{\E}{\mathbb{E}}
\DeclareMathOperator{\R}{\mathbb{R}}

%
% Code listing
%

\usepackage{listings}
\usepackage{xcolor}
\definecolor{code-green}{rgb}{0,0.6,0}
\definecolor{code-purple}{HTML}{8959a8}
\definecolor{code-light-grey}{HTML}{f2f2f2}
\definecolor{code-dark-grey}{HTML}{606060}
\lstset{
    backgroundcolor=\color{code-light-grey},
    basicstyle=\footnotesize\ttfamily,
    commentstyle=\color{code-dark-grey}\ttfamily\it,
    keywordstyle=\color{code-purple}\ttfamily\bf,
    stringstyle=\color{code-green},
    numberstyle=\tiny\color{code-dark-grey},
    numbersep=6pt,
    numbers=left,
    keepspaces=true,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    rulecolor=\color{code-light-grey},
    linewidth=1.0\textwidth
}

% ------------------------------------------------------------------------------

\setlength\parindent{0pt}
\setlength\parskip{5pt}
\pagestyle{fancy}
\fancyhf{}
\lhead{ {{cookiecutter.first_name}} {{cookiecutter.last_name}} }
\rhead{ {{cookiecutter.title}} - Page \thepage}

\begin{document}

\section{Equations}

\begin{align*}
  x &= y \\
  y &= x \\
\end{align*}

\newpage

\section{Code}

% \lstinputlisting[language=Python, firstline=1, lastline=6]{foo.py}

\newpage

\section{Pseudocode}

\begin{algorithm}
\begin{algorithmic}[1]
  \Procedure{foo}{$bar, baz$}
    \State \textbf{\textit{Input:}} Stuff
    \State \textbf{\textit{Output:}} More Stuff
    \While{$stuff$ is $stuff$}
      \State $bar \gets$ $baz$
      \State $bar = baz$
    \EndWhile
  \EndProcedure
\end{algorithmic}
\end{algorithm}

\newpage

\section{Image}

% \begin{figure}[H]
%   \includegraphics[width=\linewidth]{boat.jpg}
%   \caption{Caption}
%   \label{fig:label1}
% \end{figure}

\end{document}

{%- elif cookiecutter.template == "Presentation" -%}

% {{cookiecutter.first_name}} {{cookiecutter.last_name}} - {{cookiecutter.title}}

\documentclass[aspectratio=169]{beamer}

%
% Fonts
%

% Roboto light for main font; available in texlive-fonts-extra on Ubuntu.
\usepackage[light]{roboto}
% Math font is serif (i.e. looks like article math).
\usefonttheme[onlymath]{serif}
% Monospace font for code listings.
\usepackage{inconsolata}

%
% Packages
%

% Characters
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

% Math
\usepackage{amsmath}
\usepackage{amssymb}

% Algorithm pseudocode
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

% Images and captions
\usepackage{graphicx}
\usepackage{caption}
\usepackage{float}

% Hyperlinks
\usepackage{hyperref}
\hypersetup {
  % colorlinks=true,
  % urlcolor=blue,
}

%
% Math commands
%

\DeclareMathOperator{\E}{\mathbb{E}}
\DeclareMathOperator{\R}{\mathbb{R}}

%
% Code listing
%

\usepackage{listings}
\usepackage{xcolor}
\definecolor{code-green}{rgb}{0,0.6,0}
\definecolor{code-purple}{HTML}{8959a8}
\definecolor{code-light-grey}{HTML}{f2f2f2}
\definecolor{code-dark-grey}{HTML}{606060}
\lstset{
    backgroundcolor=\color{code-light-grey},
    basicstyle=\footnotesize\ttfamily,
    commentstyle=\color{code-dark-grey}\ttfamily\it,
    keywordstyle=\color{code-purple}\ttfamily\bf,
    stringstyle=\color{code-green},
    numberstyle=\tiny\color{code-dark-grey},
    numbersep=6pt,
    numbers=left,
    keepspaces=true,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    rulecolor=\color{code-light-grey},
    linewidth=1.0\textwidth,
    xleftmargin=8pt, % Adds some spacing on the sides.
    xrightmargin=8pt,
}

%
% Beamer settings
%

% Based on Madrid theme

% Color theme
\usecolortheme{whale}
\usecolortheme{orchid}

% Outer theme
\useoutertheme{infolines}
\setbeamertemplate{headline}[default]

% Inner theme.
\useinnertheme{rectangles}

% Bullets
\setbeamertemplate{itemize items}[default]
\setbeamertemplate{enumerate items}[default]

% Custom title page.
\setbeamerfont{title}{size=\huge}
\setbeamercolor{title}{bg=white,fg=black}
\setbeamerfont{subtitle}{shape=\itshape}
\defbeamertemplate*{title page}{clean}[1][]
{
  \begin{columns}
  \column{0.6\textwidth}
  \vbox{}
  \vfill
  \begingroup
    \centering
    \vskip2em
    \begin{beamercolorbox}[sep=5pt,left,#1]{title}
      \usebeamerfont{title}\inserttitle\par%
      \ifx\insertsubtitle\@empty%
      \else%
      \vskip0.5em
        {\usebeamerfont{subtitle}\usebeamercolor[fg]{subtitle}\insertsubtitle\par}%
      \fi%
    \end{beamercolorbox}%
    \begin{beamercolorbox}[sep=5pt,left,#1]{author}
      \usebeamerfont{author}\insertauthor
    \end{beamercolorbox}
    \begin{beamercolorbox}[sep=5pt,left,#1]{date}
      \usebeamerfont{date}\insertdate
    \end{beamercolorbox}\vskip0.5em
    % TODO
    % \begin{beamercolorbox}[sep=8pt,center,#1]{institute}
    %   \usebeamerfont{institute}\insertinstitute
    % \end{beamercolorbox}
    {\usebeamercolor[fg]{titlegraphic}\inserttitlegraphic\par}
  \endgroup
  \vfill
  \column{0.4\textwidth}
  (Some picture or other filler text)
  \end{columns}
}

% ------------------------------------------------------------------------------

\title[About Beamer] %optional
{About the Beamer class in presentation making}

\subtitle{A short story}

\author[Arthur, Doe] % (optional, for multiple authors)
{A.~B.~Arthur\inst{1} \and J.~Doe\inst{2}}

\institute[VFU] % (optional)
{
  \inst{1}%
  Faculty of Physics\\
  Very Famous University
  \and
  \inst{2}%
  Faculty of Chemistry\\
  Very Famous University
}

\date[VLC 2013] % (optional)
{Very Large Conference, April 2013}

% Adds TOC at beginning of each section.
% \AtBeginSection[]
% {
%   \begin{frame}
%     \frametitle{Table of Contents}
%     \tableofcontents[currentsection]
%   \end{frame}
% }

\begin{document}

\frame{\titlepage}

\begin{frame}
  \frametitle{Table of Contents}
  \tableofcontents
\end{frame}

\section{This is a section}

\begin{frame}[fragile]
\frametitle{Code}
\begin{lstlisting}[frame=single, language=Python]
def foobar(n):
  """Do stuff..."""
  x = 5000
  for i in range(n):
    # Very complicated.
    print("Hello World!")
\end{lstlisting}
\end{frame}

\begin{frame}
\frametitle{Equations}
A random equation:
$$ Q(s,a) = r(s,a) + \gamma\E_{a'|s \sim \pi}\left[Q(s',a')\right] $$
\end{frame}

\begin{frame}
\frametitle{Sample frame with bullets}
\begin{itemize}
    \pause
  \item This is a bullet.
    \pause
  \item This is another bullet.
    \pause
  \item And yet another.
\end{itemize}
\end{frame}

\section{This is another section}

\begin{frame}[fragile]
\frametitle{Two Columns}
\begin{columns}

\column{0.5\textwidth}
This is some text in the first column.

\column{0.5\textwidth}
This is some code in the second column.
\begin{lstlisting}[frame=single, language=Python]
def foobar(n):
  """Do stuff..."""
  x = 5000
  for i in range(n):
    # Very complicated.
    print("Hello World!")
\end{lstlisting}

\end{columns}
\end{frame}

\end{document}

{%- elif cookiecutter.template == "MLA" -%}

% {{cookiecutter.first_name}} {{cookiecutter.last_name}} - {{cookiecutter.title}}

\iffalse

## TODO

- [ ] foo

## Outline

- Intro
- Body 1
- Body 2
- Body 3
- Conclusion

\fi

\documentclass[12pt]{article}

% Margin - 1 inch on all sides
\usepackage[letterpaper]{geometry}
\usepackage{times}
\geometry{top=1.0in, bottom=1.0in, left=1.0in, right=1.0in}

% Doublespacing
\usepackage{setspace}
\doublespacing

% Rotating tables (e.g. sideways when too long)
\usepackage{rotating}

% Fancy-header package to modify header/page numbering (insert last name)
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{ {{cookiecutter.last_name}} \thepage}
\lfoot{}
\cfoot{}
\rfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
% To make sure we actually have header 0.5in away from top edge
% 12pt is one-sixth of an inch. Subtract this from 0.5in to get headsep value
\setlength\headsep{0.333in}

% Works cited environment
% (to start, use \begin{workscited...}, each entry preceded by \bibent)
\newcommand{\bibent}{\noindent \hangindent 0.5in}
\newenvironment{workscited}{\newpage \begin{center} Works Cited\end{center}}{\newpage }

% Begin document
\begin{document}
\begin{flushleft}

% First page name, class, etc
{{cookiecutter.first_name}} {{cookiecutter.last_name}}\\
{{cookiecutter.instructor}}\\
{{cookiecutter.class}}\\
{{cookiecutter.date}}\\

% Title
\begin{center}
{{cookiecutter.title}}
\end{center}

% Changes paragraph indentation to 0.5in
\setlength{\parindent}{0.5in}
% Begin body of paper here

Hello World

% Works cited
\begin{workscited}

% \bibent \textit{The Oxford Study Bible}. Edited by M. Jack Suggs, Katharine Doob
%   Sakenfeld, and James R. Mueller, Oxford University Press, 1992.

\end{workscited}

\end{flushleft}
\end{document}
\}

{%- elif cookiecutter.template == "Simple Essay" -%}

% {{cookiecutter.first_name}} {{cookiecutter.last_name}} - {{cookiecutter.title}}

\documentclass[11pt]{article}

\usepackage[top=0.5in, bottom=0.5in, left=0.5in, right=0.5in]{geometry}
\usepackage{titlesec}

%
% Font
%

\usepackage{fontspec}

% the main font, with all features on
\setmainfont
  [ ExternalLocation ,
    Mapping          = tex-text ,
    Numbers          = OldStyle ,
    Ligatures        = {Common,Contextual} ,
    BoldFont         = texgyrepagella-bold.otf ,
    ItalicFont       = texgyrepagella-italic.otf ,
    BoldItalicFont   = texgyrepagella-bolditalic.otf ]
  {texgyrepagella-regular.otf}

% Hyperlinks
\usepackage{hyperref}
\hypersetup {
  colorlinks=true,
  urlcolor=blue,
}

% No page break after title
\let\endtitlepage\relax

% Format section titles
\titleformat{\section}[block]{\bf\centering}{\thesection}{1em}{\MakeUppercase}
\titlespacing{\section}{0pt}{*0.5}{*0.5}

% Hyphenation
\hyphenpenalty=10000

\begin{document}

\frenchspacing
\pagenumbering{gobble}

\begin{center}
  \textbf{\uppercase{\Large{ {{cookiecutter.title}} }\\
  \normalsize{ {{cookiecutter.first_name}} {{cookiecutter.last_name}} }}}\\
  \vspace{-3mm}
\end{center}

Hello World

\iffalse

## TODO

- [ ] foo

## Outline

- Intro
- Body 1
- Body 2
- Body 3
- Conclusion

\fi

\end{document}

{%- endif -%}


\documentclass[12pt, fullpage,letterpaper]{article}

\usepackage[margin=1in]{geometry}
\usepackage{url}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{dsfont}
\usepackage{xspace}
\usepackage{graphicx}
\usepackage{hyperref}

\newcommand{\semester}{Spring 2022}
\newcommand{\assignmentId}{2}
\newcommand{\releaseDate}{22 Feb, 2022}
\newcommand{\dueDate}{11:59pm, 15 March, 2022}

\newcommand{\bx}{{\bf x}}
\newcommand{\bw}{{\bf w}}

\title{CS 6190: Probabilistic Machine Learning \semester}
\author{Homework \assignmentId}
\date{Handed out: \releaseDate\\
  Due: \dueDate}

\begin{document}
\maketitle

\input{emacscomm}
\footnotesize
	\begin{itemize}
		\item You are welcome to talk to other members of the class about
		the homework. I am more concerned that you understand the
		underlying concepts. However, you should write down your own
		solution. Please keep the class collaboration policy in mind.

		\item Feel free discuss the homework with the instructor or the TAs.

		\item Your written solutions should be brief and clear. You need to
		show your work, not just the final answer, but you do \emph{not}
		need to write it in gory detail. Your assignment should be {\bf no
			more than 10 pages}. Every extra page will cost a point.

		\item Handwritten solutions will not be accepted.

		\item The homework is due by \textbf{midnight of the due date}. Please submit
		the homework on Canvas.
	\end{itemize}

\section*{Analytical problems [60 points + 25 bonus]}
\label{sec:q1}
%1. show Jeffery's prior for Gaussian
%2. show Jeffern's prior for Poisson
\begin{enumerate}
\item~[10 points] Given a Gaussian likelihood, $p(x|\mu, \sigma)=\N(x|\mu, \sigma^2)=\frac{1}{\sqrt{2\pi}\sigma}\exp\big(-\frac{1}{2\sigma^2}(x-\mu)^2\big)$,  following the general definition of Jeffery's prior,
\begin{enumerate}
	\item~[5 points] show that given $\sigma$ fixed, the Jeffery's prior over $\mu$, $\pi_J(\mu) \propto 1$;
	\item~[5 points] show that given $\mu$ fixed, the Jeffery's prior over $\sigma$, $\pi_J(\sigma) \propto \frac{1}{\sigma}$.
\end{enumerate}

\item~[5 points] Derive the Jeffery's prior for $\lambda$ in the Poisson likelihood, $p(x=n) = e^{-\lambda}\frac{\lambda^n}{n!}$.

\item~[5 points] Given an infinite sequence of Independently Identically Distributed (IID) random variables, show that they  are exchangeable.

\item~[10 points] We discussed Polya's Urn  problem as an example of exchangeability. If you do  not recall, please look back at the slides we shared in the course website. Now, given  two finite sequences $(0, 1, 0, 1)$ and $(1,1, 0, 0)$,  derive their probabilities and show they are the same.

\item~[10 points] For the logistic regression model, we assign a Gaussian prior over the feature weights,  $p(\w) = \N(\w|\0, \lambda\I)$. Please derive the Newton-Raphson updates.

\item~[\textbf{Bonus}][20 points]  For the probit regression model, we assign a Gaussian prior over the feature weights,  $p(\w) = \N(\w|\0, \lambda\I)$. Please derive the Newton-Raphson updates.

\item~[10 points] What are the link functions of the following models?
\begin{enumerate}
	\item~[5 points] Logistic regression
	\item~[5 points] Poisson regression: $p(x=n) = e^{-\lambda}\frac{\lambda^n}{n!}$ where $\lambda = \w^\top \bphi$.
\end{enumerate}

\item~[10 points] As we discussed in the class, the probit regression model is equivalent to given each feature vector $\bphi$, sampling a latent variable $z$ from $\N(z|\w^\top \bphi,1)$,  and then sampling the binary label $t$ from the step distribution, $p(t|z) = \mathds{1}(t=0)\mathds{1}(z< 0) + \mathds{1}(t=1)\mathds{1}(z\ge 0)$ where $\mathds{1}(\cdot)$ is the indicator function. Show that if we marginalize out $z$, we recover the original likelihood of the probit regression.
\item~[\textbf{Bonus}][5 points] For polynomial regression (1d feature vector), show that given $N$ training points, you can always choose the highest order $M$ for the polynomial terms such that your model results in $0$ training error (\eg mean squared error or mean absolute error).  Please give the corresponding regression function as well.

\end{enumerate}

\section*{Practice [40 points + 45 Bonus]}
\begin{enumerate}
	\item~[15 Points] Let us generate a simulation dataset for fun. We consider a linear regression model $y(\x, \w) = w_0 + w_1 x$. We set the ground-truth $w_0 = -0.3$ and $w_1 = 0.5$. We generate $20$ samples $[x_1, \ldots, x_{20}]$ from the uniform distribution in $[-1, 1]$. For each sample $x_n$, we obtain an sample $y_n$ by first calculating $w_0 + w_1 x_n$ with the ground-truth values of $w_0$ and $w_n$, and then adding a Gaussian noise with zero mean, standard deviation $0.2$. Now let us verify what we have discussed in the class. We use a Bayesian linear regression model. The prior of $\w$ is $\N(\w|\0, \alpha \I)$, and the likelihood for each sample is $p(y_n|\x_n, \w) = \N(y_n|w_0 + w_1x, \beta^{-1}\I)$. Here we set $\alpha = 2$ and $\beta = 25$.
	\begin{enumerate}
		\item~[3 points] Draw the heat-map of the prior $p(\w)$ in the region $w_0 \in [-1, 1]$ and $w_1 \in [-1, 1]$, where you represent the values of $p(\w)$ for different choices of $\w$ with different colors. The darker some given color (\eg red), the larger the value; the darker some the other given color (\eg blue), the smaller the value.  Most colors should be in between. Then sample $20$ instances of $\w$ from $p(\w)$. For each $w$, draw a line $y = w_0 + w_1x$ in the region $x, y \in [-1, 1]$. Ensure these $20$ lines are in the same plot. What do you observe?
		\item~[3 points] Calculate and report the posterior distribution of $\w$ given $(\x_1, y_1)$. Now draw the heat map of the distribution. Also draw the ground-truth of $w_0$ and $w_1$ in the heat map.  Then from the posterior distribution, sample $20$ instances of $\w$, for each of which draw a line  $y = w_0 + w_1x$ in the region $x, y \in [-1, 1]$. Ensure these 20 lines are in the same plot. Also draw $(x_1, y_1)$ as a circle in that plot. What do you observe? Why?
		\item~[3 points] Calculate and report the posterior distribution of $\w$ given $(\x_1, y_1)$ and $(\x_2, y_2)$. Then draw the plots as the above. What do you observe now?
		\item~[3 points] Calculate and report the posterior distribution of $\w$ given $\{(\x_1, y_1), \ldots, (\x_5, y_5)\}$. Then draw the plots as the above. What do you observe now?
		\item~[3 points] Calculate and report the posterior distribution of $\w$ given all the $20$ data points. Then draw the plots as the above. What do you observe now?
	\end{enumerate}

	\item~[25 points] We will implement  Logistic regression and Probit regression for a binary classification task --- bank-note authentication. Please download the data ``bank-note.zip'' from Canvas. The features and labels are listed in the file ``bank-note/data-desc.txt''. The training data are stored in the file ``bank-note/train.csv'', consisting of $872$ examples. The test data are stored in ``bank-note/test.csv'', and comprise of $500$ examples. In both the training and testing datasets, feature values and labels are separated by commas. To ensure numerical stability and avoid overfitting, we assign the feature weights a standard normal prior $\N(\0, \I)$.
	\begin{enumerate}

	\item~[15 points] Implement Newton-Raphson scheme to find the MAP estimation of the feature weights in the logistic regression model. Set the maximum number of iterations to $100$ and the tolerance level to be $1e-5$,  \ie when the norm of difference between the weight vectors after one update is below the tolerance level, we consider it converges and stop updating the weights any more. Initially, you can set all the weights to be zero. Report the prediction accuracy on the test data. Now set the initial weights values be to be randomly generated, say, from the standard Gaussian, run and test your algorithm. What do you observe? Why?
	\item~[10 points]  Implement MAP estimation algorithm for Probit regression model. You can calculate the gradient and feed it to any optimization algorithm, say, L-BFGS. Set the maximum number of iterations to $100$ and the tolerance level to $1e-5$. Initially, you can set all the weights to zero. Report the prediction accuracy  on the test data. Compared with logistic regression, which one is better?
	Now set the initial weights values be to be randomly generated, say, from the standard Gaussian, run and test your algorithm. What do you observe? Can you guess why?
	\item~[\textbf{Bonus}][15 points]. Implement Newton-Raphson scheme to find the MAP estimation for Probit regression. Report the prediction accuracy

\end{enumerate}

	\item~[\textbf{Bonus}][30 points]  We will implement a multi-class logistic regression model for car evaluation task. The dataset is from UCI repository(\url{https://archive.ics.uci.edu/ml/datasets/car+evaluation}). Please download the processed dataset (car.zip) from Canvas.  In this task, we have $6$ car attributes, and the label is the evaluation of the car. The attribute and label values are listed in the file ``data-desc.txt". All the attributes are categorical. Please convert each categorical attribute into binary features. For example, for ``safety: low, med, high'', we convert it into three binary features: ``safety'' is ``low'' or not, ``safety'' is ``med'' or not, and ``safety'' is  ``high'' or not.
	The training data are stored in the file ``train.csv'', consisting of $1,000$ examples. The test data are stored in ``test.csv'', and comprise $728$ examples. In both training and test datasets, attribute values are separated by commas; the file ``data-desc.txt''  lists the attribute names in each column.  To ensure numerical stability and avoid overfitting, we assign the feature weights a standard normal prior $\N(\0, \I)$.
	\begin{enumerate}
		\item~[15 points] Implement MAP estimation algorithm for multi-class logistic regression model. To do so, you can calculate the gradient and feed it to some optimization package, say, L-BFGS. Report the prediction accuracy on the test data.
		\item~[15 points] Let us use an ``ugly'' trick to convert the multi-class classification problem into a binary classification problem.
		Let us train four logistic regression models, where each model predicts one particular label, \ie ``unacc" or not, ``acc'' or not, ``good'' or not, and ``vgood'' or not. Then for each test example, we run the models to get four logistic scores, \ie the probability that each label is one. We choose the label with the  highest score as the final prediction. Report the prediction accuracy on the test data. As compared with multi-class logistic regression ,which one is better?
	\end{enumerate}

\end{enumerate}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\subsubsection{Hand Crossbow}\label{weapon:handCrossbow}
Weapon, Hand Crossbow, One-Handed, Unusual, Ranged\\
Ammunition: Bolts\\
Size: S\\
Cost: 380 Gold

\textbf{Draw/Sheath} \\
2AP draw, 4AP sheath

\textbf{Shoot} \\
2 AP, DE to hit, Critical on 19 and 20, \passus{25} Reach\\
2d12 + \sfrac{1}{2}\texttimes DE Piercing Damage

\textbf{Reload} \\
4 AP from a back quiver\\
3 AP form a belt quiver\\
The hand crossbow has to be reloaded after every shot.\\
Reloading the Hand Crossbow is a two-handed activity.