PortOpt {strand} | R Documentation |
Portfolio optimization class
Description
The PortOpt
object is used to set up and solve a
portfolio optimization problem.
Details
A PortOpt
object is configured in the same way as a
Simulation
object, by supplying configuration in a yaml file or list
to the object constructor. Methods are available for adding constraints and
retrieving information about the optimization setup and results. See the
package vignette for information on configuration file setup.
Methods
Public methods
Method new()
Create a new PortOpt
object.
Usage
PortOpt$new(config, input_data)
Arguments
config
An object of class
list
orcharacter
. If the value passed is a character vector, it should be of length 1 and specify the path to a yaml configuration file that contains the object's configuration info. If the value passed is of class list(), the list should contain the object's configuration info in list form (e.g, the return value of callingyaml.load_file
on the configuration file).input_data
A
data.frame
that contains all necessary input for the optimization.If the top-level configuration item
price_var
is not set, prices will be expected in theref_price
column ofinput_data
.
Returns
A new PortOpt
object.
Examples
library(dplyr) data(sample_secref) data(sample_inputs) data(sample_pricing) # Construct optimization input for one day from sample data. The columns # of the input data must match the input configuration. optim_input <- inner_join(sample_inputs, sample_pricing, by = c("id", "date")) %>% left_join(sample_secref, by = "id") %>% filter(date %in% as.Date("2020-06-01")) %>% mutate(ref_price = price_unadj, shares_strategy_1 = 0) opt <- PortOpt$new(config = example_strategy_config(), input_data = optim_input) # The problem is not solved until the \code{solve} method is called # explicitly. opt$solve()
Method setVerbose()
Set the verbose flag to control the amount of informational output.
Usage
PortOpt$setVerbose(verbose)
Arguments
verbose
Logical flag indicating whether to be verbose or not.
Returns
No return value, called for side effects.
Method addConstraints()
Add optimization constraints.
Usage
PortOpt$addConstraints(constraint_matrix, dir, rhs, name)
Arguments
constraint_matrix
Matrix with one row per constraint and
(S+1) \times N
columns, where S is number of strategies and N is the number of stocks.The variables in the optimization are
x_{1,1}, x_{2,1}, \ldots, x_{N,1},
x_{1,2}, x_{2,2}, \ldots, x_{N,2},
\vdots
x_{1,S}, x_{2,S}, \ldots, x_{N,S},
y_1, \ldots, y_N
The first
N \times S
variables are the individual strategy trades. Variablex_{i,s}
represents the signed trade for stock i in strategy s. The following N auxillary variablesy_1, \ldots, y_N
represent the absolute value of the net trade in each stock. So for a stock i, we have:y_i = \sum_s |x_{i,s}|
dir
Vector of class character of length
nrow(constraint_matrix)
that specifies the direction of the constraints. All elements must be one of ">=", "==", or "<=".rhs
Vector of class numeric of length
nrow(constraint_matrix)
that specifies the bounds of the constraints.name
Character vector of length 1 that specifies a name for the set of constraints that are being created.
Returns
No return value, called for side effects.
Method getConstraintMatrix()
Constraint matrix access.
Usage
PortOpt$getConstraintMatrix()
Returns
The optimization's constraint matrix.
Method getConstraintMeta()
Provide high-level constraint information.
Usage
PortOpt$getConstraintMeta()
Returns
A data frame that contains constraint metadata, such as current constraint value and whether a constraint is currently within bounds, for all single-row constraints. Explicitly exclude net trade constraints and constraints that involve net trade variables.
Method solve()
Solve the optimization. After running solve()
,
results can be retrieved using getResultData()
.
Usage
PortOpt$solve()
Returns
No return value, called for side effects.
Method getResultData()
Get optimization result.
Usage
PortOpt$getResultData()
Returns
A data frame that contains the number of shares and the net market value of the trades at the strategy and joint (net) level for each stock in the optimization's input.
Method getLoosenedConstraints()
Provide information about any constraints that were loosened in order to solve the optimization.
Usage
PortOpt$getLoosenedConstraints()
Returns
Object of class list
where keys are the names of the
loosened constraints and values are how much they were loosened toward
current values. Values are expressed as (current constraint value -
loosened constraint value) / (current constraint value - violated
constraint value). A value of 0 means a constraint was loosened 100%
and is not binding.
Method getMaxPosition()
Provide information about the maximum position size allowed for long and short positions.
Usage
PortOpt$getMaxPosition()
Returns
An object of class data.frame
that contains the limits on
size for long and short positions for each strategy and security. The
columns in the data frame are:
- id
Security identifier.
- strategy
Strategy name.
- max_pos_lmv
Maximum net market value for a long position.
- max_pos_smv
Maximum net market value for a short position.
Method getMaxOrder()
Provide information about the maximum order size allowed for each security and strategy.
Usage
PortOpt$getMaxOrder()
Returns
An object of class data.frame
that contains the limit on
order size for each strategy and security. The
columns in the data frame are:
- id
Security identifier.
- strategy
Strategy name.
- max_order_gmv
Maximum gross market value allowed for an order.
Method summaryDf()
Provide aggregate level optimization information if the problem has been solved.
Usage
PortOpt$summaryDf()
Returns
A data frame with one row per strategy, including the joint (net) level, and columns for starting and ending market values and factor expoure values.
Method print()
Print summary information.
Usage
PortOpt$print()
Returns
No return value, called for side effects.
Method clone()
The objects of this class are cloneable with this method.
Usage
PortOpt$clone(deep = FALSE)
Arguments
deep
Whether to make a deep clone.
Examples
## ------------------------------------------------
## Method `PortOpt$new`
## ------------------------------------------------
library(dplyr)
data(sample_secref)
data(sample_inputs)
data(sample_pricing)
# Construct optimization input for one day from sample data. The columns
# of the input data must match the input configuration.
optim_input <-
inner_join(sample_inputs, sample_pricing,
by = c("id", "date")) %>%
left_join(sample_secref, by = "id") %>%
filter(date %in% as.Date("2020-06-01")) %>%
mutate(ref_price = price_unadj,
shares_strategy_1 = 0)
opt <-
PortOpt$new(config = example_strategy_config(),
input_data = optim_input)
# The problem is not solved until the \code{solve} method is called
# explicitly.
opt$solve()