use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class SemBicScoreDeterministic method getMinimalLinearlyDependentSet.
private int[] getMinimalLinearlyDependentSet(int i, int[] parents, ICovarianceMatrix cov) {
double small = getDeterminismThreshold();
List<Node> _parents = new ArrayList<>();
for (int p : parents) _parents.add(variables.get(p));
DepthChoiceGenerator gen = new DepthChoiceGenerator(_parents.size(), _parents.size());
int[] choice;
while ((choice = gen.next()) != null) {
int[] sel = new int[choice.length];
List<Node> _sel = new ArrayList<>();
for (int m = 0; m < choice.length; m++) {
sel[m] = parents[m];
_sel.add(variables.get(sel[m]));
}
TetradMatrix m = cov.getSelection(sel, sel);
double s2 = getCovariances().getValue(i, i);
TetradMatrix covxx = getSelection(getCovariances(), parents, parents);
TetradVector covxy = getSelection(getCovariances(), parents, new int[] { i }).getColumn(0);
s2 -= covxx.inverse().times(covxy).dotProduct(covxy);
if (s2 <= small) {
out.println("### Linear dependence among variables: " + _sel);
out.println("### Removing " + _sel.get(0));
return sel;
}
try {
m.inverse();
} catch (Exception e2) {
// forbidden.add(sel[0]);
out.println("### Linear dependence among variables: " + _sel);
out.println("### Removing " + _sel.get(0));
return sel;
}
}
return new int[0];
}
use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class SemBicScoreDeterministic method localScore.
/**
* Calculates the sample likelihood and BIC score for i given its parents in a simple SEM model
*/
public double localScore(int i, int... parents) {
for (int p : parents) if (forbidden.contains(p))
return Double.NaN;
double small = getDeterminismThreshold();
double s2 = getCovariances().getValue(i, i);
int p = parents.length;
TetradMatrix covxx = getSelection(getCovariances(), parents, parents);
TetradVector covxy = getSelection(getCovariances(), parents, new int[] { i }).getColumn(0);
try {
s2 -= covxx.inverse().times(covxy).dotProduct(covxy);
} catch (SingularMatrixException e) {
s2 = 0;
}
// System.out.println(s2);
int n = getSampleSize();
int k = 2 * p + 1;
if (s2 < small) {
s2 = 0;
}
if (s2 == 0) {
printDeterminism(i, parents);
return Double.NaN;
}
return -(n) * log(s2) - getPenaltyDiscount() * k * log(n);
}
use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class ShortTriangularMatrix method becomeCorrelationMatrix.
public void becomeCorrelationMatrix(DataSet dataSet) {
for (int i = 0; i < dataSet.getNumColumns(); i++) matrix[i][i] = 10000;
TetradMatrix doubleData = dataSet.getDoubleData();
TetradVector[] views = new TetradVector[dataSet.getNumColumns()];
for (int i = 0; i < views.length; i++) views[i] = doubleData.getColumn(i);
for (int i = 0; i < dataSet.getNumColumns() - 1; i++) for (int j = i + 1; j < dataSet.getNumColumns(); j++) matrix[j][i] = StatUtils.compressedCorrelation(views[i], views[j]);
}
use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class KMeans method toString.
/**
* @return a string representation of the cluster result.
*/
public String toString() {
NumberFormat n1 = NumberFormatUtil.getInstance().getNumberFormat();
TetradVector counts = countClusterSizes();
double totalSquaredError = totalSquaredError();
StringBuilder buf = new StringBuilder();
buf.append("Cluster Result (").append(clusters.size()).append(" cases, ").append(centers.columns()).append(" feature(s), ").append(centers.rows()).append(" clusters)");
for (int k = 0; k < centers.rows(); k++) {
buf.append("\n\tCluster #").append(k + 1).append(": n = ").append(counts.get(k));
buf.append(" Squared Error = ").append(n1.format(squaredError(k)));
}
buf.append("\n\tTotal Squared Error = ").append(n1.format(totalSquaredError));
return buf.toString();
}
use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class KMeans method reassignPoints.
// ==========================PRIVATE METHODS=========================//
private int reassignPoints() {
int numChanged = 0;
for (int i = 0; i < data.rows(); i++) {
TetradVector datum = data.getRow(i);
double minDissimilarity = Double.POSITIVE_INFINITY;
int cluster = -1;
for (int k = 0; k < centers.rows(); k++) {
TetradVector center = centers.getRow(k);
double dissimilarity = getMetric().dissimilarity(datum, center);
if (dissimilarity < minDissimilarity) {
minDissimilarity = dissimilarity;
cluster = k;
}
}
if (cluster != clusters.get(i)) {
clusters.set(i, cluster);
numChanged++;
}
}
// System.out.println("Moved " + numChanged + " points.");
return numChanged;
}
Aggregations