bicmath-statistics 0.1.0

Descriptive statistics, distributions, inference, and experiment analysis for BicMath.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//! Probability distributions: normal, Student's t, binomial, and chi-square.
//!
//! All functions produce float64 results and require auto or scientific mode.
//! The special functions themselves live in [`crate::mathfn`]; the upper tails
//! are computed from `erfc`/regularized incomplete gamma or beta functions so
//! they keep relative accuracy far from the center.

use std::sync::Arc;

use bicmath_core::context::ExecContext;
use bicmath_core::contract::{
    Args, CostClass, Example, FunctionDescriptor, Outcome, ParamDescriptor, SimpleFunction,
    require_mode,
};
use bicmath_core::error::{EngineError, ErrorCode};
use bicmath_core::number::Number;

use crate::common::*;
use crate::mathfn::{
    binomial_cdf, binomial_pmf, chi_square_cdf, chi_square_pdf, chi_square_quantile, chi_square_sf,
    normal_cdf, normal_pdf, normal_quantile, normal_sf, student_t_cdf, student_t_pdf,
    student_t_quantile, student_t_sf,
};

fn normal_parameters(args: &Args) -> Result<(f64, f64, f64), EngineError> {
    let x = scalar_f64(args, "x")?;
    let mean = optional_f64_param(args, "mean")?.unwrap_or(0.0);
    let sd = optional_f64_param(args, "sd")?.unwrap_or(1.0);
    Ok((x, mean, sd))
}

fn normal_params() -> Vec<ParamDescriptor> {
    vec![
        ParamDescriptor::required("x", "Quantile.", any_number_schema()),
        ParamDescriptor::optional("mean", "Mean; default 0.", any_number_schema()),
        ParamDescriptor::optional(
            "sd",
            "Standard deviation; must be > 0; default 1.",
            any_number_schema(),
        ),
    ]
}

fn normal_descriptor(
    id: &str,
    title: &str,
    summary: &str,
    description: &str,
) -> FunctionDescriptor {
    FunctionDescriptor::new(id, "statistics", "1.0.0", title, summary)
        .with_description(description)
        .with_parameters(normal_params())
        .with_output(float64_schema(), "Normal distribution value.")
        .with_modes(inferential_modes())
        .with_cost(CostClass::Constant)
        .with_method_ref(format!(
            "docs/methods/statistics.md#{}",
            id.rsplit('.').next().unwrap()
        ))
        .with_examples(vec![
            Example::new(
                "standard normal center",
                example_args(&[("x", serde_json::json!(0))]),
            )
            .with_value(parse_value(
                serde_json::json!({"kind": "float64", "value": "0.5"}),
            )),
        ])
}

fn normal_pdf_descriptor() -> FunctionDescriptor {
    let mut descriptor = normal_descriptor(
        "statistics.normal_pdf",
        "Normal probability density",
        "Probability density of the normal distribution.",
        "pdf(x) = exp(-0.5 * ((x - mean) / sd)^2) / (sd * sqrt(2 * pi)). The standard \
         deviation must be strictly positive.",
    );
    descriptor.examples = vec![
        Example::new(
            "zero standard deviation",
            example_args(&[("x", serde_json::json!(0)), ("sd", serde_json::json!(0))]),
        )
        .with_error(ErrorCode::DomainViolation),
    ];
    descriptor
}

fn normal_cdf_descriptor() -> FunctionDescriptor {
    normal_descriptor(
        "statistics.normal_cdf",
        "Normal cumulative distribution",
        "Cumulative distribution function of the normal distribution.",
        "cdf(x) = 0.5 * erfc(-(x - mean) / (sd * sqrt(2))), evaluated with a high-accuracy \
         complementary error function.",
    )
}

fn normal_sf_descriptor() -> FunctionDescriptor {
    normal_descriptor(
        "statistics.normal_sf",
        "Normal survival function",
        "Upper tail probability of the normal distribution.",
        "sf(x) = P(X > x) = 0.5 * erfc((x - mean) / (sd * sqrt(2))). The upper tail is \
         computed from erfc directly, never as 1 - cdf, so large positive x keeps relative \
         accuracy.",
    )
}

fn normal_quantile_descriptor() -> FunctionDescriptor {
    FunctionDescriptor::new(
        "statistics.normal_quantile",
        "statistics",
        "1.0.0",
        "Normal quantile",
        "Inverse normal CDF.",
    )
    .with_description(
        "Returns x such that cdf(x) = p, solved by bracket expansion, bisection, and Newton \
         polishing to a relative tolerance of 1e-15. p must be strictly between 0 and 1; \
         mean defaults to 0 and sd to 1 and must be positive.",
    )
    .with_parameters(vec![
        ParamDescriptor::required("p", "Probability in (0, 1).", any_number_schema()),
        ParamDescriptor::optional("mean", "Mean; default 0.", any_number_schema()),
        ParamDescriptor::optional(
            "sd",
            "Standard deviation; must be > 0; default 1.",
            any_number_schema(),
        ),
    ])
    .with_output(float64_schema(), "Normal quantile.")
    .with_modes(inferential_modes())
    .with_cost(CostClass::Iterative)
    .with_method_ref("docs/methods/statistics.md#normal_quantile")
    .with_examples(vec![
        Example::new("median", example_args(&[("p", serde_json::json!(0.5))])).with_value(
            parse_value(serde_json::json!({"kind": "float64", "value": "0"})),
        ),
    ])
}

fn invoke_normal_pdf(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.normal_pdf")?;
    let (x, mean, sd) = normal_parameters(args)?;
    Ok(Outcome::approximate(float_value(normal_pdf(x, mean, sd)?)?))
}

fn invoke_normal_cdf(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.normal_cdf")?;
    let (x, mean, sd) = normal_parameters(args)?;
    Ok(Outcome::approximate(float_value(normal_cdf(x, mean, sd)?)?))
}

fn invoke_normal_sf(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.normal_sf")?;
    let (x, mean, sd) = normal_parameters(args)?;
    Ok(Outcome::approximate(float_value(normal_sf(x, mean, sd)?)?))
}

fn invoke_normal_quantile(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.normal_quantile")?;
    let p = scalar_f64(args, "p")?;
    let mean = optional_f64_param(args, "mean")?.unwrap_or(0.0);
    let sd = optional_f64_param(args, "sd")?.unwrap_or(1.0);
    Ok(Outcome::approximate(float_value(normal_quantile(
        p, mean, sd,
    )?)?))
}

// ---------------------------------------------------------------------------
// Student's t
// ---------------------------------------------------------------------------

fn student_t_params() -> Vec<ParamDescriptor> {
    vec![
        ParamDescriptor::required("x", "Quantile.", any_number_schema()),
        ParamDescriptor::required(
            "df",
            "Degrees of freedom; must be > 0.",
            any_number_schema(),
        ),
    ]
}

fn student_t_descriptor(
    id: &str,
    title: &str,
    summary: &str,
    description: &str,
    example: Example,
) -> FunctionDescriptor {
    FunctionDescriptor::new(id, "statistics", "1.0.0", title, summary)
        .with_description(description)
        .with_parameters(student_t_params())
        .with_output(float64_schema(), "Student-t distribution value.")
        .with_modes(inferential_modes())
        .with_cost(CostClass::Constant)
        .with_method_ref(format!(
            "docs/methods/statistics.md#{}",
            id.rsplit('.').next().unwrap()
        ))
        .with_examples(vec![example])
}

fn student_t_pdf_descriptor() -> FunctionDescriptor {
    student_t_descriptor(
        "statistics.student_t_pdf",
        "Student-t probability density",
        "Probability density of Student's t distribution.",
        "pdf(x) = Gamma((df + 1) / 2) / (sqrt(df * pi) Gamma(df / 2)) * \
         (1 + x^2 / df)^(-(df + 1) / 2), computed with a Lanczos log-gamma.",
        Example::new(
            "zero degrees of freedom",
            example_args(&[("x", serde_json::json!(0)), ("df", serde_json::json!(0))]),
        )
        .with_error(ErrorCode::DomainViolation),
    )
}

fn student_t_cdf_descriptor() -> FunctionDescriptor {
    student_t_descriptor(
        "statistics.student_t_cdf",
        "Student-t cumulative distribution",
        "Cumulative distribution function of Student's t distribution.",
        "For x > 0, cdf(x) = 1 - 0.5 * I_y(df/2, 1/2) with y = df / (df + x^2); for x < 0 the \
         mirrored expression is used. I_y is the regularized incomplete beta function.",
        Example::new(
            "symmetric center",
            example_args(&[("x", serde_json::json!(0)), ("df", serde_json::json!(5))]),
        )
        .with_value(parse_value(
            serde_json::json!({"kind": "float64", "value": "0.5"}),
        )),
    )
}

fn student_t_sf_descriptor() -> FunctionDescriptor {
    student_t_descriptor(
        "statistics.student_t_sf",
        "Student-t survival function",
        "Upper tail probability of Student's t distribution.",
        "For x > 0, sf(x) = 0.5 * I_y(df/2, 1/2) with y = df / (df + x^2); the upper tail is \
         computed directly from the regularized incomplete beta function, not as 1 - cdf.",
        Example::new(
            "symmetric center",
            example_args(&[("x", serde_json::json!(0)), ("df", serde_json::json!(5))]),
        )
        .with_value(parse_value(
            serde_json::json!({"kind": "float64", "value": "0.5"}),
        )),
    )
}

fn student_t_quantile_descriptor() -> FunctionDescriptor {
    FunctionDescriptor::new(
        "statistics.student_t_quantile",
        "statistics",
        "1.0.0",
        "Student-t quantile",
        "Inverse Student-t CDF.",
    )
    .with_description(
        "Returns x such that cdf(x) = p, solved by bracket expansion, bisection, and Newton \
         polishing to a relative tolerance of 1e-15. p must be strictly between 0 and 1 and df \
         must be positive.",
    )
    .with_parameters(vec![
        ParamDescriptor::required("p", "Probability in (0, 1).", any_number_schema()),
        ParamDescriptor::required(
            "df",
            "Degrees of freedom; must be > 0.",
            any_number_schema(),
        ),
    ])
    .with_output(float64_schema(), "Student-t quantile.")
    .with_modes(inferential_modes())
    .with_cost(CostClass::Iterative)
    .with_method_ref("docs/methods/statistics.md#student_t_quantile")
    .with_examples(vec![
        Example::new(
            "median",
            example_args(&[("p", serde_json::json!(0.5)), ("df", serde_json::json!(5))]),
        )
        .with_value(parse_value(
            serde_json::json!({"kind": "float64", "value": "0"}),
        )),
    ])
}

fn invoke_student_t_pdf(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.student_t_pdf")?;
    let x = scalar_f64(args, "x")?;
    let df = scalar_f64(args, "df")?;
    Ok(Outcome::approximate(float_value(student_t_pdf(x, df)?)?))
}

fn invoke_student_t_cdf(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.student_t_cdf")?;
    let x = scalar_f64(args, "x")?;
    let df = scalar_f64(args, "df")?;
    Ok(Outcome::approximate(float_value(student_t_cdf(x, df)?)?))
}

fn invoke_student_t_sf(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.student_t_sf")?;
    let x = scalar_f64(args, "x")?;
    let df = scalar_f64(args, "df")?;
    Ok(Outcome::approximate(float_value(student_t_sf(x, df)?)?))
}

fn invoke_student_t_quantile(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.student_t_quantile")?;
    let p = scalar_f64(args, "p")?;
    let df = scalar_f64(args, "df")?;
    Ok(Outcome::approximate(float_value(student_t_quantile(
        p, df,
    )?)?))
}

// ---------------------------------------------------------------------------
// Binomial
// ---------------------------------------------------------------------------

fn binomial_parameters(args: &Args) -> Result<(f64, f64, f64), EngineError> {
    let k = args.integer("k")?;
    let n = args.integer("n")?;
    if n.sign() == num_bigint::Sign::Minus {
        return Err(
            EngineError::domain("n must be a non-negative integer").with_path("n".to_string())
        );
    }
    let p = scalar_f64(args, "p")?;
    if !(0.0..=1.0).contains(&p) {
        return Err(EngineError::domain("p must be in [0, 1]").with_path("p".to_string()));
    }
    let k = number_to_f64(&Number::Integer(k))?;
    let n = number_to_f64(&Number::Integer(n))?;
    Ok((k, n, p))
}

fn binomial_descriptor(
    id: &str,
    title: &str,
    summary: &str,
    description: &str,
    example: Example,
) -> FunctionDescriptor {
    FunctionDescriptor::new(id, "statistics", "1.0.0", title, summary)
        .with_description(description)
        .with_parameters(vec![
            ParamDescriptor::required("k", "Number of successes; integer.", integer_schema()),
            ParamDescriptor::required("n", "Number of trials; integer >= 0.", integer_schema()),
            ParamDescriptor::required("p", "Success probability in [0, 1].", any_number_schema()),
        ])
        .with_output(float64_schema(), "Binomial distribution value.")
        .with_modes(inferential_modes())
        .with_cost(CostClass::Constant)
        .with_method_ref(format!(
            "docs/methods/statistics.md#{}",
            id.rsplit('.').next().unwrap()
        ))
        .with_examples(vec![example])
}

fn binomial_pmf_descriptor() -> FunctionDescriptor {
    binomial_descriptor(
        "statistics.binomial_pmf",
        "Binomial probability mass",
        "Probability mass function of the binomial distribution.",
        "pmf(k) = C(n, k) p^k (1 - p)^(n - k), evaluated with a lgamma-based log-pmf. \
         k < 0 or k > n gives 0; p = 0 or p = 1 are handled exactly.",
        Example::new(
            "empty trial set",
            example_args(&[
                ("k", serde_json::json!(0)),
                ("n", serde_json::json!(0)),
                ("p", serde_json::json!(0.5)),
            ]),
        )
        .with_value(parse_value(
            serde_json::json!({"kind": "float64", "value": "1"}),
        )),
    )
}

fn binomial_cdf_descriptor() -> FunctionDescriptor {
    binomial_descriptor(
        "statistics.binomial_cdf",
        "Binomial cumulative distribution",
        "Cumulative distribution function of the binomial distribution.",
        "cdf(k) = P(X <= k) = I_(1-p)(n - k, k + 1), the regularized incomplete beta \
         function. k < 0 gives 0 and k >= n gives 1.",
        Example::new(
            "all trials succeed",
            example_args(&[
                ("k", serde_json::json!(10)),
                ("n", serde_json::json!(10)),
                ("p", serde_json::json!(0.5)),
            ]),
        )
        .with_value(parse_value(
            serde_json::json!({"kind": "float64", "value": "1"}),
        )),
    )
}

fn invoke_binomial_pmf(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.binomial_pmf")?;
    let (k, n, p) = binomial_parameters(args)?;
    Ok(Outcome::approximate(float_value(binomial_pmf(k, n, p)?)?))
}

fn invoke_binomial_cdf(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.binomial_cdf")?;
    let (k, n, p) = binomial_parameters(args)?;
    Ok(Outcome::approximate(float_value(binomial_cdf(k, n, p)?)?))
}

// ---------------------------------------------------------------------------
// Chi-square
// ---------------------------------------------------------------------------

fn chi_square_descriptor(
    id: &str,
    title: &str,
    summary: &str,
    description: &str,
    example: Example,
) -> FunctionDescriptor {
    FunctionDescriptor::new(id, "statistics", "1.0.0", title, summary)
        .with_description(description)
        .with_parameters(vec![
            ParamDescriptor::required("x", "Quantile.", any_number_schema()),
            ParamDescriptor::required(
                "df",
                "Degrees of freedom; must be > 0.",
                any_number_schema(),
            ),
        ])
        .with_output(float64_schema(), "Chi-square distribution value.")
        .with_modes(inferential_modes())
        .with_cost(CostClass::Constant)
        .with_method_ref(format!(
            "docs/methods/statistics.md#{}",
            id.rsplit('.').next().unwrap()
        ))
        .with_examples(vec![example])
}

