Program Listing for File GWPCA.h

Return to documentation for file (include/gwmodelpp/GWPCA.h)

#ifndef GWPCA_H
#define GWPCA_H

#include "armadillo_config.h"
#include <vector>
#include <tuple>
#include "SpatialMonoscaleAlgorithm.h"
#include "IMultivariableAnalysis.h"
#include "IParallelizable.h"


namespace gwm
{

class GWPCA: public SpatialMonoscaleAlgorithm, public IMultivariableAnalysis
{
private:
    typedef arma::mat (GWPCA::*Solver)(const arma::mat&, arma::cube&, arma::mat&);

public: // Constructors and Deconstructors

    GWPCA() {}

    GWPCA(const arma::mat x, const arma::mat coords, const SpatialWeight& spatialWeight)
        : SpatialMonoscaleAlgorithm(spatialWeight, coords)
    {
        mX = x;
    }

    virtual ~GWPCA() {}

    int keepComponents() { return mK; }

    void setKeepComponents(int k) { mK = k; }

    const arma::mat& localPV() { return mLocalPV; }

    const arma::cube& loadings() { return mLoadings; }

    const arma::mat& sdev() { return mSDev; }

    const arma::cube& scores() { return mScores; }

public: // IMultivariableAnalysis
    virtual const arma::mat& variables() const override { return mX; }
    virtual void setVariables(const arma::mat& x) override { mX = x; }
    virtual void run() override;

public: // Algorithm
    virtual bool isValid() override;

private:

    arma::mat pca(const arma::mat& x, arma::cube& loadings, arma::mat& sdev)
    {
        return (this->*mSolver)(x, loadings, sdev);
    }

    arma::mat solveSerial(const arma::mat& x, arma::cube& loadings, arma::mat& sdev);

    void wpca(const arma::mat& x, const arma::vec& w, arma::mat& V, arma::vec & d);

private:    // Algorithm Parameters
    int mK = 2;
    // bool mRobust = false;

private:    // Algorithm Results
    arma::mat mLocalPV;
    arma::cube mLoadings;
    arma::mat mSDev;
    arma::cube mScores;
    arma::uvec mWinner;

private:    // Algorithm Runtime Variables
    arma::mat mX;
    arma::vec mLatestWt;

    Solver mSolver = &GWPCA::solveSerial;
};

}

#endif  // GWPCA_H