Changeset 829


Ignore:
Timestamp:
Mar 24, 2007, 3:30:28 PM (17 years ago)
Author:
Jari Häkkinen
Message:

Fixes #173.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/pca_test.cc

    r680 r829  
    3838    for( size_t j = 0; j < 4; ++j )
    3939      A(i,j)= sin( static_cast<double>(i+j+2+(i+1)*(j+1)) );
    40 
     40  A.transpose();
    4141  utility::PCA pca(A);
    4242
    43 // Print only matrix that is row-centered as PCA ( process() )
    44 // will do that before calculation
    45   pca.process_transposed_problem();
    4643  return 0;
    4744}
  • trunk/yat/utility/PCA.cc

    r792 r829  
    3636
    3737  PCA::PCA(const utility::matrix& A)
    38     : A_(A), process_(false), explained_calc_(false)
     38    : A_(A)
    3939  {
     40    process();
    4041  }
    4142
    4243
    43   utility::vector PCA::get_eigenvector(size_t i) const
     44  const utility::vector& PCA::eigenvalues(void) const
    4445  {
    45     return utility::vector(eigenvectors_,i);
     46    return eigenvalues_;
    4647  }
    4748
    4849
    49   double PCA::get_eigenvalue(size_t i) const
     50  const utility::matrix& PCA::eigenvectors(void) const
    5051  {
    51     return eigenvalues_[i];
     52    return eigenvectors_;
    5253  }
    5354
     
    5556  void PCA::process(void)
    5657  {
    57     process_ = true;
    5858    // Row-center the data matrix
    5959    utility::matrix A_center( A_.rows(), A_.columns() );
     
    8989
    9090
     91  /*
    9192  void PCA::process_transposed_problem(void)
    9293  {
    93     process_ = true;
    9494    // Row-center the data matrix
    9595    utility::matrix A_center( A_.rows(), A_.columns() );
     
    132132        }
    133133  }
    134 
    135 
    136   // This function will row-center the matrix A_,
    137   // that is, A_ = A_ - M, where M is a matrix
    138   // with the meanvalues of each row
    139   void PCA::row_center(utility::matrix& A_center)
    140   {
    141     meanvalues_.clone(utility::vector(A_.rows()));
    142     utility::vector A_row_sum(A_.rows());
    143     for (size_t i=0; i<A_row_sum.size(); ++i)
    144       A_row_sum(i)=utility::sum(utility::vector(A_,i));
    145     for( size_t i = 0; i < A_center.rows(); ++i ) {
    146       meanvalues_[i] = A_row_sum(i) / static_cast<double>(A_.columns());
    147       for( size_t j = 0; j < A_center.columns(); ++j )
    148         A_center(i,j) = A_(i,j) - meanvalues_(i);
    149     }
    150   }
     134  */
    151135
    152136
     
    172156
    173157
     158  /*
    174159  utility::matrix
    175160  PCA::projection_transposed(const utility::matrix& samples) const
     
    192177    return projs;
    193178  }
     179  */
    194180
    195181
    196   void PCA::calculate_explained_intensity(void)
     182  // This function will row-center the matrix A_,
     183  // that is, A_ = A_ - M, where M is a matrix
     184  // with the meanvalues of each row
     185  void PCA::row_center(utility::matrix& A_center)
    197186  {
    198     size_t DIM = eigenvalues_.size();
    199     explained_intensity_.clone(utility::vector( DIM ));
    200     double sum = 0;
    201     for( size_t i = 0; i < DIM; ++i )
    202       sum += eigenvalues_[ i ];
    203     double exp_sum = 0;
    204     for( size_t i = 0; i < DIM; ++i ) {
    205       exp_sum += eigenvalues_[ i ];
    206       explained_intensity_[ i ] = exp_sum / sum ;
     187    meanvalues_.clone(utility::vector(A_.rows()));
     188    utility::vector A_row_sum(A_.rows());
     189    for (size_t i=0; i<A_row_sum.size(); ++i)
     190      A_row_sum(i)=utility::sum(utility::vector(A_,i));
     191    for( size_t i = 0; i < A_center.rows(); ++i ) {
     192      meanvalues_[i] = A_row_sum(i) / static_cast<double>(A_.columns());
     193      for( size_t j = 0; j < A_center.columns(); ++j )
     194        A_center(i,j) = A_(i,j) - meanvalues_(i);
    207195    }
    208196  }
    209197
    210 
    211   double PCA::get_explained_intensity( const size_t& k )
    212   {
    213     if( !explained_calc_ ) {
    214       this->calculate_explained_intensity();
    215       explained_calc_ = true;
    216     }
    217     return explained_intensity_[ k ];
    218   }
    219 
    220 
    221198}}} // of namespace utility, yat, and theplu
  • trunk/yat/utility/PCA.h

    r767 r829  
    5555 
    5656    /**
    57        Will perform PCA according to the following scheme: \n
    58        1: Rowcenter A  \n
    59        2: SVD(A)  --> USV' \n
    60        3: Calculate eigenvalues according to \n
    61           \f$ \lambda_{ii} = s_{ii}/N_{rows} \f$ \n
    62        4: Sort eigenvectors (from matrix V) according to descending eigenvalues\n
    63     */
    64     void process(void);
    65 
    66     /**
    6757       If M<N use this method instead. Using the same format as before
    6858       where rows in the matrix corresponds to the dimensional coordinate.
     
    7161       projection_transposed() method.
    7262     */
    73     void process_transposed_problem(void);
     63    //    void process_transposed_problem(void);
    7464
    7565    /**
    76        @return Eigenvector \a i.
     66       \brief Returns eigenvalues in a utility::vector.
     67
     68       \return A const reference to the internal vector containing all
     69       eigenvalues.
    7770    */
    78     utility::vector get_eigenvector(size_t i) const;
     71    const utility::vector& eigenvalues(void) const;
    7972
    8073    /**
    81        Returns eigenvalues to covariance matrix
    82        \f$ C = \frac{1}{N^2}A^TA \f$
     74       \brief Get all eigenvectors in a utility::matrix.
     75
     76       \return A const reference to the internal matrix containing all
     77       eigenvectors.
    8378    */
    84     double get_eigenvalue(size_t i) const;
    85 
    86     /**
    87        Returns the explained intensity of component \a K \n
    88        \f$I = \frac{ \sum^{K}_{i=1} \lambda_i }{ \sum^{N}_{j=1} \lambda_j }\f$ \n
    89        where \f$N\f$ is the dimension
    90     */
    91     double get_explained_intensity( const size_t& k );
     79    const utility::matrix& eigenvectors(void) const;
    9280
    9381    /**
     
    10492       process_transposed_problem().
    10593    */
    106     utility::matrix projection_transposed( const utility::matrix& ) const;
     94    //    utility::matrix projection_transposed( const utility::matrix& ) const;
    10795
    10896
    10997  private:
    110     utility::matrix A_;
    111     utility::matrix eigenvectors_;
    112     utility::vector eigenvalues_;
    113     utility::vector explained_intensity_;
    114     utility::vector meanvalues_;
    115     bool process_, explained_calc_;
    116    
     98
     99    /**
     100       Will perform PCA according to the following scheme: \n
     101       1: Rowcenter A  \n
     102       2: SVD(A)  --> USV' \n
     103       3: Calculate eigenvalues according to \n
     104          \f$ \lambda_{ii} = s_{ii}/N_{rows} \f$ \n
     105       4: Sort eigenvectors (from matrix V) according to descending eigenvalues\n
     106    */
     107    void process(void);
     108
    117109    /**
    118110       Private function that will row-center the matrix A,
     
    122114    void row_center( utility::matrix& A_center );
    123115
    124     /**
    125        Private function that will calculate the explained
    126        intensity
    127     */
    128     void calculate_explained_intensity(void);
    129   }; // class PCA 
    130 
     116    utility::matrix A_;
     117    utility::vector eigenvalues_;
     118    utility::matrix eigenvectors_;
     119    utility::vector meanvalues_;
     120  };
    131121
    132122}}} // of namespace utility, yat, and theplu
Note: See TracChangeset for help on using the changeset viewer.