Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

FactorsRisk [UP]

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Principal Components and Risk Management

Michael Ashby

28 February 2019
Last time

I It was a while ago!


I Discussed what factor models were and why we’d use one
I PPP and backtest
I Lasso and elastic net to find tangency portfolio
Today

I Principal components analysis


I Risk measures: VaR and cVaR/ES/ETL
I Performance measures: max drawdown
References

I Principal components:
I Implementation: https://programming-r-pro-bro.blogspot.
com/2011/10/principal-component-analysis-use.html
I Gory mathematical detail: http://www.cs.princeton.edu/
picasso/mats/PCA-Tutorial-Intuition_jp.pdf
I Risk/performance measures:
I Implementation: PerformanceAnalytics reference manual
I Information on expectiles: Information - https:
//getd.libs.uga.edu/pdfs/anderson_andrew_l_201212_ms.pdf
Principal components analysis (PCA)
Factor models

I We said last time a factor model of returns may be given by

ri,t − rf ,t = αi + Ft βi + ei,t
I Fama-French: case where factors are known. We have a time
series for each one
I PCA: components not known, but extracted from the data
Idea of a factor model

I Factor model explains all systematic risk (can’t be diversified


away)
I All that’s left is the idiosyncratic (diversifiable) risk
I So the factor model must explain all the cross-sectional
correlations between assets
I If there were any correlation left over, the factor model would
be incomplete. There would be some systematic part of risk
(i.e. thing that causes two assets to move together) that it
didn’t explain
I Therefore the idiosyncratic risk must be uncorrelated across i
(they may or may not be correlated through time - that’s
another issue)
I Given all this, we can write a factor structure for returns and
use PCA to extract the factors (in theory!)
Factor structure of returns
I Given how we’ve defined a factor model, we can write the
return covariance matrix as

Σr = βΣF β | + Σe
where (assuming there are K factors and N assets), ΣF is the
K × K covariance matrix of the factors F and
Σe = diag{σ12 , σ22 , . . . , σn2 }
I Even if we treat this as a statistical factor model (loadings
unknown, factors unknown), the RHS above will typically have
far fewer parameters to estimate (nK + K + K (K2+1) ) than the
LHS N(N+1)
2
I e.g. even for smallish N (N = 50) and K = 10 (large factor
model) we have 1275 parameters in LHS and only 565 in RHS
I This type of shrinkage may be helpful in terms of a
mean-variance problem
Extracting factors in theory

I See F500 if you want the gory mathematical detail


I Given that LHS in

Σr = βΣF β > + Σe
has more parameters than RHS, we can’t uniquely identify β
and F
I We can, however, identify F and β up to a rotation
I So we choose a convenient rotation: we will make our factors
orthogonal to each other
I Question: if the factors are uncorrelated and the errors are
uncorrelated, how can the assets be correlated? Answer: . . .
Extracting factors by PCA
I Given that we have decided to make our factors orthogonal to
each other, we will use principal components on our returns
matrix R
I To do this, we will need to do an eigenvalue decomposition
(princomp() in R)
I This will give us N factors - not ideal!
I Luckily, the factors will be ordered by which one explains the
most variance
I We will first consider one factor, then two factors etc. Once
“enough” of the variance is explained, we can stop adding
factors
I We will come back to what is “enough” later
I Once we have the factors, we can estimate β and Σe (residual
vairances) by OLS time-series regressions (one for each asset)
as we did in the Fama-French case when the factors were
known (we treat the factors as known once we extract them)
Digression: in MLE, how to estimate Σe and β?

I MLE requires small N and large T so we won’t consider it - I


just saw this on Moodle
I In Σr = βΣF β > + Σe , first restrict ΣF to be I (to resolve the
non-uniqueness problem)
I Then do (Q)ML, assuming returns are normally distributed
I You will end up with non-linear FOCs for each element of β
and Σe = diag{σ12 , . . . , σN
2}

I These can be solved by some kind of iterative procedure


I Once you have β̂ the period-by-period factor realisations can
be estimated by cross-sectional regressions at each t
I Can either use OLS or GLS, since know error variances (or
you’ve estimated them anyway)
Extracting factors by PCA in R
Basic procedure:

1. Use princomp() to do eigenvalue decomposition on


covariance matrix of returns
2. Look at cumulative % of variance explained by each factor.
Choose number of factors on this basis
3. Extract the factors at each t (these will be a linear
combination of the underlying assets at each t so easy to
reconstruct from the original data)
4. Given the factors, estimate the betas for whatever assets or
portfolios we are interested in. This is just an asset-by-asset
time series regression and a step we will skip (basically we are
back in the Fama-French case when the factor realisations are
known)

Extension for the excerises: adjust for average time-series


heteroscedasticity (above approach assumes time series
homoscedasticity in the idiosyncratic errors)
Extracting factors in R - packages

I Load packages as normal

