testErrorPropagation.cc
Go to the documentation of this file.
1 
9 #include <tst/Verify.h>
10 
11 #include <utl/AugerUnits.h>
12 #include <utl/AugerException.h>
13 
14 #include <iostream>
15 #include <cmath>
16 #include <vector>
17 
18 using namespace std;
19 using namespace utl;
20 using namespace tst;
21 
22 #include <cppunit/extensions/HelperMacros.h>
23 #include <boost/tuple/tuple.hpp>
24 
25 #include <utl/NumericalErrorPropagation.h>
26 #include <utl/CorrelationMatrix.h>
27 #include <utl/AugerException.h>
28 
29 #define ASSERT_EQUAL(x, y) CPPUNIT_ASSERT(x == y)
30 
34 class testErrorPropagation : public CppUnit::TestFixture {
35 
36  CPPUNIT_TEST_SUITE(testErrorPropagation);
37  CPPUNIT_TEST(TestCorrelationMatrix);
38  CPPUNIT_TEST(TestErrorPropagation);
39  CPPUNIT_TEST_SUITE_END();
40 
41 private:
42 
43  template<class Stream>
44  void
45  OutputMatrix(Stream& s, const CorrelationMatrix& c)
46  {
47  for (unsigned int i=0;i<c.GetNParameter();++i)
48  {
49  for (unsigned int j=0;j<c.GetNParameter()-1;++j)
50  s << c.Get(i,j) << " ";
51  s << c.Get(i,c.GetNParameter()-1) << "\n";
52  }
53  }
54 
55  // input 4, output 3
56  class TestFunctor : public FCNCalculator {
57  public:
58 
59  double m[3][4];
60 
61  std::vector<double> operator()(const std::vector<double>& inp) const
62  {
63  std::vector<double> out;
64  out.push_back(m[0][0]*inp[0]+m[0][1]*inp[1]+m[0][2]*inp[2]+m[0][3]*inp[3] + 10.);
65  out.push_back(m[1][0]*inp[0]+m[1][1]*inp[1]+m[1][2]*inp[2]+m[1][3]*inp[3] + 20.);
66  out.push_back(m[2][0]*inp[0]+m[2][1]*inp[1]+m[2][2]*inp[2]+m[2][3]*inp[3] + 30.);
67  return out;
68  }
69 
70  };
71 
72 public:
73 
74  void
76  {
77  { // test with nPar = 2
79  c.Set(0,1,0.3);
80 
81  ostringstream s;
82  OutputMatrix(s,c);
83 
84  const string truth =
85  "1 0.3\n"
86  "0.3 1\n";
87  ASSERT_EQUAL(s.str(),truth);
88  }
89 
90  { // test with nPar = 6
92  c.Set(0,1,0.1);
93  c.Set(2,0,0.2);
94  c.Set(0,3,0.3);
95  c.Set(4,0,0.4);
96  c.Set(0,5,0.5);
97  c.Set(1,2,-0.11);
98  c.Set(1,3,-0.21);
99  c.Set(4,1,-0.31);
100  c.Set(1,5,-0.41);
101  c.Set(2,3,0.12);
102  c.Set(2,4,0.22);
103  c.Set(2,5,0.32);
104  c.Set(4,3,-0.13);
105  c.Set(3,5,-0.23);
106  c.Set(5,4,0.111);
107 
108  ostringstream s;
109  OutputMatrix(s,c);
110 
111  const string truth =
112  "1 0.1 0.2 0.3 0.4 0.5\n"
113  "0.1 1 -0.11 -0.21 -0.31 -0.41\n"
114  "0.2 -0.11 1 0.12 0.22 0.32\n"
115  "0.3 -0.21 0.12 1 -0.13 -0.23\n"
116  "0.4 -0.31 0.22 -0.13 1 0.111\n"
117  "0.5 -0.41 0.32 -0.23 0.111 1\n";
118  ASSERT_EQUAL(s.str(),truth);
119  }
120  }
121 
122  void
124  {
125  vector<Parameter> input;
126  input.push_back(Parameter(1.23,0.321));
127  input.push_back(Parameter(4.56,0.654));
128  input.push_back(Parameter(7.89,0.987));
129  input.push_back(Parameter(2.01,0.234));
130 
131  CorrelationMatrix inputCorr(4);
132  inputCorr.Set(0,1, 0.11);
133  inputCorr.Set(0,2,-0.12);
134  inputCorr.Set(0,3,-0.13);
135  inputCorr.Set(1,2, 0.22);
136  inputCorr.Set(1,3,-0.23);
137  inputCorr.Set(2,3,-0.34);
138 
139  TestFunctor fcn; // does y = m*x + b
140  fcn.m[0][0] = 1.1;
141  fcn.m[0][1] = -2.2;
142  fcn.m[0][2] = 3.3;
143  fcn.m[0][3] = 4.4;
144  fcn.m[1][0] = -4.5;
145  fcn.m[1][1] = -5.6;
146  fcn.m[1][2] = -6.7;
147  fcn.m[1][3] = 7.8;
148  fcn.m[2][0] = 0.1;
149  fcn.m[2][1] = 0.2;
150  fcn.m[2][2] = -0.3;
151  fcn.m[2][3] = 0.5;
152 
153  NumericalErrorPropagation result(fcn,input,inputCorr);
154 
155  // V_out = J V_in J^T, mit J = Jacobi matrix, here for TestFunctor J = m
156 
157  double tmp[3][4];
158  for (int i=0;i<3;++i)
159  for (int j=0;j<4;++j)
160  {
161  tmp[i][j] = 0.0;
162  for (int k=0;k<4;++k)
163  tmp[i][j] += fcn.m[i][k]*inputCorr.Get(k,j)*input[k].error*input[j].error;
164  }
165 
166  ostringstream s1;
167  for (int i=0;i<3;++i)
168  for (int j=0;j<3;++j)
169  {
170  double vout = 0.0;
171  for (int k=0;k<4;++k)
172  vout += tmp[i][k]*fcn.m[j][k]; // fcn.m[j][k] is transposed of fcn.m[k][j]
173  s1 << vout << (j<2 ? " " : "\n");
174  }
175 
176  ostringstream s2;
177  for (int i=0;i<3;++i)
178  for (int j=0;j<3;++j)
179  {
180  s2 << result.GetPropagatedCorrelations().Get(i,j)*
181  result.GetPropagatedErrors()[i]*
182  result.GetPropagatedErrors()[j] << (j<2 ? " " : "\n");
183  }
184 
185  ASSERT_EQUAL(s1.str(),s2.str());
186  }
187 
188 };
189 
190 
void OutputMatrix(Stream &s, const CorrelationMatrix &c)
const std::vector< double > & GetPropagatedErrors() const
vector< t2list > out
output of the algorithm: a list of clusters
Definition: XbAlgo.cc:32
unsigned int GetNParameter() const
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
void Set(const int iPar, const int jPar, const double corr)
constexpr double s
Definition: AugerUnits.h:163
const Data result[]
const CorrelationMatrix & GetPropagatedCorrelations() const
double Get(const int iPar, const int jPar) const
std::vector< double > operator()(const std::vector< double > &inp) const
#define ASSERT_EQUAL(x, y)
constexpr double m
Definition: AugerUnits.h:121

, generated on Tue Sep 26 2023.