Skip to content
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
2 changes: 1 addition & 1 deletion src/main/java/blog/common/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ public static boolean closeToZero(double number) {
* note a single precision can represent up to 1E-38
* double precision 1E-308
*/
final double APPROXIMATE_ZERO = 1e-20;
final double APPROXIMATE_ZERO = 1e-12;
return (number < APPROXIMATE_ZERO) && (number > -1 * APPROXIMATE_ZERO);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/blog/distrib/InverseWishart.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public double getLogProb(MatrixLib x) {
}
throw new IllegalArgumentException(
"The matrix given should be a symmetric one. But it isn't.");

Copy link
Copy Markdown
Contributor

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.

}

/*
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/blog/distrib/NoisyOr.java
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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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:
if (b != null) this.b = b;
if (p != null) this.p = p;
if (b != null && p != null) initializeConstants();

Alternatively, since it doesn't seem that setting these to null is all that useful...:
this.prob = Float.NaN;
if (b == null && p == null) return;
if (b==null) throw new IllegalArgumentException("Parameter b not provided.");
if (p==null) throw new IllegalArgumentException("Parameter p not provided.");
this.prob = 1.0 - b.transpose().timesMat(p).det();

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 };

}