library(tidyverse)
library(reshape2)
library(lubridate)
library(tidyquant)
library(XLConnect)
library(XLConnectJars)
Extracting factors in R - data (DJIA)
prices=tq_index("dow") %>%
# complete_cases=TRUE gets rid of symbols returning
# NA due to error in get call (e.g. wrong symbol)
tq_get(get = "stock.prices", complete_cases = TRUE) %>%
# keep only variables listed inside select()
select(symbol, date, adjusted) %>%
# set cases as year, month and symbol
group_by(year(date), month(date), symbol) %>%
# for time-series variables, keep just the last
# observation of each month
summarize(adjusted = tail(adjusted, 1)) %>%
# make a date column with only years and months in
# for this new object
mutate(date = make_date(`year(date)`, `month(date)`)) %>%
#acast(data,id1~id2,value.var="x") returns a matrix
#of x where id1 describes the rows and id2 the cols
acast(date~symbol,value.var = "adjusted")
Extracting factors in R - eigendecomposition
I We need to remove NA values. We know how to do this from
last time. We also know that this particular dataset has no
missing values, so we keep going!
I It’s trivially easy to compute the PCA:

ret = diff(log(prices))
pca = princomp(ret)

I Now we can look at the proportio of variance explained using

summary(pca)

## Importance of components:
## Comp.1 Comp.2 Comp.3
## Standard deviation 0.2295718 0.09409044 0.08698306 0
## Proportion of Variance 0.4372084 0.07344167 0.06276549 0
## Cumulative Proportion 0.4372084 0.51065012 0.57341561 0
## Comp.5 Comp.6 Comp.7
plot(pca$sdev)
0.20
0.15
pca$sdev

0.10
0.05

0 5 10 15 20 25 30

Index
Extracting factors in R - how many factors?

I We need to choose the number of factors to use. This won’t


be easy as there is no clear break in the standard deviations
(there should be). Visually, I would say there appears to be a
kink after 5 factors, but note that a five-factor model model
will only explain 66% of the variance in returns
I That’s ok - what we are saying is that a large part of risk is
idiosyncratic
I You need 9 factors to explain 75% of the variance, 17 factors
to explain 90% and 21 to explain 95%. That would be getting
to why bother with a factor model territory!
I Anyway, let’s go with 5 factors
Extracting factors in R - reconstructing factors

I The factors are just a linear combination of the underlying


returns
I The weights for these combinations are stored in
pca$loadings
I Row i of pca$loadings relates to asset i
I Column j relates to factor j
I Factors are easy to compute. For factor 1:

f1 = rowSums(pca$loadings[1,]*ret)

I Can then plot against, e.g. market return, see if picking up


same information as known factors (or compute correlation
with market)
Risk measures
Background - VaR
I There is a lot more to risk than simply variance. We may be
particularly concerned about extreme losses
I We can look at measures that look at those extreme losses
I In common with the slides and convention we look at a
loss distribution
I So positive L is bad and negative L is good!
I Value at risk satisfies

Pr(L > VaRα ) = 1 − α


in continuous distributions, and in discrete ones

VaRα = inf{` ∈ R : Pr(L > `) ≤ 1 − α}.


I Clearly equivalent to the α quantile of the L distribution
I Tells us the minimum loss that occurs if the an outcome in the
worst 1 − α of the distribution happens
Background - cVaR/ES/ETL

I These are all the same thing!


I Similar to VaRα but looks at the expected loss given that the
loss exceeds VaRα , rather than the minimum loss
I We’ll call it ESα and define ESα as

ESα = E (L|L > VaRα )


Implementation in theory
I Since these risk measures all depend on a probability
distribution, we are going to have to:
I use the historical distribution, or
I assume a distribution, or
I estimate a distribution, or
I simulate a distribution
I We will look only at the first three cases
I If you’re interested in simulating a distribution, there are two
ways to do it:
I Monte Carlo: assume a (e.g. factor) model for returns and a
structure for the error term (e.g. GARCH(1,1)) and simulate M
return series to give you an empirical distribution of returns at
each t
I Bootstrap: re-sample historical returns. I personally recommend
the stationary bootstrap (re-shuffles your data in blocks of
random length, which preserves their serial
correlation/heteroscedasticity properties. You must be very
careful about the assumptions a bootstrap procedure makes
regarding your data)
Implementation in practice

I Option 1: use historical distribution


I Compute empirical CDF of returns to a given asset/portfolio
based on history
I Use that!
I Will not be able to capture “unprecedented” events
I Option 2: assume a distribution - Gaussian VaR
I Just a terrible idea
I Option 3: approximate a distribution - Cornish-Fisher expansion
I In short, approximate a non-normal distribution by a normal
one with some skewness and kurtosis corrections
I Won’t work if “very” non-normal: i.e. very high excess
kurtosis/skewness
I This approach seems to have nonetheless gained favour with
regulators
Implementation in R I

I Use the PerformanceAnalytics package:

library(PerformanceAnalytics)

I Let’s look at a portfolio of the 1st 4 stocks in the alphabetical


order of ticker with weights 0.4, 0.3, 0.2, 0.1

