[go: up one dir, main page]

File: builtin-shrinkTawny.R

package info (click to toggle)
fassets 3011.82-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 424 kB
  • sloc: makefile: 1
file content (296 lines) | stat: -rw-r--r-- 8,184 bytes parent folder | download | duplicates (4)
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

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA


################################################################################
# FUNCTION:                     DESCRIPTION:
# .cov.shrink.tawny
# .getCorFilter.Shrinkage
# .cov.sample.tawny
# .cov.prior.cc
# .cov.prior.identity
# .cor.mean.tawny
# .shrinkage.intensity
# .shrinkage.p
# .shrinkage.r
# .shrinkage.c
################################################################################


# Rmetrics: 
#   Note that tawny is not available on Debian as of 2009-04-28. 
#   To run these functions under Debian/Rmetrics we have them    
#   implemented here as a builtin.
#   We also made modifications for tailored usage with Rmetrics. 


# Package: tawny
# Title: Provides various portfolio optimization strategies including
#    random matrix theory and shrinkage estimators
# Version: 1.0
# Date: 2009-03-02
# Author: Brian Lee Yung Rowe
# Maintainer: Brian Lee Yung Rowe <tawny-help@muxspace.com>
# License: GPL-2
#
# Perform shrinkage on a sample covariance towards a biased covariance
#
# This performs a covariance shrinkage estimation as specified in Ledoit 
# and Wolf. Using within the larger framework only requires using the 
# getCorFilter.Shrinkage function, which handles the work of constructing 
# a shrinkage estimate of the covariance matrix of returns (and consequently 
# its corresponding correlation matrix).


# ------------------------------------------------------------------------------


.cov.shrink.tawny <- 
function(returns, sample = NULL, prior.fun = .cov.prior.cc, ...)
{
    # Shrink the sample covariance matrix towards the model covariance 
    #   matrix for the given time window.
    # model - The covariance matrix specified by the model, e.g. single-index, 
    #   Barra, or something else
    # sample - The sample covariance matrix. If the sample covariance is null, 
    #   then it will be computed from the returns matrix
    
    # Example
    #   S.hat <- .cov.shrink.tawny(ys)

    # if (.loglevel.tawny() > 0) cat("Shrinking covariance for",last(index(returns)),"\n")
    if (is.null(sample)) { S <- .cov.sample.tawny(returns) }
    else { S <- sample }
    
    T <- nrow(returns)
    # F <- .cov.prior.cc(S)
    F <- prior.fun(S, ...)
    k <- .shrinkage.intensity(returns, F, S)
    d <- max(0, min(k/T, 1))
    
    if (.loglevel.tawny() > 0) cat("Got intensity k =", k,
        "and coefficient d =",d,"\n")
    
    S.hat <- d * F + (1 - d) * S
    S.hat
}

# ------------------------------------------------------------------------------


.getCorFilter.Shrinkage <- 
function(prior.fun = .cov.prior.cc, ...)
{
    # Return a correlation matrix generator that is compatible with the 
    # portfolio optimizer
    
    # Example
    #   ws <- optimizePortfolio(ys, 100, .getCorFilter.Shrinkage())
    #   plotPerformance(ys,ws)

function(h) return(cov2cor(.cov.shrink.tawny(h, prior.fun=prior.fun, ...)))
}


# ------------------------------------------------------------------------------


.cov.sample.tawny <- 
function(returns)
{
    # Calculate the sample covariance matrix from a returns matrix
    # Returns a T x N returns matrix 
    # p.cov <- .cov.sample.tawny(p)

    # X is N x T
    T <- nrow(returns)
    X <- t(returns)
    ones <- rep(1,T)
    S <- (1/T) * X %*% (diag(T) - 1/T * (ones %o% ones) ) %*% t(X)
    S
}

# ------------------------------------------------------------------------------


.cov.prior.cc <- 
function(S)
{
    # Constant correlation target
    # S is sample covariance

    r.bar <- .cor.mean.tawny(S)
    vars <- diag(S) %o% diag(S)
    F <- r.bar * (vars)^0.5
    diag(F) <- diag(S)
    return(F)
}


# ------------------------------------------------------------------------------


.cov.prior.identity <- 
function(S)
{
    # This returns a covariance matrix based on the identity (i.e. no 
    # correlation)
    
    # S is sample covariance

    return(diag(nrow(S)))
}


# ------------------------------------------------------------------------------


.cor.mean.tawny <- 
function(S)
{
    # Get mean of correlations from covariance matrix
    
    N <- ncol(S)
    cors <- cov2cor(S)
    2 * sum(cors[lower.tri(cors)], na.rm=TRUE) / (N^2 - N)
}


# ------------------------------------------------------------------------------
  
                                                                             
.shrinkage.intensity <- 
function(returns, prior, sample)
{
    # Calculate the optimal shrinkage intensity constant
    # returns : asset returns T x N
    # prior : biased estimator

    p <- .shrinkage.p(returns, sample)
    
    r <- .shrinkage.r(returns, sample, p)
    c <- .shrinkage.c(prior, sample)
    (p$sum - r) / c
}


# ------------------------------------------------------------------------------


.shrinkage.p <- 
function(returns, sample)
{
    # Sum of the asymptotic variances
    # returns : T x N - Matrix of asset returns
    # sample : N x N - Sample covariance matrix
    # Used internally.
    # S <- .cov.sample.tawny(ys)
    # ys.p <- .shrinkage.p(ys, S)

    T <- nrow(returns)
    N <- ncol(returns)
    ones <- rep(1,T)
    means <- t(returns) %*% ones / T
    z <- returns - matrix(rep(t(means), T), ncol=N, byrow=TRUE)
    
    term.1 <- t(z^2) %*% z^2
    term.2 <- 2 * sample * (t(z) %*% z)
    term.3 <- sample^2
    phi.mat <- (term.1 - term.2 + term.3) / T
    
    phi <- list()
    phi$sum <- sum(phi.mat)
    phi$diags <- diag(phi.mat)
    phi
}


# ------------------------------------------------------------------------------


.shrinkage.r <- 
function(returns, sample, pi.est)
{
    # Estimation for rho when using a constant correlation target
    # returns : stock returns
    # market : market returns 
    # Example
    #   S <- .cov.sample.tawny(ys)
    #   ys.p <- .shrinkage.p(ys, S)
    #   ys.r <- .shrinkage.r(ys, S, ys.p)

    N <- ncol(returns)
    T <- nrow(returns)
    ones <- rep(1,T)
    means <- t(returns) %*% ones / T
    z <- returns - matrix(rep(t(means), T), ncol=N, byrow=TRUE)
    r.bar <- .cor.mean.tawny(sample)
    
    # Asymptotic covariance estimator
    term.1 <- t(z^3) %*% z
    term.2 <- diag(sample) * (t(z) %*% z)
    term.3 <- sample * (t(z^2) %*% matrix(rep(1,N*T), ncol=N))
    # This can be simplified to diag(sample) * sample, but this expansion is
    # a bit more explicit in the intent (unless you're an R guru)
    term.4 <- (diag(sample) %o% rep(1,N)) * sample
    script.is <- (term.1 - term.2 - term.3 + term.4) / T
    
    # Create matrix of quotients
    ratios <- (diag(sample) %o% diag(sample)^-1)^0.5
    
    # Sum results
    rhos <- 0.5 * r.bar * (ratios * script.is + t(ratios) * t(script.is))
    
    # Add in sum of diagonals of pi
    sum(pi.est$diags, na.rm = TRUE) 
    + sum(rhos[lower.tri(rhos)], na.rm = TRUE) 
    + sum(rhos[upper.tri(rhos)], na.rm = TRUE)
}


# ------------------------------------------------------------------------------

.shrinkage.c <- 
function(prior, sample)
{
    # Misspecification of the model covariance matrix

    squares <- (prior - sample)^2
    sum(squares, na.rm = TRUE)
}


# ------------------------------------------------------------------------------


.loglevel.tawny <- 
function (new.level = NULL) 
{
    if (!is.null(new.level)) {
        options(log.level = new.level)
    }
    
    if (is.null(getOption("log.level"))) {
        return(0)
    }
    
    return(getOption("log.level"))
}



################################################################################