%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Machine learning approach}
\label{sec31}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Machine learning (ML) is a sub-field of AI which belongs to computer science field.
ML is defined as \say{the ability of a computer to learn without being explicitly programmed}~\cite{munoz2014machine}.
The conventional way of software engineering is through creating rules by human and combine them with data to create a solution to a problem.
Alternatively, when it comes to ML, it utilises data and answers to learn the rules behind the problem~\cite{franoischollet2017learning}.
In Fig.~\ref{fig:Machine_learning} the conventional software programming and ML are presented in (a) and (b) respectively.
In ML, machines have to run into a learning process to learn inference rules which are responsible for controlling the relations within a phenomenon. Hence, it is called a ML.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure} [!ht]
	\begin{center}
		\centering
		\includegraphics[scale=1]{Figures/Chapter_1/machine_learning_vs_conventional_programming.png}
	\end{center}
	\caption{(a) Conventional Programming	(b) Machine learning}
	\label{fig:Machine_learning}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{}
ML techniques in SHM were heavily utilised by researchers for damage detection~\cite{raghavan2008effects, Su2009, Mitra2016}.
Moreover, ML techniques attempt to map the patterns of the input data acquired by sensors to output targets for a damage estimation at different levels.
Accordingly, ML techniques demands high domain knowledge of the examiner to perform hand-crafted damage-sensitive feature extraction on the raw data acquired by sensors before being fed into a suitable ML model.
Generally, the process of damage-sensitive features extraction (hand-crafted) in the field of SHM emerged due to the enormous development in the physics-based SHM techniques such as modal strain energy (MSE)~\cite{Kim}, modal curvature (MC)~\cite{Wahab}, modal assurance criterion (MAC), and Coordinate (MAC)~\cite{Allemang2003}, modal flexibility (MF)~\cite{Jaishi}, damage locating vector (DLV)~\cite{Bernal2002}, wavelet transform~\cite{Staszewski,Kima} and probabilistic reconstruction algorithm (PRA)~\cite{Hay2006} among others.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

There are different methods which can be implemented when performing ML.
Generally, those methods are grouped into four approaches: Supervised learning, Unsupervised learning, Reinforcement learning, and Transfer learning approach.
Fig.~\ref{fig:Machine_learning_approaches} shows the different types of ML approaches.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure} [!ht]
	\begin{center}
		\centering
		\includegraphics[scale=1]{Figures/Chapter_1/ML_approaches.png}
	\end{center}
	\caption{Machine Learning Approaches}
	\label{fig:Machine_learning_approaches}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{}
Supervised learning is the task of learning a machine the inference rules from the training data, and how to map inputs with outputs.
The training data is a collection of variables together with its labels e.g. a set of civil images of structures that are labelled as cracked or undamaged.
During the learning process, the machine gets a collection of inputs simultaneously with the corresponding label (ground truth).
Accordingly, by comparing its predicted output with the correct output to find errors, it modifies the model and the learning occurs~\cite{Ongsulee2018}.
Supervised learning uses patterns to predict the values of the output label  for new unlabeled data by applying methods like regression and classification~\cite{Ongsulee2018}.
%Fig~\ref{fig:Machine_learning_approaches} presents most utilised algorithms for ML different approaches like:
%K Nearest Neighbors algorithm (KNN) where K represents the number of the nearest neighbours used for classification of the observations in a test sample, based on their characteristics e.g. the mean distance.
%Moreover,Decision Trees where the data keeps splitting according to a specific parameter.
%Furthermore, Naive Bayes algorithm which is based on probabilistic approach, through implementing Bayes' theorem.
%In addition, Support vector Machine SVM and Logistic regression.
%For Regression purposes, algorithms like linear and polynomial regression are implemented.

Unsupervised Learning is applied to such data with no historical labels~\cite{Ongsulee2018}.
In this case, the model does not know the ground truth labels of the input values. Therefore, the algorithm needs to figure out some common characteristics among the input values.
Consequently, unsupervised learning is more difficult than supervised learning, due to removing the supervision which implies the problem becomes less defined.
%The most well-known techniques used in Unsupervised learning is clustering~\cite{Russell2010}.
%In which it creates subgroups within the input data based on their characteristics, Fig~\ref{fig:Machine_learning_approaches} presents most Clustering algorithms like:k-means algorithm, which intends to split n observations into k clusters, where each observation relates to the cluster that has the nearest mean distance.

Reinforcement learning is based on the trial and error principle, which means the algorithm learns through actions that explore the environment in a way that results with the greatest rewards~\cite{Russell2010}.
In this approach of learning the process consists of three parts: the agent which is responsible for making decisions, the environment that relates to any interaction with the agent, and the actions that are made by the agent. Fig.~\ref{fig:ReinforcementLearning} illustrates the procedure of the reinforcement learning approach.
%Fig~\ref{fig:Machine_learning_approaches} presents some examples of reinforcement learning.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure} [!ht]
	\begin{center}
		\centering
		\includegraphics[scale=1]{Figures/Chapter_1/Reinforcement_learning.png}
	\end{center}
	\caption{Reinforcement Learning}
	\label{fig:ReinforcementLearning}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Transfer learning is different compared to the traditional ML approaches that are designed to particular tasks, which means their learning and knowledge can not be transferred from one model to another.
Therefore, when starting a new ML task we have to start from scratch.
On the contrary, in transfer learning, the model knowledge (e.g features and weights) can be transferred from a previously learned task to a new learning task.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection[Data prepossessing and FE]{Data prepossessing and feature extraction techniques}

In SHM applications, the damage identification process is based on comparing the collected data from the structure without damage (base-line) with its current status to determine if there are any occurrence of changes such as damage.
Accordingly, signal processing techniques must be applied to the collected data to identify components of interest in a registered signal from a structure.
In general, the process of extracting features of the defects occurred in structures can be achieved in different domains: time domain, frequency domain, time-frequency domain, electromechanical impedance domain, and modal analysis domain~\cite{Khan2019}.

In this section, various methods for signal processing, data prepossessing and feature extraction are presented.

\subsubsection{Fourier Transform}
Fourier Transform (TF) is considered as a conventional method for signal analysis, it is used to decompose the registered signal in the time domain for its frequency components.
FT works by first transforming a function inn the time domain into a function in the frequency domain.
Then, the signal can be analysed for its frequency components because the FT coefficients of the transformed function demonstrate the contribution of the sines and cosines functions at each frequency.
FT presents global information about the frequency content, therefore, it is suited for signals with stationary frequency content meaning their frequency content does not change with time~\cite{Raghavan2006}.
Alternatively, there are other time-frequency representations (TER) that are able to identify the local frequency content and are better suited for non-stationary-frequency signals~\cite{Raghavan2006}.
Short-time Fourier transform (STFT) is considered as the simplest example of a TER, in which STFT divides the signal into a number of short overlapping segments in the time-domain, each segment is multiplied in time using a fixed modulation window and the FT is used on resulting signal~\cite{Raghavan2006}

\subsubsection{Wavelet Transform}
Wavelet Transform (WT) is a mathematical function for data preprocessing that enhancing the process of feature extraction in a wide range of applications such as civil engineering, power engineering, traffic engineering, mechanical systems and aerospace engineering. .
Furthermore, WT is considered as one of the most widely used tools for signal preprocessing in SHM in recent years~\cite{Taha2006}.
The principal idea of WT is splitting data signal into different scale components, accordingly, analysing each component with a resolution matched to its scale~\cite{Graps1995},hence wavelets are referring to little or small waves.
The WT is based on dilated scales and shifted windows that can perform a time-frequency resolution of a data signal.
WT is represented in the following Eqn~\ref{wavelet}, that yields a 2D coefficients matrix  $WT\{x\}(a,b)$.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{equation}
	WT\{x\}(a,b) = \int_{R}^{}\Psi_{a,b}(t)x(t)dt
	\label{wavelet}
