\documentclass[10pt]{article}

\usepackage{haskell}
\usepackage{proof}
\usepackage{fullpage}

\parskip=\medskipamount
\parindent=0pt

\bibliographystyle{plain}

\newcommand{\isa}{\ensuremath{\; {:}{:}{=} \;}}
\newcommand{\ora}{\ensuremath{\;\mid\;}}

\title{EECS 762 - Project 1 Solution}
\author{Perry Alexander \\
  The University of Kansas EECS Department\\
  \texttt{alex@ittc.ku.edu}}

\begin{document}

\maketitle

\section{Introduction}

The objective of Project 1 is to write an interpreter for untyped
calculus expressions as defined in \emph{Types and Programming
  Languages}~\cite{Pie02a}, Chapter 5, Figure 5-3.  This language,
called, $\lambda$, is the most basic form of the lambda calculus.  The
definition of the abstract syntax provides the following two forms
for terms and values:

\begin{eqnarray*}
  t & \isa & x \ora \lambda x.t \ora t\; t \\
  v & \isa & \lambda x.t \\
\end{eqnarray*}

The definition for call-by-value evaluation provides the following
three evaluation rules:

\[\vcenter{\infer[\textsc{E-App1}]{t_1 t_2 \longrightarrow t_1^{'} t_2}{t_1\longrightarrow t_1^{'}}}\]

\[\vcenter{\infer[\textsc{E-App2}]{t_1 t_2 \longrightarrow t_1 t_2^{'}}{t_2\longrightarrow t_2^{'}}}\]

\[\vcenter{\infer[\textsc{E-AppAbs}]{(\lambda x.t_{12})v_2 \longrightarrow [x\rightarrow v_2]t_{12}}{}}\]

Our objective is to: (i) define a data structure for representing
$\lambda$ terms embodying the abstract syntax; and (ii) an evaluation
function embodying the evaluation rules for call-by-value and
call-by-name interpretation.

\section{UntypedLambda Module}

\begin{code}
  module UntypedLambda ( Term(..) )
      where
\end{code}

The \texttt{UntypedLambda} module provides the basic definitions for
manipulating lambda calculus expressions.  The data type representing
terms is first defined, followed by the evaluation functions.  The data
type for expressions provides program representations for each of the
three abstract syntax forms defined for $\lambda$.  The evaluation
functions provide both call-by-value and call-by-name evaluation
capabilities.

\subsection{Data Types}

An abstract type, \texttt{Term}, is defined to represent $lambda$
terms for evaluation.  The implementation of the \texttt{Term} type
either represents a legal term or a form that cannot be evaluated.

\begin{code}
  data Term =
      Var Int |
      Lambda Term |
      App Term Term |
      CantEval String
      deriving (Eq,Show)

  isApp :: Term -> Bool
  isApp (App _ _) = True
  isApp (Lambda _) = False
  isApp (Var _) = False
  isApp (CantEval _) = False
\end{code}

The \texttt{Var} constructor identifies the index for a variable and
corresponds with the $x$ form in the abstract syntax.  The
\texttt{Lambda} constructor defines an abstraction by specifying a
term and corresponds with the $\lambda x.t$ form in the abstract
syntax.  The \texttt{App} constructor defines the application of one
term to another and corresponds with the \texttt{t t} form in the
abstract syntax.  The \texttt{CantEval} constructor identifies a form
that cannot be evaluated.  It's argument is a \texttt{String}
representing the form or an error message.

The \texttt{CantEval} constructor is necessary due to the need for
evaluation functions to return both terms and error messages.  Unlike
the ML implementation, exceptions are not used to break out of an
iterative evaluation process.  Instead, evaluation will be done
recursively and the \texttt{CantEval} constructor used to identify
error situations.

The evaluation functions defined later will accept a context as one
parameter.  For closed $\lambda$ expressions, the context value is not
necessary.  For future evaluators, the context may be required, thus a
placeholder is provided.  The context type, \texttt{Ctx}, is defined
to be the unit type whose only member is the unit value:

\begin{code}
  type Ctx = ()
\end{code}

Note that you could safely leave out context handling for this project
entirely without penalty.

\subsection{Shifting and Substituting}

Our implementation uses de Brujin indexes as a basis for
representation and evaluation.  Thus, definitions of \texttt{shift}
and \texttt{subst} are required to implement elements of the evaluation
function.

The \texttt{shift} definition provides a case for shifting over each
valid $\lambda$ form as defined in the \texttt{Term} data type.  The
\texttt{CantEval} constructor is not included here because it will
never be evaluated.  This definition follows directly from the
standard definition of shift from TPL Chapter 8:

\begin{code}
  shift :: Term -> Int -> Int -> Term
  shift (Var x) c d = if x < c then (Var x) else (Var (x+d))
  shift (Lambda t) c d = (Lambda (shift t (c+1) d))
  shift (App t1 t2) c d = (App (shift t1 c d) (shift t2 c d))
\end{code}

Both \texttt{Lambda} and \texttt{App} terms are shifted by shifting
their terms.  Shifting a \texttt{Var} term requires application of the
definition from Chapter 8.  Specifically, the index for a variable is
incremented by \texttt{d} if its index is greater than \texttt{c}.

The \texttt{subst} definition again provides a case for substitution
over each value $\lambda$ form other than \texttt{CantEval}as defined
in the \texttt{Term} data type.  This definition follows directly from
the standard definition of substitution from TPL Chapter 8:

\begin{code}
  subst :: Int -> Term -> Term -> Term
  subst j s (Var x) = if x==j then s else (Var x)
  subst j s (Lambda t) = (Lambda (subst (j+1) (shift s 0 1) t))
  subst j s (App t1 t2) = (App (subst j s t1) (subst j s t2))
\end{code}

\subsection{Call By Value Evaluation Function}

The \texttt{evalValue} function provides a standard definition of
call-by-value evaluation following from TPL Chapter 5.  Both
\texttt{evalValue} and the later definition of \texttt{evalLazy}
define $t\longrightarrow^* t'$ rather than $t\longrightarrow t'$.  The
function definition is split up into cases corresponding to the
evaluation rules.  Note that only \texttt{App} forms can be evaluated.
\texttt{Lambda} forms are values and \texttt{Var} forms are not
closed.  Although passing a context is allowed in this function, in
this project we are concerned only with close terms.  Thus, the
context can be safely ignored in all evaluation cases.

The form of the evaluation function is a mapping from \texttt{Ctx} and
\texttt{Term} to another \texttt{Term}

\begin{code}
  evalValue :: Ctx -> Term -> Term
\end{code}

The first case to handle is \textsc{E-AppAbs} that evaluates an
application.  Both terms in the abstraction must be values.  Because
all \texttt{Lambda} forms are values, pattern matching is used to
assure values for this particular case.  The definition of evaluation
is taken directly from TPL Chapters 7 and 8:

\begin{code}
  evalValue _ (App (Lambda t12) (Lambda v2)) =
        let nt = shift (subst 0 (shift (Lambda v2) 0 1) t12) 0 (-1) in
            if (isApp nt) then evalValue () nt else nt
\end{code}

The \texttt{let} form performs single-step evaluation on the
\texttt{App} construct.  The \texttt{if} form following determines if
the resulting expression is a value.  If it is, evaluation terminates
tha the resulting structure is returned.  If it is not, the result is
evaluated recursively.  If the $t\longrightarrow t'$ form is required,
the \texttt{let} should be removed and only one evaluation step
performed.

The evaluation rule \textsc{E-App1} is used when the first term of the
application is not a value.  It is handled by two sub-cases
corresponding with \texttt{App} and \texttt{Var} forms.  In the first
sub-case, the first term is an application and is evaluated before
evaluating the outer application.  In the second sub-case, the first
term is a variable and is again evaluation before evaluating the outer
application.

\begin{code}
  evalValue c (App (App t11 t12) t) =
      let nt = evalValue c (App t11 t12) in evalValue c (App nt t)
  evalValue c (App (Var x) t) =
      let nt = evalValue c (Var x) in evalValue c (App nt t)
\end{code}

The second sub-case will always lead to an error in this
implementation, but future implementations may not have this
restriction.  Thus, it is defined strictly rather than short
circuiting and generating an error message directly.

The evaluation rule \textsc{E-App2} is used when the first term of the
application is a value, but the second is not.  It is again handled by
two sub-cases that correspond to the same cases as \textsc{E-App1},
except the second term is being evaluated:

\begin{code}
  evalValue c (App (Lambda t12) (App t21 t22)) =
      let nt = evalValue c (App t21 t22) in evalValue c (App t21 nt)
  evalValue c (App (Lambda t12) (Var x)) =
      evalValue c (App (Lambda t12) (evalValue c (Var x)))
\end{code}

The previous cases of \texttt{evalValue} account for the successful
application of evaluation rules.  However, error cases and the case
for evaluating values remain to be defined.

In $\lambda$, all lambda expressions are treated as values.  By
definition, a value cannot be further evaluated.  Thus, evaluating a
\texttt{Lambda} form results in the \texttt{Lambda} itself:

\begin{code}
  evalValue _ (Lambda t) = (Lambda t)
\end{code}

With the evaluation of values defined, the only remaining issue is the
error case caused when a free variable is evaluated.  Three cases of
\texttt{evalValue} handle the generation of the error and propagation
back through the recursive invocation of \texttt{evalValue}:

\begin{code}
  evalValue _ (Var x) = (CantEval (show (Var x)))
  evalValue _ (App (CantEval x) t) = (CantEval x)
  evalValue _ (App t (CantEval x)) = (CantEval x)
\end{code}

The first case generates the error when a free variable is evaluated.
It converts the variable into a string and uses the \texttt{CantEval}
constructor to generate an error.  The error is propagated back
through the recursion using the two following cases.  Together they
state that the result of evaluating an \texttt{App} is
\texttt{CantEval} if either of its terms are \texttt{CantEval}.

\subsection{Call By Name Evaluation Function}

Call-by-name evaluation uses the same definition of terms and values
as call-by-value.  However, two different evaluation rules define the
evaluation relation:

\[\vcenter{\infer[\textsc{E-App1}]
  {t_1 t_2 \longrightarrow t_1^{'} t_2}
  {t_1\longrightarrow t_1^{'}}}\]

\[\vcenter{\infer[\textsc{E-AppAbs}]
  {(\lambda x.t_{12})t_2 \longrightarrow [x\rightarrow t_2]t_{12}}{}}\]

The \textsc{E-App1} rule is identical to the rule for call-by-value.
However, the \textsc{E-AppAbs} rule does not require the argument to
the \texttt{Lambda} to be a value and the old \textsc{E-App2}
evaluation rule is no longer needed.

The \texttt{evalLazy} function provides a standard definition of
call-by-name evaluation following from TPL problem solutions.  Again,
$t\longrightarrow^*t'$ is implemented rather than a single step
evaluation function.  The function definition is split up into cases
corresponding to the evaluation rules identically to
\texttt{evalValue}.  Again, only \texttt{App} forms can be evaluated,
\texttt{Lambda} forms are values and \texttt{Var} forms are not closed
and thus not considered.

\begin{code}
  evalLazy :: Ctx -> Term -> Term
\end{code}

The first case to handle is \textsc{E-AppAbs} that evaluates an
application.  The first term must be a value.  In call-by-name
evaluation, it is not necessary for the second term to be a value.
Thus, the pattern match to assure that the second term is a
\texttt{Lambda} is removed.  The definition of evaluation is taken
directly from TPL Chapters 7 and 8:

\begin{code}
  evalLazy _ (App (Lambda t12) t2) =
     let nt = shift (subst 0 (shift t2 0 1) t12) 0 (-1) in
         if (isApp nt) then (evalLazy () nt) else nt
\end{code}

The second and third cases occur when the first \texttt{App} term is
not a value.  The first case handles when the first term is an
\texttt{App} form and the second when the first term is a \texttt{Var}
form.  These cases are virtually identical to those for call-by-value.

\begin{code}
  evalLazy c (App (App t11 t12) t) =
      let nt = evalLazy c (App t11 t12) in evalLazy c (App nt t)
  evalLazy c (App (Var x) t) =
      let nt = evalLazy c (Var x) in evalLazy c (App nt t)
\end{code}

Both cases for \textsc{E-App2} from the call-by-value implementation
are no longer needed because the \textsc{E-App2} rule is gone.  The
\textsc{E-AppAbs} cases will match when \textsc{E-App2} used to
because the second term in the \texttt{App} need not be a value.

Like call-by-value, \texttt{Lambda} forms are values.  Thus,
evaluating them in the call-by-name scheme is identical to
call-by-value.

\begin{code}
  evalLazy _ (Lambda t) = (Lambda t)
\end{code}

Error cases for call-by-name are a subset of error cases for
call-by-value.  Evaluating a free variable still generates an error
and an error occurring during evaluation of the first \texttt{App} term
is propagated back.  However, in call-by-name evaluation the second
term is not evaluated until necessary and the error case from
call-by-value does not occur.

\begin{code}
  evalLazy _ (Var x) = (CantEval (show (Var x)))
  evalLazy _ (App (CantEval x) t) = (CantEval x)
\end{code}

\section{Testing and Evaluation}

To test the interpreter, some functions from the book are provided
here.  The include the identity combinator, the omege combinator,
Church Boolean functions, some Church Numbers and the successor
function defined for Church numbers.

\subsection{Identity Combinator}

\begin{code}
  ident :: Term
  ident = (Lambda (Var 0))
\end{code}

\subsection{Church Booleans}

\begin{code}
  tru :: Term
  tru = (Lambda (Lambda (Var 1)))
  fls :: Term
  fls = (Lambda (Lambda (Var 0)))
\end{code}

\subsection{Church Numbers}

\begin{code}
  c0 :: Term
  c0 = (Lambda (Lambda (Var 0)))

  c1 :: Term
  c1 = (Lambda (Lambda (App (Var 1) (Var 0))))

  c2 :: Term
  c2 = (Lambda (Lambda (App (Var 1) (App (Var 1) (Var 0)))))

  scc :: Term
  scc = (Lambda
         (Lambda
          (Lambda (App (Var 1)
                   (App (Var 2)
                    (App (Var 1) (Var 0)))))))
\end{code}

\subsection{The Omega Combinator}

The omega combinator has the following form:

\begin{code}
  omega :: Term
  omega = (Lambda (App (Var 0) (Var 0)))
\end{code}

\subsection{Simple Test Cases}

This code has not been extensively tested, but some simple tests have
been performed.  Tests to try on Church Booleans include:

\begin{code}
  testTru :: Term
  testTru = (App (App tru ident) fls)

  testFls :: Term
  testFls = (App (App fls fls) ident)
\end{code}

A simple example of where call-by-value and call-by-name evaluation
differ occurs when the \texttt{omega} combinator is used as the false
argument to the \texttt{tru} combinator:

\begin{code}
  testCase1 :: Term
  testCase1 = (App (App tru ident) (App omega omega))
\end{code}

When evaluated with \texttt{evalValue}, this form does not terminate
because \texttt{(App omega omega)} must be evaluated before
\texttt{tru} can be evaluated.  When evaluated with \texttt{evalLazy},
the false case of the \texttt{tru} combinator is not evaluated because
it is not used.  Thus, the test case terminates as expected as the
identity combinator.

\bibliography{types,prog-langs}

\end{document}


\documentclass[10pt,a4paper,notitlepage]{article}
\usepackage[ngerman,english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[%right=2cm, left=2.5cm,
 width=16cm,
 bottom=3cm, footskip=1.3cm]{geometry} %A4: 21cm x29,7cm
\usepackage{amsmath}
\usepackage{esdiff}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{caption}
\usepackage{hyperref}
\usepackage{siunitx}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{titling}
\usepackage{lipsum} % for filler text
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{} % clear all header fields
\renewcommand{\headrulewidth}{0pt} % no line in header area
\fancyfoot{} % clear all footer fields
\fancyfoot[LE,RO]{\thepage}           % page number in "outer" position of footer line
\fancyfoot[RE,LO]{h.altug.yildirim@fu-berlin.de - u.kamber@science.ru.nl} % other info in "inner" position of footer line

\fancypagestyle{plain}{%
  \fancyhf{}%
  \fancyfoot[R]{\thepage}%
  \renewcommand{\headrulewidth}{0pt} % no line in header area
  \fancyfoot{} % clear all footer fields
  \fancyfoot[LE,RO]{\thepage}           % page number in "outer" position of footer line
  \fancyfoot[RE,LO]{h.altug.yildirim@fu-berlin.de - u.kamber@science.ru.nl} % other info in "inner" position of footer line
}

\newcommand{\rulesep}{\unskip\ \vrule\ }

\title{Lock-in Amplifier Operation for $dI/dV$ Mapping via Scanning Tunneling Microscopy\vspace{1cm}}

\author{Haydar Altu\u{g} Y{\i}ld{\i}r{\i}m, Umut Kamber}
\begin{document}
\date{24.02.2015}
\maketitle{}
\tableofcontents
\section{Overview}
Usage of lock-in amplifier theory and amplifiers are common in the
experimental science as a measurement technique in noisy environments. Basically, a
reference signal is introduced to an experimental parameter and a filter is used to
extract the signal with same modulation frequency.

In the case of Scanning Tunneling Microscopy (STM) measurements, this
operation is used as a way to map the differential conductivity ($dI/dV$) of the
specimen at a constant energy. In this work we discuss the principal theory behind
this operation.
In the figure \ref{lockin}, the general schematics of the experimental setup can be seen.

\begin{figure}[!ht]
  \centering
  \includegraphics[width=0.4\textwidth]{images/lockin.png}
  \caption{Depiction of the experimental setup with STM and lock-in detection. A wave generator is
used to oscillate the $V_{bias}$ of the system and this cause an oscillation of the $I_{tunneling}$ between the
surface and tip. The lock-in amplifier multiplies this signal with the reference signal coming from the
wave generator to detect the quantity of signal at the exact modulation frequency.}
  \label{lockin}
\end{figure}

\section{Analysis}

In this model we assume our wave generator produce perfect sine wave with the value of $U_0sin(2 \pi ft)$.

\begin{equation}
\label{signal}
\begin{split}
U(t)=V_{bias}+U_0sin(2 \pi ft)
\end{split}
\end{equation}

This signal as equation \ref{signal} suggests, added to $V_{bias}$ . Also it runs through the
lock-in amplifier as a reference signal. It should be noted that the frequency can
contain higher harmonics of the $f$. The $nxf$ indicates this.
In STM, the tunneling current that caused by the quantum tunneling
phenomena is a function of the bias voltage. And with the addition of the driving
signal this $V_{bias}$, it is now become a function time,

\begin{equation}
\label{itunnel}
\begin{split}
I_{tunneling}=I_{tunneling}(V_{bias})=I_{tunneling}(V_{bias}(t))
\end{split}
\end{equation}

It should also be noted that, the signal that run through the lock-in amplifier is
not the $I_{tunneling}$ but the voltage value that converted from $I_{tunneling}$ in the STM
system with an I-V converter.

After two signals fed to the lock-in amplifier, the operation performed. The
operation is basically multiplying the two signal ad then integrated over a longer time
than the periodicity of the signals. The lock-in operation is;

\begin{equation}
\label{liop}
\begin{split}
S_n=\frac{1}{T}\int_{o}^{T} sin(2 \pi ft+\delta \phi)I_{tunneling}(t)dt
\end{split}
\end{equation}

where $T=\frac{m}{f}$, while $m$ is an integer $\gg 1$, indicates relatively long time. And $\delta \phi$ is
phase shift, possibly from different signal path of variables.
In order to perform this operation we need the Taylor expansion of the
$I_{tunneling}$. From Taylor expansion $I_{tunneling}(V_{bias})$ around $U(t=0)=V_{bias}$;

\begin{equation}
\label{}
\begin{split}
I_t(V)=\sum_{\alpha=0}^{\infty}\frac{1}{\alpha!}I^{(\alpha)}(V-V_0)^\alpha \\
I^{(\alpha)}=\left(\left(\frac{\partial}{\partial V}\right)^\alpha I_t\right)_{V=V_0}
\end{split}
\end{equation}

Also we expand the bias voltage as a function of time into its Fourier series to
be able to multiply it with Taylor expansion of the tunneling current to check
orthogonality.

\begin{equation}
  \label{}
  V(t)=\sum_{\mu=0}^{\infty}V_\mu sin(2 \pi f \mu t + \delta \phi_\mu)
\end{equation}

In order to avoid further complications, we will use abbreviation instead of the
sine values;

\begin{equation}
  s_\mu=sin(2 \pi f \mu t+\delta \phi_\mu)
\end{equation}

Then, the Fourier series of the bias voltage is;

\begin{equation}
  V(t)=\sum_{\mu=0}^{\infty} V_\mu s_\mu
\end{equation}

From Taylor expansion;

\begin{equation}
  I(t)=\frac{1}{0!}I(V-V_0)^0+\frac{1}{1!} \left(\frac{\partial I}{\partial V}\right)_{V_0} (V-V_0)^1 +\frac{1}{2!} \left(\frac{\partial^2 I}{\partial V^2}\right)_{V_0} (V-V_0)^2 + \ldots
\end{equation}

And using only the first two harmonics;

\begin{equation}
  V(t)=V_0+V_1 s_1
\end{equation}

The reason that just $\mu=0.1$ cases were used is because we add just one extra
signal to our bias voltage. Even this is not the case the physical parameters mostly
have negligible magnitude at higher harmonics.

If we explicitly write the Taylor expansion of current by taking into account
the voltage;

\begin{equation}
  \label{taylorvoltage}
  \begin{split}
    I(t)=(I)_{V_0}+\left(\frac{\partial I}{\partial V}\right)_{V_0} (V_0+V_1 s_1-V_0) +\frac{1}{2} \left(\frac{\partial^2 I}{\partial V^2}\right)_{V_0} (V_0+V_1 s_1-V_0)^2 \\
    =(I)_{0}+\left(\frac{\partial I}{\partial V}\right)_{V_0} V_1 s_1 +\frac{1}{2} \left(\frac{\partial^2 I}{\partial V^2}\right)_{V_0} {V_1}^2 {s_1}^2
  \end{split}
\end{equation}

From equation \ref{liop} and \ref{taylorvoltage};

\begin{equation}
  \begin{split}
  S_n=\frac{1}{T}\int_{0}^{T}s_1 \left[I_{0}+\left(\frac{\partial I}{\partial V}\right)_{V_0} V_1 s_1 +\frac{1}{2} \left(\frac{\partial^2 I}{\partial V^2}\right)_{V_0} {V_1}^2 {s_1}^2 \right]\partial t \\
  =\frac{1}{T}\int_{0}^{T} \left[I_{0}s_1+\left(\frac{\partial I}{\partial V}\right)_{V_0} V_1 {s_1}^2 +\frac{1}{2} \left(\frac{\partial^2 I}{\partial V^2}\right)_{V_0} {V_1}^2 {s_1}^3 \right]\partial t \\
\end{split}
\end{equation}

We are going to introduce the trigonometric identity in order to evaluate the
sinusoidal multiplications;

\begin{equation}
  sin(x)sin(y)=\frac{1}{2} \left(sin \left(x-y+\frac{\pi}{2}\right)+ sin \left(x+y+\frac{\pi}{2}\right) \right)
\end{equation}

Just consider first two term;

\begin{equation}
  S_n=\frac{1}{T} \int_{0}^{T} \left[I_0 sin\left(2 \pi f t + \delta \phi_1 \right) + \frac{1}{2} \left(\frac{\partial I}{\partial V} \right)_{V_0} V_1 \left(sin \left(\frac{\pi}{2}\right)+sin \left(4 \pi f t + 2 \delta \phi_1 + \frac{\pi}{2} \right) \right) \right] \partial t
\end{equation}

First part of the integral;

\begin{equation}
  \int_{0}^{T} I_0 sin(2 \pi f t + \delta \phi_1) \partial t = \frac{I_0}{2 \pi f} \left(cos(\delta \phi_1)-cos(2 \pi f T + \delta \phi_1) \right)
\end{equation}

And the second part;

\begin{equation}
  \begin{split}
  \frac{1}{2} \int_{0}^{T} \left(\frac{\partial I}{\partial V}\right)_{V_0} V_1 \left(sin\left(\frac{\pi}{2}\right)+sin \left(4 \pi f t +2 \delta \phi_1 +\frac{\pi}{2} \right)\right) \partial t \\
  =\frac{1}{2} \left(\frac{\partial I}{\partial V}\right)_{V_0} V_1 T + \frac{1}{2} \left(\frac{\partial I}{\partial V}\right)_{V_0} V_1 \frac{1}{2 \pi f}\left[cos \left(2 \delta \phi_1 + \frac{\pi}{2}\right)-cos \left(4 \pi f t +2 \delta \phi_1 +\frac{\pi}{2}  \right) \right]
\end{split}
\end{equation}

Thus;

\begin{equation}
  \begin{split}
  S_n=\frac{I_0}{2 \pi f T}\left[cos(\delta \phi_1)-cos(2 \pi f T + \delta \phi_1) \right] + \frac{1}{2} \left(\frac{\partial I}{\partial V}\right)_{V_0} V_1 \\
   + \frac{1}{2} \left(\frac{\partial I}{\partial V}\right)_{V_0} V_1 \frac{1}{2 \pi f T} \left[cos \left(2\delta \phi_1 + \frac{\pi}{2}\right)- cos \left(4 \pi f T + 2\delta \phi_1 + \frac{\pi}{2} \right) \right]
\end{split}
\end{equation}

If initial phase difference is equal to zero $(\delta\phi_1=0)$, and we know $T=\frac{m}{f}$ while $m$ is an integer $\gg 1$;

\begin{equation}
  \begin{split}
  S_n=\frac{I_0}{2 \pi f T}\left[1-cos(2 \pi m) \right] + \frac{1}{2} \left(\frac{\partial I}{\partial V}\right)_{V_0} V_1 \\
   + \frac{1}{2} \left(\frac{\partial I}{\partial V}\right)_{V_0} V_1 \frac{1}{2 \pi f T} \left[cos \left(\frac{\pi}{2}\right)- cos \left(4 \pi m + \frac{\pi}{2} \right) \right]
\end{split}
\end{equation}

\begin{equation}
  \begin{split}
  S_n=\frac{V_1}{2}\left(\frac{\partial I}{\partial V}\right)_{V_0} \left(1-\frac{1}{wT} \right)
\end{split}
\end{equation}

The expression $\left(\frac{\partial I}{\partial V}\right)_{V_0}$ is crucial in here, which is the differential conductivity at constant bias voltage which concludes our efforts.

\section{Acknowledgements}

This work is written under the guidance of the Assoc. Prof. O\u{g}uzhan G\"{u}rl\"{u}.
We used Ralf Vogelgesang’s “Lock-In Amplifier Theory” whitepaper \cite{ralf} extensively as the
main source of this work. We also would like to thank our lab mates at NanoBEEs of Istanbul Technical University for the patience and attention, when we lecture this work.

\bibliographystyle{unsrt}
\bibliography{biblio}

\end{document}


\hypertarget{quantum-cryptography}{%
\section{Quantum Cryptography}\label{quantum-cryptography}}

\textbf{Experiment with Electrons}:\\
Elektronen sind eigentlich Teilchen und sollten sich wie solche
Verhalten. Im Experiment verhalten sich die Elektronen jedoch nicht wie
Teilchen. Werden die Elektronen jedoch beim Experiment beobachtet (ein
Observer bei Loch 1 \& 2), dann verhalten sich die Elektronen plötzlich
wie normale Teilchen. Es ist also nicht möglich, das Experiment zu
beobachten, ohne das Verhalten der Elektronen zu verändern.

\begin{figure}[H]
\centering
\includegraphics[width=0.8\textwidth]{figures/experimentElectron.png}
\caption{Experiment with Electrons}
\end{figure}

\hypertarget{photonen}{%
\subsection{Photonen}\label{photonen}}

Ein Photon ist die minimalste Form eines elektronischen Impulses. So ein
Photon kann man mit einem Filter polarisieren. Ein Photon das horizontal
polarisiert ist, kommt nicht durch einen vertikalen Polarisationsfilter.
Es hat jedoch eine 50\% Chance, durch einen 45° polarisationsfilter zu
kommen. Und wenn es 45° polarisiert ist, natürlich wieder eine 50\%
Chance durch einen vertikalen Filter zu kommen.

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/photonExperiment1.png}
\caption{Experiment with Photon 1}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/photonExperiment2.png}
\caption{Experiment with Photon 2}
\end{figure}

\hypertarget{definitionen}{%
\subsubsection{Definitionen}\label{definitionen}}

\textbf{Ket:} Beschreibung des Zustands vom Quantum

\textbf{Braket:} Die Wahrscheinlichkeitsamplitude in einer komplexen
Zahl. Sie zeigt die Wahrscheinlichkeit an, ob sich das Quantum von
Zustand 1 in Zustand 2 verwandelt.

\textbf{Wahrscheinlichkeit:} Die findet man, wenn man den Betrag im
Quadrat nimmt.

Beispiel dieser Definitionen im Polarisations-Experiment:

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/polarizationExample.png}
\caption{Beispiel in der Polarization von Photonen}
\end{figure}

\hypertarget{conclusion}{%
\subsubsection{Conclusion}\label{conclusion}}

Das Photon befindet sich in einer sogenannten Superposition, also im
Zustand beschrieben durch einen horizontalen Zustand UND einen
vertikalen Zustand.

\begin{itemize}
\item
  With respect to our measuring system with the basis $\{|{}\updownarrow  \rangle, |{}\leftrightarrow \rangle \}$ (vertical, horizontal) we must describe the photon polarized at an angle of 45° as:

  \begin{itemize}
  \tightlist
  \item
    \textbar{}45°$\rangle = \cos(45)*|{}\leftrightarrow\rangle + \sin(45)*|{}\updownarrow\rangle$
  \end{itemize}
\item
  The photon is in a superposition state and will make up its mind at
  the moment of the measurement whether it will pass through the filter
  or not. This decision is completely unpredictable!

  \textbf{As long as we do not make a measurement, the photon is in both
  states!}
\end{itemize}

\hypertarget{superposition}{%
\subsubsection{Superposition}\label{superposition}}

\begin{itemize}
\tightlist
\item
  Two orthogonal base vectors $|{}0\rangle$ und
  $|{}1\rangle$ $\Rightarrow$ Zwei Zustände, müssen keine Vektoren
  sein, aber Komplexe Zahlen
\item
  General form of a quantum mechanic superposition
  \begin{itemize}
  \tightlist
  \item
    $|{}\psi\rangle = a*|{}0\rangle + b*|{}1\rangle$
  \item
    a, b: complex coefficients (amplitudes)
  \end{itemize}
\item
  Measurement with respect to base $|{}0\rangle$ or $|{}1\rangle$

  \begin{itemize}
  \tightlist
  \item
    Probability for $|{}0\rangle = |a|^2$
  \item
    Probability for $|{}1\rangle = |b|^2$
  \item
    Scaling: $|a|^2 + |b|^2 = 1$
  \item
    \textbf{Das Quantum wird mit 100\% Wahrscheinlichkeit entweder 0 oder
    1 sagen.}
  \end{itemize}
\item
  The system described by $|{}\psi\rangle$ is in both states $|{}0\rangle$ und $|{}1\rangle$ at the same time
\end{itemize}

\hypertarget{entangled-states-verschruxe4nkter-zustand}{%
\subsection{Entangled States (Verschränkter
Zustand)}\label{entangled-states-verschruxe4nkter-zustand}}

\begin{itemize}
\tightlist
\item
  Several quanta can share a common state
\item
  The state of the individual quantum is not defined at the time of
  synthesis of the quanta. It will be established at the time of the
  measurement.
\item
  We have to conlcude that quantum mechanics is not local, because a
  measurement at one quantum instantaneously affects the state of
  another quantum, even if both quanta are far from each other.
\end{itemize}

\hypertarget{entanglement}{%
\subsubsection{Entanglement}\label{entanglement}}

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/entanglement.png}
\caption{Entanglement}
\end{figure}

Sobald wir den einen Zustand des einen verschränkten Quantum messen und
sich der Zustand 1 herausstellt (genau in diesem Moment), ist der
Zustand des anderen Quantum in diesem Beispiel mit Sicherheit 0 (auch
wenn es Lichtjahre entfernt ist).\\
Es sind auch andere verschränkte Quanten möglich, z.B. mit 1a1b - 0a0b
$\Rightarrow$ also ist A 1, ist auch B 1.

\hypertarget{example}{%
\subsubsection{Example}\label{example}}

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/exampleEntanglement.png}
\caption{Example Entanglement}
\end{figure}

\hypertarget{hidden-variables}{%
\subsection{Hidden Variables}\label{hidden-variables}}

Assumption: Quanta do not decide randomly but due to some hidden
variables (that physically exist but that cannot not be measured).\\
$\Rightarrow$ Aber, die Quanten wissen nicht, was sie in Zukunft
gefragt werden. Diese Annahme kann also nicht stimmen.

\hypertarget{bells-inequality}{%
\subsection{Bell's Inequality}\label{bells-inequality}}

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/bellsInequality.png}
\caption{Bell's Inequality}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/bellsInequality2.png}
\caption{Bell's Inequality Extended}
\end{figure}

Dieses Experiment verletzt die Voraussetzung für eine Absprache bzw.
eine Strategie. Daher wissen wir, dass es keine versteckten Parameter
gibt.

\hypertarget{no-cloning-prinzip}{%
\subsection{No-Cloning Prinzip}\label{no-cloning-prinzip}}

\emph{Für die Kryptographie wichtig.}

\begin{itemize}
\tightlist
\item
  An unknown quantum state cannot be copied.
\item
  Otherwise it would be possible to transmit information faster than
  light!
\item
  This is a fundamental difference to the classical information
  processing
\item
  Important conclusion for cryptography: An attacker may not copy quanta
  with unknown states
\end{itemize}

\hypertarget{one-time-pad}{%
\subsection{One-Time Pad}\label{one-time-pad}}

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/oneTimePadQuantum.png}
\caption{One-time-Pad}
\end{figure}

Das Problem hier ist einfach, dass man diesen zufällig gewählten sehr
langen Schlüssel irgendwie dem Empfänger über einen unsicheren Kanal
mitteilen muss.

\hypertarget{quantum-key-distribution}{%
\subsection{Quantum Key Distribution}\label{quantum-key-distribution}}

Procedure to securely exchange a shared random secret key between to
entities - The secret key can subsequently be used for a symmetric
encryption scheme such as the one time pad - It either uses quantum
superposition or quantum entanglement to implement a communication
system that allows detection of eavesdropping - Its security relies on
the foundations of quantum mechanics - No-cloning principle -
Superposition - Entanglement

\hypertarget{bb84-protocol}{%
\subsection{BB84-Protocol}\label{bb84-protocol}}

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/bb84-protocol.png}
\caption{BB84 Protocol}
\end{figure}

\begin{itemize}
\tightlist
\item
  Alice transmits polarized photons to Bob. She chooses randomly,
  whether she uses a rectilinear (+) or a diagonal (×) base
\item
  Bob measures either with a rectilinear (+) or a diagonal (×) filter.
  If he chooses the wrong filter, he will get a random result
\item
  Afterwards, Alice and Bob will exchange (over an unsecure channel) the
  information, which base they used for every transmitted photon. If
  they used the same base, the result will be a valid key bit.
\end{itemize}

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/bb84InAction.png}
\caption{BB84 Protocol in Action}
\end{figure}

\clearpage

\hypertarget{quantum-key-distribution-with-entangled-photons-e91}{%
\subsection{Quantum Key Distribution with Entangled Photons
(E91)}\label{quantum-key-distribution-with-entangled-photons-e91}}

\begin{itemize}
\tightlist
\item
  A photon source emits a pair of entangled photons
\item
  Alice will receive photon A and randomly chooses one of the bases
  22.5°, 45° or 67.5° to perform a measurement.
\item
  Likewise, Bob receives photon B and chooses 0°, 22.5° or 45° for his
  measurement
\item
  After the transmission, Alice and Bob use an unsecure channel to
  exchange the information which base they used to measure each photon.

  \begin{itemize}
  \tightlist
  \item
    Alice and Bob used the same base. In this case, the results will be
    exactly contrary (if photon A passes Alice's filter then photon B
    will not pass Bob's filter and vice versa) and a valid symbol has
    been transmitted.
  \item
    The bases differ by 45°, yielding a completely random result. Theses
    symbols must be discarded.
  \item
    The bases differ by 22.5° or 67.5°. These measurements can be used
    to detect an eavesdropper. The probability that Alice and Bob obtain
    the same result depends on whether the photons are entangled or not.
    Any observation made by an eavesdropper will destroy the
    entanglement and thus change the probability.
  \end{itemize}
\end{itemize}

\begin{figure}[H]
\centering
\includegraphics[width=1\textwidth]{figures/e91.png}
\caption{E91}
\end{figure}

\clearpage


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS637: Database-Backed Websites
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 1}

Consider the variable \texttt{\$room\_preparing\_orders} in the code snippet given below. Answer the following questions based on model functions provided below.

\lstset{language=php}
\begin{lstlisting}
if ($action == 'student_welcome'|| $action == 'select_room') {
  try {
  $sizes = get_available_sizes();
  $toppings=  get_available_toppings();
  $room_preparing_orders=  get_preparing_orders_of_room($room);
  $room_baked_orders=  get_baked_orders_of_room($room);
  } catch (PDOException $e) {
    $error_message = $e->getMessage();
    include('../../errors/database_error.php');
    exit();
  }
  include('student_welcome.php');
}
\end{lstlisting}

\begin{enumerate}
\item Model function \texttt{get\_preparing\_orders\_of\_room(\$room)}
\begin{lstlisting}
function get_preparing_orders_of_room($room) {
    global $db;
    $query = 'SELECT * FROM pizza_orders where status=1 and room_number=:room';
    $statement = $db->prepare($query);
    $statement->bindValue(':room',$room);
    $statement->execute();
    $orders = $statement->fetchAll();
    $statement->closeCursor();
    $orders = add_toppings_to_orders($orders);
    return $orders;
}
\end{lstlisting}
\item Model function \texttt{add\_toppings\_to\_orders(\$orders)}
\begin{lstlisting}
function add_toppings_to_orders($orders) {
      for ($i=0; $i<count($orders);$i++) {
        $toppings = get_orders_toppings($orders[$i]['id']);
        $orders[$i]['toppings'] = $toppings; // add toppings to order
    }
    return $orders;
}
\end{lstlisting}
\item Model function \texttt{get\_orders\_toppings(\$order\_id)}
\begin{lstlisting}
function get_orders_toppings($order_id) {
    global $db;
    $query = 'select T.topping_name from toppings T,order_topping OT where OT.topping_id=T.id and OT.order_id=:order_id';
    $statement = $db->prepare($query);
    $statement->bindValue(':order_id',$order_id);
    $statement->execute();
    $toppings = $statement->fetchAll();
    $statement->closeCursor();
    return $toppings;
}
\end{lstlisting}
\end{enumerate}

\begin{enumerate}[label=(\alph*)]
\item Draw the data structure for \texttt{\$room\_preparing\_orders}. Assume two rows with two toppings and three toppings, respectively.

\item Write a fragment of PHP code to count the toppings across all the rows (maybe more than two) held in \texttt{\$room\_preparing\_orders}.
\end{enumerate}

\section*{Solution}

Based on provided model functions, the variable \$room\_preparing\_orders will be an associative array corresponding to the table resulting from the following MySQL query.
\lstset{language=SQL}
\begin{lstlisting}
SELECT PO.id, PO.size_id, PO.day, PO.status, T.topping_name
FROM pizza_orders PO, toppings T, order_topping OT
WHERE PO.status = 1
  AND PO.room_number = :room
  AND OT.topping_id = T.id
  AND PO.id = OT.order_id;
\end{lstlisting}

\begin{enumerate}[label=(\alph*)]
\item Data structure of the \texttt{\$room\_preparing\_orders} multi-dimensional array, holding to pizza orders with 2 and 3 toppings respectively, is given in Figure \ref{fig12}, in which cells numbers inside brackets represent their indices. Access to name of the first topping of the first item for instance is achieved by calling \texttt{\$room\_preparing\_orders[0][3][0]}. Noteworthy that \textit{order} and \textit{toppings} are for illustration and cannot be replaced with indices for fetching data.
\newpage
\begin{figure}[H]\centering
\begin{tikzpicture}[
  cell/.style={rectangle,draw,fill=blue!20,rounded corners=1mm,minimum width=3cm, minimum height=7.5mm},
  grandchild/.style={grow=down,xshift=1em,anchor=west, edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}},
  first/.style={level distance=6ex},
  second/.style={level distance=12ex},
  third/.style={level distance=18ex},
  fourth/.style={level distance=24ex},
  fifth/.style={level distance=30ex},
  level 1/.style={sibling distance=6cm}
  ]
  \coordinate
  child[level distance=0mm] {
  node[cell, anchor=east] {[0] order}
    child[grandchild, first] {node[cell] {[0] order\_id}}
    child[grandchild, second] {node[cell] {[1] size\_id}}
    child[grandchild, third] {node[cell] {[2] day}}
    child[grandchild, fourth] {node[cell] {[3] status}}
    child[grandchild, fifth] {
    node[cell] {[4] toppings}
      child[grandchild, first] {node[cell] {[0] topping\_name}}
      child[grandchild, second] {node[cell] {[1] topping\_name}}
    }
  }
  child[level distance=0mm] {
  node[cell, anchor=east] {[1] order}
    child[grandchild, first] {node[cell] {[0] order\_id}}
    child[grandchild, second] {node[cell] {[1] size\_id}}
    child[grandchild, third] {node[cell] {[2] day}}
    child[grandchild, fourth] {node[cell] {[3] status}}
    child[grandchild, fifth] {
    node[cell] {[4] toppings}
      child[grandchild, first] {node[cell] {[0] topping\_name}}
      child[grandchild, second] {node[cell] {[1] topping\_name}}
      child[grandchild, third] {node[cell] {[2] topping\_name}}
    }
  };
\end{tikzpicture}
\caption{Data structure of \texttt{\$room\_preparing\_orders}}\label{fig12}
\end{figure}
\item The following code snippet in PHP counts the number of toppings for each row (order) of \texttt{\$room\_preparing\_orders} variable with arbitrary number of rows.

\lstset{language=php}
\begin{lstlisting}
for ($i = 0; $i < sizeof($room_preparing_orders); $i++) {
  $num_toppings[$i] = sizeof($room_preparing_orders[$i][4]);
}
echo implode($num_toppings);
\end{lstlisting}

\end{enumerate}


%\documentclass{article} % For LaTeX2e
% from Jake Knigge
\RequirePackage[l2tabu, orthodox]{nag}	% tells out-of-date packages
%\documentclass[11pt]{article}
%\documentclass[12pt, letterpaper]{scrartcl}
\documentclass[10pt, letterpaper]{scrartcl}

\usepackage{amssymb,amsfonts}
\usepackage{amsthm} % if using theorems and proofs
\usepackage[cmex10]{amsmath}
\usepackage[usenames,dvipsnames,svgnames]{xcolor} % use like \textcolor{red}{..} or {\color{red} ...}
\usepackage{graphicx}
\usepackage[margin=1in]{geometry}% can also add 'nohead', or leave blank

%\usepackage{url}
%\usepackage{multirow}
%\usepackage{array} % IEEE recommends

\usepackage{cancel} % for canceling terms

\usepackage{booktabs}

\usepackage{pdfpages} % for including pdfs

\usepackage{algpseudocode}  %algorithmicx
\usepackage{wrapfig}

%\usepackage{listings}
\usepackage[numbered,framed]{matlab-prettifier}
\lstdefinestyle{mat}{
    frame=single,
    language=matlab,
    showstringspaces=false,
    %  keywordstyle=\bfseries\color{blue},
    %  commentstyle=\color{green},
    %  stringstyle=\color{magenta},
}
\lstset{
    style              = Matlab-editor,
    basicstyle         = \mlttfamily,
    escapechar         = ",
    mlshowsectionrules = true,
}
%% use withL: \lstinputlisting{myODEfunction.m}

%\renewtheorem{theorem}{Theorem}
%\usepackage{chngcntr}
%\counterwithin{equation}{section}
%\counterwithout{equation}{section}
%\counterwithout{Theorem}{section}

%\usepackage{pgf,tikz}
%\usetikzlibrary{arrows}
%\definecolor{ffqqqq}{rgb}{1.,0.,0.}
%\usepackage[caption=false]{subfig}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern} % important!

\usepackage{hyperref}
\hypersetup{pdfauthor={Stephen Becker},
    pdftitle={},
    colorlinks=true,
    citecolor=MidnightBlue,
    urlcolor=Bittersweet,
}
% -- Input macros and such --
\IfFileExists{custom_headers.tex}{
\input{custom_headers}}{
\input{../custom_headers}}{
}
\let\oldenumerate\enumerate
\renewcommand{\enumerate}{
  \oldenumerate
  \setlength{\itemsep}{1em}
  \setlength{\parskip}{1em}
  \setlength{\parsep}{0pt}
}
\usepackage{enumitem}
%\setlist{noitemsep}
% See http://mirrors.fe.up.pt/pub/CTAN/macros/latex/contrib/enumitem/enumitem.pdf
% and now, set par indent to zero
\newlength{\savedparindent}
\setlength{\savedparindent}{\parindent}
\setlist[enumerate]{listparindent=\savedparindent}
%\setlength{\parindent}{0pt} % Jan 2017, HW 3/4, comment this out.

\usepackage{xspace}
%\newcommand{\collaboration}{\textbf{Collaboration Allowed}\xspace}
%\newcommand{\nocollaboration}{\textbf{No Collaboration}\xspace}

\renewcommand{\T}{\mathbb{T}}
\newcommand{\fn}{\widehat{f}_n} % Fourier coefficients
\newcommand{\spi}{\frac{1}{\sqrt{2\pi}}}
\renewcommand{\H}{\mathcal{H}}
\renewcommand{\phi}{\varphi}
\DeclareMathOperator{\ran}{ran} % ker already defined, and with lower case

\newcommand*\Laplace{\mathop{}\!\mathbin\bigtriangleup}
\renewcommand{\SS}{\mathcal{S}} % Schwartz space

% My solution environment
%%\newenvironment{solution}{\setlength{\parindent}{\savedparindent}{\bfseries Solution}:}{}
\makeatletter
\newcommand\solParagraph{\@startsection{paragraph}{4}{\z@}%
%    {-3.25ex \@plus -1ex \@minus -0.2ex}%
    {-.55ex \@plus -1ex \@minus -0.2ex}%
    {0.01pt}%
    {\raggedsection\normalfont\sectfont\nobreak\size@paragraph}%
}
\makeatother
\newenvironment{solution}{\setlength{\parindent}{\savedparindent}\solParagraph{Solution:}}{}

%\newenvironment{solution}{\emph{Solution}:}{}
\newenvironment{instructions}{}{}
\newenvironment{SolnComment}{}{}
\usepackage{comment}

%\def\solutions{1}  % define solutions  % !!! COMMENT or UNCOMMENT THIS LINE

% NOTE: \begin{solution} should have NO SPACES BEFORE OR AFTER IT,
% 	Can give very weird errors, hiding text, etc. See http://tex.stackexchange.com/a/91431/4603
\ifdefined\solutions
    \newcommand{\solTitle}[1]{#1}
    \excludecomment{instructions}
\else
   \excludecomment{solution}% uncomment this line to hide solution
   \newcommand{\solTitle}[1]{}
   \excludecomment{SolnComment}
\fi

\title{Homework 3 \solTitle{Selected Solutions} \\APPM 4720/5720 Spring 2019 \\ Randomized Algorithms}
%\author{Stephen Becker}
\date{}
\begin{document}
\maketitle
\vspace{-6em}
\textbf{Due date}: Friday, Feb 1 2019

Theme: Background   \hfill Instructor: Stephen Becker %Dr.\ Becker
%\vspace{1em}

\begin{instructions}
\paragraph{Instructions}
Collaboration with your fellow students is allowed and in fact recommended, although direct copying is not allowed.  Please write down the names of the students that you worked with. The internet is allowed for basic tasks only, not for directly looking for solutions.

An arbitrary subset of these questions will be graded.

%\paragraph{Reading}
%Read the rest of chapters 9--11 in [BV2004].
%%Read chapter 3.1, 3.2 and 3.3 in [BV2004].
%%Students are \textbf{strongly advised} to skim appendices A and C in [BV2004] to look for unfamiliar material (and read in more detail if there is unfamiliar material)
%
%%\vspace{1em}
%%\textbf{Special instructions this week}:
%
\end{instructions}

\begin{enumerate}[align=left, leftmargin=*, label=\bfseries Problem \arabic*:]
    \item \ [READING]  The reading is
Petros Drineas and Michael W. Mahoney, \emph{Lectures on Randomized Numerical Linear Algebra}, The Mathematics of Data, IAS/Park City Math. Ser., vol. 25, Amer. Math. Soc., Providence, RI, 2018. These are the notes used in that summer school. An electronic copy is available for free at \url{http://arxiv.org/abs/1712.08880}.

Read the short introduction (section 1), and then skim section 2 on ``Linear Algebra'' and section 3 on ``Discrete Probability.'' If any of that material looks new to you, or you are rusty on it, read it in more detail.

\textbf{Deliverable}: Do the reading. You do not need to turn in anything.

    \item \ [PROGRAMMING] Let $U \in \R^{n \times 2}$ be a matrix where the first column is $\ln(1),\ln(2),\ldots,\ln(n)$ and the second column is
    $\ln(n+1),\ln(n+2),\ldots,\ln(2n)$.  This is the same matrix as in last week's homework, so you can re-use your code.

    \textbf{Deliverable}: For $n=10^5$, compute $\| UU^T\|$ to 4 significant digits (where $\| \cdot \|$ is the spectral norm)

    \emph{Hints}:
    You might find it helpful to compare with the naive implementation for a small $n$ to make sure your code is correct.

    On my laptop, the norm computation takes 0.05 seconds for $n=10^5$.

\begin{solution}
TBD
     % Matlab prettifier package
     % see https://mirror.hmc.edu/ctan/macros/latex/contrib/matlab-prettifier/matlab-prettifier.pdf
\begin{lstlisting}
% Code TBD
\end{lstlisting}
\end{solution}

    \item \ [PROGRAMMING]
    Request a research computing (RC) account from \url{https://rcamp.rc.colorado.edu/accounts/account-request/create/organization} or go to the main page \url{https://www.colorado.edu/rc/} and click ``Request an account to use RC Resources''. \textbf{This may take a few days, so do not leave this until the end of the week!}  If it asks you for a reason you are requesting the account, tell them it is for this class, and that you expect to use one of the applied math nodes on the ``Blanca'' cluster.
     Once you have the account, login to a login node, and look at the file \texttt{/rc\_scratch/stbe1590/secretMessage.txt}

    \textbf{Deliverable}: what is the message in the file \texttt{secretMessage.txt}?

     \emph{Hints}: you'll need to get an account, learn what the login node is, know very basic \texttt{ssh} commands, use the ``duo'' two-factor authentication app, do basic command line usage in a linux shell, and view the contents of a file (with \texttt{cat} or a text editor). Depending on your background, this ranges from trivial to overwhelming. If you are knew to these tools, I can give hands-on help in office hours, but please \textbf{read the documentation on the rc website first!} They have worked very hard to document every step of the process, so please take the time to read their instructions before you ask for help.

\end{enumerate}

\end{document}


\documentclass[11pt,a4paper]{article}

% These are extra packages that you might need for writing the equations:
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{booktabs}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{xcolor}
\lstset {language=C++,
		 basicstyle=\ttfamily,
         keywordstyle=\color{blue}\ttfamily,
         stringstyle=\color{red}\ttfamily,
         commentstyle=\color{purple}\ttfamily,
         morecomment=[l][\color{magenta}]{\#},
       	 basicstyle=\tiny}

% You need the following package in order to include figures in your report:
\usepackage{graphicx}

% With this package you can set the size of the margins manually:
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}

\begin{document}

% Enter the exercise number, your name and date here:
\noindent\parbox{\linewidth}{
 \parbox{.25\linewidth}{ \large ICP, Exercise 06 }\hfill
 \parbox{.5\linewidth}{\begin{center} \large Beat Hubmann \end{center}}\hfill
 \parbox{.2\linewidth}{\begin{flushright} \large Nov 7, 2018 \end{flushright}}
}
\noindent\rule{\linewidth}{2pt}

\section{Introduction}

The single spin flip Metropolis algorithm was implemented as an example Monte Carlo method to simulate
the 2-D Ising model. Energy and Magnetization of the system were measured at different temperatures for two different
system sizes.

\section{Algorithm Description}
The algorithm is implemented as originally published~\cite{metropolis} and as described in the lecture notes~\cite{herrmann}.
Specific to the 2D Ising model, a new configuration is generated by randomly flipping the spin of a lattice element
and then calculating the system's post-change energy.
The dimensionless energy $E$ and magnetization $M$ are calculated as indicated in equations~\ref{eqn:1}~and\ref{eqn:2}, where
$S_i \in \{-1, +1\}$ stands for 'spin down' respectively 'spin up' at site $i$ and $N$~stands for the total number of sites in
the 2D lattice under consideration.
The implemented version of the algorithm allows for 100 system sweeps after each increasing temperature step,
after which every tenth value for $E$ and $M$ is measured during another 100 system sweeps and then averaged to obtain
the respective site values. By doing so, any correlation between measurements should be minimized.

\begin{equation}
	E = -J \cdot \sum_{<i, j>}S_i S_j
\label{eqn:1}
\end{equation}

\begin{equation}
	M = \frac{1}{N}\sum_{i=1}^{N}S_i
\label{eqn:2}
\end{equation}

\section{Results}

The program was implemented as described above and submitted with this report.
For all experiments, the coupling constant $J$ was fixed to the simplest ferromagnetic value of $J=1.0$. The temperature $T$
was varied in the range from $1$ to $5$ $J/k_B$. No cold starts were used.
The experiment was run twice for 2D Ising lattice side lengths $L \in \{100, 200\}$. The results for dimensionless energy $E$ and magnetization $M$
 for the two lattie sizes are shown in figure~\ref{fig:1}.
The site values between the two different system sizes agree while the larger system shows smoother behaviour as one would expect.
By implementing several of the suggested algorithm options, the run time of the simulation could be kept
in the order of magnitude of minutes.

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=1.4]{figure1.eps}
\end{center}
\caption{Value of magnetization $M$ and energy $E$ at different temperatures $T$ according to Metropolis MC with $J=1$ on lattices of side length $L$.}
\label{fig:1}
\end{figure}

\section{Discussion}
As expected, the magnetization $M$ drops off steeply at the critical temperature $T_c = \frac{2}{\log(1+\sqrt{2})} \quad [J/k_B]$.
Also in line width expectations, the energy increases with temperature with the steepest ascent also around $T_c$.

\pagebreak
\begin{thebibliography}{99}

\bibitem{metropolis}
Metropolis, N.,
Rosenbluth, A.W.,
Rosenbluth, M.N.,
Teller, A.H.,
Teller, E.\\
\emph{Equations of State Calculations by Fast Computing Machines},\\
Journal of Chemical Physics. 21 (6): 1087,\\
1953.

\bibitem{herrmann}
	Herrmann, H. J.,
	Singer, H. M.,
	Mueller L.,
	Buchmann, M.-A.,\\
	\emph{Introduction to Computational Physics - Lecture Notes},\\
	ETH Zurich,\\
	2017.

\end{thebibliography}

\end{document}


\chapter{Preliminaries and State of the Art}
\label{ch:preliminaries_and_sota}

\section{Preliminary notions}
\label{sec:preliminaries}

\begin{table}[!ht]
\centering
\begin{tabular}{c l} \hline
\textbf{Notation}&\textbf{Description} \\ \hline
$G$&Graph\\
$V$&set of nodes of $G$\\
$E$&set of edges of $G$\\
$W$&set of weights corresponding to each edge in $E$\\
$w_{u,v}$&weight of edge $(u,v)$\\
$n$&$|V|$, number of nodes\\
$m$&$|E|$, number of edges\\
\hline
\end{tabular}
\caption{Graph notation.}
\label{tab:notation}
\end{table}

``In this section, we introduce the preliminary notions at the base of our study. We start by briefly introducing the problem, and then we provide the necessary concepts and the notation used."

You may insert a subsection for each of the most relevant features of your problem. You can add some reference if needed, but just to explain the problem. The references with the solutions of the problem should be put in the next section.

You can keep a notation table for the notation used in this chapter as \autoref{tab:notation}. Everything inside the notation table must be written at least once inside this chapter. You can put an extended notation for the whole thesis in the appendix.

It is likely that you have to present definitions, theorems or propositions. We suggests to use the environments provided by the template. You can find the guide in the LaTeX suggestions chapter.

\section{State of the Art}
\label{sec:sota}

In this section, we survey the most relevant works related to the argument of your thesis. If you face a problem that has more than one macro-topic, you may choose to add a subsection for each of these topics (better no more than 2-3), like \emph{Related works on Topic 1}, etc.

List the works in chronological order and cite only the most important and pertinent ones, avoid 100 citations for a master thesis.


\documentclass[jou]{apa6}

\usepackage[american]{babel}

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

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

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

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

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

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

\title{Sample Quiz 8}
\author{Discrete Structures, Spring 2020}
\affiliation{RBS}

\leftheader{Discrete Sample Quiz 8}

\abstract{%
}

%\keywords{}

\setlength\parindent{0pt}

\begin{document}

%\thispagestyle{empty}

\twocolumn
\section{Quiz 8: Probabilities}

\vspace{10pt}
{\bf Question 1}
Two chess queens are placed on two different places in a $4 \times 4$ chess-board.
Assume that all the ${16 \choose 2}$ possibilities how they are placed have equal probabilities.
Find a probability that one queen {\em attacks} the other. (Two queens attack each other, if
they are located on the same horizontal, vertical or diagonal).

Write your answer as a rational fraction: {\tt P/Q}

\vspace{10pt}
{\bf Question 2} Assume that you are generating 10-bit sequences (a string of 0's and 1's).
All the $2^{10}$ sequences have equal probabilities.
Find the probability of an event that the 10-bit sequence does NOT contain two consecutive 0's anywhere.

Write your answer as a rational fraction: {\tt P/Q}

\vspace{10pt}
{\bf Question 3}
Two players have 4 cubic dices. Instead of the usual numbers, their faces have the following numbers on their faces:\\
{\bf Dice A:} 4, 4, 4, 4, 0, 0;\\
{\bf Dice B:} 3, 3, 3, 3, 3, 3;\\
{\bf Dice C:} 6, 6, 2, 2, 2, 2;\\
{\bf Dice D:} 5, 5, 5, 1, 1, 1.

In a single round Players Alice and Bob randomly select two of the dices (with equal probabilities they can
select any of the six pairs - $(A,B)$, or $(A,C)$, or $(A,D)$, or $(B,C)$, or $(B,D)$, or $(C,D)$).
There can be three outcomes:\\
{\bf Outcome 1:} They have selected "opposite dices" - pairs $(A,C)$ or $(B,D)$. In this case the payoff
is zero (nobody pays anything to the other).\\
{\bf Outcome 2:} The dices win in the "clockwise manner" ($A>B$ or $B>C$ or $C>D$ or $D>A$) - then Alice
wins 1 euro.\\
{\bf Outcome 3:} The dices win in the "counter-clockwise manner" ($B>A$ or $C>B$ or $D>C$ or $A>D$) - then Bob
wins 1 euro.\\
({\em Note.} The expression $A>B$ means that the number that rolled out on the the dice $A$ was larger than the number on dice $B$;
but $B>A$ denotes the opposite event.)

Find the expected value - how much money Alice is expected to win in a single round of such a game.

Write your answer as a rational fraction: {\tt P/Q}\\
For example, if the expected win for Alice is 0.10 EUR, then write {\tt 1/10}. If Alice is expected on average to lose 0.10 EUR
per one round of this game, then write {\tt -1/10}.

\vspace{10pt}
{\bf Question 4} For every year we count the number of Friday's that fall on the 13th date of some month
(such as Friday, March 13, 2020). Denote this count by $X$ \textendash{} it is your random variable.
Find the expected value and the variance of $X$. Round them to the nearest thousandth.

Write your answer as two comma-separated numbers: {\tt D.DDD,D.DDD}.

\vspace{10pt}
{\bf Question 5} What is the
probability that a randomly chosen positive integer between $1$ and $600$ is not divisible
by either $6$ or $10$?

Write your answer as a rational fraction: {\tt P/Q}

\vspace{10pt}
{\bf Question 6} A chip factory {\em Intel} adds one toy animal to every bag of chips.
There are three sorts of animals - Aligators, Bears or Cats (each one appears with probability $p=1/3$).
Find the expected number of the chip bags one needs to purchase to collect all three animals.

Write your answer as a rational fraction: {\tt P/Q}

\vspace{10pt}
{\bf Question 7} You create a random bit string of length five (all $32$ bit strings are equally probable). Consider
these events:\\
$E_1$: the bit string chosen begins with $1$;\\
$E_3$: the bit string chosen has exactly three $1$’s.\\
{\bf (A)} Find $p(E_1 \,\mid\, E_3)$.\\
{\bf (B)} Find $p(E_3 \,\mid\, E_1)$.

Write your answer as a comma-separated rational fractions {\tt P1/Q1,P2/Q2}

\newpage

\subsection{Answers}

\vspace{10pt}
{\bf Question 1.} Answer: {\tt 19/30}

To make the counting easier, let us assume that both queens are distinguishable
(we can have $Q_1$ and $Q_2$ as white and black queen).
Then the ways to place them are $16 \cdot 15$ \textendash{} which is twice the number
of combinations ${16 \choose 2} = \frac{15 \cdot 16}{1 \cdot 2}$.

\begin{verbatim}
Q x x x    x Q x x    x x x .
x x . .    x x x .    x Q x x
x . x .    . x . x    x x x .
x . . x    . x . .    . x . x
\end{verbatim}

Queen $Q_1$ can be placed in a corner of the $4 \times 4$ square ($4$ chances out of $16$),
on an edge ($8$ chances out of $16$) or near the center ($4$ chances out of $16$)
\textendash{} see the pictures above.
In the first two cases queen $Q_1$ attacks $9$ places (out of $15$). In the last case
it attacks $11$ places (out of $15$). The ultimate probability that $Q_1$ attacks $Q_2$
is
$$\frac{4}{16} \cdot \frac{9}{15} + \frac{8}{16} \cdot \frac{9}{15} + \frac{4}{16} \cdot \frac{11}{15} = \frac{152}{16 \cdot 15} =
\frac{19}{30}.$$

We could also count the mutual attack positions in the original problem (when $Q_1$ and $Q_2$
are indistinguishable). But we would get the same result, since attacking positions
are symmetric (if $Q_1$ attacks $Q_2$, then $Q_2$ attacks $Q_1$).

\vspace{10pt}
{\bf Question 2.} Answer: {\tt 9/64}\\
We can count all the sequences that do not contain two consecutive 0's: \url{https://bit.ly/2TPwMaj}.
If we denote by $f_0(n)$ the count of all $n$-bit sequences that do not contain two consecutive 0s,
we can prove that
$$f_0(0)=1,\;f_0(1)=2,\;f_0(2)=3,\;f_0(3)=5,\ldots$$
For arbitrary $n$ we get $f_0(n) = F_{n+2}$, where $F_{n}$ ($0,1,1,2,3,\ldots$) is the
Fibonacci sequence.

The $12$th member of the Fibonacci sequence is $F_{12}=144$.
Therefore the proportion of $10$-bit sequences equals $\frac{144}{1024}=\frac{9}{64}$.

\vspace{10pt}
{\bf Question 3.} Answer: {\tt 2/9}\\
There is $1/3$ probability to pick $(A,C)$ or $(B,D)$ (payoff is $0$ in this case).\\
Regarding the other four pairs, the probabilities are the following:
\begin{itemize}
\item $A$ wins $B$ with probability $2/3$.
\item $B$ wins $C$ with probability $2/3$.
\item $C$ wins $D$ with probability $\frac{1}{3} \cdot \frac{1}{1} + \frac{2}{3} \cdot \frac{1}{2}=\frac{2}{3}$:
{\bf Either} we have $C=6$ (probability $1/3$); and it wins with probability $1$. {\bf Or} we have $C=2$ (probability $2/3$)
and it wins with probability $1/2$ (whenever $D=1$).
\item $D$ wins $A$ with probability $\frac{1}{2} \cdot \frac{1}{1} + \frac{1}{2} \cdot \frac{1}{3}=\frac{2}{3}$:
{\bf Either} we have $D=5$ (probability $1/2$); and it wins with probability $1$. {\bf Or} we have $D=1$ (probability $1/2$)
and it wins with probability $1/3$ (whenever $A=0$).
\end{itemize}

For each pair $(A,B)$, $(B,C)$, $(C,D)$, or $(D,A)$ the expected payoff for Alice is
$$\frac{2}{3}(+1\,\text{EUR}) + \frac{1}{3}(-1\,\text{EUR}) = \frac{1}{3}\,\text{EUR}.$$

The expected probability for Alice to win in a single round can be obtained
as a sum, where we multiply probabilities for each pair
($(A,C)$, $(B,D)$, $(A,B)$, $(B,C)$, $(C,D)$, or $(D,A)$) with their respective payoffs (either 0 or 1/3 euro):
$$\frac{1}{6} \cdot 0 + \frac{1}{6} \cdot 0 + \frac{1}{6} \cdot \frac{1}{3}
+ \frac{1}{6} \cdot \frac{1}{3} + \frac{1}{6} \cdot \frac{1}{3} + \frac{1}{6} \cdot \frac{1}{3} = \frac{2}{9}.$$

\vspace{10pt}
{\bf Question 4.} Answer: {\tt 1.714,0.490}\\
Assume that each year (and therefore each month) can start on
each weekday with exactly the same probability (that is $1/7$).
Every month has date $13$, so there is a $1/7$ probability that
this date will happen on Friday.

{\bf (A)} If $X$ is the number of months having 13th date on Friday,
then $E(X)$ must be $12/7 = 1.714$. It is not because the
weekdays that start months in a single year are independent (which they are not!),
but rather because every $28$-year cycle
should have the same number of each type of month (January, February, etc.)
starting on each weekday: exactly $4$ Januaries starting on Monday,
exactly $4$ Januaries starting on Tuesday, etc. ($4+4+\ldots+4 = 28$).
Only those Januaries that start on Sundays will have $13$th date on Friday.

{\bf (B)} Let us create a table reflecting the number of Fridays on 13th
depending on whether the year is the leap year and the weekday it starts.

\begin{tabular}{|l|l|l|} \hline
Year & Non-leap & Leap \\ \hline
Sun & 2 (Jan,Oct) & 3 (Jan,Apr,Jul) \\ \hline
Mon & 2 (Apr,Jul) & 2 (Sep,Dec) \\ \hline
Tue & 2 (Sep,Dec) & 1 (Jun) \\ \hline
Wed & 1 (Jun) & 2 (Mar,Nov) \\ \hline
Thu & 3 (Feb,Mar,Nov) & 2 (Feb,Aug) \\ \hline
Fri & 1 (Aug) & 1 (May) \\ \hline
Sat & 1 (May) &  1 (Oct) \\ \hline
\end{tabular}

The probability to get non-leap year starting on a certain
weekday is $\frac{3}{4}\cdot\frac{1}{7} = \frac{3}{28}$.
The probability to get leap year starting on a certain weekday
is $\frac{1}{28}$.

We find the variance $V(X)$ using its definition:
$$V(X) = \sum (x_i - E(X))^2 p(x_i) = $$
$$ = \frac{3}{28}\left( \left( 3 - \frac{12}{7} \right)^2 +
3\left( 2 - \frac{12}{7} \right)^2 + 3\left( 1 - \frac{12}{7} \right)^2 \right) + $$
$$ + \frac{1}{28}\left( \left( 3 - \frac{12}{7} \right)^2 +
3\left( 2 - \frac{12}{7} \right)^2 + 3\left( 1 - \frac{12}{7} \right)^2 \right) = $$
$$ = \frac{1}{7}\cdot\frac{9^2}{7^2} + \frac{3}{7}\cdot\frac{2^2}{7^2} +
\frac{3}{7}\cdot\frac{(-5)^2}{7^2} = \frac{168}{7^3} = \frac{24}{49}
\approx 0.490.$$

{\em Note.} In fact, the assumption that a year starts with each
weekday with a probability $1/7$ is (slightly) false.
\url{https://bit.ly/2vo0YQt} explains that each $400$ year cycle in Gregorian calendar
repeats the same weekdays. Namely, the calendar for year $1600$ is identical to
the calendar of year $2000$; year $1620$ starts on the same weekday as
year $2020$, and so on.

To verify this, notice that exactly $97$ of all $400$ years are leap years:
({\bf either} years divisible by $4$, but not with $100$, {\bf or} years divisible
by $400$. During this $400$ year cycle the number of days:
$$303 \cdot 365 + 97 \cdot 366 \equiv 303 \cdot 1 + 97 \cdot 2 \equiv 497 \equiv 0\;(\text{mod}\,7).$$
Since this number is divisible by $7$, we should count the proportions
of the weekdays within one $400$ year cycle.

\vspace{10pt}
{\bf Question 5.} Answer: {\tt 23/30}\\
Let $U$ be the (universe) set of all numbers between $1$ and $600$.
Define three more sets:
\begin{align}
A & = \{ i \in U \,\mid\, i\;\text{is divisible by}\;2 \}, \nonumber \\
B & = \{ i \in U \,\mid\, i\;\text{is divisible by}\;3 \}, \nonumber \\
C & = \{ i \in U \,\mid\, i\;\text{is divisible by}\;5 \}, \nonumber
\end{align}

Numbers $x \in \overline{A \cap B} = \overline{A} \cup \overline{B}$
are not divisible by $6$. Numbers
$x \in \overline{A \cap C} = \overline{A} \cup \overline{C}$
are not divisible by $10$. Numbers in the intersection are not divisible
either by $6$ or by $10$:
$$(\overline{A} \cup \overline{B}) \cap (\overline{A} \cup \overline{C}) =
\overline{A} \cup (\overline{B} \cap \overline{C}) =
\overline{A} \cup \overline{B \cup C}.$$

Inclusion-exclusion principle tells that the number of elements in this union:
$$|\overline{A} \cup \overline{B \cup C}| = |\overline{A}| +
|\overline{B \cup C}| - |\overline{A} \cap \overline{B \cup C}|.$$
We can compute:
\begin{itemize}
\item $|\overline{A}| = (1/2) \cdot 600 = 300$ ... not divisible by $2$.
\item $|\overline{B \cup C}| = 600 - |B \cup C| = 600 - (|B| + |C| - |B \cap C|) = $
$= 600 - (200 + 120 - 40) = 320$ ... not divisible either by $3$ or $5$.
\item $|\overline{A} \cap \overline{B \cup C}| = 160$.
\end{itemize}

Let us return to the original question: counting the elements
in $\overline{A} \cup \overline{B \cup C}$:
$$|\overline{A} \cup \overline{B \cup C}| = 300 + 320 - 160 = 460.$$
All these numbers are not divisible either by $10$ or by $6$, so
their proportion is $\frac{460}{600} = \frac{23}{30}$.

\vspace{10pt}
{\bf Question 6.} Answer: {\tt 11/2}\\

When you buy the first bag of chips, you necessarily get
a toy animal you did not have before. Waiting time for this is always $1$.

Assume that you already have one toy; then it might take $x_1 = 1$,
$x_2 = 2$, $x_3 = 3$, etc. more bags to find a different toy animal.
The respective probabilities of these events are $p_1 = \frac{2}{3}$,
$p_2 = \frac{1}{3}\cdot\frac{2}{3}$, $p_3 = \frac{1}{3}\cdot\frac{1}{3}\cdot\frac{2}{3}$, etc.
These probabilities make a {\em geometric distribution}.
The expected waiting time is the sum $x_1p_1 + x_2p_2 + x_3p_3 + \ldots$:
$$S = 1 \cdot \frac{2}{3} + 2 \cdot \frac{1}{3} \cdot \frac{2}{3} + 3 \cdot \frac{1^2}{3^2} \cdot \frac{2}{3} + \ldots$$
To find the value of $S$, we multiply it by $3$:
$$3S = 3 \cdot \frac{2}{3} + 2 \cdot \frac{2}{3} + 3 \cdot \frac{1}{3} \cdot \frac{2}{3} + 4 \cdot \frac{1^2}{3^2} \cdot \frac{2}{3}\ldots$$
If we subtract 2nd from the 1st and bring the factor $2/3$ to the front:
$$3S - S = \frac{2}{3} \left( 4  + \left(3 \cdot \frac{1}{3} - 2 \cdot \frac{1}{3} \right)
+  \left(4 \cdot \frac{1^2}{3^2} - 3 \cdot \frac{1}{3^2} \right) + \ldots \right).$$
$$2S = \frac{2}{3} \left( 4 + \frac{1}{3} + \frac{1}{3^2} + \ldots \right)$$
We get that $3S = 4\frac{1}{2}$ and $S = \frac{3}{2}$. I.e. we expect to get the
second toy after $1.5$ bags of chips.

Once we have 2 toys, the final toy can also be found by summation and infinite
geometrical progression. The expected waiting time is $3$ (bags of chips).

The total waiting time for all $3$ toys is $\frac{3}{3} + \frac{3}{2} + \frac{3}{1} = \frac{11}{2}$.

\vspace{10pt}
{\bf Question 7.} Answer: {\tt 3/5,3/8}\\
The conditional probability of $E_1$ given $E_3$ is defined like this:
$$p(E_1 \,\mid\, E_3) = \frac{p(E_1 \cap E_3)}{p(E_3)}.$$
The conditional probability of $E_3$ given $E_1$ is defined similarly:
$$p(E_3 \,\mid\, E_1) = \frac{p(E_1 \cap E_3)}{p(E_1)}.$$

\begin{itemize}
\item Out of $32$ bit sequences there are $6$ sequences that
are in $E_1 \cap E_3$: They start with $1$ and also contain exactly three $1$'s.
Indeed, the first bit is $1$, and there are ${4 \choose 2}$
ways to select the remaining two bits that equal $1$.
\item Out of $32$ bit sequences there are $16$ sequences that
are in $E_1$. They start with $1$.
\item Out of $32$ bit sequences there are $10$ sequences that
are in $E_3$. There are ${5 \choose 3}$ ways to select
three bits that equal $1$.
\end{itemize}

Compute both conditional probabilities:
$$p(E_1 \,\mid\, E_3) = \frac{p(E_1 \cap E_3)}{p(E_3)} = \frac{6/32}{10/32} = \frac{3}{5}.$$
$$p(E_3 \,\mid\, E_1) = \frac{p(E_1 \cap E_3)}{p(E_1)} = \frac{6/32}{16/32} = \frac{3}{8}.$$

\end{document}


%!TEX root = ceres.tex
\chapter{Further Reading}
\label{chapter:further}
 For a short but informative introduction to the subject we recommend the booklet by Madsel et al.~\cite{madsen2004methods}. For a general introduction to non-linear optimization we recommend the text by Nocedal \& Wright~\cite{nocedal2000numerical}. Bj{\"o}rck's book remains the seminal reference on least squares problems~\cite{bjorck1996numerical}. Trefethen \& Bau's book is our favourite text on introductory numerical linear algebra~\cite{trefethen1997numerical}. Triggs et al., provide a thorough coverage of the bundle adjustment problem~\cite{triggs-etal-1999}.


\documentclass[a4paper,11pt]{book}
\usepackage{import}
\usepackage{preamb}

\makeindex

\begin{document}

\input{head}
\newpage
\input{Title}
% \section{Blocks and Community structure}

\begin{subbox}{subbox}{}
\centering
\Large{\textbf{Scale-Free Networks}}
\end{subbox}

\begin{textbox}{Scale-Free: Definition}

A network is said to be \textbf{Scale-Free} when its degree distribution follows a Power-Law distribution, or can be approximated by a Power-Law distribution.

\end{textbox}

\begin{textbox}{Power-Law (PL)- Approximate Distribution}

A \textbf{Power-Law distribution} is defined as follows:
\[
P(k) \sim k^{-\alpha} = \frac{1}{k^\alpha}
\]

$\alpha$ is called the \textit{exponent} of the distribution.

Intuitively, the more $\alpha$ is large, the more large values are rare. For instance, with $\alpha=0$, it corresponds to a uniform distribution (any degree is equivalently probable). With $\alpha=1$, the probability of a node taken at random to have degree $k$ is $\sim \frac{1}{k}$. Usually, a distribution is considered scale free when $2\leq \alpha\leq 3$, as we will see.

\end{textbox}

\begin{textbox}{PL - Boundaries}

In most settings, the Power-Law degree distribution exists only for a certain range of degrees.

This makes sense in real networks: few people have 0 or 1 social contacts, for instance, few websites have no incoming nor outgoing hypertext links, or we wouldn't even be aware of them. Thus there is a \textbf{lower bound} $k_{min}$ from which the distribution exists.

Similarly, real networks represent entities of the real world, which are in finite numbers, therefore the number of elements itself is a limit. But in many situations, even lower thresholds exist: Social networks often impose a limit to the number of connections to avoid spammers, time and space also typically impose limits to what is possible or not in a network. An \textbf{upper bound} $k_{max}$ can be used to limit the distribution.

\end{textbox}

\begin{textbox}{Power-Law - Exact Distribution}

For a distribution to be properly defined, the sum of all probability must be equal to one, we therefore add a normalization constant $C$ to ensure this property.
\[
\int P(k)=1= \int Ck^{-\alpha}=C\int k^{-\alpha}
\]
From which we can define $C$:
\[
C = \frac{1}{\int_{k_{\min}}^{\infty}k^{-\alpha} dk}=(\alpha-1)k_{\min}^{\alpha-1}
\]
And finally, the exact definition of the Power-Law degree distribution with lower bound:
\[
P(k) = (\alpha-1)k_{\min}^{\alpha-1}k^{-\alpha} \\
P(k) = \frac{\alpha-1}{k_{\min}}\left(\frac{k}{k_{\min}}\right)^{-\alpha}
\]

\end{textbox}

\begin{textbox}{Power-Law - Plotting}

A famous property of the Power-Law distribution is that it looks like a line when plotted in a log-log plot, i.e., a plot in which the x-axis (degrees) and the y-axis (frequency of degrees) are represented using a logarithmic scale.
\begin{center}
Power-Law distributions in linear scale for degrees [1-10] \\
(100 000 samples)

\includegraphics[width=0.5\textwidth]{pics/SF1.png}

Power-Law distributions in linear scale for degrees [1-100000] \\
(100 000 samples). The distribution is so heterogeneous that is is not readable.

\includegraphics[width=0.5\textwidth]{pics/SF2.png}

Power-Law distributions in log-log scale for degrees [1-100000] \\
(100 000 samples).

\includegraphics[width=0.5\textwidth]{pics/SF3.png}
\end{center}

\end{textbox}

\begin{textbox}{Power-Law - Long tail}

Compared with other well-known distributions such as Poisson or exponential distribution, a key difference is what is called the \textbf{long-tail} property: very large values are rare, but possible. We can observe this long tail by comparing with other distributions on log-log plots.

\begin{center}
Comparing power-law with Poisson distributions
\includegraphics[width=0.8\textwidth]{pics/SF-poisson.png}

Comparing power-law with Exponential distributions

\includegraphics[width=0.8\textwidth]{pics/SF-exponential.png}

\end{center}

\end{textbox}

\begin{textbox}{SF networks - universality}

Scale-Free networks are widely studied because they are considered to be very frequent in the real world. Some important papers discovered the existence of Power-Law degree distribution in a variety of large real networks, notably:
\begin{enumerate}
    \item The World Wide Web (webpages) (\cite{barabasi1999emergence})
    \item The internet (physical network) (\cite{faloutsos1999power})
    \item Airline connections (\cite{guimera2004modeling})
    \item Scientific collaborations (\cite{newman2001structure})
    \item Romantic interactions (\cite{liljeros2001web})
\end{enumerate}

It must be noted, however, that many real world networks \textbf{are not scale-free}. A typical counter-example is a road-network, in which nodes correspond to intersection and edges to roads: for practical reasons, intersections with large degrees do not make sense.

\end{textbox}

\begin{textbox}{Why is it called \textit{scale free}}

Because they have no (typical) scale!

It is defined in opposition to Poisson and other Bell-Shaped distributions, which are \textbf{centered around their average value}. Let's take a typical example: The height of humans follow a Bell-shaped distribution: the average height is 1.65m, and most humans are quite close to this value, there is a \textbf{typical scale} of human height. On the contrary, human wealth distribution follows approximately a power-law \footnote{ \url{https://en.wikipedia.org/wiki/Distribution_of_wealth}}: a few humans are extremely wealthy (Billions of \$), while more than half the world population posses less than 10 000\$. As a consequence, the average human wealth (70 000\$) is not at all representative of human wealth.

\end{textbox}

\begin{textbox}{Central moments}
The first two central moments of a distribution are the mean $\langle k^1 \rangle $ and the variance $\langle k^2 \rangle $. They are defined as
\[
\langle k^m \rangle = \int_{k_{\min}}^{\infty}k^mp(k)dk=(\alpha-1)k^{\alpha-1}_{\min}\int_{k_{\min}}^{\infty}k^{-\alpha+m}dk
\]
From this, we can conclude that \textbf{central moments are defined only if $\alpha>m+1$}, otherwise they diverge towards infinity, they are not properly defined.

Thus:
\begin{enumerate}
    \item $\langle k^2 \rangle = \frac{\alpha-1}{\alpha-2}k_{\min}$, \textbf{if and only if $\alpha\geq 2$}
    \item $\langle k^3 \rangle = \frac{\alpha-1}{\alpha-3}k^2_{\min}$, \textbf{if and only if $\alpha\geq 3$}
\end{enumerate}

\end{textbox}

\begin{textbox}{Divergence in practice}
In practice, one can always compute the mean and variance of a provided, observed degree distribution. So what does it mean that they \textit{diverge}?

The problem arises when we are not certain to observe the whole network. Usually, a large sample of a population has the same mean and variation than the whole population, and the largest the sample, the more precise the value.

But in a power law, moments are \textbf{dominated} by the largest values in the long tail: some rare values are \textit{so large} that they shift the moments. So the more data we observe, the higher the moments.
\end{textbox}

\begin{textbox}{Divergence: consequences}
The consequence of diverging moments is that \textbf{if the distribution follows a power law}, then if the exponent is below 2, you should now rely on the mean degree or the variance. If the exponent is between 2 and 3, you can (relatively) rely on the mean, but not on the variance. Be careful though, even if $\alpha>2$, the mean converges slowly, i.e., you need a very large sample for your mean to be close to the real value.
\end{textbox}

\begin{textbox}{Fitting power laws}
When confronted with a power law degree distribution, we might want to \textbf{fit} the distribution, i.e., to \textbf{find the exponent} of the distribution. A naive and simple way to do it is to plot the distribution on a log-log plot and to find the slope of the line, either graphically or through least-square regression on the log-transformed values of degrees and frequencies.

This however suffers from a strong bias: values in the tail are based on a few samples, and introduce noise.

The most appropriate method is to use Maximum Likelihood Estimation (MLE\footnote{https://towardsdatascience.com/a-gentle-introduction-to-maximum-likelihood-estimation-9fbff27ea12f}), taking into account min and max-boundaries, as described in \footcite{goldstein2004problems}
\end{textbox}

\begin{textbox}{Exponent limits}
In real networks, we consider that we should have $\alpha\geq2$, because a lower exponent would mean that the distribution is so skewed that we expect to find nodes with a degree larger than the size of the network.

Furthermore, if the exponent is too large, large degree nodes becomes so rare, that the network would need to be enormous to observe such a node. For instance, with $\alpha=5$, we need to observe $N=10^{12}$ nodes to expect to observe a single node of size 1000.
\end{textbox}

\begin{textbox}{Exponent and shortest-paths}
Random networks with Poisson degree distribution already have a \textit{short average distance}. However, it is possible to define classes of networks with even smaller average distance based on the exponent $\alpha$:
\begin{itemize}
    \item $\alpha=2$: The biggest hub degree is of order $\mathcal{O}(N)$, thus most nodes are at distance 2. The average path length can be considered a small constant, independent of $N$
    \item $2<\alpha<3$: \textbf{Ultra Small World}: $\langle \ell \rangle = \frac{\log \log N}{\log(\alpha-1)}$
    \item $\alpha=3$: $\langle \ell \rangle = \frac{ \log N}{\log \log N}$
    \item $\alpha>3$: $\langle \ell \rangle = \log N$, the network behaves approximately like an ER network.
\end{itemize}
\end{textbox}

\begin{textbox}{Scale-free network controversy}
There is an on-going debate in the network science community over the prevalence of scale-free networks. For some authors \footcite{barabasi2003scale}, most real networks follow to some extent a power-law degree distribution, while, for some others, scale-free networks are rare \footcite{broido2019scale}. The controversy has been studied\footcite{jacomy2020epistemic} and can be interpreted as differences between scientific approaches: one popular among (some) physicists (\textit{scale-freeness is the sign of a universal law}) and another one common among statisticians \textit{(scale-freeness is an empirical characterization)}.
\end{textbox}

\begin{textbox}{SF networks: what to do}
Is your network Scale-Free? The first question you might ask yourself is: \textit{why do you need to know?}.
\begin{itemize}
    \item If the goal is to characterize a network, then plotting the degree distribution might be more useful than fitting a power-law exponent to it
 \item If the goal is to show that the distribution is broad, significantly different from a bell-shaped distribution, then plotting the distribution might be enough
 \item If the goal is to show that the distribution is \textit{approximately} a power-law, for instance because an algorithm complexity or a proof can be made for such cases, then fitting a line on a log-log plot and talking about \textit{power-law-ish} might be enough
 \item If on the contrary it is scientifically important to argue that the network is, without doubts, a scale-free networks, then you need to be fully aware of the controversy and to position your work relatively to it.
\end{itemize}
\end{textbox}

\input{tail}

\end{document}


\documentclass[11pt,a4wide]{article}
\usepackage{verbatim}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{a4wide}
\usepackage{color}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[dvips]{epsfig}
\usepackage[T1]{fontenc}
\usepackage{cite} % [2,3,4] --> [2--4]
\usepackage{shadow}
\usepackage{hyperref}

\setcounter{tocdepth}{2}

\lstset{language=c++}
\lstset{alsolanguage=[90]Fortran}
\lstset{basicstyle=\small}
\lstset{backgroundcolor=\color{white}}
\lstset{frame=single}
\lstset{stringstyle=\ttfamily}
\lstset{keywordstyle=\color{red}\bfseries}
\lstset{commentstyle=\itshape\color{blue}}
\lstset{showspaces=false}
\lstset{showstringspaces=false}
\lstset{showtabs=false}
\lstset{breaklines}
\begin{document}
\section*{Introduction to numerical projects}

Here follows a brief recipe and recommendation on how to write a report for each
project.
\begin{itemize}
\item Give a short description of the nature of the problem and the eventual
numerical methods you have used.
\item Describe the algorithm you have used and/or developed. Here you may find it convenient
to use pseudocoding. In many cases you can describe the algorithm
in the program itself.

\item Include the source code of your program. Comment your program properly.
\item If possible, try to find analytic solutions, or known limits
in order to test your program when developing the code.
\item Include your results either in figure form or in a table. Remember to
       label your results. All tables and figures should have relevant captions
       and labels on the axes.
\item Try to evaluate the reliabilty and numerical stability/precision
of your results. If possible, include a qualitative and/or quantitative
discussion of the numerical stability, eventual loss of precision etc.

\item Try to give an interpretation of you results in your answers to
the problems.
\item Critique: if possible include your comments and reflections about the
exercise, whether you felt you learnt something, ideas for improvements and
other thoughts you've made when solving the exercise.
We wish to keep this course at the interactive level and your comments can help
us improve it.
\item Try to establish a practice where you log your work at the
computerlab. You may find such a logbook very handy at later stages
in your work, especially when you don't properly remember
what a previous test version
of your program did. Here you could also record
the time spent on solving the exercise, various algorithms you may have tested
or other topics which you feel worthy of mentioning.
\end{itemize}

\section*{Format for electronic delivery of report and programs}
%
This project counts 1/3 of the final mark. The feedback will, in addition to our comments contain a final score, where 100 is the maximum. Project 4 counts also 1/3 of the final mark. The final exam stands for the remaining 1/3 of the final mark.
We have set up devilry so that you can use your candidate number only. Please mark your project with the candidate number. If you prefer to not use the candidate number, we will need it anyway in order to correlate with the final written exam.

The preferred format for the report is a PDF file. You can also
use DOC or postscript formats or as an ipython notebook file.
As programming language we prefer that you choose between C/C++, Fortran2008 or Python.
The following prescription should be followed when preparing the report:
\begin{itemize}
\item Use Devilry to hand in your projects, log in  at
\url{ http://devilry.ifi.uio.no} with your normal UiO username and password
and choose either 'fys3150' or 'fys4150'. This time however you should use your candidate number only. You are free to use your name as well, but we need the candidate number for the final mark.
\item You can upload the report file as well as your program files to devilry!  For the source code file(s) you have developed you can also place these at  a github domain, or eventually upload all program files to devilry.
The report file should include all of your discussions and a list of the codes you have developed.
\item You should include tests and/or unit tests of your code(s).
\item Comments  from us on your projects, with final score
will be available under
your Devilry domain and are only visible to you and the teachers of the course.

\end{itemize}

Finally, even though this project counts 1/3 of the final mark,
we encourage you to work two and two together. Optimal working groups consist of
2-3 students. You can then hand in a common report.

\section*{Project 5, Astronomy project, $N$-body simulation of an open galactic cluster, deadline  Friday December 11}

The goal in this project is to develop a code that can perform simulations of an
open cluster using Newtonian gravity. First, however we will compare
the stability of two different methods. This is because when we are
looking at a system with a large number of particles, we are more
interested in the statistical properties of the system than in the
individual motion of each of the particles. This means that the
stability of the solution method is more important than its short term
accuracy.  This project is inspired by an article by Joyce {\em et al.}, see Ref.~\cite{joyce2010} below.
When  solving this project, we recommend downloading this article. It is a good companion to understand the
physics discussed here.

In the first part of this project we will explore the stability of two
well-tested numerical methods for solving differential equations. The algorithms to test and implement are the fourth-order Runge-Kutta method and the Velocity-Verlet method.

\begin{enumerate}
\item[a)] Implement the Newtonian two-body (you can choose masses and dimensionalities as
  you wish) problem in three dimensions using the fourth order
  Runge-Kutta method and the Velocity-Verlet method discussed in
  the lecture notes.

 Compare the
stability of the two different methods. How do they work for large
time steps? How do they work for very long times? Compare also the
time used to advance one timestep for the two different
methods. Comment your results. Which algorithm would you use for
simulating systems that require long  times?

\end{enumerate}

We will now try to build a simple model of an open
cluster, see Ref.~\cite{openclusterref}. An
open cluster is a group of up to a few thousand gravitationally bound
stars created from the collapse of a molecular cloud. This collapse
leads to a flurry of star formation. Open clusters are usually found
in the arms of spiral galaxies, or in irregular galaxies. Since stars
in an open cluster have roughly the same age, and are made from the
same material, they are interesting in the study of stellar evolution,
since many of the variable parameters we have when comparing two stars
are kept constant.

Once open clusters are formed they gradually dissipate as members get
ejected from the cluster due to random collisions, this means that
open clusters generally last only a few hundred million years. In
figure \ref{HR}, we see the Hertzsprung-Russell diagrams for two open
clusters.

\begin{figure}[!h]
\centering
\includegraphics[width=10cm]{FigAstro/open_cluster_hr_diagram_ages.png}
\caption{Hertzsprung-Russell diagrams for two open clusters, M67 and
  NGC 188. We see that most of the stars are on the main sequence. In
  the older cluster, NGC 188, we see that the heaviest stars are just
  now leaving the main sequence, while the younger cluster, M67, is
  following closely after.}
\label{HR}
\end{figure}

We will look at a simple model for how an open cluster is made from
the gravitational collapse and interaction among a large number of
stars. We want to study this collapse, and the statistical properties
of the collapsed system.

One particle in our model represents one or a few stars, and we will
work first with a few hundred particles. We will simulate what is called a
``cold collapse'', this means that we start the particles with little
or no initial velocity.
\begin{enumerate}
\item[b)] Extend your code to an arbitrary number of particles, $N$,
  starting with a uniform (random) distribution within a sphere of a
  given radius $R_0$. Start the particles at rest, with masses
  randomly distributed by a Gaussian distribution around ten solar
  masses with a standard deviation of one solar mass. Use solar masses
  and light years as units of mass and length and make your equations dimensionless.
The function {\em GaussPDF} included with this project can be used to generate
random numbers which follow a Gaussian (or normal) distribution.  The function for calculating these random numbers can be found at the webpage of the course together with the project files.

How large time steps are required given $R_0 = 20 ly$
(light years), and a $N = 100$? Do we have any units of time that fit
this timescale?
In the limit where $N \rightarrow \infty$, keeping $\rho_0$ constant,
we get a continuous fluid. In this case the system collapses into a
singularity at a finite time $\tau_{crunch} = \sqrt{\frac{3\pi}{32G\rho_0}}$.
(For the especially interested (Not required!): Can
you derive this result? Hint: recall the Friedman equations \cite{friedmaneqs}).

Why do we not observe this singularity in our model? Use
$\tau_{crunch}$ as the unit of time, and find $G$ in these units ($G$
will become a function of the number of particles $N$, and the average
mass of the particles, $\mu$).

You should run these calculations with both the fourth-order Runge-Kutta algorithm
and the Velocity-Verlet method. Which method would you prefer? Give a critical discussion.

For the remaining exercises, you should use only one of the above methods.

\item[c)] Run the system for a few $\tau_{crunch}$. Save the positions
  of the particles at different times to file. Does the system
  reach an equilibrium? How long time does this take ?

\item[d)] Make a function that calculates the kinetic and potential
  energy of the system. Is the energy conserved? Some of the
  particles are ejected from the system, how can we identify these
  particles from the energies we have calculated? How much of the
  energy of the system is taken away by particle ejection? How does
  this change with different values of $N$? Are there still particles
  being ejected after the system reaches equilibrium?

\item[e)] We will now introduce a smoothing function to take care of
  the numerical instability that arises when two of the particles come
  very close. There are a lot of ways of inserting such a smoothing, but
  we will just look at a very simple one. We will modify the Newtonian
  force law to make it finite at short ranges
\[ F_{mod} = -\frac{GM_1M_2}{r^2 + \epsilon^2}.\]

The parameter $\epsilon$ is a ``small'' real constant. What should the value of this parameter be? Try out different values (which one gives you the best energy conservation?). Can we justify this correction to the pure Newtonian force by noting that our particles do not represent actual point particles
but rather mass distributions of some finite extent? Does the addition
of this correction change any of the results from part e) ?

\item[f)] Now we will look at the particles that are bound (not
  ejected). What is the distribution of potential and kinetic energy?

The virial theorem says that for a bound gravitational system in
equilibrium we have
\[
2\langle K\rangle = -\langle V \rangle,
\]
where $\langle K\rangle$
is the average (over time) kinetic energy of the system and $ \langle V
\rangle$ is the average potential energy.

By the ergodic hypothesis we can take an ensamble average (average over a large system) instead of the time average.

Are your results consistent with the virial theorem?

\item[h)]  Try to plot the radial density of the particles (the particle density as a function of radius) in the
  equilibrium state. How would you extract such an information from your calculations? (Hint: make a histogram for the radial
particle density) What is the average distance? What is the
  standard deviation? Plot the radial distribution of particles.

Run the code for different number of initial particles, keeping the
total mass constant. \footnote{The interpretation of one particle as one or a few stars will not be useful any more when you increase the number of particles beyond a few thousand, however the analysis of gravitationally bound systems of many particles have much broader applications than cold collapse of open clusters, so the results are still highly relevant.} What is the average distance from the centre of the cluser as a function of $N$?

The radial distribution of particles in this kind of cold collapse can often be fit very well with the simple expression
\[
n(r) = \frac{n_0}{\left(1 +\left(\frac{r}{r_0}\right)^4\right)}.
\]
Try to fit your data to this curve, what is the value $n_0$ and $r_0$? Can you find how these values depend on N?

How many particles can you simulate?

Compare your results with those found in Ref.~\cite{joyce2010}.

If you want, you can also compare your results to the well-known Navarro-Frenk-White profile
\[
\rho(r) = \frac{\rho_0}{\frac{r}{r_0}\left(1 +\frac{r}{r_0}\right)^2}.
\]
Does this fit better?

\end{enumerate}
\begin{thebibliography}{100}
\bibitem{joyce2010} M.~Joyce, B.~Marcos, and F.~Sylos Labini, Cold uniform spherical collapse revisited, AIP Conf. Proc. 1241, 955 (2010); \url{http://dx.doi.org/10.1063/1.3462740} and arXiv1011.0614 (2011),
\url{http://arxiv.org/abs/1011.0614}.
\bibitem{openclusterref} P.~J.~E. Peebles, \emph{{The Large-Scale Structure of the Universe}}, Princeton
  University Press, 1980. See also C.~Payne-Gaposchkin,  {\em Stars and clusters}, (Cambridge, Harvard University Press, 1979), or just \url{https://en.wikipedia.org/wiki/Open_cluster}.
\bibitem{friedmaneqs} A.~Friedman,  {\em On the Curvature of Space},  General Relativity and Gravitation {\bf 31},  1991 (1999), or just \url{https://en.wikipedia.org/wiki/Friedmann_equations}.
\end{thebibliography}
\end{document}