fn chi_square_pdf_descriptor() -> FunctionDescriptor {
    chi_square_descriptor(
        "statistics.chi_square_pdf",
        "Chi-square probability density",
        "Probability density of the chi-square distribution.",
        "pdf(x) = x^(df/2 - 1) e^(-x/2) / (2^(df/2) Gamma(df/2)) for x >= 0, evaluated in log \
         space with a Lanczos log-gamma. For x < 0 the density is 0; at x = 0 the density is \
         unbounded for df < 2 and 0.5 for df = 2.",
        Example::new(
            "density at zero for df = 2",
            example_args(&[("x", serde_json::json!(0)), ("df", serde_json::json!(2))]),
        )
        .with_value(parse_value(
            serde_json::json!({"kind": "float64", "value": "0.5"}),
        )),
    )
}

fn chi_square_cdf_descriptor() -> FunctionDescriptor {
    chi_square_descriptor(
        "statistics.chi_square_cdf",
        "Chi-square cumulative distribution",
        "Cumulative distribution function of the chi-square distribution.",
        "cdf(x) = P(df/2, x/2), the regularized lower incomplete gamma function, evaluated \
         with the series for x < a + 1 and the continued fraction otherwise.",
        Example::new(
            "zero quantile",
            example_args(&[("x", serde_json::json!(0)), ("df", serde_json::json!(3))]),
        )
        .with_value(parse_value(
            serde_json::json!({"kind": "float64", "value": "0"}),
        )),
    )
}

fn chi_square_sf_descriptor() -> FunctionDescriptor {
    chi_square_descriptor(
        "statistics.chi_square_sf",
        "Chi-square survival function",
        "Upper tail probability of the chi-square distribution.",
        "sf(x) = Q(df/2, x/2), the regularized upper incomplete gamma function, computed \
         directly so the upper tail keeps relative accuracy.",
        Example::new(
            "zero quantile",
            example_args(&[("x", serde_json::json!(0)), ("df", serde_json::json!(3))]),
        )
        .with_value(parse_value(
            serde_json::json!({"kind": "float64", "value": "1"}),
        )),
    )
}

fn chi_square_quantile_descriptor() -> FunctionDescriptor {
    FunctionDescriptor::new(
        "statistics.chi_square_quantile",
        "statistics",
        "1.0.0",
        "Chi-square quantile",
        "Inverse chi-square CDF.",
    )
    .with_description(
        "Returns x such that cdf(x) = p, solved by bracket expansion, bisection, and Newton \
         polishing to a relative tolerance of 1e-15. p must be in [0, 1); p = 0 returns 0 and \
         p = 1 is rejected because the quantile is unbounded. df must be positive.",
    )
    .with_parameters(vec![
        ParamDescriptor::required("p", "Probability in [0, 1).", any_number_schema()),
        ParamDescriptor::required(
            "df",
            "Degrees of freedom; must be > 0.",
            any_number_schema(),
        ),
    ])
    .with_output(float64_schema(), "Chi-square quantile.")
    .with_modes(inferential_modes())
    .with_cost(CostClass::Iterative)
    .with_method_ref("docs/methods/statistics.md#chi_square_quantile")
    .with_examples(vec![
        Example::new(
            "zero probability",
            example_args(&[("p", serde_json::json!(0)), ("df", serde_json::json!(3))]),
        )
        .with_value(parse_value(
            serde_json::json!({"kind": "float64", "value": "0"}),
        )),
    ])
}

fn invoke_chi_square_pdf(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.chi_square_pdf")?;
    let x = scalar_f64(args, "x")?;
    let df = scalar_f64(args, "df")?;
    Ok(Outcome::approximate(float_value(chi_square_pdf(x, df)?)?))
}

fn invoke_chi_square_cdf(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.chi_square_cdf")?;
    let x = scalar_f64(args, "x")?;
    let df = scalar_f64(args, "df")?;
    Ok(Outcome::approximate(float_value(chi_square_cdf(x, df)?)?))
}

fn invoke_chi_square_sf(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.chi_square_sf")?;
    let x = scalar_f64(args, "x")?;
    let df = scalar_f64(args, "df")?;
    Ok(Outcome::approximate(float_value(chi_square_sf(x, df)?)?))
}

fn invoke_chi_square_quantile(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.chi_square_quantile")?;
    let p = scalar_f64(args, "p")?;
    let df = scalar_f64(args, "df")?;
    Ok(Outcome::approximate(float_value(chi_square_quantile(
        p, df,
    )?)?))
}

// ---------------------------------------------------------------------------
// Registration
// ---------------------------------------------------------------------------

