use of de.lmu.ifi.dbs.elki.database.relation.DoubleRelation in project elki by elki-project.
the class ODIN method run.
/**
* Run the ODIN algorithm
*
* @param database Database to run on.
* @param relation Relation to process.
* @return ODIN outlier result.
*/
public OutlierResult run(Database database, Relation<O> relation) {
// Get the query functions:
DistanceQuery<O> dq = database.getDistanceQuery(relation, getDistanceFunction());
KNNQuery<O> knnq = database.getKNNQuery(dq, k);
// Get the objects to process, and a data storage for counting and output:
DBIDs ids = relation.getDBIDs();
WritableDoubleDataStore scores = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_DB, 0.);
double inc = 1. / (k - 1);
double min = Double.POSITIVE_INFINITY, max = 0.0;
// Process all objects
for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
// Find the nearest neighbors (using an index, if available!)
DBIDs neighbors = knnq.getKNNForDBID(iter, k);
// For each neighbor, except ourselves, increase the in-degree:
for (DBIDIter nei = neighbors.iter(); nei.valid(); nei.advance()) {
if (DBIDUtil.equal(iter, nei)) {
continue;
}
final double value = scores.doubleValue(nei) + inc;
if (value < min) {
min = value;
}
if (value > max) {
max = value;
}
scores.put(nei, value);
}
}
// Wrap the result and add metadata.
OutlierScoreMeta meta = new InvertedOutlierScoreMeta(min, max, 0., inc * (ids.size() - 1), 1);
DoubleRelation rel = new MaterializedDoubleRelation("ODIN In-Degree", "odin", scores, ids);
return new OutlierResult(meta, rel);
}
use of de.lmu.ifi.dbs.elki.database.relation.DoubleRelation in project elki by elki-project.
the class IntrinsicDimensionalityOutlier method run.
/**
* Run the algorithm
*
* @param database Database
* @param relation Data relation
* @return Outlier result
*/
public OutlierResult run(Database database, Relation<O> relation) {
final DistanceQuery<O> distanceQuery = database.getDistanceQuery(relation, getDistanceFunction());
final KNNQuery<O> knnQuery = database.getKNNQuery(distanceQuery, k + 1);
FiniteProgress prog = LOG.isVerbose() ? new FiniteProgress("kNN distance for objects", relation.size(), LOG) : null;
DoubleMinMax minmax = new DoubleMinMax();
WritableDoubleDataStore id_score = DataStoreUtil.makeDoubleStorage(relation.getDBIDs(), DataStoreFactory.HINT_STATIC);
for (DBIDIter iditer = relation.iterDBIDs(); iditer.valid(); iditer.advance()) {
double id = 0.;
try {
id = estimator.estimate(knnQuery, iditer, k + 1);
} catch (ArithmeticException e) {
id = 0.;
}
id_score.putDouble(iditer, id);
minmax.put(id);
LOG.incrementProcessed(prog);
}
LOG.ensureCompleted(prog);
DoubleRelation scoreres = new MaterializedDoubleRelation("Intrinsic dimensionality", "id-score", id_score, relation.getDBIDs());
OutlierScoreMeta meta = new BasicOutlierScoreMeta(minmax.getMin(), minmax.getMax(), 0.0, Double.POSITIVE_INFINITY, 0.0);
return new OutlierResult(meta, scoreres);
}
use of de.lmu.ifi.dbs.elki.database.relation.DoubleRelation in project elki by elki-project.
the class ALOCI method run.
public OutlierResult run(Database database, Relation<O> relation) {
final int dim = RelationUtil.dimensionality(relation);
final Random random = rnd.getSingleThreadedRandom();
FiniteProgress progressPreproc = LOG.isVerbose() ? new FiniteProgress("Build aLOCI quadtress", g, LOG) : null;
// Compute extend of dataset.
double[] min, max;
{
double[][] hbbs = RelationUtil.computeMinMax(relation);
min = hbbs[0];
max = hbbs[1];
double maxd = 0;
for (int i = 0; i < dim; i++) {
maxd = MathUtil.max(maxd, max[i] - min[i]);
}
// Enlarge bounding box to have equal lengths.
for (int i = 0; i < dim; i++) {
double diff = (maxd - (max[i] - min[i])) * .5;
min[i] -= diff;
max[i] += diff;
}
}
List<ALOCIQuadTree> qts = new ArrayList<>(g);
double[] nshift = new double[dim];
ALOCIQuadTree qt = new ALOCIQuadTree(min, max, nshift, nmin, relation);
qts.add(qt);
LOG.incrementProcessed(progressPreproc);
/*
* create the remaining g-1 shifted QuadTrees. This not clearly described in
* the paper and therefore implemented in a way that achieves good results
* with the test data.
*/
for (int shift = 1; shift < g; shift++) {
double[] svec = new double[dim];
for (int i = 0; i < dim; i++) {
svec[i] = random.nextDouble() * (max[i] - min[i]);
}
qt = new ALOCIQuadTree(min, max, svec, nmin, relation);
qts.add(qt);
LOG.incrementProcessed(progressPreproc);
}
LOG.ensureCompleted(progressPreproc);
// aLOCI main loop: evaluate
FiniteProgress progressLOCI = LOG.isVerbose() ? new FiniteProgress("Compute aLOCI scores", relation.size(), LOG) : null;
WritableDoubleDataStore mdef_norm = DataStoreUtil.makeDoubleStorage(relation.getDBIDs(), DataStoreFactory.HINT_STATIC);
DoubleMinMax minmax = new DoubleMinMax();
for (DBIDIter iditer = relation.iterDBIDs(); iditer.valid(); iditer.advance()) {
final O obj = relation.get(iditer);
double maxmdefnorm = 0;
// For each level
for (int l = 0; ; l++) {
// Find the closest C_i
Node ci = null;
for (int i = 0; i < g; i++) {
Node ci2 = qts.get(i).findClosestNode(obj, l);
if (ci2.getLevel() != l) {
continue;
}
// TODO: always use manhattan?
if (ci == null || distFunc.distance(ci, obj) > distFunc.distance(ci2, obj)) {
ci = ci2;
}
}
// LOG.debug("level:" + (ci != null ? ci.getLevel() : -1) +" l:"+l);
if (ci == null) {
// no matching tree for this level.
break;
}
// Find the closest C_j
Node cj = null;
for (int i = 0; i < g; i++) {
Node cj2 = qts.get(i).findClosestNode(ci, l - alpha);
// TODO: allow higher levels or not?
if (cj != null && cj2.getLevel() < cj.getLevel()) {
continue;
}
// TODO: always use manhattan?
if (cj == null || distFunc.distance(cj, ci) > distFunc.distance(cj2, ci)) {
cj = cj2;
}
}
// LOG.debug("level:" + (cj != null ? cj.getLevel() : -1) +" l:"+l);
if (cj == null) {
// no matching tree for this level.
continue;
}
double mdefnorm = calculate_MDEF_norm(cj, ci);
// LOG.warning("level:" + ci.getLevel() + "/" + cj.getLevel() +
// " mdef: " + mdefnorm);
maxmdefnorm = MathUtil.max(maxmdefnorm, mdefnorm);
}
// Store results
mdef_norm.putDouble(iditer, maxmdefnorm);
minmax.put(maxmdefnorm);
LOG.incrementProcessed(progressLOCI);
}
LOG.ensureCompleted(progressLOCI);
DoubleRelation scoreResult = new MaterializedDoubleRelation("aLOCI normalized MDEF", "aloci-mdef-outlier", mdef_norm, relation.getDBIDs());
OutlierScoreMeta scoreMeta = new QuotientOutlierScoreMeta(minmax.getMin(), minmax.getMax(), 0.0, Double.POSITIVE_INFINITY);
OutlierResult result = new OutlierResult(scoreMeta, scoreResult);
return result;
}
use of de.lmu.ifi.dbs.elki.database.relation.DoubleRelation in project elki by elki-project.
the class FlexibleLOF method doRunInTime.
/**
* Performs the Generalized LOF_SCORE algorithm on the given database and
* returns a {@link FlexibleLOF.LOFResult} encapsulating information that may
* be needed by an OnlineLOF algorithm.
*
* @param ids Object ids
* @param kNNRefer the kNN query w.r.t. reference neighborhood distance
* function
* @param kNNReach the kNN query w.r.t. reachability distance function
* @param stepprog Progress logger
* @return LOF result
*/
protected LOFResult<O> doRunInTime(DBIDs ids, KNNQuery<O> kNNRefer, KNNQuery<O> kNNReach, StepProgress stepprog) {
// Assert we got something
if (kNNRefer == null) {
throw new AbortException("No kNN queries supported by database for reference neighborhood distance function.");
}
if (kNNReach == null) {
throw new AbortException("No kNN queries supported by database for reachability distance function.");
}
// Compute LRDs
LOG.beginStep(stepprog, 2, "Computing LRDs.");
WritableDoubleDataStore lrds = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_HOT | DataStoreFactory.HINT_TEMP);
computeLRDs(kNNReach, ids, lrds);
// compute LOF_SCORE of each db object
LOG.beginStep(stepprog, 3, "Computing LOFs.");
WritableDoubleDataStore lofs = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_STATIC);
// track the maximum value for normalization.
DoubleMinMax lofminmax = new DoubleMinMax();
computeLOFs(kNNRefer, ids, lrds, lofs, lofminmax);
LOG.setCompleted(stepprog);
// Build result representation.
DoubleRelation scoreResult = new MaterializedDoubleRelation("Local Outlier Factor", "lof-outlier", lofs, ids);
OutlierScoreMeta scoreMeta = new QuotientOutlierScoreMeta(lofminmax.getMin(), lofminmax.getMax(), 0.0, Double.POSITIVE_INFINITY, 1.0);
OutlierResult result = new OutlierResult(scoreMeta, scoreResult);
return new LOFResult<>(result, kNNRefer, kNNReach, lrds, lofs);
}
use of de.lmu.ifi.dbs.elki.database.relation.DoubleRelation in project elki by elki-project.
the class KDEOS method run.
/**
* Run the KDEOS outlier detection algorithm.
*
* @param database Database to query
* @param rel Relation to process
* @return Outlier detection result
*/
public OutlierResult run(Database database, Relation<O> rel) {
final DBIDs ids = rel.getDBIDs();
LOG.verbose("Running kNN preprocessor.");
KNNQuery<O> knnq = DatabaseUtil.precomputedKNNQuery(database, rel, getDistanceFunction(), kmax + 1);
// Initialize store for densities
WritableDataStore<double[]> densities = DataStoreUtil.makeStorage(ids, DataStoreFactory.HINT_HOT | DataStoreFactory.HINT_TEMP, double[].class);
estimateDensities(rel, knnq, ids, densities);
// Compute scores:
WritableDoubleDataStore kofs = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_DB);
DoubleMinMax minmax = new DoubleMinMax();
computeOutlierScores(knnq, ids, densities, kofs, minmax);
DoubleRelation scoreres = new MaterializedDoubleRelation("Kernel Density Estimation Outlier Scores", "kdeos-outlier", kofs, ids);
OutlierScoreMeta meta = new ProbabilisticOutlierScore(minmax.getMin(), minmax.getMax());
return new OutlierResult(meta, scoreres);
}
Aggregations