% Anisoptera-Zygoptera are switched in the code output box notes on p.18
% Fix bandwith error on density plots

\chapter{$t$ tests and $F$ tests}
\label{ch:t_F_tests}

Aims of this chapter\footnote{Here you work with the script file {\tt t\_F\_tests.R}}:
\begin{compactitem}
	\item Using $t$ tests to look at differences between means
	\item Using $F$ tests to compare the variance of two samples
	\item Using non-parametric tests for differences
\end{compactitem}

In the last chapter, we looked at the genome size and morphology of
species of dragonflies and damselflies (Odonates: Anisoptera and
Zygoptera). Box and whisker plots and density plots both show that the
two groups have rather different genome size and morphology. We can use
$t$ tests to test whether the means of the variables of the two groups
are different and we can use the $F$ test to check whether they have
the same variance.

In this chapter, we will continue to practise building scripts and
creating your own R code, so we will start from an empty script file.
Use this script to store the code you run in this practical session and
add notes to keep a record of what each bit is doing:

\begin{compactitem}[$\quad\star$]
	\item Open R and change to the {\tt Code} directory.
	\item If you have misplaced the data then it is on Blackboard /
	Bitbucket, so download it again.
	\item Create a new blank script called {\tt ttests.R} and save it to
	the working directory.
	\item Put a comment at the top (using \#) to describe the script
	and.
	\item For the rest of this session, type your code into this script,
	adding comments and then run them in R using Ctrl+R. If you make
	mistakes, correct the script and run the code again. This way you
	will end up with a complete neat version of the commands for your
	analysis.
	\item Add code to your script to load the genome size data into R,
	assigning the object name {\tt genome} and use {\tt str(genome)} to
	check it has loaded correctly.
\end{compactitem}

\section{Using $t$ tests}

The $t$ test is used to compare the mean of a sample to another value:
either some reference point (Is the mean different from 5?) or another
mean (Does the mean of A differ from the mean of B?). If you have a
factor with two levels then a $t$ test is a good way to compare the
means of those samples. If you have more than two levels, then you have
a problem: as the number of levels ($n$) increases, the number of
possible comparisons between pairs of levels ($p$) increases very
rapidly\footnote{The number of pairs is the binomial coefficient
$C(n,2)$ --- inevitably, R has a function for this: {\tt
choose(2:6,2)}}. Making these many comparisons using something like a
t-test is a problem because we neglect the covariance among measures
and inflate the chance of falsely rejecting at least one hypothesis
(Type I error -- recall hypothesis testing in chapter \ref{ch:ExpDesign}).

\noindent\begin{tikzpicture}[label distance=1.2cm]
\node (2) at (0,0) [label=below:{$n=2, p=1$}]{};
\node (3) at (3,0) [label=below:{$n=3, p=3$}]{};
\node (4) at (6,0) [label=below:{$n=4, p=6$}]{};
\node (5) at (9,0) [label=below:{$n=5, p=10$}]{};
\node (6) at (12,0)[label=below:{$n=6, p=15$}]{};
\path (2) ++(0:1)   node (a) {$a$};
\path (2) ++(180:1) node (b) {$b$};
\draw (a) -- (b);
\path (3) ++(0:1)   node (a) {$a$};
\path (3) ++(120:1) node (b) {$b$};
\path (3) ++(240:1) node (c) {$c$};
\draw (a) -- (b) -- (c) -- (a);
\path (4) ++(0:1)   node (a) {$a$};
\path (4) ++(90:1)  node (b) {$b$};
\path (4) ++(180:1) node (c) {$c$};
\path (4) ++(270:1) node (d) {$d$};
\draw (a) -- (b) -- (c) -- (d) -- (a) ;
\draw (a) -- (c)    (d) -- (b) ;
\path (5) ++(0:1)   node (a) {$a$};
\path (5) ++(72:1)  node (b) {$b$};
\path (5) ++(144:1) node (c) {$c$};
\path (5) ++(216:1) node (d) {$d$};
\path (5) ++(288:1) node (e) {$e$};
\draw (a) -- (b) -- (c) -- (d) -- (e) -- (a) ;
\draw (a) -- (c)    (d) -- (b)    (a) -- (d) ;
\draw (e) -- (b)    (e) -- (c) ;
\path (6) ++(0:1)   node (a) {$a$};
\path (6) ++(60:1)  node (b) {$b$};
\path (6) ++(120:1) node (c) {$c$};
\path (6) ++(180:1) node (d) {$d$};
\path (6) ++(240:1) node (e) {$e$};
\path (6) ++(300:1) node (f) {$f$};
\draw (a) -- (b) -- (c) -- (d) -- (e) -- (f) -- (a) ;
\draw (a) -- (c)    (d) -- (b)    (a) -- (d) ;
\draw (e) -- (b)    (e) -- (c)    (a) -- (f) ;
\draw (d) -- (f)    (b) -- (f)    (a) -- (f) ;
\draw (c) -- (f)    (a) -- (e) ;
\end{tikzpicture}

The basic idea  behind the $t$ test is to divide the difference between
two values by a measure of how much uncertainty there is about the size
of that difference. The measure of uncertainty used is the
\emph{standard error}.

\[
	t=\frac{\textrm{difference between values}}{\textrm{standard error}}
\]

When there is no difference between the values $t$ will be zero, but
with big differences and/or small errors, will be larger. The $t$
distribution below shows how commonly different values of $t$ are found
under the null hypothesis.
\begin{center}
\includegraphics[width=0.7\textwidth]{tvals.pdf}
\end{center}

Some points about the plot above:
\begin{compactitem}
	\item The  null hypothesis is that there is no difference between the
	values but because we only estimate the values from samples,
	differences will creep in by chance.
	\item Mostly these differences will be small --- hence the peak in
	the middle --- but sometimes the differences will be large and the
	errors will be small.
	\item 95\% of the area under the curves are between these two sets of
	vertical lines. Values of $t$ more extreme than this will only occur
	1 in 20 times or with a probability ($p$) of 0.05.
	\item The means of small samples are more easily influenced by
	extreme values and so produce extreme $t$ values more frequently.
	This is why the red curve above for smaller samples is more flattened
	out and why the 95\% limits are more spread out.
\end{compactitem}

\section{One sample $t$ tests}

In the simplest example, a $t$ test can be used to test whether the
mean of a sample is different from a specific value. For example:

\begin{compactitem}
	\item Is the ozone in a set of air samples above the legal limit?
	\item Is the change in a set of patient' weights different from zero?
	\item Is the mean genome size for Odonata smaller than 1.25 pg, which
	is the average for insects
	\href{http://www.genomesize.com/statistics.php?stats=insects}{[see
	here]}?
\end{compactitem}

Oh look! We can test that last one\ldots

To calculate $t$, we need that observed difference and then the
standard error of the difference between the mean of our sample and the
known value. This is calculated using the \emph{variance} and the
\emph{sample size} ($n$) of the sample ($s$).

\[se_s = \sqrt{\frac{\textrm{var}(s)}{n}}\]

This simple equation trades off variance --- high variance in the data
gives higher uncertainty about the location of the mean --- and sample
size -- more data gives more certainty. So, \emph{low variance} and
\emph{large datasets} have \emph{small} standard errors; \emph{high
variance} and \emph{small datasets} have \emph{large} standard errors.
Variance is calculated using sums of squares and so the square root is
needed to give a standard error in the same units as the mean.

So, all we need are three values calculated from the data: mean,
variance and the number of data points and we can calculate $t$. R can
do this for us:

\begin{lstlisting}
# calculate the three values from the data:
> mean.gs <- mean(genome$GenomeSize)
> print(mean.gs)
[1] 1.014
> var.gs <- var(genome$GenomeSize)
> print(var.gs)
[1] 0.1397
> n.gs <- length(genome$GenomeSize)
> print(n.gs)
[1] 100
# get the difference
> diff <- mean.gs - 1.25
> print(diff)
[1] -0.2357
# get the standard error
> se.gs <- sqrt(var.gs/n.gs)
> print(se.gs)
[1] 0.03738
# get the t value
> t.gs <- diff/se.gs
> print(t.gs)
[1] -6.306
\end{lstlisting}

\begin{compactitem}[$\quad\star$]
\item Copy and paste the code above into your script in R and run it.
Read through the code and make sure you understand the steps.
\end{compactitem}

This is a big $t$ value --- values this extreme don't even appear on
the graph above --- so we would conclude that the mean genome size for
Odonata is different from the average for insects.

We can do this more easily and get some more information using the
function {\tt t.test}. The null hypothesis can be set using the option
(sometimes called a function \emph{argument}) {\tt mu} --- the Greek
letter $\mu$ is often used to refer to a mean:

\begin{lstlisting}
> t.test(genome$GenomeSize, mu = 1.25)

	One Sample t-test

 data:  genome$GenomeSize
 t = -6.306, df = 99, p-value = 8.034e-09
 alternative hypothesis: true mean is not equal to 1.25
 95 percent confidence interval:
  0.9401 1.0885
 sample estimates:
 mean of x
     1.014
\end{lstlisting}

This confirms the values we calculated by hand and adds a $p$ value.
The output also gives the degrees of freedom. This is something we will
come back to later, but the degrees of freedom are basically the number
of data points minus the number of estimated parameters, which in this
case is one mean.

The output also gives a confidence interval for the observed mean.
The mean is the best estimate of the population mean given our sample
of species of Odonata, but the actual mean for the order could be
bigger or smaller. The confidence interval tells us the region in which
we are 95\% confident that this actual mean lies.

It is calculated using the $t$ distribution. Remember that $t$ is a
difference divided by a standard error; if we multiply $t$ by a
standard error, we get back to a difference. If we pick a pair of $t$
values that contain the middle 95\% of the $t$ distribution, as in the
plot on page 2, then we can multiply that by the standard error from
the data to get a range above and below the mean.  If we sampled lots
of sets of 100 species of Odonata, we expect 95\% of the observed means
to lie inside this range. The code below shows the calculation of the
confidence interval for the test above.

\begin{lstlisting}
# Find the edges of the middle 95% of a t distribution with 99 df
# (quantiles of the t distribution, so qt)
> tlim <- qt(c(0.025,0.975), df = 99)
> print(tlim)
[1] -1.984 1.984
# use the mean and standard error from above to get a confidence interval
> mean.gs + tlim * se.gs
[1] 0.9401 1.0885
\end{lstlisting}

\begin{compactitem}[$\quad\star$]

	\item Using the {\tt t.test} code above as an template, test whether
	the body weight (in grams) of Odonata is different from the
	average\footnote{This slightly dodgy estimate comes from an estimated
	average volume for arthropods of of 45.21 mm$^3$ and assuming a
	density of 1 gm per cm$^3$. The volume is from: Orme, C. D. L.,
	Quicke, D. L. J., Cook, J. M. and Purvis, A. (2002), Body size does
	not predict species richness among the metazoan phyla. Journal of
	Evolutionary Biology, 15: 235--247.} for arthropods of 0.045 grams.

\end{compactitem}

\section{Two sample $t$ tests}

It is more common to use a $t$ test to compare the means of two
samples. This includes questions like:

\begin{compactitem}
	\item Do two rivers have the same concentration of a pollutant?
	\item Do chemical A and chemical B cause different rates of mutation?
	\item Do damselflies and dragonflies have different genome sizes?
\end{compactitem}

The main difference here is that with a one sample $t$ test, we assume
that one of the means is known exactly: the only error is in the single
sample. With a two sample test, we are comparing two means estimated
from samples and both contain error. The graph below illustrates this:
\begin{center}
\includegraphics[width=0.9\textwidth]{twosampleIllustration.pdf}
\end{center}

The vertical lines show the mean (solid lines) and one standard error
to each side (dashed lines). The red mean is the same in both cases,
but the second graph shows that this is also estimated from a sample
with error: the difference in the means looks less convincing and we'd
expect a smaller $t$ value. The $t$ tests in for these two graphs
confirm this:

\begin{compactitem}
\item The mean for blue is significantly different from 16.74
(mean=14.98, se=0.38, df=59, $t$=-4.65, $p$=0.00002).
\item The means of blue and red are significantly different (blue:
mean=14.98, se=0.38; red: mean=16.74, se=0.42; df=118, $t$=-3.13,
$p$=0.002)
\end{compactitem}

\begin{compactitem}[$\quad\star$]
\item Have a close look at the previous two statements. This shows the
kind of detail needed when reporting the results of $t$ tests. The
following is \emph{not} acceptable: The means of blue and red are
significantly different ($p=0.002$).
\end{compactitem}

So, with two samples, we shouldn't be so confident about the difference
between the values --- it should have a higher standard error. We can
do this simply by combining the variance and sample size for the two
samples ($a$ and $b$) into the calculation:

\[
se_{a-b}= \sqrt{\frac{\textrm{var}(a)}{n_a} + \frac{\textrm{var}(b)}{n_b}}
\]

We'll use a $t$ test to address that last question: are the genome
sizes of Anisoptera and Zygoptera different? First, we'll do this by
hand. We'll use a really handy function {\tt tapply(X, INDEX, FUN)} to
quickly find the values for the two groups: it takes some values (X),
splits those values into groups based on a factor (INDEX) and runs each
group through another function (FUN).

\begin{lstlisting}
# calculate the three values from the data
> mean.gs <- tapply(X = genome$GenomeSize, INDEX = genome$Suborder, FUN = mean)
> print(mean.gs)
Anisoptera 	Zygoptera
1.018				1.012
> var.gs <- tapply(X = genome$GenomeSize, INDEX = genome$Suborder, FUN = var)
> print(var.gs)
Anisoptera 	Zygoptera
0.18458 		0.06946
> n.gs <- tapply(X = genome$GenomeSize, INDEX = genome$Suborder, FUN = length)
> print(n.gs)
Anisoptera 	Zygoptera
38					62

# get the difference
> diff <- mean.gs[1] - mean.gs[2]
> print(diff)
Anisoptera
-0.006647

# get the standard error of the difference
> se.gs <- sqrt((var.gs[1]/n.gs[1]) + (var.gs[2]/n.gs[2]))
> print(se.gs)
Anisoptera
0.06932

# get the t value
> t.gs <- diff/se.gs
> print(t.gs)
Anisoptera

-0.09589

\end{lstlisting}

\begin{compactitem}[$\quad\star$]
\item Type the code above into your script in R and run it. Again, read
through the code and make sure you understand the steps.
\end{compactitem}

The {\tt t.test} function automates this all for us, and we can use a
formula (see Chapter \ref{ch:ExpDesign}) to get a test between the two
suborders.

\begin{lstlisting}
>  t.test(GenomeSize ~ Suborder, data = genome)

 	Welch Two Sample t-test

 data:  GenomeSize by Suborder
 t = -0.0959, df = 98, p-value = 0.9238
 alternative hypothesis: true difference in means is not equal to 0
 95 percent confidence interval:
  -0.1442  0.1309
 sample estimates:
 mean in group Anisoptera  mean in group Zygoptera
                    1.012                    1.018

\end{lstlisting}

The output looks very similar to the one sample test, except that the
output  now gives two estimated means, rather than one and it reports
the $p$ value for the calculated $t$ value.

\begin{compactitem}[$\quad\star$]
\item Add this to your script and run it.
\item Copy and modify this in your script to test whether the body
weight of the two suborders are different.
\end{compactitem}

\section{$F$ tests for equal variance}

$F$ tests are used to compare the variances of two samples or
populations. You will most prominently see them in analysis of variance
(ANOVA), to test the hypothesis that the means of a given set of
normally distributed populations all have the same variance.

Let's use our genomesize datast to have a look at F-tests as well.
Ideally, the $t$ test should be used with data that are: i)
\emph{relatively normally distributed}, so that means can be estimated
sensibly; and ii) have \emph{similar variances}. We'll deal with the
similar variance question here using an $F$ test for equal variances.

First, let's visualize the data. As I hope you've already noticed, this
session has been neglecting one very important part of analysis ---
\emph{plotting the data}. We are going to compare two plots, so it
helps to have them side by side in the same window. We can use the
function {\tt par} to change a set of options called graphics
parameters to get R to do this. The option to change is {\tt mfrow}.
This sets a graphics window to include \emph{m}ultiple \emph{f}igures
and we need to tell R the number of rows and columns to divide the
window into: {\tt par(mfrow=c(1,2))}.

\begin{compactitem}[$\quad\star$]
\item Copy {\tt par(mfrow=c(1,2))} into your script, add a comment and
run it.
\item Using your (rapidly improving!) R skills, create a boxplot comparing the
genome sizes of the two suborders.
\item Add another boxplot beside it comparing the body weight of the
two suborders.
\end{compactitem}
It should look like this:
\begin{center}
\includegraphics[width=0.9\textwidth]{bxp.pdf}
\end{center}

The distribution of the test statistic $F$ is simply the ratio of the
variances for sample $a$ and $b$: $\textrm{var}(a)/\textrm{var}(b)$. If
the two variances are the same then $F=1$; if $\textrm{var}(a) >
\textrm{var}(b)$ then $F > 1$; and if $\textrm{var}(a) <
\textrm{var}(b)$ then $F < 1$ (\ref{fig:fdist}).

\begin{figure}
	\centering
	\includegraphics[width=0.7\textwidth]{fvals.pdf}
	\caption[Caption for LOF]{The F distribution. The two vertical blue
	lines show the edges of the central 95\% of the area of the curve: if
	the two samples are drawn at random from a population with the same
	variance then values of $F < 0.482$ or $> 2.074$ are observed fewer
	than 1 time in 20 ($p\le0.05$ )\protect\footnotemark . The shape of
	the $F$ distribution changes depending on the amount of data in each
	of the two samples but will always be centered
	near 1 and with a tail to the right (right-skewed). Note that the
	F-distribution arises as the ratio of two appropriately scaled {\it
	chi-square distributed variates}, because, as we saw above, variances
	should be chi-square distributed.}
\label{fig:fdist}
\end{figure}
\footnote{Note that $1/0.482 \approx 2.074$
	and $1/2.074 \approx 0.482$: in this case, it doesn't matter which
	way round you compare the two variances!}

We can use R to calculate $F$ for the variance in genome size in each
of the two suborders. We calculated the variance for the $t$ test
above, so we can just do this:

\begin{lstlisting}
> var.gs[1]/var.gs[2]
Anisoptera
		2.657
\end{lstlisting}

That's quite a big $F$ value and we can use the function {\tt var.test}
to do all the calculations for us and give us the actual $p$ value:

\begin{lstlisting}
> var.test(GenomeSize ~ Suborder, data = genome)
 	F test to compare two variances

 data:  GenomeSize by Suborder
 F = 2.657, num df = 61, denom df = 37, p-value = 0.001946
 alternative hypothesis: true ratio of variances is not equal to 1
 95 percent confidence interval:
  1.449 4.671
 sample estimates:
 ratio of variances
              2.657
\end{lstlisting}

It produces the same value that we calculated by hand and shows that,
if the two samples are drawn from populations with the same variance,
an $F$ value this extreme will only be observed roughly 1 time in 500
($1/0.00195 \approx 500$) .

\begin{compactitem}[$\quad\star$]
\item Open a new empty script called {\tt FTests.R}.
\item In this write your script to test whether the variances in the
body weight of the two suborders from the {\tt GenomSize} dataset are
different.
\end{compactitem}

There are clearly problems with the variance in both examples. The next
two sections present ways to address these kinds of problems.

\section{$t$ tests revisited}

The first thing to say is that R is aware of the problem with the
variance. If you look back at the output from the previous $t$ tests,
you will see that the degrees of freedom vary a lot. We have 100
observations and -- after subtracting one for each mean we calculate
--- our degrees of freedom should be either 99 (one sample test) or 98
(two sample test). What we actually see are smaller numbers, with the
smallest being {\tt df = 60.503} for the two sample test of body
weight.

The explanation is that R is applying a \emph{penalty} to the degrees
of freedom to account for differences in variance. With fewer degrees
for freedom, more extreme $t$ values are more likely and so it is
harder to find significant results. This doesn't mean we can forget
about checking the variances or plotting the data!

In this case, we can also apply a transformation to the data in order
to make the variances more equal. Forgetting the wings and assuming
Odonata are shaped like a box, the model in the graph below shows how
volume changes with length: equal changes in length do not lead to
equal changes in volume and longer species will have a
disproportionately large volume. This is a classic feature of
morphological data known as allometric scaling and we'll look at it
again in Chapter \ref{ch:ANOVA}. In the meantime, a log transformation will
turn body weight from a skewed distribution to a more normal
distribution.

\begin{center}
\includegraphics[width=\textwidth]{transform.pdf}
\end{center}

% We can create a new variable in the {\tt genome} data frame of
$\log_e$ body weight as follows:

\begin{lstlisting}
> genome$logBodyWeight <- log(genome$BodyWeight)
\end{lstlisting}

\begin{compactitem}[$\quad\star$]
\item Copy the line into your script and run it.
\item Now write three lines of code to get a boxplot of $\log_e$ body
weight and then run a variance test and $t$ test on the differences in
$\log_e$ body weight between suborders.
\end{compactitem}

This gives a much clearer result --- the variances are almost identical
and the differences between the suborders are much more cleanly tested.

\section{Non-parametric tests}

What happens if there isn't a convenient transformation for the
variable that gives roughly constant variation and equal variance?  In
a parametric test, like the $t$ and $F$ test above, we use parameters
(mean and variance) to describe the data, assume these describe the
data well and then just use these parameters to run the test. If these
assumptions don't seem very sound, the non-parametric tests provide a
way of using the ranks of the data to test for differences. They aren't
as powerful --- they are less likely to reveal significant differences
--- but they are more robust. The most commonly used alternative is the
Wilcoxon test, which uses the function {\tt wilcox.test} in R.

\begin{compactitem}[$\quad\star$]

\item Using {\tt wilcox.test} as a replacement for {\tt t.test}, repeat
the one and two sample $t$ test for genome size and body weight.
\item Compare the two results.
\item Repeat the same with the predator and prey body mass data from
the plotting and visualization chapter in SilBioComp.pdf -- check how
different the results are when using $t$ vs. Wilcoxon test.

\end{compactitem}


\section{Introduction}

Modelling the variance\footnote{In this document variance means variance of the returns and volatility is the standard deviation of returns.} of the rate of return on stocks is a cornerstone of modern finance.
Variance is a basic building block of risk measurement and prediction uncertainty, and capturing empirical facts about its behaviour is the aim of many theorists~\citep{Christie1982}.
This master thesis is concerned with the so-called leverage effect, also called asymmetric volatility, that captures the correlation between returns and changes in volatility.

\subsection{The anti-leverage effect in China}

The leverage effect is, by definition, the negative relationship between the change in price and the change in volatility.
It was first described by~\citet{black1976studies}, whose explanation was the connection to firms' leverage ratio (LR), i.e.\ the market equity to debt ratio.
Asymmetric volatility can be deduced in basic structural models of corporate finance, and the effect has also been shown using both realised volatility and implied volatility estimates of various models~\citep{Christie1982,french1987expected,Harvey1996,Bouchaud2001}.

The Chinese financial market has been traditionally different from western systems mainly due to the differently regulated environment~\citep{GORDON2003}.
That might also lead to counter-intuitive consequences that are not in line with widely used assumptions.
\citet{Shen2009} compared the leverage effect in Germany and in China, and they found a positive correlation between returns and volatility in the latter, contradicting the previously mentioned works resulting in negative correlation.

\subsection{Research objective}

The main question addressed by this master thesis:
\begin{center}
	Can the anti-leverage effect be observed in the Chinese market?
\end{center}
The question is a reflection to~\citet{Shen2009}, it is answered after choosing an appropriate model that estimates the leverage effect.
Inspired by~\citet{Christensen2015}, time-variability of the effect is also examined around the Global Financial Crisis of 2007.


%%% Title:    Categorical Code Interpretations
%%% Author:   Kyle M. Lang
%%% Created:  2021-03-23
%%% Modified: 2021-03-24

\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
% maxwidth is the original width if it is less than linewidth
% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0, 0, 0}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.69,0.494,0}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.749,0.012,0.012}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.514,0.506,0.514}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0,0.341,0.682}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.004,0.004,0.506}{#1}}%
\let\hlipl\hlkwb

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}

\usepackage{graphicx}
\usepackage{booktabs}
\usepackage[natbibapa]{apacite}
\usepackage[libertine]{newtxmath}
\usepackage{fancybox}
\usepackage{datetime2}

\title{Interpreting the Intercept in Linear Regression Models with Categorical Predictors}
\author{Kyle M. Lang}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}

\maketitle

In this document, I'll go through the interpretation of the estimated intercept
in multiple linear regression models that contain categorical predictors. The
interpretation of the intercept differs based on the nature of the variables on
the right hand side (RHS) of the regression equation. For simple linear
regression models, the following represent the intercept interpretations for
some common types of predictor variables.

\begin{itemize}
\item Continuous $X$: The model-implied mean of $Y$ (i.e., the expected value
  for $Y$, given our model) for cases with $X = 0$.
\item Dummy-coded $X$: The model-implied mean of $Y$ for cases in the reference
  group of $X$.
\item Unweighted effects-coded $X$: The model-implied, unweighted mean of $Y$
  (see below for definition of the unweighted mean)
\item Weighted effects-coded $X$: The model-implied, weighted mean of $Y$
  (see below for definition of the weighted mean)
\end{itemize}

When we combine different types of variables and/or codes on the RHS of a
multiple linear regression model, the intercept is interpreted as a sort of
``intersection'' of the interpretations for the separate variable types/codes.

\begin{itemize}
\item Dummy-coded $X_1$ and continuous $X_2$: The model-implied mean of $Y$ for
  cases in the reference group of $X_1$ with $X_2 = 0$.
\item Unweighted effects-coded $X_1$ and continuous $X_2$: The model-implied,
  unweighted mean of $Y$ for cases with $X_2 = 0$.
\item Weighted effects-coded $X_1$ and continuous $X_2$: The model-implied,
  weighted mean of $Y$ for cases with $X_2 = 0$.
\item Dummy-coded $X_1$ and unweighted effects-coded $X_2$: The model-implied,
  unweighted mean of $Y$ for cases in the reference group of $X_1$.
\item Dummy-coded $X_1$ and weighted effects-coded $X_2$: The model-implied,
  weighted mean of $Y$ for cases in the reference group of $X_1$.
\item Weighted effects-coded $X_1$ and unweighted effects-coded $X_2$: The
  model-implied mean of $Y$ calculated with proportional weighting across the
  groups of the $X_1$ factor and without weighting for the groups of the $X_2$
  factor.
\end{itemize}

Note that the final combination (e.g., both weighted and unweighted effects
codes) should not come up much, in practice. Usually, your data characteristics
or your inferential goals will motivate the use of either all weighted effects
codes or all unweighted effects codes. In the following section, I'll show a few
examples of the combined interpretations described above.

\section{Example Interpretations}

We'll anchor our interpretations by refitting the same model with different
coding schemes for the predictor variables. In our example model, we'll regress
BMI onto a two-level factor representing biological sex and a three-level factor
representing educational attainment level.
\begin{align*}
  Y_{\textit{bmi}} =
  \beta_0 +
  \beta_1X_{\textit{female}} +
  \beta_2X_{\textit{ed.mid}} +
  \beta_2X_{\textit{ed.hi}} +
  \varepsilon
\end{align*}

We will use the \emph{BMI} data from the \textbf{wec} package \citep{wec} to fit
this model using different flavors of categorical coding for the \emph{sex} and
\emph{education} factors.

\subsection{Two Dummy Codes}

When coding each factor using dummy codes, we get the following fitted
regression equation.
\begin{align*}
  \hat{Y}_{\textit{bmi}} =
  26.4
  -0.5X_{\textit{female}}
  -1.14X_{\textit{ed.mid}}
  -1.86X_{\textit{ed.hi}}
\end{align*}
The male group is the reference group for the \emph{sex} factor, and the lowest
educational attainment group is the reference group for the \emph{education}
factor. So, the intercept, $\hat{\beta}_0 = 26.4$,
represents the model-implied mean of BMI (i.e., the expected BMI given our
fitted model) for males in the lowest educational attainment group.

\subsection{Two Unweighted Effects Codes}

When coding each factor using unweighted effects codes, we get the following
fitted regression equation.
\begin{align*}
  \hat{Y}_{\textit{bmi}} =
  25.15 +
  0.25X_{\textit{female}} +
  1X_{\textit{ed.mid}}
  -0.14X_{\textit{ed.hi}}
\end{align*}
In this case, we don't need to worry about keeping track of the reference groups
because the intercept represents the (unweighted) grand mean of BMI. Since both
factors are coded with unweighted effects codes, the model-implied, unweighted
mean of BMI is $\hat{\beta}_0 = 25.15$.

\subsubsection{What is the Unweighted Mean?}
For this model, the unweighted mean is defined as the simple average of the
group-specific means of BMI. Since the \emph{sex} factor has two levels and the
\emph{education} factor has three levels, we have $2 \times 3 = 6$
group-specific means.

% latex table generated in R 4.0.4 by xtable 1.8-4 package
% Wed Mar 24 22:45:55 2021
\begin{table}[ht]
\centering
\begin{tabular}{rrrr}
  \toprule
 & highest & middle & lowest \\
  \midrule
female & 26.07 & 25.29 & 24.71 \\
  male & 26.22 & 24.74 & 23.89 \\
   \bottomrule
\end{tabular}
\caption{Group-Specific Means of BMI}
\label{mTab}
\end{table}

Averaging these six group-specific means produces the unweighted mean of BMI
($\overline{\textit{BMI}} = 25.15$). We call this
quantity the \emph{unweighted} mean because we compute it as the simple average
of the group-specific means (i.e., we don't weight the means by their relative
sample sizes).

\subsection{Two Weighted Effects Codes}

When coding each factor using weighted effects codes, we get the following
fitted regression equation.
\begin{align*}
  \hat{Y}_{\textit{bmi}} =
  24.98
  -0.23X_{\textit{female}} +
  0.02X_{\textit{ed.mid}}
  -0.7X_{\textit{ed.hi}}
\end{align*}
Again, we don't need to concern ourselves with the reference groups because the
intercept represents the (weighted) grand mean of BMI. Since both factors are
coded with weighted effects codes, the model-implied, weighted mean of BMI is
$\hat{\beta}_0 = 24.98$.

\subsubsection{What is the Weighted Mean?}
In this case, the weighted mean is defined as the weighted average of the
group-specific means of BMI. We weight the group-specific means shown in Table
\ref{mTab} by weights representing the proportion of the total sample size
associated with each group.

% latex table generated in R 4.0.4 by xtable 1.8-4 package
% Wed Mar 24 22:45:55 2021
\begin{table}[ht]
\centering
\begin{tabular}{rrrr}
  \toprule
 & lowest & middle & highest \\
  \midrule
male & 0.103 & 0.187 & 0.181 \\
  female & 0.107 & 0.241 & 0.180 \\
   \bottomrule
\end{tabular}
\caption{Group-Specific Sample Weights}
\label{wTab}
\end{table}

To compute the weighted mean, assume we have two matrices such as those shown in
Tables \ref{mTab} and \ref{wTab}:
\begin{itemize}
\item An $L \times K$ matrix of group-specific means of $Y$, $M$, with elements
  $m_{lk}$
\item The corresponding matrix of group-specific sample weights, $W$, with
  elements $w_{lk}$
\end{itemize}
We can then compute the weighted mean of $Y$ via the following formula:
\begin{align*}
  \bar{Y} = \sum_{l = 1}^L \sum_{k = 1}^K w_{lk} m_{lk}
\end{align*}
For our example data, the estimated weighted mean is $\overline{\textit{BMI}} =
24.98$. When computing this quantity from the raw
data, the weighted mean of $Y$ will equal the arithmetic average of $Y$,
$\bar{Y} = N^{-1} \sum_{n = 1}^N y_n$.

\subsection{Dummy Code and Unweighted Effects Code}

When we code \emph{sex} with a dummy code and \emph{education} with unweighted
effects codes, we get the following fitted regression equation.
\begin{align*}
  \hat{Y}_{\textit{bmi}} =
  25.4
  -0.5X_{\textit{female}} +
  1X_{\textit{ed.mid}}
  -0.14X_{\textit{ed.hi}}
\end{align*}
The reference groups for \emph{sex} is still males. So, the intercept,
$\hat{\beta}_0 = 25.4$, represents the model-implied,
unweighted mean of BMI for males. To compute the analogous marginal mean from
the raw data, we would simply average the three group-specific means in the
first row of Table \ref{mTab}.

\subsection{Dummy Code and Weighted Effects Code}

When coding \emph{sex} with a dummy code and \emph{education} with weighted
effects codes, we get the following fitted regression equation.
\begin{align*}
  \hat{Y}_{\textit{bmi}} =
  25.24
  -0.5X_{\textit{female}} +
  0.02X_{\textit{ed.mid}}
  -0.7X_{\textit{ed.hi}}
\end{align*}
Here, the intercept, $\hat{\beta}_0 = 25.24$, represents the
model-implied, weighted mean of BMI for males. To compute the analogous marginal
mean from the raw data, we would weight the three group-specific means in the
first row of Table \ref{mTab} by the corresponding sample weights in the first
row of Table \ref{wTab} and sum these products.

\section{Estimated Intercepts $\neq$ Data-Derived Means?}

Often, the estimated intercept from a given model does not match the
corresponding marginal mean that we would calculate from the raw data. We can
see an example of this discordance if we review the example with two dummy-coded
predictors from above. When we use dummy codes for both the \emph{sex} and
\emph{education} factors, we get an estimated intercept of $\hat{\beta}_0 =
26.402$, but the corresponding marginal mean from Table
\ref{mTab} is $\overline{\textit{BMI}}_{male,ed.low} = 26.074 \neq \hat{\beta}_0$.

