Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
systematicinvestor
ok
file 336 lines (291 sloc) 10.256 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
###############################################################################
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
# Collection of Utilities to Biuld and Analyze Multiple Factor Model
# Copyright (C) 2012 Michael Kapler
#
# For more information please visit my blog at www.SystematicInvestor.wordpress.com
# or drop me a line at TheSystematicInvestor at gmail
###############################################################################



###############################################################################
# Count Consecutive Changes
#' @export
###############################################################################
consecutive.changes <- function
(
data, # data series
positive=T # count positive consecutive changes
)
{
if(positive) dir = diff(data) > 0 else dir = diff(data) < 0

temp = cumsum(iif(dir, 1, 0))
temp - ifna.prev(iif(dir, NA, coredata(temp)))
}

###############################################################################
# Create plot of factors average correlations and returns
###############################################################################
# http://stackoverflow.com/questions/4310727/what-is-rs-multidimensional-equivalent-of-rbind-and-cbind
# apply(temp, 3, rbind)
# http://r.789695.n4.nabble.com/Collapse-an-array-td850008.html
#' @export
factor.avgcor <- function(data, next.month.ret, name) {
load.packages('abind')
# create matrix
temp = abind(data, along = 3)
temp = abind(next.month.ret, temp, along = 3)
dimnames(temp)[[3]][1] = 'Ret'

# plot
temp = t(compute.avgcor(temp, 'spearman')[,-1])
temp[] = plota.format(100 * temp, 0, '', '%')
plot.table(temp, smain=paste(name,'Correlation',sep=' \n '))
}

###############################################################################
# Compute average correlations
#' @export
###############################################################################
compute.avgcor <- function
(
data, # matrix with data: [rows,cols,factors]
method = c('pearson', 'kendall', 'spearman')
)
{
nr = dim(data)[1]
nc = dim(data)[3]
corm = matrix(NA,nc,nc)
colnames(corm) = rownames(corm) = dimnames(data)[[3]]

for( i in 1:(nc-1) ) {
for( j in (i+1):nc ) {
corm[i,j] = mean( as.double( sapply(1:nr, function(t)
try(cor(data[t,,i], data[t,,j], use = 'complete.obs', method[1]),TRUE)
)), na.rm=T)
}
}
return(corm)
}

###############################################################################
# Compute Market Cap weighted mean
#' @export
###############################################################################
cap.weighted.mean <- function
(
data, # factor
capitalization # market capitalization
)
{
capitalization = capitalization * (!is.na(data))
weight = capitalization / rowSums(capitalization,na.rm=T)
rowSums(data * weight,na.rm=T)
}

###############################################################################
# Compute factor mean for each sector
#' @export
###############################################################################
sector.mean <- function
(
data, # factor
sectors # sectors
)
{
out = data * NA
for(sector in levels(sectors)) {
index = (sector == sectors)
out[,index] = ifna(apply(data[,index, drop=F], 1, mean, na.rm=T),NA)
}
return(out)
}


###############################################################################
# Create quantiles
# http://en.wikipedia.org/wiki/Quantile
# rank each month stocks according to E/P factor
# create quantiles, and record their performance next month
#' @export
###############################################################################
compute.quantiles <- function
(
data, # factor
next.month.ret, # future returns
smain='', # title for plot
n.quantiles=5, # number of quantiles
plot=T # flag to create plot
)
{
n = ncol(data)
nperiods = nrow(data)

data = coredata(ifna(data,NA))
next.month.ret = coredata(ifna(next.month.ret,NA))

temp = matrix(NA, nperiods, n.quantiles)
hist.factor.quantiles = hist.ret.quantiles = temp

temp = matrix(NA, nperiods, n)
quantiles = weights = ranking = temp

#index = which(rowSums(!is.na(data * next.month.ret)) > n/2)
#index = which(rowSums(!is.na(data)) > n/2)
index = which(rowSums(!is.na(data)) >= n.quantiles)
for(t in index) {
factor = data[t,]
ret = next.month.ret[t,]

ranking[t,] = rank(factor, na.last = 'keep','first')
t.ranking = ceiling(n.quantiles * ranking[t,] / count(factor))

quantiles[t,] = t.ranking
weights[t,] = 1/tapply(rep(1,n), t.ranking, sum)[t.ranking]

hist.factor.quantiles[t,] = tapply(factor, t.ranking, mean)
hist.ret.quantiles[t,] = tapply(ret, t.ranking, mean)
}

# create plot
if(plot) {
par(mar=c(4,4,2,1))
temp = 100*apply(hist.ret.quantiles,2,mean,na.rm=T)
  barplot(temp, names.arg=paste(1:n.quantiles), ylab='%',
  main=paste(smain, ', spread =',round(temp[n.quantiles]-temp[1],2), '%'))
  }
 
  return(list(quantiles=quantiles, weights=weights, ranking=ranking,
  hist.factor.quantiles = hist.factor.quantiles, hist.ret.quantiles = hist.ret.quantiles))
}


