Skip to content

Fix pca.m#287

Merged
pr0m1th3as merged 2 commits intognu-octave:mainfrom
Sonu0305:pca
Nov 11, 2025
Merged

Fix pca.m#287
pr0m1th3as merged 2 commits intognu-octave:mainfrom
Sonu0305:pca

Conversation

@Sonu0305
Copy link
Copy Markdown
Contributor

Fixes #279

octave:13> [COEFF, SCORE, latent, tsquare] = pca (x, "Economy", false, "weights", [1 2 1], "variableweights", "variance")
COEFF =

   0.6325  -0.6325
   0.7416   0.7416

SCORE =

  -0.622019   0.959119
  -0.505650  -0.505650
   1.633319   0.052180

latent =

   1.7830
   0.7170

tsquare =

   1.5000
   0.5000
   1.5000

@pr0m1th3as
Copy link
Copy Markdown
Member

Can you add a few more tests with different weights and check against MATLAB, please?

@pr0m1th3as
Copy link
Copy Markdown
Member

pr0m1th3as commented Nov 11, 2025

I think that the current implementation returns different value in tsquare when the economy tag is true.

@Sonu0305
Copy link
Copy Markdown
Contributor Author

Can you add a few more tests with different weights and check against MATLAB, please?

Can you suggest few tests for the same?

@Sonu0305
Copy link
Copy Markdown
Contributor Author

I think that the current implementation returns different value in tsquare when the economy tag is true.

Replied above, thanks.

@pr0m1th3as
Copy link
Copy Markdown
Member

pr0m1th3as commented Nov 11, 2025

Using the same shared x=[1,2,3;2,1,3]';, test for

[COEFF,SCORE,latent,tsquare] = pca(x, "Economy", false, "weights", [2 1 2], "variableweights", "variance");
[COEFF,SCORE,latent,tsquare] = pca(x, "Economy", false, "weights", [1 3 2], "variableweights", "variance");
[COEFF,SCORE,latent,tsquare] = pca(x, "Economy", false, "weights", [1 0.5 1.5], "variableweights", "variance");

and with economy true

[COEFF,SCORE,latent,tsquare] = pca(x, "Economy", true, "weights", [2 1 2], "variableweights", "variance");
[COEFF,SCORE,latent,tsquare] = pca(x, "Economy", true, "weights", [1 3 2], "variableweights", "variance");
[COEFF,SCORE,latent,tsquare] = pca(x, "Economy", true, "weights", [1 0.5 1.5], "variableweights", "variance");

Use the same test set for each of the examples above:


%!assert(COEFF, [0.632455532033676 -0.632455532033676; 0.741619848709566 0.741619848709566], 10*eps);
%!assert(SCORE, [-0.622019449426284 0.959119380657905; -0.505649896847432 -0.505649896847431; 1.633319243121148 0.052180413036957], 10*eps);
%!assert(latent, [1.783001790889027; 0.716998209110974], 10*eps);
!xtest assert(tsquare, [1.5; 0.5; 1.5], 10*eps);  

@Sonu0305
Copy link
Copy Markdown
Contributor Author

MATLAB:

>> [COEFF,SCORE,latent,tsquare] = pca(x, "Economy", false, "weights", [2 1 2], "variableweights", "variance")
[COEFF,SCORE,latent,tsquare] = pca(x, "Economy", false, "weights", [1 3 2], "variableweights", "variance")
[COEFF,SCORE,latent,tsquare] = pca(x, "Economy", false, "weights", [1 0.5 1.5], "variableweights", "variance")

COEFF =

    0.7906    0.7906
    0.6614   -0.6614


SCORE =

   -0.7836   -0.4813
   -0.9071    0.9071
    1.2372    0.0277


latent =

    2.5562
    0.6438


tsquare =

    0.6000
    1.6000
    0.6000


COEFF =

    0.6216   -0.6216
    0.8118    0.8118


SCORE =

   -0.8358    1.0411
   -0.6473   -0.3792
    1.3889    0.0482


latent =

    2.9067
    0.7599


tsquare =

    1.6667
    0.3333
    0.6667


COEFF =

    0.8118    0.8118
    0.6742   -0.6742


SCORE =

   -0.9657   -0.4713
   -1.0915    0.8862
    1.0076    0.0188


latent =

    1.5257
    0.3077


tsquare =

    1.3333
    3.3333
    0.6667

Octave:

octave:4> [COEFF,SCORE,latent,tsquare] = pca(x, "Economy", false, "weights", [2 1 2], "variableweights", "variance")
[COEFF,SCORE,latent,tsquare] = pca(x, "Economy", false, "weights", [1 3 2], "variableweights", "variance")
[COEFF,SCORE,latent,tsquare] = pca(x, "Economy", false, "weights", [1 0.5 1.5], "variableweights", "variance")
COEFF =

   0.7906   0.7906
   0.6614  -0.6614

SCORE =

  -0.783641  -0.481270
  -0.907115   0.907115
   1.237199   0.027712

latent =

   2.5562
   0.6438

tsquare =

   0.6000
   1.6000
   0.6000

COEFF =

   0.6216  -0.6216
   0.8118   0.8118

SCORE =

  -0.835819   1.041113
  -0.647301  -0.379168
   1.388862   0.048196

latent =

   2.9067
   0.7599

tsquare =

   1.6667
   0.3333
   0.6667

COEFF =

   0.8118   0.8118
   0.6742  -0.6742

SCORE =

  -0.965735  -0.471322
  -1.091473   0.886180
   1.007648   0.018822

latent =

   1.5257
   0.3077

tsquare =

   1.3333
   3.3333
   0.6667

Answers match for the first one!

@Sonu0305
Copy link
Copy Markdown
Contributor Author

MATLAB:

>> x=[1,2,3;2,1,3]';
>> [COEFF,SCORE,latent,tsquare] = pca(x, "Economy", true, "weights", [2 1 2], "variableweights", "variance")
[COEFF,SCORE,latent,tsquare] = pca(x, "Economy", true, "weights", [1 3 2], "variableweights", "variance")
[COEFF,SCORE,latent,tsquare] = pca(x, "Economy", true, "weights", [1 0.5 1.5], "variableweights", "variance")

COEFF =

    0.7906    0.7906
    0.6614   -0.6614


SCORE =

   -0.7836   -0.4813
   -0.9071    0.9071
    1.2372    0.0277


latent =

    2.5562
    0.6438


tsquare =

    0.6000
    1.6000
    0.6000


COEFF =

    0.6216   -0.6216
    0.8118    0.8118


SCORE =

   -0.8358    1.0411
   -0.6473   -0.3792
    1.3889    0.0482


latent =

    2.9067
    0.7599


tsquare =

    1.6667
    0.3333
    0.6667


COEFF =

    0.8118    0.8118
    0.6742   -0.6742


SCORE =

   -0.9657   -0.4713
   -1.0915    0.8862
    1.0076    0.0188


latent =

    1.5257
    0.3077


tsquare =

    1.3333
    3.3333
    0.6667

Octave:

octave:9> x=[1,2,3;2,1,3]';
octave:10> [COEFF,SCORE,latent,tsquare] = pca(x, "Economy", true, "weights", [2 1 2], "variableweights", "variance")
[COEFF,SCORE,latent,tsquare] = pca(x, "Economy", true, "weights", [1 3 2], "variableweights", "variance")
[COEFF,SCORE,latent,tsquare] = pca(x, "Economy", true, "weights", [1 0.5 1.5], "variableweights", "variance")
COEFF =

   0.7906   0.7906
   0.6614  -0.6614

SCORE =

  -0.783641  -0.481270
  -0.907115   0.907115
   1.237199   0.027712

latent =

   2.5562
   0.6438

tsquare =

   0.6000
   1.6000
   0.6000

COEFF =

   0.6216  -0.6216
   0.8118   0.8118

SCORE =

  -0.835819   1.041113
  -0.647301  -0.379168
   1.388862   0.048196

latent =

   2.9067
   0.7599

tsquare =

   1.6667
   0.3333
   0.6667

COEFF =

   0.8118   0.8118
   0.6742  -0.6742

SCORE =

  -0.965735  -0.471322
  -1.091473   0.886180
   1.007648   0.018822

latent =

   1.5257
   0.3077

tsquare =

   1.3333
   3.3333
   0.6667

@Sonu0305
Copy link
Copy Markdown
Contributor Author

Hi @pr0m1th3as,

Both cases are MATLAB Compatible, as per two comments I posted above.

@pr0m1th3as
Copy link
Copy Markdown
Member

Can you add these series of tests before I merge?

@pr0m1th3as
Copy link
Copy Markdown
Member

Thanks a lot

@pr0m1th3as pr0m1th3as merged commit 3279644 into gnu-octave:main Nov 11, 2025
@Sonu0305 Sonu0305 deleted the pca branch November 12, 2025 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Weighted pca returns erroneous Hotelling's T-squared statistics

2 participants