\section{Functions and Polynomials}

\subsection{Floor function}

The floor function $\lfloor x \rfloor$ yields greatest integer
leq to argument. For positive values, equivalent to rounding down (truncating decimals).
For negative values, equivalent to next lowest negative integer.

\begin{example}
    $$\lfloor -3.2 \rfloor = -4$$
\end{example}

A useful simplification is

\begin{equation}
    \lfloor x \rfloor = \lfloor y+k \rfloor
\end{equation}

where $y$ is an integer and $0 \leq k < 1$. An alternate definition
is

\begin{equation}
    \lfloor x \rfloor = x - \{x\}
\end{equation}

where $\{x\}$ is the fractional component of $x$.

\subsection{Change of base formula}

Express a logarithm in base $b$ using logarithms of base $d$.
Let $d,a,b\in \R$ s.t $d,b\neq 1$.

\begin{equation}
    \log _{b} a=\frac{\log _{d} a}{\log _{d} b}
\end{equation}

\subsection{Polynomial division}

\begin{definition}[Polynomial remainder theorem]
    Upon dividing any polynomial $P(x)$ by linear polynomial $x-a$, the remainder is $P(a)$.
\end{definition}

We can express $P(x)$ as the following

\begin{equation}
    P(x)=(x-a)Q(x)+R(x)
\end{equation}

where $x-a$ is the dividend, $Q(x)$ is the quotient and $R(x)$ is the remainder.
Also, $\operatorname{deg} R(x)<\operatorname{deg}(x-a)$, hence $R(x)\in \R$.

The general approach is $P(x)=D(x)Q(x)-R(x)\implies R(x)=D(x)Q(x)-P(x)$.
Find zeros of $D(x)$ to eliminate $Q(x)$ and thus find $R(x)$ through substitution into $-P(x)$.

\subsection{Inverse functions}

\begin{definition}[Inverse of a function]
    Let $f(x):A\to B$ with range $C$. The inverse is $f^{-1}(x):C\to A$ iff $f$ is injective (i.e. a distinct one-to-one mapping from every value in $A$ to $C$).
    Can verify via horizontal line test.
\end{definition}

The properties are $f(f^{-1}(x))=x$ and $f^{-1}(f(x))=x$


	\subsubsection*{FLGV$\Rightarrow$ FLLPV}
	Construct \boldmath $A'=
	\begin{pmatrix}
	A\ I
	\end{pmatrix}$ where $I$ is identity matrix. Use algorithm for FLGV on $A'$ and $b$. Then we have exact one of the following:
	\begin{enumerate}
		\item $\exists x'\in \mathbb{R}^{n+m}:x'\geq 0,A'x'=b$.\\ Let $x'=\begin{pmatrix}
		x\\x''
		\end{pmatrix}$,$x\in \mathbb{R}^n,x''\in \mathbb{R}^m$.\\ So we have $b=A'x'=Ax+Ix''=Ax+x''\geq Ax$, thus $x\geq 0,Ax\leq b$
		\item $\exists y\in \mathbb{R}^{m}:A'^Ty\geq 0,b^Ty<0$.\\ So we have $\left\{\begin{aligned}
		A^Ty\geq& 0\\
		I^Ty\geq& 0
		\end{aligned}\right.$, thus $y\geq0,A^Ty\geq 0,b^Ty<0$
	\end{enumerate}
	\subsubsection*{FLLPV$\Rightarrow$ FLGV}
	Construct \boldmath $A'=
	\begin{pmatrix}
	A\\ -A
	\end{pmatrix}$,$b'=\begin{pmatrix}
	b\\ -b
	\end{pmatrix}$. Use algorithm for FLLPV on $A'$ and $b'$. Then we have exact one of the following:
	\begin{enumerate}
		\item $\exists x\in \mathbb{R}^n:x\geq 0,A'x\leq b'$.\\ So we have $\left\{\begin{aligned}
		Ax\leq& b\\
		-Ax\leq& -b
		\end{aligned}\right.$, thus $x\geq 0,Ax=b$
		\item $\exists y'\in \mathbb{R}^{2m}:y'\geq 0,A'^Ty'\geq 0,b^Ty'<0$.\\ Let $y'=\begin{pmatrix}
		y_1\\ y_2
		\end{pmatrix}$,where $y_1\in \mathbb{R}^m,y_2\in \mathbb{R}^m$.\\ So we have $A'^Ty'=A^Ty_1-A^Ty_2=A^T(y_1-y_2)\geq 0$, $b'^Ty'=b^Ty_1-b^Ty_2=b^T(y_1-y_2)<0$.\\ Let $y\in \mathbb{R}^m,y=y_1-y_2$, thus $A^Ty\geq 0,b^Ty<0$
	\end{enumerate}
	\subsubsection*{FLEV$\Rightarrow$ FLLPV}
	Construct \boldmath $A'=
	\begin{pmatrix}
	A\\ -I
	\end{pmatrix}$ where $I$ is identity matrix, $b'=\begin{pmatrix}
	b\\ 0
	\end{pmatrix}$. Use algorithm for FLEV on $A'$ and $b'$. Then we have exact one of the following:
	\begin{enumerate}
		\item $\exists x\in \mathbb{R}^n:A'x\leq b'$.\\ So we have $\left\{\begin{aligned}
		Ax\leq b\\
		-Ix\leq 0
		\end{aligned}\right.$, thus $x\geq 0,Ax\leq b$
		\item $\exists y'\in \mathbb{R}^{n+m}:y'\geq 0,A'^Ty'= 0,b'^Ty'<0$.\\ Let $y'=\begin{pmatrix}
		y\\y''
		\end{pmatrix},y\in \mathbb{R}^m,y''\in \mathbb{R}^n$.\\ So we have $A'^Ty'=A^Ty-Iy''=0$, $b'^Ty'=b^Ty<0$, thus $y\geq0,A^Ty\geq 0,b^Ty<0$
	\end{enumerate}
	\subsubsection*{FLLPV$\Rightarrow$ FLEV}
	Construct \boldmath $A'=
	\begin{pmatrix}
	A\ \ -A
	\end{pmatrix}$. Use algorithm for FLLPV on $A'$ and $b$. Then we have exact one of the following:
	\begin{enumerate}
		\item $\exists x'\in \mathbb{R}^{2n}:x'\geq 0,A'x'\leq b$.\\ Let $x'=\begin{pmatrix}
		x_1\\ x_2
		\end{pmatrix}$, where$x_1\in \mathbb{R}^n,x_2\in \mathbb{R}^n$.\\ So we have $A'x'=Ax_1-Ax_2=A(x_1-x_2)\leq b$. \\Let $x\in \mathbb{R}^n,x=x_1-x_2$, thus $Ax\leq b$
		\item $\exists y\in \mathbb{R}^m:y\geq 0,A'^Ty\geq 0,b^Ty<0$.\\ So we have $\left\{\begin{aligned}
		A^Ty\geq& 0\\
		-A^Ty\geq& 0
		\end{aligned}\right.$, thus $A^Ty=0,b^Ty<0$
	\end{enumerate}


\section{Convexity, Lipschitzness, and Smoothness}
\frame{\tableofcontents[currentsection, hideothersubsections]}

\begin{frame}
\frametitle{Convexity: Convex sets}

\begin{figure}
    \centering
    \includegraphics[scale=0.35]{cvx_noncvx}
\end{figure}

\begin{figure}
    \centering
    \includegraphics[scale=0.4]{cvx_set}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Convexity: Convex functions}

\begin{figure}
    \centering
    \includegraphics[scale=0.35]{cvx_fn}
\end{figure}

\begin{figure}
    \centering
    \includegraphics[scale=0.4]{cvx_fn_def}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Convexity: Convex functions}

Properties of convex fn:
\begin{itemize}
    \item  every local minimum is also a global minimum
    \item  for every $\mathbf{w}$ we can construct a tangent to $f$
    at $\mathbf{w}$ that lies below $f$ everywhere.
    If f is differentiable, this tangent is the linear function
    % $l(u) = f(w) + h∇f (w), u − wi$
\end{itemize}

\begin{figure}
    \centering
    \includegraphics[scale=0.25]{cvx_fn_tangent}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Convexity: Convex functions}

\begin{figure}
    \centering
    \includegraphics[scale=0.35]{lemma_12_3}
\end{figure}

\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}

\begin{figure}
    \centering
    \includegraphics[scale=0.375]{claim_12_4}
\end{figure}

\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}

\begin{figure}
    \centering
    \includegraphics[scale=0.375]{claim_12_5}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Lipschitzness}
\begin{figure}
    \centering
    \includegraphics[scale=0.45]{def_12_6}
\end{figure}

Properties:
\begin{itemize}
    \item a Lipschitz function can \textbf{not} change too fast
    \item if the derivative of $f$ is everywhere bounded (in absolute value) by $\rho$,
        then the function is $\rho$-Lipschitz.
    \item \textbf{composition} of Lipschitz functions \textbf{preserves} Lipschitzness.
\end{itemize}

\end{frame}

\begin{frame}
\frametitle{Smoothness}
\begin{figure}
    \centering
    \includegraphics[scale=0.45]{def_12_8}
\end{figure}

Properties:
\begin{itemize}
    \item when a function is \textbf{both} convex and smooth,\\
        we have both \textbf{upper and lower bounds} on the difference between
        the function and its first order approximation.
    \item a \text{composition} of a smooth scalar function over a linear function \textbf{preserves} smoothness.
    \begin{figure}
        \centering
        \includegraphics[scale=0.225]{claim_12_9}
    \end{figure}
\end{itemize}

\end{frame}


\subsection{Embeddings and immersions}
Whitney embedding theroem: all manfiolds can be embedded in \(R^n\) space for some \(n\).


\problemname{Infinite Slides}
Agneta and Beata are visiting the famous Swedish Amusement Park of Infinite Fun and Games.
Their favourite attraction is the Infinitely Long Slides.
Each slide can be viewed as an infinitely long, completely vertical spiral of radius $1$.

When projected into the (flat) ground, the first slide has $(0, 0)$ as its centre point, and the second one has $(1, 0)$ as its centre point.
Furthermore, the start of the slides are at $(1, 0)$ and $(2, 0)$ respectively, and then proceeds counter-clockwise around their respective centres.
When Agneta rides her slide (the first one), she does so with vertical speed $1\text{ m/s}$, while Beatas slide has a bit higher gradient, so she goes with vertical speed $2\text{ m/s}$.
However, their angular speeds around the centre is both equal to $1 \text{ rad/s}$.
Since Beata is a little afraid of the slides, she always waits for Agneta to go first.
$W$ seconds after Agneta went, Beata starts her descent.

To make the infinite ride a bit more fun, the amusement park gives a price if the two riders on the slides manage to pass a ball between them at some point.
This price consists of a lot of chocolate, which is Beata's favourite treat.
To maximize their chances of passing the ball successfully, they want to wait until their Euclidean distance is the smallest possible during the ride.
How small will this distance be?

\section*{Input}
The first and only line of the input contains the decimal number $0 \le W \le 1000$.
The number contains at most $6$ digits after the decimal point.

\section*{Output}
Output a single number -- the minimal Euclidean distance between Agneta and Beata during their trip.
Your answer will be accepted if it is within a relative or absolute error of $10^{-4}$ of the judges' answer.


\subsection{Introduction}
RSA (inventors: Rivest-Shamir-Adleman) belongs to PKCS (public key cryptosystem) where a pair of keys are required, one to encrypt another to decrypt the encrypted one. Encryption means changing a message to another using such a popular rule using some secret value (that no one but the person who is encrypting knows) no one can understand. In case of PKCS it is asymmetric cryptographic algorithm means one key is kept secret (private key) and another one is sent to everyone (public key) for decryption. The keys are long enough to make the adversary break both the keys.

\subsection{Property}
The security of RSA depends on the strengths of two separate functions. The RSA cryptosystem is most popular public-key cryptosystem strength of which is based on the practical difficulty of factoring the very large numbers.

\begin{itemize}
\item \textbf{Encryption Function:} It is considered as a one-way function of converting plaintext into ciphertext and it can be reversed only with the knowledge of private key d.
\item \textbf{Key Generation:} The difficulty of determining a private key from an RSA public key is equivalent to factoring the modulus n. An attacker thus cannot use knowledge of an RSA public key to determine an RSA private key unless he can factor n. It is also a one way function, going from p \& q values to modulus n is easy but reverse is not possible.
\end{itemize}

If either of these two functions are proved non one-way, then RSA will be broken. In fact, if a technique for factoring efficiently is developed then RSA will no longer be safe.

The strength of RSA encryption drastically goes down against attacks if the number p and q are not large primes and (or) chosen public key e is a small number.

\subsection{Algorithm}
\begin{enumerate}
\item Choose two different large random prime numbers $p$ and $q$
\item Calculate Maximum range of considered numbers: $$ n = p \times q $$
\item Calculate Euler's totient or Carmichael's totient (number of $+(ve)$ coprimes that are less than the given numbers): $$ \phi(n) = (p - 1) \times (q - 1) $$
\item Choose a random number in the range $[2, \phi(n))$ that is co-prime with both $n$ and $\phi(n)$; two numbers $a$ and $b$ are are co-prime means they have no common factor \textit{i.e.} $$ gcd(a, b) = 1 $$
\item Compute $d$ to satisfy the congruence relation
$$ d \times e \equiv 1 \mod \phi(n) $$
or $$ d \times e = 1 + k \times \phi(n) $$
or $$ d = \frac{1 + k \times \phi(n)}{e} $$
\item Private Key \{$d, n$\} is kept secret to decrypt the encrypted message after receiving from others and Public Key \{$e, n$\} is sent to everyone in the network to encrypt the encrypted message sent to the person who shared the public key.
\item Sender side encryption:
$$ c = m ^ e \mod n $$
\item Reciever side decryption:
$$ m = c ^ d \mod n $$
\item Afterwards they changed $\phi(n)$ to $\lambda(n)$ where,
$$ \lambda(n) = lcm(p - 1, q - 1) $$

\end{enumerate}

\subsection{Application}
Suppose Alice uses Bob's public key to send him an encrypted message. In the message, she can claim to be Alice but Bob has no way of verifying that the message was actually from Alice since anyone can use Bob's public key to send him encrypted messages. So, in order to verify the origin of a message, RSA can also be used to sign a message.

Suppose Alice wishes to send a signed message to Bob. She produces a hash value of the message, raises it to the power of d mod n (just like when decrypting a message), and attaches it as a \q(signature) to the message. When Bob receives the signed message, he raises the signature to the power of e mod n (just like encrypting a message), and compares the resulting hash value with the message's actual hash value. If the two agree, he knows that the author of the message was in possession of Alice's secret key, and that the message has not been tampered with since.

Note that secure padding schemes such as RSA-PSS are as essential for the security of message signing as they are for message encryption, and that the same key should never be used for both encryption and signing purposes.

\subsection{Citing}
There is a nice description in wikipedia ~\cite{wiki_rsa} and ~\cite{geeks4geeks_rsa}. The real paper ~\cite{rsa_rsa} and the paper ~\cite{milanov_rsa} helps making the code ourselves.


\documentclass{article}
\usepackage{color}
\usepackage{bigints}
\usepackage[italicdiff]{physics}
\color{white}
\definecolor{Blue}{RGB}{0,33,71}
\begin{document}
\pagecolor{Blue}
\title{Solution to Mathemaddict\textsc{\char13}s Problem}
\author{Jose Bedoya}
\maketitle
\section{Question}
{\LARGE
$$I=\bigintss_{1}^{\infty} \frac{2x\{x\}-\{x\}^2}{x^2\lfloor x\rfloor^2}\,dx=\frac{\pi^A}{B}-\phi^C$$

\vspace{4mm}
Then,
$$L=\lim_{\theta\to C} \frac{\sin\left(A\theta\right)\sec \left(\theta\right)}{B\theta}=\,?$$

\vspace{3mm}
Note: $\{x\}=x-\lfloor x\rfloor$
}
\section{Solution}
{\Large
Let\textsc{\char13}s expand the integral using the definition of the fractional part
$$I=\bigintsss_{1}^{\infty}\frac{2x^2-2x\lfloor x\rfloor-x^2+2x\lfloor x\rfloor-\lfloor x\rfloor^2}{x^2\lfloor x\rfloor^2}\,dx $$

\vspace{5mm}
Simplifying and by the definition of the floor function
$$I=\sum_{k=1}^{\infty}\bigintsss_{k}^{k+1}\frac{1}{k^2}-\frac{1}{x^2}\,dx$$

\vspace{4mm}
Therefore,
\newpage
$$I=\sum_{k=1}^{\infty}\left(\frac{x}{k^2}\Big|_k^{k+1}+\frac{1}{x}\Big|_k^{k+1}\right)$$
$$=\sum_{k=1}^{\infty}\left(\frac{1}{k^2}+\frac{1}{k+1}-\frac{1}{k}\right)$$

\vspace{3mm}
Evaluating the series we get
$$I=\frac{\pi^2}{6}-1$$

\vspace{3mm}
Clearly,
$$A=2,\,B=6,\, C=0$$

\vspace{5mm}
Substituting in the limit, we get
$$\lim_{\theta\to0} \frac{\sin\left(2\theta\right)\sec\left(\theta\right)}{6\theta}=\frac{1}{3}$$

\vspace{3mm}
Our final answer is
}
{\LARGE
$$L=\frac{1}{3}$$
}
\end{document}

Calculus/Jose Bedoya's questions/Integral with fractional part (2).tex


\documentclass{article}
%\usepackage{fullpage}
%\usepackage{nopageno}
\usepackage[margin=1.5in]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[normalem]{ulem}
\usepackage{fancyhdr}
%\renewcommand\headheight{12pt}
\pagestyle{fancy}
\lhead{March 5, 2014}
\rhead{Jon Allen}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\section*{Chapter 4}
\subsection*{\#43}
Let $X=\{a,b,c,d,e,f\}$ and let the relation $R$ on $X$ be defined by $a\,R\,b,b\,R\,c,c\,R\,d,a\,R\,e,e\,R\,f,f\,R\,d$. Verify that $R$ is the cover relation of a partially ordered set, and determine all the linear extensions of this partial order.
\begin{align*}
  a<_cb<_cc<_cd\\
  a<_ce<_cf<_cd
\end{align*}
Looks like $R$ is a cover relation since no elements can fit between any others, and all the elements are accounted for. I'll list the linear extensions now. These are basically all the combinations that have $a$ and $d$ on the ends and that respect $b<c, e<f$
\begin{align*}
  a<b<c<e<f<d\\
  a<b<e<c<f<d\\
  a<b<e<f<c<d\\
  a<e<b<c<f<d\\
  a<e<b<f<c<d\\
  a<e<f<b<c<d
\end{align*}
\subsection*{\#50}
Consider the partially ordered set $(X,\subseteq)$ of subsets of the set $X=\{a,b,c\}$ of three elements. How many linear extensions are there?

There is one set of no elements, 3 sets of 1 and 2 elements each, and one set of three elements. The number of linear extensions is the number of ways we can arrange the 1 and 2 element sets multiplied together. So $(3!)^2$ or 36.
\section*{Chapter 5}
\subsection*{\#22}
Prove that, for all real number $r$ and all integers $k$ and $m$,
\[\binom{r}{m}\binom{m}{k}=\binom{r}{k}\binom{r-k}{m-k}\]
I'll just let the algebra speak for itself
\begin{align*}
  \binom{r}{m}\binom{m}{k}&=\frac{r(r-1)\dots(r-m+1)}{m!}\cdot\frac{m!}{k!(m-k)!}\\
  &=\frac{r(r-1)\dots(r-k+1)(r-k)(r-k-1)\dots(r-m+1)}{k!(m-k)!}\\
  &=\frac{r(r-1)\dots(r-k+1)}{k!}\frac{(r-k)(r-k-1)\dots(r-m+1)}{(m-k)!}\\
  &=\binom{r}{k}\frac{(r-k)(r-k-1)\dots((r-k)-(m-k)+1)}{(m-k)!}\\
  &=\binom{r}{k}\binom{r-k}{m-k}\\
\Box
\end{align*}
\subsection*{\#25}
Use a combinatorial argument to prov the \emph{Vandermonde convolution} for the binomial coefficients: For all positive integers $m_1,m_2$ and $n$,
\[\sum\limits_{k=0}^n{\binom{m_1}{k}\binom{m_2}{n-k}}=\binom{m_1+m_2}{n}\]
Deduce the identity (5.16) as a special case.
\subsubsection*{proof}
Let $S$ be a set with $m_1+m_2$ elements.
The right side counts the number of $n$-subsets of $S$.
We partition $S$ into two subsets, $A$ and $B$, of $m_1$ and $m_2$ elements respectively.
We use this partition of $S$ to partition the $n$ subsets of $S$.
Each $n$-sized subset of $S$ contains $k$ elements from $A$ and $n-k$ elements from $B$.
Here, $k$ may be any integer between $0$ and $n$.
We partition the $n$-subsets of $S$ into $n+1$ parts,
\[C_0,C_1,C_2,\dots,C_n,\]
Where $C_k$ consists of those $n$-subsets which contain $k$ elements from $A$ and $n-k$ elements from $B$. By the addition principle,
\[\binom{m_1+m_2}{n}=\abs{C_0}+\abs{C_1}+\abs{C_1}+\dots+\abs{C_n}.\]
An $n$-subset in $C_k$ is obtained by choosing $k$ elements from $A$ (there are $\binom{m_1}{k}$ choice) and then $(n-k)$ elements from $B$ (there are $\binom{m_2}{n-k}$ choices).
Hence by the multiplication principle,
\[\abs{C_k}=\binom{m_1}{n}\binom{m_2}{n-k},\quad(k=0,1,\dots,n)\]
Combining our last two equations, we have our result
\[\sum\limits_{k=0}^n{\binom{m_1}{k}\binom{m_2}{n-k}}=\binom{m_1+m_2}{n}\]
$\Box$

Deducing $\sum\limits_{k=0}^n{\binom{n}{k}^2}=\binom{2n}{n},\quad(n\ge 0)$ is pretty straightforward. We just have to set $m_1=m_2=n$ and then we have $m_1+m_2=2n$. Since $\binom{n}{k}=\binom{n}{n-k}$
\begin{align*}
  \sum\limits_{k=0}^n{\binom{m_1}{k}\binom{m_2}{n-k}}=\binom{m_1+m_2}{n}\\
  \sum\limits_{k=0}^n{\binom{n}{k}\binom{n}{n-k}}=\binom{2n}{n}\\
  \sum\limits_{k=0}^n{\binom{n}{k}\binom{n}{k}}=\binom{2n}{n}\\
  \sum\limits_{k=0}^n{\binom{n}{k}^2}=\binom{2n}{n}\\
\end{align*}
\subsection*{\#26}
Let $n$ and $k$ be integers with $1\le k\le n$. Prove that
\[\sum\limits_{k=0}^n{\binom{n}{k}\binom{n}{k-1}=\frac{1}{2}\binom{2n+2}{n+1}-\binom{2n}{n}}\]
\subsection*{\#29}
Find and prove a formula for
\[\sum\limits_{r,s,t\ge 0}{\binom{m_1}{r}\binom{m_2}{s}\binom{m_3}{t}}\]
\[r+s+t=n\]
The formula is:
\[\binom{m_1+m_2+m_3}{n}=\sum\limits_{r,s,t\ge 0}{\binom{m_1}{r}\binom{m_2}{s}\binom{m_3}{t}}\]
\subsubsection*{proof}
Imagine we have a set $S$ partitioned into parts $A,B$ and $C$ sized $m_1,m_2$ and $m_3$ respectively. We can grab $n$ elements from $S$ in $\binom{m_1+m_2+m_3}{n}$ different ways. Now lets take some positive integers $r,s,t$ such that $r+s+t=n$. Lets imagine that when we grab our $n$ elements, that $r$ of them come from $A$, $s$ from $B$ and $t$ from $C$. The number of ways we can do this is $\binom{m_1}{r}\binom{m_2}{s}\binom{m_3}{t}$. Now if we wish to count all the ways we can grab those $n$ elements we must sum $\binom{m_1}{r}\binom{m_2}{s}\binom{m_3}{t}$ for all values of $r,s$ and $t$, which is $\sum\limits_{r,s,t\ge 0}{\binom{m_1}{r}\binom{m_2}{s}\binom{m_3}{t}}$. And so we see the formulas must be equal.$\Box$
\subsection*{\#43}
Prove by induction on $n$ that, for $n$ a positive integer,
\[\frac{1}{(1-z)^n}=\sum\limits_{k=0}^\infty{\binom{n+k-1}{k}z^k},\quad\abs{z}<1.\]
Assume the validity of
\[\frac{1}{1-z}=\sum\limits_{k=0}^\infty{z^k},\quad\abs{z}<1.\]
You may use a combinatorial proof instead of induction.
\begin{align*}
  \frac{1}{(1-z)^n}=\sum\limits_{k=0}^\infty{\binom{n+k-1}{k}z^k}\\
  \frac{1}{(1-z)^{n+1}}=\frac{1}{(1-z)^n(1-z)}\\
  \frac{1}{(1-z)^{n+1}}=\sum\limits_{k=0}^\infty{\binom{n+k-1}{k}\frac{z^k}{1-z}}\\
  \frac{1}{(1-z)^{n+1}}=\sum\limits_{k=0}^\infty{\frac{(n+k-1)(n+k-2)\dots(n+1)n}{k!(n+k-1-k)!}\frac{z^k}{1-z}}\\
  \frac{1}{(1-z)^{n+1}}=\sum\limits_{k=0}^\infty{\frac{(n+k)(n+k-1)\dots(n+2)(n+1)}{k!(n+k-k)!}z^k}\\
  \frac{1}{(1-z)^{n+1}}=\sum\limits_{k=0}^\infty{\frac{(n+k-1+1)(n+k-2+1)\dots(n+1+1)(n+1)}{k!(n+k-1-k+1)!}z^k}\\
\end{align*}
\subsection*{\#46}
Use Newton's binomial theorem to approximate $\sqrt{30}$. You may use the formula on p. 149.
\begin{align*}
  \sqrt{30}=\sqrt{25+5}&=5(1+0.2)^{1/2}=1+\sum\limits_{k=1}^\infty{\frac{(-1)^{k-1}}{k\cdot2^{2k-1}}\binom{2k-2}{k-1}0.2^k}\\
  &=5\left(1+\frac{1}{2}0.2-\frac{1}{2\cdot2^3}\binom{2}{1}0.2^2+\frac{1}{3\cdot2^5}\binom{4}{2}0.2^3-\dots\right)\\
  &\approx5.4775
\end{align*}
\subsection*{\#47}
Use Newton's binomial theorem to approximate $10^{1/3}$. It should be sufficient to evaluate 1/3 choose $k$ for $k=0,1,2,3$
\begin{align*}
  \sqrt[3]{10}&=\sqrt[3]{8(1+0.25)}=2\sqrt[3]{1+0.25}\\
  &=2(1+0.25)^{1/3}\\
  &=2\cdot\sum\limits_{k=0}^\infty{\binom{1/3}{k}0.25^k}\\
  &=2+2\cdot\sum\limits_{k=1}^\infty{\binom{1/3}{k}0.25^k}\\
  &=2+2\frac{1}{3}\cdot\frac{1}{4}+2\cdot\sum\limits_{k=2}^\infty{\binom{1/3}{k}0.25^k}\\
  &=2+\frac{1}{6}+2\frac{\frac{1}{3}\cdot-\frac{2}{3}}{2}\left(\frac{1}{4}\right)^2+2\cdot\sum\limits_{k=3}^\infty{\binom{1/3}{k}0.25^k}\\
  &=2+\frac{1}{6}-\frac{1}{72}+2\frac{\frac{1}{3}\cdot-\frac{2}{3}\cdot-\frac{5}{3}}{3!}\left(\frac{1}{4}\right)^3+2\cdot\sum\limits_{k=4}^\infty{\binom{1/3}{k}0.25^k}\\
  &=2+\frac{1}{6}-\frac{1}{72}+\frac{5}{27\cdot6}\left(\frac{1}{4}\right)^2+2\cdot\sum\limits_{k=4}^\infty{\binom{1/3}{k}0.25^k}\\
  &=2+\frac{1}{6}-\frac{1}{72}+\frac{5}{2592}+2\cdot\sum\limits_{k=4}^\infty{\binom{1/3}{k}0.25^k}\\
  &\approx2.1547+2\cdot\sum\limits_{k=4}^\infty{\binom{1/3}{k}0.25^k}\\
\end{align*}
\subsection*{\#48}
Use theorem 5.6.1 to show that, if $m$ and $n$ are positive integers, then a partially ordered set of $mn+1$ elements has a chain of size $m+1$ or an antichain of size $n+1$.
\subsection*{\#50}
Consider the partially ordered set $(X,\mid)$ on the set $X=\{1,2,\dots,12\}$ of the first 12 positive integers, partially ordered by ``is divisible by.''
\subsubsection*{(a)}
Determine a chain of largest size and a partition of $X$ into the smallest number of antichains.
\subsubsection*{(b)}
Determine an antichain of largest zise and a partition of $X$ into the smallest number of chains.
\end{document}


\subsection{The fixed effects estimator}
\subsubsection{Recap on the model}

Our model is:

\(y_{ij}=\alpha + X_{ij}\theta + \xi_j + \epsilon_{ij}\)

\subsubsection{The fixed effects estimator}

With fixed effects we assume that \(U_{ij}\) is a constant for each group. That is:

\(U_{ij}=\delta_{ij}U_j\)

\(y_{ij}=\alpha + X_{ij}\theta +\epsilon_{ij}+\delta_{ij}U_{j}\)

We can use this in a regression if the standard assumptions of OLS are met. In particular, that group membership is uncorrelated with the error term.

We add these dummies to \(X_{ij}\) and regress:

\(y_{ij}=\alpha + X_{ij}\theta +\epsilon_{ij}\)

The parameter for the dummy is the fixed effect of group membership.

As we are including membership in the dependent variables, there is no problem if group membership correlates with other independent variables.

\subsubsection{Using the within and between transformations}

\((y_{ij}-\bar y_{i})=(X_{ij}-\bar X_{i})\theta +(U_{ij} -\bar U_{i}) +(\epsilon_{ij}-\bar \epsilon_{i})\)

Or:

\(\ddot y_{ij}=\ddot X_{ij}\theta +\ddot \epsilon_{ij}\)

This this get the same outcome, but is a different computational process.


\chapter{Completions}
	\setlist[enumerate,1,2]{leftmargin=1.8cm}
	In this chapter we are intended to do \textit{elementary calculus} with a heavy taste of algebra instead of analysis. A lot of concepts in analysis are stolen by algebraists with different generalisations.
	\section{Absolute value}
		Let $K$ be a field. An \textbf{absolute value} on $K$ is a function $K \to \mathbb{R}$, $x \mapsto |x|_v$ satisfying the following three properties:
		\begin{enumerate}[start=1,label={\bfseries AV \arabic*}]
			\item We have $|x|_v \ge 0$ and $=0$ if and only if $x = 0$.
			\item For all $x,y \in K$ we have $|xy|_v = |x|_v|y|_v$.
			\item $|x+y|_v \le |x|_v + |y|_v$. \label{av3}
		\end{enumerate}

		If instead of \ref{av3} the absolute value satisfies the stronger condition
		\begin{enumerate}[start=4,label={\bfseries AV \arabic*}]
			\item $|x+y|_v \le \max(|x|_v,|y|_v)$
		\end{enumerate}
		then we shall say that it is a \textbf{valuation} or that it is non-archimedean. in particular, if the absolute value $|\cdot|$ satisfy $|x|=1$ for all $x \ne 0$, then we say $|\cdot|$ is \textbf{trivial}. We will exclude this case from now on. Our first goal is to study many possible valuations in general and in $\Q$. We are more interested in non-archimedean case because it \textit{breaks} our common sense in algebra:
		\begin{theorem}
			An absolute value $|\cdot|$ is non-archimedean if and only if there exists $M>0$ such that $|n \cdot 1|<M$ for all $n \in \N$.
		\end{theorem}
		\begin{proof}
			If $|\cdot|$ is non-archimedean, then
			\[
				|n| = |1+\cdots+1| \le 1, \forall n \in \N.
			\]

			Conversely, pick $x,y \in K$ and suppose $|x| \ge |y|$. Then
			\[
				\begin{aligned}
					|x+y|^n &= |(x+y)^n| \\
							&= \left|\sum_{j=0}^{n}{n \choose j}x^jy^{n-j}\right| \\
							&\le \sum_{j=0}^{n}\left|{n \choose j} \right||x|^j|y|^{n-j} \\
							&\le M(n+1)|x|^n.
				\end{aligned}
			\]
			Hence
			\[
				|x+y| \le \left(M(1+n)\right)^{1/n}|x| \to |x| \quad (n \to \infty).
			\]
		\end{proof}
		If $K$ is a infinite dimensional space, then this is not a big deal, but if we put $K=\Q$, does it make sense? Normally we would think the norm should grow bigger as $n$ grows.

	\section{Polynomials in complete fields}


\section{Approach}\label{approach}
Gene expression data are usually represented by the matrix $X = x_{ij}$ of the expression profiles of $i$ genes and $j$ individuals or sample tissues.
The main goal of the approach described in the current section is to infer the network topology that regulates the main interactions of the genes under investigation.
Generally speaking, a network model is formed by a set of vertices $G$, representing the genes in our specific case, and a set of edges $E$ representing pairwise interactions. The existence of edge $(i,j)$ represents the conditional dependency between gene $i$ and gene $j$.  If such an edge is not present, the two genes are considered conditionally independent, in the symbolic representation $(G_i \perp G_j) | G_k, \forall k \neq j$.
In the specific application described in this paper, we aim at finding the best set of neighbours associated to each gene. We interpret the biological meaning of genetic associations within the terms specified by regression analysis. Regressing the expression value of a gene (\emph{response}) against the remaining ones in the dataset (\emph{independent variables}) leads to selecting a subset of the most influential genes associated with the response.
Regardless of the number of mathematical models that have been considered for inferring the association between variables in genetics, linear regression is a type of analysis that has found large consensus in the field of computational biology due to its simplicity of modelling (\citealp{linregression2, linregression1}). %Moreover, the capabilities of linear regression can be extended to other genetic compounds from the field of proteomics, metabolomics, methylation etc. [move to intro]
One limitation of linear regression methods prevails in assuming a linear dependency between variables, a hypothesis that does not always apply in biology. One strategy to overcome such a limitation consists of splitting the problem of learning the topology of the entire network of genes into a number of smaller linear problems. This can be achieved by regressing each covariate against all the remaining ones.  Such a strategy, which has been used first in the work reported in (\citealp{Meinshausen06highdimensional}) makes the assumption of linearity more suitable to the analysis of biological data.  Assuming the presence of linearities on a local scale is a much more convincing and appropriate conjecture that might find an application to data from genomics and proteomics.
Another limitation that researchers have to take into account appears in the case of high-dimensional data. In such a scenario, the number of genes is usually some orders of magnitude larger than the number of the individuals.
Penalised regression has been considered as a way to circumvent such limitation due to the presence of a penalty factor that encourages sparsity of the final network.
Specifically, Lasso is one such regression method that converts the problem of estimating the covariance matrix into an optimisation problem in which a convex function, applied to each variable, is minimised.

Given $X_i$ the expression of gene $i$ and the expression profiles of the remaining genes  (referred to as $X$, for simplicity), the Lasso-based estimate consists of providing a solution for Equation \ref{eq:lasso}

\begin{equation}
\label{eq:lasso}
    \hat{\Theta}^{a,\lambda} =
    \argmin_{%
      \substack{%
        \text{s.\,t.}\, \Theta:\Theta_a = 0 \\
        \phantom{}\,
      }
    }
    (\frac{1}{n} \| X_i - X\Theta \|^{2}_2 + \lambda \| \Theta \|_1)
  \end{equation}

The vector of regression coefficients $\Theta$ determines the conditional independence structure between variables. The $l_1$-norm of the coefficient vector tends to shrink the coefficients of some variables to zero, removing them from the set of selected variables associated to the response, as extensively explained in (\citealp{Tibshirani94regressionshrinkage}).
The right choice of the shrinkage factor $\lambda$ is crucial to controlling the rate of false positives and false negatives. Regardless of the number of approaches to approximate the optimal $\lambda$, reported in (\citealp{adalasso, efron2004, tuneparamsel}), a reliable estimate that is widely used in practice is provided by cross-validation (\citealp{glmnet}).
We use a 3-fold cross validation approach and estimate $\hat{\lambda_{cv}}$ from a subset of the data. Cross-validation can be a time consuming task especially when applied to datasets with a high number of covariates. Therefore, we estimate the shrinkage factor that minimises the expected generalisation error, for a grid of $\lambda$ values, on the $10\%$ of the total number of genes. The R package $glmnet$ has been used to provide such an estimate.

The method we describe in this paper is a two-step approach that recursively performs the regression of Equation \ref{eq:lasso} for each gene, considered as response, with respect to all remaining genes, considered as independent variables. The response gene is not included in the set of independent variables. Regardless of biological evidence that supports the existence of self interactions and positive/negative feedback loops within regulatory networks (\citealp{netmotif, avrahamfeedback2011, generegmodel}), those are not considered here, in order to avoid complex interactions and simplify as much as possible the inferred network topology.

\textbf{In step 1}, the set $S$ of variables associated with the current response gene is selected. We use a Lasso method that does not fit the intercept. As explained, the choice of the optimal $\lambda$ occurs prior to this stage.

\textbf{In step 2}, we use a permutation-based approach to assess the significance of the associated edges detected in step 1. The values of the response variables are permuted a number of times specified as parameter. For each permutation we count how many times each variable within the set $S$ of selected genes has been selected again. At the end of the permutation test, the variables with the smallest counter are selected as the best candidate variables associated with the current response gene.

This approach is supported by the fact that after permuting the response variable, the genes selected at step 1 should be no longer associated and therefore should be considered as selected by chance.

\begin{algorithm}
 \begin{algorithmic}[1]
 \Procedure{lasso2net}{$X_i,X, B, fanout, best$}
 \State $fit \gets lasso.cv(X_i,X$)
 \State $\lambda_{cv} \gets fit.lambda$
 \State $S \gets fit.coeffs$
 \State $S \gets sort(S, decreasing)[1:best] $
 \While{$r < B$}
 \State $X^{perm}_i \gets permute(X_i)$
 \State $permfit \gets lasso(X^{perm}_i, X, \lambda_{cv}$)
 \State $update(counter[S])$  update counters of selected variables
 \State $r\gets r+1$
 \EndWhile
 \State $sel\gets sort(counter[S], increase)[1:fanout] $ order and select first fanout
 %\Comment{variables sorted by increasing order of counter}
 \State \textbf{return} $sel$
 \EndProcedure
 \end{algorithmic}
 \caption{Variable selection and permutation-based stability test}
 \label{algo:perm}
\end{algorithm}

The procedure we propose is summarised in Algorithm \ref{algo:perm}.
It selects the $best$ number of genes associated to the current response. Namely, the vector of the associated genes is sorted in decreasing order and the first $best$ are selected (\emph{line 5}). The parameter $best$ can be tuned in order to select a variable number of strong genetic effects according to the type of disease under investigation and the dataset at the researcher's disposal which, in turn, might determine the amount of significant genetic compounds to be considered for further analysis.
At each permutation, the counters of the selected variables are updated (\emph{line 9}) and after $B$ permutations the first $fanout$ genes are selected. These variables represent the most stable genes associated with the response variable (\emph{line 12}).

For large values of $B$ we perform an additional significance test for the smallest  counter. A critical case to deal with occurs whenever different covariates are selected with similar frequency. This phenomenon in turns produces uniform values of counters for a high number of selected variables. Lasso-based regression methods are affected by issues of this type due to the fact that one from a group of highly correlated variables can be randomly selected at each permutation. To mitigate such side effects, we compute the empirical distribution of the counters of the selected covariates regressed against each permuted response. The p-value of the smallest counter is calculated from the aforementioned empirical distribution. We found that a significance level of $0.05$ improves the precision (calculated as $\frac{TP}{TP+FP}$ ) by $3\%$.

The algorithm described above finds a solution of Equation \ref{eq:lasso} for each response variable. Subsequently, it finds the most stable non-zero regression coefficients associated to each gene.
Consequently, when the described procedure is performed on the entire set of genes, an adjacency matrix can be built directly from the counters of selected variables. The aforementioned adjacency matrix can be used to visualise the network topology of the inferred network of interactions.
Since we are interested in discovering genetic interactions we convert the non-zero values of the adjacency matrix to $1$, in order to denote the presence of an edge in the graph.
As one would expect, the method does not guarantee the adjacency matrix to be symmetric. A symmetrisation procedure would be required before further analysis or visualisation of the predicted network.

A number of approaches that perform matrix symmetrisation have been proposed in \citealp{wna}. Given two nodes $i$ and $j$ and the weights of the edges $M_{ij}$ and $M_{ji}$, the symmetric adjacency matrix can be built by taking the average value as in $M_{ij} = M_{ji} = mean(\frac{M_{ij}+M{ji}}{2})$; by selecting the largest weight $M_{ij} = M_{ji} = max(M_{ij}, M_{ji})$; or by selecting the smallest weight  $M_{ij} = M_{ji} = min(M_{ij}, M{ji})$.
For a binary adjacency matrix, in which each entry represents the presence or absence of the edge $(i,j)$, the AND rule will set $M_{ij} = M_{ji} = (M_{ij} \land M_{ji})$.
In order to detect a generic association between nodes $i$ and $j$, we symmetrise the adjacency matrix by applying the $OR$ rule which considers two variables as associated if only one of the two variables is associated with the other. Namely,
$M_{ij} = M_{ji} = (M_{ij} \vee M_{ji})$.

The main goal of the work described here is to detect the structure of the network of the main genetic associations, passing over the magnitude of interaction and its direction.


\subsection{Fraction rules}

\subsubsection{Addition}

\(\dfrac{A}{B}+\dfrac{C}{D}=\dfrac{AD+BC}{BD}\)

\subsubsection{Multiplication}

\(\dfrac{A}{B}\dfrac{C}{D}=\dfrac{AC}{BD}\)

\subsubsection{b Scaler addition}

\(C+\dfrac{A}{B}=\dfrac{BC+A}{B}\)

\subsubsection{Scaler multiplication}

\(C\dfrac{A}{B}=\dfrac{AC}{B}\)

\subsubsection{Other}

\(\dfrac{A+B}{C}=\dfrac{A}{C}+\dfrac{B}{C}\)

\(\dfrac{A}{B}=\dfrac{AC}{BC}\)


\section{Sequences}

Sum for finite arithmetic series with starting value $a$:

\begin{equation}
    \frac{2a+(n-1)d}{2}\cdot n
\end{equation}

Sum for infinite geometric series:

\begin{equation}
    \frac{a}{1-r}
\end{equation}

Sum for finite geometric series:

\begin{equation}
    \frac{a(1-r^n)}{1-r}
\end{equation}


\section{Normal Form Games and Nash Equilibria}
\label{gth-sect}

The idea of {\em game} as model of strategic interaction was first
introduced by von Neumann \cite{vn28}. A {\em finite normal form game} is
$\Gamma=(P,S,u)$ where both $P$ and $S$ are finite.
Here $P$ is the set of {\em players}, and $S=\times_{p\in P} S_p$ is
the set of {\em pure strategy profiles}, where $S_p$ is the set of
{\em pure strategies} of player~$p$.
The aim of each player $p\in P$ is to maximize their
{\em payoff function} $u^p:S\to\reals$.
The vector of payoffs is $u=\times_{p\in P}u^p$.
By ``game'' we will always mean ``finite normal form game.''
A {\em mixed strategy} of player $p$ is a probability
distribution on $S_p$. It can be described as a point on the
$(|S_p|-1)$-dimensional {\em mixed strategy simplex}
% you used subscript for the dimension earlier and should
% not use it for the player; hence superscript
\[
\Delta^p=\{ x\in\reals^{|S_p|}\ \mid\ x\geq\0,\ \1\T x = 1 \}.
\]
The set of {\em mixed strategy profiles} is the simplicial polytope
$\Delta=\times_{p\in P}\Delta^p$.
We extend the payoff functions to $u^p:\Delta\to\reals$ linearly.
A {\em Nash equilibrium} of a game is a strategy profile in which each
player cannot improve their expected payoff by unilaterally changing their
strategy; such a strategy is called a {\em best response}.
Notice that applying a positive affine transformation to all the payoffs
does not change the Nash equilibria of the game.
The following ``best-response condition'' is useful and easy
to show (e.g., Nash \cite{nash}).

\begin{proposition}
\label{br-played-thm}
A mixed strategy $x\in \Delta^p$ is a best response against some mixed
strategy profile $y\in \times_{q\neq p}\Delta^{q}$ of the other players
if and only if
every pure strategy $s_i\in S_p$ chosen with positive probability
in $x$ is a best response to $y$.
\end{proposition}

The existence of a Nash equilibrium is guaranteed by
the fundamental theorem by Nash (\cite{nash}). Notice that there can be
more than one equilibrium.

\begin{theorem}{\rm (Nash \cite{nash})}\label{nash-thm}
Every finite game in normal form has at least one Nash equilibrium.
\end{theorem}

{\em Bimatrix games} are games with only two players. They can be
% characterized by  % "characterized" suggests a theorem
described by
two $m \times n$ payoff matrices $A$ and $B$, where $a_{ij}$ and $b_{ij}$ are
the payoffs of respectively player 1 and of player 2 when the former plays
her $i$th pure strategy and the latter plays his $j$th pure
strategy, and $m=|S_1|$ and $n=|S_2|$.
A bimatrix game is {\em zero-sum} if $B=-A$, and {\em symmetric} if  $B=A\T$.
We give two classic examples of bimatrix games: the prisoners' dilemma and
the coordination game.

\begin{example}
In the symmetric non zero-sum {\em prisoners' dilemma} of
Table~\ref{prisoners-dilemma}, each player must
decide whether to ``help'' the other one or to ``betray'' them. If both
players help each other, they will get a small reward. If both betray, they
will pay a small penalty. If one betrays and the other cooperate the former
will get a large reward and the latter will pay a large penalty.
The only equilibrium is the profile in which both players betray.
If player~2 betrays, the best response of player~1 is to betray, since
it gives her payoff 1 instead of 0. If player~2 helps, her payoff
for betraying is 3 and her payoff for helping is 2, so betraying is
again the best response. The same holds for player~2, so at the
equilibrium both players will betray.

\begin{table}[hbt]
\begin{center}
\def\mm#1{\makebox(0,0){\strut#1}}
\bimatrixgame{3mm}{2}{2}{1}{2}
{{\scriptsize betray}{\scriptsize help}}
{{\scriptsize betray}{\scriptsize help}}
{
\payoffpairs{1}{{1}{3}}{{1}{0}}
\payoffpairs{2}{{0}{2}}{{3}{2}}
}
\end{center}
\caption[The prisoners' dilemma]{The prisoners' dilemma.}
\label{prisoners-dilemma}
\end{table}

Table~\ref{coordination-game} shows a {\em coordination} game.
Both players can
% match the Table description
drive on either a mountain or a valley road. They lose if
they drive on the same road and win if they avoid each
other, regardless of which road they take.
The pure strategy Nash equilibria are (mountain,~valley) and
(valley,~mountain). There is also a symmetric equilibrium in
mixed strategies which can be represented as the vectors of probabilities
$((1/2,1/2),(1/2,1/2))$.

\begin{table}[hbt]
\begin{center}
\def\mm#1{\makebox(0,0){\strut#1}}
\bimatrixgame{3mm}{2}{2}{{{\small 1}}}{{{\small 2}}}
{{\scriptsize mountain}{\scriptsize valley}}
{{\scriptsize mountain}{\scriptsize valley}}
{
\payoffpairs{1}{{0}{1}}{{0}{1}}
\payoffpairs{2}{{1}{0}}{{1}{0}}
}
\end{center}
\caption[A coordination game]{A coordination game.}
\label{coordination-game}
\end{table}
\end{example}


\lab{Gradient Descent Methods}{Gradient Descent Methods}

\objective{Iterative optimization methods choose a search direction and a step size at each iteration.
One simple choice for the search direction is the negative gradient, resulting in the method of steepest descent.
While theoretically foundational, in practice this method is often slow to converge.
An alternative method, the conjugate gradient algorithm, uses a similar idea that results in much faster convergence in some situations.
In this lab we implement a method of steepest descent and two conjugate gradient methods, then apply them to regression problems.
}

\section*{The Method of Steepest Descent} % ===================================

Let $f:\mathbb{R}^n \rightarrow \mathbb{R}$ with first derivative $Df:\mathbb{R}^n \rightarrow \mathbb{R}^n$.
% (by convention, $Df(\x)$ is a row vector).
The following iterative technique is a common template for methods that aim to compute a local minimizer $\x^*$ of $f$.
% Many optimization methods fall under the umbrella of descent algorithms.
% These algorithms utilize an initial guess, a direction on which the objective function decreases, and a step size to perform a line search for the next point in the iteration.
% This can be mathematically formulated at the $k$th iteration with the following:
\begin{equation}
\x_{k+1} = \x_k + \alpha_{k}\mathbf{p}_k
\label{eq:gradientmethods-linesearch}
\end{equation}
Here $\x_k$ is the $k$th approximation to $\x^*$, $\alpha_k$ is the \emph{step size}, and $\mathbf{p}_k$ is the \emph{search direction}.
Newton's method and its relatives follow this pattern, but they require the calculation (or approximation) of the inverse Hessian matrix $Df^2(\x_k)^{-1}$ at each step.
The following idea is a simpler and less computationally intensive approach than Newton and quasi-Newton methods.

The derivative $Df(\x)\trp$ (often called the \emph{gradient} of $f$ at $\x$, sometimes notated $\nabla f(\x)$) is a vector that points in the direction of greatest \textbf{increase} of $f$ at $\x$.
It follows that the negative derivative $-Df(\x)\trp$ points in the direction of steepest \textbf{decrease} at $\x$.
The \emph{method of steepest descent} chooses the search direction $\p_k = -Df(\x_k)\trp$ at each step of \eqref{eq:gradientmethods-linesearch}, resulting in the following algorithm.
\begin{equation}
\x_{k+1} = \x_k - \alpha_{k}Df(\x_k)\trp
\label{eq:gradientmethods-steepest-descent}
\end{equation}

Setting $\alpha_k = 1$ for each $k$ is often sufficient for Newton and quasi-Newton methods.
However, a constant choice for the step size in \eqref{eq:gradientmethods-steepest-descent} can result in oscillating approximations or even cause the sequence $(\x_k)_{k=1}^\infty$ to travel away from the minimizer $\x^*$.
To avoid this problem, the step size $\alpha_k$ can be chosen in a few ways.
\begin{itemize}
    \item Start with $\alpha_k = 1$, then set $\alpha_k = \frac{\alpha_k}{2}$ until $f(\x_k - \alpha_k Df(\x_k)\trp) < f(\x_k)$, terminating the iteration if $\alpha_k$ gets too small.
    This guarantees that the method actually descends at each step and that $\alpha_k$ satisfies the Armijo rule, without endangering convergence.

    \item At each step, solve the following one-dimensional optimization problem.
    \[
    \alpha_k = \underset{\alpha}{\text{argmin}}\ f(\x_k - \alpha Df(\x_k)\trp)
    \]
    Using this choice is called \emph{exact steepest descent}.
    This option is more expensive per iteration than the above strategy, but it results in fewer iterations before convergence.
\end{itemize}

\begin{problem} % Implement exact steepest descent.
Write a function that accepts an objective function $f:\mathbb{R}^n\rightarrow\mathbb{R}$, its derivative $Df:\mathbb{R}^n\rightarrow\mathbb{R}^n$, an initial guess $\x_0\in\mathbb{R}^n$, a convergence tolerance \li{tol} defaulting to $1e^{-5}$, and a maximum number of iterations \li{maxiter} defaulting to $100$.
Implement the exact method of steepest descent, using a one-dimensional optimization method to choose the step size (use \li{opt.minimize_scalar()} or your own 1-D minimizer).
Iterate until $\|Df(\x_k)\|_{\infty} < $ \li{tol} or $k > $ \li{maxiter}.
Return the approximate minimizer $\x^*$, whether or not the algorithm converged (\li{True} or \li{False}), and the number of iterations computed.

Test your function on $f(x,y,z) = x^4 + y^4 + z^4$ (easy) and the Rosenbrock function (hard).
It should take many iterations to minimize the Rosenbrock function, but it should converge eventually with a large enough choice of \li{maxiter}.
\label{prob:gradientmethods-steepest-descent}
\end{problem}

\section*{The Conjugate Gradient Method} % ====================================

Unfortunately, the method of steepest descent can be very inefficient for certain problems.
Depending on the nature of the objective function, the sequence of points can zig-zag back and forth or get stuck on flat areas without making significant progress toward the true minimizer.

\begin{figure}[H]
\centering
\includegraphics[width=.7\textwidth]{figures/steepest.pdf}
\caption{On this surface, gradient descent takes an extreme number of iterations to converge to the minimum because it gets stuck in the flat basins of the surface.}
\label{basis:steepest}
\end{figure}

Unlike the method of steepest descent, the \emph{conjugate gradient algorithm} chooses a search direction that is guaranteed to be a descent direction, though not the direction of greatest descent.
These directions are using a generalized form of orthogonality called \emph{conjugacy}.

Let $Q$ be a square, positive definite matrix.
A set of vectors $\{\x_0, \x_1, \ldots, \x_m\}$ is called \emph{Q-conjugate} if each distinct pair of vectors $\x_i,\x_j$ satisfy $\x_i\trp Q\x_j = 0$.
A $Q$-conjugate set of vectors is linearly independent and can form a basis that diagonalizes the matrix $Q$.
This guarantees that an iterative method to solve $Q\x = \b$ only require as many steps as there are basis vectors.

Solve a positive definite system $Q\x = \b$ is valuable in and of itself for certain problems, but it is also equivalent to minimizing certain functions.
Specifically, consider the quadratic function
\[
f(\x) = \frac{1}{2}\x\trp Q\x - \b\trp\x + c.
\]
Because $Df(\x)\trp = Q\x - \b$, minimizing $f$ is the same as solving the equation
\[
\0 = Df(\x)\trp = Q\x - \b\quad\Rightarrow\quad Q\x = \b,
\]
which is the original linear system.
Note that the constant $c$ does not affect the minimizer, since if $\x^*$ minimizes $f(\x)$ it also minimizes $f(\x)+c$.

% This technique is very useful for solving large systems of equations in situations where other methods are unsuitable and where the matrix $Q$ is positive definite, which implies invertibility.
% It works equally well for optimizing convex quadratic functions and can be extended to more general non-linear classes of optimization.

% \subsection*{The Algorithm} % -----------------------------------------------

Using the conjugate directions guarantees an iterative method to converge on the minimizer because each iteration minimizes the objective function over a subspace of dimension equal to the iteration number.
Thus, after $n$ steps, where $n$ is the number of conjugate basis vectors, the algorithm has found a minimizer over the entire space.
In certain situations, this has a great advantage over gradient descent, which can bounce back and forth.
This comparison is illustrated in Figure \ref{basis:steepVsConj}.
Additionally, because the method utilizes a basis of conjugate vectors, the previous search direction can be used to find a conjugate projection onto the next subspace, saving computational time.

\begin{figure}[H]
\centering
\includegraphics[width=.7\textwidth]{figures/steepVsConj.pdf}
\caption{Paths traced by Gradient Descent (orange) and Conjugate Gradient (red) on a quadratic surface.
Notice the zig-zagging nature of the Gradient Descent path, as opposed to the Conjugate Gradient path, which finds the minimizer in 2 steps.}
\label{basis:steepVsConj}
\end{figure}

% Algorithm \ref{Alg:linear-conjugate-gradient} contains these techniques and outlines an iterative process2 for solving the linear system $Q\x = \b$.

\begin{algorithm}[H]
\begin{algorithmic}[1]
\Procedure{Conjugate Gradient}{$\x_0$, $Q$, $\mathbf{b}$, \li{tol}}
    % \State \textrm{Choose initial point } $\x_0$.
    \State $\mathbf{r}_0 \gets Q\x_0 - \b$
    \State $\mathbf{d}_0 \gets -\mathbf{r}_0$
    \State $k \gets 0$
    \While{$\|\mathbf{r}_k\| \geq $ \li{tol},\ $k < n$}
        \State $\alpha_k \gets \mathbf{r}_k\trp \mathbf{r}_k / \mathbf{d}_k\trp Q\mathbf{d}_k$
        \State $\x_{k+1} \gets \x_k + \alpha_k \mathbf{d}_k$
        \State $\mathbf{r}_{k+1} \gets \mathbf{r}_k + \alpha_k Q\mathbf{d}_k$
        \State $\beta_{k+1} \gets \mathbf{r}_{k+1}\trp \mathbf{r}_{k+1} / \mathbf{r}_k\trp \mathbf{r}_k$
        \State $\mathbf{d}_{k+1} \gets -\mathbf{r}_{k+1} + \beta_{k+1}\mathbf{d}_k$
        \State $k \gets k+1$.
    \EndWhile
    \pseudoli{return} $\x_{k+1}$
\EndProcedure
\end{algorithmic}
\caption{}
\label{Alg:linear-conjugate-gradient}
\end{algorithm}

The points $\x_k$ are the successive approximations to the minimizer, the vectors $\mathbf{d}_k$ are the conjugate descent directions, and the vectors $\mathbf{r}_k$ (which actually correspond to the steepest descent directions) are used in determining the conjugate directions.
The constants $\alpha_k$ and $\beta_k$ are used, respectively, in the line search, and in ensuring the $Q$-conjugacy of the descent directions.

\begin{problem} % Conjugate gradient for linear systems.
Write a function that accepts an $n \times n$ positive definite matrix $Q$, a vector $\b\in\mathbb{R}^n$, an initial guess $\x_0\in\mathbb{R}^n$, and a stopping tolerance.
Use Algorithm \ref{Alg:linear-conjugate-gradient} to solve the system $Q\x = \b$.
Continue the algorithm until $\|\mathbf{r}_k\|$ is less than the tolerance, iterating no more than $n$ times.
Return the solution $\x$, whether or not the algorithm converged in $n$ iterations or less, and the number of iterations computed.

Test your function on the simple system
\[
Q = \left[\begin{array}{cc}2 & 0 \\ 0 & 4\end{array}\right],
\qquad
\b = \left[\begin{array}{c}1 \\ 8\end{array}\right],
\]
which has solution $\x^* = \left[\frac{1}{2}, 2\right]\trp$.
This is equivalent to minimizing the quadratic function $f(x,y) = x^2 + 2y^2 - x - 8y$; check that your function from Problem \ref{prob:gradientmethods-steepest-descent} gets the same solution.

More generally, you can generate a random positive definite matrix $Q$ for testing by setting setting $Q = A\trp A$ for any $A$ of full rank.
\begin{lstlisting}
>>> import numpy as np
>>> from scipy import linalg as la

# Generate Q, b, and the initial guess x0.
>>> n = 10
>>> A = np.random.random((n,n))
>>> Q = A.T @ A
>>> b, x0 = np.random.random((2,n))

>>> x = la.solve(Q, b)      # Use your function here.
>>> np.allclose(Q @ x, b)
<<True>>
\end{lstlisting}
\label{prob:gradientmethods-linear-cg}
\end{problem}

\subsection*{Non-linear Conjugate Gradient}
The algorithm presented above is only valid for certain linear systems and quadratic functions, but the basic strategy may be adapted to minimize more general convex or non-linear functions.
Though the non-linear version does not have guaranteed convergence as the linear formulation does, it can still converge in less iterations than the method of steepest descent.
Modifying the algorithm for more general functions requires new formulas for $\alpha_k$, $\mathbf{r}_k$, and $\beta_k$.

\begin{itemize}
\item The scalar $\alpha_k$ is simply the result of performing a line-search in the given direction $\mathbf{d}_k$ and is thus defined $\alpha_k = \underset{\alpha}{\text{argmin}}\ f(\x_k + \alpha \mathbf{d}_k)$.
\item The vector $\mathbf{r}_k$ in the original algorithm was really just the gradient of the objective function, so now define $\mathbf{r}_k = Df(\x_k)\trp$.
\item The constants $\beta_k$ can be defined in various ways, and the most correct choice depends on the nature of the objective function.
A well-known formula, attributed to Fletcher and Reeves, is $\beta_{k} = Df(\x_k)Df(\x_{k})\trp / Df(\x_{k-1}) Df(\x_{k-1})\trp$.
\end{itemize}
%
% Inserting these adjustments to Algorithm \ref{Alg:linear-conjugate-gradient} results in the following routine.

\begin{algorithm}[H]
\begin{algorithmic}[1]
\Procedure{Non-Linear Conjugate Gradient}{$f$,\ $Df$,\ $\x_0$,\ \li{tol},\ \li{maxiter}}
    \State $\mathbf{r}_0 \gets -Df(\x_0)\trp$
    \State $\mathbf{d}_0 \gets \mathbf{r}_0$
    \State $\alpha_0 \gets \underset{\alpha}{\text{argmin}} f(\x_0 + \alpha\mathbf{d}_0)$
    \State $\x_{1} \gets \x_0 + \alpha_0 \mathbf{d}_0$
    \State $k \gets 1$
    \While{$\|\mathbf{r}_k\| \geq$ \li{tol},\ $k < $ \li{maxiter}}
        \State $\mathbf{r}_{k} \gets -Df(\x_k)\trp$
        \State $\beta_{k} = \mathbf{r}_{k}\trp \mathbf{r}_k / \mathbf{r}_{k-1}\trp \mathbf{r}_{k-1}$
        \State $\mathbf{d}_{k} \gets \mathbf{r}_{k} + \beta_{k}\mathbf{d}_{k-1}$.
        \State $\alpha_k \gets \underset{\alpha}{\text{argmin}}\ f(\x_k + \alpha\mathbf{d}_k)$.
        \State $\x_{k+1} \gets \x_k + \alpha_k\mathbf{d}_k$.
        \State $k \gets k+1$.
    \EndWhile
\EndProcedure
\end{algorithmic}
\caption{}
\label{Alg:nonlinear-conjugate-gradient}
\end{algorithm}

\begin{problem}
Write a function that accepts a convex objective function $f$, its derivative $Df$, an initial guess $\x_0$, a convergence tolerance defaultin to $1e^{-5}$, and a maximum number of iterations defaultin to $100$.
Use Algorithm \ref{Alg:nonlinear-conjugate-gradient} to compute the minimizer $\x^*$ of $f$.
Return the approximate minimizer, whether or not the algorithm converged, and the number of iterations computed.

Compare your function to SciPy's \li{opt.fmin_cg()}.
\begin{lstlisting}
>>> opt.fmin_cg(opt.rosen, np.array([10, 10]), fprime=opt.rosen_der)
<<Optimization terminated successfully.
         Current function value: 0.000000
         Iterations: 44
         Function evaluations: 102>>  # Much faster than steepest descent!
         <<Gradient evaluations: 102>>
array([ 1.00000007,  1.00000015])
\end{lstlisting}
\label{prob:gradientdescent-nonlinear-cg}
\end{problem}

\section*{Regression Problems} % ==============================================

A major use of the conjugate gradient method is solving linear least squares problems.
Recall that a least squares problem can be formulated as an optimization problem:
\[
\x^* = \min_\x \|A\x -\textbf{ b}\|_2,
\]
where $A$ is an $m \times n$ matrix with full column rank, $\x \in \mathbb{R}^n$, and $\b \in \mathbb{R}^m$. The solution can
be calculated analytically, and is given by
\[
\x^* = (A\trp A)^{-1}A\trp \b.
\]
In other words, the minimizer solves the linear system
\begin{equation}
A\trp A\x = A\trp \b.
\label{eq:ls}
\end{equation}
Since $A$ has full column rank, it is invertible, $A\trp A$ is positive definite, and for any non-zero vector $\textbf{z}$, $A\textbf{z}\neq 0$.
Therefore, $\textbf{z}\trp A\trp A\textbf{z} = \Vert A\textbf{z} \Vert^2 > 0$.
As $A\trp A$ is positive definite, conjugate gradient can be used to solve Equation \ref{eq:ls}.

Linear least squares is the mathematical underpinning of \emph{linear regression}.
Linear regression involves a set of real-valued data points \{$y_1,\ldots, y_m\}$, where each
$y_i$ is paired with a corresponding set of predictor variables $\{x_{i,1}, x_{i,2}, \ldots, x_{i,n}\}$ with $n < m$.
The linear regression model posits that
\[
y_i = \beta_0 + \beta_1 x_{i,1} + \beta_2 x_{i,2} + \cdots + \beta_n x_{i,n} + \epsilon_i
\]
for $i = 1, 2, \ldots, m$.
The real numbers $\beta_0,\ldots,\beta_n$ are known as the parameters of the model, and the $\epsilon_i$ are independent, normally-distributed error terms.
The goal of linear regression is to calculate the parameters that best fit the data.
This can be accomplished by posing the problem in terms of linear least squares.
Define
\[
\b = \left[\begin{array}{c}y_1 \\ \vdots \\ y_m\end{array}\right],
\quad
A =
\left[\begin{array}{ccccc}
1 & x_{1,1} & x_{1,2} & \cdots & x_{1,n}\\
1 & x_{2,1} & x_{2,2} & \cdots & x_{2,n}\\
\vdots & \vdots & \vdots & \ddots & \vdots\\
1 & x_{m,1} & x_{m,2} & \cdots & x_{m,n}
\end{array}\right],
\quad
\x = \left[\begin{array}{c}
    \beta_0 \\ \beta_1 \\ \vdots \\ \beta_n
\end{array}\right].
\]
The solution $\x^* = [\beta_0^*, \beta_1^*, \ldots, \beta_n^*]\trp$ to the system $A\trp A\x = A\trp\b$ gives the parameters that best fit the data.
These values can be understood as defining the hyperplane that best fits the data.

\begin{figure}[H]
\centering
\includegraphics[width=.7\textwidth]{figures/linregression.pdf}
\caption{Solving the linear regression problem results in a best-fit hyperplane.}
\label{fig:linregression}
\end{figure}

\begin{problem}
Using your function from Problem \ref{prob:gradientmethods-linear-cg}, solve the linear regression problem specified by the data contained in the file\footnote{Source: Statistical Reference Datasets website at \url{http://www.itl.nist.gov/div898/strd/lls/data/LINKS/v-Longley.shtml}.}
\texttt{linregression.txt}.
This is a whitespace-delimited text file formatted so that the $i$-th row consists of $y_i, x_{i,1}, \ldots, x_{i,n}$.
Use \li{np.loadtxt()} to load in the data and return the solution to the normal equations.
\end{problem}

\subsection*{Logistic Regression} % -------------------------------------------

\emph{Logistic regression} is another important technique in statistical analysis and machine learning that builds off of the concepts of linear regression.
As in linear regression, there is a set of predictor variables $\{x_{i, 1}, x_{i, 2}, \dots, x_{i, n}\}_{i = 1}^{m}$ with corresponding outcome variables $\{y_i\}_{i = 1}^{m}$.
In logistic regression, the outcome variables $y_i$ are binary and can be modeled by a \emph{sigmoidal} relationship.
The value of the predicted $y_i$ can be thought of as the probability that $y_i = 1$.
In mathematical terms,
\[
\mathbb{P}(y_i = 1 \, | \, x_{i,1}, \dots, x_{i,n}) = p_i,
\]
where
\[
p_i = \frac{1}{1+\exp(-(\beta_0 + \beta_1x_{i,1} + \dots + \beta_nx_{i,n}))}.
\]
The parameters of the model are the real numbers $\beta_0, \beta_1,\dots, \beta_n$.
Note that $p_i \in (0, 1)$ regardless of the values of the predictor variables and parameters.

The probability of observing the outcome variables $y_i$ under this model, assuming they are independent, is given by
the \emph{likelihood function} $\mathcal{L}:\mathbb{R}^{n+1} \rightarrow \mathbb{R}$
\[
\mathcal{L}(\beta_0, \dots, \beta_n) = \prod_{i=1}^m p_i^{y_i}(1-p_i)^{1-y_i}.
\]
The goal of logistic regression is to find the parameters $\beta_0, \dots, \beta_k$ that maximize this likelihood function.
Thus, the problem can be written as:
\[
\max_{(\beta_0,\dots,\beta_n)}\mathcal{L}(\beta_0, \dots, \beta_n).
\]

Maximizing this function is often a numerically unstable calculation.
Thus, to make the objective function more suitable, the logarithm of the objective function may be maximized because the logarithmic function is strictly monotone increasing.
Taking the $\log$ and turning the problem into a minimization problem, the final problem is formulated as:
\[
\min_{(\beta_0,\dots,\beta_n)} - \log\mathcal{L}(\beta_0, \dots, \beta_n).
\]

A few lines of calculation reveal that this objective function can also be rewritten as
\begin{align*}
-\log\mathcal{L}(\beta_0,\dots,\beta_n) = &\sum_{i=1}^{m}\log(1+\exp(-(\beta_0 + \beta_1x_{i,1} + \dots +\beta_nx_{i,n}))) +\\
 &\sum_{i=1}^m (1- y_i)(\beta_0 + \beta_1x_{i,1} + \dots + \beta_nx_{i,n}).
\end{align*}

The values for the parameters  $\{\beta_i\}_{i = 1}^{n}$ that we obtain are known as the \emph{maximum likelihood estimate} (MLE).
To find the MLE, conjugate gradient can be used to minimize the objective function.

For a one-dimensional binary logistic regression problem, we have predictor data $\{x_i\}_{i=1}^m$ with labels $\{y_i\}_{i=1}^m$ where each $y_i \in \{0, 1\}$.
The negative log likelihood then becomes the following.
\begin{equation}
-\log\mathcal{L}(\beta_0, \beta_1) = \sum_{i=1}^m \log(1 + e^{-(\beta_0 + \beta_1 x_i)}) + (1 - y_i)(\beta_0 + \beta_1 x_i)
\label{eq:gradientmethods-negative-log-likelihood}
\end{equation}

\begin{problem}
Write a class for doing binary logistic regression in one dimension that implement the following methods.
\begin{enumerate}
\item \li{fit()}: accept an array $\x\in\mathbb{R}^n$ of data, an array $\y\in\mathbb{R}^n$ of labels ($0$s and $1$s), and an initial guess $\boldsymbol{\beta}_0\in\mathbb{R}^2$.
Define the negative log likelihood function as given in \eqref{eq:gradientmethods-negative-log-likelihood}, then minimize it (with respect to $\boldsymbol{\beta}$) with your function from Problem \ref{prob:gradientdescent-nonlinear-cg} or \li{opt.fmin_cg()}.
Store the resulting parameters $\beta_0$ and $\beta_1$ as attributes.

\item \li{predict()}: accept a float $x\in\mathbb{R}$ and calculate
\[\sigma(x) = \frac{1}{1 + \exp(- (\beta_0 + \beta_1 x))},\]
where $\beta_0$ and $\beta_1$ are the optimal values calculated in \li{fit()}.
The value $\sigma(x)$ is the probability that the observation $x$ should be assigned the label $y=1$.
\end{enumerate}
This class does not need an explicit constructor.
You may assume that \li{predict()} will be called after \li{fit()}.
\label{prob:gradientmethods-logistic-regression}
\end{problem}

\begin{problem}
On January 28, 1986, less than two minutes into the Challenger space shuttle's 10th mission, there was a large explosion that originated from the spacecraft, killing all seven crew members and destroying the shuttle.
The investigation that followed concluded that the malfunction was caused by damage to O-rings that are used as seals for parts of the rocket engines.
There were 24 space shuttle missions before this disaster, some of which had noted some O-ring damage.
Given the data, could this disaster have been predicted?

The file \texttt{challenger.npy} contains data for 23 missions (during one of the 24 missions, the engine was lost at sea).
The first column ($\x$) contains the ambient temperature, in Fahrenheit, of the shuttle launch.
The second column ($\y$) contains a binary indicator of the presence of O-ring damage (1 if O-ring damage was present, 0 otherwise).

Instantiate your class from Problem \ref{prob:gradientmethods-logistic-regression} and fit it to the data, using an initial guess of $\boldsymbol{\beta}_0 = [20, -1]\trp$.
Plot the resulting curve $\sigma(x)$ for $x \in [30, 100]$, along with the raw data.
Return the predicted probability (according to this model) of O-ring damage on the day the shuttle was launched, given that it was $31^\circ$F.

\begin{figure}[H]
\centering
    \includegraphics[width=.7\textwidth]{figures/logreg.pdf}
    % \caption{The logistic curve models the probability of O-ring damage on the Challenger shuttle. According to this model, given that the temperature was $31^\circ$ on the day of launch, the shuttle had close to $100\%$ likelihood of O-ring damage. This model had an initial guess of $1.$ and $-1$ for $\beta_0$ and $\beta_1$ respectively.}
    \label{fig:logistic_curve}
\end{figure}
\end{problem}


% !TeX spellcheck = en_GB
%\documentclass[12pt,a4paper]{article}
%\usepackage{import}
%\subimport{../}{uebung.tex}

%\toggletrue{showSolution}

%\title{\thetitle{}\\Tutorial 2}
%\date{\WeeksAfter{8}}

%\begin{document}

% \maketitle

\BoSSSopen{tutorial2/uebung2tutorial}
\graphicspath{{tutorial2/uebung2tutorial.texbatch/}}

\BoSSScmd{
/// \section*{What's new}
///
/// \begin{itemize}
/// \item Creating numerical grids
/// \item Projecting functions onto the DG space and further evaluation
/// \item Performing a hp-convergence study
/// \end{itemize}
///
/// \section*{Prerequisites}
/// No \BoSSS{} specific prerequisites are needed to complete this tutorial.
///
/// \section{Problem statement}
/// First, we define two functions: $g_1$ is continuous, $g_2$ has a discontinuity at $\vec{X} = \pi$ in the first derivative
/// \begin{align*}
/// g_1 =& \sin(\vec{X}), \\
/// g_2 =& \vert(\sin(\vec{X})\vert\, .
/// \end{align*}
/// The function argument is a vector $\vec{X}$, consisting only of one entry since we are working in a one dimensional space. \BoSSS{} however supports 1D, 2D and 3D, so the spatial coordinate is a general vector.
///
/// \section{Solution within the BoSSS framework}
///
/// To initialize, we have to call \code{restart}:\textsl{}
 }
\BoSSSexeSilent
\BoSSScmd{
restart;
 }
\BoSSSexeSilent
\BoSSScmd{
using NUnit.Framework;
 }
\BoSSSexe
\BoSSScmd{
/// \subsection{Plotting the functions} First, we plot the functions that are defined above over the interval $(0 ,2 \pi)$ with
/// 1000 sampling points using a Gnuplot-object.
/// %=======================================================================================
 }
\BoSSSexe
\BoSSScmd{
Func<double[],double> g1 = (X => Math.Sin(X[0]));          \newline
// continuous, smooth         \newline
Func<double[],double> g2 = (X => Math.Abs(Math.Sin(X[0])));          \newline
// continuous, non-smooth
 }
\BoSSSexe
\BoSSScmd{
/// We define equidistant sampling points...
double[] x = GenericBlas.Linspace(0, 2.0*Math.PI, 1000);
 }
\BoSSSexe
\BoSSScmd{
/// ...and compute the function values. In the loop, we have to convert the scalar \code{x[i]} into an array
/// with one element, since \code{g1} has to be feed with arrays.
double[] g1\_values = new double[x.Length];\newline
for(int i = 0; i < x.Length; i++) \{\newline
\btab g1\_values[i] = g1(new[] \{ x[i]\} );\newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// Instead of loops, we can also use Linq-functions:
double[] g2\_values = x.Select(x => g2(new []\{ x \})).ToArray();
 }
\BoSSSexe
\BoSSScmd{
/// For now, we are using the simple plotting interface, which supports
/// Matlab-Style format specifiers and color names. (More advanced plots
/// can be produced with \code{Plot2Ddata} and/or \code{Gnuplot} classes)
Plot(X1:x, Y1:g1\_values, Name1:"function g1", Format1:"--red",\newline
\btab  X2:x, Y2:g2\_values, Name2:"function g2", Format2:"-.blue");
 }
\BoSSSexe
\BoSSScmd{
/// Next, we create a grid which has a cell boundary exactly at the position of
/// the discontinuity of \code{g2}.
/// %=========================================================================================
 }
\BoSSSexe
\BoSSScmd{
var Nodes1 = new double[] \{0, 2, Math.PI, 4.5, 2*Math.PI \};         \newline
var Grid1 = Grid1D.LineGrid(Nodes1);
 }
\BoSSSexe
\BoSSScmd{
/// We can get the total number of cells by using the following command:
 }
\BoSSSexe
\BoSSScmd{
Grid1.NumberOfCells;
 }
\BoSSSexe
\BoSSScmd{
/// The recently created grid-object is not directly usable because it contains only the nodes of the grid.
/// We have to create a \code{GridData}-object which provides all necessary transformation metrics, etc. .
/// %=======================================================================================
 }
\BoSSSexe
\BoSSScmd{
var gdata1 = new GridData(Grid1);
 }
\BoSSSexe
\BoSSScmd{
/// \subsection{Projection onto the DG space}
/// At this point, we are able to create the so-called \emph{DG fields} to approximate \code{g1}
/// on \code{grid1}. Therefore, we project \code{g1} onto \code{grid1} using polynomial orders
/// of $p=2$ and $p=8$.
/// % =======================================================================================
 }
\BoSSSexe
\BoSSScmd{
var g1\_grid1\_p2 = new SinglePhaseField(new Basis(gdata1, 2), "g1 with p2 at Grid 1");      \newline
g1\_grid1\_p2.ProjectField(g1);
 }
\BoSSSexe
\BoSSScmd{
var g1\_grid1\_p8 = new SinglePhaseField(new Basis(gdata1, 8), "g1 with p8 at Grid 1");         \newline
g1\_grid1\_p8.ProjectField(g1);
 }
\BoSSSexe
\BoSSScmd{
/// Now, let us plot the projected solution for $p=2$.
/// By using the upsampling parameter, we can determine
/// the amount of sampling points per cell.
/// %=======================================================================================
 }
\BoSSSexe
\BoSSScmd{
var upsampling = 20;
 }
\BoSSSexe
\BoSSScmd{
var gp1 = new Gnuplot();
 }
\BoSSSexe
\BoSSScmd{
gp1.PlotField(g1\_grid1\_p2,          \newline
\btab new PlotFormat(lineColor: (LineColors)(1)),\newline
\btab upsampling);
 }
\BoSSSexe
\BoSSScmd{
gp1.PlotNow(); // shows the plot
 }
\BoSSSexe
\BoSSScmd{
/// \subsection{Computing the $L^2$-error}
/// Next, we learn how to compute the $L^2$-error for both
/// approximations of \code{g1} with different polynomial degrees:
/// %=======================================================================================
 }
\BoSSSexe
\BoSSScmd{
g1\_grid1\_p2.L2Error(g1);
 }
\BoSSSexe
\BoSSScmd{
g1\_grid1\_p8.L2Error(g1);
 }
\BoSSSexe
\BoSSScmd{
/// \subsection{Plotting the point-wise error}
/// Now, we plot the point-wise error for the approximation of \code{g1}
/// on \code{grid1} with a polynomial degree of 8.
/// %=======================================================================================
 }
\BoSSSexe
\BoSSScmd{
int K = 20; // number of points per cell         \newline
var gp2 = new Gnuplot();         \newline
gp2.PlotLogError(g1\_grid1\_p8, g1, "g1 with p8 at Grid 1", 20,          \newline
\btab new PlotFormat(lineColor: (LineColors)(1)));         \newline
gp2.PlotNow();
 }
\BoSSSexe
\BoSSScmd{
/// \subsection{Decay behavior of the DG modes for smooth and non-smooth functions}
/// We investigate the decay behavior of the DG modes for smooth and non-smooth
/// functions. For this purpose, we create a second grid which has the
/// discontinuity of \code{g2} within a cell and project \code{g2} onto this grid
/// like mentioned above.
/// %=======================================================================================
 }
\BoSSSexe
\BoSSScmd{
var Nodes2      = new double[] \{0, 2, 4.5, 2*Math.PI \};         \newline
var Grid2       = Grid1D.LineGrid(Nodes2);         \newline
var gdata2      = new GridData(Grid2);         \newline
var g2\_grid2\_p8 = new SinglePhaseField(new Basis(gdata2, 8), "g2\_p8 at Grid2");         \newline
g2\_grid2\_p8.ProjectField(g2);
 }
\BoSSSexe
\BoSSScmd{
/// The cell coordinates can be extracted by using the \code{Coordinates} parameter.
 }
\BoSSSexe
\BoSSScmd{
double[] cell1 = g2\_grid2\_p8.Coordinates.GetRow(1);          \newline
\btab // coord. in cell 1 (with kink)
 }
\BoSSSexe
\BoSSScmd{
double[] cell0 = g2\_grid2\_p8.Coordinates.GetRow(0);          \newline
\btab // coord. in cell 0 (smooth)
 }
\BoSSSexe
\BoSSScmd{
double[] cell2 = g2\_grid2\_p8.Coordinates.GetRow(2);          \newline
\btab // coord. in cell 2 (smooth)
 }
\BoSSSexe
\BoSSScmd{
/// Only the absolute value shall be plotted. We use a for-loop to replace the data in
/// \code{cell0}, \code{cell1} and \code{cell2} by their absolute values.
/// %=======================================================================================
 }
\BoSSSexe
\BoSSScmd{
for(int i = 0; i < cell0.Length; i++) \{         \newline
\btab cell0[i] = Math.Abs(cell0[i]);         \newline
\btab cell1[i] = Math.Abs(cell1[i]);         \newline
\btab cell2[i] = Math.Abs(cell2[i]);         \newline
 \}
 }
\BoSSSexe
\BoSSScmd{
Plot(X1:null, Y1:cell1, Name1:"disc. cell", Format1:"*-magenta",\newline
\btab  X2:null, Y2:cell0, Name2:"cell0",      Format2:"o-blue",\newline
\btab  X3:null, Y3:cell2, Name3:"cell2",      Format3:"o-red");
 }
\BoSSSexe
\BoSSScmd{
/// Note: Using a shortcut for the for-loop above, the absolute values in cell 0
/// can also be stored using the following command:
 }
\BoSSSexe
\BoSSScmd{
double[] cell0 = g2\_grid2\_p8.Coordinates.GetRow(0)         \newline
\btab .Select(d => Math.Abs(d)).ToArray();
 }
\BoSSSexe
\BoSSScmd{
/// Now, we would like to plot the logarithm (use \code{Math.Log10(...)}) of the absolute
/// values of the DG coordinates.
 }
\BoSSSexe
\BoSSScmd{
Plot(X1:null, Y1:cell1, Name1:"disc. cell", Format1:"*-magenta",\newline
\btab  X2:null, Y2:cell0, Name2:"cell0",      Format2:"o-blue",\newline
\btab  X3:null, Y3:cell2, Name3:"cell2",      Format3:"o-red",\newline
\btab  logY:true);
 }
\BoSSSexe
\BoSSScmd{
///\subsection{Convergence study}
 }
\BoSSSexe
\BoSSScmd{
/// In this section, we learn how to perform a convergence study for \code{g2}
/// for two different sequences of grid resolutions and different polynomial
/// orders. Therefore, we define two different sequences of grid resolutions:
/// %=======================================================================================
 }
\BoSSSexe
\BoSSScmd{
int[][] ResSeq = new int[2][];
 }
\BoSSSexe
\BoSSScmd{
/// Grid resolutions so that the kink in \code{g2} is located at a cell boundary:
ResSeq[0] = new int[] \{ 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048 \};
 }
\BoSSSexe
\BoSSScmd{
/// Grid resolutions so that the kink in \code{g2} is located within a cell:
ResSeq[1] = new int[] \{ 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047 \};
 }
\BoSSSexe
\BoSSScmd{
/// We save our errors into a multidimensional array by looping over
/// \begin{enumerate}
/// \item the resolution sequence
/// \item the polynomial order
/// \item the resolution
/// \end{enumerate}
var Errors = MultidimensionalArray.Create(2, 5, ResSeq[0].Length);
 }
\BoSSSexe
\BoSSScmd{
for(int i = 0; i < 2; i++) \{ // loop over the resolution sequence         \newline
\btab for(int p = 0; p <= 4; p++) \{ // loop over polynomial orders         \newline
\btab \btab for(int k = 0; k < ResSeq[i].Length; k++) \{ // loop over different resolutions        \newline
 \newline
\btab \btab \btab Console.Write("polynomial order \{1\}"+         \newline
\btab \btab \btab ",\textbackslash tResolution \{0\}... ", ResSeq[i][k], p);         \newline
 \newline
\btab \btab \btab var grid  = Grid1D.LineGrid(GenericBlas.Linspace(0,         \newline
\btab \btab \btab 2.0*Math.PI, ResSeq[i][k] + 1));           \newline
\btab \btab \btab \btab  // number of nodes == number of cells + 1         \newline
 \newline
\btab \btab \btab var gData = new GridData(grid);         \newline
 \newline
\btab \btab \btab var g2\_h  = new SinglePhaseField(new Basis(gData, p));         \newline
 \newline
\btab \btab \btab g2\_h.ProjectField(g2);         \newline
 \newline
\btab \btab \btab Errors[i,p,k] = g2\_h.L2Error(g2);         \newline
 \newline
\btab \btab \btab Console.WriteLine("\textbackslash tdone: L2 error is \{0:0.###e-00\}.", Errors[i,p,k]);         \newline
\btab \btab \}         \newline
\btab \}         \newline
 \}
 }
\BoSSSexe
\BoSSScmdSilent{
/// NUnit test (few random tests) BoSSScmdSilent
Assert.LessOrEqual(Errors[1,4,9],8E-06);       \newline
Assert.LessOrEqual(Errors[1,3,9],7.5E-06);       \newline
Assert.LessOrEqual(Errors[1,2,9],2E-05);       \newline
Assert.LessOrEqual(Errors[1,1,9],2E-05);       \newline
Assert.LessOrEqual(Errors[0,3,9],1E-12);       \newline
Assert.LessOrEqual(Errors[0,3,9],1E-12);       \newline
Assert.LessOrEqual(Errors[1,4,0],0.25);       \newline
Assert.LessOrEqual(Errors[0,0,0],0.2);       \newline
Assert.LessOrEqual(Errors[0,3,0],1E-03);
 }
\BoSSSexe
\BoSSScmd{
/// We plot the error for the grids which have the kink at the cell boundary,
/// there we reach spectral convergence:
 }
\BoSSSexe
\BoSSScmd{
var hValues = ResSeq[0].Select(J => Math.PI*2.0/J);\newline
Plot(X1:hValues, Y1:Errors.ExtractSubArrayShallow(0,0,-1).To1DArray(),\newline
\btab  Name1:"grid1,p0", Format1:"o-red",\newline
\btab  X2:hValues, Y2:Errors.ExtractSubArrayShallow(0,1,-1).To1DArray(),\newline
\btab  Name2:"grid1,p0", Format2:"o-blue",\newline
\btab  X3:hValues, Y3:Errors.ExtractSubArrayShallow(0,2,-1).To1DArray(),\newline
\btab  Name3:"grid1,p0", Format3:"o-green",\newline
\btab  X4:hValues, Y4:Errors.ExtractSubArrayShallow(0,3,-1).To1DArray(),\newline
\btab  Name4:"grid1,p0", Format4:"o-magenta",\newline
\btab  X5:hValues, Y5:Errors.ExtractSubArrayShallow(0,4,-1).To1DArray(),\newline
\btab  Name5:"grid1,p0", Format5:"o-orange",\newline
\btab  logX:true, logY:true);
 }
\BoSSSexe
\BoSSScmd{
/// Now we plot the error for the grids which have the kink within a cell;
/// due to the low regularity, the convergence of the DG method
/// degenerates:
 }
\BoSSSexe
\BoSSScmd{
var hValues = ResSeq[0].Select(J => Math.PI*2.0/J);\newline
Plot(X1:hValues, Y1:Errors.ExtractSubArrayShallow(1,0,-1).To1DArray(),\newline
\btab  Name1:"grid1,p0", Format1:"o-red",\newline
\btab  X2:hValues, Y2:Errors.ExtractSubArrayShallow(1,1,-1).To1DArray(),\newline
\btab  Name2:"grid1,p0", Format2:"o-blue",\newline
\btab  X3:hValues, Y3:Errors.ExtractSubArrayShallow(1,2,-1).To1DArray(),\newline
\btab  Name3:"grid1,p0", Format3:"o-green",\newline
\btab  X4:hValues, Y4:Errors.ExtractSubArrayShallow(1,3,-1).To1DArray(),\newline
\btab  Name4:"grid1,p0", Format4:"o-magenta",\newline
\btab  X5:hValues, Y5:Errors.ExtractSubArrayShallow(1,4,-1).To1DArray(),\newline
\btab  Name5:"grid1,p0", Format5:"o-orange",\newline
\btab  logX:true, logY:true);
 }
\BoSSSexe
\BoSSScmd{
/// \section{Advanced topics} This tutorial addressed the very basics of setting up a \BoSSS{}~application, namely grid instantiation, the $L^2$-projection of functions onto the DG space and performing a spatial convergence study. Where do you go from here? We recommend that you continue with other relevant basics as provided in the tutorials dealing with the creation of a spatial operator, explicit time integration and the implementation of numerical fluxes.
 }
\BoSSSexe


\section{Mathematical Model}
\label{sec:model}

In this section we give a summary of the mathematical model.

\subsection{Boltzmann Equation}

We consider approximate solutions to the Boltzmann equation for the transport of massless particles through a static material in Cartesian geometry, which, after scaling to dimensionless units, can be written as
\begin{equation}
  \pd{f}{t}+\vect{\ell}\cdot\nabla f
  =\f{1}{\tau}\,\cC(f),
  \label{eq:boltzmann}
\end{equation}
where the distribution function $f\colon(\omega,\varepsilon,\vect{x},t)\in\bbS^{2}\times\bbR^{+}\times\bbR^{3}\times\bbR^{+}\to\bbR^{+}$ gives the number of particles propagating in the direction $\omega\in\bbS^{2}:=\{\,\omega=(\thetaNu,\phiNu)~|~\thetaNu\in[0,\pi],\phiNu\in[0,2\pi)\,\}$, with energy $\varepsilon\in\bbR^{+}$, at position $\vect{x}\in\bbR^{3}$ and time $t\in\bbR^{+}$.
Here we use spherical momentum space coordinates $(\varepsilon,\omega)$, and the unit vector $\vect{\ell}(\omega)\in\bbR^{3}$ (independent of $\varepsilon$ and $\vect{x}$) is parallel to the particle three-momentum $\vect{p}=\varepsilon\,\vect{\ell}$.
We also define the energy-position coordinates $\vect{z}:=\{\varepsilon,\vect{x}\}\in\bbR^{+}\times\bbR^{3}$.
On the right-hand side of Eq.~\eqref{eq:boltzmann}, $\tau$ is the ratio of the particle mean-free path (due to interactions with a background) to some characteristic length scale of the problem.
In opaque regions, $\tau\ll1$, while for free streaming particles, $\tau\gg1$.
The collision operator, which models emission, absorption, and isotropic and elastic scattering, is given by
\begin{equation}
  \cC(f)=\xi\,\big(\,f_{0}-f\,\big)
  +(1-\xi)\,\big(\,\f{1}{4\pi}\int_{\bbS^{2}}f\,d\omega-f\,\big),
  \label{eq:collisionTerm}
\end{equation}
where $\xi=\sigma_{\Ab}/\sigma_{\Tot}\in[0,1]$ is the ratio of the absorption opacity $\sigma_{\Ab}\,(\ge0)$ to the total opacity $\sigma_{\Tot}=\sigma_{\Ab}+\sigma_{\Scatt}$, and $\sigma_{\Scatt}\,(\ge0)$ is the scattering opacity.
In particular, $\xi=1$ models pure emission and absorption, while $\xi=0$ models pure scattering.
In general, $\sigma_{\Ab}$ and $\sigma_{\Scatt}$ (and $\tau$ and $\xi$) depend on $\vect{z}$.
The equilibrium distribution function is denoted by $f_{0}(\vect{z})$.
Here, we consider transport of Fermions (e.g., neutrinos), so the equilibrium distribution function takes the form
\begin{equation}
  f_{0}(\vect{z})=\f{1}{e^{(\varepsilon-\mu(\vect{x}))/T(\vect{x})}+1},
  \label{eq:fermiDirac}
\end{equation}
where the temperature $T$ and the chemical potential $\mu$ depend on properties of the background.

\subsection{Angular Moment Equations: Two-Moment Model}

The Boltzmann equation is often too expensive to solve directly.
Instead, approximate equations for angular moments of the distribution function are solved.
To this end, we define the angular moments of the distribution function
\begin{equation}
  \big\{\,\cJ,\vect{\cH},\vect{\cK}\,\big\}(\vect{z},t)
  =\f{1}{4\pi}\int_{\bbS^{2}}f(\omega,\vect{z},t)\,\{\,1,\vect{\ell},\vect{\ell}\otimes\vect{\ell}\,\}\,d\omega.
  \label{eq:angularMoments}
\end{equation}
We refer to $\cJ$ (zeroth moment) as the particle density, $\vect{\cH}$ (first moment) as the particle flux, and $\vect{\cK}$ (second moment) as the stress tensor.
Note that the moments defined in Eq.~\eqref{eq:angularMoments} are \emph{spectral moments} (depending on energy as well as position and time).
The \emph{grey moments} (depending only on position and time) are obtained by integration over energy:
\begin{equation}
  \big\{\,J,\vect{H},\vect{K}\,\big\}(\vect{x},t)
  =\int_{\bbR^{+}}\big\{\,\cJ,\vect{\cH},\vect{\cK}\,\big\}(\varepsilon,\vect{x},t)\,\varepsilon^{2}d\varepsilon.
\end{equation}

Taking the zeroth and first moments of Eq.~\eqref{eq:boltzmann} gives the two-moment model, comprising a system of conservation laws with sources
\begin{equation}
  \pd{\vect{\cM}}{t}+\nabla\cdot\vect{\cF}=\f{1}{\tau}\,\vect{\cC}(\vect{\cM}),
  \label{eq:momentEquations}
\end{equation}
where $\vect{\cM}=(\cJ,\vect{\cH})^{T}$ and $\vect{\cF}=(\vect{\cH},\vect{\cK})^{T}$.
Components of the fluxes in each coordinate direction are $\vect{\cF}^{i}=\vect{e}_{i}\cdot\vect{\cF}=(\vect{e}_{i}\cdot\vect{\cH},\vect{e}_{i}\cdot\vect{\cK})^{T}$, where $\vect{e}_{i}$ is the unit vector parallel to the $i$th coordinate direction.
On the right-hand side of Eq.~\eqref{eq:momentEquations}, the source term is
\begin{equation}
  \vect{\cC}(\vect{\cM})=\vect{\eta}-\vect{\cD}\,\vect{\cM},
  \label{eq:collisionTermMoments}
\end{equation}
where $\vect{\eta}=(\xi\,f_{0},\vect{0})^{T}$ and $\vect{\cD}=\mbox{diag}(\xi,\vect{I})$, with $\vect{I}$ the identity matrix.

In order to close the system given by Eq.~\eqref{eq:momentEquations}, the components of the stress tensor $\vect{\cK}$ must be related to the lower moments through a closure procedure.
To this end, Levermore \cite{levermore_1984} defined the Eddington tensor $\vect{k}=\vect{\cK}/\cJ$ and assumed that the radiation field is symmetric about a preferred direction $\widehat{\vect{h}}=\vect{\cH}/|\vect{\cH}|$ so that
\begin{equation}
  \vect{k}=\f{1}{2}\big[\,\big(1-\chi\big)\,\vect{I}+\big(3\,\chi-1\big)\,\widehat{\vect{h}}\otimes\widehat{\vect{h}}\,\big],
  \label{eq:eddingtonTensor}
\end{equation}
where $\chi=\chi(\cJ,|\vect{\cH}|)$ is the Eddington factor.
The two-moment model is then closed once the Eddington factor is determined from $\cJ$ and $\vect{\cH}$.
We will return to the issue of determining the Eddington factor in Section~\ref{sec:algebraicClosure}.


\chapter{Bayes' Theorem}

Let's say that that I am holding a bag of marbles in each of my hands.
You know that one bag contains 60 white marbles and 40 red marbles. You
know that the other holds 10 white marbles and 90 red marbles. You
don't know which is which -- you can't see the marbles.

I say ``Guess which bag is mostly red marbles.'' You pick one.

Holding out choosen bag, I ask ``What is the probability
that this is the bag that is mostly red marbles?'' You answer ``That
is the mostly-red-marbles bag with a probability of 0.5.  There is
also a 0.5 probability that it is the mostly-white-marbles bag.''

Then I let you pick one marble from the bag.  It is red. Now you must
update your beliefs. Clearly, it is more likely that this is the
mostly-red-marbles bag. What is the probability now?

Bayes Theorem gives you the rule for updating your beliefs based on
new data.

\section{Bayes Theorem}

Let's say you have two events or conditions $C$ and $D$.  Maybe $C$ is
``The person has a cough'' and $D$ is ``The person is waiting to see a doctor.''

Using the chain rule of probability, we now have two ways to calculate $p(C \text{ AND } D)$:

$$p(C \text{ AND } D) = p(C | D) p(D)$$

(The probability the person is at the doctor times the probability they have a cough if they are at the doctor.)

or

$$p(C \text{ AND } D) = p(C | D) p(D)$$

(The probability the person has a cough times times the probability they are at the doctor if they have a cough.)

Thus:

$$p(D | C) = \frac {p(C | D)p(D)}{P(C)}$$

Now you can calculate $p(D | C)$ (in this case, the probability that
you are waiting to see a doctor given that you have a cough.) if you
know:

\begin{itemize}
\item $p(C | D)$ (The probability that you have a cough given that you are waiting to see a doctor)
\item $p(D)$ (The probabilty that you are waiting for a doctor for any reason.)
\item $p(C)$ (The probability that you have a cough anywhere)
\end{itemize}

Pretty much all modern statistical methods (including most artificial
intelligence) are based on this formula, which is known as Bayes'
Theorem.  It was written down by Thomas Bayes before he died in
1761. It was found and published after his death.

\section{Using Bayes' Theorem}

Back to the example at the beginning. To review:

\begin{itemize}
\item There are two bags that look exactly the same.
\item Bag W has 60 white marbles and 40 red marbles.
\item Bag R has 10 white marbles and 90 red marbles.
\item You pull one marble from the selected bag -- it is red.
\end{itemize}

What is the probabilty that the selected bag is Bag R? Intuitively,
you know that the probability is now more than 0.5. What is the exact
number?

In terms of conditional probability, we say we are looking for ``the probability
that the selected bag is Bag R, given that you drew a red marble?'' or
$p(B_R | D_R)$, where $B_R$ is ``The selected bag is Bag R'' and $D_R$ is
``You drew a red marble from the selected bag''.

From Bayes' Theorem, we can write:

$$p(B_R | D_R) = \frac{ P(D_R | B_R) P(B_R) } {P(D_R)}$$

$P(D_R | B_R)$ is just the probability of drawing a red marble given that the
selected bag is Bag R. That is easy to calculate: There are 100
marbles in the bag, and 90 are red.  Thus $P(D_R | B_R) = 0.9$.

$P(B_R)$ is just the probability that you chose Bag R before you drew
out a marble. Both bags look the same, so $P(B_R)= 0.5$.  This is
called \textit{the prior} because it represents what you thought the
probability was before you got more information.

$P(D_R)$ is the probabilty of drawing a red marble. There was 0.5
probability that you put your hand into Bag W (in which 40 of the 100
marbles are red) and a 0.5 probability that you put your hand into Bag
R (in which 90 of the 100 marbles are red).  So

$$P(D_R) = 0.5 \frac{40}{100} + 0.5 \frac{90}{100} = 0.65$$

Putting it together:

$$p(B_R | D_R) = \frac{ P(D_R | B_R) P(B_R) } {P(D_R)} = \frac{(0.9)(0.5)}{0.65} = \frac{9}{13} \approx 0.69$$

Thus, given that you have pulled a red marble, there is about a 69\% chance
that you have selected the bag with 90 red marbles.

\section{Confidence}

Bayes' Theorem, then, is about updating your beliefs based on
evidence.  Before you drew out the red marble, you selected one bag
thinking it might contain 90 red marbles. How certain were you? You
were 0.5 certain, where 0.0 is complete disbelief and 1.0 is complete
confidence.  After pulling out the red marble, you were about 0.69
confident that you had chosen the bag with 90 red marbles.

The question ``How confident are you in your guess?'' is very
important in some situations. For example in medicine, diagnoses often
lead to risky interventions. Few diagnoses come with 100\% confidence.
All doctors should know how to use Bayes' Theorem.

In a trial, a jury is asked to determine if the accused person is
guilty of a crime. Few jurors at 100\% certain. In some trials, Bayes'
Theorem is a really important tool.


\documentclass{article}
\usepackage{amsmath}
\usepackage[margin=1.0in]{geometry}
\usepackage{xcolor}

\begin{document}

\noindent
Does $\displaystyle \sum_{n=1}^\infty \frac{15n^4+4n^3+3n^2+2n+1}{3n^5+n^4+n^3+n^2+n+1}$
diverge, converge absolutely, or converge conditionally?

\subsection*{Solution}

The function $\displaystyle f(x)=\frac{15x^4+4x^3+3x^2+2x+1}{3x^5+x^4+x^3+x^2+x+1}$ is continuous, positive, and decreasing.

We will apply the Integral Test, so we integrate $f(x)$ using the substitution $u=3x^5+x^4+x^3+x^2+x+1$ to get
\begin{align*}
\int \frac{15x^4+4x^3+3x^2+2x+1}{3x^5+x^4+x^3+x^2+x+1}\,dx
&= \int \frac1u\,du\\
&= \ln|u|+C\\
&= \ln|3x^5+x^4+x^3+x^2+x+1|+C.
\end{align*}

Thus the definite, improper integral is
\begin{align*}
\int_1^\infty  \frac{15x^4+4x^3+3x^2+2x+1}{3x^5+x^4+x^3+x^2+x+1}\,dx
&= \lim_{t \to \infty} \int_1^t  \frac{15x^4+4x^3+3x^2+2x+1}{3x^5+x^4+x^3+x^2+x+1}\,dx \\
&= \lim_{t \to \infty} ( \ln|3t^5+t^4+t^3+t^2+t+1| -  \ln|8|)\\
&= \infty.
\end{align*}
Since the integral $\displaystyle \int_1^\infty f(x)\,dx$ diverges, by the Integral Test, the series $\displaystyle \sum_{n=1}^\infty \frac{15n^4+4n^3+3n^2+2n+1}{3n^5+n^4+n^3+n^2+n+1}$ diverges.

\subsection*{Comment}

This series can be studied using the Limit Comparison Test, and would be rather awful (but possible!) using the Direct Comparison Test. However, the Integral Test (above) is probably going to be much shorter!

\end{document}%%%%%%%%%%%%%%%%%

\begin{align*}
L&=\lim_{n \to \infty} \sqrt[n]{|a_n|}\\
&= \lim_{n \to \infty} \sqrt[n]{\left| \right|}\\
\end{align*}

\begin{align*}
L&=\lim_{n \to \infty} \left|\frac{a_{n+1}}{a_n}\right|\\
&= \lim_{n \to \infty} \left| \right|\\
\end{align*}

\begin{align*}
\lim_{n \to \infty} a_n
&= \lim_{n \to \infty} \\
\end{align*}

Since $\sum |a_n| = \sum a_n$, the series $\displaystyle \sum_{n=1}^\infty AAAAAAAAAAAAAA$ converges absolutely.

Since $|r| < 1$, the series ...  converges by the Geometric Series Test.

Since $|r| \geq 1$, the series ...  diverges by the Geometric Series Test.

The function $f(x)=\frac{}{}$ is continuous, positive, and decreasing on $[1,\infty)$.

\subsection*{Solution}