\documentclass[12pt, letterpaper]{article}

% Document metadata
\title{Magic: The Gathering Tournament Deck Evaluation}
\date{April 30, 2019}
\author{John Meyer, Scott Harris, Jacob Freedman, Bryan Hayes}

% Required packages
\usepackage[autostyle, english=american]{csquotes}
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{setspace}
\usepackage{commath}
\usepackage{amsmath}
\usepackage[section]{minted}

\usepackage[colorlinks=true,linkcolor=blue,urlcolor=blue,bookmarksopen=true]{hyperref}

% Define subsections down to \subsubsubsection
\usepackage{titlesec}
\setcounter{secnumdepth}{4}

\titleclass{\subsubsubsection}{straight}[\subsection]
\newcounter{subsubsubsection}[subsubsection]

\renewcommand\thesubsubsubsection{\thesubsubsection.\arabic{subsubsubsection}}
\renewcommand\theparagraph{\thesubsubsubsection.\arabic{paragraph}}

\titleformat{\subsubsubsection}
{\normalfont\normalsize\bfseries}
{\thesubsubsubsection}
{1em}
{}

\titlespacing*{\subsubsubsection}
{0pt}
{3.25ex plus 1ex minus .2ex}
{1.5ex plus .2ex}

% Packages configuration
\setminted{linenos, autogobble, breakautoindent, breaklines, breakindentnchars=4,
    fontsize=\footnotesize}
\setminted[python]{python3}

\doublespacing

\begin{document}

\maketitle

\section{Introduction}

Magic the Gathering is a popular card game that has withstood the test of time,
boasting of twenty million players as of 2015.
Its sustained interest over the years has lead to organized tournaments
and has even attracted the interest of academic research and scholarly articles.
The game hinges on both strategy and uncertainty.
In a tournament style known as \enquote{limited, sealed-deck,}
players must pre-select 40 or more cards from a set of 90 cards given to them.
Players must consider the strengths and weaknesses of each card,
the synergies that may exist between them,
and forecast their utility during gameplay,
given that their opponent is doing the same.
We propose to help these participants by authoring an objective function for this
domain to score potential card combinations according to its strategic utility
as illuminated by common play-styles and professional strategies.
We also plan to implement an optimization algorithm to maximize this function
so that players can play with the most effective deck.
This algorithm could help professionals as well as novices to have better odds of success
and a more enjoyable experience.

The implementation of our objective function is located on
\href{https://github.com/0x326/miami-university-cse-464-group-project}{GitHub}

\section{Definition}

Magic the Gathering consists of a finite set $ C $ of distinct cards.
$ C $ can be partitioned into sets $ S_1, S_2, ..., S_n $, each called a Magic the Gathering \enquote{set}.
$ C $ can also be partitioned into two sets $ L $ and $ L_0 $, where $ L $ are \enquote{basic lands} and $ L_0 $ are not.

Recall: A \enquote{multiset} $ M $ is a 2-tuple $ (A, m) $
where $ A $ is a set and $ m: A \rightarrow \{x \in \mathbb{N} \mid x \ge 1 \} $.
$ A $ is called the \enquote{support} of $ M $ and
a set $ U $ is a \enquote{universe} of $ M $ if and only if $ A \subseteq U $.
The \enquote{sum} of multisets $ M_1 $ and $ M_2 $ is defined as
$ (A_1 \cup A_2, \{ x \in (A_1 \cup A_2) \mid (x, m_1(x) + m_2(x)) \} $.

Let $ P $ denote probability.

In a limited, sealed-deck tournament style, a partition $ S $ is chosen and used for play.
Each participant is provided a multiset $ B $ of cards whose universe is $ S $
as well as a multiset $ B_L $ whose universe is $ L $.
$ B $ is constrained to contain $ 90 $ cards
whereas $ B_L $ contains an infinite amount of cards.

Given participants must select a multiset $ D, \ |D| \ge 40 $ from the sum of $ B $ and $ B_L $,
we want to choose $ D $ such that we maximize $ P(\text{game success} \mid D) $.

\section{Literature Review}

\subsection{\enquote{Ensemble Determinization in Monte Carlo Tree Search for the Imperfect Information Card Game Magic: The Gathering.}}
\label{sec:3.1}

This article discusses how the Monte Carlo Tree Search (MCTS) algorithm
has been applied to create AI's for games in the
past and how it can be applied with the game Magic: the Gathering.
Due to Magic's core element of having incomplete information while playing the game
due to the fact that both players have hidden hands of cards that were drawn from
shuffled decks makes the Monte Carlo Tree Search approach quite effective.
In addition,
since each player gets to create their own deck to play in the manner of their choice,
this adds further hidden information as well as an interesting challenge to the game.
Other key elements of Magic: the Gathering such as the ability of a player to
interrupt another player during their turn,
interact with other player's resources, and predicting how an opponent
react to different plays makes this game an interesting field of study.
The MCTS algorithm provides the advantage over
other algorithms since it is not required for a terminal game state to be reached for a result to be calculated
but instead can be stopped at any point to evaluate the current state.
This paper details all of the core rules of Magic such that they compare the advantages of the
MCTS algorithm over a strictly rule based approach.
By pruning different kinds of moves to be added to the tree,
the ability to formulate the best move is much more efficient.
By using multiple trees,
the authors compensate for incomplete information allowing for the ideal course of action based on the known information.

\subsection{\enquote{Rarity and Power: Balance in Collectible Object Games.}}
\label{sec:3.2}

Mr. Ham details the issues that come along with the collectable card game format.
The main issue involves the game both being fun as well as balanced.
In the game Magic: the Gathering,
there were 9 cards within the first sets that were significantly more powerful than their counterparts.
The game designers did not intend for these cards to so
significantly impact the game that each player would own and use two or so copies of these cards in roughly every deck.
By these cards being so overpowered, the balancing of the game was completely out of whack.
Other games such as Sanctum have tried their hardest to not create cards
that were so good that they could individually break the gameplay of
Sanctum, especially if the reason they were so good is that they were hard to obtain.
These applications also apply to trading baseball card games.
In the game you can play with baseball cards, all of the baseball players all can be reduced to a statistical value.
This allows for many different strategies such that a player can accomplish the same
goals with many different strategies/collections of cards.
With this versatility Suitcase players are not put at as much
of an advantage in the baseball card game as they are in games such as Magic: the Gathering.

\subsection{\enquote{BOA: Bayesian Optimization Algorithm.}}
\label{sec:3.3}

This paper describes how a bayesian optimization algorithm can be used to increase the efficiency of genetic algorithms.
It describes how genetic algorithms are optimization programs
based on artificial selection and genetic recombination operators
and how the two most important factors to a genetic algorithms success, proper growth and mixing, are often not achieved.
The paper describes how a bayesian algorithm functions.
It selects the best options from a population using any searching method.
Than the new options are added to the old population, replacing some of the old options.
This generates a bayesian network, where each node is one variable.
This method could be helpful for creating our optimization algorithm because it is fast.
As shown in the paper, the algorithm runs in close to linear time.
This method could also be useful because it selects the best from a network,
which would be an optimal way of selecting the best cards for the deck.

\subsection{\enquote{Surprising strategies obtained by stochastic optimization in partially observable games.}}
\label{sec:3.4}

This paper takes a different approach to optimization.
The authors use a evolutionary algorithm and a stochastic optimization algorithm
to find optimal outputs for partially observable games.
They use 5 different algorithms, one naïve evolutionary algorithm, two coevolutionary algorithms,
an iterative evolutionary algorithm, and a seed method.
These algorithms are tested by playing multiple different games, batawaf, war, cheat, battleship and guess who.
The results found that these algorithms found strategies for games that seem entirely luck based.
This type of algorithm could be useful for finding the optimal deck against different styles of play.

\subsection{\enquote{Active Set Methods with Reoptimization for Convex Quadratic Integer Programming}}
\label{sec:3.5}

\enquote{Active Set Methods with Reoptimization for Convex Quadratic Integer Programming}
proposes a new algorithm to solve \enquote{unconstrained convex quadratic integer}
problems with linear constraints.
The algorithm they propose is a generalized branch-and-bound approach,
which is the most effective method to solve problems of this type (quadratic integer programming problems),
and it has wide application as many real-world problems can be represented in this fashion.
It consists of a preprocessing phase and an iterative main phase,
which uses an optimization known as a \enquote{warmstart.}
For this optimization,
each iteration of the main phase uses knowledge of the previous iteration to inform its next guess.
If the number of constraints are held constant,
the algorithm runs in linear time with respect to the problem size.
It was tested with 200 random problem instances and it was found to outperform IBM CPLEX by a moderate measure.

\subsection{\enquote{Approximating the k-Set Packing Problem by Local Improvements}}
\label{sec:3.6}

\enquote{Approximating the k-Set Packing Problem by Local Improvements}
proposes a more efficient variant of many algorithms already created to solve the k-set packing problem.
The k-set packing problem, given a universe $ U $ and a collection $ S \subset U $ with $ |S| \le k $,
seeks to find the maximum number of sets $ \subset S $ which are mutually disjoint.
Prior work has improved solving this problem from $ \frac{k}{2} $ to $ \frac{k + 2}{3} $
then $ \frac{k + 1}{3} + \epsilon $ and $ \frac{1}{\epsilon} $.
The changes they propose further reduce it to $ \frac{1}{\epsilon^2} $.
They suggest that their contribution is possibly the best improvement that can be made for the problem,
since it matches a theoretical lower bound.
They use a local search strategy,
which is the most widely used approach for this problem.
Their algorithm, which they call \enquote{Algorithm CITC,}
uses a color-coding technique to make a logarithmic-time improvement with a polynomial-time algorithm.
They construct a \enquote{bipartite conflict graph}
and search it for what they call \enquote{colorful tail changes} for every $ 3 \ge $ degree vertex.
For each one they find,
they build a multigraph and continue computation.
To conclude, they use various strategies to squeeze out better performance from an already efficient solution
and do so perhaps for the last time.

\subsection{\enquote{Magic: The Gathering, A Literary Text.}}
\label{sec:3.7}

This reading starts off by talking about Magic: The Gathering and a bit of its history.
It compares the card game to the popular role-playing game
Dungeons \& Dragons in terms of its lore and overall aesthetic,
then goes on to talk about its impact on pop culture.
After this, the reading explains some of the rules of Magic,
and some of the different ways the game is played such as arranging the deck based on color of the card or
limiting the total size of the deck.
The main argument of this article is then presented;
that Magic is a form of literary work.
The author is arguing that,
while each card on its own presents little context,
the cards together paint a picture of a world filled with rich lore.
The author backs this up using the fact that works of interactive fiction,
such as graphic novels or motion comics,
are themselves considered works of literature by today's standards.
It is argued that Magic can be viewed as a form of interactive fiction.
In addition to this, the point is brought up that the Magic: The Gathering website
has a large collection of written pieces about the characters and lore of the Magic mythos.
All of these arguments, and the fact that Magic is a wildly popular and acclaimed game,
lead the author to claim that the card game is a \enquote{dynamic literary text.}

\subsection{\enquote{The Complexity of Deciding Legality of a Single Step of Magic: The Gathering.}}
\label{sec:3.8}

The reading begins with a short introduction on Magic: The Gathering,
where it explains the general circumstances and goals of the game.
It then moves on to discuss the purpose of the reading;
deciding the complexity of the legality of a single move in Magic.
The general rules and types of cards and what do they are then explained in detail.
The concepts of the deck, the player's hand, the stack, the battlefield,
and the graveyard are briefly discussed, and the types of cards included are instants/sorceries,
artifacts/enchantments, planeswalkers, creatures, and lands.
Some of the variants of the game are discussed,
including one of the single player variants known as Goldfish.
It then moves on to discuss the mathematical implications of
deciding the time complexity of the legality of a single move.
The reading discusses the ideas of min-cost flow,
the base case of having no requirements or restrictions on a move,
and the types of possible restrictions.
These restrictions include local restrictions, local counting restrictions,
global counting restrictions, blocker capacity restrictions, and attack capacity restrictions.
The complexity of the base case is then discussed,
as well as the complexities of special cases.
The reading concludes with a more in depth analysis of restriction one, the local restriction.

\subsection{Literary Analysis Conclusion}

In these scholarly readings regarding the game Magic: The Gathering and optimization algorithms,
we have gained a greater understanding of both how the game functions
as well as how we can accomplish our goal of optimizing the creation of competitive decks
based on a set of randomly generated cards.
Some possible strategies that we have read about include
using a Monte Carlo Search Tree approach, genetic programming, as well as active set methods.

\section{Algorithm Design}

In order to determine an optimal deck of cards that can be created from a given card pool,
we must define a way of evaluating each candidate deck
such that an optimization algorithm can determine
the subset of the card pool would perform most optimally.
For this we have defined an objective function that will analyze a proposed deck
and calculate a penalty for it, which an optimization algorithm can try to minimize.

\subsection{Objective Function}

In order to calculate the penalty on a single deck,
we have identified 6 sub-objectives, each considering
important aspects of previously successful decks that we want every deck to share.
% Some of the specified penalty values have not yet been fully tested and may change in the future.

\subsubsection{Deck Size}
\label{objective:Deck Size}

In the sealed format of Magic: The Gathering,
players are given 6 booster packs, each containing 15 cards,
and are able to build a deck out of whichever cards they get out of the packs.
The player can also add as many basic lands as they desire,
which are used to activate and play other cards.
When it is all said and done,
the ideal number of cards to have in a deck is the minimum allowed, 40 cards.
The reasoning behind this is that, when drawing a card,
having a smaller deck size will increase odds of the player drawing a useful card.
The larger the deck is,
the more likely it is filled with \enquote{bloat} cards,
or cards that are not very useful.
As such we will penalize for excess cards using the following quadratic function:

$$
f(n) = (40 - n) ^ 2
$$

with $ n $ being the actual number of cards in a constructed deck.

\subsubsection{Mana Curve}
\label{objective:Mana Curve}

Magic: The Gathering cards require certain amounts of \enquote{mana} in order to be played.
This mana cost is denoted on the top-right corner of every card.
Ignoring the specific mana \enquote{color} required,
each card can be categorized by the amount of mana required to play it.
For example, a card with \{1\}\{R\} would have an overall cost of 2 mana
where as a card with just \{R\} would have an overall cost of 1.
This overall cost is known as the \enquote{converted mana cost} or CMC.

Looking at the converted mana cost can be particularly useful in evaluating a deck.
A card's strength is generally directly correlated to its converted mana cost.
So, 8-CMC cards tend to be very powerful
whereas 1-CMC cards tend to be not as strong.
However, since a player's mana pool only slowly increases throughout the game,
someone who stocks up on the more-powerful cards may not be able to play them until late in the game.
On the flip side, if a deck is primarily filled with weaker 1-CMC cards,
then he can be very active in the beginning of the game
but may lack the more-powerful cards needed for the end-game.
So there exists a fine balance between how many cards of each CMC a deck should have.
This balance is what players refer to as an ideal \enquote{mana curve}.

If one were to count the converted mana cost of each card in the deck and store it in a vector,
the relative frequency graph of that vector is the \enquote{mana curve}.
Specifically, the ideal mana curve can be represented with the following R code:

\begin{minted}{R}

ideal.mana.curve <- c(
    1,
    2, 2, 2, 2, 2,
    3, 3, 3, 3,
    4, 4, 4, 4,
    5, 5, 5,
    6, 6)
relative.frequency <- table(ideal.mana.curve) / length(ideal.mana.curve)

\end{minted}

To differentiate an optimal mana curve from a suboptimal one,
let us first recall cumulative distribution functions (CDFs) from statistics.
Given a discrete random variable $ X $ and a probability mass function $ f $,
a CDF $ F $ is defined as:

$$
F(x) = P(X \le x) = \sum_{t \le x} f(t).
$$

We can use the CDF to describe our mana curve as follows:

\begin{tabular}{|l|l|}
\hline

$ F(1) $ & $ \frac{1}{19} $ \\ \hline
$ F(2) $ & $ (\frac{1}{19}) + \frac{5}{19} = \frac{6}{19} $ \\ \hline
$ F(3) $ & $ (\frac{6}{19}) + \frac{4}{19} = \frac{10}{19} $ \\ \hline
$ F(4) $ & $ (\frac{10}{19}) + \frac{4}{19} = \frac{14}{19} $ \\ \hline
$ F(5) $ & $ (\frac{14}{19}) + \frac{3}{19} = \frac{17}{19} $ \\ \hline
$ F(6) $ & $ (\frac{17}{19}) + \frac{2}{19} = \frac{19}{19} $ \\ \hline
$ F(7) $ & $ (\frac{19}{19}) + \frac{0}{19} = \frac{19}{19} $ \\ \hline
$ F(8) $ & $ (\frac{19}{19}) + \frac{0}{19} = \frac{19}{19} $ \\ \hline

\end{tabular}

Now that we have a definition of the ideal mana curve,
let us contrast it to a suboptimal mana curve
so we can develop a quantitative measure for how the two differ.
For example, take the following mana curve:

\begin{minted}{R}

bad.mana.curve <- c(
    1,
    2,
    3,
    4, 4, 4, 4,
    5, 5, 5, 5, 5, 5,
    6, 6,
    7, 7,
    8, 8, 8, 8, 8, 8)

bad.relative.frequency <- table(bad.mana.curve) / length(bad.mana.curve)

\end{minted}

Using its CDF $ F' $, we can describe this mana curve with the following table:

\begin{tabular}{|l|l|}
\hline

$ F'(1) $ & $ \frac{1}{23} $ \\ \hline
$ F'(2) $ & $ (\frac{1}{23}) + \frac{1}{23} = \frac{2}{23} $ \\ \hline
$ F'(3) $ & $ (\frac{2}{23}) + \frac{1}{23} = \frac{3}{23} $ \\ \hline
$ F'(4) $ & $ (\frac{3}{23}) + \frac{4}{23} = \frac{7}{23} $ \\ \hline
$ F'(5) $ & $ (\frac{7}{23}) + \frac{6}{23} = \frac{13}{23} $ \\ \hline
$ F'(6) $ & $ (\frac{13}{23}) + \frac{2}{23} = \frac{15}{23} $ \\ \hline
$ F'(7) $ & $ (\frac{15}{23}) + \frac{2}{23} = \frac{17}{23} $ \\ \hline
$ F'(8) $ & $ (\frac{17}{23}) + \frac{6}{23} = \frac{23}{23} $ \\ \hline

\end{tabular}

We can define the difference between the two mana curves in the following manner:

\begin{tabular}{|l|l|}
\hline

$ \abs{F(1) - F'(1)} $ & $ \abs{(\frac{1}{19}) - (\frac{1}{23})} = \frac{4}{437} \approx 0.92\% $ \\ \hline
$ \abs{F(2) - F'(2)} $ & $ \abs{(\frac{6}{19}) - (\frac{2}{23})} = \frac{100}{437} \approx 23\% $ \\ \hline
$ \abs{F(3) - F'(3)} $ & $ \abs{(\frac{10}{19}) - (\frac{3}{23})} = \frac{173}{437} \approx 40\% $ \\ \hline
$ \abs{F(4) - F'(4)} $ & $ \abs{(\frac{14}{19}) - (\frac{7}{23})} = \frac{189}{437} \approx 43\% $ \\ \hline
$ \abs{F(5) - F'(5)} $ & $ \abs{(\frac{17}{19}) - (\frac{13}{23})} = \frac{144}{437} \approx 33\% $ \\ \hline
$ \abs{F(6) - F'(6)} $ & $ \abs{(\frac{19}{19}) - (\frac{15}{23})} = \frac{8}{23} \approx 35\% $ \\ \hline
$ \abs{F(7) - F'(7)} $ & $ \abs{(\frac{19}{19}) - (\frac{17}{23})} = \frac{6}{23} \approx 26\% $ \\ \hline
$ \abs{F(8) - F'(8)} $ & $ \abs{(\frac{19}{19}) - (\frac{23}{23})} = 0\% $ \\ \hline

\end{tabular}

Using this approach,
we can define a penalty for being different from our ideal mana curve.
Namely,
\scalebox{0.5}{
$$
penalty(F') = \abs{5\% - F'(1)} + \abs{31\% - F'(2)} + \abs{52\% - F'(3)}
    + \abs{73\% - F'(4)} + \abs{89\% - F'(5)} + \abs{100\% - F'(6)}
    + \abs{100\% - F'(7)} + \abs{100\% - F'(8)}
$$}

Using this function, the penalty for the suboptimal mana curve would be
$ penalty(F') \approx 2.00 $.

\subsubsection{Land Percentage}
\label{objective:Land Percentage}

During gameplay,
players pay the cost for these cards in terms of a special card type known as a land.
Lands have a 0 CMC (so they are free to play)
and have a specific color corresponding to the kind of mana cost they can satisfy.
As with the previous sub-objective,
there exists another fine balance between having powerful non-land cards and lands to support them.
Having too few lands means not having enough mana to play the non-land cards
but, at the same time, having too many lands means not having enough non-land cards to play.
Experts recommend having a 17:23 lands-to-non-land ratio in a 40-card deck,
which is about 42.5\% lands,
but also permit 16:24 and 18:22 ratios.

In either case,
we want this ratio to be below 50\% since if it were 50\%,
then there would be a 50/50 chance every time the player draws a card that the card would be a land.
A ratio higher than 50\% would yield even more lands.
This many lands is not practical in-game,
so we want the ratio to be lower.
We will calculate the penalty for having a higher
or lower ratio of lands to non-lands with the following function:

$$
f(r) = \begin{cases}
    0 & \frac{16}{40} <= r <= \frac{18}{40} \\
    \abs{\frac{17}{40} - r} * 1000 & r > 0.75 \\
    \abs{\frac{17}{40} - r} & otherwise
\end{cases}
$$

with $ r $ being our ratio of lands to non-lands.

\subsubsection{Land Color Percentage}
\label{objective:Land Color Percentage}

As mentioned previously,
most cards in Magic: The Gathering require mana in order to be played
and the mana cost is denoted with mana symbols in the top-right corner.
Some mana symbols require a specific color of mana, such as black or red,
whereas others accept any color.
We want to craft the color ratio of our lands such that it is equal to
the ratio of the mana cost of the cards in our deck.
For example,
if we take the cards in our deck and count all the red mana symbols on them,
we can find the ratio of red mana symbols to the total number of mana symbols
and we should strive to make ratio of our red lands to the total number of lands equal to it.

Say we count 19 red mana symbols of 35 mana symbols in total.
The ratio of red mana symbols is $ \frac{19}{35} $ or about $ 54\% $.
Lets say that in this example we want to have 16 lands total.
Given this ratio,
we would want $ \frac{19}{35} * 16 \approx 9 $ of our lands to be red lands.

By comparing the deck being analyzed's ratio of each land and the total number of lands
to the ratio of mana symbols of the color that land would produce and the total number of mana symbols,
we can analyze how ideal our landbase is. For each color of mana symbol used within the deck,
we apply this function to calculate the penalty for the incorrectness of the manabase

$f(w,u,b,r,g)=|w'-w|+|u'-u|+|b'-b|+|r'-r|+|g'-g|$

where w, u, b, r, and g denote the land color ratio for each color within the deck
while their prime counterparts represent the corresponding mana symbol ratio for the deck.

\subsubsection{Color Identity}
\label{objective:Color Identity}

The ideal deck primarily consists of two mana colors.
Any other color underneath a certain threshold is considered to be a \enquote{splash}.
We define a splash to be a color with a relative frequency less than 10\%.
The best case scenario is a deck that is solely two color deck, with no splashes,
so our function will penalize any colors that do not count as the dominant two.
% It will also make sure that if a splash does occur, it only occurs once.
The penalty would be determined by a linear function such as:

$$
f(colors, splashes) = c_1 * \abs{2 - colors} + c_2 * splashes
$$

where `colors' denotes the number of dominant colors,
`splashes' denotes the number of splashed colors,
and $ c_1 $ \& $ c_2 $ are separate values to weight the results.

\subsubsection{Card Archetypes}
\label{objective:Card Archetypes}

In the card game Magic: The Gathering there are many \enquote{archetypes} of cards that are
important to keep in mind when creating a sealed deck.
The acronym BREAD is used to describe some of these archetypes.
This acronym stands for Bombs, Removal, Evasion, Aggro, and Duds.
In addition to the archetypes described,
I believe that there are other archetypes such as
mana-fixing and combat tricks,
which are also key in creating a successful sealed Magic: The Gathering deck.

Bombs refer to cards with such powerful effects or strength that they can potentially turn
the tide of the game in your favor just by playing them.
In a general context, we would ideally have one or more bombs.
So, in calculating the score of the deck,
if the deck contains a bomb
then no deduction will be made in this regard.
Otherwise, there will be a deduction of $ 10 $.
The penalty for bombs can be represented as:

$$
b(bombs) = \begin{cases}
    0 & bombs \ge 1 \\
    10 & otherwise
\end{cases}
$$

where `bombs' denotes the number of bombs present within the deck.

\enquote{Removals} are
cards that remove your opponent's cards through mechanisms such as
damage, destruction, removal from the game, or preventing your opponent from even playing the threat.
In the context of our chosen tournament type,
removals are those that generally deal damage to creatures,
as access to good
artifacts, enchantments, and planeswalkers tend to be very few and far between.
As this category is a key element in many successful limited decks,
we want at least $ 2 $ removal spells to stop our opponent from progressing their own board state.
The penalty for removal can be represented as

$$
r(removal) = \begin{cases}
    0 & removal \ge 2 \\
    c(2-removal) & otherwise
\end{cases}
$$

where `removal' denotes the number of removal spells present within the deck
and $ c $ denoting a constant to weigh the result.

\enquote{Evasive} cards are
more difficult for an opponent to block.
They require the opponent to use a removal spell or have creatures that meet a specific criterion.
Some examples of evasive cards are creatures
with flying or that are unblockable by other means.
Evasion is important as it allows you to damage an opponent
in a fashion that he would otherwise not have to deal with.
Since evasive creatures do not always present in every color,
we will only penalize decks whose dominant colors contain the possibility for an evasive.
Only if an evasive card in one of those colors is provided as an option,
will we penalize the deck if it does not include at least $ 2 $ of them.
The penalty for removal can be represented as

$$
e(evasive) = \begin{cases}
    c(2 - evasive) & evasive < 2 \land \text{evasive is an option in the deck's dominant colors} \\
    0 & otherwise
\end{cases}
$$

where `evasive' denotes the number of evasive creatures present within the deck
and $ c $ denoting a constant to weigh the result.

\enquote{Aggro} is the archetype describing aggressive creatures or spells,
which limited decks tend to be primarily comprised of.
These cards allow for constant pressure to be placed upon opponents
and potentially allow you to win the game.
They do not have the sheer strength or board presence of a bomb
but instead act as smaller, yet constant, nuisances that your opponent
has to handle or risk losing the game.
As this is a good portion of limited decks,
the quantity of these types of cards will likely be greater than those of other archetypes.

When creating a sealed deck,
there will ultimately be cards that are in the pool of cards
that can be potentially put into a deck that will be near unusable due to multiple reasons.
there can be cards that are nearly unusable for multiple reasons.
Some of these reasons include cards of different colors or
cards that are simply just too bad to utilize.
These cards are of the final letter in the BREAD acronym called \enquote{duds}.
In a sealed deck,
card quality is very important to the success of the deck,
so we want to prioritize the better cards to increase its quality.
Decks containing these \enquote{duds} will receive a penalty of $ 5 $ per dud card
as they are virtually unplayable.
The penalty for including duds within a deck can be represented as

$$
d(duds) = 5 * duds
$$

where `duds' denotes the number of dud cards within the deck.

In a game of Magic: The Gathering,
in order to cast a spell one needs access to certain colors of mana to pay for the cost of each spell.
Since there are 5 different colors of mana,
the more colors that one chooses for their deck to contain
then the harder it will be to obtain all of the mana in the right color combinations
in order for them to cast their spells.
Mana-fixing cards allow for one to usually choose one of multiple colors of mana
to allow for flexibility in how they play their spells.
If a deck is running multiple colors
it is highly recommended to use cards that allow for you to fix your
mana supply with the colors that you need.
In the case that a play decides to play more than two colors,
they almost need to have mana fixing
otherwise in some games they will just sit there hoping
they get the land to provide them with the
mana they need to do anything, putting them at a severe disadvantage.
If a deck is playing 2 colors then usually only 1
mana-fixer is needed but any more than two colors and you definitely want two or three.
The deduction due to mana-fixing would be if a three or more color deck
has less than $ 2 $ mana-fixers then the deduction would be based on the difference of
2 and the number of mana-fixers that reside within the deck.
The mana-fixing deduction can be represented as

$$
m(mana) = \begin{cases}
    0 & x \ge 2 \\
    c(2 - mana) & otherwise
\end{cases}
$$

where `mana' denotes the number of mana-fixers within the deck
and $ c $ being a constant to weight the results.

During combat in Magic: The Gathering
the active player chooses which of their creatures to attack with
and then the defending player decides how they would like to block the attacking creatures.
Then the attacking creatures deal damage to the blocking creatures and vice-versa.
By a creature reaching 0 toughness or due to another ability,
creatures die and enter the graveyard where they usually lie until the end of the game.
By killing off your opponent's creatures,
you usually are given an advantage.
One way of accomplishing this is by using \enquote{combat tricks}.
Since your opponent does not know the cards in your hand,
they are unaware of what your plans are.
Combat tricks are cards or abilities that allow for interaction
with the attacking or blocking creatures.
This allows you to mess up your opponent's plans in your favor.

To calculate the complete value of penalties within this subsection,
we take the sum of all of the deductions made,
represented by:

$$
TP(deck) = b(bombs) + r(removal) + e(evasive) + d(duds) + m(mana)
$$

\subsection{Optimization}
\label{sec:Optimization}

Through our research, we have found three possible optimization algorithms
that we could use to select the cards for our objective function to grade.
These are ant colony optimization, particle swarm optimization,
and compartmentalized knapsack optimization.

\phantomsection
\label{paragraph:Ant colony}
Ant colony optimization is based off of the way ants move around.
When an ant finds a path leading to food,
they release a pheromone to attract other ants to tell they are headed the right way.
In this optimization algorithm, artificial `ants' move through a tree
containing all possible solutions to locate the optimal ones.
The ants record their positions and the quality of the solution to direct each other.
Since we can evaluate how good each card with the objective function described above,
the use of this algorithm
would allow us to find the best cards to use from the pool.

\phantomsection
\label{paragraph:Particle Swarm}
The second optimization algorithm we are considering is particle swarm optimization,
specifically the set-based variant.
This method simulates birds flocking to food.
It works by having all particles follow the two \enquote{best} values.
These two bests are the local best and the global best.
After each iteration the velocities of each particle is updated.
The set-based variant allows for a discrete search space,
as well as the velocity being defined as a set with possibilities.
While this form of optimization may not be the best in its normal state,
if we can modify it, this method would be preferable.
This is because we would be able to account for card synergies.
This would allow for the best deck to be selected given the cards
in the pool based off of more situational factors.

\phantomsection
\label{paragraph:Knapsack}
The third algorithm is a variation of the knapsack algorithm discussed during class.
It uses the same idea of maximizing the value of a \enquote{knapsack,}
which contains some elements.
This variation stores elements of the same type in compartments within the knapsack.
The compartments each have flexible capacities, but are lower and upper bounded.
The goal is to maximize the total value of all of the items in the knapsack
minus the cost of the compartments.

While searching for an efficient optimization algorithm that would suit our needs,
we discovered a project similar to ours that a grad student
named Troy Hernandez developed while working toward's his Master's degree.
This project took the cards from the booster packs the player's received
and returned the highest rated deck.
Similar to our project, though our project can currently be used for any Magic: The Gathering
Ravnica Allegiance deck, while Hernandez's project can only be used in the sealed format.
Our project could be modified to accept cards from any Magic: The Gathering set,
given more time.
Hernandez's optimization algorithm was written entirely in R,
and while we searched for a method of integrating an R function
into Python, we ended up not going with this method.

\section{Implementation}

\subsection{Data Sets}

To implement the objective function we designed,
we require a comprehensive description of the Magic: The Gathering
set of which a candidate solution of cards are members.
To this end,
we query a publicly available database containing all MTG cards to date.
% TODO: Add link or citation
The database contains information such as:

\begin{itemize}
    \item Set id
    \item Card number
    \item Card rarity
    \item Converted mana cost as well as individual cost components
    \item Card type
\end{itemize}

Using this data,
we can account for mana curve (\ref{objective:Mana Curve}),
land percentage (\ref{objective:Land Percentage}),
land color percentage (\ref{objective:Land Color Percentage}),
as well as color identity (\ref{objective:Color Identity}).
The deck size objective (\ref{objective:Deck Size})
can be trivially implemented independently of the above data
but accounting for card archetypes (\ref{objective:Card Archetypes}) requires additional data.
So, we decided to handcraft our own database using as much pre-existing data as possible.
We decided to keep the above columns and add the following:

\begin{itemize}
    \item Archetypes (Bomb, Removal, Combat trick, Evasive, Counter, Card draw, Mana fixing)
    \item Rating
\end{itemize}

To give an example of what this looks like in practice,
we represented the \enquote{Angel of Grace} card in the following fashion:

\begin{itemize}
    \item Set id: \enquote{RNA}
    \item Card number: $ 1 $
    \item Card rarity: \enquote{Mythic rare}
    \item Converted mana cost: $ 5 $
    \item Mana cost: \enquote{\{3\}\{W\}\{W\}}
    \item Card type: \enquote{Creature}
\end{itemize}

For mana cost,
we use a commonly-understood notation.
In this example,
\enquote{\{3\}\{W\}\{W\}} means $ 2 $ white mana symbols
and $ 3 $ additional mana symbols of any color
are required to play the card.

However, we found that this schema was not enough.
Additional complexities arise due to the fact that
some cards effectively have two faces.
These cards are essentially two cards printed on one piece
of paper.
However, due to their inseparability,
they share some properties in common
(such as rating or converted mana cost).
To accommodate for this inconsistency,
we decided to store both faces in the same data entry
using the same fields,
using the special value \enquote{ // } to separate unshared properties.

To demonstrate,
let's look at card 224 from the RNA set.
The card is divided in two and on one side it reads \enquote{Consecrate},
on the other it reads \enquote{Consume}.
It shares a converted mana cost of $ 6 $
while the left side is an \enquote{Instant} card type
and the right side is a \enquote{Sorcery}.
We represent such a card in the following manner:

\begin{itemize}
    \item Set id: \enquote{RNA}
    \item Card number: $ 224 $
    \item Card rarity: \enquote{Uncommon}
    \item Converted mana cost: $ 6 $
    \item Mana cost: \enquote{\{1\}\{W/B\}//\{2\}\{W\}\{B\}}
    \item Card type: \enquote{Instant // Sorcery}
\end{itemize}

Furthermore,
this card demonstrates another exception the game provides.
\enquote{Consecrate}'s mana cost is \enquote{\{1\}\{W/B\}},
meaning that it requires $ 2 $ mana symbols,
one of which can be of any color and the other must either be white or black.
Since the \enquote{W/B} notation greatly resembles how the mana symbols appear
on the card,
we internally represent the construct in this manner as well.

\subsection{Time complexity}

\subsubsection{Database parsing}

Before running our application,
we export our database to a single CSV file.

Then, at run time,
we read it in and process it in its entirety.
Since we process each card,
this phase is at least $ \Omega(n) $

During processing,
we convert each card into the following data structures:

% CardFace definition
\inputminted[firstline=161, lastline=165]{python}{../src/algorithm.py}

% Card definition
\inputminted[firstline=168, lastline=175]{python}{../src/algorithm.py}

For each card,
we split on the string \enquote{//} and create a CardFace for each half.
If the sentinal string is absent (the card is monolithic),
we just create a single instance of CardFace.
In Card,
we store the attributes common to the card faces.

Note that we are defining forward relations from a Card instance
to an instance of Rarity, Archetype, etc.
It will be useful, as we will later find,
to reverse the relation so we can go from Rarity, etc. to appropriate Card instances.
To this end,
we create another data structure SetInfo and it stores such reverse relations:

% SetInfo definition
\inputminted[firstline=188, lastline=194]{python}{../src/algorithm.py}

Finally,
we create a CardTypes data structure to store additional properties that are
specific card types.
For example,
\enquote{creature} cards have an additional power and toughness value
while \enquote{land} cards have an additional attribute \enquote{color}.

% CardTypes definition
\inputminted[firstline=178, lastline=185]{python}{../src/algorithm.py}

% Land, Enchantment, ..., Instant
\inputminted[firstline=122, lastline=151]{python}{../src/algorithm.py}

% parse_cards_csv(...) definition
% \inputminted[firstline=430, lastline=592]{python}{../src/algorithm.py}

% \subsubsection{Booster pack generation}

% To generate a pool of cards for use in an optimization algorithm,
% we have defined the following function.
% It generates a booster pack from a given Magic: The Gathering set
% and would be called $ 6 $ times to generate the entire card pool:

% % generate_booster_pack(...) definition
% \inputminted[firstline=219, lastline=252]{python}{../src/algorithm.py}

% Before each call to random.sample(\dots),
% each entry in set_info.rarities is first converted
% from a set type to a tuple type.
% Though perhaps dependent on the Python interpreter,
% this operation can be at worst $ O(n) $.
% We perform is type conversion due to the API contract of the random module.
% It requires a so-called \enquote{subscriptable} type.
% However,
% we want to use the set data type because it removes duplicates during the CSV parsing.

\subsubsection{Objective function}

The objective function loops over each card in a candidate deck of $ n $ cards.
In this loop,
it loops over $ f $ card faces with $ m $ mana symbols each.
It also loops over $ a $ archetypes present in the cards
as well as $ c $ distinct converted mana cost values.

So,
the time complexity can be said to be $ O(nmfc + nma) $.
However, $ 1 \le f \le 2 $, $ 0 \le a \le 7 $, and $ 0 \le c \le 5 $.
so these variables can be seen as constants, even in their worst case.
As a result,
we can more accurately say the time complexity to be $ O(nm) $.

\subsection{Test Cases}

\subsubsection{Deck 1: Good Deck}

In this test deck, from the test card pool we created an above average deck that is 2 colors with a splash of a third
and less dominant color. The color combination adds a penalty of $ 3 $ due to the addition of the third color.
The mana curve of this deck is pretty good so it only adds a penalty of $ 1 $ . In this deck there are 17 lands to
23 non-lands totalling to 40 cards. Since we are at the ideal deck size there is no penalty. Since the ratio of lands
to nonlands is also very close to ideal the penalty is only $ 1.5 $ . Due to the mana symbol ratio being very close
to ideal, the penalty is only $ 0.58666 $ . Finally the archetypes represented in the deck lead to a penalty of 55.
This deck's total penalty adds up to be $ 61.17 $ .

\subsubsection{Deck 2: Somewhat Bad Deck}

In this test deck we created a somewhat bad 5 color deck. The color penalty in this case is $ 3 $ .
Due to this deck's better mana curve than Deck 1 the mana curve penalty is $ 0.4999 $ which is lower than
Deck 1's mana curve penalty. Due to this deck only containing 9 land cards, the land ratio penalty is $ 7 $
which is significantly higher than Deck 1's. Due to this deck's 5 color nature, the mana symbol penalty
is $ 0.9356 $ . Finally this deck's archetype penalty is $ 60 $ . This deck's total penalty is $ 71.4356 $ which is
greater than Deck 1's total penalty. This result is to be expected due to this deck's contents.

\subsubsection{Deck 3: Bad Deck}

This test case was created to show that our objective function works to accurately grade decks
where even though they are so bad that they are extremely suboptimal but they still could be played
and in extremely rare situations could win. This deck contains 39 lands and a single creature. This leads to a
huge penalty in the land ratio penalty of $ 11000 $ . Overall the total penalty is $ 11084.975 $ .

\subsubsection{Deck 4: Very Bad Deck}

This deck is similar to Deck 3 in terms of being extraordinarily. It differs from the previous
deck in the fact that it is entirely lands and therefore will never win. The land ratio penalty for this
deck shows that since it is $ 11500 $ which is 500 greater than Deck 3. This deck's total penalty is $ 11554.5 $ .

\subsubsection{Test Case Conclusions}

These test cases illustrate that the objective function grades decks to an acceptable degree. However the influence
of each of the individual penalties are not all equivalent and therefore future work should include weighting
these penalties to obtain a more accurate and detailed result.

\section{Contributions}

\begin{itemize}

\item Scott Harris

\begin{itemize}

    \item Literary Review Sections \ref{sec:3.1} \& \ref{sec:3.2}
    \item Archetypes Sub-objective (\ref{objective:Card Archetypes})
    \item Magic: The Gathering Consultant/Researcher
	\item PowerPoint slides
    \item Test Case Creation, Analysis, and Description
    \item Contribution: 25\%

\end{itemize}

\item John Meyer

\begin{itemize}

    \item Literary Review Sections \ref{sec:3.5} \& \ref{sec:3.6}
    \item Mana-Curve Sub-objective (\ref{objective:Mana Curve})
    \item Project Lead
	\item Powerpoint slides
    \item Data Structure Creation and Implementation
    \item Implementation of Objective Function Lead
    \item Contribution: 35\%

\end{itemize}

\item Bryan Hayes

\begin{itemize}

    \item Literary Review Sections \ref{sec:3.7} \& \ref{sec:3.8}
    \item Deck-size (\ref{objective:Deck Size}) \&
        Land Ratio (\ref{objective:Land Percentage}, \ref{objective:Land Color Percentage}) Sub-Objectives
    \item Ant Colony Optimization Paragraph (\ref{paragraph:Ant colony})
	\item Knapsack Optimization Paragraph (\ref{paragraph:Knapsack})
	\item Booster pack generation
	\item Knapsack problem research
	\item PowerPoint slides
    \item Contribution: 20\%

\end{itemize}

\item Jacob Freedman

\begin{itemize}

    \item Literary Review Sections \ref{sec:3.3} \& \ref{sec:3.4}
    \item Color Identity (\ref{objective:Color Identity}) Sub-objective
    \item Researched Optimization
    \item Particle Swarm Optimization Paragraph (\ref{paragraph:Particle Swarm})
	\item Booster pack generation
	\item Knapsack problem research
	\item PowerPoint slides
    \item Contribution: 20\%

\end{itemize}

\end{itemize}

\begin{thebibliography}{9}

% Scott's

\bibitem{Cowling2012}
Peter I. Cowling, Colin D. Ward, and Edward J. Powley. 2012.
\enquote{Ensemble Determinization in Monte Carlo Tree Search for the Imperfect Information Card Game Magic: The
Gathering.}
(June 2012). Retrieved March 4, 2019 from https://ieeexplore.ieee.org/document/6218176/

\bibitem{Ham2010}
Ethan Ham. 2010. (April 2010).
\enquote{Rarity and Power: Balance in Collectible Object Games.}
Retrieved March 4, 2019 from http://gamestudies.org/1001/articles/ham

% Jacob's

\bibitem{Pelikan1999}
Martin Pelikan, David Goldburg, Erick Cantú-Paz. 1999. BOA: Bayesian Optimization Algorithm. Illinois Genetic Algorithms
Laboratory. University of Illinois at Urbana-Champaign,Urbana IL.

\bibitem{Saint-Etienne2018}
Marie-Liesse Cauwet Mines Saint-Etienne, Olivier Teytaud. 2018. Surprising strategies obtained by stochastic
optimization in partially observable games. Univ Clermont Auvergne, CNRS, UMR 6158 LIMOS, Institut Henri Fayol,
Departement GMI, France

% John's:

\bibitem{Buchheim2014}
% ACM citation style
Christoph Buchheim and Long Trieu. 2014.
Active Set Methods with Reoptimization for Convex Quadratic Integer Programming.
In \textit{Combinatorial Optimization: Third International Symposium,
ISCO 2014 Lisbon, Portugal, March 5–7, 2014 Revised Selected Papers}.
Springer International Publishing Switzerland.
DOI: 10.1007/978-3-319-09174-7.

\bibitem{Furer2014}
% ACM citation style
Martin Fürer and Huiwen Yu. 2014.
Approximating the k-Set Packing Problem by Local Improvements.
In \textit{Combinatorial Optimization: Third International Symposium,
ISCO 2014 Lisbon, Portugal, March 5–7, 2014 Revised Selected Papers}.
Springer International Publishing Switzerland.
DOI: 10.1007/978-3-319-09174-7.

% Bryan's:

\bibitem{Chatterjee}
Krishnendu Chatterjee and Rasmus Ibsen-Jensen.
The Complexity of Deciding Legality of a Single Step of Magic: The Gathering.

\bibitem{Crutcher2017}
Paul A. Crutcher. 2017. Magic: The Gathering, A Literary Text.
\textit{Americana: The Journal of American Popular Culture} 16, 1 (2017).

\end{thebibliography}

\end{document}


\input{structure.tex}
\graphicspath{{9_fig/}}
\title{第九章\ 熵和信息}

\begin{document}

\maketitle

In thermodynamics, you were told by a law that ``entropy'' increases, in order to forbid the dream of a perpetual motion machine. To be more explicit, for an isolated system consists of $n$ subsystems, for each subsystem $i$ define
\begin{align}
    \Delta S_i = \frac{\Delta Q_i}{T} ~,
\end{align}
where $\Delta Q_i$ is the heat flowing into the subsystem if the process is reversible. Then for a general process, the entropy of the system (Clausius 1855) satisfies the second law of thermodynamics\index{entropy: Clausius}:
\marginnote{And recall that the first law is
\begin{align}\label{eq:tdfirst}
    dE = TdS - pdV~.
\end{align}}
\begin{align}
    \Delta S = \sum_{i=1}^n \Delta S_n \geq 0~.
\end{align}
The equal sign is taken for reversible process; and for non-reversible processes $S>0$. Also, $S$ is a quantity describing the state of the system.

Instead of repeating the standard discussion in thermodynamics here (I assume that you know them. Otherwise see further readings), let us think about a few questions left unclear:

\textbox{The mysterious entropy in thermodynamics}{
    \tenum{
        \item $S$ describes the state of the system. Which aspect of the system does $S$ describe? \marginnote{For other such quantities such as internal energy $E$, volume $V$, pressure $p$, temperature $T$, we can feel them and know what we are talking about.}
        \item Fundamental laws of nature has no arrow of time in it. How can anything increase, which generates an arrow of time?
        \item \label{item:demon} Let's do a thought experiment. \index{Maxwell's demon}
        \marginnote{\includegraphics[width=0.3\textwidth]{m_daemon}}
        A box of equilibrium gas is separated into left and right parts, with a small gate in between them. The gate keeper is a little demon (known as Maxwell's demon). The demon only allow molecules with $v>v_0$ to enter the right part and molecules with $v<v_0$ to enter the left part. After some time, would the right part hotter than the left? If so, is the second law of thermodynamics violated?
    }
}

\section{The Statistical Entropy}\label{sec:stat-s}

To take a closer look at entropy, let us first review the condition of equilibrium:

\textbox{Equilibrium of subsystems: thermodynamics}{
    Consider an isolated system containing two sub-systems $A$ and $B$. $A$ and $B$ can exchange heat but each has fixed volume and fixed particle number. The total energy $E=E_A+E_B$ is fixed since the whole system is isolated. Suppose $A$ and $B$ are in thermal equilibrium individually.

    Under which condition is the whole system in equilibrium?

    \tcblower

    They have the same temperature. Using the first law \eqref{eq:tdfirst} (with fixed volume and thus $dV=0$), we have
    \begin{align} \label{eq:eq-AB-thermo}
        \frac{dS_A}{dE_A} = \frac{dS_B}{dE_B}~,
    \end{align}
    since both of them equals to $1/T$.
}

\mtextbox{Beyond equal probability}{
    What if we relax our assumption such that each microscopic state has different probability to appear? Then the Boltzmann's entropy formula needs to be generalized. Here we only quote the result:\index{entropy: Gibbs}
    \begin{align}
    S = k_B \sum_i p_i \ln \frac{1}{p_i}~,
    \end{align}
    where the summation runs over all microscopic states. This formula is known as the Gibbs entropy. One can easily check that if for all $i$, $p_i=1/\Omega$, then Gibbs entropy returns to Boltzmann entropy. The Gibbs entropy can be further generalized into the entanglement entropy (Von Neumann entropy) in quantum mechanics.\index{entropy: Von Neumann}
}
\textbox{Equilibrium of subsystems: statistical point of view}{
    Consider the same system as in the previous box.
    We know the total energy $E$ of the system, and volume and particle number of individual subsystems $A$ and $B$. Given this knowledge, there are exponentially many possible microscopic states \index{microscopic states} satisfying these constraints (these microscopic states as a collection is known as an ensemble\index{ensemble}).

    We \emph{assume} that each possible microscopic state has equal chance to appear.

    What is the nature of the equilibrium state between subsystems $A$ and $B$? Say, how to partition $E$ into $E_A$ and $E_B$? The partition should correspond to a maximal number of microscopic states, to maximize its probability to appear.

    For given $E_A$, $E_B=E-E_A$ is fixed. Let's denote the number of possible microscopic states of the subsystem $A$ and $B$ as $\Omega_A(E_A)$ and $\Omega_B(E_B)$, respectively.

    The total number of microscopic states is
    \begin{align}
        \Omega = \Omega_A(E_A) \times \Omega_B(E_B)~.
    \end{align}
    For $\Omega$ to be extremal (most number of microscopic states), we need
    \begin{align}
        0
        = \frac{d\Omega}{dE_A}
        = \Omega_B \frac{d\Omega_A}{dE_A}
        + \Omega_A \frac{d\Omega_B}{dE_B} \frac{dE_B}{dE_A}
        = \Omega_B \frac{d\Omega_A}{dE_A}
        - \Omega_A \frac{d\Omega_B}{dE_B}~,
    \end{align}
    Thus the equilibrium condition is
    \begin{align}
        \frac{d\ln \Omega_A}{dE_A} = \frac{d\ln \Omega_B}{dE_B}~.
    \end{align}
    Compared with \eqref{eq:eq-AB-thermo}, we conclude that\index{entropy: Boltzmann}
    \begin{align}
        S = k_B \ln \Omega~,
    \end{align}
    where $k_B$ is a constant, known as the Boltzmann constant.
    \mtextbox{Entropy and disorder}{
        To be more intuitive about entropy and disorder, consider a box of gas. Each particle can stay either in the left half and right half of the box. An ordered situation is that all particles stay in the left (or right); and a disordered situation is that half particles stay in the left and half stay in the right. There are way more states for the disordered situation. We leave the details to an exercise.
    }
    Detailed comparison between thermodynamics and statistical physics gives $k_B \simeq 1.38 \times 10^{-23} ~ \mbox{J}/\mbox{K}$. This relation is discovered by Boltzmann in the 1870s and Planck put it into the current form in around 1900.

    \tcblower

    To summarize what we learned here, entropy measure (the logarithm of) the number of possible microscopic states.
    This can be considered as an indicator of disorder. The 2nd law is thus a more universal saying of ``if you do not clean up your room, it will automatically become more disordered instead of cleaner.''
}

\section{The Arrow of Time}

There is an arrow of time. Time flows toward the future and do not come back. Who has stolen your time? Spilled water take much more efforts to be gathered up again. Who has stolen your efforts?

\textbox{Are past and future equivalent?}{
    In Newton's mechanics of a particle,
    $
      F = m \frac{d^2 x}{dt^2}
    $.
    If we perform the time reversal transformation $t\leftrightarrow -t$, we note that the form of the equation does not change its form. (The action of Newton's mechanics also has the time reversal symmetry).

    In fact, for all the known fundamental laws of nature, if we take $t\leftrightarrow -t$, and left to right (parity), and particle to anti-particle (charge conjugate), then nothing changes.

    In other words, at the level of a fundamental particle, there is no difference between past and future.

    However, we do feel the difference between past and future. Why?
}

\textbox{Why is there a psychological arrow of time?\index{arrow of time: psychological}}{
    What is the psychological difference between past and future?
    \marginnote{\includegraphics[width=0.35\textwidth]{past_now_future}}
    We remember the things in the past, but not the things in the future.

    Why we can only remember things in the past? Because getting our brain prepared to remembering things needs increase of entropy. Thus the psychological time arrow has to agree with the thermodynamic time arrow, defined by $\Delta S \geq 0$.
}

\mtextbox{(Optional) Entropy and black holes\index{entropy: Bekenstein-Hawking}}{
    Why the earth is spherical? High objects will fall.
    \newline
    \includegraphics[width=\textwidth]{spherical_flag}
    \newline
    The black hole has infinitely deep gravitational potential. Thus there is no hill on a black hole. Classically, a black hole contains as little information as a fundamental particle: its mass $M$, angular momentum $L$, and charge $Q$. So at first sight, a black hole carries as little entropy as a particle: $S \sim k_B \times O(1)$.
    \tcblower
    Then what if we throw something into the black hole? Would the number of possible microscopic states decrease and thus $\Delta S<0$?
    \mnewline
    Taking quantum mechanics into consideration (Bekenstein \& Hawking), black holes in fact have the hugest number of microscopic states: $S = k_B A / (4l_p^2)$, $l_p \equiv \sqrt{G\hbar/c^3} \simeq 1.6 \times 10^{-35}$m . For example, for a black hole with horizon area $1\mathrm{cm}^2$, the information stored is $10^{65} \sim 2^{216}$, which corresponds to $10^{53}$ hard disks, each with $1$TB capacity. Hard disks are made in the unit of nanometer, while black hole entropy is in the unit of Planck length ($10^{-35}$m).
}
\textbox{Why $\Delta S \geq 0$?}{
    The second law of thermodynamics is somewhat psychological: Even if we live in a universe where entropy decreases, we still feel increase of entropy because we feel that the time arrow is reverted (we can only remember things in the other time direction).
}

\textbox{But why not $\Delta S =0$?\index{arrow of time: thermodynamical}}{
    However, according to statistical physics, a system is more likely in the macroscopic state with maximum underlying microscopic states. If it is the case for our universe, then we should stay in equilibrium almost forever and have $\Delta S = 0$. Why our universe is not the case? Unfortunately, we don't know a definite answer at the moment.
}

\textbox{Other time arrows in physics}{
    There are a few other physical effects which are not time reversal invariant:
    \titem{
        \item The collapse of wave function after measurement. This is probably because of decoherence and follows from the thermodynamics arrow.
        \item Radiation of charge propagates to the future, not the past. This is again related to the thermodynamic arrow of time. The situation is similar to that we often see a stone drops onto water and water waves propagated away. But we do not often see water waves propagates inward and excite a stone out. (Except in carefully designed devices, for example the Chinese fish basin.)
        \item Black hole (classically) absorbs matter but does not emit matter. It is also related to entropy as we comment in the margin.
        \item Weak interaction. As we mentioned before, we have to revert parity and charge together with time reversal to make weak interaction invariant.
        \item Cosmological expansion. There is no evidence to show that the cosmological expansion is tied to the thermodynamic time arrow. It may be an independent arrow of time.
    }
}

\section{Entropy and Information}\label{sec:info-s}

\textbox{Maxwell's demon\index{Maxwell's demon}}{
    Recall \ref{item:demon} at the beginning of this part: Can the Maxwell demon reduce the entropy? If so, can we use it to do work as a perpetual motion machine?

    If the demon indeed \emph{knows} the states of the molecules as Maxwell has described, then indeed, he can use it to reduce entropy and do work. As we know,

    \begin{quote}
        Knowledge is power -- Francis Bacon
    \end{quote}

    \tcblower

    Now, does it contradict with the 2nd law? No. Information is not free lunch. There are two possibilities (or combination of them) for the demon to know the states:
    \tenum{
        \item The demon has a huge memory in his brain, to record all the molecule data. In this case, the demon's brain must also contain a huge number of possible configurations $\Omega_D$. The entropy of the demon's brain $S_D=k_B \ln \Omega_D$ must be considered as well. The cost of making the gas from disordered to ordered is to make the demon's brain from ordered to disordered.
        \item The demon has little (say, only one bit of) memory. So he has to measure and know the information about the molecules one by one when they arrive. To know the state of one molecule, the demon has to erease the state of the previous molecule from his mind. Landauer (1961)\index{Landauer's principle} noticed that, to erase one bit of information, the minimal entropy generated is $k_B \ln 2$ (from the 1st law, the associated heat is at least $k_B T \ln 2$). Adding this part of increased entropy, the 2nd law is not broken.
    }
}

The Maxwell's demon unveils deep connection between entropy and information. What's information?

\textbox{From a newspaper editor to an information theorist\index{information content}}{
    Journalists may be among the first people who understand information deeply. They benefit from news (information). What is a piece of news (information)?

    For example, in 1909, a newspaper editor Greeley said,
    \begin{quote}
        If a dog bites a man that's nothing; but if a man bites a dog, that's news.
    \end{quote}
    Following the spirit of Greeley, what's two pieces of news (information)? Two men bites two dogs, respectively and independently, are two pieces of news.
    \tcblower
    What can we observe about the nature of information from above?
    \titem{
        \item A man bites a dog is more informative than a dog bits a man: for an event with probability $p$ to happen, the information content $h(p)$ should be a decreasing function of $p$. For example, $h(p)$ may be related to $1/p$ in some ways.
        \item Two men bites two dogs are two pieces of information: Information is additive for the information content of independent events. For two events with $p_1$ and $p_2$ probabilities to happen, the probability for both of them to happen is $p_1 p_2$. Thus the informaiton content should satisfy
        $ h(p_1p_2) = h(p_1) + h(p_2) $.
    }
    The most natural function to satisfy the above observations is \marginnote{Here we have used logarithm of base 2 just to match the convention of information theory. It's trivial to convert it to physicists convention by noting $\log_2 x = \ln x / \ln 2$.}
    \begin{align}
        h(p) = \log_2 \frac{1}{p} ~.
    \end{align}

    The unit of information content is \emph{bit}, since it reflects the minimal binary bits needed to encode the corresponding information.
}

\textbox{\emph{Thought experiment}: efficient cheating in an exam}{
    Let's do a thought experiment, i.e. don't do it in reality: cheat in an exam with your partner, by sending answers of multiple choice questions (one of $A$, $B$, $C$, $D$) by binary code. You would like to send minimal length of binary codes to minimize the chance of being caught. Suppose you know the probability $p_A, p_B, p_C, p_D$, how to encode the cheat message most efficiently?

    Instead of a general discussion, let's consider two examples:
    \tenum{
        \item $p_A=p_B=p_C=p_D=1/4$. The information conetent for each choice is $\log_2 (4) = 2$. In other words, each piece of information deserves two binary bits of information. We may encode like $A\rightarrow 00$, $B\rightarrow 01$, $C\rightarrow 10$, $D\rightarrow 11$.
        \item $p_A=1/8$, $p_B=1/8$, $p_C=1/2$, $p_D=1/4$. This is more interesting. A naive cheater may still use two bits to send each choice. But is it the most efficient?

        It is intuitive to note that $C$ is more likely to appear, and thus it deserves a shorter code. $A$ and $B$ are rare so we can bear with longer codes. Formally, we can compute: $h(A)=3$, $h(B)=3$, $h(C)=1$, $h(D)=2$. That suggests
        \marginnote{This encoding can be done easily using a \href{https://en.wikipedia.org/wiki/Huffman_coding}{Huffman encoder}. \href{https://planetcalc.com/2481/}{Online calculators} are available.}
        that we can encode like $A \rightarrow 000$, $B \rightarrow 001$, $C\rightarrow 1$, $D\rightarrow 01$. This is known as the Huffman encoding.
    }
    \tcblower
    Let us now verify that the Huffman encoding is indeed more efficient. To do so, we compute the \emph{average information content}, i.e. average length of binary code for sending one answer:
    \begin{align}
        \left \langle h \right \rangle = \sum_{i=A,B,C,D} p_i h(i) = \sum_{i=A,B,C,D} p_i \log_2 \frac{1}{p_i}  = 1.75~.
    \end{align}
    This is indeed shorter than that of the fixed length coding (2 binary numbers per answer). Shannon's source coding theorem says that you cannot get any better. This is why lossless compression (something like zip, rar, 7z in your computer with smaller file size than original) is possible.

    Have you found the equation above similar to something we have learned in physics?
}

\textbox{Information entropy\index{entropy: Shannon}}{
    Inspired by the above discussion, the information entropy is defined as the weighted average information content for all possible outcomes. If we have a set of events $X$, each event $i\in X$ has a probability $p_i$ to happen, then the entropy is
    \begin{align}
      H(X) = \sum_{i\in X} p_i \log_2 \frac{1}{p_i}~.
    \end{align}
    $H(X)$ is known as the Shannon entropy (Shannon 1948). Again, it has the same meaning as the average information content.
    \tcblower
    Back to physics, up to a constant, the information entropy is nothing but the Gibbs entropy:
    \begin{align}
        S = (k_B \ln 2) \times H~,
    \end{align}
    where the $p_i$ in the definition of $H$ is now restricted to physical microscopic states.
}

\section{Epilogue: Summary and What's Next}

\textbox{Further reading about the content}{
    \titem{
        \item For statistical mechanics, find a textbook to find more. For example, \href{https://www.amazon.com/Statistical-Mechanics-Third-R-Pathria/dp/0123821886}{``Statistical Mechanics''}, Pathria and Beale.
        \item For black hole entropy, read \href{https://www.amazon.com/Black-Hole-War-Stephen-Mechanics/dp/0316016411}{``The Black Hole War''}, Susskind.
        \item For information theory, read Shannon's paper: \href{http://math.harvard.edu/~ctm/home/text/others/shannon/entropy/entropy.pdf}{``A Mathematical Theory of Communication''}.
        \item For connection between information theory and statistical physics, read \href{https://arxiv.org/abs/0708.2837}{``The Physics of Information''}, Bais and Farmer.
    }
}

\textbox{What happens next?}{
    Statistical mechanics will be the next course to learn for understanding entropy, thermodynamics and much more based on the microscopic states of the system. There is another course of information physics. And definitely information theory is a corner stone of computer science and modern communication. There are many related courses in the School of Engineering.
}

\section{Exercises}

\textbox{E\wref{sec:stat-s}-1 Count the number of states in a simple system}{
    Consider gas in a box (classically without involving quantum mechanics) containing $N$ distinguishable molecules. Let us coarse-grain the state of each molecule as being in the left half and the right half of the box. Then each molecule has two states: $L$ or $R$. Compute the number of states with $M$ molecule in the left half and $N-M$ molecules in the right half. Find the entropy of such state and find the maximal entropy state when varying $M$.
}

\textbox{E\wref{sec:info-s}-1 Find the different object using a balance}{
    Given 12 balls, one and only one of them has different weight. With a balance, how to use fewest measures to make sure to find out the different one?
    \tcblower
    Hint: for the first measurement, one can calculate the information content and entropy of all possibilities and choose the measurement method with maximal entropy. Then the second measurement, etc.
}

\printindex

\end{document}


% Intended LaTeX compiler: pdflatex
\documentclass[bigger]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\author{Alasdair McAndrewCollege of Engineering and ScienceVictoria University,Melbourne, Australia}
\date{December, 2017}
\title{Lindenmayer Systems}
\hypersetup{
 pdfauthor={Alasdair McAndrewCollege of Engineering and ScienceVictoria University,Melbourne, Australia},
 pdftitle={Lindenmayer Systems},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 25.3.1 (Org mode 9.1.4)},
 pdflang={English}}
\begin{document}

\maketitle

\section*{What is a Lindenmayer system?}
\label{sec:orgfa9adf7}
\begin{itemize}
\item Designed to explore organic growth
\item Creates complex shapes from simple rules
\item For example, with the rules:
\[
  \Rule{0em}{3ex}{1.3ex}\color{blue}{0\rightarrow 1},\qquad\color{red}{1\rightarrow 01}
  \]
we have this string transformation:
\[
  \Rule{0em}{2.7ex}{1ex}01101=\color{blue}{0}\;\color{red}{1\;1}\;\color{blue}{0}\;\color{red}{1}
  \longrightarrow\color{blue}{1}\;\color{red}{01\;01}\;\color{blue}{1}\;\color{red}{01}=10101101
  \]
\item All operations take place \emph{simultaneously}
\item L-systems are \emph{term-rewriting systems}
\end{itemize}

\section*{Fractals: real\ldots{}}
\label{sec:org7e26818}
\begin{center}
\includegraphics[width=.9\linewidth]{./trees2.jpg}
\end{center}

All these lovely fractal trees in the city of Melbourne, Australia, in the winter.

\section*{Fractals are everywhere}
\label{sec:orgb534ab5}
Once you start looking, you can't stop seeing them

\begin{center}
\includegraphics[width=.9\linewidth]{./out1.jpg}
\end{center}
\begin{center}
\includegraphics[width=.9\linewidth]{./out2.jpg}
\end{center}

\section*{Fractals: manufactured\ldots{}}
\label{sec:orgddda475}
\begin{center}
\includegraphics[width=.9\linewidth]{./agop.jpg}
\end{center}

These are all examples from \emph{The Algorithmic Beauty of Plants} by
Aristid Lindenmayer and Przemysław Prusinkiewicz, available at
\url{http://algorithmicbotany.org/papers/abop/abop.pdf}

\section*{Turning strings of symbols into pictures}
\label{sec:org2810c9d}

\begin{itemize}
\item Sets of rules describe how one string of symbols will be expanded to a new string
\item Each symbol corresponds to a \emph{turtle graphics} instruction:

\begin{itemize}
\item \texttt{F}: Move forward
\item \texttt{-}: Turn left
\item \texttt{+}: Turn right
\item \texttt{[}: Memorize current position and heading
\item \texttt{]}: Move to most recently memorized position and heading
\end{itemize}
\end{itemize}

\section*{An example}
\label{sec:org8056486}

For example, this sequence of symbols:

\texttt{F[+F]F[-F]F}

has this output:

We can clearly alter the output by changing the angle of the turns,
and the length of the move forward.

In this example, the angle is 26\textdegree{}
\begin{center}
\includegraphics[width=.9\linewidth]{./l_tree1.jpg}
\end{center}

\section*{How turtle graphics works}
\label{sec:org33ef1d7}

This shows how the turtle draws a path with branches:

\begin{center}
\includegraphics[width=.9\linewidth]{./growth.jpg}
\end{center}

\section*{More on turtle graphics}
\label{sec:orgcd1c995}

It's all done from the point of view of the turtle.  A side of \emph{Koch's
snowflake} can be computed by the rules:

\begin{itemize}
\item Start: \texttt{F}
\item Modify: \texttt{F} \(\rightarrow\) \texttt{F+F-{}-F+F} (with turns of 60\textdegree{})
\item At every further step, each \texttt{F} is replaced by the string \texttt{F+F-{}-F+F}
\item The second iteration produces

\texttt{F+F-{}-F+F+F+F-{}-F+F-{}-F+F-{}-F+F+F+F-{}-F+F}
\item The third iteration produces:

\texttt{F+F-{}-F+F+F+F-{}-F+F-{}-F+F-{}-F+F+F+F-{}-F+F+F+F-{}-F+F+F+F-{}-F+F-{}-F+F-{}-F+F+F+F-{}-F+F-{}-F+F-{}-F+F+F+F-{}-F+F-{}-F+F-{}-F+F+F+F-{}-F+F+F+F-{}-F+F+F+F-{}-F+F-{}-F+F-{}-F+F+F+F-{}-F+F}
\item and so on\ldots{}
\end{itemize}

\section*{Turtle graphics \emph{with pictures!}}
\label{sec:org4335b0d}

First iteration:~

Second iteration:~

Third iteration:~

Fourth iteration:~
\begin{center}
\includegraphics[width=.9\linewidth]{./koch_snowflake.jpg}
\end{center}

\section*{Some mathematics}
\label{sec:orge5165d4}

Remember the \texttt{F} \(\rightarrow\) \texttt{F+F-{}-F+F} iteration?  How many symbols
are in the \(n^{\text{th}}\) string?

Let \(f_n\) be the number of \texttt{F}'s, and \(k_n\) be the number of other
symbols in the \(n^{\text{th}}\) string. We have:

\begin{eqnarray*}
f_{n+1}&=&4f_n,\quad f_1=4\\
k_{n+1}&=&4f_n+k_{n-1},\quad k_1=4
\end{eqnarray*}

It follows immediately that
\[
f_n=4^n\mbox{ and }k_n=4+4^2+4^3+\cdots+4^n=\frac{4}{3}(4^n-1).
\]
The total length is thus
\[
f_n+k_n=4^n+\frac{4}{3}(4^n-1)=\frac{1}{3}(7(4^n)-4).
\]

\section*{The fractal plant in modern languages: Python}
\label{sec:orgcd8cc98}

\begin{verbatim}
import turtle as t  # "turtle" is a turtle graphics module

# Lindenmayer system (a) from ABOP figure 1.24(a), p 25
def edgetree(level, size, angle):
    if (level==0):
	t.fd(size)
    else:
	edgetree(level-1, size/3, angle)
	t.lt(angle)
	edgetree(level-1, size/3, angle)
	t.bk(size/3)
	t.rt(angle)
	edgetree(level-1, size/3, angle)
	t.rt(angle)
	edgetree(level-1, size/3, angle)
	t.bk(size/3)
	t.lt(angle)
	edgetree(level-1, size/3, angle)
\end{verbatim}

\section*{The fractal plant in modern languages: Racket}
\label{sec:orgdd489e9}

Racket is a modern lisp; descended from Scheme.
\begin{verbatim}
;;   F -> F[+F]F[-F]F

(require furtle) ;; furtle is a simple but fast turtle graphics library
(: ltree_b (-> Real Real Real TurtleF))  ;; typed Racket so must declare types
(define (ltree level size angle)
  (if (= level 0)
      (turtles (forward size))
      (turtles (ltree (- level 1) (/ size 3) angle)               ; F
	       (save)                                             ; [
	       (left angle) (ltree (- level 1) (/ size 3) angle)  ; +F
	       (restore)                                          ; ]
	       (ltree (- level 1) (/ size 3) angle)               ; F
	       (save)                                             ; [
	       (right angle) (ltree (- level 1) (/ size 3) angle) ; -F
	       (restore)                                          ; ]
	       (ltree (- level 1) (/ size 3) angle))))            ; F
\end{verbatim}

\section*{Some more mathematics}
\label{sec:orgf84faee}

\emph{Fractal dimension} can be defined by the "box-counting measure":

Suppose our picture is subdivided into boxes of size \(b\), and \(N(b)\) boxes
are needed to cover the shape.  Its dimension can be defined as
\[
\lim_{b\to 0}\frac{\log(N(b))}{\log(1/b)}.
\]
For example, take a curve of length \(k\). As \(b\to 0\), we would find that
\[
N(b)\to \frac{k}{b}.
\]
Thus
\[
\lim_{b\to 0}\frac{\log(N(b))}{\log(1/b)}=\lim_{b\to 0}\frac{\log(k/b)}{\log(1/b)}
=\lim_{b\to 0}1-\frac{\log(k)}{\log(b)}=1.
\]
In general a fractal will have a non-integer dimension between 1 and 2.

\section*{The end}
\label{sec:orgd7afff7}

Thanks, folks!
\end{document}


%% Based on a TeXnicCenter-Template by Gyorgy SZEIDL.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%------------------------------------------------------------
%
\documentclass{article}%
%Options -- Point size:  10pt (default), 11pt, 12pt
%        -- Paper size:  letterpaper (default), a4paper, a5paper, b5paper
%                        legalpaper, executivepaper
%        -- Orientation  (portrait is the default)
%                        landscape
%        -- Print size:  oneside (default), twoside
%        -- Quality      final(default), draft
%        -- Title page   notitlepage, titlepage(default)
%        -- Columns      onecolumn(default), twocolumn
%        -- Equation numbering (equation numbers on the right is the default)
%                        leqno
%        -- Displayed equations (centered is the default)
%                        fleqn (equations start at the same distance from the right side)
%        -- Open bibliography style (closed is the default)
%                        openbib
% For instance the command
%           \documentclass[a4paper,12pt,leqno]{article}
% ensures that the paper size is a4, the fonts are typeset at the size 12p
% and the equation numbers are on the left side
%
\usepackage{amsmath}%
\usepackage{amsfonts}%
\usepackage{amssymb}%
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{float}
\usepackage[utf8]{inputenc}
\usepackage{longtable}
\usetikzlibrary {positioning}

%-------------------------------------------
\newtheorem{theorem}{Theorem}
\newtheorem{acknowledgement}[theorem]{Acknowledgement}
\newtheorem{algorithm}[theorem]{Algorithm}
\newtheorem{axiom}[theorem]{Axiom}
\newtheorem{case}[theorem]{Case}
\newtheorem{claim}[theorem]{Claim}
\newtheorem{conclusion}[theorem]{Conclusion}
\newtheorem{condition}[theorem]{Condition}
\newtheorem{conjecture}[theorem]{Conjecture}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{criterion}[theorem]{Criterion}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\newtheorem{exercise}[theorem]{Exercise}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{notation}[theorem]{Notation}
\newtheorem{problem}[theorem]{Problem}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{remark}[theorem]{Remark}
\newtheorem{solution}[theorem]{Solution}
\newtheorem{summary}[theorem]{Summary}
\newenvironment{proof}[1][Proof]{\textbf{#1.} }{\ \rule{0.5em}{0.5em}}
\def\labelitemi{}

\begin{document}

\title{Science of Information, Statistics, and Learning Lecture $15$} %Change lecture number here
\author{Parmanand Khajuriya 12D070041, Bankuru Dharma Teja 130050049,\\
 Jay Mardia13D070011, Sudipto Mitra 130100014,\\
 Sachin Garg 13D070061
}
\date{Mar 17,2017}
\maketitle

\begin{abstract}
In the previous lecture, we studied linear regression and spoke about the need to choose a hypothesis class, and given a hypothesis class, to choose an underlying model for generating our data which could motivate the error measure we use to pick the best hypothesis from out hypothesis class. Specifically, we saw that Gaussian measurement noise coupled with wanting to maximize likelihood lead to a square-error. In this lecture, we study the binary classification problem and relate the quantities that arise to Physics and Information Theory. In doing so, we end up studying the single neuron as a classifier.

Most of today's lecture is based on Ch-39 from "Information Theory, Inference, and Learning Algorithms" by David J.C. MacKay
\end{abstract}

\section*{Introduction}
Suppose we are given a bunch of points in an n-dimensional feature space ($\mathbb{R}^n$) with each point labelled by a {Yes, No} or {0,1}.

\begin{figure}[H]
	\centering
		\includegraphics[scale=0.37]{BinaryClassification.png}
   	\caption{A bunch of data points with labels of {Yes,No} represented by [Plus,Minus]}
\end{figure}

\pagebreak
\section{Problem Formulation}
\textbf{Input:}
\newline
$x_1 \in \mathbb{R}^n, t_1 \in \{Yes,No\}$
\newline
$x_2 \in \mathbb{R}^n, t_2 \in \{Yes,No\}$
\newline
$x_3 \in \mathbb{R}^n, t_3 \in \{Yes,No\}$
\newline
.
\newline
.
\newline
$x_N \in \mathbb{R}^n, t_N \in \{Yes,No\}$
\\ \\
\textbf{Desired Output:}
\newline
A procedure $l: \mathbb{R}^n \rightarrow [0,1]$ such that $l$ does a \textit{good job} classifying data points. What we meant mean by good job will depend on the error measure we use which will depend on what our model for generating the data is.

The things we want to keep in mind are:
\begin{itemize}
\item We want to reduce some measure or error. Some proposed measures were squar-distance, number-of-misclassifications.
\item We want to fix a hypothesis class that is big but not too big. Suppose out hypothesis class allowed many many functions. We could choose a function that is 'Yes' on the 'Yes' samples and no everywhere else. This would give zero error, but would not be a good predictor. Hence we want a restricted hypothesis class that reflects our assumptions on how the data must have been generated.
\item A good idea would be to use a linear separator. Specifically, a hyperplane in $\textbf{R}^n$ that can separate the two types of points. This is demonstrated in the image.
\item Some issues with this could be that the data points may inherently be such that they cannot be separated by a line (hyperplane).
\item Our procedure should be robust to a few misclassifications of our given data. It should be able to discount outliers.
\end{itemize}

\begin{figure}[H]
	\centering
		\includegraphics[scale=0.37]{Classified.png}
   	\caption{A separating line that can help classify our data}
\end{figure}

\pagebreak
\section{Logistic Regression (Single Neuron Classifier)}
We restrict our hypothesis class to linear separators (hyperplanes) and, given an input $x \in \mathbb{R}^n$, compute $a = \sum_{i=0}^{n}w_ix^i$.
Here the vector $w \in \mathbb{R}^n$ is our classifier and $x^i$ is the i-th component of the vector $x$.

The quantity $a$ is then sent through a function whose output we use to classify the input $x$.
\\
Some candidate functions are:
\begin{enumerate}
\item $y(a) = a$
\item $y(a) = \frac{1}{1+e^{-a}}$
\item $y(a) = sign(a)$
\end{enumerate}

Pictorially, this process can be represented as a neuron in the following manner.

\begin{figure}[H]
	\centering
		\includegraphics[scale=0.56]{SingleNeuron.PNG}
   	\caption{A single neuron}
\end{figure}

Why do people choose $y(a) = \frac{1}{1+e^{-a}}$?
This function, called the logistic function, has the disadvantage that it is hard to compute digitally. It's advantage is that it is analytically nice and smooth.
\\
But the key reason is that using it allows us a nice interpretation of the neuron via physics.
\\ \\
\textbf{Physics Interpretation to the Logistic function:}
Suppose there is a 2-state neuron. The state "Yes" has energy E and the state "No" has energy 0.
If $E(Yes) =-\sum_{i=0}^nw_ix^i$ and E(No) = 0.
\\ \\
Then from thermodynamics we would expect $P(Yes|E) \propto e^a$ and $P(No|E) \propto e^0$.
\\ \\
This would give us $Pr(Yes) = \frac{e^a}{1+e^a} = \frac{1}{1+e^{-a}}$.
Hence we can interpret this as the energy barrier for neurons firing. The probability of the neuron firing changes with change in $a$.
\\ \\
Of course, the actual neuron in the brain is much more complicated and this is merely a very simplified toy model that is nevertheless useful in providing intuition when we consider many neurons together.

\section{Error Model}
We want to choose the weights $w$ that minimize the error according to some error model. We now present an error model that can be interpreted both information-theoretically as well as statistically.

$$G(w) = -\sum_{i=1}^N[t_ilog(y(x_i;w))+(1-t_i)log(1-y(x_i;w))]$$

This can be interpreted as the total expected surprise from the data given our prior belief $w$. Hence minimizing this quantity seems reasonable. This is the information theoretic interpretation.

This quantity can also be seen as the negative log-likelihood of the data. Hence minimizing it is the same as maximizing the likelihood.
\\ \\
As a function of $w$, $G(w)$ need not be convex. However, the easiest thing we can do is gradient-descent and obtain a local optima. We shall see that gradient descent for this particular error model has a nice interpretation.

\textbf{Gradient Descent:}
$$\frac{\partial G}{\partial w_j} = -\sum_{i=1}^N[(t_i-y_i)x_i^j]$$
The error decreases in the direction of the negative of the gradient.
Hence we pick a learning rate and move in the direction opposite to the gradient in w-space.
\\ \\
This is called Hebbian Learning. It has the nice interpretation that if some input coordinate (say $x^j$) was right and we did not listen to it, it's contribution in the weights is increased ($w_j$ increases). Similarly if some input is wrong but we gave it large weight, we reduce its contribution.

\textbf{Remarks on Hebbian Learning:}
\begin{enumerate}
\item If the data points are linearly separable, after some time, Hebbian learning does not converge but diverges and all the weights go to $\infty$. This means the sigmoids become sharper and sharper but this may not lead to any actual decrease in prediction error. Hence this is false refining.
\item People counter this in different ways. Some just stop the algorithm after a while, even though it has not converged. This is called early stopping.
\item Some add a regularizing term ($\lambda ||w||^2$) corresponding to the weight of $w$ to the error function being optimized. This means that weights of $w$ won't increase too much unless there is a significant gain associated with it.
\end{enumerate}

\end{document}


\chapter{Lecture 28 Jul 09th 2018}%
\label{chp:lecture_28_jul_09th_2018}
% chapter lecture_28_jul_09th_2018

\section{Polynomial Ring (Continued 1)}%
\label{sec:polynomial_ring_continued_1}
% section polynomial_ring_continued

\subsection{Factorization of Polynomials (Continued)}%
\label{sub:factorization_of_polynomials_continued}
% subsection factorization_of_polynomials_continued

Since the actual focus of our study right now is really fields instead of just integral domains, we shall use fields in place of integral domains or commutative rings from here on unless explicitly stated otherwise. So we redefine \cref{defn:division_of_polynomials} as follows:

\begin{defnnonum}[Division of Polynomials]
  Let $F$ be a field and consider $F[x]$. For $f(x), g(x) \in F[x]$, we say that $f(x) \, | \, g(x)$ if $\exists q(x) \in F[x]$ such that
  \begin{equation*}
    g(x) = q(x) f(x).
  \end{equation*}
\end{defnnonum}

and restate the last stated proposition as follows:

\begin{propo}[$f(x) \, | \, g(x) \, \land \, g(x) \, | \, f(x) \implies f(x) = g(x)$]
\label{propo:f_div_g_and_g_div_f_implies_f_is_g}
Let $F$ be a field and $f(x), g(x) \in F[x]$ be monic polynomials\sidenote{Note that polynomials being monic is analogous to integers being positive. For example, you (read: I) should try to reiterate the proof below by replacing the monic property with positive integers.}. If $f(x) \, | \, g(x)$ and $g(x) \, | \, f(x)$, then $f(x) = g(x)$.
\end{propo}

\begin{proof}
  Since $f(x) \, | \, g(x)$ and $g(x) \, | \, f(x)$, $\exists r(x), s(x) \in F[x]$ such that
  \begin{equation*}
    g(x) = r(x) f(x) \text{ and } f(x) = s(x) g(x).
  \end{equation*}
  Then
  \begin{equation*}
    f(x) = s(x) r(x) f(x).
  \end{equation*}
  By \cref{propo:polynomial_ring_is_an_integral_domain}, we have that
  \begin{equation*}
    \deg f = \deg s + \deg r + \deg f
  \end{equation*}
  and so
  \begin{equation*}
    \deg s + \deg r = 0 \implies \deg s = \deg r = 0 \quad \because \deg s, \, \deg r \geq 0.
  \end{equation*}
  And so $\exists t \in F$ such that $f(x) = tg(x)$. Since $f(x)$ and $g(x)$ are monic, we must have $t = 1$ and so $f(x) = g(x)$.\qed
\end{proof}

\begin{propo}[Division Algorithm for Polynomials]
\index{Division Algorithm}
\label{propo:division_algorithm_for_polynomials}
  Let $F$ be a field, and $f(x), g(x) \in F[x]$ with $f(x) \neq 0$. Then $\exists ! q(x), \, r(x) \in F[x]$ such that
  \begin{equation*}
    g(x) = q(x) f(x) + r(x)
  \end{equation*}
  with $\deg r < \deg f$.\sidenote{Note that this includes the case for $r = 0$, and this is yet another reason why we defined $\deg 0 = -\infty$.}
\end{propo}

\begin{proof}
  We shall first prove the existence of such a $q(x)$ and $r(x)$. For simplicity, write
  \begin{equation*}
    \deg f = m \text{ and } \deg g = n.
  \end{equation*}
  If $n < m$, then
  \begin{equation*}
    g(x) = 0 f(x) + g(x)
  \end{equation*}
  and we are done. Suppose that $n \geq m$ and proceed by induction of $n$. Write
  \begin{gather*}
    f(x) = a_0 + a_1 x + \hdots + a_m x^m \\
    g(x) = b_0 + b_1 x + \hdots + b_n x^n.
  \end{gather*}
  Consider\sidenote{We are implicitly using the fact that $x \in Z[x]$.}
  \begin{align*}
    g_1 (x) &= g(x) - b_n a_m^{-1} x^{n - m} f(x) \\
            &= (b_n x^n + \hdots + b_0) - b_n a_m^{-1} x^{n - m} ( a_m x^m + \hdots a_0 ) \\
            &= 0 x^n + (b_{n - 1} - b_n a_m^{-1} a_{m - 1}) x^{n - 1} + \hdots,
  \end{align*}
  thus either $g_1 (x) = 0$ or $g_1(x) \neq 0$, but in any case, $\deg g_1 < n$.

  \noindent \textbf{Case 1}: $g_1(x) = 0$. In this case, we have
  \begin{equation*}
    g(x) = b_n a_m^{-1} x^{n - m} f(x)
  \end{equation*}
  and so we can pick
  \begin{gather*}
    q(x) = b_n a_m^{-1} x^{n - m} \\
    r(x) = 0,
  \end{gather*}
  and the result follows.

  \noindent \textbf{Case 2}: $g_1(x) \neq 0$. By induction, we can find some $q_1 (x), r_1(x) \in F[x]$ such that
  \begin{equation*}
    g_1(x) = q_1(x) f(x) + r_1(x)
  \end{equation*}
  with $\deg r_1 < \deg f$. It follows that
  \begin{align*}
    g(x) &= g_1(x) + b_n a_m^{-1} x^{n - m} f(x) \\
         &= q_1(x) f(x) + r_1(x) + b_n a_m^{-1} x^{n - m} f(x).
  \end{align*}
  So pick
  \begin{gather*}
    q(x) = q_1 (x) + b_n a_m^{-1} x^{n - m} \\
    r(x) = r_1 (x) < \deg f,
  \end{gather*}
  and so the result follows.

  To prove uniqueness, suppose we have
  \begin{equation*}
    q_1(x) f(x) + r_1(x) = q_2(x) f(x) + r_2(x)
  \end{equation*}
  with $\deg r_1, \deg r_2 < \deg f$. Then
  \begin{equation*}
    r_2(x) - r_1(x) = [ q_1(x) - q_2(x) ] f(x).
  \end{equation*}
  If $q_1(x) - q_2(x) \neq 0$, then
  \begin{equation*}
    \deg (r_2 - r_1) = \deg (q_1 - q_2) + \deg f \geq \deg f
  \end{equation*}
  which is a contradiction since $\deg (r_2 - r_1) < \deg f$. Thus we must have $q_1(x) = q_2(x)$ and so $r_1 (x) = r_2(x)$.\qed
\end{proof}

\begin{propo}[Properties of the Greatest Common Divisor]\index{Greatest common divisor}
\label{propo:properties_of_the_greatest_common_divisor}
Let $F$ be a field and $f(x), \, g(x) \in F[x]$ with $f(x) \neq 0 \neq g(x)$. Then $\exists ! d(x) \in F[x]$ such that
\begin{enumerate}
  \item $d(x)$ is monic;
  \item $d(x) \, | \, f(x)$ and $d(x) \, | \, g(x)$;
  \item $e(x) \, | \, f(x) \, \land \, e(x) \, | \, g(x) \implies e(x) \, | \, d(x)$;
  \item $\exists u(x), v(x) \in F[x] \quad d(x) = u(x) f(x) + v(x) g(x)$
\end{enumerate}
In this case, we say that $d(x)$ is the \hlnoteb{greatest common divisor} of $f(x)$ and $g(x)$, and denote this by $d(x) = \gcd[ f(x), g(x) ]$.
\end{propo}

\begin{proof}
  Consider the set
  \begin{equation*}
    X = \{ u(x) f(x) + v(x) g(x) : u(x), \, v(x) \in F[x] \}.
  \end{equation*}
  Since $f(x) = 1 \cdot f(x) + 0 \cdot g(x) \in X$, the set $X$ contains non-zero polynomial and thus contains monic polynomials (since $F$ is a field\sidenote{This is cause if we have
  \begin{equation*}
    f(x) = a_m x^m + \hdots + a_0
  \end{equation*}
  Then
  \begin{equation*}
    a_m^{-1} f(x) = x^m + \hdots + a_m^{-1} a_0
  \end{equation*}
  is a moic polynomial in $F[x]$.}). Among all of the monic polynomials, choose
  \begin{equation*}
    d(x) = u(x) f(x) + v(x) g(x)
  \end{equation*}
  to have minimal degree. Then we get $(1)$ and $(4)$ in the bag automatically so. $(3)$ also follows almost immediately, since
  \begin{align*}
    &e(x) \, | \, f(x) \, \land \, e(x) \, | \, g(x) \\
    &\implies \exists a(x), b(x) \in F[x] \quad f(x) = a(x) e(x) \, \land \, g(x) = b(x) e(x) \\
    &\implies d(x) = u(x) f(x) + v(x) = [ u(x) a(x) + v(x) b(x) ] e(x) \\
    &\implies e(x) \, | \, d(x).
  \end{align*}
  It remains to prove $(2)$. By \cref{propo:division_algorithm_for_polynomials}, we have that $\exists q(x), r(x) \in F[x]$ with $\deg r < \deg f$ such that
  \begin{equation*}
    f(x) = q(x) d(x) + r(x).
  \end{equation*}
  Then\marginnote{
  \begin{ex}
    Reiterate this proof for integers, by removing the `$(x)$' and replacing instances of monic polynomials with positive integers.
  \end{ex}
  }
  \begin{align*}
    r(x) &= f(x) - q(x) d(x) = f(x) - q(x) [ u(x) f(x) + v(x) g(x) ] \\
         &= [ 1 - q(x) u(x) ] f(x) - q(x) v(x) g(x).
  \end{align*}
  Note that if $r(x) \neq 0$, then write $k \neq 0 \in F$ as the leading coefficient of $r(x)$. Since $F$ is a field, we have that $\exists k^{-1} \in F$, and so $k^{-1} r(x)$ is a monic polynomial of $X$ with $\deg( k^{-1} r ) < \deg d$, which contradicts the fact that the degree of $d(x)$ is minimal. Thus $r(x) = 0$ and $d(x) \, | \, f(x)$. Using a similar argument, we can show that $d(x) \, | \, g(x)$. Therefore, $(2)$ follows.\qed
\end{proof}

% subsection factorization_of_polynomials_continued (end)

% section polynomial_ring_continued_1 (end)

% chapter lecture_28_jul_09th_2018 (end)