PLearn 0.1
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Static Protected Member Functions | Protected Attributes | Private Types | Private Member Functions
PLearn::CubicSpline Class Reference

Unidimensional cubic spline learner. More...

#include <CubicSpline.h>

Inheritance diagram for PLearn::CubicSpline:
Inheritance graph
[legend]
Collaboration diagram for PLearn::CubicSpline:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 CubicSpline ()
 Default constructor.
virtual int outputsize () const
 Returns the size of this learner's output: 1 -> a single interpolated value.
virtual void forget ()
 (Re-)initializes the PLearner in its fresh state (that state may depend on the 'seed' option) and sets 'stage' back to 0 (this is the stage of a fresh learner!).
virtual void train ()
 Fit the splines to the *last* input point.
virtual void computeOutput (const Vec &input, Vec &output) const
 Computes the output from the input.
virtual void computeCostsFromOutputs (const Vec &input, const Vec &output, const Vec &target, Vec &costs) const
 Computes the costs from already computed output.
virtual TVec< std::string > getTestCostNames () const
 Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method).
virtual TVec< std::string > getTrainCostNames () const
 Returns the names of the objective costs that the train method computes and for which it updates the VecStatsCollector train_stats.
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual CubicSplinedeepCopy (CopiesMap &copies) const
virtual void build ()
 Finish building the object; just call inherited::build followed by build_()
virtual void makeDeepCopyFromShallowCopy (CopiesMap &copies)
 Transforms a shallow copy into a deep copy.

Static Public Member Functions

static string _classname_ ()
static OptionList_getOptionList_ ()
static RemoteMethodMap_getRemoteMethodMap_ ()
static Object_new_instance_for_typemap_ ()
static bool _isa_ (const Object *o)
static void _static_initialize_ ()
static const PPathdeclaringFile ()

Public Attributes

real m_low_slope
 The slope to enforce at the leftmost node -- Default: NaN [None].
real m_high_slope
 The slope to enforce at the rightmost node -- Default: NaN [None].

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

static void declareOptions (OptionList &ol)
 Declares the class options.

Protected Attributes

Vec m_inputs
 A buffer containing the last training set inputs.
Vec m_targets
 A buffer containing the last training set targets.
Vec m_coefficients
 The learnt coefficients.

Private Types

typedef PLearner inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

Unidimensional cubic spline learner.

This learner fits a unidimensional cubic spline to a given set of points. That is, inputsize() must be one and the inputs are considered to be the x values, while targetsize() must also be one and the targets are considered to be the y values. The spline is fitted to the (x,y)-pairs so formed. X values don't need to be ordered; the ordering is ensured within the train method.

Definition at line 60 of file CubicSpline.h.


Member Typedef Documentation

Reimplemented from PLearn::PLearner.

Definition at line 62 of file CubicSpline.h.


Constructor & Destructor Documentation

PLearn::CubicSpline::CubicSpline ( )

Default constructor.

Definition at line 56 of file CubicSpline.cc.

    : m_low_slope(MISSING_VALUE),
      m_high_slope(MISSING_VALUE)
{
    // Nothing to do here
} 

Member Function Documentation

string PLearn::CubicSpline::_classname_ ( ) [static]

Reimplemented from PLearn::PLearner.

Definition at line 54 of file CubicSpline.cc.

OptionList & PLearn::CubicSpline::_getOptionList_ ( ) [static]

Reimplemented from PLearn::PLearner.

Definition at line 54 of file CubicSpline.cc.

RemoteMethodMap & PLearn::CubicSpline::_getRemoteMethodMap_ ( ) [static]

Reimplemented from PLearn::PLearner.

Definition at line 54 of file CubicSpline.cc.

bool PLearn::CubicSpline::_isa_ ( const Object o) [static]

Reimplemented from PLearn::PLearner.

Definition at line 54 of file CubicSpline.cc.

Object * PLearn::CubicSpline::_new_instance_for_typemap_ ( ) [static]

Reimplemented from PLearn::Object.

Definition at line 54 of file CubicSpline.cc.

StaticInitializer CubicSpline::_static_initializer_ & PLearn::CubicSpline::_static_initialize_ ( ) [static]

Reimplemented from PLearn::PLearner.

Definition at line 54 of file CubicSpline.cc.

void PLearn::CubicSpline::build ( ) [virtual]

Finish building the object; just call inherited::build followed by build_()

Reimplemented from PLearn::PLearner.

Definition at line 96 of file CubicSpline.cc.

References PLearn::PLearner::build(), and build_().

Here is the call graph for this function:

void PLearn::CubicSpline::build_ ( ) [private]

This does the actual building.

Reimplemented from PLearn::PLearner.

Definition at line 90 of file CubicSpline.cc.

Referenced by build().

