Skip to content

Commit

Permalink
[Feat] Add rate Smoother EBS
Browse files Browse the repository at this point in the history
  • Loading branch information
lixun910 committed Oct 21, 2024
1 parent b512f74 commit a2a40be
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@
*.app
.DS_Store
*.tmp
.vscode/settings.json
68 changes: 68 additions & 0 deletions gda_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,71 @@ bool gda_rateStandardizeEB(const std::vector<double>& P,
delete [] p;
return !has_undef;
}

bool gda_rateSmootherEBS(const std::vector<double> &P,
const std::vector<double> &E,
std::vector<double> &results,
std::vector<bool> &undefined) {
int obs = (int)P.size();

if (results.size() != obs) {
results.resize(obs);
}

bool has_undef = false;
double SP = 0.0, SE = 0.0;
double *pi_raw = new double[obs];
int i = 0;
int valid_obs = 0;

for (i=0; i<obs; i++) {
if (undefined[i]) {
pi_raw[i] = 0;
results[i] = 0;
has_undef = true;
} else {
valid_obs += 1;
SP += P[i];
SE += E[i];
if (P[i] == 0) {
undefined[i] = true;
results[i] = 0;
has_undef = true;
} else {
pi_raw[i] = E[i] / P[i];
}
}
}

double theta1 = 1.0, theta2 = 0.0;

if (SP > 0) {
theta1 = SE / SP;
}

double p_bar = SP / valid_obs;
double q1 = 0, w = 0;

for (i=0; i<obs; i++) {
if (!undefined[i]) {
q1 += P[i] * (pi_raw[i] - theta1) * (pi_raw[i] - theta1);
}
}

theta2 = (q1 / SP) - (theta1 / p_bar);

if (theta2 < 0) {
theta2 = 0;
}

for (i=0; i<obs; i++) {
if (!undefined[i]) {
q1 = (theta2 + (theta1 / P[i]));
w = q1 > 0 ? theta2 / q1 : 1;
results[i] = (w * pi_raw[i]) + ((1 - w) * theta1);
}
}

delete[] pi_raw;
return !has_undef;
}
49 changes: 30 additions & 19 deletions gda_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,44 @@

// APIs for data processing

std::vector<std::vector<double> > gda_demean(const std::vector<std::vector<double> >& data);
std::vector<std::vector<double>>
gda_demean(const std::vector<std::vector<double>> &data);

std::vector<std::vector<double> > gda_standardize(const std::vector<std::vector<double> >& data);
std::vector<std::vector<double>>
gda_standardize(const std::vector<std::vector<double>> &data);

std::vector<std::vector<double> > gda_standardize_mad(const std::vector<std::vector<double> >& data);
std::vector<std::vector<double>>
gda_standardize_mad(const std::vector<std::vector<double>> &data);

std::vector<double> gda_naturalbreaks(int k, const std::vector<double>& data,
const std::vector<bool>& undefs);
std::vector<double> gda_naturalbreaks(int k, const std::vector<double> &data,
const std::vector<bool> &undefs);

std::vector<double> gda_quantilebreaks(int k, const std::vector<double>& data,
const std::vector<bool>& undefs);
std::vector<double> gda_quantilebreaks(int k, const std::vector<double> &data,
const std::vector<bool> &undefs);

std::vector<double> gda_hinge15breaks(const std::vector<double>& data,
const std::vector<bool>& undefs);
std::vector<double> gda_hinge15breaks(const std::vector<double> &data,
const std::vector<bool> &undefs);

std::vector<double> gda_hinge30breaks(const std::vector<double>& data,
const std::vector<bool>& undefs);
std::vector<double> gda_hinge30breaks(const std::vector<double> &data,
const std::vector<bool> &undefs);

std::vector<double> gda_percentilebreaks(const std::vector<double>& data,
const std::vector<bool>& undefs);
std::vector<double> gda_percentilebreaks(const std::vector<double> &data,
const std::vector<bool> &undefs);

std::vector<double> gda_stddevbreaks( const std::vector<double>& data,
const std::vector<bool>& undefs);
std::vector<double> gda_stddevbreaks(const std::vector<double> &data,
const std::vector<bool> &undefs);

void gda_transform_inplace(std::vector<double>& vals, const std::string& method);
void gda_transform_inplace(std::vector<double> &vals,
const std::string &method);

bool gda_rateStandardizeEB(const std::vector<double>& P, const std::vector<double>& E,
std::vector<double>& results, std::vector<bool> &undefined);
bool gda_rateStandardizeEB(const std::vector<double> &P,
const std::vector<double> &E,
std::vector<double> &results,
std::vector<bool> &undefined);

#endif //GEODA_GDA_DATA_H
bool gda_rateSmootherEBS(const std::vector<double> &P,
const std::vector<double> &E,
std::vector<double> &results,
std::vector<bool> &undefined);

#endif // GEODA_GDA_DATA_H

0 comments on commit a2a40be

Please sign in to comment.