patchGNU Octave - Patches: patch #10104, [octave forge] (statistics) Add...


patch #10104: [octave forge] (statistics) Add new function pca

Submitted by:  None
Submitted on:  Mon 30 Aug 2021 04:46:02 PM UTC
Category:  Forge : new function Priority:  5 - Normal
Status:  Done Privacy:  Public
Assigned to:  None Originator Email:  -email is unavailable-
Open/Closed:  Closed
* Mandatory Fields

Add a New Comment (Rich Markup)

Comment Type & Canned Response:


Thu 11 Nov 2021 09:21:52 PM UTC, comment #2: 

Thanks, Stefano, for the contribution. I incorporated Nicholas' improvements, removed the dependence on ismissing and weighted std since these have not been added to Octave yet, and added tests for the weighted case. The new function is now in the Statistics package repository:

Nir Krakauer <nir_krakauer>
Mon 30 Aug 2021 07:11:30 PM UTC, comment #1: 

perhaps worth adding to the help text something like
"Matlab compatibility note:  the method 'als' and associated options 'Coeff0', 'Score0', and 'Options' are not yet implemented"

it also looks like while an error is provided for the als method is present, you could add error messages for those other options as well. e.g., "Parameter %s is only valid with the 'ALS' method which is not yet implemented"

coding style things:
also looks like there's a .- at 302 that doesn't need to be there (all - are elementwise, so .- is redundant), and there was maintainer discussion about deprecating .- and .+ notation, so probably should remove.

some of your lines have end-of-line whitespace that should be removed. (mostly blank lines). 

in this package for a new fn add it seems to be sufficient to mention the bug/patch number at the end of the commit message

Nicholas Jankowski <nrjank>
Project Member
Mon 30 Aug 2021 04:46:02 PM UTC, original submission:  

This is a patch to add function 'pca' to the statistics package.

This is just the new name of the old function 'princomp', however I tried to improve its compatibility, adding new features.

This patch requires patch #10102 (ismissing) and patch #10103 (weighted standard deviation).

This patch misses the 'als' algorithm and its options. Also there is an inconsistency with its counterpart when computing the Hotelling statistics with weights: I am not sure if it is just numerical conditioning or if there is some unbiasing (or something like that) of the data in MATLAB.
Anyway this needs testing, even though I used it for some time and I can say that many things work.

Example usage:

octave:3> x
x =

   7   4   3
   4   1   8
   6   3   5
   8   6   1
   8   5   7
   7   2   9
   5   3   3
   9   5   8
   7   4   5
   8   2   2

octave:4> [c, s, l, t, e, m] = pca (x, "VariableWeights", "variance")
c =

   0.9783   0.5862  -1.0107
   1.0852   0.1536   1.1396
  -0.9590   2.5764   0.5660

s =

   0.514813  -0.630836   0.033512
  -2.660010   0.062809   0.330893
  -0.584039  -0.290606   0.156589
   2.047758  -0.909635   0.366273
   0.883274   0.991202   0.341538
  -1.083764   1.208571  -0.447062
  -0.761870  -1.197124   0.448103
   1.182837   1.570676  -0.021826
   0.271349   0.023254   0.177213
   0.189653  -0.828313  -1.385233

l =


t =


e =


m =

   6.9000   3.5000   5.1000

New help with a description of the new features:

octave:5> help pca

 -- Function File: [COEFF] = pca(X)
 -- Function File: [COEFF] = pca(X, Name, Value)
 -- Function File: [COEFF,SCORE,LATENT] = pca(...)
 -- Function File: [COEFF,SCORE,LATENT,TSQUARED] = pca(...)
     Performs a principal component analysis on a data matrix X

     A principal component analysis of a data matrix of 'n' observations
     in a 'p'-dimensional space returns a 'p'-by-'p' transformation
     matrix, to perform a change of basis on the data.  The first
     component of the new basis is the direction that maximizes the
     variance of the projected data.

     Input argument:
        * X : a 'n'-by-'p' data matrix

     Pair arguments:
        * 'Algorithm' : the algorithm to use, it can be either 'eig',
          for eigenvalue decomposition, or 'svd' (default), for singular
          value decomposition
        * 'Centered' : boolean indicator for centering the observation
          data, it is 'true' by default
        * 'Economy' : boolean indicator for the economy size output, it
          is 'true' by default; 'pca' returns only the elements of
          LATENT that are not necessarily zero, and the corresponding
          columns of COEFF and SCORE, that is, when 'n <= p', only the
          first 'n - 1'
        * 'NumComponents' : the number of components 'k' to return, if
          'k < p', then only the first 'k' columns of COEFF and SCORE
          are returned
        * 'Rows' : action to take with missing values, it can be either
          'complete' (default), missing values are removed before
          computation, 'pairwise' (only with algorithm 'eig'), the
          covariance of rows with missing data is computed using the
          available data, but the covariance matrix could be not
          positive definite, which triggers the termination of 'pca',
          'complete', missing values are not allowed, 'pca' terminates
          with an error if there are any
        * 'Weights' : observation weights, it is a vector of positive
          values of length 'n'
        * 'VariableWeights' : variable weights, it can be either a
          vector of positive values of length 'p' or the string
          'variance' to use the sample variance as weights

     Return values:
        * COEFF : the principal component coefficients, a 'p'-by-'p'
          transformation matrix
        * SCORE : the principal component scores, the representation of
          X in the principal component space
        * LATENT : the principal component variances, i.e., the
          eigenvalues of the covariance matrix of X
        * TSQUARED : Hotelling's T-squared Statistic for each
          observation in X
        * EXPLAINED : the percentage of the variance explained by each
          principal component
        * MU : the estimated mean of each variable of X, it is zero if
          the data are not centered


       1. Jolliffe, I. T., Principal Component Analysis, 2nd Edition,
          Springer, 2002



(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

Attached Files
file #51840:  pca.diff added by None (19KiB - text/x-patch - hg export -r tip)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by nir_krakauer (Posted a comment)
  • -email is unavailable- added by nrjank (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.





    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-11-23 nrjank StatusReady For Test => Done
        Open/ClosedOpen => Closed
    2021-11-11 nrjank StatusIn Progress => Ready For Test
    2021-08-30 nrjank StatusNone => In Progress
    2021-08-30 None Attached File- => Added pca.diff, #51840

    Back to the top

    Powered by Savane 3.9