Although this discordance may be disturbing, we don't really need to worry about
it. Remember that, for this example, the intercept is the \emph{model-implied}
mean of males in the lowest educational group. Our model does not perfectly
reproduce our data, though, so there can be discrepancies. If we estimate the
appropriate marginal means using our fitted model \citep[e.g., by using the
  \textbf{emmeans} package,][]{emmeans}, we see that the relevant marginal mean
estimated from our fitted model matches the estimated intercept.

% latex table generated in R 4.0.4 by xtable 1.8-4 package
% Wed Mar 24 22:45:55 2021
\begin{table}[ht]
\centering
\begin{tabular}{rrrr}
  \toprule
 & lowest & middle & highest \\
  \midrule
male & 26.40 & 25.26 & 24.55 \\
  female & 25.90 & 24.76 & 24.05 \\
   \bottomrule
\end{tabular}
\caption{Marginal Means Estimated from the Model}
\label{mmTab}
\end{table}

\subsection{How is this Possible?}

To get some intuition for how the discordance between the model-based estimates
and the data-derived quantities can occur, consider the following figure.

\vspace{12pt}

\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}

{\centering \includegraphics[width=0.85\linewidth]{figure/unnamed-chunk-10-1}

}

\end{knitrout}

The above figure shows a scatterplot of $Y$ against $X$ with the fitted
regression line overlaid. Notice that the estimated intercept is $\hat{\beta}_0
= 4.74$. If, however, we average all of the $Y$ values for
which $X = 0$ (i.e., the red points in the figure), we get an estimated mean of
$Y_{X = 0} = 2.22 \neq \hat{\beta}_0$. Although these two
quantities share the same interpretation, they need not match in
practice. Regardless of what types of predictors are included, the model from
which the intercept is derived is only approximating the ideal relationship
between $X$ and $Y$. So, the estimated intercept may not match the analogous
quantity estimated directly from the raw data.

\bibliographystyle{apacite}
\bibliography{references.bib}

\end{document}


\documentclass[11pt,a4paper]{report}
\usepackage{amsmath,amsfonts,amssymb,amsthm,epsfig,epstopdf,titling,url,array}
\usepackage{enumitem}
\usepackage{changepage}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{listings}
\usepackage{color}
\theoremstyle{plain}
\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}[thm]{Lemma}
\newtheorem{prop}[thm]{Proposition}
\newtheorem*{cor}{Corollary}
\theoremstyle{definition}
\newtheorem{defn}{Definition}[section]
\newtheorem{conj}{Conjecture}[section]
\newtheorem{exmp}{Example}[section]
\newtheorem{exercise}{Exercise}[section]
\theoremstyle{remark}
\newtheorem*{rem}{Remark}
\newtheorem*{note}{Note}
\def\changemargin#1#2{\list{}{\rightmargin#2\leftmargin#1}\item[]}
\let\endchangemargin=\endlist

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
	backgroundcolor=\color{backcolour},
	commentstyle=\color{codegreen},
	keywordstyle=\color{magenta},
	numberstyle=\tiny\color{codegray},
	stringstyle=\color{codepurple},
	basicstyle=\footnotesize,
	breakatwhitespace=false,
	breaklines=true,
	captionpos=b,
	keepspaces=true,
	numbers=left,
	numbersep=5pt,
	showspaces=false,
	showstringspaces=false,
	showtabs=false,
	tabsize=2
}

\lstset{style=mystyle}
\begin{document}

\section*{Problem}
Moe’s wife Mathilda got him a cool watch like the one shown below for Christmas.  He complained that without any hash marks for minutes, he could never tell exactly what time it was.  Matty said that a few minutes after the time in the picture, he could tell exactly what time it is – when the hands make a straight line.  What time will that be?

\begin{figure}[h!]
	\includegraphics[width=2in]{watch.png}
	\caption{}
	\label{fig:Moe's Watch}
\end{figure}

\section*{Bonus}
What is the formula for the minutes after the hour $h$ when the hands will make a straight line for $h$ between 6 and 11?

\newpage
\section*{Solution}
Let $\alpha$ be the angle (in radians) between the vertical (12 o'clock) and the position of the minute hand at time $t$ minutes after $8$.  So for example at $15$ minutes after the hour, $\alpha = \pi/4$ or in general, since $t$ is measured in minutes,  $\alpha = 2{\pi}t/60.$  Now for the hour hand to make a vertical line with the minute hand, it has to be $\pi$ more radians around the clock, which means it has to be at $2{\pi}t/60 + \pi.$  Now we know that at exactly $8$, the hour hand's position is $2{\pi}8/12 = 4{\pi}/3$ and we know that after $t$ minutes it will be $2\pi/12 \times t/60$ radians further along the circle.  (It has to make it one twelfth of the way around the circle in 60 minutes.) So to find  time $t$ when the straight line alignment happens, we just have to solve $$4\pi/3 +  (2\pi/12 )(t/60) = 2{\pi}t/60 + \pi.$$  Solving that for $t$ gives 10.90909..., which translates to 8:10:55 (rounding 54.5454 seconds to 55).

\section*{Bonus Solution}
Replacing 8 with $h$ in the formula for $\alpha$ above results in the general solution  $$t = {\dfrac {(h - 6)60}{11}}$$

\end{document}


\section{Evaluation}
Once the community partition result is retrieved by taking a certain model, a subsequent approach is to evaluate model performance by running comparisons among different models. Therefore, how to carry out comparative analysis and what metrics should be reported as benchmarks are two imperative factors to be considered. In the following sections, I will address these two questions separately by introducing several highly cited papers about comparative analysis and a number of best known metrics widely used in industry and academia.

\subsection{Comparative Studies}
Comparative studies often run a number of methods on different datasets to explore what factors to cause the performance differences. This type of research aims to  offer model suggestions according to graph structure. As a follow-up work of \cite{leskovec2010empirical}, \cite{yang2015defining} compares 13 community scoring functions and employs them on 230 graph datasets belonging to various topics (i.e. social graphs, collaboration graphs, and purchasing graphs) and with different sizes (from hundreds of thousand to hundreds of millions of nodes and edges in graphs). The 13 scoring functions lie in four categories including internal connectivity, external connectivity, the combination of internal \& external connectivity and network model (modularity). To assess these scoring functions' community adequacy, four metrics are thereafter to considered, including separability, density, cohesiveness and clustering coefficient , which are often used to represent structural-level community coherence and compactness (details will be introduced in the next section). There are significant performance differences among all scoring functions: conductance (it measures the ratio of edges linking outside of the community) and triad participation ratio (the ratio of nodes in the same cluster forms a triad) achieves the best performance, while modularity is with poor performance.

\cite{orman2012comparative} argues that current community detection methods totally ignore the topological nature
of the communities as two methods may have similar evaluation performance but totally different community distributions. It claims that adding community topological metrics such as community-wise density, average distance, internal transitivity, and hub dominance may give more supportive understandings about the community partition result. Moreover, by testing on several real-world graph datasets as well as synthetic graphs using different types of approaches (e.g., modularity-based and diffusion-based approaches), it also empirically shows that there is no direct correlation between the model performance metrics (e.g., rand index and normalized mutual information) and community topological metrics.

\cite{hric2014community} questions the  appropriateness to simply use the node metadata information (e.g., node category or membership) as the criteria to build up the ground truth. By running 11 different community detection models on 16 different graph datasets, it claims that there is a trivial correlation between the communities constructed by graph metadata and model-detected communities. Thus, it concludes that metadata communities are not able to fully infer graph topological structure as they are separate views for the graphs. Instead, graph metadata can be taken as either auxiliary information to enhance community partition performance or another perspective to interpret graphs.

By employing 8 different models on Lancichinetti-Fortunato-Radicchi (LFR) benchmark graph \cite{lancichinetti2008benchmark}, \cite{yang2016comparative} summarizes how well these models can handle large-scale graph and community size recovery problems. All model performance and computing time are assessed and compared with each other. In the end, the paper offers some insights about how to choose the proper model given graph descriptive parameters such as LFR mixing parameter.

 For the proposed approach in \cite{abrahao2012separability}, eight large scale datasets are analayzed through 10 different methods for community detection. Along with the annotated communities from graph metadata, each graph dataset is associated with 11 types of community partition results. After calculating 36 types of structural properties of each filtered community from all 11 community partition results, the paper takes these structural properties as features to predict their related community detection methods via a Support Vector Machine (SVM) model.  The final comparative analysis gives researchers clear insights for community detection model selection.

\cite{wang2015community} formulates a generalized procedure of community detection. A procedure-oriented framework
for benchmarking is proposed with four core modules including Setup, Detection Framework, Diagnoses, and Evaluation, which enables researchers to evaluate and compare various approaches through a universal pipeline. Particularly, in the Detection Framework, the paper shows an example by re-implementing 10 community detection models; in the Diagnoses module, key factors and key steps in each individual community detection model can be analyzed for improving model performance; in the Evaluation module, models can be assessed from multiple perspectives including efficiency, accuracy, effectiveness, and sensitivity.

\cite{shen2010spectral} evaluates the top five graph matrices utilized in spectral clustering methods which can generate the best community partition.  The five matrices conducted in the comparative study include adjacency matrix, standard Laplacian matrix, normalized Laplacian matrix,
modularity matrix and correlation matrix. By employing k-means method on top of the decomposed eigenvectors of all matrices from a set of benchmark graphs, the paper shows that the normalized Laplacian matrix and correlation matrix achieve better performance in terms of community partition accuracy.

\subsection{Metrics}

The ill-defined community detection task leads to no universal standard to quantify the appropriateness of a community detection algorithm. \cite{chakraborty2017metrics} introduces a bunch of metrics as benchmarks from both community structure and community evaluation perspective. It also categorizes the metrics for overlapping and non-overlapping purposes.  In this section, we follows the taxonomy offered by \cite{chakraborty2017metrics} to group several widely used metrics into two categories: community structure (to only describe generated communities' characteristics) and community evaluation (to compare the detected communities with ground truth communities). While I won't separate metrics based on whether it is designed for overlapping communities or not because many metrics are eligible for both types of communities and many of the rest metrics for overlapping and non-overlapping community detection are with trivial difference.

As mentioned in Table \ref{tab:c2_glossary}, given a graph $G(V,E)$, $C = \{c_{1},c_{2},...,c_{k}\}$ is the set of detected communities users generated with a specific partition model. $\Omega = \{\omega_{1},\omega_{2},...\omega_{k}\}$ is the ground truth communities given as a prior knowledge. $N = |V| = \sum_{\omega \in \Omega}|\omega|= \sum_{c \in C}|c|$  denotes the total number of the nodes in the original graph. $M= |E|$ denotes the number of total edges in the original graph. $N_{c} = |c|$ denotes the number of nodes in community $c$.

\subsubsection{Community Structure Metrics}

The structure metrics are used to measure the community intrinsic characteristics such as how well a graph community is densely connected. There are numerous metrics existing among current works and hereby I just report several most frequently used ones. In this section, given a community $c$, its varied structure metrics are formulated as $f(c)$. Table \ref{tab:c2_structure_metric} summarizes all mentioned metrics which details will be further explained in the following paragraphs.

\begin{table}
	% \scriptsize
	\centering
	%   \vspace{-3em}
	% \renewcommand{\tabcolsep}{2pt}
	\begin{tabular}{|p{5cm}|p{9cm}|} \hline
	\textbf{Metric} &  \textbf{Definition}    \\ \hline
				Separability & $f(c) = \frac{|E_{c}^{in}|}{|E_{c}^{out}|}$ \\ \hline
				Density&$f(c) = \frac{2|E_{c}^{in}|}{N_{c}(N_{c}-1)}$ \\ \hline
				Clustering coefficient & $f(c) = \frac{T_{c}^{clo}}{T_{c}^{clo} +T_{c}^{op} }$\\ \hline
				Cut Ratio & $f(c) = \frac{|E_{c}^{in}|}{N_{c}(N-N_{c})}$ \\ \hline
				Conductance& $f(c) = \frac{|E_{c}^{out}|}{2|E_{c}^{in}|+|E_{c}^{out}|}$ \\ \hline
				Normalized cut & $f(c) = \frac{|E_{c}^{out}|}{2|E_{c}^{in}|+|E_{c}^{out}|} + \frac{|E_{c}^{out}|}{2(M-|E_{c}^{in}|)+|E_{c}^{out}|}$\\ \hline
				Modularity& $f(c) = \frac{|E_{c}^{in}|}{M}-\left(\frac{|E_{c}^{in}|+|E_{c}^{out}|}{2M}\right)^2$\\ \hline
				Surprise& $f(c) = -log\frac{\binom{F_c}{|E_c|}\binom{F-F_c}{M-|E_c|}}{\binom{F}{M}}$ \\ \hline
				Significance & $f(c) =  \binom{N_c}{2}D(p_c|| p)$\\ \hline
				Permanence & $f(c) = \sum_{v \in c}\left[ \frac{I(v)}{E_{max}(v) } \times \frac{1}{D(v)}- (1-c_{in}(v))\right]$ \\ \hline
				Communitude & $f(c) = \frac{\frac{|E_c|}{M} - \left(\frac{D_c}{2M}\right)^2}{\sqrt{\left(\frac{D_c}{2M}\right)^2\left(1-\frac{D_c}{2M}\right)^2}}$\\ \hline
	\end{tabular}
	\caption{Community Structure Metrics}
	\label{tab:c2_structure_metric}

\end{table}

\textbf{Separability} measures the ratio between the numbers of edges within or on the boundary of the community $c$, which is calculated as:
\begin{equation}
f(c) = \frac{|E_{c}^{in}|}{|E_{c}^{out}|}
\end{equation}

where $|E_{c}^{in}|$ denotes the edges within the community $c$ and $|E_{c}^{out}|$ denotes the edges on the boundary of community. Higher value indicates the community is with better coherence.

\textbf{Density} assumes that nodes in an optimized community should be densely connected to each other. It measures a community by considering the ratio of actual number of edges and the possible edge counts within community $c$.

\begin{equation}
	f(c) = \frac{2|E_{c}^{in}|}{N_{c}(N_{c}-1)}
\end{equation}

where $N_{c}$ is the number of nodes inside community $c$.  Higher score means the nodes in the community are better connected.

\textbf{Clustering coefficient} is calculated based on the number of triplet nodes within a community $c$. There are two types of triplets: If there are two edges among the three nodes in the triplet, it forms a open triplet $T_{c}^{op}$; If there are three edges existing in a triplet (meaning each pair of nodes are connected), it forms a closed triplet  $T_{c}^{clo}$.  Clustering coefficient indicates the ratio of closed triplets in the two types of triplets, which is calculated as:
\begin{equation}
f(c) = \frac{T_{c}^{clo}}{T_{c}^{clo} +T_{c}^{op} }
\end{equation}

\textbf{Cut ratio} calculates the ratio of boundary edges out of all possible edges that leaving the community $c$, which is calculated as:
\begin{equation}
f(c) = \frac{|E_{c}^{out}|}{N_{c}(N-N_{c})}
\end{equation}
where $N$ denotes the number of nodes in the whole graph. $N_{c}(N-N_{c})$ calculates the number of possible node pairs that the community $c$ reaching to outside. Small ratio indicates the same-community nodes are likely to be grouped together and apart from the rest nodes in the graph.

\textbf{Conductance} measures the ratio of edges that community $c$ points to the inside and outside of the community. It indicates the degree of edges leaving out of current community. Higher value indicates the community $c$ has more edges connected with external communities. The conductance is calculated as:
\begin{equation}
f(c) = \frac{|E_{c}^{out}|}{2|E_{c}^{in}|+|E_{c}^{out}|}
\end{equation}

The reason that $|E_{c}^{in}|$ is calculated twice is because both the start and end nodes of these edges are inside the community $c$.

\textbf{Normalized cut} is an extension of conductance by adding another term related to the outside community linkages, which is calculated as:

\begin{equation}
f(c) = \frac{|E_{c}^{out}|}{2|E_{c}^{in}|+|E_{c}^{out}|} + \frac{|E_{c}^{out}|}{2(M-|E_{c}^{in}|)+|E_{c}^{out}|}
\end{equation}

The first term in Normalized cut is the same as conductance, which represents the tendency that nodes in community $c$ link inside the community. Similarly, the second term represents the tendency that nodes in outside communities connect to current community $c$. $M$ is the total number of edges in the graph.

\textbf{Modularity}, as mentioned in previous chapters, is a type of scoring function to measure how well the current community surpasses random-generated communities in terms of the within-community node connection. It is calculated as:
\begin{equation}
f(c) = \frac{|E_{c}^{in}|}{M}-\left(\frac{|E_{c}^{in}|+|E_{c}^{out}|}{2M}\right)^2
\end{equation}

Derived from Modularity, \textbf{Surprise} \cite{aldecoa2013surprise,aldecoa2011deciphering} also compares the distribution of the nodes and edges in
a community to a random-emerged null model. By calculating the actual probability of a community given a partition result, it measures how unlikely that distribution happens. The calculation for each community Surprise score is showed as:

\begin{equation}
f(c) = -log\frac{\binom{F_c}{|E_c|}\binom{F-F_c}{M-|E_c|}}{\binom{F}{M}}
\end{equation}

where $F= \frac{N(N-1)}{2}$ is the maximum possible number of edges in the graph $G$. $F_c= \frac{N_c(N_c-1)}{2}$ is the maximum possible number of edges appeared in the community $c$.

\textbf{Significance} \cite{traag2013significant} converts the original community partition task to a new thought of finding dense subgraphs with a certain number of internal edges in a random graph. The Significance score of a community with size $N_c$ and density $p_c$ is calculated as its log-likelihood probability of generating it from the full graph with $N$ nodes and density $p$, which is rigorously proved  as:
\begin{equation}
f(c) = \binom{N_c}{2}\mathcal{D}(p_c|| p)
\end{equation}
where $\mathcal{D}(\cdot|| \cdot)$ denotes the Kullback-Leibler divergence.

\textbf{Permanence} \cite{chakraborty2014permanence,chakraborty2016permanence} is a vertex-based community quality metric to quantify how well the nodes will be persisted in the community instead of pulled out by other communities. For each node $v$ in community $c$, its in-community connections $I(v)$ is divided by its maximum connections $E_{max}(v)$ to each external communities. To ensure the final score in a range of 0 to 1, it is normalized by the node degree $D(v)$. $c_{in}(v)$  denotes the internal clustering coefficient of node $v$ in community $c$, which calculates the ratio of its edges within the community to all its edges. It functions as a penalty part in the permanence metric. Finally, the community score is the sum of all its nodes scores, which is calculated as:
\begin{equation}
f(c) = \sum_{v \in c}\left[ \frac{I(v)}{E_{max}(v) } \times \frac{1}{D(v)}- (1-c_{in}(v))\right]
\end{equation}

\textbf{Communitude} \cite{miyauchi2015network} quantifies the community $c$ in terms of the fraction of the number of edges
within the community using the Z-score as follows:

\begin{equation}
f(c) = \frac{\frac{|E_c|}{M} - \left(\frac{D_c}{2M}\right)^2}{\sqrt{\left(\frac{D_c}{2M}\right)^2\left(1-\frac{D_c}{2M}\right)^2}}
\end{equation}
where $D_c$ is the sum of node degrees in community $c$.

\subsubsection{Community Evaluation Metrics}

Once the community partition result $C$ is retrieved by employing a specific detection model, its performance can thereafter be evaluated by comparing with ground truth community $\Omega$. In this dissertation, various types of measurements are introduced to help quantify model performances, which are named as community evaluation metrics and showed in Table \ref{tab:c2_evaluation_metric}. The score of evaluation metric given a community partition $C$ and ground truth $\Omega$ is formulated as $f(C,\Omega)$.

\begin{table}
	% \scriptsize
	\centering
	%   \vspace{-3em}
	% \renewcommand{\tabcolsep}{2pt}
	\begin{tabular}{|p{5.5cm}|p{8.5cm}|} \hline
		\textbf{Metric} &  \textbf{Definition}    \\ \hline
		Purity& $f(C,\Omega) = \frac{1}{N} \sum_{k} \max_{j}N_{c_{k},\omega_{j}}$  \\ \hline
		Precision (P)& $f(C,\Omega) = \frac{TP}{TP+FP}$ \\ \hline
		Recall (R)& $f(C,\Omega) = \frac{TP}{TP+FN}$\\  \hline
		$F_{\beta}$& $f(C,\Omega) = \frac{(\beta^{2}+1)P\times R}{\beta^{2}(P+R)}$ \\ \hline
		Rand index& $f(C,\Omega) = \frac{TP+TN}{TP+FP+FN+TN}$ \\ \hline
		Normalized mutual information & $ f(C,\Omega) = \frac{I(C,\Omega)}{[H(C)+H(\Omega)]/2}$ \\ \hline
	\end{tabular}
	\caption{Community Evaluation Metrics}
	\label{tab:c2_evaluation_metric}

\end{table}

To calculate \textbf{Purity}, a detected community $c$ is assigned to a label which appears most frequently on the nodes within community $c$. It measures the ratio of nodes which labels are correctly assigned in each community:
\begin{equation}
	f(C,\Omega) = \frac{1}{N} \sum_{k} \max_{j}N_{c_{k},\omega_{j}}
\end{equation}

where $N_{c_{k},\omega_{j}} = |c_{k}\cap \omega_{j}|$ denotes the number of common nodes detected in the two communities $c_k \in C $ and $\omega_j \in \Omega$.  However, purity is really sensitive to the number of communities for the original graph. If the number of communities increases, the purity score is more likely to increase as well. Therefore, it is not an ideal evaluation metric to judge whether the community partition is good or bad, especially when the number of communities is unknown.

Similarly to the evaluation in classification tasks, evaluation in community detection also builds up a confusion matrix to measure the pairwise node community relationship under four categories.  A true positive (TP) node pair means two nodes in the same ground-truth community are also assigned to the same detected community. A true negative (TN) node pair means two nodes in different ground-truth communities are also assigned to different detected communities. A false positive (FP) node pair means two nodes in different ground-truth communities are mistakenly assigned to the same detected community. And a false negative (FN) node pair means two nodes in the the same ground-truth community are mistakenly assigned to different detected communities. Using the four types of node pairs, by computing all possible node pairs in the graph  $G$, the following metrics can be retrieved including Precision, Recall, $F_\beta$ and Rand index.

\textbf{Precision} indicates the correctness ratio of all node pairs which are predicted in the same community:
\begin{equation}
f(C,\Omega) = \frac{TP}{TP+FP}
\end{equation}

\textbf{Recall} shows the ratio of same-community node pairs in the ground truth is actually retrieved in the detected communities:
\begin{equation}
f(C,\Omega) = \frac{TP}{TP+FN}
\end{equation}

\textbf{$F_\beta$} is the weighted harmonic mean of Precision and Recall to balance the two metrics with a weighting factor $\beta$:
\begin{equation}
f(C,\Omega) =\frac{(\beta^{2}+1)P\times R}{\beta^{2}(P+R)}
\end{equation}

\textbf{Rand index} considers the ratio of correctly predict node pairs in all the possible pairs:
\begin{equation}
f(C,\Omega) = \frac{TP+TN}{TP+FP+FN+TN}
\end{equation}

\textbf{Normalized mutual information} (NMI) measures the model performance through entropy perspective, which is defined as:
\begin{equation}
f(C,\Omega)  =  \frac{I(C,\Omega)}{[H(C)+H(\Omega)]/2}
\end{equation}

where $I(C,\Omega)$ refers to the mutual information of the predicted community partition $C$ with the ground truth community partition $\Omega$.  Mutual information calculates how much similar the two partitions look like, which is calculated as:
\begin{equation}
I(C,\Omega) = \sum_{k}\sum_{j}\frac{N_{c_{k},\omega_{j}}}{N} log \frac{N_{c_{k},\omega_{j}}}{N_{c_{k}}N_{\omega_{j}}}
\end{equation}

And $H(\cdot)$ is the entropy function, defined as:
\begin{equation}
H(C) = - \sum_{k} \frac{N_{c_k}}{N}log\frac{N_{c_k}}{N}
\end{equation}

NMI is always a number between 0 and 1. However, it can't correctly handle the case where detected community size is hugely different with ground truth community size. One of its variant, rNMI \cite{zhang2015evaluating}, solves this problem by comparing the NMI of two community partitions with a random community partition.

\subsection{Summary}
In this section, evaluation works are summarized into two tracks including comparative studies and metric introduction. The comparative studies always run empirical experiments on different graphs using various community detection models. These researches aim to find the best parameter settings for each model and explore the correlation between models and the intrinsic graph structures. As all mentioned comparative studies in this section are the best known works published in the recent decade, it gives a solid and authentic insight for the model understanding and selection.

For metric introduction, I introduce a set of structure metrics to reveal community self-characteristics. Several evaluation metrics are also introduced to assess the model performance by comparing the detected communities with the ground truth. The structure metrics such as density and modularity are classic ones and have been widely used for years in numerous research works.  While some other structure metrics such as supervise and permanence are just recently designed which are still need to be testified in future works. As for those mentioned evaluation metrics, they are derived from classification evaluation metrics, which are all fundamental ones for all types of evaluation tasks.


\documentclass[t]{beamer}
\usetheme{Copenhagen}
\setbeamertemplate{headline}{} % remove toc from headers
\beamertemplatenavigationsymbolsempty

\usepackage{amsmath, tikz, bm, tkz-euclide,pgfplots}
\pgfplotsset{compat = 1.16}
\usetkzobj{all}

\title{Compound Inequalities}
\author{}
\date{}

\AtBeginSection[]
{
  \begin{frame}
    \frametitle{Objectives}
    \tableofcontents[currentsection]
  \end{frame}
}

\begin{document}

\begin{frame}
\maketitle
\end{frame}

\section{Solve and graph solutions of compound inequalities}

\begin{frame}{Compound Inequalities -- AND}
	Compound inequalities involve inequalities connected by either the word {\color{blue}\textbf{and}} or the word {\color{red}\textbf{or}}.	\newline\\	\pause
	A number is a solution to a compound inequality that involves the word \emph{and} if it is a solution to \textbf{both} inequalities.
\end{frame}

\begin{frame}{Visual Interpretation of AND}
	\begin{center}
		\[x \geq -1 \textbf{ and } x < 2\]
		\begin{tikzpicture}
			\draw[<->](-3,0)--(3,0);
			\foreach \x in {-2,-1,...,2}
			\draw(\x,0.15)--(\x,-0.15) node [below] {$\x$};
			\draw[color=blue,fill=blue] (-1,0) circle [radius = 2.5pt];
			\draw[color=blue, ultra thick] (-1,0)--(2,0);
			\draw[color=blue, fill=white] (2,0) circle [radius=2.5pt];
		\end{tikzpicture}	\newline\\
		If you graph each individual inequality, the solution is the part of the number where they overlap.
	\end{center}
\end{frame}

\begin{frame}{Example 1}
	Solve and graph each.	\newline\\
	(a) \quad $-3 < 2x + 1 \leq 3$
	\begin{align*}
		\onslide<2->{-3 &< 2x+1 & 2x+1 &\leq 3} \\[6pt]
		\onslide<3->{-4 &< 2x   & 2x   &\leq 2} \\[6pt]
		\onslide<4->{2x &> -4   & 2x   &\leq 2} \\[6pt]
		\onslide<5->{x  &> -2   &  x   &\leq 1}
	\end{align*}
	\begin{center}
		\onslide<6->{
			\begin{tikzpicture}
				\draw[<->] (-2.5,0) -- (2.5,0);
				\draw (-1.75,0.15) -- (-1.75,-0.15) node [below] {$-2$};
				\draw (1.75,0.15) -- (1.75,-0.15) node [below] {1};
				\onslide<7->{\draw[->,red,very thick] (-1.75,0.5) -- (2.5,0.5);
						\draw[color=red,fill=white] (-1.75,0.5) circle [radius=2.5pt];}
				\onslide<8->{\draw[->,blue,very thick] (1.75,-0.75) -- (-2.5,-0.75);
						\draw[color=blue,fill=blue] (1.75,-0.75) circle [radius=2.5pt];}
				\onslide<9->{\draw[ultra thick, violet, shorten >=2.5pt, shorten <=2.5pt] (-1.75,0) -- (1.75,0);
						\draw[color=red,fill=white] (-1.75,0) circle [radius=2.5pt];
						\draw[color=blue,fill=blue] (1.75,0) circle [radius=2.5pt];}
						}
			\end{tikzpicture}
	\end{center}
\end{frame}

\begin{frame}{Example 1}
	(b) \quad $1 \leq 2x + 3 < 11$
	\begin{align*}
		\onslide<2->{1 &\leq 2x+3 & 2x+3 &< 11} \\[6pt]
		\onslide<3->{-2 &\leq 2x & 2x &< 8} \\[6pt]
		\onslide<4->{2x &\geq -2 & 2x &< 8} \\[6pt]
		\onslide<5->{x &\geq -1  &  x &< 4}
	\end{align*}
	\begin{center}
		\onslide<6->{
			\begin{tikzpicture}
				\draw[<->] (-2.5,0) -- (2.5,0);
				\draw (-1.75,0.15) -- (-1.75,-0.15) node [below] {$-1$};
				\draw (1.75,0.15) -- (1.75,-0.15) node [below] {$4$};
				\onslide<7->{\draw[->,red,very thick] (-1.75,0.5) -- (2.5,0.5);
						\draw[color=red,fill=red] (-1.75,0.5) circle [radius=2.5pt];}
				\onslide<8->{\draw[->,blue,very thick] (1.75,-0.75) -- (-2.5,-0.75);
						\draw[color=blue,fill=white] (1.75,-0.75) circle [radius=2.5pt];}
				\onslide<9->{\draw[ultra thick, violet, shorten >=2.5pt, shorten <=2.5pt] (-1.75,0) -- (1.75,0);
						\draw[color=red,fill=red] (-1.75,0) circle [radius = 2.5pt];
						\draw[color=blue,fill=white] (1.75,0) circle [radius=2.5pt];}
						}
			\end{tikzpicture}
	\end{center}
\end{frame}

\begin{frame}{Example 1}
	(c) \quad $2x-7>3$ and $5x-4\leq 6$
	\begin{align*}
		\onslide<2->{2x-7 &> 3 & 5x-4 &\leq 6} \\[6pt]
		\onslide<3->{2x &> 10 & 5x &\leq 10} \\[6pt]
		\onslide<4->{x &> 5 & x &\leq 2}
	\end{align*}
	\begin{center}
		\onslide<5->{
			\begin{tikzpicture}
				\draw[<->] (-2.5,0) -- (2.5,0);
				\draw (-1.75,0.15)--(-1.75,-0.15) node [below] {2};
				\draw (1.75,0.15)--(1.75,-0.15) node [below] {5};
				\onslide<6->{\draw[color=red,->,very thick] (1.75,0.5) -- (2.5,0.5);
						\draw[color=red,fill=white] (1.75,0.5) circle [radius=2.5pt];}
				\onslide<7->{\draw[color=blue,->,very thick] (-1.75,-0.75) -- (-2.5,-0.75);
						\draw[color=blue,fill=blue] (-1.75,-0.75) circle [radius=2.5pt];}
						\end{tikzpicture}
						}
	\end{center}

	\onslide<8->{Do not overlap \quad}	\onslide<9->{\textbf{No solution}}
	\end{frame}

\begin{frame}{Compound Inequalities -- OR}
	The word \alert{or} indicates the solution be in {\color{blue}\textbf{either}} inequality (or both).	\newline\\
	\[x > 2 \text{ or } x \leq -1\]
	\begin{center}
		\begin{tikzpicture}
			\draw[<->] (-3.5,0) -- (3.5,0);
			\foreach \x in {-2,-1,...,3}
			\draw (\x,0.15) -- (\x,-0.15) node [below] {$\x$};
			\draw[->,blue,very thick] (-1,0) -- (-3.5,0);
			\draw[color=blue,fill=blue] (-1,0) circle [radius=2.5pt];
			\draw[->,red,very thick] (2,0) -- (3.5,0);
			\draw[color=red,fill=white] (2,0) circle [radius=2.5pt];
		\end{tikzpicture}
	\end{center}
\end{frame}

\begin{frame}{Example 2}
	Solve and graph each.	\newline\\
	(a) \quad $2x-3<7$ or $35-4x \leq 3$
	\begin{align*}
		\onslide<2->{2x-3 &< 7 & 35-4x &\leq 3} \\[6pt]
		\onslide<3->{2x   &<10 &   -4x &\leq -32} \\[6pt]
		\onslide<4->{x    &<5  &     x &\geq 8}
	\end{align*}
	\begin{center}
		\onslide<5->{
			\begin{tikzpicture}
				\draw[<->] (-2.5,0) -- (2.5,0);
				\draw (-1.5,0.15) -- (-1.5,-0.15) node [below] {5};
				\draw (1.5,0.15) -- (1.5,-0.15) node [below] {8};
				\onslide<6->{\draw[->,red,very thick] (-1.5,0)--(-2.5,0);
						\draw[color=red,fill=white] (-1.5,0) circle [radius=2.5pt];}
				\onslide<7->{\draw[->,blue,very thick] (1.5,0) -- (2.5,0);
						\draw[color=blue,fill=blue] (1.5,0) circle [radius=2.5pt];}
			\end{tikzpicture}}
	\end{center}
\end{frame}

\begin{frame}{Example 2}
	(b) \quad $3x-5\leq 13$ or $5x+2>-3$
	\begin{align*}
		\onslide<2->{3x-5 &\leq 13 & 5x+2 &> -3} \\[6pt]
		\onslide<3->{3x &\leq 18 & 5x &> -5} \\[6pt]
		\onslide<4->{x &\leq 6 & x &> -1}
	\end{align*}
	\begin{center}
		\onslide<5->{
			\begin{tikzpicture}
				\draw[<->] (-2.5,0) -- (2.5,0);
				\draw (-1.5,0.15) -- (-1.5,-0.15) node [below] {$-1$};
				\draw (1.5,0.15) -- (1.5,-0.15) node [below] {6};
				\onslide<6->{\draw[->,red,very thick] (1.5,0) -- (-2.5,0);
						\draw[color=red,fill=red] (1.5,0) circle [radius=2.5pt];}
				\onslide<7->{\draw[->,blue,very thick] (-1.5,0) -- (2.5,0);
						\draw[color=blue] (-1.5,0) circle [radius=2.5pt];}
			\end{tikzpicture}}	\newline\\
			\onslide<8->{All Real Numbers}
	\end{center}
\end{frame}

\begin{frame}{Example 2}
	(c) \quad $2x + 5 \leq 3$ or $2x + 3 < 3$
	\begin{align*}
		\onslide<2->{2x+5 &\leq 3 & 2x+3 &< 3} \\[6pt]
		\onslide<3->{2x &\leq -2 & 2x &< 0} \\[6pt]
		\onslide<4->{x &\leq -1 & x &< 0}
	\end{align*}
	\begin{center}
		\onslide<5->{
			\begin{tikzpicture}
				\draw[<->] (-2.5,0) -- (2.5,0);
				\draw (-1.5,0.15) -- (-1.5,-0.15) node [below] {$-1$};
				\draw (1.5, 0.15) -- (1.5, -0.15) node [below] {0};
				\onslide<6->{\draw[->,red,very thick] (-1.5,0) -- (-2.5,0);
						\draw[color=red,fill=red] (-1.5,0) circle [radius=2.5pt];}
				\onslide<7->{\draw[->,blue,very thick] (1.5,0) -- (-2.5,0);
						\draw[color=blue,fill=white] (1.5,0) circle [radius=2.5pt];}
			\end{tikzpicture}}
			\onslide<8->{\[x < 0\]}
	\end{center}
\end{frame}

\end{document}


% Created 2020-09-29 mar 19:00
% Intended LaTeX compiler: pdflatex
\documentclass[presentation,aspectratio=169, usenames, dvipsnames]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{khpreamble}
\usepackage{amssymb}
\usepgfplotslibrary{groupplots}
\usepackage{pgfplotstable}
\newcommand*{\shift}{\operatorname{q}}
\definecolor{ppc}{rgb}{0.1,0.1,0.6}
\definecolor{iic}{rgb}{0.6,0.1,0.1}
\definecolor{ddc}{rgb}{0.1,0.6,0.1}
\usetheme{default}
\author{Kjartan Halvorsen}
\date{2020-09-28}
\title{Process Automation Laboratory - Cascade control and feed forward control}
\hypersetup{
 pdfauthor={Kjartan Halvorsen},
 pdftitle={Process Automation Laboratory - Cascade control and feed forward control},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 26.3 (Org mode 9.3.6)},
 pdflang={English}}
\begin{document}

\maketitle

\section{Context and main idea}
\label{sec:orgf84f686}
\begin{frame}[label={sec:org46648dd}]{The two-tank model with one level sensor}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/two-tanks-shutoff-valve}
\end{center}
\end{frame}

\begin{frame}[label={sec:orgded96b9}]{The two-tank model with two level sensors and one flow sensor}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/two-tanks-2LE-FE}
\end{center}

