\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[legalpaper, portrait, margin=1in]{geometry}

\title{Calculus Q.2}
\author{Shreenabh Agrawal}
\date{\today}

\begin{document}

\maketitle

\section{Question}

Evaluate the Integral:
$$\int\limits_{0}^{\infty} \frac{\left(x^{2}-1\right) \ln x}{1+x^{6}} \: d x$$
\section{Solution}

Taking Substitution,
$$\begin{aligned}
x &=\tan ^{1 / 3} \theta \\
d x &=\frac{1}{3} \tan ^{-2 / 3} \theta \sec ^{2} \theta \: d \theta
\end{aligned}$$
The Integral Becomes,

$$\begin{aligned}
{\Rightarrow} I &=\frac{1}{3} \int\limits_{0}^{\pi / 2} \frac{\left(\tan ^{2 / 3} \theta-1\right) \ln (\tan \theta)}{1+\tan ^{2} \theta}\left(\frac{\tan ^{-2 / 3} \theta \sec ^{2} \theta \: d \theta}{3}\right) \\
&=\frac{1}{9} \int\limits_{0}^{\pi / 2}\left(1-\tan ^{-2 / 3} \theta\right) \ln \tan \theta \: d \theta
\end{aligned}$$
We know the Formula,

$$\int\limits_{0}^{\pi / 2} \tan ^{n}(x) \log (\tan (x)) d x=\pi^{2} \sin ^{3}\left(\frac{\pi n}{2}\right) \csc ^{2}(\pi n)$$

\begin{flushright}
[For $-1<n<1$]
\end{flushright}
Using this, and Splitting the Integral,

$$\begin{aligned}
\Rightarrow I
&= \frac{1}{9}\left[\int\limits_{0}^{\pi / 2} \ln (\tan \theta) \: d \theta-\int\limits_{0}^{\pi / 2}\left(\tan ^{-2 / 3} \theta\right) \ln (\tan \theta) \: d \theta\right]\\
&=\frac{1}{9}\left[0-\pi^{2} \sin ^{3}\left(\frac{\pi}{3}\right) \csc ^{2}\left(\frac{\pi}{3}\right)\right]
\end{aligned}$$

Thus, the final answer is:
$$\boxed{I=\frac{\pi^{2}}{6 \sqrt{3}}}$$

\end{document}


\chapter{Sort}

\section{Introduction}
List of general algorithms:
\begin{enumerate}
\item Selection sort: invariant
\begin{enumerate}
\item Elements to the left of $i$ (including $i$) are fixed and in ascending order (fixed and sorted).
\item No element to the right of $i$ is smaller than any entry to the left of $i$ ($A[i]  \leq\min(A[i+1:n])$.
\end{enumerate}
\item Insertion sort: invariant
\begin{enumerate}
\item Elements to the left of $i$ (including $i$) are in ascending order (sorted).
\item Elements to the right of $i$ have not yet been seen.
\end{enumerate}
\item Shell sort: h-sort using insertion sort.
\item Quick sort: invariant
\begin{enumerate}
\item $|A_p|..\leq..|..unseen..|..\geq..|$ maintain the 3 subarrays.
\end{enumerate}
\item Heap sort: compared to quick sort it is guaranteed $O(N \lg N)$, compared to merge sort it is $O(1)$ extra space.
\end{enumerate}

\section{Algorithms}
\subsection{Quick Sort}
\subsubsection{Normal pivoting}\label{section:pivot}
The key part of quick sort is pivoting:
\begin{python}
def pivot(self, A, i, j):
    """
    pivoting algorithm:
    | p | closed set | open set |
    | closed set | p | open set |
    """
    p = i
    closed = p
    for ptr in xrange(i, j):
        if A[ptr] < A[p]:
            closed += 1
            A[ptr], A[closed] = A[closed], A[ptr]

    A[closed], A[p] = A[p], A[closed]
    return closed
\end{python}

Notice that this implementation goes $O(N^2)$ for arrays with all duplicates.

\textbf{Problem with duplicate keys}: it is important to stop scan at duplicate
keys (counter-intuitive); otherwise quick sort will goes $O(N^2)$ for the
array with all duplicate items, because the algorithm will put all items
equal to the $A[p]$ on \textbf{a single side}.

Example: quadratic time to sort random arrays of 0s and 1s.

\subsubsection{Stop-at-equal pivoting}
Alternative pivoting implementation with optimization for duplicated keys:
\begin{python}
def pivot_optimized(self, A, lo, hi):
    """
    Fix the pivot as the 1st element
    Scan from left to right and right to left simultaneously
    Avoid the case that the algo goes O(N^2) with duplicated keys
    """
    p = lo
    i = lo
    j = hi
    while True:
        while True:
            i += 1
            if i >= hi or A[i] >= A[lo]:
                break
        while True:
            j -= 1
            if j < lo or A[j] <= A[lo]:
                break

        if i >= j:
            break

        A[i], A[j] = A[j], A[i]

    A[lo], A[j] = A[j], A[lo]
    return j

\end{python}
\subsubsection{3-way pivoting}
3-way pivoting: pivot the array into 3 subarrays:

$|..\leq..|..=..|..unseen..|..\geq..|$
\begin{python}
def pivot_3way(self, A, lo, hi):
    lt = lo-1  # pointing to end of array LT
    gt = hi  # pointing to the end of array GT (reversed)

    v = A[lo]
    i = lo  # scanning pointer
    while i < gt:
        if A[i] < v:
            lt += 1
            A[lt], A[i] = A[i], A[lt]
            i += 1
        elif A[i] > v:
            gt -= 1
            A[gt], A[i] = A[i], A[gt]
        else:
            i += 1

    return lt+1, gt
\end{python}
\subsection{Merge Sort}
TODO
\section{Properties}
\subsection{Stability}
Definition: a stable sort preserves the \textbf{relative order of items with equal keys} (scenario: sorted by time then sorted by location).

Algorithms:
\begin{enumerate}
\item Stable
\begin{enumerate}
\item Merge sort
\item Insertion sort
\end{enumerate}
\item Unstable
\begin{enumerate}
\item Selection sort
\item Shell sort
\item Quick sort
\item Heap sort
\end{enumerate}
\end{enumerate}
\textbf{Long-distance swap} operation is the key to find the unstable case during sorting.
\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[scale=.50]{stable_sort}}
\caption{Stale sort vs. unstable sort}
\label{fig:trie}
\end{figure}

\subsection{Sort Applications}
\begin{enumerate}
\item Sort
\item Partial quick sort (selection), k-th largest elements
\item Binary search
\item Find duplicates
\item Graham scan
\item Data compression
\end{enumerate}

\subsection{Considerations}
\begin{enumerate}
\item Stable?
\item Distinct keys?
\item Need guaranteed performance?
\item Linked list or arrays?
\item Caching system? (reference to neighboring cells in the array?
\item Usually randomly ordered array?
(or partially sorted?)\item Parallel?
\item Deterministic?
\item Multiple key types?
\end{enumerate}

$O(N\lg N)$ is the lower bound of comparison-based sorting; but for other
contexts, we may not need $O(N \lg N)$:
\begin{enumerate}
\item Partially-ordered arrays: insertion sort to achieve $O(N)$. \textbf{Number of inversions}: 1 inversion $=$ 1 pair of keys that are out
of order.
\item Duplicate keys
\item Digital properties of keys: radix sort to achieve $O(N)$.
\end{enumerate}

\subsection{Summary}
\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[scale=0.80]{sort_summary}}
\caption{Sort summary}
\label{fig:trie}
\end{figure}
\section{Partial Quicksort}
\subsection{Find $m$ smallest}
\runinhead{Heap-based solution.} $O(n \log m)$
\runinhead{Partial Quicksort}  Then the $A[:m]$ is sorted $m$ smallest. The algorithm recursively sort the $A[i:j]$

The average time complexity is
\begin{eqnarray*}
F(n) = \left\{ \begin{array}{rl}
  F(\frac{n}{2})+O(n) &\mbox{// if $\frac{n}{2} \geq m$} \\
  2F(\frac{n}{2})+O(n) &\mbox{// otherwise}
       \end{array} \right.
\end{eqnarray*}
Therefore, the complexity is $O(n+m \log m)$.
\begin{python}
def partial_qsort(self, A, i, j, m):
    if i >= j: return

    p = self.pivot(A, i, j)
    self.partial_qsort(A, i, p, m)
    if p+1 >= m: return
    self.partial_qsort(A, p+1, j, m)
\end{python}

\subsection{Find $k$-th}
Use partial quick sort to find $k$-th smallest element in the unsorted array. The algorithm recursively sort the $A[i:j]$

The average time complexity is
\begin{align*}
F(n) &= F(n/2) + O(n) \\
&= O(n)
\end{align*}
\begin{python}
def find_kth(self, A, i, j, k):
    if i >= j: return

    p = self.pivot(A, i, j)
    if p == k: return A[p]
    if p > k:  return self.find_kth(A, i, p, k)
    else:      return self.find_kth(A, p+1, j, k)
\end{python}
Pivoting see section - \ref{section:pivot}.

\section{Inversion}
If $a_i > a_j$ but $i<j$, then this is considered as 1 Inversion. That is, for an element, the count of other elements that are \textit{larger} than the element but appear \textit{before} it. This is the default definition.

There is also an alternative definition: for an element, the count of other elements that are \textit{samller} than the element but appear \textit{after} it.

\subsection{MergeSort \& Inversion Pair}
MergeSort to calculate the reverse-ordered paris. The only difference from a normal
merge sort is that - when pushing the 2nd half of the array to the place, you calculate
the inversion generated by the element $A_2[i_2]$ compared to $A_1[i_1:]$.

\begin{python}
def merge(A1, A2, A):
  i1 = i2 =0
  ret = 0
  for i in xrange(len(A)):
    if i1 == len(A1):
      A[i] = A2[i2]
      i2 += 1
    elif i2 == len(A2):
      A[i] = A1[i1]
      i1 += 1
    else:
      # use array diagram to illustrate
      if A1[i1] > A2[i2]:  # push the A2 to A
        A[i] = A2[i2]
        i2 += 1
        # number of reverse-ordered pairs
        ret += len(A1) - i1
      else:
        A[i] = A1[i1]
        i1 += 1

  return ret

def merge_sort(a):
  n = len(a)
  if n == 1:
    return 0

  a1 = a[:n/2]
  a2 = a[n/2:]

  ret1 = merge_sort(a1)
  ret2 = merge_sort(a2)
  # merge not merge_sort
  ret = ret1+ret2+merge(a1, a2, a)
  return ret
\end{python}

\subsection{Binary Index Tree \& Inversion Count}
Given $A$, calculate each element's inversion number.

Construct a BIT (\ref{BIT}) with length $max(A)+1$. Let BIT maintains the index of values. Scan the element from left to right (or right to left depends on the definition of inversion number), and set the index equal val to 1. Use the prefix sum to get the inversion number.

\pyinline{get(end) - get(a)} get the count of number that appears \textit{before} $a$ (i.e. already in the BIT) and also \textit{larger} than $a$.

Possible to extend to handle duplicate number.
\\
Core clues:
\begin{enumerate}
\item BIT maintains \textbf{index of values} to count the number of at each value.
\item \pyinline{get(end) - get(a)} to get the inversion count of $a$.
\end{enumerate}
\begin{python}
def inversion(self, A):
    bit = BIT(max(A)+1)
    ret = []
    for a in A:
        bit.set(a, 1)  # += 1 if possible duplicate
        inversion = bit.get(max(A)+1) - bit.get(a)
        ret.append(inversion)

    return ret
\end{python}

\subsection{Segment Tree \& Inversion Count}\label{segmentTreeInversionCount}
Compared to BIT, Segment Tree can process queries of both $idx \rightarrow sum$ and $sum \rightarrow idx$; while BIT can only process $idx \rightarrow sum$.

Core clues:
\begin{enumerate}
\item Segment Tree maintains \textbf{index of values} to count the number of at each value.
\item \pyinline{get(root, end) - get(root, a)} to get the inversion count of $a$.
\end{enumerate}
\begin{python}
class SegmentTree(object):
  def __init__(self):
    self.root = None

  def build(self, root, lo, hi):
    if lo >= hi: return
    if not root: root = Node(lo, hi)

    root.left = self.build(root.left, lo, (lo+hi)/2)
    if root.left:
      root.right = self.build(root.right, (lo+hi)/2, hi)

    return root

  def set(self, root, i, val):
    if root.lo == i and root.hi-1 == root.lo:
      root.cnt_this += val
    elif i < (root.lo+root.hi)/2:
      root.cnt_left += val
      self.set(root.left, i, val)
    else:
      self.set(root.right, i, val)

  def get(self, root, i):
    if root.lo == i and root.hi-1 == root.lo:
      return root.cnt_left
    elif i < (root.lo+root.hi)/2:
      return self.get(root.left, i)
    else:
      return (
          root.cnt_left + root.cnt_this +
          self.get(root.right, i)
      )

class Solution(object):
  def _build_tree(self, A):
    st = SegmentTree()
    mini, maxa = min(A), max(A)
    st.root = st.build(st.root, mini, maxa+2)
    # maxa+1 is the end dummy
    return st

  def countOfLargerElementsBeforeElement(self, A):
    st = self._build_tree(A)
    ret = []
    end = max(A)+1
    for a in A:
      ret.append(
          st.get(st.root, end) - st.get(st.root, a)
      )
      st.set(st.root, a, 1)

    return ret
\end{python}

\subsection{Reconstruct Array from Inversion Count}\label{inversionReconstruct}
Given a \textit{sorted} numbers with their associated inversion count (\# larger numbers before this element). $A[i].val$ is the value of the number, $A[i].inv$ is the inversion number. Reconstruct the original array $R$ that consists of each $A[i].val$.

Brute force can be done in $O(n^2)$. Put the $A[i].val$ into $R$ at an index/slot s.t. the \# \textit{empty} slots before it equals to $A[i].inv$.

\rih{BST}. Possible to use BST to maintain the empty slot indexes in the original array. Each node's rank indicates the count of empty indexes in its left subtree. But need to maintain the deletion.

\rih{Segment Tree}. Use a segment tree to maintain the size of empty slots. Each node has a $start$ and a $end$ s.t slot indexes $\in [start, end)$. Go down to find the target slot, go up to decrement the size of empty slots.

Reconstruction of array cannot use BIT since there is no map of $prefixSum \rightarrow i$.
\newpage
\begin{python}
class Node(object):
  def __init__(self, start, end, cnt):
    self.start = start
    self.end = end
    self.cnt = cnt

    self.left = None
    self.right = None

  def __repr__(self):
    return repr("[%d,%d)" % (self.start, self.end))

class SegmentTree(object):
  """empty space"""
  def __init__(self):
    self.root = None

  def build(self, start, end):
    """a node can have right ONLY IF has left"""
    if start >= end:
      return

    root = Node(start, end, end-start)
    root.left = self.build(start, (end+start)/2)
    if root.left:
      root.right = self.build((start+end)/2, end)
    return root

  def find_delete(self, root, val):
    """
    :return: index
    """
    root.cnt -= 1
    if not root.left:
      return root.start
    elif root.left.cnt >= val:
      return self.find_delete(root.left, val)
    else:
      return self.find_delete(root.right,
                              val - root.left.cnt)

class Solution(object):
  def reconstruct(self, A):
    st = SegmentTree()
    n = len(A)
    st.root = st.build(0, n)
    A = sorted(A, key=lambda x: x[0])
    ret = [0]*n
    for a in A:
      idx = st.find_delete(st.root, a[1]+1)
      ret[idx] = a[0]

    return ret

if __name__ == "__main__":
  A = [(5, 0), (2, 1), (3, 1), (4, 1,), (1, 4)]
  assert Solution().reconstruct(A) == [5, 2, 3, 4, 1]
\end{python}


\section{Testing for Weak Instruments}
Testing for presence of weak instruments is, at the time of writing, an active field of research. For a detailed overview, see \cite{stock2002survey}. For the purpose of our study, we limit our attention to two tests - the widely-used first-stage F-statistic and the Anderson-Rubin Test, which has gained resurgence in recent years in light of new developments in instrumental variables research.

\subsection{Defining the `Weakness' precisely}
\cite{stock2002testing} posit that the definition of weak instruments depends on the inferential task to be carried out, and cannot be resolved in the abstract. One approach is to define a set of instruments to be weak if $\mu^2/K$
is small enough that inferences based on conventional normal
approximating distributions are misleading. For instance, if a researcher wants their 2SLS estimate bias to be small, one measure
of whether an instrument(s) is strong is whether $\mu^2/K$ is
large enough such that the 2SLS relative bias (relative to the bias of ordinary least squares) is below a certain threshold, for example the relative bias is below 10\%. Hence, to be deemed a `weak' instrument, the 2SLS estimate using that instrument should have relative bias above 10\%. The definition we discussed (and use for our simulation) is based on relative bias, another definition (for instance on size of test) may result in a different cut-off value.

\subsection{First Stage F-statistic}
The first-stage F-statistic is the F-statistic testing the hypothesis that the coefficients on the instruments equal zero ($\pi=0$) in the first stage of two stage least squares.
\cite{stock2002testing} show that the definition of weak instruments discussed above implies a threshold value for $\mu^2/K$, under weak asymptotics. A weak instrument will have a $\mu^2/K$ value (and hence, an F-statistic value, since F$-$1 can be treated as an estimator of $\mu^2/K$ as discussed in Section 2.2) lower than the threshold.
For the case of a single endogenous regressor, \cite{staiger1997stock} provide a rule-of thumb threshold of 10: a value less than 10 indicates that the instruments are weak, in which case the 2SLS estimator is biased and 2SLS t-statistics and confidence intervals are unreliable.

\cite{stock2002survey} provide a table listing critical values of the first-stage F-statistic such that the relative bias of 2SLS estimates is greater than 10\%, for different numbers of instruments. The authors arrived at those critical values based on weak-instrument asymptotic approximations. We include a subset of this table (which is relevant for our simulations) as Table B.1 in appendix B for reference.

\subsection{Anderson-Rubin Test}

The AR test is a hypothesis test that has the property of being valid whether instruments are strong, weak or even irrelevant ($\pi=0$). It tests the null hypothesis $\beta$ = $\beta_0$ using the statistic. It was proposed by \cite{anderson1949estimation}.

\begin{equation}
  AR(\beta) = \frac{(\mathbf Y- \mathbf X\beta)' P_z (\mathbf Y-\mathbf X\beta)/K}{(\mathbf Y-\mathbf X\beta)' \mathbf M_Z (\mathbf Y-\mathbf X\beta)/(N-K)}
        \end{equation}

One definition of the LIML estimator is that it minimizes
$AR(\beta)$.
With fixed instruments and normal errors, the quadratic
forms in the numerator and denominator of (3.1) are independent
chi-squared random variables under the null hypothesis,
and $AR(\beta_0)$ has an exact $F_{K,T - K}$ null distribution. Under the more general conditions of weak-instrument asymptotics, $AR(\beta_0)$ $\xrightarrow{\text{d}}$ ${\chi_k}^2/K$ under the null hypothesis, regardless of the
value of $\mu^2/K$. Thus the AR statistic provides a fully robust
test of the hypothesis $\beta$ = $\beta_0$.
\par The set of
values of $\beta$ that are not rejected by a 5\% Anderson–Rubin test will constitute a 95\% confidence
set for $\beta$. The logic behind the Anderson–Rubin statistic is that it
never assumes instrument relevance, and the AR confidence set will have a
coverage probability of 95\% in large samples, regardless of the strength or weakness of instruments.
In light of the importance given to the problem of weak instruments in recent years, this test has gained traction among econometricians, who increasingly advocate for its use for robust inference with weak instruments (See \cite{staiger1997stock}). Particularly, recent research has shown the AR confidence set to be optimal in the single-endogenous-regressor just-identified setting.


\documentclass[a4paper]{article}
\usepackage[colorlinks]{hyperref}
\usepackage{amsmath,esint}
\title{Green's Theorem}
\author{Naitree Zhu}
\date{Last modified: \today}
\begin{document}
\maketitle
In mathematics, Green's theorem\footnote{For more information:\url{http://en.wikipedia.org/wiki/Green\%27s_Theorem}} gives the relationship between a line integral around a simple closed curve $\partial\Sigma$ and a double integral over the plane region $\Sigma$ bounded by $\partial\Sigma$. It is named after George Green and is the two-dimensional special case of the more general Stokes' theorem.
\part{Theorem}
Let $\partial\Sigma$ be a positively oriented, piecewise smooth, simple closed curve in a plane, and let $\Sigma$ be the region bounded by $\partial\Sigma$. If P and Q are functions of (\textit{x, y}) defined on an open region containing $\Sigma$ and have continuous partial derivatives there, then
\begin{equation}
\iint_\Sigma \left(\frac{\partial Q}{\partial x}-\frac{\partial P}{\partial y}\right)\mathrm{d}x
\mathrm{d}y=\oint_{\partial\Sigma} \left\lbrace P\mathrm{d}x+Q\mathrm{d}y\right\rbrace
\end{equation}
where the path of integration along $\partial\Sigma$ is counterclockwise.
\part{Relationship to the Stokes theorem}
Green's theorem is a special case of the Kelvin–-Stokes theorem, when applied to a region in the \textit{xy}-plane.
\part{Relationship to the divergence theorem}
Considering only two-dimensional vector fields, Green's theorem is equivalent to the two-dimensional version of the divergence theorem
\begin{equation}
\iint_\Sigma\left(\nabla\cdot\boldsymbol{F}\right)\mathrm{d}S=\oint_{\partial\Sigma}\boldsymbol{F}\cdot\hat{n}\mathrm{d}l
\end{equation}
\textbf{F} is the two-dimensional vector field, and $\hat{n}$ is the outward-pointing unit normal vector on the boundary.
\section*{Proof}
Let $\boldsymbol{F}=(Q,-P)$. If angle of $\hat{n}$ to the x-axis is $\theta$,then outward-pointing unit normal vector $\hat{n}=(cos\theta-sin\theta)$, and $\mathrm{d}\boldsymbol{l}=(-sin\theta,cos\theta)\mathrm{d}l$, apply them to eq.~(2), we can easily deduce to eq.~(1) from eq.~(2).
\part{Area Calculation}
Green's theorem can be used to compute area by line integral,which can be achieved simply by choosing P and Q satisfying $\frac{\partial Q}{\partial x}-\frac{\partial P}{\partial y}=1$.
Thus, area of $\Sigma$ can be calculated by line integral which is
\begin{equation}
A=\oint_{\partial\Sigma} \left\lbrace P\mathrm{d}x+Q\mathrm{d}y\right\rbrace
\end{equation}
Possible formulas for calculating the area of $\Sigma$ include:
\[
A=\oint_{\partial\Sigma} x\mathrm{d}y=-\oint_{\partial\Sigma} y\mathrm{d}x=\frac{1}{2}\oint_{\partial\Sigma}\left(-y\mathrm{d}x+x\mathrm{d}y\right)
\]
\end{document}


%!TEX root = ../main.tex

%===============================================================================
%
%    Chapter: vectors
%
%===============================================================================

\chapter{Vectors}
\label{chapter:vectors}

In this chapter we'll learn how to manipulate multi-dimensional objects called vectors.						\index{vector|textbf}
Vectors are the precise way to describe directions in space.
We need vectors in order to describe physical quantities like forces, velocities, and accelerations.

Vectors are built from ordinary numbers,
which form the \emph{components} of the vector.
You can think of a vector as a list of numbers,
and \emph{vector algebra} as operations
performed on the numbers in the list.
Vectors can also be manipulated as geometric objects,
represented by arrows in space.
For instance, the arrow that corresponds to the vector $\vec{v}=(v_x,v_y)$ starts at the origin $(0,0)$
and ends at the point $(v_x,v_y)$.
The word vector comes from the Latin \emph{vehere},
which means \emph{to carry}.
Indeed, the vector $\vec{v}$ takes the point $(0,0)$ and carries it to the point $(v_x,v_y)$.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.4\textwidth]{figures/vectors/vector_components.pdf}
	\vspace{-2mm}
	\caption{	The vector $\vec{v}=(3,2)$ is an arrow in the Cartesian plane.
			The horizontal component of $\vec{v}$ is $v_x=3$
			and the vertical component  is $v_y=2$.}
	\label{fig:vector_components}
\end{figure}


\chapter{Quasi-atoms}

\begin{defn}
\emph{Quasi-atoms} funcoid~$\mathscr{A}$ is the funcoid $A\rightarrow \atoms^{\mathfrak{A}} A$ defined by the formula
$\rsupfun{\mathscr{A}} X = \atoms^{\mathfrak{A}} X$.
\end{defn}

This really defines a funcoid because $\atoms^{\mathfrak{A}} \bot = \emptyset$ and
$\atoms^{\mathfrak{A}}(X\cup Y) = \atoms^{\mathfrak{A}}X\cup\atoms^{\mathfrak{A}}Y$.

\begin{obvious}
$\mathscr{A}$ is a co-complete funcoid.
\end{obvious}

\begin{prop}
$\rsupfun{\mathscr{A}^{-1}} Y = \bigsqcup Y$.
\end{prop}

\begin{proof}
$Y \nasymp \left\langle \mathscr{A} \right\rangle^{\ast} X \Leftrightarrow Y
\nasymp \atoms^{\mathfrak{A}} X \Leftrightarrow \exists x \in
\atoms^{\mathfrak{A}} X, y \in Y : x \nasymp y \Leftrightarrow \exists y
\in Y : X \nasymp y \Leftrightarrow \text{(because $X$ is a principal filter)}
\Leftrightarrow X \nasymp \bigsqcup Y$.
\end{proof}

Note
$\rsupfun{\mathscr{A}} \mathcal{X} =
\bigsqcap^{\mathscr{F}}_{X \in \up \mathcal{X}}
\atoms^{\mathfrak{A}} X$;

$\rsupfun{\mathscr{A}^{-1}} \mathcal{Y} =
\bigsqcap^{\mathscr{F}}_{Y \in \up \mathcal{Y}} \bigsqcup Y$
($\mathcal{Y}$ is filter on the set of ultrafilters).

Can $\atoms^{\mathfrak{A}} \mathcal{X}$ be restored knowing $\supfun{\mathscr{A}} \mathcal{X}$?
Can $\bigsqcup \mathcal{Y}$ be restored knowing $\supfun{\mathscr{A}^{-1}} \mathcal{X}$?

\begin{prop}
(Provided that~$A$ is infinite) $\mathscr{A}$ is not complete.
\end{prop}

\begin{proof}
Take a nonprincipal ultrafilter~$x$. Then $\rsupfun{\mathscr{A}^{-1}}\{x\} = \bigsqcup\{x\} = x$
is a nonprincipal filter.
\end{proof}

\begin{conjecture}
There is such filter~$\mathcal{X}$ that $\rsupfun{\mathscr{A}} \mathcal{X}$ is non-principal.
\end{conjecture}

Does quasi-atoms funcoid define a more elegant replacement of $\atoms^{\mathfrak{A}}$? Does this concept have any use?


\chapter[\ulex]{Theory: \ulex}\label{ch:ulex-theory}

{\Large NOTE: this chapter has been integrated into a paper, and thereafter much improved.  In the near future, the paper will be re-adapted to replace this chapter.}

\section{Regular expressions}

Throughout this section, we assume an \emph{alphabet} $\Sigma$; any $a \in \Sigma$ is a \emph{symbol}.  Since we support unicode, $\Sigma$ can be quite large.  Our abstract regular expression (RE) language is as follows:

\Grammar{
\GFirst{\rm RE}{\epsilon}{empty string}
\GNext{\CS}{symbol set, $\CS \subseteq \Sigma$}
\GNext{\rm RE\cdot RE}{concatenation}
\GNext{\rm RE^*}{Kleene-closure}
\GNext{\rm RE \OR RE}{alternation (union)}
\GNext{\rm RE \AND RE}{intersection}
\GNext{\neg \rm RE}{negation}
}

Note that we treat symbol sets (\ie{}, character classes) as primitive; this matches the implementation strategy and simplifies the description of DFA generation.  With this representation, the empty set $\emptyset$ and the alphabet $\Sigma$ are both treated as symbol sets.  The former will yield an RE that matches no input (\ie{}, $\CL\Sem{\emptyset} = \emptyset$), and the latter will match any single symbol.  Notice also that our language of REs allows for intersection and negation in addition to the standard operations.

The semantics of our RE language are given in the form of a function $\Ls{-} \ : \ \mathrm{RE} \rightarrow \Sigma^*$ from REs to their corresponding language over $\Sigma$:

\begin{eqnarray*}
\Ls{\epsilon} 	&=& 	\epsilon \\
\Ls{\CS}		&=& 	\CS \\
\Ls{r\cdot s}	&=& 	\Ls{r} \cdot \Ls{s} \\
\Ls{r^*}		&=& 	\epsilon \cup \Ls{r}\cdot\Ls{r^*} \\
\Ls{r \OR s}	&=&		\Ls{r} \cup \Ls{s} \\
\Ls{r \AND s}	&=& 	\Ls{r} \cap \Ls{s} \\
\Ls{\neg r}		&=& 	\Sigma \setminus \Ls{r}
\end{eqnarray*}

\section{Derivatives}\label{sec:derivatives}

Brzozowski introduced \emph{derivatives} of regular expressions as an alternative means of DFA construction \cite{derivatives}.  His approach is attrative because it easily allows the language of REs to be extended with arbitrary boolean operations.  Further, it is intuitive, relatively easy to implement, goes directly from an RE to a DFA, and with some care in implementation can be made competitive with other DFA construction approaches.  We begin by introducing the notion of a derivative of some language $\CL$.

\begin{definition}  The \New{derivative} of a set of symbol sequences $\CL \subset \Sigma^*$ with respect to a finite symbol sequence $u$ is defined to be $D_u(\CL) = \{ v \ | \ u\cdot v \in \CL \}$.
\end{definition}

Derivatives give a very natural algorithm for DFA construction.  Before giving that algorithm, however, we need a means of computing derivatives for regular expressions.

\begin{definition} A regular expression $\RE$ is \New{nullable} if the language it defines contains the empty string, that is, if $\epsilon \in \Ls{\RE}$.
\end{definition}

We also need the following function:
\[ \delta(\RE) =
    \begin{cases}
        \epsilon & \textrm{if} \ \epsilon \in \CL\Sem{\RE} \\
        \emptyset & \textrm{if} \ \epsilon \notin \CL\Sem{\RE}
    \end{cases}
\]
The $\delta$ function takes REs to REs (recall that the empty set is a symbol set, which is an RE).  Intuitively, $\delta$ collapses an RE to the ``smallest'' RE with the same nullability.

The following function, due to Brzozowski, gives the derivative of a regular expression with respect to a symbol $a$.
\begin{eqnarray*}
D_a (\epsilon)  &=& \emptyset \\
D_a (\CS)         &=&
    \begin{cases}
        \epsilon & \textrm{if} \ a \in \CS \\
        \emptyset & \textrm{if} \ a \notin \CS \\
    \end{cases} \\
D_a (r \cdot s) &=& D_a(r)\cdot s \OR \delta(r) \cdot D_a(s) \\
D_a (r^*)       &=& D_a(r) \cdot r^* \\
D_a (r \OR s)   &=& D_a(r) \OR D_a(s) \\
D_a (r \AND s)  &=& D_a(r) \AND D_a(s) \\
D_a (\neg r)    &=& \neg D_a(r)
\end{eqnarray*}

We can take the derivative of an RE with respect to a sequence of symbols in a straightforward way:
\begin{eqnarray*}
D_\epsilon (r) &=& r \\
D_{ua} (r) &=& D_a(D_u(r))
\end{eqnarray*}

Intuitively, the derivative of an RE with respect to a symbol $a$ yields a new RE after matching $a$.  The following two theorems, again due to Brzozowski, make this precise.

\begin{theorem} The derivative $D_s(\RE)$ of any regular expression $\RE$ with respect to any sequence $u$ is a regular expression.
\end{theorem}

\begin{theorem} A sequence $u$ is contained in $\Ls{\RE}$ if and only if $\Ls{D_u(\RE)}$ is nullable.
\end{theorem}

Derivatives provide an easy method of DFA construction.  Suppose we want to build a DFA that recognizes $\RE$.  We can think of each state of the DFA as a regular expression.  We start with a state $Q_0$ that represents $\RE$.  We then take the derivative of $\RE$ with respect to each symbol of the alphabet and create a new state each time a new derivative is found, adding each new state to the work list.  We pop a state from the work list and repeat, until the work list is empty.  There will be a transition from $Q_j$ to $Q_k$ if and only if (identifying states and their REs) $D_a (Q_j) = Q_k$ for some symbol $a$; the transition will be labeled with the set of all such $a$.  Finally, any state that represents a nullable RE is an accepting state.  The correctness of the recognizer is a direct consequence of the above theorems.

The sketch glosses over several important details.  First, what notion of equality do we intend for the equation $D_a (Q_j) = Q_k$?  Ideally, we would identify as a single state all those REs which admit the same language, so that $D_a (Q_j) = Q_k$ if and only if $\Ls{D_a (Q_j)} = \Ls{Q_k}$.  This is expensive to compute, so Brzozowski introduced the notion of RE similarity, an equivalence on REs which is easy to compute but still guarantees that the DFA is finite.

Let $\approx$ denote the least equivalence relation on REs such that
\begin{eqnarray*}
r \OR r &\approx& r \\
r \OR s &\approx& s \OR r \\
(r \OR s) \OR t &\approx& r \OR (s \OR t)
\end{eqnarray*}

\begin{definition} Two regular expressions $r$ and $s$ are \New{similar} if $r \approx s$ and are \New{dissimilar} otherwise.
\end{definition}

\begin{theorem} Every regular expression has only a finite number of dissimilar derivatives.
\end{theorem}

Hence, DFA construction is guaranteed to succeed if new states are only created when no existing state is similar to a given derivative.  In fact, we want to do much better then this to avoid blowup in DFA size.

\begin{remark}
In a practical implementation of DFA construction using derivatives, it is crucial to aggresively identify when a derivative admits the same language as an existing state (RE) in the DFA.  The cost of this identification must be balanced against the number of duplicate states avoided.
\end{remark}

In \ulex{}, we accomplish this by canonicalizing all input and derived REs.  The canonicalization is described in detail in section~\ref{sec:reg-exp}.

\section{Factorings}\label{sec:factorings}

Another problem with DFA construction is the size of the unicode alphabet: taking the derivative with respect to each unicode symbol is not feasible.  But to construct the DFA, we have to examine every possible derivative of a given RE.  We must try to conservatively estimate what sets of symbols will yield the same derivative for an RE.  Here we break from Brzozowski's work and introduce new terminology and an algorithm to make derivatives more amenable to large alphabets.

Let $\sim_\RE$ be the relation defined as follows.  For a regular expression $\RE$ and symbols $a, b$, $a \sim_\RE b$ if and only if $D_a (\RE) = D_b (\RE)$.

\begin{definition}
The \New{derivative classes} of $\RE$ are the the equivalence classes $\Sigma/{\sim_\RE}$.
\end{definition}

Ultimately, the outedges for a DFA state and the derivative classes of the RE for that state are in one-to-one correspondence.\footnote{This is not quite true: we usually drop error transitions, that is, transitions going to the RE $\emptyset$.}   Hence, we must eventually determine all the derivative classes for an RE in order to construct the DFA.  To avoid testing the entire alphabet a symbol at a time, we introduce an algorithm which (over)partitions $\Sigma$, so that each partition is a subset of a derivative class.   We can then take the derivative with respect to a representative from each partition, and determine which partitions actually belong to the same derivative class.

\begin{definition}
Let $r$ be an RE.  A \New{factoring} of $\Sigma$ under $r$ is a partitioning of $\Sigma$ such that each partition is a subset of a derivative class for $\RE$.
\end{definition}

To be clear: we are factoring the \emph{alphabet} into partitions, but the factoring is guided by (\emph{under}) a regular expression.  A factoring under a given RE is not unique.  The derivative classes for an RE are one possible factoring (with a minimal number of partitions) while the set of all singleton sets of symbols is another factoring (with a maximal number of partitions).  We will present a simple recursive factoring algorithm and prove its correctness, but first, an example.

Suppose we have two regular expressions $r$ and $s$ yielding factorings $\{ \CR_1, \CR_2 \}$ and $\{ \CS_1, \CS_2 \}$ respectively.  Let $t = r \OR s$.  The derivative of $t$ with respect to some symbol $a$ is $D_a(t) = D_a(r) \OR D_a(s)$.  Hence, if $D_a(r) = D_b(r)$ and $D_a(s) = D_b(s)$ for some symbols $a, b$, then $D_a(t) = D_b(t)$ and so $a \sim_t b$.  We can use this to give a factoring under $t$.  The relationship between the factorings under $r$, $s$ and $t$ can be visualized as follows:

\[
  \xymatrix{
    \bullet \ar@{-}[rrr]|{\Sigma} &&& \bullet \\
    \bullet \ar@{-}[rr]|{\CR_1} && \bullet \ar@{-}[r]|{\CR_2} & \bullet \\
    \bullet \ar@{-}[r]|{\CS_1} & \bullet \ar@{-}[rr]|{\CS_2} &&  \bullet \\
    \bullet \ar@{-}[r]|{\CR_1 \cap \CS_1} &
    \bullet \ar@{-}[r]|{\CR_1 \cap \CS_2} &
    \bullet \ar@{-}[r]|{\CR_2 \cap \CS_2} &
    \bullet
  }
\]

This small example captures the essential idea of the algorithm.  To give a factoring under an RE, we recursively find factorings under its components and ``compress'' those factorings into a single new factoring that respects them.  The factorings are being compressed (flattened) in the sense that the boundaries of one factoring are forced onto another, causing some partitions to split.  The algorithm we present is in two stages: first, a factoring function recurively collects factorings under an RE; then, a compress function compresses them all onto $\Sigma$ to produce a single factoring for an RE.  We now make this precise.

The \emph{factoring} function $F$ takes a regular expression and gives a factoring of $\Sigma$ under that RE.  It is defined recursively as follows:
\begin{eqnarray*}
F(\epsilon)     &=& \emptyset \\
F(\CS)          &=& \{ \CS \} \\
F(r \cdot s)    &=&
    \begin{cases}
        F(r) & \epsilon \notin \Ls{r} \\
        F(r) \cup F(s) & \textrm{otherwise}
    \end{cases} \\
F(r \OR s)      &=& F(r) \cup F(s) \\
F(r \AND s)     &=& F(r) \cup F(s) \\
F(r^*)          &=& F(r) \\
F(\neg r)       &=& F(r)
\end{eqnarray*}

The \emph{compress} function $C : \CP(\Sigma) \longrightarrow \CP(\Sigma)$ takes a set of subsets of the alphabet and produces the smallest partitioning of $\Sigma$ that respects them.  In particular, if
\[ C(\{\CS_1, \CS_2, \dots, \CS_m \}) = \{ \CS'_1, \CS'_2, \dots, \CS'_n \} \]
then we have that $\{ \CS'_1, \CS'_2, \dots, \CS'_n \}$ is a partitioning of $\Sigma$ such that for each $\CS'_i$ and $\CS_k$ either $\CS'_i \subseteq \CS_k$ or $\CS'_i \cap \CS_k = \emptyset$.

\begin{theorem}  Let $\RE$ be an RE.  Then $C(F(r))$ is a factoring of $\Sigma$ under $\RE$.
\end{theorem}

\emph{Proof:} by induction on the structure of $\RE$.  We use $a$ to denote an arbitrary symbol.

\vskip 5pt
\emph{Case} $\epsilon$: we have $D_a(\epsilon) = \emptyset$ for all $a \in \Sigma$, so $\Sigma/{\sim_\epsilon} = \{ \Sigma \}$.  We have $C(F(\epsilon)) = C(\{ \emptyset \})= \{ \Sigma \}$.

\vskip 5pt
\emph{Case} $\CS$: we have $D_a(\CS) = \epsilon$ if $a \in \CS$ and $D_a(\CS) = \emptyset$ otherwise. Thus the derivative classes are $\CS$ and $\Sigma \setminus \CS$, which are exactly the sets produced by $C(F(\CS)) = C(\{ \CS \})$.

\vskip 5pt
\emph{Case} $s \cdot t$ and $\epsilon \notin \Ls{s}$:  here $D_a(s \cdot t) = D_a(s) \cdot t$. Because $t$ is fixed as $a$ varies, the derivative classes are just the derivative classes of $s$.  Since $F(s \cdot t) = F(s)$ the result holds by the induction hypothesis on $s$.

\vskip 5pt
\emph{Case} $s \cdot t$ and $\epsilon \in \Ls{s}$: here $D_a(s \cdot t) = D_a(s) \cdot t \OR \epsilon \cdot D_a(t)$.  Let $b, c \in \Sigma$ such that $b \sim_s c$ and $b \sim_t c$.  Then $b \sim_{s \cdot t} c$.  The result follows from this fact and the inductive hypothesis applied to $s$ and $t$.

\vskip 5pt
The other cases are similar.


%---------------------------Shear---------------------------
\section{Shear\label{s:hex-shear}}

The shear metric is the minimum of the Jacobian matrix
evaluated at the element corners divided by the product of the length of the 3
edge vectors meeting at that corner:
\[
  q = \min_{i\in\{0,1,\ldots,8\}}
  \left\{
    \hat \alpha_i
  \right\}.
\]

Note that if $\hat \alpha_i \leq DBL\_MIN$ for any $i$ or if ${L_{\min}}^2 \leq DBL\_MIN$,
we set $q = 0$.

\hexmetrictable{shear}%
{$1$}%                                        Dimension
{$[0.3,1]$}%                                  Acceptable range
{$[0,1]$}%                                    Normal range
{$[0,1]$}%                                    Full range
{$1$}%                                        Cube
{\cite{knu:03}}%                              Citation
{v\_hex\_shear}%                              Verdict function name


\setchapterstyle{kao}
\setchapterpreamble[u]{\margintoc}
\chapter{Mathematics and Boxes}
\labch{mathematics}

\section{Theorems}

Despite most people complain at the sight of a book full of equations,
mathematics is an important part of many books. Here, we shall
illustrate some of the possibilities. We believe that theorems,
definitions, remarks and examples should be emphasised with a shaded
background; however, the colour should not be to heavy on the eyes, so
we have chosen a sort of light yellow.\sidenote{The boxes are all of the
same colour here, because we did not want our document to look like
\href{https://en.wikipedia.org/wiki/Harlequin}{Harlequin}.}

\begin{definition}
\labdef{openset}
Let $(X, d)$ be a metric space. A subset $U \subset X$ is an open set
if, for any $x \in U$ there exists $r > 0$ such that $B(x, r) \subset
U$. We call the topology associated to d the set $\tau\textsubscript{d}$
of all the open subsets of $(X, d).$
\end{definition}

\refdef{openset} is very important. I am not joking, but I have inserted
this phrase only to show how to reference definitions. The following
statement is repeated over and over in different environments.

\begin{theorem}
A finite intersection of open sets of (X, d) is an open set of (X, d),
i.e $\tau\textsubscript{d}$ is closed under finite intersections. Any
union of open sets of (X, d) is an open set of (X, d).
\end{theorem}

\begin{proposition}
A finite intersection of open sets of (X, d) is an open set of (X, d),
i.e $\tau\textsubscript{d}$ is closed under finite intersections. Any
union of open sets of (X, d) is an open set of (X, d).\marginnote{You can even insert footnotes inside the theorem
	environments; they will be displayed at the bottom of the box.}
\end{proposition}

\begin{lemma}
A finite intersection\footnote{I'm a footnote} of open sets of (X, d) is
an open set of (X, d), i.e $\tau\textsubscript{d}$ is closed under
finite intersections. Any union of open sets of (X, d) is an open set of
(X, d).
\end{lemma}

You can safely ignore the content of the theorems\ldots I assume that if
you are interested in having theorems in your book, you already know
something about the classical way to add them. These example should just
showcase all the things you can do within this class.

\begin{corollary}[Finite Intersection, Countable Union]
A finite intersection of open sets of (X, d) is an open set of (X, d),
i.e $\tau\textsubscript{d}$ is closed under finite intersections. Any
union of open sets of (X, d) is an open set of (X, d).
\end{corollary}

\begin{proof}
The proof is left to the reader as a trivial exercise. Hint: \blindtext
\end{proof}

\begin{definition}
Let $(X, d)$ be a metric space. A subset $U \subset X$ is an open set
if, for any $x \in U$ there exists $r > 0$ such that $B(x, r) \subset
U$. We call the topology associated to d the set $\tau\textsubscript{d}$
of all the open subsets of $(X, d).$\marginnote{
	Here is a random equation, just because we can:
	\begin{equation*}
  x = a_0 + \cfrac{1}{a_1
          + \cfrac{1}{a_2
          + \cfrac{1}{a_3 + \cfrac{1}{a_4} } } }
	\end{equation*}
}
\end{definition}

\begin{example}
Let $(X, d)$ be a metric space. A subset $U \subset X$ is an open set
if, for any $x \in U$ there exists $r > 0$ such that $B(x, r) \subset
U$. We call the topology associated to d the set $\tau\textsubscript{d}$
of all the open subsets of $(X, d).$
\end{example}

\begin{remark}
Let $(X, d)$ be a metric space. A subset $U \subset X$ is an open set
if, for any $x \in U$ there exists $r > 0$ such that $B(x, r) \subset
U$. We call the topology associated to d the set $\tau\textsubscript{d}$
of all the open subsets of $(X, d).$
\end{remark}

As you may have noticed, definitions, example and remarks have
independent counters; theorems, propositions, lemmas and corollaries
share the same counter.

\begin{remark}
Here is how an integral looks like inline: $\int_{a}^{b} x^2 dx$, and
here is the same integral displayed in its own paragraph:
\[\int_{a}^{b} x^2 dx\]
\end{remark}

We provide two files for the theorem styles:
\href{style/plaintheorems.sty}{plaintheorems.sty}, which you should
include if you do not want coloured boxes around theorems; and
\href{style/mdftheorems.sty}{mdftheorems.sty}, which is the one used for
this document.\sidenote{The plain one is not showed, but actually it is
exactly the same as this one, only without the yellow boxes.} Of course,
you will have to edit these files according to your taste and the
general style of the book.

\section[Boxes \& Environments]{Boxes \& Custom Environments
\sidenote[*1.6][]{Notice that in the table of contents and in the
	header, the name of this section is \enquote{Boxes \& Environments};
	we achieved this with the optional argument of the \texttt{section}
	command.}}

Say you want to insert a special section, an optional content or just
something you want to emphasise. We think that nothing works better than
a box in these cases. We used \Package{mdframed} to construct the ones
shown below. You can create and modify such environments by editing the
provided file \href{style/environments.sty}{environments.sty}.

\begin{kaobox}[frametitle=Title of the box]
\blindtext
\end{kaobox}

If you set up a counter, you can even create your own numbered
environment.

\begin{kaocounter}
	\blindtext
\end{kaocounter}

\section{Experiments}

It is possible to wrap marginnotes inside boxes, too. Audacious readers
are encouraged to try their own experiments and let me know the
outcomes.

\marginnote[-2.2cm]{
	\begin{kaobox}[frametitle=title of margin note]
		Margin note inside a kaobox.\\
		(Actually, kaobox inside a marginnote!)
	\end{kaobox}
}

I believe that many other special things are possible with the
\Class{kaobook} class. During its development, I struggled to keep it as
flexible as possible, so that new features could be added without too
great an effort. Therefore, I hope that you can find the optimal way to
express yourselves in writing a book, report or thesis with this class,
and I am eager to see the outcomes of any experiment that you may try.

%\begin{margintable}
	%\captionsetup{type=table,position=above}
	%\begin{kaobox}
		%\caption{caption}
		%\begin{tabular}{ |c|c|c|c| }
			%\hline
			%col1 & col2 & col3 \\
			%\hline
			%\multirow{3}{4em}{Multiple row} & cell2 & cell3 \\ & cell5
			%%& cell6 \\
			%& cell8 & cell9 \\
			%\hline
		%\end{tabular}
	%\end{kaobox}
%\end{margintable}


\documentclass{article}

\usepackage{amsmath}
\usepackage{setspace}
\usepackage[margin=1.0in]{geometry}
\usepackage{graphicx}
\graphicspath{ {../oldPlots} }

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

\begin{document}

%\addtolength{\oddsidemargin}{-.875in}
%\addtolength{\evensidemargin}{-.875in}
%\addtolength{\textwidth}{1.75in}
\setlength{\parskip}{0.08cm}

\title{nflMarkov: k of n}

\date{\today}

\maketitle

\tableofcontents

\newpage
%%%%%%%%
\setcounter{footnote}{0}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{1 of n: a basic markov chain}

This is the first in a series of posts describing my work on a Markov chain tool to analyze football. The code is available from github,
{\tt https://github.com/bdilday/nflMarkov}

Before talking about football, I want to talk about a less complicated problem. It is probably worth saying at this point that I am not a mathematician, nor a computer scientist. What I am is an astronomer and astrophysicist, so obviously I know some math and some programming, but at the same time I'm not necessarily an expert on Markov chains. As with most things, what I know is what I've picked up in order to work on research problems, so what I have to say may be obvious, or naive. In any case, writing this out is in large part for my own sake, just to see it all layed out and to help myself clarify my thoughts on the problem; as well as to hopefully be useful people who read this without much knowledge of Markov chains coming in.

The first simple problem I want to talk about, which is pretty much the simplest non-trivial Markov chain problem I can think of, is this; imagine a random walk where there are 5 possible states which I will call $s_{-2}, s_{-1}, s_{0}, s_{+1}, s_{+2}$. In each step of the process, the walker can move one step to the right, which happens with probability p, or one step to the left, which happens with probability q=1-p. If the walker lands at position +2 or -2 they stay there. Here is what the transition matrix looks like, \\

$
T_{ij} = \left(
\begin{array}{ccccc}
 1 & 1-p & 0 & 0 & 0 \\
 0 & 0 & 1-p & 0 & 0 \\
 0 & p & 0 & 1-p & 0 \\
 0 & 0 & p & 0 & 0 \\
 0 & 0 & 0 & p & 1 \\
\end{array}
\right)$ \\

To be clear, this is the probability to transition {\it to} the state i {\it from} the state j. If I iterate once, i.e., compute $ T^2 = T_{ik}T_{kj}$ (the is the Einstein notation for maytrix multiplication), I get, \\

$T^{2} =
\left(
\begin{array}{ccccc}
 1 & 1-p & (1-p)^2 & 0 & 0 \\
 0 & (1-p) p & 0 & (1-p)^2 & 0 \\
 0 & 0 & 2 (1-p) p & 0 & 0 \\
 0 & p^2 & 0 & (1-p) p & 0 \\
 0 & 0 & p^2 & p & 1 \\
\end{array}
\right)
$ \\

Two times, \\

$ T^{3} =
\left(
\begin{array}{ccccc}
 1 & p (1-p)^2-p+1 & (1-p)^2 & (1-p)^3 & 0 \\
 0 & 0 & 2 (1-p)^2 p & 0 & 0 \\
 0 & 2 (1-p) p^2 & 0 & 2 (1-p)^2 p & 0 \\
 0 & 0 & 2 (1-p) p^2 & 0 & 0 \\
 0 & p^3 & p^2 & (1-p) p^2+p & 1 \\
\end{array}
\right)
$ \\

Three times, \\

$ T^{4} =
\left(
\begin{array}{ccccc}
 1 & p (1-p)^2-p+1 & p (1-p)^3+\left(p (1-p)^2-p+1\right) (1-p) & (1-p)^3 & 0 \\
 0 & 2 (1-p)^2 p^2 & 0 & 2 (1-p)^3 p & 0 \\
 0 & 0 & 4 (1-p)^2 p^2 & 0 & 0 \\
 0 & 2 (1-p) p^3 & 0 & 2 (1-p)^2 p^2 & 0 \\
 0 & p^3 & (1-p) p^3+\left((1-p) p^2+p\right) p & (1-p) p^2+p & 1 \\
\end{array}
\right)
$ \\

You can stare at these and start to see the structure of how you move from state to state as you iterate the Markov chain, but I think the main usefulness is just to illustrate that repeated applications of the transition matrix mix things up in a deterministic and straight-forward (if tedious to enumerate) way. For a simple problem like this you could probably even come up with a closed form solution.

Now let me put in some numbers and keep iterating the transition matrix until it converges. lets try p=1-p=0.5. If I iterate 100 times, and round to zero values less than $ 1 \times 10^{-6}$, I get, \\

$
T^{100} \approx T^{\infty} =
\left(
\begin{array}{ccccc}
 1. & 0.75 & 0.5 & 0.25 & 0 \\
 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & 0 & 0 \\
 0 & 0.25 & 0.5 & 0.75 & 1. \\
\end{array}
\right)
$ \\

So what exactly does this mean? It means if I start in the state $s_{-2}$ (all the way to the left), I end in the state $s_{-2}$ with probability 1. If I start in the state $s_{-1}$ I end in the state $s_{-2}$ with probability 0.75 and $s_{+2}$ with probability 0.25. If I start in the state $s_0$, I end in the state $s_{-2}$ with probability 0.5 and $s_{+2}$ with probability 0.5. In other words, reading down each column (j) tells me the probability to end in the state (i) after 100 (which may as well be infinity) transitions. \\

If I use p=0.501 instead, this is what I get, \\

$
\left(
\begin{array}{ccccc}
 1. & 0.748498 & 0.498 & 0.248502 & 0 \\
 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & 0 & 0 \\
 0 & 0.251502 & 0.502 & 0.751498 & 1. \\
\end{array}
\right)
$ \\

If I use p=0.99, I get, \\

$
\left(
\begin{array}{ccccc}
 1. & 0.010101 & 0.00010202 & 1.02 \times 10^{-6} & 0 \\
 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & 0 & 0 \\
 0 & 0.989899 & 0.999898 & 0.999999 & 1. \\
\end{array}
\right)
$ \\

This first post just shows that you can set up a Markov chain that models a random walk and make the states at the end "sinks" (or roach motels; once you enter, you never leave), and after iterating enough times, you will always end up in one of the sinks. In the football application, the sinks are going to be scoring events. So we can imagine that making it all the way to the right is like scoring a touchdown, all the way to the left, like giving up a safety. In the next part I'll look at the expectation values associated with such a Markov chain.

\section{2 of n: expectation values of a basic markov chain}

In part 1 I talked about a simple random walk in 1-dimension, where the states all the way to the left and all the way to the right are sinks (or roach motels). The next step from that is to ask what is the expectation value associated with each state? I mentioned that we could think of the state $s_{+2}$ as scoring a touchdown (+6 or +7 or +8 points) and $s_{-2}$ as giving up a safety (-2 points), but to illustrate the problem, it is convenient to assume that landing at location +2 is worth +1 point and at location -2, -1 point.

As a concrete example, let me take p=0.6 and ask what the expectation values are, given that I start in state $s_{-1}, s_{0},$ or $s_{+1}$. Iterating the transition matrix to convergence, as I did in part 1, I get, \\

$T^{\infty} =
\left(
\begin{array}{ccccc}
 1. & 0.584615 & 0.307692 & 0.123077 & 0 \\
 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & 0 & 0 \\
 0 & 0.415385 & 0.692308 & 0.876923 & 1. \\
\end{array}
\right)
$ \\

This says that if I start at state $s_0$, I end in state $s_{-2}$ with probability 0.308 and $s_{+2}$ with probability 0.692. If these are worth -1 and +1 points, respectively, then the expectation value for points will be $e_{0} = 0.692-0.308 = 0.384$. In a similar way I can see that the expectation values for states $s_{-1}$ and $s_{+1}$ are $e_{-1} = 0.415-0.585 = -0.169$ and $0.877-0.123 = 0.754$, respectively. I will use $p=0.6$ as sort of a test case and refer back to these values later. So this gives me the answer using the first method.

The question I want to address now is, other than iterating to convergence, how do we go from the transition matrix (which is relatively easy to code up for football) to the expectation value (which is what we really want to know)?
%I want to get the expectation values by explicitly setting up a system of equations for them. Although I can setup a transition matrix for football and iterate it, I think this will be a more straight-forward implementation.
Logically, the expectation value, $e_{0}$, when starting from the state $s_{0}$, should be, \\

$
e_0 = e_{-2} p_{-2,0} + e_{-1} p_{-1,0} + e_{0} p_{0,0} + e_{+1} p_{+1,0} + e_{+2} p_{+2,0}
$ \\

In words, its the expectation value of the subsequent state in the chain (for example, $ e_{-1}$), multiplied by the probability to transition to that state (for example $ p_{-1, 0}$). Inspection shows that this says (using Einstein notation for matrix/vector multiplication), \\

$
e_{i} = e_{k} T_{k,i}
$ \\

or, in vector notation, \\

$ \vec{e} = \vec {e} ~\vec{T}$, \\

or \\

$ \vec{e} ~(\vec{T} - \vec{1}) = 0$, where $ \vec{1}$ is the identity matrix. \\

For the particular transition matrix we are discussing, we have, \\

$
\left(
\begin{array}{c}
e_{-2} \\
e_{-1} \\
e_{0} \\
e_{+1} \\
e_{+2}
\end{array}
\right) =
\left(
\begin{array}{c}
e_{-2} \\
e_{-1} \\
e_{0} \\
e_{+1} \\
e_{+2}
\end{array}
\right)
\times
\left(
\begin{array}{ccccc}
 1 & 1-p & 0 & 0 & 0 \\
 0 & 0 & 1-p & 0 & 0 \\
 0 & p & 0 & 1-p & 0 \\
 0 & 0 & p & 0 & 0 \\
 0 & 0 & 0 & p & 1 \\
\end{array}
\right)
$ \\

Because $e_{m} T_{m,n} = T_{m,n} e_{m} = T'_{n,m} e_{m}$, where $T'$ is the transpose of $T$, I can also write this as, \\

$\vec{e} =
\left(
\begin{array}{ccccc}
 1 & 0 & 0 & 0 & 0 \\
 1-p & 0 & p & 0 & 0 \\
 0 & 1-p & 0 & p & 0 \\
 0 & 0 & 1-p & 0 & p \\
 0 & 0 & 0 & 0 & 1 \\
\end{array}
\right) \times
\vec{e}
$ \\

This way, with the matrix multiplying the vector from the left is the more common way to see a set of equations represented, and some code for numerically solving such an equation expect this convention. The values $ e_{-2}$ and $ e_{+2}$, however, aren't really variables, i.e. quantities that need to take on particular values for the set of equations to hold; they are parameters such that their values are arbitrary, and, once chosen, impact the expectation values for the non-sink states. \\

Let me denote by (s, t) states that are {\bf s}inks, and (n,m) states that are {\bf NOT} sinks. What I really want to know are the set of values $ e_{n}$, i.e. the expectation values for states that are {\bf not} sinks. So I can write, \\

$ e_{n} = e_{s} T_{s,n} + e_{m} T_{m,n}$ \\

Rearranging this equation gives, \\

$e_{m} ~(T_{m,n} - \delta_{m,n}) = - e_{s} T_{s,n}$ \\

or in vector notation, \\

$ \vec{e}^{n} ~(\vec{T}^{n} - \vec{1}) = - \vec{e}^{s} ~\vec{T}^{s}$ \\

Taking the transpose of both sides I get, \\

$ (\vec{T}^{n} - \vec{1})' ~\vec{e}^{n}  = - \vec{T'}^{s} ~\vec{e}^{s} $ \\

Note that the superscripts here denote sink or non-sink and {\bf do not} denote exponentiation. Also, let $S$ denote the number of sinks and $N$ the number of non-sinks; then the dimensions of $e^{n}, e^{s}, T'^{n}$, and $T'^{s}$ are
$N \times 1$, $S \times 1$, $N \times N$, and $N \times S$, respectively.

For the particular problem we've been discussing, this can be expanded as,

$
\left(
\begin{array}{ccc}
 0 & p & 0 \\
 1-p & 0 & p \\
 0 & 1-p & 0 \\
\end{array}
\right) \times
\left(
\begin{array}{c}
 e_{-1} \\
 e_{0} \\
 e_{+1} \\
\end{array}
\right) =
- \left(
\begin{array}{cc}
 1-p & 0 \\
 0 & 0 \\
 0 & p \\
\end{array}
\right) \times
\left(
\begin{array}{c}
 e_{-2} \\
 e_{+2} \\
\end{array}
\right)
$ \\

or \\

$
\left(
\begin{array}{ccc}
 0 & p & 0 \\
 1-p & 0 & p \\
 0 & 1-p & 0 \\
\end{array}
\right) \times
\left(
\begin{array}{c}
 e_{-1} \\
 e_{0} \\
 e_{+1} \\
\end{array}
\right) =
-\left(
\begin{array}{c}
 e_{-2} (1-p) \\
 0 \\
 e_{+1} p \\
\end{array}
\right)
$ \\

Note that once I choose values for $e_{-2}$ and $e_{+2}$, the the right hand side is just a constant vector. \\

$
\left(
\begin{array}{ccc}
 0 & p & 0 \\
 1-p & 0 & p \\
 0 & 1-p & 0 \\
\end{array}
\right) \times
\left(
\begin{array}{c}
 e_{-1} \\
 e_{0} \\
 e_{+1} \\
\end{array}
\right) =
\left(
\begin{array}{c}
 (1-p) \\
 0 \\
 -p \\
\end{array}
\right)
$ \\

The formal solution is, \\

$ ((\vec{T}^{n}-\vec{1})')^{-1} \cdot (- \vec{T}^{s} \cdot \vec{e}^{s})$ \\

$
=  \left(
\begin{array}{c}
 \frac{-{e_{-2}} p^3+{e_{+2}} p^3+2 {e_{-2}} p^2-2 {e_{-2}} p+{e_{-2}}}{2 p^2-2 p+1} \\
 \frac{{e_{-2}} (p-1)^2+{e_{+2}} p^2}{2 p^2-2 p+1} \\
 \frac{{e_{+2}} p \left(p^2-p+1\right)-{e_{-2}} (p-1)^3}{2 p^2-2 p+1} \\
\end{array}
\right)
$ \\

so if I set $e_{-2} = -1$ and $e_{+2} = +1$, then I get, \\

$
\left(
\begin{array}{c}
 \frac{2 p^3-2 p^2+2 p-1}{2 p^2-2 p+1} \\
 \frac{p^2-(p-1)^2}{2 p^2-2 p+1} \\
 \frac{(p-1)^3+p \left(p^2-p+1\right)}{2 p^2-2 p+1} \\
\end{array}
\right)
$ \\

and then if I further set $p = 0.6$, then I get, \\

$
\left(
\begin{array}{c}
 -0.169231 \\
 0.384615 \\
 0.753846 \\
\end{array}
\right)
$ \\

which is exactly what I got from iterating the transition matrix until it converges and then reading off the columns. One last thing to note, is that the equation, \\

$
\vec{e} \cdot \vec{T} = \vec{e}$ \\

from above implies that (if I multiply by $T$ from the right on both sides), \\

$\vec{e} \cdot \vec{T}^2 = \vec{e} \cdot \vec{T}$,  \\

but $\vec{e} \cdot \vec{T} = \vec{e}$, so \\

$\vec{e} \cdot \vec{T}^{2} = \vec{e}$,  \\

and it follows that, \\

$\vec{e} \cdot \vec{T}^{\infty} = \vec{e}$,  \\

which is exactly what we got by iterating the transition matrix to convergence.

\section{3 of n: accounting for turnovers}

In parts 1 and 2 I described a problem that could be modeled as a Markov chain and that roughly corresponds to the scoring of touchdowns and safetys. Now I will discuss introducing the concept of a turnover. In the orginal problem, the walker had a probability to move right of p, and left of (1-p). I will now model the case where a ``turnover'' occurs with probability a. This reverses right and left, so that the walker moves left with probability p and right with probability (1-p). The most straight-forward way to model this augmented problem is to double the size of the number of possible states, by including a flag that tells you whether the walker is moving ``right'' (as in the original problem), or left (after reversing directions). It is convenient to refer to a state as $s^{y}_{x}$, where $x$ refers to location and $y$ to direction. As a concrete example, the state 1 position over from the left sink, and moving to the right can be denoted as $s^{+}_{-1}$, the state one step over from the left sink and moving to the left as $s^{-}_{-1}$. Additionally, the values of the sinks get transposed, so that $e^{+}_{+2} = -e^{-}_{-2}$ (think a touchdown scored in the + direction is +7, a touchdown in the - direction is -7) and $e^{+}_{-2} = e^{-}_{+2}$ (a safety given up in the + direction is worth -2, in the - direction +2). Combining the effects of stepping and turning over, results in a transition matrix that looks like this, \\

$
\left(
\begin{array}{cccccccccc}
 1 & (1-a) (1-p) & 0 & 0 & 0 & 0 & a (1-p) & 0 & 0 & 0 \\
 0 & 0 & (1-a) (1-p) & 0 & 0 & 0 & 0 & a (1-p) & 0 & 0 \\
 0 & (1-a) p & 0 & (1-a) (1-p) & 0 & 0 & a p & 0 & a (1-p) & 0 \\
 0 & 0 & (1-a) p & 0 & 0 & 0 & 0 & a p & 0 & 0 \\
 0 & 0 & 0 & (1-a) p & 1 & 0 & 0 & 0 & a p & 0 \\
 0 & a p & 0 & 0 & 0 & 1 & (1-a) p & 0 & 0 & 0 \\
 0 & 0 & a p & 0 & 0 & 0 & 0 & (1-a) p & 0 & 0 \\
 0 & a (1-p) & 0 & a p & 0 & 0 & (1-a) (1-p) & 0 & (1-a) p & 0 \\
 0 & 0 & a (1-p) & 0 & 0 & 0 & 0 & (1-a) (1-p) & 0 & 0 \\
 0 & 0 & 0 & a (1-p) & 0 & 0 & 0 & 0 & (1-a) (1-p) & 1 \\
\end{array}
\right)
$ \\

So, for example, the walker moves from state $s^{+}_{-1}$ (the 2nd column) to
the state $s^{+}_{-2}$ with probability $(1-a)(1-p)$,
the state $s^{+}_{0}$ with probability $(1-a) p$,
the state $s^{-}_{-2}$ with probability $a p$,
the state $s^{-}_{-2}$ with probability $a (1-p)$.
Note that when $a=0$, ``moving to the right'' (the top-left 5x5) decouples from ``moving to the left'' (the bottom-right 5x5). Specifically, \\

$
\left(
\begin{array}{cccccccccc}
 1 & 1-p & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 1-p & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
 0 & p & 0 & 1-p & 0 & 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & p & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & p & 1 & 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & 0 & 0 & 1 & p & 0 & 0 & 0 \\
 0 & 0 & 0 & 0 & 0 & 0 & 0 & p & 0 & 0 \\
 0 & 0 & 0 & 0 & 0 & 0 & 1-p & 0 & p & 0 \\
 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1-p & 0 & 0 \\
 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1-p & 1 \\
\end{array}
\right)
$ \\

With this formulation, everything I talked about in parts 1 and 2 carries over, i.e., the expectation values can be determined by iterating the transition matrix until it converges, or by separating out the sink and non-sink states and explicitly solving the system of equations for the non-sink expectation values. If I take p=1-p=0.5, and a=0, the solution is, \\

$
\left(
\begin{array}{c}
 e^{+}_{-1} \\
 e^{+}_{0} \\
 e^{+}_{+1} \\
 e^{-}_{-1} \\
 e^{-}_{0} \\
 e^{-}_{+1} \\
\end{array}
\right) =
\left(
\begin{array}{c}
 -0.5 \\
 0 \\
 0.5 \\
 -0.5 \\
 0 \\
 0.5 \\
\end{array}
\right)
$ \\

If I take p=0.6, and a=0, the solution is, \\

$
\left(
\begin{array}{c}
 e^{+}_{-1} \\
 e^{+}_{0} \\
 e^{+}_{+1} \\
 e^{-}_{-1} \\
 e^{-}_{0} \\
 e^{-}_{+1} \\
\end{array}
\right) =
\left(
\begin{array}{c}
 -0.169231 \\
 0.384615 \\
 0.753846 \\
 -0.753846 \\
 -0.384615 \\
 0.169231 \\
\end{array}
\right)
$ \\

These make sense based on the results from parts 1 and 2. If I take p=0.5 and vary the value of $a$, then the result is as shown in Fig. \ref{fignm_05}. In other words, it makes no difference if I reverse direction or not. This makes sense because either way I am moving left or right with equal probability, and moreover, I have defined the points for landing at location +2 (a ``touchdown'') to be equal to the points for landing in location -2 (a ``safety''). \\

If I set p=0.6 and vary a, the results are as shown in Fig. \ref{fignm_06}. And, if I set p=0.9 and vary a, the results are as in Fig. \ref{fignm_09}.

As a test case to investigate in more detail, let me choose $p=0.6, a=0.1$. In that case the solution is,

$
\left(
\begin{array}{c}
 e^{+}_{-1} \\
 e^{+}_{0} \\
 e^{+}_{+1} \\
 e^{-}_{-1} \\
 e^{-}_{0} \\
 e^{-}_{+1} \\
\end{array}
\right) =
\left(
\begin{array}{c}
 -0.316552 \\
 0.206897 \\
 0.642069 \\
 -0.642069 \\
 -0.206897 \\
 0.316552 \\
\end{array}
\right)
$ \\

An important point to note is that the solution has a lot of symmetry. In other words, if you flip the direction you're moving and also flip the starting point, then the expectation value is equal in magnitude and opposite in sign. So, for example, starting from $s_{-1}$ and moving right is the negative of starting from $s_{+1}$ and moving left. I can write this more concisely as, $e^{-}_{i} = -e^{+}_{2m-i}$, where $m$ stands for ``middle''. In this case $m=0$, and \\

$
\begin{array}{c}
e^{-}_{-1} = - e^{+}_{+1} \\
e^{-}_{0} = - e^{+}_{0} \\
e^{-}_{+1} = - e^{+}_{-1} \\
\end{array}
$ \\

Moreover, the transition probabilities are symmetric if we flip directions and flip position around the middle, that is, $T^{+,-}_{i,j} = T^{+,-}_{2 m-i,2 m - j }$. The main point of this section is that we can use the symmetry of the expectation values and transition probabilities to avoid the doubling of the state space. To repeat the general equation for expectation value in terms of transition probabilities (Einstein notation), \\

$e^{+}_{j} = e^{+}_{i} T^{+}_{i,j} \\
= e^{+}_{i} T^{+,+}_{i,j}  + e^{-}_{i} T^{-,+}_{i,j} \\
$ \\

So far this is just an identity, where I have split up $T$ into the parts where no turnover occurs (the $T^{+}_{i} terms) and the parts where a turn over did occur (the $T^{-}_{i} terms). With the assumption that a ``turnover'' occurs with probability a, and the relations described above, I can write, \\

$e^{+}_{j} = (1-a) e^{+}_{i} T^{+,+}_{i,j} - a e^{+}_{2 m-i} T^{+,+}_{2 m -1, 2 m - j}$ \\

So if I generate a matrix, $U$, with elements $U_{i,j} = (1-a) T_{i,j} - a T_{i, 2m - j}$, then I can write the vector equation $\vec{e} = \vec{e} \vec{U}$. Everything discussed in part 2 regarding solving this then carries over. As a concrete example, here is what the $5 \times 5$ matrix $U$ looks like for this problem, \\

$
\left(
\begin{array}{ccccc}
 1-a & (1-a) (1-p) & 0 & -a (1-p) & -a \\
 0 & 0 & (1-a) (1-p)-a (1-p) & 0 & 0 \\
 0 & (1-a) p-a (1-p) & 0 & (1-a) (1-p)-a p & 0 \\
 0 & 0 & (1-a) p-a p & 0 & 0 \\
 -a & -a p & 0 & (1-a) p & 1-a \\
\end{array}
\right)
$ \\

Finally, since this equation is identical to the one in part 2, with $U$ replacing $T$, the transition matrix without considering turnovers, it follows that a 2nd way to determine the expectation values is to iterate $U$ until it converges, and read off the columns. In the football application, where the transition matrix has (depending on the parameterization) about 8000 elements, iterating may be a better choice, computationally. Another important thing to note here is that I've looked at a restricted case where a turnover allows occured at the same location from which the walker started. So the relation in this case is
$e^{-}_{i} T^{-,+}_{i,j} = e^{+}_{i} T^{+,+}_{i,2 m -j}$,
more generally it is,
$e^{-}_{i} T^{-,+}_{i,j} = e^{+}_{i} T^{+,+}_{i,j'}$, where $j'$ is the state that the turnover brounght you to.
As a concrete example, consider having 1st and 10 from the 20. Say there is a probability $\eta$ to throw an interception, with the ball ending up 20 yards down field, at the 40. Then the part of the transition matrix encoding that will be
$ - e_{1-10-60} T^{-,+}_{1-10-60,1-10-20}$.

\section{4 of n: the yards gained distributions (transition matrix)}

In football, a basic state consists of a set of down-distance-yardline values. One could include score differential, or time I suppose, but I'm not considering those here. The transition matrix can be built by using the yards-gained distribution, along with the probabilities to run a play as opposed to punting or attempting a field goal. So the main questions I want to address here are,

\begin{itemize}

\item what are the probabilities for passing versus running versus kicking?

\item given that a play is run, what is the yards gained distribution?

\end{itemize}

I am ignoring some context-specific distinctions such as time on the clock, score-differential and weather. This is very much in the same vein as something like RE24 (from the more developed field of baseball analytics) in that it accounts for game state, but is othwerwise context neutral.

With that being said, it is not immediately obvious what the dependence of the yards-gained distribution on down, distance, and yardline should be. So my starting point was to grab some data and start slicing and dicing and making some graphs. To clarify, yardline is encoded as ``yards-from-own-goal'', abbreviated yfog, where 1 means you are backed up against your end zone, and 99 means you are 1 yard away from scoring a touchdown.

\begin{itemize}

\item In between, say yfog=20 and yfog=75, mean yards gained doesn't depend strongly on field position (Fig. \ref{fignm1}).

\item For plays originating between yfog=20 and yfog=75, mean yards gained doesn't depend strongly on yards-to-go (Fig. \ref{fignm2}).

\item The fraction of plays that are passes depends strongly on down and distance, and less strongly on field position (Figs. \ref{fignm3} \& \ref{fignm5}).

\item On fourth down, the probabilities to punt versus try a field goal versus go for it vary rapidly as a function of yards-to-go and yards-from-own-goal This is particularly true for yfog $\sim 50$ to yfog $\sim 80$ (Figs. \ref{figep1}, \ref{figep2}, and \ref{figep3}).

\end{itemize}

So now the question is, how can we model the yards-gained distribution? Since mean yards gained doesn't depend too strongly on down, distance, and field position, it is instructive to pool a bunch of states, and look in more detail at the distribution to get a feel for what it looks like. In Fig. \ref{fignm6}, I show distributions for passes (left) and rushes (right), for all first-down plays originating between yfog=20 and yfog=75.

After playing around with some functions, the best general agreement I could find came from the following,  \\

$ p(y) = A ~\frac{e^{(y-y_0)/\sigma_1}} {1+e^{(y-y_0) (\sigma_1+\sigma_2)/(\sigma_1 \sigma_2)})} + G ~e^{- (y-g_0)^2/ (2 \sigma_g^2)}$. \\

I refer to this as a ``Bazin plus Gauss'' function; Bazin because I first encountered the ``ratio of exponentials'' in a paper by Bazin, et al, that used it to model supernova light curves ({\tt http://arxiv.org/abs/1109.0948}), and Gauss for obvious reasons. The first (Bazin) term basically stitches together two exponentials at the location $y_0$. For $y \ll y_0$, it looks like a rising exponential with a scale factor $\sigma_1$, and for $y \gg y_0$, like a declining exponential with a scale factor $\sigma_2$. The Gaussian part describes being sacked, and in the football application, $G$ is identically 0 for rushes.

Using this functional form, I use the function minimization package {\tt pyminuit} to determine the maximum likelihood values for the parameters, $y_0, \sigma_1, \sigma_2$ for rushes, and additionally $G/A, g_0,$ and $\sigma_g$ for passes. For rushes, typical values are $y_0 \sim 1, \sigma_1 \sim 1.5, \sigma_2 \sim 3.5$. For passes, typical values are $y_0 \sim 4.5, \sigma_1 \sim 1.8, \sigma_2 \sim 8.0, G/A \sim 0.12, g_0 \sim -6.5, $ and $\sigma_g \sim 3.0$. Figs. \ref{figye1} \& \ref{figym1} compare the model to the empirical distribution for 1st and 10 plays from the 20.

In the next section I will describe how my model is implemented in code.

\section{5 of n: nflMarkov, the Python code}

In part 4, I described modeling the yards-gained distribution, including the probabilities to kick versus run a play. Here I will describe how these are controlled in the Markov chain computer program.

The program reads a parameter file which has a generic form of \\

{\tt parameter-name down ytg-min ytg-max yfog param-value} \\

For a given down and distance range (yards-to-go, or ytg), the parameter values are stored as a function of yards-from-own-goal. Then, during run time, the parameter value is set using linear interpolation over yfog. This gives a very flexible input scheme. As a concrete example, here are example parameter values for ``going for it on 4th'',

\begin{verbatim}
4thGoForItProb 4 1 1 0 0
4thGoForItProb 4 1 1 20 0
4thGoForItProb 4 1 1 50 0.24
4thGoForItProb 4 1 1 60 0.78
4thGoForItProb 4 1 1 70 0.75
4thGoForItProb 4 1 1 85 0.42
4thGoForItProb 4 1 1 100 0.50

4thGoForItProb 4 2 4 0 0
4thGoForItProb 4 2 4 20 0
4thGoForItProb 4 2 4 40 0.07
4thGoForItProb 4 2 4 50 0.10
4thGoForItProb 4 2 4 60 0.45
4thGoForItProb 4 2 4 80 0.14
4thGoForItProb 4 2 4 100 0.14
\end{verbatim}

This says that, when its 4th and between 1 and 1 to go (in other words precisely 1), if yfog is between 0 and 20, then interpolate between 0 and 0. In other words the probability to go for it is 0. If yfog is between 20 and 50, interpolate between 0 and 0.24. So for example, half way in between, when yfog=35, the probability would be 0.12. On the other hand, if yards-to-go is between 2 and 4 (inclusive), then the probability at yfog=35 will be the slope, $(0.07-0.00)/(40-20)$, times $35-20 = 15$, plus the initial value at yfog = 20, which is 0. That is, $0 + 0.07/20*15 = 0.0525$, which is a long-winded way of saying that the value is determined through linear interpolation between the points (20, 0) and (40, 0.07).
. If down is set to 0, it means the parameters hold for downs 1-4. The parameters that are controllable are,

\begin{verbatim}
intProb: interception probability
fumProb: fumble probability
incompleteProb: incompletion probability
yardsDistParsRush: parameters of the Bazin-Gauss function for rushes (G identically 0)
yardsDistParsPass: parameters of the Bazin-Gauss function for passes
4thGoForItProb: probability to go for it on 4th
4thFgProb: probability to go for a field goal on 4th
FgMakeProb: probability to make a field goal
passProb: given that you run a play, the probability to pass
\end{verbatim}

The code has two modes, empirical and user-defined. If the empirical model is chosen, the parameters controlling the yards-gained distributions are ignored and the distributions are instead read in empirically.
The final input is a model name, which determines the output data file name, where the transition matrix and the expectation value vector (among other things) are stored for later reference or ease of comparison to a different model.

%%%%%%%%%%%%%%%%%%%%%%%%
% figures
\begin{figure} [!th]
\begin{center}
\includegraphics[width=4.50in]{markovChains_05.eps}
\end{center}
\caption{
points
 }
\label{fignm_05}
\end{figure}

\begin{figure} [!ht]
\begin{center}
\includegraphics[width=4.50in]{markovChains_06.eps}
\end{center}
\caption{
points
 }
\label{fignm_06}
\end{figure}

\begin{figure} [!ht]
\begin{center}
\includegraphics[width=4.50in]{markovChains_09.eps}
\end{center}
\caption{
points
 }
\label{fignm_09}
\end{figure}

\begin{figure} [!ht]
\begin{center}
\includegraphics[width=4.50in]{nflMarkov1.eps}
\end{center}
\caption{
mean yards per play as a function of field position. black is 1st and 10 or more, red is 3 and short, specifically 3 or less. dashed lines show +- 1 standard deviation. what I take away from this is that the standard deviation is so large that you don’t have to worry about variation that depends on field position as long as you’re at 75 yards or less.
 }
\label{fignm1}
\end{figure}

\begin{figure} [!ht]
\begin{center}
\includegraphics[width=4.50in]{nflMarkov2.eps}
\end{center}
\caption{
(green, blue, red) = (2nd, 3rd, 4th) downs. The only 1st down values that matter are 1st and 5, 1st and 10 and first and 15, which are
1 and 5 : 5.27 +- 8.63
1 and 10; : 5.71 +- 9.00
1 and 15; : 6.15 +- 8.94
 }
\label{fignm2}
\end{figure}

\begin{figure} [!ht]
\begin{center}
\includegraphics[width=4.50in]{nflMarkov3.eps}
\end{center}
\caption{
here is fraction of plays that are passes by yard from own goal. black, green, blue are 1st, 2nd 3rd downs. the point at yfog=29 is interesting; if you are 3rd down at the 29, you probably got there starting from 1st and 10 at the 20 (a touchback) so you are 3rd and 1 and you rush more often. its so logical!
 }
\label{fignm3}
\end{figure}

\begin{figure} [!ht]
\begin{center}
\includegraphics[width=4.50in]{nflMarkov5.eps}
\end{center}
\caption{
here is fraction of plays that are passes (given that its either a pass or a rush and originates between yfog=20 and yfog=75), based on distance to a first down. green, blue, red are 2nd, 3rd, 4th downs. 1st and 10 is 49.83\% pass (with N=88547).
 }
\label{fignm5}
\end{figure}

\begin{figure} [!ht]
\begin{center}
\includegraphics[width=4.50in]{empProbs1.eps}
\end{center}
\caption{
Probabilities to attempt (pass, rush, field goal, punt) for 4th and $\ge 4$
 }
\label{figep1}
\end{figure}

\begin{figure} [!ht]
\begin{center}
\includegraphics[width=4.50in]{empProbs2.eps}
\end{center}
\caption{
Probabilities to attempt (pass, rush, field goal, punt) for 4th and $< 4$
 }
\label{figep2}
\end{figure}

\begin{figure} [!ht]
\begin{center}
\includegraphics[width=4.50in]{empProbs3.eps}
\end{center}
\caption{
Probabilities to attempt (pass, rush, field goal, punt) for 4th and 1
 }
\label{figep3}
\end{figure}

\begin{figure} [!ht]
\begin{center}
\includegraphics[width=4.50in]{nflMarkov6.eps}
\end{center}
\caption{
distributions of yards gained for all 1st downs that originate between 20 and 75, left panel is passes (35\% or so gain 0 yards), right is rushes.
 }
\label{fignm6}
\end{figure}

\begin{figure} [!ht]
\begin{center}
\includegraphics[width=4.50in]{empYardsDist.eps}
\end{center}
\caption{
distributions of yards gained for all 1st downs that originate between 20 and 75, left panel is passes (35\% or so gain 0 yards), right is rushes.
 }
\label{figye1}
\end{figure}

\begin{figure} [!ht]
\begin{center}
\includegraphics[width=4.50in]{modelYardsDist.eps}
\end{center}
\caption{
distributions of yards gained for all 1st downs that originate between 20 and 75, left panel is passes (35\% or so gain 0 yards), right is rushes.
 }
\label{figym1}
\end{figure}

\end{document}


\documentclass{article}

\usepackage[a4paper, total={6in, 8in}]{geometry}
% include some useful things
\usepackage{verbatim}  % for printing unformatted text
\usepackage{float}         % for controlling the location of figure and graphics on the page
\usepackage{blindtext}
\usepackage{graphicx}\usepackage{amsmath}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{caption}
\usepackage{subcaption}
%  Begin writing content below this line

\begin{document}

%  Print your name and the assignment number
\begin{center}{\huge  Deepayan Bhadra - hmwk4 Solutions}\end{center}

\section*{Q1(a): Optimality condition for  L2 denoising}
% The verbatim environment is good for reproducing text without having latex try to format it for you.
min $\frac{\mu}{2}||\nabla{x}||^2 + \frac{1}{2}||x-b||^2$
\\\\
Taking the gradient and setting it to zero gives the necessary condition:
\\\\
$\mu \nabla^T\nabla(x) + x - b = 0$
\\\\
Writing this in the form of a large linear system Ax = b gives \\\\
$ (\mu \nabla^T\nabla + I)(x) = b $

\section*{Q1(b): Creating Function Handle for linear operator A}

\begin{verbatim}

img= mpimg.imread('lena512.bmp')
img = img.astype(float)
b = img/max(img.flatten()) # Scaling to [0,1]
mu = 2
x0 = np.random.randn(*b.shape)
f = lambda x:mu*div2d(grad2d(x))+x-b
output = f(x0) # Evaluating A at x0

\end{verbatim}

\section*{Q1(c): Richardson Iteration}

\begin{verbatim}

def richardson(A,b,x,t):
    resids = 1
    all_res = []
    while resids > 10e-6:
        x = x+t*(b-A(x))
        resids = np.linalg.norm(b-A(x),'fro')
        all_res = np.append(all_res,resids)
    return all_res,x

t = 0.05
all_res,x = richardson(f,b,x0,t)
plt.xlabel('# of iterations')
plt.ylabel('Residual norm')
plt.plot(all_res)
print("# of iterations to convergence is",all_res.size)
plt.imshow(b) # Noisy image
plt.imshow(x.astype(float)) # De-noised image

\end{verbatim}

\textbf{\Large{No. of iterations to convergence is 334}}

\begin{figure}[h!]
\centering
\begin{subfigure}[h!]{.5\textwidth}
\centering
\includegraphics [scale = 0.5] {./pictures/Noisy1c.jpg}
\caption{Richardson: Noisy Image}
\end{subfigure}
\begin{subfigure}[h!]{.5\textwidth}
\centering
\includegraphics [scale = 0.5] {./pictures/Denoised1c.jpg}
\caption{Richardson: De-noised Image}
\end{subfigure}
\end{figure}
\begin{figure}[h!]
\centering
\includegraphics [scale = 0.85] {./pictures/1c.jpg}
\caption{Richardson: Residual norm v/s No. of Iterations}
\end{figure}

\textbf{\large{(Plots at the end)}}

\section*{Q1(d): Conjugate Gradient}

\begin{verbatim}

def conjgrad(A,b,x):
    tol = 10e-6;
    xk = x; rk = b-A(xk); pk = rk # Initial values
    res = [];
    while np.linalg.norm(rk)>tol:
        grad = A(pk);
        alpk = np.dot(rk.flatten(),rk.flatten())/(np.dot(pk.flatten(),
                                                     grad.flatten()))
        xk = xk+alpk*pk;
        rkk = rk-alpk*grad;
        betak = np.dot(rkk.flatten(),rkk.flatten())/(np.dot(rk.flatten(),
                                                           rk.flatten()))
        pk = rkk+betak*pk;
        rk = rkk;
        res = np.append(res,np.linalg.norm(rk,'fro'))
        # Storing all the residuals

    return x,resids

[x,res] = conjgrad(f,b,x0)
plt.xlabel('# of iterations')
plt.ylabel('Residual norm')
plt.plot(all_res)
print("No of iterations to convergence is",all_res.size)
plt.imshow(b) # Noisy image
plt.imshow(x.astype(float)) # De-noised image

\end{verbatim}

\textbf{\Large{No. of iterations to convergence is 34}}\\
\textbf{\large{(Plots at the end)}}

\begin{figure}[h!]
\centering
\includegraphics [scale = 0.85] {./pictures/1d.jpg}
\caption{Conjugate Gradient: Residual norm v/s No. of Iterations}
\end{figure}
\begin{figure}[h!]
\centering
\begin{minipage}{.5\textwidth}
\centering
\includegraphics [scale = 0.5] {./pictures/Noisy1c.jpg}
\caption{Conjugate Gradient: Noisy Image}
\end{minipage}
\begin{minipage}{.5\textwidth}
\centering
\includegraphics [scale = 0.5] {./pictures/Denoised1c.jpg}
\caption{Conjugate Gradient: De-noised Image}
\end{minipage}
\end{figure}

\section*{Q1(e): With mu = 10}

The iteration count increases almost two-fold. This is because, now significantly less weight (1/10) is allotted to the difference factor (x-b) (Images and convergence plots at the end)

\section*{Q1(f): Compute exact solutions using FFT}

\begin{verbatim}

def l2denoise(b,mu):
    kernel = np.zeros((b.shape[0],b.shape[1]))
    kernel[0,0] = 1
    kernel[0,1] = -1
    Dx = np.fft.fft2(kernel)
    kernel = np.zeros((b.shape[0],b.shape[1]))
    kernel[0,0] = 1
    kernel[1,0] = -1
    Dy = np.fft.fft2(kernel)
    dd = np.divide(1,(mu*(np.conj(Dx)*Dx+np.conj(Dy)*Dy)+1))
    x = np.real(np.fft.ifft2(dd*np.fft.fft2(b)))
    return x

print('The norm of the gradient of the objective function is')
np.linalg.norm(f(l2denoise(b,mu)))

\end{verbatim}

\textbf{\Large{The norm of the gradient of the objective function is
ans = 7.0224e-13}}

\section*{Q2(a): Building Two-Moons dataset}

\begin{verbatim}
def make_moons(n):
    """Create a 'two moons' dataset with n feature vectors,
        and 2 features per vector."""

    assert n%2==0, 'n must be even'
    # create upper moon
    theta = np.linspace(-pi / 2, pi / 2, n//2)
    # create lower moon
    x = np.r_[np.sin(theta) - pi / 4, np.sin(theta)]
    y = np.r_[np.cos(theta), -np.cos(theta) + .5]
    data = np.c_[x, y]
    # Add some noise
    data = data + 0.03 * np.random.standard_normal(data.shape)

    # create labels
    labels = np.r_[np.ones((n//2, 1)), -np.ones((n//2, 1))]
    labels = labels.ravel().astype(np.int32)

    return data,labels

data,l = make_moons(100)
plt.scatter(data[l>0,0],data[l>0,1],c='r')
plt.scatter(data[l<0,0],data[l<0,1],c='b')
plt.show()
S = np.zeros((100,100))
sig = 0.09
B = pdist(data)
C = squareform(B)
S = np.exp(-C/sig)

\end{verbatim}

\begin{figure}[h!]
\centering
\includegraphics [scale = 0.75] {./pictures/2aTwoMoons.jpg}
\caption{Small Dataset: Two Moons}
\end{figure}
\section*{Q2(b): Compute Normalized Similarity Matrix}

\begin{verbatim}
S_sum = np.sum(S,axis=1,keepdims = True)
D = np.diagflat(S_sum)
temp = np.power(S_sum,-0.5)
S_hat = np.diagflat(temp).dot(S).dot(np.diagflat(temp))
\end{verbatim}

\section*{Q2(c): Spectral Embedding}

\begin{verbatim}
E,V = np.linalg.eig(S_hat)
idx = E.argsort()[::-1]
E = E[idx]
V = V[:,idx]

U2 = np.stack([V[:,1],V[:,2]],axis=1)
plt.scatter(U2[l==1,0],U2[l==1,1],c='r')
plt.scatter(U2[l==-1,0],U2[l==-1,1],c='b')
plt.show()
\end{verbatim}
\textbf{\large{(Combined plots at the end)}}
\section*{Q2(d): k-means clustering}

\begin{verbatim}
kmeans = KMeans(n_clusters = 2)
y_kmeans = kmeans.fit_predict(U2)
plt.scatter(U2[y_kmeans == 0, 0], U2[y_kmeans == 0, 1], s = 100, c = 'red', label = 'Cluster 1')
plt.scatter(U2[y_kmeans == 1, 0], U2[y_kmeans == 1, 1], s = 100, c = 'blue', label = 'Cluster 2')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1],
				s = 300, c = 'yellow', label = 'Centroids')
\end{verbatim}

\begin{figure}[h!]
\centering
\begin{subfigure}{.5\textwidth}
\centering
\includegraphics [scale = 0.75] {./pictures/2cSE.jpg}
\caption{Spectral Embedding: Scattered columns of U2}
\end{subfigure}
\begin{subfigure}[h!]{.5\textwidth}
\centering
\includegraphics [scale = 0.75] {./pictures/2dKM.jpg}
\caption{k-means clustering}
\end{subfigure}
\end{figure}

\section*{Q3(a): Spectral Grouping Using Nystrom Method}

\begin{verbatim}
data,l = make_moons(100000)
sig = 0.09
idx = np.random.choice(100000,size = 200, replace=False) # Random Sampling of 200 columns
idx_c = np.setdiff1d(np.arange(100000),idx)
temp1 = cdist(data,data[idx,:]) # Pairwise distance between two sets of observations
C = np.exp(-temp1/sig)
W = C[idx,:]
\end{verbatim}\\

\section*{Q3(b): Forming Normalized C, W, M matrices}

\begin{verbatim}
Z = C[200:,:]
W_m = np.sum(W,axis=1,keepdims = True)+ np.sum(np.transpose(Z),
                                                 axis=1,keepdims = True)
M_e = np.sum(Z,axis=1,keepdims = True)+ np.transpose(np.sum(Z,
                                                 axis=0,keepdims = True)
                                                .dot(np.linalg.inv(W))
                                                .dot(np.transpose(Z)))
diagD = np.concatenate([W_m,M_e],axis=0)
temp = np.multiply(np.repeat(diagD,200,axis=1),C)
C_n = np.dot(temp,np.diagflat(diagD[idx]))
W_n = C_n[idx,:]
M_n = C_n[idx_c,:]
\end{verbatim}

\section*{Q3(c): Approximate eigenvectors}

\begin{verbatim}
W_hat = W_n + sp.linalg.sqrtm(np.linalg.inv(W_n)).dot(np.transpose(M_n)).dot(M_n).
dot(sp.linalg.sqrtm(np.linalg.inv(W_n)))
# Orthogonalization matrix

D_w,U_w = np.linalg.eig(W_hat)
D_w,U_w = np.real(D_w), np.real(U_w)

# Eigen-decomposition W_hat = V*D*V'

U = np.matmul(C_n,np.matmul(sp.linalg.sqrtm(np.linalg.inv(W_n)),U_w))*
np.power(np.expand_dims(D_w,axis=-1),-0.5).T

idx = np.argsort(D_w)[::-1]
U_w = U_w[idx]
U = U[:,idx]

U = U/np.expand_dims(U[:,0],axis=-1)
U2 = np.expand_dims(U[:,1],axis=-1)
plt.scatter(U2[l==1],U2[l==1],c='r')
plt.scatter(U2[l==-1],U2[l==-1],c='b')
plt.show()
# Approximate eigen-vectors
\end{verbatim}

\textbf{\large{(Combined plots at the end)}}

\section*{Q3(d): k-means}

\begin{verbatim}
kmeans = KMeans(n_clusters = 2)
U2 = U[:,-3:-1]
y_kmeans = kmeans.fit_predict(U2)
plt.scatter(U2[y_kmeans == 0, 0], U2[y_kmeans == 0, 1], s = 100, c = 'red', label = 'Cluster 1')
plt.scatter(U2[y_kmeans == 1, 0], U2[y_kmeans == 1, 1], s = 100, c = 'blue', label = 'Cluster 2')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s = 300, c = 'yellow',
label = 'Centroids')
\end{verbatim}

\begin{figure}[h!]
\centering
\includegraphics [scale = 0.75] {./pictures/3aTwoMoons.jpg}
\caption{Large Dataset: Two Moons}
\end{figure}

\begin{figure}[h!]
\centering
\begin{subfigure}{.5\textwidth}
\centering
\includegraphics [scale = 0.75] {./pictures/3cSE.jpg}
\caption{Approximate eigenvectors: Scattered columns of U2}
\end{subfigure}
\begin{subfigure}[h!]{.5\textwidth}
\centering
\includegraphics [scale = 0.75] {./pictures/3dKM.jpg}
\caption{k-means clustering}
\end{subfigure}
\end{figure}
\end{document}