\section{Introduction}
\label{sec:introduction}

\begin{figure}[t]
    %\vspace*{-2px}

    \hspace*{-0.25cm}
    \begin{minipage}[t]{0.5\textwidth}
        \vspace*{-10px}
        \centering
        \begin{minipage}{0.495\textwidth}
        	\begin{tcolorbox}[
        		left=0pt,
        		right=0pt,
        		top=0pt,
        		bottom=0pt,
        		colback=white,
        		colframe=gray!10!white,
        		width=1\textwidth,
        		enlarge left by=0mm,
        		boxsep=5pt,
        		arc=0pt,outer arc=0pt,
        		boxrule=1pt,
        		]

        		\centering
        		\small\textbf{Adversarial Training (\AdvTrain):}
                \vspace*{-1px}
        	\end{tcolorbox}
            \begin{tcolorbox}[
                left=0pt,
                right=0pt,
                top=0pt,
                bottom=0pt,
                colback=white,
                colframe=gray!10!white,
                width=1\textwidth,
                enlarge left by=0mm,
                boxsep=5pt,
                arc=0pt,outer arc=0pt,
                boxrule=1pt,
                ]

                \vspace*{-4px}
                \includegraphics[width=3.8cm]{fig_intro_advtrain_1_adversarial}
                \vskip -2px

                \includegraphics[width=3.5cm]{fig_intro_advtrain_3_l2_adversarial}
                \vspace*{-6px}
            \end{tcolorbox}
        \end{minipage}
        \hfill
        \begin{minipage}{0.495\textwidth}
        	\begin{tcolorbox}[
        		left=0pt,
        		right=0pt,
        		top=0pt,
        		bottom=0pt,
        		colback=gray!12!white,
        		colframe=gray!12!white,
        		width=1\textwidth,
        		enlarge left by=0mm,
        		boxsep=5pt,
        		arc=0pt,outer arc=0pt,
        		boxrule=1pt,
        		]

        		\centering
        		\small\textbf{Ours (CCAT):}
                \vspace*{-1px}
        	\end{tcolorbox}
            \begin{tcolorbox}[
                left=0pt,
                right=0pt,
                top=0pt,
                bottom=0pt,
                colback=gray!12!white,
                colframe=gray!12!white,
                width=1\textwidth,
                enlarge left by=0mm,
                boxsep=5pt,
                arc=0pt,outer arc=0pt,
                boxrule=1pt,
                ]

                \vspace*{-4px}
                \includegraphics[width=3.8cm]{fig_intro_ours10_1_adversarial}
                \vskip -2px

                \includegraphics[width=3.5cm]{fig_intro_ours10_3_l2_adversarial}
                \vspace*{-6px}
            \end{tcolorbox}
        \end{minipage}

        \begin{tcolorbox}[
            left=0pt,
            right=0pt,
            top=0pt,
            bottom=0pt,
            colback=white,
            colframe=gray!10!white,
            width=1\textwidth,
            enlarge left by=0mm,
            boxsep=5pt,
            arc=0pt,outer arc=0pt,
            boxrule=1pt,
            ]
            \centering
            \scriptsize

            \textcolor{colorbrewer2}{{\rule[2pt]{10pt}{1pt} (Correct) Predicted Class}}\xspace\xspace\xspace \textcolor{colorbrewer8}{{\rule[2pt]{10pt}{1pt} Adversarial Class}}\xspace\xspace\xspace \textcolor{colorbrewer9}{{\rule[2pt]{10pt}{1pt} Other Classes}}
        \end{tcolorbox}
    \end{minipage}
    \vskip -2px

    \caption{
    \textbf{Adversarial Training (\AdvTrain) versus our \ConfTrain.} We plot the confidence in the
    direction of an adversarial example. AT enforces high
    confidence predictions for the correct class on the $L_\infty$-ball of radius $\epsilon$ (\emph{``seen''}
    attack during training, top left). As AT
    enforces no particular bias beyond the $\epsilon$-ball, adversarial examples can be found right beyond this ball. In contrast \ConfTrain enforces a decaying confidence in the correct class up to uniform confidence within the $\epsilon$-ball (top right). Thus, \ConfTrain biases the model to extrapolate uniform confidence beyond
    the $\epsilon$-ball. This behavior also extends to \emph{``unseen''} attacks during training, \eg, $L_2$ attacks (bottom), such that adversarial examples can be
    rejected via confidence-thresholding.
    }
    \label{fig:introduction}
    \vspace*{-2px}
\end{figure}

Deep networks were shown to be susceptible to adversarial examples \cite{SzegedyICLR2014}: adversarially perturbed examples that cause mis-classification while being nearly ``imperceptible'', \ie, close to the original example. Here, ``closeness'' is commonly enforced by constraining the $L_p$ norm of the perturbation, referred to as threat model. Since then, numerous defenses against adversarial examples have been proposed. However, many were unable to keep up with more advanced attacks \cite{AthalyeICML2018b,AthalyeARXIV2018b}. Moreover, most defenses are tailored to only one specific threat model.

Adversarial training \citep{GoodfellowICLR2015,MadryICLR2018}, \ie, training on adversarial examples, can be regarded as state-of-the-art. However, following \figref{fig:introduction}, adversarial training is known to ``overfit'' to the threat model \emph{``seen''} during training, \eg, $L_\infty$ adversarial examples.
Thus, robustness does not extrapolate to larger $L_\infty$ perturbations, \cf \figref{fig:introduction} (top left), or generalize to \emph{``unseen''} attacks, \cf \figref{fig:introduction} (bottom left), \eg, other $L_p$ threat models \citep{SharmaICLRWORK2018,TramerNIPS2019,LiARXIV2019,KangARXIV2019,MainiICML2020}.
We hypothesize this to be a result of enforcing high-confidence predictions on adversarial examples. However, high-confidence predictions are difficult to extrapolate beyond the adversarial examples seen during training. Moreover, it is not meaningful to extrapolate high-confidence predictions to arbitrary regions.
Finally, adversarial training often hurts accuracy, resulting in a robustness-accuracy trade-off \citep{TsiprasICLR2019,StutzCVPR2019,RaghunathanARXIV2019,ZhangICML2019}.

\textbf{Contributions:}
%
We propose \textbf{confidence-calibrated adversarial training (\ConfTrain)} which trains the network to predict a convex combination of uniform and (correct) one-hot distribution on adversarial examples that becomes more uniform as the distance to the attacked example increases. This is illustrated in \figref{fig:introduction}. Thus, \ConfTrain implicitly biases the network to predict a uniform distribution beyond the threat model \emph{seen} during training, \cf \figref{fig:introduction} (top right). Robustness is obtained by rejecting low-confidence (adversarial) examples through confidence-thresholding. As a result, having seen \emph{only} $L_\infty$ adversarial examples during training, \ConfTrain improves robustness against previously \emph{unseen} attacks, \cf \figref{fig:introduction} (bottom right), \eg, $L_2$, $L_1$ and $L_0$ adversarial examples or larger $L_\infty$ perturbations. Furthermore, robustness extends to adversarial frames \cite{ZajaxAAAIWORK2019}, distal adversarial examples \cite{HeinCVPR2019}, corrupted examples (\eg, noise, blur, transforms \etc) and accuracy of normal training is preserved better than with adversarial training.

For thorough evaluation, following best practices \cite{CarliniARXIV2019}, we adapt several state-of-the-art white- and black-box attacks \cite{MadryICLR2018,IlyasICML2018,AndriushchenkoARXIV2019,NarodytskaCVPRWORK2017,KhouryARXIV2018} to \ConfTrain by explicitly maximizing confidence and improving optimization through a backtracking scheme. In total, we consider $7$ different attacks for each threat model (\ie, $L_p$ for $p \in \{\infty, 2, 1, 0\}$), allowing up to $50$ random restarts and $5000$ iterations each. We report worst-case robust test error, extended to our confidence-thresholded setting, across \emph{all} attacks and restarts, on a \emph{per test example} basis. We demonstrate improved robustness against unseen attacks compared to standard adversarial training \cite{MadryICLR2018}, TRADES \cite{ZhangICML2019}, adversarial training using multiple threat models \cite{MainiICML2020} and two detection methods \cite{MaICLR2018,LeeNIPS2018}, while training \emph{only} on $L_\infty$ adversarial examples.

We make our code (training and evaluation) and pre-trained models publicly available at \href{http://davidstutz.de/ccat}{davidstutz.de/ccat}.


\subsection{Styling with an image transformation network with instance normalization}
\label{seq:johnson_method}
This implementation differs from the two earlier implementations, since we trained a neural network to style the images, as described in Section \ref{sec:neuralnetwork}. The implementation is one of our own experiments, where we combine the architecture from Johnson et al \cite{Johnson:1} with the ideas from Ulyanov et al \cite{Ulyanov:1}. Our architecture was based on Figure \ref{fig:architecture}. We trained our image transformation network on the MS-COCO dataset \cite{Coco:1}, using stochastic gradient descent. The neural network consists of $5$ residual layers, where each residual layer is based on Figure \ref{fig:residual}. The convolutional layers are followed up by ReLU nonlinearities, except the last layer, which is followed up by a scaled $\tanh$, as discussed in Section \ref{sec:neuralnetwork}. To train the network faster, we replaced Batch normalization in the residual layers with Instance normalization, as presented by Ulyanov et al \cite{Ulyanov:1}. This was not done by Johnson et al.\newline\newline
The loss network $\phi$ was implemented by using a pre-trained VGG19-network, unlike the VGG16-network used by Johnson et al \cite{Johnson:1}. The only difference between these two networks is the number of layers used in the network. We used the following layers in the VGG-19 network to extract the feature maps for the output image $\hat{y}$ from the image transformation network, and for $y_c$ and $y_s$, the content and style images. The perceptual loss functions used to calculate the style loss and content loss, are discussed in Section \ref{sec:loss}.\newline\newline
To conclude, our architecture was based on Figure $\ref{fig:architecture}$, with the exception that we used instance normalization instead of batch normalization, and used a different loss network $\phi,$ namely VGG-19. As discussed in Section \ref{seq:johnson}, we concluded that it is faster to style an image with this method because we only have to do one forward-pass through the image transformation network to get the styled image. Combining this method with instance normalization made the stylization of the images faster, as discussed in Section \ref{sec:instance_normalization}. This speed-up let us produce videos in $30$ frames per second with a maximum dimension of 1024. We fed the images to the network frame for frame, and we did not use temporal loss as discussed in Section \ref{seq:ruder}. Because we used a neural network to solve the optimization problem, the flickering on the video became surprisingly low. This is because the trained image transformation network is parameterized by a distinct set of weights $W,$ and these do not change during of the frames. So two consecutive frames $x_i$ and $x_{i+1}$ will yield almost the same stylized images $\hat{y}_i$ and $\hat{y}_{i+1},$ resulting in lower flickering and a smoother video. The results from this implementation can be viewed in Section \ref{seq:johnson_result}.


\documentclass[a4paper,10pt]{article}
%\documentclass[a4paper,10pt]{scrartcl}

\usepackage{geometry}
\geometry{
 a4paper,
 total={175mm,243mm},
 left=20mm,
 top=20mm,
 }

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathrsfs}
\usepackage{dsfont}

\usepackage{physics}
\usepackage{siunitx}
\usepackage{slashed}

\usepackage[utf8]{inputenc}

\title{}
\author{Carlos Lopetegui, Ayoub Ouazzani, Matteo Vilucchio}
\date{\today}

\pdfinfo{%
  /Title    ()
  /Author   ()
  /Creator  ()
  /Producer ()
  /Subject  ()
  /Keywords ()
}

\begin{document}
\maketitle
\section{Deduction of the Gross-Pitaevskii equation}
Gross-Pitaevskii equation describes the zero temperature properties of the non-uniform Bose gas when the scattering length $a$ is much less than the mean interparticle distance. In what follows we follow the derivation presented in \cite{petchik} \\
The effective interaction between two particles at low energies is a constant in the momentum representation, given by :
\begin{equation}
 U_0=\frac{4 \pi \hbar^2a}{m}
\end{equation}
When translated into the coordinate space it represents a contact interaction:
\begin{equation}
 U(\vec{r},\vec{r}')=U_0 \delta(\vec{r}-\vec{r}')
\end{equation}
where $\vec{r}$ and $\vec{r}'$ represent the position of the two particles. Adopting the Hartree or mean field approach we can assume a symmetrized product of single particle wave functions as the wave function of the many body system. Thus, in the fully condensate state, when all bosons are in the saame single particle state $\phi(\vec{r})$ the $N$-particle wave function is:
\begin{equation} \label{basic}
 \Phi(\vec{r}_1,\vec{r}_2,...,\vec{r}_N)=\prod_{i=1}^{N}\phi(\vec{r_i})
\end{equation}
Where, as usual the single particle state is normalized.
To introduce in the description the effects of the correlations produced by the interaction when two atoms are close to each other  we should take into account the effective interaction $U_0\delta{(\vec{r}-\vec{r}')}$, which include the influence of short wavelength degrees of freedom, that have been set aside in the derivation of \ref{basic}. Then,
\begin{equation}
 H=\sum_{i=1}^N \left(\frac{p_i^2}{2m}+V(\vec{r}_i)\right)+U_0\sum_{i<j}\delta(\vec{r}_i-\vec{r_j})
\end{equation}
Where $V(\vec{r})$ is the eternal potential. If now we insert \ref{basic} in this Hamiltonian, to consider first order perturbation, we get:
\begin{equation}
 \hat{H}\prod_{i=1}^{N}\phi(\vec{r_i})=E \prod_{i=1}^{N}\phi(\vec{r_i})
\end{equation}
where $E$ is the expectation value of $\hat{H}$ in the state \ref{basic}. we compute it now:
\begin{equation}\label{En_Statev}
 E=\langle\Phi(\vec{r}_1,\vec{r}_2,...,\vec{r}_N)|\hat{H}|\Phi(\vec{r}_1,\vec{r}_2,...,\vec{r}_N)\rangle
\end{equation}
In order to do it step by step, let's consider now:
\begin{equation}\label{H_action}
 \hat{H}\Phi(\vec{r}_1,\vec{r}_2,...,\vec{r}_N)=\sum_{i=1}^{N}\left(-\frac{\hbar^2}{2m}\nabla^2\phi(\vec{r}_i)+V(\vec{r}_i)\phi(\vec{r}_i)\right)\prod_{j\neq i}\phi(\vec{r}_j)+U_0\sum_{i<j}\delta(\vec{r}_i-\vec{r}_j)\prod_{k}\phi(\vec{r}_k)
\end{equation}
Now we determine the value of E using \ref{En_Statev}:
\begin{equation}\label{Energy}
 E=\int d\vec{r}_1 d\vec{r}_2 ... d\vec{r}_N \Phi^{*}\hat{H}\Phi
\end{equation}
Let's treat it term by term of \ref{H_action} starting from the easiest one, which is the one containing the interactions:
\begin{equation}
\int  d\vec{r}_1 d\vec{r}_2 ... d\vec{r}_N \left( U_0\sum_{i<j}\delta(\vec{r}_i-\vec{r}_j)\prod_{k}\phi(r_k)^{*}\phi(\vec{r}_k)\right)
\end{equation}
Clearly, the integral of each term of the sum is one, times $\int d\vec{r_i} |\phi(\vec{r_i})|^4$, due to normalization and to the integration properties of the $\delta$ function. Then, we just have to determine the number of terms, which is indeed $N(N-1)/2$, which is the number of pairs of two elements in the ensemble of N. So, the previous integral reduce to:
\begin{equation}\label{term3}
 \frac{N(N-1)}{2}U_0\int d\vec{r_i} |\phi(\vec{r_i})|^4
\end{equation}
The next term to consider would be
\begin{equation}\label{term2}
 \int d\vec{r_i} d\vec{r_2} ... d\vec{r_N}\left(\sum_{i=1}^{N}V(\vec{r}_i)\prod_{j=1}^{N}\phi(\vec{r}_j)\right)=N\int d\vec{r} V(\vec{r})|\phi(\vec{r})|^2
\end{equation}
For the remaining term we just have to do the same as have been doing until here, i.e. consider the normalization of all states, so that when we integrate over $\vec{r}_{j\neq i}$ it results in one, and consider that all the terms in the sum are equal as the one particle wave functions are all the same. So, we get:
\begin{equation}
 \int \left(\sum_{i=1}^{N}\left(-\frac{\hbar^2}{2m}\nabla^2\phi(\vec{r}_i)\prod_{j\neq i}\phi(\vec{r}_j)\right)\right)=N\int d\vec{r} \left(-\frac{\hbar^2}{2m}\nabla^2\phi(\vec{r})\right)
\end{equation}
Now, the latter can just be integrated by parts and results in:
\begin{equation}\label{term1}
 N\int d\vec{r} \left(\frac{\hbar^2}{2m}|\nabla\phi(\vec{r})|^2\right)
\end{equation}
Now, substituting \ref{term1},\ref{term2} and \ref{term3} into \ref{Energy}, we get:
\begin{equation}\label{Energy_int}
 E=N\int d\vec{r}\left(\frac{\hbar^2}{2m}|\nabla\phi(\vec{r})|^2+V(\vec{r})|\phi(\vec{r})|^2+\frac{(N-1)}{2}U_0|\phi(\vec{r})|^4\right)
\end{equation}
Let's now just as a starting point consider a uniform Bose gas. In this case the single particle wave function of a particle in the ground state, in a volume V, is $1/V^{1/2}$, and then, the interaction energy of two particles is given by $U_0/V$. Then, for a state with N bosons all in the same state, the interaction energy is:
\begin{equation}
 E=\frac{N(N-1)}{2V}U_0 \approx\frac{1}{2}V n^2 U_0
\end{equation}
where $n=N/V$, and we consider the scenario where $N\gg1$.\\
In what follows, it is convenient to introduce the wave function of the condensate, given by
\begin{equation}
 \psi(\vec{r})=N^{1/2}\phi(\vec{r})
\end{equation}
so that the density of particles is given by:
\begin{equation}
 n(\vec{r})=|\psi(\vec{r})|^2
\end{equation}
So that the equation \ref{Energy_int} reduces to:
\begin{equation}\label{Energy_cond_wave_f}
 E=\int d\vec{r}\left(\frac{\hbar^2}{2m}|\nabla\psi(\vec{r})|^2+V(\vec{r})|\psi(\vec{r})|^2+\frac{1}{2}U_0|\psi(\vec{r})|^4\right)
\end{equation}
As we are looking forr the zero temperature state, all what we have to do now is to minimize the Energy with respect independent variations of $\psi(\vec{r})$ and $\psi^{*}(\vec{r})$, under the constraint that the total number of particles remain constant.
\begin{equation}
 N=\int d\vec{r}|\psi(\vec{r})|^2
\end{equation}
So, what we have to determine is the condition which ensures:
\begin{equation}
 \delta E- \mu \delta N=0
\end{equation}
where $\mu$ is just a Lagrange multiplier which in fact matches the chemical potential of a particle in the condensate (energy per single particle). The previous equation in fact results into a variation where we get two terms, one multiplying the variation of $\delta \psi$ and other the variation of $\delta \psi^*$, but once you consider one of them the other provides no new information as equals the comple conjugate of the first equation. Then, now we show just the variation with respect to $\delta \psi^*$:
\begin{equation}
 \delta E-\mu \delta N \sim \int d\vec{r} \left(\frac{\hbar^2}{2m}\nabla( \delta \psi^*)\nabla \psi +V(\vec{r})\delta \psi^* \psi +U_0 |\psi(\vec{r})|^2 \psi(\vec{r})-\mu \psi(\vec{r}) \delta \psi^*\right)=0
\end{equation}
Integrating by parts the first term, considering a localized system, so that the boundary terms can be eliminated, we get:
\begin{equation}
 \delta E-\mu \delta N \sim \int d\vec{r}\delta \psi^* \left(\frac{-\hbar^2}{2m}\nabla^2 \psi(\vec{r}) +V(\vec{r}) \psi +U_0 |\psi(\vec{r})|^2 \psi(\vec{r})-\mu \psi(\vec{r})\right)=0
\end{equation}
So, that considering that it has to be true for any arbitrary variation $\delta \psi^*$, we get the condition fro minimizing the Energy:
\begin{equation}\label{GP_Eq1}
 \frac{-\hbar^2}{2m}\nabla^2 \psi(\vec{r}) +V(\vec{r}) \psi +U_0 |\psi(\vec{r})|^2 \psi(\vec{r})=\mu \psi(\vec{r})
\end{equation}
Which can be rewritten as:
\begin{equation}\label{GP_Eq}
 \left(\frac{-\hbar^2}{2m}\nabla^2 +V(\vec{r}) +U_0 |\psi(\vec{r})|^2 \right)\psi(\vec{r})=\mu \psi(\vec{r})
\end{equation}
Last equation is the time independent Gross-Pitaevski equation. This clearly has the form of a Schrodinger equation in the presence of a potential which is the result of combining the potential $V(\vec{r})$ with the nonlinear term $U_0|\psi(\vec{r})|^2$, which represents the mean field effect of the whole condensate over each individual member of the system of bosons. On the other side, the eigenvalue of the equation is the chemical potential, which in the case of non interacting particles, all in the same state is the energy per particle, yet the same is no true for interacting particles. For a uniform Bose gas, this equation reads in fact:
\begin{equation}\label{GP_uniform_Bose_gas}
\mu=U_0|\psi(\vec{r})|^2=U_0 n
\end{equation}

\subsection{Dimensionless Gross-Pitaevskii Equation}
To proceed with numerical simulation is more useful to work with a dimensionless GP equation. This result can be achieved by scaling quantities by unit dimensional quantities. In the particular case of an isotropic harmonic oscillator potential one has that the scaling quantities can be chosen as:
\begin{equation}\label{eq:dimensionless-units}
	\tilde{t}=\omega_{x} t, \quad \tilde{\mathbf{x}}=\frac{\mathbf{x}}{x_{s}}, \quad \tilde{\psi}(\tilde{\mathbf{x}}, \tilde{t})=x_{s}^{3 / 2} \psi(\mathbf{x}, t)
\end{equation}
Now by substituting inside the Gross-Pitaevskii equation and rescaling the relation one obtains:
\[
	\mathrm{i} \varepsilon \frac{\partial \psi(\mathbf{x}, t)}{\partial t}=-\frac{\varepsilon^{2}}{2} \nabla^{2} \psi(\mathbf{x}, t)+V(\mathbf{x}) \psi(\mathbf{x}, t)+\delta \varepsilon^{5 / 2}|\psi(\mathbf{x}, t)|^{2} \psi(\mathbf{x}, t)
\]
where some constants has been introduced:
\begin{equation}\label{eq:adim-param}
	\varepsilon=\frac{\hbar}{\omega m x_{s}^{2}}=\left(\frac{a_{0}}{x_{s}}\right)^{2}, \quad \delta=\frac{U_{0} N}{a_{0}^{3} \hbar \omega_{x}}=\frac{4 \pi a N}{a_{0}}, \quad a_{0}=\sqrt{\frac{\hbar}{\omega_{x} m}}
\end{equation}
where the product $\kappa := \delta \epsilon^{5/2}$ is called coefficient of non linearity.

\section{Time dependent Gross-Pitaevskii Equation}
The time dependent Gross-Pitaevskii equation, which describe the dynamics of the condensate in a first approximation. It is natural to generalize the previously obtained statinary ``Schrodinger'' equation and directly define the $GP$ time dependent equation as a Schrodinger equation in the presence of a potential perturbed for the nonlinear term $U_0|\psi(\vec{r},t)|^2$, which in turn is:
\begin{equation}\label{GP_time_Dep}
 i\hbar \frac{\partial \psi(\vec{r},t)}{\partial t}=\left(\frac{-\hbar^2}{2m}\nabla^2 +V(\vec{r},t) +U_0 |\psi(\vec{r},t)|^2 \right)\psi(\vec{r}),t
\end{equation}
But just as the time independent one, the latter can be derived from variational principles considering:
\begin{equation}
 \delta \int_{t_1}^{t2}Ldt=0
 \end{equation}
 where $L$, is given by:
 \begin{equation}
  L=\int d\vec{r}\frac{i\hbar}{2}\left(\psi^*\frac{\partial \psi}{\partial t}-\psi \frac{\partial \psi^*}{\partial t}\right)-E
 \end{equation}
 which is equivalent to:
 \begin{equation}
  L=\int d\vec{r}\frac{i\hbar}{2}\left(\psi^*\frac{\partial \psi}{\partial t}-\psi \frac{\partial \psi^*}{\partial t}-\epsilon\right)
 \end{equation}
where $\epsilon=\frac{\hbar^2}{2m}|\nabla\psi|^2+V(\vec{r})|\psi|^2+\frac{1}{2}U_0|\psi|^4$. Solving the variational problem one directly determines the time dependent Gross-Pitaevskii equation \ref{GP_time_Dep}.

\section{Dimensionless Time Dependant Gross-Pitaevskii Equation}
For the Time Dependant Gross-Pitaevskii equation one can find a dimensionless form like the following. One starts with defining the same dimensionless variables as in (\ref{eq:dimensionless-units}) and apply them to the following equation.
\begin{equation*}
	\hbar \pdv{\psi}{t} = -\frac{\hbar^2}{2m} \laplacian \psi + \frac{m}{2}\omega^2\qty(x^2 + y^2) \psi + NU_0 \abs{\psi}^2 \psi
\end{equation*}
where for dimensional reasons one chooses $\hbar$ as the proportionality coefficient for the time derivative term. The time and spatial derivative become:
\begin{align*}
	\pdv{t} &= \pdv{\tilde{t}}{t} \pdv{\tilde{t}} = \omega \pdv{\tilde{t}} \\
	\pdv[2]{x} &= \pdv{\tilde{x}}{x} \pdv{\tilde{x}} \qty(\pdv{\tilde{x}}{x} \pdv{\tilde{x}}) = \frac{1}{x_s^2} \pdv[2]{\tilde{x}}
\end{align*}
and then by substituting one has that:
\begin{equation*}
	\frac{\hbar \omega}{x_s^{3/2}} \pdv{\tilde{\psi}}{\tilde{t}} = -\frac{\hbar^2}{2m} \frac{1}{x_s^{3/2}}\frac{1}{x_s^2} \tilde{\laplacian} \tilde{\psi} + \frac{m\omega^2}{2}\frac{x_s^2}{x_s^{3/2}}\qty(\tilde{x}^2 + \tilde{y}^2) \tilde{\psi} + NU_0\frac{1}{x_s^{3/2}}\frac{1}{x_s^{3}} \abs{\tilde{\psi}}^2 \tilde{\psi}
\end{equation*}
Now by removing the same power of $x_s$ from all the terms and multiplying by $1/m\omega^2 x_s^{1/2}$ one obtains from the previous definition of parameters (\ref{eq:adim-param}) one obtains, dropping the tilde notation, that:
\begin{equation}
	\varepsilon \pdv{\psi}{t} = -\frac{\varepsilon^2}{2} \laplacian \psi + \frac{1}{2} \qty(x^2 + y^2) \psi + \delta \epsilon^{5/2} \abs{\psi}^2 \psi
\end{equation}
to which we will refeer to as the \textit{time dependent dimensionless Gross-Pitaevskii equation}.

\end{document}


\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
%
\input{common_symbols_and_format.tex}%
\usepackage{tocloft}%
\renewcommand{\cfttoctitlefont}{\Large\bfseries}%
%
\begin{document}%
\normalsize%
\logo%
\rulename{Rolling Price Change Regression}%
\tblofcontents%
\ruledescription{Regresses the one-day price changes against the prior day's change in the price for the specified number of days.}%

\howtotrade{
    This trading rules regresses the 1-day price changes seen historical against the prior day's change of the
     price series: larger positions are taken if the research signal is increasing.
}

\ruleparameters{Amplitude}{0.1}{Amplitude weighting (Kelly Fraction). 1.0 is maximum growth if regression is exact. <1.0 scales down positions taken.}{F}{Regression Length}{100}{This is the number of days used to estimate the regression coefficients.}{L}%
\stoptable%

\section{Equation}
Below are the equations which govern how this trading rule determines a trading position.

\begin{equation}
\regressionprice_\currenttime = \amplitudecoefficient\left(\frac{\price_\currenttime}{\price_{\currenttime - 1}} - 1\right)+\constantc
\label{eq1}
\end{equation}

The equation ($\ref{eq1}$) predicts the value of the price $\regressionprice_\currenttime$ at time $\currenttime$ using the ratio of the market value at time $\currenttime$ at $\price_\currenttime$ and the market value of the first day price changes seen historically $\price_{\currenttime-1}$.The amplitude coefficient $\amplitudecoefficient$ is related to the slope of the equation (\ref{eq1}), since in this trading rule the data behaviour is analyzed  using regression analysis. In order to calculate the resultant fractional portfolio allocation $\position_{\currenttime}$ we use the Kelly fraction to obtain the maximum results for the long run.

\begin{equation}
\position_\currenttime = \kellyfraction \frac{\regressionprice_\currenttime}{ \rmserror_{\regressionprice}^{2}}
\label{eq2}
\end{equation}
Additionally, the standard error $\rmserror_{\regressionprice}$ is calculated and included in equation (\ref{eq2}) to normalize the predicted price.

\assumptions%
\keyterms%
\furtherlinks%
\end{document}


\documentclass[letterpaper,9pt,twoside,printwatermark=false]{pinp}

%% Some pieces required from the pandoc template
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

% Use the lineno option to display guide line numbers if required.
% Note that the use of elements such as single-column equations
% may affect the guide line number alignment.

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

% The geometry package layout settings need to be set here...
\geometry{layoutsize={0.95588\paperwidth,0.98864\paperheight},%
          layouthoffset=0.02206\paperwidth,%
		  layoutvoffset=0.00568\paperheight}

\definecolor{pinpblue}{HTML}{185FAF}  % imagecolorpicker on blue for new R logo
\definecolor{pnasbluetext}{RGB}{101,0,0} %

\title{DALITE Q4 - Normal Curve Calculations and Confidence Intervals.
Solutions.}

\author[a]{EPIB607 - Inferential Statistics}

  \affil[a]{Fall 2018, McGill University}

\setcounter{secnumdepth}{5}

% Please give the surname of the lead author for the running footer
\leadauthor{Bhatnagar and Hanley}

% Keywords are not mandatory, but authors are strongly encouraged to provide them. If provided, please include two to five keywords, separated by the pipe symbol, e.g:
 \keywords{  Normal calculations |  Confidence intervals |  Central Limit Theorem (CLT)  }

\begin{abstract}
This DALITE quiz will cover the normal curve calculations and confidence
intervals.
\end{abstract}

\dates{This version was compiled on \today}
\doi{\url{https://sahirbhatnagar.com/EPIB607/}}

\pinpfootercontents{DALITE Q4 due Sepetember 26, 2018 by 5pm}

\begin{document}

% Optional adjustment to line up main text (after abstract) of first page with line numbers, when using both lineno and twocolumn options.
% You should only change this length when you've finalised the article contents.
\verticaladjustment{-2pt}

\maketitle
\thispagestyle{firststyle}
\ifthenelse{\boolean{shortarticle}}{\ifthenelse{\boolean{singlecolumn}}{\abscontentformatted}{\abscontent}}{}

% If your first paragraph (i.e. with the \dropcap) contains a list environment (quote, quotation, theorem, definition, enumerate, itemize...), the line after the list may have some extra indentation. If this is the case, add \parshape=0 to the end of the list environment.

\section{Normal Calculations}\label{normal-calculations}

Cholesterol levels among fourteen-year-old boys are roughly Normal with
mean 170 and standard deviation 30 milligrams per deciliter (mg/dl). In
a SRS of 4 fourteen-year-old boys, the probability that the average
cholesterol level is 200 mg/dl or more is close to (simply provide the
corresponding \texttt{R} code used to answer this question in your
rationale)

\begin{enumerate}
\def\labelenumi{\alph{enumi}.}
\tightlist
\item
  \textbf{0.023 (Correct)}
\item
  0.159
\item
  0.977
\end{enumerate}

\subsection{Correct rationales}\label{correct-rationales}

\begin{itemize}
\tightlist
\item
  \texttt{stats::pnorm(q=200,\ mean=170,\ sd=(30/sqrt(4)),\ lower.tail\ =\ FALSE)}
\end{itemize}

\subsection{Incorrect rationales}\label{incorrect-rationales}

\begin{itemize}
\tightlist
\item
  \texttt{stats::pnorm(q=\ 200,\ mean=170,\ sd=\ 30)}
\item
  \texttt{stats::pnorm(q=200,\ mean\ =\ 170,\ sd\ =\ 30,\ lower.tail\ =\ FALSE)}
\item
  \texttt{1-mosaic::xpnorm(q=200,\ mean\ =\ 170,\ sd=30)}
\end{itemize}

\section{Normal calculations 2}\label{normal-calculations-2}

Suppose that the distribution of heights of all male students on your
campus is Normal with mean 70 inches and standard deviation 2.8 inches.
How large a simple random sample (SRS) do you need to reduce the
standard deviation of the mean to 0.5?

\begin{enumerate}
\def\labelenumi{\alph{enumi}.}
\tightlist
\item
  31.36
\item
  \textbf{32 (Correct)}
\item
  6
\item
  12
\end{enumerate}

\subsection{Correct rationales}\label{correct-rationales-1}

\begin{itemize}
\tightlist
\item
  If we want to reduce the standard deviation of the sample mean to 0.5
  inches, then we must choose \(n\) to satisfy \(2.8/ \sqrt{n} = 0.5\).
  Solving for \(n\) gives \(n = (2.8/0.5)^2 = 31.36\). So we need 32
  people (cant have 31.36 people).
\item
  Standard deviation of the mean is equal to standard deviation of the
  population over the square root of \(n\). \(0.5 = 2.8 / \sqrt{n}\).
  Solve for \(n\). \(n = 31.36\) Round up to nearest whole person to get
  standard deviation of the mean equal to 0.5.
\item
  We cannot take \(n=31\) because then our standard deviation of the
  mean would not be at least 0.5
\end{itemize}

\subsection{Incorrect rationales}\label{incorrect-rationales-1}

\begin{itemize}
\tightlist
\item
  \(2.8/\sqrt{n}=0.5 \to\) \(n=(2.8/0.5)^2 \to\) \(n=31.36\)
\end{itemize}

\section{Confidence Interval 1}\label{confidence-interval-1}

A study reports the mean change in HDL of adults eating raw garlic six
days a week for six months. The margin of error for a 95\% confidence
interval is given as plus or minus 6 milligrams per deciliter of blood
(mg/dl). This means that

\begin{enumerate}
\def\labelenumi{\alph{enumi}.}
\tightlist
\item
  we can be certain that the study result is within 6 mg/dl of the truth
  about the population
\item
  we could be certain that the study result is within 6 mg/dl of the
  truth about the population if the conditions for inferences were
  satisfied
\item
  \textbf{the study used a method that gives a result within 6 mg/dl of
  the truth about the population in 95\% of all samples (Correct)}
\end{enumerate}

\subsection{Correct rationales}\label{correct-rationales-2}

\begin{itemize}
\tightlist
\item
  Confidence interval comments about the method of obtaining the result,
  not the result itself. A 95\% confidence interval implies that we can
  be certain that if this method was repeated, 95\% of the time it will
  be within 6 mg/dl of the population mean.
\item
  You cannot determine whether the true mean was within or outside the
  CI. We do not know the true mean. The confidence interval's purpose is
  to provide a certain level of confidence on the method.
\item
  The confidence interval of 95\% means that the test will give accurate
  results (within 6 milligrams per decilitre of blood) 95\% of the time
\item
  The confidence level states the probability that the method will give
  a correct result. Since the confidence level is 95\%, we can only be
  certain that 95\% of the time the method will correctly capture the
  true mean.
\end{itemize}

\subsection{Incorrect rationales}\label{incorrect-rationales-2}

\begin{itemize}
\tightlist
\item
  If conditions were satisfied(all statistics such as mean, sd, and n
  were calculated), the result interprets that 95\% of the data will
  fall within the CI range.
\item
  A 95\% confidence interval indicates that 95\% of observations will
  fall within the given margin
\item
  As per the definition of confidence interval
\item
  1.96 x sigma/square root of n
\item
  95\% of the population distribution is contained in the confidence
  interval
\end{itemize}

\section{Confidence Intervals 2}\label{confidence-intervals-2}

A laboratory scale is known to have a standard deviation of \(\sigma\) =
0.001 gram in repeated weighings. Scale readings in repeated weighings
are Normally distributed, with mean equal to the true weight of the
specimen. Three weighings of a specimen on this scale give 3.412, 3.416
and 3.414 grams. Answer both questions below:

\begin{enumerate}
\def\labelenumi{\roman{enumi})}
\tightlist
\item
  A 95 \% confidence interval for the true weight is
\item
  The margin of error for a 99\% confidence interval would be
\end{enumerate}

\begin{enumerate}
\def\labelenumi{\alph{enumi}.}
\item
  \begin{enumerate}
  \def\labelenumii{\roman{enumii})}
  \tightlist
  \item
    3.414 +/- 0.00113
  \item
    smaller
  \end{enumerate}
\item
  \begin{enumerate}
  \def\labelenumii{\roman{enumii})}
  \tightlist
  \item
    3.414 +/- 0.00113
  \item
    about the same
  \end{enumerate}
\item
  \textbf{i) 3.414 +/- 0.00113 ii) larger (Correct)}
