# Game of Thrones.

Surely you're wondering how doesÂ Mathematics fit inÂ Game of Thrones? Well I confess, I could use any other TV series to tell you what follows in this post, but honestly ... what is best than Game of Thrones (GoT)?

The connection to Mathematics is not so much considered in the series but in the type of data used. In particular one kind of data that has been used is the set of the comments regardingÂ GoT posted on the IMDb site reviewsÂ to produce this infographic, on whichÂ I worked with my brother.

# Infographic.

Firstly, very briefly, let us see what types of information were extracted from various comments (671).

The map on the left shows the major lineages; each one has been placed in itsÂ city of reference, indicating:

- The number of reviews in which the single lineage is appointed;
- The number of reviews that speak positively of the family;
- The number of reviews that speak negatively of the family;
- The member of the most cited lineage;
- The not mentioned member of the family.

Instead, in the chart on the right hand side appear:

- the performance of the nominated characters from each house in five seasons of the series;
- the most cited characters that are not attributable to lineages;
- the graphs of co-occurrences between characters and between lineages.

The part on the infographics which, however, I would like to focus attention is the oneÂ related to the word-cloud in the bottom right side of the picture, where the adjectives that have been mainlyÂ used in the reviews of a lineage are reported.

It needs to beÂ explained how to determine whether a word in a speech has to be regarded as an adjective rather than as a preposition or article. And it is here that Mathematics comes to help.

To perform this type of analysis a **POS (Part of Speech) Tagger**Â has been used, namely a tool able to make grammatical analysis of a text. The POS Tagger taken into accountÂ is basedÂ onÂ OpenNLP library, which is essentially basedÂ on the **Maximum Entropy model** that we will analyze in detail.

# Information Theory and Entropy.

Before examining the MaxEnt algorithm,Â I would define the concept of entropy used here.

Most commonly we talk about entropy in the following areas:

**thermodynamics**: the entropy of a gas is function of its temperature, volume,Â mass andÂ nature; it is a reversibility index: ifÂ there is no change, the process is reversible, otherwise it is irreversible (just think of the exchange of heat between two bodies, one hot and one cold);**statistical mechanics**: the entropy is a measure ofÂ the increase of disorder (inability to predict the position and velocity of the particles); the greater is the knowledge, the lower the entropy, and viceversa.

To these interpretations of entropy, one can beÂ added and it plays a very important role in information theory (especially in the field of signal processing) and will be the way in which we understand it in our case.

Let's consider a source of messages . The amount of information transmitted from the message increases with the increase of the uncertainty of the product message.Â The greater our knowledge about the message produced by the source, the lower the uncertainty, theÂ entropy and the transmitted information.

We formulate the concept of entropy, as introduced by **Claude Shannon** in 1948.

**Definition 1**

Let be a source and a signal emitted by . The information given by is called ** autoinformation** and is defined as

whereÂ is the probabilityÂ that the event happens.

**Definition 2**

The entropy of a source is the expected value of the autoinformation, i.e. the average information contained in any signal from ,Â in particular

ifÂ is a discrete variable

ifÂ is a continuousÂ variable

**Proposition 1**

LetÂ be a discrete source,Â then

In particularÂ the maximumÂ of theÂ entropy isÂ when all theÂ eventsÂ are equally probable.

Let's see a simple example.

**ExampleÂ 1**

Suppose to have a sourceÂ thatÂ emits with probability and Â with probabilityÂ .

Then

and the entropy is equal toÂ ifÂ (*unpredictable sources*), while it is equal toÂ ifÂ (*predictable sources*, i.e. itÂ always outputs or always ).

# The MaxEnt Algorithm.

## Introduction.

The classifier Maximum Entropy is a discriminative classifier widely used in the areas of *Natural Language Processing*, *Speech Recognition* and *Information Retrieval.*Â In particular, it is used in order to solve problems of classification of text such as language detection, topic and sentiment analysis.

The Maximum Entropy algorithm is based on the **principle of Maximum Entropy** and selects the model that has the maximum entropy (as enunciated by Shannon) on the training set of all tested models.

Recalling Bayes' theorem (see here), the Max Entropy classifier is used when you do not have any information about the prior distribution of the data and it is not correct to make assumptions about.

