Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Multinomial Distribution and Modify Examples #127

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/main/java/blang/distributions/Multinomial.bl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package blang.distributions

import static bayonet.distributions.Multinomial.sampleMultinomial

/** A generalization of a Binomial Distribution. Value \in \mathbb{R}^n\). */
model Multinomial {
/** Number of successes for each of the \(n\) categories. */
random List<IntVar> numberofSuccesses

/** Vector of probabilities \((p_0, p_1, \dots, p_{n-1})\) for each of the \(n\) categories. */
param Simplex probabilities

/** The number of independent trials. Value in \(k > 0\) */
param IntVar numberofTrials

laws{

logf(numberofTrials) { return logFactorial(numberofTrials) }

logf(numberofSuccesses, probabilities, numberofTrials) {
var sum0 = 0.0
for (int i: 0..< numberofSuccesses.size()) {
if (probabilities.get(i) < 0.0 || probabilities.get(i) > 1.0) return NEGATIVE_INFINITY
if (numberofSuccesses.get(i) < 0.0) return NEGATIVE_INFINITY
if (numberofTrials <= 0 || numberofSuccesses.get(i) < numberofTrials) return NEGATIVE_INFINITY
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A first problem: numberofSuccesses.get(i) < numberofTrials should be numberofSuccesses.get(i) > numberofTrials

sum0 += (numberofSuccesses.get(i) * log(probabilities.get(i)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 26 could cause 0 * -infty = NaN, which is not the right behaviour

return sum0
}
}

logf(numberofSuccesses, probabilities, numberofTrials) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This second block should be combined with the first since it uses the same arguments.

var sum1 = 0.0
for (int i: 0..< numberofSuccesses.size()) {
if (numberofSuccesses.get(i) < 0) return NEGATIVE_INFINITY
if (numberofTrials <= 0 || numberofSuccesses.get(i) < numberofTrials) return NEGATIVE_INFINITY
sum1 += logFactorial(numberofSuccesses.get(i))
return sum1
}
}
}
generate(rand) {sampleMultinomial(rand, probabilities.vectorToArray)}
}
11 changes: 10 additions & 1 deletion src/test/java/blang/Examples.xtend
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,16 @@ class Examples {
.build,
intRealizationSquared
)


public val multinomial = add(
new Multinomial.Builder()
.setProbabilities(fixedSimplex(0.2,0.3,0.5))
.setNumberofTrials(fixedInt(3))
.setNumberofSuccesses(latentIntList(3))
.build,
listHash
)

public val hyperGeometric = add(
new HyperGeometric.Builder()
.setNumberOfDraws(fixedInt(3))
Expand Down