| TerraLib 4.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

E:/Projetos_Primeiro_Semestre_2012/TerraView/terralib/src/terralib/stat/TeBayesFunctions.h
Go to the documentation of this file.

    00001 /************************************************************************************
    00002 TerraLib - a library for developing GIS applications.
    00003 Copyright � 2001-2007 INPE and Tecgraf/PUC-Rio.
    00004
    00005 This code is part of the TerraLib library.
    00006 This library is free software; you can redistribute it and/or
    00007 modify it under the terms of the GNU Lesser General Public
    00008 License as published by the Free Software Foundation; either
    00009 version 2.1 of the License, or (at your option) any later version.
    00010
    00011 You should have received a copy of the GNU Lesser General Public
    00012 License along with this library.
    00013
    00014 The authors reassure the license terms regarding the warranties.
    00015 They specifically disclaim any warranties, including, but not limited to,
    00016 the implied warranties of merchantability and fitness for a particular purpose.
    00017 The library provided hereunder is on an "as is" basis, and the authors have no
    00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
    00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
    00020 indirect, special, incidental, or consequential damages arising out of the use
    00021 of this library and its documentation.
    00022 *************************************************************************************/
    00027 #ifndef __TEBAYESFUNCTIONSH__
    00028 #define __TEBAYESFUNCTIONSH__
    00029
    00030 #include "TeStatDataStructures.h"
    00031 #include "../kernel/TeSTElementSet.h"
    00032 #include "../kernel/TeNeighbours.h"
    00033
    00038
    00039 enum TeGlobalBayesError {
    00040    BAYES_GREATER_CASES,
    00041    BAYES_NULL_POPULATION
    00042 };
    00043
    00045 class STAT_DLL TeGlobalBayesException  {
    00046    TeGlobalBayesError error_;
    00047
    00048 public:
    00049
    00050   TeGlobalBayesException(TeGlobalBayesError e) {
    00051     error_ = e;
    00052   }
    00053
    00054   TeGlobalBayesError getErrorCode() {
    00055     return error_;
    00056   }
    00057 };
    00058
    00059
    00061 STAT_DLL bool TeGlobalEmpiricalBayes(TeSTStatInstanceSet *rs, double rate);
    00062
    00063 /* Taxa Bayesiana Local -- recebe conjunto de areas, cria matriz de vizinhanca,
    00064    e estima taxa local baseado na vizinhanca.
    00065
    00066    As duas primeiras propriedades das areas sao populacao e casos.
    00067    Ha mais duas propriedades double onde serao armazenados total de casos e total
    00068    de populacao dos vizinhos.
    00069
    00070   teta[i]=w[i]*r[i]+(1-w[i])*c[i]
    00071   w[i]=V/(V+r[i]))
    00072   V=Sum(Area i,Pop[i]*(ri-m)^2)/Sum(Area i,Pop[i])-m/PopMedia
    00073 */
    00075 template<typename Matrix> bool
    00076 TeLocalEmpiricalBayes(TeSTElementSet *rs, Matrix* neighMatrix, string& colname, double rate);
    00079 //Implementation
    00080 template<typename Matrix> bool
    00081 TeLocalEmpiricalBayes(TeSTElementSet *rs, Matrix* neighMatrix, string& colname, double rate)
    00082 {
    00083            //Percorre areas, calculando populacao e contagem total dos vizinhos
    00084            typename TeSTElementSet::iterator it = rs->begin();
    00085            double     neiPop,  //Somatorio da populacao de uma area e seus vizinhos
    00086                       neiCases; //Somatorio de casos de uma area e seus vizinhos
    00087            double     myPop,    //Populacao de uma area
    00088                       myCases;  //Casos de uma area
    00089
    00090            std::string val;
    00091
    00092            //Adds a new property in the element set
    00093            TeAttribute r;
    00094            r.rep_.name_ = "neiPop";
    00095            r.rep_.type_ = TeREAL;
    00096            r.rep_.decimals_ = 15;
    00097            rs->addProperty(r);
    00098            r.rep_.name_ = "neiCases";
    00099            rs->addProperty(r);
    00100
    00102            int indexAttrPop = rs->getAttributeIndex("neiPop");
    00103            int indexAttrCases = rs->getAttributeIndex("neiCases");
    00104
    00105            //Calcula numero de casos e populacao de uma area e seus vizinhos para todas as
    00106            //areas --> propriedades neiPop e neiCases
    00107            while ( it != rs->end())
    00108            {
    00109
    00110                       //Inicializa total de casos e de populacao
    00111                       (*it).getPropertyValue(val, 0);
    00112                       myPop = atof(val.c_str());
    00113                       neiPop = myPop;
    00114                       (*it).getPropertyValue(val, 1);
    00115                       neiCases = atof(val.c_str());
    00116
    00117                       TeNeighboursMap  neighbors = neighMatrix->getMapNeighbours((*it).objectId());
    00118                       if ((myPop > 0)  && (neighbors.size() > 0))
    00119                       {
    00120                                  typename TeNeighboursMap::iterator itNeigs = neighbors.begin();
    00121                                  //Total de casos e de populacao
    00122                                  while(itNeigs != neighbors.end())
    00123                                  {
    00124                                  //Pega numero de casos e de populacao
    00125                                  rs->getAttributeValue((*itNeigs).first, 0, val);
    00126                                  neiPop += atof(val.c_str());
    00127                                  rs->getAttributeValue((*itNeigs).first, 1, val);
    00128                                  neiCases += atof(val.c_str());
    00129                                  (++itNeigs);
    00130                                  }
    00131            }
    00132
    00133            (*it).addPropertyValue(Te2String(neiPop, 9));
    00134                       (*it).addPropertyValue(Te2String(neiCases, 9));
    00135            (++it);
    00136            }
    00137
    00138            double     m, //taxa de uma area e seus vizinhos
    00139                       variance, //variancia de uma area e seus vizinhos
    00140                       V, W, LocalBayesRate;
    00141            double     neiJPop, //Populacao de uma area vizinha
    00142                       neiJCases; //Casos de uma area vizinha
    00143
    00144            // adds rate property
    00145            r.rep_.name_ = colname;
    00146            r.rep_.type_ = TeREAL;
    00147            r.rep_.decimals_ = 15;
    00148            rs->addProperty(r);
    00149            it = rs->begin();
    00150            //Calcula taxa corrigida local para cada area
    00151             while ( it != rs->end())
    00152            {
    00153                       //Pega numero de casos e de populacao
    00154                       (*it).getPropertyValue(val, 0);
    00155                       myPop = atof(val.c_str());
    00156                       (*it).getPropertyValue(val, 1);
    00157                       myCases = atof(val.c_str());
    00158
    00159                       (*it).getPropertyValue(val, indexAttrPop);
    00160                       neiPop = atof(val.c_str());
    00161                       (*it).getPropertyValue(val, indexAttrCases);
    00162                       neiCases = atof(val.c_str());
    00163
    00164            if (neiPop <= 0)
    00165                                  return false;
    00166                       m = (double)neiCases/(double)neiPop;
    00167
    00168            TeNeighboursMap  neighbors = neighMatrix->getMapNeighbours((*it).objectId());
    00169                    if (neighbors.size() == 0)
    00170                       {
    00171                                  LocalBayesRate = (myPop > 0) ? (double)myCases/(double)myPop : 0.0;
    00172            }
    00173            else
    00174                       {
    00175                                  //Calculo da variancia
    00176                                  typename TeNeighboursMap::iterator itNeigs = neighbors.begin();
    00177                                  variance = (double)myPop * pow((double)myCases/(double)myPop - m,2);
    00178                                  //Total de casos e de populacao
    00179                                  while(itNeigs != neighbors.end())
    00180                                  {
    00181                                             //Pega numero de casos e de populacao
    00182                                             rs->getAttributeValue((*itNeigs).first, 0, val);
    00183                                             neiJPop = atof(val.c_str());
    00184                                             rs->getAttributeValue((*itNeigs).first, 1, val);
    00185                                             neiJCases = atof(val.c_str());
    00186
    00187                                             variance += (double)neiJPop * pow((double)neiJCases/(double)neiJPop - m,2);
    00188                                             (++itNeigs);
    00189                                  }
    00190                       variance /= (double) neiPop;
    00191                                  //Calculo da taxa
    00192                                  V = variance - (m * ((double)neighbors.size()+1)/(double)neiPop);
    00193                                  if (V < 0)
    00194                                  V = 0;
    00195                                  if ((V == 0) && (m == 0))
    00196                                  W  = 1;
    00197                                  else
    00198                                  W = V/(V + (m/(double)myPop));
    00199                                  LocalBayesRate = W * (double)myCases/(double)myPop + (1 - W) * m;
    00200
    00201                       }
    00202                       LocalBayesRate *= rate;
    00203                       (*it).addPropertyValue(Te2String(LocalBayesRate, 9));
    00204                       (++it);
    00205            }
    00206            return true;
    00207 }
    00208 #endif

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines