\subsection{Developing Applications with Objective Caml}

\begin{enumerate}
  \item caveat
    \begin{enumerate}
    \item + (modulo the boundary, \emph{will not be checked})
    \item $1.0/0.0 \rightarrow \infty $
    \item $+. -. *. /. **$  mod ceil floor sqrt exp log log10 cos sin tan acos asin atan
    \item $asin 3.14  \rightarrow nan    $
    \item \verb|char_of_int 255| $\rightarrow$ \verb|'\255'| (can not display)
    \item \verb|char_of_int int_of_char string_of_int int_of_string|
      \verb|string_of_int 2551 -> ``2551''|
    \item string (length $\le 2^{24} - 6$ )
    \item == (\textit{physical equal}) (=, != <>)

\begin{alternate}
true == true;;
- : bool = true
# 3 == 3;;
- : bool = true
# 1. == 1.;;
- : bool = false
\end{alternate}

    \item int * int * int is different from (int * int ) * int
    \item unreasonable parametric equality \verb|(=) : 'a -> 'a -> bool|
    \item recursive declaration

\begin{ocamlcode}
let rec ones = 1 :: ones;;
\end{ocamlcode}

\begin{ocamlcode}
val ones : int list =
  [1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   ...]
\end{ocamlcode}

\begin{ocamlcode}
 let special_size l =
    let rec size_aux prev = function
      |[] -> 0
      |_ :: l1  -> if List.memq l1 prev then 1 else 1 + size_aux (l1::prev) l1 in size_aux [l]  l;;
    \end{ocamlcode}
\begin{ocamlcode}
  val special_size : 'a list -> int = <fun>
\end{ocamlcode}

\begin{alternate}
# special_size ones;;
- : int = 1
# let rec twos = 1 :: 2 :: twos in special_size twos;;
- : int = 2
# special_size [];;
- : int = 0
\end{alternate}
\item combine patterns \\
  p1 | .. |  pn (all name is forbidden within these patterns)
 'a' .. 'e'

 \begin{alternate}
let test 'a' .. 'e' = true;;
^^^^^^^^^^^^^^^^^
 \end{alternate}

\begin{ocamlcode}
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
'f'
val test : char -> bool = <fun>
\end{ocamlcode}

    \item records

\begin{alternate}
type complex = {re:float;img:float};;
type complex = { re : float; img : float; }
# let add {re; img} {re; img} = 3;;
val add : complex -> complex -> int = <fun>
# let add {re; img} {re; img} = {re = re +. re; img = img +. img};;
val add : complex -> complex -> complex = <fun>
 \end{alternate}

    \item \emph{redefinition marsks the previous one, while values of the masked types
        still exist, but it now turns to be an abstract type }
    \item exception
      \begin{enumerate}
      \item \verb|Match_failure Division_by_zero Failure|
      \item exception Name of t -- monomorphic , extensible sum Type \\
        when pattern match your exception, its type should be fixed
      \item control flow
      \end{enumerate}
    \item {\bf disagree over interface} \\
      when toplevel loads the same module (only the name is the same),
      it will check the interface is equal, this sucks since ocaml has
      flat namespace for module
    \end{enumerate}
  \item sharing \\
    for structured values, it will be sharing , however,
    \emph{vectors of floats don't share}

\begin{alternate}
let a = Array.create 3 0.;;
val a : float array = [|0.; 0.; 0.|]
# a.(0)==a.(1);;
- : bool = false
\end{alternate}

  \item weak type variables \\

\begin{alternate}
  let b = ref []
  (* b should '_a list ref, since b is not pure, cannot be shared *)
  let a = []
  (* a : 'a list *)
  let a = None
  (* a : 'a option *)n
  let a = Array.create 3 None
  (* '_a option array *)
# type ('a,'b) t ={ch1 : 'a list; mutable ch2 : 'b list};;
type ('a, 'b) t = { ch1 : 'a list; mutable ch2 : 'b list; }
# let v = {ch1=[];ch2=[]};;
val v : ('a, '_b) t = {ch1 = []; ch2 = []}
\end{alternate}
 \textit{mutable sharing conflicts with polymorphism}

  \item library
    \begin{enumerate}
    \item List \\

\begin{ocamlcode}
      @ length hd tl nth rev append rev_append concat flatten
      iter map rev_map left_fold fold_right iter2 map2 rev_map2
      fold_left2 fold_right2 for_all exists for_all2 exists2
      mem memq find filter partition assoc assq remove_assoc remove_assq
      split combine sort statble_sort fast_sort merge
    \end{ocamlcode}

\begin{alternate}
# List.assq 3 [3,4;1,2];;
- : int = 4
# List.assq 3. [3.,4;1.,2];;
Exception: Not_found.
\end{alternate}

    \item Array \\
      \verb|Array.create_matrix creates Non-Rectangular matrices|

\begin{ocamlcode}
length get set make create init -- when you don't want to initialize
make_matrix (int->int->'a -> 'a array array) create_matrix;
append concat sub copy fill ('a array -> int -> int -> 'a -> int)
blit (Array.Labels.blit), to_list, of_list map iteri mapi fold_left
fold_right sort stable_sort fast_sort unsafe_get unsafe_set copy
\end{ocamlcode}

    \item IO \\

\begin{ocamlcode}
open_in open_out close_in close_out input_line
input : Batteries.Legacy.in_channel -> string -> int -> int -> int = <fun>
output: Batteries.Legacy.out_channel -> string -> int -> int -> unit =<fun>
read_line print_string print_newline print_endline
\end{ocamlcode}

    \item stack (imperative data structure actually)

\begin{ocamlcode}
exceptin Empty
create
type 'a t = { mutable c : 'a list }
(* mutable to delay initialization *)
push pop top clear copy is_empty length iter enum copy
of_enum print
module Exceptionless
  top : 'a t -> 'a option, pop
\end{ocamlcode}

    \item stream \textbf{imperative}

\begin{ocamlcode}
'a t
exception Failure
exception Error of string
from
of_list of_string of_channel iter empty peek junk count npeek
iapp icons ising lapp lcons lsing
sempty slazy dump npeek
\end{ocamlcode}

      syntax extension (for my experience, use it in shell, but not in
      tuareg toplevel)
\begin{ocamlcode}
  let concat_stream a b = [<a;b>]
\end{ocamlcode}
\begin{ocamlcode}
val concat_stream :
  'a Batteries.Stream.t -> 'a Batteries.Stream.t -> 'a Batteries.Stream.t =
\end{ocamlcode}
   expression not preceded by an \` considered to be sub-stream
   destructive pattern matching (camlp5 or extended parser can merge)
   consumed (error), failure
    \item Array List String Hashtbl Buffer Queue
    \item Sort

\begin{ocamlcode}
module X = Sort ;;
\end{ocamlcode}

\begin{ocamlcode}
module X :
  sig
    val list : ('a -> 'a -> bool) -> 'a list -> 'a list
    val array : ('a -> 'a -> bool) -> 'a array -> unit
    val merge : ('a -> 'a -> bool) -> 'a list -> 'a list -> 'a list
  end
\end{ocamlcode}

    \item Weak (vector of weak pointers) abstract type

\begin{ocamlcode}
sig
  type 'a t = 'a Weak.t
end
\end{ocamlcode}

    \item Printf

\begin{ocamlcode}
%t -> (output->unit)
%t%s -> (output->unit)->string->unit
\end{ocamlcode}
they all should be processed at \textbf{compile time}

    \item Digest \\
      hash functions return a fingerprint of their entry (reversible)

\begin{ocamlcode}
   val string : string -> t -- fingerprint of a string
   val file : string -> t -- fingerprint of a file
\end{ocamlcode}

    \item Marshal estimate data size

\begin{alternate}
type external_flag = No_sharing | Closures

let size x = x |> flip Marshal.to_string [] |> flip Marshal.data_size 0;;           ;;
val size : 'a -> int = <fun>
# size 3;;
- : int = 1
# size 3.;;
- : int = 9
# size "ghsogho";;
- : int = 8
# size "ghsogho1";;
- : int = 9
# size "ghsogho1ah";;
- : int = 11
# size 111;;
- : int = 2
\end{alternate}

    \item Sys

\begin{ocamlcode}
os_type interactive word_size max_string_length
max_array_length time argv getenv command file_exists
remove rename chdir getcwd
\end{ocamlcode}

\begin{alternate}
# float (Sys.max_string_length ) /. (2. ** 57.);;
- : float = 0.999999999999999889
\end{alternate}

    \item Arg Filename Printexc
    \item Printexc

\begin{ocamlcode}
# module P = Printexc;;
\end{ocamlcode}

\begin{ocamlcode}
module P :
  sig
    val to_string : exn -> string
    val catch : ('a -> 'b) -> 'a -> 'b
    val get_backtrace : unit -> string
    val record_backtrace : bool -> unit
    val backtrace_status : unit -> bool
    val register_printer : (exn -> string option) -> unit
    val pass : ('a -> 'b) -> 'a -> 'b
    val print : 'a BatInnerIO.output -> exn -> unit
    val print_backtrace : 'a BatInnerIO.output -> unit
  end
\end{ocamlcode}

    \item Num
    \item \verb|Arith_status|

\begin{ocamlcode}
# module X = Arith_status;;
\end{ocamlcode}
\begin{ocamlcode}
module X :
  sig
    val arith_status : unit -> unit
    val get_error_when_null_denominator : unit -> bool
    val set_error_when_null_denominator : bool -> unit
    val get_normalize_ratio : unit -> bool
    val set_normalize_ratio : bool -> unit
    val get_normalize_ratio_when_printing : unit -> bool
    val set_normalize_ratio_when_printing : bool -> unit
    val get_approx_printing : unit -> bool
    val set_approx_printing : bool -> unit
    val get_floating_precision : unit -> int
    val set_floating_precision : int -> unit
  end
\end{ocamlcode}

    \item Dynlink \\
      choice at execution time, load a new module and hide the
      code code (hot-patch)
      actually (\#load is kinda hot-patch), however to write it in programs
      \emph{more flexible} than \#load , load requires its name are fixed, and
      load will check .mli file, Dynlink \textbf{does not} do this check, while when you
      want to do X.blabla, it still checks, so still don't work, only side
      effects will work.

\begin{ocamlcode}
#direcotry "+dynlink";;
#load "dynlink.cma";;
Dynlink.loadfile "test.cmo";;
\end{ocamlcode}

    \end{enumerate}

  \item syntaxes
  \item expr

\begin{ocamlcode}
exp	::=value-path  -- value-name or module-path.value-name
 	| constant
 	| ( expr )
 	| begin expr end
 	| ( expr :  typexpr )
 	| expr ,  expr  { , expr } -- tuple
 	| constr  expr  -- constructor
 	| `tag-name  expr  -- polymorphic variant
 	| expr ::  expr  -- list
 	| [ expr  { ; expr } ]
 	| [| expr  { ; expr } |]
 	| { field =  expr  { ; field =  expr } }
 	| { expr with  field =  expr  { ; field =  expr } }
 	| expr  { argument }+ -- application
 	| prefix-symbol  expr  -- prefix operator
 	| expr  infix-op  expr
 	| expr .  field
 	| expr .  field <-  expr  -- still an expression
 	| expr .(  expr )
 	| expr .(  expr ) <-  expr
 	| expr .[  expr ]
 	| expr .[  expr ] <-  expr
 	| if expr then  expr  [ else expr ]
 	| while expr do  expr done
 	| for ident =  expr  ( to |  downto ) expr do  expr done
 	| expr ;  expr
 	| match expr with  pattern-matching
 	| function pattern-matching
 	| fun multiple-matching  -- multiple parameters matching
 	| try expr with  pattern-matching
 	| let [rec] let-binding   { and let-binding } in  expr
 	| new class-path
 	| object class-body end
 	| expr #  method-name
 	| inst-var-name
 	| inst-var-name <-  expr
 	| ( expr :>  typexpr )
 	| ( expr :  typexpr :>  typexpr )
 	| {< inst-var-name =  expr  { ; inst-var-name =  expr } >}
 	| assert expr
 	| lazy expr

argument::=expr
 	| ~ label-name
 	| ~ label-name :  expr
 	| ? label-name
 	| ? label-name :  expr

pattern-matching::=
 [|] pattern [when expr]-> expr { |pattern  [when expr] ->  expr }

multiple-matching::= { parameter }+  [when expr]-> expr

let-binding::=pattern =  expr
 	| value-name  { parameter }  [: typexpr] =  expr

parameter::=pattern
 	| ~ label-name
 	| ~ ( label-name  [: typexpr] )
 	| ~ label-name :  pattern
 	| ? label-name
 	| ? ( label-name  [: typexpr]  [= expr] )
 	| ? label-name :  pattern
 	| ? label-name : (  pattern  [: typexpr]  [= expr] )
\end{ocamlcode}

\begin{alternate}
  let f ?test:(Some x ) y = x + y;;
  ^^^^^^^^^^^^^^^^^^^^^^^^^
\end{alternate}

\begin{ocamlcode}
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
None
val f : ?test:int -> int -> int = <fun>
\end{ocamlcode}

  \item pattern

\begin{ocamlcode}
pattern	::=	value-name
 	| _
 	| constant
 	| pattern as  value-name
 	| ( pattern )
 	| ( pattern :  typexpr )
 	| pattern |  pattern
 	| constr  pattern
 	| `tag-name  pattern
 	| #typeconstr-name  -- object ?
 	| pattern  { , pattern }
 	| { field =  pattern  { ; field =  pattern } }
 	| [ pattern  { ; pattern } ]
 	| pattern ::  pattern
 	| [| pattern  { ; pattern } |]
 	| lazy pattern
\end{ocamlcode}

  \item toplevel-phrase

\begin{ocamlcode}
toplevel-input::= { toplevel-phrase } ;;

toplevel-phrase::=definition
 	| expr
 	| #ident  directive-argument

directive-argument::=epsilon
 	| string-literal
 	| integer-literal
 	| value-path
defition ::= let [rec] let-binding {and let-binding}
        | external value-name : typexpr = external-declartion
        | type-definition
        | exception-defition
        | class-definition
        | classtype-definition
        | module module-name {(module-name : module-type)} [:module-type] = module-expr
        | module type module-name = module-type
        | open module-path
        | include module-expr
\end{ocamlcode}

  \item type-definition

\begin{ocamlcode}

type-definition	::= type typedef  { and typedef }

typedef	::= [type-params]  typeconstr-name  [type-information]

type-information::=
  [type-equation] [type-representation]{ type-constraint }
type-equation::= = typexpr

type-representation::=
          = constr-decl  { | constr-decl }
 	| = { field-decl  { ; field-decl } }

type-params::=	type-param
 	| ( type-param  { , type-param } )

type-param::=	' ident
 	| + ' ident
 	| - ' ident

constr-decl::=	constr-name
 	| constr-name of  typexpr  { * typexpr }

field-decl::=	field-name :  poly-typexpr
 	| mutable field-name :  poly-typexpr
type-constraint	::=constraint ' ident =  typexpr
\end{ocamlcode}

\begin{alternate}
# type t;;
type t
\end{alternate}

\item  interoperating with C

  Difficutilies
  \begin{enumerate}
  \item Machine reperesentation of data
  \item GC \\
    calling a c function from ocaml must not modify the memory in ways incompatible
    with ocaml gc.
  \item Exceptions \\
    C does not support exceptions, different mechanisms for aborting computations,
    this complicates ocaml's exception handling
  \item sharing common resources \\
    input-output. each language maintains its own input-output buffers.
  \end{enumerate}

  Communications
  \begin{enumerate}
  \item external declarations \\
    it associates a c function definition with an ocaml name, while giving the
    type of the latter.

    \begin{bluetext}
      external caml_name : type = "C_name"
      val caml_name : type
    \end{bluetext}
    both workds, but in the latter case, calls to the c function \textit{first go}
    through the general function application mechanism of ocaml. This is slightly
    less efficient, but hides the implementation of the function as a c function.
  \item external functions with more than five arguments
    \begin{bluetext}
      external caml_name : type = "C_name_bytecode" "C_name_native"
    \end{bluetext}
  \end{enumerate}

\end{enumerate}

\subsubsection{chap7 Development Tools}
\label{sec:chap7-devel-tools}
\begin{enumerate}

\item Command names  \\

\item chap10 Program Analysis Tool \\
  \begin{enumerate}
  \item ocamldep \\

    \begin{tabular}{|c|c|}
      -I & add dir \\
      -impl,-intf & \\
      -ml(i)-synonym <e> & cosider <e> as a synonym of .ml(i) extension \\
      -modules & Print module dependencies in raw form(not suitable for make) \\
      -native & generate dependencies for a pure native-code project \\
      -slash & for windows \& unix \\
    \end{tabular}

\begin{ocamlcode}
ocamldep -modules *.ml
\end{ocamlcode}

\begin{ocamlcode}
ta.ml: Array Printf
tb.ml: Array Ta
\end{ocamlcode}

  \begin{ocamlcode}
ocamldep *.ml
\end{ocamlcode}

\begin{ocamlcode}
ta.cmo:
ta.cmx:
tb.cmo: ta.cmo
tb.cmx: ta.cmx
\end{ocamlcode}

other examples

\begin{bluetext}
ocamlfind ocamldep -modules dir_top_level_util.ml > dir_top_level_util.ml.depends
ocamlfind ocamldep -pp 'camlp4of -parser pa_mikmatch_pcre.cma' -modules dir_top_level.ml > dir_top_level.ml.depends
\end{bluetext}

\item debug

  \#(un)trace command ,\#untrace\_all.
  \begin{alternate}
let verify_div a b q r = a = b * q + r ;;
val verify_div : int -> int -> int -> int -> bool = <fun>
# #trace verif_div ;;
Unbound value verif_div.
# #trace verify_div ;;
verify_div is now traced.
\end{alternate}

\begin{ocamlcode}
verify_div 11 5 2 1 ;;
\end{ocamlcode}

\begin{ocamlcode}
verify_div <-- 11
verify_div --> <fun>
verify_div* <-- 5
verify_div* --> <fun>
verify_div** <-- 2
verify_div** --> <fun>
verify_div*** <-- 1
verify_div*** --> true
- : bool = true
\end{ocamlcode}

\begin{bluetext}
let rec belongs_to (e:int) = function
    | [] -> false
    | t :: q -> (e=t) || belongs_to e q;;
    val belongs_to : int -> int list -> bool = <fun>
# #trace belongs_to;;
belongs_to is now traced.
# belongs_to 4 [3;5;7;4];;
belongs_to <-- 4
belongs_to --> <fun>
belongs_to* <-- [3; 5; 7; 4]
belongs_to <-- 4
belongs_to --> <fun>
belongs_to* <-- [5; 7; 4]
belongs_to <-- 4
belongs_to --> <fun>
belongs_to* <-- [7; 4]
belongs_to <-- 4
belongs_to --> <fun>
belongs_to* <-- [4]
belongs_to* --> true
belongs_to* --> true
belongs_to* --> true
belongs_to* --> true
- : bool = true
\end{bluetext}

\begin{bluetext}
# let rec belongs to (e : int) = function
[] -> false
| t :: q -> belongs to e q || (e = t) ; ;
val belongs_to : int -> int list -> bool = <fun> # #trace belongs to ;;
belongs_to is now traced.
# belongs to 3 [3;5;7] ;;
belongs_to <-- 3
belongs_to --> <fun>
belongs_to* <-- [3; 5; 7]
belongs_to <-- 3
belongs_to --> <fun>
belongs_to* <-- [5; 7]
belongs_to <-- 3
belongs_to --> <fun>
belongs_to* <-- [7]
belongs_to <-- 3
belongs_to --> <fun>
belongs_to* <-- []
belongs_to* --> false
belongs_to* --> false
belongs_to* --> false
belongs_to* --> true
- : bool = true
\end{bluetext}

Trace providing a mechanism for the efficiency analysis of recursive functions, not that friendly, however, no idented output.
To make things worse, trace \textit{does not show the value corresponding to an argument of a parameterized type}. The toploop can show
only monomorphic types.

Moreover, it only keeps the inferred types of
\textit{global declarations}. Therefore after compilation of the
expression, the toplevel in fact \textit{no longer } processes any
furthuer type information about the expression.

Only global type declarations are kept in the environment of the
toplevel loop, \textit{local functions} can not be traced for the same reasons
as above
\begin{bluetext}
let rec belongs_to e = function
    | [] -> false
    | t :: q -> (e=t) || belongs_to e q;;
    val belongs_to : 'a -> 'a list -> bool = <fun>
# belongs_to 4 [3;5;7;4];;
- : bool = true
# #trace belongs_to;;
belongs_to is now traced.
# belongs_to 4 [3;5;7;4];;
belongs_to <-- <poly>
belongs_to --> <fun>
belongs_to* <-- [<poly>; <poly>; <poly>; <poly>]
belongs_to <-- <poly>
belongs_to --> <fun>
belongs_to* <-- [<poly>; <poly>; <poly>]
belongs_to <-- <poly>
belongs_to --> <fun>
belongs_to* <-- [<poly>; <poly>]
belongs_to <-- <poly>
belongs_to --> <fun>
belongs_to* <-- [<poly>]
belongs_to* --> true
belongs_to* --> true
belongs_to* --> true
belongs_to* --> true
- : bool = true
\end{bluetext}

\item ocamldbg

  The \textit{-g} option produces a \textit{.cmo} file with the
  debugging information. (bytecode only)
  \end{enumerate}
\end{enumerate}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../master"
%%% End:


\documentclass[11pt, oneside]{article}   	% use "amsart" instead of "article" for AMSLaTeX format

% \usepackage{draftwatermark}
% \SetWatermarkText{Draft}
% \SetWatermarkScale{5}
% \SetWatermarkLightness {0.9}
% \SetWatermarkColor[rgb]{0.7,0,0}

\usepackage{geometry}                		% See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper}                   		% ... or a4paper or a5paper or ...
%\geometry{landscape}                		% Activate for for rotated page geometry
%\usepackage[parfill]{parskip}    		% Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}				% Use pdf, png, jpg, or eps� with pdflatex; use eps in DVI mode
								% TeX will automatically convert eps --> pdf in pdflat
								% TeX will automatically convert eps --> pdf in pdflatex
\usepackage{amssymb}
\usepackage{mathrsfs}
\usepackage{hyperref}
\usepackage{url}
\usepackage{subcaption}
\usepackage{authblk}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{fixltx2e}
\usepackage{hyperref}
\usepackage{alltt}
\usepackage{color}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{float}
\usepackage{bigints}
\usepackage{braket}
\usepackage{siunitx}
\usepackage{relsize}
\usepackage{setspace}

%
% so you can do e.g., \begin{bmatrix}[r] (or [c] or [l])
%

\newcommand{\argmax}{\operatornamewithlimits{argmax}}
\newcommand{\argmin}{\operatornamewithlimits{argmin}}

\title{Euler's Product Formula and the Riemann Zeta Function}
\author{David Meyer \\ dmm@1-4-5.net}

\date{Last update: \today}							% Activate to display a given date or no date

\begin{document}
\maketitle

\section{Introduction}
In the 1730s Leonhard Euler proved one of the most important theorems in number theory, known today as Euler's product formula  \cite{euler_product_formula}.
Euler's product formula exposes the deep relationship between the prime numbers and the Riemann zeta function \cite{wiki:zeta}. Euler's proof appeared in his thesis titled
\emph{Variae observationes circa series infinitas} (Various Observations about Infinite Series), which was published by the
St. Petersburg Academy in 1737 \cite{mactutor:history_of_calculus}. The discovery of the product formula was an important advancement in number theory
and is perhaps the main reason why (or at least one of the main reasons why) the zeta function plays such a central role in the study of prime numbers.

\bigskip
\noindent
The Euler product formula for the Riemann zeta function $\zeta(s)$ is

\bigskip
\begin{equation*}
\zeta (s)=\sum _{n=1}^{\infty }{\frac {1}{n^{s}}}=\prod _{p{\text{ prime}}}{\frac {1}{1-p^{-s}}}
\end{equation*}

\bigskip
\noindent
Here the left hand side is the famous Riemann zeta function:

\bigskip
\begin{equation*}
\zeta (s) = \sum _{n=1}^{\infty } \frac {1}{n^{s}} = 1 + \frac {1}{2^{s}} + \frac {1}{3^{s}} + \frac {1}{4^{s}}  + \frac {1}{5^{s}}  + \cdots
\end{equation*}

\bigskip
\noindent
and the product on the right hand side extends over all prime numbers $p$:

\bigskip
\begin{equation*}
\prod _{p {\text{ prime}}}  \frac {1}{1-p^{-s}} = \frac {1}{1-2^{-s}} \cdot \frac {1}{1-3^{-s}} \cdot \frac {1}{1-5^{-s}}  \cdots  \frac {1}{1-p^{-s}} \cdots
\end{equation*}

\bigskip
\noindent
So how did Euler find this amazing relationship between the zeta function and the primes?

\section{Euler's Proof of the Product Formula}
The proof presented here (amazingly) makes use only of simple algebra and is the method by which Euler originally discovered the product formula.
Euler's proof is quite clever and takes advantage of a sieving process that is analogous to the ancient Sieve of Eratosthenes \cite{wiki:sieve_of_eratosthenes}.
The proof is iterative and proceeds as follows:

\bigskip
\noindent
Euler's proof starts with the zeta function\footnote{Euler first studied $\zeta(s)$ as a real function; Riemann was the
first to view it as a complex function \cite{nichols2016}.}, Equation \ref{equation1},
as the input to the first iteration of the proof.

\bigskip
\begin{equation}
\zeta(s) = \sum _{n=1}^{\infty } \frac {1}{n^{s}} = 1 + \frac {1}{2^{s}} + \frac {1}{3^{s}} + \frac{1}{4^s} + \frac{1}{5^2} + \cdots
\label{equation1}
\end{equation}

\bigskip
\noindent
The first step of the algorithm is to multiply both sides of the input, Equation \ref{equation1}, by the second term on its hand right side,  $\frac{1}{2^s}$.
This gives us

\bigskip
\begin{equation}
\frac {1}{2^{s}} \zeta (s) = \frac {1}{2^{s}} + \frac {1}{4^{s}} + \frac {1}{6^{s}} + \frac {1}{8^{s}} + \frac {1}{10^{s}} + \cdots
\label{equation2}
\end{equation}

\bigskip
\noindent
The next step is to subtract Equation \ref{equation2} from Equation \ref{equation1}, which gives us a new equation, Equation \ref{equation3}.
This new equation will serve as the input to the next iteration.

\bigskip
\begin{equation}
\left (1- \frac {1}{2^{s}} \right ) \zeta (s) = 1 + \frac {1}{3^{s}} + \frac {1}{5^{s}} + \frac {1}{7^{s}} + \frac {1}{9^{s}} + \frac {1}{11^{s}} + \frac {1}{13^{s}} + \cdots
\label{equation3}
\end{equation}

\bigskip
\noindent
Notice that this process knocks out all of the terms on the right hand side of the input equation (Equation \ref{equation1}) which have a factor of 2.
These are the terms that are "sieved" in this iteration of the algorithm.

\bigskip
\noindent
Now we repeat the previous steps with their output (Equation \ref{equation3}) as input.
Accordingly we multiply Equation \ref{equation3} by the the second term on its right hand side, $\frac{1}{3^s}$, which gives us

\bigskip
\begin{equation}
\frac {1}{3^{s}} \left (1 - \frac {1}{2^{s}} \right ) \zeta (s) = \frac {1}{3^{s}} + \frac {1}{9^{s}} + \frac {1}{15^{s}} + \frac {1}{21^{s}} + \frac {1}{27^{s}} + \frac {1}{33^{s}} + \cdots
\label{equation4}
\end{equation}

\bigskip
\noindent
Subtracting Equation \ref{equation4} from Equation \ref{equation3} we  get a new equation, Equation \ref{equation5}:

\bigskip
\begin{equation}
\left ( 1 - \frac {1}{3^{s}} \right ) \left ( 1 - \frac {1}{2^{s}} \right ) \zeta (s) = 1 + \frac {1}{5^{s}} + \frac {1}{7^{s}} + \frac {1}{11^{s}} + \frac {1}{13^{s}} + \frac {1}{17^{s}} + \cdots
\label{equation5}
\end{equation}

\bigskip
\noindent
This iteration knocks out all of the terms on the right hand side of Equation \ref{equation3} which have a factor of 3. The next iteration will knock out all of the terms
on the right hand side that have a factor of 5, yielding

\bigskip
\begin{equation*}
\left ( 1 - \frac {1}{5^{s}} \right )  \left ( 1 - \frac {1}{3^{s}} \right ) \left ( 1 - \frac {1}{2^{s}} \right ) \zeta (s) = 1 + \frac {1}{7^{s}} + \frac {1}{11^{s}} + \frac {1}{13^{s}} + \frac {1}{17^{s}} + \cdots
\end{equation*}

\medskip
\bigskip
\noindent
What we see is that this algorithm "sieves" the right hand sides of these successive equations while building up the product of
$\left ( 1 - \frac{1}{p^{s}} \right)$ terms for prime $p$ on the left hand side.

\bigskip
\noindent
If we repeat this process infinitely we wind up with

\begin{equation*}
\cdots \left ( 1 - \frac {1}{13^{s}} \right ) \left ( 1 - \frac {1}{11^{s}} \right ) \left ( 1 - \frac {1}{7^{s}} \right ) \left ( 1 - \frac {1}{5^{s}} \right )
 \left ( 1 - \frac {1}{3^{s}} \right ) \left ( 1 - \frac {1}{2^{s}} \right ) \zeta (s) = 1
\end{equation*}

\bigskip
\noindent
Solving for $\zeta(s)$ we get

\bigskip
\begin{equation*}
\zeta (s) = \frac {1}{\left ( 1 - \frac {1}{2^{s}} \right ) \left ( 1 - \frac {1}{3^{s}} \right ) \left ( 1 - \frac {1}{5^{s}} \right ) \left ( 1 - \frac {1}{7^{s}} \right )
                              \left ( 1 - \frac {1}{11^{s}} \right ) \left ( 1 - \frac {1}{13^{s}} \right )\cdots}
\end{equation*}

\bigskip
\bigskip
\noindent
which can be written in a perhaps more familiar (and more compact) way as an infinite product over all of the primes:

\bigskip
\begin{equation*}
\zeta (s) = \prod _{p{\text{ prime}}} \frac {1}{1-p^{-s}}
\end{equation*}

\bigskip
\noindent
which is Euler's product formula for the zeta function\footnote{This result is frequently referred to simply as "Euler's product formula".}.

\bigskip
\noindent
Finally, to make this proof rigorous we need to show that when $\Re (s) > 1$ the right hand side of the sieved equations approaches 1.
Fortunately for us this follows immediately from the convergence of the Dirichlet series for $\zeta(s)$ \cite{wiki:dirichlet_series}. $\square$

\section{Conclusions}
Euler's product formula is one of the most important results in the history of number theory and paved the way for Dirichlet, Riemann and others to the fuse
arithmetic and analysis into analytic number theory \cite{wiki:analytic_number_theory}.  In fact, so important is Euler's product formula in the history of number
theory that John Derbyshire, in his 2003 book \emph{Prime Obsession: Bernhard Riemann and the Greatest Unsolved Problem in Mathematics}
\cite{book:prime_obsession}, calls Euler's result the "Golden Key" to indicate it's significance in the development of analytic number theory and number
theory in general.

\bigskip
\noindent
As an aside, Mathologer  has a very nice video on Euler's product formula that has some fancy animations that make it easy to see how the sieve
works \cite{mathologer:eulers_product_formula}.

\bibliographystyle{plain}
\bibliography{/Users/dmm/papers/bib/qc}

\end{document}


\documentclass{article}
\usepackage{fullpage,graphicx}
\usepackage{amsmath,amsfonts,amsthm,amssymb,multirow, url}
\usepackage{tikz}
\usepackage{wrapfig}
\usepackage{algorithmic}
\usepackage[ruled,vlined,commentsnumbered,titlenotnumbered]{algorithm2e}
\newcommand{\expecting}[1]{\noindent{[\textbf{We are expecting:} \em #1\em]}}
\newcommand{\pts}[1]{\textbf{(#1 pt.)}}

\begin{document}
\noindent
CS 161 \hfill \textbf{Problem Set 1} \newline
{Winter 2019} \hfill \textbf{Due:} Friday, January 18, 2019 at 3pm on Gradescope.

\noindent
\rule{\linewidth}{0.4pt}

\noindent
\textbf{Style guide and expectations:} Please see the ``Homework" part of the ``Resources" section on the webpage for guidance on what we are looking for in homework solutions.  We will grade according to these standards.
\newline\newline
Make sure to look at the ``\textbf{We are expecting}'' blocks below each problem to see what we will be grading for in each problem!

\section*{Exercises}
Please do the exercises on your own.

\noindent
\rule{\linewidth}{0.4pt}
\begin{enumerate}

%%%%%%%%%%

\item[0.] \pts{1} Have you thoroughly read the course policies on the webpage?

\expecting{The answer ``yes."}

%%%%%%%%%%%

\item \pts{1} See the IPython notebook \texttt{HW1.ipynb} for Exercise 1.  Modify the code to generate a plot that convinces you that $T(x) = O(g(x))$. \textbf{Note:} There are instructions for installing Jupyter notebooks in the pre-lecture exercise for Lecture 2.

\expecting{Your choice of $c$, $n_0$, the plot that you created after modifying the code in Exercise 1, and a short explanation of why this plot should convince a viewer that $T(x) = O(g(x))$.}

%%%%%%%%%%%

\item \pts{3} See the IPython notebook \texttt{HW1.ipynb} for Exercise 2, parts A, B and C.
\begin{itemize}
	\item[(A)] What is the asymptotic runtime of the function \texttt{numOnes( lst )} given in the Python notebook?  Give the smallest answer that is correct.  (For example, it is true that the runtime is $O(2^n)$, but you can do better).

	\expecting{Your answer in the form ``The running time of \texttt{numOnes( lst )} on a list of size $n$ is $O(\_\_\_)$.", and a short explanation of why this is the case. }

	\item[(B)] Modify the code in \texttt{HW1.ipynb} to generate a picture that backs up your claim from Part (A).

	\expecting{Your choice of $c$, $n_0$, and $g(n)$; the plot that you created after modifying the code in Exercise 2; and a short explanation of why this plot should convince a viewer that the runtime of \texttt{numOnes} is what you claimed it was.}

	\item[(C)] How much time do you think it would take to run \texttt{numOnes} on an input of size $n = 10^{15}$?

\expecting{Your answer (in whichever unit of time makes the most sense) with a short explanation that references the runtime data you generated in part (B).  You don't need to do any fancy statistics, just a reasonable back-of-the-envelope calculation.}
\end{itemize}

%%%%%%%%%%%

\item \pts{4} Using the definition of big-Oh, formally prove the following statements.
\begin{enumerate}
	\item $3\sqrt{n} + 2 = O( \sqrt{n} )$  (Note that you gave a ``proof-by-picture" of this in Exercise 1).
	\item $n^2 = \Omega(n)$.
	\item $2^{2^{2^{100}}} = \Theta(1)$.
	\item $4^n$ is \textbf{not} $O(2^n)$.
\end{enumerate}
\expecting{A proof for each part, using the definition of $O(), \Omega()$, and $\Theta()$.}

\end{enumerate}

\noindent
\rule{\linewidth}{0.4pt}
\newpage
\section*{Problems}
You may talk with your fellow CS161-ers about the problems.  However:
\begin{itemize}
	\item Try the problems on your own \em before \em collaborating.
	\item Write up your answers yourself, in your own words.   You should never share your typed-up solutions with your collaborators.
	\item If you collaborated, list the names of the students you collaborated with at the beginning of each problem.
\end{itemize}

\noindent
\rule{\linewidth}{0.4pt}

\begin{enumerate}
\item \textbf{[True or false?]} \pts{4}
In the following, suppose that $f:\mathbb{N} \to \mathbb{N}$ and $g:\mathbb{N} \to \mathbb{N}$ are strictly increasing functions.  (Recall that $\mathbb{N}$ denotes the natural numbers $\{0,1,2,\ldots\}$).

True or false?
\begin{enumerate}
\item \pts{2} If $f(n) = O(g(n))$, then $\log(f(n)) = O( \log(g(n)) )$.  (If it helps, you may assume that $f$ and $g$ are strictly positive).
\item \pts{2} If $f(n) = O(g(n))$, then $2^{f(n)} = O\left( 2^{g(n)} \right)$.
\end{enumerate}
\expecting{For each part, either a proof or a counter-example (along with a proof that your counter-example is a counter-example), using the definition of $O(\cdot)$.}

%%%%%%%%%%%%%%%

\item \textbf{[Alternative MergeSorts]} \pts{6}

In class, we saw how \textsc{MergeSort} works by recursively breaking up a list into two smaller lists.  Consider the following version, which recursively breaks a list up into three smaller lists:

\begin{verbatim}
# Assume that A is a list of distinct integers and len(A) is a power of 3.
# This function returns a sorted version of A.
def MergeSort3(A):
     n = len(A)
     if n <= 3:
         return InsertionSort(A)
            # It takes time O(1) to InsertionSort a list of length <= 3.
     L = MergeSort3(A[0:n/3])
     M = MergeSort3(A[n/3:2n/3])
     R = MergeSort3(A[2n/3:n])
     return Merge3(L,M,R)
         # Merge3 merges three sorted lists of size n/3 into a sorted list of size n.
\end{verbatim}

\begin{enumerate}
\item \pts{2} Write pseudocode for \texttt{Merge3} that runs in time $O(n)$.  Your function should take as input three sorted lists \texttt{L}, \texttt{M}, and \texttt{R} of length $n/3$ (where $n$ is a power of $3$) and return a sorted list of length $n$ which contains all the entries of \texttt{L} and \texttt{M} and \texttt{R}.

\expecting{Pseudocode \textbf{AND} an English description of what it does, as well as an informal explanation of why the running time is $O(n)$. You may assume that $n$ is a power of $3$ and that all of the elements across \texttt{L,M,R} are distinct.}
\item \pts{2} Show that, with your version of \texttt{Merge3} from part (a), \texttt{MergeSort3} runs in time $O(n \log(n))$ when run on a list of length $n$.

\expecting{An informal but convincing argument.  Do not use the Master Theorem, instead argue this ``from scratch'' like we did in Lecture 2.}

\vfill\hfill (More parts on next page...)
\newpage
\item \pts{2}
Your friend has gotten pretty excited by this, and thinks they have a sorting algorithm that runs in time $O(n \log\log(n))$, even faster than \textsc{MergeSort}.  Here is their reasoning:

\begin{enumerate}
\item Instead of \texttt{MergeSort3} as above, consider a version \texttt{MergeSort\_k} which breaks up the list $A$ recursively into $k$ parts, and uses a routine \texttt{Merge\_k} similar to the \texttt{Merge3} you wrote in part (a).
\item The routine \texttt{Merge\_k} takes $k$ sorted lists of size $n/k$, and returns a sorted list of size $n$.  Your approach in part (a) still applies: we've already seen that \texttt{Merge\_k} runs in time $O(n)$ for $k=2$ and $k=3$, and it's not hard to see that the natural generalization also runs in time $O(n)$ for $k=4,5,6, \ldots$.
\item Now instantiate this algorithm \texttt{MergeSort\_k} for $k = \sqrt{n}$.  That is, at each step we divide a list of size $n$ into $\sqrt{n}$ pieces of size $\sqrt{n}$, and recurse on those.  (For simplicity, assume that $n$ is of the form $n= 2^{2^t}$ for some $t$ so that $\sqrt{n}$ and $\sqrt{ \sqrt{n} }$ and so on is always an integer until $n=2$.)
\item Now we have a recursive algorithm with the following properties:
\begin{itemize}
	\item A problem of size $n$ gets broken up into $\sqrt{n}$ problems of size $\sqrt{n}$.
	\item The work to run \texttt{Merge\_}$\sqrt{n}$ for a subproblem of size $n$ is $O(n)$ by part (ii).
\end{itemize}
The running time is $O(n \log\log(n))$.
To see this, first notice that there are $O(\log\log(n))$ levels in the recursion tree, since that's how many times you need to take the square root of $n$ to get down to problems of size $O(1)$.  At the $0$'th level of the recursion tree, there is one problem of size $n$.  At level $1$, there are $\sqrt{n}$ problems of size $\sqrt{n}$.  At level $2$, there are $\sqrt{n}\cdot n^{1/4} = n^{3/4}$ problems of size $n^{1/4}$.  In general, at the $j$'th level there are $n^{1 -1/2^j}$ problems of size $n^{1/2^j}$.  Thus, the amount of work at level $j$ is $O(n^{1-1/2^j} \cdot n^{1/2^j} ) = O(n)$.  Thus, since there is $O(n)$ work per layer for each of $O(\log\log(n))$ layers, the total amount of work is $O(n \log\log(n) )$.
\end{enumerate}

This is pretty neat!  Unfortunately, it's not correct.  What is the problem with your friend's argument?  (Don't go looking for little bugs---there is a big conceptual error.  The assumption that $n$ is of the form $2^{2^t}$ is fine.)

\expecting{An identification of which step(s) of the argument (i)-(iv) are problematic, and a clear explanation about what is wrong.}
\end{enumerate}

%%%%%%%%%%%%%%%

\vfill
\hfill
(More questions on next page...)

\newpage
\item \textbf{[Nuts!]} \pts{8}

\begin{minipage}{.55\textwidth}
Socrates the Scientific Squirrel is conducting some experiments.
Socrates lives in a very tall tree with $n$ branches, and she wants to find out what is the lowest branch $i$ so that an acorn will break open when dropped from branch $i$.  (If an acorn breaks open when dropped from branch $i$, then an acorn will also break open when dropped from branch $j$ for any $j \geq i$.)

\vspace{.3cm}
The catch is that, once an acorn is broken open, Socrates will eat it immediately and it can't be dropped again.

\begin{enumerate}
\item[(a)] \pts{2} Suppose that Socrates has $\lceil\log(n)\rceil + 1$ acorns.  Give a procedure so that she can identify the correct branch using $O(\log(n))$ drops.

\expecting{Very clear pseudocode or a short English description of your algorithm.  You do not need to justify the number of drops.  If it helps you may assume that $n$ is a power of $2$.}
\end{enumerate}
\end{minipage}
\hspace{.5cm}
\begin{minipage}{.4\textwidth}
\hfill
\begin{tikzpicture}
\node{
\includegraphics[width=6cm]{squirrel}};
\node(bn) at (-2.5,1.8) {Branch $n-1$};
\node(b0) at (-2.2,-1.25) {Branch $0$};
\node(b1) at (-2.2,-.55) {Branch $1$};
\draw(b0) -- (-.3, -1.25);
\draw(b1) -- (-.7, -.55);
\end{tikzpicture}
\end{minipage}

\begin{enumerate}
\item[(b)] \pts{1} Suppose that Socrates has only one acorn.  Give a procedure so that she can identify the correct branch using $O(n)$ drops, and explain why your $O(\log(n))$-drop solution from part (a) won't work.

\expecting{Very clear pseudocode or a short English description of your algorithm, and one sentence about why your algorithm from part (a) does not apply.  You do not need to justify the number of drops.}

\item[(c)] \pts{2} Suppose that Socrates has two acorns.  Give a procedure so that she can identify the correct branch using $O(\sqrt{n})$ drops.

\expecting{Pseudocode \textbf{AND}  a short English description of your algorithm, and a justification of the number of drops.  If it helps you may assume that $n$ is a perfect square.}

\item[(d)] \pts{2} Suppose that Socrates has $k = O(1)$ acorns.  Give a procedure so that she can identify the correct branch using $O(n^{1/k})$ drops.

\expecting{Pseudocode \textbf{AND} a short English description of your algorithm, and a justification of the number of drops.  If it helps you many assume that $n$ is of the form $n = m^k$ for some integer $m$.}

\item[(e)] \pts{1} What happens to your algorithm in part (d) when $k = \lceil \log(n) \rceil + 1$?
Is it $O(\log(n))$, like in part (a)? Is it $O(n^{1/k})$ when $k=\lceil \log(n) \rceil + 1$, like in part (d)?

\expecting{A sentence of the form ``the number of drops of my algorithm in part (d) when $k=\lceil \log(n) \rceil + 1$ is $O(\_\_\_\_)$'', along with justification.  Also, two yes/no answers to the two yes/no questions (you should justify your answers but do not need to include a formal proof). }

\item[(f)] \pts{NOT REQUIRED.  1 BONUS} Is $\Theta(n^{1/k})$ drops is the best that Socrates can do with $k$ acorns, for $k = O(1)$?  Either give a proof that she can't do better, or give an algorithm with asymptotically fewer drops.

\expecting{Nothing.  This part is not required.}

\end{enumerate}

\end{enumerate}

\rule{\linewidth}{0.4pt}
\section*{Feedback}
This part is not worth any points, but it is quick, painless, and anonymous, and we'd really appreciate it if you'd help us out by giving us feedback!
\begin{enumerate}
\item \pts{0} Please fill out the following poll, which asks about your expectations for the course:
\begin{center} \url{https://goo.gl/forms/RKAgpsDPu2IT9RWE3} \end{center}
\end{enumerate}

\end{document}


\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{geometry}
\usepackage{enumerate}
\usepackage{natbib}
\usepackage{float}%稳定图片位置
\usepackage{graphicx}%画图
\usepackage[english]{babel}
\usepackage{a4wide}
\usepackage{indentfirst}%缩进
\usepackage{enumerate}%加序号
\usepackage{multirow}%合并行
\title{\large UM-SJTU JOINT INSTITUTE\\Data Structures and Algorithms\\(VP281)\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\
Programming Assignment\\\ \\\ Programming Assignment One\\\  Sorting \\\ \\\ \\\ \\\ \\\ }
\author{Name: Pan Chongdan\\ID: 516370910121}
\date{Date: \today}

\begin{document}
\maketitle
\newpage
\section{Introduction}
The programming assignment asks me to implement six sorting algorithms including bubble sort, insertion sort, selection sort, merge sort and quick sort with in-place and not-inplace. The goal is clear, to gain experience in implementing these six sorting algorithms to sort lots of random numbers in ascending order.
\par Second, the assignment asks me to study the performance of these 6 studies by studying their time efficiency. In lecture slides3, professor gives a table summarizing the time required for each algorithm to complete the sorting process for consideration.
\begin{figure}[H]
\centering
\includegraphics[scale=0.4]{P1.png}
\end{figure}
\par However, we need to test the algorithms' time efficiency by ourselves, so I wrote a cpp file to print out the time required for each algorithm, which is included in appendix part.
\section{Result}
To test time efficiency, I used clock() function to get the time required. To get rid of other disturbing factor, I used to variable start and stop to get the time right before and after the sorting complete, then their difference is the time used. In my analysis, I get 10 set of data ,from which the sorted array' size is 1, 10, 50, 100, 500, 1000, 5000, 25000 and 100000.
\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|}
\hline
Data Size                 & 1    & 10   & 50     & 100    & 500    & 1000   & 5000   & 25000  & 100000 \\ \hline
Bubble Sort               & 3e-6 & 4e-6 & 2.7e-5 & 1.1e-4 & 1.7e-3 & 0.0057 & 0.053  & 1.4    & 24     \\ \hline
Insertion Sort            & 1e-6 & 3e-6 & 1.4e-5 & 5.4e-5 & 8.1e-4 & 0.0011 & 0.018  & 0.45   & 7.2    \\ \hline
Selection Sort            & 1e-6 & 3e-6 & 1.7e-5 & 6.3e-5 & 9e-4   & 0.0011 & 0.019  & 0.46   & 7.2    \\ \hline
Merge Sort                & 1e-6 & 5e-6 & 1.8e-5 & 4.8e-5 & 2e-4   & 1.4e-4 & 5.6e-4 & 0.0033 & 0.015  \\ \hline
Quick Sort (not in-place) & 1e-6 & 4e-6 & 1.9e-5 & 4.3e-5 & 2.1e-4 & 1.6e-4 & 6.5e-4 & 0.0036 & 0.016  \\ \hline
Quick Sort (in place)     & 1e-6 & 4e-6 & 1.7e-5 & 3.7e-5 & 2e-4   & 1.4e-4 & 5.6e-4 & 0.0032 & 0.014  \\ \hline
\end{tabular}
\end{table}
\begin{figure}[H]
\centering
\includegraphics[scale=0.55]{P2.png}
\end{figure}
\section{Conclusion}
This chart is plot by matlab, showing the time efficiency of each algorithms compared to each other, and it has following characteristics.
\begin{enumerate}
\item The average time required for each algorithm grows as the array grows bigger.
\item As shown in the graph before, bubble sort, insertion sort and selection sort require much more time to sort huge array than the other sort algorithms. However, when the data size is extremely small, there is no such characteristic and the insertion sort and selection sort even requires less time than merge sort and quick sort.
\item Even though insertion sort, selection sort and bubble sort have the same time complexity as shown in the graph from lecture, it seems that bubble sort needs more time to sort than others. I guess its because bubble sort needs to compare every two members of the array once.
\end{enumerate}
\section{Appendix}
The appendix shows the cpp code for time comparison.
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P3.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P4.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P5.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P6.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P7.png}
\end{figure}
Then the appendix shows the cpp code for each sort algorithm
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P8.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P8.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P10.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P11.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P12.png}
\end{figure}
\end{document}


\documentclass[11pt,largemargins]{homework}

\newcommand{\hwname}{Jacob Zimmerman}
\newcommand{\hwemail}{jezimmer}
\newcommand{\hwtype}{Homework}
\newcommand{\hwnum}{0}
\newcommand{\hwclass}{12-345}
\newcommand{\hwlecture}{0}
\newcommand{\hwsection}{Z}
\newcommand{\hwdate}{January 1, 1970}

% This is just used to generate filler content. You don't need it in an actual
% homework!
\usepackage{lipsum}

\begin{document}
\maketitle

\question
  This is my answer to the first question.

  Don't forget to fill in your personal and class information at the top!

\question
  This question's number will be auto-incremented.

  \lipsum[2]

\question*{Super Fancy Named Question}
  This question was given a fancy name!

  \lipsum[3]

% Sometimes questions get separated from their bodies. Use a \newpage to force
% them to wrap to the next page.
\newpage
\question
  Using the \texttt{induction} environment is a great way to typeset induction proofs!
  \begin{induction}
    \basecase
      Here I have my base case.
      This is usually about 1-2 lines of text that is not entirely difficult to come up with.
      That doesn't mean it's not important though!
    \indhyp
      Assume cool things to make proof work. Look, math:
      \[a^2 + b^2 = c^2\]
    \indstep
      Prove all the things.  When in doubt, write in Latin, because things
      written in Latin sound more true.  Lorem ipsum dolor sit amet, consectetur
      adipiscing elit. Maecenas tempor risus in dapibus aliquam. Donec at
      euismod dui. In libero turpis, blandit quis vestibulum ac, rutrum sit amet
      est. Suspendisse nec lacus vel dui lobortis lacinia at sit amet risus.
      Fusce dui ex, imperdiet nec finibus ut, bibendum a lacus.
  \end{induction}

  Therefore, we have proven the claim by induction in the \texttt{induction} environment.

\question
  Use the arabicparts environment to include the questionCounter number in the list.
  \begin{arabicparts}
    \questionpart Use \LaTeX
    \questionpart ???
    \questionpart Profit!
  \end{arabicparts}

  \lipsum[7]

\question
  Use the alphaparts environment for letters instead of numbers.
  \begin{alphaparts}
    \questionpart
      Use \LaTeX
    \questionpart ???
    \questionpart Profit!
  \end{alphaparts}

\question
    Question part numbers are auto-incremented within the same question, so if you have some question parts...

    \begin{alphaparts}
        \questionpart
            Use \LaTeX
        \questionpart ???
    \end{alphaparts}

    Followed by some additional text, followed by some more question parts:

    \begin{alphaparts}
        \questionpart Profit!
    \end{alphaparts}

    All parts will be continuously numbered.

\question
  Question numbers continue to auto-increment, regardless of question type.

  \lipsum[4]

% Use \renewcommand{\questiontype}{<text>} to change what word is displayed
% before numbered questions
\renewcommand{\questiontype}{Task}
\question
  This question has a different question type!

  \lipsum[5]

\renewcommand{\questiontype}{Question}

\question
  You can still do things like nesting lists inside of these environments.
  \begin{alphaparts}
    \questionpart Use \LaTeX
      \begin{enumerate}
        \item Open terminal
        \item Open vim
        \item Write LaTeX
      \end{enumerate}
    \questionpart ???
    \questionpart Profit!
  \end{alphaparts}

  \lipsum[9]

% Use the \setcounter{questionCounter}{<x>} to force the question number to a
% particular question. If your written homework's question start at number 9001,
% use the following
\setcounter{questionCounter}{9000}
\question
  \href{https://www.youtube.com/watch?v=SiMHTK15Pik}{It's over 9000!!!}

  \lipsum[6]

\section{}
  You can also use the \texttt{section} macro for starting a question.

  \lipsum[10]

\section*{Using the \texttt{section} Macro}
  The starred \texttt{section*} works as well.

  \lipsum[11]

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% University/School Laboratory Report
% LaTeX Template
% Version 3.1 (25/3/14)
%
% This template has been downloaded from:
% http://www.LaTeXTemplates.com
%
% Original author:
% Linux and Unix Users Group at Virginia Tech Wiki
% (https://vtluug.org/wiki/Example_LaTeX_chem_lab_report)
%
% License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------------------------
%	PACKAGES AND DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------

\documentclass[11pt,a4paper]{article}

\usepackage[version=3]{mhchem} % Package for chemical equation typesetting
\usepackage{siunitx} % Provides the \SI{}{} and \si{} command for typesetting SI units
\usepackage{graphicx} % Required for the inclusion of images
\usepackage{natbib} % Required to change bibliography style to APA
\usepackage{amsmath} % Required for some math elements

\usepackage{listings}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{subcaption}

\definecolor{mygray}{rgb}{0.4,0.4,0.4}
\definecolor{myorange}{rgb}{1.0,0.4,0}

%http://tex.stackexchange.com/questions/144503/why-does-a-space-destroy-my-keywords-in-listingspackage

\lstset{
 language=C,
basicstyle=\footnotesize\sffamily\color{black},
commentstyle=\color{mygray},
frame=single,
numbers=left,
numbersep=5pt,
numberstyle=\tiny\color{mygray},
keywordstyle=\color{green!70!blue},
showspaces=false,
showstringspaces=false,
stringstyle=\color{myorange},
tabsize=2,
  morekeywords={
    omp,
    parallel,
    for,
  }
  alsoother={\#},
  otherkeywords={\#pragma,\#define,\#ifdef,\#endif,\#else},
}

% Fall mal eigene margins definiert werden müssen
%\usepackage{geometry}
% \geometry{
% a4paper,
% total={170mm,257mm},
% left=20mm,
% top=20mm,
% }
%
\setlength\parindent{0pt} % Removes all indentation from paragraphs

\renewcommand{\labelenumi}{\alph{enumi}.} % Make numbering in the enumerate environment by letter rather than number (e.g. section 6)

%\usepackage{times} % Uncomment to use the Times New Roman font

%----------------------------------------------------------------------------------------
%	DOCUMENT INFORMATION
%----------------------------------------------------------------------------------------

\title{CPA Lab-Report \\ Lab 2 Prime Numbers} % Title

\author{Simon \textsc{Birrer}, Dominic \textsc{Sch\"urmann}} % Author name

\date{\today} % Date for the report

\begin{document}

\maketitle % Insert the title, author and date

\begin{center}
\begin{tabular}{l r}
Date Performed: & October 27, 2015 \\ % Date the experiment was performed
Students: & Simon Birrer \\ % Partner names
& Dominic Sch\"urmann \\
Instructor: & Francisco Javier Piris Ruano % Instructor/supervisor
\end{tabular}
\end{center}

\bigskip

\tableofcontents

\pagebreak

\section{Biggest prime storable in 8 bytes}

The Source for the solution is in the file \textit{primo\textunderscore  grande.c}. In the figure \ref{fig:code_primogrande} you will see the changes applied to function primo of the original code.

\begin{figure}[h]
\begin{lstlisting}
   int numberOfThreads;
   int offset;
   #ifdef _OPENMP
   #pragma omp parallel
   numberOfThreads = omp_get_num_threads();
   offset = 2 * numberOfThreads;
   #else
   numberOfThreads = 1;
   offset = 2;
   #endif

    #pragma omp parallel private(i)
    {
      #ifdef _OPENMP
      int threadIndex =  omp_get_thread_num();
      int startIndex = (2 * threadIndex) + 3;
      #else
      int startIndex = 3;
      #endif
      for (i = startIndex; p && i <= s; i += offset)
        if (n % i == 0) p = 0;
    }
\end{lstlisting}
\caption{code changes primo grande}
\label{fig:code_primogrande}
\end{figure}

\subsection{Compiling without OpenMP}

To use the program in both ways, either with or without OpenMP , we used the preprocessor directives. Now the compiler decides upon the arguments if the code will use OpenMP or not for the passages where OpenMP function calls will happen.\\

This can be seen in figure \ref{fig:code_primogrande} in lines 3-10 and 14-19.

\pagebreak
\subsection{Time measurement of parallelized version}

In figure \ref{ex12execution} are the measured times of executing the program with different numbers of threads using kahan.\\

\begin{figure}[h]
\centering
  \includegraphics[scale=0.35]{statistics/Ex12ResultGraph.png}
	\caption{execution times for exercise 1.2}
	\label{ex12execution}
\end{figure}

Since a node of kahan has 32 cores, the execution with 32 threads was the fastest. In addition the performance decreases if the number of threads will be increased to more than 32 threads. This is shown in figure \ref{ex12execution} and the overhead is even more visible in figure \ref{ex12speedUp} .
As a result of this, the best speedup will be achieved with one thread for each processor.

\begin{figure}[h]
\centering
\begin{minipage}{.5\textwidth}
  \centering
  \includegraphics[scale=0.25]{statistics/Ex12SpeedUpGraph.png}
	\caption{speed up for exercise 1.2}
	\label{ex12speedUp}
\end{minipage}%
\begin{minipage}{.5\textwidth}
  \centering
     \includegraphics[scale=0.25]{statistics/Ex12EfficiencyGraph.png}
	\caption{efficiency graph of exercise 1.2}
	\label{ex12efficiency}
\end{minipage}
\end{figure}

\section{Count primes in a range}

In all code examples, where a time measurement was needed, following code was used to measure the time in seconds:

\begin{figure}[h]
\label{code_timemeasurement}
\begin{lstlisting}
  #ifdef _OPENMP
  double t1 = omp_get_wtime();
  #endif

  //code for algorithm here

  #ifdef _OPENMP
  double t2 = omp_get_wtime();
  printf("looptime: %f seconds \n", t2-t1);
  #endif
\end{lstlisting}
\caption{execution time measurement}
\end{figure}

\subsection{Measurement sequential execution}

To measure the sequential time for the
\textit{primo\textunderscore numeros\textunderscore .c} code, we compiled the code with OpenMP. To achieve a sequential execution we defined the environment variable
\textit{OMP\textunderscore NUM\textunderscore THREADS = 1}. \\
The sequential execution took 289.24 seconds.

\subsection{Measurement parallel most inner loop}

Based on the exercise a measurement for the most inner loop does not make much sense, because it is really inefficient. Every execution without an if clause in the pragma omp definiton was terminated because of the walltime exceeding.

\pagebreak

\subsection{Measurement parallel most outer loop}

The most efficient way to speedup the given exercise code is by parallelize the most outer loop.
The source is in the file \textit{primo\textunderscore numeros\textunderscore reduction.c}.
The following code changes where made to achieve this:

\begin{figure}[h]
\label{code_mostouterloop}
\begin{lstlisting}
  int numberOfThreads;
  #pragma omp parallel
  numberOfThreads = omp_get_num_threads();
  n = 2; /* Por el 1 y el 2 */
  #pragma omp parallel for reduction(+:n) schedule(runtime)
  for (i = 3; i <= N; i += 2){
      if (primo(i))
      {
         n++;
      }
  }
\end{lstlisting}
\caption{parallelization of the most outer loop}
\end{figure}

The environment variable \textit{OMP\textunderscore NUM\textunderscore THREADS} was set to 32 Threads to achieve the best speedup.\\

\pagebreak

\subsubsection{scheduling distribution}

We checked the execution time for the same code with different scheduling distributions. Therefore we changed the environment variable \textit{OMP\textunderscore SCHEDULE} .

\begin{table}[h]
\centering
\label{table_scheduledistribution}
\begin{tabular}{| l | l | l | l |}
    \hline
    schedule type & chunk size & execution time in [s]\\
    \hline
	static & 0 & 17.58 \\
	\hline
	static & 1 & 13.58 \\
    \hline
	dynamic & 0 & 14.18 \\
    \hline
    dynamic & 1 & 14.23 \\
    \hline
    dynamic & 2 & 13.52 \\
    \hline
\end{tabular}
\caption{measurement results for different schedule distribution}
\end{table}

\subsection{Exercise 1: Without reduction clause}
\label{ex21}

The whole parallelization can be done without using a reduction for the variable n. In this code segment we used an OpenMP atomic clause to achieve the same behaviour.
The source is in the file \textit{primo\textunderscore numeros\textunderscore without\textunderscore reduction.c}.

\begin{figure}[h]
\label{code_withoutreduction}
\begin{lstlisting}
  int numberOfThreads;
  #pragma omp parallel
  numberOfThreads = omp_get_num_threads();
  n = 2; /* Por el 1 y el 2 */
  #pragma omp parallel for schedule(runtime)
  for (i = 3; i <= N; i += 2){
      if (primo(i))
      {
         #pragma omp atomic
         n++;
      }
  }
\end{lstlisting}
\caption{parallelization without reduction}
\end{figure}

\pagebreak

\subsection{Exercise 2: Printing found primes for each thread}
\label{ex22}

To find out which thread found how many prime numbers, we had to save the information for each thread in a vector. We use a vector based on the index of each thread and therefore we do not have to care about race conditions or any other problem based on public variables. The source is in the file \textit{primo\textunderscore numeros\textunderscore threadinfo.c}.

\begin{figure}[h]
\label{code_printprimeseachthread}
\begin{lstlisting}
  int numberOfThreads;
  #pragma omp parallel
  numberOfThreads = omp_get_num_threads();
  n = 2; /* Por el 1 y el 2 */

  int* primesFoundPerThread =
  	(int*) calloc(numberOfThreads, sizeof(int));

  for(i = 0; i < numberOfThreads; i++){
    primesFoundPerThread[i] = 0;
  }

  #pragma omp parallel for
  for (i = 3; i <= N; i += 2){

      if (primo(i))
      {
        int threadId = omp_get_thread_num();
        primesFoundPerThread[threadId]++;
         #pragma omp atomic
         n++;
      }
  }

  for(i = 0; i < numberOfThreads; i++){
    printf("thread %i: %i primes\n",i,primesFoundPerThread[i]);
  }
\end{lstlisting}
\caption{printing found primes by each thread}
\end{figure}

If we run this code with a static distribution we see, that thread zero computes the most prime numbers and thread 31 computes the least amount of numbers. Based on the prime number theory ,the bigger the numbers to look for primes are, the less prime numbers can be found. This reflects in the results of out measurements.

\pagebreak

\section{Appendix}

\subsection{Formulas}
Speed Up
$$ S(n,p) = \frac{t(n)}{t(n,p)} $$

Efficiency
$$ E(n,p) = \frac{S(n,p)}{p} $$

\subsection{Measurements of exercise 1}

\begin{table}[h]
\centering
\label{measuresEx1}
\begin{tabular}{@{}l||l|l|l|l|l|l|l|@{}}
number or threads & 1      & 2      & 4      & 8     & 16    & 32    & 64    \\
\hline
time in seconds   & 55.571 & 27.850 & 13.931 & 7.019 & 4.349 & 2.580 & 2.741
\end{tabular}
\caption{execution time exercise 1}
\end{table}

\begin{table}[h]
\centering
\label{measuresSpeedUpEx1}
\begin{tabular}{@{}l||l|l|l|l|l|l|l|@{}}
number or threads & 1      & 2      & 4      & 8     & 16    & 32    & 64    \\
\hline
speed up S(n,p)   & 1.000 & 1.995 & 3.989 & 7.917 & 12.779 & 21.539 & 20.273
\end{tabular}
\caption{speed up exercise 1}
\end{table}

\begin{table}[h]
\centering
\label{measuresSpeedUpEx1}
\begin{tabular}{@{}l||l|l|l|l|l|l|l|@{}}
number or threads & 1      & 2      & 4      & 8     & 16    & 32    & 64    \\
\hline
efficiency E(n,p)   & 1.000 & 0.998  & 0.997 & 0.990 & 0.799 & 0.673 & 0.317
\end{tabular}
\caption{efficiency exercise 1}
\end{table}

\listoffigures

\listoftables

%----------------------------------------------------------------------------------------
%	BIBLIOGRAPHY
%----------------------------------------------------------------------------------------

\bibliographystyle{apalike}

\bibliography{sample}

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

\end{document}


\subsubsection{Associative Array}

\begin{frame}<beamer>{\LectureToC}
  \tableofcontents[currentsection,currentsubsection]
\end{frame}

\begin{frame}{Associative Arrays}{Practical Example - Associative Array}
  \textbf{Idea using associative arrays:}
  \begin{itemize}
    \item
      Take the continent as {\color{Mittel-Blau}key}
    \item
      Use a counter (occurences) or a list with all countries associated
      with this continent as {\color{Mittel-Blau}value}
  \end{itemize}
  \textbf{Advantage:}
  \begin{itemize}
    \item
      Runtime $\mathcal{O}(n)$, implied we can find an element in
      $\mathcal{O}(1)$ as in normal arrays
  \end{itemize}
\end{frame}

\codeslide{python}{
\begin{frame}{Associative Arrays}{Python}
  \textbf{Python:}
  \lstinputlisting[
    language=Python,
    basicstyle=\small,
    tabsize=4,
    style={python-idle-code},
    escapechar={@}
  ]{Code/AssociativeArray.py}
\end{frame}
}

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

\codeslide{java}{
\begin{frame}{Associative Arrays}{Java}
  \vspace{-0.5em}
  \textbf{Java:}
  \vspace{-0.25em}
  \lstinputlisting[
    language=Java,
    basicstyle=\small,
    tabsize=4,
    style={java-eclipse-code},
    breaklines=false,
    escapechar={@},
    emph={countries},
    emphstyle=\color{java_variable}
  ]{Code/AssociativeArray.java}
\end{frame}
}

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

\codeslide{cpp}{
\begin{frame}{Associative Arrays}{C++}
  \textbf{C++:}
  \lstinputlisting[
    language=C++,
    basicstyle=\small,
    tabsize=4,
    style={cpp-eclipse-code},
    breaklines=false,
    morekeywords={endl},
    escapechar={@}
  ]{Code/AssociativeArray.cpp}
\end{frame}
}

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

\begin{frame}{Associative Arrays}{Efficiency}
  \textbf{Efficiency:}
  \begin{itemize}
    \item
      Depends on implementation
    \item
      Two typical implementations:
      \begin{itemize}
        \item
          \textbf{Hashing:}
          Calculates a checksum of the key used as key of a normal array\\
          \texttt{search}: $\mathcal{O}(1) \dots \mathcal{O}(n)$\\
          \texttt{insert}: $\mathcal{O}(1) \dots \mathcal{O}(n)$\\
          \texttt{delete}: $\mathcal{O}(1) \dots \mathcal{O}(n)$
        \item
          \textbf{(Binary-)Tree:}
          Creates a sorted (binary) tree\\
          \texttt{search}: $\mathcal{O}(\log n) \dots \mathcal{O}(n)$\\
          \texttt{insert}: $\mathcal{O}(\log n) \dots \mathcal{O}(n)$\\
          \texttt{delete}: $\mathcal{O}(\log n) \dots \mathcal{O}(n)$
      \end{itemize}
  \end{itemize}
\end{frame}

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

\begin{frame}{Associative Arrays}{Efficiency}
  \begin{table}[!h]
    \caption{Map implementions of programming languages}
    \begin{tabular}{c|cc}
      {} & Hashing & (Binary-)Tree\\
      \midrule
      Python & all dictionaries & {}\\
      Java & \texttt{java.util.HashMap} & \texttt{java.util.TreeMap}\\
      C++11/14 & \texttt{std::unordered\_map} & \texttt{std::map}\\
      C++98 & \texttt{\_\_gnu\_cxx::hash\_map} & \texttt{std::map}
    \end{tabular}
  \end{table}
\end{frame}

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

\codeslide{cpp}{
\begin{frame}{Associative Arrays}{C++}
  \textbf{\enquote{Feature} of C++ maps:}
  \lstinputlisting[
    language=C++,
    basicstyle=\small,
    tabsize=4,
    style={cpp-eclipse-code},
    breaklines=false,
    morekeywords={endl},
    escapechar={@}
  ]{Code/AssociativeArrayFeature.cpp}
\end{frame}
}


\documentclass[a4paper]{article}
%% Welcome to Overleaf!
%% If this is your first time using LaTeX, it might be worth going through this brief presentation:
%% https://www.overleaf.com/latex/learn/free-online-introduction-to-latex-part-1

%% Researchers have been using LaTeX for decades to typeset their papers, producing beautiful, crisp documents in the process. By learning LaTeX, you are effectively following in their footsteps, and learning a highly valuable skill!

%% The \usepackage commands below can be thought of as analogous to importing libraries into Python, for instance. We've pre-formatted this for you, so you can skip right ahead to the title below.

%% Language and font encodings
\usepackage[spanish,english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}

%% Sets page size and margins
\usepackage[a4paper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}

%% Useful packages
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage[colorlinks=true, allcolors=blue]{hyperref}
\usepackage{float}

%% Title
\title{
		%\vspace{-1in}
		\usefont{OT1}{bch}{b}{n}
		\normalfont \normalsize \textsc{CSE-4111 Artificial Intelligence Lab} \\ [10pt]
		\huge Constraint Satisfaction Problem : Arc Consistency Algorithms \\
}

\usepackage{authblk}
\author[0]{\textbf{Saif Mahmud}\\
		4th Year 1st Semester, Roll : SH - 054\\}

\begin{document}
\maketitle

\selectlanguage{english}
\begin{abstract}
Constraint Satisfaction Problem (CSP) is defined as a triple $ P = (X, D, C) $ where $ X = \{1,...,n\} $ is a set of variables. Each variable $ X_{i} ∈ X $ has a finite domain $ D_{i} ∈ D $ of values that can be assigned to it. Arc consistency is defined as $ X_{i} $ is arc-consistent with respect to another variable $ X_{j} $ if for every value in the current domain $ D_{i} $ there is some value in the domain $ D_{j} $ that satisfies the binary constraint on the arc $ (X_{i} , X_{j}) $. A CSP is arc consistent (AC) if and only if every variable is arc consistent. Arc Consistency is achieved through removing arc inconsistent values until complete consistency or a failure is obtained. In this regard, there are 4 (four) algorithms within the scope of this experiment attaining the goal of removing arc inconsistency from CSPs and they are namely \textit{AC - 1, AC - 2, AC - 3, and AC - 4}.
\end{abstract}
\section*{Problem Formulation}
This experiment will incorporate the implementation of AC algorithms and measuring the performance of the algorithms against defined evaluation metrics. The problem for this experiment is designed in a manner which is analogous to optimization through linear programming. In case of linear programming, an optimal value for each variable is obtained within the bar of given constraints. Here, I have merged the idea of removing inconsistency with finding optimal solution for given variables. Arc Consistency algorithms will eliminate the inconsistent values with respect to the constraints and thus will yield a reduced domain for each variable. If the size of reduced domain is 1 then it is the optimal solution and otherwise I have to search through the reduced domain for obtaining optimal value in reduced search space for linear programming.
\section*{Input Specification}
All four algorithms will take the CSP as the argument. Here, the nodes and edges of parameter CSP will be formulated with the following principles :
\begin{itemize}
	\item Constraint graphs will be generated with the variables for optimization in linear programming as nodes and binary constraints incorporating two variables as edges between the nodes.
	\item The domain size will be varied in the range $ [5, 25] $ (suppose) in order to measure the performance of the algorithms in proportion to domain size.
	\item Each variable will be assigned a domain set with discrete values ranging $ [a, b] $. In this regard, the experiment will include intersecting and non-intersecting domain values to detect the behavior of specific algorithm. Here, the value of a and b will be chosen for a specific variable like $ a = -50 $ and $ b = 150 $ and the values of a and b will be varied for each variable. In fact, each variable will have a domain of bounded range. This formulation of domain values will incorporate bounds propagation within the scope of binary constraints. Therefore, the algorithms will reduce the range of values for each domain and this domain will indicate the optimal solution for variables in linear programming.
\end{itemize}
\section*{Constraint Definition}
In order to maintain arc consistency, binary constraints can be imposed between the edge of two variables. Moreover, unary global constraints will be used to obtain node consistency. The following is an illustration of the constraints used in optimization technique for linear programming :\\

\textbf{Global Constraints} (Not always used)
\begin{itemize}
	\item All variables must assume positive value ($ X_{i}>0 $)
	\item \textit{Alldiff} : All variable will assume different values at the same time.
\end{itemize}

\textbf{Binary Constraints} (Let, X and Y are arbitrary variables in CSP)
\begin{itemize}
	\item $ X \pm Y \geq val$ where $ val $ is natural number within range $ [0, 100] $
	\item $ X \pm Y \leq val$ where $ val $ is natural number within range $ [0, 100] $
	\item $ X \pm Y = val$ where $ val $ is natural number within range $ [0, 100] $
\end{itemize}

\section*{Evaluation Metric}
The implementation of algorithms will be evaluated with the following metric in order to show a comparison among them :
\begin{itemize}
	\item CPU running time on the same input graph
	\item Change in performance with a gradual change in the size of input CSP graphs
	\item Time and Space Complexity of individual implementation of algorithms
	\item Mean constraint check and revision of nodes (in number) for each algorithm
\end{itemize}

\end{document}


\chapter{Introduction}

\section{Overview of the arbitrage-free asset pricing}

\begin{figure}[H]
	\centering
	\includegraphics[width=1\linewidth]{pic/bis}
	\caption{OTC derivatives notional amount outstanding. Source: \cite{bis2019statistics}}
	\label{fig:bis2019statistics}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=1\linewidth]{pic/bis2}
	\caption{OTC derivatives gross market values. Source: \cite{bis2019statistics}}
	\label{fig:bis2019statistics2}
\end{figure}

What is an appropriate price of an asset? Financial assets, which are contracts over other assets, are often homogeneous and standardized. Intuitively their prices should mainly depend on investors' preferences over the distributions of future returns. Investors have different preferences.  Risk appetite, regulatory requirements, investment horizons and business concerns differentiates them. Secondly, history has showed that predicting the future is difficult and, as a corollary, detailed prediction of price behavior should be also hard. Without auguries, the derivation of exact probabilities is impossible.

According to \textcite{cochrane2009asset}, "asset pricing theory tries to understand the prices or values of claims to uncertain payments (p. xiii)". Asset pricing theory has two main approaches, absolute and relative pricing. Absolute pricing tries to model sources of economic risks and/or underlying preferences, and derive prices from these. The canonical examples are CAPM and other equilibrium models. In contrast, relative pricing does not try the model the whole investment universe. In relative prices, some asset prices are exogenous and other assets are priced relative to these. Black-Scholes--model (\cite{blackscholes1973pricing}) is canonical example of this approach. The demarcation of these approaches is not clear-cut. CAPM assumes the equilibrium prices as given and Black-Scholes--model makes a fundamental assumption about the distribution of asset returns. (\cite[pp. xiii--xiv]{cochrane2009asset})

The relative pricing approach is often called as arbitrage pricing methodology. The common theme in these models is that we make an assumptions about the underlying distribution of the exogenous prices. If the market is arbitrage-free, then these exogenous prices contain meaningful information about probabilities for different outcomes probability distribution. Thus we do not try to derive actual probability distribution, but we construct a new probability measure, which is commonly called as the equivalent martingale measure (EMM).  These implied distributions are then used to construct replicating portfolios. By a construction of a perfect hedge, preferences do not affect the price of a replicating portfolio.

The seed of the arbitrage pricing theory is usually attributed to the thesis by \textcite{bachelier1900theorie}. But the research by \textcite{jovanoviclegall2001does} suggest that Bacheliers work was antecedent by another French \textcite{regnault1863calcul}. The insight of these early pioneers was not try to outguess the market but the idea was model the price movements by Brownian motion\footnote{The problem with Brownian motion is that with non-zero probability the price will be negative given enough time. A more valid approach is to use geometric Brownian motion which do not have this problem. But geometric Brownian motion is still continous so it will not model the jumps in the price process.} and use the distribution to price the derivative contract. Although the work of Bachelier was temporarily forgotten, it resurfaced in the 1950's and influenced a host of work \parencite{samuelson1973mathematics}. Thus Regnault and Bachelier can be also seen as the forefathers of the efficient-market hypothesis which was largely developed in modern sense by \textcite{fama1965behavior} and \textcite{samuelson1965proof}.

These early modern attempts to price options were not satisfactory even to the authors who derived them\footnote{See, for example, the lamentation in \textcite{samuelson1965rational}.} until the seminal work by \textcite{blackscholes1973pricing}. The major insight in this and subsequent work was that, in certain idealized frictionless market model, the cash flow of the option could be perfectly replicated by trading the underlying stock and a risk-free bond. Since there was no practical difference in executing this trading strategy and owning the option, there was a unique no-arbitrage price for the option. This price depends neither the risk aversion of the inverstor nor his views of the market (expect for the volatility of stock price).

The mathematical formulation in \textcite{blackscholes1973pricing} was lacking. According to \textcite[p. 129]{musielarutkowski2005martingale}, it was \textcite{bergman1982pricing} who first noted that the trading strategy used by \textcite{blackscholes1973pricing} was neither risk-free nor self-financing. The modern arbitrage asset pricing theory was formalized by \textcite{harrisonkreps1979martingales} in discrete time and \textcite{harrisonpliska1981martingales} in continuous time (see also \textcite{harrisonpliska1983stochastic}). Several authors have expanded these works and \textcite{delbaenschachermayer1998fundamental} (and references within) present one version of the theory in very general a semi-martingale setting.

According to \textcite{bis2019statistics}, the outstanding amount of over the counter derivatives was over 598 trillion USD. However, since

Figures \ref{fig:bis2019statistics} and \ref{fig:bis2019statistics2}

\subsection{Arbitrage-free interest rate models}

Interest is the time value of money. A vast literature exist to explain the mechanics driving the term-structure of interest rates. Short-rate modeling began under this framework and was based on macro-economical argumentation. According to \textcite[p. 161]{duffie2010dynamic}, the earliest example of markovian term-structure model is by \textcite{pye1966markov}. His approach influenced \textcite{merton1974pricing}, a paper which contains an example of gaussian short-rate model. The first relevant short-rate model is the famous Va\v{s}\'{i}\v{c}ek--model by \textcite{vasicek1977equilibrium}. Another widely used short-rate model is CIR--model by \textcite{coxingersollross1985theory}. Since the original formulation of these models are based on economic equilibrium argumentation, they are often called either equilibrium or fundamental models and risk-preferences have to be explicitly formulated and they influences prices. Although fundamental models may have economically sound argumentation, their basic problem is practical impossibility to fit them to the observed interest-rate structure.

These early models can be also cast in arbitrage pricing framework, but we lose the economic justification behind the interest rate process. Arbitrage-free short-rate modeling just assumes that the interest rates are generated by a given exogenous stochastic process, a short-rate. This rate is mathematically modeled by an It\'{o}-process. Observed rates are then stochastic integrals of this short rate. By this approach, pricing of interest rate derivatives leads to solving stochastic differential equations. When the short-rate model has constant parameters, then the observed interest-rate structure may not be matched. \textcite{ho1986term} introduced a short-rate model with time-varying parameter, which can be chosen to fit the observed term-structure. Va\v{s}\'{i}\v{c}ek--model was extended with time-varying parameters in  \textcite{hull1990pricing}.

Since short-rate models capture only a point, they have hard time capturing the complex dynamics of the term-structures. For example, we later show that in single-factor affine term-structure models rates of different maturities are perfectly correlated. An alternative to the short-rate approach is to directly model the entire term structure of interest rates. An early successor was HJM--framework  (introduced in \cite{HJM1990bondpricing} and \cite{HJM1992bondpricing}), which models the entire forward rate process. It should be noted Ho-Lee and Hull-White models can be cast as special cases of HJM model. A major problem for general HJM models is that these are not necessary markovian (\cite{ritchken1995volatility}).

Nowadays so called market models are widely used in pricing. According to \textcite[p. 182]{wu2009interest}, traders had assumed that LIBOR and swap rates were log-normal processes and used Black's formula\footnote{See \textcite{black1976pricing} and section \ref{blackformula}.} to quote volatility when pricing caps and swaptions since the early 1990's. Theoretical justification for these practices were finally found in 1997 by a series of articles by \cite{bracegatarek1997market}, \textcite{miltersen1997closed} and \textcite{jamshidian1997libor}. Market models use LIBOR (or other market interest rates) as the fundamental objects and assume that they can be modeled as log-normal processes. Thus they are often called as LIBOR models. The main feature of market models is that cap and swaption pricing is given by Black's formula and they can be made to fit given term-structure and volatility structures. Since the LIBOR rates follow log-normal rates, these models have to be extended to handly negative interest rate. This is often done by modeling a shifted log-normal process. A popular extension of market model is SABR volatility model by \textcite{hagan2002managing}.

The global financial crisis of 2007-08 has had a major impact on interest-rate modeling. One of the significant additions is the multi-curve framework (\textcite{mercurio2009interest}). Before the crisis, the spread between overnight indexed swap (OIS) and LIBOR curves were minimal and LIBOR curve was both the discounting and forward rate generating curve. During the crisis, this spread widened dramatically and afterwards it has been necessary the model these curves separately.

\subsection{Intensity based modeling of credit risk}

An early example of credit risk modeling is \textcite{merton1974pricing}, which uses Black-Scholes--model to price corporate debt subject to credit risk. In Merton's model corporate assets are assumed to follow geometric brownian motion and the corporate debt consists of single zero-coupon bond. Now the equity can be seen as a call option on corporate assets with the strike price of the face value of a bond at the maturity. Thus the equity price is given by the Black-Scholes formula and the bond price is the difference of asset and equity values. \textcite{merton1974pricing} is extended by proprietary Moody's KMV model.

An alternative to the structural credit models are dynamic models. One approach is to use so called intensity based modeling of default. In this framework, the default time is a stopping time with a intensity process\footnote{See chapter \ref{chap:intensity} and section \ref{sec:stoppingtime}.}. This intensity process may be influenced by properties of economy or the underlying entity. In the arbitrage-free settings intensity may be modeled by either Poisson or Cox processes. This approach was pioneered by \textcite{artzner1995default}, \textcite{jarrow1995pricing} and \textcite{lando1998cox}.

The popularity intensity based modeling is due to synergies with short-rate models of interest rates. When set-up correctly, the default intensity process is the credit spread process. Then the pricing of debt and related instruments can be made using the machinery developed for the short-rate processes. \textcite{schonbucher2001libor} has extended market models to cover credit intensity modeling.

After the global financial crisis of 2007-08, the credit risk modeling has gained importance. For example, Basel III framework requires that the prices of unsecured derivative positions has to be corrected with CVA\footnote{CVA is credit value adjustment and pricing has also account DVA, debit value adjustment.}, which accounts for the counterparty credit risk (\cite{basel2015cva}).

\section{Overview of the thesis}

The purpose of this thesis is to have a rough overview arbitrage-free pricing methodology and affine short-rate processes used in interest rate modeling and credit risk. Although short-rate models have been eclipsed by market models, they still have their uses in risk management, portfolio management and scenario planning.

As the short-rate models were developed before global financial crisis of 2007-08, we test how well they can be fitted to the post-crisis interest rate data. We also try to test calibrate a combined short-rate and credit spread model to post-crisis bond price data.

\subsubsection*{Chapter \ref{chap:instuments}}

Chapter \ref{chap:instuments} gives a basic overview of the common interest rates and financial instruments.

\subsubsection*{Chapter \ref{chap:arbitrage}}

Chapter \ref{chap:arbitrage} gives a very brief introduction to arbitrage pricing theory. We first develop discrete one period model in order to highlight the basic concepts of arbitrage pricing such as justification behind martingale measures and laws of asset pricing. This treatment is based on \textcite[pp. 5--34]{bjork2004arbitrage}. All the proofs are detailed as we feel that they give insight to martingale measures.

After that the arbitrage pricing in continuous markets is overviewed. This treatment is not rigorous. Measure theoretical justifications and arguments are simply omitted although some basic results are  presented in Appendix \ref{chap:math}.

\added]{While not strictly necessary for the empirical work in this thesis, the arbitrage pricing theory is essential for understanding the peculiarities in interest rate and credit spread modeling.}

\subsubsection*{Chapter \ref{chap:shortrate}}

Chapter \ref{chap:shortrate} starts with the derivation of term-structure equation and the overview of the fundamental and preference-free models. We have a brief overview of the single-factor Va\v{s}\'{i}\v{c}ek and CIR--models.

We also cover multi-factor affine $A(M,N)$--models, which are a combination of $M$ Va\v{s}\'{i}\v{c}ek and $N-M$ CIR--models. The Gaussian processes can be chosen to be correlated but square-root processes has to be uncorrelated. The theory of affine term-structure models was developed for example in \textcite{brown1994interest}, \textcite{duffie1994multi} and \textcite{duffiekan1996yield}. The main advantage of these affine models is that they have analytical bond prices which make calibration by term-structure easy. By dynamic extension shifting\footnote{See \textcite{brigomercurio2001deterministic} and section \ref{sec:dynamicextension}.}, we even could make sure that term-structures fit perfectly.
Many of these models also have analytical bond option prices, which allows us to price caps and floors and for single factor models, this allows also easy pricing of swaptions. Therefore they can be easily fit to volatility structures also. Even if the model does not have a bond option pricing formula, we may use Fourier transformations to have semi-analytical pricing of bond options\footnote{See \textcite{heston1993closed} and section \ref{chap:fourier}.}.

\subsubsection*{Chapter \ref{chap:intensity}}

Chapter \ref{chap:intensity} is an overview of the basic interest rates and financial instruments. For default modeling based on a Cox process\footnote{Cox process is also called as doubly stochastic process, since in addition to the stochastic stopping time, the time dependent intensity process is also stochastic.}. We show that in this setting the pricing of defaultable zero-coupon bonds can be made using the machinery of Chapter \ref{chap:shortrate}. We develop pricing formulas bonds and credit default swaps using different assumptions about the default.

\subsubsection*{Chapter \ref{chap:empirical}}

Chapter \ref{chap:empirical} presents the methods and results from the empirical work done in this thesis. We test some of the methods develop in earlier chapters. In order to find suitable initial starting value for descending optimization algorithm, we tried to scan the problem space with a differential evolution (DE) algorithm. Due to computational limitations, the sample sizes were small compared to the dimension of the problem space. As such, the algorithm did not produce consistent results.

Simple affine models were fitted to various interest rate and yield curves with and without default risk. Fittings of these curves proved to be challenging for the models as the maturities ranged from overnight rates (or 6-month rates) to 30-year rates. No model provided satisfactory fitting in every case. Fitting done by models with credit risk component proved bad overall. However, due to the problems with optimization algorithm, we can not decisively rule that the models will provide bad fits with the used data.

Dynamic fitting of Euribor rates ranging from 1-week rate to 1-year rate was also attempted to single factor models. Depending on the time-period, the fits ranges from horrible to rather satisfactory. As the short end of the rate curve has been rather flat after 2014, calibrated curves had very little errors.

Due to data and computational limitations, fitting to the volatility structures was not attempted.

The code with the Jupyter notebook used in analysis can be found at \url{https://github.com/mrytty/gradu-public} (\textcite{rytty}).

\subsubsection*{Appendices \ref{chap:math} and \ref{chap:charts}}

Appendix \ref{chap:math} is a brief review of mathematical machinery and methods needed in the earlier chapters.

Appendix \ref{chap:charts} presents some additional data charts and tables from the empirical work.


% -----------------------------*- LaTeX -*------------------------------
\documentclass[12pt]{report}
\usepackage{scribe_hgen486}
\usepackage{tikz}
\usetikzlibrary{fit,positioning}
\begin{document}

\scribe{Chris Stamper}		% required
\lecturenumber{10}			% required, must be a number
\lecturedate{February 4}		% required, omit year
\lecturer{John Novembre}

\maketitle

% please leave this comment
\framebox[.95\textwidth]{\parbox{.93\textwidth}{ {{\bf Note:}} These
lecture notes are still rough, and have only have been mildly
proofread.  }}
\vspace*{.1in}

% feel free to delete content below this line
% ----------------------------------------------------------------------

\section{Hidden Markov Models}

\subsection{Hidden Markov Models Introduction}
Hidden Markov Models have a variety of applications and are widley used in many disparate fields. One common application is their use
in speech recognition. (Please see Lawrence Rabiner's 'A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition')

\subsection{Uses of Hidden Markov Models}
Hidden Markov Models are perfect for an underlying Markov Model with noisy data.
\vspace{10 mm}
\newcommand{\HMM}[1][]{
	\begin{tikzpicture}[#1]
	\centering
	\tikzstyle{main}=[circle, minimum size = 10mm, thick, draw =black!80, node distance = 12mm]
	\tikzstyle{connect}=[-latex, thick]
	\node[main, fill = white!100] (Q1) [label=below:$Q_1$] { };
	\node[main] (Q2) [right=of Q1,label=below:$Q_2$] { };
	\node[main] (Q3) [right=of Q2,label=below:$Q_3$] { };
	\node[main] (Q4) [right=of Q3,label=below:$Q_4$] { };
	\node[main, fill = black!10] (O1) [above=of Q1,label=left:$O_1$] { };
	\node[main, fill = black!10] (O2) [above=of Q2,label=left:$O_2$] { };
	\node[main, fill = black!10] (O3) [above=of Q3,label=left:$O_3$] { };
	\node[main, fill = black!10] (O4) [above=of Q4,label=left:$O_4$] { };
	\path (Q1) edge [connect] (Q2)
        (Q2) edge [connect] (Q3)
        (Q3) edge [connect] (Q4)
        (Q1) edge [connect] (O1)
        (Q2) edge [connect] (O2)
        (Q3) edge [connect] (O3)
        (Q4) edge [connect] (O4);
	\end{tikzpicture}}

\HMM{}
\vspace{10 mm}

Meaning that if one conditioned on $Q_3$ the observation would only depend on it and nothing else.

\subsection{Basic Structure of Hidden Markov Models}

The basic structure of a Hidden Markov Model is:

\begin{itemize}
	\item A Transition Probability Matrix: ($A=\{a_{ij}, i=1, ...N, j=1 ...N\}$) with
	\vspace{5 mm}
	\begin{equation}
	a_ij = P(q_t+1 = S_j |q_t = S_i), \hspace {3 mm} 1 \leq i,j \leq N, 1 \leq t \leq T - 1.
	\end{equation}
	\item An Emission Probability Matrix: $B_{NXM}$ is a matrix with all $b_j(i)$
	\vspace{5 mm}
	\begin{equation}
	P(O_t = i|Q_t=j)=b_j(i), \hspace {3 mm} 1 \leq j \leq N, 1 \leq i \leq M, 1 \leq t \leq T.
	\end{equation}
	\item An Initial State Distribution: $\pi = \{\pi_i, i=1 ..., N\}$ or with
	\vspace{5 mm}
	\begin{equation}
	\pi_i = P(q_1 = S_i), \hspace {3 mm} 1 \leq i \leq N.
	\end{equation}
\end{itemize}

\vspace {5 mm}

Which means there are three main problems for Hidden Markov Models.
\begin{enumerate}
 \item $P(O|\lambda)$: How do we compute efficiently?
 \item Given $O$ and $\lambda$, what is the most probable sequence, $Q$.
 \item How can we estimate $\lambda$? AKA How can we find a $rp_{max} \lambda P(O|\lambda)$?
\end{enumerate}

\section{Algorithms for solving the problmes of Hidden Markov Models}

\subsection{Forward Algorithm}

For problem 1 of HMMs we need a way to solve
\[P(O|\lambda) = \sum_Q P(O, Q|\lambda)\]

\vspace {5 mm}

Which we can accomplish by using the forward algorithm.
\begin{itemize}
	\item Initialize
	\begin{equation}
	\alpha_1(i) = \pi_ib_i(O_1) = P (O, Q_1)
	\end{equation}
	\item Induction
	\begin{equation}
	\alpha_{t+1}(j) = [\sum_{i=1}^N \alpha_t(i)a_{ij}]b_j(O_{t+1}), \hspace{3 mm} 1 \leq t \leq T-1 \hspace{2 mm} and \hspace{2 mm} 1 \leq j \leq N
	\end{equation}
	\item Termination
	\begin{equation}
	P(O|\lambda) = \sum_j\alpha_T(j)
	\end{equation}
\end{itemize}

\subsection{Viterbi Algorithm}

For Problem 2 we need a way for the argmax of $P(O,Q|\lambda)$. In other words we need a way to consider all possible $Q$ and find the $Q$ that is maximal.

\newcommand{\latice}[2][]{
	\begin{tikzpicture}[#2]
	\centering
	\tikzstyle{main}=[circle, minimum size = 6mm, node distance = 5mm]
	\tikzstyle{connect}=[-latex, thin]
	\node[main, fill = black!100] (1) [label=left:$1$] { };
	\node[main, fill = black!100] (2) [below=of 1,label=left:$2$] { };
	\node[main, fill = black!100] (3) [below=of 2,label=left:$3$, label=below:$t_1$] { };
	\node[main, fill = black!100] (4) [right=of 1] { };
	\node[main, fill = black!100] (5) [below=of 4] { };
	\node[main, fill = black!100] (6) [below=of 5, label=below:$t_2$] { };
	\node[main, fill = black!100] (7) [right=of 4] { };
	\node[main, fill = black!100] (8) [below=of 7] { };
	\node[main, fill = black!100] (9) [below=of 8, label=below:$t_3$] { };
	\path (1) edge [connect] (6);
	\path (2) edge [connect] (6);
	\path (3) edge [connect] (6);
	\path (4) edge [connect] (7);
	\path (4) edge [connect] (9);
	\path (4) edge [connect] (9);
	\path (5) edge [connect] (7);
	\path (5) edge [connect] (8);
	\path (5) edge [connect] (9);
	\path (6) edge [connect] (7);
	\path (6) edge [connect] (8);
	\path (6) edge [connect] (9);
\end{tikzpicture}}

\vspace{2 mm}
To visualize this we can imagine a lattice graph:
\newline
\vspace{5 mm}
\latice{}
\vspace {1 mm}\\
In which we need to take the maximum of the three paths coming from some $i$ at time 2 and going to some $j$ at time 3. \\

To do this we can make use of the Viterbi Algortithm.
\\
In which we have:
\begin{itemize}
	\item An array defined as: $\psi_{t+1}j = argmax_i[\delta_t(i)a_{ij}], \hspace{3 mm} 1 \leq j \leq N, 1 \leq t \leq T-1$
	\item An auxillary variable, $\delta$: $\delta_t(i) = max_{q_1q_2...q_{t-1}}P\{q_1,q_2,...q_{t-1}=i,O_1,O_2,...,O_{t-1}|\lambda\}$
\end{itemize}
Which we can use in the algortihm.
\begin{itemize}
	\item Initialize
	\begin{equation}
	\begin{split}
	\delta_1 (i) \leftarrow \pi_ib_i(O_1), \hspace{3 mm} 1 \leq i \leq N \hspace{3 mm} \\
	\psi_1 (i) \leftarrow 0, \hspace{3 mm} 1 \leq i \leq N \\
	t \leftarrow 1
	\end{split}
	\end{equation}
	\item Repeat
	\begin{equation}
	\begin{split}
	\psi_{t+1}(j) \leftarrow argmax_i[\delta_t(i)a_(ij)], \hspace{3 mm} 1 \leq j \leq N \\
	\delta_{t+1}(j) \leftarrow \delta_t(\psi_{t+1}(j))a_{\psi_{t+1}(j),j}b_j(O_{t+1}), \hspace{3 mm} 1 \leq j \leq N \\
	t \leftarrow t + 1
	\end{split}
	\end{equation}
	\item Until
	\begin{equation}
	\begin{split}
	t = T \\
	P^* \leftarrow max_{1 \leq i \leq N}[\delta_T(i)] \\
	i_T^* \leftarrow argmax_{1 \leq i \leq N}[\delta_T(i)] \\
	\text{State Sequence Backtracking:} \\
	q_T^* \leftarrow S_{i_T^*} \\
	t \leftarrow T
	\end{split}
	\end{equation}
	\item Repeat
	\begin{equation}
	\begin{split}
	i_{t-1}^* \leftarrow \psi_t(i_t^*) \\
	q_{t-1}^* \leftarrow S_{i_{t-1}^*} \\
	t \leftarrow t - 1 \\
	\text{until} \\
	t = 1 \\
	Q^* \leftarrow q_1^*,...q_T^*
	\end{split}
	\end{equation}
\end{itemize}

\subsection{Forward-Backward Algorithm}
To solve the final problem, efficiently learning the parameters of a HMM, we can make use of a backward recursive procedure. \\
Our goal is that we want the backward variable, $B_t(i)$, or: \\
\vspace{3 mm}
$B_t(i) = P(O_{t+1},...,O_T|Q_t=j,\lambda)$ \\
\vspace{3 mm}
such that \\ $\alpha_t(j)B_t(j) = P(O_1,...,O_T|Q_t=j,\lambda)$
$\propto P(Q_t=j|O,\lambda)$ \\
\vspace{3 mm}
and \\
\vspace{3 mm}
$P(Q_t=j|O,\lambda) = \frac{\alpha_t(j)B_t(j)}{\sum_i\alpha_t(i)B_t(i)} = \lambda_t(j)$ \\

To compute this we can use the Backwards Algorithm, AKA the Forward-Backward Algorithm:

\begin{itemize}
	\item Initialize
	\begin{equation}
	B_T(i)=1
	\end{equation}
	\item Induction
	\begin{equation}
	B_t(i) = \sum_{j=1}^Na_{ij}b_j(O_t)B_t(j), \hspace{3 mm} t=T-1, T-2,..., 1 \hspace{1 mm} \text{and} \hspace{1 mm} 1 \leq i \leq N
	\end{equation}
	\item Continue unitl
	\begin{equation}
	P(Q_t=j|O,\lambda) = \frac{\alpha_t(j)B_t(j)}{\sum_i\alpha_t(i)B_t(i)} = \lambda_t(j)
	\end{equation}
\end{itemize}
\end{document}


\subsection{Thermodynamic Processes}
This subsection is devoted to all the different types of thermodynamic processes that we talk about in this course. Note that in all of these processes, we have that some quantity is held constant/is zero (e.g. volume, pressure, temperature, or no heat flow). Nearly all processes you will see in this course and in your exams will be one of these four types, but don't assume it will be unless you can justify it. Also note that there are quite a few derivations in this section (e.g. deriving the work done in an isothermal process). This is mainly to aid your understanding, and for exams, you can just use the final results to calculate whatever is relevant without worrying about the derivation.
\input{OneLaw/isochoric}
\input{OneLaw/isobaric}
\input{OneLaw/isothermal}
\input{OneLaw/adiabatic}


\section{Conclusions}
\label{sec:conclusions}

We introduced the notion of context-free grammar in Marciani Normal Form (MNF).
We proved that a context-free grammar in such a form always generates a regular
language.
Such a demonstration states the semidecidability of the CF Regularity Problem,
which is the problem of saying whether or not a context-free grammar generates a
regular language.
We gave representative examples of applying Marciani's Rule, showing the
simplicity of determining the solution for the CF Regularity Problem.