STK++ 1.0
STK::LocalVariance Class Reference

A LocalVariance is an implementation of the abstract ILinearReduct class. More...

#include <STK_LocalVariance.h>

Inheritance diagram for STK::LocalVariance:
Collaboration diagram for STK::LocalVariance:

List of all members.

Public Types

enum  TypeGraph { prim_, minimalDistance_, unknown_ }
 Type of proximity graph to used in order to compute the local variance: More...

Public Member Functions

 LocalVariance (Matrix const &data, TypeGraph const &type=minimalDistance_, Integer const &nbNeighbor=1)
 Constructor.
virtual ~LocalVariance ()
 Destructor.
Integer const & nbNeighbor () const
 get the number of neighbors used n the local covariance.
Array2D< Integer > const & pred () const
 get the predeccesor array
MatrixSquare const & covariance () const
 get the covariance
MatrixSquare const & localCovariance () const
 get the local covariance

Static Public Member Functions

static TypeGraph StringToTypeGraph (String const &type)
 convert a String to a TypeGraph.
static String TypeGraphToString (TypeGraph const &type)
 convert a TypeGraph to a String.

Protected Member Functions

virtual void update ()
 Compute the proximity graph if the data set is modified.
virtual void maximizeIndex ()
 Compute the axis by maximizing the ratio of the local variance on the total variance of the data set.
virtual void maximizeIndex (Vector const &weights)
 Compute the axis by maximizing the ratio of the weighted local variance on the weighted total variance of the data set.
void prim ()
 compute the minimal spanning tree
void minimalDistance ()
 compute the minimal distance graph
void computeCovarianceMatrices ()
 compute the covariances matrices of the data set
void computeCovarianceMatrices (Vector const &weights)
 compute the weighted covariances matrices of the data set

Protected Attributes

TypeGraph type_
 number of neighbors
Integer nbNeighbor_
 number of neighbors
Array2D< Integerneighbors_
 Predecessor array : store the spanning tree or the minimal distance to the neighbors.
Matrix dist_
 distance matrix : store the minimal distance to the neighbors
MatrixSquarep_localCov_
 the local covariance Matrix
MatrixSquare covariance_
 the covariance Matrix
Stat::MultivariateMatrixp_dataStatistics_
 the multivariate Statistics.

Private Member Functions

void computeAxis ()
 compute the axis using the first eigenvectors of the matrix of covariance and local covariance
void initializeMemory ()
 initialize memory
void clear ()
 clear allocated memory

Detailed Description

A LocalVariance is an implementation of the abstract ILinearReduct class.

A LocalVariance is an Index which maximize the projected local variance of the data set. The class can use the algorithm of Prim or the minimal distance in order to compute the proximity graph defining the local variance.

Definition at line 57 of file STK_LocalVariance.h.


Member Enumeration Documentation

Type of proximity graph to used in order to compute the local variance:

  • prim_ the minimal spanning tree
  • minimalDistance_ the first neighbors
  • unknown_ unknown type of graph
Enumerator:
prim_ 
minimalDistance_ 
unknown_ 

Definition at line 66 of file STK_LocalVariance.h.


Constructor & Destructor Documentation

STK::LocalVariance::LocalVariance ( Matrix const &  data,
TypeGraph const &  type = minimalDistance_,
Integer const &  nbNeighbor = 1 
)

Constructor.

the TypeGraph and the number of neighbors are given by the user and are not modified.

Parameters:
datathe data set to process
typetype of proximity graph to build
nbNeighbornumber of neighbors to use in the proximity graph

Definition at line 73 of file STK_LocalVariance.cpp.

References _T, minimalDistance(), minimalDistance_, prim(), prim_, type_, and unknown_.

                            : ILinearReduct(data)
                            , type_(type)
                            , nbNeighbor_(nbNeighbor)
                            , neighbors_(data.rangeVe(), Range(nbNeighbor_))
                            , dist_(data.rangeVe(), Range(nbNeighbor_), Arithmetic<Real>::max())
                            , p_localCov_(0)
                            , p_dataStatistics_(0)
{
  // compute minimal proximity graph of the data set
  switch (type_)
  {
    case prim_:
      prim();
      break;
    case minimalDistance_:
      minimalDistance();
      break;
    case unknown_:
      throw runtime_error(_T("Error in LocalVariance::LocalVariance(data, type, nbNeighbor)\nWhat: "
                             "unknown proximity graph."));
      break;
  };
}

