Program Listing for File GWSS.h

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

#ifndef GWSS_H
#define GWSS_H

#include "SpatialMonoscaleAlgorithm.h"
#include "IMultivariableAnalysis.h"
#include "IParallelizable.h"

namespace gwm
{

class GWSS : public SpatialMonoscaleAlgorithm, public IMultivariableAnalysis, public IParallelizable, public IParallelOpenmpEnabled
{
public:

    static double covwt(const arma::mat &x1, const arma::mat &x2, const arma::vec &w)
    {
        return sum((sqrt(w) % (x1 - sum(x1 % w))) % (sqrt(w) % (x2 - sum(x2 % w)))) / (1 - sum(w % w));
    }

    static double corwt(const arma::mat &x1, const arma::mat &x2, const arma::vec &w)
    {
        return covwt(x1,x2,w)/sqrt(covwt(x1,x1,w)*covwt(x2,x2,w));
    }

    static arma::vec del(arma::vec x, arma::uword rowcount);

    static arma::vec rank(arma::vec x)
    {
        arma::vec n = arma::linspace(0.0, (double)x.n_rows - 1, x.n_rows);
        arma::vec res = n(sort_index(x));
        return n(sort_index(res)) + 1.0;
    }

    typedef void (GWSS::*SummaryCalculator)();

    enum class GWSSMode {
        Average,
        Correlation
    };

protected:
    static arma::vec findq(const arma::mat& x, const arma::vec& w);

public:

    GWSS() {}

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

    ~GWSS() {}

    bool quantile() const { return mQuantile; }

    void setQuantile(bool quantile) { mQuantile = quantile; }

    bool isCorrWithFirstOnly() const { return mIsCorrWithFirstOnly; }

    void setIsCorrWithFirstOnly(bool corrWithFirstOnly) { mIsCorrWithFirstOnly = corrWithFirstOnly; }

    const arma::mat& localMean() const { return mLocalMean; }

    const arma::mat& localSDev() const { return mStandardDev; }

    const arma::mat& localSkewness() const { return mLocalSkewness; }

    const arma::mat& localCV() const { return mLCV; }

    const arma::mat& localVar() const { return mLVar; }


    const arma::mat& localMedian() const { return mLocalMedian; }

    const arma::mat& iqr() const { return mIQR; }

    const arma::mat& qi() const { return mQI; }


    const arma::mat& localCov() const { return mCovmat; }

    const arma::mat& localCorr() const { return mCorrmat; }

    const arma::mat& localSCorr() const { return mSCorrmat; }

public:     // SpatialAlgorithm interface
    bool isValid() override;

public:     // IMultivariableAnalysis
    const arma::mat& variables() const override { return mX; }

    void setVariables(const arma::mat& x) override { mX = x; }


    void setGWSSMode(GWSSMode mode);

    void run() override;

public:     // IParallelizable
    int parallelAbility() const override
    {
        return ParallelType::SerialOnly
#ifdef ENABLE_OPENMP
            | ParallelType::OpenMP
#endif
            ;
    }
    ParallelType parallelType() const override { return mParallelType; }

    void setParallelType(const ParallelType& type) override;

public:     // IParallelOpenmpEnabled

    void setOmpThreadNum(const int threadNum) override { mOmpThreadNum = threadNum; }

    void updateCalculator();

private:
    void GWAverageSerial();

    void GWCorrelationSerial();

#ifdef ENABLE_OPENMP
    void GWAverageOmp();

    void GWCorrelationOmp();
#endif

private:
    bool mQuantile = false;
    bool mIsCorrWithFirstOnly = false;

    arma::mat mX;
    arma::mat mLocalMean;
    arma::mat mStandardDev;
    arma::mat mLocalSkewness;
    arma::mat mLCV;
    arma::mat mLVar;
    arma::mat mLocalMedian;
    arma::mat mIQR;
    arma::mat mQI;
    arma::mat mCovmat;
    arma::mat mCorrmat;
    arma::mat mSCorrmat;

    GWSSMode mGWSSMode = GWSSMode::Average;

    SummaryCalculator mSummaryFunction = &GWSS::GWAverageSerial;
    ParallelType mParallelType = ParallelType::SerialOnly;
    int mOmpThreadNum = 8;
};

}


#endif  // GWSS_H