Question Details

No question body available.

Tags

python scipy scipy.stats

Answers (1)

Accepted Answer Available
Accepted Answer
May 29, 2025 Score: 1 Rep: 2,961 Quality: High Completeness: 60%

For some reason this is not available by default and you need to patch original distribution if you'd like to do such optimization. The reason why it is not available lay most probably in distribution definition - Negative binomial distribution describes a sequence of i.i.d. Bernoulli trials, repeated until a predefined, non-random number of successes occurs. Based on this definition n must be an integer.

Here is a way to patch this behaviour:

from scipy import stats
from scipy.optimize import differentialevolution
import numpy as np
from scipy.stats.discretedistns import nbinomgen, ShapeInfo

class new
nbinom(nbinomgen): def shapeinfo(self): # in Shapeinfo we specify 'n' as non-integer return [ShapeInfo("n", False, (0, np.inf), (True, False)), ShapeInfo("p", False, (0, 1), (True, True))]

def optimizer(fun, bounds, *, integrality): return differential
evolution(fun, bounds, strategy='best2bin', integrality=[False, False, False])

data = [0,0,0,1,2,4,11] bounds = [(0.1, 1.55), (0, 1)] distr = new_nbinom(name = 'nbinom')

res4 = stats.fit(distr, data, bounds, optimizer=optimizer) print(res4)