use of edu.cmu.tetrad.util.dist.Uniform in project tetrad by cmu-phil.
the class TestLingamPattern method test1.
@Test
public void test1() {
RandomUtil.getInstance().setSeed(4938492L);
int sampleSize = 1000;
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < 6; i++) {
nodes.add(new ContinuousVariable("X" + (i + 1)));
}
Graph graph = new Dag(GraphUtils.randomGraph(nodes, 0, 6, 4, 4, 4, false));
List<Distribution> variableDistributions = new ArrayList<>();
variableDistributions.add(new Normal(0, 1));
variableDistributions.add(new Normal(0, 1));
variableDistributions.add(new Normal(0, 1));
variableDistributions.add(new Uniform(-1, 1));
variableDistributions.add(new Normal(0, 1));
variableDistributions.add(new Normal(0, 1));
SemPm semPm = new SemPm(graph);
SemIm semIm = new SemIm(semPm);
DataSet dataSet = simulateDataNonNormal(semIm, sampleSize, variableDistributions);
Score score = new SemBicScore(new CovarianceMatrixOnTheFly(dataSet));
Graph estPattern = new Fges(score).search();
LingamPattern lingam = new LingamPattern(estPattern, dataSet);
lingam.search();
double[] pvals = lingam.getPValues();
double[] expectedPVals = { 0.18, 0.29, 0.88, 0.00, 0.01, 0.58 };
for (int i = 0; i < pvals.length; i++) {
assertEquals(expectedPVals[i], pvals[i], 0.01);
}
}
use of edu.cmu.tetrad.util.dist.Uniform 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;
}
use of edu.cmu.tetrad.util.dist.Uniform 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);
}
Aggregations