|
STK++ 1.0
|
A LocalVariance is an implementation of the abstract ILinearReduct class.
More...
#include <STK_LocalVariance.h>


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< Integer > | neighbors_ |
| Predecessor array : store the spanning tree or the minimal distance to the neighbors. | |
| Matrix | dist_ |
| distance matrix : store the minimal distance to the neighbors | |
| MatrixSquare * | p_localCov_ |
| the local covariance Matrix | |
| MatrixSquare | covariance_ |
| the covariance Matrix | |
| Stat::MultivariateMatrix * | p_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 | |
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.
Type of proximity graph to used in order to compute the local variance:
Definition at line 66 of file STK_LocalVariance.h.
{ prim_, minimalDistance_, unknown_ };
| 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.
| data | the data set to process |
| type | type of proximity graph to build |
| nbNeighbor | number 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; }; }

| STK::LocalVariance::~LocalVariance | ( | ) | [virtual] |
Destructor.
Definition at line 101 of file STK_LocalVariance.cpp.
References clear().
{ clear();}

| LocalVariance::TypeGraph STK::LocalVariance::StringToTypeGraph | ( | String const & | type | ) | [static] |
convert a String to a TypeGraph.
| type | the type of graph in a 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_;
}

convert a TypeGraph to a String.
| type | the type of graph we want to convert to a string |
Definition at line 62 of file STK_LocalVariance.cpp.
References _T, minimalDistance_, and prim_.
| Integer const& STK::LocalVariance::nbNeighbor | ( | ) | const [inline] |
get the number of neighbors used n the local covariance.
Definition at line 99 of file STK_LocalVariance.h.
References nbNeighbor_.
{ return nbNeighbor_;}
get the predeccesor array
Definition at line 104 of file STK_LocalVariance.h.
References neighbors_.
{ return neighbors_;}
| MatrixSquare const& STK::LocalVariance::covariance | ( | ) | const [inline] |
get the covariance
Definition at line 109 of file STK_LocalVariance.h.
References covariance_.
{ return covariance_;}
| MatrixSquare const& STK::LocalVariance::localCovariance | ( | ) | const [inline] |
get the local covariance
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;
};
}

| 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();
}

| 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.
| weights | the 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();
}

| 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];}
}
}
}

| 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;
}
}
}
}

| 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;
}
}
}

| 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;
}
}
}

| 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;
}

| 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().
{
if (p_dataStatistics_) delete p_dataStatistics_;
p_dataStatistics_ = 0;
if (p_localCov_) delete p_localCov_;
p_localCov_ = 0;
}
TypeGraph STK::LocalVariance::type_ [protected] |
number of neighbors
Definition at line 124 of file STK_LocalVariance.h.
Referenced by LocalVariance(), and update().
Integer STK::LocalVariance::nbNeighbor_ [protected] |
number of neighbors
Definition at line 126 of file STK_LocalVariance.h.
Referenced by computeCovarianceMatrices(), minimalDistance(), nbNeighbor(), and update().
Array2D<Integer> STK::LocalVariance::neighbors_ [protected] |
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().
Matrix STK::LocalVariance::dist_ [protected] |
distance matrix : store the minimal distance to the neighbors
Definition at line 131 of file STK_LocalVariance.h.
Referenced by minimalDistance(), and update().
MatrixSquare* STK::LocalVariance::p_localCov_ [protected] |
the local covariance Matrix
Definition at line 134 of file STK_LocalVariance.h.
Referenced by clear(), computeAxis(), initializeMemory(), and localCovariance().
MatrixSquare STK::LocalVariance::covariance_ [protected] |
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().