1 | // $Id: qQuantileNormalizer.cc 1739 2009-01-21 01:34:18Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2009 Jari Häkkinen, Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 3 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | */ |
---|
21 | |
---|
22 | #include "qQuantileNormalizer.h" |
---|
23 | |
---|
24 | #include "yat/regression/CSplineInterpolation.h" |
---|
25 | #include "yat/statistics/Averager.h" |
---|
26 | #include "yat/statistics/AveragerWeighted.h" |
---|
27 | #include "yat/utility/DataWeight.h" |
---|
28 | #include "yat/utility/Vector.h" |
---|
29 | #include "yat/utility/VectorBase.h" |
---|
30 | #include "yat/utility/WeightIterator.h" |
---|
31 | |
---|
32 | #include <algorithm> |
---|
33 | #include <cassert> |
---|
34 | #include <numeric> |
---|
35 | #include <sstream> |
---|
36 | #include <stdexcept> |
---|
37 | #include <string> |
---|
38 | #include <vector> |
---|
39 | |
---|
40 | namespace theplu { |
---|
41 | namespace yat { |
---|
42 | namespace normalizer { |
---|
43 | |
---|
44 | |
---|
45 | void |
---|
46 | qQuantileNormalizer::Partitioner::init(const utility::VectorBase& sortedvec, |
---|
47 | unsigned int N) |
---|
48 | { |
---|
49 | assert(N>1); |
---|
50 | assert(N<=sortedvec.size()); |
---|
51 | double range=static_cast<double>(sortedvec.size())/N; |
---|
52 | assert(range); |
---|
53 | unsigned int start=0; |
---|
54 | for (unsigned int i=0; i<N; ++i) { |
---|
55 | unsigned int end = ( i==(N-1) ? sortedvec.size() : |
---|
56 | static_cast<unsigned int>((i+1)*range) ); |
---|
57 | statistics::Averager av; |
---|
58 | for (unsigned int r=start; r<end; ++r) |
---|
59 | av.add(sortedvec(r)); |
---|
60 | average_(i) = av.mean(); |
---|
61 | index_(i) = 0.5*(end+start-1); |
---|
62 | start=end; |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | void qQuantileNormalizer::Partitioner::init |
---|
68 | (const std::vector<utility::DataWeight>& sortedvec, unsigned int N) |
---|
69 | { |
---|
70 | assert(N>1); |
---|
71 | assert(N<=sortedvec.size()); |
---|
72 | double total_w = std::accumulate(utility::weight_iterator(sortedvec.begin()), |
---|
73 | utility::weight_iterator(sortedvec.end()), |
---|
74 | 0.0); |
---|
75 | |
---|
76 | assert(total_w); |
---|
77 | double sum_w = 0; |
---|
78 | std::vector<utility::DataWeight>::const_iterator iter(sortedvec.begin()); |
---|
79 | for (unsigned int i=0; i<N; ++i) { |
---|
80 | statistics::AveragerWeighted av; |
---|
81 | double end_sum_w = (i+1) * total_w / N - sum_w; |
---|
82 | if (i!=N-1) { |
---|
83 | while(av.sum_w() < end_sum_w) { |
---|
84 | av.add(iter->data(), iter->weight()); |
---|
85 | ++iter; |
---|
86 | } |
---|
87 | } |
---|
88 | // use all remaining data for last bin (to avoid problems |
---|
89 | // due to rounding errors) |
---|
90 | else |
---|
91 | add(av, iter, sortedvec.end()); |
---|
92 | |
---|
93 | if (av.sum_w() == 0) { |
---|
94 | std::stringstream ss; |
---|
95 | ss << "yat::normalizer::qQuantileNormalizer: relative weight too " |
---|
96 | << "large in\n"; |
---|
97 | throw std::runtime_error(ss.str()); |
---|
98 | } |
---|
99 | average_(i) = av.mean(); |
---|
100 | index_(i) = sum_w + 0.5*av.sum_w(); |
---|
101 | sum_w += av.sum_w(); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | const utility::Vector& qQuantileNormalizer::Partitioner::averages(void) const |
---|
107 | { |
---|
108 | return average_; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | const utility::Vector& qQuantileNormalizer::Partitioner::index(void) const |
---|
113 | { |
---|
114 | return index_; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | size_t qQuantileNormalizer::Partitioner::size(void) const |
---|
119 | { |
---|
120 | return average_.size(); |
---|
121 | } |
---|
122 | |
---|
123 | }}} // end of namespace normalizer, yat and thep |
---|