\section{Conclusions}
In this paper we presented the developed 3D implicit particle-in-cell code, which can be applied for various problems in the collisionless plasma. We have tested it and obtained results, that are consistent with theoretical predictions. We studied the influence of admixture of heavy ions on the evolution of the shock wave and simulations have shown that the presence of heavy ions increases the number of accelerated particles. Further research is necessary in order to study this phenomena in detail.
\ack
A M Bykov, P E  Gladilin and S M  Osipov have developed the implicit fields solver module, which provides conservation of the divergenceless magnetic field.
V I Romansky acknowledges support from RSF grant 16-12-10519 which was used to develop the
particle mover, parallelization and perform the testing of the code.

The results of the work were obtained using computational resources of Peter the Great Saint-Petersburg Polytechnic University Supercomputing Center (http://www.spbstu.ru).


\chapter{ISOCAM dedicated software}
\label{ch_iso}
% \chapterhead{ISOCAM dedicated software}
\markright{ISOCAM dedicated software}

The ISOCAM infrared camera is one of the four instruments on board the ISO
(Infrared Space Observatory) spacecraft which was launched successfully
on 1995 November 17. It operates in the 2.5-17 micron range, and was
developed by the ISOCAM consortium lead by the French Service d'Astrophysique of
CEA Saclay.
Some software have been developed using the \proj package.

For one observation, the ISOCAM instrument delivers a set of frame pairs (start of
integration {\em Reset} and end of integration (EOI)). For the
long wavelength detector (LW), the signal corresponds  to a simple difference
between EOI and Reset. For the short wavelength detector (SW), it is more complex,
and several operations such as  ``cross
talk correction'' must be done. We assume that these corrections have been
applied for the data we are considering here. Then we have a set of data  noted
$D(x,y,t,c)$: we
have one measurement per pixel position $(x,y)$ in the detector, which is repeated
$t$ times,
and we have $c$ configurations (there is a new configuration each time the
pointing position,
the filter, the integration time, ..., were changed).

Then, in the ideal case, calibration will consist of \cite{starck:sta98_4}
\begin{itemize}
\item normalizing the data to ADU/g.s by
\[ D_1(x,y,t,c) = \frac{D_0(x,y,t,c)}{gain*tint*Naccu} \]
where $gain$ is the electronic gain, $tint$ is the integration time, $Naccu$ is
the number of
frames already added  by the on-board processing ($Naccu$ is greater than one only
in the accumulation mode, normally confined to the $0.28$ seconds readouts
($Naccu=4$),
or the CAM parallel mode $Naccu=12$)).
\item subtract the DARK current
\[ D_2(x,y,t,c) = D_1(x,y,t,c) - dark(x,y) \]
The corresponding dark is extracted from the calibration library.
\item divide by the optical and the detector flats
\[ D_3(x,y,t,c) = \frac{D_2(x,y,t,c)}{oflat(x,y)dflat(x,y)} \]
The corresponding optical and detector flats are extracted from the calibration
library.
\item average the values corresponding to the same sky position and the same
configuration
\[ Image(x,y,c) = mean(D_3(x,y,1..t,c)) \]
\[ RMS(x,y,c) =  sigma(D_3(x,y,1..t,c)) \]
\item in case of a raster observation, reconstruct the final raster map
$Raster(x,y)$ from all images $Image(x,y,c)$.
\end{itemize}

But in practice, we have to take into account several problems:
\begin{itemize}
\item since the dark exhibits variation from one orbit to another, library
darks which are done on specific orbits do not always be applicable to
the data, specifically at low flux.
\item calibration flat field are done with an internal source, which
creates flats different from those coming through the telescope.
\item cosmic rays hit the detector.
\item the detector exhibits a transient behavior. Every time a detector pixel is
illuminated successively by a source and
the background, as the detector is scanning the sky, the transition between the
two flux levels is not instantaneous.
\item spacecraft jitter.
\item field of view distortion: the resolution of each pixel depends on its
position in the detector.
\item no signal is read from column 24.
\end{itemize}
Studies have been done in order to resolve all these problems, and
some solutions have been proposed.

\section{Cosmic ray impact suppression}
As the glitch structures can have different sizes, we need a multiresolution
tool in order to make an efficient automatic detection. The idea developed here is the following: as we observe
the same position in the sky during $n$ exposures, we cannot have any structure in our signal
which has a temporal size lower than $n*tint$ ($tint$ being the exposure time
of a single frame). It means that all the
significant structures (i.e. not due to the noise) at small scales
are due to the glitches.
The method consists in doing for each pixel $(x,y)$ the following steps
\begin{enumerate}
\item set $v(t) = C(x,y,t)$
\item estimate the temporal noise $\sigma_t$ in $v(t)$
\item calculate the multiresolution median transform  $T$ of $v(t)$: we get $T(j, t)$ where $j$ is the
temporal scale. The number of scales in the transform is fixed by the
number of frames per satellite position.
\item derive from $\sigma_t$ the standard deviation $\sigma_j$ at each scale $j$.
\item at all scales, set to zero all structure higher a given level.
\begin{eqnarray}
\mbox{ if } abs(T(j, t)) > k \sigma_j \mbox{ then } T(j, t) = 0
\end{eqnarray}
$k$ is generally taken equal to 3.
\end{enumerate}
The method is robust and works for non stabilized data. The only real limitation is that we
cannot detect glitches which are longer than or equal to $n*tint$. That
means that more we have frames per camera configuration, better will be the deglitch.

\begin{center}
 USAGE: mr1d\_deglitch option cube\_in cube\_out
\end{center}
where options are:
\begin{itemize}
\item {\bf [-n number\_of\_scales]} \\
number of scales used in the multiresolution transform. Default is 3.
\item {\bf [-s Nsigma]} \\
\item {\bf [-f]} \\
Suppress the noise, but not the glitches.
\end{itemize}

\begin{figure}[htb]
\centerline{
\vbox{
\psfig{figure=fig_deglitch2.ps,bbllx=2.5cm,bblly=13cm,bburx=19.5cm,bbury=25.5cm,width=17cm,height=12.5cm}
}}
\caption{Original data (top), deglitched data (middle), and both overplotted
(bottom).}
\label{fig_deglitch2}
\end{figure}

Figure \ref{fig_deglitch2} shows the results after
a such treatment. Figure \ref{fig_deglitch2} (top) shows the values
taken by a pixel of the camera as time elapses. The x-axis represents the frame
number (time / integration time), and the y-axis gives the ADU per second. These
data were collected during a raster observation, and the satellite stayed at the
same
position during around 20 frames, and the integration time was equal 2.1 second. A
source is at the limit of detection  (frames 130 to 150). All peaks
are due to cosmic ray impacts. The intensity of a cosmic ray can take all
values between a few ADUs to more than one thousand ADUs. Figure
\ref{fig_deglitch2}
(middle) shows the same data after the glitch suppression.
The third plot (Figure \ref{fig_deglitch2} (bottom)) shows both data and
deglitched data overplotted.
 We see that the noise
and the signal are not modified during this operation.

The method is robust and works for non stabilized data. The only real limitation
is that we
cannot detect glitches which are longer than or equal to $n*tint$. That
means that the more frames we have per camera configuration, the
better will be the deglitching.

\subsection*{IDL routine}
{\em mr1d\_deglitch} IDL routine eliminates glitches in
a cube $D$ calling the previous program. Each vector $D(s,y,*)$
at position $(x,y)$ is treated individually.
\begin{center}
     USAGE: mr1d\_deglitch, Cube, filter=filter, Nscale=Nscale, Nsigma=Nsigma, mask=mask
\end{center}
where
\begin{itemize}
\item {\em Cube}: 3D IDL array (input-ouput parameter). Cube to deglitch.
\item {\em Nscale}: number of scale for the multiresolution analysis.
\item {\em Nsigma}: glitch detection at Nsigma.
\item {\em filter}: if set, then an adaptive temporal filter is applied.
\item {\em mask}: output 3D IDL array: Mask where glitches have been detected.
\end{itemize}

\clearpage
\section{Dark Pattern removal in the Fourier space}
One has to subtract the dark from the image. However, the standard
procedure is not always  able to remove completely the visual dark
pattern. The dark pattern is visible due to signal variation between
odd and even lines in the image.
If the dark of the calibration library is applicable, then the operation
just consist of subtracting it from the data.
But in practice, due to small dfits in dark current, this dark is not always good
enough, and visible
artifacts can remain in the reduced image. For these reasons,
several methods have been developed \cite{iso:bure96,starck:tr_dark96}
 to accomplish good dark subtraction.
The dark pattern can be suppressed in the Fourier space by the
following method:

\begin{enumerate}
\item Average together all deglitched frames. we obtain $I_a$.

\item Eliminate in $I_a$ the low frequencies. we obtain $I_h$.

\item Estimate the noise in $I_h$, and set to zero all structures
higher than three times the noise standard deviation.

\item Compute the FFT $\hat{I_h}$ of $I_h$, and estimate the noise in the
real part $\hat{I_h}_r$, and imaginary part $\hat{I_h}_i$ of
$\hat{I_h}$.

\item Threshold all Fourier coefficients lower than the noise.
We get $\hat{T_h}_r$, $\hat{T_h}_i$.

\item Compute the inverse FFT transform of
$(\hat{T_h}_r$, $\hat{T_h}_i)$. Its real part gives the pattern
$P$.  The pattern $P$ can then be subtracted from the input image.
\end{enumerate}
As the program may also be used for other purpose, it is describes in
section~\ref{pat}.

\section{Calibration from pattern recognition and faint source detection}
The main difficulty in
dealing with ISOCAM faint source detection is the combination of the cosmic
ray impacts (glitches) and the transient behavior of the detectors.
Indeed, for glitches producing a single fast increase and decrease of
the signal, a simple median filtering allows a fairly good deglitching,
while for other glitches, memory effects can produce false
detections. Consequently, the major source of errors here is not the
detection limit of the instrument, which is quite low, but the large
number of glitches which create false detection.

\begin{figure}[htb]
\centerline{
\hbox{
\psfig{figure=fig_3glitch.ps,bbllx=2.cm,bblly=12.5cm,bburx=20cm,bbury=25.5cm,width=14cm,height=8cm}
}}
\caption{Examples of glitches. (a) this signal contains three
glitches of
the first type. (b) A glitch a long tail appears (named fader)
around the position 80. The
glitch has been truncated, and its real amplitude is 2700 ADUs. (c) A
glitch
with a negative tail (named deeper) appears around position 240.}
\label{fig_glitch3}
\end{figure}
Three types of glitches can be distinguished: (i) a positive strong and short feature (lasting one readout),
(ii) a positive tail (called {\em fader}, lasting a few readouts),
and (iii), a negative tail (called {\em dipper}, lasting  several tens of readouts).
Figure~\ref{fig_glitch3} is
a plot of the camera units (ADU, for Analog to Digital Units) measured
by a single pixel as a function of the number of readouts, i.e.  time,
which shows the three types of glitches.  On top (a), three sharp type "1"
glitches are clearly visible. On the middle plot (b), another pixel
history shows a "fader" (at about 80 readouts and lasting about 20
readouts). On the bottom plot (c), a "dipper" is present at  readout
230, which lasts about 150 readouts.

Finally, the signal measured by a single pixel as a function of time
is the combination of memory effects, cosmic ray impacts and real
sources: memory effects begin with the first readouts, since the
detector faces a flux variation from an offset position to the target
position (stabilization), then appear with long-lasting glitches and
following real sources.  One needs to clearly separate all these
constituents of the signal in each pixel before building a final
raster map and to keep the information of the associated noise before
applying a source detection algorithm.  Indeed, since the glitches do
not follow a gaussian statistic, it is clear that an analysis of the
final raster map would lead to poor results, for the standard
detection criteria (detection above N times the standard deviation of
the noise) would no more be valid. \\

The concept of pattern recognition
using a multiresolution algorithm leads to an efficient calibration
procedure, free of the major problems described above.
The idea developed here is to use the multiscale vision modeling for
a decomposition of a signal into its principal components. A simple
object reconstruction from the detected structured in the wavelet
space, as proposed in \cite{wave:bijaoui95}, will produce poor
results because of the strong confusion between the objects.
Furthermore
we need to extract positive and negative components, which imply that
 we may detect the wing around a negative structure as a positive
structure. The wings are a normal effect of the wavelet transform,
which
always create negative features around a positive component, as
well as
positive features around a negative component. Furthermore,
 the quality of the object reconstruction is good only when additional
 constraints are introduced (positivity constraint for positive
objects, and
negativity constraint for negative objects). An object is defined as
positive
(negative) when the wavelet coefficient of the object
which has the maximum absolute value is positive (negative).

A solution for limiting the confusion
is to use the knowledge we have about the objects. The problem
of unknown object reconstruction is reduced to a pattern recognition
problem. We search only for objects which verify given
conditions. For example, finding glitches of the first type is
equivalent to  finding
objects which are positive, strong, and with a temporal size lower
than that of the sources. The principal component decomposition
 method of the signal $D(t_0..t_n)$ is:
\begin{enumerate}
\item Search for the glitches of the first type. We get a signal $C_1(t_0..t_n)$, and
we calculate the deglitched data by $D_1 = D - C_1$.
\item Search for the negative components due to glitches: the multiscale
vision
model is applied to $D_1$, and negative objects are reconstructed: we
get $C_2(t_0..t_n)$, and we calculate $D_2 = D_1 - C_2$.
\item Search for the positive components due to glitches: this step must
be
done carefully, in order not to erase a source by mistake.
Positive object with a temporal size different from that of the
sources are automatically considered as glitches. Positive objects having
a negative slope cannot be an object either.
 We get $C_3(t_0..t_n)$, and we calculate $D_3 = D_2 - C_3$.
\item Search for sources components: the multiscale vision
model is applied to $D_3$, and positive objects with a correct
temporal
size are reconstructed: we get $C_4(t_0..t_n)$, and we calculate $D_4
= D_3 - C_4$.
\item Search for the baseline: the signal $D_4$ contains only noise and
sources at the noise level. The baseline is easily obtained by
convolving
$D_4$ by a low frequency pass band filter. We get $C_5(t_0..t_n)$.
\item The residual noise is obtained by $C_6 = D_4 - C_5$.
\end{enumerate}
The set $(C_1,C_2,C_3,C_4,C_5,C_6)$, represents the decomposition of
the
signal into its principal components. Note also that the input signal
$D$ is
equal to the sum of all components: $D = \sum_{i=1}^{6} C_i$.
A complete deglitching (all types of glitches) is obtained by
$  D_g = D - C_1 - C_2 - C_3 $,
and the signal  $D_b =  C_4 + C_6$ is background, dark, and glitch
free.
$D_b$ represents the set of data we need for the deep survey study.
The
background has been subtracted, and glitches with their long duration
effects
have been suppressed. Applying the pattern recognition method to all
detector pixels, we obtain a cube $D_b(x,y,t)$. All other component
signals $C_i$ are also cubes.
The baseline suppression presents several advantages: first the
final
raster map is dark-corrected without the need of a library dark.
This is particularly important when the library dark is  not good
enough,
and visual artifacts often remains \cite{starck:tr_idum96}.
Second,
the effect of the flat-field uncertainty is less severe, because the error
introduced by the flat-field correction on the background does not exist
anymore,
since the background has been removed.
\begin{figure}[htb]
\centerline{
\vbox{
\psfig{figure=fig_iso_plot7.ps,bbllx=2.5cm,bblly=13cm,bburx=19.5cm,bbury=25.5cm,width=14cm,height=8cm}
}}
\caption{Top, original data, and bottom, calibrated data (background
free).
The flux in ADU (Analogic Digital Unit) is plotted against time given by the number of exposures.
Note the gain variation of about 5 ADUs which appears after the
second glitch.}
\label{fig_calib}
\end{figure}

Figure~\ref{fig_calib} (bottom) presents the result after applying
such a  treatment. Original data are shown in Figure~\ref{fig_calib}
(top).
Figure~\ref{fig_decomp} shows the decomposition of the
 original signal (see figure~\ref{fig_calib} top) into its principal
components: (a), (b), and (d) are features (short glitch, glitch
negative tail, and baseline) which present no direct interest for
faint source detection, and
(c) and (e) (source and noise) must be considered. The noise must also
be kept because faint sources could be undetectable in a single
temporal signal, but
detectable after co-addition of the data.
The simple sum of the fives components is exactly equal to the
original data (see figure~\ref{fig_calib} top). The calibrated
background free data
(see figure~\ref{fig_calib} bottom) are then obtained by addition of
(c) and (e).

\begin{figure}[htb]
\centerline{
\vbox{
\psfig{figure=fig_iso_plot6.ps,bbllx=2.cm,bblly=13cm,bburx=19.5cm,bbury=25.5cm,width=17cm,height=12.5cm}
}}
\caption{Decomposition of the signal into its principal components:
(a) short glitch, (b) glitch negative tail, (c) source, (d) baseline,
(e) noise. The
simple sum of the fives components is exactly equal to the original
data (see
figure 2). The calibrated background free data are obtained by
addition of signals (c) and (e).}
\label{fig_decomp}
\end{figure}

The {\em mr\_isocalib} applies the calibration from pattern recognition.
The first input (raw data + observation parameters) file should be created
using the {\em mosaic2fits} routine from the CAM Interactive Analysis package.
\begin{center}
 USAGE: mr\_isocalib option cube\_in cube\_in\_or\_out
\end{center}
where options are:
\begin{itemize}
\item {\bf [-n number\_of\_scales]} \\
Number of scales used in the multiresolution transform for the short glitches
removal.
Default is 3.
\item {\bf [-N number\_of\_scales]} \\
Nnumber of scales used for the suppresion of long glitches.
Default is 8.
\item {\bf [-g sigma]} \\
sigma = noise standard deviation. By default, it is automatically
estimated for each temporal vector.
\item {\bf [-s Nsigma]} \\
Detection level at nsigma * SigmaNoise.
\item {\bf [-d]} \\
Apply a short  glitches removal.  Default is no.
\item {\bf [-D]} \\
Apply a long glitches removal. Default is no.
\item {\bf [-b]} \\
Suppress the base line. Default is no.
\item {\bf [-B number\_of\_scales]} \\
 Number of scales used for baseline suppression.
\item {\bf [-f]} \\
Flat field correction. Default is no.
\item {\bf [-t]} \\
 Transient correction. Default is no.
\item {\bf [-K BorderNumber]} \\
Do not take into account the border.
\item {\bf [-a]} \\
Average the frames belonging to the same sky position. Default is no.
\item {\bf [-r]} \\
Create a rasmap.
\item {\bf [-S NSigma\_Long\_Glitch]} \\
NSigma for long glitch detection. Default is 5.
\item {\bf [-F]} \\
Kill detected objects containing more than 50 masked  pixels.
\item {\bf [-G]} \\
Kill detected objects if the first 3 pixels are masked .
\item {\bf [-M]} \\
Mask pixels where long glitches have been detected.
\item {\bf [-A]} \\
(for all) Apply a flat field correction, a short and long glitches removal,
a baseline suppression, average the frame, create the raster map. It is equivalent
to options "-d -D -f -b -a -r".
\item {\bf [-L]} \\
Use the slope of an object for source/glitch separation.
\item {\bf [-m]} \\
Creates a micro-scan map instead of a raster map.
\end{itemize}

There is two modes for using the {\em mr\_isocalib} program: \\
If option "-d" and/or "-D" is set then the second file of the command
line is an output file, which contains the deglitched data cube, without
the baseline (if "-b" is set), and hence contains only the
noise and the sources. In this case  the program will also create
a set of other files:
\begin{itemize}
\item xx\_baddata.fits: data cube which contains the detected long glitches.
\item xx\_mask.fits: data cube which contains the mask. mask(x,y,z) equal
to 1 if a short glitch is found at position (x,y,z). If "-M" is set,
positions were long glitches have been detected are set also to 1.
\item xx\_bgr.fits: data cube which contains the baseline.
\item xx\_sigmanoise.fits: image which contains the estimated noise for each
detector pixel.
\item xx\_source.fits: data cube which contains the detected sources.
\end{itemize}

If neither "-d" or "-D" is set (second mode), all the files described above are read
by {\em mr\_isocalib}. This allows us to treat in several steps the data.

other created files are created:
\begin{itemize}
\item Results concerning the flat field (only if "-f" is set):
\begin{itemize}
\item "xx\_auto\_flat.fits": estimated flat field.
\item "xx\_rms\_flat.fits": RMS of the estimated flat field.
\end{itemize}
\item Results concerning the reduced data (frame at the same sky position
have been averaged). Only if "-r" or/and "-f", or/and "-m" is set.
\begin{itemize}
\item "xx\_reduce.fits": reduced and flat-fielded (if "-f") cube.
\item "xx\_rms.fits": RMS cube of the averaged cube.
\item "xx\_npix.fits": number of pixels used for pixel of the averaged cube.
\item "xx\_rms\_noise.fits": Estimated noise on the averaged cube.
\item "xx\_raster.fits": raster scan or micro scan final map.
\item "xx\_rmsraster.fits": RMS of the final map
\item "xx\_npixraster.fits": number of pixels used
 for the final map calculation.
\end{itemize}
\item Results concerning the baseline:
\begin{itemize}
\item "xx\_bgrreduce.fits"
\item "xx\_bgrrms.fits"
\item "xx\_bgrnpix.fits"
\item "xx\_bgrraster.fits"
\item "xx\_bgrrmsraster.fits"
\item "xx\_bgrnpixraster.fits"
\end{itemize}
\item Results concerning the bad data (long glitches):
\begin{itemize}
\item "xx\_badreduce.fits"
\item "xx\_badraster.fits"
\end{itemize}
\end{itemize}

\clearpage
\section{transient fit}

There are  different methods to achieve stabilization which have
been tested.
The best  method try to fit the transients using a
model, and then suppress them. See \cite{iso:delattre96} for a description of each of them. A transient can be split into two parts:
\begin{itemize}
\item a first part where data are quickly decreasing and they can
be fitted by an downward exponential.
\item a second part where data are decreasing very slowly but which
cannot be seen as the tail of the decreasing exponential of the first
part.
\end{itemize}
The best model takes into account this behaviour, and proposes a transient
function with two relaxation times:

\begin{itemize}
\item For downward transient
	\[ J_d(t)\,=\,J_{\infty} + (J_0 - J_{\infty}).\beta.exp(- {t \over \tau}) + \beta' . exp(- {t \over \tau'}) \]
where $\tau' >> \tau$ ;

\item and for upward transient
	\[ J_u(t)\,=\, J_{\infty} - (J_{\infty} - J_0).\beta.exp(- {t \over \tau}) \]
\end{itemize}
where $J_{\infty}$ is the stabilized value (observed flux), and $J_0$ is
the starting value (observed flux at the previous satellite configuration). This model has six parameters $J_0$,$J_{\infty}$, $\beta$, $\beta'$, $\tau$, $\tau'$.
The low decrease of the transient is modeled by a larger time constant
$\tau'$.  This model has two advantages: it leads to a complete fit of
the data (and not only the beginning or the end of the transient), and
it also allows us to work with the first points to guess the
stabilized value $J_{\infty}$.

\begin{center}
 USAGE: transient\_fit option cube\_in cube\_out
\end{center}
where options are:
\begin{itemize}
\item {\bf [-d downward\_model]} \\
for fitting downward transients.
\item {\bf [-e ending\_scd]} \\
e = position of the ending scd to be considered  for fitting transients.
\item {\bf [-g]} \\
Global correction. upward \& downward transients are fitted over all the scds.
 \item {\bf [-m method]} \\
m = name of the method used for the minimization of the merit-function:
'cauchy','exponential','gaussian'.
\item {\bf [-n number\_of\_iterations]} \\
n = max number of iterations when fitting the model with the data.
\item {\bf [-o]} \\
overshoot upward model.
\item {\bf [-s starting\_scd]} \\
s = position of the starting scd to be considered  for fitting transients.
\item {\bf [-t down\_threshold]} \\
t = the greatest negative value of the slope of a downward transient above
which   no fitting is undertaken.
\item {\bf [-T up\_Threshhold]} \\
T = the lowest positive value of the slope of a upward transient beneath
 which no fitting is undertaken.
\item {\bf [-u upward\_model]} \\
for fitting upward transients.
\end{itemize}

\begin{figure}[htb]
\centerline{
\hbox{
\psfig{figure=fig_ex_transient_CEA.ps,bbllx=2.5cm,bblly=2cm,bburx=18.5cm,bbury=13.1cm,width=16cm,height=11cm}}}
\caption{Example of transient fit using the SACLAY model.}
\label{fig_trans_cea1}
\end{figure}

Figure \ref{fig_trans_cea1} shows the result of a fit. The detector pixel values
  are represented in solid line. The pixel detector is observing
a flux of around three ADUs. We see that even after
eighty frames, the flux is not stabilized. Overplotted to the data, the
model is plotted, and in the bottom of the figure, we see the data corrected
from the transient effect.
\clearpage

\section{Field of View Distortion Correction}
  Field distortion in ISOCAM is mostly due to the off-axis Fabry mirror
that directs the light beam toward each detector. The field distortion
was measured for the  LW channel 6 and 3 arcsec PFOV lens, using calibration field observations that contains many stars. No measurements are planned for the 1.5 arcsec PFOV
because they are difficult to make due to the satellite jitter which is of
the order of the quantities to be measured. Moreover, results at 6
and 3 arcsec are in good agreement with the predicted (ray-tracing)
and measured pre launch values. Since the distortion at 1.5 arcsec
PFOV is predicted to be negligeable,
no error should be made if it is not taken into account.

 ISOCAM suffers from lens jitter. In order to avoid any
mechanical blocking, the gear wheel has been designed with small
play.  Therefore, the position at which the lens stops is not fixed.  It has been
shown by the CAM Instrument Dedicated Team (CIDT) and by us that there
are only two positions that the lens wheel can take for a commanded
position, and it is suspected that the wheel stops at either side of
the play.  This can be very easily detected by a close inspection of
the flat field derived from the data : the leftmost column of the
detector receives very little light.  This is called the `left' position.
This jitter results in an offset of about 1.2 pixels of the optical
axis, thus $\approx$ 7" with the 6" PFOV lens. It modifies also the
distortion pattern of ISOCAM that has been measured for both
positions. The measurment method is discussed in \cite{starck:aussel98}.

Following the work done on the HST WFPC by \cite{iso:holtzman95},  each
measurment is fitted with a general polynomial of degree 3, that is :
\begin{equation}
x_{c} = a_{0}+ a_{1}x + a_{2} y + a_{3}x^{2}+ a_{4}x y + a_{5}y^{2} +
a_{6}x^{3}+ a_{7}x^{2}y + a_{8}xy^{2}+ a_{9}y^{3}
\end{equation}
\begin{equation}
y_{c} = b_{0}+ b_{1}x + b_{2} y + b_{3}x^{2}+ b_{4}x y + b_{5}y^{2} +
b_{6}x^{3}+ b_{7}x^{2}y + b_{8}xy^{2}+ b_{9}y^{3}
\end{equation}

where $x_{c}$ and $y_{c}$ are the positions on the ISOCAM LW array in
pixels, corrected for distortion, while $x$ and $y$ are the non-corrected ones. Figure \ref{fig_disto} shows a map of the
distortion of the LW channel of ISOCAM with the 6" PFOV, where each vector starts from
where the center of a pixel should fall if they were no distortion and
ends at its actual position.  The length of the vectors are at the
scale of the plot. At the lower corners of the array (lines 0 to 5),
the effect is greater than one pixel.

Since the pixel size is not uniform on the sky, the pixels at the edges of
the array cover a wider surface. Therefore, a new flat-field correction has
to be applied in order to account for it. This flat-field is of the
form :
\begin{equation}
F_{i,j} = \frac{S_{16,16}}{S_{i,j}}
\end{equation}
with $S_{i,j}$ , the surface on the sky of pixel i,j.  The pixel
(16,16), being the center pixel of ISOCAM LW array and therefore the
less distorted, has been taken as reference.

Once corrected for this effect, each raster sub-image is projected on
the raster map, using a flux-conservative shift and add algorithm.
The intersecting surface $S_{(x,y,i,j)}$ of each sky pixel of the raster map with each
ISOCAM pixel is computed. The flux in the pixel (x,y)
of the raster map is therefore:

\begin{equation}
R_{x,y} = \frac{\sum_{pointings}S_{(x,y,i,j)}\sqrt{N_{i,j}}I_{i,j}}
{\sum_{pointings}S_{(x,y,i,j)}\sqrt{N_{i,j}}}
\end{equation}
Assuming gaussian distributions for pixels, the noise
map is:
\begin{equation}
\sigma_{x,y} = \sqrt{\frac{\sum_{pointings}S^{2}_{(x,y,i,j)}N_{i,j}\sigma^{2}_{i,j}}
{\sum_{pointings}S^{2}_{(x,y,i,j)}N_{i,j}}}
\end{equation}
Where $R_{x,y}$ is the value of the final raster map at (x,y),
$S_{(x,y,i,j)}$ is the intercepted surface between ISOCAM pixel (i,j)
and raster map pixel (x,y) and $N_{i,j}$is the number of readouts that
where averaged together to produce $I_{i,j}$, the image of the raster
pointing. The computation of $S_{(x,y,i,j)}$ is derived from the
algorithm used by Fruchter et al. in their `drizzle' IRAF task.
The files containing the coefficients for the distortion
correction are available from the authors under a format accepted by
this task.

\begin{center}
 USAGE: projection  options input1 input2 ... output
\end{center}
where options are:
\begin{itemize}
\item {\bf [-R CROTA2]} \\
Creates a map with given {\em CROTA2} (decimal degrees).
Default is 0 .
\item {\bf [-d CDELT1]} \\
Creates a map with a given resolution (in arcsec). \\
if {\em CDELT1} is set and not {\em CDELT2}, then  \\
                 CDELT1 = -{\em CDELT1} and CDELT2 = {\em CDELT1} \\
to ensure North top and West left astronomical convention  with CROTA2=0 images.
\item {\bf [-D CDELT2]} \\
 Creates a map with a given CDELT2 (in arcsec). CDELT1 must have been provided.
\item {\bf [-M map]} \\
 Will use the orientation of the map input number map for the result map.
\item {\bf [-C]} \\
Will produce as many outputs as inputs, with the same astrometry,
but without coadding the maps. Fits files will be numbered from 1 to n
after the given output filename.
\item {\bf [-o output file name]} \\
Output file name. Default is "result.fits".
\item {\bf [-x line of pixel to be examined]} \\
Default is -1000.
\item {\bf [-y column of pixel to be examined]} \\
 Default is -1000.
\end{itemize}

\begin{figure}[htb]
\centerline{
\hbox{
\psfig{figure=velo.ps,bbllx=2.7cm,bblly=5.5cm,bburx=19cm,bbury=22.5cm,width=11cm,height=11cm}}}
\caption{ISOCAM field of view distortion.}
\label{fig_disto}
\end{figure}


\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[margin=1in]{geometry}
\usepackage{palatino}
\usepackage{amsthm,amsmath,amssymb}
\usepackage{xcolor}
\usepackage[utf8]{inputenc}
\usepackage[colorlinks=true,urlcolor=blue]{hyperref}
\usepackage{listings}
\newcommand{\task}{\par\noindent\emph{Task:}\ }

\definecolor{darkblue}{rgb}{0,0,0.5}
\definecolor{lightgray}{gray}{0.75}

\lstset{basicstyle=\ttfamily\small, numbers=left,
  xleftmargin=3em,extendedchars=true,
  numberstyle=\tiny\color{lightgray}, keywordstyle=\color{darkblue}, morekeywords={and,
    as, begin, check, do, done, downto, else, end, effect, external, finally,
    for, fun, function, handle, handler, if, in, match, let, new, of, operation,
    perform, rec, val, while, to, type, then, with},
literate=%
    {→}{{$\rightarrow$}}1%
    {×}{{$\times$}}1%
    {←}{{$\leftarrow$}}1%
    {↦}{{$\mapsto$}}1%
    {↝}{{$\leadsto$}}1%
    {…}{{$\ldots$}}1%
    {⇒}{{$\Rightarrow$}}1%
    {∈}{{$\in$}}1%
    {≡}{{$\equiv$}}1%
    {λ}{{$\lambda$}}1%
    {⊢}{{$\vdash$}}1%
    {κ}{{$\kappa$}}1%
    {Σ}{{$\Sigma$}}1%
    {Δ}{{$\Delta$}}1%
    {Γ}{{$\Gamma$}}1%
    {Θ}{{$\Theta$}}1%
    {₀}{{${}_0$}}1%
    {₁}{{${}_1$}}1%
    {₂}{{${}_2$}}1%
    {ᵢ}{{${}_\mathtt{i}$}}1%
    {ⱼ}{{${}_\mathtt{j}$}}1%
}

{\theoremstyle{definition}
\newtheorem{problem}{Problem}[section]}

\begin{document}
\title{Algebraic effects and handlers\\(OPLSS 2018 lecture notes)}
\author{Andrej Bauer\\University of Ljubljana}
\date{July 2018}

\maketitle

These are the notes and materials for the lectures on algebraic effects and
handlers at the
\href{https://www.cs.uoregon.edu/research/summerschool/summer18/index.php}{Oregon
  programming languages summer school 2018 (OPLSS)}. The notes were originally
written in Markdown and converted to {\LaTeX} semi-automatically, please excuse
strange formatting. You can find all the resources at
\href{https://github.com/OPLSS/introduction-to-algebraic-effects-and-handlers}{the
  accompanying GitHub repository}.

The lectures were recorded on video that are available at the summer school web
site.

\hypertarget{general-resources-reading-material}{%
\subsubsection*{General resources \& reading
material}\label{general-resources-reading-material}}

\begin{itemize}
\item
  \href{https://github.com/yallop/effects-bibliography}{Effects
  bibliography}
\item
  \href{https://github.com/effect-handlers/effects-rosetta-stone}{Effects
  Rosetta Stone}