\end{equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
$\Psi_{a,b}$ is defined as the mother wavelet which scaled and dilated wavelets  where a and b are the scale and dilation parameters.
Scaling in WT indicates stretching or compressing it in the time domain.
Therefore, compressed wavelets are represented by smaller scales while stretched wavelets can be produced by larger scales~\cite{Graps1995}.
%\subsubsection{Principle component analysis} Principle component analysis (PCA) is a technique of multi-variable and mega-variate analysis used for reducing complex data dimensionality in ML.
%Furthermore, PCA can be identified as an unsupervised, simple and non-parametric method for information extraction and data compression~\cite{Jolliffe2002}.
%
%Consequently, PCA is considered as a patterns recognition technique, and when it is applied on collected data, new important hidden data with some simplified patterns  are identified.
%Accordingly, PCA is responsible for determining the dynamics in the system according to their importance, as a result, there are more important dynamics and redundant dynamics and which are just noise~\cite{Farrar2007}.
%To develop a PCA model it is essential to organise the data in an (\(m \times n\)) matrix \(X = [x_{i1}x_{i2}...x_{ij}]\) where $i = 1,2,3...m ; j = 1,2,3,...n$ which carries information from \(n\) sensors (variables) and \(m\) experimental trials (observations).
%Considering different magnitudes and scales regarding the physical variables and sensors in the structure, each point in the collected data is computed using the mean of all the sensor measurements at the same time and the standard deviation of all sensor measurements.
%Following normalization the variables the covariance matrix $C_x$ is calculated as show in  Eqn~\ref{covar matrix}~\cite{Tibaduiza}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{equation}
%	C_x =  \frac{1}{m-1}X^TX
%	\label{covar matrix}
%\end{equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%where $C_x$ is a square symmetric $(m \times m)$ matrix that determines the linear relationship degree in the data set within all possible pairs of variables which are the sensors in this case, and $T$ is the transposition.
%Considering the covariance matrix $C_x$ and the eigenvalues $(\lambda) $ of $C_x$, therefore, the eigenvector $(E)$ can be determined according to Eqn~\ref{eigenvector}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{equation}
%	C_xE=\lambda E
%	\label{eigenvector}
%\end{equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Columns of the eigenvectors matrix $E$ are arranged based on the eigenvalues by descending order and they are termed the Principal Components (PCs) of the data set.
%Accordingly, the most important features in the data with the highest weight of information are represented by the eigenvectors with the highest eigenvalue.
%Therefore, by picking only a reduced number of $r$ of PCs, that is corresponding to the first eigenvalues, the reduced transformation matrix could be considered as a model for the structure with compressed data.
%
%The transformed data matrix T (score matrix) can be represented geometrically as the projection of the original data over the direction of the PCs of the eigenvector matrix E as presented in Eqn~\ref{score matrix}.
%The Principal Component Coefficient (PCC) quantify the influence of each variable $(x_{1,i},x_{2,i},x_{3,i},...,x_{i,j})$ have on each principle component $(z_{i,1},z_{i,2},z_{i,3},...,z_{i,j})$.
%For the PCC matrix $W$, the rows represent the variables, columns represent the component the PCC for each variable mentioned before, the component principal can be calculated as shown in Eqn~\ref{PCC},
%where $e_{i,j}$ denotes an element of eigenvector matrix E and Var($x_{i,j}$) denotes the variance of $x_{i,j}$~\cite{DeOliveira2014}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{equation}
%	T = XE
%	\label{score matrix}
%\end{equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{equation}
%	W = \frac{e_{i,j}}{\sqrt{ Var(x_{i,j})}}
%	\label{PCC}
%\end{equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{table}
%	\renewcommand{\arraystretch}{1.1}
%	\centering
%	\caption{Advantages/Disadvantages of PCA}
%	\scriptsize
%	\begin{tabular}{ll}
%		\toprule
%		\textbf{Advantages} & \textbf{Disadvantages} \\
%		\midrule
%		Removes Correlated Features & Independent variables become less 	interpretable  \\
%		%\hline
%		Improves Algorithm Performance & Data must be standardized before PCA \\
%		%	\hline
%		Reduces Overfitting & Information Loss \\
%		%	\hline
%		Improves Visualisation &  \\
%		\bottomrule
%	\end{tabular}
%	\label{tab:pca pros and cons}
%\end{table}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Consequently, determining the optimal number of PCs is performed by looking at the cumulative variance ratio as a function of the number of components. The choice of selecting the number of PCs completely relies on the trade-off between information loss and dimensionality reduction.
%PCA technique has several advantages and disadvantages, as presented in Table~\ref{tab:pca pros and cons}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Principal component analysis}
PCA is a popular method used for damage identification in SHM.
Further, PCA shows a solid and efficient performance in feature extraction, and structural damage detection~\cite{liu2014research, wang2014principal, nguyen2010fault}.
Besides, PCA proves to be an effective tool to improve the training efficiency and enhance the classification accuracy for other ML algorithms, such as unsupervised learning methods~\cite{liu2019rapid, datteo2017statistical, torres2014data}.

PCA is dimensionality reduction technique utilised to reduce the dimensionality of large data (input space) into a lower dimension (feature space) through transforming a large set of variables into a smaller one with minimal loss information~\cite{Jolliffe2002}.
Moreover, PCA can be utilised for damage detection by eliminating noise and obtaining sensitive features of damage as eigenvectors.
The PCA technique is illustrated below.
In the beginning, a matrix \(U(t)\) is constructed as shown in Eqn. \ref{U(t)}, which contains all registered data with time histories.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{equation}
	U(t)=
	\begin{bmatrix}
		u_1{(t1)}       & u_2{(t1)} & \dots & u_M{(t1)} \\
		u_1{(t2)}       & u_2{(t2)} & \dots & u_M{(t2)} \\
		\vdots 			& \vdots 	& \ddots & \vdots \\
		u_1{(t_N)}      & u_2{(t_N)} & \dots & u_M{(t_N)}
	\end{bmatrix}\ ,
	\label{U(t)}
\end{equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
where \(t\) corresponds to the time, \(u_i\ (i = 1, 2, ..., M)\) represents to the response from the \(i-th\) sensor installed in the monitored structure, \(M\) represents the total number of sensors, \(t_j\ (j = 1, 2, ..., N)\) represents the \(j-th\) time step of the data registering and \(N\) is the total time observations during monitoring.
Additionally, each column represents data registration of one sensor.
The next step is to normalise the time series of each sensor data registrations by subtracting the mean value shown in Eqn.~\ref{mean value}:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{equation}
	\bar{u_i} = \frac{1}{N}\sum_{j=1}^{N}u_i(t_j)\ ,
	\label{mean value}
\end{equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Equation~\ref{normalised matrix} represents the normalised matrix.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{equation}
	U'(t)=
	\begin{bmatrix}
		u_1{(t1)}-\bar{u_1}       & u_2{(t1)}-\bar{u_2} & \dots  & u_M{(t1)}-\bar{u_M} \\
		u_1{(t2)}-\bar{u_1}       & u_2{(t2)}-\bar{u_2} & \dots  & u_M{(t2)}-\bar{u_M} \\
		\vdots 					  & \vdots 	  			& \ddots & \vdots \\
		u_1{(t_N)}-\bar{u_1}      & u_2{(t_N)}-\bar{u_2}& \dots  & u_M{(t_N)}-\bar{u_M}
	\end{bmatrix}
	\label{normalised matrix}
\end{equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
After computing the normalised matrix, the covariance matrix is computed as shown in Eqn.~\ref{covariance}:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{equation}
	C = \frac{1}{M}U'^TU' \ ,
	\label{covariance}
\end{equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Next, the eigenvalue and the corresponding eigenvector of the covariance matrix are computed through solving the following equation~\ref{eigvalue}:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{equation}
	(C-\lambda_iI)\psi_i =0 \ ,
	\label{eigvalue}
\end{equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
where \(I\) represents the \(M\times M\) identity matrix, \(\psi_i = [\psi_{i,1},\psi_{i,2}, \hdots, \psi_{i,j}]^T\) in which \(\psi_{i,j}(j=1, 2, \hdots, M)\) is the element related to the \(j-th\) sensor.
Usually, eigenvalues are sorted into decreasing order, particularly \(\lambda_1>\lambda_2>\hdots>\lambda_M\).
Then, the first eigenvector \(\psi_1\) corresponding to \(\lambda_1\) holds the greatest variance and consequently holds the most important information for the original matrix U.
The first few principal components hold most of the variance, whereas the remaining less important components involve the measurement of noise.
Accordingly, the first few eigenvectors are utilised as sensitive features for damage detection and localisation.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Auto-associative Neural Networks}
Auto-associative Neural Network (AANN) which is also called autoencoder is considered as one of the ANN architectures.
Generally, AANN is composed of five layers as shown in the Fig.~\ref{fig:AANN}, which includes the input layer, mapping layer, bottleneck layer (has less neurons than the input and the output layers), demapping layer and the output layer.
AANN is considered as an unsupervised learning technique, the idea behind AANN is to map the input using nonlinear functions then reconstruct it using nonlinear functions so the network can learn from the inputs themselves.
The main purpose of the bottleneck is to push the model to learn important features of the mapped data patterns.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}[!ht]
	\begin{center}
		\centering
		\includegraphics[scale=1]{Figures/Chapter_1/Auto-associative NN.png}
	\end{center}
	\caption{Auto-associative Neural Network architecture}
	\label{fig:AANN}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{}
Originally, AANN technique was based on nonlinear principal component analysis (NLPCA) which is a powerful statistical technique used in the process of the feature extracting and data dimensionality reduction~\cite{Dervilis2014}.
The difference between the PCA and NLPCA is that NLPCA is utilising nonlinear functions for mapping the input data as shown in Eqn~\ref{NLPCA}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{equation}
	T= G(X)
	\label{NLPCA}
\end{equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
As mentioned in the previously $T$ is the score matrix, $X$ is the input data with of size $(m \times n)$ where m represents the number of variables and n represents the number of observations, and G is a nonlinear vector function that holds several individual nonlinear functions.
Accordingly, the demapping process is performed by the inverse of the Eqn~\ref{NLPCA} using a nonlinear function $H$ as shown in Eqn~\ref{inverseNLPCA}.
The loss of information occurred due to the mapping and demapping process can be calculated in the reconstruction error matrix as shown in Eqn~\ref{errorMatrix}~\cite{Dervilis2014}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{equation}
	\hat{X} = H(T)
	\label{inverseNLPCA}
\end{equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{equation}
	E= X-\hat{X}
	\label{errorMatrix}
\end{equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Mahalanobis squared distance}
MSD is an effective multivariate distance measuring technique in which it measures the distance between a point and a distribution.
Therefore, MSD is utilised with multivariate statistics outlier detection~\cite{Worden2000}.
Assuming \(X\) to be a training set with data acquired when the undamaged structure is under environmental and/or operational variations (EOVs) with multivariate mean vector \(\mu\) and covariance matrix \(\Sigma\)~\cite{Farrar2013}.
Accordingly, the damage index \((DI_i)\) between feature vectors from training set \(X\) and any new feature vector from the test matrix \(Z\) is calculated using Eqn.~\ref{msd}.
\begin{equation}
	DI_i = (z_i-\mu)\Sigma^{-1}(z_i-\mu)^T
	\label{msd}
\end{equation}
where \(z_i\) is a tested feature vector.
The performance of this technique mainly relies on acquiring all likely EOVs in the training set~
\cite{Farrar2013}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Gaussian mixture models}
GMM is a clustering method commonly used with unsupervised learning, in which it aims to find main clusters of points in a dataset that share some common characteristics or features.
Additionally, GMM has also been referred to as Expectation-Maximization (EM) clustering that is based on the optimization strategy.
%%%%%%%%%%
The damage detection is performed based on multiple MSD-based algorithms, in which the covariance matrices and mean vectors are functions of the main components.
%%%%%%%%%%
A GMM is defined as a superposition of K Gaussian distributions as shown in Eqn. \ref{gmm}.

\begin{equation}
	p(x) = \sum_{k=1}^K P(k) \mathcal{N}(x|\mu_k,\Sigma_k)
	\label{gmm}
\end{equation}
where \(x\) represents the training samples in the dataset, and \(P(k)\) corresponds to the mixture proportion (contribution weight) of the \(k-\)th distribution, in which the mixture proportion must satisfy \(0\leq P(x)\leq 1\).
The sum of all mixture proportion satisfies the following Eqn.~\ref{mixture}
\begin{equation}
	\sum_{k=1}^{K}P(x) =1
	\label{mixture}
\end{equation}
\(\mathcal{N}(x|\mu_k,\Sigma_k)\) refers to the conditional probability of the instance \(x\) for the \(k-\)th Gaussian distribution \(\mathcal{N}(\mu_k,\Sigma_k)\) presented in Eqn.~\ref{conditional}, where \(\mu_k\) and \(\Sigma_k\) are the mean and the covariance of that Gaussian distribution respectively.
\begin{equation}
	\mathcal{N}(x|\mu_k,\Sigma_k) = \frac{\exp(-\frac{1}{2}(x-\mu_k)^T\Sigma_k^{-1}(x-\mu_k))}{(2\pi)^{\frac{d}{2}\sqrt{\det(\Sigma_k)}}}
	\label{conditional}
\end{equation}
The complete GMM is parameterized by the mean vectors, covariance matrices and the mixture weights from all component densities \(\{\mu_k,\Sigma_k, P(x)\}_{k=1,\hdots,K}\).

The parameters can be carried out from the training data using the classical maximum likelihood estimator (CMLE) based on the EM algorithm~\cite{Dempster1977}.
Damage can be detected through estimating \(k\) \(DIs\) for each data sample \(x\) as shown in Eqn. \ref{DIs}
\begin{equation}
	DI_q(x) = (x-\mu_k)\Sigma_k^{-1}(x-\mu_k)^T
	\label{DIs}
\end{equation}
where \(\mu_k\) and \(\Sigma_k\) refers to all observations from the \(k\) data component.
For each observation the DI is given by the smallest DI estimated on each component as in Eqn. \ref{DI}
\begin{equation}
	DI(x) = \min[DI_k(x)]
	\label{DI}
\end{equation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Classification techniques}
In this section, various classification techniques utilised in classifying the extracted features are presented.
\subsubsection{Support vector machine}
Support vector machine (SVM) is a supervised ML model that is utilised as a classification and regression tool.
The idea behind SVM is to find an optimal hyperplane (e.g separate line) in N-dimensional space (N is the number of features) that separates the classes, furthermore, the aim of the hyperplane is to maximize the margin between the points on either side hence so called "decision line/boundary".
Furthermore, when we try to separate two classes of data points, we could have many possible hyperplanes, however, our goal is to find the hyperplane that has the maximum margin (maximum distance between data points of both classes).
Figure~\ref{fig:SVM} shows SVM hyperplanes in 2D feature space and 3D feature space.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}[!ht]
	\centering
	\begin{subfigure}[b]{0.40\textwidth}
		\includegraphics[width=1\textwidth]{Figures/Chapter_2/2d_svm.png}
		\caption{Hyperplane 2D feature space.}
		\label{fig:2dsvm}
	\end{subfigure}
	\begin{subfigure}[b]{0.49\textwidth}
		\includegraphics[width=1\textwidth]{Figures/Chapter_2/3d_svm.png}
		\caption{Hyperplane 3D feature space.}
		\label{fig:3dsvm}
	\end{subfigure}
	\caption{SVM for 2D and 3D feature space.}
	\label{fig:SVM}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{K-Nearest Neighbor}
K-Nearest Neighbor (KNN) is a supervised ML technique utilized to perform classification tasks.
KNN does not have a specialized training phase.
It saves all the training data and uses the entire training set for classifying a new data point, which adds time complexity at the testing time.
Moreover, KNN is a non-parametric learning algorithm, which means it does not have any assumptions regarding the input data, which is useful considering the real-world data does not obey the typical theoretical assumptions such as linear separability, uniform distribution among others.

In the KNN technique, at the first, the distance between the new data point and the whole other data points is calculated.
Furthermore, any distance method can be applied e.g. Euclidean, Manhattan, etc.
Accordingly, it picks the K-nearest points, where K is an integer number (number of neighbors) that can be chosen in such a way the model will be able to predict new unseen data accurately.
Then, it assigns the new data point to the class to which the majority of the K data points belong.
In Fig.~\ref{fig:datapoints} shows initial data points (training set) before classification, and Fig.~\ref{fig:KNN_K_5} shows the result of applying KNN techniques on the data points (3 classes) assuming \(K=6\).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}[!ht]
	\centering
	\begin{subfigure}{0.49\textwidth}
		\centering
		\includegraphics[width=1\textwidth]{Figures/Chapter_2/KNN_data_points.png}
		\caption{Data points}
		\label{fig:datapoints}
	\end{subfigure}
	\hfill
	\begin{subfigure}{0.49\textwidth}
		\centering
		\includegraphics[width=1\textwidth]{Figures/Chapter_2/KNN_K_6.png}
		\caption{3-Classes with \(K=6\)}
		\label{fig:KNN_K_5}
	\end{subfigure}
	\caption{KNN algorithm: data classification with \(K=6\).}
	\label{fig:KNN}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Decision tree}
Decision trees are supervised ML that is used in applications for classification and regression.
Additionally, decision trees are considered the bases for many other ML techniques such as random forests, bagging and boosted decision trees.
The idea of a decision tree is to represent the whole data as a tree where each internal node represents a test on an attribute (a decision rule) and each branch represents an outcome of the test, and finally each leaf node (terminal node) holds the label of the class.

Decision tree can be divided into two categories:
\begin{enumerate}
	\item Categorical variable decision trees: which includes categorical target variables that are divided into categories. A category means that the decision falls into one of the categories and there is no in-between such as (Yes/No category).
	\item Continuous variable decision trees: which has a continuous target variable that can be predicted based on available information (e.g. crack length).
\end{enumerate}
Figure~\ref{fig:Decision_tree} presents a typical decision tree.
Any decision tree has a root node where data input is carried through.
Furthermore, the root node is split into sets of decision rules that result either in a leaf node which is a non-splitting node, or into another decision rule, creating what so-called a branch or sub-tree.
In case there are decision rules that can be eliminated from the tree, a process called "pruning" is applied to minimize the complexity of the algorithm.
\begin{figure}[!ht]
	\begin{center}
		\includegraphics[scale=1]{Figures/Chapter_3/decision_tree.png}
	\end{center}
	\caption{Decision tree.}
	\label{fig:Decision_tree}
\end{figure}


\documentclass[12pt]{cdblatex}
\usepackage{bssn-eqtns}

\begin{document}

\section*{PhysRevD.62.044034 equation (9)}

\begin{cadabra}
   from shared import *
   import cdblib

   jsonfile = 'bssn-eqtns-09.json'
   cdblib.create (jsonfile)

   # --------------------------------------------------------------------------

   DgBarDt := \partial_{t}{gBar_{ij}}.           # cdb(eq09.00,DgBarDt)
   DgBarDt := -2 N ABar_{i j}.                   # cdb(eq09.01,DgBarDt)

   canonicalise (DgBarDt)                        # cdb(eq09.02,DgBarDt)  # no change
                                                 # cdb(eq09.99,DgBarDt)  # no change

   cdblib.put ('DgBarDt',DgBarDt,jsonfile)
\end{cadabra}

\begin{dgroup*}
   \begin{dmath*} \cdb{eq09.00} = \Cdb*{eq09.02}\end{dmath*}
\end{dgroup*}

\end{document}


\documentclass[main.tex]{subfiles}
\begin{document}

\section*{7 October 2019}

% The exam for this part of the course: it might be around the end of october.

% The subscripts on last lecture \emph{were} inverted after all.

We introduce the \emph{mirror principle}: when the core contracts or expands, the envelope does the opposite.

The shell must remain at around the same temperature to maintain equilibrium: contracting the core would increase the temperature, therefore the envelope exapands.
This heuristic argument is actually derived from simulations.

The relevant time scale for oscillations is the free-fall, dynamical time scale.

We come back to the energy equation

\begin{equation}
  \pdv{L}{m} = \varepsilon - \varepsilon_\nu - \varepsilon_g
\end{equation}

we incorporate the nuclear energy generation rate and the energy lost as neutrino production into an effective energy generation rate per unit mass \(\varepsilon - \varepsilon_\nu = \varepsilon_{\text{eff}} \) and express the energy absorbed by the stellar layer as
%
\begin{align} \label{eq:heat-transfer-effective}
 \varepsilon_g = \dv{Q}{t} = \varepsilon_{\text{eff}} - \pdv{L}{m}
\,.
\end{align}
%

This makes the meaning of this transfer equation clearer.
Using the first and second laws of thermodynamics, and recalling some thermodynamical values: the specific heat at constant volume \(c_V = \qty(\pdv{Q}{T})_V \), the equation of state exponents \(\chi_T \) and \(\chi_\rho
\) which satisfy: \(P = T^{ \chi_{T}}\) and \(P = \rho^{\chi_{\rho }}\) and the adiabatic exponents \(\Gamma_{1,2,3}\), which are defined by
%
\begin{subequations}
\begin{align}
\Gamma_1 = \gamma_{\text{ad}} &= \qty(\pdv{\log P}{\log \rho })_{s}  \\
\frac{\Gamma_2 }{\Gamma_2 -1} = \frac{1}{\nabla _{\text{ad}}} &= \qty(\pdv{\log P}{\log T})_{s}  \\
\Gamma_3 -1 &= \qty(\pdv{\log T}{\log \rho })_{s}  \\
\,
\end{align}
\end{subequations}
%
and satisfy
%
\begin{align}
\frac{\Gamma_1 }{\Gamma_3 -1} = \frac{\Gamma_2 }{\Gamma_2 -1}
\,.
\end{align}

These are all \emph{exponents} in some power law.
We use log values since our variables change by orders of magnitude.

We start with  the definition of the entropy differential: \(\dd{Q}\) is not an exact differential but \(\dd{Q} / T = \dd{S}\) is. So, we express it using the first law of thermodynamics: \(\dd{Q} = \dd{E} + P \dd{V}\).
We assume the internal energy \(E\) to be a function of the volume \(V\) and of the temperature \(T\), so we will have:
%
\begin{align}
\dd{E} = \pdv{E}{V} \dd{V} + \pdv{E}{T} \dd{T}
\,,
\end{align}
%
which we can substitute into the expression for the entropy differential:
%
\begin{subequations}
\begin{align}
\dd{S} &= \frac{1}{T} \qty(\pdv{E}{V} \dd{V} + \pdv{E}{T} \dd{T} + P \dd{V})  \\
&= \qty(\frac{1}{T} \pdv{E}{V} + \frac{P}{T}) \dd{V}
+ \frac{1}{T} \pdv{E}{T} \dd{T}  \\
&= \pdv{S}{V} \dd{V} + \pdv{S}{T} \dd{T}
\,,
\end{align}
\end{subequations}
%
so we have identified the partial derivatives of the entropy. By Schwarz's lemma, we then have the equality:
%
\begin{subequations}
\begin{align}
\pdv[2]{S}{T}{V} &= \pdv[2]{S}{V}{T}  \\
\pdv{}{T} \qty(\frac{1}{T} \pdv{E}{V} + \frac{P}{T})&=
\pdv{}{V} \qty(\frac{1}{T} \pdv{E}{T})  \\
\frac{1}{T} \qty(\cancelto{}{\pdv[2]{E}{T}{V}} + \pdv{P}{T} )
- \frac{1}{T^2} \qty(\pdv{E}{V} + P) &=
\cancelto{}{\frac{1}{T} \pdv[2]{E}{V}{T}}  \\
 \pdv{E}{V} &= T \pdv{P}{T} - P
\,,
\end{align}
\end{subequations}
%
but we can turn the derivatives with respect to \(V\) with ones with respect to \(\rho \propto V^{-1}\), by
%
\begin{align}
\pdv{}{V} = \pdv{\rho }{V} \pdv{}{\rho } = - \rho^2 \pdv{}{\rho }
\,,
\end{align}
%
so after dividing through by \(\rho \) we find:
%
\begin{subequations}
\begin{align}
\rho \pdv{E}{\rho } &= -\frac{T}{\rho } \pdv{P}{T} + \frac{P}{\rho }  \\
&= - \frac{P}{\rho } \pdv{\log P }{\log T} + \frac{P}{\rho }  \\
\rho \pdv{E}{\rho }&= - \frac{P}{\rho } \qty(\pdv{\log P }{\log T} -1 )  \\
\pdv{E}{\log \rho }&= -\frac{P}{\rho } \qty(\chi_{T} - 1)
\,.
\end{align}
\end{subequations}
%

We used the fact that
%
\begin{align}
\pdv{}{\log x} = \pdv{x}{\log x} \pdv{}{x}= x \pdv{}{x}
\,.
\end{align}

Now, we can write the first law of thermodynamics for the specific energy density \(E(\rho, T)\):
%
\begin{subequations}
\begin{align}
\dd{Q} &= \dd{E} - \frac{P}{\rho^2} \dd{\rho }  \label{eq:heat-differential}\\
&= \pdv{E}{\log \rho } \dd{\log \rho }
+ \pdv{E }{\log T} \dd{\log T} - \frac{P}{\rho}  \dd{\log \rho }  \\
&= \qty(- \frac{P}{\rho } \qty(\chi_{T} -1) - \frac{P}{\rho }) \dd{\log \rho } + \pdv{E}{\log T} \dd{\log T}  \\
&= - \frac{P \chi_T }{\rho } \dd{\log \rho } + \pdv{E}{\log T} \dd{\log T}
\,,
\end{align}
\end{subequations}
%
which in the adiabatic (\(\dd{Q} = 0\)) case reduces to
%
\begin{subequations}
\begin{align}
\Gamma_3 - 1 \overset{\text{def}}{=} \pdv{\log T }{\log \rho } &= - \frac{P \chi_{T}  }{\rho } \pdv{\log T}{E} = - \frac{P \chi_T }{\rho T c_V}  \\
&=- \frac{1}{\rho } \times \underbrace{\frac{P \chi_T}{T}}_{\pdv{P}{T}} \times \underbrace{\frac{1}{c_V}}_{\pdv{T}{E}}  \\
&= - \frac{1}{\rho } \pdv{P}{E}
\,,
\end{align}
\end{subequations}
%
where we used the fact that \(c_V = \pdv*{E}{T}\). This can also be written as
%
\begin{align}
\pdv{E}{P} = \frac{1}{\rho (\Gamma_3 -1)}
\,.
\end{align}

If we drop the hypothesis of adiabaticity, we can study the variation with respect to time of \(Q\), both when writing \(E = E(\rho , T)\) and when writing \(E = E(\rho , P)\).
In the first case we can use equation \eqref{eq:heat-differential}, ``dividing through by \(\dd{t}\)'' (more formally, applying the differential covector equation to the vector \(\partial_{t}\)), after some manipulation we can bring out a factor \(T \pdv*{E}{T}\) to get:
%
\begin{align} \label{eq:heat-variation-temperature-dependence}
\dv{Q}{t} = T \pdv{E}{T} \qty(\dv{\log T}{t} + \frac{\rho \pdv{E}{\rho } - \frac{P}{\rho }}{T \pdv{E}{T}} \dv{\log \rho }{t})
\,,
\end{align}
%
and similarly if we express \(E = E(\rho, P)\) we find
%
\begin{align}\label{eq:heat-variation-pressure-dependence}
\dv{Q}{t} = P \pdv{E}{P} \qty(\dv{\log P}{t} + \frac{\rho \pdv{E}{\rho } - \frac{P}{\rho }}{P \pdv{E}{P}} \dv{\log \rho }{t})
\,.
\end{align}

We can simplify these two expressions by recalling some results from before, plus two more expression we now derive for the coefficients \(\Gamma_1\) and \(\Gamma_3 -1\): we assume adiabaticity, and get
%
\begin{subequations}
\begin{align}
0=\dd{S} &= \frac{1}{T} \qty(\pdv{E}{\rho } \dd{\rho } + \pdv{E}{P } \dd{P} - \frac{P}{\rho^2} \dd{\rho })  \\
0&= \qty(\rho \pdv{E}{\rho } - \frac{P}{\rho } ) \dd{\log \rho }   + P \pdv{E}{P} \dd{\log P}  \\
\Gamma_1 = \left. \dv{\log P}{\log \rho } \right\vert_{\text{ad}} &= \frac{\frac{P}{\rho } - \rho \pdv{E}{\rho }}{P \pdv{E}{P}}
\,
\end{align}
\end{subequations}
%
and
%
\begin{subequations}
\begin{align}
0 = \dd{S} &= \frac{1}{T} \qty(\pdv{E}{\rho } \dd{\rho } + \pdv{E}{T} \dd{T} - \frac{P}{\rho^2} \dd{\rho })  \\
0&= \qty(\rho \pdv{E}{ \rho } - \frac{P}{\rho }) \dd{\log \rho } + T \pdv{E}{T} \dd{\log T}  \\
\Gamma_3 -1 = \left. \dv{\log T}{\log \rho } \right\vert_{\text{ad}} &= \frac{\frac{P}{\rho } - \rho \pdv{E}{\rho }}{T \pdv{E}{T}}
\,,
\end{align}
\end{subequations}
%

The identifications are:
%
\begin{align}
\dv{Q}{t} = T \underbrace{\pdv{E}{T}}_{c_V} \qty(\dv{\log T}{t} + \underbrace{\frac{\rho \pdv{E}{\rho } - \frac{P}{\rho }}{T \pdv{E}{T}}}_{- (\Gamma_3 - 1) } \dv{\log \rho }{t})
\,,
\end{align}
%
and
%
%
\begin{align}
\dv{Q}{t} = P \underbrace{\pdv{E}{P}}_{1/ \rho (\Gamma_3 -1)} \qty(\dv{\log P}{t} + \underbrace{\frac{\rho \pdv{E}{\rho } - \frac{P}{\rho }}{P \pdv{E}{P}}}_{- \Gamma_1 } \dv{\log \rho }{t})
\,.
\end{align}

Finally, we substitute in the equation of energy conservation:
%
\begin{align}
\dv{Q}{t} = \varepsilon_{\text{eff}} - \pdv{L}{m}
\,,
\end{align}
%
to find the equations for the evolution of the energy and pressure:
%
\boxalign{
\begin{subequations}
\begin{align}
  \pdv{\log P }{t} &= \Gamma_1 \pdv{\log \rho}{t} + \frac{\rho}{P} (\Gamma_3 - 1) \qty(\varepsilon_{\text{eff}} - \pdv{L}{m})  \label{eq:log-pressure-energy-conservation}\\
  \pdv{\log T }{t} &= (\Gamma_3 - 1) \pdv{\log \rho}{t} + \frac{1}{c_V T} \qty(\varepsilon_{\text{eff}} - \pdv{L}{m}) \label{eq:log-temperature-energy-conservation}
\end{align}
\end{subequations}}

\subsection{Linear perturbation theory}

Say we have a solution for these equations, we look at linear perturbations of them.
This makes sense: the main solution is basically static on the pulsation time-scales.

The perturbed model is \(f = f(m)\),  the unperturbed one is \(f_0(m)\).
The Lagrangian perturbation is \(\delta f (m, t) = f(m, t) - f_0(m, t)\).

Let us consider specific cases for \(f\): the radial displacement is \(\delta r (m, t)\). The position of the layer at time \(t\) is \(r = r_0 + \delta r\).

We can write:
%
\begin{equation}
  r = r_0 \qty(1 + \frac{\delta r}{r_0} ) = r_0 (1+ \zeta)
  \,,
\end{equation}
%
where we define \(\zeta = \delta r / r_0 \).

In general the fractional perturbation \(\delta f / f_0\) is assumed to be \(\ll 1 \). So, \(\delta f / f_0 \sim \delta_f / f\). Formally, we only consider terms which are of first order in either perturbed function.
We will insert expressions which are functions of perturbations of all our variables, and thus get linear differential equations.

\subsubsection{Properties of Lagrangian perturbations}

In general for a Lagrangian perturbation we have the following useful properties:
\begin{enumerate}
  \item we can use the properties of derivatives: \(\delta (f^n) = n f_0^{n-1} \delta f\);
  \item we can use the properties of logarithmic derivatives:
  %
  \begin{align} \label{eq:lagrangian-perturbation-logarithmic}
  \frac{ \delta \qty( \prod f_i)}{\prod f_i} = \sum \frac{ \delta f_i}{f_i}
  \,;
  \end{align}
  %
  \item \(\delta \) commutes with partial derivation.
\end{enumerate}

\subsubsection{Continuity equation}

Let us try the continuity equation, substituting in \(r = r_0 (1+\zeta)\) and \(\rho = \rho_0 (1 + \delta \rho / \rho_0)\).

\begin{subequations}
\begin{align}
  \pdv{r}{m} &= \frac{1}{4 \pi r^2 \rho }  \\
  \pdv{}{m} \qty(r_0 (1+\zeta)) &=
  \frac{1}{4 \pi r_0^2} \qty(1+\zeta)^{-2} \qty(1 + \frac{\delta \rho}{\rho_0})^{-1}
\end{align}
\end{subequations}

and we use \((1+x)^n \approx 1 + nx\) plus the zeroth order equation: \(\pdv*{r_0 }{m} = 1 / 4 \pi r_0^2 \rho_0\).
With these, we find:
%
\begin{equation}
  4 \pi \rho_0^2 \qty(\pdv{r_0}{m} (1+\zeta) + r_0 \pdv{\zeta}{m})
  = (1-2 \zeta) -\frac{\delta \rho}{\rho_0}
\end{equation}

We can collapse the equation into:
\boxalign{
\begin{align} \label{eq:linearized-continuity}
  \frac{\delta \rho}{\rho_0} =
  - 3 \zeta - 4 \pi r_0^3 \rho_0 \pdv{\zeta}{m}
\end{align}}

or, the density perturbation is proportional with a negative constant to the radial perturbation, plus a term proportional to \(\pdv*{\zeta}{m}\).
If there is a positive gradient of radial perturbation, the corresponding layer expands.

\subsubsection{Momentum conservation}

Let us also perturb the momentum conservation equation; the unperturbed solution will be at hydrostatic equilibrium, so \(\pdv*[2]{r_0}{t} = \pdv{r_0 }{t} =0 \), which means
%
\begin{align} \label{eq:hydrostatic-equilibrium}
\pdv{P_0 }{m} = - \frac{Gm}{4 \pi r_0^{4}}
\,.
\end{align}

Substituting in we find that, to linear order:
%
\begin{subequations}
\begin{align}
\pdv[2]{r}{t} &= - 4 \pi r^2 \pdv{P}{m} - \frac{Gm}{r^2} \\
\pdv[2]{}{t} \qty(r_0 \qty(1 + \zeta ))
&=
- 4 \pi \qty(r_0 (1+z))^2 \pdv{}{m} \qty(P_0 \qty(1 + \frac{ \delta P }{P_0 })) - \frac{Gm}{r_0^2 (1 + \zeta^2)}
\\
r_0 \pdv[2]{\zeta }{t}
&=
- 4 \pi r_0^2 \qty(1 + 2 \zeta )
\qty(\pdv{P_0 }{m} \qty(1 + \frac{ \delta P}{P_0 }) + P_0 \pdv{}{m} \qty(\frac{ \delta P}{P_0 }))
- \frac{GM}{r_0^2} \qty(1 - 2z)
\\
\frac{r_0}{4 \pi r_0^2} \pdv[2]{\zeta }{t} &= - \qty(1 + 2 \zeta )
\qty(\pdv{P_0 }{m} \qty(1 + \frac{ \delta P}{P_0 }) + P_0 \pdv{}{m} \qty(\frac{ \delta P}{P_0 }))
+ \qty(1 - 2z) \pdv{P_0 }{m} \marginnote{Used equation \eqref{eq:hydrostatic-equilibrium}.} \\
\begin{split}
\frac{1}{4 \pi r_0} \pdv[2]{\zeta }{t} &=
- \qty(\pdv{P_0 }{m} \qty(1 + \frac{ \delta P}{P_0 }) + P_0 \pdv{}{m} \qty(\frac{ \delta P}{P_0 })) + \pdv{P_0 }{m} + \\
&\phantom{=}\ - 2\zeta \qty(\pdv{P_0 }{m} \qty(1 + \frac{ \delta P}{P_0 }) + P_0 \pdv{}{m} \qty(\frac{ \delta P}{P_0 }))
- 2 \zeta \pdv{P_0 }{m}
\end{split} \marginnote{Split the terms into different orders of \(\zeta \)} \\
\begin{split}
\frac{1}{4 \pi r_0} \pdv[2]{\zeta }{t} &=
- \pdv{}{m} \qty(\delta P ) + \\
&\phantom{=}\
- 4 \zeta \pdv{P_0 }{m}
- 2\zeta \pdv[]{}{m} \qty(\delta P )
\end{split} \\
r_0 \dv[2]{\zeta }{t} &= -4 \pi r_0^2 \qty( \pdv{}{m} (\delta P) + 4 \zeta \pdv{P_0 }{m}) \marginnote{Neglected a second order term}
\,.
\end{align}
\end{subequations}

The final equation then looks like
%
\boxalign{
\begin{align} \label{eq:linearized-momentum-conservation}
r_0 \pdv[2]{\zeta}{t} = - 4 \pi r_0^2 \qty(
  \underbrace{P_0 \pdv{}{m} \qty( \frac{ \delta P}{P_0 }) +
  \frac{ \delta P}{P_0 } \pdv{P_0 }{m}}_{ \pdv{ \delta P}{m} } + 4 \zeta \pdv{P_0 }{m}
)
\,,
\end{align}}
%
and we can now give a physical interpretation of the various terms.

We have a term \(-16 \pi r_0^2 \zeta \pdv*{P_0}{m} = 4 \zeta Gm /r_0^2\).
This, by itself, is a force moving the system away from equilibrium: the equation with only that term on the RHS is precisely like a harmonic repulsor, \(\ddot{\zeta} = \omega^2 z\) with
%
\begin{align}
\omega^2 = \frac{4Gm}{r_0^3}
\,.
\end{align}

This term is of geometric origin: as the layer moves outwards it expands, and the expansion is favoured by the decrease in the gravitational potential and the corresponding increase in pressure due to the increase of the area of the layer.

The equation with only the other term looks like
%
\begin{align}
\ddot{\zeta} = - 4 \pi r_0 \pdv{ \delta P }{m}
= - 4 \pi r_0  \qty( P_0 \pdv{}{m} \qty(\frac{ \delta P }{P_0 }) + \frac{ \delta P}{P_0 } \pdv{P_0 }{m})
\,.
\end{align}

\todo[inline]{According to the slides, the action of this term is to be split in two: for the second, a restoring force towards equilibrium, since if the pressure decreases as the layer expands then the force is inward, and a more vague interpretation for the first bit, stating that ``a non uniform variation of \(\delta P\) from a layer to the next generates a change in the pressure gradient'': but it \emph{is} a change in the pressure field\dots}

\subsubsection{Energy conservation}

Let us also consider the expression for the time derivative of \(\log P\) coming from the energy conservation equation: equation \eqref{eq:log-pressure-energy-conservation}; the perturbed equation for the time derivative of \(\log T\) (equation \eqref{eq:log-temperature-energy-conservation}) is analogously derived.
Besides \(\rho \) and \(P\), we also perturb the adiabatic exponents and the \(\dv*{Q}{t} = \epsilon_{\text{eff}} - \pdv*{L}{m}\) term.

After difficult manipulations we get back an equation which relates the changes in density and pressure to the change in energy: we manipulate until we get something which is similar to the original equation:
%
\boxalign{
\begin{subequations}
\begin{align}
\pdv{}{t} \qty(\frac{ \delta P }{P_0 }) &=
\Gamma_{1,0} \pdv{}{t} \qty(\frac{ \delta \rho }{\rho_0 })
+ \frac{\rho_0 }{P_0 } \qty(\Gamma_{3, 0} -1) \delta \qty(\epsilon_{\text{eff}} - \pdv{L}{m}) \label{eq:perturbed-linearized-energy-log-pressure}\\
\pdv{}{t} \qty(\frac{ \delta T }{T_0 }) &=
(\Gamma_{3,0} - 1) \pdv{}{t} \qty(\frac{ \delta \rho }{\rho_0 })
+ \frac{1}{c_V T_{0}} \delta \qty(\epsilon_{\text{eff}} - \pdv{L}{m}) \label{eq:perturbed-linearized-energy-log-temperature}
\,,
\end{align}
\end{subequations}}

%
where we note that the first index of the adiabatic exponents denotes \emph{which exponent it is}, while the second indicates that it is the unperturbed value.

\todo[inline]{The last \(T\) in the temperature equation is not unperturbed nor perturbed in the slides.}

\subsubsection{Luminosity equation}

In the radiative case with the diffusion approximation we can perturb the luminosity equation, \eqref{eq:luminosity-equation}. It is much more convenient not to calculate \(\delta L\) but \(\delta L  / L_0 \) instead: this allows us to use the logarithmic derivative properties of the perturbation; also, since
%
\begin{align}
T^{3} \pdv{T}{m} = T^{4} \pdv{\log T}{m}
\,
\end{align}
%
we use the latter expression, which is more convenient.
So we have \(L = \prod f_i\), with
%
\begin{align}
f_{i} = \qty{- \frac{64 \pi^2 ac}{3}, r^{4}, \kappa_{R}^{-1}, T^{4}, \pdv{\log T}{m}}
\,.
\end{align}

Now we can apply the rule given in equation \eqref{eq:lagrangian-perturbation-logarithmic}: we find
%
\begin{equation}
  \frac{\delta L}{L_0} = 4 \zeta + 4 \frac{\delta T}{T_0} - \frac{\delta \kappa_R}{\kappa_{R,0}} + \qty(\pdv{\log T}{m})^{-1}_{0} \pdv{}{m} \qty( \frac{\delta T}{T_0 })
  \,,
\end{equation}
%
where we used the simplification
%
\begin{subequations}
\begin{align}
\delta \qty(\pdv{\log T}{m}) &= \delta \qty(\frac{1}{T} \pdv{T}{m})  \\
&= \delta \qty(\frac{1}{T}) \pdv{T_0 }{m} + \frac{1}{T_0 }  \delta \qty(\pdv{T}{m})  \\
&= - \frac{ \delta T}{ T_{0}} \pdv{T_0 }{m} + \frac{1}{T_0 } \pdv{ \delta T }{m }  \\
&= \pdv{}{m} \qty(\frac{ \delta T}{T_0 }) \marginnote{Inverse application of the product rule}
\,.
\end{align}
\end{subequations}

The last step is to assume a certain dependence of the Rosseland mean opacity on the temperature and density: specifically, it is a ``Kramers-like'' expression, given by
%
\begin{align}
\kappa_{R} \propto \rho^{n} T^{-s}
\,,
\end{align}
%
which can be substituted into our expression: the proportionality factor does not matter, and we get additional temperature and density terms:
%
\boxalign{
\begin{align}
\frac{ \delta L}{L_0 }
= 4 \zeta + (4+s) \frac{\delta T}{T_0} - n \frac{ \delta \rho }{\rho_0 } + \qty(\pdv{\log T}{m})^{-1}_{0} \pdv{}{m} \qty( \frac{\delta T}{T_0 })
\,,
\end{align}}
%

In the end, we have a set of four linear PDE equations (written as a 5-equation system).

These describe implicitly how the properties of the star change over time.

Pulsation usually affects mostly the outer layers of a star.

% Moving on to

\section{Adiabatic oscillations}

\subsection{Derivation of the LAWE}

Exploiting the adiabatic approximation we will get the  Linear Adiabatic Wave Equation (LAWE): a single equation which summarizes the 4 and can be solved explicitly.

Recall the heat transfer equation \eqref{eq:heat-transfer-effective}:
if we suppose that each layer does not lose nor gain heat, \(\dv*{Q}{t} =0\), this implies that \(\delta (\varepsilon_{\text{eff}} - \pdv*{L}{m} )=0\).

Is this approximation justified? The term multiplying \(\delta (\varepsilon_{\text{eff}} - \pdv*{L}{m} )\) in the perturbed energy equation \eqref{eq:perturbed-linearized-energy-log-pressure} is \(\rho (\Gamma_3-1)/P  = \chi_T / (c_V T)\).
Usually \(\chi_T \sim 1\), while the density perturbation term is multiplied by \(\Gamma_1 \sim 1\).

This term,  \( \chi_T / (c_V T) \delta (\varepsilon_{\text{eff}} - \pdv*{L}{m} )\), is of the order \(1/\tau_{\text{th}}\), the thermal time scale of this layer, while the term before, \(\Gamma_1 \pdv*{}{t} (\delta \rho / \rho)\), is of the order \(1/\tau_{\text{dyn}}\), the dynamical time scale.

\todo[inline]{Is this just because the first term contains a time derivative while the second one does not?}

Therefore, we neglect the second part.
This only works for the star as a whole, not for single layers.
There are stellar layers which are \emph{strongly} non-adiabatic (driving layers). We will need some non-adiabatic theory to explain how pulsations \emph{start}.

So, the energy conservation equations become
%
\begin{align} \label{eq:linearized-adiabatic-energy-conservation}
\frac{ \delta P}{P} = \Gamma_1 \frac{ \delta \rho }{\rho }
\qquad \text{and} \qquad
\frac{ \delta T }{T} = \qty(\Gamma_3 -1) \frac{ \delta \rho }{\rho }
\,,
\end{align}
%
which we can substitute into the momentum conservation equation \eqref{eq:linearized-momentum-conservation} to find
%
\begin{align}
r_0 \pdv[2]{\zeta }{t} =
- 4 \pi r_0^2 \qty(
P_0 \pdv{}{m} \qty(\Gamma_1  \frac{ \delta \rho}{\rho })
+ \Gamma_1 \frac{ \delta \rho }{\rho } \pdv{P_0}{m}
+ 4 \zeta \pdv{P_0 }{m}
)
\,,
\end{align}
%
and now we can use the continuity equation \eqref{eq:linearized-continuity} which gives us an expression for \(\delta \rho  / \rho \): inserting it we get
%
\begin{subequations}
\begin{align}
\begin{split}
r_0 \pdv[2]{\zeta }{t} &=
- 4 \pi r_0^2 \bigg[
P_0 \pdv{}{m} \qty(\Gamma_1  \qty(-3\zeta - 4 \pi r_0^3 \rho_0 \pdv{\zeta }{m})) + \\
&\phantom{=}\ + \Gamma_1 \qty(- 3\zeta - 4 \pi r_0^3 \rho_0 \pdv{\zeta }{m}) \pdv{P_0}{m}
+ 4 \zeta \pdv{P_0 }{m}
\bigg]
\end{split}  \\
\label{eq:step0-LAWE-derivation-substituted}
\begin{split}
r_0 \pdv[2]{\zeta }{t} &= 4 \pi r_0^2 \bigg(
\underbrace{(3 \Gamma_1 - 4)\zeta \pdv{P}{m}}_{\Circled{1}}
+
\underbrace{4 \pi r_0^3 \Gamma_1 \rho \pdv{\zeta }{m} \pdv{P}{m} }_{\Circled{2}}
+ \\
&\phantom{=}\
+ \underbrace{3 P \pdv{}{m} \qty(\Gamma_1 \zeta )}_{\Circled{3}}
+ \underbrace{4 \pi P \pdv{}{m} \qty(\Gamma_1 r^3 \rho \pdv{\zeta }{m})}_{\Circled{4}}
\bigg)
\,,
\end{split}
\end{align}
\end{subequations}
%
and now we can can manipulate the terms inside the parentheses; we start to drop the zero indices, any quantity not being \(\delta \)'d is meant to be unperturbed.
The first and third terms are respectively given by
%
\begin{align}
\Circled{1} =
\pdv{P}{m} \zeta \qty(3 \Gamma_1 - 4) =
\hlc{pink}{\zeta \pdv{}{m} \qty((3 \Gamma_1 - 4) P)}- \hlc{teal}{3 \zeta P \pdv{\Gamma_1}{m}} \marginnote{Backwards derivative of a product}
\,
\end{align}
%
and
%
\begin{align} \label{eq:step1-LAWE-derivation}
\Circled{3} =
3 P \pdv{}{m} \qty(\Gamma_1 \zeta ) = 3P \Gamma_1 \pdv{\zeta }{m} + \hlc{teal}{ 3 P \zeta \pdv{\Gamma_1 }{m}}
\,,
\end{align}
%
so we can see that the highlighted terms cancel.
Also, the other term in equation \eqref{eq:step1-LAWE-derivation} can be rewritten using the continuity equation \eqref{eq:continuity}:
%
\begin{align}
3P \Gamma_1 \pdv{\zeta }{m}
= \hlc{lightgray}{12 \pi r^2 \rho  \Gamma_1 P \pdv{\zeta }{m} \pdv{r}{m}}
\,,
\end{align}
%
so we can see that if we expand the fourth term in \eqref{eq:step0-LAWE-derivation-substituted} we find a thing that is equal to it:
%
\begin{align}
\Circled{4} =
4 \pi P \pdv{}{m} \qty(r^3 \times  \Gamma_1  \rho \pdv{\zeta }{m})
= \hlc{lightgray}{4 \pi P \times 3 r^2 \pdv{r}{m} \Gamma_1 \rho \pdv{\zeta }{m}}
+  \hlc{lime}{4 \pi P r^3 \pdv{}{m} \qty(\Gamma_1 \rho \pdv{\zeta }{m})}
\,,
\end{align}
%
so we will have twice that contribution in the final result.

For now then, we have shown that
%
\begin{align}
\Circled{1} + \Circled{3} + \Circled{4} =
\hlc{pink}{\zeta \pdv{}{m} \qty((3 \Gamma_1 - 4) P)} +
2 \times \hlc{lightgray}{12 \pi P r^2 \pdv{r}{m} \Gamma_1 \rho \pdv{\zeta }{m}}
+ \hlc{lime}{4 \pi P r^3 \pdv{}{m} \qty(\Gamma_1 \rho \pdv{\zeta }{m})}
\,.
\end{align}

Now then, the full equation reads
%
\begin{subequations}
\begin{align}
\begin{split}
r_0 \pdv[2]{\zeta }{t} &=
4 \pi r^2 \zeta \pdv{}{m} \qty((3\Gamma_1 -4) P)
+ \hlc{red}{16 \pi^2 r_0^5 \Gamma_1 \rho \pdv{\zeta }{m} \pdv{P}{m}} + \\
&\phantom{=}\
\hlc{red}{+ 96 \pi^2 P r^4 \pdv{r}{m} \Gamma_1 \rho \pdv{\zeta }{m}
+ 16 \pi^2 P r^5 \pdv{}{m} \qty(\Gamma_1 \rho \pdv{\zeta }{m})}
\,,
\end{split}
\end{align}
\end{subequations}
%
and we can notice a certain similarity between the highlighted terms: consider the expression
%
\begin{align}
\pdv{}{m} \qty(16 \pi^2 \Gamma_1 P \rho r^{6} \pdv{\zeta }{m})
\,,
\end{align}
to which we can apply the general expression, which holds for nonzero differentiable functions of a certain variable \(x\) (and if it is interpreted as a limit, even if the functions go to 0):
%
\begin{align}
\pdv{}{x} \qty(\prod_i f_i)
= \qty(\prod_i f_i) \sum_i \frac{1}{f_i} \pdv{f_i}{x}
\,,
\end{align}
%
so
%
\begin{subequations}
\begin{align}
\frac{\displaystyle\pdv{}{m} \qty(16 \pi^2 \Gamma_1 P \rho r^{6} \pdv{\zeta }{m})}{\displaystyle 16 \pi^2 \Gamma_1 P \rho r^{6} \pdv{\zeta }{m}} &=
\frac{1}{P} \pdv{P}{m}
+ \frac{1}{\Gamma_1 \rho \pdv*{\zeta }{m}} \pdv{}{m} \qty(\Gamma_1 \rho \pdv{\zeta }{m})
+ \frac{1}{r^{6}} \pdv{r^{6}}{m}  \\
&= \frac{1}{P} \pdv{P}{m}
+ \frac{1}{\Gamma_1 \rho \pdv*{\zeta }{m}} \pdv{}{m} \qty(\Gamma_1 \rho \pdv{\zeta }{m})
+ \frac{6}{r} \pdv{r}{m}
\,,
\end{align}
\end{subequations}
%
so
%
\begin{subequations}
\begin{align}
\begin{split}
\frac{1}{r} \pdv{}{m} \qty(16 \pi^2 \Gamma_1 P \rho r^{6} \pdv{\zeta }{m}) &=
\hlc{red}{16 \pi^2 r^{5} \Gamma_1 \rho \pdv{\zeta }{m} \pdv{P}{m} +} \\
&\phantom{=}\
\hlc{red}{+ 16 \pi^2 r^{5} P \pdv{}{m} \qty(\Gamma_1 \rho \pdv{\zeta }{m}) + 96 \pi^2r^{4} \Gamma_1 \rho \pdv{\zeta }{m} P \pdv{r}{m}}
\,.
\end{split}
\end{align}
\end{subequations}

Now, we can finally write the simplest form of the LAWE:
%
\boxalign{
\begin{align} \label{eq:LAWE}
r \pdv[2]{\zeta}{t} =
4 \pi r^2 \zeta \pdv{}{m} \qty((3 \Gamma_1 - 4)P)+
\frac{1}{r} \pdv{}{m} \qty(16 \pi^2 \Gamma_1 P \rho r^6 \pdv{\zeta}{m} )
\,,
\end{align}}
%

Next time, we will decompose: \(\zeta(m, t) = \eta(m) e^{i \sigma t}\) with a constant \(\sigma\): putting this into the LAWE we simplify the exponentials and get the space dependent form of the LAWE.

The LAWE is a Storm-Liouville equation.

\end{document}


\documentclass[12pt]{article}
\topmargin=0cm \oddsidemargin=0cm
\textheight=22cm
\textwidth=17cm
\parskip=0.1cm
\parindent=0.5cm
\pdfminorversion=4
\renewcommand{\baselinestretch}{1.2}

\usepackage{mathrsfs}

\usepackage{latexsym}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{epsfig}
\usepackage{subfigure}
\usepackage{arydshln}

\usepackage[nolists]{endfloat}

\usepackage{natbib}
\usepackage{times}
\usepackage[usenames]{color}
%\usepackage{rotating}
\bibpunct{(}{)}{;}{a}{,}{,}

% Nuevos comandos que estoy usando:
\newcommand{\bx}{\boldsymbol{x}}
\newcommand{\by}{\boldsymbol{y}}
\newcommand{\bu}{\boldsymbol{u}}
\newcommand{\bU}{\boldsymbol{U}}
\newcommand{\bI}{\boldsymbol{I}}
\newcommand{\bbeta}{\boldsymbol{\beta}}
\newcommand{\bmu}{\boldsymbol{\mu}}
\newcommand{\brho}{\boldsymbol{\rho}}
\newcommand{\bm}{\boldsymbol{m}}
\newcommand{\bS}{\boldsymbol{S}}
\newcommand{\bPsi}{\boldsymbol{\Psi}}

\newcommand{\Xcur}{\mathscr{X}}

\newtheorem{definition}{Definition}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{proposition}{Proposition}
\newtheorem{corollary}{Corollary}
\newtheorem{remark}{Remark}
\pdfminorversion=4

\newcommand{\afb}{\color{red}}

\begin{document}

\section{The models}
In what follows, the three models that we are now considering are presented. The term $F(\cdot\mid \theta)$ and $H(\cdot \mid \mu)$ will denote general distribution functions with parameters (can be vectors) $\theta$ and $\mu$, respectively, otherwise the distribution will be explicit. \\

Model 1:
\begin{align*}
&y_i\mid \theta_i \sim N(\theta_i, \sigma^2),\\
&\theta_i= \theta^{\star}_{\xi_i^{\star}},\\
&\xi_i^{\star}\mid \boldsymbol{w} \sim Categorical(\boldsymbol{w}),\\
&w_k=z_k\prod_{l<k}(1-z_l), \hspace{0.5cm} k=2, \ldots, K, w_1=z_1\\
&z_k \sim Beta(1, \alpha), \hspace{0.5cm} k=1, \ldots, K-1, z_K=1, \\
&\theta^{\star}_k  \sim N(\mu, \tau^2), \hspace{0.5cm}k=1, \ldots, K.
\end{align*}

Model 2:
\begin{align*}
&y_i\mid \theta_i \sim F(\cdot \mid \theta_i),\\
&\theta_i= \tilde{\theta}_{\tilde{\xi}_i},\\
&(\tilde{\xi}_1, \ldots, \tilde{\xi}_n) \sim CRP(\alpha),\\
&\alpha \sim H_1,\\
&\tilde{\theta}_k  \sim H(\cdot\mid \mu), \hspace{0.5cm}k=1, \ldots, 	\tilde{K}.
\end{align*}

Model 3:
\begin{align*}
&y_i\mid \theta_i \sim N(\theta_i, \sigma^2),\\
&\theta_i\mid G\sim G,\\
&G  \sim DP(\alpha, N(\mu, \tau^2)).
\end{align*}

\section{About Model 2}
I'll give more details on the definition of the model in the BUGS code and describe the sampler. I will change the notation just to be consistent with the notation in code.
\subsection{The model and its BUGS Code}
\begin{minipage}{.5\textwidth}
The model:
\begin{align*}
&\tilde{\theta}_i  \sim H(\cdot\mid \mu), \hspace{0.5cm}i=1, \ldots, 	n,\\
&({\xi}_1, \ldots, {\xi}_n) \sim CRP(\alpha),\\
& \alpha \sim H_1,\\
&\theta_i= \tilde{\theta}_{{\xi}_i},\\
&y_i\mid \theta_i \sim F(\cdot \mid \theta_i).
\end{align*}
\end{minipage}
\begin{minipage}{.5\textwidth}
BUGS code:
\begin{verbatim}
Code=nimbleCode({
    for(i in 1:n){
    thetatilde[i] ~ H
    }
    xi[1:n] ~ dCRP(conc)
    conc ~ H_1

    for(i in 1:n){
      theta[i] <- thetatilde[xi[i]]
      y[i] ~ F
    }
  })
\end{verbatim}
\end{minipage}
\vspace{0.5cm}

I have tried different combinations of $F$ and $H$: normal with known variance and normal, normal with unknown variance and normal, Poisson and gamma, Weibull and gamma.

Saying that $({\xi}_1, \ldots, {\xi}_n) \sim CRP(\alpha)$ means that $\xi_1=1$, and
\begin{align*}
\xi_i\mid \xi_1, \ldots, \xi_{i-1} \sim \frac{1}{i-1+\alpha} \sum_{j=1}^{i-1}\delta_{\xi_j} + \frac{\alpha}{i-1+\alpha}\delta_{\xi^{new}},
\end{align*}
where $\xi^{new}=\max\{ \xi_1, \ldots, \xi_{i-1}\}+1$.

\subsection{Samplers}
\begin{enumerate}
\item Sampling $\tilde{\theta}_k$: using NIMBLE's sampler.
\item Sampling $\xi_i$:
\begin{enumerate}
\item when $H$ is not conjugate for $F$, a non conjugate sampler sampler based on algorithm 8 of \cite{neal;2000} is used.  More specifically, the $\xi_i$ are updated one at the time  from the following conditional distribution
%each $\tilde{\theta}_k$ is updated from its  posterior based on  directly or an  random walk Metropolis-Hastings algorithm, which by the specification of the model, updates the  $\tilde{\theta}_k$ that are related to observations from the corresponding posterior distribution, or from the prior distribution if it is not related to observations.
\begin{align*}
\xi_i\mid \boldsymbol{y}, \xi_{-i},... \sim \frac{1}{n-1+\alpha}\sum_{j \neq i}f(y_i\mid \tilde{\theta}_{\xi_j})\delta_{\xi_j} +\frac{\alpha}{n-1+\alpha}f(y_i\mid \tilde{\theta}_{\xi^{new}})\delta_{\xi^{new}},
\end{align*}
this is, $\xi_i$ is an already existing label, $\xi_j$,  or a new one, $\xi^{new}$, with probabilities proportional to $f(y_i\mid \tilde{\theta}_{\xi_j})$ and $\alpha f(y_i\mid \tilde{\theta}_{\xi^{new}})$, respectively,   $\xi^{new}$ is the smallest label related with no observations.
\item when $H$ is conjugate for $F$, a conjugate sampler based on algorithm 2  of \cite{neal;2000} is used. Here, the  $\xi_i$  are updated one at the time from the following conditional distribution
\begin{align*}
\xi_i\mid \boldsymbol{y}, \xi_{-i},... \sim \frac{1}{n-1+\alpha}\sum_{j \neq i}f(y_i\mid \tilde{\theta}_{\xi_j})\delta_{\xi_j} +\frac{\alpha}{n-1+\alpha}f(y_i)\delta_{\xi^{new}},
\end{align*}
where $f(y_i)$ is the prior predictive density function at $y_i$.  If a new component for $\xi_i$ is sampled, then the corresponding $\tilde{\theta}_{\xi_i}$ has to be updated. In this case $\tilde{\theta}_{\xi_i}$ is updated from its posterior distribution  based on observation $y_i$, which is conjugate.\\

Here are the conjugate cases that are implemented in NIMBLE:
\begin{itemize}
\item $F=N(\theta, \sigma^2)$, with known variance $\sigma^2$, and $H=N(\mu, \tau^2)$:
$$\theta \mid y_i \sim N\left(\frac{y_i/\sigma^2 + \mu/\tau^2}{1/\sigma^2+1/\tau^2}, \frac{1}{1/\sigma^2 +1/\tau^2} \right), \hspace{0.2cm}f(y_i)=N(y_i \mid \mu, \sigma^2+\tau^2).$$
\item $F=Poisson(\theta)$,  and $H=Gamma(a, b)$:
$$\theta \mid y_i \sim Gamma(a+y_i, b+1), \hspace{0.2cm}f(y_i)=\frac{b^a}{(b+1)^{a+y_i}}\frac{\Gamma(a+y_i)}{\Gamma(a)}\frac{1}{y_i!}.$$
\item $F=Bernoulli(\theta)$,  and $H=Beta(a, b)$:
$$\theta \mid y_i \sim Beta(a+y_i, b+1-y_i), \hspace{0.2cm}f(y_i)=\frac{\Gamma(a+y_i)\Gamma(b+1-y_i)}{(a+b)\Gamma(a)\Gamma(b)}.$$
\item $F=exp(\theta)$,  and $H=Gamma(a, b)$:
$$\theta \mid y_i \sim Gamma(a+1, b+y_i), \hspace{0.2cm}f(y_i)=\frac{ab^a}{(b+y_i)^{a+1}}.$$
\item $F=Gamma(\lambda, \theta)$, with known $\lambda$, and $H=Gamma(a, b)$:
$$\theta \mid y_i \sim Gamma(a+\lambda, b+y_i), \hspace{0.2cm}f(y_i)=\frac{y_i^{\lambda-1}b^a \Gamma(\lambda+a)}{\Gamma(\lambda)\Gamma(a)(b+y_i)^{a+\lambda}}.$$
\item $F=Multinomial(n, \boldsymbol{\theta})$,  with known $n$, and $H=Dirichlet(\boldsymbol{\alpha})$, where $\boldsymbol{\theta}=(\theta_1, \ldots, \theta_d)$,  $\boldsymbol{\alpha}=\alpha_1, \ldots, \alpha_d$: for $\by_i=(y_{i,1}, \ldots, y_{i,d})$,
$$\boldsymbol{\theta} \mid \by_i \sim Dirichelt(\boldsymbol{\alpha}+\by_i), \hspace{0.2cm}f(\by_i)=\frac{n! \Gamma(\sum_{l=1}^d\alpha_l)}{\Gamma(\sum_{l=1}^d\alpha_l+y_{i,l})}\prod_{l=1}^d\left\{ \frac{\Gamma(\alpha_l+y_{i,l})}{y_{i,l}! \Gamma(\alpha_l)}\right\}.$$
\end{itemize}

\item when $H$ is conjugate for $F$, we can define a new sampler were we can integrate out $\tilde{\theta}$ eliminating them from the algorithm. In this case, the sampler is  based on algorithm 3 of \cite{neal;2000}. Let $1, 2, \ldots, K$, be the relabeled  $K$ unique values in $(\xi_1, \ldots, \xi_n)$, let and $m_{-i,k}$ be the number of times label $k$ appears in $\xi_{-i}=(\xi_1, \ldots, \xi_{i-1}, \xi_{i+1}, \ldots, \xi_n)$.  The $\xi_i$ are updated one at the time  from the following conditional distribution
\begin{align*}
\xi_i\mid \boldsymbol{y}, \xi_{-i},... \sim \sum_{k =1 }^K\frac{m_{-i,k}}{n-1+\alpha}f(y_i\mid \by_{-i,k})\delta_{k} +\frac{\alpha}{n-1+\alpha}f(y_i)\delta_{k+1},
\end{align*}
where $\by_{-i, k}$ denotes all observations $y_l$ such that $l\neq i$ and $\xi_l=k$,  $f(y_i\mid \by_{-i, k})$ is the posterior predictive density function  at $y_i$ based on data $\by_{-i, k}$ and the prior $H$, i.e., $f(y_i\mid \by_{-i, k})=\int f(y_i\mid \theta)\prod_{\{j : j\neq i, \xi_j=k\}}f(y_j\mid \theta)H(d\theta) $,  and $f(y_i)$ is the prior predictive density function at $y_i$, i.e., $f(y_i)=\int f(y_i\mid \theta)H(d\theta)$.

For instance, considering
\begin{itemize}
\item $F=N(\theta, \sigma^2)$, with known variance $\sigma^2$, and $H=N(\mu, \tau^2)$, it follows that \\$f(y_i\mid \by_{-i, k})=N\left(y_i \mid \mu_{-i,1}, \sigma^2+ \tau^2_{-i,1} \right)$, where $\tau^2_{-i,1}=1/\left(m_{-i,k}/\sigma^2 + 1/\tau^2 \right)$,  $\mu_{-i,1}=\tau^2_{-i,1}\left(\sum_{\{j: j\neq i, \xi_j=k\}}y_j/\sigma^2 +\mu/\tau^2 \right)$,  and $f(y_i)=N(y_i \mid \mu, \sigma^2+\tau^2)$
\item $F=Poisson(\theta)$  and $\theta \sim Gamma(a,b)$, it follows that \\
$f(y_i\mid \by_{-i, k})=\frac{b_{1}^{a_{1 }}}{(b_1+1)^{a_1+y_i}}\frac{\Gamma(a_1+y_i)}{\Gamma(a_{1})}\frac{1}{y_i!}$, where $a_1=a+\sum_{\{j: j\neq i, \xi_j=k\}}y_j$, $b_1=b+m_{-i,k}$, and $f(y_i)=\frac{b^a}{(b+1)^{a+y_i}}\frac{\Gamma(a+y_i)}{\Gamma(a)}\frac{1}{y_i!}$.
\end{itemize}
\end{enumerate}

\underline{Some inefficiencies}:  1) the \textit{calculate} function is used at each step, 2) maybe we could update only few more $\tilde{\theta}$ than the unique ones, rather than the whole vector, in the random walk Metropolis step. ( 3) The $\xi$ are updated one at the time, we could update only the ones related with observations (need of varying dimensions)).

\item Sampling $\alpha$: if $H_1\equiv Gamma(a,b)$, then the following sampler can be used:
\begin{itemize}
\item sample $z\sim Beta(1+\alpha, n)$ and compute $w=\frac{a+K-1}{a+K-1+n(b-ln(z))},$ where $K$ is the number of unique values in $(\xi_1 ,\ldots, \xi_n)$.
\item sample $\alpha\sim Gama(a+K, b-ln(z))$ with probability $w$, and sample $\alpha\sim Gamma(a+K-1, b-ln(z))$ with probability $1-w$.
\end{itemize}
\end{enumerate}

\subsection{Output}
Given samples  $(\tilde{\theta}, (\xi_1, \ldots, \xi_n)$  or $\theta$,  we compute an approximation of measure $G$ based on a truncation level, say $L$, given by the user. \\
Measure $G$ is given by
$$G(\cdot)=\sum_{j=1}^Lw_j\delta_{\theta^{\star}_j}(\cdot), \hspace{0.5cm} w_1=v_1, \hspace{0.2cm}w_j=v_j\prod_{l<j}(1-v_l), l=2, \ldots, L-1, \hspace{0.2cm}w_L=\prod_{l<L}(1-v_l),$$
where $v_l\sim Beta(1, \alpha+n)$, and $$\theta^{\star}_j\sim \frac{\alpha}{\alpha+n}G_0 + \sum_{j=1}^K\frac{m_j}{\alpha+n}\delta_{\overline{\theta}_j},$$
where $K$ is the number of unique values in $(\xi_1 ,\ldots, \xi_n)$, $m_j$ denotes the number of relabeled $(\xi_1 ,\ldots, \xi_n)$ equal to $j$, and $\overline{\theta}_j$ denote the  unique values in $(\tilde{\theta}_{\xi_1}, \ldots, \tilde{\theta}_{\xi_n})$.\\

\underline{Comments}: $K$, $m_j$, and $\overline{\theta}$ can be obtained from $\theta$.\\
There are results relating $\alpha$, and acceptable error, $\epsilon$, and  the truncation level, $L$, of $G$. More specifically,  $\left(\frac{\alpha}{\alpha+1}\right)^{L-1}=\epsilon$, so we can give a warning when the truncation level is too small for the value (or samples) of $\alpha$ and $\epsilon$.

\section{Model 1}
We don't have a BUGS version for this model as is stated before. We have  BUGS code for another representation of this model that involves the random measure $G$ written as the truncation of its stick breaking representation  written as a matrix,  and integrating out the random indexes.
\subsection{The model and its BUGS Code}
\begin{minipage}{.5\textwidth}
The model:
\begin{align*}
&w_k=z_k\prod_{l<k}(1-z_l), \hspace{0.5cm} k=2, \ldots, T, w_1=z_1\\
&z_k \sim Beta(1, \alpha), \hspace{0.5cm} k=1, \ldots, T-1, z_T=1, \\
&\theta^{\star}_k  \sim N(\mu, \tau^2), \hspace{0.5cm}k=1, \ldots, T,\\
&\theta_i= \theta^{\star}_{\xi_i^{\star}},\\
&\xi_i^{\star}\mid \boldsymbol{w} \sim Categorical(\boldsymbol{w}),\\
&y_i\mid \theta_i \sim N(\theta_i, \sigma^2).\\
\end{align*}
\end{minipage}
\begin{minipage}{.5\textwidth}
BUGS code:
\begin{verbatim}
Code=nimbleCode(
  {
    G[1:T,1:2] ~ dtruncSBDPnormal(
        conc=conc0, mean=mean0, sd=tau0)
    theta[1:T] ~ dNPDiscreteV(G[1:T,1:2])
    for(i in 1:n){
      y[i] ~ dnorm(theta[i], sd=s0)
    }
    conc0 <-1;
    mean0 <- 5; tau0 <- sqrt(10);
     s0 <- sqrt(10)
})
\end{verbatim}
\end{minipage}
\vspace{0.5cm}

For this  model only the conjugate normal-normal model has been considered.

In the BUGS code, saying \texttt{G[1:T,1:2] \~{} truncSBDPnormal(conc=conc0, mean=mean0, sd=tau0)} means that
 \begin{align*}
 G(\cdot)=\sum_{k=1}^T w_k \delta_{\theta_k}(\cdot), \hspace{0.5cm}w_k=z_k\prod_{l<k}(1-z_l), \hspace{0.2cm}z_k \sim Beta(1, \alpha), \hspace{0.2cm} \theta_k  \sim N(\mu, \tau^2),
 \end{align*}
  and saying $theta[1:T] ~ dNPDiscreteV(G[1:T,1:2])$ means that
 \begin{align*}
 \theta_k\sim \sum_{j=1}^T w_j \delta_{m_j},
 \end{align*}
 where $w_j$ are weights that add up to one, and $m_j$ are possible values for $\theta_k$.

Note that lines 1 to 3 in the model correspond to \texttt{G[1:T,1:2] \~{} dtruncSBDPnormal(conc=conc0, mean=mean0, sd=tau0) } in the BUGS code, and lines 4 and 5 are related  to \texttt{theta[1:T] \~{} dNPDiscreteV(G[1:T,1:2])} in the BUGS code.

\subsection{Sampler}
The sampler that we have is the blocked Gibbs sampler and samples the random measure $G$ as a matrix whose first and second columns are the vector of $\theta^{\star}$ and $w$, respectively. The steps of the sampler are the following:
\begin{itemize}
\item[a)] updating $(\theta^{\star}_1, \ldots, \theta^{\star}_T)$: $\theta^{\star}_k$ is updated from the prior if it is no related with any observation. Otherwise, $\theta^{\star}_k$ is updated from the posterior (which is conjugate in this case) considering the observations that have label $k$.
\item[b)] updating the labels $\xi^{\star}$:
\begin{align*}
\xi^{\star}_i\mid \boldsymbol{y}, ...\sim \sum_{k=1}^T w_kN(y_i\mid \theta^{\star}_k, \sigma^2)\delta_k.
\end{align*}
\item[c)] updating the weights $(w_1, \ldots, w_T)$: first the stick variables are updated as \begin{align*}
z_k\mid \boldsymbol{y}, ... \sim Beta \left(1 + M_k, \alpha+\sum_{l=k+1}^TM_l\right),
\end{align*}
where $M_k$ denotes the number of observations that  have label $k$. Then $w_k=v_k\prod_{l<k}(1-z_l)$.
\end{itemize}

Several changes: 1) $\theta^{\star}$ could be updated using NIMBLE samplers, 2) I need to check the conjugacy between $z_k$, $w_k$ and $\xi^{\star}$ (Dirichlet-categorical), 3) if there is conjugacy, then use the NIMBLE sampler too, and 4) create a sampler only for the $\xi^{\star}$.

\bibliographystyle{biometrika}
\bibliography{ref}
\end{document}


\XtoCBlock{SinGen}
\label{block:SinGen}
\begin{figure}[H]\includegraphics{SinGen}\end{figure}

\begin{XtoCtabular}{Inports}
A & Amplitude\tabularnewline
\hline
f & Frequency\tabularnewline
\hline
\end{XtoCtabular}

\begin{XtoCtabular}{Outports}
u & Sine wave output\tabularnewline
\hline
\end{XtoCtabular}

\begin{XtoCtabular}{Mask Parameters}
fmax & Maximum Frequency in Hz\tabularnewline
\hline
Offset & Offset\tabularnewline
\hline
Phase & Phase [-Pi..Pi]\tabularnewline
\hline
ts\_fact & Multiplication factor of base sampling time (in integer format)\tabularnewline
\hline
\end{XtoCtabular}

\subsubsection*{Description:}
Generation of a sine wave with amplitude (A) and frequency (f).

