Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/R/constraints_ROI.R
1433 views
1
#' constructor for class constraint_ROI
2
#'
3
#' @param assets number of assets, or optionally a named vector of assets specifying seed weights
4
#' @param op.problem an object of type "OP" (optimization problem, of \code{ROI}) specifying the complete optimization problem, see ROI help pages for proper construction of OP object.
5
#' @param solver string argument for what solver package to use, must have ROI plugin installed for that solver. Currently support is for \code{glpk} and \code{quadprog}.
6
#' @param weight_seq seed sequence of weights, see \code{\link{generatesequence}}
7
#' @author Hezky Varon
8
#' @export
9
constraint_ROI <- function(assets=NULL, op.problem, solver=c("glpk", "quadprog"), weight_seq=NULL)
10
{
11
#
12
# Structure for this constructor function borrowed from "constraints.R"
13
#
14
if(is.null(op.problem) | !inherits(op.problem, "OP"))
15
stop("Need to pass in optimization problem of ROI:::OP object type.")
16
17
if (is.null(assets)) {
18
stop("You must specify the assets")
19
}
20
21
if(is.character(assets)){
22
nassets=length(assets)
23
assetnames=assets
24
message("assuming equal weighted seed portfolio")
25
assets<-rep(1/nassets,nassets)
26
names(assets)<-assetnames # set names, so that other code can access it,
27
# and doesn't have to know about the character vector
28
# print(assets)
29
}
30
if(!is.null(assets)){
31
# TODO FIXME this doesn't work quite right on matrix of assets
32
if(is.numeric(assets)){
33
if (length(assets) == 1) {
34
nassets=assets
35
#we passed in a number of assets, so we need to create the vector
36
message("assuming equal weighted seed portfolio")
37
assets<-rep(1/nassets,nassets)
38
} else {
39
nassets = length(assets)
40
}
41
# and now we may need to name them
42
if (is.null(names(assets))) {
43
for(i in 1:length(assets)){
44
names(assets)[i]<-paste("Asset",i,sep=".")
45
}
46
}
47
}
48
}
49
print(paste("You chose to use the ",solver[1]," solver", sep=""))
50
return(structure(
51
list(
52
assets = assets,
53
constrainted_objective = op.problem,
54
solver = solver[1],
55
weight_seq = weight_seq,
56
objectives = list(),
57
call = match.call()
58
),
59
class=c("constraint_ROI","constraint")
60
))
61
}
62
63
64
###############################################################################
65
# R (https://r-project.org/) Numeric Methods for Optimization of Portfolios
66
#
67
# Copyright (c) 2004-2021 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt
68
#
69
# This library is distributed under the terms of the GNU Public License (GPL)
70
# for full details see the file COPYING
71
#
72
# $Id$
73
#
74
###############################################################################
75
76