[R-SIG-Finance] Réf. : Re: assetsStat function Question

guillaume.nicoulaud at halbis.com guillaume.nicoulaud at halbis.com
Thu Feb 15 17:39:08 CET 2007


Hi all,
Here are a few functions for those who want play with the higher moment CAPM. I never really used them so they may be some area for improvements. Suggestions are welcome (in particular, the cokurt function is likely to hurt your pc for more
than 100 assets)!!



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

covar <- function(X)

      { # author: me!



      # Definition:

      # The covar() function returns the sample covariance matrix of X. Unlike

      # the cov() function in R, the denominator used is n (not n-1): this

      # yields in a potentially biased estimator.

      # X should be a matrix without NAs, rows should be observations and

      # columns the assets.



      if(!is.matrix(X))

            stop('X should be a matrix\n')

      if(any(is.na(X)) == TRUE)

            stop('NAs detected in X!\n')



      t(X) -> x

      ncol(x) -> n

      rowMeans(x) -> xbar



      f <- function(xi, xbar) { xi-xbar }

      apply(x, 2, f, xbar = xbar) -> T



      (1/n) * T%*%t(T) -> res



      return (res)



      }



coskew <- function(X)

      { # author: me!



      # Definition:

      # The coskew() function returns the co-skewness matrix of X.

      # As suggested by Athayde and Flôres (2001), we transform the (n,n,n)

      # co-skewness matrix into a (n,n^2) matrix, simply by slicing each (n,n)

      # layer and pasting them, in the same order, sideways.

      # X should be a matrix without NAs, rows should be observations and

      # columns the assets.



      if(!is.matrix(X))

            stop('X should be a matrix\n')

      if(any(is.na(X)) == TRUE)

            stop('NAs detected in X!\n')



      t <- ncol(X)

      n <- nrow(X)



      f1 <- function(xi) xi - mean(xi)

      apply(X, 2, f1) -> x



      f2 <- function(xi, t) rep(xi, t)



      x1 <- matrix( apply(x, 2, f2, t = t), n, t^2 )

      x2 <- matrix( rep(x, t), n, t^2 )



      res <- (1/n) * t(x) %*% (x1*x2)



      return (res)



      }



cokurt <- function(X)

      { # author: me!



      # Definition:

      # The cokurt() function returns the co-kurtosis matrix of X.

      # As suggested by Athayde and Flôres (2001), we transform the (n,n,n,n)

      # co-kurtosis matrix into a (n,n^3) matrix, simply by slicing each (n,n)

      # layer and pasting them, in the same order, sideways.

      # X should be a matrix without NAs, rows should be observations and

      # columns the assets.



      if(!is.matrix(X))

            stop('X should be a matrix\n')

      if(any(is.na(X)) == TRUE)

            stop('NAs detected in X!\n')



      t <- ncol(X)

      n <- nrow(X)



      f1 <- function(xi) xi - mean(xi)

      apply(X, 2, f1) -> x



      f2 <- function(xi, t) rep(xi, t^2)

      f3 <- function(xi, t) rep(xi, t)



      x1 <- matrix( apply(x, 2, f2, t = t), n, t^3 )

      x2 <- matrix( apply(x, 2, f3, t = t) ,n, t^3 )

      x3 <- matrix( rep(x, t^2), n, t^3)



      res <- (1/n) * t(x) %*% (x1*x2*x3)



      return (res)



      }



evar <- function(

      w,                      # A vector of n weights

      M                       # A (n,n) covariance matrix

      )



      { # author: me!



      # Definition:

      # The evar() function returns the sample variance from the sample

      # covariance matrix (M) and a vector of weights (w).



      as.numeric(w) -> w



      if( !is.matrix(M) || nrow(M) != ncol(M) )

            stop('Argument M should be a squared matrix!\n')

      if( length(w) != ncol(R) )

            stop('Arguments w and R are not consistent!\n')



      as.matrix(w) -> w



      as.numeric( t(w)%*%M%*%w ) -> res



      return (res)



      }



eskew <- function(

      w,                      # A vector of n weights

      M                       # A (n,n^2) co-skewness matrix

      )



      { # author: me!



      # Definition:

      # The eskew() function returns the sample skewness from the sample

      # co-skewness matrix (M) and a vector of weights (w). Based on the

      # methodology proposed by Athayde and Flôres (2001).



      as.numeric( w %x% w ) -> w2



      if( !is.matrix(M) )

            stop('Argument M should be a matrix!\n')

      if( ncol(M) < nrow(M) )

            M <- t(M)

      if( ncol(M) != nrow(M)^2 )

            stop('Argument M should be a n x n^2 matrix!\n')

      if( length(w2) != ncol(M) )

            stop('Arguments w and R are not consistent!\n')



      as.matrix(w2) -> w2



      as.numeric( t(w) %*% M %*% w2 ) -> res



      return (res)



      }



