31 template<
typename DISTMAT> std::vector<double>
computeLOF(
const DISTMAT &distmat,
size_t ndata,
size_t k)
39 std::vector<std::multimap<double, size_t>> knn(ndata);
40 for (
size_t tmp1 = 0; tmp1 < ndata; ++tmp1)
42 for (
size_t tmp2 = 0; tmp2 < ndata; ++tmp2)
44 double maxval = (knn[tmp1].size() >= k) ? knn[tmp1].rbegin()->first : std::numeric_limits<double>::max();
45 if (distmat[tmp1][tmp2] < maxval)
47 if (knn[tmp1].size() >= k)
48 knn[tmp1].erase(maxval);
49 knn[tmp1].insert(std::make_pair(distmat[tmp1][tmp2], tmp2));
55 std::vector<double> lrd(ndata, 0.0);
56 for (
size_t tmp1 = 0; tmp1 < ndata; ++tmp1)
58 for (
const auto &nn : knn[tmp1])
59 lrd[tmp1] +=
crn::Max(distmat[tmp1][nn.second], knn[nn.second].rbegin()->first);
60 lrd[tmp1] = double(k) / lrd[tmp1];
64 std::vector<double> lof(ndata, 0.0);
65 for (
size_t tmp1 = 0; tmp1 < ndata; ++tmp1)
67 for (
const auto &nn : knn[tmp1])
69 lof[tmp1] += lrd[nn.second];
71 lof[tmp1] /= double(k) * lrd[tmp1];
106 std::vector<double>
crn::ComputeLOF(
const std::vector<std::vector<double>> &distmat,
size_t k)
108 const auto ndata = distmat.size();
109 for (
const auto &row : distmat)
110 if (row.size() != ndata)
117 template<
typename DISTMAT> std::vector<double>
computeLoOP(
const DISTMAT &distmat,
size_t ndata,
size_t k,
double lambda)
127 std::vector<std::multimap<double, size_t>> knn(ndata);
128 for (
size_t tmp1 = 0; tmp1 < ndata; ++tmp1)
130 for (
size_t tmp2 = 0; tmp2 < ndata; ++tmp2)
132 double maxval = (knn[tmp1].size() >= k) ? knn[tmp1].rbegin()->first : std::numeric_limits<double>::max();
133 if (distmat[tmp1][tmp2] < maxval)
135 if (knn[tmp1].size() >= k)
136 knn[tmp1].erase(maxval);
137 knn[tmp1].insert(std::make_pair(distmat[tmp1][tmp2], tmp2));
143 std::vector<double> pdist(ndata, 0.0);
144 for (
size_t tmp = 0; tmp < ndata; ++tmp)
146 for (
const auto &neigh : knn[tmp])
147 pdist[tmp] +=
Sqr(neigh.first);
148 pdist[tmp] = lambda * sqrt(pdist[tmp] /
double(k));
152 std::vector<double> plof(ndata, 0.0);
153 for (
size_t tmp = 0; tmp < ndata; ++tmp)
155 for (
const auto &neigh : knn[tmp])
156 plof[tmp] += pdist[neigh.second];
157 plof[tmp] = (double(k) * pdist[tmp] / plof[tmp]) + 1;
159 double nplof = sqrt(2.0) * lambda * double(ndata) / std::accumulate(plof.begin(), plof.end(), 0.0);
162 std::vector<double> loop(ndata, 0.0);
163 for (
size_t tmp = 0; tmp < ndata; ++tmp)
164 loop[tmp] =
Max(0.0, erf(plof[tmp] / nplof));
200 std::vector<double>
crn::ComputeLoOP(
const std::vector<std::vector<double>> &distmat,
size_t k,
double lambda)
202 const size_t ndata = distmat.size();
203 for (
const auto &row : distmat)
204 if (row.size() != ndata)
205 throw ExceptionDimension(
crn::StringUTF8(
"std::vector<double> ComputeLoOP(const std::vector<std::vector<double>> &distmat, size_t k, double lambda): ") +
_(
"The distance matrix is not square."));
size_t GetRows() const noexcept
Returns the number of rows.
std::vector< double > ComputeLOF(const SquareMatrixDouble &distmat, size_t k)
Compute the Local Outlier Factor for each element from the distance matrix.
const T & Max(const T &a, const T &b)
Returns the max of two values.
std::vector< double > computeLoOP(const DISTMAT &distmat, size_t ndata, size_t k, double lambda)
std::vector< double > ComputeLoOP(const SquareMatrixDouble &distmat, size_t k, double lambda)
compute the local outlier probability for each element from the distance matrix
constexpr SumType< T > Sqr(const T &v) noexcept(noexcept(v *v))
Returns the square of a value.
std::vector< double > computeLOF(const DISTMAT &distmat, size_t ndata, size_t k)
Square double matrix class.
A character string class.