pub struct Beta<T> { /* private fields */ }Expand description
Beta distribution with shape parameters α and β on [0, 1].
f(x) = x^{α−1} (1−x)^{β−1} / B(α, β) for 0 ≤ x ≤ 1.
§Example
use numeris::stats::{Beta, ContinuousDistribution};
let b = Beta::new(2.0_f64, 5.0).unwrap();
assert!((b.mean() - 2.0/7.0).abs() < 1e-14);Implementations§
Source§impl<T: FloatScalar> Beta<T>
impl<T: FloatScalar> Beta<T>
Sourcepub fn new(alpha: T, beta: T) -> Result<Self, StatsError>
pub fn new(alpha: T, beta: T) -> Result<Self, StatsError>
Create a Beta distribution with shape parameters alpha and beta.
Requires both > 0.
Examples found in repository?
docs/examples/gen_plots.rs (line 568)
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> Beta<T>
impl<T: FloatScalar> Beta<T>
Trait Implementations§
Source§impl<T: FloatScalar> ContinuousDistribution<T> for Beta<T>
impl<T: FloatScalar> ContinuousDistribution<T> for Beta<T>
impl<T: Copy> Copy for Beta<T>
Auto Trait Implementations§
impl<T> Freeze for Beta<T>where
T: Freeze,
impl<T> RefUnwindSafe for Beta<T>where
T: RefUnwindSafe,
impl<T> Send for Beta<T>where
T: Send,
impl<T> Sync for Beta<T>where
T: Sync,
impl<T> Unpin for Beta<T>where
T: Unpin,
impl<T> UnsafeUnpin for Beta<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Beta<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