ekurt <- function(

      w,                      # A vector of n weights

      M                       # A (n,n^3) co-kurtosis matrix

      )



      {

      # author: me!



      # Definition:

      # The eskurt() function returns the sample kurtosis from the sample

      # co-kurtosis matrix (M) and a vector of weights (w). Based on the

      # methodology proposed by Athayde and Flôres (2001).



      as.numeric( w %x% w %x% w ) -> w3



      if( !is.matrix(M) )

            stop('Argument M should be a matrix!\n')

      if( ncol(M) < nrow(M) )

            M <- t(M)

      if( ncol(M) != nrow(M)^3 )

            stop('Argument M should be a n x n^3 matrix!\n')

      if( length(w3) != ncol(M) )

            stop('Arguments w and R are not consistent!\n')



      as.matrix(w3) -> w3



      as.numeric( t(w) %*% M %*% wxwxw ) -> res



      return (res)



      }

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




                                                                                                                            
                                                                                                                            
                                                        Pour :   brian at braverock.com, r-sig-finance at stat.math.ethz.ch       
                                                        cc :                                                                
                                                        Objet :  Re: [R-SIG-Finance] assetsStat function Question           
             Joe Byers <ecjbosu at aol.com>                                                                                    
             Envoyé par :                                                                                                   
             r-sig-finance-bounces at stat.math.ethz.                                                                          
             ch                                                                                                             
                                                                                                                            
                                                                                                                            
             15/02/2007 17:13                                                                                               
                                                                                                                            
                                                                                                                            




brian at braverock.com wrote:
> On Thursday 15 February 2007 06:49, Joe W. Byers wrote:
>
>>> Hope it helps.
>>>
>> This helps a bunch and is what I was thinking about doing to modify
>> this function.  I was also trying to be tactful about the assetsStat
>> functions is clearly wrong and all the risk stats that use these two
>> variables are incorrect.
>>
>
> I also think the modifiedVaR calculation is wrong there, and doesn't take
> the higher moments   Our functions have a Modified Cornish-Fisher VaR
> calc that I think is more complete/correct.
>
>
>>> We're working on packing all our previously released functions now,
>>> but writing .Rd files, standardizing parameter names and ordering,
>>> and collecting sample data, examples, and tests is taking time.
>>>
>> Do you want some help? I might be able to help with docs, data (I have
>> a lot of energy data), and examples ( teach so I have some good ones ).
>>
>
> I'll do a little more cleanup by Saturday morning at the latest, and post
> the code and package tarball for you to take a look at.  In additiona to
> all the analytics functions, have quite a few chart and (text) table
> functions that collect performance statistics into groupings that we find
> useful.
>
> I am wondering whether you've ever done any thinking about co-skewness or
> co-kurtosis.  I had previously implemented and released functions for
> these, but I'm now pretty much convinced that they are incorrect (which
> also makes the standardized systematic functions that rely on them
> incorrect).  Help sorting that out would be greatly appreciated.
>
Brian,
First, I looked at your code and the calculations look good.  My
knowledge of the higher moment CAPM literature for these moments is that
the co-skewness is the return on asset i * the square of the return on
the portfolio. So your Ra and Ri may be reversed.  This I would clarify.

Second, I am not a theoretical statistician, so please bear that in mind
with the following statements.  Skewness for a single asset is the third
moment.  Co-skewness of 2 assets is also the third moment or could be
the second moment of the covariances.  The co-skewness is
mean((Ri-mean(Ri)*(Rj-mean(Rj)^2) but could also be the
mean((Rj-mean(Rj)*(Ri-mean(Ri)^2), could it not?  This means that for
every combination of assets there are 2 co-skewness terms and 2
co-kurtosis terms.  A matrix of co-skewness or co-kurtosis may not be
symmetric.  The higher moment CAPM uses asset j as the market and
investors are concerned with minimizing systematic skewness and kurtosis
meaning the first equation above it the co-skewness calculation.
Investors want little negative skewness with the market and small
positive kurtosis (see COVARIANCE, COSKEWNESS AND COKURTOSIS IN GLOBAL
REAL ESTATE SECURITIES Kim Hiang LIOW and Lanz C.W.J. CHAN, Department
of Real Estate, National University of Singapore).  I am not sure but
these seem to a choice of direction is required in measuring the co
moments.  The higher moment CAPM chooses the market return and
systematic skewness and kurtosis.  A hedged portfolio will choose the
hedging instrument to measure systematic skewness and kurtosis.

This is my thoughts and I have no empirical work on co-skewness or
co-kurtosis, but we do have R and the ability to download security
prices to look at these.  I know this probably does not help, but maybe
we will get some additional comments.

Good luck
Joe

> Regards,
>
>   - Brian
>
>


             [[alternative HTML version deleted]]

_______________________________________________
R-SIG-Finance at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance




Les informations contenues dans ce message sont confidentielles et peuvent constituer des informations privilegiees. Si vous n etes pas le destinataire de ce message, il vous est interdit de le copier, de le faire suivre, de le divulguer ou d en utiliser tout ou partie. Si vous avez recu ce message par erreur, merci de le supprimer de votre systeme, ainsi que toutes ses copies, et d en avertir immediatement l expediteur par message de retour.
Il est impossible de garantir que les communications par messagerie electronique arrivent en temps utile, sont securisees ou denuees de toute erreur ou virus. En consequence, l expediteur n accepte aucune responsabilite du fait des erreurs ou omissions qui pourraient en resulter.
--- ----------------------------------------------------- ---
The information contained in this e-mail is confidential. It...{{dropped}}



More information about the R-SIG-Finance mailing list