% include optional documentation file
\InputIfFileExists{\XcHomePath/Library/General/Doc/SinGen_Info.tex}{\vspace{1ex}}{}

\subsubsection*{Implementations:}
\begin{tabular}{l l}
\textbf{FiP8} & 8 Bit Fixed Point Implementation\tabularnewline
\textbf{FiP16} & 16 Bit Fixed Point Implementation\tabularnewline
\textbf{FiP32} & 32 Bit Fixed Point Implementation\tabularnewline
\textbf{Float32} & 32 Bit Floating Point Implementation\tabularnewline
\textbf{Float64} & 64 Bit Floating Point Implementation\tabularnewline
\end{tabular}

\XtoCImplementation{FiP8}
\index{Block ID!416}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP8 \tabularnewline
\textbf{ID} & 416 \tabularnewline
\textbf{Revision} & 1.0 \tabularnewline
\textbf{C filename} & SinGen\_FiP8.c \tabularnewline
\textbf{H filename} & SinGen\_FiP8.h \tabularnewline
\end{tabular}
\vspace{1ex}

8 Bit Fixed Point Implementation

\begin{XtoCtabular}{Controller Parameters}
delta\_phi & Angle increment\tabularnewline
\hline
phase & Angle offset\tabularnewline
\hline
offset & Amplitude offset\tabularnewline
\hline
phi & Current angle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int8          *A;
     int8          *f;
     int8          u;
     int8          delta_phi;
     int8          phase;
     int8          offset;
     int8          phi;
} SINGEN_FIP8;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/General/Doc/Test_SinGen_FiP8.tex}{}{}
\fi
\XtoCImplementation{FiP16}
\index{Block ID!417}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP16 \tabularnewline
\textbf{ID} & 417 \tabularnewline
\textbf{Revision} & 1.0 \tabularnewline
\textbf{C filename} & SinGen\_FiP16.c \tabularnewline
\textbf{H filename} & SinGen\_FiP16.h \tabularnewline
\end{tabular}
\vspace{1ex}

16 Bit Fixed Point Implementation

\begin{XtoCtabular}{Controller Parameters}
delta\_phi & Angle increment\tabularnewline
\hline
phase & Angle offset\tabularnewline
\hline
offset & Amplitude offset\tabularnewline
\hline
phi & Current angle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int16         *A;
     int16         *f;
     int16         u;
     int16         delta_phi;
     int16         phase;
     int16         offset;
     int16         phi;
} SINGEN_FIP16;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/General/Doc/Test_SinGen_FiP16.tex}{}{}
\fi
\XtoCImplementation{FiP32}
\index{Block ID!418}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP32 \tabularnewline
\textbf{ID} & 418 \tabularnewline
\textbf{Revision} & 1.0 \tabularnewline
\textbf{C filename} & SinGen\_FiP32.c \tabularnewline
\textbf{H filename} & SinGen\_FiP32.h \tabularnewline
\end{tabular}
\vspace{1ex}

32 Bit Fixed Point Implementation

\begin{XtoCtabular}{Controller Parameters}
delta\_phi & Angle increment\tabularnewline
\hline
phase & Angle offset\tabularnewline
\hline
offset & Amplitude offset\tabularnewline
\hline
phi & Current angle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int32         *A;
     int32         *f;
     int32         u;
     int32         delta_phi;
     int32         phase;
     int32         offset;
     int32         phi;
} SINGEN_FIP32;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/General/Doc/Test_SinGen_FiP32.tex}{}{}
\fi
\XtoCImplementation{Float32}
\index{Block ID!419}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & Float32 \tabularnewline
\textbf{ID} & 419 \tabularnewline
\textbf{Revision} & 0.1 \tabularnewline
\textbf{C filename} & SinGen\_Float32.c \tabularnewline
\textbf{H filename} & SinGen\_Float32.h \tabularnewline
\end{tabular}
\vspace{1ex}

32 Bit Floating Point Implementation

\begin{XtoCtabular}{Controller Parameters}
delta\_phi & Angle increment\tabularnewline
\hline
phase & Angle offset\tabularnewline
\hline
offset & Amplitude offset\tabularnewline
\hline
phi & Current angle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     float32       *A;
     float32       *f;
     float32       u;
     float32       delta_phi;
     float32       phase;
     float32       offset;
     float32       phi;
} SINGEN_FLOAT32;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/General/Doc/Test_SinGen_Float32.tex}{}{}
\fi
\XtoCImplementation{Float64}
\index{Block ID!420}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & Float64 \tabularnewline
\textbf{ID} & 420 \tabularnewline
\textbf{Revision} & 0.1 \tabularnewline
\textbf{C filename} & SinGen\_Float64.c \tabularnewline
\textbf{H filename} & SinGen\_Float64.h \tabularnewline
\end{tabular}
\vspace{1ex}

64 Bit Floating Point Implementation

\begin{XtoCtabular}{Controller Parameters}
delta\_phi & Angle increment\tabularnewline
\hline
phase & Angle offset\tabularnewline
\hline
offset & Amplitude offset\tabularnewline
\hline
phi & Current angle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     float64       *A;
     float64       *f;
     float64       u;
     float64       delta_phi;
     float64       phase;
     float64       offset;
     float64       phi;
} SINGEN_FLOAT64;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/General/Doc/Test_SinGen_Float64.tex}{}{}
\fi


