Guest User

Untitled

a guest
Jan 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. def fit_mme(sample_data, replace_values=None, name=None):
  2. """
  3. Calculates the Maximum-of-Momentum Estimator of `NB(r, p)` for given sample data along axis 0.
  4.  
  5. :param sample_data: matrix containing samples for each distribution on axis 0n
  6. E.g. `(N, M)` matrix with `M` distributions containing `N` observed values each
  7. :param replace_values: Matrix of size `shape(sample_data)[1:]`
  8. :param name: A name for the operation (optional).
  9. :return: estimated values of `r` and `p`
  10. """
  11. with tf.name_scope(name, "MME"):
  12. mean = tf.reduce_mean(sample_data, axis=0, name="mean")
  13. variance = tf.reduce_mean(tf.square(sample_data - mean),
  14. axis=0,
  15. name="variance")
  16. if replace_values is None:
  17. replace_values = tf.fill(tf.shape(variance), math.nan, name="NaN_constant")
  18.  
  19. r_by_mean = tf.where(tf.less(mean, variance),
  20. mean / (variance - mean),
  21. replace_values)
  22. r = r_by_mean * mean
  23. r = tf.identity(r, "r")
  24.  
  25. p = 1 / (r_by_mean + 1)
  26. p = tf.identity(p, "p")
  27.  
  28. return r, p
Add Comment
Please, Sign In to add comment