{
    // Nothing to do here    
}

Here is the caller graph for this function:

string PLearn::CubicSpline::classname ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 54 of file CubicSpline.cc.

void PLearn::CubicSpline::computeCostsFromOutputs ( const Vec input,
const Vec output,
const Vec target,
Vec costs 
) const [virtual]

Computes the costs from already computed output.

Implements PLearn::PLearner.

Definition at line 202 of file CubicSpline.cc.

{
    // No costs...
}
void PLearn::CubicSpline::computeOutput ( const Vec input,
Vec output 
) const [virtual]

Computes the output from the input.

Reimplemented from PLearn::PLearner.

Definition at line 183 of file CubicSpline.cc.

References a, b, PLearn::TVec< T >::findSorted(), PLearn::TVec< T >::length(), m_coefficients, m_inputs, m_targets, PLearn::max(), PLearn::min(), n, PLASSERT, PLearn::TVec< T >::resize(), and x.

{
    PLASSERT( input.length() == 1 );
    output.resize(1);
 
    int n = m_inputs.length();
    real x = input[0];
    int pos = min( max(1, m_inputs.findSorted(x)),  n-1 );

    real h = m_inputs[pos] - m_inputs[pos-1];
    PLASSERT( h > 0.0 );

    real a = (m_inputs[pos] - x) / h;
    real b = (x - m_inputs[pos-1]) / h;
    output[0] = a*m_targets[pos-1] + b*m_targets[pos]
        + ((a*a*a - a)*m_coefficients[pos-1]
           + (b*b*b - b)*m_coefficients[pos]) * h*h/6.0;
}

Here is the call graph for this function:

void PLearn::CubicSpline::declareOptions ( OptionList ol) [static, protected]

Declares the class options.

Reimplemented from PLearn::PLearner.

Definition at line 63 of file CubicSpline.cc.

References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::PLearner::declareOptions(), PLearn::OptionBase::learntoption, m_coefficients, m_high_slope, m_inputs, m_low_slope, and m_targets.

{
    declareOption(ol, "low_slope", &CubicSpline::m_low_slope,
                  OptionBase::buildoption,
                  "The slope to enforce at the leftmost node -- Default: NaN [None]");
    
    declareOption(ol, "high_slope", &CubicSpline::m_high_slope,
                  OptionBase::buildoption,
                  "The slope to enforce at the rightmost node -- Default: NaN [None]");
    
    // Learnt options
    declareOption(ol, "inputs", &CubicSpline::m_inputs,
                  OptionBase::learntoption,
                  "A buffer containing the last training set inputs");

    declareOption(ol, "targets", &CubicSpline::m_targets,
                  OptionBase::learntoption,
                  "A buffer containing the last training set targets");

    declareOption(ol, "coefficients", &CubicSpline::m_coefficients,
                  OptionBase::learntoption,
                  "The learnt coefficients");
    
    // Now call the parent class' declareOptions
    inherited::declareOptions(ol);
}

Here is the call graph for this function:

static const PPath& PLearn::CubicSpline::declaringFile ( ) [inline, static]

Reimplemented from PLearn::PLearner.

Definition at line 113 of file CubicSpline.h.

:
    //#####  Protected Options  ###############################################
CubicSpline * PLearn::CubicSpline::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::PLearner.

Definition at line 54 of file CubicSpline.cc.

void PLearn::CubicSpline::forget ( ) [virtual]

(Re-)initializes the PLearner in its fresh state (that state may depend on the 'seed' option) and sets 'stage' back to 0 (this is the stage of a fresh learner!).

Reimplemented from PLearn::PLearner.

Definition at line 118 of file CubicSpline.cc.

References PLearn::PLearner::forget(), m_coefficients, and PLearn::TVec< T >::resize().

{
    m_coefficients->resize(0);
    inherited::forget();
}

Here is the call graph for this function:

OptionList & PLearn::CubicSpline::getOptionList ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 54 of file CubicSpline.cc.

OptionMap & PLearn::CubicSpline::getOptionMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 54 of file CubicSpline.cc.

RemoteMethodMap & PLearn::CubicSpline::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 54 of file CubicSpline.cc.

TVec< string > PLearn::CubicSpline::getTestCostNames ( ) const [virtual]

Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method).

[Empty]

Implements PLearn::PLearner.

Definition at line 208 of file CubicSpline.cc.

{
    // None for now
    return TVec<string>();
}
TVec< string > PLearn::CubicSpline::getTrainCostNames ( ) const [virtual]

Returns the names of the objective costs that the train method computes and for which it updates the VecStatsCollector train_stats.

[Empty]

Implements PLearn::PLearner.

Definition at line 214 of file CubicSpline.cc.

