Python Scipy stats.halfgennorm.rvs() Function

Scipy Library in Python:

  • SciPy is a scientific computation package that uses the NumPy library underneath.
  • SciPy is an abbreviation for Scientific Python.
  • It includes additional utility functions for optimization, statistics, and signal processing.
  • SciPy, like NumPy, is open source, so we can freely use it.
  • Travis Olliphant, the developer of NumPy, created SciPy.
  • SciPy has optimized and added/enhanced functions that are often used in NumPy and Data Science.

stats.halfgennorm.rvs() Function:

We can generate a random variate from a generalized normal distribution using the stats.halfgennorm.rvs() function.

Syntax:

stats.halfgennorm.rvs(beta)

Return Value:

A random variate value is returned by the stats.halfgennorm.rvs() Function.

Examples:

Example1:

Input:

Given Beta = 4

Output:

The random variate value for the given beta { 4 } value = 0.2221758090994274

Example2:

Input:

Given Beta = 2

Output:

The random variate value for the given beta { 2 } value = 0.9441734155526698

Scipy stats.halfgennorm.rvs() Function in Python

Method #1: Using rvs() Function (Static Input)

Approach:

  • Import halfgennorm() method from stats of scipy module using the import keyword
  • Give the beta value as static input and store it in a variable.
  • Pass the given beta value as an argument to the rvs() function of halfgennorm to generate a random variate value from a generalized normal distribution.
  • Store it in another variable.
  • Print the random variate value for the given beta value.
  • The Exit of the Program.

Below is the implementation:

# Import halfgennorm() method from stats of scipy module using the import keyword
from scipy.stats import halfgennorm

# Give the beta value as static input and store it in a variable.
gvn_beta = 2

# Pass the given beta value as an argument to the rvs() function of halfgennorm to
# generate a random variate value from a generalized normal distribution.
# Store it in another variable.
rslt = halfgennorm.rvs(gvn_beta)

# Print the random variate value for the given beta value
print("The random variate value for the given beta {", gvn_beta,"} value = ", rslt)

Output:

The random variate value for the given beta { 2 } value = 0.24472017026361062

Method #2: Using rvs() Function (User Input)

Approach:

  • Import halfgennorm() method from stats of scipy module using the import keyword
  • Give the beta value as user input using the int(input()) function and store it in a variable.
  • Pass the given beta value as an argument to the rvs() function of halfgennorm to generate a random variate value from a generalized normal distribution.
  • Store it in another variable.
  • Print the random variate value for the given beta value.
  • The Exit of the Program.

Below is the implementation:

# Import halfgennorm() method from stats of scipy module using the import keyword
from scipy.stats import halfgennorm

# Give the beta value as user input using the int(input()) function and store it in a variable.
gvn_beta = int(input("Enter some random number = "))

# Pass the given beta value as an argument to the rvs() function of halfgennorm to
# generate a random variate value from a generalized normal distribution.
# Store it in another variable.
rslt = halfgennorm.rvs(gvn_beta)

# Print the random variate value for the given beta value
print("The random variate value for the given beta {", gvn_beta,"} value = ", rslt)

Output:

Enter some random number = 4
The random variate value for the given beta { 4 } value = 0.8736129067330833