\alert{Key idea: We can improve the control using more information}
\end{frame}

\section{Cascade control}
\label{sec:orgf103e54}
\begin{frame}[label={sec:org6010b11}]{Cascade control}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/block-diagram-cascade-control.pdf}
\end{center}
\end{frame}

\begin{frame}[label={sec:orga520990}]{Designing the inner loop}
\begin{center}
\includegraphics[width=0.6\linewidth]{../../figures/block-diagram-cascade-control.pdf}
\end{center}
Have model \(G_1(s) = \frac{K_1}{s\tau_1 + 1} = \frac{51}{51s + 1}\). PI controller \[F_1(s) = k_{c} \left( 1 + \frac{1}{\tau_{i}s}\right) = k_{c} \frac{\tau_{i}s + 1}{\tau_{i}s}\]
Characteristic equation
\[ s(s\tau_1 + 1) + k_c \frac{K}{\tau_{i}}(s\tau_{i} + 1) = 0\]
Choose \(\tau_i\) and \(k_c\) to place the poles at any desired location in the LHP.
\end{frame}

\begin{frame}[label={sec:org5bdecd0}]{Exercise}
Have characteristic equation
\[ s(s\tau_1 + 1) + k_c \frac{K}{\tau_{i}}(s\tau_{i} + 1) = 0\]

