ChiSquare

Struct ChiSquare 

Source
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

Source

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]);
Source

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.

Source

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ยง

Sourceยง

impl Display for ChiSquare

Formatter for Displaying the Chi-Square Distribution

This implementation enables the display of Chi-Square distributions in macros like print!.

ยงArguments

  • f - The formatter to write the formatted string to.

ยงReturns

Returns core::fmt::Result.

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementationsยง

Blanket Implementationsยง

Sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T> ToString for T
where T: Display + ?Sized,

Sourceยง

fn to_string(&self) -> String

Converts the given value to a String. Read more
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.