Tapkee
pca.hpp
Go to the documentation of this file.
00001 /* This software is distributed under BSD 3-clause license (see LICENSE file).
00002  *
00003  * Copyright (c) 2012-2013 Sergey Lisitsyn
00004  */
00005 
00006 #ifndef TAPKEE_PCA_H_
00007 #define TAPKEE_PCA_H_
00008 
00009 /* Tapkee includes */
00010 #include <tapkee/defines.hpp>
00011 #include <tapkee/utils/time.hpp>
00012 /* End of Tapkee includes */
00013 
00014 namespace tapkee
00015 {
00016 namespace tapkee_internal
00017 {
00018 
00019 template <class RandomAccessIterator, class FeatureVectorCallback>
00020 DenseMatrix project(const DenseMatrix& projection_matrix, const DenseVector& mean_vector,
00021                     RandomAccessIterator begin, RandomAccessIterator end, 
00022                     FeatureVectorCallback callback, IndexType dimension)
00023 {
00024     timed_context context("Data projection");
00025 
00026     DenseVector current_vector(dimension);
00027     DenseVector current_vector_subtracted_mean(dimension);
00028 
00029     DenseMatrix embedding = DenseMatrix::Zero((end-begin),projection_matrix.cols());
00030 
00031     for (RandomAccessIterator iter=begin; iter!=end; ++iter)
00032     {
00033         callback.vector(*iter,current_vector);
00034         current_vector_subtracted_mean = current_vector - mean_vector;
00035         embedding.row(iter-begin) = projection_matrix.transpose()*current_vector_subtracted_mean;
00036     }
00037 
00038     return embedding;
00039 }
00040 
00041 template <class RandomAccessIterator, class FeatureVectorCallback>
00042 DenseVector compute_mean(RandomAccessIterator begin, RandomAccessIterator end,
00043                          FeatureVectorCallback callback, IndexType dimension) 
00044 {
00045     DenseVector mean = DenseVector::Zero(dimension);
00046     DenseVector current_vector(dimension);
00047     for (RandomAccessIterator iter=begin; iter!=end; ++iter)
00048     {
00049         callback.vector(*iter,current_vector);
00050         mean += current_vector;
00051     }
00052     mean.array() /= (end-begin);
00053     return mean;
00054 }
00055 
00056 template <class RandomAccessIterator, class FeatureVectorCallback>
00057 DenseSymmetricMatrix compute_covariance_matrix(RandomAccessIterator begin, RandomAccessIterator end, 
00058                                                const DenseVector& mean, FeatureVectorCallback callback, IndexType dimension)
00059 {
00060     timed_context context("Constructing PCA covariance matrix");
00061 
00062     DenseSymmetricMatrix covariance_matrix = DenseSymmetricMatrix::Zero(dimension,dimension);
00063     
00064     DenseVector current_vector(dimension);
00065     for (RandomAccessIterator iter=begin; iter!=end; ++iter)
00066     {
00067         callback.vector(*iter,current_vector);
00068         covariance_matrix.selfadjointView<Eigen::Upper>().rankUpdate(current_vector,1.0);
00069     }
00070     covariance_matrix.selfadjointView<Eigen::Upper>().rankUpdate(mean,-1.0);
00071 
00072     return covariance_matrix;
00073 }
00074 
00075 template <class RandomAccessIterator, class KernelCallback>
00076 DenseSymmetricMatrix compute_centered_kernel_matrix(RandomAccessIterator begin, RandomAccessIterator end, 
00077                                                     KernelCallback callback)
00078 {
00079     timed_context context("Constructing kPCA centered kernel matrix");
00080 
00081     DenseSymmetricMatrix kernel_matrix(end-begin,end-begin);
00082 
00083     for (RandomAccessIterator i_iter=begin; i_iter!=end; ++i_iter)
00084     {
00085         for (RandomAccessIterator j_iter=i_iter; j_iter!=end; ++j_iter)
00086         {
00087             ScalarType k = callback.kernel(*i_iter,*j_iter);
00088             kernel_matrix(i_iter-begin,j_iter-begin) = k;
00089             kernel_matrix(j_iter-begin,i_iter-begin) = k;
00090         }
00091     }
00092 
00093     centerMatrix(kernel_matrix);
00094 
00095     return kernel_matrix;
00096 }
00097 
00098 } // End of namespace tapkee_internal
00099 } // End of namespace tapkee
00100 
00101 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines