Search in sources :

Example 11 with TetradMatrix

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

the class ColtDataSet method addVariable.

/**
 * Adds the given variable to the dataset, increasing the number of
 * columns by one, moving columns i >= <code>index</code> to column i + 1,
 * and inserting a column of missing values at column i.
 */
public final void addVariable(int index, Node variable) {
    if (variables.contains(variable)) {
        throw new IllegalArgumentException("Expecting a new variable.");
    }
    if (index < 0 || index > variables.size()) {
        throw new IndexOutOfBoundsException("Index must in (0, #vars).");
    }
    variables.add(index, variable);
    resize(tetradMatrix.rows(), variables.size());
    TetradMatrix _data = new TetradMatrix(tetradMatrix.rows(), tetradMatrix.columns() + 1);
    for (int j = 0; j < tetradMatrix.columns() + 1; j++) {
        if (j < index) {
            for (int i = 0; i < tetradMatrix.rows(); i++) {
                _data.set(i, j, tetradMatrix.get(i, j));
            }
        } else if (j == index) {
            for (int i = 0; i < tetradMatrix.rows(); i++) {
                _data.set(i, j, Double.NaN);
            }
        } else {
            for (int i = 0; i < tetradMatrix.rows(); i++) {
                _data.set(i, j, tetradMatrix.get(i, j - 1));
            }
        }
    }
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 12 with TetradMatrix

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

the class ColtDataSet method permuteRows.

/**
 * Randomly permutes the rows of the dataset.
 */
public void permuteRows() {
    List<Integer> permutation = new ArrayList<>();
    for (int i = 0; i < getNumRows(); i++) {
        permutation.add(i);
    }
    Collections.shuffle(permutation);
    TetradMatrix data2 = tetradMatrix.like();
    for (int i = 0; i < getNumRows(); i++) {
        for (int j = 0; j < getNumColumns(); j++) {
            data2.set(i, j, tetradMatrix.get(permutation.get(i), j));
        }
    }
    this.tetradMatrix = data2;
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 13 with TetradMatrix

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

the class ColtDataSet method resize.

/**
 * Resizes the tetradMatrix to the given dimensions. Data that does not fall within
 * the new dimensions is lost, and positions in the redimensioned tetradMatrix that
 * have no correlates in the old tetradMatrix are set to missing (that is,
 * Double.NaN).
 *
 * @param rows The number of rows in the redimensioned tetradMatrix.
 * @param cols The number of columns in the redimensioned tetradMatrix.
 */
private void resize(int rows, int cols) {
    TetradMatrix _data = new TetradMatrix(rows, cols);
    for (int i = 0; i < _data.rows(); i++) {
        for (int j = 0; j < _data.columns(); j++) {
            if (i < tetradMatrix.rows() && j < tetradMatrix.columns()) {
                _data.set(i, j, tetradMatrix.get(i, j));
            } else {
                _data.set(i, j, Double.NaN);
            }
        }
    }
    tetradMatrix = _data;
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 14 with TetradMatrix

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

the class ColtDataSet method subsetColumns.

/**
 * Creates and returns a dataset consisting of those variables in the list
 * vars.  Vars must be a subset of the variables of this DataSet. The
 * ordering of the elements of vars will be the same as in the list of
 * variables in this DataSet.
 */
public final DataSet subsetColumns(List<Node> vars) {
    // if (vars.isEmpty()) {
    // throw new IllegalArgumentException("Subset must not be empty.");
    // }
    vars = GraphUtils.replaceNodes(vars, getVariables());
    if (!(getVariables().containsAll(vars))) {
        List<Node> missingVars = new ArrayList<>(vars);
        missingVars.removeAll(getVariables());
        throw new IllegalArgumentException("All vars must be original vars: " + missingVars);
    }
    int[] rows = new int[tetradMatrix.rows()];
    for (int i = 0; i < rows.length; i++) {
        rows[i] = i;
    }
    int[] columns = new int[vars.size()];
    for (int j = 0; j < columns.length; j++) {
        columns[j] = getVariables().indexOf(vars.get(j));
    }
    TetradMatrix _data = tetradMatrix.getSelection(rows, columns).copy();
    ColtDataSet _dataSet = new ColtDataSet(0, new LinkedList<Node>());
    _dataSet.tetradMatrix = _data;
    // _dataSet.name = name + "_copy";
    _dataSet.variables = vars;
    _dataSet.selection = new HashSet<>();
    // _dataSet.multipliers = new HashMap<>(multipliers);
    // Might have to delete some knowledge.
    _dataSet.knowledge = knowledge.copy();
    return _dataSet;
}
Also used : Node(edu.cmu.tetrad.graph.Node) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 15 with TetradMatrix

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

the class NumberObjectDataSet method getCovarianceMatrix.

/**
 * @return the covariance matrix for this dataset. Defers to
 * <code>Statistic.covariance()</code> in the COLT matrix library, so it
 * inherits the handling of missing values from that library--that is, any
 * covariance involving a column with a missing value is Double.NaN. If
 * that's not the desired behavior, missing values can be removed or imputed
 * first.
 */
public final TetradMatrix getCovarianceMatrix() {
    if (!isContinuous()) {
        throw new IllegalStateException("Not a continuous data set.");
    }
    TetradMatrix cov = new TetradMatrix(data[0].length, data[0].length);
    double[] x = new double[data.length];
    double[] y = new double[data.length];
    for (int i = 0; i < data[0].length; i++) {
        for (int j = 0; j < data[0].length; j++) {
            for (int k = 0; k < data.length; k++) {
                x[k] = data[k][i].doubleValue();
                y[k] = data[k][j].doubleValue();
                cov.set(i, j, StatUtils.covariance(x, y));
            }
        }
    }
    return cov;
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Aggregations

TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)161 TetradVector (edu.cmu.tetrad.util.TetradVector)46 ArrayList (java.util.ArrayList)43 Node (edu.cmu.tetrad.graph.Node)41 List (java.util.List)12 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)10 DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)9 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)9 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)8 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)8 Test (org.junit.Test)8 Regression (edu.cmu.tetrad.regression.Regression)7 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)7 SemIm (edu.cmu.tetrad.sem.SemIm)7 Graph (edu.cmu.tetrad.graph.Graph)6 SemPm (edu.cmu.tetrad.sem.SemPm)6 Vector (java.util.Vector)6 DoubleArrayList (cern.colt.list.DoubleArrayList)5 DataSet (edu.cmu.tetrad.data.DataSet)5 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)5