Skip to content

Commit 6da23cd

Browse files
authored
fix: Box-Cox lambda when using Guerrero's method (#97)
1 parent 232416d commit 6da23cd

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

include/coreforecast/scalers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ T BoxCox_GuerreroCV(T lambda, const std::vector<T> &x_mean,
7979
auto x_rat =
8080
std_vec.array() / (mean_vec.array().log() * (1.0 - lambda)).exp();
8181
double mean = x_rat.mean();
82-
double std = (x_rat.array() - mean).square().sum() / (x_rat.size() - 1);
83-
return static_cast<T>(std / mean);
82+
double var = (x_rat.array() - mean).square().sum() / (x_rat.size() - 1);
83+
return static_cast<T>(std::sqrt(var) / mean);
8484
}
8585

8686
template <typename T>

tests/test_scalers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ def test_boxcox_correctness(data, indptr, dtype, method):
9191
np.testing.assert_allclose(first_tfm, transformed[first_grp])
9292
np.testing.assert_allclose(first_restored, restored[first_grp])
9393

94+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
95+
def test_guerrero_correctness(dtype):
96+
data = [194, 229, 249, 203, 196, 238, 252, 210, 205, 236]
97+
expected_lambda = 1.99 # value from R's BoxCox.lambda function (forecast package)
98+
calculated_lambda = boxcox_lambda(data, method="guerrero", season_length=4)
99+
np.testing.assert_allclose(calculated_lambda, expected_lambda, atol=0.1)
94100

95101
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
96102
def test_difference_correctness(data, indptr, dtype):

0 commit comments

Comments
 (0)