-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonefact.quantile.r
84 lines (68 loc) · 2.1 KB
/
onefact.quantile.r
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
library(zoo)
args = commandArgs(T)
if(length(args)>1)
{
qfn = args[1]
factfn = args[2]
winsize = args[2]
}else
{
qfn = 'sz800.csv'
factfn = 'sz800.coint.csv'
# rebalance frequency
freq = 20
# number of quantiles
quantN = 5
}
quote = read.zoo(qfn, stringsAsFactors=F, header=T, sep=',')
fact = read.zoo(factfn, stringsAsFactors=F, header=T, sep=',')
# clean quote series: fill 0 with most recent non-zeros
quote[which(quote==0)] = NA
quote = na.locf(quote)
# quote -> daily return
ret.zoo = diff(log(quote))
# TODO: need this line?
#ret.zoo[which(is.na(ret.zoo))] = 0
getrb <-function(fact, freq)
{
# return data-frame of two columns, as start/end points of rebalancing.
ind = index(fact)
ind.seq = seq(from=1, to=length(ind), by=freq)
if(ind.seq[length(ind.seq)] != length(ind))
{
ind.seq = c(ind.seq, length(ind))
}
# (starttk, endtk]
starttk = ind.seq[-length(ind.seq)] + 1
endtk = ind.seq[-1]
starttk = ind[starttk]
endtk = ind[endtk]
return(data.frame(starttk, endtk))
}
rbtick = getrb(fact, freq)
fact.ret = matrix(0, ncol=quantN, nrow=NROW(ret.zoo))
fact.ret = zoo(fact.ret, order.by=index(ret.zoo))
# TODO: there're NA and 0 in fact, and 0 in ret
for(i in 1:NROW(rbtick))
{
starttk = rbtick[i,1]
endtk = rbtick[i,2]
subret = window(ret.zoo, start=starttk, end=endtk)
fact.row = as.vector(coredata(fact[starttk,]))
fact.exclude = union(which(is.na(fact.row)), which(fact.row==0))
fact.ord = setdiff(order(fact.row), fact.exclude)
fact.grp = split(fact.ord, ceiling(seq_along(fact.ord)/(length(fact.ord)/quantN)))
for(j in 1:length(fact.grp))
{
fact.weight = apply(subret[,fact.grp[[j]]] + 1, 2, cumprod)
fact.weight = diff(log(c(1, apply(fact.weight, 1, mean))))
window(fact.ret[,j], start=starttk, end=endtk) = fact.weight
}
}
fact.perf = apply(fact.ret+1, 2, cumprod)
#fact.perf = rbind(0, fact.perf)
fact.perf = zoo(fact.perf, order.by=index(quote)[-1])
qcol = rainbow(quantN+5)
plot(fact.perf, plot.type='s', col=qcol)
legend('topleft', title='quantiles', as.character(1:quantN), col=qcol, lty=1, lwd=2)
dev.print(pdf, file='coint.pdf')