pub struct ChiSquare { /* private fields */ }Expand description
Chi-Square Distribution
The Chi-Square distribution represents the distribution of the sum of squares of ๐ independent standard normal random variables.
ยงExamples
// Create a new Chi-Square distribution with specified seeds
let mut chi_square = rand_simple::ChiSquare::new([1192_u32, 765_u32, 1543_u32, 2003_u32]);
// Output the string representation of the distribution
assert_eq!(format!("{chi_square}"), "ฯ^2(Degree of Freedom parameter) = ฯ^2(1)");
// Generate a random number following the distribution with initial settings
println!("For initial settings, returns a random number following Chi-Square distribution with 1 degree of freedom -> {}", chi_square.sample());
// Change the parameters of the random variable
let degree_of_freedom: u64 = 2_u64;
let result: Result<u64, &str> = chi_square.try_set_params(degree_of_freedom);
// Output the updated string representation of the distribution
assert_eq!(format!("{chi_square}"), "ฯ^2(Degree of Freedom parameter) = ฯ^2(2)");
// Generate a random number following the distribution with updated parameters
println!("Generates a random number following Chi-Square distribution with {} degrees of freedom -> {}", degree_of_freedom, chi_square.sample());Implementationsยง
Sourceยงimpl ChiSquare
impl ChiSquare
Sourcepub fn new(seeds: [u32; 4]) -> Self
pub fn new(seeds: [u32; 4]) -> Self
Chi-Square Distribution Constructor
This constructor initializes a new instance of the Chi-Square distribution with the given seeds for random number generation.
_seed- Seeds for random number generation
ยงExamples
// Create a new Chi-Square distribution with specified seeds
let mut chi_square = rand_simple::ChiSquare::new([1192_u32, 765_u32, 1543_u32, 2003_u32]);Sourcepub fn sample(&mut self) -> f64
pub fn sample(&mut self) -> f64
Generates a Random Number
This method generates a random number based on the Chi-Square distribution algorithm.
Algorithm 3.79:
- When the degree of freedom is 2, the distribution becomes an Exponential distribution Exp(2).
- For r > 1, it generates a random number X following the Gamma distribution ฮ(r_div2, 2).
- For r = 1, it generates a random number Y following the Gamma distribution ฮ(3/2, 2), then generates a uniform random number U in the interval (0, 1), and finally calculates X = 2YU^2.
ยงReturns
The generated random number.
Sourcepub fn try_set_params(&mut self, degree_of_freedom: u64) -> Result<u64, &str>
pub fn try_set_params(&mut self, degree_of_freedom: u64) -> Result<u64, &str>
Sets the Parameters of the Probability Variable
This method allows for changing the parameters of the Chi-Square distribution.
ยงArguments
degree_of_freedom- The degree of freedom (r) parameter of the distribution.
ยงReturns
Returns Ok(degree_of_freedom) if successful, else returns an error message.
Trait Implementationsยง
Auto Trait Implementationsยง
impl Freeze for ChiSquare
impl RefUnwindSafe for ChiSquare
impl Send for ChiSquare
impl Sync for ChiSquare
impl Unpin for ChiSquare
impl UnwindSafe for ChiSquare
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