pub fn functions() -> Vec<Arc<dyn bicmath_core::contract::Function>> {
    vec![
        SimpleFunction::arc(normal_pdf_descriptor(), invoke_normal_pdf),
        SimpleFunction::arc(normal_cdf_descriptor(), invoke_normal_cdf),
        SimpleFunction::arc(normal_sf_descriptor(), invoke_normal_sf),
        SimpleFunction::arc(normal_quantile_descriptor(), invoke_normal_quantile),
        SimpleFunction::arc(student_t_pdf_descriptor(), invoke_student_t_pdf),
        SimpleFunction::arc(student_t_cdf_descriptor(), invoke_student_t_cdf),
        SimpleFunction::arc(student_t_sf_descriptor(), invoke_student_t_sf),
        SimpleFunction::arc(student_t_quantile_descriptor(), invoke_student_t_quantile),
        SimpleFunction::arc(binomial_pmf_descriptor(), invoke_binomial_pmf),
        SimpleFunction::arc(binomial_cdf_descriptor(), invoke_binomial_cdf),
        SimpleFunction::arc(chi_square_pdf_descriptor(), invoke_chi_square_pdf),
        SimpleFunction::arc(chi_square_cdf_descriptor(), invoke_chi_square_cdf),
        SimpleFunction::arc(chi_square_sf_descriptor(), invoke_chi_square_sf),
        SimpleFunction::arc(chi_square_quantile_descriptor(), invoke_chi_square_quantile),
    ]
}

#[cfg(test)]
mod tests {
    use super::*;
    use bicmath_core::value::Value;
    use std::collections::BTreeMap;

    fn call(id: &str, raw: serde_json::Value) -> Result<Outcome, EngineError> {
        let module = crate::module();
        let function = module
            .functions
            .iter()
            .find(|f| f.descriptor().id == id)
            .expect("function exists");
        let ctx = ExecContext::scientific();
        let args_json = raw.as_object().expect("object args");
        let mut values = BTreeMap::new();
        for (name, value) in args_json {
            let param = function
                .descriptor()
                .parameter(name)
                .expect("parameter exists");
            values.insert(
                name.clone(),
                param
                    .schema
                    .coerce(value, name, &ctx.limits, true)
                    .expect("argument coerces"),
            );
        }
        function.invoke(&Args::new(values), &ctx)
    }

    fn as_f64(outcome: &Outcome) -> f64 {
        match &outcome.value {
            Value::Number(number) => number.to_f64().expect("number"),
            other => panic!("expected number, got {other:?}"),
        }
    }

    fn close(actual: f64, expected: f64, tolerance: f64) {
        assert!(
            (actual - expected).abs() <= tolerance,
            "expected {expected}, got {actual} (tolerance {tolerance})"
        );
    }

    #[test]
    fn published_reference_values() {
        close(
            as_f64(&call("statistics.normal_cdf", serde_json::json!({"x": 1.96})).unwrap()),
            0.975_002_104_851_779_5,
            1e-12,
        );
        close(
            as_f64(
                &call(
                    "statistics.normal_quantile",
                    serde_json::json!({"p": 0.975}),
                )
                .unwrap(),
            ),
            1.959_963_984_540_054,
            1e-9,
        );
        close(
            as_f64(
                &call(
                    "statistics.student_t_cdf",
                    serde_json::json!({"x": 2, "df": 10}),
                )
                .unwrap(),
            ),
            0.963_305_982_614_629_8,
            1e-12,
        );
        close(
            as_f64(
                &call(
                    "statistics.chi_square_cdf",
                    serde_json::json!({"x": 3.841458820694124, "df": 1}),
                )
                .unwrap(),
            ),
            0.95,
            1e-12,
        );
        close(
            as_f64(
                &call(
                    "statistics.binomial_cdf",
                    serde_json::json!({"k": 5, "n": 10, "p": 0.5}),
                )
                .unwrap(),
            ),
            0.623_046_875,
            1e-15,
        );
        let tail = as_f64(&call("statistics.normal_sf", serde_json::json!({"x": 10})).unwrap());
        close(tail / 7.619_853_024_160_593e-24, 1.0, 1e-9);
    }
}