Changeset 1521 for trunk


Ignore:
Timestamp:
Sep 21, 2008, 7:54:19 PM (13 years ago)
Author:
Peter
Message:

Adding a Zscore normalizer

Location:
trunk
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/test/normalization_test.cc

    r1520 r1521  
    2828#include "yat/normalizer/RowNormalizer.h"
    2929#include "yat/normalizer/Spearman.h"
     30#include "yat/normalizer/Zscore.h"
    3031
    3132#include "yat/utility/DataIterator.h"
     
    4445void test_spearman(test::Suite&);
    4546void test_spearman_weighted(test::Suite&);
     47void test_z_score(test::Suite&);
    4648
    4749int main(int argc, char* argv[])
     
    5557  test_row_normalize(suite);
    5658  test_spearman(suite);
     59  test_z_score(suite);
    5760
    5861  return suite.return_value();
     
    220223  suite.add(suite.equal(res(0,3).data(), 0.5/3));
    221224}
     225
     226void test_z_score(test::Suite& suite)
     227{
     228  suite.err() << "Testing Zscore\n";
     229  std::vector<double> vec;
     230  vec.push_back(0);
     231  vec.push_back(3.14);
     232  normalizer::Zscore zscore;
     233  zscore(vec.begin(), vec.end(), vec.begin());
     234  for (size_t i=0; i<vec.size(); ++i)
     235    suite.add(suite.equal(vec[i], 2.0*i-1.0));
     236}
     237
     238
  • trunk/yat/normalizer/Makefile.am

    r1520 r1521  
    2626
    2727include_normalizer_HEADERS = Centralizer.h ColumnNormalizer.h \
    28   QuantileNormalizer.h RowNormalizer.h Spearman.h
     28  QuantileNormalizer.h RowNormalizer.h Spearman.h Zscore.h
  • trunk/yat/normalizer/Zscore.h

    r1517 r1521  
    1 #ifndef _theplu_yat_normalizer_centralizer_
    2 #define _theplu_yat_normalizer_centralizer_
     1#ifndef _theplu_yat_normalizer_z_score_
     2#define _theplu_yat_normalizer_z_score_
    33
    44/*
     
    2121*/
    2222
    23 #include "yat/statistics/Average.h"
    24 
    25 #include <algorithm>
    26 #include <functional>
     23#include "yat/statistics/Averager.h"
    2724
    2825namespace theplu {
     
    3128
    3229  /**
    33      \brief Centralize a range
     30     \brief Zero mean and unity variance
    3431
    35      The center is calculated and then that value is subtracted from
    36      each element. By default the center is defined as the arithmetic
    37      mean, but this can be changed by providing a suitable
    38      UnaryFunction.
     32     Create a range that has zero mean and unity variance
    3933
    4034     \since New in yat 0.5
    4135   */
    42   template<class UnaryFunction = statistics::Average>
    43   class Centralizer
     36  class Zscore
    4437  {
    4538  public:
    4639    /**
    47        \brief default constructor
    48 
    49        Creates a UnaryFunction using default constructor.
    50      */
    51     Centralizer(void){}
    52 
    53     /**
    54        \param uf unary function defining the center.
    55      */
    56     Centralizer(const UnaryFunction& uf)
    57       : func_(uf) {}
    58 
    59     /**
    60        Calculates the center \a c of the range [first, last) using
    61        UnaryFunction. This value, \a c, is then subtracted from each
    62        element in the range [first, last) and assigned to the
    63        corresponding element in range [result, result + (last-first) ).
     40       The element in range [result, result + (last-first)) is
     41       calculated as result[i] = (first[i] - m) / std where m and std
     42       are the mean and standard deviation, respectively, of the range
     43       [first, last).
    6444
    6545       It is possible to centralize a range "in place"; it is
     
    7353                              OutputIterator result) const
    7454    {
    75       return std::transform(first, last,
    76                             result, std::bind2nd(std::minus<double>(),
    77                                                  func_(first, last)));
     55      statistics::Averager a;
     56      add(a, first, last);
     57      double m = a.mean();
     58      double std = a.std();
     59      while (first!=last) {
     60        *result = (*first - m) / std;
     61        ++first;
     62        ++result;
     63      }
     64      return result;
    7865    }
    7966
    80   private:
    81     UnaryFunction func_;
    8267  };
    8368
Note: See TracChangeset for help on using the changeset viewer.