diff --git a/CMakeLists.txt b/CMakeLists.txt index e67d424e..8766481d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,17 +15,20 @@ add_subdirectory(extern/NNS-core) # Preserve the accepted NNS-core vendor tree; explicitly overlay the R14 # central-tendency kernels that its R13.3 snapshot does not yet supply, the -# multivariate prediction kernel with R's own nmath densities, and R's NNS.seas -# lag scan (kept out of the nanobind module, which is compiled for size). +# multivariate prediction kernel with R's own nmath densities, the partial +# moments with R's D.UPM degree handling, and R's NNS.seas lag scan (kept out of +# the nanobind module, which is compiled for size). get_target_property(NNS_CORE_SOURCES nnscore SOURCES) set(NNS_R14_CORE_SOURCES "") foreach(source IN LISTS NNS_CORE_SOURCES) - if(NOT source STREQUAL "src/central_tendencies.cpp" AND NOT source STREQUAL "src/mreg_predict.cpp") + if(NOT source STREQUAL "src/central_tendencies.cpp" AND NOT source STREQUAL "src/mreg_predict.cpp" + AND NOT source STREQUAL "src/partial_moments.cpp") list(APPEND NNS_R14_CORE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/extern/NNS-core/${source}") endif() endforeach() list(APPEND NNS_R14_CORE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/nns/_r14_central_tendencies.cpp") list(APPEND NNS_R14_CORE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/nns/_r14_mreg_predict.cpp") +list(APPEND NNS_R14_CORE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/nns/_r14_partial_moments.cpp") list(APPEND NNS_R14_CORE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/nns/_r14_seas.cpp") set_property(TARGET nnscore PROPERTY SOURCES "${NNS_R14_CORE_SOURCES}") diff --git a/src/nns/_r14_partial_moments.cpp b/src/nns/_r14_partial_moments.cpp new file mode 100644 index 00000000..6cd27bc3 --- /dev/null +++ b/src/nns/_r14_partial_moments.cpp @@ -0,0 +1,940 @@ +// src/nns/_r14_partial_moments.cpp: R14 overlay of NNS-core src/partial_moments.cpp +// +// Identical to the vendored kernel except for d_upm's mixed-degree branches. +// The vendor applied the integer fast path to the wrong side when exactly one +// degree is an integer; this follows R's DUPM_C, so D.UPM matches R bit for bit. +// +// Implementation extracted from NNS 13.0 src/partial_moments.{h,cpp}. +// Every numerical rule (>= vs > boundaries, integer-degree fast powers, +// min/max-length recycling, population adjustment gating, crossed DUPM/DLPM +// mirroring) is preserved verbatim. The vectorised (> 32 target) backend uses +// a cancellation-free formulation instead of the upstream median-shifted +// binomial expansion (see the comment above PrefixBackend). +// +// SPDX-License-Identifier: GPL-3.0-only + +// ---- NNS-CORE PROVENANCE (managed by scripts/sync_adapter.py; do not hand-edit) ---- +// NNS-R-SOURCE: src/partial_moments.cpp @ 82a92eb45c1a1892acce82e66175cf2e29434b65 +// NNS-R-SOURCE: src/partial_moments.h @ 82a92eb45c1a1892acce82e66175cf2e29434b65 +// ---- end NNS-CORE PROVENANCE ---- +#include "nns/partial_moments.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "nns/parallel.hpp" + +namespace nns { +namespace { + +constexpr double kNaN = std::numeric_limits::quiet_NaN(); + +// --- shared helpers (ports of the static helpers in partial_moments.cpp) --- + +inline double repeat_multiplication(double value, int n) { + double result = 1.0; + for (int i = 0; i < n; ++i) result *= value; + return result; +} + +inline bool is_integer(double v) { return v == static_cast(static_cast(v)); } + +// Rejects negative and NaN degrees (NNS 13.3 returned the degree-0 count for a +// negative degree). +inline void check_degree(double degree, const char* what = "degree") { + if (!(degree >= 0.0)) { + throw std::invalid_argument(std::string("`") + what + + "` must be a non-negative number"); + } +} + +inline double lower_component(double diff, double degree, bool degree_is_int) { + if (degree == 0) return diff >= 0.0 ? 1.0 : 0.0; + if (diff < 0.0) return 0.0; + return degree_is_int ? repeat_multiplication(diff, static_cast(degree)) + : std::pow(diff, degree); +} + +inline double upper_component(double diff, double degree, bool degree_is_int) { + if (degree == 0) return diff > 0.0 ? 1.0 : 0.0; + if (diff < 0.0) return 0.0; + return degree_is_int ? repeat_multiplication(diff, static_cast(degree)) + : std::pow(diff, degree); +} + +// --- sorted-anchor prefix backend (vectorised LPM/UPM, > 32 targets) ------- +// +// NNS 13.0-13.3 expanded sum_{x_i <= t} (t - x_i)^d binomially around the +// sample median, which cancels catastrophically for d >= 3. This backend keeps +// the O((n + m) log n) cost but anchors every expansion at an adjacent order +// statistic, so all terms are non-negative: +// +// lower_j(k) = sum_{i < k} (x_(k-1) - x_(i))^j (anchor = largest point <= t) +// upper_j(k) = sum_{i >= k} (x_(i) - x_(k))^j (anchor = smallest point > t) +// +// Both tables are built with the exact recurrence +// lower_j(k+1) = sum_{l <= j} C(j,l) delta^(j-l) lower_l(k) + [j == 0], +// delta = x_(k) - x_(k-1) >= 0, +// and a target t with k = #{x_i <= t} is evaluated as +// LPM = sum_j C(d,j) (t - x_(k-1))^(d-j) lower_j(k) / n +// UPM = sum_j C(d,j) (x_(k) - t)^(d-j) upper_j(k) / n. + +constexpr int kPrefixMaxDegree = 32; +constexpr std::size_t kDirectPathMaxTargets = 32; // NNS_DIRECT_PATH_MAX_TARGETS + +bool prefix_supported_degree(double degree, int& degree_int) { + if (!std::isfinite(degree) || degree < 0.0) return false; + const double rounded = std::round(degree); + if (std::fabs(degree - rounded) > 1e-12) return false; + if (rounded > static_cast(kPrefixMaxDegree)) return false; + degree_int = static_cast(rounded); + return true; +} + +struct PrefixBackend { + std::vector sorted; + std::vector lower; // (n + 1) x (degree + 1), row k = lower_j(k) + std::vector upper; // (n + 1) x (degree + 1), row k = upper_j(k) + std::vector pascal; // (degree + 1) x (degree + 1), C(j, l) + std::size_t n; + int degree; + + PrefixBackend(const double* variable, std::size_t n_, int degree_) + : sorted(variable, variable + n_), n(n_), degree(degree_) { + for (std::size_t i = 0; i < n; ++i) { + if (!std::isfinite(sorted[i])) { // defensive guard, as upstream + sorted.clear(); + n = 0; + return; + } + } + std::sort(sorted.begin(), sorted.end()); + const std::size_t w = static_cast(degree) + 1U; + + pascal.assign(w * w, 0.0); + for (std::size_t j = 0; j < w; ++j) { + pascal[j * w] = 1.0; + for (std::size_t l = 1; l <= j; ++l) + pascal[j * w + l] = pascal[(j - 1U) * w + l - 1U] + + (l < j ? pascal[(j - 1U) * w + l] : 0.0); + } + + lower.assign((n + 1U) * w, 0.0); + upper.assign((n + 1U) * w, 0.0); + if (n == 0U) return; + std::vector dpow(w); + + lower[1U * w] = 1.0; // k = 1: the single point equals its anchor + for (std::size_t k = 1; k < n; ++k) { + fill_powers(sorted[k] - sorted[k - 1U], dpow); + step(&lower[k * w], &lower[(k + 1U) * w], dpow); + } + upper[(n - 1U) * w] = 1.0; // k = n - 1: the single point equals its anchor + for (std::size_t k = n - 1U; k > 0; --k) { + fill_powers(sorted[k] - sorted[k - 1U], dpow); + step(&upper[k * w], &upper[(k - 1U) * w], dpow); + } + } + + void fill_powers(double base, std::vector& pw) const { + pw[0] = 1.0; + for (std::size_t p = 1; p < pw.size(); ++p) pw[p] = pw[p - 1U] * base; + } + + // next_j = sum_{l <= j} C(j,l) delta^(j-l) prev_l + [j == 0] + void step(const double* prev, double* next, const std::vector& dpow) const { + const std::size_t w = static_cast(degree) + 1U; + for (std::size_t j = 0; j < w; ++j) { + double acc = (j == 0U) ? 1.0 : 0.0; + for (std::size_t l = 0; l <= j; ++l) { + if (prev[l] != 0.0) acc += pascal[j * w + l] * dpow[j - l] * prev[l]; + } + next[j] = acc; + } + } + + // sum_j C(d,j) gap^(d-j) sums_j, all terms >= 0. + double combine(const double* sums, double gap) const { + const std::size_t d = static_cast(degree); + const std::size_t w = d + 1U; + double out = 0.0; + double gpow = 1.0; // gap^(d-j), built from j = d downwards + for (std::size_t jj = w; jj-- > 0U;) { + if (sums[jj] != 0.0) out += pascal[d * w + jj] * gpow * sums[jj]; + gpow *= gap; + } + return out; + } + + bool ok() const { return n > 0U; } + + std::size_t count_leq(double target) const { + return static_cast( + std::upper_bound(sorted.begin(), sorted.end(), target) - + sorted.begin()); + } + + double lpm(double target) const { + if (!std::isfinite(target)) return kNaN; + const std::size_t k = count_leq(target); + const double nd = static_cast(n); + if (degree == 0) return static_cast(k) / nd; + if (k == 0U) return 0.0; + const std::size_t w = static_cast(degree) + 1U; + return combine(&lower[k * w], target - sorted[k - 1U]) / nd; + } + + double upm(double target) const { + if (!std::isfinite(target)) return kNaN; + const std::size_t k = count_leq(target); + const double nd = static_cast(n); + if (degree == 0) return static_cast(n - k) / nd; + if (k == n) return 0.0; + const std::size_t w = static_cast(degree) + 1U; + return combine(&upper[k * w], sorted[k] - target) / nd; + } + + std::pair both(double target) const { + return std::make_pair(lpm(target), upm(target)); + } +}; + +std::shared_ptr make_prefix_backend(double degree, + const double* x, + std::size_t n) { + int degree_int = 0; + if (n == 0) return nullptr; + if (!prefix_supported_degree(degree, degree_int)) return nullptr; + for (std::size_t i = 0; i < n; ++i) + if (!std::isfinite(x[i])) return nullptr; + return std::make_shared(x, n, degree_int); +} + +} // namespace + +// --- univariate scalar kernels (ports of LPM_C / UPM_C) -------------------- + +double lpm(double degree, double target, const double* x, std::size_t n) { + check_degree(degree); + double out = 0; + const bool deg_is_int = is_integer(degree); + for (std::size_t i = 0; i < n; ++i) { + const double value = target - x[i]; + if (value >= 0) { + if (deg_is_int) { + if (degree == 0) + out += 1; + else if (degree == 1) + out += value; + else + out += repeat_multiplication(value, static_cast(degree)); + } else { + out += std::pow(value, degree); + } + } + } + out /= static_cast(n); + return out; +} + +double upm(double degree, double target, const double* x, std::size_t n) { + check_degree(degree); + double out = 0; + const bool deg_is_int = is_integer(degree); + for (std::size_t i = 0; i < n; ++i) { + const double value = x[i] - target; + if (value > 0) { + if (deg_is_int) { + if (degree == 0) + out += 1; + else if (degree == 1) + out += value; + else + out += repeat_multiplication(value, static_cast(degree)); + } else { + out += std::pow(value, degree); + } + } + } + out /= static_cast(n); + return out; +} + +// --- vectorized univariate (ports of LPM_CPv / UPM_CPv / ratio kernels) ---- + +namespace { + +enum class PMKind { Lower, Upper, LowerRatio, UpperRatio }; + +template +void pm_vectorized(double degree, const double* target, std::size_t n_targets, + const double* x, std::size_t n, double* out, + int n_threads) { + check_degree(degree); + // Direct path for few targets: bit-identical to pre-13.0 semantics and + // avoids building the prefix backend (port of the <=32 guard). + const bool few_targets = n_targets <= kDirectPathMaxTargets; + std::shared_ptr prefix = + few_targets ? nullptr : make_prefix_backend(degree, x, n); + + auto body = [&](std::size_t begin, std::size_t end) { + for (std::size_t i = begin; i < end; ++i) { + const double t = target[i]; + double l = 0.0, u = 0.0; + if (prefix && std::isfinite(t)) { + if (K == PMKind::Lower) { + out[i] = prefix->lpm(t); + continue; + } + if (K == PMKind::Upper) { + out[i] = prefix->upm(t); + continue; + } + const std::pair pm = prefix->both(t); + l = pm.first; + u = pm.second; + } else { + if (K == PMKind::Lower) { + out[i] = lpm(degree, t, x, n); + continue; + } + if (K == PMKind::Upper) { + out[i] = upm(degree, t, x, n); + continue; + } + l = lpm(degree, t, x, n); + u = upm(degree, t, x, n); + } + out[i] = (K == PMKind::LowerRatio) ? l / (l + u) : u / (l + u); + } + }; + + if (few_targets) { + body(0, n_targets); // serial direct path, as upstream + } else { + // Prefix lookups are O(log n + degree); the direct scan (fractional or + // > 32 degree) is O(n) per target. + parallel_for(0, n_targets, body, n_threads, prefix ? 64U : n); + } +} + +} // namespace + +void lpm_v(double degree, const double* target, std::size_t n_targets, + const double* x, std::size_t n, double* out, int n_threads) { + pm_vectorized(degree, target, n_targets, x, n, out, + n_threads); +} + +void upm_v(double degree, const double* target, std::size_t n_targets, + const double* x, std::size_t n, double* out, int n_threads) { + pm_vectorized(degree, target, n_targets, x, n, out, + n_threads); +} + +void lpm_ratio_v(double degree, const double* target, std::size_t n_targets, + const double* x, std::size_t n, double* out, int n_threads) { + if (degree > 0) { + pm_vectorized(degree, target, n_targets, x, n, out, + n_threads); + } else { + lpm_v(degree, target, n_targets, x, n, out, n_threads); + } +} + +void upm_ratio_v(double degree, const double* target, std::size_t n_targets, + const double* x, std::size_t n, double* out, int n_threads) { + if (degree > 0) { + pm_vectorized(degree, target, n_targets, x, n, out, + n_threads); + } else { + upm_v(degree, target, n_targets, x, n, out, n_threads); + } +} + +// --- bivariate co-moments (ports of CoUPM_C/CoLPM_C/DLPM_C/DUPM_C) --------- + +double co_upm(double degree_x, double degree_y, const double* x, + const double* y, std::size_t n_x, std::size_t n_y, + double target_x, double target_y) { + check_degree(degree_x, "degree_x"); + check_degree(degree_y, "degree_y"); + const std::size_t max_size = (n_x > n_y ? n_x : n_y); + const std::size_t min_size = (n_x < n_y ? n_x : n_y); + if (min_size == 0) return 0; + double out = 0; + const bool d_x_0 = (degree_x == 0), d_y_0 = (degree_y == 0); + const bool x_is_int = is_integer(degree_x), y_is_int = is_integer(degree_y); + for (std::size_t i = 0; i < min_size; ++i) { + double x1 = (x[i] - target_x); + double y1 = (y[i] - target_y); + if (d_x_0) + x1 = (x1 > 0 ? 1 : 0); + else + x1 = (x1 < 0 ? 0 : x1); + if (d_y_0) + y1 = (y1 > 0 ? 1 : 0); + else + y1 = (y1 < 0 ? 0 : y1); + if (!d_x_0) + x1 = x_is_int ? repeat_multiplication(x1, static_cast(degree_x)) + : std::pow(x1, degree_x); + if (!d_y_0) + y1 = y_is_int ? repeat_multiplication(y1, static_cast(degree_y)) + : std::pow(y1, degree_y); + out += x1 * y1; + } + return out / static_cast(max_size); +} + +double co_lpm(double degree_x, double degree_y, const double* x, + const double* y, std::size_t n_x, std::size_t n_y, + double target_x, double target_y) { + check_degree(degree_x, "degree_x"); + check_degree(degree_y, "degree_y"); + const std::size_t max_size = (n_x > n_y ? n_x : n_y); + const std::size_t min_size = (n_x < n_y ? n_x : n_y); + if (min_size == 0) return 0; + double out = 0; + const bool d_x_0 = (degree_x == 0), d_y_0 = (degree_y == 0); + const bool x_is_int = is_integer(degree_x), y_is_int = is_integer(degree_y); + for (std::size_t i = 0; i < min_size; ++i) { + double x1 = (target_x - x[i]); + double y1 = (target_y - y[i]); + if (d_x_0) + x1 = (x1 >= 0 ? 1 : 0); + else + x1 = (x1 < 0 ? 0 : x1); + if (d_y_0) + y1 = (y1 >= 0 ? 1 : 0); + else + y1 = (y1 < 0 ? 0 : y1); + if (!d_x_0) + x1 = x_is_int ? repeat_multiplication(x1, static_cast(degree_x)) + : std::pow(x1, degree_x); + if (!d_y_0) + y1 = y_is_int ? repeat_multiplication(y1, static_cast(degree_y)) + : std::pow(y1, degree_y); + out += x1 * y1; + } + return out / static_cast(max_size); +} + +double d_lpm(double degree_lpm, double degree_upm, const double* x, + const double* y, std::size_t n_x, std::size_t n_y, + double target_x, double target_y) { + check_degree(degree_lpm, "degree_lpm"); + check_degree(degree_upm, "degree_upm"); + const std::size_t max_size = (n_x > n_y ? n_x : n_y); + const std::size_t min_size = (n_x < n_y ? n_x : n_y); + if (min_size == 0) return 0; + double out = 0; + const bool dont_use_pow_lpm = is_integer(degree_lpm), + dont_use_pow_upm = is_integer(degree_upm), + d_lpm_0 = (degree_lpm == 0), d_upm_0 = (degree_upm == 0); + for (std::size_t i = 0; i < min_size; ++i) { + double x1 = (x[i] - target_x); + double y1 = (target_y - y[i]); + if (d_upm_0) + x1 = (x1 > 0 ? 1 : 0); + else + x1 = (x1 < 0 ? 0 : x1); + if (d_lpm_0) + y1 = (y1 >= 0 ? 1 : 0); + else + y1 = (y1 < 0 ? 0 : y1); + if (dont_use_pow_lpm && dont_use_pow_upm) { + if (!d_upm_0) x1 = repeat_multiplication(x1, static_cast(degree_upm)); + if (!d_lpm_0) y1 = repeat_multiplication(y1, static_cast(degree_lpm)); + out += x1 * y1; + } else if (dont_use_pow_lpm && !dont_use_pow_upm) { + if (!d_lpm_0) y1 = repeat_multiplication(y1, static_cast(degree_lpm)); + out += std::pow(x1, degree_upm) * y1; + } else if (dont_use_pow_upm && !dont_use_pow_lpm) { + if (!d_upm_0) x1 = repeat_multiplication(x1, static_cast(degree_upm)); + out += x1 * std::pow(y1, degree_lpm); + } else { + out += std::pow(x1, degree_upm) * std::pow(y1, degree_lpm); + } + } + return out / static_cast(max_size); +} + +double d_upm(double degree_lpm, double degree_upm, const double* x, + const double* y, std::size_t n_x, std::size_t n_y, + double target_x, double target_y) { + check_degree(degree_lpm, "degree_lpm"); + check_degree(degree_upm, "degree_upm"); + const std::size_t max_size = (n_x > n_y ? n_x : n_y); + const std::size_t min_size = (n_x < n_y ? n_x : n_y); + if (min_size == 0) return 0; + double out = 0; + const bool dont_use_pow_lpm = is_integer(degree_lpm), + dont_use_pow_upm = is_integer(degree_upm), + d_lpm_0 = (degree_lpm == 0), d_upm_0 = (degree_upm == 0); + for (std::size_t i = 0; i < min_size; ++i) { + double x1 = (target_x - x[i]); + double y1 = (y[i] - target_y); + if (d_lpm_0) + x1 = (x1 >= 0 ? 1 : 0); + else + x1 = (x1 < 0 ? 0 : x1); + if (d_upm_0) + y1 = (y1 > 0 ? 1 : 0); + else + y1 = (y1 < 0 ? 0 : y1); + if (dont_use_pow_lpm && dont_use_pow_upm) { + if (!d_lpm_0) x1 = repeat_multiplication(x1, static_cast(degree_lpm)); + if (!d_upm_0) y1 = repeat_multiplication(y1, static_cast(degree_upm)); + out += x1 * y1; + } else if (dont_use_pow_lpm && !dont_use_pow_upm) { + if (!d_lpm_0) x1 = repeat_multiplication(x1, static_cast(degree_lpm)); + out += x1 * std::pow(y1, degree_upm); + } else if (dont_use_pow_upm && !dont_use_pow_lpm) { + if (!d_upm_0) y1 = repeat_multiplication(y1, static_cast(degree_upm)); + out += std::pow(x1, degree_lpm) * y1; + } else { + out += std::pow(x1, degree_lpm) * std::pow(y1, degree_upm); + } + } + return out / static_cast(max_size); +} + +// --- vectorized bivariate (port of NNS_PM_TWO_VARIABLES_WORKER macro) ------ + +namespace { + +template +void two_var_vectorized(ScalarFn&& scalar, const double* target_x, + std::size_t n_tx, const double* target_y, + std::size_t n_ty, double* out, int n_threads, + std::size_t work_per_target) { + const std::size_t n_out = (n_tx > n_ty ? n_tx : n_ty); + parallel_for( + 0, n_out, + [&](std::size_t begin, std::size_t end) { + for (std::size_t i = begin; i < end; ++i) + out[i] = scalar(target_x[i % n_tx], target_y[i % n_ty]); + }, + n_threads, work_per_target); +} + +} // namespace + +void co_lpm_v(double degree_x, double degree_y, const double* x, + const double* y, std::size_t n_x, std::size_t n_y, + const double* target_x, std::size_t n_tx, const double* target_y, + std::size_t n_ty, double* out, int n_threads) { + // Validate before the parallel region: an exception must not escape a worker. + check_degree(degree_x, "degree_x"); + check_degree(degree_y, "degree_y"); + two_var_vectorized( + [&](double tx, double ty) { + return co_lpm(degree_x, degree_y, x, y, n_x, n_y, tx, ty); + }, + target_x, n_tx, target_y, n_ty, out, n_threads, std::min(n_x, n_y)); +} + +void co_upm_v(double degree_x, double degree_y, const double* x, + const double* y, std::size_t n_x, std::size_t n_y, + const double* target_x, std::size_t n_tx, const double* target_y, + std::size_t n_ty, double* out, int n_threads) { + // Validate before the parallel region: an exception must not escape a worker. + check_degree(degree_x, "degree_x"); + check_degree(degree_y, "degree_y"); + two_var_vectorized( + [&](double tx, double ty) { + return co_upm(degree_x, degree_y, x, y, n_x, n_y, tx, ty); + }, + target_x, n_tx, target_y, n_ty, out, n_threads, std::min(n_x, n_y)); +} + +void d_lpm_v(double degree_lpm, double degree_upm, const double* x, + const double* y, std::size_t n_x, std::size_t n_y, + const double* target_x, std::size_t n_tx, const double* target_y, + std::size_t n_ty, double* out, int n_threads) { + // Validate before the parallel region: an exception must not escape a worker. + check_degree(degree_lpm, "degree_lpm"); + check_degree(degree_upm, "degree_upm"); + two_var_vectorized( + [&](double tx, double ty) { + return d_lpm(degree_lpm, degree_upm, x, y, n_x, n_y, tx, ty); + }, + target_x, n_tx, target_y, n_ty, out, n_threads, std::min(n_x, n_y)); +} + +void d_upm_v(double degree_lpm, double degree_upm, const double* x, + const double* y, std::size_t n_x, std::size_t n_y, + const double* target_x, std::size_t n_tx, const double* target_y, + std::size_t n_ty, double* out, int n_threads) { + // Validate before the parallel region: an exception must not escape a worker. + check_degree(degree_lpm, "degree_lpm"); + check_degree(degree_upm, "degree_upm"); + two_var_vectorized( + [&](double tx, double ty) { + return d_upm(degree_lpm, degree_upm, x, y, n_x, n_y, tx, ty); + }, + target_x, n_tx, target_y, n_ty, out, n_threads, std::min(n_x, n_y)); +} + +// --- n-dimensional co-partial moments (ports of *_nD_cpp) ------------------ + +namespace { + +// data: column-major n x d -> element (i, j) = data[j * n + i] +inline double at(const double* m, std::size_t n, std::size_t i, + std::size_t j) { + return m[j * n + i]; +} + +void check_nd_args(std::size_t n, std::size_t d) { + if (d == 0) throw std::invalid_argument("`data` must have at least one column"); + if (n == 0) throw std::invalid_argument("`data` must have at least one row"); +} + +// Parallel row reduction: each chunk fills disjoint slots, then a serial sum. +// Mirrors R's parallelFor-into-vector + sum() pattern (same summation order +// as the upstream NumericVector accumulation). +template +double reduce_rows(std::size_t n, std::size_t d, int n_threads, RowFn&& row_value) { + std::vector out(n); + parallel_for( + 0, n, + [&](std::size_t begin, std::size_t end) { + for (std::size_t i = begin; i < end; ++i) out[i] = row_value(i); + }, + n_threads, d); + double total = 0.0; + for (std::size_t i = 0; i < n; ++i) total += out[i]; + return total; +} + +double clpm_nd_impl(const double* data, std::size_t n, std::size_t d, + const double* target, double degree, int n_threads) { + const bool deg_is_int = is_integer(degree); + if (degree == 0.0) { + const double count = reduce_rows(n, d, n_threads, [&](std::size_t i) { + for (std::size_t j = 0; j < d; ++j) + if (at(data, n, i, j) > target[j]) return 0.0; + return 1.0; + }); + return count / static_cast(n); + } + const double s = reduce_rows(n, d, n_threads, [&](std::size_t i) { + double prod = 1.0; + for (std::size_t j = 0; j < d; ++j) { + const double diff = target[j] - at(data, n, i, j); + if (diff < 0.0) return 0.0; + prod *= deg_is_int ? repeat_multiplication(diff, static_cast(degree)) + : std::pow(diff, degree); + } + return prod; + }); + return s / static_cast(n); +} + +double cupm_nd_impl(const double* data, std::size_t n, std::size_t d, + const double* target, double degree, int n_threads) { + const bool deg_is_int = is_integer(degree); + if (degree == 0.0) { + // Strictly above, as Co.UPM does: ties go to the lower side so that at + // degree 0 CLPM + CUPM + DPM partition the sample. + const double count = reduce_rows(n, d, n_threads, [&](std::size_t i) { + for (std::size_t j = 0; j < d; ++j) + if (at(data, n, i, j) <= target[j]) return 0.0; + return 1.0; + }); + return count / static_cast(n); + } + const double s = reduce_rows(n, d, n_threads, [&](std::size_t i) { + double prod = 1.0; + for (std::size_t j = 0; j < d; ++j) { + const double diff = at(data, n, i, j) - target[j]; + if (diff < 0.0) return 0.0; + prod *= deg_is_int ? repeat_multiplication(diff, static_cast(degree)) + : std::pow(diff, degree); + } + return prod; + }); + return s / static_cast(n); +} + +double dpm_nd_impl(const double* data, std::size_t n, std::size_t d, + const double* target, double degree, int n_threads) { + const bool deg_is_int = is_integer(degree); + if (degree == 0.0) { + // Discordant = neither all-lower (x <= target) nor all-upper (x > target). + const double count = reduce_rows(n, d, n_threads, [&](std::size_t i) { + bool all_below = true, all_above = true; + for (std::size_t j = 0; j < d; ++j) { + const double diff = at(data, n, i, j) - target[j]; + if (diff > 0.0) all_below = false; + if (diff <= 0.0) all_above = false; + if (!all_below && !all_above) break; + } + return (!all_below && !all_above) ? 1.0 : 0.0; + }); + return count / static_cast(n); + } + const double s = reduce_rows(n, d, n_threads, [&](std::size_t i) { + bool all_below = true, all_above = true; + for (std::size_t j = 0; j < d; ++j) { + const double diff = at(data, n, i, j) - target[j]; + if (diff >= 0.0) all_below = false; + if (diff <= 0.0) all_above = false; + if (!all_below && !all_above) break; + } + if (all_below || all_above) return 0.0; + double prod = 1.0; + for (std::size_t j = 0; j < d; ++j) { + const double abs_dev = std::abs(at(data, n, i, j) - target[j]); + prod *= deg_is_int + ? repeat_multiplication(abs_dev, static_cast(degree)) + : std::pow(abs_dev, degree); + } + return prod; + }); + return s / static_cast(n); +} + +} // namespace + +double clpm_nd(const double* data, std::size_t n, std::size_t d, + const double* target, double degree, bool norm, int n_threads) { + check_degree(degree); + check_nd_args(n, d); + const double clpm_un = clpm_nd_impl(data, n, d, target, degree, n_threads); + if (degree == 0.0 || !norm) return clpm_un; + const double cupm_un = cupm_nd_impl(data, n, d, target, degree, n_threads); + const double dpm_un = dpm_nd_impl(data, n, d, target, degree, n_threads); + const double norm_const = clpm_un + cupm_un + dpm_un; + return norm_const > 0.0 ? clpm_un / norm_const : 0.0; +} + +double cupm_nd(const double* data, std::size_t n, std::size_t d, + const double* target, double degree, bool norm, int n_threads) { + check_degree(degree); + check_nd_args(n, d); + const double cupm_un = cupm_nd_impl(data, n, d, target, degree, n_threads); + if (degree == 0.0 || !norm) return cupm_un; + const double clpm_un = clpm_nd_impl(data, n, d, target, degree, n_threads); + const double dpm_un = dpm_nd_impl(data, n, d, target, degree, n_threads); + const double norm_const = clpm_un + cupm_un + dpm_un; + return norm_const > 0.0 ? cupm_un / norm_const : 0.0; +} + +double dpm_nd(const double* data, std::size_t n, std::size_t d, + const double* target, double degree, bool norm, int n_threads) { + check_degree(degree); + check_nd_args(n, d); + const double dpm_un = dpm_nd_impl(data, n, d, target, degree, n_threads); + if (degree == 0.0 || !norm) return dpm_un; + const double clpm_un = clpm_nd_impl(data, n, d, target, degree, n_threads); + const double cupm_un = cupm_nd_impl(data, n, d, target, degree, n_threads); + const double norm_const = clpm_un + cupm_un + dpm_un; + return norm_const > 0.0 ? dpm_un / norm_const : 0.0; +} + +// --- batched nD CoLPM (port of CoLPM_nD_batch_RCPP, new in 13.0) ------------ + +void clpm_nd_batch(const double* data, std::size_t n, std::size_t d, + const double* targets, std::size_t n_targets, double degree, + bool norm, double* out, int n_threads) { + check_degree(degree); + if (n == 0) throw std::invalid_argument("`data` must have at least one row"); + const bool deg_is_int = is_integer(degree); + + parallel_for( + 0, n_targets, + [&](std::size_t begin, std::size_t end) { + for (std::size_t r = begin; r < end; ++r) { + if (degree == 0.0) { + double count = 0.0; + for (std::size_t i = 0; i < n; ++i) { + bool below_all = true; + for (std::size_t j = 0; j < d; ++j) { + if (at(data, n, i, j) > at(targets, n_targets, r, j)) { + below_all = false; + break; + } + } + if (below_all) count += 1.0; + } + out[r] = count / static_cast(n); + continue; + } + double clpm_sum = 0.0, cupm_sum = 0.0, dpm_sum = 0.0; + for (std::size_t i = 0; i < n; ++i) { + double lower_prod = 1.0, upper_prod = 1.0, dpm_prod = 1.0; + bool all_below_strict = true, all_above_strict = true; + for (std::size_t j = 0; j < d; ++j) { + const double diff = + at(data, n, i, j) - at(targets, n_targets, r, j); + lower_prod *= lower_component(-diff, degree, deg_is_int); + upper_prod *= upper_component(diff, degree, deg_is_int); + if (diff >= 0.0) all_below_strict = false; + if (diff <= 0.0) all_above_strict = false; + dpm_prod *= + deg_is_int + ? repeat_multiplication(std::abs(diff), + static_cast(degree)) + : std::pow(std::abs(diff), degree); + } + clpm_sum += lower_prod; + cupm_sum += upper_prod; + if (!(all_below_strict || all_above_strict)) dpm_sum += dpm_prod; + } + const double inv_n = 1.0 / static_cast(n); + const double clpm_un = clpm_sum * inv_n; + if (!norm) { + out[r] = clpm_un; + } else { + const double cupm_un = cupm_sum * inv_n; + const double dpm_un = dpm_sum * inv_n; + const double norm_const = clpm_un + cupm_un + dpm_un; + out[r] = norm_const > 0.0 ? clpm_un / norm_const : 0.0; + } + } + }, + n_threads, n * d); +} + +// --- PM matrix (port of the 13.0 tensorized PMMatrix_CPv) ------------------- + +PMMatrixResult pm_matrix(double degree_lpm, double degree_upm, + const double* target, const double* variable, + std::size_t n, std::size_t d, bool pop_adj, bool norm, + int n_threads) { + check_degree(degree_lpm, "degree_lpm"); + check_degree(degree_upm, "degree_upm"); + // Mirrors Rcpp::stop("variable matrix cols != target vector length") — + // bindings pass target length separately, so enforce d > 0 here and let + // the binding layer validate target length against d before calling. + PMMatrixResult res; + if (n == 0) return res; + res.dim = d; + res.cupm.assign(d * d, 0.0); + res.dupm.assign(d * d, 0.0); + res.dlpm.assign(d * d, 0.0); + res.clpm.assign(d * d, 0.0); + res.cov.assign(d * d, 0.0); + + const bool lpm_is_int = is_integer(degree_lpm); + const bool upm_is_int = is_integer(degree_upm); + + // Step 1: precompute deviation matrices once per element (column-parallel). + std::vector D_lower(n * d), D_upper(n * d); + parallel_for( + 0, d, + [&](std::size_t begin, std::size_t end) { + for (std::size_t j = begin; j < end; ++j) { + const double t_j = target[j]; + for (std::size_t i = 0; i < n; ++i) { + const double val = at(variable, n, i, j); + D_lower[j * n + i] = + lower_component(t_j - val, degree_lpm, lpm_is_int); + D_upper[j * n + i] = + upper_component(val - t_j, degree_upm, upm_is_int); + } + } + }, + n_threads, n); + + double adjust = 1.0; + if (pop_adj && n > 1) + adjust = static_cast(n) / static_cast(n - 1); + const bool apply_adj = pop_adj && n > 1 && degree_lpm > 0 && degree_upm > 0; + const double inv_rows = 1.0 / static_cast(n); + + auto M = [d](std::vector& m, std::size_t i, + std::size_t j) -> double& { return m[j * d + i]; }; + + // Step 2: fused contraction over the upper triangle, crossed DUPM/DLPM + // mirror — identical to FusedMatrixMultiplicationWorker. + parallel_for( + 0, d, + [&](std::size_t begin, std::size_t end) { + for (std::size_t i = begin; i < end; ++i) { + for (std::size_t j = i; j < d; ++j) { + double sum_cupm = 0.0, sum_clpm = 0.0, sum_dupm = 0.0, + sum_dlpm = 0.0; + const double* u_i = &D_upper[i * n]; + const double* l_i = &D_lower[i * n]; + const double* u_j = &D_upper[j * n]; + const double* l_j = &D_lower[j * n]; + for (std::size_t k = 0; k < n; ++k) { + sum_cupm += u_i[k] * u_j[k]; + sum_clpm += l_i[k] * l_j[k]; + sum_dupm += l_i[k] * u_j[k]; + sum_dlpm += u_i[k] * l_j[k]; + } + sum_cupm *= inv_rows; + sum_clpm *= inv_rows; + sum_dupm *= inv_rows; + sum_dlpm *= inv_rows; + if (apply_adj) { + sum_cupm *= adjust; + sum_clpm *= adjust; + sum_dupm *= adjust; + sum_dlpm *= adjust; + } + const double cov_ij = sum_cupm + sum_clpm - sum_dupm - sum_dlpm; + M(res.cupm, i, j) = sum_cupm; + M(res.clpm, i, j) = sum_clpm; + M(res.dupm, i, j) = sum_dupm; + M(res.dlpm, i, j) = sum_dlpm; + M(res.cov, i, j) = cov_ij; + if (j != i) { + M(res.cupm, j, i) = sum_cupm; + M(res.clpm, j, i) = sum_clpm; + M(res.dupm, j, i) = sum_dlpm; // crossed mirror + M(res.dlpm, j, i) = sum_dupm; // crossed mirror + M(res.cov, j, i) = cov_ij; + } + } + } + }, + n_threads, (d + 1) * n / 2 + 1); // row i contracts d - i column pairs + + // Step 3: cellular normalization, preserving the crossed mirror. + if (norm) { + for (std::size_t i = 0; i < d; ++i) { + for (std::size_t j = i; j < d; ++j) { + double cupm_ij = M(res.cupm, i, j); + double dupm_ij = M(res.dupm, i, j); + double dlpm_ij = M(res.dlpm, i, j); + double clpm_ij = M(res.clpm, i, j); + const double total = cupm_ij + dupm_ij + dlpm_ij + clpm_ij; + if (total > 0.0) { + cupm_ij /= total; + dupm_ij /= total; + dlpm_ij /= total; + clpm_ij /= total; + } else { + cupm_ij = dupm_ij = dlpm_ij = clpm_ij = 0.0; + } + const double cov_ij = cupm_ij + clpm_ij - dupm_ij - dlpm_ij; + M(res.cupm, i, j) = cupm_ij; + M(res.clpm, i, j) = clpm_ij; + M(res.dupm, i, j) = dupm_ij; + M(res.dlpm, i, j) = dlpm_ij; + M(res.cov, i, j) = cov_ij; + if (j != i) { + M(res.cupm, j, i) = cupm_ij; + M(res.clpm, j, i) = clpm_ij; + M(res.dupm, j, i) = dlpm_ij; // crossed mirror after norm too + M(res.dlpm, j, i) = dupm_ij; + M(res.cov, j, i) = cov_ij; + } + } + } + } + return res; +} + +} // namespace nns diff --git a/sync/nns_source.json b/sync/nns_source.json index feacd961..90360f18 100644 --- a/sync/nns_source.json +++ b/sync/nns_source.json @@ -45,6 +45,15 @@ "adaptation": "The vendored NNS-core multivariate prediction kernel (src/mreg_predict.cpp, sha256 c98d3fbb93c3165109870cb3d2cc1a55c8cb0fa35770b3ff0204441fd20e1e04) with its Student-t and normal ensemble weights evaluated by R's own nmath dt and dnorm4 (src/nns/_rnmath.hpp) instead of the vendor's lgamma-based closed form, so the weights and predictions match R's NNS_mreg_predict_v2_cpp bit for bit. CMake replaces only the vendor translation unit.", "verified_r_commit": "9c8bd21a77b377cb49d43fececc662d62b00a94e" }, + { + "path": "src/nns/_r14_partial_moments.cpp", + "sha256": "2b294085d9a736ed1818bff78bc081947306b9768db2b8d72d913f09406fa863", + "r_source_path": "src/partial_moments.cpp", + "r_source_sha256": "21101bdeb7309070cfa5999ab62a893025a5251cd57469552cba35a8c693d445", + "r_source_commit": "9c8bd21a77b377cb49d43fececc662d62b00a94e", + "adaptation": "The vendored NNS-core partial-moment kernels (src/partial_moments.cpp, sha256 8786f5df1fcd8e29c6ea28d91903fbdbaa93ed93a803d924422a89a19e23bdb0) with d_upm's two mixed-degree branches matched to R's DUPM_C: when exactly one degree is an integer, repeated multiplication goes to that degree's side and pow to the other. The vendor had them swapped, so native D.UPM ignored the fractional degree. CMake replaces only the vendor translation unit.", + "verified_r_commit": "9c8bd21a77b377cb49d43fececc662d62b00a94e" + }, { "path": "src/nns/_r14_seas.cpp", "sha256": "2b956af7f064a575d1acd1283ec5a6f4ced8e91b227e294634ce3ab7fd83420e", @@ -58,7 +67,7 @@ "r_cache_summary": { "nns_version": "14.0", "shards": 149, - "entries": 2999, + "entries": 3004, "generation": "Pinned live R NNS 14.0 under R 4.5.0 (x86-64 Linux, reference BLAS/LAPACK) only; no Python-authored expected values." }, "r_nmath_ports": [ diff --git a/tests/_r_cache/pm14.co_moments.json b/tests/_r_cache/pm14.co_moments.json index 9b0e776d..ecc56d3b 100644 --- a/tests/_r_cache/pm14.co_moments.json +++ b/tests/_r_cache/pm14.co_moments.json @@ -26,6 +26,32 @@ 0.8529608826778399 ] }, + "43b150f4eb96a250580c96ea0904b436bf7db3fdf0101ad40969b77c76725363": { + "clpm": [ + 0.008732933411513353, + 4.459861005369236e-05, + 0.0, + 0.06715507695894372 + ], + "cupm": [ + 1.8690589137333462, + 2.9688685530277814, + 0.8158763424995649, + 1.2880358641677538 + ], + "dlpm": [ + 1.7784130956558163e-05, + 0.48098421670656116, + 18.371826198337732, + 0.0003097466738247113 + ], + "dupm": [ + 1.182062035988315, + 0.15067213365192414, + 4.5436206465996924e-05, + 1.1856102250340825 + ] + }, "544bfcda75ccca613a3fe23d001da9df7c1df4329d8cd59054807e47695f8dc3": { "clpm": [ 0.016936198826809357, @@ -52,6 +78,84 @@ 0.9724393761905308 ] }, + "b4ebaa6a20b5f421f3b029c3f711b329b4e671de10c60afe25f8ca1a62625dbe": { + "clpm": [ + 0.04221908198747999, + 0.006649764927319831, + 0.0, + 0.18248108730427992 + ], + "cupm": [ + 0.4698865804796724, + 0.2700608485637695, + 0.06370527639070526, + 0.34999884161522943 + ], + "dlpm": [ + 0.013158490901839157, + 0.6278060791547678, + 1.3002738998941497, + 0.011352561822610529 + ], + "dupm": [ + 0.4545052113512802, + 0.16071902262047263, + 0.00589220988023026, + 0.30514851631065876 + ] + }, + "b6c1c0db6b8c8ef197ce2befad9592f900b747ece4b4f1bf3cfd392f975d7e79": { + "clpm": [ + 0.009452148518111011, + 0.00011927462236230367, + 0.0, + 0.06360746994487715 + ], + "cupm": [ + 3.007164511933813, + 3.39094624435647, + 0.4856325182276167, + 2.195068762679768 + ], + "dlpm": [ + 2.3796606056003564e-05, + 0.5729705132571268, + 13.049439697821999, + 0.00020053063275026652 + ], + "dupm": [ + 1.4854771507781988, + 0.2973333437406137, + 9.87161938502122e-05, + 1.3106443627990423 + ] + }, + "d6d4956f67f2e9b671b4519fb61fc827b4e7c42f656b59e718144051dc0553dc": { + "clpm": [ + 0.01902439844217805, + 0.0008905886819986904, + 0.0, + 0.09615169561134661 + ], + "cupm": [ + 0.977780662175471, + 0.8631776087098678, + 0.17029238536997762, + 0.7467962952800206 + ], + "dlpm": [ + 0.0005533593826607962, + 0.5715327658420989, + 4.027125513332004, + 0.0014318069297456443 + ], + "dupm": [ + 0.6785482878051876, + 0.17991950033146595, + 0.0007626641021727362, + 0.5357427942608763 + ] + }, "ddc472b920b5793d95a60299439f3d2c1267dcbde32fa3d3576aaf721d676cdc": { "clpm": [ 0.003284243649347838, @@ -77,6 +181,32 @@ 6.607013638004929e-07, 4.255326430325899 ] + }, + "f5e3f747bc1b07b18f9428726840cd3a8f060ceea68eeb828f609147ae0d9bf0": { + "clpm": [ + 0.009792971945914311, + 1.667610409764698e-05, + 0.0, + 0.08408210618662464 + ], + "cupm": [ + 1.1766547241730678, + 2.7517583287713685, + 1.442229724844584, + 0.7621131907905048 + ], + "dlpm": [ + 2.9978716716942153e-05, + 0.44915704001637924, + 27.114182234776557, + 0.0005987749488019435 + ], + "dupm": [ + 0.9878647591876922, + 0.07670274603273747, + 2.0912970582650386e-05, + 1.1715593357380694 + ] } }, "label": "pm14.co_moments", diff --git a/tests/invariants/test_r14_shared_paths.py b/tests/invariants/test_r14_shared_paths.py index 3b4eee51..c0438a2e 100644 --- a/tests/invariants/test_r14_shared_paths.py +++ b/tests/invariants/test_r14_shared_paths.py @@ -90,7 +90,7 @@ def test_native_r14_overlays_have_separate_source_provenance() -> None: root = Path(__file__).resolve().parents[2] manifest = json.loads((root / "sync/nns_source.json").read_text()) assert manifest["r_version"] == "14.0" - assert len(manifest["r14_native_overlays"]) == 4 + assert len(manifest["r14_native_overlays"]) == 5 for overlay in manifest["r14_native_overlays"]: # Each overlay names the R commit its source was ported from and the # pinned oracle it was verified against bit for bit. diff --git a/tests/parity/test_partial_moments_bits_r14.py b/tests/parity/test_partial_moments_bits_r14.py index feba4acc..c46c5d77 100644 --- a/tests/parity/test_partial_moments_bits_r14.py +++ b/tests/parity/test_partial_moments_bits_r14.py @@ -21,6 +21,7 @@ from _r import nns_frame_call, r_column import nns +from nns._native import native_fn pytestmark = pytest.mark.parity @@ -48,7 +49,11 @@ def test_nns_moments_match_r_bitwise(population: bool, flag: str) -> None: @pytest.mark.parametrize("native", [True, False]) -@pytest.mark.parametrize(("degree_x", "degree_y"), [(3, 1), (0.5, 1.5), (2, 0)]) +# Exactly one integer degree takes a separate branch in R's D.LPM/D.UPM kernels. +@pytest.mark.parametrize( + ("degree_x", "degree_y"), + [(3, 1), (0.5, 1.5), (2, 0), (1, 1.5), (1.5, 1), (2, 0.5), (0.5, 1), (0, 0.5)], +) def test_bivariate_co_moments_match_r_bitwise( monkeypatch: pytest.MonkeyPatch, native: bool, degree_x: float, degree_y: float ) -> None: @@ -75,6 +80,10 @@ def test_bivariate_co_moments_match_r_bitwise( } for key, value in expected.items(): np.testing.assert_array_equal(np.atleast_1d(actual[key]), np.atleast_1d(value), key) + if native and native_fn("d_upm") is not None: + args = (degree_x, degree_y, x, y, tx[0], ty[0]) + assert native_fn("d_lpm")(*args) == expected["dlpm"][0] + assert native_fn("d_upm")(*args) == expected["dupm"][0] @pytest.mark.parametrize("degree", [2, 0.5, 3])