Search in sources :

Example 11 with RandomUtil

use of edu.cmu.tetrad.util.RandomUtil in project tetrad by cmu-phil.

the class DiscreteTetradTest method estimatePolychoric.

/**
 * Estimate the polychoric correlation of two variables.
 */
private double estimatePolychoric(int[] indices) {
    this.indices = indices;
    double[] start = new double[1];
    RandomUtil r = RandomUtil.getInstance();
    this.currentVar1 = indices[0];
    this.currentVar2 = indices[1];
    this.currentFiBuffer = new double[this.values[currentVar1].length + 1][this.values[currentVar2].length + 1];
    this.currentPi = new double[this.values[currentVar1].length][this.values[currentVar2].length];
    this.currentRho = start[0] = r.nextDouble() / 2. + // choose random correlation between 0.2 and 0.7
    0.2;
    this.currentRho = gridOptimizer();
    return this.currentRho;
}
Also used : RandomUtil(edu.cmu.tetrad.util.RandomUtil)

Example 12 with RandomUtil

use of edu.cmu.tetrad.util.RandomUtil in project tetrad by cmu-phil.

the class MixedUtils method generateMixedEdgeParams.

// generates a vector of length L with maximum value w that sums to 0
public static double[] generateMixedEdgeParams(double w, int L) {
    double[] vec = new double[L];
    RandomUtil ru = RandomUtil.getInstance();
    for (int i = 0; i < L; i++) {
        vec[i] = ru.nextUniform(0, 1);
    }
    double vMean = StatUtils.mean(vec);
    double vMax = 0;
    for (int i = 0; i < L; i++) {
        vec[i] = vec[i] - vMean;
        if (Math.abs(vec[i]) > Math.abs(vMax))
            vMax = vec[i];
    }
    double scale = w / vMax;
    // maintain sign of w;
    if (vMax < 0)
        scale *= -1;
    for (int i = 0; i < L; i++) {
        vec[i] *= scale;
    }
    return vec;
}
Also used : RandomUtil(edu.cmu.tetrad.util.RandomUtil)

Example 13 with RandomUtil

use of edu.cmu.tetrad.util.RandomUtil in project tetrad by cmu-phil.

the class TestStandardizedSem method test4.

// This tests what the user is going to try to do in the GUI.
@Test
public void test4() {
    List<Node> nodes = new ArrayList<>();
    for (int i1 = 0; i1 < 10; i1++) {
        nodes.add(new ContinuousVariable("X" + (i1 + 1)));
    }
    SemGraph graph = new SemGraph(new Dag(GraphUtils.randomGraph(nodes, 0, 10, 30, 15, 15, false)));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    StandardizedSemIm sem = new StandardizedSemIm(im);
    for (int i = 0; i < 20; i++) {
        List<Edge> edges = new ArrayList<>(graph.getEdges());
        RandomUtil random = RandomUtil.getInstance();
        int index = random.nextInt(edges.size());
        Edge edge = edges.get(index);
        Node a = edge.getNode1();
        Node b = edge.getNode2();
        StandardizedSemIm.ParameterRange range = sem.getCoefficientRange(a, b);
        double high = range.getHigh();
        double low = range.getLow();
        double coef = low + random.nextDouble() * (high - low);
        assertTrue(sem.setEdgeCoefficient(a, b, coef));
        coef = high + random.nextDouble() * (high - low);
        assertFalse(sem.setEdgeCoefficient(a, b, coef));
        coef = low - random.nextDouble() * (high - low);
        assertFalse(sem.setEdgeCoefficient(a, b, coef));
    }
}
Also used : ArrayList(java.util.ArrayList) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) RandomUtil(edu.cmu.tetrad.util.RandomUtil) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) Test(org.junit.Test)

Example 14 with RandomUtil

use of edu.cmu.tetrad.util.RandomUtil in project tetrad by cmu-phil.

the class TestStandardizedSem method test7.

// This tests what the user is going to try to do in the GUI.
@Test
public void test7() {
    RandomUtil random = RandomUtil.getInstance();
    random.setSeed(9394929393L);
    List<Node> nodes1 = new ArrayList<>();
    for (int i1 = 0; i1 < 5; i1++) {
        nodes1.add(new ContinuousVariable("X" + (i1 + 1)));
    }
    SemGraph graph = new SemGraph(new Dag(GraphUtils.randomGraph(nodes1, 0, 5, 30, 15, 15, false)));
    List<Node> nodes = graph.getNodes();
    int n1 = RandomUtil.getInstance().nextInt(nodes.size());
    int n2 = RandomUtil.getInstance().nextInt(nodes.size());
    while (n1 == n2) {
        n2 = RandomUtil.getInstance().nextInt(nodes.size());
    }
    Node node1 = nodes.get(n1);
    Node node2 = nodes.get(n2);
    Edge _edge = Edges.bidirectedEdge(node1, node2);
    graph.addEdge(_edge);
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    StandardizedSemIm sem = new StandardizedSemIm(im);
    DataSet data3 = sem.simulateDataReducedForm(1000, false);
    graph.setShowErrorTerms(false);
    for (int i = 0; i < 1; i++) {
        for (Edge edge : graph.getEdges()) {
            Node a = edge.getNode1();
            Node b = edge.getNode2();
            if (Edges.isDirectedEdge(edge)) {
                double initial = sem.getEdgeCoef(a, b);
                StandardizedSemIm.ParameterRange range = sem.getCoefficientRange(a, b);
                assertEquals(initial, sem.getEdgeCoef(a, b), 0.1);
                double low = range.getLow();
                double high = range.getHigh();
                double _coef = sem.getEdgeCoef(a, b);
                double coef = low + random.nextDouble() * (high - low);
                assertTrue(sem.setEdgeCoefficient(a, b, coef));
                sem.setEdgeCoefficient(a, b, _coef);
                coef = high + random.nextDouble() * (high - low);
                assertFalse(sem.setEdgeCoefficient(a, b, coef));
                coef = low - random.nextDouble() * (high - low);
                assertFalse(sem.setEdgeCoefficient(a, b, coef));
            } else if (Edges.isBidirectedEdge(edge)) {
                sem.setErrorCovariance(node1, node2, .15);
                assertTrue(isStandardized(sem));
                StandardizedSemIm.ParameterRange range2 = sem.getCovarianceRange(a, b);
                double low = range2.getLow();
                double high = range2.getHigh();
                if (low == Double.NEGATIVE_INFINITY)
                    low = -10000;
                if (high == Double.POSITIVE_INFINITY)
                    high = 10000;
                double _coef = sem.getErrorCovariance(a, b);
                double coef = low + random.nextDouble() * (high - low);
                assertTrue(sem.setErrorCovariance(a, b, coef));
                sem.setErrorCovariance(a, b, _coef);
                if (high != 10000) {
                    coef = high + random.nextDouble() * (high - low);
                    assertFalse(sem.setErrorCovariance(a, b, coef));
                }
                if (low != -10000) {
                    coef = low - random.nextDouble() * (high - low);
                    assertFalse(sem.setErrorCovariance(a, b, coef));
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) RandomUtil(edu.cmu.tetrad.util.RandomUtil) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) Test(org.junit.Test)

Example 15 with RandomUtil

use of edu.cmu.tetrad.util.RandomUtil in project tetrad by cmu-phil.

the class TetradDesktop method setGoodBounds.

/**
 * Randomly picks the location of a new window, such that it fits completely
 * on the screen.
 *
 * @param desktopPane the desktop pane that the frame is being added to.
 * @param frame the JInternalFrame which is being added.
 * @param desiredSize the desired dimensions of the frame.
 */
public static void setGoodBounds(JInternalFrame frame, JDesktopPane desktopPane, Dimension desiredSize) {
    RandomUtil randomUtil = RandomUtil.getInstance();
    Dimension desktopSize = desktopPane.getSize();
    Dimension d = new Dimension(desiredSize);
    int tx = desktopSize.width - d.width;
    int ty = desktopSize.height - d.height;
    if (tx < 0) {
        tx = 0;
        d.width = desktopSize.width;
    } else {
        tx = (int) (randomUtil.nextDouble() * tx);
    }
    if (ty < 0) {
        ty = 0;
        d.height = desktopSize.height;
    } else {
        ty = (int) (randomUtil.nextDouble() * ty);
    }
    frame.setBounds(tx, ty, d.width, d.height);
}
Also used : RandomUtil(edu.cmu.tetrad.util.RandomUtil) Dimension(java.awt.Dimension) Point(java.awt.Point)

Aggregations

RandomUtil (edu.cmu.tetrad.util.RandomUtil)25 Test (org.junit.Test)14 Node (edu.cmu.tetrad.graph.Node)13 LinkedList (java.util.LinkedList)10 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)8 DataSet (edu.cmu.tetrad.data.DataSet)8 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)5 ArrayList (java.util.ArrayList)5 SemIm (edu.cmu.tetrad.sem.SemIm)2 SemPm (edu.cmu.tetrad.sem.SemPm)2 StandardizedSemIm (edu.cmu.tetrad.sem.StandardizedSemIm)2 TetradVector (edu.cmu.tetrad.util.TetradVector)2 DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)1 Graph (edu.cmu.tetrad.graph.Graph)1 Dimension (java.awt.Dimension)1 Point (java.awt.Point)1 HashSet (java.util.HashSet)1