| trade.optimizer {PortfolioProbe} | R Documentation |
Finds an approximately optimal portfolio trade. The required inputs are the prices, and a sufficient indication of the monetary size of the portfolio or the turnover. Typically a variance matrix and possibly a vector of expected returns are given as well. Almost always only integer values are traded, thus round lotting is automatically done if the prices are given for lots. Other constraints include the number of assets traded, and the number of assets in the optimal portfolio.
trade.optimizer(prices, variance=NULL, expected.return=NULL,
existing=NULL, gross.value=NULL, net.value=NULL, long.value=NULL,
short.value=NULL, turnover=NULL, long.only=FALSE, max.weight=1,
min.weight.thresh=NULL, universe.trade=NULL, lower.trade=NULL,
upper.trade=NULL, risk.fraction=NULL, rf.style="fraction",
rf.loc=NULL, ntrade=NULL, port.size=NULL, threshold=NULL,
forced.trade=NULL, positions=NULL, tol.positions=0,
lin.constraints=NULL, lin.bounds=NULL, lin.trade=FALSE,
lin.abs=TRUE, lin.style="weight", lin.direction=0,
lin.rfloc=NULL, alpha.constraint=NULL, var.constraint=NULL,
bench.constraint=NULL, dist.center=NULL, dist.style="weight",
dist.bounds=NULL, dist.trade=FALSE, dist.utility=FALSE,
dist.prices=NULL, sum.weight=NULL, limit.cost=NULL, close.number=NULL,
utility=NULL, risk.aversion=1, benchmark=NULL, bench.trade=FALSE,
bench.weights=NULL, long.buy.cost=NULL,
long.sell.cost=long.buy.cost, short.buy.cost=long.buy.cost,
short.sell.cost=long.buy.cost, cost.par=NULL, ucost=1,
scale.cost="gross", start.sol=NULL, allowance=.9999, do.warn=NULL,
penalty.constraint=1000, quantile=0.5, dest.wt=NULL,
utable=NULL, atable=NULL, vtable=NULL, dumpfile="", ...,
seed=.standard.seed.BurSt, control=trade.optimizer.control,
identity=NULL)
prices |
required.
Named vector of prices of the assets.
This vector determines the assets to be used in the problem – other
inputs such as |
variance |
numeric matrix or three-dimensional array.
This may have assets in addition to those named in
Benchmarks do not need to be given in the variance when
their weights (of assets in the variance) are given in
Alternatively, compact forms of variances may be given via a list with special components (see the User's Manual) – this is very seldom useful. |
expected.return |
numeric vector or matrix of expected returns of the assets.
If a matrix, then each vector of expected returns is in a different
column.
This need not have (row) names, but it is generally advised.
If it does have asset names, then it can contain information for
assets in addition to those in |
existing |
named vector of units (shares or lots) of the existing portfolio.
The
|
gross.value |
numeric vector of length one or two giving the desired gross value
of the optimal portfolio.
If of length two, this is a range of values that are allowed.
If of length one, then the lower bound is created by multiplying
by |
net.value |
numeric vector of length two (or possibly one) giving the range that the net value may have. There is more on this argument in the Details section. |
long.value |
numeric vector of length one or two giving the desired sum of the
values of the long positions of the optimal portfolio.
If of length two, this is a range of values that are allowed.
If of length one, then the lower bound is created by multiplying
by |
short.value |
numeric vector of length one or two giving the desired sum of the
value of the short positions of the optimal portfolio.
If of length two, this is a range of values that are allowed.
If of length one, then the lower bound is created by multiplying
by |
turnover |
numeric vector of length one or two giving the desired sum of the absolute value of all the trades. If of length one, then the maximum turnover; otherwise the range of allowable trade values. There is more on this argument in the Details section. |
long.only |
logical value stating if the optimal portfolio is restricted to be long-only (no negative numbers of units of assets). |
max.weight |
a numeric vector containing numbers between zero and one giving
the maximum weight allowed in the gross value for the assets.
If this is an unnamed vector, then it is replicated to have length
equal to the number of assets.
If named, then it bounds those specified assets.
|
min.weight.thresh |
a single numeric value giving the minimum weight for any asset in a long-only portfolio. This is ignored for long-short portfolios – you need to use
|
universe.trade |
character vector giving the names of all the assets that are
allowed to be traded.
Assets not named here will be disallowed from trading.
If |
lower.trade |
numeric vector of the desired lower bound on the units to trade
in the assets.
If this is an unnamed vector, then it is replicated to have length
equal to the number of assets.
If named, then it bounds just those specified assets.
Bounds may automatically be placed on the assets from other information
about the problem.
See also the |
upper.trade |
numeric vector of the desired upper bound on the units to trade
in the assets.
If this is an unnamed vector, then it is replicated to have length
equal to the number of assets.
If named, then it bounds just those specified assets.
Bounds may automatically be placed on the assets from other information
about the problem.
See also the |
risk.fraction |
a number, a vector, a two-column matrix, a three-dimensional array
with two columns, a list, or The maximum allowed is constrained for all assets by a single number, or for named assets in a vector. If a matrix is given, then the first column is the minimum allowed and the second column is the maximum allowed – the rows should be named with asset ids (not all assets need be constrained). If there are multiple combinations of variances and benchmarks or multiple risk fraction styles are used, then a three-dimensional array can be used to specify constraints for as many of the situations as desired. An alternative to giving a three-dimensional array is to give a list with length equal to the number of variance-benchmark-style combinations. The big advantage of a list is that simple values, such as a single number, can be given in some or all of the components. See the |
rf.style |
character vector giving the style of each risk fraction constraint.
That is, it might say that some are value constraints rather than
fraction constraints.
The length of this should either be 1 or the number of risk
fraction constraints.
The allowable values are (abbreviations of):
|
rf.loc |
numeric vector giving the zero-based columns in |
ntrade |
an integer vector of length one or two.
If length one, an integer giving the maximum number of assets to trade.
If length two, then the minimum and maximum number of assets to trade.
if |
port.size |
an integer or a vector of two integers. If a single integer, the upper bound on the desired number of assets in the optimal portfolio. If two integers, then the allowed range of the number of assets in the optimal portfolio. |
threshold |
a vector (with names); or a matrix with 1, 2, 3 or 4 columns, and row names. The first two columns give the minimum amount to be traded for (some of) the assets if the asset is traded at all. If a vector or one-column matrix, then the constraint is considered to be symmetric for buying and selling. If a two-column matrix, then the minimum of the two numbers for each asset can not be positive, and the maximum of the two numbers can not be negative. The third and fourth columns give the minimum number of units (shares or lots) allowed in the portfolio if the asset is in the portfolio at all. A three-column matrix can only be given for long-only portfolios, in which case the third column is the minimum number of units for the assets if the asset is in the portfolio. Otherwise, the third column should be non-positive. There is no requirement for row names if all of the rows are the same. |
forced.trade |
a named numeric vector giving a minimum amount to trade for
specific assets (which are identified by the names of the vector).
If you want to trade an exact amount, you need to also use
|
positions |
a numeric matrix with 2, 4 or 8 columns giving the monetary
value of positions that are allowed for each asset (rows).
The first two columns give the minimum and maximum amount of money
allowed in the portfolio for each of the assets.
The third and fourth columns give the minimum and maximum amount
of money that can be traded.
Forced trades can be implied.
If there is at least one |
tol.positions |
a number giving a monetary value of the tolerance for existing
positions to break the portfolio constraints given in the
|
lin.constraints |
a matrix or data frame describing the linear (and/or count)
constraints to be placed on the portfolio and/or trade.
Factors or characters (which are coerced to factors) represent as
many constraints as there are levels in the factor.
It is often easiest if this and |
lin.bounds |
a two-column matrix with rows that correspond to the constraints
represented by |
lin.trade |
a logical vector (replicated to have length equal to the number of
columns in |
lin.abs |
a logical vector (replicated to have length equal to the number of
columns in WARNING: the default for this changed – it is now |
lin.style |
a character vector (replicated to have length equal to the number of
columns in For "weight" constraints, the bounds are expressed in weights (position value divided by gross value of the portfolio). For "value" constraints, the bounds are expressed in monetary units. For "count" constraints, the bounds are expressed in the number of assets; count constraints must be categorical, not numeric. For "varfraction" and "varmbfraction", the bounds are in fractions of the variance; the latter being the variance relative to the marginal impact of the benchmark. For "varvalue" and "varmbvalue", the bounds are in variance. |
lin.direction |
a numeric vector (replicated to have length equal to the number of
columns in |
lin.rfloc |
a numeric vector or |
alpha.constraint |
a numeric vector giving lower bounds for the alphas (expected returns),
or a two-column matrix giving lower and upper bounds.
If this has names, then they should coerce to integers that are one
less than the column number of the desired column in the input or
default |
var.constraint |
a numeric vector giving upper bounds for the variances,
or a two-column matrix giving lower and upper bounds.
If this has (row) names, then they should coerce to integers giving
indices of the variance-benchmark combinations starting from zero
(that is, it is one less than the column number of the combination
in the input or default |
bench.constraint |
a named vector giving upper bounds for variances relative to the
named benchmarks, or a two-column matrix giving lower and
upper bounds.
The names need to be asset names represented in the |
dist.center |
either a named numeric vector, or a list with one or more components
that are named numeric vectors.
The names need to be asset ids like those of If this is a numeric vector, then it is changed to be a length one list with that vector as its component (hence making it length one for the purposes of the descriptions of the other distance arguments). The components of this list are the target portfolios from which distances are measured. |
dist.style |
a character vector that is replicated to have length equal to
the length of The styles that start with "custom" all use a price vector from
|
dist.bounds |
a numeric vector, numeric one-column matrix, or numeric two-column
matrix.
A numeric vector is coerced into a one-column matrix, and a
one-column matrix is coerced into a two-column matrix with the
first column consisting of all zeros.
The number of rows in this matrix may either be the number of
components in the |
dist.trade |
a logical vector that is replicated to be the length of
|
dist.utility |
a logical vector that is replicated to be the length of
|
dist.prices |
either a named numeric vector or a list with components that
are named numeric vectors.
If a list, it can either have length one, length equal to the
number of "custom" styles, or length equal to the length of
|
sum.weight |
a numeric vector with names that correspond to integers, or a
two-column matrix with row names.
This constrains the sum of the largest absolute weights relative to
the gross value.
The name of an element (or row) gives the number of the largest
weights to be summed, and the value gives the maximum (minimum and
maximum in the case of a matrix) for the sum of that number of weights.
For example, to limit the sum of the 5 largest weights to be no more
than 40 percent, the appropriate |
limit.cost |
either |
close.number |
if given, a numeric vector of length 1 or 2. This is the lower and upper bounds for the number of positions to close in the existing portfolio. If of length 1, then exactly that number of positions are to be closed. |
utility |
character string; the possibilities are (the start of):
|
risk.aversion |
number giving the risk aversion for the mean-variance or
mean-volatility utility.
The utility is the risk aversion times the variance (or volatility)
of the portfolio minus the expected return of the portfolio.
Many other formulations use one-half the variance, so the risk
aversion is different by a factor of two.
If you want infinite risk aversion, you should set the objective to
|
benchmark |
vector of one or more character strings naming the benchmark(s).
These must appear in the variance but need not appear as names in
|
bench.trade |
logical value.
If |
bench.weights |
a list where the name of each component is the name of a benchmark.
Each component should be a named numeric vector that sums to 1
(or 100).
All of the names on the weights need to be in |
long.buy.cost |
a vector or matrix of the cost to buy assets if they are long.
The meaning depends on whether or not If If this is a matrix with more than one column and If If this has names (row names in the case of a matrix), then it may contain information for assets that are not in the current problem. However, it must have information on all of the tradable assets that are in the current problem. |
long.sell.cost |
a vector or matrix of the cost to sell long assets.
See |
short.buy.cost |
a vector or matrix of the cost to buy short assets.
See |
short.sell.cost |
a vector or matrix of the cost to sell short assets.
See |
cost.par |
Not giving this argument is equivalent to giving a sequence of integers (that starts at zero if there is more than one column in the costs). If this is a matrix, then it should have row names that identify the assets. This form allows each asset to have its own individual exponents in the cost function. |
ucost |
a number that scales the trading costs in the utility.
For example, when maximizing returns, what is really maximized is
the portfolio return minus |
scale.cost |
a character string indicating what scaling should be done on the costs.
This must partially match one of: |
start.sol |
there are three possibilities (apart from If a single starting solution is given and |
allowance |
number less than one (but generally close to one) which provides
the allowable range of |
do.warn |
logical vector.
The most likely use is to give a vector of all |
penalty.constraint |
a numeric vector giving the multiplier for each penalty.
This is replicated to have length equal to the number of columns in
|
quantile |
a number between zero and one (inclusive) giving the quantile from the
utilities when there is more than one utility destination.
In general, it only makes sense to use numbers that are one-half or
greater.
When |
dest.wt |
numeric vector giving strictly positive weights to the destinations
of the combinations of expected returns and variances.
When this is given, then the |
utable |
a numeric matrix which controls the combinations of expected returns,
variances and benchmarks, and their placement into the destinations.
Providing this is only necessary if there are multiple
expected returns, variances or benchmarks, and the default
treatment of them is not what is desired.
The matrix has 6 rows: "alpha.spot" is the (zero-based) index of the
columns of |
atable |
numeric matrix with two rows describing the combinations
of expected returns and benchmarks.
The first row is the zero-based index of the expected returns
(that is, one less than the column index of |
vtable |
numeric matrix with three rows describing the combinations of variances
and benchmarks.
The first row is the zero-based index of the variance matrix
(that is, one less than the index of the third dimension
of |
dumpfile |
a character string giving the name of a file to be produced for the purposes of debugging in the event of certain errors being triggered due to a bug. This is hopefully a superfluous argument. No file is created by default. |
... |
individual arguments to |
seed |
the seed for the random number generator that is internal to the
optimizer.
The whole name ("seed") must be used – no abbreviations allowed.
If this is given, it will most likely be |
control |
a list like the output of |
identity |
an arbitrary object, most likely a string or an integer. This is merely added unchanged to the output. It is useful when an optimization is repeatedly done in parallel. The whole name ("identity") must be used – no abbreviations allowed. |
an object of class portfolBurSt which is a list with the following
components:
new.portfolio |
named numeric vector giving the optimal portfolio. The numbers are the number of units (shares, lots) of the assets. |
trade |
named numeric vector giving the trade needed to achieve the optimal portfolio. The numbers are the number of units (shares, lots) of the assets. |
results |
numeric vector containing various results from the optimization. It contains the objective achieved, the negative utility, the trading cost, and the penalty (for unmet constraints) component of the objective. The objective is the sum of the negative utility and the penalty. The cost is included (possibly non-linearly) in the negative utility. |
converged |
logical value(s): if a sufficiently large number of consecutive
iterations failed to improve the solution, then convergence is
declared to have occurred.
If |
objective.utility |
character string stating the utility that is the objective of the optimization. |
universe.size |
a vector giving the number of assets in the universe, the number
of assets allowed to trade, the number that the |
utable |
the utility table that was input or created for the problem. This component is suppressed when the object is printed. |
atable |
the input or created |
vtable |
the input or created |
alpha.values |
vector with length equal to the number of expected return-benchmark combinations giving the (ex-ante) expected return(s) of the optimal portfolio. The values assume that the net asset value is the gross value. Scaling is called for if that assumption is not correct. |
var.values |
vector with length equal to the number of variance-benchmark combinations giving the (ex-ante) variance(s) of the optimal portfolio. The values assume that the net asset value is the gross value. Scaling is called for if that assumption is not correct. |
utility.values |
vector of the negative utilities
(including costs and penalties) of the optimal portfolio.
If there are multiple values, these are sorted – they are not in
the same order as the columns of |
constraint.violations |
numeric vector of the violations of each constraint type. Zero means the constraint is satisfied. This component is suppressed when the object is printed. |
penalty.constraint |
numeric vector of the penalties for each constraint type. This component is suppressed when the object is printed. |
value.limits |
two-column matrix giving the lower and upper bounds of the gross value, net value, long value and short value. This component is suppressed when the object is printed, but is shown in the summary. |
prices |
the vector of prices of relevant assets used in the optimization. That is, prices relevant to the solution found. This component is suppressed when the object is printed. |
optim.mumbo.jumbo |
numeric vector of results of the optimization.
The first four values pertain to the final run (which is the
only run unless The next group refers to the entire optimization. These give: the number of portfolios evaluated, the total number of requested evaluations, the number of evaluations requested that were amusingly silly, and a flag denoting the state of the result. Finally there are values that are relevant when This component is suppressed when the object is printed, but is shown in the summary. |
risk.fraction |
if argument |
existing |
the existing portfolio before the optimization. |
violated |
character vector giving the types (if any) of constraints that are broken by the solution. |
seed |
the random seed for the internal random number generator of the optimizer. This component is suppressed when the object is printed. |
version |
character vector giving the version numbers for the C code and the S language code. This component is suppressed when the object is printed. |
sizes |
numeric vector describing aspects of the problem – generally uninteresting. This component is suppressed when the object is printed. |
dist.table |
a data frame giving information on each distance.
The columns are: |
dist.center |
a list giving the target portfolio(s). This component is suppressed when the object is printed. |
dist.prices |
either |
benchmarks |
vector giving any benchmarks used.
The names of the vector are the names of the benchmarks.
The values in the vector are the zero-based indices of the benchmarks
as used in |
forced.explicit |
vector giving the trades explicitly forced using the
|
positions.forced |
vector giving the trades that were forced via the
|
all.forced |
vector giving the trades that were forced – including trades
implicitly forced in order to try to satisfy maximum weight
constraints, and trades forced via the |
positions |
the input |
tol.positions |
the input |
lin.table |
a data frame showing information on the set of linear constraints, one row for each (top-level) constraint. The columns are:
This component is not present unless |
con.realized |
a list with 0 or 1 components. The possible component is:
This component is suppressed when the object is printed, but is shown in the summary. |
iterhistory |
a vector giving the objective value at each iteration of the
optimization process.
The length of this is two more than the number of iterations done:
the first value gives the objective after the pre-iteration phase,
and the last value gives the objective after the post-iteration phase.
This is only present if the |
identity |
the input value of |
checkinput |
a list containing components: |
timestamp |
two character strings giving the time and date of creation (the time the command started and the time it ended). |
call |
an image of the call that created the object. |
The S language random seed is changed or created if the
seed argument is such that it generates a new seed.
That is not the default.
SPECIFYING THE MONETARY VALUE.
While prices is always required, there is no
other single argument that is always required.
However, there is the requirement that the value
of the portfolio be specified somehow.
For any problem, specifying turnover is enough.
For long-only problems, any one of gross.value, long.value or
net.value being specified works.
For long-short portfolios, then either gross.value and net.value
need to be specified, or long.value and short.value (assuming
that turnover has not been given).
If any of the gross, net, long or short values are given, then at least one
pair – either gross and net, or long and short – need to be given.
BOUNDING THE TRADE.
The lower.trade and upper.trade arguments
bound the number of units that may be traded for each asset.
The optimizer insists that the range of allowable units includes zero.
But you can use the forced.trade argument to specify trades that
must be performed.
You can also give the positions argument which is in monetary
value rather than shares (or lots).
positions can give limits on the value in the portfolio and
the value to trade including forced trades; it can also specify assets
not to trade (by placing NA in at least one of the first four
columns of that row).
THE PENALTY. The objective not only includes the negative utility (including the cost of the trade) but also a penalty for breaking constraints. If the penalty is non-zero, then the optimization should generally be redone, or the constraints changed – the portfolio may not be very optimal when penalties are imposed. There are times when trivial penalties are imposed by constraints that are at the boundary – the optimality will not be affected in such cases.
SPEED AND OPTIMIZATION QUALITY.
There is a trade-off between the time the optimizer takes and the quality
of the optimization that you are likely to get.
The control arguments iterations.max and fail.iter can
be increased to improve the solution, or decreased to speed the optimization.
The quality can also be improved by increasing stringency from zero.
The User's Manual has more on this.
If the existing portfolio breaks maximum weight constraints
(from the max.weight argument), there are situations in which
the optimal portfolio will not satisfy those constraints – see the
User's Manual for more information.
For long-only portfolios if more than one of gross.value,
net.value, long.value is given, then the range of all these
values is used as the allowable interval.
More logical (but much more bother) would be the intersection of the
intervals.
Best is to just give one of these.
The trace argument has no effect under Windows in some cases – tracing
information on this platform is given in batch mode.
This help was last revised 2012 June 10.
trade.optimizer.control,
build.constraints, random.portfolio,
summary.portfolBurSt,
deport.portfolBurSt, seed.BurSt,
constraints.realized,
Cfrag.list.
# build long-only portfolio worth 1 million containing no more than 60 assets op1 <- trade.optimizer(eq.prices, varian, long.only=TRUE, gross.value=1e6, ntrade=60) summary(op1) deport(op1) # csv file of the trade # revise a long-short portfolio, keep the number of assets no more than 50 op2 <- trade.optimizer(eq.prices, varian, gross.value=1e6, net.value=c(-1e3, 5e3), exist=cur.port, port.size=50)