###############################################################################
# Create Average factor
#' @export
###############################################################################
add.avg.factor <- function
(
data # factors
)
{
# compute the overall factor
temp = abind(data, along = 3)
data$AVG = data[[1]]
data$AVG[] = ifna(apply(temp, c(1,2), mean, na.rm=T),NA)
return(data)
}


###############################################################################
# Convert factor to Z scores, normalize using market capitalization average
#' @export
###############################################################################
normalize.mkval <- function
(
data, # factors
MKVAL # capitalization
)
{
# normalize (convert to z scores) cross sectionaly all factors
for(i in names(data)) {
#data[[i]] = (data[[i]] - apply(data[[i]], 1, mean, na.rm=T)) / apply(data[[i]], 1, sd, na.rm=T)
data[[i]] = (data[[i]] - cap.weighted.mean(data[[i]], MKVAL)) /
apply(data[[i]], 1, sd, na.rm=T)
}
return(data)
}


###############################################################################
# Convert factor to Z scores, only keep the ranks
#' @export
###############################################################################
normal.transform <- function(data)
{
rk=rank(data, na.last='keep', ties.method = 'first')
n = count(data)
x = qnorm((1:n) / (n+1))
return(x[rk])
}

normalize.normal <- function
(
data # factors
)
{
# normalize (convert to z scores) cross sectionaly all factors
for(i in names(data)) {
data[[i]][] = t(apply(data[[i]], 1, normal.transform))
}
return(data)
}


###############################################################################
# Plot Quantiles
#' @export
###############################################################################
plot.quantiles <- function
(
data, # factors
next.month.ret, # future one month returns
smain='' # title
)
{
layout(matrix(1:(2*ceiling(len(data)/2)), nc=2))
sapply(1:len(data), function(i)
compute.quantiles(data[[i]], next.month.ret, paste(names(data)[i],smain))
)
}

###############################################################################
# Plot Backtest Quantiles and spread (Q5-Q1)
#' @export
###############################################################################
plot.bt.quantiles <- function
(
factors, # factors
next.month.ret, # future one month returns
smain='', # title
data # data
)
{
out = compute.quantiles(factors, next.month.ret, plot=F)

prices = data$prices
prices = bt.apply.matrix(prices, function(x) ifna.prev(x))

# find month ends
month.ends = endpoints(prices, 'months')

# create strategies that invest in each qutile
models = list()

for(i in 1:5) {
data$weight[] = NA
data$weight[month.ends,] = iif(out$quantiles == i, out$weights, 0)
capital = 100000
data$weight[] = (capital / prices) * (data$weight)
models[[paste('Q',i,sep='')]] = bt.run(data, type='share', capital=capital)
}

# spread
data$weight[] = NA
data$weight[month.ends,] = iif(out$quantiles == 5, out$weights,
iif(out$quantiles == 1, -out$weights, 0))
capital = 100000
data$weight[] = (capital / prices) * (data$weight)
models$Q5_Q1 = bt.run(data, type='share', capital=capital)

#*****************************************************************
# Create Report
#******************************************************************
plotbt(models, plotX = T, log = 'y', LeftMargin = 3, main=smain)
mtext('Cumulative Performance', side = 2, line = 1)
}



###############################################################################
# Plot Factors details
#' @export
###############################################################################
plot.factors <- function
(
data, # factors
name, # name of factor group
next.month.ret # future one month returns
)
{
x = as.vector(t(data))
y = as.vector(t(next.month.ret))
x = ifna(x,NA)
y = ifna(y,NA)
index = !is.na(x) & !is.na(y)
x = x[index]
y = y[index]

cor.p = round(100*cor(x, y, use = 'complete.obs', method = 'pearson'),1)
cor.s = round(100*cor(x, y, use = 'complete.obs', method = 'spearman'),1)

# Plot
layout(1:2)
plot(x, pch=20)

par(mar=c(4,4,2,1))
plot(x, y, pch=20, xlab=name, ylab='Next Month Return')
abline(lm(y ~ x), col='blue', lwd=2)
plota.legend(paste('Pearson =',cor.p,',Spearman =', cor.s))
}


Something went wrong with that request. Please try again.