Blyx CUSUM logo # Blyx CUSUM Detect persistent shifts in a sequence of standardized observations with a two-sided cumulative sum. A single residual can be noisy. Accumulating several modest deviations in the same direction can surface a shift that a pointwise limit misses. Feed the detector an ordered, standardized sequence and retain its three-number state between observations. ## Use it in a project Install [Blyx 0.2.1](https://github.com/Rahulchaube1/blyxxxx/releases/tag/v0.2.1), Rust 1.85 or newer, and a host linker for native compilation. From the directory containing your entry file, pin the library with Git. ```sh git clone --depth 1 --branch v0.1.0 https://github.com/Rahulchaube1/blyx-cusum.git vendor/cusum ``` Save this program as `main.blyx` beside `vendor` and run `blyxc native-run main.blyx`. ```blyx use vendor::cusum::cusum; fn main() { let mut state = cs_empty(); let observations = [2, 2, -3.5]; let mut i = 0; while i < len(observations) { state = cs_step(state, at(observations, i), 0.5, 3.0); println!(at(state, 2)); i += 1; } } ``` Expected output: ```text 0 1 -1 ``` [Run this example in your browser](https://www.blyx-lang.space/play?example=package-cusum) ## API and data contract | Function | Result | | --- | --- | | `cs_empty()` | Initial state [0, 0, 0]. | | `cs_step(state, z, allowance, limit)` | Updated upward magnitude, downward magnitude and signal. | For z, the upward sum is max(0, previous_up + z − allowance) and the downward sum is max(0, previous_down − z − allowance). The limit is inclusive. Signals are +1 for upward movement, −1 for downward movement and 0 otherwise. The observation following an alert starts a fresh episode. The example alerts on the second positive observation and again on the following negative observation. Use a nonnegative allowance and positive limit in standard-deviation units. Invalid values and arithmetic overflow return an empty vector. Set the limits with representative stable data and a false-alert budget. Autocorrelation affects how often the chart signals. [NIST CUSUM control charts](https://www.itl.nist.gov/div898/handbook/pmc/section3/pmc323.htm) ## Contribute Clone this repository, run `blyxc native-run tests.blyx` and `blyxc native-run example.blyx`, and include a failing example with any behavior change. Include the compiler version, inputs and expected output in bug reports. [Open a package issue](https://github.com/Rahulchaube1/blyx-cusum/issues) · [Blyx handbook](https://doc.blyx-lang.space/guide/training-and-monitoring) · [Ecosystem catalog](https://www.blyx-lang.space/packages)