Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 331
Yashu Seth Avatar asked Jan 23 '26 16:01

Yashu Seth


1 Answers

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.

like image 89
Ami Tavory Avatar answered Jan 25 '26 07:01

Ami Tavory



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!