Showing posts with label Simulation. Show all posts
Showing posts with label Simulation. Show all posts

Saturday, 5 November 2011

Unit root versus breaking trend: Perron's criticism

I came across an ingenious simulation by Perron during my Time-series lecture which I thought was worth sharing. The idea was to put your model to a further test of breaking trend before accepting the null of unit root. Let me try and illustrate this in simple language.

A non-stationary time series is one that has its mean changing with time. In other words, if you randomly choose a bunch of values from the series from the middle, you would end up with different values of mean for different bunches. In short there is a trend in the data which needs to be removed to make it stationary and proceed with our analysis (its far easier to work with stationary timeseries). In order to deal with non-stationary time-series one has to be careful about the kind of non-stationarity that is exhibited by the variable. Two corrections for non-stationarity include fitting

(1) Trend stationary (TS) models, which are suitable for models that have a deterministic trend and fluctuations about that deterministic trend. This can be fit by a simple zt = a + bt + et where et ~ ARMA(p,q)

(2) Difference stationary (DS) models, which are suitable for models having a stochastic trend. The DS models are appropriate for models that have a unit root in the AR polynomial. Unit root in the AR polynomial means that the trend part in the series cannot be represented by a simple linear trend with time (a + bt). And the correct representation is (1 – B)zt = a + et, where et is i.i.d. 

The asymptotic properties of the estimates, forecasts and forecast errors vary substantially between the TS and DS models. (For the ones interested in the algebra behind this, lecture notes of Dr. Krishnan are here) Therefore it is important for us be sure that the model belongs to the appropriate class before we fit a TS or DS model. This is the reason why the clash between the two school of thoughts has bred enormous literature and discussions on the methodology to check for unit roots. One could try and endlessly argue about these discussions but I want to illustrate the genius of Perron who criticized the idea of fitting a DS model to series that could have a structural breaks. He said that you ought to take into account the structural break before you check for the unit roots, if you don't do so, you might end up accepting the null of unit root, even when the true data generating process (DGP) is a trend stationary process. He illustrated this using a simple, but very elegant, simulation exercise. Madhav and I, along with fine-tuning on the codes provided by Utkarsh, replicated this exercise with R.

The steps involved are as follows:
(1)   Simulate 1000 series with the DGP as:
 z­­t = u1  + (u2 – u1)DUt + bt + et  
where et ­are i.i.d innovations and t = 1,2,3,...100. For simplicity I have assumed b = 1 and u1 = 0.
(2)   Assume that there is a crash at time Tb = 50 and the entire series comes down by amount u2.

## Simulating a trend stationary model with a crash in the intercept ##
t <- c(1:100) # specifying the time
dummy <- as.numeric(ifelse(t <= 50, 0, 1)) # specifying the dummy for trend break at T = 50

z <- ts(t - 15*dummy + rnorm(100, mean = 0, sd = 3))# This is the trend stationary model with break in trend
x <- ts(t - 15*dummy) # This is just the trend line that we see in "red" in the plot below

plot(z, main = "Simulated series with break at T = 50")
lines(x, col = "red") ## Plotting a sample of the model that we have simulated


(3)   For these simulations compute the autoregressive coefficient, “rho” in the regression:
zt = u + bt + ‘rho’zt-1 + et
(4)   Plot the cumulative distribution function (c.d.f) of “rho” for different values of u2 (crash).

## Now we will simulate the sample data above 1000 times and check for unit roots for each of these samples ##

# For simplicity we define a function to generate the "rho's" for each of the simulated series

sim <- function(crash) ## Function name "sim"
d <- ts(t - crash*dummy + rnorm(100, mean = 0, sd = 3))
## saving the simulated series in "d"
trend <- lm(d ~ t) ## remove the trend from the
simulated series

# crash in the above function refers to the value of u2 in equation 1

res <- ar(ts(trend$residuals), order=1, aic= FALSE) ##
Fit an AR(1) model to the residue obtained after
detrending the series 

if(length(res$ar) < 1) 0 else res$ar ## Return the ar
coefficient of the fitted AR(1) model above.
}

## Generate "rho's" for different magnitude of crash by
simply using the sim() function defined above

temp1 <- replicate(n, sim(10))
temp2 <- replicate(n, sim(15))
temp3 <- replicate(n, sim(20))
temp4 <- replicate(n, sim(35))

## Sort the values of "rho", we do this to plot the CDF
as we will see shortly

temp1.1 <- sort(temp1)
temp2.1 <- sort(temp2)
temp3.1 <- sort(temp3)
temp4.1 <- sort(temp4)
y <- seq(from=0, to=1, length.out=n)## This is how I
define the y-axis of my CDF which are basically the
probabilities. 

## Plotting all the CDF of rho for different magnitude in one plot.

   plot(c(min(temp1.1), max(temp4.1)), c(0, 1), type='n',       xlab = "Rho", ylab= "Probability", main = "CDF of 'Rho' for   differnt magniturde of crashes")
   lines(temp1.1, y, type = 'l', col = 'red')
   lines(temp2.1, y, type = 'l', col = 'green')
   lines(temp3.1, y, type = 'l', col = 'blue')
   lines(temp4.1, y, type = 'l', col = 'black')
   b <- c("10 unit crash", "15 unit crash", "20 unit crash", "35 unit crash")

   legend("topleft", b , cex=0.5, col=c("red", "green", "blue", "black"), lwd=2, bty="n")







An interesting observation that we make (or rather Perron made) is that the c.d.f of our autoregressive coefficient “rho” tends more towards unity with increase in the magnitude in crash. What this means is that as the magnitude of crash increases the possibility of your accepting the (false) null of unit root increases. Why I say the false null is because I know the true DGP is a trend stationary one.

This idea of Perron was criticised on the ground that he was specifying the break point (Tb) exogenously, that is from outside the DGP. Frankly speaking I do not understand why was this taken as a criticism. I think fixing the break point exogenously was a good way of fixing it with an economic intuition and not making is a purely statistical exercise. Some researchers (I don’t understand why) termed this (simulation) illustration as a “data mining” exercise, and improved it by selecting the break point (Tb) endogenously (by Zivot and Andrews as mentioned in the lecture notes).

I would hate to impose my opinion here but I feel this was a very elegant and logical way of driving home the point that the null of unit root should be accepted for your sample if and only if your model stands the test of extreme rigour and not otherwise, and the rigour could be imposed exogenously with economic intuition too.

P.S. Perron did a similar simulation for breaking trend model, i.e where the slope of the model had a structural break. The codes would be quite similar to the ones given above, in fact it would be a good practice if you could do the similar simulation for a breaking trend. In case you do want to try but face any issues please feel free to post/email your queries.

Criticism and discussions welcome.  

Thursday, 3 November 2011

Modern Portfolio Optimization Theory: The idea

We were recently given a lecture (by Dr. Susan Thomas) on Harry Markowitz portfolio optimization theory, and I was really fascinating with the noble laureate's story of how he found it difficult to convince his guide about the importance of his thesis work. Little did anyone know that his thesis would get him the most respected award in academia 35 years down the lane. Let me try and illustrate what was the basic idea behind the modern portfolio theory in simple English language. The principal task of any financial advisory firm/individual is to try and solve the capital allocation problem of an agent, meaning how much money should be invested in what asset/security to get the desired rate of return with minimum risk. Lets try and visualize this problem of capital allocation from the individuals point of view. Suppose I want a 15% p.a rate of return on my investment and there is a security in the market that gives you an expected 15% annual rate of return with some risk(sigma or standard deviation) associated with that security/asset. One way for me to achieve the desired expected return is to invest in this security take the risk(sigma) associated with this security and hope that I am awarded for the risk I took and get a 15% return in the end. However, the beauty of financial markets is that since there are so many assets/securities being traded in the market, it is possible for me to diversify my investment, in simple words, park fraction of my money with different assets in a way that gives me approximately ~ 15% return but with substantially less risk. This is referred to as "diversification" in Finance parlance. The mathematics behind the results can be easily shown, one can find the maths in the lecture slides here. Let me try and illustrate this result through empirical simulation using R.

