import Base import ./window.bend as Window import ./rolling_average.bend as Average type Value<-N: Data> is Data: Moments{mean: N, variance: N} type Center<-N: Data> is Data: Center{origin: N, mean_delta: N} def delta(~N: Data, ~add: N -> N -> N, ~sub: N -> N -> N, origin: N, sum: N, price: N) -> N: add(sum, sub(price, origin)) def square(~N: Data, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, center: Center, sum: N, price: N) -> N: match center: case Center{origin, mean_delta}: +distance = sub(sub(price, origin), mean_delta) add(sum, mul(distance, distance)) def finish(~N: Data, ~div: N -> N -> N, mean: N, count: N, result: Window.State & N) -> Window.State & Maybe<&2, Value>: match result: case (window, sum): (window, Some{Moments{mean, div(sum, count)}}) def centered( ~N: Data, ~from_u32: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, +count: N, +origin: N, result: Window.State & N ) -> Window.State & Maybe<&2, Value>: match result: case (window, sum): +mean_delta = div(sum, count) finish(~N, ~div, add(origin, mean_delta), count, Window.fold(~N, ~Center, ~N, ~(c => acc => x => square(~N, ~add, ~sub, ~mul, c, acc, x)), window, Center{origin, mean_delta}, from_u32(0))) def origin( ~N: Data, ~from_u32: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, count: U32, result: Window.State & Maybe<&2, N> ) -> Window.State & Maybe<&2, Value>: match result: case (window, None{}): (window, None{}) case (window, Some{+anchor}): centered(~N, ~from_u32, ~add, ~sub, ~mul, ~div, from_u32(count), anchor, Window.fold(~N, ~N, ~N, ~(a => acc => x => delta(~N, ~add, ~sub, a, acc, x)), window, anchor, from_u32(0))) def counted( ~N: Data, ~from_u32: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, result: Window.State & U32 ) -> Window.State & Maybe<&2, Value>: match result: case (window, count): origin(~N, ~from_u32, ~add, ~sub, ~mul, ~div, count, Window.lag(~N, window, 0)) def ready( ~N: Data, ~from_u32: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, result: Window.State & Bool ) -> Window.State & Maybe<&2, Value>: match result: case (window, False{}): (window, None{}) case (window, True{}): counted(~N, ~from_u32, ~add, ~sub, ~mul, ~div, Window.length(~N, window)) def read( ~N: Data, ~from_u32: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, window: Window.State ) -> Window.State & Maybe<&2, Value>: ready(~N, ~from_u32, ~add, ~sub, ~mul, ~div, Window.full(~N, window)) def pushed( ~N: Data, ~from_u32: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, result: Window.State & Maybe<&2, N> ) -> Window.State & Maybe<&2, Value>: match result: case (window, old): read(~N, ~from_u32, ~add, ~sub, ~mul, ~div, window) def next( ~N: Data, ~from_u32: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, window: Window.State, price: N ) -> Window.State & Maybe<&2, Value>: pushed(~N, ~from_u32, ~add, ~sub, ~mul, ~div, Window.push(~N, window, price)) def energy.safe(~N: Data, ~from: U32 -> N, ~div: N -> N -> N, ~le: N -> N -> Bool, energy: N, count: U32) -> Bool: +minimum = div(div(div(div(from(1), from(1073741824)), from(1073741824)), from(1073741824)), from(4194304)) le(minimum, div(energy, from(count))) type Values<-N: Data> is Data: Values{origin: N, total: Average.Sum, squares: Average.Sum, peak: N, flat: Bool} type Cached<-N: Data> is Type: Cached{window: Window.State, cache: Maybe<&2, Values>} def cached.init(~N: Data, ~from: U32 -> N, +period: U32, depth: Nat) -> Cached: Cached{Window.init(~N, period, depth, from(0)), None{}} def cached.value(~N: Data, ~from: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, +period: U32, values: Values) -> Value: match values: case Values{origin, +total, squares, peak, flat}: +mean = div(Average.sum.value(~N, total), from(period)) Moments{add(origin, mean), div(sub(Average.sum.value(~N, squares), mul(Average.sum.value(~N, total), mean)), from(period))} def cached.valid(~N: Data, ~from: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, ~abs: N -> N, ~le: N -> N -> Bool, +period: U32, values: Values) -> Bool: match values: case Values{+origin, +total, squares, peak, flat}: +s = Average.sum.value(~N, total) +q = Average.sum.value(~N, squares) +m2 = sub(q, mul(s, div(s, from(period)))) +bounded = mul(q, from(16)) +mean = add(origin, div(s, from(period))) +stable_mean = Bool.and(le(abs(origin), mul(from(32), abs(mean))), le(div(q, from(period)), mul(from(1024), mul(mean, mean)))) +stable_energy = Bool.or(flat, energy.safe(~N, ~from, ~div, ~le, m2, period)) +stable_scale = Bool.and(le(sub(bounded, bounded), from(0)), Bool.and(le(q, mul(from(32), m2)), le(div(peak, from(65536)), q))) Bool.and(stable_mean, Bool.and(stable_energy, stable_scale)) def cached.step(~N: Data, ~from: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~le: N -> N -> Bool, +origin: N, values: Values, price: N) -> Values: match values: case Values{anchor, total, squares, peak, flat}: +d = sub(price, origin) Values{origin, Average.sum.add(~N, ~add, ~sub, total, d), Average.sum.add(~N, ~add, ~sub, squares, mul(d, d)), peak, Bool.and(flat, Bool.and(le(d, from(0)), le(from(0), d)))} def cached.fresh(~N: Data, cache: Maybe<&2, Values>, result: Window.State & Maybe<&2, Value>) -> Cached & Maybe<&2, Value>: match result: case (window, value): (Cached{window, cache}, value) def cached.accepted(~N: Data, ~from: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, +period: U32, window: Window.State, +values: Values, valid: Bool) -> Cached & Maybe<&2, Value>: match valid: case True{}: (Cached{window, Some{values}}, Some{cached.value(~N, ~from, ~add, ~sub, ~mul, ~div, period, values)}) case False{}: cached.fresh(~N, None{}, read(~N, ~from, ~add, ~sub, ~mul, ~div, window)) def cached.rebuilt(~N: Data, ~from: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, ~abs: N -> N, ~le: N -> N -> Bool, +period: U32, result: Window.State & Values) -> Cached & Maybe<&2, Value>: match result: case (window, Values{origin, total, +squares, peak, flat}): +values : Values = Values{origin, total, squares, Average.sum.value(~N, squares), flat} cached.accepted(~N, ~from, ~add, ~sub, ~mul, ~div, period, window, values, cached.valid(~N, ~from, ~add, ~sub, ~mul, ~div, ~abs, ~le, period, values)) def cached.anchor(~N: Data, ~from: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, ~abs: N -> N, ~le: N -> N -> Bool, +period: U32, result: Window.State & Maybe<&2, N>) -> Cached & Maybe<&2, Value>: match result: case (window, None{}): (Cached{window, None{}}, None{}) case (window, Some{+origin}): +zero = from(0) cached.rebuilt(~N, ~from, ~add, ~sub, ~mul, ~div, ~abs, ~le, period, Window.fold(~N, ~N, ~Values, ~(o => a => x => cached.step(~N, ~from, ~add, ~sub, ~mul, ~le, o, a, x)), window, origin, Values{origin, Average.Sum{zero, zero}, Average.Sum{zero, zero}, zero, True{}})) def cached.rebuild(~N: Data, ~from: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, ~abs: N -> N, ~le: N -> N -> Bool, +period: U32, window: Window.State) -> Cached & Maybe<&2, Value>: cached.anchor(~N, ~from, ~add, ~sub, ~mul, ~div, ~abs, ~le, period, Window.lag(~N, window, 0)) def cached.changed(~N: Data, ~from: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, ~abs: N -> N, ~le: N -> N -> Bool, +period: U32, window: Window.State, +values: Values, valid: Bool) -> Cached & Maybe<&2, Value>: match valid: case True{}: (Cached{window, Some{values}}, Some{cached.value(~N, ~from, ~add, ~sub, ~mul, ~div, period, values)}) case False{}: cached.rebuild(~N, ~from, ~add, ~sub, ~mul, ~div, ~abs, ~le, period, window) def cached.update(~N: Data, ~from: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, ~abs: N -> N, ~le: N -> N -> Bool, +period: U32, window: Window.State, values: Values, old: N, price: N) -> Cached & Maybe<&2, Value>: match values: case Values{+origin, total, squares, +peak, flat}: +before = sub(old, origin) +after = sub(price, origin) +next = Average.sum.change(~N, ~from, ~add, ~sub, squares, mul(before, before), mul(after, after)) +changed : Values = Values{origin, Average.sum.change(~N, ~from, ~add, ~sub, total, before, after), next, Bool.pick(N, le(Average.sum.value(~N, next), peak), peak, Average.sum.value(~N, next)), Bool.and(flat, Bool.and(le(after, from(0)), le(from(0), after)))} cached.changed(~N, ~from, ~add, ~sub, ~mul, ~div, ~abs, ~le, period, window, changed, cached.valid(~N, ~from, ~add, ~sub, ~mul, ~div, ~abs, ~le, period, changed)) def cached.ready(~N: Data, ~from: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, ~abs: N -> N, ~le: N -> N -> Bool, +period: U32, window: Window.State, cache: Maybe<&2, Values>, old: Maybe<&2, N>, price: N, full: Bool, cycle: Bool) -> Cached & Maybe<&2, Value>: match cache old full cycle: case _ _ False{} _: (Cached{window, None{}}, None{}) case _ _ True{} True{}: cached.rebuild(~N, ~from, ~add, ~sub, ~mul, ~div, ~abs, ~le, period, window) case Some{values} Some{removed} True{} False{}: cached.update(~N, ~from, ~add, ~sub, ~mul, ~div, ~abs, ~le, period, window, values, removed, price) case _ _ True{} False{}: cached.fresh(~N, None{}, read(~N, ~from, ~add, ~sub, ~mul, ~div, window)) def cached.pushed(~N: Data, ~from: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, ~abs: N -> N, ~le: N -> N -> Bool, cache: Maybe<&2, Values>, price: N, result: Window.State & Maybe<&2, N>) -> Cached & Maybe<&2, Value>: match result: case (Window.Window{buffer, +period, +size, +pos}, old): cached.ready(~N, ~from, ~add, ~sub, ~mul, ~div, ~abs, ~le, period, Window.Window{buffer, period, size, pos}, cache, old, price, U32.is_eq(size, period), U32.is_zero(pos)) def cached.next(~N: Data, ~from: U32 -> N, ~add: N -> N -> N, ~sub: N -> N -> N, ~mul: N -> N -> N, ~div: N -> N -> N, ~abs: N -> N, ~le: N -> N -> Bool, state: Cached, +price: N) -> Cached & Maybe<&2, Value>: match state: case Cached{window, cache}: cached.pushed(~N, ~from, ~add, ~sub, ~mul, ~div, ~abs, ~le, cache, price, Window.push(~N, window, price))