jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
light_lof.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2013 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #ifndef JUBATUS_CORE_ANOMALY_LIGHT_LOF_HPP_
18 #define JUBATUS_CORE_ANOMALY_LIGHT_LOF_HPP_
19 
20 #include <string>
21 #include <utility>
22 #include <vector>
23 #include "jubatus/util/data/serialization.h"
24 #include "jubatus/util/data/unordered_set.h"
25 #include "jubatus/util/lang/shared_ptr.h"
26 #include "anomaly_base.hpp"
27 
28 namespace jubatus {
29 namespace core {
30 namespace storage {
31 class column_table;
32 } // namespace storage
33 namespace unlearner {
34 class unlearner_base;
35 } // namespace unlearner
36 namespace nearest_neighbor {
38 } // namespace nearest_neighbor
39 namespace framework {
41 } // namespace framework
42 namespace anomaly {
43 
44 // LOF implementation using nearest_neighbor as a backend.
45 class light_lof : public anomaly_base {
46  public:
47  // Configuration parameters of LOF.
48  struct config {
49  config();
50 
53 
54  template<typename Ar>
55  void serialize(Ar& ar) {
56  ar
57  & JUBA_MEMBER(nearest_neighbor_num)
58  & JUBA_MEMBER(reverse_nearest_neighbor_num);
59  }
60  };
61 
62  light_lof(
63  const config& config,
64  const std::string& id,
65  jubatus::util::lang::shared_ptr<nearest_neighbor::nearest_neighbor_base>
66  nearest_neighbor_engine);
67 
68  light_lof(
69  const config& config,
70  const std::string& id,
71  jubatus::util::lang::shared_ptr<nearest_neighbor::nearest_neighbor_base>
72  nearest_neighbor_engine,
73  jubatus::util::lang::shared_ptr<unlearner::unlearner_base> unlearner);
74 
75  virtual ~light_lof();
76 
77  // anomaly_base interface
78 
79  float calc_anomaly_score(const common::sfv_t& query) const;
80  float calc_anomaly_score(const std::string& id) const;
81  void clear();
82  // clear_row is not supported
83  void clear_row(const std::string& id);
84  // update_row is not supported
85  void update_row(const std::string& id, const sfv_diff_t& diff);
86  void set_row(const std::string& id, const common::sfv_t& sfv);
87 
88  void get_all_row_ids(std::vector<std::string>& ids) const;
89  std::string type() const;
90  std::vector<framework::mixable*> get_mixables() const;
91 
92  void pack(framework::packer& packer) const;
93  void unpack(msgpack::object o);
94 
95  bool is_updatable() const {
96  return false;
97  }
98 
99  private:
100  // Parameters of each data point.
101  struct parameter {
102  float kdist;
103  float lrd;
104  };
105 
106  void touch(const std::string& id);
107  void unlearn(const std::string& id);
108 
109  float collect_lrds(
110  const common::sfv_t& query,
111  std::vector<float>& neighbor_lrds) const;
112  float collect_lrds(
113  const std::string& query,
114  std::vector<float>& neighbor_lrds) const;
116  const std::vector<std::pair<std::string, float> >& neighbors,
117  std::vector<float>& neighbor_lrd) const;
118 
119  void collect_neighbors(
120  const std::string& query,
121  jubatus::util::data::unordered_set<std::string>& neighbors) const;
122  void update_entries(
123  const jubatus::util::data::unordered_set<std::string>& neighbors);
124 
125  // Gets parameters of given row. If row does not exist, it throws an
126  // exception.
127  parameter get_row_parameter(const std::string& row) const;
128 
129  jubatus::util::lang::shared_ptr<nearest_neighbor::nearest_neighbor_base>
131  jubatus::util::lang::shared_ptr<unlearner::unlearner_base> unlearner_;
132 
133  // Mixable of nearest neighbor model.
134  jubatus::util::lang::shared_ptr<framework::mixable_versioned_table>
136  // Mixable of score table that contains k-dists and LRDs.
137  jubatus::util::lang::shared_ptr<framework::mixable_versioned_table>
139 
141  std::string my_id_;
142 };
143 
144 } // namespace anomaly
145 } // namespace core
146 } // namespace jubatus
147 
148 #endif // JUBATUS_CORE_ANOMALY_LIGHT_LOF_HPP_
float collect_lrds_from_neighbors(const std::vector< std::pair< std::string, float > > &neighbors, std::vector< float > &neighbor_lrd) const
Definition: light_lof.cpp:258
jubatus::util::lang::shared_ptr< nearest_neighbor::nearest_neighbor_base > nearest_neighbor_engine_
Definition: light_lof.hpp:130
jubatus::util::lang::shared_ptr< framework::mixable_versioned_table > mixable_scores_
Definition: light_lof.hpp:138
void set_row(const std::string &id, const common::sfv_t &sfv)
Definition: light_lof.cpp:161
parameter get_row_parameter(const std::string &row) const
Definition: light_lof.cpp:368
void touch(const std::string &id)
Definition: light_lof.cpp:197
void pack(framework::packer &packer) const
Definition: light_lof.cpp:382
light_lof(const config &config, const std::string &id, jubatus::util::lang::shared_ptr< nearest_neighbor::nearest_neighbor_base > nearest_neighbor_engine)
void update_row(const std::string &id, const sfv_diff_t &diff)
Definition: light_lof.cpp:157
jubatus::util::lang::shared_ptr< framework::mixable_versioned_table > mixable_nearest_neighbor_
Definition: light_lof.hpp:135
jubatus::util::data::optional< std::string > unlearner
common::sfv_t sfv_diff_t
void clear_row(const std::string &id)
Definition: light_lof.cpp:153
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
float collect_lrds(const common::sfv_t &query, std::vector< float > &neighbor_lrds) const
Definition: light_lof.cpp:226
float calc_anomaly_score(const common::sfv_t &query) const
Definition: light_lof.cpp:131
void update_entries(const jubatus::util::data::unordered_set< std::string > &neighbors)
Definition: light_lof.cpp:302
void unlearn(const std::string &id)
Definition: light_lof.cpp:215
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
jubatus::util::lang::shared_ptr< unlearner::unlearner_base > unlearner_
Definition: light_lof.hpp:131
void collect_neighbors(const std::string &query, jubatus::util::data::unordered_set< std::string > &neighbors) const
Definition: light_lof.cpp:290
std::vector< framework::mixable * > get_mixables() const
Definition: light_lof.cpp:188
void get_all_row_ids(std::vector< std::string > &ids) const
Definition: light_lof.cpp:180
void unpack(msgpack::object o)
Definition: light_lof.cpp:388