01:
\n",
"
+02: import random
\n",
"
__pyx_t_1 = __Pyx_Import(__pyx_n_s_random, 0, 0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 2, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_1);\n",
" if (PyDict_SetItem(__pyx_d, __pyx_n_s_random, __pyx_t_1) < 0) __PYX_ERR(0, 2, __pyx_L1_error)\n",
" __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"
03:
\n",
"
+04: def cython_inversion_binomial(int n, double p) -> int:
\n",
"
/* Python wrapper */\n",
"static PyObject *__pyx_pw_46_cython_magic_beb04c003b39b7afc4e8a1cfd3d3afad_1cython_inversion_binomial(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n",
"static PyMethodDef __pyx_mdef_46_cython_magic_beb04c003b39b7afc4e8a1cfd3d3afad_1cython_inversion_binomial = {\"cython_inversion_binomial\", (PyCFunction)(void*)(PyCFunctionWithKeywords)__pyx_pw_46_cython_magic_beb04c003b39b7afc4e8a1cfd3d3afad_1cython_inversion_binomial, METH_VARARGS|METH_KEYWORDS, 0};\n",
"static PyObject *__pyx_pw_46_cython_magic_beb04c003b39b7afc4e8a1cfd3d3afad_1cython_inversion_binomial(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n",
" int __pyx_v_n;\n",
" double __pyx_v_p;\n",
" PyObject *__pyx_r = 0;\n",
" __Pyx_RefNannyDeclarations\n",
" __Pyx_RefNannySetupContext(\"cython_inversion_binomial (wrapper)\", 0);\n",
" {\n",
" static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_n,&__pyx_n_s_p,0};\n",
" PyObject* values[2] = {0,0};\n",
" if (unlikely(__pyx_kwds)) {\n",
" Py_ssize_t kw_args;\n",
" const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);\n",
" switch (pos_args) {\n",
" case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
" CYTHON_FALLTHROUGH;\n",
" case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
" CYTHON_FALLTHROUGH;\n",
" case 0: break;\n",
" default: goto __pyx_L5_argtuple_error;\n",
" }\n",
" kw_args = PyDict_Size(__pyx_kwds);\n",
" switch (pos_args) {\n",
" case 0:\n",
" if (likely((values[0] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_n)) != 0)) kw_args--;\n",
" else goto __pyx_L5_argtuple_error;\n",
" CYTHON_FALLTHROUGH;\n",
" case 1:\n",
" if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_p)) != 0)) kw_args--;\n",
" else {\n",
" __Pyx_RaiseArgtupleInvalid(\"cython_inversion_binomial\", 1, 2, 2, 1); __PYX_ERR(0, 4, __pyx_L3_error)\n",
" }\n",
" }\n",
" if (unlikely(kw_args > 0)) {\n",
" if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"cython_inversion_binomial\") < 0)) __PYX_ERR(0, 4, __pyx_L3_error)\n",
" }\n",
" } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {\n",
" goto __pyx_L5_argtuple_error;\n",
" } else {\n",
" values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
" values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
" }\n",
" __pyx_v_n = __Pyx_PyInt_As_int(values[0]); if (unlikely((__pyx_v_n == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 4, __pyx_L3_error)\n",
" __pyx_v_p = __pyx_PyFloat_AsDouble(values[1]); if (unlikely((__pyx_v_p == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 4, __pyx_L3_error)\n",
" }\n",
" goto __pyx_L4_argument_unpacking_done;\n",
" __pyx_L5_argtuple_error:;\n",
" __Pyx_RaiseArgtupleInvalid(\"cython_inversion_binomial\", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 4, __pyx_L3_error)\n",
" __pyx_L3_error:;\n",
" __Pyx_AddTraceback(\"_cython_magic_beb04c003b39b7afc4e8a1cfd3d3afad.cython_inversion_binomial\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n",
" __Pyx_RefNannyFinishContext();\n",
" return NULL;\n",
" __pyx_L4_argument_unpacking_done:;\n",
" __pyx_r = __pyx_pf_46_cython_magic_beb04c003b39b7afc4e8a1cfd3d3afad_cython_inversion_binomial(__pyx_self, __pyx_v_n, __pyx_v_p);\n",
"\n",
" /* function exit code */\n",
" __Pyx_RefNannyFinishContext();\n",
" return __pyx_r;\n",
"}\n",
"\n",
"static PyObject *__pyx_pf_46_cython_magic_beb04c003b39b7afc4e8a1cfd3d3afad_cython_inversion_binomial(CYTHON_UNUSED PyObject *__pyx_self, int __pyx_v_n, double __pyx_v_p) {\n",
" int __pyx_v_result;\n",
" double __pyx_v_q;\n",
" double __pyx_v_current_proba;\n",
" double __pyx_v_cum_proba;\n",
" double __pyx_v_one_uniform_sample;\n",
" PyObject *__pyx_r = NULL;\n",
" __Pyx_RefNannyDeclarations\n",
" __Pyx_RefNannySetupContext(\"cython_inversion_binomial\", 0);\n",
"/* … */\n",
" /* function exit code */\n",
" __pyx_L1_error:;\n",
" __Pyx_XDECREF(__pyx_t_2);\n",
" __Pyx_XDECREF(__pyx_t_3);\n",
" __Pyx_XDECREF(__pyx_t_4);\n",
" __Pyx_XDECREF(__pyx_t_5);\n",
" __Pyx_XDECREF(__pyx_t_6);\n",
" __Pyx_XDECREF(__pyx_t_7);\n",
" __Pyx_XDECREF(__pyx_t_9);\n",
" __Pyx_AddTraceback(\"_cython_magic_beb04c003b39b7afc4e8a1cfd3d3afad.cython_inversion_binomial\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n",
" __pyx_r = NULL;\n",
" __pyx_L0:;\n",
" __Pyx_XGIVEREF(__pyx_r);\n",
" __Pyx_RefNannyFinishContext();\n",
" return __pyx_r;\n",
"}\n",
"/* … */\n",
" __pyx_tuple_ = PyTuple_Pack(7, __pyx_n_s_n, __pyx_n_s_p, __pyx_n_s_result, __pyx_n_s_q, __pyx_n_s_current_proba, __pyx_n_s_cum_proba, __pyx_n_s_one_uniform_sample); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 4, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_tuple_);\n",
" __Pyx_GIVEREF(__pyx_tuple_);\n",
"/* … */\n",
" __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_beb04c003b39b7afc4e8a1cfd3d3afad_1cython_inversion_binomial, NULL, __pyx_n_s_cython_magic_beb04c003b39b7afc4); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 4, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_1);\n",
" if (PyDict_SetItem(__pyx_d, __pyx_n_s_cython_inversion_binomial, __pyx_t_1) < 0) __PYX_ERR(0, 4, __pyx_L1_error)\n",
" __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"
+05: if p <= 1e-9:
\n",
"
__pyx_t_1 = ((__pyx_v_p <= 1e-9) != 0);\n",
" if (__pyx_t_1) {\n",
"/* … */\n",
" }\n",
"
+06: return 0
\n",
"
__Pyx_XDECREF(__pyx_r);\n",
" __Pyx_INCREF(__pyx_int_0);\n",
" __pyx_r = __pyx_int_0;\n",
" goto __pyx_L0;\n",
"
+07: if p >= 1 - 1e-9:
\n",
"
__pyx_t_1 = ((__pyx_v_p >= (1.0 - 1e-9)) != 0);\n",
" if (__pyx_t_1) {\n",
"/* … */\n",
" }\n",
"
+08: return n
\n",
"
__Pyx_XDECREF(__pyx_r);\n",
" __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_n); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 8, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_2);\n",
" __pyx_r = __pyx_t_2;\n",
" __pyx_t_2 = 0;\n",
" goto __pyx_L0;\n",
"
+09: if p > 0.5: # speed up by computing for q and then substracting
\n",
"
__pyx_t_1 = ((__pyx_v_p > 0.5) != 0);\n",
" if (__pyx_t_1) {\n",
"/* … */\n",
" }\n",
"
+10: return n - cython_inversion_binomial(n, 1.0 - p)
\n",
"
__Pyx_XDECREF(__pyx_r);\n",
" __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_n); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_2);\n",
" __Pyx_GetModuleGlobalName(__pyx_t_4, __pyx_n_s_cython_inversion_binomial); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_4);\n",
" __pyx_t_5 = __Pyx_PyInt_From_int(__pyx_v_n); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_5);\n",
" __pyx_t_6 = PyFloat_FromDouble((1.0 - __pyx_v_p)); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_6);\n",
" __pyx_t_7 = NULL;\n",
" __pyx_t_8 = 0;\n",
" if (CYTHON_UNPACK_METHODS && unlikely(PyMethod_Check(__pyx_t_4))) {\n",
" __pyx_t_7 = PyMethod_GET_SELF(__pyx_t_4);\n",
" if (likely(__pyx_t_7)) {\n",
" PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_4);\n",
" __Pyx_INCREF(__pyx_t_7);\n",
" __Pyx_INCREF(function);\n",
" __Pyx_DECREF_SET(__pyx_t_4, function);\n",
" __pyx_t_8 = 1;\n",
" }\n",
" }\n",
" #if CYTHON_FAST_PYCALL\n",
" if (PyFunction_Check(__pyx_t_4)) {\n",
" PyObject *__pyx_temp[3] = {__pyx_t_7, __pyx_t_5, __pyx_t_6};\n",
" __pyx_t_3 = __Pyx_PyFunction_FastCall(__pyx_t_4, __pyx_temp+1-__pyx_t_8, 2+__pyx_t_8); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
" __Pyx_XDECREF(__pyx_t_7); __pyx_t_7 = 0;\n",
" __Pyx_GOTREF(__pyx_t_3);\n",
" __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n",
" __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;\n",
" } else\n",
" #endif\n",
" #if CYTHON_FAST_PYCCALL\n",
" if (__Pyx_PyFastCFunction_Check(__pyx_t_4)) {\n",
" PyObject *__pyx_temp[3] = {__pyx_t_7, __pyx_t_5, __pyx_t_6};\n",
" __pyx_t_3 = __Pyx_PyCFunction_FastCall(__pyx_t_4, __pyx_temp+1-__pyx_t_8, 2+__pyx_t_8); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
" __Pyx_XDECREF(__pyx_t_7); __pyx_t_7 = 0;\n",
" __Pyx_GOTREF(__pyx_t_3);\n",
" __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n",
" __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;\n",
" } else\n",
" #endif\n",
" {\n",
" __pyx_t_9 = PyTuple_New(2+__pyx_t_8); if (unlikely(!__pyx_t_9)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_9);\n",
" if (__pyx_t_7) {\n",
" __Pyx_GIVEREF(__pyx_t_7); PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_7); __pyx_t_7 = NULL;\n",
" }\n",
" __Pyx_GIVEREF(__pyx_t_5);\n",
" PyTuple_SET_ITEM(__pyx_t_9, 0+__pyx_t_8, __pyx_t_5);\n",
" __Pyx_GIVEREF(__pyx_t_6);\n",
" PyTuple_SET_ITEM(__pyx_t_9, 1+__pyx_t_8, __pyx_t_6);\n",
" __pyx_t_5 = 0;\n",
" __pyx_t_6 = 0;\n",
" __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_4, __pyx_t_9, NULL); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_3);\n",
" __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;\n",
" }\n",
" __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n",
" __pyx_t_4 = PyNumber_Subtract(__pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_4);\n",
" __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n",
" __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n",
" __pyx_r = __pyx_t_4;\n",
" __pyx_t_4 = 0;\n",
" goto __pyx_L0;\n",
"
+11: cdef int result = 0
\n",
"
__pyx_v_result = 0;\n",
"
+12: cdef double q = 1.0 - p
\n",
"
__pyx_v_q = (1.0 - __pyx_v_p);\n",
"
+13: cdef double current_proba = q**n
\n",
"
__pyx_v_current_proba = pow(__pyx_v_q, ((double)__pyx_v_n));\n",
"
+14: cdef double cum_proba = current_proba
\n",
"
__pyx_v_cum_proba = __pyx_v_current_proba;\n",
"
+15: cdef double one_uniform_sample = random.random()
\n",
"
__Pyx_GetModuleGlobalName(__pyx_t_3, __pyx_n_s_random); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 15, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_3);\n",
" __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_random); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 15, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_2);\n",
" __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n",
" __pyx_t_3 = NULL;\n",
" if (CYTHON_UNPACK_METHODS && unlikely(PyMethod_Check(__pyx_t_2))) {\n",
" __pyx_t_3 = PyMethod_GET_SELF(__pyx_t_2);\n",
" if (likely(__pyx_t_3)) {\n",
" PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_2);\n",
" __Pyx_INCREF(__pyx_t_3);\n",
" __Pyx_INCREF(function);\n",
" __Pyx_DECREF_SET(__pyx_t_2, function);\n",
" }\n",
" }\n",
" __pyx_t_4 = (__pyx_t_3) ? __Pyx_PyObject_CallOneArg(__pyx_t_2, __pyx_t_3) : __Pyx_PyObject_CallNoArg(__pyx_t_2);\n",
" __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0;\n",
" if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 15, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_4);\n",
" __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n",
" __pyx_t_10 = __pyx_PyFloat_AsDouble(__pyx_t_4); if (unlikely((__pyx_t_10 == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 15, __pyx_L1_error)\n",
" __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n",
" __pyx_v_one_uniform_sample = __pyx_t_10;\n",
"
+16: while cum_proba < one_uniform_sample:
\n",
"
while (1) {\n",
" __pyx_t_1 = ((__pyx_v_cum_proba < __pyx_v_one_uniform_sample) != 0);\n",
" if (!__pyx_t_1) break;\n",
"
+17: current_proba *= (p * (n - result)) / (q * (result + 1))
\n",
"
__pyx_t_10 = (__pyx_v_p * (__pyx_v_n - __pyx_v_result));\n",
" __pyx_t_11 = (__pyx_v_q * (__pyx_v_result + 1));\n",
" if (unlikely(__pyx_t_11 == 0)) {\n",
" PyErr_SetString(PyExc_ZeroDivisionError, \"float division\");\n",
" __PYX_ERR(0, 17, __pyx_L1_error)\n",
" }\n",
" __pyx_v_current_proba = (__pyx_v_current_proba * (__pyx_t_10 / __pyx_t_11));\n",
"
+18: cum_proba += current_proba
\n",
"
__pyx_v_cum_proba = (__pyx_v_cum_proba + __pyx_v_current_proba);\n",
"
+19: result += 1
\n",
"
__pyx_v_result = (__pyx_v_result + 1);\n",
" }\n",
"
+20: return result
\n",
"
__Pyx_XDECREF(__pyx_r);\n",
" __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_result); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 20, __pyx_L1_error)\n",
" __Pyx_GOTREF(__pyx_t_4);\n",
" __pyx_r = __pyx_t_4;\n",
" __pyx_t_4 = 0;\n",
" goto __pyx_L0;\n",
"
"
],
"text/plain": [
"