Commit fb3c4147 authored by Jeannie Backer's avatar Jeannie Backer
Browse files

Removed bugs, deleted dulicate code, and added documentation.

git-svn-id: http://subversion.wr.usgs.gov/repos/prog/isis3/branches/ipce@6063 41f8697f-d340-4b68-9986-7bafba869bb8
parent 79135929
Loading
Loading
Loading
Loading
+18 −33
Original line number Diff line number Diff line
@@ -10,7 +10,10 @@
#include "IString.h"

namespace Isis {
  
  /** 
   * Sets up a maximumlikelihood estimation function with Huber model and default tweaking
   * constant
   */  
  MaximumLikelihoodWFunctions::MaximumLikelihoodWFunctions() {
    this->setModel(Huber);
  } // choose Model and define the tweaking constant
@@ -45,17 +48,13 @@ namespace Isis {
  MaximumLikelihoodWFunctions::MaximumLikelihoodWFunctions(Model modelSelection, 
                                                           double tweakingConstant) {
    // choose Model and define the tweaking constant
    m_model = modelSelection;
    if (tweakingConstant <= 0.0) {
      IString msg = "Maximum likelihood estimation tweaking constants must be > 0.0";
      throw IException(IException::Programmer, msg, _FILEINFO_);
    }
    m_tweakingConstant = tweakingConstant;
    setModel(modelSelection, tweakingConstant);
  }

  MaximumLikelihoodWFunctions::MaximumLikelihoodWFunctions(
      const MaximumLikelihoodWFunctions &other)
      : m_model(other.m_model), m_tweakingConstant(other.m_tweakingConstant) {
      : m_model(other.m_model), 
        m_tweakingConstant(other.m_tweakingConstant) {
  }


@@ -77,11 +76,10 @@ namespace Isis {
   * @Param[in] enum Model modelSelection,  the model to be used
   *                                        (see documentation for enum Model)
   */
  bool MaximumLikelihoodWFunctions::setModel(Model modelSelection) {
  void MaximumLikelihoodWFunctions::setModel(Model modelSelection) {
    // choose Model and use default tweaking constant
    m_model = modelSelection;
    this->setTweakingConstantDefault();
    return true;
  }


@@ -89,21 +87,21 @@ namespace Isis {
  /** 
   * Sets default tweaking constants based on the maximum likelihood estimation model being used.
   */
  bool MaximumLikelihoodWFunctions::setTweakingConstantDefault() {
  void MaximumLikelihoodWFunctions::setTweakingConstantDefault() {
    // default tweaking constants for the various likelihood models
    switch (m_model) {
      case Huber:
        m_tweakingConstant = 1.345; // "95% asymptotice efficiecy on the standard normal distribution"
        m_tweakingConstant = 1.345; // "95% asymptotic efficiecy on the standard normal distribution"
                                    // is obtained with this constant, 
                                    // see Zhang's "Parameter Estimation"
        break;
      case HuberModified:
        m_tweakingConstant = 1.2107;// "95% asymptotice efficiecy on the standard normal distribution"
        m_tweakingConstant = 1.2107;// "95% asymptotic efficiecy on the standard normal distribution"
                                    // is obtained with this constant,
                                    // see Zhang's "Parameter Estimation"
        break;
      case Welsch:
        m_tweakingConstant = 2.9846;// "95% asymptotice efficiecy on the standard normal distribution"
        m_tweakingConstant = 2.9846;// "95% asymptotic efficiecy on the standard normal distribution"
                                    // is obtained with this constant,
                                    // see Zhang's "Parameter Estimation"
        break;
@@ -114,9 +112,7 @@ namespace Isis {
      default:
        m_tweakingConstant = 1;     // default, though at the time of writing this class,
                                    // this value should never actually be used
        break;
    }
    return true;
  }


@@ -126,22 +122,16 @@ namespace Isis {
 *
 * @param[in] enum Model modelSelection,  the model to be used
 *                                        (see documentation for enum Model)
 * @param[in] double tweaking constant,  exact meaning varies by model, but generally the
 * @param[in] tweakingConstant,  exact meaning varies by model, but generally the
 *                                       larger the value the more influence larger resiudals
 *                                       have on the solution. As well as possibly the more
 *                                       measures are included in the solution.
 * @throws IsisProgrammerError if tweakingConstant <= 0.0
 */
  bool MaximumLikelihoodWFunctions::setModel(Model modelSelection, double tweakingConstant) { 
  void MaximumLikelihoodWFunctions::setModel(Model modelSelection, double tweakingConstant) { 
    // choose Model and define the tweaking constant
    m_model = modelSelection;
    if (tweakingConstant <= 0.0) {
      IString msg = "Maximum likelihood estimation tweaking constants must be > 0.0";
      throw IException(IException::Programmer, msg, _FILEINFO_);
      return false;
    }
    m_tweakingConstant = tweakingConstant;
    return true;
    setTweakingConstant(tweakingConstant);
  }


@@ -149,24 +139,19 @@ namespace Isis {
  /** 
   * Allows the tweaking constant to be changed without changing the maximum likelihood function
   *
   * @param[in] double tweaking constant,  exact meaning varies by model, but generally the larger
   * the value the more influence larger resiudals have on the solution. As well as possiblely the
   * @param[in] tweakingConstant,  exact meaning varies by model, but generally the larger
   * the value the more influence larger resiudals have on the solution. As well as possiblly the
   * more measures are included in the solution.
   * 
   * @throws IsisProgrammerError if tweakingConstant <= 0.0
   */
  bool MaximumLikelihoodWFunctions::setTweakingConstant(double tweakingConstant) { 
  void MaximumLikelihoodWFunctions::setTweakingConstant(double tweakingConstant) { 
    // leave model type unaltered and change tweaking constant
    if (tweakingConstant <= 0.0) {
      return false;  // the constant must be positive
    }
    if (tweakingConstant <= 0.0) {
      IString msg = "Maximum likelihood estimation tweaking constants must be > 0.0";
      throw IException(IException::Programmer, msg, _FILEINFO_);
      return false;
    }
    m_tweakingConstant = tweakingConstant;
    return true;
  }


+8 −5
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ namespace Isis {
   *              Chen, "Robust Regression with Projection Based M-estimators"
   *
   * @ingroup ControlNetwork
   * @ingroup Math
   *
   * @author 2012-03-23 Orrin Thomas
   *
@@ -54,7 +55,9 @@ namespace Isis {
   *   @history 2014-07-03 Jeannie Backer - Replace member variable m_PI with Isis constant
   *   @history 2014-07-16 Jeannie Backer - Added enum to QString method and its inverse.
   *   @history 2014-07-23 Jeannie Backer - Added QDataStream >> and << operators and read/write
   *                           methods. Created unitTest.
   *                           methods.
   *   @history 2014-09-19 Jeannie Backer - Removed bugs. Added documentation. Cleaned
   *                           duplicate code.
   */
  class MaximumLikelihoodWFunctions {
  public:
@@ -113,11 +116,11 @@ namespace Isis {
    ~MaximumLikelihoodWFunctions();
    MaximumLikelihoodWFunctions &operator=(const MaximumLikelihoodWFunctions &other);

    bool setModel(Model modelSelection); // uses default tweaking constant
    bool setTweakingConstantDefault();
    void setModel(Model modelSelection); // uses default tweaking constant
    void setTweakingConstantDefault();

    bool setModel(Model modelSelection, double tweakingConstant);
    bool setTweakingConstant(double tweakingConstant);
    void setModel(Model modelSelection, double tweakingConstant);
    void setTweakingConstant(double tweakingConstant);

    Model model() const;
    double tweakingConstant() const;
+224 −27
Original line number Diff line number Diff line
Test Huber
Model set in Constructor with defualt TC: wFunc->sqrtWeightScaler(-0.5): 1.000000 wFunc->sqrtWeightScaler(0.75): 1.000000 wFunc->sqrtWeightScaler(-2): 0.820061 wFunc->sqrtWeightScaler(2.5): 0.733485
TC constant re-set to 2.0:              : wFunc->sqrtWeightScaler(-0.5): 1.000000 wFunc->sqrtWeightScaler(0.75): 1.000000 wFunc->sqrtWeightScaler(-2): 1.000000 wFunc->sqrtWeightScaler(2.5): 0.894427
TC constant re-est to default:          : wFunc->sqrtWeightScaler(-0.5): 1.000000 wFunc->sqrtWeightScaler(0.75): 1.000000 wFunc->sqrtWeightScaler(-2): 0.820061 wFunc->sqrtWeightScaler(2.5): 0.733485
Model Manually set with default TC:     : wFunc->sqrtWeightScaler(-0.5): 1.000000 wFunc->sqrtWeightScaler(0.75): 1.000000 wFunc->sqrtWeightScaler(-2): 0.820061 wFunc->sqrtWeightScaler(2.5): 0.733485
Model Manually set with TC =2.0:        : wFunc->sqrtWeightScaler(-0.5): 1.000000 wFunc->sqrtWeightScaler(0.75): 1.000000 wFunc->sqrtWeightScaler(-2): 1.000000 wFunc->sqrtWeightScaler(2.5): 0.894427

Test HuberModified
Model set in Constructor with defualt TC: wFunc->sqrtWeightScaler(-0.5): 0.985807 wFunc->sqrtWeightScaler(0.75): 0.968121 wFunc->sqrtWeightScaler(-2): 0.778042 wFunc->sqrtWeightScaler(2.5): 0.695902
TC constant re-set to 2.0:              : wFunc->sqrtWeightScaler(-0.5): 0.994794 wFunc->sqrtWeightScaler(0.75): 0.988295 wFunc->sqrtWeightScaler(-2): 0.917317 wFunc->sqrtWeightScaler(2.5): 0.871314
TC constant re-est to default:          : wFunc->sqrtWeightScaler(-0.5): 0.985807 wFunc->sqrtWeightScaler(0.75): 0.968121 wFunc->sqrtWeightScaler(-2): 0.778042 wFunc->sqrtWeightScaler(2.5): 0.695902
Model Manually set with default TC:     : wFunc->sqrtWeightScaler(-0.5): 0.985807 wFunc->sqrtWeightScaler(0.75): 0.968121 wFunc->sqrtWeightScaler(-2): 0.778042 wFunc->sqrtWeightScaler(2.5): 0.695902
Model Manually set with TC =2.0:        : wFunc->sqrtWeightScaler(-0.5): 0.994794 wFunc->sqrtWeightScaler(0.75): 0.988295 wFunc->sqrtWeightScaler(-2): 0.917317 wFunc->sqrtWeightScaler(2.5): 0.871314

Test Welsch
Model set in Constructor with defualt TC: wFunc->sqrtWeightScaler(-0.5): 0.986065 wFunc->sqrtWeightScaler(0.75): 0.968920 wFunc->sqrtWeightScaler(-2): 0.798898 wFunc->sqrtWeightScaler(2.5): 0.704114
TC constant re-set to 2.0:              : wFunc->sqrtWeightScaler(-0.5): 0.969233 wFunc->sqrtWeightScaler(0.75): 0.932102 wFunc->sqrtWeightScaler(-2): 0.606531 wFunc->sqrtWeightScaler(2.5): 0.457833
TC constant re-est to default:          : wFunc->sqrtWeightScaler(-0.5): 0.986065 wFunc->sqrtWeightScaler(0.75): 0.968920 wFunc->sqrtWeightScaler(-2): 0.798898 wFunc->sqrtWeightScaler(2.5): 0.704114
Model Manually set with default TC:     : wFunc->sqrtWeightScaler(-0.5): 0.986065 wFunc->sqrtWeightScaler(0.75): 0.968920 wFunc->sqrtWeightScaler(-2): 0.798898 wFunc->sqrtWeightScaler(2.5): 0.704114
Model Manually set with TC =2.0:        : wFunc->sqrtWeightScaler(-0.5): 0.969233 wFunc->sqrtWeightScaler(0.75): 0.932102 wFunc->sqrtWeightScaler(-2): 0.606531 wFunc->sqrtWeightScaler(2.5): 0.457833

Test Chen
Model set in Constructor with defualt TC: wFunc->sqrtWeightScaler(-0.5): 1.837117 wFunc->sqrtWeightScaler(0.75): 1.071652 wFunc->sqrtWeightScaler(-2): 0.000000 wFunc->sqrtWeightScaler(2.5): 0.000000
TC constant re-set to 2.0:              : wFunc->sqrtWeightScaler(-0.5): 9.185587 wFunc->sqrtWeightScaler(0.75): 8.420121 wFunc->sqrtWeightScaler(-2): 0.000000 wFunc->sqrtWeightScaler(2.5): 0.000000
TC constant re-est to default:          : wFunc->sqrtWeightScaler(-0.5): 1.837117 wFunc->sqrtWeightScaler(0.75): 1.071652 wFunc->sqrtWeightScaler(-2): 0.000000 wFunc->sqrtWeightScaler(2.5): 0.000000
Model Manually set with default TC:     : wFunc->sqrtWeightScaler(-0.5): 1.837117 wFunc->sqrtWeightScaler(0.75): 1.071652 wFunc->sqrtWeightScaler(-2): 0.000000 wFunc->sqrtWeightScaler(2.5): 0.000000
Model Manually set with TC =2.0:        : wFunc->sqrtWeightScaler(-0.5): 9.185587 wFunc->sqrtWeightScaler(0.75): 8.420121 wFunc->sqrtWeightScaler(-2): 0.000000 wFunc->sqrtWeightScaler(2.5): 0.000000
Test for MaximumLikelihoodWFunctions 

Default constructor sets model to Huber and corresponding default TC: 
Model                    =  "Huber" 
TweakingConstant         =  "1.345" 
WeightedResidualCutoff   =  "N/A" 
TweakingConstantQuantile =  "0.5" 
wFunc->sqrtWeightScaler(-0.5):  1 
wFunc->sqrtWeightScaler(0.75):  1 
wFunc->sqrtWeightScaler(-2):    0.820061 
wFunc->sqrtWeightScaler(2.5):   0.733485 

TC constant re-set to 2.0: 
Model                    =  "Huber" 
TweakingConstant         =  "2.0" 
WeightedResidualCutoff   =  "N/A" 
TweakingConstantQuantile =  "0.5" 
wFunc->sqrtWeightScaler(-0.5):  1 
wFunc->sqrtWeightScaler(0.75):  1 
wFunc->sqrtWeightScaler(-2):    1 
wFunc->sqrtWeightScaler(2.5):   0.894427 

TC constant re-set to default: 
Model                    =  "Huber" 
TweakingConstant         =  "1.345" 
WeightedResidualCutoff   =  "N/A" 
TweakingConstantQuantile =  "0.5" 
wFunc->sqrtWeightScaler(-0.5):  1 
wFunc->sqrtWeightScaler(0.75):  1 
wFunc->sqrtWeightScaler(-2):    0.820061 
wFunc->sqrtWeightScaler(2.5):   0.733485 

Model manually set with default TC: 
Model                    =  "Huber" 
TweakingConstant         =  "1.345" 
WeightedResidualCutoff   =  "N/A" 
TweakingConstantQuantile =  "0.5" 
wFunc->sqrtWeightScaler(-0.5):  1 
wFunc->sqrtWeightScaler(0.75):  1 
wFunc->sqrtWeightScaler(-2):    0.820061 
wFunc->sqrtWeightScaler(2.5):   0.733485 

Model manually set with TC = 2.0: 
Model                    =  "Huber" 
TweakingConstant         =  "2.0" 
WeightedResidualCutoff   =  "N/A" 
TweakingConstantQuantile =  "0.5" 
wFunc->sqrtWeightScaler(-0.5):  1 
wFunc->sqrtWeightScaler(0.75):  1 
wFunc->sqrtWeightScaler(-2):    1 
wFunc->sqrtWeightScaler(2.5):   0.894427 

Testing Copy constructor: 
Model                    =  "Huber" 
TweakingConstant         =  "2.0" 
WeightedResidualCutoff   =  "N/A" 
TweakingConstantQuantile =  "0.5" 
copyWFunc->sqrtWeightScaler(-0.5):  1 
copyWFunc->sqrtWeightScaler(0.75):  1 
copyWFunc->sqrtWeightScaler(-2):    1 
copyWFunc->sqrtWeightScaler(2.5):   0.894427 


Reassign object using operator= and passing HuberModified to constructor with default TC: 
Model                    =  "HuberModified" 
TweakingConstant         =  "1.2107" 
WeightedResidualCutoff   =  "N/A" 
TweakingConstantQuantile =  "0.4" 
wFunc->sqrtWeightScaler(-0.5):  0.985807 
wFunc->sqrtWeightScaler(0.75):  0.968121 
wFunc->sqrtWeightScaler(-2):    0.778042 
wFunc->sqrtWeightScaler(2.5):   0.695902 

TC constant re-set to 2.0: 
Model                    =  "HuberModified" 
TweakingConstant         =  "2.0" 
WeightedResidualCutoff   =  "N/A" 
TweakingConstantQuantile =  "0.4" 
wFunc->sqrtWeightScaler(-0.5):  0.994794 
wFunc->sqrtWeightScaler(0.75):  0.988295 
wFunc->sqrtWeightScaler(-2):    0.917317 
wFunc->sqrtWeightScaler(2.5):   0.871314 

TC constant re-set to default: 
Model                    =  "HuberModified" 
TweakingConstant         =  "1.2107" 
WeightedResidualCutoff   =  "N/A" 
TweakingConstantQuantile =  "0.4" 
wFunc->sqrtWeightScaler(-0.5):  0.985807 
wFunc->sqrtWeightScaler(0.75):  0.968121 
wFunc->sqrtWeightScaler(-2):    0.778042 
wFunc->sqrtWeightScaler(2.5):   0.695902 

Model manually set to Huber_Modified with default TC: 
Model                    =  "HuberModified" 
TweakingConstant         =  "1.2107" 
WeightedResidualCutoff   =  "N/A" 
TweakingConstantQuantile =  "0.4" 
wFunc->sqrtWeightScaler(-0.5):  0.985807 
wFunc->sqrtWeightScaler(0.75):  0.968121 
wFunc->sqrtWeightScaler(-2):    0.778042 
wFunc->sqrtWeightScaler(2.5):   0.695902 

Model manually set with TC = 2.0: 
Model                    =  "HuberModified" 
TweakingConstant         =  "2.0" 
WeightedResidualCutoff   =  "N/A" 
TweakingConstantQuantile =  "0.4" 
wFunc->sqrtWeightScaler(-0.5):  0.994794 
wFunc->sqrtWeightScaler(0.75):  0.988295 
wFunc->sqrtWeightScaler(-2):    0.917317 
wFunc->sqrtWeightScaler(2.5):   0.871314 


Reassign object using operator= and passing Welsch with TC = 2.0: 
Model                    =  "Welsch" 
TweakingConstant         =  "2.0" 
WeightedResidualCutoff   =  "3.0" 
TweakingConstantQuantile =  "0.7" 
wFunc->sqrtWeightScaler(-0.5):  0.969233 
wFunc->sqrtWeightScaler(0.75):  0.932102 
wFunc->sqrtWeightScaler(-2):    0.606531 
wFunc->sqrtWeightScaler(2.5):   0.457833 

TC constant set to default: 
Model                    =  "Welsch" 
TweakingConstant         =  "2.9846" 
WeightedResidualCutoff   =  "4.4769" 
TweakingConstantQuantile =  "0.7" 
wFunc->sqrtWeightScaler(-0.5):  0.986065 
wFunc->sqrtWeightScaler(0.75):  0.96892 
wFunc->sqrtWeightScaler(-2):    0.798898 
wFunc->sqrtWeightScaler(2.5):   0.704114 

Model manually set with default TC: 
Model                    =  "Welsch" 
TweakingConstant         =  "2.9846" 
WeightedResidualCutoff   =  "4.4769" 
TweakingConstantQuantile =  "0.7" 
wFunc->sqrtWeightScaler(-0.5):  0.986065 
wFunc->sqrtWeightScaler(0.75):  0.96892 
wFunc->sqrtWeightScaler(-2):    0.798898 
wFunc->sqrtWeightScaler(2.5):   0.704114 

Model manually set with TC = 2.0: 
Model                    =  "Welsch" 
TweakingConstant         =  "2.0" 
WeightedResidualCutoff   =  "3.0" 
TweakingConstantQuantile =  "0.7" 
wFunc->sqrtWeightScaler(-0.5):  0.969233 
wFunc->sqrtWeightScaler(0.75):  0.932102 
wFunc->sqrtWeightScaler(-2):    0.606531 
wFunc->sqrtWeightScaler(2.5):   0.457833 


Reassign object using operator= and passing Chen with default TC: 
Model                    =  "Chen" 
TweakingConstant         =  "1.0" 
WeightedResidualCutoff   =  "1.0" 
TweakingConstantQuantile =  "0.98" 
wFunc->sqrtWeightScaler(-0.5):  1.83712 
wFunc->sqrtWeightScaler(0.75):  1.07165 
wFunc->sqrtWeightScaler(-2):    0 
wFunc->sqrtWeightScaler(2.5):   0 

TC constant re-set to 2.0: 
Model                    =  "Chen" 
TweakingConstant         =  "2.0" 
WeightedResidualCutoff   =  "2.0" 
TweakingConstantQuantile =  "0.98" 
wFunc->sqrtWeightScaler(-0.5):  9.18559 
wFunc->sqrtWeightScaler(0.75):  8.42012 
wFunc->sqrtWeightScaler(-2):    0 
wFunc->sqrtWeightScaler(2.5):   0 

TC constant re-set to default: 
Model                    =  "Chen" 
TweakingConstant         =  "1.0" 
WeightedResidualCutoff   =  "1.0" 
TweakingConstantQuantile =  "0.98" 
wFunc->sqrtWeightScaler(-0.5):  1.83712 
wFunc->sqrtWeightScaler(0.75):  1.07165 
wFunc->sqrtWeightScaler(-2):    0 
wFunc->sqrtWeightScaler(2.5):   0 

Model manually set with default TC: 
Model                    =  "Chen" 
TweakingConstant         =  "1.0" 
WeightedResidualCutoff   =  "1.0" 
TweakingConstantQuantile =  "0.98" 
wFunc->sqrtWeightScaler(-0.5):  1.83712 
wFunc->sqrtWeightScaler(0.75):  1.07165 
wFunc->sqrtWeightScaler(-2):    0 
wFunc->sqrtWeightScaler(2.5):   0 

Model manually set with TC = 2.0: 
Model                    =  "Chen" 
TweakingConstant         =  "2.0" 
WeightedResidualCutoff   =  "2.0" 
TweakingConstantQuantile =  "0.98" 
wFunc->sqrtWeightScaler(-0.5):  9.18559 
wFunc->sqrtWeightScaler(0.75):  8.42012 
wFunc->sqrtWeightScaler(-2):    0 
wFunc->sqrtWeightScaler(2.5):   0 


Testing serialization... 
Previous class written and read from QByteArray: 
Model                    =  "Chen" 
TweakingConstant         =  "2.0" 
WeightedResidualCutoff   =  "2.0" 
TweakingConstantQuantile =  "0.98" 
wFunc->sqrtWeightScaler(-0.5):  9.18559 
wFunc->sqrtWeightScaler(0.75):  8.42012 
wFunc->sqrtWeightScaler(-2):    0 
wFunc->sqrtWeightScaler(2.5):   0 

Huber enum written to and read from QByteArray: 
Model                    =  "Huber" 


Testing error throws... 
**PROGRAMMER ERROR** Maximum likelihood estimation tweaking constants must be > 0.0.
**PROGRAMMER ERROR** Unknown estimation model enum [4].
**PROGRAMMER ERROR** Unknown maximum likelihood model name Nonsense.
+326 −149

File changed.

Preview size limit exceeded, changes collapsed.