use of edu.cmu.tetrad.data.DiscreteVariable in project tetrad by cmu-phil.
the class BayesPm method copyAvailableInformationFromOldBayesPm.
// =========================PRIVATE METHODS=============================//
private void copyAvailableInformationFromOldBayesPm(BayesPm oldbayesPm, int lowerBound, int upperBound) {
Graph newGraph = getDag();
Graph oldGraph = oldbayesPm.getDag();
for (Node node1 : newGraph.getNodes()) {
if (oldGraph.containsNode(node1)) {
copyOldValues(oldbayesPm, node1, node1, lowerBound, upperBound);
} else {
setNewValues(node1, lowerBound, upperBound);
}
}
for (Node node2 : newGraph.getNodes()) {
if (oldGraph.containsNode(node2)) {
Node _node2 = dag.getNode(node2.getName());
DiscreteVariable oldNode2 = oldbayesPm.nodesToVariables.get(_node2);
oldNode2.setNodeType(node2.getNodeType());
oldNode2.setName(node2.getName());
this.nodesToVariables.put(_node2, oldNode2);
} else {
setNewValues(node2, lowerBound, upperBound);
}
}
}
use of edu.cmu.tetrad.data.DiscreteVariable in project tetrad by cmu-phil.
the class MlBayesImObs method simulateTimeSeries.
private DataSet simulateTimeSeries(int sampleSize) {
TimeLagGraph timeSeriesGraph = getBayesPm().getDag().getTimeLagGraph();
List<Node> variables = new ArrayList<>();
for (Node node : timeSeriesGraph.getLag0Nodes()) {
variables.add(new DiscreteVariable(timeSeriesGraph.getNodeId(node).getName()));
}
List<Node> lag0Nodes = timeSeriesGraph.getLag0Nodes();
DataSet fullData = new ColtDataSet(sampleSize, variables);
Graph contemporaneousDag = timeSeriesGraph.subgraph(lag0Nodes);
List<Node> tierOrdering = contemporaneousDag.getCausalOrdering();
int[] tiers = new int[tierOrdering.size()];
for (int i = 0; i < tierOrdering.size(); i++) {
tiers[i] = getNodeIndex(tierOrdering.get(i));
}
// Construct the sample.
int[] combination = new int[tierOrdering.size()];
for (int i = 0; i < sampleSize; i++) {
int[] point = new int[nodes.length];
for (int nodeIndex : tiers) {
double cutoff = RandomUtil.getInstance().nextDouble();
for (int k = 0; k < getNumParents(nodeIndex); k++) {
combination[k] = point[getParent(nodeIndex, k)];
}
int rowIndex = getRowIndex(nodeIndex, combination);
double sum = 0.0;
for (int k = 0; k < getNumColumns(nodeIndex); k++) {
double probability = getProbability(nodeIndex, rowIndex, k);
if (Double.isNaN(probability)) {
throw new IllegalStateException("Some probability " + "values in the BayesIm are not filled in; " + "cannot simulate data.");
}
sum += probability;
if (sum >= cutoff) {
point[nodeIndex] = k;
break;
}
}
}
}
return fullData;
}
use of edu.cmu.tetrad.data.DiscreteVariable in project tetrad by cmu-phil.
the class MlBayesImObs method simulateDataHelper.
/**
* Constructs a random sample using the given already allocated data set, to
* avoid allocating more memory.
*/
private DataSet simulateDataHelper(DataSet dataSet, boolean latentDataSaved) {
if (dataSet.getNumColumns() != nodes.length) {
throw new IllegalArgumentException("When rewriting the old data set, " + "number of variables in data set must equal number of variables " + "in Bayes net.");
}
int sampleSize = dataSet.getNumRows();
int numMeasured = 0;
int[] map = new int[nodes.length];
List<Node> variables = new LinkedList<>();
for (int j = 0; j < nodes.length; j++) {
if (!latentDataSaved && nodes[j].getNodeType() != NodeType.MEASURED) {
continue;
}
int numCategories = bayesPm.getNumCategories(nodes[j]);
List<String> categories = new LinkedList<>();
for (int k = 0; k < numCategories; k++) {
categories.add(bayesPm.getCategory(nodes[j], k));
}
DiscreteVariable var = new DiscreteVariable(nodes[j].getName(), categories);
variables.add(var);
int index = ++numMeasured - 1;
map[index] = j;
}
for (int i = 0; i < variables.size(); i++) {
Node node = dataSet.getVariable(i);
Node _node = variables.get(i);
dataSet.changeVariable(node, _node);
}
constructSample(sampleSize, numMeasured, dataSet, map);
return dataSet;
}
use of edu.cmu.tetrad.data.DiscreteVariable in project tetrad by cmu-phil.
the class BayesXmlRenderer method getVariablesElement.
private static Element getVariablesElement(BayesIm bayesIm) {
Element element = new Element("bnVariables");
for (int i = 0; i < bayesIm.getNumNodes(); i++) {
Node node = bayesIm.getNode(i);
BayesPm bayesPm = bayesIm.getBayesPm();
DiscreteVariable variable = (DiscreteVariable) bayesPm.getVariable(node);
Element element1 = new Element("discreteVariable");
element1.addAttribute(new Attribute("name", variable.getName()));
element1.addAttribute(new Attribute("index", "" + i));
boolean latent = node.getNodeType() == NodeType.LATENT;
if (latent) {
element1.addAttribute(new Attribute("latent", "yes"));
}
element1.addAttribute(new Attribute("x", "" + node.getCenterX()));
element1.addAttribute(new Attribute("y", "" + node.getCenterY()));
for (int j = 0; j < variable.getNumCategories(); j++) {
Element category = new Element("category");
category.addAttribute(new Attribute("name", variable.getCategory(j)));
category.addAttribute(new Attribute("index", "" + j));
element1.appendChild(category);
}
element.appendChild(element1);
}
return element;
}
use of edu.cmu.tetrad.data.DiscreteVariable in project tetrad by cmu-phil.
the class IndTestMixedMultipleTTest method expandVariable.
private List<Node> expandVariable(DataSet dataSet, Node node) {
if (node instanceof ContinuousVariable) {
return Collections.singletonList(node);
}
if (node instanceof DiscreteVariable && ((DiscreteVariable) node).getNumCategories() < 3) {
return Collections.singletonList(node);
}
if (!(node instanceof DiscreteVariable)) {
throw new IllegalArgumentException();
}
List<String> varCats = new ArrayList<>(((DiscreteVariable) node).getCategories());
// first category is reference
varCats.remove(0);
List<Node> variables = new ArrayList<>();
for (String cat : varCats) {
Node newVar;
do {
String newVarName = node.getName() + "MULTINOM" + "." + cat;
newVar = new DiscreteVariable(newVarName, 2);
} while (dataSet.getVariable(newVar.getName()) != null);
variables.add(newVar);
dataSet.addVariable(newVar);
int newVarIndex = dataSet.getColumn(newVar);
int numCases = dataSet.getNumRows();
for (int l = 0; l < numCases; l++) {
Object dataCell = dataSet.getObject(l, dataSet.getColumn(node));
int dataCellIndex = ((DiscreteVariable) node).getIndex(dataCell.toString());
if (dataCellIndex == ((DiscreteVariable) node).getIndex(cat))
dataSet.setInt(l, newVarIndex, 1);
else
dataSet.setInt(l, newVarIndex, 0);
}
}
return variables;
}
Aggregations