\clearpage
\newpage

\section{Results}
\paragraph{Statistical errors} We used coalescent simulations of neutral
polymorphisms under spatial models of admixture to compare the statistical
errors of the AQP and APLS estimates with those of the {\tt tess3}
algorithm~\citep{Caye2016}. The ground truth for the $Q$-matrix (${\bf Q}_0$)
was computed from the mathematical model for admixture proportions used to
generate the data. For the $G$-matrix, the ground truth matrix (${\bf G}_0$) was
computed from the empirical genotype frequencies in the two population samples
before an admixture event. The root mean squared errors (RMSE) for the ${\bf Q}$
and ${\bf G}$ estimates decreased as the sample size and the number of loci
increased (Figure~\ref{fig:fig1}). For all algorithms, the statistical errors were generally
small when the number of loci was greater than $10$k SNPs. Those results
provided evidence that the three algorithms produced equivalent estimates of the
matrices ${\bf Q}_0$ and ${\bf G}_0$. The results also provided a check
that the APLS and {\tt tess3} algorithms converged to the same estimates as
those obtained after the application of the AQP algorithm, which is guaranteed
to converge mathematically.

\paragraph{The benefit of including spatial information in algorithms} Using
neutral coalescent simulations of spatial admixture, we compared the statistical
estimates obtained from the spatial algorithm APLS and the non-spatial
algorithm {\tt snmf}~\citep{Frichot2014}. For various levels of ancestral
population differentiation, estimates obtained from the spatial algorithm were
more accurate than for those obtained using non-spatial approaches
(Figure~\ref{fig:fig2}). For the larger samples, much finer population structure
was detected with the spatial method than with the non-spatial algorithm
(Figure~\ref{fig:fig2}).

In simulations of outlier loci, we used the area under the precision-recall
curve (AUC) for quantifying the performances of tests based on the estimates of
ancestry matrices, {\bf Q} and {\bf G}. In addition, we computed AUCs for
$F_{\rm ST}$-based neutrality tests using truly ancestral genotypes. As they
represented the maximum reachable values, AUCs based on truly ancestral
genotypes were always higher than those obtained for tests based on
reconstructed matrices. For all values of the relative selection intensity, AUCs
were higher for spatial methods than for non-spatial methods (Figure~\ref{fig:fig3}, the
relative selection intensity is the ratio of migration rates at neutral and
adaptive loci). For high selection intensities, the performances of tests based
on estimates of ancestry matrices were close to the optimal values reached by
tests based on true ancestral frequencies. These results provided evidence that
including spatial information in ancestry estimation algorithms improves the
detection of signatures of hard selective sweeps having occurred in unknown
ancestral populations.

\paragraph{Sensitivity of estimates to spatial measurements} Next, we used the
simulated data sets to evaluate the robustness of APLS estimates to inaccurate
measurements of spatial coordinates. To this aim, Gaussian noise was added to
truly observed geographic coordinates by considering values of the
noise-to-signal ratio ranging from 0 to 3. We computed variograms in all cases,
and found that the spatial signal was removed from simulations for
noise-to-signal ratios greater than two, while the signal was still observable
with a noise-to-signal ratio lower than one. For all simulations, we compared
the relative error of APLS $Q$-matrix estimates to those obtained from an
non-spatial method ({\tt snmf}). For small levels of uncertainty in spatial
coordinates the errors of APLS estimates were lower than those of {\tt snmf}
(Figure~\ref{fig:fig2_5}). For simulations with $n = 500$ individuals and $L =
10^5$ loci, a larger noise-to-signal ratio increased statistical errors in the
$Q$-matrix estimates from the APLS algorithm. For smaller noise-to-signal ratios,
RMSEs remained generally lower for the APLS algorithm than for methods without
spatial coordinates. For simulations with $n = 50$ individuals and $L = 10^4$
loci, the APLS estimates were more accurate than the non-spatial estimates. This
unexpected result could be explained by subtle algorithmic differences in tested
programs. To a large extent, estimates from the APLS algorithm were robust to
uncertainty in spatial measurements. Standard graphical tests such as a
variogram analysis can help deciding whether our spatially explicit algorithm is
useful or not.

\paragraph{Runtime and convergence analyses} We subsampled a large SNP data set
for {\it A. thaliana} ecotypes to compare the convergence properties and
runtimes of the {\tt tess3}, AQP, and APLS algorithms. In those experiments, we
used $K = 6$ ancestral populations, and replicated 5 runs for each simulation.
For $n = 100-600$ individuals ($L = 50$k SNPs), the APLS algorithm required more
iterations (25 iterations) than the AQP algorithm (20 iterations) to converge to
its solution (Figure~\ref{fig:fig4}). This was less than for {\tt tess3} (30 iterations). For
$L = 10-200$k SNPs ($n = 150$ individuals), similar results were observed. For
$50$k SNPs, the runtimes were significantly lower for the APLS algorithm than
for the {\tt tess3} and AQP algorithms. For $L = 50$k SNPs and $n = 600$
individuals, it took on average 1.0 min for the APLS and 100 min for the AQP
algorithm to compute ancestry estimates. For {\tt tess3}, the runtime was on
average 66 min. For $L = 100$k SNPs and $n = 150$ individuals, it took on
average 0.6 min (9.0 min) for the APLS (AQP) algorithm to compute ancestry
estimates. For {\tt tess3}, the runtime was on average 1.3 min. For those
values of $n$ and $L$, the APLS algorithm implementation ran about 2 to 100
times faster than the other algorithm implementations.

\paragraph{Human data analysis} To evaluate a case of model misspecification, we
analyzed data from the 1000 Genomes project for African Americans, Africans from
Nigeria and from Kenya, and Europeans from the United Kingdom and from Italy.
Using the default values for the hyper-parameters, the Laplacian matrix was a
block diagonal matrix where each block corresponded to one of the five
populations. The spatial variogram exhibited a flat shape. For $K = 2$, the APLS
estimates for the African American population were equal to $24.2 \%$ for
European ancestors and $75.8 \%$ for African ancestors. The corresponding {\tt
  snmf} estimates were equal to $22.4 \%$ for European ancestors and $77.6 \%$
for African ancestors. For $K = 3$, the APLS estimates for the African American
population were equal to $21.4 \%$ for European ancestors, $51.8 \%$ for West
African ancestors and $26.8 \%$ for East African ancestors. The corresponding
{\tt snmf} estimates were equal to $22.2 \%$ for European ancestors, $68.4 \%$
for West African ancestors and $9.4 \%$ for East African ancestors. Overall, the
results obtained with our spatial method for African Americans were similar to
those obtained with {\tt snmf}. The main difference between APLS and {\tt snmf}
estimates were for African populations. For Africans, {\tt snmf} detected two
distinct genetic clusters whereas APLS detected a larger proportion of shared
ancestry between Eastern and Western populations.

\paragraph{Application to European ecotypes of {\it Arabidopsis thaliana}} We
used the APLS algorithm to survey spatial population genetic structure and
perform a genome scan for adaptive alleles in European ecotypes of the plant
species {\it A. thaliana}. The cross validation criterion decreased rapidly from
$K=1$ to $K=3$ clusters, indicating that there were three main ancestral groups
in Europe, corresponding to geographic regions in Western Europe, Eastern and
Central Europe and Northern Scandinavia. For $K$ greater than four, the values
of the cross validation criterion decreased in a slower way, indicating that
subtle substructure resulting from complex historical isolation-by-distance
processes could also be detected (Figure~\ref{fig:fig5}). The spatial analysis provided an
approximate range of $\sigma = 150$km for the spatial variogram (Figure~\ref{fig:fig5}).
Figure~\ref{fig:map} displays the $Q$-matrix estimate interpolated on a geographic map of
Europe for $K = 6$ ancestral groups. The estimated admixture coefficients
provided clear evidence for the clustering of the ecotypes in spatially
homogeneous genetic groups.

\paragraph{Targets of selection in {\it A. thaliana} genomes} Tests based on the
$F^Q_{\rm ST}$ statistic were applied to the 241k SNP data set to reveal new
targets of natural selection in the {\it A. thaliana} genome. {\it A. thaliana}
occurs in a broad variety of habitats, and local adaptation to the environment
is acknowledged to be important in shaping its genetic diversity through
space~\citep{Hancock2011, Fournier-Level2011}. The APLS algorithm was run on the
1,095 European lines of {\it A. thaliana} with $K=6$ ancestral populations and
$\sigma = 1.5$ for the range parameter. Using the Benjamini-Hochberg algorithm to
control the FDR at the level $1\%$, the program produced a list of 12,701
candidate SNPs, including linked loci and representing 3\% of the total number
of loci. The top 100 candidates included SNPs in the flowering-related genes
SHORT VEGETATIVE PHASE (SVP), COP1-interacting protein 4.1 (CIP4.1) and FRIGIDA
(FRI) ($p$-values $< 10^{-300}$). These genes were detected by previous scans
for selection on this dataset~\citep{Horton2012}. We performed a gene ontology
enrichment analysis using AmiGO in order to evaluate which biological functions
might be involved in local adaptation in Europe. We found a significant
over-representation of genes involved in cellular processes (fold enrichment of
1.06, $p$-value equal to 0.0215 after Bonferonni correction).


\chapter{Problem set Solutions Fourier}
\begin{abox}
	Practise Set-1
\end{abox}
\begin{enumerate}[label=\color{ocre}\textbf{\arabic*.}]
	\item   The first few terms in the Laurent series for $\frac{1}{(z-1)(z-2)}$ in the region $1 \leq|z| \leq 2$ and around $z=1$ is
	{\exyear{NET/JRF(JUNE-2012)}}
	\begin{tasks}(1)
		\task[\textbf{A.}] $\frac{1}{2}\left[1+z+z^{2}+\ldots\right]\left[1+\frac{z}{2}+\frac{z^{2}}{4}+\frac{z^{3}}{8}+\ldots .\right]$
		\task[\textbf{B.}] $\frac{1}{1-z}-z-(1-z)^{2}+(1-z)^{3}+\ldots .$
		\task[\textbf{C.}] $\frac{1}{\mathrm{z}^{2}}\left[1+\frac{1}{\mathrm{z}}+\frac{1}{\mathrm{z}^{2}}+\ldots .\right]\left[1+\frac{2}{\mathrm{z}}+\frac{4}{\mathrm{z}^{2}}+\ldots . .\right]$
		\task[\textbf{D.}]  $2(z-1)+5(z-1)^{2}+7(z-1)^{3}+\ldots$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\frac{1}{(z-1)(z-2)}&=\frac{1}{z-2}-\frac{1}{z-1}=\frac{1}{1-z}+\frac{1}{(z-1)-1}\\&=\frac{1}{1-z}-(1+(1-z))^{-1}\\
		&=\frac{1}{1-z}-\left[1+(1-z)+\frac{(-1)(-2)}{2 !}(1-z)^{2}+\frac{(-1)(-2)(-3)}{3 !}(1-z)^{3} \ldots\right]\\
		&=\frac{1}{1-z}-\left[z+(1-z)^{2}-(1-z)^{3}+\ldots . .\right]
		\end{align*}
		So the correct answer is \textbf{Option (B)}
	\end{answer}
	\item Consider a sinusoidal waveform of amplitude $1 V$ and frequency $f_{0}$. Starting from an arbitrary initial time, the waveform is sampled at intervals of $\frac{1}{2 f_{0}}$. If the corresponding Fourier spectrum peaks at a frequency $\bar{f}$ and an amplitude $\bar{A}$, them
	{\exyear{NET/JRF(JUNE-2012)}}
	\begin{tasks}(2)
		\task[\textbf{A.}] $\bar{f}=2 f_{0}$ and $\bar{A}=1 V$
		\task[\textbf{B.}] $\bar{f}=2 f_{0}$ and $0 \leq \bar{A} \leq 1 V$
		\task[\textbf{C.}] $\bar{f}=0$ and $\bar{A}=1 V$
		\task[\textbf{D.}] $\bar{f}=\frac{f_{0}}{2}$ and $\bar{A}=\frac{1}{\sqrt{2}} V$
	\end{tasks}
	\begin{answer}$\left. \right. $
		\begin{figure}[H]
			\centering
			\includegraphics[height=4cm,width=8cm]{diagram-20211005(11)-crop}
		\end{figure}
		\begin{align*}
		y&=1 \sin \left(2 \pi f_{0} t\right)\\
		\text{The Fourier transform is:}\\
		F(y)&=\frac{1}{2}\left[\delta\left(f+f_{0}\right)\right]-\delta\left[f-f_{0}\right]\\
		\text{In Fourier domain }\bar{f}&=f_{0}, \bar{A}=\frac{1}{2}
		\end{align*}
		So the correct answer is \textbf{Option (B)}
	\end{answer}
	\item The Fourier transform of the derivative of the Dirac $\delta-$ function, namely $\delta^{\prime}(x)$, is proportional to
	{\exyear{NET/JRF(DEC-2013)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] 0
		\task[\textbf{B.}] 1
		\task[\textbf{C.}] $\sin k$
		\task[\textbf{D.}] $i k$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\text{Fourier transform of }\delta^{\prime}(x)\\
		H(K)=\int_{-\infty}^{\infty} \delta^{\prime}(x) e^{i k x} d x&=i k e^{(k \cdot 0)}=i k
		\end{align*}
		So the correct answer is \textbf{Option (D)}
	\end{answer}
	\item The Laplace transform of $6 t^{3}+3 \sin 4 t$ is
	{\exyear{NET/JRF(JUNE-2015)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $\frac{36}{s^{4}}+\frac{12}{s^{2}+16}$
		\task[\textbf{B.}] $\frac{36}{s^{4}}+\frac{12}{s^{2}-16}$
		\task[\textbf{C.}] $\frac{18}{s^{4}}+\frac{12}{s^{2}-16}$
		\task[\textbf{D.}] $\frac{36}{s^{3}}+\frac{12}{s^{2}+16}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		L\left[6 t^{3}+3 \sin 4 t\right] &\quad \because L\left[t^{n}\right]=\frac{\sqrt{n+1}}{s^{n+1}}\\
		\because L[\sin a t]&=\frac{a}{\left(s^{2}+a^{2}\right)}\\
		L\left[6 t^{3}+3 \sin 4 t\right]&=\frac{6 \times \sqrt{4}}{s^{4}}+\frac{3 \times 4}{s^{2}+16}=\frac{36}{s^{4}}+\frac{12}{s^{2}+16}
		\end{align*}
		So the correct answer is \textbf{Option (A)}
	\end{answer}
	\item  The Fourier transform of $f(x)$ is $\tilde{f}(k)=\int_{-\infty}^{+\infty} d x e^{i k x} f(x)$.
	If $f(x)=\alpha \delta(x)+\beta \delta^{\prime}(x)+\gamma \delta^{\prime \prime}(x)$, where $\delta(x)$ is the Dirac delta-function (and prime denotes derivative), what is $\tilde{f}(k) ?$
	{\exyear{NET/JRF(DEC-2015)}}
	\begin{tasks}(2)
		\task[\textbf{A.}] $\alpha+i \beta k+i \gamma k^{2}$
		\task[\textbf{B.}] $\alpha+\beta k-\gamma k^{2}$
		\task[\textbf{C.}]  $\alpha-i \beta k-\gamma k^{2}$
		\task[\textbf{D.}] $i \alpha+\beta k-i \gamma k^{2}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\tilde{f}(k)&=\int_{-\infty}^{\infty} d x e^{i k x}\left(\alpha \delta(x)+\beta \delta^{\prime}(x)+\gamma \delta^{\prime \prime}(x)\right)\\
		\int_{-\infty}^{\infty} \alpha \delta(x) e^{i k x} d x&=\alpha\\
		\int_{-\infty}^{\infty} \beta \delta^{\prime}(x) e^{i k x} d x&=\beta\left[\left.e^{i k x} \delta(x)\right|_{-\infty} ^{\infty}-\int_{-\infty}^{\infty} i k e^{i k x} \delta(x) d x\right]=-i \beta k\\
		\int_{-\infty}^{\infty} \gamma \delta^{\prime \prime}(x) e^{i k x} d x&=-\gamma k^{2}
		\end{align*}
		So the correct answer is \textbf{Option (C)}
	\end{answer}
	\item  What is the Fourier transform $\int d x e^{i l x} f(x)$ of
	$$
	f(x)=\delta(x)+\sum_{n=1}^{\infty} \frac{d^{n}}{d x^{n}} \delta(x)
	$$
	where $\delta(x)$ is the Dirac delta-function?
	{\exyear{NET/JRF(JUNE-2016)}}
	\begin{tasks}(4)
		\task[\textbf{A.}]  $\frac{1}{1-i k}$
		\task[\textbf{B.}] $\frac{1}{1+i k}$
		\task[\textbf{C.}] $\frac{1}{k+i}$
		\task[\textbf{D.}] $\frac{1}{k-i}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		f(x)&=\delta(x)+\sum_{n=1}^{\infty} \frac{d^{n}}{d x^{n}} \delta(x)\\&=\sum_{n=0}^{\infty} \frac{d^{n}}{d x^{n}} \delta(x)=\sum_{n=0}^{\infty} \delta^{(n)}(x)\\
		\because F[\delta(x)]&=1 \Rightarrow F\left[\delta^{(n)}(x)\right]\\&=(-i k)^{n} F[\delta(x)]=(-i k)^{n}\\
		\because f(x)&=\sum_{n=0}^{\infty} \delta^{(n)}(x)\\
		\Rightarrow F[f(x)]&=\sum_{n=0}^{\infty}(-i k)^{n}=1-i k+(i k)^{2}-(i k)^{3}+\ldots .\\&=\frac{1}{1-(-i k)}=\frac{1}{1+i k}
		\end{align*}
		So the correct answer is \textbf{Option (B)}
	\end{answer}
	\item The Laplace transform of
	$$
	f(t)=\left\{\begin{array}{cc}
	\frac{t}{T}, & 0<t<T \\
	1 & t>T
	\end{array}\right.
	$$
	is
	{\exyear{NET/JRF(DEC-2016)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $\frac{-\left(1-e^{-s T}\right)}{s^{2} T}$
		\task[\textbf{B.}] $\frac{\left(1-e^{-s T}\right)}{s^{2} T}$
		\task[\textbf{C.}] $\frac{\left(1+e^{-s T}\right)}{s^{2} T}$
		\task[\textbf{D.}] $\frac{\left(1-e^{s T}\right)}{s^{2} T}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\intertext{we can write}
		f(t)&=\left[u_{0}(t)-u_{T}(t)\right] \frac{t}{T}+u_{T}(t)\\&=\left[1-u_{T}(t)\right] \frac{t}{T}+u_{T}(t)=\frac{t}{T}-u_{T}(t) \frac{t}{T}+u_{T}(t)
		\intertext{Hence the transform of $f(t)$ is}
		L\{f(t)\}&=L\left\{\frac{t}{T}\right\}-L\left\{u_{T}(t)\left[\frac{(t-T)+T}{T}\right]\right\}+L\left\{u_{T}(t)\right\}\\
		&=\frac{1}{s^{2} T}-\frac{e^{-s T}}{T}\left(\frac{1}{s^{2}}+\frac{T}{s}\right)+\frac{e^{-s T}}{s}=\frac{1-e^{-s T}}{s^{2} T}
		\end{align*}
		So the correct answer is \textbf{Option (B)}
	\end{answer}
	\item The Fourier transform $\int_{-\infty}^{\infty} d x f(x) e^{i k x}$ of the function $f(x)=\frac{1}{x^{2}+2}$ is
	{\exyear{NET/JRF(DEC-2016)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $\sqrt{2} \pi e^{-\sqrt{2}|| \mid}$
		\task[\textbf{B.}] $\sqrt{2} \pi e^{-\sqrt{2 k}}$
		\task[\textbf{C.}] $\frac{\pi}{\sqrt{2}} e^{-\sqrt{2 k}}$
		\task[\textbf{D.}] $\frac{\pi}{\sqrt{2}} e^{-\sqrt{2}|k|}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\text{Fourier transform of }f(x)&=\frac{1}{x^{2}+a^{2}}, \\ a>0\text{ is }\int \frac{1}{x^{2}+a^{2}} e^{i k x} d x&=\frac{\pi}{a} e^{-a|k|}\\
		\text{Hence }\int \frac{1}{x^{2}+a^{2}} e^{i k x} d x&=\frac{\pi}{\sqrt{2}} e^{-\sqrt{2}|k|}
		\end{align*}
		So the correct answer is \textbf{Option (D)}
	\end{answer}
	\item Consider the differential equation $\frac{d y}{d t}+a y=e^{-b t}$ with the initial condition $y(0)=0$. Then the Laplace transform $Y(s)$ of the solution $y(t)$ is
	{\exyear{NET/JRF(DEC-2017)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $\frac{1}{(s+a)(s+b)}$
		\task[\textbf{B.}] $\frac{1}{b(s+a)}$
		\task[\textbf{C.}] $\frac{1}{a(s+b)}$
		\task[\textbf{D.}] $\frac{e^{-a}-e^{-b}}{b-a}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\text{Given }\frac{d y}{d t}+a y&=e^{-b t}
		\intertext{Taking Laplace transform of both sides}
		\text{	We obtain}\\
		L\left\{\frac{d y}{d t}\right\}+a L\{y(t)\}&=L\left\{e^{-b t}\right\} \Rightarrow s Y(s)-y(0)+a Y(s)=\frac{1}{s+b}\\
		\text{Since, }	y(0)&=0,\text{ we obtain}\\
		(s+a) Y(s)&=\frac{1}{s+b} \Rightarrow Y(s)=\frac{1}{(s+a)(s+b)}
		\end{align*}
		So the correct answer is \textbf{Option (A)}
	\end{answer}
	\item  The Fourier transform $\int_{-\infty}^{\infty} d x f(x) e^{i k x}$ of the function $f(x)=e^{-|x|}$
	{\exyear{NET/JRF(JUNE-2018)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $-\frac{2}{1+k^{2}}$
		\task[\textbf{B.}] $-\frac{1}{2\left(1+k^{2}\right)}$
		\task[\textbf{C.}] $\frac{2}{1+k^{2}}$
		\task[\textbf{D.}] $\frac{2}{\left(2+k^{2}\right)}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\int_{-\infty}^{+\infty} d x e^{-|x|} e^{i k x}&=\int_{-\infty}^{+\infty} d x e^{-|x|} \cos k x d x\text{ odd functions in }k x\text{ vanishes}\\
		\Rightarrow 2 \int_{0}^{\infty} e^{-x} \cos k x d x&=2 \frac{e^{-x}}{1+k^{2}}[-\cos k x+k \sin k x]_{0}^{\infty}\\
		\because \int e^{a x} \cos b x d x&=\frac{e^{a x}}{a^{2}+b^{2}}[a \cos b x+b \sin b x]\\
		\Rightarrow 2 \int_{0}^{\infty} e^{-x} \cos k x d x&=2 \frac{e^{0}}{1+k^{2}}=\frac{2}{1+k^{2}}
		\end{align*}
		So the correct answer is \textbf{Option (C)}
	\end{answer}
	\item The function $f(t)$ is a periodic function of period $2 \pi$. In the range $(-\pi, \pi)$, it equals $e^{-t}$. If $f(t)=\sum_{-\infty}^{\infty} c_{n} e^{\text {int }}$ denotes its Fourier series expansion, the sum $\sum_{-\infty}^{\infty}\left|c_{n}\right|^{2}$ is
	{\exyear{NET/JRF(DEC-2019)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] 1
		\task[\textbf{B.}] $\frac{1}{2 \pi}$
		\task[\textbf{C.}] $\frac{1}{2 \pi} \cosh (2 \pi)$
		\task[\textbf{D.}]  $\frac{1}{2 \pi} \sinh (2 \pi)$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		f(t)&=e^{-t} \quad-\pi<x<\pi\\
		f(t)&=\sum_{-\infty}^{\infty} c_{n} e^{\mathrm{int}}\\
		\sum_{-\infty}^{\infty}\left|c_{n}\right|^{2}&=\frac{1}{2 \pi} \int_{-\pi}^{\pi} e^{-2 t} d t=\left.\frac{1}{2 \pi} \cdot \frac{e^{-2 t}}{-2}\right|_{-\pi} ^{\pi}\\&=\frac{1}{2 \pi}\left[\frac{e^{-2 \pi}-e^{2 \pi}}{-2}\right]=\frac{1}{2 \pi} \sinh 2 \pi
		\end{align*}
		So the correct answer is \textbf{Option (D)}
	\end{answer}
\end{enumerate}
\colorlet{ocre1}{ocre!70!}
\colorlet{ocrel}{ocre!30!}
\setlength\arrayrulewidth{1pt}
\begin{table}[H]
	\centering
	\arrayrulecolor{ocre}
	\begin{tabular}{|p{1.5cm}|p{1.5cm}||p{1.5cm}|p{1.5cm}|}
		\hline
		\multicolumn{4}{|c|}{\textbf{Answer key}}\\\hline\hline
		\rowcolor{ocrel}Q.No.&Answer&Q.No.&Answer\\\hline
		1&\textbf{B} &2&\textbf{B}\\\hline
		3&\textbf{D} &4&\textbf{A} \\\hline
		5&\textbf{C} &6&\textbf{B} \\\hline
		7&\textbf{B}&8&\textbf{D}\\\hline
		9&\textbf{A}&10&\textbf{C}\\\hline
		11&\textbf{D} &&\textbf{}\\\hline

	\end{tabular}
\end{table}

\newpage
\begin{abox}
	Practise Set-2
\end{abox}
\begin{enumerate}[label=\color{ocre}\textbf{\arabic*.}]
	\item If $f(x)=\left\{\begin{array}{ll}0 & \text { for } x<3, \\ x-3 & \text { for } x \geq 3\end{array}\right.$ then the Laplace transform of $f(x)$ is
	{\exyear{GATE 2010}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $s^{-2} e^{3 s}$
		\task[\textbf{B.}] $s^{2} e^{3 s}$
		\task[\textbf{C.}] $s^{-2}$
		\task[\textbf{D.}] $s^{-2} e^{-3 s}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		L\{f(x)\}&=\int_{0}^{\infty} e^{-s x} f(x) d x\\&=\int_{0}^{3} e^{-s x} f(x) d x+\int_{3}^{\infty} e^{-s x} f(x) d x\\&=\int_{3}^{\infty}(x-3) e^{-s x} d x\\
		L\{f(x)\}&=\left.(x-3) \frac{e^{-s x}}{-s}\right|_{3} ^{\infty}-\int_{3}^{\infty} 1 \cdot\left(\frac{e^{-s x}}{-s}\right) d x\\&=0+\frac{1}{s} \int_{3}^{\infty} e^{-s x} d x\\&=\frac{1}{s}\left[\frac{e^{-s x}}{-s}\right]_{3}^{\infty}=s^{-2} e^{-3 s}
		\end{align*}
		So the correct answer is \textbf{Option (D)}
	\end{answer}
	\item The coefficient of $e^{i k x}$ in the Fourier expansion of $u(x)=A \sin ^{2}(\alpha x)$ for $k=-2 \alpha$ is
	{\exyear{GATE 2017}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $\frac{A}{4}$
		\task[\textbf{B.}] $\frac{-A}{4}$
		\task[\textbf{C.}] $\frac{A}{2}$
		\task[\textbf{D.}] $\frac{-A}{2}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\text{	Since, }\sin (\alpha x)&=\frac{e^{i \alpha x}-e^{-i \alpha x}}{2 i} \Rightarrow \sin ^{2}(\alpha x)\\&=\frac{e^{i 2 \alpha x}-2+e^{-2 i \alpha x}}{(-4)}\\
		\text{Since, }2 \alpha&=-k,\text{ hence }\sin ^{2}(\alpha x)\\&=\frac{e^{-i k x}-2+e^{i k x}}{(-4)}\\
		\text{Hence, }c_{k}&=\frac{A}{2 \pi} \int_{-\pi}^{\pi} \sin ^{2}(\alpha x) d x\\&=-\frac{A}{8 \pi}\left[\int_{-\pi}^{\pi} e^{-i k x} e^{-i k x} d x-2 \int_{-\pi}^{\pi} e^{-i k x} d x+\int_{-\pi}^{\pi} e^{-i k x} e^{i k x} d x\right]\\
		&=-\frac{A}{8 \pi}\left[\int_{-\pi}^{\pi} e^{-2 i k x} d x-2 \int_{-\pi}^{\pi} e^{-i k x} d x+\int_{-\pi}^{\pi} d x\right]
		\intertext{The first two integrals are zero and the third integral has the value $2 \pi$.
			Thus,}
		c_{k}&=-\frac{A}{8 \pi}(2 \pi)=-\frac{A}{4}
		\end{align*}
		So the correct answer is \textbf{Option (B)}
	\end{answer}
	\item Given the fundamental constants $\hbar$ (Planck's constant), $G$ (universal gravitation constant) and $c$ (speed of light), which of the following has dimension of length?
	{\exyear{JEST 2014}}
	\begin{tasks}(2)
		\task[\textbf{A.}]$\sqrt{\frac{\hbar G}{c^{3}}}$
		\task[\textbf{B.}] $\sqrt{\frac{\hbar G}{c^{5}}}$
		\task[\textbf{C.}]$\frac{\hbar G}{c^{3}}$
		\task[\textbf{D.}] $\sqrt{\frac{\hbar c}{8 \pi G}}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\left[\frac{\left[M L^{2} T^{-1}\right]\left[M^{-1} L^{3} T^{-2}\right]}{L^{3} T^{-3}}\right]^{\frac{1}{2}}&=\left[L^{2}\right]^{\frac{1}{2}}=L\\
		\hbar=\left[M L^{2} T^{-1}\right], G=\frac{g r^{2}}{m}&=\left[M^{-1} L^{3} T^{-2}\right]
		\end{align*}
		So the correct answer is \textbf{Option (A)}
	\end{answer}
	\item The Fourier transform of the function $\frac{1}{x^{4}+3 x^{2}+2}$ up to proportionality constant is
	{\exyear{JEST 2017}}
	\begin{tasks}(2)
		\task[\textbf{A.}]$\sqrt{2} \exp \left(-k^{2}\right)-\exp \left(-2 k^{2}\right)$
		\task[\textbf{B.}]$\sqrt{2} \exp (-|k|)-\exp (-\sqrt{2}|k|)$
		\task[\textbf{C.}]$\sqrt{2} \exp (-\sqrt{|k|})-\exp (-\sqrt{2|k|})$
		\task[\textbf{D.}]  $\sqrt{2} \exp \left(-\sqrt{2} k^{2}\right)-\exp \left(-2 k^{2}\right)$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		f(x)&=\frac{1}{\left(x^{4}+3 x^{2}+2\right)}=\frac{1}{\left(x^{2}+1\right)}-\frac{1}{\left[x^{2}+(\sqrt{2})^{2}\right]}
		\intertext{Now, Fourier transform of $f(x)$ is,}
		F(p)&=A \int_{-\infty}^{\infty} f(x) e^{-1 k x} d x\\
		&=A \int_{-\infty}^{\infty}\left[\frac{1}{\left(x^{2}+1\right)}-\frac{1}{x^{2}+(\sqrt{2})^{2}}\right] e^{-i k x} d x=A\left[\int_{-\infty}^{\infty} \frac{1}{\left(x^{2}+1\right)} \times e^{-i k x} d x-\int_{-\infty}^{\infty} \frac{e^{-i k x}}{x^{2}+(\sqrt{2})^{2}} d x\right]\\
		\because &\int_{-\infty}^{\infty} \frac{1}{\left(x^{2}+a^{2}\right)} e^{-i k x} d x=\sqrt{\frac{\pi}{2}} \frac{e^{-a|k|}}{a}\\
		F(k)&=A\left[\sqrt{\frac{\pi}{2}} \frac{e^{-|k|}}{1}-\sqrt{\frac{\pi}{2}} \frac{e^{-\sqrt{2} \mid k}}{\sqrt{2}}\right]=\frac{A \sqrt{\pi}}{2}[\sqrt{2} \exp (-|k|)-\exp (-\sqrt{2}|k|)]\\
		\end{align*}
		So the correct answer is \textbf{Option (B)}
	\end{answer}
	\item The function $f(x)=\cosh x$ which exists in the range $-\pi \leq x \leq \pi$ is periodically repeated between $x=(2 m-1) \pi$ and $(2 m+1) \pi$, where $m=-\infty$ to $\infty$. Using Fourier series, indicate the correct relation at $x=0$
	{\exyear{JEST 2017}}
	\begin{tasks}(2)
		\task[\textbf{A.}] $\sum_{n=-\infty}^{\infty} \frac{(-1)^{n}}{1-n^{2}}=\frac{1}{2}\left(\frac{\pi}{\cosh \pi}-1\right)$
		\task[\textbf{B.}]$\sum_{n=-\infty}^{\infty} \frac{(-1)^{n}}{1-n^{2}}=2 \frac{\pi}{\cosh \pi}$
		\task[\textbf{C.}]$\sum_{n=-\infty}^{\infty} \frac{(-1)^{-n}}{1+n^{2}}=2 \frac{\pi}{\sinh \pi}$
		\task[\textbf{D.}] $\sum_{n=1}^{\infty} \frac{(-1)^{n}}{1+n^{2}}=\frac{1}{2}\left(\frac{\pi}{\sinh \pi}-1\right)$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		f(x)&=\cosh x, \quad-\pi \leq x \leq \pi\\
		\text{Here, }a_{0}&=\frac{1}{2 \pi} \int_{-\pi}^{\pi} \cosh x d x=\frac{1}{2 \pi}[\sinh x]_{-\pi}^{\pi}=\frac{\sinh \pi}{\pi}\\
		b_{n}&=0,\text{ due to even function}\\
		\text{	and }a_{n}&=\frac{1}{2 \pi} \int_{-\pi}^{\pi}\left(e^{x}+e^{-x}\right) \cos n x d x\qquad
		\left[\because \cosh x=\frac{1}{2}\left(e^{x}+e^{-x}\right)\right]\\
		a_{n}&=\frac{1}{2 \pi}\left[\frac{e^{x}}{\left(1+n^{2}\right)}(\cos n x+n \sin n x)+\frac{e^{-x}}{1+n^{2}}(-\cos n x+n \sin n x)\right]_{-\pi}^{\pi}\\
		&=\frac{1}{2 \pi}\left[\frac{e^{\pi}(-1)^{n}}{\left(1+n^{2}\right)}-\frac{e^{-\pi}(-1)^{n}}{\left(1+n^{2}\right)}-\frac{e^{-\pi}(-1)^{n}}{\left(1+n^{2}\right)}+\frac{e^{\pi}(-1)^{n}}{\left(1+n^{2}\right)}\right]\\&=\frac{2(-1)^{n} \cdot 2 \sinh \pi}{2 \pi\left(1+n^{2}\right)}=\frac{2(-1)^{n} \sinh \pi}{\pi\left(1+n^{2}\right)}\\
		\text{	Hence, }f(x)&=a_{0}+\sum_{n=1}^{\infty}\left(a_{n} \cos n x+b_{n} \sin n x\right) \Rightarrow \cosh x=\frac{\sinh \pi}{\pi}+\sum_{n=1}^{\infty} \frac{2(-1)^{n} \sinh \pi}{\pi\left(1+n^{2}\right)} \cos n x\\
		\text{At }x&=0,\\
		\sum_{n=1}^{\infty} \frac{2(-1)^{n} \sinh \pi}{\pi\left(1+n^{2}\right)}&=\left(1-\frac{\sinh \pi}{\pi}\right) \Rightarrow \sum_{n=1}^{\infty} \frac{(-1)^{n}}{\left(1+n^{2}\right)}=\frac{1}{2}\left[\frac{\pi}{\sinh \pi}-1\right]
		\end{align*}
		So the correct answer is \textbf{Option (D)}
	\end{answer}
	\item The Laplace transform of $\frac{(\sin (a t)-a t \cos (a t))}{\left(2 a^{3}\right)}$ is
	{\exyear{JEST 2018}}
	\begin{tasks}(2)
		\task[\textbf{A.}]$\frac{2 a s}{\left(s^{2}+a^{2}\right)^{2}}$
		\task[\textbf{B.}]$\frac{s^{2}-a^{2}}{\left(s^{2}+a^{2}\right)^{2}}$
		\task[\textbf{C.}]$\frac{1}{(s+a)^{2}}$
		\task[\textbf{D.}] $\frac{1}{\left(s^{2}+a^{2}\right)^{2}}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		L\left\{\frac{\sin a t-a t \cos a t}{2 a^{3}}\right\}=\frac{1}{\left(s^{2}+a^{2}\right)^{2}}
		\end{align*}
		So the correct answer is \textbf{Option (D)}
	\end{answer}
\end{enumerate}
\colorlet{ocre1}{ocre!70!}
\colorlet{ocrel}{ocre!30!}
\setlength\arrayrulewidth{1pt}
\begin{table}[H]
	\centering
	\arrayrulecolor{ocre}
	\begin{tabular}{|p{1.5cm}|p{1.5cm}||p{1.5cm}|p{1.5cm}|}
		\hline
		\multicolumn{4}{|c|}{\textbf{Answer key}}\\\hline\hline
		\rowcolor{ocrel}Q.No.&Answer&Q.No.&Answer\\\hline
		1&\textbf{D} &2&\textbf{B}\\\hline
		3&\textbf{A} &4&\textbf{B} \\\hline
		5&\textbf{D} &6&\textbf{D} \\\hline

	\end{tabular}
\end{table}

CSIR- Mathematical Physics/chapter/Problem set Solutions Fourier.tex


\section{Refinement Reflection}
\label{sec:formalism}
\label{sec:types-reflection}
\label{sec:refinementreflection:theory}
Next, we formalize refinement reflection
via a core calculus \corelan.
%
We define a decidable SMT language \smtlan to approximate the
higher order, potentially diverging
target language \corelan
and present a decidable and sound type system
for \corelan.

\subsection{Syntax}
\input{text/refinementreflection/syntax}
%
Figure~\ref{fig:syntax} summarizes the syntax of \corelan,
which is essentially the calculus \undeclang (from~\S~\ref{sec:language})
with explicit recursion and a special $\erefname$ binding
to denote terms that are reflected into the refinement logic.
%
The elements of \corelan are layered into
primitive constants, values, expressions, binders
and programs.

\mypara{Constants}
The primitive constants of \corelan
include all the primitive logical
operators $\op$, here, the set $\{ =, <\}$.
%
Moreover, they include the
primitive booleans $\etrue$, $\efalse$,
integers $\mathtt{-1}, \mathtt{0}$, $\mathtt{1}$, \etc,
and logical operators $\mathtt{\land}$, $\mathtt{\lor}$, $\mathtt{\lnot}$, \etc.

\mypara{Data Constructors}
%
Data constructors are special constants.
% Each data type has an equality predicate $\haseq{T}$
% that is true only if values of type $T$ can be finitely compared.
For example, the data type \tintlist, which represents
finite lists of integers, has two data constructors: $\dnull$ (nil)
and $\dcons$ (cons).

\mypara{Values \& Expressions}
%
The values of \corelan include
constants, $\lambda$-abstractions
$\efun{x}{\typ}{e}$, and fully
applied data constructors $D$
that wrap values.
%
The expressions of \corelan
include values, variables $x$,
applications $\eapp{e}{e}$, and
$\mathtt{case}$ expressions.

\mypara{Binders \& Programs}
%
A \emph{binder} $\bd$ is a series of possibly recursive
$\mathtt{let}$ definitions, followed by an expression.
%
A \emph{program} \prog is a series of $\erefname$
definitions, each of which names a function
that is reflected into the refinement
logic, followed by a binder.
%
The stratification of programs via binders
is required so that arbitrary recursive definitions
are allowed in the program but cannot be inserted into the logic
via refinements or reflection.
%
(We \emph{can} allow non-recursive $\mathtt{let}$
binders in expressions $e$, but omit them for simplicity).

\subsection{Operational Semantics}
We define $\hookrightarrow$ to be the small step, call-by-name
$\beta$-reduction semantics for \corelan.
%
We evaluate reflected terms %$\erefb{x}{\gtyp}{e}{\prog}$
as recursive $\mathtt{let}$ bindings, with extra termination-check
constraints imposed by the type system:
%
$$
\erefb{x}{\gtyp}{e}{\prog}
\hookrightarrow
\eletb{x}{\gtyp}{e}{\prog}
$$
%
We define $\evalsto{}{}$ to be the reflexive,
transitive closure of $\evals{}{}$.
%
Moreover, we define $\betaeq{}{}$ to be the reflexive,
symmetric, and transitive closure of $\evals{}{}$.

\mypara{Constants} Application of a constant requires the
argument be reduced to a value; in a single step, the
expression is reduced to the output of the primitive
constant operation, \ie ${c\ v} \hookrightarrow \ceval{c}{v}$.
%
For example, consider $=$, the primitive equality
operator on integers.
%
We have $\ceval{=}{n} \defeq =_n$
where $\ceval{=_n}{m}$ equals \etrue
iff $m$ is the same as $n$.
%

\mypara{Equality}
We assume that the equality operator
is defined \emph{for all} values,
and, for functions, is defined as
extensional equality.
%
That is, for all
$f$ and
$f'$,
$\evals{(f = f')}{\etrue}$
   $\mbox{iff}$
  $\forall v.\ \betaeq{f\ v}{f'\ v}$.
%
We assume source \emph{terms} only contain implementable equalities
over non-function types; while function extensional equality only appears
in \emph{refinements} and is approximated by the underlying logic.

\subsection{Types}

\corelan types include basic types, which are \emph{refined} with predicates,
and dependent function types.
%
\emph{Basic types} \btyp comprise integers, booleans, and a family of data-types
$T$ (representing lists, trees \etc).
%
For example, the data type \tintlist represents lists of integers.
%
We refine basic types with predicates (boolean-valued expressions \refa) to obtain
\emph{basic refinement types} $\tref{v}{\btyp}{\refa}$.
%
%
We use \tlabel to mark provably terminating
computations and use
refinements to ensure that if
${{e}\text{:}{\tref{v}{\btyp^\tlabel}{\refa'}}}$,
then $e$ terminates (as in chapter~\ref{chapter:refinedhaskell}).
%
Finally, we have dependent \emph{function types} $\tfun{x}{\typ_x}{\typ}$
where the input $x$ has the type $\typ_x$ and the output $\typ$ may
refer to the input binder $x$.
%
We write $\btyp$ to abbreviate $\tref{v}{\btyp}{\etrue}$,
and \tfunbasic{\typ_x}{\typ} to abbreviate \tfun{x}{\typ_x}{\typ} if
$x$ does not appear in $\typ$.
%
%We use $r$ to refer to refinements.

\mypara{Constants}
For each constant $c$ we define its type \constty{c}, \eg%
%
$$
\begin{array}{lcl}
\constty{3} &\doteq& \tref{v}{\tint}{v = 3}\\
\constty{+} &\doteq& \tfun{\ttx}{\tint}{\tfun{\tty}{\tint}{\tref{v}{\tint}{v = x + y}}}\\
\constty{\leq} &\doteq& \tfun{\ttx}{\tint}{\tfun{\tty}{\tint}{\tref{v}{\tbool}{v \Leftrightarrow x \leq y}}}\\
\end{array}
$$
%

\subsection{Refinement Reflection}\label{subsec:logicalannotations}
%
The key idea in our work is to
\emph{strengthen} the output type of functions
with a refinement that \emph{reflects} the
definition of the function in the logic.
%
We do this by treating each
%
$\erefname$-binder
%
(${\erefb{f}{\gtyp}{e}{\prog}}$)
%
as a $\eletname$-binder with a reflected singleton type
%
(${\eletb{f}{\exacttype{\gtyp}{e}}{e}{\prog}}$)
%
during type checking (rule $\rtreflect$ in Figure~\ref{fig:typing}).

\mypara{Reflection}
%
We write \exacttype{\typ}{e} for the \emph{reflection}
of the term $e$ into the type $\typ$,  defined by strengthening
\typ as:
%
$$
\begin{array}{lcl}
\exacttype{\tref{v}{\btyp}{r}}{e}
  & \defeq
  & \tref{v}{\btyp}{r \land v = e}\\
\exacttype{\tfun{x}{\typ_x}{\typ}}{\efun{y}{}{e}}
  & \defeq
  & \tfun{x}{\typ_x}{\exacttype{\typ}{e\subst{y}{x}}}
\end{array}
$$
%
As an example, recall from \S~\ref{sec:refinementreflection:overview}
that the @reflect fib@ strengthens the type of
@fib@ with the refinement @fibP@.
%% NV In Overview, we have fibP v n = v = fib n && fibR v n
%% NV Here we get the reflection part (fibR v n)
%% NV which we can verify
%% NV at each fix invocation we also get the v = fib n portion
%% NV via the exact rule
%% NV We can not add the v = fib n as a port condition, because
%% NV we cannot prove it.

\mypara{Consequences for Verification}
%
Reflection has two consequences for verification.
%
First, the reflected refinement is \emph{not trusted};
it is itself verified (as a valid output type)
during type checking.
%
Second, instead of being tethered to quantifier
instantiation heuristics or having to program
``triggers'' as in Dafny~\citep{dafny} or
\fstar~\citep{fstar},
%
the programmer can predictably ``unfold'' the
definition of the function during a proof simply
by ``calling'' the function, which, as discussed
in~\S~\ref{sec:evaluation}, we have found to be
a very natural way of structuring proofs.

\subsection{The SMT logic \smtlan}
\corelan is a higher order, potentially diverging language
that cannot be used for decidable verification.
%
Next, we describe \smtlan, a conservative, first order
approximation of \corelan where higher order features are
approximated with uninterpreted functions,
yielding an SMT-based
algorithmic logic that enjoys soundness and decidability.

\input{text/refinementreflection/smtsyntax}

\mypara{Syntax}
%
Figure~\ref{fig:smtsyntax} summarizes the syntax
of \smtlan, the \emph{sorted} (SMT-)
decidable logic of quantifier-free equality,
uninterpreted functions and linear
arithmetic (QF-EUFLIA) ~\citep{Nelson81,SMTLIB2}.
%
The \emph{terms} of \smtlan include
integers $n$,
booleans $b$,
variables $x$,
data constructors $\dc$ (encoded as constants),
fully applied unary \unop and binary \binop operators,
and application $x\ \overline{\pred}$ of an uninterpreted function $x$.
%
The \emph{sorts} of \smtlan include the built-in
\tint and \tbool.
%
The interpreted functions of \smtlan, \eg
the logical constants $=$ and $<$,
%% NV and the uninterpreted functions app and lam
%% NV but we have not introduced these yet
have the function sort $\sort \rightarrow \sort$.
%
Other functional values in \corelan, \eg
reflected \corelan functions and
$\lambda$-expressions, have the first-order
uninterpreted sort \tsmtfun{\sort}{\sort}.
%
The universal sort \tuniv represents all other values.

\subsection{Transforming \corelan into \smtlan}
%
\label{subsec:embedding}

%\input{defuncrules}
%
A \emph{type environment} $\env$ is a sequence of type bindings
$\tbind{x_1}{\typ_1},$ $\ldots,\tbind{x_n}{\typ_n}$.
We use the type environment to define the judgment
\tologicshort{\env}{e}{\typ}{\pred}{\sort}{\smtenv}{\axioms}
that transforms a $\corelan$ term $e$
% under an environment $\env$,
into a $\smtlan$ term $\pred$.
%
Most of the transformation rules are identity
and can be found in~\cite{vazou16techrep}.
Here we discuss the non-identity ones.

\mypara{Embedding Types}
%
We embed \corelan types into \smtlan sorts as:
%
\[
\begin{array}{rclcrcl}
\embed{\tint}                       & \defeq &  \tint & \quad &
\embed{T}                           & \defeq &  \tuniv \\
\embed{\tbool}                      & \defeq &  \tbool & \quad &
\embed{\tfun{x}{\typ_x}{\typ}} & \defeq & \tsmtfun{\embed{\typ_x}}{\embed{\typ}}
\end{array}
\]

\mypara{Embedding Constants}
%
Elements shared on both \corelan and \smtlan
translate to themselves.
%
These elements include
booleans,
integers,
variables,
binary
and unary
operators.
%
SMT solvers do not support currying,
and so in \smtlan, all function symbols
must be fully applied.
%
Thus, we assume that all applications
to primitive constants and data
constructors are fully applied, \eg by converting
source terms like @(+ 1)@ to
@(\z -> z+1)@.
%

\mypara{Embedding Functions}
%
As \smtlan is a first-order logic, we
embed $\lambda$-abstraction
using the uninterpreted function
\smtlamname{}{}.
%
$$
\inference{
    \tologicshort{\env, \tbind{x}{\typ_x}}{e}{}{\pred}{}{}{} &
        \hastype{\env}{(\efun{x}{}{e})}{(\tfun{x}{\typ_x}{\typ})}\\
}{
	\tologicshort{\env}{\efun{x}{}{e}}{(\tfun{x}{\typ_x}{\typ})}
	        {\smtlamname{\embed{\typ_x}}{\embed{\typ}}\ {x}\ {\pred}}
	        {\sort'}{\smtenv, \tbind{f}{\sort'}}{\andaxioms{\{\axioms_{f_1}, \axioms_{f_2}\}}{\axioms}}
}[\lgfun]
$$
%
The term $\efun{x}{}{e}$ of type
${\typ_x \rightarrow \typ}$ is transformed
to
${\smtlamname{\sort_x}{\sort}\ x\ \pred}$
of sort
${\tsmtfun{\sort_x}{\sort}}$, where
%
$\sort_x$ and $\sort$ are respectively
$\embed{\typ_x}$ and $\embed{\typ}$,
%
${\smtlamname{\sort_x}{\sort}}$
is a special uninterpreted function
of sort
${\sort_x \rightarrow \sort\rightarrow\tsmtfun{\sort_x}{\sort}}$,
and
$x$ of sort $\sort_x$ and $r$ of sort $\sort$ are
the embedding of the binder and body, respectively.
%
As $\smtlamname{}{}$ is an SMT-function,
it \emph{does not} create a binding for $x$.
%
Instead, $x$ is renamed to
a \emph{fresh} name pre-declared in
the SMT logic.

\mypara{Embedding Applications}
%
Dually, we embed applications via
defunctionalization~\citep{Reynolds72}
with the uninterpreted $\smtappname{}{}$ function.
%
$$
\inference{
	\tologicshort{\env}{e'}{\typ_x}{\pred'}{\embed{\typ_x}}{\smtenv}{\axioms'}
	&
	\tologicshort{\env}{e}{\tfun{x}{\typ_x}{\typ}}{\pred}{\tsmtfun{\embed{\typ_x}}{\embed{\typ}}}{\smtenv}{\axioms}
	&
	\hastype{\env}{e}{{\typ_x}\rightarrow{\typ}}
}{
	\tologicshort{\env}{e\ e'}{\typ}{\smtappname{\embed{\typ_x}}{\embed{\typ}}\ {\pred}\ {\pred'}}{\embed{\typ}}{\smtenv}{\andaxioms{\axioms}{\axioms'}}
}[\lgapp]
$$
%
The term ${e\ e'}$, where $e$ and $e'$ have
types ${\typ_x \rightarrow \typ}$ and $\typ_x$,
is transformed to
${\tbind{\smtappname{\sort_x}{\sort}\ \pred\ \pred'}{\sort}}$
where
%
$\sort$ and $\sort_x$ are $\embed{\typ}$ and $\embed{\typ_x}$,
the
${\smtappname{\sort_x}{\sort}}$
is a special uninterpreted function of sort
${\tsmtfun{\sort_x}{\sort} \rightarrow \sort_x \rightarrow \sort}$,
and
$\pred$ and $\pred'$ are the respective translations of $e$ and $e'$.

\mypara{Embedding Data Types}
%
We translate each data constructor to a
predefined \smtlan constant ${\smtvar{\dc}}$ of
sort ${\embed{\constty{\dc}}}$.
$$
	\tologicshort{\env}{\dc}{\constty{\dc}}{\smtvar{\dc}}{\embed{\constty{\dc}}}{\emptyset}{\emptyaxioms}
$$
%
For each datatype, we assume the existence of reflected functions that
\emph{check} the top-level constructor
and \emph{select} their individual fields.
%
For example, for lists, we assume the existence of measures:
%
\begin{mcode}
  isNil []     = True     isCons (x:xs) = True
  isNil (x:xs) = False    isCons []     = False

  sel1 (x:xs)  = x        sel2 (x:xs)   = xs
\end{mcode}
%
Due to the simplicity of their syntax the above checkers and selectors
can be automatically instantiated in the logic
(\ie without actual calls to the reflected functions at source level)
using the measure mechanism (\S~\ref{sec:measures}).

To generalize, let $\dc_i$ be a data constructor such that
$$
\constty{\dc_i} \defeq \typ_{i,1} \rightarrow \dots \rightarrow \typ_{i,n} \rightarrow \typ
$$
Then the \emph{check function}
${\checkdc{{\dc_i}}}$ has the sort
$\tsmtfun{\embed{\typ}}{\tbool}$,
and the \emph{select function}
${\selector{\dc}{i,j}}$ has the sort
$\tsmtfun{\embed{\typ}}{\embed{\typ_{i,j}}}$.
%

% \mypara{Checking and Projection}
%
We translate case-expressions
of \corelan into nested $\mathtt{if}$
terms in \smtlan, by using the check
functions in the guards, and the
select functions for the binders
of each case.
$$
\inference{
	\tologicshort{\env}{e}{\typ_e}{\pred}{\embed{\typ_e}}{\smtenv}{\axioms} &&
	\tologicshort{\env}{e_i\subst{\overline{y_i}}{\overline{\selector{\dc_i}{}\ x}}\subst{x}{e}}{\typ}{\pred_i}{\embed{\typ}}{\smtenv}{\axioms_i}
}{
	\tologicshort{\env}{\ecase{x}{e}{\dc_i}{\overline{y_i}}{e_i}}{\typ}
	 {\eif{\smtappname{}{}\ \checkdc{\dc_1}\ \pred}{\pred_1}{\ldots} \ \mathtt{else}\ \pred_n}{\embed{\typ}}{\smtenv}
	 {\andaxioms{\axioms}{\axioms_i}}
}[\lgcase]
$$
%
For example, the body of the list append function
%
\begin{code}
  []     ++ ys = ys
  (x:xs) ++ ys = x : (xs ++ ys)
\end{code}
%
is reflected into the \smtlan refinement:
%
$$
\ite{\mathtt{isNil}\ \mathit{xs}}
    {\mathit{ys}}
    {\mathtt{sel1}\ \mathit{xs}\
       \dcons\
       (\mathtt{sel2}\ \mathit{xs} \ \mathtt{++}\  \mathit{ys})}
$$
%
We favor selectors to the axiomatic translation of
HALO~\citep{halo} to avoid
universally quantified formulas and the resulting
instantiation unpredictability.

\subsection{Typing Rules}
\input{text/refinementreflection/typing}
%
Next, we present the
typing, well-formedness, and subtyping~\citep{Knowles10,Vazou14}
rules of \corelan.

\mypara{Typing}
A judgment \hastype{\env}{\prog}{\typ} states that
the program $\prog$ has the type $\typ$ in
the environment $\env$.
That is, when the free variables in $\prog$ are
bound to expressions described by $\env$, the
program $\prog$ will evaluate to a value
described by $\typ$.

\mypara{Rules}
%
All but two of the rules are standard~\cite{Knowles10,Vazou14}.
%
First, rule \rtreflect is used to strengthen the type of each
reflected binder with its definition, as described previously
in \S~\ref{subsec:logicalannotations}.
%
Second, rule \rtexact strengthens the expression with
a singleton type equating the value and the expression
(\ie reflecting the expression in the type).
%
This is a generalization of the ``selfification'' rules
from \cite{Ou2004,Knowles10}, and is required to
equate the reflected functions with their definitions.
%
For example, the application $(\fib\ 1)$ is typed as
${\tref{v}{\tint}{\fibdef\ v\ 1 \wedge v = \fib\ 1}}$ where
the first conjunct comes from the (reflection-strengthened)
output refinement of \fib~\S~\ref{sec:refinementreflection:overview} and
the second comes from rule~\rtexact.
%
%%Finally, rule \rtfix is used to type the intermediate
%%$\texttt{fix}$ expressions that appear, not in the
%%surface language but as intermediate terms in the
%%operational semantics.

\mypara{Well-formedness}
A judgment \iswellformed{\env}{\typ} states that
the refinement type $\typ$ is well-formed in
the environment $\env$.
%
Following chapter~\ref{chapter:refinedhaskell}, the type $\typ$ is well-formed if all
the refinements in $\typ$ are $\tbool$-typed,
provably terminating expressions in $\env$.
%
%%\mypara{Termination}
%%%
%%Under arbitrary beta-reduction semantics
%%(which includes lazy evaluation), soundness
%%of refinement type checking requires checking
%%termination, for two reasons:
%%%
%%(1)~to ensure that refinements cannot diverge, and
%%(2)~to account for the environment during subtyping~\citep{Vazou14}.
%%%
%%We use \tlabel to mark provably terminating
%%computations, and extend the rules to use
%%refinements to ensure that if
%%${\ahastype{\env}{e}{\tref{v}{\btyp^\tlabel}{r}}}$,
%%then $e$ terminates~\citep{Vazou14}.
%%%

\mypara{Subtyping}
A judgment \issubtype{\env}{\typ_1}{\typ_2} states
that the type $\typ_1$ is a subtype of %the type
$\typ_2$ in the environment $\env$.
%
Informally, $\typ_1$ is a subtype of $\typ_2$ if
the refinement of $\typ_1$ \emph{implies}
the refinement of $\typ_2$
under the assumptions described by $\env$.
%
Subtyping of basic types reduces to implication checking.

\mypara{Verification Conditions}
The implication or \emph{verification condition} (VC)
${\vcond{\env}{\pred}}$
is \emph{valid} only if the set of values
described by $\env$, is subsumed by
the set of values described by $\pred$.
%
$\env$ is embedded into logic by conjoining
(the embeddings of) the refinements of
provably terminating binders (Chapter~\ref{chapter:refinedhaskell}):
%
\begin{align*}
\embed{\env} \defeq & \bigwedge_{x \in \env} \embed{\env, x} \\
\intertext{where we embed each binder as}
\embed{\env, x} \defeq & \begin{cases}
                           \pred  & \text{if } \env(x)=\tref{v}{\btyp^{\tlabel}}{e},\
                                    \tologicshort{\env}{e\subst{v}{x}}{\btyp}{\pred}{\embed{\btyp}}{\smtenv}{\axioms} \\
                           \etrue & \text{otherwise}.
                         \end{cases}
\end{align*}

It is important to note that since \smtlan
is carefully restricted to SMT-decidable theories,
VC checking, and thus type checking of \corelan, is decidable.

\subsection{Soundness}

Following \undeclang from chapter~\ref{chapter:refinedhaskell}, in~\citep{vazou16techrep}, we show that
if validity checking respects the axioms of $\beta$-equivalence,
then \corelan is sound.

We define the $\beta$-equivalence axioms on the uninterpreted function
that represent $\lambda$-abstraction (\smtlamname{}{}) and
and application (\smtappname{}{}).
$$
\begin{array}{rcl}
\forall x\ y\ e. \smtlamname{}{}\ x\ e
  & = & \smtlamname{}{}\ y\ (e\subst{x}{y}) \\
\forall x\ e_x\ e. (\smtappname{}{}\ (\smtlamname{}{}\ x\ e)\ e_x)
  & = &  e\subst{x}{e_x}
\end{array}
$$
%
 We prove that when validity checking assumes the $\beta$-equivalence
 axioms, \corelan is sound.

\begin{theorem}{[Soundness of \corelan]}\label{thm:safety}
Assuming the $\beta$-equivalence axioms,
if \hastype{\emptyset}{\prog}{\typ}
       and $\evalsto{\prog}{w}$ then $\hastype{\emptyset}{w}{\typ}$.
\end{theorem}

Theorem~\ref{thm:safety} lets us interpret well typed terminating programs as proofs of
propositions.
%
For example, in \S~\ref{sec:refinementreflection:overview} we verified that
%
$\fibincrname\text{ :: }{\tfun{n}{\tnat}{\ttref{\fib{n} \leq \fib{(n+1)}}}}$.
%
Via soundness of \corelan, we get runtime monotonicity of @fib@.
$$
\forall n. \evalsto{0 \leq n}{\etrue} \Rightarrow \evalsto{\fib{n} \leq \fib{(n+1)}}{\etrue}
$$
% \section{Algorithmic Verification}\label{sec:algorithmic}

\mypara{Approximation of $\beta$-equivalence}
%
Though sound and precise, directly extending the logic with $\beta$-equivalence axioms
would render SMT validity checking undecidable.
%
Next, we discuss an incomplete,
yet decidable, technique that allows the
user to manually instantiate the $\beta$-equivalence axioms
when required for precise typing.


\subsection{Training decision trees with Mean Squared Error (MSE)}


\documentclass{article}

\usepackage{graphicx}
\usepackage{amsmath}
\usepackage[T5]{fontenc}
\usepackage[utf8]{inputenc}

% You can use a package 📦 called booktabs \usepackage{booktabs} for a visually better table.
\usepackage{booktabs}

\graphicspath{ {images/} }

% from begin-latex-in-minutes
\usepackage{listings}
\usepackage{color}

\lstdefinestyle{mystyle}{
keywordstyle=\color{magenta},
backgroundcolor=\color{yellow},
commentstyle=\color{green},
basicstyle=\footnotesize,
}
\lstset{style=mystyle}

\begin{document}

\begin{figure}[h]
    \centering
    \includegraphics[width=0.25\textwidth]{mesh}
    \caption{a nice plot}
    \label{fig:mesh1}
\end{figure}

As you can see in the figure \ref{fig:mesh1}, the
function grows near 0. Also, in the page \pageref{fig:mesh1}
is the same example.

\begin{itemize}
  \item The individual entries are indicated with a black dot, a so-called bullet.
  \item The text in the entries may be of any length.
\end{itemize}

\begin{enumerate}
  \item This is the first entry in our list
  \item The list numbers increase with each entry we add
\end{enumerate}

In physics, the mass-energy equivalence is stated
by the equation $E=mc^2$, discovered in 1905 by Albert Einstein.

The mass-energy equivalence is described by the famous equation
\[ E=mc^2 \]
discovered in 1905 by Albert Einstein.
In natural units ($c = 1$), the formula expresses the identity
\begin{equation}
E=m
\end{equation}

Subscripts in math mode are written as $a_b$ and superscripts are written as $a^b$. These can be combined an nested to write expressions such as

\[ T^{i_1 i_2 \dots i_p}_{j_1 j_2 \dots j_q} = T(x^{i_1},\dots,x^{i_p},e_{j_1},\dots,e_{j_q}) \]

We write integrals using $\int$ and fractions using $\frac{a}{b}$. Limits are placed on integrals using superscripts and subscripts:

\[ \int_0^1 \frac{dx}{e^x} =  \frac{e-1}{e} \]

Lower case Greek letters are written as $\omega$ $\delta$ etc. while upper case Greek letters are written as $\Omega$ $\Delta$.

Mathematical operators are prefixed with a backslash as $\sin(\beta)$, $\cos(\alpha)$, $\log(x)$ etc.

\paragraph{dsfsfsdfds}
This is a paragraph. Hi let me introduce myself Hi let me introduce myself Hi let me introduce myself Hi let me introduce myself Hi let me introduce myself Hi let me introduce myself

\section{zczxzzcxzx}
We begin a section with section and a paragraph with paragraph. You can also add subsection with subsection and subparagraph with subparagraph

\subsection{zczxcxzcc}
We begin a section with section and a paragraph with paragraph. You can also add subsection with subsection and subparagraph with subparagraph
xcvcxvcxv

Hi let me introduce myself\footnote{\label{myfootnote}Hello footnote}.
... (later on)

I'm referring to myself \ref{myfootnote}.

\begin{table}[h!]
  \centering
  \caption{Caption for the table.}
  \label{tab:table1}

  \begin{tabular}{|l|c|||r|}
    \hline
    1 sadfsd sdfsf sfds & 2 sdfds dsfd sdf sdf & asdasd asd asdaad adaa 3\\
    \hline
    a & b xcv dfd dsf sdfsd & ada adsdc\\
    \hline
  \end{tabular}

\end{table}

\paragraph{dsfsfsdfds}
This is a paragraph. Hi let me introduce myself Hi let me introduce myself Hi let me introduce myself Hi let me introduce myself Hi let me introduce myself Hi let me introduce myself

\paragraph{dsfsfsdfds}
This is a paragraph. Hi let me introduce myself Hi let me introduce myself Hi let me introduce myself Hi let me introduce myself Hi let me introduce myself Hi let me introduce myself

\begin{figure}[h]
  % \includegraphics[width=\linewidth]{mesh.png}
  \includegraphics[width=0.25\linewidth]{mesh.png}
  \caption{What is it about?}
  \label{fig:whateverlabel}
\end{figure}

\begin{verbatim}
  #include <iostream>

  int main()
  {
    std::cout << "hello world!\n";
    return 0;
  }
\end{verbatim}

\begin{lstlisting}[language=Python]

  print "Hello World!"

\end{lstlisting}

Lorem ipsum dolor sit amet \lstinline{print "Hello World"} , consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

\input{part2}

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{amsmath,amssymb,amsthm}

\theoremstyle{plain}
\newtheorem{result}{Result}[section]

\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]

\newcommand{\GF}{\operatorname{GF}}

\title{DammSum: efficient mnemonic seeds from quasigroup checksums}
\author{Cypher Stack \\ for Slaz Labs}
\date{\today}

\begin{document}

\maketitle

\begin{abstract}
This technical note describes DammSum, a method for producing digital asset mnemonic seed checksums that are robust against all single substitutions and transpositions.
The method uses an optimization of the Damm algorithm to produce and validate checksums efficiently, in particular without requiring elementwise construction of a required quasigroup and its associated Cayley table.
\end{abstract}

This document describes technical information relevant to mnemonic seed generation in digital assets.
It has not been independently reviewed, and may contain errors.
The author asserts no warranty and disclaims liability for its use.
The author further expresses no endorsement of Slaz Labs or its associated entities.

\section{Introduction}

Many digital asset projects and protocols support the use of mnemonic seeds to generate keys in common client applications.
At its core, a mnemonic seed is simply a mapping between a given list of words and a corresponding bit string; depending on the method used, this bit string is used to produce keys, either directly or using a pseudorandom function like a hash function or key derivation function.

Mnemonic seeds are useful precisely because they are designed for use by humans, as they can be easily written down, spoken by voice, typed into wallet software, or otherwise securely stored using physical media.
However, this poses challenges:
\begin{itemize}
	\item What happens if the user communicates the seed over a noisy channel, and one or more words are substituted with others?
	\item What happens if the user reads the seed incorrectly, and one or more words are transposed?
	\item What happens if the user stores the seed incorrectly, and it is truncated when later read?
\end{itemize}

One mitigation to these and related challenges is to introduce a checksum to the seed phrase.
When generating a seed, client software first uses suitable randomness to produce the bitstring intended for use in key generation.
It then maps the bitstring to the mnemonic seed using a given word list.
Finally, it produces the checksum, which (depending on the method used) is typically prepended or appended to the seed.
When later communicating or using the seed, client software ensures that the checksum is valid for the given seed.

When considering designs for checksums, we consider two important concepts from coding theory relating to errors: detection and correction.
A design that detects errors is intended to alert the user that some particular type of transmission error occurred, and that the seed is invalid.
A design that corrects errors can do more, identifying the (ideally unique) valid seed to which the errors were applied.
Different constructions provide varying degrees of error detection and correction.

In this technical note, we describe a checksum design called DammSum that has the following properties:
\begin{itemize}
	\item The checksum design is compatible with the common Electrum word set used in many digital asset projects.
	\item The checksum is a single word, regardless of the length of the seed.
	\item All erasure errors are detected.
	\item All single substitution errors are detected.
	\item All single adjacent transpositions, including transposition of the checksum itself, are detected.
	\item Computation and verification of a checksum is efficient, with no lookup tables or complex computations required.
\end{itemize}

While error detection is useful, this design does not permit unique correction of substitution, transposition, or erasure errors.

\section{Mathematical background}

In a dissertation \cite{damm_thesis} and related papers \cite{damm2000,damm2007}, Damm notes the applicability of group structures to check digits, in particular observing that while the use of a group operation on antisymmetric permutation compositions can always detect single substitutions, it is limited in detection of adjacent transpositions based on the order of the group.
It is shown that replacement of the general group structure with at least a weakly totally antisymmetric quasigroup removes this limitation, such that a checksum construction based on this structure reliably and provably detects single substitutions and adjacent transpositions.

This observation then reduces the problem of checksum design to construction of weakly totally antisymmetric quasigroups of the required order, as well as optimizations for evaluation of the quasigroup operation on each digit of the seed.

For completeness, we define quasigroups, the weakly totally antisymmetric and totally antisymmetric properties, and the Damm algorithm, but refer the reader to \cite{damm_thesis,damm2000,damm2007} for a more complete treatment.

\subsection{Quasigroups and asymmetry}

\begin{definition}
	A \textit{quasigroup} $(G,\star)$ is a set $G \neq \emptyset$ closed under a binary operation $\star$ such that for all $a,b \in G$ there exist unique $x,y \in G$ such that $a \star x = b$ and $y \star a = b$.
\end{definition}

For the Damm algorithm, we will consider finite quasigroups, where the definition implies the usual cancellation laws.

\begin{definition}
	A quasigroup $(G,\star)$ is \textit{weakly totally antisymmetric} if for all $c,x,y \in G$, if $(c \star x) \star y = (c \star y) \star x$, then $x = y$.
\end{definition}

\begin{definition}
	A quasigroup $(G,\star)$ is \textit{totally antisymmetric} if it is weakly totally antisymmetric and $x \star y = y \star x$ implies $x = y$ for all $x,y \in G$.
\end{definition}

The paper \cite{damm2007} proves that there exist totally antisymmetric quasigroups of order $n$ for all $n \not\in \{2,6\}$, and that there exists a weakly totally antisymmetric quasigroup of order $n$ if and only if there exists a totally antisymmetric quasigroup of order $n$.

For the specific DammSum construction we describe later, the following additional results from \cite{damm2007} will be useful.

\begin{result}
	\label{result:gf_is_ta}
	Let $k > 1$ be an integer, and let $G = \GF(2^k)$ be the Galois field with $2^k$ elements.
	Let $a \in G$ such that $a \not\in \{0,1\}$, and define the binary operation $\star$ such that $x \star y = ax + y$ for all $x,y \in G$.
	Then $(G,\star)$ is a totally antisymmetric quasigroup.
\end{result}

\begin{result}
	\label{result:permute_wta}
	Let $(G,\star)$ be a totally antisymmetric quasigroup, and let $\beta: G \to G$ be a permutation of the elements of $G$.
	Define a binary operation $\star'$ on $G$ such that for all $x,y \in G$, we have $x \star' y = x \star \beta(y)$.
	Then $(G,\star')$ is a weakly totally antisymmetric quasigroup.
\end{result}

We now prove that a particular construction over a Galois field is a weakly totally antisymmetric quasigroup.

\begin{result}
	\label{result:wta}
	Let $k > 1$ be an integer, and let $G = \GF(2^k)$ be the Galois field with $2^k$ elements.
	Define a binary operation $\star'$ on $G$ such that for $x,y \in G$ we have $x \star' y = 2 \cdot (x + y)$.
	Then $(G,\star')$ is a weakly totally antisymmetric quasigroup.
\end{result}

\begin{proof}
	Define a binary operation $\star$ on $G = \GF(2^k)$ such that $x \star y = 2 \cdot x + y$ for all $x,y \in G$; then by Result \ref{result:gf_is_ta}, $(G,\star)$ is a totally antisymmetric quasigroup.

	Let $\beta: G \to G$ be a permutation on $G$ defined such that $\beta(x) = 2 \cdot x$ for all $x \in G$.
	Then for all $x,y \in G$ we have
	\begin{alignat*}{1}
		x \star' y &= 2 \cdot (x + y) \\
		&= 2 \cdot x + 2 \cdot y \\
		&= 2 \cdot x + \beta(y) \\
		&= x \star \beta(y)
	\end{alignat*}
	Result \ref{result:permute_wta} implies that $(G,\star')$ is weakly totally antisymmetric.
\end{proof}
Note that by construction, $x \star' x = 0$ for all $x \in G$.

\subsection{Damm algorithm}

We now define the Damm algorithm, which produces a checksum that provably detects single substitutions and adjacent transpositions.
The algorithm requires a finite weakly totally antisymmetric quasigroup $(G,\star)$ of order $n$, where for notational convenience we denote $G = \{0,1,\ldots,n-1\}$.
For optimization purposes, it further requires that $x \star x = 0$ for all $x \in G$ (the element $0$ is arbitrary and denoted as such for convenience); this is equivalent to asserting that the diagonal of the Cayley table for $G$ contain only this element.

Let $w = d_m | d_{m-1} | \cdots | d_1$ be an $m$-digit word formed by concatenating the digits $\{d_i\}_{i=1}^m$, where $d_i \in G$ for all $i \in [1,m]$ and $m > 0$.
Define the checksum of $w$ to be the digit $d_0$ such that the equation
$$(\cdots((d_m \star d_{m-1}) \star d_{m-2}) \star \cdots \star d_1) \star d_0 = 0$$
holds.
Observe that because we require $x \star x = 0$ for all $x \in G$, we may simplify the above equation by defining
$$d_0 = (\cdots((d_m \star d_{m-1}) \star d_{m-2}) \star \cdots \star d_1)$$
and using the former equation as verification of the checksum $d_0$.

Because $(G,\star)$ is a weakly totally antisymmetric quasigroup, any single substitution or transposition is detected.

\section{DammSum}

We now describe the construction of DammSum, a method for efficiently producing Damm-based checksums for digital asset mnemonic seed phrases.

Let $k = 11$, so $2^k = 2^{11} = 2048$.
Let $m = 12$.
Let $G = \GF(2^k)$, and define the binary operation $\star'$ on $G$ such that $x \star' y = 2 \cdot (x + y)$ for all $x,y \in G$.

Generation of a DammSum seed proceeds as follows:
\begin{enumerate}
	\item For $i \in [1,m]$, sample a digit $d_i \in G$ uniformly at random, and let $w = d_m | d_{m-1} | \cdots | d_1$.
	\item Compute $d_0 = (\cdots((d_m \star' d_{m-1}) \star' d_{m-2}) \star' \cdots \star' d_1)$.
	\item For each $i \in [0,m]$, let $D_i$ be the English word from the Electrum word list corresponding to $d_i$.
	\item Output the seed $D_m | D_{m-1} | \cdots | D_1 | D_0$.
\end{enumerate}

To verify a DammSum seed has the correct checksum:
\begin{enumerate}
	\item For each $i \in [0,m]$, let $d_i$ be the element of $\GF(2^k)$ corresponding to $D_i$.
	\item If the equation
	$$(\cdots((d_m \star' d_{m-1}) \star' d_{m-2}) \star' \cdots \star' d_1) \star' d_0 = 0$$
	holds, then verification succeeds; otherwise, it fails.
\end{enumerate}

The above construction meets the requirements of the Damm algorithm by Result \ref{result:wta}.
Further, a seed generated in this manner has $\log_2\left((2^{11})^{12}\right) = 132$ bits of entropy.

\section{Implementation}

We can implement DammSum efficiently, and note here how to do so.

Consider a representation of $\GF(2^k)$ as the set of binary-valued polynomials of degree at most $k-1$, reduced by an arbitrary monic irreducible binary-valued polynomial $f$ of degree exactly $k$.
While all choices of $f$ are valid, for efficiency we seek a polynomial of low weight.
For our purpose, we use the table in \cite{hp}, which lists $f(x) = x^{11} + x^2 + 1$.

In order to compute the checksum for a seed, we must compute quantities of the form $2 \cdot (x + y)$ in $\GF(2^k)$.
The sum $x + y$ is simply the bitwise \texttt{XOR} of the binary representations of $x$ and $y$, which is trivial to compute.

In order to compute the multiplication of the sum $x + y$ by $2$, it suffices to perform a bitwise left shift of $x + y$ and, if the result has the $k$ bit set, \texttt{XOR} this result with the binary representation of $f(2) = 2053$.

We then iterate this process over each digit in the seed to produce the checksum.

\section{Observations}

We note that while computation and verification of DammSum checksums is extremely efficient and can detect single substitution and transposition errors, it cannot uniquely correct them in all cases.

In order to provide robust and flexible error correction, a design based on constructions like Bose-Chaudhuri-Hocquenghem codes \cite{hocquenghem,bose} is recommended; however, such constructions are generally more complex and marginally less efficient.

\bibliographystyle{plain}
\bibliography{main}

\end{document}


\section{Factorial designs: Round 2}

Designs with more than one independent variable refer to designs where
the experimenter manipulates at least two independent variables.
Consider the light-switch example from the previous chapter. Imagine you
are trying to figure out which of two light switches turns on a light.
The dependent variable is the light (we measure whether it is on or
off). The first independent variable is light switch \#1, and it has two
levels, up or down. The second independent variable is light switch \#2,
and it also has two levels, up or down. When there are two independent
variables, each with two levels, there are four total conditions that
can be tested. We can describe these four conditions in a 2x2 table.

\begin{longtable}[]{@{}ccc@{}}
\toprule
& Switch 1 Up & Switch 1 Down\tabularnewline
\midrule
\endhead
Switch 2 Up & Light ? & Light ?\tabularnewline
Switch 2 Down & Light ? & Light ?\tabularnewline
\bottomrule
\end{longtable}

This kind of design has a special property that makes it a factorial
design. That is, the levels of each independent variable are each
manipulated across the levels of the other indpendent variable. In other
words, we manipulate whether switch \#1 is up or down when switch \#2 is
up, and when switch numebr \#2 is down. Another term for this property
of factorial designs is ``fully-crossed''.

It is possible to conduct experiments with more than independent
variable that are not fully-crossed, or factorial designs. This would
mean that each of the levels of one independent variable are not
necessarilly manipulated for each of the levels of the other independent
variables. These kinds of designs are sometimes called unbalanced
designs, and they are not as common as fully-factorial designs. An
example, of an unbalanced design would be the following design with only
3 conditions:

\begin{longtable}[]{@{}ccc@{}}
\toprule
& Switch 1 Up & Switch 1 Down\tabularnewline
\midrule
\endhead
Switch 2 Up & Light ? & Light ?\tabularnewline
Switch 2 Down & Light ? & NOT MEASURED\tabularnewline
\bottomrule
\end{longtable}

Factorial designs are often described using notation such as AXB, where
A= the number of levels for the first independent variable, and B = the
number of levels for the second independent variable. The fully-crossed
version of the 2-light switch experiment would be called a 2x2 factorial
design. This notation is convenient because by multiplying the numbers
in the equation we can find the number of conditions in the design. For
example 2x2 = 4 conditions.

More complicated factorial designs have more indepdent variables and
more levels. We use the same notation describe these designs. The number
for each variable represents the number of levels for that variable, and
the number of numbers in the equation represents the number of
variables. So, a 2x2x2 design has three independent variables, and each
one has 2 levels, for a total of 2x2x2=6 conditions. A 3x3 design has
two independent variables, each with three levels, for a total of 9
conditions. Designs can get very complicated, such as a 5x3x6x2x7
experiment, with five independent variables, each with differing numbers
of levels, for a total of 1260 conditions. If you are considering a
complicated design like that one, you should consider how to simplify
it.

\subsection{2x2 Factorial designs}\label{x2-factorial-designs}

For simplicity, we will focus mainly on 2x2 factorial designs. As with
simple designs with only one independent variable, factorial designs
have the same basic empirical question. Did the manipulation cause a
change in the measurement? However, 2x2 designs have more than one
manipulation, so there is more than one way that a change in measurement
can be observed. So, we end up asking the basic empirical question more
than once.

More specifically, the analysis of factorial designs are split into two
parts: main effects and interactions. Main effects are occur when the
levels of one independent variable cause a change in the dependent
variable. In a 2x2 design, there are two independent variables, so there
are two possible main effects: the main effect of independent variable
1, and the main effect of independent variable 2. An interaction occurs
when the effect of one independent variable \emph{depends} on the levels
of the other independent variable. My experience in teaching the concept
of main effects and interactions is that they are confusing. So, I
expect that these definitions will not be very helpful, and although
they are clear and precise, they only become helpful as definitions
after you understand the concepts\ldots{}so they are not useful for
explaining the concepts. To explain the concepts we will go through
several different kinds of examples.

To briefly add to the confusion, or perhaps to illustrate why these two
concepts can be confusing, we will look at the eight possible outcomes
that could occur in a 2x2 factorial experiment.

\begin{longtable}[]{@{}cccc@{}}
\toprule
Possible outcome & IV1 main effect & IV2 main effect &
Interaction\tabularnewline
\midrule
\endhead
1 & yes & yes & yes\tabularnewline
2 & yes & no & yes\tabularnewline
3 & no & yes & yes\tabularnewline
4 & no & no & yes\tabularnewline
5 & yes & yes & no\tabularnewline
6 & yes & no & no\tabularnewline
7 & no & yes & no\tabularnewline
8 & no & no & no\tabularnewline
\bottomrule
\end{longtable}

In the table, a yes means that there was statistically significant
difference for one of the main effects or interaction, and a no means
that there was not a statisically significant difference. As you can
see, just by adding one more independent variable, the number of
possible outcomes quickly become more complicated. When you conduct a
2x2 design, the task for analysis is to determine which of the 8
possibilites occured, and then explain the patterns for each of the
effects that occurred. That's a lot of explaining to do.

\subsection{Main effects}\label{main-effects}

Main effects occur when the levels of an independent variable cause
change in the measurement or dependent variable. There is one possible
main effect for each independent variable in the design. When we find
that independent variable did cause change, then we say there was a main
effect. When we find that the independent variable did not cause change,
then we say there was no main effect.

The simplest way to understand a main effect is to pretend that the
other independent variables do not exist. If you do this, then you
simply have a single-factor design, and you are asking whether that
single factor caused change in the measurement. For a 2x2 experiment,
you do this twice, once for each independent variable.

Let's consider a silly example to illustrate an important property of
main effects. In this experiment the dependent variable will be height
in inches. The independent variables will be shoes and hats. The shoes
independent variable will have two levels: wearing shoes vs.~no shoes.
The hats independent variable will have two levels: wearing a hat
vs.~not wearing a hat. The experiment will provide the shoes and hats.
The shoes add 1 inch to a person's height, and the hats add 6 inches to
a person's height. Further imagine that we conduct a within-subjects
design, so we measure each person's height in each of the fours
conditions. Before we look at some example data, the findings from this
experiment should be pretty obvious. People will be 1 inch taller when
they wear shoes, and 6 inches taller when they where a hat. We see this
in the example data from 10 subjects presented below:

\begin{longtable}[]{@{}rrrr@{}}
\toprule
NoShoes\_NoHat & Shoes\_NoHat & NoShoes\_Hat & Shoes\_Hat\tabularnewline
\midrule
\endhead
56 & 57 & 62 & 63\tabularnewline
57 & 58 & 63 & 64\tabularnewline
58 & 59 & 64 & 65\tabularnewline
57 & 58 & 63 & 64\tabularnewline
57 & 58 & 63 & 64\tabularnewline
57 & 58 & 63 & 64\tabularnewline
58 & 59 & 64 & 65\tabularnewline
57 & 58 & 63 & 64\tabularnewline
57 & 58 & 63 & 64\tabularnewline
57 & 58 & 63 & 64\tabularnewline
\bottomrule
\end{longtable}

The mean heights in each condition are:

\begin{longtable}[]{@{}lr@{}}
\toprule
NoShoes\_NoHat & 57.1\tabularnewline
Shoes\_NoHat & 58.1\tabularnewline
NoShoes\_Hat & 63.1\tabularnewline
Shoes\_Hat & 64.1\tabularnewline
\bottomrule
\end{longtable}

To find the main effect of the shoes manipulation we want to find the
mean height in the no shoes condition, and compare it to the mean height
of the shoes condition. To do this, we \emph{collapse}, or average over
the observations in the hat conditions. For example, looking only at the
no shoes vs.~shoes conditions we see the following averages for each
subject.

\begin{longtable}[]{@{}rr@{}}
\toprule
NoShoes & Shoes\tabularnewline
\midrule
\endhead
59 & 60\tabularnewline
60 & 61\tabularnewline
61 & 62\tabularnewline
60 & 61\tabularnewline
60 & 61\tabularnewline
60 & 61\tabularnewline
61 & 62\tabularnewline
60 & 61\tabularnewline
60 & 61\tabularnewline
60 & 61\tabularnewline
\bottomrule
\end{longtable}

The group means are:

\begin{longtable}[]{@{}lr@{}}
\toprule
NoShoes & 60.1\tabularnewline
Shoes & 61.1\tabularnewline
\bottomrule
\end{longtable}

As expected, we see that the average height is 1 inch taller when
subjects wear shoes vs.~do not wear shoes. So, the main effect of
wearing shoes is to add 1 inch to a person's height.

We can do the very same thing to find the main effect of hats. Except in
this case, we find the average heights in the no hat vs.~hat conditions
by averaging over the shoe variable.

\begin{longtable}[]{@{}rr@{}}
\toprule
NoHat & Hat\tabularnewline
\midrule
\endhead
56.5 & 62.5\tabularnewline
57.5 & 63.5\tabularnewline
58.5 & 64.5\tabularnewline
57.5 & 63.5\tabularnewline
57.5 & 63.5\tabularnewline
57.5 & 63.5\tabularnewline
58.5 & 64.5\tabularnewline
57.5 & 63.5\tabularnewline
57.5 & 63.5\tabularnewline
57.5 & 63.5\tabularnewline
\bottomrule
\end{longtable}

The group means are:

\begin{longtable}[]{@{}lr@{}}
\toprule
NoHat & 57.6\tabularnewline
Hat & 63.6\tabularnewline
\bottomrule
\end{longtable}

As expected, we the average height is 6 inches taller when the subjects
wear a hat vs.~do not wear a hat. So, the main effect of wearing hats is
to add 1 inch to a person's height.

Instead of using tables to show the data, let's use some bar graphs.
First, we will plot the average heights in all four conditions.

\includegraphics{Factorial_files/figure-latex/unnamed-chunk-7-1}

Some questions to ask yourself are 1) can you identify the main effect
of wearing shoes in the figure, and 2) can you identify the main effet
of wearing hats in the figure. Both of these main effects can be seen in
the figure, but they aren't fully clear. You have to do some visual
averaging.

Perhaps the most clear is the main effect of wearing a hat. The red bars
show the conditions where people wear hats, and the green bars show the
conditions where people do not wear hats. For both levels of the wearing
shoes variable, the red bars are higher than the green bars. That is
easy enough to see. More specifically, in both cases, wearing a hat adds
exactly 6 inches to the height, no more no less.

Less clear is the main effect of wearing shoes. This is less clear
because the effect is smaller so it is harder to see. How to find it?
You can look at the red bars first and see that the red bar for
no\_shoes is slightly smaller than the red bar for shoes. The same is
true for the green bars. The green bar for no\_shoes is slightly smaller
than the green bar for shoes.

\begin{figure}
\includegraphics[width=.5\linewidth]{Factorial_files/figure-latex/unnamed-chunk-8-1} \end{figure}

\begin{figure}
\includegraphics[width=.5\linewidth]{Factorial_files/figure-latex/unnamed-chunk-8-2} \end{figure}

Data from 2x2 designs is often present in graphs like the one above. An
advantage of these graphs is that they display means in all four
conditions of the design. However, they do not clearly show the two main
effects. Someone looking at this graph alone would have to guesstimate
the main effects. Or, in addition to the main effects, a researcher
could present two more graphs, one for each main effect (however, in
practice this is not commonly done because it takes up space in a
journal article, and with practice it becomes second nature to ``see''
the presence or absence of main effects in graphs showing all of the
conditions). If we made a separate graph for the main effect of shoes we
should see a difference of 1 inch between conditions. Similarly, if we
made a separate graph for the main effect of hats then we should see a
difference of 6 between conditions. Examples of both of those graphs
appear in the margin.

Why have we been talking about shoes and hats? These independent
variables are good examples of variables that are truly independent from
one another. Neither one influences the other. For example, shoes with a
1 inch sole will always add 1 inch to a person's height. This will be
true no matter whether they wear a hat or not, and no matter how tall
the hat is. In other words, the effect of wearing a shoe does not depend
on wearing a hat. More formally, this means that the shoe and hat
independent variables do not interact. It would be very strange if they
did interact. It would mean that the effect of wearing a shoe on height
would depend on wearing a hat. This does not happen in our universe. But
in some other imaginary universe, it could mean, for example, that
wearing a shoe adds 1 to your height when you do not wear a hat, but
adds more than 1 inch (or less than 1 inch) when you do wear a hat. This
thought experiment will be our entry point into discussing interactions.
A take-home message before we begin is that some independent variables
(like shoes and hats) do not interact; however, there are many other
independent variables that do.

\subsection{Interactions}\label{interactions}

Interactions occur when the effect of an independent variable depends on
the levels of the other independent variable. As we discussed above,
some independent variables are independent from one another and will not
produce interactions. However, other combinations of independent
variables are not independent from one another and they produce
interactions. Remember, independent variables are always manipulated
independently from the measured variable (see margin note), but they are
not necessarilly independent from each other.

\begin{marginfigure}
These ideas can be confusing if you think that the word ``independent''
refers to the relationship between independent variables. However, the
term ``independent variable'' refers to the relationship between the
manipulated variable and the measured variable. Remember, ``independent
variables'' are manipulated independently from the measured variable.
Specifically, the levels of any independent variable do not change
because we take measurements. Instead, the experimenter changes the
levels of the independent variable and then observes possible changes in
the measures.
\end{marginfigure}

There are many simple examples of two independent variables being
dependent on one another to produce an outcome. Consider driving a car.
The dependent variable (outcome that is measured) could be how far the
car can drive in 1 minute. Independent variable 1 could be gas (has gas
vs.~no gas). Independent variable 2 could be keys (has keys vs.~no
keys). This is a 2x2 design, with four conditions.

\begin{longtable}[]{@{}ccc@{}}
\toprule
& Gas & No Gas\tabularnewline
\midrule
\endhead
Keys & can drive & x\tabularnewline
No Keys & x & x\tabularnewline
\bottomrule
\end{longtable}

Importantly, the effect of the gas variable on driving depends on the
levels of having a key. Or, to state it in reverse, the effect of the
key variable on driving depends on the levesl of the gas variable.
Finally, in plain english. You need the keys and gas to drive.
Otherwise, there is no driving.

\subsection{What makes a people
hangry?}\label{what-makes-a-people-hangry}

To continue with more examples, let's consider an imaginary experiment
examining what makes people hangry. You may have been hangry before.
It's when you become highly irritated and angry because you are very
hungry\ldots{}hangry. I will propose an experiment to measure conditions
that are required to produce hangriness. The pretend experiment will
measure hangriness (we ask people how hangry they are on a scale from
1-10, with 10 being most hangry, and 0 being not hangry at all). The
first independent variable will be time since last meal (1 hour vs.~5
hours), and the second independent variable will be how tired someone is
(not tired vs very tired). I imagine the data could look something the
following bar graph.

\includegraphics{Factorial_files/figure-latex/unnamed-chunk-10-1}

The graph shows clear evidence of two main effects,
\emph{and an interaction}. There is a main effect of time since last
meal. Both the bars in the 1 hour conditions have smaller hanger ratings
than both of the bars in the 5 hour conditions. There is a main effect
of being tired. Both of the bars in the ``not tired'' conditions are
smaller than than both of the bars in the ``tired'' conditions. What
about the interaction?

Remember, an interaction occurs when the effect of one independent
variable depends on the level of the other independent variable. We can
look at this two ways, and either way shows the presence of the very
same interaction. First, does the effect of being tired depend on the
levels of the time since last meal? Yes. Look first at the effect of
being tired only for the ``1 hour condition''. We see the red bar
(tired) is 1 unit lower than the green bar (not\_tired). So, there is an
effect of 1 unit of being tired in the 1 hour condition. Next, look at
the effect of being tired only for the ``5 hour'' condition. We see the
red bar (tired) is 3 units lower than the green bar (not\_tired). So,
there is an effect of 3 units for being tired in the 5 hour condition.
Clearly, the size of the effect for being tired depends on the levels of
the time since last meal variable. We call this an interaction.

The second way of looking at the interaction is to start by looking at
the other variable. For example, does the effect of time since last meal
depend on the levels of the tired variable? The answer again is yes.
Look first at the effect of time since last meal only for the red bars
in the ``not tired'' condition. The red bar in the 1 hour condition is 1
unit smaller than the red bar in the 5 hour condition. Next, look at the
effect of time since last meal only for the green bars in the ``tired''
condition. The green bar in the 1 hour condition is 3 units smaller than
the green bar in the 5 hour condition. Again, the size of the effect of
time since last meal depends on the levels of the tired variable.No
matter which way you look at the interaction, we get the same numbers
for the size of the interaction effect, which is 2 units (a difference
between 3 and 1 = 2). The interaction suggests that something special
happens when people are tired and haven't eaten in 5 hours. In this
condition, they can become very hangry. Whereas, in the other
conditions, there are only small increases in being hangry.

\subsection{Identifying main effects and
interactions}\label{identifying-main-effects-and-interactions}

Research findings are often presented to readers using graphs or tables.
For example, the very same pattern of data can be displayed in a bar
graph, line graph, or table of means. These different formats can make
the data look different, even though the pattern in the data is the
same. An important skill to develop is the ability to identify the
patterns in the data, regardless of the format they are presented in.
Some examples of bar and line graphs are presented in the margin, and
two example tables are presented below. Each format displays the same
pattern of data.

\begin{marginfigure}
\includegraphics{Factorial_files/figure-latex/unnamed-chunk-11-1} \end{marginfigure}
\begin{marginfigure}
\includegraphics{Factorial_files/figure-latex/unnamed-chunk-11-2} \end{marginfigure}

\begin{longtable}[]{@{}lll@{}}
\toprule
DV I & V1 I & V2\tabularnewline
\midrule
\endhead
10 & Level\_1 & Level\_1\tabularnewline
12 & Level\_2 & Level\_1\tabularnewline
17 & Level\_1 & Level\_2\tabularnewline
13 & Level\_2 & Level\_2\tabularnewline
\bottomrule
\end{longtable}

\begin{verbatim}
##          df$IV2
## df$IV1    Level_1 Level_2
##   Level_1      10      17
##   Level_2      12      13
\end{verbatim}

After you become comfortable with interpreting data in these different
formats, you should be able to quickly identify the pattern of main
effects and interactions. For example, you would be able to notice that
all of these graphs and tables show evidence for two main effects and
one interaction.

As an exercise toward this goal, we will first take a closer look at
extracting main effects and interactions from tables. This exercise will
how the condition means are used to calculate the main effects and
interactions. Consider the table of condition means below.

\begin{longtable}[]{@{}cccc@{}}
\toprule
& & IV1 &\tabularnewline
\midrule
\endhead
& & A & B\tabularnewline
IV2 & 1 & 4 & 5\tabularnewline
& 2 & 3 & 8\tabularnewline
\bottomrule
\end{longtable}

\subsection{Main effects}\label{main-effects-1}

Main effects are the differences between the means of single independent
variable. Notice, this table only shows the condition means for each
level of all independent variables. So, the means for each IV must be
calculated. The main effect for IV1 is the comparison between level A
and level B, which involves calculating the two column means. The mean
for IV1 Level A is (4+3)/2 = 3.5. The mean for IV1 Level B is (5+8)/2 =
6.5. So the main effect is 3 (6.5 - 3.5). The main effect for IV2 is the
comparison between level 1 and level 2, which involves calculating the
two row means. The mean for IV2 Level 1 is (4+5)/2 = 4.5. The mean for
IV2 Level 2 is (3+8)/2 = 5.5. So the main effect is 1 (5.5 - 4.5). The
process of computing the average for each level of a single independent
variable, always involves collapsing, or averaging over, all of the
other conditions from other variables that also occured in that
condition

\subsection{Interactions}\label{interactions-1}

Interactions ask whether the effect of one independent variable depends
on the levels of the other independent variables. This question is
answered by computing difference scores between the condition means. For
example, we look the effect of IV1 (A vs.~B) for both levels of of IV2.
Focus first on the condition means in the first row for IV2 level 1. We
see that A=4 and B=5, so the effect IV1 here was 5-4 = 1. Next, look at
the condition in the second row for IV2 level 2. We see that A=3 and
B=8, so the effect of IV1 here was 8-3 = 5. We have just calculated two
differences (5-4=1, and 8-3=5). These difference scores show that the
size of the IV1 effect was different across the levels of IV2. To
calculate the interaction effect we simply find the difference between
the difference scores, 5-1=4. \emph{In general, if the difference
between the difference scores is different, then there is an interaction
effect.}

\subsection{Example bar graphs}\label{example-bar-graphs}

\includegraphics{Factorial_files/figure-latex/unnamed-chunk-13-1}

The IV1 graph shows a main effect only for IV1 (both red and green bars
are lower for level 1 than level 2). The IV1\&IV2 graphs shows main
effects for both variables. The two bars on the left are both lower than
the two on the right, and the red bars are both lower than the green
bars. The IV1xIV2 graph shows an example of a classic cross-over
interaction. Here, there are no main effects, just an interaction. There
is a difference of 2 between the green and red bar for Level 1 of IV1,
and a difference of -2 for Level 2 of IV1. That makes the differences
between the differences = 4. Why are their no main effects? Well the
average of the red bars would equal the average of the green bars, so
there is no main effect for IV2. And, the average of the red and green
bars for level 1 of IV1 would equal the average of the red and green
bars for level 2 of IV1, so there is no main effect. The bar graph for
IV2 shows only a main effect for IV2, as the red bars are both lower
than the green bars.

\subsection{Example line graphs}\label{example-line-graphs}

You may find that the patterns of main effects and interaction looks
different depending on the visual format of the graph. The exact same
patterns of data plotted up in bar graph format, are plotted as line
graphs for your viewing pleasure. Note that for the IV1 graph, the red
line does not appear because it is hidden behind the green line (the
points for both numbers are identical).

\includegraphics{Factorial_files/figure-latex/unnamed-chunk-14-1}

\subsection{Interpreting main effects and
interactions}\label{interpreting-main-effects-and-interactions}

The presence of an interaction can sometimes change how we interpet main
effects. For example, a really strong interaction can produce the
appearance of a main effect, even though when we look at the data most
people would agree the main effect is not there.

\includegraphics{Factorial_files/figure-latex/unnamed-chunk-15-1}

In the above graph there is clearly an interaction. IV2 has no effect
under level 1 of IV1 (e.g., the red and green bars are the same). IV2
has a large effect under level 2 of IV2 (the red bar is 2 and the green
bar is 9). So, the interaction effect is a total of 7. Are there any
main effects? This is a debatable question. Consider the main effect for
IV1. The mean for level 1 is (2+2)/2 = 2, and the mean for level 2 is
(2+9)/2 = 5.5. There is a difference between the means of 3.5, which is
consistent with a main effect. Consider, the main effect for IV2. The
mean for level 1 is again (2+2)/2 = 2, and the mean for level 2 is again
(2+9)/2 = 5.5. Again, there is a difference between the means of 3.5,
which is consistent with a main effect. What is going on here is that
the process of averagin over conditions that we use to compute main
effects is causing a main effect to appear, even though we don't really
see clear evidence of main effects.

Clear evidence of a main effect typically refers to cases where there is
a consistent additive influence. For example, if there really was a main
effect of IV1, then both red and green bars for level 2 should be
higher, not just one of them. In other words, the effect of IV1 did not
uniformly raise or lower the means across all of the other conditions.
For this reason, the main effects that we observed by performing the
calculation are really just an interaction in disguise.

The next example shows a case where it would be more appropriate to
conclude that the main effects and the interaction were both real.

\includegraphics{Factorial_files/figure-latex/unnamed-chunk-16-1}

Can you spot the interaction right away? The difference between red and
green bars is small for level 1 of IV1, but large for level 2. The
differences between the differences are different, so there is an
interaction. But, we also see clear evidence of two main effects. For
example, both the red and green bars for IV1 level 1 are higher than IV1
Level 2. And, both of the red bars (IV2 level 1) are higher than the
green bars (IV2 level 2).


\chapter{Assignment: Data Preprocessing}
\label{ch:arheo_data_preprocessing}

\newthought{Data preprocessing is crucial in archeology.} The data often contains many diverse labels, some of which are certain and others speculative. Machine learning doesn't handle categorical data with many different labels well, hence we need to preprocess the data.

We will use the \href{https://archaeologydataservice.ac.uk/catalogue/adsdata/arch-1115-2/dissemination/csv/pottery/pottery.csv}{pottery} data from the Antikythera data base. The data contains 13,865 data instances and 36 features describing the pottery collected across the full extent of the Greek island of Antikythera.

Let us look at the VesselPart feature, which describes the part of the ceramic vessel involved and it has 34 values.

\begin{figure*}[h]
    \centering
    \includegraphics[scale=0.35]{box-plot-vesselpart.png}
    \caption{$\;$} % empty caption for proper page setting
\end{figure*}

But looking at the \widget{Box Plot}, we notice there are 4 fairly frequent values, while others are rare. We would like to keep B (body), Ba (base), H (handle), and R (rim), and consider all the others as one group.

\begin{figure*}[h]
    \centering
    \newcommand{\before}{\includegraphics[scale=0.35]{edit-domain-before.png}}
    \newcommand{\after}{\includegraphics[scale=0.35]{edit-domain-during.png}}
    \infinitewidthbox{
    \stackinset{r}{-0.5\linewidth}{t}{0.0\linewidth}{\after}{\before}\hspace{8cm}
    }
\end{figure*}

We will use \widget{Edit Domain} to merge infrequent values together. Select \textit{VesselPart} and press M. This opens a dialogue for merging values. You can use any technique you wish. Here, since we know there are 4 frequent values, we will use the final option, \textit{Group all except 4 most frequent values}. We can also give the name to the new value, but let's leave it a \textit{other} for now.

\newpage

You can see the less frequent values have been remapped. Do not forget to press \textit{Apply} once you have finished editing. Looking at the Box Plot, the values are now much easier to interpret.

\begin{marginfigure}
    \centering
    \includegraphics[scale=0.5]{workflow.png}
\end{marginfigure}

\begin{figure}[h]
    \centering
    \includegraphics[scale=0.35]{box-plot-after.png}
    \caption{$\;$} % empty caption for proper page setting
\end{figure}

\subsection{Assignment}

Preprocess the pottery data using the technique from above and answer the following questions:
\begin{itemize}
    \item Put metadata to meta attributes using Select Columns. Which features did you put there? Why?
    \item Which features had to be preprocessed by merging infrequent values?
    \item Is there another way we could merge values?
    \item Use Box Plot after Edit Domain and
\end{itemize}