riskDecomp: Decompose Risk into individual factor contributions

View source: R/riskDecomp.R

riskDecompR Documentation

Decompose Risk into individual factor contributions

Description

Compute the factor contributions to Sd, VaR and ES of returns based on Euler's theorem, given the fitted factor model.

Usage

riskDecomp(object, ...)

## S3 method for class 'tsfm'
riskDecomp(
  object,
  risk,
  weights = NULL,
  portDecomp = TRUE,
  p = 0.05,
  type = c("np", "normal"),
  factor.cov,
  invert = FALSE,
  use = "pairwise.complete.obs",
  ...
)

## S3 method for class 'ffm'
riskDecomp(
  object,
  risk,
  weights = NULL,
  portDecomp = TRUE,
  factor.cov,
  p = 0.05,
  type = c("np", "normal"),
  invert = FALSE,
  ...
)

Arguments

object

fit object of class tsfm, or ffm.

...

other optional arguments passed to quantile and optional arguments passed to cov

risk

one of "Sd" (Standard Deviation) or "VaR" (Value at Risk) or "ES" (Expected Shortfall)

weights

a vector of weights of the assets in the portfolio, names of the vector should match with asset names. Default is NULL, in which case an equal weights will be used.

portDecomp

logical. If True the decomposition of risk is done for the portfolio based on the weights. Else, the decomposition of risk is done for each asset. Default is TRUE

p

tail probability for calculation. Default is 0.05.

type

one of "np" (non-parametric) or "normal" for calculating Es. Default is "np".

factor.cov

optional user specified factor covariance matrix with named columns; defaults to the sample covariance matrix.

invert

a logical variable to choose if change ES to positive number, default is False

use

an optional character string giving a method for computing factor covariances in the presence of missing values. This must be (an abbreviation of) one of the strings "everything", "all.obs", "complete.obs", "na.or.complete", or "pairwise.complete.obs". Default is "pairwise.complete.obs".

Value

A list containing

portES

factor model ES of portfolio returns.

mES

length-(K + 1) vector of marginal contributions to Es.

cES

length-(K + 1) vector of component contributions to Es.

pcES

length-(K + 1) vector of percentage component contributions to Es.

Where, K is the number of factors.

Author(s)

Eric Zivot, Yi-An Chen, Sangeetha Srinivasan, Lingjie Yi and Avinash Acharya

See Also

fitTsfm, fitFfm for the different factor model fitting functions.

portSdDecomp for factor model Sd decomposition. portVaRDecomp for factor model VaR decomposition.

Examples

# Time Series Factor Model
data(managers, package = 'PerformanceAnalytics')
fit.macro <- FactorAnalytics::fitTsfm(asset.names=colnames(managers[,(1:6)]),
                     factor.names=colnames(managers[,(7:9)]),
                     rf.name=colnames(managers[,10]), data=managers)
decompSd <- riskDecomp(fit.macro,risk = "Sd")
decompVaR <- riskDecomp(fit.macro,invert = TRUE, risk = "VaR")
decompES <- riskDecomp(fit.macro,invert = TRUE, risk = "ES")
# get the component contribution

# random weights 
wts = runif(6)
wts = wts/sum(wts)
names(wts) <- colnames(managers)[1:6]
portSd.decomp <- riskDecomp(fit.macro, wts, portDecomp = TRUE, risk = "Sd")
portVaR.decomp <- riskDecomp(fit.macro, wts, portDecomp = TRUE, risk = "VaR")
portES.decomp <- riskDecomp(fit.macro, wts, portDecomp = TRUE, risk = "ES")

# Fundamental Factor Model
data("stocks145scores6")
dat = stocks145scores6
dat$DATE = zoo::as.yearmon(dat$DATE)
dat = dat[dat$DATE >=zoo::as.yearmon("2008-01-01") & dat$DATE <= zoo::as.yearmon("2012-12-31"),]


# Load long-only GMV weights for the return data
data("wtsStocks145GmvLo")
wtsStocks145GmvLo = round(wtsStocks145GmvLo,5)  
                                                     
# fit a fundamental factor model
exposure.vars = c("SECTOR","ROE","BP","PM12M1M","SIZE", "ANNVOL1M", "EP")
fit.cross <- fitFfm(data = dat, 
              exposure.vars = exposure.vars,
              date.var = "DATE", 
              ret.var = "RETURN", 
              asset.var = "TICKER", 
              fit.method="WLS", 
              z.score = "crossSection")
              
decompES = riskDecomp(fit.cross, risk = "ES") 

#get the factor contributions of risk 
portES.decomp = riskDecomp(fit.cross, weights = wtsStocks145GmvLo, risk = "ES", portDecomp = TRUE)  

braverock/factorAnalytics documentation built on Dec. 16, 2024, 1:05 p.m.