8 releases (4 breaking)

✓ Uses Rust 2018 edition

0.5.1 Mar 24, 2020
0.4.0 Mar 12, 2020
0.3.0-beta.1 Dec 19, 2019
0.1.0 Aug 8, 2019

#16 in Machine learning

Download history 14/week @ 2019-12-10 26/week @ 2019-12-17 4/week @ 2019-12-24 4/week @ 2019-12-31 16/week @ 2020-01-07 20/week @ 2020-01-14 3/week @ 2020-01-21 15/week @ 2020-01-28 7/week @ 2020-02-04 8/week @ 2020-02-11 52/week @ 2020-02-18 48/week @ 2020-02-25 6/week @ 2020-03-03 48/week @ 2020-03-10 31/week @ 2020-03-17 56/week @ 2020-03-24

99 downloads per month

MIT license

620KB
641 lines

changepoint - Change point detection for Rust

Changepoint is a library for doing change point detection for streams of data.

Gitlab pipeline status (branch) Crates.io Crates.io docs.rs

Usage

To use changepoint, first add this to your Cargo.toml:

[dependencies]
changepoint = "0.1"

Algorithms

Includes the following change point detection algorithms:

  • BOCPD -- Online Bayesian Change Point Detection Reference.

Example

//! A demo of the online Bayesian change point detection on
//! the 3-month Treasury Bill Secondary Market Rate from
//!
//! After this example is run, the file `trasury_bill.ipynb` can be run to generate
//! plots for this dataset.
//!
//! > Board of Governors of the Federal Reserve System (US), 3-Month Treasury Bill: Secondary
//! > Market Rate [TB3MS], retrieved from FRED, Federal Reserve Bank of St. Louis;
//! > https://fred.stlouisfed.org/series/TB3MS, August 5, 2019.

use changepoint::{constant_hazard, utils, BocpdTruncated, MapPathDetector};
use rv::prelude::*;
use std::io;

fn main() -> io::Result<()> {
    // Parse the data from the TB3MS dataset
    let data: &str = include_str!("./resources/TB3MS.csv");
    let (dates, pct_change): (Vec<&str>, Vec<f64>) = data
        .lines()
        .skip(1)
        .map(|line| {
            let split: Vec<&str> = line.splitn(2, ',').collect();
            let date = split[0];
            let raw_pct = split[1];
            (date, raw_pct.parse::<f64>().unwrap())
        })
        .unzip();

    // Create the Bocpd processor
    let mut cpd = utils::MostLikelyPathWrapper::new(BocpdTruncated::new(
        constant_hazard(250.0),
        Gaussian::standard(),
        NormalGamma::new_unchecked(0.0, 1.0, 1.0, 1E-5),
    ));

    // Feed data into change point detector
    let res: Vec<Vec<f64>> = pct_change
        .iter()
        .map(|d| cpd.step(d).map_path_probs.clone().into())
        .collect();

    // Determine most likely change points
    let change_points: Vec<usize> =
        utils::ChangePointDetectionMethod::DropThreshold(0.1).detect(&res);
    let change_dates: Vec<&str> =
        change_points.iter().map(|&i| dates[i]).collect();

    // Write output for processing my `trasury_bill.ipynb`.
    utils::write_data_and_r(
        "treasury_bill_output",
        &pct_change,
        &res,
        &change_points,
    )?;

    println!("Most likely dates of changes = {:#?}", change_dates);

    Ok(())
}

To run this example, from the source root, run cargo run --example treasury_bill. The partner notebook can be used to generate the following plots:

Treasury Bill Plots

Dependencies

~4MB
~87K SLoC