% Created 2022-01-16 Sun 11:18
% Intended LaTeX compiler: pdflatex
\documentclass[presentation]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{xypic}
\usepackage{tabularx}
\xyoption{pdf}
\usetheme{default}
\newcommand\cat[1]{\ensuremath{\mathcal{#1}}}
\newcommand\hm[2]{\hom(#1,#2)}
\newcommand\nat[2]{{#1}\overset{\cdot}{\rightarrow}{#2}}
\newcommand\tup[1]{\langle #1\rangle}
\newcommand\id[1]{\mathrm{id}_{#1}}
\author{jens.jensen@stfc.ac.uk \\0000-0003-4714-184X\\CC-BY 4.0}
\date{April 24, 2022}
\title{Functional Programming 4}
\hypersetup{
 pdfauthor={jens.jensen@stfc.ac.uk},
 pdftitle={Functional Programming 4},
 pdfkeywords={functional, monad, programming theory},
 pdfsubject={functional programming},
 pdfcreator={Emacs 27.1 (Org mode 9.3) then by hand},
 pdflang={English}}
\begin{document}

\maketitle
\begin{frame}{Outline of Talks}

  \begin{itemize}
    \item Previous talks (talks 1-3):
    \begin{itemize}
\item Introibo
\item Pure Functional Programming Principles
    \item Mapping
    \item Labels and naming
    \item Lists
    \end{itemize}
  \item This talk (Talk 4):
    \begin{itemize}
    \item Advanced(ish) Topics
    \end{itemize}
\item Impure Functional? Side Effects
\item Category Theory
\item Categories and Functions
\item Categories and Computation

  \end{itemize}
Still written in the author's spare time!

\medskip

Very much a personal perspective, and not following any particular textbook.  Using \emph{meditations} and \emph{exercises} -- solutions to all exercises given during the talks.

\end{frame}

\section{Introibo}
\label{sec:org0edb596}

\begin{frame}{Superpowertools -- Lemonodor fame awaits!}
  Summarising the Lisp functional programming Super Power Power Tools we have met (plus one from an earlier talk):
  \begin{itemize}
  \item \emph{Recursion}, the elegant engine of functional programming;
  \item \texttt{list} and list tools, designed for the first/rest paradigm;
  \item \texttt{cond} and friends for dividing and conquering;
  \item Mapping functions (and \texttt{reduce}) implement functional patterns concisely (including recursion, without recursing)
  \item Sequence functions such as \texttt{reduce}
  \item Functions: \texttt{funcall}, \texttt{lambda}, \texttt{apply}; multivalued in CL
  \item \texttt{lambda} lists in Lisp are much more powerful than other functional languages -- more powerful than other languages;
  \item \texttt{flet} and \texttt{labels} for defining local functions;
  \item \emph{macros}, because Lisp macros are superpowertools for everything
  \item \emph{stubs}, and \texttt{trace} (the latter CL only, but see later)
  \end{itemize}
\end{frame}

\section{Advanced(ish) Functional Programming}
\begin{frame}{Common/Advanced(ish) Features of Functional Languages}
\label{sec:org95ddb74}

\begin{enumerate}
\item Lambda (anonymous (unnamed) functions) and \emph{currying}
\item List comprehension
\item Functions -- mutually recursive, higher order
\item Symbols
\item Tail recursion
\item Scope and extent (Lisp)
\item Types and type inference
\item Branch-on-pattern-matching and guards
\item Memoisation
\item Lazy evaluation types
\item Pipes (not the lazy kind) style composition
\begin{itemize}
\item  \(h(g(f(x)))\equiv\texttt{(h (g (f x)))}\equiv{}x\vert{f}\vert{g}\vert{h}\)
\end{itemize}
\item Monads: theoretical framework for types and computation
\item Applied monads: Maybe, Arrays
\item Bonus section for survivors of MonadLand: Lisp Hacking
\end{enumerate}

\end{frame}

\begin{frame}[fragile]{Common Features of Functional Languages - Currying}
\begin{itemize}
\item Currying (binding some but not all function arguments)
\begin{itemize}
\item In this example \texttt{+} is curryed, binding its second argument to 2
\begin{itemize}
\item (Assuming \texttt{+} is considered as a function of \emph{two} args (which it isn't))
\end{itemize}
\item ``Proper'' functional languages like F\# do this more elegantly
\begin{itemize}
\item As long as we bind the \emph{first argument}
\item E.g.\ \texttt{+ 2} would be the function that adds two to something
\end{itemize}
\end{itemize}
\end{itemize}

\begin{verbatim}
(mapcar (lambda (x) (+ x 2)) '(1 2 3 4))
(3 4 5 6)
\end{verbatim}

\begin{itemize}
\item Hence the \emph{type} notation of a function \(+::\mathrm{int}\rightarrow\mathrm{int}\rightarrow\mathrm{int}\)
\item After currying the (first) argument, the expr has type \(::\mathrm{int}\rightarrow\mathrm{int}\)
\item Of course in Lisp, \texttt{\#'+} takes any number of arguments (incl 0)
\item In talk 2 we had \texttt{(apply \#'mapcar \#'list rows)} where \texttt{apply} effectively curryes \texttt{mapcar} by binding \texttt{\#'list} as its first argument
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Lambdas}
  Almost but not quite a translation of $\lambda$ calculus.  Taking the Quine
  \begin{equation*}
    ((\lambda x. (x x)) (\lambda x. (x x)))
  \end{equation*}
  becomes in EL
\begin{verbatim}
((lambda (x) (list x x)) (lambda (x) (list x x)))
((closure (t) (x) (list x x)) (closure (t) (x) (list x x)))
\end{verbatim}
which is a Quine if you appreciate that evaluating \texttt{lambda} \texttt{coerce}s the \texttt{lambda} expression into a function
\begin{verbatim}
(lambda (v) (+ v 2))
(closure (t) (v) (+ v 2))
(coerce (lambda (v) (+ v 2)) 'function)
(closure (t) (v) (+ v 2))
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Lambdas}
  In CL, functions can return multiple values (including none) but special tools are needed to capture the not-first values:
\begin{verbatim}
(floor 12 7)
1
5
(multiple-value-bind (q r) (floor 12 7)
  (+ (* q 7) r))
12
\end{verbatim}
When not used in a multiple value context, only \texttt{floor}'s first value is used (as in EL where it returns only one value):
\begin{verbatim}
(+ (floor 12 7) 3)
4
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Lambdas}
Both \texttt{lambda} and \texttt{let} create variable bindings:
\begin{verbatim}
((lambda (v)
  (+ v (let ((v 2)) (* 3 v))))
  6)
12
\end{verbatim}
\begin{itemize}
\item The inner \texttt{v} is bound by \texttt{let} to the value 2
\item The outer \texttt{v} is bound by \texttt{lambda} to the value 6
\item It's the same symbol \texttt{v} but the inner binding shadows the outer during execution of the \texttt{let}
\item The outermost parenthesis
\end{itemize}
\end{frame}

\begin{frame}[fragile]{List comprehension}
\emph{List comprehension} is the idea of constructing elements
$$(f(x,y,\ldots)\vert x\in A, y\in B(x),\ldots)$$
analogous to how sets are constructed (this is obviously not a precise definition but a it-will-do-for-now expression of the idea).

\medskip
Arguably, list comprehension for Lisp is \texttt{loop}, even though it looks imperative sometimes:
\begin{verbatim}
(loop for i from 1 to 10 collecting (* i i))
(1 4 9 16 25 36 49 64 81 100)
\end{verbatim}
but this could be done with \texttt{iota} functionally.  It can express actions on lists (and vectors) equally naturally:
\begin{verbatim}
(loop for a in '(1 2) nconcing
  (loop for b in '(3 4) collecting (cons a b)))
((1 . 3) (1 . 4) (2 . 3) (2 . 4))
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{List comprehension}
  Another common case is calling a function (with side effects!) $n$ times, collecting the results (which means we can't just use \texttt{dotimes}):
\begin{verbatim}
(loop repeat 10 collect (random 10))
(4 9 1 2 9 3 8 0 0 8)
\end{verbatim}
The alternative could be
\begin{verbatim}
(map 'vector (lambda (x) (random 10)) (make-vector 10 0))
[8 3 3 9 8 6 5 0 5 3]
\end{verbatim}
which would have made more sense with CL's \texttt{map-into}
\begin{verbatim}
(let ((v (make-array 10)))
  (map-into v
    (lambda (x) (declare (ignore x)) (random 10))
    v))
\end{verbatim}
\end{frame}

\begin{frame}{Mutually recursive functions}
  A puzzle: given a number $N$, create an array of length $2N$ with each of the digits $1,\ldots,N$ occurring precisely twice, such that for every digit $k\in\{1,\ldots,N\}$ there is $k$ digits between the two occurrences of the digit $k$.

  \medskip
  Example for $N=3$: \texttt{312132}.  Starting digits may be given, e.g.\ starting with \texttt{*1*1**}

  \medskip
  Solving it functionally, the original design had three functions calling each other -- now we're down to two...
\end{frame}

\begin{frame}{Functions -- Mutually recursive functions}
  \resizebox{!}{8cm}
            {
              \includegraphics{mutual.pdf}
            }
\end{frame}

\begin{frame}[fragile]{Functions -- Hooks and callbacks}
Getting one value out of a deeply nested computation: throw it.
\begin{verbatim}
(... (when (goalp state) (throw 'found state)) ...)
\end{verbatim}
(for some hypothetical function \texttt{goalp}.)  When we need to collect multiple values, a callback makes sense:
\begin{verbatim}
(... (when (goalp state) (funcall cb state)) ...)
\end{verbatim}
Later we shall look at other ways of solving the same problem.
\end{frame}

\begin{frame}[fragile]{Functions -- Hooks and callbacks}
  Suppose for a moment we want Fibonacci numbers (to which we will also return later), starting with the CL version:
\begin{verbatim}
(defun fib (k &optional (a 0) (b 1))
  (if (zerop k) b
    (fib (1- k) b (+ a b))))
FIB
(fib 10)
89
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Functions -- Hooks and callbacks}
  In CL we can now do
\begin{verbatim}
(trace fib)
(FIB)
(fib 5)
  0: (FIB 5)
    1: (FIB 4 1 1)
      2: (FIB 3 1 2)
        3: (FIB 2 2 3)
          4: (FIB 1 3 5)
            5: (FIB 0 5 8)
            5: FIB returned 8
          4: FIB returned 8
        3: FIB returned 8
      2: FIB returned 8
    1: FIB returned 8
  0: FIB returned 8
8
(untrace fib)
T
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Functions -- Hooks and callbacks}
In EL, the \texttt{fib} function could look like this:
\begin{verbatim}
(defun fib (k &optional a b)
  (if (zerop k) (or b 1)
    (fib (1- k) (or b 1) (+ (or a 0) (or b 1)))))
fib
(fib 10)
89
\end{verbatim}

\end{frame}

\begin{frame}[fragile]{Functions -- Hooks and callbacks}
  EL uniquely allows us to alter functions with \emph{advice}:
\begin{verbatim}
(defun my-trace (name func args)
  ;; Enter function
  (princ (format "%s: %s\n" name args))
  ;; Call function and remember value
  (let ((val (apply func args)))
    ;; Print result
    (princ (format "%s => %s\n" name val))
    val))
(advice-add 'fib :around
  (lambda (func &rest args) (my-trace 'fib func args))
  '((name . trace)))
nil
\end{verbatim}
In this respect, advice work quite a lot like methods (specifically, standard method combination) in CLOS
\end{frame}
\begin{frame}[fragile]{Functions -- Hooks and callbacks}
\begin{verbatim}
(fib 5)
fib: (5)
fib: (4 1 1)
fib: (3 1 2)
fib: (2 2 3)
fib: (1 3 5)
fib: (0 5 8)
fib => 8
fib => 8
fib => 8
fib => 8
fib => 8
fib => 8
8
(advice-remove 'fib 'trace)
nil
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Functions -- Hooks and callbacks}
Emacs uses \emph{hooks} a lot, callbacks called at specific times.  Normal hooks are single function or (more or less) lists of functions to be called without arguments:
\begin{verbatim}
text-mode-hook
(text-mode-hook-identify)
(add-hook 'text-mode-hook 'auto-fill-mode)
(auto-fill-mode text-mode-hook-identify)
c++-mode-hook
(irony-mode)
(add-hook 'c++-mode-hook
  (lambda () (message "Hack and be merry!"))
  10)
(irony-mode (closure (t) nil (message "Hack and be merry!")))
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Functions -- Higher order functions}
\begin{verbatim}
(defun compose (f g)
  "Compose two functions calling g first then f"
  (lambda (&rest data) (funcall f (apply g data))))
compose
(funcall (compose #'sqrt #'+) 2 3 4)
3.0
(mapcar (compose #'sqrt #'abs) '(-1 -4 9 -16))
(1.0 2.0 3.0 4.0)
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Functions -- Higher order functions}
The CL standard (ANSI X3J13) has tried to deprecate the \texttt{-if-not} functions in favour of the \texttt{-if}s:
\begin{verbatim}
(remove-if-not #'evenp '(1 2 3 4 5 6))
(2 4 6)
(remove-if (complement #'evenp) '(1 2 3 4 5 6))
(2 4 6)
\end{verbatim}
The first of these works fine in EL but EL does not have \texttt{complement}.  However, we can easily write it:
\begin{verbatim}
(defun complement (func)
  "Logical complement of a function of any number of args"
  (lambda (&rest args)
    (not (apply func args))))
\end{verbatim}
Notice that no \texttt{funcall} is required when we use it (why not?)
\end{frame}

\begin{frame}[fragile]{Symbols}
  \emph{Symbols} are one of the important tools of Lisp, as
  \begin{itemize}
  \item Names -- of variables, functions, catches, types, structures, classes, etc.
  \item Enums (eg.\ \texttt{(list 'Jan 'Feb 'Mar ...)})
  \item Keys for hooks or looking up stuff
  \item Anywhere else where you need an atom
  \end{itemize}

What is going on here (this is EL but could be CL with one difference):
\begin{verbatim}
(atom nil)
t
(eq 'fred 'Fred)
nil
(eq 'fred 'fred)
t
(eq (make-symbol "fred") (make-symbol "fred"))
nil
\end{verbatim}

\end{frame}

\begin{frame}[fragile]{Symbols}
  A symbol has precisely three things:
  \begin{itemize}
  \item A \emph{name} (or more accurately, a \emph{print name}):
  \end{itemize}
\begin{verbatim}
(symbol-name 'fred)
"fred"
(symbol-name (make-symbol "wilma"))
"wilma"
\end{verbatim}
\begin{itemize}
\item A \emph{package} (sort of, EL doesn't really do packages)
\item A \emph{property list}:
\end{itemize}
\begin{verbatim}
(symbol-plist 'fred)
nil
(symbol-plist 'car)
(byte-compile byte-compile-one-arg byte-opcode byte-car ...
... byte-optimizer byte-optimize-predicate
    side-effect-free t)
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Symbols}
If we take the plist first, it provides key/value lookup.  Each symbol has its own plist.
\begin{verbatim}
(get 'fred 'foo)
nil
(setf (get 'fred 'foo) 'blemps)
blemps
(get 'fred 'foo)
blemps
(setf (get 'fred 'bar) 17)
17
(symbol-plist 'fred)
(foo blemps bar 17)
\end{verbatim}
It works exactly like an alist except
\begin{itemize}
\item for hysterical raisins the structure is different
\item it returns the value rather than the key/value
\item it does not normally shadow
\item it has fewer lookup functions (no equiv of \texttt{assoc-if})
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Symbols}
Unlike CL, Emacs uses property lists extensively:
\begin{verbatim}
(get 'sort 'side-effect-free)
nil
(get 'mapcar 'side-effect-free)
nil
(get 'cons 'side-effect-free)
error-free
(get '+ 'side-effect-free)
t
\end{verbatim}
Keys should normally be symbols, though values need not be
\begin{verbatim}
(setf (get 'fred "name") "jones")
"jones"
(get 'fred "name")
nil
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Symbols}
Removing a key (``tag'') from a plist is slightly different:
\begin{verbatim}
(remprop 'fred 'foo)
t
(symbol-plist 'fred)
(bar 17)
\end{verbatim}

Altering plists of EL's built in functions is not advisable!

\medskip
There are functions to work directly on plists:
\begin{verbatim}
(let ((plist (list 'a 1 'b 2)))
  (setf (getf plist 'c) 3)
  (remf plist 'b)
  plist)
(c 3 a 1)
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Symbols}
Let's return to the packages.  In simplified terms, a package is a context where symbols can be looked up; when the Reader first reads a symbol it is created:
\begin{verbatim}
(eq 'fred 'fred)
t
\end{verbatim}
The same symbol is referenced twice, so obviously \texttt{eq}.  However, a small number of functions and macros create symbols that are \emph{not in any package}, so cannot be looked up again:
\begin{verbatim}
(make-symbol "fred")
fred
(eq (make-symbol "fred") 'fred)
nil
\end{verbatim}
Here \texttt{'fred} is in the package but \texttt{make-symbol} creates one that isn't, so they can never be \texttt{eq}: effectively, symbols not in the (same) package cannot be \texttt{eq} even if they have the same name.
\end{frame}

\begin{frame}[fragile]{Symbols}
We use this construction to write macros (this is CL from the author's Advent of Code 14/12/2021):
\begin{verbatim}
(defmacro incf-list2-entry (list x y delta)
  "Create or add delta to the entry in list for the pair x y"
  (let ((bzz (gensym)) (garg (gensym)))
    `(let* ((,bzz (cons ,x ,y))
            (,garg (assoc ,bzz ,list :test #'equal)))
       (if ,garg
           (incf (cdr ,garg) ,delta)
           (setf ,list (acons ,bzz ,delta ,list)))
       ,list)))
\end{verbatim}
(For EL code, leave out the \texttt{:test \#'equal}).  Dodgy names apart, the macro is a variation on \texttt{incf} for alists of the form
\begin{verbatim}
(((C . B) . 1) ((N . C) . 1) ((N . N) . 1))
\end{verbatim}
-- looking up a pair \texttt{(x . y)}, it creates or adds to the pair.
\end{frame}

\begin{frame}[fragile]{Symbols}
The Reader can also create symbols not-in-any-package using Reader macros:
\begin{verbatim}
(eq '#:fred 'fred)
nil
(eq '#:fred '#:fred)
nil
(symbol-name '#:fred)
"fred"
\end{verbatim}
The Reader macro \texttt{\#:} creates a symbol with the same print name but it is in no package; the next time the Reader reads the same characters, a different symbol is created.
\end{frame}

\begin{frame}[fragile]{Symbols}
Since it is created by the Reader (before Eval), we need to use other Reader constructions to reference the symbol (EL code):
\begin{verbatim}
(defmacro incf-list2-entry (list x y delta)
  `(let* ((#1=#:bzz (cons ,x ,y))
          (#2=#:garg (assoc #1# ,list)))
       (if #2#
           (incf (cdr #2#) ,delta)
           (setf ,list (acons #1# ,delta ,list)))
       ,list))
incf-list2-entry
(let ((mylist (list '((a . b) . 3))))
  (incf-list2-entry mylist 'a 'b 2))
(((a . b) . 5))
(let ((mylist (list '((a . b) . 3))))
  (incf-list2-entry mylist 'x 'y 17))
(((x . y) . 17) ((a . b) . 3))
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Symbols}
Now let's return to symbols that \emph{are} in ``the'' package, called \emph{interned} symbols (symbols not in a package are called \emph{uninterned}):
\begin{verbatim}
(eq 'fred (intern "fred"))
t
(eq (intern "fred") (intern "fred"))
t
\end{verbatim}

\medskip
Symbols are atoms and not sequences; if we really wanted to turn a list of two elements \texttt{(fizz buzz)} into a single symbol \texttt{fizzbuzz}, we'd have to do (EL code)
\begin{verbatim}
(defun join-symbols (w)
  (if (endp (cdr w)) ; 0-1 elt
      (first w)
    (intern (concat (symbol-name (first w))
                    (symbol-name (second w))))))
join-symbols
(join-symbols '(fizz buzz))
fizzbuzz
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Symbols}
Symbols -- whether interned or not -- have few restrictions on their names: this is a single symbol:
\begin{verbatim}
(symbolp 'b^2-4*a*c/2*a)
t
\end{verbatim}
but notice we cannot add parentheses for the denominator as the Reader would start constructing a list.  Likewise, \texttt{1-} is a symbol, the name of the function
\begin{verbatim}
(symbol-function #'1-)
#<subr 1->
\end{verbatim}
Also note that in CL (but not in EL), the Reader maps all symbols to upper case:
\begin{verbatim}
* (eq 'fred 'FRED)
T
* 'fred
FRED
\end{verbatim}
\end{frame}

\end{document}


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

\subsection{Type Signatures}

\begin{lstlisting}
;; int
;; 2

(define p1 (a-program (const-exp 2)))
\end{lstlisting}

\begin{lstlisting}
;; (int -> int) -> (int ->bool)
;; proc(x:?)
;;     proc(y:?)
;;         zero? ((x 0) - y)

(define ex1
    (a-program
      (proc-exp 'x (no-type)
        (proc-exp 'y (no-type)
         (zero?-exp
           (diff-exp
             (call-exp
               (var-exp 'x)
               (const-exp 0))
             (var-exp 'y)))))))
\end{lstlisting}

\begin{lstlisting}
;; (int->int) -> (bool->bool)
;; proc(x:?)
;;     proc(y:?)
;;         if y
;;         then zero? ((x 0) - 10)
;;         else zero? ((x 0) - 0)

(define ex2
  (a-program
   (proc-exp 'x (no-type)
             (proc-exp 'y (no-type)
                       (if-exp (var-exp 'y)
                               (zero?-exp (diff-exp
                                           (call-exp (var-exp 'x) (const-exp 0))
                                           (const-exp 10)))
                               (zero?-exp (diff-exp
                                           (call-exp (var-exp 'x) (const-exp 0))
                                           (const-exp 0))))))))
\end{lstlisting}

\begin{lstlisting}
;; %1
;; letrec ? f(x:?) = (f x) in (f 3)

(define ex3
  (a-program
   (letrec-exp (no-type) ; Proc return type
               'f ; Proc name
               'x ; Proc arg name
               (no-type) ; Proc arg type
               (call-exp (var-exp 'f) (var-exp 'x)) ; Proc arg body
               (call-exp (var-exp 'f) (const-exp 3))))) ; Letrec body, type of f.

\end{lstlisting}

\begin{lstlisting}
;; (%1 -> %2) -> (%2 -> int) -> (%1 -> bool)
;; proc(f) proc(g) proc(x) (zero? (g (f x)))

(define d
  (a-program
   (proc-exp 'f (no-type)
             (proc-exp 'g (no-type)
                       (proc-exp 'x (no-type)
                                 (zero?-exp (call-exp (var-exp 'g)
                                                      (call-exp (var-exp 'f) (var-exp 'x)))))))))
\end{lstlisting}

\begin{lstlisting}
;; %1 -> %2
;;letrec ? f(x:?) = (f x) in f

(define ex5
  (a-program
   (letrec-exp (no-type) ; Proc return type
               'f ; Proc name
               'x ; Proc arg name
               (no-type) ; Proc arg type
               (call-exp (var-exp 'f) (var-exp 'x)) ; Proc arg body
               (var-exp 'f)))) ; Letrec body, a function

\end{lstlisting}

\begin{lstlisting}
;; (%1 -> %2) -> %1
;; proc (g) letrec f(x) = f x in let y = ( g ( f x) ) in (f x)

(define f
  (a-program
   (proc-exp 'g
             (no-type)
             (letrec-exp (no-type)
                         'f
                         'x
                         (no-type)
                         (call-exp (var-exp 'f) (var-exp 'x))
                         (let-exp 'y
                                  (call-exp (var-exp 'g)
                                            (call-exp (var-exp 'f)
                                                      (var-exp 'x)))
                                  (call-exp (var-exp 'f) (var-exp 'x)))))))
\end{lstlisting}

\subsection{CHECKED Tuple Extension}

\end{document}


\documentclass[10pt,fleqn]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fullpage}
\usepackage{amsmath,amssymb,amsthm,mathtools}
\usepackage{mathrsfs} % mathscr
\usepackage{color}
\usepackage[paperwidth=160mm,paperheight=90mm,margin=10mm]{geometry}

% multicol
\usepackage{multicol}
\usepackage{array}

% graphics
\usepackage{graphicx}
\DeclareGraphicsRule{*}{mps}{*}{}

\usepackage{hyperref}
\hypersetup{
  colorlinks=true
}

\usepackage{mathpazo}
\usepackage{helvet}
%\usepackage{courier}
\usepackage{inconsolata}

\usepackage{titling}
\newcommand{\subtitle}[1]{%
  \posttitle{%
    \par\end{center}
    \begin{center}\large#1\end{center}
    \vskip0.5em}%
}

\title{$\mathbb{MENRVA}$}
\subtitle{@futurice freaklies}
\author{Oleg Grenrus}
\date{July 03, 2014}

\DeclareMathOperator*{\inr}{\mathsf{inr}}
\DeclareMathOperator*{\inl}{\mathsf{inl}}
\DeclareMathOperator*{\none}{\mathsf{none}}
\DeclareMathOperator*{\some}{\mathsf{some}}
\DeclareMathOperator*{\ap}{\mathsf{ap}}
\DeclareMathOperator*{\fst}{\mathsf{fst}}
\DeclareMathOperator*{\snd}{\mathsf{snd}}

% Theorems
% http://en.wikibooks.org/wiki/LaTeX/Theorems

\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]
\newtheorem{example}[definition]{Example}
\newtheorem{exercise}[definition]{Exercise}
\newtheorem{note}[definition]{Note}

\theoremstyle{plain}
\newtheorem{proposition}[definition]{Proposition}
\newtheorem{lemma}[definition]{Lemma}
\newtheorem{theorem}[definition]{Theorem}

\pagestyle{empty}

\definecolor{blue}{rgb}{0,0,0.5}
\definecolor{green}{rgb}{0,0.5,0}
\definecolor{red}{rgb}{0.5,0,0}
\definecolor{orange}{rgb}{1,0.5,0}
\definecolor{darkorange}{rgb}{0.5,0.25,0}
\definecolor{lightgrey}{rgb}{0.7,0.7,0.7}
\definecolor{grey}{rgb}{0.5,0.5,0.5}

\setlength\parindent{0pt}
\setlength{\parskip}{3mm plus1mm minus1mm}

\newcommand{\identifier}[1]{\texttt{\color{blue}#1}}
\newcommand{\constructor}[1]{\ensuremath{\mathsf{\color{green}#1}}}
\newcommand{\functor}[1]{\ensuremath{\mathcal{\color{darkorange}#1}}}

\begin{document}

\maketitle
\thispagestyle{empty}
\newpage

{\LARGE$\mathbb{MENRVA}$-- yet another reactive library}

\href{https://github.com/baconjs/bacon.js}{\textsc{Bacon.js}} is (almost) non-glitchy, but not-so performant.
\href{https://github.com/Reactive-Extensions/RxJS}{\textsc{RxJS}} is glitchy but performant.

I begin to bias towards using cold-observables of \textsc{RxJS}\footnote{Can use \textsc{Bacon.js} too, but I don't need glitch-free events} (the good parts?) with data-flow propagation of something else ($=$ \textsc{Menrva}).

\begin{center}
\includegraphics[scale=0.7]{figures/data-events.mps}
\end{center}

\newpage

\section*{Semantics}

Following definitions are from \emph{Push-pull functional reactive programming}\footnote{Conal Elliott, \emph{Push-pull functional reactive programming}, Haskell Symposium, 2009, \url{http://conal.net/papers/push-pull-frp/}}.

\subsection*{FRP -- Behavior}

Semantically, a \emph{(reactive) behavior} is just a function of time. In \textsc{Menrva} we call them \emph{signals}.
\begin{align*}
\mathcal{S}\,\alpha = \mathcal{T} \to \alpha
\end{align*}

Historically in FRP, $\mathcal{T} = \mathbb{R}$. As we’ll see, however, the semantics of behaviors assumes only that $\mathcal{T}$ is totally ordered.

\newpage

\subsubsection*{Applicative functor}

Pure functions can be "lifted" to apply to signals. Classic FRP (CFRP) had a family of lifting combinators:
\begin{align*}
\texttt{lift}_n &:: (\alpha_1 \to \cdots \alpha_n \to \beta) \to (\mathcal{S}\,\alpha_1 \to \cdots \mathcal{S}\,\alpha_n \to \mathcal{S}\,\beta)
\end{align*}
Lifting is pointwise and synchronous:
\begin{align*}
\texttt{lift}_n\,f\,b_1\cdots b_n &= \lambda\,t \mapsto f (b_1\, t)\cdots (b_n\, t)
\end{align*}
The \emph{Functor} \texttt{map} is $\texttt{lift}_1$:
\begin{align*}
\texttt{map}\,f\,b = f \circ b
\end{align*}

\newpage

In \textsc{Menrva} \texttt{map} is \texttt{map} and $\texttt{lift}_n$ is \texttt{combine} (with function parameter at the end):
\begin{verbatim}
var $sq = $source.map(function (x) {
  return x * x;
});

var $quad = menrva.combine($a, $b, $c, function (a, b, c) {
  return a * b + c;
});
\end{verbatim}

And that's the whole (functional) API.

{\small There is \texttt{onValue} to observe changes. And \texttt{set} and \texttt{modify} to initiate changes.}

\newpage

\subsubsection*{Monad}

Although Signal is a semantic \emph{Monad} as well, \textsc{Menrva} doesn't have monadic \texttt{join} combinator.

Semantic of \texttt{join} is simple, but the correct implementation hard.
\begin{align*}
\texttt{join}\,s = t \mapsto s\,t\,t
\end{align*}

Monad instance would make possible to make dynamic data flow networks. With applicative functor the shape of the network is static (and you can't make loops) $\equiv$ simple and easy.

\newpage

\section*{Examples}

\url{https://github.com/phadej/menrva/tree/master/examples}

\begin{itemize}
\item \emph{counter} examples (pun not-intended). With data-first approach, you segregate \emph{query} from \emph{control}.
\item \emph{suggestions} \href{https://gist.github.com/staltz/868e7e9bc2a7b8c1f754}{\emph{The introduction to Reactive Programming you've been missing}}
\end{itemize}

\newpage

\section*{What are plans for \textsc{Menrva}?}

Write more examples, to verify that \textsc{Menrva}'s simplistic approach is enough for the real world.

\hspace{5mm}\vdots

Use in the real world application.

\hspace{5mm}\vdots

PROFIT!

\newpage

\section*{What are you going to add still?}

Something to handle network destruction.
% Currently all nodes are double linked (parent $\leftrightarrow$ child).
Thus dynamically created (and destroyed) UI components create leaf-subnetwork which should be cleaned up.
\textsc{JavaScript} doesn't have weak references (which might be enough to solve the problem).
So we need some manual mechanism for cleanup.

One idea is to make own reference counting:
\begin{verbatim}
var $state = menrva.source(...);
var $derived = $state.map(...).retain();

var $componentData = $derived.map(...).retain();
// cleanup:
$componentData.release(); // also removes all callbacks etc.
\end{verbatim}
\newpage

\section*{What are you going to add still? -- 2}

Should we have monadic join? (= \texttt{flatMap}). Do we really need it? Can be worked around, but sometimes it is convenient:
{\small
\begin{verbatim}
$artistImage = $artist.flatmap(getArtistImageSignal);

function getArtistImageSignal(artist) {
  if (!cache[artist.id]) {
    cache[artist.id] = menrva.source(placeholderImage);

    // schedule carousel for images...
  }
  return cache[artist.id];
}
\end{verbatim}
}
Here the cache (or artist image store) can be signal itself -- no need for \texttt{flatMap}.

\section*{What are you going to add still? -- 3}

\emph{Borrow} goodies from \textsc{Bacon.js} -- Function construction rules:
\begin{verbatim}
$signal.map(".foo"); // shorter way of saying:
$signal.map(function (x) {
  return x.foo;
});
\end{verbatim}

\newpage

\section*{What are you going to add still? -- 4}

Lenses. Sub-value of source acting as source itself.
\begin{verbatim}
var $foobar = $source.lens("foo.bar");

$foobar.set(tx, value); // the same as
$source.modify(tx, function (source) {
  return assocIn(source, ["foo", "bar"], value);
});
\end{verbatim}
\texttt{assocIn} -- \url{http://clojuredocs.org/clojure_core/clojure.core/assoc-in}

\newpage

\section*{And why the choice for transactions?}

You able to update many source simultaneously, avoiding immediate propagation:
\begin{verbatim}
var $a = menrva.source(1);
var $b = menrva.source(2);

menrva.combine($a, $b, add).onValue(console.log);

var tx = menrva.transaction();
$a.set(tx, 2);
$b.set(tx, 3);
tx.commit();
\end{verbatim}

\section*{And why the choice for transactions? -- 2}

Also to mention, transactions arise naturally, if you try to implement anything similar in
\textsc{Haskell} or \textsc{Clojure} (using \textsc{STM}).

There you will be forced to do all mutation inside STM transaction.

Changes initiated in transaction are applied only when you commit the transaction: you \emph{always} have consistent data.

\newpage
{\LARGE the end}
\newpage

\end{document}


%\section{Physical Human Factors Report: Tristan Griffith}
\rhead{\today}
\begin{center}
{\textbf{\Large Brief on Modal EEG Fingerprinting}}\\
\vspace{2mm}
{\large  Tristan Griffith}\\
\vspace{2mm}
{\large Dr. James Hubbard Jr.}
\noindent\rule{\textwidth}{2pt}
\end{center}

%\begin{wrapfigure}{r}{0.45\textwidth}
%\centering
%\includegraphics[scale=.3]{../../../figures/demo_map.png}
%\caption{Modal Heatmap for Subject 32}
%\end{wrapfigure}
\subsection{Motivation}
A correlation between electrical activity in the brain (as electroencephalography) and cognition has already been established \cite{alarcao2017emotions}. However, formal models of this relationship have thus far been experimentally limited. We postulate that data driven system identification methods of EEG data, such as Output Only Modal Analysis and Dynamic Mode Decomposition, may yield predictive models of cognition for more complete information flow between human operators and co-robots. As an initial step to verify that there is statistically significant information in these modal representations, an artificial neural network is proposed to discriminate subjects in a database. If the network shows sufficient accuracy in identifying subjects from the modal representation of their brain activity, we gain confidence that the modal representation is capturing significant information in the modes. This document surveys this approach and details the results of the network. It is shown that the network can discriminate 32 subjects from one another with $<99$\% accuracy based on knowledge of the significant modes alone.
\subsection{The EEG Dataset}
The data used to test this modal decomposition approach is primarily the DEAP dataset \cite{koelstra2011deap}. This dataset was established to aid the analysis of human affective states. It includes 40 different minute long  trials for 32 different subjects. From this timeseries EEG data with 32 available spatial channels, 1280 modal decompositions will be performed. 1024 of these decompositions are provided to the neural network for training, while the remaining 256 decompositons will be used to validate the performance of the network. A closer look at the modal decomposition follows.
\subsection{Modal Decomposition of Data}
\subsubsection{Output Only Modal Analysis (OMA)}
Output Only Modal Analysis (OMA) algorithms have been in use since at least the 90's \cite{peeters1999reference}. Originally developed for large mechanical structures, which are extremely difficult to precisely excite, OMA algorithms assume broadband stochastic input to the system in order to determine a state space model for a given system. These algorithms fall under a broader class of subspace identification methods, which rely directly on measured data. The specific method applied to this data results from solving a set of linear equations with least squares to match data held in a Hankel matrix \cite{261604}. Empirically, we have seen this algorithm perform more consistently on the DEAP dataset. That is, there is less variance in the generated modes for a given subject.

%% ADD PLOT OF CLASSIFICATION ACC VS NUMBER OF CHANNELS, ALSO SYSTEM ORDER
To perform the modal decomposition presented below, the first 16 channels out of 32 available were selected for analysis. These 16 channels correspond to the electrodes placed on the front half of the scalp. The selection was driven by computational limits and the desire to use as few channels as possible for subject identification. Selection of the appropriate system order is also important. This is generally determined from the stability diagram, which gives insight into how much the
\begin{wrapfigure}{r}{0.55\textwidth}
\centering
\includegraphics[scale=0.6]{../../../figures/S19T38.png}
\caption{Stability Diagram for DEAP}
\label{fig:stabdiag}
\end{wrapfigure}
poles and zeros are changing as the model order increases. That is, we can track the percent change in the norm of the poles and zeros as additional states are added to the system. From Figure \ref{fig:stabdiag}, it can be seen that the system properties are more unstable than is typical for mechanical structures, especially at the higher frequencies. For the purposes of this brief, the selected subject order was 25, in order to capture the faster modes near the 45 hertz range. With these selections, 20 linearly independent, but not orthogonal modes are identified, ranging in frequency from 3-45 hertz. We postulate that these eigenvectors are relevant to emergent cognitive behaviors, such as engagement.

\subsubsection{Dynamic Mode Decomposition (DMD)}
While OMA algorithms are considered subspace identification algorithms, there are other modal decomposition tools available. These include independent component analysis, principle component analysis, and proper orthogonal decomposition. An extension of proper orthogonal decomposition (POD) known as dynamic mode decomposition (DMD) has gained traction as data driven dynamic modeling and control becomes of increasing interest to the research community. DMD is based on the singular value decomposition, combining the spatial analysis of POD with the temporal analysis of the Fourier transform to estimate a state matrix directly from measured data. Unlike OMA algorithms, it makes no assumptions on the nature of the input. It has shown wide application in modeling of fluids dynamics \cite{schmid2010dynamic}, biological systems \cite{brunton2016extracting}, and human diseases \cite{proctor2015discovering}.

Since DMD is a singular value method, generated modes are sorted by their importance in explaining the variance in the data. Once an acceptably accurate number of modes is selected, the state matrix may be truncated to that number of modes. Previous work has found selecting too many modes leads to decreased classification performance, due to the introduction of spurious modes as a form of noise \cite{jung1997estimating}.

\begin{figure}
\centering
\includegraphics[scale=0.85]{../../../figures/S_r.png}
\caption{Significant Singular Values for DMD}
\label{fig:svd}
\end{figure}

For this data, approximately 80 modes were selected, describing $\approx 85$\% of the total energy in the system. This can be seen in Figure \ref{fig:svd} below. Keep in mind that as with OMA algorithms, DMD will generate complex conjugate mode pairs, so only 40 independent mode shapes of interest are actually identified.

\subsection{Modal Decomposition as a Heatmap}
Having generated both OMA and DMD modes for 32 subjects over 40 trials in this data set, it is worth validating the quality of information in the modes. As a first check, we postulate that each of the 32 subjects may be distinguished from one another based solely on the significant modes. If a classification technique shows sufficient accuracy distinguishing subjects, we gain confidence that the modes contain statistically significant information.

\begin{wrapfigure}{r}{0.6\textwidth}
\centering
\includegraphics[scale=0.45]{../../../figures/demo_map.png}
\caption{Modal Information Encoded as an Image}
\label{fig:heatmaps}
\end{wrapfigure}
The most obvious technique for this EEG fingerprinting classification is neural networks. Properly encoding these networks for efficient classification is important. While networks will readily accept modal information in a tabular form, recent advancements in gradient decent methods, transfer learning, etc are especially geared for three channel images. It is therefore desirable to transform our modal information into an RGB image. Image encoding of temporal and spatial information has seen wide application of late, with good success \cite{ke2020intersected, venugopal2020unboxing, bruno2019using}. The most obvious solution is to encode modal information as a heatmap with a color scale. Since both DMD and OMA algorithms allow for complex (out of phase) mode components, it is necessary to include both real and complex components as part of the image. This may be included as either a real and complex component or in polar form as an angle and amplitude. Along the x-axis of the figure is each of the modes, increasing in temporal frequency. Along the y-axis of the figure is the real or imaginary component of each spatial location of the given mode. This forms an image that is well suited for neural network classification. The authors wish to note that the application of machine learning in this case is used only as a validation technique for the modeling. The network is not used to discover the underlying dynamics of the system. Here, they merely serve as a check on the system identification techniques presented above.

\subsection{Neural Nets for Fingerprinting}
Having encoded our modal decompositions as images, the images may be fed to a neural network. Since it's inception in 2016, the ResNet architecture has served as a default method for computer vision machine learning due \cite{he2016deep}. It is important for this study to limit the complexity of the network as a further check on the validity of the data in the modal heatmaps. For this reason, an 18 layer ResNet architecture was selected for the subject classification model. That is, the network includes 18 linear transforms with a non-linearity in between each. For reference, default modern configurations of this architecture range from 18 to 152 layers. With 1024 heatmaps in the training set, and the remaining 256 reserved for validation, the network trains in fewer than 10 minutes total. The classification accuracy on unseen data is 98\% for the OMA modes, and 100\% for the DMD modes. The relevant confusion matrices are included in Figures \ref{fig:OMA} and \ref{fig:DMD}.
\begin{figure}[h!]
    \centering
    \begin{minipage}{0.45\textwidth}
        \centering
        \includegraphics[scale=0.32]{../../../figures/conf_mat_OMA.png} % first figure itself
        \caption{Conf. Matrix for OMA Modes}
        \label{fig:OMA}
    \end{minipage}\hfill
    \begin{minipage}{0.45\textwidth}
        \centering
        \includegraphics[scale=0.32]{../../../figures/conf_mat_DMD.png} % second figure itself
        \caption{Conf. Matrix for DMD Modes}
        \label{fig:DMD}
    \end{minipage}
\end{figure}

\subsection{Discussion}
These classification results are promising. On the Bayesian nature of neural networks alone, validation results approaching 100\% are very encouraging, especially with such a simple hypothesis class. The results also confirm existing knowledge that EEG data is interpersonally dependent \cite{makeig2012evolving}. This gives some initial credence to our system identification and modal approach to cognition. As a final check, we may ask how the network would perform if the labels were shuffled. The best we should expect this network to perform is 3.125\% accuracy ($1/32$), which is the best case scenario for a totally random guess, which represents the best case hypothesis for random subject labels. If, however, the network performs better, say any higher than 8\%, it may indicate that random noise can be permuted to match the labels we provided, even if they are not random.
\begin{wrapfigure}{r}{0.6\textwidth}
\centering
\includegraphics[scale=0.45]{../../../figures/conf_mat_random.png}
\caption{Conf. Mat for Random Labels}
\label{fig:random}
\end{wrapfigure}
Fortunately, in this case, the random label network shows approximately 3\% accuracy on unseen data. The relevant confusion matrix is included. This further increases our confidence on the modal decomposition. There is relevant information in the modes, and it can be used to identify one subject from another.

\begin{figure}[b]
    \centering
    \begin{minipage}{0.45\textwidth}
        \centering
        \includegraphics[scale=0.32]{../../../figures/heatmap_162_OMA.png} % first figure itself
        \caption{Focal Map for OMA Modes}
        \label{fig:OMA_heat}
    \end{minipage}\hfill
    \begin{minipage}{0.45\textwidth}
        \centering
        \includegraphics[scale=0.32]{../../../figures/heatmap_4_DMD.png} % second figure itself
        \caption{Focal Map for DMD Modes}
        \label{fig:DMD_heat}
    \end{minipage}
\end{figure}
From here, interpretation of the network results becomes relevant. Is the network relying on specific modes for classification? Is the real component more important than the complex component? Are the faster modes more important than the slow ones? Since neural networks are a gradient method, they assign a weight to each pixel in the image. We can track these weights and overlay them on the original image. Hot spots indicate special attention was paid by the network to make a classification, while cold spots indicate the opposite. Interestingly, the OMA and DMD representations drive the network to select different features for classification, while both show excellent results. The OMA network greatly relies on very few real components of all the modes. This corresponds to a single sensor location on the EEG device, suggesting interpersonal differences are highly constrained to spatial location. On the other hand, the DMD network seems to rely on specific modes, such as mode 9 in Figure \ref{fig:DMD_heat}. The additional noise surrounding the DMD network results in this figure may be a result of the data driven nature of the algorithm. In both cases, notice that neither network has any interest in the lower third of the image, which corresponds to the imaginary component of the modes.

\begin{wrapfigure}{r}{0.\textwidth}
\centering
\includegraphics[scale=0.55]{../../../figures/channels.png}
\caption{No. of Channels vs. Classification Accuracy}
\label{fig:channels}
\end{wrapfigure}
Since the results seem highly dependent on just a few features, we may further ask if spatial sampling may be decreased. How few channels are necessary for accuracy classification results? Figure \ref{fig:channels} indicates that the minimum number of channels is somewhere between 5 and 10 for subject fingerprinting.

From these results, we now must think about connecting the modes to relevant emergent cognitive properties such as engagement. Perhaps we may remove the features that are used to identify subjects, resulting in a broadly applicable model of engagement. This portion of the work is ongoing, but encouraged by the success of the fingerprinting networks.

%\begin{displayquote}
%The class of monotone DNF expressions is learnable via an algorithm $B$ that uses $L=L(h,d)$ calls of examples and $dt$ calls of oracle, where $d$ is the degree of the DNF expression $f$ to be learnt and $t$ the number of variables.
%\end{displayquote}

%\begin{wrapfigure}{r}{0.55\textwidth}
%\centering
%\includegraphics[scale=.5]{../figures/complexity.png}
%\caption{The Error-Complexity Trade Off}
%\end{wrapfigure}


%        File: stochastic_methods.tex
%     Created: Mon Jun 08 03:00 PM 2009 C
% Last Change: Mon Jun 08 03:00 PM 2009 C
%

\documentclass[letterpaper,10pt]{article}
\usepackage[pdftex]{color,graphicx}
\usepackage{amsmath, amsfonts, amssymb, latexsym, inputenc, moreverb, wrapfig, subfigure, array, lscape, setspace, cite}
\usepackage[pdftex, colorlinks]{hyperref}
\newcommand{\ud}{\mathrm{d}}
\newcommand{\degree}{$^{\circ}$}
\newcommand{\tb}[1]{\textcolor{blue}{#1}}
\newcommand{\E}{\text{E}}

%\usepackage{fullpage} %% 1 inch margins by default
%\usepackage{pslatex}  %% use normal postscript fonts (like times new roman)

\title{Stochastic Methods for Ecology and Evolution}
\author{Carl Boettiger}
\begin{document}
\maketitle

% It begins.  Here I am at IIASA, beginning of the second week, but almost the first chance I have to settle down and do some real work -- 8 June, 2009.  This is meant to be a collection of methods to calculate certain quantities from stochastic processes, the applications of which form the bulk of the problem I will address this summer.

% The organization of these ideas has yet to be developed -- whether by model, mathematical method, or biological problem.

\section{Extinction Probabilities}

Gardiner (1985).

We consider single-step birth death models for the majority of this work.  Though they will become multidimensional in later parts of the work, as we deal with multiple species or explicit environmental changes, for the time being we will focus on the one-dimensional bith-death process.  The Markov process is defined over positive integers, $n \in \mathbb{Z}^+$, and will sometimes be assumed to be bounded above by some integer $N$.  The process is determined by two state-dependent rates, $b(n)$ and $d(n)$, as depicted in Figure~\ref{fig:markov}.

\begin{figure}[h]
\begin{center}
\includegraphics[width=.4\textwidth]{images/markov}
\end{center}
\caption{The birth-death Markov process}
\label{fig:markov}
\end{figure}

The process is specified by the Master equation:

\begin{equation}
\frac{\partial P(n,t)}{\partial t} = b(n-1)P(n-1,t)+d(n+1)P(n+1) - \left( b(n) + d(n)  \right) P(n)
\label{mastereq}
\end{equation}

and approximated by the method of Kurtz (1971) or van Kampen's linear nois expansion as:

\begin{equation}
\frac{\partial P(\tilde n,t)}{\partial t} = - \underbrace{\frac{\partial \left( b(\tilde n)-d(\tilde n) \right)}{\partial \tilde n}}_{A(\tilde n) } \frac{\partial}{\partial \tilde n} \tilde n  P(\tilde n,t)+ \underbrace{\frac{b(\tilde n)+ d(\tilde n)}{2} }_{B(\tilde n)/2} \frac{\partial^2}{\partial \tilde n^2} P(\tilde n)
\label{forward}
\end{equation}

Note that $n\to \tilde n$ where $\tilde n \in \mathbb{R}$ has become a continuous value in the limit of large system size.  The coefficients $A(x)$ and $B(x)$ are the jump moments $\partial_x \alpha_{1,0}(x)$ and $\alpha_{2,0}(x)$ in the van Kampen expansion.  This expression is known as the forward Kolmogrov equation.  Its solution is a Guassian, with mean and variance given respectively by

\begin{align}
\frac{\ud x }{\ud t} &= b(x)-d(x) \nonumber \\
\frac{\ud \sigma^2}{\ud t} &= A(x) \sigma^2 + B(x)
\label{moments}
\end{align}

Related to~\eqref{forward} is the backward Kolmogrov equation which we use for first-passage time problems:

\begin{equation}
\frac{\partial P(\tilde n,t)}{\partial t} = A(\tilde n) \frac{\partial}{\partial \tilde n}  P(\tilde n,t)+ \tfrac{1}{2}  B(\tilde n)\frac{\partial^2}{\partial \tilde n^2} P(\tilde n)
\label{backward}
\end{equation}

From this we have the first passage time starting at $x$ across an absorbing boundary $a < b$ given $b$ is reflecting:

\begin{equation}
T(x) = 2 \int^x_a \frac{\ud y}{\psi(y)} \int_y^b \frac{\psi(z) }{B(z)} \ud z
\label{firstpassage}
\end{equation}
where we define the integrating factor
\begin{equation}
\exp\left( \int_a^y \ud x \left[ \frac{2 A(x)}{B(x)} \right] \right)
\label{integrating factor}
\end{equation}

It is useful to work these results out for some familiar models.
\subsubsection*{Levins Model}
We first consider the Levins model:
\begin{align}
b(n) &= c n \left( 1 - n/K \right) \nonumber \\
d(n) &= e n
\label{levins}
\end{align}

$K$ is the number of patches, and also the convenient measure of system size.  In the limit of large $K$, $\tilde n = \tfrac{n}{K}$ represents the fraction of occupied patches, and we have the coeffients:
\begin{align*}
& A(\tilde n) = c(1-2 \tilde n)-e \\
& B(\tilde n) = c \tilde n (1 - \tilde n) + e \tilde n
\end{align*}

By way of~\eqref{moments} we have the equilibrium mean and variance:
\begin{align*}
& \langle \tilde n \rangle = 1-\frac{e}{c} \\
& \langle (\tilde n - \langle \tilde n \rangle )^2 \rangle = \frac{e}{c}
\end{align*}

\subsubsection*{Logistic Model}
A closely related model is the logistic with the following interpretation:
\begin{align*}
& b(n) = r n \\
& d(n) = \frac{r n^2}{K}
\end{align*}
Which has mean and variance both equal to $K$, and coefficents:
\begin{align*}
& A(x) = r  - \frac{2 r x}{K} \\
& B(x) = rx + \frac{rx^2}{K}
\end{align*}

\end{document}


%%!TEX TS-program = latex
\documentclass[11pt]{article} %DIF >
\usepackage{etex}
\usepackage[utf8]{inputenc}
\input ../AuxFiles/PreambleNotes.tex

\begin{document}
\onehalfspace

\vspace*{\fill}
\begingroup
\centering

\Large {\scshape Introduction to Probability}\\

(Lectures 1-2)

\endgroup
\vspace*{\fill}

\newpage

\section{Probability Spaces and Random Variables}
The goal of Lecture 1 and Lecture 2 is twofold. First, we will present the formal definitions of a \emph{probability space} and a \emph{random variable}. Then, we will introduce some more concrete concepts that will be used throughout the course (such as  \emph{cumulative distribution function} and \emph{probability density function}).

{\scshape Overview of Lectures 1-2:} The triplet:

$$(\Omega, \mathcal{F}, \mathbb{P}),$$

\noindent will be our notation to describe a probability space. We will call

\begin{enumerate}
\item $\Omega$: The underlying space of uncertainty or set of states of the world.

\item $\mathcal{F}$: The ``$\sigma$-algebra'' of subsets of $\Omega$. We will think about it is as a collection of ``events'' whose likelihood of occurrence we would like to analyze.

\item $\mathbb{P}$: The probability measure. We will think about it as a $[0,1]$-valued function summarizing the likelihood of an event.

\end{enumerate}

A real-valued random variable  will be denoted by the map

$$X: \Omega \rightarrow \R,$$

\noindent which will satisfy a restriction that we will call ``measurability''. We will focus on how the probability measure on $(\Omega, \mathcal{F})$ induces a probability over subsets of $\R$.

Once we are done with abstract definitions, we will introduce objects that we will use quite often during this course. In the context of a real-valued random variable we will define

\begin{enumerate}[a)]
\item The cumulative distribution function (c.d.f.): which we will connect to the ``induced’’ probability measure of a random variable,
\item ``Discrete'' and ``Absolutely Continuous'' random variables: this classification will emerge from differences in the c.d.f. of random variables.

\item The probability density function (p.d.f.): a convenient way of summarizing the information in the c.d.f. of an absolutely continuous random variable,

\item The expectation operator $\expec_{\mathbb{P}}[\cdot]$ (and then the variance of a real-valued random variable).

\item Finally, the moment generating function.

\end{enumerate}

\subsection{Measurable spaces, Probability Measures, and Probability Spaces}

%---------------------------------Section 1:Probability Space and Independence-------------------------------

\subsubsection{$\sigma$-algebras and Measurable Spaces}
We will start with the definition of a ``measurable space''. In order to model randomness, we need to have some structure that allows us to specify what the randomness is all about. \\

Let $\Omega$ be any given nonempty set and let $\mathcal{F}$ be a nonempty collection of subsets satisfying the following properties:
\begin{enumerate}
	\item $\mathcal{F}$ is ``closed'' under complements: $F \in \mathcal{F} \implies \Omega \backslash F \in \mathcal{F}$
	\item $\mathcal{F}$ is ``closed'' under countable unions: $F_{n} \in \mathcal{F}$ for all $n \in \mathbb{N} \implies \cup_{n=1}^{\infty} F_n \in \mathcal{F}$.
\end{enumerate}

\begin{definition}[\textbf{$\sigma$-algebra}] (\cite{Billingsley95}, p. 19 and 20)
	If the nonempty collection $\mathcal{F} \subseteq 2^{\Omega}$ satisfies properties 1,2 above then $\mathcal{F}$ is called a $\sigma$-algebra (or a $\sigma$-algebra of subsets of $\Omega$)
\end{definition}

\begin{definition}[\textbf{Measurable Space}] (\cite{Billingsley95}, p. 161)
	The pair $(\Omega, \mathcal{F})$, where $\mathcal{F}$ is a $\sigma$-algebra of subsets of $\Omega$ is called a measurable space. The subsets of $\mathcal{F}$ are called the events of $\Omega$.
\end{definition}

\noindent If you are interested in playing with these definitions, here is a simple exercise. \\

\begin{prproblem}[Optional]
Let $A$ be a collection of elements of $2^\Omega$. Define
$$F^*(A) \equiv \{\mathcal{F} \: | \: \mathcal{F} \: \text{is a} \: \sigma\text{-algebra of } \Omega \: \text{containing} \: A\} $$

\begin{enumerate}[i)]
\item Show that $F^*(A)$ is non-empty.
\item Let $\sigma(A)$ denote the intersection over all the $\sigma$-algebras contained in $F^*(A)$. Show that $\sigma(A)$ is a $\sigma$-algebra. The collection $\sigma(A)$ will be the called the ``$\sigma$-algebra generated by A''. Such collection is unique.
\end{enumerate}
\end{prproblem}

\begin{prproblem}[Optional] \label{prprob:closed_under_inter}
  Show that $\sigma$-algebra is ``closed'' under countable intersections. That
  is, if $F_n \in \mathcal{F}$ for all $n\in\mathbb{N}$ then $\cap_{n=1}^\infty
  F_n \in \mathcal{F}$.
\end{prproblem}

\subsubsection{Probability Measures}

Once we know which are the objects that uncertainty will refer to, we need to have a formal way of expressing the ``likelihood'' of the different events we have specified. In order to do this we will define a \emph{probability measure}.

\begin{definition} (\textbf{Probability Measure}) (\cite{Billingsley95}, p. 22) Let $(\Omega, \mathcal{F})$ be a measurable space. Let $\mathbb{P}: \mathcal{F} \rightarrow [0,1]$ be a ``set'' function mapping the $\sigma$-algebra of subsets of $\Omega$ into the real line. We say that $\mathbb{P}$ is a probability measure if it satisfies the following properties:
\begin{enumerate}
	\item $\mathbb{P}(\emptyset)=0$, $\mathbb{P}(\Omega)=1$ [Normalization]
	\item $\mathbb{P}(\bigcup_{n=1}^{\infty} A_n)= \sum_{n=1}^{\infty} \mathbb{P}(A_n)$ for a countable disjoint sequence of elements of $\mathcal{F}$ [$\sigma$-additivity]\\
\end{enumerate}
\end{definition}

\begin{definition}[\textbf{Probability space}] (\cite{Billingsley95}, p. 23)
The triplet $(\Omega, \mathcal{F}, P)$ is called a probability space.
\end{definition}

Some implications of the definition of a probability measure.

\begin{enumerate}

\item \textbf{Monotonicity:} For events $A,B$, $A \subseteq B$ implies $\mathbb{P}(A) \leq \mathbb{P}(B)$.

\begin{proof}
Write $B= A \cup (B \backslash A)$. By assumption, $A\in \mathcal{F}$; Practice Problem \ref{prprob:closed_under_inter} implies $B\setminus
A \in \mathcal{F}$ because $B\setminus A = B
\cap \left( \Omega\setminus A \right)$. By definition $A \cap (B\backslash
A)=\emptyset$.  Therefore, by finite additivity (which is implied by $\sigma$-additivity) we have $\prob(B)=\prob(A) + \prob(B \backslash A)$, which implies $\prob(B)-\prob(A)= \prob(B \backslash A) \geq 0$, as $\prob$ is a $[0,1]$-valued set function.
\end{proof}

\item \textbf{Boole's Inequality:} (\cite{Billingsley95} p. 24) Boole's inequality is a pretty useful result that will show up quite often.  Let $A_n$ be any sequence of events in $\mathcal{F}$. Then
$$\prob(\cup_{n=1}^{\infty} A_n) \leq \sum_{i=1}^{\infty} \prob(A_n)  $$

We provide a proof of Boole's inequality for only two events $A,B$. This is, we show that:

$$\prob(A \cup B) \leq \prob(A) + \prob(B) $$

\begin{proof}
Let $A_1=A$, $A_2=B \backslash A$. By finite additivity:
\begin{eqnarray*}
\prob(A \cup B) &=& \prob(A_1 \cup A_2)\\
&=& P(A_1) + P(A_2)
\end{eqnarray*}
Since $B \backslash A \subseteq B$, then $\prob(B\backslash A) \leq P(B)$. Therefore, $\prob(A \cup B) \leq P(A) + P(B)$.
\end{proof}

\end{enumerate}

In order for you to manipulate the definition of a probability measure, you will be asked to write down proofs of the following statements:\\

\noindent\begin{prproblem}[Properties of a probability measure] Let $(\Omega, \mathcal{F}, \prob)$ be a probability space. Show that a probability measure satisfies:
\begin{enumerate}
\item $\prob(F_1 \cup F_2) = \prob(F_1) + \prob(F_2) - \prob(F_1 \cap F_2)$ \text{for any } $F_1,F_2 \in \mathcal{F}$
\item $\prob(\cup_{n \in \mathbb{N}} F_n) \leq \sum_{n \in \mathbb{N}} \prob(F_n) $ for any countable collection $\{F_n\}$
\end{enumerate}
\end{prproblem}

%\noindent \textbf{Comment:} [Sierpinski Class Lemma or Dynkin's $\pi$-$\lambda$ theorem]: Note that we have defined a probability measure as a set-valued function mapping elements in the $\sigma$-algebra $\mathcal{F}$ into the $[0,1]$ interval. Often times, it will be desirable to characterize the probability measure by the values that it takes in a smaller subclass of sets. One interesting result, which follows Sierpinski’s Class Lemma (pg. 34, Chapter B, Efe's book in Probability Theory), is the following:
%
%\begin{result}
%Let $\mathcal{A}$ be a collection of sets satisfying:
%$$a,a' \in \mathcal{A} \implies a \cap a' \in \mathcal{A} $$
%Suppose that $\prob_1$, $\prob_2$ are two probability measures with domain $\sigma(\mathcal{A})$ such that:
%$$\prob_1(a) = \prob_2(a) \quad \forall \: a\in \mathcal{A}.$$
%\noindent Then $\prob_1(a)=\prob_2(a)$ for every $a \in \sigma(\mathcal{A}).$
%\end{result}

\subsection{Random Variables}

Along with a probability measure, a central concept in probability and statistics is that of a \emph{random variable}.

\begin{definition}[S-valued random variable]
Let $(\Omega, \mathcal{F})$, $(S, \mathcal{S})$ be two measurable spaces. The map:
$$X: \Omega \rightarrow S$$
\noindent is called an S-valued random variable [relative to $(\Omega, \mathcal{F})$-$(S, \mathcal{S})$] if for all $A \in \mathcal{S}$  :
$$X^{-1}(A) \equiv \{\omega \in \Omega \: | \: X(\omega) \in A\} \in \mathcal{F},$$
\noindent this is, if $X^{-1}(A)$ is \emph{measurable} for all $A \in \mathcal{S}$.
\end{definition}

Note that, by definition, a random variable takes events in the space $S$ to well-defined events in the space $\Omega$. If there is already a well-defined probability measure $\prob$ on $(\Omega, \mathcal{F})$, then there is a natural ``induced’’ probability measure in $(S, \mathcal{S})$:

\begin{definition}[``Induced’’ Probability Measure or ``Law’’ of a Random Variable] (see \cite{Billingsley95} p. 185 on Transformation of measures and see if you can make the connection) Let $(\Omega, \mathcal{F}, \prob)$ be a probability space. The probability measure over $(S, \mathcal{S})$ induced by the random variable $X: \Omega \rightarrow S$ is given by:

$$P_{X}(A) \equiv \prob(X^{-1}(A)) \quad \forall \: A \in \mathcal{S}.$$

\end{definition}

\subsubsection{Cumulative Distribution Function for an $\R$-valued random variable}

In this section, we will analyze the statement: ``X is a real-valued random variable with a \emph{cumulative distribution function} $F:\R \rightarrow [0,1]$''. We will explain the relation between the c.d.f. of a random variable and the induced probability measure we discussed in the last subsection. We will focus on real-valued random variables (relative to the Borel $\sigma$-algebra on the real line, which is the smallest $\sigma$-algebra containing all open sets).

\begin{definition} (\cite{Billingsley95}, p. 188 and 256)
The cumulative distribution function (c.d.f.) of a real-valued random variable $X: \Omega \rightarrow \R$ is defined as the real-valued function $F:\R\rightarrow[0,1]$ given by
$$F_{X}(x) \equiv \prob\{ \omega \in \Omega \: | \: X(\omega) \leq x\} = \prob\{X^{-1}(-\infty,x]\}    $$
\end{definition}

The c.d.f. of a random variable $X$ is nothing else than its induced probability measure evaluated at sets of the form $(-\infty, x]$. The c.d.f. satisfies the following properties:

\begin{proposition} If $F_{X}$ is the c.d.f. of a random variable $X: \Omega \rightarrow \R$ then
\begin{enumerate}
\item $F_{X}$ is non-decreasing.
\item $\lim_{x \uparrow \infty} F_{X}(x)=1$
\item $\lim_{x \downarrow -\infty} F_{X}(x)=0$
\item $\lim_{h \rightarrow 0^{+}} F(x+h)=F(x)$
\end{enumerate}

\noindent Furthermore, if $F$ is a function satisfying 1,2,3,4, then there is a probability space $(\Omega, \mathcal{F}, \prob)$ and a random variable $X: \Omega \rightarrow \R$ such that $F$ coincides with $F_{X}$.

\end{proposition}

\noindent \begin{prproblem}
   This week's problem set will walk you through the proof of Proposition 1.\\
\end{prproblem}

Here are some commonly used examples of c.d.f.s of real-valued random variables:

\begin{itemize}
\item \emph{Uniform Distribution}: The random variable $X$ is said to have a uniform distribution in $[a,b]$ if the c.d.f. is given by:
\begin{equation*}
F(x) =
\left\{
\begin{array}{ccc}
0  & \text{if}  & x < a   \\
x-a/[b-a]  & \text{if}  & x \in [a,b)   \\
1 & \text{if}  & x \geq b
\end{array}
\right.
\end{equation*}

\item \emph{Bernoulli Distribution}: The random variable $X$ is said to have a Bernoulli distribution with parameter $p \in [0,1]$ if its c.d.f. is given by:

\begin{equation*}
F(x) =
\left\{
\begin{array}{ccc}
0  & \text{if}  & x < 0   \\
1-p  & \text{if}  & x \in [0,1)   \\
1 & \text{if}  & x \geq 1
\end{array}
\right.
\end{equation*}

\item \emph{Geometric Distribution}: The random variable $X$ is said to have a Geometric Distribution with parameter $p$ if its c.d.f. is given by:

\begin{equation*}
F(x) =
\left\{
\begin{array}{ccc}
0  & \text{if}  & x < 1   \\
1-(1-p)^{k}  & \text{if}  & x \in [k,k+1) \text{ for } k \in \mathbb{N} \\
\end{array}
\right.
\end{equation*}

\item \emph{Exponential Distribution}: The random variable $X$ is said to have an exponential distribution with parameter $\lambda$>0 if

\begin{equation*}
F(x) =
\left\{
\begin{array}{ccc}
0  & \text{if}  & x < 0   \\
1-e^{-\lambda x}  & \text{if}  & x \geq 0   \\
\end{array}
\right.
\end{equation*}

\item \emph{Gaussian Distribution:} The random variable $X$ is said to have a Gaussian distribution with parameters $(\mu, \sigma^2)$ if

$$F(x) = \int_{-\infty}^{x} \frac{1}{\sqrt{2 \pi \sigma^2} } \exp\Big(-\frac{1}{2 \sigma^2} (u-\mu)^2 \Big) du $$

\item \emph{Pareto Distribution:} The random variable $X$ is said to have a Pareto distribution with parameters $(x_m, \alpha)$ if

\begin{equation*}
F(x) =
\left\{
\begin{array}{ccc}
0  & \text{if}  & x < x_m   \\
1-\Big(\frac{x_m}{x} \Big)^{\alpha} & \text{if}  & x \geq x_m   \\
\end{array}
\right.
\end{equation*}

\end{itemize}

One common way to classify the random variables and their c.d.f.s is the following:

\begin{definition}[Discrete and Absolutely Continuous Random Variables]  \:\
\begin{enumerate}
\item \textbf{Discrete Random Variables:} A random variable $X$ is discrete if there is a countable set $\{x_1, x_2, \ldots\}$, $x_1<x_2<\ldots$ such that
$$\prob_{X}(\{x_1, x_2\ldots \}^c)=0$$
The smallest set $\{x_1, x_2, \ldots\}$ for which $\prob_{X}(\{x_1, x_2\ldots \}^c)=0$ is called the support of $X$. The map $p: \text{Supp} \rightarrow [0,1]$ defined by $p(s)\equiv\prob_{X}(s)$ is called the probability mass function.
\item \textbf{Absolutely Continuous Random Variables (w.r.t. Lebesgue Measure):} A random variable $X$ is absolutely continuous if:
$$F(x) = \int_{-\infty}^{x} f(y)dy $$
for some integrable, nonnegative function $f$ that we will call the \textbf{probability density function} (p.d.f.) of $X$. If $F$ is continuously differentiable the p.d.f. will coincide with the derivative of $F$ (see p. 257 in \cite{Billingsley95}).
\end{enumerate}

\end{definition}

A Bernoulli r.v. is an example of a discrete distribution with finite support. A Geometric r.v. is an example of a discrete random variable with countable support. The gaussian, pareto, and exponential distribution are both continuous and absolutely continuous random variables. Note that not every random variable falls into one of the categories above (see, for example, the entry for Cantor's function in wikipedia) .

\subsubsection{Moments of Discrete and Absolutely Continuous Random Variables}
In this section we introduce the definition of expectation for only discrete and absolutely continuous random variables. For a more detailed exposition you can see Chapters 1.4-1.6 in \cite{durrett2010}.

\begin{definition}[Expectation of the transformation of an absolutely continuous random variable] Let $X$ be an absolutely continuous real-valued random variable with c.d.f $F$ and p.d.f. $f(x)$. Let $g:X \rightarrow \R$ be a real-valued function. The expectation of $g(X)$ under $F$ is defined as:

$$\expec_{F}[g(X)] \equiv \int_{\R} g(x)f(x)dx  $$

\end{definition}

 \begin{definition}[Expectation of the transformation of a discrete random variable] Let $\{x_1, x_2, \ldots\}$ be the support of the discrete random variable $X$. Let $g:X \rightarrow \R$ be a real-valued function. The expectation of $g(X)$ under the probability mass function $p$ is given by:
 $$\expec_{F}[g(X)] \equiv \sum_{x_i \in \text{Supp}} g(x_i) p(x_i).   $$
 \noindent
 \end{definition}

 \noindent Now, some important observations:

 \begin{enumerate}
 \item \textbf{Mean of a random variable:} $E_{F}[X] \equiv \mu$ is called the \emph{mean} or \emph{first moment} of $X$. If $E_{f}[|X|]< \infty$ then $X$ is said to be integrable.
 \item \textbf{Variance of a random variable:} $E_{F}[ (X-\mu)^2 ]\equiv \sigma^2$ is called the \emph{variance} or \emph{second centered moment} of $X$. The mean and the variance of random variables will play an important role in Weak Laws of Large numbers and Central Limit Theorems. For certain random variables the variance need not be finite (the pareto distribution with parameter $\alpha=2$ is an example of this fact)
 \item \textbf{Jensen's Inequality:} For any convex function $g$: $\expec_{F}[g(X)] \geq g(\expec[X])$. This is an important result. We will not prove it, but we will be using it quite often.
 \item \textbf{Markov's Inequality:} For any random variable $X$ and $c>0$, $c\prob_{X}[|X|>c] \leq \expec_{F}[|X|]$
 \item \textbf{k-th Moment:} $E_{F}[X^{k}]$ is called the $k$-th uncentered moment of the random variable $X$, $k=1,2, \ldots$. Note that Jensen's inequality implies that if $E_{F}[|X|^{k}]<\infty$ for some $k \in \mathbb{N}$ then $E_{F}[|X|^{j}]<\infty$ for all $j<k$. Likewise, if $E_{F}[|X|^{k}]=\infty$, then $E_{F}[|X|^{j}]=\infty$ for all $j>k$.

  \item \textbf{Layer Representation of k-th moments:} if $X$ is an absolutely continuous random variable and $\expec[|X|^{k}]<\infty$ then $\expec[|X|^{k}]=k\int_{0}^{\infty}c^{k-1}\prob_{X}(|x|>c)dc$.

  \item \textbf{Moment Generating Function:} The random variable $X$ is said to have a moment generating function $m_{X}(t)$ if
 $$m_{X}(t) \equiv \expec_{F}[\exp(tX)]<\infty \quad \text{for all } t \in (-\epsilon, \epsilon)$$
 \noindent for some $\epsilon>0$. {\scshape Important}: Two random variables with the same moment generating function have the same distribution.
\end{enumerate}

\noindent The last question of this week's problem set will ask you to go over some of these concepts.

\newpage

\noindent \textbf{Quick Summary of Lectures 1 and 2:} We have provided formal definitions of a probability space and a real-valued random variable.

We defined discrete and absolutely continuous random variables. We used our classification to provide a definition of the expectation of a function $g(X)$. We defined the mean of a real-valued random variable ($\mu$), the variance of a real-valued random variable ($\sigma^2$), the $k$-th moment of a real-valued random variable, and at the very end of Lecture 2, the \emph{moment generating function}.

The moment generating function contains---in a sense that we did not make precise---the same information as the c.d.f. of a real-valued random variable. Here are some results to keep in mind.

\begin{itemize}
\item Two discrete random variables with the same moment generating function have the same support and the same probability mass function. See \cite{Billingsley95}, p.147, second paragraph.
\item Two positive random variables with the same moment generating function have the same distribution. See \cite{Billingsley95}, Theorem 22.2. Can you fill in the details of the proof?
\end{itemize}

\noindent {\scshape Optional:} Suppose that $F_{X}$ has a density $f$. Suppose that $m_{X}(t)=\expec[\exp(tX)]$ is well defined for every $t \in \R$. The question is: how do we recover $f$ from the moment generating function? An sketch of the answer is the following:

\begin{enumerate}
\item Extend the image of the m.g.f. to the \emph{complex} plane: Let $i=\sqrt{-1}$. For $t \in \R$, define:
$$\phi(t)\equiv m_{X}(it)=\expec[\exp(itX)] $$
\noindent The function $\phi(t):\R \rightarrow \mathbb{C}$ is called the \emph{characteristic function} (in fact, this is always well defined: even if the m.g.f. does not exist.).
\item Recover the p.d.f. from the characteristic function: It turns out that one can prove the following equality (\emph{or inversion formula}):
$$f(x) = \int_{-\infty}^{\infty} \exp(-it) \phi(t) dt $$
\end{enumerate}

You can found details in Section 26 (342-349) in \cite{Billingsley95} or Section 3.3 (106-111) in \cite{durrett2010}. The main take away (and we will use this result later) is that under certain conditions two random variables with the same moment generating function have the same distribution over the Borel $\sigma$-algebra in the real line. In this sense, the moment generating function provides a characterization of the c.d.f.

\newpage

\bibliographystyle{../AuxFiles/ecta}
\bibliography{../AuxFiles/BibMaster}

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


\chapter{Introduction}\label{ch:intro}
\setcounter{page}{1}
\pagenumbering{arabic}
\section{Abstract interpretation}
Static program analyses are a useful set of techniques to infer properties of programs directly from their source code, without executing them. Recently they became part of the software development, and are used in major companies like Facebook \cite{distefano-static-analysis-fb} and Google \cite{static-analysis-google} to catch bugs before they reach production.

Among those techniques, abstract interpretation \cite{cousot-77,cousot-79,cousot-92} (see chapter 4 of \cite{principles-of-program-analysis-book} for an introduction) is a general framework to define sound analyses based on constructive approximations that found its way through many aspects of modern computer science.
The idea of abstract interpretation is to execute a program keeping track only of some properties that are valid of the current program state, abstracting from the complexity of concrete values. This introduces approximation errors, but make the analysis feasible. Differently from logic, the properties considered are fixed beforehand and define the \textit{abstract domain} of the analysis, making it possible to define ad hoc abstract interpreters for such domains.

\begin{example}[Intervals]\label{intr:ex:intervals}
	A classic example of abstract domain is that of intervals: instead of keeping track of precise values each variable can take, the analysis approximates them with an interval to which they belong.

	The simple code fragment
	\begin{minted}{C}
int a[6];
int j;
for (int i = 0; i <= 5; ++i) {
	j = i * 2;
	a[j] += 1;
}
	\end{minted}
	could be analysed to determine that within the loop the variable \code{i} belongs to the interval $[0, 5]$. From this, after the assignment \code{j = i * 2} the analyser can derive that \code{j} belongs to $[0, 5] * 2 = [0, 10]$, and so that the array access to \code{a} may happen out of bounds.
	Note that the interval $[0, 5]$ to which \code{i} belongs is exact, that is the variable actually has all the possible values during the loop, while $[0, 10]$ for \code{j} is not: it will never be the case that \code{j} is $3$.
\end{example}

Abstract interpretation's generality allows it to be used within a broad variety of topics in computer science, for instance program transformations and security. We refer the reader to \cite{cousot-absint-survey} for a survey of applications.

\section{Under-approximations}
In the example above, the error of the analysis is of over-approximation: the result contains all the values that appear in the concrete execution, but also some more.
Nowadays, most static analyses focus on over-approximation in order to show absence of bugs: they compute a superset of all possible behaviours of a program, and if none of them exhibits a bug also all real behaviours, that are a subset, are correct. Unfortunatley, values added by the over-approximation can cause an alert that never happens in concrete executions: those are called false alarms. An analyser may produce lots of them, undermining the credibility of the tool.
However, in principle there is a dual approach to static analysis, that is given by under-approximation: compute a subset of all possible behaviours, and if any of them exhibits a bug than that is a real bug and not an artifact of the analysis.

Hoare logic \cite{hoare-logic} is perhaps the first example of formal static analysis, and indeed is an over-approximation one, that correctly fits its goal of proving the absence of errors. Maybe the influence of early works like this and the position of people like Dijkstra, summarized in his renowned quote ``\textit{Testing shows the presence, not the absence of bugs}", directed the focus toward over-approximation, and so very few studied formal under-approximation static analyses.
Recently, O'Hearn \cite{ohearn-incorrectness-logic} advocated for a change of trend: while it would be ideal to have only provably correct programs, this clashes with both theoretical and practical issues. Program analysis is often undecidable, and in any case is computationally expensive and requires formal specifications, imposing an heavy burden on programmers, hence the need (also) for bug catching. In his work he proposes ``incorrectness logic", a dual version of Hoare logic thought from the ground up for under-approximation in order to find bugs, and propose to do the same for other over-approximation techniques.
More recently \cite{bruni-lcl}, it has been shown how the integration of over-approximation abstract intepretation with under-approximation techniques can improve the precision of the analysis.

\section{Under-approximation abstract interpretation}
In his paper, O'Hearn leaves as an open question whether abstract interpretation could ``\textit{eventually play a guiding and explanatory role for a wide range of static and dynamic under-approximate tools for bug catching, similar to what it already does for over-approximate analyses}".
In the works that first introduced abstract interpretation \cite{cousot-77}, the authors themselves considered the possibility of using it for both over and under-approximation. However, there have been only sparse studies on the latter.
Bourdoncle \cite{bourdoncle-abs-debugging} proposed abstract debugging using over-approximation domains, but acknowledged that under-approximation ones are better suited.
Lev-Ami et al. \cite{lev-backward-analysis-complement} propose to use complements of over-approximation domains to infer sufficient precondition.
For the same goal, Miné \cite{mine-backward-underapprox-14} uses directly over-approximation domains, giving up the best abstraction and handling the choice of a minimal one with heuristics.
Schmidt \cite{schmidt-higher-order-approx-2007} ``existentially quantifies" abstract properties, giving rise to an over-approximation of under-approximations in the context of transition systems.

All these works design under-approximation domains starting from over-approximation ones. To the extent of our knowledge there are no abstract domains thought from the ground up for under-approximation, and we believe this to be caused by intrinsic difficulties in designing them. In this thesis, we try to determine and explain some of the reasons behind this difficulties, from both a technical and a conceptual point of view, and thus why under-approximation hasn't been studied as extensively as over-approximation in the framework of abstract interpretation.

\section{Overview and main results}
Chapter \ref{ch2:background} lays out the background and the notation used in the thesis.
In Chapter \ref{ch3:comparison} we present a comparison of over and under-approximation, investigating similarities and differences between the two. In particular we're interested in the role of basic transfer functions, the semantics of elementary constructs of the language, that in our opinion are the main asymmetry between over and under-approximation.
In Chapter \ref{ch4:integers} we apply ideas from the previous chapter to integer domains to show that, under some conditions, no under-approximation domain can exist. To do this, we introduces the new definition of \emph{non emptying function}, that describes the fact that such function doesn't tamper analysis, and show that no abstract domain makes all sums non emptying.
In Chapter \ref{ch5:arbitrary-domains} we try to generalize the result we obtained for integers to arbitrary concrete domains, obtaining conditions on the family of functions under which no abstract domain exists that makes them all non emptying. Then we study under which conditions there do exist abstract domains that make all functions in a certain family non emptying, effectively restricting applicability of approaches that uses this definition to show non existence of abstract domains.
Lastly, Chapter \ref{ch:conclusions} draws conclusions and propose some future research directions.