Ignore:
Timestamp:
Mar 26, 2021, 4:29:42 AM (8 months ago)
Author:
Peter
Message:

add some test for correlation(Matrix); speed up, particuarly when #rows is much greater than #columns. Avoid copying and modifying the whole matrix, instead calculate simple sums and squared sums using BLAS functionality as much as possible.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/statistics.cc

    r3875 r4053  
    150150void test_correlation(test::Suite& suite)
    151151{
    152   utility::Matrix x(10,100);
     152  utility::Matrix x(100,10);
     153  for (size_t i=0; i<x.rows(); ++i)
     154    for (size_t j=0; j<x.columns(); ++j)
     155      x(i, j) = sin(5*i + j);
    153156  utility::Matrix C = statistics::correlation(x);
    154157  if (!suite.equal(C(0,0), 1.0)) {
     
    156159    suite.err() << "correlation element(0, 0) not unity\n";
    157160  }
     161
     162  for (size_t i=0; i<C.rows(); ++i)
     163    for (size_t j=0; j<C.columns(); ++j) {
     164      statistics::AveragerPair ap;
     165      add(ap, x.begin_column(i), x.end_column(i), x.begin_column(j));
     166      if (!suite.equal(C(i, j), ap.correlation(), 10)) {
     167        suite.add(false);
     168        suite.err() << "error: C(" << i << ", " << j << ") is " << C(i,j)
     169                    << "; expected " << ap.correlation() << "\n";
     170      }
     171    }
     172
    158173}
    159174
Note: See TracChangeset for help on using the changeset viewer.