Copyright 2011, 2016, 2018 Jon Danielsson. This code is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. The GNU General Public License is available at: https://www.gnu.org/licenses/.

The original 2011 R code will not fully work on a recent R because there have been some changes to libraries. The latest version of the Matlab code only uses functions from Matlab toolboxes.

The GARCH functionality in the econometric toolbox in Matlab is trying to be too clever, but can't deliver and could well be buggy. If you want to try that, here are the docs (estimate). Besides, it can only do univariate GARCH and so can't be used in Chapter 3. Kevin Sheppard's MFE toolbox is much better, while not as user friendly, it is much better written and is certainly more comprehensive. It can be downloaded here and the documentation here is quite detailed.

Last updated June 2018

```
import numpy as np
import matplotlib.pyplot as plt
price = np.loadtxt('index.csv', delimiter = ',', skiprows = 1)
y = np.diff(np.log(price), n=1, axis=0)
plt.plot(y)
plt.show()
plt.close()
```

Last updated June 2018

```
using CSV;
price = CSV.read("index.csv", nullable = false);
y = diff(log.(price[:,1]));
using Plots;
plot(y)
```

Last updated June 2018

```
from scipy import stats
import statsmodels.api as sm
print (np.mean(y))
print (np.std(y, ddof=1))
print (np.min(y))
print (np.max(y))
print (stats.skew(y))
print (stats.kurtosis(y, fisher = False))
print (sm.tsa.stattools.acf(y, nlags=1))
print (sm.tsa.stattools.acf(np.square(y),nlags=1))
print (stats.jarque_bera(y))
from statsmodels.stats.diagnostic import acorr_ljungbox
print (acorr_ljungbox(y, lags=20))
print (acorr_ljungbox(np.square(y), lags=20))
```

Last updated June 2018

```
using StatsBase;
println(std(y))
println(minimum(y))
println(maximum(y))
println(skewness(y))
println(kurtosis(y))
println(autocor(y, 1:20))
println(autocor(y.^2, 1:20))
using HypothesisTests;
println(JarqueBeraTest(y))
println(LjungBoxTest(y,20))
println(LjungBoxTest(y.^2, 20))
```

Last updated June 2018

```
import statsmodels.api as sm
import matplotlib.pyplot as plt
q = sm.tsa.stattools.acf(y, nlags=20)
plt.bar(x = np.arange(1,len(q)), height = q[1:])
plt.show()
plt.close()
q = sm.tsa.stattools.acf(np.square(y), nlags=20)
plt.bar(x = np.arange(1,len(q)), height = q[1:])
plt.show()
plt.close()
```

Last updated June 2018

```
using Plots;
q1 = autocor(y, 1:20)
q2 = autocor(y.^2, 1:20)
plot(bar(q1), bar(q2))
```

Last updated June 2018

```
from statsmodels.graphics.gofplots import qqplot
fig1 = qqplot(y, line='q', dist = stats.norm, fit = True)
plt.show()
plt.close()
fig2 = qqplot(y, line='q', dist = stats.t, distargs=(5,), fit = True)
plt.show()
plt.close()
```

Last updated June 2018

```
using Plots, StatPlots, Distributions;
plot(qqplot(Normal, float(y), qqline =:quantile), qqplot(TDist(5), float(y), qqline = :quantile))
```

Last updated June 2018

```
import numpy as np
p = np.loadtxt('stocks.csv',delimiter=',',skiprows = 1)
y = np.diff(np.log(p), n=1, axis=0)
np.corrcoef(y, rowvar=False) # correlation matrix
## rowvar=False indicates that columns are variables
```

Last updated June 2018

```
using CSV;
price = CSV.read("stocks.csv",nullable=false)
y1 = diff(log.(price[:,1]))
y2 = diff(log.(price[:,2]))
y3 = diff(log.(price[:,3]))
y = hcat(y1,y2,y3)
println(cor(y)) # correlation matrix
```