\documentclass[jou]{apa6}

\usepackage[american]{babel}

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

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

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

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

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

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

\setlength\parindent{0pt}

\title{Sample Quiz 4}
\author{Discrete Structures, Fall 2020}
\affiliation{RBS}

\leftheader{Discrete Sample Quiz 4}

\abstract{%
}

%\keywords{}

\begin{document}

%\thispagestyle{empty}

\twocolumn
\section{Midterm Preparation: Sample Problems}

{\footnotesize
Midterm may contain 3-5 computation problems \textendash{}
Part A, and also 1-2 analysis problems \textendash{} Part B and/or
1-2 proofs (see Part C). Expect around 8 problems in the midterm (it would be
about 15 minutes per problem).

This document is {\bf not} a mock exam; it contains $42$ sample questions
(many more than you would expect to get in actual midterm). This material
is meant for discussion in order to prepare and to show some variations of the
problems.

Proofs will closely follow some pattern that has been shown in a homework
exercise (a variant of an existing proof).
Also the computation and analysis tasks will follow a
recognizable pattern that is presented in this preparation material
or any of the quizzes or homeworks.

Besides the proofs, midterm can contain
procedural problems (applying algorithms in one
particular situation); analysis tasks (for example,
formalize something with predicates or quantifiers or apply some
other conceptual knowledge) and so on.
Even, if you are following a known procedure
you need to add comments to explain what you are doing.
They can earn you partial credit,
even if your result turns out incomplete.
}

% Computation example (15 + 15 + 15 + 15)
% Analysis of a situation (20 + 20)
% General proof (25 + 25)

\subsection{Part A. Computational Problems}

In this part we apply known algorithms to particular situations.
The goal is to obtain correct result and and add a short explanation, what
you did and why.

\subsubsection{A1. Propositional Logic}

{\bf Question 1.}
Consider this Boolean expression: $E = p \rightarrow q \rightarrow r$.
Implication ($\rightarrow$) is right-associative.
\begin{enumerate}[(a)]
\item Write an equivalent Boolean expression using only conjunctions ($\wedge$) and negations ($\neg$).
\item Fill in the missing parts in the truth table:\\
\begin{tabular}{ c | c | c | c }
$p$ & $q$ & $r$ & $E$ \\ \hline
{\tt T} & {\tt T} & {\tt T} & $\ldots$ \\ \hline
{\tt T} & {\tt T} & {\tt F} & $\ldots$ \\ \hline
{\tt T} & {\tt F} & {\tt T} & $\ldots$ \\ \hline
{\tt T} & {\tt F} & {\tt F} & $\ldots$ \\ \hline
{\tt F} & {\tt T} & {\tt T} & {\tt T} \\ \hline
{\tt F} & {\tt T} & {\tt F} & {\tt T} \\ \hline
{\tt F} & {\tt F} & {\tt T} & {\tt T} \\ \hline
{\tt F} & {\tt F} & {\tt F} & {\tt T} \\ \hline
\end{tabular}
\end{enumerate}

\vspace{6pt}
{\bf Question 2.}
Assume the following precedence order and associativity of the $5$ Boolean operations:

\begin{tabular}{lll}
{\tt \textasciitilde{}a} & 1st precedence & right-associative \\
{\tt a/\textbackslash{}b} & 2nd precedence & right-associative \\
{\tt a\textbackslash{}/b} & 3rd precedence & right-associative \\
{\tt a<->b} & 4th precedence & left-associative \\
{\tt a->b} & 5th precedence & right-associative
\end{tabular}

Show step-by-step how would you restore parentheses in the following
Boolean expression (each step adds one pair of parentheses \textendash{}
in the order that follows from the rules of precedence
and associativity):\\
{\tt a -> b /\textbackslash{} \textasciitilde{} c
\textbackslash{}/ \textasciitilde{} \textasciitilde{} d
<-> e -> f <-> g}

\vspace{6pt}
{\bf Question 3.} The diagram on Figure~\ref{fig:midterm-syntax-tree} shows different options how
$5$ people \textendash{} Bill, John, Jim, Simon and Mike \textendash{} can
work in a yard. (Variable {\tt Bill} is true iff Bill works in the yard,
and the whole expression evaluates to true, iff it shows a possible combination
how a team of boys can work in the yard).

\begin{figure}[!htb]
\center{\includegraphics[width=2in]{midterm-exam-preparation/midterm-syntax-tree.png}}
\caption{\label{fig:midterm-syntax-tree} Boolean expression as a tree.}
\end{figure}

\begin{enumerate}[(A)]
\item Write this tree as a Boolean expression using
Boolean connectives ($\neg$, $\wedge$, etc.).
\item Write the same expression and ensure that it does not
use any unnecessary parentheses (precedence and associativity is
same as in Question 2).
\item Write an equivalent expression using just two Boolean
connectives (negation $\neg$ and disjunction $\vee$).
\end{enumerate}

\subsubsection{A2. Sets and Quantifiers}

{\bf Question 4.}
The universe $U$ is all integers between $1$ and $600$.
$K_2 \subseteq U$ denotes all even numbers from $U$.
Similarly, $K_3 \subseteq U$ denotes all numbers divisible by $3$;
$K_5 \subseteq U$ denotes all numbers divisible by $5$.

\begin{enumerate}[(A)]
\item Draw Euler-Venn diagram with a big rectangle (the set $U$),
the subsets $K_2$, $K_3$, $K_5$ as three intersecting circles.
\item Shade the region that corresponds to the set
$(K_2 \cap \overline K_3) \cup K_5$.
\item Find the cardinality of the set
$\left| (K_2 \cap \overline K_3) \cup K_5 \right|$,
justify your answer.
\item Describe, which elements are in the set
$(K_2 \cap \overline K_3) \cup K_5$ in English.
For example: {\em ``All ... such that ... is divisible ...
and/or is not divisible...''}.
\end{enumerate}

\vspace{6pt}
{\bf Question 5.}
In Figure~\ref{fig:predicate-grid} a red square on the intersection
of row $i$ and column $j$
means that the predicate $L(i,j)$ is true (person $i$
loves person $j$). White square means that the predicate $L(i,j)$
is false. Here
$i,j \in \{ \mathtt{a},\mathtt{b},\mathtt{c},\mathtt{d},\mathtt{e} \}$.
\begin{figure}[!htb]
\center{\includegraphics[width=2in]{midterm-exam-preparation/predicate-grid.png}}
\caption{\label{fig:predicate-grid} Predicate on a Cartesian product.}
\end{figure}

Which statement is shown in the left picture?
\begin{enumerate}[(A)]
\item Everyone is loved by someone.
\item Eveyone loves someone.
\item Someone loves everyone.
\item Someone is loved by everyone.
\end{enumerate}
Which statement is shown in the right picture?
\begin{enumerate}[(A)]
\item $\forall x\, \exists y,\;L(y,x)$.
\item $\forall x\, \exists y,\;L(x,y)$.
\item $\exists x\, \forall y,\;L(x,y)$.
\item $\exists x\, \forall y,\;L(y,x)$.
\end{enumerate}

\subsubsection{A3. Algorithms and Big-O Notation}

In these problems you can verify the definition of the Big-O Notation
or find the limit $f(n)/g(n)$ as $n \rightarrow \infty$.

{\bf Question 6.} For each function find the smallest
$k$ such that $f(n)$ is in $O(n^k)$. Justify your answer.
\begin{enumerate}[(A)]
\item $f(n) = \sum_{j=1}^{n} (j^3 + j \log_2 j)$.
\item $f(n) = n^3 + \sin n^7$.
\item $f(n) = 1^2 + 2^2 + \ldots + n^2$.
\end{enumerate}

\vspace{6pt}
{\bf Question 7.} We say that the functions $f(n)$ and $g(n)$
{\em are of the same order}, if $f(n)$ is in $O(g(n))$ and
$g(n)$ is in $O(f(n))$. Find all pairs of functions in this
list that are of the same order:
$$n^2 + \log_2 n,\; 2^n + 3^n,\; 100n^3 + n^2,\; n^2 + 2^n,$$
$$n^2 + n^3,\;3n^3 + 2^n.$$

\subsubsection{A4. Number Theory}

{\bf Question 8.}
\begin{enumerate}[(A)]
\item
Somebody has written a long hexadecimal number
{\tt F0F0F0...F0F} \textendash{} it uses $28$ digits "F"
separated by $27$ digits "0".
Express the value of this number as a short expression
(without $\ldots$), using
the formula of a geometric progression: $\frac{b_1(q^{n+1}-1)}{q - 1}$.
\item
Somebody has written an infinite hexadecimal fraction
{\tt 0.F0F0F0\ldots}. Express it as a rational number in
decimal notation.
\end{enumerate}

\vspace{6pt}
{\bf Question 9.}

\begin{figure}[!htb]
\center{\includegraphics[width=3in]{midterm-exam-preparation/arithmetic-sequences.png}}
\caption{\label{fig:arithmetic-sequences} 3 arithmetic progressions.}
\end{figure}

\begin{enumerate}[(A)]
\item Find any number $M \in \mathbb{Z}^{+}$
that gives the following remainders when divided by
$5$, $7$ and $11$ (to ensure that in Figure~\ref{fig:arithmetic-sequences}
red circles denote multiples of these numbers).
$$\left\{
\begin{array}{l}
M \equiv 4\;(\text{mod}\,5)\\
M \equiv 0\;(\text{mod}\,7)\\
M \equiv 6\;(\text{mod}\,11)
\end{array} \right.$$
\item Find the arithmetic progression containing all such numbers $M$.
\end{enumerate}

\vspace{6pt}
{\bf Question 10.}
\begin{enumerate}[(A)]
\item Alice has $13$-cent coins; Bob has $21$-cent coins.
Alice wants to pay Bob exactly $1$ cent. How to do this?
\item Alice has $21$-cent coins; Bob has $13$-cent coins.
Alice wants to pay Bob exactly $1$ cent. How to do this?
\item Solve the congruence equation \textendash{} find
$x$ such that $13x \equiv 1\;(mod\;21)$.
\item Solve the congruence equation \textendash{} find
$x$ such that $13x \equiv 4\;(mod\;21)$.
\end{enumerate}

\vspace{6pt}
{\bf Question 11.}
\begin{enumerate}[(A)]
\item Write all positive powers of number $2$ ($2^1, 2^2, 2^3,\ldots$) modulo $11$ until
you find a loop: i.e. some remainder repeats itself. How long is the period?
\item Write all negative powers of number $2$ modulo $13$ ($2^{-1},2^{-2},2^{-3},\ldots$) until
you find a loop. How long is the period?
\end{enumerate}

{\em Note.} A negative power, for example $2^{-k}$ is such that $2^{-k}\cdot 2^k \equiv 1\;(\text{mod}\;11)$.

\subsection{Part B. Analysis Tasks}

In these problems you have to apply concepts
(such as predicates or quantifiers) to new situations,
describe algorithms or procedures for new tasks
or sort cases in adaptive ways.

\subsubsection{B1. Propositional Logic}

{\bf Question 12.}
Among the people $A,B,C$ one is a truth-teller,
the other two are liars.
Every person ($A$, $B$, and $C$) has a closed box
in front of himself/herself. Exactly one of the
boxes has a candy inside. $A,B,C$ know everything
about each other and the location of candy.\\
Find out, which YES/NO questions you
need to ask to find out, which box contains candy.
Ask as few questions as possible
(``brute-force'' strategies that ask clearly redundant
questions may only get partial credit).

\vspace{6pt}
{\bf Question 13.}
\begin{enumerate}[(a)]
\item Find out, if this formula is a tautology:
$$\mathtt{((A \vee B) \wedge (A \rightarrow C) \wedge
(B \rightarrow C)) \rightarrow C}.$$
\item Find out, if this formula is satisfiable:
$$\mathtt{\neg (((A \vee B) \wedge (A \rightarrow C) \wedge
(B \rightarrow C)) \rightarrow C)}.$$
\end{enumerate}

{\em Note.} A Boolean expression is called {\em tautology},
if it is true for all
possible truth values of its variables.
An expression is called {\em satisfiable},
if there is a way to assign
variables so that it becomes true.

\vspace{6pt}
{\bf Question 14.}
Let us have three people \textendash{} Ada, Barbara and Cecilia.
Every day some of them are charging batteries for Lego robots.
Let us have propositional variables $a,b,c$ (a variable $a,b,c$ is true, iff
today batteries were charged by Ada, Barbara or Cecilia respectively).

Write a Boolean expression telling that today batteries were charged
by {\bf exactly} two people out of the three. You can use variables $a,b,c$ and
all $5$ Boolean connectives ($\neg$, $\vee$, $\wedge$, $\rightarrow$,
$\leftrightarrow$).

\subsubsection{B2. Sets and Quantifiers}

{\bf Question 15.} We define the set of all bounded functions defined on the interval $(a;b)$
as functions that have all their values between two bounds: there is a positive number $M$
such that all all values of the function $f$ are in the interval $[-M;M]$. With predicates and quantifiers:
$$\exists M \in \mathbb{R}^{+},\,\forall x \in \mathbb{R}\,\;x \in (a;b) \rightarrow |f(x)| \leq M.$$
In Figure~\ref{fig:bounded-function} one of the functions (the red one) is bounded, the other one is unbounded.
\begin{figure}[!htb]
\center{\includegraphics[width=2in]{midterm-exam-preparation/bounded-function.png}}
\caption{\label{fig:bounded-function} A bounded function.}
\end{figure}

\begin{enumerate}[(A)]
\item Write an expression with predicates and quantifiers to express the statement:
``Function $f:(a;b) \rightarrow \mathbb{R}$ is {\bf not} bounded.''
\item Find an example of a function that is defined and bounded in the interval $(0;1)$.
\item Find an example of a function that is defined, but unbounded in the interval $(0;1)$.
\item What functions are described by another predicate logic expression, where we switch the order of quantifiers:
$$\forall x \in \mathbb{R}\,\exists M \in \mathbb{R}^{+},\;x \in (a;b) \rightarrow |f(x)| \leq M.$$
\end{enumerate}

\vspace{6pt}
{\bf Question 16.} An infinite sequence $a_0,a_1,a_2,\ldots$ is called {\em purely periodic}, if
all the members of it repeat the same finite pattern over and over again.
For example, $3,3,3,3,\ldots$ or $1,3,1,3,1,3,\ldots$ are purely periodic.
Meanwhile,
$1,6,6,6,6,\ldots$ or $1,4,1,4,2,1,3,5,\ldots$ (the digits of $\sqrt{2}$)
are not purely periodic (they are either ``eventually periodic'' with some digits preceding the period
or not periodic at all).
\begin{enumerate}[(A)]
\item Write an expression to tell that a given sequence $(a_n)_{n \in \mathbb{N}}$
is purely periodic.
\item Write an expression to tell that a given sequence $(a_n)_{n \in \mathbb{N}}$
is {\bf NOT} purely periodic.
\end{enumerate}

\vspace{6pt}
{\bf Question 17.}
Consider the set $A = \{ 1,2,3,4,6,12 \}$. We define a
predicate
$$P \,:\, A \times A \rightarrow \{ \mathtt{True}, \mathtt{False} \},$$
where $P(i,j)$ is true iff the divisibility by $i$ implies
divisibility by $j$ (for any $i,j \in A$).
For example $P(12,2) = \mathtt{True}$, since any number
that is divisible by $i = 12$ is also divisible by $j=2$.

\begin{enumerate}[(A)]
\item Is the 2-argument predicate $P$ {\em reflexive} \textendash{}
does it satisfy the logic formula:
$$\forall i \in A,\;P(i,i).$$
\item Is the predicate $P$ {\em symmetric} \textendash{}
does it satisfy the logic formula:
$$\forall i \in A,\,\forall j \in A,\;P(i,j) \rightarrow P(j,i).$$
\item Is the predicate $P$ {\em transitive} \textendash{}
does it satisfy the logic formula:
$$\forall i,j,k \in A,\;P(i,j) \wedge P(j,k) \rightarrow P(i,k).$$
\item Is the predicate $P$ {\em connex} \textendash{}
does it satisfy the logic formula:
$$\forall i,j \in A,\;P(i,j) \vee P(j,i).$$
\end{enumerate}

{\em Note 1.} Even though the predicate formulas can be verified
by checking all possible combinations of $i,j,k \in A$ (the set $A$ is finite),
it is much faster to reason by the properties of divisibility.\\
{\em Note 2.} For each statement either add a short justification or provide
a counterexample.

\subsubsection{B3. Algorithms and Big-O Notation}

{\bf Question 18.} Let $A,B$ be subsets from the same universe $U = \{ 1,2,\ldots,n \}$.
We want to find the intersection of these two sets $A \cap B$.

\begin{enumerate}[(A)]
\item Describe an algorithm (using steps written in English or some pseudocode)
to find and to output all numbers that are in this intersection $A \cap B$.
(You can call the algorithms that are in the K.Rosen's textbook - for sorting,
linear or binary search and similar. But write out your assumptions, so that it is unambiguous,
which method you are using.)
\item Estimate the time complexity $O(f(n))$: provide a function that estimates the
worst-case time required as the size of the universe $U$ grows.
\end{enumerate}

\vspace{6pt}
{\bf Question 19.}
\begin{enumerate}
\item
Prove that $f(n) = n^2 + n \cdot \ln n$ is in $O(n^2)$ (i.e. $g(n) = n^2$) by finding the limit
of the ratio:
$$\lim_{n \rightarrow \infty} \frac{n^2 + n \cdot \ln n}{n^2}.$$
Or by directly checking the definition of the Big-O notation (finding the two
constants $C$ and $k$ that give you the estimate:
$n>k \rightarrow |f(n)| \leq C|g(n)|$.
\item Is the function $g(n) = n^2$ also in $O(f(n))$, where $f(n) = n^2 + n \cdot \ln n$?
Once again, justify by finding a limit or by checking the definition.
\end{enumerate}

\subsubsection{B4. Number Theory}

{\bf Question 20.} Let us consider the following statement:\\

{\bf Bezout's identity:}
Let $a$ and $b$ be integers and $d$ is their greatest common divisor: $\text{gcd}(a,b)=d$.
Then, there exist integers $x$ and $y$ such that $ax + by = d$.\\

\begin{enumerate}[(A)]
\item Let us have parameters $a,b \in \mathbb{Z}$ and $d \in \mathbb{Z}^{+}$ such that $\text{gcd}(a,b)=d$.
Write the Bezout's identity using predicates and quantifiers (you can also
use equality and four arithmetic operations).
\item Using the same notation as before, write another statement using predicates and quantifiers:
``The modular equation $ax\equiv{}c\;(\text{mod}\,b)$ has a solution if and only if
$c$ is a multiple of $d$.''
\item Using the same notation as before, write another statement using predicates and quantifiers:
``The greatest common divisor $d$ is the smallest positive integer that can be expressed as $ax+by$ for
the given $a,b$ and any integers $x,y$.''
\end{enumerate}

{\em Note 1.} In this expression $a,b,c,d$ are {\em free variables}; they are given and can be used in your formulas.
You can also use any number of {\em bound variables}.\\
{\em Note 2.} We call $c$ a {\em multiple} of $d$ iff $c$ can be expressed as some integer number times $d$.

\vspace{6pt}
{\bf Question 21.} Somebody has proposed a way to encode finite sequences of natural numbers $\mathbb{N}$
(i.e. nonnegative integers: $\mathbb{N} = 0,1,2,\ldots$) as natural numbers.
To encode a sequence $(a_1,a_2,\ldots,a_k)$ we take the first $k$ primes
($p_1 = 2$, $p_2 = 3$, $p_3 = 5$, and so on) and build a number:
$$f(a_1,a_2,\ldots,a_k) = p_1^{a_1}p_2^{a_2}p_3^{a_3}\ldots$$
For example, the sequence $(1,0,2)$ is encoded as $2^1\cdot{} 3^0 \cdot 5^2 = 50$,
but the sequence $(1,1,1,1)$ is encoded as $2^1 \cdot 3^1 \cdot 5^1 \cdot 7^1 =  210$.
\begin{enumerate}
\item Is the function $f: \mathbb{N}^{\ast} \rightarrow \mathbb{N}$ a surjection?
\item Is the function $f: \mathbb{N}^{\ast} \rightarrow \mathbb{N}$ an injection?
\item Can you suggest a bijective function $g: \mathbb{N}^{\ast} \rightarrow \mathbb{N}$ to encode any finite sequence of natural numbers
with a single natural number?
\end{enumerate}

{\em Note.} By $\mathbb{N}^{\ast}$ we denote a list of all finite tuples:
$$\mathbb{N}^{\ast} = \{()\} \cup (\mathbb{N}) \cup (\mathbb{N} \times \mathbb{N}) \cup \ldots.$$
It contains one list of length $0$,\\
It contains $(\mathbb{N})$ all single-element lists $(0)$, $(1)$, etc.,\\
It contains $(\mathbb{N} \times \mathbb{N})$, i.e. all two-element pairs $(0,0)$, $(0,1)$, $(1,0)$, $(1,1)$, etc.\\
And so on.

\vspace{6pt}
{\bf Question 22.} Consider the set of all square functions ($f:\mathbb{R} \rightarrow \mathbb{R}$)
that can be expressed as $f(x) = ax^2 + bx + c$ with integer coefficients $a,b,c$.
Is the set of all such square functions finite? countable? uncountable?
Justify your answer.

\subsection{Part C. Proofs}

In proof problems the goal is to prove some general statement by
showing every essential step of your reasoning.
Below we describe various proof strategies and give some sample
statements that can be proven by that strategy.

\vspace{6pt}
{\bf Translate into Algebra}

In K.Rosen's textbook these are called ``direct proofs''
of IF-THEN statements.
You assume that the condition is true, introduce some notation and
prove that also the conclusion is true.
You sometimes need to sort cases.

\vspace{6pt}
{\bf Question 23.} Prove that, if $n$ is odd, then $n^2$ is also odd.

\vspace{6pt}
{\bf Question 24.} Prove that, if the decimal notation of a number $n$ ends with the digit "5",
then $n^2$ ends with digits "25".

\vspace{6pt}
{\bf Question 25.}
Prove that $n^2 \equiv 5\;(\text{mod}\;11)$ is true
iff
$n \equiv 4\;(\text{mod}\;11)$
or $n \equiv -4\;(\text{mod}\;11)$.

\vspace{20pt}
{\bf Proofs by Contradiction}

In these examples you assume that the statement is false
and get a contradiction.

\vspace{6pt}
{\bf Question 26.} Prove that there are infinitely many primes.

\vspace{6pt}
{\bf Question 27.} Prove that there are infinitely many primes of the form $4n+3$.

\vspace{6pt}
{\bf Question 28.} Prove that there are infinitely many primes
that divide some value of the polynomial
$P(n) = n^2 + n + 1$.

\vspace{6pt}
{\bf Question 29.}
Prove that $\sqrt{2}$ is irrational.

\vspace{6pt}
{\bf Question 30.}
$\log_2 10$ is irrational.

\vspace{20pt}
{\bf Building Bijective Functions (Countable)}

Any combinations of these tactics can be used
to prove that two sets have the same cardinality
(and that there is a bijective function from one set to another).

\vspace{6pt}
{\bf Question 31.}
There is a bijection from $\mathbb{Z}^{+} \cup \{ 1,2,\ldots,n \}$
to $\mathbb{Z}^{+}$.\\
{\em Hint.} One can add $n$ new guests to the Hilbert's hotel.

\vspace{6pt}
{\bf Question 32.}
There is a bijection from $\mathbb{Z}^{+} \times \{ 1,2 \}$
to $\mathbb{Z}^{+}$.\\
{\em Hint.} If there are two infinite buses with guests:
$(1,1),(2,1),(3,1),\ldots$ and $(1,2),(2,2),(2,3),\ldots$,
then they can be hosted in a single Hilbert's hotel.

\vspace{6pt}
{\bf Question 33.}
There is a bijection from $\mathbb{Z}^{+} \times \mathbb{Z}^{+}$
to $\mathbb{Z}^{+}$.\\
{\em Hint.} Show how infinitely many infinite buses can be hosted in a
single Hilbert's hotel (similar to Question 9 from HW1).

\vspace{6pt}
{\bf Question 34.}
There is a bijection from $\mathbb{Q}$ to $\mathbb{Z}^{+}$.\\
{\em Hint.} Same as above, but now you need to combine
three sets of numbers into the same Hilberts hotel (positive rationals,
negative rationals and $0$).

\vspace{20pt}
{\bf Building Bijective Functions (Uncountable)}

\vspace{6pt}
{\bf Question 35.}
There is a bijective function from any closed
segment $[a;b]$ to any other closed segment $[c;d]$
(regardless of their lengths).\\
{\em Hint.} This can be achieved by a simple linear function.

\vspace{6pt}
{\bf Question 36.}
There is a bijective function from any open
segment $(a;b)$ to any other open segment $(c;d)$.\\
{\em Hint.} This also can be achieved by a linear function.

\vspace{6pt}
{\bf Question 37.}
There is a bijective function from any open
segment $(a;b)$ to the set of all real numbers $\mathbb{R}$
or to the half-line of all positive reals $(0;+\infty)$.\\
{\em Hint.} You can use a continuous function with one or two
vertical asymptotes. Such as $f(x) = x/(1-x)$ or $f(x) = \tan x$.

\vspace{6pt}
{\bf Question 38.}
There is a bijective function from a
semi-open segment $[0;1)$ to an open segment $(0;1)$.\\
{\em Hint.} Map the extra point (argument $x = 0$) to any
point inside the target interval $(0;1)$, then
change values for an infinite sequence (bounded inside $(0;1)$)
in the same way as Hibert's hotel.

\vspace{20pt}
{\bf Proving there is no Bijection}

All these proofs use Cantor's diagonalization. (See subsection
``2.5.3 An uncountable set'' in the textbook; pp. 183-187.)

\vspace{6pt}
{\bf Question 39.} Numbers on $(0;1)$ are uncountable.

\vspace{6pt}
{\bf Question 40.}
 All real numbers $\mathbb{R}$ are uncountable - there
is no bijection from $\mathbb{R}$ to $\mathbb{Z}^{+}$ or
to $\mathbb{Q}$.

\vspace{6pt}
{\bf Question 41.} All infinite sequences of integers are uncountable
(Also, all non-decreasing sequences of integers are uncountable.

\vspace{6pt}
{\bf Question 42.} The Power-set of any countable set is
uncountable. For example,
there is no bijection from $\mathcal{P}(\mathbb{Z}^{+})$
to $\mathbb{Z}^{+}$ (i.e. no mapping from the set of all subsets of
positive integers to the set of integers themselves).

\end{document}


\section{Exercise 02}
\subsection{}

\begin{frame}
\frametitleTC{Building a block diagram in Modelica}
\framesubtitleTC{and experimenting with direct synthesis}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Launch the model editor OMEdit.
 \item Issue \texttt{File->New Modelica Class}.
 \item Name the class \texttt{PID4CS}, select \texttt{Package} as \texttt{Specialization}. \underline{un}check\\
       \texttt{Save contents in one file} and click \texttt{OK.}
 \item You just created a \texttt{package}, a hierarchically organised collection\\
       of models.\\
 \item Right-click the P (Package) entry \texttt{PID4CS} in the \texttt{Libraries Browser}\\
       on the left, select \texttt{Save As}, and save. A folder named PID4CS\\
       will be created at the location you chose, details on its structure\\
       (inessential here) at \texttt{modelica.org} for the interested.
 \item Right-click \texttt{PID4CS} in the \texttt{Libraries Browser} and select\\
       \texttt{New Modelica Class}.
 \item Create a \texttt{Model} named \texttt{PS02\_ex02}, that will be part of \texttt{PID4CS}.\\
       Expand the entry (small triangle icon on the left) to see.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Building a block diagram in Modelica}
\framesubtitleTC{and experimenting with direct synthesis}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item No more details on the \texttt{Libraries Browser}, it works like any file manager.
 \item Double-click \texttt{PS02\_ex02} to edit: your window should look like
       \begin{center}
        \includegraphics[width=0.40\columnwidth]{./Unit-05/img/PS02-ex02-fig01.png}
       \end{center}
       and you should be in \texttt{Diagram view} (click its button if not).
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Building a block diagram in Modelica}
\framesubtitleTC{and experimenting with direct synthesis}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Open the \texttt{Modelica Standard Library} (MSL for short); it is the \texttt{Modelica}
       entry in the \texttt{Libraries Browser}.
 \item Navigate to the \texttt{Blocks} package, that contains block diagram elements.
 \item You need \texttt{Transfer Function} from the \texttt{Discrete} sub-package, \texttt{Add}
       and \texttt{Feedback} from the \texttt{Math} one, and \texttt{RealExpression} from \texttt{Sources}.
 \item By dragging{\&}dropping from the browser, and wiring by click{\&}drag, create the\\
       diagram below (block types added for your reference, names above blocks):
       \begin{center}
        \includegraphics[width=0.60\columnwidth]{./Unit-05/img/PS02-ex02-fig02.png}
       \end{center}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Building a block diagram in Modelica}
\framesubtitleTC{and experimenting with direct synthesis}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Double-click on the \texttt{P} and \texttt{C} blocks to set their transfer functions. Polynomials\\
       are specified as vectors of coefficient in decreasing power order: for example,\\
       $2z^3+z+5$ becomes \{2,0,1,5\}. Do not forget the braces and the zero entries\\
       for possibly missing powers --- including the constant term, $z$ becomes \{1,0\}.
 \item You also need to set a \texttt{samplePeriod}, i.e., the time between $k$ and $k+1$\\
       (in seconds). For this exercise it is inessential, enter 1 throughout.
 \item Double-click on the \texttt{w} and \texttt{d} blocks to enter the expression of their output\\
       as a function of time (the symbol \texttt{time} is system-declared).\\
       For example, $w=step(k)$ and $d=step(k-10)$ become respectively\\
       \texttt{if time<0 then 0 else 1} and \texttt{if time<10 then 0 else 1}.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Building a block diagram in Modelica}
\framesubtitleTC{and experimenting with direct synthesis}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Issue \texttt{Simulation->Simulation Setup} to set the final time and then \texttt{Simulate}.
 \item Use the \texttt{Variable Browser} in the \texttt{Plotting} tab to examine the results. Below\\
       is an example with some explanatory lettering.
       \begin{center}
        \includegraphics[width=0.60\columnwidth]{./Unit-05/img/PS02-ex02-fig03.png}
       \end{center}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Building a block diagram in Modelica}
\framesubtitleTC{and experimenting with direct synthesis}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Please take some time at home to familiarise with OMEdit: it is quite intuitive\\
       and you can find plenty of tutorials online, many with videos.
 \item For the moment, replicate for example the case with unstable hidden part:
       \begin{displaymath}
        \begin{array}{lcl}
         P(z)              = \frac{1}{\red{z-2}},\,
         G_{yw}^{\circ}(z) = \frac{0.2}{z-0.8} &
         \Rightarrow &
         C(z)              = \frac{0.2\red{(z-2)}}{z-1}.
        \end{array}
       \end{displaymath}
 \item Below is the plot of $y$ (\texttt{ad.y}) and $u$ (\texttt{C.y}) for $w=step(k)$ and $d=step(k-30)$.\\
       Apparently, something happens that $G_{yw}$ does not tell...
       \begin{center}
        \includegraphics[width=0.50\columnwidth]{./Unit-05/img/PS02-ex02-fig04.png}
       \end{center}
 \end{itemize}
\end{frame}


%!TEX root = main.tex
\section{Introduction}
Publicly available datasets have helped the computer vision community to
compare new algorithms and develop applications. Especially
MNIST~\cite{LeNet-5} was used thousands of times to train and evaluate models
for classification. However, even rather simple models consistently get about
$\SI{99.2}{\percent}$ accuracy on MNIST~\cite{TF-MNIST-2016}. The best models
classify everything except for about 20~instances correct. This makes
meaningful statements about improvements in classifiers hard. Possible reason
why current models are so good on MNIST are
\begin{enumerate*}
    \item MNIST has only 10~classes
    \item there are very few (probably none) labeling errors in MNIST
    \item every class has \num{6000}~training samples
    \item the feature dimensionality is comparatively low.
\end{enumerate*}
Also, applications which need to recognize only Arabic numerals are rare.

Similar to MNIST, \dbName{} is of very low resolution. In contrast to MNIST,
the \dbNameVersion~dataset contains \dbTotalClasses~classes, including Arabic
numerals and Latin characters. Furthermore, \dbNameVersion{} has much less
recordings per class than MNIST and is only in black and white whereas
MNIST is in grayscale.

\dbName{} could be used to train models for semantic segmentation of
non-cursive handwritten documents like mathematical notes or forms.

\section{Terminology}
A \textit{symbol} is an atomic semantic entity which has exactly one visual
appearance when it is handwritten. Examples of symbols are:
$\alpha, \propto, \cdot, x, \int, \sigma, \dots$
%\footnote{The first symbol is an \verb+\alpha+, the second one is a \verb+\propto+.}

While a symbol is a single semantic entity with a given visual appearance, a
glyph is a single typesetting entity. Symbols, glyphs and \LaTeX{} commands do
not relate:

\begin{itemize}
    \item Two different symbols can have the same glyph. For example, the symbols
\verb+\sum+ and \verb+\Sigma+ both render to $\Sigma$, but they have different
semantics and hence they are different symbols.
    \item Two different glyphs can correspond to the same semantic entity. An example is
\verb+\varphi+ ($\varphi$) and \verb+\phi+ ($\phi$): Both represent the small
Greek letter \enquote{phi}, but they exist in two different variants. Hence
\verb+\varphi+ and \verb+\phi+ are two different symbols.
    \item Examples for different \LaTeX{} commands that represent the same symbol are
          \verb+\alpha+ ($\alpha$) and \verb+\upalpha+ ($\upalpha$): Both have the same
semantics and are hand-drawn the same way. This is the case for all \verb+\up+
variants of Greek letters.
\end{itemize}

All elements of the data set are called \textit{recordings} in the following.

\section{How HASY was created}
\dbName{} is derived from the HWRT dataset which was first used and described
in~\cite{Thoma:2014}. HWRT is an on-line recognition dataset, meaning it does
not contain the handwritten symbols as images, but as point-sequences. Hence
HWRT contains strictly more information than \dbName. The larger dimension
of each recordings bounding box was scaled to be \SI{32}{\pixel}. The image
was then centered within the $\SI{32}{\pixel} \times \SI{32}{\pixel}$ bounding
box.

\begin{figure}[h]
    \centering
    \includegraphics*[width=\linewidth, keepaspectratio]{figures/sample-images.png}
    \caption{100 recordings of the \dbNameVersion{} data set.}
    \label{fig:100-data-items}
\end{figure}

HWRT contains exactly the same recordings and classes as \dbName, but \dbName{}
is rendered in order to make it easy to use.

HWRT and hence \dbName{} is a merged dataset. $\SI{91.93}{\percent}$ of HWRT
were collected by Detexify~\cite{Kirsch,Kirsch2014}. The remaining recordings
were collected by \href{http://write-math.com}{http://write-math.com}. Both
projects aim at helping users to find \LaTeX{} commands in cases where the
users know how to write the symbol, but not the symbols name. The user writes
the symbol on a blank canvas in the browser (either via touch devices or with a
mouse). Then the websites give the Top-$k$ results which the user could have
thought of. The user then clicks on the correct symbol to accept it as the
correct symbol. On \href{http://write-math.com}{write-math.com}, other users
can also suggest which symbol could be the correct one.

After collecting the data, Martin Thoma manually inspected each recording. This
manual inspection is a necessary step as anonymous web users could submit any
drawing they wanted to any symbol. This includes many creative recordings as
shown in~\cite{Kirsch,Thoma:2014} as well as loose associations. In some cases,
the correct label was unambiguous and could be changed. In other cases, the
recordings were removed from the data set.

It is not possible to determine the exact number of people who contributed
handwritten symbols to the Detexify part of the dataset. The part which was
created with \href{http://write-math.com}{write-math.com} was created by
477~user~IDs. Although user IDs are given in the dataset, they are not
reliable. On the one hand, the Detexify data has the user ID 16925,
although many users contributed to it. Also, some users lend their phone to
others while being logged in to show how write-math.com works. This leads to
multiple users per user ID. On the other hand, some users don't register and
use write-math.com multiple times. This can lead to multiple user IDs for one
person.

\section{Classes}
The \dbNameVersion~dataset contains \dbTotalClasses~classes. Those classes include the
Latin uppercase and lowercase characters (\verb+A-Z+, \verb+a-z+), the Arabic
numerals (\verb+0-9+), 32~different types of arrows, fractal and calligraphic
Latin characters, brackets and more. See \cref{table:symbols-of-db-0,table:symbols-of-db-1,table:symbols-of-db-2,table:symbols-of-db-3,table:symbols-of-db-4,table:symbols-of-db-5,table:symbols-of-db-6,table:symbols-of-db-7,table:symbols-of-db-8} for more information.

\section{Data}
The \dbNameVersion~dataset contains \dbTotalInstances{} black and white images
of the size $\SI{32}{\pixel} \times \SI{32}{\pixel}$. Each image is labeled
with one of \dbTotalClasses~labels. An example of 100~elements of the
\dbNameVersion{} data set is shown in~\cref{fig:100-data-items}.

The average amount of black pixels is \SI{16}{\percent}, but this is highly
class-dependent ranging from \SI{3.7}{\percent} of \enquote{$\dotsc$} to \SI{59.2}{\percent} of \enquote{$\blacksquare$} average
black pixel by class.

The ten classes with most recordings are:
\[\int, \sum, \infty, \alpha, \xi, \equiv, \partial, \mathds{R}, \in, \square\]
Those symbols have \num{26780} recordings and thus account for
\SI{16}{\percent} of the data set. 47~classes have more than \num{1000}
recordings. The number of recordings of the remaining classes are distributed
as visualized in~\cref{fig:class-data-distribution}.

\begin{figure}[h]
    \centering
    \includegraphics*[width=\linewidth, keepaspectratio]{figures/data-dist}
    \caption{Distribution of the data among classes. 47~classes with
             more than \num{1000} recordings are not shown.}
    \label{fig:class-data-distribution}
\end{figure}

A weakness of \dbNameVersion{} is the amount of available data per class. For
some classes, there are only 51~elements in the test set.

The data has $32\cdot 32 = 1024$ features in $\Set{0, 255}$.
As~\cref{table:pca-explained-variance} shows, \SI{32}{\percent} of the features
can explain~\SI{90}{\percent} of the variance, \SI{54}{\percent} of the
features explain \SI{99}{\percent} of the variance and \SI{86}{\percent} of the
features explain \SI{99}{\percent} of the variance.

\begin{table}[h]
    \centering
    \begin{tabular}{lccc}
    \toprule
    Principal Components &  331              & 551               & 882  \\
    Explained Variance   & \SI{90}{\percent} & \SI{95}{\percent} & \SI{99}{\percent} \\
    \bottomrule
    \end{tabular}
    \caption{The number of principal components necessary to explain,
             \SI{90}{\percent}, \SI{95}{\percent}, \SI{99}{\percent}
             of the data.}
    \label{table:pca-explained-variance}
\end{table}

The Pearson correlation coefficient was calculated for all features. The
features are more correlated the closer the pixels are together as one can see
in~\cref{fig:feature-correlation}. The block-like structure of every 32th
feature comes from the fact the features were flattened for this visualization.
The second diagonal to the right shows features which are one pixel down in the
image. Those correlations are expected as symbols are written by continuous
lines. Less easy to explain are the correlations between high-index
features with low-index features in the upper right corner of the image.
Those correlations correspond to features in the upper left corner with
features in the lower right corner. One explanation is that symbols which have
a line in the upper left corner are likely $\blacksquare$.

\begin{figure}[h]
    \centering
    \includegraphics*[width=\linewidth, keepaspectratio]{figures/feature-correlation.pdf}
    \caption{Correlation of all $32 \cdot 32 = 1024$ features. The diagonal
             shows the correlation of a feature with itself.}
    \label{fig:feature-correlation}
\end{figure}

\section{Classification Challenge}
\subsection{Evaluation}
\dbName{} defines 10 folds which should be used for calculating the accuracy
of any classifier being evaluated on \dbName{} as follows:

\begin{algorithm}[H]
    \begin{algorithmic}
        \Function{CrossValidation}{Folds $F$}
            \State $D \gets \cup_{i=1}^{10} F_i$\Comment{Complete Dataset}
            \For{($i=0$; $\;i < 10$; $\;i$++)}
                \State $A \gets D \setminus F_i$\Comment{Train set}
                \State $B \gets F_i$\Comment{Test set}
                \State Train Classifier $C_i$ on $A$
                \State Calculate accuracy $a_i$ of $C_i$ on $B$
            \EndFor
            \State \Return ($\frac{1}{10}\sum_{i=1}^{10} a_i$, $\min(a_i)$, $\max(a_i)$)
        \EndFunction
    \end{algorithmic}
    \caption{Calculate the mean accuracy, the minimum accuracy, and the maximum
             accuracy with 10-fold cross-validation}
\label{alg:seq1}
\end{algorithm}

\subsection{Model Baselines}
Eight standard algorithms were evaluated by their accuracy on the raw image
data. The neural networks were implemented with
Tensorflow~0.12.1~\cite{tensorflow2015-whitepaper}. All other algorithms are
implemented in sklearn~0.18.1~\cite{scikit-learn}. \Cref{table:classifier-results}
shows the results of the models being trained and tested on MNIST and also for
\dbNameVersion{}:
\begin{table}[h]
    \centering
    \begin{tabular}{lrrr}
    \toprule
    \multirow{2}{*}{Classifier}    & \multicolumn{3}{c}{Test Accuracy}           \\%& \multirow{2}{*}{\parbox{1.2cm}{\centering HASY\\Test time}}\\
                  & MNIST                & HASY                & min -- max\hphantom{00 } \\\midrule% &
    TF-CNN        & \SI{99.20}{\percent} & \SI{81.0}{\percent} & \SI{80.6}{\percent} -- \SI{81.5}{\percent}\\% &  \SI{3.1}{\second}\\
    Random Forest & \SI{96.41}{\percent} & \SI{62.4}{\percent} & \SI{62.1}{\percent} -- \SI{62.8}{\percent}\\% & \SI{19.0}{\second}\\
    MLP (1 Layer) & \SI{89.09}{\percent} & \SI{62.2}{\percent} & \SI{61.7}{\percent} -- \SI{62.9}{\percent}\\% &  \SI{7.8}{\second}\\
    LDA           & \SI{86.42}{\percent} & \SI{46.8}{\percent} & \SI{46.3}{\percent} -- \SI{47.7}{\percent}\\% &   \SI{0.2}{\second}\\
    $k$-NN ($k=3$)& \SI{92.84}{\percent} & \SI{28.4}{\percent} & \SI{27.4}{\percent} -- \SI{29.1}{\percent}\\% & \SI{196.2}{\second}\\
    $k$-NN ($k=5$)& \SI{92.88}{\percent} & \SI{27.4}{\percent} & \SI{26.9}{\percent} -- \SI{28.3}{\percent}\\% & \SI{196.2}{\second}\\
    QDA           & \SI{55.61}{\percent} & \SI{25.4}{\percent} & \SI{24.9}{\percent} -- \SI{26.2}{\percent}\\% & \SI{94.7}{\second}\\
    Decision Tree & \SI{65.40}{\percent} & \SI{11.0}{\percent} & \SI{10.4}{\percent} -- \SI{11.6}{\percent}\\% &   \SI{0.0}{\second}\\
    Naive Bayes   & \SI{56.15}{\percent} &  \SI{8.3}{\percent} & \SI{7.9}{\percent} -- \hphantom{0}\SI{8.7}{\percent}\\% & \SI{24.7}{\second}\\
    AdaBoost      & \SI{73.67}{\percent} &  \SI{3.3}{\percent} & \SI{2.1}{\percent} -- \hphantom{0}\SI{3.9}{\percent}\\% &  \SI{9.8}{\second}\\
    \bottomrule
    \end{tabular}
    \caption{Classification results for eight classifiers.
             % The test time is the time needed for all test samples in average.
             The number of
             test samples differs between the folds, but is $\num{16827} \pm
             166$. The decision tree was trained with a maximum depth of~5. The
             exact structure of the CNNs is explained
             in~\cref{subsec:CNNs-Classification}. For $k$ nearest neighbor,
             the amount of samples per class had to be reduced to 50 for HASY
             due to the extraordinary amount of testing time this algorithm
             needs.}
    \label{table:classifier-results}
\end{table}

The following observations are noteworthy:
\begin{itemize}
    \item All algorithms achieve much higher accuracy on MNIST than on
          \dbNameVersion{}.
    \item While a single Decision Tree performs much better on MNIST than
          QDA, it is exactly the other way around for~\dbName{}. One possible
          explanation is that MNIST has grayscale images while \dbName{} has
          black and white images.
\end{itemize}

\subsection{Convolutional Neural Networks}\label{subsec:CNNs-Classification}
Convolutional Neural Networks (CNNs) are state of the art on several computer
vision benchmarks like MNIST~\cite{wan2013regularization}, CIFAR-10, CIFAR-100
and SVHN~\cite{huang2016densely},
ImageNet~2012~\cite{deep-residual-networks-2015} and more. Experiments on
\dbNameVersion{} without preprocessing also showed that even the
simplest CNNs achieve much higher accuracy on \dbNameVersion{} than all other
classifiers (see~\cref{table:classifier-results}).

\Cref{table:cnn-results} shows the 10-fold cross-validation results for four
architectures.
\begin{table}[H]
    \centering
    \begin{tabular}{lrrrr}
    \toprule
    \multirow{2}{*}{Network} & \multirow{2}{*}{Parameters}    & \multicolumn{2}{c}{Test Accuracy} & \multirow{2}{*}{Time} \\
            &               & mean                & min -- max\hphantom{00 } & \\\midrule
    2-layer & \num{3023537} & \SI{73.8}{\percent} & \SI{72.9}{\percent} -- \SI{74.3}{\percent} & \SI{1.5}{\second}\\
    3-layer & \num{1530609} & \SI{78.4}{\percent} & \SI{77.6}{\percent} -- \SI{79.0}{\percent} & \SI{2.4}{\second}\\
    4-layer &  \num{848753} & \SI{80.5}{\percent} & \SI{79.2}{\percent} -- \SI{80.7}{\percent} & \SI{2.8}{\second}\\
    TF-CNN  & \num{4592369} & \SI{81.0}{\percent} & \SI{80.6}{\percent} -- \SI{81.5}{\percent} & \SI{2.9}{\second}\\
    \bottomrule
    \end{tabular}
    \caption{Classification results for CNN architectures. The test time is,
             as before, the mean test time for all examples on the ten folds.}
    \label{table:cnn-results}
\end{table}
The following architectures were evaluated:
\begin{itemize}
    \item 2-layer: A convolutional layer with 32~filters of size $3 \times 3 \times 1$
          is followed by a $2 \times 2$ max pooling layer with stride~2. The output
          layer is --- as in all explored CNN architectures --- a fully
          connected softmax layer with 369~neurons.
    \item 3-layer: Like the 2-layer CNN, but before the output layer is another
          convolutional layer with 64~filters of size $3 \times 3 \times 32$
          followed by a $2 \times 2$ max pooling layer with stride~2.
    \item 4-layer: Like the 3-layer CNN, but before the output layer is another
          convolutional layer with 128~filters of size $3 \times 3 \times 64$
          followed by a $2 \times 2$ max pooling layer with stride~2.
    \item TF-CNN: A convolutional layer with 32~filters of size $3 \times 3 \times 1$
          is followed by a $2 \times 2$ max pooling layer with stride~2.
          Another convolutional layer with 64~filters of size $3 \times 3 \times 32$
          and a $2 \times 2$  max pooling layer with stride~2 follow. A fully
          connected layer with 1024~units and tanh activation function, a
          dropout layer with dropout probability 0.5 and the output softmax
          layer are last. This network is described in~\cite{tf-mnist}.
\end{itemize}

For all architectures, ADAM~\cite{kingma2014adam} was used for training. The
combined training and testing time was always less than 6~hours for the 10~fold
cross-validation on a Nvidia GeForce GTX Titan Black with CUDA~8 and CuDNN~5.1.
\clearpage
\subsection{Class Difficulties}
The class-wise accuracy
\[\text{class-accuracy}(c) = \frac{\text{correctly predicted samples of class } c}{\text{total number of training samples of class } c}\]
is used to estimate how difficult a class is.

32~classes were not a single time classified correctly by TF-CNN and hence have
a class-accuracy of~0. They are shown in~\cref{table:hard-classes}. Some, like
\verb+\mathsection+ and \verb+\S+ are not distinguishable at all. Others, like
\verb+\Longrightarrow+ and
\verb+\Rightarrow+ are only distinguishable in some peoples handwriting.
Those classes account for \SI{2.8}{\percent} of the data.

\begin{table}[h]
    \centering
    \begin{tabular}{lcrlc}
    \toprule
    \LaTeX & Rendered & Total & Confused with & \\\midrule
    \verb+\mid+ & $\mid$ & 34 & \verb+|+ & $|$ \\
    \verb+\triangle+ & $\triangle$ & 32 & \verb+\Delta+ & $\Delta$ \\
    \verb+\mathds{1}+ & $\mathds{1}$ & 32 & \verb+\mathbb{1}+ & \includegraphics{symbols/mathbb1.pdf} \\
    \verb+\checked+ & {\mbox {\wasyfamily \char 8}} & 28 & \verb+\checkmark+ & $\checkmark$ \\
    \verb+\shortrightarrow+ & $\shortrightarrow$ & 28 & \verb+\rightarrow+ & $\rightarrow$ \\
    \verb+\Longrightarrow+ & $\Longrightarrow$ & 27 & \verb+\Rightarrow+ & $\Rightarrow$ \\
    \verb+\backslash+ & $\backslash$ & 26 & \verb+\setminus+ & $\setminus$ \\
    \verb+\O+ & \O & 24 & \verb+\emptyset+ & $\emptyset$ \\
    \verb+\with+ & $\with$ & 21 & \verb+\&+ & $\&$ \\
    \verb+\diameter+ & {\mbox {\wasyfamily \char 31}} & 20 & \verb+\emptyset+ & $\emptyset$ \\
    \verb+\triangledown+ & $\triangledown$ & 20 & \verb+\nabla+ & $\nabla$ \\
    \verb+\longmapsto+ & $\longmapsto$ & 19 & \verb+\mapsto+ & $\mapsto$ \\
    \verb+\dotsc+ & $\dotsc$ & 15 & \verb+\dots+ & $\dots$ \\
    \verb+\fullmoon+ & {\mbox {\wasyfamily \char 35}} & 15 & \verb+\circ+ & $\circ$ \\
    \verb+\varpropto+ & $\varpropto$ & 14 & \verb+\propto+ & $\propto$ \\
    \verb+\mathsection+ & $\mathsection$ & 13 & \verb+\S+ & $\S$ \\
    \verb+\vartriangle+ & $\vartriangle$ & 12 & \verb+\Delta+ & $\Delta$ \\
    \verb+O+ & $O$ & 9 & \verb+\circ+ & $\circ$ \\
    \verb+o+ & $o$ & 7 & \verb+\circ+ & $\circ$ \\
    \verb+c+ & $c$ & 7 & \verb+\subset+ & $\subset$ \\
    \verb+v+ & $v$ & 7 & \verb+\vee+ & $\vee$ \\
    \verb+x+ & $x$ & 7 & \verb+\times+ & $\times$ \\
    \verb+\mathbb{Z}+ & $\mathbb{Z}$ & 7 & \verb+\mathds{Z}+ & $\mathds{Z}$ \\
    \verb+T+ & $T$ & 6 & \verb+\top+ & $\top$ \\
    \verb+V+ & $V$ & 6 & \verb+\vee+ & $\vee$ \\
    \verb+g+ & $g$ & 6 & \verb+9+ & $9$ \\
    \verb+l+ & $l$ & 6 & \verb+|+ & $|$ \\
    \verb+s+ & $s$ & 6 & \verb+\mathcal{S}+ & $\mathcal{S}$ \\
    \verb+z+ & $z$ & 6 & \verb+\mathcal{Z}+ & $\mathcal{Z}$ \\
    \verb+\mathbb{R}+ & $\mathbb{R}$ & 6 & \verb+\mathds{R}+ & $\mathds{R}$ \\
    \verb+\mathbb{Q}+ & $\mathbb{Q}$ & 6 & \verb+\mathds{Q}+ & $\mathds{Q}$ \\
    \verb+\mathbb{N}+ & $\mathbb{N}$ & 6 & \verb+\mathds{N}+ & $\mathds{N}$ \\
    \bottomrule
    \end{tabular}
    \caption{32~classes which were not a single time classified correctly by
             the best CNN.}
    \label{table:hard-classes}
\end{table}

In contrast, 21~classes have an accuracy of more than \SI{99}{\percent} with
TF-CNN (see~\cref{table:easy-classes}).

\begin{table}[h]
    \centering
    \begin{tabular}{lcr}
    \toprule
    \LaTeX & Rendered & Total\\\midrule
    \verb+\forall        + & $\forall        $ & 214 \\
    \verb+\sim           + & $\sim           $ & 201 \\
    \verb+\nabla         + & $\nabla         $ & 122 \\
    \verb+\cup           + & $\cup           $ & 93  \\
    \verb+\neg           + & $\neg           $ & 85  \\
    \verb+\setminus      + & $\setminus      $ & 52  \\
    \verb+\supset        + & $\supset        $ & 42  \\
    \verb+\vdots         + & $\vdots         $ & 41  \\
    \verb+\boxtimes      + & $\boxtimes      $ & 22  \\
    \verb+\nearrow       + & $\nearrow       $ & 21  \\
    \verb+\uplus         + & $\uplus         $ & 19  \\
    \verb+\nvDash        + & $\nvDash        $ & 15  \\
    \verb+\AE            + & \AE               & 15  \\
    \verb+\Vdash         + & $\Vdash         $ & 14  \\
    \verb+\Leftarrow     + & $\Leftarrow     $ & 14  \\
    \verb+\upharpoonright+ & $\upharpoonright$ & 14  \\
    \verb+-              + & $-              $ & 12  \\
    \verb+\guillemotleft + & $\guillemotleft $ & 11  \\
    \verb+R              + & $R              $ & 9   \\
    \verb+7              + & $7              $ & 8   \\
    \verb+\blacktriangleright+ & $\blacktriangleright$ & 6 \\
    \bottomrule
    \end{tabular}
    \caption{21~classes with a class-wise accuracy of more than \SI{99}{\percent}
             with TF-CNN.}
    \label{table:easy-classes}
\end{table}

\section{Verification Challenge}
In the setting of an online symbol recognizer like
\href{http://write-math.com}{write-math.com} it is important to recognize when
the user enters a symbol which is not known to the classifier. One way to achieve
this is by training a binary classifier to recognize when two recordings belong to
the same symbol. This kind of task is similar to face verification.
Face verification is the task where two images with faces are given and it has
to be decided if they belong to the same person.

For the verification challenge, a training-test split is given. The training
data contains images with their class labels. The test set
contains 32~symbols which were not seen by the classifier before. The elements
of the test set are pairs of recorded handwritten symbols $(r_1, r_2)$. There
are three groups of tests:
\begin{enumerate}[label=V\arabic*]
    \item $r_1$ and $r_2$ both belong to symbols which are in the training set,
    \item $r_1$ belongs to a symbol in the training set, but $r_2$
          might not
    \item $r_1$ and $r_2$ don't belong symbols in the training set
\end{enumerate}

When evaluating models, the models may not take advantage of the fact that it
is known if a recording $r_1$ / $r_2$ is an instance of the training symbols.
For all test sets, the following numbers should be reported: True Positive (TP),
True Negative (TN), False Positive (FP), False Negative (FN),
Accuracy $= \frac{TP+ TN}{TP+TN+FP+FN}$.

% \section{Open Questions}

% There are still a couple of open questions about \dbNameVersion:

% \begin{enumerate}
%     \item What is the accuracy of human expert labelers?
%     \item What is the variance between human experts labeling the samples?
% \end{enumerate}

\section{Acknowledgment}

I want to thank \enquote{Begabtenstiftung Informatik Karls\-ruhe}, the Foundation
for Gifted Informatics Students in Karlsruhe. Their support helped me to write
this work.


% NB: use pdflatex to compile NOT pdftex.  Also make sure youngtab is
% there...

% converting eps graphics to pdf with ps2pdf generates way too much
% whitespace in the resulting pdf, so crop with pdfcrop
% cf. http://www.cora.nwra.com/~stockwel/rgspages/pdftips/pdftips.shtml

\documentclass[10pt,aspectratio=169,dvipsnames]{beamer}

\usetheme[color/block=transparent]{metropolis}

\usepackage[absolute,overlay]{textpos}
\usepackage{booktabs}
\usepackage[utf8]{inputenc}

\usepackage{tikz}

\usepackage[scale=2]{ccicons}

\usepackage[official]{eurosym}

%use this to add space between rows
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}

\newcommand{\R}{\mathbb{R}}

\setbeamerfont{alerted text}{series=\bfseries}
\setbeamercolor{alerted text}{fg=Mahogany}
\setbeamercolor{background canvas}{bg=white}

\def\l{\lambda}
\def\m{\mu}
\def\d{\partial}
\def\cL{\mathcal{L}}
\def\co2{CO${}_2$}
\def\bra#1{\left\langle #1\right|}
\def\ket#1{\left| #1\right\rangle}
\newcommand{\braket}[2]{\langle #1 | #2 \rangle}
\newcommand{\norm}[1]{\left\| #1 \right\|}
\def\corr#1{\Big\langle #1 \Big\rangle}
\def\corrs#1{\langle #1 \rangle}

\def\mw{\text{ MW}}
\def\mwh{\text{ MWh}}
\def\emwh{\text{ \euro/MWh}}

\newcommand{\ubar}[1]{\text{\b{$#1$}}}

% for sources http://tex.stackexchange.com/questions/48473/best-way-to-give-sources-of-images-used-in-a-beamer-presentation

\setbeamercolor{framesource}{fg=gray}
\setbeamerfont{framesource}{size=\tiny}

\newcommand{\source}[1]{\begin{textblock*}{5cm}(10.5cm,8.35cm)
    \begin{beamercolorbox}[ht=0.5cm,right]{framesource}
        \usebeamerfont{framesource}\usebeamercolor[fg]{framesource} Source: {#1}
    \end{beamercolorbox}
\end{textblock*}}

\usepackage{hyperref}

\usepackage{tikz}

\usepackage[europeanresistors,americaninductors]{circuitikz}

%\usepackage[pdftex]{graphicx}

\graphicspath{{graphics/}}

\DeclareGraphicsExtensions{.pdf,.jpeg,.png,.jpg,.gif}

\def\goat#1{{\scriptsize\color{green}{[#1]}}}

\let\olditem\item
\renewcommand{\item}{%
\olditem\vspace{5pt}}

\title{Energy System Modelling\\ Summer Semester 2020, Lecture 11}
%\subtitle{---}
\author{
  {\bf Dr. Tom Brown}, \href{mailto:tom.brown@kit.edu}{tom.brown@kit.edu}, \url{https://nworbmot.org/}\\
  \emph{Karlsruhe Institute of Technology (KIT), Institute for Automation and Applied Informatics (IAI)}
}

\date{}

\titlegraphic{
  \vspace{0cm}
  \hspace{10cm}
    \includegraphics[trim=0 0cm 0 0cm,height=1.8cm,clip=true]{kit.png}

\vspace{5.1cm}

  {\footnotesize

  Unless otherwise stated, graphics and text are Copyright \copyright Tom Brown, 2020.
  Graphics and text for which no other attribution are given are licensed under a
  \href{https://creativecommons.org/licenses/by/4.0/}{Creative Commons
  Attribution 4.0 International Licence}. \ccby}
}

\begin{document}

\maketitle

\begin{frame}

  \frametitle{Table of Contents}
  \setbeamertemplate{section in toc}[sections numbered]
  \tableofcontents[hideallsubsections]
\end{frame}

\section{Present value and discounting}

\begin{frame}
  \frametitle{The value of money depends on time}

  \alert{Question 1:} What would you prefer: \euro 1000 today, or \euro 1000 in 3 years?

  \pause

  \euro 1000 today can be invested in the bank with an interest rate of 5\%.

  After 3 years you would have
  \begin{equation*}
     1000 \cdot (1 + 0.05)^3 = 1158
  \end{equation*}

  \alert{Answer 1:} Best to take the money today and use the opportunity to invest!

  \vspace{.5cm}

 ``Money in the future is worth less than money today.''

\end{frame}

\begin{frame}
  \frametitle{The value of money depends on time}

  \alert{Question 2:} What would you prefer: \euro 1000 today, or \euro 1300 in 5  years?

  \pause

  If you invested \euro 1000 today, after 5 years you would have only
  \begin{equation*}
     1000 \cdot (1 + 0.05)^5 = 1276
  \end{equation*}

  \alert{Answer 2:} Best to wait for the \euro 1300 in 5 years!

\end{frame}

\begin{frame}
  \frametitle{Present value}

  To allow comparison between income and outgoings in different years, we need to agree on a particular point in time to evaluate the cash flows.

  The simplest and most frequently used time point: today's value, known as the \alert{present value}.

  For an \alert{interest rate} $r$ we multiply the income or outgoings in year $t$ by the \alert{discount factor}
  \begin{equation*}
    \frac{1}{(1+r)^t}
  \end{equation*}
  to calculate the present value. We have \alert{discounted} the future cash flow.

  Future income or outgoings are \alert{worth less} from today's point of view (as long as $r$ is positive).

  ``Money in the future is worth less than money today.''

\end{frame}

\begin{frame}
  \frametitle{Example: present value}

  For our example with interest rate 5\% we can now order the options:
  \ra{1.1}
  \begin{table}[!t]
    \begin{tabular}{rrr}
      \toprule
      Income (\euro) & Year & Present value (\euro)  \\
      \midrule
      1000  &  3 & $\frac{1000}{(1+0.05)^3} = 863$  \\
      1000  &  0 & $\frac{1000}{(1+0.05)^0} = 1000$ \\
      1300  &  5 & $\frac{1300}{(1+0.05)^5} = 1019$ \\
      \bottomrule
    \end{tabular}
  \end{table}

\end{frame}

\section{Investment calculations}

\begin{frame}
  \frametitle{Motivation: Power plant investment}

  A company is considering investing in a photovoltaic plant on its roof. The key figures:

  \begin{columns}[T]
\begin{column}{6cm}

    \begin{table}[!t]
    \begin{tabular}{lr}
      \toprule
      Size &  100 kW \\
      Investment cost & 800 \euro kW$^{-1}$ \\
      Operating cost & 20 \euro kW$^{-1}$ a$^{-1}$\\
      Feed-In Tariff & 0.1 \euro kWh$^{-1}$\\
      Full load hours & 1000 \\
      Period of subsidy & 20 years \\
      \bottomrule
    \end{tabular}
  \end{table}
\end{column}
\begin{column}{6cm}

    \includegraphics[width=6.5cm]{PV_Anlage_auf_der_Rottweiler_Stadthalle.jpg}
\end{column}
  \end{columns}

  \vspace{.5cm}

  The company can invest its money elsewhere for a return of 5\%.

  \alert{Is it worthwhile to invest in the photovoltaic plant?}

  \source{\href{https://www.ise.fraunhofer.de/content/dam/ise/de/documents/publications/studies/DE2018_ISE_Studie_Stromgestehungskosten_Erneuerbare_Energien.pdf}{Fraunhofer ISE Stromgestehungskosten 2018}, \href{https://de.wikipedia.org/wiki/ENRW_Energieversorgung_Rottweil\#/media/Datei:PV_Anlage_auf_der_Rottweiler_Stadthalle.jpg}{Wikipedia}}

\end{frame}

\begin{frame}
  \frametitle{Investment calculations}

  An \alert{investment calculation} quantifies the financial costs and benefits of an investment, assuming that future income and outgoings can be predicted.

  It considers
  \begin{itemize}
  \item \alert{Capital costs} - Costs for investments and installation
  \item \alert{Consumption costs} - Fuel, other materials (e.g. lubricants for wind turbine), etc.
  \item \alert{Operating costs} - Maintenance, wages, insurance, management, etc.
  \item \alert{Income} - depends on market price, subsidies, and production
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Dynamic investment calculation}

  For a \alert{dynamic investment calculation} we sum the present values of all income and outgoings over the $T$ years of operation taking account of the interest rate $r$ to get the \alert{Net Present Value (NPV)}:
  \begin{equation*}
    NPV = \sum_{t=0}^T \frac{ - I_t - V_t - B_t + U_t}{(1+r)^t}
  \end{equation*}
  where $I_t$ is the capital expenditure in year $t$, $V_t$ the
  consumption costs (e.g. for fuel cost $o_t$ and annual production $Q_t$, $V_t = o_t\cdot Q_t$), $B_t$ the operating costs und $U_t$ the income (e.g. average market value $\lambda_t$ times annual production
  $Q_t$, $U_t = \lambda_t\cdot Q_t$).

  \alert{Conclusion:} If $NPV > 0$, the investment is worthwhile.

  If $NPV < 0$, better to invest with a rate of return of $r$ elsewhere.

  For comparisons between different investments, a higher NPV should be preferred.
\end{frame}

\begin{frame}
  \frametitle{Example: Rooftop photovoltaic unit}

    All cash flows (costs and income) in \euro:
    \begin{table}[!t]
    \begin{tabular}{lrrrrr}
      \toprule
      year $t$ &  0 & 1 & 2 & $\cdots$ & 20 \\
      \midrule
      Capital costs $I_t$ & 80,000  & 0 & 0  & & 0\\
      Operating costs $B_t$ & 0 & 2,000 & 2,000 & & 2,000  \\
      Income $U_t$ & 0 & 10,000  & 10,000 & & 10,000 \\
      \midrule
      Net cash flow $U_t - I_t - B_t$ & -80,000 & 8,000 & 8,000 & & 8,000 \\
      \midrule
      Discount factor $\frac{1}{(1+r)^t}$ & 1 & $\frac{1}{(1+r)}$ & $\frac{1}{(1+r)^2}$ & &  $\frac{1}{(1+r)^{20}}$ \\
      \bottomrule
    \end{tabular}
    \end{table}

\end{frame}

\begin{frame}
  \frametitle{NPV simplification}

  If investments only occur in the first year, and the costs and income for the following years are constant, we can simplify the NPV formula:
  \begin{equation*}
    NPV = -I_0 + (U - V - B) \sum_{t=1}^T \frac{1}{(1+r)^t}
  \end{equation*}
  The sum $\sum$ is called the \alert{Present Value Factor} $PVF(r,T)$.

  For a geometric series with $|q| < 1$ we have $\sum_{n=0}^\infty q^n = \frac{1}{1-q}$.
  For $q= (1+r)^{-1}$ we can simplify the formula
  \begin{align*}
    PVF(r,T) & =  \sum_{t=1}^T \frac{1}{(1+r)^t} \\
             & = \left[\frac{1}{(1+r)} - \frac{1}{(1+r)^{T+1}}  \right] \sum_{t=0}^\infty \frac{1}{(1+r)^t}
              = \left[\frac{1}{(1+r)} - \frac{1}{(1+r)^{T+1}}  \right] \frac{1}{1 - (1+r)^{-1}} \\
             & = \left[\frac{1}{(1+r)} - \frac{1}{(1+r)^{T+1}}  \right] \frac{1+r}{1 + r  - 1}  = \frac{1}{r} \left[1  - \frac{1}{(1+r)^{T}}  \right]
  \end{align*}

\end{frame}

\begin{frame}
  \frametitle{Example: Rooftop photovoltaic unit}

  For our example with $r= 0.05$
  \begin{align*}
    NPV & = -80,000 + (10,000 - 2,000)  \cdot \frac{1}{r} \left[1  - \frac{1}{(1+r)^{T}}  \right] \\
    & = -80,000 + 8,000*12.5 \\
    & = 19,698
  \end{align*}

  \alert{Conclusion:} It's worthwile to invest in the photovoltaic unit!

  \pause

  NB: The calculation is very sensitive to the interest rate, e.g. with $r = 0.08$
  \begin{align*}
    NPV & = -80,000 + 8,000*9.8 \\
    & = -1,454
  \end{align*}

  \alert{Conclusion:} The investment is not worthwhile.

\end{frame}

\begin{frame}
  \frametitle{Return On Investment (ROI)}

  The expected return or \alert{Return On Investment (ROI)} is the required interest rate to reach the point $NPV = 0$.

  In our example you can either experiment or use the Newton-Raphson algorithm to determine the ROI $r$
  \begin{equation*}
    0 = NPV = -I_0 + (U - V - B) \sum_{t=1}^T \frac{1}{(1+r)^t}
  \end{equation*}

  In our example we find an ROI of $r=7.75$\%.

\end{frame}

\begin{frame}
  \frametitle{German example figures for electricity production technologies in 2018}

  WACC is the \alert{Weighted Average Cost of Capital} over the bank interest rate for borrowed capital (Fremdkapital) and the investor's ROI on their own investment (Eigenkapital).

  \centering
  \includegraphics[width=14cm]{stromgestehungskosten.png}

  \source{\href{https://www.ise.fraunhofer.de/content/dam/ise/de/documents/publications/studies/DE2018_ISE_Studie_Stromgestehungskosten_Erneuerbare_Energien.pdf}{Fraunhofer ISE Stromgestehungskosten 2018}}

\end{frame}

\begin{frame}
  \frametitle{Warning: Discounting over long time periods}

  Over long time periods the discounting can have a very large effect....

  \begin{columns}[T]
\begin{column}{9cm}
  \centering
  \includegraphics[width=9.5cm]{discounting.pdf}
\end{column}
\begin{column}{5cm}
  \begin{itemize}
  \item Long-term benefits aren't seen, e.g. long production life of nuclear power plants or benefits of long-lived efficiency measures
  \item Long-term costs are also suppressed, e.g. decommissioning, waste disposal, climate damages
  \item This is a \alert{controversial topic!}
  \end{itemize}
\end{column}
  \end{columns}

\end{frame}

\begin{frame}
  \frametitle{Programming example: photovoltaic plant}

  \centering
  \includegraphics[width=10.5cm]{PV_programming_example.png}

\end{frame}

\begin{frame}
  \frametitle{Programming example: nuclear plant}

  \centering
  \includegraphics[width=10.5cm]{nuclear_programming_example.png}

\end{frame}

\begin{frame}
  \frametitle{Summary}

  \begin{itemize}
  \item Future income or costs are worth less from today's point of view
  \item To calculate the \alert{present value} give the \alert{interest rate} $r$, multiply the cash flow in year $t$ by the \alert{discount factor}  $\frac{1}{(1+r)^t}$
  \item To calculate the \alert{net present value (NPV)} for an investment, sum the present values of all income and costs
  \item If  $NPV > 0$, the investment is worthwhile compared to investing with interest rate $r$
  \item For two different investments, a higher NPV should be preferred
  \item \alert{Long-term} costs or benefits are \alert{suppressed} by discounting
  \end{itemize}

\end{frame}

\section{Levelised Cost Of Electricity (LCOE)}

\begin{frame}
  \frametitle{Levelised Cost Of Energy (LCOE)}

  You can also solve for the market value or feed-in tariff that's
  necessary to cover all the costs of the investment, i.e. the point
  where the present value of all income balances the present value of
  all costs.  You solve for the price $\lambda$ such that
  \begin{equation*}
   0 = NPV = -I_0 + (\lambda Q - oQ - B) PVF(r,T)
  \end{equation*}
  (using $V = oQ$). We find:
  \begin{equation*}
    \lambda = \frac{1}{Q} \left( \frac{I_0}{PVF(r,T)} + B + oQ \right) = \frac{1}{Q} \left( \frac{I_0}{PVF(r,T)} + B  \right) + o
  \end{equation*}

  In our example we find a price of $\lambda =$ 89 \euro/MWh for $i=0.05$. % One could reduce the feed-in tariff from 100 \euro/MWh to this value!

  This value corresponds to the average long-term costs of the unit, since we've divided the total yearly costs by the total production $Q$. It is called the
the \alert{Levelised Cost Of Energy (LCOE)}.

  It is also called the  \alert{Long-Run Marginal Cost (LMRC)}, since we've added to the short-run marginal cost $o$ an annualised contribution to the capital cost and the operating costs.

  Check: The higher $I_0$ or $B$ are, the higher the LCOE. The higher $Q$ is, the lower the LCOE.
\end{frame}

\begin{frame}
  \frametitle{Annuity}

  The \alert{annuity} is the annualised investment cost $a = \frac{I_0}{PVF(r,T)}$ and
  $a(r,T) = \frac{1}{PVF(r,T)}$ is the \alert{annuity factor}, which spreads the capital costs $I_0$ evenly over the operational years of the investment (like a mortgage for a house).

  For a loan $I_0$ from the bank, the bank is compensated for the \alert{opportunity cost} of investing elsewhere at a rate of $r$ by an annual fixed sum $a$ so that the NPV for the bank is zero
  \begin{equation*}
    0 = NPV  = -I_0 + \sum_{t=0}^T \frac{a}{(1+r)^t} = -I_0 + PVF(r,T) \frac{I_0}{PVF(r,T)}
  \end{equation*}

  The formula for the annuity factor is derived from that for the PVF:
  \begin{equation*}
     a(r,T)  = \frac{1}{PVF(r,T)} = \frac{ r}{ 1 - (1+r)^{-T}}
  \end{equation*}

\end{frame}

\begin{frame}[fragile]
  \frametitle{Examples of annuity factor}

  \begin{columns}[T]
\begin{column}{6.5cm}

  AF = Annuity Factor, $a(r,T)$
  \begin{table}[!t]
	\centering
	\begin{tabular}{@{}rrr@{}}
\toprule
Lifetime $T$  &  Discount Rate $r$ & AF $a(r,T)$ \\
years & \% & per unit \\
\midrule
20 & 0 & 0.05 \\
20 & 5 & 0.08 \\
20 & 10 & 0.12 \\
20 & 20 & 0.21 \\
40 & 0 & 0.025 \\
40 & 5 & 0.06 \\
40 & 10 & 0.10 \\
40 & 20 & 0.20 \\
\bottomrule
	\end{tabular}
\end{table}
\end{column}
\begin{column}{7.5cm}
  Things to notice:
  \begin{itemize}
  \item AF reduce to $1/T$ in limit $r \to 0$
  \item AF climbs steeply with $r$
  \item For long lifetimes, AF is similar to short lifetimes for high $r$ - in reality investors try to pay off investments faster than lifetime
  \item In reality, an investor would provide some capital themselves, e.g. 10-20\% of the capital cost, and borrow the rest from the bank. The weighted average of the investor's desired internal rate of return and that of the bank loan is the \alert{weighted average cost of capital} (WACC).
  \end{itemize}
\end{column}
  \end{columns}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Parameters for different generation technologies}

  Here are some typical investment and operational parameters projected for 2020:
  \vspace{-.5cm}
  \begin{table}[!t]
	\centering
	\begin{tabular}{@{}lrrrrrrr@{}}
\toprule
Source   & Lifetime & Capital Cost &  Fix O\&{}M &  Var O\&{}M & $\eta$ & Fuel Cost & Marg. Cost \\
& years & \euro kW$^{-1}$  & \euro kW$^{-1}$a$^{-1}$ &  \euro MWh$_{\textrm{el}}^{-1}$ & [\%] & \euro/MWh$_{\textrm{th}}$ & \euro/MWh$_{\textrm{el}}$ \\
\midrule
Hard Coal    & 40 & 1200 & 30 & 6 & 39 & 10 & 32 \\
Gas OCGT     & 30 & 400  & 15 & 3 & 39 & 20 & 54 \\
Gas CCGT     & 30 & 800  & 20 & 4 & 60 & 20 & 37 \\
Nuclear      & 40-60 & 6000  & 0 & 6 & 33 & 3.3 & 16 \\
Wind Onshore & 25 & 1240 & 35 & 0 & & 0 & 0 \\
Solar PV     & 25 & 750  & 25 & 0 & & 0 & 0 \\
\bottomrule
	\end{tabular}
\end{table}
O\&{}M = Operation and Maintenance, Var. = Variable, Fix. = Fixed, $\eta$ = efficiency

For a plant with capacity $G_s$ in MW and yearly production $Q$ in MWh$_{\textrm{el}}$, we have $I_0 = 1000 \cdot G_s \cdot (\textrm{Capital Cost})$, $B = 1000\cdot G_s \cdot (\textrm{Fix O\&{}M})$, $V = Q\cdot o$ where o is the marginal cost $o =  (\textrm{Marg. Cost}) = (\textrm{Var O\&{}M}) + (\textrm{Fuel Cost})/\eta$.

\source{\href{https://www.diw.de/documents/publikationen/73/diw_01.c.424566.de/diw_datadoc_2013-068.pdf}{DIW Data Documentation, 2013}}
\end{frame}

\begin{frame}
  \frametitle{LCOE for dispatchable generators depends on capacity factor}

  The LCOE had the form (Marg. Cost) + (Yearly Fixed Costs)/(Yearly Production). Therefore it decreases with increasing capacity factor:

  \begin{columns}[T]
    \begin{column}{8.5cm}
      \vspace{.5cm}
  \centering
  \includegraphics[width=8cm]{lcoe_cf.pdf}
\end{column}
\begin{column}{6.5cm}
  \begin{itemize}
  \item LCOE $>$ marginal cost
  \item LCOE starts high then reduces as fixed costs are spread over more hours
  \item There are \alert{crossing points} where some types of generators become cheaper for a given capacity factor
  \item NB: All generators need downtime for regular maintenance, so cf $< 0.9$
  \item NB: Carbon pricing would alter this graphic by adding to the marginal cost
  \end{itemize}
\end{column}
  \end{columns}

\end{frame}

\begin{frame}{LCOE for wind and solar depends on location: worldwide auction results 2017}

  \centering
  \includegraphics[width=11.5cm]{baringa-auctions.png}

  \source{\href{https://www.baringa.com/getmedia/99d7aa0f-5333-47ef-b7a8-1ca3b3c10644/Baringa_Scottish-Renewables_UK-Pot-1-CfD-scenario_April-2017_Report_FINA/}{Baringa Partners LLP 2017}}

\end{frame}

\begin{frame}[fragile]
  \frametitle{Levelised Cost of Electricity Since 2009 in US}

  NB: Treat with care since LCOE doesn't take account of time or place of generation!

  \centering
  \includegraphics[width=14cm]{lazard-cropped.jpg}

  \source{\href{https://www.lazard.com/perspective/levelized-cost-of-energy-2017/}{Lazard's LCOE Analysis V11}}

\end{frame}

\section{Multi-horizon investment: Motivation}

\begin{frame}
  \frametitle{Short-run efficiency}

  Short-run efficiency is concerned with the \alert{efficient operation} of the existing energy system, assuming that the capacities of all investments are fixed.
  \begin{columns}[T]
    \begin{column}{7.5cm}

      \alert{Example}: Power plant dispatch for inelastic demand $d$. All capacities $G_s$ [MW] are fixed. We optimise the \alert{dispatch} $g_s$ [MW], assuming that the \alert{marginal costs} $o_s$ [\euro/MWh] scale linearly with the dispatch. We minimise \alert{total operational costs}:
      \begin{equation*}
        \min_{\{g_s\}} \sum_s o_s g_s
      \end{equation*}
      with constraints
  \begin{align*}
    \sum_s g_s & = d\hspace{1cm} \leftrightarrow \hspace{1cm} \l \\
        g_s  & \leq  G_s  \hspace{1cm}\leftrightarrow\hspace{1cm} \bar{\m}_s \\
    - g_s  & \leq  0  \hspace{1cm}\leftrightarrow\hspace{1cm} \ubar{\m}_s
  \end{align*}
    \end{column}
    \begin{column}{6.5cm}

        \vspace{.5cm}
        %https://www.forum-csr.net/News/7186/Marktwirtschaftparadox.html
        \includegraphics[trim=0 0cm 0 0cm,width=7cm,clip=true]{merit-order-effekt_neu1.png}

    \end{column}
  \end{columns}

\end{frame}

\begin{frame}
  \frametitle{Long-run efficiency}

  Long-run efficiency is concerned with the \alert{efficient operation} and \alert{the efficient dimensioning of investments} in the energy system.

  \begin{columns}[T]
    \begin{column}{7.5cm}

      \alert{Example}: Power plant \alert{dispatch} $g_{s,t}$ (costs $o_s$) and \alert{capacities} $G_s$ (annualised costs $c_s$) are optimised over a year of hourly time periods $t$ with demand $d_t$:
      \begin{equation*}
        \min_{\{g_{s,t},G_s\}} \sum_{s,t} o_s g_{s,t} + \sum_s c_s G_s
      \end{equation*}
      with constraints
  \begin{align*}
    \sum_s g_{s,t} & = d_t\hspace{1cm} \leftrightarrow \hspace{1cm} \l_t \\
        g_{s,t}   & \leq  G_s  \hspace{1cm}\leftrightarrow\hspace{1cm} \bar{\m}_{s,t} \\
    - g_{s,t}  & \leq  0  \hspace{1cm}\leftrightarrow\hspace{1cm} \ubar{\m}_{s,t}
  \end{align*}
    \end{column}
    \begin{column}{7cm}

  \begin{tikzpicture}
\node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=6cm]{screening-load-duration-clean}};
\draw (1.8,4.0) node{$c_2$};
\draw (1.8,5.2) node{$c_1$};
\draw (2.5,3.3) node{$\theta_2$};
\draw (4.8,3.3) node{$\theta_1$};
\draw (2.5,0.1) node{$\theta_2$};
\draw (4.8,0.1) node{$\theta_1$};
  \end{tikzpicture}

    \end{column}
  \end{columns}

\end{frame}

\begin{frame}
  \frametitle{Multi-horizon investment}

  Dynamic multi-horizon investment is concerned with the \alert{changing capacities of investments} in the energy system over many years or even \alert{decades}.

  At which point in time should we invest in renewables/gas/storage?

  We consider several time horizons, typically years, in which plants can be dismantled or built.

  Why are we concerned with changes over decades?

  \pause

  Since many aspects of the energy system change over decades, e.g.:
  \begin{itemize}
  \item \alert{Energy consumption} (particularly in developing countries)
  \item \alert{Resource scarcity} (scarcity of oil, cobalt, rare earth metals, etc.)
  \item \alert{Political targets} (e.g. reduction of greenhouse gas emissions)
  \item \alert{Technology maturity, costs and other parameters} (e.g. efficiency)
  \item \alert{Economic growth}
  \item \alert{Behavioural change} (car sharing, less flying, online gaming, etc.)
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Example: political targets}

  \includegraphics[width=14cm]{co2_targets.png}

  \source{Agora Energiewende}

\end{frame}

\begin{frame}
  \frametitle{Example: Net-Zero Emissions by 2050}

  Paris-compliant 1.5$^\circ$~C scenarios from European Commission - \alert{net-zero GHG in EU by 2050}

  \vspace{.4cm}

  %Figure 6 from https://ec.europa.eu/clima/sites/clima/files/docs/pages/com_2018_733_en.pdf
  \includegraphics[width=14cm]{eu-lts-net-zero.png}

    \source{\href{https://ec.europa.eu/clima/sites/clima/files/docs/pages/com_2018_733_en.pdf}{European Commission `Clean Planet for All', 2018}}
\end{frame}

\begin{frame}
  \frametitle{Example: Cost Developments of Renewable Energy}

  LCOE = \alert{Levelised Cost of Energy} = Total Costs $/$ Energy Output
  \centering
  \includegraphics[width=14cm]{lazard-cropped.jpg}

  \source{\href{https://www.lazard.com/perspective/levelized-cost-of-energy-2017/}{Lazard's LCOE Analysis V11}}

\end{frame}

\section{Multi-horizon investment: Theoretical formulation}

\begin{frame}
  \frametitle{Discounted Total Costs}

  We will consider the total costs over \alert{multiple years} $a = 1,\dots A$.

  How do we compare costs in 2020 to those in 2040?

  \pause

  The totals costs are expressed in their
  \alert{present value} using the
 \alert{discount rate} $r$, to allow comparison between different years.

 For costs (or income) in year $a$ we \alert{discount} the costs with a factor
  \begin{equation*}
    \frac{1}{(1+r)^a}
  \end{equation*}
  because we could have invested until this year $a$ with return $r$.

  Costs in the future are \alert{worth less} from today's point of view.

  For rate $r$ we optimised the \alert{discounted total costs}
  \begin{equation*}
     \sum_{a=1}^A \frac{1}{(1+r)^a} \left\{ \textrm{Total costs in year } a \right\}
  \end{equation*}

\end{frame}

\begin{frame}
  \frametitle{Warning: Discounting over long time periods}

  Over long time periods the discounting can have a very large effect....

  \begin{columns}[T]
\begin{column}{9cm}
  \centering
  \includegraphics[width=9.5cm]{discounting.pdf}
\end{column}
\begin{column}{5cm}
  \begin{itemize}
  \item Long-term benefits aren't seen, e.g. long production life of nuclear power plants or benefits of long-lived efficiency measures
  \item Long-term costs are also suppressed, e.g. decommissioning, waste disposal, climate damages
  \item This is a \alert{controversial topic!}
  \end{itemize}
\end{column}
  \end{columns}

\end{frame}

\begin{frame}
  \frametitle{Example of Electricity System until 2050}

We optimise the discounted total costs over 30 years from 2021 to 2050
  \begin{equation*}
              \min_{\{g_{s,t,a},Q_{s,a},G_{s,a}\}} \sum_{a=1}^A \frac{1}{(1+r)^a} \left\{ \sum_{s,t} o_{s,a} g_{s,t,a} + \sum_{s,b} c_{s,b} Q_{s,b} \mathbb{I}(a  \geq b) \mathbb{I}(a < b+L_s)
 \right\}
  \end{equation*}
  Here $Q_{s,a}$ is the new capacity built in year $a$, $G_{s,a}$ is the total capacity available in year $a$, $L_s$ is the lifetime  and $\mathbb{I}$ is an indicator function that is $1$ if the condition is fulfilled, $0$ otherwise. $Q_{s,a}$ may also have fixed values for $a<1$ to represent existing capacity. $Q_{s,a}$ and $G_{s,a}$ are related by
  \begin{equation*}
    G_{s,a} = \sum_{b = 1}^{L_s} Q_{s,a-b}
  \end{equation*}

  The old constraints apply for each year $a$
  \begin{align*}
    \sum_s g_{s,t,a} & = d_{s,a}\hspace{1cm} \leftrightarrow \hspace{1cm} \l_{t,a} \\
        g_{s,t,a}   & \leq  G_{s,a}  \hspace{1cm}\leftrightarrow\hspace{1cm} \bar{\m}_{s,t,a} \\
    - g_{s,t,a}  & \leq  0  \hspace{1cm}\leftrightarrow\hspace{1cm} \ubar{\m}_{s,t,a}
  \end{align*}

\end{frame}

\begin{frame}
  \frametitle{Global constraints}

  With a long-term perspective we can now set exciting constraints.

  For example, we can restrict total \alert{emissions} over the period:
  \begin{equation*}
    \sum_{s,t,a} e_i g_{s,t,a}\leq \textrm{CAP}_{\textrm{CO}_2}
  \end{equation*}
  where $e_s$ is the specific emissions of technology $s$ (tonnes of CO$_2$ per MWh$_{\textrm{el}}$).

  Or limit \alert{resource consumption} for a technology $s$:
  \begin{equation*}
    \sum_{t,a} g_{s,t,a}\leq \textrm{CAP}_{s}
  \end{equation*}
\end{frame}

\begin{frame}
  \frametitle{Learning effects}

  Technology costs sink with accumulated manufacturing experience, particularly for new immature technologies.

  We promote $c_{s,a}$ to an optimisation variable that depends on the cumulative generator capacity.

 A simple \alert{one-factor learning model} for the costs is
  \begin{equation*}
    c_{s,a} = c_{s,0} \left(\sum_{b=1}^a Q_{s,b} \right)^{-\gamma_s}
  \end{equation*}
  where $c_{s,0}$ is the initial cost, $Q_{s,b}$ is the capacity produced in year $b$ and $\gamma_s$ is the \alert{learning parameter}.

  The \alert{learning rate} $LR$ is the reduction in cost for every doubling of production
  \begin{equation*}
    LR_s = 1-2^{-\gamma_s}
  \end{equation*}

  Example for photovoltaics: $\gamma = 0.33$ $\implies$ if cumulative production doubles, the costs reduce by 20\% (\alert{Swanson's Law}).

\end{frame}

\begin{frame}
  \frametitle{Swanson's Law for photovoltaic modules}

  The underlying dynamic is a fast decay in costs with deployment (\alert{learning-by-doing}).

  \vspace{.2cm}

  \centering
      \includegraphics[width=9.5cm]{1024px-Swansons-law.png}
\end{frame}

\begin{frame}
  \frametitle{More complicated learning models}

  In the literature there are more sophisticated learning models than the one-factor model, e.g.
  \begin{itemize}
  \item \alert{Multi-component learning models}: different parts of the
    cost experience different learning rates, e.g. some parts of the
    cost do not experience learning, such as fixed material and labour
    costs, call it $c_{s,\textrm{base}}$. Only the remainder
    experiences learning:
  \begin{equation*}
    c_{s,a} = c_{s,\textrm{base}} + (c_{s,0}-c_{s,\textrm{base}}) \left(\sum_{b=1}^a Q_{s,b} \right)^{-\gamma_s}
  \end{equation*}
  In the case of PV, $c_{s,\textrm{base}}$ would include e.g. the labour costs of installation.
  \item \alert{Multi-factor learning models}: the cost depends not just on the cumulative capacity, but on other factors such as knowledge stock $KS$ through research and development
  \begin{equation*}
    c_{s,a} = c_{s,0}\left(\sum_{b=1}^a Q_{s,b} \right)^{-\gamma_{s,1}} \left(\sum_{b=1}^a KS_{s,b} \right)^{-\gamma_{s,2}}
  \end{equation*}

  \end{itemize}

\end{frame}

\section{Multi-horizon investment: Simplified example}

\begin{frame}
  \frametitle{Simplified example}

  \url{https://nworbmot.org/courses/esm-2020/lectures/notebooks/dynamic_investment.ipynb}

  Time period: 2021 until 2070. Discount rate: $r = 0.05$.

  Constant electricity demand $d_{t,a} = d = 100$ GW.

  At the start of the simulation there is already 100~GW of 20-year-old coal plants.

  3 generation technologies are available that are dispatchable (for Concentrating Solar Power (CSP) need good direct solar insolation, e.g. New Mexico or Morocco).

  \ra{1.1}
  \begin{table}[!t]
    \begin{tabular}{lrrrrrr}
      \toprule
      Tech & Capital costs & Marg. costs & LCOE & Cap & Emissions & Lifetime    \\
       &  (\euro MW$^{-1}$ a$^{-1}$) &  (\euro MWh$_{\textrm{el}}^{-1}$) &  (\euro MWh$_{\textrm{el}}^{-1}$) & factor & (tCO$_2$MWh$_{\textrm{el}}^{-1}$) & years \\
      \midrule
      Coal &30*8760 & 20 & 50 & 1 & 1 & 40\\
      Nuclear & 65*8760 & 10 & 75 & 1 & 0 & 40 \\
      CSP & 150*8760 & 0 & 150 & 1 & 0 & 30 \\
      \bottomrule
    \end{tabular}
  \end{table}

\end{frame}

\begin{frame}
  \frametitle{Simplified example}

  Since each technology can generate continuously and the demand is constant, we assume $g_{s,t,a}$ is constant for all $t$
   \begin{equation*}
    g_{s,t,a} = g_{s,a} \leq G_{s,a}
   \end{equation*}

  This simplifies the optimisation problem considerably:
  \begin{equation*}
              \min_{\{g_{s,t,a},Q_{s,a},G_{s,a}\}} \sum_{a=1}^A \frac{1}{(1+r)^a} \left\{ \sum_{s} o_{s,a} g_{s,a}\cdot 8760 + \sum_{s,b} c_{s,b} Q_{s,b} \mathbb{I}(a  \geq b) \mathbb{I}(a < b+L_s)
 \right\}
  \end{equation*}
  with constraints for each year $a$
  \begin{align*}
    \sum_s g_{s,a} & = d
  \end{align*}
\end{frame}

\begin{frame}
  \frametitle{Vanilla Version: No CO$_2$ budget, no learning, no discounting}

  Only new coal is built, since it's cheapest.

  Total costs without discounting: 50\euro/MWh $\cdot$ 8760 $\cdot$ 100 GW $\cdot$ 50 years = 2190 billion \euro

    \centering
  \includegraphics[width=11cm]{no_co2-no_learning-dispatch.pdf}
\end{frame}

\begin{frame}
  \frametitle{Vanilla Version: No CO$_2$ budget, no learning, discounting}

  Only coal is built, since it's cheapest.

  Total costs with discount rate 5\%: 840 billion \euro

   \centering
  \includegraphics[width=11cm]{no_co2-no_learning-dispatch.pdf}

\end{frame}

\begin{frame}
  \frametitle{CO$_2$ budget, no learning, discounting}

  Limit CO$_2$ to 20\% of coal emissions. Nuclear takes over before coal lifetimes are finished. Why is it built only later in the period (even when no existing plants assumed)? (Hint: discounting)

  Total costs with discount rate 5\%: 1147 billion \euro

  \centering
  \includegraphics[width=10cm]{co2-0p2-no_learning-dispatch.pdf}

\end{frame}

\begin{frame}
  \frametitle{CO$_2$ budget, learning for CSP, discounting}

  Limit CO$_2$ to 20\% of coal emissions. CSP has learning rate 20\%, $\gamma = 0.33$, and a base long-term potential LCOE of 35~\euro/MWh that represents material and labour costs.

  Total costs with discount rate 5\%: 1020 billion \euro

  \centering
  \includegraphics[width=10cm]{co2-0p2-learning-dispatch.pdf}

\end{frame}

\begin{frame}
  \frametitle{CO$_2$ budget, learning for CSP, discounting}

  LCOE needs subsidy initially to push down learning curve, since it is more expensive than incumbent technologies. But from 2034 onwards it is the most competitive technology.

  \centering
  \includegraphics[width=11cm]{co2-0p2-learning-lcoe.pdf}

\end{frame}

\begin{frame}
  \frametitle{Lessons from this example}

  \begin{itemize}
  \item Non-linear effects such as learning-by-doing make the results hard to predict
  \item It may be cost-effective in the long-run to subsidise technologies that are uncompetitive today
  \item Depending on how subsidy and policy is arranged, there could be \alert{path dependencies}
  \end{itemize}

  To improve the realism of this example we need to:
  \begin{itemize}
  \item Include more technologies, spatial resolution
  \item Consider more representative times per year to capture the variability of renewables and load
  \end{itemize}

\end{frame}

\end{document}


\documentclass[letterpaper,12pt]{article}

\usepackage{alltt}
\usepackage{amsfonts}
\usepackage{amsmath}
\allowdisplaybreaks[1]%0-4 4 is most permissive of breaking equations
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{array}
\usepackage{calc}
\usepackage{color}
\usepackage{graphicx}
\usepackage{stmaryrd}
\usepackage{supertabular}
\usepackage{url}%Wanted by the bibilography
\usepackage[all]{xy}

%Note: Throughout the preamble there are occassional references to pages out of
%books. Unless otherwise noted they are from The LaTeX Companion 2nd ed by
%Mittelbach and Goossens

%######################################
%Font Selection.
%Concrete
%\usepackage{beton}
%\usepackage{euler}
%Computer Modern Bright
%\usepackage[T1]{fontenc}
%\usepackage{cmbright}
%######################################

%######################################
%Quote environment
\newsavebox{\quauth}
\newenvironment{Quotation}[1]
	{\sbox{\quauth}{\textit{#1}}\begin{quote}}
	{\hspace*{\fill}\nolinebreak[1]\hspace*{\fill}\usebox{\quauth}\end{quote}}
%######################################

%######################################
%Theorem setup, needs package amsthm
%Theorems
\theoremstyle{plain}
\newtheorem{thm}{Theorem}[section]
\newtheorem{algo}[thm]{Algorithm}
\newtheorem{cor}[thm]{Corollary}
\newtheorem{lem}[thm]{Lemma}
\newtheorem{prop}[thm]{Proposition}

%Definitions
\theoremstyle{definition}
\newtheorem{appl}[thm]{Application}
\newtheorem{conj}[thm]{Conjecture}
\newtheorem{defn}[thm]{Definition}
\newtheorem{exmp}[thm]{Example}
\newtheorem{exer}[thm]{Exercise}

%Remarks
\theoremstyle{remark}
\newtheorem{rem}[thm]{Remark}
\newtheorem{note}[thm]{Note}
\newtheorem*{case}{Case}
\newtheorem*{claim}{Claim}

%For referencing Theorems/Defs/etc...
\providecommand{\thmref}[1]{ (Thm. \ref{#1})}
\providecommand{\defref}[1]{ (Def. \ref{#1})}
\providecommand{\exref}[1]{Example \ref{#1}}
%######################################

%######################################
%Define a few column types for convenience
%Needs package array

%These make the columns all be in math mode
%The extra %stopzone are to make vim do syntax
%highlighting correctly with this code, otherwise
%it chokes
\newcolumntype{C}{>{$}c<{$}}
%stopzone%stopzone%stopzone
\newcolumntype{L}{>{$}l<{$}}
%stopzone%stopzone%stopzone
\newcolumntype{R}{>{$}r<{$}}
%stopzone%stopzone%stopzone
%######################################

%######################################
%Various shortcuts
%Some may need package amssymb

%Shortcuts for various sets
\providecommand{\A}{\ensuremath{\mathfrak{A}}}%ideal A
\providecommand{\B}{\ensuremath{\mathcal{B}}}%basis
\providecommand{\C}{\ensuremath{\mathbb{C}}}%complex
\providecommand{\F}{\ensuremath{\mathbb{F}}}%finite field
\providecommand{\N}{\ensuremath{\mathbb{N}}}%natural
\providecommand{\RI}{\ensuremath{\mathcal{O}}}%ring of integers
\providecommand{\p}{\ensuremath{\mathfrak{P}}}%prime ideal
\providecommand{\Q}{\ensuremath{\mathbb{Q}}}%rationals
\providecommand{\R}{\ensuremath{\mathbb{R}}}%reals
\providecommand{\T}{\ensuremath{\mathcal{T}}}%topology
\providecommand{\Z}{\ensuremath{\mathbb{Z}}}%integers
\providecommand{\Zpos}{\ensuremath{\mathbb{Z}^{+}}}%positive integers

%Miscellaneous shortcuts
\providecommand{\lcm}{\ensuremath{\text{lcm}}}%least common multiple
\providecommand{\st}{\ensuremath{\backepsilon}}
\providecommand{\ud}{\ensuremath{\,\,\mathrm{d}}}%For derivatives
%######################################

%######################################
% Various operators
% some may need package amsmath

%Algebra
\providecommand{\amod}{\ensuremath{\diagup}}%Algebraic mod
\providecommand{\embed}{\ensuremath{\hookrightarrow}}%inclusion map
\providecommand{\gen}[1]{\ensuremath{\left<#1\right>}}%Group generated by #1
\providecommand{\idx}[2]{\ensuremath{\left[#1:#2\right]}}%index of #2 in #1
\providecommand{\im}{\ensuremath{\mathrm{im}}}%Image
\providecommand{\iso}{\ensuremath{\simeq}}%isomorphism
\providecommand{\normal}{\ensuremath{\vartriangleleft}}%Normal subgroup
\providecommand{\subgp}{\ensuremath{\le}}%Sub group
\providecommand{\vect}[1]{\ensuremath{\left\langle#1\right\rangle}}%Vector

%Logic
\providecommand{\land}{\ensuremath{\wedge}}
\DeclareMathSymbol\lnot{\mathbin}{symbols}{"3A}%p528
\providecommand{\lor}{\ensuremath{\vee}}
\providecommand{\lxor}{\ensuremath{\oplus}}

%Number Theory
\providecommand{\jacobi}[2]{\ensuremath{\left(\frac{#1}{#2}\right)}}
\providecommand{\legendre}[2]{\ensuremath{\left(\frac{#1}{#2}\right)}}

%Set Theory
\providecommand{\intersect}{\ensuremath{\bigcap}}
\providecommand{\less}{\ensuremath{\diagdown}}
\providecommand{\union}{\ensuremath{\bigcup}}

%Miscellaneous
\providecommand{\abs}[1]{\ensuremath{\left\lvert#1\right\rvert}}
\providecommand{\card}[1]{\ensuremath{\left\lvert#1\right\rvert}}
\providecommand{\ceiling}[1]{\ensuremath{\left\lceil#1\right\rceil}}
\providecommand{\define}{\ensuremath{\stackrel{\text{\tiny def}}{=}}}
\providecommand{\floor}[1]{\ensuremath{\left\lfloor#1\right\rfloor}}
\providecommand{\norm}[1]{\ensuremath{\left\lVert#1\right\rVert}}%Analysis norm
\providecommand{\order}[1]{\ensuremath{\left\lvert#1\right\rvert}}

%Asymptotic
\providecommand{\Oh}{\ensuremath{\mathcal{O}}}
\providecommand{\oh}{\ensuremath{o}}
%######################################
%######################################
%Page layout
% If using hyperef, load it before this block
\usepackage[paper=letterpaper,tmargin=1in,bmargin=42pt,lmargin=1in,rmargin=1in,headheight=30pt,headsep=30pt,footskip=20pt]{geometry}
% Note 1in = 72pt, therefore bmargin=42pt is 1in - 30pt
\usepackage{ifpdf}
\ifpdf
  \geometry{driver=pdftex}
\else
  \geometry{driver=dvips}
\fi

\usepackage{fancyhdr}
\lhead{}
\chead{PPRL Things}
\rhead{}
\lfoot{}
\cfoot{}
\rfoot{\thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
%######################################

\begin{document}
\pagestyle{fancy}
This document contains various bits relevant to the PPRL project

\section{Notation}
\begin{itemize}
\item $m$ - length of Bloom Filter in bits (not always standard, sometimes $m$, sometimes $n$)
\item $k$ - number of hash functions in the bloom filter (standard as $k$)
\end{itemize}

\section{Expected number of elements in a Bloom Filter}
In a Bloom Filter with $B$ bits set, the number of elements expected, $N$, is
\begin{equation*}
  N \approx - m \ln [1 - B/m]/k
\end{equation*}

Wikipedia claims proved in Swamidass \& Baldi (2007):

Swamidass, S. Joshua; Baldi, Pierre (2007), "Mathematical correction for fingerprint similarity measures to improve chemical retrieval", Journal of chemical information and modeling (ACS Publications) 47 (3): 952–964

There is almost certainly a better/earlier source on this.

The proof of this does assume each of the $N$ elements flipped on $k$ bits, which is not completely true and this bound can be made better taking into account collisions of bits of the elements put into the filter.

\end{document}