Sunday, December 23, 2012

Evolution of Natural-logged Financial Assets Return

I have some engagement over the break, and while most of the analysis have been churned out by R after some programming, it will be some time before I  could write about them.

For now, enjoy the clip. 


Friday, December 21, 2012

Financial Assets Return Correlation Heatmap

Diversification is a timeless advice in wealth management. Even Antonio (from Shakespeare's Merchant of Venice) quipped:
"My ventures are not in one bottom trusted,
Nor to one place; nor is my whole estate
Upon the fortune of this present year;
Therefore, my merchandise makes me not sad." 

In contrast, David Wilson(from Mark Twain's Pudd'nhead Wilson) declared, "Put all your eggs in the one basket, and - WATCH THAT BASKET."

The quantitative revolution had shaped much of today's financial landscape, yet such contrasting financial proverbs have not been swept away. While Harry Markowitz reshaped the way people think about and manage risks, the counterpoint of Antonio's and Wilson's remark still dazzles us in a different guise.

Is tactical asset allocation a good way to manage risk? What is the most efficient portfolio? Should it consist of the entire stock market within a nation's border? A global portfolio of stock markets? A portfolio across all assets classes imaginable? 

The Global Financial Crisis of 2007-2008 had made me reflect on such questions deeply. In this series of posts I attempt to answer the questions above. This exploration will be done in a model-free and dynamic framework by:
1) Decomposing the eigenvalues/vectors of the correlation matrix of daily natural-logged returns of financial assets. The variation that can be explained by projecting the data in the direction of the first eigenvector is in a sense, the analog of the Beta that investors should think hardly about.
2) Building Asset Price Stress Indices to compare the observed change in asset prices relative to its history.
3) Tracking the evolution of the correlation matrix with heatmaps. 

As a sneak peek, here is the correlation heatmap of daily natural-logged returns across 82 financial assets for the period 6/1/2006-14/12/2012.

The heatmap is graphed with the ggplot2 package in R. 
The packages required are, ggplot,reshape2,scales,and R color Brewer.

Here's a sample script, inspired by this question in Stackoverflow.
library(ggplot2)
library(reshape2) 
library (scales) 
library(RColorBrewer)
Mat<-as.data.frame(Mat)
Matm<-as.data.frame(Mat)
Matm<-data.frame(row=rownames(Matm),Matm) 
Matm.m<-melt(Matm)
Matm.m$value<-cut(Matm.m$value,breaks=c(1,0.75,0.5,0.25,0,-0.25,-0.5,-0.75,-1),include.lowest=TRUE,label=c("(-0.75,-1)","(-0.5,-0.75)","(-0.25,-0.5)","(0,-0.25)","(0,0.25)","(0.25,0.5)","(0.5,0.75)","(0.75,1)")) 
Matm.m$row<-factor(Matm.m$row,levels=rev(unique(as.character(Matm.m$variable))))
po.nopanel<-list(opts(panel.background=theme_blank(),panel.grid.minor=theme_blank(),panel.grid.major=theme_blank()))
jpeg("histheatmap.jpeg",height=1000,width=1000)
ggplot(Matm.m,aes(row,variable))+geom_tile(aes(fill=value),colour="black")+scale_fill_brewer(palette="YlOrRd",name="Correlation")+opts(axis.text.x=theme_text(angle=-90))+po.nopanel
dev.off()

As an aside, to download the packages above, type the following command in your R prompt.
>install.packages('ggplot',dep=TRUE) 
Likewise for the other packages. 

Wednesday, December 19, 2012

A Tutorial in Graphing in R

Found an excellent tutorial from Isomorphisems for plotting graphs in R. This really showcases R's graphing ability, and is definitely of publishing quality.

Here's the URL.

R's learning curve can be steep, but it is immensely rewarding.

Saturday, November 10, 2012

Bootstraping Scripts

Now that we have wrapped up Chapter 4 in class, we're all required to perform bootstrap simulations for the problem set. Econometrics is a class that requires a lot of commitment, not only on grasping the materials and going through the painful algebra, but also in terms of learning how to programme in R.

In the spirit of the last few posts, here then, is the script of loops that I had written to complete the bootstrap simulations, both parametric and non-parametric.

P/S: To anyone who is following this blog faithfully, I swear I will be get back to writing when Winter break begins.

#Simulating DGP under the Null
lmnull<-lm(cs$V2~cs$V3)
#Use the estimate under the null to begin bootstrap.  

Loops for Parametric Bootstrapping

#For Bootstrap Size 99
BST99<-numeric(0)
for(i in 1:99){
       simdtb<-numeric(0)
simdtb<-rnorm(50,mean=0,sd=0.1384)
ybs<-numeric(0)
ybs<-(5.3426+(1.3234*cs$V3)+simdtb)
bsolsi<-lm(ybs~cs$V3+cs$V4)
B3simT<-summary(bsolsi)$coefficients["cs$V4","t value"]
BST99[i]<-B3simT}
#Notice that I applied the absolute function here. We are doing a 2-tailed test,and thus it is important that we apply the absolute operator on the entire
vector. We'd want extreme values that are too large or too small, and then be able to discriminate statistically. 
BST99<-abs(BST99)
#To get R to calculate the frequency of X greater or equal to a specified values while implicitly using a logical argument.A very useful trick.
PBST99<-mean(BST99>=2.349)

#For Bootstrap Size 999
BST999<-numeric(0)
for(i in 1:999){
       simdtb<-numeric(0)
simdtb<-rnorm(50,mean=0,sd=0.1384)
ybs<-numeric(0)
ybs<-(5.3426+(1.3234*cs$V3)+simdtb)
bsolsi<-lm(ybs~cs$V3+cs$V4)
B3simT<-summary(bsolsi)$coefficients["cs$V4","t value"]
BST999[i]<-B3simT}
BST999<-abs(BST999)
PBST999<-mean(BST999>=2.349)

#For Bootstrap Size 9999
BST9999<-numeric(0)
for(i in 1:9999){
       simdtb<-numeric(0)
simdtb<-rnorm(50,mean=0,sd=0.1384)
ybs<-numeric(0)
ybs<-(5.3426+(1.3234*cs$V3)+simdtb)
bsolsi<-lm(ybs~cs$V3+cs$V4)
B3simT<-summary(bsolsi)$coefficients["cs$V4","t value"]
BST9999[i]<-B3simT}
BST9999<-abs(BST9999)
PBST9999<-mean(BST9999>=2.349)



Loops for Non-Parametric Bootstrapping 
# Extracting residuals from the OLS regression under the null, and then scaling them by (n/n-k)^(1/2) where k is the number of restrictions under the null.
nullres<-residuals(lmnull)
scalednullres<-(sqrt(50/48))*nullres
#For Non-Parametric Bootstrap Size 99
BSTNP99<-numeric(0)
for(i in 1:99){
simds<-numeric(0)
simds<-sample(scalednullres,50,replace=TRUE)
ynpbs<-numeric(0)
ynpbs<-(5.3426+(1.3234*cs$V3)+simds)
bolsnpi<-lm(ynpbs~cs$V3+cs$V4)
B3NPSimT<-summary(bolsnpi)$coefficients["cs$V4","t value"]
BSTNP99[i]<-B3NPSimT}
BSTNP99<-abs(BSTNP99)
PBSTNP99<-mean(BSTNP99>=2.349)

#For Non-Parametric Bootstrap Size 999
BSTNP999<-numeric(0)
for(i in 1:999){
simds<-numeric(0)
simds<-sample(scalednullres,50,replace=TRUE)
ynpbs<-numeric(0)
ynpbs<-(5.3426+(1.3234*cs$V3)+simds)
bolsnpi<-lm(ynpbs~cs$V3+cs$V4)
B3NPSimT<-summary(bolsnpi)$coefficients["cs$V4","t value"]
BSTNP999[i]<-B3NPSimT}
BSTNP999<-abs(BSTNP999)
PBSTNP999<-mean(BSTNP999>=2.349)

#For Non-Parametric Bootstrap Size 9999
BSTNP9999<-numeric(0)
for(i in 1:9999){
simds<-numeric(0)
simds<-sample(scalednullres,50,replace=TRUE)
ynpbs<-numeric(0)
ynpbs<-(5.3426+(1.3234*cs$V3)+simds)
bolsnpi<-lm(ynpbs~cs$V3+cs$V4)
B3NPSimT<-summary(bolsnpi)$coefficients["cs$V4","t value"]
BSTNP9999[i]<-B3NPSimT}
BSTNP9999<-abs(BSTNP9999)
PBSTNP9999<-mean(BSTNP9999>=2.349)

Wednesday, October 24, 2012

Now that was an embarassment, loops are very inefficient

R's learning curve can be very steep, and thankfully I am surrounded by intelligent and smart colleagues. The point is that the loop script in the last post is embarrassingly inefficient, and will work horribly if one intends to simulate a large sample.

Here's a much more elegant solution, for 3.1 of EMT,but this time using the function argument in R.The script is provided by Evan Jo.


ARsample<-function(beta1=1,beta2=.8,n=25,y0=0){
y<-vector(length=n+1);y[1]<-y0
for (i in 2:n+1){
y[i]<-beta1+beta2*y[i-1]+rnorm(1)}

m<-embed(y,2);return(data.frame(y=m[,1],y1=m[,2]))};

meanbeta<-function(samplesize=25,n=100){
m<-data.frame(beta1=NA,beta2=NA);
for(i in 1:n){m[i,]<-coef(lm(y~y1,data=ARsample(n=samplesize)))}
return(colMeans(m));}

In particular look at how efficient this script is compared to the previous one.

And finally, this is why the honours stream is so much more intellectually rewarding to be in.

PS: Don't worry, I will still be writing about economics, and hopefully when Quantdary takes off we'll start having more interesting economics/finance related posts.

Saturday, October 20, 2012

Simulation Script for ETM Question 3.1(Public Service)

I promised I'll update my blog, but I have been really really busy. Anyway, this post is not about the KLCI as promised(sorry to disappoint,I hardly had time for myself either), but rather, it is a loop for Q3.1 in Davidson and McKinnon's Econometric Theory and Methods. For all the poor souls who have just started learning R.




B1<-1
B2<-0.8
y0<-0
B1hat<-numeric(0)
B2hat<-numeric(0)

for(j in 1:100){
residualsj<-numeric(0)
residualsj<-rnorm(200,0,1)
ysimj<-y0
for(i in 1:200){
ynew<-B1+B2*ysimj[i]+residualsj[i]
ysimj<-c(ysimj,ynew)
}
OLSj<-lm(ysimj[2:201]~ysimj[1:200])
beta1j<-coefficients(OLSj)[1]
beta2j<-coefficients(OLSj)[2]
B1hat[j]<-beta1j
B2hat[j]<-beta2j
}


Time permitting I'll write an explanatory note with the #, although I hardly doubt it with midterms and a lot of other deadlines vying for whatever precious time I have. To change the sample size just change the numbers in the "i" part of the loop. To change the number of runs, just change the numbers in the j part of the loop.

Update:I made some errors in the previous post and those have since be corrected. Note that this script is meant to simulate an OLS drawing from 200 observations 100 times, and feel free to tweak around to adjust the trials and sample size.

This should also make it easier for you to attempt the last 2 questions. For information on how to extract vectors out from a data frame and then transforming it, I'd refer you to Mirza Trokic's website.
http://www.mirzatrokic.ca/aes.html

He has the most concise and helpful R guide that I have ever seen.


Sunday, April 15, 2012

Orszag on the secondary effects of Food Stamp programmes

Readers of this blog may be familiar with my affinity for food stamp programmes. Not only have I argued in a previous post that this is the holy grail of automatic stabalisers, but I also feel it is a social safety net with incontrovertible utility.

Peter Orszag (former chairman of the Office of Management and Budget for President Obama) recently wrote an opinion piece for Bloomberg about the secondary effects of Food Stamp programmes on students.

As a student of econometrics and behavioural economics I am inspired by the work that has been done to quantify the impacts of such programmes.

P/S: Due to the rigour of the my academic programme I have not updated my blog as frequently as I had hoped. As a sneak peek, the next post has to do with the KLCI Index, so Malaysian readers interested in equities might find it interesting.