Search in sources :

Example 1 with Split

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

the class SemIm method initialValue.

/**
 * @return a random value from the appropriate distribution for the given
 * parameter.
 */
private double initialValue(Parameter parameter) {
    if (!getSemPm().getParameters().contains(parameter)) {
        throw new IllegalArgumentException("Not a parameter for this SEM: " + parameter);
    }
    if (parameter.isInitializedRandomly()) {
        if (parameter.getType() == ParamType.COEF) {
            final double coefLow = getParams().getDouble("coefLow", .5);
            final double coefHigh = getParams().getDouble("coefHigh", 1.5);
            double value = new Split(coefLow, coefHigh).nextRandom();
            if (getParams().getBoolean("coefSymmetric", true)) {
                return value;
            } else {
                return Math.abs(value);
            }
        } else if (parameter.getType() == ParamType.COVAR) {
            final double covLow = getParams().getDouble("covLow", 0.1);
            final double covHigh = getParams().getDouble("covHigh", 0.2);
            double value = new Split(covLow, covHigh).nextRandom();
            if (getParams().getBoolean("covSymmetric", true)) {
                return value;
            } else {
                return Math.abs(value);
            }
        } else {
            // if (parameter.getType() == ParamType.VAR) {
            return RandomUtil.getInstance().nextUniform(getParams().getDouble("varLow", 1), getParams().getDouble("varHigh", 3));
        }
    } else {
        return parameter.getStartingValue();
    }
}
Also used : Split(edu.cmu.tetrad.util.dist.Split)

Example 2 with Split

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

the class LargeScaleSimulation method setupModel.

private void setupModel(int size) {
    if (alreadySetUp) {
        return;
    }
    Map<Node, Integer> nodesHash = new HashedMap<>();
    for (int i = 0; i < variableNodes.size(); i++) {
        nodesHash.put(variableNodes.get(i), i);
    }
    this.parents = new int[size][];
    this.coefs = new double[size][];
    this.errorVars = new double[size];
    this.means = new double[size];
    for (int i = 0; i < size; i++) {
        this.parents[i] = new int[0];
        this.coefs[i] = new double[0];
    }
    Distribution edgeCoefDist = new Split(coefLow, coefHigh);
    Distribution errorCovarDist = new Uniform(varLow, varHigh);
    Distribution meanDist = new Uniform(meanLow, meanHigh);
    for (Edge edge : graph.getEdges()) {
        Node tail = Edges.getDirectedEdgeTail(edge);
        Node head = Edges.getDirectedEdgeHead(edge);
        int _tail = nodesHash.get(tail);
        int _head = nodesHash.get(head);
        int[] parents = this.parents[_head];
        int[] newParents = new int[parents.length + 1];
        System.arraycopy(parents, 0, newParents, 0, parents.length);
        newParents[newParents.length - 1] = _tail;
        double[] coefs = this.coefs[_head];
        double[] newCoefs = new double[coefs.length + 1];
        System.arraycopy(coefs, 0, newCoefs, 0, coefs.length);
        double coef = edgeCoefDist.nextRandom();
        if (includePositiveCoefs && !includeNegativeCoefs) {
            coef = Math.abs(coef);
        } else if (!includePositiveCoefs && includeNegativeCoefs) {
            coef = -Math.abs(coef);
        } else if (!includePositiveCoefs && !includeNegativeCoefs) {
            coef = 0;
        }
        newCoefs[newCoefs.length - 1] = coef;
        this.parents[_head] = newParents;
        this.coefs[_head] = newCoefs;
    }
    if (graph instanceof TimeLagGraph) {
        TimeLagGraph lagGraph = (TimeLagGraph) graph;
        // TimeSeriesUtils.getKnowledge(lagGraph);
        IKnowledge knowledge = getKnowledge(lagGraph);
        List<Node> lag0 = lagGraph.getLag0Nodes();
        for (Node y : lag0) {
            List<Node> _parents = lagGraph.getParents(y);
            for (Node x : _parents) {
                List<List<Node>> similar = returnSimilarPairs(x, y, knowledge);
                int _x = variableNodes.indexOf(x);
                int _y = variableNodes.indexOf(y);
                double first = Double.NaN;
                for (int i = 0; i < parents[_y].length; i++) {
                    if (_x == parents[_y][i]) {
                        first = coefs[_y][i];
                    }
                }
                for (int j = 0; j < similar.get(0).size(); j++) {
                    int _xx = variableNodes.indexOf(similar.get(0).get(j));
                    int _yy = variableNodes.indexOf(similar.get(1).get(j));
                    for (int i = 0; i < parents[_yy].length; i++) {
                        if (_xx == parents[_yy][i]) {
                            coefs[_yy][i] = first;
                        }
                    }
                }
            }
        }
    }
    for (int i = 0; i < size; i++) {
        this.errorVars[i] = errorCovarDist.nextRandom();
        this.means[i] = meanDist.nextRandom();
    }
    alreadySetUp = true;
}
Also used : Uniform(edu.cmu.tetrad.util.dist.Uniform) Distribution(edu.cmu.tetrad.util.dist.Distribution) HashedMap(org.apache.commons.collections4.map.HashedMap) Split(edu.cmu.tetrad.util.dist.Split)

