\section{Algebraic Integers}
\subsection{The Gaussian Integers}
Recall the ring of Gaussian integers $\mathbb Z[i]=\{a+bi:a,b\in\mathbb Z\}\le\mathbb C$ is a ED due to the norm $N(a+bi)=a^2+b^2$.
Hence $\mathbb Z[i]$ is a PID hence UFD.
In particular irreducibles and primes are the same.
The units in $\mathbb Z[i]$ are $\pm 1,\pm i$ by the norm $N$.
By convention, primes in $\mathbb Z$ are positive (since others are associates to them), but there is no corresponding convention in the Gaussian Integers.
\begin{lemma}
    If $\pi\in\mathbb Z[i]$ is prime, then there is a unique prime $p\in\mathbb Z$ with $\pi|p$.
\end{lemma}
\begin{proof}
    Since $\pi$ is nonzero and nonunit, we can write $N(\pi)=p_1\cdots p_n$ where $p_i\in\mathbb Z$ are (not necessarily distinct) primes.
    But $\pi|\pi\bar\pi=N(\pi)=p_1\cdots p_n$, so there is some $i$ such that $\pi|p_i$.\\
    For uniqueness, if $\pi|p,\pi|q$ with $p,q$ distinct primes in $\mathbb Z$, then there is some $a,b\in\mathbb Z$ such that $ap+bq=1$, so $\pi|1$, so $\pi$ is a unit, contradiction.
\end{proof}
\begin{lemma}
    Let $p\in\mathbb Z$ be a prime in $\mathbb Z$, then the followings are equivalent:\\
    1. $p$ is not prime in $\mathbb Z[i]$.\\
    2. $p$ can be written as the sum of two squares.\\
    3. $p=2$ or $p\equiv 1\pmod{4}$.
\end{lemma}
\begin{proof}
    $1\implies 2$: Write $p=xy$ where $x,y\in\mathbb Z[i]$ are not unit, then $p^2=N(p)=N(x)N(y)$.
    But $x,y$ are not unit hence have $\ge 1$ norm, therefore $N(x)=N(y)=p$, so $p$ is the sum of two squares.\\
    $2\implies 3$: Obvious.\\
    $3\implies 1$: $2=(1-i)(1+i)$, so $2$ is not prime.
    Otherwise, for $p\equiv 1\pmod{4}$, then $-1=x^2\pmod{p}$ is solvable, so $p|(x+i)(x-i)$.
    If $p$ is prime in $\mathbb Z[i]$, then either $p|x+i$ or $p|x-i$, none of which can happen.
\end{proof}
\begin{theorem}
    1. Every prime $p\equiv 1\pmod{4}$ in $\mathbb Z$ is the sum of two integer squares $p=a^2+b^2$.\\
    2. Primes in the Gaussian Integers (up to associates) are $1+i$, primes in $\mathbb Z$ which congruent to $3\bmod{4}$, and $a\pm bi$ where $a,b$ are as in 1.
\end{theorem}
\begin{proof}
    1. The preceding lemma.\\
    2. Let $\pi\in\mathbb Z[i]$ be prime in $\mathbb Z[i]$, then $\pi|p$ for some prime $p\in\mathbb Z$.
    If $p\equiv 3\pmod{4}$, then $p$ is prime in $\mathbb Z[i]$ and $\pi,p$ are associates.\\
    Otherwise, $p=(a+ib)(a-ib)$ by the preceding lemma, so each of $a\pm ib$ has norm $p$ hence is prime, so $\pi$ is associate to one of $a\pm ib$.
    Note that $1+i,1-i$ are associates but $a\pm ib$ are not associates otherwise by simple calculation.
    This completes the proof.
\end{proof}
\begin{corollary}
    Any integer $n\ge 1$ is a sum of two squares iff every prime factor $p$ of $n$ with $p\equiv 3\pmod{4}$ divides $n$ to an even power.
\end{corollary}
\begin{proof}
    $\exists a,b\in\mathbb Z, n=a^2+b^2$ if and only if $n=N(x)$ for some $x\in\mathbb Z[i]$, which happens iff $n$ is a product of norms of primes in $\mathbb Z[i]$, but by preceding theorem, the norms of primes in $\mathbb Z[i]$ are either primes in $\mathbb Z$ or the squares of primes congurent to $3\bmod 4$.
\end{proof}
\begin{example}
    $65=5\cdot 13$, so $65$ is a sum of two squares.
    Indeed $65=1+64=1+8^2$ does it.
    Another way to get this sum is to write $65=(2+i)(2-i)(2+3i)(2-3i)=|(2+i)(2+3i)|^2=|1+8i|^2=1+8^2$.
    We also have $65=|(2+i)(2-3i)|^2=|7-4i|^2=7^2+4^2$.
\end{example}
\subsection{Algebraic Integers}
\begin{definition}
    Suppose $R\le S$ are rings.
    Given $\alpha\in S$, we write $R[\alpha]$ for the smallest subring of $S$ containing both $R$ and $\alpha$.
\end{definition}
We know that such a subring exists since we can collect all the candidates and take their intersection.
In fact, $R[\alpha]=\phi(R[X])$ where $\phi:R[X]\to S$ is the evaluation homomorphism $f(X)\mapsto f(\alpha)$.
\begin{definition}
    If $R\le S$ are fields and $\alpha\in S$, we write $R(\alpha)$ to denote the smallest subfield of $S$ containing $R$ and $\alpha$.
\end{definition}
So $R(\alpha)$ is simply the field of fraction of $R[\alpha]$.
\begin{definition}
    1. $\alpha\in\mathbb C$ is an algebraic number if $\exists f\in\mathbb Q[X]\setminus\{0\},f(\alpha)=0$.\\
    2. $\alpha\in\mathbb C$ is an algebraic integer if $\exists f\in\mathbb Z[X]$ such that the leading coefficient of $f$ is $1$ with $f(\alpha)=0$.
\end{definition}
Let $\alpha$ be an algebraic number and $\phi:\mathbb Q[X]\to\mathbb C$ be the evaluation $f[X]\mapsto f(\alpha)$, then since $\mathbb Q[X]$ is a PID, $\ker\phi=(f)$ for some $f\in\mathbb Q[X]$.
Since $\alpha$ is an algebraic number, $f\neq 0$.
We may assume that $f$ is monic (since $\mathbb Q$ is a field), so we say $f$ is the minimal polynomial of $\alpha$.
By the isomorphism theorem, we have $\mathbb Q[X]/(f)\cong \mathbb Q[\alpha]\le\mathbb C$.\\
But $\mathbb Q[\alpha]$ is hence a integral domain, which means that $f$ is prime (hence irreducible as $\mathbb Q[X]$ is a UFD), therefore $(f)$ is maximal (since $\mathbb Q[X]$ is a PID), which means that $\mathbb Q[\alpha]=\mathbb Q(\alpha)$.
\begin{lemma}
    Let $\alpha$ be an algebraic number with minimal polynomial $f\in\mathbb Q[X]$.
    Write $f=\lambda f_0$ where $\lambda\in\mathbb Q^\times$ and $f_0\in\mathbb Z[X]$ is primitive.
    Then the ring homomorphism given by $\phi:\mathbb Z[X]\to\mathbb C$ by $g(X)\mapsto g(\alpha)$ has $\ker\phi=(f_0)$.
\end{lemma}
\begin{proof}
    Clearly $\phi(f_0)=f_0(\alpha)=\lambda^{-1}f(\alpha)=0$, so $(f_0)\subset\ker\phi$.
    Suppose we have some other $g\in\ker\phi$, then $f|g$ in $\mathbb Q[X]$ and hence $f_0|g$ in $\mathbb Q[X]$, but then $f_0|g$ in $\mathbb Z[X]$ by Lemma \ref{primitive_div_fof}, therefore $g\in(f_0)$.
\end{proof}
Suppose further that $\alpha$ is an algebraic integer, then $\ker\phi=(f_0)\lhd\mathbb Z[X]$.
But by definition of algebraic integer $(f_0)$ contains a monic polynomial, which must mean that one of $\pm f_0$ is monic.
We have $f=\lambda f_0$ with the assumption that $f$ is monic, so $\lambda=\pm 1$, so $f\in\mathbb Z[X]$.
Consequently, we get $\mathbb Z[X]/(f_0)=\mathbb Z[X]/(f)\cong \mathbb Z[\alpha]\le\mathbb C$.
\begin{example}
    $i,\sqrt{2},(-1+\sqrt{3})/2,\sqrt[n]{p}$ are all algebraic integers and indeed their minimal polynomials are $X^2+1,X^2-2,X^2+X+1,X^n-p$.
    In particular, $\mathbb Z[X]/(X^2+1)\cong\mathbb Z[i]$.
\end{example}
\begin{lemma}
    An algebraic number $\alpha\in\mathbb C$ is an algebraic integer if and only if its minimal polynomial (which by convention is monic) has integer coefficients.
\end{lemma}
\begin{proof}
    Immediate.
\end{proof}
\begin{corollary}
    If $\alpha$ is an algebraic integer and $\alpha\in\mathbb Q$, then $\alpha\in\mathbb Z$.
\end{corollary}
\begin{proof}
    By preceding lemma.
\end{proof}


\lab{Applications}{Scheduling}{Scheduling}
\label{lab:Scheduling}
\objective{In this lab, you will determine the schedule that optimizes preferences and reduces spending for nurses in a hospital.}

\section*{Scheduling}

Scheduling is typically an NP-hard combinatorial problem.
It can take many forms, from scheduling teachers and classrooms at a university to scheduling jobs in a queue.
The nurse scheduling problem is highly constrained, which makes it difficult for local search alogrithms to find even a local solution.
Finding optimal solutions is much harder.
We will use a Baysian approach via linear programming to find the optimal solution.

\subsection*{The Problem}
Hospitals employ a lot of nurses around the clock.
Weekly schedules have to satisfy several constraints.
First, each nurse has a certain grade, or level, depending on education, experience and expertise.
At any given time, there must be a minimum number of nurses at each grade.
These numbers depend on how busy the hospital is and what types of procedures they perform.
For example, a hospital in Manhattan will need more higher grade nurses at night than the Utah Valley Regional Medical Center due to the larger population and higher crime rate.

Second, the working contracts of each nurse is different, with over $400$ possible shift schedules in a two-week pay period.
Most contracts require that the nurse works either all nights or days in a given period, but there are some exceptions for special nurses.
More experienced or specialized nurses have a higher grade, which means that they can take over lower grade shifts, but not vice versa.
Since grades are not independent, one can't schedule the grades separately.
Also complicating the schedule, nurses usually do not work the same number of day shifts as night shifts.

The third constraint is that schedules must be fair, or appear fair to the nurses.
An obvious aspect is that nurses' requests must be taken into consideration.
If a nurse is stuck with a shift schedule he/she hates, they will be very unhappy and cause problems.
Also included in this is vacation; the hospital still has to meet the requirements when nurses use vacation days.
Since hospitals operate constantly, there are less desirable shifts.
As mentioned above, this can be problematic when these shifts are always assigned to the same nurses.
So the schedule mu evenly distribute unpopular shifts so that nurses don't get angry.

Finally, the hospital wants to maximize revenue by minimizing the amount of money spent on nurses.

In order to solve this problem, we'll make some assumptions that will simplify it.

The first assumption is that nurses are assigned a shift pattern.
Shift patterns can be represented by a $14$-dimensional vector where the first $7$ entries represent the day shifts, and the second $7$ entries represent the nights.
Each nurse is given a set of allowable shift patterns based off their contract.
For example,
\begin{center}
$(1,1,1,1,0,0,0,0,0,0,0,0,0,0)$
\end{center}
is an allowable shift pattern, which indicates that the nurse works $4$ consecutive days and then has $3$ days off.
\begin{center}
$(1,1,0,0,0,0,0,1,1,0,0,0,0,0)$
\end{center}
is not allowed because the nurse would work for over $30$ hours straight.

The second assumption involves taking into account the nurses' preferences.
This can be tricky since we have to have some way to measure and compare preferences.
We will not worry about that here.

For each nurse and their allowable shift patterns, we assign a preference.

\subsection*{Linear Program}
How can we formulate this as an optimization problem?

The preference of a nurse $i$ working a given shift pattern $j$ takes into account the nurse's contract and the fairness aspect by assigning a number to that nurse a pattern.
The number can be seen as the cost for assigning nurse i shift pattern j.
Thus a high cost indicates that the nurse should probably not work that pattern.
In creating the schedule, the goal is then to minimize the cost of possible schedules.

We have data for $30$ nurses, $70$ possible shift schedules, and $2$ grades.
$3$ of the nurses are grade $1$ and the rest are grade $3$.

We can formulate the problem as an integer program in the following manner.

Indices:
\begin{itemize}
\item i = 1...n = 30, nurse index
\item j = 1...m = 70, shift pattern index
\item k = 1...14, day and night index where 1-7 are days and 8-14 are nights
\item s = 1...p = 3, grade index
\end{itemize}

Decision Variables:
\begin{displaymath}
   x_{ij} = \left\{
     \begin{array}{lr}
       1, & \text{nurse i works pattern j}\\
       0, & \text{else}
     \end{array}
   \right.
\end{displaymath}

Parameters:
\begin{itemize}
\item n = Number of nurses
\item m = Number of shift patterns
\item p = Number of grades
\item $p_{ij}$ = Preference cost of nurse i working pattern j
\item $R_{ks}$ = Demand of nurses with grade s on day/night k
\item F(i) = Set of feasible work patterns for nurse i
\item \begin{displaymath}
%\begin{flalign*}
   a_{jk} = \left\{
     \begin{array}{lr}
       1, & \text{shift pattern j covers day/night k}\\
       0, & \text{else}
     \end{array}
   \right.
\end{displaymath}
\item \begin{displaymath}
   q_{is} = \left\{
     \begin{array}{lr}
       1, & \text{nurse i is of grade s or higher}\\
       0, & \text{else}
     \end{array}
   \right.
\end{displaymath}
%\end{flalign*}
\end{itemize}

Now set up the integer program that describes this problem.

Objective Function:

Minimize the preference cost for all nurses
\begin{center}
min $\displaystyle\sum_{i=1}^{n} \displaystyle\sum_{j\in F(i)}^{m} p_{ij}x_{ij}$
\end{center}

Constraints:

1) Each nurse works exactly one feasible shift pattern
\begin{center}
$\displaystyle\sum_{j\in F(i)} x_{ij} = 1 \forall i$
\end{center}

2) The demand of nurses for each grade is met for every shift
\begin{center}
$\displaystyle\sum_{j\in F(i)} \displaystyle\sum_{i=1}^n q_{is}a_{jk}x_{ij} \geq R_{ks} \forall k,s$
\end{center}

\textbf{Problem}
Solve this optimization problem using cvxopt.glpk.ilp and the data included in this lab.
Assume that $F(i)$ consists of all possible $70$ shifts for each nurse $i$.

Hints:

Remember that all data must be a float.
$G$ should be a $4242$ by $2100$ matrix.
$A$ should be a $30$ by $2100$ matrix.

\subsection*{The Data}
Preferences.csv contains the level of each nurse, as well as their shift preferences.
The $30$ nurses are represented by rows.
The first column is their level, an integer between $1$ and $3$.
The next $70$ columns represent each nurse's preference for that shift with an integer between $0$ and $50$.
The higher the number, the less desirable that shift is.

SchedulingData.csv is the list of $70$ shift schedules.
Each row is an allowable shift schedule and the $14$ columns represent the $7$ days and $7$ nights.
A $1$ indicates a working shift, while a $0$ represents an off shift, just like in the examples above.

Demands.csv is the list of the number of required nurses for each level.
It has one column with $42$ rows.
The first $14$ rows represent the number of level $1$ nurses for each of the $14$ shifts, the second $14$ rows represent the level $2$ nurses, and the last rows are the number of level $3$ nurses.

All of these files are comma delimited.

%This solution doesn't necessarily minimize the payout to the nurses, so we need to adapt the problem above. For example, it may be more effective for a nurse to work overtime instead of hiring a new nurse. ? other example
%Each nurse is assigned a salary or wage. Calculate the new schedule to minimize cost.
%Does it differ from part 1?


\documentclass[12pt, titlepage, oneside]{article}

\input{settings}

\begin{document}

	\textbf{ELECENG 3TQ3}\\
	\textbf{Elston A.}

	\section{Lecture 3}

	\items
 \item Probability measure ? For example absolute value is a measure that maps set of real numbers into the set of nonnegative real numbers
 \item Probability is a function that maps events in sample space to real numbers so that
 \item For any event A probability of event A is nonnegative $P[A] \geq 0$
 \item Probability of sample space is 1
\item For any countable collection $A1,A2,...$ of mutually exclusive events the probability of their union set is equal to sum of individual probabilities $P[A1 \u A2 \u ... \u An] = P[A1] + P[A2] + \dots + P[An]$
\eitems

\subsection{Axioms of Probability}

A probability model assigns a number between 0 and 1 to every event. The probability of the union of mutually exclusive events is the sum of the probabilities of the probabilities of the events in the union.

A probability measure $P[\cdot]$ is a function that maps events in the sample spaces to non-negative real numbers such that:

\b{Axiom 1}: For any event $A$, $P[A] \geq 0$.

\b{Axiom 2}: $P[S] = 1$

\b{Axiom 3}: For any countable collection $A1,A2,...$ of mutually exclusive events
\begin{align}
P[A1 \u A2 \u \dots ] = P[A1] + P[A2] + \dots
\end{align}

\subsection{Theorems of Probability}

\b{Theorem 1.2}: For mutually exclusive events $A1$ and $A2$.
\begin{align}
P[A1 \u A2] = P[A1] + P[A2]
\end{align}

\b{Theorem 1.3}: If $A = A1 \u A2 \u \dots \u Am$ and $Ai \n Aj = \emptyset$ for $i \neq j$, then
\begin{align}
P[A] = \sum_{i=1}^m P[Ai]
\end{align}

\b{Theorem 1.4}: The probability measure $P[\cdot]$ satisfies
\items
\item $P[\emptyset] = 0$
\item $P[A^c] = 1-P[A]$
\item For any $A$ and $B$ (not necessary mutually exclusive)
\begin{align}
P[A \u B] = P[A] + P [B] - P[A\n B] = P[A] + P[A^c \n B]
\end{align}
\item If $A \subset B$, then $P[A] \leq P[B]$
\eitems

\b{Theorem 1.5}: The probability of an event $B = \{s_1, s_2, \dots, s_m\}$ is the sum of the outcomes contained in the event
\begin{align}
P[B] = \sum_{i=1}^{m} P[\{s_i\}]
\end{align}
The skeleton for this proof is to see that $B = \{s_1\} \u \{s_2\} \u \dots \u \{s_n\}$ where $\{s_i\} \n \{s_j\} = \emptyset$ if $i \neq j$. Then we apply theorem 1.3.

\ex Let $s_i$ be the outcome of the event of tossing a coin 4 times. $s_i$ is a 4 letter word describing the 4 tosses. To find the probability of getting $hhht$ or $hhth$ we simply need to add the probability of those individual events.

\subsection{Equally Likely Outcomes}
If we believe no outcome is more likely than any other. From the axioms of probability this implies that every outcomes has probability $1/n$.
\begin{align}
P[s_i] = \frac{1}{n}, \enspace \enspace 1 \leq i \leq n.
\end{align}

\ex Consider a 6 sided fair die. What is the probability of getting a number larger than 4?

Note that our favorable outcomes is the event set $A = \{5,6\}$.
\begin{align}
P[A] = P[\{5\}] + P[\{6\}] = 1/6 + 1/6 = 1/3
\end{align}

\subsection{Conditional Probability}

Conditional probability refers to the modified probability model that reflects partial information about the outcome of an experiment. The modified probability has a smaller sample space than the original model.

If we have some knowledge of an event $A$ prior to performing the experiment ($P[A]\approx 1$ or $P[A] \approx 0$ or $P[A]\approx 0.5$) then we call $P[A]$ the priori probability or the prior probability of $A$.

When we want to state a conditional probability, that is, the probability based on the condition that our knowledge on some other probability is true, we denote this as
\begin{align}
P[A|B]
\end{align}
We read this as "The probability of A given B". So the priori probability is $P[B]$.

\b{Definition 1.5}: The conditional probability of the event $A$ given the occurrence of the event $B$ is
\begin{align}
P[A|B] =  \frac{P[A \n B]}{P[B]}
\end{align}
The probability of A given B is the probability of A and B divided by the probability of B.

\b{Theorem 1.7}: A conditional probability measure $P[A|B]$ has the following properties that correspond to the axioms of probability

\b{Axiom 1}: $P[A|B] \geq 0$

\b{Axiom 2}: $P[B|B] = 1$

\b{Axiom 3}: If $A = A1 \u A2 \u \dots $ with $Ai \n Aj = \emptyset$ for $i \neq j$ then,
\begin{align}
P[A|B] = P[A1|B] + P[A2|B] + \dots
\end{align}

\ex Lets roll two six sided die. Let $X1$ be the number of dots on he first die and let $X2$ be the number of dots on the second die.

Let $A$ be the event that $X1 \geq 4$.

Let $B$ be the event that $X2 \ge X1 + 1$

Find $P[B]$ and $P[A|B]$

The probability to get a number greater than 4 on the first die is $P[A] = 3/6$.

$B = \{ (1,3), (1,4), (1,5), (1,6), (2,4), (2,5), (2,6), (3,5), (3,6), (4,6)\} $

$B \n A = {(4,6)}$

The probability to get a number on die 2 greater than the number on die 1 plus 1 is$P[B] = 10/(6*6)$

The probability for both occurring $P[B|A] = P[B \n A]/P[A] = (1/36)/(3/6) = 1/18 = 0.05$

\subsection{Partitions and The Law of Total Probability}
A partition divides the sample space into mutually exclusive sets. The law of total probability expresses the probability of an event as the sub of the probabilities of the outcomes that are in the separate sets of a partition.

\b{Theorem 1.10}: Law of Total Probability: For a partition $\{B_1,B_2, \dots, B_n\}$ with $P[B_i] > 0$ for all $i$.
\begin{align}
P[A] = \sum_{i=1}^{m} P[A|B_i] P[B_i]
\end{align}

\b{Theorem 1.11}: Bayes Theorem
\begin{align}
P[B|A] = \frac{P[A|B] P[B]}{P[A]}
\end{align}
\end{document}


\documentclass[a4paper,12pt]{article}

\usepackage{graphicx}
\usepackage{amssymb}

\title{Probabilistic Spiking Neuron Model}
\author{Saptarshi Soham Mohanta\\
Indian Institute of Science Education and Research, Pune\\
Maharashtra - 411008}

\begin{document}
\maketitle

\section*{Model Description}
The dynamic variable for this model of spiking neuron is the vector $\vec{\theta}$. Let us consider a system of n neurons connected via excitatory and inhibitory connections.

Let $\vec{\theta}_t$ be the n-dimensional vector of firing probabilities of each of the n neurons at the time t. By the definition of probability, each element of this vector is bounded by 0 and 1. In absence of any current input to the neurons, the probability of firing would go down as the activity decays to equilibrium. Let $\lambda$ be the rate for the exponential decay.

Thus in absence of current input (external and synaptic), $\vec{\theta}$ will follow the dynamical equation:
$$\vec{\theta}_{t+1} = \lambda \vec{\theta}_t$$

But at each time step = t, we perform a sampling event to evaluate the firing of the neurons. Let $\langle\vec{\theta}\rangle_t$ be the result of the sampling event. This means that  $[\langle\vec{\theta}\rangle_t]_i$ is the result of a binary coin toss with $P(1) = [\vec{\theta}_t]_i$ and $P(0) = 1-[\vec{\theta}_t]_i$, where $i = 1,2,3...n$.

But whenever the neuron fires, the probability of firing again is set to zero. Thus the final dynamical equation without current input becomes

$$\vec{\theta}_{t+1} = \lambda (1-\langle\vec{\theta}\rangle_t) \vec{\theta}_t$$

We initialize the values of $\vec{\theta}_t$ with values from a uniform random distribution and then follow the system by the rules. This will give a system of non interacting neurons that have a dynamic probability of firing.

Now we model the interactions and the current response. Let us define excitatory and inhibitory connectivity matrices $\mathbf{[E]_{n\times n}}$ and  $\mathbf{[I]_{n\times n}}$ with the horizontal axis representing the pre-synaptic neuron and the vertical axis the post-synaptic neuron. We also define a function $I_{ext}(t):\mathbb{R}\rightarrow\mathbb{R}^n$ that gives us the external current input to each neuron at time t. We also define two parameters $e$ and $i$ that describe the excitation and inhibition coupling coefficient respectively. Finally, we define a response function based on Mirollo Strogatz Model, $U:\mathbb{R}\rightarrow\mathbb{R}$ which has the following properties:
$$U'(x)>0\ and\ U''(x)<0,\forall x \in [0,1]$$
$$U(0)=0\ and\ U(1)=1$$

We will use this function to update the probability based on current input. Say, the current input is $\vec{\epsilon}$ where each element $\epsilon_i \in [-1,1]$ or any other bound that depends on the range of the response function over which the response is variable. We define the new probability using the function $H:\mathbb{R}^n\rightarrow\mathbb{R}^n$ where

$$[H(\vec{x},\vec{\epsilon})]_i= [U^{-1}(U(x_i)+\epsilon_i)]_i\ \forall i\in \{1,2,3...n\}$$

One such function that satisfies these properties is:
$$U_b(x)= \frac{1}{b}ln(1+(e^b-1)x)$$

This function is numerically and analytically useful because the function $H_b(\vec{x},\vec{\epsilon})$ for $U_b(x)$ becomes an affline linear map

$$H_b(\vec{x},\vec{\epsilon}) = e^{b\epsilon}x + \frac{e^{b\epsilon}-1}{e^b-1}$$

Now, given a synaptic response function $F:\mathbb{R}\rightarrow[0,1]$, we define the synaptic current $I^{syn}$ at time t+1 as

$$I_{syn}(t+1) = e.F(\mathbf{E}\times \langle\vec{\theta}\rangle_{t}) - i.F(\mathbf{I}\times \langle\vec{\theta}\rangle_{t})$$

For simplicity, we can use the linear response function
$$F(x)=x/n$$

Now we define the dynamical equation with current input as
$$\vec{\theta}_{t+1} = H_b(\ \lambda (1-\langle\vec{\theta}\rangle_t) \vec{\theta}_t\ ,\ I_{ext}(t+1) + I_{syn}(t+1)\ )$$

This model can now be simulated.

\end{document}


\newpage
\section{Notes on Section 4}

\subsubsection{Explaining Lemma 4.1}

A proof of Lemma 4.1 can also be found in the appendix of the original paper. However,
the relation between the norms is missing in the original paper - that is why we state it here again.
From 3.5 in the paper, we had:
\begin{align}
	\frac{1}{(L+1)^2} \pqnorm{\theta}{fr}{2} = \E{v^\TT X X^\TT v} = \E{\norm{f_\theta}^2}.
\end{align}
Remember that the output of the network, $f_\theta(x)$, is a vector in $\setreal^k$ of probabilities for each of the $k$ classes.
For the Frobenius Norm of a Matrix $A$ and a vector $x$ the following holds.
\begin{align}
	\frobnorm{A} &\geq \spectralnorm{A} \\
	\frobnorm{x} &= \lnorm{x} = \spectralnorm{x} \\
	\spectralnorm{Ax} &\leq \spectralnorm{A} \cdot \lnorm{x} \\
\end{align}
It follows.
\begin{align}
	\E{\spectralnorm{f_\theta}^2}
	&=
		\E{ \spectralnorm{f_\theta}^2} \\
	&=
		\E{ \spectralnorm{\structuredNN}^{2} } \\
	&\leq
		\E{ \spectralnorm{x}^2 \prod \spectralnorm{D^i(x)}^2 \prod \spectralnorm{W^i}^2 }.
\end{align}
Since $W^i$ is independent of the data $x$, it does not have to be inside the expectation.
\begin{align}
\frac{1}{(L+1)^2} \frnorm{\theta}^2
	&=
		\E{\spectralnorm{f_\theta}^2}  \\
	&\leq
		\E{ \spectralnorm{x}^2 \prod \spectralnorm{D^i(x)}^2 \prod \spectralnorm{W^i}^2 } \\
	&=
		\E{ \lnorm{x}^2 \prod_{t=1}^{L+1} \spectralnorm{D^t(x)}^2 } \prod_{t=0}^{L} \spectralnorm{W^i}^2
\end{align}
Now taking the root on both sides reveals Lemma 4.1 and concludes the explainiation.

\begin{figure}[htb]
	\centering
	\includegraphics[width=\textwidth]{whiteboard_notes/18.jpg}
\end{figure}

\begin{figure}[htb]
	\centering
	\includegraphics[width=\textwidth]{whiteboard_notes/19.jpg}
\end{figure}

\begin{figure}[htb]
	\centering
	\includegraphics[width=\textwidth]{whiteboard_notes/20.jpg}
\end{figure}

\begin{figure}[htb]
	\centering
	\includegraphics[width=\textwidth]{whiteboard_notes/21.jpg}
\end{figure}

\begin{figure}[htb]
	\centering
	\includegraphics[width=\textwidth]{whiteboard_notes/22.jpg}
\end{figure}

\begin{figure}[htb]
	\centering
	\includegraphics[width=\textwidth]{whiteboard_notes/23.jpg}
\end{figure}

\begin{figure}[htb]
	\centering
	\includegraphics[width=\textwidth]{whiteboard_notes/24.jpg}
\end{figure}

\begin{figure}[htb]
	\centering
	\includegraphics[width=\textwidth]{whiteboard_notes/25.jpg}
\end{figure}


\documentclass[11pt]{article}

\usepackage{latexsym}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{pseudocode}
\usepackage{url}
\usepackage[backref, colorlinks=true, citecolor=red, urlcolor=blue, pdfauthor={Jyh-Ming Lien}]{hyperref}

\newcommand{\handout}[5]{
  \noindent
  \begin{center}
  \framebox{
    \vbox{
      \hbox to 5.78in { {\bf } \hfill #2 }
      \vspace{4mm}
      \hbox to 5.78in { {\Large \hfill #5  \hfill} }
      \vspace{2mm}
      \hbox to 5.78in { {\em #3 \hfill #4} }
    }
  }
  \end{center}
  \vspace*{4mm}
}

\newcommand{\lecture}[4]{\handout{#1}{#2}{#3}{#4}{#1}}

\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{observation}[theorem]{Observation}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{claim}[theorem]{Claim}
\newtheorem{fact}[theorem]{Fact}
\newtheorem{assumption}[theorem]{Assumption}

% 1-inch margins, from fullpage.sty by H.Partl, Version 2, Dec. 15, 1988.
\topmargin 0pt
\advance \topmargin by -\headheight
\advance \topmargin by -\headsep
\textheight 8.9in
\oddsidemargin 0pt
\evensidemargin \oddsidemargin
\marginparwidth 0.5in
\textwidth 6.5in

\parindent 0in
\parskip 1.5ex
%\renewcommand{\baselinestretch}{1.25}

\begin{document}

\lecture{Assignment3 Report}{Fall 2017}{Genqian Hu}{Computational Geometry}

\section{Summary of the two methods}

\subsection{hedcuter method}
\subsubsection{Voronoi diagram and Centroidal Voronoi tessellation}
\paragraph{First, the algorithm resizes the input image to a larger virtual image. Then, it scans the cells and stores all sites in a heap. After, it pops a cell to compute dx and dy so to get the centroidal voronoi diagram. When all cells are visited and the heap is empty, it collects cells from (0,0) rightwards and then remove empty cells. Finally, it moves each cell to its center of its coverage.}
\subsubsection{stippling}
\paragraph{It reads the image and the centroidal voronoi tessellation computed before to stipple. It retrieves each cell from CVT and its grayscale information. Then, the location of each disk is defined by the x and y coordinates of each sites of the cells retrieved. The color of it is determinded by grayscale value. If it is required a colorful output, then the rgb value is from the original image's rgb value at that pixel.}
\subsection{voronoi method}
\paragraph{First, it computes the range of the coordinates of the sites. Then do the plane sweep, if the sites' y coordinate is bigger than the lowest intersection's, it determines which half edge the new site intersects with and will triangulate it. If the sites' y is smaller, it creates a new bisector between the left and right half edges. Finally, it scans the edges and eliminates the invalid edges. }
\subsubsection{Centroidal Voronoi tessellation}
\paragraph{It adds clip lines to the voronoi digrams one by one. If the density is higher there, the x and y there will also get larger weight when computing the new centroid. If no points are outside the clip planes,the centroid is computed by the sum of coordinates and the density there. }
\subsubsection{stippling}
\paragraph{It read the coordinates of all the centroids created before. Then it outputs them to the output file. And it assigns each of them the right color as needed.}
\section{Comparison of the two methods}
\begin{figure}[h!]
\centering
\includegraphics[width=0.80\textwidth]{figure1.eps}
\caption{Different outputs from hedcuter}
\label{threadsVsSync}
\end{figure}
\paragraph{1. The output images from hedcuter are different as shown in Figure 1. But the output images from voronoi are the same. Because hedcuter uses rng\_uniorm and rng\_gaussian methods to randomly initial points, the details is normal to be different during each run.}

\begin{figure}[h!]
\centering
\includegraphics[width=0.80\textwidth]{figure2.eps}
\caption{Outputs from hedcuter}
\label{threadsVsSync}
\end{figure}
\begin{figure}[h!]
\centering
\includegraphics[width=0.80\textwidth]{figure3.eps}
\caption{Outputs from voronoi}
\label{threadsVsSync}
\end{figure}
\paragraph{2. The overall distribution will be the same for both methods. As Figure 2 and Figure 3 shows, the distribution remains the same. The difference here is that the one with more disks seems to be darker, because its density of disks is higher. And following these 2 methods, darker places deserve more disks. So the distribution should always be the same.}

\begin{figure}[h!]
\centering
\includegraphics[width=0.80\textwidth]{figure4.eps}
\caption{Outputs from hedcuter}
\label{threadsVsSync}
\end{figure}
\begin{figure}[h!]
\centering
\includegraphics[width=0.80\textwidth]{figure5.eps}
\caption{Outputs from voronoi}
\label{threadsVsSync}
\end{figure}
\paragraph{3. As Figure 4 and 5 show, the voronoi method is always much faster than the other one when parameters are the same. And I think the reason hedcuter method is slower that much is because it generates the same virtual image repetitively. So, I take it as one of my improvement and the result is not bad. And other reasons are voronoi precomputes the sets of distribution and merge them at runtime. But hedcuter method is doing plane sweep from points with small x coordinates which is time consuming.}

\begin{figure}[h!]
\centering
\includegraphics[width=0.80\textwidth]{figure6.eps}
\caption{Outputs from hedcuter}
\label{threadsVsSync}
\end{figure}
\begin{figure}[h!]
\centering
\includegraphics[width=0.80\textwidth]{figure7.eps}
\caption{Outputs from hedcuter}
\label{threadsVsSync}
\end{figure}
\begin{figure}[h!]
\centering
\includegraphics[width=0.80\textwidth]{figure8.eps}
\caption{Outputs from hedcuter}
\label{threadsVsSync}
\end{figure}
\paragraph{4. As figure 6, 7 and 8 show, for both methods, the same picture with smaller size consumes less time, because less pixels mean less iteration. For hedcuter method, it runs much faster with the low contrast picture. Because lowering contrast makes the picture darker. And when the picture is getting darker and darker, its distribution of disks becomes more uniform. Thus, the propagation task is achieved faster.  }

\begin{figure}[h!]
\centering
\includegraphics[width=0.80\textwidth]{figure9.eps}
\caption{Outputs from hedcuter}
\label{threadsVsSync}
\end{figure}
\begin{figure}[h!]
\centering
\includegraphics[width=0.80\textwidth]{figure10.eps}
\caption{Outputs from hedcuter}
\label{threadsVsSync}
\end{figure}
\paragraph{5. From Figure 9 and 10, different types of images have the approximate total time for voronoi method. But for hedcuter method, it performs better on landscape picture. I think it's because the landscape picture I chose is relatively darker than other types of pictures. So the reason is like for the pictures with low contrast value.  }

\begin{figure}[h!]
\centering
\includegraphics[width=3in]{figure11.eps}
\caption{hedcut image from Wall Street Journal}
\label{threadsVsSync}
\end{figure}
\begin{figure}[h!]
\centering
\includegraphics[width=0.80\textwidth]{figure12.eps}
\caption{Outputs from hedcuter}
\label{threadsVsSync}
\end{figure}
\paragraph{6. I think hedcut images from the Wall Street Journal are better than images generated by these 2 methods. As shown in Figure 11, it has smooth boundaries and more artistic distribution of disks. Figure 12 is generated by voronoi method from a 200x200 picture with 2000 disks. Although viewers are able to tell what's in the hedcut picture, its distribution of disks is not as natural as the one from Wall Street Journal.}
\section{Improvement of hedcuter method}
\begin{figure}[h!]
\centering
\includegraphics[width=0.80\textwidth]{figure13.eps}
\caption{output from original method}
\label{threadsVsSync}
\end{figure}
\begin{figure}[h!]
\centering
\includegraphics[width=0.80\textwidth]{figure14.eps}
\caption{outputs from new one}
\label{threadsVsSync}
\end{figure}
\paragraph{The first improvement is that I reorganized same codes so that the workload for vor and move\_sites methods are reduced. According to the original code, it seems like  each loop these two methods will generate a virtual high resolution image with the same parameters which means there is no need to do it repetitively. And it's also time consuming. Then, I changed that part of code so that the virtual image generating will be done only once. Figure 13 shows the total time the original program used to output the result. Figure 14 shows the total time the improved program used. The total time has reduced.}
\begin{figure}[h!]
\centering
\includegraphics[width=0.80\textwidth]{figure15.eps}
\caption{comparison}
\label{threadsVsSync}
\end{figure}
\paragraph{The second improvement is that the program now can treat png files which contain transparent alpha channel correctly. The original version treats the transparent background as black which makes the output file weird. I modified main.cpp to make the input file lay on a white image. Fiqure 15 shows the comparison of the original program against the improved program with the same parameters. But when outputing colorful disks, the color of each disk is not always right. }
 \paragraph{Folder hedcuter2 has the codes for the first improvement. And folder hedcuter3 has the second improvement. And the output image makes more sense now.}
\bibliographystyle{plain}
\bibliography{report}

\end{document}


\section{Risk-Based Stochastic Capital Budgeting using Conditional Value-at-Risk}
\label{sec:CVaR}

Value-at-Risk (VaR) is currently used by finance businesses to indicate the percentiles
of loss distributions. For instance, $95\%$-VaR is an upper estimate of losses which
is exceeded with $5\%$ probability. The popularity of VaR is mostly related to a simple
and easy to understand representation of high losses. However, VaR may have undesirable
mathematical characteristics such as a lack of subadditivity and convexity~\cite{ThinkingCoherently,CoherentMeasureRisk} .
Another alternative percentile risk measure is called Conditional Value-at-Risk (CVaR),
which has more attractive properties than VaR, such as sub-additive and convex.
CVaR also called mean excess loss, mean shortfall, or tail VaR, is defined as the
expected loss exceeding VaR. In general, CVaR is the weighted average of VaR and
losses exceeding VaR. In this section, we will focus on using CVaR for capital
budgeting problem.

\subsection{Definitions of VaR and CVaR}
\label{definitionCVaR}
Let $X$ be a random variable with a cumulative distribution function
$F(z) = P{X\le z}$. It can be useful to think of $X$ as a ``loss'' or more generally
a variable such that large values need to be avoid. The VaR of $X$ with confidence level
$\alpha$ (e.g., $\alpha = 0.9$ is:

\begin{equation}
VaR_\alpha (X) = \min {z|F_X(z)\ge \alpha}
\end{equation}

which is equivalent to $VaR_\alpha (X) = F_{X}^{-1}(\alpha)$ if $X$ is a continuous
random variable. By this definition, $VaR_\alpha (X)$ is a (lower) $\alpha$-percentile
of the random variable $X$. An alternative measure of risk is CVaR. Here, $CVaR_\alpha (X)$
is the conditional expectation of $X$ given that $X \ge VaR_\alpha (X)$.
Figure~\ref{fig:CVaR} shows the relationship between these two measures of risk.

\begin{figure}
    \centering
    \centerline{\includegraphics[scale=0.5]{CVaR.jpg}}
    \caption{Relationship between value-at-risk and conditional value-at-risk.}
    \label{fig:CVaR}
\end{figure}

The typical definition of $CVaR_\alpha (X)$ is $CVaR_\alpha (X) = E{X|X > VaR_\alpha (X)}$.
There are alternative ways to define this measure, which are mathematically equivalent.
Rockafellar and Uryasev in~\cite{OptCVaR} (see also~\cite{RemarksCVaR}) defines CVaR as:

\begin{equation}
CVaR_\alpha (X) = \min_u {u + 1/(1-\alpha) E[X-u]^+}
\end{equation}

where $[X-u] = \max (X - u, 0)$. Here, variable $u$ is simply an auxiliary decision
variable whose optimal value turns out to be $CVaR_\alpha (X)$. The above definition
is particularly useful for computation in the context of optimization.

Researchers have argued for using CVaR over VaR as a measure of risk. Theoretically,
CVaR satisfies the assumptions of a so-called coherent risk measure, and VaR does not.
In simpler terms, minimizing VaR is concerned with the numerical value of the 95-th
percentile (say) of the loss, but it does not care about the magnitude of larger losses.
CVaR takes these magnitudes into account.

\subsection{CVaR in Capital Budgeting}
\label{CVaRCapitalBudgeting}
In this section, an explicit risk measure is constructed using a weighted
combination of expectation and CVaR. This approach allows us to parametrically
vary the weight on maximizing expected NPV versus penalizing solutions that yield
low-NPV scenarios, and we denote the weight by $\lambda$ with $0 \le \lambda \le 1$.
Let $NPV(s,\xi)$ denote the net present value under a prioritization decision
specified by decision $s$, and under a realization of the budget and profit of
each project, denoted by $\xi$. Then we seek to solve the following optimization
model:

\begin{equation}
\max_{s\in S} (1-\lambda)E[NPV(s, \xi)] - \lambda CVaR_\alpha [-NPV(s, \xi)]
\end{equation}

When $\lambda = 0$ the model reduces to stochastic optimization model as
discussed in Section~\ref{sec:StochasticCapitalBudgeting}; i.e., we seek
a prioritization decision, $s$, to maximize expected NPV, where ``$s \in S$''
simply indicates the constraints that a prioritized solution must satisfy.
$CVaR_\alpha [X]$ is typically applied to a random variable, $X$, which
represents a loss; i.e., we seek to avoid large values of $X$. In this
context, let $VaR_\alpha [X]$ denote the $\alpha$-level quantile of $X$.
Thus, if $\alpha = 0.75$ then $VaR_0.75 [X]$ is the value such that $75\%$
of the realizations of $X$ have lower values of loss. Suppose for simplicity
that $NPV(s, \xi)$ values are positive. Large values of $NPV(s, \xi)$ are
good, and hence large values of $-NPV(s, \xi)$ (i.e., those closer to zero)
are bad. Using the definition of $CVaR_\alpha [X] = E[X|X > VaR_\alpha [X]]$
we thus have that the conditional value-at-risk is the expected value of loss,
given that the loss exceeds a certain percentile. So, when $\lambda = 1$ we
seek to minimize the expected value of NPV given that they fall below a
threshold. More generally, values of $\lambda$ between 0 and 1 seek a trade-off
between reward and risk, captured by expected NPV and CVaR, respectively.

The full mathematical optimization model of CVaR for capital budgeting problem
is as follows:

\begin{subequations}\label{fullCVaR}
\begin{eqnarray}
& & \max_{x, y, \nu, u} (1-\lambda) \sum _{ \omega  \in  \Omega }^{}q^{ \omega } \sum _{i \in I}^{} \sum _{j \in J_{i}}^{}a_{ij}^{ \omega }x_{ij}^{ \omega } - \lambda[u+1/(1-\alpha)\sum_{\omega \in \Omega} q^\omega \nu^\omega] \\
& & \nu^\omega \ge - \sum _{i \in I}^{} \sum _{j \in J_{i}}^{}a_{ij}^{ \omega }x_{ij}^{ \omega } - u, \omega \in \Omega \\
& & y_{ii^{'}}+y_{i^{'}i} \geq 1,~ i<i^{'}\text{, i, }i^{'} \in I \\
& & \sum_{j=1}^{J_i} x_{ij}^\omega \geq \sum_{j=1}^{J_i} x_{i'j}^\omega + y_{ii'} -1,~ i \neq i^{'}\text{, i, }i^{'} \in I,  \omega  \in  \Omega \\
& & \sum _{i \in I}^{} \sum _{j \in J_{i}}^{}\text{~ c}_{ijkt}^{ \omega }x_{ij}^{ \omega }~  \leq  b_{kt}^{ \omega },~ k \in K, t \in T,  \omega  \in  \Omega \\
& & \sum_{j\in J_i} x_{ij}^{ \omega } \leq 1,~ i \in I, \omega  \in  \Omega \\
& & y_{ii'}, x_{ij}^\omega \in {0, 1} \\
& & \nu^\omega \ge 0, \omega \in \Omega
\end{eqnarray}
\end{subequations}

\subsection{LOGOS Settings for CVaR Problems}
\label{subsec:CVaRSettings}
CVaR approach is an extension for stochastic optimization approach discussed in
Section~\ref{sec:StochasticCapitalBudgeting}. Both of them share the same input
structures except the \xmlNode{Settings} block. In both cases, the user need to
specify a collection of scenarios via \xmlNode{Uncertainties} block. The
\xmlNode{problem\_type} within \xmlNode{Settings} block is used to select the
type of CVaR problems. The currently available CVaR problem types are:
\xmlString{cvarskp}, \xmlString{cvarmkp}, and \xmlString{cvarmckp}. The user can
use \xmlNode{risk\_aversion} (i.e. $\lambda$) and \xmlNode{confidence\_level}
(i.e., $\alpha$) to control CVaR problem.

Example LOGOS input XML for CVaR:
\begin{lstlisting}[style=XML]
<Settings>
<Logos>
  <solver>cbc</solver>
  <solverOptions>
    <StochSolver>EF</StochSolver>
    <risk_aversion>0.1</risk_aversion>
    <confidence_level>0.95</confidence_level>
  </solverOptions>
  <sense>maximize</sense>
  <problem_type>cvarskp</problem_type>
</Settings>
</Logos>
\end{lstlisting}

\subsection{CVaR for Single Knapsack Problem}
\label{subsec:CVaR_SKP}

\vst \noi {\em Model Formulation:}
\begin{subequations}\label{CVaRSimpleKP}
\begin{eqnarray}
& & \max_{x, y, \nu, u} (1-\lambda)  \sum _{ \omega  \in  \Omega }^{}q^{ \omega } \sum _{i \in I}^{} a_{i}^{ \omega }x_{i}^{ \omega } - \lambda[u+1/(1-\alpha)\sum_{\omega \in \Omega} q^\omega \nu^\omega] \\
& & \nu^\omega \ge - \sum _{i \in I}^{} a_{i}^{ \omega }x_{i}^{ \omega } - u, \omega \in \Omega \\
& & \sum_{i \in I} c_{i}^\omega x_{i}^\omega \leq b^\omega, \omega \in \Omega\\
& & y_{ii'} + y_{i'i} \geq 1, i<i'  \\
& & x_{i}^\omega \geq x_{i'}^\omega + y_{ii'}-1, i\neq i' \\
& & x_{i}^\omega, y_{ii'}^\omega \in {0, 1} \\
& & \nu^\omega \ge 0, \omega \in \Omega, \lambda \in [0, 1]
\end{eqnarray}
\end{subequations}

See next section~\ref{subsec:CVaR_DKP} for the example of LOGOS input file, since multi-dimensional Knapsack problem
is just a simple extension of a single-dimensional Knapsack problem, and both of them belong to the same
\xmlNode{problem\_type}: \xmlString{cvarskp}.

\subsection{CVaR for Multi-Dimensional Knapsack Problem}
\label{subsec:CVaR_DKP}

\vst \noi {\em Model Formulation:}
\begin{subequations}\label{CVaRMultiDKP}
\begin{eqnarray}
& & \max_{x, y, \nu, u} (1-\lambda)  \sum _{ \omega  \in  \Omega }^{}q^{ \omega } \sum _{i \in I}^{} a_{i}^{ \omega }x_{i}^{ \omega } - \lambda[u+1/(1-\alpha)\sum_{\omega \in \Omega} q^\omega \nu^\omega] \\
& & \nu^\omega \ge - \sum _{i \in I}^{} a_{i}^{ \omega }x_{i}^{ \omega } - u, \omega \in \Omega \\
& & \sum_{i \in I} c_{it}^\omega x_{i}^\omega \leq b_{t}^\omega, t\in T \\
& & y_{ii'} + y_{i'i} \geq 1, i<i'  \\
& & x_{i}^\omega \geq x_{i'}^\omega + y_{ii'}-1, i\neq i' \\
& & x_{i}^\omega, y_{ii'}^\omega \in {0, 1} \\
& & \nu^\omega \ge 0, \omega \in \Omega, \lambda \in [0, 1]
\end{eqnarray}
\end{subequations}

Example LOGOS input XML:
\begin{lstlisting}[style=XML]
<Logos>
  <Sets>
    <investments>
      1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
    </investments>
    <time_periods>
      1,2,3,4,5
    </time_periods>
  </Sets>

  <Parameters>
    <net_present_values index="investments">
      2.315,0.824,22.459,60.589,0.667,5.173,4.003,0.582,0.122,
      -2.870,-0.102,-0.278,-0.322,-3.996,-0.246,-20.155
    </net_present_values>
    <costs index="investments, time_periods">
      0.219,0.257,0.085,0.0,0.0,
      0.0,0.0,0.122,0.103,0.013,
      5.044,1.839,0.0,0.0,0.0,
      6.74,6.134,10.442,0.0,0.0,
      0.425,0.0,0.0,0.0,0.0,
      2.125,2.122,0.0,0.0,0.0,
      2.387,0.19,0.012,2.383,0.192,
      0.0,0.95,0.0,0.0,0.0,
      0.03,0.03,0.688,0.0,0.0,
      0,0.2,0.763,0.739,2.539,
      0.081,0.032,0,0,0,
      0.3,0,0,0,0,
      0.347,0,0,0,0,
      4.025,0.297,0,0,0,
      0.095,0.095,0.095,0,0,
      5.487,5.664,0.5,6.803,6.778
    </costs>
    <available_capitals index="time_periods">
      18,18,18,18,18
    </available_capitals>
  </Parameters>

  <Uncertainties>
    <available_capitals>
      <totalScenarios>10</totalScenarios>
      <probabilities>
        0.012, 0.019, 0.032, 0.052, 0.086, 0.142, 0.235, 0.188, 0.141, 0.093
      </probabilities>
      <scenarios>
        11, 11, 11, 11, 11,
        12, 12, 12, 12, 12,
        13, 13, 13, 13, 13,
        14, 14, 14, 14, 14,
        15, 15, 15, 15, 15,
        16, 16, 16, 16, 16,
        17, 17, 17, 17, 17,
        18, 18, 18, 18, 18,
        19, 19, 19, 19, 19,
        20, 20, 20, 20, 20
      </scenarios>
    </available_capitals>
  </Uncertainties>

  <Settings>
    <mandatory>10,11,12,13,14,15,16</mandatory>
    <solver>cbc</solver>
    <solverOptions>
      <StochSolver>EF</StochSolver>
      <!-- lambda for risk aversion -->
      <risk_aversion>0.1</risk_aversion>
      <!-- confidence level -->
      <confidence_level>0.95</confidence_level>
    </solverOptions>
    <sense>maximize</sense>
    <problem_type>cvarskp</problem_type>
  </Settings>
</Logos>
\end{lstlisting}

\subsection{CVaR for Multiple Knapsack Problem}
\label{subsec:CVaR_MKP}

\vst \noi {\em Model Formulation:}
\begin{subequations}\label{CVaRMKP}
\begin{eqnarray}
& & \max_{x, y, \nu, u} (1-\lambda)  \sum _{ \omega  \in  \Omega }^{}q^{ \omega } \sum_{m\in M} \sum _{i \in I}^{} a_{i}^{ \omega }x_{i, m}^{ \omega } - \lambda[u+1/(1-\alpha)\sum_{\omega \in \Omega} q^\omega \nu^\omega] \\
& & \nu^\omega \ge - \sum_{m\in M} \sum _{i \in I}^{} a_{i}^{ \omega }x_{i, m}^{ \omega } - u, \omega \in \Omega \\
& & \sum _{i \in I}^{} c_{i}^{ \omega }x_{im}^{ \omega }~  \leq  b_{m}^{ \omega },~ m \in M,  \omega  \in  \Omega \\
& & y_{ii'} + y_{i'i} \geq 1, i<i'  \\
& & \sum_{m=1}^{M} x_{im}^\omega \geq \sum_{m=1}^{M} x_{i'm}^\omega + y_{ii'} -1,~ i \neq i^{'}\text{, i, }i^{'} \in I,  \omega  \in  \Omega \\
& & \sum_{m=1}^{M} x_{im}^\omega \leq 1 \\
& & x_{im}^\omega, y_{ii'}^\omega \in {0, 1} \\
& & \nu^\omega \ge 0, \omega \in \Omega, \lambda \in [0, 1]
\end{eqnarray}
\end{subequations}

Example LOGOS input XML:
\begin{lstlisting}[style=XML]
<Logos>
  <Sets>
    <investments>
      1,2,3,4,5,6,7,8,9,10
    </investments>
    <capitals>
      unit_1, unit_2
    </capitals>
  </Sets>

  <Parameters>
    <net_present_values index="investments">
      78, 35, 89, 36, 94, 75, 74, 79, 80, 16
    </net_present_values>
    <costs index="investments">
      18, 9, 23, 20, 59, 61, 70, 75, 76, 30
    </costs>
    <available_capitals index="capitals">
      103, 156
    </available_capitals>
  </Parameters>

  <Uncertainties>
    <available_capitals>
      <totalScenarios>10</totalScenarios>
      <probabilities>
        0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
      </probabilities>
      <scenarios>
        101, 154,
        102, 155,
        103, 156,
        104, 157,
        105, 158,
        106, 159,
        107, 160,
        108, 161,
        109, 162,
        110, 163
      </scenarios>
    </available_capitals>
  </Uncertainties>

  <Settings>
    <solver>cbc</solver>
    <solverOptions>
      <StochSolver>EF</StochSolver>
      <risk_aversion>1.0</risk_aversion>
      <confidence_level>0.95</confidence_level>
    </solverOptions>
    <sense>maximize</sense>
    <problem_type>cvarmkp</problem_type>
  </Settings>
</Logos>
\end{lstlisting}

\subsection{CVaR for Multiple-Choice Knapsack Problem}
\label{subsec:CVaR_MCKP}

\vst \noi {\em Model Formulation:}
\begin{subequations}\label{CVaRMCKP}
\begin{eqnarray}
& & \max_{x, y, \nu, u} (1-\lambda)  \sum _{ \omega  \in  \Omega }^{}q^{ \omega } \sum_{j\in J_i} \sum _{i \in I}^{} a_{ij}^{ \omega }x_{ij}^{ \omega } - \lambda[u+1/(1-\alpha)\sum_{\omega \in \Omega} q^\omega \nu^\omega] \\
& & \nu^\omega \ge - \sum_{j\in J_i} \sum _{i \in I}^{} a_{ij}^{ \omega }x_{ij}^{ \omega } - u, \omega \in \Omega \\
& & \sum _{i \in I}^{} \sum _{j \in J_{i}}^{}\text{~ c}_{ijkt}^{ \omega }x_{ij}^{ \omega }~  \leq  b_{kt}^{ \omega },~ k \in K, t \in T,  \omega  \in  \Omega \\
& & y_{ii'} + y_{i'i} \geq 1, i<i'  \\
& & \sum_{j=1}^{J_i - 1} x_{ij}^\omega \geq \sum_{j=1}^{J_i - 1} x_{i'j}^\omega + y_{ii'} -1,~ i \neq i^{'}\text{, i, }i^{'} \in I,  \omega  \in  \Omega, and i \neq i' \\
& & \sum_{j=1}^{J_i} x_{ij}^\omega = 1 \\
& & x_{i,j}^\omega, y_{ii'}^\omega \in {0, 1} \\
& & \nu^\omega \ge 0, \omega \in \Omega, \lambda \in [0, 1]
\end{eqnarray}
\end{subequations}

Example LOGOS input XML:
\begin{lstlisting}[style=XML]
<Logos>
  <Sets>
    <investments>
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17
    </investments>
    <options index='investments'>
      1;
      1;
      1;
      1,2,3;
      1,2,3,4;
      1,2,3,4,5,6,7;
      1;
      1;
      1;
      1;
      1;
      1;
      1;
      1;
      1;
      1;
      1
    </options>
  </Sets>

  <Parameters>
    <net_present_values index='options'>
      2.046
      2.679
      2.489
      2.61
      2.313
      1.02
      3.013
      2.55
      3.351
      3.423
      3.781
      2.525
      2.169
      2.267
      2.747
      4.309
      6.452
      2.849
      7.945
      2.538
      1.761
      3.002
      3.449
      2.865
      3.999
      2.283
      0.9
      8.608
    </net_present_values>
    <costs index='options'>
      36538462
      83849038
      4615385
      2788461538
      2692307692
      5480769231
      1634615385
      2981730768
      7211538462
      9038461538
      649038462
      650000000
      216346154
      212500000
      3076923077
      3942307692
      1144230769
      675721154
      1442307692
      99711538
      4807692
      123076923
      138461538
      86538462
      108653846
      75092404
      6413462
      147932692
    </costs>
    <available_capitals>
      15E9
    </available_capitals>
  </Parameters>

  <Uncertainties>
    <available_capitals>
      <totalScenarios>3</totalScenarios>
      <probabilities>
        0.2,0.6,0.2
      </probabilities>
      <scenarios>
        5E9,10E9,15E9
      </scenarios>
    </available_capitals>
  </Uncertainties>

  <Settings>
    <solver>glpk</solver>
    <solverOptions>
      <StochSolver>EF</StochSolver>
      <risk_aversion>0.1</risk_aversion>
      <confidence_level>0.95</confidence_level>
    </solverOptions>
    <sense>maximize</sense>
    <problem_type>cvarmckp</problem_type>
  </Settings>
</Logos>
\end{lstlisting}


\subsection{NetRexx arithmetic}\label{refoarith}
\index{Overview,Arithmetic}
\index{Arithmetic,overview}
:p.
Character strings in NetRexx are commonly used for arithmetic (assuming,
of course, that they represent numbers).  The string representation of
numbers can include integers, decimal notation, and exponential
notation; they are all treated the same way.  Here are a few:
\begin{verbatim}
'1234'
'12.03'
'-12'
'120e+7'
\end{verbatim}
:p.
The arithmetic operations in NetRexx are designed for people rather than
machines, so are decimal rather than binary, do not overflow at certain
values, and follow the rules that people use for arithmetic.
The operations are completely defined by the ANSI X3.274 standard for
Rexx, so correct implementations always give the same results.
:p.
An unusual feature of NetRexx arithmetic is the \texttt{numeric}
instruction: this may be used to select the \emph{arbitrary
precision} of calculations.  You may calculate to whatever
precision that you wish (for financial calculations, perhaps), limited
only by available memory.  For example:
\begin{verbatim}
numeric digits 50
say 1/7
\end{verbatim}
:p.
which would display
\begin{verbatim}
0.14285714285714285714285714285714285714285714285714
\end{verbatim}
:p.
The numeric precision can be set for an entire program, or be adjusted
at will within the program.  The \texttt{numeric}  instruction can also
be used to select the notation (\emph{scientific}
or \emph{engineering}) used for numbers in exponential format.
:p.
NetRexx also provides simple access to the native binary arithmetic of
computers.  Using binary arithmetic offers many opportunities for
errors, but is useful when performance is paramount.  You select binary
arithmetic by adding the instruction:
\begin{verbatim}
options binary
\end{verbatim}
:p.
at the top of a NetRexx program.  The language processor will then use
:a id=refobinar.binary arithmetic:ea. instead of NetRexx decimal
arithmetic for calculations, if it can, throughout the program.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Brief background for derivation of the cable equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
See \cite{lindsay_2004} for a detailed derivation of the cable equation, and extensions to the one-dimensional model that account for radial variation of potential.

The one-dimensional cable equation introduced later in equations~\eq{eq:cable} and~\eq{eq:cable_balance} is based on the following expression in three dimensions (based on Maxwell's equations adapted for neurological modelling)
\begin{equation}
    \nabla \cdot \vv{J} = 0,
    \label{eq:J}
\end{equation}
where $\vv{J}$ is current density (units $A/m^2$).
Current density is in turn defined in terms of electric field $\vv{E}$ (units $V/m$)
\begin{equation}
    \vv{J} = \sigma \vv{E},
\end{equation}
where $\sigma$ is the specific electrical conductivity of intra-cellular fluid (typically 3.3 $S/m$).

The derivation of the cable equation is based on two assumptions:
\begin{enumerate}
    \item that charge disperion is effectively instantaneous for the purposes of dendritic modelling.
    \item that diffusion of magnetic field is instant, i.e. it behaves quasi-statically in the sense that it is determined by the electric field through the Maxwell equations.
\end{enumerate}
Under these conditions, $\vv{E}$ is conservative, and as such can be expressed in terms of a potential field
\begin{equation}
    \vv{E} = \nabla \phi,
\end{equation}
where the extra/intra-cellular potential field $\phi$ has units $mV$.

The derivation of the one-dimensional conservation equation \eq{eq:cable_balance} is based on the assumption that the intra-cellular potential (i.e. inside the cell) does not vary radially.
That is, potential is a function of the axial distance $x$ alone
\begin{equation}
    \vv{E} = \nabla \phi = \pder{V}{x}.
\end{equation}
This is not strictly true, because a potential field that is a variable of $x$ and $t$ alone can't support the axial gradients required to drive the potential difference over the cell membrane.
I am still trying to get my head around the assumptions made in mapping a three-dimensional problem to a pseudo one-dimensional one.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{The cable equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The cable equation is a nonlinear parabolic PDE that can be written in the form
\begin{equation}
    \label{eq:cable}
    c_m \pder{V}{t} = \frac{1}{2\pi a r_{L}} \pder{}{x} \left( a^2 \pder{V}{x} \right) - i_m + i_e,
\end{equation}
where
\begin{itemize}
    \item $V$ is the potential relative to the ECM $[mV]$
    \item $a$ is the cable radius $(mm)$, and can vary with $x$
    \item $c_m$ is the {specific membrane capacitance}, approximately the same for all neurons $\approx 10~nF/mm^2$. Related to \emph{membrane capacitance} $C_m$ by the relationship $C_m=c_{m}A$, where $A$ is the surface area of the cell.
    \item $i_m$ is the membrane current $[A\cdot/mm^{2}]$ per unit area. The total contribution from ion and synaptic channels is expressed as a the product of current per unit area $i_m$ and the surface area.
    \item $i_e$ is the electrode current flowing into the cell, divided by surface area, i.e. $i_e=I_e/A$.
    \item $r_L$ is intracellular resistivity, typical value $1~k\Omega \text{cm}$
\end{itemize}

Note that the standard convention is followed, whereby membrane and synapse currents ($i_m$) are positive when outward, and electrod currents ($i_e$) are positive inward.

The PDE in (\ref{eq:cable}) is derived by integrating~\eq{eq:J} over the volume of segment $i$:
\begin{equation*}
    \int_{\Omega_i}{\nabla \cdot \vv{J} } \deriv{v} = 0,
\end{equation*}
Then applying the divergence theorem to turn the volume integral on the lhs into a surface integral
\begin{equation}
          \sum_{j\in\mathcal{N}_i} {\int_{\Gamma_{i,j}} J_{i,j} \deriv{s} }
        + \int_{\Gamma_{i}} {J_m} \deriv{s} = 0
    \label{eq:cable_balance_intermediate}
\end{equation}
where
\begin{itemize}
    \item $\int_\Omega \cdot \deriv{v}$ is shorthand for the volume integral over the segment $\Omega_i$
    \item $\int_\Gamma \cdot \deriv{s}$ is shorthand for the surface integral over the surface $\Gamma$
    \item $J_{i,j}=-\frac{1}{r_L}\pder{V}{x} n_{i,j}$ is the flux per unit area of current \emph{from segment $i$ to segment $j$} over the interface $\Gamma_{i,j}$ between the two segments.
    \item the set $\mathcal{N}_i$ is the set of segments that are neighbours of $\Omega_i$
\end{itemize}
The transmembrane current density $J_m=\vv{J}\cdot\vv{n}$ is a function of the membrane potential
\begin{equation}
    J_m = c_m\pder{V}{t} + i_m - i_e,
    \label{eq:Jm}
\end{equation}
which has contributions from the ion channels and synapses ($i_m$), electrodes ($i_e$) and capacitive current due to polarization of the membrane whose bi-layer lipid structure causes it to behave locally like a parallel plate capacitor.

Substituting~\eq{eq:Jm} into~\eq{eq:cable_balance_intermediate} and rearanging gives
\begin{equation}
      \int_{\Gamma_{i}} {c_m\pder{V}{t}} \deriv{s}
    = - \sum_{j\in\mathcal{N}_i} {\int_{\Gamma_{i,j}} J_{i,j} \deriv{s} }
    - \int_{\Gamma_{i}} {(i_m - i_e)} \deriv{s}
    \label{eq:cable_balance}
\end{equation}

The surface of the cable segment is sub-divided into the internal and external surfaces.
The external surface $\Gamma_{i}$ is the cell membrane at the interface between the extra-cellular and intra-cellular regions.
The current, which is the conserved quantity in our conservation law, over the surface is composed of the synapse and ion channel contributions.
This is derived from a thin film approximation to the cell membrane, whereby the membrane is treated as an infinitesimally thin interface between the intra and extra cellular regions.

Note that some information is lost when going from a three-dimensional description of a neuron to a system of branching one-dimensional cable segments.
If the cell is represented by cylinders or frustrums\footnote{a frustrum is a truncated cone, where the truncation plane is parallel to the base of the cone.}, the three-dimensional values for volume and surface area at branch points can't be retrieved from the one-dimensional description.

\begin{figure}
    \begin{center}
        \includegraphics[width=0.5\textwidth]{./images/cable.pdf}
    \end{center}
    \caption{A single segment, or control volume, on an unbranching dendrite.}
    \label{fig:segment}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Finite volume discretization}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The finite volume method is a natural choice for the solution of the conservation law in~\eq{eq:cable_balance}.

\begin{itemize}
    \item   the $x_i$ are spaced uniformly with distance $x_{i+1}-x_{i} = \Delta x$
    \item   control volumes are formed by locating the boundaries between adjacent points at $(x_{i+1}+x_{i})/2$
    \item   this discretization differs from the finite differences used in Neuron because the equation is explicitly solved for at the end of cable segments, and because the finite volume discretization is applied to all points. Neuron uses special algebraic \emph{zero area} formulation for nodes at branch points.
\end{itemize}

%-------------------------------------------------------------------------------
\subsubsection{Temporal derivative}
%-------------------------------------------------------------------------------
The integral on the lhs of~\eq{eq:cable_balance} can be approximated by assuming that the average transmembrane potential $V$ in $\Omega_i$ is equal to the potential $V_i$ defined at the centre of the segment:
\begin{equation}
    \int_{\Gamma_i}{c_m \pder{V}{t} } \deriv{v} \approx \sigma_i \cmi \pder{V_i}{t},
    \label{eq:dvdt}
\end{equation}
where $\sigma_i$ is the surface area, and $\cmi$ is the average specific membrane capacitance, respectively of the surface $\Gamma_i$.

Each control volume is composed of \emph{sub control volumes}, which are illustrated as the coloured sub-regions in \fig{fig:segment}.
\begin{equation*}
    \Omega_i = \bigcup_{j\in\mathcal{N}_i}{\Omega_i^j}.
\end{equation*}
Likewise, the surface $\Gamma_i$ is composed of subsufaces as follows
\begin{equation*}
    \Gamma_i = \bigcup_{j\in\mathcal{N}_i}{\Gamma_i^j},
\end{equation*}
where $\Gamma_i^j$ is the surface of each of the sub-control volumes in $\Omega_i$.
Thus, the surface area of the CV as
\begin{equation*}
    \sigma_i = \sum_{i\in\mathcal{N}_i}{\sigma_i^j},
\end{equation*}
where $\sigma_i^j$ is the area of $\Gamma_i^j$, and the average specific membrane capacitance $\cmi$ is
\begin{equation*}
    \cmi = \frac{1}{\sigma_i}\sum_{i\in\mathcal{N}_i}{\sigma_i^j c_m^{i,j}}.
\end{equation*}
\todo{This  is included as a placeholder, we really need more illustrations to show how CV averages are computed for quantities that vary between sub-control volumes of the same CV.}

%-------------------------------------------------------------------------------
\subsubsection{Intra-cellular flux}
%-------------------------------------------------------------------------------
The intracellular flux terms in~\eq{eq:cable_balance} are sum of the flux over the interfaces between compartment $i$ and its set of neighbouring compartments $\mathcal{N}_i$ is
\begin{equation}
    \sum_{j\in\mathcal{N}_i} { \int_{\Gamma_{i,j}} { J_{i,j} \deriv{s} } }.
\end{equation}
where the flux per unit area from compartment $i$ to compartment $j$ is
\begin{align}
    J_{i,j} = - \frac{1}{r_L}\pder{V}{x} n_{i,j}.
    \label{eq:J_ij_exact}
\end{align}
The derivative with respect to the outward-facing normal can be approximated as follows
\begin{equation*}
    \pder{V}{x} n_{i,j} \approx \frac{V_j - V_i}{\Delta x_{i,j}}
\end{equation*}
where $\Delta x_{i,j}$ is the distance between $x_i$ and $x_j$, i.e. $\Delta x_{i,j}=|x_i-x_j|$.
Using this approximation for the derivative, the flux over the surface in~\eq{eq:J_ij_exact} is approximated as
\begin{align}
    J_{i,j} \approx \frac{1}{r_L}\frac{V_i - V_j}{\Delta x_{i,j}}.
    \label{eq:J_ij_intermediate}
\end{align}

The terms inside the integral in equation~\eq{eq:J_ij_intermediate} are constant everywhere on the surface $\Gamma_{i,j}$, so the integral becomes
\begin{align}
  J_{i,j} &\approx \int_{\Gamma_{i,j}}  \frac{1}{r_L}\frac{V_i-V_j}{\Delta x_{i,j}} \deriv{s} \nonumber \\
          &= \frac{1}{r_L \Delta x_{i,j}}(V_i-V_j) \int_{\Gamma_{i,j}} 1 \deriv{s} \nonumber \\
          &= \frac{\sigma_{ij}}{r_L \Delta x_{i,j}}(V_i-V_j) \nonumber \\
          \label{eq:J_ij}
\end{align}
where $\sigma_{i,j}=\pi a_{i,j}^2$ is the area of the surface $\Gamma_{i,j}$, which is a circle of radius $a_{i,j}$.

Some symmetries
\begin{itemize}
    \item $\sigma_{i,j}=\sigma_{j,i}$ : surface area of $\Gamma_{i,j}$
    \item $\Delta x_{i,j}=\Delta x_{j,i}$ : distance between $x_i$ and $x_j$
    \item $n_{i,j}=-n_{j,i}$ : surface ``norm''/orientation
    \item $J_{i,j}=n_{j,i}\cdot J_{i,j}=-J_{j,i}$ : charge flux over $\Gamma_{i,j}$
\end{itemize}

%-------------------------------------------------------------------------------
\subsubsection{Cell membrane flux}
%-------------------------------------------------------------------------------
The final term in~\eq{eq:cable_balance} with an integral is the cell membrane flux contribution
\begin{equation}
    \int_{\Gamma_{ext}} {(i_m - i_e)} \deriv{s},
\end{equation}
where the current $i_m$ is due to ion channel and synapses, and $i_e$ is any artificial electrode current.
The $i_m$ term is dependent on the potential difference over the cell membrane $V_i$.
The current terms are an average per unit area, therefore the total flux
\begin{equation}
    \int_{\Gamma_{ext}} {(i_m - i_e)} \deriv{s}
        \approx
    \sigma_i(i_m(V_i) - i_e(x_i)),
        \label{eq:J_im}
\end{equation}
where $\sigma_i$ is the surface area the of the exterior of the cable segment, i.e. the surface corresponding to the cell membrane.

Each cable segment is a conical frustrum, as illustrated in \fig{fig:segment}.
The lateral surface area of a frustrum with height $\Delta x_i$ and radii of  is
The area of the external surface $\Gamma_{i}$ is
\begin{equation}
    \sigma_i = \pi (a_{i,\ell} + a_{i,r}) \sqrt{\Delta x_i^2 + (a_{i,\ell} - a_{i,r})^2},
    \label{eq:cv_volume}
\end{equation}
where $a_{i,\ell}$ and $a_{i,r}$ are the radii of at the left and right end of the segment respectively (see~\eq{eq:frustrum_area} for derivation of this formula).
%-------------------------------------------------------------------------------
\subsubsection{Putting it all together}
%-------------------------------------------------------------------------------
By substituting the volume averaging of the temporal derivative in~\eq{eq:dvdt} approximations for the flux over the surfaces in~\eq{eq:J_ij} and~\eq{eq:cv_volume} respectively into the conservation equation~\eq{eq:cable_balance} we get the following ODE defined for each node in the cell
\begin{equation}
    \sigma_i \cmi \dder{V_i}{t}
       = -\sum_{j\in\mathcal{N}_i} {\frac{\sigma_{i,j}}{r_L \Delta x_{i,j}} (V_i-V_j)} - \sigma_i\cdot(i_m(V_i) - i_e(x_i)),
    \label{eq:ode}
\end{equation}
where
\begin{equation}
    \sigma_{i,j} = \pi a_{i,j}^2
    \label{eq:sigma_ij}
\end{equation}
is the area of the surface between two adjacent segments $i$ and $j$, and
\begin{equation}
    \sigma_{i}   = \pi(a_{i,\ell} + a_{i,r}) \sqrt{\Delta x_i^2 + (a_{i,\ell} - a_{i,r})^2},
    \label{eq:sigma_i}
\end{equation}
is the lateral area of the conical frustrum describing segment $i$.

%-------------------------------------------------------------------------------
\subsubsection{Time Stepping}
%-------------------------------------------------------------------------------
The finite volume discretization approximates spatial derivatives, reducing the original continuous formulation into the set of ODEs, with one ODE for each compartment, in equation~\eq{eq:ode}.
Here we employ an implicit euler temporal integration sheme, wherby the temporal derivative on the lhs is approximated using forward differences
\begin{align}
    \sigma_i c_m \frac{V_i^{k+1}-V_i^{k}}{\Delta t}
        = & -\sum_{j\in\mathcal{N}_i} {\frac{\sigma_{i,j}}{r_L \Delta x_{i,j}} (V_i^{k+1}-V_j^{k+1})} \nonumber \\
          & - \sigma_i\cdot(i_m(V_i^{k}) - i_e),
    \label{eq:ode_subs}
\end{align}
Where $V^k$ is the value of $V$ in compartment $i$ at time step $k$.
Note that on the rhs the value of $V$ at the target time step $k+1$ is used, with the exception of calculating the ion channel and synaptic currents $i_m$.
The current $i_m$ is often a nonlinear function of voltage, so if it was formulated in terms of $V^{k+1}$ the system in~\eq{eq:ode_subs} would be nonlinear, requiring Newton iterations to resolve.

The equations can be rearranged to have all unknown voltage values on the lhs, and values that can be calculated directly on the rhs:
\begin{align}
    & \frac{\sigma_i \cmi}{\Delta t} V_i^{k+1} + \sum_{j\in\mathcal{N}_i} {\alpha_{ij} (V_i^{k+1}-V_j^{k+1})}
            \nonumber \\
    = & \frac{\sigma_i \cmi}{\Delta t} V_i^k -  \sigma_i(i_m^{k} - i_e),
    \label{eq:ode_linsys}
\end{align}
where the value
\begin{equation}
    \alpha_{ij} = \alpha_{ji} = \frac{\sigma_{ij}}{ r_L \Delta x_{ij}}
    \label{eq:alpha_linsys}
\end{equation}
is a constant that can be computed for each interface between adjacent compartments during set up.

The left hand side of \eq{eq:ode_linsys} can be rearranged
\begin{equation}
    \left[ \frac{\sigma_i \cmi}{\Delta t} + \sum_{j\in\mathcal{N}_i} {\alpha_{ij}} \right] V_i^{k+1}
    - \sum_{j\in\mathcal{N}_i} { \alpha_{ij} V_j^{k+1}},
    \label{eq:rhs_linsys}
\end{equation}
which gives the coefficients for the linear system.

The capacitance of the cell membrane, $\sigma_i \cmi$, varies between control volumes, while the $\alpha_{i,j}$ term in symmetric (i.e. $\alpha_{i,j}=\alpha_{j,i}$.)
With this in mind, we can see that when the linear system is written in the form~\eq{eq:ode_linsys}, the matrix is symmetric.
Furthermore, because $\alpha_{i,j} > 0$, the linear system is diagonally dominant for sufficiently small $\Delta t$.

%-------------------------------------------------------------------------------
\subsubsection{The Soma}
%-------------------------------------------------------------------------------
We model the soma as a sphere with a centre $\vv{x}_s$ and a radius $r_s$.
The soma is conceptually treated as the center of the cell: the point from which all other parts of the cell branch.
It requires special treatment, because of its size relative to the radius of the dendrites and axons that branch off from it.

Though the soma is usually visualized as a sphere, it is \emph{worth noting} that Neuron models the soma as a cylinder
\begin{itemize}
    \item A cylinder that has the same diameter as length ($L=2r$) has the same area as a sphere with radius $r$, i.e. $4\pi r^2$.
    \item However a sphere has 4/3 times the volume of the cylinder.
\end{itemize}

If the soma is modelled as a single compartment the potential is assumed constant throughout the cell.
This is exactly the same model used to handle branches, with the main difference being how the area and volume of the control volume centred on the soma are calculated as a result of the spherical model.

\begin{figure}
    \begin{center}
        \includegraphics[width=0.5\textwidth]{./images/soma.pdf}
    \end{center}
    \caption{A soma with two dendrites.}
    \label{fig:soma}
\end{figure}

\fig{fig:soma} shows a soma that is attached to two unbranched dendrites.
In this example the simplest possible compartment model is used, with three compartments: one for the soma and one for each of the dendrites.
The soma CV extends to half way along each dendrite.
To calculate the flux from the soma compartment to the dendrites the flux over the CV face half way along each dendrite has to be computed.
For this we use the voltage defined at each end of the dendrite, which raises the question about what value to use for the end of the dendrite that is attached to the soma.
For this we use the voltage as defined for the soma, i.e. we use the same value at the start for each dendrite, as illustrated in \fig{fig:soma}.

This effectivly of ``collapses'' the node that defines the start of dendrites attached to the soma onto the soma in the mathematical formulation.
This requires a little slight of hand when loading the cell description from file (for example a \verb!.swc! file).
In such file formats there would be 5 points used to describe the cell in \fig{fig:soma}:
\begin{itemize}
    \item 1 to describe the center of the soma.
    \item 2 for each dendrite: 1 describing where the dendrite is attached to the soma, and 1 for the terminal end.
\end{itemize}
However, in the mathematical formulation there are only 3 values that are solved for: the soma and one for each of the dendrites.

On a side note, one possible extension is to model the soma as a set of shells, like an onion.
Conceptually this would look like an additional branch from the surface of the soma, with the central core being the terminal of the branch.
However, the cable equation would have to be reformulated in spherical coordinates.

%-------------------------------------------------------------------------------
\subsubsection{Handling Branches}
%-------------------------------------------------------------------------------
The value of the lateral area $\sigma_i$ in~\eq{eq:sigma_i} is the sum of the areaof each branch at branch points.

\todo{a picture of a branching point to illustrate}

\todo{a picture of a soma to illustrate the ball and stick model with a sphere for the soma and sticks for the dendrites branching off the soma.}

\begin{equation}
    \sigma_i = \sum_{j\in\mathcal{N}_i} {\dots}
\end{equation}


\section{Basic heap concepts}
\Label{sec:heap-concepts}

The description of heaps at the beginning of this chapter is of course fairly vague.
It outlines only the most important properties
of various operations but does not clearly state what specific and verifiable
properties a range must satisfy such that it may be called a heap.

A more detailed description can be found in the Apache \cxx Standard Library User's Guide:\footnote{
  See \url{http://stdcxx.apache.org/doc/stdlibug/14-7.html}
}

\begin{quote}
A heap is a binary tree in which every node is larger than the values
associated with either child. A heap and a binary tree, for that matter,
can be very efficiently stored in a vector, by placing the children of
node $i$
at positions $2i + 1$ and $2i + 2$.
\end{quote}

We have, in other words, the following basic relations between indices of a heap:

\begin{align}
\Label{eq:heap-left}
   &\text{left child for index $i$}   && \mathrm{child_l}: i \mapsto 2i + 1  \\
\Label{eq:heap-right}
   &\text{right child for index $i$}  && \mathrm{child_r}: i \mapsto 2i + 2  \\
\intertext{and}
\Label{eq:heap-parent}
   &\text{parent index for index $i$}  && \mathrm{parent}: i \mapsto \frac{i - 1}{2}
\end{align}

%\clearpage

These function are related through the following two equations
that hold for all integers~$i$.
Note that in \acsl integer division rounds towards zero (cf.\ \cite[\S 2.2.4]{ACSLSpec}).

\begin{align}
\Label{eq:heap-parent-left}
   \mathrm{parent}(\mathrm{child_l}(i)) &= i \\
\Label{eq:heap-parent-right}
   \mathrm{parent}(\mathrm{child_r}(i)) &= i
\end{align}

In order to given an example for the usefulness of heaps
we consider the following multiset of integers $X$.

\begin{align}
\Label{eq:heap-multiset}
  X &= \{2,3,3,3,6,7,8,8,9,11,13,14\}
\end{align}

\clearpage

Figure~\ref{fig:heap-tree} shows how the multiset from Equation~\eqref{eq:heap-multiset}
can, according to the parent-child relations of a heap, be represented as a tree.

\begin{figure}[hbt]
\centering
\includegraphics[width=0.75\linewidth]{Figures/heap_tree_color.pdf}
\caption{\Label{fig:heap-tree}Tree representation of the multiset~$X$}
\end{figure}

\FloatBarrier

The numbers outside the nodes in Figure~\ref{fig:heap-tree} are the indices at which
the respective node value is stored in the underlying array of a heap (cf.\ Figure~\ref{fig:heap-array}).

\begin{figure}[hbt]
\centering
\includegraphics[width=0.65\linewidth]{Figures/heap_array_color.pdf}
\caption{\Label{fig:heap-array}Underlying array of a heap}
\end{figure}

\FloatBarrier
\clearpage

It is important to understand that there can be various representations of a multiset
as a heap.
Figure~\ref{fig:heap-alternative-tree}, for example, arranges the elements of
the multiset~$X$ as a heap in a different tree.

\begin{figure}[hbt]
\centering
\includegraphics[width=0.75\linewidth]{Figures/heap_tree_alternative_color.pdf}
\caption{\Label{fig:heap-alternative-tree} An alternative representation of the multiset~$X$}
\end{figure}

\FloatBarrier

Figure~\ref{fig:heap-array-alternative} then shows the underlying array that
corresponds to the tree in Figure~\ref{fig:heap-alternative-tree}.

\begin{figure}[hbt]
\centering
\includegraphics[width=0.65\linewidth]{Figures/heap_array_alternative_color.pdf}
\caption{\Label{fig:heap-array-alternative}Underlying array of the alternative representation}
\end{figure}

\FloatBarrier
\clearpage


\subsection*{b)}
In this assignment we will provide a parametric bootstrapped 95\% confidence interval for the parameters. Since we have a dataset that we can evaluate parameters from, we need to determine the uncertainty of the estimators. To assess the uncertainty we analyze the distribution function $F_{\Delta}$ of the error $\Delta$ under $\mathbb{P}_0$. This uncertainty can be bounded by confidence intervals to assess the uncertainty we analyze the distribution function $F_{\Delta}$ of the error $\Delta$ under $\mathbb{P}_0$. \\

First of, we begin the bootstrap as described previously. We therefore estimate the parameters of the data by the function \texttt{est\_gumpel.m}. These estimates are the frequentistic approach using maximum likelihood. We then sample new data by generating random numbers and sampling using the inverse method of the relation derived in section \textbf{a)}. Thereafter we calculate the error of our estimates by the relation $\Delta_b^*(t)=\hat{t}_b^*-\hat{t}$. To get the confidence bounds for our estimates we used the relation \eqref{conf} derived in Jimmy Olssons lecture notes \cite{JO}
\begin{equation} I_{\alpha}=\left(t(y) - F_\Delta^{-1}(F_\Delta^{-1}(1-\alpha/2),t(y)-F_\Delta^{-1}(\alpha/2)\right).
\label{conf}
 \end{equation}

However, we also need to check the bias of our estimate by the relation
\[ B_t=\mathbb{E}_0(t(Y)-\tau)=\mathbb{E}_0(\Delta(Y))=\int zf_\Delta (z) dz, \]
where $f_\Delta(z)=\frac{d}{dz}F_\Delta(z)$ denoting the density of $\Delta(Y)$. The calculated bias of the estimator for $\mu$ and $\beta$ respectively was computed as
\[ B_t = \mathbb{E}_0(\Delta_b^*(t))=\left\{ \begin{array}{l}
-0.0009 \\ 0.0037
\end{array}\right. \]

The bias was then taken into consideration by making the bias-corrected estimate $t-B_t$.

\begin{table}
\centering
\begin{tabular}{|c|c|c|}
\hline

 & L & U \\ \hline
%Estimates & 1.4858 & 4.1477 \\ \hline
$\beta$ & 1.3942 & 1.5821 \\ \hline
$\mu$ & 4.0220 & 4.2723 \\ \hline

\end{tabular}
\caption{Table showing the expected values and upper and lower bounds.}
\end{table}

In figure \ref{fig:estwaves} the histogram of the estimated dataset as well as the dataset is plotted.

\begin{figure}[H]
\centering
\includegraphics[scale=0.26]{./Figures/estwaves.png}

\label{fig:estwaves}
\caption{A histrogram of the estimated dataset, plotted over the real dataset.}
\end{figure}

Here we can see the upper and lower cconfidence bound of the estimated wavesplotted with the raw data. One can observe that the gumpel approximation is indeed an approximation of the data.


\documentclass[20pt,a4paper]{extarticle}
\usepackage[a4paper,margin=6mm]{geometry}
\usepackage{amsmath}
\usepackage{hyperref}

\title{\LaTeX\ Mathematics Examples}
\author{Prof Tony Roberts}

\begin{document}

\maketitle

\tableofcontents

\section{Delimiters}

See how the delimiters are of reasonable size in these examples
\[
	\left(a+b\right)\left[1-\frac{b}{a+b}\right]=a\,,
\]
\[
	\sqrt{|xy|}\leq\left|\frac{x+y}{2}\right|,
\]
even when there is no matching delimiter
\[
	\int_a^bu\frac{d^2v}{dx^2}\,dx
	=\left.u\frac{dv}{dx}\right|_a^b
	-\int_a^b\frac{du}{dx}\frac{dv}{dx}\,dx.
\]

\section{Spacing}

Differentials often need a bit of help with their spacing as in
\[
	\iint xy^2\,dx\,dy
	=\frac{1}{6}x^2y^3,
\]
whereas vector problems often lead to statements such as
\[
	u=\frac{-y}{x^2+y^2}\,,\quad
	v=\frac{x}{x^2+y^2}\,,\quad\text{and}\quad
	w=0\,.
\]
Occasionally one gets horrible line breaks when using a list in mathematics such as listing the first twelve primes  \(2,3,5,7,11,13,17,19,23,29,31,37\)\,.
In such cases, perhaps include \verb|\mathcode`\,="213B| inside the inline maths environment so that the list breaks: \(\mathcode`\,="213B 2,3,5,7,11,13,17,19,23,29,31,37\)\,.
Be discerning about when to do this as the spacing is different.

\section{Arrays}

Arrays of mathematics are typeset using one of the matrix environments as
in
\[
	\begin{bmatrix}
		1 & x & 0 \\
		0 & 1 & -1
	\end{bmatrix}\begin{bmatrix}
		1  \\
		y  \\
		1
	\end{bmatrix}
	=\begin{bmatrix}
		1+xy  \\
		y-1
	\end{bmatrix}.
\]
Case statements use cases:
\[
	|x|=\begin{cases}
		x, & \text{if }x\geq 0\,,  \\
		-x, & \text{if }x< 0\,.
	\end{cases}
\]
Many arrays have lots of dots all over the place as in
\[
	\begin{matrix}
		-2 & 1 & 0 & 0 & \cdots & 0  \\
		1 & -2 & 1 & 0 & \cdots & 0  \\
		0 & 1 & -2 & 1 & \cdots & 0  \\
		0 & 0 & 1 & -2 & \ddots & \vdots \\
		\vdots & \vdots & \vdots & \ddots & \ddots & 1  \\
		0 & 0 & 0 & \cdots & 1 & -2
	\end{matrix}
\]

\section{Equation arrays}

In the flow of a fluid film we may report
\begin{eqnarray}
	u_\alpha & = & \epsilon^2 \kappa_{xxx}
	\left( y-\frac{1}{2}y^2 \right),
	\label{equ}  \\
	v & = & \epsilon^3 \kappa_{xxx} y\,,
	\label{eqv}  \\
	p & = & \epsilon \kappa_{xx}\,.
	\label{eqp}
\end{eqnarray}
Alternatively, the curl of a vector field $(u,v,w)$ may be written
with only one equation number:
\begin{eqnarray}
	\omega_1 & = &
	\frac{\partial w}{\partial y}-\frac{\partial v}{\partial z}\,,
	\nonumber  \\
	\omega_2 & = &
	\frac{\partial u}{\partial z}-\frac{\partial w}{\partial x}\,,
	\label{eqcurl}  \\
	\omega_3 & = &
	\frac{\partial v}{\partial x}-\frac{\partial u}{\partial y}\,.
	\nonumber
\end{eqnarray}
Whereas a derivation may look like
\begin{eqnarray*}
	(p\wedge q)\vee(p\wedge\neg q) & = & p\wedge(q\vee\neg q)
	\quad\text{by distributive law}  \\
	 & = & p\wedge T \quad\text{by excluded middle}  \\
	 & = & p \quad\text{by identity}
\end{eqnarray*}

\section{Functions}

Observe that trigonometric and other elementary functions are typeset
properly, even to the extent of providing a thin space if followed by
a single letter argument:
\[
	\exp(i\theta)=\cos\theta +i\sin\theta\,,\quad
	\sinh(\log x)=\frac{1}{2}\left( x-\frac{1}{x} \right).
\]
With sub- and super-scripts placed properly on more complicated
functions,
\[
	\lim_{q\to\infty}\|f(x)\|_q
	=\max_{x}|f(x)|,
\]
and large operators, such as integrals and
\begin{eqnarray*}
	e^x & = & \sum_{n=0}^\infty \frac{x^n}{n!}
	\quad\text{where }n!=\prod_{i=1}^n i\,,  \\
	\overline{U_\alpha} & = & \bigcap_\alpha U_\alpha\,.
\end{eqnarray*}
In inline mathematics the scripts are correctly placed to the side in
order to conserve vertical space, as in
\(
	1/(1-x)=\sum_{n=0}^\infty x^n.
\)

\section{Accents}

Mathematical accents are performed by a short command with one
argument, such as
\[
	\tilde f(\omega)=\frac{1}{2\pi}
	\int_{-\infty}^\infty f(x)e^{-i\omega x}\,dx\,,
\]
or
\[
	\dot{\vec \omega}=\vec r\times\vec I\,.
\]

\section{Command definition}

\newcommand{\Ai}{\operatorname{Ai}}
The Airy function, $\Ai(x)$, may be incorrectly defined as this
integral
\[
	\Ai(x)=\int\exp(s^3+isx)\,ds\,.
\]

\newcommand{\D}[2]{\frac{\partial #2}{\partial #1}}
\newcommand{\DD}[2]{\frac{\partial^2 #2}{\partial #1^2}}
\renewcommand{\vec}[1]{\boldsymbol{#1}}

This vector identity serves nicely to illustrate two of the new
commands:
\[
	\vec\nabla\times\vec q
	=\vec i\left(\D yw-\D zv\right)
	+\vec j\left(\D zu-\D xw\right)
	+\vec k\left(\D xv-\D yu\right).
\]

Recall that typesetting multi-line mathematics is an art normally too hard for computer recipes.  Nonetheless, if you need to be automatically flexible about multi-line mathematics, and you do not mind some rough typesetting, then perhaps invoke \verb|\parbox| to help as follows:
% The \verb|breqn| package is not yet reliable enough for general use.
\newcommand{\parmath}[2][0.8\linewidth]{\parbox[t]{#1}%
    {\raggedright\linespread{1.2}\selectfont\(#2\)}}
\[
u_1=\parmath{ -2 \gamma  \epsilon^{2} s_{2}+\mu  \epsilon^{3} \big( \frac{3}{8} s_{2}+\frac{1}{8} s_{1} i\big)+\epsilon^{3} \big( -\frac{81}{32} s_{4} s_{2}^{2}-\frac{27}{16} s_{4} s_{2} s_{1} i+\frac{9}{32} s_{4} s_{1}^{2}+\frac{27}{32} s_{3} s_{2}^{2} i-\frac{9}{16} s_{3} s_{2} s_{1}-\frac{3}{32} s_{3} s_{1}^{2} i\big) +\int_a^b 1-2x+3x^2-4x^3\,dx }
\]
Also, sometimes use \verb|\parbox| to typeset multiline entries in tables.

\section{Theorems et al.}

\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{definition}[theorem]{Definition}

\begin{definition}[right-angled triangles] \label{def:tri}
A \emph{right-angled triangle} is a triangle whose sides of length~\(a\), \(b\) and~\(c\), in some permutation of order, satisfies \(a^2+b^2=c^2\).
\end{definition}

\begin{lemma}
The triangle with sides of length~\(3\), \(4\) and~\(5\) is right-angled.
\end{lemma}

This lemma follows from the Definition~\ref{def:tri} as \(3^2+4^2=9+16=25=5^2\).

\begin{theorem}[Pythagorean triplets] \label{thm:py}
Triangles with sides of length \(a=p^2-q^2\), \(b=2pq\) and \(c=p^2+q^2\) are right-angled triangles.
\end{theorem}

Prove this Theorem~\ref{thm:py} by the algebra \(a^2+b^2 =(p^2-q^2)^2+(2pq)^2
=p^4-2p^2q^2+q^4+4p^2q^2
=p^4+2p^2q^2+q^4
=(p^2+q^2)^2 =c^2\).

\end{document}


%% \NV{TO CHECL that in the logic we use = instead of ==}
%% \NV{TO CHECK that list type is L a}
\section{Proofs as Haskell Functions}\label{sec:haskell-proofs}

Refinement Reflection, as explained in chapter~\ref{refinementrflection}, is a technique
that lets you write Haskell functions that prove theorems
about other Haskell functions and have your proofs machine-checked
by \toolname.
%
As an introduction to Refinement Reflection,
in this section, we prove that lists are monoids by
\begin{itemize}
\item \textit{specifying monoid laws} as refinement types,
\item \textit{proving the laws} by writing the implementation of the law specifications, and
\item \textit{verifying the proofs} using \toolname.
\end{itemize}

\subsection{Reflection of data types into logic.}
To start with,
we define a List data structure and
teach \toolname basic properties about List,
namely, how to check that proofs on lists are \textit{total}
and how to encode functions on List into the logic.

The data list definition @L@ is the standard recursive definition.
\begin{code}
  data L [length] a = N | C a (L a)
\end{code}
%
With the @length@ annotation in the definition \toolname
will use the @length@ function to check termination
of functions recursive on Lists.
%
We define @length@ as the standard Haskell function
that returns natural numbers.
%
We lift @length@ into logic as a \textit{measure} (\S~\ref{sec:measures}),
that is, a \textit{unary} function whose (1) domain is the data type and
(2) body is a single case-expression over the datatype.
\begin{code}
  type Nat = {v:Int | 0 <= v}

  measure length    :: L a -> Nat
    length N        = 0
    length (C x xs) = 1 + length xs
\end{code}

Finally, we teach \toolname how to encode functions on Lists
into logic.
%
The flag @"--exact-data-cons"@
automatically derives measures which
(1) test if a value has a given data constructor and
(2) extract the corresponding field's value.
%
For example, \toolname will automatically derive the following
List manipulation measures from the List definition.
\begin{code}
  isN :: L a -> Bool    -- Haskell's null
  isC :: L a -> Bool    -- Haskell's not . null

  selC1 :: L a -> a     -- Haskell's head
  selC2 :: L a -> L a   -- Haskell's tail
\end{code}
%
Next, we describe how \toolname uses the above measures
to automatically reflect Haskell functions on Lists into logic.

\subsection{Reflection of Haskell functions into logic.}
Next, we define and reflect into logic the two monoid operators on Lists.
Namely, the identity element @mempty@ (which is the empty list)
and an associative operator @(mappend)@ (which is list append).
%
\begin{code}
  reflect mempty
  mempty :: L a
  mempty = N

  reflect (mappend)
  (mappend) :: L a -> L a -> L a
  N        mappend ys = ys
  (C x xs) mappend ys = C x (xs mappend ys)
\end{code}

The reflect annotations lift the Haskell functions into logic in three steps.
%
First, check that the Haskell functions indeed terminate by checking
that the @length@ of the input list is decreasing,
as specified in the data list definition.
%
Second, in the logic, they define the respective uninterpreted functions
@mempty@ and @(mappend)@.
%
Finally, the Haskell functions and the logical uninterpreted functions
are related by strengthening the result type of the Haskell function
with the definition of the function's implementation.
%
For example, with the above @reflect@ annotations,
\toolname will \textit{automatically} derive the following strengthened
types for the relevant functions.
\begin{code}
  mempty  :: {v:L a | v = mempty && v = N }

  (mappend) :: xs:L a -> ys:L a
      -> {v:L a | v = xs mappend ys
                && v = if isN xs then ys
                      else C (selC1 xs) (selC2 xs mappend ys)
         }
\end{code}

\subsection{Specification and Verification of Monoid Laws}

Now we are ready to specify the monoid laws as refinement types and
provide their respective proofs as terms of those type. \toolname
will verify that our proofs are valid.
%
Note that this is exactly what
one would do in any standard logical framework,
like LF~\cite{Harper93}.

The type @Proof@ is predefined as an alias of the unit type (@()@)
in the \toolname's library @ProofCombinators@.
We summarize all the definitions we use from @ProofCombinators@ in
Figure~\ref{figure:proofcombinators}.
%
We express theorems as refinement types by refining
the @Proof@ type with appropriate refinements.
%
For example, the following theorem states
the @mempty@ is always equal to itself.
\begin{code}
  trivial :: {mempty = mempty}
\end{code}
%
Where @{mempty = mempty}@ is a simplification for the @Proof@ type
@{v:Proof | mempty = mempty}@, since the binder @v@ is irrelevant, and
@trivial@ is defined in @ProofCombinators@ to be unit.
%
\toolname will typecheck the above code using an SMT
solver to check congruence on @mempty@.
%% \NV congruence on or with?
%

\begin{definition}[Monoid] \label{definition:monoid}
The triple (@m@, @epsilon@, @<>@) is a monoid
(with identity element @epsilon@ and associative operator @<>@),
if the following functions are defined. % on @m@.
%
\begin{code}
  idLeft_m  :: x:m -> {mempty mappend x = x}
  idRight_m :: x:m -> {x mappend mempty = x}
  assoc_m   :: x:m -> y:m -> z:m -> {x mappend (y mappend z) = (x mappend y) mappend z}
\end{code}
\end{definition}
%
Using the above definition, we prove that our list type @L@ is a monoid
by defining Haskell proof terms that satisfy the above monoid laws.
%%We now represent these conditions applied to our list type @L@ and
%%their proofs as (refined) types and terms of those types.

\mypara{Left Identity} is expressed
as a refinement type signature that takes as input
a list @x:L a@ and returns a @Proof@ type refined
with the property @mempty <> x = x@
\begin{code}
  idLeft :: x:L a -> {mempty mappend x = x}
  idLeft x
    =  mempty <> x
    =. N <> x
    =. x
    ** QED
\end{code}
%
We prove left identity using combinators from @ProofCombinators@ as
defined in Figure~\ref{figure:proofcombinators}.
%
We start from the left hand side @mempty <> x@,
which is equal to @N <> x@ by calling @mempty@ thus
unfolding the equality @mempty = N@ into the logic.
%
Next, the call @N <> x@ unfolds into the logic the definition of @(<>)@
on @N@ and @x@, which is equal to @x@, concluding our proof.
%
Finally, we use the operators @p ** QED@ which casts @p@ into a proof term.
%
In short, the proof of left identity, proceeds by unfolding the definitions of @mempty@
and @(<>)@ on the empty list.

\begin{figure}[t]
\centering
\captionsetup{justification=centering}
\begin{code}
  type Proof = ()
  data QED   = QED

  trivial :: Proof
  trivial = ()

  (=.) :: x:a -> y:{a | x = y} -> {v:a | v = x}
  x =. _ = x

  (**) :: a -> QED -> Proof
  _ ** _ = ()

  (?) :: (Proof -> a) -> Proof -> a
  f ? y = f y
\end{code}
\caption[Proof Operators and Types.]{Operators and Types defined in \texttt{ProofCombinators}.}
\label{figure:proofcombinators}
\end{figure}

\mypara{Right identity} is proved by structural induction.
%
We encode inductive proofs by case splitting on the base and inductive case
and enforcing the inductive hypothesis via a recursive call.
\begin{code}
  idRight :: x:L a -> { x <> mempty = x }
  idRight N
    =  N <> empty
    =. N
    ** QED

  idRight (C x xs)
    =  (C x xs) <> empty
    =. C x (xs <> empty)
    =. C x xs ? idRight xs
    ** QED
\end{code}
The recursive call @idRight xs@ is provided
as a third optional argument in the @(=.)@
operator to justify the equality @xs <> empty = xs@,
while the operator @(?)@ is merely a function application
with the appropriate precedence.
%
Note that LiquiHaskell, via termination and totality checking,
is verifying that all the proof terms are well formed because
(1) the inductive hypothesis is only applying to smaller terms and
(2) all cases are covered.

\mypara{Associativity} is proved in a very similar manner,
using structural induction.
%
\begin{code}
  assoc :: x:L a -> y:L a -> z:L a -> {x mappend (y mappend z) = (x mappend y) mappend z}
  assoc N y z
    =  N <> (y <> z)
    =. y <> z
    =. (N <> y) <> z
    ** QED

  assoc (C x xs) y z
    =  (C x xs) <> (y <> z)
    =. C x (xs <> (y <> z))
    =. C x ((xs <> y) <> z) ? associativity xs y z
    =. (C x (xs <> y)) <> z
    =. ((C x xs) <> y) <> z
    ** QED
 \end{code}
%
As with the left identity, the proof proceeds by
(1) function unfolding (or rewriting in paper and pencil proof terms),
(2) case splitting (or case analysis), and
(3) recursion (or induction).

Since our list implementation satisfies the three monoid laws
we can conclude that @L a@ is a monoid.
%

%% \NV{I need to discuss the difference between representation of proof terms and methods in the logic}

\begin{theorem}\label{theorem:monoid:list}
(@L a@, @epsilon@, @<>@) is a monoid.
\end{theorem}
\begin{proof}
@L a@ is a monoid, as the implementation of
@idLeft@, @idRight@, and @assoc@
satisfy the specifications of
@idLeft_m@, @idRight_m@, and @assoc_m@, with @m = L a@.
\cqed\end{proof}


\documentclass[a4paper,11pt]{article}
\usepackage{amsmath}
\usepackage{array} % format tabular by cm
% define the title
\usepackage{hyperref}
\hypersetup{pdftex,colorlinks=true,allcolors=red}
\usepackage{hypcap}
\author{A.~Faraz}
\title{\AmS{} Package}
\begin{document}
% generates the title
\maketitle
% insert the table of contents
\tableofcontents
\pagebreak
\section{Basics}
\paragraph{Simple}
	Add $a$ squared and $b$ squared
	to get $c$ squared. Or, using
	a more mathematical approach:
	$a^2 + b^2 = c^2$

%tags + label + eqref
\paragraph{Single-line}
	Add $a$ squared and $b$ squared
	to get $c$ squared. Or, using
	a more mathematical approach
	\begin{equation}
	a^2 + b^2 = c^2
	\end{equation}
	Einstein says
	\begin{equation}
	E = mc^2 \label{clever}
	\end{equation}
	He didn’t say
	\begin{equation}
	1 + 1 = 3 \tag{dumb}
	\end{equation}
	This is a reference to
	\eqref{clever}.

\paragraph{Without Numbers}
	Add $a$ squared and $b$ squared
	to get $c$ squared. Or, using
	a more mathematical approach
	\begin{equation*}
	a^2 + b^2 = c^2
	\end{equation*}
	or you can type less for the
	same effect:
	\[ a^2 + b^2 = c^2 \]

\pagebreak
\section{Formula}
\paragraph{Sums \& Lim}
\mbox{}\\
This is text style:
$\lim_{n \to \infty}
\sum_{k=1}^n \frac{1}{k^2}
= \frac{\pi^2}{6}$.
And this is display style:
\begin{equation}
\lim_{n \rightarrow \infty}
\sum_{k=1}^n \frac{1}{k^2}
= \frac{\pi^2}{6}
\end{equation}

\paragraph{Matrix}
\[
\begin{matrix}
a & b & c \\
d & e & f \\
g & h & i
\end{matrix}
\]
\begin{equation*}
M = \begin{bmatrix}
	\frac{5}{6} & \frac{1}{6} & 0           \\[0.3em]
	\frac{5}{6} & 0           & \frac{1}{6} \\[0.3em]
	0           & \frac{5}{6} & \frac{1}{6}
\end{bmatrix}
\end{equation*}
\begin{equation*}
	\begin{matrix}
		1 & 2 \\
		3 & 4
	\end{matrix} \qquad
	\begin{bmatrix}
		p_{11} & p_{12} &
		\ldots
		& p_{1n} \\
		p_{21} & p_{22} &
		\ldots
		& p_{2n} \\
		\vdots & \vdots &
		\ddots
		& \vdots \\
		p_{m1} & p_{m2} &
		\ldots
		& p_{mn}
	\end{bmatrix}
\end{equation*}

\paragraph{Integral}
\[ F(s) = \int_{0}^{\infty}{f(t)e^{-st}}\,dt \]
\[ S(x) = \iint f(x).g(y).h(z)\,dy\,dz\]
\paragraph{Formula + Matrix}
~
% h -> here
% b -> buttom of page
% t -> top of page
% p -> on extra page
% ! -> override(will force specified location)
\begin{table}[h!]
	\centering
	\begin{tabular}{| m{6.1cm} | m{6.1cm} |}
	\hline
	\multicolumn{2}{|c|}{laplace}
	\\
	\hline
	\multicolumn{1}{|c|}{$t \rightarrow s$} & \multicolumn{1}{c|}{$s \rightarrow t$} \\
	\hline
	$F(s) = \int_{0}^{\infty}{f(t).e^{-st}}\,dt$ &
	$f(t) = \newline \frac{1}{2 \pi i}\lim_{T \to \infty} \int_{\gamma - iT}^{\gamma + iT} F(s).e^{st}\, ds$ \\
	\hline
	\end{tabular}
\end{table}

\end{document}