jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
jubatus::core::anomaly::light_lof Class Reference

#include <light_lof.hpp>

Inheritance diagram for jubatus::core::anomaly::light_lof:
Inheritance graph
Collaboration diagram for jubatus::core::anomaly::light_lof:
Collaboration graph

Classes

struct  config
 
struct  parameter
 

Public Member Functions

float calc_anomaly_score (const common::sfv_t &query) const
 
float calc_anomaly_score (const std::string &id) const
 
void clear ()
 
void clear_row (const std::string &id)
 
void get_all_row_ids (std::vector< std::string > &ids) const
 
std::vector< framework::mixable * > get_mixables () const
 
bool is_updatable () const
 
 light_lof (const config &config, const std::string &id, jubatus::util::lang::shared_ptr< nearest_neighbor::nearest_neighbor_base > nearest_neighbor_engine)
 
 light_lof (const config &config, const std::string &id, jubatus::util::lang::shared_ptr< nearest_neighbor::nearest_neighbor_base > nearest_neighbor_engine, jubatus::util::lang::shared_ptr< unlearner::unlearner_base > unlearner)
 
void pack (framework::packer &packer) const
 
void set_row (const std::string &id, const common::sfv_t &sfv)
 
std::string type () const
 
void unpack (msgpack::object o)
 
void update_row (const std::string &id, const sfv_diff_t &diff)
 
virtual ~light_lof ()
 
- Public Member Functions inherited from jubatus::core::anomaly::anomaly_base
 anomaly_base ()
 
uint64_t find_max_int_id () const
 
virtual ~anomaly_base ()
 
- Public Member Functions inherited from jubatus::core::framework::model
virtual ~model ()
 

Private Member Functions

float collect_lrds (const common::sfv_t &query, std::vector< float > &neighbor_lrds) const
 
float collect_lrds (const std::string &query, std::vector< float > &neighbor_lrds) const
 
float collect_lrds_from_neighbors (const std::vector< std::pair< std::string, float > > &neighbors, std::vector< float > &neighbor_lrd) const
 
void collect_neighbors (const std::string &query, jubatus::util::data::unordered_set< std::string > &neighbors) const
 
parameter get_row_parameter (const std::string &row) const
 
void touch (const std::string &id)
 
void unlearn (const std::string &id)
 
void update_entries (const jubatus::util::data::unordered_set< std::string > &neighbors)
 

Private Attributes

config config_
 
jubatus::util::lang::shared_ptr< framework::mixable_versioned_tablemixable_nearest_neighbor_
 
jubatus::util::lang::shared_ptr< framework::mixable_versioned_tablemixable_scores_
 
std::string my_id_
 
jubatus::util::lang::shared_ptr< nearest_neighbor::nearest_neighbor_basenearest_neighbor_engine_
 
jubatus::util::lang::shared_ptr< unlearner::unlearner_baseunlearner_
 

Detailed Description

Definition at line 45 of file light_lof.hpp.

Constructor & Destructor Documentation

jubatus::core::anomaly::light_lof::light_lof ( const config config,
const std::string &  id,
jubatus::util::lang::shared_ptr< nearest_neighbor::nearest_neighbor_base nearest_neighbor_engine 
)
jubatus::core::anomaly::light_lof::light_lof ( const config config,
const std::string &  id,
jubatus::util::lang::shared_ptr< nearest_neighbor::nearest_neighbor_base nearest_neighbor_engine,
jubatus::util::lang::shared_ptr< unlearner::unlearner_base unlearner 
)
jubatus::core::anomaly::light_lof::~light_lof ( )
virtual

Definition at line 128 of file light_lof.cpp.

128  {
129 }

Member Function Documentation

float jubatus::core::anomaly::light_lof::calc_anomaly_score ( const common::sfv_t query) const
virtual

Implements jubatus::core::anomaly::anomaly_base.

Definition at line 131 of file light_lof.cpp.

References collect_lrds().

131  {
132  std::vector<float> neighbor_lrds;
133  const float lrd = collect_lrds(query, neighbor_lrds);
134 
135  return calculate_lof(lrd, neighbor_lrds);
136 }
float collect_lrds(const common::sfv_t &query, std::vector< float > &neighbor_lrds) const
Definition: light_lof.cpp:226

Here is the call graph for this function:

float jubatus::core::anomaly::light_lof::calc_anomaly_score ( const std::string &  id) const
virtual

Implements jubatus::core::anomaly::anomaly_base.

Definition at line 138 of file light_lof.cpp.

References collect_lrds().

138  {
139  std::vector<float> neighbor_lrds;
140  const float lrd = collect_lrds(id, neighbor_lrds);
141 
142  return calculate_lof(lrd, neighbor_lrds);
143 }
float collect_lrds(const common::sfv_t &query, std::vector< float > &neighbor_lrds) const
Definition: light_lof.cpp:226

Here is the call graph for this function:

void jubatus::core::anomaly::light_lof::clear ( )
virtual

Implements jubatus::core::anomaly::anomaly_base.

Definition at line 145 of file light_lof.cpp.

References mixable_scores_, nearest_neighbor_engine_, and unlearner_.

Referenced by unpack().

145  {
146  nearest_neighbor_engine_->clear();
147  mixable_scores_->get_model()->clear();
148  if (unlearner_) {
149  unlearner_->clear();
150  }
151 }
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
jubatus::util::lang::shared_ptr< unlearner::unlearner_base > unlearner_
Definition: light_lof.hpp:131

Here is the caller graph for this function:

void jubatus::core::anomaly::light_lof::clear_row ( const std::string &  id)
virtual

Implements jubatus::core::anomaly::anomaly_base.

Definition at line 153 of file light_lof.cpp.

References JUBATUS_EXCEPTION.

153  {
154  throw JUBATUS_EXCEPTION(common::unsupported_method(__func__));
155 }
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
float jubatus::core::anomaly::light_lof::collect_lrds ( const common::sfv_t query,
std::vector< float > &  neighbor_lrds 
) const
private

Definition at line 226 of file light_lof.cpp.

References collect_lrds_from_neighbors(), config_, nearest_neighbor_engine_, and jubatus::core::anomaly::light_lof::config::nearest_neighbor_num.

Referenced by calc_anomaly_score().

228  {
229  std::vector<std::pair<std::string, float> > neighbors;
230  nearest_neighbor_engine_->neighbor_row(
231  query, neighbors, config_.nearest_neighbor_num);
232 
233  return collect_lrds_from_neighbors(neighbors, neighbor_lrds);
234 }
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

Here is the call graph for this function:

Here is the caller graph for this function:

float jubatus::core::anomaly::light_lof::collect_lrds ( const std::string &  query,
std::vector< float > &  neighbor_lrds 
) const
private

Definition at line 236 of file light_lof.cpp.

References collect_lrds_from_neighbors(), config_, nearest_neighbor_engine_, jubatus::core::anomaly::light_lof::config::nearest_neighbor_num, and jubatus::core::clustering::swap().

238  {
239  std::vector<std::pair<std::string, float> > neighbors;
240  nearest_neighbor_engine_->neighbor_row(
241  id, neighbors, config_.nearest_neighbor_num + 1);
242 
243  // neighbors may contain given id. We ignore it.
244  for (size_t i = 0; i < neighbors.size(); ++i) {
245  if (neighbors[i].first == id) {
246  std::swap(neighbors[i], neighbors.back());
247  neighbors.pop_back();
248  break;
249  }
250  }
251  if (neighbors.size() > static_cast<size_t>(config_.nearest_neighbor_num)) {
252  neighbors.resize(config_.nearest_neighbor_num);
253  }
254 
255  return collect_lrds_from_neighbors(neighbors, neighbor_lrds);
256 }
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
void swap(weighted_point &p1, weighted_point &p2)
Definition: types.hpp:47

Here is the call graph for this function:

float jubatus::core::anomaly::light_lof::collect_lrds_from_neighbors ( const std::vector< std::pair< std::string, float > > &  neighbors,
std::vector< float > &  neighbor_lrd 
) const
private

Definition at line 258 of file light_lof.cpp.

References get_row_parameter().

Referenced by collect_lrds().

260  {
261  neighbor_lrds.resize(neighbors.size());
262  if (neighbors.empty()) {
263  return std::numeric_limits<float>::infinity();
264  }
265 
266  // Collect parameters of given neighbors.
267  std::vector<parameter> parameters(neighbors.size());
268  for (size_t i = 0; i < neighbors.size(); ++i) {
269  parameters[i] = get_row_parameter(neighbors[i].first);
270  neighbor_lrds[i] = parameters[i].lrd;
271  }
272 
273  // Calculate LRD value of the query.
274  float sum_reachability = 0;
275  for (size_t i = 0; i < neighbors.size(); ++i) {
276  // Accumulate the reachability distance of the query and the i-th neighbor.
277  sum_reachability += std::max(neighbors[i].second, parameters[i].kdist);
278  }
279 
280  if (sum_reachability == 0) {
281  // All k-nearest neighbors are at the same point with given query.
282  return std::numeric_limits<float>::infinity();
283  }
284 
285  // LRD is an inverse of mean of reachability distances between the query and
286  // its k-nearest neighbors.
287  return neighbors.size() / sum_reachability;
288 }
parameter get_row_parameter(const std::string &row) const
Definition: light_lof.cpp:368

Here is the call graph for this function:

Here is the caller graph for this function:

void jubatus::core::anomaly::light_lof::collect_neighbors ( const std::string &  query,
jubatus::util::data::unordered_set< std::string > &  neighbors 
) const
private

Definition at line 290 of file light_lof.cpp.

References config_, nearest_neighbor_engine_, and jubatus::core::anomaly::light_lof::config::reverse_nearest_neighbor_num.

Referenced by set_row(), and unlearn().

292  {
293  std::vector<std::pair<std::string, float> > nn_result;
294  nearest_neighbor_engine_->neighbor_row(
295  query, nn_result, config_.reverse_nearest_neighbor_num);
296 
297  for (size_t i = 0; i < nn_result.size(); ++i) {
298  neighbors.insert(nn_result[i].first);
299  }
300 }
jubatus::util::lang::shared_ptr< nearest_neighbor::nearest_neighbor_base > nearest_neighbor_engine_
Definition: light_lof.hpp:130

Here is the caller graph for this function:

void jubatus::core::anomaly::light_lof::get_all_row_ids ( std::vector< std::string > &  ids) const
virtual

Implements jubatus::core::anomaly::anomaly_base.

Definition at line 180 of file light_lof.cpp.

References nearest_neighbor_engine_.

180  {
181  nearest_neighbor_engine_->get_all_row_ids(ids);
182 }
jubatus::util::lang::shared_ptr< nearest_neighbor::nearest_neighbor_base > nearest_neighbor_engine_
Definition: light_lof.hpp:130
std::vector< framework::mixable * > jubatus::core::anomaly::light_lof::get_mixables ( ) const
virtual

Implements jubatus::core::anomaly::anomaly_base.

Definition at line 188 of file light_lof.cpp.

References mixable_scores_, and nearest_neighbor_engine_.

188  {
189  std::vector<framework::mixable*> mixables;
190  mixables.push_back(nearest_neighbor_engine_->get_mixable());
191  mixables.push_back(mixable_scores_.get());
192  return mixables;
193 }
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
light_lof::parameter jubatus::core::anomaly::light_lof::get_row_parameter ( const std::string &  row) const
private

Definition at line 368 of file light_lof.cpp.

References JUBATUS_EXCEPTION, jubatus::core::anomaly::light_lof::parameter::kdist, jubatus::core::anomaly::light_lof::parameter::lrd, and mixable_scores_.

Referenced by collect_lrds_from_neighbors().

369  {
370  shared_ptr<column_table> table = mixable_scores_->get_model();
371  std::pair<bool, uint64_t> hit = table->exact_match(row);
372  if (!hit.first) {
373  throw JUBATUS_EXCEPTION(common::exception::runtime_error(
374  "row \"" + row + "\" not found in light_lof table"));
375  }
376  parameter param;
377  param.kdist = table->get_float_column(KDIST_COLUMN_INDEX)[hit.second];
378  param.lrd = table->get_float_column(LRD_COLUMN_INDEX)[hit.second];
379  return param;
380 }
jubatus::util::lang::shared_ptr< framework::mixable_versioned_table > mixable_scores_
Definition: light_lof.hpp:138
jubatus::core::common::jsonconfig::config parameter
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79

Here is the caller graph for this function:

bool jubatus::core::anomaly::light_lof::is_updatable ( ) const
inlinevirtual

Implements jubatus::core::anomaly::anomaly_base.

Definition at line 95 of file light_lof.hpp.

95  {
96  return false;
97  }
void jubatus::core::anomaly::light_lof::pack ( framework::packer packer) const
virtual

Implements jubatus::core::anomaly::anomaly_base.

Definition at line 382 of file light_lof.cpp.

References mixable_scores_, and nearest_neighbor_engine_.

382  {
383  packer.pack_array(2);
385  mixable_scores_->get_model()->pack(packer);
386 }
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
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
void jubatus::core::anomaly::light_lof::set_row ( const std::string &  id,
const common::sfv_t sfv 
)
virtual

Implements jubatus::core::anomaly::anomaly_base.

Definition at line 161 of file light_lof.cpp.

References collect_neighbors(), mixable_scores_, my_id_, nearest_neighbor_engine_, touch(), and update_entries().

161  {
162  unordered_set<std::string> update_set;
163 
164  shared_ptr<column_table> table = mixable_scores_->get_model();
165  if (table->exact_match(id).first) {
166  collect_neighbors(id, update_set);
167  }
168 
169  touch(id);
170  nearest_neighbor_engine_->set_row(id, sfv);
171  collect_neighbors(id, update_set);
172 
173  // Primarily add id to lof table with dummy parameters.
174  // update_entries() below overwrites this row.
175  table->add(id, storage::owner(my_id_), -1.f, -1.f);
176  update_set.insert(id);
177  update_entries(update_set);
178 }
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 touch(const std::string &id)
Definition: light_lof.cpp:197
void update_entries(const jubatus::util::data::unordered_set< std::string > &neighbors)
Definition: light_lof.cpp:302
void collect_neighbors(const std::string &query, jubatus::util::data::unordered_set< std::string > &neighbors) const
Definition: light_lof.cpp:290

Here is the call graph for this function:

void jubatus::core::anomaly::light_lof::touch ( const std::string &  id)
private

Definition at line 197 of file light_lof.cpp.

References JUBATUS_EXCEPTION, and unlearner_.

Referenced by set_row().

197  {
198  if (unlearner_) {
199  if (!unlearner_->touch(id)) {
200  throw JUBATUS_EXCEPTION(common::exception::runtime_error(
201  "no more space available to add new ID: " + id));
202  }
203  }
204 }
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
jubatus::util::lang::shared_ptr< unlearner::unlearner_base > unlearner_
Definition: light_lof.hpp:131

Here is the caller graph for this function:

std::string jubatus::core::anomaly::light_lof::type ( ) const
virtual

Implements jubatus::core::anomaly::anomaly_base.

Definition at line 184 of file light_lof.cpp.

184  {
185  return "light_lof";
186 }
void jubatus::core::anomaly::light_lof::unlearn ( const std::string &  id)
private

Definition at line 215 of file light_lof.cpp.

References collect_neighbors(), mixable_nearest_neighbor_, mixable_scores_, and update_entries().

215  {
216  unordered_set<std::string> reverse_knn;
217  collect_neighbors(key, reverse_knn);
218  reverse_knn.erase(key);
219 
220  mixable_nearest_neighbor_->get_model()->delete_row(key);
221  mixable_scores_->get_model()->delete_row(key);
222 
223  update_entries(reverse_knn);
224 }
jubatus::util::lang::shared_ptr< framework::mixable_versioned_table > mixable_scores_
Definition: light_lof.hpp:138
jubatus::util::lang::shared_ptr< framework::mixable_versioned_table > mixable_nearest_neighbor_
Definition: light_lof.hpp:135
void update_entries(const jubatus::util::data::unordered_set< std::string > &neighbors)
Definition: light_lof.cpp:302
void collect_neighbors(const std::string &query, jubatus::util::data::unordered_set< std::string > &neighbors) const
Definition: light_lof.cpp:290

Here is the call graph for this function:

void jubatus::core::anomaly::light_lof::unpack ( msgpack::object  o)
virtual

Implements jubatus::core::anomaly::anomaly_base.

Definition at line 388 of file light_lof.cpp.

References clear(), mixable_scores_, and nearest_neighbor_engine_.

388  {
389  if (o.type != msgpack::type::ARRAY || o.via.array.size != 2) {
390  throw msgpack::type_error();
391  }
392 
393  // clear before load
394  clear();
395 
396  nearest_neighbor_engine_->unpack(o.via.array.ptr[0]);
397  mixable_scores_->get_model()->unpack(o.via.array.ptr[1]);
398 }
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

Here is the call graph for this function:

void jubatus::core::anomaly::light_lof::update_entries ( const jubatus::util::data::unordered_set< std::string > &  neighbors)
private

Definition at line 302 of file light_lof.cpp.

References config_, mixable_scores_, my_id_, nearest_neighbor_engine_, jubatus::core::anomaly::light_lof::config::nearest_neighbor_num, and jubatus::core::storage::typed_column< T >::push_back().

