\section{Why PID is so generally applicable}
\subsection{}

\begin{frame}
\frametitleTC{The second-order process case}
\framesubtitleTC{aka ``one size fits many''}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item We consider an asymptotically  process with two poles and one zero at most, i.e.,
       \begin{displaymath}
        P(z) = \mu \frac{z-z_1}{(z-p_1)(z-p_2)}, \qquad |p_{1,2}|<1.
       \end{displaymath}
 \item We want to show that this gives rise to the four possible cases
       \begin{displaymath}
        \begin{array}{rcll}
         P_1(z) &=& \frac{\mu}{z-p_1},                                               \\
         P_2(z) &=& \frac{\mu}{(z-p_1)(z-p_2)},                                      \\
         P_3(z) &=& \frac{\mu(z-z_1)}{(z-p_1)(z-p_2)}, & |z_1|<1,                    \\
         P_4(z) &=& \frac{\mu(z-z_1)}{(z-p_1)(z-p_2)}, & |z_1|>1 \text{ or } z_1=-1, \\
        \end{array}
       \end{displaymath}
       and that each of these is very naturally paired to a PI(D) controller.
 \item But: why not a case with one pole and one zero? Why not $z_1=1$?
 \item We need another short \emph{intermezzo}...
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Why not $z_1=1$}
\framesubtitleTC{}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item We start from the simple. If $z_1=1$ the process output $y$ tends asymptotically to zero
       for any constant input $u$, as it comes to depend on $u(k)-u(k-1)$.
 \item This means that to keep $y$ at a certain constant reference value, $u$ should indefinitely
       increase at a corresponding constant \TC{rate}, up to infinity---or overflow.
       Apparently, such a process cannot be managed (control people use to say ``you cannot prescribe
       the steady state'').
 \item If conversely $z_1=-1$ one can prescribe the steady state, but not cancel the zero with a
       pole of $C$, or the cancellation would be critical, as we already know.
 \item We thus consider $z_1=-1$ and $|z_1|>1$ the same case, and we know\\
       what the outcome will be.
 \item More interesting is to discuss why we assume $P$ to be always strictly\\
       proper (more poles than zeroes).
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{A controller's execution timeline}
\framesubtitleTC{Some considerations}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Let us first point a further peculiarity of control in computers.
       \begin{itemize}[<+-| alert@+>]
       \item When the controlled object is outside the computer, it always evolves in \TC{physical}
             parallel with the controller's execution; when it is inside, that parallel might be physical
             or just emulated, if the two entities share a CPU. Said otherwise, \TC{running the controller
             can halt the process}.
       \item More in general, in non-computer applications, running controllers adds value to the process
             by improving its operation. In computers, on the contrary, time to compute the control signals
             is \TC{anyway stolen} from that devoted to applications. And since value ultimately comes from
             running applications, time for running controllers must be almost negligible.
       \end{itemize}
 \item For the sake of completeness, the second statement may not hold\\
       if the control payback is really huge, or if controllers have dedicated\\
       hardware to run; we leave discussing such cases to advanced\\
       activities, however.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{A controller's execution timeline}
\framesubtitleTC{Some considerations}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item In general -- i.e., considering also cases we do not address in our activity -- one can distinguish
       three cases (thinking for simplicity of a sampled signals context):
       \begin{itemize}[<+-| alert@+>]
       \item the time $\tau(k)$ to compute the generic $u(k)$ is negligible wrt the sampling period $T_s$;
       \item $\tau(k)$ is not negligible wrt $T_s$ but it is -- almost -- invariant (no branches, no operations
             with operand-dependent duration, no or negligible cache effects, and so on---or countermeasures taken
             in the code for such issues);
       \item $\tau(k)$ is not negligible wrt $T_s$ and can vary significantly over the control steps.
       \end{itemize}
 \item We do not have the time to investigate the matter (but those who want to deal\\
       safely with high-performance real-time control \TC{should} study it very\\
       carefully in control technology courses).
 \item In fact a PI(D) algorithm can be made very lightweight (down to a\\
       few tens of clock cycles, to give a figure) so that we are practically\\
       always in the first case.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{A controller's execution timeline}
\framesubtitleTC{Some considerations}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item The timeline is therefore as follows:
       \begin{center}
        \vspace{1mm}\includegraphics[width=0.70\columnwidth]{./Unit-07/img/CtrlTimeline.pdf}
       \end{center}
 \item As can be seen, the effect of $u(k)$ can be seen only at step $k+1$,\\
       hence the process is correctly viewed as a strictly proper model.
 \item Incidentally, being in the first case is essential to make the P action\\
       possible.
 \item ...end of the \emph{intermezzo}.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Back to the second-order process}
\framesubtitleTC{for a reasoned recap and a systematisation}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Exercise: take the four cases
       \begin{displaymath}
        \begin{array}{rcll}
         P_1(z) &=& \frac{\mu}{z-p_1},                                               \\
         P_2(z) &=& \frac{\mu}{(z-p_1)(z-p_2)},                                      \\
         P_3(z) &=& \frac{\mu(z-z_1)}{(z-p_1)(z-p_2)}, & |z_1|<1,                    \\
         P_4(z) &=& \frac{\mu(z-z_1)}{(z-p_1)(z-p_2)}, & |z_1|>1 \text{ or } z_1=-1. \\
        \end{array}
       \end{displaymath}
 \item For each of them select a controller structure, devise a way to provide\\
       a response speed specification, and formalise a tuning procedure;\\
       then build a Modelica LTI scheme to simulate the so obtained\\
       loops, design and carry out experiments, and comment.
 \end{itemize}
\end{frame}


\section{Discussion} \label{sec:discuss}
\subsection{Impact of Model Priors}
The most significant limitation of the {\sc PROVABGS} SED modeling in
inferring the true galaxy properties is the prior on galaxy properties imposed
by the model.
The effect of such priors is  a major limitation for any SED modeling
methods~\citep[\emph{e.g.}][]{carnall2017, leja2019} and is a consequence of
the fact that galaxy properties are \emph{not} parameters of the SPS model.
For instance, $\avgsfr$, $\zmw$, and $\tage$ are derived by integrating the SFH
and ZH (Eq.~\ref{eq:prop_eqs}), which are parameterized by $\beta_1, \beta_2,
\beta_3, \beta_4$ and $\gamma_1, \gamma_2$.
Uniform priors on $\beta$s and $\gamma$s (Section~\ref{sec:infer} and
Table~\ref{tab:params}) do not translate into uniform priors on $\avgsfr$,
$\zmw$ and $\tage$.
Other galaxy properties (\emph{e.g.}~SFH, and ZH) likewise have
non-uniform, and undesireable, priors.

One way to address this issue is to choose an SED model parameterization that
does not impose extreme priors on galaxy properties and to characterize the
priors in detail so that final posteriors can be appropriately interpreted.
For the {\sc PROVABGS} model, we explicitly chose our SFH prescription so that
the prior on $\log\avgssfr$ extends the range $-12$ to $-9$ dex.
Furthermore, we fully characterize the prior on $\avgssfr$, $\zmw$, $\tage$,
SFH, and ZH in Appendix~\ref{sec:model_priors} (Figures~\ref{fig:model_prior}
and~\ref{fig:sfh_prior}).
This way, we understand exactly how the model prior impacts the derived
posteriors as we discuss in detail in Section~\ref{sec:results}.
Beyond mitigating the effect of the priors, we can impose uniform prior (or any
other desired prior distribution) on the derived galaxy properties by adjusting
the priors on the SED model parameters.
\cite{handley2019} recently demonstrated that maximum-entropy priors can be
used for this purpose to impose uniform priors on the inferred sum of neutrino
masses in cosmological analyses.
In an upcoming paper, Hahn (in prep.), I will demonstrate that maximum-entropy
priors can also be used in Bayesian SED modeling to correct for the impact of
priors on infer posteriors on derived galaxy properties.

%With this prior correction, we will be able to infer even more accurate posteriors on the physical properties of galaxies with our {\sc PROVABGS} SED modeling.

%However, we can go beyond minimizing the impact of the priors and use maximum-entropy.  With an estimate of the prior distribution, we can impose maximum-entropy priors in a specified distribution~\citep{handley2019}.  From an estimate of the prior distribution on the galaxy properties, we can derive a new prior on the SPS model parameters that would impose uniform priors on the galaxy properties.

\subsection{Aperture Effects}
In this work, we use forward modeled mock observations to demonstrate that we
can infer accurate and precise posteriors on certain galaxy properties.
The mock observations are constructed from \lgal~and include photometry and
spectra.
In generating the mock spectra, we model the fiber aperture effect ---
\emph{i.e.} spectra only include light from a galaxy collected within its fiber
diameter --- by scaling the SED flux (Section~\ref{sec:spec}).
In our SED modeling, we account for this fiber aperture effect using a
normalization factor, $f_{\rm fiber}$ (Section~\ref{sec:sps}).
Hence, our mock observations and SED modeling have a consistent treatment of
the fiber aperture effect.
In observations, however, aperture effects can be wavelength
dependent~\citep{gerssen2012, richards2016} and if the dependence is strong,
an overall $f_{\rm fiber}$ factor would not be sufficient.
In order to examine the wavelength dependence, we compare the ratio of the
fiber aperture flux over total flux, $f^{\rm fiber}_X/f_X$, in $g$, $r$, and
$z$ bands of BGS targets from LS.
We find find no significant difference in the flux ratios of the different
bands, which suggests that the fiber aperture effect in DESI does not have a
strong wavelength dependence.

Flux calibration performed on DESI spectra can also induce wavelength dependent
residuals.
DESI spectra are measured using three-arm spectrographs that split the spectra
into three $b$, $r$, and $z$ channels with overlapping wavelength ranges:
$3600 - 5930$, $5660 - 7720$, and $7470 - 9800 \AA$.
After flat fielding and sky subtraction, flux calibration is performed on each
channel of the spectra by matching physical stellar models to spectra of
spectrohotometric standard stars observed in the same exposure
(Guy~\etal~in prep.).
Since the calibration is performed for each channel seaparately, imperfections
can imprint a wavelength dependent residual.
In a subsequent paper, Ramos \etal~(in prep.), we examine the fiber aperture
effect and wavelength dependent imprints on DESI spectra using BGS
spectra from the DESI Survey Validation data and observations from the Mapping
Nearby Galaxies at APO (MaNGA) survey.
Using galaxy properties derived using the {\sc PROVABGS} pipeline for spectra
from integrated field unit MaNGA observations, we will present aperture
corrections that can be applied on derived BGS galaxy properties.
We also note that the {\sc PROVABGS} SED modeling pipeline already includes flux
calibration models beyond a single $f_{\rm fiber}$ and can easily be extended
to include more sophisticated models~\citep[\emph{e.g.} Chebyschev
polynomial;][]{carnall2017, tacchella2021}.

%\todo{paragraph on how we handle theoretical assumptions --- isochrones and stellar libraries, summary of the appendix}
\subsection{Stellar Model Choices}
In both our {\sc PROVABGS} SED model and mock observations, we use the MIST
isochrones,
\edits{
    the combined MILES+BaSeL spectral library,
}
and the \cite{chabrier2003} IMF.
With the same set of choices, our analysis does not consider how different
choices for stellar evolution or IMF can affect the inferred galaxy properties.
Yet, it is well-established that there are major uncertainties in each of these
choices~\citep{conroy2009, conroy2013}.
For instance, recent observational works suggest that there may be significant
variations in IMF~\citep[\emph{e.g.}][]{treu2010, vandokkum2010, rosani2018,
sonnenfeld2019}.
Different SPS model choices can also significantly impact the derived galaxy
propeties~\citep[\emph{e.g.}][]{ge2019}.
We reserve a detailed examination of this effect for future work.
In the meantime, for the {\sc PROVABGS} catalog we will release multiple
catalogs each with different sets of choices for isochrone, spectral library,
and IMF.

\subsection{Advantages of PROVABGS}
We demonstrate with the mock challenge that we can derive accurate and precise
constraints on specific galaxy properties using the {\sc PROVABGS} SED modeling.
The {\sc PROVABGS} catalog will have a number of key advantages over other
value-added galaxy catalogs.
First, {\sc PROVABGS} will provide full Bayesian posteriors on galaxy
properties instead of ``best-fit'' point estimates from maximizing the
likelihood.
Posterior distributions are essential for accurately estimating uncertainties
on galaxy properties.
As we find earlier, these uncertainties are significant, especially for
properties such as $\zmw$ (Figure~\ref{fig:prop_inf}).
Ignoring the uncertainties dramatically overestimates the statistical precision
of the derived galaxy properties and can significantly bias any galaxy study.
%We also note that the maximum-entropy method, mentioned earlier, to correct for the effect of priors on derived galaxy properties requires full posterior distributions.

Furthermore, the {\sc PROVABGS} posteriors will be derived from MCMC sampling
rather than grid-based methods often used in the
past~\citep[\emph{e.g.}][]{dacunha2008, moustakas2013, boquien2019}.
As a result, they can accurately estimate posterior distributions with
significant parameter degeneracies or multiple modes (peaks).
For instance, in the posterior of Figure~\ref{fig:posterior} we find
degeneracies between $f_{\rm burst}$ and $\{\beta_1, \beta_2, \beta_3,
\beta_4\}$ and between $\{\gamma_1, \gamma_2\}$ and $\{\beta_1, \beta_2,
\beta_3, \beta_4\}$.
The posterior is also multi-modal.
Accurate estimates of the full posterior distribution are especially important,
as they enable the maximum-entropy method, mentioned earlier,
to correct for the significant impact of priors on derived galaxy properties.
Grid-based methods also scale exponentially with the number of SPS parameters
so they quickly become infeasible as the dimensionality of SPS models increase.
MCMC, on the other hand, scales approximately linearly with the number of
parameters.

\begin{figure}
\begin{center}
\includegraphics[width=0.7\textwidth]{figs/sfh_demo.pdf}
    \caption{
        With the {\sc PROVABGS} SPS model, we can infer posteriors on the full
        star formation and metallicity histories.
        We present the inferred SFH and ZH for an arbitrarily chosen
        star-forming (blue) and quiescent galaxy (orange).
        The shaded region represent the 64 and 95\% confidence intervals of the
        SFH and ZH posteriors.
        For comparison, we include the true SFH and ZH (dashed).
        The inferred SFH and ZH show good agreement with the true values;
        however, similar to the inferred $\avgsfr$ and $\zmw$, the SFH and ZH
        are significantly impacted by priors imposed by the SPS model.
    } \label{fig:sfh_demo}
\end{center}
\end{figure}
%\todo{Beyond the galaxy properties we discuss in Section~\ref{sec:results}, we can also derive SFH and ZH}

In this work, we primarily focus on the following physical properties of
galaxies: $\log M_*$, $\log\avgsfr$, $\log\zmw$, $\tage$, and $\tauism$.
The {\sc PROVABGS} SPS model, however, can constrain galaxy properties beyond
these properties.
The SPS model employs nonparametric SFH and ZH prescriptions based on NMF bases
and the model parameters include coefficients for these bases.
Posteriors on the SPS model parameters can, thus, be used to derive constraints
on the SFH and ZH.
In Figure~\ref{fig:sfh_demo}, we present the inferred SFH and ZH of two
simulated galaxies from our \lgal~sample: a star-forming (blue) and a quiescent
galaxy (orange).
We mark the 68 and 95\% confidence intervals in the shaded regions.
For comparison, we include the true SFH and ZH from \lgal~(dashed).
The inferred SFH and ZH is able to generally recover the true histories.
We emphasize that current SPS models typically assume constant ZHs that does
not vary over time~\citep{carnall2017, leja2019}.
Hence inferring ZH over time is a key advantage of the {\sc PROVABGS} SPS
model.
Similar to the inferred $\avgsfr$ and $\zmw$, the SFH and ZH constraints are
also impacted by the priors imposed by our SPS model
(Appendix~\ref{sec:model_priors}, Figure~\ref{fig:sfh_prior}).

Another key advantage of {\sc PROVABGS} is that it will infer galaxy
properties from joint SED modeling of photometry \emph{and spectra}.
Our results illustrate the advantages of including spectra in SED modeling.
Galaxy spectra provide substantial statistical power for constraining
galaxy properties.
In addition to tightening constraints overall, their statistical power is
essential for mitigating the effect of the model priors.
For instance, including spectra in the SED modeling significantly reduces the
bias of our $\zmw$ and $\tage$ constraints (Figure~\ref{fig:etas}).
It also reduces the lower bound on the inferred $\avgsfr$.
In fact, without spectra, we are dominated by priors on $\avgsfr$ and cannot
robustly infer galaxy properties of quiescent galaxies with $\log\avgsfr < 0$
dex.
%We emphasize that all of these benefits come from spectra with the SNR of BGS, which is observing during bright time.

\subsection{Applications of PROVABGS}
{\sc PROVABGS} will be a value-added galaxy catalog with unprecedented
statistical power.
With physical galaxy properties of over $10$ million DESI BGS galaxies,
{\sc PROVABGS} will provide a transformational galaxy sample to extend
previous statistical galaxy studies.
For example, we will be able to make the most precise measurement of the
stellar mass function~\citep[SMF]{li2009, moustakas2013}, star-forming
sequence~\citep{noeske2007}, mass-metallicity relation~\citep{tremonti2004}, or
any other summary statistic of galaxy populations.
{\sc PROVABGS} will also include large sample of dwarf galaxies thanks to the
faint apparent magnitude limit of BGS.
Dwarf galaxies are dark matter dominated and, thus, probe the physics of
dark matter; they are also sensitive to star formation feedback and can help
distinguish different aspects of galaxy formation~\citep{mao2021}.
Galaxy studies examining the galaxy-halo connection can also be extended to
exploit the additional statistical power of {\sc
PROVABGS}~\citep[\emph{e.g.}][]{tinker2011, wetzel2013, zu2015, hahn2017,
hahn2019b}.
With detailed galaxy properties, {\sc PROVABGS} will also enable
multiple-tracer galaxy clustering analyses that can circumvent cosmic variance
in inferring cosmological parameters~\citep{seljak2009, mcdonald2009,
wang2020}.
Analyses exploiting new forward modeling approaches, such as \cite{hahn2021},
will also greatly benefit from the statistical power of {\sc PROVABGS}.
%\ch{Text above will be updated with more science applications based on any feedback.}

In addition to the applications above, {\sc PROVABGS} will also unlock
applications that can exploit the full posteriors of the probabilistic catalog.
In this work, we utilized the posteriors in order to quantify accuracy and
precision of galaxy population constraints using population inference with a
hierarchical Bayesian approach.
This is only the \emph{simplest} illustration of such an approach.
\edits{
    Another application is to use posteriors on $M_*$,
    $p(M_* \given {\bfi X}_i)$, to measure $p(M_* \given \{ {\bfi X_i} \})$ ---
    the \emph{probabilistic} SMF.
    With full posteriors, we can probe even the lowest signal-to-noise regime
    accurately so the SMF will be reliable even at the lowest mass end, down to
    ${\sim}10^{7} M_\odot$ (Figure~\ref{fig:bgs_mstar}).
    This will constrain the SMF of dwarf galaxies and have important
    implications for both galaxy evolution and cosmology.
}

%With the {\sc PROVABGS} posteriors we can infer fully probabilistic galaxy
%population statistics, which will allow us to robustly probe even the lowest
%signal-to-noise regime.
%A probabilistic SMF of BGS, for example, will provide accurate constraints on
%the low mass end down to ${\sim}10^{7} M_\odot$ (Figure~\ref{fig:bgs_mstar}),
%which has important implications for both galaxy evolution and cosmology.
\edits{
    Probabilistic analyses can extend to higher dimensions.
    Joint posteriors on $M_*$ and SFR, $p(M_*, {\rm SFR} \given {\bfi X}_i)$
    can be used to measure the probabilistic star formation sequence.
    Since the posteriors reliably estimate the uncertainties and parameter
    degeneracies, we will more accurately infer the intrinsic width of the
    SFS, which encodes information about star formation and stellar and AGN
    feedback in galaxies~\citep{davies2021}.
    We can even extend the approach to infer the distribution of \emph{all}
    galaxy properties given observations, $p(\theta | \{X_i\})$, which would
    exploit the \emph{full} statistical power of observations and reveal new
    trends among galaxy properties.
    This is only possible with population inference using the posterior
    distributions of every galaxy.
}

%We can also use population inference to robustly derive galaxy property distributions of galaxy subpopulations --- \emph{without stacking observations}.
Population inference also allows us to avoid stacking observations.
Stacking makes the strong assumption that galaxies that are grouped together in
some \emph{e.g.} color-space are from a subpopulation with the same properties.
This assumption fails if, for instance, there are contaminants or multiple
disparate galaxy subpopulations that are degenerate in color-space and
therefore are included in the stack.
With all of the applications listed above, {\sc PROVABGS} will enable us to
fully extract the statistical power of >10 million BGS galaxies.


\subsubsection*{Design}
Under the design of the two algorithms of the Bresenham family, we also consider the algorithm for filling rectangles, so everything mentioned for the line and circle also counts for the rectangle. We planned to make all three functions self-sufficient objects, meaning that they could also be used on their own without the thought of a graphics accelerator. Theoretically as they are implemented in hardware they would be able to run in parallel, though the execution on the graphics accelerator is planned to sequential, so that the drawing order can be assured. As there are several renditions of the Bresenham algorithms, we decided to use the most optimized versions, as they lend themselves better for a concurrent execution and were most likely simpler to implement in hardware and as they are designed to draw the most pixels per iteration, in our case this would be on pixel per clock cycle.

\subsubsection*{Implementation}
The inputs and outputs for all three algorithms are rather straight forward. They mainly consist of the input values discussed in \cref{subsec:bres}. As we cannot draw into the negative screen space we decided to create the coordinates as vectors of unsigned integers. This can be seen in \cref{bundline} for the inputs and outputs of the line algorithm. Furthermore we needed a way to trigger the algorithm, which is done with the start boolean. The ready signal tells the rest of the hardware when the algorithm is done and can accept the next values. The last to signals determine the address to where the pixel information is written and if the algorithm is writing pixels at all.
\begin{lstlisting}[language=scala, caption={Line IOs}, label=bundline]
val io = new Bundle {
	val coord1 = in Vec(UInt(1 + log2Up(config.hDisplayBuffer) bits),
			    		UInt(1 + log2Up(config.vDisplayBuffer) bits))
	val coord2 = in Vec(UInt(1 + log2Up(config.hDisplayBuffer) bits),
			    		UInt(1 + log2Up(config.vDisplayBuffer) bits))
	val start = in Bool
	val ready = out Bool
	val address = out Vec(UInt(log2Up(config.hDisplayBuffer) bits),
			      		  UInt(log2Up(config.vDisplayBuffer) bits))
	val setPixel = out Bool
}
\end{lstlisting}
All three algorithms, the line, rectangle and circle, operate with a finite state machine. One such state of this state machine can be seen in \cref{fsmCircle}. This is the idle state. Being the first and initial state of the algorithm, it waits till the start signal is set to high before it starts setting the needed values and calculates certain value. This is done by all three algorithms. After that the line algorithm goes into two state where it calculates the needed error value. This has to be calculated by two states, in the line algorithm, for dependencies reasons, as the error value is dependent on the correct delta values of the x- and y-coordinate. Once the needed variables are calculated the state machine goes into drawing the actual pixels, before it goes back to idle once it is done drawing.
\begin{lstlisting}[language=scala, caption={Start of the Circle Algorithm}, label=fsmCircle]
idle.whenIsActive{
	when (io.start) {
		x1 := io.coord(0)
		y1 := io.coord(1)
		x := (- io.r.asSInt).resized
		y := 0
		err := (2 - (io.r << 1).asSInt).resized
		goto(setPixel1)
	}
}
\end{lstlisting}
The circle algorithm is structured in a similar way as the line algorithm. We can ommit the second coordinate from the line algorithm an replace it with the circle radius. Similarly to the line algorithm it sets and calculates some variables, though it does not need another calculation state, as all the values it needs are done in idle. We the have to draw the four pixels individually, as we have only one write access to the frame buffer. Once it is done with drawing the set of pixels we have to calculate where we have to draw the next. This is done in two calculation states afterwards, similarly to the line algorithm. When it is finished with drawing and calculating it goes back into the idle state.


\documentclass{antiquebook}

\usepackage{lipsum}
\usepackage[math]{blindtext}

\author{Masum Billal \and Author Two}
\title{{Testing Antique Book}}

\subfile{glossary.tex}

\begin{document}
	\frontmatter
	\maketitle
	\tableofcontents
	\mainmatter
	\chapter{First}
	\section{Dummy}
	\blindmathtrue
	\blindmathpaper
	\section{Second}
	\blindmathpaper
	See \gls{erdos}.
		\begin{align*}
			\parenthesis{\squarebracket{\floor{\dfrac{x}{n}}}}
				& \leq \curlybrace{\ceiling{\dfrac{x}{n}}}\\
			\prod_{p\leq x}p
				& \leq\sqrt[4]{avbc}
		\end{align*}
	$a,b,c,\alpha,\beta,\gamma,\phi,\varphi,\psi,\Psi,\rho,\partial$
\end{document}


% !TeX root = article.tex
\section{Description of plasticity in the framework of physics engines}

In this section, key concepts related to the introduced model are explained. The main differences between
traditional structural analysis and physics engines based approaches are reviewed and discussed.

Velocity-based formulation of constraint based rigid body simulation
is  commonly used by physics based game
developers and film production teams.
%\citet[p.~45]{erleben.thesis}
\cite{erleben.thesis}
provides reasoning and theoretical details for the popularity of
velocity-based formulation in constraint-based rigid body simulation instead of accelaration based.
The main reason is that collision handling can be done without the use of additional procedures.

Work presented by
\cite{erleben.thesis} provides the basis for the velocity-based formulation discussed in this work.
%\citet[p.~45-50]{erleben.thesis}.
% pdf page 64
In the following section, these formulations will be clarified by a simple example using \cbullet\ implementation.

Impulse $\vec{J}$
in the time interval $\Delta t $ can be written as:
\begin{equation} \label{eq:impulseIntegral}
\vec{J} = \int_{0}^{\Delta t} \vec{f}_{true}(t) dt,
\end{equation}
where $\vec{f}_{true}(t)$ is force.

Using Newton's second law of motion $\vec{F}=m\vec{a}$ ,
$\vec{v}^{\Delta t}$ can be solved for the velocity as:
\begin{equation} \label{eq:impulseIntegraWithNewton}
\int_{0}^{\Delta t} m \frac{d\vec{v}}{dt}dt= \int_{0}^{\Delta t} \vec{f}_{true}(t)
\end{equation}
\begin{equation} \label{eq:impulse}
m(\vec{v}^{\, \Delta t} - \vec{v}^{\, 0})=\vec{J},
\end{equation}
where superscripts denote time, i.e. ${\vec{v}}^{\Delta t}=\vec{v}(\Delta t)$.
Next position can be found
by integrating the velocity.
Updates after each step can be summarized  for locations and
for velocities respectively as follows:

\begin{equation} \label{eq:eomL} % pdf page 69
\vec{s}^{\, t+\Delta t} = \vec{s}^{\, t}+\Delta t S \vec{u}^{\, t+\Delta t}
\end{equation}

\begin{equation} \label{eq:eomV}
\vec{u}^{\, t+\Delta t} = \vec{u}^{\, t}+\Delta t M^{-1}(C N \vec{f}^{\ t+\Delta t} + \vec{f}_{ext}) .
\end{equation}

The symbols used in Equations \ref{eq:eomL} and \ref{eq:eomV}
are summarized in Table \ref{tab:eom}.
Figure \ref{fig:eom-contact} describes the collision of two
bodies, rectangular body $B_1$ and triangular body $B_2$
where $\vec{r}_i$ is position of body $i$,
$\vec{p}_k $ is position of contact point $k$,
$\vec{r}_{ik} $ is a vector between the center of gravity of body $i$ and contact point $k$,
and
$\vec{n}_{k}$ is the contact normal for contact point $k$.
It is common convention that the contact normal points
from the body with the smallest index to the body with the largest index, \cite{erleben.thesis}.
In case of point or edge contacts, averaging the normals of neighboring polygons can be used, \cite{Hahn:1998}.

\begin{figure}[tb!]
\centering
\begin{tikzpicture}
\coordinate (O1) at(2,1);
\coordinate (O2) at(2,4);
\coordinate (C) at(2,2);
\draw (0,0) -- (4,0) -- (4,2) -- (0,2) --(0,0);
\draw (2,2) -- (3.5,5) -- (0.5,5) -- (2,2) ;
\node at (3.5,0.4) {$B_1$};
\filldraw (O1) circle (0.5mm) node[anchor=north] {$\vec{r}_1$};
\node at (2.8,4.5) {$B_2$};
\filldraw (O2) circle (0.5mm) node[anchor=south] {$\vec{r}_2$};
\filldraw (C) circle (0.5mm) node[anchor=north west] {$\vec{p}_1$};
\draw[-{Stealth[length=3mm]}] (O1) -- (C) node[anchor=north east] {$\vec{r}_{11}$};
\draw[-{Stealth[length=3mm]}] (O2) -- (C) node[anchor=south east] {$\vec{r}_{21}$};
\draw[-latex,thick] (C) -- ++(0,1.4) node[anchor=west] {$\vec{n}_{1}$};
\node[anchor=west] at (4.5,4.5) {
$\vec{r}_i$  =  position of body $i$
};
\node[anchor=west] at (4.5,4) {$\vec{p}_k $ = position of contact point $k$};
\node[anchor=west] at (4.5,3.5) {$\vec{r}_{ik} $ = $\vec{p}_k - \vec{r}_i $};
\node[anchor=west] at (4.5,3) {$\vec{n}_{k} $ = normal for contact point $k$};
\end{tikzpicture}
\caption{Illustration of nomenclature for equations of motion for collision.}
\label{fig:eom-contact}
\end{figure}

% pdf page 33, notation in typical ODEs
\begin{table}
\tbl{Nomenclature for equations of motion}{
\begin{tabular}{|l| l|}
\hline
{\bf Symbol} & {\bf Description} \\  \hline
$\vec{r}_i$ & position of center of mass for body $i$  \\ \hline
$\vec{q}_i$ & orientation for body $i$ as quaternion $\lbrack s_i, x_i, y_i, z_i \rbrack ^T $ \\
\hline
$\vec{p}_h$ & contact or joint point $k$  \\ \hline
$\vec{r}_{ki}$ & $\vec{p}_k - \vec{r}_i$  \\ \hline
$\vec{s}$ & $\lbrack \vec{r}_1, \vec{q}_1,...,\vec{r}_n, \vec{q}_n \rbrack ^T $\\ \hline
$Q_i$ & \begin{tabular}{@{}c}
rotation of quaternion $\vec{q}_i$
as matrix \\ where
$\frac{1}{2}\vec{\omega}_i \vec{q}_i=Q_i \vec{\omega}_i$
\end{tabular}
   $
\frac{1}{2} \left[ \begin{array}{ccc}
-x_i & -y_i & -z_i \\
s_i & z_i & -y_i \\
-z_i & s_i & x_i \\
y_i & -x_i & s_i
\end{array} \right]
$
\\ \hline
$S$ &
\begin{tabular}{@{}c}
generalized transformation matrix \\
$ S \in \mathbb{R}^{7n \times 6n}$
\end{tabular}
 $ \left[ \begin{array}{ccccc}
1 &  &  & & 0 \\
 & Q_i  \\
 & & \ddots  \\
 & & & 1 \\
0 & & & & Q_n
\end{array} \right]
$
\\ \hline
$\vec{v}_i$ & linear velocity of  center of mass for body $i$   \\ \hline
$\vec{\omega}_i$ & angular velocity of center of mass for body $i$  \\ \hline
$\vec{u}$ & $\lbrack \vec{v}_1, \vec{\omega}_1,...,\vec{v}_n, \vec{\omega}_n \rbrack ^T $\\ \hline
$M$ &
\begin{tabular}{@{}c}
 generalized mass matrix \\
$ M \in \mathbb{R}^{6n \times 6n}$
\end{tabular}
$
\left[ \begin{array}{ccccc}
m_i 1 &  &  & & 0 \\
 & I_1  \\
 & & \ddots  \\
 & & & m_n 1 \\
0 & & & & I_n
\end{array} \right]
$
\\ \hline
$I_i$ & inertia tensor for body $i$  \\ \hline
$C$ & contact condition matrix  $ C \in \mathbb{R}^{6n \times 3K}$ \\ \hline
$N$ & contact normal matrix  $ N \in \mathbb{R}^{3K \times K}$ \\ \hline
\end {tabular}}
\label{tab:eom}
\end{table}

Friction in contacts and joint constraints can be handled in a unified way by refactoring
equation \ref{eq:eomV} as,
\cite{erleben.thesis}
%\citet[p.~66-67]{erleben.thesis}
\begin{equation} \label{eq:eomV2}
\vec{u}^{\, t+\Delta t} = \vec{u}^{\, t}+\Delta t M^{-1}(
 J_{contact}^T \vec{\lambda}_{contact}
+ J_{joint}^T \vec{\lambda}_{joint}
+ \vec{f}_{ext}),
\end{equation}
where Jacobian terms $J_{joint}^T$ for joints are
derived by taking time derivatives of the kinematic constraints.
Symbols used in Equation \ref{eq:eomV2} are summarized in Table
\ref{tab:eom-g} and Figure \ref{fig:eom-joint},
where $\vec{r}_{anc}^{\,i}$ is used to define at which point
joint constraint is applied relative to body $i$.

\begin{figure}
\centering
\begin{tikzpicture}
\coordinate (O1) at(1,1);
\coordinate (O2) at(1,3);
\coordinate (C) at(1,2);
\draw (0,0) -- (2,0) -- (2,2) -- (0,2) --(0,0);
\draw (0,2) -- (2,2) -- (2,4) -- (0,4) --(0,2) ;
\node at (0.5,0.4) {$B_1$};
\filldraw (O1) circle (0.5mm) node[anchor=north] {$\vec{r}_1$};
\node at (0.5,3.5) {$B_2$};
\filldraw (O2) circle (0.5mm) node[anchor=south] {$\vec{r}_2$};
\draw[-{Stealth[length=3mm]}] (O1) -- (C) node[anchor=north east] {$\vec{r}_{anc}^{\,1}$};
\draw[-{Stealth[length=3mm]}] (O2) -- (C) node[anchor=south east] {$\vec{r}_{anc}^{\,2}$};
\node[anchor=west] at (4.5,3.5) {
$\vec{r}_i$  =  position of body $i$
};
\node[anchor=west] at (4.5,3) {$\vec{r}_{anc}^{\,i} $ = body frame vector $i$};
\end{tikzpicture}
\caption{Illustration of nomenclature for equations of motion for joint.}
\label{fig:eom-joint}
\end{figure}

% pdf page 33, notation in typical ODEs
\begin{table}
\tbl{Additional terms  for generalized equations of motion}{
\begin{tabular}{|l| l|}
\hline
{\bf Symbol} & {\bf Description} \\  \hline
$J_{contact}$ & Jacobian matrix for contacts  \\ \hline
$\lambda_{contact}$ & vector of lagrange multipliers for contacts  \\ \hline
$J_{joint}$ & Jacobian matrix for joints  \\ \hline
$\lambda_{joint}$ & vector of lagrange multipliers for joints  \\ \hline
\end {tabular}}
\label{tab:eom-g}
\end {table}

Constraint processing in \cbullet\ is based on ODE, \cite{ode}.
Joints are also discussed in detail in
\cite{erleben.thesis}.
%\citet[p.~60-90]{erleben.thesis}.
Equations \ref{eq:constraintEquation}, \ref{eq:lambdaLow} and
\ref{eq:lambdaHigh}
are created for each constraint.
Derivation for terms in Equation \ref{eq:constraintEquation}
can be done using the position and orientation of connected bodies
e.g. for ball joint formulation is based on both joint points having the same position.
In contact cases, formulation is easier if it is done using velocities, \cite{ode.joints}.

\begin{equation} \label{eq:constraintEquation}
J_1 \vec{v}_1 + \Omega_1 \vec{\omega}_1 +
J_2 \vec{v}_2 + \Omega_2 \vec{\omega}_2 = \vec{c} + C \vec{\lambda}
\end{equation}

\begin{equation} \label{eq:lambdaLow}
\vec{\lambda} \geq \vec{l}
\end{equation}

\begin{equation} \label{eq:lambdaHigh}
\vec{\lambda} \leq \vec{h}
\end{equation}

In the following section, these equations will be explained by a simple example.
The main parameters  and corresponding fields in \cbullet\
are given in Table \ref{tab:constraintParameters}.

\begin {table}
\tbl {Constraint parameters}{
\begin{tabular}{|c| l| l|}
\hline
{\bf Parameter} & {\bf Description} & {\bf btConstraintInfo2 pointer}\\  \hline
$J_1, \Omega_1$ & Jacobian & m\_J1linearAxis, m\_J1angularAxis \\
$J_2, \Omega_2$ & & m\_J2linearAxis, m\_J2angularAxis \\ \hline
$\vec{v}$ & linear velocity & \\ \hline
$\vec{\omega}$ & angular velocity & \\ \hline
$\vec{c}$        &  right side vector   & m\_constraintError \\ \hline
$C$  & constraint force mixing & cfm \\  \hline
$\vec{\lambda}$ & constraint force &  \\ \hline
$\vec{l}$ & low limit for constraint force & m\_lowerLimit \\ \hline
$\vec{h}$ & high limit for constraint force & m\_upperLimit \\ \hline
\end {tabular}}
\label{tab:constraintParameters}
\end {table}

In structural analysis, a formulation and associated numerical solution procedure are selected
based on needed features.
Often,  finite element method is used.
In most cases, a static solution with an assumption of linear strain-displacement relation
using displacement based boundary conditions is used.
\cite{bathe-1975} provides a description for handling of various nonlinearities.
In large displacement analysis, formulation may be based on updated formulation (Eulerian) or
Lagrangian formulation where initial configuration is used.
Further enhancements are material nonlinearity and dynamic analysis.
Physics engine provides dynamic analysis with large reference translations and rotations
while assuming bodies to be undeformable.

Material plasticity can be accounted for in simulations by using a suitable coefficient of restitution.
This provides a reasonable means to simulate loss of energy in collisions.
In this work simulation of breaking of bodies made of ductile material is made more realistic
by splitting the rigid body
to multiple bodies that are connected by energy absorbing joints.
A typical engineering stress-strain curve of ductile steel is shown in Figure \ref{fig:sscurve}.

\begin{figure}
\centering
\begin{tikzpicture}
\coordinate (Y) at (1,4);
\draw[->] (0,0) -- (6,0) node[right] {\large{$\epsilon$}};
\draw[->] (0,0) -- (0,5) node[above] {\large{$\sigma$}};
\draw(0,0) -- (Y) -- (2,4) .. controls (5,5) .. (6,4);
\draw[dashed](0,4) -- (Y);
\node at (-0.2,4) [align=right] {$f_y$};
\draw(0.25,1) -- (0.5,1) -- (0.5,2);
\node at (0.75,1.5) {$E$};
\node at (0.8,2.5) [anchor=west] {$\sigma = E \epsilon$ if $\sigma \le f_y$};
\end{tikzpicture}
\caption{Engineering stress-strain curve of ductile steel (not to scale).}
\label{fig:sscurve}
\end{figure}

In Figure \ref{fig:sscurve}, $\sigma$ is stress, $E$ is Youngs modulus and $f_y$ is yield stress.
Engineering stress and strain mean that original dimensions are used in stress calculation,
\cite{dowling}.
%\citet[p.~108]{dowling}.
The stress-strain curve is not drawn to scale as elastic strain could not be seen as it is typically
0.001 to 0.005 and fracture strain can be 100 times larger.

In this work, an elastic-fully plastic material model is used in most scenarios.
Having elastic part allows elastic displacements for slender structures.
Elastic material behavior is ignored in approach introduced in this work if
the deformation is related to ahigher frequency
than integration stability would allow.
It should be noted that geometry
of bodies is not updated during analysis and thus engineering stress-strain properties are used.

In this work, strain hardening is taken into account by assuming that plastic volume in bending
expands,
\cite{dowling}.
%\citet[p.~672]{dowling}.
Material that starts to yield first is hardened and as a result of which yielding moves.

The difference between the elastic and plastic section modulus is depicted in Figure \ref{fig:wp}.

\begin{figure}[htb!]
\centering
\begin{tikzpicture}
\coordinate (S) at (2.5,5);
\draw (0,5) -- (4,5) ;
\draw (0,0) -- (4,0) ;
\draw (2,0) -- (2,5) ;
\draw (1.5,0) -- (S);
\node[above] at (S) [align=center] {\large{$\sigma<f_y$}};
\node[anchor=west] at (3,3) {
\begin{tabular}{l}
Under elastic load\\
stress increases\\
linearly from zero\\
at neutral axis to\\
maximum value at \\
surface of body
\end{tabular}
};
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}
\coordinate (S) at (3,5);
\draw (0,5) -- (4,5) ;
\draw (0,0) -- (4,0) ;
\draw (2,0) -- (2,5) ;
\draw (1,0) -- (1,2.5) -- (3,2.5) -- (S);
\node[above] at (S) [align=center] {\large{$\sigma=f_y$}};
\node[anchor=west] at (3,3) {
\begin{tabular}{l}
Under fully plastic load\\
stress is at yield\\
level over full\\
cross section
\end{tabular}
};
\end{tikzpicture}
\caption{Axial stress distribution over a cross section for bending under elastic and fully plastic loads.}
\label{fig:wp}
\end{figure}

As shown in Figure 2.4, if stress is below yield limit $f_y$, stress and strain are linear within the material.
If cross section is fully plastic, stress is assumed to be at yield level over the whole cross section such that
the plastic section modulus is higher than the elastic section modulus.

In this work, plasticity is handled by defining maximum forces
in Equations \ref{eq:lambdaLow} and
\ref{eq:lambdaHigh} using plastic capacities, which are defined below.

Maximum force acting in a direction of $\vec{r}_{anc}^{\,i} $
is product of area and yield stress as follows:

\begin{equation} \label{eq:fN}
N_{max}= \int_A f_y.
\end{equation}

Maximum forces acting perpendicular to $\vec{r}_{anc}^{\,i} $
are a product of area and shear yield stress $\tau_y$ as follows:
\begin{equation} \label{eq:fQ}
Q_{max}= \int_A \tau_y.
\end{equation}

Maximum moments acting around the axis perpendicular to $\vec{r}_{anc}^{\,i} $
are integrals of the perpdendicular distance
and yield stress $f_y$ as given for the moment around the $x$-axis
and moment around the $z$-axis, respectively:

\begin{equation} \label{eq:Mx}
M_{max}^x= \int_A z f_y,
\end{equation}

\begin{equation} \label{eq:Mz}
M_{max}^z= \int_A x f_y.
\end{equation}

Maximum moment around $\vec{r}_{anc}^{\,i} $
is an integral of distance $d$ from the joint point
and shear yield stress $\tau_y$ as:

\begin{equation} \label{eq:My}
M_{max}^y= \int_A d \tau_y.
\end{equation}

Maximum forces and moments for a
rectangular section with width $b$ and height $h$ using constant yield stress
are given in Table \ref{tab:maxForces}.
Yield shear stress is assumed to be $ 0.5\, f_y$ using the Tresca yield critetion.
If the von Mises yield criterion is used 0.5 is replaced by 0.58 ($1/\sqrt{3}$), \cite{dowling}.
% p. 262, p. 268
These are not exact values in a multiaxial stress state but they
should be acceptable in most gaming scenarios.

\begin {table}
\tbl {Maximum forces and moments for
rectangular section with width $b$ and height $h$ using constant yield stress $f_y$}{
\begin{tabular}{| c| c|}
\hline
{\bf Direction} & {\bf Maximum value}  \\ \hline
maximum shear force & $0.5\, b\, h f_y$ \\ \hline
maximum normal force & $b\, h\, f_y$  \\ \hline
maximum bending moment in direction of $h$& $0.25\, b\, h^2 \, f_y$  \\ \hline
maximum bending moment in direction of $b$ & $0.25\, b^2\, h\, f_y$  \\ \hline
maximum torque & $ \approx 0.19\, b\, h\, \frac{b\, + h}{2} f_y$  \\ \hline
\end{tabular}}
\label{tab:maxForces}
\end {table}

For torque there is a closed form solution only for
circular cross sections.
Given approximation is
best suited for cases where $b$ and $h$ are similar.
Better approximation for any given $b$ and $h$ can be obtained
by integrating distance from the center of the joint over cross section and
multiplying it with the yield shear stress e.g. using Octave, \cite{octave}.
An example of calculation of the maximum moment  around $\vec{r}_{anc}^{\,i} $
is shown in Figure \ref{fig:octave-mp}.

\begin{figure}
\centering
\lstset{language=octave}
\begin{lstlisting}
b=0.01; h=0.01; fy=200e6;
wpy=fy/2*dblquad(@(x,z)...
 sqrt(x.*x+z.*z),-b/2,b/2,-h/2,h/2)
38.2
\end{lstlisting}

\caption{Calculation of maximum moment  around $\vec{r}_{anc}^{\,i} $ using Octave.}
\label{fig:octave-mp}
\end{figure}

The basic idea introduced in this study can be tested with any framework having motors and hinge constraints.
This can be done by setting the target velocity of the motor to zero and limiting
the maximum motor impulse to plastic moment multiplied by a timestep.

Further enhancements were created and tested by forking \cbullet\ source code
and adding new constraints, \cite{pbullet}.
Instructions for using  windows executable and  source code are available, \cite{bp}.


\section{Contributions}
\label{sec:contrib}

We introduced a paradigmatic representation for word context which
contains possible substitutes for the target word and their
probabilities.  The substitute probabilities were estimated using a
standard 4-gram language model with Kneser-Ney smoothing.  We
investigated distance metrics, dimensionality reduction techniques and
clustering algorithms appropriate for substitute probability vectors
and evaluated them using a supervised k-nearest-neighbor baseline and
many-to-one accuracy relative to a 45-tag 24K word test corpus.  We
found that the KL2 distance metric (a symmetric version of
Kullback-Leibler divergence), dimensionality reduction using Laplacian
eigenmaps, and spectral clustering work well with substitute vectors.
Spectral clustering of substitute vectors reveal a grouping that
largely match traditional part of speech boundaries (\spectralResult\%
many-to-one accuracy) which further improve when the one-tag-per-word
constraint is imposed (\collapseResult\% many-to-one accuracy).  These
results compare favorably to previous results published for the 45-tag
24K word test corpus we have used.
%% We are investigating ways to speed up the algorithm so it can be tested on larger corpora.


\chapter{Object-based Image Analysis }\label{sec:OBIA}

Object-Based Image Analysis (OBIA) focusses on analyzing images at
the object level instead of working at the pixel level. This approach
is particularly well adapted for high resolution images and leads to
more robust and less noisy results.

OTB allows to implement OBIA by using ITK's Label Object framework
(\url{http://www.insight-journal.org/browse/publication/176}). This
allows to represent a segmented image as a set of regions and not
anymore as a set of pixels. Added to the compression rate achieved by
this kind of description, the main advantage of this approach is the
possibility to operate at the segment (or object level).

A classical OBIA pipeline will use the following steps:

\begin{enumerate}
\item Image segmentation (the whole or only parts of it);
\item Image to LabelObjectMap (a kind of \code{std::map<LabelObject>}) transformation;
\item Eventual relabeling;
\item Attribute computation for the regions using the image before
  segmentation:
  \begin{enumerate}
         \item Shape attributes;
         \item Statistics attributes;
         \item Attributes for radiometry, textures, etc.
  \end{enumerate}

\item Object filtering
  \begin{enumerate}
         \item Remove/select objects under a condition (area less than X, NDVI higher than X, etc.)
	 \item Keep N objects;
	 \item etc.
  \end{enumerate}
\item LabelObjectMap to image transformation.
\end{enumerate}

\section{From Images to Objects}\label{sec:FromImagesToObjects}
\input{ImageToLabelToImage.tex}

\section{Object Attributes}\label{sec:ObjectAttributes}
\input{ShapeAttributeComputation.tex}

%\section{Object Filtering}
%\input{KeepNObjects.tex}

\section{Object Filtering based on radiometric and statistics attributes}
\input{RadiometricAttributesLabelMapFilterExample.tex}

\section{Hoover metrics to compare segmentations}
\input{HooverMetricsEstimation.tex}

%\section{Objects vectorization}
%\input{LabelMapVectorization.tex}

%\section{Object-based classification}


%TCIDATA{Version=5.50.0.2960}
%TCIDATA{LaTeXparent=0,0,Master.tex}

%TCIDATA{ChildDefaults=chapter:1,page:1}

\chapter{Algorithms and listings}

\section{The code tag/environment}

For algorithms I use my own environment called \textquotedblleft
code\textquotedblright\ (Shift-F8). Before you use this you have to install
my code.sty file somewhere latex can find it. Here is an example

%TCIMACRO{\TeXButton{B SingleSpaced}{\begin{singlespaced}}}%
%BeginExpansion
\begin{singlespaced}%
%EndExpansion

\begin{code}
\textbf{let} $x\mid x\in\mathbb{N}\cdot$

\textbf{while} $x>0$ do

\begin{indent}
\item $x:=x-1$
\end{indent}
\end{code}

%TCIMACRO{\TeXButton{E SingleSpaced}{\end{singlespaced}}}%
%BeginExpansion
\end{singlespaced}%
%EndExpansion

Indentation is done with the indent tag (Shift-F7). To unindent use F2. Also
use F2 to get out of code. Please only use the indentation tag (Shift-F7)
inside of the code tag (Shift-F8). Unfortunately SWP limits the number of
indentations levels to 4 --- counting not indented at all. To get more
indentation, I\ start putting in 2-Em spaces (Insert \TEXTsymbol{>}%
\TEXTsymbol{>}\ Spacing \TEXTsymbol{>}\TEXTsymbol{>}\ Horizontal Space...).
Even the first line can be indented.

%TCIMACRO{\TeXButton{B SingleSpaced}{\begin{singlespaced}}}%
%BeginExpansion
\begin{singlespaced}%
%EndExpansion

\begin{code}
\begin{indent}
\item $\left\langle P\right\rangle $
\end{indent}

$\sqsubseteq $

\begin{indent}
\item \textbf{if} $B$ \textbf{then}

\begin{indent}
\item $\left\langle B\Rightarrow P\right\rangle $
\end{indent}

\item \textbf{else}

\begin{indent}
\item $\left\langle \lnot B\Rightarrow P\right\rangle $
\end{indent}
\end{indent}
\end{code}

%TCIMACRO{\TeXButton{E SingleSpaced}{\end{singlespaced}}}%
%BeginExpansion
\end{singlespaced}%
%EndExpansion

The fragment \textquotedblleft sscode\textquotedblright\ inserts a single
spaced code environment.

Make sure that all code belonging to a single example is in one code
environment. You should only see one leader (i.e., one green box containing
the word \textquotedblleft code\textquotedblright ). If there are several
leaders, delete all but the first using the back-space key. This often
needed after backing out of an indented section using F2. Figure~\ref%
{fig:extra-code} shows what can happen. If you are looking at this document
with SW or SWP, you will see the second leader in front of the word
\textquotedblleft assert\textquotedblright . If you are looking at the
typeset document, you will see extra space caused by having two code
environments where there should be one. After using F2 to get out of the
indent environment I\ should have used the backspace key to eliminate the
second green leader. You might wonder whether a similar trick is needed when
you use F2 to leave a nested indent environment; the answer is `no'.

%TCIMACRO{\TeXButton{B Figure}{\begin{figure}[tb]}}%
%BeginExpansion
\begin{figure}[tb]%
%EndExpansion
%TCIMACRO{\TeXButton{B SingleSpaced}{\begin{singlespaced}}}%
%BeginExpansion
\begin{singlespaced}%
%EndExpansion

\begin{code}
\textbf{assume} $t<\infty $

\textbf{var} $i:\in \mathbb{N}$

\textbf{while} $i>0$ \textbf{do}

\begin{indent}
\item $i:=i-1$
\end{indent}
\end{code}

\begin{code}
\textbf{assert} $t<\infty $
\end{code}

%TCIMACRO{\TeXButton{E SingleSpaced}{\end{singlespaced}}}%
%BeginExpansion
\end{singlespaced}%
%EndExpansion
%TCIMACRO{%
%\TeXButton{E Figure}{\caption{Incorrect spacing casued by an extra code environment}\label{fig:extra-code}
%\end{figure}}}%
%BeginExpansion
\caption{Incorrect spacing casued by an extra code environment}\label{fig:extra-code}
\end{figure}%
%EndExpansion

The fragment \textquotedblleft parcode\textquotedblright\ inserts code
consisting of two parallel threads. (It is not singlespaced, but you can use
the \textquotedblleft singlespaced\textquotedblright\ fragment to make it
so.) You may need to fiddle with the widths of the minipages.\ See Algorithm~%
\ref{alg:prod-cons} for an example.

%TCIMACRO{\TeXButton{B Algorithm}{\begin{algorithm}}}%
%BeginExpansion
\begin{algorithm}%
%EndExpansion
%TCIMACRO{\TeXButton{B centering}{\begin{centering}}}%
%BeginExpansion
\begin{centering}%
%EndExpansion
%TCIMACRO{\TeXButton{B tabular}{\begin{tabular}{r||l}}}%
%BeginExpansion
\begin{tabular}{r||l}%
%EndExpansion
%TCIMACRO{\TeXButton{B minipage}{\begin{minipage}[t]{2.25in}}}%
%BeginExpansion
\begin{minipage}[t]{2.25in}%
%EndExpansion
%TCIMACRO{\TeXButton{B SingleSpaced}{\begin{singlespaced}}}%
%BeginExpansion
\begin{singlespaced}%
%EndExpansion

\begin{code}
\textbf{while} $\mathit{true}$ \textbf{do}

\begin{indent}
\item compute next value

\item $P(e)\qquad $

\item fill the buffer

\item $V(f)$
\end{indent}
\end{code}

%TCIMACRO{\TeXButton{E SingleSpaced}{\end{singlespaced}}}%
%BeginExpansion
\end{singlespaced}%
%EndExpansion
%TCIMACRO{\TeXButton{E minipage}{\end{minipage}}}%
%BeginExpansion
\end{minipage}%
%EndExpansion
%TCIMACRO{\TeXButton{newcolumn}{&}}%
%BeginExpansion
&%
%EndExpansion
%TCIMACRO{\TeXButton{B minipage}{\begin{minipage}[t]{2.25in}}}%
%BeginExpansion
\begin{minipage}[t]{2.25in}%
%EndExpansion
%TCIMACRO{\TeXButton{B SingleSpaced}{\begin{singlespaced}}}%
%BeginExpansion
\begin{singlespaced}%
%EndExpansion

\begin{code}
\textbf{while} $\mathit{true}$ \textbf{do}

\begin{indent}
\item $P(f)$

\item empty the buffer

\item $V(e)$

\item use the value
\end{indent}
\end{code}

%TCIMACRO{\TeXButton{E SingleSpaced}{\end{singlespaced}}}%
%BeginExpansion
\end{singlespaced}%
%EndExpansion
%TCIMACRO{\TeXButton{E minipage}{\end{minipage}}}%
%BeginExpansion
\end{minipage}%
%EndExpansion
%TCIMACRO{\TeXButton{E tabular}{\end{tabular}}}%
%BeginExpansion
\end{tabular}%
%EndExpansion
%TCIMACRO{\TeXButton{E centering}{\end{centering}}}%
%BeginExpansion
\end{centering}%
%EndExpansion

%TCIMACRO{\TeXButton{Caption}{\caption{A producer and a consumer}}}%
%BeginExpansion
\caption{A producer and a consumer}%
%EndExpansion
\label{alg:prod-cons}%
%TCIMACRO{\TeXButton{E Algorithm}{\end{algorithm}}}%
%BeginExpansion
\end{algorithm}%
%EndExpansion

Sometimes you want algorithms to float like figures and tables. You can use
the \textquotedblleft figure\textquotedblright\ fragment as in Figure~\ref%
{fig:extra-code}. You can also use the \textquotedblleft
algorithm\textquotedblright\ fragment. Use Alt-4 and select
\textquotedblleft algorithm\textquotedblright . Edit the caption and marker;
add your code. Algorithms inserted using this fragment are singlespaced.
Algorithm~\ref{alg:subsetcons} is an example.

%TCIMACRO{\TeXButton{B Algorithm}{\begin{algorithm}\begin{singlespaced}}}%
%BeginExpansion
\begin{algorithm}\begin{singlespaced}%
%EndExpansion

\begin{code}
$\dot{q}_{\mathrm{start}}:=\epsilon$-closure$(q_{\mathrm{start}})\;;$

\textbf{var} $W:=\{\dot{q}_{\mathrm{start}}\}\cdot$

$\dot{Q}:=\emptyset\;;$

$\dot{T}:=\emptyset\;;$

\textbf{while} $W\neq\emptyset$ \textbf{do }$\mathbf{(}$

\begin{indent}
\item \textbf{let} $\dot{q}\mid\dot{q}\in W\cdot$

\item $W:=W-\{\dot{q}\}\;;$

\item $\dot{Q}:=\dot{Q}\cup\{\dot{q}\}\;;$

\item \textbf{for} \textbf{each} $a\in S\;$\textbf{do}$\;($

\begin{indent}
\item \textbf{let} $\dot{r}=\epsilon$-closure$(\delta(\dot{q},a))\cdot$

\item \textbf{if} $\dot{r}\neq\emptyset$ \textbf{then }$($

\begin{indent}
\item $\dot{T}:=\dot{T}\cup\{(\dot{q},$\textsf{\textquotedblleft}$a$\textsf{%
\textquotedblright}$,\dot{r})\}\;;$

\item \textbf{if} $\dot{r}\not \in \dot{Q}$ \textbf{then} $W:=W\cup\{\dot {r}%
\}$ \textbf{else skip}$\;)$
\end{indent}

\item \textbf{else skip} $)\;)$
\end{indent}
\end{indent}

$\dot{F}:=\{\dot{q}\in\dot{Q}\mid\dot{q}\cap F\neq\emptyset\}$
\end{code}

%TCIMACRO{\TeXButton{Caption}{\caption{The subset construction algorithm}}}%
%BeginExpansion
\caption{The subset construction algorithm}%
%EndExpansion
\label{alg:subsetcons}%
%TCIMACRO{\TeXButton{E Algorithm}{\end{singlespaced}\end{algorithm}}}%
%BeginExpansion
\end{singlespaced}\end{algorithm}%
%EndExpansion

\section{Listings}

Listings are lines of source code. If you want to include source code in
your thesis, you will want listings

\subsection{If you do want to use listings\label{sec:doWantListings}}

You must install the listings package somewhere latex can find it. You
should read the documentation: listings.pdf.

You will also want to install the fragments mentioned below.

You can edit the default settings by editing the \TEXTsymbol{\backslash}%
lstset command in the preamble (Typeset \TEXTsymbol{>}\TEXTsymbol{>}\
Preamble). For example, if all (or most of) the listing in your thesis are
in Java, add \textquotedblleft language=Java\textquotedblright\ to the list
of key/value pairs in the \TEXTsymbol{\backslash}lstset command in the
preamble of the Master document.

\subsection{In-situ listings}

In-situ listings are fragments of code that are placed right in your TeX
file. Typically you copy text from a source file and paste it in to
Scientific Workplace.

Here is a listing that appears \textquotedblleft displayed\textquotedblright
, i.e. as a paragraph in your document.

%TCIMACRO{\TeXButton{B SingleSpaced}{\begin{singlespaced}}}%
%BeginExpansion
\begin{singlespaced}%
%EndExpansion

%TCIMACRO{%
%\TeXButton{In-situ-listing}{\begin{lstlisting}[language=Pascal]
%program Hello ;
%begin
%   write("Hello");
%end.
%\end{lstlisting}}}%
%BeginExpansion
\begin{lstlisting}[language=Pascal]
program Hello ;
begin
   write("Hello");
end.
\end{lstlisting}%
%EndExpansion

%TCIMACRO{\TeXButton{E SingleSpaced}{\end{singlespaced}}}%
%BeginExpansion
\end{singlespaced}%
%EndExpansion

You can also have listings as floating elements, like figures and tables.
These have captions and labels, which means you can refer to them using
cross-references. See Listing \ref{lst:inSitu} for an example.

%TCIMACRO{\TeXButton{B SingleSpaced}{\begin{singlespaced}}}%
%BeginExpansion
\begin{singlespaced}%
%EndExpansion

%TCIMACRO{%
%\TeXButton{Floating-in-situ-listing}{\begin{lstlisting}[float=tb,language=Haskell,caption={This listing is {\it in situ}}, label=lst:inSitu]
%data Tree a = Empty | Branch a (Tree a) (Tree a)
%
%Flatten t = FlattenAndCat t []
%FlattenAndCat Empty xs = xs
%FlattenAndCat (Branch x left right ) ys
%   =  FlattenAndCat left (x :: FlattenAndCat right ys)
%
%five = let 2+2 = 5 in 2+2
%
%\end{lstlisting}}}%
%BeginExpansion
\begin{lstlisting}[float=tb,language=Haskell,caption={This listing is {\it in situ}}, label=lst:inSitu]
data Tree a = Empty | Branch a (Tree a) (Tree a)

Flatten t = FlattenAndCat t []
FlattenAndCat Empty xs = xs
FlattenAndCat (Branch x left right ) ys
   =  FlattenAndCat left (x :: FlattenAndCat right ys)

five = let 2+2 = 5 in 2+2

\end{lstlisting}%
%EndExpansion

%TCIMACRO{\TeXButton{E SingleSpaced}{\end{singlespaced}}}%
%BeginExpansion
\end{singlespaced}%
%EndExpansion

Use fragment \textquotedblleft listing (nonfloating)\textquotedblright\ for
nonfloating listings and \textquotedblleft listing
(floating)\textquotedblright\ for floating listings.

\subsection{Turning files into listings}

The listings package allows LaTeX to read in a source file and turn it into
a nicely typeset listing.

An example can be found in Listing \ref{lst:fromFileExample}.

%TCIMACRO{\TeXButton{B SingleSpaced}{\begin{singlespaced}}}%
%BeginExpansion
\begin{singlespaced}%
%EndExpansion

%TCIMACRO{%
%\TeXButton{Include of CommonParserHelper}{\lstinputlisting
%[float=tb,language=Java,caption={This listing comes from a file}, label=lst:fromFileExample,firstline=18]{code/CommonParserHelper.java}}}%
%BeginExpansion
\lstinputlisting
[float=tb,language=Java,caption={This listing comes from a file}, label=lst:fromFileExample,firstline=18]{code/CommonParserHelper.java}%
%EndExpansion

%TCIMACRO{\TeXButton{E SingleSpaced}{\end{singlespaced}}}%
%BeginExpansion
\end{singlespaced}%
%EndExpansion

Use the fragment \textquotedblleft listing (from file)\textquotedblright\
and edit the contents of the grey box that gets inserted.

By default these listings will float. If you don't want that, just remove
the \textquotedblleft float=tb\textquotedblright\ key/value pair. Also it is
best to remove the caption and label for nonfloating listings.

Note that floating listings can not exceed a page, which in a MUN\ thesis is
about 40 lines single spaced. In the example, I started this listing at line
18 of the file (firstline=18) so as to get the float to fit on a page. (The
first 17 lines are comments; and who wants to read those?)

\subsection{In-line listings}

In-line listings are short pieces of text that can be included in the normal
flow of a paragraph. Use the fragment \textquotedblleft listing
(inline)\textquotedblright . Here is an example:\
%TCIMACRO{%
%\TeXButton{Inline listing}{\lstinline[prebreak={},postbreak={}]@thesis.MagicAlgorithm.runSpell( i );@}}%
%BeginExpansion
\lstinline[prebreak={},postbreak={}]@thesis.MagicAlgorithm.runSpell( i );@%
%EndExpansion

\subsection{Typeset the master document}

If you use inputted file listings, only typeset the \textquotedblleft
Master\textquotedblright\ document. Typesetting a chapter on its own won't
work. The reason is that SW/SWP usually will move your document to a
temporary location for typesetting. But as it doesn't move your code files,
they can't be found from the temporary location. An exception is when SW/SWP
typesets a \textquotedblleft Master\textquotedblright\ document; then it
doesn't use a temporary location and sanity is restored.

\subsection{If you don't want to use listings}

With Typeset\ \TEXTsymbol{>}\TEXTsymbol{>}\ Options and Packages... remove
\textquotedblleft listings\textquotedblright\ from the package list. Then,
in the preamble (Typeset \TEXTsymbol{>}\TEXTsymbol{>}\ Preamble),\ remove
all commands that have to do with listings. You should also remove the List
of Listings from the frontMatter.tex document. This will improve compile
times and save you from having to install the \textquotedblleft
listings\textquotedblright\ package.


\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{algorithmic}
\usepackage{algorithm}
\usepackage{pst-plot}
\usepackage{graphicx}
\usepackage{endnotes}
\usepackage{graphics}
\usepackage{floatflt}
\usepackage{wrapfig}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{verbatim}
\usepackage{hyperref}
\usepackage{multirow}
\usepackage{pdflscape}

\usepackage{hyperref}
\hypersetup{pdfborder={0 0 0 0}}

\pdfpagewidth 210mm
\pdfpageheight 297mm
\setlength\topmargin{0mm}
\setlength\headheight{0mm}
\setlength\headsep{0mm}
\setlength\textheight{250mm}
\setlength\textwidth{159.2mm}
\setlength\oddsidemargin{0mm}
\setlength\evensidemargin{0mm}
\setlength\parindent{7mm}
\setlength\parskip{0mm}

\newenvironment{exercise}[3]{\paragraph{Exercise #1: #2 \textsc{(#3pt)}}\ \\}{
\medskip}
\newcommand{\question}[2]{\setlength\parindent{0mm}\ \\$\mathbf{Q_#1:}$ #2\ \\}

\author{\large{Ilya Kuzovkin, Raul Vicente}}
\title{\huge{Introduction to Computational Neuroscience}\\\LARGE{Practice II: Data Analysis - Continuous Data}}

\begin{document}
\maketitle
\ \\
This is how usual EEG recording of one channel looks like:
\begin{figure}[H]
   \centering
   \includegraphics[width=0.8\textwidth]{eegrecording.png}
\end{figure}
On the $x$ axis there is time, on the $y$ axis strength of the recorded signal in $\mu$V. The dashed line at the time point $x=1000$ is the moment when the \emph{stimulus} (picture) was shown to the test subject.

%
% ERP
%
\begin{exercise}{1}{Event Related Potential (ERP)}{1}
By looking at the plot above one cannot say that showing the stimulus had any considerable effect on the test subject. It seems that there is an increase starting at the time point 1200 and ending at the time point 1500, but that can be a random event, which is not related to the stimulus.

In such studies the way to go is to conduct the same experiment several times and then average the results. In this way the response, if it is there, will reveal itself much more clearly.

In the \texttt{data} folder you have file \texttt{erptrials.csv}. One row is one trial recorded for 2 seconds with sampling rate of 1000 Hz. The stimulus was shown at the time point 1000 (1 second). There are 79 trials in this file. Your task is to plot an average of all 79 trials and see whether there is a clear ERP response or not.
\end{exercise}

%
% Apply fourier
%
\begin{exercise}{2}{Frequency Analysis}{2}
The most popular operation that you can do with continuous brain data is converting it from the \emph{time domain} to the \emph{frequency domain}. Due to the fact that any function can be represented as a sum of sinusoids we can decompose our signal into such sinusoids and observe from which frequency components it consists. In terms of the brain data such transformation makes particular sense, because of the \emph{brain rhythms} -- different frequencies of the firings of the neurons are related to the different kinds of mental activity\footnote{\url{http://en.wikipedia.org/wiki/Electroencephalography\#Wave_patterns}}.

\ \\
In this exercise we will plot \emph{power spectrum} and see how \emph{alpha wave} emerges when the test subject's eyes are closed. In the \texttt{data} folder you can find two files: \texttt{eyes\_open.csv} and \texttt{eyes\_closed.csv}. The data is recorded from the channel \texttt{Pz}. One row contains 4 seconds of the signal, sampling rate is 512. Each file has 15 recordings.

\ \\
Your task is to perform Fourier analysis on both datasets, plot power spectra and compare the results. Do it as follows:
\begin{enumerate}
	\item Plot some of the signals just to see how they look like.
	\item For each recorded signal (2048 data points):
		\begin{enumerate}
			\item Use \texttt{fft(signal)} to compute power spectrum of this signal. You will get a vector of complex numbers.
			\item Use \texttt{abs(result\_of\_fft)} to obtain the magnitude\footnote{\url{http://www.mathworks.se/help/matlab/ref/abs.html}}. \texttt{abs(result\_of\_fft)}$^2$ will give you power.
		\end{enumerate}
	\item Sum together 15 power spectrum distributions, that you got from the previous step.
	\item And divide the resulting vector by 15 to obtain the average.
	\item Plot it. You will see that the right part of the graph is mirror image of the left part. Discard the right part.
	\item Your $x$ axis goes from 1 to 1024, which does not correspond to actual frequencies. Compute correct $x$ axis as follows:
		\begin{verbatim}
		dt = 1/512;             % time step
df = 1/4;               % frequency step
fNQ = 1/dt/2;           % fNQ is the maximal frequency, in our case it is 256
                        %   if you have discarded part of the data in step 5
                        %   or 512 if you did not
xaxis = (0:df:fNQ-df);  % points for your X axis, should be of the same length
                        %   as the vector of frequencies
		\end{verbatim}
	\item And plot it again
		\begin{verbatim}
		plot(xaxis, powers_average);
		\end{verbatim}
		Now your axis goes from 0 to 256 (or 512) with step of 4, which corresponds to the frequency range \texttt{pow} function produced.
\end{enumerate}

\ \\
After making the plot more beautiful and focusing on the range from 0 to 30 Hz you should obtain the result that looks something like this:
\begin{figure}[H]
   \centering
   \includegraphics[width=1\textwidth]{fouriereyes.png}
\end{figure}
\end{exercise}

%
% Epilepsy
%
\begin{exercise}{3}{Epilepsy}{2}
In the folder \texttt{data/epilepsy} you have two data files: \texttt{sz4\_pre.dat} and \texttt{sz4\_ict.dat}. This data was recorded using ECoG\footnote{http://en.wikipedia.org/wiki/Electrocorticography} array. The file with the suffix \texttt{\_pre} is recorded before the seizure and the \texttt{\_ict} file is recorded during the epilepsy seizure. Read the \texttt{README.txt} file for the description of the data. Your task is to perform Fourier analysis on both files and see how brain activity changes during the epilepsy seizure. Speculate what is the nature of the peak around 60Hz.
\paragraph{Hint.} Reuse the code from the previous exercise.
\paragraph{Hint.} The data has readings from the 76 electrodes. First make Fourier transform on each of them separately, then compute the power spectra (each one separately) and then compute the average and plot that.
\end{exercise}

%
% Discuss
%
\begin{exercise}{4}{Nyquist theorem}{1}
What does Nyquist theorem say? What does it mean? What are the implications?\\
What does this picture illustrate?
\begin{figure}[H]
   \includegraphics[width=0.4\textwidth]{nyquist.png}
\end{figure}
\ \\
Prepare to present and explain the theorem to your fellow students.
\end{exercise}

%
% Play DFT
%
\begin{exercise}{5*}{Discrete Fourier Transform (DFT)}{bonus 2}
Now let us have a look on what happens behind the scenes when you call the \texttt{fft}\footnote{Actually Fast Fourier Transform (FFT) is a specific algorithm. In this exercise we do not analyse that, but rather look at the equations, which FFT solves in his own mysterious way.} function. Fourier transform is possible because, as we mentioned before, any function can be represented as a sum of sinusoidal functions of different frequencies and amplitudes. If you have some variable changing over time you can transform this data from the \emph{time domain} (time on the $x$ axis and the value of the variable on the $y$ axis) to the \emph{frequency domain} (frequency on the $x$ axis and it's \emph{power} on the $y$ axis) to see if there is any periodicity in the changes of that variable.

\ \\
Transformation is defined both for continuous and discrete data. On practice we mostly work with a discrete signal and therefore we use Discrete Fourier Transform (DFT). The equation for the discrete transform is
$$X_f = \displaystyle\sum_{t=0}^{N-1}x_t e^{-i2\pi f\frac{t}{N}}$$
where
\begin{itemize}
\itemsep 0em
	\item $f$ is the frequency of the component, contribution of which you are trying to estimate.
	\item $X_f$ is a complex number, that encodes both amplitude and phase of the sinusoidal component.
	\item $x_t$ is a value of your signal at the time point $t$.
	\item $N$ is the total number of time points.
\end{itemize}
Let us say that you would like to know contribution of sinusoidal components of frequencies from 1 to 100 Hz. Then you should apply this equation 100 times. As a result you will get 100 complex numbers.
To extract amplitude from these numbers use the following equation on each of them:
$$A_f = 2\frac{\sqrt{\text{Re}(X_f)^2 + \text{Im}(X_f)^2}}{N}$$
Now you have all the contributions and you have completed Discrete Fourier Transform of your signal. For example if 7th number is "25" then it means that the amplitude of the sinusoidal component with frequency 7 $\times$ df Hz is 25.

\ \\
Here you see a curve, that is a sum of three sinusoids. Your task is to apply DFT and find out frequencies and amplitudes of those components. You can find data points for this curve in the file \texttt{curve.csv}.
\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.6\textwidth]{dodft.png}
\end{figure}
\end{exercise}

\ \\
\ \\
\ \\
\ \\
Please submit a \texttt{pdf} report with the answers to the questions, plots and comments about your solutions. Also submit the code for the programming exercise(s). Pack those into the \texttt{zip} archive and upload to the course web page.

\end{document}

2015/Practices/03 - Continuous Data/text/cns-continuousdata.tex


\section{Introduction} \label{introduction}
The scenario used for this thesis is an application which constantly receives data in form of a time series stream from
one or more sensors. The application itself does not have a general interest in a long term evaluation of the data. Only
the most recent time series window has to be classified in the evaluation. The result of the evaluation can be stored or
processed by an other application, the time series window moves on and has to be evaluated again. This process is
continuously repeated with every times series window being readable only once. Dynamic Time Warping (DTW) in combination
with a 1-Nearest-Neighbour (1NN) classification for the evaluation is an obvious approach for such a scenario. The
disadvantage is that this approach is computationally too demanding for many real time applications \cite{xi2006fast}.
This disadvantage becomes even more tragic under the assumption that perhaps a large amount of incoming time series
windows is unclassifiable due to a too large distance to the nearest neighbour.

This bachelor thesis explains the approach of a sliding window filter for time series ahead of the 1NN in combination
with DTW (1NN-DTW) to reduce the execution of 1NN-DTW on unclassifiable time series windows. The condition for such a
filter is linear complexity. Furthermore the combination of the filter ahead of the 1NN-DTW should perform with a
similar accuracy as 1NN-DTW on its own.

An experiment that simulates an above described scenario was carried out to expose the additional benefit of a filter
ahead of 1NN-DTW under the given conditions.%TODO double of

The bachelor thesis is organized as follows. Section \ref{background_and_notation} is explaining the sliding window
technique and basics around time series similarity measures that are used in this bachelor thesis. Furthermore
the sliding window filter is described at the end of the same section. The description of the experiment and the
evaluation of the results are contained in section \ref{experiment}. Conclusions are offered and future work
is suggested in section \ref{conclusion_and_future_work}.


\section{Solution}
\label{sec:sol}
% (The actual solution in details. Note that there is no need for code or
% specific software component tools description here. Also, you do not explain things
% already known by the theory, e.g., do not start elaborating on what is clustering and
% how useful it is.)
In this section, we present an algorithm to deal with the problem.
Furthermore, we also describe some optimizations, whose improvement will be
shown in Section~\ref{sec:eval} along with the evaluation results.

\subsection{Proposed solution}
The solution we propose is based on the A-Priori algorithm.

The general idea is that we first group the tweets by the period they belong to,
and then, for each period, we find the popular topics in the period by
exploiting the A-Priori algorithm. In this case, the transactions are represented by
the tweets and the items of a transaction are the relative tokens. Consequently,
the frequent itemsets returned by the algorithm are the popular topics of the period.
So, we extract the popular topics in the period using the A-Priori algorithm,
setting as threshold $s$.

By doing so, we build another dataset which will be the input for another
instance of the A-Priori: in
the new dataset, we have a transaction for each period of time and the items of a transaction
are the popular topics in that period. As threshold for this second instance we use $r$.

This algorithm answer exactly the problem stated in Section~\ref{sec:ps}, as
it finds the topics which are $s$-popular in a fraction of periods of time greater than or equal to $r$,
which is the definition of $s$-popular-$r$-consistent.

Actually, since in the second instance of the algorithm we only need to find
itemsets of size $1$, i.e. single topics,
we only need to count in how many periods of time a topic is found to be frequent.
Thus, we only need to perform the first pass of A-Priori, without the need to build
candidate pairs and so on. The complete process is shown in Algorithm~\ref{alg:pcis}.

\input{algorithms/popular_consistent_itemsets.tex}

The classical A-Priori algorithm is described in Section~\ref{sec:rw}.
However, if the dataset is not too big, we can exploit this
fact to implement it more efficiently, by storing some additional
data structures. We now proceed to describe these optimizations.

% \input{algorithms/apriori_standard.tex}

\subsection{Optimizations}

The idea is illustrated in Algorithm~\ref{alg:apriori_opt}.

\input{algorithms/apriori_std_opt.tex}

First, Procedure~\ref{alg:getItems} is called. The procedure scans all the transactions
and the items in them, building three data structures:
\begin{description}
    \item[itemIndex, indexItem] used to map items to numbers and vice versa. In fact,
        the algorithm finds frequent itemsets of numbers corresponding to items, to
        be more efficient both in terms of time and memory.
        Once the itemsets of numbers are found, they are converted back to sets of items.
    \item[items] that is a set composed of the sets containing single items, i.e. the candidates
        of size $1$.
    \item[indexTransactions] an array where the $i$-th entry is the set of the transactions
        containing the $i$-th item.
\end{description}

The rest of the algorithm is almost the same as the classical
version, except for the computation of the support of an itemset
in Procedure~\ref{alg:getSupport},
which exploits the \emph{indexTransactions} structure to speed up the
computation. In fact, having in main memory the set of transactions
containing each item allows us to get the support of the
itemset fast by computing the size of the intersection of the transactions set of
each item in the itemset.

Finally, once found the frequent itemsets are found, the algorithm maps back
set of numbers to sets of items, as shown in Procedure~\ref{alg:mapItems}.

% \input{algorithms/apriori_opt.tex}

\input{algorithms/apriori_std_get_items.tex}

\input{algorithms/apriori_std_get_support.tex}

\input{algorithms/apriori_std_map_items.tex}


\section{Tensor product}


\section{Task B}
\label{sec:task-b}

The given boundary value problem has been solved using Matlab script
\texttt{test\_plate\_shear.m} (see section \ref{app:matlab-code}).
Figure \ref{fig:small-bvp} shows total reaction force plotted against
displacement of the top nodes and the shape of the plate before
and after the deformation.
\begin{figure}[th]
  \centering
  % Reaction force
  \begin{subfigure}[t]{\textwidth}
    \begin{tikzpicture}
      \begin{axis}[
        width = 0.95\textwidth,
        height=\axisdefaultheight,
        tick label style={/pgf/number format/fixed},
        try min ticks=6,
        minor tick num=1,
        grid=both,
        xlabel = {\(u_{x}\), [mm]},
        ylabel = {Reaction force, [N]},
        xmin=0, xmax=0.1, ymax=550
        ]
        \addplot table[skip first n=1] {data/force_displacement.dat};
      \end{axis}
    \end{tikzpicture}
    \caption{}
  \end{subfigure}

  % Geometry
  \begin{subfigure}[t]{\textwidth}
    \begin{tikzpicture}
      \begin{axis}[
        width = 0.95\textwidth,
        try min ticks=6,
        enlargelimits=false,
        axis equal image,
        axis on top,
        xlabel={\(x\), [mm]},
        ylabel={\(y\), [mm]}
        ]
        \addplot graphics[xmin=-0.9945,xmax=-0.1363,
                          ymin=-0.3132,ymax=0.3352] {small_mesh};
      \end{axis}
    \end{tikzpicture}
    \caption{}
  \end{subfigure}
  \caption{(a) Reaction force versus displacement and (b) deformed mesh.}
  \label{fig:small-bvp}
\end{figure}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../main"
%%% End:


% !TEX root = rankfromsets.tex
\section{Discussion}
The task of recommending items with attributes is difficult for several reasons. It is unclear how to incorporate set-valued side information into models that scale to large numbers of items and attributes. In addition, existing recommendation models that leverage item attributes (for example, content-based matrix factorization) are not directly tied to evaluation metrics. We developed \gls{rfs}, a class of scalable recommendation models for items with attributes. Theoretically, we showed that optimizing the \gls{rfs} objective optimizes recall, and that \gls{rfs} can approximate permutation-invariant recommendation models including content-based matrix factorization. Empirically, models in the \gls{rfs} class outperform competing models and scale to large datasets, such as our motivating problem of meal recommendation for 55k users who consume 16M meals.

How well does binary classification perform for other ranking-based recommendation metrics, such as non-discounted cumulative gain? Analyzing this question is more difficult, and we leave this to future work. For generalization theory, we conjecture that a different loss function should allow a similar proof to \Cref{prop:maximizing-recall}. With sufficient data, \gls{rfs} can learn arbitrary distributions of users consuming items with attributes. But performance on finite data can vary, and developing generalization theory for \gls{rfs} remains an open question.

% \paragraph{Simple theory and implementation} It is surprising that \gls{rfs}
% outperforms the collaborative topic Poisson factorization model in
% \citet{gopalan2014content-based}, as \gls{rfs} is a much simpler model.
% \gls{rfs} does not require approximate posterior inference as in \gls{ctpf}, but
% mere binary classification. \gls{rfs} is also simpler in terms of
% implementation: a scalable example is a few dozen lines of python in
% \Cref{sec:code}, compared to several thousand lines of C++ released by the
% authors of \gls{ctpf}.

% \paragraph{Architectures} There is a wealth of deep learning models that can be
% used to parameterize \gls{rfs}. For example, attention-based transformer
% networks can operate on set-valued input~\citep{lee2018set-transformer} and are
% an interesting choice of architecture for \gls{rfs}.

% larger batch sizes on cpu
% For good performance on the arXiv dataset, large batch sizes were necessary. We
% were limited in batch size due to our use of a GPU. Reimplementing the model in
% C++ would free the model from GPU memory restrictions, enabling larger batch
% sizes and better performance.
% optimization: ctpf benefits from coordinate ascent
% \paragraph{Optimization} Collaborative topic Poisson factorization as the latter
% enjoys an efficient optimization algorithm. The conjugacy properties of the
% matrix factorization model permit coordinate ascent and fast convergence in few
% iterations. The nonconvex objective function that \gls{rfs} relies on may be
% amenable to certain optimization techniques, such as dual coordinate
% ascent~\citep{yu2011dual}, and we leave this to future work.

% \paragraph{Side information} The \gls{rfs} model can easily accommodate various
% metadata about users, items, or attributes. Attribute-level or user-level side
% information is particularly interesting, and when available, should lead to
% improved recommendation performance.

% % we did not study regularization
% \paragraph{Regularization} Further performance gains from regularizing \gls{rfs}
% are also interesting avenues for future work. L2 regularization for sparsity and
% dropout for decorrelation may yield improvements.

% RMSProp~\citep{graves2013generating} was also tested when fitting
% \gls{rfs} to the arXiv data. This optimizer outperformed
% collaborative topic Poisson factorization in terms of both in-matrix precision
% and recall, but matched the performance of the matrix factorization model in
% terms of out-matrix precision and recall. This means there are tradeoffs of
% using different optimizers that depend on the data and evaluation metrics of
% interest.
% initializing to word2vec
% \paragraph{Pretrained embeddings} We note that the word embedding baseline
% performed slightly worse as collaborative topic Poisson factorization, which is
% surprising given that the word embedding model is much simpler. Further
% performance gains in \gls{rfs} should be possible by initializing user and item
% attribute embeddings to pretrained word embeddings as in \citet{chen2017joint}.

% % gap in prop 2
% \paragraph{Theory (approximation)} \Cref{prop:universal-approximation} means
% that \gls{rfs} can approximate the output of any order-invariant model
% such as matrix factorization. However, there is no guarantee that fit with
% maximum likelihood estimation using the objective in \Cref{eq:objective},
% \gls{rfs} \emph{will} approximate a specific model. We leave the development of
% such approximation techniques to future work.
% out-of-vocabulary words
% An open question is how to account for new attributes, for example in
% crowdsourced data. An extension of this work would be to combine it with
% character-level embeddings as in \citep{bojanowski2017enriching} to enable the
% model to better perform recommendations for out-of-vocabulary attributes.
% can we optimize metrics other than recall?

% \section*{Acknowledgments}
% % and emotional support
% J.A. is grateful to Mark Goldstein and Bharat Srikisan for helpful
% discussions, and to Kyle Cranmer
% and the Center for Data Science at New York University for desk space. The
% experiments presented in this article were performed on computational resources
% supported by the Princeton Institute for Computational Science and Engineering
% and the Office of Information Technology's High Performance Computing Center and
% Visualization Laboratory at Princeton University.

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "set_recommendation"
%%% End:


    \filetitle{compile}{Compile an m-file function based on a steady-state file}{sstate/compile}

	\paragraph{Syntax}

 \begin{verbatim}
 compile(S)
 compile(S,Fname,...)
 \end{verbatim}

 \paragraph{Input arguments}

 \begin{itemize}
 \item
   \texttt{S} {[} sstate {]} - Sstate object built on a steady-state
   file.
 \item
   \texttt{Fname} {[} char \textbar{} empty {]} - Filename of the
   compiled m-file function; if not specified or empty the original
   steady-state filename will be used with an '.m' extension.
 \end{itemize}

 \paragraph{Options}

 \begin{itemize}
 \item
   \texttt{'excludeZero='} {[} \texttt{true} \textbar{}
   \emph{\texttt{false}} {]} - Automatically detect and exclude zero
   solutions in blocks that result in multiple solutions.
 \item
   \texttt{'deleteSymbolicMfiles='} {[} \emph{\texttt{true}} \textbar{}
   \texttt{false} {]} - Delete auxiliary m-files created to call the
   Symbolic Math toolbox.
 \item
   \texttt{'end='} {[} numeric \textbar{} char \textbar{}
   \emph{\texttt{Inf}} {]} - Compile the steady-state m-file function
   only up to this block.
 \item
   \texttt{'simplify='} {[} numeric \textbar{} \emph{\texttt{Inf}} {]} -
   The minimum length for a symbolic expression to be simplified using
   the \texttt{simplify} function; Inf means no expressions will undergo
   simplification.
 \item
   \texttt{'start='} {[} numeric \textbar{} char \textbar{} \emph{1} {]}
   - Compile the steady-state m-file function starting from this block.
 \item
   \texttt{'symbolic='} {[} \emph{\texttt{true}} \textbar{}
   \texttt{false} {]} - Call the Symbolic Math toolbox to solve blocks
   marked with a \href{sstatelang/symbolic}{\texttt{!symbolic}} keyword;
   otherwise, all blocks will be solved numerically regardless of their
   specification.
 \end{itemize}

 \paragraph{Description}

 \paragraph{Example}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Symmetry analysis}

The symmetry analysis performed by \calcprog\ is relatively extensive
and flexible.  While the program doesn't find all
of the symmetry elements possessed by molecules, it does get a lot of
them.

In order to make the symmetry analysis as flexible as possible, the
molecule can first be moved to the center of mass frame of reference.
The
moments of inertia are then found and the whole molecule is rotated
into the principle axis frame.  This allows molecules which are not
located exactly at the origin or aligned perfectly with the Cartesian
axes to be analyzed.

The transformation to the principle axis frame is controlled by the
keyword {\sf Principle Axes}.  If this keyword is not specified, the
symmetry analysis will be done in the orientation specified in the
{\sf Geometry} section.

The program searches for the following
symmetry elements:

\begin{itemize}

\item {\bf an inversion center}

\item {\bf rotation axes} from C$_2$ through C$_8$ about the three
Cartesian axes.

\item {\bf improper rotation axes} from S$_3$ through S$_8$ about the
three Cartesian axes.

\item {\bf mirror planes} perpendicular to the Cartesian axes.

\end{itemize}

The elements found, their axes, and atoms which are equivalent under
each operation are printed to the output file.

The characters of the wavefunctions with respect to each operation are
determined by constructing the appropriate transformation matrix for
each operation and transforming the vector of atomic orbital
coefficients for each molecular orbital.  The result of this process
is the actual character of the wavefunction with respect to the
symmetry operation, not just a symmetric/anti-symmetric label.
It is important to realize that the results of this method of
displaying the results of symmetry analysis can give results which
are, at first, confusing for degenerate orbitals.  If you are looking
at the characters of a set of degerate orbitals and trying to compare
them to the characters given in a character table, it is very
important that you sum the characters of each of the members of the
degenerate set.

When a reaction coordinate is being followed, \calcprog\ first
generates all the geometries along the coordinate and determines the
symmetry elements which they possess.  The only symmetry elements
reported are those which are conserved along the entire distortion.
This means that you don't have to worry about moving from high to low
symmetry geometries or {\it vice versa}.  {\bf Note:} It is possible
that loss of symmetry elements will lead to problems in constructing a
Walsh diagram.  In these cases {\bf {\sf fit\_walsh}} will warn you.
If the diagram as constructed is incorrect, you can either change your
reaction coordinate to not include geometries with problematic
degeneracies or edit the {\tt .WALSH} file by hand to fix it.  This is
explained in more detail below in the section on fitting programs.

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