% -----------------------------*- LaTeX -*------------------------------
\documentclass[12pt]{report}
\usepackage{scribe_hgen486}
\usepackage{bbm}

\begin{document}

\scribe{Justin Hsu}		% required
\lecturenumber{4}			% required, must be a number
\lecturedate{January 14}		% required, omit year
\lecturer{Matthew Stephens}

\maketitle

% please leave this comment
\framebox[.95\textwidth]{\parbox{.93\textwidth}{ {{\bf Note:}} These
lecture notes are still rough, and have only have been mildly
proofread.  }}
\vspace*{.1in}

% feel free to delete content below this line
% ----------------------------------------------------------------------
\section{Likelihood Analysis}
Consider the set of data of 100 tusks, 40 of which have the "1" allele, 60 with the "0" allele
Then the data has the likelihood function \begin{equation} L(q) = P(Data|M_q)\end{equation}. We can write this as \begin{equation} L(q) = q^{40}(1-q)^{60}\end{equation}

Now consider the log of the likelihood function: \begin{equation} \ell(q) = 40log(q) + 60 log(1-q)\end{equation}

We can estimate q by finding the value of q that maximizes $L(q)$. This is known as the Maximum Likelihood estimator (mle), which we denote as $\hat{q}$. A useful feature is that the value that maximizes the likelihood function also maximizes the log likelihood function.
\newcommand{\argmax}{\arg\!\max}
\begin{equation}
\begin{aligned} \hat{q} = \argmax_q L(q) \\
= \argmax_q \ell(q)
\end{aligned}
 \end{equation}

This is useful because it is sometimes easier to find the maximum of $\ell(q)$.

Returning to the elephant tusk example, we find the maximum of the likelihood function by taking the derivative of the log likelihood.
\begin{equation}
\begin{aligned}
\ell'(q) = \frac{40}{q} - \frac{60}{1-q}\\
0 = \frac{40}{q} - \frac{60}{1-q} \\
\hat{q} = \frac{40}{100}
\end{aligned}
\end{equation}

We can extend this generally so that given two populations $n_1$ and $n_0$, we have a likelihood function \begin{equation}
L(q) = q^{n_1}(1-q)^{n_0}
\end{equation}
and a log likelihood function \begin{equation}
\ell(q) = n_1log(q) - n_0log(1-q)
\end{equation}
Then the maximum likelihood estimate will have the form \begin{equation}
\hat{q} = \frac{n_1}{n_1+n_0}
\end{equation}

This is the maximum likelihood of the Binomial Distribution.

\section{Mixture Models}
We now move on to mixture models, which our models that consist of a mixture of two or more distributions. As an example, consider the heights of all humans of these worlds. What would be the distribution of these heights. We could assume that they are normally distributed, but what if the male heights come from a different distribution than the female heights?

Suppose we have \begin{equation}
\begin{aligned}
male height\sim N(\mu_m,\sigma^2_m)\\
female height \sim N(\mu_f,\sigma^2_f)
\end{aligned}
\end{equation} and suppose the population is 50\% male and 50\% female.

Let $X$ be the height of a randomly chosen person. What would be the density function for $X$?

If $X$ was discrete, then \begin{equation}
Pr(X=x) = Pr(X = x|male)Pr(male) + Pr(X=x|female)Pr(female)
\end{equation}

The continuous analogue would be:
\begin{equation}
\begin{aligned}
f_x(x) = \frac{1}{2}f_{x|male}(x) + \frac{1}{2}f_{x|female}(x) \\
= \frac{1}{2}N(X;\mu_m,\sigma^2_m) +  \frac{1}{2}N(X;\mu_f,\sigma^2_f)
\end{aligned}
\end{equation}

We call the probabilities $Pr(male) = \frac{1}{2}$ and $Pr(female) = \frac{1}{2}$ the "mixture proportions". We call $f_{x|male}(x)$ and $f_{x|female}(x)$ the "component densities".

Returning to our elephant tusk example, suppose we have data $X = (X_1,X_2,...,X_n)$ on $n$ tusks, and that we know the allele frequencies.

Let the proportion of elephants that are Savannah be $\Pi_S$.

Let $Z_i \in \lbrace S,F\rbrace$ denote whether tusk $i$ came from either a Savannah or Forest elephant. We call $\lbrace S,F\rbrace$ the "component labels".

Then we have the mixture model \begin{equation}
\begin{aligned}
P(X_i = x_i|\Pi_S) = Pr(Z_i=S)Pr(X_i = x_i|Z_i=S) + Pr(Z_i = F)Pr(X_i=x_i|Z_i = F) \\
 = \Pi_SPr(X_i = x_i|Z_i=S) + (1-\Pi_S)Pr(X_i=x_i|Z_i = F)
\end{aligned}
\end{equation}

More generally \begin{equation}
Pr{X_i = x_i} = \sum_{k}\Pi_kPr(X_i = x_i|Z_i = k)
\end{equation}
where $\Pi_1,...\Pi_k$ are nonnegative and sum to 1.

The likelihood function of this mixture model is
\begin{equation}
\begin{aligned}
L(\Pi_S) = P(X|\Pi_S)\\=\prod_{i=1}^{n}P(X_i=x_i|\Pi_S)
\end{aligned}
\end{equation}

When we take the log of this likelihood function, we get \begin{equation}
\begin{aligned}
\ell(\Pi_S) = \sum_{i=1}^{n}log(Pr(X_i=x_i|\Pi_S))\\
= \sum_{i=1}^{n}log[\Pi_SP(X_i=x_i|Z_i=S)+(1-\Pi_S)P...]
\end{aligned}
\end{equation}

Unlike the example with the binomial distribution, this log likelihood is difficult to differentiate, so to find the maximum, we must rely on numerical methods.

\section{EM Algorithm}
The Expectation Maximization (EM) Algorithm is a method for finding maximum likelihood estimates for a model. The key idea behind the EM algorithm is "data augmentation". It is data which we do not have but wish we would have. Suppose our data is $X$, then the augmented data would be $(X,Z)$, where $Z$ is the "missing data".

Let $L(\theta) = P(X|\theta)$ be the "marginal likelihood"/"observed likelihood". The "complete likelihood" is $L_{comp}(\theta) = P(X,Z|\theta)$.

The steps of the EM algorithm are as follows:
\begin{enumerate}

\item Choose some $\theta_0$

\item E step: Form the "expected" complete log likelihood by taking the expectation over Z. In other words find \begin{equation}
Q(\theta,\theta_0) = E_{Z|X,\theta_0}[\ell_{comp}(\theta;Z,X)]
\end{equation}

\item M step: Choose the value of $\theta$ which maximizes $Q(\theta,theta_0)$.

\item The maximizes theta is your new $\theta_0$. Repeat the E and M steps until $\ell(\theta)$ does not change very much.
\end{enumerate}

The advantage of the EM algorith is that the likelihood will always increase with each iteration.

Sometimes the algorithm will converge to a local optimum rather than a global optimum. In practice the algorithm is run multiple times.

Returning to elephant tusk mixture model, which has a complete likelihood \begin{equation}
\begin{aligned}
L(\Pi_S) = P(X,Z|\Pi_S) = \prod_{i=1}^{n}P(X_i,Z_i|\Pi_S) \\
 = \prod_{i=1}^{n}P(Z_i|\Pi_S)
 \propto \prod_{i=1}^{n}\Pi_S^{\mathbbm{1}_{Z_i=S}}(1-\Pi_S)^{\mathbbm{1}_{Z_i=F}}
\end{aligned}
\end{equation}

$\mathbbm{1}$ stands for the indicator function, which is 1 for the given event and 0 otherwise.

Taking the log of this expression, we get
\begin{equation}
log(\prod_{i=1}^{n}P(Z_i|\Pi_S)) = \underset{ constant}{C} + \sum_i \mathbbm{1}(Z_i=S)log(\Pi_S) + \sum_i \mathbbm{1}(Z_i=F)log(1-\Pi_S)
\end{equation}

If we take the expectation of the sum of indicator functions, we find the probability of that event occurring. We find that the log likelihood above is maximizes at
\begin{equation}
\frac{\sum_i E(\mathbbm{1}(Z_i=S)|X,\theta)}{\sum_i E(...) = n}
\end{equation}

\end{document}


\section{Computational Methods and Software}
\label{sec:software}

The previous section described a series of models
for the system of interest.
%These models are too
%complicated to solve exactly, and must instead be
%instantiated with software to produce a numerical
%result.
This section details the numerical formulation
and simulation of these models. It begins with a
discussion of the numerical discretization of the equations
of interest. The mesh discretization is then described.
Next, the scientific software in which these numerical
models are used is discussed. Finally, the tool
chain and supercomputer systems are briefly introduced.

\subsection{Discretization Scheme}

To numerically solve the Navier-Stokes equations on a computer, a
Galerkin finite element method (FEM) is used, which
requires that the equations in section~\ref{sub_sec:ns_en}
be cast into a weak form. Manipulating these partial differential
equations into a variational formulation is accomplished by multiplying
the equations by appropriate test functions and integrating over the
domain, $\Omega$. The resulting weak problem is to find,
$(u,p,T) \in H^1(\Omega)^3 \times L_2(\Omega) \times H^1(\Omega)$ such
that

%
% http://www.numerik.uni-hd.de/Oberwolfach-Seminar/CFD-Course.pdf
%
\begin{align}
  (\frac{\partial u}{\partial t}, v) + (u \cdot \nabla u,v) + \nu
   (\nabla u, \nabla v)
  -(p,\nabla \cdot u) &= (gT'/T_0,v)
 \label{eqn:ns_weak} \\
 (\nabla \cdot u,q) &= 0
 \label{eqn:cont_weak} \\
 (\frac{\partial T}{\partial t}, w) + (u \cdot \nabla T, w) + (k \, \nabla
 T, \nabla w) &= 0.\label{eqn:en_weak}
\end{align}

$\forall (v,q,w) \in H^1(\Omega)^3 \times L_2(\Omega) \times
H^1(\Omega)$, where $(u,v) = \int_\Omega u \cdot v \, dx$.
Some of the simulations presented here were conducted under
steady conditions, for which the $\frac{\partial}{\partial t}$ terms
vanish. A Galerkin FEM scheme is obtained by posing the weak form in
terms of discrete subspaces of the function spaces specified above
defined using piecewise-polynomial basis functions. All of the
simulations discussed in this work were accomplished using
linear basis functions for both the velocity and pressure.
%Typically, the use of equal order elements for velocity and pressure is
%ruled out in the standard Galerkin FEM formulation by the Babuska-Brezzi
%condition\cite{bb-cond}.
The scheme is
stable with equal-order elements for velocity and pressure due to the
introduction of streamline upwind/Petrov-Galerkin (SUPG) stabilization
terms as first described by Hughes\cite{Hughes198685,supg} and extended
to natural convection as in Becker and Braack\cite{Becker2002428}. The
stabilization terms add artificial dissipation that approaches zero as the
residual converges. This scheme is ``consistent'' because the
underlying order of convergence of the numerical method is not
affected\cite{hughes2000finite}.

This stabilization is accomplished by introducing an additional term,
$\langle Lc,S\phi \rangle_\tau$, to the weak form defined in Equations
\ref{eqn:ns_weak}-\ref{eqn:en_weak}. Here $L()$ is the operator for the PDEs
in \ref{sub_sec:ns_en}, S is a stabilization operator
which is chosen to be the negative adjoint of the differential terms of
$L()$, and $c$ and $\phi$ are state and test functions, i.e.
$ c= (u,p,T)$, and $\phi = ( v,w,q )$.
The angle brackets $\langle \cdot,\cdot \rangle$ signify integration of
the element interiors for each of the K elements, that is:
\begin{equation}
 \langle u,v \rangle_\tau = \sum_K \tau_K(u,v)_K.
\end{equation}
This results in three stabilization parameters, $\tau_P, \tau_v, \tau_T$
which are selected as proposed by Becker and Braack.
A full derivation of the weak form and stabilization terms will be
provided in an appendix of the full thesis.

The system of ODEs are discretized in time using the
backward Euler method\cite{moin2010fundamentals}. The time interval
$(0,T)$ is sliced into $N_t$ steps of
uniform temporal length,  $\Delta t$, where $n = 0,\dots,N_t$.
This has the form,
\begin{equation}
 y_{n+1} = y_n + \Delta t \, f(y_{n+1},t_{n+1}).
\end{equation}
As $f$ is non-linear, a Newton-Raphson method is used to solve the
resulting implicit nonlinear problem. While an iterative method is
significantly more computationally expensive per timestep than a similar
explicit method, the method was selected due to its unconditional
stability and ease of statistical sampling for a uniform timestep.

%
% gave not completely described numerical methods
% for instance, have not indicated the stabilization schemes
% do not need complete equations, but should permit someone to access
% the literature and construct precisely the numerical formulations used
%

\subsection{Mesh Discretization}

%
% what about mesh...
%
The domain's described in subsection~\ref{sec:bc} are
consistently discretized. The domain extents
are scaled by system diameter but the same number of grid points are used
for every simulation. Thus, while the ratio of the domain length to system
diameter remains fixed, the grid spacing increases proportionally with
domain length.

The mesh has a uniform spacing in the lateral directions, except for a
single refinement in the region of the vanes. Typically, the grid is
roughly one hundred points in the streamwise and spanwise directions
before the refinement. The refinement halves the spacing (doubles the
number of points) in all three
coordinate directions, \{x,y,z\} in this region. The refinement is made
from the ground to 1.5 times the height of the vanes and cone.

The mesh is non-uniform in height to
resolve the boundary layer. This is accomplished by redistributing a
mesh uniform in height, z, according to,
\begin{equation}
 z = \begin{cases} C_1(z-L_z)+L_z,& \text{if } z \geq z_\delta\\
      C_2 \text{ exp}(C_3 z - 1),                 & \text{otherwise}
     \end{cases}
\end{equation}

where $z_\delta$ is the chosen height of the boundary layer mesh, and
$C_1-C_3$ are scaling coefficients. This gives the mesh an exponentially
varying character, with the coefficients chosen to ensure ten or more
points in the boundary layer, isotropic spacing in cells outside of
it, and smooth blending between these two regimes. Each boundary layer
spacing was tested against a finer spacing to ensure that the results
were not sensitive to the choice of spacing. A horizontal slice though a
representative domain is shown in Figure \ref{fig:meshing}. The single
refinement in the region of the vanes is visible, as well as the finer
meshed boundary layer region near the ground.

  \begin{figure}[!htb]
    \begin{center}
     \includegraphics[width = 10 cm]{figs/meshing}
     \caption{Horizontal slide through the domain, to show a
     representative meshing. The single refinement region around the
     vanes is visible, as well as the finer boundary layer mesh near the
     ground.}
     \label{fig:meshing}
    \end{center}
  \end{figure}

Finally, the diffusivities are proportionally
scaled with grid size to ensure that the cell Reynolds number,
\begin{equation}
 \text{Re}_\text{cell} = \frac{\text{max}(\Delta x,\Delta y) u}{\nu_T}
\end{equation}
 is maintained for every simulation, in order to ensure stability.

%% hmin = 0.001
%% hmax = 0.4
%% zb = 2.0
%% hrat = ${/ ${mesh-options/hmax} ${mesh-options/hmin}}
%% zmax = ${mesh-options/domain_x3_max}
%% loghrat = ${= log ${mesh-options/hrat}}
%% c2 = ${/ ${mesh-options/zb} ${- ${mesh-options/hrat} 1}}
%% zetab = ${/ ${mesh-options/zmax} ${+ 1 ${/ ${- ${mesh-options/zmax} ${mesh-options/zb}} ${* ${mesh-options/c2} ${mesh-options/hrat} ${mesh-options/loghrat}}}}}
%% c3 = ${/ ${mesh-options/loghrat} ${mesh-options/zetab}}
%% mesh_nx3 = ${= ceil ${/ ${* ${mesh-options/zmax} ${mesh-options/c2} ${mesh-options/c3}} ${mesh-options/hmin}}}
%% c1 = ${* ${mesh-options/c2} ${mesh-options/c3} ${= exp ${* ${mesh-options/c3} ${mesh-options/zetab}}}}
%% redistribute = '{x}{y}{if(z>${mesh-options/zetab},${mesh-options/c1}*(z-${mesh-options/zmax})+${mesh-options/zmax},${mesh-options/c2}*(exp(${mesh-options/c3}*z)-1))}'

%After operation, solutions are evaluated to ensure that
%the qualitative character of the solution does not change.

\subsection{Software Stack}

The numerical approximations described above had been implemented using
the GRINS library\cite{GRINSpaper} by Bauman and Stogner using the
Libmesh\cite{libMeshPaper} FEM infrastructure. It was designed to
support multiphysics FEM applications, the reusability and extensibility
of mathematical modeling kernels, supporting interfaces to existing
solver and discretization libraries to enable modern solution
strategies, while, at the same time, retaining flexibility to
effectively address a wide range of science or engineering problems.

GRINS provides a platform that enables powerful numerical algorithms
such as adjoint-based AMR, adaptive modeling, sensitivity analysis,
and, eventually, enabling uncertainty quantification. While few of these
capabilities are in use for the present work, they could be useful in
future investigations.

GRINS stands for, ``General Reacting Incompressible Navier-Stokes'',
which roughly encapsulates the physical regimes it was originally
designed to simulate. GRINS is open-source, and available on
\hyperref[www.github.com/grinsfem/grins]{github}. It is released
under LGPL2.1.  GRINS is heavily unit tested, with over 60 tests
available to ensure the reliability of results regardless of install
platform.

%The remainder of this subsection is devoted to
%discussing the underlying libraries used and the description of the
%GRINS framework.
% PETSC\cite{petsc} trilinos\cite{trilinos}

% GRINS also uses the fparser\cite{fparser}
% library to support both parsing and compilation of mathematical
% functions into high
% performance kernels. This capability allows for easy specification of
% boundary conditions, initial conditions, or constitutive equations from an input file.

% Currently, libMesh has been scaled tens of thousands of cores and has
% been run on over 100,000 cores on the BG/Q machine Mira at Argonne National
% Lab\cite{libmesh-scaling}

%In principle, alternative software libraries/frameworks such as
%FEniCS\cite{fenics}, OpenFOAM\cite{openfoam}, etc. would likely be
%capable of simulating this regime.

%
% INCLUDE IN THESIS
%
% \subsection{Solver Options}

% GRINS uses PETSC\cite{petsc} and trilinos\cite{trilinos} for numerical
% linear algebra, such as constructing and using sparse matrices, finding
% the iterative solution of linear systems, and for preconditioning.

% While a variety of solver options have been tested in PETSC, all the
% results shown in this document use GMRES with block Jacobi for
% preconditioning\cite{Saad:2003} for the linear solve.

% This uses the inverse of the diagonal block for that processor for
% preconditioning.

% the preconditioner it's going to use to precondition the linear system
% for the solution of the diagonal block. To approximate this, incomplete
% LU factorization is used.

% ILU(0) factorization

%% (11:41:54 AM) nick: ``-ksp_view -ksp_type gmres -pc_type bjacobi -sub_pc_type ilu -sub_pc_factor_levels 0''
%% (11:42:00 AM) Paul Bauman: OK
%% (11:42:17 AM) Paul Bauman: -pc_type is the preconditioner for the entire linear system
%% (11:42:25 AM) Paul Bauman: You're doing bjacobi = block Jacobi
%% (11:42:28 AM) nick: right
%% (11:42:36 AM) nick: and does anyone have a good reference I can learn this better? i feel as if I cant look this up, for some reason
%% (11:42:39 AM) Paul Bauman: That is just using the inverse of the diagonal block for that processor
%% (11:42:46 AM) Paul Bauman: Now
%% (11:42:55 AM) Paul Bauman: that is a linear solve
%% (11:43:17 AM) Paul Bauman: So you can use all the linear solver technology to solve or approximately that block
%% (11:43:24 AM) Paul Bauman: Hence, -sub_pc_type
%% (11:43:32 AM) hil left the room.
%% (11:43:46 AM) Paul Bauman: That's the preconditioner it's going to use to precondition the linear system for the solution of the diagonal block
%% (11:43:53 AM) Paul Bauman: You're telling it to use incomplete lu
%% (11:44:26 AM) Paul Bauman: Now the -sub_pc_factor_levels options applies to ilu
%% (11:44:28 AM) hil entered the room.
%% (11:44:44 AM) Paul Bauman: The incomplete part of imcomplete LU is about the level of fill you use
%% (11:45:05 AM) Paul Bauman: The more levels of fill you have, the more ``complete'' the incomplete LU will be
%% (11:45:08 AM) Paul Bauman: Does that make sense?
%% (11:45:23 AM) nick: no, that is where you lost me
%% (11:45:39 AM) nick: i dont think i know this level of fill
%% (11:46:17 AM) Paul Bauman: Check out Youssef Saad's book if more curious about the subject
%% (11:46:37 AM) nick: cool thanks
%% (11:46:38 AM) Paul Bauman: Suffice it to say, you heopfully shouldn't ever need to go past 3 or 4 levels of fill
%% (11:47:03 AM) Paul Bauman: Also, if you've got superlu installed with the PETSc, consider using -sub_pc_factor_mat_solver_package superlu
%% (11:47:15 AM) Paul Bauman: That's a *much* faster/better implementation than PETSc's

\subsection{Tool Chain and Simulation Custodianship}

Simulations are performed on the Texas Advanced Computing Center (TACC)
supercomputers Lonestar Four and Stampede. Run durations for transient
cases are typically twelve hours to perform several hundred timesteps.
These runs are submitted to the production queue and are
264-528 processing cores,
or 22-44 nodes on Lonestar (with 12 cores per node), and a similar number
for Stampede. The runs typically have several million degrees of freedom (DoF),
and the local number of DoF per core is maintained at $O(10^4)$. This was
selected due to memory constraints, after a strong scaling
analysis of the performance of the code on these resources, and
after consulting with the software developers.

After a run terminates, several scripts are automatically invoked.
These scripts archive the run (outside of the volatile /scratch
production directories) and simultaneously, label the concluded run with
unique metadata that defines the system environment, the jobs input
files and run definitions, as well as information detailing the
hypothesis or physics the job was intended to investigate. Finally, once
a week a script performs \textbf{rsync} on the entire archived database to
ensure more than single redundancy for the runs.

In other words, the workflow is designed to permit rapid queuing of a
series of runs (in parallel) to investigate a variety of conditions or
scenario parameters. This capability is necessary for the optimization
campaign detailed in \ref{sec:proposed_work}, where running many
concurrent investigations will be required to adequately sample the
configuration space.


\subsection{Implications of axiom schema of specification}

\subsubsection{All finite subsets exist}

Finite subsets. Don't know about infinite subsets

If we can define a subset, by the axiom of specification it exists.

For example if set \(\{a,b,c\}\) exists, we can define a preterite to select any subset of this.

For example we can use define a \(P(x)\) as \(x=a\lor x=b\) to extract the subset \(\{a,b\}\).

If a subset is infinitely large,

\subsubsection{Intersections of finite sets exist}

Can prove exists from this axiom

\subsubsection{If any set exists, the empty set exists}

\(\forall x \forall a \exists s[(P(x)\land x\in a )\leftrightarrow (x\in s)]\)


\section{Exercises in Statistics}\label{S:xsStatistics}
\begin{ExerciseList}
\Exercise What is the sample mean and sample variance of the following dataset:
\[
1, 3, 2, 1, 2, 3, 3
\]
\Answer Apply the formulas for the sample mean, $\overline{X_7}$, and sample variance.
\end{ExerciseList}


%!TEX root = ../TTK26-Summary.tex
\section{Muscle modeling}
The main purpose of a mathematical model is
\begin{itemize}
    \item \emph{comprehension}, the ability to aid in understanding the system, and
    \item \emph{prediction}, the ability to predict dynamics outside of experimental boundaries.
\end{itemize}
Further, a good model should have
\begin{itemize}
    \item \emph{credibility}, that it predicts well, and
    \item \emph{tractability}, that it is simple.
\end{itemize}
In a way, these oppose each other: A very precise model is often also very complex. It is necessary to balance these qualities.

\subsection{Types of muscle models}
Divided first by the level they represent:
\begin{enumerate}
    \item Microscopic (crossbridge/sarcomere) models
        \begin{enumerate}
            \item Conventional cross-bridge models (introduced by Huxley)
            \item Unconventional cross-bridge models (based on different postulates than Huxley's)
        \end{enumerate}
    \item Macroscopic (whole muscle) models
        \begin{enumerate}
            \item Viscoelastic models (consider muscle as viscoelastic material)
            \item Hill-type models (based on Hill, 1938)
            \item Black box models (use system identification methods)
        \end{enumerate}
    \item Fiber models
\end{enumerate}

\subsection{Conventional microscopic models}
Introduced by Huxley, 1957 and improved upon since. Assumes all sarcomeres identical, and macroscopic properties can be calculated through integrals on $n(x,t)$, where $n$ is ...

Huxley's model was extended by hill with more states of the ...

\subsection{Unconventional microscopic models}
Founded on different assumptions.
\paragraph{Bornhorst \& Minardi} Modeling each cross-bridge as linear energy converters.
\paragraph{Iwazumi} No direct binding between myosine and actin, ATP causes hydrolysis.
\paragraph{Tirosh} Hydrodynamic model
\paragraph{Hatze} Assume elastisity in Z-disks and M-lines rather than in the cross-bridges.

\subsection{Macroscopic models}
\paragraph{Viscoelastic} Assume muscle is viscoelastic material. Can be represented by a spring-damper in series with an undamped spring.
\paragraph{Hill-type} Improvement upon viscoelastic. Most used model for dynamic analysis and control.  Consists of a series elastic element, a contractile element, and a parallel elastic element. Made to model contraction under max stimuli over short contraction distances. Has been further developed in many directions.
\begin{equation}
    \dot{L} = C(P) \dot(P) - F(P, P_0)
\end{equation}

\subsection{Black box models}
Based on statistics: Do a bunch of experiments, and fit a model to the data. Many model types can be used
\begin{itemize}
    \item LTI SISO
    \item etc...
\end{itemize}
Simplest form is an LTI SISO system
\begin{equation}
    y(t) = \int G(t-\tau) \dot{x}(\tau) \dif \tau
\end{equation}
Time delay $\tau$ represents phase delay at high frequencies and is sometimes simplified away.

Can also write a 2I1O-model: Muscle force a function of length and activation.

\subsection{Fiber models}
Fibres are assumed non-uniform, as opposed to other models.

\subsection{Distribution-moment models}
Represents the bond restriction function $n(x,t)$ from Huxley model as a Gaussian probability distribution dependent on stiffness, force, and elastic energy.

TTK26 Biomedisinsk instrumentering og regulering/tex/sec-w41-muscle-modeling.tex


\chapter{Optimal Power and Natural Gas Flow}
\label{chap:formulation}

In this chapter, we explain in detail the optimization model behind \mpng{}. We introduce the problem as an interdependent steady-state optimal power and natural flow, where a linear objective function models the overall operational cost, subject to several linear and non-linear constraints coming from both technical and operational conditions.

In practice, the state-variables of a natural gas network change quite slowly in comparison to a power system~\cite{Cui2016}. Then, we consider a window of analysis of one entire day for the gas network. On the other hand, as it is found in most of the simulation tools, the power system is modeled using standard windows of one hour. However, a 24-hour time resolution would introduce too much complexity in terms of computational and mathematical tractability. Therefore, we propose a multi-period window of analysis defined by the user. Figure \ref{fig:periods} shows how the optimization model allows $n$ periods for the power system, whose individual duration is such that $\{t_i\in \mathbb{N}, \; i=1,2,...,n : \sum_i t_i = 24\}$, and one entire day for the natural gas system. The interconnection between systems is always consistent. Namely, each individual period of the power system is precisely linked to the single period of the natural gas network.

\begin{figure}[!ht]
	\centering
	\includegraphics[scale=1.0]{Figures/Time_resolution}
	\caption{Periods considered in \mpng{}.}
	\label{fig:periods}
\end{figure}

\section*{Nomenclature}
\subsection*{Indexes}
\begin{labeling}{Longer label\quad}
\item [$i$, $j$] Gas nodes.
\item [$m$, $n$] Electric nodes (buses).
\item [$o$] Gas pipeline.
\item [$c$] Compressor.
\item [$l$] Transmission line.
\item [$w$] Gas well.
\item [$e$] Power generator.
\item [$ref$] Reference bus.
\item [$r$] Spinning reserve.
\item [$\sigma$] Type of gas load.
\end{labeling}

\subsection*{Parameters}

\begin{labeling}{Longer label\quad}
\item [$\alpha^{i}_{\pi_+}, \alpha^{i}_{\pi _-}$] Penalties for over-pressure and under-pressure at node $i$.
\item [$\alpha_{\gamma}$] Penalties for non-supplied gas.
\item [$\alpha_{\epsilon}$] Penalties for non-supplied electricity.
\item [$C^{w}_{G}$] Gas cost at the well $w$.
\item [$C^{oij}_{O}$] Transport cost of pipeline $o$, from node $i$ to node $j$.
\item [$C^{cij}_{C}$] Compression cost of compressor $c$, from node $i$ to node $j$.
\item [$C^{i}_{S}$] Storage cost at node $i$.
\item [$C^{i}_{S_+}$] Storage outflow price at node $i$.
\item [$C^{i}_{S_-}$] Storage inflow price at node $i$.
\item [$C^{e}_{E}$] Power cost generation (excluding gas cost).
\item [$\eta^{q}_{e}$] Thermal efficiency at generator $e$.
\item [$D_{g}^{i \sigma}$] Gas demand of type $\sigma$ at node $i$.
\item [$D_{e}^{tm}$] Electricity demand in the bus $m$ at time $t$.
\item [$\bar{g}^{w}$, $\underline{g}^{w}$] Gas production limits.
\item [$\overline{\pi}^{i}$, $\underline{\pi}^{i}$] Quadratic pressure limits at node $i$.
\item [$S^{i}_{0}$] Initial stored gas at node $i$.
\item [$\overline{S}^{i}$, $\underline{S}^{i}$] Storage limits at node $i$.
\item [$\kappa^{oij}$] Weymouth constant of pipeline $o$.
\item [$\delta^{oij}$] Threshold for gas flow capacities.
\item [$\beta^{cij}$] Compression ratio of compressor $c$.
\item [$Z^{c}$] Ratio parameter of compressor $c$.
\item [$B^{c}$] Compressor design parameter of compressor $c$.
\item [$x$, $y$, $z$] Gas consumption parameters of gas-driven compressors.
\item [$\overline{f}^{oij}_{g}$] Gas transport capacity of pipeline $o$, from node $i$ to node $j$.
\item [$\overline{f}^{cij}_{g}$] Gas flow capacity of compressor $c$, from node $i$ to node $j$.
\item [$\overline{f}^{i}_{s}$,$\underline{f}^{i}_{s}$] Storage outflow capacities at node $i$.
\item [$\overline{p}_{g}^{e}$, $\underline{p}_{g}^{e}$] Active power generation limits of generator $e$.
\item [$\overline{q}_{g}^{e}$, $\underline{q}_{g}^{e}$] Reactive power generation limits of generator $e$.
\item [$\overline{V}^{tm} \underline{V}^{tm}$] Voltage limits for bus $m$ at time $t$.
\item [$\mathbb{S}^{l}$] Transmission capacity of power line $l$.
%\item [$x^{mn}_{l}$] Reactance of line $l$, from bus $m$ to bus $n$.
\item [$R^{tr}$]  Spinning reserve in the $r$-th spinning reserve zone at time $t$.
%\item [$M$] Generators assignment matrix.
%\item [$L$] Compressors assignment matrix.
\item [$u^{te}$] Unit commitment state for generator $e$ at time $t$.
\item [$\tau^{t}$] Energy weight related to period of time $t$.
\item [$E^{e}$] Available energy for hydroelectric generator $e$, \break during the total analysis window.
\end{labeling}

\subsection*{Sets}

\begin{labeling}{Longer label\quad}
\item [$\cal{N}$] Gas nodes, $\left| \cal{N} \right|= n_{\cal{N}}$.
\item [$\cal{N}_{S}$] Gas nodes with storage, $\cal{N}_{S} \subset \cal{N} $, $\left| \cal{N}_{S} \right|= n_{\cal{S}}$.
\item [${\cal{O}}$] Gas pipelines, $\left| \cal{O}  \right| = n_{\cal{O}}$
\item [${\cal{C}}$] Compressors, ${\cal{C}}_{G} \cup {\cal{C}}_{E}$, $\left| \cal{C}  \right| = n_{\cal{C}}$
\item [${\cal{C}}_{G}$] Compressors controlled by natural gas, ${\cal{C}}_{G} \subseteq {\cal{C}}$, $\left| {\cal{C}}_{G}  \right| = n_{{\cal{C}}_{G}}$
\item [${\cal{C}}_{E}$] Compressors controlled by electric power, ${\cal{C}}_{E} \subseteq {\cal{C}}$, $\left| {\cal{C}}_{E}  \right| = n_{{\cal{C}}_{P}}$
\item [${\cal{W}}$] Gas wells, $\left| \cal{W} \right|= n_{\cal{W}}$.
%\item [${\cal{W}}^{i}$] Gas wells at node $i$, ${\cal{W}}^{i} \subset \cal{W} $, $\left| {\cal{W}}^{i} \right|= n_{{\cal{W}}^{i}}$.
\item [$\cal{B}$] Power buses, $\left| \cal{B} \right|= n_{\cal{B}}$.
\item [$\cal{L}$] Power lines, $\left| \cal{L} \right|= n_{\cal{L}}$.
\item [$\cal{E}$] Power unit generators, ${\cal{E}}_{H} \cup {\cal{E}}_{G}^{i} =\cal{E}$, $\left| \cal{E} \right|= n_{\cal{E}}$.
\item [${\cal{E}}_{H}$] Hydroelectric power units, ${\cal{E}}_{H} \subseteq \cal{E} $, $\left| {\cal{E}}_{H} \right|= n_{{\cal{E}}_{H}}$.
\item [${\cal{E}}^{i}_{G}$] Gas-fired power units connected to gas node $i$, \break ${\cal{E}}^{i}_{G} \subseteq \cal{E}$, $\left| {\cal{E}}^{i}_{G} \right|= n_{{\cal{E}}_{G}}$.
\item [${\cal{Z}}_{r}$] Spinning reserve zones.
\item [${\cal{F}}^{i}_{G}$, ${\cal{T}}^{i}_{G}$] Connected pipelines to node $i$ at side \textit{From} or \textit{To}.
\item [${\cal{F}}^{i}_{C}$, ${\cal{T}}^{i}_{C}$] Connected compressors to node $i$ at side \textit{From} or \textit{To}.
\item [${\cal{F}}^{m}_{E}$, ${\cal{T}}^{m}_{E}$] Connected power lines to bus $m$ at side \textit{From} or \textit{To}.
\item [$\cal{T}$] Periods of analysis.
\item [$\Sigma$] Different types of gas demands.
\end{labeling}

\subsection*{Variables}

\begin{labeling}{Longer label\quad}
\item [${f}_{g}^{oij}$] Gas flow in pipeline $o$, from node $i$ to node $j$.
\item [${f}_{g_+}^{oij}$ ${f}_{g_-}^{oij}$] Positive and negative gas flow in pipeline $o$.
\item [${f}_{g}^{cij}$] Gas flow in compressor $c$, from node $i$ to node $j$.
\item [$\psi^{c}$] Power consumed by compressor $c$.
\item [$\phi^{c}$] Gas consumed by compressor $c$, connected to node $i$ at side \textit{from}.
\item [$\gamma^{i \sigma}$] Non-served gas of type $\sigma$ at node $i$.
\item [$\pi^{i}$] Quadratic pressure at node $i$.
\item [${\pi}^{i}_{+}$, ${\pi}^{i}_{-}$] Quadratic over/under pressures at node $i$.
\item [$g^{w}$] Gas production at well $w$.
\item [$f_{s}^{i}$] Storage outflow difference.
\item [$f_{s_+}^{i}$, $f_{s_-}^{i}$] Storage outflow and inflow.
\item [$p_{g}^{te}$] Active power production of generator $e$ at time $t$.
\item [$q_{g}^{te}$] Reactive power production of generator $e$ at time $t$.
\item [$V^{tm}$] Voltage magnitude of bus $m$ at time $t$.
\item [$\theta^{tm}$] Voltage angle of bus $m$ at time $t$.
\item [$\epsilon^{tm}$] Non-served active power of bus $m$ at time $t$.
\end{labeling}
%\section{~}
\section{Objective function}

The cost function presented in Equation \ref{eq:obj_func} consists of several linear components, both from the power and the gas networks. It comprises the sum of the operation cost  of the interdependent system. In the case of the natural gas network, it includes the natural gas extraction cost, the transportation cost, the storage cost, the penalties associates with quadratic over/sub pressures, and non-supplied natural gas. In the case of the power network, it includes the generation cost and the penalties associates with non-supplied power demand.

%The cost function is represented by the equation \ref{obj_func} and is composed by several linear components. The first component is the gas production cost at each of the wells. As well as the first component, the second one is the power generation cost for every power plant, for the complete period. \color{red}The third component is the cost of the storage flow at every node with storage availability. \color{black}The fourth  expression is the storage cost, having into account the previous storage level and the outing flow. The fifth component is the gas transport cost for each pipeline. Finally, the last three component are the penalties cost for over/under pressure, non-supply gas and non-supply power, respectively. Related to the non-gas supply, is important to clarify that the non-supply cost depends on the type of user.

\begin{equation}
\begin{aligned}
C \left( x \right) = & \sum_{w \in \cal{W}}{C^{w}_{G} g^{w}} + \sum_{t \in \cal{T}} {\tau}^{t}  \sum_{e \in \cal{E}} {C^{e}_{E} p_{g}^{te}}\\
				& + \sum_{i \in {\cal{N}}_{S}}{\left({C^{i}_{S_+} f^{i}_{s_+}} - {C^{i}_{S_-} f^{i}_{s_-}}  \right)}\\
				& + \sum_{i \in {\cal{N}}_{S}}{C^{i}_{S} \left( S^{i}_{0} - f^{i}_{s} \right)} \\
				& + \sum_{o \in \cal{O}}{C^{oij}_{O} f^{oij}_{g_+}} - \sum_{o \in \cal{O}}{C^{oij}_{O} f^{oij}_{g_-}} \\
				& + \sum_{c \in \cal{C}}{C^{cij}_{C} f^{cij}_{g}} \\
				& + \sum_{i \in \cal{N}}{\alpha^{i}_{\pi_+} \pi^{i}_{+}} + \sum_{i \in \cal{N}}{ \alpha^{i}_{\pi_-} \pi^{i}_{-}} \\
				& + \sum_{i \in \cal{N}}\sum_{\sigma \in \Sigma} {\alpha_{\gamma}^{i \sigma}\gamma^{i \sigma}} + \alpha_{\epsilon} \sum_{t \in \cal{T}} {\tau}^{t} \sum_{m \in \cal{B}} {\epsilon^{tm}}
\end{aligned}
\label{eq:obj_func}
\vspace{0.3cm}
\end{equation}

The aim of the Optimal Power and Natural Gas Flow is to minimize the linear objective $C(x)$ subject to the constraints explained below.

\section{Constraints}

%The model of the optimal power and natural gas flow consists in maintain the balance in both networks. For the natural gas network the nodal balance must be has linear and non-linear constraints, which model the

\subsection{Gas network}

Equation \ref{eq:node_gas_balance} shows the gas balance for a specific node $k$ during a day. This gas balance is composed by the incoming and outgoing flows associated with pipelines and compressors at the node $k$, the outgoing stored flow in the available storage, the generation related to that node, and the total gas demand. In detail, the gas demand is composed by the required flow in the gas-fired power plants and compressors, and the total gas demand of the rest of the consumers, excluding the non-supplied natural gas.

\begin{equation}
\begin{array}{l}\displaystyle
\sum_{o \in {\cal{T}}^{k}_{G}}{{f}_{g}^{oij}} - \sum_{o \in {\cal{F}}^{k}_{G}}{{f}_{g}^{oij}} + \sum_{c \in {\cal{T}}^{k}_{C}}{{f}_{g}^{cij}} - \sum_{c \in {\cal{F}}^{k}_{C}}{ \left({f}_{g}^{cij} + \phi^{c}\right)} + {f}^{k}_{s}\\ [0.8cm] \displaystyle
+ \sum_{w \in {\cal{W}}^{k}}{g^{w}}  - \sum_{t \in \cal{T}} {\tau}^{t} \sum_{e \in {\cal{E}}^{k}_{G}}\left({\eta^{q}_{e}}\cdot {p_{g}^{te}}\right) = {\sum_{\sigma \in \Sigma}\left( D^{\sigma k}_{g} - \gamma^{\sigma k}\right)} \\
\end{array}
,\;\; \forall k \in \cal{N}.
\label{eq:node_gas_balance}
\end{equation}

\newpage
\noindent \textbf{Nodes}\\

Constraints related to node $k$ are those that involve variables of non-supplied gas demands and quadratic pressures. The non-supplied gas at node $k$ for a specific user $\sigma$ can not exceed the total demand of that user. This constraint is presented in Equation \ref{eq:nsg_limits}.

\begin{equation}
{0 \le \gamma^{\sigma k} \le D^{\sigma k}_{g}, \quad \forall \sigma \in \Sigma, \quad \forall k \in \cal{N}}.
\label{eq:nsg_limits}
\vspace{0.3cm}
\end{equation}

Moreover, Equations \ref{overp} and \ref{underp} are the constraints that characterize the quadratic over-pressure and under-pressure at each node of the system, respectively.

\begin{equation}
\begin{array}{l}
 \pi^{k} \le \overline{\pi}^{k} + \pi^{k}_{+}\\
 0 \le \pi^{k}_{+}\\
\end{array}
,\quad \forall k  \in {\cal{N}}\\
\label{overp}
\end{equation}

\begin{equation}
\begin{array}{l}
\underline{\pi}^{k} - \pi^{k}_{-} \le \pi^{k}\\
0 \le \pi^{k}_{-}\\
\end{array}
,\quad \forall k  \in {\cal{N}}.\\
\label{underp}
\vspace{0.3cm}
\end{equation}

\noindent \textbf{Wells}\\

The constraints related to the gas wells injection depends on each well characteristics. The injection limits are represented as follows:

\begin{equation}
\underline{g}^{w} \le g^{w} \le \overline{g}^{w}, \quad \forall w \in \cal{W}.
\label{eq:g_limits}
\end{equation}
%\vspace{5cm}

\noindent \textbf{Pipelines} \\

The gas flow in pipeline $o$, connecting nodes $i$ and $j$, depends on the quadratic pressures of such nodes. This behavior is given by the Weymouth Equation \ref{eq:wey}. In particular, the gas flow is allowed to be bidirectional within a physical limit for a maximum daily transportation according to Equation \ref{fgo_limits}. On the other hand, as the transport cost is always a positive quantity no matter the direction, variables $f^{oij}_{g+}$ and $f^{oij}_{g-}$ support a positive contribution to the objective function. Equation \ref{eq:fgo} shows the sum of both directional flows to determine the actual flow in the direction $from$  node $i$ - $to$ node $j$. These flow variables are constrained by Equations \ref{eq:fgopos_limits} and \ref{eq:fgoneg_limits}. As seen, the positive gas flow must be greater than or equal to zero but lower than or equal to the maximum transport capacity multiplied by a threshold factor, $\delta^{oij}$, which states an extra flow margin. Analogously, the negative gas flow has similar bounds in the negative side.

\begin{equation}
{f}^{oij}_{g} = {{\kappa}^{oij}} sgn \left(\pi^{i}-\pi^{j}\right) {\sqrt{\left|\pi^{i}-\pi^{j}\right|}}, \quad \forall o \in {\cal{O}}
\label{eq:wey}
\end{equation}
\begin{equation}
 - \overline{f}^{oij}_{g}  \le f^{oij}_{g} \le  \overline{f}^{oij}_{g},  \quad \forall o \in {\cal{O}}
\label{fgo_limits}
\end{equation}
\begin{equation}
{f}^{oij}_{g} =  f^{oij}_{g_+} + {f}^{oij}_{g_-}, \quad \forall o \in {\cal{O}}
\label{eq:fgo}
\end{equation}
\begin{equation}
0 \le f^{oij}_{g_+} \le \delta^{oij} \cdot \overline{f}^{oij}_{g}, \quad \forall o \in {\cal{O}}
\label{eq:fgopos_limits}
\end{equation}
\begin{equation}
- \delta^{oij} \cdot \overline{f}^{oij}_{g} \le f^{oij}_{g_-} \le 0, \quad \forall o \in {\cal{O}}.
\label{eq:fgoneg_limits}
\end{equation}

\noindent \textbf{Compressors}\\

Compressors allow recovering from pressure losses through the gas network. This process demands energy. The power consumption of compressor $c$ between the suction node $i$ and the discharge node $j$ is given by Equation \ref{eq:hp_fc}. It depends on the quadratic pressure ratio of nodes $i$ and $j$, and the gas flow through the compressor. Moreover, the additional gas required by a gas-driven compressor relies on its consumed power as shown in Equation \ref{eq:g_fc}. As the gas flow through compressors is restricted to flow in one direction, the flow limits of compressor $c$ are fixed according to Equation \ref{eq:gfa_limits}. Finally, the quadratic pressure at suction and discharge nodes must fall within acceptable margins, as presented in Equation \ref{eq:presa_rel}, where $\beta^{cij}$ is the maximum compressor ratio.\\

\begin{equation}
\psi^{c} = {B^{c}}{f}^{cij}_{g} \cdot \left[ {\left( \frac{\pi^{j}}{\pi^{i}} \right)}^{\frac{Z^{c}}{2}} - 1 \right],   \quad \forall c \in {\cal{C}}
\label{eq:hp_fc}
\end{equation}
\begin{equation}
{\phi}^{c} = x + y \psi^{c} +  z {\psi^{c}}^{2},  \quad \forall c \in {\cal{C}}_{G}
\label{eq:g_fc}
\end{equation}
\begin{equation}
0 \le {f}^{cij}_{g} \le \overline{f}^{cij}_{g},  \quad \forall c \in {\cal{C}}
\label{eq:gfa_limits}
\end{equation}
\begin{equation}
\begin{aligned}
&\pi^{i} \le \pi^{j} \le \beta^{cij} \pi^{i}\\
&\beta^{cij} \ge 1 \\
\end{aligned}
,\quad\forall i,j \in {\cal{N}}, \quad \forall c \in {\cal{C}}.
\label{eq:presa_rel}
\end{equation}

\noindent \textbf{Storage}\\

The storage outflow difference is the subtraction between the storage outflow and the storage inflow at the storage nodes; this relationship is represented by Equation \ref{eq:fs}. Additionally, Equation \ref{eq:fs_limits} shows that the outflow storage difference is restricted by the maximum and minimum amount of gas that is allowed to be injected to or demanded from the network in every storage node. Furthermore, as the storage unit can operate either as an injection or a demand for the network, Equations \ref{eq:fs+} and \ref{eq:fs-} represent the possible storage unit behavior. In particular, the maximum gas amount that can be injected is the difference between the currently available stored gas and the minimum possible volume of the unit. Similarly, the maximum inflow is the difference between the maximum volume of the unit and the currently available stored gas.

\begin{equation}
f_{s}^{k} = f_{s_+}^{k}  - f_{s_-}^{k}, \quad \forall k \in \cal{N}
\label{eq:fs}
\end{equation}
\begin{equation}
\underline{f}^{k}_{s} \le f^{k}_{s} \le \overline{f}^{k}_{s}, \quad \forall k \in \cal{N}
\label{eq:fs_limits}
\end{equation}
\begin{equation}
0 \le f_{s_+}^{i} \le S^{k}_{0}  - \underline{S}^{k}, \quad \forall k \in \cal{N}
\label{eq:fs+}
\end{equation}
\begin{equation}
0 \le f_{s_-}^{i} \le \overline{S}^{k} - S^{k}_{0}, \quad \forall k \in \cal{N}.
\label{eq:fs-}
\end{equation}

\subsection{Power network}

The power network balance equations of active and reactive power are given by Equation \ref{eq:power_balance}. The model also takes into consideration the non-supplied power demand and the power consumed by compressors connected to the power system.

\begin{equation}
\begin{array}{l}
g_{p_m}\left(\theta^{tm}, V^{tm}, p_{g}^{te}, \epsilon^{te}, \psi^{c}\right) = 0\\
g_{q_m}\left(\theta^{tm}, V^{tm}, q_{g}^{te}\right) = 0\\
\\
\quad \forall m \in {\cal{B}} \quad \forall t  \in {\cal{T}} \quad \forall c  \in {\cal{C}}_{E}.
\end{array}
\label{eq:power_balance}
\vspace{0.3cm}
\end{equation}

The main variables of the power system are the voltage angles $\theta^{tm}$ and the voltage magnitudes $V^{tm}$ at each bus $m$ for every period of time $t$, as well as the active generation $p^{te}_{g}$ and reactive generation $g^{te}_{g}$ at each generator $e$ for each time period. The voltage limits are represented by Equation \ref{eq:v_lims}, and the generation limits are shown in Equation \ref{eq:pq_lims}.

\begin{equation}
\begin{aligned}
&\theta^{t_\text{ref}} = 0\\
&\underline{V}^{tm} \le V^{tm}  \le \overline{V}^{tm}\\
\end{aligned}
\;,\quad \forall m \in {\cal{B}} \quad \forall t  \in {\cal{T}}
\label{eq:v_lims}
\end{equation}

\begin{equation}
\begin{aligned}
&\underline{p}_{g}^{e} \le p_{g}^{te}  \le \overline{p}_{g}^{e}\\
&\underline{q}_{g}^{e} \le q_{g}^{te}  \le \overline{q}_{g}^{e}\\
\end{aligned}
\;,\quad \forall e \in {\cal{E}}, \quad \forall t  \in {\cal{T}}.
\label{eq:pq_lims}
\vspace{0.3cm}
\end{equation}

The power flow limits are bidirectional and are presented in Equation \ref{eq:sft_lims}, where $\mathbb{S}_{fl}$ and $\mathbb{S}_{tl}$ are the complex power injections at side $from$ and $to$ of line $l$, respectively.

\begin{equation}
\begin{aligned}
&|\mathbb{S}_{fl}\left(\theta,V\right)| \le \overline{\mathbb{S}}_{fl}\\
&|\mathbb{S}_{tl}\left(\theta,V\right)| \le \overline{\mathbb{S}}_{tl}\\
\end{aligned}
\;,\quad \forall l \in {\cal{L}}.
\label{eq:sft_lims}
\vspace{0.3cm}
\end{equation}

 The non-supplied active power demand at bus $m$ can not exceed the total bus demand, according to Equation \ref{nsd_limits}.

\begin{equation}
0 \le \epsilon^{tm} \le D^{tm}_{e}, \quad \forall m \in {\cal{B}}, \quad \forall t  \in {\cal{T}}.
\label{nsd_limits}
\vspace{0.3cm}
\end{equation}

The model also considers the required spinning reserve for each zone $r$ at every time $t$. This constraint is given by Equation \ref{eq:reserve}.

\begin{equation}
%\begin{array}{l}
\sum_{e \in {\cal{Z}}_{r}}{u^{te} \left( \overline{p}_{g}^{e} - p_{g}^{te} \right)} \ge R^{tr} ,\quad \forall r \in {\cal{Z}}_{r}, \quad \forall t  \in {\cal{T}}.
%\end{array}
\label{eq:reserve}
\vspace{0.3cm}
\end{equation}

Finally, the model takes into consideration the maximum available energy during a day for certain generators, especially the energy stored in the dams for hydro-power plants. Equation \ref{eq:hydro_e} represents such a constraint.

\begin{equation}
\sum_{t \in {\cal{T}}}{{\tau}^{t} p_{g}^{te}} \le E^{e}, \quad \forall e \in {\cal{E}}_{H}
\label{eq:hydro_e}.
\vspace{0.3cm}
\end{equation}

\newpage


\section{Review of stochastic calculus}
\subsection{Riemann integration}
\subsection{The It\^o integral}


\documentclass[12pt, a4paper]{article}

\input{preamble}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\rhead{Arnau Abella}
\lhead{Randomized Algorithms - UPC}
\rfoot{Page \thepage}

\title{%
  \vspace{-10ex}
  An Exploratory Assignment on Minimum Spanning Trees
}
\author{%
  Arnau Abella \\
  \large{Universitat Polit\`ecnica de Catalunya}
}
\date{\today}

\begin{document}
\maketitle

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

\vspace{5ex}

\section{Introduction}\label{sec:1}

Let the weight of a tree be the sum of the squares of its edges lengths. Given a set of points $P$ in the unit square $I \times I$, let $W(P)$ be the weight of the \textit{minimum spanning tree (MST)} of $P$, where an edge length is the \textit{Euclidean distance} between its endpoints. If $P$ consist of the four corners of the square, then $W(P) = 3$. Gilbert and Pollack \cite{gil68} proved that $W(P)$ is $\mathcal{O}(1)$ and this was extended to an arbitrary number of dimensions by Bern and Eppstein \cite{bern93}. While more recent \textit{divide-and-conquer} approaches have show that $W(P) \leq 4$, no point set is known with $W(P) > 3$, and hence it has been widely conjectured that $W(P) \leq 3$. In 2013, it was proven that $W(P) < 3.41$ \cite{aichholzer2013sum}. Here we show an empirical experiment to check whether $W(P) < 3.41$ holds for any $MST(P)$.

\section{Experiment}\label{sec:2}

In order to check the previous theorem, we generate uniformaly at random points in the unite square $P$ and compute the weight of the $MST$. We do this with an increasing number of points in order to explore the solution space. It is important to note that the exploration is not exhaustive since exploring the whole solution space would require a large amount of computational power and the search does not explicitly aims for the degenerate instances where $W(P) > 3.41$ may happen.

These random points are generated using a pseudo-random number generator (PRNG) that uses \textit{Marsaglia's MWC256} (also known as \textit{MWC8222}) which has a period of $2^{8222}$ and fares well in tests of randomness. It is also extremely fast, between $2-3$ times faster than the \textit{Mersenne Twister}.

\newpage

Once the random points are generated, we build a complete undirected graph $G = (V, E)$ where $|V| = n$ and $|E| = \binom{n}{2}$ using an \textit{inductive graph} representation which is efficiently implemented as a \textit{big-endian patricia trees}.

Then, we search the \textit{minimum spanning tree} on the inducive graph using \textit{Prim's algorihm} (see Algorithm \ref{algo:prim}). The inductive implementation has $\mathcal{O}(|V|^2)$ time complexity which could be improved up to $\mathcal{O}(|E| + |V| \log |V|)$ using a \textit{fibonacci heap}.

\begin{algorithm}[H]
  \SetAlgoLined
  \DontPrintSemicolon
  \SetKwInput{Input}{Input}
  \SetKwInput{Output}{Output}
  \Input{An undirected weighted graph $G = (V,E)$}
  \Output{The minimum spanning tree of the input graph $G$}
  \ForEach{$v \in V$}{
    $key(v) = \infty$\;
    $parent(v) = NIL$\;
  }
  $key(r) = 0$ \tcp*[r]{Pick \textit{u.a.r.} the initial vertex $r \in V$}
  $Q = V$\;
  \While{$Q \neq \emptyset$}{
    $u = EXTRACT-MIN(Q)$\;
    \ForEach{$v \in ADJ(u)$}{
      \If{$v \in Q$ and $w(u,v) < key(v)$}{
        $key(v) = w(u,v)$\;
        $parent(v) = u$\;
      }
    }
  }
  \caption{Minimum Spanning Tree Prim's Algorithm}
  \label{algo:prim}
\end{algorithm}

We emphasise that there are more efficient algorithms such as Karger, Klein and Tarjan linear-time randomized algorithm \cite{karger1995randomized} or Bernand Chazelle almost linear algorithm \cite{chazelle2000minimum} but we decided to use Prim's algorithm because it is the simplest to implement and the performance was good enough for this experiment (see Table \ref{table:1}).

\begin{table}[H]
  \center
  \begin{tabular}{ccccc}
    $|V|$  & Time       & $R^2$   & $\mu$      & $\sigma$    \\ \hline
    $128$  & $8.1$   ms & $0.996$ & $8.358$ ms & $327.8$ $\mu$s \\
    $512 $ & $406.0$ ms & $1.0  $ & $391.9$ ms & $10.33$ ms    \\
    $1024$ & $153.6$ ms & $1.0  $ & $150.3$ ms & $3.178$ ms    \\
    $2048$ & $943.1$ ms & $1.0  $ & $934.4$ ms & $14.24$ ms
  \end{tabular}
\caption{Prim's Algorithm benchmark.}
\label{table:1}
\end{table}

One optimization applied to the search is the removal of edges that are unlikely to be used. The minimum spanning tree is unlikely to use any edge of weight greater than $k(n)$ for some function $k(n)$. We estimated the function $k(n)$ by calculating the $MST$ on random graphs of increasing size in the unite square (see Table \ref{table:2}).

\begin{table}[H]
  \center
  \begin{tabular}{cc}
    $|V|$  & $W_{max}$     \\ \hline
    $16$   & $0.220$ \\
    $32$   & $0.150$ \\
    $64$   & $0.060$ \\
    $128$  & $0.040$ \\
    $256$  & $0.020$ \\
    $512$  & $0.013$ \\
    $1024$ & $0.008$ \\
    $2048$ & $0.004$ \\
  \end{tabular}
  \caption{Estimation of $k(n)$.}
\label{table:2}
\end{table}

The code of the experiment is publicly available at \url{https://github.com/monadplus/mst-experiment}.

\section{Results}\label{sec:3}

The experiment was run on a ThinkPad T495 (AMD Ryzen 7 Pro 3700U, $13934$ MiB of RAM, Linux 5.8.10-arch1-1). The experiment was implemented purely in haskell on the Glasgow Haskell Compiler (GHC) 8.8.4. The experiment was run up to $8196$ vertices where the execution time increased up to $30'$ of CPU time and we could not generate reliable results.

\begin{table}[H]
  \center
  \begin{tabular}{ccccc}
    $|V|$  & $\mu_W$    & $\sigma_W$ \\ \hline
    $16$   & $1.1067605$ & $0.21767415$ \\
    $32$   & $1.1015033$ & $0.20797518$ \\
    $64$   & $1.2079728$ & $0.12993012$ \\
    $128$  & $1.1780577$ & $0.10956229$ \\
    $256$  & $1.2000419$ & $0.08088318$ \\
    $512$  & $1.1488761$ & $0.02073750$ \\
    $1024$ & $1.2366604$ & $0.05549732$ \\
    $2048$ & $1.2135003$ & $0.00306511$ \\
    $4096$ & $1.1810393$ & $0.01102620$
  \end{tabular}
\caption{Experiment results.}
\label{table:3}
\end{table}

The results show that $W(P)$ is $\mathcal{O}(1)$ (see Table \ref{table:3} and Figure \ref{fig:1}). Note, the deviation decreases when the number of vertices increases. As a conclusion, we  believe that there is no evidence to refute the claim $W(P) < 3.41$.

For future work, the search could be tunned to better explore the degenerate instances which would lead to a stronger position to accept or refute the theorem.

\begin{figure}[H]
  \center
  \includegraphics[scale=0.4]{plot}
  \caption{Experiment results.}
  \label{fig:1}
\end{figure}

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

\bibliographystyle{unsrt}
\bibliography{refs}

\end{document}


\documentclass[11pt]{article}
\usepackage{natbib,unatbib}
\usepackage[nohide,twocolumn]{ulecnot}
\usepackage{usynsem}
\usepackage{uprog}
\usepackage{utheorem}
\usepackage{linguex}
	\renewcommand{\refdash}{}

\lhead{COGS 543 -- Computational Semantics}
\chead{$\lambda$-Calculus}
\rhead{Updated \it \today}
\lfoot{Umut \"Ozge}
\cfoot{}
\rfoot{Page \thepage/\pageref{LastPage}}
\setlength{\headheight}{13.6pt}

\usepackage{tikz-qtree}

\begin{document}

\section{Introduction} Here are two functions:

\begin{align}
\label{f} f(x) & = x + 2\\
\label{g}g(x) & = x^2
\end{align}

The composition of these functions is expressed as:

\begin{align}
f\circ g = f(g(x))\label{fog}
\end{align}

\xref{fog} is of no use without looking up the definitions of the names $f$ and $g$ in \xref{f} and \xref{g}. Furthermore, it is simply impossible to define a function that takes two functions as arguments and gives a third function which is the composition of the first two. Actually, it is impossible to define any higher order function within the language of our expressions -- i.e.\ numbers, arithmetic operators and parentheses.

Given a formal language -- the language of arithmetic in our case -- lambda calculus adds to the language the lambda operator `$\lambda$' and the dot `.'. This additions allow one to write anonymous functions who do not bear names like $f$ or $g$, by which you look up definitions. Instead, in lambda calculus, the name and the function definition are one and the same entities. Here is how functions in \xref{f} and \xref{g} are rendered in lambda calculus:

\begin{align}
\label{lf} & (\lambda x.x + 2)\\
\label{lg} & (\lambda x.x^2)
\end{align}

Application of a function to its argument is depicted by concatenating the function and its argument and enclosing both in a parentheses. To compute the result of application, you substitute the argument to its place as in standard functions and get rid of the lambda, the variable and the dot.

\begin{align}
\label{af} & ((\lambda x.x + 2) 8)\\
 & = 8 +2  = 10\nonumber
\end{align}

\begin{align}
\label{ag} & ((\lambda x.x^2) 8)\\
& = 8^2 = 64\nonumber
\end{align}

So far everything appears as a little more cryptic way of defining functions and computing the result of applying a function to its argument. The real power of lambda calculus becomes visible when you apply functions to other functions, in exactly the same way you apply them to non-function arguments. We will illustrate this over function composition. Things will get a little complicated at this point; either go very slowly, or have a first pass, read on and come back. In either case you need to use pencil and paper to understand the transitions between forms.  Now let us have the following higher order function that composes two functions -- the lambda calculus way of having $f_1\circ f_2$:

\begin{align}
(\lambda f_1.(\lambda f_2.(\lambda z.(f_1(f_2z)))))
\end{align}

First apply this to $(\lambda x. x + 2)$:

\begin{align}
& ((\lambda f_1.(\lambda f_2.(\lambda z.(f_1(f_2z))))) (\lambda x. x +2))\\
=& (\lambda f_2.(\lambda z.((\lambda x. x +2)(f_2z)))) \nonumber
\end{align}

Now apply this result to the second function $(\lambda y. y^2)$:

\begin{align}
&  ((\lambda f_2.(\lambda z.((\lambda x. x +2)(f_2z))))(\lambda y. y^2)) \\
=& (\lambda z.((\lambda x. x+2)((\lambda y. y^2)z))) \nonumber
\end{align}

We obtained the composition of the functions defined in \xref{f} and \xref{g}. To see that this is indeed the case, apply this composite to an argument, say 6:

\begin{align}
\label{faint} & ((\lambda z.((\lambda x. x+2)((\lambda y. y^2)z))) 6)\\
&=  ((\lambda x. x+2)((\lambda y. y^2)6))\nonumber
\end{align}

If you look carefully, you will realize two more patterns in the form of $(f a)$ -- a function $f$ applied to argument $a$ -- in the result we obtained in \xref{faint}. The one that is relatively easier to see is
\begin{align}
\label{occ1} ((\lambda y. y^2)6)
\end{align}

if you perform this application you will obtain 36 as result. Replacing the occurrence of \xref{occ1} in \xref{faint} with 36 will give,

\begin{align}
& ((\lambda x. x+2)36)\\
& = 36+2 = 38 \nonumber
\end{align}

We now turn to a detailed and formal characterization of the concepts we encountered.

\section{The set of lambda terms}

From here on, we take a rather abstract stance towards lambda calculus.  The discussion will apply to any domain of use, not just arithmetic. Try to perceive the lambda calculus as a system of manipulating expressions constructed from a set of names, the lambda, the dot, and left and right parentheses, which we call \uterm{lambda terms}. Also refrain from assuming that $x, y, z$ stand for variables, $f, g, h$ for functions, $a, b, c$  for constants, etc. Names are just names; although our use may be suggestive at times, there is no necessary association between a name and the type of object it stands for.

\begin{udefinition}[Lambda terms]\label{dfexp}
Let $A=\{ a,b,\ldots,z\}$ be the set of \uterm{names}, we extend the set by subscripting like $x_1,x_2\ldots$ as we need, so that we have an infinite set of names.

\begin{itemize}
\item[i.] all names are lambda terms;
\item[ii.] if $\omega_1$ and $\omega_2$ are lambda terms, so is $(\omega_1\omega_2)$; \hfill (concatenation/application)
\item[iii.] if $\omega$ is a lambda term and $\alpha$ is a name, then
$(\lambda\alpha.\omega)$ is a lambda term; \hfill (abstraction)
\item[iv.] nothing else is a lambda term.
\end{itemize}
\qed
\end{udefinition}

We usually shorten ``lambda term'' to ``term''. Given a name $\alpha$, we call the expression `$\lambda\alpha$' a ``lambda binder''.

\begin{uexample}\label{fullex}
Some example terms:
\renewcommand{\arraycolsep}{6pt}
\renewcommand{\arraystretch}{2}

$$
\begin{array}{cccc}
x & (xy) & (x(yz))  & ((xy)z) \\
(\lambda x.x) & (\lambda y.(\lambda x.x)) & (\lambda z.(x(\lambda y.(yz)))) & (x(\lambda z.(\lambda y.(yz))))\\
(x(\lambda x.x)) & ((\lambda y.(\lambda x.x))(\lambda x.x)) & (((\lambda y.(\lambda x.x))(\lambda x.x))(xy)) & ((x(yz))((xy)z))
\end{array}
$$

\qed
\end{uexample}

Once again note that in lambda calculus we write $(f x)$ (simplified to $fx$) rather than the usual $f(x)$
to represent the application of function $f$ to the argument $x$.

\section{Notational conventions}
\label{scnotcon}

\begin{itemize}

\item[A.] Omit outer parentheses.

\item[B.] Concatenation of terms associate to left:

\begin{align*}
\omega_1\omega_2\omega_3\ldots\omega_n & \equiv (\ldots((\omega_1\omega_2)\omega_3)\ldots\omega_n)\\
fxyz & \equiv   (((fx)y)z)\\
fx(yz) & \equiv   ((fx)(yz))\\
f(xyz) & \equiv   (f((xy)z))\\
f(xy)z & \equiv   ((f(xy))z)
\end{align*}

\item[C.] The scope of dot extends to right until the first right parenthesis whose matching pair falls to the left of the dot. In restoring parentheses, put a left parenthesis just before the first lambda sign you encounter to the left of the dot, then start to scan rightwards, and put a right parenthesis at the point you encounter a right parenthesis unmatched during your rightward scan or the end of the term.

\begin{align*}
(\lambda x.xx)y & \equiv ((\lambda x.xx)y) \not\equiv \lambda x.xxy\\
\lambda x.xyz & \equiv (\lambda x.xyz) \not\equiv (\lambda x. x)yz
\end{align*}

\item[D.] Stacked lambda binders associate to right, and dots between lambda binders are deleted:
\begin{align*}
\lambda \alpha_1\lambda \alpha_2\lambda \alpha_3\ldots\lambda \alpha_n.\omega &\equiv (\lambda\alpha_1.(\lambda\alpha_2.(\lambda\alpha_3\ldots(\lambda\alpha_n.\omega)\ldots)))\\
\lambda f \lambda x.f(fx)  &\equiv  (\lambda f.(\lambda x.(f(fx))))\\
\lambda f.(\lambda x\lambda y.xyy)zf & \equiv (\lambda f.(((\lambda x.(\lambda y.((xy)y)))z)f))
\end{align*}

\end{itemize}

\begin{uexample}
The terms in Example~\ref{fullex} in simplified form:

\begin{align*}
(xy) &\equiv xy\\
(x(yz)) &\equiv x(yz)\\
((xy)z) &\equiv xyz\\
(\lambda x.x) &\equiv \lambda x.x\\
(\lambda y.(\lambda x.x)) &\equiv \lambda y.\lambda x.x\\
(\lambda z.(x(\lambda y.(yz)))) &\equiv \lambda z.x(\lambda y.yz)\\
(x(\lambda z.(\lambda y.(yz)))) &\equiv x(\lambda z\lambda y.yz)\\
(x(\lambda x.x)) &\equiv x(\lambda x.x)\\
((\lambda y.(\lambda x.x))(\lambda x.x)) &\equiv (\lambda y\lambda x.x)\lambda x.x\\
(((\lambda y.(\lambda x.x))(\lambda x.x))(xy)) &\equiv (\lambda y\lambda x.x)(\lambda x.x)(xy)\\
((x(yz))((xy)z)) &\equiv x(yz)(xyz)
\end{align*}
\qed
\end{uexample}

Here are some exercises from \cite{selinger13} to train your hand on notational conventions:

\begin{uexercise}
Simplify the following terms by removing parentheses and dots using the conventions above:
\begin{itemize}
\item[i.] $(\lambda x.(\lambda y.(\lambda z.((xz)(yz)))))$
\item[ii.] $(((ab)(cd))((ef)(gh)))$
\item[iii.] $(\lambda x.((\lambda y.(yx))(\lambda v.v)z)u)(\lambda w.w)$
\end{itemize}
\qed
\end{uexercise}

\begin{uexercise}
Restore the parentheses to the following terms:
\begin{itemize}
\item[i.] $xxxx$
\item[ii.] $\lambda x.x\lambda y.y$
\item[iii.] $\lambda x.(x\lambda y.yxx)x$
\end{itemize}
\qed
\end{uexercise}

\section{Bondage and freedom}

\subsection{Expressions as binary trees}

First observe that every compound term (=terms that are more complicated than a name) corresponds to a binary tree; this is obvious if you take the steps (ii) and (iii) in Definition~\xref{dfexp} as an instruction to form a binary branching tree. Here are some examples:

\smallskip
\ex.
\parbox[t]{0.2\textwidth}{
a. \Tree [.$(\lambda z.(x(\lambda y.(yz))))$
			[.$\lambda z$ ]
			[.$(x(\lambda y.(yz)))$
				[.$x$ ]
				[.$(\lambda y.(yz))$
					[.$\lambda y$ ]
					[.$(yz)$
						[.$y$ ]
						[.$z$ ]
					]
				]
			]
]
}
\parbox[t]{0.22\textwidth}{
b. \Tree [.$(\lambda f.(\lambda g.(\lambda x.((fx)(gx)))))$
		[.$\lambda f$ ]
		[.$(\lambda g.(\lambda x.((fx)(gx))))$
			[.$\lambda g$ ]
			[.$(\lambda x.((fx)(gx)))$
				[.$\lambda x$ ]
				[.$((fx)(gx))$
					[.$(fx)$
						[.$f$ ]
						[.$x$ ]
					]
					[.$(gx)$
						[.$g$ ]
						[.$x$ ]
					]
				]
			]
		]
]
}

Now the same examples simplified by the conventions of Section~\ref{scnotcon}:

\ex.\label{extree}
\parbox[t]{0.2\textwidth}{
a. \Tree [.$\lambda z.x(\lambda y.yz)$
			[.$\lambda z$ ]
			[.$x(\lambda y.yz)$
				[.$x$ ]
				[.$(\lambda y.yz)$
						[.$\lambda y$ ]
					[.$yz$
						[.$y$ ]
						[.$z$ ]
					]
				]
			]
]
}
\parbox[t]{0.23\textwidth}{
b. \Tree [.$\lambda f\lambda g\lambda x.fx(gx)$
		[.$\lambda f$ ]
		[.$\lambda g\lambda x.fx(gx)$
			[.$\lambda g$ ]
			[.$\lambda x.fx(gx)$
				[.$\lambda x$ ]
				[.$fx(gx)$
					[.$fx$
						[.$f$ ]
						[.$x$ ]
					]
					[.$gx$
						[.$g$ ]
						[.$x$ ]
					]
				]
			]
		]
]
}

We need some auxiliary definitions before we define bondage and freedom.

\medskip
\noindent {\bf Occurrence}\\
The notions of bondage and freedom are defined for \uterm{occurrences} of names and lambda binders. Each leaf\footnote{Leaves are nodes without children.} in the construction tree of a lambda term is either an occurrence of a name or a lambda binder. Names also occur as part of lambda binders, but in the technical sense we define here occurrence of a name does not include such cases. For instance the name $x$ has 1 occurrence in \xxref{extree}{a} and 2 occurrences in \xxref{extree}{b}.

\medskip
\noindent {\bf Step}\\
In traversing a tree, going from one node to an adjacent\footnote{Two nodes are adjacent if there exists a line connecting them without any nodes in between.} node is one \uterm{step}. Steps can be taken down or up.

\medskip
\noindent {\bf Command}\\
A node $\alpha$ in a tree $\Gamma$ commands a node $\beta$ in the same tree iff you can go one step up from $\alpha$ and go down one or more steps down to reach $\beta$.

\medskip
\noindent {\bf Bondage}\\
Given a term $\Gamma$ and a name $\alpha$, an occurrence of $\alpha$ is \uterm{bound} in $\Gamma$ by an occurrence of a lambda binder  $\lambda\alpha$ iff the occurence of $\lambda\alpha$ commands the occurrence of $\alpha$ and there is no occurrence of $\lambda\alpha$ that commands $\alpha$ and closer to it.

Given a term $\Gamma$ and a name $\alpha$, an occurrence of $\alpha$ is \uterm{bound} in $\Gamma$ iff there exists at least one occurrence of a lambda binder $\lambda\alpha$ in $\Gamma$ binding that occurrence of $\alpha$  in $\Gamma$.

Note that bondage is defined between occurrences of names and lambda binders; therefore, it does not make sense to ask whether $\alpha$ is bound in a term $\Gamma$ or not; one has to specify which occurrence of $\alpha$ s/he is talking about.

\medskip
\noindent {\bf Freedom}\\
Given an expression $\Gamma$ and a name $\alpha$, an occurrence of $\alpha$ is \uterm{free} in $\Gamma$, if it is not bound by any occurrence of a lambda binder in $\Gamma$.

% It will be useful to define a function \sysm{FN}, which gives the set of free names in a term:
%
% \begin{udefinition}[Set of free names.]
%
% The function \sysm{FN} that maps the set of lambda terms to the power set of the set of names:
% \begin{itemize}
% \item[i.] \sysm{FN (\omega) = \{\omega \}  }, if $\omega$ is a name;
% \item[ii.] \sysm{FN ((\omega_1\omega_2)) = FN(\omega_1) \cup FN(\omega_2)};
% \item[iii.] \sysm{FN (\lambda \alpha.\omega) = FN(\omega) - \{ \alpha\}}.
% \end{itemize}
% \qed
% \end{udefinition}
%

\begin{uexample}
\begin{itemize}
\item[]
\item In \xxref{extree}{a}, there is a single occurrence of $x$ and it is free in \xxref{extree}{a}
\item In \xxref{extree}{b}, there is no free occurrence of a name; all the occurrences of all the names are bound by some occurrence of a lambda term.
\item Our definitions allow for expressions like $\lambda x\lambda k.c y$, where all the occurrences of all names are free and none of the lambda terms bind any name.
\item In $\lambda x\lambda y.z (y x) y $ both occurrences of $y$ are bound by $\lambda y$, whereas in $(\lambda x\lambda y.z (y x)) y $, the second $y$ (counting from left to right) is free.
\end{itemize}
\qed
\end{uexample}

\section{$\alpha$-equivalence}

The functions $\lambda x.x$ and $\lambda y.y$ are one and the same function; they just do the same thing using different names. Similarly for $\lambda f\lambda g.fa(ga)$ and $\lambda g\lambda f.ga(fa)$. We express this relation of being alphabetical variants of the same term as \uterm{\sysm{\mathbf{\alpha}}-equivalence}. Symbolically,

$$
\lambda f\lambda g.fa(ga) \equiv_\alpha \lambda g\lambda f.ga(fa)
$$

Again think of $\alpha$-equivalence over trees:

\parbox[t]{0.2\textwidth}{
\Tree [.$\lambda f\lambda g.fa(ga)$
		[.$\lambda f$ ]
		[.$\lambda g.fa(ga)$
			[.$\lambda g$ ]
			[.$fa(ga)$
					[.$fa$ $f$ $a$ ]
					[.$ga$ $g$ $a$ ]
			]
		]
]
}
\parbox[t]{0.2\textwidth}{
\Tree [.$\lambda g\lambda f.ga(fa)$
		[.$\lambda g$ ]
		[.$\lambda f.ga(fa)$
			[.$\lambda f$ ]
			[.$.ga(fa)$
					[.$ga$ $g$ $a$ ]
					[.$fa$ $f$ $a$ ]
			]
		]
]
}

The terms have structurally identical trees with identical binding relations. The only difference between the two concerns the bound names.
Here is a formal definition:

\begin{udefinition}[$\alpha$-equivalence]
Let $\Lambda_1$ and $\Lambda_2$ are lambda terms with construction trees $\tau_1$ and $\tau_2$;\\
and let $\nu_1$ and $\nu_2$ be sets of nodes of $\tau_1$ and $\tau_2$, respectively;\\
$\Lambda_1$ and $\Lambda_2$ are $\alpha$-equivalent iff
there exists a one to one correspondence $c$ between $\nu_1$ and $\nu_2$ such that,
\begin{itemize}
\item for all nodes $i,j$ in $\nu_1$, $i$ immediately dominates\footnote{A node $\nu_1$ immediately dominates node $\nu_2$ iff it takes only one step going down from $\nu_1$ to $\nu_2$} $j$ iff $c(i)$ immediately dominates $c(j)$;
\item for all nodes $i$ in $\nu_1$, $i$ is some name $\alpha$, free in $\tau_1$, iff $c(i)$ is some name $\alpha$, free in $\tau_2$;
\item for all nodes $i,j$ in $\nu_1$, $i$ is a lambda binder binding $j$ iff $c(i)$ is a lambda binder binding $c(j)$.
\end{itemize}
\qed
\end{udefinition}

The above definition gives a declarative specification of what does it mean for two terms to be $\alpha$-equivalent. It does not tell how to go about obtaining an $\alpha$-equivalent term from a given one. The essence of $\alpha$-equivalence is to obtain a term that has the same configuration and binding relations with the original, but possibly differing in some bound names. This looks like a renaming operation where you change the name on a lambda binder and all the occurrences of that name bound by the binder, crucially without touching any free names. Care needs to be taken, however, in renaming bound variables. For instance take the following term:

\begin{align}\label{alphaorg}
\lambda x.z(\lambda y.yx)
\end{align}

You want to replace the name $x$ with $z$. If you replace all the occurrences, you would obtain,

\begin{align}\label{alphawrong}
\lambda z.z(\lambda y.yz)
\end{align}

where you made the first $z$, which was free in \xref{alphaorg}, accidentally bound in \xref{alphawrong}. In other words, you failed to obtain an $\alpha$-equivalent term:

$$
\lambda x.z(\lambda y.yx)\not\equiv_\alpha \lambda z.z(\lambda y.yz)
$$

Likewise, renaming $x$ to $y$ would again yield a non-$\alpha$-equivalent term:

$$
\lambda x.z(\lambda y.yx)\not\equiv_\alpha \lambda y.z(\lambda y.yy)
$$

At this point one way to avoid such accidental bondages is to always rename to a name that is not in the term at all. But this move misses the general concept of $\alpha$-equivalence. Observe that it is legitimate to rename $y$ to $z$ in \xref{alphaorg}:

$$
\lambda x.z(\lambda y.yx)\equiv_\alpha \lambda x.z(\lambda z.zx)
$$

We take another strategy for avoiding accidental bondage. We again make use of trees and define renaming as a single step procedure that targets a specific occurrence of a lambda binder. Here is a rough algorithm:

\begin{ualgorithm}[Single shot rename]
\label{renamealg}
\begin{algorithmic}
\item[]
\item[]
\Function{Rename}{\sysm{term, binder, name}}
\State \sysm{nodes \gets } nodes bound by \sysm{binder}
\For{\sysm{node} in \sysm{nodes} }
\State replace the name on \sysm{node} with \sysm{name}
\State check for new binders of \sysm{node}
 	 	\If{any new binder dominated by \sysm{binder} }
 	 		\State ERROR
 	 	\EndIf
\EndFor
\State replace the name on \sysm{binder} with \sysm{name}
\If{new bound nodes not in \sysm{nodes} are created }
\State ERROR!
\EndIf
\EndFunction
\end{algorithmic}
\qed
\end{ualgorithm}

Two terms $\Lambda_1$ and $\Lambda_2$ are $\alpha$-equivalent iff one can be obtained from the other by zero or more applications of Algorithm~\xref{renamealg}.

\section{Substitution}

We will see a process quite similar to renaming. In renaming you alphabetically change a lambda binder and the names it binds. You do this carefully so that you do not introduce binding relations that were not present among the nodes of the original tree. The process of \uterm{substitution} again operates on a lambda term, but replaces all \emph{free} occurrences of a name with a \emph{lambda term}; therefore, substitution may alter the tree structure of the lambda term it operates on. Again, care is needed not to accidentally bind the free names in the substituted term. The substitution of a term $\nu$ for $\alpha$ in a term $\Lambda$ is denoted as:
$$
\subs{\Lambda}{\nu}{\alpha}
$$

and defined via induction as:

\begin{udefinition}[Substitution]

\begin{itemize}
\item[]
\item[i.] $\subs{\alpha}{\nu}{\alpha}= \nu$;
\item[ii.] $\subs{\gamma}{\nu}{\alpha}= \gamma$, if $\gamma$ is a name and $\gamma\neq\alpha$;
\item[iii.] $\subs{(\omega_1\omega_2)}{\nu}{\alpha}= (\subs{\omega_1}{\nu}{\alpha}\subs{\omega_2}{\nu}{\alpha})$;
\item[iv.] $\subs{(\lambda \alpha.\omega)}{\nu}{\alpha}= (\lambda \alpha.\omega)$;
\item[v.] $\subs{(\lambda \gamma.\omega)}{\nu}{\alpha} = (\lambda \gamma.\subs{\omega}{\nu}{\alpha})$, where $\gamma\neq\alpha$ and $\gamma$ is not free in $\nu$.

\end{itemize}

\qed
\end{udefinition}

Observe that substitution can get blocked in clause (v). For instance take,

\begin{align}
& \subs{(k(\lambda z.zx))}{(\lambda y.zy)}{x}\\
 \equiv & \, \subs{k}{(\lambda y.zy)}{x}\subs{(\lambda z.zx)}{(\lambda y.zy)}{x}  \nonumber\\
 \equiv & \, k \subs{(\lambda z.zx)}{(\lambda y.zy)}{x} \nonumber
\end{align}

The substitution operation gets blocked at $\subs{(\lambda z.zx)}{(\lambda y.zy)}{x}$, since $z$ is free in $(\lambda y.zy)$. What needs to be done at such situations is to switch to an $\alpha$-equivalent term. First,

\begin{align}
& (k(\lambda z.zx)) \equiv_\alpha (k(\lambda f.fx))
\end{align}

then

\begin{align}
& \subs{(k(\lambda f.fx))}{(\lambda y.zy)}{x}\\
 \equiv & \, \subs{k}{(\lambda y.zy)}{x}\subs{(\lambda f.fx)}{(\lambda y.zy)}{x}  \nonumber\\
 \equiv & \, k (\lambda f.\subs{(fx)}{(\lambda y.zy)}{x}) \nonumber\\
 \equiv & \, k (\lambda f.f(\lambda y.zy))\nonumber
\end{align}

\section{$\beta$-reduction}
\ezimeti{
\item An expression of the form $(\lambda \alpha.\gamma)\omega$ is called a
\uterm{$\beta$-redex}.

\item It can be \uterm{$\beta$-reduced} to $\subs{\gamma}{\omega}{\alpha}$,
called a \uterm{reduct}, if $\omega$ is free for $\alpha$ in $\gamma$.

\item  A lambda expression can be reduced by turning all the redexes to reducts,
which results in a \uterm{$\beta$-normal} form.

\item Some example reductions:
\begin{align*}
(\lambda f.fx)g  & \breduce gx \\
(\lambda f.fx)ga  & \breduce gxa \\
(\lambda f.fx)(ga) & \breduce gax \\
(\lambda f\lambda x.fx)g a & \breduce ga \\
%(\lambda x\lambda y \lambda z.x(yz))f & \breduce \\
\end{align*}

\item There may be more than one redex in a
expression:

\begin{align*}
(\lambda x.y)((\lambda z.zz)(\lambda w.w)) & \breduce (\lambda x.y)((\lambda w.w)(\lambda w.w))\\
											& \breduce (\lambda x.y)(\lambda w.w)
											& \breduce y
\end{align*}

\item Another reduction of the same expression would be:

\begin{align*}
(\lambda x.y)((\lambda z.zz)(\lambda w.w)) & \breduce y
\end{align*}

\item The first is called the \uterm{applicative order} reduction; the second is
called the \uterm{normal order} reduction.

\item Reduce the following expressions:
\begin{align*}
(\lambda x. m x)j\\
(\lambda y. y j)m\\
(\lambda x.\lambda y. y(y x))jm\\
(\lambda y.y j)(\lambda x. m x)\\
(\lambda x. xx)(\lambda y. yyy)
\end{align*}
}

% \item Reduce the following expression in both applicative and normal order:
%
% \begin{align*}
% (\lambda p_1\lambda p_2. p_1 0 \land p_2)(\lambda x. x\not= 0)((\lambda y.\frac{5}{y} = 0)0)
% \end{align*}

\section{Lambda calculus in action: some examples}

\subsection{Logic}
\ezimeti{
\item Let's define the truth values:

\begin{align*}
\combf{T} \equiv \lambda x\lambda y.x\\
\combf{F} \equiv \lambda x\lambda y.y
\end{align*}

\item Verify that $\lambda x\lambda y.yxy$ behaves like \emph{and} in prefix
notation (i.e. $\land p q$).

\item Can you think of lambda expressions for $\lor$ and $-$?
\item What about an if-then-else function that applies to the test, a function
to execute if the test is true and a function to execute if the test is false.

}
\subsection{Arithmetic}

\ezimeti{

\item Number can be represented as lambda expressions in the following way:
\begin{align*}
0 & \equiv \lambda f\lambda x.x\\
1 & \equiv \lambda f\lambda x.fx\\
2 & \equiv \lambda f\lambda x.f(fx)\\
3 & \equiv \lambda f\lambda x.f(f(fx))\\
\vdots
\end{align*}

\item A successor function which returns $n+1$ given $n$ is:
\begin{align*}
\mathbf{S}\equiv \lambda a\lambda f\lambda x. f(afx)
\end{align*}

\item Here are addition and multiplication (again in prefix notation); verify
that they do what they are meant to do.

\begin{align*}
\mathbf{+} & \equiv \lambda a\lambda b\lambda f\lambda x.af(bfx)\\
\mathbf{\times} & \equiv \lambda a\lambda b\lambda f.a(bf)
\end{align*}

}

\section{Lambda Calculus in LISP}

We will adopt \href{https://slideplayer.com/slide/11363727/}{the implementation} provided by \href{https://es-static.fbk.eu/people/cimatti/}{Alessandro Cimatti}, with some minor modifications.

\bibliographystyle{plain}
\bibliography{ozge}
\end{document}


%-------------------------------------------------------------------------------
\section{Gradient Analysis Framework}
%-------------------------------------------------------------------------------

The gradient analysis framework works similarly to autodifferentiation, computing the gradient of each operation and using the results as inputs to the next gradient. However, unlike autodifferentiation, nonsmooth functions are handled by Proximal Gradients, so that a meaningful gradient can be computed over all program operations. Since Proximal Gradients require local Lipschitz Constants to bound their sampling in some cases, Lipschitz Constants are also propogated over the program along with the gradient.

\noindent \textbf{Gradient Propogation with Chain Rule.} We model a program as an arbitrary function $P$, that operates on the system state $x$ and produces a modified system state $x'$. Since programs are generally nonsmooth, we can use subgradients to approximate the gradient of $P$. Programs in general are too complex to model, but since the program $P$ is composed of individual operations whose behavior is known, we model $P$ as a composition of $N$ individual functions on the system state that represent each operation in the program:

\vspace{-10pt}\begin{align*}
P(x) = P_N \circ P_{N-1} \circ \cdots \circ P_2 \circ P_1 (x)
\end{align*}

Subgradients compose under the chain rule, so we apply the same approach used in Automatic Differentation to evaluate the subgradient of $P$,  $\nabla_{sub} P(x)$ by computing the product of the subgradients of each operation in $P$ ~\cite{rockafellar2009variational}.

\vspace{-10pt}\begin{align*}
  \nabla_{sub} P(x) = \nabla_{sub} P_N * \nabla_{sub} P_{N-1} * \cdots * \nabla_{sub} P_2 * \nabla_{sub} P_1
\end{align*}

This is the same approach used in Automatic Differentiation, but applied to discrete functions and subgradients instead of continuous functions with gradients. Then, instead of attempting to evaluate a subgradient over the program function $P$, which has unknown behavior and operate on a large number of variables, subgradients can be evaluated on the individual operations $P_i$, which have known behavior and usually only operate on 1 or 2 variables.

For some operations that are analytically differentiable on continuous variables, the subgradient of the operation on discrete variables can also be computed analytically. However, the subgradients of nonsmooth operations must be approximated with Proximal Gradients.

\noindent \textbf{Sampling Bounds with Lipschitz Constants.} In order to bound the samples required to evaluate the Proximal Gradient, Lipschitz Bounds must also be known for each operation. Fortunately, Lipschitz Bounds can be computed for each type of nonsmooth operation in a program based on its behavior and the Lipschitz bounds of its input. This means that in addition to propogating the gradient through a program, we also compute the Lipschitz Constant on each operation based on the Lipschitz Constants of its inputs, and propogate the result to the next function.

In some cases the Lipschitz Constant $K_f$ cannot be derived from knowledge of the function, so it is estimated by sampling the function at $x \pm K$ and taking the maximum difference.

\noindent \textbf{Partial Derivatives.} A gradient is composed of partial derivatives with regard to each part of the input, so when defining rules for computing the gradient we focus on partial derivatives with regard to the variable in the system state that was first marked for tracking, which is denoted $x_i$. Typically $x_i$ is a byte in an input read from a file or socket, but may also be an internal program variable.

\noindent \textbf{Program Inputs.} In the case where $g$ and $h$ are direct inputs to the program, such as bytes read from a file, we consider them to be identity functions with a partial derivative of 1 if they operate on the byte of the input that the partial derivative is being taken with regard to, and 0 otherwise. The Lipschitz Constant in this case is also 1.

\noindent \textbf{Propogation rules.} We then define gradient and Lipschitz Constant propogation rules as follows, where $f$ represents the current operation and $g$ and $h$ represent the prior operations on its input.

\begin{enumerate}
  \item \textbf{Addition and Multiplication.} Addition and multiplication, as well as subtraction, are examples of discrete functions whose subgradients can still be computed analytically and therefore do not require sampling. The partial derivatives and associated Lipschitz Constants with regard to the $i$th input variable $x_i$ can then be computed as follows based on the definition of $f$ as $f\left(g, h\right) = g+h$:

\vspace{-10pt}\begin{align*}
  K_f &= K_{g} + K_{h} &\tfrac{df}{dx_i} &= \tfrac{dg}{dx_i} + \tfrac{dh}{dx_i}
\end{align*}

Multiplication can likewise rely on analytical derivatives as it is a linear operation:

\vspace{-10pt}\begin{align*}
  K_f &= h*K_{g} + g*K_{h} & \frac{df}{dx_i} &= h\frac{dg}{dx_i} + g\frac{dh}{dx_i}
\end{align*}

    \gabe{These are pretty obvious, textual description may be sufficient.}

\item \textbf{Integer Division.} Unlike division with continuous values, division on integers is not a linear function because any fractional portion of the result is truncated. Therefore sampling the function is necessary to evaluate an accurate partial derivative. To compute the Lipschitz Constant, we use the quotient rule and consider the case that causes the maximum possible change in $f$, where $\tfrac{dg}{dx_i} = K_g$, and $\tfrac{dh}{dx_i} = -K_h$.

\vspace{-10pt}\begin{align*}
  K_f &= \frac{K_g h + K_h g}{h^2} &
  \frac{df}{dxi} &= prox_{\nabla f}\left(\bar{x}, K_f, \lambda\right)
\end{align*}

\item \textbf{Modulo Operations.} Like integer division, modulo operations are also generally nonsmooth and require evaluating the proximal gradient. In cases the modulo operand is constant, which we found usually the case in our test programs, the Lipschitz Constant for the modulo function is simply the modulo operand:

\vspace{-10pt}\begin{align*}
  K_f &= h &
  \frac{df}{dxi} = prox_{\nabla f}\left(\bar{x}, K_f, \lambda\right)
\end{align*}

In cases where the derivative of the modulo operand is nonzero, the Lipschitz Constant is estimated by sampling and used to evaluate the Proximal Gradient.

\item \textbf{Shift Operations.} Shift operations can be defined as a multiplication with an exponent, but have the additional condition that bits shifted past the width of the datatype are dropped. Therefore the Lipschitz Constant is also estimated by sampling and used to evaluate the Proximal Gradient.

  \gabe{May want to combine integer division, modulo and shift.}

\item \textbf{Bitwise Operations.} When one of the operands of a bitwise operation is a constant, or at least has a 0 derivative with regard the marked input, the Lipschitz Constant can be set the value of the highest set bit in the constant operand. However, in cases where both operands have nonzero derivatives, the Lipschitz Constant must be estimated and used to evaluate the Proximal Gradient.

\item \textbf{Memory Indexing.} Indexing into an array in memory can be modeled as an arbitrary nonsmooth function $f\left(g\right)$. Since we cannot derive the Lipschitz Constant $K_f$ from knowledge of the function, we estimate it by sampling the function at $g \pm g'$ and taking the maximum difference. The proximal gradient can be computed using the estimated Lipschitz Constant

%\begin{align*}
  %f\left(g\right) &= \textrm{mem}\left[g\right]\\
  %\frac{df}{dxi} &= prox_{\nabla f}\left(\bar{x}, K_f, \lambda\right)
%\end{align*}

\item \textbf{Load Operations.} Load operations will normally take on the saved partial derivative of whatever variable was originally saved to that memory location. However, there is possibility that one or more types with smaller bit widths are being loaded into a single larger width type. In these cases each partial derivative and Lipschitz Constant associated with an address being loaded, if there are more than one, is shifted by its offset with the base address and summed with the others.

\vspace{-10pt}\begin{align*}
  K_f &= \sum_j K_j 2^{offset_j} &
  \frac{df}{dxi} &= \sum_j \frac{d \textrm{addr}_j}{dx_i}2^{offset_j}
\end{align*}

\item \textbf{Integer Branches.} In cases where a variable can take on different values based on which portion of the branch was executed, we model the branch and subsequent merge as either a piecewise point function or a piecewise step function, depending on if the branch comparison defines a single value or set of values, such as greater than. In order to the Proximal Gradient on this function, the alternate branch path must be sampled to determine its value in the merge operation. In practice any execution of an alternate path will be invalid, but for many branches will still result in an accurate representation of program behavior. In cases where the sample execution does not return to the merge operator, the alternate branch portion of the piecewise function is considered to be undefined, resulting in a partial deriviative of 0. Once the path has been sampled, the Proximal Gradient can be evaluated on it normally, and the associated Lipschitz Constant can be set to the size of the step in the piecewise function.

  This approach will handle most single branches, but breaks down in the case where multiple nested branches on different values must be bypassed for the merge variable to be set. To handle these cases, when sampling alternate branch paths any subsequent branch encountered can also be sampled, and the Proximal Gradient computed over each branch individually. Figure \ref{fig:branch_exs} shows how this process works on nested branches with two conditions, \tc{x>1} and \tc{set>0}. When the branch is initially executed with \tc{x=0} and \tc{set=0}, the result is \tc{y=0}. To evaluate Proximal Gradient, the first branch is sampled by executing the alternate path with \tc{x=2}. This execution also results in \tc{y=0}, but during it the second nested branch on \tc{set} is encountered and sampled with \tc{set=1}, which results in \tc{y=1}. These samples are then used to form a function that is 1 for \tc{x>1} and \tc{set>0} and 0 elsewhere. The closest point to \tc{x=0, set=0} that gives the greatest change on this function is \tc{x=2, set=1}, so the Proximal Gradient evaluates on that point, resulting in partial derivatives of $\tfrac{dy}{dx}=\tfrac{1}{2}$ and $\tfrac{dy}{dset} = 1$.

\begin{figure}
  %\centering
  \begin{subfigure}{0.38\columnwidth}
    %\includegraphics[width=\linewidth]{figs/sosp-working_ex3}
    \begin{lstlisting}
// int x=0
// bool set=0
int y=0;
if (x>1)
    if (set==1)
        y=1;
    \end{lstlisting}
    %\caption{\label{fig:branch_ex1}Example of nested branch with $\tfrac{dy}{dx}=\tfrac{1}{2}$ and $\tfrac{dy}{dset} = 1$}
  \end{subfigure}
  \hspace{0.1cm}
  \begin{subfigure}{0.58\columnwidth}
    \includegraphics[width=\textwidth]{figs/nested_branch_plot.jpg}
    %\caption{\label{fig:branch_ex2}Surface}
  \end{subfigure}
  \vspace{-0pt}
  \caption{\label{fig:branch_exs}Example of nested branch with $\tfrac{dy}{dx}=\tfrac{1}{2}$ and $\tfrac{dy}{dset} = 1$.}
  \vspace{-0pt}
\end{figure}

\item \textbf{Floating Point Branches.} Unlike derivatives of integer operations, derivatives of floating point operations can generally be computed analytically, as is done in existing autodifferentiation frameworks. The exception to this is branches, which are normally not handled. Like integer branches, we model branches with floating point values as a piecewise step function, and execute the alternate branch path to determine the value of the step. However, unlike integer branches, floating point branches are not locally lipschitz, because they have an infinite slope at the point of discontinuity \gabe{add a figure showing this}, so a Proximal Gradient cannot be evaluated on them. Instead, we apply a Gaussian smoothing function, where the variance of the gaussian used is set according the magnitude of the input derivative.
\end{enumerate}


\chapter{Assignment: Clustering}
\label{hw:clustering}

\newthought{Clustering helps to discover groups of data}, for example similar countries based on certain socio-economic features. For this task, we will use \textit{HDI} data set from the \widget{Datasets} widget. The data reports the Human Development Index for the year 2016 for 188 countries. While HDI has its limitations, the data offers an interesting exercise in clustering.

\begin{figure}[h]
  \centering
  \includegraphics[width=\linewidth]{workflow.png}%
  \caption{Workflow for the assignment.}
\end{figure}

\begin{enumerate}
    \item Try Euclidean and cosine distance with Ward linkage. Which one works better? Why?
    \item How many groups did you discover? What number would make sense?
    \item Explain the final clusters. What defines each of them?
    \item Use Euclidean distance and Ward linkage. Can you explain why is Cuba clustered together with South Korea and the United Stated? Use \widget{Box Plot} and the Data output from Hierarchical Clustering to answer this question.
\end{enumerate}

\begin{wrapfigure}{o}{1.0\textwidth}
  \includegraphics[scale=0.4]{clustering.png}
\end{wrapfigure}