Changeset 3138


Ignore:
Timestamp:
Nov 29, 2013, 3:07:58 AM (7 years ago)
Author:
Peter
Message:

change name of class and implement function llr(). closes #769

Location:
trunk
Files:
2 edited
3 moved

Legend:

Unmodified
Added
Removed
  • trunk/test/Makefile.am

    r3134 r3138  
    5959  test/kernel_pca.test test/kernel.test \
    6060  test/knn.test test/kolmogorov_smirnov.test test/large_file.test \
    61   test/loglikelihood_ratio_test_binomial.test \
     61  test/likelihood_ratio_test_binomial.test \
    6262  test/matrix_lookup.test \
    6363  test/matrix_lookup_weighted.test test/matrix.test \
     
    8181
    8282# tests not passing through yet
    83 XFAIL_TESTS = test/loglikelihood_ratio_test_binomial.test
     83XFAIL_TESTS =
    8484
    8585DISTRIBUTED_TESTS = \
  • trunk/test/likelihood_ratio_test_binomial.cc

    r3134 r3138  
    2424#include "Suite.h"
    2525
    26 #include "yat/statistics/LoglikelihoodRatioTestBinomial.h"
     26#include "yat/statistics/LikelihoodRatioTestBinomial.h"
    2727
    2828using namespace theplu::yat;
     
    3232  test::Suite suite(argc, argv);
    3333
    34   statistics::LoglikelihoodRatioTestBinomial lrt;
     34  statistics::LikelihoodRatioTestBinomial lrt;
    3535
    3636  suite.add(lrt.size() == 2);
     
    4949  suite.add(lrt.negative(1) == 5000);
    5050
    51   double log_lambda = lrt();
     51  double log_lambda = lrt.llr();
    5252  suite.out() << log_lambda << "\n";
    5353  suite.add(log_lambda > 0);
  • trunk/yat/statistics/LikelihoodRatioTestBinomial.cc

    r3134 r3138  
    2222#include <config.h>
    2323
    24 #include "LoglikelihoodRatioTestBinomial.h"
     24#include "LikelihoodRatioTestBinomial.h"
    2525
     26#include <cassert>
     27#include <cmath>
    2628#include <numeric>
    27 #include <cassert>
    2829
    2930namespace theplu {
     
    3132namespace statistics {
    3233
    33   LoglikelihoodRatioTestBinomial::LoglikelihoodRatioTestBinomial(size_t n)
    34     : neg_(n, 0), pos_(n, 0)
     34  LikelihoodRatioTestBinomial::LikelihoodRatioTestBinomial(size_t n)
     35    : count_(2, n, 0)
    3536  {}
    3637
    37   void LoglikelihoodRatioTestBinomial::add(size_t population, bool positive,
     38  void LikelihoodRatioTestBinomial::add(size_t population, bool positive,
    3839                                           long int n)
    3940  {
    40     assert(population < neg_.size());
    41     assert(population < pos_.size());
    42     if (positive)
    43       pos_[population] += n;
    44     else
    45       neg_[population] += n;
     41    assert(population < count_.columns());
     42    assert(population < count_.columns());
     43    count_(positive ? 1 : 0, population) += n;
    4644  }
    4745
    4846
    49   unsigned long int LoglikelihoodRatioTestBinomial::n(void) const
     47  unsigned long int LikelihoodRatioTestBinomial::n(void) const
    5048  {
    51     return negative() + positive();
     49    return positive() + negative();
    5250  }
    5351
    5452
    55   unsigned long int LoglikelihoodRatioTestBinomial::n(size_t i) const
     53  unsigned long int LikelihoodRatioTestBinomial::n(size_t i) const
    5654  {
    57     assert(i<neg_.size());
    58     assert(i<pos_.size());
    59     return pos_[i] + neg_[i];
     55    return positive(i) + negative(i);
    6056  }
    6157
    6258
    63   unsigned long int LoglikelihoodRatioTestBinomial::negative(void) const
     59  unsigned long int LikelihoodRatioTestBinomial::negative(void) const
    6460  {
    65     return std::accumulate(neg_.begin(), neg_.end(), 0);
     61    return sum(count_.row_const_view(0));
    6662  }
    6763
    6864
    69   unsigned long int LoglikelihoodRatioTestBinomial::negative(size_t i) const
     65  unsigned long int LikelihoodRatioTestBinomial::negative(size_t i) const
    7066  {
    71     assert(i<neg_.size());
    72     return neg_[i];
     67    assert(i<count_.columns());
     68    return count_(0, i);
    7369  }
    7470
    7571
    76   unsigned long int LoglikelihoodRatioTestBinomial::positive(void) const
     72  unsigned long int LikelihoodRatioTestBinomial::positive(void) const
    7773  {
    78     return std::accumulate(pos_.begin(), pos_.end(), 0);
     74    return sum(count_.row_const_view(1));
    7975  }
    8076
    8177
    82   unsigned long int LoglikelihoodRatioTestBinomial::positive(size_t i) const
     78  unsigned long int LikelihoodRatioTestBinomial::positive(size_t i) const
    8379  {
    84     assert(i<pos_.size());
    85     return pos_[i];
     80    assert(i<count_.columns());
     81    return count_(1, i);
    8682  }
    8783
    8884
    89   size_t LoglikelihoodRatioTestBinomial::size(void) const
     85  size_t LikelihoodRatioTestBinomial::size(void) const
    9086  {
    91     assert(neg_.size()==pos_.size());
    92     return neg_.size();
     87    return count_.columns();
    9388  }
    9489
    9590
    96   double LoglikelihoodRatioTestBinomial::operator()(void) const
     91  double LikelihoodRatioTestBinomial::llr(void) const
    9792  {
    98     return 0.0;
     93    double res = 0;
     94    for (size_t i=0; i<size(); ++i) {
     95      double ni = n(i);
     96      if (positive(i))
     97        res += positive(i) * std::log(positive(i)/ni);
     98      if (negative(i))
     99        res += negative(i) * std::log(negative(i)/ni);
     100    }
     101    double npos = positive();
     102    double nneg = negative();
     103    double n = npos + nneg;
     104    res -= npos * std::log(npos/n);
     105    res -= nneg * std::log(nneg/n);
     106    return res;
    99107  }
    100108
  • trunk/yat/statistics/LikelihoodRatioTestBinomial.h

    r3134 r3138  
    2323*/
    2424
    25 #include <vector>
     25#include "yat/utility/Matrix.h"
    2626
    2727namespace theplu {
     
    3030
    3131  /**
    32      \brief Loglikelihood-ratio test for binomial data
     32     \brief Likelihood-ratio test for binomial data
    3333
    3434     This class is useful when having binomial data for several
     
    3636     differen samples.
    3737  */
    38   class LoglikelihoodRatioTestBinomial
     38  class LikelihoodRatioTestBinomial
    3939  {
    4040  public:
     
    4343       \param n number of populations in data
    4444     */
    45     LoglikelihoodRatioTestBinomial(size_t n=2);
     45    LikelihoodRatioTestBinomial(size_t n=2);
    4646
    4747    /**
     
    9090    /**
    9191       \brief Calculate natural logarithm of likelihood ratios
     92
     93       The logairithm of the likelihood ratio is calculated as
     94       \f$ LLR = \sum_i \left ( n_{i+} \ln \frac{n_{i+}}{n_i} +
     95       n_{i-} \ln \frac{n_{i-}}{n_i} \right ) -
     96       \left ( n_+ \ln \frac{n_+}{n} +
     97       n_- \ln \frac{n_-}{n} \right )
     98       \f$
    9299     */
    93     double operator()(void) const;
     100    double llr(void) const;
    94101
    95102  private:
    96     std::vector<unsigned long int> neg_;
    97     std::vector<unsigned long int> pos_;
     103    utility::Matrix count_;
    98104  };
    99105
  • trunk/yat/statistics/Makefile.am

    r3134 r3138  
    3232  yat/statistics/Kendall.cc yat/statistics/KolmogorovSmirnov.cc \
    3333  yat/statistics/KolmogorovSmirnovOneSample.cc \
    34   yat/statistics/LoglikelihoodRatioTestBinomial.cc \
     34  yat/statistics/LikelihoodRatioTestBinomial.cc \
    3535  yat/statistics/Pearson.cc \
    3636  yat/statistics/PearsonCorrelation.cc yat/statistics/Percentiler.cc \
     
    6262  $(srcdir)/yat/statistics/KolmogorovSmirnov.h \
    6363  $(srcdir)/yat/statistics/KolmogorovSmirnovOneSample.h \
    64   $(srcdir)/yat/statistics/LoglikelihoodRatioTestBinomial.h \
     64  $(srcdir)/yat/statistics/LikelihoodRatioTestBinomial.h \
    6565  $(srcdir)/yat/statistics/Pearson.h \
    6666  $(srcdir)/yat/statistics/PearsonCorrelation.h \
Note: See TracChangeset for help on using the changeset viewer.