ProteoWizard
StatsTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: StatsTest.cpp 4129 2012-11-20 00:05:37Z chambm $
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
6 //
7 // Copyright 2006 Louis Warschaw Prostate Cancer Center
8 // Cedars Sinai Medical Center, Los Angeles, California 90048
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //
22 
23 
24 #include "Stats.hpp"
27 #include <cstring>
28 
29 
30 using namespace pwiz::util;
31 using namespace pwiz::math;
32 
33 
34 ostream* os_ = 0;
35 
36 
37 void test()
38 {
39  Stats::vector_type a(2);
40  a(0) = 1;
41  a(1) = 2;
42 
43  Stats::vector_type b(2);
44  b(0) = 3;
45  b(1) = 4;
46 
47  Stats::vector_type c(2);
48  c(0) = 5;
49  c(1) = 6;
50 
51  Stats::data_type data;
52  data.push_back(a);
53  data.push_back(b);
54  data.push_back(c);
55 
56  Stats stats(data);
57  if (os_) *os_ << "mean: " << stats.mean() << endl;
58  if (os_) *os_ << "covariance: " << stats.covariance() << endl;
59 
60  // mean & covariance computed using good old-fashioned reckoning
61  Stats::vector_type mean(2);
62  mean(0) = 3;
63  mean(1) = 4;
64  Stats::matrix_type covariance(2,2);
65  covariance(0,0) = covariance(0,1) = covariance(1,0) = covariance(1,1) = 8/3.;
66 
67  // verify results
68  const double epsilon = 1e-12;
69  unit_assert_vectors_equal(stats.mean(), mean, epsilon);
70  unit_assert_matrices_equal(stats.covariance(), covariance, epsilon);
71 
72  double rms0_good = sqrt(35./3);
73  double rms1_good = sqrt(56./3);
74  double rms0_test = sqrt(stats.meanOuterProduct()(0,0));
75  double rms1_test = sqrt(stats.meanOuterProduct()(1,1));
76  unit_assert_equal(rms0_test, rms0_good, epsilon);
77  unit_assert_equal(rms1_test, rms1_good, epsilon);
78 }
79 
80 
81 int main(int argc, char* argv[])
82 {
83  TEST_PROLOG(argc, argv)
84 
85  try
86  {
87  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
88  if (os_) *os_ << "StatsTest\n";
89  test();
90  }
91  catch (exception& e)
92  {
93  TEST_FAILED(e.what())
94  }
95  catch (...)
96  {
97  TEST_FAILED("Caught unknown exception.")
98  }
99 
101 }
102