All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
StatisticsFunctions.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/sa/core/StatisticsFunctions.cpp
22 
23  \brief Functions used in statistics operations.
24 
25  \reference Methods adapted from TerraLib4.
26 */
27 
28 // TerraLib
29 #include "../../common/StringUtils.h"
30 #include "../../datatype/SimpleData.h"
31 #include "../../graph/core/AbstractGraph.h"
32 #include "../../graph/core/Vertex.h"
33 #include "../../graph/iterator/MemoryIterator.h"
35 #include "StatisticsFunctions.h"
36 #include "Utils.h"
37 
38 // STL
39 #include <cassert>
40 
41 
43 {
44  assert(gpm);
45 
46  double sum = 0.;
47 
48  //get graph
49  te::graph::AbstractGraph* graph = gpm->getGraph();
50 
51  //create graph vertex iterator
52  std::auto_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(graph));
53 
54  te::graph::Vertex* v = it->getFirstVertex();
55 
56  while(!it->isVertexIteratorAfterEnd())
57  {
58  te::dt::AbstractData* ad = v->getAttributes()[attrIdx];
59 
60  if(ad)
61  {
62  sum += te::sa::GetDataValue(ad);
63  }
64 
65  v = it->getNextVertex();
66  }
67 
68  return sum;
69 }
70 
72 {
73  double sum = 0.;
74 
75  te::sa::KernelMap::iterator it = kMap.begin();
76 
77  while(it != kMap.end())
78  {
79  sum += it->second.second;
80 
81  ++it;
82  }
83 
84  return sum;
85 }
86 
88 {
89  assert(gpm);
90 
91  double mean = 0.;
92  int count = 0;
93 
94  //get graph
95  te::graph::AbstractGraph* graph = gpm->getGraph();
96 
97  //create graph vertex iterator
98  std::auto_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(graph));
99 
100  te::graph::Vertex* v = it->getFirstVertex();
101 
102  while(!it->isVertexIteratorAfterEnd())
103  {
104  te::dt::AbstractData* ad = v->getAttributes()[attrIdx];
105 
106  if(ad)
107  {
108  double attrValue = te::sa::GetDataValue(ad);
109 
110  mean += attrValue;
111 
112  ++count;
113  }
114 
115  v = it->getNextVertex();
116  }
117 
118  return mean /= count;
119 }
120 
121 double te::sa::FirstMoment(std::vector<double> vec)
122 {
123  double tot = 0.;
124 
125  for(std::size_t t = 0; t < vec.size(); ++t)
126  tot += vec[t];
127 
128  return tot / (double)vec.size();
129 }
130 
131 double te::sa::SecondMoment(te::sa::GeneralizedProximityMatrix* gpm, int attrIdx, double mean)
132 {
133  assert(gpm);
134 
135  double ssd = 0.; //sum of squares of desviation
136  int count = 0;
137 
138  //get graph
139  te::graph::AbstractGraph* graph = gpm->getGraph();
140 
141  //create graph vertex iterator
142  std::auto_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(graph));
143 
144  te::graph::Vertex* v = it->getFirstVertex();
145 
146  while(!it->isVertexIteratorAfterEnd())
147  {
148  te::dt::AbstractData* ad = v->getAttributes()[attrIdx];
149 
150  if(ad)
151  {
152  double attrValue = te::sa::GetDataValue(ad);
153 
154  ssd += pow((attrValue - mean), 2);
155 
156  ++count;
157  }
158 
159  v = it->getNextVertex();
160  }
161 
162  return ssd /= count;
163 }
Functions used in statistics operations.
This class defines a Generalized Proximity Matrix.
TESAEXPORT double FirstMoment(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx)
Function used to calculate mean (first moment) of a specific attribute from a gpm.
std::map< int, std::pair< te::gm::Geometry *, double > > KernelMap
std::vector< te::dt::AbstractData * > & getAttributes()
It returns the vector of attributes associated with this element.
Definition: Vertex.cpp:74
Utilitary function for spatial analysis module.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
TESAEXPORT double Sum(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx)
Function used to calculate sum of a specific attribute from a gpm.
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
TESAEXPORT double SecondMoment(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx, double mean)
Function used to calculate variance (second moment) of a specific attribute from a gpm...
TESAEXPORT double GetDataValue(te::dt::AbstractData *ad)
Function used to get the numeric value from a gpm property.
Definition: Utils.cpp:200
This class defines the GPM class.