Sunday, January 27, 2013

Quantdary's Softlaunch

I have officially soft-launched Quantdary.

The entire (fatigued) editorial team have informed that a soft-launch is preferred, lest we fail to live up to the momentum we have generated. For now, you can access our bi-weekly featured articles here:
http://quantdary.wordpress.com/featured-article/

Ivan is working on revamping the site to enhance the aesthetics, and we'll try to expand the standard features if we have enough recruits to allow us to focus on product development. 
We'd be delighted to garner your feedback! 

Tuesday, January 1, 2013

PSA for Netroots

I am more than happy to lend this blog space to Netroots, a student-led social media consultancy I am involved with. The membership consists of students with a diverse range of background, and consultation and strategy execution is free.


Netroots has worked with a couple of non-profit entities, be it to establish their online presence, augment their fund-raisers, enhance information dissemination or improving their internal efficiency through the adoption of specialised social media platforms.

If you are involved with a non-profit entity that could use our help, feel free to send me a message, or email netroots.exec@gmail.com.
Also, check out our success stories page for a representative list of satisfied non-profit entities we have helped.
http://www.netrootsmontreal.com/#!success-stories

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.