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} <: AbstractEstimatorNames 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.
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-lis 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 fieldlqis 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 besidel,sandtn.
flq: Fixed liquidation fees, which the fee charges when a position leaves the Investable Mask. The fieldflqis on the complement of the mask, aslqis. The fee charges the amount once for each entry whose absolute previous weight is notisapproxto zero underkwargs, and it falls on the clockfanames besideflandfs.
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 termsfl,fsandflqfall on.nothingand aFirstObservationFeescharge them one time, on the first observation of a return series. AnAmortisedFeesspreads them evenly over the observation count that the calling site passes in. It affects no other term, becausel,s,tnandlqare rates per period.
kwargs: Named tuple of keyword arguments that the fixed fee terms forward toisapprox, 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)) -> FeesEstimatorKeywords correspond to the struct's fields.
Validation
- The constructor checks
l,fl,fs,dl,dflanddfswithassert_nonempty_nonneg_finite_val. - The constructor checks
sanddswithassert_nonempty_finite_val. A negative short rate is a credit. The bounds >= -lreads both rates on one asset axis, soFeeschecks it after the resolution, throughassert_short_fee_convex.
Propagated parameters
factory passes its arguments on to these @fprop fields:
tn: Recursively updated throughfactory.lq: Recursively updated throughfactory.flq: Recursively updated throughfactory.
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 throughport_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
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} <: AbstractResultCharges 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.
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 × 1per 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 × 1per 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
lqandflq. 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
wfield oflqand offlq. 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 × 1turnover vector as defined inTurnover. Thewfield 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
Turnoverholds the turnover fee, and itsvalis the per asset fee rate. - $1\left\{\cdot\right\}$: Elementwise (Hadamard) indicator function. It is
1when the condition is true and0otherwise. 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...), sokwargsdecides 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 = lcharges $l \lvert w \rvert$. This is a carry or a borrow cost on the position, whatever its sign.s = -lcharges 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-lis 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 fieldlqis 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 besidel,sandtn.
flq: Fixed liquidation fees, which the fee charges when a position leaves the Investable Mask. The fieldflqis on the complement of the mask, aslqis. The fee charges the amount once for each entry whose absolute previous weight is notisapproxto zero underkwargs, and it falls on the clockfanames besideflandfs.
fa: Fee amortisation algorithm, and the clock the fixed fee termsfl,fsandflqfall on.nothingand aFirstObservationFeescharge them one time, on the first observation of a return series. AnAmortisedFeesspreads them evenly over the observation count that the calling site passes in. It affects no other term, becausel,s,tnandlqare rates per period.
kwargs: Named tuple of keyword arguments that the fixed fee terms forward toisapprox, to decide how near zero a weight must be to count as zero.
imsk: The Investable Mask thatinvestable_fees_viewreduced this fee on, ornothingwhen the fee is a caller's statement over the full universe. Onlyinvestable_fees_viewwrites it, and a caller never does. When it is set,tn,l,s,flandfsare onfindall(imsk), andlqandflqare on its complement.investable_fees_viewreturns a marked fee unchanged when it meets it a second time. It drops or slices the two liquidation charges,lqandflq, 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) -> FeesKeywords correspond to the struct's fields.
Validation
- The constructor checks
l,flandfswithassert_nonempty_nonneg_finite_val. - The constructor checks
swithassert_nonempty_finite_val, and checkss >= -lentry by entry withassert_short_fee_convex. Anothinglreads as zero there. - The
valoftn,lqandflqis finite.Turnoveraccepts a+Infrate, which is an uncapped bound, but a fee rate of+Infgives the model an infinite coefficient.
Propagated parameters
factory passes its arguments on to these @fprop fields:
tn: Recursively updated throughfactory.lq: Recursively updated throughfactory.flq: Recursively updated throughfactory.
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 throughport_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 tonothing. The view only slices, andinvestable_fees_viewwrites 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
add_to_fees!set_non_fixed_fees!set_long_non_fixed_fees!set_short_non_fixed_fees!set_proportional_fees!set_turnover_fees!FeesEstimatorOptionTurnoverNum_VecNumAbstractResultAbstractFeeAmortisationAmortisedFeescharge_feesassert_nonempty_nonneg_finite_valassert_nonempty_finite_valassert_short_fee_convexfees_constraintscalc_feescalc_asset_feescalc_net_returnsset_non_fixed_fees!PartsBoundWeightsJuMPOptimiserMeanRiskVariancefactoryport_opt_viewinvestable_fees_viewlift_feesshow_fields
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.
PortfolioOptimisers.AmortisedFees — Type
struct AmortisedFees <: AbstractFeeAmortisationSpreads 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() -> AmortisedFeesExamples
julia> AmortisedFees()AmortisedFees()Related
PortfolioOptimisers.FirstObservationFees — Type
struct FirstObservationFees <: AbstractFeeAmortisationCharges 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() -> FirstObservationFeesExamples
julia> FirstObservationFees()FirstObservationFees()Related
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
- Resolve
fees.tnagainst the universe ofsetswithturnover_constraints. The result is aTurnoverwhosevalis one turnover fee rate per asset. Anothingtnstaysnothing. - Resolve
fees.lwithestimator_to_valintol, one long proportional rate per asset in the order of the universe. Every asset the keys miss takesfees.dl, orzero(datatype)whenfees.dlisnothing. Anothingfees.lstaysnothing. - Resolve
fees.sthe same way againstfees.dsintos. - Resolve
fees.flthe same way againstfees.dflintofl. - Resolve
fees.fsthe same way againstfees.dfsintofs. - Resolve
fees.lqandfees.flqthe same way as step 1, over the full universe ofsets.investable_fees_viewmoves them to the complement of the Investable Mask later. - Build a
Feesfrom the seven resolved fields,fees.faunchanged, andfees.kwargsunchanged.fees.kwargssets how near zero a weight must be for the fixed terms to treat it as zero.
Arguments
fees: TheFeesEstimatorthat states the turnover, proportional and fixed fee values.sets: TheUniverseSetsthat holds the asset names or indices.datatype: Output data type for fee values.strict: Iftrue, a mismatch between assets and fee values throws an error. Iffalse, it warns.
Validation
- A key that names neither an asset nor a group of
setsraises anArgumentErrorwhenstrictistrue, and warns otherwise. Steps 1 to 6 each check their own field, so one bad key inlraises whatevers,flandfshold.
Returns
fe::Fees: The turnover, proportional and fixed fee values, aligned withsets.
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
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
- Return
fees. AFeesalready 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 existingFeesobject ornothing.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
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
- On a
nothingfees, returnzero(eltype(w)). The method reads neitherwnorop. - Otherwise build
idx, the mask of the assets thatopselects against a zero of the promoted element type. - On a
Numberfees, scale the selected weights by the one rate, and sum them. - On a
VecNumfees, 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 thatopselects.VecNum: One rate per asset.
op: Broadcast comparison that selects the assets,.>=for the long side and.<for the short side. Anothingfeesdoes not read it.
Returns
val::Number: The proportional fee of the selected side.
Examples
julia> calc_fees([0.1, 0.2], 0.01, .>=)0.003Related
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
- On a
nothingtn, returnzero(eltype(w)). The method readswonly for its element type. - Otherwise form the traded amount per asset, the absolute difference between
wand the reference weightstn.w. - On a
Numbertn.val, sum the traded amount and scale it by the one rate. - On a
VecNumtn.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.02Related
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
- Charge the per period terms with
calc_periodic_fees. - Charge the one-off terms with
calc_one_off_fees. - On a
nothingfees.fa, return the two charges unchanged. - On an
AmortisedFeesfees.fa, divide the one-off charge byT, 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:Feesstructure.
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
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
- On a
nothingfees, returnzero(eltype(w)). The method reads neitherkwargsnorop. - Otherwise build
idx1, the mask of the assets thatopselects against a zero of the promoted element type. - Build
idx2, the mask of the selected positions thatisapproxdoes not call zero. The method forwardskwargstoisapprox, so itsatolsets the boundary. Under the defaultatol = 1e-8a weight of1e-9pays no fee and a weight of1e-7pays one. - On a
Numberfees, scale the count of the positions thatidx2marks by the one rate. - On a
VecNumfees, sum the rates of the positions thatidx2marks.
Arguments
w: Portfolio weights.fees: Fee rate.nothing: No fixed fee. The method returns zero.Number: One rate for every asset thatopselects.VecNum: One rate per asset.
kwargs: Named tuple of keyword arguments that decides how small an asset weight must be forisapproxto call it zero.op: Broadcast comparison that selects the assets,.>=for the long side and.<for the short side. Anothingfeesdoes not read it.
Returns
val::Number: Total fixed fee.
Examples
julia> calc_fixed_fees([0.1, 0.2], 0.01, (; atol = 1e-6), .>=)0.02Related
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
- On a
nothingfees, return a vector with one zero per asset, in the element type ofw. The method readswonly for its length and its element type, and it does not readop. - 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. - Build
idx, the mask of the assets thatopselects against a zero of the promoted element type. - On a
Numberfees, write the selected weights, scaled by the one rate, into the selected entries offees_w. - On a
VecNumfees, write the selected weights, multiplied elementwise by the selected rates, into the selected entries offees_w.
Arguments
w: Portfolio weights.fees: Fee rate.nothing: No proportional fee. The method returns zero.Number: One rate for every asset thatopselects.VecNum: One rate per asset.
op: Broadcast comparison that selects the assets,.>=for the long side and.<for the short side. Anothingfeesdoes 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.002Related
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
- On a
nothingtn, return a vector with one zero per asset, in the element type ofw. - Otherwise form the traded amount per asset, the absolute difference between
wand the reference weightstn.w. - On a
Numbertn.val, scale the traded amount by the one rate. - On a
VecNumtn.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.002Related
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
- Charge the per period terms with
calc_asset_periodic_fees, which covers both axes. - Charge the one-off terms with
calc_asset_one_off_fees, which covers both axes. - On a
nothingfees.fa, return the two charges unchanged. - On an
AmortisedFeesfees.fa, divide each one-off charge byT, add it to the per period charge of its own axis, and return those beside zeros of the same type. The liquidation axis sums throughadd_liquidation_terms. A caller setslqandflqindependently, and an unset one gives an empty vector.
Arguments
w: Portfolio weights.T: The observation count that the site charges the fee over.fees:Feesstructure.
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 anAmortisedFees, which spreads it intoamortised.
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
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
- On a
nothingfees, return a vector with one zero per asset, in the element type ofw. The method reads neitherkwargsnorop. - 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. - Build
idx1, the mask of the assets thatopselects against a zero of the promoted element type. - Build
idx2, the mask of the selected positions thatisapproxdoes not call zero. The method forwardskwargstoisapprox, so itsatolsets the boundary. - On a
Numberfees, write the one rate timesidx2into the selected entries offees_w. - On a
VecNumfees, write the selected per-asset rates timesidx2into the selected entries offees_w.
Arguments
w: Portfolio weights.fees: Fee rate.nothing: No fixed fee. The method returns zero.Number: One rate for every asset thatopselects.VecNum: One rate per asset.
kwargs: Named tuple of keyword arguments that decides how small an asset weight must be forisapproxto call it zero.op: Broadcast comparison that selects the assets,.>=for the long side and.<for the short side. Anothingfeesdoes 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.01Related
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
- Charge
Ttimes the per period terms withcalc_periodic_fees. - Charge the one-off terms one time with
calc_one_off_fees. - Return the sum of the two terms.
Arguments
w: Portfolio weights.T: Horizon, in periods.fees:Feesstructure, ornothing.
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.024000000000001Related
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
- Charge
Ttimes the per period terms withcalc_asset_periodic_fees. The step scales both halves of its pair, because both are rates. - 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. - 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:Feesstructure, ornothing.
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, froml,s,tn,flandfs, whichinvestable_fees_viewsliced to the Investable Mask.liquidation::VecNum: The whole cost of the forced exit of each asset that left, fromlqandflq, whichinvestable_fees_viewsliced 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
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
- On a
Fees, passiandsize(X, 2)totwo_axis_fees_view, which does the split. - On a
FeesEstimator, buildj, the complement ofiin1:size(X, 2), and do the same split in this method. Slicetnatithroughport_opt_view, whose@vproptags slicewand a vectorvaland leave a scalar or dictionaryvalalone. Slicel,s,flandfsatithroughnothing_scalar_array_view. Slicelqandflqatj, or set them tonothingon an emptyj, because then no asset left and there is nothing to liquidate. - Rebuild through the keyword constructor, and keep the defaults,
faandkwargs.
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::Feesorfees::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 fromtypeof(fees).
Related
PortfolioOptimisers.needs_previous_weights — Method
needs_previous_weights(fe::FeesE_Fees) -> BoolCheck 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
- Forward
fe.tn,fe.lqandfe.flqtoneeds_previous_weights, which returns!tn.fixedon a turnover object andfalseonnothing. - Return
truewhen one of the three answers istrue.
Arguments
fe: Fee constraint or estimator.
Returns
Bool:trueif the fee needs the previous weights,falseotherwise.
Related
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
- On a
nothingfee, returnnothing. - On a marked fee, return it untouched when the mark is
imsk, or when the mark is aBitVectorandimskisnothing. Raise anArgumentErrorthat names both masks when the mark is aBitVectorother thanimsk. - On an unmarked fee and a
nothingmask, pass the fee tostrip_liquidation_charges. That verb dropslqandflq, and it returns unchanged a fee that holds neither liquidation charge. - On an unmarked fee and a
BitVectormask, taketwo_axis_fees_viewatfindall(imsk)over the width of the unreduced universe,size(X, 2)orn. That view slices the five per-asset fields to the mask andlqandflqto its complement. Rebuild the result withimskas its mark.
Arguments
fees: The fee resolved over the caller's full universe, a fee this verb already reduced, ornothing.imsk: The Investable Mask, ornothingwhen 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
BitVectorother thanimsk, with anArgumentErrorthat names the two masks. An earlier call of this verb sliced itslqandflqto 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 withlift_feesand reduce the caller's statement instead.
Returns
Option{<:Fees}: The fee on the axes the mask leaves, marked with the mask.
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).