HMM

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{HMM(}\VariableTok{self}\NormalTok{, num_states, data_dim)}
\end{Highlighting}
\end{Shaded}

A Hidden Markov Models class with Gaussians emission distributions.

fit

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{HMM.fit(}\VariableTok{self}\NormalTok{, data, max_steps}\OperatorTok{=}\DecValTok{100}\NormalTok{, batch_size}\OperatorTok{=}\VariableTok{None}\NormalTok{, TOL}\OperatorTok{=}\FloatTok{0.01}\NormalTok{, min_var}\OperatorTok{=}\FloatTok{0.1}\NormalTok{, num_runs}\OperatorTok{=}\DecValTok{1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Implements the Baum-Welch algorithm.

\begin{verbatim}
Args:
  data: A numpy array with rank two or three.
  max_steps: The maximum number of steps.
  batch_size: None or the number of batch size.
  TOL: The tolerance for stoping training process.

Returns:
  True if converged, False otherwise.
\end{verbatim}

posterior

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{HMM.posterior(}\VariableTok{self}\NormalTok{, data)}
\end{Highlighting}
\end{Shaded}

Runs the forward-backward algorithm in order to calculate the log-scale
posterior probabilities.

\begin{verbatim}
Args:
  data: A numpy array with rank two or three.

Returns:
  A numpy array that contains the log-scale posterior probabilities of
  each time serie in data.
\end{verbatim}

run\_viterbi

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{HMM.run_viterbi(}\VariableTok{self}\NormalTok{, data)}
\end{Highlighting}
\end{Shaded}

Implements the viterbi algorithm. (I am not sure that it works properly)

\begin{verbatim}
Args:
  data: A numpy array with rank two or three.

Returns:
  The most probable state path.
\end{verbatim}

generate

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{HMM.generate(}\VariableTok{self}\NormalTok{, num_samples)}
\end{Highlighting}
\end{Shaded}

Generate simulated data from the model.

\begin{verbatim}
Args:
  num_samples: The number of samples of the generated data.

Returns:
  The generated data.
\end{verbatim}

\hypertarget{installation-using-pip}{%
\subsection{installation using pip:}\label{installation-using-pip}}

pip install git +https://github.com/kesmarag/ml-utils.git

pip install git+https://github.com/kesmarag/ml-hmm.git

\hypertarget{usage}{%
\subsection{usage}\label{usage}}

\begin{Shaded}
\begin{Highlighting}[]
\ImportTok{import}\NormalTok{ numpy }\ImportTok{as}\NormalTok{ np}
\ImportTok{from}\NormalTok{ kesmarag.ml.hmm }\ImportTok{import}\NormalTok{ HMM}

\CommentTok{# create a random data set with 3 time series.}
\NormalTok{data }\OperatorTok{=}\NormalTok{ np.random.randn(}\DecValTok{3}\NormalTok{, }\DecValTok{100}\NormalTok{, }\DecValTok{2}\NormalTok{)}

\CommentTok{# create a model with 10 hidden states.}
\NormalTok{model }\OperatorTok{=}\NormalTok{ HMM(}\DecValTok{10}\NormalTok{, }\DecValTok{2}\NormalTok{)}

\CommentTok{# fit the model}
\NormalTok{model.fit(data)}

\CommentTok{# print the trained model}
\BuiltInTok{print}\NormalTok{(model)}

\CommentTok{# Good luck}
\end{Highlighting}
\end{Shaded}


\section{State of the art}
\label{sec:stateart}

To combine optimal investment and operational decision, many people have investigated the use of large multi-time-steps optimization models~\cite{zonal_int,Baker2012OptimalIO,paolone}.
The integration of uncertainty in optimization is usually done either in a robust way~\cite{LorcaSun,adaptative,BM2} or using stochastic optimization~\cite{Oren}.
The main challenge when using optimization for planning the network is that they usually quickly become intractable.
To decrease the computation complexity, linear and convex relaxations have been proposed~\cite{Farivar_Relax1,Farivar_Relax2,BM1} where computational cost is considerably reduced.
The drawback is that the solutions proposed are not necessary feasible anymore.
Feasibility can be recovered by solving a sequence of convex problems~\cite{Nali}.

More recently, Markov Decision Processes (MDP) and Reinforcement Learning (RL) have been proposed to deal with uncertainty in optimal planning of electricity grids.
A recent survey reviews the use of RL in power system operations and propose new perspectives for developing this field of research~\cite{RLSurvey}.
\emph{The goal of this project is thus to make a comparison between classic optimization
models versus more recent MDP and RL implementations, and to analyze what trade-offs
have to be made in either cases.}

Q-Learning has been successfully applied for congestion management in~\cite{ZARRABIAN2016179}.
In~\cite{fuzzy}, a controller for microgrids is developed by combining deep learning and reinforcement learning ideas.
The idea is that the reinforcement signal is used alongside the backpropagation of error to tune the output layer weights of the controller.
Still in the domain of congestion control, a distributed Q-learning algorithm to find the optimal reactive power dispatch have been applied in~\cite{volt_control}.
The approach seems to be good to quickly reach a good sub-optimal solution.
RL have also been used to find an optimal solution to the battery management problem in microgrid~\cite{Battery}.
They model the problem as an MDP by defining the battery State of Charge as the action variable.
The algorithm proposed is a batch RL algorithm.

Finally, in~\cite{Gemine} the problem of ANM for the DSO grid with large renewable generation is explored.
In addition of describing a multi-time-steps optimization model and defining new benchmark, they cast the problem as a MDP.
In this model, the control actions are the active power curtailment decided by the DSO at each time step as well as the corresponding reactive power level.
It is also possible to decide on the state (on or off) of some predefined flexible loads.
The uncertainty comes from the solar irradiance and the wind speed at each time steps.
Past realizations of uncertainty are added in the state so that accurate transition probabilities can be obtained and the problem can be cast as Markov.
In the present work, we will use the model developed in~\cite{Gemine} and extend it to incorporate an RL method so that the model can be used when the transition probabilities are not known beforehand.


\newpage

\section{Selective functors}\label{sec-selective}

\begin{figure}
\begin{minted}[fontsize=\small]{haskell}
class Functor f where
    fmap :: (a -> b) -> f a -> f b

-- An infix synonym for fmap
(<$>) :: Functor f => (a -> b) -> f a -> f b       -- (<$>) is pronounced as "map"

class Functor f => Applicative f where
    pure  :: a -> f a
    (<*>) :: f (a -> b) -> f a -> f b              -- (<*>) is pronounced as "apply"

-- A variant of <*> with the arguments reversed
(<**>) :: Applicative f => f a -> f (a -> b) -> f b

class Applicative f => Selective f where
    select :: f (Either a b) -> f (a -> b) -> f b

-- An infix synonym for select
(<*?) :: f (Either a b) -> f (a -> b) -> f b       -- (<*?) is pronounced as "select"

class Selective f => Monad f where
    return :: a -> f a
    (>>=)  :: f a -> (a -> f b) -> f b             -- (>>=) is pronounced as "bind"
\end{minted}
\caption{The proposed type class hierarchy, where \hs{Functor}, \hs{Applicative}
and \hs{Monad} are standard Haskell type classes, and \hs{Selective} is
a new intermediate abstraction introduced between \hs{Applicative} and
\hs{Monad}.}\label{fig-types}
\end{figure}

See Fig.~\ref{fig-types}.

It may be illuminating to compare the following type signatures:

\begin{minted}[xleftmargin=10pt]{haskell}
(<**>) :: Applicative f => f a            -> f (a -> b) -> f b
select :: Selective   f => f (Either a b) -> f (a -> b) -> f b
(>>=)  :: Monad       f => f a            -> (a -> f b) -> f b
\end{minted}

\subsection{Basic examples}

Const

Validation


\section{Polynomial Time Reduction}

%%%%%%%%%%
\begin{frame}{Reduction}
  \begin{alertblock}{Why reduction?}
    To relate complexities of individual problems.
  \end{alertblock}

  \vspace{0.40cm}
  \begin{definition}[Reduction.]
    \fignocaption{width = 0.65\textwidth}{fig/reduction-algs.png}
  \end{definition}
\end{frame}
%%%%%%%%%%
\begin{frame}{Polynomial reduction}
  \begin{definition}[Polynomial reduction.]
    $f(I)$ in polynomial time.
  \end{definition}

  \begin{theorem}
    Transitivity: $A \le_{P} B, B \le_{P} C \Rightarrow A \le_{P} C.$
  \end{theorem}

  \begin{proof}
    \begin{itemize}
      \item $A \le_{P} B \Rightarrow \exists \text{poly. } f: a \in A \iff f(a) \in B$
      \item $B \le_{P} C \Rightarrow \exists \text{poly. } g: b \in B \iff g(b) \in C$
      \item $a \in A \iff \text{poly. } g(f(a)) \in C \Rightarrow A \le_{P} C$
    \end{itemize}
  \end{proof}
\end{frame}
%%%%%%%%%%
\begin{frame}{Polynomial reduction}
  \begin{theorem}
    $A \le_{P} B, B \in P \Rightarrow A \in P.$
  \end{theorem}

  \begin{proof}
    \begin{itemize}
      \item $A \le_{P} B \Rightarrow \exists \text{poly. } f: a \in A \iff f(a) \in B$
      \item $B \in \text{P} \Rightarrow \exists \text{poly. } g: g \text{ solves } B$
      \item $\text{poly. } g(f(\cdot)) \text{ solves } A$
    \end{itemize}
  \end{proof}
\end{frame}
%%%%%%%%%%


\input{permve-ntnu-latex-assignment.tex}

\title{
\normalfont \normalsize
\textsc{Norwegian University of Science and Technology\\IT3105 -- Artificial Intelligence Programming}
\horrule{0.5pt} \\[0.4cm]
\huge Module 3:\\ Combining Best-First Search and Constraint-Satisfaction to Solve Nonograms\\
\horrule{2pt} \\[0.5cm]
}

\author{Per Magnus Veierland\\permve@stud.ntnu.no}

\date{\normalsize\today}

\newacro{CSP}{Constraint Satisfaction Problem}
\newacro{GAC}{General Arc Constraint}

\begin{document}

\maketitle

\section*{Variables}
The formulation chosen to solve the problem is the one suggested in the assignment where one variable is used to represent each row; and one variable to represent each column. Each value in a variable's domain is represented as an integer, where each bit represents whether the cell is filled or unfilled. This allows a compact representation where many operations can be completed efficiently, but limits the width and height of the nonogram to $64 \times 64$ cells.

\section*{Domains}
To allow the domain reduction between rows and columns as suggested in the assignment the input row and column specifications must first be converted into valid entries in their respective domains.

When creating the initial domains for each row and column variable it is important to efficiently generate the domain values. One way this can be done is to view the area before the first segment in a line as a bucket, the area between each segment as a bucket, and the area after the last segment as a bucket. When all of the segments are placed as close to the start as possible, with one space between each segment; the remaining spaces after the last segment can be considered ``free'' since they can be arbitrarily split between the buckets. When a bucket between two segment has 0 entries; there is a single space between the two segments -- and when a bucket between two segments has one entry there are two spaces between the two segments. Using this formulation the problem is to multichoose the position of $k$ balls in $n$ buckets, which can be done using an easy to formulate ``Stars and bars'' approach.

After generating the initial domains a reduction is performed to remove impossible domain values. If for a column variable all values in its domain agree for a single cell; i.e. they are either all 0 or 1 -- then all values in the domain for the row sharing this cell which disagree with the column cell value are removed. This reduction is performed both from cell domains to row domains and for all cells. The result of the domain reduction can be seen in Table~\ref{table:nonoresults}.

\begin{table}
\centering
\begin{tabular}{rlccccc}
\toprule
& Scenario & Size & Initial domains & Reduced domains & Constraints & Solution length \\
\midrule
0 & \texttt{heart-1}   & $10 \times 10$ &  116 &  94 &  44 & 1 \\
1 & \texttt{cat}       & $ 8 \times  9$ &  158 &  72 &  41 & 0 \\
2 & \texttt{chick}     & $15 \times 15$ & 1113 &  54 &  33 & 0 \\
3 & \texttt{rabbit}    & $20 \times 15$ &  955 & 344 & 165 & 0 \\
4 & \texttt{camel}     & $15 \times 15$ &  755 & 229 & 124 & 0 \\
5 & \texttt{telephone} & $15 \times 15$ & 2456 & 138 &  59 & 0 \\
6 & \texttt{sailboat}  & $20 \times 20$ & 3389 &  92 &  85 & 0 \\
\bottomrule
\end{tabular}
\caption{Nonogram scenario results}
\label{table:nonoresults}
\end{table}

\section*{Constraints}

The nonogram solver is implemented with one variable for each row and one variable for each column. Initially these variables are initialized with only valid configurations. This means that constraints are only needed to ensure consistent values between row and column variables.

The naive approach is to use a separate constraint for every cell to connect intersecting row and column variables. This can lead to a large number of unnecessary constraints. A simple effort to improve upon this is to only add constraints to cells where the value is not the same in all domains. The effect of this can be seen in Table~\ref{table:nonoresults}. This strategy is still not optimal as there may still be redundant constraints.

\section*{Heuristics}

The heuristics used for the A*-GAC search are unchanged from module 2. The main $h$-value is calculated as a sum of the size of all domains in the network minus 1; such that for a goal state where all domains has a size of 1 the heuristic value will be 0.

When generating successor states; the variable with the smallest domain greater than 1 is chosen.

Given the composite representation where each variable represents a multitude of individual cells it could be beneficial to take this into account when building the heuristic. The software interface could be modified to allow the introduction of a \ac{GAC} problem specific heuristic. However due to very short search paths for all the given problems it was not necessary to implement this.

\section*{Generality of implementation}

The A*-GAC algorithm built for module 2 could be applied to solve nonograms with no changes to its implementation. The following set of methods were created to build a \ac{CSP} formulation of a specific nonogram problem through instantiating and connecting \texttt{vi.csp.Variable} and \texttt{vi.csp.Constraint} objects and collecting them in a \texttt{vi.csp.Network} object:

\begin{itemize}
\item \texttt{build\_problem} -- Builds a \texttt{vi.csp.Network} representation of the input file contents describing a nonogram.
\item \texttt{build\_domain(dimension, specification)} -- Builds the domain with all legal variations of the given specification for the given dimension size.
\item \texttt{multichoose(n, k)} -- Generates all possible ways to place $k$ balls into $n$ buckets. Used by \texttt{build\_domain} to generate all valid domain configurations.
\item \texttt{build\_pattern(spec, configuration)} -- Generates a domain value pattern based on a specification and a configuration. Used by \texttt{build\_domain} to generate domain value patterns.
\item \texttt{build\_constraints(row\_variables, column\_variables, row\_domains, column\_domains, width, height)} -- Builds the constraints needed to ensure consistent values between all row and column variables. Used by \texttt{build\_problem}.
\item \texttt{reduce\_row\_domain(row\_domain, column\_domain, row, column, width, height)} -- Returns a set of column domains based on eliminating impossible configurations. A corresponding method exists for columns. Used by \texttt{build\_problem} to reduce the domain size.
\end{itemize}

The resulting \texttt{vi.csp.Network} object is used to construct a \texttt{vi.search.gac.Problem} object which is the basis for the A*-GAC search. Further details are described in the documentation for module 2.

\end{document}


\chapter{Examples with Weights}

\label{chapt:exampleswithweights}

\section{Introduction}

This chapter, definitely work in progress, presents examples that
use weights.  New examples will be added as they become available.

\section{Weighted Edit Distance for Spelling Correction}

The following script, based on an example by M\r{a}ns Huldén, illustrates how weighted \fst{}s
can be used to make a spelling corrector that returns a set of possible
corrections ranked by likelihood, or even just the best (most likely)
correction, for a misspelled word.

The first phenomenon that we need to model is the degree of dissimilarity between an
observed misspelled word and a correctly spelled possible correction.  For example, the misspelled
word \emph{accomodation} is intuitively very similar to the correct word
\emph{accommodation}, differing in only one missing letter.  Similarly,
\emph{panick}
is very similar to \emph{panic}, differing in having one extra letter.  And
\emph{advertize} is similar to the correctly spelled \emph{advertise}, differing only
in one changed letter. Increasing in dissimilarity, \emph{camoflague} differs from
\emph{camouflage} in two letters, \emph{garentee} also differs from
\emph{guarantee} in
two letters, and \emph{bizness} differs from \emph{business} in three letters.

We can quantify the amount of dissimilarity between two words as the \emph{edit distance},
a count of the number of editing changes, or, perhaps a more sophisticated measure
of the amount of work, required to change one word into the
other.  The changes could include adding a letter, deleting a letter, or changing
one letter into another letter.

Weighted Kleene \fst{}s have weights that are ``costs,'' under the Tropical Semiring, and
the edit distance can be formalized in Kleene as the cost of
transducing one word into another, assigning a cost to deleting a
letter, a cost to adding a letter and a cost to changing one letter into
another letter.  In the Tropical Semiring, the costs associated with multiple changes are simply
added together to yield the total cost.  Mapping a letter to itself is free, involving no cost,
i.e., a neutral cost of 0.0.  Of
course, with
enough editing changes, any word can be changed to any other word,
but intuitively we want to concentrate on the possible corrections that
involve the lowest cost, the fewest edits.  These intuitions will be encoded in what is known
as a \emph{error model} or \emph{edit model}.

Another phenomenon that needs to be modeled is the fact that some words are
simply more common than others in any reasonably sized corpus.  Looking at a misspelled word
in isolation,
if a set of possible corrections having the same edit distance includes a more common word and
	some less
common words, the more common word is more likely to be the best correction.  For
example, the misspelled \emph{frm} differs in only one letter from the possible
corrections \emph{farm}, \emph{firm} and \emph{from}, but \emph{from} is far more common than
the alternatives and is---considered in isolation---more likely to be the intended
word.  These intuitions will be encoded in a unigram \emph{language model}.

We will build our weighted edit-modeling transducer from four main parts.  The first
part is the identity mapping:

\begin{Verbatim}
$ident = . <0.0> ;	// map any symbol to itself
\end{Verbatim}

\noindent
Recall that . (the dot, period or full stop) denotes any symbol or the transducer that maps any
symbol to itself.  Such identity mappings are free, having no cost in
our spell-checking example, so we assign the identity mapping a weight of 0:0.

The second part is

\begin{Verbatim}
$changecost = <7.0> ;
$delete = .:"" $changecost ;  // map a symbol
                              // to the empty string
\end{Verbatim}

\noindent which denotes the transducer that maps any symbol, denoted as . (dot),
downward to the empty string, denoted \texttt{""}.  We arbitrarily assign the
weight of 7.0 to such a deletion, a value that we can adjust later.

The third part is

\begin{Verbatim}
$insert = "":. $changecost ;  // map the empty string
                              // to a symbol
\end{Verbatim}

\noindent which denotes the transducer that maps the empty string downward to any
symbol, inserting a symbol where there was none before.  Such mappings are technically known as
epentheses.  We assign such
insertions the weight of 7.0.

Finally, the fourth part is

\begin{Verbatim}
$swap = ( .:. - . ) $changecost ;
\end{Verbatim}

\noindent
Recall that \texttt{.:.} denotes the transducer that maps any symbol to any
symbol, including itself.  Then \texttt{(~.:.~- .~)} denotes the transducer
that maps any symbol to any other symbol, so \emph{not} including identity mappings.  We
assign such letter swaps the cost of 7.0.

Armed with these definitions, our first draft of the model for transducing between a
misspelled word and a properly spelled word, the \emph{edit model}, is a
sequence of zero or more identity mappings, deletions, insertions or swaps,
in Kleene terms:

\begin{Verbatim}
$editModel = ( $ident | $delete | $insert | $swap )* ;
\end{Verbatim}

We also need a unigram \emph{language model} that encodes the language (i.e., the
set) of properly spelled words that will serve as possible corrections.
Initially, such a model can be constructed
as a simple unweighted union of words,

\begin{Verbatim}
$langModel = a | back | bake | bee | carrot | deer | eye ;
\end{Verbatim}

\noindent
extending it eventually to hundreds of thousands or even millions of words.  For the purposes
of this example, we can simply search the web for posted wordlists, such as the
``Simpsons Frequency Dictionary,''\footnote{\url{http://pastebin.com/anKcMdvk}} which contains the 5000 most frequent words
found in open subtitles from \emph{The Simpsons} television series.  If we download this list, we can, with a
little editing,\footnote{There are some apparently extraneous punctuation letters
	in this list, and for Kleene regular expressions, some vertical bars (``pipes''), digits and punctuation
letters need to be removed or literalized using double quotes or backslashes.} convert it into a useful test model.

\begin{Verbatim}
$langModel = the | you | i | a | to | and | of | it  ... ;
// the full list has 5000 words, some needing editing
//     to compile correctly in Kleene
\end{Verbatim}

\noindent
Then if the misspelled word is \emph{frm}, the language of possible corrections,
\verb!$corr!, is computed as

\begin{Verbatim}
$corr = frm _o_ $editModel _o_ $langModel ;
// FstType: vector, Semiring: standard, 12992 states,
56693 arcs, 2330246 paths, Transducer, Weighted, Closed Sigma
\end{Verbatim}

\noindent
When I run this experiment, using the bare Simpsons wordlist, with my edits, Kleene
informs me that the result \texttt{\$corr} contains 2330246 paths, representing
the fact that, with enough edits, \emph{frm} can be transduced into any of the
5000 words in the language model, in multiple ways.

At this point, we can start to use the weights (costs) to focus on the more
likely corrections, given our edit model and language model.  In the OpenFst
library, the function that prunes an \fsm{} to contain only the best paths (i.e.,
lowest cost paths, in the Tropical Semiring) is called ShortestPath.  That operation
and terminology are exposed in the Kleene function \verb!$^shortestPath($fst, #num=1)!.  For
example, we can limit \verb!$corr! to the best five corrections using

\begin{Verbatim}
$corr = $^shortestPath($^lowerside(frm
                                   _o_
                                   $editModel
                                   _o_
                                   $langModel),
                       5) ;
print $corr ;
fry : 7.0
from : 7.0
firm : 7.0
farm : 7.0
arm : 7.0
\end{Verbatim}

\noindent
Each of the five possible corrections has a weight of 7.0, indicating (in our
current model that assigns a weight of 7.0 to each change) that just one
edit was needed to change \emph{frm} into the correctly
spelled word.

If we ask ShortestPath to return just one result,

\begin{Verbatim}
$corr = $^shortestPath($^lowerside(frm
                                   _o_
                                   $editModel
                                   _o_
                                   $langModel),
                       1) ;
print $corr ;
arm : 7.0
\end{Verbatim}

\noindent
it randomly gives us \emph{arm}, which is as close to \emph{frm} as the
other possibilities, given our edit model,
but intuitively is not the mostly likely correction.

What's missing in this experiment so far is a modeling of the fact that some
words, in the language model, occur more frequently than others.  And when a
misspelled word is considered in isolation, possible corrections that are more
likely should have precedence over those that are less likely.  We
know intuitively that \emph{from}, a common English function word, occurs more often than the
alternatives \emph{farm}, \emph{firm}, \emph{fry} and \emph{arm},  and we can confirm this by looking at actual frequency counts.  Again, we will
model the likelihood of individual words in the language model using cost weights in the Tropical Semiring.

The Simpsons word list, in fact, gives us some additional information that allows
us to at least approximate the probability (and therefore the cost) of each
correct word.  The top of the list looks like this:

\begin{Verbatim}
Rank   Word                           Frequency
1      the                            (107946)
2      you                            (98068)
3      i                              (91502)
4      a                              (79241)
5      to                             (70362)
6      and                            (47916)
7      of                             (42175)
8      it                             (36497)
9      in                             (32503)
10     my                             (32254)
11     that                           (32083)
12     is                             (31533)
13     this                           (29902)
14     me                             (28208)
...
\end{Verbatim}

\noindent
The ``Frequency'' column shows the count of occurrences of each word from the
corpus.  For example, P(the), the probability of a word being \emph{the}, i.e., the probability of
selecting a word from the corpus at random, and having it be \emph{the}, is
computed as the count of occurrences of \emph{the}, here 107946, divided by the
total number of tokens in the corpus, i.e.\@

\begin{Verbatim}
	P(the) = 107946 / TotalTokens
\end{Verbatim}

\noindent
Disappointingly, the Simpsons wordlist doesn't tell us the TotalTokens,
so we are left to do some informed guessing.  In the Brown Corpus, the
most common word in English,
\emph{the}, is said to account for almost 7\% of the tokens.  For now, let's accept
this number as being valid for the Simpsons corpus as well.  If 107946 instances of \emph{the}
represented 7\% of the corpus, then the total number of tokens would be a little over one and half
million.

\begin{Verbatim}
	107946 / TotalTokens = .07

	TotalTokens = 1542085
\end{Verbatim}

\noindent
For our example, let's use the round number of 1,500,000.  The probability of \emph{the}, and some
other common words, can then be estimated as

\begin{Verbatim}
	P(the) = 107946 / 1,500,000 = 0.07196

	P(you) = 98068 / 1,500000 = 0.06538

	P(i) = 91502 / 1,500,000 = 0.06100

	P(a) = 79241 / 1,500,000 = 0.05300
\end{Verbatim}

\noindent
Note that as the frequency of occurrence decreases, the probability value also decreases.
As usual, if the probability of an event, such as the appearance of the word \emph{the} in
running English text, is p,
then the cost, C(the), is computed as $-$log(p).

\begin{Verbatim}
	C(the) = -log(107946 / 1,500,000) = 2.63159

	C(you) = -log(98068 / 1,500,000) = 2.72756

	C(i) = -log(91502 / 1,500,000) = 2.79686

	C(a) = -log(79241 / 1,500,000) = 2.94073
\end{Verbatim}

\noindent
Note that as the frequencies/probabilities decrease, the costs \emph{in}crease.
High probabilities correspond to low costs, and low probabilities correspond to
high costs.

These costs can now be included in an improved \emph{weighted} language model.  If we tediously precompute
the costs, we can simply list the cost for each word, using the usual
angle-bracket syntax.  Here is the Kleene syntax:

\begin{Verbatim}
$langModel =
  the   <2.63159>
| you   <2.72756>
| i     <2.79686>
| a     <2.940731>
... ;
\end{Verbatim}

\noindent
Alternatively, we can let Kleene convert the probabilities into costs using the
\verb!#^prob2c()! function, ``probability to cost,'' which is pre-defined as

\begin{Verbatim}
#^prob2c(#prob) {
    return -#^log(#prob) ;
}
\end{Verbatim}

\noindent
The definition of the improved, weighted language model might then look like this, with
\verb!#TotalTokens! defined as 1500000.0, a floating-point number, so that
when it is used in division, the result is also a float.

\begin{Verbatim}
#TotalTokens = 1500000.0 ;

$langModel =
  the   <#^prob2c(107946 / #TotalTokens)>
| you   <#^prob2c(98068 / #TotalTokens)>
| i     <#^prob2c(91502 / #TotalTokens)>
| a     <#^prob2c(79241 / #TotalTokens)>
... ;
// and so on for all 5000 words in the model
\end{Verbatim}

\noindent
We could easily adjust the value of \verb!#TotalTokens! if we ever get more
precise information about the size of the corpus.

With the improved language model, now weighted, the best correction for \emph{frm} selected by
\verb!$^shortestPath()! is now \emph{from}, which intuitively seems right.

\begin{Verbatim}
$corr = $^shortestPath($^lowerside(frm
                                   _o_
                                   $editModel
                                   _o_
                                   $langModel)) ;
print $corr ;
from : 12.295898
\end{Verbatim}

\noindent
And we can see the \emph{n} best possible corrections by passing an optional numerical
argument to \verb!$^shortestPath()!, e.g.\@ 10:

\begin{Verbatim}
$corr = $^shortestPath($^lowerside(frm
                                   _o_
                                   $editModel
                                   _o_
                                   $langModel),
                       10) ;
print $corr ;
from : 12.295898
from : 15.744141
arm : 15.818359
farm : 16.557617
fry : 17.483398
firm : 17.483398
for : 18.069336
i'm : 18.161133
are : 18.52832
him : 19.44336
\end{Verbatim}

\noindent
In the Tropical Semiring, as in golf, the lower scores are the better scores,
and \verb!$^shortestPath()! chooses the path(s) with the lowest cost(s).

After the weighted language model is defined, the rest of the final script is shown below,
including the definition of the convenience
functions \verb!$^correct()!, which returns an \fsm{},
and \verb!^correctp()!, which simply prints out the results.  Both functions have an
optional numerical second argument, default 1, which controls how many paths are
retained in the shortest-path result, and an optional numerical third argument,
default 7.0, which assigns the
weight for each editing change.

\begin{Verbatim}
$ident = . ;        // identity mapping, no change

//	Three kinds of change

$delete = .:"" ;    // map any symbol to the empty string

$insert = "":. ;    // map the empty string to any symbol

$swap = .:. - . ;   // map one symbol to another symbol
                    // (not to itself)

// Higher change weights (costs) make changes more costly,
//  favoring corrections that look as much as possible
//  like the misspelled word. Lower change weights will
//  tend to favor "corrections" that are very high-frequency
//  words, no matter how high the edit distance between
//  the observed misspelled word and the proposed corrections.

$^editModel(#changecost) {
    return (  $ident <0.0> |
              $delete <#changecost> |
              $insert <#changecost> |
              $swap <#changecost>
           )* ;
}

$^correct($word, #num=1, #changecost = 7.0) {
    return $^shortestPath($^lowerside($word
                                      _o_
                                      $^editModel(#changecost)
                                      _o_
                                      $langModel),
                          #num) ;
}

^correctp($word, #num=1, #changecost=7.0) {
    print "\n" ;
    print $^toString(#num) " correction(s) for " $word ;
    print $^shortestPath($^lowerside($word
                                     _o_
                                    $^editModel(#changecost)
                                    _o_
                                    $langModel),
                        #num) ;
}

// end of script
\end{Verbatim}

Once the script is loaded, one can simply call

\begin{alltt}
	print \verb!$^correct!(\emph{misspelledword}) ;
	print \verb!$^correct!(\emph{misspelledword}, 5) ;
	print \verb!$^correct!(\emph{misspelledword}, 5, 8.0) ;
\end{alltt}

\noindent
or just

\begin{alltt}
	\verb!^correctp!(\emph{misspelledword}) ;
	\verb!^correctp!(\emph{misspelledword}, 5) ;
	\verb!^correctp!(\emph{misspelledword}, 5, 8.0) ;
\end{alltt}

\noindent
The full script, \verb!weightededitdistance.kl!, is available from the
download page at \url{www.kleene-lang.org}.


\section{Background}

Quantum computing is a new framework of computation that promises exponential asymptotic speed-ups in certain problems compared to classical models by using quantum mechanical effects like \emph{superposition} and \emph{entanglement} to write fast algorithms which are also physically realizable. The de facto way of formally expressing universal quantum computation has traditionally been the quantum circuit model\cite{Nielsen2009}. This model uses unitary evolution for data encoding and processing while using hermitian measurements for recording computation results. Algorithms formulated for this model usually start with a qubit register initialized to a specific value, usually \(\ket0\). Then a unitary block, usually composed of smaller and reusable components called \emph{gates}, is applied to this register. Finally, the register is measured on the \(\sigma_z\) basis, and the outcome is written onto a classical register of the same size. This whole process is usually visualized through circuit diagrams. We provide a circuit diagram describing Grover's algorithm in Figure \ref{fig:grover} as a quantum circuit example.

\begin{figure}[htb]
  \centering
  \input{fig/grover}
  \caption{Grover's Algorithm is composed of three steps. The initialization step creates an equal superposition of \(\ket0\) and \(\ket1\) for all the qubits in the input register. Then the unitary main body, the Grover Operator, is applied consecutively \(\approx\pi\sqrt{n}/4\) times. The last phase is the measurement of the input register.}\label{fig:grover}
\end{figure}

The quantum circuit model is undoubtedly a popular, though not the only way of reasoning about quantum computation. Issues regarding its scalability led Russendorf et al. to introduce the one-way quantum computer \cite{russendorf2001, russendorf2003}. At the heart of this model's mathematical foundations lie graph states. Graph states are considered by Hein et al. as the results of a class of qubit interactions\cite{hein2006}. Within this formalism, qubits are described as vertices of a graph, and the entanglement patterns between those qubits create the edges between those vertices. Since qubits can not self entangle, it should be possible to represent any graph state with a simple graph.

Representation of graph states is an important matter. Even though wave functions can represent graph states, this turns out to be less than ideal. The wave function of an \(n\) qubit graph state has up to \(2^n\) terms, so it is in no way a compact representation. Furthermore, using the wave functions to represent graph states makes seeing entanglement patterns tricky. Due to these shortcomings of wave function representations, \emph{stabilizers}\cite{Nielsen2009} are used for representing graph states. The subgroup of \(n\) dimensional Pauli group \(\symcal{P}^n\) for which a graph state remains invariant with the application of its elements is called the stabilizer of that graph state. It is important to note that a graph state is uniquely represented by its stabilizer.

The main computational resource of a one-way quantum computer is a  \emph{cluster}, which is defined by Briegel and Russendorf as a \(d\)-dimensional rectangular lattice of qubits that have Ising-type interactions with their neighbours\cite{Briegel_2001}. The state of a cluster can be represented by a graph and can be prepared via the application of controlled \(\sigma_z\) gates to vertices connected by an edge. Then, information is encoded and processed via spin measurements to the cluster. The output of this process can then be accessed directly by the measurement of output qubits. An example execution of a one-way computer with a 2-dimensional cluster is given in Figure \ref{fig:cluster}.

\begin{figure}
  \centering
  \includegraphics[
    width=0.75\textwidth
    ]{fig/figure1.eps}
  \caption{Computation on a cluster. \(\odot\) denotes a measurement along \(\sigma_z\), a vertical arrow denotes a measurement along \(\sigma_x\) and a tilted arrow denotes a measurement along \((\sigma_x + \sigma_y)/\sqrt2\). The image by Raussendorf et. al.\cite{russendorf2003} \label{fig:cluster}}
\end{figure}


%%%%%%%%%%%%%%%%%%%%%definitions%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\input{../../doc/related_pages/header.tex}
\input{../../doc/related_pages/newcommands.tex}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%DOCUMENT%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\title{Testing Advection Schemes}
\author{ M.~Wiesenberger}
\maketitle

\begin{abstract}
  This is a program to test various advection schemes on the 2d incompressible Euler
  equation used in Reference~\cite{Einkemmer2014}.
\end{abstract}

\section{Equations}
We implement the 2d incompressible Euler equation
\begin{subequations}
\begin{align}
 \frac{\partial \omega}{\partial t} + \{ \phi, \omega\} = 0 \\
 -\Delta \phi = \omega \label{eq:euler_poisson_elliptic}
\end{align}
\label{eq:euler_poisson}
\end{subequations}
with vorticity $\omega$ and stream-function $\phi$.
The Poisson bracket is given by $\{ \phi, \omega\} := \phi_x \omega_y - \phi_y \omega_x$.
Eq.~\eqref{eq:euler_poisson} is a reformulation of the standard conservative form
\begin{subequations}
\begin{align}
    \frac{\partial \omega}{\partial t} + \nabla\cdot({\vec v \omega}) = 0 \\
\nabla\cdot\vec v = 0 \quad \omega = -(\nabla\times \vec v)\cdot \zhat
\end{align}
\label{eq:euler_conservative}
\end{subequations}
with $v_x = - \phi_y$ and $v_y = \phi_x$
or since the divergence of $\vec v$ is $0$, $\nc \vec v = 0$ we have the advection form
\begin{subequations}
\begin{align}
    \frac{\partial \omega}{\partial t} + \vec v\cn \omega = 0 \\
    -\Delta\phi = \omega \quad v_x = -\phi_y \quad v_y = \phi_x
\end{align}
\label{eq:euler_advection}
\end{subequations}

Eqs.~\eqref{eq:euler_poisson} have an infinite amount of conserved quantities
among them the total vorticity $V$, the kinetic energy $E$ and the enstrophy $\Omega$
 \begin{align}
     V := \int_D \omega \dA\quad
     E :=\frac{1}{2} \int_D \left( \nabla \phi\right)^2 \dA \quad
     \Omega:= \frac{1}{2} \int_D \omega^2 \dA
 \end{align}

\section{Initialization}
We will consider several different initial conditions in order to test
our numerical methods
\subsection{Lamb Dipole}
The Lamb dipole is a stationary solution to the Euler equations~\cite{Nielsen1997} with infinite
boundary conditions
\begin{align}
    \omega(x,y,0) = \begin{cases}
        \frac{2\lambda U}{J_0(\lambda R)} J_1(\lambda R) \cos \theta,\ r < R,\\
        0, \text{ else}
    \end{cases}
\end{align}
Unfortunately, for a finite box this is not an exact solution any more.
on the domain $[0,1]\times [0,1]$.
The Lamb dipole is chosen with the following parameters in the input file
%%This is a booktabs table
\begin{longtable}{llll}
\toprule
\rowcolor{gray!50}\textbf{Name} &  \textbf{Type} & \textbf{Value}  & \textbf{Description}  \\ \midrule
grid & dict &  & Grid parameters \\
\qquad x & float[2]& [0,1] & Choose x box boundaries appropriately \\
\qquad y & float[2]& [0,1] & Choose y box boundaries appropriately \\
\qquad bc & string[2] & [DIR, PER] & Choose boundary conditions [x,y] appropriately \\
init &  dict &   & Parameters for initialization \\
\qquad type      & string & lamb & This choice necessitates the following parameters \\
\qquad velocity  & float &  1    &  blob speed \\
\qquad sigma     & float &  0.1  & dipole radius in units of lx \\
\qquad posX      & float &  0.5  & in units of lx \\
\qquad posY      & float &  0.8  & in units of ly \\
\bottomrule
\end{longtable}
\subsection{Manufactured Solution}
We manufacture a solution via
\begin{align}
    \phi(x,y,t) &=
    x \exp\left( - \frac{ x^2 + (y+vt)^2}{\sigma^2}\right) \\
    \omega(x,y,t) &= -\Delta \phi = -\sigma^{-4} \left[ 4\phi(x,y,t) ( x^2-2\sigma^2  + (y+tv)^2)\right]
\end{align}
which is solution to the modified equations
\begin{subequations}
\begin{align}
    \frac{\partial \omega}{\partial t} + \{ \phi, \omega\} = S(x,y,t) \\
    -\Delta \phi = \omega
\end{align}
\label{eq:euler_poisson_modified}
\end{subequations}
with the source
\begin{align}
    S(x,y,t) =& 8 x \sigma^{-6}(y+vt)\exp\left( - 2\frac{ x^2 + (y+vt)^2}{\sigma^2} \right) \nonumber\\
    &\left(-\sigma^2  + \exp\left( \frac{ x^2 + (y+vt)^2}{\sigma^2} \right) v( -3\sigma^2 + x^2 + (y+vt)^2) \right)
\end{align}
on the domain $[-1,1]\times [-1,1]$.

The manufactured solution is chosen with the following parameters in the input file
\begin{longtable}{llll}
\toprule
\rowcolor{gray!50}\textbf{Name} &  \textbf{Type} & \textbf{Value}  & \textbf{Description}  \\ \midrule
grid & dict &  & Grid parameters \\
\qquad x & float[2]& [-1,1] & Choose x box boundaries appropriately \\
\qquad y & float[2]& [-1,1] & Choose y box boundaries appropriately \\
\qquad bc & string[2] & [DIR, PER] & Choose boundary conditions appropriately \\
init &  dict &   & Parameters for initialization \\
\qquad type      & string & mms & This choice necessitates the following parameters \\
\qquad sigma      & float & 0.2 & The width $\sigma$ \\
\qquad velocity   & float & 1 & The velocity $v$ \\
\bottomrule
\end{longtable}
\subsection{Simple sine function}
A simple sine function is given by
\begin{align}
    \omega(x,y,0) = 2 \sin(x)\sin(y)
\end{align}
which has an analytical solution
\begin{align}
\omega(x,y,t) = 2 \sin(x)\sin(y)\exp( -(2\nu)^s t)
\end{align}
if there is artificial viscosity and is invariant else.
on the domain $[0,2\pi]\times [0,2\pi]$.

This solution is chosen with the following parameters in the input file
\begin{longtable}{llll}
\toprule
\rowcolor{gray!50}\textbf{Name} &  \textbf{Type} & \textbf{Value}  & \textbf{Description}  \\ \midrule
grid & dict &  & Grid parameters \\
\qquad x & float[2]& [0, 6.283185307179586] & Choose x box boundaries appropriately \\
\qquad y & float[2]& [0, 6.283185307179586] & Choose y box boundaries appropriately \\
\qquad bc & string[2] & [DIR, PER] & Choose boundary conditions appropriately \\
init &  dict &   & Parameters for initialization \\
\qquad type      & string & sine & No other parameters are necessary \\
\bottomrule
\end{longtable}

\subsection{ Double Shear layer}
Here, we follow~\cite{Liu2000} and test the scheme on a double shear layer problem.
\begin{align}
    \omega(x,y,0) = \begin{cases}
        \delta \cos(x) - \frac{1}{\rho} \text{sech}^2 \left(\frac{y-\pi/2}{\rho}\right),\ y \leq \pi \\
        \delta \cos(x) + \frac{1}{\rho} \text{sech}^2 \left(\frac{3\pi/2-y}{\rho}\right),\ y > \pi \\
    \end{cases}
\end{align}
where $\rho = \pi/15$ and $\delta =0.05$ on the domain $[0,2\pi]\times [0,2\pi]$.
This solution will quickly roll-up and generate smaller and smaller scales.
A thin shear layer corresponds to $\rho = \pi/50$ or smaller.
The double shear layer initialization is chosen with the following parameters in the input file
%%This is a booktabs table
\begin{longtable}{llll}
\toprule
\rowcolor{gray!50}\textbf{Name} &  \textbf{Type} & \textbf{Value}  & \textbf{Description}  \\ \midrule
grid & dict &  & Grid parameters \\
\qquad x & float[2]& [0, 6.283185307179586] & Choose x box boundaries appropriately \\
\qquad y & float[2]& [0, 6.283185307179586] & Choose y box boundaries appropriately \\
\qquad bc & string[2] & [PER, DIR] & Choose boundary conditions appropriately \\
init &  dict &   & Parameters for initialization \\
\qquad type      & string & shear & This choice necessitates the following parameters \\
\qquad rho    & float & 0.20943951023931953 & The width $\rho = \pi/15$ \\
\qquad delta  & float & 0.05 & The velocity $v$ \\
\bottomrule
\end{longtable}

\section{Numerical methods}
Our goal is to try out various time integration and advection discretization techniques.
We know from Godunov's theorem
that any linear advection scheme of order 2 or higher is prone to oscillations.
\subsection{Spatial grid}
The spatial grid is a two-dimensional Cartesian product-grid adaptable with the following parameters
\begin{longtable}{llll}
\toprule
\rowcolor{gray!50}\textbf{Name} &  \textbf{Type} & \textbf{Value}  & \textbf{Description}  \\ \midrule
grid & dict & & \\
\qquad n  & integer & 3  & The number of polynomial coefficients \\
\qquad Nx & integer & 48 & Number of cells in x \\
\qquad Ny & integer & 48 & Number of cells in y \\
\qquad  x & float[2]& [0,1] & Boundaries in x \\
\qquad  y & float[2]& [0,1] & Boundaries in y \\
\qquad bc & string[2] & [DIR, PER] & Boundary conditions in [x,y] \\
\bottomrule
\end{longtable}
\subsection{Time steppers}
Possible time-steppers are the explicit and semi-implicit multistep schemes
as well as the Shu-Osher scheme that originally incorporated limiters in the dG scheme.
\begin{longtable}{lllp{6cm}}
\toprule
\rowcolor{gray!50}\textbf{Name} &  \textbf{Type} & \textbf{Value}  & \textbf{Description}  \\ \midrule
timestepper & dict & & \\
\qquad type     & string& ImExMultistep & The semi-implicit multistep scheme (only in combination with viscosity regularization) \\
\qquad tableau  & string & Any ImEx tableau & for example ImEx-BDF-3-3* \\
\qquad dt       & float & 2e-3 & Fixed timestep \\
\qquad eps\_time & float & 1e-9 & Accuracy requirement for implicit solver \\
timestepper & dict & & \\
\qquad type & string & Shu-Osher & An explicit Runge Kutta method with filter (viscosity is treated explicitly) \\
\qquad tableau   & string & Any Shu-Osher tableau & for example SSPRK-3-3* \\
\qquad dt      & float & 1e-3 & Fixed Time-step \\
timestepper & dict & & \\
\qquad type & string & FilteredExplicitMultistep & an explicit multistep class with the option to use a filter (viscosity is treated explicitly)\\
\qquad tableau   & string & Any explicit multistep tableau & for example eBDF-3-3* \\
\qquad dt      & float & 2e-3 & Fixed timestep \\
\bottomrule
\end{longtable}
*See the dg documentation for what tableaus are available.
\subsection{Regularization technique}
Choose either no regularization or artificial viscosity or modal filtering by the following
parameters in the input file.

For no regularization choose
\begin{longtable}{lllp{7.5cm}}
\toprule
\rowcolor{gray!50}\textbf{Name} &  \textbf{Type} & \textbf{Value}  & \textbf{Description}  \\ \midrule
regularization & dict & & \\
\qquad type  & string& none & No regularization\\
\bottomrule
\end{longtable}

For artificial viscosity Eqs.~\eqref{eq:euler_poisson} are modified to
\begin{subequations}
\begin{align}
    \frac{\partial \omega}{\partial t} + \{ \phi, \omega\} = -(-\nu \Delta)^s \omega\\
 -\Delta \phi = \omega
\end{align}
\label{eq:euler_poisson_viscous}
\end{subequations}
where $\nu$ is the viscosity coefficient and $s=1,2,3,\cdots$ is the order
\begin{longtable}{lllp{7.5cm}}
\toprule
\rowcolor{gray!50}\textbf{Name} &  \textbf{Type} & \textbf{Value}  & \textbf{Description}  \\ \midrule
regularization & dict & & \\
\qquad type  & string& viscosity & the artificial viscosity \\
\qquad order    & integer & 2 & Order: 1 is normal diffusion, 2 is hyperdiffusion, can be arbitrarily high, but higher orders might take longer to solve or restrict the CFL condition \\
\qquad nu    & float & 1e-3 & Viscosity coefficient \\
\qquad direction & string & centered & Direction of the Laplacian: forward or centered \\
\bottomrule
\end{longtable}
The other regularization method is the modal filter that applies an exponential filter
\begin{align}
    \begin{cases}
    1 \text{ if } \eta < \eta_c \\
    \exp\left( -\alpha  \left(\frac{\eta-\eta_c}{1-\eta_c} \right)^{2s}\right) \text { if } \eta \geq \eta_c \\
    0 \text{ else} \\
    \eta := \frac{i}{n-1}
    \end{cases}
\end{align}
and is choosable with the following parameters
\begin{longtable}{lllp{7.5cm}}
\toprule
\rowcolor{gray!50}\textbf{Name} &  \textbf{Type} & \textbf{Value}  & \textbf{Description}  \\ \midrule
regularization & dict & & \\
\qquad type  & string& modal & Not choosable for \textbf{ImExMultistep} timestepper\\
\qquad order & integer & 8 & Order: normally 8 or 16 \\
\qquad eta\_c & float & 0.5 & cutoff wavelength below which no damping is applied \\
\qquad alpha & float & 36 & damping coefficient determining damping for highest wavenumber \\
\bottomrule
\end{longtable}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Elliptic solver}
Currently, the multigrid solver is the only one choosable
to solve Eq.~\eqref{eq:euler_poisson_elliptic}
\begin{longtable}{lllp{7.5cm}}
\toprule
\rowcolor{gray!50}\textbf{Name} &  \textbf{Type} & \textbf{Value}  & \textbf{Description}  \\ \midrule
elliptic & dict & & \\
\qquad type  & string& multigrid & Actually a nested iterations class \\
\qquad stages    & integer & 3 & Number of stages (3 is best in virtually all cases) \\
\qquad eps\_pol    & float[stages] & [1e-6,10,10] & Accuracy requirement on each stage of the multigrid scheme. $\eps_0 = \eps_{pol,0}$, $\eps_i = \eps_{pol,i} \eps_{pol,0}$  for $i>1$. \\
\qquad direction & string & centered & Direction of the Laplacian: forward or centered \\
\bottomrule
\end{longtable}
\subsection{Advection schemes}
The following parameters control the advection scheme in the code
\begin{longtable}{lllp{7.5cm}}
\toprule
\rowcolor{gray!50}\textbf{Name} &  \textbf{Type} & \textbf{Value}  & \textbf{Description}  \\ \midrule
advection & dict & & \\
\qquad type  & string& arakawa & Discretize Eqs.~\eqref{eq:euler_poisson} using Arakawa's scheme \cite{Einkemmer2014} \\
\qquad type  & string& centered & Discretize Eqs.~\eqref{eq:euler_conservative} using the centered flux \\
\qquad type  & string& upwind & Discretize Eqs.~\eqref{eq:euler_conservative} using the upwind flux \\
\qquad type  & string& centered-advection & Discretize Eqs.~\eqref{eq:euler_advection} using the centered flux \\
\qquad type  & string& upwind-advection & Discretize Eqs.~\eqref{eq:euler_advection} using the upwind flux \\
\qquad multiplication    & string & pointwise & The multiplications in the scheme
are done pointwise in nodal space\\
\qquad multiplication    & string & projection & The multiplications in the scheme
are done by first interpolating to a higher polynomial grid with $n_{fine} = 2n$, and projecting the result back to the coarse grid\\
\bottomrule
\end{longtable}
%\subsection{Forward time and centered space}
%It is well known that the forward in time, centered in space method for solving
%hyperbolic systems is unconditionally unstable~\cite{LeVeque}.
%\subsection{Arakawa scheme and centered flux}
%Reference~\cite{Liu2000} reports that the centered flux does not have any numerical
%diffusion while the upwind flux does.
%They also prove that upwind and centered fluxes do not dissipate energy.
%From finite differences we know that centered differences for the advection term
%are unstable (or at least produce a lot of oscillations).

\section{Compilation and useage}
The program shu\_b.cu compiles with
\begin{verbatim}
make <shu_b> device = <omp gpu>
make <shu_hpc> device = <omp gpu>
\end{verbatim}
and depends on both GLFW3 and NETCDF. If GLFW3 is not available then compile shu\_hpc which avoids this dependency.
Run with
\begin{verbatim}
path/to/feltor/src/shu/shu_b input.json
\end{verbatim}

\subsection{Output structure}
Input file format: json

We can either display the results in real-time to screen using the glfw3 library or
write the results to a file in netcdf-4 format.
This is regulated by the output paramters in the input file
\begin{longtable}{lllp{7cm}}
\toprule
\rowcolor{gray!50}\textbf{Name} &  \textbf{Type} & \textbf{Value}  & \textbf{Description}  \\ \midrule
output & dict & & \\
\qquad type  & string& glfw & Use glfw to display results in a window while computing (requires to compile with the glfw3 library) \\
\qquad type  & string& netcdf & Use netcdf to write results into a file (see next section for information about what is written in there) \\
\qquad itstp  & integer& 4 & The number of steps between outputs of 2d fields \\
\qquad maxout  & integer& 500 & The total number of field outputs. The endtime is T=itstp*maxout*dt \\
\bottomrule
\end{longtable}
\subsection{Structure of output file}
Output file format: netcdf-4/hdf5
%
%Name | Type | Dimensionality | Description
%---|---|---|---|
\begin{longtable}{lll>{\RaggedRight}p{7cm}}
\toprule
\rowcolor{gray!50}\textbf{Name} &  \textbf{Type} & \textbf{Dimension} & \textbf{Description}  \\ \midrule
inputfile  &             text attribute & 1 & verbose input file as a string \\
time                     & Coord. Var. & 1 (time) & time at which fields are written \\
x                        & Coord. Var. & 1 (x) & x-coordinate  \\
y                        & Coord. Var. & 1 (y) & y-coordinate \\
vorticity                & Dataset & 3 (time, y, x) & electon density $n$ \\
potential                & Dataset & 3 (time, y, x) & electric potential $\phi$  \\
vorticity\_1d            & Dataset & 1 (time) & Vorticity integral $V$  \\
enstrophy\_1d            & Dataset & 1 (time) & Enstropy integral $\Omega$  \\
energy\_1d               & Dataset & 1 (time) & Total energy integral computed using $E = \int_D \phi\omega \dA$ \\
time\_per\_step          & Dataset & 1 (time) & Average time for one output \\
error                    & Dataset & 1 (time) & Relative error to analytical solution if available, 0 else \\
\bottomrule
\end{longtable}
The output fields are determined in the file \texttt{feltor/src/lamb\_dipole/diag.h}.

%..................................................................
\bibliography{../../doc/related_pages/references}
%..................................................................

\end{document}


\section*{Abstract} \label{sec:abstract}

Two dimensional polynomials of various orders are fit to noisy Franke function using linear regression methods. The regression methods are ordinary least squares (OLS) and shrinkage methods like ridge and lasso regression. The parameters are either found through analytical methods, scikit functions, or by optimizing a cost function using stochastic gradient descent. Next, logistic regression and support vector machines are used to learn a classification model to predict whether a tumor is benign or malignant based on Wisconsin Breast Cancer Dataset. Various statistics are utilized to determine the performance of the regression and classification models.


\documentclass[12pt, letterpaper]{report}
\usepackage[margin=1in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{float}
\usepackage{subfig}
\graphicspath{ {./img/} }
\setlength\parindent{0pt}
\renewcommand\thesection{\arabic{section}.}
\renewcommand{\thesubsection}{\alph{subsection})}
\def\ojoin{\setbox0=\hbox{$\bowtie$}
\rule[-.02ex]{.25em}{.4pt}\llap{\rule[\ht0]{.25em}{.4pt}}}
\def\leftouterjoin{\mathbin{\ojoin\mkern-5.8mu\bowtie}}
\def\rightouterjoin{\mathbin{\bowtie\mkern-5.8mu\ojoin}}
\def\fullouterjoin{\mathbin{\ojoin\mkern-5.8mu\bowtie\mkern-5.8mu\ojoin}}

\title{CS1555 - Relational Algebra}
\author{Zachary M. Mattis}

\begin{document}

\maketitle

\section{Arity / Cardinality}

% A
\subsection{$\pi_{country}(Users)$}

\begin{verbatim}
Arity       - 1
Cardinality - Min() = 1, Max() = 100
\end{verbatim}

The arity is 1 due to only projecting the \textit{Country} column from the \textit{Users} relation. The min cardinality value occurs when all users live in the same country. The max cardinality occurs when each user lives in a different country (no duplicates). This could have an absolute max value of 195, as there are that many countries in the world today.

% B
\subsection{$Mail \leftouterjoin_{Mail.mailID=Recipients.mailID} Recipients$}

\begin{verbatim}
Arity       - 9
Cardinality - Min() = 3500, Max() = 5499
\end{verbatim}

The arity is 9 due to selecting all columns from both the \textit{Mail} and \textit{Recipients} relations, even if they have the same value (\textit{mailID}). The min cardinality value occurs when each piece of mail has been sent to at least 1 user. The max cardinality occurs if only 1 piece of mail has been sent to 3500 recipients, resulting in 1999 records with null recipients on the left outer join. However, if mail MUST have at least 1 recipient, then the max cardinality is 3500.

% C
\subsection{$Mail * Labels$}

\begin{verbatim}
Arity       - 9
Cardinality - 5,000,000
\end{verbatim}

The arity is 9 due to selecting all columns from both the \textit{Mail} and \textit{Labels} relations, even if they have the same value (\textit{mailID}). The cardinality value is equal to the cardinality of each relation multiplied together.

\section{Expressions}

% A
\subsection{Urgent Mail}

\[ \pi_{fname,\;lname,\;userID,\;subject,\;timeSent} (\sigma_{urgent=1} (Mail\;\leftouterjoin_{Mail.senderID=Users.userID}\;Users) )  \]

\begin{verbatim}
SQL:

SELECT
    u.fname,
    u.lname,
    u.userID,
    m.subject,
    m.timeSent
FROM
    Mail m
LEFT JOIN Users u
    ON m.senderID = u.userID
WHERE
    m.urgent = 1;

\end{verbatim}

% B
\subsection{Outside USA}

\[ \pi_{userID} (\sigma_{urgent=1\;\wedge\;country <> ``USA"} (Mail\;\leftouterjoin_{Mail.senderID=Users.userID}\;Users) )  \]

\begin{verbatim}
SQL:

SELECT  DISTINCT
    u.userID
FROM
    Mail m
LEFT JOIN Users u
    ON m.senderID = u.userID
WHERE
    m.urgent = 1 AND
    u.country <> "USA";

\end{verbatim}

% C
\subsection{Important Label}

\[ G_{sum(mailID)} (\pi_{mailID} (\sigma_{label=``important"\;\wedge\;urgent<>1\;\wedge\;timeSent=2015} (Mail\;\leftouterjoin_{Mail.mailID=Labels.mailID}\;Labels ))  \]

\begin{verbatim}
SQL:

SELECT
    COUNT( DISTINCT m.mailID)
FROM
    Mail m
LEFT JOIN Labels l
    ON m.mailID = l.mailID
WHERE
    l.label = "important" AND
    m.urgent <> 1 AND
    YEAR(m.timeSent) = 2015;

\end{verbatim}

% D
\subsection{Ava Lovelace}

\[ \pi_{fname,\;lname} (\sigma_{recipientID=[\pi_{recipientID} (\sigma_{fname=``Ava"\;\wedge\;lname=``Lovelace"} (Mail\;\leftouterjoin\;_{senderID=userID}\;Users\leftouterjoin\;Recipients ))]} \]
\[ (Mail\;\leftouterjoin\;Recipients\;\leftouterjoin\;_{senderID=userID}\;Users ))  \]

\begin{verbatim}
SQL:

SELECT
    u.fname,
    u.lname
FROM
    Mail m
LEFT JOIN Users u
    ON m.senderID = u.userID
LEFT JOIN Recipients r
    ON m.mailID = r.mailID
WHERE
    r.recipientID = (

   SELECT DISTINCT
       r.recipientID
   FROM
       Mail m
   LEFT JOIN Recipients r
       ON m.mailID = r.mailID
   LEFT JOIN Users u
       ON m.senderID = u.userID
   WHERE
       u.fname = "Ava" AND
       u.lname = "Lovelace"

   );

\end{verbatim}

% E
\subsection{Not Reply in USA}

\[ \pi_{mailID} (\sigma_{replyTo=NULL\;\wedge\;(u\_send.country=``USA"\;\vee\;u\_recv.country=``USA")} \]
\[ (Mail\;\leftouterjoin\;Recipients\;\leftouterjoin\;_{senderID=u\_send.userID}\;Users\;u\_send\;\leftouterjoin\;_{recipientID=u\_recv.userID}\;Users\;u\_recv ))  \]

\begin{verbatim}
SQL:

SELECT DISTINCT
    m.mailID
FROM
    Mail m
LEFT JOIN Users u_send
    ON m.senderID = u_send.userID
LEFT JOIN Recipients r
    ON m.mailID = r.mailID
LEFT JOIN user u_recv
    ON r.recipientID = u_recv.userID
WHERE
    m.replyTo IS NULL AND
    ( u_send.country = "USA" OR
      u_recv.country = "USA"
    );

\end{verbatim}

\end{document}


\documentclass{subfile}

\begin{document}
	\section{BkMO}\label{sec:bkmo}

		\begin{problem}[$2019$, problem $2$]
			Let $a,b,c$ be real numbers such that $0\leq a\leq b\leq c$ and $a+b+c=ab+bc+ca>0$. Prove that
				\begin{align*}
					\sqrt{bc}(a+1)
						& \geq2
				\end{align*}
			Determine when equality occurs.
		\end{problem}

		\begin{problem}[$2015$, problem $1$]
			Let $a,b,c$ be positive real numbers. Prove that
				\begin{align*}
					a^{3}b^{6}+b^{3}c^{6}+c^{3}a^{6}+3a^{3}b^{3}c^{3}
						& \geq abc(a^{3}b^{3}+b^{3}c^{3}+c^{3}a^{3})+a^{2}b^{2}c^{2}(a^{3}+b^{3}+c^{3})
				\end{align*}
		\end{problem}

		\begin{problem}[$2012$, problem $2$]
			Prove that
				\begin{align*}
					\sum_{cyc}(x+y)\sqrt{(z+x)(z+y)}
						& \geq 4(xy+yz+zx)
				\end{align*}
			for all positive real numbers $x,y,z$.
		\end{problem}

		\begin{problem}[$2011$, problem $2$]
			Given real numbers $x,y,z$ such that $x+y+z=0$. Prove that
				\begin{align*}
					\dfrac{x(x+1)}{2x^{2}+1}+\dfrac{y(y+2)}{2y^{2}+1}+\dfrac{z(z+1)}{2z^{2}+1}
						& \geq 0
				\end{align*}
		\end{problem}

		\begin{problem}[$2010$, problem $1$]
			Let $a,b,c$ be positive real numbers. Prove that
				\begin{align*}
					\dfrac{a^{2}(b-c)}{a+b}+\dfrac{b^{2}(c-a)}{b+c}+\dfrac{c^{2}(a-b)}{c+a}
						& \geq0
				\end{align*}
		\end{problem}

		\begin{problem}[$2008$, problem $2$]
			Is there a sequence of positive real numbers $a_{1},a_{2},\ldots$ such that
				\begin{align*}
					a_{1}+\ldots+a_{n}
						& \leq n^{2}\\
					\dfrac{1}{a_{1}}+\ldots+\dfrac{1}{a_{n}}
						& \leq 2008
				\end{align*}
			for all positive integer $n$.
		\end{problem}

		\begin{problem}[$2006$, problem $1$]
			Let $a,b,c$ be positive real numbers. Prove the inequality
				\begin{align*}
					\dfrac{1}{a(b+1)}+\dfrac{1}{b(c+1)}+\dfrac{1}{c(a+1)}
						& \dfrac{3}{1+abc}
				\end{align*}
		\end{problem}

		\begin{problem}[$2005$, problem $3$]
			Let $a,b,c$ be positive real numbers. Prove the inequality
				\begin{align*}
					\dfrac{a^{2}}{b}+\dfrac{b^{2}}{c}+\dfrac{c^{2}}{a}
						& \geq a+b+c+\dfrac{4(a-b)^{2}}{a+b+c}
				\end{align*}
		\end{problem}

		\begin{problem}[$2001$, problem $3$]
			Let $a,b,c$ be positive real numbers such that $a+b+c\leq abc$. Prove that
				\begin{align*}
					a^{2}+b^{2}+c^{2}
						& \geq \sqrt{3}abc
				\end{align*}
		\end{problem}

		\begin{problem}[$1998$, problem $2$]
			Let $n\geq2$ be an integer and $a_{0}<a_{1}<\ldots<a_{2n+1}$ be real numbers. Prove that
				\begin{align*}
					\sqrt[n]{a_{1}}-\sqrt[n]{a_{2}}+\ldots+\sqrt[n]{a_{2n+1}}
						& < \sqrt[n]{a_{1}-a_{2}+\ldots+a_{2n+1}}
				\end{align*}
		\end{problem}

		\begin{problem}[$1993$, problem $1$]
			Let $a,b,c,d,e,f$ be real numbers such that $a+b+c+d+e+f=10$ and
				\begin{align*}
					(a-1)^{2}+(b-1)^{2}+(c-1)^{2}+(d-1)^{2}+(e-1)^{2}+(f-1)^{2}
						& = 6
				\end{align*}
			What is the maximum possible value of $f$?
		\end{problem}
\end{document}


\section{Graph databases}
A graph $G = (V, E)$ is a set of edges and vertices which can have different properties. Edges can also have intermediate ramifications between nodes.

RDF is a special type of graph database; general ones can store more variants of data and have less standards.

Examples of interesting properties regarding a graph are:
\begin{itemize}
	\item Degree, the number of edges incident to every vertex;
	\item Centrality, defining the most important vertices in terms of flow and cohesiveness;
	\item Betweeness, centrality based on shortest paths;
	\item Connectivity, the smallest number of either edges or vertices whose removal disconnects the graph.
\end{itemize}

Graph databases can be implemented using semantic data in triples, introducing properties of nodes.

There is no defined standard for structure of graphs, in fact they can also be implemented using relational databases, yet some tools are more popular then others.

Queries on graph databases do not strictly return a set of tuples: they allow pattern matching, algorithms, aggregation, or properties of graph and its elements.

Graphs are an essential structure to perform algorithms with wide real-life applications, such as Dijkstra and breadth-first search. although computationally heavy.

\subsection{Cypher}
Cypher is one of the most supported query languages for graphs. Its syntax allows nodes and edges to have a type along with some attributes.

Its functions are similar to SQL, implementing aggregating, pattern matching and graph properties in addition.

Neo4j, implementing Cypher, is a graph database offering dynamic visualization and machine learning algorithms, but it kind of sucks (not scalable).

\subsection{Pregel/Giraph}
Pregel is Google's iterative graph processing system for distributed data, while Giraph is its open source alternative.

They work using Gremlin, a functional graph traversal language, which with one operation lets a vertex modify its state and propagate information.

The reason why this tool has been developed is because MapReduce systems do not allow operations on a vertex level. In addition, MapReduce requires transferring state to all nodes.

Foundations of Data Engineering/lectures/lecture8.tex


\documentclass{memoir}
\usepackage{notestemplate}

%\logo{~/School-Work/Auxiliary-Files/resources/png/logo.png}
%\institute{Rice University}
%\faculty{Faculty of Whatever Sciences}
%\department{Department of Mathematics}
%\title{Class Notes}
%\subtitle{Based on MATH xxx}
%\author{\textit{Author}\\Gabriel \textsc{Gress}}
%\supervisor{Linus \textsc{Torvalds}}
%\context{Well, I was bored...}
%\date{\today}

%\makeindex

\begin{document}

% \maketitle

% Notes taken on

\chapter{Vector Spaces}

\begin{defn}[Vector Space]
A \textbf{vector space} has four key elements:

\begin{itemize}
	\item Our space of vectors $V$
	\item A field $F$
	\item An addition operation \(+:V\times V \to V\)
	\item A scalar multiplication operation \(\cdot:F\times V \to V\)
\end{itemize}

where operations (3) and (4) must satisfy:
\begin{itemize}
	\item Commutativity: $u + v = v + u$ $ \forall u,v \in V$
	\item Associativity: $(u+v)+w = u+(v+w)$, $ \forall u,v,w \in V$ AND $(\lambda_1 \cdot  \lambda_2)\cdot v = \lambda_1 \cdot (\lambda_2\cdot v)$
	\item Additive identity: $\exists \overline{0}\in V$ such that $v+\overline{0} = v$, $\forall v\in V$.
	\item Scalar multiplicative identity: $1_F \cdot v = v$, $\forall v\in V$
	\item Distributivity:
\begin{align*}
		\lambda \cdot (u+v) = \lambda \cdot u + \lambda * v \quad \lambda \in F, \; u,v \in V\\
	(\lambda_1 + \lambda_2) \cdot v = \lambda_1 \cdot v + \lambda_2 \cdot v \quad \lambda_1, \lambda_2 \in F, \; v \in V
\end{align*}
\end{itemize}
We call elements \(v \in V\) \textbf{vectors}.
\end{defn}
\begin{exmp}[Vector Spaces]
Some pedagogical examples:
	\begin{itemize}
\item $(\R^2,\R,+,\cdot)$
\item $(\mathbb{C}^2, \mathbb{C}, +, \cdot)$ (via complex scalar multiplication)
\item $(\mathbb{C}^2,\R,+,\cdot)$ (via real scalar multiplication)
\item $(F^{n}, F, +, \cdot )$
\item $(F^{\infty}, F, +, \cdot )$ where $F^{\infty} = \left\{ (x_1,x_2,\ldots) \mid x_i \in F, i=1,2,\ldots \right\}$ (note that this space is infinite dimensional)
\item Let $F$ be a field, and $S$ a set. Let $V = F^{S} := \left\{ \text{ all functions } f:S\to F \right\} $. Addition is defined in $V$ as follows: Let  $f,g:S\to F$. Then $\forall  s \in S$
	\begin{align*}
		(f+g): S\to F \text{ and } (f+g)(s) := f(s)+g(s)
	\end{align*}
	Scalar multiplication is defined as follows: Let $\lambda \in F; f\in V; f:S\to F$, so $(\lambda \cdot f):S\to F$. Then $\forall s \in S$
	\begin{align*}
	(\lambda \cdot f)(s) := \lambda \cdot f(s) \forall s\in S
\end{align*}
	\item  Let $F = \R$ and $V = \left\{  \text{polynomials of degree }\leq 19 \text{ with coefficients in }\R  \right\} $.
Then V is a vector space.
	\end{itemize}

\end{exmp}

\begin{rmrk}

If $S = \left\{ 1,\ldots,n \right\} $ then $V = F^{S}$ which is equivalent to functions $f:\left\{ 1,\ldots,n \right\} \to F$, which is equivalent to $F^{n}$. Thus they are isomorphic.
\end{rmrk}

% \printindex
\end{document}


\section{Neural Networks}
This section introduces the main concepts related to neural networks. Neural networks have been around since the 1940s and could initially handle only one hidden layer. But with the development of technologies and hardware, it became possible to build deeper, more effective architectures, which lead to deep learning as we know it today. \par

\subsection{Brief History}

At first, neural networks were inspired by the functioning of the biological brain, which is why deep learning is also called artificial neural networks (ANNs)~\cite{Goodfellow-et-al-2016}. In biology, a neuron is the cell that receives, processes and transmits information to other neurons through connections called synapses~\cite{neuron}. On the other hand, artificial neurons are defined as computational units (usually mathematical functions) that take one or more inputs and generate an output. \par

McCulloch and Pits designed an initial version of the neuron as a linear model in 1943, aiming to replicate brain function~\cite{REF:11}:

\begin{equation}
f(x,w)=x_1*w_1 +x_2*w_2 +...+x_n*w_n
\end{equation}
where $x_1, ..., x_n$ are the input values and $w_1, ..., w_2$ is a set of hand-chosen weights.

\subsection{Components of an artificial neural network}

A simple artificial neural network (ANN) consists of an input layer, hidden layer and output layer, where the values of the hidden layer are used as inputs for the output layer. A network with several layers is known as a deep neural network. Data flows through the neurons of the layer. Each neuron transforms the input it receives and sends it to the next layer. The neurons share the same characteristics regardless of the layer they are part of. \par

The Neuron, also called a node, is the basic unit of a neural network. Its main components include inputs,
weights, activation function and output(s). From a high-level point of view, the inputs are multiplied by
weights, then an activation function is applied to the result and finally, another function computes the output~\cite{REF:12}~\cite{REF:13}. \par

\begin{itemize}
	\item Weights are defined as adaptive coefficients, whose values are changed during the learning process. They represent the strength of the connection between units. A weight decides how much impact the input will have on the output

	\item The summation function helps combine the input and weights, before passing the result to the activation function. Denote the input as $X = [x_1, x_2, ...x_n]$ and the weight vector as $W = [w_1, w_2, ...w_n]$.\\
	The summation function could be defined as the dot product between these two vectors:\\
	\begin{equation}
	X \cdot W =x_1 \cdot w_1 +x_2 \cdot w_2 +...+x_n \cdot w_n
	\end{equation}

	The summation function could instead compute the minimum, maximum, etc. depending on the designated network architecture. The simplest form of an artificial neuron is a linear function which computes the weighted sum of inputs, to which, optionally, bias can be added:
	\begin{equation}
		y = \sum_{i=1}^{i=n}(x_i \cdot w_i) + b \textnormal{, where $b$ is the bias, $x_i \in X, w_i \in W$}
	\end{equation}

	\item The activation function transforms the result of the summation function (usually) in a non-linear way. Typically, it has a squashing effect. It serves as a threshold. It divides the original space into two partitions. Its main purpose is to make the neural network non-linear. We denote the activation function as $g$
	\begin{equation}
		y = g(\sum_{i=1}^{i=n}(x_i \cdot w_i) + b) \textnormal{, where $b$ is the bias, $x_i \in X, w_i \in W$}
	\end{equation}

	\begin{figure}[h]
		\caption[Common activation functions]{Common activation functions~\cite{activation_fig} }
		\centering
		\includegraphics[width=1\textwidth, height=\textheight, keepaspectratio]{activation}
	\end{figure}
	\item The output is usually the result of an activation function

\end{itemize}

\clearpage
\subsection{Under-fitting and Over-fitting}
%reformulat + surse noi

Neural networks are able to learn complicated non-linear functions to fit any training set. On the downside, this may lead to over-fitting where the neural network learns the training data so well that it is unable to generalize on new, unseen data. This problem can especially occur on datasets with a small amount of data to learn from. \par

Under-fitting, the counterpart of over-fitting, happens when a machine learning model isn’t complex enough to accurately capture relationships between a dataset’s features and a target variable. Under-fitted models result in problematic outcomes on new data or data that they  were not trained on, and many times perform poorly even on training data. \par

\begin{figure}[h]
	\caption[Example of over-fitting and under-fitting]{Example of over-fitting and under-fitting~\cite{vitaflux}}
	\centering
	\includegraphics[width=1\textwidth, height=\textheight, keepaspectratio]{"resources/overfitting"}
\end{figure}

\subsection{Convolutional Neural Networks}
\par
Convolutional Neural Networks (CNNs) are a class of Deep Neural Networks,
specialized in analyzing images. Their development was inspired by biological processes.
The connectivity pattern between neurons resembles the animal visual cortex~\cite{cnn}. \par

\begin{figure}[h]
	\centering
	\caption[Typical convolutional neural network architecture]{Typical CNN architecture~\cite{cnn_fig}}
	\label{fig:cnn}
	\includegraphics[width=1.1\textwidth, height=1.5\textheight, keepaspectratio]{"resources/typical_cnn"}
\end{figure}

\par
A convolutional neural network consists of an input and an output layer, as well as multiple hidden layers. The hidden layers are typically convolutional layers, RELU layers, pooling layers, fully connected layers and normalization layers~\cite{cnn_git}.

\begin{itemize}
	\item \textbf{Convolutional} layers are the core building block of convolutional networks. They are responsible for feature extraction and do most of the computation
	\item \textbf{Pooling} layers reduce the dimension of the data by combining the outputs of neuron clusters at one layer into a single neuron in the next layer
	\item \textbf{Normalization} layers apply a transformation that maintains the mean activation close to 0 and the activation standard deviation close to 1
	\item \textbf{Fully connected} layers link every neuron in one layer to every neuron in another layer
	\item \textbf{Dropout} layers have a \% chance to deactivate the neurons of a particular layer. This is a technique used to improve over-fitting by improving generalization. Forcing neurons to deactivate forces the network to learn the same "concept" with different neurons. It is mostly used after fully connected or pooling layers
\end{itemize}


\subsection{\texttt{cluster\_box.py}}\label{geocode:cluster_box}

\begin{verbatim}
# --- geometry setup script for block with clustered particles ---
from gengeo import *

# - input parameters --
# block dimensions
xdim=10
ydim=20
zdim=10

# particle size range
minRadius = 0.2
maxRadius = 1.0
# ---------------------

# corner points
minPoint = Vector3(0.0,0.0,0.0)
maxPoint = Vector3(xdim,ydim,zdim)

# neighbour table
mntable = MNTable3D(minPoint,maxPoint,2.5*maxRadius,2)

# block volume
box = BoxWithPlanes3D(minPoint,maxPoint)

# boundary planes
bottomPlane=Plane(minPoint,Vector3(0.0,1.0,0.0))
leftPlane=Plane(minPoint,Vector3(1.0,0.0,0.0))
frontPlane=Plane(minPoint,Vector3(0.0,0.0,1.0))
topPlane=Plane(maxPoint,Vector3(0.0,-1.0,0.0))
rightPlane=Plane(maxPoint,Vector3(-1.0,0.0,0.0))
backPlane=Plane(maxPoint,Vector3(0.0,0.0,-1.0))

# add them to the box
box.addPlane(bottomPlane)
box.addPlane(leftPlane)
box.addPlane(frontPlane)
box.addPlane(topPlane)
box.addPlane(rightPlane)
box.addPlane(backPlane)

# -- setup packer --
# iteration parameters
insertFails = 1000
maxIter = 1000
tol = 1.0e-6

# packer
packer = InsertGenerator3D( minRadius,maxRadius,insertFails,maxIter,tol,False)

# pack particles into volume
packer.generatePacking(box,mntable,0,1)

# -- set up clustering seeds in a regular grid
# cluster parameters
ncluster_x=3
ncluster_y=6
ncluster_z=3
dx=xdim/float(ncluster_x)
dy=ydim/float(ncluster_y)
dz=zdim/float(ncluster_z)

# regular grid
for i in range(ncluster_x):
    for j in range(ncluster_y):
        for k in range(ncluster_z):
            x=(float(i)+0.5)*dx
            y=(float(j)+0.5)*dy
            z=(float(k)+0.5)*dz
            seed_pos=Vector3(x,y,z)
            tag=i*ncluster_y*ncluster_z+j*ncluster_z+k
            # construct & insert seed "pseudo-particle"
            cseed=Sphere(seed_pos,0.0)
            cseed.setTag(tag)
            mntable.insert(cseed,1)

# -- tag particle according to nearest seed
mntable.tagParticlesToClosest(0,1)

# generate bonds
mntable.generateClusterBonds(0,1.0e-5,1,2)

# remove dummy particles before writing files
mntable.removeParticlesInGroup(1)

# write a geometry file
mntable.write("cluster_box.geo", 1)
mntable.write("cluster_box.vtu", 2)
\end{verbatim}


\documentclass[a4paper,14pt]{article}
\title{Notes on Measure-Theoretic Probability}
\usepackage{amsmath, amssymb, amsthm}
\usepackage{tikz-cd}
\newtheorem*{prop}{Proposition}
\newtheorem*{corollary}{Corollary}
\newtheorem*{remark}{Remark}
\newtheorem*{defn}{Definition}
\begin{document}
\maketitle
\section{Algebras, $\sigma$-algebras, monotone classes}
An \textbf{algebra} of a set $X$ is any non-empty collection $\mathcal{A}$ of subsets of $X$ such that:

\begin{itemize}
    \item if $A \in \mathcal{A}$, then $X-A \in \mathcal{A}$
    \item if $A_1, \ldots, A_n \in \mathcal{A}$, then $\bigcup_1^n A_i \in \mathcal{A}$
\end{itemize}

A \textbf{$\sigma$-algebra} of a set $X$ is an algebra with the second condition replaced by this stronger one:

\begin{itemize}
    \item if $A \in \mathcal{A}$, then $X-A \in \mathcal{A}$
    \item if $(A_1, A_2, \ldots)$ is a sequence of sets in $\mathcal{A}$, then $\bigcup_1^{\infty} A_i \in \mathcal{A}$
\end{itemize}

In other words, an algebra is a non-empty collection of subsets closed under complement and finite union, and a $\sigma$-algebra is a non-empty collection of subsets closed under complement and \textit{countable} union.

It is straightforward to prove, using De Morgan's laws, that an algebra/$\sigma$-algebra is closed under finite/countable intersection.

A \textbf{monotone class} of a set $X$ is any non-empty collection $\mathcal{A}$ of subsets of $X$ such that:

\begin{itemize}
    \item if $(A_1, A_2, \ldots)$ is a sequence of sets in $\mathcal{A}$ with $A_n \subseteq A_{n+1}$ for all $n$, then $\bigcup_1^{\infty} A_i \in \mathcal{A}$
    \item if $(A_1, A_2, \ldots)$ is a sequence of sets in $\mathcal{A}$ with $A_n \supseteq A_{n+1}$ for all $n$, then $\bigcap_1^{\infty} A_i \in \mathcal{A}$
\end{itemize}

\begin{prop}
An algebra is a $\sigma$-algebra iff it is a monotone class.
\begin{proof}
Every $\sigma$-algebra is both an algebra and a monotone class. Conversely, if $\mathcal{A}$ is a both a monotone class and an algebra on $X$, then for any sequence $(A_1, A_2, \ldots)$ of subsets of $\mathcal{A}$, we can define $B_1 := A_1$, $B_n := B_{n-1} \cup A_n$. Then $B_n \subseteq B_{n+1}$ for all $n$, and since each $B_n$ is in $\mathcal{A}$ due to it being an algebra, we have $\bigcup_1^{\infty} B_i \in \mathcal{A}$ because it's a monotone class. But $\bigcup_1^{\infty} B_i = \bigcup_1^{\infty} A_i$, so this proves $\mathcal{A}$ is also a $\sigma$-algebra.
\end{proof}
\end{prop}

\begin{prop}
The intersection of any number of $\sigma$-algebras is a $\sigma$-algebra. The intersection of any number of monotone classes is a monotone class.
\end{prop}

\begin{defn}
For any set $X$ and any collection $\mathcal{C}$ of subsets of $X$, we denote by $\sigma(\mathcal{C})$ the intersection of all $\sigma$-algebras of $X$ that contain $\mathcal{C}$, called the \textbf{$\sigma$-algebra generated by $\mathcal{C}$}.
\end{defn}

\end{document}


\subsection{Schrodinger}

\subsubsection{Discrete time}

With discrete time we can use a canonical operator for moving between discrete states in single jumps.

With discrete time there must a countable number of states.

We can index time to the integers.

At time \(0\) we have \(v\)

At time \(1\) we have \(\Psi v\)

At time \(2\) we have \(\Psi \Psi v\)

We can write this as \(\Psi (t_1,t_0)=\Psi ^{t_1-t_0}\)


\subsection{Implementation}
The fact that there is an unclear definition of a malicious webpage makes the implementation of a malware detection system quite challenging.

\subsubsection{Frequency of the keyword}

In this approach HTML malware detection takes a URL and a keyword as an input. The HTML crawler scans the main webpage and counts the frequency of the given keyword in that webpage. Also, the crawler scans the contents of the webpage for all the hyperlinks in the main webpage. The scanner then computes the ratio as showed below;

Ratio = Total number of keywords within the main web page / Total number of keywords in the inner hyperlinked web page

There are three possible ratio result as shown below:
\begin{enumerate}
\item
$Ratio = 0$\\
The zero ratio indicates the keyword does not exist in the main web page. Therefore the attackers used, a trendy keyword in the components of the given URL to misdirect the user to the malicious webpage.
\item
$Ratio = infinity$\\
According to the ratio equation, there is no instance of a given keyword within the inner hyperlinked web page. Therefore, the contents of the inner hyperlink web page are not relevant to the content of the main web page. Hence, the given URL is deemed malicious.
\item
$Ratio < 1$\\
That means the number of trendy keyword appears in the main page is more than or equal to the total number of trendy keyword in the web page inner links. In the other words, the contents of the inner links web page is less relevant to the contents of the main web page. In this case the given URL has identified as a malicious URL.
\item
$0 < Ratio < 1$\\
If the ratio is closer to zero that means the number of keywords appearing in the inner hyperlinked web pages are greater than the number of the keywords on the main web page. Hence, a smaller ratio indicates a higher probability that a given URL is benign. Accordingly, a large ratio (ratios close to one) has a greater probability that the given URL is malicious.
\end{enumerate}
\paragraph{}
The prima facie weakness of this approach is the risk that a malware writer will insert many instances of trendy keyword terms within the inner hyperlinked web page and instead add malicous hyperlinks from within the inner hyperlinked webpage to another malicious websites or perhaps even from a further removed linked website. Thus, a malware writer could use an iterative process to manipulate the ratio result.To avoid this problem, there is similarly a iterrative process for checking and calculating a ratio at each instance when there are links to another webpage. Hence, the ratio may not inidicate a malicious web page at the first few iterations but then identify a malicious website after checking the last inner hyperlinks. Adapting this method has helped to overcome the major problem for designing HTML malware scanner, because it is independent of the malware definition. In additions, the method is fairly fast.