Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/R/objectiveFUN.R
1433 views
1
2
#' Calculates turnover given two vectors of weights.
3
#' This is used as an objective function and is called when the user adds an objective of type turnover with \code{\link{add.objective}}
4
#' @param weights vector of weights from optimization
5
#' @param wts.init vector of initial weights used to calculate turnover from
6
#' @author Ross Bennett
7
#' @export
8
turnover <- function(weights, wts.init=NULL) {
9
# turnover function from https://r-forge.r-project.org/scm/viewvc.php/pkg/PortfolioAnalytics/sandbox/script.workshop2012.R?view=markup&root=returnanalytics
10
11
N <- length(weights)
12
13
# If wts.init is not given, then assume a vector of equal weights
14
if(is.null(wts.init)) {
15
wts.init <- rep(1/N, N)
16
}
17
18
# Check that weights and wts.init are the same length
19
if(length(weights) != length(wts.init)) stop("weights and wts.init are not the same length")
20
21
return(sum(abs(wts.init - weights)) / N)
22
}
23
24
#' Calculate portfolio variance
25
#'
26
#' This function is used to calculate the portfolio variance via a call to
27
#' constrained_objective when var is an object for mean variance or quadratic
28
#' utility optimization.
29
#'
30
#' @param R xts object of asset returns
31
#' @param weights vector of asset weights
32
#' @return numeric value of the portfolio variance
33
#' @author Ross Bennett
34
#' @export
35
var.portfolio <- function(R, weights){
36
weights <- matrix(weights, ncol=1)
37
return(as.numeric(t(weights) %*% var(R) %*% weights))
38
}
39
40
#' Concentration of weights
41
#'
42
#' This function computes the concentration of weights using the Herfindahl Hirschman Index
43
#'
44
#' @param weights set of portfolio weights
45
#' @param groups list of vectors of grouping
46
#' @author Ross Bennett
47
#' @export
48
HHI <- function(weights, groups=NULL){
49
50
# calculate overall HHI
51
hhi <- sum(weights^2)
52
53
# calculate group HHI
54
if(!is.null(groups)){
55
ngroups <- length(groups)
56
group_hhi <- rep(0, ngroups)
57
if(!is.null((names(groups)))) names(group_hhi) <- names(groups)
58
for(i in 1:ngroups){
59
group_hhi[i] <- sum(weights[groups[[i]]]^2)
60
}
61
return(list(HHI=hhi, Groups_HHI=group_hhi))
62
} else {
63
return(hhi)
64
}
65
}
66
67
# portfolio mean return
68
port.mean <- function(weights, mu){
69
# t(weights) %*% moments$mu
70
as.numeric(crossprod(weights, mu))
71
}
72
73
###############################################################################
74
# R (https://r-project.org/) Numeric Methods for Optimization of Portfolios
75
#
76
# Copyright (c) 2004-2021 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt
77
#
78
# This library is distributed under the terms of the GNU Public License (GPL)
79
# for full details see the file COPYING
80
#
81
# $Id$
82
#
83
###############################################################################
84
85