[R-SIG-Finance] [R-sig-Finance] Modified Cornish-Fisher VaR

Brian G. Peterson brian at braverock.com
Fri Aug 4 19:12:27 CEST 2006


On Thursday 27 July 2006 08:35, Diethelm Wuertz wrote:
> Brian G. Peterson wrote:
> >Has anyone done any work on Modified Cornish-Fisher VaR calculations
> > in R?
>
> ----------------- YES ----------------------
>
> You can download R-functions and Description PDF from
>
> http://www.itp.phys.ethz.ch/econophysics/favre/
>
> WARNING - UNTESTED and not part of official Rmetrics !!!!!
>
> Diethelm Wuertz

Thank you to everyone who responded to this thread.  The file provided by 
Prof. Wuertz was an excellent starting point. I've corrected a small 
calculation error, and added some type checking and other bits to the 
function.  I've attached the modified .R file here which implements a 
Modified Cornish-Fisher VaR calculation.  Interested persons can now 
consider this updated function as tested and working. I'll post again as 
we get the co-moment calculations for co-kurtosis and co-skewness 
working. 

Regards,

    - Brian

> Brian G. Peterson wrote:
> >The limitations of VaR in modeling of risk for non-normal
> > distributions have been known for quite some time, and this approach
> > seems to hold some value over other approaches already implemented in
> > RMetrics like Conditional VaR.
> >
> >I'm trying to replicate the calculation as laid out in:
> >Favre, Laurent, and Jose-Antonio Galeano. “Mean-Modified
> >Value at Risk Optimization with Hedge Funds.” The Journal
> >of Alternative Investments, 5 (2002), pp. 21-25.
> >
> >and presented in a different form in an earlier paper:
> >Fallon, William. "Calculating Value-at-Risk"
> >Working Paper, Wharton, 1996.
> >
> >Both of these papers rely on calculating traditional VaR (as is done
> > with fPortfolio.VaR() from the RMetrics package) and then using a
> >Cornish-Fisher Expansion for skew (both papers) or skew and kurtosis
> >(Favre 2002) (as is done using the fBasics.skewness() and
> >fBasics.kurtosis() functions from RMetrics)
> >
> >I'm wondering if anyone has already replicated this work in R, and
> > could provide a pointer, or alternately if some of the more
> > experienced people on this list could render an opinion on which of
> > the Cornish-Fisher functions in the core R stats package might be
> > appropriate for this kind of analysis.
> >
> >I would like to implement and share a function for Modified
> > Cornish-Fisher VaR, so any assistance would be greatly appreciated.
> >
> >Regards,
> >
> >   - Brian
> >
> >_______________________________________________
> >R-SIG-Finance at stat.math.ethz.ch mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-------------- next part --------------
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA

# Copyright 1999-2003 Diethelm Wuertz for this R-port


################################################################################
# FUNCTION:
# annualizedMean
# annualizedVolatility
# annualizedSkewness
# annualizedKurtosis
# maxDrawdown
# timeUnderWater
# maxMonthlyLoss
# modifiedVaR
# monthlySharpeRatio
# skewnesskurtosisPrice
################################################################################


################################################################################
# The following functions are implemented from Extreme Metrics
# These are the risk measures implemented in the hedge fund
#   software from www.AlternativeSoft.com
#   See, "ExtremeMetrics Software", Help Document, Alternative Software,
#   March 2003, 4 pages.


# All returns are assumed to be on a monthly scale!
# The argument r is the monthly return time series !


annualizedMean =
function(r)
{   # A function implemented by Diethelm Wuertz

    # Description:

    # FUNCTION:

    # Mean of Monthly Returns
    result = (1 + mean(r))^12 - 1

    # Return Value:
    result
}


# ------------------------------------------------------------------------------


annualizedVolatility =
function(r)
{   # A function implemented by Diethelm Wuertz

    # Description:

    # FUNCTION:

    # Standard deviation of Monthly Returns:
    result = sqrt(var(r))

    # Return Value:
    result
}

std =
function(r)
{   # A function implemented by Diethelm Wuertz
    # NOTE: std function is listed in the doc for fBasics, but not implemented
    # Standard deviation of Monthly Returns:
    result = sqrt(var(r))

    # Return Value:
    result
}

# ------------------------------------------------------------------------------


annualizedSkewness =
function(r)
{   # A function implemented by Diethelm Wuertz

    # Description:

    # FUNCTION:

    # Skewness of Monthly Returns:
    #   [Skewness is part of the fBasics library]
    result = skewness(r)

    # Return Value:
    result
}


# ------------------------------------------------------------------------------


annualizedKurtosis =
function(r)
{   # A function implemented by Diethelm Wuertz

    # Description:

    # FUNCTION:

    # Excess Kurtosis of Monthly Returns:
    #    [Excess Kurtosis is part of the fBasics library]
    result = kurtosis(r)

    # Return Value:
    result
}


# ------------------------------------------------------------------------------


maxDrawdown =
function(r)
{   # A function implemented by Diethelm Wuertz

    # Description:

    # FUNCTION:

    # Maximum Drawdon of the Return Series
    result = NA

    # Return Value:
    result
}


# ------------------------------------------------------------------------------


timeUnderWater =
function(r)
{   # A function implemented by Diethelm Wuertz

    # Description:

    # FUNCTION:

    # Maximum time under water:
    NA

    # Return Value:
    result

}


# ------------------------------------------------------------------------------


maxMonthlyLoss =
function(r)
{   # A function implemented by Diethelm Wuertz

    # Description:

    # FUNCTION:

    result = min(r)

    # Return Value:
    result
}


# ------------------------------------------------------------------------------


modifiedVaR =
function(r, modified = TRUE, p=0.99, column=1)
{   # A function implemented by Diethelm Wuertz,
    # function completed/debugged by Brian G. Peterson

    # Description:

    # The limitations of mean Value-at-Risk are well covered in the literature.
    # Laurent Favre and Jose-Antonio Galeano published a paper in the
    # Fall 2002, volume 5 of the Journal of Alternative Investment,
    # "Mean ?modified Value-at-Risk optimization With Hedge Funds",
    # that proposed a modified VaR calculation that takes the higher moments
    # of non-normal distributions (skewness, kurtosis) into account, and
    # collapses to standard mean-VaR if the return stream follows a
    # standard distribution.
    # This measure is now widely cited and used in the literature,
    # and is usually referred to as "Modified VaR" or "Modified Cornish-Fisher VaR"

    # Diethelm Wuertz's original function was called monthlyVaR, but did not
    # contain the required modifications to get to a monthly number.  I have converted
    # it to modifiedVaR, and made the assumption of p=0.99, with an option for p=0.95 and
    # a collapse to normal mean VaR.

    # FUNCTION:

    # NOTE: see the data type conditionals in 'cov' and replicate here
    if (class(r) == "matrix") {
        r = r[, column]
        warning("Column ", column, colnames(r)[,column], " of matrix used")
    }
    if (class(r) == "data.frame") {
        r = r[, column]
        warning("Column ", column, colnames(r)[,column], " of data.frame used")
    }
    if (class(r) == "timeSeries") {
        r = r at Data[, column]
        warning("Column ", column, colnames(r)[,column], " of timeSeries used")
    }
    if (!is.numeric(r)) stop("The selected column is not numeric")
    r = as.vector(r)

    if ( p == 0.95 ) {
        zc = -1.96 #95% probability
    }
    if ( p == 0.99 ) {
        zc = -2.33 #99% probability
    }
    #} else {
    #    #some function here to compute zc with arbitrary p
    #}

    if (modified) {
        s = colSkewness(r) #use regular skewness and kurtosis fn if data.frame is converted to matrix?
        k = colKurtosis(r) #to compute excess kurtosis
        Zcf = zc + (((zc^2-1)*s)/6) + (((zc^3-3*zc)*k)/24) + (((2*zc^3)-(5*zc)*s^2)/36)
        result = mean(r) - (Zcf * sqrt(var(r)))
    } else {
        # should probably add risk-free-rate skew here?
        result = mean(r) - (zc * sqrt(var(r)))
    }

    # Return Value:
    result
}

monthlyVaR =
function(r, modified = FALSE)
{
    # stub function to preserve the original syntax in the port of ExtremeMetrics
    result = modifiedVaR(r, modified)
    # Return Value:
    result
}

# ------------------------------------------------------------------------------


monthlySharpeRatio =
function(r, modified=FALSE)
{   # A function implemented by Diethelm Wuertz

    # Description:

    # FUNCTION:

    if (modified) {
        result = mean(r)/monthlyVaR(r, modified = TRUE)
    } else {
        result = mean(r)/sqrt(var(r))}

    # Return Value:
    result
}


# ------------------------------------------------------------------------------


skewnesskurtosisPrice =
function(r)
{   # A function implemented by Diethelm Wuertz

    # Description:

    # FUNCTION:

    result = mean(r) *
        ( monthlyVaR(r, modified = TRUE) / monthlyVaR(r, modified = FALSE) - 1   )

    # Return Value:
    result
}


################################################################################


More information about the R-SIG-Finance mailing list