Fees

Fees lower the return of a portfolio, and they can be large for an active strategy or a small portfolio. The types below state the fees that an optimisation deducts, such as proportional and fixed fees on long and short positions, and a fee on the turnover. AmortisedFees spreads the fixed fees evenly over the holding period, and FirstObservationFees charges them once, on the first observation.

PortfolioOptimisers.FeesEstimator — Type
struct FeesEstimator{__T_tn, __T_l, __T_s, __T_fl, __T_fs, __T_lq, __T_flq, __T_dl, __T_ds, __T_dfl, __T_dfs, __T_fa, __T_kwargs} <: AbstractEstimator

Names the per-asset fee rates, for fees_constraints to align to a universe.

Every fee field accepts a dictionary, a pair, or a vector of pairs keyed by asset or group name. The matching d* field fills every asset that the keys miss. Each default fills only its own field. l draws on dl, s on ds, fl on dfl and fs on dfs, and none draws on a neighbour's default. fees_constraints resolves the names against a UniverseSets and returns a Fees, whose fee fields are plain per-asset vectors and whose kwargs is the kwargs of this estimator.

Warning

l, s, tn and lq are rates per period, and each of them charges on every observation of a return series. fa affects none of the four. fl, fs and flq charge each non-zero position one time for the whole holding period. On a return series they are a fraction of capital, and in the finite allocation they are a currency amount. fa names the clock they fall on, and a nothing fa charges them on the first observation. The units of the fees and returns must also be consistent.

Fields

  • tn: Turnover estimator or result.
  • l: Long proportional fees.
  • s: Short proportional fees. A negative rate down to -l is a credit on a short position.
  • fl: Long fixed fees.
  • fs: Short fixed fees.
  • lq: Proportional liquidation fees, which the fee charges when a position leaves the Investable Mask. The field lq is on the complement of the mask, so its entries are the assets that left and not the assets held. A forced exit trades to zero, so the charge is the rate times the absolute previous weight, and it falls on every period beside l, s and tn.
  • flq: Fixed liquidation fees, which the fee charges when a position leaves the Investable Mask. The field flq is on the complement of the mask, as lq is. The fee charges the amount once for each entry whose absolute previous weight is not isapprox to zero under kwargs, and it falls on the clock fa names beside fl and fs.
  • dl: Default long proportional fee.
  • ds: Default short proportional fee. A negative rate is a credit on a short position.
  • dfl: Default long fixed fee.
  • dfs: Default short fixed fee.
  • fa: Fee amortisation algorithm, and the clock the fixed fee terms fl, fs and flq fall on. nothing and a FirstObservationFees charge them one time, on the first observation of a return series. An AmortisedFees spreads them evenly over the observation count that the calling site passes in. It affects no other term, because l, s, tn and lq are rates per period.
  • kwargs: Named tuple of keyword arguments that the fixed fee terms forward to isapprox, to decide how near zero a weight must be to count as zero.

Constructors

FeesEstimator(;    tn::Option{<:TnE_Tn} = nothing,    l::Option{<:VectorEstValType} = nothing,    s::Option{<:VectorEstValType} = nothing,    fl::Option{<:VectorEstValType} = nothing,    fs::Option{<:VectorEstValType} = nothing,    lq::Option{<:TnE_Tn} = nothing,    flq::Option{<:TnE_Tn} = nothing,    dl::Option{<:Number} = nothing,    ds::Option{<:Number} = nothing,    dfl::Option{<:Number} = nothing,    dfs::Option{<:Number} = nothing,    fa::Option{<:AbstractFeeAmortisation} = nothing,    kwargs::NamedTuple = (; atol = 1e-8)) -> FeesEstimator

Keywords correspond to the struct's fields.

Validation

Propagated parameters

factory passes its arguments on to these @fprop fields:

  • tn: Recursively updated through factory.
  • lq: Recursively updated through factory.
  • flq: Recursively updated through factory.

View parameters

This type spans two axes, so this file defines port_opt_view for it by hand rather than generating the method from @vprop tags, and no field carries such a tag. tn, l, s, fl and fs price the positions the portfolio holds, so the view slices them to the selected indices. lq and flq price the positions the portfolio must sell, so the view slices them to the complement of those indices. The verb derives that complement from the width of the unreduced returns matrix it receives:

  • tn: Recursively viewed at the selected indices through port_opt_view.
  • l: Sliced to the selected indices.
  • s: Sliced to the selected indices.
  • fl: Sliced to the selected indices.
  • fs: Sliced to the selected indices.
  • lq: Recursively viewed at the complement of the selected indices.
  • flq: Recursively viewed at the complement of the selected indices.

Examples

julia> FeesEstimator(; tn = TurnoverEstimator(; w = [0.2, 0.3, 0.5], val = Dict("A" => 0.1)),                     l = Dict("A" => 0.001, "B" => 0.002), s = ["A" => 0.001, "B" => 0.002],                     fl = Dict("A" => 5.0), fs = ["B" => 10.0])FeesEstimator      tn ┼ TurnoverEstimator         │       w ┼ Vector{Float64}: [0.2, 0.3, 0.5]         │     val ┼ Dict{String, Float64}: Dict("A" => 0.1)         │    dval ┼ nothing         │   fixed ┴ Bool: false       l ┼ Dict{String, Float64}: Dict("B" => 0.002, "A" => 0.001)       s ┼ Vector{Pair{String, Float64}}: ["A" => 0.001, "B" => 0.002]      fl ┼ Dict{String, Float64}: Dict("A" => 5.0)      fs ┼ Vector{Pair{String, Float64}}: ["B" => 10.0]      lq ┼ nothing     flq ┼ nothing      dl ┼ nothing      ds ┼ nothing     dfl ┼ nothing     dfs ┼ nothing      fa ┼ nothing  kwargs ┴ @NamedTuple{atol::Float64}: (atol = 1.0e-8,)

Related

source
PortfolioOptimisers.Fees — Type
struct Fees{__T_tn, __T_l, __T_s, __T_fl, __T_fs, __T_lq, __T_flq, __T_fa, __T_kwargs, __T_imsk} <: AbstractResult

Charges a portfolio a turnover fee, a long or short proportional fee, and a long or short fixed fee.

The long and short components key on the sign of the position, not on the sign of the trade. An asset held long pays l and fl, and an asset held short pays s and fs, whatever the direction of the rebalance that reached it. The turnover component keys on the trade instead. It holds a Turnover whose val is a per-asset fee rate rather than a bound.

Two more fields charge a forced exit. lq is a proportional rate and flq a fixed amount, and both price the sale to zero of the positions of the assets that left the Investable Mask. Each is a Turnover whose w holds the previous weights of those assets.

A fixed fee is a charge per position held, whatever its size. Only a non-zero position pays one, and isapprox receives kwargs to decide how near zero counts as zero.

A fee value is either a scalar, which applies to all assets, or a vector of per-asset values. calc_fees computes the portfolio fees and calc_asset_fees the per asset fees.

Warning

l, s, tn and lq are rates per period, and each of them charges on every observation of a return series. fa affects none of the four. fl, fs and flq charge each non-zero position one time for the whole holding period. On a return series they are a fraction of capital, and in the finite allocation they are a currency amount. fa names the clock they fall on, and a nothing fa charges them on the first observation. The units of the fees and returns must also be consistent.

The axis a fee is on

A caller states every field over the full universe, the two liquidation charges lq and flq included, because a caller cannot know which asset will delist. investable_fees_view then places the fee on the axes the Investable Mask defines. It puts the five holding fields on the investable assets and lq and flq on the complement.

investable_fees_view records the mask it reduced on in imsk, so a reader of the returned fee can tell which axes it is on. A nothing imsk means the fee is a caller's statement on the full universe, and a BitVector means investable_fees_view has run. A caller never writes the field. When an unmarked fee meets investable_fees_view with no mask, no asset has left. The verb then drops lq and flq, and does not charge every position as a forced exit. When a marked fee meets investable_fees_view a second time, the verb returns it untouched.

Mathematical definition

Portfolio fees

On a return series, the fee of the portfolio has a term per period and a one-off term:

\[\begin{align} F_{\text{r}}(\boldsymbol{w}) &\coloneqq F_{\text{Tn}} + F_{\text{p}} + F_{\text{lq}} \\ F_{\text{o}}(\boldsymbol{w}) &\coloneqq F_{\text{f}} + F_{\text{flq}} \\ F_{\text{Tn}}(\boldsymbol{w}) &= \boldsymbol{Tn} \cdot \boldsymbol{f}_{\text{Tn}}\\ F_{\text{p}}(\boldsymbol{w}) &= \left(1\left\{\boldsymbol{w} \geq 0\right\} \odot \boldsymbol{w}\right) \cdot \boldsymbol{f}_{\text{p}}^{+} - \left(1\left\{\boldsymbol{w} \lt 0\right\} \odot \boldsymbol{w}\right) \cdot \boldsymbol{f}_{\text{p}}^{-} \\ F_{\text{f}}(\boldsymbol{w}) &= 1\left\{\boldsymbol{w} \geq 0 \land \boldsymbol{w} \neq 0\right\} \cdot \boldsymbol{f}_{\text{f}}^{+} + 1\left\{\boldsymbol{w} \lt 0 \land \boldsymbol{w} \neq 0\right\} \cdot\boldsymbol{f}_{\text{f}}^{-} \\ F_{\text{lq}} &= \left\lvert \boldsymbol{w}_{\text{lq}} \right\rvert \cdot \boldsymbol{f}_{\text{lq}} \\ F_{\text{flq}} &= 1\left\{\boldsymbol{w}_{\text{flq}} \neq 0\right\} \cdot \boldsymbol{f}_{\text{flq}} \end{align}\]

The clock fa puts the one-off term on the observations $t = 1, \ldots, T$ of the series:

\[\begin{align} c_t(\boldsymbol{w}) &= F_{\text{r}}(\boldsymbol{w}) + 1\left\{t = 1\right\} F_{\text{o}}(\boldsymbol{w}) && \text{a } \texttt{nothing} \text{ or a } \texttt{FirstObservationFees} \text{ clock,} \\ c_t(\boldsymbol{w}) &= F_{\text{r}}(\boldsymbol{w}) + \frac{F_{\text{o}}(\boldsymbol{w})}{T} && \text{an } \texttt{AmortisedFees} \text{ clock,} \\ \sum_{t = 1}^{T} c_t(\boldsymbol{w}) &= T F_{\text{r}}(\boldsymbol{w}) + F_{\text{o}}(\boldsymbol{w}) && \text{on each clock.} \end{align}\]

The finite allocation charges the same three terms on the money value of the shares it buys. It holds the share counts and the prices, so $\boldsymbol{x} \odot \boldsymbol{p}$ is the exact money in each position. set_allocation_fees! writes the terms in the allocation's own variables, and the charge enters the budget constraint rather than the cash. On a return series fl and fs are a fraction of capital, and in the allocation they are a currency amount.

Per asset fees

The per asset fees use the same definitions, with an elementwise (Hadamard) product in place of each dot product. The terms of the assets that stay and the terms of the assets that left are on two different axes, so they are two vectors and never one sum.

\[\begin{align} \boldsymbol{F}_{\text{r}}(\boldsymbol{w}) &\coloneqq \boldsymbol{F}_{\text{Tn}} + \boldsymbol{F}_{\text{p}} \\ \boldsymbol{F}_{\text{o}}(\boldsymbol{w}) &\coloneqq \boldsymbol{F}_{\text{f}} \\ \boldsymbol{F}_{\text{Tn}}(\boldsymbol{w}) &= \boldsymbol{Tn} \odot \boldsymbol{f}_{\text{Tn}}\\ \boldsymbol{F}_{\text{p}}(\boldsymbol{w}) &= \left(1\left\{\boldsymbol{w} \geq 0\right\} \odot \boldsymbol{w}\right) \odot \boldsymbol{f}_{\text{p}}^{+} - \left(1\left\{\boldsymbol{w} \lt 0\right\} \odot \boldsymbol{w}\right) \odot \boldsymbol{f}_{\text{p}}^{-} \\ \boldsymbol{F}_{\text{f}}(\boldsymbol{w}) &= 1\left\{\boldsymbol{w} \geq 0 \land \boldsymbol{w} \neq 0\right\} \odot \boldsymbol{f}_{\text{f}}^{+} + 1\left\{\boldsymbol{w} \lt 0 \land \boldsymbol{w} \neq 0\right\} \odot\boldsymbol{f}_{\text{f}}^{-} \\ \boldsymbol{F}_{\text{lq}} &= \left\lvert \boldsymbol{w}_{\text{lq}} \right\rvert \odot \boldsymbol{f}_{\text{lq}} \\ \boldsymbol{F}_{\text{flq}} &= 1\left\{\boldsymbol{w}_{\text{flq}} \neq 0\right\} \odot \boldsymbol{f}_{\text{flq}} \end{align}\]

Where:

  • $\boldsymbol{w}$: Portfolio weights vector $N \times 1$.
  • $F$: Portfolio fee.
  • $\boldsymbol{F}$: N × 1 per asset vector of portfolio fees. $\boldsymbol{F}_{\text{lq}}$ and $\boldsymbol{F}_{\text{flq}}$ have one entry per asset that left the Investable Mask instead.
  • $\boldsymbol{f}$: N × 1 per asset fee vector. A scalar value applies to all assets.
  • $\text{r},\, \text{o}$: Subscripts for the term per period and the one-off term.
  • $\text{lq},\, \text{flq}$: Subscripts for the proportional and the fixed liquidation charge, the fields lq and flq. They price the assets that left the Investable Mask.
  • $\boldsymbol{w}_{\text{lq}},\, \boldsymbol{w}_{\text{flq}}$: The previous weights of the assets that left, the w field of lq and of flq. A forced exit sells each position to zero, so the traded amount is $\left\lvert \boldsymbol{w}_{\text{lq}} \right\rvert$, and the fee charges both sides.
  • $c_t$: Fee charged at observation $t$ of a return series.
  • $T$: Number of observations.
  • $\boldsymbol{Tn}$: N × 1 turnover vector as defined in Turnover. The w field of the turnover object holds the benchmark weight vector, and the new weight vector is the portfolio weight vector.
  • $+,\, -$: Superscripts for the long and the short fees. Brokers sometimes charge different fees for long and short positions.
  • $\text{Tn},\, \text{p},\, \text{f}$: Subscripts for the turnover, the proportional and the fixed fees. A Turnover holds the turnover fee, and its val is the per asset fee rate.
  • $1\left\{\cdot\right\}$: Elementwise (Hadamard) indicator function. It is 1 when the condition is true and 0 otherwise. It selects the long fee where the asset weight is non-negative and the short fee otherwise.
  • $\boldsymbol{w} \neq 0$: Evaluated as !isapprox(w, 0; kwargs...), so kwargs decides how near zero counts as zero. Only the fixed terms use this test, $\boldsymbol{w}_{\text{flq}} \neq 0$ included, because a proportional fee on a zero weight is zero.
  • $\odot$: Elementwise (Hadamard) product.

The turnover term is the linear transaction cost of Equations 1 and 2 of [186], with one rate on the two sides of the trade. The proportional and the fixed fees key on the sign of the position, not of the trade, so they price a holding.

The definition subtracts the short proportional term. $\boldsymbol{w}$ is negative wherever its indicator is one, so the minus sign makes the fee a positive charge when $\boldsymbol{f}_{\text{p}}^{-}$ is non-negative.

A negative short rate is a credit

s can be negative down to -l, asset by asset. Per asset, the proportional term $\phi(w) = l \max(w, 0) - s \min(w, 0)$ has slope $l$ for $w > 0$ and $-s$ for $w < 0$, so it is convex if and only if $s \geq -l$. The constructor refuses a rate below that bound, because a model that charges a non-convex fee is no longer convex.

The two ends of the range model two kinds of fee:

  • s = l charges $l \lvert w \rvert$. This is a carry or a borrow cost on the position, whatever its sign.
  • s = -l charges the linear $l w$. This is an expense ratio that the instrument holds and that the returns do not show. It lowers the return of the asset, and a short position earns it.

The default leaves s at nothing, which charges a short position nothing.

The per asset fees sum to the portfolio fee

The two families compute the same definition. calc_asset_fees splits over the assets what calc_fees contracts into a scalar, so the entries of the vector sum to the scalar. The sums differ in the order in which they add, so the identity holds to rounding and not to ==.

The JuMP model charges the same fee only when the decomposition is pinned

set_non_fixed_fees! writes the proportional terms against the model's lw and sw variables rather than against $\boldsymbol{w}$, and it writes no fixed term at all. A fixed fee needs a binary variable, and the MIP builder writes it instead.

Under a PartsBoundWeights head those variables only bound the parts of $\boldsymbol{w}$, so the model's fee is an upper bound on this definition. The budget pins sum(lw) and sum(sw) whether or not the portfolio holds a short position, so the model charges both sides in full.

Setting xbgt = true on the JuMPOptimiser pins the decomposition, and the model's fee then agrees with this definition. The setting adds binary variables, so the same problem then needs a mixed-integer conic solver rather than a conic one.

A negative s does not use the parts. With the parts, a slack sw earns a credit on an exposure that the model does not hold, and the bound falls below the definition. set_proportional_fees! charges the whole proportional fee against $\boldsymbol{w}$ instead, through the epigraph of $\max(l w, -s w)$, which equals the definition exactly.

A long-only model needs no pinning, because it holds no short side to bound.

Fee amortisation

l, s, tn and lq are rates per period. Each of them charges one time per observation of a return series, and fa affects none of them.

fl, fs and flq charge each non-zero position one time for the whole holding period, as a fraction of capital on a return series and as a currency amount in the finite allocation. The fa field names the clock they fall on. Both calc_fees and calc_asset_fees return a pair, (amortised, one_time).

A nothing fa puts the fixed charges in one_time, and charge_fees subtracts that from the first observation alone. An AmortisedFees divides them by the observation count and adds them to amortised, so every observation carries an equal share and one_time is zero.

The field holds no count. Every site that charges a fee knows the count it charges over and passes it in, so the count cannot disagree with the series. The JuMP model applies the same rule through :one_time_fees and charge_one_time_fees.

The two clocks charge the same total over a horizon of T observations, which is the number calc_total_fees reports. They differ in where they place the one-off charge. One charges it whole on the first observation, and the other charges a 1 / T share of it on each observation. So the charge of each observation differs, and so does the path of the cumulative return.

Fields

  • tn: Turnover result.
  • l: Long proportional fees.
  • s: Short proportional fees. A negative rate down to -l is a credit on a short position.
  • fl: Long fixed fees.
  • fs: Short fixed fees.
  • lq: Proportional liquidation fees, which the fee charges when a position leaves the Investable Mask. The field lq is on the complement of the mask, so its entries are the assets that left and not the assets held. A forced exit trades to zero, so the charge is the rate times the absolute previous weight, and it falls on every period beside l, s and tn.
  • flq: Fixed liquidation fees, which the fee charges when a position leaves the Investable Mask. The field flq is on the complement of the mask, as lq is. The fee charges the amount once for each entry whose absolute previous weight is not isapprox to zero under kwargs, and it falls on the clock fa names beside fl and fs.
  • fa: Fee amortisation algorithm, and the clock the fixed fee terms fl, fs and flq fall on. nothing and a FirstObservationFees charge them one time, on the first observation of a return series. An AmortisedFees spreads them evenly over the observation count that the calling site passes in. It affects no other term, because l, s, tn and lq are rates per period.
  • kwargs: Named tuple of keyword arguments that the fixed fee terms forward to isapprox, to decide how near zero a weight must be to count as zero.
  • imsk: The Investable Mask that investable_fees_view reduced this fee on, or nothing when the fee is a caller's statement over the full universe. Only investable_fees_view writes it, and a caller never does. When it is set, tn, l, s, fl and fs are on findall(imsk), and lq and flq are on its complement. investable_fees_view returns a marked fee unchanged when it meets it a second time. It drops or slices the two liquidation charges, lq and flq, of an unmarked fee only.

Constructors

Fees(;    tn::Option{<:Turnover} = nothing,    l::Option{<:Num_VecNum} = nothing,    s::Option{<:Num_VecNum} = nothing,    fl::Option{<:Num_VecNum} = nothing,    fs::Option{<:Num_VecNum} = nothing,    lq::Option{<:Turnover} = nothing,    flq::Option{<:Turnover} = nothing,    fa::Option{<:AbstractFeeAmortisation} = nothing,    kwargs::NamedTuple = (; atol = 1e-8),    imsk::Option{<:BitVector} = nothing) -> Fees

Keywords correspond to the struct's fields.

Validation

Propagated parameters

factory passes its arguments on to these @fprop fields:

  • tn: Recursively updated through factory.
  • lq: Recursively updated through factory.
  • flq: Recursively updated through factory.

View parameters

This type spans two axes, so this file defines port_opt_view for it by hand rather than generating the method from @vprop tags, and no field carries such a tag. tn, l, s, fl and fs price the positions the portfolio holds, so the view slices them to the selected indices. lq and flq price the positions the portfolio must sell, so the view slices them to the complement of those indices. The verb derives that complement from the width of the unreduced returns matrix it receives:

  • tn: Recursively viewed at the selected indices through port_opt_view.
  • l: Sliced to the selected indices.
  • s: Sliced to the selected indices.
  • fl: Sliced to the selected indices.
  • fs: Sliced to the selected indices.
  • lq: Recursively viewed at the complement of the selected indices.
  • flq: Recursively viewed at the complement of the selected indices.
  • imsk: Set to nothing. The view only slices, and investable_fees_view writes the mark.

The pretty-printer shows imsk only when it is set, so a fee that a caller wrote prints no imsk row. See show_fields.

Examples

julia> Fees(; tn = Turnover(; w = [0.2, 0.3, 0.5], val = [0.1, 0.0, 0.0]), l = [0.001, 0.002, 0.0],            s = [0.001, 0.002, 0.0], fl = [5.0, 0.0, 0.0], fs = [0.0, 10.0, 0.0])Fees      tn ┼ Turnover         │       w ┼ Vector{Float64}: [0.2, 0.3, 0.5]         │     val ┼ Vector{Float64}: [0.1, 0.0, 0.0]         │   fixed ┴ Bool: false       l ┼ Vector{Float64}: [0.001, 0.002, 0.0]       s ┼ Vector{Float64}: [0.001, 0.002, 0.0]      fl ┼ Vector{Float64}: [5.0, 0.0, 0.0]      fs ┼ Vector{Float64}: [0.0, 10.0, 0.0]      lq ┼ nothing     flq ┼ nothing      fa ┼ nothing  kwargs ┴ @NamedTuple{atol::Float64}: (atol = 1.0e-8,)

Related

References

  • [186] G. A. Pogue. An extension of the Markowitz portfolio selection model to include variable transactions' costs, short sales, leverage policies and taxes. Working paper 388-69B, Sloan School of Management, Massachusetts Institute of Technology (1969). Pages 8 and 12, Equations 1 and 2.
source
PortfolioOptimisers.AmortisedFees — Type
struct AmortisedFees <: AbstractFeeAmortisation

Spreads the one-off terms of a fee, the fixed charges fl, fs and flq, evenly over a holding period.

The algorithm holds no number. Every site that charges a fee knows the observation count it charges over and passes it in. charge_fees passes the length of the series, calc_total_fees takes the horizon as an argument, and the model passes the observation count of the fit. No field stores the count of the holding period, so the count cannot disagree with the series that the site charges.

FirstObservationFees names the other clock, which charges the two fixed terms one time, on the first observation. A nothing fa names that clock too.

The turnover charge tn is a rate per period, so it charges on every observation beside l and s. This algorithm never divides it.

Constructors

AmortisedFees() -> AmortisedFees

Examples

julia> AmortisedFees()AmortisedFees()

Related

source
PortfolioOptimisers.FirstObservationFees — Type
struct FirstObservationFees <: AbstractFeeAmortisation

Charges the one-off terms of a fee, the fixed charges fl, fs and flq, on the first observation of a return series.

The algorithm holds no number, and it names the same clock as a nothing fa. On a Fees it is therefore a synonym for nothing. A caller that must name this clock uses this type. The fa field of a cross-validation scheme is such a caller. There nothing inherits the clock of the fee, so the scheme needs a different value for the first-observation clock.

The turnover charge tn is a rate per period, so it charges on every observation beside l and s. This algorithm never moves it.

Constructors

FirstObservationFees() -> FirstObservationFees

Examples

julia> FirstObservationFees()FirstObservationFees()

Related

source
PortfolioOptimisers.fees_constraints — Function
fees_constraints(fees::FeesEstimator, sets::UniverseSets; datatype::DataType = Float64,
                 strict::Bool = false)

Resolve the name-keyed fee fields of a FeesEstimator against a universe into a Fees of plain per-asset vectors.

Seven fields carry a specification keyed by name: the three turnover fields tn, lq and flq, and the four proportional and fixed fields. Each of the four fills its gaps from its own default. l draws on dl, s on ds, fl on dfl and fs on dfs. A default never fills a neighbour's field. The three turnover fields resolve through turnover_constraints, so a FeesEstimator that holds a TurnoverEstimator returns a Fees that holds a Turnover. fees.fa holds no universe-keyed specification, so the result holds it unchanged.

Algorithm

  1. Resolve fees.tn against the universe of sets with turnover_constraints. The result is a Turnover whose val is one turnover fee rate per asset. A nothing tn stays nothing.
  2. Resolve fees.l with estimator_to_val into l, one long proportional rate per asset in the order of the universe. Every asset the keys miss takes fees.dl, or zero(datatype) when fees.dl is nothing. A nothing fees.l stays nothing.
  3. Resolve fees.s the same way against fees.ds into s.
  4. Resolve fees.fl the same way against fees.dfl into fl.
  5. Resolve fees.fs the same way against fees.dfs into fs.
  6. Resolve fees.lq and fees.flq the same way as step 1, over the full universe of sets. investable_fees_view moves them to the complement of the Investable Mask later.
  7. Build a Fees from the seven resolved fields, fees.fa unchanged, and fees.kwargs unchanged. fees.kwargs sets how near zero a weight must be for the fixed terms to treat it as zero.

Arguments

  • fees: The FeesEstimator that states the turnover, proportional and fixed fee values.
  • sets: The UniverseSets that holds the asset names or indices.
  • datatype: Output data type for fee values.
  • strict: If true, a mismatch between assets and fee values throws an error. If false, it warns.

Validation

  • A key that names neither an asset nor a group of sets raises an ArgumentError when strict is true, and warns otherwise. Steps 1 to 6 each check their own field, so one bad key in l raises whatever s, fl and fs hold.

Returns

  • fe::Fees: The turnover, proportional and fixed fee values, aligned with sets.

Examples

julia> sets = UniverseSets(; dict = Dict("nx" => ["A", "B", "C"]));julia> fees = FeesEstimator(;                            tn = TurnoverEstimator(; w = [0.2, 0.3, 0.5], val = Dict("A" => 0.1),                                                   dval = 0.0),                            l = Dict("A" => 0.001, "B" => 0.002), s = ["A" => 0.001, "B" => 0.002],                            fl = Dict("A" => 5.0), fs = ["B" => 10.0]);julia> fees_constraints(fees, sets)Fees      tn ┼ Turnover         │       w ┼ Vector{Float64}: [0.2, 0.3, 0.5]         │     val ┼ Vector{Float64}: [0.1, 0.0, 0.0]         │   fixed ┴ Bool: false       l ┼ Vector{Float64}: [0.001, 0.002, 0.0]       s ┼ Vector{Float64}: [0.001, 0.002, 0.0]      fl ┼ Vector{Float64}: [5.0, 0.0, 0.0]      fs ┼ Vector{Float64}: [0.0, 10.0, 0.0]      lq ┼ nothing     flq ┼ nothing      fa ┼ nothing  kwargs ┴ @NamedTuple{atol::Float64}: (atol = 1.0e-8,)julia> fees = FeesEstimator(;                            tn = TurnoverEstimator(; w = [0.2, 0.3, 0.5], val = Dict("A" => 0.1),                                                   dval = 0.0, fixed = true),                            l = Dict("A" => 0.001, "B" => 0.002), s = ["A" => 0.001, "B" => 0.002],                            fl = Dict("A" => 5.0), fs = ["B" => 10.0]);julia> fees_constraints(fees, sets)Fees      tn ┼ Turnover         │       w ┼ Vector{Float64}: [0.2, 0.3, 0.5]         │     val ┼ Vector{Float64}: [0.1, 0.0, 0.0]         │   fixed ┴ Bool: true       l ┼ Vector{Float64}: [0.001, 0.002, 0.0]       s ┼ Vector{Float64}: [0.001, 0.002, 0.0]      fl ┼ Vector{Float64}: [5.0, 0.0, 0.0]      fs ┼ Vector{Float64}: [0.0, 10.0, 0.0]      lq ┼ nothing     flq ┼ nothing      fa ┼ nothing  kwargs ┴ @NamedTuple{atol::Float64}: (atol = 1.0e-8,)

Related

source
fees_constraints(fees::Option{<:Fees}, args...; kwargs...)

Return a resolved fee, or nothing, unchanged.

This method lets a caller call fees_constraints on any fee input: a FeesEstimator, a Fees or nothing. It returns a Fees or a nothing as it receives it.

Algorithm

  1. Return fees. A Fees already holds one rate per asset, so the method resolves no universe. The method reads none of its other arguments and none of its keywords.

Arguments

  • fees: An existing Fees object or nothing.
  • args...: Positional arguments that the method does not read.
  • kwargs...: Keyword arguments that the method does not read.

Returns

  • fe::Option{<:Fees}: The input constraint object, unchanged.

Examples

julia> fees = Fees(; tn = Turnover(; w = [0.2, 0.3, 0.5], val = [0.1, 0.0, 0.0]),                   l = [0.001, 0.002, 0.0]);julia> fees_constraints(fees)Fees      tn ┼ Turnover         │       w ┼ Vector{Float64}: [0.2, 0.3, 0.5]         │     val ┼ Vector{Float64}: [0.1, 0.0, 0.0]         │   fixed ┴ Bool: false       l ┼ Vector{Float64}: [0.001, 0.002, 0.0]       s ┼ nothing      fl ┼ nothing      fs ┼ nothing      lq ┼ nothing     flq ┼ nothing      fa ┼ nothing  kwargs ┴ @NamedTuple{atol::Float64}: (atol = 1.0e-8,)julia> fees_constraints(nothing)

Related

source
PortfolioOptimisers.calc_fees — Function
calc_fees(w::VecNum, ::Nothing, ::Function)
calc_fees(w::VecNum, fees::Number, op::Function)
calc_fees(w::VecNum, fees::VecNum, op::Function)

Compute the proportional fees for portfolio weights.

This is one term of the total fee. calc_periodic_fees calls it twice, under .>= for the long side and under .< for the short side, and negates the short call. Fees states the closed form as $F_{\text{p}}$.

Algorithm

  1. On a nothing fees, return zero(eltype(w)). The method reads neither w nor op.
  2. Otherwise build idx, the mask of the assets that op selects against a zero of the promoted element type.
  3. On a Number fees, scale the selected weights by the one rate, and sum them.
  4. On a VecNum fees, contract the selected rates with the selected weights.

Arguments

  • w: Portfolio weights.

  • fees: Fee rate.

    • nothing: No proportional fee. The method returns zero.
    • Number: One rate for every asset that op selects.
    • VecNum: One rate per asset.
  • op: Broadcast comparison that selects the assets, .>= for the long side and .< for the short side. A nothing fees does not read it.

Returns

  • val::Number: The proportional fee of the selected side.

Examples

julia> calc_fees([0.1, 0.2], 0.01, .>=)0.003

Related

source
calc_fees(w::VecNum, ::Nothing)
calc_fees(w::VecNum, tn::Turnover)

Compute the turnover fees for portfolio weights.

This is one term of the total fee. Fees states the closed form as $F_{\text{Tn}}$, and treats tn.val as a per-asset fee rate rather than as a bound. No method here uses the fixed flag of Turnover. The flag decides, through factory, which reference weights tn.w holds. When this method runs, tn.w already holds the vector to charge the fee against.

Algorithm

  1. On a nothing tn, return zero(eltype(w)). The method reads w only for its element type.
  2. Otherwise form the traded amount per asset, the absolute difference between w and the reference weights tn.w.
  3. On a Number tn.val, sum the traded amount and scale it by the one rate.
  4. On a VecNum tn.val, contract the rates with the traded amount.

Steps 3 and 4 are not the same expression. They agree to rounding when tn.val is a constant vector, and they differed by 3.47e-18 on w = [0.6, -0.4, 0.0, 0.25], tn.w = [0.1, 0.2, 0.3, 0.4] and a rate of 0.02.

Arguments

  • w: Portfolio weights.

  • tn: Turnover structure.

    • nothing: No turnover fee. The method returns zero.
    • tn.val::Number: One turnover rate for every asset.
    • tn.val::VecNum: One turnover rate per asset.

Returns

  • val::Number: Turnover fee.

Examples

julia> calc_fees([0.8, 0.2], Turnover(; w = [0.0, 0.0], val = 0.02))0.02

Related

source
calc_fees(w::VecNum, T::Number, fees::Fees)

Compute total fees for portfolio weights.

Sums the proportional, fixed, turnover and liquidation fees. calc_asset_fees(w::VecNum, fees::Fees) splits the same total over the assets, and its sum is this number up to the order of summation.

The verb returns a pair, (amortised, one_time). l, s, tn and lq are rates per period, so they charge on every observation and go into amortised. fl, fs and flq charge each non-zero position one time for the whole holding period, as a fraction of capital on the weights this verb takes. So fees.fa decides where they go. A nothing fa puts them in one_time, and an AmortisedFees divides them by T, adds them to amortised and leaves one_time zero.

T is the observation count the calling site charges over, and the site always knows it, so no fee stores one. charge_fees passes the length of the series it applies the pair to, and calc_total_fees contracts the pair to the cost of a whole holding period.

Algorithm

  1. Charge the per period terms with calc_periodic_fees.
  2. Charge the one-off terms with calc_one_off_fees.
  3. On a nothing fees.fa, return the two charges unchanged.
  4. On an AmortisedFees fees.fa, divide the one-off charge by T, add it to the per period charge, and return that number beside a zero of the same type.

Arguments

  • w: Portfolio weights.
  • T: The observation count that the site charges the fee over.
  • fees: Fees structure.

Returns

  • amortised::Number: The charge every observation carries.
  • one_time::Number: The charge the first observation carries alone.

Examples

julia> fees = Fees(; l = [0.01, 0.02], s = [0.01, 0.02], fl = [5.0, 0.0], fs = [0.0, 10.0]);julia> calc_fees([0.1, -0.2], 21, fees)(0.005, 15.0)

Related

source
PortfolioOptimisers.calc_fixed_fees — Function
calc_fixed_fees(w::VecNum, ::Nothing, kwargs::NamedTuple, ::Function)
calc_fixed_fees(w::VecNum, fees::Number, kwargs::NamedTuple, op::Function)
calc_fixed_fees(w::VecNum, fees::VecNum, kwargs::NamedTuple, op::Function)

Compute the fixed portfolio fees for the assets the portfolio holds.

A fixed fee is a charge per position held, whatever its size. Fees states the closed form as $F_{\text{f}}$.

Algorithm

  1. On a nothing fees, return zero(eltype(w)). The method reads neither kwargs nor op.
  2. Otherwise build idx1, the mask of the assets that op selects against a zero of the promoted element type.
  3. Build idx2, the mask of the selected positions that isapprox does not call zero. The method forwards kwargs to isapprox, so its atol sets the boundary. Under the default atol = 1e-8 a weight of 1e-9 pays no fee and a weight of 1e-7 pays one.
  4. On a Number fees, scale the count of the positions that idx2 marks by the one rate.
  5. On a VecNum fees, sum the rates of the positions that idx2 marks.

Arguments

  • w: Portfolio weights.

  • fees: Fee rate.

    • nothing: No fixed fee. The method returns zero.
    • Number: One rate for every asset that op selects.
    • VecNum: One rate per asset.
  • kwargs: Named tuple of keyword arguments that decides how small an asset weight must be for isapprox to call it zero.

  • op: Broadcast comparison that selects the assets, .>= for the long side and .< for the short side. A nothing fees does not read it.

Returns

  • val::Number: Total fixed fee.

Examples

julia> calc_fixed_fees([0.1, 0.2], 0.01, (; atol = 1e-6), .>=)0.02

Related

source
PortfolioOptimisers.calc_asset_fees — Function
calc_asset_fees(w::VecNum, ::Nothing, ::Function)
calc_asset_fees(w::VecNum, fees::Number, op::Function)
calc_asset_fees(w::VecNum, fees::VecNum, op::Function)

Compute the proportional per asset fees for portfolio weights.

This is one term of the total fee. It is the elementwise form of calc_fees(w::VecNum, fees::Number, op::Function), and Fees states the closed form as $\boldsymbol{F}_{\text{p}}$.

Algorithm

  1. On a nothing fees, return a vector with one zero per asset, in the element type of w. The method reads w only for its length and its element type, and it does not read op.
  2. Otherwise allocate fees_w, a vector with one zero per asset, in the promoted element type. An asset the mask of step 3 leaves out keeps its zero.
  3. Build idx, the mask of the assets that op selects against a zero of the promoted element type.
  4. On a Number fees, write the selected weights, scaled by the one rate, into the selected entries of fees_w.
  5. On a VecNum fees, write the selected weights, multiplied elementwise by the selected rates, into the selected entries of fees_w.

Arguments

  • w: Portfolio weights.

  • fees: Fee rate.

    • nothing: No proportional fee. The method returns zero.
    • Number: One rate for every asset that op selects.
    • VecNum: One rate per asset.
  • op: Broadcast comparison that selects the assets, .>= for the long side and .< for the short side. A nothing fees does not read it.

Returns

  • val::VecNum: The proportional fee of each asset on the selected side, and zero elsewhere.

Examples

julia> calc_asset_fees([0.1, 0.2], 0.01, .>=)2-element Vector{Float64}: 0.001 0.002

Related

source
calc_asset_fees(w::VecNum, ::Nothing)
calc_asset_fees(w::VecNum, tn::Turnover)

Compute the per asset turnover fees for portfolio weights.

This is one term of the total fee. It is the elementwise form of calc_fees(w::VecNum, tn::Turnover), and Fees states the closed form as $\boldsymbol{F}_{\text{Tn}}$. No method here uses the fixed flag of Turnover, for the reason the docstring of that scalar form gives.

Algorithm

  1. On a nothing tn, return a vector with one zero per asset, in the element type of w.
  2. Otherwise form the traded amount per asset, the absolute difference between w and the reference weights tn.w.
  3. On a Number tn.val, scale the traded amount by the one rate.
  4. On a VecNum tn.val, multiply the traded amount elementwise by the per-asset rates.

Arguments

  • w: Portfolio weights.

  • tn: Turnover structure.

    • nothing: No turnover fee. The method returns zero.
    • tn.val::Number: One turnover rate for every asset.
    • tn.val::VecNum: One turnover rate per asset.

Returns

  • val::VecNum: Per asset turnover fee.

Examples

julia> calc_asset_fees([0.1, 0.2], Turnover(; w = [0.0, 0.0], val = 0.01))2-element Vector{Float64}: 0.001 0.002

Related

source
calc_asset_fees(w::VecNum, T::Number, fees::Fees)

Compute total per asset fees for portfolio weights.

Sums the proportional, fixed, turnover and liquidation fees. Each half sums to the matching half of the pair calc_fees(w::VecNum, T::Number, fees::Fees) returns, up to the order of summation.

The verb returns a pair, (amortised, one_time), and each half is itself a pair with one entry per axis of a reduced Fees. The first entry is the charge of the assets that stayed, and the second is the charge of the assets that left.

l, s and tn are rates per period and go into amortised, as lq does on the other axis. fl and fs charge each non-zero position one time for the whole holding period, as a fraction of capital on the weights this verb takes. So fees.fa decides where they go, as it does for flq. A nothing fa puts them in one_time, and an AmortisedFees divides them by T, adds them to amortised and leaves one_time zero. The doctest below shows all four vectors.

T is the observation count the calling site charges over, and the site always knows it, so no fee stores one. charge_asset_fees passes the row count of the matrix it applies the pair to, and calc_total_asset_fees contracts the pair to the cost of a whole holding period.

Algorithm

  1. Charge the per period terms with calc_asset_periodic_fees, which covers both axes.
  2. Charge the one-off terms with calc_asset_one_off_fees, which covers both axes.
  3. On a nothing fees.fa, return the two charges unchanged.
  4. On an AmortisedFees fees.fa, divide each one-off charge by T, add it to the per period charge of its own axis, and return those beside zeros of the same type. The liquidation axis sums through add_liquidation_terms. A caller sets lq and flq independently, and an unset one gives an empty vector.

Arguments

  • w: Portfolio weights.
  • T: The observation count that the site charges the fee over.
  • fees: Fees structure.

Returns

  • amortised::Tuple{VecNum, VecNum}: The charge every observation carries, per asset that stayed and per asset that left.
  • one_time::Tuple{VecNum, VecNum}: The charge the first observation carries alone, on the same two axes. It is zero under an AmortisedFees, which spreads it into amortised.

Examples

julia> fees = Fees(; l = [0.01, 0.02], s = [0.01, 0.02], fl = [5.0, 0.0], fs = [0.0, 10.0]);julia> calc_asset_fees([0.1, -0.2], 21, fees)(([0.001, 0.004], Float64[]), ([5.0, 10.0], Float64[]))

Related

source
PortfolioOptimisers.calc_asset_fixed_fees — Function
calc_asset_fixed_fees(w::VecNum, ::Nothing, kwargs::NamedTuple, ::Function)
calc_asset_fixed_fees(w::VecNum, fees::Number, kwargs::NamedTuple, op::Function)
calc_asset_fixed_fees(w::VecNum, fees::VecNum, kwargs::NamedTuple, op::Function)

Compute the per asset fixed portfolio fees for the assets the portfolio holds.

This is the elementwise form of calc_fixed_fees, and its entries sum to the number that function returns. Fees states the closed form as $\boldsymbol{F}_{\text{f}}$.

Algorithm

  1. On a nothing fees, return a vector with one zero per asset, in the element type of w. The method reads neither kwargs nor op.
  2. Otherwise allocate fees_w, a vector with one zero per asset, in the promoted element type. An asset the masks of steps 3 and 4 leave out keeps its zero.
  3. Build idx1, the mask of the assets that op selects against a zero of the promoted element type.
  4. Build idx2, the mask of the selected positions that isapprox does not call zero. The method forwards kwargs to isapprox, so its atol sets the boundary.
  5. On a Number fees, write the one rate times idx2 into the selected entries of fees_w.
  6. On a VecNum fees, write the selected per-asset rates times idx2 into the selected entries of fees_w.

Arguments

  • w: Portfolio weights.

  • fees: Fee rate.

    • nothing: No fixed fee. The method returns zero.
    • Number: One rate for every asset that op selects.
    • VecNum: One rate per asset.
  • kwargs: Named tuple of keyword arguments that decides how small an asset weight must be for isapprox to call it zero.

  • op: Broadcast comparison that selects the assets, .>= for the long side and .< for the short side. A nothing fees does not read it.

Returns

  • val::VecNum: The fixed fee of each asset on the selected side, and zero elsewhere.

Examples

julia> calc_asset_fixed_fees([0.1, 0.2], 0.01, (; atol = 1e-6), .>=)2-element Vector{Float64}: 0.01 0.01

Related

source
PortfolioOptimisers.calc_total_fees — Function
calc_total_fees(w::VecNum, T::Number, fees::Option{<:Fees})

Charge the whole cost of holding a portfolio for T periods.

calc_fees gives the charge of one observation of a return series. This verb gives the charge of the whole holding period, so it charges the per period terms T times and the one-off terms one time. Fees states the closed form as the sum of $c_t$ over the $T$ observations, which is the same on each clock. It needs the rates, the fixed amounts and the horizon, and nothing else. fees.fa affects no term here, because that field places a one-off cost on a return series, and this verb reports no series. The finite allocation states the same rule in money, in its own variables, through set_allocation_fees! and allocation_fee.

Algorithm

  1. Charge T times the per period terms with calc_periodic_fees.
  2. Charge the one-off terms one time with calc_one_off_fees.
  3. Return the sum of the two terms.

Arguments

  • w: Portfolio weights.
  • T: Horizon, in periods.
  • fees: Fees structure, or nothing.

Returns

  • val::Number: The whole cost of the holding period.

Examples

julia> fees = Fees(; l = 0.01, fl = 5.0, tn = Turnover(; w = [0.0, 0.0], val = 0.002));julia> calc_total_fees([0.5, 0.5], 252, fees)13.024000000000001

Related

source
PortfolioOptimisers.calc_total_asset_fees — Function
calc_total_asset_fees(w::VecNum, T::Number, fees::Option{<:Fees})

Split over the assets the whole cost of holding a portfolio for T periods.

This is the per asset counterpart of calc_total_fees. Its entries sum to that number, up to the order of summation.

Algorithm

  1. Charge T times the per period terms with calc_asset_periodic_fees. The step scales both halves of its pair, because both are rates.
  2. Charge the one-off terms one time with calc_asset_one_off_fees. The step scales neither half, because the holding period pays both one time.
  3. Return the pair, each axis summed with its own half. The first entry is the investable total. The second is the liquidation total through add_liquidation_terms, which handles the axis whose two terms a caller sets independently.

Arguments

  • w: Portfolio weights.
  • T: Horizon, in periods.
  • fees: Fees structure, or nothing.

Returns

The verb returns a pair, one entry per axis of a reduced Fees.

  • investable::VecNum: The whole cost of the holding period for each asset that stayed, from l, s, tn, fl and fs, which investable_fees_view sliced to the Investable Mask.
  • liquidation::VecNum: The whole cost of the forced exit of each asset that left, from lq and flq, which investable_fees_view sliced to the mask's complement. It is empty when neither liquidation charge is set.

Examples

julia> fees = Fees(; l = 0.01, fl = 5.0, tn = Turnover(; w = [0.0, 0.0], val = 0.002));julia> calc_total_asset_fees([0.5, 0.5], 252, fees)([6.5120000000000005, 6.5120000000000005], Float64[])

Related

source
PortfolioOptimisers.port_opt_view — Method
port_opt_view(fees::Fees, i, X::MatNum, args...)
port_opt_view(fees::FeesEstimator, i, X::MatNum, args...)
port_opt_view(fees::FeesE_Fees, i, args...)

Sub-select a fee to the assets an optimisation keeps, on both of its axes.

A Fees spans two axes once an optimisation has reduced to its Investable Mask. tn, l, s, fl and fs price the positions the portfolio holds, so they are on the investable axis and the verb slices them at i. lq and flq price the positions the portfolio must sell, so they are on the complement of that axis and the verb slices them at the assets i leaves out.

For this reason neither lq nor flq has a @vprop tag, and this file writes the verb by hand rather than generating it. The generated method passes one index to every tagged field, and here the two groups need different indices. The verb derives the complement from i and the width of X and never stores it.

X is the unreduced returns matrix, which investable_reduction and investable_view already pass, so size(X, 2) is the width of the full universe. On a resolved Fees, two_axis_fees_view does the split. It takes that width as a number, so investable_fees_view can call it without a matrix. The FeesEstimator method does the same split in its own body.

The three-argument method serves a caller that passes no matrix. It cannot derive a complement, so it slices the five per-asset fields and returns lq and flq untouched. That is correct, because lq and flq are already on their own axis.

Neither method writes imsk, so both return an unmarked fee. two_axis_fees_view states the reason.

Algorithm

  1. On a Fees, pass i and size(X, 2) to two_axis_fees_view, which does the split.
  2. On a FeesEstimator, build j, the complement of i in 1:size(X, 2), and do the same split in this method. Slice tn at i through port_opt_view, whose @vprop tags slice w and a vector val and leave a scalar or dictionary val alone. Slice l, s, fl and fs at i through nothing_scalar_array_view. Slice lq and flq at j, or set them to nothing on an empty j, because then no asset left and there is nothing to liquidate.
  3. Rebuild through the keyword constructor, and keep the defaults, fa and kwargs.

Arguments

  • fees: The fee to reduce.
  • i: Indices of the assets the optimisation keeps.
  • X: The unreduced returns matrix. The verb uses only its width.

Returns

  • fees::Fees or fees::FeesEstimator: The fee on the two reduced axes, unmarked, of the same type family as the input. The fields can be views, so the concrete type can differ from typeof(fees).

Related

source
PortfolioOptimisers.needs_previous_weights — Method
needs_previous_weights(fe::FeesE_Fees) -> Bool

Check whether a fee constraint or estimator needs the previous portfolio weights, through its three turnover fields fe.tn, fe.lq and fe.flq.

Only these three fields use a previous weight vector. tn charges the trade from the previous weights, and lq and flq charge the forced sale of the previous positions of the assets that left. The proportional and fixed terms key on the sign of the position that w already carries, so a Fees whose three turnover fields are nothing needs none.

Algorithm

  1. Forward fe.tn, fe.lq and fe.flq to needs_previous_weights, which returns !tn.fixed on a turnover object and false on nothing.
  2. Return true when one of the three answers is true.

Arguments

  • fe: Fee constraint or estimator.

Returns

  • Bool: true if the fee needs the previous weights, false otherwise.

Related

source
PortfolioOptimisers.investable_fees_view — Function
investable_fees_view(fees::Nothing, imsk, X)
investable_fees_view(fees::Fees, imsk::Nothing, X)
investable_fees_view(fees::Fees, imsk::BitVector, X::MatNum)
investable_fees_view(fees::Fees, imsk::BitVector, n::Integer)

Place a fee resolved over the caller's universe onto the axes an Investable Mask defines, and mark it with that mask.

fees_constraints resolves a fee against the sets the caller stated before the fee reaches this verb, and this verb then reduces the resolved fee. That order lets a name-keyed fee name an asset the data later delists. After the reduction, sets holds only the investable names, so a departed name is missing and strict refuses it. A liquidation charge keyed by name cannot resolve at all, because its w is already on the complement while sets is on the mask. The resolution before the view avoids both problems. The order needs no new arithmetic, because port_opt_view already splits a resolved Fees across its two axes.

The verb has one arm for each kind of mask. A BitVector means assets left, so the view runs at findall(imsk) and derives the complement from the width of the unreduced universe. A fit site gives that width through X, and a consumer that holds no full-width matrix states it as n. Such a consumer is a result-taking verb, which meets a caller's fee beside a result whose own prior is already reduced, and the only width it has is length(imsk). A nothing mask means every asset is investable, so there is no complement to slice to. strip_liquidation_charges drops lq and flq instead, which keeps that path allocation-free.

This is the only verb that writes fees.imsk. It checks the mark before it acts, so it is idempotent. A caller states the two liquidation charges over the full universe, and a result holds them on the complement of its mask. Their widths cannot tell the two apart, and this one verb serves both.

expected_risk(r, w, pr, fees) on an all-investable prior meets the caller's fee under a nothing mask. It must drop lq and flq, or it charges every position as a forced exit on every period. expected_risk(r, res) on a reduced result meets the result's fee under the same nothing mask, because the reduced prior holds no NaN. It must charge the exit that lq and flq hold.

The mark tells the two cases apart. An unmarked fee is a caller's statement and takes the arm the mask names. This verb already reduced a fee marked with this mask, so the verb returns it as it is. The verb refuses a fee marked with another mask, because its lq and flq hold no rate for an asset that the other reduction kept.

Algorithm

  1. On a nothing fee, return nothing.
  2. On a marked fee, return it untouched when the mark is imsk, or when the mark is a BitVector and imsk is nothing. Raise an ArgumentError that names both masks when the mark is a BitVector other than imsk.
  3. On an unmarked fee and a nothing mask, pass the fee to strip_liquidation_charges. That verb drops lq and flq, and it returns unchanged a fee that holds neither liquidation charge.
  4. On an unmarked fee and a BitVector mask, take two_axis_fees_view at findall(imsk) over the width of the unreduced universe, size(X, 2) or n. That view slices the five per-asset fields to the mask and lq and flq to its complement. Rebuild the result with imsk as its mark.

Arguments

  • fees: The fee resolved over the caller's full universe, a fee this verb already reduced, or nothing.
  • imsk: The Investable Mask, or nothing when every asset is investable.
  • X: The unreduced returns matrix. The verb uses only its width, to derive the complement.
  • n: The width of the unreduced universe, for a caller that holds no matrix of it.

Validation

  • The verb refuses a fee marked with a BitVector other than imsk, with an ArgumentError that names the two masks. An earlier call of this verb sliced its lq and flq to the complement of its own mask, so they hold no rate for an asset that left under the caller's mask. A charge of zero for that asset overstates the net return. Lift the fee with lift_fees and reduce the caller's statement instead.

Returns

  • Option{<:Fees}: The fee on the axes the mask leaves, marked with the mask.

Related

source

References

[186]
G. A. Pogue. An Extension of the Markowitz Portfolio Selection Model to Include Variable Transactions' Costs, Short Sales, Leverage Policies and Taxes. Working paper 388-69B (Sloan School of Management, Massachusetts Institute of Technology, 1969).