Here is the call graph for this function:

STK::LocalVariance::~LocalVariance ( ) [virtual]

Destructor.

Definition at line 101 of file STK_LocalVariance.cpp.

References clear().

{ clear();}

Here is the call graph for this function:


Member Function Documentation

LocalVariance::TypeGraph STK::LocalVariance::StringToTypeGraph ( String const &  type) [static]

convert a String to a TypeGraph.

Parameters:
typethe type of graph in a string
Returns:
the TypeGraph represented by the String type. If the string does not match any known name, the unknown_ type is returned.

Definition at line 51 of file STK_LocalVariance.cpp.

References _T, minimalDistance_, prim_, STK::toUpperString(), and unknown_.

Referenced by STK::LocalVariancePage::validate().

{
  if (toUpperString(type) == toUpperString(_T("prim")))  return prim_;
  if (toUpperString(type) == toUpperString(_T("minimalDistance"))) return minimalDistance_;
  return unknown_;
}

Here is the call graph for this function:

String STK::LocalVariance::TypeGraphToString ( TypeGraph const &  type) [static]

convert a TypeGraph to a String.

Parameters:
typethe type of graph we want to convert to a string
Returns:
the string associated to this type of graph

Definition at line 62 of file STK_LocalVariance.cpp.

References _T, minimalDistance_, and prim_.

{
  if (type == prim_)  return String(_T("prim"));
  if (type == minimalDistance_) return String(_T("minimalDistance"));
  return String(_T("unknown"));
}
Integer const& STK::LocalVariance::nbNeighbor ( ) const [inline]

get the number of neighbors used n the local covariance.

Returns:
an array with the index of the neighbors of an individual

Definition at line 99 of file STK_LocalVariance.h.

References nbNeighbor_.

{ return nbNeighbor_;}
Array2D<Integer> const& STK::LocalVariance::pred ( ) const [inline]

get the predeccesor array

Returns:
an array with the index of the neighbors of an individual

Definition at line 104 of file STK_LocalVariance.h.

References neighbors_.

{ return neighbors_;}
MatrixSquare const& STK::LocalVariance::covariance ( ) const [inline]

get the covariance

Returns:
the covariance matrix of the data set

Definition at line 109 of file STK_LocalVariance.h.

References covariance_.

{ return covariance_;}
MatrixSquare const& STK::LocalVariance::localCovariance ( ) const [inline]

get the local covariance

Returns:
the local covariance matrix computed using the proximity graph.

Definition at line 115 of file STK_LocalVariance.h.

References p_localCov_.

{ return *p_localCov_;}
void STK::LocalVariance::update ( ) [protected, virtual]

Compute the proximity graph if the data set is modified.

Reimplemented from STK::IRunnerConstRef< Matrix >.

Definition at line 107 of file STK_LocalVariance.cpp.

References _T, dist_, STK::max(), minimalDistance(), minimalDistance_, nbNeighbor_, neighbors_, STK::IRunnerConstRef< Matrix >::p_y_, prim(), prim_, STK::IContainer2D::resize(), type_, and unknown_.

{
  // update dimensions of the containers for the proximity graph
  neighbors_.resize(p_y_->rangeVe(), Range(nbNeighbor_));
  dist_.resize(p_y_->rangeVe(), Range(nbNeighbor_));
  dist_ = Arithmetic<Real>::max();

  // compute minimal proximity graph of the data set
  switch (type_)
  {
    case prim_:
      prim();
      break;
    case minimalDistance_:
      minimalDistance();
      break;
    case unknown_:
      throw runtime_error(_T("Error in LocalVariance::update()\nWhat: "
                          "unknown proximity graph."));
      break;
  };
}

Here is the call graph for this function:

void STK::LocalVariance::maximizeIndex ( ) [protected, virtual]

Compute the axis by maximizing the ratio of the local variance on the total variance of the data set.

Implements STK::ILinearReduct.

Definition at line 134 of file STK_LocalVariance.cpp.

References clear(), computeAxis(), computeCovarianceMatrices(), and initializeMemory().

{
  // clear allocated memory
  clear();
  // initialize memory
  initializeMemory();
  // compute covariance matrices
  computeCovarianceMatrices();
  // compute the axis
  computeAxis();
}

Here is the call graph for this function:

void STK::LocalVariance::maximizeIndex ( Vector const &  weights) [protected, virtual]

Compute the axis by maximizing the ratio of the weighted local variance on the weighted total variance of the data set.

Parameters:
weightsthe weights to use

Implements STK::ILinearReduct.

Definition at line 150 of file STK_LocalVariance.cpp.

References clear(), computeAxis(), computeCovarianceMatrices(), and initializeMemory().

{
  // clear allocated memory
  clear();
  // initialize memory
  initializeMemory();
  // compute covariance matrices
  computeCovarianceMatrices(weights);
  // compute the axis
  computeAxis();
}

Here is the call graph for this function:

void STK::LocalVariance::prim ( ) [protected]

compute the minimal spanning tree

Definition at line 273 of file STK_LocalVariance.cpp.

References STK::dist(), STK::max(), neighbors_, STK::Funct::P, and STK::IRunnerConstRef< Matrix >::p_y_.

Referenced by LocalVariance(), and update().

{
  // get dimensions
  const Integer first_ind = p_y_->firstRow();
  const Integer last_ind = p_y_->lastRow();
  /* value vector : store the minimal value. */
  Vector value(p_y_->rangeVe(), Arithmetic<Real>::max());
  /* position of the points */
  Array1D<Integer> ipos(p_y_->rangeVe());
  // Initialization the position array
  for (Integer i=first_ind; i<=last_ind; i++) ipos[i] = i;

  // start Prim algorithm
  //Initialization of the root
  value[first_ind] = 0.0;               // the root have value 0.0
  neighbors_(first_ind, 1) = first_ind;          // and have itself as predecessor
  Integer imin = first_ind;             // the index of the current minimal value
  Real    kmin = 0.0;                   // the current minimal value
  // begin iterations
  for (Integer iter = last_ind; iter>=first_ind; iter--)
  {
    // put the minimal key at the end of the array key_
    value.swap(imin, iter);  // put the minimal value to the end
    ipos.swap(imin, iter);   // update the position of current minimal point
    // Update the value for the neighbors points and find minimal value
    imin = first_ind;
    kmin = value[first_ind];
    // ref on the current point
    Integer icur = ipos[iter];
    Point P((*p_y_)(icur), true);
    // update distance of the neighbors point
    for (Integer i=first_ind; i<iter; i++)
    {
      // check if we have a better distance for the neighbors
      Real d=dist(P, (*p_y_)(ipos[i]));
      if (d < value[i])
      {
        value[i] = d;
        neighbors_(ipos[i], 1) = icur;
      }
      // minimal key
      if (kmin>value[i]) { imin=i; kmin = value[i];}
    }
  }
}

Here is the call graph for this function:

void STK::LocalVariance::minimalDistance ( ) [protected]

compute the minimal distance graph

Definition at line 319 of file STK_LocalVariance.cpp.

References STK::dist(), dist_, nbNeighbor_, neighbors_, STK::Funct::P, and STK::IRunnerConstRef< Matrix >::p_y_.

Referenced by LocalVariance(), and update().

