-
Notifications
You must be signed in to change notification settings - Fork 6
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
sum0 += (numberofSuccesses.get(i) * log(probabilities.get(i))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 26 could cause |
||
return sum0 | ||
} | ||
} | ||
|
||
logf(numberofSuccesses, probabilities, numberofTrials) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)} | ||
} |
There was a problem hiding this comment.
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 benumberofSuccesses.get(i) > numberofTrials