def nb_likelihood(x, m, r):
    p = float(r) / (r + m)
    return nbinom.pmf(x, r, p)
Poisson-NB *regression* mixture using concomitant variables (mixture of experts)
20-11-2014
"""

"""
Can use pois_likelihood from before
"""

"""
Negative binomial regression likelihood
"""
def nb_regression_likelihood(y, x, (a_0, a_1), r):
    m = a_0 + a_1 * x
    p = float(r) / (r + m)

    return nbinom.pmf(y, r, p)

def mix_from_params(x, (w0, w1)): # just a sigmoid function
    pi = np.zeros((x.size,2))
    for i in range(x.size):
        pi[i,0] = 1.0/(1 + np.exp(w0 + w1*x[i]))
    pi[:,1] = 1 - pi[:,0]
    return pi

def NB_regression_objective_function((a0, a1, r), y, x, gam):
    value = 0

    # convert to floats otherwise math.log(m / (m+r)) -> -inf
    r = float(r)
    m = a0 + a1 * x