~/bend-docscommunity

portfolio.bend checks

raw source on the hub · import bend-quant-portfolio@0.1.0.0/portfolio.bend as Portfolio

portfolio: factor risk decomposition, portfolio construction (analytical MVO, Woodbury-ADMM constrained/turnover-aware MVO), hedging, dynamic risk sizing, and ex-post performance attribution over float32 vectors and matrices.

Built on bend-quant-linalg@0.1.0.0 following Paleologo's "The Elements of Quantitative Investing" (Ch 4.5, 9-14). Every non-public def is internal; LAWS.bend and PROOF.bend name internals and they can change in any version.

4 imports
import Base
import bend-kit-f32array@0.2.1.0/f32array.bend as A
import bend-quant-linalg@0.1.0.0/linalg.bend as L
import ./admm.bend as ADMM

Types

type FactorModel source · line 18 · raw

Data

Strict factor risk model: Omega_r = B Omega_f B^T + diag(idio_var). b: N x K loadings, cov_f: K x K SPD factor covariance, idio_var: N positive variances.

type RiskDecomp source · line 22 · raw

Data

Portfolio risk decomposition under a factor model (Paleologo Ch 4.5.2).

type AlphaDecomp source · line 35 · raw

Data

Alpha decomposition alpha = B * factor_alpha + orth under Omega_eps^{-1} (Ch 4.3, 9.4).

type Constraints source · line 774 · raw

Data

Linear + box + optional GMV constraints (Ch 10.2.1). Set l_c_j == u_c_j for equality constraints; gmv_max <= 0.0 disables the L1 cap.

type TradeCosts source · line 785 · raw

Data

Transaction / market impact cost vectors (non-negative, length N).

type OptConfig source · line 792 · raw

Data

ADMM solver configuration. rho_admm <= 0.0 selects automatic Hessian scaling.

type OptResult source · line 802 · raw

Data

Constrained MVO solution and KKT residual diagnostics.

type Two source · line 928 · raw

@-A:Data -> Data

type HedgeShrink source · line 982 · raw

Data

Result of beta-error shrinkage hedging (Paleologo Ch 12.3 Eq 12.6-12.7).

type DrawdownState source · line 1213 · raw

Data

type GzState source · line 1273 · raw

Data

type PeriodAttrib source · line 1310 · raw

Data

Single-period holdings-based attribution and uncertainty (Ch 14.1, 14.2).

type MaxAttrib source · line 1322 · raw

Data

Maximal attribution to a factor subset S via Schur complement rotation (Ch 14.3).

type NestedGroup source · line 1332 · raw

Data

One group's result in nested maximal attribution (Ch 14.3 Procedure 14.2).

type SelectionSizing source · line 1339 · raw

Data

Selection vs. sizing IR decomposition (Ch 14.4).

type NestedCur source · line 1696 · raw

Data

type SsAcc source · line 1748 · raw

Data

type RowPair source · line 1818 · raw

Data

type AttribItem source · line 1861 · raw

Data

type AttribDims source · line 1864 · raw

Data

Definitions

def fmax source · line 44 · raw

@+a:F32 -> @+b:F32 -> F32

def fmin source · line 47 · raw

@+a:F32 -> @+b:F32 -> F32

def all_pos source · line 50 · raw

@xs:List<&2, F32> -> Bool

def all_nonneg source · line 57 · raw

@xs:List<&2, F32> -> Bool

def inv_f32 source · line 71 · raw

@x:F32 -> F32

def sqrt_f32 source · line 74 · raw

@x:F32 -> F32

def inv_sqrt_f32 source · line 77 · raw

@x:F32 -> F32

def list_add_scalar source · line 80 · raw

@xs:List<&2, F32> -> @+s:F32 -> List<&2, F32>

def vec_inv source · line 87 · raw

@v:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec

def vec_sqrt source · line 91 · raw

@v:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec

def vec_inv_sqrt source · line 95 · raw

@v:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec

def vec_add_scalar source · line 99 · raw

@v:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+s:F32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec

def list_quad_diag.go source · line 103 · raw

@ws:List<&2, F32> -> @ds:List<&2, F32> -> @acc:F32 -> F32

def vec_quad_diag.pick source · line 112 · raw

@ok:Bool -> @ws:List<&2, F32> -> @ds:List<&2, F32> -> Maybe<&2, F32>

def vec_quad_diag source · line 119 · raw

@w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @d:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

def fm_dims_ok source · line 126 · raw

@b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Bool

def FactorModel.new.pos source · line 132 · raw

@ok:Bool -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, FactorModel>

def FactorModel.new.dims source · line 139 · raw

@ok:Bool -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, FactorModel>

def FactorModel.new source · line 148 · raw

@+b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, FactorModel>

Validates dimensions (b is N x K, cov_f is K x K, idio_var is N) and idio_var > 0.

def exposures source · line 154 · raw

@fm:FactorModel -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

Factor exposures b = B^T w (length K).

def cov_matvec.step3 source · line 158 · raw

@fpart:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @ipart:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def cov_matvec.step2 source · line 165 · raw

@mbz:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def cov_matvec.step1 source · line 172 · raw

@mexp:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def cov_matvec source · line 180 · raw

@fm:FactorModel -> @+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

Implicit matrix-vector product Omega_r w = B (Omega_f (B^T w)) + diag(idio_var) w in O(N K + K^2).

def risk_decomp.build source · line 184 · raw

@mfvar:Maybe<&2, F32> -> @mivar:Maybe<&2, F32> -> @exp:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, RiskDecomp>

def risk_decomp.go source · line 196 · raw

@mexp:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, RiskDecomp>

def risk_decomp source · line 204 · raw

@fm:FactorModel -> @+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, RiskDecomp>

Total, factor, and idiosyncratic variance/volatility and exposures.

def active_risk.go source · line 208 · raw

@mwa:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @fm:FactorModel -> Maybe<&2, RiskDecomp>

def active_risk source · line 216 · raw

@fm:FactorModel -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @w_bench:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, RiskDecomp>

Tracking error and risk decomposition of active holdings w - w_bench (Ch 10.2.1).

def marginal_risk.pos source · line 219 · raw

@ok:Bool -> @tvar:F32 -> @om_w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def marginal_risk.scale source · line 226 · raw

@mtvar:Maybe<&2, F32> -> @om_w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def marginal_risk.go source · line 233 · raw

@momw:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def marginal_risk source · line 242 · raw

@fm:FactorModel -> @+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

Marginal contribution to portfolio volatility: (Omega_r w) / sigma(w). Returns None on shape mismatch or zero portfolio variance.

def risk_contrib.pos source · line 245 · raw

@ok:Bool -> @tvar:F32 -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @om_w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def risk_contrib.scale source · line 253 · raw

@mtvar:Maybe<&2, F32> -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @om_w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def risk_contrib.go source · line 260 · raw

@momw:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def risk_contrib source · line 268 · raw

@fm:FactorModel -> @+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

Euler risk contributions w_i * (Omega_r w)_i / sigma(w); sums to sigma(w).

def herfindahl.pick source · line 271 · raw

@ok:Bool -> @n1:F32 -> @n2:F32 -> F32

def herfindahl source · line 280 · raw

@+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> F32

Portfolio concentration H(w) = ||w||_2^2 / ||w||_1^2 in [1/N, 1] (Ch 12 note 5).

def eff_breadth.go source · line 285 · raw

@mwtilde:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> Maybe<&2, F32>

def eff_breadth.pos source · line 294 · raw

@ok:Bool -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

def eff_breadth.pick source · line 301 · raw

@ok:Bool -> @ds:List<&2, F32> -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

def eff_breadth source · line 309 · raw

@+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

Volatility-weighted effective breadth ||sigma .* w||_1 / ||sigma .* w||_2 (Ch 14.4).

def mvo_unconstrained.scale source · line 316 · raw

@mz:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @+risk_aversion:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_unconstrained.pos source · line 323 · raw

@ok:Bool -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+risk_aversion:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_unconstrained source · line 331 · raw

@fm:FactorModel -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+risk_aversion:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

Unconstrained MVO: max alpha^T w - (rho / 2) w^T Omega_r w => (1 / rho) Omega_r^{-1} alpha (Ch 9.2).

def mvo_target_vol.pos source · line 335 · raw

@ok:Bool -> @q:F32 -> @z:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_target_vol.scale source · line 342 · raw

@mq:Maybe<&2, F32> -> @z:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_target_vol.zero source · line 349 · raw

@is_zero:Bool -> @+z:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_target_vol.go source · line 357 · raw

@mz:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_target_vol.ge source · line 364 · raw

@ok:Bool -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_target_vol source · line 372 · raw

@fm:FactorModel -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

Volatility-targeted MVO: w* = (sigma / sqrt(alpha^T Omega_r^{-1} alpha)) Omega_r^{-1} alpha (Eq 9.3).

def mvo_robust.ok source · line 376 · raw

@ok:Bool -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+risk_aversion:F32 -> @+tau_sq:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_robust source · line 385 · raw

@fm:FactorModel -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+risk_aversion:F32 -> @+tau_sq:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

Robust diagonal-regularized MVO: (1 / rho) (Omega_r + tau_sq * I)^{-1} alpha (Ch 10.2.3). Unifies uncertain alpha, robust missing factor, and Frobenius-robust covariance.

def mvo_trade_quad.solve source · line 389 · raw

@md_eff:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @mrhs:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+risk_aversion:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_trade_quad.rhs source · line 396 · raw

@mcw:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @cost_quad:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+risk_aversion:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_trade_quad.nonneg source · line 403 · raw

@ok:Bool -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @w0:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+cost_quad:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+risk_aversion:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_trade_quad.pick source · line 410 · raw

@ok:Bool -> @cs:List<&2, F32> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @w0:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @cost_quad:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+risk_aversion:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_trade_quad source · line 419 · raw

@fm:FactorModel -> @+alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+w0:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+cost_quad:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+risk_aversion:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

Single-period quadratic impact closed form (Ch 11 Eq 11.7): max alpha^T w - (rho / 2) w^T Omega_r w - (1/2) (w - w0)^T diag(cost_quad) (w - w0).

def fmp.finish source · line 429 · raw

@mx:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @d_isq:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat>

def fmp.from_qr source · line 436 · raw

@mqr:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.QR> -> @d_isq:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat>

def fmp.weighted source · line 443 · raw

@ma:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @d_isq:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat>

def fmp.pos source · line 450 · raw

@ok:Bool -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat>

def fmp.k0 source · line 458 · raw

@is_k0:Bool -> @+bm:U32 -> @ds:List<&2, F32> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat>

def fmp.pick source · line 465 · raw

@ok:Bool -> @bm:U32 -> @bn:U32 -> @ds:List<&2, F32> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat>

def fmp source · line 474 · raw

@fm:FactorModel -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat>

Factor-Mimicking Portfolios P = Omega_eps^{-1} B (B^T Omega_eps^{-1} B)^{-1} (N x K, Eq 9.12). Computed via thin QR of D^{-1/2} B to avoid squaring the condition number.

def alpha_decomp.wrap source · line 480 · raw

@msp:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @morth:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @lam:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, AlphaDecomp>

def alpha_decomp.build source · line 487 · raw

@mlam:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @morth_w:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @d_sq:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, AlphaDecomp>

def alpha_decomp.solve_r source · line 494 · raw

@mqta:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @morth_w:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @r:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @d_sq:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, AlphaDecomp>

def alpha_decomp.from_qr source · line 501 · raw

@mqr:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.QR> -> @mta:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @d_sq:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, AlphaDecomp>

def alpha_decomp.weighted source · line 508 · raw

@ma:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @d_isq:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @d_sq:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, AlphaDecomp>

def alpha_decomp.pos source · line 515 · raw

@ok:Bool -> @+b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, AlphaDecomp>

def alpha_decomp.k0 source · line 524 · raw

@is_k0:Bool -> @bm:U32 -> @ds:List<&2, F32> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, AlphaDecomp>

def alpha_decomp.pick source · line 531 · raw

@ok:Bool -> @bm:U32 -> @bn:U32 -> @ds:List<&2, F32> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, AlphaDecomp>

def alpha_decompose source · line 539 · raw

@fm:FactorModel -> @+alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, AlphaDecomp>

Decomposes alpha = B * factor_alpha + orth under Omega_eps^{-1} metric (Ch 4.3, 9.4).

def mvo_fn.nrm source · line 546 · raw

@ok:Bool -> @nrm:F32 -> @ow:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @d_isq:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_fn.zero source · line 553 · raw

@is_zero:Bool -> @+ow:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @d_isq:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_fn.scale source · line 562 · raw

@mow:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @d_isq:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_fn.from_qr source · line 569 · raw

@mqr:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.QR> -> @mta:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @d_isq:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_fn.weighted source · line 576 · raw

@ma:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @+d_isq:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_fn.k0 source · line 583 · raw

@is_k0:Bool -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+d_isq:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_fn.pos source · line 590 · raw

@ok:Bool -> @+b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_fn.pick source · line 599 · raw

@ok:Bool -> @ds:List<&2, F32> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_factor_neutral source · line 608 · raw

@fm:FactorModel -> @+alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+target_vol:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

Pure-alpha / factor-neutral MVO (Ch 9.4 Eq 9.18): max alpha^T w s.t. B^T w = 0, w^T Omega_eps w <= target_vol^2.

def mvo_fspace.apply source · line 615 · raw

@mp:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @mu:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @+risk_aversion:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_fspace.chol source · line 622 · raw

@ml:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @fm:FactorModel -> @lambda_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+risk_aversion:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_fspace.pos source · line 629 · raw

@ok:Bool -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @fm:FactorModel -> @lambda_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+risk_aversion:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_fspace.pick source · line 636 · raw

@ok:Bool -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @fm:FactorModel -> @lambda_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+risk_aversion:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def mvo_factor_space source · line 644 · raw

@+fm:FactorModel -> @+lambda_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+risk_aversion:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

Factor-space MVO: u* = (1 / rho) Omega_f^{-1} lambda_f, w* = P u* (Ch 9.3.1).

def sre.pos source · line 653 · raw

@ok:Bool -> @ret:F32 -> @rvar:F32 -> @opt2:F32 -> Maybe<&2, F32>

def sre.finish source · line 660 · raw

@mret:Maybe<&2, F32> -> @mrvar:Maybe<&2, F32> -> @mopt2:Maybe<&2, F32> -> Maybe<&2, F32>

def sre.with_omw source · line 667 · raw

@momw:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @z_true:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+alpha_true:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

def sre.go source · line 674 · raw

@mw:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @mz:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @fm_true:FactorModel -> @alpha_true:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

def sre source · line 682 · raw

@fm_est:FactorModel -> @alpha_est:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+fm_true:FactorModel -> @+alpha_true:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

Sharpe Ratio Efficiency SR(alpha_est, fm_est) / SR(alpha_true, fm_true) (Ch 10.3).

def tc_lin.go source · line 689 · raw

@ws:List<&2, F32> -> @w0s:List<&2, F32> -> @cs:List<&2, F32> -> @acc:F32 -> F32

def tc_lin.pick source · line 696 · raw

@ok:Bool -> @ws:List<&2, F32> -> @w0s:List<&2, F32> -> @cs:List<&2, F32> -> Maybe<&2, F32>