{
  // get dimensions
  const Integer first_ind = p_y_->firstRow();
  const Integer last_ind = p_y_->lastRow();
  // start minimal distance algorithm
  for (Integer j = first_ind; j<last_ind; j++)
  {
    // ref on the current point
    Point P((*p_y_)(j), true);
    // update distance of the neighbors point
    for (Integer i=j+1; i<=last_ind; i++)
    {
      // compute distance between point i and point j
      Real d=dist(P, (*p_y_)(i));
      // check if we get a better distance for the point j
      if (dist_(i, nbNeighbor_) > d )
      {
        // check if we get a better distance for the point i
        Integer pos = nbNeighbor_;
        while (dist_(i, pos) > d && pos-- > 1) {}
        pos++;
        // shift values
        for (int k= nbNeighbor_ -1; k>= pos; k--)
        {
          dist_(i, k+1) = dist_(i, k);
          neighbors_(i, k+1) = neighbors_(i, k);
        }
        // set minimal distance in place
        dist_(i, pos) = d;
        neighbors_(i, pos) = j;
      }
      // check if we get a better distance for the point j
      if (dist_(j, nbNeighbor_) > d )
      {
        // insertion sorting algorihtm
        Integer pos = nbNeighbor_;
        while (dist_(j, pos) > d && pos-- > 1) {}
        pos++;
        // shift valuesconst
        for (int k= nbNeighbor_ -1; k>= pos; k--)
        {
          dist_(j, k+1) = dist_(j, k);
          neighbors_(j, k+1) = neighbors_(j, k);
        }
        // set minimal distance in place
        dist_(j, pos) = d;
        neighbors_(j, pos) = i;
      }
    }
  }
}

Here is the call graph for this function:

void STK::LocalVariance::computeCovarianceMatrices ( ) [protected]

compute the covariances matrices of the data set

Definition at line 165 of file STK_LocalVariance.cpp.

References covariance_, nbNeighbor_, neighbors_, p_dataStatistics_, STK::IRunnerConstRef< Matrix >::p_y_, STK::Stat::Multivariate< TYPE, TContainer2D >::run(), and STK::sum().

Referenced by maximizeIndex().

{
  // compute the covariance matrix
  p_dataStatistics_->run();
  covariance_ = p_dataStatistics_->covariance();

  // constants
  const Integer first_ind = p_y_->firstRow();
  const Integer last_ind  = p_y_->lastRow();
  const Integer first_var = p_y_->firstCol();
  const Integer last_var  = p_y_->lastCol();
  const Real pond = 2* nbNeighbor_ * p_y_->sizeVe();

  // compute local covariance matrix
  for (Integer j=first_var; j<=last_var; j++)
  {
    // compute local covariance
    for (Integer k=first_var; k<=last_var; k++)
    {
      Real sum = 0.0;
      for (Integer i=first_ind; i<=last_ind; i++)
      {
        for (Integer l = 1; l <= nbNeighbor_; ++l)
        {
          sum += ((*p_y_)(i, j) - (*p_y_)(neighbors_(i, l), j))
               * ((*p_y_)(i, k) - (*p_y_)(neighbors_(i, l), k));
        }
      }
      (*p_localCov_)(j, k) = sum/pond;
    }
  }
}

Here is the call graph for this function:

void STK::LocalVariance::computeCovarianceMatrices ( Vector const &  weights) [protected]

compute the weighted covariances matrices of the data set

Definition at line 200 of file STK_LocalVariance.cpp.

References covariance_, nbNeighbor_, neighbors_, p_dataStatistics_, STK::IRunnerConstRef< Matrix >::p_y_, STK::Stat::Multivariate< TYPE, TContainer2D >::run(), and STK::sum().

{
  // compute the weighted covariance matrix using mMutivariate class
  p_dataStatistics_->run(weights);
  covariance_ = p_dataStatistics_->covariance();

  // get dimensions
  const Integer first_ind = p_y_->firstRow();
  const Integer last_ind  = p_y_->lastRow();
  const Integer first_var = p_y_->firstCol();
  const Integer last_var  = p_y_->lastCol();
  const Real pond = 2* nbNeighbor_;
  // compute weighted local covariance matrix
  for (Integer i=first_var; i<=last_var; i++)
  {
    // compute the local covariance matrix
    for (Integer j=first_var; j<=last_var; j++)
    {
      Real sum = 0.0;
      for (Integer k=first_ind; k<=last_ind; k++)
      {
        for (Integer l = 1; l <= nbNeighbor_; ++l)
        {
          sum += (weights[k] * weights[neighbors_(k, l)])
               * ((*p_y_)(k, i) - (*p_y_)(neighbors_(k, l), i))
               * ((*p_y_)(k, j) - (*p_y_)(neighbors_(k, l), j));
        }
      }
      (*p_localCov_)(i, j) = sum / pond;
    }
  }
}

