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));
}
}
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations