Skip to content

Gibbs sampling #305

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

Open
wants to merge 10 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
28 changes: 18 additions & 10 deletions src/main/java/blog/bn/CBN.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,31 @@
package blog.bn;

import blog.common.DGraph;
import blog.world.PartialWorld;

/**
* A contingent bayes net (CBN) contains a set of random variables V. For each
* random variable X in V, there is an associated domain dom(X) and decision
* tree T_X. The decision tree is a binary tree where each node is a predicate
* on some subset of V. Each leaf of T_X is a probability distribution
* parametrized by a subset of V. (Summarized from Arora et. al, UAI-10)
*
* TODO: As the requirements for CBNs become clearer, add actual methods here
*
* @author rbharath
* @date Aug 11, 2012
*
* @author Da Tang
* @since Sep 07, 2014
*/

public interface CBN extends DGraph {
/**
* An empty CBN
*/
static final CBN EMPTY_CBN = new DefaultCBN();
public interface CBN extends DGraph {
/**
* An empty CBN
*/
static final CBN EMPTY_CBN = new DefaultCBN();

/**
* Calculating whether an edge Y -> Z is contingent on variable X or not in
* the
* PartialWorld or not.
*
*/
boolean isContingentOn(PartialWorld world, BayesNetVar X, BayesNetVar Y,
Copy link
Contributor

Choose a reason for hiding this comment

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

comment?

BayesNetVar Z);
}
79 changes: 64 additions & 15 deletions src/main/java/blog/bn/DefaultCBN.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,76 @@

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import blog.common.DefaultDGraph;
import blog.common.DefaultDGraph.NodeInfo;
import blog.common.Util;
import blog.sample.TraceParentRecEvalContext;
import blog.world.PartialWorld;

/**
* This class provides a default implementation of CBNs. Over the next few weeks,
* all inference algorithms will be modified to use CBNs.
* This class provides a default implementation of CBNs.
* Uses a DefaultCBN rather than a DefaultDGraph, so as to avoid
* ClassCastExceptions.
*
* @author Da Tang
* @since Sep 7, 2014
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

author?


public class DefaultCBN extends DefaultDGraph implements CBN {
/**
* Uses a DefaultCBN rather than a DefaultDGraph, so as to avoid ClassCastExceptions
*/
public Object clone() {
DefaultCBN clone = new DefaultCBN();
clone.nodeInfo = (Map) ((HashMap) nodeInfo).clone();
for (Iterator iter = clone.nodeInfo.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
entry.setValue(((NodeInfo) entry.getValue()).clone());
}
return clone;
}
/**
* clone method for the class Default CBN.
*/
public Object clone() {
Copy link
Contributor

Choose a reason for hiding this comment

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

comment???

Copy link
Contributor

Choose a reason for hiding this comment

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

what is the purpose of clone???

Copy link
Contributor

Choose a reason for hiding this comment

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

the comment seems strange

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is not written by me.

Copy link
Contributor

Choose a reason for hiding this comment

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

sorry. but please fix.

DefaultCBN clone = new DefaultCBN();
clone.nodeInfo = (Map) ((HashMap) nodeInfo).clone();
for (Iterator iter = clone.nodeInfo.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
entry.setValue(((NodeInfo) entry.getValue()).clone());
}
return clone;
}

@Override
public boolean isContingentOn(PartialWorld world, BayesNetVar X,
BayesNetVar Y, BayesNetVar Z) {
TraceParentRecEvalContext context = new TraceParentRecEvalContext(world);
if (Z instanceof VarWithDistrib) {
((VarWithDistrib) Z).getDistrib(context);
Copy link
Contributor

Choose a reason for hiding this comment

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

no use?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will be used sometimes.

} else if (Z instanceof DerivedVar) {
((DerivedVar) Z).getValue(context);
Copy link
Contributor

Choose a reason for hiding this comment

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

no use?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

used.

} else {
return true;
}

LinkedList<BayesNetVar> parentTrace = new LinkedList<BayesNetVar>();
parentTrace.addAll(context.getParentTrace());

int x = parentTrace.indexOf(X), y = parentTrace.indexOf(Y);
Copy link
Contributor

Choose a reason for hiding this comment

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

better to put in two lines

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean two empty lines?

if (x < 0 || y < 0) {
return false;
}
if (X instanceof NumberVar) {
if (x < y && world.getCBN().getAncestors(Y).contains(X)) {
if (Util.verbose()) {
System.out.println("\t Contingent relations type 1: " + X.toString()
+ " " + Y.toString() + " " + Z.toString());
}
return true;
} else {
return false;
}
} else {
if (x < y) {
if (Util.verbose()) {
System.out.println("\t Contingent relations type 2: " + X.toString()
+ " " + Y.toString() + " " + Z.toString());
}
return true;
} else {
return false;
}
}
}
}
67 changes: 57 additions & 10 deletions src/main/java/blog/bn/PatchCBN.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,70 @@

package blog.bn;

import blog.common.ParentUpdateDGraph;
import java.util.LinkedList;

import blog.common.DGraph;
import blog.common.ParentUpdateDGraph;
import blog.common.Util;
import blog.sample.TraceParentRecEvalContext;
import blog.world.PartialWorld;

/**
* A patch data structure to an underlying CBN that represents changes to
* the set of nodes and to the parent sets of existing nodes. Currently is a
* the set of nodes and to the parent sets of existing nodes. Currently is a
* thin wrapper over ParentUpdateDGraph.
*
*
* @author rbharath
* @date August 12, 2012
*/
public class PatchCBN extends ParentUpdateDGraph implements CBN {
/**
* Creates a new ParentUpdateDGraph that represents no changes to the given
* underlying graph.
*/
public PatchCBN(CBN underlying) {
super((DGraph) underlying);
}
/**
* Creates a new ParentUpdateDGraph that represents no changes to the given
* underlying graph.
*/
public PatchCBN(CBN underlying) {
super((DGraph) underlying);
}

@Override
public boolean isContingentOn(PartialWorld world, BayesNetVar X,
BayesNetVar Y, BayesNetVar Z) {
TraceParentRecEvalContext context = new TraceParentRecEvalContext(world);
if (Z instanceof VarWithDistrib) {
((VarWithDistrib) Z).getDistrib(context);
} else if (Z instanceof DerivedVar) {
((DerivedVar) Z).getValue(context);
} else {
return true;
}

LinkedList<BayesNetVar> parentTrace = new LinkedList<BayesNetVar>();
parentTrace.addAll(context.getParentTrace());

int x = parentTrace.indexOf(X), y = parentTrace.indexOf(Y);
if (x < 0 || y < 0) {
return false;
}
if (X instanceof NumberVar) {
if (x < y && world.getCBN().getAncestors(Y).contains(X)) {
if (Util.verbose()) {
System.out.println("\t Contingent relations type 1: " + X.toString()
+ " " + Y.toString() + " " + Z.toString());
}
return true;
} else {
return false;
}
} else {
if (x < y) {
if (Util.verbose()) {
System.out.println("\t Contingent relations type 2: " + X.toString()
+ " " + Y.toString() + " " + Z.toString());
}
return true;
} else {
return false;
}
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/blog/sample/AbstractProposer.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public PartialWorldDiff reduceToCore(PartialWorld curWorld, BayesNetVar var) {
}

public double proposeNextState(PartialWorldDiff proposedWorld,
BayesNetVar var, int i) {
BayesNetVar var, Object value) {
return 0;
}

Expand Down
Loading