python - Alternative inverse calculation of the beta function of scipy.stats -
python - Alternative inverse calculation of the beta function of scipy.stats -
i discovered bug in implementation of beta.ppf function in scipy.stats. has been confirmed , marked defect in bug reproting system.
however, need calculate confidence intervals of beta distribution , hence need inverse of beta function. since cannot rely on current version of beta.ppf need alternative python. preferably not want implent function on own.
does know function can replace beta.ppf function scipy.stats?
okay, created workaround using c++ boost library implements version of beta distribution. in order utilize have install boost library , compile it. afterwards can utilize using next code:
betainv.cpp
#include <boost/python.hpp> #include <boost/math/distributions/beta.hpp> using namespace boost::python; class betainvclass { public: double betainv(double p, double a, double b); }; double betainvclass::betainv(double p, double a, double b) { homecoming boost::math::ibeta_inv(a, b, p); } // expose classes , methods python boost_python_module(betainv) { class_<betainvclass> ("create_betainv_instance") .def("betainv", &betainvclass::betainv) ; }
makefile:
target = betainv python_inc = /usr/local/cellar/python/2.7.5/frameworks/python.framework/versions/2.7/include/python2.7 python_lib = /usr/local/cellar/python/2.7.5/frameworks/python.framework/versions/2.7/lib/python2.7/config boost_inc = /usr/local/include boost_lib = /usr/local/lib $(target).so: $(target).o g++ -shared -wl \ $(target).o -l$(boost_lib) -lboost_python \ -l$(python_lib) -lpython2.7 \ -o $(target).so $(target).o: $(target).cpp g++ -i$(python_inc) -i$(boost_inc) -c $(target).cpp clean: rm -f *.o *.a *.so *~ core
python illustration file:
import betainv beta = betainv.create_betainv_instance() print "0.25, 0.0342, 170 -> " + str(beta.betainv(0.25, 0.0342, 170)) print "0.25, 0.0342, 171 -> " + str(beta.betainv(0.25, 0.0342, 171)) print "0.25, 0.0342, 172 -> " + str(beta.betainv(0.25, 0.0342, 172))
btw farther comment. did speed test , ran method 1000 time. first using scipy , boost implementation. these results in milliseconds:
required time scipy = 295.145019531 required time c++ = 7.68383789062
boost c++ implementation 42 time quicker.
python scipy beta
Comments
Post a Comment