Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/R/custom.covRob.R
1433 views
1
###############################################################################
2
# R (https://r-project.org/) Numeric Methods for Optimization of Portfolios
3
#
4
# Copyright (c) 2004-2023 Yifu Kang, Doug Martin, Xinran Zhao
5
#
6
# This library is distributed under the terms of the GNU Public License (GPL)
7
# for full details see the file COPYING
8
#
9
# $Id$
10
#
11
###############################################################################
12
13
#' @title Compute returns mean vector and covariance matrix with custom.covRob.MM
14
#'
15
#' @description
16
#' custom.covRob.MM uses the RobStatTM package function covRobMM to compute a robust
17
#' mean vector and robust covariance matrix for a portfolio's asset returns
18
#'
19
#' @param R xts object of asset returns
20
#' @param ... parameters for covRob.MM
21
#'
22
#' @references For parameter details, see covRobMM in the RobStatTM Reference
23
#' Manual at \url{https://CRAN.R-project.org/package=RobStatTM}
24
#'
25
#' @return a list containing covariance matrix sigma and mean vector mu
26
#' @author Yifu Kang, Xinran Zhao
27
#' @export
28
#'
29
custom.covRob.MM <- function(R, ...){
30
out <- list()
31
if(hasArg(tol)) tol = match.call(expand.dots = TRUE)$tol else tol = 1e-4
32
if(hasArg(maxit)) maxit = match.call(expand.dots = TRUE)$maxit else maxit = 50
33
34
robustCov <- RobStatTM::covRobMM(X = R, tolpar = tol, maxit = maxit)
35
36
out$sigma <- robustCov$cov
37
out$mu <- robustCov$center
38
return(out)
39
}
40
41
#' @title Compute returns mean vector and covariance matrix with custom.covRob.Rocke
42
#'
43
#' @description
44
#' custom.covRob.Rocke uses the RobStatTM package function covRobRocke to compute a robust
45
#' mean vector and robust covariance matrix for a portfolio's asset returns
46
#'
47
#' @param R xts object of asset returns
48
#' @param ... parameters for covRob.Rocke
49
#'
50
#' @details For parameter details, see covRobRocke in the RobStatTM Reference
51
#' Manual at \url{https://CRAN.R-project.org/package=RobStatTM}
52
#'
53
#' @return a list containing covariance matrix sigma and mean vector mu
54
#' @author Yifu Kang
55
#' @export
56
#'
57
custom.covRob.Rocke <- function(R, ...){
58
out <- list()
59
if(hasArg(tol)) tol = match.call(expand.dots = TRUE)$tol else tol = 1e-4
60
if(hasArg(maxit)) maxit = match.call(expand.dots = TRUE)$maxit else maxit = 50
61
if(hasArg(initial)) initial = match.call(expand.dots = TRUE)$initial else initial = 'K'
62
if(hasArg(maxsteps)) maxsteps = match.call(expand.dots = TRUE)$maxsteps else maxsteps = 5
63
if(hasArg(propmin)) propmin = match.call(expand.dots = TRUE)$propmin else propmin = 2
64
if(hasArg(qs)) qs = match.call(expand.dots = TRUE)$qs else qs = 50
65
66
robustCov <- RobStatTM::covRobRocke(X = R, initial = initial, maxsteps = maxsteps, propmin = propmin,
67
qs = qs, tol = tol, maxit = maxit)
68
69
out$sigma <- robustCov$cov
70
out$mu <- robustCov$center
71
return(out)
72
}
73
74
#' @title Compute returns mean vector and covariance matrix with custom.covRob.Mcd
75
#'
76
#' @description
77
#' custom.covRob.Mcd uses the robustbase package function covMcd to compute a robust
78
#' mean vector and robust covariance matrix for a portfolio's asset returns
79
#'
80
#' @param R xts object of asset returns
81
#' @param ... parameters for covRob.Mcd
82
#'
83
#' @details For parameter details, see covMcd in the robustbase Reference
84
#' Manual at \url{https://CRAN.R-project.org/package=robustbase}
85
#'
86
#' @return a list containing covariance matrix sigma and mean vector mu
87
#' @export
88
custom.covRob.Mcd <- function(R, ...){
89
90
if(hasArg(control)) control = match.call(expand.dots = TRUE)$control else control = MycovRobMcd()
91
if(hasArg(alpha)) alpha = match.call(expand.dots = TRUE)$alpha else alpha = control$alpha
92
if(hasArg(nsamp)) nsamp = match.call(expand.dots = TRUE)$nsamp else nsamp = control$nsamp
93
if(hasArg(nmini)) nmini = match.call(expand.dots = TRUE)$nmini else nmini = control$nmini
94
if(hasArg(kmini)) kmini = match.call(expand.dots = TRUE)$kmini else kmini = control$kmini
95
if(hasArg(scalefn)) scalefn = match.call(expand.dots = TRUE)$scalefn else scalefn = control$scalefn
96
if(hasArg(maxcsteps)) maxcsteps = match.call(expand.dots = TRUE)$maxcsteps else maxcsteps = control$maxcsteps
97
if(hasArg(initHsets)) initHsets = match.call(expand.dots = TRUE)$initHsets else initHsets = control$initHsets
98
if(hasArg(seed)) seed = match.call(expand.dots = TRUE)$seed else seed = control$seed
99
if(hasArg(tolSolve)) tolSolve = match.call(expand.dots = TRUE)$tolSolve else tolSolve = control$tolSolve
100
if(hasArg(wgtFUN)) wgtFUN = match.call(expand.dots = TRUE)$wgtFUN else wgtFUN = control$wgtFUN
101
if(hasArg(use.correction)) use.correction = match.call(expand.dots = TRUE)$use.correction else use.correction = control$use.correction
102
103
104
robustMCD <- robustbase::covMcd(x = R, alpha = alpha,
105
nsamp = nsamp, nmini = nmini,
106
kmini = kmini, seed = seed,
107
tolSolve = tolSolve, scalefn = scalefn,
108
maxcsteps = maxcsteps,
109
initHsets = initHsets,
110
wgtFUN = wgtFUN, use.correction = use.correction)
111
112
return(list(mu = robustMCD$center, sigma = robustMCD$cov))
113
}
114
115
#' @title
116
#' Control settings for custom.covRob.Mcd
117
#'
118
#' @description
119
#' Auxiliary function for passing the estimation options as parameters
120
#' to the estimation function MCD.robust.moment
121
#'
122
#' @param alpha numeric parameter controlling the size of the subsets over
123
#' which the determinant is minimized. Allowed values are between
124
#' 0.5 and 1 and the default is 0.5.
125
#' @param nsamp number of subsets used for initial estimates or "best", "exact",
126
#' or "deterministic". Default is nsamp = 500. For nsamp = "best"
127
#' exhaustive enumeration is done, as long as the number of trials
128
#' does not exceed 100'000, which is the value of nlarge. For "exact",
129
#' exhaustive enumeration will be attempted however many samples are needed.
130
#' In this case a warning message may be displayed saying that
131
#' the computation can take a very long time.
132
#' For "deterministic", the deterministic MCD is computed;
133
#' as proposed by Hubert et al. (2012) it starts from the h most
134
#' central observations of six (deterministic) estimators.
135
#' @param nmini,kmini for n >= 2*n0, n0 := nmini, the algorithm splits the data
136
#' into maximally kmini (by default 5) subsets, of size approximately,
137
#' but at least nmini. When nmini*kmini < n, the initial search
138
#' uses only a subsample of size nmini*kmini. The original algorithm
139
#' had nmini = 300 and kmini = 5 hard coded.
140
#' @param scalefn function to compute a robust scale estimate or character string
141
#' specifying a rule determining such a function for the deterministic MCD.
142
#' The default is "hrv2012". Another option value is "v2014".
143
#' @param maxcsteps maximal number of concentration steps in the deterministic MCD
144
#' @param seed initial seed for random generator
145
#' @param tolSolve numeric tolerance to be used for inversion of the covariance matrix
146
#' @param wgtFUN a character string or function, specifying how the weights for
147
#' the reweighting step should be computed. Default is "01.originalz".
148
#' @param beta a quantile, experimentally used for some of the prespecified wgtFUNs. For our
149
#' MCD method, the default is 0.975.
150
#' @param use.correction whether to use finite sample correction factors; defaults to TRUE.
151
#' @return a list of passed parameters
152
#' @export
153
#'
154
155
MycovRobMcd <- function(alpha = 1/2,
156
nsamp = 500, nmini = 300, kmini = 5,
157
scalefn = "hrv2012", maxcsteps = 200,
158
seed = NULL, tolSolve = 1e-14,
159
wgtFUN = "01.original", beta,
160
use.correction = TRUE
161
){
162
if(missing(beta) || !is.numeric(beta))
163
beta <- 0.975
164
165
return(list(alpha = alpha, nsamp = nsamp, nmini = as.integer(nmini), kmini = as.integer(kmini),
166
seed = as.integer(seed),
167
tolSolve = tolSolve, scalefn = scalefn, maxcsteps = as.integer(maxcsteps),
168
wgtFUN = wgtFUN, beta = beta,
169
use.correction = use.correction))
170
}
171
172
#' @title Compute returns mean vector and covariance matrix with custom.covRob.TSGS
173
#'
174
#' @description
175
#' This is a function uses the TSGS function from GSE package to compute
176
#' the Two-Step Generalized S-Estimate, a robust estimate of location
177
#' and scatter for data with cell-wise and case-wise contamination.
178
#'
179
#' @param R xts object of asset returns
180
#' @param ... parameters for covRob.TSGS
181
#'
182
#' @return a list contains mean and covariance matrix of the stock return matrix
183
#' @export
184
#'
185
#' @references Claudio Agostinelli, Andy Leung, "Robust estimation of multivariate
186
#' location and scatter in the presence of cellwise and casewise contamination",
187
#' 2014.
188
189
custom.covRob.TSGS <- function(R, ...){
190
if(hasArg(control)) control = match.call(expand.dots = TRUE)$control else control = MycovRobTSGS()
191
if(hasArg(filter)) filter = match.call(expand.dots = TRUE)$filter else filter = control$filter
192
if(hasArg(partial.impute)) partial.impute = match.call(expand.dots = TRUE)$partial.impute else partial.impute = control$partial.impute
193
if(hasArg(tol)) tol = match.call(expand.dots = TRUE)$tol else tol = control$tol
194
if(hasArg(maxiter)) maxiter = match.call(expand.dots = TRUE)$maxiter else maxiter = control$maxiter
195
if(hasArg(loss)) loss = match.call(expand.dots = TRUE)$loss else loss = control$loss
196
if(hasArg(init)) init = match.call(expand.dots = TRUE)$init else init = control$init
197
198
tsgsRob <- GSE::TSGS(x = R, filter = filter,
199
partial.impute = partial.impute, tol = tol,
200
maxiter = maxiter, method = loss,
201
init = init)
202
203
return(list(mu = tsgsRob@mu, sigma = tsgsRob@S))
204
205
}
206
207
#' @title
208
#' Control settings for custom.covRob.TSGS
209
#'
210
#' @description
211
#' Auxiliary function for passing the estimation options as parameters
212
#' to the estimation function custom.TSGS
213
#'
214
#' @param filter the filter to be used in the first step. Available choices are
215
#' "UBF-DDC","UBF","DDC","UF". The default one is "UBF-DDC".
216
#' @param partial.impute whether partial imputation is used prior to estimation.
217
#' The default is FALSE.
218
#' @param tol tolerance for the convergence criterion. Default is 1e-4.
219
#' @param maxiter maximum number of iterations. Default is 150.
220
#' @param loss loss function to use, "bisquare" or "rocke". Default is "bisquare"
221
#' @param init type of initial estimator. Options include "emve", "qc", "huber","imputed","emve_c"
222
#'
223
#' @return a list of passed parameters
224
#' @export
225
#'
226
227
MycovRobTSGS <- function(filter = c("UBF-DDC","UBF","DDC","UF"),
228
partial.impute = FALSE, tol = 1e-4, maxiter = 150,
229
loss = c("bisquare","rocke"),
230
init = c("emve","qc","huber","imputed","emve_c")){
231
232
filter <- match.arg(filter)
233
loss <- match.arg(loss)
234
init <- match.arg(init)
235
236
return(list(filter = filter, partial.impute = partial.impute,
237
tol = tol, maxiter = as.integer(maxiter),
238
loss = loss,init))
239
}
240