Search in sources :

Example 51 with DiscreteVariable

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);
        }
    }
}
Also used : DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable)

Example 52 with DiscreteVariable

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;
}
Also used : DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) DataSet(edu.cmu.tetrad.data.DataSet) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet)

Example 53 with DiscreteVariable

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;
}
Also used : DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable)

Example 54 with DiscreteVariable

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;
}
Also used : DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) Attribute(nu.xom.Attribute) Element(nu.xom.Element) Node(edu.cmu.tetrad.graph.Node)

Example 55 with DiscreteVariable

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;
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) Node(edu.cmu.tetrad.graph.Node)

Aggregations

DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)56 Node (edu.cmu.tetrad.graph.Node)37 DataSet (edu.cmu.tetrad.data.DataSet)18 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)16 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)11 LinkedList (java.util.LinkedList)9 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 Dag (edu.cmu.tetrad.graph.Dag)3 NumberFormat (java.text.NumberFormat)3 Element (nu.xom.Element)3 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)2 Graph (edu.cmu.tetrad.graph.Graph)2 LogisticRegression (edu.cmu.tetrad.regression.LogisticRegression)2 List (java.util.List)2 Elements (nu.xom.Elements)2 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)1 TakesInitialGraph (edu.cmu.tetrad.algcomparison.utils.TakesInitialGraph)1 StoredCellProbs (edu.cmu.tetrad.bayes.StoredCellProbs)1 BoxDataSet (edu.cmu.tetrad.data.BoxDataSet)1