Here is the call graph for this function:

void STK::LocalVariance::computeAxis ( ) [private]

compute the axis using the first eigenvectors of the matrix of covariance and local covariance

Definition at line 235 of file STK_LocalVariance.cpp.

References STK::ILinearReduct::axis_, covariance_, STK::IReduct::dim_, STK::EigenvaluesSymmetric::eigenvalues(), STK::Range::first(), STK::EigenvaluesSymmetric::ginv(), STK::ILinearReduct::index_values_, STK::Range::last(), STK::min(), STK::mult(), p_localCov_, STK::IRunnerConstRef< Matrix >::p_y_, STK::IContainer1D::resize(), STK::IContainer2D::resize(), STK::EigenvaluesSymmetric::rotation(), and STK::EigenvaluesSymmetric::run().

Referenced by maximizeIndex().

{
  // compute the number of axis
  Range range(p_y_->firstCol(), min(p_y_->firstCol()+dim_-1, p_y_->lastCol()));
  // constant
  const Integer first_axe = range.first();
  const Integer last_axe = range.last();

  // compute the eigenvalues decomposition of the local covariance
  EigenvaluesSymmetric* decomp = new EigenvaluesSymmetric(p_localCov_);
  decomp->run();
  // compute the generalized inverse of the local covariance
  MatrixSquare* inv_local = decomp->ginv();
  // we can safely remove the decomposition
  delete decomp;

  // compute the product
  MatrixSquare* prod = mult(*inv_local, covariance_);
  // we can safely remove the inverse
  delete inv_local;
  // compute the eigenvalues decomposition of the product
  decomp = new EigenvaluesSymmetric(prod);
  decomp->run();
  // we can sagely remove the product
  delete prod;

  // save axis and index values
  axis_.resize(p_y_->rangeHo(), range);
  index_values_.resize(range);
  for (Integer j=first_axe; j<=last_axe; j++)
  {
    axis_[j] = decomp->rotation()[j];
    index_values_[j] = decomp->eigenvalues()[j];
  }
  // we can safely remove the decomposition
  delete decomp;
}

Here is the call graph for this function:

void STK::LocalVariance::initializeMemory ( ) [private]

initialize memory

Definition at line 382 of file STK_LocalVariance.cpp.

References p_dataStatistics_, p_localCov_, and STK::IRunnerConstRef< Matrix >::p_y_.

Referenced by maximizeIndex().

{
  p_dataStatistics_ = new Stat::MultivariateMatrix(p_y_);
  p_localCov_       = new MatrixSquare(p_y_->rangeHo());
}
void STK::LocalVariance::clear ( ) [private]

clear allocated memory

Definition at line 373 of file STK_LocalVariance.cpp.

References p_dataStatistics_, and p_localCov_.

Referenced by maximizeIndex(), and ~LocalVariance().


Member Data Documentation

number of neighbors

Definition at line 124 of file STK_LocalVariance.h.

Referenced by LocalVariance(), and update().

number of neighbors

Definition at line 126 of file STK_LocalVariance.h.

Referenced by computeCovarianceMatrices(), minimalDistance(), nbNeighbor(), and update().

Predecessor array : store the spanning tree or the minimal distance to the neighbors.

Definition at line 129 of file STK_LocalVariance.h.

Referenced by computeCovarianceMatrices(), minimalDistance(), pred(), prim(), and update().

distance matrix : store the minimal distance to the neighbors

Definition at line 131 of file STK_LocalVariance.h.

Referenced by minimalDistance(), and update().

the local covariance Matrix

Definition at line 134 of file STK_LocalVariance.h.

Referenced by clear(), computeAxis(), initializeMemory(), and localCovariance().

the covariance Matrix

Definition at line 136 of file STK_LocalVariance.h.

Referenced by computeAxis(), computeCovarianceMatrices(), and covariance().

the multivariate Statistics.

This structure is used in order to compute the global covariance matrix of the data set.

Definition at line 140 of file STK_LocalVariance.h.

Referenced by clear(), computeCovarianceMatrices(), and initializeMemory().


The documentation for this class was generated from the following files: