use of edu.cmu.tetrad.calculator.expression.Context in project tetrad by cmu-phil.
the class TestExpressionParser method test5.
// Test distribution means.
@Test
public void test5() {
final Map<String, Double> values = new HashMap<>();
Context context = new Context() {
public Double getValue(String var) {
return values.get(var);
}
};
Map<String, Double> formulas = new LinkedHashMap<>();
formulas.put("ChiSquare(1)", 1.0);
formulas.put("Gamma(2, .5)", 1.0);
formulas.put("Beta(1, 2)", 0.33);
formulas.put("Normal(2, 3)", 2.0);
formulas.put("N(2, 3)", 2.0);
formulas.put("StudentT(5)", 0.0);
formulas.put("U(0, 1)", 0.5);
formulas.put("Uniform(0, 1)", 0.5);
formulas.put("Split(0, 1, 5, 6)", 3.0);
ExpressionParser parser = new ExpressionParser();
try {
for (String formula : formulas.keySet()) {
Expression expression = parser.parseExpression(formula);
double sum = 0.0;
int sampleSize = 10000;
for (int i = 0; i < sampleSize; i++) {
double value = expression.evaluate(context);
sum += value;
}
double mean = sum / sampleSize;
assertEquals(formulas.get(formula), mean, 0.1);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
use of edu.cmu.tetrad.calculator.expression.Context in project tetrad by cmu-phil.
the class GeneralizedSemIm method simulateDataNSteps.
public DataSet simulateDataNSteps(int sampleSize, boolean latentDataSaved) {
final Map<String, Double> variableValues = new HashMap<>();
List<Node> continuousVariables = new LinkedList<>();
final List<Node> variableNodes = pm.getVariableNodes();
// Work with a copy of the variables, because their type can be set externally.
for (Node node : variableNodes) {
ContinuousVariable var = new ContinuousVariable(node.getName());
var.setNodeType(node.getNodeType());
if (var.getNodeType() != NodeType.ERROR) {
continuousVariables.add(var);
}
}
DataSet fullDataSet = new ColtDataSet(sampleSize, continuousVariables);
final Context context = new Context() {
public Double getValue(String term) {
Double value = parameterValues.get(term);
if (value != null) {
return value;
}
value = variableValues.get(term);
if (value != null) {
return value;
}
throw new IllegalArgumentException("No value recorded for '" + term + "'");
}
};
// Do the simulation.
ROW: for (int row = 0; row < sampleSize; row++) {
// Take random draws from error distributions.
for (Node variable : variableNodes) {
Node error = pm.getErrorNode(variable);
if (error == null) {
throw new NullPointerException();
}
Expression expression = pm.getNodeExpression(error);
double value = expression.evaluate(context);
if (Double.isNaN(value)) {
throw new IllegalArgumentException("Undefined value for expression: " + expression);
}
variableValues.put(error.getName(), value);
}
// Set the variable nodes to zero.
for (Node variable : variableNodes) {
// RandomUtil.getInstance().nextUniform(-5, 5));
variableValues.put(variable.getName(), 0.0);
}
for (int m = 0; m < 1; m++) {
double[] values = new double[variableNodes.size()];
for (int i = 0; i < values.length; i++) {
Node node = variableNodes.get(i);
Expression expression = pm.getNodeExpression(node);
double value = expression.evaluate(context);
if (Double.isNaN(value)) {
throw new IllegalArgumentException("Undefined value for expression: " + expression);
}
values[i] = value;
}
for (double value : values) {
if (value == Double.POSITIVE_INFINITY || value == Double.NEGATIVE_INFINITY) {
row--;
continue ROW;
}
}
for (int i = 0; i < variableNodes.size(); i++) {
variableValues.put(variableNodes.get(i).getName(), values[i]);
}
}
for (int i = 0; i < variableNodes.size(); i++) {
double value = variableValues.get(variableNodes.get(i).getName());
fullDataSet.setDouble(row, i, value);
}
}
if (latentDataSaved) {
return fullDataSet;
} else {
return DataUtils.restrictToMeasured(fullDataSet);
}
}
use of edu.cmu.tetrad.calculator.expression.Context in project tetrad by cmu-phil.
the class GeneralizedSemIm method simulateDataMinimizeSurface.
public DataSet simulateDataMinimizeSurface(int sampleSize, boolean latentDataSaved) {
final Map<String, Double> variableValues = new HashMap<>();
List<Node> continuousVariables = new LinkedList<>();
final List<Node> variableNodes = pm.getVariableNodes();
// Work with a copy of the variables, because their type can be set externally.
for (Node node : variableNodes) {
ContinuousVariable var = new ContinuousVariable(node.getName());
var.setNodeType(node.getNodeType());
if (var.getNodeType() != NodeType.ERROR) {
continuousVariables.add(var);
}
}
DataSet fullDataSet = new ColtDataSet(sampleSize, continuousVariables);
final Context context = new Context() {
public Double getValue(String term) {
Double value = parameterValues.get(term);
if (value != null) {
return value;
}
value = variableValues.get(term);
if (value != null) {
return value;
}
throw new IllegalArgumentException("No value recorded for '" + term + "'");
}
};
final double[] _metric = new double[1];
MultivariateFunction function = new MultivariateFunction() {
double metric;
public double value(double[] doubles) {
for (int i = 0; i < variableNodes.size(); i++) {
variableValues.put(variableNodes.get(i).getName(), doubles[i]);
}
double[] image = new double[doubles.length];
for (int i = 0; i < variableNodes.size(); i++) {
Node node = variableNodes.get(i);
Expression expression = pm.getNodeExpression(node);
image[i] = expression.evaluate(context);
if (Double.isNaN(image[i])) {
throw new IllegalArgumentException("Undefined value for expression " + expression);
}
}
metric = 0.0;
for (int i = 0; i < variableNodes.size(); i++) {
double diff = doubles[i] - image[i];
metric += diff * diff;
}
for (int i = 0; i < variableNodes.size(); i++) {
variableValues.put(variableNodes.get(i).getName(), image[i]);
}
_metric[0] = metric;
return metric;
}
};
MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
// Do the simulation.
ROW: for (int row = 0; row < sampleSize; row++) {
// Take random draws from error distributions.
for (Node variable : variableNodes) {
Node error = pm.getErrorNode(variable);
if (error == null) {
throw new NullPointerException();
}
Expression expression = pm.getNodeExpression(error);
double value = expression.evaluate(context);
if (Double.isNaN(value)) {
throw new IllegalArgumentException("Undefined value for expression: " + expression);
}
variableValues.put(error.getName(), value);
}
for (Node variable : variableNodes) {
// RandomUtil.getInstance().nextUniform(-5, 5));
variableValues.put(variable.getName(), 0.0);
}
while (true) {
double[] values = new double[variableNodes.size()];
for (int i = 0; i < values.length; i++) {
values[i] = variableValues.get(variableNodes.get(i).getName());
}
PointValuePair pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MINIMIZE, new MaxEval(100000));
values = pair.getPoint();
for (int i = 0; i < variableNodes.size(); i++) {
if (isSimulatePositiveDataOnly() && values[i] < 0) {
row--;
continue ROW;
}
if (!Double.isNaN(selfLoopCoef) && row > 0) {
values[i] += selfLoopCoef * fullDataSet.getDouble(row - 1, i);
}
variableValues.put(variableNodes.get(i).getName(), values[i]);
fullDataSet.setDouble(row, i, values[i]);
}
if (_metric[0] < 0.01) {
// while
break;
}
}
}
if (latentDataSaved) {
return fullDataSet;
} else {
return DataUtils.restrictToMeasured(fullDataSet);
}
}
use of edu.cmu.tetrad.calculator.expression.Context in project tetrad by cmu-phil.
the class GeneralizedSemIm method simulateTimeSeries.
private DataSet simulateTimeSeries(int sampleSize) {
SemGraph semGraph = new SemGraph(getSemPm().getGraph());
semGraph.setShowErrorTerms(true);
TimeLagGraph timeLagGraph = getSemPm().getGraph().getTimeLagGraph();
List<Node> variables = new ArrayList<>();
for (Node node : timeLagGraph.getLag0Nodes()) {
if (node.getNodeType() == NodeType.ERROR)
continue;
variables.add(new ContinuousVariable(timeLagGraph.getNodeId(node).getName()));
}
List<Node> lag0Nodes = timeLagGraph.getLag0Nodes();
for (Node node : new ArrayList<>(lag0Nodes)) {
if (node.getNodeType() == NodeType.ERROR) {
lag0Nodes.remove(node);
}
}
DataSet fullData = new ColtDataSet(sampleSize, variables);
Map<Node, Integer> nodeIndices = new HashMap<>();
for (int i = 0; i < lag0Nodes.size(); i++) {
nodeIndices.put(lag0Nodes.get(i), i);
}
Graph contemporaneousDag = timeLagGraph.subgraph(timeLagGraph.getLag0Nodes());
List<Node> tierOrdering = contemporaneousDag.getCausalOrdering();
for (Node node : new ArrayList<>(tierOrdering)) {
if (node.getNodeType() == NodeType.ERROR) {
tierOrdering.remove(node);
}
}
final Map<String, Double> variableValues = new HashMap<>();
Context context = new Context() {
public Double getValue(String term) {
Double value = parameterValues.get(term);
if (value != null) {
return value;
}
value = variableValues.get(term);
if (value != null) {
return value;
} else {
return RandomUtil.getInstance().nextNormal(0, 1);
}
}
};
ROW: for (int currentStep = 0; currentStep < sampleSize; currentStep++) {
for (Node node : tierOrdering) {
Expression expression = pm.getNodeExpression(node);
double value = expression.evaluate(context);
if (isSimulatePositiveDataOnly() && value < 0) {
currentStep--;
continue ROW;
}
int col = nodeIndices.get(node);
fullData.setDouble(currentStep, col, value);
variableValues.put(node.getName(), value);
}
for (Node node : lag0Nodes) {
TimeLagGraph.NodeId _id = timeLagGraph.getNodeId(node);
for (int lag = 1; lag <= timeLagGraph.getMaxLag(); lag++) {
Node _node = timeLagGraph.getNode(_id.getName(), lag);
int col = lag0Nodes.indexOf(node);
if (_node == null) {
continue;
}
if (currentStep - lag + 1 >= 0) {
double _value = fullData.getDouble((currentStep - lag + 1), col);
variableValues.put(_node.getName(), _value);
}
}
}
}
return fullData;
}
use of edu.cmu.tetrad.calculator.expression.Context in project tetrad by cmu-phil.
the class GeneralizedSemIm method simulateDataAvoidInfinity.
public DataSet simulateDataAvoidInfinity(int sampleSize, boolean latentDataSaved) {
final Map<String, Double> variableValues = new HashMap<>();
List<Node> continuousVariables = new LinkedList<>();
final List<Node> variableNodes = pm.getVariableNodes();
// Work with a copy of the variables, because their type can be set externally.
for (Node node : variableNodes) {
ContinuousVariable var = new ContinuousVariable(node.getName());
var.setNodeType(node.getNodeType());
if (var.getNodeType() != NodeType.ERROR) {
continuousVariables.add(var);
}
}
DataSet fullDataSet = new ColtDataSet(sampleSize, continuousVariables);
final Context context = new Context() {
public Double getValue(String term) {
Double value = parameterValues.get(term);
if (value != null) {
return value;
}
value = variableValues.get(term);
if (value != null) {
return value;
}
throw new IllegalArgumentException("No value recorded for '" + term + "'");
}
};
boolean allInRange = true;
// Do the simulation.
ROW: for (int row = 0; row < sampleSize; row++) {
// Take random draws from error distributions.
for (Node variable : variableNodes) {
Node error = pm.getErrorNode(variable);
if (error == null) {
throw new NullPointerException();
}
Expression expression = pm.getNodeExpression(error);
double value;
value = expression.evaluate(context);
if (Double.isNaN(value)) {
throw new IllegalArgumentException("Undefined value for expression: " + expression);
}
variableValues.put(error.getName(), value);
}
// Set the variable nodes to zero.
for (Node variable : variableNodes) {
Node error = pm.getErrorNode(variable);
Expression expression = pm.getNodeExpression(error);
double value = expression.evaluate(context);
if (Double.isNaN(value)) {
throw new IllegalArgumentException("Undefined value for expression: " + expression);
}
// value); //0.0; //RandomUtil.getInstance().nextUniform(-1, 1));
variableValues.put(variable.getName(), 0.0);
}
// Repeatedly update variable values until one of them hits infinity or negative infinity or
// convergence within delta.
double delta = 1e-10;
int count = -1;
while (++count < 5000) {
double[] values = new double[variableNodes.size()];
for (int i = 0; i < values.length; i++) {
Node node = variableNodes.get(i);
Expression expression = pm.getNodeExpression(node);
double value = expression.evaluate(context);
values[i] = value;
}
allInRange = true;
for (int i = 0; i < values.length; i++) {
Node node = variableNodes.get(i);
// outside of the bound (-1e6, 1e6), judge nonconvergence and pick another random starting point.
if (!(Math.abs(variableValues.get(node.getName()) - values[i]) < delta)) {
if (!(Math.abs(variableValues.get(node.getName())) < 1e6)) {
if (count < 1000) {
row--;
continue ROW;
}
}
allInRange = false;
break;
}
}
for (int i = 0; i < variableNodes.size(); i++) {
variableValues.put(variableNodes.get(i).getName(), values[i]);
}
if (allInRange) {
break;
}
}
if (!allInRange) {
if (count < 10000) {
row--;
System.out.println("Trying another starting point...");
continue;
} else {
System.out.println("Couldn't converge in simulation.");
for (int i = 0; i < variableNodes.size(); i++) {
fullDataSet.setDouble(row, i, Double.NaN);
}
return fullDataSet;
}
}
for (int i = 0; i < variableNodes.size(); i++) {
double value = variableValues.get(variableNodes.get(i).getName());
if (isSimulatePositiveDataOnly() && value < 0) {
row--;
continue ROW;
}
if (!Double.isNaN(selfLoopCoef) && row > 0) {
value += selfLoopCoef * fullDataSet.getDouble(row - 1, i);
}
fullDataSet.setDouble(row, i, value);
}
}
if (latentDataSaved) {
return fullDataSet;
} else {
return DataUtils.restrictToMeasured(fullDataSet);
}
}
Aggregations