Package: rvlib
rvlibAnyone who has usedDistributions.jlwill tell
you how nice the interface is relative to the "exotic" (the most polite word
we can think of) interface to distributions exposed byscipy.stats.Distributions.jlalso brings better performance, particularly when its
methods are used inside loops.For these reason we've put togetherrvlib, which mimics the
interface ofDistributions.jl, while at the same
time attaining similar performance by exploitingnumba.This package was inspired by Joshua Adelman's (@synapticarbors)blog postdescribing how
to call the Rmath C library from numba usingCFFI, and utilizes his build script
to set up the CFFI interface.ObjectivesFollow the API of theDistributions.jlpackage as closely as possibleCreate a python package that has better performance thanscipy.stats.MethodologyAll the classes are marked for optimization using the@jitclassdecorator. As a result, instances of different distributions can be called within user specific routines or passed as arguments innopythonmode usingnumba.The evaluation and sampling methods are built on theRmathC library -- also used by theDistributions.jlpackage.Distributions currently implementedUnivariate continuous:NormalChisqUniformTLog-normalFBetaGammaExponentialCauchyLogisticWeibullUnivariate discrete:PoissonGeometricHypergeometricBinomialNegative BinomialMultivariate continuous:check for updates on mulitvariate normal inmultivariatebranchFunctionalityThe following properties are shared by all the univariate distributions:params: tuple of the distribution's parameterslocation: the location of the distribution (if exists)scale: the scale of the distribution (if exists)shape: the shape of the distribution (if exists)mean: the mean of the distributionmedian: the median of the distributionmode: the mode of the distributionvar: the variance of the distributionstd: the standard deviation of the distributionskewness: the skewness of the distributionkurtosis: the kurtosis of the distributionisplatykurtic: boolean indicating if kurtosis is greater than zeroisleptokurtic: boolean indicating if kurtosis is less than zeroismesokurtic: boolean indicating if kurtosis is equal to zeroentropy: the entropy of the distributionThe following methods can be called for all univariate distributions:mgf: evaluate the moment generating function (if exists)cf: evaluate the characteristic function (if exists)pdf: evaluate the probability density functionlogpdf: evaluate the logarithm of the prabability density functionloglikelihood: evaluate the log-likelihood of the distribution with respect to all samples contained in array xcdf: evaluate the cumulative distribution functionccdf: evaluate the complementary cdf, i.e. (1 - cdf)logcdf: evaluate the logarithm of the cdflogccdf: evaluate the logarithm of the complementary cdfquantile: evaluate the quantile function at a critical valuecquantile: evaluate the complementary quantile functioninvlogcdf: evaluate the inverse function of the logcdfinvlogccdf: evaluate the inverse function of the logccdfrand: generate array of independent random drawsSeed settingAs the package is built around theRmathlibrary the seed for the random number generator has to be set using theRmathset_seed(x,y)function. For example:importrvlibasrlrl.set_seed(123,456)# note that it requires two argumentsUse and PerformancePreliminary comparison with thescipy.statspackage.fromrvlibimportNormalfromscipy.statsimportnormimportnumpyasnpimporttimeitN_dist=Normal(0,1)# rvlib versionN_scipy=norm(0,1)# scipy.stats versionx=np.linspace(0,100,100)In[1]:%timeitN_dist.pdf(x)Out[1]:Theslowestruntook8.85timeslongerthanthefastest.Thiscouldmeanthatanintermediateresultisbeingcached.100000loops,bestof3:9.69µsperloopIn[2]:%timeitN_scipy.pdf(x)Out[2]:10000loops,bestof3:150µsperloopIn[3]:%timeitN_dist.cdf(x)Out[3]:Theslowestruntook20325.82timeslongerthanthefastest.Thiscouldmeanthatanintermediateresultisbeingcached.100000loops,bestof3:8.08µsperloopIn[4]:%timeitN_scipy.cdf(x)Out[4]:Theslowestruntook190.64timeslongerthanthefastest.Thiscouldmeanthatanintermediateresultisbeingcached.10000loops,bestof3:126µsperloopIn[5]:%timeitN_dist.rand(1000)Out[5]:Theslowestruntook2166.80timeslongerthanthefastest.Thiscouldmeanthatanintermediateresultisbeingcached.10000loops,bestof3:85.8µsperloopIn[6]:%timeitN_scipy.rvs(1000)Out[6]:10000loops,bestof3:119µsperloopContributorsDaniel Csaba (daniel.csaba@nyu.edu)Spencer Lyon (spencer.lyon@stern.nyu.edu)This is a fork of theRmath-julialibrary, with Python support added.The original readme of the Rmath-julia repository is included below.Rmath-juliaThis is the Rmath library from R, which is used mainly by Julia'sDistributions.jlpackage.The main difference here is that this library has been patched to use
theDSFMTRNG
insrc/runif.c.The Julia RNG is in sync with the one used by the Distributions.jl package:julia> srand(1);

julia> [rand(), rand()]
2-element Array{Float64,1}:
 0.236033
 0.346517

julia> srand(1);

julia> using Distributions

julia> [rand(Uniform()), rand(Uniform())]
2-element Array{Float64,1}:
 0.236033
 0.346517Build instructionsRmath-julia requires GNU Make (https://www.gnu.org/software/make). Just runmaketo compile the library.