Skip to content

Instantly share code, notes, and snippets.

@rahulbhadani
Last active March 30, 2020 02:42
Show Gist options
  • Save rahulbhadani/cfa1b13c5f3e65c28e4ac8c1ab4813ce to your computer and use it in GitHub Desktop.
Save rahulbhadani/cfa1b13c5f3e65c28e4ac8c1ab4813ce to your computer and use it in GitHub Desktop.
Negative Binomial
@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