\chapter{Explainability}
\label{ch_explainability}
%
During evaluation of the benchmark study, we found that proset models are very open to inspection after fitting.
This is due to their built-in feature selection and geometric structure.
In this chapter, we review diagnostic plots and other techniques that provide a better understanding of model behavior.
%
\section{Low-dimensional representations of the data}
\label{sec_low_dimensional}
%
Verification of the model structure is greatly simplified if the algorithm is able to reduce the inputs to a small set.
We can then conduct what amounts to post-fit exploratory data analysis.\par
%
In the benchmark study, the proset classifier achieves the largest reduction in the number of features for the cancer data set, where only 4 out of 30 features yield a model that is still equivalent to XGBoost in terms of log-loss (see Table \ref{tab_e3_e8_e9}).
With only four inputs, we can visually inspect all scatter plots involving a combination of two features.
Figure \ref{fig_scatter_plots_cancer} shows the positions of the prototypes (large circles) and test samples (small circles).
The three misclassified cases are circled in black.
The density plots on the diagonal show the marginal distributions for each feature, both for the prototypes (solid curves / vertical lines) and test samples (dashed curves / dots).
Two interesting findings are that (a) any pair of features allows us to distinguish the two classes fairly well and (b) the prototypes are more clearly separated with lower variance than the whole population.\par
%
\begin{figure}
\caption{Scatter plots for active features of the cancer data model}
\label{fig_scatter_plots_cancer}
%
\begin{center}
\includegraphics[width=0.95\textheight, angle=90]{figures/cancer_scatter_plot_testing_data.pdf}
\end{center}
\end{figure}
%
A `small' model is also easier to review for domain experts.
For example, an oncologist should be able to provide a clear assessment whether the four features (in decreasing order of weights: worst radius, worst concave points, worst texture, worst smoothness) are a natural and sufficient choice of indicators.
If we have access to the original image data, we can ask experts to review the misclassified cases in detail and compare them to the most similar prototypes.
This can reveal labeling errors, important features excluded by the algorithm due to dearth of relevant training samples, or genuine `hard cases' that are difficult to classify even for humans.\par
%
A technique that can work even if the number of features remains large is to create low-dimensional maps of the data that take model fit results into account.
For the visualization we call the `batch 1 map', we perform doubly weighted PCA on the prototypes of the first batch:\par
%
Let $Z\in\R^{J_1\times D}$ be the matrix obtained by rescaling the features for the prototypes of the first batch with the corresponding feature weights:
%
\begin{equation}
Z_{j,d}:=v_{1,d}x_{s_{1,j},d}\label{eq_scaled_x}
\end{equation}
%
This gives larger impact to features for which the algorithm is more sensitive and removes any deselected features from the analysis.
Now let $\overline{z}$ be the row mean of $Z$ weighted using the prototype weights:
%
\begin{equation}
\overline{z}:=\frac{\sum_{j=1}^{J_1}w_{1,j}Z_{j,\bullet}}
{\sum_{j=1}^{J_1}w_{1,j}}\label{eq_weighted_mean}
\end{equation}
%
Denote by $\overline{Z}$ the centered matrix
%
\begin{equation}
\overline{Z}_{j,d}:=Z_{j,d}-\overline{z}_d\label{eq_centered_Z}
\end{equation}
%
Let $W\in\R^{J_1\times J_1}$ be the diagonal matrix with diagonal elements $w_{1,j}$ and compute the matrix decomposition
%
\begin{equation}
\overline{Z}^TW\overline{Z}=:U\Sigma U^T\label{eq_weighted_svd}
\end{equation}
%
In the above, $\Sigma$ is the diagonal matrix of eigenvalues with row and column dimension equal to the rank of $\overline{Z}^TW\overline{Z}$, while $U$ is the corresponding orthogonal matrix of eigenvectors.
If $\Sigma$ has at least rank two, the `batch 1 map' coordinates for the prototypes of the first batch are the first two columns of the product $\overline{Z}U$.
Other samples can be mapped via the same transform, i.e., scale with $v_1$, center with $\overline{z}$, and multiply with $U$.\par
%
\begin{figure}
\caption{`Batch 1 map' of cancer data showing test samples}
\label{fig_batch_1_map_test}
%
\begin{center}
\includegraphics[height=0.4\textheight]{figures/cancer_batch_1_map_testing_data.pdf}
\end{center}
\end{figure}
%
The `batch 1 map' for the prototypes and test set of the cancer data is shown in Figure \ref{fig_batch_1_map_test}.
It appears that the two classes are almost linearly separable except for the three misclassified samples.
Two are close to the boundary between the classes, while the last is a `malignant' case surrounded by `benign' cases.
%
\section{Assessing the familiarity of new samples}
\label{sec_familiarity}
%
\begin{figure}
\caption{Model behavior outside the `area of competence'}
\label{fig_area_of_competence}
%
\begin{center}
\subfloat[\textbf{kNN on the rotated checkerboard}]{\includegraphics[width=0.49\textwidth]{figures/checker_rot_knn_surf_test.pdf}}
\subfloat[\textbf{XGBoost on the rotated checkerboard}]{\includegraphics[width=0.49\textwidth]{figures/checker_rot_xgb_surf_test.pdf}}\\
\subfloat[\textbf{proset on the rotated checkerboard}]{\includegraphics[width=0.49\textwidth]{figures/checker_rot_surf_test.pdf}}
\end{center}
\end{figure}
%
One problem faced by all machine learning is that their behavior becomes arbitrary for samples that are far removed from the original training data.
The way a model extrapolates outside its `area of competence' depends entirely on the specific algorithm and does not reflect any empirical evidence.
This is illustrated in Figure \ref{fig_area_of_competence} for the rotated checkerboard example.
The plots show the probability surfaces for each of the three classifiers studied in Chapter \ref{ch_classifier}, i.e., the color scale represents the estimated probability for class 1 from green (0.0) to red (1.0).
While kNN and XGBoost create idiosyncratic patterns in regions of the feature space with no training data, proset reverts to the marginal distribution.
The proset estimate is no more correct than that of the other two algorithms, but we consider it the most `honest' in the absence of information.
For balanced training data as in the example, proset does not assign a high probability to an arbitrary class as may happen with kNN or XGBoost.
Given that users tend to interpret the probability of the class with the highest probability estimate as the model's `confidence' in the result, this is an important advantage.\par
%
The proset algorithm can even provide an explicit indicator that a new sample is far away from the training data.
For any $x\in\R^D$, we define the familiarity of a proset model with $x$ as
%
\begin{equation}
\mathfrak{f}(x):=\sum_{b=1}^B\sum_{j=1}^{J_b}w_{b,j}G_{v_b}(x-x_{s_{b,j}})\label{eq_familiarity}
\end{equation}
%
This is just the denominator of (\ref{eq_pkx}) minus 1, which can be provided alongside the estimate with no additional effort.
Familiarity measures the total contribution of the weighted prototypes to the estimate relative to the marginal distribution represented by the 1.\par
%
Instead of using the absolute size of $\mathfrak{f}(x)$ to assess new samples, we find it more useful to express it as a quantile of the distribution of familiarity observed for test data.
As the model may overfit its training data slightly, it is better to use held-out data for this analysis.
Figure \ref{fig_area_of_competence} shows the resulting 1 \% contour line of familiarity for the proset classifier.
For a new sample, familiarity expressed as a quantile of the previously observed distribution can be interpreted as p-value of a statistical test whether the estimate is inside the model's `area of competence'.\par
%
For a supervised learning problem with many features, the training data is typically close to a `thin' manifold in the high-dimensional feature space.
Using quantiles of familiarity as indicated above, we can measure proximity to that manifold via a scalar indicator.
This is a powerful tool for monitoring a machine learning model in production and to detect data drift.
However, there is a trade-off between feature selection and the usefulness of familiarity.
Consider the case where proset does not select a feature because of insufficient variation in the training data.
A new sample that has a markedly different value in that one feature is still assigned a high familiarity score if the features actually used by the model lie in the expected range.
%
\section{Explaining individual estimates}
\label{sec_individual_explanations}
%
\subsection{Explanation report}
\label{sec_explanation_report}
%
\begin{figure}
\caption{Example of explanation report for cancer data}
\label{fig_explanation_report}
%
\begin{center}
\includegraphics[width=0.99\textwidth]{figures/cancer_2d_95_model_explain.pdf}
\end{center}
\end{figure}
%
Our implementation of proset can export information about the model as tabular data.
The basic report contains information on all prototypes, their weights, and active features.
A more interesting report relates a particular sample to the selected prototypes.
Figure \ref{fig_explanation_report} shows an example analysis for the proset classifier.
It deals with the one misclassified `malignant' case from the cancer data example that is surrounded by `benign' cases.
The report contains the following information:
%
\begin{description}
\item[Batch:] the number of the batch to which each prototype belongs.
The model in the example has only a single batch.
The first three rows contain information on the misclassified sample and the marginal distribution, they are not associated with any batch.
%
\item[Sample:] the zero-based index of the prototype with respect to the ordering of the training data after the train-test split.
%
\item[Sample name:] the record for the test sample states its zero-based index within the set of test data, the class with the highest estimated probability, and familiarity expressed as a quantile of the distribution for the test data.
The remaining rows indicate whether they belong to the marginal distribution or individual prototypes.
%
\item[Target:] the numeric representation of the class, either 0 (`benign') or 1 (`malignant').
The first rows contains the true target for the test sample.
%
\item[Prototype weight:] the value $w_{b,j}$ for each prototype.
The test sample has no entry, records for the marginal distribution show the marginal probability for the corresponding class.
%
\item[Similarity:] the value of the unnormalized Kernel $G_{v_b}$ at $x-x_{s_{b,j}}$.
This is a number in $(0,1]$, where $1$ indicates that the sample features $x$ are identical to those of the prototype.
The test sample has no entry, records for the marginal probabilities list a 1 as they affect all estimates equally.
%
\item[Impact:] the product of prototype weight and similarity is the total impact of the prototype on the estimated probability (\ref{eq_pkx}).
By default, the report lists the prototypes in order of descending impact.
%
\item[Dominant set:] a binary indicator of observations that have the largest impact on the estimate.
The target values for prototypes outside the dominant set can be changed without affecting which class is assigned the highest probability.
See below for a precise definition of the dominant set.
%
\item[p class 0:] the record for the test sample shows the estimated probability for class 0 (`benign').
The remaining rows contain the additive contribution of the marginal distribution and prototypes to this estimate.
The contribution is equal to the impact listed for the row divided by the total impact if the target is 0.
Otherwise, the contribution is zero.
%
\item[p class 1:] as above for class 1 (`malignant').
\end{description}
%
The report provides a complete breakdown of the estimate for one sample into the contribution of the marginals and prototypes.
The multivariate structure of the feature space is replaced by a simple list that can be filtered and sorted by different criteria to identify major contributors.
While the total number of prototypes can be much larger than in the example, most tend to have negligible impact on any particular case.\par
%
For the computation of the `dominant set' indicator, we use the following procedure:
%
\begin{algorithm}[Dominant set]~
\label{alg_dominant_set}
%
\begin{enumerate}
\item Sort the prototypes in descending order of impact and assign a rank to each.
Prototypes with the same impact receive the same rank.
%
\item For each rank $r$, compute $p_{r,k}$ as sum of additive contributions (columns `p class 0', etc.\ in the report) to the probability estimate for class $k$ from the marginal distribution and prototypes up to rank $r$.
These satisfy $0<\sum_{k=0}^{K_1}p_{r,k}\leq 1$.
The remainder $t_r:=1-\sum_{k=0}^{K-1}p_{r,k}$ is the total contribution of prototypes with rank greater than $r$.
%
\item For each rank $r$, denote by $k_{1,r}$ and $k_{2,r}$ the classes with the largest and second largest value of $p_{r,k}$.
Determine the set  $R:=\{r:p_{r,k_{1_r}}-p_{r,k_{2,r}}>t_r\}$.
%
\item If $R$ is empty, the dominant set is empty.
Otherwise, all prototypes with a rank less than or equal to the minimal element in $R$ form the dominant set.
\end{enumerate}
\end{algorithm}
%
\begin{remark}
By constructions, if $R$ is not empty, it contains all ranks greater than or equal to its minimal element and all share the same value for $k_{1,r}$.
Thus, it contains all ranks where the remainder $t_r$ is too small to affect which class is assigned the highest probability.
\end{remark}
%
In the example report, the prototypes of the dominant set together with the marginal distribution contribute 53 percentage points of the 99 \% probability estimated for class 0.
Thus, class 0 would be assigned the highest probability even if all remaining prototypes belonged to class 1.
The purpose of the dominant set is to serve as a guideline which prototypes need to be reviewed for a better understanding of a particular result.\par
%
\begin{figure}
\caption{`Batch 1 map' of cancer data showing prototype impact}
\label{fig_batch_1_map_impact}
%
\begin{center}
\includegraphics[height=0.4\textheight]{figures/cancer_batch_1_map_impact.pdf}
\end{center}
\end{figure}
%
The impact of prototypes on a particular estimate can also be visualized in the `batch 1 map'.
Figure \ref{fig_batch_1_map_impact} does this for the case from the example report.
The size of each circle is proportional to the impact of the prototype, numbers refer to the sample index.
%
\subsection{Combining SHAP values and geometric analysis}
\label{sec_shap_values}
%
So far, we have looked at model-specific explanations for proset estimators on their own.
In this section, we combine them with a model-agnostic approach to gain additional insight into the model structure.
We choose the SHAP (SHapley Additive exPlanations) method for assessing feature importance proposed by Lundberg and Lee \cite{Lundberg_17}.
SHAP provides explanations for individual estimates and is the only additive feature attribution method that has certain desirable properties.
A downside is that the computational effort grows exponentially in the number of features, although an efficient algorithm exists for decision trees \cite{Lundberg_18}.
To facilitate usage of SHAP, our implementation of proset allows `shrinking' the model to take only the features with nonzero weights as input.\par
%
SHAP explains an estimate by comparing the sample of interest to a reference point.
The relative change in the estimator between the points is attributed to the relative change in the features.
There appears to be little research so far on the best choice of reference point, but Izzo et.\ al.\ \cite{Izzo_20} consider the issue.
The authors point out that the common choice of using the origin of the feature space as baseline is not satisfactory.
For a binary classifier, they recommend to select a reference point on the decision boundary between the two classes.
They refer to this as a `neutral baseline', since the algorithm is indifferent which class to choose.
We cannot follow this approach, as we want to study the probability estimate itself without reference to a decision rule\footnote{
We do use the `naive' decision rule that selects the class with the highest estimated probability to define bins for Algorithm \ref{alg_bins} and to compute balanced accuracy as a secondary metric for the benchmark study.
See the discussion in the next paragraph why we do not consider this rule for further analysis.
}.
Also, several of the benchmark cases have more than two classes and it is not certain that a point can be found that is `neutral' with regards to all of them at once.\par
%
We believe that classification should always be carried out in two stages by combining a stochastic model with a decision rule.
The purpose of the former is to accurately estimate the probability distribution with no regard to the practical problem we want to solve.
The purpose of the latter is to optimize the expected value of a problem-specific utility or loss function under the estimated distribution.
Any algorithm that conflates the two stages implicitly applies an arbitrary decision rule that is likely to yield sub-optimal results.
As an example, consider a credit scoring algorithm that estimates the default probability for loans.
It is doubtful that the lending company wants to grant loans to all applicants with default probability less than 50 \%.
A more thorough discussion of this issue can be found, e.g., in a blog article by Harrell \cite{Harrell_20}.\par
%
For the above reason, we want to find a suitable reference point for explaining the estimated probabilities themselves regardless of any decision rule that could be applied later.
We expect such a point to have the following three properties:
%
\begin{enumerate}
\item The point itself represents a feasible combination of features.
%
\item The point is not extreme in terms of the feature space.
For every feature, there are training samples that have either a higher or lower value.
%
\item The point is not extreme in terms of the estimated probabilities.
For every class, there are training samples that have either a higher or lower estimate.
\end{enumerate}
%
\begin{remark}
\begin{enumerate}
\item Property 1 acknowledges that the baseline is part of the explanation and should have no `unnatural' characteristics.
There is less risk of misinterpreting the feature importance if they are computed with respect to a relatable feature vector.
%
\item Properties 2 and 3 mean that SHAP can explore changes of the input and output in all directions.
%
\item Property 3 can be viewed as a weaker notion of the `neutral baseline' proposed in \cite{Izzo_20}.
%
\item Properties 1 and 2 are mutually exclusive for binary features.
Here, we prefer property 1, i.e., the baseline should be a genuine case and not a hypothetical half-way point.
\end{enumerate}
\end{remark}
%
One way to satisfy the first condition is to use an actually observed point as reference.
The first two conditions imply that a medoid\footnote{
A medoid is any point from a set for which the average absolute distances to all other points is minimal.
In one dimension, a medoid is also a median of the data.
} of the feature vectors for the training data is a suitable choice.
Likewise, condition 1 and 3 suggest to use a point whose estimated probabilities are a medoid of the probability vectors for the training data.
We thus propose the following procedure for choosing a baseline:
%
\begin{algorithm}[SHAP baseline]~
\label{alg_shap_baseline}
%
\begin{enumerate}
\item Rank all training samples in increasing order of average absolute distance to all other training samples in the feature space.
Use only features selected by proset to compute distances.
%
\item Rank all training samples in increasing order of average absolute distance to all other training samples in the space of estimated probability vectors.
%
\item Combine both rankings using Borda's rule\footnote{
Borda's rule converts rankings to scores by assigning each candidate half a point for each other candidate ranked the same, plus a full point for each candidate ranked worse.
Scores are totaled across rankings and the winners are the candidates with the most points.
} and use the winner as reference.
In case of a tie, choose the candidate whose probability vector has the highest entropy.
If still tied, use the point with the lowest sample index.
\end{enumerate}
\end{algorithm}
%
\begin{remark}
\begin{enumerate}
\item We do not recommend to combine the active features and estimated probabilities into a single vector.
The medoid property depends on the scaling of of individual variables and there is no obvious common scale.
Also, if there are many more features than classes, the former might dominate the decision.
Computing two separate rankings and using Borda's rule results in a choice that balances properties 2 and 3.
%
\item The method requires computing pairwise distances for all training samples, which may be unattractive for large problems.
In this case, we recommend subsampling.
\end{enumerate}
\end{remark}
%
\begin{figure}
\caption{`Batch 1 map' of cancer data showing reference point}
\label{fig_batch_1_map_reference}
%
\begin{center}
\includegraphics[height=0.4\textheight]{figures/cancer_batch_1_map_training_data_reference.pdf}
\end{center}
\end{figure}
%
\begin{figure}
\caption{SHAP feature importance}
\label{fig_feature_importance}
%
\begin{center}
\subfloat[\textbf{Global importance}]{\includegraphics[width=0.99\textwidth]{figures/cancer_global_shap.pdf}}\\
\subfloat[\textbf{Misclassified case}]{\includegraphics[width=0.99\textwidth]{figures/cancer_example_shap.pdf}}
\end{center}
\end{figure}
%
\begin{figure}
\caption{Scatter plots for important features}
\label{fig_scatter_important}
%
\begin{center}
\subfloat[\textbf{Global importance}]{\includegraphics[height=0.4\textheight]{figures/cancer_scatter_global_shap.pdf}}\\
\subfloat[\textbf{Misclassified case}]{\includegraphics[height=0.4\textheight]{figures/cancer_scatter_example_shap.pdf}}
\end{center}
\end{figure}
%
\begin{figure}
\caption{Probability surfaces for important features}
\label{fig_probability_important}
%
\begin{center}
\subfloat[\textbf{Global importance}]{\includegraphics[height=0.4\textheight]{figures/cancer_surface_global_shap.pdf}}\\
\subfloat[\textbf{Misclassified case}]{\includegraphics[height=0.4\textheight]{figures/cancer_surface_example_shap.pdf}}
\end{center}
\end{figure}
%
If we apply the above rule to the cancer data, it selects a baseline near the center of the point cloud of training samples.
The estimated probability for being `benign' is 92 \%, which is far from the 50 \% that would be achievable given the structure of the data.
However, the data is unbalanced in favor of `benign' cases and we have seen for the misclassified case in the previous section that probabilities in excess of 92 \% are achievable.
Figure \ref{fig_batch_1_map_reference} shows the reference point in a `batch 1 map' together with training data and the misclassified case we want to explain.\par
%
Figure \ref{fig_feature_importance} shows the results of SHAP analysis for the whole test data and the misclassified case.
Note that the global values are just the averages of the absolute scores of each feature across all samples.
Ordering the features by descending global importance yields the same sequence as for the feature weights, but we do not believe that this necessarily holds for every data set.
The two most important features globally are `worst radius' and `worst concave points'.
For the misclassified case, the estimate for class `benign' is shifted from 92 \% (baseline) to 99 \% because of the extremely low values for `worst concave points' and `worst smoothness'.
Figure \ref{fig_scatter_important} shows the bivariate scatter plots for both pairs of features, this time with training data as supplementary information.
As an alternative, we can study the probability surface resulting for the most important features if the other features are fixed at their values for the misclassified case.
This is shown in Figure \ref{fig_probability_important}.\par
%
\clearpage
%
\begin{figure}
\caption{`Batch 1 map' for digits data showing test samples}
\label{fig_batch_1_map_test_digits}
%
\begin{center}
\includegraphics[width=0.95\textheight, angle=90]{figures/digits_batch_1_map.pdf}
\end{center}
\end{figure}
%
\begin{figure}
\caption{Explanation combining prototypes and SHAP values}
\label{fig_impact_shap}
%
\begin{center}
\includegraphics[width=0.95\textheight, angle=90]{figures/digits_8_vs_1_explanation.pdf}
\end{center}
\end{figure}
%
As a final example, we show how to create an explanation for the digits data set that relies both on prototypes with high impact and SHAP values.
This classification problem is more complex than the one for cancer data.
Figure \ref{fig_batch_1_map_test_digits} presents the `batch 1 map' including test data and a reference point selected using the ranking rule.
Several classes are well differentiated in this plot, but a two-dimensional representation clearly cannot capture the whole structure.\par
%
Figure \ref{fig_impact_shap} is a custom plot for explaining a particular misclassified digit.
It shows the case itself -- an `8' for which the highest probability is estimated for class `1' -- as well as the five prototypes with the largest impact on the result.
The second row overlays the SHAP values for the probability estimate of the true class `8', the third does the same for class `1'.
A red color indicates that the estimate is increased because of the grayscale value of the associated pixel, a blue color that it is decreased.
Note that the estimated probabilities for the baseline are 61 \% for class `8', 27 \% for class `3', and negligible for the other digits.
Thus, the SHAP values indicate the importance of the features for a shift away from this distribution towards 60 \% for class `1' and 32 \% for class `8'.
%
\endinput


\subsection{Exponential function}\label{subsec:exponential_function}

\begin{definition}\label{def:exponential_function}
  We define the \term{exponential function}
  \begin{equation}\label{def:exponential_function/series}
    \exp(z) \coloneqq \sum_{k=0}^\infty \frac {z^k} {k!}
  \end{equation}
  and \term{Euler's number}
  \begin{equation*}
    e \coloneqq \exp(1) = \sum_{i=0}^k \frac 1 {k!}.
  \end{equation*}

  \Fullref{thm:def:exponential_function/interpolates_power} justifies the notation \( e^z = \exp(z) \).
\end{definition}
\begin{proof}
  We will show that \( \exp(z) \) converges everywhere. By \fullref{thm:power_series_radius_of_convergence}, the radius of convergence is
  \begin{equation*}
    \limsup_{k \to \infty} \frac {k!} {(k-1)!}
    =
    \limsup_{k \to \infty} k
    =
    +\infty
  \end{equation*}

  Hence, the radius of convergence of \( \exp(x) \) is infinite.
\end{proof}

\begin{proposition}\label{thm:def:exponential_function}
  The exponential function \( \exp(z) \) has the following basic properties (not that we do not use the notation \( e^z \) here in order to reduce confusion with yet-undefined power \hyperref[def:power_function]{functions}):

  \begin{thmenum}
    \thmitem{thm:def:exponential_function/eulers_identity} (Euler's identity)
    \begin{equation*}
      \exp(i \pi) = -1.
    \end{equation*}

    \thmitem{thm:def:exponential_function/derivative} \( \exp(z) \) is its own derivative.

    \thmitem{thm:def:exponential_function/homomorphism} \( \exp(x + y) = \exp(x) \exp(y) \). Stated in another way, \( \exp \) is a homomorphism from the additive group of \( \BbbC \) to the multiplicative group.

    \thmitem{thm:def:exponential_function/interpolates_power} The notation \( \exp(x) \) is consistent with iterated multiplication as defined in \fullref{def:semiring/identity}, that is, \( \exp(n) = \underbrace{e \cdot \ldots \cdot e}_{n \text{times}} \) and for positive integers \( n \), \( \exp(n) =  \) and \( \exp(-n) =\tfrac 1 {\exp(n)} \).

    \thmitem{thm:def:exponential_function/negative_power}
    \begin{equation*}
      \exp(z) = \frac 1 {\exp(-z)}.
    \end{equation*}

    \thmitem{thm:def:exponential_function/real_positive} For real \( t \), \( e^t \) is a positive real number.

    \thmitem{thm:def:exponential_function/conjugate} \( \overline{\exp(z)} = \exp(\overline{z}) \).

    \thmitem{thm:def:exponential_function/unit_circle} For any \( c \in \BbbR \), the function \( t \mapsto \exp(it) \) is a bijection between any half-open interval \( [c, c + 2\pi) \) and the unit circle in \( \BbbC \).

    \thmitem{thm:def:exponential_function/real_bijective} \( t \mapsto \exp(t) \) is a bijection from \( \BbbR \) to \( [0, \infty) \).

    \thmitem{thm:def:exponential_function/bijective} For any \( c \in \BbbR \), \( \exp(z) \) is a bijection between the strip \( S \coloneqq \{ a + bi \colon c \leq b < c + 2\pi \} \) and the complex plane \( \BbbC \setminus \{ 0 \} \).

    \thmitem{thm:def:exponential_function/periodic} \( \exp(z) \) is \( 2i\pi \)-\hyperref[def:periodic_function]{periodic}.

    \thmitem{thm:def:exponential_function/compound_interest} For nonnegative real \( t \geq 0 \) we have
    \begin{equation*}
      \exp(t) = \lim_{n \to \infty} \left(1 + \frac t n \right)^n
    \end{equation*}
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:exponential_function/eulers_identity} By \fullref{eq:thm:trigonometric_function_basic_roots/pi} and \fullref{thm:exponential_trigonometric_identities/eulers_formula}, we have
  \begin{equation*}
    \exp(i\pi) = \cos(\pi) + i\sin(\pi) = -1.
  \end{equation*}

  \SubProofOf{thm:def:exponential_function/derivative} Follows from \fullref{thm:power_series_are_locally_uniform_convergent} and \fullref{thm:derivative_limit_exchange}.

  \SubProofOf{thm:def:exponential_function/homomorphism} The Cauchy product of \( \exp(x) \) and \( \exp(y) \) is
  \begin{balign*}
    \exp(x) \exp(y)
     & =
    \left( \sum_{k=0}^\infty \frac {x^k} {k!} \right) \left( \sum_{k=0}^\infty \frac {y^k} {k!} \right)
    =                                       \\ &=
    \sum_{k=0}^\infty \sum_{m=0}^k \frac {x^m} {m!} \frac {x^{k-m}} {(k-m)!}
    =                                       \\ &=
    \sum_{k=0}^\infty \frac 1 {k!} \sum_{m=0}^k \binom{k}{m} x^m y^{k-m}
    \reloset {\ref{thm:binomial_theorem}} = \\ &=
    \sum_{k=0}^\infty \frac {(x + y)^k} {k!}
    =
    \exp(x + y).
  \end{balign*}

  \SubProofOf{thm:def:exponential_function/interpolates_power} We use induction on \( n \) to prove \( \exp(n) = e^n \). The case \( \exp(0) = 1 \) is obvious. If we assume that \( \exp(n) = e^n \), by \fullref{thm:def:exponential_function/homomorphism}, we have
  \begin{equation*}
    \exp(n + 1)
    =
    \exp(n) \exp(1)
    =
    e^n \cdot e
    =
    e^{n+1}.
  \end{equation*}

  Note that this works for negative \( n \) too.

  \SubProofOf{thm:def:exponential_function/negative_power} Note that
  \begin{equation*}
    1 = \exp(0) = \exp(z - z) = \exp(z) \exp(-z),
  \end{equation*}
  hence
  \begin{equation*}
    \exp(-z) = \frac 1 {\exp(z)}.
  \end{equation*}

  \SubProofOf{thm:def:exponential_function/real_positive} For \( t > 0 \), the following
  \begin{equation*}
    \exp(t) = \sum_{k=0}^\infty \frac {t^k} {k!}
  \end{equation*}
  is a series of positive real numbers. To see its convergence, we apply \fullref{thm:dalamberts_ratio_test}:
  \begin{equation*}
    \frac {t^k} {k!} \cdot \frac {(k-1)!} {t^{k-1}}
    =
    \frac t k
    \xrightarrow[k \to \infty]{} 0.
  \end{equation*}

  Thus, \( \exp(t) \) is a nonnegative real number. Furthermore, since the sequence of partial sums is monotone, \( \exp(t) \) cannot be zero. Hence, for \( t > 0 \), we have \( \exp(t) > 0 \).

  Notice that \( \exp(t) \exp(-t) > 0 \), hence if \( \exp(t) > 0 \), then \( \exp(-t) > 0 \).

  \SubProofOf{thm:def:exponential_function/conjugate} By \fullref{thm:exponential_trigonometric_identities/eulers_formula},
  \begin{balign*}
    \overline{\exp(a + bi)}
     & \reloset {\ref{thm:def:exponential_function/homomorphism}} =
    \overline{\exp(a) \exp(bi)}
    \reloset {\ref{thm:def:exponential_function/real_positive}} =   \\ &=
    \exp(a) \overline{(\cos(b) + i\sin(b))}
    =                                                                      \\ &=
    \exp(a) (\cos(b) - i\sin(b))
    \reloset {\ref{thm:power_series_parity}} =                             \\ &=
    \exp(a) (\cos(-b) + i\sin(-b))
    =                                                                      \\ &=
    \exp(a) \exp(-bi)
    =                                                                      \\ &=
    \exp(a - bi)
    =                                                                      \\ &=
    \exp(\overline{a + bi}).
  \end{balign*}

  \SubProofOf{thm:def:exponential_function/periodic} By \fullref{thm:def:exponential_function/eulers_identity},
  \begin{equation*}
    \exp(x + 2i\pi) = \exp(x) \exp(2i\pi) = \exp(x).
  \end{equation*}

  Furthermore, this is also the minimal period. If we assume that \( \sin(x) \) has another period, say \( p \in (0, 2\pi) \), we would have \( \sin(p) = \sin(0) = 0 \) and \fullref{thm:trigonometric_identities/pythagorean_identity} would imply that \( \cos(p) \in \{ -1, 1 \} \). But then \( \cos(p) \) would be an extreme point for \( \cos \), which is not possible because \( \cos \) is convex in \( [0, 2\pi] \) and only has three extremal points --- \( 0, \pi, 2\pi \).

  \SubProofOf{thm:def:exponential_function/unit_circle} For \( c, t \in \BbbR \) we have
  \begin{equation*}
    \abs{\exp(it)}
    =
    \abs{\cos(t) + i\sin(t)}
    =
    \sqrt{\cos(t)^2 + \sin(t)^2}
    \reloset {\eqref{eq:thm:trigonometric_identities/pythagorean_identity}} =
    1.
  \end{equation*}

  Furthermore, if \( r \) is another real number,
  \begin{equation}
    \exp(ir)
    =
    \exp(i(t + (r - t)))
    =
    \exp(it) \exp(i(r - t)).
  \end{equation}

  It follows that \( \exp(ir) \neq \exp(it) \) if and only if \( \exp(i(r - t)) \neq 0 \). If \( t, r \in [c, c + 2\pi) \) and \( t \neq r \), this is satisfied.

  Hence, \( t \mapsto \exp(it) \) is indeed an injection of \( [c, c + 2\pi) \) into the unit circle of \( \BbbC \). It is also a surjection because of the intermediate value theorem.

  \SubProofOf{thm:def:exponential_function/real_bijective} First, assume that \( e^t \) is not injective on \( \BbbR \). Then there exist \( t, r \in \BbbR \), \( t \neq r \), such that \( e^t = e^r \). By \fullref{thm:def:exponential_function/real_positive}, both are positive real numbers. In particular, we can divide by \( e^t \) to obtain
  \begin{equation*}
    1
    =
    \frac {e^r} {e^t}
    \reloset {\ref{thm:def:exponential_function/negative_power}} =
    =
    e^r e^{-t}
    \reloset {\ref{thm:def:exponential_function/homomorphism}} =
    e^{r - t}.
  \end{equation*}

  We know that \( e^0 = 1 \) from \fullref{thm:def:exponential_function/interpolates_power}. Thus, it is enough to show that \( e^t = 1 \) if and only if \( t = 0 \).

  Assume that \( e^t = 1 \) holds for some \( t > 0 \). The partial sums are monotonely increasing, so in order for them to converge to \( 1 \), for any fixed index \( n \) we must have
  \begin{balign*}
    0  & \leq \sum_{k=0}^n \frac {t^k} {k!} = 1 + \sum_{k=1}^n \frac {t^k} {k!} \leq 1, \\
    -1 & \leq \sum_{k=1}^n \frac {t^k} {k!} \leq 0.
  \end{balign*}

  But \( \sum_{k=1}^n \frac {t^k} {k!} > 0 \) because \( t > 0 \). The obtained contradiction proves that \( e^t \neq 1 \) for positive \( t \).

  For negative \( t \), note that
  \begin{equation*}
    e^t e^{-t} = 1.
  \end{equation*}

  Since \( -t \) is positive, \( e^{-t} \neq 1 \) and hence \( e^t \neq 1 \).

  Therefore, the function \( t \mapsto e^t \) is injective on \( \BbbR \). It is also surjective onto \( \BbbR^{>0} \) because of the intermediate value theorem.

  \SubProofOf{thm:def:exponential_function/bijective} Fix \( a + bi \in S_c \), that is, \( b \in [c, c + 2\pi) \). By \fullref{thm:def:exponential_function/homomorphism},
  \begin{equation*}
    e^{a + bi} = e^a e^{bi}.
  \end{equation*}

  By \fullref{thm:def:exponential_function/unit_circle}, \( b \mapsto e^{bi} \) is injective for \( b \in [c, c + 2\pi) \) and by \fullref{thm:def:exponential_function/real_bijective}, \( a \mapsto e^a \) is injective on \( \BbbR \). It follows that their product is also injective.

  \SubProofOf{thm:def:exponential_function/compound_interest}\mcite[3.31]{Rudin1976Principles}By \fullref{thm:binomial_theorem},
  \begin{balign*}
    \left(1 + \frac t n \right)^n
     & =
    \sum_{k=0}^n \binom{n}{k} \left(\frac t n\right)^k 1^{n-k}
    =    \\ &=
    \sum_{k=0}^n \frac {n!} {(n-k)! k!} \frac {t^k} {n^k}
    =    \\ &=
    \sum_{k=0}^n \frac {n!} {(n-k)! n^k} \frac {t^k} {k!}
    =    \\ &=
    \sum_{k=0}^n \left[ \prod_{j=1}^k \left(1 - \frac {k+j} n \right) \right] \frac {t^k} {k!}.
  \end{balign*}

  Fix an index \( m \). Since the series is nonnegative, there exists an index \( N \) such that for \( n \geq N \)
  \begin{equation*}
    \sum_{k=0}^m \frac {t^k} {k!}
    \leq
    \sum_{k=0}^n \left[ \prod_{j=1}^k \left(1 - \frac {k+j} n \right) \right] \frac {t^k} {k!}.
  \end{equation*}

  Note that
  \begin{equation*}
    \left[ \prod_{j=1}^k \left(1 - \frac {k+j} n \right) \right] \frac {t^k} {k!}
    \leq
    \frac {t^k} {k!},
  \end{equation*}
  hence
  \begin{equation*}
    \sum_{k=0}^m \frac {t^k} {k!}
    \leq
    \sum_{k=0}^n \left[ \prod_{j=1}^k \left(1 - \frac {k+j} n \right) \right] \frac {t^k} {k!}
    \leq
    \sum_{k=0}^n \frac {t^k} {k!}.
  \end{equation*}

  By \fullref{thm:squeeze_lemma},
  \begin{equation*}
    \lim_{n \to \infty} \left(1 + \frac t n \right)^n
    =
    \lim_{n \to \infty} \sum_{k=0}^n \frac {t^k} {k!}
    =
    \exp(t).
  \end{equation*}
\end{proof}

\begin{definition}\label{def:logarithm}
  Fix \( c \in \BbbR \). Unless specified otherwise, we assume \( c = 0 \).

  We define the \term{natural logarithm} \( \log(x) \) as \hyperref[def:multi_valued_function/inverse]{inverse function} of \( e^x \) from \( \BbbC \setminus \{ 0 \} \) to the strip \( S_c \coloneqq \{ a + bi \colon c \leq b < c + 2\pi \} \).

  We also define the \term{base \( b \) logarithm} \( \log_b(x) \) for \( b > 0 \) over the same domain as
  \begin{equation*}
    \log_b(x) \coloneqq \frac {\log(x)} {\log(b)}.
  \end{equation*}
\end{definition}
\begin{proof}
  The well-definedness follows from \fullref{thm:def:exponential_function/bijective}.
\end{proof}

\begin{proposition}\label{thm:def:logarithm}
  \hfill
  \begin{thmenum}
    \thmitem{thm:def:logarithm/homomorphism} \( \log(xy) = \log(x) \log(y) \)
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:logarithm/homomorphism} Follows from \fullref{thm:def:exponential_function/homomorphism}.
\end{proof}

\begin{definition}\label{def:power_function}
  For each positive real number \( y > 0 \), we define the \term{power function}
  \begin{equation*}
    x^y \coloneqq e^{y \ln x}
  \end{equation*}
  as a function of \( x \).
\end{definition}

\begin{proposition}\label{thm:def:power_function}
  \hfill
  \begin{thmenum}
    \thmitem{thm:def:power_function/composition} \( (x^y)^z = x^{yz} \).
    \thmitem{thm:def:power_function/derivative} \( D_x(x^y) = \log(x) x^y \).
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:power_function/composition}
  \begin{equation*}
    (x^y)^z
    =
    e^{z \log(e^{y \log(x)})}
    =
    e^{z y \log(x)}
    =
    x^{yz}.
  \end{equation*}

  \SubProofOf{thm:def:power_function/derivative} Using the chain rule for differentiation, we obtain
  \begin{equation*}
    D_x(x^y) = D_x(e^{\log(y) x}) = \log(x) e^{\log(y) x} = \log(x) x^y.
  \end{equation*}
\end{proof}
\begin{proposition}\label{thm:exponential-trigonometric_identities}
  We have the following exponential-trigonometric identities:
  \thmitem{thm:exponential_trigonometric_identities/eulers_formula} (Euler's formula) For any \( z \in \BbbC \),
  \begin{equation}\label{thm:exponential_trigonometric_identities/eulers_formula/identity}
    e^{iz} = \cos(z) + i \sin(z).
  \end{equation}

  \thmitem{thm:exponential_trigonometric_identities/inverse_eulers_formula} (Inverse Euler's identities) For any \( z \in \BbbC \),
  \begin{balign}
    \sin(z) & = \real(e^z) = \frac {e^{iz} - e^{-iz}} {2i} \label{thm:exponential_trigonometric_identities/inverse_eulers_formula/sin} \\
    \cos(z) & = \imag(e^z) = \frac {e^{iz} + e^{-iz}} 2 \label{thm:exponential_trigonometric_identities/inverse_eulers_formula/cos}
  \end{balign}

  \thmitem{thm:exponential_trigonometric_identities/de_moivre} (De Moivre's formula) For any complex number \( z \) and any nonnegative integer \( n \),
  \begin{equation}\label{thm:exponential_trigonometric_identities/de_moivre/identity}
    (\cos(z) + i \sin(z))^n = \cos(nz) + i \sin(nz).
  \end{equation}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:exponential_trigonometric_identities/eulers_formula} Simply note that \fullref{def:exponential_function} is a termwise sum of \fullref{def:trigonometric_functions/sine} and \fullref{def:trigonometric_functions/cosine}, therefore \fullref{thm:exponential_trigonometric_identities/eulers_formula/identity} holds.

  \SubProofOf{thm:exponential_trigonometric_identities/inverse_eulers_formula} Follows from \fullref{thm:exponential_trigonometric_identities/eulers_formula}.

  \SubProofOf{thm:exponential_trigonometric_identities/de_moivre} From \fullref{thm:exponential_trigonometric_identities/eulers_formula},
  \begin{equation*}
    (\cos(z) + i \sin(z))^n
    =
    {e^{iz}}^n
    \reloset {\ref{thm:def:power_function/composition}} {=}
    =
    e^{i(zn)}
    =
    \cos(nz) + i \sin(nz).
  \end{equation*}
\end{proof}


\chapter*{Appendix C: Derivative Needed for the Minimization}
\label{annexB}
\addcontentsline{toc}{chapter}{Appendix C: Derivative Needed for the
Minimization}

\subsection*{The error function}
 The error function is written erf(x) and the complementary error function
 erfc(x). Their definitions are:
\begin{eqnarray}
  \mbox{erf(x)} = \frac{2}{\sqrt{\pi}}\int_{0}^{x} e^{-t^{2}} dt
\end{eqnarray}

\begin{eqnarray}
  \mbox{erfc}(x) = 1-\mbox{erf}(x) = \frac{2}{\sqrt{\pi}}\int_{x}^{\infty} e^{-t^{2}} dt
\end{eqnarray}

These functions have the following limits and symmetries:
\begin{eqnarray}
   \begin{tabular}{c|c}
   \hline
    \mbox{erf}(0) = 0         & $ \mbox{erf}(\infty) = 1 $  \\
   \mbox{erfc}(0) = 1        & $  \mbox{erfc}(\infty) = 0 $ \\
   \mbox{erf}(-x) = \mbox{erf}(x)   &   \mbox{erfc}(-x) = 2-\mbox{erfc}(x) \\
   \hline
   \end{tabular}
\end{eqnarray}

\subsection*{N1-MSE}
Now we compute the contribution of the wavelet coefficient $x$ to the noise information:
\begin{eqnarray}
h_{n}(x) = \frac{x^2}{2\sigma^2}\mbox{erfc}(\frac{x}{\sqrt{2}\sigma})
\end{eqnarray}

\begin{eqnarray}
\frac{ d h_{n}(x)}{dx} = \frac{x}{\sigma^2}\mbox{erfc}(\frac{x}{\sqrt{2}\sigma}) + \frac{x^2}{2\sigma^2} \frac{\partial \mbox{erfc}(\frac{x}{\sqrt{2}\sigma})}{\partial x}
\end{eqnarray}
We derive the function erfc:
\begin{eqnarray}
\frac{\partial \: \mbox{erfc}(x)}{\partial x} = - \frac{2}{\sqrt{\pi}}   e^{-x^{2}}
\end{eqnarray}
then
\begin{eqnarray}
\frac{ d h_{n}(x)}{dx} = \frac{x}{\sigma^2}\mbox{erfc}(\frac{x}{\sqrt{2}\sigma})  -\frac{x^2}{\sqrt{2\pi}\sigma^3}e^{-\frac{x^{2}}{2\sigma^2}}
\end{eqnarray}

In order to minimize the functional~\ref{eqn_func1}, we may want to calculate
the derivative of $h_s(x-y)$, $h_s(x-y)$ measuring the amount of information
contained in the residual ($y$ being the data).
\begin{eqnarray}
h_s(x-y) = \frac{(y-x)^2}{2\sigma^2}\mbox{erf}(\frac{y-x}{\sqrt{2}\sigma})
\end{eqnarray}
Denoting $z = y - x$, we have
\begin{eqnarray}
h_s(z) = \frac{z^2}{2\sigma^2}\mbox{erf}(\frac{z}{\sqrt{2}\sigma})
\end{eqnarray}
\begin{eqnarray}
\frac{ d h_{s}(z)}{dz} & = & \frac{z}{\sigma^2}\mbox{erf}(\frac{z}{\sqrt{2}\sigma}) + \frac{z^2}{2\sigma^2} \frac{\partial \mbox{erf}(\frac{z}{\sqrt{2}\sigma})}{\partial z}  \\
                & = & \frac{z}{\sigma^2}\mbox{erf}(\frac{z}{\sqrt{2}\sigma}) + \frac{z^2}{\sqrt{2\pi}\sigma^3} e^{-\frac{z^{2}}{2\sigma^2}}
\end{eqnarray}
and
\begin{eqnarray}
\frac{ d h_{s}(x-y)}{dx}  =  - \frac{ d h_{s}(z)}{dz}
\end{eqnarray}

\subsection*{N2-MSE}
We compute the contribution of the wavelet coefficient $x$ to the noise information:
\begin{eqnarray}
h_{n}(x) =
\frac{1}{\sigma^{2}}\int_{0}^{x} t \: \mbox{erfc}( \frac{x-t}{\sqrt{2}\sigma}) dt
\end{eqnarray}

\begin{eqnarray}
\frac{d h_{n}(x)}{dx} &  = &  h_{n}(x+dx) - h_{n}(x)  \\
   &  = & \frac{1}{\sigma^{2}} \int_{0}^{x+dx}
             \mbox{erfc}(\frac{x+dx-t}{\sqrt{2}\sigma}) dt -
\frac{1}{\sigma^{2}}\int_{0}^{x} \mbox{erfc} (\frac{x-t}{\sqrt{2}\sigma}) dt
\end{eqnarray}

\begin{eqnarray}
\frac{d h_{n}(x)}{dx} =
\frac{x}{\sigma^{2}}  \, \mbox{erfc}( \frac{x-x}{\sqrt{2}\sigma})  +
\frac{1}{\sigma^{2}}\int_{0}^{x}
          \frac{ \partial \: t \, \mbox{erfc} (\frac{x-t}{\sqrt{2}\sigma})}{ \partial x} dt
\end{eqnarray}

Now, because erfc(0) = 1  we have:
\begin{eqnarray}
\frac{d H_{n}(x)}{dx} =
\frac{x}{\sigma^{2}}   +
\frac{1}{\sigma^{2}}\int_{0}^{x}
          \frac{ \partial \: t \, \mbox{erfc} (\frac{x-t}{\sqrt{2}\sigma})}{ \partial x} dt
\end{eqnarray}

We derive the function erfc:
\begin{eqnarray}
\frac{\partial \: \mbox{erfc}(x)}{\partial x} = - \frac{2}{\sqrt{\pi}}   e^{-x^{2}}
\end{eqnarray}

\begin{eqnarray}
\frac{ \partial\:  \mbox{erfc}( \frac{(x-t)}{\sqrt{2}\sigma} )}{\partial x} =
-\frac{2}{\sqrt{\pi}} \frac{1}{\sqrt{2}\sigma}
  e^{-\frac{(x-t)^{2}}{2\sigma^{2}}} =
 -\sqrt{\frac{2}{\pi}} \frac{1}{\sigma} e^{-\frac{(x-t)^{2}}{2\sigma^{2}}}
\end{eqnarray}

Now we deduce for the derivative of $h_n$:

\begin{eqnarray}
\frac{d h_{n}(x)}{dx} =
\frac{x}{\sigma^{2}}   +
\frac{1}{\sigma^{2}}\int_{0}^{x}
 -\sqrt{\frac{2}{\pi}}\frac{1}{\sigma}t \, e^{-\frac{(x-t)^{2}}{2\sigma^{2}}}dt
\end{eqnarray}

\begin{eqnarray}
\frac{d h_{n}(x)}{dx} =
\frac{x}{\sigma^{2}}   +
\frac{1}{\sigma^{3}}\sqrt{\frac{2}{\pi}} \int_{0}^{x} t \, e^{-\frac{(x-t)^{2}}{2\sigma^{2}}}dt
\end{eqnarray}

We create the variable J
\begin{eqnarray}
J = \int_{0}^{x} t \, e^{-\frac{(x-t)^{2}}{2\sigma^{2}}}dt
\end{eqnarray}

We create the variable u
\begin{eqnarray}
\begin{array}{cc}
  u = \frac{t-x}{\sqrt{2}\sigma}  &  t = x+u \, \sqrt{2}\sigma         \\
   & dt = \sqrt{2}\sigma du         \\
  t=0 \Rightarrow u = \frac{-x}{\sqrt{2}\sigma}  &
  t=x \Rightarrow u = 0                     \\
   \end{array}
\end{eqnarray}

The variable J can be written with u
\begin{eqnarray}
 J = \int_{\frac{-x}{\sqrt{2}\sigma} }^{0} (x+u \,\sqrt{2}\sigma) e^{-u^{2}}\sqrt{2}\sigma du
\end{eqnarray}
\begin{eqnarray}
 J = \sqrt{2}\sigma x \int_{\frac{-x}{\sqrt{2}\sigma} }^{0}
 e^{-u^{2}} du  +
 2 \sigma^{2} \int_{\frac{-x}{\sqrt{2}\sigma} }^{0} u \,
 e^{-u^{2}} du
\end{eqnarray}

The first part of J can be rewritten as:
\begin{eqnarray}
 J_{0} = \sqrt{2}\sigma x \int_{0}^{\frac{x}{\sqrt{2}\sigma} }
 e^{-u^{2}} du
\end{eqnarray}
$ J_{0} $ can be expressed with the error function.
\begin{eqnarray}
 J_{0} = \sqrt{2}\sigma \frac{\sqrt{\pi}}{2} x  \mbox{erf}(\frac{x}{\sqrt{2}\sigma})
 J_{0} = \sigma \sqrt{\frac{\pi}{2}} \: x \:  \mbox{erf}(\frac{x}{\sqrt{2}\sigma})
\end{eqnarray}
Now the second part of J is obvious
\begin{eqnarray}
 J_{1} = 2 \sigma^{2} \int_{\frac{-x}{\sqrt{2}\sigma} }^{0} u \,
 e^{-u^{2}} du
\end{eqnarray}
 or
\begin{eqnarray}
\frac{d e^{-u^{2}}} {du} = -2 u e^{-u^{2}}
\end{eqnarray}

We replace
\begin{eqnarray}
 J_{1} = -\sigma^{2}  \int_{\frac{-x}{\sqrt{2}\sigma} }^{0} d(e^{-u^{2}})
\end{eqnarray}
\begin{eqnarray}
 J_{1} = \sigma^{2}  [e^{-\frac{x^{2}}{2\sigma^{2}}} - 1]
\end{eqnarray}

Now we can write J
\begin{eqnarray}
 J = J_{0} + J_{1}
  = \sigma \sqrt{\frac{\pi}{2}} \: x \:  \mbox{erf}(\frac{x}{\sqrt{2}\sigma})
    + \sigma^{2}  [e^{-\frac{x^{2}}{2\sigma^{2}}} - 1 ]
\end{eqnarray}

Now we can write the derivative of $h_n$
\begin{eqnarray}
\frac{d h_{n}(x)}{dx} & = &
\frac{x}{\sigma^{2}}   -
\frac{1}{\sigma^{3}} \sqrt{\frac{2}{\pi} } \: J \\
                   & = &   \frac{x}{\sigma^{2}} -
  \frac{x}{\sigma^{2}}\: x \:  \mbox{erf}(\frac{x}{\sqrt{2}\sigma})
  + \frac{1}{\sigma} \sqrt{\frac{2}{\pi} } \:
      [1 - e^{-\frac{x{2}}{2\sigma{2}}}]  \\
                    & = &  \frac{x}{\sigma^{2}}\mbox{erfc}(\frac{x}{\sqrt{2}\sigma})
    + \frac{1}{\sigma} \sqrt{\frac{2}{\pi} } \:
      [1 - e^{-\frac{x^{2}}{2\sigma^{2}}}]
\end{eqnarray}

In order to minimize the functional~\ref{eqn_func1}, we may want to calculate
the derivative of $h_s(x \mid y)$, $h_s(x \mid
y)$ measuring the amount of information
contained in the residual ($y$ being the data).
\begin{eqnarray}
h_s(x \mid y) = \frac{1}{\sigma^{2}} \int_{0}^{y-x} t \mbox{ erf}(\frac{y-x-t}{\sqrt{2}\sigma}) dt
\end{eqnarray}
Denoting $z = y - x$, we have
\begin{eqnarray}
h_s(z) &  =  & \frac{1}{\sigma^{2}} \int_{0}^{z} t \mbox{ erf}(\frac{z-t}{\sqrt{2}\sigma}) dt \\
       &  =  & \frac{1}{\sigma^{2}} \int_{0}^{z} t dt - \frac{1}{\sigma^{2}} \int_{0}^{z} t \mbox{ erfc}(\frac{z-t}{\sqrt{2}\sigma}) dt
\end{eqnarray}
and
\begin{eqnarray}
\frac{d h_s(x)}{dx} = \frac{\partial h_s(z)}{\partial z} \frac{\partial z}{\partial x}
\end{eqnarray}
\begin{eqnarray}
\frac{\partial h_s(z)}{\partial z} & = \frac{z}{\sigma^2} - \frac{\partial h_n(z)}{\partial z}
 \end{eqnarray}
then
\begin{eqnarray}
\frac{d h_s(x)}{dx} & = & - \frac{y-x}{\sigma^2} + \frac{y-x}{\sigma^2}\mbox{erfc}(\frac{y-x}{\sqrt{2}\sigma}) + \sqrt{\frac{2}{\pi}}\frac{1}{\sigma}[1 - e^{-\frac{(y-x)^{2}}{2\sigma^{2}}}] \\
                    & = & - \frac{y-x}{\sigma^2} \mbox{erf}(\frac{y-x}{\sqrt{2}\sigma}) + \sqrt{\frac{2}{\pi}}\frac{1}{\sigma}[1 - e^{-\frac{(y-x)^{2}}{2\sigma^{2}}}]
\end{eqnarray}


\subsubsection{Seasonal and Trend Decomposition using Loess}
\label{stl}

A time series $y_t$ may exhibit different types of patterns; to fully capture
    each of them, the series must be decomposed.
Then, each component is forecast with a distinct model.
Most commonly, the components are the trend $t_t$, seasonality $s_t$, and
    remainder $r_t$.
They are themselves time series, where only $s_t$ exhibits a periodicity $k$.
A decomposition may be additive (i.e., $y_t = s_t + t_t + r_t$) or
    multiplicative (i.e., $y_t = s_t * t_t * r_t$); the former assumes that
    the effect of the seasonal component is independent of the overall level
    of $y_t$ and vice versa.
The seasonal component is centered around $0$ in both cases such that its
    removal does not affect the level of $y_t$.
Often, it is sufficient to only seasonally adjust the time series, and model
    the trend and remainder together, for example, as $a_t = y_t - s_t$ in the
    additive case.

Early approaches employed moving averages (cf., Sub-section \ref{ets}) to
    calculate a trend component, and, after removing that from $y_t$, averaged
    all observations of the same seasonal lag to obtain the seasonal
    component.
The downsides of this are the subjectivity in choosing the window lengths for
    the moving average and the seasonal averaging, the incapability of the
    seasonal component to vary its amplitude over time, and the missing
    handling of outliers.

The X11 method developed at the U.S. Census Bureau and described in detail by
    \cite{dagum2016} overcomes these disadvantages.
However, due to its background in economics, it is designed primarily for
    quarterly or monthly data, and the change in amplitude over time cannot be
    controlled.
Variants of this method are the SEATS decomposition by the Bank of Spain and
    the newer X13-SEATS-ARIMA method by the U.S. Census Bureau.
Their main advantages stem from the fact that the models calibrate themselves
    according to statistical criteria without manual work for a statistician
    and that the fitting process is robust to outliers.

\cite{cleveland1990} introduce a seasonal and trend decomposition using a
    repeated locally weighted regression - the so-called Loess procedure - to
    smoothen the trend and seasonal components, which can be viewed as a
    generalization of the methods above and is denoted by the acronym STL.
In contrast to the X11, X13, and SEATS methods, the STL supports seasonalities
    of any lag $k$ that must, however, be determined with additional
    statistical tests or set with out-of-band knowledge by the forecaster
    (e.g., hourly demand data implies $k = 24 * 7 = 168$ assuming customer
    behavior differs on each day of the week).
Moreover, the seasonal component's rate of change, represented by the $ns$
    parameter and explained in detail with Figure \ref{f:stl} in Section
    \ref{decomp}, must be set by the forecaster as well, while the trend's
    smoothness may be controlled via setting a non-default window size.
Outliers are handled by assignment to the remainder such that they do not
    affect the trend and seasonal components.
In particular, the manual input needed to calibrate the STL explains why only
    the X11, X13, and SEATS methods are widely used by practitioners.
However, the widespread adoption of concepts like cross-validation (cf.,
    Sub-section \ref{cv}) in recent years enables the usage of an automated
    grid search to optimize the parameters.
The STL's usage within a grid search is facilitated even further by its being
    computationally cheaper than the other methods discussed.


\documentclass[a4paper]{article}

% Language and Font Encoding
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{float}
\usepackage{caption}         % For captions
\usepackage{fancyhdr}        % For header and footer
\usepackage{rotating}
\usepackage{amssymb}
\usepackage{varwidth}

% Page size and margins
\usepackage[a4paper,top=3cm,bottom=2cm,left=2cm,right=2cm,marginparwidth=1.75cm]{geometry}

% Useful Packages
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{automata, positioning}
\usepackage{amsthm}

\DeclareMathOperator*{\argmax}{argmax}
\DeclareMathOperator*{\argmin}{argmin}
\DeclareMathOperator*{\softmax}{softmax}

\newcommand{\squeezeup}{\vspace{-2.5mm}}
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}

\usepackage[colorinlistoftodos]{todonotes}
\usepackage[colorlinks=true, allcolors=blue]{hyperref}

\pagestyle{fancy}
\lhead{Linear Algebra Notes}
\rhead{}

\begin{document}

\title{Linear Algebra: Vector Spaces, Subspaces, and Linear Maps}
\author{Ibrahim Akbar\\}
\maketitle

%--------------------------------------------------------------------------------------------------------Vector Space--------------------------------------------------------------------------------------------------------------------
\section{Vector Space}
\textbf{Definition:} A collection, $\mathcal{V}$, is known as a vector (linear) space over the field $\mathbb{F}$ if it is equipped with the operations $+:\mathcal{V}\times\mathcal{V} \rightarrow \mathcal{V}$ and $\cdot:\mathbb{F}\times\mathcal{V}\rightarrow\mathcal{V}$ such that the following properties hold.\\

\begin{itemize}
\item Closure under Vector Addition:  $\mathbf{u + v} \in \mathcal{V} \quad \forall \mathbf{u,v} \in \mathcal{V}$
\item Closure under Scalar Multiplication:  $\alpha \mathbf{v} \in \mathcal{V}  \quad \forall \mathbf{v} \in \mathcal{V}, \alpha \in \mathbb{F}$
\item $(\alpha + \beta)\mathbf{v} = \alpha \mathbf{v} + \beta \mathbf{v}$
\item $\alpha(\beta\mathbf{v}) = (\alpha\beta)\mathbf{v}$
\item $\alpha(\mathbf{u + v}) = \alpha\mathbf{u} + \alpha\mathbf{v} $
\item $\exists \{\mathbf{0}\} \hspace{2mm} \textnormal{s.t.} \hspace{2mm} \mathbf{v} +\{\mathbf{0}\} = \mathbf{v} \quad \forall\mathbf{v}\in\mathcal{V}$
\item For each $\mathbf{v}\in\mathcal{V}\quad\exists \mathbf{(-v)}\hspace{2mm} \textnormal{s.t.} \hspace{2mm} \mathbf{v} +\mathbf{(-v)} = \{\mathbf{0}\}$
\item $1\in\mathbb{F},\mathbf{v}\in\mathcal{V} \Rightarrow \mathbf{1v} = \mathbf{v}$
\end{itemize}

\subsection{Examples}
Some examples of vector spaces are: $\mathbb{R}^{n}, \mathbb{C}^{n}$

%----------------------------------------------------------------------------------------------------------Sub Space --------------------------------------------------------------------------------------------------------------------
\section{Subspace}
\textbf{Definition:} A subspace, $\mathcal{U}$ of a vector space, $\mathcal{V}$, over the field, $\mathbb{F}$, is a non-empty set such that,

\begin{itemize}
\item $\mathbf{u,v}\in\mathcal{W}\Rightarrow\mathbf{u+v}\in\mathcal{W}$
\item $\mathbf{v}\in\mathcal{W}\Rightarrow\alpha\mathbf{w}\in\mathcal{W},\forall\alpha\in\mathbb{F}$
\item $\exists \{\mathbf{0}\} \hspace{2mm} \textnormal{s.t.} \hspace{2mm} \mathbf{w} +\{\mathbf{0}\} = \mathbf{w} \quad \forall\mathbf{w}\in\mathcal{W}$
\end{itemize}

\subsection{Examples}
Some examples of subspaces are:

$$
V := \{x\in\mathbb{R}^{n}\mid x^{2} \geq 4x\}
$$

Note that $V' := \{x\in\mathbb{R}^{n}\mid x^{2} > 4x\}$ would not be a subspace nor would any polynomial that does not equal zero. Why?\\
In general, all spaces in $\mathbb{R}^{n}$ that are defined by a homogeneous system of linear equations adhere to these properties. For a visual/geometric representation it is a point, line, plane, hyper-plane that goes through the origin $\{\mathbf{0}\}$.

%----------------------------------------------------------------------------------------------------Linear Independence-----------------------------------------------------------------------------------------------------------------
\section{Linearly Independent}
\textbf{Definition:} A set of vectors $\{\mathbf{x}_{1},\mathbf{x}_{2},\ldots,\mathbf{x}_{n}\}$ is said to be linearly independent if:
$$
\sum_{i = 1}^{n}\alpha_{i}\mathbf{x}_{i} = 0 \iff \alpha_{i} = 0 \hspace{2mm} \forall i
$$

The vectors cannot be written as a linear combination of each other and thus cannot describe each other. Therefore it is the same as saying there is no redundant information in the vectors.

\subsection{Example}
Let $\mathcal{S} = \Bigg\{\begin{bmatrix}2\\1\\-1\\\end{bmatrix},\begin{bmatrix}3\\2\\-1\\\end{bmatrix},\begin{bmatrix}-4\\-2\\1\\\end{bmatrix}\Bigg\}$, then we see that $-2v_{1} + 0v_{2} = v_{3}$ and therefore $\mathcal{S}$ is not a linearly independent set.\\

%------------------------------------------------------------------------------------------------------------Span------------------------------------------------------------------------------------------------------------------------
\section{Span}
\textbf{Definition:} The span of a set of vectors $\mathcal{S} = \{\mathbf{x}_{1}, \mathbf{x}_{2}, \ldots, \mathbf{x}_{n}\}$ is the set of vectors, $\mathcal{V}$, that is described by all possible linear combinations of $\mathcal{S}$.
$$
V := span(\mathcal{S}) = \{y \mid y=\sum_{i = 1}^{n}\alpha_{i}x_{i}\hspace{2mm}\forall\alpha_{i}\in\mathbb{F}\}
$$

\subsection{Examples}
Given the vector $v = \begin{bmatrix}1\\0\\\end{bmatrix}$ over the field of all reals it would span the space
$$
\mathcal{S} = \bigg\{\begin{bmatrix}\alpha\\0\\\end{bmatrix}\mid \alpha\in\mathbb{R}\bigg\}
$$
Lets also consider $u = \begin{bmatrix}0\\1\\\end{bmatrix}$ in the same field. Together these would span
$$
\mathcal{T} = \bigg\{\begin{bmatrix}\alpha\\\beta\\\end{bmatrix}\mid\alpha,\beta\in\mathbb{R}\bigg\} = \mathbb{R}^{2}
$$

%------------------------------------------------------------------------------------------------------------Basis-----------------------------------------------------------------------------------------------------------------------
\section{Basis}
\textbf{Definition:} A basis for a vector space, $\mathcal{V}$, is a set of vectors, $\mathcal{T}$, such that it is the maximal linear independent set and minimal spanning set of $\mathcal{V}$.

% Existence of a Basis and Uniqueness of a Basis
Is a basis for a space unique?\\
Let their be two bas
Does a basis for a space exist?\\
\subsection{Examples}
A basis for $\mathbb{R}^{2}$ is $\begin{bmatrix} 0 & 1\\ 1 & 0\\ \end{bmatrix}$.\\

We need to show that the set of vectors are linearly independent and span the space of $\mathbb{R}^{2}$.
$$
\alpha_{1}v_{1} + \alpha_{2}v_{2} = 0 \iff \alpha_{i} = 0\hspace{2mm}i = \{1,2\}
$$
$$
\alpha_{1}\begin{bmatrix}0\\1\\\end{bmatrix} + \alpha_{2}\begin{bmatrix}1\\0\\\end{bmatrix} = 0
$$
$$
\Rightarrow
\begin{bmatrix}0\\\alpha_{1}\end{bmatrix} + \begin{bmatrix}\alpha_{2}\\0\\\end{bmatrix} = 0
$$
$$
\Rightarrow
\begin{bmatrix}\alpha_{2}\\\alpha_{1}\\\end{bmatrix} = 0 \Rightarrow \alpha_{1} = \alpha_{2} = 0
$$

To see that it spans $\mathbb{R}^{2}$ can be observed from the previous section.\\
%----------------------------------------------------------------------------------------------------------Linear Maps-------------------------------------------------------------------------------------------------------------------
\section{Linear Maps}
\textbf{Definition:} Given vector spaces, $\mathcal{U,V}$ over the field $\mathbb{F}$ a linear map, $T: \mathcal{U}\rightarrow\mathcal{V}$ is a transformation that preserves additivity and homogeneity.

\begin{itemize}
\item $T(\mathbf{u + w}) = T(\mathbf{u}) + T(\mathbf{w})\hspace{2mm} \forall\mathbf{u,w}\in\mathcal{U}$
\item $T(\alpha\mathbf{u}) = \alpha T(\mathbf{u})$
\end{itemize}

\subsection{Examples}
\begin{itemize}
\item $T(x): x\rightarrow x^{2}$ is not linear.
\item $T(x): x\rightarrow 0$ is linear.
\end{itemize}

If $\mathcal{U,V}$ are finite dimensional and a basis exists for them, the linear maps can be defined as matrices.

\subsection{Range}
\textbf{Definition:} Let $T: \mathcal{V}\rightarrow\mathcal{U}$ be a linear map and denote the Range(Image) of $T$ as,
$$
\mathcal{R}(T) = \{\mathbf{u}\mid T(v) = u,\hspace{2mm} \mathbf{u}\in\mathcal{U},\hspace{1mm}\mathbf{v}\in\mathcal{V}\}
$$

%-Is the range space unique? Does the range space exist?

\subsection{Nullspace}
\textbf{Definition:} Let $T: \mathcal{V}\rightarrow\mathcal{U}$ be a linear map, then denote the Nullspace of $T$ as,
$$
\mathcal{N}(T) = \{\mathbf{v}\mid T(v) = 0,\hspace{2mm}\mathbf{u}\in\mathcal{U}\}
$$

% Is the nullspace unique?

\end{document}


\subsection{Higgs mechanism and Electroweak symmetry breaking}
\label{symbreaking}

As shown in previous subsection, the Lagrangian $L_{gauge}$ does not involve any mass term due to the requirement of gauge invariance.
So all the W and B bosons should be massless. But experimental observations show that the gauge bosons are massive.
Therefore, the gauge invariance must be broken spontaneously.
The Higgs field is introduced to break the $SU(2)_{L} \times U(1)_{Y}$ symmetry and
gauge bosons and fermions can interact with Higgs filed to acquire their masses.
And this specific process is named \textit{Higgs mechanism} in SM.

The Higgs field $\phi$ is a doublet and can be written in a Hermitian basis as
\begin{equation}
	\phi = \binom{\phi^{+}}{\phi^{0}} = \frac{1}{\sqrt{2}} \binom{\phi_{1} - i\phi_{2}}{\phi_{3} - i\phi_{4}}
\end{equation}
where $\phi_{i} = \phi_{i}^{+}$ stand for four Hermitian field.
In this new basis, the Higgs potential in Eq.~\ref{eq:Vhiggs} can be expressed as:
\begin{equation}
	V(\phi) = \frac{1}{2}\mu^{2}\left(\sum_{i=1}^{4}\phi_{i}^{2}\right) + \frac{1}{4}\lambda\left(\sum_{i=1}^{4}\phi_{i}^{2}\right)^{2}
\end{equation}
To simplify the situation, the axis in this four-dimensional space can be chosen to satisfied
~$\left<0\left| \phi_{i} \right|0\right> = 0$ for $i = 1, 2, 4$, and $<0\left| \phi_{3} \right|0> = v$. Thus,
\begin{equation}
	V(\phi) \rightarrow V(v) = \frac{1}{2}\mu^{2}v^{2} + \frac{1}{4}\lambda v^{4}
\end{equation}
The minimization of this potential depends on the sign of $\mu^{2}$ as shown in figure~\ref{fig:C2_Higgs_potential}.
When $\mu^{2} > 0$ the minimum occurs at $v = 0$, namely the vacuum is empty space and $SU(2)_{L} \times U(1)_{Y}$ symmetry is unbroken.
In the case of $\mu^{2} < 0$, the $v = 0$ symmetric point is no longer stable and the minimum occurs at nonzero value of
$v = \left( -\mu^{2}/\lambda\right)^{1/2}$ which breaks the $SU(2)_{L} \times U(1)_{Y}$ symmetry.
\begin{figure}[!htb]
  \centering
  \includegraphics[width=0.7\textwidth]{figures/Theory/Vhiggs.png}
  \caption{The Higgs potential $V(\phi)$ with $\mu^{2}>0$ (left) and $\mu^{2}<0$ (right).}
  \label{fig:C2_Higgs_potential}
\end{figure}
Thus, the classical vacuum $\phi_{0}$ of Higgs doublet can be expressed by
\begin{equation}
	\phi_{0} = \frac{1}{\sqrt{2}}\binom{0}{v}
\end{equation}
And to quantize around the classical vacuum in a general form:
\begin{equation}
	\phi = \frac{1}{\sqrt{2}} \binom{0}{v+H}
\end{equation}
Where H is a Hermitian field for physical Higgs scalar.
In this gauge, the Lagrangian $L_{Higgs}$ in Eq.~\ref{eq:Lhiggs} takes a simple form
\begin{equation}
\begin{split} \label{eq:Lhiggs2}
	L_{Higgs} & = \left(D^{\mu}\phi\right)^{\dagger}D_{\mu}\phi - V(\phi) \\
	& = M_{W}^{2}W^{\mu+}W_{\mu}^{-}\left(1+\frac{H}{\nu}\right)^{2} + \frac{1}{2}M_{Z}^{2}Z^{\mu}Z_{\mu}\left(1+\frac{H}{\nu}\right)^{2} \\
        &   + \frac{1}{2}\left(\partial_{\mu}H\right)^{2} - V(\phi)
\end{split}
\end{equation}
where the W and Z fields are
\begin{equation}
\begin{split}
	& W^{\pm} = \frac{1}{\sqrt{2}} \left(W^{1} \mp iW^{2}\right) \\
	& Z = - sin\theta_{W}B + cos\theta_{W}W^{3}
\end{split}
\end{equation}
Therefore, in Eq.~\ref{eq:Lhiggs2} spontaneous symmetry breaking brings out masses for the W and Z gauge bosons
\begin{equation}
\begin{split}
	& M_{W} = \frac{gv}{2} \\
	& M_{Z} = \sqrt{g^{2} + g'^{2}} \frac{v}{2} = \frac{M_{W}}{cos\theta_{W}}
\end{split}
\end{equation}
where $\theta_{W}$ is the weak angle defined as
\begin{equation}
	sin\theta_{W} = \frac{g'}{\sqrt{g^{2} + g'^{2}}} \qquad cos\theta_{W} = \frac{g}{\sqrt{g^{2} + g'^{2}}} \qquad tan\theta_{W} = \frac{g'}{g}
\end{equation}
Then another gauge boson photon remains massless with the field of
\begin{equation}
	A = cos\theta_{W}B + sin\theta_{W}W^{3}
\end{equation}

After the symmetry breaking, the Higgs potential in unitary gauge can be written into
\begin{equation}
	V(\phi) = -\frac{\mu^{4}}{4\lambda} - \mu^{4}H^{2} + \lambda\nu H^{3} + \frac{\lambda}{4}H^{4}
\end{equation}
The first term in $V$ is a constant, while the second term denotes a (tree-level) mass of Higgs boson
\begin{equation}
	M_{H} = \sqrt{-2\mu^{2}} = \sqrt{2\lambda}v
\end{equation}
Due to the unknown of quartic Higgs coupling $\lambda$, the Higgs mass is not predicted.
The third and fourth terms in the Higgs potential $V$ denote the induced cubic and quartic interactions of the Higgs scalar.

Through the Higgs mechanism, fermions can also acquire their masses.
In the unitary gauge, Yukawa Lagrangian ($L_{Yukawa}$) can be written as a simple form of \cite{Pich:2015lkh}
\begin{equation}
	L_{Yukawa} = -\left(1+\frac{H}{v}\right) \left(m_{d}\bar{d}d + m_{u}\bar{u}u + m_{l}\bar{l}l\right)
\end{equation}
in which $m_{f} = \frac{y_{f}v}{\sqrt{2}}$ for $f = d, u, l$.


\subsection{Convex functions}\label{subsec:convex_functions}

Let \( X \) be a Hausdorff \hyperref[def:topological_vector_space]{topological vector space} and \( D \) be a \hyperref[def:convex_set]{convex} subset of \( X \).

\begin{definition}\label{def:convex_functions}
  A function \( f: D \to \BbbR \) is called \term{convex} if any of the following equivalent conditions hold:

  \begin{thmenum}
    \thmitem{def:convex_functions/ineq} For any two points \( x, y \in D \) and any \( t \in [0, 1] \) we have
    \begin{equation*}
      f(tx + (1-t)y) \leq tf(x) + (1-t)f(y).
    \end{equation*}

    \thmitem{def:convex_functions/epi} The \hyperref[def:epigraph]{epigraph}
    \begin{equation*}
      \epi f \coloneqq \{ (x, a) \in X \times \BbbR \colon f(x) \leq a \}
    \end{equation*}
    is convex.
  \end{thmenum}

  If \( -g \) is convex for some function \( g: D \to \BbbR \), we call \( g \) \term{concave}.

  Note that definitions do not require any topological structure on \( X \). Most of their properties, however, require a topology.
\end{definition}
\begin{proof}
  Let \( x, y \in D \) and let \( t \in [0, 1] \).

  \ImplicationSubProof{def:convex_functions/ineq}{def:convex_functions/epi} Let \( \epi f \) be a convex set. Obviously \( (x, f(x)) \in D \) and \( (y, f(y)) \in D \). By the convexity of \( \epi f \), we have
  \begin{equation*}
    f(tx + (1-t)y) \leq tf(x) + (1-t)f(y).
  \end{equation*}

  Thus, \( f \) is a convex function.

  \ImplicationSubProof{def:convex_functions/epi}{def:convex_functions/ineq} Let \( f \) be convex. Let \( a \geq f(x) \) and \( b \geq f(y) \), so that \( (x, a) \in \epi f \) and \( (y, b) \in \epi f \). Hence,
  \begin{equation*}
    f(tx + (1-t)y) \leq tf(x) + (1-t)f(y) \leq ta + (1-t)b,
  \end{equation*}
  which implies that
  \begin{equation*}
    (tx + (1-t)y, ta + (1-t)b) \in \epi f.
  \end{equation*}

  Thus, \( \epi f \) is a convex set.
\end{proof}

\begin{definition}\label{def:affine_functions_concave_and_convex}
  \hyperref[def:affine_operator]{Affine functions} \( f: X \to \BbbR \) are simultaneously convex and concave.
\end{definition}

\begin{proposition}\label{thm:convex_subdifferential_is_convex_and_weak*_closed}\mcite[exer. 1.10]{Phelps1993}
  For any convex function \( f \) and any \( x \in D \), the set \( \partial f(x) \) is convex and weak* closed.
\end{proposition}
\begin{proof}
  Fix \( x \in D \). If \( \partial f(x) \) is empty, then the theorem is trivially true.

  Suppose it is nonempty and \( y^*, z^* \in \partial f(x) \). For any \( x \in D \) we then have
  \begin{balign*}
     & \inprod{y^*} {x - x} \leq f(x) - f(x), \\
     & \inprod{z^*} {x - x} \leq f(x) - f(x).
  \end{balign*}

  Fix \( t \in [0, 1] \) and \( x \in D \). It follows that
  \begin{balign*}
    \inprod{t y^* + (1-t) z^*} {x - x}
     & =
    t \inprod{y^*} {x - x} + (1-t) \inprod{z^*} {x - x}
    \leq \\ &\leq
    t [f(x) - f(x)] + (1-t) [f(x) - f(x)]
    =    \\ &=
    f(x) - f(x),
  \end{balign*}
  thus \( t y^* + (1-t)z^* \in \partial f(x) \) and hence \( \partial f(x) \) is convex.

  To prove weak*-closedness, we consider the decomposition
  \begin{balign*}
    \partial f(x)
     & =
    \{ x^* \in E^* \colon \forall x \in D, \inprod {x^*} {x - x} \leq f(x) - f(x) \}
    =    \\ &=
    \bigcap_{x \in D} \{ x^* \in E^* \colon \inprod {x^*} {x - x} \leq f(x) - f(x) \}
    =    \\ &=
    \bigcap_{x \in D} L(x)^{-1} (-\infty, f(x) - f(x)],
  \end{balign*}
  where
  \begin{balign*}
     & L: E \to E^{**}                  \\
     & L(x)(x^*) = \inprod {x^*} {x - x}.
  \end{balign*}

  For each \( x \in E \), the functionals \( L(x) \) are weak*-to-weak continuous because the image \( L(E) \subseteq E^{**} \) is isometrically isomorphic to a translation of \( E \). Hence, the preimage \( L(x)^{-1} (-\infty, f(x) - f(x)] \) is closed and \( \partial f(x) \) is weak*-closed as the intersection of weak*-closed sets.
\end{proof}

\begin{lemma}\label{thm:convex_difference_quotient_grows}
  For every point \( x \in X \) and every direction \( h \in S_X \) the difference quotient is a monotone function of \( t > 0 \), i.e. for \( 0 < s < t \)
  \begin{balign*}
    \frac {f(x + sh) - f(x)} s
    \leq
    \frac {f(x + th) - f(x)} t
  \end{balign*}
\end{lemma}
\begin{proof}
  \begin{balign*}
    \frac {f(x + sh) - f(x)} s
    =
    \frac t s \frac {f(x + \frac s t t h) - f(x)} t
    =
    \frac t s \frac {f\left(\frac s t (x + th) + (1 - \frac s t) x \right) - f(x)} t
    \leq \\ \leq
    \frac t s \frac {\frac s t f(x + t h) + (1 - \frac s t) f(x) - f(x)} t
    =
    \frac t s \frac s t \frac {f(x + th) - f(x)} t
    =
    \frac {f(x + th) - f(x)} t
  \end{balign*}
\end{proof}

\begin{proposition}\label{thm:convex_one_sided_derivatives_exist}
  For every point \( x \in X \) and every direction \( h \in S_X \) the one-sided derivative \( f_+'(x)(h) \) exists.
\end{proposition}
\begin{proof}
  We use the convexity of \( f \) to obtain
  \begin{balign*}
    f(x) = f \left(x + \frac {th} 2 - \frac {th} 2 \right) \leq \frac {f(x + th) + f(x - th)} 2,
    \\
    0 \leq [f(x - th) - f(x)] + [f(x + th) - f(x)],
    \\
    -[f(x - th) - f(x)] \leq [f(x + th) - f(x)],
    \\
    -\frac {f(x + t(-h)) - f(x)} t \leq \frac {f(x + th) - f(x)} t,
  \end{balign*}
  thus the difference quotient in \( f_+'(x)(h) \) is bounded below by the difference quotient for \( -f_+'(x)(-h) \).

  \Fullref{thm:convex_difference_quotient_grows} implies that the right difference quotient is non-increasing, thus both limits exist and
  \begin{equation*}
    -f_+'(x)(-h) \leq f_+'(x)(h).
  \end{equation*}
\end{proof}

\begin{proposition}\label{thm:convex_one_sided_derivatives_sublinear}
  For every point \( x \in X \) and every direction \( h \in S_X \) the one-sided derivative \( f_+'(x)(h) \) is a \hyperref[def:sublinear_functional]{sublinear functional}.
\end{proposition}
\begin{proof}
  \SubProofOf{def:sublinear_functional/subadditivity} It follows directly from
  \begin{balign*}
    \frac {f(x + t(a + b)) - f(x)} t
     & =
    \frac {f(\tfrac 1 2 (x + 2ta) + \tfrac 1 2 (x + 2tb)) - f(x)} t
    \leq \\ &\leq
    \frac {\tfrac 1 2 f(x + 2ta) + \tfrac 1 2 f(x + 2tb) - f(x)} t
    =    \\ &=
    \frac {f(x + 2ta) - f(x)} {2t} + \frac {f(x + 2tb) - f(x)} {2t}.
  \end{balign*}

  \SubProofOf{def:sublinear_functional/positive_homogeneity} For \( \lambda > 0 \) the equality \( f_+'(x)(\lambda h) = \lambda f_+'(x)(h) \) follows from
  \begin{balign*}
    \frac {f(x + t \lambda h) - f(x)} t
    =
    \lambda \frac {f(x + t \lambda h) - f(x)} {t \lambda}
  \end{balign*}
\end{proof}

\begin{corollary}\label{thm:convex_one_sided_derivative_negative_inequality}
  \begin{equation*}
    -f_+'(x)(-h) \leq f_+'(x)(h)
  \end{equation*}
\end{corollary}
\begin{proof}
  \begin{equation*}
    0 = f_+'(x)(h + (-h)) \leq f_+'(x)(h) + f_+'(x)(-h)
  \end{equation*}
\end{proof}

\begin{proposition}\label{thm:convex_iff_subdifferential_nonempty}
  The continuous function \( f: D \to X \) is convex if and only if its subdifferential \( \partial f(x) \) (see \fullref{def:subdifferentials/convex}) is nonempty for every \( x \) in \( D \).
\end{proposition}
\begin{proof}
  \todo{Prove}.
\end{proof}

\begin{proposition}
  \label{thm:convex_one_sided_derivative_is_max}
  For every direction \( h \in S_X \), we have that
  \begin{equation*}
    f_+'(x)(h) = \max\{ \inprod {x^*} h \colon x^* \in \partial f(x) \}.
  \end{equation*}
\end{proposition}

\begin{theorem}\label{thm:singleton_subdifferential_implies_gateaux}
  If \( f \) is continuous and if the subdifferential \( \partial f(x) \) at \( x \in X \) is a singleton with element \( x^* \), then \( f \) is Gateaux differentiable at \( x \) and \( f_G'(x) = x^* \).
\end{theorem}
\begin{proof}
  Let \( h \in S_X \) be arbitrary. \Fullref{thm:convex_one_sided_derivatives_exist} implies that the one-sided derivatives \( f_+'(x)(-h) \) and \( f_+'(x)(h) \) exist and
  \begin{equation*}
    -f_+'(x)(-h) \leq f_+'(x)(h).
  \end{equation*}

  Assume that \( f \) is not Gateaux differentiable at \( x \), i.e. for some \( h_0 \in X \), we have a strict inequality. Then by \fullref{thm:convex_one_sided_derivative_is_max}
  \begin{balign*}
    \min\{ \inprod {x^*} {h_0} \colon x^* \in \partial f(x) \}
    =
    -\max\{ \inprod {x^*} {-h_0} \colon x^* \in \partial f(x) \}
    =
    -f_+'(x)(-h_0)
    < \\ <
    f_+'(x)(h_0)
    =
    \max\{ \inprod {x^*} {h_0} \colon x^* \in \partial f(x) \},
  \end{balign*}
  which implies that there is more that one functional \( x^* \in \partial_C f(x) \). This contradicts the assumption of the theorem.

  Thus, \( f \) is Gateaux differentiable at \( x \).
\end{proof}

\begin{theorem}\label{thm:rn_continuous_convex_partial_derivatives_imply_gateaux}\mcite[exer. 1.15(b]{Phelps1993})
  In \( \BbbR^n \), the existence of the partial derivatives at \( x \) for a continuous convex function \( f: D \to \BbbR \) at a point \( x \in D \) implies Gateaux differentiability.
\end{theorem}
\begin{proof}
  Let \( D \subseteq \BbbR^n \) be an open and convex set and let \( f: D \to \BbbR \) be continuous and convex. Then \( f_+'(x) \) exists everywhere by \fullref{thm:convex_one_sided_derivatives_exist} and is a subdifferential functional by \fullref{thm:convex_one_sided_derivatives_sublinear}.

  Let \( e_1, \ldots, e_n \) be the canonical basis for \( \BbbR^n \).

  The partial derivatives
  \begin{balign*}
    \frac {\partial f} {\partial x_i} (x)
    \coloneqq
    \lim_{t \to 0} \frac {f(x + t e_i) - f(x)} t
    =
    f_+'(x)(e_i)
  \end{balign*}
  exist, hence the projections of \( f_+'(x) \) along the coordinate exes are linear.

  Define line linear functional
  \begin{equation*}
    l(h) \coloneqq \sum_{i=1}^n h_i \inprod{\frac {\partial f} {\partial x_i} (x)} h,
  \end{equation*}
  where \( h_1, \ldots, h_n \) are the coordinates of \( h \) along \( e_1, \ldots, e_n \).

  We will show that \( l \cong f_+' \). Fix \( h \in S_X \). We have
  \begin{balign}\label{thm:rn_continuous_convex_partial_derivatives_imply_gateaux/diff_dominated}
    f_+'(x)(h)
     & =
    f_+'(x)\left(\sum_{i=1}^n h_i e_i \right)
    \reloset {\text{sublinearity}} \leq \nonumber      \\ &\leq
    \sum_{i=1}^n f_+'(x)(h_i e_i)
    \reloset {\text{linearity along } e_i} = \nonumber \\ &=
    \sum_{i=1}^n h_i f_+'(x)(e_i)
    =
    \sum_{i=1}^n h_i \inprod{\frac {\partial f} {\partial x_i} (x)} h.
  \end{balign}

  Thus,
  \begin{balign*}
    \inprod l h
    =
    -\inprod l {-h}
    \reloset {\ref{thm:rn_continuous_convex_partial_derivatives_imply_gateaux/diff_dominated}} \leq
    -f_+'(x)(-h)
    \reloset {\text{\ref{thm:convex_one_sided_derivative_negative_inequality}}} \leq
    f_+'(x)(h)
    \reloset {\ref{thm:rn_continuous_convex_partial_derivatives_imply_gateaux/diff_dominated}} \leq
    \inprod l h,
  \end{balign*}
  i.e. \( f_+'(x)(h) = \inprod l h \) for all \( h \in S_X \), hence \( f_+'(x) \) is a linear functional and \( f \) is Gateaux differentiable at \( x \).
\end{proof}

\begin{theorem}\label{thm:rn_continuous_convex_gateaux_implies_frechet}\mcite[exer. 1.15(a]{Phelps1993})
  In \( \BbbR^n \), Gateaux differentiability of a continuous convex function \( f: D \to \BbbR \) at a point \( x \in D \) implies Frechet differentiability.
\end{theorem}
\begin{proof}
  Since \( f \) is Gateaux differentiable (\fullref{def:differentiability/gateaux}) at \( x \), the derivative \( f'(x) = f_+'(x) \) is linear.

  Because \( f \) is continuous and convex, it is locally Lipschitz with constant \( L \) in some \( \delta \)-ball with center \( x \).

  Suppose that \( f \) is not Frechet differentiable at \( x \). Inverting the condition in \fullref{def:differentiability/frechet}, we obtain that there exist \( \varepsilon > 0 \) and a sequence \( \{ h_n \}_n \subseteq B(x, \delta) \setminus \{ 0 \} \) such that \( \norm{h_n} \to 0 \) and yet for all \( n \in \BbbZ_{>0} \),
  \begin{balign}\label{thm:rn_continuous_convex_gateaux_implies_frechet/frechet_assumption}
    \abs{f(x + h_n) - f(x) - \inprod{f'(x)} {h_n}} > \varepsilon \norm{h_n}.
  \end{balign}

  Define
  \begin{balign*}
    t_n \coloneqq \norm{h_n}
     &  &
    u_n \coloneqq \frac{h_n} {\norm {h_n}}.
  \end{balign*}

  Obviously \( t_{n_k} \downarrow 0 \). The vectors \( h_n \) are linearly independent since otherwise \( f \) would not be Gateaux differentiable at \( x \), hence \( u_n \) are not all equal.

  Since \( S_{\BbbR^n} \) is compact, by the Bolzano-Weierstrass theorem, there exists a convergent subsequence \( \{ u_{n_k} \}_k \underset {k \to \infty} \to u_0 \) of \( \{ u_n \}_n \). We have

  \begin{balign}\label{thm:rn_continuous_convex_gateaux_implies_frechet/frechet_estimate}
     & \phantom= \abs{\frac {f(x + t_{n_k} u_{n_k}) - f(x)} {t_{n_k}} - \inprod{f'(x)} {u_{n_k}}}
    \leq \nonumber
    \abs{\frac {f(x + t_{n_k} u_{n_k}) - f(x + t_{n_k} u_0)} {t_{n_k}}} +                       \\ &+ \abs{\frac {f(x + t_{n_k} u_0) - f(x)} {t_{n_k}} - \inprod{f'(x)} {u_0}} + \abs{\inprod{f'(x)} {u_0 - u_{n_k}}}
    \leq \nonumber                                                                              \\ &\leq
    L \norm{u_{n_k} - u_0} + \abs{\frac {f(x + t_{n_k} u_0) - f(x)} {t_{n_k}} - \inprod{f'(x)} {u_0}} + \norm{f'(x)} \norm{u_0 - u_{n_k}}.
  \end{balign}

  Fix \( \delta > 0 \). Because of the Gateaux differentiable of \( f \) at \( x \), we can pick \( k_0 \) such that
  \begin{equation*}
    \abs{\frac {f(x + t_{n_{k_0}} u_0) - f(x)} {t_{n_{k_0}}} - \inprod{f'(x)} {u_0}} < \delta.
  \end{equation*}

  Because \( \{ u_{n_k} \}_k \) converges to \( u_0 \), we can choose \( k_1 \) such that
  \begin{equation*}
    \norm{u_0 - u_{n_{k_1}}} < \delta.
  \end{equation*}

  Thus, for \( k > \max \{ k_0, k_1 \} \), \fullref{thm:rn_continuous_convex_gateaux_implies_frechet/frechet_estimate} is bounded by
  \begin{balign*}
    \abs{\frac {f(x + t_{n_k} u_{n_k}) - f(x)} {t_{n_k}} - \inprod{f'(x)} {u_{n_k}}}
    \leq
    (L + 1 + \norm{f'(x)}) \delta.
  \end{balign*}

  It suffices to choose \( \delta > 0 \), so that
  \begin{equation*}
    \delta < \frac 1 {L + 1 + \norm{f'(x)}}
  \end{equation*}
  in order to have, for \( k > \max \{ k_0, k_1 \} \),
  \begin{equation*}
    \abs{\frac {f(x + t_{n_k} u_{n_k}) - f(x)} {t_{n_k}} - \inprod{f'(x)} {u_{n_k}}} < \varepsilon.
  \end{equation*}

  But this contradicts \fullref{thm:rn_continuous_convex_gateaux_implies_frechet/frechet_assumption}, hence \( f \) is Frechet differentiable at \( x \).
\end{proof}

\begin{corollary}\label{thm:rn_continuous_convex_partial_derivatives_imply_frechet}
  In \( \BbbR^n \), the existence of the partial derivatives at \( x \) for a continuous convex function \( f: D \to \BbbR \) at a point \( x \in D \) is equivalent to Frechet differentiability.
\end{corollary}
\begin{proof}
  A direct consequence of and \fullref{thm:rn_continuous_convex_partial_derivatives_imply_gateaux} and \fullref{thm:rn_continuous_convex_gateaux_implies_frechet}.
\end{proof}

\begin{theorem}\label{thm:rn_continuous_convex_frechet_almost_everywhere}\mcite[exer. 1.17]{Phelps1993}
  In \( \BbbR^n \), continuous convex functions \( f: D \to \BbbR \) are differentiable almost everywhere.
\end{theorem}
\begin{proof}
  For all \( h \in S_X \) and small enough \( t > 0 \) we define
  \begin{balign*}
     & \varphi_h^t: D \to \BbbR
     & \varphi_h^t(x) \coloneqq \frac {f(x + th) - f(x)} t
  \end{balign*}
  and \( \varphi_h(x) \coloneqq f_+'(x)(h) = \lim_{t \downarrow 0} \varphi_h^t(x) \).

  Considered as functions of \( x \), \( \varphi_h^t \) are obviously continuous hence Borel measurable, and so \( \varphi_h \) is also Borel measurable.

  Denote by
  \begin{balign*}
    B_h
    \coloneqq
    \{ x \in D \colon -f_+'(x)(-h) < f_+'(x)(h) \}
    =
    \{ x \in D \colon -\varphi_{-h}(x) - \varphi_h(x) < 0 \}
  \end{balign*}
  the set of points \( x \in D \) where the one-sided derivative \( f_+'(x)(h) \) is not linear, given a fixed direction \( h \in S_X \). If \( B_h \) is nonempty, \( f \) is not differentiable at \( x \).

  The sets \( B_h \) are Borel sets since they are the preimages of \( (-\infty, 0) \) under a Borel function. We will show that it is a null set for every direction \( h \).

  Fix \( h \in S_X \). Denote by \( \delta_x \coloneqq \sup \{ t > 0 \colon x + th \in D \} \).

  The function \( t \mapsto f(x + th) \) is a convex function of one variable. By \cite[theorem 1.16]{Phelps1993}, it is differentiable \( \mu_1 \)-almost everywhere in \( [0, \delta_x) \), where \( \mu_m \) is the Lebesgue \( m \)-measure.

  Denote
  \begin{balign*}
     & H \coloneqq \linspan\{ h \} \cong \BbbR^1,
    \\
     & H^\perp \cong \BbbR^{n-1} \text{ - the orthogonal complement of \( H \) in \( \BbbR^n \)},
    \\
     & L_x \coloneqq \{ x + th, 0 \leq t < \delta_x \} - half-open segments in D.
  \end{balign*}

  THe whole domain \( D \) can be represented as \( D = \cup \{ L_x \colon x \in H^\perp \} \).

  We can now use Fubini's theorem to show that \( B_h \) is a null set:
  \begin{balign*}
    \mu_n(B_h)
    =
    \int_{B_h} dz
    =
    \int_{\BbbR^n = H^\perp \oplus H} \chi_{B_h} (z) dz
    =
    \int_{H^\perp} \int_{L_x} \chi_{B_h} (y) dy dx
    = \\ =
    \int_{H^\perp} \mu_1(B_h \cap L_x) dx
    =
    \int_{H^\perp} 0 dx
    =
    0.
  \end{balign*}

  Hence, for all \( h \in S_X \), \( -f_+'(x)(-h) = f_+'(x)(h) \) for almost all \( x \in D \).

  In particular, if \( e_1, \ldots, e_n \) is the canonical basis of \( \BbbR^n \), the \( i \)-th partial derivative \( \frac{\partial f} {\partial x_i} (x) \) exists only in \( D \ B_{e_i} \).

  The gradient
  \begin{equation*}
    \nabla f(x) = \left( \frac{\partial f} {\partial x_1} (x), \ldots, \frac{\partial f} {\partial x_n} (x) \right)
  \end{equation*}
  then exists in
  \begin{equation*}
    \hat D \coloneqq (D \ B_{e_1}) \cap \ldots \cap (D \ B_{e_n}) = D \setminus \left( \bigcup_{i=1}^n B_{e_i} \right).
  \end{equation*}

  \Fullref{thm:rn_continuous_convex_partial_derivatives_imply_frechet} then implies that \( f \) is Frechet differentiable in \( \hat D \), i.e. almost everywhere in \( D \).
\end{proof}


%!TEX root = ../main.tex
%-------------------------------------------------------------------------------
\subsection{Economic framework}
%-------------------------------------------------------------------------------
EKW models describe sequential decision-making under uncertainty \citep{Gilboa.2009, Machina.2014}. At time $t = 1, \hdots, T$ each individual observes the state of the economic environment $s_t\in S$ and chooses an action $a_t$ from the set of admissible actions $\mathcal{A}$. The decision has two consequences: an individual receives an immediate utility $u_t(s_t, a_t)$ and the economy evolves to a new state $s_{t + 1}$. The transition from $s_t$ to $s_{t + 1}$ is affected by the action but remains uncertain. Individuals are forward-looking. Thus they do not simply choose the alternative with the highest immediate utility. Instead, they take the future consequences of their current action into account.

A policy $\pi = (a^\pi_1(s_1), \hdots, a^\pi_T(s_T))$ provides the individual with instructions for choosing an action in any possible future state. It is a sequence of decision rules $a^\pi_t(s_t)$ that specify the action at a particular time $t$ for any possible state $s_t$ under $\pi$. The implementation of a policy generates a sequence of utilities that depends on the objective transition probability distribution $p_t(s_t, a_t)$ for the evolution of state $s_t$ to $s_{t + 1}$ induced by the model. Individuals have rational expectations \citep{Muth.1961} so their subjective beliefs about the future agree with the objective transition probabilities of the model.

\autoref{Timing} depicts the timing of events in the model for two generic periods. At the beginning of period $t$, an individual fully learns about the immediate utility of each alternative, chooses one of them, and receives its immediate utility. Then the state evolves from $s_t$ to $s_{t + 1}$ and the process is repeated in $t + 1$.
%
\begin{figure}[b]
	%\vspace{1.0cm}
	\centering
	\scalebox{0.755}{\input{../material/fig-timing-events.tex}}
	\caption{Timing of events}
	\label{Timing}
\end{figure}
%

Individuals face uncertainty and they seek to maximize the expected total discounted utilities. An exponential discount factor ${0 < \delta < 1}$ parameterizes their time preference and captures a taste for immediate over future utilities.

Equation~\eqref{Objective Risk} provides the formal representation of the individual's objective. Given an initial state $s_1$, individuals implement the policy $\pi$ from the set of all possible policies $\Pi$ that maximizes the expected total discounted utilities over all $T$ decision periods given the information available in the first-period state, $s_1$:
%
\begin{align}\label{Objective Risk}
	\max_{\pi \in\Pi} \E_{s_1}^\pi\left[\left.\sum^{T}_{t = 1}  \delta^{t - 1} u_t(s_t, a^\pi_t(s_t))\right]\right..
\end{align}
%
The superscript of the expectation emphasizes that each policy $\pi$ induces a different probability distribution over the sequences of utilities.


\section{Verification of Parallel Concatenation on Monoids}\label{sec:parallelization}
In this section we define how to concatenate a list of monoids
into a monoid.
%
Then, we parallelize list concatenation
and use LiquidHaskell to prove that the
parallel and sequential versions are equivalent.
\NV{What does equivalence means here?}

\subsection{Concatenation of lists using monoids}
If @m@ is a monoid, then @mconcat@ reduces a list of monoids @xs@
to one monoid.
\begin{code}
mconcat :: List m -> m
mconcat N        = mempty
mconcat (C x xs) = x mappend (mconcat xs)
\end{code}
%
Concatenation folds the elements of the list using @(mappend)@
and returns @mempty@ when the list is empty.

Next, we define a parallel version of list concatenation
%
\begin{code}
pmconcat :: Int -> List m -> m
pmconcat i xs  | i <= 1 || llen xs <= i
  = mconcat xs
pmconcat i xs
  = pmconcat i $ withStrategy parStrategy $
                 map mconcat (chunk i xs)
\end{code}%$
The function @pmconcat i xs@ calls @mconcat xs@ in the base case,
otherwise it
(1) chunks the list @xs@ in lists of size @i@,
(2) runs in parallel @mconcat@ to each chunk,
(3) recursively runs itself in the result list.
%
Termination of @pmconcat@ holds, as the length of @chunk i xs@
is smaller than the length of @xs@, when @1 < i@.
%
Concretely, we define @chunk@ using the standard definition for
list @take@ and @drop@.
\begin{code}
chunk :: i:Pos -> xs:List a
  -> {v:List (List a) | chunkRes i xs }
chunk i xs
  | length xs <= i
  = C xs N
  | otherwise
  = C (take i xs) (chunk i (drop i xs))

chunkRes i xs
  | length xs <= i = length v == 1
  | i == 1         = length v == length xs
  | otherwise     = length v < length xs
\end{code}

Where @take@ and @drop@ type as follows
\begin{code}
drop :: i:Nat -> xs:{List a | i <= length xs }
  -> {v:List a | length v == length xs - i }
take :: i:Nat -> xs:{List a | i <= length xs }
  -> {v:List a | length v == i }
\end{code}

\paragraph{Runtime Parallelism.}
To parallelize concatenation of the chunks we use parallel strategies
from the Haskell library @Control.Parallel.Strategies@.
%
For example, in our implementation @parStrategy@ is defined as
\begin{code}
parStrategy :: Strategy (List a)
parStrategy = parTraversable rdeepseq
\end{code}

Note that parallelization of concatenation leads to runtime speedups
only when @(mappend)@ runs in constant time with respect to the monoid size.
%
Otherwise, at each recursive call of @pmconcat@,
runtime will be dominated by the size of the biggest list element.

Even though runtime of the parallel version depends on the implementation details
of the monoids, correctness holds for every monoid.
%
In the rest of this section we prove equivalence of the sequential and parallel concatenation operations.

\paragraph{Parallelism in the Logic.}
To prove equivalence we make the \textit{assumption}
that the Haskell function @withStrategy s x@
returns @x@ for every strategy.
\begin{code}
assume withStrategy :: Strategy a -> x:a -> {v:a | v == x}
\end{code}
%
Moreover, to prove equivalence, we need to represent
the implementation of @pmconcat@ in logic.
%
Which, in turn, requires representation of @withStrategy@.
%
LiquidHaskell represents @withStrategy@ in logic as a function that merely returns
its second argument @withStrategy _ x = x@
and does not reason about parallelism.

\subsection{Equivalence of Parallel and Sequential Concatenation}

\begin{theorem}[Monoid Concatenation Equivalence]\label{theorem:equivalence:concat}
If @m@ is monoid, then the parallel and sequential concatenations are equivalent:
\begin{code}
  pmconcatEq :: i:Int -> is:List m -> {pmconcat i is == mconcat is }
\end{code}
\end{theorem}

\begin{proof}
We prove the theorem by providing a Haskell implementation of @pmconcatEq@
that is safe under LiquidHaskell. The details of the proof can be found in~\cite{implementation},
here we provide the sketch of the proof.

First we prove that @mconcat@ distributes over list splitting
\begin{code}
mconcatSplit
  :: i:Nat -> xs:{List m | i <= llen xs}
  -> { mconcat xs == mconcat (take i xs)
                 <> mconcat (drop i xs) }
\end{code}
%
The proofs proceeds by structural induction, using monoid left identity in the base case
and associavity in the inductive step.

We generalize the above
to prove that @mconcat@ distributes over list chunking
\begin{code}
mconcatChunk
  :: i:Pos -> xs:List (Monoid a)
  -> { mconcat xs == mconcat (map mconcat (chunk i xs)) }
\end{code}
%
The proofs proceeds by structural induction, using monoid left identity in the base case
and lemma @mconcatSplit@ in the inductive step.

Lemma @mconcatChunk@ is sufficient to prove @pmconcatEq@ by structural induction,
using monoid left identity in the base case.
\cqed\end{proof}

Note that the proof uses only the monoid laws and not the implementations of the monoid
operators, thus the theorem holds \textit{for any} type that satisfies the monoid laws.
%
Next we define a monoid structure for string matching and prove it satisfies the monoid laws.
Thus we conclude that string matching concatenation can be parallelized.


\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb}
\usepackage{amsthm}
\usepackage{enumitem}
\usepackage[legalpaper, margin=1in]{geometry}
\usepackage{amsfonts}
\usepackage[mathscr]{eucal}
\usepackage{hyperref}
\newtheorem{thm}{Theorem}
%this makes it so that everything labelled thm is numbered by section
\newtheorem{claim}[thm]{Claim}
\newtheorem{cor}[thm]{Corollary}
\newtheorem{conclusion}{Conclusion}
%\newtheorem{remark}[thm]{Remark}
\newtheorem{lemma}[thm]{Lemma}
\newtheorem{prop}[thm]{Proposition}
\newtheorem{prob}[thm]{Problem}
%\newtheorem{exam}[thm]{Example}
\theoremstyle{definition}
%this makes the following things non-italicized
\newtheorem{defn}[thm]{Definition}
\newtheorem{postulate}{Postulate}
\newtheorem{remark}[thm]{Remark}
\newtheorem{discrepency}[thm]{Discrepency}
\newtheorem{exam}[thm]{Example}
\newtheorem{innercustomthm}{Word Postulate}
\newenvironment{customthm}[1]
{\renewcommand\theinnercustomthm{#1}\innercustomthm}
{\endinnercustomthm}

\title{Time Series Analysis Notes}
% \author{Mozahid Haque}
% \date{October 2020}

\begin{document}
\maketitle
\section{Overview of Time Series Characteristics}
\begin{defn}
A \textbf{univariate time series} is a sequence of measurements of one variable at regular time steps.
\end{defn}
\noindent Such data need not be IID.

\subsection{Objectives of Analysis}
\begin{enumerate}
    \item Describe important features of any time series pattern
    \item Explain how past affects future or how two time series "interact"
    \item Forecasting
    \item Use time series as a control standard, e.g., measurements of the quality of some manufacturing product
\end{enumerate}

\subsection{Model Types and Considerations}
There are two basic types:
\begin{enumerate}
    \item Relating time series values to past values and past prediction errors -- called Autoregressive Integrated Moving Average or ARIMA models.
    \item Regular regression models with time indices as x-variables.
\end{enumerate}

\noindent Initial data exploration:
\begin{itemize}
    \item Trend -- overall progression of measurements
    \item Seasonality -- periodic behavior based around calendar intervals such as seasons, quarters, months, weeks, etc.
    \item Outliers -- data away from original data or away from some manipulation of it
    \item Long-run Cycle -- periodic behavior unrelated to seasonality type
    \item Constant Variance -- variance changing or not
    \item Abrupt Changes -- significant disturbances to series or variance or other things
\end{itemize}

\subsection{Autoregressive Models:  Autocorrelation and Partial Autocorrelation}
\begin{defn}
Let $\{y_t\}_{t=1}^n$ be a time series indexed by $t$. An \textbf{autoregressive model} is one where time series values are regressed on previous values.  For example, a first-order autoregression AR(1) would look like:
\[ y_t = \beta_0 + \beta_1 y_{t-1} + \epsilon_t \]
and a second-order autoregression AR(2) would look like:
\[ y_t = \beta_0 + \beta_1 y_{t-1} + \beta_2 y_{t-2} + \epsilon_t. \]
In general, a $k^\text{th}$-order autoregression AR(k) is given by
\[ y_t =  \left( \beta_0, \beta_1, \hdots, \beta_n, 1 \right) \left( 1, y_{t-1}, \hdots, \beta_{t-n}, \epsilon_t \right)^T. \]
Usually the errors $\epsilon_t \overset{iid}{\sim}N(0, \sigma_{\epsilon}^2)$ and independent of $y$.
\end{defn}

This begs the question:
How do we choose the order for a given time series?
There are two methods:
\begin{enumerate}
    \item Autocorrelation function (ACF) and
    \item Partial Autocorrelation function (PACF).
\end{enumerate}
\begin{defn}
The coefficient of correlation between two values in a time series given by:
\[ \text{Corr}(y_t, y_{t+k}) = r_k = \frac{c_k}{c_0} ~~~~\text{where}~~~~
c_k = \frac{1}{n} \sum_{t=1}^{n-k} (y_t - \bar{y})(y_{t+k} - \bar{y}) \]
is called the \textbf{autocorrelation function (ACF)}.
Recall what familiar term $c_0$ is.  Notice the similarity to the covariance formula which is meant to exhibit the linear relationship between two variables.  In this case, this is the covariance (or linear relationship) of lagged values.  Here the \textbf{lag} is given by \textbf{k}.
\end{defn}

This is one method.  This method includes in it the influence of other lags on $y_t$.  The second method removes this influence of the other lags in between...
\begin{defn}
Here the \textbf{Partial Autocorrelation Function (PACF)} is given by
\[ f_k =
\begin{cases}
      r_1 = \text{Corr}(y_t, y_0) & \text{if}~ k=1; \\
      \text{Corr}(y_t - y_t^{t-1}, y_0 - y_0^{k-1}) & \text{if}~ k \geq 2
   \end{cases}.
\]
Essentially, this has the effect of determining the linear correlation between $y_t$ and $y_{t+k}$ but removing the linear dependence of lags in between $t$ and $t+k$, namely $t_i$ such that $t < t_i < t+k$.  One can also think of this as subtracting away the projection of $y_t$ on the linear subspace spanned by $y_{t+1}, \hdots, y_{t+k-1}$.
\end{defn}

We can additionally complicate the whole thing by considering certain trend behavior within our original time series via decomposition.

\subsection{Complexifying ACF/PACF via Decomposition and/or Higher Order Trends}
The typical decomposition of a time series involves:
\begin{itemize}
    \item overall trend, $m_t$
    \item seasonality, $s_t$, and
    \item error, $\epsilon_t$
\end{itemize}
so that
\[ y_t = m_t + s_t + \epsilon_t. \]
Typical estimation involves first estimating the overall trend through linear filters.  One example of this is a moving average given by some "window" size:
\[ \hat{m_t} = \sum_{k=-a}^a \left( \frac{1}{1+2a} \right) y_{t+k}. \]
We can experiment with the window size to get a feel for a good overall trend.  Then once this is done, we can estimate the seasonality by looking at what remains:
\[ \hat{s_t} = y_t - \hat{m_t}. \]
Note that this $\hat{s_t}$ depends on the window size.  So we can take the average of the window sized seasonality estimates to fix a single $s_t$.  Then this gives us a way to calculate the error:
\[ \epsilon_t = x_t - \hat{m_t} - \hat{s_t}. \]
Luckily there are packages for such decomposition.

Another way to complexify our model for our time series is to add quadratic trends to the model by considering not just linear time factors $t$, but higher order factors and interactions such as $t^2$, $t^3$, etc.
\end{document}