Granger Causality Tyrion Cersei Python Data Science
By: Heiko Onnen
Tags: Cointegrated Time Series, Data Science, Deep Dives, Granger Causality, Game of Thrones

Sibling Rivalry and Cointegration in Game of Thrones

Testing for time series cointegration, Granger causality, stationarity (ADF and KPSS) and white noise residuals. With a quick helping of vector autoregression.

Process Steps:

0. Game Of Thrones and Granger Causality — What Is This About?

It is a truth universally acknowledged that a data scientist in possession of a somewhat nerdy mind must be in want of a fantasy world, for her or his after-hours immersion.

Game of Thrones (GoT) has been a choice of spending time for many of us for a decade. There must be a substantial overlap between Data Science aficionados and GoT devotees. A certain dearth of GoT-related quantitative analyses suggests a gap in need of a fix, though.

If, however, you are not a GoT enthusiast and wonder what this is all about, here the rundown:

Game of Thrones

GoT is a medieval fantasy world set on the continents of Westeros and Essos; published originally as a series of books which George R. R Martin had started to write in the 1990s; since 2010, it also streamed as an HBO series.

GoT is staging:

dragons (alive, dead, or indeterminate) and their riders;

swaggering sellswords;

undead white walkers (no relation to the AMC series), who are impervious to steel blades but shatter into icicles when touched by obsidian;

sloshed kings, and scheming courtiers with names such as Littlefinger;

grey-haired academic maesters, who would no doubt be eager to try out data science if they did not have to spend so much time on cleaning the cages of the ravens they employ as souped-up messenger pigeons;

a sly eunuch spymaster and battle-hardened eunuch soldiers;

incestuous proclivities and other familial complications;

an undead monstrous knight who has carried his brutal personality over into the not-quite-life beyond, with the assistance of an expelled Frankensteinian maester;

a red-haired fire priestress, who — for unexplained reasons — must bare her chest on-screen whenever she incantates magic spells;

a royal red wedding feast that puts any other drunken family festivity and brawl to shame.

You get the gist.

Tyrion and Cersei are among the key protagonists of this reinvented Dark Age.

Tyrion is the younger brother of the twins Cersei and Jamie, who have been entangled in an incestuous relationship since their teen years. Tyrion, born a dwarf, uses his considerable wit to fight back against mockery and humiliations, many of which Cersei instigates.

Cersei blames Tyrion for their mother’s death in childbed when giving birth to Tyrion. Cersei becomes the queen regent after helping her permanently soused husband, King Robert Baratheon, along into an early royal crypt. With this royal couple, there must have been some granger causality up for analytical grabs, but we don’t have enough data on King Robert as he quickly exits the stage left, before the beheadings begin. Cersei is manipulative, power-hungry and vengeful. She believes herself to be politically astute, but pursues shortsighted ambitions. Dragons or undead walkers are mere inconveniences to her quest for power. Her little brother Tyrion becomes the lodestone of her hatred.

There — you’ve got the 1-minute rundown.

Now, we are going to feed a small piece of this dramatic tapestry into a few selected Python algorithms and see which hard facts we can distill from it.

Granger Causality

G-causality is a probabilistic (as opposed to deterministic) approach to define and investigate cause and effect.

A variable X is said to be Granger-causing a target variable Y if past values of X contain information that helps predict Y beyond the information that is contained in past values of Y.

The null hypothesis of the GC test is that lagged values of X do not help explain the variation in the target variable Y. There is no signal in X from which predictions of Y in future periods could be derived. If we reject the null hypothesis at low p-values, we can conclude that X can contribute some predictive value: X Granger-causes Y by helping to forecast Y.

Thus, G-causality does not prove a true cause-and-effect chain.

Over a range of lags, the Granger-causality may not remain unidirectional (X Granger-causing Y), but can go in both directions.

1. Preprocessing and Data Wrangling

We download the worldwide Google Trends time series for the keywords “Tyrion” and “Cersei” from 2011 to 2020.

https://trends.google.com/trends/explore?date=today%205-y&q=tyrion,cersei

Note that Google Trends offers weekly keyword data for chosen custom time ranges of up to 5 years. Beyond 5 years, the frequency changes to monthly. To get weekly data for 2011–2020, we need to rerun the search with a custom date range starting 5 years earlier, download a separate .csv file and then merge the time series in a single file.

The Google keyword trends express the waxing or waning public interest in the lives of Cersei and Tyrion while the siblings carry out their machinations on-screen. We take the trends as metrics for their fame or ill repute, and ask if the ups and downs in the popularity of one sibling are Granger-causing the peaks and dips of the other sibling’s presence in the mind of the audience.

We create a Jupyter notebook and import the required packages. Then we load the Google Trend source data file ‘got_Cersei_Tyrion.csv’

After converting string objects to numbers and dates, we can chart Tyrion and Cersei’s data.

Between 2011 and 2020, news about Tyrion and Cersei were concentrated in certain weeks and months: whenever a new HBO season of was aired. The schedule did not quite exhibit a constant periodicity.

We must assume that Tyrion and Cersei’s sibling rivalry consisted of periods of calm, building up tension over months and then exploding in moments of extreme drama, releasing the tension via nasty remarks at the dinner table, beheadings, or similar approaches to resolving intrafamilial disputes.

How strong is the correlation of Tyrion and Cersei?

On its own, the high correlation of Tyrion and Cersei does not confirm Granger causality. Nor would a low correlation disprove cointegration or Granger causality.

Correlation does not necessarily remain stable over time, for instance if the series follow trend curves with different ascent or descent rates.

Two series with perfectly aligned peaks can exhibit a correlation of 1.0. But if one of the series experiences a small shift on the time axis so that its peaks now align with the dips of the other series, the series would cancel out each other and their correlation could drop to 0.

2. Pre-Test Diagnostics

2.1 Cointegration: A First Hint at Granger-Causality

Two series are cointegrated if each of them individually is non-stationary, but there exists a linear combination that is stationary.

The two series are not necessarily perfectly in sync (not precisely matching each other’s peaks and dips in each period), but the average distance between them remains relatively constant over time: the series will return to an equilibrium. Cointegration takes over from correlation if we look at long-run relationships.

If two series are cointegrated, there must exist Granger causality between them.

2.2 Stationarity Tests and Differencing

Granger causality requires that the time series are stationary.

The Augmented Dickey-Fuller (ADF) and the Kwiatkowski-Phillips-Schmidt-Shin (KPSS) tests are the usual choices for investigating the stationarity of a time series. However, they can return conflicting results. We will have to cross-check them to decide whether or not the time series need to be differenced.

Possible conflicts:

If ADF identifies a unit root, but KPSS finds that the series is stationary around a deterministic trend, the series is only trend-stationary. Difference it (or try to de-trend it by other transformations).

If ADF does not find a unit root, but KPSS passes a judgment of non-stationarity, the series is only difference-stationary. Difference it.

We are going to run the ADF and KPSS tests, review their results and determine the appropriate order of differencing.

After defining the tests, we check Tyrion for stationarity.

The combined tests result in a recommendation to difference Tyrion’s series - after which we will test the differenced data for stationarity.

After one order of first-differencing, Tyrion is a stationary process.

Next, we difference Cersei.

After one order of differencing, Cersei is a stationary process.

Let’s summarize and chart Tyrion and Cersei’s series, before and after differencing:

3. A Reasonable Number of Lags

If we were to choose too many lags for the Granger causality test, we would be prone to accept false negatives and detect apparent causality where there is none. We would be committing p-hacking or data dredging by performing a large number of tests until we hit causality at one of the outlying lags.

If, on the other hand, we choose too few lags, the test outcome may suffer from bias because of autocorrelations beyond the cut-off period.

Therefore, we will run vector autoregression models (VAR) for lags up to 52 weeks and select the lag that results in the lowest Akaike information criterion.

We will follow Dave Giles’ guidance: in the VAR model, we will use the source data before differencing. https://davegiles.blogspot.com/2011/04/testing-for-granger-causality.html

3.1 VAR Model Before Differencing

Let’s confirm, by using the Durban-Watson test, whether the residuals suggest that the VAR model did not fail to find a significant signal in the time series.

3.2 VAR Model After Differencing

We run the VAR model again, this time for the differenced series, and add the order of differencing to the AIC-minimizing lags the previous VAR model had returned.

Let’s confirm, using the Durban-Watson test again, whether the residuals can be interpreted as white noise.

3.3 Confirming the Cointegration

We repeat the test for cointegration, this time with the number of lags which the VAR model recommends as reasonable.

If the time series “are cointegrated, then there must be Granger causality between them — either one-way or in both directions.” (Dave Giles, 2011)

If there is no cointegration, the series may still exhibit Granger causality.

https://davegiles.blogspot.com/2011/04/testing-for-granger-causality.html

4. Granger Causality Tests

4.1 Cersei as Causal Agent

We start with Tyrion in the first column of our array; Cersei, in the second column. Thus, Cersei will be treated as the causal agent. We are going to test if Cersei’s notoriety contributes an advance signal for predicting Tyrion’s popularity a few weeks later.

We use the number of AIC-minimizing lags recommended by our VAR1 model.

Null hypothesis: Cersei’s notoriety does not granger-cause Tyrion’s popularity.

Alternative hypothesis: Cersei’s prominence does granger-cause Tyrion’s.

Output: The method applies 4 alternative tests at the chosen lags: F, Chi-square, Likelihood ratio, and Parameter-F.

F-test: applicable only if the series are cointegrated.

We will focus on the F-test.

We observe that Cersei’s p-values remain well below 0.05 and conclude that she indeed appears to Granger-cause Tyrion’s cointegrated prominence.

Let’s turn around and investigate if the Granger causality goes in both directions.

4.2 Tyrion as Causal Agent

His high p-values suggest — apart from a brief time window at a lag of 3 weeks — that Tyrion does not Granger-cause Cersei’s fame or notoriety. Cersei, true to her role as the evil queen, must be perceived as her own agent of destruction, without needing a push or shove by any of the preceding ups or downs in her little brother’s walk of fame.

4.3 Going Off the Rails: The Extended Lag Curve of Causality

We now do something that statisticians will frown upon. We are going to run causality tests over a higher number of lags than the Akaike information criterion of our VAR model recommends.

If too many lags are chosen, we may be led astray by spurious regression results and accept apparent causality where there is none. The minimum AIC is not a hard rule, though, but just a heuristic to find a plausible number of lags.

Mitigating circumstances: We have already observed Cersei’s solid Granger causality at a low number of lags. We don’t want to search for spurious causality at later lags but rather will do the opposite: Are there later lags at which Cersei’s influence on Tyrion begins to dwindle?

In Tyrion’s case, we saw only mild, if any, causality. We will observe if this pattern changes at a later lag, but we would not accept that Tyrion’s Granger causality suddenly kicks in with a time lag of 15 or 18 weeks. A fading pattern, after the final episode of a season has been aired and the audience no longer receives weekly updates on Cersei and Tyrion’s escapades, would be more plausible. Thus, Granger causality that surfaces after as many as 15 or 20 weeks would not pass the smell test, given the nature of the source data.

We choose an “implausible” maximum number: a lag of up to 20 weeks.

The high p-values at increasing lags demonstrate that Tyrion only intermittently Granger-causes brief movements in Cersei’s fame or notoriety index.

By contrast, Cersei’s low p-values are persistent. She appears to Granger-cause, by inflicting a lot of grief on her little brother, distinctive movements in the public attention Tyrion receives.