\chapter{Anomaly Detection}
\label{cha:anomaly_detection}
\epigraph{
  The field of anomaly detection is very broad and many different fields have
  developed a great variation of different methods for finding outliers.  This
  work is focusing on detecting anomalies in sequences by predicting the
  expected evolution of a time series and comparing this prediction to real
  observations (or simply: the truth).  Before describing this process in more
  detail, this chapter defines the different kinds of anomalies that exist in
  sequences and briefly summarizes two other, common detection methods
  for sequence outlier detection.
}

\section{Anomalies}
\label{sec:anomalies}
An anomaly (or outlier) refers to a pattern in a dataset that does not match
an expected behaviour.
In time series, there are two basic types of anomalies:
\begin{enumerate}

  \item \emph{Simple anomalies}, describe instances that can be considered as
    an outlier only with respect to their value. This is the most basic kind of
    anomaly which can easily be caught by ordinary, statistical, range-based
    detection algorithms. The anomaly in Fig.~\ref{fig:intro_point_anomaly}
    consists of a single instance and is hence called a \emph{point} anomaly.
    \begin{figure}
      \centering
      \includegraphics[width=\linewidth]{intro_point_anomaly.pdf}
      \caption{The simple point anomaly can easily be detected by appropriate
      thresholds (green lines).}
      \label{fig:intro_point_anomaly}
    \end{figure}

  \item \emph{Contextual anomalies} are patterns that are only anomalous within
    a certain context, but not otherwise. In time series, the context is
    provided by two attributes: The \emph{contextual attribute} is typically
    time itself, while the \emph{behavioural attributes} describe the actual
    values of the examined dataset. Fig.~\ref{fig:intro_context_anomaly} shows
    a contextual anomaly consisting of several abnormal points, which is
    referred to as a \emph{discord} or \emph{subsequence} anomaly.
    \begin{figure}
      \centering
      \includegraphics[width=\linewidth]{intro_context_anomaly.pdf}
      \caption{Contextual anomalies are not detectable with simple statistical
      methods without causing a large number of false positives.}
      \label{fig:intro_context_anomaly}
    \end{figure}
\end{enumerate}

\newpage
\section{Techniques}
\label{sec:techniques}

The difficulty of anomaly detection stems from the fact that the patterns that
are searched for are typically not known.  Different fields have come up with a
large number of different approaches for the detection of the different kinds
of anomalies. Two such approaches  are sketched below: \emph{proximity-based}
techniques, and {\em information theoretical} approaches [\cite{AggarwalCharuC2017}].

\subsection{Proximity-based Anomaly Detection}
\label{sub:proximity_based_anomaly_detection}

By applying certain transformations to segments of a time series, a segment can be
mapped into a multidimensional vector space. The proximity can then be
calculated for example with the Euclidean distance, which opens up the whole
range of proximity-based outlier detection methods, such as cluster, distance,
and density based techniques.

The trivial example of such a transformation is to just consider segments of
length $n$ as a vector of length $n$.  Other approaches include \emph{Discrete
Fourier Transforms} (DFT) or \emph{Discrete Wavelet Transforms} (DWT), the
simplest of which is the \emph{Haar Wavelet}. A Haar Wavelet decomposition is
sketched in Fig.~\ref{fig:haar_wavelets}, a more detailed description of how
wavelet transformations work is given in [\cite{AggarwalCharuC2017}]. The
advantage of transforming a sequence into a vector of wavelet coefficients is
that the coefficients directly represent short-term and long-term dependencies.
This enables a reduction of the dimensionality of the space that has to be
analyzed depending on the nature of the problem. For example, frequencies above
a certain threshold can be ignored.  The application of proximity-based methods
naturally becomes much more effective on the transformed sequences. Depending
on the nature of the dataset different transformations are more effective.  For
sequences with dominant periodic parts the DFT works better, while series with
discontinuities are well represented by the Haar-DWT.

\begin{figure}
  \centering
  \includegraphics[width=0.9\linewidth]{haar_wavelets.png}
  \caption{Haar wavelet decomposition of series of length $N=4$.
  [\cite{AggarwalCharuC2017}]}
  \label{fig:haar_wavelets}
\end{figure}

\subsection{Information Theoretical Approaches}
\label{sub:information_theoretical_approaches}

Information theoretical models rely on the creation of so-called
\emph{summaries} of a dataset. The length of the summary is shorter the simpler
the sequence is.  A completely periodic sequence can be described very
concisely.  Sequences that contain anomalies require a longer description,
hence the summary becomes longer. An anomaly is defined as a point or
subsequence that leads to a large increase of the length of the summary.  In
practice, measures such as entropy can take the part of the summary length. The
entropy of a sequence $X$ is defined by:

\begin{equation}
  H(X) = - \sum_{x \in X} P(x) \log P(x),
\end{equation}

where $P(x)$ is the probability of a value $x$.

\subsection{Prediction}
\label{sub:prediction}

The approach that is used in this thesis relies on modelling the normal
behaviour of the given dataset and creating predictions. The predictions can
then be compared to the actual values of the series, essentially reducing the
problem to a \emph{simple anomaly}, which can be detected by a simple threshold
on the error.\\
Considering a time series of length $T$, the single input \emph{frames} of a
series will further be denoted by $\vt{u}$ with $t \in [0,T]$.  An input frame
contains all the features at time $t$ and is represented by a vector with $m$
components, which could be the pixel values of a flattened image.  The same
holds for the target frames $\vt{d}$, which hold the desired output at every
time step. The output of the forecasting algorithm is called the prediction,
denoted by $\vt{y}$, and ought to be as close as possible to the target
$\vt{d}$.  Prediction and target frames can have a different size from the
input frames:

\begin{align}
  \vt{u} &= (u^t_1, u^t_2, ..., u^t_m)^T, \nonumber\\
  \vt{d} &= (d^t_1, d^t_2, ..., d^t_k)^T. \\
  \vt{y} &= (y^t_1, y^t_2, ..., y^t_k)^T. \nonumber
\end{align}

By defining an input sequence $\textbf{u}$ as a sequence of vectors

\begin{equation}
  \textbf{u} := (\vec{u}_0, \vec{u}_1, \vec{u}_2, ..., \vec{u}_M)
\end{equation}

the prediction problem can be formulated by finding a function $F$, that
returns a good estimate $\textbf{y}$ of the next $N$ true vectors $\textbf{d}
:= (\vec{u}_{M+1}, ..., \vec{u}_{M+N})$.

\begin{equation}
  \textbf{y} = (\vec{y}_{M+1}, ..., \vec{y}_{M+N})
             = F(\vec{u}_0, \vec{u}_1, ..., \vec{u}_M)
             = F(\textbf{u})
\end{equation}

The approximation of the function $F$ is a regression problem which has
been studied intensively throughout history [\cite{narx_prediction}].
Recently, Neural Networks (NN) have been applied to anomaly detection, as they
can model certain non-linear sequences without a priori knowledge about the
data, just by applying a learning algorithm. An in depth explanation of how NNs
can be applied to find $F$ for spatio-temporal datasets is given in
Chapter~\ref{cha:neural_networks}.  Before 1980, time series were typically
predicted by using autoregressive, moving-average models (ARMA), which where
introduced by [\cite{boxjenkins}].  Another method published by
[\cite{winters1960forecasting}] on forecasting sales improved Holt`s double
exponential smoothing.  This method later became known as the Holt-Winters
method.  Both approaches are described in in the next paragraphs.  It should be
noted though that they are both linear regression models, which makes them
incapable of predicting non-linear time series. For the sake of simplicity they
are described only for the case of scalar time series.\\

During the regression (or training) phase of the forecasting algorithm the
target for the prediction is typically the true observation of the next time
step $\vt{d} = \vec{u}_{t+1}$.  The parameters of the algorithm are tuned until
the predictions $\vt{y}$ are good enough.  As soon as the `one step ahead'
prediction problem is solved, predictions further into the future can be made
by feeding $\vt{y}$ back to the algorithm as the next input $\vec{u}_{t+1}$. In
this case the algorithm is in the forecasting phase and is not optimized
any more. This prevents information about the next frame to leak into the
prediction process.

\begin{figure}
  \centering
  \includegraphics[width=\linewidth]{train_predict_mode.pdf}
  \caption{Input $\vt{u}$, target $\vt{d}$, and prediction $\vt{y}$ during the
  regression phase (left of the black line) and during the prediction phase.
  In the prediction phase the algorithm has no access to the true target
  any more, which is why they are depicted by a dashed line.}
  \label{fig:train_predict_mode}
\end{figure}

\subsubsection{Auto-regressive Integrated Moving Average}
\label{sub:arma}

Auto-regressive integrated moving average (ARIMA) models are widely used in
forecasting and are typically applied to non-stationary time series.  A
non-seasonal ARIMA model is defined by three parameters $p$, $q$, and $d$.  The
first parameter $p$ is the order of the auto-regressive (AR) model, $q$ is the
order of the moving-average (MA) model, and $d$ is the number of non-seasonal
differences that are needed to obtain a stationary time series.  This means
that $Y_t$ is the result of applying the sequence difference operator $\Delta
y_t = y_{t+1}-y_{t}$ for $d$ times to $y_t$:
\begin{equation}
  Y_t = \Delta^d y_t = \sum_{k=0}^d (-1)^{d-k} \binom{d}{k} y_{t+k}
\end{equation}

With the difference $Y_t$ we can write the general forcasting equation of the
ARIMA model:
\begin{equation}
  y_t = \mu + \sum_{i=1}^p \varphi_i Y_{t-i} + \sum_{i=1}^q \theta_i \epsilon_{t-i}
\end{equation}

where $\mu$ is the mean of the series, $\varphi_i$ are the $p$ parameters of
the AR model, $\theta_i$ the parameters of the MA model, and $\epsilon_i$ white
noise error terms.  The parameters of the MA and AR models have to be fit to
the data and the order of the difference must chosen a priori.

\subsubsection{Holt-Winters Method}
\label{sub:holt_winters_method}

The Holt-Winters method belongs to the class of exponential smoothing
algorithms.  In contrast to MA models that apply the same weights over a window
of a time series, exponential smoothing uses exponentially decaying weights
back in time.

The most basic form is called simple exponential smoothing (SES), where the
level of previous points provides an estimate for the next time step.  The
method maintains an estimated point $\vt{y}$, which is calculated based on
previous points and estimates.  They are assigned weights, which decrease
exponentially going back in time.
\begin{align}
  y_t = \alpha d_t + (1-\alpha)y_{t-1}
\end{align}
The smoothing parameter $\alpha$ determines exponentially
decreasing weights that are applied to previous data points.  The smoothing
parameter is chosen such that the mean squared error of prediction and data
point is minimized.

The Holt-Winters method extends the SES approach from only forecasting the level
to smoothing equations for trend $b_t$ and seasonal $s_t$.
It is described by the following four equations:
\begin{align}
  l_t &= \alpha(d_t - s_{t-L}) + (1-\alpha)(y_{t-1} + b_{t-1}) &\text{level} \\
  b_t &= \beta(l_t - l_{t-1}) + (1-\beta)b_{t-1} &\text{trend} \\
  s_t &= \gamma(y_t - l_t) + (1 - \gamma)s_{t-L} &\text{seasonal} \\
  y_{t+m} &= l_t + m b_t + s_{t-L+1+(m-1) \bmod L} &\text{forecast}
\end{align}

where $L$ is the length of the seasonal component, $\alpha$, $\beta$, $\gamma$
are constants that need to be fit to the data and $m$ denotes how far into
the future the prediction goes.

\subsection{Anomaly Score}%
\label{sub:anomaly_score}

\begin{figure}
  \centering
  \includegraphics[width=\linewidth]{anomaly_score.pdf}
  \caption{With a good prediction $y$, the problem can be reduced to a simple
    anomaly detection over the error sequence $E$ (upper plot). The error
    sequence still takes on arbitrary values and can be converted into a
    probability of normality with an anomaly score (lower plot).
    The normality is calculated with sliding windows of length 100 for $\mu_E$
    and a length of 5 for $\mu_e$.
  }
  \label{fig:anomaly_score}
\end{figure}

With the predicted sequence $\textbf{y}$ it becomes quite simple to detect an
outlier, because the problem can be reduced to a \emph{simple anomaly} by
calculating the absolute error sequence $E$.

\begin{equation}
  \label{eq:err_seq}
  E_i = || \vt{y} - \vt{d} ||_2
\end{equation}

The error can now be thresholded to detect an anomaly, but the value of this
threshold depends on the specific dataset that is being analyzed.  To obtain a
probability for how anomalous a point or subsequence is, an \emph{anomaly
score} $\Sigma$ is defined as suggested by [\cite{numenta_realtime}]:

\begin{equation}
  \label{eq:normality_score}
  \Sigma = 1 - \text{erf}\bigg(\frac{\mu_e - \mu_E}{\sqrt{2}\sigma_E}\bigg),
\end{equation}

where $\mu_E$ and $\sigma_E$ denote the mean and standard deviation of a window
of length $N$ of the error sequence $E$. The mean $\mu_e$ is calculated from a
smaller subsequence of $E$ of length $n \ll N$. If $\mu_e$ is close to $\mu_E$
then $\Sigma$ will be close to one, and can thus be considered \emph{normal}
with a high probability. If $\mu_e$ and $\mu_E$ are far apart, the probability
of normality is low and the point becomes more likely to be an
outlier. This results in a score for the whole sequence, which can be used as
an intuitive threshold according to the problem at hand.
Fig.~\ref{fig:anomaly_score} shows how the anomaly detection problem of a
contextual outlier is first reduced to a simple anomaly by calculating $E$ and
then detected with a threshold on $\Sigma$.

If the quantities $\vt{y}$ and $\vt{d}$ are vectors or matrices it might be
desirable to know which components in the vector (matrix) caused the anomaly.
For such a spatially resolved anomaly score the norm from Eq.~\ref{eq:err_seq}
can be removed and $\Sigma$ is calculated component-wise for every
coefficient in $|\vt{y} - \vt{d}|$.


\begin{frame}[fragile]{Lineage specific evolution}
   \begin{columns}[T,onlytextwidth]
    \column{0.5\textwidth}
    \begin{figure}
		\includegraphics[width=0.77\textwidth]{images/monkey-model.png}
        \caption{Modeling full phylogeny as one component}
	\end{figure}
    \column{0.5\textwidth}
    \uncover<2>{\begin{figure}
		\includegraphics[width=\textwidth]{images/csmet-model.png}
        \caption{Modeling full phylogeny as one component}
	\end{figure}}
    \end{columns}
\end{frame}

\begin{frame}[fragile]{Ornstein-Uhlenbeck Models: Whole TFBS evolving as a unit}
\begin{itemize}
\item HB models neglects lineage or specie specific selection
\item OU models this gap by accounting for lineage/specie specific selection by requiring regime specific optima to be obtained
\item OU models can account for whole element substituion by defining a quantitative trait as a score attached to the TFBS $X(t)$
\item $X(t)$ evolves by two components one deterministic, other stochastic (BM)
\end{itemize}
\begin{align*}
dX(t) &= \alpha(\theta-X(t)) + \sigma dB(t)\\
\alpha & = \text{Strength of selection}\\
\theta - X(t) & = \text{Distance from optimum value}\\
\sigma &= \text{strength of random drift}\\
dB(t) &= \text{random white noise}
\end{align*}
\end{frame}

\section{Brownian Motion and Evolution}

\begin{frame}[fragile]{Ornstein-Uhlenbeck Models: Whole TFBS evolving as a unit}
   \begin{columns}[T,onlytextwidth]
    \column{0.3\textwidth}
	\begin{figure}
    \begin{tikzpicture}[scale=0.6]
    \tikzstyle{operator} = [draw,fill=white,minimum size=1em]
        \draw[->] (0,-2) -- (6,-2);
        \draw[line width=1mm] (0,0) -- (1,0);
        \draw[line width=1mm] (1,0) -- (2,1);
        \draw[line width=1mm] (2,1) -- (5,1);
        \draw[line width=1mm] (1,0) -- (2,-1);
        \draw[line width=1mm] (2,-1) -- (5,-1);
        \draw[dashed] (1,0) -- (1,-2);
        \node at (1,-2.4) {$s$};
        \node at (3,-2.4) {Time};
        \node at (5,-2.4) {$T$};
        \node at (5.3,1) {$s_1$};
        \node at (5.3,-1) {$s_2$};
    \end{tikzpicture}
\caption{$s_1$, $s_2$ -- BM}
\end{figure}

	\column{0.7\textwidth}
    \begin{align*}
E[\mathbf{X}(t)] &= \begin{pmatrix}
\theta_0\\
\theta_1
\end{pmatrix}\\
 \texorpdfstring{{\Sigma}} &= \sigma^2\begin{pmatrix}
T & s\\
s & T
\end{pmatrix}\\
\end{align*}
  \end{columns}
  \begin{columns}[T,onlytextwidth]
    \column{0.3\textwidth}
    \begin{figure}
    \begin{tikzpicture}[scale=0.6]
    \tikzstyle{operator} = [draw,fill=white,minimum size=1em]
        \draw[->] (0,-2) -- (6,-2);
        \draw[line width=1mm] (0,0) -- (1,0);
        \draw[line width=1mm] (1,0) -- (2,1);
        \draw[line width=1mm] (2,1) -- (5,1);
        \draw (1,0) -- (2,-1);
        \draw (2,-1) -- (5,-1);
        \draw[dashed] (1,0) -- (1,-2);
        \node at (1,-2.4) {$s$};
        \node at (3,-2.4) {Time};
        \node at (5,-2.4) {$T$};
        \node at (5.3,1) {$s_1$};
        \node at (5.3,-1) {$s_2$};
    \end{tikzpicture}
    \caption{$s_2$ -- new optimum regime, $s_1$ -- ancestral}
\end{figure}
    \column{0.7\textwidth}
    \begin{align*}
      E[X_1(T)] &= \theta_0e^{-\alpha T}+ \theta_1(1-e^{-\alpha T})\\
      E[X_2(T)] &= \theta_0e^{-\alpha T} + \theta_1e^{-\alpha(T-s)}(1-e^{-\alpha s})\\
      &+ \theta_2(1-e^{-\alpha(T-s)})\
	\end{align*}
  \end{columns}
\end{frame}