Tuesday, June 16, 2015

[9] Plot: Time Series (Quartile Excess Return)

Create the following r code.


QTLR_F.r

# Portfolio Management with the R Programming Language
# http://pmwr.blogspot.com
#
QTLR_F <- function(
                df,   # data flame
                rtn,  # column: monthly total return
                tmh,  # column: mm/dd/yyyy (month end date)
                qtl,  # column: alpha score quartile (from 1:highest to 4:lowest)
                QQQ  # specify one quartile; 1, 2, 3, or 4
                )
{
  # aggregate (column_data_to_aggregate, column_data_aggregation_unit, FUN=mean:calc average, na.rm=TRUE:remove_NA)
  all.agg_mean <<- aggregate(df[,rtn],df[c(tmh)],FUN=mean,na.rm=TRUE) # average monthly total returns of the whole investment universe
  
  quartile.agg_mean.name <<- c("dmy")


  if(length(qtl) == 1){ # if qtl specifies only one column

    quartile <<- subset(df,df[,qtl] == QQQ)       # extract all the data with a specified quartile (1, 2, 3, or 4)
    quartile.agg_mean.name[1] <<- names(df)[qtl]  # alpha score column name like "ALPHA"
    
  }
  else {
  
    printf("Error: specify only one quartile.")
       
  }


  quartile.agg_mean <<- aggregate(quartile[,rtn],quartile[c(tmh)],FUN=mean,na.rm=TRUE)  # average monthly total returns for a specified quartile group


  tmp.all <-  xts(
                zoo(
                  subset(all.agg_mean[,2],all.agg_mean[,2]!= "DUMMY"),  # all.agg_mean[,2]: monthly total returns of the whole investment universe
                  as.Date(as.character(subset(all.agg_mean[,1],all.agg_mean[,2]!="DUMMY")), # all.agg_mean[,1]: mm/dd/yyyy
                  format='%m/%d/%Y')
                )
              )


  tmp.quartile <- xts(
                zoo(
                  subset(quartile.agg_mean[,2],quartile.agg_mean[,2]!="DUMMY"), # quartile.agg_mean[,2]: monthly total returns of the specified quartile
                  as.Date(as.character(subset(quartile.agg_mean[,1],quartile.agg_mean[,2]!="DUMMY")), # quartile.agg_mean[,1]: mm/dd/yyyy
                  format='%m/%d/%Y')
                )
              )


  tmp.comb <<- merge(tmp.all,tmp.quartile)
  tmp.comb.a <- tmp.comb[,2] - tmp.comb[,1] # TR_MEAN(specified quartile) - TR_MEAN(whole universe)
  tmp.comb.b <- tmp.comb.a


  for(i1 in 1:nrow(tmp.comb.b)){
    if(is.na(tmp.comb.b[i1,])==TRUE) tmp.comb.b[i1,]=0  # If a monthly total return for a specified quartile is not available, then it is regarded as zero.
  }
  
  quartile.agg_mean.dif <<-  tmp.comb.b
  quartile.agg_mean.dif.cum <<- cumsum(quartile.agg_mean.dif)


}              




Plot: Cumulative Quartile Excess Return (x axis: yyyy format)

Create the following monthly tab-separated file for the investment universe (TOPIX, Japan Equity index in this case).

sample code(s)
source ("setup.r")
source ("QTLR_F.r")

# JP_EQ_ALPHA_SCORE.txt is a tab-separated text file which contains (1) monthly total returns of each stock and (2) times series monthly data from 1/29/1999 to 5/29/2015. Details to follow.
TSV <- read.delim("JP_EQ_ALPHA_SCORE.txt")

# Only stocks with the market cap ranking <= 800 (1 is the largest.)
TSV2 <- subset(TSV,TSV[,22]<=800)

names(TSV)

 [1] "Identifier"                        
 [2] "Name"                              
 [3] "Period..Formatted."                
...
 [6] "Universe.Returns"                
...            
[10] "ALPHA"                            
...                  
[22] "MKT.CAP.RANK"                      
...


sample code(s)
# 1:5 show first to fifth row.
#
# c() 1 SEDOL code
# c() 2 Name
# c() 6 monthly total absolute return (%)
# c() 3 mm/dd/yyyy
# c() 10 quartile of Alpha score (1 the highest/most attractive to 4 the lowest/least attractive)
# c() 22 market cap ranking (1 is the largest)

TSV[1:5,c(1,2,6,3,10,22)]

> TSV[1:5,c(1,2,6,3,10,22)]
  Identifier                                     Name Universe.Returns Period..Formatted. ALPHA MKT.CAP.RANK
1   CASH_JPY                             Japanese Yen         0.000000          1/29/1999    NA           NA
2     686803                           SUZUTAN CO LTD        -2.013421          1/29/1999     1         1239
3     687012                      Mitsui Sumitomo NPV        -6.500000          1/29/1999     3          124
4     648390  Kanto Natural Gas Development Co., Ltd.        -6.185567          1/29/1999     3          713
5     689476 Tokio Marine and Fire Insurance Co. Ltd.        -3.787881          1/29/1999     4           22



sample code(s)
TSV2[1:5,c(1,2,6,3,10,22)]

ntifier                                     Name Universe.Returns Period..Formatted. ALPHA MKT.CAP.RANK
3     687012                      Mitsui Sumitomo NPV        -6.500000          1/29/1999     3          124
4     648390  Kanto Natural Gas Development Co., Ltd.        -6.185567          1/29/1999     3          713
5     689476 Tokio Marine and Fire Insurance Co. Ltd.        -3.787881          1/29/1999     4           22
6     600739                        Aderans Co., Ltd.         6.727278          1/29/1999     4          329
7     601004              Aica Kogyo Company, Limited         0.815213          1/29/1999     1          795




Run the following r code.

sample code(s)

QTLR_F(TSV2,6,3,10,1)
gf <- quartile.agg_mean.dif.cum

QTLR_F(TSV2,6,3,10,2)
gf <- cbind(gf,quartile.agg_mean.dif.cum)

QTLR_F(TSV2,6,3,10,3)
gf <- cbind(gf,quartile.agg_mean.dif.cum)

QTLR_F(TSV2,6,3,10,4)
gf <- cbind(gf,quartile.agg_mean.dif.cum)

plot.zoo(gf,plot.type="single",col=1:10,ylim=c(-150,150),lwd=3)
grid()

legend("topleft",col=1:10,lty=1,legend=c("Q1","Q2","Q3","Q4"))




Plot: Cumulative Quartile Excess Return Spread (x axis: yyyy format)

Run the following r code.

sample code(s)


QTLR_F(TSV2,6,3,10,1)
gf1 <- quartile.agg_mean.dif.cum

QTLR_F(TSV2,6,3,10,4)
gf4 <- quartile.agg_mean.dif.cum

gf14sprd <- cbind(gf1, gf4, gf1 - gf4)

plot.zoo(gf14sprd,plot.type="single",col=1:10,ylim=c(-200,200),lwd=3)

legend("topleft",col=1:10,lty=1,legend=c("Q1","Q2","Q1-Q4")




No comments:

Post a Comment