criterion/plot/plotters_backend/
t_test.rs

1use super::*;
2use std::path::Path;
3
4pub(crate) fn t_test(
5    path: &Path,
6    title: &str,
7    comparison: &ComparisonData,
8    size: Option<(u32, u32)>,
9) {
10    let t = comparison.t_value;
11    let (xs, ys) = kde::sweep(&comparison.t_distribution, KDE_POINTS, None);
12
13    let x_range = plotters::data::fitting_range(xs.iter());
14    let mut y_range = plotters::data::fitting_range(ys.iter());
15    y_range.start = 0.0;
16    y_range.end *= 1.1;
17
18    let root_area = SVGBackend::new(&path, size.unwrap_or(SIZE)).into_drawing_area();
19
20    let mut chart = ChartBuilder::on(&root_area)
21        .margin((5).percent())
22        .caption(format!("{}: Welch t test", title), (DEFAULT_FONT, 20))
23        .set_label_area_size(LabelAreaPosition::Left, (5).percent_width().min(60))
24        .set_label_area_size(LabelAreaPosition::Bottom, (5).percent_height().min(40))
25        .build_cartesian_2d(x_range, y_range.clone())
26        .unwrap();
27
28    chart
29        .configure_mesh()
30        .disable_mesh()
31        .y_desc("Density")
32        .x_desc("t score")
33        .draw()
34        .unwrap();
35
36    chart
37        .draw_series(AreaSeries::new(
38            xs.iter().zip(ys.iter()).map(|(x, y)| (*x, *y)),
39            0.0,
40            DARK_BLUE.mix(0.25),
41        ))
42        .unwrap()
43        .label("t distribution")
44        .legend(|(x, y)| {
45            Rectangle::new([(x, y - 5), (x + 20, y + 5)], DARK_BLUE.mix(0.25).filled())
46        });
47
48    chart
49        .draw_series(std::iter::once(PathElement::new(
50            vec![(t, 0.0), (t, y_range.end)],
51            DARK_BLUE.filled().stroke_width(2),
52        )))
53        .unwrap()
54        .label("t statistic")
55        .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], DARK_BLUE));
56
57    chart.configure_series_labels().draw().unwrap();
58}