Showing posts with label covariance of a matrix. Show all posts
Showing posts with label covariance of a matrix. Show all posts

## How do you find the covariance of a matrix?

[1] "SNP"
> chartSeries(Cl(SNP))

> ret <- dailyReturn(Cl(SNP), type='log')
> par(mfrow=c(2,2))
> acf(ret, main="Return ACF");
> pacf(ret, main="Return PACF");
> acf(ret^2, main="Squared return ACF");
> pacf(ret^2, main="Squared return PACF")
> par(mfrow=c(1,1))
> m=mean(ret);s=sd(ret);
> par(mfrow=c(1,2))

> hist(ret, nclass=40, freq=FALSE, main='Return histogram');curve(dnorm(x,
+              mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")

> plot(density(ret), main='Return empirical distribution');curve(dnorm(x,
+                 mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")

> par(mfrow=c(1,1))
> library("moments", lib.loc="~/R/win-library/3.6")
> kurtosis(ret)
daily.returns
9.313108
> plot(density(ret), main='Return EDF - upper tail', xlim = c(0.1, 0.2),
+      ylim=c(0,2));

> curve(dnorm(x, mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
> plot(density(ret), xlim=c(-5*s,5*s),log='y', main='Density on log-scale')
> curve(dnorm(x, mean=m,sd=s), from=-5*s, to=5*s, log="y", add=TRUE,
+       col="red")

> qqnorm(ret);qqline(ret);

> library("rugarch", lib.loc="~/R/win-library/3.6")

Attaching package: ‘rugarch’

The following object is masked from ‘package:stats’:

sigma

> chartSeries(ret)
> garch11.spec = ugarchspec(variance.model = list(model="sGARCH",
+             garchOrder=c(1,1)), mean.model = list(armaOrder=c(0,0)))
> snp.garch11.fit = ugarchfit(spec=garch11.spec, data=ret)
> coef(snp.garch11.fit)
mu        omega       alpha1        beta1
1.773421e-04 5.540422e-06 5.524744e-02 9.319838e-01

### Covariance of a matrix garch

> vcov(snp.garch11.fit)
[,1]          [,2]          [,3]
[1,]  8.196197e-08  3.072906e-11  3.293773e-08
[2,]  3.072906e-11  7.413453e-12  4.300375e-09
[3,]  3.293773e-08  4.300375e-09  3.436546e-05
[4,] -9.782812e-08 -2.004214e-08 -3.852019e-05
[,4]
[1,] -9.782812e-08
[2,] -2.004214e-08
[3,] -3.852019e-05
[4,]  7.893676e-05

#### Common information criteria list

infocriteria(snp.garch11.fit)

Akaike       -4.987786
Bayes        -4.981419
Shibata      -4.987788
Hannan-Quinn -4.985528
calculate the news impact curve
newsimpact(snp.garch11.fit)
\$zy
[1] 0.0053822011 0.0051833306 0.0049885186
[4] 0.0047977653 0.0046110705 0.0044284344
[7] 0.0042498568 0.0040753377 0.0039048773
[10] 0.0037384755 0.0035761322 0.0034178475
[13] 0.0032636214 0.0031134539 0.0029673449
[16] 0.0028252946 0.0026873028 0.0025533696
[19] 0.0024234950 0.0022976789 0.0021759215
[22] 0.0020582226 0.0019445823 0.0018350006
[25] 0.0017294775 0.0016280130 0.0015306070
[28] 0.0014372596 0.0013479708 0.0012627406
[31] 0.0011815690 0.0011044559 0.0010314015
[34] 0.0009624056 0.0008974683 0.0008365895
[37] 0.0007797694 0.0007270078 0.0006783048
[40] 0.0006336604 0.0005930746 0.0005565474
[43] 0.0005240787 0.0004956687 0.0004713172
[46] 0.0004510243 0.0004347899 0.0004226142
[49] 0.0004144970 0.0004104385 0.0004104385
[52] 0.0004144970 0.0004226142 0.0004347899
[55] 0.0004510243 0.0004713172 0.0004956687
[58] 0.0005240787 0.0005565474 0.0005930746
[61] 0.0006336604 0.0006783048 0.0007270078
[64] 0.0007797694 0.0008365895 0.0008974683
[67] 0.0009624056 0.0010314015 0.0011044559
[70] 0.0011815690 0.0012627406 0.0013479708
[73] 0.0014372596 0.0015306070 0.0016280130
[76] 0.0017294775 0.0018350006 0.0019445823
[79] 0.0020582226 0.0021759215 0.0022976789
[82] 0.0024234950 0.0025533696 0.0026873028
[85] 0.0028252946 0.0029673449 0.0031134539
[88] 0.0032636214 0.0034178475 0.0035761322
[91] 0.0037384755 0.0039048773 0.0040753377
[94] 0.0042498568 0.0044284344 0.0046110705
[97] 0.0047977653 0.0049885186 0.0051833306
[100] 0.0053822011

\$zx
[1] -0.300000000 -0.293939394 -0.287878788
[4] -0.281818182 -0.275757576 -0.269696970
[7] -0.263636364 -0.257575758 -0.251515152
[10] -0.245454545 -0.239393939 -0.233333333
[13] -0.227272727 -0.221212121 -0.215151515
[16] -0.209090909 -0.203030303 -0.196969697
[19] -0.190909091 -0.184848485 -0.178787879
[22] -0.172727273 -0.166666667 -0.160606061
[25] -0.154545455 -0.148484848 -0.142424242
[28] -0.136363636 -0.130303030 -0.124242424
[31] -0.118181818 -0.112121212 -0.106060606
[34] -0.100000000 -0.093939394 -0.087878788
[37] -0.081818182 -0.075757576 -0.069696970
[40] -0.063636364 -0.057575758 -0.051515152
[43] -0.045454545 -0.039393939 -0.033333333
[46] -0.027272727 -0.021212121 -0.015151515
[49] -0.009090909 -0.003030303  0.003030303
[52]  0.009090909  0.015151515  0.021212121
[55]  0.027272727  0.033333333  0.039393939
[58]  0.045454545  0.051515152  0.057575758
[61]  0.063636364  0.069696970  0.075757576
[64]  0.081818182  0.087878788  0.093939394
[67]  0.100000000  0.106060606  0.112121212
[70]  0.118181818  0.124242424  0.130303030
[73]  0.136363636  0.142424242  0.148484848
[76]  0.154545455  0.160606061  0.166666667
[79]  0.172727273  0.178787879  0.184848485
[82]  0.190909091  0.196969697  0.203030303
[85]  0.209090909  0.215151515  0.221212121
[88]  0.227272727  0.233333333  0.239393939
[91]  0.245454545  0.251515152  0.257575758
[94]  0.263636364  0.269696970  0.275757576
[97]  0.281818182  0.287878788  0.293939394
[100]  0.300000000

\$yexpr
expression(sigma[t]^2)

\$xexpr
expression(epsilon[t - 1])

#### Engle - Ng sign bias test

signbias(snp.garch11.fit)
t-value                        prob sig
Sign Bias                   0.75012749               0.4532227
Negative Sign Bias   0.36573746                0.7145806
Positive Sign Bias    0.02449422               0.9804596
Joint Effect               1.72016577              0.6324598
obtain the fitted data series

#### How do you calculate the residuals?

residuals(snp.garch11.fit)
2007-12-05  6.979400e-02
2007-12-06  4.663104e-02
2007-12-07 -4.291163e-02
2007-12-10 -1.899338e-02
2007-12-11 -1.708279e-02
2007-12-12  7.185605e-03
2007-12-13 -6.113582e-02
2007-12-14 -2.939426e-02
2007-12-17 -6.152846e-02
2007-12-18  5.920000e-02
2007-12-19  4.398681e-03
2007-12-20  2.088195e-02
[ reached getOption("max.print") -- omitted 2946 rows ]
unconditional (long-run) variance

uncvariance(snp.garch11.fit)
[1] 0.0004339032
unconditional (long-run) mean
> uncmean(snp.garch11.fit)
[1] 0.0001773421
The concept of news impact curves
It is  useful tools to visualize the magnitude of volatility changes in response to shocks
plot(ni.garch11\$zx, ni.garch11\$zy, type="l", lwd=2, col="blue", main="GARCH(1,1) - News Impact", ylab=ni.garch11\$yexpr, xlab=ni. garch11\$xexpr)

#### Forecasting the conditional volatility based on that is just one step

snp.garch11.fit = ugarchfit(spec=garch11.spec, data=ret, out.sample=20)