yat  0.13.2pre
Spearman.h
1 #ifndef _theplu_yat_normalizer_spearman_
2 #define _theplu_yat_normalizer_spearman_
3 
4 // $Id: Spearman.h 3330 2014-10-14 08:03:25Z peter $
5 
6 /*
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010, 2014 Peter Johansson
9 
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11 
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16 
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24 */
25 
26 #include "utility.h"
27 
28 #include "yat/utility/concept_check.h"
29 #include "yat/utility/DataIterator.h"
30 #include "yat/utility/iterator_traits.h"
31 #include "yat/utility/sort_index.h"
33 #include "yat/utility/utility.h"
34 #include "yat/utility/WeightIterator.h"
35 
36 #include <boost/concept_check.hpp>
37 
38 #include <algorithm>
39 #include <functional>
40 #include <numeric>
41 #include <vector>
42 
43 namespace theplu {
44 namespace yat {
45 namespace normalizer {
46 
56  class Spearman
57  {
58  public:
62  Spearman(void){}
63 
78  template<typename RandomAccessIter1, typename RandomAccessIter2>
79  void operator()(RandomAccessIter1 first, RandomAccessIter1 last,
80  RandomAccessIter2 result) const
81  {
82  using boost_concepts::ReadableIterator;
83  BOOST_CONCEPT_ASSERT((ReadableIterator<RandomAccessIter1>));
84  using boost_concepts::RandomAccessTraversal;
85  BOOST_CONCEPT_ASSERT((RandomAccessTraversal<RandomAccessIter1>));
87  BOOST_CONCEPT_ASSERT((DataIteratorConcept<RandomAccessIter1>));
88 
89  BOOST_CONCEPT_ASSERT((ReadableIterator<RandomAccessIter2>));
90  using boost_concepts::WritableIterator;
91  BOOST_CONCEPT_ASSERT((WritableIterator<RandomAccessIter2>));
92  BOOST_CONCEPT_ASSERT((RandomAccessTraversal<RandomAccessIter2>));
93  BOOST_CONCEPT_ASSERT((DataIteratorConcept<RandomAccessIter2>));
94 
96  typename weighted_if_any2<RandomAccessIter1,RandomAccessIter2>::type tag;
97  normalize(first, last, result, tag);
98  }
99 
100 
101  private:
102  // unweighted version
103  template<typename RandomAccessIter1, typename RandomAccessIter2>
104  void normalize(RandomAccessIter1 first, RandomAccessIter1 last,
105  RandomAccessIter2 result,
107  {
108  std::vector<size_t> perm;
109  utility::sort_index(first, last, perm);
110  double n = perm.size();
111  size_t i=0;
112  while ( i<perm.size() ) {
113  size_t min_i = i;
114  while (i<perm.size() && first[perm[i]]<=first[perm[min_i]])
115  ++i;
116  double res = (i + min_i)/(2.0*n);
117  for ( ; min_i < i; ++min_i)
118  result[perm[min_i]] = res;
119  }
120  }
121 
122 
123  // weighted version
124  template<typename RandomAccessIter1, typename RandomAccessIter2>
125  void normalize(RandomAccessIter1 first, RandomAccessIter1 last,
126  RandomAccessIter2 result,
127  utility::weighted_iterator_tag) const
128  {
129  detail::copy_weight_if_weighted(first, last, result);
130  std::vector<size_t> perm;
131  utility::sort_index(first, last, perm);
132  utility::iterator_traits<RandomAccessIter1> trait;
133  utility::iterator_traits<RandomAccessIter2> rtrait;
134 
135  double total_w = utility::sum_weight(first, last);
136  double sum_w=0;
137  size_t i=0;
138  while ( i<perm.size() ) {
139  double w=0;
140  size_t min_i = i;
141  while (i<perm.size() && (trait.weight(first+perm[i])==0 ||
142  trait.data(first+perm[i]) <=
143  trait.data(first+perm[min_i])) ) {
144  w += trait.weight(first+perm[i]);
145  ++i;
146  }
147  double res = (sum_w + 0.5*w)/total_w;
148  for ( size_t j=min_i; j<i; ++j)
149  rtrait.data(result+perm[j]) = res;
150  sum_w += w;
151  }
152  }
153  };
154 
155 }}} // end of namespace normalizer, yat and thep
156 #endif
Concept check for Data Iterator.
Definition: concept_check.h:226
double sum_weight(Iterator first, Iterator last)
Definition: utility.h:524
Some useful functions are placed here.
Definition: iterator_traits.h:47
Spearman(void)
default constructor
Definition: Spearman.h:62
void operator()(RandomAccessIter1 first, RandomAccessIter1 last, RandomAccessIter2 result) const
Definition: Spearman.h:79
Replace elements with normalized rank.
Definition: Spearman.h:56
void sort_index(InputIterator first, InputIterator last, std::vector< size_t > &sort_index)
Definition: sort_index.h:147
Definition: iterator_traits.h:159

Generated on Wed Jan 4 2017 02:23:07 for yat by  doxygen 1.8.5