signal.bend checks
raw source on the hub · import bend-quant-signal@0.1.0.0/signal.bend as Signal
signal: cross-sectional signal evaluation over a T x N panel. Design notes: https://github.com/robinmarin/bend-quant/blob/main/signal/DESIGN.md
Public: Panel, Summary, Panel.from_rows, Panel.from_flat, rank_ic, ic, forward_rank_ic, forward_ic, lead, summary, nw_t, zscore, demean, rank, winsorize, ewma, rolling_mean. Every other def is internal. LAWS.bend and the proofs name them, and they can change in any version.
2 imports
import Base import bend-kit-f32array@0.2.1.0/f32array.bend as A
Types
type Panel source · line 13 · raw
Data
Rows are dates, oldest first; column j is the same name on every date. NaN = missing.
Panel@t:U32 -> @n:U32 -> @rows:List<&2, List<&2, F32>> -> Panel
type Summary source · line 17 · raw
Data
IC statistics over the non-NaN dates. ir is per period; t = ir * sqrt(count).
Summary@mean:F32 -> @sd:F32 -> @ir:F32 -> @t:F32 -> @hit:F32 -> @count:U32 -> Summary
type Flat source · line 58 · raw
Type
Flat@a:0x10a054003f9647d4e93c2ae59fdb197a/f32array.F32Array -> @rows:List<&2, List<&2, F32>> -> Flat
type Two source · line 80 · raw
@-A:Data -> Data
Two@-A:Data -> @l:List<&2, A> -> @r:List<&2, A> -> Two<A>
type Tr source · line 88 · raw
@-A:Data -> Data
Leaf@-A:Data -> Tr<A>
Node@-A:Data -> @l:Tr<A> -> @v:A -> @c:Nat -> @s:Nat -> @r:Tr<A> -> Tr<A>
type Pr source · line 206 · raw
Data
Pr@x:F32 -> @y:F32 -> Pr
type KV source · line 235 · raw
@-A:Data -> @-B:Data -> Data
KV@-A:Data -> @-B:Data -> @k:A -> @v:B -> KV<A, B>
type XY source · line 448 · raw
Data
XY@x:List<&2, F32> -> @y:List<&2, F32> -> XY
type Tally source · line 523 · raw
Data
Tally@s:F32 -> @n:U32 -> @hits:U32 -> Tally
type Cfg source · line 681 · raw
Data
Cfg@n:U32 -> @q:F32 -> Cfg
type Ew source · line 720 · raw
Data
Ew@a:F32 -> @s:List<&2, F32> -> Ew
type Win source · line 749 · raw
Data
Win@w:Nat -> @rows:List<&2, List<&2, F32>> -> Win
type Cols source · line 756 · raw
Data
Cols@s:List<&2, F32> -> @c:List<&2, F32> -> Cols
Definitions
def nan source · line 20 · raw
F32
def is_nan source · line 23 · raw
@+x:F32 -> Bool
def drop_nan source · line 26 · raw
@xs:List<&2, F32> -> List<&2, F32>
def Panel.rows.ok source · line 35 · raw
@xs:List<&2, List<&2, F32>> -> @+n:Nat -> Bool
def Panel.rows.pick source · line 42 · raw
@ok:Bool -> @+rows:List<&2, List<&2, F32>> -> @+n:Nat -> Maybe<&2, Panel>
def Panel.from_rows source · line 50 · raw
@xs:List<&2, List<&2, F32>> -> Maybe<&2, Panel>
None when two rows differ in length.
def Panel.flat.add source · line 61 · raw
@r:Pair(0x10a054003f9647d4e93c2ae59fdb197a/f32array.F32Array, 0x10a054003f9647d4e93c2ae59fdb197a/f32array.F32Array) -> @rows:List<&2, List<&2, F32>> -> Flat
def Panel.flat.go source · line 65 · raw
@k:Nat -> @st:Flat -> @+n:U32 -> @+i:U32 -> List<&2, List<&2, F32>>
def Panel.from_flat source · line 75 · raw
@a:0x10a054003f9647d4e93c2ae59fdb197a/f32array.F32Array -> @+t:U32 -> @+n:U32 -> Panel
A row-major t x n array, as numpy's tofile writes it.
def tsize source · line 92 · raw
@-A:Data -> @t:Tr<A> -> Nat
def deal.cons source · line 170 · raw
@-A:Data -> @+x:A -> @two:Two<A> -> Two<A>
def deal source · line 175 · raw
@-A:Data -> @xs:List<&2, A> -> Two<A>
elements 0, 2, 4, .. and 1, 3, 5, ..
def shf source · line 185 · raw
@-A:Data -> @+k:Nat -> @two:Two<A> -> List<&2, A>
k levels of dealing, then the halves in order ponytail: a fixed interleave, balanced for sorted rows and random ones; a row ordered adversarially to it could still build a deep tree
def shuffle source · line 194 · raw
@-A:Data -> @k:Nat -> @xs:List<&2, A> -> List<&2, A>
def zip2 source · line 210 · raw
@xs:List<&2, F32> -> @ys:List<&2, F32> -> List<&2, Pr>
name j of xs with name j of ys, up to the shorter row
def both source · line 220 · raw
@p:Pr -> Bool
present on both sides; y is tested first, so a NaN y decides without x
def complete source · line 224 · raw
@xs:List<&2, F32> -> @ys:List<&2, F32> -> List<&2, Pr>
def nat_f32 source · line 227 · raw
@n:Nat -> F32
def key source · line 238 · raw
@-A:Data -> @-B:Data -> @x:KV<A, B> -> A
def val source · line 242 · raw
@-A:Data -> @-B:Data -> @x:KV<A, B> -> B
def emit source · line 298 · raw
@-B:Data -> @ys:List<&2, B> -> @+r:Nat -> @acc:List<&2, KV<B, Nat>> -> List<&2, KV<B, Nat>>
def pr_kv source · line 331 · raw
@ps:List<&2, Pr> -> List<&2, KV<F32, F32>>
def kv_pr source · line 338 · raw
@xs:List<&2, KV<Nat, Nat>> -> List<&2, Pr>
def prep source · line 347 · raw
@+ps:List<&2, Pr> -> @rank:Bool -> List<&2, Pr>
the pairs a Pearson IC runs on: the values, or for a rank IC their ranks (2 x the average rank; Pearson doesn't see the scale or the order)
def pr.sum source · line 357 · raw
@ps:List<&2, Pr> -> @+sa:F32 -> @+sb:F32 -> Pr
def pr.cent source · line 364 · raw
@ps:List<&2, Pr> -> @+ma:F32 -> @+mb:F32 -> @+saa:F32 -> @+sbb:F32 -> @+sab:F32 -> F32
def pearson.of source · line 373 · raw
@s:Pr -> @ps:List<&2, Pr> -> @+m:F32 -> F32
def pearson source · line 377 · raw
@+ps:List<&2, Pr> -> F32
def date.m source · line 380 · raw
@small:Bool -> @+ps:List<&2, Pr> -> F32
def core source · line 388 · raw
@+ps:List<&2, Pr> -> @+rank:Bool -> F32
the IC of a list of complete pairs; NaN for fewer than 3
def date_ic source · line 392 · raw
@x:List<&2, F32> -> @y:List<&2, F32> -> @+rank:Bool -> F32
NaN when fewer than 3 names are present in both rows
def split.cons source · line 409 · raw
@-A:Data -> @h:A -> @two:Two<A> -> Two<A>
def split source · line 414 · raw
@-A:Data -> @k:Nat -> @xs:List<&2, A> -> Two<A>
Two{take(k, xs), drop(k, xs)} in one pass
def date_xy source · line 451 · raw
@rank:Bool -> @p:XY -> F32
def zip source · line 456 · raw
@xs:List<&2, List<&2, F32>> -> @ys:List<&2, List<&2, F32>> -> List<&2, XY>
row i of xs with row i of ys, up to the shorter list
def ics source · line 465 · raw
@xs:List<&2, List<&2, F32>> -> @ys:List<&2, List<&2, F32>> -> @+t:U32 -> @+rank:Bool -> List<&2, F32>
def ic.chk source · line 468 · raw
@ok:Bool -> @rx:List<&2, List<&2, F32>> -> @ry:List<&2, List<&2, F32>> -> @+t:U32 -> @+rank:Bool -> Maybe<&2, List<&2, F32>>
def ic.go source · line 475 · raw
@x:Panel -> @y:Panel -> @+rank:Bool -> Maybe<&2, List<&2, F32>>
def rank_ic source · line 481 · raw
@x:Panel -> @y:Panel -> Maybe<&2, List<&2, F32>>
One Spearman IC per date (average ranks for ties); None when the shapes differ.
def ic source · line 485 · raw
@x:Panel -> @y:Panel -> Maybe<&2, List<&2, F32>>
One Pearson IC per date; None when the shapes differ.
def lead.rows source · line 490 · raw
@xs:List<&2, List<&2, F32>> -> @k:Nat -> @pad:List<&2, List<&2, F32>> -> List<&2, List<&2, F32>>
def lead source · line 496 · raw
@p:Panel -> @+h:U32 -> Panel
Row i of the result is row i + h of p (law lead_row); the last min(h, t) rows are NaN. With p a panel of one-period returns, lead(p, 1) pairs date i with the return over the period after it.
def fwd.rows source · line 506 · raw
@rs:List<&2, List<&2, F32>> -> @+h:Nat -> @+t:U32 -> @+n:U32 -> List<&2, List<&2, F32>>
def fwd source · line 509 · raw
@p:Panel -> @+h:Nat -> Panel
def forward_rank_ic source · line 514 · raw
@x:Panel -> @ret:Panel -> @+h:Nat -> Maybe<&2, List<&2, F32>>
IC i is the rank IC of signal row i against return row i + 1 + h (law forward_row).
def forward_ic source · line 518 · raw
@x:Panel -> @ret:Panel -> @+h:Nat -> Maybe<&2, List<&2, F32>>
The same with Pearson.
def tally.add source · line 526 · raw
@skip:Bool -> @+x:F32 -> @st:Tally -> Tally
def tally source · line 534 · raw
@xs:List<&2, F32> -> @st:Tally -> Tally
def ss source · line 541 · raw
@xs:List<&2, F32> -> @+m:F32 -> @+acc:F32 -> F32
def summary.of source · line 549 · raw
@few:Bool -> @+s:F32 -> @+n:U32 -> @+hits:U32 -> @+xs:List<&2, F32> -> Maybe<&2, Summary>
def summary.k source · line 560 · raw
@st:Tally -> @+xs:List<&2, F32> -> Maybe<&2, Summary>
def summary source · line 565 · raw
@+xs:List<&2, F32> -> Maybe<&2, Summary>
None with fewer than 2 non-NaN ICs.
def row.affine source · line 584 · raw
@xs:List<&2, F32> -> @+a:F32 -> @+b:F32 -> List<&2, F32>
def row.mean.of source · line 591 · raw
@st:Tally -> F32
def row.mean source · line 595 · raw
@+xs:List<&2, F32> -> F32
def row.sd.of source · line 598 · raw
@st:Tally -> @+xs:List<&2, F32> -> @+m:F32 -> F32
def row.zscore source · line 603 · raw
@+xs:List<&2, F32> -> List<&2, F32>
(x - mean) / sample sd over the present names; NaN when fewer than 2 or sd = 0
def row.demean source · line 607 · raw
@+xs:List<&2, F32> -> List<&2, F32>
def pct.go source · line 612 · raw
@xs:List<&2, F32> -> @+t:Tr<F32> -> @+m2:F32 -> List<&2, F32>
def row.rank source · line 619 · raw
@+xs:List<&2, F32> -> @+n:U32 -> List<&2, F32>
def wz.kv1 source · line 626 · raw
@b:Bool -> @+x:F32 -> @t:List<&2, KV<F32, Unit>> -> List<&2, KV<F32, Unit>>
the present values, keyed for msort
def wz.kv source · line 633 · raw
@xs:List<&2, F32> -> List<&2, KV<F32, Unit>>
def nth source · line 641 · raw
@xs:List<&2, KV<F32, Unit>> -> @k:Nat -> F32
key k of a list; NaN past the end
def q.at.pick source · line 652 · raw
@empty:Bool -> @+s:List<&2, KV<F32, Unit>> -> @+i:U32 -> @+j:U32 -> @+t:F32 -> F32
def q.at source · line 661 · raw
@+s:List<&2, KV<F32, Unit>> -> @+m:U32 -> @+q:F32 -> F32
the q quantile of m sorted keys; NaN for m = 0
def clip source · line 667 · raw
@xs:List<&2, F32> -> @+lo:F32 -> @+hi:F32 -> List<&2, F32>
def row.winsorize source · line 674 · raw
@+xs:List<&2, F32> -> @+q:F32 -> List<&2, F32>
def zscore.f source · line 684 · raw
@c:Cfg -> @x:List<&2, F32> -> List<&2, F32>
def demean.f source · line 687 · raw
@c:Cfg -> @x:List<&2, F32> -> List<&2, F32>
def rank.f source · line 690 · raw
@c:Cfg -> @x:List<&2, F32> -> List<&2, F32>
def winsorize.f source · line 694 · raw
@c:Cfg -> @x:List<&2, F32> -> List<&2, F32>
def zscore source · line 699 · raw
@p:Panel -> Panel
Each date: (x - mean) / sd across names.
def demean source · line 704 · raw
@p:Panel -> Panel
Each date: x - mean across names.
def rank source · line 709 · raw
@p:Panel -> Panel
Each date: percentile rank across names, in (0, 1].
def winsorize source · line 714 · raw
@p:Panel -> @+q:F32 -> Panel
Each date: clip to that date's q and 1 - q quantiles.
def ew.one source · line 723 · raw
@+p:F32 -> @+v:F32 -> @+a:F32 -> F32
def ew.row source · line 726 · raw
@ps:List<&2, F32> -> @xs:List<&2, F32> -> @+a:F32 -> List<&2, F32>
def ew.next source · line 735 · raw
@st:Ew -> @x:List<&2, F32> -> Ew
def ew.out source · line 739 · raw
@st:Ew -> @x:List<&2, F32> -> List<&2, F32>
def ewma source · line 745 · raw
@p:Panel -> @+a:F32 -> Panel
s_i = a x_i + (1 - a) s_(i-1) per name. A NaN carries the last value on, and a name is NaN until its first value, as pandas ewm(alpha=a, adjust=False, ignore_na=True).
def win.next source · line 752 · raw
@st:Win -> @x:List<&2, F32> -> Win
def col.cons source · line 759 · raw
@+s:F32 -> @+c:F32 -> @r:Cols -> Cols
def col.add source · line 763 · raw
@ss:List<&2, F32> -> @cs:List<&2, F32> -> @xs:List<&2, F32> -> Cols
def col.sum source · line 770 · raw
@rows:List<&2, List<&2, F32>> -> @acc:Cols -> Cols
def col.div source · line 778 · raw
@ss:List<&2, F32> -> @cs:List<&2, F32> -> List<&2, F32>
def win.out.of source · line 785 · raw
@r:Cols -> List<&2, F32>
def win.out source · line 789 · raw
@st:Win -> @x:List<&2, F32> -> List<&2, F32>
def rolling_mean source · line 797 · raw
@p:Panel -> @+w:Nat -> Panel
Mean over the last w dates per name, skipping NaN; NaN only when all w are missing, as pandas rolling(w, min_periods=1).mean(). ponytail: re-sums the window each date, O(w) per value; running sums if w gets large
def dot source · line 803 · raw
@xs:List<&2, F32> -> @ys:List<&2, F32> -> @+acc:F32 -> F32
def nw.sum source · line 813 · raw
@k:Nat -> @+j:Nat -> @+lags:Nat -> @+ds:List<&2, F32> -> @+tf:F32 -> @+acc:F32 -> F32
2 * sum over lags l = j..L of (1 - l / (L + 1)) * gamma_l, gamma_l = sum d_t d_(t-l) / T
def nw.of source · line 822 · raw
@few:Bool -> @+vs:List<&2, F32> -> @+lags:Nat -> Maybe<&2, F32>
def nw_t source · line 837 · raw
@+ics:List<&2, F32> -> @+lags:Nat -> Maybe<&2, F32>
t of the mean IC with a Newey-West (Bartlett) variance over lags lags, on the
non-NaN ICs. Use it when forward returns overlap: with horizon h, lags >= h.
lags = 0 is the plain t of summary, except the variance divides by T, not T - 1.
None with fewer than 2 non-NaN ICs.
Templates
template ord source · line 100 · raw
@-A:Data -> @-le:(@_:A -> @_:A -> Bool) -> @+a:A -> @+v:A -> Cmp
where a goes relative to v: EQ when tied, LT left, GT right
template root_ord source · line 103 · raw
@-A:Data -> @-le:(@_:A -> @_:A -> Bool) -> @t:Tr<A> -> @+a:A -> Cmp
template ins source · line 112 · raw
@-A:Data -> @-le:(@_:A -> @_:A -> Bool) -> @t:Tr<A> -> @+a:A -> @d:Cmp -> Tr<A>
d is root_ord(t, a), computed by the caller: a match can't branch on a computed value, and a helper can't call back into ins.
template insert source · line 123 · raw
@-A:Data -> @-le:(@_:A -> @_:A -> Bool) -> @+t:Tr<A> -> @+a:A -> Tr<A>
template build source · line 126 · raw
@-A:Data -> @-le:(@_:A -> @_:A -> Bool) -> @xs:List<&2, A> -> @t:Tr<A> -> Tr<A>
template root_lt source · line 133 · raw
@-A:Data -> @-le:(@_:A -> @_:A -> Bool) -> @t:Tr<A> -> @+q:A -> Bool
template root_le source · line 140 · raw
@-A:Data -> @-le:(@_:A -> @_:A -> Bool) -> @t:Tr<A> -> @+q:A -> Bool
template dlt source · line 148 · raw
@-A:Data -> @-le:(@_:A -> @_:A -> Bool) -> @t:Tr<A> -> @+q:A -> @d:Bool -> Nat
#{x in t : x < q}, with x < q meaning not le(q, x); d is root_lt(t, q)
template dle source · line 158 · raw
@-A:Data -> @-le:(@_:A -> @_:A -> Bool) -> @t:Tr<A> -> @+q:A -> @d:Bool -> Nat
#{x in t : x <= q}; d is root_le(t, q)
template tree source · line 197 · raw
@-A:Data -> @-le:(@_:A -> @_:A -> Bool) -> @+xs:List<&2, A> -> Tr<A>
template rank_of source · line 201 · raw
@-A:Data -> @-le:(@_:A -> @_:A -> Bool) -> @+t:Tr<A> -> @+q:A -> Nat
2 x the average 1-based rank of q among the values in t
template kle source · line 246 · raw
@-A:Data -> @-B:Data -> @-le:(@_:A -> @_:A -> Bool) -> @x:KV<A, B> -> @y:KV<A, B> -> Bool
template mrg source · line 250 · raw
@-A:Data -> @-B:Data -> @-le:(@_:A -> @_:A -> Bool) -> @xt:List<&2, KV<A, B>> -> @yt:List<&2, KV<A, B>> -> @+x:KV<A, B> -> @+y:KV<A, B> -> @d:Bool -> List<&2, KV<A, B>>
merge with the heads unpacked; d is kle(x, y)
template merge source · line 261 · raw
@-A:Data -> @-B:Data -> @-le:(@_:A -> @_:A -> Bool) -> @xs:List<&2, KV<A, B>> -> @ys:List<&2, KV<A, B>> -> List<&2, KV<A, B>>
template isort source · line 271 · raw
@-A:Data -> @-B:Data -> @-le:(@_:A -> @_:A -> Bool) -> @xs:List<&2, KV<A, B>> -> List<&2, KV<A, B>>
only runs when msorted's fuel is short, which msort never lets happen
template msorted source · line 278 · raw
@-A:Data -> @-B:Data -> @-le:(@_:A -> @_:A -> Bool) -> @k:Nat -> @two:Two<KV<A, B>> -> List<&2, KV<A, B>>
template msort source · line 287 · raw
@-A:Data -> @-B:Data -> @-le:(@_:A -> @_:A -> Bool) -> @+xs:List<&2, KV<A, B>> -> List<&2, KV<A, B>>
template nxt source · line 291 · raw
@-A:Data -> @-B:Data -> @-le:(@_:A -> @_:A -> Bool) -> @t:List<&2, KV<A, B>> -> @+v:A -> Bool
le(key of the head of t, v); False on an empty list
template runs source · line 307 · raw
@-A:Data -> @-B:Data -> @-le:(@_:A -> @_:A -> Bool) -> @ps:List<&2, KV<A, B>> -> @+v:A -> @+p:Nat -> @+c:Nat -> @ys:List<&2, B> -> @acc:List<&2, KV<B, Nat>> -> @d:Bool -> List<&2, KV<B, Nat>>
a list sorted by key in; (value, 2 x the average rank of its key) out. The current run has key v, c values ys, and p values before it.
template rk source · line 316 · raw
@-A:Data -> @-B:Data -> @-le:(@_:A -> @_:A -> Bool) -> @ps:List<&2, KV<A, B>> -> List<&2, KV<B, Nat>>
template step source · line 324 · raw
@-A:Data -> @-B:Data -> @-le:(@_:A -> @_:A -> Bool) -> @+ps:List<&2, KV<A, B>> -> List<&2, KV<B, Nat>>
sort by key and rank it
template rank2 source · line 328 · raw
@-A:Data -> @-le:(@_:A -> @_:A -> Bool) -> @+ps:List<&2, KV<A, A>> -> List<&2, KV<Nat, Nat>>
(x, y) pairs in, (2 x rank of x, 2 x rank of y) out, in some order
template pseq source · line 402 · raw
@-A:Data -> @-B:Data -> @-C:Data -> @-f:(@_:C -> @_:A -> B) -> @xs:List<&2, A> -> @+c:C -> List<&2, B>
f(c, a) for each element a, in order
template par source · line 427 · raw
@-A:Data -> @-B:Data -> @-C:Data -> @-f:(@_:C -> @_:A -> B) -> @+d:Nat -> @two:Two<A> -> @+h:U32 -> @+r:U32 -> @+c:C -> List<&2, B>
fork-join: 2^(d+1) tasks. h and r only balance the split; any values give the same list.
template pmap source · line 442 · raw
@-A:Data -> @-B:Data -> @-C:Data -> @-f:(@_:C -> @_:A -> B) -> @xs:List<&2, A> -> @+t:U32 -> @+c:C -> List<&2, B>
t is the length of xs, used only to balance the split. ponytail: fixed 64 tasks once t >= 64, 2 below; tune per machine if it matters
template scan source · line 574 · raw
@-S:Data -> @-next:(@_:S -> @_:List<&2, F32> -> S) -> @-out:(@_:S -> @_:List<&2, F32> -> List<&2, F32>) -> @xs:List<&2, List<&2, F32>> -> @+s:S -> List<&2, List<&2, F32>>
state S; next folds row i into it; out builds output row i from the new state and row i