use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.
the class LoOP method computePLOFs.
/**
* Compute the LOF values, using the pdist distances.
*
* @param relation Data relation
* @param knn kNN query
* @param pdists Precomputed distances
* @param plofs Storage for PLOFs.
* @return Normalization factor.
*/
protected double computePLOFs(Relation<O> relation, KNNQuery<O> knn, WritableDoubleDataStore pdists, WritableDoubleDataStore plofs) {
FiniteProgress progressPLOFs = LOG.isVerbose() ? new FiniteProgress("PLOFs for objects", relation.size(), LOG) : null;
double nplof = 0.;
for (DBIDIter iditer = relation.iterDBIDs(); iditer.valid(); iditer.advance()) {
// + query
final KNNList neighbors = knn.getKNNForDBID(iditer, kcomp + 1);
// point
// use first kref neighbors as comparison set.
int ks = 0;
double sum = 0.;
for (DBIDIter neighbor = neighbors.iter(); neighbor.valid() && ks < kcomp; neighbor.advance()) {
if (DBIDUtil.equal(neighbor, iditer)) {
continue;
}
sum += pdists.doubleValue(neighbor);
ks++;
}
double plof = MathUtil.max(pdists.doubleValue(iditer) * ks / sum, 1.0);
if (Double.isNaN(plof) || Double.isInfinite(plof)) {
plof = 1.0;
}
plofs.putDouble(iditer, plof);
nplof += (plof - 1.0) * (plof - 1.0);
LOG.incrementProcessed(progressPLOFs);
}
LOG.ensureCompleted(progressPLOFs);
nplof = lambda * FastMath.sqrt(nplof / relation.size());
if (LOG.isDebuggingFine()) {
LOG.debugFine("nplof normalization factor is " + nplof);
}
return nplof > 0. ? nplof : 1.;
}
use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.
the class ParallelLOF method run.
public OutlierResult run(Database database, Relation<O> relation) {
DBIDs ids = relation.getDBIDs();
DistanceQuery<O> distq = database.getDistanceQuery(relation, getDistanceFunction());
KNNQuery<O> knnq = database.getKNNQuery(distq, k + 1);
// Phase one: KNN and k-dist
WritableDoubleDataStore kdists = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_DB);
WritableDataStore<KNNList> knns = DataStoreUtil.makeStorage(ids, DataStoreFactory.HINT_DB, KNNList.class);
{
// Compute kNN
KNNProcessor<O> knnm = new KNNProcessor<>(k + 1, knnq);
SharedObject<KNNList> knnv = new SharedObject<>();
WriteDataStoreProcessor<KNNList> storek = new WriteDataStoreProcessor<>(knns);
knnm.connectKNNOutput(knnv);
storek.connectInput(knnv);
// Compute k-dist
KDistanceProcessor kdistm = new KDistanceProcessor(k + 1);
SharedDouble kdistv = new SharedDouble();
WriteDoubleDataStoreProcessor storem = new WriteDoubleDataStoreProcessor(kdists);
kdistm.connectKNNInput(knnv);
kdistm.connectOutput(kdistv);
storem.connectInput(kdistv);
ParallelExecutor.run(ids, knnm, storek, kdistm, storem);
}
// Phase two: lrd
WritableDoubleDataStore lrds = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_DB);
{
LRDProcessor lrdm = new LRDProcessor(knns, kdists);
SharedDouble lrdv = new SharedDouble();
WriteDoubleDataStoreProcessor storelrd = new WriteDoubleDataStoreProcessor(lrds);
lrdm.connectOutput(lrdv);
storelrd.connectInput(lrdv);
ParallelExecutor.run(ids, lrdm, storelrd);
}
// No longer needed.
kdists.destroy();
kdists = null;
// Phase three: LOF
WritableDoubleDataStore lofs = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_DB);
DoubleMinMax minmax;
{
LOFProcessor lofm = new LOFProcessor(knns, lrds, true);
SharedDouble lofv = new SharedDouble();
DoubleMinMaxProcessor mmm = new DoubleMinMaxProcessor();
WriteDoubleDataStoreProcessor storelof = new WriteDoubleDataStoreProcessor(lofs);
lofm.connectOutput(lofv);
mmm.connectInput(lofv);
storelof.connectInput(lofv);
ParallelExecutor.run(ids, lofm, storelof, mmm);
minmax = mmm.getMinMax();
}
DoubleRelation scoreres = new MaterializedDoubleRelation("Local Outlier Factor", "lof-outlier", lofs, ids);
OutlierScoreMeta meta = new BasicOutlierScoreMeta(minmax.getMin(), minmax.getMax(), 0.0, Double.POSITIVE_INFINITY, 1.0);
return new OutlierResult(meta, scoreres);
}
use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.
the class SimplifiedLOF method computeSimplifiedLOFs.
/**
* Compute the simplified LOF factors.
*
* @param ids IDs to compute for
* @param knnq kNN query class
* @param slrds Object densities
* @param lofs SLOF output storage
* @param lofminmax Minimum and maximum scores
*/
private void computeSimplifiedLOFs(DBIDs ids, KNNQuery<O> knnq, WritableDoubleDataStore slrds, WritableDoubleDataStore lofs, DoubleMinMax lofminmax) {
FiniteProgress progressLOFs = LOG.isVerbose() ? new FiniteProgress("Simplified LOF scores", ids.size(), LOG) : null;
for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
final double lof;
final double lrdp = slrds.doubleValue(iter);
final KNNList neighbors = knnq.getKNNForDBID(iter, k);
if (!Double.isInfinite(lrdp)) {
double sum = 0.;
int count = 0;
for (DBIDIter neighbor = neighbors.iter(); neighbor.valid(); neighbor.advance()) {
// skip the point itself
if (DBIDUtil.equal(neighbor, iter)) {
continue;
}
final double val = slrds.doubleValue(neighbor);
sum += val;
count++;
if (Double.isInfinite(val)) {
break;
}
}
lof = sum / (lrdp * count);
} else {
lof = 1.0;
}
lofs.putDouble(iter, lof);
// update minimum and maximum
lofminmax.put(lof);
LOG.incrementProcessed(progressLOFs);
}
LOG.ensureCompleted(progressLOFs);
}
use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.
the class ParallelSimplifiedLOF method run.
public OutlierResult run(Database database, Relation<O> relation) {
DBIDs ids = relation.getDBIDs();
DistanceQuery<O> distq = database.getDistanceQuery(relation, getDistanceFunction());
KNNQuery<O> knnq = database.getKNNQuery(distq, k + 1);
// Phase one: KNN and k-dist
WritableDataStore<KNNList> knns = DataStoreUtil.makeStorage(ids, DataStoreFactory.HINT_DB, KNNList.class);
{
// Compute kNN
KNNProcessor<O> knnm = new KNNProcessor<>(k + 1, knnq);
SharedObject<KNNList> knnv = new SharedObject<>();
WriteDataStoreProcessor<KNNList> storek = new WriteDataStoreProcessor<>(knns);
knnm.connectKNNOutput(knnv);
storek.connectInput(knnv);
ParallelExecutor.run(ids, knnm, storek);
}
// Phase two: simplified-lrd
WritableDoubleDataStore lrds = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_DB);
{
SimplifiedLRDProcessor lrdm = new SimplifiedLRDProcessor(knns);
SharedDouble lrdv = new SharedDouble();
WriteDoubleDataStoreProcessor storelrd = new WriteDoubleDataStoreProcessor(lrds);
lrdm.connectOutput(lrdv);
storelrd.connectInput(lrdv);
ParallelExecutor.run(ids, lrdm, storelrd);
}
// Phase three: Simplified-LOF
WritableDoubleDataStore lofs = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_DB);
DoubleMinMax minmax;
{
LOFProcessor lofm = new LOFProcessor(knns, lrds, true);
SharedDouble lofv = new SharedDouble();
DoubleMinMaxProcessor mmm = new DoubleMinMaxProcessor();
WriteDoubleDataStoreProcessor storelof = new WriteDoubleDataStoreProcessor(lofs);
lofm.connectOutput(lofv);
mmm.connectInput(lofv);
storelof.connectInput(lofv);
ParallelExecutor.run(ids, lofm, storelof, mmm);
minmax = mmm.getMinMax();
}
DoubleRelation scoreres = new MaterializedDoubleRelation("Simplified Local Outlier Factor", "simplified-lof-outlier", lofs, ids);
OutlierScoreMeta meta = new BasicOutlierScoreMeta(minmax.getMin(), minmax.getMax(), 0.0, Double.POSITIVE_INFINITY, 1.0);
return new OutlierResult(meta, scoreres);
}
use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.
the class SimpleKernelDensityLOF method run.
/**
* Run the naive kernel density LOF algorithm.
*
* @param database Database to query
* @param relation Data to process
* @return LOF outlier result
*/
public OutlierResult run(Database database, Relation<O> relation) {
StepProgress stepprog = LOG.isVerbose() ? new StepProgress("KernelDensityLOF", 3) : null;
final int dim = RelationUtil.dimensionality(relation);
DBIDs ids = relation.getDBIDs();
LOG.beginStep(stepprog, 1, "Materializing neighborhoods w.r.t. distance function.");
KNNQuery<O> knnq = DatabaseUtil.precomputedKNNQuery(database, relation, getDistanceFunction(), k);
// Compute LRDs
LOG.beginStep(stepprog, 2, "Computing densities.");
WritableDoubleDataStore dens = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_HOT | DataStoreFactory.HINT_TEMP);
FiniteProgress densProgress = LOG.isVerbose() ? new FiniteProgress("Densities", ids.size(), LOG) : null;
for (DBIDIter it = ids.iter(); it.valid(); it.advance()) {
final KNNList neighbors = knnq.getKNNForDBID(it, k);
int count = 0;
double sum = 0.0;
// Fast version for double distances
for (DoubleDBIDListIter neighbor = neighbors.iter(); neighbor.valid(); neighbor.advance()) {
if (DBIDUtil.equal(neighbor, it)) {
continue;
}
double max = knnq.getKNNForDBID(neighbor, k).getKNNDistance();
if (max == 0) {
sum = Double.POSITIVE_INFINITY;
break;
}
final double v = neighbor.doubleValue() / max;
sum += kernel.density(v) / MathUtil.powi(max, dim);
count++;
}
final double density = count > 0 ? sum / count : 0.;
dens.putDouble(it, density);
LOG.incrementProcessed(densProgress);
}
LOG.ensureCompleted(densProgress);
// compute LOF_SCORE of each db object
LOG.beginStep(stepprog, 3, "Computing KLOFs.");
WritableDoubleDataStore lofs = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_STATIC);
// track the maximum value for normalization.
DoubleMinMax lofminmax = new DoubleMinMax();
FiniteProgress progressLOFs = LOG.isVerbose() ? new FiniteProgress("KLOF_SCORE for objects", ids.size(), LOG) : null;
for (DBIDIter it = ids.iter(); it.valid(); it.advance()) {
final double lrdp = dens.doubleValue(it);
final double lof;
if (lrdp > 0) {
final KNNList neighbors = knnq.getKNNForDBID(it, k);
double sum = 0.0;
int count = 0;
for (DBIDIter neighbor = neighbors.iter(); neighbor.valid(); neighbor.advance()) {
// skip the point itself
if (DBIDUtil.equal(neighbor, it)) {
continue;
}
sum += dens.doubleValue(neighbor);
count++;
}
lof = (lrdp == Double.POSITIVE_INFINITY) ? (sum == Double.POSITIVE_INFINITY ? 1 : 0.) : sum / (count * lrdp);
} else {
lof = 1.0;
}
lofs.putDouble(it, lof);
// update minimum and maximum
lofminmax.put(lof);
LOG.incrementProcessed(progressLOFs);
}
LOG.ensureCompleted(progressLOFs);
LOG.setCompleted(stepprog);
// Build result representation.
DoubleRelation scoreResult = new MaterializedDoubleRelation("Kernel Density Local Outlier Factor", "kernel-density-slof-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 result;
}
Aggregations