Public API — re-exports all sub-namespaces for single-ns consumers.
Aliases here carry the defining var's :doc, :arglists, :file and :line, so (doc linopt-commons-math3.core/foo) shows the real documentation and REPL-driven editor hover (Calva, CIDER) works. Static-only analysis (clj-kondo, Cursive's offline index) does not expand this macro and will show less — require the defining namespace directly if that is how your editor resolves symbols.
Public API — re-exports all sub-namespaces for single-ns consumers. Aliases here carry the defining var's :doc, :arglists, :file and :line, so (doc linopt-commons-math3.core/foo) shows the real documentation and REPL-driven editor hover (Calva, CIDER) works. Static-only analysis (clj-kondo, Cursive's offline index) does not expand this macro and will show less — require the defining namespace directly if that is how your editor resolves symbols.
(benchmark-relative-average-constraint universe-coll
bench-coll
weight-fn
value-fn
ow-uw)(benchmark-relative-average-constraint universe-coll
bench-coll
weight-fn
value-fn
ow-uw
total-weight)±ow-uw band around the benchmark's weight-weighted average of value-fn
(e.g. duration, beta). bench-coll/weight-fn compute the benchmark
average; the LP coefficients are value-fn applied to universe-coll.
The LP variables are weights on some total scale (e.g. 0-100 NAV%, or the
invested 100-minus-cash if some weight is held back); total-weight is
that scale (default 1.0, i.e. weights already sum to 1), needed because
sum(value_i * weight_i) = bench-avg * total-weight at the true benchmark
average, not bench-avg itself.
`±ow-uw` band around the benchmark's weight-weighted average of `value-fn` (e.g. duration, beta). `bench-coll`/`weight-fn` compute the benchmark average; the LP coefficients are `value-fn` applied to `universe-coll`. The LP variables are weights on some total scale (e.g. 0-100 NAV%, or the invested 100-minus-cash if some weight is held back); `total-weight` is that scale (default `1.0`, i.e. weights already sum to 1), needed because `sum(value_i * weight_i) = bench-avg * total-weight` at the true benchmark average, not `bench-avg` itself.
(benchmark-relative-group-constraint universe-coll
bench-coll
weight-fn
group-key-fn
ow-uw)Per-group ±ow-uw band around each group's benchmark weight. The LP
coefficient vectors run over universe-coll (the investable/LP variables);
the benchmark weight for each group is computed over bench-coll (the
reference universe, via weight-fn/group-key-fn). Only groups that have
at least one investable item are constrained - a group with benchmark
weight but no investable item can't be held, so a positive lower bound for
it would be infeasible.
Per-group `±ow-uw` band around each group's benchmark weight. The LP coefficient vectors run over `universe-coll` (the investable/LP variables); the benchmark weight for each group is computed over `bench-coll` (the reference universe, via `weight-fn`/`group-key-fn`). Only groups that have at least one investable item are constrained - a group with benchmark weight but no investable item can't be held, so a positive lower bound for it would be infeasible.
(benchmark-weights-by bench-coll weight-fn group-key-fn)Sum weight-fn over bench-coll (the reference/true-index universe),
grouped by group-key-fn. Feeds benchmark-relative-group-constraint.
Sum `weight-fn` over `bench-coll` (the reference/true-index universe), grouped by `group-key-fn`. Feeds `benchmark-relative-group-constraint`.
(bv n i w)Creates a basis vector: an all-zero double array of length n with w at index i.
Creates a basis vector: an all-zero double array of length `n` with `w` at index `i`.
(exclude-values universe k excluded)Drop items whose (get item k) is in excluded (a collection or set).
Drop items whose `(get item k)` is in `excluded` (a collection or set).
(exclude-values-all universe exclusions)Apply exclude-values for each [k excluded] pair.
Apply `exclude-values` for each `[k excluded]` pair.
(group-constraint universe group-key target-value [mn mx])Band constraint [min max] on the summed weight of items where
(get item group-key) equals target-value.
Band constraint `[min max]` on the summed weight of items where `(get item group-key)` equals `target-value`.
(grouped-cap-constraint universe group-key-fn cap-fn)Per-group upper bound, e.g. an issuer/ticker concentration limit where the cap itself depends on the group (rating, sector, ...).
group-key-fn maps an item to its group. cap-fn receives [group items]
(all items in that group) and returns that group's max weight, or nil to
skip the constraint entirely for that group.
Per-group upper bound, e.g. an issuer/ticker concentration limit where the cap itself depends on the group (rating, sector, ...). `group-key-fn` maps an item to its group. `cap-fn` receives `[group items]` (all items in that group) and returns that group's max weight, or nil to skip the constraint entirely for that group.
(keep-values universe k included)Keep only items whose (get item k) is in included (a collection or set) -
e.g. restrict a universe to index constituents or currently-held names.
Keep only items whose `(get item k)` is in `included` (a collection or set) - e.g. restrict a universe to index constituents or currently-held names.
(linear-optimization target constraints)(linear-optimization target
constraints
{:keys [goal epsilon max-ulps cut-off rule non-negative?
max-iter stats?]
:or {goal :minimize
epsilon 1.0E-6
max-ulps 10
cut-off 1.0E-10
rule :dantzig
non-negative? false
max-iter Integer/MAX_VALUE}})Solves a linear problem.
Target is defined as a vector of coefficients and constant as the last value:
[a1 a2 a3 ... c] means f(x1,x2,x3) = a1*x1 + a2*x2 + a3*x3 + ... + c
Constraints are defined as a sequence of one of the following triplets:
[a1 a2 a3 ...] R n - which means a1*x1+a2*x2+a3*x3+... R n[a1 a2 a3 ... ca] R [b1 b2 b3 ... cb] - which means a1*x1+a2*x2+a3*x3+...+ca R b1*x1+b2*x2+b3*x3+...+cb
where R is a relationship and can be one of <=, >= or = as symbol or keyword. Also :leq, :geq and :eq are valid.Function returns pair of optimal point and function value. If stat? option is set to true, returns also information about number of iterations.
Possible options:
:goal - :minimize (default) or :maximize:rule - pivot selection rule, :dantzig (default) or :bland:max-iter - maximum number of iterations, maximum integer by default:non-negative? - allow non-negative variables only, default: false:epsilon - convergence value, default: 1.0e-6::max-ulps - floating point comparisons, default: 10 ulp:cut-off - pivot elements smaller than cut-off are treated as zero, default: 1.0e-10(linear-optimization [-1 4 0] [[-3 1] :<= 6
[-1 -2] :>= -4
[0 1] :>= -3])
;; => [(9.999999999999995 -3.0) -21.999999999999993]
Solves a linear problem.
Target is defined as a vector of coefficients and constant as the last value:
`[a1 a2 a3 ... c]` means `f(x1,x2,x3) = a1*x1 + a2*x2 + a3*x3 + ... + c`
Constraints are defined as a sequence of one of the following triplets:
* `[a1 a2 a3 ...] R n` - which means `a1*x1+a2*x2+a3*x3+... R n`
* `[a1 a2 a3 ... ca] R [b1 b2 b3 ... cb]` - which means `a1*x1+a2*x2+a3*x3+...+ca R b1*x1+b2*x2+b3*x3+...+cb`
where `R` is a relationship and can be one of `<=`, `>=` or `=` as symbol or keyword. Also `:leq`, `:geq` and `:eq` are valid.
Function returns pair of optimal point and function value. If `stat?` option is set to true, returns also information about number of iterations.
Possible options:
* `:goal` - `:minimize` (default) or `:maximize`
* `:rule` - pivot selection rule, `:dantzig` (default) or `:bland`
* `:max-iter` - maximum number of iterations, maximum integer by default
* `:non-negative?` - allow non-negative variables only, default: `false`
* `:epsilon` - convergence value, default: `1.0e-6`:
* `:max-ulps` - floating point comparisons, default: `10` ulp
* `:cut-off` - pivot elements smaller than cut-off are treated as zero, default: `1.0e-10`
```clojure
(linear-optimization [-1 4 0] [[-3 1] :<= 6
[-1 -2] :>= -4
[0 1] :>= -3])
;; => [(9.999999999999995 -3.0) -21.999999999999993]
```(linear-optimization-generic target constraints)(linear-optimization-generic target constraints options)Generic linear optimization that supports optional absolute sum constraint
(pass :sum-of-abs-weights in options to allow short positions).
Generic linear optimization that supports optional absolute sum constraint (pass `:sum-of-abs-weights` in options to allow short positions).
(linear-optimization-with-abs-sum target constraints sum-of-abs-weights)(linear-optimization-with-abs-sum target constraints sum-of-abs-weights options)Linear optimization with sum of absolute values constraint.
For each original variable x_i, we introduce two non-negative variables:
Where: x_i = x_i+ - x_i- and |x_i| = x_i+ + x_i-
Parameters:
Example: (linear-optimization-with-abs-sum [-1 4 0] ; objective coefficients [[-3 1] :<= 6 ; other constraints [1 1] := 100] ; sum of weights = 100 120.0 ; sum of absolute weights {})
Linear optimization with sum of absolute values constraint.
For each original variable x_i, we introduce two non-negative variables:
- x_i+ (positive part)
- x_i- (negative part)
Where: x_i = x_i+ - x_i- and |x_i| = x_i+ + x_i-
Parameters:
- target: vector of coefficients for objective function [a1 a2 ... c]
- constraints: standard linear constraints (will be transformed, should include sum-of-weights)
- sum-of-abs-weights: target for sum of absolute weights (must be positive)
- options: same as linear-optimization
Example:
(linear-optimization-with-abs-sum
[-1 4 0] ; objective coefficients
[[-3 1] :<= 6 ; other constraints
[1 1] := 100] ; sum of weights = 100
120.0 ; sum of absolute weights
{})(maximize-objective universe objective-key constraints)(maximize-objective universe objective-key constraints options)Solve for a universe (a seq of items/maps) whose objective coefficients are
(get item objective-key), maximizing sum(objective) subject to constraints.
options are the same as linear-optimization-generic (e.g. :non-negative?,
:sum-of-abs-weights); :goal is forced to :maximize.
Solve for a universe (a seq of items/maps) whose objective coefficients are `(get item objective-key)`, maximizing sum(objective) subject to `constraints`. `options` are the same as `linear-optimization-generic` (e.g. `:non-negative?`, `:sum-of-abs-weights`); `:goal` is forced to `:maximize`.
(position-bounds n mn mx)Per-item [min max] bound on each of the n variables. mn/mx can be
nil to skip that side - e.g. pass nil for mn when the solver already
gets :non-negative? true, so a redundant >= 0 row (and its n-long
coefficient array) is never allocated instead of being built and later
dropped by the solver.
Per-item `[min max]` bound on each of the `n` variables. `mn`/`mx` can be `nil` to skip that side - e.g. pass `nil` for `mn` when the solver already gets `:non-negative? true`, so a redundant `>= 0` row (and its `n`-long coefficient array) is never allocated instead of being built and later dropped by the solver.
(predicate-constraint universe pred rel target)Single one-sided constraint on the summed weight of items matching pred
(an arbitrary item predicate): sum(weight_i : pred(item_i)) rel target.
Generalizes rating-bucket / flag-style caps that group-constraint can't
express because membership isn't a single equality check. For a band on
both sides, concat two calls (one :<=, one :>=).
Single one-sided constraint on the summed weight of items matching `pred` (an arbitrary item predicate): `sum(weight_i : pred(item_i)) rel target`. Generalizes rating-bucket / flag-style caps that `group-constraint` can't express because membership isn't a single equality check. For a band on both sides, concat two calls (one `:<=`, one `:>=`).
(range-cut universe k [mn mx])Keep only items where (get item k) falls within [min max] inclusive.
Uses compare, not <=, so it works for any Comparable (numbers, strings,
dates, ...), not just numbers.
Keep only items where `(get item k)` falls within `[min max]` inclusive. Uses `compare`, not `<=`, so it works for any Comparable (numbers, strings, dates, ...), not just numbers.
(range-cut-all universe cuts)Apply a sequence of [k min max] range cuts in turn.
Apply a sequence of `[k min max]` range cuts in turn.
(require-numeric universe ks)Drop items where (get item k) is missing or NaN, for each k in ks.
Useful before an optimisation target or risk constraint that needs a real
number for every surviving item.
Drop items where `(get item k)` is missing or NaN, for each `k` in `ks`. Useful before an optimisation target or risk constraint that needs a real number for every surviving item.
(seq->double-array vs)Convert sequence to double array. Returns input if vs is double array already.
Convert sequence to double array. Returns input if `vs` is double array already.
(sum-constraint n target)Sum of all n variables equals target.
Sum of all `n` variables equals `target`.
(value-band-constraint universe value-key [mn mx])Band constraint [min max] on the sum of (get item value-key) across
universe (e.g. a weight-weighted duration/beta, where each item's LP
coefficient IS its per-unit value).
Band constraint `[min max]` on the sum of `(get item value-key)` across `universe` (e.g. a weight-weighted duration/beta, where each item's LP coefficient IS its per-unit value).
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |