scipy stats.rv_continuous() | Python

scipy.stats.rv_continuous() is a continuous random variable class which is meant for sub-classing. It is a base class for constructing specific distribution from continuous random variables. This class can’t directly be used as a distribution.

Parameters :
moment : [int] moment calculation to use: 0 for pdf, 1 for ppf. Default = 1
a : [float] Lower bound for distribution. Default is -ve infinity.
b : [float] Upper bound for distribution. Default is +ve infinity.
xtol : [float] tolerance for fixed point calculation for ppf
name : [str] Name of the instance. Used to construct the default e.g. for distributions
badvalue : [object] Default is np.nan. Value in a result arrays that indicates a value that for which some argument restriction is violated.
logname : [str] Used as part of theFirst line of the docstring.
extradoc : [str] Used as the last part of the docstring
shapes : [str] Shape of the distribution.

Return : Continuous Random Variable Distribution.

Code #1 : Using “rv_continuous class”.

filter_none

edit
close

play_arrow

link
brightness_4
code

def sample(self, size = 1, random_state = None):
      
        """
        Return a sample from PDF - Probability Distribution Function.
        calling - rv_continuous class.
  
        """
          
        return self._rv.rvs(size = size, random_state = random_state) 

chevron_right


Code #2 : Creating Gaussian Distribution from rv_continuous.

filter_none

edit
close

play_arrow

link
brightness_4
code

from scipy.stats import rv_continuous
import numpy as np
  
class gaussian_gen(rv_continuous):
    '''Gaussian distribution'''
    def _pdf(self, x):
        return np.exp(-x**2 / 2.) / np.sqrt(2.0 * np.pi)
      
gaussian = gaussian_gen(name = 'gaussian')
  
x = 2.0
gaussian._pdf(x)

chevron_right


Output :

0.05399096651318806



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.