%%%%%%%%%%%%%%%%%%%%%%
% Useful Observation %
%%%%%%%%%%%%%%%%%%%%%%

\section{Useful Observations}

\begin{observation}{}{Bounding Measures}

    The following inequalities to bound measures are \emph{always} applicable, for \emph{any} sets $A, B, C \in \CalF$:

    \begin{enumerate}
        \setlength{\parskip}{0em}
        \item ``Dropping a set in an intersection gives an upper bound'' $\Leftrightarrow$ ``Relaxing constraints'':

            \begin{align*}
                \mu(A \cap B) \leq \mu(A).
            \end{align*}
        \item ``Dropping a set in a union gives an lower bound'':

            \begin{align*}
                \mu(A \cup B) \geq \mu(A).
            \end{align*}
        \item ``Adding a set in a union gives an upper bound'' $\Leftrightarrow$ ``Adding constraints'':

            \begin{align*}
                \mu(A \cup B) \leq \mu(A \cup B \cup C).
            \end{align*}
        \item ``Intersections are less than a set and a set is less than a union'':

            \begin{align*}
                \mu(A \cap B) \leq \mu(A) \leq \mu(A \cup B).
            \end{align*}
    \end{enumerate}

\end{observation}

\begin{observation}{}{Adding $\Omega$ by Intersection}

    If you would like to introduce a property to an existing set $A$ to make it easier to work with, for instance easier to bound, you can add an intersection with $\Omega$:

        \begin{align*}
            \mu(A) = \mu(\Omega \cap A).
        \end{align*}

    Then $\Omega$ can be split into the set $B$ that represents the property and $B^C$ that does not have the property, where $\Omega = B \cup B^C$. Then:

        \begin{align*}
            \mu(A) = \mu(\Omega \cap A) = \mu((B \cup B^C) \cap A) = \\
            \mu((B \cup B^C) \cap A) = \mu((B \cap A) \cup (B^C \cap A)).
        \end{align*}

    Using $\sigma$-additivity, we get:

        \begin{align*}
            \mu(A) = \mu(B \cap A) + \mu(B^C \cap A).
        \end{align*}

    Then by the observation on bounding measures, this can be made into an inequality:

        \begin{align*}
            \mu(A) &= \mu(B \cap A) + \mu(B^C \cap A) \\
            &\leq \mu(B \cap A) + \mu(B^C).
        \end{align*}

\end{observation}

\begin{observation}{}{Increasing Sequence of Sets}

    For an \emph{increasing} sequence of sets $(A_n)_{n=1}^{\infty}$ we can define:

        \begin{align*}
            \lim_{n \to \infty} A_n \coloneqq \bigcup_{n=1}^{\infty} A_n
        \end{align*}

\end{observation}

\begin{observation}{}{Decreasing Sequence of Sets}

    For an \emph{decreasing} sequence of sets $(A_n)_{n=1}^{\infty}$ we can define:

        \begin{align*}
            \lim_{n \to \infty} A_n \coloneqq \bigcap_{n=1}^{\infty} A_n
        \end{align*}

\end{observation}

\begin{observation}{}{$\mu$-Almost Everywhere Finite, I}

    If $f: \Omega \to \mathbb{R}$ is $\mu$-a. e. finite, then note that if $A_n \coloneqq \{ |f| \geq n \}$, then $(A_n)_{n=1}^{\infty}$ is a decreasing sequence and so:

        \begin{align*}
            \mu\left(\bigcap_{n=1}^{\infty} A_n\right) = \mu\left(\lim_{n \to \infty} A_n\right) = \mu(|f| = \infty) \\ = 0.
        \end{align*}

\end{observation}

\begin{observation}{}{$\mu$-Almost Everywhere Finite, II}

    If $f: \Omega \to \mathbb{R}$ is $\mu$-a. e. finite, then observe

        \begin{align*}
            \mu(|f| = \infty) = \lim_{R \to \infty} \mu(|f| \geq R) = 0.
        \end{align*}

\end{observation}

\begin{observation}{}{Almost Surely Finite, II}

    If $f: \Omega \to \mathbb{R}$ is a.s. finite, then observe

        \begin{align*}
            P(|f| = \infty) = \lim_{R \to \infty} P(|f| \geq R) = 0. \\
            \iff P(|f| < \infty) = \lim_{R \to \infty} P(|f| < R) = 1.
        \end{align*}

\end{observation}

\begin{observation}{}{Almost Surely Finite}

    If $f: \Omega \to \mathbb{R}$ is a. s. finite, then note that if $A_n \coloneqq \{ |f| \geq n \}$, then $(A_n)_{n=1}^{\infty}$ is a decreasing sequence and so:

        \begin{align*}
            P\left(\bigcap_{n=1}^{\infty} A_n\right) = P\left(\lim_{n \to \infty} A_n\right) = P(|f| = \infty) \\ = 0.
        \end{align*}

\end{observation}

\begin{observation}{}{$\mu$-Almost Everywhere Convergence I}

    If $f_n \to f$ $\mu$-a.e., then $\mu(f_n \not\to f) = 0$.

\end{observation}

\begin{observation}{}{$\mu$-Almost Everywhere Convergence II}

    If $A \in \CalF$ is a set such that $\mu(A) = 0$ and

        \begin{align*}
            \lim_{n \to \infty} |f_n(\omega) - f(\omega)| = 0 \quad \forall \omega \in A^C,
        \end{align*}

    then $f_n \to f$ $\mu$-almost everywhere.

\end{observation}

\begin{observation}{}{Almost Sure Convergence}

    If $f_n \to f$ a.s., then $P(f_n \not\to f) = 0$ or equivalently $P(f_n \to f) = 1$.

\end{observation}

\begin{observation}{}{Splitting Measures of Inequalities}

    Let $f,g$ be measurable functions and $a \in \mathbb{R}$, then observe that:

        \begin{align*}
            \mu(|f| \geq a) \leq \mu\left( |f - g| \geq \frac{a}{2} \right) + \mu\left( |g| \geq \frac{a}{2} \right)
        \end{align*}

\end{observation}

\begin{observation}{}{Using Borel-Cantelli}

    If you can define sets $(A_k)_{k=1}^{\infty}$ such that $\mu(A_k) \leq 1/k^2$, then you can use Borel-Cantelli as:

        \begin{align*}
            \sum_{k=1}^{\infty} \mu(A_k) \leq \sum_{k=1}^{\infty} \frac{1}{k^2} < \infty.
        \end{align*}

    In fact, the choice of $1/k^2$ is more or less arbitrary. This technique would work with any $r_k$ s.t. $\sum_{k=1}^{\infty} r_k < \infty$ and $\mu(A_k) \leq r_k$. Caution: $r_k = 1/k$ does \emph{not} work.

\end{observation}

\begin{observation}{}{Function As Integral}

    Let $f: \Omega \to \overline{\mathbb{R}}$ be a \emph{non-negative} measurable function, the obvserve that

        \begin{align*}
            f(\omega) = \int\limits_0^{f(\omega)} \, dx = \int\limits_0^{\infty} \Indicator{x \leq f(\omega)} \, dx
        \end{align*}

\end{observation}

\begin{observation}{}{Bounding Complement Probabilities}

    Note that $1 - x \leq e^{-x}$. Therefore, we can bound probabilities of a product of complement events, for instance:

        \begin{align*}
            \prod_{n = 1}^{\infty} P(A_n^C) = \prod_{n = 1}^{\infty} [1 - P(A_n)] \leq \\ \prod_{n = 1}^{\infty} e^{-P(A_n)} = e^{\sum_{n = 1}^{\infty} -P(A_n)}
        \end{align*}

\end{observation}

\begin{observation}{}{Interchanging Expectation \& Infinite Sum}

    Observe that if $f$ is \emph{non-negative}, then:

        \begin{align*}
            E\left(\sum_{n=1}^{\infty} f(X_n)\right) =
            E\left(\lim_{N \to \infty} \sum_{n=1}^{N} f(X_n)\right) = \\
            = \lim_{N \to \infty} \sum_{n=1}^{N} E f(X_n) =
            \sum_{n=1}^{\infty} E f(X_n),
        \end{align*}

    where pulling the expectation through the sum can be done due to the Monotone Convergence Theorem, as $\sum_{n=1}^{N} f(X_n)$ is an increasing sequence of \emph{non-negative} random variables.

\end{observation}