Suppose you are in a universe of only risky assets, i.e all assets that have a positive sigma. Then what Harry Markowitz did, with mathematics, was to compute the set of Expected returns that you could achieve with minimum risk (sigma). To illustrate this result I took stock returns for these 7 random companies for about 251 days, computed their respective expected returns, and the variance-co-variance matrix of the returns. With this I have all the necessary fodder to compute the E(r) and sigma for all the possible combination of weights that I can assign between these securities, meaning all possible different combinations of parking my money among these securities.

# Map your working directory using setwd()
# Read the relevant file
r1 <- read.csv("Markowitz.csv")


mu = colMeans(r1[,-7])  ## Calculate the column means and storing in "mu", I have removed the 7th column from the data frame as I have a risk free asset's return there, I will come back to this later in the post.


bigsig = cov(r1[,-7]) ## Variance-co-variance matrix


m = nrow(bigsig)-1
w = diff(c(0,sort(runif(m)), 1)); ## Assigning random weights between 0-1 to "w" which will have the dimension = no. of securities between which you have to divide your investment.


rb = sum(w*mu); ## Creating matrix "rb" which stores the E(r)


sb = sum(w*bigsig*w); ## Creating matrix "sb" which stores sigma's


N = 2000  ## Number of different combinations of "w" you want to look at


## Simulating the different combinations of weights "w's" and storing the E(r) and sigma^2

for (j in 2:N) {
w = diff(c(0,sort(runif(m)), 1));
r = sum(w*mu); rb = rbind(rb,r); 
s = sum(w*bigsig*w); sb = rbind(sb,s); ## Note this is sigma^2 (variance)
}

d = data.frame(rb, sb); ## Merge all the E(r) and sigmas in one data.frame.

d$sb = sqrt(d$sb); ## Square root the variance to get the sigmas

plot(d$sb, d$rb, ylab="E(r)", xlab="Sigma", col="blue",  xlim = c(0.5,10), ylim = c(-0.1,0.5), main = "E(r)- sigma (With risk free asset), N = 200000")


With only Risky assets



I did this simulation for 200000 different values of weights too, for those who are curious the plot can be obtained from here.


If we close the envelope of the above mentioned points with a smooth curve (from the left hand side) we get the efficient portfolio frontier (EFF). So what the graph suggests is that one would want to choose the weights (allocations) that lie, more specifically, on the above half of the curve. It makes intuitive sense too, why would one take on more risk to get a lesser return (which is implied by the lower half of the curve). Also another observation that we can make from the above plot is that for every additional unit of returns we will have to take on an increasingly higher risk (illustrated by the upper half of the plot). This risk-return trade off graph changes its characteristics when a risk free assets comes into the picture. Lets see what happens is we incorporate the risk free asset in our data frame. Note that a risk free assets could be thought of as a government security, whose risk (sigma) is 0, hence even its co-variances with the other risky assets would be 0. The only addition to the above codes would be to replace r1[,-7] with just r1 that is include the risk free asset too.




With risk-free asset


Simulation with N = 200000 here.


Look what happened to our risk-return trade off relationship, its linear!! So its a straight line instead of a smooth curve. Meaning for every additional unit of return you will have to take a fixed additional amount of risk, which will be given by the slope of the above half of the graph. This result, which sounds to be a trivial finding, can be extended to the entire universe of assets and one can compute the optimal portfolio that will give you a desired rate of return with minimum sigma. However the implementation of this methodology in the actual world is the real challenge. Computation of E(r), sigma, getting the up to date data for all the securities or assets in the universe are some of the challenges that one would face. But nevertheless it is inspiring to see how Harry Markowitz stuck to his idea and triumphed in the end with this path breaking research that shaped the financial advisory industry as we see it now.

P.S In case you want to replicate the exercise the data can be downloaded from here.