In scipy.stats
you can create a frozen distribution that allows the parameterization (shape, location & scale) of the distribution to be permanently set for that instance.
For example, you can create an gamma distribution (scipy.stats.gamma
) with a
,loc
and scale
parameters and freeze them so they do not have to be passed around every time that distribution is needed.
import scipy.stats as stats
# Parameters for this particular gamma distribution
a, loc, scale = 3.14, 5.0, 2.0
# Do something with the general distribution parameterized
print 'gamma stats:', stats.gamma(a, loc=loc, scale=scale).stats()
# Create frozen distribution
rv = stats.gamma(a, loc=loc, scale=scale)
# Do something with the specific, already parameterized, distribution
print 'rv stats :', rv.stats()
gamma stats: (array(11.280000000000001), array(12.56))
rv stats : (array(11.280000000000001), array(12.56))
rv
parameters?Since the parameters will most likely not be passed around as a result of this feature, is there a way to get those values back from only the frozen distribution, rv
, later on?
The location ( loc ) keyword specifies the mean. The scale ( scale ) keyword specifies the standard deviation. As an instance of the rv_continuous class, norm object inherits from it a collection of generic methods (see below for the full list), and completes them with details specific for this particular distribution.
ppf() takes a percentage and returns a standard deviation multiplier for what value that percentage occurs at. It is equivalent to a, 'One-tail test' on the density plot. From scipy. stats.
In the above program, first, we need to import the norm module from the scipy. stats, then we passed the data as Numpy array in the cdf() function. To get the median of the distribution, we can use the Percent Point Function (PPF), this is the inverse of the CDF.
describe() function | Python. scipy. stats. describe(array, axis=0) computes the descriptive statistics of the passed array elements along the specified axis of the array.
scipy.stats.f () is an F continuous random variable that is defined with a standard format and some shape parameters to complete its specification. loc : [optional] location parameter. Default = 0 scale : [optional] scale parameter.
The distributions in scipy.stats have recently been corrected and improved and gained a considerable test suite; however, a few issues remain: The distributions have been tested over some range of parameters; however, in some corner ranges, a few incorrect results may remain.
The parameters are the key things we want to learn about. The parameters are usually unknown. Sample statistics gives us estimates for parameters. There will always be some uncertainty about how accurate estimates are.
The scale parameter represents the variability present in the distribution. Changing the scale parameter affects how far the probability distribution stretches out. As you increase the scale, the distribution stretches further right, and the height decreases.
rv
frozen parametersYes, the parameters used to create a frozen distribution are available within the instance of the distribution. They are stored within the args
& kwds
attribute. This will be dependent on if the distribution's instance was created with positional arguments or keyword arguments.
import scipy.stats as stats
# Parameters for this particular alpha distribution
a, loc, scale = 3.14, 5.0, 2.0
# Create frozen distribution
rv1 = stats.gamma(a, loc, scale)
rv2 = stats.gamma(a, loc=loc, scale=scale)
# Do something with frozen parameters
print 'positional and keyword'
print 'frozen args : {}'.format(rv1.args)
print 'frozen kwds : {}'.format(rv1.kwds)
print
print 'positional only'
print 'frozen args : {}'.format(rv2.args)
print 'frozen kwds : {}'.format(rv2.kwds)
positional and keyword
frozen args : (3.14, 5.0, 2.0)
frozen kwds : {}
positional only
frozen args : (3.14,)
frozen kwds : {'loc': 5.0, 'scale': 2.0}
args
and kwds
There is an private method, .dist._parse_args()
, which handles both cases of parameter passing and will return a consistent result.
# Get the original parameters regardless of argument type
shape1, loc1, scale1 = rv1.dist._parse_args(*rv1.args, **rv1.kwds)
shape2, loc2, scale2 = rv2.dist._parse_args(*rv2.args, **rv2.kwds)
print 'positional and keyword'
print 'frozen parameters: shape={}, loc={}, scale={}'.format(shape1, loc1, scale1)
print
print 'positional only'
print 'frozen parameters: shape={}, loc={}, scale={}'.format(shape2, loc2, scale2)
positional and keyword
frozen parameters: shape=(3.14,), loc=5.0, scale=2.0
positional only
frozen parameters: shape=(3.14,), loc=5.0, scale=2.0
Granted, using private methods is typically bad practice because technically internal APIs can always change, however, sometimes they provide nice features, would be easy to re-implement should things change and nothing is really private in Python :).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With