Core component analysis using libpca

libpca is a C ++ core component analysis library that builds on Armadillo , a linear algebra library.

I have a problem with this. I compare his conclusion with the example given by Lindsay Smith in her excellent ATP textbook. When I extract the first main component, in his tutorial, I get the same values ​​as Smith, but with inverted signs. For the second main component, the signs and values ​​are correct.

Does anyone know why this is?

code:

#include "pca.h"
#include <iostream>

using namespace std;

int main(int argc, char** argv) {
    stats::pca pca(2);

    double* elements = new double[20]{2.5, 2.4, 0.5, 0.7, 2.2, 2.9, 1.9, 2.2, 3.1, 3.0, 2.3, 2.7, 2, 1.6, 1, 1.1, 1.5, 1.6, 1.1, 0.9};
    for (int i = 0; i < 20; i++) {
        vector<double> record;
        record.push_back(elements[i++]);
        record.push_back(elements[i]);
        pca.add_record(record);
    }

    pca.solve();             

    const vector<double> principal_1 = pca.get_principal(0);
    for (int i = 0; i < principal_1.size(); i++)
        cout << principal_1[i] << " ";
    cout << endl;

    const vector<double> principal_2 = pca.get_principal(1);
    for (int i = 0; i < principal_2.size(); i++)
        cout << principal_2[i] << " ";
    cout << endl;

    delete elements;
    return 0;
}

      

Output:

0.82797 -1.77758 0.992197 0.27421 1.6758 0.912949 -0.0991094 -1.14457 -0.438046 -1.22382 
-0.175115 0.142857 0.384375 0.130417 -0.209498 0.175282 -0.349825 0.0464173 0.0177646 -0.162675 

      

+3


source to share


1 answer


@mtall already has a root cause: the main components form a normal subspace basis. Regardless of how you created the base, multiplying any base vector by -1 forms another base of the same subspace.



This is quite easy to see: the multiplication of a vector v by any constant does not change its direction. if v is normal to w, then 2 * v is normal to 3 * w. Multiplying a vector by -1 reverses its direction. If v and w have an angle alpha, then -v and w have an angle (pi-alpha). But if alpha was pi / 2, v and w were normal, (pi-pi / 2) is still pi / 2, and therefore -v and w are normal as well.

+4


source







All Articles