Example 3 with Split

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

the class SemPm method initializeParams.

private void initializeParams() {
    // Note that a distinction between parameterizable and non-parameterizable nodes is being added
    // to accomodate time lag graphs. jdramsey 4/14/10.
    List<Parameter> parameters = new ArrayList<>();
    Set<Edge> edges = graph.getEdges();
    Collections.sort(new ArrayList<>(edges), new Comparator<Edge>() {

        public int compare(Edge o1, Edge o2) {
            int compareFirst = o1.getNode1().getName().compareTo(o2.getNode1().toString());
            int compareSecond = o1.getNode2().getName().compareTo(o2.getNode2().toString());
            if (compareFirst != 0) {
                return compareFirst;
            }
            if (compareSecond != 0) {
                return compareSecond;
            }
            return 0;
        }
    });
    // aren't error edges *and are into parameterizable node* (the last bit jdramsey 4/14/10).
    for (Edge edge : edges) {
        if (edge.getNode1() == edge.getNode2()) {
            continue;
        // throw new IllegalStateException("There should not be any" +
        // "edges from a node to itself in a SemGraph: " + edge);
        }
        if (!SemGraph.isErrorEdge(edge) && edge.getEndpoint1() == Endpoint.TAIL && edge.getEndpoint2() == Endpoint.ARROW) {
            if (!graph.isParameterizable(edge.getNode2())) {
                continue;
            }
            Parameter param = new Parameter(newBName(), ParamType.COEF, edge.getNode1(), edge.getNode2());
            param.setDistribution(new Split(0.5, 1.5));
            // param.setDistribution(new SplitDistributionSpecial(0.5, 1.5));
            // param.setDistribution(new UniformDistribution(-0.2, 0.2));
            // param.setDistribution(coefDistribution);
            parameters.add(param);
        }
    }
    // Add error variance freeParameters for exogenous nodes of all *parameterizable* nodes.
    for (Node node : getVariableNodes()) {
        if (!graph.isParameterizable(node)) {
            continue;
        }
        Parameter param = new Parameter(newTName(), ParamType.VAR, node, node);
        param.setDistribution(new Uniform(1.0, 3.0));
        parameters.add(param);
    }
    // connect exogenous nodes.)
    for (Edge edge : edges) {
        if (Edges.isBidirectedEdge(edge)) {
            Node node1 = edge.getNode1();
            Node node2 = edge.getNode2();
            // no...
            if (!graph.isParameterizable(node1) || !graph.isParameterizable(node2)) {
                continue;
            }
            node1 = getGraph().getVarNode(node1);
            node2 = getGraph().getVarNode(node2);
            Parameter param = new Parameter(newTName(), ParamType.COVAR, node1, node2);
            param.setDistribution(new SingleValue(0.2));
            parameters.add(param);
        }
    }
    // Add mean freeParameters for all parameterizable nodes.
    for (Node node : getVariableNodes()) {
        if (!graph.isParameterizable(node)) {
            continue;
        }
        Parameter mean = new Parameter(newMName(), ParamType.MEAN, node, node);
        mean.setDistribution(new Normal(0.0, 1.0));
        parameters.add(mean);
    }
    this.parameters = Collections.unmodifiableList(parameters);
}
Also used : SingleValue(edu.cmu.tetrad.util.dist.SingleValue) Uniform(edu.cmu.tetrad.util.dist.Uniform) Split(edu.cmu.tetrad.util.dist.Split) Normal(edu.cmu.tetrad.util.dist.Normal)

Aggregations

Split (edu.cmu.tetrad.util.dist.Split)3 Uniform (edu.cmu.tetrad.util.dist.Uniform)2 Distribution (edu.cmu.tetrad.util.dist.Distribution)1 Normal (edu.cmu.tetrad.util.dist.Normal)1 SingleValue (edu.cmu.tetrad.util.dist.SingleValue)1 HashedMap (org.apache.commons.collections4.map.HashedMap)1