%!TEX root = ../OGUSAdoc.tex

Three markets must clear in \ogindia---the labor market, the capital market, and the goods market. By Walras' Law, we only need to use two of those market clearing conditions because the third one is redundant. In the model, we choose to use the labor market clearing condition and the capital market clearing condition, and to ignore the goods market clearing condition. But we present all three market clearing conditions here. Further, the redundant goods market clearing condition---sometimes referred to as the resource constraint---makes for a nice check on the solution method to see if everything worked.

We also characterize here the law of motion for total bequests $BQ_t$. Although it is not technically a market clearing condition, one could think of the bequests law of motion as the bequests market clearing condition.

\section{Market Clearing Conditions}\label{SecMarkClrMktClr}

  Labor market clearing \eqref{EqMarkClrLab} requires that aggregate labor demand $L_t$ measured in efficiency units equal the sum of household efficiency labor supplied $e_{j,s}n_{j,s,t}$.
  \begin{equation}\label{EqMarkClrLab}
    L_t = \sum_{s=E+1}^{E+S}\sum_{j=1}^{J} \omega_{s,t}\lambda_j e_{j,s}n_{j,s,t} \quad \forall t
  \end{equation}
  Capital market clearing \eqref{EqMarkClrCap} requires that aggregate capital demand from firms $K_t$ and from the government $D_t$ equal the sum of capital savings and investment by households $b_{j,s,t}$.
  \begin{equation}\label{EqMarkClrCap}
    K_t + D_t = \sum_{s=E+2}^{E+S+1}\sum_{j=1}^{J}\Bigl(\omega_{s-1,t-1}\lambda_j b_{j,s,t} + i_s\omega_{s,t-1}\lambda_j b_{j,s,t}\Bigr) \quad \forall t
  \end{equation}
  Note that the capital demand side of the capital market clearing equation \eqref{EqMarkClrCap} includes both capital demand by firms $K_t$ and capital demand by government $D_t$. It is here that we can see the potential of government deficits to crowd out investment.

  Aggregate consumption $C_t$ is defined as the sum of all household consumptions, and aggregate investment is defined by the resource constraint $Y_t = C_t + I_t + G_t$ as shown in \eqref{EqMarkClrGoods}.
  \begin{equation}\label{EqMarkClrGoods}
    \begin{split}
      Y_t &= C_t + K_{t+1} - \biggl(\sum_{s=E+2}^{E+S+1}\sum_{j=1}^{J}i_s\omega_{s,t}\lambda_j b_{j,s,t+1}\biggr) - (1-\delta)K_t + G_t \quad\forall t \\
      &\quad\text{where}\quad C_t \equiv \sum_{s=E+1}^{E+S}\sum_{j=1}^{J}\omega_{s,t}\lambda_j c_{j,s,t}
    \end{split}
  \end{equation}

  Note that the extra terms with the immigration rate $i_s$ in the capital market clearing equation \eqref{EqMarkClrCap} and the resource constraint \eqref{EqMarkClrGoods} accounts for the assumption that age-$s$ immigrants in period $t$ bring with them (or take with them in the case of out-migration) the same amount of capital as their domestic counterparts of the same age. Note also that the term in parentheses with immigration rates $i_s$ in the sum acts is equivalent to a net exports term in the standard equation $Y=C+I+G+NX$. That is, if immigration rates are positive, then immigrants are bringing capital into the country and the term in parentheses has a negative sign in front of it. Negative exports are imports.

\section{Total Bequests Law of Motion}\label{SecMarkClrBQ}

  Total bequests $BQ_t$ are the collection of savings of household from the previous period who died at the end of the period. These savings are augmented by the interest rate because they are returned after being invested in the production process.
  \begin{equation}\label{EqMarkClrBQ}
    BQ_{t} = (1+r_{t})\left(\sum_{s=E+2}^{E+S+1}\sum_{j=1}^J\rho_{s-1}\lambda_j\omega_{s-1,t-1}b_{j,s,t}\right) \quad\forall t
  \end{equation}
  Because the form of the period utility function in \eqref{EqHHPerUtil} ensures that $b_{j,s,t}>0$ for all $j$, $s$, and $t$, total bequests will always be positive $BQ_{j,t}>0$ for all $j$ and $t$.


 \documentclass[a4paper,11pt]{article}

\usepackage{fancyhdr}
\usepackage[dvips]{graphicx}  % for eps and MatLab Diagrams
\usepackage{amsmath}
\usepackage{psfrag,color}
\usepackage[framed]{/Applications/TeX/mcode}

\pagestyle{fancy}

\begin{document} % Begin Document

% Title
\title{\textsc{DERIVEST}}