{
    // None for now
    return TVec<string>();
}
void PLearn::CubicSpline::makeDeepCopyFromShallowCopy ( CopiesMap copies) [virtual]

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::PLearner.

Definition at line 103 of file CubicSpline.cc.

References PLearn::deepCopyField(), m_coefficients, m_inputs, m_targets, and PLearn::PLearner::makeDeepCopyFromShallowCopy().

Here is the call graph for this function:

int PLearn::CubicSpline::outputsize ( ) const [virtual]

Returns the size of this learner's output: 1 -> a single interpolated value.

Implements PLearn::PLearner.

Definition at line 113 of file CubicSpline.cc.

References PLearn::PLearner::inputsize().

{
    return inputsize();
}

Here is the call graph for this function:

void PLearn::CubicSpline::train ( ) [virtual]

Fit the splines to the *last* input point.

Implements PLearn::PLearner.

Definition at line 124 of file CubicSpline.cc.

References PLearn::TVec< T >::fill(), PLearn::VMat::getColumn(), i, PLearn::PLearner::inputsize(), PLearn::is_missing(), PLearn::VMat::length(), m_coefficients, m_high_slope, m_inputs, m_low_slope, m_targets, n, PLASSERT, PLearn::TVec< T >::resize(), PLearn::TVec< T >::sortingPermutation(), PLearn::PLearner::targetsize(), PLearn::PLearner::train_set, u, and PLearn::VMat::width().

{
    // This learner fits unidimensional inputs/targets
    PLASSERT( inputsize() == 1 );
    PLASSERT( targetsize() == 1 );
    
    // Train set is a member of PLearner; set through setTrainingSet()
    int n = train_set->length();
    m_inputs = train_set.getColumn(0);
    m_targets = train_set.getColumn(1);
    PLASSERT( n >= 2 && train_set->width() == 2 );

    // Sort the inputs and targets along the inputs values
    TVec<int> indices = m_inputs.sortingPermutation();
    m_inputs          = m_inputs(indices);
    m_targets         = m_targets(indices);
    
    Vec u(n-1, 0.0);
    m_coefficients.resize(n);
    m_coefficients.fill(0.0);

    // Low slope hack
    if ( !is_missing(m_low_slope) ) {
        u[0] = (3.0/(m_inputs[1]-m_inputs[0])) *
            ( (m_targets[1]-m_targets[0])/
              (m_inputs[1] - m_inputs[0]) - m_low_slope );
        m_coefficients[0] = -0.5;
    }

    // Forward pass on coefficients
    for (int i=1; i < (n-1); i++) {
        real sig = (m_inputs[i]-m_inputs[i-1]) / (m_inputs[i+1]-m_inputs[i-1]);        
        real p   = sig*m_coefficients[i-1]+2.0;
        
        m_coefficients[i] = (sig-1.0)/p;
        
        u[i] = (m_targets[i+1]-m_targets[i]) / (m_inputs[i+1]-m_inputs[i])
            - (m_targets[i]-m_targets[i-1])  / (m_inputs[i]-m_inputs[i-1]);
        
        u[i] = (6.0*u[i]/(m_inputs[i+1]-m_inputs[i-1]) - sig*u[i-1]) / p;
    }

    // High slope hack
    real un=0, qn=0;
    if ( !is_missing(m_high_slope) ) {
        qn = 0.5;
        un = (3.0/(m_inputs[n-1]-m_inputs[n-2])) *
            (m_high_slope  -  (m_targets[n-1]-m_targets[n-2]) / (m_inputs[n-1]-m_inputs[n-2]));
    }

    // Compute the last coefficient
    m_coefficients[n-1] = (un-qn*u[n-2])/(qn*m_coefficients[n-1]+1.0);

    // Backsubstitution step
    for (int k=n-2; k >= 0; k--)
        m_coefficients[k] = m_coefficients[k]*m_coefficients[k+1]+u[k];
}

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::PLearner.

Definition at line 113 of file CubicSpline.h.

The learnt coefficients.

Definition at line 131 of file CubicSpline.h.

Referenced by computeOutput(), declareOptions(), forget(), makeDeepCopyFromShallowCopy(), and train().

The slope to enforce at the rightmost node -- Default: NaN [None].

Definition at line 71 of file CubicSpline.h.

Referenced by declareOptions(), and train().

A buffer containing the last training set inputs.

Definition at line 125 of file CubicSpline.h.

Referenced by computeOutput(), declareOptions(), makeDeepCopyFromShallowCopy(), and train().

The slope to enforce at the leftmost node -- Default: NaN [None].

Definition at line 68 of file CubicSpline.h.

Referenced by declareOptions(), and train().

A buffer containing the last training set targets.

Definition at line 128 of file CubicSpline.h.

Referenced by computeOutput(), declareOptions(), makeDeepCopyFromShallowCopy(), and train().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines