Description Details Methods Examples
The PortOpt
object is used to set up and solve a
portfolio optimization problem.
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.
new()
Create a new PortOpt
object.
PortOpt$new(config, input_data)
config
An object of class list
or character
. 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 calling yaml.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 the ref_price
column of input_data
.
A new PortOpt
object.
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()
setVerbose()
Set the verbose flag to control the amount of informational output.
PortOpt$setVerbose(verbose)
verbose
Logical flag indicating whether to be verbose or not.
No return value, called for side effects.
addConstraints()
Add optimization constraints.
PortOpt$addConstraints(constraint_matrix, dir, rhs, name)
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}, …, x_{N,1},
x_{1,2}, x_{2,2}, …, x_{N,2},
\vdots
x_{1,S}, x_{2,S}, …, x_{N,S},
y_1, …, y_N
The first N \times S variables are the individual strategy trades. Variable x_{i,s} represents the signed trade for stock i in strategy s. The following N auxillary variables y_1, …, y_N represent the absolute value of the net trade in each stock. So for a stock i, we have:
y_i = ∑_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.
No return value, called for side effects.
getConstraintMatrix()
Constraint matrix access.
PortOpt$getConstraintMatrix()
The optimization's constraint matrix.
getConstraintMeta()
Provide high-level constraint information.
PortOpt$getConstraintMeta()
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.
solve()
Solve the optimization. After running solve()
,
results can be retrieved using getResultData()
.
PortOpt$solve()
No return value, called for side effects.
getResultData()
Get optimization result.
PortOpt$getResultData()
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.
getLoosenedConstraints()
Provide information about any constraints that were loosened in order to solve the optimization.
PortOpt$getLoosenedConstraints()
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.
getMaxPosition()
Provide information about the maximum position size allowed for long and short positions.
PortOpt$getMaxPosition()
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:
Security identifier.
Strategy name.
Maximum net market value for a long position.
Maximum net market value for a short position.
getMaxOrder()
Provide information about the maximum order size allowed for each security and strategy.
PortOpt$getMaxOrder()
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:
Security identifier.
Strategy name.
Maximum gross market value allowed for an order.
summaryDf()
Provide aggregate level optimization information if the problem has been solved.
PortOpt$summaryDf()
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.
print()
Print summary information.
PortOpt$print()
No return value, called for side effects.
clone()
The objects of this class are cloneable with this method.
PortOpt$clone(deep = FALSE)
deep
Whether to make a deep clone.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | ## ------------------------------------------------
## 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()
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.