Unlike the Naive Bayes classifier, the Maximum Entropy has not the hypothesis that the variables are independent of one another, which reflects the nature of the natural text where the variables into account are the words, that of course are not independent of one another since the grammatical rules of the language; moreover,Â it requires more time in training while providing more reliable results.

**ExampleÂ 2**

Before going into the theory behind the MaxEnt, consider an example which clarifies from the outset what will be said in a formal way in the following.

Suppose we want to determine the grammatical form of the word "*set*."

The word "*set*" can take the following forms:

**Adjective**: "*He has aÂ set smile*."**Noun**: "*Give me your chess set.*"**Verb**: "*They set a fast pace.*"

We collect a large number of examples from which to extract information to determine the decision-making model . The model we're going to build will assign the word "*set*" a chance to take a particular grammatical meaning.

As we don'tÂ have other information from the data, you can impose for our model:

There are severalÂ models thatÂ holdÂ previous identity, including:

By analyzing the data set further, let's suppose to get other informations, such as every time the word "*set*" is preceded by a pronounÂ is a verb. This, added to the normalization condition, changes the possible chances, reducing the possible models.

The goal of the MaxEnt algorithm is to determine the model uniform as possible (maximizing the entropy), according to the information derived from the data, without making any additional assumptions.

## The algorithm.

We pass now to the explanation of the algorithm.

Consider a text-based document and have words to each of which corresponds to a particular tag (i.e. a grammatical part of the document: noun, adjective, pronoun, article, etc.). We introduce the concept of "*history*" of the word as the possible informations arising from the context in which is located and we indicate it with .

We make a small example to explain how you can understand the "*story*" of a word.

**ExampleÂ 3**

Consider the sentence "*Today is a beautifulÂ day*". The set of words is {*Today**,* *is*, *just*, *a*,Â *beautiful*, *day*} and we call "*history*" of a word the grammatical information of the previous and next word.

For example, for the word "*beautiful*"

= {feminine singular indefinite article - "*a*", feminine singular noun - "*day*"}

What we haveÂ to define is a stochastic model that estimates the conditional probability of getting a tag, given a particular "*story*" , namelyÂ isÂ .

Then we follow the usual classification scheme, i.e. we build our model starting from couples of the training set, where is the "*story*" of the word and is the class assigned to it (the grammatical part of speech) .

Consider the probability distribution based on the sample

whereÂ is the size of the training set, while is the number of occurrences of pair in the training set.

We introduce the indicator function

and consider theÂ features as variables for the construction of our model.

The average value of variable compared to the probability derived from the sample is

where clearly whether each pair of the training set has occurrence .

While the average value of variable with respect to probability model is equal to

WeÂ impose the condition that the average value of the model is limited to on the training set, i.e.

We now have so many conditions as the previous ones for each , which can be met by severalÂ models. To choose the best conditions, we use the principle of Maximum Entropy, by selecting the closest possible model to standard form (maximization of information) .

In particular, it shows that exists and a well established model Â that maximizes the entropy of a system withÂ constraints.

In our case the problem is the following:

Determine such that

with

- .
- .
- .

We useÂ Lagrange multipliers to solve itÂ :

obtaining the solution

At this point we insert in the Lagrangian the values â€‹â€‹of and and get , maximizing the function that follows for which it is stated that (without proving it here):

- is the log-likelihood of the exponential model ;
- the maximumÂ problem cannot be resolved analytically but by numerical methods (
*conjugate gradient*,*GIS - Generalized Iterative Scaling*), taking advantage of the regularity and the convexity of the function for each .

So, asÂ made the POS Tagger training on a givenÂ set (training set), we proceed to make a classification on new words to test (test set). In our case the POS Tagger, already trained on a large enough data set, is used for the classification of words present in IMDb's review; discarded all the words that are not classified as adjectives, then weÂ will assign adjectives to families based on the frequency in the review of a specific lineage.

# Conclusions.

Now, if you're not a fan of Game of Thrones, I repeat that it can also be applied to other TV series (House of Cards, The Walking Dead, etc.) or in completely different areas, like ... what we will see in myÂ next post.

Until nowÂ there is a unique conclusion ...Â 24.04 the sixth season begins.

**Winter is coming ...**

Â