6.4.1 The package fBasics
The packagefBasicsis part of the Rmetrics suite of packages (see Würtz et al. 2014).
The primary purpose of this package is to provide basic tools for the statistical analy- sis of financial market data. Within the packageS4classes and methods are utilized.
The package is considered a core package in the CRAN “Finance” Task View and is also listed in the “Distributions” Task View. The package has dependencies on other packages contained in the Rmetrics suite. With respect to the modelling, fitting, and inferences drawn from the GHD, quite a few functions have been directly ported and/or included from the packageGeneralizedHyperbolicto this package. The latter package will be presented in the next subsection.
With respect to the topic of this chapter, the following distributions are addressed in this package: the generalized hyperbolic, the generalized hyperbolic Student’st, the hyperbolic, and the normal inverse Gaussian, as well as the standardized ver- sions of the GHD and NIG distributions. For each of these distributions, functions for calculating the value of the density, the probabilities, the quantiles, and the gen- eration of random numbers are available. TheRnaming convention for the density (prefixd), distribution (prefixp), quantile function (prefixq), and the generation of random numbers (prefixr) is followed. In addition, routines for fitting and for cal- culation of the mode and moments are included in the package. In particular, the mean, variance, skewness, and kurtosis are implemented for all distributions. It is
k k further possible to return their robust counterparts, namely, the median, the interquar-
tile range, and skewness or kurtosis measures that are derived from these quartiles.
The shape of the density for the GHD, HYP, and NIG can be plotted interactively for various parameter combinations through a tcl/Tkinterface. These functions are termedghSlider(),hypSlider(), andnigSlider(), respectively, and are intended primarily for illustrative purposes.
The functions that relate to the GHD have gh in their names, while those for the generalized hyperbolic Student’sthaveght, those for the HYP havehyp, and those for the NIG have nig. Thus, the routines for fitting these distributions to financial market return data are termedfooFit()wherefoois replaced by one of these abbreviations. Similarly, the mode is returned by the functionfooMode() and the routines for the moments are fooMean(), fooVar(), fooSkew(), and fooKurt()for the mean, the variance, the skewness, and the kurtosis, re- spectively. Analogously, the functionsfooMED(),fooIQR(),fooSKEW(), and fooKURT()relate to the robust counterparts, namely the median, the interquartile range, and the robust definitions of the skewness and the kurtosis.
By default, the unknown parameters of the distributions are estimated by applying the ML principle. Here, the negative log-likelihood is minimized with the function nlminb(). It should be noted that the ellipsis argument in the functionfooFit() is passed down to the plotting of the fitted distribution, hence the user cannot pass arguments directly to the optimization routine. With respect to fitting the NIG, in addition to the ML principle, the parameters can be estimated by the generalized methods of moments, maximum product spacing, or minimum variance product spac- ing. It is also possible to produce a shape triangle for fitted objects with the routine nigShapeTriangle(). All fit()methods return an object of formal class fDISTFIT, for which ashow()method is defined.
6.4.2 The package GeneralizedHyperbolic
This package offers functions not only for the GHD, but also for the derived distribu- tions HYP, GIG, and skew Laplace (see Scott 2015). The package is written purely inR. ANAMESPACEfile is included in the package’s source that contains the export directives for the functions andS3methods pertinent to the above-mentioned distri- butions. Some of the routines contained in this package have been ported tofBasics.
Routines for fitting the hyperbolic, normal inverse Gaussian, and generalized inverse Gaussian distributions to data have been implemented. Six data sets from the fields of geology, engineering, and finance are provided. The package is contained in the CRAN Task View “Distributions.”
The functions that relate to the generalized hyperbolic have the acronymghypin their names. Routines for the density, the distribution, the quantile function, and the generation of random numbers are implemented, and thedpqrnaming convention as introduced in Section 6.4.1 is followed. Furthermore, the first derivative of the density function has been implemented as functionddghyp(). Quantile–quantile and percentile–percentile plots for a given set of parameters and an observed time series have been implemented as functions qqghyp() and ppghyp(),
k k respectively. It is further possible to return the ranges on the real line where the
probability mass is small for a given parameter set (ghypCalcRange()). Whether certain parameters are in the allowable set of values can be checked with the function ghypCheckPars(). Furthermore, with the routine ghypChangePars() the user can calculate the parameters for the alternative specifications of the GHD. The moments and mode of the GHD distribution can be computed with the functions ghyperbMean(), ghyperbVar(), ghyperbSkew(), ghyperbKurt(), and ghyperbMode() for the mean, variance, skewness, kurtosis, and mode, respectively. The moments of the GHD can also be computed with the function ghypMom().
The chosen acronym for the hyperbolic distribution in this package ishyperb.
Thedpqrnaming convention is followed as in the case of the generalized hyperbolic distribution, and the first derivative of the density function has been implemented as functionddhyperb(). The two-sided Hessian matrix for a given data vector and parameter set can be calculated by callinghyperbHessian(). The points on the real line for which the probability mass for a given set of parameters is negligibly small can be determined by means of the routine hyperbCal- cRange(). Conversion from one HYP parameterization to another is accomplished by invoking hyperbChangePars(). A similar group of functions to the case of the GHYP for recovering the moments is also made available, now prefixed by hyperb instead of ghyp. The fitting of the HYP distribution is achieved with the functionhyperbFit(). Suitable starting parameters can be determined with the function hyperbFitStart(). The user has the option to choose between six different numerical optimization algorithms, namely Nelder–Mead, BFGS (Broyden–Fletcher–Goldfarb–Shanno), and L-BFGS-B (all implemented in optim(), nonlinear minimization (nlm()), or constrained optimization (con- strOptim()). Arguments to control the behavior of the optimizer can be specified in the call tohyperbFit(). This function returns an object with class attributes hyperbFit,distFit. For objects of this kind,print(), summary(), and plot()methods have been defined. In addition, there arecoef()andvcov() methods for retrieving the estimated parameters and their variance-covariance matrix.
The fit can be assessed in the form of a quantile–quantile (QQ) plot (qqhyperb()) or probability–probability (PP) plot (pphyperb()). Furthermore, a Cramér–von Mises goodness-of-fit test is implemented as function hyperbCvMTest(). This function returns an object with class attribute hyperbCvMTest for which a print()method has been defined. Finally, a linear model with hyperbolic errors can be fitted by utilizinghyperblm().
Basically, the same set of functions that have been defined for the HYP are made available for the generalized inverse Gaussian (acronymgig). In addition, the mo- ments and raw moments for a given parameter specification are computed by the functionsgigMom()andgigRawMom(), respectively. The moments of its spe- cial cases, namely the gamma and inverse gamma distributions, can be invoked by gammaRawMom(). Similarly, the same set of functions that have been defined for the GHD are made available for the GIG. In addition, QQ and PP plot routines are included in the package for this distribution.
k k In addition, the functions that directly relate to the skew Laplace distribution
(acronym skewlap) are available: the density, the distribution, the quantile function, and the generation of random numbers, as well as methods for producing QQ and PP plots.
6.4.3 The package ghyp
In contrast to the previous package,ghypprovides functions for fitting not only the univariate HYP, but also the GHD, NIG, VG, Student’st, and Gaussian distributions for the univariate and multivariate cases (see Luethi and Breymann 2013). The pack- age utilizesS4 classes and methods and is shipped with aNAMESPACEfile. It is contained in the CRAN “Distributions” and “Finance” Task Views. In addition to the package’s help files, a vignette is available.
The functions that relate to the GHD or the GIG are the density, quantile, proba- bility, and random variates routines. Thedpqrnaming convention is followed and the chosen acronyms areghyp andgigfor the GHD and the GIG, respectively.
A feature of this package is the inclusion of routines for calculating the expected shortfall for these distributions. The functions are namedESghyp()andESgig(). Furthermore, the package offers a function for portfolio optimization (portfo- lio.optimize()). The user can choose the risk measure employed, namely the standard deviation, the VaR, or the ES, and whether the portfolio should be a mini- mum risk, a tangency, or a target return portfolio. These portfolios are derived from a multivariate GHD.
To estimate the unknown coefficients of the GHD and its special cases, the ML principle is employed. The function names are made up of the prefixfit.followed by the acronym of the desired distribution, followed by eitheruvor mvfor fitting univariate or multivariate data. The objects returned are of formal classmle.ghyp.
For objects of this kind, show()andsummary() methods are defined as well as methods for extracting the Akaike information criterion (AIC) and the value of the log-likelihood. Furthermore, a routine for model selection using the AIC is im- plemented as functionstepAIC.ghyp(). In addition, a function for discriminat- ing between models in the form of a likelihood ratio test is implemented (see rou- tine lik.ratio.test()). The estimated parameters can be extracted with the coef()method. The moments of objects that inherit from the classghypcan be computed with the functionmean()for the mean, with the functionvcov()for the variance in the univariate and the variance-covariance matrix in the multivariate case, and with the functionsghyp.skewness()andghyp.kurtosis()for the skewness and kurtosis, respectively. In general, central and non-central moments can be computed with the functionghyp.moment(). By default, the skewness and the kurtosis are returned.
For plotting purposes, a QQ plot, a histogram view, and a pairs plot for the graphical display of multivariate QQ plots, as well as plotting the density or superimposing the density on existing plot devices, are available. The functions are termedqqghyp(), hist(),pairs(),plot(), andlines(), in that order.
k k The package comes with two data sets. The first,indices, contains monthly
returns for five asset classes between August 1999 and October 2008. The second data set (smi.stocks) contains daily return data for the Swiss equity market and equity returns of selected Swiss companies from 5 January 2000 to 10 January 2007.
6.4.4 The package QRM
Most of the examples contained in McNeil et al. (2005) can be replicated with the functions contained in the packageQRM(see Pfaff and McNeil 2016). These were originally written in the S-PLUS language by A. McNeil and distributed as pack- ageQRMlib. An initialRport was accomplished by S. Ulman and is still available from the CRAN archive (see McNeil and Ulman 2011). The packageQRMis based on this initialR port. It has dependencies on the CRAN packagesgsl,mvtnorm, numDeriv, andtimeSeries. WithinQRMpartial use ofS3classes and methods is made. The more burdensome computations are interfaced fromCroutines. In addi- tion, 14 financial data sets are available.
With respect to the GHD, functions for fitting data to its special cases, namely the NIG and HYP, are included in this package. These are termedfit.NH()for univari- ate andfit.mNH()for multivariate data. Thecaseargument of these functions controls whether the negative log-likelihood of the NIG or HYP is minimized. Both routines return a list object without a class attribute. Hence, no further methods are available.
In addition, the moment and log moment of the GIG can be computed with the functionsEGIG()andElogGIG(), respectively. Random variates of this distribu- tion can be generated with the functionrGIG()that interfaces to a routine written inC.
6.4.5 The package SkewHyperbolic
The packageSkewHyperbolicis dedicated solely to the modelling and fitting of the skew hyperbolic Student’stdistribution (see Scott and Grimson 2015). The package is written purely inR, andS3classes and methods are used. It is shipped with a NAMESPACEfile, and some underlying utility functions are imported from the pack- agesGeneralizedHyperbolicandDistributionUtils. As well as the functions that primarily deal with the skew hyperbolic distribution, three data sets are included in the package.
With respect to the distribution itself, routines for its density, distribution, and quantile functions as well as for the generation of random variates are included. The dpqrnaming convention is followed, and the chosen acronym for this distribution isskewhyp. In addition to these functions there is a routine for returning the first derivative of the density function (ddskewhyp()). Similar to the packageGener- alizedHyperbolic, a function for determining ranges for which the probability mass is small is available (skewhypCalcRange()). The coherence of a parameter set can be checked with the functionskewhypCheckPars().
The included routinesskewhypMean(),skewhypVar(),skewhypSkew(), andskewhypKurt()are used for calculating the mean, variance, skewness, and
k k kurtosis, respectively. The mode of the distribution for a given parameter set can be
computed with skewhypMode(). Similarly to the packageGeneralizedHyper- bolic, the central and non-central moments of any order can be calculated with the routineskewhypMom().
The fitting of data to the skew hyperbolic Student’stdistribution is accomplished by the functionskewhypFit(). Suitable starting values can be determined with the routine skewhypFitStart(). The parameters are determined numerically by applying the ML principle. The negative log-likelihood is minimized by employ- ing either the general purpose optimizeroptim()or the functionnlm(). For the former the user can use either the BFGS or Nelder–Mead algorithm. The function skewhypFit()returns an object of informal classskewhypFit. For objects of this kind,print(),plot(), andsummary()methods are available. Goodness of fit can be inspected graphically by means of a QQ and/or PP plot. The relevant func- tions are termedqqskewhyp()andppskewhyp(), respectively. In addition, a function for producing a tail plot line (skewhypTailPlotLine()) for a given data set and parameter specification is provided.
6.4.6 The package VarianceGamma
The package VarianceGamma can be considered as a twin package to the Ske- wHyperbolicpackage discussed in the previous subsection, but its focus is on the variance gamma distribution (see Scott and Dong 2015). As its twin, the package is contained in the CRAN “Distributions” Task View. Within the packageS3classes and methods are employed and the package is shipped with aNAMESPACEfile in which import directives for the utility functions contained in the packagesGener- alizedHyperbolicandDistributionUtilsare included. Basically, all functionalities contained in the packageSkewHyperbolichave been mirrored in this package, and thedpqrnaming convention is followed. The acronymvgis used for the variance gamma distribution. Hence, the discussion of the functions, methods, and classes in Section 6.4.5 carries over to these instances, too.