Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleUncertaintyTransform.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3 Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9 BIAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13 
14 BIAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public License
20 along with BIAS; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 
24 /** @example ExampleUncertaintyTransform.cpp
25  @relates UnscentedTransform, MonteCarloTransform
26  @ingroup g_examples
27  @brief this is an example transforming uncertainty with UnscentedTransform
28  @author woelk 07/2004
29 */
30 
31 #include <MathAlgo/UnscentedTransform.hh>
32 #include <MathAlgo/MonteCarloTransform.hh>
33 #include <Base/Math/Random.hh>
34 
35 using namespace BIAS;
36 using namespace std;
37 
39 
40 void MyConstTransform(const Vector<double>& src, Vector<double>& dst)
41 {
42  const int dim = src.Size();
43  T.newsize(dim, dim);
44  T.SetIdentity();
45  dst = T * src;
46 }
47 
48 void MyLinTransform(const Vector<double>& src, Vector<double>& dst)
49 {
50  const int dim = src.Size();
51  T.newsize(dim, dim);
52  const double c[] = { 1.0, 2.0, 0.0 };
53  const int numc = 3;
54  const int num = dim*dim;
55  for (int i=0; i<num; i++){
56  T.GetData()[i] = c[i%numc];
57  }
58  dst = T * src;
59 }
60 
61 void MySqTransform(const Vector<double>& src, Vector<double>& dst)
62 {
63  const double c[] = { 1.57, 3.14, 6.28 };
64  //const double c[] = { 1.0, 2.0, 0.0 };
65  const int dim = src.Size();
66  T.newsize(dim, dim);
67  dst.newsize(dim);
68  for (int i=0; i<dim; i++){
69  dst[i] = c[0] + c[1] * src[i] + c[2] * src[i] * src[i];
70  }
71 }
72 
73 void MyTransform(const Vector<double>& src, Vector<double>& dst)
74 {
75  MyLinTransform(src, dst);
76  //MyConstTransform(src, dst);
77  //MySqTransform(src, dst);
78 }
79 
80 /** \cond HIDDEN_SYMBOLS */
81 class MyUnscentedTransform
82  : public UnscentedTransform
83 {
84 public:
85  MyUnscentedTransform() : UnscentedTransform() {
86  /*AddDebugLevel(D_UT_TRANSFORM);
87  AddDebugLevel(D_UT_PARAM);
88  AddDebugLevel(D_UT_SIGMA_POINTS); */
89  };
90 
91  virtual ~MyUnscentedTransform() {};
92 
93 protected:
94 
95  virtual int Transform_(const Vector<double>& src,
96  Vector<double>& dst) const
97  {
98  MyTransform(src, dst);
99  return 0;
100  }
101 };
102 
103 class MyMonteCarloTransform
104  : public MonteCarloTransform
105 {
106 public:
107  MyMonteCarloTransform() : MonteCarloTransform() {
108  /*AddDebugLevel(D_UT_TRANSFORM);
109  AddDebugLevel(D_UT_PARAM);
110  AddDebugLevel(D_UT_SIGMA_POINTS); */
111  };
112 
113  virtual ~MyMonteCarloTransform() {};
114 
115 protected:
116 
117  virtual int Transform_(const Vector<double>& src,
118  Vector<double>& dst) const
119  {
120  MyTransform(src, dst);
121  return 0;
122  }
123 };
124 /** \endcond */
125 
126 
127 
128 
129 
130 int main(int /*argc*/, char ** /*argv*/)
131 {
132  Vector<double> src, dst, gt_dst;
133  Matrix<double> src_cov, dst_cov, gt_dst_cov;
134 
135  // generate src and src_cov using random number generator
136  const int dim = 2;
137  Random rand;
138  src.newsize(dim);
139  src_cov.newsize(dim, dim);
140  for (int r=0; r<dim; r++){
141  src[r] = rand.GetUniformDistributed(-10.0, 10.0);
142  for (int c=r; c<dim; c++){
143  src_cov[r][c] = src_cov[c][r] =
144  rand.GetUniformDistributed(-1.0, 1.0);
145  }
146  src_cov = src_cov * src_cov;
147  }
148  /*src[0] = 1.0;
149  src[1] = 2.0;
150  src_cov[0][0] = 0.5;
151  src_cov[1][1] = 0.75;
152  src_cov[0][1] = src_cov[1][0] = 0.25; */
153 
154 
155  cout << "src: "<<src<<endl;
156  cout << "src_cov: "<<src_cov<<endl;
157 
158  MyTransform(src, gt_dst);
159  gt_dst_cov = T * src_cov * T.Transpose();
160  cout << "T: "<<T<<endl;
161  cout << "gt: dst: "<<gt_dst<<endl;
162  cout << "gt: dst_cov: "<<gt_dst_cov<<endl;
163 
164  MyUnscentedTransform mut;
165  mut.SetBeta(2.0);
166  mut.SetAlpha(0.001);
167 
168  if (mut.Transform(src, src_cov, dst, dst_cov)!=0){
169  BIASERR("error transforming (unscented)");
170  return -1;
171  }
172  cout << "ut: dst: "<<dst<<endl;
173  cout << "ut: dst_cov: "<<dst_cov<<endl;
174 
175  cout << "diff: "<<(dst - gt_dst)<<endl;
176  cout << "diff cov: "<<(dst_cov - gt_dst_cov)<<endl;
177 
178  MyMonteCarloTransform mmct;
179  //mmct.AddDebugLevel("D_MCT_WRITE_SRC_SAMPLES");
180  //mmct.AddDebugLevel("D_MCT_WRITE_DST_SAMPLES");
181  //mmct.AddDebugLevel("D_MCT_EIGENVALVEC");
182  mmct.SetNumSamplesPerDimension((int)1e2);
183  mmct.SetUseQuasiRandomNumbers(true);
184  if (mmct.Transform(src, src_cov, dst, dst_cov)!=0){
185  BIASERR("error transforming (monte carlo)");
186  return -1;
187  }
188  cout << "mc: dst: "<<dst<<endl;
189  cout << "mc: dst_cov: "<<dst_cov<<endl;
190 
191  cout << "diff: "<<(dst - gt_dst)<<endl;
192  cout << "diff cov: "<<(dst_cov - gt_dst_cov)<<endl;
193  return 0;
194 }
Matrix< T > Transpose() const
transpose function, storing data destination matrix
Definition: Matrix.hh:823
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
unsigned int Size() const
length of the vector
Definition: Vector.hh:143
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
Vector< T > & newsize(Subscript N)
Definition: vec.h:220
T * GetData()
get the pointer to the data array of the matrix (for faster direct memeory access) ...
Definition: Matrix.hh:185
void SetIdentity()
Converts matrix to identity matrix.
Definition: Matrix.cpp:383
monte carlo propagation of uncertainty
uses the unscented transformation to map a normal distribututed random variable using a nonlinear tra...
class for producing random numbers from different distributions
Definition: Random.hh:51