pret = 0.4*ret[,1]+0.3*ret[,2]+0.2*ret[,3]+0.1*ret[,4]
Implementation in R II
I We’ll compute the Cornish-Fisher 95% VaR

VaR(R=pret,p=0.95,method=c("modified"))

## [,1]
## VaR -0.06957187

ES(R=pret,p=0.95,method=c("modified"))

## [,1]
## ES -0.09684417

I Change method to method=c("gaussian") for assuming


Normal distribution or method=c("historical") for regular
VaR from historical data
Component VaR

I If we want to know the contribtion and % contribution of each


asset to risk, we can put the vectors of returns and weight into
the VaR() (or ES()) function and change the portfolio method
to portfolio_method=c("component")
I Notice now that our VaR comes back multiplied by -1: just an
oddity of the package!
VaR(as.data.frame.matrix(ret[,1:4]),method=c("modified"),
portfolio_method=c("component"),
weights=c(0.4,0.3,0.2,0.1))

## $MVaR
## [1] 0.0701611
##
## $contribution
## AAPL AXP BA CAT
## 0.030492149 0.012802774 0.019668320 0.007197861
##
## $pct_contrib_MVaR
## AAPL AXP BA CAT
## 0.4346019 0.1824768 0.2803308 0.1025905
Expectiles

I We can also look at expectiles. These reflect information in


both tails of the distribution but weight the tails
asymmetrically.

n h io
eτ (L) = arg min E τ 1(L > `)(L − `)2 + (1 − τ )1(L < `)(L − `)2
`∈R

I I won’t go into the properties in detail, but it suffices to say


that expectiles are sub-additive (ρ(L1 + L2 ) ≤ ρ(L1 ) + ρ(L2 )),
unlike VaR, and elicitable (see Emmer, Kratz and Tache, 2015,
in required reading folder)
Implementation in R - historical expectile

I Use expectile() in expectreg package. Note you will need


to install BayesX and mboost then download expectreg from
the CRAN archive and install manually (Install > Install from
package archive file > wherever you saved the downloaded zip
file from the CRAN archive)

library(expectreg)
expectile(-pret,0.95)

## 0.95
## 0.0495
Implementation in R - theoretical expectile

I Since eτ (aL + b) = aeτ (L) + b, we can use the theoretical


expectiles to back out our theoretical expectiles
I enorm(0.95) gives the theoretical τ = 0.95 expectile for a
standard normal distribution
I Assuming our losses (−1 × returns) have a normal (!)
distribution Z = Ltσ−µ
L
so Lt = µ + Z σL
I Therefore our theoretical 95% expectile for losses assuming a
normal distribution is

mean(-pret)+ sd(-pret)*enorm(0.95)

## [1] 0.04947906
Performance measures
Performance measures

I PerformanceAnalytics computes a huge variety of these


I We’ll look at the ones in the notes:
I Information ratio
I Sortino ratio
I Drawdown/max drawdown
I Calmar ratio
Information ratio

ra − rb /sd(ra − rb )

ra are the returns to an active strategy and rb returns to a benchmar

I Can be interpreted as active premium ÷ tracking error


I Let’s continue with our 4 asset portfolio and compare it to an
equally weighted portfolio

bench = 0.25*(ret[,1]+ret[,2]+ret[,3]+ret[,4])
InformationRatio(pret,bench)

## [1] 0.3761627
Sortino ratio
I Suppose an investor has a minimum acceptable return in mind
I Then we can compute the Sortino ratio which only penalises
variance in returns less than the MAR
I This is unlike the Sharpe ratio, which penalises both upside and
downside risk equally

r̄p − rMAR
SortR =
σd
v
u T
u 1 X
σd = t (rp,t − rMAR )2 1(rp,t < rMAR )
T − 1 t=1
I Very straightforward in R (we’ll set rMAR = 0)

SortinoRatio(pret)

## [,1]
## Sortino Ratio (MAR = 0%) 0.6195425
(Max) drawdown
I Drawdown at time t tells us how far below previous peak rp,t is
(and is 0 if above peak)
I We can get a whole time series of drawdowns for our return
series using

Drawdowns(pret)

I Notice the drawdowns will be given as negative numbers


I Max DD is maximum of all the drawdowns (in terms of
absolute value)
I Highest loss from peak to trough
I This will be given as a positive number (so maximum loss)
I Again, straightforward to compute

maxDrawdown(pret)

## [1] 0.2691596
Calmar ratio

I The Calmar ratio gives the cumulative return over the whole
period ÷ max DD
I Surprise, surprise, we calculate it as

CalmarRatio(pret)

## [,1]
## Calmar Ratio 0.9360109
That’s all folks!

I Hope you’ve enjoyed the course


I Reminder: the period to complete the project is Friday 8 -
Friday 22 March
I You are not allowed to contact me (or the lecturer) in that
time regarding the project or any material related to the course
I If you have questions about the project itself (e.g. clarification
on the questions, unsure about submission details/rules/etc)
please contact the Graduate Office

You might also like