ProteoWizard
StatsTest.cpp
Go to the documentation of this file.
1//
2// $Id$
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
30using namespace pwiz::util;
31using namespace pwiz::math;
32
33
34ostream* os_ = 0;
35
36
37void test()
38{
40 a(0) = 1;
41 a(1) = 2;
42
44 b(0) = 3;
45 b(1) = 4;
46
48 c(0) = 5;
49 c(1) = 6;
50
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;
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
81int 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
int main(int argc, char *argv[])
Definition StatsTest.cpp:81
ostream * os_
Definition StatsTest.cpp:34
void test()
Definition StatsTest.cpp:37
std::vector< vector_type > data_type
Definition Stats.hpp:46
boost::numeric::ublas::vector< double > vector_type
Definition Stats.hpp:44
boost::numeric::ublas::matrix< double > matrix_type
Definition Stats.hpp:45
vector_type mean() const
matrix_type covariance() const
matrix_type meanOuterProduct() const
const double epsilon
Definition DiffTest.cpp:41
#define unit_assert_matrices_equal(A, B, epsilon)
Definition unit.hpp:135
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define unit_assert_equal(x, y, epsilon)
Definition unit.hpp:99
#define unit_assert_vectors_equal(A, B, epsilon)
Definition unit.hpp:139
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175