use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class BooleanGlassSimulation method createData.
@Override
public void createData(Parameters parameters) {
this.graph = randomGraph.createGraph(parameters);
LagGraphParams params = new LagGraphParams(parameters);
params.setIndegree(2);
params.setMlag(1);
RandomActiveLagGraph _graph = new RandomActiveLagGraph(params);
BooleanGlassGenePm pm = new BooleanGlassGenePm(_graph);
BooleanGlassGeneIm im = new BooleanGlassGeneIm(pm, parameters);
DataModelList data = im.simulateData();
List<DataSet> dataSets = new ArrayList<>();
for (DataModel model : data) {
dataSets.add((DataSet) model);
}
this.dataSets = dataSets;
List<String> factors = new ArrayList<>(_graph.getFactors());
Map<String, Node> nodes = new HashMap<>();
for (String factor : factors) {
nodes.put(factor, new ContinuousVariable(factor));
}
TimeLagGraph graph = new TimeLagGraph();
graph.setMaxLag(_graph.getMaxLag());
for (String factor : factors) {
graph.addNode(nodes.get(factor));
}
for (String factor : factors) {
for (Object o : _graph.getParents(factor)) {
LaggedFactor laggedFactor = (LaggedFactor) o;
String _factor = laggedFactor.getFactor();
int lag = laggedFactor.getLag();
Node node1 = graph.getNode(_factor + ":" + lag);
Node node2 = graph.getNode(factor);
graph.addDirectedEdge(node1, node2);
}
}
topToBottomLayout(graph);
this.graph = graph;
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class GeneralSemSimulation method getPm.
private GeneralizedSemPm getPm(Graph graph, Parameters parameters) {
GeneralizedSemPm pm = new GeneralizedSemPm(graph);
List<Node> variablesNodes = pm.getVariableNodes();
List<Node> errorNodes = pm.getErrorNodes();
String measuredFunction = parameters.getString("generalSemFunctionTemplateMeasured");
String latentFunction = parameters.getString("generalSemFunctionTemplateLatent");
String error = parameters.getString("generalSemErrorTemplate");
try {
for (Node node : variablesNodes) {
if (node.getNodeType() == NodeType.LATENT) {
String _template = TemplateExpander.getInstance().expandTemplate(latentFunction, pm, node);
pm.setNodeExpression(node, _template);
} else {
String _template = TemplateExpander.getInstance().expandTemplate(measuredFunction, pm, node);
pm.setNodeExpression(node, _template);
}
}
for (Node node : errorNodes) {
String _template = TemplateExpander.getInstance().expandTemplate(error, pm, node);
pm.setNodeExpression(node, _template);
}
} catch (ParseException e) {
System.out.println(e);
}
return pm;
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class LinearSineSimulation method simulate.
private DataSet simulate(Graph G, Parameters parameters) {
HashMap<String, Integer> nd = new HashMap<>();
List<Node> nodes = G.getNodes();
Collections.shuffle(nodes);
if (this.shuffledOrder == null) {
List<Node> shuffledNodes = new ArrayList<>(nodes);
Collections.shuffle(shuffledNodes);
this.shuffledOrder = shuffledNodes;
}
for (int i = 0; i < nodes.size(); i++) {
nd.put(shuffledOrder.get(i).getName(), 0);
}
G = makeMixedGraph(G, nd);
nodes = G.getNodes();
DataSet mixedData = new BoxDataSet(new MixedDataBox(nodes, parameters.getInt("sampleSize")), nodes);
List<Node> tierOrdering = G.getCausalOrdering();
int[] tiers = new int[tierOrdering.size()];
for (int t = 0; t < tierOrdering.size(); t++) {
tiers[t] = nodes.indexOf(tierOrdering.get(t));
}
for (int mixedIndex : tiers) {
ContinuousVariable child = (ContinuousVariable) nodes.get(mixedIndex);
ArrayList<ContinuousVariable> continuousParents = new ArrayList<>();
for (Node node : G.getParents(child)) {
continuousParents.add((ContinuousVariable) node);
}
HashMap<String, double[]> intercept = new HashMap<>();
HashMap<String, double[]> linear = new HashMap<>();
HashMap<String, double[]> beta = new HashMap<>();
HashMap<String, double[]> gamma = new HashMap<>();
HashMap<String, double[]> bounds = new HashMap<>();
for (int j = 1; j <= continuousParents.size(); j++) {
String key = continuousParents.get(j - 1).toString();
if (!bounds.containsKey(key)) {
double m0 = mixedData.getDouble(0, mixedData.getColumn(continuousParents.get(j - 1)));
double m1 = mixedData.getDouble(0, mixedData.getColumn(continuousParents.get(j - 1)));
for (int i = 1; i < parameters.getInt("sampleSize"); i++) {
m0 = Math.min(m0, mixedData.getDouble(i, mixedData.getColumn(continuousParents.get(j - 1))));
m1 = Math.max(m1, mixedData.getDouble(i, mixedData.getColumn(continuousParents.get(j - 1))));
}
double[] temp = new double[3];
temp[0] = m0;
temp[1] = (m1 - m0) / 2;
temp[2] = m1;
bounds.put(key, temp);
}
}
double mean = 0;
double var = 0;
for (int i = 0; i < parameters.getInt("sampleSize"); i++) {
double[] parents = new double[continuousParents.size()];
double value = 0;
String key = "";
for (int j = 1; j <= continuousParents.size(); j++) parents[j - 1] = mixedData.getDouble(i, mixedData.getColumn(continuousParents.get(j - 1)));
if (!intercept.containsKey(key)) {
double[] interceptCoefficients = new double[1];
interceptCoefficients[0] = randSign() * RandomUtil.getInstance().nextUniform(interceptLow, interceptHigh);
intercept.put(key, interceptCoefficients);
}
if (!linear.containsKey(key) && !continuousParents.isEmpty()) {
double[] linearCoefficients = new double[parents.length];
for (int j = 0; j < parents.length; j++) linearCoefficients[j] = randSign() * RandomUtil.getInstance().nextUniform(linearLow, linearHigh);
linear.put(key, linearCoefficients);
}
if (!beta.containsKey(key) && !continuousParents.isEmpty()) {
double[] betaCoefficients = new double[parents.length];
for (int j = 0; j < parents.length; j++) betaCoefficients[j] = randSign() * RandomUtil.getInstance().nextUniform(betaLow, betaHigh);
beta.put(key, betaCoefficients);
}
if (!gamma.containsKey(key) && !continuousParents.isEmpty()) {
double[] gammaCoefficients = new double[parents.length];
for (int j = 0; j < parents.length; j++) {
String key2 = continuousParents.get(j).toString();
gammaCoefficients[j] = (bounds.get(key2)[1] - bounds.get(key2)[0]) / (2 * Math.PI * RandomUtil.getInstance().nextUniform(gammaLow, gammaHigh));
}
gamma.put(key, gammaCoefficients);
}
value += intercept.get(key)[0];
if (!continuousParents.isEmpty()) {
for (int x = 0; x < parents.length; x++) {
String key2 = continuousParents.get(x).toString();
value += linear.get(key)[x] * parents[x] + beta.get(key)[x] * Math.sin(parents[x] / (gamma.get(key)[x]));
}
}
mixedData.setDouble(i, mixedIndex, value);
mean += value;
var += Math.pow(value, 2);
}
if (continuousParents.size() == 0) {
var = 1;
} else {
mean /= mixedData.getNumRows();
var /= mixedData.getNumRows();
var -= Math.pow(mean, 2);
var = Math.sqrt(var);
}
double noiseVar = RandomUtil.getInstance().nextUniform(varLow, varHigh);
for (int i = 0; i < parameters.getInt("sampleSize"); i++) {
mixedData.setDouble(i, mixedIndex, mixedData.getDouble(i, mixedIndex) + var * RandomUtil.getInstance().nextNormal(0, noiseVar));
}
}
return mixedData;
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class GeneralSemSimulationSpecial1 method getPm.
private GeneralizedSemPm getPm(Graph graph) {
GeneralizedSemPm pm = new GeneralizedSemPm(graph);
List<Node> variablesNodes = pm.getVariableNodes();
List<Node> errorNodes = pm.getErrorNodes();
Map<String, String> paramMap = new HashMap<>();
String[] funcs = { "TSUM(NEW(B)*$)", "TSUM(NEW(B)*$+NEW(C)*sin(NEW(T)*$+NEW(A)))", "TSUM(NEW(B)*(.5*$ + .5*(sqrt(abs(NEW(b)*$+NEW(exoErrorType))) ) ) )" };
paramMap.put("s", "U(1,3)");
paramMap.put("B", "Split(-1.5,-.5,.5,1.5)");
paramMap.put("C", "Split(-1.5,-.5,.5,1.5)");
paramMap.put("T", "U(.5,1.5)");
paramMap.put("A", "U(0,.25)");
paramMap.put("exoErrorType", "U(-.5,.5)");
paramMap.put("funcType", "U(1,5)");
String nonlinearStructuralEdgesFunction = funcs[0];
String nonlinearFactorMeasureEdgesFunction = funcs[0];
try {
for (Node node : variablesNodes) {
if (node.getNodeType() == NodeType.LATENT) {
String _template = TemplateExpander.getInstance().expandTemplate(nonlinearStructuralEdgesFunction, pm, node);
pm.setNodeExpression(node, _template);
} else {
String _template = TemplateExpander.getInstance().expandTemplate(nonlinearFactorMeasureEdgesFunction, pm, node);
pm.setNodeExpression(node, _template);
}
}
for (Node node : errorNodes) {
String _template = TemplateExpander.getInstance().expandTemplate("U(-.5,.5)", pm, node);
pm.setNodeExpression(node, _template);
}
Set<String> parameters = pm.getParameters();
for (String parameter : parameters) {
for (String type : paramMap.keySet()) {
if (parameter.startsWith(type)) {
pm.setParameterExpression(parameter, paramMap.get(type));
}
}
}
} catch (ParseException e) {
System.out.println(e);
}
return pm;
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class LoadContinuousDataSmithSim method readGraph.
public Graph readGraph(File file) {
try {
DataReader reader = new DataReader();
reader.setVariablesSupplied(false);
reader.setDelimiter(DelimiterType.COMMA);
DataSet data = reader.parseTabular(file);
List<Node> variables = data.getVariables();
Graph graph = new EdgeListGraph(variables);
for (int i = 0; i < variables.size(); i++) {
for (int j = 0; j < variables.size(); j++) {
if (i == j)
continue;
if (data.getDouble(i, j) != 0) {
graph.addDirectedEdge(variables.get(i), variables.get(j));
}
}
}
return graph;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations