Analyzing Risk of Global Asset Allocators
#Setup packages to be used
library(readxl)
## Warning: package 'readxl' was built under R version 3.4.4
library(FRAPO)
## Warning: package 'FRAPO' was built under R version 3.4.4
## Loading required package: cccp
## Warning: package 'cccp' was built under R version 3.4.4
## Loading required package: Rglpk
## Warning: package 'Rglpk' was built under R version 3.4.4
## Loading required package: slam
## Warning: package 'slam' was built under R version 3.4.4
## Using the GLPK callable library version 4.47
## Loading required package: timeSeries
## Warning: package 'timeSeries' was built under R version 3.4.4
## Loading required package: timeDate
## Warning: package 'timeDate' was built under R version 3.4.4
## Financial Risk Modelling and Portfolio Optimisation with R (version 0.4-1)
library(zoo)
## Warning: package 'zoo' was built under R version 3.4.4
##
## Attaching package: 'zoo'
## The following object is masked from 'package:timeSeries':
##
## time<-
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(timeSeries)
library(fGarch)
## Warning: package 'fGarch' was built under R version 3.4.4
## Loading required package: fBasics
## Warning: package 'fBasics' was built under R version 3.4.4
library(forecast)
## Warning: package 'forecast' was built under R version 3.4.4
library(QRM)
## Warning: package 'QRM' was built under R version 3.4.4
## Loading required package: gsl
## Warning: package 'gsl' was built under R version 3.4.4
## Loading required package: Matrix
## Loading required package: mvtnorm
## Loading required package: numDeriv
##
## Attaching package: 'QRM'
## The following object is masked from 'package:base':
##
## lbeta
1. Load data sets
setwd("C:/Users/jcolu/OneDrive/Documents/Harrisburg/Fall 2019/ANLY 515/")
GPIC = read_excel("Global Price Index Commodities.xlsx", col_types = c("date", "numeric"))
## readxl works best with a newer version of the tibble package.
## You currently have tibble v1.4.2.
## Falling back to column name repair from tibble <= v1.4.2.
## Message displays once per session.
MSCI = read_excel("MSCI World Index.xlsx", col_types = c("date", "numeric"))
GBI= read_excel("Global Bonds Index.xlsx", col_types = c("date", "numeric"))
USFutures=read_excel("USD Futures Index.xlsx")
2 Create new variables from data set
date=GPIC$observation_date
GPICPrice=GPIC$`Global Commodities Index`
MSCIPrice=MSCI$`MSCI World Index`
GBIPrice=GBI$`Global Bonds Index`
USFPrice=USFutures$`USD Futures`
#2.a Review that format is numeric
str(GPICPrice)
## num [1:76] 58.1 59.8 62.1 64.5 62.1 ...
str(MSCIPrice)
## num [1:76] 1432 1378 1305 1221 1061 ...
str(GBIPrice)
## num [1:76] 48800 49600 49400 50300 50100 50100 49900 49700 50800 50500 ...
str(USFPrice)
## num [1:76] 105 107 113 109 118 ...
3 Add time as a new variable using date as an attribute
attr(GPICPrice,'time')=date
attr(MSCIPrice,'time')=date
attr(GBIPrice,'time')=date
attr(USFPrice,'time')=date
4 Create new data set of the overall portfolio.
Portfolio = cbind(GPICPrice,MSCIPrice,GBIPrice,USFPrice)
head(Portfolio)
## GPICPrice MSCIPrice GBIPrice USFPrice
## [1,] 58.05939 1431.938 48800 105.16
## [2,] 59.76052 1377.722 49600 106.58
## [3,] 62.09196 1305.245 49400 113.00
## [4,] 64.45788 1221.253 50300 109.28
## [5,] 62.07480 1061.262 50100 117.63
## [6,] 60.03447 1084.788 50100 119.75
5 Create & Plot Timeseries of Portfolio
PortfolioZoo=as.zoo(Portfolio)
plot(PortfolioZoo,main= "Time Series of Portfolio")
## Found more than one class "zoo" in cache; using the first, from namespace 'FRAPO'
## Also defined by 'quantmod'
## Found more than one class "zoo" in cache; using the first, from namespace 'FRAPO'
## Also defined by 'quantmod'
## Found more than one class "zoo" in cache; using the first, from namespace 'FRAPO'
## Also defined by 'quantmod'