\item
  \href{http://www.eff-lang.org}{Programming language Eff}
\end{itemize}

\hypertarget{what-is-algebraic-about-algebraic-effects-and-handlers}{%
\section{What is algebraic about algebraic effects and
handlers?}\label{what-is-algebraic-about-algebraic-effects-and-handlers}}

The purpose of the first two lectures is to review algebraic theories and
related concepts, and connect them with computational effects. We shall start on
the mathematical side of things and gradually derive from it a programming
language.

\hypertarget{outline}{%
\subsubsection*{Outline}\label{outline}}

Pretty much everything that will be said in the first two lectures is written
up in \href{https://arxiv.org/abs/1807.05923}{``What is algebraic about
algebraic effects and handlers?''}, which still a bit rough around the
edges, so if you see a typo please
\href{https://github.com/andrejbauer/what-is-algebraic-about-algebraic-effects}{let
me know}.

Contents of the first two lectures:
%
\begin{itemize}
\item
  signatures, terms, and algebraic theories
\item
  models of an algebraic theory
\item
  free models of an algebraic theory
\item
  generalization to parameterized operations with arbitrary arities
\item
  sequencing and generic operations
\item
  handlers
\item
  comodels and tensoring of comodels and models
\end{itemize}

\hypertarget{problems}{%
\subsection{Problems}\label{problems}}

Each section contains a list of problems, which are ordered roughly in the order
of difficulty, either in terms of trickiness, the amount of work, or
prerequisites. I recommend that you discuss the problems in groups, and pick
whichever problems you find interesting.

\begin{problem}[The theory of an associative unital operation]
Consider the theory $T$ of an associative operation with a unit.
It has a constant $\epsilon$ and a binary operation $\cdot$
satisfying equations
%
\begin{align*}
(x \cdot y) \cdot z &= x \cdot (y \cdot z) \\
\epsilon \cdot x &= x \\
x \cdot \epsilon &= x
\end{align*}
%
Give a useful description of the free model of $T$ generated by a
set $X$. You can either guess an explicit construction of free
models and show that it has the required universal property, or you can
analyze the free model construction (equivalence classes of well-founded
trees) and provide a simple description of it.
\end{problem}

\begin{problem}[The theory of apocalypse]
We formulate an algebraic theory $\mathsf{Time}$ in it is possible to
explicitly record passage of time. The theory has a single unary
operation $\mathsf{tick}$ and no equations. Each application of
$\mathsf{tick}$ records the passage of one time step.

\task Give a useful description of the free model of the
theory, generated by a set $X$.

\task Let a given fixed natural number $n$ be given.
Describe a theory $\mathsf{Apocalypse}$ which extends the theory
$\mathsf{Time}$ so that a computation crashes (aborts, necessarily
terminates) if it performs more than $n$ of ticks. Give a useful
description of its free models.

Advice: do \emph{not} concern yourself with any sort of operational
semantics which somehow ``aborts'' after $n$ ticks. Instead, use
equations and postulate that certain computations are equal to an
aborted one.
\end{problem}

\begin{problem}[The theory of partial maps]

The models of the empty theory are precisely sets and functions. Is
there a theory whose models form (a category equivalent to) the category
of sets and \emph{partial} functions?

Recall that a partial function $f : A \hookrightarrow B$ is an ordinary
function $f : S \to B$ defined on a subset $S \subseteq A$.
(How do we define composition of partial functions?)
\end{problem}

\begin{problem}[Models in the category of models]

In \href{https://arxiv.org/abs/1807.05923}{Example 1.27 of the reading
material} it is calculated that a model of the theory $\mathsf{Group}$ in
the category $\mathsf{Mod}(\textsf{Group})$ is an abelian group. We may generalize
this idea and ask about models of theory $T_1$ in the category of
models $\mathsf{Mod}(T_2)$ of theory $T_2$.

The \textbf{tensor product $T_1 \otimes T_2$} of algebraic theories
$T_1$ and $T_2$ is a theory such that the category of models
of $T_1$ in the category $\mathsf{Mod}(T_2)$ is equivalent to the
category of models of $T_1 \otimes T_2$.

Hint: start by outlining what data is needed to have a $T_1$-model
in $\mathsf{Mod}(T_2)$ is, and pay attention to the fact that the
operations of $T_1$ must be interpreted as
$T_2$-homomorphisms. That will tell you what the ingredients of
$T_1 \otimes T_2$ should be.
\end{problem}

\subsubsection{Problem: Morita equivalence}

It may happen that two theories $T_1$ and $T_2$ have
equivalent categories of models, i.e.,
%
\begin{equation*}
\mathsf{Mod}(T_1) \simeq \mathsf{Mod}(T_2)
\end{equation*}
%
In such a case we say that $T_1$ and $T_2$ are
\textbf{Morita equivalent}.

Let $T$ be an algebraic theory and $t$ a term in context
$x_1, \ldots, x_i$. Define a \textbf{definitional extension
$T + (\mathsf{op} {{:}{=}} t)$} to be the theory $T$ extended with an
additional operation $\mathsf{op}$ and equation
%
\begin{equation*}
x_1, \ldots, x_i \mid \mathsf{op}(x_1, \ldots, x_i) = t
\end{equation*}
%
We say that $\mathsf{op}$ is a \textbf{defined operation}.
%
\task Confirm the intuitive feeling that
$T + (\mathsf{op} {{:}{=}} t)$ by proving that $T$ and
$T + (\mathsf{op} {{:}{=}} t)$ are Morita equivalent.

\task Formulate the idea of a definitional extension so that
we allow an arbitrary set of defined operations, and show that we still
have Morita equivalence.

\begin{problem}[The theory of a given set]
Given any set $A$, define the \textbf{theory $T(A)$ of the set $A$} as follows:
%
\begin{itemize}
\item
  for every $n$ and every map $f : A^n \to A$,
  $\mathsf{op}(f)$ is an $n$-ary operation
\item
  for all $f : A^i \to A$, $g : A^j \to A$ and
  $h_1, \ldots, h_i : A^j \to A$, if
  %
  \begin{equation*}
    f \circ (h_1, \ldots, h_i) = g
  \end{equation*}
  %
  then we have an equation
  %
  \begin{equation*}
    x_1, \ldots, x_j \mid \mathsf{op}(f)(\mathsf{op}(h_1)(x_1,\ldots,x_j), \ldots, h_i(x_1,\ldots,x_j)) = g(x_1, \ldots, x_j)
  \end{equation*}
\end{itemize}

\task Is $T(\{0,1\})$ Morita equivalent to another, well-known algebraic theory?
\end{problem}

\begin{problem}[A comodel for non-determinism]
  In \href{https://arxiv.org/abs/1807.05923}{Example 4.6 of the reading
    material} it is shown that there is no comodel of non-determinism in the
  category of sets. Can you suggest a category in which we get a reasonable
  comodel of non-determinism?
\end{problem}

\begin{problem}[Formalization of algebraic theories]

If you prefer avoiding doing Real Math, you can formalize algebraic
theories and their comodels in your favorite proof assistant. A possible
starting point is
\href{https://gist.github.com/andrejbauer/3cc438ab38646516e5e9278fdb22022c}{this
gist}, and a good goal is the construction of the free model of a theory
generated by a set (or a type).

Because the free model requires quotienting, you should think ahead on
how you are going to do that. Some possibilities are:
%
\begin{itemize}
\item
  use homotopy type theory and make sure that the types involved are
  h-Sets
\item
  use setoids
\item
  suggest your own solution
\end{itemize}
%
It may be wiser to first show as a warm-up exercise that theories
without equations have initial models, as that only requires the
construction of well-founded trees (which are inductive types).
\end{problem}

\section{Designing a programming language}

Having worked out algebraic theories in previous lectures, let us turn the
equational theories into a small programming language.

What we have to do:
%
\begin{enumerate}
\item Change mathematical terminology to one that is familiar to programmers.
\item Reuse existing concepts (generators, operations, trees) to set up the overall
   structure of the language.
\item Add missing features, such as primitive types and recursion, and generally
   rearrange things a bit to make everything look nicer.
\item Provide operational semantics.
\item Provide typing rules.
\end{enumerate}

\subsection{Reading material}

There are many possible ways and choices of designing a programming language
around algebraic operations and handlers, but we shall mostly rely on Matija
Pretnar's tutorial \href{http://www.eff-lang.org/handlers-tutorial.pdf}{An
  Introduction to Algebraic Effects and Handlers. Invited tutorial paper}. A
more advanced treatment is available in
\href{https://arxiv.org/abs/1306.6316}{An effect system for algebraic effects
  and handlers}.

\subsection{Change of terminology}

\begin{itemize}
\item The elements of $\mathsf{Free}_\Sigma(V)$ are  are \textbf{computations} (instead of trees).
\item The elements of $V$ are \textbf{values} (instead of generators).
\item We speak of \textbf{value types} (instead of sets of generators).
\item We speak of \textbf{computation type} (instead of free models).
\end{itemize}

Henceforth we ignore equations.

\subsection{Abstract syntax}

We add only one primitive type, namely $\mathsf{bool}$. Other constructs
(integers, products, sums) are left as exercises.

\noindent
Value:
%
\begin{lstlisting}
v ::= x              (variable)
    | false          (boolean constants)
    | true
    | h              (handler)
    | λ x . c        (function)
\end{lstlisting}
%
Handler:
%
\begin{lstlisting}
h ::= handler { return x ↦ c_ret, ... opᵢ(x, κ) ↦ cᵢ, ... }
\end{lstlisting}
%
Computation:
%
\begin{lstlisting}
c ::= return v               (pure computation)
    | if v then c₁ else c₂   (conditional)
    | v₁ v₂                  (application)
    | with v handle c        (handling)
    | do x ← c₁ in c₂        (sequencing)
    | op (v, λ x . c)        (operation call)
    | fix x . c              (fixed point)
\end{lstlisting}
%
We introduce \textbf{generic operations} as syntactic abbreviation and let
$\mathsf{op}\;v$ stand for $\mathsf{op}(v, \lambda x . \mathsf{return}\; x)$.

\subsection{Operational semantics}

We provide small-step semantics, but big step semantics can also be given (see
reading material). In the rules below \lstinline{h} stands for
%
\begin{lstlisting}
handler { return x ↦ c_ret, ... opᵢ(x,y) ↦ cᵢ, ... }
\end{lstlisting}
%
We write \lstinline{e₁[e₂/x]} for \lstinline{e₁} with \lstinline{e₂} substituted
for \lstinline{x}. The operational rules are:
%
\begin{lstlisting}
________________________________
(if true then c₁ else c₂)  ↦  c₁

_________________________________
(if false then c₁ else c₂)  ↦  c₂

______________________
(λ x . c) v  ↦  c[v/x]

_____________________________________
with h handle return v  ↦  c_ret[v/x]

_____________________________________________________________
with h handle opᵢ(v,κ)  ↦  cᵢ[v/x, (λ x . with h handle κ x)/y]

_________________________________
do x ← return v in c₂  ↦  c₂[v/x]

_______________________________________________________
do x ← op(v, κ) in c₂  ↦  op(v, λ y . do x ← κ y in c₂)

______________________________
fix x . c  ↦  c[(fix x . c)/x]
\end{lstlisting}

\subsection{Effect system}

\subsubsection{Value and computation types}

Value type:
%
\begin{lstlisting}
A, B := bool | A → C | C ⇒ D
\end{lstlisting}
%
Computation type:
%
\begin{lstlisting}
C, D := A!Δ
\end{lstlisting}
%
Dirt:
%
\begin{lstlisting}
Δ ::= {op₁, …, opⱼ}
\end{lstlisting}
%
The idea is that a computation which returns values of type \lstinline{A} and
\emph{may} perform operations \lstinline{op₁, …, opⱼ} has the computation type
\lstinline|A!{op₁, …, opⱼ}|.

\subsubsection{Signature}

We presume that some way of declaring operations is given, i.e., that we have a
signature \lstinline{Σ} which lists operations with their parameters and
arities:
%
\begin{lstlisting}
Σ = { …, opᵢ : Aᵢ ↝ Bᵢ, … }
\end{lstlisting}
%
Note that the the parameter and the arity types \lstinline{Aᵢ} and
\lstinline{Bᵢ} are both value types.

\subsubsection{Typing rules}

A typing context assigns value types to free variables:
%
\begin{lstlisting}
Γ ::= x₁:A₁, …, xᵢ:Aᵢ
\end{lstlisting}
%
We think of \lstinline{Γ} as a map which takes variables to their types.

There are two forms of typing judgment:
%
\begin{enumerate}
\item \lstinline{Γ ⊢ v : A} -- value \lstinline{v} has value type \lstinline{A} in context \lstinline{Γ}
\item \lstinline{Γ ⊢ c : C} -- computation \lstinline{c} has computation type \lstinline{C} in context \lstinline{Γ}
\end{enumerate}

Rules for value typing:
%
\begin{lstlisting}
Γ(x) = A
_________
Γ ⊢ x : A

________________
Γ ⊢ false : bool

________________
Γ ⊢ true : bool

Γ, x : A ⊢ c_ret : B!Θ
Γ, x : Pᵢ, κ : Aᵢ → B!Θ ⊢ cᵢ : B!Θ  (for each opᵢ : Pᵢ ↝ Aᵢ in Δ)
_______________________________________________________________________
Γ ⊢ (handler { return x ↦ c_ret, ... opᵢ(x) κ ↦ cᵢ, ... }) : A!Δ ⇒ B!Θ

   Γ, x:A ⊢ c : C
_____________________
Γ ⊢ (λ x . c) : A → C
\end{lstlisting}
%
Rules for computation typing:
%
\begin{lstlisting}
    Γ ⊢ v : A
__________________
Γ ⊢ return v : A!Δ

Γ ⊢ v : bool    Γ ⊢ c₁ : C    Γ ⊢ c₂ : C
________________________________________
    Γ ⊢ (if v then c₁ else c₂) : C

Γ ⊢ v₁ : A → C    Γ ⊢ v₂ : A
____________________________
   Γ ⊢ v₁ v₂ : C

Γ ⊢ v : C ⇒ D     Γ ⊢ c : C
___________________________
 Γ ⊢ (with v handle c) : D

Γ ⊢ c₁ : A!Δ    Γ, x:A ⊢ c₂ : B!Δ
_________________________________
  Γ ⊢ (do x ← c₁ in c₂) : B!Δ

Γ ⊢ v : Aᵢ    opᵢ ∈ Δ    opᵢ : Aᵢ ↝ Bᵢ
___________________________________
        Γ ⊢ op v : Bᵢ!Δ

 Γ, x:A ⊢ c : A!Δ
_____________________
Γ ⊢ (fix x . c) : A!Δ
\end{lstlisting}

\subsection{Safety theorem}

If \lstinline{⊢ c : A!Δ} then:
%
\begin{enumerate}
\item \lstinline{c = return v} for some \lstinline{⊢ v : A} \emph{or}
\item \lstinline{c = op(v, κ)} for some \lstinline{op ∈ Δ} and some value \lstinline{v} and continuation \lstinline{κ}, \emph{or}
\item \lstinline{c ↦ c'} for some \lstinline{⊢ c' : A!Δ}.
\end{enumerate}
%
For a mechanized proof see \href{https://arxiv.org/abs/1306.6316}{An effect
  system for algebraic effects and handlers}.

