\chapter{Results}
\label{cha:results}

% Move this to Ericssons appendix
%\section{Positive feedback on fan and overheating faults}
%\noindent\fbox{
%	\parbox{\textwidth}
%	{
%		Comment on the fan issue found while mining faults
%	}
%}

\section{Comparative imputing experiments}
\label{sec:imp-exps}

A set of thorough experiments has been designed to compare both model estimation techniques and empirically determine which one performs better for the current dataset.

The starting point is a database containing approximately two months of measurements from several \acp{rbs}. Then, the database is mined to find $n=50$ signals per feature with no missing data. After that, data has been artificially removed at a set ratio using a uniform distribution so that it can be simulated a \ac{mcar} scenario \cite{rantou2017missing}.

The two algorithms run to estimate the process models and use them to run the Kalman smoothing. It is used the coefficient of determination $R^2$ to compare the performance of the models.

\begin{equation}\label{eq:R2}
	R^2 = 1 - \frac{\sum_{i}{(y_i - \hat{y}_i)^2}}{\sum_{i}{(y_i - \bar{y})^2}}
\end{equation}

Where $y_i$ refers to the $i$-th data sample, $\hat{y}$ its estimated value and $\bar{y}$ the series mean.

Thus, having perfect predictions would cancel out the right-hand term's numerator and produce a score equal to 1. Therefore, the closer the score is to 1, the better quality has been the data imputation.

The process of removing data, imputing and computing the coefficient of determination value is performed iteratively while increasing the amount of simulated missing data. The mean of the $R^2$ values from the  selected \ac{rbs} are reported.

In Figure \ref{alg:imputing_exp}, it is shown the experiment's algorithm pseudocode in order to give more context of the meaning of the results plots.

\begin{figure}[hptb]
\begin{lstlisting}[keywords={,let, input, output, return, datatype, function, in, if, else, foreach, while, begin, end, do, }, mathescape=true, tabsize=4, basicstyle=\ttfamily\small]
input : database, ratio_step, ratio_min, ratio_max
output: mean_scores

let n $\gets$ rbs batch size
let grid $\gets$ [ratio_min : ratio_step : ratio_max]
let mean_scores $\gets$ []

foreach feature in database.get_features() do:
	sites $\gets$ database.get_random_sites(n)
	i $\gets$ 0
	foreach ratio in grid do:
		let arima_scores $\gets$ []
		let structural_scores $\gets$ []
		j $\gets$ 0
		foreach site in sites do:
			data $\gets$ database.get_site_data(site, feature)
			sim_data $\gets$ remove_uniform_random_data(data, ratio)

			imputed_arima $\gets$ impute_with_arima(sim_data)
			imputed_structural $\gets$ impute_with_structural_model(sim_data)

			arima_scores[j] $\gets$ $R^2$(data, imputed_arima)
			structural_scores[j] $\gets$ $R^2$(data, imputed_structural)
			j $\gets$ j + 1
		mean_scores[i] $\gets$ (ratio, mean(arima_scores), mean(structural_scores))
		i $\gets$ i + 1
return mean_scores
\end{lstlisting}
\caption{Comparative imputing experiment pseudocode.}
\label{alg:imputing_exp}
\end{figure}

\pagebreak

Figure \ref{fig:imp_exp_good} shows the results of the imputation comparison for the radio traffic as example. It can be seen that the $R^2$ value decreases --as expected-- when the missing data ratio increase. It also can be seen that, in this example, and most of them, structural models performs better than ARIMA models for imputing.

\begin{figure}[hptb]
	\centering
	\includegraphics[width=0.7\textwidth]{imp_traffic}
	\caption{Radio traffic load imputation}
	\label{fig:imp_exp_good}
\end{figure}

%\begin{figure}[hptb]
%	\centering
%	\begin{subfigure}{.48\textwidth}
%		\includegraphics[width=\textwidth]{imp_traffic}
%		\caption{Radio traffic load imputation}
%		\label{fig:imp_radio_traffic}
%	\end{subfigure}%
%	\hfill
%	\begin{subfigure}{.48\textwidth}
%		\includegraphics[width=\textwidth]{imp_temp}
%		\caption{Cabinet temperature imputation}
%		\label{fig:imp_temp}
%	\end{subfigure}
%	\caption{Imputation experiments good results}
%	\label{fig:imp_exp_good}
%\end{figure}

Although promising results were obtained for some of the imputed signals, there were other cases where the experiment did not perform as expected. In the following subsections, they will be discussed.

\subsection{Unintuitive $R^2$ values}

There are cases in which Auto-ARIMA estimation show poor and even negative $R^2$ values as shown in Figure \ref{fig:imp_exp_issue}. These are unintuitive results, as $R^2$ values are usually expected to be limited to the $[0,1]$ interval.

Nonetheless, this is meant only for linear models, where the worst fitted model is assumed to be the observations mean \cite{wackerly}. Thus, having negative $R^2$ values implies that the observations mean explains more variance than the fitted model.

\begin{figure}[hptb]
	\centering
	\begin{subfigure}{.48\textwidth}
		\includegraphics[width=\textwidth]{imp_sys_voltage}
		\caption{\ac{pdu} system voltage imputation.}
		\label{fig:imp_pdu_sys_voltage}
	\end{subfigure}%
	\hfill
	\begin{subfigure}{.48\textwidth}
		\includegraphics[width=\textwidth]{imp_power_load}
		\caption{Average \ac{psu} utilisation imputation.}
		\label{fig:imp_psu_load}
	\end{subfigure}
	\caption{Imputation experiments with bad results.}
	\label{fig:imp_exp_issue}
\end{figure}

\subsection{Optim convergence failures}

The model estimation threw runtime exceptions for some features due to the R code calls to the \texttt{optim} library not converging. These exceptions were found to be an actual bug in the library that occurs when the function being optimised tends to a constant value as shown in Figure \ref{fig:imp_conv_issue}. A dedicated routine was written to catch whenever this exception was thrown and run a simple interpolation instead of estimating the model.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.55\linewidth]{imp_conv_issue}
	\caption{Example of problematic signals for Auto-ARIMA estimation.}
	\label{fig:imp_conv_issue}
\end{figure}

\subsection{Conclusion of the imputing experiments}

The imputations using ARIMA models have shown to be unstable for some signals. Even though when, for the same signals, the structural model does not overperform either, it stills better than the very negative $R^2$ scores produced by the ARIMA approximations. In conclusion, based on this experiment's results, the structural models' approach has been chosen to impute the missing data and construct the database.

\section{Forecasting initial experiments}

The average \ac{psu} load signal shown in Figure \ref{fig:forecast_experiment_signal} has been chosen to run the following experiments. It is not a particularly easy signal since it contains some severe outliers in the first days that turned the power to almost zero, and in the end, the average power consumption seems to decrease.

It is necessary to notice that in every forecasting model, the further the prediction horizon is, the more uncertainty and, thus, the lower performance. For this experiment, it has been decided to leave the last 10\% of data for testing purposes.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.6\linewidth]{forecast_experiment_signal}
	\caption{Average PSU load signal used for the forecasting experiments}
	\label{fig:forecast_experiment_signal}
\end{figure}

In the following sections, the baselines models will be implemented first. Then, results of the univariate Prophet model will be shown. %To, later, add more exogenous regressors to observe how the predictions are improved.
The models will be evaluated by using the scores described in Section \ref{subsec:eval_criteria}.

\subsection{Evaluation criteria}\label{subsec:eval_criteria}
\subsubsection*{Coefficient of determination}

As previously mentioned, the coefficient of determination $R^2$ is a measure of how much variance of $\hat{y}$ is explained by the variance in $y$ in a linear regression context. It is compared against the considered \emph{worst possible linear approximation} which corresponds to the samples mean.

Although the current application is not linear, $R^2$ is still considered a valuable score to compare predictor performances. Nonetheless, as the linearity assumption is not met, its values would reside in $(-\infty, 1]$, where 0 still is the mean value, but it is no longer considered the worst possible fit.

%\begin{equation}\label{eq:rsq}
%	R^2 = 1 - \frac{\sum_{i}{(y_i - \hat{y}_i)^2}}{\sum_{i}{(y_i - \bar{y}_i)^2}}
%\end{equation}

\subsubsection*{Mean absolute error}

It measures, in absolute terms, the deviations from the true values. \ac{mae} is preferred, given its interpretability, over \ac{rmse} \cite{MAE}.

\begin{equation}\label{eq:mae}
	\text{MAE}	= \frac{1}{N} \sum_{i=1}^{N}{ \left| y_i - \hat{y}_i \right| }
\end{equation}

\subsubsection*{Mean out-of-bounds error}

As a result of the fitting or forecasting process, Prophet's output is comprised by the mean prediction $\hat{y}_t$ and also its lower and upper boundaries $\hat{y}_t - \Delta\hat{y}_t$ and $\hat{y}_t + \Delta\hat{y}_t$, respectively. Where $\Delta \hat{y}_t$ is the computed confidence interval half-magnitude for the time $t$. The width of this interval is defined by the user and defaults to $80\%$

Let  the \ac{obe} be the out-of-confidence-bands error defined as zero if the true value $y_t$ lies inside the confidence interval, and if it lies outside, define the distance to the closest confidence boundary as defined in (\ref{eq:obe}). Then the \ac{mobe} of $N$ samples can be obtained by taking the mean of these values to obtain an overall performance score as in (\ref{eq:mobe}).

\begin{equation}
	\text{OBE}_t =
	\begin{cases}
		\hfil 0  & \text{, if } \hat{y}_t - \Delta\hat{y}_t \leq y_t \leq \hat{y}_t + \Delta\hat{y}_t \\
		\bm{\min} \Big\{ \big| y_t - (\hat{y}_t - \Delta\hat{y}_t) \big| , \big	| y_t - (\hat{y}_t + \Delta\hat{y}_t) \big| \Big\} & \text{, otherwise}\\
	\end{cases}\label{eq:obe}
\end{equation}

\begin{equation}
	\label{eq:mobe}
	\text{MOBE} = \frac{1}{N} \sum_{t=1}^{N}{ \text{OBE}_t }
\end{equation}

\subsection{Baseline predictions models}

For the imputation step, the seasonal ARIMA and structural models have been learnt to fill the missing values by applying the Kalman smoothing algorithm. Furthermore, these models can also be used to forecast future observations. They are not expected to overperform. Nonetheless, predicting with them can be used as a benchmark to compare other models' improvements over their baseline.

\subsubsection*{Structural model predictions}
\label{subsubsec:structts_preds}

Figure \ref{fig:base_sts_overall} shows the overall performance of the structural time series predictor. It can be seen that its predictive mean tends to follow the test dataset mean, whereas the confidence interval is wider the further the prediction horizon is. More detailed plots are available in Appendix \ref{app:structts_preds}.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.8\linewidth]{baseline_imp_overall-sts}
	\caption{Structural model 3-days baseline overall performance}
	\label{fig:base_sts_overall}
\end{figure}

When plotting how the predictions $\hat{y}$ and the real values $y$ are distributed, the ideal prediction would be denoted by the positive unitary line. Therefore, the results in Figure \ref{fig:base_sts_joints} can be considered very poor.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.55\linewidth]{baseline_imp_joints-sts}
	\caption{Structural model 3-days baseline joint distribution}
	\label{fig:base_sts_joints}
\end{figure}

\subsubsection*{Auto-ARIMA implementation}

Same as in Section \ref{subsubsec:structts_preds}, an Auto-ARIMA model is trained and used to make predictions. In this case, it can be seen that, in the short-range, the predictive mean tries, but poorly succeeds to follow the actual data fluctuations. However later, in the long-range, it converges to the data mean. More detailed plots of the results can be found in Appendix \ref{app:arima_preds}.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.8\linewidth]{baseline_imp_overall}
	\caption{Auto-ARIMA 3-days baseline overall performance}
	\label{fig:base_arima_overall}
\end{figure}

Although the joint distribution of predictions and true values has been slightly improved, it still is very poor.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.55\linewidth]{baseline_imp_joints}
	\caption{Auto-ARIMA 3-days baseline joint distribution}
	\label{fig:base_arima_joints}
\end{figure}

\subsubsection*{Overall baselines scores}

In Table \ref{table:base_scores}, the long-range prediction of both baselines is summarised. They perform very poorly; nonetheless, as naive baselines, it is not expected any other outcome.

\begin{table}[H]
	\centering
	\begin{tabular}{|c|c|c|}
		\hline
		Score		& Structural Model	& Auto-ARIMA \\
		\hline
		\ac{mae} 	&  1.87 			& 1.95 \\
		$R^2$ 		& -0.02				& -0.12 \\
		\ac{mobe} 	&  0				& 0 \\
		\ac{rmse}	& 2.02 				& 2.12 \\
		\hline
	\end{tabular}
	\caption{Baselines long-range performance}
	\label{table:base_scores}
\end{table}

\subsection{Univariate Prophet model implementation}

Figure \ref{fig:prophet_uni_split} shows the complete results of training and testing for a univariate Prophet model. In comparison to the baselines, now it can be observed that the predictive mean tends to follow a more clear seasonal pattern. Nonetheless, in the testing set, it appears to miss the trend change.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.8\linewidth]{prophet_uni_split}
	\caption{Training, test and predictions from the univariate Prophet model}
	\label{fig:prophet_uni_split}
\end{figure}

More detailed plots can be found in Appendix \ref{app:uniprophet}.

\subsubsection*{Model learnt components}

Figure \ref{fig:prophet_uni_components} presents the learnt approximation of every component in the \ac{gam}. Although the trend did not overfit the outliers, it stills learnt that there is a breakpoint and changed the piecewise trend.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.65\linewidth]{prophet_uni_components}
	\caption{Learnt components from the univariate Prophet model}
	\label{fig:prophet_uni_components}
\end{figure}

\subsubsection*{Data and predictions joint distribution}

If the joint distributions for the true values $y$ and the predicted values $\hat{y}$ are plotted, it can be seen how the outlier in the training set was not learnt and how the approximation becomes erratic in the test set. Nonetheless, they already show a considerable improvement from the baselines distributions.

\begin{figure}[hptb]
	\centering
	\begin{subfigure}{.49\textwidth}
		\includegraphics[width=\textwidth]{prophet_train_res_joint}
		\caption{Train}
		\label{fig:prophet_train_res_joint}
	\end{subfigure}%
	\hfill
	\begin{subfigure}{.49\textwidth}
		\includegraphics[width=\textwidth]{prophet_test_res_joint}
		\caption{Test}
		\label{fig:prophet_test_res_joint}
	\end{subfigure}
	\caption{Univariate Prophet joint distributions of $y$ and $\hat{y}$}
	\label{fig:prophet_res_joint}
\end{figure}

\subsection{Prophet implementation using exogenous regressors}

To fully exploit the flexibility of \acp{gam}, Prophet's API allows defining custom regressors, which in the current work will be called exogenous variables as a resemblance of SARIMAX models. These variables are the ones explained in Chapter \ref{cha:data_analysis}.

The overall performance can be seen in Figure \ref{fig:prophet_multi_split}. Compared to the univariate model, it can be seen that the confidence bounds now are narrower since there is less uncertainty in the predictions. It also can be seen that the model now can predict the trend change in the test set.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.8\linewidth]{figures/prophet_multi_split}
	\caption{Training, test and predictions from the multivariate Prophet model}
	\label{fig:prophet_multi_split}
\end{figure}

\subsubsection*{Model learnt components}

The model components now show the additive factor of all the exogenous regressors, which seems to contain the information not learnt by the univariate training residuals.

%
%The $R^2$ score has considerably improved. Nonetheless, the other scores have become worse. The interpretation of this decline could be that the confidence band is now narrower than the univariate model. Thus, being out of the confidence band is now likely than before.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.65\linewidth]{prophet_multi_components}
	\caption{Learnt components from multivariate Prophet}
	\label{fig:prophet_multi_components}
\end{figure}

\subsubsection*{Data and predictions joint distribution}

The joint distributions for $y$ and $\hat{y}$ in Figure \ref{fig:prophet_test_res_joint_multi} now show that the model has improved significantly  its prediction accuracy compared against the baselines.

In the plot for the training set, it can be seen that the trend model approximates the abrupt drop. Nonetheless, the positive trend in the correlation between  $y$ and $\hat{y}$ in the test set shows that the model is not overfitting the training data. Moreover, the trend drop in the testing set is also approximated without issues.

\begin{figure}[hptb]
	\centering
	\begin{subfigure}{.49\textwidth}
		\includegraphics[width=\textwidth]{prophet_train_res_joint_multi}
		\caption{Train}
		\label{fig:prophet_train_res_joint_multi}
	\end{subfigure}%
	\hfill
	\begin{subfigure}{.49\textwidth}
		\includegraphics[width=\textwidth]{prophet_test_res_joint_multi}
		\caption{Test}
		\label{fig:prophet_test_res_joint_multi}
	\end{subfigure}
	\caption{Multivariate Prophet joint distributions of $y$ and $\hat{y}$}
	\label{fig:prophet_res_joint_multi}
\end{figure}

\subsection{Example forecast performance comparison}

In order to quantify the improvement of the long-range predictions made by the usage of the Prophet model. Table \ref{table:prophet_scores} summarises the performance scores.

\begin{table}[H]
\centering
\begin{tabular}{|l|c|c|c|c|}
	\hline
					& \ac{mae}	&	$R^2$		& \ac{mobe}  & \ac{rmse} 	\\
	\hline
	Univar - Train 	&	2.16	&	0.14		& 	0.04	&	2.53		\\
	Univar - Test 	&	2.11	&	-0.57		&	0.04	&	2.70		\\
	Multivar - Train&	0.55	&	0.83		& 	0.05	&	1.13		\\
	Multivar - Test &	0.57	&\textbf{0.85}	&	0.03	&\textbf{0.85}	\\
	\hline
\end{tabular}
\caption{Example of long-range prediction performance by the Prophet model}
\label{table:prophet_scores}
\end{table}

In this example, the significant improvement done by using the exogenous predictors in the multivariate model can be observed, obtaining a $R^2=0.85$, which for a non-linear case can be considered a good approximation.

\section{Exhaustive forecasting experiments}

After visualising how the models comparatively perform in an example case, a general view of how they perform is needed. Therefore, similar to the experiments in Section \ref{sec:imp-exps}, it is performed an exhaustive iterative evaluation of the predictions is performed while moving the predicting horizon further and further.

In the following subsections, the exhaustive baseline results are presented, and then Prophet model results are obtained to visualise its general improvement.

\subsection{Baselines predictions moving horizon}

\begin{figure}[htpb]
	\centering
	\begin{subfigure}{.49\textwidth}
		\includegraphics[width=\linewidth]{baseline_db-structts_MAE}
		\caption{Baseline MAE}
	\end{subfigure}%
	\hfill
	\begin{subfigure}{.49\textwidth}
		\includegraphics[width=\linewidth]{baseline_db-structts_RMSE}
		\caption{Baseline RMSE}
	\end{subfigure}
	\caption{Baseline predictions performance vs forecast horizon time}
	\label{fig:baseline_performances}
\end{figure}

Figure \ref{fig:baseline_performances} shows the Auto-ARIMA and structural model performances together. The curves are interesting due to their peak-valley seasonal-alike shapes. Their possible explanation is related to the original signal peaks and valleys variability, i.e., the minima are more show less variability than the maxima. Therefore, the predictions for the minima are less error-prone than for the maxima.

On the other hand, it can be seen that, opposed to the results for the imputation phase, the structural model provides poorer predictions than the ARIMA. Thus, if any of the baselines would have to be chosen, the Auto-ARIMA would be the best candidate.

\subsection{Univariate Prophet prediction moving horizon}
\label{subsec:Results-Experiments-UnivariateProphet}

This set of experiments have been found to require hefty computing power and time. Since the following overall experiment took $\sim17$ hours to finish, it has been limited to sampling 50 models performances for a three days long-range horizon maximum.

%\begin{figure}[H]
%	\centering
%	\begin{subfigure}{0.49\linewidth}
%		\includegraphics[width=\linewidth]{}
%		\caption{}
%	\end{subfigure}
%	\hfill
%	\begin{subfigure}{0.49\linewidth}
%		\includegraphics[width=\linewidth]{}
%		\caption{}
%	\end{subfigure}
%	\caption{}
%	\label{fig:}
%\end{figure}

\begin{figure}[H]
	\centering
	\begin{subfigure}{0.49\linewidth}
		\includegraphics[width=\linewidth]{exp_prophet_uni_mae_time}
		\caption{\ac{mae}}
	\end{subfigure}
	\hfill
	\begin{subfigure}{0.49\linewidth}
		\includegraphics[width=\linewidth]{exp_prophet_uni_rmse_time}
		\caption{\ac{rmse}}
	\end{subfigure}
	\caption{Univariate Prophet predictions performance vs forecast horizon time}
	\label{fig:uni_prophet_performances}
\end{figure}

In the results in Figure \ref{fig:uni_prophet_performances}, it can be observed that as long the time horizon increases, as expected, the prediction errors tend to increase also. While \ac{mae} show that the prediction errors are consistently higher than the training error, the \ac{rmse} shows lower scores for the testing set than for the training when the time horizon is close to the present time. This shift can be explained by the fact that \ac{rmse} penalises higher the large errors compared to \ac{mae}.

It is important to notice that, as the \ac{psu} utilisation signal is already in percent, these results are easily interpretable as percent error also. Therefore, in the long-range, the univariate Prophet model, shows that in average it can achieve $\text{MAE}\sim2.5$ and $\text{RMSE} \sim 3.6$ \ac{psu} per-cent utilisation.

%\subsubsection*{Mean Absolut Error}
%\begin{figure}[H]
%	\centering
%	\includegraphics[width=0.8\linewidth]{exp_prophet_uni_mae_time}
%	\caption{Univariate moving prediction horizon: \ac{mae}}
%	\label{fig:exp_prophet_uni_mae}
%\end{figure}
%
%\subsubsection*{Root Mean Squared Error}
%\begin{figure}[H]
%	\centering
%	\includegraphics[width=0.8\linewidth]{exp_prophet_uni_rmse_time}
%	\caption{Univariate moving prediction horizon: \ac{rmse}}
%	\label{fig:exp_prophet_uni_rmse}
%\end{figure}

\subsection{Multivariate Prophet prediction moving horizon}

Now it is the turn of the Prophet model with external regressors that showed the best performance in the first example. Therefore, it is expected to maintain that tendency but now with more confidence that the performance in the example was not some fortunate random event.

These experiments have consumed even more computing time than the previous in Section \ref{subsec:Results-Experiments-UnivariateProphet}, having spent more than 18.5 hours to finish.

\begin{figure}[H]
	\centering
	\begin{subfigure}{0.49\linewidth}
		\includegraphics[width=\linewidth]{exp_prophet_mv_mae_time}
		\caption{\ac{mae}}
	\end{subfigure}
	\hfill
	\begin{subfigure}{0.49\linewidth}
		\includegraphics[width=\linewidth]{exp_prophet_mv_rmse_time}
		\caption{\ac{rmse}}
	\end{subfigure}
	\caption{Multivariate Prophet predictions performance vs forecast horizon time}
	\label{fig:multi_prophet_performances}
\end{figure}

The results in Figure \ref{fig:multi_prophet_performances} show that the long-range prediction results for the Prophet model using exogenous regressors are consistently good having both, $\text{MAE}$ and $\text{RMSE}$ below $1\%$.

%\begin{figure}[H]
%	\centering
%	\includegraphics[width=0.8\linewidth]{exp_prophet_mv_mae_time}
%	\caption{Multivariate moving prediction horizon: \ac{mae}}
%	\label{fig:exp_prophet_mv_mae}
%\end{figure}
%\begin{figure}[H]
%	\centering
%	\includegraphics[width=0.8\linewidth]{exp_prophet_mv_rmse_time}
%	\caption{Multivariate moving prediction horizon: \ac{rmse}}
%	\label{fig:exp_prophet_mv_rmse}
%\end{figure}

Other noticeable results are the ones shown in Figure \ref{fig:exp_prophet_mv_r2}, in which the $R^2$ scores in long-range are $\sim0.88$. Although it might seem unintuitive to have better $R^2$ in the long-range rather than in the short term, it can be explained by the amount of data used to compute the score: the less data, the more likely to have \emph{a general bad approximation}.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.65\linewidth]{exp_prophet_mv_r2_time}
	\caption{Multivariate Prophet model $R^2$ vs forecast horizon time}
	\label{fig:exp_prophet_mv_r2}
\end{figure}

\subsection{Time performances}

Figure \ref{fig:time-perfs} shows the average consumed time for running the exhaustive experiments in Ericsson's computing farm. It can be seen that the testing time is very similar for univariate and multivariate case. On the other hand, the training time for the multivariate model is slightly higher than the univariate one. Nonetheless, both are under half of a second on average. It is also interesting that the longer the prediction range, the times remain almost constant.

\begin{figure}[H]
	\centering
	\begin{subfigure}{0.49\linewidth}
		\includegraphics[width=\linewidth]{exp_prophet_uni_time}
		\caption{Univariate experiment computing time}
	\end{subfigure}
	\hfill
	\begin{subfigure}{0.49\linewidth}
		\includegraphics[width=\linewidth]{exp_prophet_mv_time}
		\caption{Multivariate experiment computing time}
	\end{subfigure}
	\caption{Experiments computing times}
	\label{fig:time-perfs}
\end{figure}

\section{Performance summary}

Finally, for ease of comparison this section summarises the average performances per day for all the models under analysis. Table \ref{table:mae_perf_summary} shows that the Prophet model with exogenous regressors vastly outperforms the other methods in terms of \ac{mae}.

\begin{table}[H]
	\centering
	\begin{tabular}{|c|c|c|c|}
		\hline
		\textbf{Model} & \textbf{1 day} & \textbf{2 day}	& \textbf{3 day} \\
		\hline
		Structural		 & 		2.55 	&		3.17 		& 3.08 			\\
		\hline
		ARIMA 			 &		2.59 	& 		2.812		& 2.70 			\\
		\hline
		UniVar-Prophet 	 & 		2.88 	& 		2.96 		& 3.01 			\\
		\hline
		MultiVar-Prophet & \textbf{0.41}& \textbf{0.40}		& \textbf{0.40} \\
		\hline
	\end{tabular}
	\caption{\ac{mae} Summary of models performance by day}
	\label{table:mae_perf_summary}
\end{table}

Likewise, in Table \ref{table:rmse_perf_summary}, it can be observed the long-range stability of the multivariate Prophet model having, again, the best \ac{rmse} scores.

\begin{table}[H]
	\centering
	\begin{tabular}{|c|c|c|c|}
		\hline
		\textbf{Model} & \textbf{1 day} & \textbf{2 day}	& \textbf{3 day} \\
		\hline
		Structural		 & 		3.27 	&		4.31 		& 4.35 			\\
		\hline
		ARIMA 			 &		3.22 	& 		3.77		& 3.81 			\\
		\hline
		UniVar-Prophet 	 & 		3.54	& 		3.88		& 4.02 			\\
		\hline
		MultiVar-Prophet & \textbf{0.62}& \textbf{0.71} 	& \textbf{0.68}	\\
		\hline
	\end{tabular}
	\caption{\ac{rmse} Summary of models performance by day}
	\label{table:rmse_perf_summary}
\end{table}

Lastly, Table \ref{table:r2_perf_summary} presents how the multivariate Prophet model is even capable of having acceptable $R^2$ scores, which in this type of data is not an easy task.

\begin{table}[H]
	\centering
	\begin{tabular}{|c|c|c|c|}
		\hline
		\textbf{Model} & \textbf{1 day} & \textbf{2 day}	& \textbf{3 day} \\
		\hline
		Structural		 & 		-7.23 	&		-8.90 		& -2.90 		  \\
		\hline
		ARIMA 			 &		-95.76 	& 		-2.25		& -0.23 		  \\
		\hline
		UniVar-Prophet 	 & 		-203.76	& 		-1.04 		& -0.98 	      \\
		\hline
		MultiVar-Prophet & \textbf{0.33}& 	\textbf{0.85} 	& \textbf{0.86} \\
		\hline
	\end{tabular}
	\caption{$R^2$ Summary of models performance by day}
	\label{table:r2_perf_summary}
\end{table}

\section{Power headroom estimation}

Until this point of the work, the research has been mainly focused on PSU loads forecasting. Nonetheless, as mentioned in the introductory context, the goal is to predict the power headroom defined in (\ref{eq:phdroom}). However, as there are no direct measurements of power headroom that can be learnt, it needs to be derived from the power consumption.

Therefore, to provide a power headroom forecast, after estimating the loads future values, a further step is needed to translate that information into power headroom terms. The following sections will propose a way to manage to achieve it and discuss other aspects of its technical applicability.

\subsection{Power headroom derivation as PSU utilisation complement}

As it has been exposed in Section \ref{subsec:data_description:power_supply}, the power load is a measure of \emph{how many percentual power capacity it is being used at that time}. Thus, it is straightforward to claim that the percentual power headroom is its complement.

\begin{align}\label{eq:ph_deriv}
	\begin{split}
		P_{h[\%]}	&= 100 - P_{L[\%]} \\
		\text{Where }P_{h[\%]}	&: \text{Power headroom in percents} \\
		\text{and } P_{L[\%]}	&: \text{Power loads consumptions in percents}
	\end{split}
\end{align}

If the interest is to obtain a measurement in Watts units, the installed power capacity in the RBS, $P_{max}$, needs to be known so that its proportion can be computed as showed in (\ref{eq:phwatts}).

\begin{align}\label{eq:phwatts}
	\begin{split}
		P_h	&= P_{max} \cdot \frac{P_{h[\%]}}{100} \\
		\text{Where } P_{max}	&: \text{Installed power capacity in Watts}
	\end{split}
\end{align}

Figure \ref{fig:ph_der}, shows the \ac{psu} utilisation signal used as example test in Chapter \ref{cha:forecasting} and its derived power headroom.

%\begin{figure}[H]
%	\centering
%	\begin{subfigure}{0.49\linewidth}
%		\includegraphics[width=\linewidth]{}
%		\caption{}
%	\end{subfigure}
%	\hfill
%	\begin{subfigure}{0.49\linewidth}
%		\includegraphics[width=\linewidth]{}
%		\caption{}
%	\end{subfigure}
%	\caption{}
%	\label{fig:}
%\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=0.65\linewidth]{power_headroom_derivation}
	\caption{Power headroom derivation from PSU Load}
	\label{fig:ph_der}
\end{figure}

%\begin{figure}[hptb]
%	\centering
%	\begin{subfigure}{.65\textwidth}
%		\centering
%		\includegraphics[width=\textwidth]{power_headroom_derivation}
%		\caption{Derived power headroom in percents}
%		\label{fig:ph_der_percent}
%	\end{subfigure}%
%	\hfill
%	\begin{subfigure}{.65\textwidth}
%		\centering
%		\includegraphics[width=\textwidth]{power_headroom_derivation_watts}
%		\caption{Derived power headroom in Watts}
%		\label{fig:ph_der_watts}
%	\end{subfigure}
%	\caption{Power headroom derivation from PSU Load}
%	\label{fig:ph_der}
%\end{figure}

\subsection{Criterion considerations}

In general terms, an alarm is meant to make someone notice that something abnormal is happening in a given process or, in a forecasting context, may (or will) occur in the future, to support the operator response \cite{iec_alarms}.

In the present works' context, the alarm meaning can be conceived as the \ac{rbs} not having enough power to maintain its by-design behaviour.

Although it should be possible to define how likely a power overload is, given the system's current state in probability terms, triggering an alarm is a binary task. The system is operating either in a \emph{"safe"}  or in an \emph{"abnormal"} region. Then, crossing a boundary that separates these two regions is the trigger for the alarm.

There are different techniques to tune an optimal trigger level. It could be done based on a system variable, or latent variable, or even a joint distribution of the two kinds \cite{izadi2009alarms}.

Choosing the best possible boundary is considered to be out of scope. Nonetheless, as an initial approach, it can be proposed to be settable by the user according to their needs and expertise.

From this assumption and the derivations in (\ref{eq:ph_deriv}) and (\ref{eq:phwatts}), the following guidelines should be taken into consideration.

\subsubsection*{Values interpretation}

Percentages values are not always able to fully represent the system conditions. For example, it is very different having 10\% of power headroom where the total capacity is 10 kW and 1 kW since the relative oscillations could be drastically different. Therefore, the power magnitude should be taken into consideration.

\subsubsection*{PSUs do not partially fail}

The power capacity is not continuous. It will have discrete values as shown in Figure \ref{fig:ph_lvls}, and also, the \acp{psu} can have different power contributions. This quantisation means that in order to set the threshold is needed to take into account the worst-case scenario, which can be depicted by the sentence: \emph{"can the \ac{rbs} work if the next failure is the most power contributing \ac{psu}?"}

%\begin{figure}[hptb]
%	\centering
%	\begin{subfigure}{.65\textwidth}
%		\centering
%		\includegraphics[width=\textwidth]{power_headroom_levels_percent}
%		\caption{Power headroom and discrete availability in percents}
%		\label{fig:ph_lvls_percent}
%	\end{subfigure}%
%	\hfill
%	\begin{subfigure}{.65\textwidth}
%		\centering
%		\includegraphics[width=\textwidth]{power_headroom_levels_watts}
%		\caption{Power headroom and discrete availability in Watts}
%		\label{fig:ph_lvls_watts}
%	\end{subfigure}
%	\caption{Power headroom derivation from PSU Load}
%	\label{fig:ph_lvls}
%\end{figure}

\begin{figure}[hptb]
	\centering
	\includegraphics[width=.65\textwidth]{power_headroom_levels_percent}
	\caption{Power headroom and discrete availability in percents}
	\label{fig:ph_lvls}
\end{figure}

\pagebreak
\subsection{Alarm triggering and the $n$-level safety criteria}

Let $P_{max}$ be the maximum power capacity installed and working in an \ac{rbs},  $\bar{P}_{Load}$ the average \ac{psu} utilisation and $\Delta p$ the confidence interval used when training the Prophet model. Let $N$ be the amount of working \acp{psu}, let $\{P_{PSU_1}, \ldots, P_{PSU_N}\}$ such that $P_{PSU_1} > \ldots > P_{PSU_N}$ are the descending sorted power contributions from the working \acp{psu}.

Then, let the $n$-level safety criteria, be the $n$ amount of worst-case \ac{psu} failures to keep as operational margin:

\begin{align}
	\begin{split}
		P_{max} - \sum_{i=1}^{n}{P_{PSU_i}} &> \bar{P}_{Load} + \Delta p \\
		P_{max} - \left\{\bar{P}_{Load} + \Delta p \right\} &> \sum_{i=1}^{n}{P_{PSU_i}}
	\end{split}
\end{align}

However, as the power headroom is by definition the difference between the installed power capacity and the power being consumed, it can be said that:

\begin{align}
	\begin{split}
		P_{h} &= P_{max} - \left\{\bar{P}_{Load} + \Delta p \right\} \\
		P_{h} &> \sum_{i=1}^{n}{P_{PSU_i}} \\
		\therefore P_{crit_n} &= \sum_{i=1}^{n}{P_{PSU_i}}
	\end{split}
\end{align}

Where $ P_{crit_n}$ is the $n$-level criteria threshold. Thus, whenever the power headroom forecast $\hat{P}_h$ at time $t+k$, $t$ being the current time, meets the condition in (\ref{eq:n-trigger}) a fault alarm must be triggered

\begin{equation}
	\label{eq:n-trigger}
	\hat{P}_{h_{t+k}} \geq P_{crit_n}
\end{equation}


% ***********************************************************************************
% Pure LaTeX part to be inserted in a document (be careful of depencies of packages & commands
% Prepared by XXX and YYY under the supervision of Arnaud de La Fortelle
% Fall 2017
% 2D wave propagation subsection of the modeling part
% ***********************************************************************************

\subgroup{1}{Bradley Cage and Lin Yang}

\paragraph{Description}
\begin{figure}[htb]
	\centering
	\includegraphics[width=10cm]{Figures/2D_waves_system.png}
	\caption{The membrane system }
	\label{2D_waves_system.fig}
\end{figure}

Our system is comprised of a flexible membrane stretched to some shape, with all of its edges fixed in place. The desired goal is to understand the vertical position of the various points on the membrane over time. The membrane in this system has vertical deflections which are small compared to its overall size, and deflections happen only in the vertical direction.

This 2D system is a continuation of the 1D wave equation, and is a natural precursor to the 3D wave case.

\paragraph{Model}
Assumptions:
\begin{itemize}
	\item Membrane has uniform planar density $\rho$
    \item The tension per unit length, $F_t$, caused by stretching the membrane is the same at all points and in all directions and does not change during the motion
    \item Vertical position is given by some function $u(x,y,t)$
\end{itemize}
\begin{figure}[htb]
	\centering
	\includegraphics[width=15cm]{Figures/2D_waves_model.png}
	\caption{The force analysis of a small section of the membrane system }
	\label{2D_waves_model.fig}
\end{figure}
We begin from basic principles.

$$\Sigma F = m\vec{a}$$

\noindent Taking some small section of the membrane $\ud x$ by $\ud y$, we can replace mass and acceleration and since we know density and that $\vec{a}$ is the second derivative of position with respect to time, thus enabling us to rewrite the equation.

\begin{equation}
\label{no_balance}
\Sigma F = \rho \ud x\ud y \frac{\partial^2u}{\partial t^2}
\end{equation}

\noindent Performing a force balance on the section of membrane in the x and y directions gives us tensions at each on each side, then resolved to their vertical components. Remember that since tension is constant per unit length, we must multiply the force acting on each side by the length of that side. Thus, the force acting on this balance lets us rewrite $\Sigma F$ (that is we only consider vertical forces, forces acting in the $x-u$ and $y-u$ planes):

$$\Sigma F = F_x + F_y$$

$$F_x = F_t\ud y\Big[\sin\big( \theta (x+\ud x,y,t) \big) - \sin \big((\theta (x,y,t)\big)\Big]$$
$$F_y = F_t\ud x\Big[\sin\big( \beta (x,y+\ud y,t) \big) - \sin \big((\beta (x,y,t)\big)\Big]$$

\noindent We can confidently use the small angle approximation $\sin$ in the x direction

$$ \sin(\theta) \approx \tan(\theta) = \frac{\partial u}{\partial x} = u_x$$

\noindent and likewise in the y direction

$$ \sin(\beta) \approx \tan(\beta) = \frac{\partial u}{\partial y} = u_y$$

\noindent to get our equations into the form

$$F_x = F_t\ud y\Big[u_x(x+\ud x,y,t) - u_x(x,y,t)\Big]$$
$$F_y = F_t\ud x\Big[u_y(x,y+\ud y,t) - u_y(x,y,t)\Big]$$

\noindent From there we can sum these forces and plug them back in to equation \ref{no_balance}

$$\rho \ud x\ud y \frac{\partial^2u}{\partial t^2} = F_t\bigg[dy\Big[u_x(x+\ud x,y,t) - u_x(x,y,t)\Big]+dx\Big[u_y(x,y+\ud y,t) - u_y(x,y,t)\Big] \bigg]$$

\noindent We then divide by $\ud x$ and $\ud y$ and take the limit as $\ud x,\ud y \to 0$:

$$\rho\frac{\partial^2u}{\partial t^2} = \lim_{\ud x,\ud y\to 0} F_t \bigg[ \frac{u_x(x+\ud x,y,t) - u_x(x,y,t)}{\ud x} + \frac{u_y(x,y+\ud y,t) - u_y(x,y,t)}{\ud y} \bigg]$$

\noindent We recognize that we now have derivatives in the form of difference quotients, and can take the partial derivative of each one (since $u$ is a function of multiple variables)

\begin{equation}
\rho \frac{\partial^2u}{\partial t^2} = F_t\bigg[\frac{\partial}{\partial x}u_x + \frac{\partial}{\partial y}u_y \bigg] = F_t\bigg[\frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2}\bigg]
\end{equation}

\noindent Dividing over the uniform  tension, we reach our final form.

\begin{equation}
\frac{\rho}{F_t}\frac{\partial^2u}{\partial t^2} = \frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2}
\end{equation}

\noindent We can adhere to standard conventions and write our final 2D wave equation as

\begin{align}
a^2 \frac{\partial^2u}{\partial t^2} &= \frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2} & a &= \sqrt{\frac{\rho}{F_t}}
\label{final_eq}
\end{align}

\noindent Equation \ref{final_eq} is also commonly written using the Laplace operator:

\begin{equation}
a^2 \frac{\partial^2u}{\partial t^2} = \nabla^2 u
\end{equation}

\paragraph{Initial and boundary conditions}
The initial state (i.e. at $t=0$) of the system is given by two functions:
\begin{itemize}
	\item the vertical position all over the membrane $u(x,y,0)=u_0(x,y)$;.
  \item the membrane speed $\frac{\partial u(x,y,0)}{\partial t}=u_1(x,y)$. When --- as usually assumed --- the membrane is at rest, then $u_1=0$.
\end{itemize}
\begin{figure}[htb]
	\centering
	\includegraphics[width=10cm]{Figures/2D_waves_boundary_conditions.png}
	\caption{The membrane system's boundaries}
	\label{2D_waves_boundary_conditions.fig}
\end{figure}
\noindent The membrane's area is ab (with the edge length being a,b respectively).

For membrane the boundary conditions can be of the two types (or a combination of):
\begin{itemize}
	\item The vertical positions of the 4 edges of the membrane remain fixed all the time, and usually at 0, i.e., $u(x,0,t)=0$, $u(0,y,t)=0$, $u(a,y,t)=0$, $u(x,b,t)=0$.
	\item Another type of boundary condition is the reflecting or no-flux boundary condition: this implies a derivative condition along the normal at the boundary and it is more technical to write (though not less meaningful in terms of physics).
\end{itemize}

Some hints for the control and the cost function of the 2D wave equation:
\begin{itemize}
	\item Control: Maintain the highest vertical deflection of the membrane to be some constant height H
  \item The cost function then could be finding the smallest force exerted on the membrane to maintain the highest height H in the membrane system
\end{itemize}


%
% Copyright 2018 Parakram Majumdar
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
%     http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
%

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[legalpaper, portrait, margin=1in]{geometry}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows,positioning}

\title{
                         Paragraph                    \\
  \large          A Backpropagation engine            \\
                          in C++
}
\author{Parakram Majumdar}

\begin{document}
  \maketitle

\tikzstyle{neuron} = [rectangle,
                      text centered,
                      draw=black,
                      minimum width=1.5cm,
                      minimum height=.8cm,
                      fill=blue!20]
\tikzstyle{arrow} = [thick,->,>=stealth]

\section{Introduction}
  The \emph{training} of Artificial Neural Networks
  involves calibrating the \emph{weights} of each neuron
  so that the \emph{output} generated by the neural network
  matches the expected output of a \emph{training set}.
  Most training algorithms employ some form of \emph{gradient descent},
  wherein each weight is adjusted in the opposite direction of
  the partial derivative of the error with respect to that weight.

  \[ \Delta w = -c \times \frac{\partial o}{\partial w} \]

  The \emph{backpropagation} algorithm employs \emph{algorithmic differentiation}
  to compute the gradient, and is especially common for training
  \emph{deep} neural networks.

  This document describes the anatomy of \emph{Paragraph},
  a backpropagation engine written in C++.

\section{Basic Concepts}
  A neural network consists of interconnected neurons, where each neuron,
  based on the input signals received from input connections,
  sends an output signal to output connections.

  \begin{center}
  \begin{tikzpicture}[node distance=2cm]
    \node (n) [neuron] {Neuron};
    \draw [arrow] ++(-2, -0.5) -- (n.west);
    \draw [arrow] ++(-2, 0) -- (n.west);
    \draw [arrow] ++(-2, 0.5) -- (n.west);
    \draw [arrow] (n.east) -- ++(1.25, -0.5);
    \draw [arrow] (n.east) -- ++(1.25, 0);
    \draw [arrow] (n.east) -- ++(1.25, 0.5);
  \end{tikzpicture}
  \end{center}

  Thus, the network can be considered a directed computational \texttt{Graph},
  where each \texttt{Node} represents a neuron.
  Each node is one of two types :
  \begin{itemize}
    \item \texttt{Operation}:
      Like neurons, operations react to their inputs to produce an output.
    \item \texttt{Variable}:
      Variables act as ``input nodes'', and do not have any inputs of their own.
      They allow the graph to be manipulated by external systems.
  \end{itemize}

  The nodes communicate to each other via \texttt{Tensor}s,
  or multi-dimensional arrays of real numbers (\texttt{double}s).
  The tensors also generalize quite well to represent gradients of multi-dimensional
  functions.
  These gradients can then be used to calibrate the variables in a graph.

\section{Tensors}
  \texttt{Tensor}s are multi-dimensional arrays of real numbers.
  An $n$-dimensional tensor has fixed dimensionalities
  $\left\{d_1, d_2, ..., d_n\right\}$, which drive the coordinates
  of each number stored in the array.

  Common operations on tensors are described in the TensorFunctions document.
  A notable operation is the \emph{tensor multiplication},
  a generalization of matrix multiplication,
  which is key in determining the gradient with respect to a node,
  given the gradients of its outputs.
  For example, suppose we have a node $g$
  which takes another node $f$ as input,
  then
  \[ \nabla (g \circ f) = \nabla g \cdot_n \nabla f \]
  Where $\cdot_n$ is the tensor multiplication operator on $n$ dimensions,
  $n$ being the dimesionality of $f$.

  Many useful computations on tensors can work in an iterative fashion,
  traversing the elements of the tensor in a row-major ordering.
  This means that storing the elements in contiguous memory in row-major
  ordering allows for noticable speed-ups from the co-location of data.
  With most modern operating systems providing virtual memory mappings,
  simply \texttt{alloc}ating large contiguous chunks of memory
  is a viable option.

  Furthermore, it is also critical that tensors allow for
  sparse representation of the data.
  A typical real life deep learning network will consist of
  many sparse but otherwise large tensors,
  rendering the computations intractable unless the sparsity is exploited.

\section{Node}
  A Node represents a tensor that is computed as part of a Graph.
  As mentioned earlier, nodes can be of two types: Variables and Operations.

  Variables, do not have any input nodes, and therefore,
  for the purposes of a single computation traversal, are contant tensors.
  However the graph allows for changing the values of these Variables
  across multiple computation traversals.
  Thus, effectively, if the graph can be considered a single function
  that outputs a tensor, its Variable nodes represent its inputs.

  Operations represent functions that go from multiple input tensors
  to a single output tensor.
  The backpropagation algorithm requires each operation to have
  a well defined first derivative with respect to its inputs.

  Paragraph represents a node as a thin \texttt{struct}
  with an \texttt{enum} representing the node type,
  and an \texttt{int} index uniquely identifying the node in a graph.

  Furthermore, the indices of the nodes happen to be in a
  topologically sorted order of the graph,
  as the Graph Builder assigns indices to the outputs of a node
  only after the node itself has been assigned.
  This fact is useful in determining the order in which
  nodes must be traversed in order to complete a traversal in the graph.
  However, in general, a graph may choose to ignore this ordering
  during computation, for example to reduce peak memory usage.
  The Graph does, however, use the node indices for storing and retrieving
  properties of each node, such as a node's name, its inputs and outputs,
  and for operation nodes, the tensor functor.

\section{Graph}
  \subsection{Forward propagation}
    Given the input values of Variable nodes, the Graph can compute
    the values for a set of output nodes.
    The input values for Variables which are not dependencies of the
    target output nodes need not be provided.

  \subsection{Backward propagation}
    The Graph also allows for the computation of the gradients
    of a given set of output nodes with respect to another given set
    of variable nodes.
    This is done in two traversals.

    First, a forward propagation step computes and stores
    the values of all intermediate nodes.

    Then, using a generalized chain rule for multi-argument tensor functions,
    a backward propagation step computes the derivative of the outputs
    with respect to each node.

  \subsection{Order of computation}
    It is somewhat easy to ensure that only nodes which are necessary
    are traversed, and are traversed only once (per propagation).
    This ensures that the computation time is minimized.

    Furthermore, it is also easy to ensure that the computed values
    of each node is dropped out of storage as soon as they become unnecessary.
    This allows us to reduce the peak memory consumption,
    which is key for making large graphs and/or large inputs tractable.
    However, the \emph{order} of computation of nodes,
    which may have further impact on key memory utilization,
    is currently not minimized. This requires future work.

\section{Graph Builder}
  While on one hand, it quite useful to have immutable graphs
  in a distributed processing environment, immutable graphs are quite
  cumbersome to set up.
  Thus, it is useful to have a separate construct, a Graph Builder,
  which is mutable and therefore convenient for building large graphs,
  and which can generate an immutable Graph on demand.

  The Graph Builder object allows the user to add nodes to the graph,
  along with their names, types, and in the case of operation nodes,
  their inputs and corresponding tensor functions.
  It automatically assigns indices to the nodes.

\end{document}


\documentclass[10pt,a4paper]{article}
\title{Sieve of Eratosthenes}
\date{}

\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
% For algorithms
\usepackage[linesnumbered,ruled,lined,boxed,commentsnumbered]{algorithm2e}

\usepackage{titling}

\setlength{\droptitle}{-10em}

\begin{document}
\maketitle

\begin{algorithm}[H]
	\KwData{A postive integer $n \geq 2$}
	\KwResult{An array $M[2 \text{ .. } n]$ such that $M[i] = 0$ if $i$ is
	prime and 1 if $i$ is composite}
	\BlankLine
	\Begin {
		Let $M[2 \text{ .. } n]$ be initialized with all elements = 0\;
		\BlankLine
		\For{$i \leftarrow 2$ to $n$} {
			\If{$M[i] \neq 1$} {
				\For{$j \leftarrow 2i$ and $j \leq n$} {
					$M[j] \leftarrow 1$\;
					$j \leftarrow j+i$\;
				}
			}
			$i \leftarrow i+1$\;
		}
		return $M$\;
	}
\caption{Sieve of Eratosthenes}
\end{algorithm}

\section{Proof of Correctness (By Induction)}
	\subsection{Inductive Hypothesis}
	Let $P(i) :=$ At the beginning of the outer for loop, all prime	numbers
	$< i$ have been found (set to 0 in M) and the multiples of all these
	primes have been marked (set to 1) in M.

	\subsection{Base Case}
	Initially, $i = 2$. As there are no prime numbers less than 2 and our
	indexing for M begins from 2, P(2) is trivially true.

	\subsection{Inductive Step}
	Assume the $P(k)$ to be true for some $k \geq 2$. When $i = k+1$, we have
	two cases. If $k$ is composite, it can expressed as the product of
	primes i.e $k = p_1^{\alpha_{1}} p_2^{\alpha_{2}} \text{ ... } p_n^{\alpha_{n}}$
	where $p_{j}$ is a prime number $< k$. According to our assumption, this would mean
	that $k$ has already been marked (set to 1 in M). The inner loop will not execute.
	\BlankLine
	If $k$ is indeed prime, then none of the previous marking procedures would
	have affected $M[k]$. As $M[k]$ was initially set 0, it will continue to remain 0.
	In addition to this, the inner loop will execute successfully and mark all multiples
	of $k$.
	\BlankLine
	In both the cases, we have now found all prime numbers less than $k+1$ and marked
	their multiples i.e $P(k+1)$ holds true.

	\subsection{Termination}
	The procedure terminates when $i > n$. As $i$ is always incremented by 1, $i$ will always
	be equal to $n+1$ when the procedure terminates. $\therefore P(n+1)$ is true i.e all prime
	numbers $ < n+1$ have been found. $\blacksquare$
\end{document}


\documentclass[]{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{graphicx}

%opening
\title{MTH 343 Numerical Analysis Lecture 6: Secant Method}
\author{Sheikh Abdul Raheem Ali}

\begin{document}
	\newcommand{\cfbox}[2]{%
		\colorlet{currentcolor}{.}%
		{\color{#1}%
			\fbox{\color{currentcolor}#2}}%
	}

\maketitle

Suppose \cfbox{red}{f(x) = 0}

\section*{Technique:}

\begin{enumerate}
	\item Begin with two values $ x_0 $ \& $ x_1 $ near the root $ r $. Draw a line joining the two points. The point $ x_2 $ where this line hits the x-axis is the first approximation of the exact root $ r $.
	\item Continue this repeatedly, always choosing the last computed values for drawing this straight line.
\end{enumerate}

\section*{Derivation:}

\begin{align*}
	\frac{y-y_1}{x-x_1} = m \text{ or } y - y_1 = m(x - x_1)s\\
	\frac{y - f(P_1)}{x-P_1} &= \frac{f(P_1) - f(P_0)}{P_1 - P_0}\\
	 y &= f(P_1) + \frac{f(P_1) - f(P_0)}{P_1 - P_0}(x - P_1)\\
	\text{To find x-intercept, set y=0},\\
	0 &= f(P_1) + \frac{f(P_1)-f(P_0)}{P_1 - P_0}(x-P_1)\\
	0 &= f(P_1)(P_1-P_0) + (f(P_1) - f(P_0))(x - P_1)\\
	(f(P_1)-f(P_0))(x-P_1) &= - f(P_1)(P_1 - P_0)\\
	x &= P_1 - \frac{f(P_1)(P_1 - P_0)}{f(P_1) - f(P_0)}\\
	P_2 &= P_1 - \frac{f(P_1)(P_1 - P_0)}{f(P_1) - f(P_0)}
\end{align*}

\section*{Example:}

\[ f(x) = x^2 - 2 \]

%A dirty hack to get the table centered
\begin{enumerate}
	\item[]
	\centering
	\begin{tabular}{c c c}
		$ n $ & $ P_{n} $ & $ f(P_n) $ \\
		2&1.6&0.56\\
		3&1.47826&0.18525\\
		4&1.418079&0.01094\\
		5&1.414299&0.000241
	\end{tabular}
\end{enumerate}

\section*{Disadvantages of the Secant Method}

\begin{enumerate}
	\item If the fraction is far from linear near the root, the iterates can fly off to points far from the root. This can be fixed by plotting $ f(x) $ and changing the starting points.

	\item If an iterate duplicates a previous one this results in an endless loop that never reaches the value of the true solution.

	\item Secant method doesn't have the bracketing property of the bisection method. Therefore, the method doesn't always converge. But if it does, it is generally faster than the bisection method.
\end{enumerate}

\begin{figure}[h!]
	\centering
	\includegraphics[width= 0.8\linewidth]{secant.png}
	\caption{Visual demonstration of the secant method}
\end{figure}

\end{document}


% To be compiled by XeLaTeX, preferably under TeX Live.
% LaTeX source for ``Yanqi Lake Lectures on Algebra'' Part III.
% Copyright 2019  李文威 (Wen-Wei Li).
% Permission is granted to copy, distribute and/or modify this
% document under the terms of the Creative Commons
% Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)
% https://creativecommons.org/licenses/by-nc/4.0/

% To be included
\chapter{Serre's criterion for normality and depth}

References: \cite[\S 11]{Eis95} and \cite[{X.1}]{Bour98}. Except in the last section, we will try to avoid the use of depth as in \cite{Eis95}.

\section{Review of discrete valuation rings}
Let $R$ be an integral domain.

\begin{definition}\index{discrete valuation ring}
	A \emph{discrete valuation} on a field $K$ is a surjective map $v: K^\times \to \Z$ such that
	\begin{itemize}
		\item $v(xy) = v(x) + v(y)$, and
		\item $v(x+y) \geq \min\{v(x), v(y) \}$
	\end{itemize}
	for all $x,y \in K^\times$, where we set $v(0) = +\infty$ for convenience. We say a domain $R$ with $K := \mathrm{Frac}(R)$ is a \emph{discrete valuation ring}, abbreviated as DVR, if there exists a discrete valuation $v$ such that
	\[ R = v^{-1}([0, +\infty]). \]
	We say $t \in R$ is a \emph{uniformizer} if $v(t)=1$.\index{uniformizer}
\end{definition}
It follows immediately that $R^\times = v^{-1}(0)$. Uniformizers are unique up to $R^\times$. Note that $v(K^\times) = \Z$ implies that $R$ cannot be a field.

\begin{example}
	The ring $\Z_p$ of $p$-adic integers ($p$: prime number) together with the usual $p$-adic valuations are standard examples of DVR. The algebra of formal power series $\Bbbk\llbracket X\rrbracket$ are also DVR: the valuation of $\sum_n a_n X^n$ is the smallest $n$ such that $a_n \neq 0$.

	More generally, in the geometric context, discrete valuations can be defined by looking at the vanishing order of rational/meromorphic functions along subvarieties of codimension one with suitable regularities.
\end{example}

\begin{lemma}\label{prop:dvr-regular}
	Let $R$ be a discrete valuation ring with valuation $v$ and uniformizer $t$. Every ideal $\mathfrak{a} \neq \{0\}$ of $R$ has the form $(t^r)$ for a unique $r \geq 0$. In particular, $R$ is a local principal ideal domain which is not a field, hence is of dimension $1$.
\end{lemma}
\begin{proof}
	Take $r := \min\{v(x) : x \in \mathfrak{a} \}$.
\end{proof}

In the exercises below, we assume $R$ is a discrete valuation ring with valuation $v$.
\begin{exercise}
	Show that $t$ is a uniformizer if and only if it generates the maximal ideal of $R$.
\end{exercise}
\begin{exercise}
	Reconstruct $v$ from the ring-theoretic structure of $R$.
\end{exercise}

Recall that a regular local ring $R$ with $\dim R = 1$ is a Noetherian local ring whose maximal ideal $\mathfrak{m}$ is principal and nonzero; elements generating $\mathfrak{m}$ are called the regular parameters for $R$.
\begin{proposition}\label{prop:reg-local-dvr}
	Suppose $t$ is a regular parameter in a regular local ring $R$ of dimension one, then $R$ is a domain, and every element $x \in R \smallsetminus \{0\}$ can be uniquely written as $x = t^r u$ with $r \geq 0$ and $u \in R^\times$. This makes $R$ into a discrete valuation ring by setting $v(x) = r$, for which $t$ is a uniformizer.

	Therefore $R$ is a discrete valuation ring. Conversely, every discrete valuation ring is regular local of dimension $1$.
\end{proposition}
\begin{proof}
	From Theorem \ref{prop:regular-local-domain} we know regular local rings are Noetherian domains. By applying Krull's Intersection Theorem (Corollary \ref{prop:Krull-intersection-domain}) to the powers of $(t)$, we see that $r := \sup\{k \geq 0: x \in (t)^k \}$ is finite. Write $x = t^r u$. Since $R^\times = R \smallsetminus \mathfrak{m}$, we see $u \in R^\times$. As to uniqueness, suppose $t^r u = t^s w$ with $r \geq s$, then $t^{r-s} = u^{-1}w \in R^\times$ implies $r=s$, hence $u=w$ as $R$ is a domain. As every element of $\text{Frac}(R)^\times$ is uniquely expressed as $t^r u$ with $r \in \Z$, one readily checks that $v(t^r u) = r$ satisfies all the requirements of discrete valuation.

	The converse direction has been addressed in Lemma \ref{prop:dvr-regular}.
\end{proof}

To recap, in dimension one we have
\[ \text{regular local ring} \iff \text{discrete valuation ring}. \]
This will be related to normality later on.

\begin{exercise}
	Explain that the regular local rings of dimension $0$ are just fields.
\end{exercise}

\section{Auxiliary results on the total fraction ring}
Let $R$ be a ring, henceforth assumed Noetherian. If there exist a non zero-divisor $t \in R$ and $\mathfrak{p} \in \text{Ass}(R/(t))$, we say $\mathfrak{p}$ is \emph{associated to a non zero-divisor}.

\begin{lemma}\label{prop:zero-test-ass}
	Let $M$ be a finitely generated $R$-module. An element $x \in M$ is zero if and only if its image in $M_{\mathfrak{p}}$ is zero for every maximal element $\mathfrak{p}$ in $\mathrm{Ass}(M)$.
\end{lemma}
\begin{proof}
	Suppose $x \neq 0$. Since $M$ is Noetherian, among ideals of the form $\text{ann}(y)$ there is a maximal one containing $\text{ann}(x)$, and we have seen in Lemma \ref{prop:Ass-maximal} that such an ideal $\mathfrak{p}$ belongs to $\text{Ass}(M)$. Since $\text{ann}(x) \subset \mathfrak{p}$, we have $x/1 \in M_{\mathfrak{p}} \smallsetminus \{0\}$.
\end{proof}

Call a ring \emph{reduced}\index{reduced ring} if it has no nilpotent element except zero.
\begin{lemma}
	Suppose $R$ is reduced, then $\mathrm{Ass}(R)$ consists of minimal primes.
\end{lemma}
\begin{proof}
	As $R$ is reduced, $\{0\} = \sqrt{0_R}$ is the intersection of minimal prime ideals $\mathfrak{p}_1, \mathfrak{p}_2, \ldots$ (all lying in $\text{Ass}(R)$, hence finite in number). By the theory of primary decompositions, one infers that $\text{Ass}(R) = \{\mathfrak{p}_1, \ldots \}$.
\end{proof}

For the next result, we denote by $T$ the set of non zero-divisors of $R$. Recall that the \emph{total fraction ring} $K(R)$ is $R[T^{-1}]$; this is the largest localization such that $R \to K(R)$ is injective, and $K(R) = \text{Frac}(R)$ when $R$ is a domain. The map $\mathfrak{p} \mapsto \mathfrak{p}K(R)$ sets up an order-preserving bijection $\text{Ass}(R) \rightiso \text{Ass}(K(R))$: indeed, if $\mathfrak{p} \ni t$ for some $t \in T$, then $\mathfrak{p}$ cannot belong to $\text{Ass}(R)$ because the union of $\text{Ass}(R)$ equals $R \smallsetminus T$.

\begin{lemma}\label{prop:K-vs-localization}
	Let $R$ be reduced. Then $K(R) \rightiso \prod_{\mathfrak{p}} K(R/\mathfrak{p})$ as $R$-algebras, where $\mathfrak{p}$ ranges over the minimal prime ideals of $R$. For any multiplicative subset $S \subset R$ there is a canonical isomorphism of $R[S^{-1}]$-algebras
	\begin{align*}
	K(R[S^{-1}]) & \rightiso K(R)[S^{-1}].
	\end{align*}
\end{lemma}
In other words, the formation of total fraction ring commutes with localizations.
\begin{proof}
	Each element in $K(R) = R[T^{-1}]$ is either a zero-divisor or invertible. The set of zero-divisors of $K(R)$ is the union of minimal prime ideals $\mathfrak{p}_i K(R)$ of $K(R)$ (where $\text{Ass}(R) = \{ \mathfrak{p}_1, \ldots, \mathfrak{p}_m\}$ by an earlier discussion), therefore each prime ideal of $K(R)$ must equal some $\mathfrak{p}_i K(R)$, by prime avoidance (Proposition \ref{prop:prime-avoidance}). Hence $\mathfrak{p}_1 K(R), \ldots, \mathfrak{p}_m K(R)$ are also the maximal ideals in $K(R)$, with zero intersection. Chinese Remainder Theorem entails that $K(R) \simeq \prod_{i=1}^m K(R)/\mathfrak{p}_i K(R)$. To conclude the first part, notice that $K(R)/\mathfrak{p}_i K(R) = (R/\mathfrak{p}_i)[T^{-1}]$; this is a field in generated by an isomorphic copy of $R/\mathfrak{p}_i$ since $\mathfrak{p}_i \cap T = \emptyset$, hence equals $\text{Frac}(R/\mathfrak{p}_i)$.

	As for the second part, one decomposes $K(R[S^{-1}])$ and $K(R)[S^{-1}]$ by the previous step, noting that
	\begin{compactitem}
		\item $R[S^{-1}]$ is reduced;
		\item $\text{Ass}(R[S^{-1}]) = \left\{ \mathfrak{p}_i R[S^{-1}]: 1 \leq i \leq m, \; \mathfrak{p}_i \cap S = \emptyset \right\}$ consists of minimal primes;
		\item $K\left( R[S^{-1}] \big/ \mathfrak{p}_i R[S^{-1}]\right) \simeq K(R/\mathfrak{p}_i) = K(R/\mathfrak{p}_i)[S^{-1}]$ when $\mathfrak{p}_i \cap S = \emptyset$, by the arguments above;
		\item $K(R/\mathfrak{p}_i)[S^{-1}] = \{0\}$ when $\mathfrak{p}_i \cap S \neq \emptyset$.
	\end{compactitem}
	A term-by-term comparison finishes the proof.
\end{proof}

We use Lemma \ref{prop:K-vs-localization} to interchange $K(\cdot)$ and localizations in what follows.
\begin{lemma}\label{prop:intersection-ass}
	Suppose $R$ is reduced. Then $x \in K(R)$ belongs to $R$ if and only if its image in $K(R)_{\mathfrak{p}} = K(R_{\mathfrak{p}})$ belongs to $R_{\mathfrak{p}}$ for every prime $\mathfrak{p}$ associated to a non zero-divisor.
\end{lemma}
\begin{proof}
	Only the ``if'' direction requires a proof. Write $x = a/t$ with $t$ not a zero-divisor. Suppose that $a \notin (t)$, i.e. $a$ does not map to zero in $R/(t)$. Lemma \ref{prop:zero-test-ass} asserts there exists $\mathfrak{p} \in \text{Ass}(R/(t))$ such that $a$ does not map to $0 \in (R/(t))_{\mathfrak{p}} = R_{\mathfrak{p}}/t R_{\mathfrak{p}}$. It follows that the image of $a/t$ in $K(R_{\mathfrak{p}})$ does not lie in $R_{\mathfrak{p}}$.
\end{proof}

\section{On normality}
Fix a Noetherian ring $R$.

\begin{exercise}
	Suppose $R$ is a domain, $K := \text{Frac}(R)$. If $R = \bigcap_{i \in I} R_i$ where $\{ R_i \subset K\}_{i \in I}$ are subrings such that $\text{Frac}(R_i) = K$ and $R_i$ is normal, for each $i$, then $R$ is normal.
\end{exercise}

\begin{proposition}\label{prop:normality-principal}
	Let $R$ be a Noetherian domain. Then $R$ is normal if and only if for every principal ideal $(t) \subset R$ and every $\mathfrak{p} \in \mathrm{Ass}(R/(t))$, the ideal $\mathfrak{p}R_{\mathfrak{p}}$ is principal.
\end{proposition}
\begin{proof}
	Assume the conditions above. To prove the normality of $R$, it suffices to use $R = \bigcap R_{\mathfrak{p}}$ where $\mathfrak{p}$ ranges over the primes associated to nonzero principal ideals (consequence of Lemma \ref{prop:intersection-ass}). Indeed, each $R_{\mathfrak{p}}$ is regular, hence normal by Proposition \ref{prop:reg-local-dvr}, therefore so is their intersection by the previous exercise.

	Conversely, assume $R$ is normal and let $\mathfrak{p} \in \text{Ass}(R/(t))$ with $t \neq 0$, we have to show $\mathfrak{p}R_{\mathfrak{p}}$ is principal. Upon replacing $R$ by $R_{\mathfrak{p}}$ and recalling how associated primes behave under localization, we may even assume $R$ is local with maximal ideal $\mathfrak{p}$. Express $\mathfrak{p}$ as the annihilator of some $\bar{x} \in R/(t)$ with $x \in R$. Define the fractional ideal
	\[ \mathfrak{p}^{-1} := \{ y \in \text{Frac}(R) : y\mathfrak{p} \subset R \}. \]
	It is an $R$-submodule of $\text{Frac}(R)$ containing $R$. Define the $R$-submodule $\mathfrak{p}^{-1}\mathfrak{p}$ of $\text{Frac}(R)$ in the obvious way. Clearly $\mathfrak{p} \subset \mathfrak{p}^{-1}\mathfrak{p} \subset R$. By maximality of $\mathfrak{p}$, exactly one of the $\subset$ is equality. If $\mathfrak{p}\mathfrak{p}^{-1}=\mathfrak{p}$, every element of $\mathfrak{p}^{-1}$ is integral over $R$, hence $\mathfrak{p}^{-1} \subset R$ by normality (integrality is ``witnessed'' by the module $\mathfrak{p}$). From $\mathfrak{p}x \subset (t)$ we see $x/t \in \mathfrak{p}^{-1} = R$; this would imply $\bar{x} = 0$ and $\mathfrak{p}=R$, which is absurd.

	Therefore we must have $\mathfrak{p}\mathfrak{p}^{-1} = R$. This implies that $\mathfrak{p}y \not\subset \mathfrak{p}$ for some $y \in \mathfrak{p}^{-1}$, hence $\mathfrak{p}y = R$ since $R$ is local. Hence $\mathfrak{p} = y^{-1}R \simeq R$ is principal.
\end{proof}

\begin{corollary}\label{prop:normality-dvr}
	The following are equivalent for a local domain $R$:
	\begin{enumerate}[(i)]
		\item $R$ is normal of dimension $1$;
		\item $R$ is a regular local ring of dimension $1$;
		\item $R$ is a discrete valuation ring.
	\end{enumerate}
\end{corollary}
\begin{proof}
	(iii) $\implies$ (i). We have seen that discrete valuation rings are principal ideal rings of dimension $1$, therefore also normal by unique factorization property.

	(i) $\implies$ (ii). Under the normality assumption, choose any $t \in R \smallsetminus \{0\}$. Since $\dim R = 1$ and $\{0\}$ is a prime ideal, the associated prime of $(t)$ can only be the maximal ideal $\mathfrak{m}$, which is principal by Proposition \ref{prop:normality-principal}. This shows that $R$ is regular local.

	(ii) $\implies$ (iii) is included in Proposition \ref{prop:reg-local-dvr}.
\end{proof}

\begin{corollary}
	Let $R$ be a Noetherian normal domain. Then
	\[ R = \bigcap_{\mathrm{ht}(\mathfrak{p}) = 1} R_{\mathfrak{p}} \]
	inside $\mathrm{Frac}(R)$.
\end{corollary}
\begin{proof}
	Evidently $\subset$ holds. By Lemma \ref{prop:intersection-ass} together with Proposition \ref{prop:normality-principal}, $R$ can be written as an intersection of $R_{\mathfrak{p}}$ where $\mathfrak{p}$ is associated to some non zero-divisor, such that $\mathfrak{p}R_{\mathfrak{p}}$ is principal; it suffices to show $\text{ht}(\mathfrak{p})=1$. From $\mathfrak{p} \neq \{0\}$ we see $\text{ht}(\mathfrak{p}) \geq 1$; on the other hand, by Hauptidealsatz or by the discussion on regular local rings, we see $\text{ht}(\mathfrak{p}) = \text{ht}(\mathfrak{p}R_{\mathfrak{p}}) \leq 1$.
\end{proof}

\section{Serre's criterion}

\begin{lemma}\label{prop:reduced-test}
	Let $R$ be a Noetherian ring. Suppose that
	\begin{compactitem}
		\item the primes in $\mathrm{Ass}(R)$ are all minimal, and
		\item $R_{\mathfrak{p}}$ is a field for every minimal prime ideal $\mathfrak{p}$,
	\end{compactitem}
	then $R$ is reduced.
\end{lemma}
\begin{proof}
	Take a minimal primary decomposition $\{0\} = I_1 \cap \cdots \cap I_m$ with $\text{Ass}(R/I_j) = \{\mathfrak{p}_j = \sqrt{I_j} \}$ and $\text{Ass}(R) = \{\mathfrak{p}_1, \ldots, \mathfrak{p}_m \}$. By the properties of primary ideals, $\mathfrak{p}_j = \sqrt{I_j} \supset I_j$ for all $j$. By assumption each $\mathfrak{p}_j$ is minimal, and $R_{\mathfrak{p}_j}$ is a field. From the uniqueness of non-embedded components in primary decompositions, $I_j = \Ker\left[R \to R_{\mathfrak{p}_j}\right]$ is a prime contained in $\mathfrak{p}_j$, hence $I_j = \mathfrak{p}_j$. We deduce that $\{0\} = \bigcap_{j=1}^m \mathfrak{p}_j$, thereby showing $\sqrt{0_R} = \{0\}$.
\end{proof}

\begin{theorem}[J.-P. Serre]\label{prop:Serre-criterion}\index{Serre's criterion}
	A Noetherian ring $R$ is a finite direct product of normal domains if and only if the following two conditions hold.
	\begin{description}
		\item[R1] The localization of $R$ at every prime ideal of height $1$ (resp. $0$) is a discrete valuation ring (resp. a field).
		\item[S2] For every non zero-divisor $t$ of $R$, the primes in $\mathrm{Ass}(R/(t))$ are all of height $1$; the primes in $\mathrm{Ass}(R)$ are all of height $0$.
	\end{description}
\end{theorem}
The condition \textbf{R1} means regularity in codimension $\leq 1$. The condition \textbf{S2} is often rephrased in terms of \emph{depth}, which will be discussed in Proposition \ref{prop:S2}.

\begin{proof}
	We begin with the $\implies$ direction. Suppose $R = R_1 \times \cdots \times R_n$ where each $R_i$ is a normal domain. As is well-known, $\Spec(R) = \bigsqcup_{i=1}^n \Spec(R_i)$ as topological spaces: to be precise, the elements of $\Spec(R)$ take the form $\mathfrak{p} = R_1 \times \cdots \times \mathfrak{p}_i \times \cdots \times R_n$, where $\mathfrak{p}_i \in \Spec(R_i)$. We have
	\[ \text{ht}(\mathfrak{p}) = \text{ht}(\mathfrak{p}_i), \quad R_{\mathfrak{p}} \simeq (R_i)_{\mathfrak{p}_i}. \]
	Furthermore, one easily checks that
	\[ \text{Ass}(R/(t)) = \bigsqcup_{i=1}^n \text{Ass}(R_i/(t_i)), \quad t = (t_1, \ldots, t_n) \in R \]
	compatibly with the description above.

	This reduces the verification of \textbf{S2} to the case of normal domains, which is addressed in Proposition \ref{prop:normality-principal}. The condition \textbf{R1} is implied by Corollary \ref{prop:normality-dvr} since normality is preserved under localizations.

	Assume conversely \textbf{R1} and \textbf{S2}. They imply the conditions of Lemma \ref{prop:reduced-test}, hence $R$ is reduced. Now for every prime $\mathfrak{p}$ associated to a non zero-divisor, we have $\text{ht}(\mathfrak{p})=1$ and $R_{\mathfrak{p}}$ is a normal domain by \textbf{R1} $\wedge$ \textbf{S2}. By Lemma \ref{prop:intersection-ass} (as $R$ is reduced), $R$ is integrally closed in $K(R)$: indeed, if $x \in K(R)$ is integral over $R$, so is its image in $K(R_{\mathfrak{p}}) = K(R)_{\mathfrak{p}}$ for every $\mathfrak{p}$ as above, therefore lies in $R_{\mathfrak{p}}$. Decompose $K(R) = \prod_{i=1}^m K(R/\mathfrak{p}_i)$ as in Lemma \ref{prop:K-vs-localization}. The idempotents $e_i \in K(R)$ associated to this decomposition are trivially integral over $R$: $e_i^2 - e_i = 0$, hence $e_i \in R$ for all $i$. It follows that $R = Re_1 + \cdots + Re_m \prod_{i=1}^m Re_i \subset K(R)$ and one easily checks that $Re_i = R/\mathfrak{p}_i$.

	Finally, since $R$ is integrally closed in $K(R)$, the decomposition above implies $R/\mathfrak{p}_i$ is integrally closed in $K(R/\mathfrak{p}_i)$. All in all, we have written $R$ as a direct product of normal domains.
\end{proof}

\begin{exercise}
	Recall that for an $R$-module $M$, a prime ideal $\mathfrak{p} \in \text{Ass}(M)$ is called \emph{embedded} if $\mathfrak{p}$ is not a minimal element in $\text{Ass}(M)$. Show that for $M=R$, embedded primes are primes in $\text{Ass}(R)$ with height $> 0$. For $M=R/(t)$ where $t$ is not a zero-divisor, embedded primes are primes in $\text{Ass}(R/(t))$ with height $> 1$. Use this to rephrase \textbf{S2} as follows: there are no embedded primes in $\text{Ass}(R/(t))$ ($t$ not a zero-divisor) or in $\text{Ass}(R)$.
\end{exercise}

\begin{exercise}
	Suppose a ring $R$ is isomorphic to a direct product $\prod_{i \in I} R_i$. Show that $R$ is a domain if and only if $|I|=1$ (say $I=\{i_0\}$), and $R_{i_0}$ is a domain.
\end{exercise}

\begin{corollary}
	A Noetherian domain $R$ is normal if and only if \textbf{R1} and \textbf{S2} hold for $R$.
\end{corollary}
\begin{proof}
	Immediate from the previous exercise and Theorem \ref{prop:Serre-criterion}.
\end{proof}

\section{Introduction to depth}
Based on \cite{Bour98}, we give a brief account on the notion of depth. Let $R$ be a ring and $M$ be an $R$-module, $M \neq \{0\}$. Recall the $\Ext$-functors
\[ \Ext^n_R(X,Y) := H^n(R\mathcal{H}\text{om}(X,Y)) = \text{Hom}_{D^+(R\dcate{Mod})}(X, Y[n]). \]

\begin{definition}[Depth of a module]\index{depth}
	Let $I$ be a proper ideal of $R$. We define the \emph{depth} of $M$ relative to $I$ as
	\[ \text{depth}_I(M) := \inf \left\{n \geq 0: \Ext^n_R(R/I, M) \neq 0 \right\} \]
	with values in $\Z_{\geq 0} \sqcup \{+\infty\}$.
\end{definition}

\begin{proposition}\label{prop:depth-zero}
	For $I$, $M$ as above, the following are equivalent:
	\begin{enumerate}[(i)]
		\item $\mathrm{depth}_I(M)=0$;
		\item for all $x \in I$, the homomorphism $M \xrightarrow{x} M$ is not injective;
		\item $\mathrm{Ass}(M) \cap V(I) \neq \emptyset$.
	\end{enumerate}
\end{proposition}
\begin{proof}
	In each case we have $M \neq \{0\}$. If (i) holds, then $M \xrightarrow{x} M$ vanishes on the image of some nonzero $R/I \to M$, hence (ii). If (ii) holds, the union of $\text{Ass}(M)$ will cover $I$, and (iii) follows by prime avoidance. Finally, suppose $\mathfrak{p} \in \text{Ass}(M) \cap V(I)$, there is an embedding $R/\mathfrak{p} \hookrightarrow M$, which yields a non-zero $R/I \to M$.
\end{proof}

\begin{definition}\index{regular sequence}
	A sequence $x_1, \ldots, x_n \in R$ is called an $M$-\emph{regular sequence} of length $n$ if $(x_1, \ldots, x_n)M \subsetneq M$ and
	\[ 0 \to M/(x_1, \ldots, x_{k-1}) \xrightarrow{x_k} M/(x_1, \ldots, x_{k-1}) \]
	is exact for all $1 \leq k \leq n$.
\end{definition}

\begin{lemma}
	Let $M$ be an $R$-module, $x_1, \ldots, x_r$ be an $M$-regular sequence lying in an ideal $I \subsetneq R$. We have $\mathrm{depth}_I(M) = r + \mathrm{depth}_I(M/(x_1, \ldots, x_r)M)$.
\end{lemma}
\begin{proof}
	The case $r=1$ follows by staring at the long exact sequence attached to $0 \to M \xrightarrow{x_1} M \to M/x_1 M \to 0$. The general case follows by induction on $r$.
\end{proof}

\begin{theorem}\label{prop:regular-vs-depth}
	Assume $R$ Noetherian, $M$ finitely generated and $I \subsetneq R$.
	\begin{enumerate}[(i)]
		\item $\mathrm{depth}_I(M)$ is the supremum of the lengths of $M$-regular sequences with elements in $I$.
		\item Suppose $\mathrm{depth}_I(M) < +\infty$. Every $M$-regular sequence with elements in $I$ can be extended to one of length $\mathrm{depth}_I(M)$.
		\item The depth of $M$ relative to $I$ is finite if and only if $V(I) \cap \Supp(M) \neq \emptyset$, or equivalently $IM \neq M$.
	\end{enumerate}
\end{theorem}
\begin{proof}
	To prove (i) and (ii), by the previous Lemma we are reduced to show that $\text{depth}_I(M) > 0$ implies the existence of $x \in I$ which is not a zero-divisor of $M$; this follows from Proposition \ref{prop:depth-zero}.

	Now pass to the word ``equivalently'' in (iii). We have $M \neq IM$ if and only if $(M/IM)_{\mathfrak{p}} = M_{\mathfrak{p}}/ I_{\mathfrak{p}} M_{\mathfrak{p}} \neq 0$ for some prime ideal $\mathfrak{p}$. That quotient always vanishes when $\mathfrak{p} \not\supset I$, in which case $I_{\mathfrak{p}} = R_{\mathfrak{p}}$. On the other hand, when $\mathfrak{p} \in V(I)$ we have $I_{\mathfrak{p}} \subset \text{rad}(R_{\mathfrak{p}})$, thus the non-vanishing is equivalent to $M_{\mathfrak{p}} \neq \{0\}$ by Nakayama's Lemma \ref{prop:NAK}.

	The ``if'' direction of (iii) is based on the following fact
	\[ IM \neq M \implies \text{depth}_I(M) < +\infty \]
	which will be proved in the next lecture (Theorem \ref{prop:Koszul-depth}) using \emph{Koszul complexes}. As for the ``only if'' direction, $V(I) \cap \Supp(M) = \emptyset$ implies $I + \text{ann}(M) = R$, but the elements in $I + \text{ann}(M)$ annihilate each $\Ext^n(R/I, M)$, hence $\text{depth}_I(M)=+\infty$.
\end{proof}

\begin{figure}[h]
	\centering \vspace{1em} \includegraphics[height=220pt]{Jean-Louis_Koszul.jpg} \\ \vspace{1em}
	\begin{minipage}{0.7\textwidth}
		\small Jean-Louis Koszul (1921---2018) created the Koszul complexes in order to define a cohomology theory for Lie algebras; this device turns out to be a general, convenient construction in homological algebra, which will be discussed in the next lecture. The study of ``Koszulness'' in a broader (eg. operadic) context is now an active area of research. J.-L. Koszul is also a second-generation member of the Bourbaki group. Source: by Konrad Jacobs - \href{http://owpdb.mfo.de/detail?photo_id=2273}{Oberwolfach Photo Collection}, ID 2273.
	\end{minipage}
\end{figure}

\begin{corollary}
	With the same assumptions, let $(x_1, \ldots, x_r)$ be an $M$-regular sequence with $x_i \in I$. It is of length $\mathrm{depth}_I(M)$ if and only if $\mathrm{Ass}(M/(x_1, \ldots, x_r)M) \cap V(I) \neq \emptyset$.
\end{corollary}
\begin{proof}
	The sequence has length $\text{depth}_I(M)$ if and only if $R$-module $M/(x_1, \ldots, x_r)M$ has depth zero, so it remains to apply Proposition \ref{prop:depth-zero}.
\end{proof}

\begin{corollary}
	Let $R$ be a Noetherian local ring with maximal ideal $\mathfrak{m}$, and $M \neq \{0\}$ a finitely generated $R$-module, then $\mathrm{depth}_{\mathfrak{m}}(M) \leq \dim M$.
\end{corollary}
\begin{proof}
	Consider the following situation: $x \in \mathfrak{m}$ is not a zero-divisor for $M \neq \{0\}$. In the discussion of dimensions, we have seen that $d(M) \geq d(M/xM) \geq d(M/xM)-1$, where $d(\cdot)$ is the degree of Hilbert--Samuel polynomial; on the other hand, since the alternating sum of Hilbert--Samuel polynomials in $0 \to M \xrightarrow{x} M \to M/xM \to 0$ has degree $< d(M)$, we infer that $d(M/xM) = d(M) - 1$. Hence $\dim(M/xM) = \dim(M) - 1$. By relating depth to $M$-regular sequences, we deduce $\text{depth}_{\mathfrak{m}}(M) \leq \dim M$.
\end{proof}

\begin{definition}[Cohen--Macaulay modules]\index{Cohen--Macaulay module}
	Let $R$ be a Noetherian ring. A finitely generated $R$-module $M$ is called \emph{Cohen--Macaulay} if
	\[ \mathrm{depth}_{\mathfrak{m}A_{\mathfrak{m}} }(M_{\mathfrak{m}}) = \dim M_{\mathfrak{m}} \]
	for every $\mathfrak{m} \in \MaxSpec(R) \cap \Supp(M)$. We say $R$ is a Cohen--Macaulay ring if it is Cohen--Macaulay as a module.
\end{definition}

\begin{example}
	Regular local rings are Cohen--Macaulay, although we do not prove this here. Another important class of Cohen--Macaulay rings is the algebra of invariants $A^G$ where $A$ is the algebra of regular functions on an affine $\Bbbk$-variety $\mathcal{X}$ with \emph{rational singularities} (eg. $A = \Bbbk[X_1, \ldots, X_n]$) with an action by a reductive $\Bbbk$-group $G$ (finite groups allowed), and we assume $\text{char}(\Bbbk)=0$. Here is the reason: Boutot \cite{Bou87} proved the GIT quotient $\mathcal{X}/\!/G$ has rational singularities as well, hence is Cohen--Macaulay; in characteristic zero this strengthens an earlier theorem of Hochster--Roberts. These algebras are interesting objects from the geometric, algebraic or even combinatorial perspectives.
\end{example}

\begin{exercise}
	Show by using Proposition \ref{prop:depth-zero} that $\text{depth}_{\mathfrak{m}}(R) = 0$ if $R$ is local with maximal ideal $\mathfrak{m}$ and has dimension zero.
\end{exercise}

\begin{proposition}\label{prop:S2}
	The condition \textbf{S2} in Theorem \ref{prop:Serre-criterion} is equivalent to
	\[ \mathrm{ht}(\mathfrak{p}) \geq i \implies \mathrm{depth}_{\mathfrak{p}R_{\mathfrak{p}}}(R_{\mathfrak{p}}) \geq i \]
	for all $\mathfrak{p} \in \Spec(R)$ and $i \in \{1,2\}$.
\end{proposition}
\begin{proof}
	Assume the displayed conditions. We first show that every $\mathfrak{p} \in \text{Ass}(R)$ has height zero. Upon localization we may assume $R$ local with maximal ideal $\mathfrak{p}$, thus $\mathfrak{p}$ has depth zero by Proposition \ref{prop:depth-zero}; our conditions force $\text{ht}(\mathfrak{p})=0$. Next, consider $\mathfrak{p} \in \text{Ass}(R/(t))$ with $t$ non zero-divisor. Note that $t$ remains a non zero-divisor in $R_{\mathfrak{p}}$, and $\mathfrak{p}R_{\mathfrak{p}} \in \text{Ass}(R_{\mathfrak{p}}/(t))$. Hence $\text{depth}(R_{\mathfrak{p}}) = \text{depth}(R_{\mathfrak{p}}/(t)) + 1 = 1$ since $\text{depth}_{\mathfrak{p}}(R_{\mathfrak{p}}/(t)) = 0$ by Proposition \ref{prop:depth-zero}. Our conditions force $\text{ht}(\mathfrak{p}) \leq 1$ and $t \in \mathfrak{p}$ implies $\text{ht}(\mathfrak{p}) > 0$.

	Conversely, assume \textbf{S2}. Suppose $\text{ht}(\mathfrak{p}) \geq 1$. If $R_{\mathfrak{p}}$ has depth zero then $\text{Ass}(R_{\mathfrak{p}}) \cap V(\mathfrak{p}R_{\mathfrak{p}}) \neq \emptyset$, which implies $\mathfrak{p}R_{\mathfrak{p}} \in \text{Ass}(R_{\mathfrak{p}})$ thus $\mathfrak{p} \in \text{Ass}(R)$, contradicting \textbf{S2}. Next, suppose $\text{ht}(\mathfrak{p}) \geq 2$. If $R_{\mathfrak{p}}$ has depth $\leq 1$, the standard property
	\[ \forall i \geq 0, \quad \Ext^i_{R_{\mathfrak{p}}}(X_{\mathfrak{p}}, Y_{\mathfrak{p}}) \simeq \Ext^i_R(X,Y)_{\mathfrak{p}} \]
	valid for Noetherian $R$ and finitely generated $R$-modules $X, Y$, yields
	\[ 0 \leq \text{depth}_{\mathfrak{p}}(R) \leq \text{depth}_{\mathfrak{p}R_{\mathfrak{p}}}(R_{\mathfrak{p}}) \leq 1. \]
	If $\text{depth}_{\mathfrak{p}}(R)=0$, there exists of $\mathfrak{p}' \supset \mathfrak{p}$ and $\mathfrak{p}' \in \text{Ass}(R)$ by Proposition \ref{prop:depth-zero}, but \textbf{S2} implies $\text{ht}(\mathfrak{p}) \leq \text{ht}(\mathfrak{p}') = 0$ which is absurd. If $\text{depth}_{\mathfrak{p}}(R)=1$, there exists a non zero-divisor $x$ in $R$ such that $\text{depth}_{\mathfrak{p}}(R/(x)) = 0$. The same argument furnishes $\mathfrak{p}' \supset \mathfrak{p}$ such that $\mathfrak{p}' \in \text{Ass}(R/(x))$. Now \textbf{S2} implies $\text{ht}(\mathfrak{p}) \leq \text{ht}(\mathfrak{p}')=1$, again a contradiction.
\end{proof}

Now the conditions \textbf{R1} and \textbf{S2} can be generalized to arbitrary $k \in \Z_{\geq 0}$:
\begin{description}
	\item[Rk] $\text{ht}(\mathfrak{p}) \leq k$ implies $R_{\mathfrak{p}}$ is a regular local ring, for every $\mathfrak{p} \in \Spec(R)$;
	\item[Sk] $\text{depth}(R_{\mathfrak{p}}) \geq \min\{ k, \text{ht}(\mathfrak{p})\}$ for every $\mathfrak{p} \in \Spec(R)$.
\end{description}
One readily checks their compatibility with Proposition \ref{prop:S2}. Note that \textbf{S0} is trivial, whilst $R$ is Cohen--Macaulay if and only if it satisfies \textbf{Sk} for all $k$.

\begin{example}
	From Theorem \ref{prop:Serre-criterion}, we see that any finite direct product of normal domains of dimension $\leq 2$ is Cohen--Macaulay.
\end{example}

\begin{exercise}
	Show that \textbf{S1} holds if and only if there are no embedded primes in $\text{Ass}(R)$.
\end{exercise}