pub struct Normal<T> { /* private fields */ }Expand description
Normal (Gaussian) distribution N(μ, σ²).
§Example
use numeris::stats::{Normal, ContinuousDistribution};
let n = Normal::new(0.0_f64, 1.0).unwrap();
assert!((n.cdf(0.0) - 0.5).abs() < 1e-14);
assert!((n.quantile(0.975) - 1.96).abs() < 0.01);Implementations§
Source§impl<T: FloatScalar> Normal<T>
impl<T: FloatScalar> Normal<T>
Sourcepub fn new(mu: T, sigma: T) -> Result<Self, StatsError>
pub fn new(mu: T, sigma: T) -> Result<Self, StatsError>
Create a normal distribution with mean mu and standard deviation sigma.
Requires sigma > 0.
Examples found in repository?
docs/examples/gen_plots.rs (line 513)
511fn make_normal_pdf_plot() -> String {
512 let dists: Vec<(Normal<f64>, &str)> = vec![
513 (Normal::new(0.0, 1.0).unwrap(), "μ=0, σ=1"),
514 (Normal::new(0.0, 2.0).unwrap(), "μ=0, σ=2"),
515 (Normal::new(2.0, 0.7).unwrap(), "μ=2, σ=0.7"),
516 ];
517
518 const N: usize = 300;
519 let x_min = -6.0_f64;
520 let x_max = 6.0_f64;
521 let mut xv: Vec<f64> = (0..N)
522 .map(|i| x_min + (x_max - x_min) * i as f64 / (N - 1) as f64)
523 .collect();
524 // ensure exact 0
525 xv[N / 2] = 0.0;
526
527 let traces: Vec<String> = dists
528 .iter()
529 .map(|(d, name)| {
530 let yv: Vec<f64> = xv.iter().map(|&x| d.pdf(x)).collect();
531 line_trace(name, &xv, &yv)
532 })
533 .collect();
534
535 let layout = decorate_layout("Normal Distribution — PDF", "x", "f(x)", "");
536 plotly_snippet("plot-normal-pdf", &format!("[{}]", traces.join(",")), &layout, 400)
537}
538
539fn make_gamma_pdf_plot() -> String {
540 let dists: Vec<(Gamma<f64>, &str)> = vec![
541 (Gamma::new(1.0, 1.0).unwrap(), "α=1, β=1 (Exp)"),
542 (Gamma::new(2.0, 1.0).unwrap(), "α=2, β=1"),
543 (Gamma::new(5.0, 1.0).unwrap(), "α=5, β=1"),
544 (Gamma::new(2.0, 2.0).unwrap(), "α=2, β=2"),
545 ];
546
547 const N: usize = 300;
548 let x_min = 0.01_f64;
549 let x_max = 12.0_f64;
550 let xv: Vec<f64> = (0..N)
551 .map(|i| x_min + (x_max - x_min) * i as f64 / (N - 1) as f64)
552 .collect();
553
554 let traces: Vec<String> = dists
555 .iter()
556 .map(|(d, name)| {
557 let yv: Vec<f64> = xv.iter().map(|&x| d.pdf(x)).collect();
558 line_trace(name, &xv, &yv)
559 })
560 .collect();
561
562 let layout = decorate_layout("Gamma Distribution — PDF", "x", "f(x)", "");
563 plotly_snippet("plot-gamma-pdf", &format!("[{}]", traces.join(",")), &layout, 400)
564}
565
566fn make_beta_pdf_plot() -> String {
567 let dists: Vec<(Beta<f64>, &str)> = vec![
568 (Beta::new(0.5, 0.5).unwrap(), "α=0.5, β=0.5"),
569 (Beta::new(2.0, 2.0).unwrap(), "α=2, β=2"),
570 (Beta::new(2.0, 5.0).unwrap(), "α=2, β=5"),
571 (Beta::new(5.0, 2.0).unwrap(), "α=5, β=2"),
572 ];
573
574 const N: usize = 300;
575 let eps = 0.005;
576 let xv: Vec<f64> = (0..N)
577 .map(|i| eps + (1.0 - 2.0 * eps) * i as f64 / (N - 1) as f64)
578 .collect();
579
580 let traces: Vec<String> = dists
581 .iter()
582 .map(|(d, name)| {
583 let yv: Vec<f64> = xv.iter().map(|&x| d.pdf(x).min(8.0)).collect();
584 line_trace(name, &xv, &yv)
585 })
586 .collect();
587
588 let layout = decorate_layout_ex(
589 "Beta Distribution — PDF",
590 "x",
591 "",
592 "f(x)",
593 ",\"range\":[0,4.5]",
594 "",
595 );
596 plotly_snippet("plot-beta-pdf", &format!("[{}]", traces.join(",")), &layout, 400)
597}
598
599// ─── Stats: discrete PMF plots ───────────────────────────────────────────
600
601fn make_binomial_pmf_plot() -> String {
602 let dists: Vec<(Binomial<f64>, &str)> = vec![
603 (Binomial::new(10, 0.3).unwrap(), "n=10, p=0.3"),
604 (Binomial::new(10, 0.5).unwrap(), "n=10, p=0.5"),
605 (Binomial::new(20, 0.7).unwrap(), "n=20, p=0.7"),
606 ];
607
608 let k_max = 21u64;
609 let kv: Vec<f64> = (0..=k_max).map(|k| k as f64).collect();
610
611 let traces: Vec<String> = dists
612 .iter()
613 .map(|(d, name)| {
614 let yv: Vec<f64> = (0..=k_max).map(|k| d.pmf(k)).collect();
615 bar_trace(name, &kv, &yv)
616 })
617 .collect();
618
619 let layout = decorate_layout_ex(
620 "Binomial Distribution — PMF",
621 "k",
622 "",
623 "P(X = k)",
624 "",
625 ",\"barmode\":\"group\"",
626 );
627 plotly_snippet("plot-binomial-pmf", &format!("[{}]", traces.join(",")), &layout, 400)
628}
629
630fn make_poisson_pmf_plot() -> String {
631 let dists: Vec<(Poisson<f64>, &str)> = vec![
632 (Poisson::new(1.0).unwrap(), "λ = 1"),
633 (Poisson::new(4.0).unwrap(), "λ = 4"),
634 (Poisson::new(10.0).unwrap(), "λ = 10"),
635 ];
636
637 let k_max = 20u64;
638 let kv: Vec<f64> = (0..=k_max).map(|k| k as f64).collect();
639
640 let traces: Vec<String> = dists
641 .iter()
642 .map(|(d, name)| {
643 let yv: Vec<f64> = (0..=k_max).map(|k| d.pmf(k)).collect();
644 bar_trace(name, &kv, &yv)
645 })
646 .collect();
647
648 let layout = decorate_layout_ex(
649 "Poisson Distribution — PMF",
650 "k",
651 "",
652 "P(X = k)",
653 "",
654 ",\"barmode\":\"group\"",
655 );
656 plotly_snippet("plot-poisson-pmf", &format!("[{}]", traces.join(",")), &layout, 400)
657}
658
659fn make_continuous_cdf_plot() -> String {
660 let normal = Normal::new(0.0, 1.0).unwrap();
661 let gamma = Gamma::new(3.0, 1.0).unwrap();
662 let beta = Beta::new(2.0, 5.0).unwrap();
663
664 const N: usize = 300;
665
666 // Normal CDF on [-4, 4]
667 let xn: Vec<f64> = (0..N)
668 .map(|i| -4.0 + 8.0 * i as f64 / (N - 1) as f64)
669 .collect();
670 let yn: Vec<f64> = xn.iter().map(|&x| normal.cdf(x)).collect();
671
672 // Gamma CDF on [0, 10]
673 let xg: Vec<f64> = (0..N)
674 .map(|i| 0.01 + 10.0 * i as f64 / (N - 1) as f64)
675 .collect();
676 let yg: Vec<f64> = xg.iter().map(|&x| gamma.cdf(x)).collect();
677
678 // Beta CDF on [0, 1]
679 let xb: Vec<f64> = (0..N)
680 .map(|i| 0.005 + 0.99 * i as f64 / (N - 1) as f64)
681 .collect();
682 let yb: Vec<f64> = xb.iter().map(|&x| beta.cdf(x)).collect();
683
684 let traces = format!(
685 "[{},{},{}]",
686 line_trace("Normal(0, 1)", &xn, &yn),
687 line_trace("Gamma(3, 1)", &xg, &yg),
688 line_trace("Beta(2, 5)", &xb, &yb),
689 );
690
691 let layout = decorate_layout(
692 "Continuous Distributions — CDF",
693 "x",
694 "F(x)",
695 "",
696 );
697 plotly_snippet("plot-continuous-cdf", &traces, &layout, 400)
698}Source§impl<T: FloatScalar> Normal<T>
impl<T: FloatScalar> Normal<T>
Sourcepub fn sample(&self, rng: &mut Rng) -> T
pub fn sample(&self, rng: &mut Rng) -> T
Draw a random sample from this distribution.
§Example
use numeris::stats::{Normal, Rng};
let n = Normal::new(0.0_f64, 1.0).unwrap();
let mut rng = Rng::new(42);
let x = n.sample(&mut rng);
// x is drawn from N(0, 1)Sourcepub fn sample_array<const K: usize>(&self, rng: &mut Rng) -> [T; K]
pub fn sample_array<const K: usize>(&self, rng: &mut Rng) -> [T; K]
Fill a fixed-size array with independent samples.
Trait Implementations§
Source§impl<T: FloatScalar> ContinuousDistribution<T> for Normal<T>
impl<T: FloatScalar> ContinuousDistribution<T> for Normal<T>
impl<T: Copy> Copy for Normal<T>
Auto Trait Implementations§
impl<T> Freeze for Normal<T>where
T: Freeze,
impl<T> RefUnwindSafe for Normal<T>where
T: RefUnwindSafe,
impl<T> Send for Normal<T>where
T: Send,
impl<T> Sync for Normal<T>where
T: Sync,
impl<T> Unpin for Normal<T>where
T: Unpin,
impl<T> UnsafeUnpin for Normal<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Normal<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more