\subsection{Other considerations}

The effect system suffers from the so-called \emph{poisoning}, which can be
resolved if we introduce \textbf{effect subtyping}. Recursion requires that we
use domain-theoretic denotational semantics. Such a semantics turns out to be
adequate (but not fully abstract for the same reasons that domain theory is not
fully abstract for PCF). See \href{https://arxiv.org/abs/1306.6316}{An effect
  system for algebraic effects and handlers} where the above points are treated
carefully.

\subsubsection{Problems}

\begin{problem}[Products]

Add simple products \lstinline{A × B} to the core language:
%
\begin{enumerate}
\item Extend the syntax of values with pairs.
\item Extend the syntax of computations with an elimination of pairs, e.g., \lstinline{do (x,y) ← c₁ in c₂}.
\item Extend the operational semantics.
\item Extend the typing rules.
\end{enumerate}
\end{problem}

\begin{problem}[Sums]

Add simple sums \lstinline{A + B} to the core language:
%
\begin{enumerate}
\item Extend the syntax of values with injections.
\item Extend the syntax of computations with an elimination of sums (a suitable \lstinline{match} statement).
\item Extend the operational semantics.
\item Extend the typing rules.
\end{enumerate}
\end{problem}

\begin{problem}[\lstinline{empty} and \lstinline{unit} types]
Add the \lstinline{empty} and \lstinline{unit} types to the core language. Follow the same steps as
in the previous exercises.
\end{problem}

\begin{problem}[Non-terminating program]
Define a program which prints infinitely many booleans. You may assume that the
\lstinline{print : bool → unit} operation is handled appropriately by the runtime
environment. For extra credit, make it "funny".
\end{problem}