\begin{observation}{}{Markov-Chebyshev's Inequality \& Norm}

    The following is the general Markov-Chebyshev Inequality rewritten using the norm instead of an integral. Let $f: \Omega \to \overline{\mathbb{R}}$ be a \emph{non-negative}, measurable function in $L_{\alpha}(\Omega, \CalF, \mu)$, then

        \begin{align*}
            \mu(f \geq \lambda) \leq \lambda^{-\alpha} \Norm{f}_{\alpha}^{\alpha} \, d\mu \quad \forall \lambda > 0, \alpha > 0.
        \end{align*}

\end{observation}

\begin{observation}{}{Distribution Function as Expectation}

    Let $X$ be a random variable and $F_X$ its distribution function. Then:

        \begin{align*}
            F_X(a) = P(X \leq a) = \int_{\Omega} \Indicator{X \leq a} \, dP = E\Indicator{X \leq a}.
        \end{align*}

\end{observation}

\begin{observation}{}{Distribution Function as Expectation, II}

    Let $X$ be a random variable and $F_X$ its distribution function. Then:

        \begin{align*}
            F_X(x + a) - F_X(x) = E\Indicator{x < X \leq x + a}.
        \end{align*}

\end{observation}

\begin{observation}{}{Tightening/Relaxing Expectations}

    Let $X$ be a random variable and $\lambda \in \mathbb{R}$. Then the following  holds:

        \begin{align*}
            EX \geq E(\Indicator{X \geq \lambda}X) \geq E(\Indicator{X \geq \lambda}\lambda).
        \end{align*}

    Left-to-right can be thought of as ``tightening'' the constraints and thus (potentially) decreasing the area that is integrated over, right-to-left as ``loosening'' and thus (potentially) increasing the area that is integrated over.

\end{observation}

\begin{observation}{}{Identical Distribution Giving Equal Probability}

    Let $(X_n)_{n=1}^{\infty}$ be a sequence of \emph{independent, identically distributed} random variables. Let $A_n$ be an event depending on $X_n$, for instance $A_n \coloneqq \{ X_n \geq K \}$ for some $K \in \mathbb{R}$, then all $P(A_n)$ are equal due to $X_n$ being identically distributed, i.e.

        \begin{align*}
            P(A_n) = p \quad \textrm{for } n \geq 1, p \in [0,1].
        \end{align*}

\end{observation}

\begin{observation}{}{Identical Distribution \& Infinite Sum}

    Let $(X_n)_{n=1}^{\infty}$ be a sequence of \emph{independent, identically distributed} random variables. Let $A_n$ be an event depending on $X_n$, for instance $A_n \coloneqq \{ X_n \geq K \}$ for some $K \in \mathbb{R}$, then

        \begin{align*}
            \sum_{n=1}^{\infty} P(A_n) < \infty \Rightarrow P(A_n) = 0 \textrm{ for } n \geq 1.
        \end{align*}

\end{observation}


\section{Definitions}
Note, in our previous two examples of rotational symmetrical groups, the notion of composition is quite important:
In both of our proofs that the tetrahedron group and the isocagonal cone group are different, we have harness this notion.
Naturally, we should have it in our definition of group.
\begin{definition}
    Let $X$ be a set. A binary operation $\cdot$ is a function $:X\times X\to X$.
\end{definition}
\begin{definition}
    A group $G$ is a triple $(G,\cdot,e)$, where $G$ is a nonempty set, $\cdot$ is a binary operation on $G$ and $e\in G$, that satisfies\\
    G1. (Law of Associativity) $\forall a,b,c\in G, (a\cdot b)\cdot c=a\cdot (b\cdot c)$.\\
    G2. (Identity) $\forall a\in G, a\cdot e=a$.\\
    G3. (Inverse) $\forall a\in G, \exists b\in G, a\cdot b=e$
\end{definition}
Most of the time we write $a\cdot b$ as $ab$.
\begin{theorem}
    Let $(G, \cdot, e)$ be a group, then\\
    1. $ab=e\implies ba=e$.\\
    2. $ea=a$.\\
    3. $ab=e\land ab^\prime=e\implies b=b^\prime$.\\
    4. $\exists a, ae^\prime=a\implies e^\prime=e$.
\end{theorem}
\begin{proof}
    1. Choose $c$ such that $(ba)c=e$, so $e=bac=beac=b(ab)ac=ba((ba)c)=bae=ba$.\\
    2. By part 1, we can choose $b$ such that $ab=ba=e$, so $ea=aba=ae=a$.\\
    3. $b=bab^\prime=eb^\prime$ (due to part 1) $=b^\prime$ (due to part 2).\\
    4. By part 1, we choose $b$ such that $ba=e$, so $e^\prime=ee^\prime$ (by part 2) $=bae^\prime=ba=e$.
\end{proof}
\begin{remark}
    There are a LOT of proofs to the preceding theorem.
    For example, the lecturer used a somewhat different proof for part 1 of the theorem.
    Therefore, you should try and come up with your own -- it's great fun.
\end{remark}
By part 3 and axiom G3, any $a$ in the group has a unique $b$ in the group such that $ab=ba=e$.
Then we write $a^{-1}=b$ for this element.
This is called the \textit{inverse} of $a$.
Note as well that $(a^{-1})^{-1}=a$.
In addition, $(ab)^{-1}=b^{-1}a^{-1}$.
\begin{definition}
    For any element $a\in G$, declare $a^0=e$.
    Say inductively that $a^n=a(a^{n-1})$ for a positive integer $n$.
    Similarly for a negative integer $n$, $a^n=(a^{-1})^{-n}$
\end{definition}
One can check that the usual laws of indices apply.
\begin{proposition}
    1. $a^na^m=a^{m+n}$.\\
    2. $(a^n)^m=a^{nm}$.
\end{proposition}
\begin{proof}
    Trivial.
\end{proof}
There are some `fake axioms' that we do not actually have to be stated.
For example, the definition of binary operation includes the axiom of closure.
However, we do have to verify the closure property to show that something is a group.


\subsection{Compact spaces}

A space \(X\) is compact if each open cover has a finite subcover.

If we can define a cover which does not have a finite subcover, then the space is not compact.

For example an infinite cover could be tend towards \((0,1)\), eg as \(\dfrac{1}{n},1-\dfrac{1}{n}\)

This covers \((0,1)\), but there is no finite subcover. As a result \((0,1)\) is not compact.


\section{Sparse Matrices and Solid Boolean Algebras}\label{boolean-algebras}
%=================================================

Manuscript~\cite{paoluzzi2019finite} shows that the set of join-irreducible atoms of the Boolean Algebra generated by a partition of $\E^3$ are one-to-one with the basis of 3-chain space also generated by the partition, and hence with the columns of the $[\partial_3]$ matrix, which provides a boundary representation (as 2-cycles) of the independent elements of 3-space.
An example of such space decomposition is shown in Figures~\ref{fig:image1} and~\ref{fig:image2}. The 3-cells are not in scale, and are suitably rotated to better exhibit their
complex structure, even containing internal holes. Their assembly gives the union of the five cubes. Each
\emph{3-cell is generated} as \emph{a column} of the \emph{sparse matrix} of
\emph{chain map} $\partial_3: C_3\to C_2$,  with values in $\{-1,0,1\}$.
They are the join-irreducible \emph{atoms} of the CSG algebra with closed regular cells.

\begin{figure}[htbp]
    \centering
    \begin{minipage}{0.475\textwidth}
        \centering
       \includegraphics[width=0.6\linewidth]{../figs/image1.png}
       \caption{A collection $\mathcal{S}$ of five random cubes in 3D Euclidean space.}
       \label{fig:image1}
    \end{minipage}\hfill
    \begin{minipage}{0.475\textwidth}
        \centering
       \includegraphics[width=0.8\linewidth]{../figs/image2.png}
        \caption{3-cells of the generated arrangement $\mathcal{A}(\mathcal{S})$ of $\E^3$.
        They are given by columns of $[\partial_3]$ as \emph{atoms} of a solid Boolean algebra.}
       \label{fig:image2}
    \end{minipage}
\end{figure}

To generate the $d$-space arrangement induced by a collection of cellular ($d$--1)-complexes, some numerical algebra and basic tools of linear algebra and algebraic topology are used. In particular, (sparse) matrices of operators and matrix multiplication and transposition. Interval-trees and $kd$-trees are also introduced for acceleration of clustering of face cells into subsets of congruent shape.

Using our approach with sparse matrices, the validity of topological computations is guaranteed, since these operator matrices must satisfy by construction the (graded) constraints: $[\partial_2][\partial_3]=[0]$ and $[\partial_1][\partial_2]=[0]$.  Similarly, we have $[\delta_1][\delta_0]=[0]$ and $[\delta_2][\delta_1]=[0]$.

With our approach based on boundary operators, the distinctions are removed between manifold and non-manifold representations (fairly standard in Solid Modeling), so allowing for mixing B-reps, cellular decompositions of elementary solids, and/or regular grids. This allows, e.g., for the generation of internal structures and mixed-dimensional objects needed in many applications, for example to make stronger and more resilient the ones to be produced via 3D printing.

Even more, the evaluation of CSG expressions of arbitrary complexity is done in a novel way, by combining the binary columns of 3D basis elements (i.e., the elements of the $U_3$ basis) with native Julia's operators for bitwise operations. In other words, once the $3$-space partition is generated, and $3$-cells are classified w.r.t.~all solids terms, via a single point-set containment test,  \emph{all CSG algebraic  expressions}---of any complexity---can be  evaluated simply by bitwise vectorized logical operations.
Finally, the sparse matrix approach can be extended to general dimensions and/or implemented on highly parallel computational engines, even using standard GPU computing kernels.


\subsection{Height of Semilattice:}
\begin{itemize}
    \item \textbf{Definition}: It is defined as the largest number of greater than($>$) relations that would fit in a descending order.
    \par It can also be defined as the longest path length in the semilattice diagram.
    \item \textbf{Set of values $V$ and a partial order} For eg. in a semilattice with power set of $n$ definitions as the set of values $V$ and set intersection($\cap$) as the meet operator there would be 2\textsuperscript{n} number of elements but the height of the semilattice would be $n$ as that is the maximum path length in the graph(as can be seen in the figure \ref{fig:height_semilattice}). As we go down on each edge the cardinality reduces by 1 and the longest path is from complete set to null set of length $n$.
    \begin{figure}
    \centering
    \includegraphics[width=1\linewidth]{images/semilattice_height.png}
    \caption{Semilattice height example}
    \label{fig:height_semilattice}
    \end{figure}
    \par Similarly, for DFA of reaching definitions the height of the semilattice is the number of definitions whereas the number of values is exponential in number of definitions.
    \item Now, There could be infinite height semilattices also having infinite no. of values(eg. set of integers with $max$ as the meet operator) but even with infinite no. of elements in the semilattice it’s height can still be finite, for eg. in case of constant propagation.
    \item Height of the semilattice is an important property because, one of the requirements for the fixed point iteration to converge is that the semilattice height is finite, if it’s infinite then the iteration is not guaranteed to converge.
\end{itemize}


\chapter{Directed graphical models (Bayes nets)}
\label{chap:DGM}

\section{Introduction}

\subsection{Chain rule}
\begin{equation}
p(x_{1:V}) = p(x_1)p(x_2|x_1)p(x_3|x_{1:2})\cdots p(x_N|x_{1:V-1})
\end{equation}

\subsection{Conditional independence}
$X$ and $Y$ are \textbf{conditionally independent} given $Z$, denoted $X \perp Y|Z$, iff the conditional joint can be written as a product of conditional marginals, i.e.
\begin{equation}
X \perp Y|Z \Longleftrightarrow p(X,Y|Z)=p(X|Z)p(Y|Z)
\end{equation}

first order \textbf{Markov assumption}: the future is independent of the past given the present,
\begin{equation}
x_{t+1} \perp x_{1:t-1}|x_t
\end{equation}

first-order \textbf{Markov chain}
\begin{equation}
p(x_{1:V}) = p(x_1)\prod\limits_{t=2}^V p(x_t|x_{t-1})
\end{equation}

\subsection{Graphical models}
A \textbf{graphical model}(GM) is a way to represent a joint distribution by making CI assumptions. In particular, the nodes in the graph represent random variables, and the (lack of) edges represent CI assumptions.

There are several kinds of graphical model, depending on whether the graph is directed, undirected, or some combination of directed and undirected. In this chapter, we just study directed graphs. We consider undirected graphs in Chapter 19.

\subsection{Directed graphical model}
A \textbf{directed graphical model}or \textbf{DGM} is a GM whose graph is a DAG. These are more commonly known as \textbf{Bayesian networks}. However, there is nothing inherently “Bayesian” about Bayesian networks: they are just a way of defining probability distributions. These models are also called \textbf{belief networks}. The term “belief” here refers to subjective probability. Once again, there is nothing inherently subjective about the kinds of probability distributions represented by DGMs.

\textbf{Ordered Markov property}
\begin{equation}
x_s \perp x_{\mathrm{pred}(s)\ \mathrm{pa}(s)} \perp x_{\mathrm{pa}(s)}
\end{equation}
where pa$(s)$ are the parents of nodes, and pred$(s)$ are the predecessors of nodes in the DAG.

\textbf{\textbf{Markov chain}} on a DGM
\begin{equation}
p(x_{1:V}|G)=\prod\limits_{t=1}^V p(x_t|x_{\mathrm{pa}(t)})
\end{equation}

\begin{figure}[hbtp]
\centering
    \includegraphics[scale=.50]{graphical-models.png}
\caption{(a) A simple DAG on 5 nodes, numbered in topological order. Node 1 is the root, nodes 4 and 5 are the leaves. (b) A simple undirected graph, with the following maximal cliques: {1,2,3}, {2,3,4}, {3,5}.}
\label{fig:graphical-models}
\end{figure}

\section{Examples}

\subsection{Naive Bayes classifiers}
\begin{figure}[hbtp]
\centering
    \includegraphics[scale=.60]{naive-bayes-as-DGM.png}
\caption{(a) A naive Bayes classifier represented as a DGM. We assume there are $D=4$ features, for simplicity. Shaded nodes are observed, unshaded nodes are hidden. (b) Tree-augmented naive Bayes classifier for $D=4$ features. In general, the tree topology can change depending on the value of $y$.}
\label{fig:naive-bayes-as-DGM}
\end{figure}

\subsection{Markov and hidden Markov models}
\begin{figure}[hbtp]
\centering
    \includegraphics[scale=.50]{second-order-Markov-chain.png}
\caption{A first and second order Markov chain.}
\label{fig:second-order-Markov-chain}
\end{figure}

\begin{figure}[hbtp]
\centering
    \includegraphics[scale=.60]{first-order-HMM.png}
\caption{A first-order HMM.}
\label{fig:first-order-HMM}
\end{figure}

\section{Inference}
Suppose we have a set of correlated random variables with joint distribution $p(\vec{x}_{1:V}|\vec{\theta})$. Let us partition this vector into the \textbf{visible variables} $\vec{x}_v$, which are observed, and the \textbf{hidden variables}, $\vec{x}_h$, which are unobserved. Inference refers to computing the posterior distribution of the unknowns given the knowns:
\begin{equation}
p(\vec{x}_h|\vec{x}_v,\theta) = \frac{p(\vec{x}_h, \vec{x}_v|\vec{\theta})}{p(\vec{x}_v|\vec{\theta})}=\frac{p(\vec{x}_h, \vec{x}_v|\vec{\theta})}{\sum_{\vec{x}_h'}p(\vec{x}_h', \vec{x}_v|\vec{\theta})}
\end{equation}

Sometimes only some of the hidden variables are of interest to us. So let us partition the hidden variables into \textbf{query variables}, $\vec{x}_q$, whose value we wish to know, and the remaining \textbf{nuisance variables}, $\vec{x}_n$, which we are not interested in. We can compute what we are interested in by \textbf{marginalizing out} the nuisance variables:
\begin{equation}
p(\vec{x}_q|\vec{x}_v,\vec{\theta}) = \sum_{\vec{x}_n}p(\vec{x}_q, \vec{x}_n|\vec{x}_v, \vec{\theta})
\end{equation}

\section{Learning}
MAP estimate:
\begin{equation}
\hat{\vec{\theta}} =\arg\max_{\vec{\theta}}\sum\limits_{i=1}^N \log p(\vec{x}_{i,v}|\vec{\theta})+\log p(\vec{\theta})
\end{equation}

\subsection{Learning from complete data}
If all the variables are fully observed in each case, so there is no missing data and there are no hidden variables, we say the data is \textbf{complete}. For a DGM with complete data, the likelihood is given by
\begin{equation}\begin{split}
p(\mathcal{D}|\vec{\theta}) & = \prod_{i=1}^N p(\vec{x}_i|\vec{\theta}) \\
& = \prod_{i=1}^N\prod_{t=1}^V p(\vec{x}_{it}|\vec{x}_{i, \mathrm{pa}(t)}, \vec{\theta}_t) \\
& = \prod_{t=1}^V p(\mathcal{D}_t|\vec{\theta}_t)
\end{split}\end{equation}
where $\mathcal{D}_t$ is the data associated with node $t$ and its parents, i.e., the $t$'th family.

Now suppose that the prior factorizes as well:
\begin{equation}
p(\vec{\theta})=\prod\limits_{t=1}^V p(\vec{\theta}_t)
\end{equation}

Then clearly the posterior also factorizes:
\begin{equation}
p(\vec{\theta}|\mathcal{D}) \propto p(\mathcal{D}|\vec{\theta})p(\vec{\theta}) = \prod\limits_{t=1}^V p(\mathcal{D}_t|\vec{\theta}_t)p(\vec{\theta}_t)
\end{equation}

\subsection{Learning with missing and/or latent variables}
If we have missing data and/or hidden variables, the likelihood no longer factorizes, and indeed it is no longer convex, as we explain in detail in Section TODO. This means we will usually can only compute a locally optimal ML or MAP estimate. Bayesian inference of the parameters is even harder. We discuss suitable approximate inference techniques in later chapters.

\section{Conditional independence properties of DGMs}

\subsection{d-separation and the Bayes Ball algorithm (global Markov properties)}
\begin{enumerate}
\item P contains a chain
\begin{equation}\begin{split}
p(x,z|y) & = \frac{p(x,y,z)}{p(y)}
= \frac{p(x)p(y|x)p(z|y)}{p(y)} \\
 & = \frac{p(x,y)p(z|y)}{p(y)} = p(x|y)p(z|y)
\end{split}\end{equation}

\item P contains a fork
\begin{equation}
p(x,z|y) = \frac{p(x,y,z)}{p(y)}
= \frac{p(y)p(x|y)p(z|y)}{p(y)}
= p(x|y)p(z|y)
\end{equation}
\item P contains v-structure
\begin{equation}
p(x,z|y) = \frac{p(x,y,z)}{p(y)}
= \frac{p(x)p(z)p(y|x,z)}{p(y)}
\neq p(x|y)p(z|y)
\end{equation}
\end{enumerate}

\subsection{Other Markov properties of DGMs}

\subsection{Markov blanket and full conditionals}

\begin{equation}
mb(t) = ch(t)\cup pa(t)\cup copa(t)
\end{equation}

\subsection{Multinoulli Learning}
Multinoulli Distribution：
\begin{equation}\label{eqn:discrite}
Cat(x|\mu) = \prod_{k=1}^K\mu_k^{x_k}
\end{equation}
then from \ref{bayes_net} and \ref{eqn:discrite}:
\begin{equation}
p(x|G,\theta) = \prod_{v=1}^V\prod_{c=1}^{C_v}\prod_{k=1}^K
\theta_{vck}^{y_{vck}}
\end{equation}
Likelihood
\begin{equation}
p(D|G,\theta) = \prod_{n=1}^N p(x_n|G,\theta)
=\prod_{n=1}^N\prod_{v=1}^V\prod_{c=1}^{C_{nv}}\prod_{k=1}^K
\theta_{vck}^{y_{nvck}}
\end{equation}
where $y_{nv} = f(pa(x_{nv}))$, f(x) is a map from x to a vector, there is only one element in the vector is 1.

\section{Influence (decision) diagrams *}


\chapter{Associative Memory Network}
Associative memory networks are single input, single output layer networks in which weights are determined in order to store a set of pattern associations. They are able to recall the desired response patterns when a given pattern is similar to the stored pattern.
$$\mathbf{{(x_1 y_1), (x_2 y_2), \ldots, (x_p y_p)}}$$
Association vectors are defined as:
$$\mathbf{x_k \rightarrow y_k}$$
\begin{description}
\item[Auto-associative] $\mathbf{x_k = y_k}$
\item[Hetero-associative] $\mathbf{x_k \ne y_k}$
\end{description}

\section{Linear Feed-forward Associative Memory network}
Linear Feed-forward Associative Memory (LAM) network is a single input, single output layer with linear activation function. \\
The synaptic input:
$$\mathbf{u=Wx}$$
The output activation:
$$\mathbf{y=\beta u}$$
\begin{center} where $\beta$ is the slope of the linear function\end{center}
\clearpage
\noindent LAM networks learn using the Hebbian rule. The weight matrix is \emph{given} by:
$$\mathbf{W_k = y_k x_k^{T}}$$
\begin{center} where $\mathbf{W_k}$ weight matrix learns the $k$ th pattern association\end{center}
The weight matrix to learn all the associations is \emph{given} by:
\begin{equation*}
\begin{split}
\mathbf{W} &= \sum_{k=1}^{P} \mathbf{W_k} \\
&= \sum_{k=1}^{P} \mathbf{y_k x_k^{T}}
\end{split}
\label{weight_lam}
\end{equation*}

\section{Cross-Talk versus Perfect Recall}
If inputs are \emph{uncorrelated or orthogonal} the Hebbian rule make \emph{perfect recall}. Otherwise, the output is corrupted or has \emph{cross-talk}. \\
Substituting \ref{weight_lam} to output, with $\mathbf{x_l}$ as input:
\begin{equation}
\begin{split}
\mathbf{y} &= \mathbf{Wx_l} \\
&= \Big(\sum_{k=1}^{P} \mathbf{y_k x_k^{T}} \Big) \mathbf{x_l} \\
&= \mathbf{y_l x_l^{T} x_l} + \sum_{k=1,k \ne l} \mathbf{y_k x_k^{T} x_l}
\end{split}
\end{equation}
If $\mathbf{x_l}$ and $\mathbf{x_k}$ are orthogonal:
$$\mathbf{x_l^{T} x_k} = 0$$
Then, perfect recall of the learned pattern:
$$\mathbf{y = y_l \| x_l \|^{2}}$$
$$\mathbf{y \propto y_l}$$
Otherwise, cross-talk:
$$\sum_{k=1,k \ne l} \mathbf{y_k x_k^{T} x_l}$$

\section{Problem with LAM Network}
Both auto and heterogeneous associative memory network do not function well in the presence of noise.
Let distorted input as:
$$\mathbf{x}_l^{i} = \mathbf{x}_l + \Delta_l$$
Then the output:
\begin{equation*}
\begin{split}
\mathbf{y} &= \mathbf{W} \mathbf{x}_l' \\
&= \mathbf{W} (\mathbf{x}_l + \Delta_l) \\
&= \Big(\sum_{k=1}^{P} \mathbf{y_k x_k^{T}}\Big) (\mathbf{x_l} + \Delta_l) \\
&= \mathbf{y_l x_l^{T} x_l + y_l x_l^{T} \Delta_l + \Big(\sum_{k \ne l}^{P} \mathbf{y_k x_k^{T} x_l}\Big) + \Big(\sum_{k \ne l}^{P} \mathbf{y_k x_k^{T} \Delta_l}\Big)}
\end{split}
\end{equation*}
Since orthogonal:
$$\mathbf{y} = \mathbf{y_l x_l^{T} x_l + y_l x_l^{T} \Delta_l +  \Big(\sum_{k \ne l}^{P} \mathbf{y_k x_k^{T} \Delta_l}\Big)}$$

\section{Recurrent/Hopfield AMN}
Suppressing noise component by thresholding the outputs and recycling of the output to the input. Weight is still based on Hebbian's learning rule, but $w_{ii} = 0$

\section{Bidirectional Associative Memory Network}
Bidirectional Associative Memory (BAM) network is a \emph{recurrent hetero-associative memory} of two layers of neurons. \\
Synaptic input to the output layer:
$$\mathbf{u = Wx}$$
The output:
$$
y_j =
\begin{cases}
1 & u_j > 0 \\
-1 & u_j \le 0
\end{cases}
$$
Synaptic input back to the input layer:
$$\mathbf{v = W^{T} y}$$
The input:
$$
x_j =
\begin{cases}
1 & v_i > 0 \\
-1 & v_i \le 0
\end{cases}
$$
Repeat until convergence


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\begin{document}

%--------------------------------------------------------------------------------
\section{First example}

The well known Pythagorean theorem \(x^2 + y^2 = z^2\) was
proved to be invalid for other exponents.
Meaning the next equation has no integer solutions:

\[ x^n + y^n = z^n \]

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

\section{Second example}

In physics, the mass-energy equivalence is stated by the equation $E=mc^2$, discovered in 1905 by Albert Einstein.

The mass-energy equivalence is described by the famous equation
\[ E=mc^2 \]
discovered in 1905 by Albert Einstein.
In natural units ($c$ = 1), the formula expresses the identity
\begin{equation}
E=m
\end{equation}

\section{Third example}

This is a simple math expression \(\sqrt{x^2+1}\) inside text.
And this is also the same:
\begin{math}
\sqrt{x^2+1}
\end{math}
but by using another command.

This is a simple math expression without numbering
\[\sqrt{x^2+1}\]
separated from text.

This is also the same:
\begin{displaymath}
\sqrt{x^2+1}
\end{displaymath}

\ldots and this:
\begin{equation*}
\sqrt{x^2+1}
\end{equation*}

    In physics, the mass-energy equivalence is stated
    by the equation $E=mc^2$, discovered in 1905 by Albert Einstein.

    The mass-energy equivalence is described by the famous equation
    \[ E=mc^2 \]
    discovered in 1905 by Albert Einstein.
    In natural units ($c = 1$), the formula expresses the identity
    \begin{equation}
        E=m
    \end{equation}

    Subscripts in math mode are written as $a_b$ and superscripts are written as $a^b$. These can be combined an nested to write expressions such as

    \[ T^{i_1 i_2 \dots i_p}_{j_1 j_2 \dots j_q} = T(x^{i_1},\dots,x^{i_p},e_{j_1},\dots,e_{j_q}) \]

    We write integrals using $\int$ and fractions using $\frac{a}{b}$. Limits are placed on integrals using superscripts and subscripts:

    \[ \int_0^1 \frac{dx}{e^x} =  \frac{e-1}{e} \]

    Lower case Greek letters are written as $\omega$ $\delta$ etc. while upper case Greek letters are written as $\Omega$ $\Delta$.

    Mathematical operators are prefixed with a backslash as $\sin(\beta)$, $\cos(\alpha)$, $\log(x)$ etc.

\end{document}


\section{Linear Support Vector Regression} \label{section:svr}

In the case of regression the goal is to predict a real-valued output for $y'$ so that our training data is of the form:

\begin{equation}
	\{(x_i,y_i), x\in\Re^m, y_i\in\Re, i=1, \dots, n\} \label{eq:svr_data}
\end{equation}

The regression SVM use a loss function that not allocating a penalty if the predicted value $y'_i$ is less than a distance $\epsilon$ away from the actual value $y_i$, i.e., if $|y_i-y'_i| \leq \epsilon$, where $y'_i = w^T x_i + b$. The region bound by $y'_i\pm\epsilon \ \forall_i$ is called an $\epsilon$-insensitive tube. The output variables which are outside the tube are given one of two slack variable penalties depending on whether they lie above, $\xi^+$, or below, $\xi^-$, the tube, provided $\xi^+ \geq 0$ and $\xi^- \geq 0 \ \forall_i$:

\begin{equation} \label{eq:svr_consts}
	\begin{aligned}
		& y_i\leq y'_i+\epsilon+\xi^+ \ \forall_i \\
    	& y_i\geq y'_i-\epsilon-\xi^- \ \forall_i \\
    	& \xi_i^+, \xi_i^- \geq 0 \ \forall_i
	\end{aligned}
\end{equation}

The objective function for SVR can then be written as:

\begin{equation} \label{eq:quad_svr_obj}
    \begin{aligned}
        \min_{w,b,\xi^+,\xi^-} \quad & \frac{1}{2} \| w \|^2 + C \sum_{i=1}^n (\xi_i^+ + \xi_i^-) \\
            \text{subject to} \quad & y_i - w^T x_i - b \leq \epsilon + \xi_i^+ \ \forall_i \\ & w^T x_i + b - y_i \leq \epsilon + \xi_i^- \ \forall_i \\ & \xi_i^+, \xi_i^- \geq 0 \ \forall_i
    \end{aligned}
\end{equation}

\begin{figure}[h!]
	\centering
  	\includegraphics[scale=0.6]{img/linear_dual_l1_svr_hyperplane}
  	\caption{Linear SVR hyperplane}
  	\label{fig:linear_dual_l1_svr_hyperplane}
\end{figure}

\pagebreak

\subsection{Epsilon-insensitive loss}

The \emph{epsilon-insensitive} loss is defined as:

\begin{equation} \label{eq:eps_loss1}
	\mathcal{L}_\epsilon^1 = \max(0, |y - (w^T x + b)| - \epsilon)
\end{equation}

or, equivalently:

\begin{equation} \label{eq:eps_loss2}
	\mathcal{L}_\epsilon^1 =
	\begin{cases}
		0 & \text{if} \ |y - (w^T x + b)| \leq \epsilon \\
		|y - (w^T x + b)| - \epsilon & \text{otherwise} \\
	\end{cases}
\end{equation}

As the \emph{hinge} loss, also the \emph{epsilon-insensitive} loss is a nondifferentiable convex function due to its nonsmoothness in $\pm\epsilon$, but has a subgradient that is given by:

% http://juliaml.github.io/LossFunctions.jl/stable/losses/distance/#L1EpsilonInsLoss-1

\begin{equation} \label{eq:eps_loss_der}
	\partial_w \mathcal{L}_\epsilon^1=
		\begin{cases}
            \displaystyle \frac{y - (w^T x + b)}{|y - (w^T x + b)|}x & \text{if} \ |y - (w^T x + b)| \geq \epsilon \\
            0 & \text{otherwise} \\
        \end{cases}
\end{equation}

\subsubsection{Primal formulation}

The general primal unconstrained formulation takes the same form of~\eqref{eq:primal_svm}.

The quadratic optimization problem~\eqref{eq:quad_svr_obj} can be equivalently formulated as:

\begin{equation} \label{eq:primal_l1_svr}
	\min_{w,b} \frac{1}{2} \| w \|^2 + C \sum_{i=1}^n \max(0, |y_i - (w^T x_i + b)| - \epsilon)
\end{equation}

where we make use of the \emph{epsilon-insensitive} loss~\eqref{eq:eps_loss1} or~\eqref{eq:eps_loss2}.

The above formulation penalizes slacks $\xi$ linearly and is called $\mathcal{L}_1$-SVR.

The $\mathcal{L}_1$-SVR objective~\eqref{eq:primal_l1_svr} can be rewritten in form~\eqref{eq:reg_bias_primal_svm1} or~\eqref{eq:reg_bias_primal_svm2} as:

\begin{equation} \label{eq:reg_bias_primal_l1_svr}
	\min_{w,b} \frac{1}{2} (\| w \|^2 + b^2) + C \sum_{i=1}^n \max(0, |y_i - (w^T x_i + b)| - \epsilon)
\end{equation}

\begin{figure}[h!]
	\centering
  	\includegraphics[scale=0.4]{img/l1_svr_loss}
  	\caption{Epsilon-insensitive loss with different optimization steps}
  	\label{fig:l1_svr_loss}
\end{figure}

\subsubsection{Wolfe dual formulation}

To reformulate the~\eqref{eq:quad_svr_obj} as a \emph{Wolfe dual}, we introduce the Lagrange multipliers $\alpha_i^+, \alpha_i^-, \mu_i^+, \mu_i^- \geq 0 \ \forall_i$:

\begin{equation} \label{eq:svr_wolfe_dual}
	\begin{aligned}
    	\max_{\alpha^+,\alpha^-,\mu^+,\mu^-} \min_{w,b,\xi^+,\xi^-} \mathcal{W}(w,b,\xi^+,\xi^-,\alpha^+,\alpha^-,\mu^+,\mu^-) = \frac{1}{2} \| w \|^2 + C \sum_{i=1}^n (\xi_i^+ + \xi_i^-)-\sum_{i=1}^n (\mu_i^+ \xi_i^+ + \mu_i^- \xi_i^-) \\ -\sum_{i=1}^n \alpha_i^+(\epsilon+\xi_i^+ + y'_i-y_i)-\sum_{i=1}^n \alpha_i^-(\epsilon+\xi_i^- - y'_i+y_i)
	\end{aligned}
\end{equation}

Substituting for $y_i$, differentiating wrt $w, b, \xi^+$, $\xi^-$ and setting the derivatives to $0$ gives:

\begin{equation} \label{eq:svr_wolfe_der_w}
	\frac{\partial \mathcal{W}}{\partial w}=w-\sum_{i=1}^n (\alpha_i^+ - \alpha_i^-) x_i \Rightarrow w=\sum_{i=1}^n (\alpha_i^+ - \alpha_i^-) x_i
\end{equation}

\begin{equation} \label{eq:svr_wolfe_der_b}
	\frac{\partial \mathcal{W}}{\partial b}=-\sum_{i=1}^n (\alpha_i^+ - \alpha_i^-)\Rightarrow \sum_{i=1}^n (\alpha_i^+ - \alpha_i^-)=0
\end{equation}

\begin{equation}\label{eq:svr_wolfe_der_xip}
	\frac{\partial \mathcal{W}}{\partial\xi_i^+}=0\Rightarrow C=\alpha_i^+ + \mu_i^+
\end{equation}

\begin{equation} \label{eq:svr_wolfe_der_xim}
	\frac{\partial \mathcal{W}}{\partial\xi_i^-}=0\Rightarrow C=\alpha_i^- + \mu_i^-
\end{equation}

Substituting~\eqref{eq:svr_wolfe_der_w} and~\eqref{eq:svr_wolfe_der_b} in, we now need to maximize $\mathcal{W}$ wrt $\alpha_i^+$ and $\alpha_i^-$, where $\alpha_i^+ \geq 0,\ \alpha_i^- \geq 0 \ \forall_i$:

\begin{equation} \label{eq:svr_max_wolfe_dual}
    \max_{\alpha^+,\alpha^-} \mathcal{W}(\alpha^+,\alpha^-) = \sum_{i=1}^n y_i(\alpha_i^+ - \alpha_i^-)-\epsilon\sum_{i=1}^n (\alpha_i^+ + \alpha_i^-)-\frac{1}{2}\sum_{i,j}(\alpha_i^+ - \alpha_i^-)\langle x_i, x_j \rangle(\alpha_j ^+ - \alpha_j ^-)
\end{equation}

Using $\mu_i^+ \geq 0$ and $\mu_i^- \geq 0$ together with~\eqref{eq:svr_wolfe_der_w} and~\eqref{eq:svr_wolfe_der_b} means that $\alpha_i^+ \leq C$ and $\alpha_i^- \leq C$. We therefore need to find:

\begin{equation} \label{eq:svr_min_wolfe_dual}
    \begin{aligned}
        \min_{\alpha^+,\alpha^-} \quad & \frac{1}{2}(\alpha^+ - \alpha^-)^TK(\alpha^+ - \alpha^-)+\epsilon e^T(\alpha^+ + \alpha^-)-y^T(\alpha^+ - \alpha^-) \\
            \text{subject to} \quad & 0\leq\alpha_i^+,\alpha_i^- \leq C \ \forall_i \\ & e^T(\alpha^+ - \alpha^-)=0
    \end{aligned}
\end{equation}

where $e^T = [1, \dots, 1]$.

We can write the~\eqref{eq:svr_min_wolfe_dual} in a standard quadratic form as:

\begin{equation}
    \begin{aligned} \label{eq:svr_min_qp_wolfe_dual}
        \min_{\alpha} \quad & \frac{1}{2}\alpha^T Q\alpha-q^T\alpha \\
            \text{subject to} \quad & 0\leq\alpha_i\leq C \ \forall_i \\ & e^T\alpha=0
    \end{aligned}
\end{equation}

where the Hessian matrix $Q =
\begin{bmatrix}
K & -K\\
-K & K
\end{bmatrix}$
, $\alpha =
\begin{bmatrix}
\alpha^+\\
\alpha^-
\end{bmatrix}$
, $q =
\begin{bmatrix}
-y\\
y
\end{bmatrix} + \epsilon$
, and $e =
\begin{bmatrix}
1\\
-1
\end{bmatrix}$.

Each new predictions $y'$ can be found using:

\begin{equation} \label{eq:svr_pred}
    y'= \sum_{i=1}^n (\alpha_i^+ - \alpha_i^-)\langle x_i, x' \rangle+b
\end{equation}

A set $S$ of support vectors $x_s$ can be created by finding the indices $i$ where $0\leq\alpha\leq C$ and $\xi_i^+=0$ or $\xi_i^-=0$.

This gives us:

\begin{equation} \label{eq:svr_b}
    b=y_s-\epsilon-\sum_{m\in S}(\alpha_m^+ -\alpha_m^-) \langle x_m, x_s \rangle
\end{equation}

As before it is better to average over all the indices $i$ in $S$:

\begin{equation} \label{eq:svr_b_avg}
    b=\frac{1}{N_s}\sum_{s\in S}y_s-\epsilon-\sum_{m \in S}(\alpha_m^+ - \alpha_m^-)\langle x_m, x_s \rangle
\end{equation}

From~\eqref{eq:svr_min_qp_wolfe_dual} we can notice that the equality constraint $e^T \alpha = 0$ arises form the stationarity condition $\partial_{{b}} \mathcal{W}=0$. So, again, for simplicity, we can again consider the bias term $b$ embedded into the weight vector. We report below the box-constrained dual formulation~\cite{hsu2002simple} that arises from the primal~\eqref{eq:reg_bias_primal_svm1} or~\eqref{eq:reg_bias_primal_svm2} where the bias term $b$ is embedded into the weight vector $w$:

\begin{equation} \label{eq:svr_min_bcqp_wolf_dual}
    \begin{aligned}
        \min_{\alpha} \quad & \frac{1}{2} \alpha^T (Q + ee^T)\alpha+q^T\alpha \\
            \text{subject to} \quad & 0\leq\alpha_i\leq C \ \forall_i
    \end{aligned}
\end{equation}

\subsubsection{Lagrangian dual formulation}

In order to relax the constraints in the \emph{Wolfe dual} formulation~\eqref{eq:svr_min_wolfe_dual} we define the problem as a \emph{Lagrangian dual} relaxation by embedding them into objective function, so we need to allocate the Lagrange multipliers $\mu$ and $\lambda_+, \lambda_- \geq 0$:

\begin{equation} \label{eq:l1_svr_lagrangian_dual}
	\begin{aligned}
		    \max_{\mu,\lambda_+,\lambda_-} \min_{\alpha} \mathcal{L}(\alpha,\mu,\lambda_+,\lambda_-) &= \frac{1}{2} \alpha^T Q\alpha+q^T\alpha + \mu^T (e^T \alpha) + \lambda_+^T (ub - \alpha) - \lambda_-^T \alpha \\
    &= \frac{1}{2} \alpha^T Q\alpha + (q + \mu e^T + \lambda_+ - \lambda_-)^T \alpha + \lambda_+^T ub \\
    \text{subject to} \quad & \,\, \lambda_+, \lambda_- \geq 0
	\end{aligned}
\end{equation}

where the upper bound $ub^T = [C, \dots, C]$.

Taking the derivative of the Lagrangian $\mathcal{L}$ wrt $\alpha$ and settings it to 0 gives:

\begin{equation} \label{eq:svr_lagrangian_der_a}
	\frac{\partial \mathcal{L}}{\partial \alpha}=0\Rightarrow Q \alpha + (q + \mu e^T + \lambda_+ - \lambda_-) = 0
\end{equation}

With $\alpha$ optimal solution of the linear system:

\begin{equation} \label{eq:l1_svr_lagrangian_sol}
    Q \alpha = - (q + \mu e^T + \lambda_+ - \lambda_-)
\end{equation}

the gradients wrt $\mu$, $\lambda_+$ and $\lambda_-$ are:

\begin{equation} \label{eq:svr_lagrangian_der_mu}
	\frac{\partial \mathcal{L}}{\partial \mu}=-e \alpha
\end{equation}

\begin{equation} \label{eq:svr_lagrangian_der_lp}
	\frac{\partial \mathcal{L}}{\partial \lambda_+}=\alpha - ub
\end{equation}

\begin{equation} \label{eq:svr_lagrangian_der_lm}
    \frac{\partial \mathcal{L}}{\partial \lambda_-}=-\alpha
\end{equation}

From~\eqref{eq:svr_min_qp_wolfe_dual} we can notice that the equality constraint $e^T \alpha = 0$ arises form the stationarity condition $\partial_{{b}} \mathcal{W}=0$. So, again, for simplicity, we can again consider the bias term $b$ embedded into the weight vector. In this way the dimensionality of~\eqref{eq:l1_svr_lagrangian_dual} is reduced by removing the multipliers $\mu$ which was allocated to control the equality constraint $e^T \alpha=0$, so we will end up solving exactly the problem~\eqref{eq:svr_min_bcqp_wolf_dual}.

\begin{equation} \label{eq:l1_svr_bcqp_lagrangian_dual}
	\begin{aligned}
    	\max_{\lambda_+,\lambda_-} \min_{\alpha} \mathcal{L}(\alpha,\lambda_+,\lambda_-) &= \frac{1}{2} \alpha^T (Q + ee^T)\alpha+q^T\alpha + \lambda_+^T (ub - \alpha) - \lambda_-^T \alpha \\
    &= \frac{1}{2} \alpha^T (Q + ee^T)\alpha + (q + \lambda_+ - \lambda_-)^T \alpha + \lambda_+^T ub \\
    \text{subject to} \quad & \,\, \lambda_+, \lambda_- \geq 0
	\end{aligned}
\end{equation}

where, again, the upper bound $ub^T = [C, \dots, C]$.

Now, taking the derivative of the Lagrangian $\mathcal{L}$ wrt $\alpha$ and settings it to 0 gives:

\begin{equation} \label{eq:l1_svr_bcqp_lagrangian_der_a}
	\frac{\partial \mathcal{L}}{\partial \alpha}=0\Rightarrow (Q + ee^T) \alpha + (q + \lambda_+ - \lambda_-) = 0
\end{equation}

With $\alpha$ optimal solution of the linear system:

\begin{equation} \label{eq:l1_svr_bcqp_lagrangian_sol}
    (Q + ee^T) \alpha = - (q + \lambda_+ - \lambda_-)
\end{equation}

the gradients wrt $\lambda_+$ and $\lambda_-$ are:

\begin{equation} \label{eq:l1_svr_bcqp_lagrangian_der_lp}
	\frac{\partial \mathcal{L}}{\partial \lambda_+}=\alpha - ub
\end{equation}

\begin{equation} \label{eq:l1_svr_bcqp_lagrangian_der_lm}
    \frac{\partial \mathcal{L}}{\partial \lambda_-}=-\alpha
\end{equation}

\bigskip

Note that since the Hessian matrix $Q$ of the $\mathcal{L}_1$-SVR is not strictly positive definite, i.e., the Lagrangian function is not strictly convex since it will be linear along the eigenvectors correspondent to the null eigenvalues and so it will be unbounded below, the Lagrangian dual relaxation, i.e.,~\ref{eq:l1_svr_lagrangian_sol} and~\ref{eq:l1_svr_bcqp_lagrangian_sol}, will be nondifferentiable, so it will have infinite solutions and for each of them it will have a different subgradient. In order to compute an approximation of the gradient, we will choose $\alpha$ in such a way as the one that minimizes the 2-norm since it is good almost like the gradient:

\begin{equation} \label{eq:svr_lagrangian_krylov_sol}
	\min_{\alpha_n \in K_n(Q, b)} \| Q \alpha_n - b \|
\end{equation}

Since we are dealing with a symmetric system we will choose a well-known Krylov method that performs the Lanczos iterate, i.e., symmetric Arnoldi iterate, called \emph{minres}, i.e., symmetric \emph{gmres}, to compute the vector $\alpha_n$ that minimizes the norm of the residual $r_n = Q \alpha_n - b$ among all vectors in $K_n(Q, b) = span(b, Qb, Q^2b, \dots, Q^{n-1}b)$.

\bigskip

Since the linear algebra methods in the ML context are crucial and also in order to deal with a per-iteration cost equals to the other algorithms described later to provide a coherent comparison of all at the end, we will solve it with a primal-dual optimization method and we modify its definition by adding a strictly convex augmentation term, i.e., a penalty term, in order to improve the actual convergence of the algorithms. So, if we consider a general quadratic optimization problem subject to linear constraints, i.e., equality and inequality constraints, defined as:

\begin{equation}
    \begin{aligned}
        \min_{\alpha} \quad & \frac{1}{2} \alpha^T Q \alpha + q^T \alpha \\
            \textrm{subject to} \quad & A \alpha = b \\ & G \alpha \leq h \\ & lb \leq \alpha \leq ub
    \end{aligned}
\end{equation}

or, equivalently:

\begin{equation}
    \begin{aligned}
        \min_{\alpha} \quad & \frac{1}{2} \alpha^T Q \alpha + q^T \alpha \\
            \textrm{subject to} \quad & A \alpha = b \\ & \hat{G} \alpha \leq \hat{h}
    \end{aligned}
\end{equation}

where $\hat{G} =
\begin{bmatrix}
 G \\
-I \\
 I
\end{bmatrix}$ and $\hat{h} =
\begin{bmatrix}
h & -lb & ub
\end{bmatrix}$; we give the following \emph{augmented Lagrangian dual}:

\begin{equation} \label{eq:l1_svr_gen_aug_lagrangian_dual}
	\begin{aligned}
		    \max_{\mu,\lambda} \min_{\alpha} \quad & \frac{1}{2} \alpha^T Q \alpha + q^T \alpha + \mu^T (A \alpha - b) + \lambda^T (\hat{G} \alpha - \hat{h}) + \frac{\rho}{2} \| A \alpha - b \|^2 + \frac{\rho}{2} \| \hat{G} \alpha - \hat{h} \|^2 \\
    \text{subject to} \quad & \lambda \geq 0
	\end{aligned}
\end{equation}

with $\rho > 0$.

\bigskip

According to this definition, we change the formulation~\ref{eq:l1_svr_lagrangian_dual} as:

\begin{equation} \label{eq:l1_svr_aug_lagrangian_dual}
	\begin{aligned}
		    \max_{\mu,\lambda} \min_{\alpha} \mathcal{L}(\alpha,\mu,\lambda) &= \frac{1}{2} \alpha^T Q\alpha+q^T\alpha + \mu^T (e^T \alpha) + \lambda^T (\hat{G} \alpha - \hat{h}) + \frac{\rho}{2} \| e^T \alpha \|^2 + \frac{\rho}{2} \| \hat{G} \alpha - \hat{h} \|^2 \\
    \text{subject to} \quad & \,\, \lambda \geq 0
	\end{aligned}
\end{equation}

and the formulation~\ref{eq:l1_svr_bcqp_lagrangian_dual} as:

\begin{equation} \label{eq:l1_svr_bcqp_aug_lagrangian_dual}
	\begin{aligned}
    	\max_{\lambda} \min_{\alpha} \mathcal{L}(\alpha,\lambda) &= \frac{1}{2} \alpha^T (Q + ee^T) \alpha + q^T \alpha + \lambda^T (\hat{G} \alpha - \hat{h}) + \frac{\rho}{2} \| \hat{G} \alpha - \hat{h} \|^2 \\
    \text{subject to} \quad & \,\, \lambda \geq 0
	\end{aligned}
\end{equation}

where $\hat{G} =
\begin{bmatrix}
-I \\
 I
\end{bmatrix}$ and $\hat{h} =
\begin{bmatrix}
-lb & ub
\end{bmatrix}$ with $lb^T = [0, \dots, 0]$, $ub^T = [C, \dots, C]$ and $\rho > 0$.

\pagebreak

\subsection{Squared epsilon-insensitive loss}

The \emph{squared epsilon-insensitive} loss is defined as:

\begin{equation} \label{eq:squared_eps_loss1}
	\mathcal{L}_\epsilon^2 = \max(0, |y - (w^T x + b)| - \epsilon)^2
\end{equation}

or, equivalently:

\begin{equation} \label{eq:squared_eps_loss2}
	\mathcal{L}_\epsilon^2 =
	\begin{cases}
		0 & \text{if} \ |y - (w^T x + b)| \leq \epsilon \\
		(|y - (w^T x + b)| - \epsilon)^2 & \text{otherwise} \\
	\end{cases}
\end{equation}

As the \emph{squared hinge} loss, also the \emph{squared epsilon-insensitive} loss is a strictly convex function and its gradient is given by:

% http://juliaml.github.io/LossFunctions.jl/stable/losses/distance/#L2EpsilonInsLoss-1

\begin{equation} \label{eq:squared_eps_loss_der}
	\nabla_w \mathcal{L}_\epsilon^2=
		\begin{cases}
            \displaystyle 2 \operatorname{sign}(y - (w^T x + b)) (|y - (w^T x + b)| - \epsilon) x & \text{if} \ |y - (w^T x + b)| \geq \epsilon \\
            0 & \text{otherwise} \\
        \end{cases}
\end{equation}

\subsubsection{Primal formulation}

To provide a continuously differentiable function the optimization problem~\eqref{eq:primal_l1_svr} can be formulated as:

\begin{equation} \label{eq:primal_l2_svr}
    \min_{w,b} \frac{1}{2} \| w \|^2 + C \sum_{i=1}^n \max(0, |y_i - (w^T x_i + b)| - \epsilon)^2
\end{equation}

where we make use of the \emph{squared epsilon-insensitive} loss that quadratically penalized slacks $\xi$ and is called $\mathcal{L}_2$-SVR.

The $\mathcal{L}_2$-SVR objective~\eqref{eq:primal_l2_svr} can be rewritten in form~\eqref{eq:reg_bias_primal_svm1} or~\eqref{eq:reg_bias_primal_svm2} as:

\begin{equation} \label{eq:reg_bias_primal_l2_svr}
	\min_{w,b} \frac{1}{2} (\| w \|^2 + b^2) + C \sum_{i=1}^n \max(0, |y_i - (w^T x_i + b)| - \epsilon)^2
\end{equation}

\begin{figure}[h!]
	\centering
  	\includegraphics[scale=0.4]{img/l2_svr_loss}
  	\caption{Squared epsilon-insensitive loss with different optimization steps}
  	\label{fig:l2_svr_loss}
\end{figure}

\subsubsection{Wolfe dual formulation}

As done for the $\mathcal{L}_1$-SVR we can derive the \emph{Wolfe dual} formulation of the $\mathcal{L}_2$-SVR by obtaining:

\begin{equation} \label{eq:wolfe_dual_l2_svr}
    \begin{aligned}
        \min_{\alpha} \quad & \frac{1}{2} \alpha^T (Q + D) \alpha + q^T \alpha \\
            \text{subject to} \quad & \alpha_i \geq 0 \ \forall_i \\ & e^T \alpha=0
    \end{aligned}
\end{equation}

or, alternatively, with the regularized bias term by obtaining:

\begin{equation} \label{eq:reg_bias_wolfe_dual_l2_svr}
    \begin{aligned}
        \min_{\alpha} \quad & \frac{1}{2}\alpha^T (Q + ee^T + D) \alpha + q^T \alpha \\
            \text{subject to} \quad & \alpha_i \geq 0 \ \forall_i
    \end{aligned}
\end{equation}

where the diagonal matrix $\displaystyle D_{ii} = \frac{1}{2C} \ \forall_i$.

\subsubsection{Lagrangian dual formulation}

In order to relax the constraints in the $\mathcal{L}_2$-SVR \emph{Wolfe dual} formulation~\eqref{eq:wolfe_dual_l2_svr} we define the problem as a \emph{Lagrangian dual} relaxation by embedding them into objective function, so we need to allocate the Lagrange multipliers $\mu$ and $\lambda \geq 0$:

\begin{equation} \label{eq:l2_svr_lagrangian_dual}
	\begin{aligned}
		    \max_{\mu,\lambda} \min_{\alpha} \mathcal{L}(\alpha,\mu,\lambda) &= \frac{1}{2} \alpha^T (Q+D)\alpha+q^T\alpha + \mu^T (e^T \alpha) - \lambda^T \alpha \\
    &= \frac{1}{2} \alpha^T (Q+D)\alpha + (q + \mu e^T - \lambda)^T \alpha \\
    \text{subject to} \quad & \,\, \lambda \geq 0
	\end{aligned}
\end{equation}

Taking the derivative of the Lagrangian $\mathcal{L}$ wrt $\alpha$ and settings it to 0 gives:

\begin{equation} \label{eq:l2_svr_lagrangian_der_a}
	\frac{\partial \mathcal{L}}{\partial \alpha}=0\Rightarrow (Q+D) \alpha + (q + \mu e^T - \lambda) = 0
\end{equation}

With $\alpha$ optimal solution of the linear system:

\begin{equation} \label{eq:l2_svr_lagrangian_sol}
    (Q+D) \alpha = - (q + \mu e^T - \lambda)
\end{equation}

the gradients wrt $\mu$ and $\lambda$ are:

\begin{equation} \label{eq:l2_svr_lagrangian_der_mu}
	\frac{\partial \mathcal{L}}{\partial \mu}=-e \alpha
\end{equation}

\begin{equation} \label{eq:l2_svr_lagrangian_der_lalbda}
    \frac{\partial \mathcal{L}}{\partial \lambda}=-\alpha
\end{equation}

From~\eqref{eq:svr_min_qp_wolfe_dual} we can notice that the equality constraint $e^T \alpha = 0$ arises form the stationarity condition $\partial_{{b}} \mathcal{W}=0$. So, again, for simplicity, we can again consider the bias term $b$ embedded into the weight vector. In this way the dimensionality of~\eqref{eq:l2_svr_lagrangian_dual} is reduced by removing the multipliers $\mu$ which was allocated to control the equality constraint $e^T \alpha=0$, so we will end up solving exactly the problem~\eqref{eq:reg_bias_wolfe_dual_l2_svr}.

\begin{equation} \label{eq:l2_svr_lb_lagrangian_dual}
	\begin{aligned}
    	\max_{\lambda} \min_{\alpha} \mathcal{L}(\alpha,\lambda) &= \frac{1}{2} \alpha^T (Q + ee^T + D) \alpha+q^T\alpha - \lambda^T \alpha \\
    &= \frac{1}{2} \alpha^T (Q + ee^T + D)\alpha + (q - \lambda)^T \alpha \\
    \text{subject to} \quad & \,\, \lambda \geq 0
	\end{aligned}
\end{equation}

Now, taking the derivative of the Lagrangian $\mathcal{L}$ wrt $\alpha$ and settings it to 0 gives:

\begin{equation} \label{eq:l2_svr_lb_lagrangian_der_a}
	\frac{\partial \mathcal{L}}{\partial \alpha}=0\Rightarrow (Q + ee^T + D) \alpha + (q - \lambda) = 0
\end{equation}

With $\alpha$ optimal solution of the linear system:

\begin{equation} \label{eq:l2_svr_lb_lagrangian_sol}
    (Q + ee^T + D) \alpha = - (q - \lambda)
\end{equation}

the gradient wrt $\lambda$ is:

\begin{equation} \label{eq:l2_svr_lb_lagrangian_der_l}
    \frac{\partial \mathcal{L}}{\partial \lambda}=-\alpha
\end{equation}

\bigskip

Note that since the Hessian matrix $Q$ of the $\mathcal{L}_2$-SVR is symmetric and strictly positive definite, we can find the unique solution of the Lagrangian dual relaxation, i.e.,~\ref{eq:l2_svr_lagrangian_sol} and~\ref{eq:l2_svr_lb_lagrangian_sol}, by solving the system with the Cholesky factorization.

\bigskip

Since the linear algebra methods in the ML context are crucial and also in order to deal with a per-iteration cost equals to the other algorithms described later to provide a coherent comparison of all at the end, we will solve it with a primal-dual optimization method and we modify its definition by adding a strictly convex augmentation term, i.e., a penalty term, in order to improve the actual convergence of the algorithms. So, if we consider a general quadratic optimization problem subject to linear constraints, i.e., equality and inequality constraints, defined as:

\begin{equation}
    \begin{aligned}
        \min_{\alpha} \quad & \frac{1}{2} \alpha^T Q \alpha + q^T \alpha \\
            \textrm{subject to} \quad & A \alpha = b \\ & G \alpha \leq h \\ & lb \leq \alpha \leq ub
    \end{aligned}
\end{equation}

or, equivalently:

\begin{equation}
    \begin{aligned}
        \min_{\alpha} \quad & \frac{1}{2} \alpha^T Q \alpha + q^T \alpha \\
            \textrm{subject to} \quad & A \alpha = b \\ & \hat{G} \alpha \leq \hat{h}
    \end{aligned}
\end{equation}

where $\hat{G} =
\begin{bmatrix}
 G \\
-I \\
 I
\end{bmatrix}$ and $\hat{h} =
\begin{bmatrix}
h & -lb & ub
\end{bmatrix}$; we give the following \emph{augmented Lagrangian dual}:

\begin{equation} \label{eq:l2_svr_gen_aug_lagrangian_dual}
	\begin{aligned}
		    \max_{\mu,\lambda} \min_{\alpha} \quad & \frac{1}{2} \alpha^T Q \alpha + q^T \alpha + \mu^T (A \alpha - b) + \lambda^T (\hat{G} \alpha - \hat{h}) + \frac{\rho}{2} \| A \alpha - b \|^2 + \frac{\rho}{2} \| \hat{G} \alpha - \hat{h} \|^2 \\
    \text{subject to} \quad & \lambda \geq 0
	\end{aligned}
\end{equation}

with $\rho > 0$.

\bigskip

According to this definition, we change the formulation~\ref{eq:l2_svr_lagrangian_dual} as:

\begin{equation} \label{eq:l2_svr_aug_lagrangian_dual}
	\begin{aligned}
		    \max_{\mu,\lambda} \min_{\alpha} \mathcal{L}(\alpha,\mu,\lambda) &= \frac{1}{2} \alpha^T (Q + D) \alpha+q^T\alpha + \mu^T (e^T \alpha) + \lambda^T (\hat{G} \alpha - \hat{h}) + \frac{\rho}{2} \| e^T \alpha \|^2 + \frac{\rho}{2} \| \hat{G} \alpha - \hat{h} \|^2 \\
    \text{subject to} \quad & \,\, \lambda \geq 0
	\end{aligned}
\end{equation}

and the formulation~\ref{eq:l2_svr_lb_lagrangian_dual} as:

\begin{equation} \label{eq:l2_svr_lb_aug_lagrangian_dual}
	\begin{aligned}
    	\max_{\lambda} \min_{\alpha} \mathcal{L}(\alpha,\lambda) &= \frac{1}{2} \alpha^T (Q + ee^T + D) \alpha + q^T \alpha + \lambda^T (\hat{G} \alpha - \hat{h}) + \frac{\rho}{2} \| \hat{G} \alpha - \hat{h} \|^2 \\
    \text{subject to} \quad & \,\, \lambda \geq 0
	\end{aligned}
\end{equation}

where $\hat{G} =
\begin{bmatrix}
-I
\end{bmatrix}$ and $\hat{h} =
\begin{bmatrix}
-lb
\end{bmatrix}$ with $lb^T = [0, \dots, 0]$ and $\rho > 0$.


\subsection{Membership relation}

Say we have a preterite \(P(x)\) which is true for some values of \(x\). Sets allow us to explore the properties of these values.

We may want to talk about a collection of terms for which \(P(x)\) is true, which we call a set.

To do this we need to introduce new axioms, however first we can add (conservative) definitions to help us do this.

We introduce a new relation: membership. If element \(x\) is in set \(s\) then the following relation is true, otherwise it is false:

\(x\in s\)

Sets are also terms. In first-order logic they will be included in quantifiers. Indeed, in set theory, we aim to treat everything as sets.

If a term is not a member of another term, we can write this using the non-member relation as follows:

\(\forall x \forall s [\neg (x\in s)\leftrightarrow x\not\in s]\)


\documentclass[a4paper]{scrartcl}
\usepackage{amsmath}

\title{Important ML formulas}
\date{}

\begin{document}
\maketitle

\section{Gaussians}

Definition:

\begin{align}
    \mathcal{N}(x|\mu, C) &= \frac{1}{|C| (2 \pi)^{d/2}} \exp\left[ -\frac{1}{2} (x - \mu)^T C^{-1} (c - \mu) \right]
\end{align}

Marginals and conditionals (see Bishop, (2.113) - (2.117)):

\begin{align}
    p(x) &= \mathcal{N}(x|\mu, \Lambda^{-1}) \\
    p(y|x) &= \mathcal{N}(y|A x + b, L^{-1}) \\
    p(y) &= \int \mathrm{d}x\; p(y|x)p(x)
        = \mathcal{N}(y|A\mu + b, L^{-1} + A \Lambda^{-1} A^T) \\
    p(x|y) &= \frac{p(y|x)p(x)}{p(y)}
        = \mathcal{N}(y|\Sigma \left\{ A^T L (y - b) + \Lambda \mu \right\}, \Sigma) \\
    \Sigma &= \left( \Lambda  + A^T L A \right)^{-1}
\end{align}

Conditioning on blocks

\begin{align}
    p(x) &= \mathcal{N}(x|\mu, C) \\
    C &= \begin{pmatrix}
        C_N & k \\
        k^T & c
    \end{pmatrix} \\
    x &= (x_N, x_{N+1})^T \\
    \mu &= (\mu_N, \mu_{N+1})^T
\end{align}

Then:

\begin{align}
    p(x_{N+1}|x_{N}) &=
        \mathcal{N}\left( x_{N+1} | \tilde{\mu}_{N+1}, \tilde{C}_{N+1} \right) \\
    \tilde{\mu}_{N+1} &= \mu_{N+1} + k^T C_N^{-1} (x_N - \mu_N) \\
    \tilde{C}_{N+1} &= c - k^T C_N^{-1} k
\end{align}

\section{Laplace approximation}
\begin{align}
    \log p(x)
        &\approx \log p(x^\star) + \frac{1}{2} \left(x - x^\star \right)^T H \left(x - x^\star\right) \\
        &= \mathcal{N}(x|x^\star, H^{-1}) \\
    &\left. \frac{\partial}{\partial x} \log p(x) \right|_{x^\star} = 0 \\
    &H_{ij} = \frac{\partial^2}{\partial x_i \partial x_j} \log p(x)
\end{align}

\section{Sigmoids}
\begin{align}
    \sigma(x) &= \frac{1}{1 - \exp(-x)} \\
    \sigma(-x) &= 1 - \sigma(x)  \\
    \log \frac{\sigma(x)}{1 - \sigma(x)} &= x \\
    \frac{\partial}{\partial x}\sigma(x) &= \sigma(x) (1 - \sigma(x))
\end{align}

\section{Block Matrix Inverse}
\begin{align}
    \begin{bmatrix}
            A_{00} & A_{01} \\
            A_{10} & A_{11}
    \end{bmatrix}^{-1}
    &= \begin{bmatrix}
        A_{00}^{-1} + F E^{-1} G & -F E^{-1} \\
        -E^{-1} G & E^{-1}
    \end{bmatrix} \\
    E &= A_{11} - A_{10} A_{00}^{-1} A_{01} \\
    F &= A_{00}^{-1}A_{01} \\
    G &= A_{10} A_{00}^{-1}
\end{align}

\end{document}


\chapter{Algebraic Approach}
	In this chapter we present some different approaches to the construction of $\Zp$ and $\Qp$, definitely with a more algebraic flavour.
	\section{Definition and algebraic properties of $\Zp$}
		\label{section:Zp}
		\begin{defn}
			A \padic integer is a formal series $\sum_{i \geq 0} a_ip^i$ with integral coefficients $0 \leq a_i \leq p-1$.
		\end{defn}
		The set $\Zp$ contains all the so called \padic integers and is easily identified with
		\[
			\prod_{i\geq0} \{0, 1, \dots, p-1\} = \{0, 1, \dots, p-1\}^\N
		\]
		which is clearly not countable. We have a natural embedding $\N \hookrightarrow \Zp$ just writing every number in base $p$.\newline
		We can define addition between two \padic integer in a component-wise way with a carry system: given $a, b \in \Zp$ the first component of the sum is $a_0 + b_0$ if it's less than $p$, or $a_0 + b_0 - p$ otherwise and, in this case, we add a carry to the component of p and so on. Here's a quick example:
		\begin{gather*}
			1 = 1\cdot p^0 + 0\cdot p^1 + 0 \cdot p^2 + \dots \\
			x = (p - 1)\cdot p^0 + (p-1)\cdot p^1 + (p-1)\cdot p^2 + \dots = \sum_{i\geq 0}(p-1)p^i\\
			1 + x = 0\cdot p^0 + 0\cdot p^1 + 0\cdot p^2 + \dots = 0\\
			\implies -1 = \sum_{i\geq 0}(p-1)p^i.
		\end{gather*}
		This sum admits inverse in $\Zp$, given $a = \sum_{i\geq0} a_ip^i$ we define $b := \sigma(a) = \sum_{i\geq0}(p - 1 - a_i)p^i \in \Zp$ so $a + b + 1 = 0$, i.e. $-a = \sigma(a) + 1$. So $(\Zp, +)$ is an abelian group (easy to verify) and with an involution $\sigma\colon \Zp \to \Zp$ ($\sigma^2 = id$). \newline
		We can also define a product on $\Zp$, multiplying the two expansions in a Cauchy way (exactly like the multiplication between polynomials) and using a system of carries to keep the digits in $\{0, 1, \dots, p-1\}$. This procedure is simply the classical multiplication of natural integers written in base $p$, pursued indefinitely. For example
		\begin{equation*}
			-1 = (p-1)\sum_{i\geq 0}p^i \textrm{ ,  } -(p-1)\sum_{i\geq 0}p^i = 1 \textrm{ ,  } \sum_{i\geq 0}p^i = \frac{1}{1 - p}
		\end{equation*}
		which shows that $1-p \in \Zp$ is invertible. Not every element of $\Zp$ admits inverse, for example $p$ is not invertible because
		\begin{equation*}
			p \cdot \sum_{i\geq 0}a_ip^i = a_0p + a_1p^2 + \dots \neq 1 + 0p + 0p^2 + \dots = 1.
		\end{equation*}
		Then $\Zp$ equipped with these two operations is a commutative ring. We can now extend $\N \hookrightarrow \Zp$ to $\Z \hookrightarrow \Zp$, which is a ring injective homomorphism so we immediately deduct that $\textrm{char}(\Zp) = 0$.
		\begin{prop}
			The ring $\Zp$ is an integral domain.
		\end{prop}
		\begin{proof}
			Given $a = \sum_{i \geq 0}a_ip^i \neq 0, b = \sum_{i\geq 0}b_ip^i \neq 0$ we have that $ab = \sum_{i\geq 0}c_ip^i \neq 0$: infact if $a_v, b_w$ are the first non zero coefficients of $a$ and $b$ then $p \nmid a_v, p \nmid b_w \implies p \nmid a_vb_w$ which means that $c_{v+w} = a_vb_w \neq 0$.
		\end{proof}
		Let us emphasize the importance of $p$ being a prime number: in the last proposition we used the fact that $\Z/p\Z$ is a domain and this is obviously false if $p$ isn't a prime. If we choose to work with $n$-adic integers, with $n$ being a composite integer, then, since $\Z/n\Z$ is not a domain, we obtain that also $\Z_n$ is not a domain, i.e. there are divisors of zero, so we can't even talk about the quotient field.
		\begin{example}
			Here's an example with $n = 10$, using the definition of $\Zp$ given in \cref{thm:projective-lim}:
			\begin{gather*}
				u = (u_n)_n \in \lim_{\longleftarrow} \Z/10^n\Z \qquad u_n := 2^{5^n} \mod 10^n, \\
				v = (v_n)_n \in \lim_{\longleftarrow} \Z/10^n\Z \qquad v_n := 5^{2^n} \mod 10^n.
			\end{gather*}
			It can be proved by induction that
			\[
				u_n = 2^{5^n} \equiv 2^{5^{n-1}} = u_{n-1}, \qquad v_n = 5^{2^n} \equiv 5^{2^{n-1}} = v_{n-1} \mod 10^{n-1}
			\]
			so our definitions are coherent. Obviously $u, v \neq 0$ but it's easily seen that $u \cdot v = 0$: infact $u_n \cdot v_n \equiv 0 \mod 10^n$ (we recall that products in the projective limit are done component-wise). More facts about $10$-adic integers can be found at \cite{michon:padic-arithmetic}.
		\end{example}
		We can define $\ord\colon \Zp \to \N \cup \{\infty\}$ as follows
		\begin{equation*}
			\ord a :=
			\begin{cases*}
				+\infty, & if $a = 0$; \\
				v, & otherwise;
			\end{cases*}
		\end{equation*}
		where $v$ is the minimum integer such that $a_v > 0$. It's easily seen that $\ord$ behaves exactly like a discrete valuation.\newline
		Called $\Fp = \Z/p\Z$ the field with $p$ elements, the map $a = \sum_{i \geq0}a_ip^i \mapsto a_0 \mod p$ is a ring homomorphism $\varepsilon\colon \Zp \to \Fp$, which is obviously surjective and with kernel $p\Zp = \Set{a\in \Zp | a_0 = 0}$. Then $\Zp/p\Zp$ is isomorphic to $\Fp$ so $p\Zp$ is a maximal ideal of $\Zp$.
		\begin{prop}
			The group of invertible elements in $\Zp$ is $\Zp^{\times} = \Set{\sum_{i \geq 0}a_ip^i \in \Zp | a_0 \neq 0}$.
		\end{prop}
		\begin{proof}
			If $a \in \Zp$ is invertible also its reduction $\varepsilon(a) \in \Fp$ must be, so we obtain
			\[
				\Zp^{\times} \subseteq \Set{\sum_{i \geq 0}a_ip^i \in \Zp | a_0 \neq 0 }.
			\]
			The other inclusion can be proved, but, for brevity, we'll show it using an equivalent definition of $\Zp$.
		\end{proof}
		\begin{corollary}
			Every non-zero \padic integer $a \in \Zp$ has a canonical representation $a = p^vu$ where $v = \ord a$ and $u \in \Zp^{\times}$ is a \padic unit.
		\end{corollary}
		\begin{prop}
			The ring $\Zp$ is a principal ideal domain whose ideals are $\{0\}$ and $p^k\Zp := \set{x \in \Zp | \mathrm{ord}_p\, x \geq k}$ for $k \in \N$.
		\end{prop}
		\begin{proof}
			Let $I \neq 0$ be a nonzero ideal a $\Zp$. Chosen $0 \neq a \in I$ an element of minimal order, we have $a = p^ku$ with $u \in \Zp^{\times}$ so $p^k = a \cdot u^{-1} \in I$ which implies $p^k\Zp = (p^k) \subseteq I$. Conversely if $b \in I$ then $w = \ord b \geq k$ so $b = p^wu' = p^kp^{w-k}u' \in p^k\Zp$, which proves $I \subseteq p^k\Zp$.
		\end{proof}
		Lastly, we note that $\Zp$ is a local ring, i.e. a commutative ring with a maximal ideal $p\Zp$.
	\section{Topological properties of $\Zp$}
		Now we are ready to add a topological structure to the ring of \padic integers.
	    Since we can identify every element of $\Zp$ with the sequence of its coefficients $(a_n)_{n \in \N} \in \{0, 1, \dots, p-1\}^\N =: X_p$ it's a natural choice to assign to $\Zp$ the product topology of $X_p$, where each factor is a discrete set.\newline
		By Tychonoff theorem we immediately get that $\Zp$ is compact and it's also easy to see that its connected components are points, i.e. it's totally disconnected. Since the discrete topology is metrizable (using the trivial metric) also $\Zp$ is metrizable, being product of a countable number of metric spaces. Given $x = (a_n)_n, y=(b_n)_n \in X_p \leftrightarrow \Zp$ we can define their distance as
		\begin{equation*}
			d(x, y) := \sup_{i \geq 0} \frac{\delta_{a_i, b_i}}{p^i} = \frac{1}{p^{\ord(x - y)}}.
		\end{equation*}
		This is exactly the metric induced by the \padic absolute value $\pabs{\ }$ introduced above (and satisfies all of its properties)!
		\begin{defn}
			A topological group is a group $G$ equipped with a topology such that the map $(x, y) \mapsto xy^{-1}$ is continuous. A topological ring is a ring $A$ with a topology such that addition $(x, y) \mapsto x + y$ and multiplication $(x, y) \mapsto xy$ are continuous.
		\end{defn}
		\begin{prop}
			$\Zp$ is a topological ring.
		\end{prop}
		\begin{proof}
			First of all we prove that $(x, y) \mapsto x - y$ is a continuous map, i.e. $(\Zp, +)$ is a topological group. Using the \padic metric, given $a, b \in \Zp$ we have
			\begin{gather*}
				\pabs{x - a} \leq p^{-n}, \qquad \pabs{y - b} \leq p^{-n} \\
				\implies \pabs{(x - y) - (a - b)} \leq \max\left\{\pabs{x-a},\pabs{y-b} \right\} \leq p^{-n}
			\end{gather*}
			so the map is continuous at every point $(a, b) \in \Zp \times \Zp$. Now we have to prove the continuity of multiplication. Fixed $a, b \in \Zp$ if $x = a + h, y = b + k \in \Zp$ we have
			\begin{gather*}
				\pabs{xy - ab} = \pabs{(a + h)(b + k) - ab} = \pabs{ak + hb + hk} \leq \\
				\leq \max\left\{\pabs{a}, \pabs{b} \right\}\cdot \left(\pabs{h} + \pabs{k}\right) + \pabs{h}\pabs{k} \to 0, \qquad \text{as } \pabs{h},\pabs{k} \to 0,
			\end{gather*}
			proving the continuity of the multiplication at any point. These two conditions are equivalent to the ones given in the definition of topological ring: infact the map $(x, y) \mapsto x + y$ can be obtained by composing the map $(x, y) \mapsto (x, -y)$ (continuous thanks to product topology and continuity of multiplication) and the map $(x, y) \mapsto x - y$.
		\end{proof}
		\begin{defn}
			A completion of a topological metrizable group $G$ is a pair $(\widehat{G}, j)$ where $\widehat{G}$ is a Cauchy-complete group and $j\colon G \to \widehat{G}$ is a homomorphism such that
			\begin{itemize}
				\item $j(G)$ is dense in $\widehat{G}$;
				\item $j$ is a homeomorphism $G \to j(G)$;
				\item any continuous homomorphism $f\colon G \to G'$, where $G'$ is a complete group, can be uniquely factorized as $f = g \circ j\colon  G \to \widehat{G} \to G'$ with a continuous homomorphism $g\colon \widehat{G} \to G'$.
			\end{itemize}
		\end{defn}
		It's clear that if $G$ admits a completion $\widehat{G}$ then every other completion $\widehat{G}'$ is isomorphic to $\widehat{G}$ (from the definition we have a continuous bijective homomorphism $g\colon \widehat{G} \to \widehat{G}'$).
		Our aim is now to prove that $\Zp$ is a complete space and $(\Zp, +)$ is the completion of $(\Z, +)$ equipped with the \padic metric. We'll now show (and prove) some general results on topological groups which will help us.
		\begin{lemma}
			Let $G$ be a topological group. $G$ is metrizable (i.e. there exists a metric which induces the topology) if and only if $G$ is Hausdorff and first countable (i.e. every point has a countable fundamental system of neighbourhoods).
		\end{lemma}
		\begin{proof}
			The $\implies$ part is trivial. For the converse statement, check \cite[Chap. \RN{11}]{bourbaki:topologie}.
		\end{proof}
		%%% Facts %%%
		A metrizable group $G$ always admits a metric $d$ invariant under left translations, i.e. $d(x, y) = d(gx, gy)$ for every $g \in G$. A metrizable group can always be completed.
		%%% End of facts %%%
		\begin{lemma}
			\label{lemma:closure-group}
			If $G$ is a topological group and $H$ is a subgroup of $G$ then the closure $\overline{H}$ of $H$ is a subgroup of $G$.
			%%% This may be useful in future but not now %%%
			\begin{comment}
			\begin{enumerate}[label=(\alph*)]
				\item the closure $\overline{H}$ of $H$ is a subgroup of $G$.
				\item $G$ is Hausdorff exactly when its neutral element is closed.
			\end{enumerate}
			\end{comment}
		\end{lemma}
		\begin{proof}
			%\textit{(a)}
		 Let $\phi\colon G \times G \to G$ be the continuous map $(x, y) \to xy^{-1}$. Since $H \leq G$ we have $\phi(H \times H) \subseteq H$ hence
			\begin{equation*}
				\phi(\overline{H} \times \overline{H}) = \phi(\overline{H \times H}) \subseteq \overline{\phi(H \times H)} \subseteq \overline{H}
			\end{equation*}
			which proves $\overline{H} \leq G$.
			\begin{comment}
			\textit{(b)} We recall that $G$ is Hausdorff exactly when its diagonal $\Delta_G$ is closed in $G \times G$. We have this chain of implications
			\begin{center}
				G Hausdorff $\implies$ $\{e\}$ closed $\implies$ \\
				$\Delta_G = \phi^{-1}(e)$ closed in $G \times G$ $\implies$ G Hausdorff
			\end{center}
			which concludes the proof.
			\end{comment}
		\end{proof}
		\begin{prop}
			\label{prop:clopen-group}
			Let $G$ be a topological group and $H \leq G$. If $H$ contains a neighbourhood of the neutral element of $G$ then $H$ is a clopen of $G$.
		\end{prop}
		\begin{proof}
			Let $V$ be such neighbourhood; then $\forall h \in H$, $hV$ is a neighbourhood of $h$ in $G$ which is fully contained in $H$. This proves $H$ is open in $G$. Since maps like $x \mapsto gx$ are homeomorphisms for every $g$ in $G$, we have that every coset $gH$ of $H$ in $G$ is open. Now $G\backslash H = \bigcup_{g \notin H}gH$ is open, i.e. $H$ is closed in $G$.
		\end{proof}
		For example the subgroups $p^n\Zp$ of $(\Zp, +)$ are open and closed.
		\begin{defn}
			A subspace $Y$ of a topological space $X$ is \emph{locally closed} (in $X$) when each point $y \in Y$ has an open neighbourhood $V$ in $X$ such that $Y \cap V$ is closed in $V$.
		\end{defn}
		It can be proved that $Y \subseteq X$ is locally closed if and only if $Y$ is open in its closure $\overline{Y}$.
		\begin{thm}
			Let $G$ be a topological group and $H$ a locally closed subgroup. Then $H$ is closed.
		\end{thm}
		\begin{proof}
			If $H$ is locally closed then it's open in its closure $\overline{H}$. In particular, if $e$ is the neutral element of $G$ then $e \in H$ and $\exists V \subseteq H$, which is a neighbourhood of $e$ in $\overline{H}$. By \cref{lemma:closure-group}, $\overline{H} \leq G$ so, applying \cref{prop:clopen-group} with $H \leq \overline{H}$, we get that $H$ is closed in $\overline{H}$, i.e. $H = \overline{H}$, which clearly implies $H$ is closed in $G$.
		\end{proof}
		If we consider only Hausdorff spaces we immediately get that locally compact subsets are locally closed, because a compact set is closed in a Hausdorff space. We recall that a topological group is locally compact exactly when one of its points has a fundamental system of compact neighbourhoods (then by translation every point admits one).
		\begin{corollary}
			Let $H$ be a locally compact subgroup of a Hausdorff topological group $G$. Then $H$ is closed.
		\end{corollary}

		Let $G$ be a topological metrizable group which has $\widehat{G}$ as its completion. If $G$ is locally compact then it must be closed in its completion (we identify $G$ with its image in $\widehat{G}$). But since $G$ is dense in $\widehat{G}$ we get $\widehat{G} = G$.
		\begin{corollary}
			\label{corollary:locally_compact_group}
			A locally compact metrizable group is complete.
		\end{corollary}
		Now we can prove the following
		\begin{prop}
			$\Zp$ is a compact, complete metrizable space. More precisely, the topological group $\Zp$ is the completion of $\Z$, equipped with the \padic metric.
		\end{prop}
		\begin{proof}
			We have already proved that $\Zp$ is compact and metrizable. To see that it's complete we can just apply \cref{corollary:locally_compact_group}, because $\Zp$ is locally compact (from general topology we know that Hausdorff and compact implies that every point has a fundamental system of compact neighbourhoods).\newline
			Let's consider $j\colon \Z \hookrightarrow \Zp$ the natural embedding: it is a continuous homomorphism ($\Z$ with the \padic metric has the topology induced by $\Zp$) and $j(\Z)$ is dense in $\Zp$. Given $\Zp \ni x = \sum_{i \geq 0} a_ip^i$ if
			\begin{gather*}
				x_n := \sum_{0 \leq i < n} a_ip^i \in \N
			\end{gather*}
			then $(x_n)_n \subseteq \Z$ is a Cauchy sequence converging to $x$. To verify the universal property, given a continuous homomorphism $f\colon \Z \to X$, where $X$ is a complete group, we can define $\tilde{f}\colon  \Zp \to X$ as follows: given $x \in \Zp$ and $(x_n)_n \subseteq \Z$ a sequence convergent to $x$ then
			\begin{gather*}
				\tilde{f}(x) := \lim_{n \to +\infty} f(x_n)
			\end{gather*}
			where the limit is taken in $X$. This is well defined: if $(y_n)_n \subseteq \Z$ is another sequence convergent to $x$ we have that $\pabs{x_n - y_n} \to 0$ as $n \to +\infty$ so
			\begin{gather*}
				\lim_{n \to +\infty} \left( f(x_n) - f(y_n) \right) = \lim_{n \to +\infty} f(x_n - y_n) = f\left(\lim_{n \to +\infty} (x_n - y_n)\right) = f(0) = 0
			\end{gather*}
			where we exploited the fact that $f$ is continuous and a homomorphism. The fact that $\tilde{f}$ is a continuous homomorphism is easy to prove.
		\end{proof}
		\begin{corollary}
			The addition and multiplication of \padic integers are the only continuous operations on $\Zp$ extending the classic addition and multiplication on $\Z$.
		\end{corollary}
	\section{$\Zp$ as a projective limit}
		We now want to give another definition of $\Zp$, using projective limits.
		\begin{defn}
			A sequence $(E_n, \varphi_n)_{n \in \N}$ of sets and maps $\varphi_n\colon E_{n+1} \to E_n$ is called a \textit{projective system}. \newline
			A set $E$ together with maps $\psi_n\colon E \to E_n$ such that $\psi_n = \varphi_n \circ \psi_{n+1}$ $\forall n \in \N$ is called a \textit{projective limit} of the sequence $(E_n, \varphi_n)_n$ if the following holds: for each set $X$ and maps $f_n\colon X \to E_n$ satisfying $f_n = \varphi_n \circ f_{n+1}$ $\forall n \in \N$ there is a unique $f\colon X \to E$ such that $f_n = \psi_n \circ f$ for every $n \in \N$ (universal factorization property).
		\end{defn}
		The maps $\varphi_n\colon E_{n+1} \to E_n$ are called transition maps and the whole system, which is often called \textit{inverse system}, can be represented by
		\begin{gather*}
			E_0 \xleftarrow{\varphi_0} E_1 \xleftarrow{\varphi_1} E_2 \xleftarrow{\varphi_2} \dots \xleftarrow{\varphi_{n}} E_{n+1} \longleftarrow \dots
		\end{gather*}
		and denoting $E$ as $\lim\limits_{\longleftarrow}E_n$, the complete scheme would look like this:
		\begin{equation*}
			\begin{tikzcd}[row sep = large, column sep = huge]
				& & & &  \lim\limits_{\longleftarrow}E_n  \arrow[dlll, "\psi_n"']  \arrow[dll, "\psi_{n+1}"]	\\
				\dots & \arrow[l] E_n & \arrow[l, "\varphi_n"] E_{n+1} & \arrow[l] \dots \\
				 & & & &  X \arrow[ulll, "f_n"]  \arrow[ull, "f_{n+1}"'] \arrow[uu, dashed, "f"]
			\end{tikzcd}
		\end{equation*}
		\begin{thm}
			For every projective system $(E_n, \varphi_n)_{n \in \N}$ there exists a projective limit $E = \lim\limits_{\longleftarrow} E_n \subset \prod_n E_n$ with maps $\psi_n\colon E \to E_n$ given by (restriction of) projections.\newline
			Moreover, given $(E', \psi'_n)$ another projective limit of the system, there's a unique bijection $f\colon E' \to E$ such that $\psi'_n = \psi_n \circ f$.
		\end{thm}
		\begin{proof}
			First we prove existence. Let
			\begin{gather*}
				E := \{(x_n)_n : \varphi_n(x_{n+1}) = x_n \text{   } \forall n \geq 0 \} \subset \prod_{n \geq 0} E_n
			\end{gather*}
			be the set of \textit{coherent sequences} (with respect to the transition maps $\varphi_n$). If $p_n\colon \prod_{i \geq 0}E_i \to E_n$ is the canonical projection then we have
			\begin{gather*}
				\varphi_n(p_{n+1}(x)) = p_n(x) \qquad \forall x \in E.
			\end{gather*}
			So, if we define $\psi_n := \left.p_n\right|_E\colon E \to E_n$ we have $\varphi_n \circ \psi_{n+1} = \psi_n$. We'll now show that $(E, \psi_n)$ is a projective limit of the system. If $(E', \psi'_n)$ is another set equipped with maps satisfying $\varphi_n \circ \psi'_{n+1} = \psi'_n$ (for every $n\geq 0$) then we need to prove that there's a unique factorization of $\psi'_n$ by $\psi_n$. We can define a vector map
			\begin{gather*}
				(\psi'_n)\colon E' \to \prod_{n \geq 0}E_n, \qquad y \mapsto (\psi'_n(y))_n.
			\end{gather*}
			Since $\varphi_n(\psi'_{n+1}(y)) = \psi'_n(y)$, the image of this map is fully contained in $E$ (i.e. $(\psi'_n(y))_n$ is a coherent sequence). Thus there's a unique map $f\colon E' \to E$ such that $\psi'_n = \psi_n \circ f$, and it's exactly the map $(\psi'_n)$ considered with $E$ as target (uniqueness is easy to see, recalling that $\psi_n$ is just the restrictions to $E$ of the canonical projection $p_n$).\newline
			Now we have to prove uniqueness. If $(E, \psi_n)$ and $(E', \psi'_n)$ are both projective limits then, by the universal factorization property, there's a unique map $f'\colon E \to E'$ with $\psi_n = \psi'_n \circ f'$. Using the same $f\colon E' \to E$ defined before and substituting in $\psi'_n = \psi_n \circ f$ we obtain
			\begin{gather*}
				\psi'_n = \psi_n \circ f = \psi'_n \circ f' \circ f
			\end{gather*}
			which means that $f' \circ f$ is a factorization of $\text{id}_{E'}$ (identity map). Since $(E', \psi'_n)$ has also, by definition of projective limit, the unique factorization property we must have $f' \circ f = \text{id}_{E'}$. Similarly we can prove $f \circ f' = \text{id}_E$, so $f$ is the searched bijection.
		\end{proof}
		The projective limit can be defined for a lot of structures, like topological spaces, groups or vector spaces. For example, given $(G_n, \varphi_n)_n$ a projective system of groups and homomorphisms $\varphi_n\colon G_{n+1} \to G_n$, the projective limit $G=\lim\limits_{\longleftarrow}G_n$ is a group and the projections $\psi_n\colon G \to G_n$ are group homomorphisms. Likewise, a projective system of topological spaces and continuous maps will have a projective limit which is itself a topological space, equipped with continuous projections.

		We can now give another definition of $\Zp$. Let's consider the ring $\Z$ and the decreasing sequence $(p^n\Z)_n$ of ideals. The inclusion $p^{n+1}\Z \subset p^n\Z$ gives us the canonical transition homomorphism
		\begin{gather*}
			\varphi_n\colon \Z/p^{n+1}\Z \to \Z/p^n\Z, \qquad x + p^{n+1}\Z \mapsto x + p^n\Z.
		\end{gather*}
		If we consider $\Z/p^n\Z$ as a topological ring (equipped with discrete topology) then we have the following theorem.
		\begin{thm}
			\label{thm:projective-lim}
			The map $\Phi\colon \Zp \to \lim\limits_{\longleftarrow}\Z/p^n\Z$ which associates to the \padic integer $x = \sum_{i \geq 0} a_ip^i$ the sequence of its partial sums $x_n = \sum_{i < n}a_ip^i \mod p^n$ is an isomorphism of topological rings.
		\end{thm}
		\begin{proof}
			The map is well defined. In-fact, since the transition maps $\varphi_n$ are given by
			\begin{gather*}
				\sum_{i \leq n}a_ip^i \mod p^{n+1} \quad \mapsto \quad \sum_{i < n}a_ip^i \mod p^n
			\end{gather*}
			the set of coherent sequences in $\prod \Z/p^n\Z$ is exactly the set of partial sums of a \padic expansion. From the relations
			\begin{gather*}
				x_1 = a_0 \text{, } \quad x_2 = a_0 + a_1p \text{, } \quad x_3 = a_0 + a_1p + a_2p^2 \text{, \dots} \\
				a_0 = x_1 \text{, } \quad a_1 = \frac{x_2 - x_1}{p} \text{, } \quad a_2 = \frac{x_3 - x_2}{p^2} \text{, \dots}
			\end{gather*}
			we infer that $\Phi$ is bijective. It's easily proved that it is a ring homomorphism (sum and product are done component-wise on $\lim\limits_{\longleftarrow}\Z/p^n\Z$), so $\Phi$ is a ring isomorphism. Finally, this map is continuous since for every $n \in \N$, if $\pi\colon \prod_i \Z/p^i\Z \to \Z/p^n\Z$ is the canonical projection, we have
			\begin{gather*}
				\begin{aligned}
					\Zp \xrightarrow{\Phi} \lim\limits_{\longleftarrow}\Z/p^k\Z \xhookrightarrow{} \prod_k \Z/p^k\Z \xrightarrow{\pi} \Z/p^n\Z, \qquad
					\sum_{i \geq 0}a_ip^i \mapsto \sum_{i < n} a_ip^i \mod p^n
				\end{aligned}
			\end{gather*}
			which is continuous (we recall how product topology is defined). Now $\Phi$ is a continuous invertible map between two compact spaces so it's a homeomorphism.
		\end{proof}
		So we can also think $\Zp$ as the projective limit of $\Z/p^n\Z$, with canonical projection maps. Let us observe that we can choose any system of representatives $\mathcal{S}$ for $\Z/p\Z$ and write any \padic integer as $x = \sum s_ip^i$ with $s_i \in \mathcal{S}$. For example, if $p$ is odd we can choose to use $\mathcal{S} = \{-\frac{p-1}{2}, \dots, 0, \dots, \frac{p-1}{2}\}$. Although we are only working with $\Zp$, where $p$ is a prime number, this theorem also gives us a factorization of $\Z_n$, for each $n \in \N$. In-fact, since the projective limit of a cartesian product is exactly the cartesian product of the projective limits of the factors, if $m = p_1^{\alpha_1} \dotsm p_r^{\alpha_r}$ we have
		\begin{gather*}
			\Z/m^n\Z = \Z/p_1^{\alpha_1\cdot n}\Z \times \dotsm \times \Z/p_r^{\alpha_r\cdot n}\Z \\
			\implies \Z_m = \lim_{\longleftarrow} \left( \Z/p_1^{\alpha_1\cdot n}\Z \times \dotsm \times \Z/p_r^{\alpha_r\cdot n}\Z \right) = \prod_{i = 1}^r \lim_{\longleftarrow} \Z/p_i^{\alpha_i\cdot n}\Z = \prod_{i=1}^r \Z_{p_i^{\alpha_i}}.
		\end{gather*}
		In particular, for the already seen example $m=10$, we obtain $\Z_{10} = \Z_2 \times \Z_5$.

		Lastly, we can give another description of $\Zp$ using formal power series $\Z\llbracket X \rrbracket$. We recall that on $\Z\llbracket X \rrbracket$ sum is defined component-wise (obviously here there's no carry system, unlike in $\Zp$) and product is done in a Cauchy way (there's a natural inclusion $\Z[X] \xhookrightarrow{} \Z\ser{X}$).
		\begin{thm}
			The map
			\begin{gather*}
				 \Phi\colon \Z\llbracket X \rrbracket \to \Zp   \qquad \sum a_iX^i \mapsto \sum a_ip^i
			\end{gather*}
			is a ring homomorphism, which defines a canonical isomorphism
			\begin{gather*}
				\frac{\Z\ser{X}}{(X - p)} \xrightarrow{\sim} \Zp
			\end{gather*}
			where $(X - p)$ is the principal ideal of $\Z\llbracket X \rrbracket$ generated by $X - p$.
		\end{thm}
		\begin{proof}
			To prove this theorem we exploit the universal factorization property of $\lim\limits_{\longleftarrow}\Z/p^n\Z$, i.e. $\Zp$.  Let's consider this sequence of maps
			\begin{gather*}
				\Phi_n\colon \Z\llbracket X \rrbracket \to \Z/p^n\Z \text{,} \qquad \sum a_iX^i \mapsto \sum_{i < n}a_ip^i \mod p^n.
			\end{gather*}
			They're actually ring homomorphisms; the condition $\Phi_n(x + y) = \Phi_n(x) + \Phi_n(y)$ is immediate, to check $\Phi_n(x\cdot y) = \Phi_n(x)\cdot \Phi_n(y)$ we write
			\begin{gather*}
				\Phi_n\left( \sum_ia_iX^i \cdot \sum_jb_jX^j \right) = \Phi_n\left(\sum_k c_kX^k\right) = \sum_{k < n} c_kp^k \mod p^n = \\ = \sum_{k < 2n} c_kp^k \mod p^n = \left(\sum_{i < n}a_ip^i\right) \cdot \left(\sum_{j < n}b_jp^j\right) \mod p^n =\\
				= 	\Phi_n\left(\sum_ia_iX^i \right) \cdot \Phi_n\left(\sum_jb_jX^j \right).
			\end{gather*}
			It's immediate that these maps are all compatible with the transition homomorphisms
			\[
				\varphi_n\colon \Z/p^{n+1}\Z \to \Z/p^n\Z, \qquad x + p^{n+1}\Z \mapsto x + p^n\Z
			\]
			and so we infer there exists a unique homomorphism
			\begin{gather*}
				\Phi\colon \Z\llbracket X \rrbracket \to \lim\limits_{\longleftarrow}\Z/p^n\Z = \Zp
			\end{gather*}
			compatible with the $\Phi_n$ (i.e. such that $\psi_n \circ \Phi = \Phi_n$, where $\psi_n\colon \lim\limits_{\longleftarrow}\Z/p^k\Z \to \Z/p^n\Z$ is the canonical projection). This map is surjective: if $x = \sum a_ip^i$ is a \padic integer then $\Phi\left(\sum a_iX^i \right) = x$. Thanks to the first theorem of isomorphism for ring homomorphisms now we just need to prove $\ker \Phi = (X - p)$ (then we'll have $\Z\llbracket X \rrbracket/\ker \Phi = \Z\llbracket X \rrbracket/(X - p) \simeq \Ima \Phi = \Zp$).
			In other words we need to show that if the formal power series $\sum_{i \geq 0} a_iX^i$ is such that $\sum_{i < n} a_ip^i \in p^n\Z$ for every $n \geq 1$, then it is divisible by $X - p$. For $n = 1$ the condition is $a_0 \equiv 0 \mod p$ so we find $\alpha_0 \in \Z$ such that $a_0 = p\alpha_0$. For $n = 2$ we get
			\begin{gather*}
				a_0 + a_1p  = \alpha_0p + a_1p \equiv 0 \mod p^2 \implies \alpha_0 + a_1 \equiv 0 \mod p
			\end{gather*}
			so we find $\alpha_1 \in \Z$ such that $\alpha_0 + a_1 = p\alpha_1$ so $a_1 = p\alpha_1 - \alpha_0$. For a general $n \geq 1$ the condition is
			\begin{gather*}
				a_0 + a_1p + \dots + a_np^n = p^n\alpha_{n-1} + a_np^n \equiv 0 \mod p^{n+1}
			\end{gather*}
			and it furnishes an integer $\alpha_n$ such that $\alpha_{n-1} + a_n = p\alpha_n$, which can be written as $a_n = p\alpha_n - \alpha_{n-1}$. These relations between the coefficients $a_n$ and $\alpha_n$ are exactly the ones expressed by
			\begin{gather*}
				\sum a_iX^i = -(X - p) \cdot \sum \alpha_i X^i
			\end{gather*}
			which concludes our proof.
		\end{proof}
	\section{The field $\Qp$}
		\label{section:Qp}
		We have proved that $\Zp$ is an integral domain hence we can define the field
		\begin{equation*}
			\Qp = \textrm{Frac}(\Zp).
		\end{equation*}
		To understand its structure, we recall that any \padic integer can be written as $x = p^mu$ where $u \in \Zp^\times$. Then, $1/x = p^{-m}u^{-1}$, with $u^{-1} \in \Zp$. So we can write
		\begin{equation*}
			\Qp = \Zp[1/p] = \bigcup_{m \geq 0} p^{-m}\Zp.
		\end{equation*}
		Since a non-zero element of $\Qp$ admits a unique such writing, $\Qp^\times = \coprod_{m \in \Z} p^m\Zp^\times$. Similarly to Laurent expansions of meromorphic functions around a pole, we can write every non-zero element of $\Qp$ as
		\begin{equation*}
			x = p^m \cdot \sum_{i \geq 0} a_ip^i = \sum_{i \geq m} a_{i-m}p^i, \qquad m \in \Z, a_0 \neq 0.
		\end{equation*}

		We can extend the function $\ord$ to $\Qp$ as follows:
		\begin{equation*}
			\ord x =
			\begin{cases}
				m, & \text{if $x = p^mu, u \in \Zp^{\times}$;} \\
				+\infty, & \text{otherwise;} \\
			\end{cases}.
		\end{equation*}
		Given $x = 	a/b$ with $a \in \Zp, b \in \Zp^\times$ it's easy to see that $\ord x = \ord a - \ord b$ and, writing every number as above, we immediately get $\ord xy = \ord x + \ord y$ (this holds also when $xy = 0$, with the usual convention $m + \infty = \infty + m = \infty$), i.e. $\ord\colon \Qp^\times \to \Z$ is a group homomorphism. Finally we see that $\ord (x + y) \geq \min\{\ord x, \ord y\}$, with the equality holding when $\ord x \neq \ord y$. These properties tell us exactly that $\ord$ is a discrete valuation on the field $\Qp$, and that $\Zp$ is the ring of valuation of $\left(\Qp, \ord\right)$ because $x \in \Zp$ if and only if $\ord x \geq 0$ and $\ord 1/x = - \ord x$.

		We recall that
		\[
		\Z_{(p)} = \Set{\frac{a}{b} | a,b \in \Z, p \nmid b, b \neq 0}.
		\]
		The relations between $\Zp$ and $\Qp$ are similar to the ones between $\Z_{(p)}$ and $\Q$. In-fact we have
		\begin{equation*}
			\Q = \bigcup_{m \geq 0} p^{-m}\Z_{(p)} \text{, } \qquad  \Q^\times = \coprod_{p \in \Z}p^m\Z_{(p)}^\times
		\end{equation*}
		where $\Z_{(p)}^\times$ consists of all the fractions with both numerator and denominator prime to $p$.

		We can see that the definition of $\Qp$ introduced here represents exactly the same object described in chapter 1. So we can introduce the \padic absolute value, and its induced metric, in the exact same way and all properties proved before will be valid. So $\Qp$ is a metric field equipped with a discrete valuation, which implies that $\Qp$ is a topological field (i.e. a topological ring where the inverse map $\Qp^\times \to \Qp^\times: x \mapsto x^{-1}$ is continuous).
		\begin{prop}
			The field $\Qp$ is a locally compact field of characteristic $0$ which induces on $\Zp$ the \padic topology. It can be identified with the completion of $\Zp[1/p]$ or of $\Q$, for the \padic metric.
		\end{prop}
		\begin{proof}
			We have already observed that $\Zp = B_{\leq 1}(0) = \{x \in \Qp \mid \pabs{x} \leq 1\}$ and, for every $k \geq 0$ the ideal $p^k\Zp$ is exactly $B_{\leq p^{-k}}(0)$. Since $\Zp$ is a compact neighbourhood of $0$, the topological field $\Qp$ is locally compact ($x + \Zp$ is a compact neighbourhood of $x$). From \cref{corollary:locally_compact_group} we obtain that $\Qp$ is complete. We now show that $\Z[1/p]$ is dense in $\Qp$; given
			\begin{gather*}
				x = \sum_{i \geq v}^{+\infty} x_ip^i \qquad (v = \ord x \in \Z)
			\end{gather*}
			we immediately find that the sequence of truncated sums $x_n = \sum_{v \leq i < n} x_ip^i$ is a Cauchy sequence in $\Z[1/p] \subset \Q$ which converges to $x$. Finally, we have already seen that $\Qp$ is a field of characteristic $0$, since we have an immersion $\Z \hookrightarrow \Qp$.
		\end{proof}
		Here we have proved that $\Qp$ is a complete field, a fact we already knew, in a very different way than before, by just using algebraic properties of topological groups.

		Given a non-zero element $\Qp \ni x = \sum_{i \geq m}x_ip^i$ we can define
		\begin{gather*}
			[x] := \sum_{i \geq 0}x_ip^i \in \Zp \text{ : \emph{integral part} of $x$ }; \\
			\langle x \rangle := \sum_{i < 0} x_ip^i \in \Z[1/p] = \{ap^v \mid a,v \in \Z \} \subset \Q \text{ : \emph{fractional part} of $x$}.
		\end{gather*}
		Hence we obtain the decomposition $\Qp = \Zp + \Z[1/p]$, which is not canonical, because it depends on the choice of the representatives of $\Z/p\Z$ chosen for digits (here we have always chosen to use $\mathcal{S} = \{0, 1, \dots, p-1\}$). This sum is not a direct sum since $\Zp \cap \Z[1/p] = \Z$, so there's not a unique factorization of any $x \in \Qp$. If we consider the map $\Z \to \Zp \oplus \Z[1/p]: m \mapsto (m, -m)$ and the addition homomorphism $\Zp \oplus \Z[1/p] \to \Zp + \Z[1/p] = \Qp: (a, b) \mapsto a+b$ we obtain the short exact sequence:
		\begin{equation*}
			\Z \hookrightarrow \Zp \oplus \Z[1/p] \twoheadrightarrow \Qp
		\end{equation*}
		where the image of the first map is exactly the kernel of the second one.
	\section{Hensel's Lemma}
		In this section we present the important Hensel's lemma, a principle which gives us a method to find roots of polynomials in $\Zp[X]$.
		\begin{prop}
			Let $P(X) \in \Zp[X]$. The following properties are equivalent:
			\begin{enumerate}[label=(\roman*)]
				\item $P=0$ admits a solution in $\Zp$;
				\item for each $n \geq 0$, $P=0$ admits a solution in $\Z/p^n\Z$.
			\end{enumerate}
		\end{prop}
		\begin{proof}
			The part \textit{(i)} $\implies$ \textit{(ii)} is trivial: if $x = \sum_{i \geq 0} a_ip^i \in \Zp$ is a root of $P$ then $x_n = \sum_{i < n}a_ip^i$ is in $\Z/p^n\Z$ and $P(x_n) = P(x) \mod p^n$.\\
			To prove the converse, let's consider the non-empty finite sets
			\[
				X_n = \{x \in \Z/p^n\Z \mid P(x) = 0 \mod p^n \}.
			\]
			It's immediate that if $x \in X_{n+1}$ then $\tilde{x} = x \mod p^n$ is in $X_n$, because $P(\tilde{x}) = P(x) \mod p^n$. So reduction mod $p^n$ furnishes a map $\phi_n\colon X_{n+1} \to X_n$. We can consider the projective system $(X_n, \phi_n)_{n \in \N}$: it admits a projective limit $X = \lim\limits_{\longleftarrow} X_n \subset \Zp$. It's now clear that if $x \in X$ then $P(x) = 0$ in $\Zp$. Since the projective limit of non-empty sets is not empty (it immediately follows from the fact that projective limit of non-empty compact space is non-empty, \cite[30]{robert:padic-analysis}), we can conclude.
		\end{proof}
		We recall the elementary fact that if $A$ is a commutative ring and $P \in A[X]$ then
		\[
			P(X + h) = P(X) + h \cdot P'(X) + h^2 \cdot Q(X, h)
		\]
		where $Q$ is a polynomial in $A[X, Y]$ (we'll refer to this as the Taylor expansion of $P$, for obvious reasons). We're now ready to prove this proposition. For brevity, we'll write $v(\zeta) := \ord \zeta$, for $\zeta \in \Qp$.
		\begin{prop}
			\label{prop:newton-algorithm}
			Let $P \in \Zp[X]$ and $x \in \Zp$ be such that $P(x) \equiv 0 \mod p^n$. If $k = v(P'(x)) < n/2$, then $\widehat{x} := x - P(x)/P'(x)$ satisfies
			\begin{enumerate}[label=(\roman*)]
				\item $P(\widehat{x}) \equiv 0 \mod p^{n+1}$;
				\item $\widehat{x} \equiv x \mod p^{n-k}$;
				\item $v(P'(\widehat{x})) = v(P'(x))$.
			\end{enumerate}
		\end{prop}
		\begin{proof}
			Let's write $P(x) = p^ny$ for $y \in \Zp$ and $P'(x) = p^ku$ with $u \in \Zp^\times$. Then
			\[
				\widehat{x} - x = -\frac{P(x)}{P'(x)} = - p^{n-k}yu^{-1} \in p^{n-k}\Zp
			\]
			which proves \textit{(ii)}. To prove \textit{(i)} we observe
			\[
				P(\widehat{x}) = P(x + (\widehat{x} - x)) = P(x) -\frac{P(x)}{P'(x)} P'(x) + (\widehat{x} - x)^2 \cdot t
			\]
			where $t \in \Zp$. Then
			\[
				P(\widehat{x}) = p^{2(n-k)}y^2u^{-2}t^2 \in p^{n+1}\Zp \subseteq p^{2(n -k)}\Zp
			\]
			since $n - k > n/2$. Now it only remains to compute the order of $P'(\widehat{x})$. Let's use Taylor expansion:
			\[
				P'(\widehat{x}) = P'(x) + (\widehat{x} - x)\cdot s = p^ku + p^{n-k}zs = p^k(u + p^{n-2k}zs) = p^kw \quad (z, s \in \Zp).
			\]
			Since $n - 2k > 0$ and $u$ is a unit of $\Zp$, we get
			\[
				w = u + p^{n-2k}zs \in u + p^{n-2k}\Zp \subset \Zp^\times
			\]
			which proves $v(P'(\widehat{x})) = k$.
		\end{proof}
		We can finally prove the Hensel's Lemma.
		\begin{thm}[Hensel's Lemma]
			\label{thm:hensel-lemma}
			Let $P$ be a polynomial in $\Zp[X]$ and $x \in \Zp$ such that $P(x) \equiv 0 \mod p^n$. If $k = v(P'(x)) < n/2$ then there exists a unique root $\xi$ of $P$ in $\Zp$ such that $\xi \equiv x \mod p^{n-k}$ and $v(P'(\xi)) = v(P'(x))$.
		\end{thm}
		\begin{proof}
			Let's first prove the existence of such $\xi$. Let $x_0 = x$; we want to find $x_1 \in \Zp$ such that
			\[
				x_1 \equiv x_0 \mod p^{n-k}, \quad  P(x_1) \equiv 0 \mod p^{n+1}, \quad v(P'(x_1)) = k.
			\]
			By \cref{prop:newton-algorithm} we can build such an $x_1$, which represents an ``improved'' root of $P$. Similarly, we can find $x_2 \in \Zp$ such that
			\[
				x_2 \equiv x_1 \mod p^{n-k+1}, \quad P(x_2) \equiv 0 \mod p^{n+2}, \quad v(P'(x_2)) = k.
			\]
			Iterating this process we get a coherent sequence $(x_m)_{m\in \N} \subset \Zp$: more specifically, letting $h = n-k$, we obtain
			\begin{gather*}
				x_m \equiv \sum_{i=0}^{h+m} a_ip^i \mod p^{h+m+1} \text{, } \quad  P(x_m) \equiv 0 \mod p^{n+m} \\
				x_{m+1} \equiv \sum_{i=0}^{h+m} a_ip^i + a_{h+m+1}p^{h+m+1} \mod p^{h+m+2} \text{, } \quad P(x_{m+1}) \equiv 0 \mod p^{n+m+1} \\
			\end{gather*}
			so it's clear that this sequence has \padic limit $\xi = \sum_{i \geq 0} a_ip^i$ satisfying $P(\xi) = 0$ in $\Zp$, $\xi \equiv x \mod p^{n-k}$ and $v(P'(\xi)) = k$. \\
			Now we prove uniqueness. Let $\xi$ and $\eta$ be two roots of $P$ in $\Zp$ satisfying the above constraints. Then
			\[
				0 = P(\eta) = P(\xi) + (\eta - \xi)P'(\xi) + (\eta - \xi)^2a \quad (a \in \Zp)
			\]
			so, since $\eta - \xi \in p^{n-k+1}\Zp$, we have
			\[
				0 = (\eta - \xi)(P'(\xi) + (\eta - \xi)a).
			\]
			Clearly, since $v(P'(\xi)) = k$ and $v((\eta - \xi)a) \geq n-k+1 > k$, the term $(P'(\xi) + (\eta - \xi)a)$ can't vanish so we must have $\eta = \xi$.
		\end{proof}
	 	The Hensel's Lemma is a very important tool in \padic analysis, so we'll write again a weak version of it.
	 	\begin{corollary}[Weak Hensel's Lemma]
	 		Let $P$ a polynomial in $\Zp[X]$ and $a_0 \in \Zp$ such that $P(a_0) \equiv 0 \mod p$ and $P'(a_0) \not\equiv 0 \mod p$. Then there is a unique $a \in \Zp$ such that $P(a) = 0$ and $a \equiv a_0 \mod p$.
	 	\end{corollary}