\chapterquote{%
It is the simple hypotheses of which one must be most wary; because these are the ones that have the most chances of passing unnoticed.}%
{-- Henri Poincaré, \textit{Thermodynamique} %: Leçons professées pendant le premier semestre
(1892)}

In the previous chapter, we used the high-mobility model, which assumes the node positions change drastically for each time slot.
%
A refinement of the model would be to fix some elements such that we keep analytical tractability and at the same time consider the effects of a static network.

In this chapter\footnote{The present chapter was based on the article \cite{dester2021unique}.}, we add the static element as the distance of the links, i.e., at the beginning of time, each link distance is randomized according to an arbitrary distribution and remains constant throughout time.

When including static elements, a challenge arises.
%
Each node's performance depends on its initial position, thus there might exist a set of nodes with ``bad'' positions which cause their respective queues to be unstable.
%
Thus, we cannot guarantee stability anymore, because in some systems there will be \textit{almost surely} a set of unstable nodes.
%
In these systems, it is common to use the concept of $\varepsilon$-stability \cite{zhong2016stability}. A queueing system is $\varepsilon$-stable if the system is stable except for a proportion $\varepsilon\in(0,1)$ of the queues.

The unstable queues are responsible for the Markov chain having a positive probability of never returning to a given state. This implies that we no longer have a positive recurrent Markov chain, thus we cannot guarantee ergodicity nor unicity of a stationary distribution (Theorem~\ref{th:Markov_ergodic}).
%
We explore this problem of non-unicity of the stationary distribution in $\varepsilon$-stable networks.
%
As far as we know, this is the first time this problem is tackled.

% % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % %
\section{On the Uniqueness of Stationary Distributions}

Considering a general path loss model and general distribution of link distances, we provide simple sufficient conditions for the uniqueness of a stationary limiting distribution.

The developed mathematical formulation guided us in finding a counterexample to a general uniqueness assumption.
%
We verified through extensive simulations that the proposed scenario indeed possesses two very different limiting stationary distributions, which remain valid even when considering a static Poisson network without the main simplifying assumptions of the original system model.

\subsection{System Model}
\label{sec:sysmod}

In view of the model presented in Chapter~\ref{cap:P2_00}, we consider a stationary static bipolar Poisson network of spatial density ${\lambda>0}$ on $\R^2$ with a single class of users. % and under slotted ALOHA protocol.
%
Time is slotted, i.e., $\T=\N$.

The distances $\{R_i(t)\}_{i\in\N}$ are defined at the first time slot independently and according to some proper distribution with an arbitrary cdf ${F_R:\R_+\longrightarrow[0,1]}$.

% Each transmitter is located at $X_i \in \Phi$, $i\in\N$, and has a dedicated receiver $Y_i$ at a fixed distance $R_i=||Y_i-X_i||$ and a uniformly distributed random direction.

% Thus, the receiver locations ${\{Y_i\}_{i\in\N} = \hat\Phi \subset \R^2}$ also form a homogeneous Poisson point process with a spatial density $\lambda$ according to the Displacement Theorem \cite[Th.~1.3.9.]{baccelli2010stochastic}.
%
%Time is slotted and denoted by $t\in\N$.

We assume unit transmit power for all users ($P_i\equiv 1$, $i\in\N$), signals subject to small-scale Rayleigh fading independent and identically distributed (iid) across space and time, and a general omnidirectional monotone decreasing path loss model ${\ell:\R_+\longrightarrow\R_+}$ that satisfies $\int_{\R_+} x\,\ell(x)\,\d x < \infty$ to guarantee that the interference is not infinite \textit{almost surely}.
%a.s.

For each transmitter $i\in\N$, packets arrive independently at its queue according to a Bernoulli point process $A_i$ on $\T$ of parameter $a\in[0,1]$, which means that $A_i(t)\sim\mathscr{B}(a)$ are iid across $t\in\T$.

The queue service discipline is general, i.e., it does not matter in which order the queued packets are transmitted because this study deals with the distribution of queued packets and not queueing delay.

If the transmitter has a nonempty queue, then it tries to transmit with an access probability $p\in[0,1]$. Thus, $T_i$ is a Bernoulli point process on $\T$ of parameter $p$.
%
We assume $a < p$, otherwise the limiting distribution is trivial with all queues being unstable \textit{almost surely}.

Packet transmissions occur in time slots with equal duration, and we assume that the Rayleigh fading coefficient is constant during this time.
%
If a packet is successfully received, then the corresponding receiver instantly sends an acknowledgment through an error-free channel and the packet is removed from the queue.

% The Signal-to-Interference Ratio\footnote{
% We assume an interference-limited network because, for the most part of large-scale networks, the effect of noise is negligible with respect to that of aggregate interference \cite{haenggi2021stochastic}.
% } at the $i$th receiver, with $i\in\N$ and associated with receiving a packet at time $t$, is computed as
% \begin{equation}
%     \SIR_i(t) = \frac{H_{ii}\,\ell(R_i)}{ \sum_{j\neq i} e_j \ind_{\{Q_j>0\}} H_{ji}\,\ell(||X_j-Y_i||) },
% \end{equation}
% where $H_{ij}$ is exponentially distributed with a parameter $1$ and represents the Rayleigh fading coefficient from transmitter $j$ to receiver $i$, $e_j\in\{0,1\}$ is iid Bernoulli distributed with a parameter $p$ and represents if user $j$ gained access to the channel, and $\ind_{\{Q_j>0\}}\in\{0,1\}$ is equal to zero if the queue of user $j$ is empty and one otherwise.

Slivnyak’s theorem (Theorem~\ref{th:slivnyak}) allows us to concentrate on the typical user, which we denote by the index $i=0$, without changing the distribution of the PPP.

Spatio-temporal correlation leads to the coupling of queues, which results in a highly complicated analysis. Thus, we assume that the interference on the typical receiver is iid across time and use the mean-field approximation \cite{kurtz1981approximation} to maintain the analytical tractability.
%
This method considers a typical queue and substitutes the interaction with other queues for an effective mean interaction, reducing the problem to the analysis of a single entity and its interdependence with the distribution of the population of entities in each state.
%
{For more details about the method applied to wireless networks, please refer to} \cite[Page~28]{haenggi2021stochastic}.

Let us assume the \textit{capture model}, where the packet from transmitter $i\in\N$ is successfully received at time slot $t\in\T$ if $\SIR_i(t)$ is greater than the threshold $\theta\in\R_+$.

Then, in a stationary network, the mean queue load of a typical user is given by
\begin{align}
    \bar\rho &= \lim_{t\to\infty} \P(Q_0(t)>0),
\end{align}
and the mean packet success probability and mean queue load of a typical transmitter with the link distance equal to $R_0$ as
\begin{align}
    p_s(R_0) &= \lim_{t\to\infty} \P(\SIR_0(t) > \theta \mid R_0),\\
    \rho(R_0) &= \lim_{t\to\infty} \P(Q_0(t) > 0 \mid R_0),
\end{align}
where $Q_0(t)\in\N$ corresponds to the number of packets in the typical queue at time $t\in\T$.

\begin{note}
    We must be careful with the definition of the network mean transmission success probability $p_s$ from Chapter~\ref{cap:P2_00}. The following calculation is not correct, in general,
    \begin{align*}
        p_s \stackrel{\times}{=} \int_{\R_+} p_s(r)\,\d F_R(r),
    \end{align*}
    because nodes with low transmission success probability transmit more times, and here we are considering only the population of nodes with each transmission distance. Instead, the correct calculation is
    \begin{align*}
        p_s = \int_{\R_+} p_s(r) \frac{\rho(r)}{\bar\rho} \,\d F_R(r),
    \end{align*}
    which weigh on the probability of the queues being non-empty.
\end{note}

% Table~\ref{tab:symbols} summarizes some definitions used in the model.
% \begin{table}[t]
%     \centering
%     \caption{Notations and symbols.}
%     \label{tab:symbols}
%     \setlength{\tabcolsep}{3pt}
%     \begin{tabular}{l l}
%       ine
%       ine
%       \textbf{Symbol} & \textbf{Definition/explanation} \\
%       ine
%         $\Z, \Z_+$				& set of integers and non-negative integers \\
%         $\R, \R_+$				& set of real numbers and non-negative reals \\
%         $\E[\cdot]$             & expected value operator \\
%         $\ind_{\{\cdot\}}$      & indicator function\\
%         $Q_i\in\Z_+$            & queue length of the $i$th user \\
%         $\ell\in\cal{C}(\R_+)$  & path-loss function \\
%         $\theta\in\R_+$         & SIR threshold for successful transmission \\
%         ine
%       ine
%     \end{tabular}
% \end{table}

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

\subsection{Stationary Analysis}
\label{sec:stat}

At first, let us suppose that the limiting distribution exists and the mean queue load converges to $\bar\rho \in [0,1]$.
%
Then, at stationary conditions, the active transmitters form a thinned point process $\Phi^{p\bar\rho}_\TX$ from $\Phi_\TX$. Thus, it consists of a PPP of density $\lambda\,p\,\bar\rho$.
%
From Proposition~\ref{prop:PPP_ps}, the packet success probability of a typical link with a transmission distance $r > 0$ is given by
\begin{align} \label{eq:p_s}
    p_s(r) = \exp\left(- g_\ell(r,\theta)\,\lambda\,p\,\bar\rho \right),
\end{align}
where \vspace{-2mm}
$$\displaystyle g_\ell(r,\theta) \triangleq \int_{\R_+}\frac{2\pi \, x\, \theta \ell(x)}{\ell(r)+\theta \ell(x)}\, \d x,$$
which is a strictly monotone increasing function on the first argument and tends to infinity in the positive infinity, because $\ell$ is a strictly monotone decreasing function and tends to zero in the positive infinity.
%
For ease of notation, let us omit the second argument of $g_\ell$, i.e., $g_\ell(r,\theta) = g_\ell(r)$, since $\theta$ is a constant.

Then, from Queueing theory, the stationary queue load of a typical user with the link distance $r > 0$ is
\begin{align}\label{eq:rho_r}
    \rho(r) = \min\left\{ \frac{a}{p\,p_s(r)}, 1 \right\},
\end{align}
because the service rate is given by $p\,p_s(r)$.
%
% If $\rho(r) = 1$ for some $r>0$, it means there is not stability for any typical queue with link distance greater than $r$.
However, to calculate $p_s(r)$ in \eqref{eq:p_s} we need the density of active users $\lambda_a$, which depends on the mean queue load $\bar\rho$. This, in turn, depends on $\rho(r)$. The following proposition provides a solution to this problem.

\begin{proposition}\label{prop:fixed_point}
    If the stationary mean queue load $\bar\rho\in[0,1]$ exists, it must satisfy the fixed point equation $\varphi(\bar\rho) = \bar\rho$, where
    \begin{align}\label{eq:fixed_point}
        \varphi(x) \triangleq 1 - \frac{a}{p} \int_0^{\ln(p/a)} \hspace{-7mm} \euler^u\,F_*(u/px)\,\d u, \quad x\in(0,1],
    \end{align}
    $\varphi(0)=a/p$, and $F_*$ is the CDF of the random variable $\lambda g_\ell(R)$.
\end{proposition}
%
\begin{proof}
    Using \eqref{eq:p_s}, \eqref{eq:rho_r}, integration by parts, and the substitution ${u = p\bar\rho w}$, we have
    \begin{align*}
        \bar\rho &= \E\left[\rho(R_0)\right]
            = \E\!\left[\min\left\{ \frac{a}{p}\euler^{\lambda g_\ell(R_0) p \bar\rho}, 1 \right\} \right] \\
            &= 1 - F_*\!\left(\frac{\ln(p/a)}{p\bar\rho}\right) + \frac{a}{p} \int_0^{\ln(p/a)/p\bar\rho} \hspace{-12mm} \euler^{p\bar\rho w}\,\d F_*(w) \\
            &= 1 - \frac{a}{p} \int_0^{\ln(p/a)} \hspace{-7mm} \euler^u\,F_*(u/p\bar\rho)\,\d u = \varphi(\bar\rho),
    \end{align*}
    where $\d F(w)$ is the Lebesgue--Stieltjes notation, and if the probability density function exists we can write it as $F'(w) \d w$.

    Further, $\varphi(x) \to a/p$ as $x\to 0^+$, because $F_*(y) \to 1$ as $y \to \infty$, which comes from $R$ being a proper random variable.
\end{proof}

Because $g_\ell$ is strictly monotone, it is invertible, and then
\begin{align}\label{eq:F_*}
    F_*(x) =
    \begin{cases}
        F_{R}\!\left(g_\ell^{-1}\!\left(\frac{x}{\lambda}\right)\right), & \text{if } x > \lambda \displaystyle\lim_{r\to 0^+} g_\ell(r),\\
        0, &\text{otherwise}.
    \end{cases}
\end{align}

\begin{remark}
    We are dealing with a general path loss function $\ell$ and a general distribution $F_R$ of link distances, so care must be taken with $F_*$, in the sense that this function may not be sufficiently ``smooth''.
\end{remark}

Now we present some theorems that provide results on the existence and uniqueness of a valid stationary mean queue load $\bar\rho$.
%
However, before proving the theorems we need a result on the continuity of the function $\varphi$, which is defined in Proposition~\ref{prop:fixed_point} and whose fixed point provides the mean queue load of the queueing network.

\begin{lemma} \label{lem:lipschitz}
    The function $\varphi$ is $c$-Lipschitz continuous\footnote{A function $\varphi$ is $c$-Lipschitz continuous if $|\varphi(x)-\varphi(y)|\le c\,|x-y|$ for all $x,y$ in the interval.} on $[a/p,1]$ with $c = \frac{p}{a}\ln(\frac{p}{a})$.
\end{lemma}
%
\begin{proof}
    Let us prove that for all $x,y\in[\frac{a}{p},1]$, $|\varphi(x)-\varphi(y)| \le c\,|x-y|$.

    Without loss of generality let $x < y$, then
    \begin{align*}
        |\varphi(x)-\varphi(y)| &= \frac{a}{p} \left( \int_0^{\ln(p/a)} \hspace{-10mm} \euler^u F_*(u/px) \d u - \int_0^{\ln(p/a)}\hspace{-10mm} \euler^u F_*(u/py) \d u \right)\\
            &\stackrel{(i)}{=} a \left( \int_{\ln(p/a)/py}^{\ln(p/a)/px}\hspace{-14mm} x\euler^{p x w} F_*(w) \d w -  \int_0^{\ln(p/a)/py}\hspace{-14mm} (y\euler^{p y w} - x\euler^{p x w})F_*(w) \d w \right) \\
            &\le a \int_{\ln(p/a)/py}^{\ln(p/a)/px}\hspace{-12mm} x\euler^{p x w} \d w = 1 - \euler^{-\ln(p/a)\frac{y - x}{y}} \le \frac{\ln(p/a)}{y} (y - x) \\
            &\le \frac{\ln(p/a)}{a/p} |x-y|.
    \end{align*}
    In $(i)$ we perform the change of variables $u = (px)w$ for the left integral and $u = (py)w$ for the right one, and split the intervals of integration of the left integral.
\end{proof}

\begin{theorem} \label{th:existence}
    The function $\varphi$ has a fixed point on $[a/p,1]$.
\end{theorem}
\begin{proof}
    From Lemma~\ref{lem:lipschitz}, $\varphi$ is continuous because Lipschitz continuity implies continuity.
    %
    Further, $\varphi(0) = a/p < 1$ and $a/p \le \varphi(1) \le 1$.
    %
    Then, the result follows from the intermediate value theorem (Theorem~\ref{th:IVT}) applied on $\varphi(x)-x$, $x\in[a/p,1]$.
\end{proof}

\begin{theorem} \label{th:unique_sol1}
    If $a > p\,\Omega$, where $\Omega$ is the solution of $\Omega\,\euler^\Omega=1$, then the function $\varphi$ admits a unique fixed point.
\end{theorem}
%
\begin{proof}
    Using the Banach fixed point theorem (Theorem~\ref{th:banach}) along with Lemma~\ref{lem:lipschitz}, we have that $\varphi$ admits a unique fixed point if $c = \frac{p}{a}\ln(\frac{p}{a}) \in [0,1)$.

    This happens if $\frac{a}{p}\exp(\frac{a}{p}) > 1$ which is equivalent to $\frac{a}{p} > \Omega$.
\end{proof}

\begin{theorem} \label{th:unique_sol2}
    If $F_*$ is continuously differentiable and $a > p/\euler$, then the function $\varphi$ admits a unique fixed point.
\end{theorem}
%
\begin{proof}
    Let $\bar\rho$ be a solution to $\varphi(\bar\rho) = \bar\rho$. Then,
    \begin{align*}
        \varphi'(\bar\rho) &= \frac{a}{(p\bar\rho)^2} \int_0^{\ln(p/a)} \hspace{-7mm} u\,\euler^u\,F_*'(u/p \bar\rho)\,\d u \\
            &= \frac{1}{\bar\rho}\bigg( \ln(p/a) F_*(\ln(p/a)/p\bar\rho) - \frac{a}{p}\int_0^{\ln(p/a)}\hspace{-7mm} (1+u) \euler^u F_*(u/p\bar\rho)\,\d u \bigg)\\
            &\le \frac{\ln(p/a) - (1-\varphi(\bar\rho))}{\bar\rho} < \frac{\varphi(\bar\rho)}{\bar\rho} = 1,
    \end{align*}
    where we used the Leibniz integral rule (Theorem~\ref{th:leibniz}) to interchange the integral with the derivative to establish the first equality, then used integration by parts in the second equality.
    %
    As $\varphi$ is a continuous function and $(\varphi(x)-x)'|_{x=\bar\rho} < 0$, the fixed point equation cannot have more than one root, because $\varphi(x)-x$ always crosses the $x$-axis downwards.
\end{proof}

Since $\Omega > 1/\euler$, Theorems \ref{th:unique_sol1} and \ref{th:unique_sol2} complement each other. While one has a larger set of valid functions, the other has a larger set of arrival rates that guarantee uniqueness.

{Remarkably, we do not need any information about the distribution of the link distances, the path loss model, the threshold for successful communication, or the density of users to guarantee uniqueness through Theorems} \ref{th:unique_sol1} and \ref{th:unique_sol2} (except for $F_*$ being differentiable in Theorem~3).

Let $\varepsilon$ represent the proportion of unstable queues\footnote{In the literature \cite{zhong2016stability}, it is common to define the $\varepsilon$-stability region. However, in the present work, it is more convenient to simply define $\varepsilon$.} in the network, then in view of the mean-field approximation and Loynes theorem (Theorem~\ref{th:loynes}) we can define
\begin{align}
    \varepsilon \triangleq \lim_{t\to\infty} \P\big( p\,\P(\SIR_0(t) < \theta \mid R_0) < a \big).
\end{align}

Then, using \eqref{eq:p_s}, \eqref{eq:F_*}, and Proposition~\ref{prop:fixed_point}, we can show that
\begin{align} \label{eq:eps}
    \varepsilon &= 1 - F_* \!\left(\frac{\ln(p/a)}{p\,\bar\rho}\right)
        \le \bar\rho.
\end{align}
This inequality is expected because $\varepsilon$ takes into account only queues with load equal to $1$.

\begin{proposition}\label{prop:p_opt}
    If $F_*$ is continuously differentiable and $a > p/\euler$, then \[\dfrac{\partial\bar\rho}{\partial p} < 0 \quad\text{and}\quad \dfrac{\partial\varepsilon}{\partial p} < 0.\]
\end{proposition}
%
\begin{proof}
    Taking the derivative with respect to $p$ on both sides of the fixed point equation $\varphi(\bar\rho)=\bar\rho$ and after some tedious manipulations we have
    \begin{align} \label{eq:drho/dp}
        \frac{p}{\bar\rho} \frac{\partial\bar\rho}{\partial p}
            &= - \frac{(1-\ln(p/a))F_*(\Upsilon)+A}{1-\ln(p/a)F_*(\Upsilon) + A},
    \end{align}%
    where $\displaystyle A \triangleq \frac{a}{p}\int_0^{\ln(p/a)} \hspace{-7mm} u\, \euler^u F_*(u/p\bar\rho) \,\d u \ge 0$ and $\displaystyle \Upsilon \triangleq \frac{\ln(p/a)}{p\bar\rho}$.

    Since $a>p/\euler$, then $\ln(p/a) < 1$. Using this, it is easy to see that both the numerator and denominator of \eqref{eq:drho/dp} are positive. Thus, $\frac{\partial\bar\rho}{\partial p} < 0$.

    Note that $\varepsilon$ monotonically decreases with $\Upsilon$ from \eqref{eq:eps}. Then, let us calculate the derivative of $\Upsilon$ with \eqref{eq:drho/dp}. After some tedious manipulations,
    \begin{align*}
        \frac{p}{\Upsilon}\frac{\partial\Upsilon}{\partial p}
            &= \frac{1}{\ln(p/a)}\,\frac{1 - \ln(p/a)+A}{1 - \ln(p/a) F_*(\Upsilon) + A}.
    \end{align*}%
    Again, it is easy to see that both the numerator and the denominator are positive. Thus, $\frac{\partial\Upsilon}{\partial p} > 0$ and $\frac{\partial\varepsilon}{\partial p} < 0$.
\end{proof}

From the above proposition, if $a \ge 1/\euler$, then the access probability $p$ that minimizes the proportion of unstable nodes and the load on the queues is $p=1$. Other works \cite{haenggi2018soc, dester2018} also reached an equivalent result in a different framework.
%
This appears to be a powerful theoretical result because it is valid for almost any set of system parameters, general path loss model, and general distribution of link distances.
%
However, we must remember that one of the model assumptions is PPP independence of the location of active transmitters across time slots, and this assumption becomes less valid as we increase the value of $p$ \cite{haenggi2013diversity}.
%
Thus, a more sensible conclusion is that, instead of setting $p=1$, we should increase $p$ as long as the system model assumptions remain valid.
%
Section~\ref{sec:counter-example} presents more material on this topic.

Now, let us turn our attention to the scenarios that have multiple limiting distributions, i.e. when $\varphi$ admits more than one fixed point.
%
We can build a sequence that converges to the least or the greatest solution of the fixed point equation, depending on the initial conditions. Let the recurrence equation that defines the sequence $\{\bar\rho_n\}_{n\in\N}$ be
$
    {\bar\rho_{n+1} = \varphi(\bar\rho_n), n\in\N}.
$
\begin{proposition} \label{prop:sequence}
    When $\bar\rho_0 = 0$, the sequence $\{\bar\rho_n\}_{n\in\N}$ converges to the least solution of $\varphi(\bar\rho)=\bar\rho$. Otherwise, if ${\bar\rho_0 = 1}$, it converges to the greatest solution.
\end{proposition}
%
\begin{proof}
    The function $\varphi$ is monotonic increasing because $F_*$ is a monotonic increasing function.
    %
    Also, $0 \le \varphi(0)$, then
    \begin{align*}
        \varphi(0) \le \varphi(\varphi(0)) \le \dots \le \varphi^n(0) \triangleq \varphi^{n-1}(\varphi(0)).
    \end{align*}

    Hence, when $\bar\rho_0 = 0$, the sequence $\{\bar\rho_n\}_n$ is increasing.
    %
    Clearly, it is also limited, and thus, it converges \cite[Th.~3.14.]{rudin1976principles}.

    Let $x = \varphi(x)$ be a fixed point. We have that $0 \le x$, then $\varphi(0) \le \varphi(x) = x$. Repeating $n$ times we get $\varphi^n(0) \le x$.
    %
    As $n\to\infty$ we know $\varphi^n(0) = \bar\rho_n$ converges, thus it converges to a value smaller or equal to $x$. But $x$ is an arbitrary fixed point. This concludes the proof for $\bar\rho_0=0$.
    %
    The proof when $\bar\rho_0=1$ is analogous.
\end{proof}

Note that the fixed point is unique if and only if the greatest solution is equal to the least solution.
%
This is a simple form to verify whether the stationary distribution is unique when neither of the uniqueness theorems (\ref{th:unique_sol1} and \ref{th:unique_sol2}) are satisfied.

Using the concept of dominant networks \cite{ephremides1988stability}, we can build a physical interpretation of Proposition~\ref{prop:sequence}.
%
Suppose a dominant network where all nodes transmit \textit{dummy} packets. The density of active transmitters is $\lambda p$, and thus, we can calculate the typical queue distributions to find the mean queue load, which we shall denote by $\bar\rho_1$. This value can be used as an upper bound to the density of active transmitters in the original network. Then, we can define a new dominant network, whose density of active transmitters is given by $\lambda p \bar\rho_1$ and find the corresponding $\bar\rho_2$, and so on. The process described above is equivalent to calculating $\bar\rho_{n+1} = \varphi(\bar\rho_n)$ with $\bar\rho_0 = 1$.
%
We can also perform an analogous reasoning for the lower bound, i.e., we assume that the density of active users is $\lambda a$, which corresponds to a system operating with a packet success probability equal to $1$, and calculate the typical mean queue load in this network, which again we shall denote by $\bar\rho_1$. This can be used to estimate another lower bound on the density of active users in the original network, and the process continues analogously to the upper bound case with the sole difference that $\bar\rho_0 = a/p$.

% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
%\section{A counter-example}
\section{Example of a non-unique stationary distribution}
\label{sec:counter-example}

Here we present a scenario where the conditions of the uniqueness theorems are not satisfied and there is more than one solution to the fixed point equation $\varphi(\bar\rho) = \bar\rho$.

As commonly chosen in the literature \cite{haenggi2021stochastic}, let the path loss function be $\ell(r) = r^{-\alpha}$, where $\alpha>2$, the distribution of $R$ be Rayleigh with mean $\mu_R$, and let us define the auxiliary parameters $\kappa \triangleq \frac{\sin(\delta\pi)}{4 \lambda \theta^\delta \delta \pi \mu_R^2}$, $\delta\triangleq 2/\alpha$. Then, ${F_*(x) = 1-\euler^{-\kappa x}}$ and for $x>0$,
\begin{align}
    \varphi(x) &=
    \begin{cases}
        \frac{\left(\frac{a}{p}\right)\frac{\kappa}{px}-\left(\frac{a}{p}\right)^{\frac{\kappa}{px}}}{\frac{\kappa}{px}-1},\quad \text{if } x \neq \kappa/p,\\
        \frac{a}{p} \left(1 + \ln\!\left(\frac{p}{a}\right)\right), \quad\text{otherwise.}
    \end{cases}
\end{align}

For the simulations we set $a = 0.002$ packets per time slot per node, $p = 0.1$, $\lambda = 1$ node per unit of area, ${\alpha = 5}$, $\theta = 2$, $\mu_R = 3.457$ units of length, which gives a $\kappa \approx 0.012$.
%
{However, any combination of parameters that results in $\kappa < 0.248$ would work, because in this case, we can find $a$ and $p$ that give non-unicity of the fixed point, as long as $\alpha>2$.
%
Nevertheless, when $\alpha$ approaches $2$ the simulations become expensive because we need to increase the number of nodes to properly simulate the large-scale network effect.}
%
The simulations are of static Poisson networks and do not consider the simplifying assumptions of the system model, such as mean-field approximation or independence of the $\SIR$ across time and space.

\begin{figure}[ht]
    \centering
    \if\printfig1
        % \input{Plots/Ch8_fixed_point.tex}
        \includegraphics[]{Figures/Ch8_fixed_point.pdf}
    \else
        \includegraphics[draft,width=\textwidth]{Figures/placeholder.png}
    \fi
    % \includegraphics[width=\columnwidth]{Figures/main-figure0.eps}
    \caption{Red dots are the \textit{attractors} and the blue point is the \textit{repeller}.}
    \label{fig:fixed_point}
\end{figure}

We show in Fig.~\ref{fig:fixed_point} the \textit{attractors} and \textit{repellers}
% \footnote{
% We define \textit{attractor} as a fixed point $x_a$ of $\varphi$ that satisfies $\varphi^n(x)\to x_a$ as $n\to\infty$ for all $x$ in some neighborhood $V_a$ of $x_a$, where $\varphi^n$ denotes repeated composition of the function with itself.
% %
% This neighborhood $V_a$ is called \textit{basin of attraction}.
% %
% A fixed point $x_r$ of $\varphi$ is a \textit{repeller} if there exist $n_0\in\N$ and a neighborhood $V_r$ of $x_r$ such that $\varphi^n(x)\notin V_r$ for all $n > n_0, x\in V_r\backslash\{x_r\}$.
% }
of the chosen scenario, which can be intuitively seen as ``stable'' and ``unstable'' equilibrium points, respectively.
%
The formal definition follows.

\begin{definition}
    A fixed point $x_0$ of a function $\varphi$ is an \textit{attractor} if for all $x$ in some \textit{neighbourhood}\footnote{\textit{Neighborhood} is defined in Definition~\ref{def:neighborhood}.} of $x_0$ we have that $\varphi^n(x) \to x_0$ as $n\to\infty$, where $\varphi^n(x)\triangleq \varphi^{n-1}(\varphi(x))$.
    %
    A \textit{neighbourhood} that satisfies the aforementioned property is a \textit{basin of attraction} for $x_0$.

    On the other hand, $x_0$ is a \textit{repeller} if there exists a \textit{neighbourhood} $V$ of $x_0$ and an integer $n_0$ such that for all $x\in V\backslash\{x_0\}$ we have that $\varphi^n(x) \notin V$ for all $n > n_0$.
\end{definition}

\begin{figure}[H]
    \centering
    \if\printfig1
        % \input{Plots/Ch8_sim_fixed_point.tex}%
        \includegraphics[]{Figures/Ch8_sim_fixed_point.pdf}
    \else
        \includegraphics[draft]{Figures/Ch8_sim_fixed_point.pdf}
    \fi
    % \includegraphics[width=0.8\columnwidth]{Figures/Ch8_sim_fixed_point.eps}
    % \includegraphics[]{Figures/main-figure1.eps}
    \caption{System simulation with four different initial conditions for the queues.
    %
    We show the average proportion of nonempty queues as an estimation of the value of $\bar\rho$.
    %
    The horizontal red lines are the \textit{attractors} and the blue line is the \textit{repeller}.}
    \label{fig:sim_fixed_point}
\end{figure}

Fig.~\ref{fig:sim_fixed_point} shows some simulations, which converge for both \textit{attractors} depending on the initial conditions of the queues. For example, we notice that when queues start heavily loaded, the probability to converge to the upper fixed point increases.
%
The brown curve illustrates that even when we start with initial conditions above the \textit{repeller}, the system may still surpass it.
%
This is due to the fact that the initial conditions do not depend only on the initial mean queue load of the system but also on the number of packets on each one of the queues.
%
In addition, there is ``noise'' in the curves, because we are using a finite number of queues in the simulation, and it tends to vanish as the number of queues tends to infinity.

It is clear that the difference in performance from the upper fixed point to the lower fixed point is immense, e.g., the proportion of unstable queues in one case is $\varepsilon \approx 0.407$, and in the other it is $\varepsilon \approx 9\cdot 10^{-9}$.
%
Thus, when multiple limiting stationary distributions exist, it is then of key importance to operate in the smallest fixed point.

\begin{figure}[htb]
    \centering
    \if\printfig1
        % \input{Plots/Ch8_rhoL_rhoH.tex}%
        \includegraphics[scale=0.9]{Figures/Ch8_rhoL_rhoH.pdf}
    \else
        \includegraphics[draft,scale=0.9]{Figures/Ch8_rhoL_rhoH.pdf}
    \fi
    % \includegraphics[width=\columnwidth]{Figures/main-figure2.eps}
    \caption{Solution of $\varphi(\bar\rho)=\bar\rho$ as a function of $p$. The marks are simulation results. The dashed curve is the \textit{repeller} and divides the \textit{basins of attraction}.}
    \label{fig:rhoL_rhoH}
\end{figure}

For that, a simple working strategy is to keep $p$ low, for example $p < \euler\,a$ (which satisfies the uniqueness Theorem~\ref{th:unique_sol2}), until reaching a stationary state, then slowly increase $p$ for all nodes. This would improve the performance (Proposition~\ref{prop:p_opt}) and decrease the probability of converging to another, presumably worse, stationary state.
%
In Fig.~\ref{fig:rhoL_rhoH} we show the theoretical \textit{attractors} and simulate the system using different initial conditions for the queues.
%
Even when employing the strategy of the slowly increasing $p$, we could not make the system converge to the lower fixed point for $p > 0.4$.
%
That was expected at some point, because when $p$ increases, the \textit{basin of attraction} of the lower fixed point becomes smaller and the stochastic nature of the process makes it exit this region and converge to the upper fixed point. %, which has a larger \textit{basin of attraction}.

\begin{note}
    For curiosity, let us present the stationary probability distribution of a queue selected at random from the network system. %
    The equation follows.
    \begin{align*}
        \lim_{t\to\infty}\P\big(Q_0(t) = n\big) &= y \frac{x^y + x^{n+1}(n-y) - x^n (n+1-y)}{(n-y)(n+1-y)}, \quad n\in\N,~ x \triangleq \frac{a}{p}, ~ y \triangleq \frac{\kappa}{p\bar\rho}.
    \end{align*}

    Now, the reader may wonder why $Q_0$ does not follow the Geo/Geo/1 distribution at stationary state.
    %
    Indeed, a given queue follows a Geo/Geo/1 at stationary, i.e., $Q_0 \mid R_0$ is a Geo/Geo/1.
    %
    However, before knowing $R_0$ or which queue is selected, we need to take into account the distribution of the queue loads in the network, which results in the cumbersome expression showed above.

    Also, note that this distribution is not unique when $\bar\rho$ is not unique.
\end{note}

% % % % % % % % % % % % % %
\section{Summary} \label{sec:summ_P2_04}

In this chapter, we provided simple sufficient conditions for the uniqueness of a stationary limiting distribution for a Poisson network with general path loss model and general distribution of link distances.
%
The developed mathematical formulation guided us in finding a counterexample to a general uniqueness assumption for the stationary distribution.
%
We verified through extensive simulations that the proposed scenario indeed possesses two very different limiting stationary distributions, which remain valid even when considering a static Poisson network without the main simplifying assumptions of the original system model.

Furthermore, from the uniqueness theorems, we are inclined to conclude (in a more general setting) that this phenomenon of non-unicity is more likely in light traffic scenarios.
%
So those are the cases one must be most wary.


%!TEX root =  ../main.tex

\mychapters{Transformations}{transformations}{\chapdir/pics/Nasir-al_molk_-1}

More complicated things can happen to functions, not the least of which is that they can be nested,
like Russian dolls.  It is also possible for the $x$ and $y$ of our graphs to be defined by some
other variable, call the parameter.  That skill-set will then unable us to see which functions can
be ``undone'', and which cannot be (or at least not perfectly).  It is also possible
to analyze the reciprocal of a function.   We will also consider what happens
when the input and/or output of a function is a distance, i.e. absolute value.

\inlinefig{\chapdir/pics/Inner_Circle}{Russian or Matryoshka dolls are famous for being \emph{nested}.}
% https://www.flickr.com/photos/jronaldlee/5566380424

\newpage
\chapterminitoc

%							4 - 1
\newpage
\invisiblesection{Absolute}
\marginlessinput{\chapdir/0401p}
\newpage
\input{\chapdir/0401}
\newpage
\input{\chapdir/0401x}

%							4 - 2
\newpage
\invisiblesection{Parametric}
\marginlessinput{\chapdir/0402p}
\newpage
\input{\chapdir/0402}
~\vfill
%\newpage
\subsection{Exercises}
\noindent\makebox[\textwidth]{\includegraphics[width=\paperwidth]{\chapdir/0402xA.pdf}}
\newpage
\noindent\makebox[\textwidth]{\includegraphics[width=\paperwidth]{\chapdir/0402xB.pdf}}
\newpage
\noindent\makebox[\textwidth]{\includegraphics[width=\paperwidth]{\chapdir/0402xC.pdf}}
\newpage
\noindent\makebox[\textwidth]{\includegraphics[width=\paperwidth]{\chapdir/0402xD.pdf}}

%							4 - 3
%\newpage
\invisiblesection{Composition}
%\subsection{Nesting}
\noindent\makebox[\textwidth]{\includegraphics[width=\paperwidth]{\chapdir/0403p.pdf}}
\newpage
\input{\chapdir/0403}
\newpage
\subsection{Exercises}
\noindent\makebox[\textwidth]{\includegraphics[width=\paperwidth]{\chapdir/0403xA.pdf}}
\newpage
\noindent\makebox[\textwidth]{\includegraphics[width=\paperwidth]{\chapdir/0403xB.pdf}}
\newpage
\noindent\makebox[\textwidth]{\includegraphics[width=\paperwidth]{\chapdir/0403xC.pdf}}
\newpage
\noindent\makebox[\textwidth]{\includegraphics[width=\paperwidth]{\chapdir/0403xD.pdf}}

%							4 - 4
\newpage
\invisiblesection{Reciprocal}
\marginlessinput{\chapdir/0404p}
\newpage
\input{\chapdir/0404}
~\vfill
\newpage
\subsection{Exercises}
Word, needs circle inversion geometry
~\vfill

%							4 - 5
%\newpage
\invisiblesection{Inverse}
\noindent\makebox[\textwidth]{\includegraphics[width=\paperwidth]{\chapdir/0405p.pdf}}
\input{\chapdir/0405}
\newpage
\subsection{Exercises}
\noindent\makebox[\textwidth]{\includegraphics[width=\paperwidth]{\chapdir/0405x.pdf}}

\newpage
\section{Review}
\subsection{Chapter Review}
In this chapter we saw that the absolute value of common quantities have names and that absolute
values of known functions are reflections of certain parts of the graph only.
A function does not have to mean $y$ is a function of $x$: they can both be a function of
another variable, usually time.  Another way to write the derivative is $\frac{dy}{dx}$.
We saw that most functions can be understood as being
made out of parts --- called composition.  The rate of change of such these ``Russian doll''
functions is ``the derivative of the outside times the derivative of the inside''.

Next we looked at the reciprocals of graphs and lastly the inverses.  With numbers, these can
be notated the same way, but with functions, there is no shortcut to writing the derivative.
An inverse is the ``undo'' of a function, the swapping of input for output.  However, not all
functions are reversible across their entire domain.  The derivative of the inverse if the same
as the reciprocal of the derivative.

\subsection{Chapter Test}
\subsection{Cumulative Review}
This major section of the book has been about algebraic functions, that is, functions made out
of addition, subtraction, multiplication, division, and exponents (roots are exponents).  We
can either evaluate a function at a point, or take its limit.  Functions can be manipulate
algebraically or even combined.  We can move fluidly from verbal to numeric to algebraic to
graphical, or any combination thereof.  Algebraic functions have algebraic derivatives and are
themselves derivatives of more algebraic functions.
\subsection{Cumulative Test}


\documentclass[document]{article}
\usepackage[DIV=11]{typearea}
\usepackage{amsmath,amssymb}
\usepackage{amsthm}
\usepackage{array}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage[english]{babel}

%\NewDocumentCommand{\evalat}{sO{\big}mm}{%
%  \IfBooleanTF{#1}
%   {\mleft. #3 \mright|_{#4}}
%   {#3#2|_{#4}}%
%}
\linespread{1}

\begin{document}
\title{\textsc{}}
\maketitle
\tableofcontents

\newtheorem{theorem}{Theorem}
\newtheorem{assumption}{Assumption}
\newtheorem{definition}{Definition}
\newtheorem{remark}{Remark}
\newtheorem{outcome}{Outcome}
\newtheorem{proposition}{Proposition}
\newtheorem{problem}{Problem}

\section{Before midterm}
\subsection{Assignment 4}

\begin{problem}[3.B] A collection $\Sigma$ of subsets of a set $X$ is a base for a certain topology on $X$ iff $X$ is a union of of all sets in $\Sigma$ and the intersection of any two sets in $\Sigma$ is the union of some sets in $\Sigma$.\\

\noindent Proof: $(\impliedby)$ Let $\Sigma$ be a collection of subsets of a set $X$. Suppose that $X$ is the union of all sets in $\Sigma$ and the intersection of any two sets in $\Sigma$ is the union of some sets in $\Sigma$. Then, I want to show that $\Sigma$ is a base for a topology on $X$. I will do this by proving that the collection of arbitrary unions of elements of $\Sigma$ satisfy the three properties of topological structure.\\

\noindent Let $T$ be a collection of arbitrary unions of sets $\sigma$ in $\Sigma$. Since the union of all sets in $\Sigma$ is $X$, $X\in T$. Also, $T$ contains $\varnothing$ since $\cup_{i\in \varnothing} \sigma_i = \varnothing$.\\

\noindent Next, notice that arbitrary union of elements of $T$ is again in $T$, since
\begin{align}
    \bigcup_{j\in J}\left(\bigcup_{i_j\in I_j} \sigma_i \right) = \bigcup_{k \in K} \sigma_k \in T.
\end{align}
Also, the finite intersection of elements in $T$ is again in $T$, since the intersection of two elements of $T$ are in $T$. (The intersection of two elements is suffice to prove the intersection of a finite collection.)
\begin{align}
    \left( \bigcup_{i\in I} \sigma_{i}\right) \bigcap \left( \bigcup_{j\in J} \sigma_{j}\right)
    = \bigcup_{i\in I, j\in J} \left( \sigma_{i} \bigcap \sigma_{j} \right) = \bigcup_{i\in I, j\in J} \bigcup_{k_{ij}\in K_{ij}} \sigma_{k_{ij}} = \bigcup_{l\in L} \sigma_l \in T
\end{align}
since, by assumption, the intersection of $\sigma_i$ and $\sigma_j$ of $\Sigma$ is union of some elements of $\Sigma$. Hence, $T$ is a topology on $X$, and a collection $\Sigma$ of subsets of a set $X$ is a base for $T$.\\

\noindent $(\implies)$ Suppose that a collection $\Sigma$ of subsets of a set $X$ is a base for a certain topology $\Omega$ on $X$. \\

\noindent Notice that since $X$ is an open set in all topologies on $X$, $X$ is the union of some sets in $\Sigma$ by the definition of base. And, since the sets in $\Sigma$ are subsets of $X$, $X$ is the union of all sets in $\Sigma$.\\

\noindent Notice that since $\sigma_i \in \Sigma$ is open, the intersection of two sets in $\Sigma$ is open by the definition of topology. Then, the intersection of two sets is a union of some sets in $\Sigma$ by the definition of base.

\end{problem}

\begin{problem}[3.4] What topological structures have exactly one base?\\

% Hint: The whole topological structure is its own base. So, the question is, when is this the only base. No open set in such a space is the union of two open sets distinct from the entire space. Hence, open sets are linearly ordered by inclusion. Furthermore, the space should contain no increasing infinite sequence of open sets (what is an increasing infinite sequence of open sets?)since, otherwise, an open set could be obtained as a union of sets in such a sequence.\\

Let us find the topological structure $\Omega$ for some set $X$ such that there is precisely one base. Note that for a given base, there is a unique topology, and for a given topology, there can be more than one bases. For a given topology $T$ on $X$, we note that if there exists a pair of open subset $U, V$ such that it is not $U \subset V$ and not $V \subset U$, then there exists at least two bases for T. To show this, note that $U \cup V$ is also an open subset, and hence should be represented as a union of base elements. We can either do this by choosing the base elements $U, V$ or choosing the base elements $U, V, U \cup V$. Since this is true for any pair of open subsets, in order to have a unique base, we require $U \subset V$ or $V \subset U$ holds for every pair of open subsets. This is equivalent to saying that the open sets satisfy a sequence $\varnothing \subset U \subset \cdots \subset V \subset X$. Furthermore, notice that the space should not contain any increasing infinite sequence of open sets, because an open set may the result of the union of sets in the sequence that is distinct from the whole space. Consider a topology on $(0,1)$ where the open sets are $(0, 1-\frac{1}{n})$, where $n \in \mathbb{N}$. Then, $\bigcup_{n\in\mathbb{N}} (0, 1-\frac{1}{n}) = (0,1)$, which allows for more than one base.
\end{problem} %my love baby

\begin{problem}[3.11] Show that the $T_1$-topology on the real line is coarser than the canonical topology.\\

% Hint: You must only prove that $\mathbb{R} \setminus {x_i}_{i=1}^n$ is open in the canonical topology of the line.

Let us show that the $T_1$-topology on $\mathbb{R}$ denoted as $\Omega_{T_1}$ given by the empty set and complements of all finite subsets of $\mathbb{R}$ satisfies $\Omega_{T_1} \subset \Omega$ where $\Omega$ is the standard topology on $\mathbb{R}$. We need to show that for any finite subset $y \in \mathbb{R}$, it is possible to express its complement as an arbitrary union of open intervals $\mathbb{R}\setminus y = \bigcup_{i\in I} (a_i,b_i)$ for $a,b \in \mathbb{R}$. You can always do this since you can express $\mathbb{R} \setminus y$ as
\begin{align}
    \left(\bigcup_{n\in \mathbb{N}} (\min{y}-n, \min{y}) \right)\bigcup \left(\bigcup_{n \in \mathbb{N}} (\max{y}, \max{y}+n) \right) \bigcup \left(\bigcup_{i=1}^{N} (y_i, y_{i+1}) \right)
\end{align}
where ordering the elements in $y$, we have $\{y_1, y_2, \cdots, y_N \}$.
\end{problem}

\begin{problem}[4.7] What are the balls and spheres in $\mathbb{R}^2$ equipped with the metrics of 4.1 and 4.2?\\
\noindent 4.1. $\mathbb{R}^n \times \mathbb{R}^n \rightarrow \mathbb{R}_+ : \left(x,y\right) \mapsto max_{i=1,...,n} \left| x_i - y_i\right|$\\
\noindent 4.2. $\mathbb{R}^n \times \mathbb{R}^n \rightarrow \mathbb{R}_+ : \left(x,y\right) \mapsto \sum_{i=1}^n \left| x_i - y_i\right|$\\

% Hint: Squares with sides parallel to the coordinate axes and bisectors of the coordinate angles, respectively.

For a metric space $(\mathbb{R}^2,\rho^{(p)})$, the ball is one of the bases for the plane, $B_r^{(p)}(c)=\Sigma^{(p)}$, with its center at $c \in \mathbb{R}^2$. Notice that the metric of 4.1 is $p^{(\infty)}$, and the metric of 4.2 is $p^{(1)}$. In particular, for $p=\infty$, the ball in the metric space $(\mathbb{R}^2, \rho^{(\infty)})$ given by $B_r^{(\infty)}(y) = \{ x \in \mathbb{R}^2 | \max_{i=1,2}\{|x_i-y_i|\} < r \}$ is $\Sigma^\infty$, which are open squares with sides parallel to the coordinate axes, with the Euclidean distance from $y$ to any nearest point of edges being $r$. For $p=1$, the ball in the metric space $(\mathbb{R}^2, \rho^{(1)})$ given by $B_r^{(1)}(y) = \{ x \in \mathbb{R}^2 | \sum_{i=1}^2 |x_i-y_i| < r \}$ is $\Sigma^1$, which are open squares such that its diagonals are parallel to the coordinate axes, with length given by the Euclidean metric from $y$ to any vertex is $r$.

\end{problem}

\begin{problem}[4.8] Find $D_1(a), D_{1/2}(a)$ and $S_{1/2}(a)$ in the space of 4.A.\\

% Hint: We have D_1(a) = X, D_{1/2}(a) = {a} and S_{1/2}(a)= empty set.

For a metric space $\left(X,\rho\right)$ with $\rho(x,y) =  \begin{cases} 0 & \text{if} \ x=y \\ 1 & \text{if} \ x\neq y \end{cases}$, we have
\begin{align}
    D_r(a)&=\begin{cases} \{a\} & r < 1 \\ X & r \geq 1 \end{cases} \\
    D_1(a)&=X, D_{1/2}(a)=\{a\} \\
    S_r(a)&=\begin{cases} \{a\} & r=0 \\ X/\{a\} & r=1 \\ \varnothing & \text{otherwise} \end{cases} \\
    S_{1/2}(a) &= \varnothing
\end{align}
\end{problem}

\begin{problem}[4.9] Find a metric space and two balls in it such that the ball with the smaller radius contains the ball with the bigger one and does not coincide with it.\\

% Hint: For example, let X = D_1(0) \subset R^1. Then, D_{3/2}(5/6) \subset D_1(0).

Let us find a metric space $(X,\rho)$ and $a_r, a_R \in X$ such that $B_R(a_R) \subsetneq B_r(a_r)$ where $r<R,$ and $r, R \in \mathbb{R_+}.$ Consider the following metric in $\mathbb{R}^n$, where $|| \cdot ||$ is the Euclidean metric $(||x||=(x_1^2+\cdots+x_n^2)^{1/2})$.
\begin{align}
    \rho: \mathbb{R}^n \times \mathbb{R}^n \rightarrow \mathbb{R}_+ : (x,y) \mapsto \begin{cases} ||x||+||y|| & x \neq y \\ 0 & x=y \\ \end{cases}.
\end{align}
Notice that this is a metric. $\rho(x,x) = 0$ by construction. Next,  $\rho(x,y) = ||x|| + ||y|| = ||y|| + ||x|| = \rho(y,x)$. Also, $\rho(x,y)$ = $||x|| + ||y|| \leq  ||x||+||z||+||z||+||y||= \rho(x,z) + \rho(z,y)$. As an example, consider $\mathbb{R}^2$, where the two balls are $B_{3/2} (0,0)$ and $B_2 (1,0)$. Note that $B_{3/2}(0,0) = \{x \in \mathbb{R}^2 | \rho(x,y)<3/2, y= (0,0) \in \mathbb{R}^2 \} = \{x=(a,b)\in \mathbb{R}^2 |\sqrt{a^2+b^2}+\sqrt{0^2+0^2} = \sqrt{a^2+b^2} <3/2 \}$ while
\begin{align}
    B_2 (1,0)=\{(1,0)\} \bigcup \{(x,y)|\sqrt{x^2+y^2} < 1\} \subsetneq B_{2/3} (0,0).
\end{align} Notice that the radius of the second ball is greater than the first one since $(3/2<2)$. These examples can easily be constructed in $\mathbb{R}^n$ by analogy. (The intuition for this metric is that the `distance' between two points $x,y \in \mathbb{R}^n$ defined by this metric is the distance of travelling from $x$ to $O$, the origin, and to $y$.)
\end{problem}

\begin{problem}[4.10] What is the minimal number of points in the space which is required to be constructed in 4.9?\\

% Hint: Three points suffice

Consider the metric space $(X,\rho)$ where $X$ has only one point $x$. A ball with 0 radius $B_0$ is an empty set. Every other ball with nonzero radius $B_{r>0}$ is exactly $X$, since from the definition of metric, $\rho(x,x)=0$. Hence, there is no case where a ball with bigger radius is contained in the ball with smaller radius and they do not coincide. Now, consider the metric space $(X,\rho)$ where $X$ has two elements $x,y$. Similarly, a ball with zero radius is an empty set. For other ball with nonzero radius, note that two balls centered at the same point can never achieve the case in 4.9 since $B_r{(x)} \subset B_R(x)$ if $r<R$ and same for $y$. Thus, the only case left is $B_r{(x)}$ and $B_R({y})$, where without loss of generality, we chose $r<R$ for $x, y$. We should look for $B_r(x)=\{x,y\}$ and $B_R(y)=\{y\}$. However, note that $\rho(x,y)=\rho(y,x)$ and if $\rho(x,y) < r$, since $y \in B_r(x)$, then $\rho(y,x) < R$ so $x$ must be in $B_R(y)$. Hence, for two-point set metric space $(X, \rho)$, we cannot establish 4.9. For a three-point set $X$, we have an explicit example. Recall the metric space $(\mathbb{R}^2, \rho)$ in 4.9. Restrict the set $X$ to its subset with three points $X'=\{(0,0), (1,0), (2.9/2,0)$. Notice that $B_2(1,0)=\{(1,0),(0,0)\} \subsetneq B_{3/2}(0,0)=\{(0,0), (1,0), (2.9/2,0)\}$. Hence the minimum number of points in $X$ such that 4.9 is established is 3.

\end{problem}

\begin{problem}[4.11] Prove that the largest radius in 4.9 is at most twice the smaller radius.\\

% Hint: Let R>r and let D_R(b) \subset D_r(a). Take c \in D_R(b) and use the triangle inequality p(b,c) \leq p(b,a) + p(a+c).

Suppose that $D_R(b) \subset D_r(a)$, where $D_r(a)$ and $D_R(b)$ are balls in the metric space ($X$,$\rho$) as defined in 4.9, and $r<R$. Take any $c \in D_R(b) \subset D_r(a)$. Since ($X$, $\rho$) is a metric space, the triangle inequality, $\rho(b,c) \leq \rho(b,a) + \rho(a,c)$ must hold. Also, it must be that $\rho(b,c)$ is at most $R$ and $\rho(b,a)$ and $\rho(a,c)$ are at most $r$. Then we have that $\rho(b,c) \leq R \leq \rho(b,a)+\rho(a,c) \leq 2r$. Thus, $R \leq 2r$.
\end{problem} %Good my love!

\subsection{Assignment 5}

\begin{problem}[4.J] An indiscrete space is not metrizable if it is not a singleton (otherwise, it has too few open sets). \\

% Hint: The indiscrete space does not have sufficiently many open sets. For x,y in X and r = p(x,y)>0, the ball D_r(x) is nonempty and does not coincide with the whole space (it does not contain y).

Let us prove that an indiscrete space on a set $X$ is not metrizable unless it is one-point (intuitively, it has too few open sets). Assume that the indiscrete topology $\{\varnothing, X\}$ is generated by a metric $\rho$ of the metric space $(X,\rho)$. This means that all open balls in the metric space $(X, \rho)$ is precisely $\{\varnothing, X\}$. Since every ball with nonzero radius contains its center, they must be exactly $X$. This means that for any $x\in X$, $B_r(x) = \{y\in X| \rho(x,y)<r\}=X$ for all $r>0$. In other words, $\rho(x,y) <r$ for all $r>0$. From analysis, this means that $\rho(x,y)=0$ for all $x,y \in X$. Since we assumed that $\rho$ is a metric, i.e. $\rho(x,y)=0 \iff x=0$, this is not contradictory only if $X=\{x\}$. If $X$ has more than two elements, there exists $x,y\in X$ such that $x\neq y$ and $\rho(x,y)$, which makes it contradictory with $\rho$ being a metric.
\end{problem}

\begin{problem}[4.K]A finite space $X$ is metrizable iff it is discrete.\\

% Hint: (->) For x in X, put r = min{p(x,y) | y in X\x}. Which points are in B_r(x)? (<-) Obvious.

Let us prove that a finite space $X$ is metrizable iff it is discrete.
\end{problem}

\begin{problem}[4.32] 1) Prove that if $\rho_1$ and $\rho_2$ are two metrics in $X$, then $\rho_1 + \rho_2$ and max$\left{\rho_1,\rho_2\right}$ are also metrics. 2) Are the functions min$\left{\rho_1,\rho_2\right}$, $\rho_1\rho_2$ and $\rho_1$/$\rho_2$ metrics? (By definition, for $\rho = \rho_1/\rho_2$ we put $\rho(x,x)=0$.)\\

% Hint: Clearly, the only thing in all five cases which is to be proved and is not completely obvious is the triangle inequality. It is also obvious for p_1 + p_2. Furthermore, p_1(x,y) \leq p_1(x,z) + p_1(z,y) \leq max{p_1(x,z),p_2(x,z)} + max(p_1(y,z),p_2(y,z). A similar inequality holds true for p_2(x,y), and, therefore, max{p_1,p_2} is a metric. Construct examples which would prove that neither min{p_1,p_2}, nor p_1/p_2, nor p_1p_2 is a metric. (To do this, it would suffice to find three points with appropriate pairwise distances.)

Let us prove that if $\rho_1, \rho_2$ are metrics in $X$, then $\rho_1+\rho_2$ and $\max\{\rho_1,\rho_2\}$ are also metrics.\\
First, define $\rho=\rho_1+\rho_2$. We have $\rho(x,y)=\rho_1(x,y)+\rho_2(x,y)=0 \iff \rho_1(x,y)=0$ and $\rho_2(x,y)=0 \iff x=y$ and $\rho(x,y)=\rho_1(x,y)+\rho_2(x,y)=\rho_1(y,x)+\rho_2(y,x)=\rho(y,x)$ as well as the triangular inequality
\begin{align}
    \rho(x,y)&=\rho_1(x,y)+\rho_2(x,y) \leq \rho_1(x,z)+\rho_(z,y)+\rho_2(x,z)+\rho_2(z,y)\\
    &=\rho_1(x,z)+\rho_2(x,z)+\rho_1(z,y)+\rho_2(z,y)=\rho(x,z)+\rho(z,y)
\end{align}
which proves that $\rho$ is a metric. Now, we define $\rho'=\max\{\rho_1,\rho_2\}$. Again, we have $\rho'(x,y)=\max\{\rho_1(x,y),\rho_2(x,y)\}=0 \iff \rho_1(x,y)=0$ and $\rho_2(x,y)=0$ (since both $\rho_1,2(x,y) \geq 0$) $\iff x=y$, and we also have $\rho'(x,y)=\max\{\rho_1(x,y),\rho_2(x,y)\}=\max\{\rho_1(y,x),\rho_2(y,x)\}=\rho'(y,x)$, as well as the triangular inequality:
\begin{align}
    \rho'(x,y) &= \max\{\rho_1(x,y),\rho_2(x,y)\}\\
    &\leq \max\{\rho_1(x,y), \rho_2(x,y), \rho_1(x,z)+\rho_2(z,y), \rho_2(x,z)+\rho_1(z,y), \} \\
    &\leq \max\{\rho_1(x,z)+\rho_1(z,y), \rho_1(x,z)+\rho_2(z,y), \rho_2(x,z)+\rho_1(z,y), \rho_2(x,z)+\rho_2(z,y)\}\\
    &= \max\{\rho_1(x,z),\rho_2(x,z)\}+\max\{\rho_1(z,y),\rho_2(z,y)\}\\
    &=\rho'(x,z)+\rho'(z,y)
\end{align}
Hence, $\rho'$ is a metric.

Let us also prove (or disprove) that $\min\{\rho_1,\rho_2\}, \rho_1/\rho_2,$ and $\rho_1\rho_2$ are metrics, where we define $\rho(x,x)=0$ for $\rho=\rho_1/\rho_2$. (think more)

\end{problem}

\begin{problem}[4.33] Prove that if $\rho:X \times X \rightarrow \mathbb{R}_+$ is a metric, then\\
(1) the function $(x,y) \mapsto \frac{\rho(x,y)}{1+\rho(x,y)}$ is a metric\\
(2) the function $(x,y) \mapsto min\{\rho(x,y)\}$ is a metric\\
(3) the function $(x,y) \mapsto f(\rho(x,y))$ is a metric if f satisfies the following conditions:\\

(a) $f(0) = 0$\\

(b) $f$ is a monotone increasing function, and\\

(c) $f(x+y) \leq f(x) + f(y)$ for any $x,y \in \mathbb{R}$.

% Hint: Assertion (c) is quite obvious. Assertions (a) and (b) follow from (c) for f(t) = t/(1+t) and f(t) = min{1,t}, respectively. Thus, it suffices to check that these functions satisfy the assumptions of assertion (c).

Let us prove that given a metric space $(\rho, X)$, we can make new metrics
\begin{align}
    \rho_1 &: (x,y) \mapsto \frac{\rho(x,y)}{1+\rho(x,y)}\\
    \rho_2 &: (x,y) \mapsto \min \{\rho(x,y),1\}\\
    \rho_3 &: (x,y) \mapsto f(\rho(x,y))
\end{align}
where $x,y \in X$ and $f$ satisfies the following conditions: $f(0)=0$, $f$ is monotonically increasing, and $f(x+y_ \leq f(x)+f(y)$ for any $x,y \in \mathbb{R}$.
\end{problem}

\begin{problem}[4.27] Prove that two metrics $\rho_1$ and $\rho_2$ in $X$ are equivalent if there are numbers $c,C > 0$ such that $c\rho_1(x,y) \leq \rho_2(x,y) \leq C\rho_1(x,y)$ for any $x,y \in X$.\\

% Hint: First, we prove that \Omega_2 \subset \Omega_1 provided that p_2(x,y) \leq Cp_1(x,y). Indeed, the inequality p_2 \leq Cp_1 implies B_r^{p_1}(a) \subset B_{Cr}^{p_2}. Now let us use Theorem 4.1. The inequality cp_1(x,y) ]leq p_2(x,y) can be written as p_1(x,y) \leq (1/c)(p_2(x,y). Hence \Omega_1 \subset \Omega_2.

Let us prove that two metrics $\rho_1$ and $\rho_2$ in $X$ are equivalent if there exists $c, C \in \mathbb{R}$ such that $c\rho_1(x,y) \leq \rho_2 (x,y) \leq C \rho_1 (x,y)$ for any $x,y \in X$.
\end{problem}

\begin{definition}[Section 8: Set theory]
\end{definition}

\begin{definition}[Section 9: Continuous maps]
Let $X, Y$ be two topological spaces. A map $f:X\rightarrow Y$ is continuous if the preimage of any open subset of $Y$ is an open subset in $X$.
\end{definition}

\begin{problem}[9.A] We have $f(f^{-1}(B)) \subset B$ for any map $f:X \rightarrow Y$ and any $B \subset Y$.\\

% Hint: If x in f^{-1}(B) then f(x) in B.

Let us show that a map is continuous if and only if the preimage of each closed set is closed. Since a closed set $y'\in Y$ is given by $Y\setminus y$ where $y\in Y$ is open, we need to show that the preimage of $Y \setminus y$ is a closed set $x'=X \setminus x$ where $x\in X$ is open. If the map is continuous, then the preimage of $y$ is open, which we can call $x$. Note that the image of $X\setminus x$ is $Y \setminus y$, the preimage of a closed set $y'=Y\setminus y$ is a closed set $x'=X\setminus x$. We also prove the other direction, starting by assuming that the preimage of each closed set is closed. For any closed set $y'\in Y$, we have its preimage $x'\in X$ which is closed. Then, we have $y=Y\setminus y'$ and $x=X\setminus x'$ which are both open. Note that the image of $x=X\setminus x'$ is precisely $Y\setminus y'$ since $x'$ is the preimage of $y'$. Hence, each open set $y$ has an open preimage $x$, meaning that the map is continuous.
\end{problem}

\begin{problem}[9.B] $f(f^{-1}(B)) = B$ iff $B \subset Im f$.\\

% Hint: (->) Obvious. (<-) For each y in B, there exists an element x such that f(x) = y. By the definition of the preimage, x in f^{-1}(B), whence y in f(f^{-1}(B)). Thus B \subset f(f^{-1}(B)). The opposite inclusion holds true for any set, see 9.A.

Let us prove that the identity map $id: X \rightarrow X$ for any topological space $X$ is continuous. Note that the preimage of $x\in X$ is $x$, meaning that the preimage of each open set is open.
\end{problem}

\begin{problem}[4]
Let us explicitly list images and kernels of each boundary map in the attached picture, and justify their dimensions. Also, let us try to compute the dimensions from the matrices describing the boundary maps.
\end{problem}

\begin{problem}[Coding 1: Due March 6]
\end{problem}

\begin{problem}[Coding 2: Due March 13]
\end{problem}

\end{document}


%% beamer packages
% other themes: AnnArbor, Antibes, Bergen, Berkeley, Berlin, Boadilla, boxes,
% CambridgeUS, Darmstadt, Dresden, Frankfurt, Goettingen, Hannover, Ilmenau,
%JuanLesPins, Luebeck, Madrid, Malmoe, Marburg, Montpellier, PaloAlto,
%Pittsburgh, Rochester, Singapore, Szeged, Warsaw
% other colors: albatross, beaver, crane, default, dolphin, dove, fly, lily,
%orchid, rose, seagull, seahorse, sidebartab, structure, whale, wolverine,
%beetle

%\documentclass[xcolor=dvipsnames]{beamer}
\documentclass[table,dvipsnames]{beamer}
\usepackage{beamerthemesplit}
\usepackage{bm,amsmath,marvosym}
\usepackage{listings,color}%xcolor
\usepackage[ngerman]{babel}
\usepackage{natbib}
\usepackage[utf8]{inputenc}
\definecolor{shadecolor}{rgb}{.9, .9, .9}
\definecolor{darkblue}{rgb}{0.0,0.0,0.5}
\definecolor{myorange}{cmyk}{0,0.7,1,0}
%\definecolor{mypurple}{cmyk}{0.5,1.0,0,0.2}
\definecolor{mypurple}{cmyk}{0.3, 0.9, 0.0, 0.2}

% make a checkmark
\usepackage{tikz}
\def\checkmark{\tikz\fill[scale=0.4](0,.35) -- (.25,0) -- (1,.7) -- (.25,.15) -- cycle;}

% dot product
\usetikzlibrary{arrows,positioning}
\tikzset{
    %Define standard arrow tip
    >=stealth',
    % Define arrow style
    pil/.style={->,thick}
}

% math stuff
\newcommand{\argmin}{\operatornamewithlimits{argmin}}

\lstnewenvironment{code}{
    \lstset{backgroundcolor=\color{shadecolor},
        showstringspaces=false,
        language=python,
        frame=single,
        framerule=0pt,
        keepspaces=true,
        breaklines=true,
        basicstyle=\ttfamily,
        keywordstyle=\bfseries,
        basicstyle=\ttfamily\scriptsize,
        keywordstyle=\color{blue}\ttfamily,
        stringstyle=\color{red}\ttfamily,
        commentstyle=\color{green}\ttfamily,
        columns=fullflexible
    }
}{}

\lstnewenvironment{codeout}{
    \lstset{backgroundcolor=\color{shadecolor},
        frame=single,
        framerule=0pt,
        breaklines=true,
        basicstyle=\ttfamily\scriptsize,
        columns=fullflexible
    }
}{}

\hypersetup{colorlinks = true, linkcolor=darkblue, citecolor=darkblue,urlcolor=darkblue}
\hypersetup{pdfauthor={A. Richards}, pdftitle={PCA and SVD}}

\newcommand{\rd}{\textcolor{red}}
\newcommand{\grn}{\textcolor{green}}
\newcommand{\keywd}{\textcolor{myorange}}
\newcommand{\highlt}{\textcolor{darkblue}}
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}
\def\ci{\perp\!\!\!\perp}
% set beamer theme and color
\usetheme{Frankfurt}
%\usetheme{Berkeley}
\usecolortheme{dolphin}
%\usecolortheme{seagull}
%\setbeamertemplate{blocks}[rounded][shadow=true]

\title[time]{PCA and SVD}
\author[AJR]{Adam Richards}
\institute{Galvanize, Inc}
\date[\today]{Last updated: \today}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\frame{\titlepage}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\footnotesize
\tableofcontents
\normalsize
}

+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Intro}
\subsection{}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{Dimension Reduction}
\footnotesize

\begin{block}{}
\begin{itemize}
 \item Remove multicolinearity
 \item Deal with the curse of dimensionality
 \item Remove redundant features
 \item Interpretation \& visualization
 \item Make computations of algorithms easier
 \item Identify structure for supervised learning
\end{itemize}
\end{block}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Standardization}
\footnotesize
Always start by standardizing the dataset

\begin{enumerate}
 \item Center the data for each feature at the mean (so we have mean 0)
 \item Divide by the standard deviation (so we have std 1)
\end{enumerate}

\highlt{sklearn.preprocessing}
\begin{itemize}
 \item \href{http://scikit-learn.org/stable/modules/preprocessing.html}{http://scikit-learn.org/stable/modules/preprocessing.html}
 \item The function \texttt{scale} provides a quick and easy way to perform this operation on a single array-like dataset
 \item the class \texttt{StandardScaler} provides further functionality as a \texttt{Transformer} (use \texttt{fit})
\end{itemize}

\begin{code}
from sklearn import preprocessing
X = preprocessing.scale(X)

scaler = preprocessing.StandardScaler().fit(X)
X = scaler.transform(X)
\end{code}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Scaling with a train/test split}
\scriptsize
\begin{code}
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

## create some data
X,y = make_classification(n_samples=50, n_features=5)

## make a train test split
X_train, X_test, y_train, y_test = train_test_split(X, y)

## scale using sklearn
scaler = StandardScaler().fit(X_train)
X_train_1 = scaler.transform(X_train)
X_test_1 = scaler.transform(X_test)

## scale without sklearn
X_train_2 = (X_train - X_train.mean(axis=0)) / X_train.std(axis=0)
X_test_2 = (X_test - X_train.mean(axis=0)) / X_train.std(axis=0)
\end{code}
\begin{flushleft}
 \tiny{\href{https://stats.stackexchange.com/questions/77350/perform-feature-normalization-before-or-within-model-validation}{https://stats.stackexchange.com/questions/77350/perform-feature-normalization-before-or-within-model-validation}}
\end{flushleft}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\footnotesize
\begin{columns}
\begin{column}{3cm}
\begin{equation*}
 \frac{1}{N} X^{T}X
\end{equation*}
\end{column}
\begin{column}{7cm}
 \begin{code}
import numpy as np
from sklearn import preprocessing

x1 =  np.array([[10,10,40,60,70,100,100]]).T
x2 =  np.array([[3,4,7,6,9,7,8]]).T
X = np.hstack([x1,x2]).astype(float)
n,d = X.shape
X = preprocessing.scale(X)

print(X.mean(axis=0),X.std(axis=0))
print(1.0/(n) * np.dot(X.T,X))
print(np.cov(X.T,bias=True))
 \end{code}
\end{column}
\end{columns}

\begin{codeout}
 [[ 1.          0.80138769]
 [ 0.80138769  1.        ]]
\end{codeout}

\begin{enumerate}
\scriptsize
 \item This tells us that the covariance between feature 1 and feature 2 is 0.801.
 \item This intuitively makes sense, since we can tell the two features are correlated
 \item The variance of each feature is 1 and this makes sense because we standardized our data first
 \item you can set \texttt{bias} to 1 if you want
\end{enumerate}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{an example}
\footnotesize
\begin{code}
x1 =  np.array([[0,1,2,3]]).T
x2 =  np.array([[3,2,1,0]]).T
X = np.hstack([x1,x2]).astype(float)
X = preprocessing.scale(X)
print(np.cov(X.T))
\end{code}
What should the signs on the covariance matrix look like?
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{an example}
\footnotesize
\begin{code}
x1 =  np.array([[0,1,2,3]]).T
x2 =  np.array([[3,2,1,0]]).T
X = np.hstack([x1,x2]).astype(float)
X = preprocessing.scale(X)
print(np.cov(X.T,bias=1))
\end{code}

What should the signs on the covariance matrix look like?
\begin{codeout}
[[ 1. -1.]
 [-1.  1.]]
\end{codeout}

$C_{0,1}$ shows the correlation between $x_0$ and $x_1$, which is negative
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{PCA}
\subsection{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{Why do PCA in the first place?}
\scriptsize
High dimensional data causes many problems. Here are a few:

\begin{enumerate}
 \item The Curse of Dimensionality
 \item It’s hard to visualize anything with more than 3 dimensions.
 \item Points are “far away” in high dimensions, and it’s easy to overfit small datasets.
 \item Often (especially with image/video data) the most relevant features are not explicitly present in the high dimensional (raw) data.
 \item Remove Correlation (e.g. neighboring pixels in an image)
\end{enumerate}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{PCA}
\scriptsize
\begin{itemize}
 \item How is it that correlation and covariance were related again?
\end{itemize}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{PCA}
\scriptsize
\begin{itemize}
 \item How is it that correlation and covariance were related again?
\end{itemize}

\begin{align}
   Cov[X,Y] &= E[(x - E[X])(y - E[Y])] \\
            &= \left[\underset{x,y \in S_X,S_Y}{\sum or\int}\right] (x - E[X])(y - E[Y])P(X=x,Y=y) \left[dxdy\right]
\end{align}

\begin{equation}
 Corr[X,Y] = \frac{E[(x - E[X])(y - E[Y])]}{\sigma_X\sigma_Y} = \frac{Cov[X,Y]}{\sigma_X\sigma_Y}
\end{equation}
\begin{itemize}
\item In say linear regression what might an ideal covariance matrix look like?
\end{itemize}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{PCA}
\footnotesize
The ideal convariance matrix would look something like this
$$
  \begin{bmatrix}
    10 & 0  & 0 & 0 \\
    0  & 11 & 0 & 0 \\
    0  & 0  & 5 & 0 \\
    0  & 0  & 0 & 8
    \end{bmatrix}
$$

\begin{block}{Principal Component Analysis}
Usually we will get a covariance matrix with a lot of large values. Our ideal would be one where all the non-diagonal values are 0. This means that there is no relationship between the features. We can do a transformation of this data to make this happen
\end{block}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\frametitle{Behind the PCA}
\footnotesize
\begin{enumerate}
 \item Create the design matrix
 \item Standardize your matrix
 \item Compute $\frac{1}{N} X^{T}X$
 \item The principal components are the eigenvectors of the covariance matrix
\end{enumerate}

\begin{block}{}
 The size of each eigenvector’s eigenvalue denotes the amount of variance captured by that eigenvector
\end{block}

\begin{block}{}
Ordering the eigenvectors by decreasing corresponding eigenvalues, you get an uncorrelated
and orthogonal basis capturing the directions of most-to-least variance in your data
\end{block}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{SVD}
\subsection{}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\footnotesize
\frametitle{Singular Value Decomposition (SVD)}
\begin{itemize}
 \item So we can use a technique called SVD for more effiecient computation
 \item It is not always easy to directly compute eigenvalues and eigenvectors
 \item SVD is also useful for discovering hidden topics or latent features
\end{itemize}

Every matrix has a unique decomposition in the following form
\begin{equation*}
 M = U \Sigma V^{T}
\end{equation*}

where
\begin{itemize}
 \item $U$ is column orthogonal: $U^{T}U = I$
 \item $V$ is column orthogonal: $V^{T}V = I$
 \item $\Sigma$ is a diagonal matrix of positive values, where the diagonal is ordered in decreasing order
\end{itemize}

We can reduce the dimensions by sending the smaller of the diagonals to 0.
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\footnotesize
\frametitle{SVD and PCA}
In PCA we had
\begin{equation*}
 M^{T}MV = V \Lambda
\end{equation*}
where $\Lambda$ is the diagonal matrix of eigenvalues

According to SVD we have
\begin{equation*}
 M = U \Sigma V^{T}
\end{equation*}

\begin{align*}
 M^{T}M &= (U \Sigma V^{T})^{T} U \Sigma V^{T}\\
        &= V \Sigma^{T} U^{T} U \Sigma V^{T}\\
        &= V \Sigma^{2} V^{T}
\end{align*}

This is the same equation as with PCA we just have $\Lambda=\Sigma^{2}$
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\footnotesize
\frametitle{Movie ratings}
  \begin{center}
 \includegraphics[scale=0.2]{svd-table.png}\ \\
 \end{center}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\begin{code}
import numpy as np
from numpy.linalg import svd

M = np.array([[1, 1, 1, 0, 0],
              [3, 3, 3, 0, 0],
              [4, 4, 4, 0, 0],
              [5, 5, 5, 0, 0],
              [0, 2, 0, 4, 4],
              [0, 0, 0, 5, 5],
              [0, 1, 0, 2, 2]])

u, e, v = svd(M)
print M
print "="
print(np.around(u, 2))
print(np.around(e, 2))
print(np.around(v, 2))
\end{code}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
\scriptsize
  \begin{center}
 \includegraphics[scale=0.15]{svd-out.png}\ \\
 \end{center}

With $M = U \Sigma V^{T}$, $U$ is the user-to-topic matrix and $V$ is the movie-to-topic matrix.
\ \\ \ \\
\highlt{Science Fiction}
\begin{itemize}
   \item First singular value (12.4)
   \item First column of the U matrix (note: the first four users have large values)
   \item First row of the V matrix (note: the first three movies have large values)
\end{itemize}

\highlt{Romance}
\begin{itemize}
   \item Second singular value (9.5)
   \item Second column of the U matrix (note: last three users have large values)
   \item Second row of the V matrix (note: the last two movies have large values)
\end{itemize}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame{
The third singular value is relatively small, so we can exclude it with little loss of data. Let's try doing that and reconstruct our matrix

\scriptsize
  \begin{center}
 \includegraphics[scale=0.15]{svd-out-trim.png}\ \\
 \end{center}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frame[allowframebreaks]{
\frametitle{References}
\begin{tiny} \bibliography{galvanize.bib}
\bibliographystyle{apalike}         % Style BST file
\end{tiny}
}

\end{document}


                                                                                \documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{algorithm}
\usepackage{graphicx}
\usepackage[noend]{algpseudocode}

\newlength\tindent
\setlength{\tindent}{\parindent}
\setlength{\parindent}{0pt}
\renewcommand{\indent}{\hspace*{\tindent}}

\title{Manhattan Skyline Problem}
\author{Daniel Braithwaite}

\begin{document}
	\pagenumbering{gobble}
	\maketitle
	\newpage
  	\pagenumbering{arabic}

	\section{Partitioning The Problem}
		There are two ways of partitioning the Manhattan Skyline problem, one is by partitioning the list of buildings e.g. breaking the list down the middle. The other option is to partition the x-axis.

		\subsection{Partitioning By Buildings}
			When dividing the problem by the list of buildings, splitting the problem up is easy. You keep dividing the problem in half until you only have one building, from here calculating the skyline for one building is trivial. However recombining the skylines from two different sets of buildings is where it gets tricky.

		\subsection{Partitioning By x-axis}
			Partitioning this way would make combining solutions easier as by the time you reach the base case you would have a solution for some $x$ value and combining would be trivial. The hard part would be the splitting, you would want to split between buildings and try to avoid splitting in the middle of the building as then you would have that building in two lists.

	\section{Algorithm}
		The algorithm I designed splits the problem down by breaking up the list of buildings, halving it each time, until a list of size 1 is reached, at which point constructing the skyline for a single building is trivial. Merging the skylines back together is a bit more complicated, it works by taking the two partial skylines and moving through them at each point taking the one with the smallest $x$, Say we choose the next element from the left skyline then we also need to look at the last added element from the right skyline so we could tell if the left skyline was dropping below the right.\newline\newline

		When designing my algorithm I could of separated the main loop into two so that if we reached the end of one skyline we could just append the the rest of the remaining list to the output skyline. However this wouldn't improve the efficiency of the algorithm. I also felt it was more elegant to have it all in one loop.\newline\newline

		\begin{algorithm}
			\begin{algorithmic}[1]
				\Procedure{recConstructSkyline}{$buildings,start,end$}
					\If{$end - start = 1$}
						\State $building \gets buildings[start]$
						\State
						\Return skyline for building
					\EndIf

					\State $middle \gets (end - start)/2$
					\State
					\Return $mergeSkylines(recConstructSkyline(buildings, start, middle+start), recConstructSkyline(buildings, middle+start, end))$
				\EndProcedure

				\State

				\Procedure{mergeSkylines}{$leftSkyline, rightSkyline$}
					\State $skyline \gets empty list$
					\State
					\State $left \gets 0$, $right \gets 0$
					\State
					\While{$left < leftSkyline.size$ or $right < rightSkyline.size$}
						\If{$left >= leftSkyline.size$}
							\State $currentPair \gets rightSkyline[right]$
							\State $prevPair \gets leftSkyline[left-1]$
							\State $right++$
						\ElsIf{$right >= rightSkyline.size$}
							\State $currentPair \gets leftSkyline[left]$
							\State $prevPair \gets rightSkyline[right-1]$
							\State $left++$
						\ElsIf{$leftSkyline[left].x < rightSkyline[right].x$}
							\State $currentPair \gets leftSkyline[left]$
							\State $prevPair \gets rightSkyline[right-1]$ if possible
							\State $left++$
						\ElsIf{$rightSkyline[right].x < leftSkyline[left].x$}											\State $currentPair \gets rightSkyline[right]$
							\State $prevPair \gets leftSkyline[left-1]$ if possible
							\State $right++$
						\Else
							\State $currentPair \gets leftSkyline[left]$
							\State $prevPair \gets rightSkyline[right]$

							\State $right++$
							\State $left++$
						\EndIf
						\State
						\If{$prevPair$ is unassigned}
							\State add currentPair to skyline
							\State return to top of loop
						\ElsIf{$currentPair.height > prevPair.height$}
							\State skyline.add(currentPair)
						\Else
							\State $change \gets [currentPair.x, prePair.height]$

							\If{$skyline.lastElement.height != change.height$}
								\State skyline.add(change)
							\EndIf
						\EndIf
					\EndWhile
				\EndProcedure
			\end{algorithmic}

		\end{algorithm}

		\break

	\section{Proof Of Correctness}

		\subsection{Assumption}
			\begin{enumerate}
				\item The input is a list of triples of the form $(x_1, x_2, height)$, where $x_1 < x_2$
			\end{enumerate}

		\subsection{Requirements}
			\begin{enumerate}
				\item Sorted in ascending order by $x$
				\item The height at any $x$ position is the maximum height of any building spanning that $x$
				\item Any change in height must occur at the start or end of a building $x$
			\end{enumerate}

		\subsection{Direct}
			There is only one building as $n = 1$ say we had the building $(x_1, x_2, h)$ the skyline is trivial to create, we get the following ${(x_1, h), (x_2, 0)}$. Therefor clearly the requirements hold.

			\begin{enumerate}
				\item As in the input $x_1 < x_2$ then the skyline is sorted
				\item There is only one building so we can clearly see this is true as for any $x_1 <= x <= x_2$ the height is that of the building
				\item By the way we constructed the skyline, part three must hold as the only times the height changes is at $x_1$ and $x_2$ and these two $x$ values are at the start and end of the building
			\end{enumerate}

		\subsection{Divide}
			When we are splitting the data we don't touch the triples so the assumption still holds\newline
			$n_i < n$ as we are splitting the list into two halves from the middle so $n_i = n/2 < n$\newline

		\subsection{Combine}

			\subsubsection{Requirement 1}
			First we show that the output skyline is sorted, showing that part 1 of the requirements is true for the out. To show this we will use a loop invariant.\newline

			\textbf{Loop Invariant}
			\begin{enumerate}
				\item Skyline is sorted
			\end{enumerate}

			\textbf{Initialization:} Here $skyline \gets \varnothing$ and therefore is trivially sorted\newline

			\textbf{Maintenance:} We are choosing the next $x$ to be added from the left and right skylines, both of which are sorted. We look at the next element in both the lists and take the one with the smaller $x$ adding this to the skyline. so we can see if we have the skyline ${x_1, ....., x_k, x_{k+1}}$ then $ x_1 < ..... < x_k < x_{k+1})$.\newline

			Therefore part one of the requirements holds.

			\subsubsection{Requirement 2}
			Now we need to show that at any $x$ the height is the highest of any building at that $x$. We are merging two skylines, we will refer to these as the left and the right skylines.

			We start by looking at how we choose the next $x$ to be added to the skyline. When choosing this we look at the next element in the left and right skylines that are to be merged and pick the one with the smaller starting $x$. We set this to be currentPair.\newline

			Based on what list we choose currentPair from we set prevousPair to be the last element added to the output skyline from the other list. We are interested in this prevousPair because we want to keep track of the height in the other list so if the currentPair height drops below it then we know that the skyline is overlapping.\newline

			There are three cases
			\begin{enumerate}
				\item $currentPair.height > prevousPair.height$
				\item $currentPair.height < prevousPair.height$
				\item $currentPair.height = prevousPair.height$
			\end{enumerate}

			\textbf{Case 1} and \textbf{Case 3} are trivial, we just add the pair $(currentPair.x, currentPair.height)$ to the skyline\newline

			\textbf{Case 2} requires some more thought, we cant just add the pair $(currentPair.x, currentPair.height)$ as the height of the other skyline is still prevousPair.height. So we add the pair $(currentPair.x, previousPair.height)$.

			\subsubsection{Requirement 3}
				Requirement part 3 holds as the next $x$ to be added is chosen from ether the left or right skyline, we know that all the requirements hold for the left and right skylines so therefore any $x$ taken from them must be where a building starts or finishes.

	\section{Complexity}

		\subsection{Direct}

		The complexity of solving the direct solution is $O(1)$, when we have one building constructing the skyline is very simple its just two tuples. Constructing this is clearly is $O(1)$.\newline

		\subsection{Divide}

		The complexity of the divide part of the algorithm is also $O(1)$ as nothing actually happens here we just recursively pass the array of buildings with a section for the function to look at.\newline

		\subsection{Combining}

		Combining has a complexity of $O(n)$ where n is the total number of buildings represented by the left and right skylines, we see this when we look at the following. The algorithm for merging skylines takes two skylines as input and will return one representing the combination. Each of the two input skylines represents $n/2$ buildings so at worse case (all the buildings being disjoint) each of the skylines will contain $2 * n/2 = n$ tuples. Meaning the loop will iterate at most $2n$ times. Thus giving us $O(n)$\newline

		\subsection{Asymptotic Cost}

		So we want to solve the following recurrence relation using the master method we have\newline\newline

		\[
			T(n) =
			\begin{cases}
				1 & n == 1\\
				2T(n/2) + n + 1 & otherwise
			\end{cases}
		\]\newline\newline

		We fit what we have to the following from the master method\newline
		\[
			T(n) = aT(n/b) + f(n)
		\]

		where we know that
		\[
			a = 2, b = 2, f(n) = n + 1
		\]

		so we let

		\[
			\alpha = \log_2 2 = 1
		\]

		and we see that

		\[
			f(n) \in \theta(n^1) = \theta(n)
		\]

		so finally by the master theorem we see that the asymptotic cost is the following

		\[
			\theta(n\log_2 n)
		\]

	\section{Implementation}

		When implementing the algorithm I used a list to store the buildings and to store the skylines. There can be a cost to adding to the list when the internal array gets full. We can get remove this cost as we know the maximum size of the new skyline so we can initialize the list with the maximum possible size. Using a list also meant it was very easy to divide up the buildings using indices without actually having to create any new lists.

	\break

	\section{Testing}

		I tested my algorithm on a number of randomly generated test cases from size 10000 to 100000, the results where promising as they matched up perfectly with my theoretical complexity. At each input size there where three test cases. The steps plotted on the y-axis is the number of times the loop in the merge function executes for each of the problem instances as everything inside this loop was constant time so all we need to worry about is the number of times it executes\newline\newline

		\begin{enumerate}
			\item \textbf{Worst Case} where all the buildings are disjoint, i.e. none of them overlap
			\item \textbf{Average Case} where all the buildings are randomly distributed
			\item \textbf{Best Case} where all the buildings overlap
		\end{enumerate}

		\begin{figure}[h]
			\vspace{3mm}
			\begin{center}
				\includegraphics[scale=0.4]{data.png}
			\end{center}
		\end{figure}

		As you can see in the graph above the theoretical complexity is bounded from above by the worst case(purple line) and bounded from below by the best case (green line). This matches up with the theoretical complexity $nlog_2(n)$

\end{document}


\documentclass{article}

\usepackage[english]{babel}
\usepackage{filecontents}
\usepackage[letterpaper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}

\usepackage{amsmath}
\usepackage{csquotes}
\usepackage{graphicx}
\usepackage{amsthm}

\usepackage[colorlinks=true, allcolors=blue]{hyperref}
\begin{filecontents}{\jobname.bib}
@article{10.1145/69622.357194,
author = {Peterson, Gary L.},
title = {An O(nLog n) Unidirectional Algorithm for the Circular Extrema Problem},
year = {1982},
issue_date = {Oct. 1982},
publisher = {Association for Computing Machinery},
address = {New York, NY, USA},
volume = {4},
number = {4},
issn = {0164-0925},
url = {https://doi.org/10.1145/69622.357194},
doi = {10.1145/69622.357194},
journal = {ACM Trans. Program. Lang. Syst.},
month = {oct},
pages = {758–762},
numpages = {5}
}
\end{filecontents}

\usepackage{natbib}
\usepackage{cleveref}
\newtheorem{lemma}{Lemma}
\newtheorem{theorem}{Theorem}
\begin{document}
\section*{Leader election in an directed ring}

Given $n$ processes in a directed ring with synchronous communication, communicating only with message passing to its neighbors, the circular extrema-finding (or election) problem is to select a maximum process. Each process has a unique value in a set with a total order. These values may be transmitted and compared. All processes are identical (except for their value) i.e run the same algorithm and $n$, the number of processes, is not initially known.

Here we describe an algorithm presented by \cite{10.1145/69622.357194}, which achieves the above and uses at most $2 n \lfloor\log n\rfloor + n$ messages in the worst case.
\subsection*{The algorithm}
\subsubsection*{Definitions and assumptions}
The algorithm will consist of electoral rounds.

We will distinguish 4 states a node can be in:
\begin{description}

\item[\textit{active}] --- node is taking part in this electoral round
\item[\textit{relay}] --- node does not participate in election anymore and only passes messages
\item[\textit{non-leader}] --- node is not the leader
\item[\textit{leader}] --- node is the leader

\end{description}

Messages sent will consist of pairs of "value" and "status". Values will be from the set of unique identifiers of the nodes participating, while status can be either \textit{active} or \textit{leader}.

\subsubsection*{Description}

Assume all messages are passed to the right. All processes are initially \textit{active}. \textit{Active} processes operate in phases. \textit{Relay} processes in one phase just pass two at most messages they receive. The primary goal is to have the number of active processes be cut at least in haft during each phase. Each process starts with its own value as its temporary identifier (\text{tid}). During a phase, each \textit{active} process receives the \textit{tid} of its nearest \textit{active} neighbor to the left and that neighbor's nearest left \textit{active} neighbor's \textit{tid}. If the first process sees that its left \textit{active} neighbor has the largest of the three \textid{tid}s, then it sets its \textit{tid} to that value and starts the next phase. Otherwise, it becomes a \textit{relay} process.

\subsubsection*{Procedures}

Each round starts with each \textit{active} sending message ($tid$, \textit{active}) to right neighbour. Afterwards, each \textit{active} awaits to receive a message from left.

Let's denote message received from left as $(m1.ntid, m1.status)$. We need to \textit{check} whether $m1.status$ is \textit{leader} or $m1.ntid$ is equal to node's unique identifier. In first case we need set our state to \textit{non-leader} and pass the message to right. In second case we need to declare this node as leader: set state to \textit{leader} and send message with $(i, \textit{leader})$ to right. If \textit{check} is failed then we wait for one more message from left as $(m2.nntid, m2.status)$. Do the same \textit{check} as previously, and if it's failed then there are two cases. In first case $m1.ntid \geq tid \land m1.ntid \geq m2.nntid$ we assign $m1.ntid$ to node's $tid$, otherwise we set the node state to \textit{relay}.

Nodes that become \textit{non-leader} are terminated. \textit{Leader} finishes when it receives message with status \textit{leader}.

\textit{Relay} waits for at most two messages from left processed one by one, for each message do \textit{check} and if it's failed just pass the message to the right.

\subsection*{Correctness}
The correctness of the algorithm follows from two facts: during any phase there is exactly one process whose \textit{tid} is the maximum value, and no process except the maximum will ever see its own initial value. The first fact comes by noting that the process whose \textit{tid} is the maximum value is by definition larger than its two \textit{active} neighbors, so the \textit{active} process to its right will have its \textit{tid} equal to the maximum on the next phase. The second fact follows by first noting that, for any process, its value is either the \textit{tid} value of the nearest \textit{active} process to its right, or its value no longer is being propagated. It therefore follows that in order for some nonmaximum value of a process to cycle around the ring, the largest value must already have passed the process (lest the \textit{active} process with \textit{tid} equal to the maximum prevents the passage of the lower value), but then the smaller value is no longer being propagated since the \textit{tid} value to the right of the process is now the maximum.
\subsection*{Complexity analysis}
It is very simple to see that at most $2n \log n + n$ messages are sent. First one
notes that there can be at most $\lfloor\log n\rfloor$ phases to reach one \textit{active} process. This follows from the fact that any \textit{active} process survives a phase only if the \textit{active} process to its left does not survive that phase. Hence of $m$ \textit{active} processes during a phase, at most $\lfloor\frac{m}{2}\rfloor$ will survive for $m > 1$. During a phase every process sends (and receives) two messages, and on the last phase the lone \textit{active} process sends one message which is relayed around to the maximum for a total of $2n \log n + n$.
\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}


\documentclass{scrartcl}
\usepackage[a4paper,left=1in,right=1in,top=1.2in,bottom=1in]{geometry}
\usepackage{siunitx}
\usepackage{graphicx}
\usepackage{mathtools}
\usepackage{amsmath}
\usepackage{amssymb}
\setkomafont{disposition}{\normalfont\bfseries}
\newcommand*\diff{\mathop{}\!\mathrm{d}}
\newcommand*\Diff[1]{\mathop{}\!\mathrm{d^#1}}

%title
\title{Exercise 03:\\Leaky Integrate \& Fire Neuron}
\subtitle{Theoretical Neuroscience I}
\author{Maria del Cerro \and Johannes G\"atjen \and Lorena Morton}

%use these for structure/overview
\newcommand\Question{%
  \textbf{Question:}%
}
\newcommand\Answer{%
  \textbf{Answer:}%
}

\begin{document}
\maketitle
\section{Simulation of a leaky integrate-and-fire neuron}

\Question\\
Plot the input current and the membrane voltage for a leaky integrate-and-fire neuron for a constant input current, an input current of low frequency and a linearly increasing input current. Use the following parameter values:

\begin{equation*}
r_m = 1.5 \si{\mega\ohm\square\milli\meter} \qquad c_m = 20 \si{\nano\farad\per\square\milli\meter} \qquad E_L= V_\mathrm{reset}= V_0 = -65 \si{mV} \qquad V_\mathrm{thresh} = -50 \si{mV}
\end{equation*}

Also compute the inter-spike-intervals.
\\

\Answer\\

\begin{itemize}
\item
Constant input current (Figure \ref{constant}): The membrane voltage rises exponentially until it reaches the threshold value of -50 \si{mV}. The neuron fires and the membrane voltage gets reset to $V_\mathrm{reset}$. As the input current does not change this is repeated over and over. The inter-spike-intervals are all equal, i.e. the spike rate is constant.
\item
Low frequency input current (Figure \ref{low}): With the added frequency the input current does not stay large enough for long enough for the membrane voltage to cross the threshold value to fire. As a result there are no spikes, and the membrane voltage behaves the same as for the single compartment model up to an offset of $E_L$.
\item
Ramping input current (Figure \ref{ramp}): The input current slowly increases over time. The membrane current first rises slowly, until it reaches $V_\mathrm{thresh}$ for the first time. The neuron fires and the membrane voltage is reset, then rises again, as the input current is still high enough to elicit another spike and continues to rise. After every spike the gradient of the membrane voltage is higher than after the last, because the input current is higher. Consequently the inter-spike-intervals also get shorter over time. This can also be seen in Figure \ref{intervals}, where the inter-spike-intervals are plotted against the spike number. Apparently the inter-spike-interval length decreases exponentially.
\end{itemize}

\begin{figure}
\centering
\includegraphics[trim = {1.1cm 0 1.7cm 1.1cm}, width=\textwidth, clip]{../pics/constant}
\caption{Top: The constant input current of 12 \si{\nano\ampere\per\square\milli\meter} plotted against time. Bottom: The resulting membrane voltage over time.}
\label{constant}
\end{figure}

\begin{figure}
\centering
\includegraphics[trim = {1.1cm 0 1.7cm 1.1cm}, width=\textwidth, clip]{../pics/low}
\caption{Top: The low frequency (4 \si{Hz}) input current $i_e(t) = 12 \si{\nano\ampere\per\square\milli\meter} \cdot \sin{(2\pi4t)}$ plotted against time. Bottom: The resulting membrane voltage over time.}
\label{low}
\end{figure}

\begin{figure}
\centering
\includegraphics[trim = {1.1cm 0 1.7cm 1.1cm}, width=\textwidth, clip]{../pics/ramp}
\caption{Top: The ramping input current $i_e(t) = 12 \si{\nano\ampere\per\square\milli\meter} \cdot t / 150 \si{ms}$ plotted against time. Bottom: The resulting membrane voltage over time.}
\label{ramp}
\end{figure}

\begin{figure}
\centering
\includegraphics[trim = {1.3cm 0 1.8cm 0.9cm}, width=0.7\textwidth, clip]{../pics/ramp_intervals}
\caption{The inter-spike-interval length as a function of the number of spikes generated so far for the ramping input current.}
\label{intervals}
\end{figure}

%lots of figures
%\begin{figure}
%\centering
%\includegraphics[trim = {1.3cm 0 2cm 0.9cm}, width=\textwidth, clip]{../pics/picname}
%\caption{caption text}
%\label{label}
%\end{figure}

\section{Optional assignment}

\Question\\
Analytically solve the dynamical equation
\begin{equation*}
\frac{\diff V_m(t)}{\diff t} = \frac{1}{\tau_m}\left( r_m \cdot i_e(t) + E_L - V_m(t)\right)
\end{equation*}
over a time interval $\Delta t$, to derive the numerical iteration rule
\begin{equation*}
V_m(t+\Delta t) = V_m(t) \exp\left(-\frac{\Delta t}{\tau_m}\right) + \left(i_e(t) \cdot r_m + E_L \right) \left( 1 - \exp{\left( -\frac{\Delta t}{\tau_m}\right)}\right).
\end{equation*}

\Answer\\
Like in the single compartment model, the membrane potential relaxes towards an equilibrium potential as long as the membrane potential is below the threshold potential. Since we assume $i_e(t)$ to be constant over small intervals, we can say that $V_\infty = r_m \cdot i_e(t) + E_L$ and replace it in our dynamical equation and the numerical iteration rule for easier readability.
\begin{equation*}
\frac{\diff V_m(t)}{\diff t} = \frac{1}{\tau_m}\left( V_\infty - V_m(t)\right)
\end{equation*}
\begin{equation*}
V_m(t+\Delta t) = V_m(t) \exp\left(-\frac{\Delta t}{\tau_m}\right) + V_\infty \left( 1 - \exp{\left( -\frac{\Delta t}{\tau_m}\right)}\right)
\end{equation*}
With some rearrangement we obtain:
\begin{equation*}
V_m(t+\Delta t)= V_\infty + e^{-\frac{\Delta t}{\tau_m}}\left( V_m(t) -V_\infty\right)
\end{equation*}

We rearrange the terms in the dynamical equation to separate the voltage and time differentials:
\begin{alignat*}{2}
& &\frac{1}{V_\infty - V_m(t)} \diff V_m(t) &= \frac{1}{\tau_m} \diff t\\
\shortintertext{Now we can integrate over an interval $[t', t'+\Delta t]$.}
&\Leftrightarrow \qquad & \int_{t'}^{t'+\Delta t} \frac{1}{V_\infty - V_m(t)}\diff V_m(t) &= \int_{t'}^{t'+\Delta t} \frac{1}{\tau_m} \diff t\\
&\Leftrightarrow \qquad & \left[-\ln{\left(V_\infty - V_m(t)\right)} \right]_{t'}^{t'+\Delta t} &= \left[\frac{t}{\tau_m} \right]_{t'}^{t'+\Delta t}\\
&\Leftrightarrow \qquad & -\ln{\left(V_\infty - V_m(t'+\Delta t)\right)} + \ln{\left(V_\infty - V_m(t')\right)} &= \frac{t'+\Delta t}{\tau_m} - \frac{t'}{\tau_m}\\
\shortintertext{Exponentiate $e$ with the equation\dots}
&\Leftrightarrow \qquad & \frac{V_\infty - V_m(t')}{V_\infty - V_m(t'+\Delta t)} &= e^\frac{\Delta t}{\tau_m}\\
\shortintertext{\dots and do some simple rearrangement to solve for $V_m(t'+\Delta t)$:}
&\Leftrightarrow \qquad & V_m(t'+\Delta t) &= V_\infty + e^{-\frac{\Delta t}{\tau_m}}\left( V_m(t') - V_\infty \right)
\end{alignat*}
\hfill $\square$

\end{document}