\alert{Choose \(\tau_i = \tau_1\), simplify the characteristic equation, and then determine \(k_c\) which gives a pole in \(s=-\frac{4}{\tau_1}\).}
\end{frame}

\begin{frame}[label={sec:orga795158}]{Designing the outer loop}
\begin{center}
\includegraphics[width=0.6\linewidth]{../../figures/block-diagram-cascade-control.pdf}
\end{center}
The output of the outer controller \(F_2(s)\) is the desired level in tank 1. If the inner loop is sufficiently fast, we can approximate that the actual level in tank 1 is equal to the desired level.

\begin{center}
\includegraphics[width=0.9\linewidth]{../../figures/block-diagram-cascade-outer-loop}
\end{center}
\end{frame}

\begin{frame}[label={sec:orgda56d20}]{Designing the outer loop, contd}
Alternatively, we can fit a model to the plant and the inner control-loop
\begin{center}
\includegraphics[width=0.9\linewidth]{../../figures/block-diagram-cascade-control-G2}
\end{center}
\end{frame}

\section{Feed forward from the disturbance}
\label{sec:orgd7d4967}

\begin{frame}[label={sec:org1ade855}]{Feed forward from the disturbance}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/block-diagram-ffw-no-ffw}
\end{center}
\end{frame}

\begin{frame}[label={sec:org9655309}]{Feed forward from the disturbance}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/block-diagram-ffw}
\end{center}
\end{frame}

\begin{frame}[label={sec:orgdc7375e}]{Feed forward from the disturbance}
\begin{center}
\includegraphics[width=0.7\linewidth]{../../figures/block-diagram-ffw}
\end{center}

Clearly if
\[ G(s)F_v(s)V(s) = H_v(s)V(s)\]
the effect of the disturbance cancels.
\alert{Activity: Solve for \(F_V(s)\)!}
\end{frame}
\end{document}


\documentclass[12pt,a4paper]{amsart}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amscd}
\usepackage[utf8]{inputenc}
\usepackage{t1enc}
\usepackage[mathscr]{eucal}
\usepackage{indentfirst}
\usepackage{graphicx}
\usepackage{graphics}
\usepackage{pict2e}
\usepackage{epic}
\numberwithin{equation}{section}
\usepackage[margin=2.9cm]{geometry}
\usepackage{epstopdf}
\usepackage[foot]{amsaddr}

 \def\numset#1{{\\mathbb #1}}

\theoremstyle{plain}
\newtheorem{Th}{Theorem}[section]
\newtheorem{Lemma}[Th]{Lemma}
\newtheorem{Cor}[Th]{Corollary}
\newtheorem{Prop}[Th]{Proposition}

 \theoremstyle{definition}
\newtheorem{Def}[Th]{Definition}
\newtheorem{Conj}[Th]{Conjecture}
\newtheorem{Rem}[Th]{Remark}
\newtheorem{?}[Th]{Problem}
\newtheorem{Ex}[Th]{Example}

\newcommand{\im}{\operatorname{im}}
\newcommand{\Hom}{{\rm{Hom}}}
\newcommand{\diam}{{\rm{diam}}}
\newcommand{\ovl}{\overline}
%\newcommand{\M}{\mathbb{M}}

\begin{document}

\title{Conditional auctions as an equivalent problem to combinatorial ones}

\author[A. Acebrón]{Albert Acebrón}
\author[M. Graells]{Marc Graells}

\address{Universitat Autònoma de Barcelona, Barcelona, Spain}

\email{albert.acebron@e-campus.uab.cat, marc.graells@e-campus.uab.cat}

\subjclass[2019]{}

\keywords{combinatorial auction, auction, winner determination problem}

\begin{abstract} We devise a new auction system based on conditional bids, prove that it is equivalent to combinatorial auctions and give a polynomial time algorithm that solves the winner determination problem given some constraints in the bids.
\end{abstract}

\maketitle

\section{Introduction} One can find many useful sources for creating mathematical paper with latex (or tex, miktex...), see for instance \cite{wilk,wiki}. The main aim of this paper is to show a latex file as a source. It is not the aim of this paper to show how a mathematical paper looks like. By now you have seen many of them, and if you want to see more then I suggest visiting the website arxiv.org, which is a huge database of mathematical papers. After checking a few papers you will get a feeling what kind of informations the abstract or introduction contains, or how the authors organize their papers, in particular where they put their historical remarks, proofs, lemmas etc.

Now we present our main theorem.

\begin{Th} \label{main} Every beginning is hard. But once you are familiar with latex, you won't use Microsoft Words again for creating mathematical papers.
\end{Th}

\section{Proof of Theorem~\ref{main}}

In this section we present the proof of Theorem~\ref{main}.

\begin{proof} We leave the proof of the first statement to the Reader. Now we prove the second statement. The main advantage of latex is that it is much easier to create mathematical formulas and they look much better than in Microsoft Word.
For instance,
$$\frac{x^2+1}{\sqrt{u+v}}\in \mathbb{R}.$$
On the other, you can do everything which can be done with Microsoft Word. You can put pictures in it, but generally you can only indicate its place, the actual place of the figure depends on many things. For instance, in the latex file there is a picture following this sentence, but since there is no place here, the program will take it to the next page, and some texts precede it. If you have problem with some figure, don't hesitate to ask me.

\begin{figure}[h!]
%\scalebox{.45}{\includegraphics{hexagonal_lattice.eps}}
\end{figure}

From time to time, you will need some commands. Practically, I use at most 20 commands regularly, and sometimes I need tricky command which I find on the internet. I emphasize again that if you have any question, feel free to ask me.

\end{proof}

\begin{thebibliography}{99}

\bibitem{wilk} David R. Wilkins: \textit{Getting started with Latex}, \begin{verbatim} http://www.maths.tcd.ie/~dwilkins/LaTeXPrimer/
\end{verbatim}

\bibitem{wiki} \begin{verbatim} http://en.wikibooks.org/wiki/LaTeX \end{verbatim}

\end{thebibliography}

\end{document}


\chapter{Lecture 11 May 25th 2018}%
\label{chp:lecture_11_may_25th_2018}
% chapter lecture_11_may_25th_2018

The following theorem is useful for A2. The proof is not provided in this lecture, but expect the corollary to be restated and proven in a later lecture.

\begin{crlynonum}
  Let $G$ be a finite group and $H, K \triangleleft G$, $H \cap K = \{1\}$ and $\abs{H} \abs{K} = \abs{G}$. Then $G \cong H \times K$.
\end{crlynonum}

