void test() { Stats::vector_type a(2); a(0) = 1; a(1) = 2; Stats::vector_type b(2); b(0) = 3; b(1) = 4; Stats::vector_type c(2); c(0) = 5; c(1) = 6; Stats::data_type data; data.push_back(a); data.push_back(b); data.push_back(c); Stats stats(data); if (os_) *os_ << "mean: " << stats.mean() << endl; if (os_) *os_ << "covariance: " << stats.covariance() << endl; // mean & covariance computed using good old-fashioned reckoning Stats::vector_type mean(2); mean(0) = 3; mean(1) = 4; Stats::matrix_type covariance(2,2); covariance(0,0) = covariance(0,1) = covariance(1,0) = covariance(1,1) = 8/3.; // verify results const double epsilon = 1e-12; unit_assert_vectors_equal(stats.mean(), mean, epsilon); unit_assert_matrices_equal(stats.covariance(), covariance, epsilon); double rms0_good = sqrt(35./3); double rms1_good = sqrt(56./3); double rms0_test = sqrt(stats.meanOuterProduct()(0,0)); double rms1_test = sqrt(stats.meanOuterProduct()(1,1)); unit_assert_equal(rms0_test, rms0_good, epsilon); unit_assert_equal(rms1_test, rms1_good, epsilon); }
void Stats::Impl::computeSums(const Stats::data_type& data) { if (data.size()>0) D_ = data[0].size(); sumData_ = Stats::vector_type(D_); sumOuterProducts_ = Stats::matrix_type(D_, D_); sumData_.clear(); sumOuterProducts_.clear(); for (Stats::data_type::const_iterator it=data.begin(); it!=data.end(); ++it) { if (it->size() != D_) { ostringstream message; message << "[Stats::Impl::computeSums()] " << D_ << "-dimensional data expected: " << *it; throw runtime_error(message.str()); } sumData_ += *it; sumOuterProducts_ += outer_prod(*it, *it); } }
void processTLP(const TruncatedLorentzianParameters& tlp, int trialCount, bool verbose) { if (verbose) cout << tlp << "\n\n\n"; else cout << columnHeader_ << endl; for (double snr=2; snr<=10; snr+=.5) for (double gridOffset=-.5; gridOffset<=.5; gridOffset+=.1) /* double snr = 100; double gridOffset = 0; */ { if (verbose) { cout << fixed; cout << "signal-to-noise ratio: " << snr << endl; cout << "relative grid offset: " << gridOffset << endl; } Stats::data_type data; double crBound = 0; for (int i=0; i<trialCount; i++) { try { Stats::vector_type result = runTrial(tlp, snr, gridOffset, crBound); data.push_back(result); } catch (exception& e) { cerr << e.what() << endl; cerr << "Caught exception, continuing.\n"; } } if (verbose) { cout << endl << setw(13) << "err_f0_pred" << setw(14) << "err_f0" << setw(14) << "err_tau" << setw(14) << "err_magnitude" << setw(14) << "err_phase" << setw(14) << "model_error" << setw(14) << "z-score" << endl; for (Stats::data_type::iterator it=data.begin(); it!=data.end(); ++it) outputVector(*it); } Stats stats(data); double mean_f0 = stats.mean()(F0); double variance_f0 = stats.covariance()(F0,F0); double expectedF0Squared = stats.meanOuterProduct()(F0,F0); if (verbose) { outputStats(stats); cout << endl; cout << fixed << noshowpos; cout << "<err_f0^2>: " << expectedF0Squared << endl; cout << "Cramer-Rao bound: " << crBound << endl << endl; cout << columnHeader_ << endl; } cout << noshowpos << fixed << setprecision(3) << setw(7) << snr << " " << setw(7) << gridOffset << " " << setprecision(10) << setw(14) << mean_f0 << " " << setw(14) << variance_f0 << " " << setw(14) << sqrt(variance_f0) << " " << setw(14) << expectedF0Squared << " " << setw(14) << crBound << endl; if (verbose) cout << "\n\n"; } }
Stats::Impl::Impl(const Stats::data_type& data) : D_(0), N_(data.size()) { computeSums(data); }