Referenced by set_row(), and unlearn().

302  {
303  shared_ptr<column_table> table = mixable_scores_->get_model();
304  storage::float_column& kdist_column =
305  table->get_float_column(KDIST_COLUMN_INDEX);
306  storage::float_column& lrd_column = table->get_float_column(LRD_COLUMN_INDEX);
307 
308  std::vector<uint64_t> ids;
309  ids.reserve(neighbors.size());
310  for (unordered_set<std::string>::const_iterator it = neighbors.begin();
311  it != neighbors.end(); ++it) {
312  const std::pair<bool, uint64_t> hit = table->exact_match(*it);
313  if (hit.first) {
314  ids.push_back(hit.second);
315  }
316  }
317 
318  unordered_map<uint64_t, std::vector<std::pair<uint64_t, float> > >
319  nested_neighbors;
320 
321  // Gather k-nearest neighbors of each member of neighbors and update their
322  // k-dists.
323  std::vector<std::pair<std::string, float> > nn_result;
324  for (std::vector<uint64_t>::const_iterator it = ids.begin();
325  it != ids.end(); ++it) {
326  nearest_neighbor_engine_->neighbor_row(
327  table->get_key(*it), nn_result, config_.nearest_neighbor_num);
328  std::vector<std::pair<uint64_t, float> >& nn_indexes =
329  nested_neighbors[*it];
330 
331  nn_indexes.reserve(nn_result.size());
332  for (size_t i = 0; i < nn_result.size(); ++i) {
333  const std::pair<bool, uint64_t> hit =
334  table->exact_match(nn_result[i].first);
335  if (hit.first) {
336  nn_indexes.push_back(std::make_pair(hit.second, nn_result[i].second));
337  }
338  }
339 
340  kdist_column[*it] = nn_result.back().second;
341  }
342 
343  // Calculate LRDs of neighbors.
344  const storage::owner owner(my_id_);
345  for (std::vector<uint64_t>::const_iterator it = ids.begin();
346  it != ids.end(); ++it) {
347  const std::vector<std::pair<uint64_t, float> >& nn = nested_neighbors[*it];
348  float lrd = 1;
349  if (!nn.empty()) {
350  const size_t length = std::min(
351  nn.size(), static_cast<size_t>(config_.nearest_neighbor_num));
352  float sum_reachability = 0;
353  for (size_t i = 0; i < length; ++i) {
354  sum_reachability += std::max(nn[i].second, kdist_column[nn[i].first]);
355  }
356 
357  if (sum_reachability == 0) {
358  lrd = std::numeric_limits<float>::infinity();
359  } else {
360  lrd = length / sum_reachability;
361  }
362  }
363  lrd_column[*it] = lrd;
364  table->update_clock(*it, owner);
365  }
366 }
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
typed_column< float > float_column

Here is the call graph for this function:

Here is the caller graph for this function:

void jubatus::core::anomaly::light_lof::update_row ( const std::string &  id,
const sfv_diff_t diff 
)
virtual

Implements jubatus::core::anomaly::anomaly_base.

Definition at line 157 of file light_lof.cpp.

References JUBATUS_EXCEPTION.

157  {
158  throw JUBATUS_EXCEPTION(common::unsupported_method(__func__));
159 }
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79

Member Data Documentation

config jubatus::core::anomaly::light_lof::config_
private

Definition at line 140 of file light_lof.hpp.

Referenced by collect_lrds(), collect_neighbors(), and update_entries().

jubatus::util::lang::shared_ptr<framework::mixable_versioned_table> jubatus::core::anomaly::light_lof::mixable_nearest_neighbor_
private

Definition at line 135 of file light_lof.hpp.

Referenced by unlearn().

jubatus::util::lang::shared_ptr<framework::mixable_versioned_table> jubatus::core::anomaly::light_lof::mixable_scores_
private
std::string jubatus::core::anomaly::light_lof::my_id_
private

Definition at line 141 of file light_lof.hpp.

Referenced by set_row(), and update_entries().

jubatus::util::lang::shared_ptr<nearest_neighbor::nearest_neighbor_base> jubatus::core::anomaly::light_lof::nearest_neighbor_engine_
private
jubatus::util::lang::shared_ptr<unlearner::unlearner_base> jubatus::core::anomaly::light_lof::unlearner_
private

Definition at line 131 of file light_lof.hpp.

Referenced by clear(), and touch().


The documentation for this class was generated from the following files: