use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class Ling method nRookColumnAssignments.
private static List<List<Integer>> nRookColumnAssignments(TetradMatrix mat, List<Integer> availableRows) {
List<List<Integer>> concats = new ArrayList<>();
System.out.println("mat = " + mat);
int n = availableRows.size();
for (int i = 0; i < n; i++) {
int currentRowIndex = availableRows.get(i);
if (mat.get(currentRowIndex, 0) != 0) {
if (mat.columns() > 1) {
Vector<Integer> newAvailableRows = (new Vector<>(availableRows));
newAvailableRows.removeElement(currentRowIndex);
TetradMatrix subMat = mat.getPart(0, mat.rows() - 1, 1, mat.columns() - 2);
List<List<Integer>> allLater = nRookColumnAssignments(subMat, newAvailableRows);
for (List<Integer> laterPerm : allLater) {
laterPerm.add(0, currentRowIndex);
concats.add(laterPerm);
}
} else {
List<Integer> l = new ArrayList<>();
l.add(currentRowIndex);
concats.add(l);
}
}
}
return concats;
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class LingamPattern method getScore.
// =============================PRIVATE METHODS=========================//
private Score getScore(Graph dag, TetradMatrix data, List<Node> variables) {
// System.out.println("Scoring DAG: " + dag);
Regression regression = new RegressionDataset(data, variables);
List<Node> nodes = dag.getNodes();
double score = 0.0;
double[] pValues = new double[nodes.size()];
TetradMatrix residuals = new TetradMatrix(data.rows(), data.columns());
for (int i = 0; i < nodes.size(); i++) {
Node _target = nodes.get(i);
List<Node> _regressors = dag.getParents(_target);
Node target = getVariable(variables, _target.getName());
List<Node> regressors = new ArrayList<>();
for (Node _regressor : _regressors) {
Node variable = getVariable(variables, _regressor.getName());
regressors.add(variable);
}
RegressionResult result = regression.regress(target, regressors);
TetradVector residualsColumn = result.getResiduals();
// residuals.viewColumn(i).assign(residualsColumn);
residuals.assignColumn(i, residualsColumn);
DoubleArrayList residualsArray = new DoubleArrayList(residualsColumn.toArray());
double mean = Descriptive.mean(residualsArray);
double std = Descriptive.standardDeviation(Descriptive.variance(residualsArray.size(), Descriptive.sum(residualsArray), Descriptive.sumOfSquares(residualsArray)));
for (int i2 = 0; i2 < residualsArray.size(); i2++) {
residualsArray.set(i2, (residualsArray.get(i2) - mean) / std);
residualsArray.set(i2, Math.abs(residualsArray.get(i2)));
}
double _mean = Descriptive.mean(residualsArray);
double diff = _mean - Math.sqrt(2.0 / Math.PI);
score += diff * diff;
}
for (int j = 0; j < residuals.columns(); j++) {
double[] x = residuals.getColumn(j).toArray();
double p = new AndersonDarlingTest(x).getP();
pValues[j] = p;
}
return new Score(score, pValues);
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class MNLRLikelihood method getLik.
public double getLik(int child_index, int[] parents) {
double lik = 0;
Node c = variables.get(child_index);
List<ContinuousVariable> continuous_parents = new ArrayList<>();
List<DiscreteVariable> discrete_parents = new ArrayList<>();
for (int p : parents) {
Node parent = variables.get(p);
if (parent instanceof ContinuousVariable) {
continuous_parents.add((ContinuousVariable) parent);
} else {
discrete_parents.add((DiscreteVariable) parent);
}
}
int p = continuous_parents.size();
List<List<Integer>> cells = adTree.getCellLeaves(discrete_parents);
// List<List<Integer>> cells = partition(discrete_parents);
int[] continuousCols = new int[p];
for (int j = 0; j < p; j++) continuousCols[j] = nodesHash.get(continuous_parents.get(j));
for (List<Integer> cell : cells) {
int r = cell.size();
if (r > 1) {
double[] mean = new double[p];
double[] var = new double[p];
for (int i = 0; i < p; i++) {
for (int j = 0; j < r; j++) {
mean[i] += continuousData[continuousCols[i]][cell.get(j)];
var[i] += Math.pow(continuousData[continuousCols[i]][cell.get(j)], 2);
}
mean[i] /= r;
var[i] /= r;
var[i] -= Math.pow(mean[i], 2);
var[i] = Math.sqrt(var[i]);
if (Double.isNaN(var[i])) {
System.out.println(var[i]);
}
}
int degree = fDegree;
if (fDegree < 1) {
degree = (int) Math.floor(Math.log(r));
}
TetradMatrix subset = new TetradMatrix(r, p * degree + 1);
for (int i = 0; i < r; i++) {
subset.set(i, p * degree, 1);
for (int j = 0; j < p; j++) {
for (int d = 0; d < degree; d++) {
subset.set(i, p * d + j, Math.pow((continuousData[continuousCols[j]][cell.get(i)] - mean[j]) / var[j], d + 1));
}
}
}
if (c instanceof ContinuousVariable) {
TetradVector target = new TetradVector(r);
for (int i = 0; i < r; i++) {
target.set(i, continuousData[child_index][cell.get(i)]);
}
lik += multipleRegression(target, subset);
} else {
ArrayList<Integer> temp = new ArrayList<>();
TetradMatrix target = new TetradMatrix(r, ((DiscreteVariable) c).getNumCategories());
for (int i = 0; i < r; i++) {
for (int j = 0; j < ((DiscreteVariable) c).getNumCategories(); j++) {
target.set(i, j, -1);
}
target.set(i, discreteData[child_index][cell.get(i)], 1);
}
lik += MultinomialLogisticRegression(target, subset);
}
}
}
return lik;
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class Lingam method estimateCausalOrder.
// ================================PUBLIC METHODS========================//
private CausalOrder estimateCausalOrder(DataSet dataSet) {
TetradMatrix X = dataSet.getDoubleData();
FastIca fastIca = new FastIca(X, 30);
fastIca.setVerbose(false);
FastIca.IcaResult result = fastIca.findComponents();
TetradMatrix W = result.getW().transpose();
System.out.println("W = " + W);
PermutationGenerator gen1 = new PermutationGenerator(W.rows());
int[] perm1 = new int[0];
double sum1 = Double.POSITIVE_INFINITY;
int[] choice1;
while ((choice1 = gen1.next()) != null) {
double sum = 0.0;
for (int i = 0; i < W.rows(); i++) {
final double c = W.get(i, choice1[i]);
sum += 1.0 / abs(c);
}
if (sum < sum1) {
sum1 = sum;
perm1 = Arrays.copyOf(choice1, choice1.length);
}
}
TetradMatrix WTilde = W.getSelection(perm1, perm1);
System.out.println("WTilde before normalization = " + WTilde);
for (int j = 0; j < WTilde.columns(); j++) {
for (int i = j; i < WTilde.rows(); i++) {
WTilde.set(i, j, WTilde.get(i, j) / WTilde.get(j, j));
}
}
System.out.println("WTilde after normalization = " + WTilde);
final int m = dataSet.getNumColumns();
TetradMatrix B = TetradMatrix.identity(m).minus(WTilde.transpose());
System.out.println("B = " + B);
PermutationGenerator gen2 = new PermutationGenerator(B.rows());
int[] perm2 = new int[0];
double sum2 = Double.POSITIVE_INFINITY;
int[] choice2;
while ((choice2 = gen2.next()) != null) {
double sum = 0.0;
for (int i = 0; i < W.rows(); i++) {
for (int j = i; j < W.rows(); j++) {
final double c = B.get(choice2[i], choice2[j]);
sum += c * c;
}
}
if (sum < sum2) {
sum2 = sum;
perm2 = Arrays.copyOf(choice2, choice2.length);
}
}
TetradMatrix BTilde = B.getSelection(perm2, perm2);
System.out.println("BTilde = " + BTilde);
return new CausalOrder(perm2);
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class LingamPattern2 method getScore.
// Return the average score.
private Score getScore(Graph dag, List<TetradMatrix> data, List<Node> variables) {
// System.out.println("Scoring DAG: " + dag);
int totalSampleSize = 0;
for (TetradMatrix _data : data) {
totalSampleSize += _data.rows();
}
int numCols = data.get(0).columns();
List<Node> nodes = dag.getNodes();
double score = 0.0;
double[] pValues = new double[nodes.size()];
TetradMatrix residuals = new TetradMatrix(totalSampleSize, numCols);
for (int j = 0; j < nodes.size(); j++) {
List<Double> _residuals = new ArrayList<>();
Node _target = nodes.get(j);
List<Node> _regressors = dag.getParents(_target);
Node target = getVariable(variables, _target.getName());
List<Node> regressors = new ArrayList<>();
for (Node _regressor : _regressors) {
Node variable = getVariable(variables, _regressor.getName());
regressors.add(variable);
}
for (int m = 0; m < data.size(); m++) {
RegressionResult result = regressions.get(m).regress(target, regressors);
TetradVector residualsSingleDataset = result.getResiduals();
DoubleArrayList _residualsSingleDataset = new DoubleArrayList(residualsSingleDataset.toArray());
double mean = Descriptive.mean(_residualsSingleDataset);
double std = Descriptive.standardDeviation(Descriptive.variance(_residualsSingleDataset.size(), Descriptive.sum(_residualsSingleDataset), Descriptive.sumOfSquares(_residualsSingleDataset)));
for (int i2 = 0; i2 < _residualsSingleDataset.size(); i2++) {
_residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2) - mean) / std);
}
for (int k = 0; k < _residualsSingleDataset.size(); k++) {
_residuals.add(_residualsSingleDataset.get(k));
}
DoubleArrayList f = new DoubleArrayList(_residualsSingleDataset.elements());
for (int k = 0; k < f.size(); k++) {
f.set(k, Math.abs(f.get(k)));
}
double _mean = Descriptive.mean(f);
double diff = _mean - Math.sqrt(2.0 / Math.PI);
score += diff * diff;
// score += andersonDarlingPASquareStar(target, dag.getParents(target));
}
for (int k = 0; k < _residuals.size(); k++) {
residuals.set(k, j, _residuals.get(k));
}
}
for (int j = 0; j < residuals.columns(); j++) {
double[] x = residuals.getColumn(j).toArray();
double p = new AndersonDarlingTest(x).getP();
pValues[j] = p;
}
return new Score(score, pValues);
}
Aggregations