Search code examples
pythonscipyprobabilityprobability-density

Can continuous random variables be converted into discrete using scipy?


If I initialize a subclass of scipy.stats.rv_continuous , for example scipy.stats.norm

>>> from scipy.stats import norm
>>> rv = norm()

Can I convert it into a list of probabilities with each element representing the probability of a range of values after providing the number of ranges? Something like - (for the range - [(-inf,-1), (-1,0), (0,1), (1, inf)] )

>>> li
[0.15865525393145707, 0.34134474606854293, 0.34134474606854293, 0.15865525393145707]

( where 0.15865525393145707 is the probability of the variable being less than -1 and 0.34134474606854293 for being in the range -1 to 0 and similarly for others.

Can this be done using scipy? If not which python library can support such conversion operations?


Solution

  • Based on your comment, you can calculate this using the CDF:

    from scipy.stats import norm
    import numpy as np
    
    >>> norm().cdf(-1) - norm().cdf(-np.inf), \
        norm().cdf(0) - norm().cdf(-1), \
        norm().cdf(1) - norm().cdf(0), \
        norm().cdf(np.inf) - norm().cdf(1)
    (0.15865525393145707,
     0.34134474606854293,
     0.34134474606854293,
     0.15865525393145707)
    

    This follows from the definition of the CDF, basically.


    Note that I'm getting numbers that sum to 1, but not the ones you write as the expected output. I don't know your basis for saying that those are the correct ones. My guess is you're implicitly using a Normal variable with non-unit standard deviation.