-
Notifications
You must be signed in to change notification settings - Fork 29
added NoisyOr.java and changed APPROXIMATE_ZERO in util.java #332
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
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,95 @@ | ||
| /** | ||
| * | ||
| */ | ||
| package blog.distrib; | ||
|
|
||
| import blog.common.Util; | ||
| import blog.common.numerical.MatrixLib; | ||
|
|
||
| /** | ||
| * @author BEN | ||
| * @since Dec 6, 2014 | ||
| * | ||
| */ | ||
| public class NoisyOr implements CondProbDistrib { | ||
|
|
||
| @Override | ||
| public void setParams(Object[] params) { | ||
| if (params.length != 2) { | ||
| throw new IllegalArgumentException("expected two parameters"); | ||
| } | ||
| setParams((MatrixLib) params[0], (MatrixLib) params[1]); | ||
| } | ||
|
|
||
| public void setParams(MatrixLib b, MatrixLib p) { | ||
| if (b != null) { | ||
| this.b = b; | ||
| this.hasb = true; | ||
| } | ||
| if (p != null) { | ||
| this.p = p; | ||
| this.hasp = true; | ||
| } | ||
| if (this.hasb && this.hasp) { | ||
|
Contributor
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. null is never a valid value for MatrixLib objects. So hasp and hasb are redundant. This should be: Alternatively, since it doesn't seem that setting these to null is all that useful...: and later... private void checkHasParams() { if (this.prob.isNaN()) throw new IllegalArgumentException("Not initialized properly."); } Which allows eliminating all the member variables except this.prob. |
||
| initializeConstants(); | ||
| } | ||
| } | ||
|
|
||
| private void initializeConstants() { | ||
| this.prob = 1.0 - this.b.transpose().timesMat(this.p).det(); | ||
| } | ||
|
|
||
| private void checkHasParams() { | ||
| if (!this.hasp && !this.hasb) { | ||
| throw new IllegalArgumentException("parameter p not provided"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public double getProb(Object value) { | ||
| return getProb(((Boolean) value).booleanValue()); | ||
| } | ||
|
|
||
| public double getProb(boolean value) { | ||
| checkHasParams(); | ||
| if (value) { | ||
| return this.prob; | ||
| } | ||
| return 1 - this.prob; | ||
| } | ||
|
|
||
| @Override | ||
| public double getLogProb(Object value) { | ||
| return getLogProb(((Boolean) value).booleanValue()); | ||
| } | ||
|
|
||
| public double getLogProb(boolean value) { | ||
| checkHasParams(); | ||
| if (value) { | ||
| return Math.log(this.prob); | ||
| } | ||
| return Math.log(1.0 - this.prob); | ||
| } | ||
|
|
||
| @Override | ||
| public Object sampleVal() { | ||
| checkHasParams(); | ||
| if (Util.random() < this.prob) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public Object[] getFiniteSupport() { | ||
| return finiteSupport; | ||
| } | ||
|
|
||
| private MatrixLib b; | ||
| private MatrixLib p; | ||
| private double prob; | ||
| private boolean hasb; | ||
| private boolean hasp; | ||
| private static final Object[] finiteSupport = { true, false }; | ||
|
|
||
| } | ||
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.
Get rid of the extra whitespace here, so that the commit isn't polluted by mere whitespace changes.