def binomial_ll(parameter, sample): """ Return the likelihood of the model parameter under sample. """ # compile our method body from qy import ( Value, type_from_any, build_engine, ) q = \ Qy( return_type = type_from_any(float) , argument_types = [type_from_any(float)] * 3, default_return = Value.from_any(numpy.nan) , ) with q.active() as bar: q.return_(binomial_log_pdf(*q.main_body.argument_values)) engine = build_engine(q.module) # define the python wrapper from ctypes import ( CFUNCTYPE, c_double, ) prototype = CFUNCTYPE(c_double, c_double, c_double, c_double) generated = prototype(engine.get_pointer_to_function(q.main)) from qy.support import raise_if_set def wrapper(parameter, sample): """ Return the likelihood of the model parameter under sample. """ parameter = numpy.asarray(parameter, Binomial.parameter_dtype) result = generated(sample, parameter["p"], parameter["n"]) raise_if_set() return result # don't let the engine be GCed wrapper._engine = engine # replace and compute global binomial_ll binomial_ll = wrapper return binomial_ll(parameter, sample)
def wrapper(*args, **kwargs): """ Call a method of the active Qy instance. """ return getattr(Qy.get_active(), name)(*args, **kwargs)