Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/R/applyFUN.R
1433 views
1
#' Apply a risk or return function to a set of weights
2
#'
3
#' This function is used to calculate risk or return metrics given a matrix of
4
#' weights and is primarily used as a convenience function used in chart.Scatter functions
5
#'
6
#' @param R xts object of asset returns
7
#' @param weights a matrix of weights generated from random_portfolios or \code{optimize.portfolio}
8
#' @param FUN name of a function
9
#' @param arguments named list of arguments to FUN
10
#' @author Ross Bennett
11
#' @export
12
applyFUN <- function(R, weights, FUN="mean", arguments){
13
nargs <- arguments
14
15
moments <- function(R){
16
momentargs <- list()
17
momentargs$mu <- matrix(as.vector(apply(R, 2, "mean")), ncol = 1)
18
momentargs$sigma <- cov(R)
19
momentargs$m3 <- PerformanceAnalytics::M3.MM(R)
20
momentargs$m4 <- PerformanceAnalytics::M4.MM(R)
21
return(momentargs)
22
}
23
24
nargs <- c(nargs, moments(R))
25
nargs$R <- R
26
#nargs$invert=FALSE
27
28
# match the FUN arg to a risk or return function
29
switch(FUN,
30
mean = {
31
fun = match.fun(mean)
32
},
33
sd =,
34
StdDev = {
35
fun = match.fun(StdDev)
36
},
37
mVaR =,
38
VaR = {
39
fun = match.fun(VaR)
40
if(is.null(nargs$portfolio_method)) nargs$portfolio_method='single'
41
if(is.null(nargs$invert)) nargs$invert = FALSE
42
},
43
es =,
44
mES =,
45
CVaR =,
46
cVaR =,
47
ETL=,
48
mETL=,
49
ES = {
50
fun = match.fun(ES)
51
if(is.null(nargs$portfolio_method)) nargs$portfolio_method='single'
52
if(is.null(nargs$invert)) nargs$invert = FALSE
53
},
54
# CSM = {
55
# fun = match.fun(CSM)
56
# if(is.null(nargs$portfolio_method)) nargs$portfolio_method='single'
57
# if(is.null(nargs$invert)) nargs$invert = FALSE
58
# },
59
{ # see 'S Programming p. 67 for this matching
60
fun <- try(match.fun(FUN))
61
}
62
) # end switch block
63
64
if(!is.null(nrow(weights))){
65
# case for matrix of weights
66
out <- rep(0, nrow(weights))
67
.formals <- formals(fun)
68
onames <- names(.formals)
69
for(i in 1:nrow(weights)){
70
nargs$weights <- as.numeric(weights[i,])
71
nargs$x <- R %*% as.numeric(weights[i,])
72
dargs <- nargs
73
pm <- pmatch(names(dargs), onames, nomatch = 0L)
74
names(dargs[pm > 0L]) <- onames[pm]
75
.formals[pm] <- dargs[pm > 0L]
76
out[i] <- try(do.call(fun, .formals))
77
}
78
} else {
79
# case for single vector of weights
80
.formals <- formals(fun)
81
onames <- names(.formals)
82
nargs$weights <- as.numeric(weights)
83
nargs$x <- R %*% as.numeric(weights)
84
dargs <- nargs
85
pm <- pmatch(names(dargs), onames, nomatch = 0L)
86
names(dargs[pm > 0L]) <- onames[pm]
87
.formals[pm] <- dargs[pm > 0L]
88
out <- try(do.call(fun, .formals))
89
}
90
return(out)
91
}
92
93
#' Apply a risk or return function to asset returns
94
#'
95
#' This function is used to calculate risk or return metrics given a matrix of
96
#' asset returns and will be used for a risk-reward scatter plot of the assets
97
#'
98
#' @param R xts object of asset returns
99
#' @param FUN name of function
100
#' @param arguments named list of arguments to FUN
101
#' @author Ross Bennett
102
#' @export
103
scatterFUN <- function(R, FUN, arguments=NULL){
104
if(is.null(arguments)){
105
nargs <- list()
106
} else{
107
nargs <- arguments
108
}
109
110
# match the FUN arg to a risk or return function
111
switch(FUN,
112
mean = {
113
return(as.numeric(apply(R, 2, mean)))
114
#fun = match.fun(mean)
115
#nargs$x = R
116
},
117
var = {
118
return(as.numeric(apply(R, 2, var)))
119
#fun = match.fun(mean)
120
#nargs$x = R
121
},
122
sd =,
123
StdDev = {
124
fun = match.fun(StdDev)
125
},
126
mVaR =,
127
VaR = {
128
fun = match.fun(VaR)
129
if(is.null(nargs$portfolio_method)) nargs$portfolio_method='single'
130
if(is.null(nargs$invert)) nargs$invert = FALSE
131
},
132
es =,
133
mES =,
134
CVaR =,
135
cVaR =,
136
ETL =,
137
mETL =,
138
ES = {
139
fun = match.fun(ES)
140
if(is.null(nargs$portfolio_method)) nargs$portfolio_method='single'
141
if(is.null(nargs$invert)) nargs$invert = FALSE
142
},
143
# CSM = {
144
# fun = match.fun(CSM)
145
# if(is.null(nargs$portfolio_method)) nargs$portfolio_method='single'
146
# if(is.null(nargs$invert)) nargs$invert = FALSE
147
# },
148
{ # see 'S Programming p. 67 for this matching
149
fun <- try(match.fun(FUN))
150
}
151
) # end switch block
152
153
# calculate FUN on R
154
out <- rep(0, ncol(R))
155
.formals <- formals(fun)
156
onames <- names(.formals)
157
for(i in 1:ncol(R)){
158
nargs$R <- R[, i]
159
dargs <- nargs
160
pm <- pmatch(names(dargs), onames, nomatch = 0L)
161
names(dargs[pm > 0L]) <- onames[pm]
162
.formals[pm] <- dargs[pm > 0L]
163
out[i] <- try(do.call(fun, .formals))
164
}
165
return(out)
166
}
167
168
###############################################################################
169
# R (https://r-project.org/) Numeric Methods for Optimization of Portfolios
170
#
171
# Copyright (c) 2004-2021 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt
172
#
173
# This library is distributed under the terms of the GNU Public License (GPL)
174
# for full details see the file COPYING
175
#
176
# $Id$
177
#
178
###############################################################################
179
180
181