Skip to content

Commit

Permalink
Interface to set upper/lower bounds of golden selection (#75)
Browse files Browse the repository at this point in the history
* add: set upper/lower bounds for golden sel

* add: apply changes to GWRBasic
  • Loading branch information
HPDell authored Oct 30, 2023
1 parent 673df0c commit 1b403ee
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
17 changes: 17 additions & 0 deletions include/gwmodelpp/GWRBasic.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <utility>
#include <string>
#include <initializer_list>
#include <optional>
#include "GWRBase.h"
#include "RegressionDiagnostic.h"
#include "IBandwidthSelectable.h"
Expand Down Expand Up @@ -182,6 +183,20 @@ class GWRBasic : public GWRBase, public IBandwidthSelectable, public IVarialbeSe
*/
void setBandwidthSelectionCriterion(const BandwidthSelectionCriterionType& criterion);

/**
* @brief \~english Set the upper bounds of golden selection. \~chinese 设置 Golden selection 算法的上界。
*
* @param value \~english \~chinese
*/
void setGoldenUpperBounds(double value) { mGoldenUpperBounds = value; }

/**
* @brief \~english Set the lower bounds of golden selection. \~chinese 设置 Golden selection 算法的下界。
*
* @param value \~english \~chinese
*/
void setGoldenLowerBounds(double value) { mGoldenLowerBounds = value; }

/**
* \~english
* @brief Get whether auto select variables.
Expand Down Expand Up @@ -724,6 +739,8 @@ class GWRBasic : public GWRBase, public IBandwidthSelectable, public IVarialbeSe
BandwidthSelectionCriterionCalculator mBandwidthSelectionCriterionFunction = &GWRBasic::bandwidthSizeCriterionCVSerial; //!< \~english Criterion calculator for bandwidth selection. \~chinese 带宽优选的指标计算函数。
BandwidthCriterionList mBandwidthSelectionCriterionList; //!< \~english Criterion list of each bandwidth. \~chinese 每种带宽组合对应的指标值。
double mBandwidthLastCriterion = DBL_MAX; //!< \~english Last criterion for bandwidth selection. \~chinese 上一次带宽优选的有效指标值。
std::optional<double> mGoldenUpperBounds;
std::optional<double> mGoldenLowerBounds;

PredictCalculator mPredictFunction = &GWRBasic::predictSerial; //!< \~english Implementation of predict function. \~chinese 预测的具体实现函数。
FitCalculator mFitFunction = &GWRBasic::fitSerial; //!< \~english Implementation of fit function. \~chinese 拟合的具体实现函数。
Expand Down
19 changes: 18 additions & 1 deletion include/gwmodelpp/GWRMultiscale.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <utility>
#include <string>
#include <initializer_list>
#include <optional>
#include "SpatialMultiscaleAlgorithm.h"
#include "spatialweight/SpatialWeight.h"
#include "IRegressionAnalysis.h"
Expand Down Expand Up @@ -250,6 +251,20 @@ class GWRMultiscale : public SpatialMultiscaleAlgorithm, public IBandwidthSelect

public:

/**
* @brief \~english Set the upper bounds of golden selection. \~chinese 设置 Golden selection 算法的上界。
*
* @param value \~english \~chinese
*/
void setGoldenUpperBounds(double value) { mGoldenUpperBounds = value; }

/**
* @brief \~english Set the lower bounds of golden selection. \~chinese 设置 Golden selection 算法的下界。
*
* @param value \~english \~chinese
*/
void setGoldenLowerBounds(double value) { mGoldenLowerBounds = value; }

/**
* \~english
* @brief Get the type of bandwidth initilization.
Expand Down Expand Up @@ -966,7 +981,9 @@ class GWRMultiscale : public SpatialMultiscaleAlgorithm, public IBandwidthSelect
BandwidthSizeCriterionFunction mBandwidthSizeCriterion = &GWRMultiscale::bandwidthSizeCriterionAllCVSerial; //!< \~english The criterion calculator for given bandwidth size. \~chinese 根据指定带宽值计算指标值的函数。
size_t mBandwidthSelectionCurrentIndex = 0; //!< \~english The index of variable which currently the algorithm select bandwidth for. \~chinese 当前正在选带宽的变量索引值。
double mBandwidthLastCriterion = DBL_MAX; //!< \~english Last criterion for bandwidth selection. \~chinese 上一次带宽优选的有效指标值。

std::optional<double> mGoldenUpperBounds;
std::optional<double> mGoldenLowerBounds;
std::vector<double> mMaxDistances;

std::vector<BandwidthInitilizeType> mBandwidthInitilize; //!< \~english Type of bandwidth initilization values. \~chinese 带宽初始值类型。
std::vector<BandwidthSelectionCriterionType> mBandwidthSelectionApproach; //!< \~english Type of bandwidth selection approach. \~chinese 带宽选择方法类型。
Expand Down
4 changes: 2 additions & 2 deletions src/gwmodelpp/GWRBasic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ mat GWRBasic::fit()
{
GWM_LOG_STAGE("Bandwidth selection");
BandwidthWeight* bw0 = mSpatialWeight.weight<BandwidthWeight>();
double lower = bw0->adaptive() ? 20 : 0.0;
double upper = bw0->adaptive() ? nDp : mSpatialWeight.distance()->maxDistance();
double lower = mGoldenLowerBounds.value_or(bw0->adaptive() ? 20 : 0.0);
double upper = mGoldenUpperBounds.value_or(bw0->adaptive() ? nDp : mSpatialWeight.distance()->maxDistance());

GWM_LOG_INFO(IBandwidthSelectable::infoBandwidthCriterion(bw0));
BandwidthSelector selector(bw0, lower, upper);
Expand Down
12 changes: 7 additions & 5 deletions src/gwmodelpp/GWRMultiscale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ mat GWRMultiscale::fit()
uword nDp = mX.n_rows, nVar = mX.n_cols;
createDistanceParameter(nVar);
createInitialDistanceParameter();
mMaxDistances.resize(nVar);
#ifdef ENABLE_CUDA
if (mParallelType == ParallelType::CUDA)
{
Expand Down Expand Up @@ -113,12 +114,13 @@ mat GWRMultiscale::fit()
mXi = mX.col(i);
BandwidthWeight* bw0 = bandwidth(i);
bool adaptive = bw0->adaptive();
mMaxDistances[i] = mSpatialWeights[i].distance()->maxDistance();

GWM_LOG_INFO(string(GWM_LOG_TAG_MGWR_INITIAL_BW) + to_string(i));
BandwidthSelector selector;
selector.setBandwidth(bw0);
selector.setLower(adaptive ? mAdaptiveLower : 0.0);
selector.setUpper(adaptive ? mCoords.n_rows : mSpatialWeights[i].distance()->maxDistance());
selector.setLower(mGoldenLowerBounds.value_or(adaptive ? mAdaptiveLower : mMaxDistances[i] / 5000.0));
selector.setUpper(mGoldenUpperBounds.value_or(adaptive ? mCoords.n_rows : mMaxDistances[i]));
BandwidthWeight* bw = selector.optimize(this);
if (bw)
{
Expand Down Expand Up @@ -251,9 +253,9 @@ mat GWRMultiscale::backfitting(const mat &x, const vec &y)
bool adaptive = bwi0->adaptive();
BandwidthSelector selector;
selector.setBandwidth(bwi0);
double maxDist = mSpatialWeights[i].distance()->maxDistance();
selector.setLower(adaptive ? mAdaptiveLower : maxDist / 5000.0);
selector.setUpper(adaptive ? mCoords.n_rows : maxDist);
double maxDist = mMaxDistances[i];
selector.setLower(mGoldenLowerBounds.value_or(adaptive ? mAdaptiveLower : maxDist / 5000.0));
selector.setUpper(mGoldenUpperBounds.value_or(adaptive ? mCoords.n_rows : maxDist));
BandwidthWeight* bwi = selector.optimize(this);
double bwi0s = bwi0->bandwidth(), bwi1s = bwi->bandwidth();
vector<string> vbs_args {
Expand Down

0 comments on commit 1b403ee

Please sign in to comment.