Commit 5ce92b9e authored by Jeannie Backer's avatar Jeannie Backer
Browse files

Modified StatCumProbDistDynCalc. Added xml read/write serialization. Fixed bug in untested case.

git-svn-id: http://subversion.wr.usgs.gov/repos/prog/isis3/branches/ipce@6071 41f8697f-d340-4b68-9986-7bafba869bb8
parent 54fbaa87
Loading
Loading
Loading
Loading
+242 −209

File changed.

Preview size limit exceeded, changes collapsed.

+25 −24
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ namespace Isis {
  *
  *
  *
  * @ingroup Math
  * @ingroup Statistics
  *
  * @author 2012-03-23 Orrin Thomas
@@ -69,6 +70,10 @@ namespace Isis {
  *   @history 2014-07-19 Jeannie Backer - Added QDataStream >> and << operator methods. Brought
  *                           code closer to ISIS standards. Updated unitTest to include these
  *                           methods.
  *   @history 2014-09-11 Jeannie Backer - Added xml write/read capabilities. Fixed bug in cumPro()
  *                           method for previously untested lines (case where the given value is
  *                           closest to the last quantile value). Renamed member variables for
  *                           clarity.
  *
  */
  class StatCumProbDistDynCalc : public QObject {
@@ -79,25 +84,22 @@ namespace Isis {
    //  Observations"
    public:
      StatCumProbDistDynCalc(unsigned int nodes=20, QObject *parent = 0);  //individual qunatile value to be calculated
      StatCumProbDistDynCalc(Project *project, XmlStackedHandlerReader *xmlReader, QObject *parent = 0);   // TODO: does xml stuff need project???
      StatCumProbDistDynCalc(Project *project, XmlStackedHandlerReader *xmlReader, 
                             QObject *parent = 0);   // TODO: does xml stuff need project???
      StatCumProbDistDynCalc(const StatCumProbDistDynCalc &other);
      ~StatCumProbDistDynCalc();
      StatCumProbDistDynCalc &operator=(const StatCumProbDistDynCalc &other);
    
      void initialize(); // clears the member lists and initializes the rest of the member data to 0 
      void setQuantiles(unsigned int nodes); // initializes/resets the class to start new calculation

      void validate();
      void addObs(double obs);
      void set(int numberCells, 
               int numberQuantiles, 
               QList<double> quantiles, 
               QList<double> idealNum, 
               QList<int> n, 
               QVector<double> quantileValues, 
               int numberObservations);
    
      double cumProb(double value); //given a value return the cumulative probility
      double value(double cumProb); //given a cumulative probibility return a value
      double max(); //return the largest value so far
      double min(); //return the smallest values so far
      void initialize(unsigned int nodes=20); //resets the class to start a new dynamic calculation
    
      void save(QXmlStreamWriter &stream, const Project *project) const;   // TODO: does xml stuff need project???
    
@@ -130,8 +132,9 @@ namespace Isis {
          QString m_xmlHandlerCharacters;
      };

      QUuid *m_id; /**< A unique ID for this object (useful for others to reference
                        this object when saving to disk).*/
      QUuid *m_id; /**< A unique ID for this object (useful for others to reference this object when
                                      saving to disk).*/

      unsigned int m_numberCells; /**< The number of cells or histogram bins that are being used to
                                      model the probility density function.*/
      
@@ -139,25 +142,23 @@ namespace Isis {
                                      density function. This value is one more than the number of
                                      cells, (i.e. m_numberQuantiles=m_cells+1).*/
      
      unsigned int m_numberObservations; /**< The number of observations, note this is dynamically
                                      changing as observations are added.*/

      QList<double> m_quantiles; /**< The target quantiles being modeled, between 0 and 1.*/
      
      QList<double> m_idealNum; /**< The ideal number of observations that should be less than or
                                     equal to the value of the corresponding quantiles, note this is
      QList<double> m_observationValues; /**< The calculated values of the quantiles, note this is
                                      dynamically changing as observations are added.*/

      QList<int> m_n; /**< The actual number of observations that are less than or equal to the value
                           of the corresponding quantiles, note this is dynamically changing as
                           observations are added.*/
      QList<double> m_idealNumObsBelowQuantile; /**< The ideal number of observations that
                                      should be less than or equal to the value of the corresponding
                                      quantiles, note this is dynamically changing as observations
                                      are added.*/
      
      QVector<double> m_quantileValues; /**< The calculated values of the quantiles, note this is
                                             dynamically changing as observations are added.*/
      // TODO: figure out how to change this to QList... 
      //       Problem: resized in constructor, but not initialized.
      //       Values are set in addObs() using operator=, not  append()
      //       method. So size must already be set
      QList<int> m_numObsBelowQuantile; /**< The actual number of observations that are less
                                      than or equal to the value of the corresponding quantiles,
                                      note this is dynamically changing as observations are added.*/
            
      unsigned int m_numberObservations; /**< The number of observations, note this is dynamically
                                            changing as observations are added.*/
  };

  // operators to read/write StatCumProbDistDynCalc to/from binary data
+301 −17
Original line number Diff line number Diff line
Testing failure modes 
Querying minimum before the number of observations is greater than or equal to the number of quantiles: **PROGRAMMER ERROR** StatCumProbDistDynCalc will return no data until the number of observations added [20] matches the number of quantiles [51] (i.e. number of nodes) selected.
Querying minimum before the number of observations is greater than or equal to the number of quantiles: **PROGRAMMER ERROR** StatCumProbDistDynCalc will return no data until the number of observations added [20] matches the number of quantiles [51] (i.e. number of nodes) selected.
Querying a value (as a function of cumulative probability) before the number of observations is greater than or equal to the number of quantiles: **PROGRAMMER ERROR** StatCumProbDistDynCalc will return no data until the number of observations added [20] matches the number of quantiles [51] (i.e. number of nodes) selected.
Querying a cumulative probability (as a function of value) before the number of observations is greater than or equal to the number of quantiles: **PROGRAMMER ERROR** StatCumProbDistDynCalc will return no data until the number of observations added [20] matches the number of quantiles [51] (i.e. number of nodes) selected.
Querying a nonsense cumulative probability (2.0): **PROGRAMMER ERROR** Invalid cumulative probability [2.0] passed in to StatCumProbDistDynCalc::value(double cumProb). Must be on the domain [0, 1].
Querying a nonsense cumulative probability (-1.0): **PROGRAMMER ERROR** Invalid cumulative probability [-1.0] passed in to StatCumProbDistDynCalc::value(double cumProb). Must be on the domain [0, 1].
Querying minimum before the number of observations is greater than or equal to the number of quantiles:  
**PROGRAMMER ERROR** StatCumProbDistDynCalc will return no data until the number of observations added [20] matches the number of quantiles [51] (i.e. number of nodes) selected.
Querying maximum before the number of observations is greater than or equal to the number of quantiles:  
**PROGRAMMER ERROR** StatCumProbDistDynCalc will return no data until the number of observations added [20] matches the number of quantiles [51] (i.e. number of nodes) selected.
Querying a value (as a function of cumulative probability) before the number of observations is greater than or equal to the number of quantiles:  
**PROGRAMMER ERROR** StatCumProbDistDynCalc will return no data until the number of observations added [20] matches the number of quantiles [51] (i.e. number of nodes) selected.
Querying a cumulative probability (as a function of value) before the number of observations is greater than or equal to the number of quantiles:  
**PROGRAMMER ERROR** StatCumProbDistDynCalc will return no data until the number of observations added [20] matches the number of quantiles [51] (i.e. number of nodes) selected.
Querying a nonsense cumulative probability (2.0):  
**PROGRAMMER ERROR** Invalid cumulative probability [2.0] passed in to StatCumProbDistDynCalc::value(double cumProb). Must be on the domain [0, 1].
Querying a nonsense cumulative probability (-1.0):  
**PROGRAMMER ERROR** Invalid cumulative probability [-1.0] passed in to StatCumProbDistDynCalc::value(double cumProb). Must be on the domain [0, 1].
Testing XML: read XML with no attributes or values to StatCumProbDistDynCalc object... Then try to get min from object with no observations. 
**PROGRAMMER ERROR** StatCumProbDistDynCalc will return no data until the quantiles have been set. Number of cells = [0].

Testing successful construction of StatCumProbDistDynCalc object 
Min =  -1.27253 
Max =  9.07652 

Testing approximations
0.0 approximated Quantile:  -1.27253 
0.0 theoretical Quantile: -1.272533598... 
percent error:  0 % 

0.005 approximated Quantile:  -1.05122 

0.25 approximated Quantile:  2.84146 
0.25 theoretical Quantile: 2.6510204996078... 
percent error:  7.18374 % 
@@ -22,6 +36,18 @@ percent error: 2.49513%
0.75 theoretical Quantile: 5.34897950039216... 
percent error:  -0.612173 % 

0.995 approximated Quantile:  8.98943 

0.0 approximated Quantile:  9.07652 
0.0 theoretical Quantile: 9.07652065... 
percent error:  0 % 

approximated cumprobabilty [-oo, -2.0]:  0 
theoretical: 0.0... 
percent error:  0 % 

approximated cumprobabilty [-oo, -1.2]:  0.00163608 

approximated cumprobabilty [-oo, 0]:  0.0290357 
theoretical: 0.022750131948179... 
percent error:  27.6285 % 
@@ -34,21 +60,184 @@ approximate cumprobabilty [-oo, 5.0]: 0.691477
theoretical: 0.691462461274013... 
percent error:  0.00216297 % 

approximate cumprobabilty [-oo, 9.0]:  0.996511 

approximate cumprobabilty [-oo, 9.07652065]:  1 
theoretical: 1.0... 
percent error:  0 % 



reinitialize the class and redo the tests 
Min =  -1.27253 
Max =  9.07652 

0.0 approximated Quantile:  -1.27253 
0.0 theoretical Quantile: -1.272533598... 
percent error:  0 % 

0.005 approximated Quantile:  -1.05122 

0.25 approximated Quantile:  2.84146 
0.25 theoretical Quantile: 2.6510204996078... 
percent error:  7.18374 % 

0.50 approximated Quantile:  4.09981 
0.50 theoretical Quantile: 4.0 
percent error:  2.49513 % 

0.75 approximated Quantile:  5.31623 
0.75 theoretical Quantile: 5.34897950039216... 
percent error:  -0.612173 % 

0.995 approximated Quantile:  8.98943 

0.0 approximated Quantile:  9.07652 
0.0 theoretical Quantile: 9.07652065... 
percent error:  0 % 

approximated cumprobabilty [-oo, -2.0]:  0 
theoretical: 0.0... 
percent error:  0 % 

approximated cumprobabilty [-oo, -1.2]:  0.00163608 

approximated cumprobabilty [-oo, 0]:  0.0290357 
theoretical: 0.022750131948179... 
percent error:  27.6285 % 

approximated cumprobabilty [-oo, 2.0]:  0.144109 
theoretical: 0.158655253931457... 
percent error:  -9.16825 % 

approximate cumprobabilty [-oo, 5.0]:  0.691477 
theoretical: 0.691462461274013... 
percent error:  0.00216297 % 

approximate cumprobabilty [-oo, 9.0]:  0.996511 

approximate cumprobabilty [-oo, 9.07652065]:  1 
theoretical: 1.0... 
percent error:  0 % 



Testing copy constructor... 
Min =  -1.27253 
Max =  9.07652 

0.0 approximated Quantile:  -1.27253 
0.0 theoretical Quantile: -1.272533598... 
percent error:  0 % 

0.005 approximated Quantile:  -1.05122 

0.25 approximated Quantile:  2.84146 
0.25 theoretical Quantile: 2.6510204996078... 
percent error:  7.18374 % 

0.50 approximated Quantile:  4.09981 
0.50 theoretical Quantile: 4.0 
percent error:  2.49513 % 

0.75 approximated Quantile:  5.31623 
0.75 theoretical Quantile: 5.34897950039216... 
percent error:  -0.612173 % 

0.995 approximated Quantile:  8.98943 

0.0 approximated Quantile:  9.07652 
0.0 theoretical Quantile: 9.07652065... 
percent error:  0 % 

approximated cumprobabilty [-oo, -2.0]:  0 
theoretical: 0.0... 
percent error:  0 % 

approximated cumprobabilty [-oo, -1.2]:  0.00163608 

approximated cumprobabilty [-oo, 0]:  0.0290357 
theoretical: 0.022750131948179... 
percent error:  27.6285 % 

approximated cumprobabilty [-oo, 2.0]:  0.144109 
theoretical: 0.158655253931457... 
percent error:  -9.16825 % 

approximate cumprobabilty [-oo, 5.0]:  0.691477 
theoretical: 0.691462461274013... 
percent error:  0.00216297 % 

approximate cumprobabilty [-oo, 9.0]:  0.996511 

approximate cumprobabilty [-oo, 9.07652065]:  1 
theoretical: 1.0... 
percent error:  0 % 



Testing assignment operator=... 
Min =  -1.27253 
Max =  9.07652 

0.0 approximated Quantile:  -1.27253 
0.0 theoretical Quantile: -1.272533598... 
percent error:  0 % 

0.005 approximated Quantile:  -1.05122 

0.25 approximated Quantile:  2.84146 
0.25 theoretical Quantile: 2.6510204996078... 
percent error:  7.18374 % 

0.50 approximated Quantile:  4.09981 
0.50 theoretical Quantile: 4.0 
percent error:  2.49513 % 

0.75 approximated Quantile:  5.31623 
0.75 theoretical Quantile: 5.34897950039216... 
percent error:  -0.612173 % 

0.995 approximated Quantile:  8.98943 

0.0 approximated Quantile:  9.07652 
0.0 theoretical Quantile: 9.07652065... 
percent error:  0 % 

approximated cumprobabilty [-oo, -2.0]:  0 
theoretical: 0.0... 
percent error:  0 % 

approximated cumprobabilty [-oo, -1.2]:  0.00163608 

approximated cumprobabilty [-oo, 0]:  0.0290357 
theoretical: 0.022750131948179... 
percent error:  27.6285 % 

approximated cumprobabilty [-oo, 2.0]:  0.144109 
theoretical: 0.158655253931457... 
percent error:  -9.16825 % 

approximate cumprobabilty [-oo, 5.0]:  0.691477 
theoretical: 0.691462461274013... 
percent error:  0.00216297 % 

approximate cumprobabilty [-oo, 9.0]:  0.996511 

approximate cumprobabilty [-oo, 9.07652065]:  1 
theoretical: 1.0... 
percent error:  0 % 

reinitiliaze the class and redo the tests
Testing failure modes
Querying minimum before the number of observations is greater than or equal to the number of quantiles: **PROGRAMMER ERROR** StatCumProbDistDynCalc will return no data until the number of observations added [20] matches the number of quantiles [51] (i.e. number of nodes) selected.
Querying minimum before the number of observations is greater than or equal to the number of quantiles: **PROGRAMMER ERROR** StatCumProbDistDynCalc will return no data until the number of observations added [20] matches the number of quantiles [51] (i.e. number of nodes) selected.
Querying a value (as a function of cumulative probability) before the number of observations is greater than or equal to the number of quantiles: **PROGRAMMER ERROR** StatCumProbDistDynCalc will return no data until the number of observations added [20] matches the number of quantiles [51] (i.e. number of nodes) selected.
Querying a cumulative probability (as a function of value) before the number of observations is greater than or equal to the number of quantiles: **PROGRAMMER ERROR** StatCumProbDistDynCalc will return no data until the number of observations added [20] matches the number of quantiles [51] (i.e. number of nodes) selected.
Querying a nonsense cumulative probability (2.0): **PROGRAMMER ERROR** Invalid cumulative probability [2.0] passed in to StatCumProbDistDynCalc::value(double cumProb). Must be on the domain [0, 1].
Querying a nonsense cumulative probability (-1.0): **PROGRAMMER ERROR** Invalid cumulative probability [-1.0] passed in to StatCumProbDistDynCalc::value(double cumProb). Must be on the domain [0, 1].

Min =  -1.27253 
Max =  9.07652 

Testing approximations
0.0 approximated Quantile:  -1.27253 
0.0 theoretical Quantile: -1.272533598... 
percent error:  0 % 

0.005 approximated Quantile:  -1.05122 

0.25 approximated Quantile:  2.84146 
0.25 theoretical Quantile: 2.6510204996078... 
percent error:  7.18374 % 
@@ -61,6 +250,18 @@ percent error: 2.49513%
0.75 theoretical Quantile: 5.34897950039216... 
percent error:  -0.612173 % 

0.995 approximated Quantile:  8.98943 

0.0 approximated Quantile:  9.07652 
0.0 theoretical Quantile: 9.07652065... 
percent error:  0 % 

approximated cumprobabilty [-oo, -2.0]:  0 
theoretical: 0.0... 
percent error:  0 % 

approximated cumprobabilty [-oo, -1.2]:  0.00163608 

approximated cumprobabilty [-oo, 0]:  0.0290357 
theoretical: 0.022750131948179... 
percent error:  27.6285 % 
@@ -73,13 +274,24 @@ approximate cumprobabilty [-oo, 5.0]: 0.691477
theoretical: 0.691462461274013... 
percent error:  0.00216297 % 

approximate cumprobabilty [-oo, 9.0]:  0.996511 

approximate cumprobabilty [-oo, 9.07652065]:  1 
theoretical: 1.0... 
percent error:  0 % 



Testing serialization... 
Min =  -1.27253 
Max =  9.07652 

Testing approximations after serialization
0.0 approximated Quantile:  -1.27253 
0.0 theoretical Quantile: -1.272533598... 
percent error:  0 % 

0.005 approximated Quantile:  -1.05122 

0.25 approximated Quantile:  2.84146 
0.25 theoretical Quantile: 2.6510204996078... 
percent error:  7.18374 % 
@@ -92,6 +304,73 @@ percent error: 2.49513%
0.75 theoretical Quantile: 5.34897950039216... 
percent error:  -0.612173 % 

0.995 approximated Quantile:  8.98943 

0.0 approximated Quantile:  9.07652 
0.0 theoretical Quantile: 9.07652065... 
percent error:  0 % 

approximated cumprobabilty [-oo, -2.0]:  0 
theoretical: 0.0... 
percent error:  0 % 

approximated cumprobabilty [-oo, -1.2]:  0.00163608 

approximated cumprobabilty [-oo, 0]:  0.0290357 
theoretical: 0.022750131948179... 
percent error:  27.6285 % 

approximated cumprobabilty [-oo, 2.0]:  0.144109 
theoretical: 0.158655253931457... 
percent error:  -9.16825 % 

approximate cumprobabilty [-oo, 5.0]:  0.691477 
theoretical: 0.691462461274013... 
percent error:  0.00216297 % 

approximate cumprobabilty [-oo, 9.0]:  0.996511 

approximate cumprobabilty [-oo, 9.07652065]:  1 
theoretical: 1.0... 
percent error:  0 % 



Testing XML: write XML from StatCumProbDistDynCalc object... 
Testing XML: read XML to StatCumProbDistDynCalc object... 
Min =  -1.27253 
Max =  9.07652 

0.0 approximated Quantile:  -1.27253 
0.0 theoretical Quantile: -1.272533598... 
percent error:  0 % 

0.005 approximated Quantile:  -1.05122 

0.25 approximated Quantile:  2.84146 
0.25 theoretical Quantile: 2.6510204996078... 
percent error:  7.18374 % 

0.50 approximated Quantile:  4.09981 
0.50 theoretical Quantile: 4.0 
percent error:  2.49513 % 

0.75 approximated Quantile:  5.31623 
0.75 theoretical Quantile: 5.34897950039216... 
percent error:  -0.612173 % 

0.995 approximated Quantile:  8.98943 

0.0 approximated Quantile:  9.07652 
0.0 theoretical Quantile: 9.07652065... 
percent error:  0 % 

approximated cumprobabilty [-oo, -2.0]:  0 
theoretical: 0.0... 
percent error:  0 % 

approximated cumprobabilty [-oo, -1.2]:  0.00163608 

approximated cumprobabilty [-oo, 0]:  0.0290357 
theoretical: 0.022750131948179... 
percent error:  27.6285 % 
@@ -104,4 +383,9 @@ approximate cumprobabilty [-oo, 5.0]: 0.691477
theoretical: 0.691462461274013... 
percent error:  0.00216297 % 

approximate cumprobabilty [-oo, 9.0]:  0.996511 

approximate cumprobabilty [-oo, 9.07652065]:  1 
theoretical: 1.0... 
percent error:  0 % 
+568 −157

File changed.

Preview size limit exceeded, changes collapsed.

+10 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<statCumProbDistDynCalc>
    <id></id>
    <numberQuantiles></numberQuantiles>
    <numberCells></numberCells>
    <numberObservations></numberObservations>
    <observationData>
        <observation />
    </observationData>
</statCumProbDistDynCalc>