Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a function to ensure the zero-devision and devision by zero #22

Merged
merged 3 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ In addition - most of these methods available here have also been implemented in

The BiasAdjustCXX command-line tool is the subject of a publication by Schwertfeger, Benjamin Thomas, Lohmann, Gerrit, and Lipskoch, Henrik (2023) _"Introduction of the BiasAdjustCXX command-line tool for the application of fast and efficient bias corrections in climatic research"_. It provides an insight into the architecture, possible applications and new scientific questions. This publication referencing [BiasAdjustCXX v1.8.1](https://github.com/btschwertfeger/BiasAdjustCXX/tree/v1.8.1) was published in the journal SoftwareX in March 2023 and is available at [https://doi.org/10.1016/j.softx.2023.101379](https://doi.org/10.1016/j.softx.2023.101379).


These tool and data structures are developed with the aim of reducing discrepancies between modeled and observed climate data. Historical data is utilized to calibrate variables from current and future time series to achieve distributional properties that closely resemble the possible actual values.

<figure>
Expand All @@ -53,7 +52,7 @@ These tool and data structures are developed with the aim of reducing discrepanc
<figcaption>Figure 1: Schematic representation of a bias adjustment procedure</figcaption>
</figure>

For instance, modeled data typically indicate values that are colder than the actual values. To address this issue, an adjustment procedure is employed. The figure below illustrates the observed, modeled, and adjusted values, revealing that the delta adjusted time series ($T^{\*DM}{sim,p}$) are significantly more similar to the observed data ($T{obs,p}$) than the raw modeled data ($T_{sim,p}$).
For instance, modeled data typically indicate values that are colder than the actual values. To address this issue, an adjustment procedure is employed. The figure below illustrates the observed, modeled, and adjusted values, revealing that the delta adjusted time series ($T^{\*DM}_{sim,p}$) are significantly more similar to the observed data ($T{obs,p}$) than the raw modeled data ($T_{sim,p}$).

<figure>
<img
Expand Down Expand Up @@ -264,7 +263,6 @@ BiasAdjustCXX --help

<a name="notes"></a>


## 📍 6. Notes

- For adjusting data using the linear scaling, variance scaling or delta method and the `--no-group` flag: You have to separate the input files by month and then apply the correction for each month individually. e.g. For 30 years of data to correct, you need to prepare the three input data sets so that they first contain all time series for all Januaries and then apply the adjustment for this data set. After that you have to do the same for the rest of the months (see `/examples/example_all_methods.run.sh`).
Expand Down
2 changes: 2 additions & 0 deletions include/MathUtils.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class MathUtils {
static std::vector<int> get_pdf(std::vector<float>& arr, std::vector<double>& bins);
static std::vector<int> get_cdf(std::vector<float>& arr, std::vector<double>& bins);
static double interpolate(std::vector<double>& xData, std::vector<double>& yData, double x, bool extrapolate);
static double ensure_devidable(double numerator, double denominator, double max_scaling_factor);
static float ensure_devidable(float numerator, float denominator, double max_scaling_factor);

private:
};
Expand Down
37 changes: 18 additions & 19 deletions src/CMethods.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,8 @@ void CMethods::Linear_Scaling(

} else if (settings.kind == "mult" || settings.kind == "*") {
const double
adjusted_scaling_factor = get_adjusted_scaling_factor(
MathUtils::mean(v_reference) / MathUtils::mean(v_control),

adjusted_scaling_factor = MathUtils::ensure_devidable(
MathUtils::mean(v_reference), MathUtils::mean(v_control),
settings.max_scaling_factor
);
for (unsigned ts = 0; ts < v_scenario.size(); ts++)
Expand Down Expand Up @@ -214,8 +213,8 @@ void CMethods::Linear_Scaling(
std::vector<float> adj_scaling_factors;
for (unsigned day = 0; day < 365; day++)
adj_scaling_factors.push_back(
get_adjusted_scaling_factor(
ref_365_means[day] / contr_365_means[day],
MathUtils::ensure_devidable(
ref_365_means[day], contr_365_means[day],
settings.max_scaling_factor
)
);
Expand Down Expand Up @@ -285,8 +284,8 @@ void CMethods::Variance_Scaling(
VS1_scen[ts] = LS_scen[ts] - LS_scen_mean; // Eq. 4

const double
adjusted_scaling_factor = get_adjusted_scaling_factor(
MathUtils::sd(v_reference) / MathUtils::sd(VS1_contr),
adjusted_scaling_factor = MathUtils::ensure_devidable(
MathUtils::sd(v_reference), MathUtils::sd(VS1_contr),
settings.max_scaling_factor
);

Expand Down Expand Up @@ -327,8 +326,8 @@ void CMethods::Variance_Scaling(

std::vector<double> adj_scaling_factors;
for (unsigned day = 0; day < 365; day++)
adj_scaling_factors.push_back(get_adjusted_scaling_factor(
ref_365_standard_deviations[day] / VS1_contr_365_standard_deviations[day],
adj_scaling_factors.push_back(MathUtils::ensure_devidable(
ref_365_standard_deviations[day], VS1_contr_365_standard_deviations[day],
settings.max_scaling_factor
));

Expand Down Expand Up @@ -378,8 +377,8 @@ void CMethods::Delta_Method(
v_output[ts] = v_reference[ts] + scaling_factor; // Eq. 1

} else if (settings.kind == "mult" || settings.kind == "*") {
const double adjusted_scaling_factor = get_adjusted_scaling_factor(
MathUtils::mean(v_scenario) / MathUtils::mean(v_control),
const double adjusted_scaling_factor = MathUtils::ensure_devidable(
MathUtils::mean(v_scenario), MathUtils::mean(v_control),
settings.max_scaling_factor
);
for (unsigned ts = 0; ts < v_scenario.size(); ts++)
Expand Down Expand Up @@ -411,8 +410,8 @@ void CMethods::Delta_Method(
std::vector<float> adj_scaling_factors;
for (unsigned day = 0; day < 365; day++)
adj_scaling_factors.push_back(
get_adjusted_scaling_factor(
scen_365_means[day] / contr_365_means[day],
MathUtils::ensure_devidable(
scen_365_means[day], contr_365_means[day],
settings.max_scaling_factor
)
);
Expand Down Expand Up @@ -630,12 +629,12 @@ void CMethods::Quantile_Delta_Mapping(
} else {
// clang-format off
for (unsigned ts = 0; ts < v_scenario.size(); ts++) {
double delta_basis = MathUtils::interpolate(contr_cdf, v_xbins, epsilon[ts], false);
v_output[ts] = (delta_basis == 0)
? (float)(QDM1[ts] * settings.max_scaling_factor) // Eq. 2.3f.
: (float)(QDM1[ts] * get_adjusted_scaling_factor(
v_scenario[ts] / delta_basis, settings.max_scaling_factor // Eq. 2.3f.
));

v_output[ts] = QDM1[ts] * MathUtils::ensure_devidable(
(double) v_scenario[ts],
MathUtils::interpolate(contr_cdf, v_xbins, epsilon[ts], false),
settings.max_scaling_factor
); // Eq. 2.3f.
}
// clang-format on
}
Expand Down
14 changes: 14 additions & 0 deletions src/MathUtils.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,17 @@ double MathUtils::interpolate(std::vector<double>& xData, std::vector<double>& y
dydx = (yR - yL) / (xR - xL); // gradient
return yL + dydx * (x - xL); // linear interpolation
}

double MathUtils::ensure_devidable(double numerator, double denuminator, double max_scaling_factor) {
if (denuminator == (double)0)
return numerator * max_scaling_factor;

return numerator / denuminator;
}

float MathUtils::ensure_devidable(float numerator, float denuminator, double max_scaling_factor) {
if (denuminator == (double)0)
return numerator * max_scaling_factor;

return numerator / denuminator;
}
9 changes: 9 additions & 0 deletions tests/src/TestMathUtils.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ TEST_F(TestMathUtils, CheckLinearInterpolation2d) {
ASSERT_EQ(targets_with_extrapolation[i], ::MathUtils::interpolate(xData, yData, x[i], true));
}
}

TEST_F(TestMathUtils, CheckEnsureDevidable) {
ASSERT_EQ(::MathUtils::ensure_devidable((double)5, (double)5, 10), 1);
ASSERT_EQ(::MathUtils::ensure_devidable((double)0, (double)5, 10), 0);
ASSERT_EQ(::MathUtils::ensure_devidable((double)5, (double)0, 10), 50);
ASSERT_EQ(::MathUtils::ensure_devidable((float)5, (float)5, 10), 1);
ASSERT_EQ(::MathUtils::ensure_devidable((float)0, (float)5, 10), 0);
ASSERT_EQ(::MathUtils::ensure_devidable((float)5, (float)0, 10), 50);
}
} // namespace
} // namespace MathUtils
} // namespace TestBiasAdjustCXX