source: trunk/yat/normalizer/qQuantileNormalizer.cc @ 1738

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

added support for creation of an qQuantileNormalizer from a weighted range. Addresses #478

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1// $Id: qQuantileNormalizer.cc 1738 2009-01-20 01:58:44Z 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
40namespace theplu {
41namespace yat {
42namespace 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      std::cout << "end_sum_w: " << end_sum_w << std::endl;
83      if (i!=N-1) {
84        while(av.sum_w() < end_sum_w) {
85          av.add(iter->data(), iter->weight());
86          ++iter;
87        }
88      }
89      // use all remaining data for last bin (to avoid problems
90      // due to rounding errors)
91      else 
92        add(av, iter, sortedvec.end());
93
94      if (av.sum_w() == 0) {
95        std::stringstream ss;
96        ss << "yat::normalizer::qQuantileNormalizer: relative weight too "
97           << "large in\n";
98        throw std::runtime_error(ss.str());
99      }
100      average_(i) = av.mean();
101      index_(i)   = sum_w + 0.5*av.sum_w();
102      sum_w += av.sum_w();
103    }
104  }
105
106
107  const utility::Vector& qQuantileNormalizer::Partitioner::averages(void) const
108  {
109    return average_;
110  }
111
112
113  const utility::Vector& qQuantileNormalizer::Partitioner::index(void) const
114  {
115    return index_;
116  }
117
118
119  size_t qQuantileNormalizer::Partitioner::size(void) const
120  {
121    return average_.size();
122  }
123
124}}} // end of namespace normalizer, yat and thep
Note: See TracBrowser for help on using the repository browser.