6 Create & Plot Daily returns of Portfolio
ReturnPortfolio=diff(log(PortfolioZoo))*100
## Found more than one class "zoo" in cache; using the first, from namespace 'FRAPO'
## Also defined by 'quantmod'
## Found more than one class "zoo" in cache; using the first, from namespace 'FRAPO'
## Also defined by 'quantmod'
## Found more than one class "zoo" in cache; using the first, from namespace 'FRAPO'
## Also defined by 'quantmod'
plot(ReturnPortfolio,main ="Returns of Portfolio")

7 Covariance Analysis of Asset Classes
layout(matrix(1:6, nrow = 3, ncol = 2,byrow = TRUE))
ccf(ReturnPortfolio[,1],ReturnPortfolio[,2],lag.max=20, main="Returns GPIC vs MSCI")
ccf(abs(ReturnPortfolio)[,1],ReturnPortfolio[,2],lag.max=20,main="Absolute Returns GPIC vs MSC")
ccf(ReturnPortfolio[,1],ReturnPortfolio[,3],lag.max=20, main="Returns GPIC vs GBI")
ccf(abs(ReturnPortfolio)[,1],ReturnPortfolio[,3],lag.max=20,main="Absolute Returns GPIC vs GBI")
ccf(ReturnPortfolio[,1],ReturnPortfolio[,4],lag.max=20, main="Returns GPIC vs USF")
ccf(abs(ReturnPortfolio)[,1],ReturnPortfolio[,4],lag.max=20,main="Absolute Returns GPIC vs USF")

ccf(ReturnPortfolio[,2],ReturnPortfolio[,3],lag.max=20, main="Returns MSCI vs GBI")
ccf(abs(ReturnPortfolio)[,2],ReturnPortfolio[,3],lag.max=20, main="Absolute Returns MSCI vs GBI")
ccf(ReturnPortfolio[,2],ReturnPortfolio[,4],lag.max=20, main="Returns MSCI vs USF")
ccf(abs(ReturnPortfolio)[,4],ReturnPortfolio[,4],lag.max=20, main="Absolute Returns MSCI vs USF")
ccf(ReturnPortfolio[,3],ReturnPortfolio[,4],lag.max=20, main="Returns GBI vs USF")
ccf(abs(ReturnPortfolio)[,3],ReturnPortfolio[,4],lag.max=20, main="Absolute Returns GBI vs USF")

9 Determining Losses for Asset Classes
Portfolio2=cbind(GPICPrice, MSCIPrice,GBIPrice,USFPrice)
PortfolioTS=timeSeries(Portfolio2,charvec = date)
Portfolioloss= as.data.frame(na.omit(-1.0*diff(log(PortfolioTS))*100.0))
arimaGPIC=arima(Portfolioloss$GPICPrice)
arimaMSCI=arima(Portfolioloss$MSCIPrice)
arimaGBI=arima(Portfolioloss$GBIPrice)
arimaUSF=arima(Portfolioloss$USFPrice)
Forecast1=predict(arimaGPIC, 1)
Forecast2=predict(arimaMSCI, 1)
Forecast3=predict(arimaGBI, 1)
Forecast4=predict(arimaUSF,1)
print(Forecast1);print(Forecast2);print(Forecast3);print(Forecast4)
## $pred
## Time Series:
## Start = 76
## End = 76
## Frequency = 1
## [1] -1.011836
##
## $se
## Time Series:
## Start = 76
## End = 76
## Frequency = 1
## [1] 8.576891
## $pred
## Time Series:
## Start = 76
## End = 76
## Frequency = 1
## [1] -0.3657545
##
## $se
## Time Series:
## Start = 76
## End = 76
## Frequency = 1
## [1] 8.502341
## $pred
## Time Series:
## Start = 76
## End = 76
## Frequency = 1
## [1] 0.246926
##
## $se
## Time Series:
## Start = 76
## End = 76
## Frequency = 1
## [1] 2.212751
## $pred
## Time Series:
## Start = 76
## End = 76
## Frequency = 1
## [1] 0.1251987
##
## $se
## Time Series:
## Start = 76
## End = 76
## Frequency = 1
## [1] 4.160251
Commodities has the higher losses, followed by MSCI, GBI and Futures
10 Determing Risk for Asset Classes
#10.1 Garch Models
gGPIC=garchFit(~garch(1,1),data=Portfolioloss$GPICPrice)
##
## Series Initialization:
## ARMA Model: arma
## Formula Mean: ~ arma(0, 0)
## GARCH Model: garch
## Formula Variance: ~ garch(1, 1)
## ARMA Order: 0 0
## Max ARMA Order: 0
## GARCH Order: 1 1
## Max GARCH Order: 1
## Maximum Order: 1
## Conditional Dist: norm
## h.start: 2
## llh.start: 1
## Length of Series: 75
## Recursion Init: mci
## Series Scale: 8.634648
##
## Parameter Initialization:
## Initial Parameters: $params
## Limits of Transformations: $U, $V
## Which Parameters are Fixed? $includes
## Parameter Matrix:
## U V params includes
## mu -1.17183181 1.171832 -0.1171832 TRUE
## omega 0.00000100 100.000000 0.1000000 TRUE
## alpha1 0.00000001 1.000000 0.1000000 TRUE
## gamma1 -0.99999999 1.000000 0.1000000 FALSE
## beta1 0.00000001 1.000000 0.8000000 TRUE
## delta 0.00000000 2.000000 2.0000000 FALSE
## skew 0.10000000 10.000000 1.0000000 FALSE
## shape 1.00000000 10.000000 4.0000000 FALSE
## Index List of Parameters to be Optimized:
## mu omega alpha1 beta1
## 1 2 3 5
## Persistence: 0.9
##
##
## --- START OF TRACE ---
## Selected Algorithm: nlminb
##
## R coded nlminb Solver:
##
## 0: 105.36582: -0.117183 0.100000 0.100000 0.800000
## 1: 105.05295: -0.117012 0.142296 0.132517 0.776694
## 2: 103.71105: -0.116866 0.142744 0.137818 0.718714
## 3: 102.39522: -0.116496 0.174074 0.250997 0.645676
## 4: 101.90498: -0.116510 0.113823 0.292309 0.571926
## 5: 100.79627: -0.117159 0.126638 0.394766 0.562781
## 6: 100.73204: -0.119243 0.0874759 0.479416 0.520823
## 7: 100.52090: -0.122646 0.161708 0.480450 0.454333
## 8: 100.48322: -0.122901 0.168434 0.494981 0.473342
## 9: 100.41496: -0.125081 0.152867 0.498647 0.468800
## 10: 100.35443: -0.129652 0.141968 0.523269 0.484403
## 11: 100.19006: -0.168714 0.133076 0.575052 0.463370
## 12: 100.13565: -0.206978 0.155761 0.643414 0.413261
## 13: 100.13045: -0.215773 0.147235 0.656662 0.413924
## 14: 100.13009: -0.215498 0.149677 0.655297 0.412720
## 15: 100.13008: -0.215501 0.149673 0.656176 0.412309
## 16: 100.13008: -0.215645 0.149692 0.656299 0.412260
## 17: 100.13008: -0.215661 0.149703 0.656330 0.412243
## 18: 100.13008: -0.215661 0.149704 0.656333 0.412243
##
## Final Estimate of the Negative LLH:
## LLH: 261.8138 norm LLH: 3.490851
## mu omega alpha1 beta1
## -1.8621585 11.1614735 0.6563330 0.4122426
##
## R-optimhess Difference Approximated Hessian Matrix:
## mu omega alpha1 beta1
## mu -0.978247885 -0.003273138 -0.8251040 3.697602
## omega -0.003273138 -0.056439015 -0.9489553 -2.896612
## alpha1 -0.825104043 -0.948955315 -37.8789950 -72.315833
## beta1 3.697602455 -2.896611957 -72.3158325 -224.375588
## attr(,"time")
## Time difference of 0.004961014 secs
##
## --- END OF TRACE ---
##
##
## Time to Estimate Parameters:
## Time difference of 0.15962 secs
gMSCI=garchFit(~garch(1,1),data=Portfolioloss$MSCIPrice)
##
## Series Initialization:
## ARMA Model: arma
## Formula Mean: ~ arma(0, 0)
## GARCH Model: garch
## Formula Variance: ~ garch(1, 1)
## ARMA Order: 0 0
## Max ARMA Order: 0
## GARCH Order: 1 1
## Max GARCH Order: 1
## Maximum Order: 1
## Conditional Dist: norm
## h.start: 2
## llh.start: 1
## Length of Series: 75
## Recursion Init: mci
## Series Scale: 8.559597
##
## Parameter Initialization:
## Initial Parameters: $params
## Limits of Transformations: $U, $V
## Which Parameters are Fixed? $includes
## Parameter Matrix:
## U V params includes
## mu -0.42730339 0.4273034 -0.04273034 TRUE
## omega 0.00000100 100.0000000 0.10000000 TRUE
## alpha1 0.00000001 1.0000000 0.10000000 TRUE
## gamma1 -0.99999999 1.0000000 0.10000000 FALSE
## beta1 0.00000001 1.0000000 0.80000000 TRUE
## delta 0.00000000 2.0000000 2.00000000 FALSE
## skew 0.10000000 10.0000000 1.00000000 FALSE
## shape 1.00000000 10.0000000 4.00000000 FALSE
## Index List of Parameters to be Optimized:
## mu omega alpha1 beta1
## 1 2 3 5
## Persistence: 0.9
##
##
## --- START OF TRACE ---
## Selected Algorithm: nlminb
##
## R coded nlminb Solver:
##
## 0: 102.52725: -0.0427303 0.100000 0.100000 0.800000
## 1: 102.44622: -0.0427367 0.0840408 0.0991912 0.778566
## 2: 101.95302: -0.0427486 0.0930296 0.124001 0.774281
## 3: 101.74364: -0.0427923 0.0831683 0.154476 0.731477
## 4: 101.37921: -0.0429706 0.156144 0.177666 0.656940
## 5: 101.22247: -0.0430732 0.122812 0.218741 0.653633
## 6: 101.19838: -0.0430889 0.129066 0.223035 0.659034
## 7: 101.19104: -0.0434655 0.126208 0.223498 0.658142
## 8: 101.14076: -0.0503697 0.122298 0.231872 0.657244
## 9: 101.03045: -0.0779326 0.149444 0.245797 0.611640
## 10: 100.90745: -0.105579 0.137452 0.241089 0.629370
## 11: 100.87808: -0.126855 0.127012 0.247018 0.642198
## 12: 100.87692: -0.126069 0.131090 0.246902 0.637583
## 13: 100.87689: -0.125898 0.130458 0.246532 0.638501
## 14: 100.87689: -0.125920 0.130434 0.246543 0.638521
## 15: 100.87689: -0.125919 0.130434 0.246542 0.638521
##
## Final Estimate of the Negative LLH:
## LLH: 261.9059 norm LLH: 3.492078
## mu omega alpha1 beta1
## -1.0778129 9.5564703 0.2465425 0.6385207
##
## R-optimhess Difference Approximated Hessian Matrix:
## mu omega alpha1 beta1
## mu -1.312764102 -0.004748237 -1.025597 0.5865645
## omega -0.004748237 -0.114663276 -2.947990 -5.8978676
## alpha1 -1.025596575 -2.947990285 -187.822907 -227.3494544
## beta1 0.586564481 -5.897867551 -227.349454 -423.4729276
## attr(,"time")
## Time difference of 0.004985809 secs
##
## --- END OF TRACE ---
##
##
## Time to Estimate Parameters:
## Time difference of 0.1984768 secs
gGBI=garchFit(~garch(1,1),data=Portfolioloss$GBIPrice)
##
## Series Initialization:
## ARMA Model: arma
## Formula Mean: ~ arma(0, 0)
## GARCH Model: garch
## Formula Variance: ~ garch(1, 1)
## ARMA Order: 0 0
## Max ARMA Order: 0
## GARCH Order: 1 1
## Max GARCH Order: 1
## Maximum Order: 1
## Conditional Dist: norm
## h.start: 2
## llh.start: 1
## Length of Series: 75
## Recursion Init: mci
## Series Scale: 2.227652
##
## Parameter Initialization:
## Initial Parameters: $params
## Limits of Transformations: $U, $V
## Which Parameters are Fixed? $includes
## Parameter Matrix:
## U V params includes
## mu -1.10845897 1.108459 0.1108459 TRUE
## omega 0.00000100 100.000000 0.1000000 TRUE
## alpha1 0.00000001 1.000000 0.1000000 TRUE
## gamma1 -0.99999999 1.000000 0.1000000 FALSE
## beta1 0.00000001 1.000000 0.8000000 TRUE
## delta 0.00000000 2.000000 2.0000000 FALSE
## skew 0.10000000 10.000000 1.0000000 FALSE
## shape 1.00000000 10.000000 4.0000000 FALSE
## Index List of Parameters to be Optimized:
## mu omega alpha1 beta1
## 1 2 3 5
## Persistence: 0.9
##
##
## --- START OF TRACE ---
## Selected Algorithm: nlminb
##
## R coded nlminb Solver:
##
## 0: 105.66483: 0.110846 0.100000 0.100000 0.800000
## 1: 105.46836: 0.110798 0.119382 0.107538 0.811716
## 2: 105.25631: 0.110565 0.128447 0.0906490 0.797641
## 3: 105.20830: 0.110179 0.148864 0.0804424 0.791573
## 4: 105.15238: 0.109435 0.153691 0.0841644 0.769488
## 5: 104.94318: 0.105333 0.211417 0.155123 0.653459
## 6: 104.73606: 0.0660511 0.273360 0.156205 0.591240
## 7: 104.65973: 0.0258022 0.253229 0.188488 0.589292
## 8: 104.65910: 0.0228875 0.247699 0.186241 0.597246
## 9: 104.65892: 0.0228188 0.248676 0.187617 0.594407
## 10: 104.65890: 0.0231444 0.248768 0.186931 0.594662
## 11: 104.65890: 0.0231550 0.248610 0.186588 0.594949
## 12: 104.65890: 0.0231438 0.248595 0.186581 0.594966
## 13: 104.65890: 0.0231432 0.248594 0.186582 0.594966
##
## Final Estimate of the Negative LLH:
## LLH: 164.73 norm LLH: 2.1964
## mu omega alpha1 beta1
## 0.05155502 1.23363275 0.18658172 0.59496575
##
## R-optimhess Difference Approximated Hessian Matrix:
## mu omega alpha1 beta1
## mu -16.9103323 0.8659877 -7.847787 2.320462
## omega 0.8659877 -11.6820122 -30.807218 -49.915046
## alpha1 -7.8477873 -30.8072183 -140.678947 -160.788773
## beta1 2.3204615 -49.9150456 -160.788773 -251.760441
## attr(,"time")
## Time difference of 0.004982948 secs
##
## --- END OF TRACE ---
##
##
## Time to Estimate Parameters:
## Time difference of 0.01695395 secs
gUSF=garchFit(~garch(1,1),data=Portfolioloss$USFPrice)
##
## Series Initialization:
## ARMA Model: arma
## Formula Mean: ~ arma(0, 0)
## GARCH Model: garch
## Formula Variance: ~ garch(1, 1)
## ARMA Order: 0 0
## Max ARMA Order: 0
## GARCH Order: 1 1
## Max GARCH Order: 1
## Maximum Order: 1
## Conditional Dist: norm
## h.start: 2
## llh.start: 1
## Length of Series: 75
## Recursion Init: mci
## Series Scale: 4.188266
##
## Parameter Initialization:
## Initial Parameters: $params
## Limits of Transformations: $U, $V
## Which Parameters are Fixed? $includes
## Parameter Matrix:
## U V params includes
## mu -0.29892733 0.2989273 0.02989273 TRUE
## omega 0.00000100 100.0000000 0.10000000 TRUE
## alpha1 0.00000001 1.0000000 0.10000000 TRUE
## gamma1 -0.99999999 1.0000000 0.10000000 FALSE
## beta1 0.00000001 1.0000000 0.80000000 TRUE
## delta 0.00000000 2.0000000 2.00000000 FALSE
## skew 0.10000000 10.0000000 1.00000000 FALSE
## shape 1.00000000 10.0000000 4.00000000 FALSE
## Index List of Parameters to be Optimized:
## mu omega alpha1 beta1
## 1 2 3 5
## Persistence: 0.9
##
##
## --- START OF TRACE ---
## Selected Algorithm: nlminb
##
## R coded nlminb Solver:
##
## 0: 107.47327: 0.0298927 0.100000 0.100000 0.800000
## 1: 107.29086: 0.0298926 0.118043 0.0905946 0.811263
## 2: 106.94355: 0.0298926 0.118897 0.0681353 0.805286
## 3: 106.39559: 0.0298927 0.148300 0.0353975 0.820359
## 4: 106.16755: 0.0298960 0.188669 1.00000e-08 0.781873
## 5: 105.91774: 0.0299034 0.264209 1.00000e-08 0.727938
## 6: 105.91740: 0.0299131 0.235392 1.00000e-08 0.761686
## 7: 105.91592: 0.0299148 0.249796 1.00000e-08 0.744660
## 8: 105.91583: 0.0299208 0.246041 1.00000e-08 0.748756
## 9: 105.91571: 0.0299376 0.238190 1.00000e-08 0.756992
## 10: 105.91534: 0.0299924 0.214783 1.00000e-08 0.781087
## 11: 105.91420: 0.0301814 0.136027 1.00000e-08 0.861590
## 12: 105.91157: 0.0302704 0.100160 1.00000e-08 0.897889
## 13: 105.87975: 0.0304483 0.0283244 1.00000e-08 0.970384
## 14: 105.80265: 0.0306856 1.00000e-06 1.00000e-08 0.998773
## 15: 105.80069: 0.0306020 1.00000e-06 1.00000e-08 0.998737
## 16: 105.78637: 0.0296528 1.00000e-06 1.00000e-08 0.998389
## 17: 105.77969: 0.0280956 1.00000e-06 1.00000e-08 0.997937
## 18: 105.77958: 0.0278080 1.00000e-06 1.00000e-08 0.997944
## 19: 105.77897: 0.0243187 1.00000e-06 1.00000e-08 0.997957
## 20: 105.77895: 0.0237639 1.00000e-06 1.00000e-08 0.997947
## 21: 105.77895: 0.0237427 1.00000e-06 1.00000e-08 0.997945
## 22: 105.77895: 0.0237457 1.00000e-06 1.00000e-08 0.997945
##
## Final Estimate of the Negative LLH:
## LLH: 213.2005 norm LLH: 2.842673
## mu omega alpha1 beta1
## 9.945344e-02 1.754157e-05 1.000000e-08 9.979445e-01
##
## R-optimhess Difference Approximated Hessian Matrix:
## mu omega alpha1 beta1
## mu -4.367839 3.707778 59.26374 58.95083
## omega 3.707778 -248.412644 -4504.99618 -4127.10123
## alpha1 59.263745 -4504.996177 -81888.38238 -74930.99111
## beta1 58.950828 -4127.101225 -74930.99111 -68809.74286
## attr(,"time")
## Time difference of 0.005014896 secs
##
## --- END OF TRACE ---
##
##
## Time to Estimate Parameters:
## Time difference of 0.02295208 secs
#10.2 Risk Predictions
predict(gGPIC,n.ahead=1)
## meanForecast meanError standardDeviation
## 1 -1.862158 7.364683 7.364683
predict(gMSCI,n.ahead=1)
## meanForecast meanError standardDeviation
## 1 -1.077813 9.59738 9.59738
predict(gGBI,n.ahead=1)
## meanForecast meanError standardDeviation
## 1 0.05155502 2.993194 2.993194
predict(gUSF,n.ahead=1)
## meanForecast meanError standardDeviation
## 1 0.09945344 3.847598 3.847598
MSCI has the highest risk, followed by USF, GPIC, and GBI
11 Distribution using QQplot
par(mfrow=c(2,2))
qqplot(Portfolioloss$GPICPrice,date,main="Commodities QQ Plot")
qqplot(Portfolioloss$MSCIPrice,date,main="MSCI QQ Plot")
qqplot(Portfolioloss$GBIPrice,date,main="Bonds QQ Plot")
qqplot(Portfolioloss$USFPrice,date,main="US Futures QQPlot")

12 PACF & ACF Analysis
acf(Portfolioloss$GPICPrice, main = "ACF GPIC")

pacf(Portfolioloss$GPICPrice, main = "PACF GPIC") #Appropriate Model is arma(1,0)

acf(Portfolioloss$MSCIPrice, main = "ACF MSCI")

pacf(Portfolioloss$MSCIPrice, main = "PAC MSCI") #Appropriate Model is arma(1,0)

acf(Portfolioloss$GBIPrice,main = "ACF GBI")

pacf(Portfolioloss$GBIPrice,main = "PACF GBI") #Appropriate Model is arma(1,0)

acf(Portfolioloss$USFPrice,main = "ACF USF")

pacf(Portfolioloss$USFPrice,main = "PACF USF") #Appropriate Model is arma(1,0)

13 Expected Shortfall Risk Analysis
#13.a Create ESgarch function
ESgarch = function(y, p = 0.95){
gfit = garchFit(formula = ~garch(1, 1), data = y,
cond.dist = "std", trace = FALSE)
sigma = as.numeric(predict(gfit, n.ahead = 1)[3])
df = as.numeric(coef(gfit)["shape"])
ES = sigma * (dt(qt(p, df), df)/(1 - p)) *
((df + (qt(p, df))^2)/(df - 1))
return(ES)
}
#13.b Determining Risk with 95% confidence
ESgarch(Portfolioloss$GPICPrice, p=0.95)
## [1] 24.00484
ESgarch(Portfolioloss$MSCIPrice, p=0.95)
## [1] 38.08398
ESgarch(Portfolioloss$GBIPrice, p=0.95)
## Warning in sqrt(diag(fit$cvar)): NaNs produced
## [1] 32.68385
ESgarch(Portfolioloss$USFPrice, p=0.95)
## Warning in sqrt(diag(fit$cvar)): NaNs produced
## [1] 10.35681
MSCI has the highest risk, followed by Bonds, Commodities and Futures.
14. Generate one-step-ahead forecastsof the conditional variance (Standard Deviation)
gfitPort = lapply(Portfolioloss,garchFit,formula=~garch(1,1),cond.dist="std",trace=FALSE)
## Warning in sqrt(diag(fit$cvar)): NaNs produced
## Warning in sqrt(diag(fit$cvar)): NaNs produced
gprong= unlist(lapply(gfitPort,function(x) predict(x,n.ahead = 1)[3]))
gprong
## GPICPrice.standardDeviation MSCIPrice.standardDeviation
## 7.979939 11.398019
## GBIPrice.standardDeviation USFPrice.standardDeviation
## 6.022518 4.300284
#MSCI has the highest standard deviation, followd by commodities, bonds and futures.
15. Student T-distribution
#15 a.Estimate degrees-of-freedom parameters
gshape=unlist(lapply(gfitPort, function(x) x@fit$coef[5]))
#15 b. Get Residuals
gresid=as.matrix(data.frame(lapply(gfitPort,function(x) x@residuals / sqrt(x@h.t))))
head(gresid)
## GPICPrice MSCIPrice GBIPrice USFPrice
## [1,] -0.03213997 0.651223774 -0.76294908 -0.3456195
## [2,] -0.18299692 0.873320660 0.11297453 -1.3947621
## [3,] -0.18193451 1.008784393 -0.78694140 0.7488866
## [4,] 1.03731292 1.766372090 0.10392636 -1.7430055
## [5,] 0.80357668 -0.009152543 -0.05125202 -0.4456687
## [6,] 1.26582114 1.732018552 0.09919411 1.1222284
#15.c Calculate the pseudo-uniform variables from the standardized residuals for each asset.
U = sapply(1:4, function(y) pt(gresid[, y], df = gshape[y]))
head(U)
## [,1] [,2] [,3] [,4]
## [1,] 0.4878615 0.7234913 0.2596988 0.36839454
## [2,] 0.4313385 0.7823241 0.5401989 0.09664681
## [3,] 0.4317318 0.8128712 0.2538825 0.76441323
## [4,] 0.8242110 0.9211775 0.5369969 0.05596987
## [5,] 0.7692168 0.4965840 0.4817186 0.33266504
## [6,] 0.8667200 0.9180668 0.5353204 0.85600614
#15.d Estimate student's t copula model based on Kendall's rank correlations.
cop = fit.tcopula(Udata = U, method = "Kendall")
## Warning in FUN(newX[, i], ...): NaNs produced
## Warning in FUN(newX[, i], ...): NaNs produced
## Warning in FUN(newX[, i], ...): NaNs produced
## Warning in FUN(newX[, i], ...): NaNs produced
## Warning in log(pi * df): NaNs produced
## Warning in nlminb(startdf, negloglik2, data = Udata, P = P, ...): NA/NaN
## function evaluation
## Warning in FUN(newX[, i], ...): NaNs produced
## Warning in FUN(newX[, i], ...): NaNs produced
## Warning in FUN(newX[, i], ...): NaNs produced
## Warning in FUN(newX[, i], ...): NaNs produced
## Warning in log(pi * df): NaNs produced
## Warning in nlminb(startdf, negloglik2, data = Udata, P = P, ...): NA/NaN
## function evaluation
cop
## $P
## [,1] [,2] [,3] [,4]
## [1,] 1.00000000 0.06279052 0.21952407 -0.2949870
## [2,] 0.06279052 1.00000000 0.02660134 -0.3885590
## [3,] 0.21952407 0.02660134 1.00000000 0.1191534
## [4,] -0.29498698 -0.38855898 0.11915336 1.0000000
##
## $nu
## [1] 0.8838006
##
## $converged
## [1] TRUE
##
## $ll.max
## [1] 8.554514
##
## $fit
## $fit$par
## [1] 0.8838006
##
## $fit$objective
## [1] -8.554514
##
## $fit$convergence
## [1] 0
##
## $fit$iterations
## [1] 7
##
## $fit$evaluations
## function gradient
## 12 10
##
## $fit$message
## [1] "relative convergence (4)"
Most correlated are commodities and bonds, followed by commodities and public, followe by public and bonds, the commodities and Futures and lastly,Futures and Commodities.
Suggested to use Rcop - relative convergence
16. Prediction of Standard Devaition
#15.e Generating 100,000 data sets of random variates for the pseudo-uniformly distributed variables
rcop = rcopula.t(100000, df = cop$nu, Sigma = cop$P)
hist(rcop)

#15.f Compute the quantiles for Monte Carlo draws
qcop = sapply(1:4, function(x) qstd(rcop[, x], nu = gshape[x]))
hist(qcop)

#15.f Matix of 1 period ahead predictions of standard deviations
ht.mat = matrix(qcop, nrow = 100000, ncol = ncol(Portfolioloss), byrow = TRUE)
head(ht.mat)
## [,1] [,2] [,3] [,4]
## [1,] -0.2918290 0.5238747 0.60419027 0.4913161
## [2,] -1.1862393 0.4665363 -0.07689988 -0.3813966
## [3,] -0.2718209 -1.5598201 0.83782458 -0.5692675
## [4,] 0.7834701 -0.1135753 -0.77074863 0.3984220
## [5,] 0.5360641 -1.3879259 0.63394585 -0.3246103
## [6,] -0.5729120 1.3718360 0.98886582 0.2074171
16 GMV Portfolio
#16.a Monthly returns of each asset class
Portreturns=as.data.frame(-1.0*Portfolioloss)
head(Portreturns)
## GPICPrice MSCIPrice GBIPrice USFPrice
## 2000-06-30 2.887882 -3.859736 1.6260521 1.341288
## 2000-09-30 3.827134 -5.404065 -0.4040410 5.849194
## 2000-12-31 3.739540 -6.651338 1.8054653 -3.347442
## 2001-03-31 -3.767184 -14.041861 -0.3984069 7.363071
## 2001-06-30 -3.342120 2.192581 0.0000000 1.786213
## 2001-09-30 -6.845018 -15.824078 -0.4000005 -4.955873
#16.b Covariance matrix from montlhly returns of portfolio.
Portcov=cov(Portreturns,use="pairwise.complete.obs")
head(Portcov)
## GPICPrice MSCIPrice GBIPrice USFPrice
## GPICPrice 74.5571497 23.860337 0.7414518 -11.663517
## MSCIPrice 23.8603371 73.266699 -1.3346419 -13.635011
## GBIPrice 0.7414518 -1.334642 4.9624318 1.264946
## USFPrice -11.6635171 -13.635011 1.2649457 17.541574
#16.c Global minimum variance portfolio
ERC=PGMV(Portcov)
## Iteration: 0
## pobj: 0.00304446
## dobj: -1.16054
## pinf: 0
## dinf: 3.21333
## dgap: 1.16359
##
## Iteration: 1
## pobj: 0.00015428
## dobj: -0.0841111
## pinf: 1.15486e-016
## dinf: 0.222559
## dgap: 0.0842653
##
## Iteration: 2
## pobj: 7.89624e-007
## dobj: -0.00490848
## pinf: 2.86098e-017
## dinf: 0.0122636
## dgap: 0.00490927
##
## Iteration: 3
## pobj: 2.08746e-009
## dobj: -0.000248704
## pinf: 2.2388e-016
## dinf: 0.000617727
## dgap: 0.000248706
##
## Iteration: 4
## pobj: 5.23302e-012
## dobj: -1.24438e-005
## pinf: 2.22045e-016
## dinf: 3.08981e-005
## dgap: 1.24438e-005
##
## Iteration: 5
## pobj: 1.17728e-014
## dobj: -6.22212e-007
## pinf: 2.22045e-016
## dinf: 1.54494e-006
## dgap: 6.22212e-007
##
## Iteration: 6
## pobj: -1.13408e-015
## dobj: -3.11106e-008
## pinf: 4.44089e-016
## dinf: 7.72469e-008
## dgap: 3.11106e-008
##
## Optimal solution found.
w=Weights(ERC)
wGPIC=as.numeric(w[1])/10
wMSCI=as.numeric(w[2])/10
wGBI=as.numeric(w[3])/10
wUSF=as.numeric(w[4])/10
weights = c(wMSCI,wGBI,wGBI,wUSF)
pfall = (qcop * ht.mat) %*% weights
#16.d Expected Shortfall value of risk for the "global minimum variance portfolio
pfall.var95 = min(tail(sort(pfall), 5000))
pfall.var95
## [1] 8.760384
pfall.es95 = median(tail(sort(pfall), 5000))
pfall.es95
## [1] 13.30321
pfall.max95=max(tail(sort(pfall),5000))
pfall.max95
## [1] 437.2507