In [ ]:
%matplotlib inline
In [ ]:
import warnings
warnings.simplefilter('ignore')
from numpy.random import seed
from scipy import stats
from matplotlib import pyplot
import seaborn
import paramnormal
clean_bkgd = {'axes.facecolor':'none', 'figure.facecolor':'none'}
seaborn.set(style='ticks', rc=clean_bkgd)
In [ ]:
seed(0)
paramnormal.lognormal(mu=0.75, sigma=1.25).rvs(5)
What's happening here is that paramnormal.lognormal(mu=0.75, sigma=1.25) translates the arguments, passes them to scipy.stats.lognorm, and returns scipy's distribution object. Then we call the rvs method of that object to generate five random numbers in an array.
Through the activity API, that equivalent to:
In [ ]:
seed(0)
paramnormal.activity.random('lognormal', mu=0.75, sigma=1.25, shape=5)
And of course, Greek letters are still supported.
In [ ]:
seed(0)
paramnormal.activity.random('lognormal', μ=0.75, σ=1.25, shape=5)
Lastly, you can reuse an already full-specified distribution and the shape parameter can take a tuple to return N-dimensional arrays.
In [ ]:
seed(0)
my_dist = paramnormal.lognormal(μ=0.75, σ=1.25)
paramnormal.activity.random(my_dist, shape=(2, 4))
In [ ]:
data = paramnormal.activity.random('beta', α=3, β=2, shape=37)
paramnormal.activity.fit('beta', data)
Equivalent command to perform the same fits in raw scipy is shown below:
In [ ]:
# constrained loc and scale
stats.beta.fit(data, floc=0, fscale=1)
You can still fix the primary parameters and unconstrain the defaults.
In [ ]:
paramnormal.activity.fit('beta', data, β=2, loc=None)
And again in raw scipy:
In [ ]:
# constrained beta and scale, unconstrained loc
stats.beta.fit(data, f1=2, fscale=1)
In [ ]:
ax = paramnormal.activity.plot('beta', α=3, β=2)
paramnormal.activity.plot('beta', α=3, β=2, ax=ax, which='CDF')
ax.legend()
You can plot on an existing figure through the ax argument and control the line style through line_opts.
In [ ]:
fig, (ax, ax2) = pyplot.subplots(nrows=2, sharex=True, sharey=True)
paramnormal.activity.plot('beta', α=6, β=2, ax=ax, line_opts=dict(color='firebrick', lw=3))
paramnormal.activity.plot('beta', α=2, β=6, ax=ax2, line_opts=dict(color='forestgreen', lw=1.25))
ax.set_ylabel('α=6, β=2')
ax2.set_ylabel('α=2, β=6')
seaborn.despine(fig)
Of course, you can create a fully-specified distribtion and omit the distribution parameters.
In [ ]:
beta = paramnormal.beta(α=3, β=2)
ax = paramnormal.activity.plot(beta)
And finally, you can pass an array of data and an unfrozen distribution, and a new distribution will be fit to your data.
In [ ]:
data = paramnormal.activity.random('beta', α=2, β=6, shape=37) + \
paramnormal.activity.random('normal', μ=5, σ=1, shape=37)
ax = paramnormal.activity.plot('normal', data=data, line_opts=dict(label='Emperical Fit'))
ax = paramnormal.activity.plot('normal', μ=5, σ=1, line_opts=dict(label='Theoretical'))
ax.legend()