Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
Implements the CStaR algorithm (Stekhoven et al., 2012), which finds a CPDAG of that
* data and then tries all orientations of the undirected edges about a variable in the CPDAG to estimate a minimum
* bound on the effect for a given edge. Some references include the following:
*
*
Stekhoven, D. J., Moraes, I., Sveinbjörnsson, G., Hennig, L., Maathuis, M. H., and
* Bühlmann, P. (2012). Causal stability ranking. Bioinformatics, 28(21), 2819-2823.
*
*
Meinshausen, N., and Bühlmann, P. (2010). Stability selection. Journal of the Royal
* Statistical Society: Series B (Statistical Methodology), 72(4), 417-473.
*
*
Colombo, D., and Maathuis, M. H. (2014). Order-independent constraint-based causal
* structure learning. The Journal of Machine Learning Research, 15(1), 3741-3782.
*
*
This class is not configured to respect knowledge of forbidden and required
* edges.
*
* @author josephramsey
* @see Ida
*/
public class Cstar {
private boolean parallelized = false;
private int numSubsamples = 30;
private int topBracket = 5;
private double selectionAlpha = 0.0;
private final IndependenceWrapper test;
private final ScoreWrapper score;
private final Parameters parameters;
private CpdagAlgorithm cpdagAlgorithm = CpdagAlgorithm.PC_STABLE;
private SampleStyle sampleStyle = SampleStyle.SUBSAMPLE;
private boolean verbose;
private File newDir = null;
/**
* Constructor.
*/
public Cstar(IndependenceWrapper test, ScoreWrapper score, Parameters parameters) {
this.test = test;
this.score = score;
this.parameters = parameters;
}
/**
* Returns a list of records for making a CSTaR table.
*
* @param allRecords the list of all records.
* @return The list for the CSTaR table.
*/
public static LinkedList cStar(LinkedList> allRecords) {
Map> map = new HashMap<>();
for (List records : allRecords) {
for (Record record : records) {
Edge edge = Edges.directedEdge(record.getCauseNode(), record.getEffectNode());
map.computeIfAbsent(edge, k -> new ArrayList<>());
map.get(edge).add(record);
}
}
LinkedList cstar = new LinkedList<>();
for (Edge edge : map.keySet()) {
List recordList = map.get(edge);
double[] pis = new double[recordList.size()];
double[] effects = new double[recordList.size()];
for (int i = 0; i < recordList.size(); i++) {
pis[i] = recordList.get(i).getPi();
effects[i] = recordList.get(i).getMinBeta();
}
double medianPis = StatUtils.median(pis);
double medianEffects = StatUtils.median(effects);
Record record = new Record(edge.getNode1(), edge.getNode2(), medianPis, medianEffects, recordList.get(0).getNumCauses(), recordList.get(0).getNumEffects());
cstar.add(record);
}
cstar.sort((o1, o2) -> {
if (o1.getPi() == o2.getPi()) {
return Double.compare(o2.getMinBeta(), o1.getMinBeta());
} else {
return Double.compare(o2.getPi(), o1.getPi());
}
});
return cstar;
}
// Meinhausen and Buhlmann per comparison error rate (PCER) bound
private static double pcer(double pi, double q, double p) {
return (1.0 / ((2. * pi - 1.))) * ((q * q) / (p * p));
}
/**
* Sets whether the algorithm should be parallelized. Different runs of the algorithms can be run in different
* threads in parallel.
*
* @param parallelized True, if so.
*/
public void setParallelized(boolean parallelized) {
this.parallelized = parallelized;
}
/**
* Returns records for a set of variables with expected number of false positives bounded by q.
*
* @param dataSet The full datasets to search over.
* @param possibleCauses A set of variables in the datasets over which to search.
* @param possibleEffects The effect variables.
* @param path A path where interim results are to be stored. If null, interim results will not be
* stored. If the path is specified, then if the process is stopped and restarted, previously
* computed interim results will be loaded.
* @see Record
*/
public LinkedList> getRecords(DataSet dataSet, List possibleCauses, List possibleEffects, int topBracket, String path) {
if (topBracket < 1) {
throw new IllegalArgumentException("Top bracket must be at least 1.");
}
if (topBracket > possibleCauses.size()) {
throw new IllegalArgumentException("Top bracket (q) is too large; it is " + topBracket + " but the number of possible causes is " + possibleCauses.size());
}
this.topBracket = topBracket;
if (path == null || path.isEmpty()) {
path = "cstar-out";
TetradLogger.getInstance().forceLogMessage("Using path = 'cstar-out'.");
}
File origDir = null;
if (new File(path).exists()) {
origDir = new File(path);
}
File newDir;
int i;
for (i = 1; ; i++) {
if (!new File(path + "." + i).exists()) break;
}
path = path + "." + i;
newDir = new File(path);
if (origDir == null) {
origDir = newDir;
}
boolean made = newDir.mkdirs();
if (!made) {
throw new IllegalStateException("Could not make a new directory; perhaps file permissions need to be adjusted.");
}
TetradLogger.getInstance().forceLogMessage("Creating directories for " + newDir.getAbsolutePath());
newDir = new File(path);
TetradLogger.getInstance().forceLogMessage("Using files in directory " + origDir.getAbsolutePath());
this.newDir = newDir;
possibleEffects = GraphUtils.replaceNodes(possibleEffects, dataSet.getVariables());
possibleCauses = GraphUtils.replaceNodes(possibleCauses, dataSet.getVariables());
LinkedList> allRecords = new LinkedList<>();
TetradLogger.getInstance().forceLogMessage("Results directory = " + newDir.getAbsolutePath());
if (new File(origDir, "possible.causes.txt").exists() && new File(newDir, "possible.causes.txt").exists()) {
TetradLogger.getInstance().forceLogMessage("Loading data, possible causes, and possible effects from " + origDir.getAbsolutePath());
possibleCauses = readVars(dataSet, origDir, "possible.causes.txt");
possibleEffects = readVars(dataSet, origDir, "possible.effects.txt");
}
writeVars(possibleCauses, newDir, "possible.causes.txt");
writeVars(possibleEffects, newDir, "possible.effects.txt");
if (new File(origDir, "data.txt").exists()) {
try {
dataSet = SimpleDataLoader.loadContinuousData(new File(origDir, "data.txt"), "//",
'\"', "*", true, Delimiter.TAB, false);
} catch (Exception e) {
throw new IllegalArgumentException("Could not load data from " + new File(origDir, "data.txt").getAbsolutePath());
}
}
writeData(dataSet, newDir);
List