jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
lof.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2012 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 #include "lof.hpp"
18 
19 #include <cmath>
20 #include <limits>
21 #include <string>
22 #include <vector>
23 
24 #include "jubatus/util/lang/cast.h"
25 #include "jubatus/util/math/random.h"
26 
27 #include "../common/hash.hpp"
28 #include "../storage/lsh_util.hpp"
29 #include "../storage/lsh_vector.hpp"
30 
32 using jubatus::util::data::unordered_map;
33 using jubatus::util::math::random::mtrand;
34 using std::istream;
35 using std::ostream;
36 using std::numeric_limits;
37 using std::string;
38 using std::vector;
39 
40 namespace jubatus {
41 namespace core {
42 namespace anomaly {
43 
44 namespace {
45 
46 float calculate_lof(
47  float lrd,
48  const unordered_map<string, float>& neighbor_lrd) {
49  if (neighbor_lrd.empty()) {
50  return lrd == 0 ? 1 : numeric_limits<float>::infinity();
51  }
52 
53  float sum_neighbor_lrd = 0;
54  for (unordered_map<string, float>::const_iterator it = neighbor_lrd.begin();
55  it != neighbor_lrd.end(); ++it) {
56  sum_neighbor_lrd += it->second;
57  }
58 
59  if (std::isinf(sum_neighbor_lrd) && std::isinf(lrd)) {
60  return 1;
61  }
62 
63  return sum_neighbor_lrd / (neighbor_lrd.size() * lrd);
64 }
65 
66 } // namespace
67 
69  const lof_storage::config& config,
70  jubatus::util::lang::shared_ptr<recommender::recommender_base> nn_engine)
71  : mixable_storage_(),
72  nn_engine_(nn_engine) {
73 
74  if (!(2 <= config.nearest_neighbor_num)) {
75  throw JUBATUS_EXCEPTION(
76  common::invalid_parameter("2 <= nearest_neighbor_num"));
77  }
78 
79  if (!(config.nearest_neighbor_num
80  <= config.reverse_nearest_neighbor_num)) {
81  throw JUBATUS_EXCEPTION(
83  "nearest_neighbor_num <= reverse_nearest_neighbor_num"));
84  }
85 
86  mixable_lof_storage::model_ptr p(new lof_storage(config, nn_engine));
88 }
89 
91 }
92 
93 float lof::calc_anomaly_score(const common::sfv_t& query) const {
94  unordered_map<string, float> neighbor_lrd;
95  const float lrd = mixable_storage_->get_model()->collect_lrds(
96  query, neighbor_lrd);
97  return calculate_lof(lrd, neighbor_lrd);
98 }
99 
100 float lof::calc_anomaly_score(const string& id) const {
101  unordered_map<string, float> neighbor_lrd;
102  const float lrd = mixable_storage_->get_model()->collect_lrds(
103  id, neighbor_lrd);
104 
105  return calculate_lof(lrd, neighbor_lrd);
106 }
107 
108 void lof::clear() {
109  mixable_storage_->get_model()->clear();
110 }
111 
112 void lof::clear_row(const string& id) {
113  mixable_storage_->get_model()->remove_row(id);
114 }
115 
116 void lof::update_row(const string& id, const sfv_diff_t& diff) {
117  mixable_storage_->get_model()->update_row(id, diff);
118 }
119 
120 void lof::set_row(const string& id, const common::sfv_t& sfv) {
122 }
123 
124 void lof::get_all_row_ids(vector<string>& ids) const {
125  mixable_storage_->get_model()->get_all_row_ids(ids);
126 }
127 
128 string lof::type() const {
129  return "lof";
130 }
131 
132 std::vector<framework::mixable*> lof::get_mixables() const {
133  std::vector<framework::mixable*> mixables;
134  mixables.push_back(mixable_storage_.get());
135  mixables.push_back(nn_engine_->get_mixable());
136  return mixables;
137 }
138 
140  packer.pack_array(2);
141  mixable_storage_->get_model()->pack(packer);
142  nn_engine_->pack(packer);
143 }
144 
145 void lof::unpack(msgpack::object o) {
146  if (o.type != msgpack::type::ARRAY || o.via.array.size != 2) {
147  throw msgpack::type_error();
148  }
149 
150  // clear before load
151  mixable_storage_->get_model()->clear();
152  nn_engine_->clear();
153 
154  mixable_storage_->get_model()->unpack(o.via.array.ptr[0]);
155  nn_engine_->unpack(o.via.array.ptr[1]);
156 }
157 
158 } // namespace anomaly
159 } // namespace core
160 } // namespace jubatus
float calc_anomaly_score(const common::sfv_t &query) const
Definition: lof.cpp:93
void clear_row(const std::string &id)
Definition: lof.cpp:112
std::vector< framework::mixable * > get_mixables() const
Definition: lof.cpp:132
void set_row(const std::string &id, const common::sfv_t &sfv)
Definition: lof.cpp:120
void get_all_row_ids(std::vector< std::string > &ids) const
Definition: lof.cpp:124
jubatus::util::lang::shared_ptr< Model > model_ptr
void update_row(const std::string &id, const sfv_diff_t &diff)
Definition: lof.cpp:116
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
common::sfv_t sfv_diff_t
jubatus::util::lang::shared_ptr< recommender::recommender_base > nn_engine_
Definition: lof.hpp:61
void unpack(msgpack::object o)
Definition: lof.cpp:145
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
framework::linear_mixable_helper< lof_storage, lof_table_t > mixable_lof_storage
void pack(framework::packer &packer) const
Definition: lof.cpp:139
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
lof(const lof_storage::config &config, jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine)
Definition: lof.cpp:68
jubatus::util::lang::shared_ptr< mixable_lof_storage > mixable_storage_
Definition: lof.hpp:60
std::string type() const
Definition: lof.cpp:128