Frozen Distribution
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))
Accessible 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?
Accessing
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.Bonus: Private method that handles both
args
andkwds
There is an private method,
.dist._parse_args()
, which handles both cases of parameter passing and will return a consistent result.Caveat
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 :).