def trade_cost_linear source · line 704 · raw

@w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @w0:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @c1:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

Linear spread / commission cost: sum_i c1_i |w_i - w0_i|.

def tc_quad.go source · line 710 · raw

@ws:List<&2, F32> -> @w0s:List<&2, F32> -> @cs:List<&2, F32> -> @acc:F32 -> F32

def tc_quad.pick source · line 718 · raw

@ok:Bool -> @ws:List<&2, F32> -> @w0s:List<&2, F32> -> @cs:List<&2, F32> -> Maybe<&2, F32>

def trade_cost_quad source · line 726 · raw

@w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @w0:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @c2:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

Quadratic temporary market impact cost: (1/2) sum_i c2_i (w_i - w0_i)^2.

def pow_pos source · line 732 · raw

@+x:F32 -> @+p:F32 -> F32

def tc_pow.go source · line 735 · raw

@ws:List<&2, F32> -> @w0s:List<&2, F32> -> @sigs:List<&2, F32> -> @advs:List<&2, F32> -> @+kappa:F32 -> @+beta:F32 -> @acc:F32 -> F32

def tc_pow.pos source · line 745 · raw

@ok:Bool -> @ws:List<&2, F32> -> @w0s:List<&2, F32> -> @sigs:List<&2, F32> -> @advs:List<&2, F32> -> @+kappa:F32 -> @+beta:F32 -> Maybe<&2, F32>

def tc_pow.pick source · line 752 · raw

@ok:Bool -> @ws:List<&2, F32> -> @w0s:List<&2, F32> -> @+sigs:List<&2, F32> -> @+advs:List<&2, F32> -> @+kappa:F32 -> @+beta:F32 -> Maybe<&2, F32>

def trade_cost_power source · line 762 · raw

@w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @w0:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+kappa:F32 -> @sigma:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @adv:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+beta:F32 -> Maybe<&2, F32>

Power-law market impact (Almgren-Chriss / Kyle, Ch 11.1.1): sum_i kappa * sigma_i * (|w_i - w0_i| / adv_i)^beta * |w_i - w0_i|.

def OptConfig.default source · line 811 · raw

OptConfig

def Constraints.box source · line 814 · raw

@+l_w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @u_w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Constraints

def TradeCosts.zero source · line 818 · raw

@+n:U32 -> TradeCosts

def mvo_con.wrap source · line 821 · raw

@mo:Maybe<&2, 0x94e4b05af67d6c1408f568b7c9d84041/admm.SolveOut> -> Maybe<&2, OptResult>

def mvo_con.vals source · line 828 · raw

@ok:Bool -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @d_s:List<&2, F32> -> @a_s:List<&2, F32> -> @w0_s:List<&2, F32> -> @lw_s:List<&2, F32> -> @uw_s:List<&2, F32> -> @a_mat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @lc_v:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @uc_v:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @c1_s:List<&2, F32> -> @c2_s:List<&2, F32> -> @+n:U32 -> @+k:U32 -> @+m:U32 -> @+gmv:F32 -> @+rho:F32 -> @cfg:OptConfig -> Maybe<&2, OptResult>

def mvo_con.dims_ok source · line 856 · raw

@+bm:U32 -> @+bn:U32 -> @+cm:U32 -> @+cn:U32 -> @+dn:U32 -> @+an:U32 -> @+w0n:U32 -> @+lwn:U32 -> @+uwn:U32 -> @+am:U32 -> @+anc:U32 -> @+lcn:U32 -> @+ucn:U32 -> @+c1n:U32 -> @+c2n:U32 -> Bool

def mvo_con.pick source · line 868 · raw

@ok:Bool -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+d_s:List<&2, F32> -> @a_s:List<&2, F32> -> @w0_s:List<&2, F32> -> @lw_s:List<&2, F32> -> @uw_s:List<&2, F32> -> @a_mat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @lc_v:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @uc_v:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+c1_s:List<&2, F32> -> @+c2_s:List<&2, F32> -> @+n:U32 -> @+k:U32 -> @+m:U32 -> @+gmv:F32 -> @+rho:F32 -> @cfg:OptConfig -> Maybe<&2, OptResult>

def mvo_constrained source · line 899 · raw

@fm:FactorModel -> @alpha:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @w0:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @cons:Constraints -> @costs:TradeCosts -> @+risk_aversion:F32 -> @cfg:OptConfig -> Maybe<&2, OptResult>

Constrained, turnover- and market-impact-aware MVO via Woodbury-preconditioned ADMM: min_w (rho/2) w^T Omega_r w - alpha^T w + (1/2) (w - w0)^T diag(c2) (w - w0) + sum c1_i |w_i - w0_i| s.t. l_w <= w <= u_w, l_c <= A w <= u_c, ||w||_1 <= gmv_max (if > 0).

def split.cons source · line 931 · raw

@-A:Data -> @h:A -> @two:Two<A> -> Two<A>

def split source · line 935 · raw

@-A:Data -> @k:Nat -> @xs:List<&2, A> -> Two<A>

def hedge_ratio.div source · line 990 · raw

@ok:Bool -> @num:F32 -> @den:F32 -> Maybe<&2, F32>

def hedge_ratio.dots source · line 997 · raw

@mnum:Maybe<&2, F32> -> @mden:Maybe<&2, F32> -> Maybe<&2, F32>

def hedge_ratio.go source · line 1004 · raw

@momh:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @w_core:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @w_hedge:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

def hedge_ratio_model source · line 1013 · raw

@fm:FactorModel -> @w_core:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+w_hedge:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

Optimal single-instrument hedge size x_h^* = -beta(r_c, r_h) (Eq 12.2, 12.4). Hedged portfolio is w_core + x_h^* * w_hedge.

def hedge_inst.chol source · line 1016 · raw

@ml:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @rhs:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_inst.solve source · line 1023 · raw

@mgh:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @mrhs:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_inst.add source · line 1030 · raw

@mfac:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @midio:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @mrhs:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_inst.neg source · line 1037 · raw

@mv:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_inst.rhs source · line 1044 · raw

@momc:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @h_mat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_inst.fbh source · line 1051 · raw

@mofbh:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @bh:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @midio:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @mrhs:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_inst.bh source · line 1058 · raw

@mbh:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @midio:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @mrhs:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_inst.k0 source · line 1065 · raw

@is_k0:Bool -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+h_mat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @mrhs:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_inst.m0 source · line 1072 · raw

@is_m0:Bool -> @fm:FactorModel -> @w_core:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+h_mat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_inst.pick source · line 1081 · raw

@ok:Bool -> @hm:U32 -> @fm:FactorModel -> @w_core:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @h_mat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_instruments source · line 1090 · raw

@+fm:FactorModel -> @+w_core:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+h_mat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

Multi-instrument minimum-variance hedge x^* = -(H^T Omega_r H)^{-1} (H^T Omega_r w_core). h_mat is N x M (columns are tradeable hedge baskets/ETFs/FMPs). Returns length-M weights x^*.

def hedge_fexact.apply source · line 1096 · raw

@mp:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @mbc:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_factor_exact source · line 1104 · raw

@+fm:FactorModel -> @w_core:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

Exact zero-factor-exposure hedge via FMPs: w_h = -P (B^T w_core) (Ch 12.2.1 Procedure 12.2).

def list_sq source · line 1107 · raw

@xs:List<&2, F32> -> List<&2, F32>

def vec_sq source · line 1114 · raw

@v:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec

def hedge_shrink.build source · line 1118 · raw

@mraw:Maybe<&2, F32> -> @mev:Maybe<&2, F32> -> Maybe<&2, HedgeShrink>

def hedge_shrink.nonneg source · line 1128 · raw

@ok:Bool -> @+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @beta_hat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @se_beta:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, HedgeShrink>

def hedge_shrink.pick source · line 1135 · raw

@ok:Bool -> @ses:List<&2, F32> -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @beta_hat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @se_beta:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, HedgeShrink>

def hedge_beta_shrink source · line 1143 · raw

@+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+beta_hat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+se_beta:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, HedgeShrink>

Beta-error shrinkage hedge with diagonal error variance diag(se_beta^2) (Eq 12.6-12.7).

def hedge_shrink_cov.pick source · line 1149 · raw

@ok:Bool -> @+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @beta_hat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @cov_eta:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> Maybe<&2, HedgeShrink>

def hedge_beta_shrink_cov source · line 1157 · raw

@+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+beta_hat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+cov_eta:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> Maybe<&2, HedgeShrink>

Beta-error shrinkage hedge with general error covariance Omega_eta (Ch 12.3 step 4).

def hedge_track.scale source · line 1163 · raw

@mbz:Maybe<&2, F32> -> @z:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+mu_h:F32 -> @+sigma_h:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_track.after_wb source · line 1173 · raw

@mz:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @beta_hat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+mu_h:F32 -> @+sigma_h:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_track.with_d source · line 1180 · raw

@md:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+beta_hat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+mu_h:F32 -> @+sigma_h:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_track.pos source · line 1187 · raw

@ok:Bool -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @beta_hat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @se_beta:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+mu_h:F32 -> @+sigma_h:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_track.pick source · line 1195 · raw

@ok:Bool -> @ses:List<&2, F32> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @beta_hat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @se_beta:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+mu_h:F32 -> @+sigma_h:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def hedge_track_series source · line 1204 · raw

@fm:FactorModel -> @+beta_hat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+se_beta:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+mu_h:F32 -> @+sigma_h:F32 -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

Regularized tracking portfolio for a non-tradeable time series (Ch 12.4): solves (Omega_r + mu_h^2 diag(se^2) + mu_h^2 beta beta^T) w* = (mu_h^2 + sigma_h^2) beta.

def kelly_fraction.pos source · line 1221 · raw

@ok:Bool -> @mu:F32 -> @+sigma:F32 -> Maybe<&2, F32>

def kelly_fraction source · line 1229 · raw

@mu:F32 -> @+sigma:F32 -> Maybe<&2, F32>

Continuous-time / second-order Kelly fraction x* = mu / sigma^2 (Ch 13.1).

def kelly_disc.pos source · line 1232 · raw

@ok:Bool -> @+mu:F32 -> @+sigma:F32 -> Maybe<&2, F32>

def kelly_fraction_discrete source · line 1240 · raw

@+mu:F32 -> @+sigma:F32 -> Maybe<&2, F32>

Discrete-time Gaussian second-order Kelly fraction x_1* = mu / (mu^2 + sigma^2) (Eq 13.9).

def frac_kelly.pos source · line 1243 · raw

@ok:Bool -> @mu:F32 -> @+sigma:F32 -> @frac:F32 -> Maybe<&2, F32>

def fractional_kelly source · line 1251 · raw

@mu:F32 -> @+sigma:F32 -> @+frac:F32 -> Maybe<&2, F32>

Fractional Kelly sizing f = frac * (mu / sigma^2) (Eq 13.14).

def gz_formula source · line 1254 · raw

@+kelly:F32 -> @+max_dd:F32 -> @+cur_dd:F32 -> F32

def gz.ok source · line 1259 · raw

@ok:Bool -> @mu:F32 -> @+sigma:F32 -> @max_dd:F32 -> @cur_dd:F32 -> Maybe<&2, F32>

def grossman_zhou source · line 1268 · raw

@mu:F32 -> @+sigma:F32 -> @+max_dd:F32 -> @+cur_dd:F32 -> Maybe<&2, F32>

Grossman-Zhou drawdown-controlled dynamic Kelly fraction (Eq 13.13, 13.15): f = (mu / sigma^2) * max(0, 1 - (1 - max_dd) / (1 - cur_dd)).

def gz_step source · line 1276 · raw

@st:GzState -> @r:F32 -> GzState

def gz_extract source · line 1284 · raw

@xs:List<&2, GzState> -> List<&2, DrawdownState>

def gz_path.ok source · line 1291 · raw

@ok:Bool -> @returns:List<&2, F32> -> @mu:F32 -> @+sigma:F32 -> @+max_dd:F32 -> Maybe<&2, List<&2, DrawdownState>>

def grossman_zhou_path source · line 1303 · raw

@returns:List<&2, F32> -> @mu:F32 -> @+sigma:F32 -> @+max_dd:F32 -> Maybe<&2, List<&2, DrawdownState>>

Strictly causal Grossman-Zhou wealth/drawdown/allocation path over a return series (Ch 13.4). Proved causal in PROOF.bend via scan_causal.

def list_sum.go source · line 1351 · raw

@xs:List<&2, F32> -> @acc:F32 -> F32

def vec_sum source · line 1358 · raw

@v:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> F32

def est_fret.pos source · line 1362 · raw

@ok:Bool -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @r:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def est_fret.k0 source · line 1369 · raw

@is_k0:Bool -> @ds:List<&2, F32> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @r:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def est_fret.pick source · line 1376 · raw

@ok:Bool -> @bn:U32 -> @ds:List<&2, F32> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @r:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def estimate_factor_returns source · line 1384 · raw

@fm:FactorModel -> @+r:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

Cross-sectional WLS factor return estimate f_hat = (B^T Omega_eps^{-1} B)^{-1} B^T Omega_eps^{-1} r.

def aev.norm2 source · line 1391 · raw

@my:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> Maybe<&2, F32>

def aev.from_qr source · line 1399 · raw

@mqr:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.QR> -> @mexp:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> Maybe<&2, F32>

def aev.weighted source · line 1406 · raw

@ma:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @mexp:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> Maybe<&2, F32>

def aev.pos source · line 1413 · raw

@ok:Bool -> @+b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

def aev.k0 source · line 1420 · raw

@is_k0:Bool -> @ds:List<&2, F32> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

def aev.pick source · line 1427 · raw

@ok:Bool -> @bn:U32 -> @ds:List<&2, F32> -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

def attrib_error_var source · line 1435 · raw

@fm:FactorModel -> @+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, F32>

Attribution error variance b^T (B^T Omega_eps^{-1} B)^{-1} b (Ch 14.2.2).

def attrib_p.finish source · line 1442 · raw

@mpos:Maybe<&2, F32> -> @mfc:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @mic:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @mev:Maybe<&2, F32> -> @+realized_pnl:F32 -> Maybe<&2, PeriodAttrib>

def attrib_p.with_eps source · line 1449 · raw

@mexp:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @meps:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @r:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @f_ret:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @mev:Maybe<&2, F32> -> @+realized_pnl:F32 -> Maybe<&2, PeriodAttrib>

def attrib_p.with_bf source · line 1456 · raw

@mbf:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @+fm:FactorModel -> @+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+r:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @f_ret:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+realized_pnl:F32 -> Maybe<&2, PeriodAttrib>

def attrib_p.pick source · line 1463 · raw

@ok:Bool -> @+fm:FactorModel -> @w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @r:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+f_ret:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+realized_pnl:F32 -> Maybe<&2, PeriodAttrib>

def attribute_period source · line 1473 · raw

@+fm:FactorModel -> @+w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+r:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+f_ret:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+realized_pnl:F32 -> Maybe<&2, PeriodAttrib>

Single-period holdings-based attribution (Ch 14.1, 14.2): position_pnl = factor_pnl + idio_pnl; trading_pnl = realized_pnl - position_pnl.

def idx_in source · line 1482 · raw

@+x:U32 -> @xs:List<&2, U32> -> Bool

def subset_valid source · line 1489 · raw

@xs:List<&2, U32> -> @+k:U32 -> Bool

def build_comp.go source · line 1496 · raw

@rem:Nat -> @+i:U32 -> @+sub:List<&2, U32> -> List<&2, U32>

def build_comp source · line 1504 · raw

@+k:U32 -> @sub:List<&2, U32> -> List<&2, U32>

def get_f32_or source · line 1507 · raw

@m:Maybe<&2, F32> -> F32

def gather_vec.go source · line 1514 · raw

@idxs:List<&2, U32> -> @+xs:List<&2, F32> -> List<&2, F32>

def gather_vec source · line 1521 · raw

@+idxs:List<&2, U32> -> @v:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec

def get_row_or source · line 1525 · raw

@m:Maybe<&2, List<&2, F32>> -> List<&2, F32>

def gather_mat.go source · line 1532 · raw

@r_idxs:List<&2, U32> -> @+c_idxs:List<&2, U32> -> @+rows:List<&2, List<&2, F32>> -> List<&2, List<&2, F32>>

def gather_mat source · line 1539 · raw

@+r_idxs:List<&2, U32> -> @+c_idxs:List<&2, U32> -> @a:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat

def sym_mat.half source · line 1543 · raw

@ms:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat>

def sym_mat source · line 1550 · raw

@+m:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat>

def max_attr.dots source · line 1553 · raw

@msp:Maybe<&2, F32> -> @mrp:Maybe<&2, F32> -> @adj:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @rf:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @rc:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> Maybe<&2, MaxAttrib>

def max_attr.wrap source · line 1560 · raw

@madj:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @mrf:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @mrc:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @fs:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @bbar:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, MaxAttrib>

def max_attr.sym_cov source · line 1567 · raw

@md:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat>

def max_attr.sub_cov source · line 1574 · raw

@mp:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @o_bb:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat>

def max_attr.add_v source · line 1581 · raw

@mxb:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @bs:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def max_attr.sub_v source · line 1588 · raw

@mxtf:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec> -> @fbar:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec>

def max_attr.with_x source · line 1595 · raw

@mx:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @bs:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+bbar:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+fs:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @fbar:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @o_sb:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @o_bb:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> Maybe<&2, MaxAttrib>

def max_attr.chol source · line 1605 · raw

@ml:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @bs:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @bbar:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @fs:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @fbar:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+o_sb:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @o_bb:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> Maybe<&2, MaxAttrib>

def max_attr.q0 source · line 1612 · raw

@is_q0:Bool -> @+cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+f_ret:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+sub:List<&2, U32> -> @+comp:List<&2, U32> -> Maybe<&2, MaxAttrib>

def max_attr.p0 source · line 1628 · raw

@is_p0:Bool -> @+cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+f_ret:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+sub:List<&2, U32> -> @+k:U32 -> Maybe<&2, MaxAttrib>

def max_attr.valid source · line 1636 · raw

@ok:Bool -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @f_ret:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+sub:List<&2, U32> -> @+k:U32 -> Maybe<&2, MaxAttrib>

def max_attr.pick source · line 1643 · raw

@ok:Bool -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @f_ret:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+sub:List<&2, U32> -> @+k:U32 -> Maybe<&2, MaxAttrib>

def maximal_attribution source · line 1652 · raw

@+cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+f_ret:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @subset:List<&2, U32> -> Maybe<&2, MaxAttrib>

Maximal performance attribution for factor subset S (Ch 14.3, Eq 14.6-14.10). Returns None on dimension mismatch or invalid/duplicate subset indices.

def find_pos.go source · line 1660 · raw

@+x:U32 -> @xs:List<&2, U32> -> @+pos:U32 -> Maybe<&2, U32>

def map_to_local.cons source · line 1667 · raw

@mp:Maybe<&2, U32> -> @mr:Maybe<&2, List<&2, U32>> -> Maybe<&2, List<&2, U32>>

def map_to_local source · line 1674 · raw

@grp:List<&2, U32> -> @+avail:List<&2, U32> -> Maybe<&2, List<&2, U32>>

def filter_avail source · line 1681 · raw

@avail:List<&2, U32> -> @+grp:List<&2, U32> -> List<&2, U32>

def range_u32.go source · line 1689 · raw

@rem:Nat -> @+i:U32 -> List<&2, U32>

def rev_ng.go source · line 1699 · raw

@xs:List<&2, NestedGroup> -> @acc:List<&2, NestedGroup> -> List<&2, NestedGroup>

def nested.step_one source · line 1706 · raw

@mma:Maybe<&2, MaxAttrib> -> @b_cur:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @comp_loc:List<&2, U32> -> @avail_next:List<&2, U32> -> @acc:List<&2, NestedGroup> -> Maybe<&2, NestedCur>

def nested.with_loc source · line 1713 · raw

@mloc:Maybe<&2, List<&2, U32>> -> @+cov_cur:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+b_cur:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @f_cur:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @avail:List<&2, U32> -> @grp:List<&2, U32> -> @acc:List<&2, NestedGroup> -> Maybe<&2, NestedCur>

def nested.go source · line 1723 · raw

@groups:List<&2, List<&2, U32>> -> @cur:Maybe<&2, NestedCur> -> Maybe<&2, List<&2, NestedGroup>>

def nested.pick source · line 1732 · raw

@ok:Bool -> @cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @f_ret:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @groups:List<&2, List<&2, U32>> -> @+k:U32 -> Maybe<&2, List<&2, NestedGroup>>

def nested_attribution source · line 1740 · raw

@+cov_f:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+b:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @+f_ret:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @groups:List<&2, List<&2, U32>> -> Maybe<&2, List<&2, NestedGroup>>

Nested maximal attribution over ordered disjoint factor groups (Ch 14.3 Procedure 14.2).

def sgn_f32 source · line 1761 · raw

@+x:F32 -> F32

def ss.go source · line 1764 · raw

@ws:List<&2, F32> -> @es:List<&2, F32> -> @ds:List<&2, F32> -> @acc:SsAcc -> SsAcc

def ss.finish source · line 1781 · raw

@acc:SsAcc -> SelectionSizing

def ss.pos source · line 1794 · raw

@ok:Bool -> @ws:List<&2, F32> -> @es:List<&2, F32> -> @ds:List<&2, F32> -> Maybe<&2, SelectionSizing>

def ss.pick source · line 1801 · raw

@ok:Bool -> @ws:List<&2, F32> -> @es:List<&2, F32> -> @+ds:List<&2, F32> -> Maybe<&2, SelectionSizing>

def selection_sizing source · line 1810 · raw

@w:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @eps:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @idio_var:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> Maybe<&2, SelectionSizing>

Selection vs. sizing IR decomposition (Ch 14.4): ir == selection * diversification + sizing, and selection == q_long * sel_long + q_short * sel_short.

def dot_rows.go source · line 1821 · raw

@ws:List<&2, F32> -> @rs:List<&2, F32> -> @acc:F32 -> F32

def dot_pair source · line 1828 · raw

@_u:Unit -> @p:RowPair -> F32

def zip_rows source · line 1832 · raw

@ws:List<&2, List<&2, F32>> -> @rs:List<&2, List<&2, F32>> -> List<&2, RowPair>

def tail_rows source · line 1839 · raw

@rs:List<&2, List<&2, F32>> -> List<&2, List<&2, F32>>

def forward_pnl.pick source · line 1846 · raw

@ok:Bool -> @+t:U32 -> @ws:List<&2, List<&2, F32>> -> @rs:List<&2, List<&2, F32>> -> Maybe<&2, List<&2, F32>>

def forward_pnl source · line 1856 · raw

@weights:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @returns:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> Maybe<&2, List<&2, F32>>

Causal forward PnL: pairs portfolio row t with next-period return row t + 1 (t = 0 .. T - 2). Row j of returns is the return over (j - 1, j], matching signal's convention.

def attrib_item_fn source · line 1867 · raw

@dims:AttribDims -> @item:AttribItem -> Maybe<&2, PeriodAttrib>

def zip_attrib source · line 1873 · raw

@fms:List<&2, FactorModel> -> @ws:List<&2, List<&2, F32>> -> @rs:List<&2, List<&2, F32>> -> @fs:List<&2, List<&2, F32>> -> List<&2, AttribItem>

def forward_attrib.pick source · line 1880 · raw

@ok:Bool -> @+t:U32 -> @+n:U32 -> @+k:U32 -> @fms:List<&2, FactorModel> -> @ws:List<&2, List<&2, F32>> -> @rs:List<&2, List<&2, F32>> -> @fs:List<&2, List<&2, F32>> -> Maybe<&2, List<&2, Maybe<&2, PeriodAttrib>>>

def forward_attribute source · line 1889 · raw

@+fms:List<&2, FactorModel> -> @weights:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @returns:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @f_rets:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> Maybe<&2, List<&2, Maybe<&2, PeriodAttrib>>>

Causal multi-period attribution: pairs (fm[t], weights[t]) with (returns[t+1], f_rets[t+1]).

Templates

template list_map_f32 source · line 64 · raw

@-f:(@_:F32 -> F32) -> @xs:List<&2, F32> -> List<&2, F32>

template pseq source · line 944 · raw

@-A:Data -> @-B:Data -> @-C:Data -> @-f:(@_:C -> @_:A -> B) -> @xs:List<&2, A> -> @+c:C -> List<&2, B>

template par source · line 951 · raw

@-A:Data -> @-B:Data -> @-C:Data -> @-f:(@_:C -> @_:A -> B) -> @+depth:Nat -> @two:Two<A> -> @+h:U32 -> @+r:U32 -> @+c:C -> List<&2, B>

template pmap source · line 965 · raw

@-A:Data -> @-B:Data -> @-C:Data -> @-f:(@_:C -> @_:A -> B) -> @xs:List<&2, A> -> @+t:U32 -> @+c:C -> List<&2, B>

Parallel map over a list of known length t: 2 tasks below 64, 64 tasks at 64+.

template scan source · line 971 · raw

@-A:Data -> @-S:Data -> @-step:(@_:S -> @_:A -> S) -> @xs:List<&2, A> -> @s:S -> List<&2, S>

Strictly causal left scan: output[i] depends only on input[0..i] and s0.