\section{Normal Subgroup (Continued 2)}%
\label{sec:normal_subgroup_continued_2}
% section normal_subgroup_continued_2

\subsection{Normal Subgroup (Continued)}%
\label{sub:normal_subgroup_continued}
% subsection normal_subgroup_continued

\begin{note}[Recall]
  Recall the definition of a normal subgroup as in \cref{defn:normal_subgroup}. Let $H$ be a subgroup of $G$. If $gH = Hg$ for all $g \in G$, then $H \triangleleft G$.
\end{note}

\begin{propo}[Normality Test]
\index{Normality Test}
\label{propo:normality_test}
  Let $H$ be a subgroup of a group $G$. The following are equivalent:
  \marginnote{
    \begin{note}
  Note that item 3 is indeed a stronger statement that item 2. But since the statements are equivalent, while using the \hlnoteb{Normality Test}, if we can show that item 2 is true, item 3 is automatically true.
    \end{note}
    }
  \begin{enumerate}
    \item $H \triangleleft G$
    \item $\forall g \in G \enspace gHg^{-1} \subseteq H$
    \item $\forall g \in G \enspace gHg^{-1} = H$
  \end{enumerate}
\end{propo}

\begin{proof}
  $( 1 ) \implies ( 2 )$:
  \begin{align*}
    x \in gHg^{-1} &\implies \exists h \in H \enspace x = ghg^{-1} \\
      &\implies \exists h_1 \in H \enspace gh = h_1 g \qquad \because gh \in gH = Hg \\
      &\implies x = ghg^{-1} = h_1 gg^{-1} = h_1 \in H \\
      &\implies gHg^{-1} \subseteq H
  \end{align*}

  \noindent$( 2 ) \implies ( 3 )$:
  \begin{align*}
    ( 2 ) &\implies \forall g \in G \quad gHg^{-1} \subseteq H \\
      &\implies \exists g^{-1} \in G \quad g^{-1}Hg \subseteq H \\
      &\implies H \subseteq gHg^{-1} \\
      &\overset{(2)}{\implies} gHg^{-1} = H
  \end{align*}

  \noindent$( 3 ) \implies ( 1 )$:
  \begin{align*}
    ( 3 ) &\implies \forall g \in G \quad gHg^{-1} = H \\
      &\implies \forall x \in gH \quad xg^{-1} \in gHg^{-1} = H \\
      &\implies x \in Hg \qquad \because gg^{-1} = 1 \\
      &\implies gH \subseteq Hg
  \end{align*}
  Using a similar argument, we would have $Hg \subseteq Hg$. And so $gH = Hg$ as required.\qed
\end{proof}

\begin{eg}
  Let $G = GL_n(\mathbb{R})$ and $H = SL_n(\mathbb{R})$.\index{General Linear Group}\index{Special Linear Group}\sidenote{Recall \cref{defn:general_linear_group} and \cref{defn:special_linear_group}.} For $A \in G$ and $B \in H$ we have
  \begin{equation*}
    \det ABA^{-1} = \det A \det B \det A^{-1} = \det A (1) \frac{1}{\det A} = 1.
  \end{equation*}
  Thus $\forall A \in G, \, ABA^{-1} \in H$. By \cref{propo:normality_test}, $H \triangleleft G$, i.e. $SL_n(\mathbb{R}) \triangleleft GL_n(\mathbb{R})$.\sidenote{
  \begin{note}
    The normality is true for any field, not just $\mathbb{R}$.
  \end{note}
  }
\end{eg}

\begin{propo}[Subgroup of Index 2 is Normal]
\label{propo:subgroup_of_index_2_is_normal}
  \begin{equation*}
    \forall H \text{ subgroup of } G \, \land \, [G : H] = 2 \implies H \triangleleft G
  \end{equation*}
\end{propo}

\begin{proof}
  Let $a \in G$.
  \begin{align*}
    a \in H &\implies aH = H = Ha \\
    a \notin H &\implies G = H \cup Ha \implies Ha = G \setminus H \quad \because \cref{propo:properties_of_cosets} \\
    a \notin H &\implies G = H \cup aH \implies aH = G \setminus H \quad \because \cref{propo:properties_of_cosets}
  \end{align*}
  That implies that $aH = Ha$ for any $a \in G$. Hence, by \cref{propo:normality_test}, $H \triangleleft G$.\qed
\end{proof}

\begin{eg}
  Let $A_n$ be the \hldefn{Alternating Group} contained by $S_n$.\sidenote{Recall the definition of alternating group from \cref{thm:alternating_group} and $S_n$ from \cref{defn:permutations}} By \cref{propo:subgroup_of_index_2_is_normal}, since $[S_n : A_n] = 2$ because $S_n = A_n \cup O_n$ and $O_n$ is a coset of $A_n$, we have that
  \begin{equation*}
    A_n \triangleleft S_n.
  \end{equation*}
\end{eg}

\begin{eg}
  Let
  \begin{equation*}
    D_{2n} = \{1, a, a^2, ..., a^{n - 1}, b, ba, ba^2, ..., ba^{n - 1} \}
  \end{equation*}
  be the \hldefn{Dihedral Group} of order $2n$. Since $[D_{2n} : \lra{a}] = 2$,\sidenote{The coset of $\lra{a}$ is $b\lra{a}$.} we have that
  \begin{equation*}
    \lra{a} \triangleleft D_{2n} \quad \because \cref{propo:normality_test}.
  \end{equation*}
\end{eg}

\newthought{Let} $H$ and $K$ be subgroups of a group $G$. Recall an earlier discussion: $H \cap K$ is the largest subgroup contained in both $H$ and $K$.

What is the ``smallest'' subgroup that contains both $H$ and $K$? Since $H \cap K$ is the largest, it makes sense to think about $H \cup K$. However,
\begin{equation*}
  H \cup K \text{ is a subgroup of } G \iff H \subseteq K \veebar K \subseteq H
\end{equation*}
While we know that $H \cup K$ can indeed be such a subgroup, the price of the restriction is too high, since it is overly restrictive.

A more ``useful'' construction turns out to be the \hlnoteb{product} of the subgroups.

\begin{defn}[Product of Groups]\index{Product of Groups}
\label{defn:product_of_groups}
  \begin{equation*}
    HK := \{ hk \, : \, h \in H, \, k \in K \}
  \end{equation*}
\end{defn}

However, $HK$ is not necessarily a subgroup. For example, for $h_1 k_1, h_2 k_2 \in HK$, it is not necessary that $h_1 k_1 h_2 k_2 \in HK$, since $k_1 h_2$ is not necessarily equal to $h_2 k_1$.

\begin{lemma}[Product of Groups as a Subgroup]
\label{lemma:product_of_groups_as_a_subgroup}
  Let $H$ and $K$ be subgroups of $G$. The following are equivalent:
  \begin{enumerate}
    \item $HK$ is a subgroup of $G$
    \item $HK = KH$ \sidenote{If one of $H$ or $K$ is normal, then the lemma immediately kicks in.}
    \item $KH$ is a subgroup of $G$
  \end{enumerate}
\end{lemma}

\begin{proof}
  It suffices to prove $(1) \iff (2)$, since $(1) \iff (3)$ simply through exchanging $H$ and $K$.

  \noindent $(1) \implies (2)$: Let $kh \in KH$ such that $k \in K$ and $h \in H$. Their inverses are $k^{-1} \in K$ and $h^{-1} \in H$, since $K$ and $H$ are groups. Note that
  \begin{equation*}
    kh = (h^{-1} k^{-1})^{-1} \in HK \quad \because HK \text{ is a subgroup of } G.
  \end{equation*}
  Therefore $kh \in HK$, which implies $KH \subseteq HK$. By a similar argument, we can arrive at $HK \subseteq KH$ and so $HK = KH$.

  \noindent $(2) \implies (1)$: Note that $1 = 1 \cdot 1 \in HK$. $\forall hk \in HK$, $(hk)^{-1} = k^{-1} h^{-1} \in KH = HK$. For $h_1 k_1, h_2 k_2 \in HK$, note that $k_1 h_2 \in KH = HK$, so there exists $h k \in HK$ such that $k_1 h_2 = hk$. Therefore,
  \begin{equation*}
    h_1 k_1 h_2 k_2 = h_1 h k k_2 \in HK.
  \end{equation*}
  By the \hlnoteb{Subgroup Test}, $HK$ is a subgroup of $G$.\qed
\end{proof}

\begin{propo}[Product of Normal Subgroups is Normal]
\label{propo:product_of_normal_subgroups_is_normal}
  Let $H$ and $K$ be subgroups of $G$.
  \begin{enumerate}
    \item $H \triangleleft G \, \lor \, K \triangleleft G \implies HK = KH$ is a subgroup of $G$
    \item $H, K \triangleleft G \implies HK = KH \triangleleft G$
  \end{enumerate}
\end{propo}

\begin{proof}
  \begin{enumerate}
    \item Without loss of generality, suppose $H \triangleleft G$. Then
      \begin{equation}\label{eq:product_of_normal_subgroups_is_normal_pf_1}
        HK = \bigcup_{k \in K} Hk = \bigcup_{k \in K} kH = KH
      \end{equation}
      By \cref{lemma:product_of_groups_as_a_subgroup}, $HK = KH$ is a subgroup of $G$.

    \item Suppose $H, K \triangleleft G$. Then
      \begin{align*}
        \forall g \in G \enspace \forall hk \in HK \quad g^{-1}( hk )g = (g^{-1} hg)(g^{-1} k g) \in HK
      \end{align*}
      Thus $gHKg^{-1} \subseteq HK$. Thus by \cref{propo:normality_test}, we have that $HK \triangleleft G$.
  \end{enumerate}\qed
\end{proof}

\begin{note}
  Note that \cref{eq:product_of_normal_subgroups_is_normal_pf_1} is a weaker statement than the regular normality that we have defined, since it only requires all elements of $K$ to work instead of the entire $G$.
\end{note}

With that, we define the following notion:

\begin{defn}[Normalizer]\index{Normalizer}
\label{defn:normalizer}
  Let $H$ be a subgroup of $G$. The \hlnoteb{normalizer of $H$}, denoted by $N_G(H)$, is defined to be
  \begin{equation*}
    N_G(H) := \{ g \in G \, : \, gH = Hg \}
  \end{equation*}
\end{defn}

\begin{note}
  By the above definition, we immediately see that $H \triangleleft G \iff N_G(H) = G$ by \cref{eq:product_of_normal_subgroups_is_normal_pf_1}. Observe that since we only needed $kH = Hk$ in \cref{eq:product_of_normal_subgroups_is_normal_pf_1} for all $k \in K$, we have that $k \in N_G(H)$.
\end{note}

\begin{crly}\label{crly:subsets_of_normalizers_form_a_product_that_is_a_subgroup}
  Let $H$ and $K$ be subgroups of a group $G$.
  \begin{equation*}
    K \subseteq N_G(H) \, \lor \, H \subseteq N_G(K) \implies HK = KH \text{ is a subgroup of } G
  \end{equation*}
\end{crly}

The proof of \cref{crly:subsets_of_normalizers_form_a_product_that_is_a_subgroup} is embedded in the proof of \cref{propo:product_of_normal_subgroups_is_normal} while using the definition of a \hlnoteb{normalizer}.

% subsection normal_subgroup_continued (end)

% section normal_subgroup_continued_2 (end)

% chapter lecture_11_may_25th_2018 (end)