FDistribution

Struct FDistribution 

Source
pub struct FDistribution { /* private fields */ }
Expand description

F-distribution

§Usage Example

// Create a new FDistribution instance with the provided seeds.
// These seeds are used to initialize the internal state variables necessary for the random number generation.
let mut f = rand_simple::FDistribution::new([1192_u32, 765_u32, 1543_u32, 2003_u32, 1192_u32, 765_u32, 1543_u32, 2003_u32]);

// Check that the initial degrees of freedom parameters are correctly set to (1, 1).
// This ensures that the F-distribution is initialized with the default settings.
assert_eq!(format!("{f}"), "F(Degree of Freedom parameter 1, Degree of Freedom parameter 2) = F(1, 1)");

// Generate and print a random number from the F-distribution with the initial degrees of freedom (1, 1).
// The `sample()` method is used to produce this random number.
println!("With the initial setup, generates a random number following the F-distribution with degrees of freedom (1, 1) -> {}", f.sample());

// Modify the degrees of freedom parameters.
// Here, `degree_of_freedom_1` is set to 2 and `degree_of_freedom_2` is set to 3, changing the shape of the distribution.
let degree_of_freedom_1: u64 = 2_u64;  // New degrees of freedom for the first parameter.
let degree_of_freedom_2: u64 = 3_u64;  // New degrees of freedom for the second parameter.

// Attempt to update the F-distribution's parameters using the new degrees of freedom.
// The `try_set_params` method returns a `Result`, allowing us to handle any potential errors during this update.
let result: Result<(u64, u64), &str> = f.try_set_params(degree_of_freedom_1, degree_of_freedom_2);

// Verify that the degrees of freedom have been successfully updated to (2, 3).
// This assertion ensures that the distribution is now set up with the new parameters.
assert_eq!(format!("{f}"), "F(Degree of Freedom parameter 1, Degree of Freedom parameter 2) = F(2, 3)");

// Generate and print a random number from the F-distribution with the updated degrees of freedom (2, 3).
// The `sample()` method now reflects the updated distribution parameters.
println!("Generates a random number with the updated degrees of freedom ({}, {}) -> {}", degree_of_freedom_1, degree_of_freedom_2, f.sample());

Implementations§

Source§

impl FDistribution

Source

pub fn new(seeds: [u32; 8]) -> Self

Constructor for the FDistribution struct. This function initializes a new instance of the FDistribution with given seeds.

§Arguments
  • seeds - An array of 8 unsigned 32-bit integers (u32). These serve as the seeds for the random number generators that will be used within the distribution. The seeds are critical for ensuring the randomness of the generated numbers.
§Returns

Returns an instance of the FDistribution struct initialized with the provided seeds.

Source

pub fn sample(&mut self) -> f64

Generates a random number that follows the F-distribution.

This method implements the algorithm to compute a random variable that follows the F-distribution by generating two chi-squared distributed random variables and then computing their ratio.

§Returns

Returns a f64 value that represents a random sample from the F-distribution.

Source

pub fn try_set_params( &mut self, degree_of_freedom_1: u64, degree_of_freedom_2: u64, ) -> Result<(u64, u64), &str>

Attempts to set new parameters for the F-distribution’s degrees of freedom. This function validates the provided degrees of freedom and updates the struct’s internal state if the validation passes. If the validation fails, it returns an error message, and the parameters remain unchanged.

§Arguments
  • degree_of_freedom_1 - The first degree of freedom, associated with the numerator in the F-distribution.
  • degree_of_freedom_2 - The second degree of freedom, associated with the denominator in the F-distribution.
§Returns

A Result type:

  • Ok((u64, u64)) - Returns a tuple containing the updated degrees of freedom if the parameters are valid.
  • Err(&str) - Returns an error message if either degree of freedom is invalid (i.e., less than 1).

Trait Implementations§

Source§

impl Display for FDistribution

Implementing the Display trait for the FDistribution struct This allows instances of FDistribution to be formatted as a string, making it easier to print and debug the object.

Source§

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

Formats the FDistribution instance for display purposes.

§Arguments
  • f - A mutable reference to a core::fmt::Formatter that handles the formatting output.
§Returns

This function returns a core::fmt::Result, indicating whether the formatting was successful.

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.