2
# This library is free software; you can redistribute it and/or
3
# modify it under the terms of the GNU Library General Public
4
# License as published by the Free Software Foundation; either
5
# version 2 of the License, or (at your option) any later version.
7
# This library is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR Description. See the
10
# GNU Library General Public License for more details.
12
# You should have received a copy of the GNU Library General
13
# Public License along with this library; if not, write to the
14
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19
# 1999 - 2007, Diethelm Wuertz, GPL
20
# Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
23
# for the code accessed (or partly included) from other R-ports:
24
# see R's copyright and license files
25
# for the code accessed (or partly included) from contributed R-ports
27
# see Rmetrics's copyright file
30
################################################################################
31
# FUNCTION: SINGLE PORTFOLIOS:
32
# .feasibleShortMVPortfolio Returns a feasible MV portfolio
33
# .efficientShortMVPortfolio Returns a frontier MV portfolio
34
# .cmlShortMVPortfolio Returns a capital market line
35
# .tangencyShortMVPortfolio Returns the tangency MV portfolio
36
# .minvarianceShortMVPortfolio Returns the minimum variance portfolio
37
# FUNCTION: PORTFOLIO FRONTIER:
38
# .portfolioShortMVFrontier Returns the EF of a short selling MV portfolio
39
################################################################################
42
.feasibleShortMVPortfolio =
43
function(data, spec = portfolioSpec(), constraints = NULL)
44
{ # A function implemented by Rmetrics
47
# Computes Risk and Return for a feasible portfolio
50
# data - portfolio of assets
51
# spec - specification of the portfolio
52
# constraints - string of constraints
55
# In contrast to the functions *Portfolio(), which only require either the
56
# statistics or the series the functions .*Portfolio() require both as
60
# .feasibleShortMVPortfolio(engelsPortfolioData())
65
if (!inherits(data, "fPFOLIODATA")) data = portfolioData(data, spec)
66
mu = getStatistics(data)$mu
67
Sigma = getStatistics(data)$Sigma
68
nAssets = getNumberOfAssets(data)
71
targetAlpha = getTargetAlpha(spec)
74
weights = getWeights(spec)
75
if(is.null(weights)) weights = rep(1/nAssets, times = nAssets)
76
names(weights) = names(mu)
79
targetReturn = matrix(as.numeric(mu %*% weights), nrow = 1)
80
colnames(targetReturn) <- getEstimator(spec)[1]
82
# Compute Target Risks:
83
covTargetRisk = sqrt( as.numeric( weights %*% Sigma %*% weights ) )
84
x = getSeries(data)@Data %*% weights
85
VaR = quantile(x, targetAlpha, type = 1)
86
CVaR = VaR - 0.5*mean(((VaR-x) + abs(VaR-x))) / targetAlpha
87
targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
88
colnames(targetRisk) <-
89
c("cov", paste(c("CVaR.", "VaR."), targetAlpha*100, "%", sep = ""))
97
data = list(data = data),
98
spec = list(spec = spec),
99
constraints = as.character(constraints),
102
targetReturn = targetReturn,
103
targetRisk = targetRisk,
104
targetAlpha = targetAlpha,
106
title = "Feasible Portfolio",
107
description = .description())
111
# ------------------------------------------------------------------------------
114
.efficientShortMVPortfolio =
115
function(data, spec = portfolioSpec(), constraints = NULL)
116
{ # A function implemented by Rmetrics
119
# Computes target risk and weights for an efficient portfolio
122
# data - portfolio of assets
123
# spec - specification of the portfolio
124
# constraints - string of constraints
127
# .efficientShortMVPortfolio(engelsPortfolioData())
132
if (!inherits(data, "fPFOLIODATA")) data = portfolioData(data, spec)
133
mu = getStatistics(data)$mu
134
Sigma = getStatistics(data)$Sigma
135
nAssets = getNumberOfAssets(data)
137
# Get or Set Target Alpha:
138
targetAlpha = getTargetAlpha(spec)
140
# Parameter Settings:
142
one = rep(1, times = length(mu))
143
invSigma = solve(Sigma)
144
a = as.numeric(mu %*% invSigma %*% mu)
145
b = as.numeric(mu %*% invSigma %*% one)
146
c = as.numeric(one %*% invSigma %*% one)
147
d = as.numeric(a*c - b^2)
149
# Compute Target Return:
150
targetReturn = getTargetReturn(spec)
151
if (is.null(targetReturn))
152
targetReturn = getTargetReturn(.tangencyShortMVPortfolio(data, spec))
153
targetReturn = matrix(targetReturn, nrow = 1)
154
colnames(targetReturn) = getEstimator(spec)[1]
157
weights = as.vector(invSigma %*% ((a-b*mu)*C0 + (c*mu-b)*targetReturn )/d)
158
names(weights) = names(mu)
160
# Compute Target Risk:
161
covTargetRisk = sqrt((c*targetReturn^2 - 2*b*C0*targetReturn + a*C0^2) / d)
162
x = getSeries(data)@Data %*% weights
163
VaR = quantile(x, targetAlpha, type = 1)
164
CVaR = VaR - 0.5*mean(((VaR-x) + abs(VaR-x))) / targetAlpha
165
targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
166
colnames(targetRisk) <-
167
c("cov", paste(c("CVaR.", "VaR."), targetAlpha*100, "%", sep = ""))
175
data = list(data = data),
176
spec = list(spec = spec),
177
constraints = as.character(constraints),
180
targetReturn = targetReturn,
181
targetRisk = targetRisk,
182
targetAlpha = targetAlpha,
184
title = "Frontier MV Portfolio",
185
description = .description())
189
# ------------------------------------------------------------------------------
192
.cmlShortMVPortfolio =
193
function(data, spec = portfolioSpec(), constraints = NULL)
194
{ # A function implemented by Rmetrics
197
# Computes capital market line
200
# data - portfolio of assets
201
# spec - specification of the portfolio
202
# constraints - string of constraints
205
# .cmlShortMVPortfolio(engelsPortfolioData())
210
if (!inherits(data, "fPFOLIODATA")) data = portfolioData(data, spec)
211
mu = getStatistics(data)$mu
212
Sigma = getStatistics(data)$Sigma
213
nAssets = getNumberOfAssets(data)
215
# Get or Set Target Alpha:
216
targetAlpha = getTargetAlpha(spec)
219
riskFreeRate = getRiskFreeRate(spec)
221
# Parameter Settings:
223
one = rep(1, times = length(mu))
224
invSigma = solve(Sigma)
225
a = as.numeric(mu %*% invSigma %*% mu)
226
b = as.numeric(mu %*% invSigma %*% one)
227
c = as.numeric(one %*% invSigma %*% one)
228
d = as.numeric(a*c - b^2)
230
# Capital Market Line:
231
A = (a - b*riskFreeRate)
232
B = (b - c*riskFreeRate)/C0
235
weights = C0 * as.vector(invSigma %*% (mu - riskFreeRate) ) / B
236
names(weights) = names(mu)
240
targetReturn = matrix(targetReturn, nrow = 1)
241
colnames(targetReturn) = getEstimator(spec)[1]
244
covTargetRisk = sqrt(c*riskFreeRate^2 - 2*b*riskFreeRate + a) / B
245
x = getSeries(data)@Data %*% weights
246
VaR = quantile(x, targetAlpha, type = 1)
247
CVaR = VaR - 0.5*mean(((VaR-x) + abs(VaR-x))) / targetAlpha
248
targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
249
colnames(targetRisk) <-
250
c("cov", paste(c("CVaR.", "VaR."), targetAlpha*100, "%", sep = ""))
258
data = list(data = data),
259
spec = list(spec = spec),
260
constraints = as.character(constraints),
263
targetReturn = targetReturn,
264
targetRisk = targetRisk,
265
targetAlpha = targetAlpha,
267
title = "Capital Market Line",
268
description = .description())
272
# ------------------------------------------------------------------------------
275
.tangencyShortMVPortfolio =
276
function(data, spec = portfolioSpec(), constraints = NULL)
277
{ # A function implemented by Rmetrics
280
# Computes target risk and weights for the tangency portfolio
283
# data - portfolio of assets
284
# spec - specification of the portfolio
285
# constraints - string of constraints
288
# .tangencyShortMVPortfolio(engelsPortfolioData())
293
if (!inherits(data, "fPFOLIODATA")) data = portfolioData(data, spec)
294
mu = getStatistics(data)$mu
295
Sigma = getStatistics(data)$Sigma
296
nAssets = getNumberOfAssets(data)
298
# Get or Set Target Alpha:
299
targetAlpha = getTargetAlpha(spec)
301
# Parameter Settings:
303
one = rep(1, times = length(mu))
304
invSigma = solve(Sigma)
305
a = as.numeric(mu %*% invSigma %*% mu)
306
b = as.numeric(mu %*% invSigma %*% one)
307
c = as.numeric(one %*% invSigma %*% one)
308
d = as.numeric(a*c - b^2)
311
weights = C0 * as.vector(invSigma %*% mu ) / b
312
names(weights) = names(mu)
315
targetReturn = (a/b)*C0
316
targetReturn = matrix(targetReturn, nrow = 1)
317
colnames(targetReturn) = spec@model$estimator[1]
320
covTargetRisk = (sqrt(a)/b)*C0
321
x = getSeries(data)@Data %*% weights
322
VaR = quantile(x, targetAlpha, type = 1)
323
CVaR = VaR - 0.5*mean(((VaR-x) + abs(VaR-x))) / targetAlpha
324
targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
325
colnames(targetRisk) <-
326
c("cov", paste(c("CVaR.", "VaR."), targetAlpha*100, "%", sep = ""))
334
data = list(data = data),
335
spec = list(spec = spec),
336
constraints = as.character(constraints),
339
targetReturn = targetReturn,
340
targetRisk = targetRisk,
341
targetAlpha = targetAlpha,
343
title = "Tangency MV Portfolio",
344
description = .description())
348
# ------------------------------------------------------------------------------
351
.minvarianceShortMVPortfolio =
352
function(data, spec = portfolioSpec(), constraints = NULL)
353
{ # A function implemented by Rmetrics
356
# Computes target risk and weights for the minimum variance portfolio
359
# data - portfolio of assets
360
# spec - specification of the portfolio
361
# constraints - string of constraints
364
# .minvarianceShortMVPortfolio(engelsPortfolioData())
369
if (!inherits(data, "fPFOLIODATA")) data = portfolioData(data, spec)
370
mu = getStatistics(data)$mu
371
Sigma = getStatistics(data)$Sigma
372
nAssets = getNumberOfAssets(data)
374
# Get or Set Target Alpha:
375
targetAlpha = getTargetAlpha(spec)
377
# Parameter Settings:
379
one = rep(1, times = length(mu))
380
invSigma = solve(Sigma)
381
a = as.numeric(mu %*% invSigma %*% mu)
382
b = as.numeric(mu %*% invSigma %*% one)
383
c = as.numeric(one %*% invSigma %*% one)
384
d = as.numeric(a*c - b^2)
387
weights = as.vector(invSigma %*% ((a-b*mu)*C0 + (c*mu-b)*(b/c)*C0 )/d)
388
names(weights) = names(mu)
391
targetReturn = (b/c)*C0
392
targetReturn = matrix(targetReturn, nrow = 1)
393
colnames(targetReturn) = getEstimator(spec)[1]
396
covTargetRisk = C0/sqrt(c)
397
x = getSeries(data)@Data %*% weights
398
VaR = quantile(x, targetAlpha, type = 1)
399
CVaR = VaR - 0.5*mean(((VaR-x) + abs(VaR-x))) / targetAlpha
400
targetRisk = matrix(c(covTargetRisk, CVaR, VaR), nrow = 1)
401
colnames(targetRisk) <-
402
c("cov", paste(c("CVaR.", "VaR."), targetAlpha*100, "%", sep = ""))
410
data = list(data = data),
411
spec = list(spec = spec),
412
constraints = as.character(constraints),
415
targetReturn = targetReturn,
416
targetRisk = targetRisk,
417
targetAlpha = targetAlpha,
419
title = "Minimum Variance MV Portfolio",
420
description = .description())
424
################################################################################
427
.portfolioShortMVFrontier =
428
function(data, spec = portfolioSpec(), constraints = NULL)
429
{ # A function implemented by Diethelm Wuertz
432
# Calculates the efficient frontier, short selling allowed
434
# Details from a matrix
435
# Calculates the efficient frontier (short selling allowed) from a
436
# a matrix of either market or simulated assets given in matrix "x".
437
# Each time series represents a column in this matrix.
440
# data - portfolio of assets
441
# spec - specification of the portfolio
442
# constraints - string of constraints
445
# .shortMVFrontier(engelsPortfolioData())
450
if (!inherits(data, "fPFOLIODATA")) data = portfolioData(data, spec)
451
mu = getStatistics(data)$mu
452
Sigma = getStatistics(data)$Sigma
453
nAssets = getNumberOfAssets(data)
455
# Get or Set Target Alpha:
456
targetAlpha = getTargetAlpha(spec)
459
riskFreeRate = getRiskFreeRate(spec)
460
nFrontierPoints = getNFrontierPoints(spec)
462
# Parameter Settings:
464
one = rep(1, times = length(mu))
465
invSigma = solve(Sigma)
466
a = as.numeric(mu %*% invSigma %*% mu)
467
b = as.numeric(mu %*% invSigma %*% one)
468
c = as.numeric(one %*% invSigma %*% one)
469
d = as.numeric(a*c - b^2)
471
# Ranges for mean and Standard Deviation:
472
muRange = range(mu)+ 0.25*c(-diff(range(mu)), diff(range(mu)))
473
sqrtSig = sqrt(diag(Sigma))
474
sigmaRange = c(min(sqrtSig), max(sqrtSig))+
475
0.25*c(-diff(range(sqrtSig)), diff(range(sqrtSig)))
477
# Efficient Frontier Portfolios:
478
targetReturn = seq(muRange[1], muRange[2], length = nFrontierPoints)
479
targetReturn = as.vector(targetReturn)
480
targetRisk = sqrt((c*targetReturn^2 - 2*b*C0*targetReturn + a*C0^2)/d)
481
covTargetRisk = as.vector(targetRisk)
484
series = getSeries(data)@Data
486
for (i in 1:nFrontierPoints) {
487
Spec@portfolio$targetReturn = targetReturn[i]
488
nextWeight = getWeights(.efficientShortMVPortfolio(data, Spec))
489
weights = rbind(weights, t(nextWeight))
491
x = series %*% nextWeight
492
nextVaR = quantile(x, targetAlpha, type = 1)
493
nextCVaR = nextVaR-0.5*mean(((nextVaR-x)+abs(nextVaR-x))) / targetAlpha
494
nextTargetRisk = matrix(c(covTargetRisk[i], nextCVaR, nextVaR), nrow = 1)
495
targetRisk = rbind(targetRisk, nextTargetRisk)
497
colnames(targetRisk) <-
498
c("cov", paste(c("CVaR.", "VaR."), targetAlpha*100, "%", sep = ""))
501
targetReturn = matrix(targetReturn, ncol = 1)
502
colnames(targetReturn) = getEstimator(spec)[1]
505
colnames(targetRisk) <-
506
c("cov", paste(c("CVaR.", "VaR."), targetAlpha*100, "%", sep = ""))
514
data = list(data = data),
515
spec = list(spec = spec),
516
constraints = as.character(constraints),
519
targetReturn = targetReturn,
520
targetRisk = targetRisk,
521
targetAlpha = targetAlpha,
523
title = "Short Selling Portfolio Frontier",
524
description = .description())
528
################################################################################