Writing Your Own Parameter Calculation Class
You can incorporate your own parameter calculation methods into the Business Analysis Module by supplying your own parameter calculation class. Your class must be derived from the abstract base class RWRegressionCalc and must implement its five pure virtual functions:
virtual void
calc(const RWGenMat<T>& r, const RWMathVec<S>& o) = 0;
Calculates the model parameters from the input regression matrix r and observation vector v.
virtual RWMathVec<T>
parameters() = 0;
Returns the calculated parameters.
virtual bool
fail() const = 0;
Returns true if the most recent calculation failed. Otherwise, returns false.
virtual RWCString
name() const = 0;
Returns name identifying the calculation method.
virtual RWRegressionCalc<T,S>*
clone() = 0;
Returns a copy of self off the heap.
Here is an example of a calculation class for linear regression that uses the DoubleLeastSqCh class (replaced by class RWLeastSqCh<T>) found in the Linear Algebra Module of SourcePro Analysis:
 
class CholeskyLeastSquaresCalc : public RWRegressionCalc<double,double>
{
public:
static const char *methodName;
 
// Constructors-----------------------------------------------
CholeskyLeastSquaresCalc (){;}
CholeskyLeastSquaresCalc ( const CholeskyLeastSquaresCalc & c )
:parameters_(c.parameters_),fail_(c.fail_)
{
// Make sure I have my own copy of the parameters.
parameters_.deepenShallowCopy();
}
 
//------------------------------------------------------------
// Pure virtual functions inherited from RWRegressionCalc
// (see regcalc.h for function descriptions).
//------------------------------------------------------------
virtual void calc( const RWGenMat<double>& regressionMatrix,
const RWMathVec<double>& observations)
{
DoubleLeastSqCh lsqch( regressionMatrix );
fail_ = lsqch.fail();
if ( !lsqch.fail() )
{
parameters_.reference( lsqch.solve( observations ) );
}
}
 
virtual RWRegressionCalc<double,double>* clone() const {
return new CholeskyLeastSquaresCalc ( *this ); }
 
virtual RWMathVec<double> parameters()
{
if ( fail() ) // Clients should check fail status before
// they call parameters().
{
throw RWInternalErr("Parameters Failed.");
// Keeps some compilers happy.
return parameters_;
}
else
{
return parameters_;
}
}
 
virtual bool fail() const { return fail_; }
virtual RWCString name() const { return methodName; }
// Assignment operator.
RWLeastSqSVDCalc& operator=( const RWLeastSqSVDCalc& rhs )
{
parameters_.reference( rhs.parameters_ );
parameters_.deepenShallowCopy();
fail_ = rhs.fail_;
return *this;
}
 
private:
RWMathVec<double> parameters_;
bool fail_;
};