Pawel
Pawel

Reputation: 169

Tensorflow: Probability of an outcome in multinomial try

I'm doing a multinomial try - I have a vector of probabilities p=[p1, p2, p3] and a vector of observed outcomes n=[n1, n2, n3].

How does one find the probability of such an event using TensorFlow? I would like to have a solution working for matrices as well (i.e. I have a tensor such that each row represents probabilities and a tensor such that each row represents outcomes).

Upvotes: 2

Views: 294

Answers (1)

javidcf
javidcf

Reputation: 59731

You can use tf.distributions.Multinomial:

import tensorflow as tf

probs = tf.constant([.2, .4, .6], tf.float32)
counts = tf.constant([1, 2, 3], tf.float32)
total_count = tf.reduce_sum(counts)
multinomial = tf.distributions.Multinomial(total_count, probs=probs)
prob_counts = multinomial.prob(counts)
with tf.Session() as sess:
    print(sess.run(prob_counts))

Output:

0.13888888

You can also operate with multiple distributions at once like this:

import tensorflow as tf

probs = tf.placeholder(tf.float32, [None, 3])
counts = tf.placeholder(tf.float32, [None, 3])
total_counts = tf.reduce_sum(counts, axis=1)
multinomial = tf.distributions.Multinomial(total_counts, probs=probs)
prob_counts = multinomial.prob(counts)

In this case each row of probs would be a probability distribution, each row of counts a distribution sample and each element of prob_counts the probability of each sample.

Upvotes: 2

Related Questions