STK++ 1.0

STK::LinearAAModelMixtureManager Class Reference

#include <STK_LinearAAModelMixtureManager.h>

List of all members.

Public Member Functions

 LinearAAModelMixtureManager (Matrix *data)
virtual ~LinearAAModelMixtureManager ()
void centerAndReduce ()
void createIndex (const LocalVariance::TypeGraph &type=LocalVariance::minimal_distance_, Integer const &nbNeighbor=1)
void run (Array1D< Integer > const *p_dim, Integer const &maxIter)
void run (Matrix const &weight, Array1D< Integer > const *p_dim, Integer const &maxIter)
void save (String const &fileName)

Protected Attributes

Matrixp_data_
Vector mean_
Vector std_
LocalVariancep_index_
LinearAAModelMixturep_aamm_
Array1D< Integer > const * p_dim_

Private Member Functions

void createAAMM ()

Detailed Description

Definition at line 51 of file STK_LinearAAModelMixtureManager.h.


Constructor & Destructor Documentation

home iovleff Developpement workspace stkpp projects AAModels src STK_LinearAAModelMixtureManager cpp STK::LinearAAModelMixtureManager::LinearAAModelMixtureManager ( Matrix data)

Constructor. Use an existing a data set.

Parameters:
datathe data set to process

Definition at line 47 of file STK_LinearAAModelMixtureManager.cpp.

                                                        : p_data_(data)
                                                        , mean_(p_data_->rangeHo(), 0.0)
                                                        , std_(p_data_->rangeHo(), 1.0)
                                                        , p_index_(0)
                                                        , p_aamm_(0)
{ }

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

virtual destructor

Definition at line 56 of file STK_LinearAAModelMixtureManager.cpp.

References p_aamm_, and p_index_.

{
  // remove any existing index
  if (p_index_) delete p_index_;
  // delete any existing aamm
  if (p_aamm_) delete p_aamm_;
}


Member Function Documentation

void STK::LinearAAModelMixtureManager::centerAndReduce ( )

Center and reduce the data set.

Definition at line 67 of file STK_LinearAAModelMixtureManager.cpp.

References STK::IContainer2D::firstCol(), STK::IContainer2D::firstRow(), STK::IContainer2D::lastCol(), STK::IContainer2D::lastRow(), STK::Stat::mean(), mean_, p_data_, std_, and STK::Stat::variance().

{
  // get dimensions
  const Integer first_var = p_data_->firstCol();
  const Integer last_var  = p_data_->lastCol();
  const Integer first_ind = p_data_->firstRow();
  const Integer last_ind  = p_data_->lastRow();

  // for each variables compute the mean and the std
  for (Integer j=first_var; j<=last_var; j++)
  {
    Real mean_j = Stat::mean((*p_data_)[j]);
    if (!Arithmetic<Real>::isNA(mean_j))
      (*p_data_)[j] -= mean_j;
    mean_[j] = mean_j;
    Real std_j = Stat::variance((*p_data_)[j], mean_j);
    if (!Arithmetic<Real>::isNA(std_j))
    {
      std_j = sqrt(double(std_j));
      (*p_data_)[j] /= std_j;
    }
    std_[j] = std_j;
  }
  // for each individual, center and reduce
  for (Integer i= first_ind; i<= last_ind; i++)
  {
    ((*p_data_)(i) -= mean_) /= std_;
  }
}

void STK::LinearAAModelMixtureManager::createIndex ( const LocalVariance::TypeGraph type = LocalVariance::minimal_distance_,
Integer const &  nbNeighbor = 1 
)

Create the Index used by the AA Models in the mixture.

Parameters:
typetype of proximity graph to build
nbNeighbornumber of neighbors to use in the proximity graph

Definition at line 103 of file STK_LinearAAModelMixtureManager.cpp.

References p_data_, and p_index_.

Referenced by createAAMM().

{
  // remove any existing index
  if (p_index_) delete p_index_;
  // create index
  p_index_ = new LocalVariance(p_data_, type, nbNeighbor);
}

void STK::LinearAAModelMixtureManager::run ( Array1D< Integer > const *  p_dim,
Integer const &  maxIter 
)

Create the LinearAAModelMixture and run the computation. The data set have to be centered and reduced if needed, the Index have to be created and the AAMM have to be initialized before calling this method. The size of the container p_dim gives the number of clusters. Initialize the EM algorithm using an ad-hoc method. The mixture will be created in this method.

Parameters:
p_dimthe dimension of each clusters.
maxItermaximal number of iteration

Definition at line 117 of file STK_LinearAAModelMixtureManager.cpp.

References createAAMM(), STK::LinearAAModelMixture::initialize(), p_aamm_, p_dim_, and STK::LinearAAModelMixture::run().

{
  // save dimensions
  p_dim_ = p_dim;
  // create Auto-Associative model mixture
  createAAMM();
  // initialize mixtures
  p_aamm_->initialize(p_dim_);
  // run computations
  p_aamm_->run(maxIter);
}

void STK::LinearAAModelMixtureManager::run ( Matrix const &  weight,
Array1D< Integer > const *  p_dim,
Integer const &  maxIter 
)

Create the LinearAAModelMixture and run the computation. The data set have to be centered and reduced if needed, the Index have to be created and the AAMM have to be initialized before calling this method. The size of the container p_dim gives the number of clusters. Initialize the EM algorithm setting the weights. The mixture will be created in this method.

Parameters:
weightthe weights to set
p_dimthe dimension of each clusters.
maxItermaximal number of iteration

Definition at line 136 of file STK_LinearAAModelMixtureManager.cpp.

References createAAMM(), STK::LinearAAModelMixture::initialize(), p_aamm_, p_dim_, and STK::LinearAAModelMixture::run().

{
  // save dimensions
  p_dim_ = p_dim;
  // create Auto-Associative model mixture
  createAAMM();
  // initialize mixtures
  p_aamm_->initialize(weight, p_dim_);
  // run computations
  p_aamm_->run(maxIter);
}

void STK::LinearAAModelMixtureManager::save ( String const &  fileName)

Save the results in vaious files usoing the given fileName.

Parameters:
fileNamethe name of the file to use.

Definition at line 156 of file STK_LinearAAModelMixtureManager.cpp.

References STK::LinearAAModelMixture::autoAssocModel(), STK::IContainer1D::first(), STK::IContainer2D::firstRow(), STK::LinearAAModelMixture::index(), STK::IContainer1D::last(), STK::IContainer2D::lastRow(), mean_, p_aamm_, p_data_, p_dim_, STK::ExportToCsv::p_readWriteCsv(), STK::ReadWriteCsv::setWithNames(), std_, STK::typeToString(), and STK::ReadWriteCsv::write().

{
  // get dimensions
  const Integer first_cluster = p_dim_->first();
  const Integer last_cluster = p_dim_->last();
  const Integer first_ind = p_data_->firstRow();
  const Integer last_ind  = p_data_->lastRow();

  //restore original data
  Matrix restored_data(*p_data_);
  for (Integer i= first_ind; i<= last_ind; i++)
  {
    (restored_data(i) *= std_) += mean_;
  }
  // save restored data sets
  ExportToCsv* csv_data = new ExportToCsv(restored_data);
  csv_data->p_readWriteCsv()->setWithNames(false);
  csv_data->p_readWriteCsv()->write(fileName + "_data.csv");
  delete csv_data;
  // save
  csv_data = new ExportToCsv(static_cast<const LocalVariance&>(p_aamm_->index()).pred());
  csv_data->p_readWriteCsv()->setWithNames(false);
  csv_data->p_readWriteCsv()->write(fileName + "_pred.csv");
  delete csv_data;

  // save results for each clusters
  for (Integer k= first_cluster; k<= last_cluster; k++)
  {
    // copy kth restored data set
    restored_data = p_aamm_->autoAssocModel(k).predicted();
    // for each individual, unscale and uncenter
    for (Integer i= first_ind; i<= last_ind; i++)
    {
      (restored_data(i) *= std_) += mean_;
    }
    // save restord data set
    csv_data = new ExportToCsv(restored_data);
    csv_data->p_readWriteCsv()->setWithNames(false);
    csv_data->p_readWriteCsv()->write(fileName + "_resto" + typeToString(k) + ".csv");
    delete csv_data;
  }
}

void STK::LinearAAModelMixtureManager::createAAMM ( ) [private]

Create the LinearAAModelMixture.

Definition at line 202 of file STK_LinearAAModelMixtureManager.cpp.

References createIndex(), p_aamm_, and p_index_.

Referenced by run().

{
  // check if there exists an index
  if (!p_index_)
  {
    // create index with default values
    createIndex();
  }
  // delete any existing aamm
  if (p_aamm_) delete p_aamm_;
  // create aamm
  p_aamm_ = new LinearAAModelMixture(p_index_);
}


Member Data Documentation

Matrix of the data set to process

Definition at line 55 of file STK_LinearAAModelMixtureManager.h.

Referenced by centerAndReduce(), createIndex(), and save().

the vector of the means

Definition at line 115 of file STK_LinearAAModelMixtureManager.h.

Referenced by centerAndReduce(), and save().

the vector of the standard deviations

Definition at line 117 of file STK_LinearAAModelMixtureManager.h.

Referenced by centerAndReduce(), and save().

Index for computing the axis of projection.

Definition at line 120 of file STK_LinearAAModelMixtureManager.h.

Referenced by createAAMM(), createIndex(), and ~LinearAAModelMixtureManager().

dimension of each clusters

Definition at line 124 of file STK_LinearAAModelMixtureManager.h.

Referenced by run(), and save().


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