\item
  \begin{enumerate}
  \def\labelenumii{\roman{enumii})}
  \tightlist
  \item
    3.414 +/- 0.00065
  \item
    larger
  \end{enumerate}
\item
  \begin{enumerate}
  \def\labelenumii{\roman{enumii})}
  \tightlist
  \item
    3.414 +/- 0.00196
  \item
    larger
  \end{enumerate}
\end{enumerate}

\subsection{Correct rationales}\label{correct-rationales-3}

\begin{itemize}
\item
  \begin{enumerate}
  \def\labelenumi{\roman{enumi})}
  \tightlist
  \item
    3.414 \(\pm\) 1.96 \(\times\) \(0.001/\sqrt{3} = 3.414 \pm 0.00113\)
    ii) larger because 99\% corresponds to a z value of 2.58
  \end{enumerate}
\item
  The answer is C because the true weight for a 95\% confidence interval
  type is 3.414 \(\pm\) 0.00113, the margin of error for a 99\%
  confidence interval would be larger because for me to be right 99\% of
  the time the interval will have to be a lot larger than if i were to
  be right 95\% of the time
\end{itemize}

\subsection{Incorrect rationales}\label{incorrect-rationales-3}

\begin{itemize}
\item
  \begin{enumerate}
  \def\labelenumi{\roman{enumi})}
  \setcounter{enumi}{1}
  \tightlist
  \item
    About the same since we are still only using n=3
  \end{enumerate}
\item
  \begin{enumerate}
  \def\labelenumi{\roman{enumi})}
  \setcounter{enumi}{1}
  \tightlist
  \item
    Would be larger, as including more people
  \end{enumerate}
\end{itemize}

\section{Confidence Intervals 3}\label{confidence-intervals-3}

You calculate a 95\% confidence interval of 27 \(\pm\) 2 centimeters
(cm) for the mean needle length of Torrey pine trees. You ask a friend
to explain this result. He believes it means that ``95\% of all Torrey
pine needles have lengths between 25 and 29 cm.'' Is he right? or wrong?
Explain your answer in the rationale.

\begin{enumerate}
\def\labelenumi{\alph{enumi}.}
\tightlist
\item
  He is right
\item
  \textbf{He is wrong (Correct)}
\end{enumerate}

\subsection{Correct rationales}\label{correct-rationales-4}

\begin{itemize}
\tightlist
\item
  We can't know that. the mean is either 27 \(\pm\) 2cm or it's not.
  What the 95\% confidence interval means is that 95\% of the time, 27
  \(\pm\) 2cm will contain the true value of the mean needle length
\item
  The 95\% confidence interval suggests that we are 95\% certain that
  the true mean of the population is between those two numbers - not
  that 95\% of the individual values will fall between these numbers.
\item
  It means that this confidence interval has a 95\% chance to capture
  the population mean needle length of Torrey pine trees. It cannot
  indicate the population distribution.
\end{itemize}

\subsection{Incorrect rationales}\label{incorrect-rationales-4}

\begin{itemize}
\tightlist
\item
  The parameter in this case is the mean needle length of all Torrey
  pine trees. By the definition of a CI, this has a probability of 0.95
  of being within the interval. So he is right.
\item
  Because if they repeatedly took samples we would see that 95\% of them
  would contain the population mean of 27.
\item
  95\% of samples trees will have lengths between 25 and 29 cm
\item
  He is wrong because a number that lies within one standard deviation
  of the mean which would be 25 and 29 is a 68\% confidence interval
  while numbers lying 2 standard deviations from the mean is a 95\%
  confidence interval (meaning a range of 23 to 31)
\item
  25cm-29cm would be the 68\% confidence interval
\item
  Assuming the inferences are met (normally distributed, etc.), we have
  said with a 95\% confidence interval (i.e.~using a method that is
  right 95\% of the time) that 95\% of all lengths of Torrey pine
  needles are between 25 and 29cm in length.
\end{itemize}

\section{Confidence Intervals 4}\label{confidence-intervals-4}

A New York Times poll on women's issues -- which interviewed 1025 women
randomly selected from the United States excluding Alaska and Hawaii--
in which 47\% of the women said they do not get enough time for
themselves; the poll reported a margin of error of \(\pm\) 3 percentage
points for 95\% confidence in the conclusions.

Which of the following statements best explains what ``95\% confidence''
means.

\begin{enumerate}
\def\labelenumi{\alph{enumi}.}
\tightlist
\item
  This poll is accurate 19 times out of 20. (NO. This poll is either
  accurate or its not)
\item
  95\% chance that the info is correct for between 44 and 50\% of women.
  (NO. 95\% confidence in the procedure that produced the interval
  44-50)
\item
  In 95 of 100 comparable polls, expect 44 - 50\% of women will give the
  same answer. (NO. Same answer? as what?)
\item
  If this same poll were repeated many times, then 95 of every 100 such
  polls would give a range that included 47\%. (NO. Estimate will be
  between \(\mu - \textrm{margin}\) and \(\mu + \textrm{margin}\) in
  95\% of applications.)
\item
  It means that 47\% give or take 3\% is an accurate estimate of the
  population mean 19 times out of 20 such samplings. (NO. 95\% of
  applications of CI give correct answer. How can the same interval 47\%
  \(\pm\) 3 be accurate in 19 but not in the other 1?)
\end{enumerate}

\subsection{Correct rationales}\label{correct-rationales-5}

\subsection{Incorrect rationales}\label{incorrect-rationales-5}

\begin{itemize}
\tightlist
\item
  95\% confidence translates to saying this poll is accurate 95\% of the
  time
\item
  There is 95\% probability that the calculated confidence interval will
  include the mean (47\%).
\item
  95 out of 100 times, 47\% will be in this range--this is what the
  confidence interval states.
\item
  the answer is D because ``95\% confidence'' means that you are 95\%
  confident that the value you have will be right
\item
  95\% of the samples will contain the true parameter value (which in
  this case is 47\%).
\item
  Definition of confidence interval is that with 95\% confidence, our
  true mean lies within the margin of error, and 95\% of the time, our
  sample mean will also lie within that margin of error.
\item
  the answer is D because ``95\% confidence'' means that you are 95\%
  confident that the value you have will be right
\item
  95\% confidence indicates confidence level- reflects that method will
  give these same results 95\% of the time
\item
  when you do a poll of this size 100 times, 95\% will contain the value
  47\% +/- 3\% points
\item
  While we can not be sure whether the poll is accurate at reflecting
  the true mean, we can be 95\% sure that our methodology if we repeat
  the poll many times, 95 of 100 polls would include a range that
  include 47\%.
\item
  If we were to repeat the same sample many times, we are using the same
  methodology, 95 of every 100 polls would give a mean of 47\% +/- 3\%
  which includes 47 by default. Given the margin of error of +/- 3\%,
  any number found between the minimum 44\% and maximum 50\% would also
  include 47\% in their range by default.
\item
  E explains the best because 95\% of the times the mean would be
  between 47 \(\pm\) 3
\end{itemize}

%\showmatmethods

\bibliography{pinp}
\bibliographystyle{jss}

\end{document}


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

\begin{document}
\maketitle
\newpage
\section{Introduction}
The programming assignment asks me to implement three priority queue including binary heap, unsorted heap and fibonacci heap. The goal is clear, use priority queue to find the shortest path from the source cell to the ending cell
\par Second, thstudy the efficiency of different implementations. In lecture slides 13, professor gives a summary for the time complexity for binary heap and fibonacci heap.
\begin{figure}[H]
\centering
\includegraphics[scale=0.2]{P1.png}
\caption{Random Selection}
\end{figure}

\par However, we need to test the priority queue efficiency by ourselves, so I wrote a cpp file to print out the time required for each algorithm, which is included in appendix part.
\section{Result}
To test time efficiency, I used clock() function to get the time required. To get rid of other disturbing factor, I used to variable start and stop to get the time right before and after the sorting complete, then their difference is the time used. In my analysis, I get 7 set of data ,from which the map's length of side is 5,10,20,50,75,100,1000 and the size is the length's square.
\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|c|c|c|c|c|}
\hline
map Size                 & 5    & 10   & 20     & 50    & 75    & 100  &1000
  \\ \hline
Binary Heap              &6.5e-5& 6.1e-5& 3.6e-4& 1.4e-3& 3.7e-3& 5.2e-3& 0.13      \\ \hline
Unsorted Heap        &5.5e-5& 6.3e-5& 4.2e-4& 2.6e-3& 1.1e-2& 1.9e-2& 2.1 \\ \hline
Fibonacci Heap     &1.2e-4& 1.3e-4& 1.7e-3& 4.7e-3& 1.9e-2& 2.9e-2& 0.92  \\ \hline
\end{tabular}
\end{table}
\section{Conclusion}
This chart is plot by matlab, showing the time efficiency of each algorithms compared to each other, and it has following characteristics.
\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{P3.png}
\end{figure}
\begin{enumerate}
\item The average time required for each algorithm grows as the map grows bigger.
\item When the map is small, the time used for binary heap and unsorted heap is very close but binary have an advantage when the map is bigger as well as the Fibonacci.
\item Fibonacci heap always takes the longer time than Binary Heap, which is not reasonable, I think it's because it takes more time to use implement with list, but its time complexity is similar others from the picture
\end{enumerate}
\section{Appendix}
The appendix shows the cpp code for main project and time comparison.
The following part is for the main algorithm
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P4.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P5.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P6.png}
\end{figure}
The following is for binary heap.
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P7.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P8.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P9.png}
\end{figure}
The following is for unsorted heap.
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P10.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P11.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P12.png}
\end{figure}
The following is for fibonacci heap.
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P13.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P14.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{P15.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{P16.png}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{P17.png}
\end{figure}
\end{document}


\documentclass[10pt]{article}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{tabularx}

\newcommand\tmpset[2]{
  \begingroup
  #1
  #2
  \endgroup
}

\newcommand\autoscaletable[3][0pt]{
  \tmpset{\setlength\tabcolsep{#1}}{
    \noindent
    \begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} #2}
      #3
    \end{tabular*}}
}

\begin{document}

\section{Olm: A Cryptographic Ratchet}\label{olm-a-cryptographic-ratchet}

An implementation of the double cryptographic ratchet described by
https://whispersystems.org/docs/specifications/doubleratchet/.

\subsection{Notation}\label{notation}

This document uses \(`\parallel`\) to represent string concatenation.
When \(`\parallel`\) appears on the right hand side of an \(`=`\) it
means that the inputs are concatenated. When \(`\parallel`\) appears on
the left hand side of an \(`=`\) it means that the output is split.

When this document uses \(`\operatorname{ECDH}\left(K_A,K_B\right)`\) it
means that each party computes a Diffie-Hellman agreement using their
private key and the remote party's public key. So party \(`A`\) computes
\(`\operatorname{ECDH}\left(K_B^{public},K_A^{private}\right)`\) and
party \(`B`\) computes
\(`\operatorname{ECDH}\left(K_A^{public},K_B^{private}\right)`\).

Where this document uses
\(`\operatorname{HKDF}\left(salt,IKM,info,L\right)`\) it refers to the
\href{https://tools.ietf.org/html/rfc5869}{HMAC-based key derivation
function} with a salt value of \(`salt`\), input key material of
\(`IKM`\), context string \(`info`\), and output keying material length
of \(`L`\) bytes.

\subsection{The Olm Algorithm}\label{the-olm-algorithm}

\subsubsection{Initial setup}\label{initial-setup}

The setup takes four \href{http://cr.yp.to/ecdh.html}{Curve25519}
inputs: Identity keys for Alice and Bob, \(`I_A`\) and \(`I_B`\), and
one-time keys for Alice and Bob, \(`E_A`\) and \(`E_B`\). A shared
secret, \(`S`\), is generated using
\href{https://whispersystems.org/blog/simplifying-otr-deniability/}{Triple
Diffie-Hellman}. The initial 256 bit root key, \(`R_0`\), and 256 bit
chain key, \(`C_{0,0}`\), are derived from the shared secret using an
HMAC-based Key Derivation Function using
\href{https://tools.ietf.org/html/rfc6234}{SHA-256} as the hash function
(\href{https://tools.ietf.org/html/rfc5869}{HKDF-SHA-256}) with default
salt and \texttt{"OLM\_ROOT"} as the info.

$\begin{aligned}
    S&=\operatorname{ECDH}\left(I_A,E_B\right)\;\parallel\;
       \operatorname{ECDH}\left(E_A,I_B\right)\;\parallel\;
       \operatorname{ECDH}\left(E_A,E_B\right)\\
    R_0\;\parallel\;C_{0,0}&=
        \operatorname{HKDF}\left(0,S,\text{``OLM\_ROOT"},64\right)
\end{aligned}$

\subsubsection{Advancing the root key}\label{advancing-the-root-key}

Advancing a root key takes the previous root key, \(`R_{i-1}`\), and two
Curve25519 inputs: the previous ratchet key, \(`T_{i-1}`\), and the
current ratchet key \(`T_i`\). The even ratchet keys are generated by
Alice. The odd ratchet keys are generated by Bob. A shared secret is
generated using Diffie-Hellman on the ratchet keys. The next root key,
\(`R_i`\), and chain key, \(`C_{i,0}`\), are derived from the shared
secret using \href{https://tools.ietf.org/html/rfc5869}{HKDF-SHA-256}
using \(`R_{i-1}`\) as the salt and \texttt{"OLM\_RATCHET"} as the info.

$\begin{aligned}
    R_i\;\parallel\;C_{i,0}&=
        \operatorname{HKDF}\left(
            R_{i-1},
            \operatorname{ECDH}\left(T_{i-1},T_i\right),
            \text{``OLM\_RATCHET"},
            64
        \right)
\end{aligned}$

\subsubsection{Advancing the chain key}\label{advancing-the-chain-key}

Advancing a chain key takes the previous chain key, \(`C_{i,j-1}`\). The
next chain key, \(`C_{i,j}`\), is the
\href{https://tools.ietf.org/html/rfc2104}{HMAC-SHA-256} of
\texttt{"\textbackslash{}x02"} using the previous chain key as the key.

$\begin{aligned}
    C_{i,j}&=\operatorname{HMAC}\left(C_{i,j-1},\text{``\char`\\x02"}\right)
\end{aligned}$

\subsubsection{Creating a message key}\label{creating-a-message-key}

Creating a message key takes the current chain key, \(`C_{i,j}`\). The
message key, \(`M_{i,j}`\), is the
\href{https://tools.ietf.org/html/rfc2104}{HMAC-SHA-256} of
\texttt{"\textbackslash{}x01"} using the current chain key as the key.
The message keys where \(`i`\) is even are used by Alice to encrypt
messages. The message keys where \(`i`\) is odd are used by Bob to
encrypt messages.

$\begin{aligned}
    M_{i,j}&=\operatorname{HMAC}\left(C_{i,j},\text{``\char`\\x01"}\right)
\end{aligned}$

\subsection{The Olm Protocol}\label{the-olm-protocol}

\subsubsection{Creating an outbouns session}\label{creating-an-outbound-session}

Bob publishes the public parts of his identity key, \(`I_B`\), and some
single-use one-time keys \(`E_B`\).

Alice downloads Bob's identity key, \(`I_B`\), and a one-time key,
\(`E_B`\). She generates a new single-use key, \(`E_A`\), and computes a
root key, \(`R_0`\), and a chain key \(`C_{0,0}`\). She also generates a
new ratchet key \(`T_0`\).

\subsubsection{Sending the first pre-key messages}\label{sending-the-first-pre-key-messages}

Alice computes a message key, \(`M_{0,j}`\), and a new chain key,
\(`C_{0,j+1}`\), using the current chain key. She replaces the current
chain key with the new one.

Alice encrypts her plain-text with the message key, \(`M_{0,j}`\), using
an authenticated encryption scheme (see below) to get a cipher-text,
\(`X_{0,j}`\).

She then sends the following to Bob: * The public part of her identity
key, \(`I_A`\) * The public part of her single-use key, \(`E_A`\) * The
public part of Bob's single-use key, \(`E_B`\) * The current chain
index, \(`j`\) * The public part of her ratchet key, \(`T_0`\) * The
cipher-text, \(`X_{0,j}`\)

Alice will continue to send pre-key messages until she receives a
message from Bob.

\subsubsection{Creating an inbound session from a pre-key message}\label{creating-an-inbound-session-from-a-pre-key-message}

Bob receives a pre-key message as above.

Bob looks up the private part of his single-use key, \(`E_B`\). He can
now compute the root key, \(`R_0`\), and the chain key, \(`C_{0,0}`\),
from \(`I_A`\), \(`E_A`\), \(`I_B`\), and \(`E_B`\).

Bob then advances the chain key \(`j`\) times, to compute the chain key
used by the message, \(`C_{0,j}`\). He now creates the message key,
\(`M_{0,j}`\), and attempts to decrypt the cipher-text, \(`X_{0,j}`\).
If the cipher-text's authentication is correct then Bob can discard the
private part of his single-use one-time key, \(`E_B`\).

Bob stores Alice's initial ratchet key, \(`T_0`\), until he wants to
send a message.

\subsubsection{Sending normal messages}\label{sending-normal-messages}

Once a message has been received from the other side, a session is
considered established, and a more compact form is used.

To send a message, the user checks if they have a sender chain key,
\(`C_{i,j}`\). Alice uses chain keys where \(`i`\) is even. Bob uses
chain keys where \(`i`\) is odd. If the chain key doesn't exist then a
new ratchet key \(`T_i`\) is generated and a new root key \(`R_i`\) and
chain key \(`C_{i,0}`\) are computed using \(`R_{i-1}`\), \(`T_{i-1}`\)
and \(`T_i`\).

A message key, \(`M_{i,j}`\) is computed from the current chain key,
\(`C_{i,j}`\), and the chain key is replaced with the next chain key,
\(`C_{i,j+1}`\). The plain-text is encrypted with \(`M_{i,j}`\), using
an authenticated encryption scheme (see below) to get a cipher-text,
\(`X_{i,j}`\).

The user then sends the following to the recipient: * The current chain
index, \(`j`\) * The public part of the current ratchet key, \(`T_i`\) *
The cipher-text, \(`X_{i,j}`\)

\subsubsection{Receiving messages}\label{receiving-messages}

The user receives a message as above with the sender's current chain
index, \(`j`\), the sender's ratchet key, \(`T_i`\), and the
cipher-text, \(`X_{i,j}`\).

The user checks if they have a receiver chain with the correct \(`i`\)
by comparing the ratchet key, \(`T_i`\). If the chain doesn't exist then
they compute a new root key, \(`R_i`\), and a new receiver chain, with
chain key \(`C_{i,0}`\), using \(`R_{i-1}`\), \(`T_{i-1}`\) and
\(`T_i`\).

If the \(`j`\) of the message is less than the current chain index on
the receiver then the message may only be decrypted if the receiver has
stored a copy of the message key \(`M_{i,j}`\). Otherwise the receiver
computes the chain key, \(`C_{i,j}`\). The receiver computes the message
key, \(`M_{i,j}`\), from the chain key and attempts to decrypt the
cipher-text, \(`X_{i,j}`\).

If the decryption succeeds the receiver updates the chain key for
\(`T_i`\) with \(`C_{i,j+1}`\) and stores the message keys that were
skipped in the process so that they can decode out of order messages. If
the receiver created a new receiver chain then they discard their
current sender chain so that they will create a new chain when they next
send a message.

\subsection{The Olm Message Format}\label{the-olm-message-format}

Olm uses two types of messages. The underlying transport protocol must
provide a means for recipients to distinguish between them.

\subsubsection{Normal Messages}\label{normal-messages}

Olm messages start with a one byte version followed by a variable length
payload followed by a fixed length message authentication code.

\begin{verbatim}
 +--------------+------------------------------------+-----------+
 | Version Byte | Payload Bytes                      | MAC Bytes |
 +--------------+------------------------------------+-----------+
\end{verbatim}

The version byte is \texttt{"\textbackslash{}x03"}.

The payload consists of key-value pairs where the keys are integers and
the values are integers and strings. The keys are encoded as a variable
length integer tag where the 3 lowest bits indicates the type of the
value: 0 for integers, 2 for strings. If the value is an integer then
the tag is followed by the value encoded as a variable length integer.
If the value is a string then the tag is followed by the length of the
string encoded as a variable length integer followed by the string
itself.

Olm uses a variable length encoding for integers. Each integer is
encoded as a sequence of bytes with the high bit set followed by a byte
with the high bit clear. The seven low bits of each byte store the bits
of the integer. The least significant bits are stored in the first byte.

\autoscaletable{c c c c}{
\textbf{Name} & \textbf{Tag} & \textbf{Type} & \textbf{Meaning} \\
Ratchet-Key & 0x0A & String & The public part of the ratchet key, $T_i$, of
the message \\
Chain-Index & 0x10 & Integer & The chain index, $j$, of the message \\
Cipher-Text & 0x22 & String & The cipher-text, $X_{i,j}$, of the message}

The length of the MAC is determined by the authenticated encryption
algorithm being used. (Olm version 1 uses
\href{https://tools.ietf.org/html/rfc2104}{HMAC-SHA-256}, truncated to 8
bytes). The MAC protects all of the bytes preceding the MAC.

\subsubsection{Pre-Key Messages}\label{pre-key-messages}

Olm pre-key messages start with a one byte version followed by a
variable length payload.

\begin{verbatim}
 +--------------+------------------------------------+
 | Version Byte | Payload Bytes                      |
 +--------------+------------------------------------+
\end{verbatim}

The version byte is \texttt{"\textbackslash{}x03"}.

The payload uses the same key-value format as for normal messages.

\autoscaletable{c c c c}{
\textbf{Name} & \textbf{Tag} & \textbf{Type} & \textbf{Meaning} \\
One-Time-Key & 0x0A & String & The public part of Bob's single-use key,
Eb. \\
Base-Key & 0x12 & String & The public part of Alice's single-use key,
Ea. \\
Identity-Key & 0x1A & String & The public part of Alice's identity key,
Ia. \\
Message & 0x22 & String & An embedded Olm message with its own version
and MAC. \\
}

\subsection{Olm Authenticated
Encryption}\label{olm-authenticated-encryption}

\subsubsection{Version 1}\label{version-1}

Version 1 of Olm uses
\href{http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf}{AES-256}
in
\href{http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf}{CBC}
mode with \href{https://tools.ietf.org/html/rfc2315}{PKCS\#7} padding
for encryption and
\href{https://tools.ietf.org/html/rfc2104}{HMAC-SHA-256} (truncated to
64 bits) for authentication. The 256 bit AES key, 256 bit HMAC key, and
128 bit AES IV are derived from the message key using
\href{https://tools.ietf.org/html/rfc5869}{HKDF-SHA-256} using the
default salt and an info of \texttt{"OLM\_KEYS"}.

$\begin{aligned}
    AES\_KEY_{i,j}\;\parallel\;HMAC\_KEY_{i,j}\;\parallel\;AES\_IV_{i,j}
    &= \operatorname{HKDF}\left(0,M_{i,j},\text{``OLM\_KEYS"},80\right)
\end{aligned}$

The plain-text is encrypted with AES-256, using the key
\(`AES\_KEY_{i,j}`\) and the IV \(`AES\_IV_{i,j}`\) to give the
cipher-text, \(`X_{i,j}`\).

Then the entire message (including the Version Byte and all Payload
Bytes) are passed through
\href{https://tools.ietf.org/html/rfc2104}{HMAC-SHA-256}. The first 8
bytes of the MAC are appended to the message.

\subsection{Message authentication
concerns}\label{message-authentication-concerns}

To avoid unknown key-share attacks, the application must include
identifying data for the sending and receiving user in the plain-text of
(at least) the pre-key messages. Such data could be a user ID, a
telephone number; alternatively it could be the public part of a keypair
which the relevant user has proven ownership of.

\subsubsection{Example attacks}\label{example-attacks}

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  Alice publishes her public
  \href{http://cr.yp.to/ecdh.html}{Curve25519} identity key, \(`I_A`\).
  Eve publishes the same identity key, claiming it as her own. Bob
  downloads Eve's keys, and associates \(`I_A`\) with Eve. Alice sends a
  message to Bob; Eve intercepts it before forwarding it to Bob. Bob
  believes the message came from Eve rather than Alice.

  This is prevented if Alice includes her user ID in the plain-text of
  the pre-key message, so that Bob can see that the message was sent by
  Alice originally.
\item
  Bob publishes his public \href{http://cr.yp.to/ecdh.html}{Curve25519}
  identity key, \(`I_B`\). Eve publishes the same identity key, claiming
  it as her own. Alice downloads Eve's keys, and associates \(`I_B`\)
  with Eve. Alice sends a message to Eve; Eve cannot decrypt it, but
  forwards it to Bob. Bob believes the Alice sent the message to him,
  wheras Alice intended it to go to Eve.

  This is prevented by Alice including the user ID of the intended
  recpient (Eve) in the plain-text of the pre-key message. Bob can now
  tell that the message was meant for Eve rather than him.
\end{enumerate}

\subsection{IPR}\label{ipr}

The Olm specification (this document) is hereby placed in the public
domain.

\subsection{Feedback}\label{feedback}

Can be sent to olm at matrix.org.

\subsection{Acknowledgements}\label{acknowledgements}

The ratchet that Olm implements was designed by Trevor Perrin and Moxie
Marlinspike - details at
https://whispersystems.org/docs/specifications/doubleratchet/. Olm is an
entirely new implementation written by the Matrix.org team.

\end{document}


\documentclass[11pt]{article}
\usepackage{setspace}
\usepackage{pxfonts}
\usepackage{graphicx}
\usepackage{geometry}

\geometry{letterpaper,left=.5in,right=.5in,top=1in,bottom=.75in,headsep=5pt,footskip=20pt}

\vspace{-1in}
\title{Problem Set 5 -- Extensions of the Hodgkin-Huxley model}
\author{Computational Neuroscience Summer Program}
\date{June, 2011}

\begin{document}
\maketitle

In this problem set you will be extending the standard Hodgkin-Huxley model you constructed in Problem Set 4.  In this extended model we will be simulating the actions of individual voltage-dependent Sodium and Potassium channels.  Feel free to re-use any code from Problem Set 4 that you think would be useful.  Write up your results in a text editor of your choosing.  Include any relevant figures.  Include a printout of your Matlab code as well as any calculations that aren't in the code.  You may work individually or in groups, but each student should hand in their own report.

\subsection*{Equations}

\begin{center}
\begin{tabular}{l|l|l}
$\tau_x(V)\frac{dx}{dt} = x_\infty(V) - x$ & $\tau_x(V) =
\frac{1}{\alpha_x(V) + \beta_x(V)}$ & $x_\infty(V) =
\frac{\alpha_x(V)}{\alpha_x(V) + \beta_x(V)}$ \\
\hline
$\alpha_n(V) = \frac{.01(V+55)}{1 - exp(-.1(V+55))}$ &
$\alpha_m(V) = \frac{0.1(V+40)}{1 - exp(-.1(V+40))}$ &
$\alpha_h(V) = 0.07*exp(-.05(V+65))$\\
\hline
$\beta_n(V) = 0.125*exp(-.0125(V+65))$ &
$\beta_m(V) = 4*exp(-.0556(V+65))$ &
$\beta_h(V) = \frac{1}{1 + exp(-.1(V+35))}$\\
\hline
$P_K = n^4$ & $P_{Na} = m^3h$ &\\
\hline
$g_K = \bar{g}_KP_K$ & $g_{Na} = \bar{g}_{Na}P_{Na}$ &\\

\end{tabular}
\end{center}

\subsection*{Problems}

\paragraph{1.} Construct and simulate the stochastic K$^+$ channel model as shown in the state diagram below.  The diagram shows transitions between different states of a K$^+$ channel.  The symbols $\alpha_n$ and $\beta_n$ represent the opening and closing rates, respectively, of individual K$^+$ channels (these are the same variables you used in Problem Set 4).  Set the rate constants equal to their value at 10 mV (i.e., use $\alpha_n(10 \mathrm{mV}) = 0.65$/ms and $\beta_n(10 \mathrm{mV}) = 0.05$/ms).  Transitions are made between states with each iteration of your program if a random number chosen uniformly between 0 and 1 is less than the corresponding rate for that transition time.  For example, if a channel is in state 1, that channel will transition to state 2 during the current iteration of your program if the chosen random number is less than $4\alpha_ndt$.  As the channel(s) make transitions between states, keep track of whether state 5 is occupied.  If so, assume that each channel conducts 1 pA (10$^{-12}$ A) of current; otherwise no current flows through the channel.  Plot currents generated by this model for $N = $1, 10, and 100 channels over a 20 ms period (use $dt = 0.01$ ms).

\begin{figure}[h]
\begin{center}
\includegraphics[width=0.5\textwidth]{hodgkin_huxley_advanced/K_state_diagram}
\end{center}
\end{figure}

\paragraph{2.}  The Hodgkin-Huxley description of the K$^+$ channel predicts the current flowing in these simulations would be $Nn^4$ pA, where $N$ is the number of  K$^+$ channels and $n$ is the K$^+$ activation variable (same as in Problem Set 4).  On a single plot, show the amount of current predicted by the Hodgkin-Huxley prediction and the amount of current predicted with the stochastic model.  (Use $N = 1000$.)

\paragraph{3.}  Construct an stochastic Na$^+$ channel model analogous
to the K$^+$ model, using the state diagram below.  The symbols $\alpha_m$ and $\beta_m$ represent
opening and closing rates of the three main subunits of the sodium
channel.  Activation and deinactivation rates of the sodium channel
gate are represented by $k_1$, $k_2$, and $k_3$.  Set the rate
constants equal to their value at 10 mV (i.e., use $\alpha_m(10
~\mathrm{mV}) = $5.034/ms, $\alpha_h(10~\mathrm{mV}) = $0.0016/ms, and $\beta_m(10~\mathrm{mV}) = $0.0618/ms).  Use
$k_1 = 0.24$/ms, $k_2 = 0.4$/ms, and $k_3 = 1.5$/ms.  As the channel(s) make transitions between states, keep track of whether state 4 is occupied.  If so, assume that each channel conducts -1 pA (10$^-{12}$ A) of current; otherwise no current flows through the channel.  Plot currents generated by this model for $M = $1, 10, and 100 channels over a 20 ms period.

\begin{figure}[h]
\begin{center}
\includegraphics[width=0.5\textwidth]{hodgkin_huxley_advanced/Na_state_diagram}
\end{center}
\end{figure}

\paragraph{4.}  The Hodgkin-Huxley description of the Na$^+$ channel
predicts the current flowing in these simulations would be $-Mm^3h$ pA,
where $M$ is the number of Sodium channels.  On a single plot, show the amount of current predicted by the Hodgkin-Huxley prediction and the amount of current predicted with the stochastic model.  (Use $M = 1000$.)

\paragraph{5.  Challenge problem.}  Modify the Hodgkin-Huxley model from Problem Set 5 to explicitly simulate Sodium and Potassium channels using the stochastic models above (to start, use $N = M = 1000$).  You will need to update the transition probabilities $\alpha_n, \alpha_m, \alpha_h, \beta_n,$ and $\beta_m$ with each time step, since those transition probabilities are voltage-dependent.  $k_1, k_2,$ and $k_3$ are constants.  Hint: $P_X$ is equal to the total proportion of $X$ channels  currently open.  Simulate a 20 ms interval.  Apply an external current of $I_{ext} = 5$ nA/mm$^2$ from 5 to 8 ms.  Plot $V$, $P_K$, and $P_{Na}$ as a function of time.

\end{document}


\documentclass[a4paper,12pt]{article}
\usepackage[left=2.5cm,right=2.5cm,top=2.5cm,bottom=2.5cm]{geometry}
\usepackage{color}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{amsmath,amssymb,amsthm,algorithm,algorithmic,graphicx,yhmath,url,enumitem,lscape,mathtools}
\usepackage{wrapfig,subfigure}

\newcounter{problem}
\newenvironment{problem}{\refstepcounter{problem} \noindent {\bf Problem \arabic{problem}}}{\newpage}
\newenvironment{solution}{\vspace{0.3cm} \par \noindent {\bf Solution}}{}
\newenvironment{verification}{\vspace{0.3cm} \par \noindent {\bf Verification}}{}
\newenvironment{hint}{\vspace{0.3cm} \par {\bf Hint:}}{}

\newcounter{remark}
\newenvironment{remark}{\refstepcounter{remark} \vspace{0.3cm} \par \noindent {\bf Remark \arabic{remark}}}{\vspace{0.3cm}}
\newcounter{lesson}
\newenvironment{lesson}{\refstepcounter{lesson} \vspace{0.3cm} \par \noindent {\bf Lesson \arabic{lesson}}}{\vspace{0.3cm}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Rn}{\mathbb{R}^n}
\newcommand{\Rnn}{\mathbb{R}^{n \times n}}
\newcommand{\bes}{\begin{equation*}}
\newcommand{\ees}{\end{equation*}}
\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}
\newcommand{\eps}{\epsilon}
\newcommand{\fl}{\text{fl}}

\begin{document}

\title{5DV005, Fall 2018, Lab session 6}
\author{Carl Christian Kjelgaard Mikkelsen}

\maketitle
\tableofcontents

\section{The time and the place}
The lab session will take place on
\begin{center}
Wednesday, December 12th, 2018, (kl. 13.00-16.00), Room MA416-426.
\end{center}

\section{The problems}

\begin{problem} Copy {\tt rintmwe1} into {\tt /lab6/work/l6p1.m} and adapt it to the problem of computing the integral
  \bes
  \int_0^{\pi} \exp(x)\sin(x)dx
  \ees
  numerically using the trapezoidal rule.

    \begin{enumerate}
      \item What evidence do you find to support support the conjecture that there exist
        an asymptotic error expansion of the form
        \bes
        T- A_h = \alpha h^p + \beta h^q + O(h^r), \quad 0 < p < q < r.
        \ees
      \item Based on the numerical evidence, what is a reasonable value of $p$?
      \item Based on the numerical evidence, what is a reasonable value of $q$?
      \item What is the smallest value of $k$ for which the integral can be computed with a relative error less than $\tau = 10^{-6}$?
        \begin{center} {\bf You must explain why your error estimate is reliable!}
        \end{center}
      \item Compute the exact value of the integral and include this information in {\tt l6p1}.
      \item Is the behavior of Richardson's fraction related to the quality of Richardson's error estimate?
   \end{enumerate}
  \end{problem}

  \begin{problem} Copy {\tt rintmwe1.m} into {\tt /work/l6p2.m} and adapt it to the problem of computing the integral
    \bes
    T = \int_{-1}^1 \sqrt{1-x^2} dx
    \ees
    using the trapezoidal rule as your approximation $A_h$.
    \begin{enumerate}
      \item What evidence do you find to support support the conjecture that there exist
        an asymptotic error expansion of the form
        \bes
        T- A_h = \alpha h^p + \beta h^q + O(h^r), \quad 0 < p < q < r.
        \ees
      \item Based on the numerical evidence, what is a reasonable value of $p$?
      \item Based on the numerical evidence, what is a reasonable value of $q$?
      \item What is the smallest value of $k$ for which the integral can be computed with a relative error less than $\tau = 10^{-6}$?
        \begin{center} {\bf You must explain why your error estimate is reliable}
        \end{center}
      \item Compute the exact value of the integral and include this information in {\tt l6p2}. {\bf Hint:} It is quite easy to compute the integral if you make a drawing of the graph first.
      \item Is the behavior of Richardson's fraction related to the quality of Richardson's error estimate?
      \end{enumerate}
 \end{problem}

  \begin{problem} Copy {\tt rintmwe1.m} into {\tt /work/l6p3.m} and adapt it to the problem of computing the integral
    \bes
    T = \frac{1}{\sqrt{\pi}}\int_0^3 \exp(-x^2) dx
    \ees
    using the trapezoidal rule as your approximation $A_h$.
    \begin{enumerate}
      \item What evidence do you find to support support the conjecture that there exist
        an asymptotic error expansion of the form
        \bes
        T- A_h = \alpha h^p + \beta h^q + O(h^r), \quad 0 < p < q < r.
        \ees
      \item Based on the numerical evidence, what is a reasonable value of $p$?
      \item Based on the numerical evidence, what is a reasonable value of $q$?
      \item Why is Richardson's fraction not close to $2^p$ for small values of $k$?
        \item Why is Richardson's fraction not close to $2^p$ for very large values of $k$?.
      \item What is the smallest value of $k$ for which the integral can be computed with a relative error less than $\tau = 10^{-6}$?
        \begin{center} {\bf You must explain why your error estimate is reliable!}
        \end{center}
      \end{enumerate}
 \end{problem}

\begin{problem} Copy {\tt rdifmwe1} into {\tt /work/l6p4.m} and adapt it to the problem of computing the the target $T = f'(x)$,  where $f$ is you favorite differentiable function and $x$ is your favorite real number using the mysterious rule
    \bes
    M_h = A_h + \frac{A_h - A_{2h}}{2^p-1}
    \ees
    where $A_h$ is your favorite rule for computing $f'(x)$ which obeys an asymptotic error expansion of the form
    \bes
     T- A_h = \alpha h^p + \beta h^q + O(h^r), \quad 0 < p < q < r.
     \ees
     \begin{enumerate}
     \item What evidence can you uncover that suggests that $M_h$ obeys an asymptotic error expansion of the form
       \be
       T - M_h = \bar{\alpha} h^q + \bar{\beta} h^r + O(h^s), \quad 0 < q < r < s.
       \ee
     \item Based on your numerical evidence, what is a reasonable value of $q$?
     \item Based on your numerical evidence, what is a reasonable value of $r$?
     \item Include the exact value of the derivative of $f$ in the script.
       \item Examine the relationship between Richardson's fraction and the quality of the error estimate.
     \end{enumerate}
 \end{problem}

 \section{Concluding remarks}
 \begin{enumerate}
 \item You will find that quality of the error estimate improves even after the computed value of Richardson's fractions have start to deviate from the expected pattern. This happens from time to time, but it is not something you can count on.
 \item If you return an approximation without an error estimate or an error bound, then you work is incomplete.
   \item If you return an error bound or an error estimate without explaining why it is reliable, then your work is incomplete.
 \end{enumerate}

\bibliographystyle{plain}
  \bibliography{../../../lecture-notes/refs}

\end{document}


\documentclass[12pt]{article}
\usepackage[a4paper, bindingoffset=0.2in, %
							left=0.5in,right=0.5in,top=0.5in,bottom=0.5in,%
							footskip=.25in]{geometry}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{hyperref}

\title{PSet5 Report}
\author{Ali Abolhassanzadeh Mahani}

\begin{document}
	\maketitle
	\section{Generating Uniform Random Numbers}
	As we expected the amount of numbers generated for each digit is about $\frac{N}{10}$. Fig\ref{fig:p1_hist}
	\begin{figure}[h!]
		\includegraphics[width=0.9\linewidth]{../p1_hist.jpg}
		\label{fig:p1_hist}
		\caption{Hist plot of N numbers generated uniformly from 0 to 9}
	\end{figure}
	I plotted the equation and as expected we see that the equation holds for this generator.

	This is very similar to the Random walk --random generation-- in which both express the cenetral limit property. Fig\ref{fig:p1_plot}
	\begin{figure}[h!]
	\includegraphics[width=0.9\linewidth]{../p1_plot.jpg}
	\label{fig:p1_plot}
	\caption{plot to show the expression of central limit phenomena}
\end{figure}

	\section{Entanglement}
	Did what the question asked me to do and got the uniform distribution histogram again, meaning that
	there are no entanglements. Fig\ref{fig:p2}
	\begin{figure}[h!]
		\includegraphics[width=.9\linewidth]{../p2.jpg}
		\label{fig:p2}
		\caption{The uniform plot after plotting the histogram for numbers generated after every 4}
	\end{figure}

	\section{Central Limit Theorem}
	I generated rands with the length of N and summed them to get to the central limit theorem.
	The hist plots are available in Fig\ref{fig:p3}
	\begin{figure}[h!]
		\centering
		\includegraphics[width=.4\linewidth]{../p3_5.jpg}
		\includegraphics[width=.4\linewidth]{../p3_10.jpg}
		\includegraphics[width=.4\linewidth]{../p3_100.jpg}
		\includegraphics[width=.4\linewidth]{../p3_1000.jpg}
		\label{fig:p3}
		\caption{Central limit for lengths of 5, 10, 100, 1000}
	\end{figure}
	In all these exercises we witness that the higher the size N, the nearer the distribution is to a gaussian function.

	\section{Transforming the Distribution Function}
	I used the method seen in the code to generate the gaussian distribution in 2 dimentions.
	Here are the results for different numbers. Fig\ref{fig:p4}
	\begin{figure}[h!]
		\centering
		\includegraphics[width=.45\linewidth]{../p4_10}
		\includegraphics[width=.45\linewidth]{../p4_100}
		\includegraphics[width=.45\linewidth]{../p4_500}
		\includegraphics[width=.45\linewidth]{../p4_1000}
		\label{fig:p4}
		\caption{2 dimentional normal distribution for sizes 10, 100, 500, 1000}
	\end{figure}

\end{document}