% Authors and Contact Information
\author{\textbf{John R. D'Errico}\\
Email: woodchips@rochester.rr.com}

\maketitle

\section{Introduction - Derivative Estimation}

The general problem of differentiation of a function typically pops up in
three ways in Matlab.

\begin{itemize}
  \item The symbolic derivative of a function.
  \item Compute numerical derivatives of a function defined only by a sequence of data points.
  \item Compute numerical derivatives of a analytically supplied function.
\end{itemize}

Clearly the first member of this list is the domain of the symbolic toolbox, or some
set of symbolic tools. Numerical differentiation of a function defined by data points
can be achieved with the function gradient, or perhaps by differentiation of a curve fit
to the data, perhaps to an interpolating spline or a least squares spline fit.

The third class of differentiation problems is where \mcode{DERIVEST} is valuable. This
document will describe the methods used in \mcode{DERIVEST}.

\bigskip

\section{Numerical differentiation of a general function of one variable}

Surely you recall the traditional definition of a derivative, in terms of a limit.

\begin{equation} \tag{1}
   f'(x) = \lim_{\delta \to 0}{\frac{f(x+\delta) - f(x)}{\delta}}
\end{equation}

For small $\delta$, the limit approaches $f'(x)$. This is a one-sided approximation for
the derivative. For a fixed value of $\delta$, this is also known as a finite difference
approximation (a forward difference.) Other approximations for the derivative are also
available. We will see the origin of these approximations in the Taylor series expansion
of a function $f(x)$ around some point $x_0$.

\begin{multline} \tag{2}
   f(x) = f(x_0) + (x - x_0)f'(x_0) + \frac{(x - x_0)^2}{2} f''(x_0) + \\
   \frac{(x - x_0)^3}{6} f^{(3)}(x_0) + \frac{(x - x_0)^4}{24} f^{(4)}(x_0) + \\
   \frac{(x - x_0)^5}{120} f^{(5)}(x_0) + \frac{(x - x_0)^6}{720} f^{(6)}(x_0) +...
\end{multline}

Truncate the series in (2) to the first three terms, then form the forward difference
approximation (1), where $x = x_0 + \delta$.

\begin{equation} \tag{3}
   f'(x_0) = \frac{f(x_0+\delta) - f(x_0)}{\delta} - \frac{\delta}{2} f''(x_0) - \frac{\delta^2}{6} f'''(x_0) + ...
\end{equation}

When $\delta$ is small, $\delta^2$ and any higher powers are vanishingly small. So we tend
to ignore those higher powers, and describe the approximation in (3) as a "first" order
approximation since the error in this approximation approaches zero at the same rate as the first power of $\delta$. \footnote{We would normally write these additional terms using O() notation, where all that matters is that the error term is $O(\delta)$ or perhaps $O(\delta^2)$, but explicit understanding of these error terms will be useful in the Romberg extrapolation step later on.} The values of $f''(x_0)$ and $f'''(x_0)$, while unknown to us, are fixed constants as $\delta$ varies.

Higher order approximations arise in the same fashion. The central difference (4) is a second
order approximation.

\begin{equation} \tag{4}
   f'(x_0) = \frac{f(x_0+\delta) - f(x_0-\delta)}{2\delta} - \frac{\delta^2}{3} f'''(x_0) + ...
\end{equation}

\bigskip

\section{Unequally spaced finite difference rules}

While most finite difference rules used to differentiate a function will use equally spaced points,
this fails to be appropriate when one does not know the final spacing. Adaptive quadrature
rules can succeed by subdividing each sub-interval as necessary. But an adaptive
differentiation scheme must work differently, since differentiation is a point estimate.
\mcode{DERIVEST} generates a sequence of sample points that follow a log spacing away
from the point in question, then it uses a single rule (generated on the fly) to estimate the
desired derivative. Because the points are log spaced, the same rule applies at any scale,
with only a scale factor applied.

\bigskip

\section{Odd and even transformations of a function}

Returning to the Taylor series expansion of $f(x)$ around some point $x_0$, an even function \footnote{An even function is one which expresses an even symmetry around a given point. An even symmetry has the property that $f(x) = f(-x)$. Likewise, an odd function expresses an odd symmetry, wherein $f(x) = -f(-x)$.} around $x_0$ must have all the odd order derivatives vanish at $x_0$. An odd function has all its even derivatives vanish from its expansion. Consider the derived functions $f_{odd}(x)$ and $f_{even}(x)$.

\begin{equation} \tag{5}
f_{odd}(x) = \frac{f(x - x_0) - f(-x - x_0)}{2}
\end{equation}

The Taylor series expansion of $f_{odd}(x)$ has the useful property that we have killed off any even order terms, but the odd order terms are identical to $f(x)$, as expanded around $x_0$.

\begin{multline} \tag{6}
f_{odd}(x) = (x - x_0)f'(x_0) + \frac{(x - x_0)^3}{6} f^{(3)}(x_0) + \\
\frac{(x - x_0)^5}{120} f^{(5)}(x_0) + \frac{(x - x_0)^7}{5040} f^{(7)}(x_0) +...
\end{multline}

Likewise, $f_{even}(x)$ has no odd order terms or a constant term, but other even order terms that are
identical to $f(x)$.

\begin{equation} \tag{7}
f_{even}(x) = \frac{f(-x-x_0) - 2f(x_0) + f(x-x_0)}{2}
\end{equation}

\begin{multline} \tag{8}
f_{even}(x) = \frac{(x - x_0)^2}{2} f^{(2)}(x_0) + \frac{(x - x_0)^4}{24} f^{(4)}(x_0) + \\
 \frac{(x - x_0)^6}{720} f^{(6)}(x_0) + \frac{(x - x_0)^8}{40320} f^{(8)}(x_0) + ...
\end{multline}

The point of these transformations is we can rather simply generate a higher order approximation
for any odd order derivatives of $f(x)$ by working with $f_{odd}(x)$. Even order derivatives of $f(x)$ are similarly generated from $f_{even}(x)$. For example, a second order approximation for $f'(x_0)$ is trivially written in (9) as a function of $\delta$.

\begin{equation} \tag{9}
   f'(x_0; \delta) = \frac{f_{odd}(x_0 + \delta)}{\delta} - \frac{\delta^2}{6} f^{(3)}(x_0)
\end{equation}

We can do better rather simply, so why not? (10) shows a fourth order approximation for $f'(x_0)$.

\begin{equation} \tag{10}
   f'(x_0; \delta) = \frac{8 f_{odd}(x_0+\delta)-f_{odd}(x_0+2\delta)}{6\delta} + \frac{\delta^4}{30} f^{(5)}(x_0)
\end{equation}

Again, the next non-zero term (11) in that expansion has a higher power of $\delta$ on it, so we
would normally ignore it since the lowest order neglected term should dominate the behavior
for small $\delta$.

\begin{equation} \tag{11}
  \frac{\delta^6}{252} f^{(7)}(x_0)
\end{equation}

\mcode{DERIVEST} uses similar approximations for all derivatives of $f$ up to the fourth order.
Of course, its not always possible for evaluation of a function on both sides of a point, as central difference rules will require. In these cases, you can specify forward or backward difference rules
as appropriate.

\bigskip

\section{Romberg extrapolation methodology applied to derivative estimation}

Some individuals might suggest that the above set of approximations are entirely adequate for
any sane person. Can we do better?

Suppose we were to generate several different estimates of the approximation in (3) for
different values of $\delta$ at a fixed $x_0$. Thus, choose a single $\delta$, estimate a
corresponding resulting approximation to $f'(x_0)$, then do the same for $\delta/2$.
If we assume that the error drops off linearly as $\delta \to 0$, then it is a simple matter
to extrapolate this process to a zero step size. Our lack of knowledge of $f''(x_0)$ is
irrelevant. All that matters is $\delta$ is small enough that the linear term dominates so we
can ignore the quadratic term, therefore the error is purely linear.

\begin{equation} \tag{12}
   f'(x_0) = \frac{f(x_0+\delta) - f(x_0)}{\delta} - \frac{\delta}{2} f''(x_0)
\end{equation}

The linear extrapolant for this interval halving scheme as $\delta \to 0$ is given by (13).

\begin{equation} \tag{13}
   f'_0 = 2f'_\delta - f'_{\delta/2}
\end{equation}

Since I've always been a big fan of convincing myself that something will work before I
proceed too far, lets try this out in Matlab. Consider the function $e^x$. Generate a pair of
approximations to $f'(0)$, once at $\delta$ of 0.1, and the second approximation at $1/2$
that value. Recall that $\frac{d(e^x)}{dx} = e^x$, so at x = 0, the derivative should be
exactly 1. How well will we do?

\begin{lstlisting}
>> format long g

>> f = @(x) exp(x);
>> del = 0.1;

>> df1 = (f(del) - f(0))/del
df1 =
          1.05170918075648

>> df2 = (f(del/2) - f(0))/(del/2)
df2 =
          1.02542192752048

>> 2*df2 - df1
ans =
         0.999134674284488
\end{lstlisting}

In fact, this worked very nicely, reducing the error to roughly 1 percent of our initial estimates.
Should we be surprised at this reduction? Not if we recall that last term in (3). We saw there that
the next term in the expansion was $O(\delta^2)$. Since $\delta$ was 0.1 in our experiment, that
1 percent number makes perfect sense.

The Romberg extrapolant in (13) assumed a linear process, with a specific reduction in $\delta$
by a factor of 2. Assume the two term (linear + quadratic) residual term in (3), evaluating our approximation there with a third value of $\delta$. Again, assume the step size is cut in half again.
The three term Romberg extrapolant is given by (14).

\begin{equation} \tag{14}
   f'_0 = \frac{1}{3}f'_\delta - 2f'_{\delta/2} + \frac{8}{3}f'_{\delta/4}
\end{equation}

A quick test in matlab yields much better results yet.

\begin{lstlisting}
>> format long g
>> f = @(x) exp(x);
>> del = 0.1;

>> df1 = (f(del) - f(0))/del
df1 =
          1.05170918075648

>> df2 = (f(del/2) - f(0))/(del/2)
df2 =
          1.02542192752048

>> df3 = (f(del/4) - f(0))/(del/4)
df3 =
          1.01260482097715

>> 1/3*df1 - 2*df2 + 8/3*df3
ans =
          1.00000539448361
\end{lstlisting}

Again, \mcode{DERIVEST} uses the appropriate multiple term Romberg extrapolants for all derivatives
of $f$ up to the fourth order. This, combined with the use of high order approximations for the derivatives, allows the use of quite large step sizes.

\bigskip

\section{Uncertainty estimates for DERIVEST}

We can view the Romberg extrapolation step as a polynomial curve fit in the step size parameter
$\delta$. Our desired extrapolated value is seen as simply the constant term coefficient in that polynomial model. Remember though, this polynomial model (see (10) and (11)) has only a few
terms in it with known non-zero coefficients. That is, we will expect a constant term $a_0$, a term
of the form $a_1 \delta^4$, and a third term $a_2 \delta^6$.

A neat trick to compute the "statistical" uncertainty in the estimate of our desired derivative is to
use statistical methodology for that error estimate. While I do appreciate that there is nothing
truly statistical or stochastic in this estimate, the approach still works nicely, providing a very reasonable estimate in practice. A three term Romberg-like extrapolant, then evaluated at four distinct values for $\delta$, will yield an estimate of the standard error of the constant term, with one spare degree of freedom. The uncertainty is then derived by multiplying that standard error by the appropriate percentile from the Students-t distribution.

\begin{lstlisting}
>> tcdf(12.7062047361747,1)
ans =
                     0.975
\end{lstlisting}

This critical level will yield a two-sided confidence interval of 95 percent.

These error estimates are also of value in a difference sense. Since they are efficiently generated
at all the different scales, the particular spacing which yields the minimum predicted error is chosen
as the best derivative estimate. This has been shown to work consistently well. A spacing too large
tends to have large errors of approximation due to the finite difference schemes used. But a too
small spacing is bad also, in that we see a significant amplification of least significant fit errors
in the approximation. A middle value generally seems to yield quite good results. For example,
\mcode{DERIVEST} will estimate the derivative of $e^x$ automatically. As we see, the final overall
spacing used was 0.1953125.

\begin{lstlisting}
>> [d,e,del]=derivest(@(x) exp(x),1)
d =
          2.71828182845904
e =
      1.02015503167879e-14
del =
                 0.1953125
\end{lstlisting}

However, if we force the step size to be artificially large, then approximation error takes over.

\begin{lstlisting}
>> [d,e,del]=derivest(@(x) exp(x),1,'FixedStep',10)
d =
           2.3854987890005
e =
          3.90016042034995
del =
        10
\end{lstlisting}

And if the step size is forced to be too small, then we see noise dominate the problem.

\begin{lstlisting}
>> [d,e,del]=derivest(@(x) exp(x),1,'FixedStep',.0000000001)
d =
          2.71826406220403
e =
      0.000327191484277048
del =
                     1e-10
\end{lstlisting}

\mcode{DERIVEST}, like Goldilocks in the fairy tale bearing her name, stays comfortably in
the middle ground.

\bigskip

\section{DERIVEST in action}

How does \mcode{DERIVEST} work in action? A simple nonlinear function with a well
known derivative is $e^x$. At $x = 0$, the derivative should be 1.

\begin{lstlisting}
>> [d,err] = derivest(@(x) exp(x),0)
d =
         0.999999999999997

err =
      2.22066469352214e-14
\end{lstlisting}

A second simple example comes from trig functions. The first four derivatives of the sine
function, evaluated at $x = 0$, should be respectively $[cos(0), -sin(0), -cos(0), sin(0)]$,
or $[1,0,-1,0]$.

\begin{lstlisting}
>> d = derivest(@(x) sin(x),0,1)
d =
         0.999999999999999

>> d = derivest(@(x) sin(x),0,2)
d =
     0

>> d = derivest(@(x) sin(x),0,3)
d =
         -1.00000000000046

>> d = derivest(@(x) sin(x),0,4)
d =
     0
\end{lstlisting}

\bigskip

\section{Gradient (\mcode{GRADEST}) and Hessian (\mcode{HESSIAN}) estimation}

Estimation of the gradient vector (\mcode{GRADEST}) of a function of multiple variables is a
simple task, requiring merely repeated calls to \mcode{DERIVEST}. Likewise, the diagonal
elements of the hessian matrix are merely pure second partial derivatives of a function.
\mcode{HESSDIAG} accomplishes this task, again calling \mcode{DERIVEST} multiple
times. Efficient computation of the off-diagonal (mixed partial derivative) elements of the
Hessian matrix uses a scheme much like that of \mcode{DERIVEST}, wherein
\mcode{DERIVEST} is called to determine an initial step size, then Romberg extrapolation
is used to improve a set of second order finite difference estimates of those mixed partials.

\bigskip

\section{Conclusion}

\mcode{DERIVEST} is an a adaptive scheme that can compute the derivative of arbitrary
(well behaved) functions. It is reasonably fast as an adaptive method. Many options have
been provided for the user who wishes the ultimate amount of control over the estimation.

\bigskip

\section{Acknowledgments}

My thanks are due to Shaun Simmons for convincing me to learn enough
LaTeX to write this document.

\bigskip

\begin{thebibliography}{3}

\bibitem{LM66} Lyness, J. M., Moler, C. B. (1966).
\newblock Vandermonde Systems and Numerical Differentiation.
\newblock \emph{Numerische Mathematik}.

\bibitem{LM69} Lyness, J. M., Moler, C. B. (1969).
\newblock Generalized Romberg Methods for Integrals of Derivatives.
\newblock \emph{Numerische Mathematik}.

\bibitem{LM66} \emph{NAG Library}.
\newblock NAG Fortran Library Document: D04AAF

\end{thebibliography}

\end{document}


\subsection{Classification of Riemann Surfaces}
We roughly classified all Riemann surfaces in the preceding section by viewing them as quotients of their universal covers, which can only be one of $\mathbb C_\infty,\mathbb C,\mathbb D$.
We say the surface is uniformised by its universal cover.
For some of these cases, we can do something better.
\begin{proposition}
    If a Riemann surface $R$ is uniformised by $\mathbb C_\infty$, then $R$ is conformally equivalent to $\mathbb C_\infty$.
\end{proposition}
\begin{proof}
    Suppose $R=G\backslash\mathbb C_\infty$, then we know that $G$ acts by conformal equivalences $\mathbb C_\infty\to\mathbb C_\infty$.
    But this is just M\"obius transformations (from Example Sheet).
    However, any M\"obius transformation has at least one fixed point, but $G$ should act freely, hence necessarily $G$ is trivial and hence $R\cong\mathbb C_\infty$.
\end{proof}
What about $\mathbb C$?
\begin{proposition}
    If a Riemann surface $R$ is uniformised by $\mathbb C$, i.e. $R\cong G\backslash\mathbb C$, then one of the following holds:\\
    (i) $G$ is trivial and $R\cong\mathbb C$.\\
    (ii) $G\cong\mathbb Z$ and $R\cong\mathbb C_\star$.\\
    (iii) $G\cong\mathbb Z^2$ and $R\cong\mathbb C/\Lambda$ for a lattice $\Lambda$.
\end{proposition}
\begin{proof}
    The conformal automorphisms of $\mathbb C$ are simply the (nonconstant) linear maps $\{z\mapsto az+b:a\in\mathbb C_\star,b\in\mathbb C\}$ (again from Example Sheet).
    Which of them can $G$ act by?
    Note that if $a\neq 1$, then $z\mapsto az+b$ has a fixed point, therefore $G$ can only consist of translations.
    But then (Example Sheet again!) $G$ (identified by the values of $b$) can only be one of:\\
    (i) Trivial.\\
    (ii) $\langle\omega\rangle\cong\mathbb Z$ for some $\omega\neq 0$.\\
    (iii) a lattice $\Lambda\cong\mathbb Z^2$.\\
    And these corresponds to the three situations as stated.
\end{proof}
Oh, by the way, a surface cannot be uniformised by more than one of $\mathbb C_\infty,\mathbb C,\mathbb D$.
\begin{lemma}[Lifting Lemma]
    Let $f:R\to S$ be an analytic map of Riemann surfaces.
    Suppose $R$ is simply connected, and let $\pi:\tilde{S}\to S$ be the uniformising map of $S$, then there is an analytic map $F:R\to\tilde{S}$ such that $f=\pi\circ F$.
\end{lemma}
So we have the commutative diagram
\[
    \begin{tikzcd}
        &\tilde{S}\arrow{d}{\pi}\\
        R\arrow[dashed]{ur}{F}\arrow[swap]{r}{f}&S
    \end{tikzcd}
\]
\begin{proof}
    Example Sheet.
\end{proof}
\begin{proposition}
    A Riemann surface $R$ is uniformised by at most one of $\mathbb C_\infty,\mathbb C,\mathbb D$.
\end{proposition}
\begin{proof}
    By the previous discussion, we already know everything that is uniformised by $\mathbb C$ or $\mathbb C_\infty$ and they are distinct.
    Now suppose $R$ is uniformised by $\mathbb D$ and $\tilde{R}$ which is either $\mathbb C$ or $\mathbb C_\infty$.
    Let $\pi,f$ be the respective uniformisation maps, then by the preceding lemma, there is $F:\tilde{R}\to\mathbb D$ such that
    \[
        \begin{tikzcd}
            &\mathbb D\arrow{d}{\pi}\\
            \tilde{R}\arrow[swap]{r}{f}\arrow{ur}{F}&R
        \end{tikzcd}
    \]
    commutes.
    But then $F$ has to be constant by Liouville's Theorem, hence $f$ is also constant, contradiction.
\end{proof}
So any other Riemann surface must be uniformised by $\mathbb D$.
\begin{proposition}
    Any conformal automorphisms of $\mathbb D$ is in the form
    $$z\mapsto e^{i\theta}\frac{z-a}{1-\bar{a}z},a\in\mathbb C,\theta\in\mathbb R$$
\end{proposition}
\begin{proof}
    Complex Analysis.
\end{proof}
This is perhaps easier to picture if we send $\mathbb D$ to the open upper half plane $\mathbb H$ (by the M\"obius transformation $z\mapsto (1+iz)/(1-iz)$), which has automorphisms in the form $z\mapsto (az+b)/(cz+d)$ for $a,b,c,d\in\mathbb R,ad-bc=1$.
\begin{definition}
    A subgroup of $\operatorname{PSL}(\mathbb R)$ that acts properly discontinuously on $\mathbb H$ is called a Fuchsian group.
\end{definition}


\subsection{Abelian groups}\label{subsec:abelian_groups}

\begin{definition}\label{def:abelian_group}
  A \hyperref[def:magma/commutative]{commutative} \hyperref[def:group]{group} is usually called an \term{abelian group}. We denote by \( \cat{Ab} \) the category of abelian groups.

  By \fullref{thm:ring_is_integer_algebra}, the abelian groups are precisely the \hyperref[def:algebra_over_ring]{rings} over \( \BbbZ \), and we have an \hyperref[rem:category_similarity/isomorphism]{isomorphism of categories} \( \cat{Ab} \cong \cat{Mod}_\BbbZ \).
\end{definition}

\begin{remark}\label{rem:additive_magma}
  General groups often arise as \hyperref[def:automorphism_group]{automorphism groups}, which are, for the most part, non-commutative, while abelian groups are usually used as the main building block for \hyperref[def:ring]{rings} and \hyperref[def:module]{modules}.

  To make a further distinction, if the operation is denoted by \( \cdot \) or juxtaposition, we say that the group is a \term{multiplicative group}, and if the operation is denoted by \( + \), we say that the group is an \term{additive group}. This terminology usually, but not necessarily, coincides with the group (or, more generally, the \hyperref[def:magma]{magma}) being \hyperref[def:magma/commutative]{commutative}.

  To make things explicit, a \term{multiplicative magma} is any magma as defined in \fullref{def:magma}. Compare this to \term{additive magmas}, where
  \begin{thmenum}
    \thmitem{rem:additive_magma/addition} The magma operation is denoted by \( + \) and called \term{addition}.

    \thmitem{rem:additive_magma/multiplication} The magma \hyperref[def:magma/exponentiation]{exponentiation operation} \( x^n \) is denoted by \( n \cdot x \) or juxtaposition and called \term{multiplication}. Thus, multiplication is not defined for two elements of the magma, but defined for a positive integer and an element of the magma. That is,
    \begin{equation}\label{eq:rem:additive_magma/multiplication}
      \begin{aligned}
        &\cdot: \cdot: \BbbN \times R \to R \\
        &n \cdot x \coloneqq \begin{cases}
          0_M,           &n = 0, \T{initial condition if} M \T{is a monoid} \\
          x,             &n = 1, \T{initial condition if} M \T{is not a monoid} \\
          n \cdot x + x, &n > 1 \\
          -(n \cdot x),  &n < 0, \\
        \end{cases}
      \end{aligned}
    \end{equation}

    In the case of a \hyperref[def:magma/commutative]{commutative} \hyperref[def:monoid]{monoid}, if multiplication is extended to two elements of the monoid, we instead talk about \hyperref[def:semiring]{semirings}.

    \thmitem{rem:additive_magma/identity} The \hyperref[def:monoid]{identity} is usually denoted by \( 0 \).

    \thmitem{rem:additive_magma/inverse} If an \hyperref[def:monoid_inverse]{inverse} of \( x \) exists, it is denoted by \( -x \) rather than \( x^{-1} \).
  \end{thmenum}
\end{remark}

\begin{proposition}\label{thm:abelian_outer_automorphism_group}
  In an \hyperref[def:abelian_group]{abelian group}, the full \hyperref[def:automorphism_group]{automorphism group} \( \aut(G) \) is isomorphic to the \hyperref[def:inner_and_outer_automorphisms]{outer automorphism group} \( \op{out}(G) \).
\end{proposition}
\begin{proof}
  If the group operation is \hyperref[def:magma/commutative]{commutative}, then \( xyx^{-1} = yxx^{-1} = y \), which makes the \hyperref[def:inner_and_outer_automorphisms]{conjugation action} trivial. Thus, the \hyperref[def:inner_and_outer_automorphisms]{inner automorphism group} \( \op{int}(G) \) is trivial, and hence \( \aut(G) \cong \op{out}(G) \).
\end{proof}

\begin{proposition}\label{thm:abelian_normal_subgroups}
  All subgroups of an abelian group are \hyperref[thm:normal_subgroup_equivalences]{normal}.
\end{proposition}
\begin{proof}
  Let \( G \) be abelian and \( H \) be a subgroup of \( G \). Then \( x H x^{-1} = xx^{-1} H = H \) for any \( x \in G \) and thus \( H \) is normal.
\end{proof}

\begin{definition}\label{def:congruence_modulo_normal_subgroup}
  Given a \hyperref[thm:normal_subgroup_equivalences]{normal subgroup} \( N \) of an \hyperref[def:abelian_group]{abelian group} \( G \), we say that two elements \( x \) and \( y \) of \( G \) are \term{congruent modulo} \( N \) and write \( x \cong y \pmod N \) if \( x - y \in N \).

  If \( N = \braket{ z } \), this implies that \( x \cong y \pmod z \) if and only if \( x - y \in \braket{ z } \).

  This concept also extends to \hyperref[def:semiring_ideal]{ring ideals} rather than normal subgroups, in which case \( \braket{ z } \) is the \hyperref[def:semiring_ideal/generated]{ideal generated} by \( z \) rather than the \hyperref[def:cyclic_group]{cyclic subgroup} of \( z \).
\end{definition}

\begin{proposition}\label{thm:group_of_integers_modulo}
  The \hyperref[def:set_of_integers]{integers} \( \BbbZ \) form an abelian group under addition. For every positive integer \( n \), we define the group
  \begin{equation*}
    \BbbZ_n \coloneqq \set{ 0, 1, \ldots, n - 1 }
  \end{equation*}
  with the operation
  \begin{equation*}
    x \oplus y \coloneqq \rem(x + y, n)
  \end{equation*}
  so that
  \begin{equation*}
    x \oplus y \cong x + y \pmod n.
  \end{equation*}

  The group \( \BbbZ_n \) is called the \term{group of integers modulo} \( n \). Compare this result with \fullref{thm:ring_of_integers_modulo}.
\end{proposition}
\begin{proof}
  We will prove that \( \BbbZ_n \) is an abelian group.

  \SubProofOf[def:magma/associative]{associativity} Addition in \( \BbbZ_n \) is associative since
  \begin{balign*}
    (x \oplus y) \oplus z
    &=
    \rem((x \oplus y) + z, n)
    = \\ &=
    \rem(\rem(x + y, n) + z, n)
    = \\ &=
    \rem(x + y - n \quot(x + y, n) + z, n)
    = \\ &=
    \rem(x + y + z, n)
    = \\ &=
    \ldots
    = \\ &=
    x \oplus (y \oplus z).
  \end{balign*}

  \SubProofOf[def:monoid]{identity} The zero is the identity.

  \SubProofOf[def:monoid_inverse]{inverse} Fix \( x \in \BbbZ_n \). If \( x = 0 \), its inverse is \( 0 \). If \( x > 0 \), its inverse is \( n - x \) since \( n - x \in \BbbZ_n \) and
  \begin{equation*}
    x \oplus (n - x) = x + (n - x) - n = 0.
  \end{equation*}

  \SubProofOf[def:magma/commutative]{commutativity} Follows from
  \begin{equation*}
    x \oplus y
    =
    \rem(x + y, n)
    =
    \rem(y + x, n)
    =
    y \oplus x.
  \end{equation*}
\end{proof}

\begin{proposition}\label{thm:integers_modulo_isomorphic_to_quotient_group}
  The group \( \BbbZ_n \) of \hyperref[thm:group_of_integers_modulo]{integers modulo \( n \)} is isomorphic to the quotient of \( \BbbZ \) by \( n\BbbZ = \set{ nz \given z \in \BbbZ } \). That is,
  \begin{equation*}
    \BbbZ_n \cong \BbbZ / n\BbbZ.
  \end{equation*}
\end{proposition}
\begin{proof}
  Define the function
  \begin{align*}
    &\varphi: \BbbZ_n \to \BbbZ / n\BbbZ  \\
    &\varphi(x) \coloneqq x + n\BbbZ.
  \end{align*}

  It is a homomorphism because
  \begin{balign*}
    \varphi(x \oplus y)
    &=
    \varphi(\rem(x + y, n))
    = \\ &=
    \varphi(x + y - n \quot(x + y, n))
    = \\ &=
    x + y - n \quot(x + y, n) + n\BbbZ
    = \\ &=
    x + y + n\BbbZ
    = \\ &=
    (x + n\BbbZ) + (y + n\BbbZ)
    = \\ &=
    \varphi(x) + \varphi(y).
  \end{balign*}

  Furthermore, this shows that \( \varphi \) is also an isomorphism.
\end{proof}

\begin{example}\label{ex:lagranges_theorem_for_groups/direct_product_zn}
  \Fullref{thm:lagranges_theorem_for_groups} and \fullref{thm:integers_modulo_isomorphic_to_quotient_group} imply that, for any positive integer \( n \), \( (nm, k) \mapsto nm + k \) is a bijection between \( n \BbbZ \times \BbbZ_n \) and \( \BbbZ \). This bijection, however, is not necessarily a group isomorphism because \eqref{eq:def:magma/homomorphism} may not hold.

  Consider the tuples \( (nm_1, k_1) \) and \( (nm_2, k_2) \)  in \( n \BbbZ \times \BbbZ_n \). We have
  \begin{equation*}
    (nm_1, k_1) + (nm_2, k_2) = (nm_1 + nm_2, \rem(k_1 + k_2, n)).
  \end{equation*}

  Therefore, if \( k_1 + k_2 \geq n \),
  \begin{equation*}
    nm_1 + nm_2 + \rem(k_1 + k_2, n) < (nm_1 + k_1) + (nm_2 + k_2).
  \end{equation*}
\end{example}

\begin{proposition}\label{thm:cyclic_group_isomorphic_to_integers_modulo_n}
  The \hyperref[def:cyclic_group]{cyclic group} \( C_n \) is isomorphic to the group \hyperref[thm:group_of_integers_modulo]{\( \BbbZ_n \)} of integers modulo \( n \).
\end{proposition}
\begin{proof}
  The homomorphism
  \begin{equation*}
    \begin{aligned}
      &\varphi: \BbbZ_n \to C_n \\
      &\varphi(k) \coloneqq a^k,
    \end{aligned}
  \end{equation*}
  and the analogous homomorphism for the infinite group, are isomorphisms.
\end{proof}

\begin{definition}\label{def:monoid_grothendieck_completion}\mcite{nLab:grothendieck_group_of_a_commutative_monoid}
  Let \( M \) be a \hyperref[def:magma/commutative]{commutative} \hyperref[def:monoid]{monoid}. Define the \hyperref[def:equivalence_relation]{equivalence relation} \( \sim \) on tuples of members of \( M \) to hold for \( (a, b) \sim (a', b') \) if there exists an element \( u \) of \( M \) such that
  \begin{equation*}
    a + b' + u = a' + b + u.
  \end{equation*}

  Define addition on the \hyperref[thm:equivalence_partition]{equivalence partition} \( G \coloneqq (M \times M) / {\sim }\) componentwise as
  \begin{equation*}
    [(a, b)] \oplus [(c, d)] \coloneqq [(a + c, b + d)]
  \end{equation*}
  and fix a canonical embedding
  \begin{equation*}
    \begin{aligned}
      &\iota_M: M \to G \\
      &\iota_M(m) \coloneqq [(m, 0)].
    \end{aligned}
  \end{equation*}

  We call the obtained \hyperref[def:abelian_group]{abelian group} \( (G, \oplus) \) the \term{Grothendieck completion} of \( M \).
\end{definition}
\begin{defproof}
  \SubProof{Proof that \( \sim \) is an equivalence relation}
  \SubProofOf*[def:binary_relation/reflexive]{reflexivity}
  \begin{equation*}
    (a, b) \sim (a, b) \T{if and only if} a + b + 0 = a + b + 0
  \end{equation*}

  \SubProofOf*[def:binary_relation/symmetric]{symmetry} By commutativity, if \( (a, b) \sim (a', b') \), then there exists \( u \) such that
  \begin{equation*}
    a + b' + u = a' + b + u
    =
    a' + b + u = a + b' + u,
  \end{equation*}
  hence \( (a', b') \sim (a, b) \).

  \SubProofOf*[def:binary_relation/transitive]{transitivity} Suppose that \( (a, b) \sim (a', b') \) and \( (a', b') \sim (a^\dprime, b^\dprime) \). Thus, there exist elements \( u \) and \( b \) of \( M \) such that
  \begin{align*}
    a + b' + u         &= a' + b + u, \\
    a' + b^\dprime + v &= a^\dprime + b' + v.
  \end{align*}

  Summing both sides, we obtain
  \begin{equation*}
    (a + b' + u) + (a' + b^\dprime + v) = (a' + b + u) + (a^\dprime + b' + v)
  \end{equation*}

  We reorder both sides to obtain
  \begin{equation*}
    (a + b^\dprime) + (a' + b' + u + v) = (a^\dprime + b) + (a' + b' + u + v),
  \end{equation*}
  which implies \( (a, a^\dprime) \sim (b, b^\dprime) \).

  \SubProof{Proof that \( (G, \oplus) \) is an abelian group}

  \SubProof*{Proof that \( \oplus \) is well-defined} The addition operation on \( G \) does not depend on the representative of the equivalence class. Indeed, let \( (a, b) \sim (a', b') \) and \( (c, d) \sim (c', d') \). Then there exist \( u \) and \( b \) such that
  \begin{align*}
    a + b' + u &= a' + b + u, \\
    c + d' + v &= c' + d + v.
  \end{align*}

  When added combined, these give
  \begin{equation*}
    (a + c) + (b' + d') + (u + v)
    =
    (a' + c') + (b + d) + (u + v),
  \end{equation*}
  which implies that
  \begin{equation*}
    (a + c, b + d) \sim (a' + c', b' + d').
  \end{equation*}

  \SubProofOf*[def:magma/associative]{associativity} Associativity of multiplication in \( G \) is inherited from multiplication in \( M \).

  \SubProofOf*[def:monoid]{identity} The equivalence class \( [(0, 0)] \) is an identity in \( G \) and contains the pairs \( (x, x) \) of identical elements.

  \SubProofOf*[def:monoid_inverse]{inverse} For each member \( (a, b) \in M \times M \), its inverse is \( (b, a) \) because
  \begin{equation*}
    [(a, b)] \oplus [(b, a)] = [(a + b, b + c)],
  \end{equation*}
  which, by commutativity, belongs to \( [(0, 0)] \).

  \SubProofOf*[def:magma/commutative]{commutativity} Commutativity of the group operation \( \oplus \) is also inherited from the monoid operation \( + \).
\end{defproof}

\begin{theorem}[Grothendieck monoid completion universal property]\label{thm:grothendieck_monoid_completion_universal_property}\mcite{nLab:grothendieck_group_of_a_commutative_monoid}
  The \hyperref[def:monoid_grothendieck_completion]{Grothendieck completion} \( \overline{M} \) of a commutative monoid \( M \) satisfies the following \hyperref[rem:universal_mapping_property]{universal mapping property}:
  \begin{displayquote}
    For every abelian group \( G \) and every monoid homomorphism \( \varphi: M \to G \), there exists a unique group homomorphism \( \widetilde{\varphi}: \overline{M} \to G \) such that the following diagram commutes:
    \begin{equation}\label{eq:thm:grothendieck_monoid_completion_universal_property/diagram}
      \begin{aligned}
        \includegraphics[page=1]{output/thm__monoid_grothendieck_completion_universal_property.pdf}
      \end{aligned}
    \end{equation}
  \end{displayquote}

  Via \fullref{rem:universal_mapping_property}, \( \overline{\anon} \) becomes \hyperref[def:category_adjunction]{left adjoint} to the \hyperref[def:concrete_category]{forgetful functor}
  \begin{equation*}
    U: \cat{Ab} \to \cat{CMon}.
  \end{equation*}

  Compare this result to \fullref{thm:grothendieck_semiring_completion_universal_property}.
\end{theorem}
\begin{proof}
  Let \( \varphi: M \to G \) be a monoid homomorphism into an abelian group \( G \). We want to define a homomorphism \( \overline{\varphi} \) such that
  \begin{equation*}
    \overline{\varphi}(\iota_M(a)) = \overline{\varphi}([(a, 0)]) = \varphi(a).
  \end{equation*}

  Each equivalence class \( C \) in \( G \) has a unique member \( a \) such that \( (a, 0) \in C \), hence the above condition is well-posed.

  Fix pairs \( (a, b) \) and \( (a', b') \) from \( M \times M \). Suppose that \( (a, b) \sim (a', b') \). Then there exists \( u \in M \) such that
  \begin{equation*}
    a + b' + u = a' + b + u.
  \end{equation*}

  An additional restriction on \( \overline{\varphi} \) is then
  \begin{equation*}
    \overline{\varphi}\parens[\Big]{ [(a, b)] }
    =
    \overline{\varphi}\parens[\Big]{ [(a', b')] }.
  \end{equation*}

  We need to cancel out \( u \). This uniquely determines \( \overline{\varphi} \) as
  \begin{equation*}
    \overline{\varphi}([(a, b)]) \coloneqq \varphi(a) - \varphi(b).
  \end{equation*}
\end{proof}

\begin{definition}\label{def:group_commutator}\mcite[313]{Knapp2016BasicAlgebra}
  Let \( G \) be an arbitrary group. We define the \term{commutator} of the elements \( x \) and \( y \) as
  \begin{equation*}
    [x, y] \coloneqq xyx^{-1}y^{-1}.
  \end{equation*}

  The \term{commutator subgroup} \( [G, G] \) of \( G \) is the subgroup \hyperref[def:group/submodel]{generated} by all the commutators in \( G \).
\end{definition}

\begin{theorem}[Group abelianization universal property]\label{thm:group_abelianization_universal_property}\mcite[prop. 7.4]{Knapp2016BasicAlgebra}
  The commutator group \( [G, G] \) of any group \( G \) is \hyperref[thm:normal_subgroup_equivalences]{normal} and the quotient \( G / [G, G] \) is an abelian group, which we call the \term{abelianization} of \( G \), satisfies the following \hyperref[rem:universal_mapping_property]{universal mapping property}:
  \begin{displayquote}
    For every abelian group \( H \), every group homomorphism \( \varphi: G \to H \) \hyperref[def:factors_through]{uniquely factors through} \( G / [G, G] \). That is, there exists a unique group homomorphism \( \widetilde{\varphi}: G / [G, G] \to H \) such that the following diagram commutes:
    \begin{equation}\label{eq:thm:group_abelianization_universal_property/diagram}
      \begin{aligned}
        \includegraphics[page=1]{output/thm__group_abelianization_universal_property.pdf}
      \end{aligned}
    \end{equation}
  \end{displayquote}

  Via \fullref{rem:universal_mapping_property}, the abelianization functor becomes \hyperref[def:category_adjunction]{left adjoint} to the \hyperref[def:concrete_category]{forgetful functor}
  \begin{equation*}
    U: \cat{Ab} \to \cat{Grp}.
  \end{equation*}

  This result extends to \fullref{thm:ring_abelianization_universal_property}.
\end{theorem}
\begin{proof}
  Let \( C \coloneqq [G, G] \).

  \SubProof{Proof that \( G / C \) is abelian} Normality of \( G / C \) easily follows from
  \begin{equation*}
    a xyx^{-1}y^{-1} a^{-1}
    =
    (a x a^{-1}) (a y a^{-1}) (a x a^{-1})^{-1} (a y a^{-1})^{-1}.
  \end{equation*}

  Then for the cosets \( a C \) and \( b C \), we have
  \begin{equation*}
    a C \cdot b C
    =
    a b C
    =
    a b (b^{-1} a^{-1} b a) C
    =
    b a C.
  \end{equation*}

  Therefore, the quotient group \( G / C \) is abelian.

  \SubProof{Proof of universal mapping property} Let \( H \) be an abelian group and let \( \varphi: G \to H \) be a group homomorphism.

  Observe that \( \varphi(C) = e_H \). Indeed, since \( H \) is abelian, for \( [x, y] = xyx^{-1}y^{-1} \in C \) we have
  \begin{equation*}
    \varphi([x, y]) = \varphi(x) \varphi(y) \varphi(x^{-1}) \varphi(y^{-1}) = \varphi(x) \varphi(x^{-1}) \varphi(y) \varphi(y^{-1}).
  \end{equation*}

  We want \( \overline{\varphi}: G / C \to H \) to satisfy
  \begin{equation*}
    \overline{\varphi}(\underbrace{\pi_G(x)}_{xC}) = \varphi(x).
  \end{equation*}

  This suggests the definition
  \begin{equation*}
    \overline{\varphi}(xC) \coloneqq \varphi(x).
  \end{equation*}

  It is well-defined because if \( xC = yC \), we have
  \begin{equation*}
    \varphi(x)
    =
    \varphi(x) e_H
    =
    \varphi(x) \varphi(C)
    =
    \varphi(x C)
    =
    \varphi(y C)
    =
    \ldots
    =
    \varphi(y).
  \end{equation*}
\end{proof}


\chapter{DC Circuit Analysis}

In the most basic circuit, you have only a battery and a resistor:

\includegraphics[width=0.8\textwidth]{DC_Circuit_Diagram.png}

\begin{circuitikz}
\draw (0,0) to[battery1,invert,l=$6V$] ++(0,3)
to ++(3,0)
to [R=$3\Omega$, /tikz/circuitikz/bipoles/length=1.0cm,i=2A] ++(0,-3) -- (0,0);
\end{circuitikz}

In this case, you only need Ohm's Law: $V = I R$.  In this case, $6V = 3\Omega \times 2A$.
% ADD: Define Ohm's Law
\begin{Exercise}[title={Ohm's Law}, label=ohms_check]

  How many amps are going around the circuit?

  \vspace{1cm}

\begin{circuitikz}
\draw (0,0) to[battery1,invert,l=$24V$] ++(0,3)
to ++(3,0)
to [R=$6\Omega$, /tikz/circuitikz/bipoles/length=1.0cm,i={? A}] ++(0,-3) -- (0,0);
\end{circuitikz}

\end{Exercise}
\begin{Answer}[ref=ohms_check]

  $V = I R$ so $I = \frac{V}{R} = \frac{24V}{6\Omega} = 4A$.

\end{Answer}
% KA: https://youtu.be/F_vLWkkOETI

\section{Resistors in Series}

When you have two resistors wired together in a long line, we say they
are ``in series''.  If you have two resistors $R_1$ and $R_2$ wired in
series, the total resistance is $R_1 + R_2$.

In this diagram, for example, the total resistance is $5\Omega$.

\begin{circuitikz}
\draw (0,0) to[battery1,invert,l=$10V$] ++(0,5)
to ++(3,0)
to [R=$3\Omega$] ++(0,-2.5)
to [R=$2\Omega$] ++(0,-2.5) -- (0,0);
\end{circuitikz}

The current flowing through the circuit, then, is $10/4 = 2A$.

By Ohm's law, the voltage drop across the upper resistor is $I R = 2A \times 3\Omega = 6V$.

The voltage drop across the lower resistor is $I R = 2A \times 2\Omega = 4V$.

Notice that the battery pumps the voltage up to $10V$, then the two
resistors drop it by exactly $10V$. This is known as ``Kirchhoff's
Voltage Law'':
% KA: https://youtu.be/4rsswT_Rv1M

\begin{mdframed}[style=important, frametitle={Kirchhoff's Voltage Law}]\index{Kirchhoff's voltage law}
As you make a loop around a circuit, the sum of the voltage increase
must equal the sum of the voltage decrease.
\end{mdframed}

The negative end of the battery as connected to ``ground'' (
it has zero voltage), then we can draw a diagram with the
voltages(That symbol in the lower right represents a connection to ground).

\begin{circuitikz}
\draw (0,0) to[battery1,invert,l=$6V$] ++(0,5)
to [-*] ++(3,0) node[anchor=west] {10V}
to [R=$3\Omega$,-*] ++(0,-2.5) node[anchor=west] {4V}
to [R=$2\Omega$,-*] ++(0,-2.5) node[anchor=west]{0V} node[ground]{} --(0,0);
\end{circuitikz}

\begin{Exercise}[title={Resistors In Series}, label=series_resistor]

  What is the current going around the circuit?

  What is the voltage drop across each resistor?

  \vspace{1cm}
\begin{circuitikz}
\draw (0,0) to[battery1,invert,l=$16V$] ++(0,5)
to [-*] ++(3,0) node[anchor=west] {16V}
to [R=$5\Omega$,-*] ++(0,-2.5) node[anchor=west] {?}
to [R=$3\Omega$,-*] ++(0,-2.5) node[anchor=west]{0V} node[ground]{} --(0,0);
\end{circuitikz}

\end{Exercise}
\begin{Answer}[ref=series_resistors]

  There is a total resistance of $8\Omega$, so your 16V will push 2A
  of current around the circuit.

  2A going through a $5\Omega$ resistor represents a 10V drop.

  2A going through a $3\Omega$ resitor represents a 6V drop.

\end{Answer}

\section{Resistors in Parallel}

Look at this circuit. Note that the current can go two different paths.

\begin{circuitikz}
\draw (0,0) to[battery1,invert,l=$12V$] ++(0,3)
to ++(3,0)
to [R=$2\Omega$, /tikz/circuitikz/bipoles/length=1.0cm] ++(0,-3) -- (0,0);
\draw (3,3) -- (5,3)
to [R=$3\Omega$, /tikz/circuitikz/bipoles/length=1.0cm] ++(0,-3) -- (3,0);
\end{circuitikz}

There is 12 volts pushing current through both resistors. So 6A will
go through the 2$\Omega$ resistor and 4A will go through the 3$\Omega$
resistor.

\begin{circuitikz}
\draw (0,0) to[battery1,invert,l=$12V$] ++(0,3)
to ++(3,0)
to [R=$2\Omega$, /tikz/circuitikz/bipoles/length=1.0cm,i=6A] ++(0,-3) -- (0,0);
\draw (3,3) -- (5,3)
to [R=$3\Omega$, /tikz/circuitikz/bipoles/length=1.0cm,i=4A] ++(0,-3) -- (3,0);
\end{circuitikz}

Thus, a total of 10 A will be going through the battery.

Imagine you are a battery. You can't see that you have two resistors.
What does it feel like to you? $\frac{V}{I} = R$, and $V= 12$ and $I =
10$.  So the effective resistance of the two resistors in parallel is
$\frac{12}{10}$ or $\frac{6}{5} \Omega$.

\begin{mdframed}[style=important, frametitle={Resistance in Parallel}]\index{resistance!in parallel}
If you have several resistances $R_1, R_2, \ldots, R_n$ wired in
parallel, their effective resistance $R_t$ is given by

$$\frac{1}{R_t} = \frac{1}{R_1} + \frac{1}{R_2} + \ldots + \frac{1}{R_n}$$

\end{mdframed}

In our example:

$$\frac{1}{R_t} = \frac{1}{2} + \frac{1}{3} = \frac{5}{6}$$

Thus $R_t =  \frac{6}{5}\Omega$.

\begin{Exercise}[title={Resistors In Parallel}, label=parallel_resistors]

  What is the current going through the battery?
  What is the drop over the $4\Omega$ resistor?
  What is the current in each branch?

  \vspace{1cm}

  \begin{circuitikz}
\draw (0,0) to[battery1,invert,l=$12V$] ++(0,3)
to [R=$4\Omega$, /tikz/circuitikz/bipoles/length=1.0cm] ++(3,0) node [yshift=0.3cm] {? V}
to [R=$6\Omega$, /tikz/circuitikz/bipoles/length=1.0cm,i={? A}] ++(0,-3) node[ground]{} -- (0,0);
\draw (3,3) -- (5,3)
to [R=$3\Omega$, /tikz/circuitikz/bipoles/length=1.0cm,i={? A}] ++(0,-3) -- (3,0);
\end{circuitikz}

\end{Exercise}
\begin{Answer}[ref=parallel_resistors]
  The effective resistance of the $6\Omega$ and the $3\Omega$ is $2\Omega$ because

  $$\frac{1}{R_T} = \frac{1}{6} + \frac{1}{3} == \frac{1}{2}$$

  So the battery experiences a resistance of $4\Omega + 2\Omega =
  6\Omega$.  A $12V$ will push 2A through a resistance of $6\Omega$.

  The voltage drop across the $4\Omega$ resistor is $2A \times 4\Omega
  = 8V$. Thus there will be a 4V drop across the two resistors in
  parallel.  So 2/3 A will flow through the $6\Omega$ resistor. 4/3 A
  will flow through the $3\Omega$ resistor.

    \begin{circuitikz}
\draw (0,0) to[battery1,invert,l=$12V$] ++(0,3)
to [R=$4\Omega$, /tikz/circuitikz/bipoles/length=1.0cm] ++(3,0) node [yshift=0.3cm] {8 V}
to [R=$6\Omega$, /tikz/circuitikz/bipoles/length=1.0cm,i={2/3 A}] ++(0,-3) node[ground]{}-- (0,0);
\draw (3,3) -- (5,3)
to [R=$3\Omega$, /tikz/circuitikz/bipoles/length=1.0cm,i={4/3 A}] ++(0,-3) -- (3,0);
\end{circuitikz}

\end{Answer}


\documentclass{report}
\def\MLine#1{\par\hspace*{-\leftmargin}\parbox{\textwidth}{\[#1\]}}
\usepackage{amsmath}
\begin{document}

Book: Basic Mathematics
Author: Serge Lang

\tableofcontents{}

\chapter{Numbers}

\section{The Integers}
No exercises in this section.

\section{Rules for Addition}
Justify each step, using commutativity and associativity in proving the following identities.

\begin{enumerate}
    \item (a + b) + (c + d) = (a + d) + (b + c)
    \item (a + b) + (c + d) = (a + c) + (b + d)
    \item (a - b) + (c - d) = (a + c) + (- b - d)
    \item (a - b) + (c - d) = (a + c) - (b + d)
    \item (a - b) + (c - d) = (a + d) - (c - b)
    \item (a - b) + (c - d) = -(b + d) + (a + c)
    \item (a - b) + (c - d) = -(b + d) - (-a - c)
    \item ((x + y) + z) + w = (x + z) + (y + w)
    \item (x - y) - (z - w) = (x + w) - y + z
    \item (x - y) - (z - w) = (x - z) - (w - y)
    \item Show that - (a + b + c) = -a + (-b) + (-c).
    \item Show that -(a -b - c) = -a + b + c.
    \item Show that -(a - b) = b - a.
    \item[] Solve for x in the following equations.
    \item -2 + x = 4
    \item 2 - x = 5
    \item x - 3 = 7
    \item -x + 4 = 1
    \item 4 - x = 8
    \item -5 - x = -2
    \item -7 + x = -10
    \item -3 + x = 4
    \item Prove the \textbf{cancellation law for addition:}
	    \MLine{\textnormal{If } a + b = a + c, \textnormal{then } b = c}
    \item Prove: If a + b = a, then b = 0
\end{enumerate}

\section{Rules for Multiplication}

\begin{enumerate}
	\item Express each of the following expressions in the form $2^m3^na^rb^s$, where $m$, $n$, $r$, $s$ are positve integers.
		\begin{enumerate}
			\item $8a^2b^3(27a^4)(2^5ab)$
			\item $16b^3a^2(6ab^4)(ab)^3$
			\item $3^2(2ab)^3(16a^2b^5)(24b^2a)$
			\item $24a^3(1ab^2)^3(3ab)^2$
			\item $(3ab)^2(27a^3b)(16ab^5)$
			\item $32a^4b^5a^3b^2(6ab^3)^4$
		\end{enumerate}
	\item Prove:
		\MLine{(a + b)^3 = a^3 + 3a^2b + 3ab^2 + b^3}
		\MLine{(a - b)^3 = a^3 - 3a^2b + 3ab^2 - b^3}
	\item Obtain expansion for $(a + b)^4$ and $(a - b)^4$ similar to the expansions for $(a + b)^3$ and $(a - b)^3$ of the preceding exercise.
	\item[] Expand the following expressions as sums of powers of $x$ multiplied by integers.
	\item $(2 - 4x)^2$
	\item $(1 - 2x)^2$
	\item $(2x + 5)^2$
	\item $(x-1)^2$
	\item $(x + 1)(x-1)$
	\item $(2x + 1)(x + 5)$
	\item $(x^2 + 1)(x^2 - 1)$
	\item $(1 + x^3)(1 - x^3)$
	\item $(x^2 + 1)^2$
	\item $(x^2 - 1)^2$
	\item $(x^2 + 2)^2$
	\item $(x^2 - 2)^2$
	\item $(x^2 - 4)^2$
	\item $(x^3 - 4)(x^3 + 4)$
	\item $(2x^2 + 1)(2x^2 - 1)$
	\item $(-2 + 3x)(-2 - 3x)$
	\item $(x + 1)(2x + 5)(x-2)$
	\item $(2x + 1)(1 - x)(3x + 2)$
	\item $(3x -1)(2x + 1)(x + 4)$
	\item $(-1 -x)(-2 + x)(1 - 2x)$
	\item $(-4x + 1)(2 - x)(3 + x)$
	\item $(1 -  x)(1 + x)(2 - x)$
	\item $(x - 1)^2(3 - x)$
	\item $(1 - x)^2(2-x)$
	\item $(1 - 2x)^2(3 + 4x)$
	\item $(2x + 1)^2(2 - 3x)$
	\item The population of a city in 1910 was 50,000, and it doubles every 10 years. What will it be (a) in 1970 (b) in 1990 (c) in 2000?
	\item The population of a city in 1905 was 100,00 and it doubles every 25 years. What will it be after (a) 50 years (b) 100 years (c) 150 years?
	\item The population of a city was 200 thousand in 1915, and it triples every 50 years. What will be the population:
		\begin{enumerate}
			\item in the year 2215?
			\item in the year 2165?
		\end{enumerate}
	\item The population of a city was 25,000 in 1870, and it triples every 40 years. What will it be:
		\begin{enumerate}
			\item in 1990?
			\item in 2030?
		\end{enumerate}
\end{enumerate}

\section{Even and Odd Integers; Divisibility}
\begin{enumerate}
	\item Give the proofs for the cases of Theorem 1 which were not proved in the text.
	\item Prove: If $a$ is even and $b$ is any positive integer, then $ab$ is even.
	\item Prove: If $a$ is even, then $a^3$ is even.
	\item Prove: If $a$ is odd, then $a^3$ is odd.
	\item Prove: if $n$ is even, then $(-1)^n = -1$.
	\item Prove: if $n$ is odd, then $(-1)^n = -1$.
	\item Prove: if $m$, $n$ are odd, then the product $mn$ is odd.
	\item[] Find the largest power of 2 which divides the following integers.
	\item 16
	\item 24
	\item 32
	\item 20
	\item 50
	\item 64
	\item 100
	\item 36
	\item[] Find the largest power of 3 which divides the following integers.
	\item 30
	\item 27
	\item 63
	\item 99
	\item 60
	\item 50
	\item 42
	\item 25
	\item Let $a$, $b$ be integers. Define $a \equiv b (\textnormal{mod } 5)$, which we read
		``$a$ is \textbf{congruent to} $b$ \textbf{modulo} 5'', to mean that
		$a - b$ is divisible by 5. Prove: if $a \equiv b (\textnormal{mod } 5)$ and $x \equiv y (\textnormal{mod } 5)$, then
		\MLine{a + x \equiv b + y (\textnormal{mod } 5)}
		and
		\MLine{ax \equiv by (\textnormal{mod } 5)}
	\item Let $d$ be a positive integer. Let $a$, $b$ be integers. Define
		\MLine{a \equiv b (\textnormal{mod } d)}
		to mean that $a - b$ is divisible by $d$. Prove that if $a \equiv b (\textnormal{mod } d)$ and $x \equiv y (\textnormal{mod } d)$, then
		\MLine{a + x \equiv b + y (\textnormal{mod } d)}
		and
		\MLine{ax \equiv by (\textnormal{mod } d)}
	\item Assume that every positive integer can be written in one of the forms $3k$, $3k + 1$, $3k + 2$ for some integer $k$.
		Show that if the square of a positive integer is divisible by 3, then so is the integer.
\end{enumerate}

\section{Rational Numbers}
\begin{enumerate}
	\item Solve for $a$ in the following equations.
		\begin{enumerate}
			\item $2a = \dfrac{3}{4}$
			\item $\dfrac{3a}{5} = -7$
			\item $\dfrac{-5a}{2} = \dfrac{3}{8}$
		\end{enumerate}
	\item Solve for $x$ in the following equations.
		\begin{enumerate}
			\item $3x - 5 = 0$
			\item $-2x + 6 = 1$
			\item $-7x = 2$
		\end{enumerate}
	\item Put the following fractions in the lowest form.
		\begin{enumerate}
			\item $ \dfrac{10}{25} $
			\item $\dfrac{3}{9}$
			\item $\dfrac{30}{25}$
			\item $\dfrac{50}{15}$
			\item $\dfrac{45}{9}$
			\item $\dfrac{62}{4}$
			\item $\dfrac{23}{46}$
			\item $\dfrac{16}{40}$
		\end{enumerate}
\end{enumerate}

\end{document}


Clustering is a basic tool of exploratory data analysis. There are a variety of efficient algorithms---including $k$-means, EM for Gaussian mixtures, and hierarchical agglomerative schemes---that are widely used for discovering ``natural'' groups in data. Unfortunately, they don't always find a grouping that suits the user's needs.

This is inevitable. In any moderately complex data set, there are many different plausible grouping criteria. Should a collection of rocks be grouped according to value, or shininess, or geological properties? Should animal pictures be grouped according to the Linnaean taxonomy, or cuteness? Different users have different priorities, and an unsupervised algorithm has no way to magically guess these.

As a result, a rich body of work on {\it constrained} clustering has emerged.
In this setting, a user supplies guidance, typically in the form of ``must-link'' or ``cannot-link'' constraints, pairs of points that must be placed together or apart. Introduced by \citet{Wagstaff2001}, these constraints have since been incorporated into many different {\it flat} clustering procedures~\citep{Wagstaff2000,Bansal2004,Basu2004,Kulis2009,Biswas2014}.

In this chapter, we introduce constraints to {\it hierarchical clustering}, the recursive partitioning of a data set into successively smaller clusters to form a tree. A hierarchy has several advantages over a flat clustering. First, there is no need to specify the number of clusters in advance. Second, the tree captures cluster structure at multiple levels of granularity, simultaneously. As such, trees are particularly well-suited for exploratory data analysis and the discovery of natural groups.

There are several well-established methods for hierarchical clustering, the most prominent among which are the bottom-up agglomerative methods such as average linkage (see, for instance, Chapter 14 of \citet{Hastie2009}). But they suffer from the same problem of under-specification that is the scourge of unsupervised learning in general. And, despite the rich literature on incorporating additional guidance into flat clustering, there has been relatively little work on the hierarchical case.

What form might the user's guidance take? The usual must-link and cannot-link constraints make little sense when data has hierarchical structure. Among living creatures, for instance, should {\tt elephant} and {\tt tiger} be linked? At some level, yes, but at a finer level, no. A more straightforward assertion is that {\tt elephant} and {\tt tiger} should be linked in a cluster that does not include {\tt snake}. We can write this as a {\it triplet} $(\{\mbox{\tt elephant}, \mbox{\tt tiger}\}, \mbox{\tt snake})$. We could also assert $(\{\mbox{\tt tiger}, \mbox{\tt leopard}\}, \mbox{\tt elephant})$. Formally, $(\{a,b\},c)$ stipulates that the hierarchy contains a subtree (that is, a cluster) containing $a$ and $b$ but not $c$.

A wealth of research addresses learning taxonomies from triplets {\it alone}, mostly in the field of phylogenetics: see \citet{Felsenstein2004} for an overview, and \citet{Aho1981} for a central algorithmic result. Let's say there are $\numdata$ data items to be clustered, and that the user seeks a particular hierarchy $\tree^*$ on these items. This $\tree^*$ embodies at most ${\numdata \choose 3}$ triplet constraints, possibly less if it is not binary. It was pointed out in \citet{Tamuz2011} that roughly $\numdata \log \numdata$ carefully-chosen triplets are enough to fully specify $\tree^*$ if it is balanced. This is also a lower bound: there are $\numdata^{\Omega(\numdata)}$ different labeled rooted trees, so each tree requires $\Omega(\numdata \log \numdata)$ bits, on average, to write down---and each triple provides $O(1)$ bits of information, since there are just three possible outcomes for each set of points $a,b,c$. Although $\numdata \log \numdata$ is a big improvement over $\numdata^3$, it is impractical for a user to provide this much guidance when the number of points is large. In such cases, a hierarchical clustering cannot be obtained on the basis of constraints alone; the geometry of the data must play a role.

We consider an interactive process during which a user incrementally adds constraints.
\begin{itemize}
\item Starting with a pool of data $\data \subseteq \R^\d$, the machine builds a candidate hierarchy $\tree$.
\item The set of constraints $C$ is initially empty.
\item Repeat:
\begin{itemize}
\item The machine presents the user with a small portion of $\tree$: specifically, its restriction to $O(1)$ leaves $S \subset X$. We denote this $\tree|_S$.
\item The user either accepts $\tree|_S$, or provides a triplet constraint $(\{a,b\},c)$ that is violated by it.
\item If a triplet is provided, the machine adds it to $C$ and modifies the tree $T$ accordingly.
\end{itemize}
\end{itemize}
In realizing this scheme, a suitable clustering algorithm and querying strategy must be designed. Similar issues have been confronted in flat clustering---with must-link and cannot-link constraints---but the solutions are unsuitable for hierarchies, and thus a fresh treatment is warranted.

\textbf{The clustering algorithm:}
What is a method of hierarchical clustering that takes into account the geometry of the data points as well as user-imposed constraints?

We adopt an \emph{interactive Bayesian} approach. The learning procedure is uncertain about the intended tree and this uncertainty is captured in the form of a distribution over all possible trees. Initially, this distribution is informed solely by the geometry of the data but once interaction begins, it is also shaped by the growing set of constraints.

The nonparametric Bayes literature contains a variety of different distributional models for hierarchical clustering. We describe a general methodology for extending these to incorporate user-specified constraints. For concreteness, we focus on the Dirichlet diffusion tree~\citep{Neal2003}, which has enjoyed empirical success. We show that triplet constraints are quite easily accommodated: when using a Metropolis-Hastings sampler, they can efficiently be enforced, and the state space remains strongly connected, assuring convergence to the unique stationary distribution.

\textbf{The querying strategy:}
What is a good way to select the subsets $S$? A simple option is to pick them at random from $\data$. We show that this strategy leads to convergence to the target tree $\tree^*$. Along the way, we define a suitable distance function for measuring how close $\tree$ is to $\tree^*$.

%This allows us to give a simple rule for when to stop querying if we seek a tree $\epsilon$-close to the target.

We might hope, however, that a more careful choice of $S$ would lead to faster convergence, in much the same way that intelligent querying is often superior to random querying in active learning. In order to do this, we show how the Bayesian framework allows us to quantify which portions of the tree are the most uncertain, and thereby to pick $S$ that focuses on these regions.

Querying based on uncertainty sounds promising, but is dangerous because it is heavily influenced by the choice of prior, which is ultimately quite arbitrary. Indeed, if only such queries were used, the interactive learning process could easily converge to the wrong tree. We show how to avoid this situation by interleaving the two types of queries.

Finally, we present a series of experiments that illustrate how a little interaction leads to significantly better hierarchical clusterings.

\subsection*{Other related work}

A related problem that has been studied in more detail~\citep{Zoller2000,Eriksson2011,Krishnamurthy2012} is that of building a hierarchical clustering where the only information available is pairwise similarities between points, but these are initially hidden and must be individually queried.

In another variant of interactive flat clustering~\citep{Balcan2008,Awasthi2010,Awasthi2013}, the user is allowed to specify that individual clusters be merged or split. A succession of such operations can always lead to a target clustering, and a question of interest is how quickly this convergence can be achieved.

Finally, it is worth mentioning the use of triplet constraints in learning other structures, such as Euclidean embeddings~\citep{Borg1997}.

\iffalse
\section{Bayesian hierarchical clustering}
%\subsection{Priors on trees}

\fi
\section{Adding interaction}

Impressive as Bayesian nonparametric hierarchical clustering is, there is no reason to suppose that it will magically find a tree that suits the user's needs. But a little interaction can be helpful in improving the outcome.

Let $\tree^*$ denote the target hierarchical clustering. It is not necessarily the case that the user would be able to write this down explicitly, but this is the tree that captures the distinctions he/she is able to make, or wants to make. Figure~\ref{fig:refinement} (left) shows an example, for a small data set of 5 points. In this case, the user does not wish to distinguish between points $1,2,3$, but does wish to place them in a cluster that excludes point $4$.

We could posit our goal as exactly recovering $\tree^*$. But in many cases, it is good enough to find a tree that captures all the distinctions within $\tree^*$ but also possibly has some extraneous distinctions, as in the right-hand side of Figure~\ref{fig:refinement}.

\begin{figure}
    \centering
    \includegraphics[width=3in]{img/ibhc/refinement.pdf}
    \caption{Target tree $T^*$ (left) and a refinement of it.}
    \label{fig:refinement}
\end{figure}

Formally, given data set $\data$, we say $S \subseteq \data$ is a {\it cluster} of tree $\tree$ if there is some node of $\tree$ whose descendant leaves are exactly $S$. We say $\tree$ is a {\it refinement} of $\tree^*$ if they have the same set of leaves, and moreover every cluster of $\tree^*$ is also a cluster of $\tree$. This, then, is our goal: to find a refinement of the target clustering $\tree^*$.

\subsection{Triplets}

The user provides feedback in the form of triplets. The constraint $(\{a,b\},c)$ means that the tree should have a cluster containing $a$ and $b$ but not $c$. Put differently, the lowest common ancestor of $a,b$ should be a strict descendant of the lowest common ancestor of $a,b,c$.

Let $\Delta(\tree)$ denote the set of all proper triplet constraints embodied in tree $\tree$. If $\tree$ has $n$ nodes, then $|\Delta(\tree)| \leq {n \choose 3}$. For non-binary trees, it will be smaller than this number. Figure~\ref{fig:refinement} (left), for instance, has no triplet involving $1,2,3$.

Refinement can be characterized in terms of triplets.
\begin{restatable}{lemma}{treerefinement}
\label{thm:treerefinement}
Tree $\tree$ is a refinement of tree $\tree'$ if and only if $\Delta(\tree') \subseteq \Delta(\tree)$.
\end{restatable}
\begin{proof}
See \autoref{chap:ibhc-appendix}.
\end{proof}
In particular, {\it any triplet-querying scheme that converges to the full set of triplets of the target tree $\tree^*$ is also guaranteed to produce trees that converge to a refinement of $\tree^*$.}

With this lemma in mind, it is natural to measure how close a tree $\tree$ is to the target $\tree^*$ with the following (asymmetric) distance function, which we call {\it triplet distance} (TD):

%\subsection{Triplet Distance}
%To better understand the performance of clustering algorithm
%and the various triplet querying schemes,
%we first define a distance function between
%a candidate tree and the desired tree.

%$T$ is defined to be a \emph{refinement} of $T^*$ if
%if every cluster of $T^*$ is in $T$.
%In the general setting, $T^*$ may be a $k$-ary tree
%and our algorithm only produces a binary tree.
%For example, in a $K$-way classification problem, the desired
%tree $T^*$ would be a root node with $K$ children corresponding to %each class.
%Each of these $K$ children would have all data belonging to its class %as
%a child. See \autoref{fig:kway} for an example.

%If a binary tree $T$ is a refinement of $T^*$ in the $K$-way
%classification problem,
%there exist nodes in $T$ that contain
%solely the data for each class and this is the closest binary tree we
%can obtain.
%Thus, we desire a distance function such that if $T$ is a refinement
%of $T^*$, their distance should be 0.

%\begin{figure}[H]
%    \centering
%    \includegraphics[width=0.5\textwidth]{img/ibhc/KWayTree}
%    \caption{A 4-way classification tree on the left
%    and a refinement of it on the right. Nodes
%    with matching colors represent matching clusters.}
%    \label{fig:kway}
%\end{figure}

%Let $\mathcal{C}(T)$ be the enumeration of all triplets
%satisfied by tree $T$.
%For a given desired \emph{master tree} $T^*$
%the triplet distance (TD) between $T^*$ and a candidate tree $T$
%is

\begin{align}
    \mathrm{TD}(\tree^*, \tree) = \frac{\sum_{c \in \Delta(\tree^*)} \mathbb{I}(c \notin \Delta(\tree)) }{|\Delta(\tree^*)|}
\end{align}
where $\mathbb{I}$ is the indicator function.
% Lemma
%If $T$ is a refinement of $T^*$, their triplet distance is zero
%as $\mathcal{C}(T^*) \subseteq \mathcal{C}(T)$.
This distance is zero exactly when $\tree$ is a refinement of $\tree^*$, in which case we have reached our goal.

%User guidance can assist in under-specified
%unsupervised learning problems. We now
%provide a concrete means of obtaining
%information about hierarchical structure in data
%from a user, in the form of \emph{triplet feedback}.

%For hierarchies, a triplet is a set of data
%of the form $(\{a, b\}, c)$, such that
%$a$ and $b$ should belong in a subtree
%together without $c$. Let $\text{lca}(u, v)$ be
%the lowest common ancestor of nodes $u$ and $v$.
%Formally, we can verify that a tree satisfies a triplet
%$(\{a, b\}, c)$ if $\text{lca}(a, b)$ is a descendant
%of both $\text{lca}(a, c)$ and $\text{lca}(b, c)$.

A simple strategy for obtaining triplets
would be to present the user with three randomly
chosen data points and have the user pick the odd one out.
This strategy has several drawbacks.
First, some sets of three points have no triplet constraint
(for instance, points $1,2,3$ in \autoref{fig:refinement}).
Second, the chosen set of points might correspond to a triplet
that has already been specified, or is {\it implied} by
specified triplets. For example, knowledge of $(\{a, b\}, c)$ and
$(\{b, c\}, d)$ implies $(\{a, c\}, d)$.
Enumerating the set of implied
triplets is non-trivial for $n > 3$ triplets~\citep{Bryant1995},
making it difficult to avoid these implied triplets
in the first place.

We thus consider another strategy---rather
than the user arranging three data points into
a triplet, the user observes
the hierarchy induced over some $O(1)$-sized
subset $S$ of the data and corrects
an error in the tree by supplying a triplet.
We call this is a \emph{subtree query}.
Finally, we note that in this work
we only consider the \emph{realizable} case
where the triplets obtained
from a user do not contain contradictory information
and that there is a tree that satisfies
all of them.

%\subsection{Triplet feedback on subtrees}
%The interactive process integrates
%user feedback into a hierarchy
%after querying the user for a triplet.
%We perform this query by
%showing a user the subtree
%over a particular subset of data
%and ask to either
%accept the subtree as correct, or
%report a triplet that is violated by
%the subtree.
%
%Formally, given a candidate hierarchy $T$
%that satisfies a set of triplets $C$ ,
%we first obtain some $O(1)$ size
%subset of the dataset  $S \subset X$,
%and find the induced subtree
%$T|_S$.
%$T|_S$ is shown to a user whereupon
%the user can either accept the tree,
%acknowledging that there are no errors in its
%structure, or report a triplet
%$(\{a, b\}, c)$ that is violated by this subtree.

%This approach confers several benefits over
%the simple approach of selecting three points
%at random. Subtree queries have the benefit
%of never returning a triplet
%implied by $C$. A tree that satisfies $C$
%will by definition will satisfy all of $C$'s
%implied triplets.
%In addition,
%the triplet reported by the user
%is likely to be more informative
%as the triplet corresponds to an
%error in a candidate hierarchy.
%However, this method assumes
%the existence of an algorithm
%that can produce candidate trees
%that satisfy $C$.

\subsection{Finding a tree consistent with constraints}
\label{sec:aho}
We start with a randomly initialized hierarchy $\tree$
over our data
and show an induced subtree $\tree|_S$ to the user, obtaining the
first triplet. The next step is constructing
a new tree that satisfies the triplet.
This begins the feedback cycle; a user provides a triplet
given a subtree and the triplet is incorporated into a clustering algorithm,
producing a new candidate tree.
A starting point is
an algorithm that returns
a tree consistent with a set of triplets.

The simplest algorithm to solve this problem is
the \texttt{BUILD} algorithm, introduced in \citet{Aho1981}.
Given a set of triplets $C$, \texttt{BUILD}
will either return a tree that satisfies $C$, or error
if no such tree exists.
In \texttt{BUILD}, we first construct the {\it Aho graph}
$G_C$, which has a vertex for each data point and an
undirected edge $\{a,b\}$ for each triplet constraint $(\{a,b\},c)$.
If $G_C$ is connected, there is no tree that satisfies all
triplets. Otherwise, the top split of the tree is a partition of
the connected components of $G_C$: any split is fine as long as
points in the same component stay together. Satisfied triplets are discarded, and \texttt{BUILD} then continues recursively on the
left and right subtrees.

\texttt{BUILD} satisfies triplet constraints but ignores
the geometry of the data, whereas we wish to take both into
account. By incorporating triplets into the posterior DDT
sampler, we obtain high likelihood trees that still satisfy $C$.

\subsection{Incorporating triplets into the sampler}

In this section, we present an algorithm
to sample candidate trees
from the posterior DDT, constrained
by a triplet set $C$.
It is based on the subtree prune and regraft move.
%The Metropolis-Hastings algorithm for the DDT
%alternates sampling tree structure
%via SPR moves and latent divergence locations
%via Gibbs Sampling.
%This process produces tree samples
%that converge to the posterior
%distribution of the DDT
%given data.

\begin{figure*}[htp!]
    \centering
    \includegraphics[width=\textwidth]{img/ibhc/ConstrainedSPRMove}
    \caption{
            Visualized is a constrained-SPR move.
            Pruning is identical  but
            a regraft location is selected from the valid regraft locations
            limited by triplets.
            In this image, we are constrained by the sole triplet $(\{2, 3\}, 4)$.
            }
    \label{fig:constrainedsprmove}
\end{figure*}

The SPR move is of particular interest
because we can efficiently
enforce triplets to form a \emph{constrained-SPR move},
resulting in a sampler
that only produces trees that satisfy a set of triplets.
A \emph{constrained-SPR move} is defined as
an SPR move that assigns zero probability to
any resulting trees that would violate a set of triplets.
Restricting the neighborhood of an SPR move
runs the risk of partitioning the state space,
losing the convergence
guarantees of the Metropolis-Hastings algorithm.
Fortunately, a constrained-SPR move does not compromise
strong connectivity.
For any realizable triplet set $C$,
we prove
the constrained-SPR move Markov chain's aperiodicity and irreducibility.

Consider the Markov chain on the state space
of rooted binary trees that is induced by the constrained sampler.
%\subsection{Proof of \autoref{thm:apr}}
\begin{restatable}{lemma}{aperiodic}
The constrained-SPR Markov chain is aperiodic.
\end{restatable}
\begin{proof}
A sufficient condition for aperiodicity
is the existent of a ``self-loop'' in the transition matrix: a non-zero probability of a state transitioning to itself.
Supposed we have pruned a subtree already.
When regrafting, the ordinary SPR move
has a non-zero probability of choosing any branch,
and a constrained-SPR move cannot regraft
to branches that would violate triplets.
Since the current tree in the Markov chain
satisfies triplet set $C$, there is a non-zero probability
of regrafting to the same location.
We thus have an aperiodic Markov chain.
\end{proof}

%\subsection{Proof of \autoref{thm:irr}}
%\label{app:irr}
%\irreducible*
\begin{restatable}{lemma}{irreducible}
\label{thm:irr}
A constrained-SPR Markov chain is irreducible.
\end{restatable}

\begin{proof}
(sketch) To show irreducibility,
we show that a tree $\tree$ has an non-zero
probability of reaching an arbitrary tree
$\tree'$ via constrained-SPR moves where
both $\tree$ and $\tree'$ satisfy a set of triplets $C$.
Our proof strategy
is to construct a canonical tree
$\tree_C$, and show that there exists
a non-zero probability path from $\tree$ to $\tree_C$,
and therefore from $\tree'$ to $\tree_C$.
We then show that for a given constrained-SPR move,
the reverse move has a non-zero probability.
Thus, there exists a path from $\tree$ to $\tree_C$ to
$\tree'$, satisfying irreducibility.

Recall that the split at a node in a binary
tree that satisfies triplet set $C$
corresponds to a binary partition
of the Aho graph at the node (see Section \ref{sec:aho}).
$\tree_C$ is a tree such that
every node in $\tree_C$ is in \emph{canonical form}.
A node is in canonical form if it is a leaf node,
or, the partition of the Aho graph
at that node can be written as $(l, r)$.
$l$ is the single connected component
containing the point with the minimum data index,
and $r$ is the rest of the components.

To convert a particular node $s$ into canonical form,
we first perform ``grouping'',
which puts $l$ into a single descendant of $s$
via constrained-SPR moves.
We then make two constrained-SPR moves to convert
the partition at $s$ into the form $(l, r)$ (see \autoref{fig:canonical}).
We convert all nodes into canonical form recursively, turning
an arbitrary tree $\tree$ into $\tree_C$.

Finally, the reverse constrained-SPR move has a non-zero
probability. Suppose
we perform a constrained-SPR move on tree $\tree_1$, converting it into $\tree_2$ by
detaching subtree $s$
and attaching it to branch $(u, v)$.
A constrained-SPR move on $\tree_2$ can select
$s$ for pruning
and can regraft it to form $\tree_1$ with a non-zero
probability since
$\tree_1$ satisfies the same constraint set as $C$.
For a full proof, please refer to \autoref{chap:ibhc-appendix}.
\end{proof}

\begin{figure*}
    \centering
    \includegraphics[width=\textwidth]{img/ibhc/CanonicalTree}
    \caption{The process of converting $s$ into canonical form.
    We first group nodes from $l$ into their own isolated subtree, then perform
    two constrained-SPR moves to put $s$ into canonical form.}
    \label{fig:canonical}
\end{figure*}

%\begin{restatable}{lemma}{aperiodic}
%\label{thm:apr}
%\end{restatable}
%\begin{proof} See \ref{app:apr}.
%\end{proof}
%
%\begin{restatable}{lemma}{irreducible}
%\label{thm:irr}
%A constrained-SPR Markov chain is irreducible.
%\end{restatable}
%\begin{proof} See \ref{app:irr}.
%\end{proof}

The simplest possible scheme for a constrained-SPR move
would be rejection sampling. The Metropolis-Hastings
algorithm for the DDT would be the same as in the
unconstrained case,
but any trees violating $C$ would have accept
probability $0$. Although this procedure is correct,
it is impractical. As the number of triplets
grows larger, more trees will be rejected
and the sampler will slow down over time.

To efficiently sample a tree that satisfies a set of triplets $C$,
we modify the regraft in the ordinary SPR move.
The constrained-SPR move must assign zero probability
to any regraft branches that would result in a tree
that violates $C$.
This is accomplished by generating the path from the root
in the same manner as sampling a branch,
but avoiding paths that would resulted in violated triplets.

\textbf{Description of constrained-SPR sampler}
Recall that in the DDT's sampling procedure for regraft branches,
a particle at a node picks a branch, and either diverges from that
branch or recursively samples the node's child.
Let $s$ be the root of the subtree we are currently grafting
back onto tree $\tree$, let $C$ be the
triplet set, and let $\texttt{leaves}(u)$
denote the descendant-leaves of node $u$.
Suppose we are are currently at node $u$,
deciding whether to diverge
at the branch $(u, v)$ or to recursively sample $v$.
Consider any triplet $(\{a, b\}, c) \in C$. If all---or none---of $a,b,c$ are in $\texttt{leaves}(s)$, then the triplet is
unaffected by the graft, and can be ignored. Otherwise,
some checks are needed:
\begin{enumerate}
\item $c \in \texttt{leaves}(s)$

Then we know $a,b \not\in \texttt{leaves}(s)$. If $a$ and $b$ are split across $v$'s children,
we are banned from sampling $v$.

\item $a \in \texttt{leaves}(s)$ but $b,c \not\in \texttt{leaves}(s)$

If both $b$ and $c$ are in $\texttt{leaves}(v)$, we are
required to sample $v$.
If just $c$ is in $\texttt{leaves}(v)$, we are banned
from both diverging at $(u, v)$ and sampling $v$.
Otherwise we can either diverge at $(u, v)$ or sample $v$.

\end{enumerate}

%\begin{enumerate}
%\item[1.] A node $u$ is \emph{path-banned} if for
%any triplet $(\{a, b\}, c) \in C$,
%$(a \in \texttt{leaves}(s) \land b \notin \texttt{leaves}(u) \land c \in \texttt{leaves}(u)) \lor (b \in \texttt{leaves}(s) \land a \notin \texttt{leaves}(u) \land c \in \texttt{leaves}(u))$.
%Let $p$ be $u$'s parent.
%Intuitively, if $u$ is \emph{path-banned}, we cannot
%graft to the branch $(p, u)$, as we would automatically
%violate a triplet. This is because there is some triplet$(\{a, b\}, c)$
%in $C$ such that attaching $s$ to $(p, u)$ would result in a subtree
%that contains either $a$ and $c$ but not $b$ or $b$ and $c$ but not $a$.
%
%\item[2.] A node $u$ is \emph{sample-banned} if
%for
%any triplet $(\{a, b\}, c) \in C$,
%$(c \in \texttt{leaves}(s)) \land ((a \in \texttt{leaves}(v) \land b \in \texttt{leaves}(w)) \lor (a \in \texttt{leaves}(w) \land b \in \texttt{leaves}(v))$
%where $v$ and $w$ are the children of $u$.
%Intuitively,
%this means that there exist some $a$ and $b$ in our triplet set
%that are split between $u$'s children. Attaching a subtree containing
%$c$ to a any branch below $u$ would violate a triplet.
%
%\item[3.] A node $u$ is \emph{path-required} if for
%any triplet $(\{a, b\}, c) \in C$,
%$(a \in \texttt{leaves}(s) \land b \in \texttt{leaves}(u)) \lor (b \in \texttt{leaves}(s) \land a \in \texttt{leaves}(u))$.
%If $u$ is \emph{path-required}, the particle must enter its branch,
%as for some triplet $(\{a, b\}, c)$, if $a$ is in our
%pruned subtree, then $b$ is in $u$ and $c$ is in $u$'s sibling.
%Choosing the branch to $u$'s sibling would violate this triplet.
%
%\item[4.] A node $u$ is \emph{sample-required} if for
%any triplet $(\{a, b\}, c) \in C$,
%$(a \in \texttt{leaves}(s) \land b \in \texttt{leaves}(u) \land c \in \texttt{leaves}(u)) \lor (b \in \texttt{leaves}(s) \land a \in \texttt{leaves}(u) \land c \in \texttt{leaves}(u))$.
%If $u$ being \emph{sample-required}
%means that there exists some $a$ and $c$ or $b$ and $c$
%in our triplet set that are already in a subtree in $T$ together.
%We must attach our subtree such that this triplet is not violated.
%
%\end{enumerate}
(The case where $b \in \texttt{leaves}(s)$ is
symmetric to case 2.)
If we choose to sample $v$, we remove
constraints from our current set $C$ that are now satisfied,
and continue recursively.
This defines a procedure by which we can sample
a divergence branch that does not violate constraints.

While the constrained-SPR sampler can produce
a set of trees given a set of static constraints,
the \texttt{BUILD} algorithm is useful in
adding new triplets into the sampler.
Suppose we have been sampling trees with constrained-SPR moves
with satisfying triplet set $C$
and we obtain a new triplet $u = (\{a, b\}, c)$ from a user query.
We take the current tree $\tree$ and find the least common ancestor
(call it $z$) of $a$ and $b$. We then call
\texttt{BUILD}$(C + \{u\})$ on just the nodes in
$\texttt{leaves}(z)$, and
we substitute the resulting subtree at position $z$ in tree $\tree$.

\subsection{Intelligent subset queries}
We now have a method to sample a constrained
distribution over candidate trees.
Given a particular candidate tree $\tree$,
our first strategy for subtree querying
is to pick a random subset $S$ of the leaves
of constant size, and show the user
the induced subtree over the subset, $\tree|_S$.
We call this \emph{random subtree querying}.
But can we use a set of trees produced by the sampler
to make better subtree queries?
If tree structure is ambiguous in a particular region of data,
i.e. there are several hierarchies that could explain
a particular configuration of data,
the MH algorithm will sample over
these different configurations. A query over points
in these ambiguous regions may help our algorithm
converge to a better tree faster. By looking for
these regions in our samples, we can choose query
subsets $S$ for which the tree structure is highly variable,
and hopefully the resulting triplet from the user will
reduce the ambiguity.

More precisely, we desire a notion of tree variance.
Given a set of trees $\trees$, what is the variance
%TODO: check this
over a given subset of the data $S$?
We propose using the notion of tree distance
as a starting point.
For a given tree $\tree$, the tree distance between two nodes $a$ and $b$,
denoted $\texttt{treedist}_T(a, b)$,
is the number of edges of $\trees$ needed to get from $a$ to $b$.
Consider two leaves $u$ and $v$. If the tree structure around
them is static, we expect the tree distance
between $u$ and $v$ to change very little, as the surrounding tree
will not change. However, if there is
ambiguity in the surrounding structure, the tree distance will
be more variable.
Given a subset of data $S$ and a set of trees $\trees$,
the tree distance variance (TDV) of the trees over the subset is defined as:
\begin{align}
    \text{TDV}(\trees, S) = \max_{u, v \in S}\mathrm{Var}_{\tree \in \trees}[\texttt{treedist}_{\tree|_S}(u, v)]
\end{align}

This measure of variance is
the \emph{max} of the variance of tree distance between
any two points in the subset. Computing this requires
$O(|\trees||S|^2 \log|S|)$ time, and since since $|S|$ is constant,
it is not prohibitively expensive.

Given a set of trees from the sampler $\trees$,
we now select a high-variance subtree
by instantiating $L$ random subsets of constant size, $S_1, \ldots, S_L$
and picking $\text{argmax}_l \text{TDV}(\trees, S_l)$.
We call this \emph{active subtree querying}.
Although using tree variance will help reconcile ambiguity in the tree structure,
if a set of samples from a tree all violate the same triplet,
it is unlikely that active querying will recover that triplet.
Thus, interleaving random querying and active querying
will hopefully help the algorithm converge quickly, while avoiding
local optima.

\section{Experiments}

\begin{figure}
    \centering
    \begin{subfigure}[]{0.6\textwidth}
        \centering
        \includegraphics[width=\textwidth]{img/ibhc/Fisher-Iris-result.png} \caption{Fisher Iris}
        \label{fig:iris-result}
    \end{subfigure}
    \\
    \begin{subfigure}[]{0.6\textwidth}
        \centering
        \includegraphics[width=\textwidth]{img/ibhc/MNIST-result.png}
        \caption{MNIST}
        \label{fig:mnist-result}
    \end{subfigure}
    \caption{The average of four runs of constrained-SPR samplers
    for the Fisher Iris dataset and the MNIST dataset, using 5 different querying schemes. A query was made every 100 iterations.}
    \label{fig:main-results}
\end{figure}

We evaluated the convergence properties of five different querying schemes.
In a ``simple query'', a user is presented with three random data
and picks an odd one out.
In a ``smart query'', a user is unrealistically shown the entire candidate tree and reports a violated triplet.
In a ``random query'', the user is shown the induced candidate tree over
a random subset of the data.
In an ``active query'', the user is shown a high variance
subtree using tree-distance variance.
Finally, in an ``interleaved query'', the user is alternatively shown a random
subtree and a high variance subtree.
In each experiment, $\tree^*$ was known, so user queries were simulated
by picking a triplet
violated by the root split of the queried tree,
and if no such triplet existed, recursing on a child.
%\begin{enumerate}
%\item Simple querying: a user is presented with three random data and
%reports a triplet of the data that should be satisfied.
%\item Smart querying (unrealistic): a user is presented with the entire tree
%and reports a triplet that is violated.
%\item Random querying: the user is shown the induced subtree
%over a random subset of the data of constant size
%and reports a violated triplet.
%\item Active querying: the user is shown a high variance
%induced subtree, chosen from a set of random subsets of data $\{S_1, \ldots , S_L\}$,
%with variance defined as tree distance variance. The user then reports
%a violated triple from this induced subtree.
%\item Interleaved querying: we alternate performing a random query
%and an active query.
%\end{enumerate}
Each scheme was evaluated on four different datasets.
The first dataset, MNIST \citep{Lecun1998}, is an 10-way image classification
dataset where the data are 28 x 28 images of digits.
The target tree $\tree^*$ is simply the $K$-way classification
tree over the data.
The second dataset is Fisher Iris, a 3-way flower
classification problem, where each of 150 flowers has
five features.
The third dataset, Zoo \citep{Lichman2013}, is a set of 93 animals
and 15 binary morphological features for each of animals,
the target tree being the induced binary tree
from the Open Tree of Life \citep{Hinchliff2015}.
The fourth dataset is 20 Newsgroups \citep{Joachims1997}, a
corpus of text articles on 20 different subjects. We use
the first 10 principal components as features
in this classification problem.
All datasets were modeled with DDT's with
acquisition function $a(t) = 1/(1 - t)$
and Brownian motion parameter $\sigma^2$ estimated from data.
To better visualize the different convergence rates
of the querying schemes, MNIST and 20 Newsgroups were subsampled
to 150 random points.

For each dataset and querying scheme, we instantiated a SPR
sampler with no constraints. Every one hundred iterations of the
sampler, we performed a query.
In subtree queries, we used subsets of size $|S| = 10$
and in active querying, the highest-variance subset was chosen from $L = 20$ different random subsets.
As baselines,
we measured the triplet distance of the vanilla DDT
and the average linkage tree.
Finally, results were averaged over four runs of each sampler.
The triplet distances for Fisher Iris and MNIST can be seen in \autoref{fig:main-results}. Results for the other datasets
can be found in \autoref{chap:ibhc-appendix}.
Although unrealistic due to the size of the tree shown to the user,
the smart query performed the best, achieving minimum error
with the least amount of queries. Interleaved followed next,
followed by active, random, and simple. In general, the vanilla
DDT performed the worst, and the average linkage
score varied on each dataset, but in all cases, the
subtree querying schemes performed better than both the vanilla DDT
and average linkage.

In three datasets (MNIST, Fisher Iris and Zoo),
interactive methods
achieve higher data likelihood than the vanilla DDT.
Initially, the sampler is often restructuring the tree
with new triplets
and data likelihood is unlikely to rise. However, over time
as less triplets are reported,
the data likelihood increases rapidly.
We thus conjecture that triplet constraints
may help the MH algorithm find better optima.

\section{Future Work}
We are interested in studying the non-realizable case, i.e.
when there does not exist a tree that satisfies triplet set $C$. We would also like to better understand the effect of constraints on searching
for optima using MCMC methods.

\ibhcack