-
-
Save rahulbhadani/cfa1b13c5f3e65c28e4ac8c1ab4813ce to your computer and use it in GitHub Desktop.
Negative Binomial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@tf.function | |
def tfgamma(z): | |
g = tf.exp(tf.math.lgamma(z)) | |
return g | |
@tf.function | |
def NB(x, mu, phi): | |
numerator = tfgamma(x + phi)*tf.math.pow(mu,x)*tf.math.pow(phi, phi) | |
denominator = tfgamma(phi)*tfgamma(x+1) *tf.math.pow((mu + phi),(x+phi)) | |
return (numerator/denominator) | |
@tf.function | |
def negloglikelihood(Matrix, mu, phi): | |
total = tf.constant(np.zeros(len(Matrix.columns)), dtype=tf.float32) | |
index = 0 | |
for (columnName, columnData) in Matrix.iteritems(): | |
for x in columnData.values: | |
summand = tf.math.log(NB(x, mu[index], phi[index])) | |
total[index] = total[index] + summand | |
total[index] = -1.0*total[index] | |
index = index + 1 | |
return -1.0*total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment