libcrn  3.9.5
A document image processing library
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNOutliers.h
Go to the documentation of this file.
1 /* Copyright 2014-2015 INSA-Lyon, Université Paris Descartes
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNOutliers.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNOutliers_HEADER
23 #define CRNOutliers_HEADER
24 
25 #include <CRNException.h>
26 #include <CRNMath/CRNMath.h>
27 #include <vector>
28 #include <map>
29 #include <set>
30 #include <limits>
31 
32 namespace crn
33 {
34  class SquareMatrixDouble;
35 
37  std::vector<double> ComputeLOF(const SquareMatrixDouble &distmat, size_t k);
39  std::vector<double> ComputeLOF(const std::vector<std::vector<double>> &distmat, size_t k);
40 
42  std::vector<double> ComputeLoOP(const SquareMatrixDouble &distmat, size_t k, double lambda);
44  std::vector<double> ComputeLoOP(const std::vector<std::vector<double>> &distmat, size_t k, double lambda);
45 
55  template<typename ITER> std::vector<double> AngularOutliersE(ITER beg, ITER en)
56  {
57  if (beg == en)
58  throw ExceptionDomain("AngularOutliersE(): empty set of angles.");
59  auto c = 0.0, s = 0.0;
60  for (auto it = beg; it != en; ++it)
61  {
62  c += Cos(*it);
63  s += Sin(*it);
64  }
65  auto e = std::vector<double>(std::distance(beg, en));
66  const auto n = double(e.size());
67  const auto cr = 1.0 - sqrt(Sqr(c / n) + Sqr(s / n));
68  auto cnt = size_t(0);
69  for (auto it = beg; it != en; ++it, ++cnt)
70  e[cnt] = (1.0 - sqrt(Sqr((c - Cos(*it)) / (n - 1)) + Sqr((s - Sin(*it)) / (n - 1)))) / (cr);
71  return e;
72  }
73 
83  template<typename ITER> std::vector<double> AngularOutliersC(ITER beg, ITER en)
84  {
85  if (beg == en)
86  throw ExceptionDomain("AngularOutliersC(): empty set of angles.");
87  auto c = 0.0, s = 0.0;
88  for (auto it = beg; it != en; ++it)
89  {
90  c += Cos(*it);
91  s += Sin(*it);
92  }
93  auto C = std::vector<double>(std::distance(beg, en));
94  const auto n = double(C.size());
95  const auto r = sqrt(Sqr(c / n) + Sqr(s / n));
96  auto cnt = size_t(0);
97  for (auto it = beg; it != en; ++it, ++cnt)
98  C[cnt] = sqrt(Sqr((c - Cos(*it)) / (n - 1)) + Sqr((s - Sin(*it)) / (n - 1))) / r;
99  return C;
100  }
101 
102 }
103 
104 #endif
std::vector< double > ComputeLOF(const SquareMatrixDouble &distmat, size_t k)
Compute the Local Outlier Factor for each element from the distance matrix.
Definition: CRNOutliers.cpp:89
double Cos(const A &a) noexcept
std::vector< double > ComputeLoOP(const SquareMatrixDouble &distmat, size_t k, double lambda)
compute the local outlier probability for each element from the distance matrix
A generic domain error.
Definition: CRNException.h:83
std::vector< double > AngularOutliersC(ITER beg, ITER en)
Outlier C statistics of a set of angles (Collett, Outliers in circular data. Appl. Statist., 29, 50-57., 1980)
Definition: CRNOutliers.h:83
constexpr SumType< T > Sqr(const T &v) noexcept(noexcept(v *v))
Returns the square of a value.
Definition: CRNMath.h:61
double Sin(const A &a) noexcept
std::vector< double > AngularOutliersE(ITER beg, ITER en)
Outlier E statistics of a set of angles (Mardia, Statistics of directional data (with discussion)...
Definition: CRNOutliers.h:55