statrs-dev / statrs

Statistical computation library for Rust

Home Page:https://docs.rs/statrs/latest/statrs/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

StudentsT inverse_cdf is broken

scullionw opened this issue · comments

It is currently only valid for ranges .25-0.5 and .75-1, which is probably why the tests don't catch it.

use statrs::distribution::{ContinuousCDF, StudentsT};

fn t_ppf(q: f64, freedom: f64, location: f64, scale: f64) -> f64 {
    StudentsT::new(location, scale, freedom)
        .unwrap()
        .inverse_cdf(q)
}

fn main() {
    let mut x = 0.01;

    while x < 1.0 {
        let y = t_ppf(x, 2.74, 0.0, 1.0);
        debug_plotter::plot!((x, y));
        x += 0.01
    }
}

src-main rs:14:9

it should look like this:

Screen Shot 2021-11-25 at 3 48 12 PM

I currently fix it in my code with

fn t_ppf(q: f64, freedom: f64, location: f64, scale: f64) -> f64 {
    let candidate = StudentsT::new(location, scale, freedom)
        .unwrap()
        .inverse_cdf(q);

    if q < 0.25 {
        candidate * -1.0
    } else if q < 0.5 {
        candidate
    } else if q < 0.75 {
        candidate * -1.0
    } else {
        candidate
    }
}

I create a pull request with a fix: #159.