openModeller  Version 1.5.0
MeanVarianceNormalizer.cpp
Go to the documentation of this file.
1 
28 #include <openmodeller/Sampler.hh>
30 #include <openmodeller/Log.hh>
31 
32 /*******************/
33 /*** constructor ***/
35  _mean(),
36  _stddev()
37 {
38 }
39 
40 /*******************/
41 /*** destructor ***/
43 
44 /****************/
45 /*** get Copy ***/
47 
48  return new MeanVarianceNormalizer( *this );
49 }
50 
51 /*****************************/
52 /*** compute Normalization ***/
54 
55  int dim = samplerPtr->numIndependent();
56 
57  _mean.resize(dim);
58  _stddev.resize(dim);
59 
60  int numPoints = samplerPtr->numPresence() + samplerPtr->numAbsence();
61 
62  // Join all occurrences
63  OccurrencesPtr presences = samplerPtr->getPresences();
64  OccurrencesPtr absences = samplerPtr->getAbsences();
65 
66  OccurrencesPtr allOccs( new OccurrencesImpl( presences->label(), presences->coordSystem() ) );
67 
68  allOccs->appendFrom( presences );
69  allOccs->appendFrom( absences );
70 
71  // Compute mean
72  OccurrencesImpl::const_iterator p_iterator = allOccs->begin();
73  OccurrencesImpl::const_iterator p_end = allOccs->end();
74 
75  while ( p_iterator != p_end ) {
76 
77  Sample point = (*p_iterator)->environment();
78 
79  _mean += point;
80 
81  ++p_iterator;
82  }
83 
84  _mean /= Scalar( numPoints );
85 
86  // Compute standard deviation
87  p_iterator = allOccs->begin();
88 
89  while ( p_iterator != p_end ) {
90 
91  Sample point = (*p_iterator)->environment();
92 
93  point -= _mean;
94  point *= point;
95 
96  _stddev += point;
97 
98  ++p_iterator;
99  }
100 
101  _stddev /= Scalar( numPoints - 1 );
102  _stddev.sqrt();
103 }
104 
105 /*****************/
106 /*** normalize ***/
108 
109  if ( samplePtr->size() != 0 ) {
110 
111  *samplePtr -= _mean;
112  *samplePtr /= _stddev;
113  }
114 }
115 
116 /*************************/
117 /*** get configuration ***/
119 
120  ConfigurationPtr config( new ConfigurationImpl("Normalization") );
121 
122  config->addNameValue( "Class", "MeanVarianceNormalizer" );
123 
124  config->addNameValue( "Mean", _mean );
125  config->addNameValue( "StdDev", _stddev );
126 
127  return config;
128 }
129 
130 /*************************/
131 /*** set configuration ***/
133 
134  _mean = config->getAttributeAsSample( "Mean" );
135  _stddev = config->getAttributeAsSample( "StdDev" );
136 }
void setConfiguration(const ConstConfigurationPtr &)
void normalize(Sample *samplePtr)
void computeNormalization(const ReferenceCountedPointer< const SamplerImpl > &samplerPtr)
double Scalar
Type of map values.
Definition: om_defs.hh:39
ConfigurationPtr getConfiguration() const
void resize(std::size_t size)
Definition: Sample.cpp:153
std::size_t size() const
Definition: Sample.hh:70
Sample & sqrt()
Definition: Sample.cpp:445
std::vector< OccurrencePtr >::const_iterator const_iterator
Definition: Occurrences.hh:85
Definition: Sample.hh:25