use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class EmBayesProperties method logProbDataGivenStructure.
// =========================================PRIVATE METHODS===================================//
private double logProbDataGivenStructure() {
BayesIm bayesIm = this.estimator.estimate(bayesPm, dataSet);
BayesImProbs probs = new BayesImProbs(bayesIm);
List<Node> variables = bayesIm.getVariables();
System.out.println("E1 bayesIm : " + variables);
System.out.println("E2 data set : " + dataSet.getVariables());
DataSet reorderedDataSet = dataSet.subsetColumns(variables);
int n = reorderedDataSet.getNumRows();
int m = reorderedDataSet.getNumColumns();
double score = 0.0;
int[] _case = new int[m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
_case[j] = reorderedDataSet.getInt(i, j);
}
score += Math.log(probs.getCellProb(_case));
}
return score;
}
use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class DirichletBayesIm method simulateData.
/**
* Simulates a random sample with the number of cases equal to
* <code>sampleSize</code>.
*
* @param sampleSize the sample size.
* @param seed the random number generator seed allows you
* recreate the simulated data by passing in the same
* seed (so you don't have to store the sample data
* @param latentDataSaved true iff data for latent variables should be
* included in the simulated data set.
* @return the simulated sample as a DataSet.
*/
public DataSet simulateData(int sampleSize, long seed, boolean latentDataSaved) {
RandomUtil random = RandomUtil.getInstance();
long _seed = random.getSeed();
random.setSeed(seed);
DataSet dataSet = simulateData(sampleSize, latentDataSaved);
random.revertSeed(_seed);
return dataSet;
}
use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class DirichletBayesIm method simulateDataHelper.
/**
* Simulates a sample with the given sample size.
*
* @param sampleSize the sample size.
* @param randomUtil optional random number generator to use when
* creating the data
* @param latentDataSaved true iff data for latent variables should be
* saved.
* @return the simulated sample as a DataSet.
*/
private DataSet simulateDataHelper(int sampleSize, RandomUtil randomUtil, boolean latentDataSaved) {
int numMeasured = 0;
int[] map = new int[nodes.length];
List<Node> variables = new LinkedList<>();
for (int j = 0; j < nodes.length; j++) {
if (!latentDataSaved && nodes[j].getNodeType() != NodeType.MEASURED) {
continue;
}
int numCategories = bayesPm.getNumCategories(nodes[j]);
List<String> categories = new LinkedList<>();
for (int k = 0; k < numCategories; k++) {
categories.add(bayesPm.getCategory(nodes[j], k));
}
DiscreteVariable var = new DiscreteVariable(nodes[j].getName(), categories);
variables.add(var);
int index = ++numMeasured - 1;
map[index] = j;
}
DataSet dataSet = new ColtDataSet(sampleSize, variables);
constructSample(sampleSize, randomUtil, numMeasured, dataSet, map);
return dataSet;
}
use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class DataCellRenderer method checkValueAt.
/**
* @return true iff the given token is a legitimate value for the cell at
* (row, col) in the table.
*/
public boolean checkValueAt(String token, int col) {
if (col < getNumLeadingCols()) {
throw new IllegalArgumentException();
}
DataSet dataSet = getDataSet();
int dataCol = col - getNumLeadingCols();
if (dataCol < dataSet.getNumColumns()) {
Node variable = dataSet.getVariable(dataCol);
return ((Variable) variable).checkValue(token);
} else {
return true;
}
}
use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class DataCellRenderer method clearSelected.
public void clearSelected() {
TabularDataTable model = (TabularDataTable) getModel();
DataSet dataSet = model.getDataSet();
if (!getRowSelectionAllowed()) {
int[] selectedCols = getSelectedColumns();
TableCellEditor editor = getCellEditor();
if (editor != null) {
editor.stopCellEditing();
}
for (int i = selectedCols.length - 1; i >= 0; i--) {
if (selectedCols[i] < getNumLeadingCols()) {
continue;
}
int dataCol = selectedCols[i] - getNumLeadingCols();
if (dataCol >= dataSet.getNumColumns()) {
continue;
}
Node variable = dataSet.getVariable(dataCol);
Object missingValue = ((Variable) variable).getMissingValueMarker();
for (int j = 0; j < dataSet.getNumRows(); j++) {
dataSet.setObject(j, dataCol, missingValue);
}
}
} else if (!getColumnSelectionAllowed()) {
int[] selectedRows = getSelectedRows();
TableCellEditor editor = getCellEditor();
if (editor != null) {
editor.stopCellEditing();
}
for (int i = getColumnCount() - 1; i >= 0; i--) {
if (i < getNumLeadingCols()) {
continue;
}
String colName = (String) (getValueAt(1, i));
if (colName == null) {
continue;
}
int dataCol = i - getNumLeadingCols();
Node variable = dataSet.getVariable(dataCol);
Object missingValue = ((Variable) variable).getMissingValueMarker();
for (int j = selectedRows.length - 1; j >= 0; j--) {
if (selectedRows[j] < 2) {
continue;
}
if (selectedRows[j] > dataSet.getNumRows() + 1) {
continue;
}
dataSet.setObject(selectedRows[j] - 2, dataCol, missingValue);
}
}
} else {
int[] selectedRows = getSelectedRows();
int[] selectedCols = getSelectedColumns();
TableCellEditor editor = getCellEditor();
if (editor != null) {
editor.stopCellEditing();
}
for (int i = selectedCols.length - 1; i >= 0; i--) {
if (selectedCols[i] < getNumLeadingCols()) {
continue;
}
int dataCol = selectedCols[i] - getNumLeadingCols();
if (dataCol >= dataSet.getNumColumns()) {
continue;
}
String colName = (String) (getValueAt(1, selectedCols[i]));
if (colName == null) {
continue;
}
Node variable = dataSet.getVariable(dataCol);
Object missingValue = ((Variable) variable).getMissingValueMarker();
for (int j = selectedRows.length - 1; j >= 0; j--) {
if (selectedRows[j] < 2) {
continue;
}
if (selectedRows[j] > dataSet.getNumRows() + 1) {
continue;
}
dataSet.setObject(selectedRows[j] - 2, dataCol, missingValue);
}
}
}
firePropertyChange("modelChanged", null, null);
model.fireTableDataChanged();
}
Aggregations