source: trunk/yat/normalizer/Spearman.h @ 1509

Last change on this file since 1509 was 1509, checked in by Peter, 13 years ago

fixes #437 and added support for having known issues in test::Suite

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1#ifndef _theplu_yat_normalizer_spearman_
2#define _theplu_yat_normalizer_spearman_
3
4/*
5  Copyright (C) 2008 Peter Johansson
6
7  This file is part of the yat library, http://dev.thep.lu.se/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 3 of the
12  License, or (at your option) any later version.
13
14  The yat library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "yat/utility/DataIterator.h"
24#include "yat/utility/iterator_traits.h"
25#include "yat/utility/sort_index.h"
26#include "yat/utility/WeightIterator.h"
27
28#include <algorithm>
29#include <functional>
30#include <vector>
31
32namespace theplu {
33namespace yat {
34namespace normalizer {
35
36  /**
37     \brief Replace elements with normalized rank
38
39     \since New in yat 0.5
40   */
41  class Spearman
42  {
43  public:
44    /**
45       \brief default constructor
46     */
47    Spearman(void){}
48
49    /**
50       It is possible to centralize a range "in place"; it is
51       permissible for the iterators \a first and \a result to be the
52       same.
53
54       \note Currently weighted version is not implemented
55
56       \return result + (last-first)
57     */
58    template<typename ForwardIterator, typename RandomAccessIterator>
59    RandomAccessIterator operator()(ForwardIterator first, ForwardIterator last,
60                                    RandomAccessIterator result) const
61    {
62      typename utility::weighted_iterator_traits<ForwardIterator>::type tag;
63      return normalize(first, last, result, tag);
64    }
65
66
67  private:
68    // unweighted version
69    template<typename ForwardIterator, typename RandomAccessIterator>
70    RandomAccessIterator normalize(ForwardIterator first, ForwardIterator last,
71                                   RandomAccessIterator result, 
72                                   utility::unweighted_iterator_tag) const
73    {
74      std::vector<size_t> perm;
75      utility::sort_index(first, last, perm);
76      double n = perm.size();
77      for ( size_t i=0; i<perm.size(); ++i)
78        result[perm[i]] = static_cast<double>(i)/n;
79      return result + std::distance(first, last);
80    }
81
82
83    // weighted version
84    template<typename ForwardIterator, typename RandomAccessIterator>
85    RandomAccessIterator normalize(ForwardIterator first, ForwardIterator last,
86                                   RandomAccessIterator result, 
87                                   utility::weighted_iterator_tag) const
88    {
89      std::copy(utility::weight_iterator(first), 
90                utility::weight_iterator(last),
91                utility::weight_iterator(result));
92      // set values with w=0 to 0 to avoid problems with NaNs
93      utility::iterator_traits<ForwardIterator> forward_trait;
94      for (ForwardIterator i=first; i!=last; ++i)
95        if (forward_trait.weight(i)==0)
96          forward_trait.data(i)=0.0;
97
98      std::vector<size_t> index(std::distance(first, last));
99      utility::sort_index(utility::data_iterator(first), 
100                          utility::data_iterator(last), index);
101      utility::iterator_traits<RandomAccessIterator> trait;
102      trait.data(result+index[0])=0;
103      for (size_t i=1; i<index.size(); ++i)
104        trait.data(result+index[i]) = 
105          trait.data(result+index[i-1]) + trait.weight(result+index[i-1]);
106      size_t n = std::distance(first, last);
107      double w_sum = trait.data(result+index.back()) + 
108        trait.weight(result+index.back());
109      std::transform(utility::data_iterator(result),
110                     utility::data_iterator(result+n),
111                     utility::data_iterator(result),
112                     std::bind2nd(std::divides<double>(), w_sum));
113      return result + n;
114    }
115
116  };
117
118}}} // end of namespace normalizer, yat and thep
119#endif
Note: See TracBrowser for help on using the repository browser.