\begin{problem}[Implementation]
Implement the core language from Matija Pretnar's
\href{http://www.eff-lang.org/handlers-tutorial.pdf}{tutorial}. To make it
interesting, augment it with recursive function definitions, integers, and
product types. Consider implementing the language as part of the \href{http://plzoo.andrej.com}{Programming
Languages Zoo}.
\end{problem}

\hypertarget{programming-with-algebraic-effects-and-handlers}{%
\section{Programming with algebraic effects and
handlers}\label{programming-with-algebraic-effects-and-handlers}}

In the last lecture we shall explore how algebraic operations and
handlers can be used in programming.

\hypertarget{eff}{%
\subsection{Eff}\label{eff}}

There are several languages that support algebraic effects and handlers.
The ones most faithful to the theory of algebraic effects are
\href{http://www.eff-lang.org}{Eff} and the
\href{https://github.com/ocamllabs/ocaml-multicore}{multicore OCaml}.
They have very similar syntax, and we could use either, but let us use
Eff, just because it was the first language with algebraic effects and
handlers.

You can \href{http://www.eff-lang.org/try/}{run Eff in your browser} or
\href{https://github.com/matijapretnar/eff/\#installation--usage}{install
it} locally. The page also has a quick overview of the syntax of Eff,
which mimics the syntax of OCaml.

\hypertarget{reading-material-1}{%
\subsection{Reading material}\label{reading-material-1}}

We shall draw on examples from
\href{http://www.eff-lang.org/handlers-tutorial.pdf}{An introduction to
algebraic effects and handlers} and
\href{https://arxiv.org/abs/1203.1539}{Programming with algebraic
effects and handlers}. Some examples can be seen also at the
\href{https://github.com/effect-handlers/effects-rosetta-stone}{Effects
Rosetta Stone}.

Other examples, such as I/O and redirection can be seen at the
\href{http://www.eff-lang.org/try/}{try Eff} page.

\hypertarget{basic-examples}{%
\subsection{Basic examples}\label{basic-examples}}

\subsubsection*{Exceptions}
\label{sec:exceptions}

\lstinputlisting{./eff-examples/exception.eff}

\subsubsection*{State}
\label{sec:state}

\lstinputlisting{./eff-examples/state.eff}

\hypertarget{multi-shot-handlers}{%
\subsection{Multi-shot handlers}\label{multi-shot-handlers}}

A handler has access to the continuation, and it may do with it whatever
it likes. We may distinguish handlers according to how many times the
continuation is invoked:

\begin{itemize}
\item
  an \textbf{exception-like} handler does not invoke the continuation
\item
  a \textbf{single-shot} handler invokes the continuation exactly once
\item
  a \textbf{multi-shot} handler invokes the continuation more than once
\end{itemize}

Of course, combinations of these are possible, and there are handlers
where it's difficult to ``count'' the number of invocations of the
continuation, such as multi-threading below.

An exception-like handler is, well, like an exception handler.

A single-shot handler appears to the programmer as a form of
dynamic-dispatch callbacks: performing the operation is like calling the
callback, where the callback is determined dynamically by the enclosing
handlers.

The most interesting (and confusing!) are multi-shot handlers. Let us
have a look at one such handler.

\hypertarget{ambivalent-choice}{%
\subsubsection{Ambivalent choice}\label{ambivalent-choice}}

Ambivalent choice is a computational effect which works as follows. There is an
exception $\mathsf{Fail} : \mathsf{unit} \to \mathsf{empty}$ which signifies
failure to compute successfully, and an operation
$\mathsf{Select} : \alpha\; \mathsf{list} \to \alpha$, which returns one of the
elements of the list. It has to do return an element such that the subsequent
computation does \emph{not} fail (if possible).

With ambivalent choice, we may solve the $n$-queens problem (of
placing $n$ queens on an $n \times n$ chess board so they do
not attack each other):

\lstinputlisting{./eff-examples/queens.eff}

\hypertarget{cooperative-multi-threading}{%
\subsection{Cooperative
multi-threading}\label{cooperative-multi-threading}}

Operations and handlers have explicit access to continuations. A handler
need not invoke a continue, it may instead store it somewhere and run
\emph{another} (previously stored) continuation. This way we get
\emph{threads}.

\lstinputlisting{./eff-examples/thread.eff}

\hypertarget{tree-representation-of-a-functional}{%
\subsection{Tree representation of a
functional}\label{tree-representation-of-a-functional}}

Suppose we have a \textbf{functional}
%
\begin{equation*}
  h : (\mathsf{int} \to \mathsf{bool}) \to \mathsf{bool}
\end{equation*}

When we apply it to a function $f : \mathsf{int} \to \mathsf{bool}$, we feel
that $h \; f$ will proceed as follows: $h$ will \emph{ask} $f$ about the value
$f \; x_0$ for some integer $x_0$. Depending on the result it gets, it will then
ask some further question $f \; x_1$, and so on, until it provides an
\emph{answer}~$a$.

We may therefore represent such a functional $h$ as a \textbf{tree}:
%
\begin{itemize}
\item
  the leaves are the answers
\item
  a node is labeled by a question, which has two subtrees representing
  the two possible continuations (depending on the answer)
\end{itemize}
%
We may encode this as the datatype:
%
\begin{verbatim}
type tree =
  | Answer of bool
  | Question of int * tree * tree
\end{verbatim}
%
Given such a tree, we can recreate the functional $h$:
%
\begin{verbatim}
let rec tree2fun t f =
  match t with
  | Answer y -> y
  | Question (x, t1, t2) -> tree2fun (if f x then t1 else t2) f
\end{verbatim}
%
Can we go backwards? Given $h$, how do we get the tree? It turns out this is not
possible in a purely functional setting in general (but is possible for out
specific case because $\mathsf{int} \to \mathsf{bool}$ is \emph{compact},
Google ``impossible functionals''), but it is with computational effects.

\lstinputlisting{./eff-examples/fun_tree.eff}

\hypertarget{problems-1}{%
\subsection{Problems}\label{problems-1}}

\begin{problem}[Breadth-first search]
  Implement the \emph{breadth-first search} strategy for ambivalent choice.
\end{problem}

\begin{problem}[Monte Carlo sampling]
%
The \href{http://www.eff-lang.org/try/}{online Eff} page has an example
showing a handler which modifies a probabilistic computation (one that
uses randomness) to one that computes the \emph{distribution} of
results. The handler computes the distribution in an exhaustive way that
quickly leads to inefficiency.

Improve it by implement a
\href{https://en.wikipedia.org/wiki/Monte_Carlo_method}{Monte Carlo}
handler for estimating distributions of probabilistic computations.
\end{problem}

\begin{problem}[Recursive cows]
  Contemplate the
  \href{https://github.com/effect-handlers/effects-rosetta-stone/tree/master/examples/recursive-cow}{recursive
    cows}.
\end{problem}

\end{document}

%%% Local Variables:
%%% coding: utf-8
%%% mode: latex
%%% TeX-master: t
%%% End:


A common, baseline selection is applied for the resonant  analyses of
the Run~2 data that is taken directly from ~\cite{Nishu:2646455}, as
described in Section~\ref{sec:base_selection}.

%Then, specific cuts applied
%for the resonant analysis, tailored to improve the sensitivity, are described in Sections~\ref{sec:res_selection}.

\subsection{Observables and Kinematic Variables}
%\label{sec:observables}
In the Standard Model (SM), the main and predominant source of dijet
events is two-to-two scattering in QCD processes. The search exploits
two key properties of the background to enhance its sensitivity to new
physics signals:

\begin{itemize}
	\item The background at high \mjj\ is a smooth and continuously falling spectrum.
	\item The background at high mass strongly peaks in the forward
direction, due to Rutherford $t$- and $u$-channel poles in the cross sections
for many individual scattering diagrams \cite{Harris:2011bh}.
\end{itemize}

Many of the possible signals for the dijet search would appear as a
resonance peak in \mjj, the invariant mass of the dijet system formed
from the two highest-\pT\ jets. Resonant signals have $\cos{\theta}$
distributions which, in contrast to Rutherford scattering, are either
isotropic or follow some polynomial in $\cos{\theta}$~\footnote{See
Ref.~\cite{Harris:2011bh} p15 for a summary.}, giving them a distinct
angular distribution from QCD processes. For this reason, the \mjj\
search selects events with small angle separation via an upper limit on
\begin{itemize}
	\item \ystar = $(y_1-y_2)/2$
\end{itemize}
to enhance sensitivity to higher energies and enable probing the scale
of new phenomena. Where $y_{1,2}$ is the rapidity of the leading and
sub-leading jet. The value of the \ystar\ cut is optimized to reject
$t$-channel QCD while admitting the other types of signals.

\subsection{Jet Reconstruction and Calibration}
%\label{sec:jet_reconstruction}
Jets are reconstructed with the \akt algorithm \cite{Cacciari:2008gp}
with a distance parameter of 0.4, as implemented in the \textsc{FastJet}
package~\cite{Cacciari:2011ma}. For these studies, we use jets
reconstructed from topological clusters using the procedures described
in \cite{ATLAS-CONF-2015-002}. For jet cleaning, the standard
\textit{Loose} jet quality cuts are used as discussed in
\cite{ATLAS-CONF-2015-029}. An event-based jet cleaning is applied to
reject any event with a leading or sub-leading jet flagged as
\textit{Bad} for the \textit{Loose} criteria.  The jet criteria used is
summarized in Table~\ref{tab:jetCalibration}.

\begin{table}[ht]
	\label{tab:jetCalibration}
		\begin{tabular}{lc}
			\toprule
			%\large
			\multicolumn{2}{c}{Jet reconstruction parameters} \\
			%\normalsize
			%\normalsize
			\midrule
			Parameter & Value \\
			\midrule
			Algorithm & \akt  \\
			R-parameter & 0.4 \\
			Input Constituent & EMTopo \\
			Analysis Release Number & 21.2.121 \\
			%Calibration tag & JetCalibTools-00-04-76 \\
			CalibArea tag & 00-04-82 \\
			%Calibration configuration & JES\_data2017\_2016\_2015\_Consolidated\_EMTopo\_2018\_rel21.config \\
			Calibration configuration & JES\_MC16Recommendation\_Consolidated\_EMTopo\_Apr2019\_Rel21.config \\
			Calibration sequence (Data) & JetArea\_Residual\_EtaJES\_GSC\_Insitu \\
			Calibration sequence (MC) & JetArea\_Residual\_EtaJES\_GSC\_Smear \\
			%Calibration configuration (AFII) & JES\_MC16Recommendation\_AFII\_EMTopo\_April2018\_rel21.config \\
			Calibration configuration (AFII) & JES\_MC16Recommendation\_AFII\_EMTopo\_Apr2019\_Rel21.config \\
			Calibration sequence (AFII) & JetArea\_Residual\_EtaJES\_GSC\_Smear \\
			\midrule
			%\large
			\multicolumn{2}{c}{Selection requirements} \\
			%\normalsize
			\midrule
			Observable & Requirement \\
			\midrule
			Jet cleaning & Loose WP \\
			Batman cleaning & No \\
			\pT  & $>$150 GeV \\
			\textbar$\eta$\textbar & $<$2.1 \\
			\bottomrule
	\end{tabular}
\caption{Jet selection criteria used in this analysis.}
\end{table}

\subsection{Trigger}
%\label{sec:trigger}
The data used in this analysis are collected using the lowest
unprescaled small-R single-jet trigger.  The naming convention of
single-jet triggers follows either `Jnnn' for L1 triggers or `jnnn' for
HLT trigger, where 'nnn' is a number specifying the nominal jet \pT
threshold for the trigger in \GeV\ .  The energy scale of the trigger
threshold is the EM scale for L1 triggers, while a calibration sequence
very close to what is applied to offline jets is applied to HLT jets,
bringing their scale to the hadronic scale.

The lowest single jet un-prescaled trigger in 2015 was HLT\_j360, in
2016 was HLT\_j380, in 2017 was HLT\_j420 (if we ignore one run with
luminosity around 2\,\ipb ) and in 2018 was also HLT\_j420. So to keep
the selection consistent across years, the kinetic requirement derived
for HLT\_j420 is used for the analysis. In addition to this,
HLT\_j225\_gsc420\_boffperf\_split is also unprescaled for all data
taking and also considered for the trigger studies for the analysis.

For the complete \RunTwo dataset, the two single-jet unprescaled
triggers for all data taking are: HLT\_j420 and
HLT\_j225\_gsc420\_boffperf\_split as mentioned above, both of which are
seeded from the L1\_J100 trigger. Both triggers search for jets with
$\pT > 420 \GeV$, while the HLT\_j225\_gsc420\_boffperf\_split trigger
also applies the offline global sequential calibration (GSC) to improve
the trigger turn-on.\\

The two triggers were found to be very similar in
performance in Ref.~\cite{Nishu:2646455}, which finally used HLT\_j420 trigger.
It was also found in Ref.~\cite{Nishu:2646455} that obtaining the turn-on directly from \mjj\ provides a much
lower turn-on than from requiring a specific cut on the leading jet \pT.
In order not to sculpt the \mjj\ distribution in an odd way due to
different triggers and given the assessment of the turn-on, this analysis also
uses the HLT\_j420 trigger with no explicit cut on the leading jet \pT.
In practice, the invariant mass cut imposes a soft cut of 380~\GeV on
the leading jet, and 150~\GeV on the subleading jet.

\subsection{Baseline selection}
\label{sec:base_selection}
The baseline event selection is applied for all signal regions and
the cuts applied are:
\begin{itemize}
\item Good Run List (GRL): Requirement that all relevant detectors were in a good state ready for physics. The GRLs used for this analysis are:
        \begin{itemize}
                %\item 2015(3.2\,\ifb): data15\_13TeV.periodAllYear\_DetStatus-v89-pro21-02\_Unknown\_PHYS\_ \newline StandardGRL\_All\_Good\,\_25ns.xml
                %\item 2016(32.97\,\ifb): data16\_13TeV.periodAllYear\_DetStatus-v89-pro21-01\_DQDefects-00-02-04\_ \newline PHYS\_StandardGRL\_All\_Good\_25ns.xml
                %\item 2017(44.31\,\ifb): data17\_13TeV.periodAllYear\_DetStatus-v99-pro22-01\_Unknown\_ \newline PHYS\_StandardGRL\_All\_Good\_25ns\_Triggerno17e33prim.xml
                %\item 2018(59.94\,\ifb): data18\_13TeV.periodAllYear\_DetStatus-v102-pro22-04\_Unknown\_PHYS\_ \newline StandardGRL\_All\_Good\_25ns\_Triggerno17e33prim.xml
                \item 2015(3.2\,\ifb): data15\_13TeV.periodAllYear\_DetStatus-v89-pro21-02\_Unknown\_PHYS\\\_StandardGRL\_All\_Good\_25ns.xml
                \item 2016(33\,\ifb): data16\_13TeV.periodAllYear\_DetStatus-v89-pro21-01\_DQDefects-00-02-04\_PHYS\\\_StandardGRL\_All\_Good\_25ns.xml
                \item 2017(44.2\,\ifb): data17\_13TeV.periodAllYear\_DetStatus-v99-pro22-01\_Unknown\_PHYS\\\_StandardGRL\_All\_Good\_25ns\_JetHLT\_Normal2017.xml
                \item 2018(58.5\,\ifb): data18\_13TeV.periodAllYear\_DetStatus-v102-pro22-04\_Unknown\_PHYS\\\_StandardGRL\_All\_Good\_25ns\_Triggerno17e33prim.xml
        \end{itemize}
\item LAr: Liquid Argon Calorimeter error rejected ( errorState(xAOD::EventInfo::LAr) )
\item Tile: Tile Calorimeter error rejected ( errorState(xAOD::EventInfo::Tile) )
\item SCT: SCT single event upsets rejected ( errorState(xAOD::EventInfo::SCT) )
\item Core: Incomplete event build rejected ( isEventFlagBitSet(xAOD::EventInfo::Core, 18) )
\item All jets with $\pt\ge 150\,\GeV$ pass LooseBad cleaning cuts
\item Primary Vertex: the highest $\sum\pt^{2}(trk)$ (xAOD::VxType::VertexType::PriVtx) vertex has at least two tracks associated with it
\item Trigger: Passes the lowest unprescaled single-jet trigger, HLT\_j420
\item Jet preselecton: Leading jet $\pt\ge 380\,\GeV$ and Jet multiplicity $\ge 2$
\item $|\Delta\phi|$ between 2 jets: $|\Delta\phi| > 1.0$
\end{itemize}

Additional kinematic selection criteria are used for various search distributions separately to optimize the
search potential, which are then discussed in Section~\ref{section:ystarCutOptimization}.

\begin{comment}
\subsection{Analysis cutflow}
%\label{sec:data_cutflow}

This section and the next present the analysis cutflows. Cutflows obtained on
Run-2 data are presented in Tables~\ref{tab:cutFlow_resonance_run2} and
\ref{tab:cutFlow_wstar_run2}.

\begin{table}[htbp]
	\centering
	\begin{tabular}{l|c|c}
		\hline\hline
		Selection criteria & $N_{events}$ & rel. decrease (\%) \\
		\hline
		all      &	4738142726	&	0.00	\\
		Apply GRL 	& 	4442605390        & 	-6.24	 \\
		Cleaning	 & 	4379077017	 & 	-1.43	 \\
		HLT j420	 & 	266104885	 & 	-93.9	 \\
		jet pre-selection	 &     259157844         &      -2.61    \\
		$|\Delta\phi| > 1.0$	 & 		 & 		 \\
		$|\ystar| < 0.6$	 & 		 & 		 \\
		$\mjj>1100~\GeV$	 & 		 & 		 \\
		\hline\hline
	\end{tabular}
	\caption{Cutflow for
		events with H$^\prime$ cuts:  $\mjj>1100~\GeV$, and $|\ystar|<0.6$. .
		\label{tab:cutFlow_resonance_run2} }
\end{table}

\begin{table}[htbp]
	\centering
	\begin{tabular}{l|c|c}
		\hline\hline
		Selection criteria & $N_{events}$ & rel. decrease (\%) \\
		\hline
		all      &	4738142726	&	0.00	\\
		Apply GRL 	& 	4442605390        & 	-6.24	 \\
		Cleaning	 & 	4379077017	 & 	-1.43	 \\
		HLT j420	 & 	266104885	 & 	-93.9	 \\
		jet pre-selection	 &     259157844        &      -2.61    \\
		$|\Delta\phi| > 1.0$	 & 		 & 		 \\
		$|\ystar| < 0.8$	 & 		 & 	 \\
		$\mjj>1133~\GeV$	 & 		 & 	 \\
		\hline\hline
	\end{tabular}
	\caption{Cutflow for
		events with string resonance cuts:  $\mjj>1133~\GeV$, and $|\ystar|<0.8$. .
		\label{tab:cutFlow_wstar_run2} }
\end{table}
\end{comment}


\documentclass[12pt]{article}

\usepackage[margin=1.9cm, letterpaper]{geometry}
\usepackage{fontspec}
\usepackage{mathtools}
\usepackage{pgfplots}
\usepackage{siunitx}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{makecell}
\usepackage{float}

\pgfplotsset{compat=newest}

\title{EXPERIMENT 4: \protect\\Magnetic Fields}
\date{March 12, 2019}
\author{Michael Kwok | Partner: Cyrus Diego }

\begin{document}
\maketitle
\pagebreak

\section{Introduction}
In this experiment, the relationship between electric currents and magnetic
fields will be observed. This relationship was first discovered by Hans
Christian Oersted in the 1800s, where he noticed that a wire with an electric
current flowing through it caused a deflection on a compass. He came up with the
following equation for the magnetic field $\Delta B$ by a tiny segment of coil $\Delta l$:

\begin{equation} \label{eq:deltaB}
    \Delta B = \frac{\mu_0I\Delta l \sin\theta}{4\pi r^2}
\end{equation}

where $\mu_0$ is the constant of permeability of free space, $I$ is the current
in the wire, $\theta$ is the angle between the segment and the displacement, and
$r$ is the current displacement.

The Hall effect is when a potential difference is produced in a conductor due to a
current passing in the conductor while within a magnetic field. It was discovered
by Edwin hall in 1879.

We will also be using a solenoid, which is a tightly wound coil of wire. This coil
of wire produces a uniform magnetic field when current is passing through the
wire. The equation for the magnetic field in the central axis of a solenoid is given as:

\begin{equation} \label{eq:B}
    B = \frac{1}{2}\mu_0nI(\cos\beta_2-\cos\beta_1)
\end{equation}

where $n = N/L$, the number of turns per unit length of the solenoid, and $\beta_1$
and $\beta_2$ are angles between the sides and the center of the solenoid from
the displacement respectively.

A Helmholtz coil is used in the second part of the experiment. This coil is similar
to the solenoid. However, it has two sets of coils a set distance apart, and the entire
volume between the two coils will have a homogenous magnetic field. The magnetic field
in the center of the two coils is given as the following:

\begin{equation} \label{eq:B_Helmholtz}
    B_H = \frac{8\mu_0NI}{\sqrt{125}R}
\end{equation}
where $N$ is the number of turns in the coil, $I$ is the current, $R$ is the coil's
radius.

We will be determining the distribution of the magnetic field along the axis of the solenoid,
and the magnetic field of the Helmholtz coil, getting the number of turns of the coil
afterwards.

\section{Methods}
\subsection*{Part 1}
\begin{itemize}
    \item Wire up the $+$ terminal of a DC power supply to a switch, then wire the switch
          to the solenoid. Connect the other end of the solenoid to the DC power supply.
          Keep the circuit open.

    \item Set up a Hall effect probe to collect the data from. Record the ambient magnetic
          field $B_e$.

    \item Close the circuit, then set the current on the power supply to be \SI{1.0}{\volt}.
          Ensure magnetic fields are positive. If not, flip the polarity of the leads.

    \item Slide the probe along the length of the solenoid, recording the field strength
          measurements every 2 cm.

    \item Take down the length $L$, radius $R$, number of turns $N$, and the midpoint ruler
          position $x_c$ of the solenoid.
\end{itemize}

\subsection*{Part 2}
\begin{itemize}
    \item Connect the two Helmholtz coils to be in series with the DC Power source,
          just like the previous part.

    \item Carefully position the tip of the Hall effect probe to the center of the two
          coils.

    \item Record the measured field strength at \SI{0.1}{\ampere} intervals between \SI{0.1}{\ampere}
          and \SI{1.0}{\ampere}.
\end{itemize}
\section{Results}
The results collected are as follows (See also \ref{fig:part1_results} and \ref{fig:part2_results} in appendix):
\begin{figure}[!ht]
    \includegraphics[width=\textwidth]{solenoid_graph.png}
    \caption{Theoretical and Measured values of $B$ (Part 1)}

\end{figure}

For part 2, Excel's LINEST function gave the following values:
\begin{itemize}
    \item The gradient was $6.29\times10^{-4}$ with an error of $1.96\times10^{-6}$
    \item An intercept of $-5.20\times10^{-4}$ was calculated too, with an error of $1.21\times10^{-6}$
\end{itemize}
Using equation \ref{eq:B_Helmholtz},
\begin{equation*}
    \begin{aligned}
        \frac{8\mu_0N}{\sqrt{125}R}                    & = 6.29\times10^{-4} \\
        N                                              & = 103.6             \\
        \Delta\left(\frac{8\mu_0N}{\sqrt{125}R}\right) & = 1.96\times10^{-6} \\
        (\Delta N)\%                                   & = 1.42\%            \\
        N                                              & = 103.6\pm1.5
    \end{aligned}
\end{equation*}
\section{Discussion}
\subsection*{Part 1}
The value of $B-B_e$ obtained was within the same order of magnitude for most of
the results, but were all slightly off from the theoretical $B_t$. This could be
due to the amplifier box being faulty, or an unset offset within the datalogging
software. Inherent inductance and resistance from the wire could be also factors,
reducing the strength of the magnetic field.
\subsection*{Part 2}
The value of $B_H$ obtained was consistent with what we expected, however an
intercept showed up when the equation was linearized. This shows that the
amplifier might not have had enough amplification on the readings, providing us
lower voltages than what might be correct values. The gradient should not have
been affected by this, and thus neither should our final result. The final value
of $N$ we obtained is in poor agreement with the theory, which might be explained
by inductance in the wire causing a reduced final magnetic field that affected the
hall effect sensor.
\section{Conclusion}
We were able to show how the magnetic field inside a solenoid stays the same
after a certain distance in, and show how the distribution varies like a bell
curve, but while the trend and distribution is similar, the values were quite
far off, around \SI{2}{\milli\tesla} from the theoretical.

For the Helmholtz coil, our value of $103\pm1.5$ was around 20 intervals away
from the theoretical value, which means that it was not consistent with the expected value.
\pagebreak
\section*{References}
\begin{itemize}
    \item 2018-2019 PHYSICS 230 Laboratory Manual (I, Isaac): Equations and Methods
    \item Cyrus Diego: Experimental results
    \item Wikipedia: Helmholtz coil and Hall effect history
\end{itemize}
\pagebreak
\section*{Appendix}
\begin{figure}[H]
    \begin{subfigure}{0.65\textwidth}
        \begin{tikzpicture}
            \begin{axis}[
                    enlargelimits=true,
                    grid=major,
                    title=$B_H$ vs $I$,
                    ylabel=$B_H$ (\si{\tesla}),
                    xlabel=$I$ (\si{\ampere}),
                    xmin=0,
                    xmax=1.0,
                    ymin=-0.00048,
                    ymax=0.00012,
                    legend entries={Data, Trendline},
                    legend pos=south east,
                    axis lines = center
                ]
                \addplot[only marks, mark=*, mark options={fill=black}] table {Lab3_2Graph.dat};
                \addplot [blue, no marks] {0.000629394*x-0.000520467};

            \end{axis}
        \end{tikzpicture}
        \caption{$B_H$ vs $I$ graph (Part 2)}
    \end{subfigure}
    \begin{subfigure}{0.3\textwidth}
        \begin{tabular}{|r|r|}
            \hline
            I (\si{\ampere}) & B (\si{\tesla}) \\ \hline
            0.1              & -0.000457       \\
            0.2              & -0.000398       \\
            0.3              & -0.00033        \\
            0.4              & -0.000268       \\
            0.5              & -0.000205       \\
            0.6              & -0.000141       \\
            0.7              & -8.2E-05        \\
            0.8              & -1.6E-05        \\
            0.9              & 4.6E-05         \\
            1                & 0.000108        \\ \hline
        \end{tabular}
        \caption{Recorded data (Part 2)}
    \end{subfigure}
    \caption{Part 2}
    \label{fig:part2_results}
\end{figure}
\begin{figure}[!ht]
    \begin{subfigure}{0.4\textwidth}
        \begin{tabular}{|r|r|}
            \hline
            L     & $0.280\pm0.001$\si{\meter}   \\ \hline
            R     & $0.0269\pm0.0003$\si{\meter} \\ \hline
            I     & $1\pm0.05$\si{\ampere}       \\ \hline
            $x_c$ & $0.228$\si{\meter}           \\ \hline
            $B_e$ & $-0.520$\si{\milli\tesla}    \\ \hline
        \end{tabular}
        \caption{Constants (Part 1)}
    \end{subfigure}
    \begin{subfigure}{0.5\textwidth}
        \begin{tabular}{|r|r|r|r|}
            \hline
            \thead{B                                                     \\(\si{\milli\tesla})} & \thead{Position\\(x) (\si{\meter})} & \thead{Corrected\\($B - B_e$) (\si{\tesla})} & \thead{Theoretical\\($B_t$) (\si{\tesla})} \\ \hline
            $0.207$ & $0.08$ & $7.27\times10^{-4}$ & $7.14\times10^{-5}$ \\
            $1.071$ & $0.10$ & $1.59\times10^{-3}$ & $1.17\times10^{-4}$ \\
            $1.709$ & $0.12$ & $2.23\times10^{-3}$ & $2.16\times10^{-4}$ \\
            $1.939$ & $0.14$ & $2.46\times10^{-3}$ & $4.79\times10^{-4}$ \\
            $2.025$ & $0.16$ & $2.55\times10^{-3}$ & $1.24\times10^{-3}$ \\
            $2.06$  & $0.18$ & $2.58\times10^{-3}$ & $2.44\times10^{-3}$ \\
            $2.075$ & $0.20$ & $2.60\times10^{-3}$ & $3.06\times10^{-3}$ \\
            $2.081$ & $0.22$ & $2.60\times10^{-3}$ & $3.29\times10^{-3}$ \\
            $2.083$ & $0.24$ & $2.60\times10^{-3}$ & $3.36\times10^{-3}$ \\
            $2.077$ & $0.26$ & $2.60\times10^{-3}$ & $3.40\times10^{-3}$ \\
            $2.062$ & $0.28$ & $2.58\times10^{-3}$ & $3.41\times10^{-3}$ \\
            $2.037$ & $0.30$ & $2.56\times10^{-3}$ & $3.42\times10^{-3}$ \\
            $1.978$ & $0.32$ & $2.50\times10^{-3}$ & $3.42\times10^{-3}$ \\
            $1.833$ & $0.34$ & $2.35\times10^{-3}$ & $3.41\times10^{-3}$ \\
            $1.404$ & $0.36$ & $1.92\times10^{-3}$ & $3.39\times10^{-3}$ \\
            $0.485$ & $0.38$ & $1.01\times10^{-3}$ & $3.34\times10^{-3}$ \\ \hline
        \end{tabular}
        \caption{Recorded data (Part 1)}
    \end{subfigure}
    \caption{Part 1}
    \label{fig:part1_results}
\end{figure}
\pagebreak
The expression for a single coil $B_c$ of a Helmholtz coil setup is:
\begin{equation*}
    B_c = \frac{\frac{1}{2}\mu_0NR^2I}{[R^2+(x-x_c)^2]^\frac{3}{2}}
\end{equation*}

and $N$ is the number of turns in the coil.

For the $B_s$ in the center of a solenoid, the following can be done:
\begin{equation*}
    \begin{aligned}
        B_s                    & = \frac{1}{2}\mu_0NI[\cos\beta_2-\cos\beta_1]                                                      \\
        substitute \cos\beta_2 & = \frac{x_c}{\sqrt{x_c^2 + R^2}}                                                                   \\
        and \cos\beta_1        & =\frac{L-x_c}{\sqrt{(L-x_c)^2+R^2}}                                                                \\
        B_s                    & = \frac{1}{2}\mu_0NI\left[\frac{x_c}{\sqrt{x_c^2 + R^2}}-\frac{L-x_c}{\sqrt{(L-x_c)^2+R^2}}\right] \\
                               & = \frac{\mu_0NIL}{\sqrt{L^2+4R^2}}
    \end{aligned}
\end{equation*}

where $\mu_0$ is the constant of permeability of free space, $I$ is the current
in the wire, $x$ is the current displacement from the center of the coil, $x_c$
is the displacement at the center of the coil, $R$ is the radius of the coil,
and $L$ is the length of the solenoid.

To obtain the expression for $B_H$ from $B_c$,

First obtain expression for $B_c$ at $x=R$:
\begin{equation*}
    B_c = \frac{\mu_0NIR^2}{2(R^2+(\frac{R}{2})^2)^\frac{3}{2}}
\end{equation*}
Then, multiply by two (principle of superposition for magnetic fields):
\begin{equation*}
    \begin{aligned}
        B_H = 2 B_c
         & = \frac{\mu_0NIR^2}{(R^2+(\frac{R}{2})^2)^\frac{3}{2}} \\
         & = \frac{\mu_0NIR^2}{(R^2+\frac{1}{4}R^2)^\frac{3}{2}}  \\
         & = \frac{\mu_0NIR^2}{(\frac{5}{4}R^2)^\frac{3}{2}}      \\
         & = \frac{8}{\sqrt{125}}\frac{\mu_0NIR^2}{(R^2)^\frac{3}{2}}\\
         & = \frac{8}{\sqrt{125}}\frac{\mu_0NI}{R}\\
    \end{aligned}
\end{equation*}
which is exactly equation (\ref{eq:B_Helmholtz})

\end{document}


\chapter{Linear Models: Model simplification}
\label{chap:ModelSimp}

Aims of this chapter\footnote{Here you work with the script file {\tt ModelSimp.R}}:
\begin{compactitem}
	\item Simplifying complex models by removing non-explanatory terms
\end{compactitem}

In biology, we often use statistics to compare competing hypotheses in
order to work out the simplest explanation for some data. This often
involves collecting several explanatory variables that describe
different hypotheses and then fitting them together in a single model,
and often including interactions between those variables.

In all likelihood, not all of these model {\it terms} will be
important. If we remove unimportant terms, then the explanatory power
of the model will get worse, but might not get significantly worse.

\begin{quotation} \it
``It can scarcely be denied that the supreme goal of all theory is to
make the irreducible basic elements as simple and as few as possible
without having to surrender the adequate representation of a single
datum of experience.''

\em Albert Einstein

\end{quotation}

\noindent Or to paraphrase:

\begin{quotation}\it
``Everything should be made as simple as possible, but no simpler.''
\end{quotation}

The approach we will look at is to start with a {\it maximal model}
--- the model that contains everything that might be important --- and
simplify it towards the {\it null model} --- the model that says that
none of your variables are important. Hopefully, there is a point
somewhere in between where you can't remove any further terms without
making the model significantly worse: this is called the {\it minimum
adequate model}.

\begin{tikzpicture}[every node/.style={draw=black!70, very thick, rectangle, rounded corners}]
\node[fill=black!10, minimum width=4 cm] (a) at (0,0) {Maximal model};
\node[fill=black!10, minimum width=4 cm] (b) at (5,0) {Minimum adequate model};
\node[fill=black!10, minimum width=4 cm] (c) at (10,0) {Null model};
\draw [draw=black!70, very thick, -stealth, shorten >= 1mm] (a.east) -- (b.west);
\draw [draw=black!70, very thick, -stealth, shorten >= 1mm] (b.east) -- (c.west);
\end{tikzpicture}

\section{A maximal model}

We'll be using the mammal dataset for this practical, so once again:

\begin{compactitem}[$\quad\star$]
	\item Make sure you have changed the working directory to your
	stats module {\tt Code} folder.
	\item Create a new blank script called `MyModelSimp.R'.
	\item Load the mammals data into a data frame called {\tt mammals}.
\end{compactitem}

In Chapters 5 \& \ref{ch:MulExplInter}, we looked at how the
categorical variables {\tt GroundDwelling} and {\tt TrophicLevel}
predicted genome size in mammals. In this chapter, we will add in two
more continuous variables: litter size and body mass. The first thing
we will do is to log both variables and reduce the dataset to the rows
for which all of these data are available:

\begin{lstlisting}
#get logs of continuous variables
> mammals$logLS <- log(mammals$LitterSize)
> mammals$logCvalue <- log(mammals$meanCvalue)
> mammals$logBM <- log(mammals$AdultBodyMass_g)

# reduce dataset to five key variables
> mammals <- subset(mammals, select = c(logCvalue, logLS, logBM,
TrophicLevel, GroundDwelling))

# remove the row with missing data
> mammals <- na.omit(mammals)
\end{lstlisting}

\begin{compactitem}[$\quad\star$]
	\item Copy the code above into your script and run it
	\item Check that the data you end up with has this structure:
\end{compactitem}

\begin{lstlisting}
 'data.frame':	240 obs. of  5 variables:
 $logCvalue     : num  0.94 1.322 1.381 1.545 0.888 ...
 $logLS         : num  1.1 1.12 0 0 1.52 ...
 $logBM         : num  10.83 4.87 11.46 10.86 3.23 ...
 $TrophicLevel  : Factor w/ 3 levels "Carnivore","Herbivore",..: 1 2 2 2 3 3 3 2 2 3 ...
 $GroundDwelling: Factor w/ 2 levels "No","Yes": 2 2 2 2 2 1 2 1 1 1 ...
  - attr(*, "na.action")=Class 'omit'  Named int [1:139] 2 4 7 9 10 11 14 15 20 21 ...
   .. ..- attr(*, "names")= chr [1:139] "2" "4" "7" "9" ...
\end{lstlisting}

Now we'll fit a model including all of these variables and all of the
interactions:

\begin{lstlisting}
> model <- lm(formula = logCvalue ~ logLS * logBM * TrophicLevel *
GroundDwelling, data = mammals)
\end{lstlisting}

\begin{compactitem}[$\quad\star$]
	\item Run this model in your script.
	\item Look at the output of {\tt anova(model)} and {\tt
	summary(model)}.
\end{compactitem}

Scared? Don't be! There are a number of points to this exercise:
\begin{enumerate}

	\item These tables show exactly the kind of output you've seen
	before. Sure, there are lots of rows but each row is just asking
	whether a model term ({\tt anova}) or a model coefficient ({\tt
	summary}) is significant.

	\item Some of the rows are significant, others aren't: some of the
	model terms are not explanatory.

	\item The two tables show slightly different things - lots of stars
	for the {\tt anova} table and only a few for the {\tt summary} table.

	\item That last line in the {\tt anova} table: {\tt
	logLS:logBM:TrophicLevel:GroundDwelling}. This is an interaction of
	four variables capturing how the slope for litter size changes for
	different body masses for species in different trophic groups and
	which are arboreal or ground dwelling. Does this seem easy to
	understand?
\end{enumerate}

The real lesson here is that it is easy to fit complicated models in R.
{\it Understanding and explaining them is a different matter}. The
temptation is always to start with the most complex possible model but
this is rarely a good idea.

\section{A better maximal model}

Instead of all possible interactions, we'll consider two-way
interactions: how do pairs of variables affect each other? There is a
shortcut for this: {\tt y \textasciitilde{} (a + b +
c)\textasciicircum{}2} gets all two way combinations of the variables
in the brackets, so is a quicker way of getting this model:

{\tt y \textasciitilde{} a + b + c + a:b + a:c + b:c}.

So let's use this to fit a simpler maximal model:

\begin{lstlisting}
> model <- lm(logCvalue ~ (logLS + logBM + TrophicLevel + GroundDwelling)^2, data = mammals)
\end{lstlisting}

The {\tt anova} table for this model looks like this:

\begin{lstlisting}
> anova(model)

 Analysis of Variance Table

 Response: logCvalue
                              Df Sum Sq Mean Sq F value  Pr(>F)
 logLS                         1   0.99   0.989   25.72 8.2e-07 ***
 logBM                         1   3.03   3.032   78.83 < 2e-16 ***
 TrophicLevel                  2   0.48   0.239    6.21  0.0024 **
 GroundDwelling                1   0.11   0.110    2.87  0.0915 .
 logLS:logBM                   1   0.27   0.275    7.15  0.0081 **
 logLS:TrophicLevel            2   0.19   0.095    2.48  0.0862 .
 logLS:GroundDwelling          1   0.14   0.136    3.55  0.0609 .
 logBM:TrophicLevel            2   0.09   0.044    1.14  0.3230
 logBM:GroundDwelling          1   0.88   0.883   22.96 3.0e-06 ***
 TrophicLevel:GroundDwelling   2   0.04   0.022    0.58  0.5607
 Residuals                   225   8.65   0.038
 ---
 Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{lstlisting}

The first lines are the {\it main effects}, which are all significant
or near significant. Then there are the six interactions. One of these
is very significant: {\tt logBM:GroundDwelling}, which suggests that
the slope of log C value with body mass differs between ground dwelling
and non-ground dwelling species. The other interactions are
non-significant although some are close.

\begin{compactitem}[$\quad\star$]
	\item Run this model in your script.
	\item Look at the output of {\tt anova(model)} and {\tt
	summary(model)}.
	\item Check the model diagnostic plots.
\end{compactitem}

\section{Model simplification}

Model simplification is not a simple process. Each time you remove a
term from a model, the model will change: the model will get worse,
since some of the sums of squares are no longer explained, but the
remaining variables may take over.

The first question is: {\it what terms can you remove from a model}?
Obviously, you only want to remove non-significant terms, but there is
another rule -- you cannot remove a main effect or an interaction while
those main effects or interactions are present in a more complex
interaction. For example, in the model {\tt y \textasciitilde{} a + b +
c + a:b + a:c + b:c}, you cannot drop {\tt c} without dropping both
{\tt a:c} and {\tt b:c}.

The R function {\tt drop.scope} tells you what you can drop from a
model. Some examples:
\begin{lstlisting}
> drop.scope(model)
 [1] "logLS:logBM"                 "logLS:TrophicLevel"
 [3] "logLS:GroundDwelling"        "logBM:TrophicLevel"
 [5] "logBM:GroundDwelling"        "TrophicLevel:GroundDwelling"

> drop.scope(y ~ a + b + c + a:b)
 [1] "c"   "a:b"
> drop.scope(y ~ a + b + c + a:b + b:c + a:b:c)
 [1] "a:b:c"
\end{lstlisting}

Model simplification is an iterative process. The flow diagram below
shows how it works: at each stage you try and find an acceptable
simplification. If successful, then you start again with the new
simpler model and try and find a way to simplify this, until
eventually, you can't find anything more to remove.

\noindent\begin{tikzpicture}
	[bx/.style={draw=black!70, very thick, rectangle, align=center,
	                    rounded corners, fill=black!10, minimum width=4 cm},
	 every path/.style={draw=black!70, very thick, -stealth, shorten >= 1mm},
	node distance=0.8cm]
	\node[bx] (a) at (0,0) {\bf Maximal model};
	\node[bx, below= of a] (b) {Current model};
	\node[bx, below= of b] (b2) {Make list of valid\\terms to drop};
	\node[bx, below=of b2] (c) {Remove the least-\\significant valid term};
	\node[bx, below=of c] (d) {New model};
	\node[bx, below=of d] (e) {Compare current \\and new models};
	\node[bx, below=of e, diamond, aspect=2, inner sep=-1.5 mm] (f) {Is the new\\ model significantly\\ worse?};
	\node[bx, right=of f] (g) {Remove current\\ term from list \\of valid terms};
	\node[bx, above=of g, diamond, aspect=2, inner sep=-1.5 mm] (h) {Are there\\ other  valid terms \\to drop?};
	\node[bx, right=of b2, ultra thick] (i)  {\bf Minimum adequate model};
	\node[bx, left=of c, thick] (j)  {New model becomes\\ current model};

	\draw (a.south) -- (b.north);
	\draw (b.south) -- (b2.north);
	\draw (b2.south) -- (c.north);
	\draw (c.south) -- (d.north);
	\draw (d.south) -- (e.north);
	\draw (e.south) -- (f.north);
	\draw[rounded corners] (f.west) -| node[below]{No} (j.south) ;
	\draw[rounded corners] (j.north) |-  (b.west) ;
	\draw (f.east) -- node[below]{Yes} (g.west);
	\draw (g.north) --  (h.south);
	\draw[rounded corners, label distance=1cm] (h.north)   |- node[right, pos=0.5]{Yes}(c.east);
	\draw[rounded corners] (h.east)  --  node[below]{No} ++(1,0) |- (i.east);
\end{tikzpicture}

As always, we can use an $F$ test to compare two models and see if they
have significantly different explanatory power. Here, significance is a
bad thing --- it means that we've removed a term that makes the model
significantly worse.

The last thing we need to do is work out how to remove a term from a
model. We could type out the model again, but there is a shortcut using
the function {\tt update}:
\begin{lstlisting}
# a simple model
> f <- y ~ a + b + c + b:c

# remove b:c from the current model
> update(f, . ~ . - b:c)
 y ~ a + b + c

# model g as a response using the same explanatory variables.
> update(f, g ~ .)
 g ~ a + b + c + b:c
\end{lstlisting}

Yes, the syntax is a little odd. The function uses a model or a formula
and then allows you to alter the current formula. The dots in the code
{\tt .~\textasciitilde~. } mean `use whatever is currently in the
response or explanatory variables'. It gives a simple way of changing a
model.

Putting this together, let's try a simplification. From the previous
{\tt anova} and {\tt drop.scope} output, we know that the interaction
{\tt TrophicLevel:GroundDwelling} is not significant and a valid term.

\begin{lstlisting}
# remove TrophicLevel:GroundDwelling
> model2 <- update(model, . ~ . - TrophicLevel:GroundDwelling)

# use anova to compare the two models
> anova(model, model2)

 Analysis of Variance Table

 Model 1: logCvalue ~ (logLS + logBM + TrophicLevel + GroundDwelling)^2
 Model 2: logCvalue ~ logLS + logBM + TrophicLevel + GroundDwelling +
			logLS:logBM + logLS:TrophicLevel + logLS:GroundDwelling +
			logBM:TrophicLevel + logBM:GroundDwelling
   Res.Df  RSS Df Sum of Sq    F Pr(>F)
 1    225 8.65
 2    227 8.70 -2   -0.0446 0.58   0.56
\end{lstlisting}

This tells us that {\tt model2} is not significantly worse than {\tt
model}. We can now look at this model and see what else can be removed:

\begin{lstlisting}
> anova(model2)

 Analysis of Variance Table

 Response: logCvalue
                       Df Sum Sq Mean Sq F value  Pr(>F)
 logLS                  1   0.99   0.989   25.82 7.8e-07 ***
 logBM                  1   3.03   3.032   79.12 < 2e-16 ***
 TrophicLevel           2   0.48   0.239    6.24  0.0023 **
 GroundDwelling         1   0.11   0.110    2.88  0.0909 .
 logLS:logBM            1   0.27   0.275    7.17  0.0079 **
 logLS:TrophicLevel     2   0.19   0.095    2.49  0.0854 .
 logLS:GroundDwelling   1   0.14   0.136    3.56  0.0604 .
 logBM:TrophicLevel     2   0.09   0.044    1.14  0.3216
 logBM:GroundDwelling   1   0.88   0.883   23.05 2.9e-06 ***
 Residuals            227   8.70   0.038
 ---
 Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

> drop.scope(model2)

 [1] "logLS:logBM"          "logLS:TrophicLevel"   "logLS:GroundDwelling"
 [4] "logBM:TrophicLevel"   "logBM:GroundDwelling"
\end{lstlisting}

\begin{compactitem}[$\quad\star$]
	\item Run this first simplification in your script.
	\item Look at the output above and decide what is the next possible
	term to delete
	\item Using the code above as a model, create {\tt model3} as the
	next simplification! (remember to use {\tt model2} in your update
	call and not {\tt model})
\end{compactitem}

Now for a difficult exercise:

\begin{compactitem}[$\quad\star$]
	\item Using the code above to guide you, try and find a minimal
	adequate model that you are happy with. In each step, the output of
	{\tt anova(model, modelN)} should be non-significant (where $N$ is
	the current step).

	\item It can be important to consider both {\tt anova} and {\tt
	summary} tables. It can be worth trying to remove things that look
	significant in one table but not the other --- some terms can explain
	significant variation on the {\tt anova} table but the coefficients
	are not significant.

	\item Remember to remove {\it terms}: with categorical variables,
	several coefficients in the {\tt summary} table may come from one
	term in the model and have to be removed together.

	\item When you have got your final model, save the model as an R data
	file:\\
	{\tt save(modelN, file='myFinalModel.Rda')}.

	% \item Submit the file to the assignment in the Statistical Modelling
	% module on Blackboard.

\end{compactitem}