From b794fbfd1de47db6ffbd6d16edb320a1e4e56a56 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 03:03:19 +0000 Subject: [PATCH 01/16] Reproduce R's own arithmetic where NNS makes exact comparisons Python agreed with R 14.0 to ~1e-15 but not bit for bit in several intermediates that NNS then compares exactly (findInterval on regression points built from the same index, class-probability ties, rounded cutoffs, a rounded random holdout share). A last-bit difference there moved boundary rows into the neighbouring cell (NNS.ID, gradient) or could change a selected cutoff. Each helper now follows R's operation order, verified bit for bit against the pinned oracle under R 4.5.0: - NNS.dep: R::dhyper and R::pnorm are ports of R's nmath routines (Loader's saddle point, Cody) in the native overlay and the fallback, replacing long-double binomials and erfc; the fallback also uses the C library lgamma that R's C++ calls (CPython's math.lgamma differs) and R's p_constant/local-weight operation order. - NNS.caus weights: R's mean(), LPM_C/UPM_C sums and sum() order. - Dimension reduction: x.star and point.star as reference-BLAS dgemv, class blends as crossprod (dgemv 'T'), Spearman weights through R's cov.c arithmetic, "all" through rowMeans' long-double accumulation. - Shared helpers in nns._rmean: R's sum, cor (cov_na_1/cov_na_2), colMeans/rowMeans, rank, quantile types 7 and 8, round (fround), seq(by) and approx(ties = "ordered"); used by LPM/UPM.VaR at degree 0, regression intervals, smoothing tolerance, R2/SE, univariate and 1-D class prediction, NNS.norm, Stack's threshold/weight grids and holdout share, and VAR's rounded correlation cutoffs. NNS.reg contracts aligned with R/Regression.R: a class response is classified even with multivariate.call = TRUE; threshold = NULL/NA means no screening and n.best = NA the default (values NNS.stack reports can be passed back); a vector threshold is rejected for regression with R's message; per-class thresholds follow R's name and position rules; the dimension-reduction classifier reports n.best = NA and integer row ids. Tests: nns_frame_call(exact = TRUE) stores 17 significant digits so parity checks can require exact doubles; new R-authored shards cover each helper, NNS.dep (native and fallback), the dimension-reduction weights and a boundary-row fit, NNS.norm and the NNS.reg contracts. The NNS.norm property now requires columns that actually vary: a constant column is NA in R (and now in Python) although np.std reports a tiny spread for it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt --- src/nns/_caus_r14.py | 30 +- src/nns/_class_r14.py | 54 +- src/nns/_dep_r14.py | 29 +- src/nns/_r14_dependence.hpp | 22 +- src/nns/_rblas.py | 45 + src/nns/_reg_engine.py | 120 +- src/nns/_rmean.py | 345 +++++ src/nns/_rnmath.hpp | 234 +++ src/nns/_rnmath.py | 303 ++++ src/nns/_stack_class_r14.py | 3 +- src/nns/norm.py | 29 +- src/nns/stack.py | 90 +- src/nns/var.py | 16 +- sync/nns_source.json | 23 +- tests/_r.py | 16 +- tests/_r_cache/rarith.blas.json | 73 + tests/_r_cache/rarith.cor.json | 43 + tests/_r_cache/rarith.dep.json | 63 + tests/_r_cache/rarith.dhyper.json | 612 ++++++++ tests/_r_cache/rarith.dimred.fit.json | 609 ++++++++ tests/_r_cache/rarith.dimred.json | 47 + tests/_r_cache/rarith.norm.constant.json | 25 + tests/_r_cache/rarith.norm.json | 191 +++ tests/_r_cache/rarith.pnorm.json | 620 ++++++++ tests/_r_cache/rarith.quantile7.json | 91 ++ tests/_r_cache/rarith.quantile8.json | 91 ++ tests/_r_cache/rarith.round-1.json | 1509 +++++++++++++++++++ tests/_r_cache/rarith.round1.json | 1509 +++++++++++++++++++ tests/_r_cache/rarith.round12.json | 1509 +++++++++++++++++++ tests/_r_cache/rarith.round2.json | 1509 +++++++++++++++++++ tests/_r_cache/rarith.round3.json | 1509 +++++++++++++++++++ tests/_r_cache/rarith.round6.json | 1509 +++++++++++++++++++ tests/_r_cache/rarith.seq.json | 270 ++++ tests/_r_cache/rarith.summaries.json | 273 ++++ tests/_r_cache/reg14.dimred.class.meta.json | 72 + tests/_r_cache/reg14.mvcall.class.json | 41 + tests/_r_cache/reg14.nbest.na.json | 14 + tests/_r_cache/reg14.threshold.class.json | 28 + tests/_r_cache/reg14.threshold.na.json | 35 + tests/parity/test_r_arithmetic.py | 285 ++++ tests/parity/test_reg_r14_contracts.py | 115 ++ tests/property/test_norm.py | 6 +- 42 files changed, 13842 insertions(+), 175 deletions(-) create mode 100644 src/nns/_rblas.py create mode 100644 src/nns/_rnmath.hpp create mode 100644 src/nns/_rnmath.py create mode 100644 tests/_r_cache/rarith.blas.json create mode 100644 tests/_r_cache/rarith.cor.json create mode 100644 tests/_r_cache/rarith.dep.json create mode 100644 tests/_r_cache/rarith.dhyper.json create mode 100644 tests/_r_cache/rarith.dimred.fit.json create mode 100644 tests/_r_cache/rarith.dimred.json create mode 100644 tests/_r_cache/rarith.norm.constant.json create mode 100644 tests/_r_cache/rarith.norm.json create mode 100644 tests/_r_cache/rarith.pnorm.json create mode 100644 tests/_r_cache/rarith.quantile7.json create mode 100644 tests/_r_cache/rarith.quantile8.json create mode 100644 tests/_r_cache/rarith.round-1.json create mode 100644 tests/_r_cache/rarith.round1.json create mode 100644 tests/_r_cache/rarith.round12.json create mode 100644 tests/_r_cache/rarith.round2.json create mode 100644 tests/_r_cache/rarith.round3.json create mode 100644 tests/_r_cache/rarith.round6.json create mode 100644 tests/_r_cache/rarith.seq.json create mode 100644 tests/_r_cache/rarith.summaries.json create mode 100644 tests/_r_cache/reg14.dimred.class.meta.json create mode 100644 tests/_r_cache/reg14.mvcall.class.json create mode 100644 tests/_r_cache/reg14.nbest.na.json create mode 100644 tests/_r_cache/reg14.threshold.class.json create mode 100644 tests/_r_cache/reg14.threshold.na.json create mode 100644 tests/parity/test_r_arithmetic.py create mode 100644 tests/parity/test_reg_r14_contracts.py diff --git a/src/nns/_caus_r14.py b/src/nns/_caus_r14.py index 8f054a64..49be952f 100644 --- a/src/nns/_caus_r14.py +++ b/src/nns/_caus_r14.py @@ -13,6 +13,7 @@ from numpy.typing import NDArray from nns._dep_r14 import dep_levels_r14 +from nns._rmean import r_mean, r_sum Array = NDArray[np.float64] @@ -24,14 +25,21 @@ def _ranks(v: Array) -> Array: def _variance(v: Array) -> float: - """R (LPM(2,mean,v) + UPM(2,mean,v)) * m/(m-1), length >=2.""" + """R .nns_caus_dev: (LPM(2, mean(v), v) + UPM(2, mean(v), v)) * m / (m - 1). + + Evaluated as R does: R's mean(), then each partial moment as LPM_C/UPM_C + accumulate it (sequential double sums of the squared deviations on its + side of the mean, divided by m), then the factor applied left to right. + """ m = int(v.size) if m < 2: return float("nan") - # Population 2nd partial moments about the *same* sample mean. - mean = float(np.mean(v)) - centered = v - mean - return float(np.mean(centered * centered) * (m / (m - 1))) + mu = r_mean(v) + below = mu - v + above = v - mu + lpm = float(np.cumsum(np.where(below >= 0.0, below * below, 0.0))[-1]) / m + upm = float(np.cumsum(np.where(above > 0.0, above * above, 0.0))[-1]) / m + return (lpm + upm) * m / (m - 1) def _explained(cause_u: Array, effect_u: Array) -> float: @@ -43,23 +51,25 @@ def _explained(cause_u: Array, effect_u: Array) -> float: total = _variance(effect_u) if not math.isfinite(total) or total <= 0.0: return 0.0 - numerator, denominator = 0.0, 0 - for group in np.unique(groups): + weighted: list[float] = [] + denominator = 0 + for group in np.unique(groups): # split(): cells in increasing order sample = effect_u[groups == group] d = _variance(sample) if math.isfinite(d): - numerator += sample.size * d + weighted.append(sample.size * d) denominator += sample.size if denominator == 0: return 0.0 - within = numerator / denominator + # R: sum(w[ok] * d[ok]) / sum(w[ok]), with sum()'s long-double accumulator. + within = r_sum(np.asarray(weighted, dtype=np.float64)) / denominator return min(1.0, max(0.0, 1.0 - within / total)) def _rho(cause_u: Array, effect_u: Array) -> float: """R .nns_caus_rho: average, not maximum, of unfloored NNS.dep levels.""" try: - value = float(np.mean(dep_levels_r14(cause_u, effect_u))) + value = r_mean(np.asarray(dep_levels_r14(cause_u, effect_u), dtype=np.float64)) return value if math.isfinite(value) else 0.0 except (ValueError, ArithmeticError, OverflowError): # Mirrors R's tryCatch(error=function(e) 0). diff --git a/src/nns/_class_r14.py b/src/nns/_class_r14.py index bf080994..ecb47f4a 100644 --- a/src/nns/_class_r14.py +++ b/src/nns/_class_r14.py @@ -13,6 +13,9 @@ import numpy as np from numpy.typing import NDArray +from nns._rblas import r_crossprod_vec +from nns._rmean import r_approx, r_tie_means + F = NDArray[np.float64] @@ -128,15 +131,16 @@ def predict(query: F | None) -> F | None: if xx.size == 1: out = np.tile(shares[sorted_idx[0]], (q.shape[0], 1)) else: - # R stats::approx computes the fractional position before - # multiplying the ordinate difference. np.interp precomputes - # a slope instead, which can turn an exact probability tie - # into a one-ULP preference and change the selected class. - at = np.clip(q[:, 0], xx[0], xx[-1]) - lo = np.clip(np.searchsorted(xx, at, side="right") - 1, 0, xx.size - 2) - fraction = (at - xx[lo]) / (xx[lo + 1] - xx[lo]) + # R: approx(centres[o, 1], shares[o, j], xout, rule = 2, + # ties = mean)$y per class, with R's approx1 arithmetic (the + # fraction first, exact knot ordinates), so an exact + # probability tie is not turned into a one-ULP preference. yy = shares[sorted_idx] - out = yy[lo] + fraction[:, None] * (yy[lo + 1] - yy[lo]) + columns = [] + for col in range(yy.shape[1]): + kx, ky = r_tie_means(xx, yy[:, col]) + columns.append(r_approx(kx, ky, q[:, 0], ky[0], ky[-1])) + out = np.column_stack(columns) return _normalize(out, prior) ds = _mreg_distances(centres, q, dist, mins, maxs) out = np.empty((q.shape[0], shares.shape[1]), dtype=np.float64) @@ -158,7 +162,8 @@ def predict(query: F | None) -> F | None: same = chosen_d == value if np.count_nonzero(same) > 1: w[same] = np.mean(w[same]) - out[i] = np.dot(w, shares[chosen]) + # R: as.numeric(crossprod(weights, shares[o[seq_len(m)], ])). + out[i] = r_crossprod_vec(w, shares[chosen]) return _normalize(out, prior) return { @@ -188,7 +193,7 @@ def classify( dim_count: int | None = None, ) -> dict[str, Any]: """R .nns_reg_classify: return nominal posterior probabilities and sets.""" - from nns._reg_engine import _dimred_coefficients, _dimreduce + from nns._reg_engine import _dimred_coefficients, _dimreduce, _validate_threshold design_x = np.asarray(encoded["x"], dtype=np.float64) query_x = encoded["point_est"] @@ -212,14 +217,25 @@ def classify( equations = None if dim_red_method is not None and design_x.shape[1] > 1: - if isinstance(threshold, (int, float)): - threshold_values = np.full(values.size, float(threshold)) + # R: one cutoff for every class index, or one per class (named by + # class, or in class order), as NNS.stack reports for classification. + cutoffs = _validate_threshold(threshold) + per_class = "[threshold] must be one number, or one cutoff per class." + if isinstance(cutoffs, dict): + if len(cutoffs) <= 1: + threshold_values = np.full(values.size, next(iter(cutoffs.values()), 0.0)) + elif all(str(label) in cutoffs for label in labels): + threshold_values = np.array([cutoffs[str(label)] for label in labels]) + elif len(cutoffs) == values.size: + threshold_values = np.array(list(cutoffs.values()), dtype=np.float64) + else: + raise ValueError(per_class) + elif isinstance(cutoffs, float): + threshold_values = np.full(values.size, cutoffs) else: - threshold_values = np.asarray(threshold, dtype=np.float64) - if threshold_values.size not in (1, values.size): - raise ValueError("[threshold] must be one number, or one cutoff per class.") - if threshold_values.size == 1: - threshold_values = np.full(values.size, float(threshold_values[0])) + threshold_values = np.asarray(cutoffs, dtype=np.float64) + if threshold_values.size != values.size: + raise ValueError(per_class) fitted_cols, point_cols, equations = [], [], {} for c in range(values.size): coefficients = _dimred_coefficients( @@ -272,8 +288,8 @@ def classify( None, None, None, - [str(i + 1) for i in range(design_x.shape[0])], - None, + np.arange(1, design_x.shape[0] + 1, dtype=np.int64), # R: seq_len(nrow(X)) + float("nan"), # R: k <- NA_integer_ ) else: fit = _class_base( diff --git a/src/nns/_dep_r14.py b/src/nns/_dep_r14.py index d9b9b300..fc80c1b5 100644 --- a/src/nns/_dep_r14.py +++ b/src/nns/_dep_r14.py @@ -15,6 +15,8 @@ import numpy as np from numpy.typing import NDArray +from nns._rnmath import clgamma, dhyper, pnorm + FloatArray: TypeAlias = NDArray[np.float64] _EPS = sys.float_info.epsilon _SQRT_2_PI = math.sqrt(2.0 / math.pi) @@ -44,11 +46,11 @@ def _null_t0(m: int, a: int, b: int) -> float: if sd <= 0.0: return abs(mu) z = mu / sd - return sd * _SQRT_2_PI * math.exp(-0.5 * z * z) + mu * math.erf(z / math.sqrt(2.0)) + return sd * _SQRT_2_PI * math.exp(-0.5 * z * z) + mu * (1.0 - 2.0 * pnorm(-z)) - # Exact integer combinatorics to establish the first probability, followed - # by the same floating-point ratio recurrence as the R/C++ oracle. - pk = math.comb(a, k_lo) * math.comb(m - a, b - k_lo) / math.comb(m, b) + # R::dhyper for the first probability (Loader's saddle point, as R + # evaluates it), followed by the R/C++ oracle's ratio recurrence. + pk = dhyper(float(k_lo), float(a), float(m - a), float(b)) e = 0.0 for k in range(k_lo, k_hi + 1): conc = float(m - a - b + 2 * k) @@ -60,9 +62,9 @@ def _null_t0(m: int, a: int, b: int) -> float: def _log_arrangements(values: FloatArray) -> float: _, multiplicity = np.unique(values, return_counts=True) - out = math.lgamma(float(values.size) + 1.0) + out = clgamma(float(values.size) + 1.0) for count in multiplicity: - out -= math.lgamma(float(count) + 1.0) + out -= clgamma(float(count) + 1.0) return out @@ -183,9 +185,16 @@ def _between_sign(x: FloatArray, y: FloatArray) -> float: def _p_constant(m: int, counts: list[int], n: int, cache: dict[int, float]) -> float: + """R p_constant: chance that a cell of m draws from v is constant (lgamma form).""" if m not in cache: - denom = math.comb(n, m) - p = sum(math.comb(c, m) / denom for c in counts if c >= m) + lchoose_n = clgamma(n + 1.0) - clgamma(m + 1.0) - clgamma(n - m + 1.0) + p = 0.0 + for c in counts: # largest multiplicity first, as in R + if c < m: + break + p += math.exp( + clgamma(c + 1.0) - clgamma(m + 1.0) - clgamma(c - m + 1.0) - lchoose_n + ) cache[m] = min(1.0, p) return cache[m] @@ -343,7 +352,7 @@ def score(uu: FloatArray, vv: FloatArray) -> float: return 1.0 if kind == 1: return 0.0 - return math.copysign(math.sqrt(abs(magnitude)), magnitude) + return (1.0 if magnitude >= 0.0 else -1.0) * math.sqrt(abs(magnitude)) global_score = score(u, v) if not math.isfinite(global_score): @@ -359,7 +368,7 @@ def score(uu: FloatArray, vv: FloatArray) -> float: value = score(u[cell_ids], v[cell_ids]) if not math.isfinite(value): value = global_score - dep += value * (float(hi - lo) / float(n)) + dep += value * float(hi - lo) / float(n) # R: val * m / n, left to right lo = hi return max(0.0, dep) diff --git a/src/nns/_r14_dependence.hpp b/src/nns/_r14_dependence.hpp index e4f5ab64..53a189f3 100644 --- a/src/nns/_r14_dependence.hpp +++ b/src/nns/_r14_dependence.hpp @@ -1,11 +1,12 @@ -// R14 multilevel dependence with explicit non-R numerical adapters. +// R14 multilevel dependence. // Source: OVVO-Financial/NNS@7b07a837c130ba7220dbe16c584221508757d5ae // src/NNS_dep.cpp: null corrections, constant-cell rules and level selection. -// Rcpp marshalling uses vectors, normal probabilities use erfc, and the -// initial hypergeometric mass uses long-double binomial products. The -// remaining selection/null-correction arithmetic follows the pinned source. +// Rcpp marshalling uses vectors. R::dhyper and R::pnorm are the ports of R's +// own nmath routines in _rnmath.hpp, so the null expectations match R's to +// the last bit; the remaining arithmetic follows the pinned source. // SPDX-License-Identifier: GPL-3.0-only #pragma once +#include "_rnmath.hpp" #include #include #include @@ -72,18 +73,11 @@ static double null_T0(int m, int a, int b) { if (sd <= 0.0) return std::abs(mu); const double z = mu / sd; return sd * std::sqrt(2.0 / 3.14159265358979323846264338327950288) * std::exp(-0.5 * z * z) + - mu * (1.0 - 2.0 * (0.5 * std::erfc(z / std::sqrt(2.0)))); + mu * (1.0 - 2.0 * nns_rmath::pnorm(-z)); } // Hypergeometric probabilities by the ratio recurrence from the lowest count. - // The first hypergeometric mass, evaluated as long-double binomial - // products; the following double recurrence is the pinned R14 one. - const auto choose = [](int n, int k) { - k = std::min(k, n - k); - long double out = 1.0L; - for (int j = 1; j <= k; ++j) out = out * (n - k + j) / j; - return out; - }; - double pk = static_cast(choose(a, k_lo) * choose(m - a, b - k_lo) / choose(m, b)); + double pk = nns_rmath::dhyper(static_cast(k_lo), static_cast(a), + static_cast(m - a), static_cast(b)); double e = 0.0; for (int k = k_lo; k <= k_hi; ++k) { const double conc = static_cast(m - a - b + 2 * k); diff --git a/src/nns/_rblas.py b/src/nns/_rblas.py new file mode 100644 index 00000000..2851127f --- /dev/null +++ b/src/nns/_rblas.py @@ -0,0 +1,45 @@ +"""R's reference-BLAS accumulation order for the small products NNS forms. + +R evaluates ``A %*% v`` and ``crossprod(w, B)`` with the reference BLAS +``dgemv`` it ships (``src/extra/blas/blas.f``): every output element is +accumulated strictly left to right, one rounded multiply and one rounded add +per term, with no fused multiply-add. NumPy's ``@`` and ``np.dot`` call an +optimised BLAS whose blocked, vectorised partial sums can differ from that in +the last bit. NNS feeds several of these products into exact comparisons +(``findInterval`` on regression points built from the same index, tied class +probabilities), where that bit decides the result, so the port reproduces the +reference order. +""" + +from __future__ import annotations + +import numpy as np +from numpy.typing import NDArray + + +def r_matvec(design: NDArray[np.float64], coef: NDArray[np.float64]) -> NDArray[np.float64]: + """``design %*% coef``: reference ``dgemv('N')``, accumulated column by column.""" + design = np.asarray(design, dtype=np.float64) + coef = np.asarray(coef, dtype=np.float64).reshape(-1) + out = np.zeros(design.shape[0], dtype=np.float64) + for j in np.flatnonzero(coef != 0.0): + out = out + coef[j] * design[:, j] + return out + + +def r_crossprod_vec( + weights: NDArray[np.float64], values: NDArray[np.float64] +) -> NDArray[np.float64]: + """``as.numeric(crossprod(weights, values))`` for a weight vector. + + R computes it as reference ``dgemv('T')``: one sequential dot product of + ``weights`` with each column of ``values``. + """ + weights = np.asarray(weights, dtype=np.float64).reshape(-1) + values = np.asarray(values, dtype=np.float64) + if values.ndim == 1: + values = values.reshape(-1, 1) + if weights.size == 0: + return np.zeros(values.shape[1], dtype=np.float64) + products = values * weights[:, np.newaxis] + return np.asarray(np.cumsum(products, axis=0)[-1], dtype=np.float64) diff --git a/src/nns/_reg_engine.py b/src/nns/_reg_engine.py index 46732476..f48f9a47 100644 --- a/src/nns/_reg_engine.py +++ b/src/nns/_reg_engine.py @@ -48,7 +48,16 @@ split_columns, ) from nns._native import native_fn -from nns._rmean import r_mean +from nns._rblas import r_matvec +from nns._rmean import ( + r_approx, + r_cor, + r_mean, + r_quantile, + r_rank_average, + r_row_means, + r_sum, +) from nns.central_tendencies import nns_gravity, nns_mode from nns.dependence import nns_dep @@ -87,7 +96,10 @@ def _validate_order(order: Any) -> Order: def _validate_nbest(n_best: Any) -> NBest: - if n_best is None: + # NA (as NNS.stack reports for a single predictor) selects the default. + if n_best is None or ( + isinstance(n_best, (float, np.floating)) and math.isnan(float(n_best)) + ): return None if isinstance(n_best, str): if n_best.lower() == "all": @@ -101,6 +113,46 @@ def _validate_nbest(n_best: Any) -> NBest: return int(value) +def _validate_threshold(threshold: Any) -> float | NDArray[np.float64] | dict[str, float]: + """R ``.nns_reg_validate_threshold``: NULL or NA means no screening (0). + + A scalar stays a float. A vector (one cutoff per class, as ``NNS.stack`` + reports for classification) becomes an array with NA entries set to 0; a + mapping keyed by class label is R's named vector. Every cutoff must be + finite and nonnegative. + """ + message = "[threshold] must be NULL, NA, or finite nonnegative numbers." + + def one(value: Any) -> float: + if value is None: + return 0.0 + if isinstance(value, (bool, np.bool_)) or not isinstance( + value, (int, float, np.integer, np.floating) + ): + raise ValueError(message) + out = float(value) + if math.isnan(out): + return 0.0 + if not math.isfinite(out) or out < 0: + raise ValueError(message) + return out + + if threshold is None: + return 0.0 + if isinstance(threshold, dict): + if not threshold: + return 0.0 + return {str(key): one(value) for key, value in threshold.items()} + if isinstance(threshold, (list, tuple, np.ndarray)): + values = list(np.asarray(threshold, dtype=object).reshape(-1)) + if not values: + return 0.0 + if len(values) == 1: + return one(values[0]) + return np.array([one(value) for value in values], dtype=np.float64) + return one(threshold) + + def _validate_dist(dist: Any) -> str: if dist is None: return "NNS" @@ -663,13 +715,14 @@ def _smooth_spline_predictor(rx: NDArray[np.float64], ry: NDArray[np.float64], s x_all = np.asarray(rx, dtype=np.float64) y_all = np.asarray(ry, dtype=np.float64) - q25, q75 = np.quantile(x_all, [0.25, 0.75]) + # R smooth.spline: tol = 1e-6 * IQR(x) (type-7 quartiles). + q25, q75 = r_quantile(x_all, [0.25, 0.75], 7) tol = 1e-6 * float(q75 - q25) if not math.isfinite(tol) or tol <= 0.0: return None # R: xx <- round((x - mean(x)) / tol); one group per distinct rounded value, # represented by its smallest x, with the group mean of y and weight = size. - rounded = np.round((x_all - float(np.mean(x_all))) / tol) + rounded = np.rint((x_all - r_mean(x_all)) / tol) order = np.argsort(x_all, kind="mergesort") xs = x_all[order] ys = y_all[order] @@ -762,9 +815,10 @@ def _predict_univariate( pred = np.asarray(smooth_fit(xout), dtype=np.float64) elif smooth and rx.size >= 4 and not is_class: # Degenerate fit (e.g. < 4 unique points): fall back to interpolation. - pred = np.interp(xout, rx, ry) + pred = r_approx(rx, ry, xout, ry[0], ry[-1]) else: - pred = np.interp(xout, rx, ry) + # R: approx(rp$x, rp$y, xout, method = "linear", rule = 2, ties = "ordered"). + pred = r_approx(rx, ry, xout, ry[0], ry[-1]) left = xout < rx[0] right = xout > rx[-1] if np.any(left): @@ -785,12 +839,13 @@ def _predict_univariate( def _r2(actual: NDArray[np.float64], predicted: NDArray[np.float64]) -> float: """Racine-Hastie within-sample goodness-of-fit, bounded in [0, 1].""" - actual_centered = actual - float(np.mean(actual)) - predicted_centered = predicted - float(np.mean(predicted)) - denominator = float(np.sum(actual_centered**2) * np.sum(predicted_centered**2)) + # R's mean() and sum() (long-double accumulation), as .nns_reg_r2 uses. + actual_centered = actual - r_mean(actual) + predicted_centered = predicted - r_mean(predicted) + denominator = r_sum(actual_centered**2) * r_sum(predicted_centered**2) if denominator == 0.0: return 1.0 if _r_all_equal(actual, predicted) else 0.0 - numerator = float(np.sum(actual_centered * predicted_centered) ** 2) + numerator = r_sum(actual_centered * predicted_centered) ** 2 return float(np.clip(numerator / denominator, 0.0, 1.0)) @@ -832,7 +887,8 @@ def _intervals( return {"conf_lower": None, "conf_upper": None, "pred_int": None} alpha = 1.0 - confidence_interval errors = actual - fitted - q_lo, q_hi = np.quantile(errors, [alpha / 2.0, 1.0 - alpha / 2.0], method="median_unbiased") + # R: quantile(errors, c(alpha / 2, 1 - alpha / 2), na.rm = TRUE, type = 8). + q_lo, q_hi = r_quantile(errors, [alpha / 2.0, 1.0 - alpha / 2.0], 8, na_rm=True) conf_lower = fitted + q_lo conf_upper = fitted + q_hi @@ -882,12 +938,12 @@ def _dimred_coefficients( raise ValueError("[threshold] must be a single finite nonnegative number.") def cor_coef() -> NDArray[np.float64]: - from scipy import stats # type: ignore[import-untyped] - + # stats::cor(x[, j], y, method = "spearman"): Pearson on average + # ranks with R's cov.c arithmetic. + y_rank = r_rank_average(y) out = np.zeros(p, dtype=np.float64) for j in range(p): - with np.errstate(all="ignore"): - rho = stats.spearmanr(x[:, j], y).statistic + rho = r_cor(r_rank_average(x[:, j]), y_rank) out[j] = rho if math.isfinite(rho) else 0.0 return out @@ -930,7 +986,8 @@ def caus_coef() -> NDArray[np.float64]: elif method == "nns.caus": coef = caus_coef() elif method == "all": - coef = np.mean(np.column_stack((caus_coef(), dep_coef(), cor_coef(), np.ones(p))), axis=1) + # rowMeans(cbind(caus, dep, cor, 1)): long-double row sums / 4. + coef = r_row_means(np.column_stack((caus_coef(), dep_coef(), cor_coef(), np.ones(p)))) elif method == "equal": coef = np.ones(p, dtype=np.float64) else: @@ -973,10 +1030,9 @@ def normalize(m: NDArray[np.float64]) -> NDArray[np.float64]: if denominator < 1: raise ValueError("Dimension reduction retained no predictors.") - x_star = np.asarray(nx @ coef / denominator, dtype=np.float64) - point_star = ( - None if np_points is None else np.asarray(np_points @ coef / denominator, dtype=np.float64) - ) + # R: (nx %*% coef) / denominator, in reference-BLAS order (see nns._rblas). + x_star = r_matvec(nx, coef) / denominator + point_star = None if np_points is None else r_matvec(np_points, coef) / denominator names = ( list(variable_names) if variable_names is not None and len(variable_names) == x.shape[1] @@ -1511,8 +1567,14 @@ def _mreg_default_nbest(x: NDArray[np.float64], y: NDArray[np.float64], rpm_rows from nns.copula import nns_copula # R14's smoothing heuristic is unsigned: reflect only the response. - dep = 0.5 * ( - float(nns_copula(np.column_stack((x, y)))) + float(nns_copula(np.column_stack((x, -y)))) + # R: mean(c(NNS.copula(cbind(X, y)), NNS.copula(cbind(X, -y)))). + dep = r_mean( + np.array( + [ + float(nns_copula(np.column_stack((x, y)))), + float(nns_copula(np.column_stack((x, -y)))), + ] + ) ) except Exception: dep = float("nan") @@ -1698,6 +1760,7 @@ def nns_reg_engine( dist = _validate_dist(dist) noise_reduction = _validate_noise(noise_reduction) confidence_interval = _validate_ci(confidence_interval) + threshold = _validate_threshold(threshold) y_raw, y_categories = _response_vector(y) if y_raw.size < 2: @@ -1720,8 +1783,10 @@ def nns_reg_engine( # R14 nominal classes are probability-valued indicators, never numeric # regression on arbitrary class codes. This branch precedes NNS.M.reg and # the former univariate class interpolation. NNS.M.reg's standalone - # `multivariate_only` mode retains its own separate public contract. - if task["is_class"] and not multivariate_only and not multivariate_call: + # `multivariate_only` mode retains its own separate public contract. As in + # R, it also precedes `multivariate.call`: a class task returns the + # classification result even then (internal callers request type = "REG"). + if task["is_class"] and not multivariate_only: from nns._class_r14 import classify return classify( @@ -1759,6 +1824,11 @@ def nns_reg_engine( synthetic_equation = None x_star = None if ex.shape[1] > 1: + if not isinstance(threshold, float): + raise ValueError( + "[threshold] must be a single number for regression; one cutoff " + "per class applies to classification." + ) dr = _dimreduce( ex, ep, @@ -1809,7 +1879,7 @@ def nns_reg_engine( if task["is_class"] else _r2(y_numeric, fitted_pred) ) - se = float(np.sqrt(np.mean((fitted_pred - y_numeric) ** 2))) + se = math.sqrt(r_mean((fitted_pred - y_numeric) ** 2)) # R: sqrt(mean(...)) intervals = _intervals( y_numeric, fitted_pred, diff --git a/src/nns/_rmean.py b/src/nns/_rmean.py index 47e7524b..1bbe16e6 100644 --- a/src/nns/_rmean.py +++ b/src/nns/_rmean.py @@ -27,6 +27,15 @@ def r_mean(values: NDArray[np.float64]) -> float: return float(s) +def r_sum(values: NDArray[np.float64]) -> float: + """R's ``sum()`` of a double vector (summary.c ``rsum``): one sequential + long-double accumulation, rounded to double at the end.""" + v = np.asarray(values, dtype=np.float64).ravel() + if v.size == 0: + return 0.0 + return float(np.cumsum(v.astype(np.longdouble))[-1] + 0.0) + + def r_mean_no_na(values: NDArray[np.float64]) -> float: """NNS.part's C++ ``mean_no_na``: long-double sum of the non-NaN values divided by their count (single pass).""" @@ -52,3 +61,339 @@ def r_group_means_no_na( has = counts > 0 out[has] = (sums[has] / counts[has]).astype(np.float64) return out + + +def _r_cov_mean(values: NDArray[np.longdouble]) -> np.longdouble: + """cov.c ``MEAN_``: long-double sum / n, then the refinement pass.""" + n = values.size + tmp = (np.cumsum(values)[-1] + np.longdouble(0.0)) / n + if np.isfinite(float(tmp)): + tmp = tmp + np.cumsum(values - tmp)[-1] / n + # The mean is stored as a double (xm[i]) and reloaded into a long double. + return np.longdouble(float(tmp)) + + +def r_cor(x: NDArray[np.float64], y: NDArray[np.float64]) -> float: + """R ``stats::cor(x, y)`` (Pearson, ``use = "everything"``) of two complete + double vectors: cov.c ``cov_na_2``. + + Deviations from the double-stored means, their products and the sums are + all long double; each standard deviation is ``sqrtl`` of the long-double + variance rounded to double; the ratio is formed in double and clamped to + [-1, 1]. NaN where a standard deviation is zero or ``n < 2``. + """ + xv = np.asarray(x, dtype=np.float64).ravel().astype(np.longdouble) + yv = np.asarray(y, dtype=np.float64).ravel().astype(np.longdouble) + n = xv.size + if n <= 1 or yv.size != n: + return float("nan") + dx = xv - _r_cov_mean(xv) + dy = yv - _r_cov_mean(yv) + n1 = np.longdouble(n - 1) + cov = float(np.cumsum(dx * dy)[-1] / n1) + sd_x = float(np.sqrt(np.cumsum(dx * dx)[-1] / n1)) + sd_y = float(np.sqrt(np.cumsum(dy * dy)[-1] / n1)) + if sd_x == 0.0 or sd_y == 0.0: + return float("nan") + return min(1.0, max(-1.0, cov / (sd_x * sd_y))) + + +def r_cor_matrix(x: NDArray[np.float64]) -> NDArray[np.float64]: + """R ``stats::cor(X)`` (Pearson, ``use = "everything"``) of a complete + double matrix: cov.c ``cov_na_1``. + + Each covariance is a long-double sum of long-double products rounded to + double; standard deviations are double ``sqrt`` of those variances; the + correlation is a double ratio, clamped, with a unit diagonal. NaN where a + standard deviation is zero. + """ + m = np.asarray(x, dtype=np.float64) + n, p = m.shape + out = np.full((p, p), np.nan, dtype=np.float64) + if n <= 1: + return out + ld = m.astype(np.longdouble) + dev = np.column_stack([ld[:, j] - _r_cov_mean(ld[:, j]) for j in range(p)]) + n1 = np.longdouble(n - 1) + cov = np.empty((p, p), dtype=np.float64) + for i in range(p): + for j in range(i + 1): + cov[i, j] = cov[j, i] = float(np.cumsum(dev[:, i] * dev[:, j])[-1] / n1) + sd = np.sqrt(np.diag(cov)) + for i in range(p): + for j in range(i): + if sd[i] == 0.0 or sd[j] == 0.0: + out[i, j] = out[j, i] = np.nan + else: + out[i, j] = out[j, i] = min(1.0, max(-1.0, cov[i, j] / (sd[i] * sd[j]))) + out[i, i] = 1.0 + return out + + +def r_rank_average(values: NDArray[np.float64]) -> NDArray[np.float64]: + """R ``rank(x)`` with the default ``ties.method = "average"``.""" + v = np.asarray(values, dtype=np.float64).ravel() + order = np.argsort(v, kind="stable") + sorted_values = v[order] + ranks = np.empty(v.size, dtype=np.float64) + if v.size == 0: + return ranks + breaks = np.flatnonzero(sorted_values[1:] != sorted_values[:-1]) + 1 + starts = np.concatenate(([0], breaks)) + ends = np.concatenate((breaks, [v.size])) + ranks[order] = np.repeat((starts + 1 + ends) / 2.0, ends - starts) + return ranks + + +def r_row_means(x: NDArray[np.float64]) -> NDArray[np.float64]: + """R ``rowMeans(x)`` (array.c ``do_colsum``): each row accumulated column by + column in long double, divided by the column count, rounded to double.""" + m = np.asarray(x, dtype=np.float64) + sums = np.zeros(m.shape[0], dtype=np.longdouble) + for j in range(m.shape[1]): + sums = sums + m[:, j].astype(np.longdouble) + return np.asarray(sums / m.shape[1], dtype=np.float64) + + +def r_seq_by(start: float, stop: float, by: float) -> NDArray[np.float64]: + """R ``seq(from, to, by)`` for double arguments (base ``seq.default``). + + The grid is ``from + (0:n) * by`` with ``n = as.integer((to - from)/by + + 1e-10)``, capped at ``to``; its values are not decimal-rounded, so e.g. the + seventh value of ``seq(0.01, 0.99, by = 0.01)`` is 0.069999999999999993, + not the double nearest 0.07. + """ + start, stop, by = float(start), float(stop), float(by) + delta = stop - start + if delta == 0.0 and stop == 0.0: + return np.array([stop], dtype=np.float64) + n = delta / by + if not np.isfinite(n) or n < 0: + raise ValueError("invalid '(to - from)/by'") + if abs(delta) / max(abs(stop), abs(start)) < 100 * np.finfo(np.float64).eps: + return np.array([start], dtype=np.float64) + count = int(n + 1e-10) + grid = start + np.arange(count + 1, dtype=np.float64) * by + return np.minimum(grid, stop) if by > 0 else np.maximum(grid, stop) + + +def _r_pow_di(x: float, n: int) -> float: + """R ``R_pow_di``: x^n by repeated squaring.""" + xn = 1.0 + if n != 0: + negative = n < 0 + n = -n if negative else n + while True: + if n & 1: + xn *= x + n >>= 1 + if n: + x *= x + else: + break + if negative: + xn = 1.0 / xn + return xn + + +_M_LOG10_2 = 0.301029995663981195213738894724 + + +def r_round(x: float, digits: float = 0.0) -> float: + """R ``round(x, digits)`` (nmath ``fround``, R >= 4.0.0). + + R picks the nearer of ``floor(x * 10^d) / 10^d`` and ``ceil(x * 10^d) / + 10^d`` by their double distances to ``x`` (ties to an even last digit), + which can differ from Python's correctly rounded ``round()``. + """ + import math + + x = float(x) + if math.isnan(x) or math.isnan(digits): + return x + digits + if not math.isfinite(x) or digits > 323 or x == 0.0: + return x + if digits < -308: + return 0.0 + if digits == 0.0: + return float(np.rint(x)) + dig = math.floor(digits + 0.5) + sgn = 1.0 + if x < 0.0: + sgn, x = -1.0, -x + l10x = _M_LOG10_2 * (0.5 + (math.frexp(x)[1] - 1)) + if l10x + dig > 15: + return sgn * x + if dig <= 308: + pow10 = _r_pow_di(10.0, dig) + x10 = x * pow10 + i10 = math.floor(x10) + xd = i10 / pow10 + xu = math.ceil(x10) / pow10 + else: + p10 = _r_pow_di(10.0, dig - 308) + pow10 = _r_pow_di(10.0, 308) + x10 = (x * pow10) * p10 + i10 = math.floor(x10) + xd = i10 / pow10 / p10 + xu = math.ceil(x10) / pow10 / p10 + du = xu - x + dd = x - xd + return sgn * (xu if (du < dd or (math.fmod(i10, 2.0) == 1 and du == dd)) else xd) + + +def r_round_array(values: NDArray[np.float64], digits: float = 0.0) -> NDArray[np.float64]: + """Elementwise :func:`r_round` (vectorised; identical double arithmetic).""" + import math + + v = np.asarray(values, dtype=np.float64) + if digits == 0.0: + return np.asarray(np.rint(v), dtype=np.float64) + if math.isnan(digits) or digits > 323 or digits < -308 or abs(digits) > 308: + return np.asarray(np.vectorize(lambda z: r_round(float(z), digits))(v), dtype=np.float64) + dig = math.floor(digits + 0.5) + pow10 = _r_pow_di(10.0, dig) + sgn = np.where(v < 0.0, -1.0, 1.0) + a = np.abs(v) + with np.errstate(all="ignore"): + x10 = a * pow10 + i10 = np.floor(x10) + xd = i10 / pow10 + xu = np.ceil(x10) / pow10 + du = xu - a + dd = a - xd + rounded = sgn * np.where((du < dd) | ((np.fmod(i10, 2.0) == 1) & (du == dd)), xu, xd) + l10x = _M_LOG10_2 * (0.5 + (np.frexp(a)[1] - 1).astype(np.float64)) + keep = ~np.isfinite(v) | (v == 0.0) | (l10x + dig > 15) + return np.asarray(np.where(keep, v, rounded), dtype=np.float64) + + +def r_quantile( + values: NDArray[np.float64], + probs: float | NDArray[np.float64] | list[float], + type: int = 7, + na_rm: bool = False, +) -> NDArray[np.float64]: + """R ``stats::quantile(x, probs, type = 7 | 8, names = FALSE)``. + + Follows ``quantile.default`` operation for operation: type 7 interpolates + ``(1 - h) * x[lo] + h * x[hi]``; type 8 uses ``a = b = 1/3`` with R's + relative fuzz and padded order statistics. NumPy's interpolation + (``a + (b - a) * t``) rounds differently. + """ + x = np.asarray(values, dtype=np.float64).ravel() + if na_rm: + x = x[~np.isnan(x)] + elif np.isnan(x).any(): + raise ValueError("missing values and NaN's not allowed if 'na.rm' is FALSE") + p = np.asarray(probs, dtype=np.float64).reshape(-1) + eps = np.finfo(np.float64).eps + p_ok = ~np.isnan(p) + if np.any(p_ok & ((p < -100 * eps) | (p > 1 + 100 * eps))): + raise ValueError("'probs' outside [0,1]") + p = np.maximum(0.0, np.minimum(1.0, p)) + n = x.size + xs = np.sort(x) + qs = np.full(p.size, np.nan, dtype=np.float64) + if n == 0: + return qs + if type == 7: + index = 1 + max(n - 1, 0) * p + lo = np.floor(index) + hi = np.ceil(index) + lo_i = np.where(p_ok, lo, 1).astype(np.int64) - 1 + hi_i = np.where(p_ok, hi, 1).astype(np.int64) - 1 + qs = xs[lo_i].copy() + i = p_ok & (index > lo) & (xs[hi_i] != qs) + h = (index - lo)[i] + qs[i] = (1 - h) * qs[i] + h * xs[hi_i[i]] + elif type == 8: + fuzz = 4 * eps + a = b = 1.0 / 3.0 + nppm = a + p * (n + 1 - a - b) + j = np.floor(nppm * (1 + fuzz)) + h = nppm - j + h[np.abs(h) < fuzz] = 0.0 + padded = np.concatenate(([xs[0], xs[0]], xs, [xs[-1], xs[-1]])) + j_i = np.where(p_ok, j, 0).astype(np.int64) + left = padded[j_i + 1] + right = padded[j_i + 2] + qs = left.copy() + qs[h == 1] = right[h == 1] + other = (h > 0) & (h < 1) & (left != right) + qs[other] = ((1 - h) * left + h * right)[other] + else: + raise ValueError("only quantile types 7 and 8 are used by NNS") + qs[~p_ok] = p[~p_ok] + return qs + + +def r_col_means(x: NDArray[np.float64]) -> NDArray[np.float64]: + """R ``colMeans(x)``: each column summed sequentially in long double, + divided by the row count, rounded to double (no refinement pass).""" + m = np.asarray(x, dtype=np.float64) + if m.shape[0] == 0: + return np.full(m.shape[1], np.nan, dtype=np.float64) + sums = np.cumsum(m.astype(np.longdouble), axis=0)[-1] + np.longdouble(0.0) + return np.asarray(sums / m.shape[0], dtype=np.float64) + + +def r_approx( + x: NDArray[np.float64], + y: NDArray[np.float64], + xout: NDArray[np.float64], + yleft: float | None = None, + yright: float | None = None, +) -> NDArray[np.float64]: + """R ``stats::approx(x, y, xout, method = "linear", ties = "ordered")$y``. + + ``x`` must be sorted; duplicates are kept, as with ``ties = "ordered"``. + Mirrors the C kernel ``approx1``: bisection to ``x[i] <= v <= x[i + 1]``, + the stored ordinate returned exactly when ``v`` equals a knot (the last of + tied knots), else ``y[i] + (y[j] - y[i]) * ((v - x[i]) / (x[j] - x[i]))``. + ``yleft``/``yright`` default to ``NaN`` (``rule = 1``); pass ``y[0]`` and + ``y[-1]`` for ``rule = 2``. + """ + xs = np.asarray(x, dtype=np.float64).ravel() + ys = np.asarray(y, dtype=np.float64).ravel() + v = np.asarray(xout, dtype=np.float64).ravel() + n = xs.size + out = np.full(v.size, np.nan, dtype=np.float64) + if n == 0: + return out + low = np.nan if yleft is None else float(yleft) + high = np.nan if yright is None else float(yright) + finite = ~np.isnan(v) + below = finite & (v < xs[0]) + above = finite & (v > xs[-1]) + inside = finite & ~below & ~above + out[below] = low + out[above] = high + if n == 1: + out[inside] = ys[0] + return out + vi = v[inside] + i = np.clip(np.searchsorted(xs, vi, side="right") - 1, 0, n - 2) + j = i + 1 + xi, xj, yi, yj = xs[i], xs[j], ys[i], ys[j] + with np.errstate(invalid="ignore", divide="ignore"): + interp = yi + (yj - yi) * ((vi - xi) / (xj - xi)) + interp = np.where(vi == xi, yi, interp) + interp = np.where(vi == xj, yj, interp) + out[inside] = interp + return out + + +def r_tie_means( + x: NDArray[np.float64], y: NDArray[np.float64] +) -> tuple[NDArray[np.float64], NDArray[np.float64]]: + """``regularize.values(x, y, ties = mean)`` for sorted ``x``: one knot per + distinct ``x`` with R's ``mean()`` of its ordinates (in input order).""" + xs = np.asarray(x, dtype=np.float64).ravel() + ys = np.asarray(y, dtype=np.float64).ravel() + ux = np.unique(xs) + if ux.size == xs.size: + return xs, ys + groups = np.searchsorted(ux, xs) + means = np.array([r_mean(ys[groups == g]) for g in range(ux.size)]) + return ux, means diff --git a/src/nns/_rnmath.hpp b/src/nns/_rnmath.hpp new file mode 100644 index 00000000..81d8efce --- /dev/null +++ b/src/nns/_rnmath.hpp @@ -0,0 +1,234 @@ +// Bit-faithful ports of the R nmath routines called by the NNS C++ kernels. +// +// R's src/NNS_dep.cpp calls R::dhyper and R::pnorm. They use Loader's +// saddle-point binomial (dbinom_raw with stirlerr and bd0) and Cody's +// rational approximation; closed forms from binomial products or erfc agree +// with them only to a few units in the last place, which NNS later feeds into +// exact comparisons. These are the R routines themselves, evaluated with the +// same libm calls. +// +// Source: R 4.5.0 src/nmath/{dhyper,dbinom,stirlerr,bd0,pnorm}.c +// Copyright (C) 2000-2024 The R Core Team; Catherine Loader; W. J. Cody. +// GPL-2 or later. stirlerr follows the R >= 4.4.0 series cutoffs. +// Mirrored in src/nns/_rnmath.py for the pure-Python fallback. +// SPDX-License-Identifier: GPL-3.0-only +#pragma once +#include +#include + +namespace nns_rmath { + +static const double kLn2Pi = 1.837877066409345483560659472811; // log(2*pi) +static const double kLnSqrt2Pi = 0.918938533204672741780329736406; // log(sqrt(2*pi)) +static const double kSqrt32 = 5.656854249492380195206754896838; // sqrt(32) +static const double k1SqrtTwoPi = 0.398942280401432677939946059934; // 1/sqrt(2*pi) + +inline double stirlerr(double n) { + static const double S[17] = { + 0.083333333333333333333, // 1/12 + 0.00277777777777777777778, // 1/360 + 0.00079365079365079365079365, // 1/1260 + 0.000595238095238095238095238, // 1/1680 + 0.0008417508417508417508417508, // 1/1188 + 0.0019175269175269175269175262, // 691/360360 + 0.0064102564102564102564102561, // 1/156 + 0.029550653594771241830065352, // 3617/122400 + 0.17964437236883057316493850, // 43867/244188 + 1.3924322169059011164274315, // 174611/125400 + 13.402864044168391994478957, // 77683/5796 + 156.84828462600201730636509, // 236364091/1506960 + 2193.1033333333333333333333, // 657931/300 + 36108.771253724989357173269, // 3392780147/93960 + 691472.26885131306710839498, // 1723168255201/2492028 + 15238221.539407416192283370, // 7709321041217/505920 + 382900751.39141414141414141}; // 151628697551/396 + static const double sferr_halves[31] = { + 0.0, /* n=0 - wrong, place holder only */ + 0.1534264097200273452913848, 0.0810614667953272582196702, + 0.0548141210519176538961390, 0.0413406959554092940938221, + 0.03316287351993628748511048, 0.02767792568499833914878929, + 0.02374616365629749597132920, 0.02079067210376509311152277, + 0.01848845053267318523077934, 0.01664469118982119216319487, + 0.01513497322191737887351255, 0.01387612882307074799874573, + 0.01281046524292022692424986, 0.01189670994589177009505572, + 0.01110455975820691732662991, 0.010411265261972096497478567, + 0.009799416126158803298389475, 0.009255462182712732917728637, + 0.008768700134139385462952823, 0.008330563433362871256469318, + 0.007934114564314020547248100, 0.007573675487951840794972024, + 0.007244554301320383179543912, 0.006942840107209529865664152, + 0.006665247032707682442354394, 0.006408994188004207068439631, + 0.006171712263039457647532867, 0.005951370112758847735624416, + 0.005746216513010115682023589, 0.005554733551962801371038690}; + // (S0-(S1-(...-S[k-1]/nn)/nn)...)/n with k terms, as R spells it out. + auto series = [&](double nn, int k) { + double acc = S[k - 1]; + for (int j = k - 2; j >= 0; --j) acc = S[j] - acc / nn; + return acc / n; + }; + double nn; + if (n <= 23.5) { + nn = n + n; + if (n <= 15.0 && (nn == static_cast(nn))) return sferr_halves[static_cast(nn)]; + if (n <= 5.25) { + if (n >= 1.0) { + const double l_n = std::log(n); + return std::lgamma(n) + n * (1 - l_n) + std::ldexp(l_n - kLn2Pi, -1); + } + return std::lgamma(1.0 + n) - (n + 0.5) * std::log(n) + n - kLnSqrt2Pi; + } + nn = n * n; + if (n > 12.8) return series(nn, 7); + if (n > 12.3) return series(nn, 8); + if (n > 8.9) return series(nn, 9); + if (n > 7.3) return series(nn, 11); + if (n > 6.6) return series(nn, 13); + if (n > 6.1) return series(nn, 15); + return series(nn, 17); + } + nn = n * n; + if (n > 15.7e6) return S[0] / n; + if (n > 6180) return series(nn, 2); + if (n > 205) return series(nn, 3); + if (n > 86) return series(nn, 4); + if (n > 27) return series(nn, 5); + return series(nn, 6); +} + +inline double bd0(double x, double np) { + if (!std::isfinite(x) || !std::isfinite(np) || np == 0.0) return NAN; + if (std::fabs(x - np) < 0.1 * (x + np)) { + double v = (x - np) / (x + np); + double s = (x - np) * v; + if (std::fabs(s) < DBL_MIN) return s; + double ej = 2 * x * v; + v *= v; + for (int j = 1; j < 1000; j++) { + ej *= v; + const double s_ = s; + s += ej / ((j << 1) + 1); + if (s == s_) return s; + } + } + return x * std::log(x / np) + np - x; +} + +inline double pow1p(double x, double y) { + if (std::isnan(y)) return (x == 0) ? 1. : y; + if (0 <= y && y == std::trunc(y) && y <= 4.) { + switch (static_cast(y)) { + case 0: return 1; + case 1: return x + 1.; + case 2: return x * (x + 2.) + 1.; + case 3: return x * (x * (x + 3.) + 3.) + 1.; + case 4: return x * (x * (x * (x + 4.) + 6.) + 4.) + 1.; + } + } + if ((x + 1) - 1 == x || std::fabs(x) > 0.5 || std::isnan(x)) return std::pow(1 + x, y); + return std::exp(y * std::log1p(x)); +} + +inline double dbinom_raw(double x, double n, double p, double q) { + if (p == 0) return (x == 0) ? 1. : 0.; + if (q == 0) return (x == n) ? 1. : 0.; + if (x == 0) { + if (n == 0) return 1.; + return (p > q) ? std::pow(q, n) : pow1p(-p, n); + } + if (x == n) return (p > q) ? pow1p(-q, n) : std::pow(p, n); + if (x < 0 || x > n) return 0.; + const double lc = + stirlerr(n) - stirlerr(x) - stirlerr(n - x) - bd0(x, n * p) - bd0(n - x, n * q); + const double lf = kLn2Pi + std::log(x) + std::log1p(-x / n); + return std::exp(lc - 0.5 * lf); +} + +// R::dhyper(x, r, b, n, 0) for non-negative integer arguments. +inline double dhyper(double x, double r, double b, double n) { + if (r < 0 || b < 0 || n < 0 || n > r + b) return NAN; + if (x < 0) return 0.; + if (n < x || r < x || n - x > b) return 0.; + if (n == 0) return (x == 0) ? 1. : 0.; + const double p = n / (r + b); + const double q = (r + b - n) / (r + b); + const double p1 = dbinom_raw(x, r, p, q); + const double p2 = dbinom_raw(n - x, b, p, q); + const double p3 = dbinom_raw(n, r + b, p, q); + return p1 * p2 / p3; +} + +// R::pnorm(x, 0, 1, 1, 0): the lower tail of pnorm_both. +inline double pnorm(double x) { + static const double a[5] = {2.2352520354606839287, 161.02823106855587881, + 1067.6894854603709582, 18154.981253343561249, + 0.065682337918207449113}; + static const double b[4] = {47.20258190468824187, 976.09855173777669322, + 10260.932208618978205, 45507.789335026729956}; + static const double c[9] = {0.39894151208813466764, 8.8831497943883759412, + 93.506656132177855979, 597.27027639480026226, + 2494.5375852903726711, 6848.1904505362823326, + 11602.651437647350124, 9842.7148383839780218, + 1.0765576773720192317e-8}; + static const double d[8] = {22.266688044328115691, 235.38790178262499861, + 1519.377599407554805, 6485.558298266760755, + 18615.571640885098091, 34900.952721145977266, + 38912.003286093271411, 19685.429676859990727}; + static const double p[6] = {0.21589853405795699, 0.1274011611602473639, + 0.022235277870649807, 0.001421619193227893466, + 2.9112874951168792e-5, 0.02307344176494017303}; + static const double q[5] = {1.28426009614491121, 0.468238212480865118, + 0.0659881378689285515, 0.00378239633202758244, + 7.29751555083966205e-5}; + if (std::isnan(x)) return x; + if (!std::isfinite(x)) return x < 0 ? 0. : 1.; + const double eps = DBL_EPSILON * 0.5; + const double y = std::fabs(x); + double xnum, xden, temp, xsq, del, cum, ccum; + auto do_del = [&](double X) { + xsq = std::ldexp(std::trunc(std::ldexp(X, 4)), -4); + del = (X - xsq) * (X + xsq); + cum = std::exp(-xsq * std::ldexp(xsq, -1)) * std::exp(-std::ldexp(del, -1)) * temp; + ccum = 1.0 - cum; + }; + if (y <= 0.67448975) { + if (y > eps) { + xsq = x * x; + xnum = a[4] * xsq; + xden = xsq; + for (int i = 0; i < 3; ++i) { + xnum = (xnum + a[i]) * xsq; + xden = (xden + b[i]) * xsq; + } + } else { + xnum = xden = 0.0; + } + temp = x * (xnum + a[3]) / (xden + b[3]); + return 0.5 + temp; + } + if (y <= kSqrt32) { + xnum = c[8] * y; + xden = y; + for (int i = 0; i < 7; ++i) { + xnum = (xnum + c[i]) * y; + xden = (xden + d[i]) * y; + } + temp = (xnum + c[7]) / (xden + d[7]); + do_del(y); + return x > 0. ? ccum : cum; + } + if (-38.4674 < x && x < 8.2924) { + xsq = 1.0 / (x * x); + xnum = p[5] * xsq; + xden = xsq; + for (int i = 0; i < 4; ++i) { + xnum = (xnum + p[i]) * xsq; + xden = (xden + q[i]) * xsq; + } + temp = xsq * (xnum + p[4]) / (xden + q[4]); + temp = (k1SqrtTwoPi - temp) / y; + do_del(x); + return x > 0. ? ccum : cum; + } + return x > 0 ? 1. : 0.; +} + +} // namespace nns_rmath diff --git a/src/nns/_rnmath.py b/src/nns/_rnmath.py new file mode 100644 index 00000000..f1e53c69 --- /dev/null +++ b/src/nns/_rnmath.py @@ -0,0 +1,303 @@ +"""Bit-faithful ports of the R ``nmath`` routines the NNS kernels call. + +R's C++ kernels call ``R::dhyper`` and ``R::pnorm``. Those use Loader's +saddle-point binomial (``dbinom_raw`` with ``stirlerr`` and ``bd0``) and +Cody's rational approximation, which agree with closed forms built from +``math.comb`` or ``math.erf`` only to within a few units in the last place. +NNS feeds the results into exact comparisons downstream (the dependence +coefficients become dimension-reduction weights whose index is cut by +``findInterval``), so the port reproduces R's own arithmetic. + +Source: R 4.5.0 ``src/nmath/{dhyper,dbinom,stirlerr,bd0,pnorm}.c`` +(Copyright (C) 2000-2024 The R Core Team; Catherine Loader; W. J. Cody; +GPL-2 or later). ``stirlerr`` follows the R >= 4.4.0 series cutoffs. +""" + +from __future__ import annotations + +import ctypes +import ctypes.util +import functools +import math +import sys +from collections.abc import Callable + +_DBL_EPSILON = sys.float_info.epsilon +_DBL_MIN = sys.float_info.min +_M_LN_2PI = 1.837877066409345483560659472811 # log(2*pi) +_M_LN_SQRT_2PI = 0.918938533204672741780329736406 # log(sqrt(2*pi)) +_M_SQRT_32 = 5.656854249492380195206754896838 # sqrt(32) +_M_1_SQRT_2PI = 0.398942280401432677939946059934 # 1/sqrt(2*pi) + +_S = ( + 0.083333333333333333333, # 1/12 + 0.00277777777777777777778, # 1/360 + 0.00079365079365079365079365, # 1/1260 + 0.000595238095238095238095238, # 1/1680 + 0.0008417508417508417508417508, # 1/1188 + 0.0019175269175269175269175262, # 691/360360 + 0.0064102564102564102564102561, # 1/156 + 0.029550653594771241830065352, # 3617/122400 + 0.17964437236883057316493850, # 43867/244188 + 1.3924322169059011164274315, # 174611/125400 + 13.402864044168391994478957, # 77683/5796 + 156.84828462600201730636509, # 236364091/1506960 + 2193.1033333333333333333333, # 657931/300 + 36108.771253724989357173269, # 3392780147/93960 + 691472.26885131306710839498, # 1723168255201/2492028 + 15238221.539407416192283370, # 7709321041217/505920 + 382900751.39141414141414141, # 151628697551/396 +) + +_SFERR_HALVES = ( + 0.0, # n=0 - wrong, place holder only + 0.1534264097200273452913848, # 0.5 + 0.0810614667953272582196702, # 1.0 + 0.0548141210519176538961390, # 1.5 + 0.0413406959554092940938221, # 2.0 + 0.03316287351993628748511048, # 2.5 + 0.02767792568499833914878929, # 3.0 + 0.02374616365629749597132920, # 3.5 + 0.02079067210376509311152277, # 4.0 + 0.01848845053267318523077934, # 4.5 + 0.01664469118982119216319487, # 5.0 + 0.01513497322191737887351255, # 5.5 + 0.01387612882307074799874573, # 6.0 + 0.01281046524292022692424986, # 6.5 + 0.01189670994589177009505572, # 7.0 + 0.01110455975820691732662991, # 7.5 + 0.010411265261972096497478567, # 8.0 + 0.009799416126158803298389475, # 8.5 + 0.009255462182712732917728637, # 9.0 + 0.008768700134139385462952823, # 9.5 + 0.008330563433362871256469318, # 10.0 + 0.007934114564314020547248100, # 10.5 + 0.007573675487951840794972024, # 11.0 + 0.007244554301320383179543912, # 11.5 + 0.006942840107209529865664152, # 12.0 + 0.006665247032707682442354394, # 12.5 + 0.006408994188004207068439631, # 13.0 + 0.006171712263039457647532867, # 13.5 + 0.005951370112758847735624416, # 14.0 + 0.005746216513010115682023589, # 14.5 + 0.005554733551962801371038690, # 15.0 +) + + +def _series(n: float, nn: float, k: int) -> float: + """R's nested ``(S0-(S1-(...-S[k-1]/nn)/nn)...)/n`` with ``k`` terms.""" + acc = _S[k - 1] + for j in range(k - 2, -1, -1): + acc = _S[j] - acc / nn + return acc / n + + +def stirlerr(n: float) -> float: + """R ``stirlerr(n) = log(n!) - log(sqrt(2*pi*n) * (n/e)^n)``.""" + if n <= 23.5: + nn = n + n + if n <= 15.0 and nn == int(nn): + return _SFERR_HALVES[int(nn)] + if n <= 5.25: + if n >= 1.0: + l_n = math.log(n) + return clgamma(n) + n * (1 - l_n) + math.ldexp(l_n - _M_LN_2PI, -1) + return math.lgamma(1.0 + n) - (n + 0.5) * math.log(n) + n - _M_LN_SQRT_2PI + nn = n * n + if n > 12.8: + return _series(n, nn, 7) + if n > 12.3: + return _series(n, nn, 8) + if n > 8.9: + return _series(n, nn, 9) + if n > 7.3: + return _series(n, nn, 11) + if n > 6.6: + return _series(n, nn, 13) + if n > 6.1: + return _series(n, nn, 15) + return _series(n, nn, 17) + nn = n * n + if n > 15.7e6: + return _S[0] / n + if n > 6180: + return _series(n, nn, 2) + if n > 205: + return _series(n, nn, 3) + if n > 86: + return _series(n, nn, 4) + if n > 27: + return _series(n, nn, 5) + return _series(n, nn, 6) + + +def bd0(x: float, np_: float) -> float: + """R ``bd0(x, np) = x log(x/np) + np - x``, accurately.""" + if not (math.isfinite(x) and math.isfinite(np_)) or np_ == 0.0: + return math.nan + if abs(x - np_) < 0.1 * (x + np_): + v = (x - np_) / (x + np_) + s = (x - np_) * v + if abs(s) < _DBL_MIN: + return s + ej = 2 * x * v + v *= v + for j in range(1, 1000): + ej *= v + s_prev = s + s += ej / ((j << 1) + 1) + if s == s_prev: + return s + return x * math.log(x / np_) + np_ - x + + +def pow1p(x: float, y: float) -> float: + """R ``pow1p(x, y) = (1 + x)^y``, accurately for small ``|x|``.""" + if math.isnan(y): + return 1.0 if x == 0 else y + if 0 <= y and y == math.trunc(y) and y <= 4.0: + k = int(y) + if k == 0: + return 1.0 + if k == 1: + return x + 1.0 + if k == 2: + return x * (x + 2.0) + 1.0 + if k == 3: + return x * (x * (x + 3.0) + 3.0) + 1.0 + return x * (x * (x * (x + 4.0) + 6.0) + 4.0) + 1.0 + if (x + 1) - 1 == x or abs(x) > 0.5 or math.isnan(x): + return math.pow(1 + x, y) + return math.exp(y * math.log1p(x)) + + +def dbinom_raw(x: float, n: float, p: float, q: float) -> float: + """R ``dbinom_raw(x, n, p, q, give_log = FALSE)``.""" + if p == 0: + return 1.0 if x == 0 else 0.0 + if q == 0: + return 1.0 if x == n else 0.0 + if x == 0: + if n == 0: + return 1.0 + return math.pow(q, n) if p > q else pow1p(-p, n) + if x == n: + return pow1p(-q, n) if p > q else math.pow(p, n) + if x < 0 or x > n: + return 0.0 + lc = stirlerr(n) - stirlerr(x) - stirlerr(n - x) - bd0(x, n * p) - bd0(n - x, n * q) + lf = _M_LN_2PI + math.log(x) + math.log1p(-x / n) + return math.exp(lc - 0.5 * lf) + + +def dhyper(x: float, r: float, b: float, n: float) -> float: + """R ``dhyper(x, r, b, n, log = FALSE)`` for non-negative integer arguments.""" + if r < 0 or b < 0 or n < 0 or n > r + b: + return math.nan + if x < 0: + return 0.0 + if n < x or r < x or n - x > b: + return 0.0 + if n == 0: + return 1.0 if x == 0 else 0.0 + p = float(n) / float(r + b) + q = float(r + b - n) / float(r + b) + p1 = dbinom_raw(x, r, p, q) + p2 = dbinom_raw(n - x, b, p, q) + p3 = dbinom_raw(n, r + b, p, q) + return p1 * p2 / p3 + + +_A = (2.2352520354606839287, 161.02823106855587881, 1067.6894854603709582, + 18154.981253343561249, 0.065682337918207449113) +_B = (47.20258190468824187, 976.09855173777669322, 10260.932208618978205, + 45507.789335026729956) +_C = (0.39894151208813466764, 8.8831497943883759412, 93.506656132177855979, + 597.27027639480026226, 2494.5375852903726711, 6848.1904505362823326, + 11602.651437647350124, 9842.7148383839780218, 1.0765576773720192317e-8) +_D = (22.266688044328115691, 235.38790178262499861, 1519.377599407554805, + 6485.558298266760755, 18615.571640885098091, 34900.952721145977266, + 38912.003286093271411, 19685.429676859990727) +_P = (0.21589853405795699, 0.1274011611602473639, 0.022235277870649807, + 0.001421619193227893466, 2.9112874951168792e-5, 0.02307344176494017303) +_Q = (1.28426009614491121, 0.468238212480865118, 0.0659881378689285515, + 0.00378239633202758244, 7.29751555083966205e-5) + + +def _do_del(x: float, temp: float) -> tuple[float, float]: + xsq = math.ldexp(math.trunc(math.ldexp(x, 4)), -4) + delta = (x - xsq) * (x + xsq) + cum = math.exp(-xsq * math.ldexp(xsq, -1)) * math.exp(-math.ldexp(delta, -1)) * temp + return cum, 1.0 - cum + + +def pnorm(x: float) -> float: + """R ``pnorm(x, 0, 1, lower.tail = TRUE, log.p = FALSE)`` (``pnorm_both``).""" + if math.isnan(x): + return x + if math.isinf(x): + return 0.0 if x < 0 else 1.0 + eps = _DBL_EPSILON * 0.5 + y = abs(x) + if y <= 0.67448975: + if y > eps: + xsq = x * x + xnum = _A[4] * xsq + xden = xsq + for i in range(3): + xnum = (xnum + _A[i]) * xsq + xden = (xden + _B[i]) * xsq + else: + xnum = xden = 0.0 + temp = x * (xnum + _A[3]) / (xden + _B[3]) + return 0.5 + temp + if y <= _M_SQRT_32: + xnum = _C[8] * y + xden = y + for i in range(7): + xnum = (xnum + _C[i]) * y + xden = (xden + _D[i]) * y + temp = (xnum + _C[7]) / (xden + _D[7]) + cum, ccum = _do_del(y, temp) + return ccum if x > 0.0 else cum + if -38.4674 < x < 8.2924: + xsq = 1.0 / (x * x) + xnum = _P[5] * xsq + xden = xsq + for i in range(4): + xnum = (xnum + _P[i]) * xsq + xden = (xden + _Q[i]) * xsq + temp = xsq * (xnum + _P[4]) / (xden + _Q[4]) + temp = (_M_1_SQRT_2PI - temp) / y + cum, ccum = _do_del(x, temp) + return ccum if x > 0.0 else cum + return 1.0 if x > 0 else 0.0 + + +@functools.cache +def _libm_lgamma() -> Callable[[float], float]: + """The C library's ``lgamma``, as R's C++ kernels call it (``std::lgamma``). + + CPython's ``math.lgamma`` is its own Lanczos approximation and differs from + the C library's in the last bit for about half of all integer arguments. + Falls back to ``math.lgamma`` where no C math library can be loaded. + """ + names = [ctypes.util.find_library("m"), ctypes.util.find_library("c"), "ucrtbase"] + for name in names: + if not name: + continue + try: + fn = ctypes.CDLL(name).lgamma + except (OSError, AttributeError): + continue + fn.restype = ctypes.c_double + fn.argtypes = [ctypes.c_double] + if fn(1.0) == 0.0 and abs(fn(10.0) - math.lgamma(10.0)) < 1e-12: + return lambda x: float(fn(x)) + return math.lgamma + + +def clgamma(x: float) -> float: + """``std::lgamma(x)`` as evaluated by R's compiled code on this platform.""" + return _libm_lgamma()(x) + diff --git a/src/nns/_stack_class_r14.py b/src/nns/_stack_class_r14.py index e6418b95..f653ad1f 100644 --- a/src/nns/_stack_class_r14.py +++ b/src/nns/_stack_class_r14.py @@ -18,6 +18,7 @@ from nns._class_r14 import classify as classify_r14 from nns._reg_engine import _dependence, _dimred_coefficients +from nns._rmean import r_seq_by F = NDArray[np.float64] Index = NDArray[np.int64] @@ -94,7 +95,7 @@ def choose(probabilities: F, threshold: float = 0.5) -> F: def evaluate(probabilities: F, actual: F) -> tuple[float, float]: thresholds = ( - np.arange(1, 100, dtype=np.float64) / 100.0 + r_seq_by(0.01, 0.99, 0.01) # R: seq(.01, .99, .01) if class_count == 2 and optimize_threshold else np.asarray([0.5]) ) diff --git a/src/nns/norm.py b/src/nns/norm.py index 4dce667b..9d462cb0 100644 --- a/src/nns/norm.py +++ b/src/nns/norm.py @@ -2,12 +2,13 @@ import warnings from collections.abc import Sequence -from typing import Any, cast, overload +from typing import Any, overload import numpy as np from numpy.typing import NDArray from nns._helpers import _as_matrix +from nns._rmean import r_col_means, r_cor_matrix, r_mean from nns.dependence import nns_dep @@ -83,6 +84,10 @@ def nns_norm( ) return _norm_unequal_series(series) values = _as_matrix(np.column_stack(series), "x") + normalized = _norm_matrix(values, linear, list_input=True) + if plot: + _plot_norm(values, normalized) + return normalized normalized = _norm_matrix(values, linear) if plot: _plot_norm(values, normalized) @@ -99,17 +104,23 @@ def _plot_norm(values: NDArray[np.float64], normalized: NDArray[np.float64]) -> plot_nns_norm(normalized, ax=bottom) -def _norm_matrix(values: NDArray[np.float64], linear: bool) -> NDArray[np.float64]: - means = np.mean(values.astype(np.longdouble), axis=0).astype(np.float64) - means = means.copy() +def _norm_matrix( + values: NDArray[np.float64], linear: bool, list_input: bool = False +) -> NDArray[np.float64]: + # R: m <- colMeans(X) (list input: sapply(X, mean)); RG <- m %o% (1 / m); + # scales <- colMeans(RG [* scale.factor]). + if list_input: + means = np.array([r_mean(values[:, j]) for j in range(values.shape[1])]) + else: + means = r_col_means(values) means[means == 0.0] = 1e-10 ratio_grid = means[:, np.newaxis] * (1.0 / means[np.newaxis, :]) if linear: - scales = np.mean(ratio_grid, axis=0) + scales = r_col_means(ratio_grid) else: scale_factor = _scale_factor(values) - scales = np.mean(ratio_grid * scale_factor, axis=0) + scales = r_col_means(ratio_grid * scale_factor) # Annotated assignment instead of cast(): under mypy targeting 3.12+ the # numpy stubs type this product precisely, making an explicit cast redundant @@ -120,16 +131,16 @@ def _norm_matrix(values: NDArray[np.float64], linear: bool) -> NDArray[np.float6 def _norm_unequal_series(series: list[NDArray[np.float64]]) -> list[NDArray[np.float64]]: - means = np.array([float(np.mean(item)) for item in series]) + means = np.array([r_mean(item) for item in series]) # R: sapply(X, mean) means[means == 0.0] = 1e-10 ratio_grid = means[:, np.newaxis] * (1.0 / means[np.newaxis, :]) - scales = np.mean(ratio_grid, axis=0) + scales = r_col_means(ratio_grid) return [item * scale for item, scale in zip(series, scales, strict=True)] def _scale_factor(values: NDArray[np.float64]) -> NDArray[np.float64]: if values.shape[1] < 10: - return cast(NDArray[np.float64], np.abs(np.corrcoef(values, rowvar=False))) + return np.abs(r_cor_matrix(values)) # R: abs(cor(X_mat)) n_variables = values.shape[1] deps = np.eye(n_variables, dtype=np.float64) diff --git a/src/nns/stack.py b/src/nns/stack.py index 5c6e8685..1137baf1 100644 --- a/src/nns/stack.py +++ b/src/nns/stack.py @@ -48,6 +48,7 @@ split_columns, ) from nns._native import external_stacklevel, honours_ncores +from nns._rblas import r_matvec from nns._reg_engine import ( _mreg_predict, _mreg_predict_path, @@ -56,6 +57,7 @@ _validate_order, nns_reg_engine, ) +from nns._rmean import r_cor, r_rank_average, r_round, r_row_means, r_seq_by from nns._rrng import RRNG from nns.central_tendencies import nns_gravity from nns.dependence import nns_dep @@ -85,85 +87,22 @@ def _rank_average(values: NDArray[np.float64]) -> NDArray[np.float64]: - """Average ranks of tied values (R's rank(ties.method = "average")). - - Runs of equal sorted values [start, end) get (start + 1 + end) / 2, which is - exact in float64; NaNs compare unequal, so each forms its own run. - """ - order = np.argsort(values, kind="mergesort") - sorted_values = values[order] - ranks = np.empty(values.size, dtype=np.float64) - if values.size == 0: - return ranks - breaks = np.flatnonzero(sorted_values[1:] != sorted_values[:-1]) + 1 - starts = np.concatenate(([0], breaks)) - ends = np.concatenate((breaks, [values.size])) - ranks[order] = np.repeat((starts + 1 + ends) / 2.0, ends - starts) - return ranks - - -def _pearson(a: NDArray[np.float64], b: NDArray[np.float64]) -> float: - a = a - a.mean() - b = b - b.mean() - denom = math.sqrt(float(a @ a) * float(b @ b)) - if denom == 0.0: - return float("nan") - return float(a @ b) / denom - - -def _spearman_signed(x: NDArray[np.float64], y: NDArray[np.float64]) -> NDArray[np.float64]: - """Signed Spearman correlations, R cor(method='spearman') semantics.""" - y_rank = _rank_average(y) - out = np.empty(x.shape[1], dtype=np.float64) - for col in range(x.shape[1]): - out[col] = _pearson(_rank_average(x[:, col]), y_rank) - out[~np.isfinite(out)] = 0.0 - return out - - -def _r_column_means(m: NDArray[np.float64]) -> NDArray[np.float64]: - """Column means as R's cov.c MEAN macro: long-double sum, then refinement.""" - ld = m.astype(np.longdouble) - n = np.longdouble(m.shape[0]) - mean = np.cumsum(ld, axis=0)[-1] / n - refined = mean + np.cumsum(ld - mean, axis=0)[-1] / n - return np.asarray(np.where(np.isfinite(mean), refined, mean), dtype=np.float64) - + """R ``rank(x)`` with average ties.""" + return r_rank_average(values) def _r_spearman_cor(x: NDArray[np.float64], y: NDArray[np.float64]) -> NDArray[np.float64]: """``stats::cor(x[, j], y, method = "spearman")`` for every column of ``x``. - Reproduces R's C_cor rounding (cov.c, two-argument complete-case path): - double deviations from long-double means, products summed sequentially in - long double, long-double square roots. Columns with zero spread give NaN. + Pearson correlation of average ranks with R's cov.c arithmetic (see + :func:`nns._rmean.r_cor`). Columns with zero spread give NaN. """ - n = x.shape[0] - if n < 2: - return np.full(x.shape[1], np.nan) - rx = np.column_stack([_rank_average(x[:, col]) for col in range(x.shape[1])]) - ry = _rank_average(y) - dx = rx - _r_column_means(rx) - dy = ry - _r_column_means(ry[:, None])[0] - n1 = np.longdouble(n - 1) - - def ld_sum(values: NDArray[np.float64]) -> NDArray[np.longdouble]: - return cast(NDArray[np.longdouble], np.cumsum(values.astype(np.longdouble), axis=0)[-1]) - - cov = (ld_sum(dx * dy[:, None]) / n1).astype(np.float64) - sd_x = np.sqrt(ld_sum(dx * dx) / n1).astype(np.float64) - sd_y = float(np.sqrt(ld_sum(dy * dy) / n1)) - with np.errstate(divide="ignore", invalid="ignore"): - out = np.clip(cov / (sd_x * sd_y), -1.0, 1.0) - out[(sd_x == 0.0) | (sd_y == 0.0)] = np.nan - return np.asarray(out, dtype=np.float64) + y_rank = r_rank_average(y) + return np.array([r_cor(r_rank_average(x[:, col]), y_rank) for col in range(x.shape[1])]) def _r_matvec(design: NDArray[np.float64], coef: NDArray[np.float64]) -> NDArray[np.float64]: """``design %*% coef`` in R's reference-BLAS dgemv order (column by column).""" - out = np.zeros(design.shape[0], dtype=np.float64) - for j in np.flatnonzero(coef != 0.0): - out = out + coef[j] * design[:, j] - return out + return r_matvec(design, coef) def _r_xstar_path(design: NDArray[np.float64], coef: NDArray[np.float64]) -> NDArray[np.float64]: @@ -429,7 +368,7 @@ def round_codes(raw: NDArray[np.float64], threshold: float) -> NDArray[np.float6 def best_threshold(raw: NDArray[np.float64], actual: NDArray[np.float64]) -> float: if not is_class or not optimize_threshold: return 0.5 - grid = np.round(np.arange(0.01, 1.0, 0.01), 2) + grid = r_seq_by(0.01, 0.99, 0.01) # R: seq(0.01, 0.99, by = 0.01) scores = np.asarray([score(round_codes(raw, th), actual) for th in grid]) valid = np.flatnonzero(np.isfinite(scores)) if valid.size == 0: @@ -614,7 +553,8 @@ def make_splits() -> list[dict[str, NDArray[np.int64]]]: holdout_size = cv_size if holdout_size is None and folds_value == 1: - holdout_size = round(float(rng.runif(0.20, 1.0 / 3.0)), 3) + # R: round(stats::runif(1L, 0.20, 1 / 3), 3L), with R's rounding rule. + holdout_size = r_round(float(rng.runif(0.20, 1.0 / 3.0)), 3) if holdout_size is not None: out = [] @@ -732,8 +672,8 @@ def caus_coef() -> NDArray[np.float64]: elif dim_red_value == "all": # R: rowMeans(cbind(caus, dep, cor, rep(1, p))) - the equal-weight # term is part of the average. - coef = np.mean( - np.column_stack((caus_coef(), dep_coef(), cor_coef(), np.ones(p))), axis=1 + coef = r_row_means( + np.column_stack((caus_coef(), dep_coef(), cor_coef(), np.ones(p))) ) else: raise ValueError("Unsupported [dim.red.method].") @@ -1175,7 +1115,7 @@ def score_method1_candidate(cid: str) -> bool: valid = np.isfinite(reg_oof_raw) & np.isfinite(dim_oof_raw) if not np.any(valid): raise ValueError("The two component models have no common finite OOF predictions.") - weight_grid = np.round(np.arange(0.0, 1.0 + 1e-9, 0.01), 2) + weight_grid = r_seq_by(0.0, 1.0, 0.01) # R: seq(0, 1, by = 0.01) blend_scores = np.full(weight_grid.size, np.nan) blend_thresholds = np.full(weight_grid.size, 0.5) for i, w in enumerate(weight_grid.tolist()): diff --git a/src/nns/var.py b/src/nns/var.py index 090a8b06..27dadb57 100644 --- a/src/nns/var.py +++ b/src/nns/var.py @@ -17,6 +17,7 @@ from nns._helpers import _warn_unsupported from nns._native import honours_ncores +from nns._rmean import r_cor_matrix, r_quantile, r_rank_average, r_round_array from nns.core import lpm_ratio, upm_ratio ObjectiveFn = Callable[[NDArray[np.float64], NDArray[np.float64]], float] @@ -752,7 +753,6 @@ def _var_relevant( """ from nns.central_tendencies import nns_gravity from nns.regression import nns_reg - from nns.stack import _spearman_signed x = np.asarray(ivs_train, dtype=np.float64) y = np.asarray(dv_train, dtype=np.float64) @@ -769,13 +769,18 @@ def _var_relevant( def coefficients(xs: NDArray[np.float64], ys: NDArray[np.float64]) -> NDArray[np.float64]: if dim_red_method == "cor": - return _spearman_signed(xs, ys) + # R: cor(data.matrix(cbind(y, x)), method = "spearman")[-1, 1], whose + # zero-spread columns are NA (kept and then zeroed with the cutoffs). + columns = [r_rank_average(ys)] + columns += [r_rank_average(xs[:, j]) for j in range(xs.shape[1])] + ranks = np.column_stack(columns) + return r_cor_matrix(ranks)[1:, 0] fit = nns_reg(xs, ys, dim_red_method=dim_red_method, point_only=True, **reg_args) return np.asarray(fit["equation"]["Coefficient"], dtype=np.float64)[:n] def rounded(values: NDArray[np.float64]) -> NDArray[np.float64]: - # R abs(round(x, 2)): Python's round() also rounds the exact binary value. - return np.array([np.nan if np.isnan(v) else abs(round(float(v), 2)) for v in values]) + # R abs(round(x, digits = 2)), with R's own rounding rule (nmath fround). + return np.abs(r_round_array(np.asarray(values, dtype=np.float64), 2)) split = n_rows - ts_test cv_x, cv_x_test = x[:split], x[split:] @@ -992,7 +997,8 @@ def _partial_moment_var( # for the quantile and the integer-degree inversion. lpm_pcts = pcts if lower else 1.0 - pcts if degree == 0: - out = np.asarray(np.quantile(values, lpm_pcts, method="linear"), dtype=np.float64) + # R: stats::quantile(x, percentile) (type 7). + out = r_quantile(values, lpm_pcts, 7) elif _is_supported_integer_degree(degree): solve = _lpm_var_integer_solver(int(degree), values) out = np.array([solve(float(p)) for p in lpm_pcts], dtype=np.float64) diff --git a/sync/nns_source.json b/sync/nns_source.json index c473530c..4f6d29a8 100644 --- a/sync/nns_source.json +++ b/sync/nns_source.json @@ -28,11 +28,11 @@ }, { "path": "src/nns/_r14_dependence.hpp", - "sha256": "782ffb312545d722a86ff32994a17c600fba65ba70bc9c7ecf647f37e291e47b", + "sha256": "4babec2c922cc5a50396cd2154fff48b0e88c43449de83da46aec4d4e1cf848d", "r_source_path": "src/NNS_dep.cpp", "r_source_sha256": "2e577f57165fd38d779dcde369eabf4800135eb50e07079069c11be2fedaa115", "r_source_commit": "7b07a837c130ba7220dbe16c584221508757d5ae", - "adaptation": "R14 multilevel/null-corrected dependence with vector marshalling, erfc normal probabilities, and long-double binomial hypergeometric initialization. Bound explicitly instead of the untouched vendor dependence estimator." + "adaptation": "R14 multilevel/null-corrected dependence with vector marshalling. R::dhyper and R::pnorm are the ports of R's own nmath routines in src/nns/_rnmath.hpp, so the null expectations match R's bit for bit. Bound explicitly instead of the untouched vendor dependence estimator." } ], "r_cache_summary": { @@ -40,5 +40,22 @@ "shards": 80, "entries": 2764, "generation": "Pinned live R 14.0 only; no Python-authored expected values." - } + }, + "r_nmath_ports": [ + { + "path": "src/nns/_rnmath.hpp", + "sha256": "1e074f7aab24c9f4e1762f5c2641bf283e52a2aaf5e1b95f8a5c05cd29318556", + "python_mirror": "src/nns/_rnmath.py", + "r_version": "4.5.0", + "r_sources": [ + "src/nmath/dhyper.c", + "src/nmath/dbinom.c", + "src/nmath/stirlerr.c", + "src/nmath/bd0.c", + "src/nmath/pnorm.c" + ], + "license": "GPL-2 or later (R Core Team), redistributed under GPL-3.0-only", + "notes": "dhyper/dbinom_raw/stirlerr/bd0/pow1p and the lower tail of pnorm_both, as called by R::dhyper and R::pnorm in src/NNS_dep.cpp; stirlerr follows the R >= 4.4.0 series cutoffs." + } + ] } diff --git a/tests/_r.py b/tests/_r.py index 5b66265e..3a80984a 100644 --- a/tests/_r.py +++ b/tests/_r.py @@ -919,7 +919,9 @@ def nns_cdf_custom( return result -def nns_frame_call(label: str, expr: str, data: dict[str, Any]) -> RValue: +def nns_frame_call( + label: str, expr: str, data: dict[str, Any], *, exact: bool = False +) -> RValue: """Evaluate one R expression over typed data frames / vectors. ``data`` maps an R variable name to a column spec (see :func:`r_column`) @@ -928,8 +930,15 @@ def nns_frame_call(label: str, expr: str, data: dict[str, Any]) -> RValue: tests can exercise R's name matching and factor/character handling. Console output of ``expr`` is discarded; named atomic vectors come back as ``{"names": [...], "values": [...]}``. + + ``exact=True`` stores numbers with 17 significant digits, which round-trip + every double, so a test can require bit-for-bit agreement with R; the + default 15 digits suit tolerance comparisons. Inputs are sent with full + precision either way. """ - args = {"expr": expr, "data": data} + args: dict[str, Any] = {"expr": expr, "data": data} + if exact: + args["exact"] = True key = _cache_key(label, (args,)) cache, refresh = _cache_state() if key in cache: @@ -2132,7 +2141,8 @@ def _call_r_frame(args: dict[str, Any]) -> RValue: " }\n" " as.numeric(x)\n" "}\n" - "cat(jsonlite::toJSON(encode(result), auto_unbox = TRUE, digits = NA, " + "digits <- if (isTRUE(spec$exact)) I(17) else NA\n" + "cat(jsonlite::toJSON(encode(result), auto_unbox = TRUE, digits = digits, " "null = 'null', na = 'string'))\n" ) completed = subprocess.run( diff --git a/tests/_r_cache/rarith.blas.json b/tests/_r_cache/rarith.blas.json new file mode 100644 index 00000000..a20a6740 --- /dev/null +++ b/tests/_r_cache/rarith.blas.json @@ -0,0 +1,73 @@ +{ + "entries": { + "79110cf29d3bd6942b44d748b3bb8519f0032695c2ee3f9f865a381478f34d46": [ + -0.6107450282931098, + -1.5792723368877377, + 0.3981147879823206, + 0.17968371410489126, + 1.056842905651877, + -0.05904479981876998, + -0.14142576682287666, + 1.3108918163451737, + -0.6023722150044779, + 1.7925989466194023, + 0.48102514531280494, + 0.6480008270945398, + 0.5172635676621442, + 1.0994828683619107, + -0.8976432486888525, + -0.43005829995247763, + -0.8443899623307349, + 1.4368482611479076, + -0.5698279065094706, + -1.1150835103279764, + 1.7433437037565955, + 1.4063587259768335, + 0.40921591595096296, + -0.37329277967049523, + 0.4397859728045299, + -0.7637628754872963, + 1.7258689840650003, + 0.869181272801941, + -0.20640735475585514, + 1.454368694626093, + 0.41596180063145644, + 0.5825624334853325, + 1.3382722060817371, + -1.1170587173754887, + 0.4849854531488751, + 0.1673773740518424, + 2.0686309819369333, + 1.6741654478749992, + 1.619020574317841, + 1.5613233332308354, + 1.1028924533643094, + -1.1510963984613904, + -0.04554265177756911, + -0.4901449239127578, + -0.0705607856368442, + -1.0921298286577816, + 0.10440233026672197, + 0.7453782070594914, + 0.5413155497633126, + 0.7736893296967466, + 1.6421933113727047, + 1.0188860851540718, + 0.03555956278729688, + 0.6021492903233844, + 0.07641049149099702, + 0.32198611583207515, + -0.22631912729387077, + -0.06070717436186812, + -0.37604740115873503, + 1.296010245100336, + 1.2948943510100657, + 1.7214356322763131, + 1.4382556227841103, + 2.0607387710968768 + ] + }, + "label": "rarith.blas", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.cor.json b/tests/_r_cache/rarith.cor.json new file mode 100644 index 00000000..53de85c7 --- /dev/null +++ b/tests/_r_cache/rarith.cor.json @@ -0,0 +1,43 @@ +{ + "entries": { + "3119359bd16437a1fabfd38308c987c2833136e93ba9f6225e1bff5d9dd97009": [ + -0.1902278397239263, + -0.07564018558151743, + 1.0, + -0.1902278397239263, + 0.06550105362684461, + -0.05592464988183996, + -0.1902278397239263, + 1.0, + 0.042859044780389974, + 0.041976927639579276, + 0.06550105362684461, + 0.042859044780389974, + 1.0, + -0.13328125109103586, + -0.05592464988183996, + 0.041976927639579276, + -0.13328125109103586, + 1.0, + 1.0, + 0.10633474665211366, + 0.08395953150581419, + -0.015386861007810494, + 0.10633474665211366, + 1.0, + -0.061794215188279246, + 0.10599715726335596, + 0.08395953150581419, + -0.061794215188279246, + 1.0, + -0.07564018558151744, + -0.015386861007810494, + 0.10599715726335596, + -0.07564018558151744, + 1.0 + ] + }, + "label": "rarith.cor", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.dep.json b/tests/_r_cache/rarith.dep.json new file mode 100644 index 00000000..77c7a8c6 --- /dev/null +++ b/tests/_r_cache/rarith.dep.json @@ -0,0 +1,63 @@ +{ + "entries": { + "09f57dc631830be72c5aae4e257824e5ffb177ec94710b687c21afd05680c31c": [ + 0.02092081022221376, + 0.939995549302242 + ], + "177d92f25f078fdc55c2fd2bbd3e7d2fd6886e9ecab3c5fc95b598c4e35629e4": [ + -0.5869881027588373, + 0.5869881027588373 + ], + "2cacca065f69f4b5363cb481c4ef30fd29f08f7bddcba7d47e3b8fc489d1dc64": [ + 0.11401447428599842, + 0.11401447428599842 + ], + "39f94e03ae041fd9bfe1eeb728dbc67e007123f5ddc945eda65d83d3b6f3f3c2": [ + -0.019427112197968777, + 0.8820578457700496 + ], + "4156bf673a93782138c1680be682790550a0a9acdd2f318c0d52d8ce69e89264": [ + 0.9248010582525601, + 0.9248010582525601 + ], + "65fdb68e04909f5d8836c20eff9d207d603b4d736cfe8b391472ba0e39ad0146": [ + 0.92480105825256, + 0.92480105825256 + ], + "685f73dcb530cc09c69a3417f7b023142834154f0bf99719f02a5ae23f4dd05a": [ + -0.04001080836482708, + 0.04001080836482708 + ], + "a1be2c89d470ff29deedeff43eb2079730a0580dad0b5da538f6cf92c8e68fbd": [ + 0.003832322148350005, + 0.01229477002488083 + ], + "b66122a621ec364c255cedc41ee127697ba5e2a8d107785648fc2054e195bb78": [ + 0.02092081022221376, + 0.939995549302242 + ], + "b952fbf80aaabfa56ff24ebbcc34e53bf4c313ace7a82ce62b8588828a2b9168": [ + 0.9111698815228297, + 0.9111698815228297 + ], + "cbc7b437f85bf2ae02a59d30ba01ee3f39aa219c81f054b01c8a5765952b8578": [ + 0.043892429897344085, + 0.043892429897344085 + ], + "d5bffbbc72e03e18b8ec165d2a41eef34c594beb8939a0f6eaa732671b21fc7e": [ + -0.5869881027588373, + 0.5869881027588373 + ], + "e0972a9d2a0d370493b5a24e07003bff34a673a102e91060390a5846114e0710": [ + 0.9111698815228297, + 0.9111698815228297 + ], + "e8be6de41bb2270baf9590c51aad625bf460cb1096cda1b1cc2467da5440fc1f": [ + -0.019427112197968777, + 0.8820578457700496 + ] + }, + "label": "rarith.dep", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.dhyper.json b/tests/_r_cache/rarith.dhyper.json new file mode 100644 index 00000000..045bd930 --- /dev/null +++ b/tests/_r_cache/rarith.dhyper.json @@ -0,0 +1,612 @@ +{ + "entries": { + "7a8198cbaf8c96743de53e876b7e267d8ed346d79ec80cd2cdffe65b4047741f": [ + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 0.5000000000000001, + 1.0, + 0.6666666666666667, + 0.3333333333333333, + 0.7500000000000001, + 0.8, + 0.24999999999999994, + 0.30000000000000016, + 0.3333333333333333, + 0.16666666666666663, + 0.2307692307692309, + 0.5714285714285715, + 0.42857142857142866, + 0.5333333333333333, + 0.21052631578947367, + 0.23809523809523808, + 0.19047619047619035, + 0.75, + 0.42857142857142855, + 1.0, + 0.16666666666666666, + 0.7142857142857142, + 0.28571428571428553, + 0.7777777777777778, + 0.06666666666666667, + 0.018181818181818188, + 0.06593406593406594, + 1.0, + 0.18300653594771243, + 0.05928853754940711, + 0.15669515669515668, + 0.3126436781609196, + 1.0, + 0.25, + 1.0, + 0.0833333333333333, + 0.09999999999999985, + 0.446078431372549, + 0.2022058823529411, + 0.08152173913043478, + 0.0004940711462450594, + 0.8888888888888888, + 0.017094017094017092, + 0.08965517241379312, + 0.3570564516129033, + 0.00015278838808250578, + 0.027887901572112093, + 0.3333333333333333, + 0.039682539682539805, + 1.0, + 0.011695906432748536, + 0.0014116318464144552, + 0.030103995621236973, + 0.4880422691879866, + 0.01747311827956989, + 1.0, + 0.5971008838148232, + 0.12121212121212128, + 1.0, + 0.0003869969040247679, + 6.449948400412796e-05, + 0.0016642396505096727, + 0.11976269929551353, + 0.020196020196020185, + 0.03571428571428572, + 5.8944886531093324e-05, + 0.009202143796137114, + 0.00568990042674253, + 0.0004470636049583416, + 0.02777777777777778, + 0.5882352941176471, + 1.0, + 0.08625442330375928, + 0.11111111111111112, + 0.022222222222222223, + 0.14285714285714288, + 0.0008741258741258743, + 0.014705882352941168, + 5.7474211763291205e-06, + 1.0, + 1.0, + 1.0, + 0.016253869969040245, + 0.12406015037593986, + 0.5454545454545453, + 0.0347826086956521, + 0.11029411764705876, + 0.07505888896159138, + 0.25, + 0.00016534888614977636, + 0.005928853754940707, + 0.09777777777777788, + 0.5625, + 1.221285740316549e-09, + 0.022073578595317736, + 0.0539772727272727, + 1.0, + 6.93433187712364e-05, + 0.005722779635823115, + 0.5555555555555557, + 1.0, + 0.07000000000000005, + 5.296890725144332e-05, + 1.6757766914946381e-09, + 0.0006274778243632522, + 0.012096774193548382, + 2.285524210470056e-06, + 0.0005473453749315823, + 3.361065015397137e-08, + 4.65378232901143e-08, + 0.006451612903225808, + 6.041172439704758e-08, + 0.019230769230769246, + 1.0, + 6.020410248852401e-10, + 3.283835426327818e-25, + 2.9750458915525814e-05, + 0.006736750972752584, + 0.002258222876779578, + 1.9021626160787747e-55, + 5.620198400106224e-60, + 1.4697334128492425e-46, + 0.5000000000000001, + 0.3333333333333333, + 0.6666666666666667, + 0.25, + 0.2, + 0.7500000000000003, + 0.7, + 0.6666666666666669, + 0.833333333333333, + 0.7692307692307689, + 0.42857142857142866, + 0.5714285714285715, + 0.4666666666666665, + 0.7894736842105263, + 0.7619047619047619, + 0.8095238095238093, + 0.25, + 0.5714285714285716, + 0.6666666666666665, + 0.4, + 0.28571428571428575, + 0.5714285714285714, + 0.22222222222222224, + 0.46666666666666673, + 0.32727272727272727, + 0.43956043956043944, + 0.522875816993464, + 0.4031620553359684, + 0.5014245014245015, + 0.5080459770114941, + 0.7500000000000001, + 0.4166666666666667, + 0.4000000000000001, + 0.446078431372549, + 0.47181372549019607, + 0.3532608695652173, + 0.031126482213438736, + 0.11111111111111112, + 0.17094017094017094, + 0.35862068965517246, + 0.4590725806451614, + 0.014667685255920554, + 0.20393028024606968, + 0.6666666666666666, + 0.07142857142857138, + 0.4545454545454547, + 0.42857142857142855, + 0.388888888888889, + 0.4117647058823528, + 0.888888888888889, + 0.35555555555555557, + 0.5142857142857142, + 0.000370218017276841, + 0.41353383458646625, + 0.4545454545454546, + 0.48529411764705854, + 0.7500000000000001, + 3.4021705848331225e-05, + 0.0016339869281045741, + 0.37333333333333346, + 0.43750000000000006, + 0.00412796697626419, + 0.4444444444444443, + 0.42000000000000004, + 2.8956550695681063e-06, + 1.3203089084503208e-09, + 1.5376926921154814e-06, + 6.105006105006106e-05, + 0.07407407407407407, + 3.3241302350081722e-09, + 0.18064516129032265, + 0.26153846153846155, + 1.0, + 0.16666666666666666, + 0.5999999999999999, + 1.0, + 0.14285714285714285, + 1.0, + 0.46666666666666673, + 0.6545454545454547, + 0.49450549450549436, + 0.29411764705882354, + 1.0, + 0.5375494071146245, + 0.341880341880342, + 0.1793103448275863, + 0.7500000000000002, + 0.47619047619047616, + 0.1666666666666667, + 0.3649122807017544, + 0.21597967250141148, + 0.394088669950739, + 0.09760845383759736, + 0.3621700879765397, + 0.06605008069511867, + 0.28571428571428553, + 0.42857142857142855, + 0.11738906088751293, + 0.06772445820433441, + 0.17474516330351564, + 0.3522432332220986, + 0.3197703197703199, + 0.42857142857142855, + 0.535714285714286, + 0.000254647313470843, + 0.5833333333333335, + 0.6222222222222221, + 0.00037526972511492656, + 2.0819329855513853e-05, + 0.04545454545454549, + 0.3428571428571429, + 0.3529411764705882, + 0.34829721362229105, + 0.4044117647058824, + 0.0007223942208462337, + 0.25691699604743085, + 0.38628762541806017, + 0.3704322638146168, + 8.725841866896426e-09, + 0.09287925696594428, + 0.24526198439241917, + 0.51, + 0.02597402597402598, + 0.33870967741935487, + 0.1510673234811166, + 0.9259259259259258, + 0.8129032258064516, + 0.7192307692307695, + 0.18878743249877278, + 0.25000000000000006, + 0.0833333333333333, + 1.0, + 0.09999999999999985, + 0.004901960784313726, + 0.042892156862745105, + 0.14130434782608692, + 0.657114624505929, + 0.34798534798534797, + 0.13793103448275856, + 0.016935483870967754, + 0.7578304048892286, + 0.31233766233766225, + 0.7999999999999999, + 0.5555555555555555, + 0.1, + 0.6666666666666669, + 0.5714285714285714, + 0.06060606060606059, + 1.0, + 0.5714285714285714, + 0.15679339817270854, + 0.34381636161391443, + 0.33264033264033266, + 0.21794350741719154, + 0.20455594459389514, + 0.3181818181818181, + 0.3671328671328671, + 0.36363636363636365, + 0.09022556390977454, + 0.2918820310124658, + 0.21727573120460655, + 0.2549019607843138, + 0.12444444444444451, + 0.0007036307345904866, + 0.1941612925594617, + 8.251808048504144e-06, + 0.3116883116883117, + 0.2554731142050386, + 0.04700854700854702, + 1.0, + 0.19999999999999998, + 1.0, + 0.007936507936507938, + 0.27777777777777773, + 0.11946532999164554, + 0.2879728966685487, + 0.07662835249042153, + 0.00013904338153503893, + 0.11840175953079164, + 0.9000000000000001, + 0.00017110055974743792, + 0.33333333333333337, + 0.8333333333333333, + 0.1428571428571429, + 0.07142857142857138, + 0.3846153846153846, + 0.217391304347826, + 0.2000000000000002, + 0.04999999999999998, + 0.11077158135981671, + 0.15909090909090925, + 0.33333333333333326, + 0.1590909090909093, + 0.06293706293706292, + 0.010835913312693497, + 0.10992977535541182, + 0.6363636363636364, + 2.5491286614757132e-05, + 0.091304347826087, + 0.46955624355005166, + 0.00031684562577011087, + 0.6623376623376623, + 0.14626099706744877, + 0.3877394636015325, + 0.004901960784313727, + 3.293322197550942e-54, + 0.3451153657339225, + 0.16666666666666663, + 1.0, + 0.6153846153846152, + 1.0, + 0.1291279669762643, + 0.1936919504643964, + 0.08924485125858118, + 0.7826086956521732, + 0.8000000000000004, + 0.0014831294030404163, + 0.026928026928026916, + 0.37999999999999956, + 0.47727272727272746, + 1.0, + 0.5333333333333333, + 0.27319635988366653, + 0.10826051524867195, + 0.38181818181818183, + 0.15909090909090937, + 0.4772727272727272, + 0.2545454545454548, + 0.36281365693130424, + 0.014705882352941168, + 0.016253869969040245, + 0.2829245058347227, + 0.23366013071895428, + 0.3178534571723427, + 0.04710144927536237, + 0.007965686274509788, + 0.05410628019323674, + 0.2647385984427142, + 0.15712982669504402, + 0.12884327067235632, + 0.06734767635252174, + 0.08529411764705887, + 0.5699999999999998, + 0.11423519009725894, + 0.00920214379613712, + 0.46524064171123014, + 0.016632016632016626, + 0.07711847185531394, + 0.13333333333333333, + 0.509090909090909, + 0.5090909090909091, + 0.41958041958041953, + 0.010489510489510485, + 0.3250773993808049, + 0.0009121313469139563, + 0.000343518942615979, + 0.023839009287925688, + 0.23611820942188017, + 0.2708978328173375, + 0.0793287566742944, + 0.05264294474528068, + 0.24804337151389438, + 0.26794871794871794, + 0.14928734924808984, + 1.0, + 0.045454545454545484, + 1.0, + 1.165560937856949e-05, + 0.8888888888888891, + 0.10909090909090911, + 0.04545454545454548, + 0.17647058823529427, + 0.2111989166706968, + 0.013124242832144301, + 0.4210526315789474, + 0.18947368421052638, + 0.10102151452878261, + 0.5117647058823529, + 0.0525874628185413, + 0.11111111111111113, + 0.04024767801857589, + 1.0, + 0.1711869397369628, + 0.018181818181818202, + 0.3103448275862067, + 0.46323529411764697, + 0.6923076923076924, + 0.17784760845383757, + 0.023839009287925716, + 0.09470766987073378, + 1.0, + 0.5789473684210527, + 0.5210526315789473, + 0.3171624713958811, + 0.000986603472457318, + 0.0001509613856666137, + 0.01209677419354838, + 0.19643627334255, + 0.0004995004995004996, + 1.0, + 4.113533525298233e-05, + 0.021671826625387008, + 1.0, + 0.6896551724137924, + 0.010510176472356665, + 1.0, + 0.3076923076923076, + 0.31785345717234254, + 1.0, + 0.28927066762947584, + 0.5263157894736843, + 0.2894736842105263, + 0.03718535469107547, + 0.24881369254344055, + 0.02431771127423304, + 0.23783661787240637, + 0.034767231768731005, + 0.3846153846153845, + 0.0514705882352941, + 0.7857142857142856, + 0.00018711938216582177, + 0.1746031746031746, + 0.0046439628482972135, + 0.005982762948707219, + 0.47368421052631576, + 0.03478260869565211, + 0.6153846153846154, + 8.126120473172752e-05, + 0.33870967741935487, + 0.001888985759953507, + 0.19298245614035078, + 0.2142857142857143, + 0.5079365079365078, + 0.19487512084093056, + 0.007449354501634796, + 5.296890725144332e-05, + 1.866956044853621e-05, + 0.0016907675194660784, + 0.0046097265229634605, + 0.8070175438596489, + 0.0007223942208462331, + 0.3174603174603175, + 1.0, + 1.5822859918638897e-06, + 0.22933924636711003, + 0.28396089036821304, + 0.001922196796338673, + 0.00010856239999732761, + 0.0009987740799754148, + 0.14626099706744877, + 0.0032934742306266283, + 1.0, + 0.2918820310124658, + 0.24862407505924458, + 0.0024694963515875653, + 0.263533328049457, + 1.4201110588400782e-05, + 0.6000000000000005, + 0.3259092594465231, + 0.09791587722622207, + 0.009213759213759243, + 0.23248053392658516, + 0.2758620689655174, + 0.008214923627768652, + 1.1511931781185592e-08, + 2.0395093756246e-06, + 0.4000000000000003, + 0.06703762586115529, + 0.00043216186152782344, + 0.000624089868941128, + 0.11381825721448365, + 0.5149425287356322, + 1.4735371515185944e-08, + 0.3084677419354839, + 0.3671845395983327, + 0.0009121313469139569, + 0.05297196314993863, + 0.23015572858731903, + 0.20919540229885059, + 0.16193181818181815, + 0.5080645161290323, + 3.7628803890834084e-07, + 0.32248157248157244, + 0.17302362764845586, + 0.007529523658555903, + 8.57593010671378e-07, + 0.4286430481283421, + 0.036657190752637105, + 0.18346774193548385, + 0.0015063032999629233, + 1.6757766914946381e-09, + 0.5413105413105415, + 6.448388708871366e-06, + 0.002122015915119365, + 0.3988603988603986, + 0.3231162196679436, + 0.031506339056523815, + 0.07603609625668448, + 0.05982905982905988, + 0.013828059678534933, + 0.8076923076923068, + 0.4967741935483872, + 5.452681628825046e-07, + 0.09643193291691625, + 3.347930218540404e-12, + 0.19230769230769226, + 1.0, + 0.0015410146989094369, + 0.4258064516129035, + 0.19386973180076617, + 0.07741935483870971, + 1.8694908441685885e-07, + 0.13894905927567264, + 0.001277139208173691, + 0.35319993099879243, + 1.0, + 6.482208929372443e-07, + 0.04707271210816602, + 2.4859948338736265e-11, + 0.1765999654993962, + 0.5376955903271694, + 1.0, + 0.3982930298719774, + 1.0, + 0.005529305084224532, + 2.156287735035363e-05, + 0.06401137980085352, + 1.5729567312509488e-09, + 5.413286979059627e-19, + 1.0, + 1.0, + 0.16719829877724623, + 0.0856334979840468, + 1.2711001292914166e-22, + 0.17068228597332302, + 0.3203463203463203, + 2.9045830515803293e-05, + 0.012265512265512271, + 3.303880819622581e-07, + 2.019550040413152e-05, + 2.6246825938530674e-08, + 8.97483168133487e-20, + 8.445115556661612e-70, + 8.529723399344586e-11, + 0.012534835804601841, + 0.006262789234865839, + 0.10407914592596813, + 0.4466666666666665, + 0.44666666666666666, + 0.10666666666666665, + 4.824753035507861e-25, + 1.1063175270256819e-13, + 4.4481386434078676e-13, + 4.754555544558533e-40, + 5.927166241607708e-31, + 2.8821530256953693e-61, + 6.2465174852630654e-27, + 0.009908849550597744, + 3.329277158394862e-05, + 0.08388809611351228, + 8.867388962950022e-08, + 0.07655673921443128, + 1.397967397056305e-48, + 4.3911386633935685e-44, + 2.41024529522204e-33, + 3.061818059550294e-116, + 1.1022996644131505e-08, + 0.008607007331955175, + 7.711647901273779e-23 + ] + }, + "label": "rarith.dhyper", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.dimred.fit.json b/tests/_r_cache/rarith.dimred.fit.json new file mode 100644 index 00000000..f670e9c0 --- /dev/null +++ b/tests/_r_cache/rarith.dimred.fit.json @@ -0,0 +1,609 @@ +{ + "entries": { + "34d701bf07d49fc07da54df55a2d2c2efbd67b781856ada181528746eb9bf145": [ + 0.37436333672077193, + 0.38928747535970465, + 0.10447279172596606, + 0.17032303485618006, + 0.29101445581604957, + 0.4435492294602747, + 0.371868060348378, + 0.11517718495944264, + 0.10412913839118537, + 0.3314426183679357, + 0.1567646159124609, + 0.07600390617365381, + 0.1369455637072105, + 0.435159185901896, + 0.16923423040130492, + 0.13958515416689746, + 0.35397460907742656, + 0.38281981584443553, + 0.4237071816850742, + 0.4289766814763792, + 0.38077877457319315, + 0.0886498140363877, + 0.3737289920795217, + 0.05613272617124301, + 0.3759744348951794, + 0.37125712422146195, + 0.329356908588371, + 0.4025376095854915, + 0.15499683549930396, + 0.377032155895958, + 0.17213676057422675, + 0.37065478229487786, + 0.40228288564936104, + 0.11428243644304587, + 0.13379211828273488, + 0.37088498574424467, + 0.13165678755521215, + 0.21144659860086842, + 0.314064471063046, + 0.17285347418874275, + 0.3976963037606905, + 0.31253961784749146, + 0.2249340639925973, + 0.11894438493254435, + 0.18758208110958943, + 0.3256776999987165, + 0.1408197371675271, + 0.14767674657754482, + 0.046542980536988444, + 0.12708701742016162, + 0.4113671548754269, + 0.39367321201681754, + 0.35448470444666946, + 0.12547014371347306, + 0.333315717336893, + 0.13888829304122333, + 0.38732034420740025, + 0.40681003003634225, + 0.33398167714252885, + 0.0522473234007014, + 0.08265962441562212, + 0.18958580602404, + 0.12713488505506454, + 0.16522580913009274, + 0.17170903719689987, + 0.1463234634698084, + 0.11159821786735186, + 0.3140277650872381, + 0.14876743097168357, + 0.15002655743803203, + 0.3733088813719076, + 0.36651092626959797, + 0.15222567710159848, + 0.3842466448643516, + 0.11503442041022653, + 0.12863015988281218, + 0.1533709830217543, + 0.3259057369174341, + 0.15002265491108943, + 0.3412419728028981, + 0.34642014966083945, + 0.054887378499918954, + 0.4006254096203265, + 0.04933556130484489, + 0.39776700928929376, + 0.45176409213411467, + 0.32812611868820546, + 0.4234251686368973, + 0.1502409252467897, + 0.40513694452784654, + 0.3341937617947712, + 0.10023277965211619, + 0.10784356931402561, + 0.3809470736248153, + 0.40537697990621085, + 0.12279174655081256, + 0.35471549775433564, + 0.12082420536901949, + 0.3764659780077009, + 0.39460199072156715, + 0.12928449038005463, + 0.43796354702731827, + 0.1383158850562505, + 0.3906118774836662, + 0.12215841984213986, + 0.0813266136260161, + 0.4011993027680693, + 0.37297938637527034, + 0.3597172745338679, + 0.17213989428882284, + 0.18225352716294563, + 0.1579707162268721, + 0.13181294682756936, + 0.12632859432605248, + 0.36411194063405367, + 0.12478265713959774, + 0.1276804629004066, + 0.37754315606044875, + 0.41475458943882587, + 0.3800172942497999, + 0.2580772164984652, + 0.06186409138785328, + 0.3321416837936152, + 0.3660232179207821, + 0.40021625599965344, + 0.39081966022514547, + 0.11621069644408388, + 0.07089299874623897, + 0.3730889749034058, + 0.11053231492540773, + 0.10838885742548832, + 0.40019756848336924, + 0.3616861566858674, + 0.1489926747842108, + 0.4098969337846998, + 0.14343692838178243, + 0.09366771749968576, + 0.17715674250267954, + 0.332748324734697, + 0.33807708508172, + 0.3763148701160834, + 0.11674264859314969, + 0.31810222969560703, + 0.3884470592665872, + 0.17309375707589403, + 0.17837491840581743, + 0.3313676553097264, + 0.3203967158199785, + 0.1129080580379029, + 0.09022925151809875, + 25.0, + 28.0, + 5.0, + 14.0, + 17.0, + 33.0, + 24.0, + 6.0, + 5.0, + 20.0, + 13.0, + 3.0, + 9.0, + 32.0, + 14.0, + 10.0, + 22.0, + 28.0, + 32.0, + 32.0, + 27.0, + 4.0, + 25.0, + 3.0, + 26.0, + 24.0, + 19.0, + 30.0, + 13.0, + 26.0, + 14.0, + 24.0, + 30.0, + 6.0, + 9.0, + 24.0, + 9.0, + 16.0, + 18.0, + 14.0, + 30.0, + 18.0, + 17.0, + 7.0, + 16.0, + 19.0, + 10.0, + 11.0, + 1.0, + 8.0, + 31.0, + 29.0, + 22.0, + 7.0, + 20.0, + 10.0, + 28.0, + 31.0, + 20.0, + 2.0, + 4.0, + 16.0, + 8.0, + 13.0, + 14.0, + 11.0, + 6.0, + 18.0, + 11.0, + 12.0, + 25.0, + 23.0, + 12.0, + 28.0, + 6.0, + 8.0, + 12.0, + 19.0, + 12.0, + 21.0, + 21.0, + 2.0, + 30.0, + 2.0, + 30.0, + 33.0, + 19.0, + 32.0, + 12.0, + 31.0, + 21.0, + 5.0, + 5.0, + 27.0, + 31.0, + 7.0, + 22.0, + 7.0, + 26.0, + 29.0, + 8.0, + 32.0, + 9.0, + 29.0, + 7.0, + 4.0, + 30.0, + 25.0, + 22.0, + 14.0, + 15.0, + 13.0, + 9.0, + 8.0, + 23.0, + 7.0, + 8.0, + 27.0, + 31.0, + 27.0, + 17.0, + 3.0, + 20.0, + 23.0, + 30.0, + 29.0, + 6.0, + 3.0, + 25.0, + 6.0, + 5.0, + 30.0, + 23.0, + 11.0, + 31.0, + 11.0, + 4.0, + 15.0, + 20.0, + 21.0, + 26.0, + 6.0, + 18.0, + 28.0, + 15.0, + 15.0, + 20.0, + 18.0, + 6.0, + 4.0, + -23.759591429292932, + 21.92076358365115, + 37.659532397042526, + 127.96703551860521, + -16.43912574964911, + 27.1967354192459, + 48.478280817264555, + 20.26021218050715, + 37.659532397042526, + -7.327983001538023, + 4.32367050008537, + 30.906707774719887, + 22.462755624917794, + 27.285920737580433, + 127.96703551860521, + 3.9162971268734146, + -12.168972949208934, + 21.92076358365115, + 27.285920737580433, + 27.285920737580433, + 22.814374950143744, + 22.52813113361826, + -23.759591429292932, + 30.906707774719887, + 54.22319727984013, + 48.478280817264555, + 46.61325079348976, + 27.018287691023403, + 4.32367050008537, + 54.22319727984013, + 127.96703551860521, + 48.478280817264555, + 27.018287691023403, + 20.26021218050715, + 22.462755624917794, + 48.478280817264555, + 22.462755624917794, + 9.367224068768225, + 19.680233052739478, + 127.96703551860521, + 27.018287691023403, + 19.680233052739478, + -16.43912574964911, + 19.731617015780838, + 9.367224068768225, + 46.61325079348976, + 3.9162971268734146, + 8.165455032092966, + 0.0, + 25.253581559481443, + -1.3822338180987916, + 44.53289966758467, + -12.168972949208934, + 19.731617015780838, + -7.327983001538023, + 3.9162971268734146, + 21.92076358365115, + -1.3822338180987916, + -7.327983001538023, + 39.36220243187919, + 22.52813113361826, + 9.367224068768225, + 25.253581559481443, + 4.32367050008537, + 127.96703551860521, + 8.165455032092966, + 20.26021218050715, + 19.680233052739478, + 8.165455032092966, + 74.4870340853643, + -23.759591429292932, + 52.25150813923053, + 74.4870340853643, + 21.92076358365115, + 20.26021218050715, + 25.253581559481443, + 74.4870340853643, + 46.61325079348976, + 74.4870340853643, + 37.42700352280355, + 37.42700352280355, + 39.36220243187919, + 27.018287691023403, + 39.36220243187919, + 27.018287691023403, + 27.1967354192459, + 46.61325079348976, + 27.285920737580433, + 74.4870340853643, + -1.3822338180987916, + 37.42700352280355, + 37.659532397042526, + 37.659532397042526, + 22.814374950143744, + -1.3822338180987916, + 19.731617015780838, + -12.168972949208934, + 19.731617015780838, + 54.22319727984013, + 44.53289966758467, + 25.253581559481443, + 27.285920737580433, + 22.462755624917794, + 44.53289966758467, + 19.731617015780838, + 22.52813113361826, + 27.018287691023403, + -23.759591429292932, + -12.168972949208934, + 127.96703551860521, + 19.913722253277772, + 4.32367050008537, + 22.462755624917794, + 25.253581559481443, + 52.25150813923053, + 19.731617015780838, + 25.253581559481443, + 22.814374950143744, + -1.3822338180987916, + 22.814374950143744, + -16.43912574964911, + 30.906707774719887, + -7.327983001538023, + 52.25150813923053, + 27.018287691023403, + 44.53289966758467, + 20.26021218050715, + 30.906707774719887, + -23.759591429292932, + 20.26021218050715, + 37.659532397042526, + 27.018287691023403, + 52.25150813923053, + 8.165455032092966, + -1.3822338180987916, + 8.165455032092966, + 22.52813113361826, + 19.913722253277772, + -7.327983001538023, + 37.42700352280355, + 54.22319727984013, + 20.26021218050715, + 19.680233052739478, + 21.92076358365115, + 19.913722253277772, + 19.913722253277772, + -7.327983001538023, + 19.680233052739478, + 20.26021218050715, + 22.52813113361826, + 2.122824642179952, + 2.4927955486490876, + -0.5622618623653975, + 0.9293423586836713, + 0.7797029789296257, + 3.762666456103456, + 2.116882476026178, + -0.24197609154837313, + -0.5752036862599226, + 1.051325723913254, + 0.7360967760209972, + -1.3244166210704291, + 0.23040709350796862, + 3.534135810565336, + 0.7900112803338492, + 0.2695155828385136, + 1.6912519137939306, + 2.3510195134753213, + 3.2216573312186996, + 3.365440484850944, + 2.305268011953307, + -1.0006880851463231, + 2.1378964116814188, + -1.9385693745437973, + 2.1509458027261807, + 2.0872653429041303, + 0.9792595759714422, + 2.9875260396934933, + 0.7284534759980018, + 2.208298817218429, + 1.1614394620659603, + 2.0580648418391743, + 2.980643835105331, + -0.2601038863387657, + 0.15957201956145745, + 2.0692247093026817, + 0.11160660725073651, + 1.7396667392573613, + 0.49939602584980447, + 1.2531551786313975, + 2.856722246118792, + 0.4693865591964714, + 1.8660068497014424, + -0.16665407096728244, + 1.5161224566235123, + 0.8077597032603137, + 0.27435057669676627, + 0.31922031995138006, + -2.206120753977817, + -0.00036348531304844847, + 2.99473707364648, + 2.6812988852731716, + 1.685044577044097, + -0.03789029796462812, + 1.0375996865085364, + 0.26678646761420627, + 2.4496745317213877, + 3.0010360857123604, + 1.0327195443731294, + -2.0915073849272403, + -1.1356358624381695, + 1.534891796869344, + 0.0008453439090317962, + 0.7726801875315944, + 1.1067049694474334, + 0.30817014758946737, + -0.314486724221185, + 0.49867364369147665, + 0.3281262543259256, + 0.3814874352654686, + 2.1478780704494014, + 1.849511786724487, + 0.5452933366033377, + 2.382296695095195, + -0.2448685316073459, + 0.03860638872539637, + 0.6306037777161542, + 0.8183892453426712, + 0.38119674760807604, + 1.3004144179397523, + 1.4942180614436236, + -1.987589001680524, + 2.9358616709119003, + -2.206120753977817, + 2.858632588431941, + 3.986083902749321, + 0.9218884576809308, + 3.2139623455391804, + 0.39745505754320565, + 3.003348681082774, + 1.0366209997091256, + -0.7219387344243983, + -0.43531995458464406, + 2.3091076496207683, + 3.003016896065259, + -0.09073940499439936, + 1.6822360595262489, + -0.12956217405631704, + 2.1775988418879795, + 2.722660094145175, + 0.05513057730436463, + 3.6106553859531596, + 0.2611882870990618, + 2.5449687816594313, + -0.10323596505579394, + -1.1656661043089418, + 2.951367281081523, + 2.155706736947499, + 1.621369573198139, + 1.1618404742329838, + 1.4411995085560743, + 0.7413115563705605, + 0.11511437482426148, + -0.019516384776728016, + 1.7241611692629468, + -0.05145551974382734, + 0.014623138523423805, + 2.23144939800793, + 2.990054847036353, + 2.287895314338257, + 1.3211623979176816, + -1.7614317446438295, + 1.0462029843569118, + 1.8240282899667626, + 2.9248070406787314, + 2.5542219496383827, + -0.22103692957855053, + -1.4823779433931847, + 2.1531029582936623, + -0.3360821439887998, + -0.4147846592852922, + 2.9243021359875345, + 1.5974102995502772, + 0.3299654725483739, + 2.9967692629581646, + 0.28460027512963276, + -0.8876440979093074, + 1.3397035544467688, + 1.0417575298526272, + 1.1819621540499432, + 2.1694052888702635, + -0.21025946616860058, + 0.5788600567493157, + 2.4743729861599637, + 1.2587943911386468, + 1.3639619710374924, + 1.0518750519295552, + 0.6240160784132227, + -0.28794908444326933, + -0.9651063104409849 + ] + }, + "label": "rarith.dimred.fit", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.dimred.json b/tests/_r_cache/rarith.dimred.json new file mode 100644 index 00000000..0432e283 --- /dev/null +++ b/tests/_r_cache/rarith.dimred.json @@ -0,0 +1,47 @@ +{ + "entries": { + "1527c593a38abe807a1f696cfa0a03154c3934e4f937d1810e3a9983d47137e6": [ + 0.26006707317073174, + 0.34177462681839155, + 0.009471189083651438 + ], + "1a8f3056fbf55a288e8dff2d1fb5614320edfc6185731466a31828535361bfed": [ + 0.6112053825479852, + -0.0068139137684348995, + -0.1424184273629041 + ], + "1cbbea403ad61846493190c711b6a610b4de18f950ab3a6310485c26ca59013e": [ + 0.7423250057323427, + 0.5816647470918792, + 0.21424440407855094 + ], + "4078f74b07aeb9ee62edca66759a7c1a4853f6102aaa30535871f915ca5838a1": [ + 0.18284347884258234, + 0.298749991340673, + 0.010366553801056827 + ], + "4c1bd7649a5842272c71d3f2fda9d1d597aa2c85c7be5aee1ea9f22793afb5c7": [ + 0.5710045567472546, + 0.4945070464509698, + 0.262836805446757 + ], + "92d98092f0d047bc58f6efecfc891afa96ce13ba27e58f189568d47425abfec9": [ + 0.489969365598451, + 0.6860921082316411, + 0.18339909534887533 + ], + "a27db4b848872ef14b395c352917c0f36969bf983c7578a0044647b738aecb23": [ + 0.6823991454293333, + 0.5184771042655602, + 0.35508462061700463 + ], + "e994cfce1c2a611941bf5e7688e7a94133e65b85cf114d74ec141df7d02dddee": [ + 0.7272045028142589, + 0.15046904315196996, + 0.19662288930581612 + ] + }, + "label": "rarith.dimred", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.norm.constant.json b/tests/_r_cache/rarith.norm.constant.json new file mode 100644 index 00000000..8ee53a85 --- /dev/null +++ b/tests/_r_cache/rarith.norm.constant.json @@ -0,0 +1,25 @@ +{ + "entries": { + "8790ecf2d874a4b6ddee6d8291562e43f968c395532998cd4341c93bf4676327": [ + "NA", + "NA", + "NA", + "NA", + "NA", + "NA", + "NA", + "NA", + "NA", + "NA", + "NA", + "NA", + "NA", + "NA", + "NA", + "NA" + ] + }, + "label": "rarith.norm.constant", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.norm.json b/tests/_r_cache/rarith.norm.json new file mode 100644 index 00000000..d5dd1dc2 --- /dev/null +++ b/tests/_r_cache/rarith.norm.json @@ -0,0 +1,191 @@ +{ + "entries": { + "c6940b4b7c40e374ac63dffdb59daeb5df3f20987aceb14d844980f2984a45c6": [ + 91.43492745660201, + 127.8676244332362, + 87.21311660082583, + 100.55027727233019, + 57.753936131163265, + 89.56567261073542, + 197.76168311191628, + 59.809780407112406, + 62.837643863639656, + 149.0975393988462, + 140.8480309064117, + 167.71938644776134, + 89.6621074376932, + 92.38893308811834, + 63.94256814437659, + 55.06973290790499, + 139.49932214020873, + 103.13653977233443, + 126.23998628476531, + 71.57226186656624, + 230.90055310470441, + 211.89776667054988, + 49.23248337342747, + 90.42423238423804, + 124.30358191950133, + 216.60581598001446, + 75.83867879621552, + 59.27116171516123, + 116.15254831614554, + 154.17295198809384, + 96.834588315893, + 273.46586660330615, + 57.468602514172154, + 23.236477333686913, + 169.66571647350807, + 54.32909819616837, + 150.50193361427318, + 19.104295161956365, + 52.65069027888686, + 120.80891328070656, + 78.7087842374709, + 324.77645534484327, + 136.78576705117925, + 79.47780501841453, + 13.537659353642082, + 102.50203949158406, + 54.18066254616437, + 98.54873095477154, + 83.12640777798329, + 114.13748476478818, + 6.403856496983964, + 165.0186243475053, + 145.86383680987794, + 28.179505958314156, + 283.69198371458737, + 206.9838713415903, + 149.26927400232748, + 210.19599382158574, + 4.962264640349412, + 98.35365508407872, + 147.6027371874681, + 54.81384236093464, + 202.28253888385845, + 129.98872042548146, + 81.09206231976654, + 25.03900330380029, + 213.8869618619028, + 19.64847994238787, + 127.9757927826788, + 240.1686632285016, + 53.23413908805143, + 44.80520752494922, + 192.8400316690387, + 15.260633318689841, + 140.85891991448366, + 229.41577738372587, + 14.847674726076882, + 259.8808672367343, + 30.114835321485586, + 149.3535639771142, + 46.15993073630568, + 70.71354777018162, + 48.42016510915449, + 7.060723539225821, + 221.94469567123727, + 110.2086000160072, + 64.28348470933223, + 175.2371916070358, + 134.64712401634466, + 150.9849288986446 + ], + "f48af4c4862c65a73116a556cfb7d7aa1b6aa65d4780a1a5086c19fb0d9e0d46": [ + 6.927511980929279, + 9.68781323367529, + 6.60764903469915, + 7.618130946956128, + 4.375692042675421, + 6.7858890180161495, + 14.983294319085934, + 4.531451840913597, + 4.760856084512589, + 11.296284901662096, + 10.671265879852923, + 12.707157881242257, + 6.793195344358957, + 6.999791640639624, + 4.844570004408875, + 4.172325008811952, + 10.569081775850762, + 7.814077561154637, + 9.564496213715916, + 5.422629135000697, + 17.494040762434498, + 16.05430614071538, + 3.730069328071571, + 6.85093728001763, + 9.417785708076188, + 16.411008649322955, + 5.745871633441055, + 4.490643721462578, + 8.800227576682634, + 11.680820466989255, + 18.04466878420289, + 50.959074360331414, + 10.70900301115906, + 4.330007949537609, + 31.61640598720292, + 10.123971189883116, + 28.045325442932942, + 3.559995292473127, + 9.811207791196184, + 22.512171159528272, + 14.66701068980895, + 60.52056055102064, + 25.48938250018418, + 14.810314085027903, + 2.522678966248516, + 19.100771578613198, + 10.096310906644732, + 18.364091179706236, + 15.490213999578799, + 21.268981923317355, + 1.1933284525636367, + 30.750442288882176, + 27.18103798036472, + 5.251118018506563, + 52.864663049566886, + 38.57046812495263, + 27.815625138443977, + 39.169031998191066, + 0.9246946097665255, + 18.32773971133464, + 139.9238659812195, + 51.96221207392553, + 191.75901070978875, + 123.22619920512922, + 76.87333633766164, + 23.736376501842578, + 202.7597262555105, + 18.626289231354328, + 121.31799192463897, + 227.6741508105765, + 50.46469113863103, + 42.474265534921294, + 182.8077396207231, + 14.466715536317206, + 133.53088843698583, + 217.48067210872284, + 14.07524066349571, + 246.36084893284266, + 28.548143902088878, + 141.5836079192001, + 43.758510750690796, + 67.03475267321485, + 45.90115889867848, + 6.693397108064989, + 210.39826526320977, + 104.47511795822501, + 60.93920662087871, + 166.12066808900593, + 127.64225443658813, + 143.13010286229024 + ] + }, + "label": "rarith.norm", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.pnorm.json b/tests/_r_cache/rarith.pnorm.json new file mode 100644 index 00000000..a6aabab2 --- /dev/null +++ b/tests/_r_cache/rarith.pnorm.json @@ -0,0 +1,620 @@ +{ + "entries": { + "e6f74787772129950ca693de052db54afe31428ced85ac072c1299794da0fb1b": [ + 0.5014722772150865, + 0.814936797345462, + 0.20542078048883283, + 0.003772555230267082, + 0.0862818385239801, + 0.0014652308965369444, + 0.5715928149608153, + 0.9999709805847657, + 0.06988882334007554, + 0.03134211263212036, + 0.929154932883978, + 0.8578390631600938, + 0.6240908680291367, + 0.00262399505601256, + 0.46503551651327024, + 0.981506592980832, + 2.7576441128105152e-05, + 0.08489991397097357, + 5.862810330950917e-09, + 5.4728054755727616e-05, + 1.645504509368424e-08, + 0.24032005750018653, + 7.16680778556121e-05, + 0.7921182489618801, + 0.680912956672605, + 0.28746938623465124, + 2.1716318390907723e-14, + 0.053038650743129086, + 0.442156938307539, + 0.6330442326709025, + 2.2119095375619756e-06, + 0.0758917988414343, + 0.0016647459167950405, + 0.0076223789969442745, + 0.9992704460597781, + 0.007704856568988265, + 0.46113881331569534, + 0.9960130103411546, + 0.03999007145793839, + 0.36877262051407506, + 0.6298259588272421, + 0.575872472707065, + 0.00011884671924671054, + 0.5903403972994614, + 0.9999771377502306, + 1.7299974130281956e-06, + 0.9950334287214385, + 0.6398515709215757, + 0.027151531947129208, + 0.9999999990209766, + 0.9888956410620637, + 0.0001604189810554064, + 0.5884457522584087, + 0.95819100372931, + 0.28557917007695344, + 0.9797557772261533, + 0.4209155009983637, + 0.9773437933398622, + 0.9999920403400752, + 0.021331883719748354, + 0.7288755764577485, + 0.08227617133209073, + 0.6486970817825954, + 0.00018431235811079168, + 0.04111379581242945, + 0.27806887021730964, + 0.9964941873457994, + 0.9997044504181571, + 3.584842965704201e-05, + 0.008564497633814392, + 0.9738532848724181, + 1.1346138995557046e-09, + 0.0823389226636764, + 0.38519653690371397, + 0.9999187173020055, + 0.9806899398071642, + 0.16313855280983533, + 0.134422202249528, + 0.2264508639558611, + 0.9999975682141712, + 0.09955788737992262, + 0.18113684217428055, + 0.85491920547832, + 0.3585596942409667, + 0.27697462031453546, + 0.00041558360232820995, + 0.48621354296487834, + 0.09163668442181397, + 0.999765956081346, + 0.9749591495437027, + 0.47112952162143085, + 0.977526122906521, + 0.153957049265169, + 0.9992012944600075, + 0.49353794346308555, + 0.9599535296653262, + 5.382261898209027e-05, + 0.8508393754174562, + 2.0461611446450686e-07, + 5.108371482751731e-10, + 0.18050805031043723, + 0.0034692376561836543, + 0.6886963103724821, + 0.9999999999917623, + 0.006294847979257611, + 0.030615010660692884, + 0.7311226418720992, + 0.9304351709456481, + 0.29832704714134045, + 0.2683565614766154, + 0.9824580570097184, + 0.9405873122538009, + 0.0009642519097568631, + 0.4061174115003155, + 0.5421535010707744, + 0.0007795452996408338, + 0.7821624752307628, + 0.005028410880524918, + 0.9982283842506356, + 0.7184481881720512, + 0.6056193418595208, + 0.038107306720013494, + 0.3609841382664156, + 1.0285117217704976e-09, + 0.0003441188654278015, + 0.8618170609820461, + 8.530696889815796e-11, + 0.9944546115631723, + 8.103587821031981e-08, + 0.9884027447363279, + 0.005598479462045268, + 0.9902797125609186, + 0.6527862015585176, + 2.008461624354997e-06, + 0.9999106779487275, + 0.9999923776081268, + 0.42175150647762655, + 0.2056099347920444, + 0.3157556038790585, + 0.0017197537065509194, + 0.9995092213394842, + 0.05169083469883747, + 0.43897390828091826, + 0.008658921944116138, + 0.030175585540668553, + 6.324983000315715e-05, + 0.9999187703887152, + 0.32194646531487986, + 0.9981208387312217, + 0.515942989226756, + 0.018616016435212036, + 0.1635292926689246, + 0.04641126576384296, + 0.5095247584766882, + 0.13012498221944818, + 0.18412262265045387, + 1.7691859326062355e-05, + 0.007748784345170364, + 0.9999996514550514, + 0.0220205384658738, + 0.0007826911760039795, + 0.8442259522789987, + 0.9999878829369416, + 6.441795951835659e-06, + 0.26579997214033524, + 0.028969599874195784, + 6.352242692929288e-08, + 0.9862652364106254, + 0.471964803857514, + 0.5848533958667319, + 0.012006086528478557, + 0.9137716733057638, + 0.05284293210399133, + 0.33406727745357195, + 0.0004424378780548161, + 0.00013198638914578025, + 0.9999691998960609, + 0.06409051507373745, + 0.809224219991801, + 0.4596278574787366, + 0.09284527835106071, + 0.06376898153957267, + 0.9706375850205946, + 0.18257305951909622, + 0.32479532345138057, + 0.5265756701440137, + 0.9997918467842711, + 0.9794010576096305, + 0.8744745971567921, + 0.04544570236196614, + 1.692362183015376e-05, + 0.9978043270371577, + 0.9981302626317381, + 0.3364668184939936, + 0.9479880961734185, + 0.9904693296449166, + 0.9936764212990791, + 0.9971464388222632, + 0.08583570070092726, + 0.9999972521801417, + 9.209099407453285e-05, + 0.9951334394161282, + 0.9308027396742651, + 0.995614552188869, + 0.9999999913493458, + 0.9999957726937247, + 0.0002956972029750378, + 2.0311523629493651e-07, + 0.9928704134825476, + 0.0011632574743150178, + 0.48515632449164897, + 0.9941186152794372, + 4.0822840890262535e-07, + 1.226271915826692e-10, + 0.781685149478735, + 0.552965655989793, + 0.23043676689895404, + 0.5460169191304551, + 0.004917932402272423, + 2.8062722665835893e-06, + 0.3085500186226204, + 0.0017777244162627563, + 4.102147354224944e-07, + 0.9353721130944599, + 0.4269298936683902, + 0.8886889978881399, + 0.001499283334349254, + 0.024180845643173857, + 0.0013626764386513502, + 0.0039078956966876735, + 0.7211374193112252, + 0.009413878487157345, + 0.857284592004335, + 0.8459620711398678, + 0.9999999993819245, + 1.4680471634326298e-05, + 0.9961357592451486, + 0.3941711349497181, + 0.483213798676427, + 6.819569352827563e-06, + 0.08370384691100415, + 0.9871127223132059, + 0.4022857501114422, + 0.5960604788566874, + 0.19156326472267016, + 0.9997336089730205, + 0.4743188179175068, + 2.0385677374687258e-11, + 0.018936912728123936, + 1.7484298767795967e-09, + 8.839484804389536e-23, + 0.05587841203417149, + 0.9999684195790404, + 0.5562071126344122, + 0.0002177036649472591, + 0.002385517832119564, + 0.9996528747674562, + 0.6818505564429103, + 0.557248866825345, + 0.43628875811407525, + 0.545857011627388, + 0.9921586427695338, + 0.9513111741267628, + 0.741220949832915, + 0.0008781910058600835, + 0.9374022605233355, + 0.0200488403373823, + 0.9994837851598692, + 6.860270589028904e-05, + 0.339853521456327, + 0.49119412065042795, + 3.534718643230321e-05, + 0.9999998803671993, + 0.999994099176466, + 0.082150430050885, + 0.9896980069346961, + 0.8720276207199048, + 2.2405940819910376e-15, + 0.7737320593798596, + 0.42699408168941166, + 0.5985717826933481, + 0.000617599922484882, + 0.20953345015274133, + 0.2964022800453334, + 0.9998175736842707, + 0.8421373933100538, + 0.49335189847544647, + 0.9999977507274889, + 0.047882228100893896, + 0.12134471943303354, + 2.5147405661687193e-08, + 0.9999987450111539, + 0.9980920759262126, + 0.9970251811767618, + 0.9776089437034334, + 0.6294684161085662, + 0.7410114599642537, + 0.22481867100650468, + 0.2706655738993098, + 0.5647056802037269, + 0.9999971264849806, + 0.9522491309406614, + 0.4303903791417022, + 0.041089889986257386, + 0.028390330483373225, + 0.9999992381995512, + 0.935752292049592, + 0.5802963514249935, + 0.14950807572769348, + 0.0004386795399381474, + 0.42051167206669815, + 0.9956160539022397, + 0.11947571358402131, + 0.2477055588421278, + 0.2536322773211164, + 0.628840182596622, + 8.807030217938705e-07, + 0.24003342229608055, + 0.005185815159644913, + 0.9960199211652679, + 0.010394463163008657, + 0.9582866227646679, + 0.9999975996319033, + 0.17340655492585083, + 0.03555856880672985, + 0.7171165052226629, + 0.49757162029226704, + 0.0014372522605149757, + 0.9166304973473013, + 0.99999999926009, + 0.21936606417097307, + 0.2713853254125804, + 0.000859868083438271, + 0.8307833313809824, + 9.167077531731422e-05, + 0.0004488103083809871, + 0.9999382322413682, + 0.003300213390017297, + 0.9994108316813497, + 0.9999975968986414, + 0.781709413080877, + 0.9515602942053691, + 0.999999997639976, + 0.2775332445837716, + 0.03761845541575928, + 2.4568618806037728e-05, + 0.5497862162387924, + 0.9999954480899695, + 0.9980039552936119, + 0.002354650138428453, + 0.005142070910382453, + 0.06520161656417253, + 0.8097035141720934, + 0.2689689474188174, + 0.7400053295960394, + 0.8133256153641015, + 0.18504073728359605, + 0.45203489597904506, + 0.7322977367758761, + 0.40055565869833953, + 0.934550034955565, + 0.9999999900361258, + 0.9621266887345868, + 0.5664847839826157, + 2.1144334783963206e-07, + 0.8777619055138668, + 2.609489153500239e-09, + 1.1836005253818442e-05, + 0.9948250967280517, + 0.9829423048621635, + 0.3264212012242982, + 1.448454494453484e-07, + 0.13262982286684963, + 0.020864387614312103, + 0.9719669970642829, + 0.9999999999937014, + 0.742409444378177, + 0.00969535206514488, + 0.00022266318861228183, + 0.43318038561341166, + 0.29792439031233536, + 0.0002755983549241425, + 0.6364800094986791, + 0.00027746121369811086, + 0.9995755258519443, + 0.9992835779483625, + 0.9994315466999626, + 0.07749115025554004, + 0.938652724054736, + 0.3459756048492263, + 0.12171906798785706, + 0.15447280081861134, + 4.8266314660688335e-05, + 7.401784072042725e-06, + 0.9914126053096433, + 0.2830833120584076, + 0.7419195347595029, + 0.9986726682831814, + 9.996778008217351e-08, + 0.00932659743429204, + 0.5727304085051657, + 3.0110906340588208e-148, + 0.5357893758157657, + 5.931813317383353e-07, + 2.7280100942709642e-15, + 0.9999999999999742, + 2.593345062183256e-75, + 7.68690744504914e-83, + 3.4463938335993694e-08, + 0.96017884976355, + 7.771047828779701e-120, + 3.582853441209726e-11, + 3.470348954667477e-192, + 1.0834164411404956e-35, + 0.06160791917755148, + 1.0372399774837876e-295, + 0.00015434465300509138, + 0.0, + 0.9999999999999988, + 6.08433002295983e-62, + 2.2109131172609318e-85, + 3.4782677514638644e-05, + 7.830667682584144e-44, + 0.0002725482215222125, + 2.1449835217482085e-280, + 9.502513451807355e-33, + 5.6418929055458765e-34, + 0.9999999999802172, + 1.36282416e-316, + 8.429132669804874e-68, + 1.5027021671520994e-17, + 5.966721646957959e-36, + 1.0839098368396887e-288, + 2.357349998499676e-25, + 7.936511484466112e-184, + 5.989404806060699e-201, + 0.9999583226420301, + 1.9097042853974851e-199, + 3.0266983628617736e-67, + 0.00646564688532315, + 1.791336423959393e-06, + 3.238760107042614e-35, + 0.6376886115787677, + 5.617442114664175e-63, + 1.344901399963149e-19, + 0.8278828685720732, + 5.457585878192528e-10, + 1.301689210772439e-15, + 1.2242124841251198e-86, + 6.42960485442903e-34, + 5.322122232262983e-91, + 0.0026731916171802905, + 5.155561398055509e-98, + 1.102306010185186e-62, + 0.012968775566823331, + 5.669866110034634e-50, + 2.4039003069253678e-118, + 0.9100311603406088, + 9.020259428014377e-98, + 0.9873390101760454, + 0.2302633423695834, + 6.569538415636013e-70, + 6.639600015771844e-06, + 6.46e-321, + 8.750349914366374e-94, + 0.9987222209730482, + 1.4795994702772e-28, + 4.7488804624017864e-34, + 6.755562075966376e-12, + 4.785092745057634e-10, + 1.3825671112909088e-27, + 1.5522059233094592e-80, + 5.909081230009288e-209, + 2.8518889653500735e-142, + 4.0704950896360234e-142, + 2.059003981862269e-76, + 1.0, + 3.6545928249453735e-111, + 7.116756905137635e-70, + 4.480040517650836e-102, + 4.206218390591554e-28, + 0.9999999999999293, + 0.9999957787614886, + 1.5982331354452882e-99, + 7.84246693392784e-157, + 0.9999877811024783, + 3.363294506648777e-53, + 5.234730904329522e-09, + 2.232024259163923e-289, + 0.9995624598478053, + 2.7780797937900815e-131, + 5.728626144567344e-53, + 2.500852128321824e-199, + 1.0743673430185029e-80, + 0.9196089272652772, + 0.9160468254805807, + 3.772850982762574e-60, + 0.05430580316795843, + 7.858273039277001e-66, + 2.8540466024127237e-101, + 2.753999333520512e-37, + 3.666198116012843e-197, + 2.1961227195375964e-129, + 1.4801055206634571e-06, + 0.07020029335528849, + 1.2740300815293246e-302, + 0.0003915600771030414, + 0.8082585197075648, + 1.0238036101278924e-70, + 5.340400101183212e-298, + 2.3945799569212127e-09, + 4.380402004741672e-169, + 2.3982980250580695e-15, + 9.24603522537903e-100, + 1.9468351859927693e-16, + 5.911760956672793e-162, + 3.9003651161768894e-59, + 2.033567242953447e-43, + 1.4647598527715793e-17, + 5.640284853623808e-196, + 0.9999999999999998, + 3.275723823795058e-144, + 5.482253203578931e-135, + 4.968706373925477e-163, + 5.478247386307499e-294, + 1.365964467491869e-79, + 0.07953823211653202, + 3.0679268826925305e-06, + 6.204031416316419e-201, + 2.5291628863416834e-241, + 7.717840097248274e-127, + 0.05622888545824758, + 3.8693622172898315e-55, + 0.9999999996344543, + 3.4649370540503923e-13, + 1.703906097644539e-19, + 0.05310732184585579, + 0.006439683778281522, + 1.9055073700083603e-115, + 1.2805480082222047e-162, + 1.1297216988422022e-73, + 9.647825793807782e-55, + 6.961256711774004e-238, + 3.2355383804762057e-72, + 5.789903568049986e-16, + 3.349282671764364e-242, + 1.5821741039555227e-240, + 1.5078574976355356e-215, + 1.867988233006962e-147, + 0.9999999999999997, + 0.0, + 8.762790307800866e-209, + 1.7130100420926456e-221, + 6.558895746002814e-60, + 0.0, + 8.608734361313349e-66, + 5.816152326630215e-223, + 3.0061005468676274e-155, + 0.35272654709260076, + 1.4532741878999552e-208, + 3.0215236337942453e-20, + 1.467343011980448e-86, + 8.568904227666537e-82, + 0.8001818265077066, + 2.357779608109812e-224, + 0.9999999997001531, + 0.9999985269544842, + 2.29129839323408e-11, + 1.6320358933514576e-308, + 4.112465745874196e-12, + 9.080723342749532e-24, + 0.08230930351420879, + 4.050502798138048e-189, + 0.9999999999999912, + 0.9999918320107632, + 7.241860571147641e-258, + 4.22441630652081e-130, + 1.53e-322, + 0.9391118454627696, + 0.9999999956611693, + 1.0, + 2.6326372075935805e-21, + 1.0369680021562792e-11, + 6.019504893349725e-10, + 7.1878161275131836e-31, + 0.9999999999999875, + 3.736904195799771e-22, + 1.6556761290186992e-119, + 4.875582168235822e-47, + 0.0, + 7.421404740720105e-05, + 5.6662865267697897e-08, + 2.3178421549603347e-80, + 0.9949742784003796, + 0.013084478950181945, + 5.041508001330934e-119, + 4.577557833744692e-286, + 9.95108467524629e-245, + 1.993269754716402e-208, + 4.048530456963521e-136, + 0.5, + 0.7499999999376897, + 0.25000000006231027, + 0.999999992291371, + 7.708628950139992e-09, + 1.0, + 0.0, + 0.5, + 0.5, + 1.0, + 1.286769202e-314 + ] + }, + "label": "rarith.pnorm", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.quantile7.json b/tests/_r_cache/rarith.quantile7.json new file mode 100644 index 00000000..28784786 --- /dev/null +++ b/tests/_r_cache/rarith.quantile7.json @@ -0,0 +1,91 @@ +{ + "entries": { + "3d7afe5ee39c7008dcc36cb1ebe6350aa9ba3d5b4e1add8a0cebb4ea81031274": [ + -0.0563217863067781, + -0.055931935557986305, + -0.055347159434798614, + -0.054372532562819126, + -0.05242327881886014, + -0.04657551758698322, + -0.04333975637201132, + -0.03682924886718834, + -0.026303278649809865, + -0.01928596517155755, + -0.018311338299578057, + -0.017336711427598572 + ], + "5dbf16251df5a98afb530df0d3f8d856d674e72cefe20e66d78903c446fc25f6": [ + -1613.6526639761714, + -1420.269558824045, + -1210.4114730578963, + -1095.855796173002, + -842.229857901648, + -496.6232582808637, + -363.1729629842296, + -95.7361489910401, + 162.13109409314092, + 785.2687766855747, + 910.8296184749363, + 1282.592642908718 + ], + "6fd8565f6ed19ac10430d166a41551b10cf6a2493ced9ce0c0cf46cedfd8c613": [ + -874.3859352535358, + -856.9073997493758, + -830.689596493136, + -786.9932577327363, + -699.6005802119369, + -305.28286863466786, + -28.599903651329527, + 99.06061494887989, + 412.84004779163155, + 595.4719634749958, + 624.4335708856769, + 653.395178296358 + ], + "7c903d803b913c4fea7f76bece98f2cadab796193072155372dbcb77f338e880": [ + -60.08602580733764, + -58.81142065221125, + -56.89951291952166, + -53.713000031705675, + -47.33997425607374, + -28.22089692917787, + -17.64167414162883, + 3.644231948981903, + 45.5278903842065, + 73.4503293410229, + 77.32844586280297, + 81.20656238458301 + ], + "9b5aad95663ec9c1dbf514060ebca0c6ef2957260c1b3058980266047763c848": [ + -10.319651682419721, + -6.696809400236576, + -6.027378657005787, + -5.351525898631781, + -4.097130853666314, + -2.4457821163648092, + -1.4710931225911648, + -0.28402340086060013, + 1.89806167883947, + 4.7114409898712175, + 5.086781710562684, + 8.051891551079493 + ], + "ba626fcdcfbabf7cb654fc403fa807ed05c1df8f20363561ebfb87abbd6bcfad": [ + 0.2973322806695969, + 0.2973322806695969, + 0.2973322806695969, + 0.2973322806695969, + 0.2973322806695969, + 0.2973322806695969, + 0.2973322806695969, + 0.2973322806695969, + 0.2973322806695969, + 0.2973322806695969, + 0.2973322806695969, + 0.2973322806695969 + ] + }, + "label": "rarith.quantile7", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.quantile8.json b/tests/_r_cache/rarith.quantile8.json new file mode 100644 index 00000000..eefcd21f --- /dev/null +++ b/tests/_r_cache/rarith.quantile8.json @@ -0,0 +1,91 @@ +{ + "entries": { + "1288c70af60e6e96e8242c028912f8bcc3d82bfc78a64d86452e3620f40e6688": [ + -0.20204115921962626, + -0.20204115921962626, + -0.1911907147404344, + -0.1540962876858643, + -0.11861656367169024, + -0.07061252199987414, + -0.04122896884283419, + -0.009533592815139224, + 0.07233582619163305, + 0.14890867379538836, + 0.1879135054663072, + 0.2080223960864082 + ], + "5835f11c3beee9acf64b32bb5fde0b093130140ae666aa7d029fdad0592e29f1": [ + -0.00419505375063892, + -0.00419505375063892, + -0.00419505375063892, + -0.00419505375063892, + -0.00419505375063892, + -0.0036338516759389255, + -0.0027022562319369333, + -0.0008278413024389495, + 0.0034417955917266786, + 0.003916199691078415, + 0.003916199691078415, + 0.003916199691078415 + ], + "6a572a563eb18178080fe2ace3045cad01934193e35662ff0794224fb75392c2": [ + -2.00602114758143, + -1.5464542071497887, + -1.1833139067954728, + -1.0235954690076998, + -0.8127654755013443, + -0.4054552243560582, + -0.2532883422986205, + -0.017219388548570195, + 0.4576469326461715, + 1.098502486344814, + 1.2972453892254152, + 2.059401393594646 + ], + "754453d088d4ec128e1a2429afc849b37606b2cda1d6ab551fc5eec4d761f525": [ + -225.5647568578974, + -225.5647568578974, + -225.5647568578974, + -225.5647568578974, + -225.5647568578974, + -225.5647568578974, + -207.10662906045246, + -141.91765203563932, + -58.27054721338114, + -58.27054721338114, + -58.27054721338114, + -58.27054721338114 + ], + "aeaed66a92d599187bd8e0f627c54620d329581360fcb9097aaf00450486aaec": [ + -1458.0667124359468, + -1458.0667124359468, + -1458.0667124359468, + -1458.0667124359468, + -1458.0667124359468, + -1458.0667124359468, + -1458.0667124359468, + -1458.0667124359468, + -1458.0667124359468, + -1458.0667124359468, + -1458.0667124359468, + -1458.0667124359468 + ], + "fcc717f08759e82a93c93c090f4354ba3444774e643447c714152b4bfff3c9d0": [ + -0.022430111997858337, + -0.022430111997858337, + -0.022430111997858337, + -0.022430111997858337, + -0.021056096557576214, + 0.00197444663813608, + 0.015831328958133092, + 0.0263381036890329, + 0.04741417159146736, + 0.05166522855050867, + 0.05166522855050867, + 0.05166522855050867 + ] + }, + "label": "rarith.quantile8", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.round-1.json b/tests/_r_cache/rarith.round-1.json new file mode 100644 index 00000000..e956b65a --- /dev/null +++ b/tests/_r_cache/rarith.round-1.json @@ -0,0 +1,1509 @@ +{ + "entries": { + "a81f4bc2c24885110d35436406e9098c1e32b0772881c20c2f70a0a8781bf7c6": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ] + }, + "label": "rarith.round-1", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.round1.json b/tests/_r_cache/rarith.round1.json new file mode 100644 index 00000000..e6139865 --- /dev/null +++ b/tests/_r_cache/rarith.round1.json @@ -0,0 +1,1509 @@ +{ + "entries": { + "cbac821923d67a992b199333b9cf99547a2b6b68d8363834c42a020d272db250": [ + 1.8, + -0.6, + 1.1, + 0.4, + -0.8, + 1.7, + 1.5, + -0.5, + 1.9, + -1.1, + 1.2, + 0.7, + -0.1, + -1.9, + 1.6, + 0.3, + -0.4, + -0.6, + 0.6, + -0.6, + 0.0, + -0.5, + -1.8, + -1.0, + 1.4, + 1.3, + 0.7, + -0.1, + 1.9, + 1.4, + -1.0, + 0.3, + 1.8, + 1.1, + -0.5, + -0.1, + 0.1, + 1.6, + 0.9, + -0.4, + -0.2, + -1.6, + 0.0, + -1.1, + -1.6, + -1.6, + -1.1, + 0.8, + -1.3, + 0.3, + 0.4, + 1.9, + -0.2, + 1.1, + -1.9, + 0.6, + 1.9, + -1.8, + 1.6, + -1.4, + 0.7, + -1.9, + -1.8, + -1.5, + 1.6, + 1.1, + 0.6, + 0.6, + -0.8, + 0.1, + -0.1, + 1.8, + -0.2, + 1.0, + -0.6, + 1.7, + 1.4, + 2.0, + -1.1, + 1.5, + -0.1, + 0.0, + 0.2, + -1.6, + 1.5, + 0.4, + -1.6, + 1.0, + -1.5, + -0.5, + 0.7, + -0.1, + -1.7, + 0.5, + -0.5, + -0.5, + -1.3, + -1.3, + 2.0, + 0.4, + 0.3, + 1.1, + -0.5, + -1.4, + 0.4, + -0.4, + -0.4, + -1.6, + -0.3, + -1.8, + 1.6, + -0.5, + 0.9, + 0.4, + -0.2, + -0.6, + -1.9, + 0.6, + -1.6, + -1.1, + 1.8, + -0.5, + -0.6, + 0.8, + -0.9, + -0.5, + -0.8, + 0.8, + -1.7, + -0.5, + 2.0, + 0.6, + -0.1, + -0.8, + -1.9, + 1.6, + 1.7, + 1.9, + -0.1, + 0.4, + -0.8, + 1.3, + 0.3, + -0.8, + -0.8, + -0.6, + 0.7, + 1.1, + -1.6, + 1.7, + 1.0, + 1.2, + -1.2, + -1.4, + 0.9, + 1.8, + -0.6, + -1.6, + -0.7, + 1.4, + 1.3, + -1.1, + -1.4, + 0.0, + -0.7, + 1.4, + -0.1, + -1.1, + -0.3, + -1.1, + 0.8, + 1.9, + 0.4, + -1.0, + 1.2, + -0.5, + -1.6, + -1.4, + -0.5, + 1.3, + -1.1, + 1.1, + -1.7, + 1.4, + 0.9, + -1.6, + -1.8, + -1.0, + -1.3, + 0.6, + -0.1, + 0.3, + -0.9, + 1.9, + -1.6, + 0.7, + 0.1, + 2.0, + 1.8, + -0.1, + -0.2, + -0.6, + -1.3, + 0.7, + 1.5, + -0.1, + 1.7, + -1.0, + -0.9, + 1.8, + 1.7, + 0.0, + 0.2, + -0.6, + -1.9, + -0.5, + 1.9, + 0.0, + 1.2, + -1.4, + 0.9, + 0.4, + 1.9, + -0.8, + -0.1, + -0.9, + 0.9, + -1.7, + 0.9, + 1.3, + -1.0, + -0.6, + -0.6, + 1.5, + -1.4, + 1.7, + -1.6, + -1.9, + 1.2, + -1.7, + 0.3, + 0.1, + 1.5, + 0.7, + 1.9, + 0.7, + -0.7, + 1.7, + -1.7, + 0.7, + -1.6, + -1.3, + -0.5, + 0.0, + -1.8, + -0.4, + 0.1, + -0.9, + -0.4, + 1.1, + -1.2, + -0.8, + -0.9, + -1.0, + -1.9, + 1.8, + 0.0, + -0.7, + 1.5, + -1.9, + -1.6, + -1.9, + -1.9, + -2.0, + 0.4, + 0.9, + -0.6, + 1.7, + 1.0, + 2.0, + 1.1, + 0.1, + -1.8, + -1.5, + -0.7, + -1.5, + 0.5, + -1.7, + 0.2, + -0.5, + -0.5, + -1.7, + 0.9, + 1.1, + -0.7, + -0.5, + 0.8, + -1.5, + 0.5, + -0.6, + 0.8, + 0.5, + -0.8, + -0.9, + -0.4, + -0.8, + 1.0, + 1.2, + 0.0, + -1.1, + 0.0, + 1.0, + 0.2, + -1.9, + -1.6, + -0.5, + 1.4, + -0.3, + 0.5, + 0.3, + -1.7, + -0.1, + -0.3, + -0.2, + 1.6, + -0.8, + 1.9, + -0.1, + -0.1, + 0.8, + -0.1, + -1.8, + 0.2, + -1.7, + 1.7, + -0.8, + 1.7, + 0.1, + 1.1, + 0.0, + -1.2, + 2.0, + 0.5, + -0.2, + 1.8, + 0.2, + -0.4, + -1.2, + -0.7, + 1.2, + -1.4, + -0.1, + 0.6, + -0.2, + -0.4, + -1.9, + 1.3, + -1.7, + -0.3, + -1.7, + -0.7, + 1.1, + 1.7, + -1.8, + -0.8, + 0.8, + -0.5, + 1.9, + -1.4, + -1.4, + 0.9, + 0.8, + 1.7, + -1.7, + -1.9, + -1.5, + 1.7, + -0.3, + -0.3, + -0.9, + -1.2, + -1.5, + 1.0, + -1.8, + 1.0, + 0.0, + -1.3, + -1.1, + -1.8, + 1.4, + 0.4, + 0.9, + 0.2, + -1.8, + 0.8, + 1.6, + -0.9, + -0.4, + 2.0, + 0.5, + -1.5, + -1.3, + -1.0, + 1.7, + 1.2, + -1.9, + 0.2, + -1.2, + -1.0, + 0.1, + -1.2, + 0.7, + -1.8, + -1.9, + -0.8, + -1.6, + 1.4, + 1.1, + 0.2, + 0.9, + -2.0, + -1.0, + 0.1, + 1.2, + 0.4, + -0.7, + -1.0, + 1.4, + 0.2, + 1.4, + -1.6, + 1.2, + -1.7, + -0.6, + -0.4, + -1.7, + 1.7, + -0.3, + -1.7, + 0.8, + -0.7, + 0.4, + 0.7, + 1.9, + 0.3, + 0.6, + 1.8, + -0.7, + -0.7, + 0.8, + 0.6, + -0.7, + 1.7, + 1.9, + 2.0, + 0.8, + 1.3, + 1.6, + 0.4, + 1.6, + 0.2, + -0.7, + 0.1, + -1.4, + 1.9, + -1.4, + -0.7, + -1.5, + 1.3, + 1.0, + 1.4, + 1.7, + 1.1, + 0.4, + 0.8, + 1.4, + -1.4, + -1.1, + 0.3, + -0.1, + 1.7, + -0.7, + -1.7, + -0.9, + -1.3, + -0.3, + -1.6, + 0.5, + -0.4, + -1.1, + 1.5, + 2.0, + 0.7, + -1.6, + 0.1, + 0.8, + -0.1, + -1.3, + -0.6, + 1.6, + -1.8, + -1.8, + 0.8, + 1.3, + 0.5, + 1.6, + 0.1, + 0.6, + -0.3, + 1.2, + -1.9, + -0.4, + -0.6, + -1.6, + -0.1, + 1.0, + -1.4, + 2.0, + -1.2, + 1.6, + -1.9, + 0.0, + -1.8, + -0.8, + 1.2, + 0.4, + 1.6, + 0.0, + -1.6, + -1.7, + 0.1, + 1.7, + 1.9, + 1.0, + -1.7, + -1.3, + 0.5, + 0.4, + 1.6, + -0.1, + -0.9, + -1.5, + 1.0, + 1.6, + 0.9, + -1.4, + 0.5, + 1.6, + -0.1, + -1.8, + -0.9, + 0.6, + -1.5, + -0.6, + 1.9, + -1.5, + -0.3, + 0.6, + -1.2, + 1.2, + -1.3, + 0.4, + -1.8, + 2.0, + 0.1, + -1.0, + -1.5, + -1.8, + -0.9, + 1.4, + -1.0, + 1.8, + 2.0, + 0.3, + -1.5, + -0.6, + 0.8, + -0.5, + 0.6, + 0.8, + -1.3, + -0.8, + 0.6, + 0.0, + 0.0, + -1.5, + 1.6, + -1.9, + 1.0, + -0.8, + 1.1, + -0.7, + 1.2, + 0.5, + 1.9, + 0.9, + 1.6, + -0.6, + 0.8, + 0.7, + -1.2, + 1.1, + 0.2, + 1.7, + 0.0, + 1.5, + -0.8, + -0.6, + 0.2, + -1.7, + -0.2, + 1.6, + 0.6, + 1.7, + 1.7, + -1.6, + -1.0, + 1.0, + -0.2, + 1.2, + -0.1, + -1.2, + -0.9, + 0.2, + 1.6, + -1.9, + 0.0, + 0.2, + 1.7, + -1.8, + -0.4, + 0.8, + -1.2, + 0.7, + -1.0, + 0.1, + 0.2, + -1.6, + 0.2, + 0.0, + -1.1, + -1.9, + 1.1, + -0.2, + 0.0, + -0.5, + -0.5, + 0.3, + 0.2, + -0.6, + 0.3, + -0.6, + -0.2, + -0.6, + -1.9, + -1.7, + 1.6, + -0.1, + -1.4, + 0.5, + 1.4, + -0.6, + -0.3, + 1.7, + 1.7, + 1.3, + -0.2, + -0.6, + -1.8, + 0.9, + -1.2, + 1.0, + 1.4, + 0.0, + 0.4, + 0.5, + -0.9, + -0.8, + -0.8, + 0.6, + 1.4, + 1.0, + 0.5, + -1.6, + -0.5, + -0.2, + 0.8, + 1.3, + -0.1, + 1.2, + -0.2, + -1.0, + -1.6, + -0.8, + -0.1, + 1.0, + 1.5, + -0.3, + -0.8, + 0.1, + -0.5, + -1.0, + 1.0, + -1.0, + 0.9, + 0.6, + -0.7, + 1.2, + -0.7, + 1.4, + 0.6, + -1.6, + -0.2, + -1.2, + -0.1, + -1.8, + -1.7, + 0.1, + 0.9, + -1.3, + 0.2, + 0.8, + 1.4, + -0.2, + 2.0, + 1.9, + 1.3, + -0.6, + -1.3, + -1.2, + 1.7, + -1.8, + -0.3, + 1.4, + -1.1, + -1.5, + -1.0, + 0.8, + 0.5, + -1.6, + -1.4, + 0.4, + 0.2, + 0.9, + -1.1, + 0.1, + 1.3, + 1.9, + 1.0, + 1.5, + -1.2, + 0.0, + 1.9, + 0.5, + -1.4, + -0.5, + -1.2, + 1.3, + -1.4, + -0.2, + 0.0, + 0.0, + 1.3, + -1.8, + -1.3, + -1.2, + -0.2, + 1.7, + 0.9, + -0.2, + -0.6, + 1.2, + 0.6, + 0.8, + 1.6, + -1.8, + 1.2, + 2.0, + 0.0, + -1.7, + -1.6, + 1.6, + -1.3, + 1.1, + 1.0, + -0.4, + -1.1, + -1.5, + -0.8, + 0.5, + 1.3, + -0.1, + -1.5, + 1.3, + 0.0, + -0.2, + 1.0, + -1.8, + -1.3, + -1.9, + -1.0, + -0.1, + -1.3, + -1.9, + -1.7, + -0.6, + -0.2, + -1.3, + 0.0, + 0.5, + -0.6, + 0.1, + -1.2, + -0.5, + -0.5, + 0.7, + 1.1, + -0.7, + 0.7, + -1.0, + 1.5, + -1.5, + 0.4, + -1.1, + -0.7, + -0.5, + -1.2, + 0.1, + 1.6, + 1.9, + -1.6, + -1.7, + -0.5, + 1.8, + 0.9, + -0.6, + 1.8, + 1.6, + -0.4, + 1.8, + 1.3, + 0.8, + 1.1, + 1.8, + 0.9, + 0.7, + -0.1, + 1.8, + -0.9, + 1.5, + -1.0, + -1.1, + -1.1, + 1.6, + 0.9, + -1.0, + -1.4, + 0.8, + -1.4, + 1.7, + -1.5, + 0.7, + -1.1, + 0.6, + 1.7, + 1.3, + 0.5, + 0.0, + -0.3, + 0.8, + 1.1, + 0.6, + -0.1, + 1.0, + -0.6, + -1.0, + -1.8, + -1.2, + 1.9, + 0.5, + -0.5, + 1.8, + -1.7, + -0.7, + -0.3, + -1.0, + 1.3, + -0.9, + 0.0, + 1.9, + 0.0, + -1.0, + 0.4, + 1.4, + -0.6, + -1.7, + -0.3, + 1.2, + -1.4, + -0.8, + 0.2, + 0.3, + 0.3, + 0.2, + 0.2, + 0.2, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.2, + 0.3, + 0.2, + 0.2, + 0.3, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.2, + 0.2, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.3, + 0.2, + 0.2, + 0.2, + 0.3, + 0.2, + 0.2, + 0.3, + 0.2, + 0.3, + 0.2, + 0.2, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.3, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.2, + 0.2, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.2, + 0.2, + 0.3, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.2, + 0.2, + 0.3, + 0.3, + 0.2, + 0.2, + 0.3, + 0.3, + 0.3, + 0.2, + 0.3, + 0.2, + 0.2, + 0.3, + 0.3, + 0.3, + 0.2, + 0.2, + 0.2, + 0.2, + 0.3, + 0.2, + 0.2, + 0.2, + 0.3, + 0.3, + 0.2, + 0.2, + 0.2, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.2, + 0.2, + 0.3, + 0.2, + 0.2, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.2, + 0.2, + 0.3, + 0.2, + 0.2, + 0.3, + 0.3, + 0.2, + 0.2, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.2, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.2, + 0.2, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.2, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.3, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.2, + 0.2, + 0.2, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.2, + 0.2, + 0.3, + 0.3, + 0.2, + 0.3, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.2, + 0.3, + 0.3, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.2, + 0.3, + 0.3, + 0.2, + 0.2, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.2, + 0.3, + 0.3, + 0.3, + 0.3, + 0.2, + 0.3, + 0.3, + 0.2, + 0.3, + 0.2, + 0.3, + 0.3, + 0.2, + 0.2, + 0.2, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1 + ] + }, + "label": "rarith.round1", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.round12.json b/tests/_r_cache/rarith.round12.json new file mode 100644 index 00000000..831f6b4d --- /dev/null +++ b/tests/_r_cache/rarith.round12.json @@ -0,0 +1,1509 @@ +{ + "entries": { + "80e5e107bb02e4d457ee09e0579da72001b27e49700216eff88cc9501f74e9f2": [ + -1.534163947969, + 1.376614291943, + 1.194693697299, + -1.930413538811, + -1.692213126001, + -0.014174616805, + 1.308395253043, + -0.952404620571, + -0.748427784079, + 0.889627018675, + -1.802291395957, + 1.287889780562, + 1.865989375851, + 1.176410093709, + 1.937972986353, + 0.037571536334, + -1.246835708248, + 1.244724178495, + 1.93218233791, + 1.059280296432, + -1.555842414622, + -0.698598324488, + -1.09710239244, + 1.046754841088, + -1.235341190831, + -1.858611023497, + -0.059777119539, + -0.029288572008, + -0.101280096804, + 0.217671987411, + 0.693214693242, + 0.262143208519, + -0.435833532287, + -1.914466091134, + 0.878184434909, + -0.96476566288, + -1.641357612741, + 1.160565050385, + -1.316061288456, + 1.50156010181, + -0.538683539184, + 1.760314840798, + 1.947787629685, + 1.920961202505, + 1.17274336176, + -0.96545237264, + 0.859525883565, + 0.145634026541, + 0.832571701565, + -0.502808914093, + 0.308516083413, + -0.108437199206, + -0.386738224362, + -0.396824997329, + 0.769429488803, + -0.628267554512, + -0.051357497956, + 0.042469795252, + -1.244991200024, + 0.946382639534, + 0.866531194706, + 1.038747928376, + 0.047233007733, + 0.994962926075, + 0.329475150161, + -1.541527006672, + -1.086887692394, + 1.1486655048, + 0.445073865534, + 1.604615561496, + -1.765501136226, + 1.330152098011, + -0.065831901562, + -1.808835394032, + -0.531467109113, + 0.653634509702, + -0.699590284796, + 1.328856728806, + 0.242562384962, + -0.644032451922, + 1.963307137579, + 1.950142020743, + -0.702892807824, + 0.324768071211, + -1.247513753964, + 0.247665125084, + 0.149881381015, + -0.238607782953, + -0.80139644972, + -1.186582966454, + -1.980066392817, + 0.738779704293, + 0.31078506055, + -0.06053340411, + -0.84012516241, + -0.305624134134, + 1.055945729719, + 0.910968466777, + 1.531261376188, + -1.152236879779, + -1.630042309074, + 1.53921810725, + 0.122346389944, + -0.446625610355, + 1.338165022283, + 1.082251530021, + 0.926717201848, + -1.063768397561, + -0.394167939846, + 1.721157427599, + -1.927977205117, + 1.091105564818, + 1.477339419058, + 1.133257832124, + 0.900576669663, + 1.270635373182, + -1.56798290366, + -0.793315011573, + -0.838719101362, + 0.392734874072, + -0.210998962231, + 1.197581725936, + 0.647111668573, + 1.463016289343, + 0.030417073437, + -0.798719528165, + -1.338864405957, + -1.845085837577, + -0.349472524585, + -0.179617680243, + 1.461171045568, + 1.2635425132, + 1.097013267992, + -0.914416572979, + -0.190063713268, + 0.468270742804, + 1.348600888912, + -0.903947228862, + 1.414044390676, + -0.630745691328, + 1.232824500157, + 0.583625355773, + 0.423895728897, + -0.400272806979, + -1.4551992879, + -1.762170025145, + 1.224678040886, + -1.984844063765, + 0.70771223682, + -0.774944745819, + 1.142576959611, + -0.863533602299, + 1.944379902514, + -0.483343531564, + 1.220382049296, + -0.797347464561, + -0.736463230397, + 0.053473001228, + 0.591533345914, + -0.391041663075, + -1.473955014724, + -1.56084658621, + 1.081161487556, + 0.577798451358, + 0.167861145422, + -0.188651707607, + 1.18757234908, + -1.734790119019, + -1.059965634528, + -1.25442572157, + -0.833977080071, + -0.299057413306, + 0.164227500694, + 0.022774763334, + 0.88243643336, + 1.188371698218, + 0.747371897611, + 1.579502240038, + 0.881451890574, + -0.320891477369, + 0.320399267302, + 1.973006844743, + -1.151962253703, + 1.705907828424, + -1.772155291638, + -1.053934961326, + -1.097263749548, + -0.906606333841, + -1.138741446562, + 0.130106487089, + -0.02173477713, + -1.046344916841, + -1.487508157868, + -1.274171946513, + -0.472813136627, + 0.520533120421, + 0.647555917863, + 1.856319570709, + 1.944346038784, + -0.145556083197, + -0.155789106698, + -0.618919527056, + 1.499843093236, + -1.044436788801, + -0.722086614897, + -1.112013751419, + -0.358844007815, + -1.873109556475, + 0.173396001676, + 0.435492422493, + -1.342269707087, + 1.826534037978, + 1.013566242421, + -0.418880590958, + 1.091163932422, + -0.99530527599, + 0.59861368502, + -0.270089600212, + 0.702164060747, + -1.651355894152, + -1.661006484302, + -1.437310621666, + -1.292057994196, + -0.393831903894, + -0.83634574171, + -1.299422479892, + -0.317464666407, + -1.620427303012, + 0.236024254457, + -0.382288856821, + -1.921671982498, + 0.2734956352, + 1.264814546999, + -0.460431009662, + 0.227848618627, + 1.225837333056, + -1.561342236538, + -1.440409412174, + 1.782416179137, + 0.667378276964, + 0.240632213987, + -0.166035344476, + 0.853505351804, + 1.005445390316, + 0.69430402017, + 0.366945064833, + 0.479659243518, + -0.482546421686, + -0.127992295626, + 0.587273995177, + -1.074501620325, + 1.887698546599, + -0.736988922224, + -1.515641759012, + 0.516059338967, + -0.655619935337, + 1.089176050354, + -0.484773879158, + -0.148399192218, + -1.955271819182, + 1.252039693721, + -0.676930458532, + 1.309854465933, + -0.642129643766, + -1.482528663887, + 0.133492279418, + 1.414732998514, + 1.822353121639, + 1.994950003456, + -0.530913586661, + -1.500647569622, + -0.949496213652, + 0.37773186595, + -1.343816078181, + 0.373592511476, + 1.905961314439, + 1.226400954185, + -1.327398144703, + -0.814797862424, + -1.205861349477, + -0.396691191561, + 1.684430383818, + -1.720835007319, + -1.479718783988, + 0.252204506336, + 0.801114643944, + -0.849477377949, + -0.352604375972, + 0.435948325445, + 1.168238914796, + -1.764586849864, + 0.904974855423, + 1.432309120046, + -0.816947987109, + 1.250611144274, + 1.756364994568, + 1.315173752401, + -1.207796523025, + -0.322280608315, + -0.648730780726, + -1.707291363434, + 1.717963798834, + 0.748234178715, + -1.946311193321, + -0.631021391991, + -1.276721142519, + 0.805022236169, + 0.259594994766, + 0.107294717985, + 0.838195985243, + 1.090160448842, + 1.673367607562, + 0.355669420278, + -1.670304860862, + 1.289275807964, + -0.646604406268, + -0.907310544573, + 1.714665860773, + -1.222103077694, + -1.468263089829, + 0.245338781428, + 1.441652859651, + -1.778811821025, + -1.959590806846, + 1.150819687215, + 1.870975362647, + -0.344637457104, + -0.912112145321, + 0.653688952737, + 0.53280725026, + 0.782846047669, + 1.862712224889, + -1.243845521209, + -1.349398233885, + 0.414275586795, + 1.379587668827, + 0.312833050246, + 0.138815389109, + 0.272026729113, + 0.386676019552, + -1.788605236245, + 0.624499974355, + -0.4510640393, + 1.923553652602, + 0.133254208871, + 1.519272275253, + 0.886158104819, + -0.231823425112, + -1.209097845636, + -0.024679919233, + -1.34160090577, + -1.633388491043, + 1.357121315867, + -1.963771768858, + 1.753021778291, + -1.579225728666, + 0.955620326624, + -0.597157136823, + 0.544632215981, + -1.681200678224, + 0.240164062704, + 1.543361730111, + 0.696314820377, + 1.204401983463, + 0.006391058012, + -0.955032362625, + -0.123808287616, + -0.235258294194, + 0.955240239723, + 0.749955539877, + 0.520744059285, + 1.599961803027, + -0.209314282648, + 0.961899119231, + -0.526789898392, + 0.317996556107, + 1.324614874249, + -0.353549356279, + 1.736046164564, + -0.455358859245, + -0.70983007415, + 0.025628253903, + 0.474944395713, + -0.841224215377, + -0.304733158769, + 1.280985223014, + 1.499891415747, + -0.949517578058, + -1.632579887468, + -0.466486021271, + 1.785484414861, + 1.683530725946, + 1.276725472694, + 1.763388522565, + -1.039107120923, + -1.505657348727, + -1.309071548489, + -0.813188319564, + -0.549347295145, + 0.358362425428, + 0.678449108713, + -0.873363336007, + 0.647565841659, + -0.572097363669, + 0.393073389608, + 0.827483694115, + -1.660214704776, + -1.144274445134, + -0.586735505285, + -0.358664074531, + 1.855048799512, + -1.687633153085, + -0.467744986985, + -0.369247571703, + 1.833715534336, + 0.669769320619, + -0.576572774692, + 1.519290550085, + -1.249609707697, + 0.781351556804, + -1.191333649382, + 0.741281064225, + 1.689768802518, + 1.999216871868, + 1.747643758194, + 0.610224529708, + -0.743491702646, + -1.928967281357, + -1.391976848103, + 1.764412575745, + -0.772326802426, + -1.637698738117, + -1.204449856215, + 1.97045322504, + 0.725858253124, + 0.800769778234, + 1.869552235528, + -1.3951819909, + -0.860745028193, + 0.626141120908, + 1.080949051179, + 1.711479268016, + -1.615930090468, + -0.437369506578, + -1.65052256222, + 0.667489467715, + 1.564897014763, + -0.280052627919, + 0.519606181741, + -1.382892752804, + 0.328289570581, + 1.342959349566, + 1.366819406075, + 1.230025957527, + -1.139963034222, + -0.29252781243, + 0.616851324198, + -0.64827781116, + -1.466825191256, + -0.02845109952, + 1.777358837939, + -1.135279897707, + -0.835116639898, + -1.295785412247, + 1.140843677469, + 1.942688443813, + -1.866366501506, + -0.682668971088, + -0.118199858923, + -1.620555379581, + 0.521412589278, + 1.03298722974, + 0.515009045259, + 0.988193005261, + -1.422395245594, + -1.422012680166, + 1.858932142641, + -0.893546145535, + -1.611565844833, + -1.591900348472, + -1.883576626117, + -1.024294665087, + -0.396529324228, + 1.415666699738, + -1.094571875182, + -0.881136428201, + -1.55600439863, + -1.056373154856, + 0.296593147562, + 0.48783405302, + -1.753936362861, + 1.130311848741, + 1.825352794418, + 0.65590468846, + 1.561616772229, + -1.494514029521, + 1.499447676218, + -1.237683901305, + -1.410321727421, + 1.64584535992, + -0.928843236511, + -0.86443091681, + -1.654345996724, + -0.889232634553, + 1.589023800143, + 0.361071113502, + -0.660261050426, + 1.419715800034, + 1.162205399589, + -0.544763292583, + 0.777758812281, + 0.972229861133, + -0.016152186911, + 1.366024768392, + -0.188519907506, + -0.915640183727, + -1.231421894541, + -0.116109182573, + 1.259136121648, + -1.633555540669, + 1.028548678381, + 1.821041350493, + -0.551634175767, + -0.104121993842, + 1.962016821208, + -1.1344617412, + 1.733919545239, + 0.803087797682, + 1.286400059516, + 1.398589910758, + 0.66127083411, + -1.396187497288, + -1.768750388977, + 1.316636598307, + -1.831835817983, + 1.559695676142, + -0.501832452234, + -1.152781688525, + 1.618758459335, + 0.089400316561, + 0.224739513148, + -0.869569370794, + 0.079426410847, + -0.685069286725, + 1.791253834113, + -1.454184398792, + 0.35517587024, + -0.053314186743, + -0.514724077207, + -1.983060131275, + -0.463645422033, + -0.766660172108, + -1.285589465346, + -0.567048552341, + 1.781837675253, + -0.963167562411, + 1.53592166572, + -1.042781931212, + -0.236377159224, + 1.632862180662, + 1.641442034187, + -1.635113003944, + 0.518487686089, + -0.322058619981, + -0.101944792821, + -0.612293563965, + -0.733548142883, + -1.889596203764, + 0.138916089568, + -0.153361858171, + -1.235110541985, + 1.04680147371, + -0.271829439415, + -0.383034118153, + 1.279669764056, + -0.091698856653, + -0.638918862485, + -1.43697926235, + 0.93662036203, + -1.174321988432, + -0.596437355879, + 1.252625797434, + 0.25556737746, + -1.993091461039, + 0.918225655972, + 0.607749337104, + 1.399579033013, + -1.469881725631, + -1.62996741312, + -1.19239011948, + -1.885805488925, + 1.042373912557, + 1.988312797159, + -0.835289224019, + 0.030838487342, + -0.818340531799, + -0.906315781812, + -0.223109754243, + -0.378306976601, + -1.46632771714, + 1.809, + -0.828, + -1.481, + 1.816, + -0.183, + -1.476, + -0.298, + 1.405, + -0.491, + -1.052, + 1.287, + 1.901, + 0.96, + -0.585, + 1.609, + 0.264, + -1.008, + -1.28, + -0.576, + -0.509, + 0.652, + 0.172, + -0.563, + 1.277, + 1.65, + 0.017, + -0.95, + 1.223, + 0.595, + 1.374, + 0.636, + 0.563, + -0.185, + -0.361, + -0.393, + -0.74, + -0.452, + 1.802, + 1.447, + 1.992, + -1.766, + -0.291, + -1.129, + -0.938, + -1.616, + 1.115, + -1.901, + -1.518, + 1.891, + -1.721, + 0.232, + -0.116, + -0.256, + -0.477, + -1.238, + 1.29, + 1.943, + -0.979, + -1.128, + 0.376, + 1.593, + -0.455, + -0.95, + 0.241, + 0.858, + 0.045, + -1.264, + -1.209, + -1.279, + 0.153, + 2.002, + -1.079, + 0.021, + 0.944, + -0.882, + 0.177, + 1.286, + -1.452, + 1.035, + -0.69, + -1.606, + 1.543, + 1.913, + -0.377, + 0.927, + -0.232, + -0.539, + 1.048, + -0.628, + -1.498, + 0.444, + 1.054, + -0.111, + -1.604, + -0.34, + 1.614, + 0.143, + 0.066, + -0.028, + -0.315, + -1.338, + 1.913, + 0.571, + -0.444, + 1.929, + -1.595, + -1.012, + 0.824, + -1.171, + -1.722, + 0.98, + 1.02, + -0.801, + -0.498, + 1.529, + 0.231, + -1.296, + -0.07, + -0.07, + -0.943, + -1.033, + 0.217, + -1.5, + 1.51, + 0.039, + 0.319, + 0.811, + -1.859, + -0.629, + 0.834, + 1.28, + -1.084, + -1.472, + -0.89, + 0.032, + -0.617, + 1.885, + 1.81, + -1.353, + 1.776, + -0.997, + -1.484, + -0.181, + -1.462, + 0.891, + 0.716, + 0.152, + 0.206, + -0.231, + -0.919, + -1.617, + 0.982, + -1.568, + -1.108, + -0.407, + 0.323, + 0.773, + 0.917, + -1.028, + 1.999, + 0.767, + 0.717, + 0.308, + 0.366, + 1.155, + -1.465, + 0.387, + 0.18, + -0.933, + 0.02, + -0.632, + 0.48, + -1.384, + 0.17, + -1.213, + 1.585, + -0.523, + -1.754, + 1.016, + -0.593, + -0.098, + 1.557, + -1.335, + 1.762, + 1.718, + 1.676, + 0.681, + 0.092, + 1.792, + 1.831, + 0.426, + 0.458, + 0.124, + 0.601, + -1.832, + 1.843, + 1.731, + -0.746, + -1.013, + -1.453, + 0.704, + -1.596, + -0.616, + -0.848, + 0.686, + -0.841, + 1.293, + 0.095, + 0.693, + 1.999, + 0.779, + -0.417, + -1.123, + 1.418, + 1.192, + -0.883, + -0.929, + -0.692, + -1.523, + -1.853, + -0.752, + -1.841, + -1.566, + -1.005, + 0.911, + 1.965, + -1.301, + -0.888, + -1.394, + -0.815, + 0.619, + 1.737, + -1.601, + -0.656, + -0.997, + -1.015, + -0.412, + -1.569, + 1.557, + 0.561, + -1.63, + 1.246, + -0.69, + 0.033, + 1.117, + 0.284, + -0.078, + -1.795, + -1.919, + 1.852, + -1.528, + -1.86, + -0.388, + 0.156, + -0.181, + 0.142, + 1.46, + 0.451, + 1.046, + 0.111, + 0.359, + 0.884, + -1.547, + -0.36, + 0.174, + -0.386, + -0.397, + -0.678, + 0.711, + -0.095, + -1.522, + -0.47, + -0.288, + 1.527, + -0.748, + -1.441, + -1.048, + -0.837, + 1.46, + 0.587, + -0.771, + 0.811, + 0.667, + 0.939, + -1.683, + 0.3, + 0.788, + -0.015, + -0.482, + -0.884, + 1.15, + -1.409, + 1.352, + 1.697, + -0.421, + 0.638, + -0.054, + -0.249, + -0.159, + 0.888, + 0.256006550217, + 0.29835945962, + 0.310168405041, + 0.229345180859, + 0.281013789155, + 0.302414478581, + 0.213620318948, + 0.242053211493, + 0.307215235427, + 0.327598632791, + 0.313293580841, + 0.237089401282, + 0.279417888747, + 0.313423643161, + 0.279788539621, + 0.217324646516, + 0.322839311371, + 0.32396926469, + 0.231307902033, + 0.216860770246, + 0.322124893485, + 0.309096174348, + 0.244681162886, + 0.234447174301, + 0.201889284594, + 0.327480279534, + 0.271571227031, + 0.200564913415, + 0.283996691818, + 0.202002693524, + 0.211730854293, + 0.287762599212, + 0.307766873248, + 0.311909070006, + 0.224194515736, + 0.261222588811, + 0.263589280154, + 0.226219212327, + 0.206930533542, + 0.267547474478, + 0.218997210358, + 0.24097250334, + 0.235473785212, + 0.277061294747, + 0.242748744539, + 0.326745278153, + 0.323370183268, + 0.215839592306, + 0.202760692432, + 0.273153568349, + 0.307664922227, + 0.261055320676, + 0.205170660277, + 0.254720008136, + 0.272230477595, + 0.304820643804, + 0.251885549445, + 0.235452924347, + 0.255169840263, + 0.297510108556, + 0.313034185236, + 0.228048791885, + 0.27650414603, + 0.253775531349, + 0.203228552777, + 0.311232570629, + 0.297760081771, + 0.314268800194, + 0.220739061863, + 0.238205883903, + 0.23561311036, + 0.20783589431, + 0.291293114537, + 0.285738063952, + 0.324510303754, + 0.270886532863, + 0.244216846327, + 0.316685058637, + 0.248250692174, + 0.304134640984, + 0.315415468327, + 0.260476191152, + 0.224409490408, + 0.251733923878, + 0.213000600956, + 0.32874824412, + 0.2489557011, + 0.233384442437, + 0.305828475153, + 0.31250374806, + 0.315788404891, + 0.276978904864, + 0.222050065457, + 0.296253073698, + 0.256803938126, + 0.307075608353, + 0.320311030402, + 0.233645827612, + 0.25721982676, + 0.202949815786, + 0.27960920506, + 0.308694542309, + 0.238150301543, + 0.21057840667, + 0.221611500008, + 0.213039046637, + 0.275654639847, + 0.248620836675, + 0.255180040644, + 0.259013944683, + 0.223927133064, + 0.226905490838, + 0.242619932833, + 0.32481502315, + 0.220916402235, + 0.320417791042, + 0.288275330223, + 0.209438739481, + 0.278725408094, + 0.301457992539, + 0.237480416993, + 0.222311392143, + 0.308542637831, + 0.215906666348, + 0.23433036983, + 0.28065101997, + 0.318234731564, + 0.241329409954, + 0.246383673773, + 0.307265690576, + 0.32593483888, + 0.218052342417, + 0.292018699648, + 0.296171109302, + 0.252436630013, + 0.304711666704, + 0.213083740966, + 0.295587584515, + 0.204011371167, + 0.22638702593, + 0.302784129382, + 0.304379247334, + 0.276233302505, + 0.303461760266, + 0.277825364172, + 0.324993763759, + 0.323124851721, + 0.237203678531, + 0.277442543212, + 0.285421617668, + 0.248907164658, + 0.230663439561, + 0.299180882276, + 0.311825662004, + 0.285816600672, + 0.291197226059, + 0.253007564008, + 0.227962559711, + 0.218643180199, + 0.303544846719, + 0.239424846864, + 0.294157653717, + 0.319436868023, + 0.256381663515, + 0.274878353482, + 0.265561578377, + 0.208380074908, + 0.291856543023, + 0.214638636351, + 0.287255827958, + 0.23224664688, + 0.298780385308, + 0.239889453164, + 0.30026417776, + 0.288484500379, + 0.261640552824, + 0.279090694667, + 0.314135675237, + 0.292881858941, + 0.321847983508, + 0.270567439765, + 0.203132036442, + 0.268593646341, + 0.215462794179, + 0.27203952408, + 0.205996153664, + 0.220573153419, + 0.230818931835, + 0.296908956847, + 0.282821010592, + 0.317024380851, + 0.20414177368, + 0.331498613961, + 0.33316673428, + 0.24976062119, + 0.264316057628, + 0.260345611442, + 0.23885385238, + 0.26056333047, + 0.222185530242, + 0.221430417662, + 0.211260922621, + 0.285700606715, + 0.330752897098, + 0.268064337247, + 0.270055461836, + 0.20683293417, + 0.255581792343, + 0.221590333074, + 0.277610112977, + 0.209096370046, + 0.276846521391, + 0.291334113213, + 0.268327768528, + 0.233513833457, + 0.21502067193, + 0.29449584136, + 0.234672306907, + 0.231669972311, + 0.228044401362, + 0.300353938543, + 0.259168184336, + 0.276281564083, + 0.231750524806, + 0.233329489019, + 0.249885103178, + 0.278913451536, + 0.300644638446, + 0.230770400581, + 0.207080689347, + 0.202035175303, + 0.314886631151, + 0.274524335005, + 0.282020550562, + 0.283753731932, + 0.285517005576, + 0.239057905702, + 0.294666105616, + 0.203718670675, + 0.204848601197, + 0.32509380686, + 0.236298115154, + 0.282794962802, + 0.290490423789, + 0.326157997156, + 0.246421559495, + 0.267612400481, + 0.249862454234, + 0.212999111892, + 0.229329182119, + 0.273640591275, + 0.23952493563, + 0.326657101736, + 0.304389994398, + 0.241638426896, + 0.26233785413, + 0.200344380546, + 0.237027849296, + 0.26338687186, + 0.332300653257, + 0.219065118158, + 0.21781981019, + 0.312787745494, + 0.26525578932, + 0.253201745057, + 0.236682881627, + 0.299057250189, + 0.282881221513, + 0.294247966933, + 0.202255950079, + 0.229792952329, + 0.305211469123, + 0.26941197283, + 0.269073174493, + 0.282153478205, + 0.279031113566, + 0.235192702072, + 0.286526026576, + 0.259799828224, + 0.31646336434, + 0.294703111541, + 0.332677629235, + 0.263701749454, + 0.217460477207, + 0.319883809477, + 0.318040229947, + 0.201376361943, + 0.206858407603, + 0.250142648316, + 0.227389672031, + 0.281222565532, + 0.296201603824, + 0.282317606184, + 0.238306343278, + 0.241586129815, + 0.25925151634, + 0.242996093066, + 0.22221312802, + 0.30216126458, + 0.301831797881, + -0.1495, + -0.1485, + -0.1475, + -0.1465, + -0.1455, + -0.1445, + -0.1435, + -0.1425, + -0.1415, + -0.1405, + -0.1395, + -0.1385, + -0.1375, + -0.1365, + -0.1355, + -0.1345, + -0.1335, + -0.1325, + -0.1315, + -0.1305, + -0.1295, + -0.1285, + -0.1275, + -0.1265, + -0.1255, + -0.1245, + -0.1235, + -0.1225, + -0.1215, + -0.1205, + -0.1195, + -0.1185, + -0.1175, + -0.1165, + -0.1155, + -0.1145, + -0.1135, + -0.1125, + -0.1115, + -0.1105, + -0.1095, + -0.1085, + -0.1075, + -0.1065, + -0.1055, + -0.1045, + -0.1035, + -0.1025, + -0.1015, + -0.1005, + -0.0995, + -0.0985, + -0.0975, + -0.0965, + -0.0955, + -0.0945, + -0.0935, + -0.0925, + -0.0915, + -0.0905, + -0.0895, + -0.0885, + -0.0875, + -0.0865, + -0.0855, + -0.0845, + -0.0835, + -0.0825, + -0.0815, + -0.0805, + -0.0795, + -0.0785, + -0.0775, + -0.0765, + -0.0755, + -0.0745, + -0.0735, + -0.0725, + -0.0715, + -0.0705, + -0.0695, + -0.0685, + -0.0675, + -0.0665, + -0.0655, + -0.0645, + -0.0635, + -0.0625, + -0.0615, + -0.0605, + -0.0595, + -0.0585, + -0.0575, + -0.0565, + -0.0555, + -0.0545, + -0.0535, + -0.0525, + -0.0515, + -0.0505, + -0.0495, + -0.0485, + -0.0475, + -0.0465, + -0.0455, + -0.0445, + -0.0435, + -0.0425, + -0.0415, + -0.0405, + -0.0395, + -0.0385, + -0.0375, + -0.0365, + -0.0355, + -0.0345, + -0.0335, + -0.0325, + -0.0315, + -0.0305, + -0.0295, + -0.0285, + -0.0275, + -0.0265, + -0.0255, + -0.0245, + -0.0235, + -0.0225, + -0.0215, + -0.0205, + -0.0195, + -0.0185, + -0.0175, + -0.0165, + -0.0155, + -0.0145, + -0.0135, + -0.0125, + -0.0115, + -0.0105, + -0.0095, + -0.0085, + -0.0075, + -0.0065, + -0.0055, + -0.0045, + -0.0035, + -0.0025, + -0.0015, + -0.0005, + 0.0005, + 0.0015, + 0.0025, + 0.0035, + 0.0045, + 0.0055, + 0.0065, + 0.0075, + 0.0085, + 0.0095, + 0.0105, + 0.0115, + 0.0125, + 0.0135, + 0.0145, + 0.0155, + 0.0165, + 0.0175, + 0.0185, + 0.0195, + 0.0205, + 0.0215, + 0.0225, + 0.0235, + 0.0245, + 0.0255, + 0.0265, + 0.0275, + 0.0285, + 0.0295, + 0.0305, + 0.0315, + 0.0325, + 0.0335, + 0.0345, + 0.0355, + 0.0365, + 0.0375, + 0.0385, + 0.0395, + 0.0405, + 0.0415, + 0.0425, + 0.0435, + 0.0445, + 0.0455, + 0.0465, + 0.0475, + 0.0485, + 0.0495, + 0.0505, + 0.0515, + 0.0525, + 0.0535, + 0.0545, + 0.0555, + 0.0565, + 0.0575, + 0.0585, + 0.0595, + 0.0605, + 0.0615, + 0.0625, + 0.0635, + 0.0645, + 0.0655, + 0.0665, + 0.0675, + 0.0685, + 0.0695, + 0.0705, + 0.0715, + 0.0725, + 0.0735, + 0.0745, + 0.0755, + 0.0765, + 0.0775, + 0.0785, + 0.0795, + 0.0805, + 0.0815, + 0.0825, + 0.0835, + 0.0845, + 0.0855, + 0.0865, + 0.0875, + 0.0885, + 0.0895, + 0.0905, + 0.0915, + 0.0925, + 0.0935, + 0.0945, + 0.0955, + 0.0965, + 0.0975, + 0.0985, + 0.0995, + 0.1005, + 0.1015, + 0.1025, + 0.1035, + 0.1045, + 0.1055, + 0.1065, + 0.1075, + 0.1085, + 0.1095, + 0.1105, + 0.1115, + 0.1125, + 0.1135, + 0.1145, + 0.1155, + 0.1165, + 0.1175, + 0.1185, + 0.1195, + 0.1205, + 0.1215, + 0.1225, + 0.1235, + 0.1245, + 0.1255, + 0.1265, + 0.1275, + 0.1285, + 0.1295, + 0.1305, + 0.1315, + 0.1325, + 0.1335, + 0.1345, + 0.1355, + 0.1365, + 0.1375, + 0.1385, + 0.1395, + 0.1405, + 0.1415, + 0.1425, + 0.1435, + 0.1445, + 0.1455, + 0.1465, + 0.1475, + 0.1485, + 0.1495 + ] + }, + "label": "rarith.round12", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.round2.json b/tests/_r_cache/rarith.round2.json new file mode 100644 index 00000000..221bc5c9 --- /dev/null +++ b/tests/_r_cache/rarith.round2.json @@ -0,0 +1,1509 @@ +{ + "entries": { + "fb28379de5d09d3c7ba48b54d7f7275fce5dbdac681fabe21bc15c07d2196f2d": [ + -1.36, + 0.34, + 1.3, + -0.81, + 0.74, + -0.14, + 1.57, + 0.26, + 1.97, + -1.03, + -1.56, + -0.62, + 1.12, + 0.16, + 1.7, + 1.98, + 1.69, + -1.35, + 0.36, + 0.9, + 1.68, + -1.41, + 0.45, + -1.78, + 0.97, + 0.29, + -1.49, + -1.55, + 0.1, + -0.02, + 0.15, + 1.91, + 1.91, + -0.82, + -1.7, + -0.83, + 1.25, + -0.94, + -1.43, + 0.42, + 0.76, + -1.3, + -1.77, + -1.34, + -1.05, + -0.55, + 0.25, + 1.65, + -0.6, + 0.54, + 0.65, + -0.87, + 0.22, + 1.93, + 1.99, + -0.3, + 1.3, + -0.66, + 1.21, + -0.49, + 1.12, + -1.69, + -1.2, + 0.51, + -1.13, + -0.81, + 0.7, + -0.82, + -0.73, + 1.28, + -1.53, + -1.84, + 1.32, + 1.61, + 0.71, + 0.51, + 1.56, + 0.12, + -1.54, + -0.5, + -0.24, + 1.4, + 0.44, + -1.74, + 0.64, + 1.37, + -1.67, + 1.67, + -1.74, + 0.77, + -0.53, + 0.01, + -1.3, + -1.31, + -0.86, + 1.19, + 1.35, + 1.17, + -0.65, + -0.64, + -0.22, + -1.37, + -0.45, + 0.19, + -0.83, + -1.33, + -1.65, + -0.18, + 0.12, + -0.77, + -1.97, + -0.11, + 1.82, + 0.32, + -0.08, + -0.18, + -0.68, + 0.37, + -1.23, + 1.57, + -0.18, + 0.66, + -1.75, + -0.53, + -1.4, + -1.26, + 0.56, + 0.63, + 0.53, + -0.69, + 1.42, + 1.12, + 0.39, + -0.5, + 0.95, + -0.94, + -1.06, + -1.88, + 0.97, + -1.36, + -0.67, + -0.08, + -0.09, + 1.2, + -1.47, + 0.49, + 0.09, + -1.1, + -0.76, + -0.43, + -1.52, + -1.8, + -1.38, + -0.39, + 1.42, + -0.82, + 1.28, + 1.52, + -1.1, + 1.29, + -0.13, + 0.08, + 1.11, + -0.04, + 0.56, + -1.42, + -1.75, + 1.38, + 0.12, + -1.8, + 1.04, + 1.04, + -0.99, + 0.81, + 1.84, + 0.34, + 1.5, + -1.56, + -1.64, + -0.54, + -1.47, + 1.97, + -1.32, + -0.02, + -1.7, + -1.83, + 1.02, + 0.67, + -0.44, + 1.32, + 0.91, + 0.09, + 1.08, + 0.48, + -1.67, + -1.0, + 0.14, + -0.78, + 0.24, + 0.66, + -0.78, + 0.37, + -0.86, + 0.71, + -1.95, + 1.87, + 0.13, + -0.49, + -0.25, + -1.34, + 1.68, + -0.07, + -0.02, + -1.51, + 0.0, + -0.91, + -0.2, + 0.81, + 0.87, + -1.0, + -0.03, + 1.53, + 0.37, + -0.94, + -1.36, + -1.32, + -0.6, + -1.36, + 1.14, + 1.8, + 1.97, + 0.81, + 0.02, + 1.2, + -0.24, + -1.86, + 1.88, + -1.6, + -1.79, + 1.03, + 0.17, + 0.53, + 0.56, + -0.23, + -1.72, + 0.14, + 1.94, + 0.13, + 1.48, + 1.53, + 0.78, + -1.18, + 1.75, + 1.24, + 1.17, + -1.71, + 1.49, + 0.53, + 0.43, + -1.62, + 1.14, + 1.01, + -0.94, + -1.44, + 0.74, + -1.22, + -0.32, + 0.95, + -0.02, + 1.09, + -1.32, + 1.41, + 0.24, + 0.15, + -1.56, + 0.23, + -0.41, + 1.64, + -1.42, + 0.19, + -0.16, + 1.79, + 1.46, + -0.05, + 1.46, + 0.25, + -1.53, + -1.61, + -0.38, + 1.7, + 1.03, + 1.6, + 1.81, + 0.98, + -0.41, + -0.95, + 1.12, + 1.2, + 1.82, + 0.54, + 1.49, + -0.92, + -0.88, + -0.18, + -1.4, + 1.41, + 1.25, + 1.63, + -1.3, + 0.62, + -1.89, + -1.59, + 0.14, + 0.85, + 0.61, + -1.55, + -0.69, + -0.01, + -1.96, + 0.34, + -0.01, + 0.22, + 1.98, + -0.19, + 1.86, + 0.34, + -0.18, + -1.2, + 1.28, + -0.73, + -0.88, + -0.49, + 0.04, + -0.54, + 1.13, + -1.14, + -1.08, + 0.48, + -1.91, + -0.58, + 1.32, + 1.75, + 0.68, + -1.23, + -0.57, + -0.3, + 1.41, + -1.0, + 0.98, + -0.26, + 0.86, + 0.68, + 1.57, + 0.71, + -1.0, + -0.27, + -0.34, + -1.29, + 1.63, + -1.2, + -0.79, + 1.9, + 0.61, + 1.35, + -1.96, + 0.62, + -0.39, + 1.83, + 1.5, + 0.49, + 1.76, + 0.98, + -1.59, + 0.38, + 1.8, + -1.44, + 0.43, + -1.72, + -0.31, + 0.97, + -0.26, + 1.79, + -0.71, + 1.24, + -1.88, + -0.49, + 1.56, + 0.86, + 1.06, + -0.33, + 0.38, + 0.7, + -0.21, + 0.44, + 0.19, + -0.44, + 0.09, + 0.22, + 0.45, + 0.92, + 0.78, + 1.61, + 1.86, + -1.96, + 0.82, + 1.41, + 0.29, + -1.42, + 1.87, + 1.06, + -0.75, + -0.79, + -1.69, + -0.73, + -0.16, + -0.92, + -1.66, + -1.16, + -1.8, + -0.42, + 0.44, + 1.38, + 1.38, + -0.63, + 0.34, + 0.56, + -1.4, + 0.55, + -0.19, + 0.25, + 1.56, + -0.93, + -1.64, + -0.09, + -1.77, + 1.83, + 1.34, + -1.22, + -0.15, + 1.71, + -1.91, + 1.0, + -0.33, + -1.47, + 1.52, + 0.67, + -0.39, + -1.17, + 1.55, + -1.4, + 0.48, + 0.53, + -0.69, + 1.87, + 1.88, + 1.26, + -1.18, + 1.98, + 1.64, + 1.83, + 1.53, + 0.21, + -1.59, + -0.41, + -0.25, + 0.24, + 1.08, + 1.06, + -0.61, + 1.09, + -1.2, + -0.21, + 1.29, + 0.63, + -1.87, + 1.67, + 1.2, + -0.05, + -0.25, + 0.88, + -1.79, + -0.46, + -0.47, + 1.59, + -1.02, + -1.52, + 0.33, + -0.6, + -1.53, + 0.29, + 0.1, + 0.4, + 0.81, + 0.75, + 1.39, + -0.92, + -1.38, + -0.13, + -1.13, + -1.02, + 0.64, + -1.18, + 1.44, + 0.23, + 0.88, + 0.03, + 1.88, + 0.36, + 1.26, + 1.24, + -0.66, + -1.28, + 0.08, + -0.14, + 1.14, + -0.54, + 0.07, + -1.54, + -0.55, + -0.88, + -0.59, + 1.8, + 1.26, + -0.12, + -0.41, + -0.96, + -0.33, + 1.87, + -1.19, + 1.34, + 0.41, + 1.7, + 0.96, + -1.19, + 1.85, + 1.78, + -1.53, + -0.59, + -1.61, + 0.0, + 1.6, + 1.05, + 0.67, + -0.18, + -1.37, + -1.47, + 0.11, + -0.62, + 1.22, + 0.98, + 0.97, + -1.24, + 0.55, + 0.63, + 0.68, + 0.09, + 0.31, + 0.86, + 1.91, + -1.9, + -1.02, + 1.18, + 0.31, + -1.3, + -1.06, + 0.82, + -0.34, + 1.1, + 1.82, + -0.46, + 1.31, + -0.19, + 1.09, + -0.43, + -1.48, + -0.75, + 0.6, + 1.82, + -0.71, + 0.06, + 1.69, + 0.82, + -0.93, + 0.46, + -0.42, + -1.68, + -0.22, + 1.93, + 0.66, + 0.5, + 0.37, + 1.54, + -0.33, + -1.83, + -0.48, + -1.94, + 0.14, + 1.6, + -0.53, + -0.08, + 1.27, + 1.1, + -0.97, + 1.93, + 0.86, + -1.46, + -1.33, + -1.18, + 1.84, + 1.04, + 1.88, + 1.39, + 0.09, + 1.44, + -0.87, + 0.61, + 0.7, + 1.13, + 1.42, + 1.67, + -1.54, + -1.93, + -0.26, + 1.02, + -0.54, + 1.9, + 1.84, + 0.4, + 1.48, + 0.87, + -0.78, + 1.2, + 0.9, + 1.34, + -1.66, + -0.86, + -0.01, + 1.05, + 1.82, + -1.91, + -1.29, + -0.03, + -0.53, + -0.81, + -0.18, + 0.1, + 1.22, + 1.01, + -0.05, + -0.72, + -0.06, + 0.78, + 1.48, + -0.69, + -0.97, + -1.61, + -1.69, + 0.13, + -1.16, + 1.89, + 0.04, + 0.97, + 1.36, + -1.58, + -0.3, + 1.99, + -1.26, + -1.68, + -1.03, + -0.42, + 1.66, + 1.74, + -0.38, + 0.33, + -0.42, + 1.21, + 0.19, + 0.02, + 1.2, + 0.29, + 0.55, + -0.86, + -0.68, + 0.67, + -1.65, + 1.4, + 0.46, + -0.96, + 1.2, + -0.37, + -1.08, + 1.01, + 1.35, + -1.42, + -1.44, + 1.04, + 1.78, + 1.16, + 0.74, + -1.43, + -1.25, + -1.24, + 1.39, + 1.98, + -1.39, + -0.3, + 1.57, + 1.87, + -0.09, + -1.95, + 0.8, + 1.16, + -1.41, + 1.04, + -1.05, + -1.75, + 0.84, + -1.49, + 0.02, + 1.39, + 0.58, + -0.9, + 1.16, + -0.53, + -0.83, + 1.21, + -0.92, + -0.81, + -0.4, + -1.87, + 0.58, + -1.45, + -0.63, + 1.06, + -1.5, + -0.55, + 1.38, + 0.42, + -1.76, + -1.01, + 0.85, + 1.01, + -1.94, + -0.47, + -0.23, + -0.07, + 0.72, + 1.32, + 1.95, + 0.72, + -0.49, + 1.34, + 1.74, + 0.08, + -0.39, + 0.51, + -1.26, + 0.34, + 0.19, + 1.21, + -1.6, + -1.76, + -0.4, + -0.14, + 0.21, + 1.51, + -1.97, + 1.77, + 1.46, + 1.92, + -0.84, + 1.04, + 0.87, + 0.41, + -1.11, + 0.32, + 0.7, + 1.32, + 0.32, + 0.05, + 1.72, + -0.15, + 0.73, + 1.06, + 0.68, + 0.19, + -0.8, + -1.33, + 1.3, + 0.95, + 0.58, + 1.71, + 1.14, + 1.64, + 1.17, + -0.32, + 0.84, + 1.03, + -0.35, + -0.86, + -0.1, + -1.65, + -0.47, + 1.13, + 0.42, + 1.21, + -0.41, + 1.88, + 0.49, + 1.28, + -1.59, + -0.85, + -1.79, + -1.91, + -1.15, + 1.57, + -0.38, + -1.74, + -1.7, + 0.61, + 0.67, + -0.55, + 1.9, + -1.99, + -1.16, + 0.24, + -0.96, + 1.26, + -1.51, + -0.51, + 0.71, + -1.52, + -1.12, + -0.6, + 0.5, + -0.04, + 1.86, + 1.75, + -0.13, + -1.18, + -0.82, + 0.74, + -1.56, + -1.74, + -0.97, + 0.32, + 1.86, + -0.67, + -1.64, + 1.17, + 1.5, + -0.56, + 0.03, + 0.75, + 0.72, + 0.92, + 0.64, + -1.93, + -0.6, + -1.69, + -1.78, + -0.9, + 1.24, + -1.01, + -0.04, + 0.76, + -0.16, + 0.75, + 1.6, + 1.64, + -0.91, + -0.2, + -0.77, + 1.26, + -0.88, + -0.42, + -1.4, + -1.22, + 0.3, + -1.86, + 1.33, + -1.16, + -0.91, + -1.94, + 1.95, + 1.89, + 0.95, + 0.87, + -1.98, + -0.23, + -1.16, + 0.71, + 1.12, + -0.19, + -0.37, + 0.22, + -1.43, + -1.82, + 0.38, + 0.97, + 0.27, + 0.28, + 0.23, + 0.28, + 0.22, + 0.2, + 0.24, + 0.22, + 0.28, + 0.32, + 0.23, + 0.3, + 0.27, + 0.26, + 0.28, + 0.26, + 0.27, + 0.23, + 0.31, + 0.27, + 0.29, + 0.23, + 0.26, + 0.21, + 0.2, + 0.26, + 0.32, + 0.28, + 0.2, + 0.25, + 0.24, + 0.29, + 0.24, + 0.29, + 0.3, + 0.25, + 0.25, + 0.31, + 0.23, + 0.32, + 0.24, + 0.29, + 0.25, + 0.33, + 0.22, + 0.29, + 0.26, + 0.22, + 0.28, + 0.32, + 0.23, + 0.21, + 0.31, + 0.26, + 0.31, + 0.26, + 0.2, + 0.3, + 0.32, + 0.3, + 0.3, + 0.31, + 0.32, + 0.28, + 0.24, + 0.3, + 0.32, + 0.25, + 0.33, + 0.27, + 0.22, + 0.29, + 0.27, + 0.29, + 0.3, + 0.32, + 0.24, + 0.2, + 0.21, + 0.21, + 0.28, + 0.25, + 0.24, + 0.27, + 0.26, + 0.24, + 0.2, + 0.25, + 0.33, + 0.2, + 0.3, + 0.26, + 0.24, + 0.33, + 0.32, + 0.28, + 0.3, + 0.26, + 0.32, + 0.24, + 0.33, + 0.23, + 0.33, + 0.33, + 0.31, + 0.23, + 0.21, + 0.31, + 0.24, + 0.26, + 0.28, + 0.29, + 0.31, + 0.27, + 0.28, + 0.27, + 0.25, + 0.29, + 0.33, + 0.3, + 0.22, + 0.27, + 0.22, + 0.33, + 0.32, + 0.3, + 0.32, + 0.24, + 0.21, + 0.24, + 0.24, + 0.24, + 0.28, + 0.23, + 0.29, + 0.32, + 0.28, + 0.27, + 0.24, + 0.27, + 0.25, + 0.3, + 0.29, + 0.22, + 0.26, + 0.21, + 0.23, + 0.31, + 0.27, + 0.28, + 0.32, + 0.3, + 0.24, + 0.23, + 0.29, + 0.25, + 0.27, + 0.31, + 0.33, + 0.31, + 0.24, + 0.26, + 0.26, + 0.33, + 0.29, + 0.33, + 0.31, + 0.29, + 0.3, + 0.33, + 0.21, + 0.24, + 0.31, + 0.21, + 0.24, + 0.22, + 0.29, + 0.32, + 0.3, + 0.22, + 0.31, + 0.28, + 0.28, + 0.26, + 0.26, + 0.33, + 0.23, + 0.29, + 0.24, + 0.24, + 0.32, + 0.25, + 0.27, + 0.31, + 0.25, + 0.3, + 0.25, + 0.28, + 0.26, + 0.27, + 0.28, + 0.3, + 0.23, + 0.33, + 0.27, + 0.3, + 0.23, + 0.25, + 0.27, + 0.26, + 0.23, + 0.21, + 0.24, + 0.3, + 0.28, + 0.29, + 0.26, + 0.32, + 0.27, + 0.21, + 0.33, + 0.21, + 0.31, + 0.26, + 0.25, + 0.23, + 0.3, + 0.29, + 0.3, + 0.22, + 0.31, + 0.32, + 0.2, + 0.32, + 0.21, + 0.26, + 0.28, + 0.3, + 0.32, + 0.23, + 0.2, + 0.22, + 0.21, + 0.22, + 0.26, + 0.22, + 0.27, + 0.29, + 0.28, + 0.26, + 0.28, + 0.3, + 0.22, + 0.21, + 0.21, + 0.28, + 0.29, + 0.23, + 0.3, + 0.33, + 0.25, + 0.29, + 0.26, + 0.28, + 0.27, + 0.22, + 0.31, + 0.22, + 0.2, + 0.2, + 0.32, + 0.28, + 0.25, + 0.25, + 0.26, + 0.26, + 0.23, + 0.26, + 0.26, + 0.3, + 0.29, + 0.22, + 0.29, + 0.21, + 0.28, + 0.3, + 0.23, + 0.25, + 0.22, + 0.29, + 0.31, + 0.32, + 0.3, + 0.32, + 0.29, + 0.27, + 0.23, + 0.27, + 0.25, + 0.31, + -0.15, + -0.15, + -0.15, + -0.15, + -0.15, + -0.14, + -0.14, + -0.14, + -0.14, + -0.14, + -0.14, + -0.14, + -0.14, + -0.14, + -0.14, + -0.13, + -0.13, + -0.13, + -0.13, + -0.13, + -0.13, + -0.13, + -0.13, + -0.13, + -0.13, + -0.12, + -0.12, + -0.12, + -0.12, + -0.12, + -0.12, + -0.12, + -0.12, + -0.12, + -0.12, + -0.11, + -0.11, + -0.11, + -0.11, + -0.11, + -0.11, + -0.11, + -0.11, + -0.11, + -0.11, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.1, + -0.09, + -0.09, + -0.09, + -0.09, + -0.09, + -0.09, + -0.09, + -0.09, + -0.09, + -0.09, + -0.08, + -0.08, + -0.08, + -0.08, + -0.08, + -0.08, + -0.08, + -0.08, + -0.08, + -0.08, + -0.07, + -0.07, + -0.07, + -0.07, + -0.07, + -0.07, + -0.07, + -0.07, + -0.07, + -0.07, + -0.06, + -0.06, + -0.06, + -0.06, + -0.06, + -0.06, + -0.06, + -0.06, + -0.06, + -0.06, + -0.05, + -0.05, + -0.05, + -0.05, + -0.05, + -0.05, + -0.05, + -0.05, + -0.05, + -0.05, + -0.04, + -0.04, + -0.04, + -0.04, + -0.04, + -0.04, + -0.04, + -0.04, + -0.04, + -0.04, + -0.03, + -0.03, + -0.03, + -0.03, + -0.03, + -0.03, + -0.03, + -0.03, + -0.03, + -0.03, + -0.02, + -0.02, + -0.02, + -0.02, + -0.02, + -0.02, + -0.02, + -0.02, + -0.02, + -0.02, + -0.01, + -0.01, + -0.01, + -0.01, + -0.01, + -0.01, + -0.01, + -0.01, + -0.01, + -0.01, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.01, + 0.01, + 0.01, + 0.01, + 0.01, + 0.01, + 0.01, + 0.01, + 0.01, + 0.01, + 0.02, + 0.02, + 0.02, + 0.02, + 0.02, + 0.02, + 0.02, + 0.02, + 0.02, + 0.02, + 0.03, + 0.03, + 0.03, + 0.03, + 0.03, + 0.03, + 0.03, + 0.03, + 0.03, + 0.03, + 0.04, + 0.04, + 0.04, + 0.04, + 0.04, + 0.04, + 0.04, + 0.04, + 0.04, + 0.04, + 0.05, + 0.05, + 0.05, + 0.05, + 0.05, + 0.05, + 0.05, + 0.05, + 0.05, + 0.05, + 0.06, + 0.06, + 0.06, + 0.06, + 0.06, + 0.06, + 0.06, + 0.06, + 0.06, + 0.06, + 0.07, + 0.07, + 0.07, + 0.07, + 0.07, + 0.07, + 0.07, + 0.07, + 0.07, + 0.07, + 0.08, + 0.08, + 0.08, + 0.08, + 0.08, + 0.08, + 0.08, + 0.08, + 0.08, + 0.08, + 0.09, + 0.09, + 0.09, + 0.09, + 0.09, + 0.09, + 0.09, + 0.09, + 0.09, + 0.09, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.11, + 0.11, + 0.11, + 0.11, + 0.11, + 0.11, + 0.11, + 0.11, + 0.11, + 0.11, + 0.12, + 0.12, + 0.12, + 0.12, + 0.12, + 0.12, + 0.12, + 0.12, + 0.12, + 0.12, + 0.13, + 0.13, + 0.13, + 0.13, + 0.13, + 0.13, + 0.13, + 0.13, + 0.13, + 0.13, + 0.14, + 0.14, + 0.14, + 0.14, + 0.14, + 0.14, + 0.14, + 0.14, + 0.14, + 0.14, + 0.15, + 0.15, + 0.15, + 0.15, + 0.15 + ] + }, + "label": "rarith.round2", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.round3.json b/tests/_r_cache/rarith.round3.json new file mode 100644 index 00000000..c2149070 --- /dev/null +++ b/tests/_r_cache/rarith.round3.json @@ -0,0 +1,1509 @@ +{ + "entries": { + "2fc2d49b7b74fa34dd2c2ef215d493e069776b7e0b2cf01b02ec24dfde800e92": [ + -0.749, + -1.103, + -1.272, + 1.441, + 1.43, + -1.586, + -0.844, + 0.646, + 0.805, + -0.245, + -0.074, + 0.96, + -0.244, + 1.839, + -0.762, + -1.553, + -0.987, + -1.777, + 1.688, + 0.595, + -1.215, + -1.853, + 1.387, + -0.516, + -1.163, + -0.374, + -1.348, + -1.714, + -1.913, + -1.966, + 0.473, + -0.68, + -0.973, + -0.163, + 0.522, + -1.033, + -1.486, + 0.682, + -0.88, + 1.386, + 0.045, + 1.931, + -1.275, + -1.496, + -1.366, + 1.907, + 0.728, + 0.391, + -0.632, + -1.396, + -1.922, + 1.207, + -0.133, + 1.956, + -1.939, + 1.417, + -1.342, + 1.949, + -1.395, + -1.094, + -0.142, + 0.078, + -1.344, + 0.897, + 1.629, + -1.685, + -1.308, + 1.069, + -1.76, + 0.716, + 0.957, + 1.589, + 0.163, + -0.519, + -1.628, + -0.927, + -1.389, + 0.06, + -1.895, + 1.029, + 1.964, + -1.418, + 0.609, + 1.578, + -1.109, + 1.642, + 0.902, + 1.027, + -0.98, + 0.816, + 1.174, + 1.587, + 0.26, + -1.606, + 0.577, + -0.398, + 1.519, + -0.608, + 0.431, + 0.168, + -1.489, + 0.926, + -0.181, + 0.117, + -0.282, + -1.884, + -1.537, + 0.041, + -0.345, + -0.33, + -1.132, + 0.29, + -0.234, + 0.039, + 0.467, + 1.909, + 0.868, + 0.48, + 0.796, + -0.928, + 1.029, + -0.987, + 0.675, + -1.111, + 1.214, + -0.603, + 1.662, + 1.192, + 1.761, + 1.063, + -0.546, + -1.241, + 1.61, + -1.793, + 1.835, + -1.357, + 0.371, + 0.057, + 0.768, + -1.417, + -0.491, + 0.33, + 1.194, + 0.334, + 0.696, + -0.955, + -0.339, + 0.481, + -0.595, + 1.18, + 1.406, + -1.945, + -0.265, + -1.094, + 0.543, + 1.625, + -0.853, + -0.652, + -0.297, + 0.008, + -0.063, + -0.025, + 1.289, + -0.683, + 0.062, + -1.244, + -0.539, + -0.151, + 1.796, + 0.998, + -0.962, + 1.73, + 1.219, + 1.467, + 1.031, + -1.419, + -0.725, + -1.555, + 0.581, + 1.099, + -0.999, + 0.185, + -1.813, + -0.245, + -1.883, + -1.216, + 0.563, + 1.777, + 1.241, + 0.679, + -0.37, + -1.546, + 1.411, + 0.966, + 0.747, + -1.08, + -0.027, + -0.786, + -0.586, + 0.605, + 0.738, + -0.694, + 0.893, + -1.161, + 1.565, + 0.027, + -1.558, + -0.926, + -0.058, + -0.725, + 0.604, + 0.193, + -0.169, + 0.904, + -1.072, + -1.032, + 1.427, + -0.35, + 1.69, + 1.854, + 1.064, + -1.812, + -0.767, + 1.205, + -1.275, + 1.891, + -1.951, + 1.49, + -0.764, + -0.018, + 0.037, + -1.771, + 0.538, + 0.369, + 1.624, + -0.579, + 1.909, + -1.807, + 1.906, + 1.785, + -0.054, + 1.718, + -0.878, + -0.227, + -0.871, + 1.735, + -1.329, + -1.682, + -1.946, + 0.337, + 1.03, + -0.354, + -1.359, + 1.607, + 1.767, + -1.699, + -1.572, + 0.569, + -0.525, + -1.623, + 0.461, + 1.603, + -0.852, + 0.377, + 1.107, + 0.292, + -0.373, + 0.4, + -1.354, + 1.27, + -0.959, + -0.924, + -1.077, + 1.373, + 0.73, + -1.171, + 1.768, + 0.294, + -0.823, + -0.369, + -0.455, + -1.084, + 0.865, + 0.872, + 1.276, + -0.227, + 1.934, + 0.526, + -1.708, + -1.424, + -0.517, + 0.26, + -1.446, + 0.094, + 1.983, + 0.715, + 0.606, + -0.888, + -1.473, + 0.266, + -0.034, + 0.896, + 1.976, + -0.139, + -1.699, + 0.994, + -1.832, + -1.21, + 1.328, + 1.891, + -1.234, + -0.931, + 1.757, + 0.293, + 1.425, + 1.4, + -1.562, + 0.558, + 1.369, + 0.66, + 0.528, + -0.305, + -0.417, + 0.565, + -1.556, + 1.033, + 0.185, + -1.668, + -0.023, + -0.376, + -1.346, + 0.491, + 0.121, + 1.942, + -1.337, + 0.279, + -1.644, + 1.753, + 0.889, + 1.125, + -0.534, + 1.296, + -1.762, + -1.003, + -1.461, + -0.941, + -0.449, + -1.648, + 0.691, + 1.108, + -0.48, + 0.68, + -0.77, + 0.192, + -1.531, + 1.906, + -1.457, + -0.107, + -1.379, + -1.508, + 0.33, + 1.907, + -0.914, + 1.13, + 1.922, + 0.134, + -1.841, + -0.493, + -0.062, + -0.517, + -0.512, + 0.08, + -1.775, + 0.754, + -0.042, + -1.599, + -1.703, + -0.791, + -0.449, + 1.827, + 1.164, + 0.029, + 0.968, + 0.159, + -0.392, + -1.266, + 1.665, + -1.103, + -1.829, + 1.298, + -0.215, + 0.752, + -1.685, + 1.251, + -0.851, + -0.778, + -1.515, + -1.812, + -0.835, + -1.513, + -0.295, + -0.483, + -0.458, + -0.027, + 0.559, + -1.607, + 1.252, + -1.191, + -0.663, + 0.746, + 1.957, + -1.363, + 1.647, + -1.286, + 0.325, + -0.015, + -1.021, + -0.171, + -1.761, + 0.002, + 0.247, + 0.094, + -0.361, + -0.674, + -1.548, + -0.366, + -0.874, + -1.078, + 1.853, + 1.701, + -1.828, + -1.206, + 1.888, + -1.994, + -1.35, + 1.893, + 1.452, + -0.987, + 0.272, + -0.238, + -0.237, + -1.891, + -0.356, + 1.774, + -1.766, + -1.491, + 1.978, + 1.658, + 0.834, + 0.369, + -0.184, + -0.301, + 1.877, + 1.963, + -1.427, + 1.06, + 0.973, + -1.043, + -0.192, + 1.66, + -1.994, + 0.696, + -1.944, + 1.414, + 0.568, + -1.753, + -1.932, + -1.16, + 0.805, + 1.096, + -1.21, + 0.267, + -0.511, + 1.696, + -0.33, + -1.242, + -0.293, + 1.667, + -1.283, + 0.925, + -1.144, + 0.56, + 1.585, + 0.951, + -0.767, + 1.765, + 1.514, + 1.267, + 1.829, + -0.094, + 1.856, + -1.492, + 0.362, + -1.302, + -0.049, + -1.787, + 1.232, + -0.79, + 0.542, + 0.037, + 1.291, + -1.835, + 0.739, + 0.923, + -1.405, + 0.584, + -0.643, + -0.726, + 0.94, + -1.867, + -0.313, + -1.215, + 0.863, + 0.281, + -1.126, + -1.345, + -0.845, + -0.714, + 1.435, + -1.273, + 0.903, + -1.982, + -1.679, + 1.385, + 1.184, + 0.08, + 0.605, + 0.857, + 1.687, + 0.292, + 0.089, + 0.347, + 0.694, + -1.666, + 0.214, + -1.661, + 0.575, + -1.365, + -1.391, + -1.322, + 0.24, + -1.971, + -1.865, + 1.252, + 1.479, + 1.294, + -1.191, + -1.631, + 0.806, + -0.584, + 0.577, + -0.41, + -1.691, + 0.569, + -0.152, + -0.317, + -1.614, + -0.049, + 1.597, + 1.56, + 0.432, + 0.421, + 0.074, + -1.515, + 0.829, + 0.044, + 1.804, + 0.905, + -1.946, + 0.318, + -1.464, + -1.54, + -1.058, + -1.732, + -0.287, + -0.067, + 0.305, + -0.253, + -1.307, + 0.174, + -1.322, + 0.951, + -1.054, + 0.424, + -1.288, + -1.728, + -1.712, + 1.581, + 0.736, + -0.326, + 1.242, + 1.734, + 0.317, + -1.172, + 1.321, + 1.784, + 0.926, + 1.496, + 0.634, + -1.006, + -1.335, + -1.94, + -1.34, + -0.414, + 1.46, + -0.521, + 1.927, + 0.312, + 1.04, + 1.153, + 1.553, + -1.015, + -1.947, + 1.387, + 1.7, + -1.558, + -1.456, + -1.377, + -0.094, + -1.298, + 0.906, + 0.714, + 0.083, + 0.848, + 1.122, + 0.11, + 1.625, + -1.584, + 1.237, + 1.82, + 0.39, + 0.872, + 0.29, + 1.98, + 0.762, + 1.256, + 0.379, + 1.773, + -0.74, + -0.824, + -0.58, + -0.566, + -1.879, + 1.161, + 0.714, + 1.344, + -1.522, + -0.278, + 1.122, + 0.815, + 1.64, + 0.196, + 1.885, + 1.296, + -0.815, + 1.701, + 1.801, + -0.163, + -1.527, + 0.179, + 1.717, + 0.878, + 1.944, + 1.329, + -0.811, + 0.184, + -0.284, + -0.945, + -0.92, + -1.674, + 1.12, + 0.275, + -0.886, + 1.501, + -1.288, + 0.161, + 0.563, + -1.258, + 1.258, + 1.847, + 1.11, + -0.093, + 1.253, + 1.896, + 1.307, + -1.382, + 1.872, + 1.558, + 0.125, + 0.325, + 1.718, + -1.728, + 0.498, + -1.142, + 0.338, + -0.494, + 0.532, + 0.862, + -1.673, + -1.644, + 0.064, + 0.886, + 0.67, + -1.952, + 1.085, + 0.985, + -0.546, + -0.26, + 0.692, + -0.659, + -0.117, + -1.972, + 1.302, + 0.4, + -0.585, + 0.155, + 1.805, + 1.11, + -1.507, + 0.938, + -0.928, + 0.214, + -1.422, + 1.902, + -1.907, + 1.702, + 0.216, + -0.035, + 0.987, + 0.823, + 1.983, + -0.112, + 1.694, + 0.275, + -1.898, + 0.036, + 1.392, + 1.631, + 1.135, + -0.668, + 1.658, + 0.402, + -1.426, + -1.526, + -0.668, + 1.788, + -1.963, + 1.134, + 1.726, + 1.547, + 0.456, + 0.166, + -1.652, + -1.017, + -1.632, + 0.648, + 1.37, + 0.115, + 0.02, + -0.238, + 0.724, + 0.216, + -0.56, + -0.642, + -1.648, + 1.72, + -0.917, + -1.404, + 0.585, + -1.696, + -0.457, + 0.477, + -0.963, + -1.9, + 1.066, + 1.224, + 0.708, + 0.111, + -0.873, + -1.059, + -1.147, + 1.631, + 0.021, + 0.549, + 1.923, + -1.269, + 0.862, + -1.866, + 1.113, + -1.721, + 0.616, + -1.145, + 1.81, + -1.338, + -0.948, + -1.064, + 0.573, + -1.814, + 1.476, + 0.679, + 0.175, + -1.561, + -0.661, + 0.607, + 1.409, + -1.708, + 1.481, + 1.806, + 0.324, + 0.128, + -0.587, + -1.532, + 0.603, + 1.224, + 0.633, + -0.806, + 0.847, + -1.929, + -1.602, + 1.213, + 1.086, + 1.352, + 0.435, + 1.375, + -1.932, + -0.746, + -0.511, + -1.852, + 1.492, + 1.438, + 1.305, + -0.491, + -0.998, + 0.572, + 0.592, + -1.764, + 0.439, + -1.748, + -1.052, + 0.94, + 1.528, + 1.085, + 0.913, + 0.357, + 0.755, + 0.174, + 1.208, + 1.549, + -1.509, + 0.483, + -1.985, + 0.222, + 1.91, + 1.214, + -0.934, + 1.863, + -0.315, + -1.738, + -1.076, + 0.885, + -0.293, + 1.308, + 1.407, + -1.21, + -1.981, + 0.039, + 1.265, + 1.004, + -0.42, + -0.246, + -0.144, + -1.231, + 1.517, + 0.253, + -1.817, + 0.833, + -1.015, + -0.172, + -1.525, + 1.791, + -0.634, + 1.726, + -0.511, + -1.662, + -1.004, + -0.164, + 0.26, + 0.524, + -0.795, + -1.223, + 1.158, + 1.011, + 0.635, + 1.433, + 0.155, + 0.906, + -0.42, + -0.916, + -1.618, + -1.186, + 0.237, + 0.216, + 0.323, + 0.311, + 0.264, + 0.267, + 0.279, + 0.279, + 0.321, + 0.236, + 0.219, + 0.282, + 0.209, + 0.273, + 0.209, + 0.225, + 0.237, + 0.201, + 0.253, + 0.231, + 0.204, + 0.327, + 0.305, + 0.21, + 0.27, + 0.205, + 0.267, + 0.307, + 0.313, + 0.249, + 0.236, + 0.309, + 0.328, + 0.302, + 0.228, + 0.202, + 0.271, + 0.268, + 0.239, + 0.277, + 0.331, + 0.243, + 0.309, + 0.321, + 0.253, + 0.332, + 0.287, + 0.302, + 0.257, + 0.309, + 0.321, + 0.331, + 0.323, + 0.286, + 0.321, + 0.306, + 0.262, + 0.303, + 0.284, + 0.289, + 0.261, + 0.311, + 0.32, + 0.267, + 0.32, + 0.297, + 0.259, + 0.324, + 0.236, + 0.243, + 0.296, + 0.254, + 0.224, + 0.254, + 0.232, + 0.259, + 0.216, + 0.212, + 0.323, + 0.254, + 0.269, + 0.284, + 0.303, + 0.287, + 0.244, + 0.211, + 0.259, + 0.297, + 0.289, + 0.288, + 0.21, + 0.226, + 0.291, + 0.262, + 0.268, + 0.238, + 0.294, + 0.282, + 0.312, + 0.249, + 0.283, + 0.216, + 0.267, + 0.232, + 0.221, + 0.211, + 0.268, + 0.259, + 0.213, + 0.307, + 0.279, + 0.288, + 0.23, + 0.227, + 0.289, + 0.326, + 0.279, + 0.206, + 0.292, + 0.288, + 0.285, + 0.282, + 0.299, + 0.259, + 0.289, + 0.317, + 0.314, + 0.297, + 0.222, + 0.302, + 0.304, + 0.264, + 0.328, + 0.258, + 0.285, + 0.291, + 0.237, + 0.281, + 0.28, + 0.223, + 0.214, + 0.269, + 0.241, + 0.213, + 0.292, + 0.225, + 0.317, + 0.263, + 0.207, + 0.296, + 0.217, + 0.251, + 0.251, + 0.216, + 0.265, + 0.295, + 0.273, + 0.275, + 0.329, + 0.326, + 0.273, + 0.248, + 0.33, + 0.242, + 0.229, + 0.228, + 0.247, + 0.236, + 0.224, + 0.254, + 0.231, + 0.271, + 0.267, + 0.26, + 0.206, + 0.224, + 0.279, + 0.239, + 0.216, + 0.259, + 0.329, + 0.295, + 0.211, + 0.287, + 0.311, + 0.285, + 0.315, + 0.279, + 0.29, + 0.275, + 0.244, + 0.208, + 0.237, + 0.285, + 0.324, + 0.255, + 0.241, + 0.216, + 0.298, + 0.275, + 0.224, + 0.207, + 0.234, + 0.292, + 0.314, + 0.289, + 0.331, + 0.273, + 0.311, + 0.303, + 0.32, + 0.296, + 0.272, + 0.306, + 0.279, + 0.312, + 0.227, + 0.306, + 0.275, + 0.246, + 0.302, + 0.319, + 0.209, + 0.304, + 0.206, + 0.2, + 0.262, + 0.307, + 0.29, + 0.305, + 0.328, + 0.331, + 0.309, + 0.251, + 0.21, + 0.258, + 0.251, + 0.23, + 0.271, + 0.322, + 0.267, + 0.279, + 0.206, + 0.209, + 0.325, + 0.208, + 0.212, + 0.325, + 0.32, + 0.303, + 0.283, + 0.333, + 0.261, + 0.234, + 0.212, + 0.251, + 0.222, + 0.257, + 0.328, + 0.214, + 0.269, + 0.284, + 0.284, + 0.209, + 0.3, + 0.238, + 0.24, + 0.233, + 0.217, + 0.22, + 0.243, + 0.262, + 0.23, + 0.277, + 0.207, + 0.271, + 0.233, + 0.205, + 0.213, + 0.269, + 0.269, + 0.251, + 0.23, + 0.297, + 0.272, + 0.294, + 0.289, + 0.308, + 0.208, + 0.201, + 0.328, + 0.257, + 0.243, + 0.283, + 0.285, + 0.233, + 0.241, + 0.259, + 0.307, + 0.213, + -0.15, + -0.148, + -0.148, + -0.146, + -0.146, + -0.144, + -0.144, + -0.142, + -0.142, + -0.14, + -0.14, + -0.138, + -0.138, + -0.136, + -0.136, + -0.134, + -0.134, + -0.132, + -0.132, + -0.13, + -0.13, + -0.128, + -0.128, + -0.126, + -0.126, + -0.124, + -0.124, + -0.122, + -0.122, + -0.12, + -0.12, + -0.118, + -0.117, + -0.116, + -0.116, + -0.114, + -0.114, + -0.112, + -0.112, + -0.11, + -0.11, + -0.108, + -0.108, + -0.106, + -0.106, + -0.104, + -0.104, + -0.102, + -0.101, + -0.1, + -0.1, + -0.098, + -0.098, + -0.096, + -0.096, + -0.094, + -0.094, + -0.092, + -0.092, + -0.09, + -0.09, + -0.088, + -0.088, + -0.086, + -0.085, + -0.084, + -0.084, + -0.082, + -0.082, + -0.08, + -0.08, + -0.078, + -0.078, + -0.076, + -0.076, + -0.074, + -0.074, + -0.072, + -0.072, + -0.07, + -0.07, + -0.068, + -0.068, + -0.066, + -0.066, + -0.064, + -0.064, + -0.062, + -0.062, + -0.06, + -0.06, + -0.058, + -0.058, + -0.056, + -0.056, + -0.054, + -0.054, + -0.052, + -0.052, + -0.05, + -0.05, + -0.048, + -0.048, + -0.046, + -0.046, + -0.044, + -0.044, + -0.042, + -0.042, + -0.04, + -0.04, + -0.038, + -0.038, + -0.036, + -0.035, + -0.034, + -0.034, + -0.032, + -0.032, + -0.03, + -0.029, + -0.028, + -0.028, + -0.026, + -0.025, + -0.024, + -0.024, + -0.022, + -0.021, + -0.02, + -0.02, + -0.018, + -0.017, + -0.016, + -0.016, + -0.014, + -0.014, + -0.012, + -0.012, + -0.01, + -0.01, + -0.008, + -0.007, + -0.007, + -0.005, + -0.005, + -0.004, + -0.002, + -0.002, + 0.0, + 0.0, + 0.002, + 0.002, + 0.004, + 0.005, + 0.005, + 0.007, + 0.007, + 0.009, + 0.01, + 0.011, + 0.012, + 0.013, + 0.014, + 0.015, + 0.016, + 0.016, + 0.018, + 0.018, + 0.02, + 0.02, + 0.022, + 0.022, + 0.024, + 0.024, + 0.026, + 0.026, + 0.028, + 0.028, + 0.03, + 0.03, + 0.032, + 0.032, + 0.034, + 0.034, + 0.036, + 0.036, + 0.038, + 0.038, + 0.04, + 0.04, + 0.042, + 0.043, + 0.044, + 0.044, + 0.046, + 0.046, + 0.048, + 0.048, + 0.05, + 0.051, + 0.052, + 0.052, + 0.054, + 0.054, + 0.056, + 0.056, + 0.058, + 0.059, + 0.06, + 0.06, + 0.062, + 0.062, + 0.064, + 0.064, + 0.066, + 0.066, + 0.068, + 0.068, + 0.07, + 0.071, + 0.072, + 0.072, + 0.074, + 0.074, + 0.076, + 0.076, + 0.078, + 0.078, + 0.08, + 0.08, + 0.082, + 0.082, + 0.084, + 0.084, + 0.086, + 0.086, + 0.088, + 0.088, + 0.09, + 0.09, + 0.092, + 0.092, + 0.094, + 0.094, + 0.096, + 0.096, + 0.098, + 0.098, + 0.1, + 0.1, + 0.102, + 0.102, + 0.104, + 0.104, + 0.106, + 0.106, + 0.108, + 0.108, + 0.11, + 0.11, + 0.112, + 0.112, + 0.114, + 0.114, + 0.116, + 0.116, + 0.118, + 0.118, + 0.12, + 0.12, + 0.122, + 0.122, + 0.124, + 0.124, + 0.126, + 0.126, + 0.128, + 0.128, + 0.13, + 0.13, + 0.132, + 0.132, + 0.134, + 0.134, + 0.136, + 0.136, + 0.138, + 0.138, + 0.14, + 0.141, + 0.142, + 0.142, + 0.144, + 0.144, + 0.146, + 0.146, + 0.148, + 0.148, + 0.15 + ] + }, + "label": "rarith.round3", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.round6.json b/tests/_r_cache/rarith.round6.json new file mode 100644 index 00000000..7f56352c --- /dev/null +++ b/tests/_r_cache/rarith.round6.json @@ -0,0 +1,1509 @@ +{ + "entries": { + "3c2a798a6b5f3a8f302212044e4f75fda220d4669e2f292209790703b4980ec2": [ + 1.929631, + -1.838399, + -1.508853, + -0.142938, + 1.857769, + -1.444803, + -0.697579, + -1.233169, + -0.713427, + -1.779009, + 0.543659, + -1.725677, + 1.900402, + -1.394995, + -0.819236, + -1.792832, + 0.852245, + -0.402865, + 0.074518, + 0.763623, + -1.825023, + -0.958251, + -0.324269, + -1.628229, + -1.670353, + -1.981984, + 0.789741, + -0.296921, + 1.542723, + -1.436414, + -0.436098, + 1.92082, + -1.217206, + -0.705224, + 1.974638, + 1.555376, + 1.538609, + -1.004718, + 1.613556, + -0.920633, + 0.156876, + 0.733174, + 1.045296, + -1.378775, + 1.465572, + -1.583246, + 0.813969, + 1.234731, + -1.917129, + -0.519904, + -0.107957, + 0.634506, + -0.992339, + 1.475894, + 1.962854, + -1.532413, + 1.86794, + -0.51089, + 0.277095, + -0.39127, + 0.676218, + -1.124499, + 1.117745, + -0.501187, + 0.157498, + 1.901338, + -1.465512, + -0.011493, + 1.724743, + 1.164057, + 1.987232, + 0.807053, + 0.985953, + -0.30724, + -0.899323, + 0.365413, + -0.11101, + -0.729425, + -0.177695, + 1.13543, + 1.864919, + 0.097402, + 0.451017, + 1.918048, + 0.108659, + -1.056738, + -0.367633, + -1.374943, + -0.758551, + 0.034749, + 1.626081, + -0.920986, + 1.365205, + 0.95812, + -1.89176, + 0.209384, + 0.474386, + -1.531969, + 1.753592, + -1.671459, + -0.810394, + 1.585449, + 0.812557, + 1.471831, + -0.142906, + 1.204371, + -0.625361, + -0.234007, + -0.714314, + 0.772988, + -1.017604, + 1.037063, + 1.707162, + 0.395018, + -0.031017, + -1.716304, + -0.37703, + 0.115106, + 1.865796, + -0.313319, + -0.793632, + 0.592019, + 0.189771, + 1.965588, + 1.519098, + 0.262424, + -1.806595, + 1.27026, + 0.47629, + 1.401893, + 0.848412, + -1.216761, + 0.380811, + 0.698764, + -0.519674, + -0.191902, + 1.295278, + 0.73567, + -1.144544, + 0.821756, + 0.443288, + -1.616793, + -0.476897, + 1.835862, + -0.369623, + 0.835395, + -1.885451, + -1.399409, + 0.207653, + -1.670559, + -0.512295, + 1.069684, + -0.958085, + -0.355453, + -0.695048, + -1.260074, + 0.087517, + 0.65731, + -1.12783, + 0.433807, + 0.748153, + -0.447354, + -1.156221, + 0.775685, + -1.312728, + -0.483966, + 1.529001, + -1.678354, + -0.142959, + 0.302861, + -1.380795, + -1.478404, + -1.325242, + -1.542866, + 1.285451, + -1.701739, + 1.569679, + 1.137457, + 0.702284, + 1.894563, + 1.910207, + -1.453029, + -0.458389, + 1.834757, + 1.201378, + 0.2395, + -0.41984, + -1.88389, + 1.942084, + 1.74434, + 0.982715, + 0.877331, + 1.067938, + -1.131433, + -1.183507, + -1.5537, + 1.560503, + -0.074076, + 1.661322, + 0.502331, + -1.492204, + 1.613413, + -0.979982, + 1.739982, + -1.267927, + -1.104416, + 0.807836, + 0.501683, + -1.011758, + -1.352763, + 1.306126, + 1.971379, + 1.928585, + -0.299095, + -0.554168, + 1.894539, + -1.660079, + -0.994084, + -1.723207, + -1.357591, + -1.698864, + -0.293583, + 0.947586, + 1.547199, + 0.783219, + -1.398497, + 1.171273, + 0.495143, + 0.964648, + 1.242115, + -0.205079, + 1.615241, + 0.487675, + 1.158841, + -0.210588, + -0.046447, + 1.665409, + -1.576409, + -1.716513, + 0.848564, + -0.634237, + -0.991872, + -1.634594, + -1.237318, + 1.256472, + 1.35859, + -0.781931, + -1.188844, + -1.046346, + -1.864257, + -1.70504, + -0.89731, + -0.700983, + -1.452269, + 1.281262, + -1.073661, + 0.659999, + 0.412742, + 0.771226, + 1.657397, + 1.586685, + 1.625207, + 1.31554, + 0.488621, + 0.97111, + 0.708848, + 0.522409, + 0.353847, + 0.640438, + 1.149655, + -0.057578, + -0.907161, + 1.657076, + -1.616278, + 0.063328, + -1.074652, + 1.296397, + -0.220453, + 1.701521, + 1.381719, + -1.628807, + -1.888218, + 0.638772, + 0.436472, + -0.780932, + 1.070746, + -0.936272, + -0.154516, + 0.481794, + 1.002824, + -0.4341, + 0.426745, + 0.779786, + 1.862075, + 0.677129, + -1.607696, + 1.748403, + 0.832274, + -1.621934, + -0.025966, + -0.805643, + -0.790424, + -0.927641, + -0.642177, + 1.281366, + -0.641508, + 0.368417, + -0.527186, + 1.361227, + -0.97073, + -0.961453, + -1.457145, + 0.085259, + 0.043022, + -0.130781, + 0.996265, + -1.609944, + 0.984958, + -0.013794, + -0.154722, + -0.455191, + 0.725833, + -0.491252, + -0.087548, + -1.765681, + 0.045006, + -0.084289, + 1.380503, + -0.002089, + -0.84326, + 0.577511, + 1.739742, + 1.28725, + 1.588692, + -1.15001, + 0.83842, + 1.409429, + 0.777924, + 1.020131, + 1.654195, + -1.231766, + 0.271935, + -1.422687, + -1.915117, + -1.038122, + -0.93985, + -1.035559, + -1.088398, + -1.848886, + 1.781251, + 0.218201, + -0.787944, + 1.26524, + -1.544626, + -1.929998, + -1.618899, + 0.058669, + -1.374734, + 0.198617, + -1.439396, + 0.655347, + 1.46563, + 0.194784, + -1.996977, + 1.938767, + 1.474107, + -0.991993, + -1.362235, + -0.437943, + -0.310937, + -1.53961, + 1.358882, + 1.952976, + -0.541743, + -0.278628, + -0.163826, + 0.385791, + -1.956936, + 0.056257, + -1.478185, + 0.924378, + 0.535105, + -0.112177, + 0.767621, + 0.69562, + 0.097806, + -1.588465, + 0.626055, + -1.86119, + 0.193348, + 0.933963, + -0.97344, + -0.7953, + -1.253854, + 0.953573, + -0.020447, + 0.521927, + -0.13404, + -0.210416, + 1.763183, + 1.312044, + -0.734535, + 1.719406, + 1.409419, + -1.954982, + -1.01674, + 1.856298, + 0.782333, + 1.360449, + 1.857282, + -1.593564, + 1.689435, + -0.984678, + 1.873284, + -1.442594, + -1.08868, + 1.035611, + 0.018955, + -1.753098, + -1.705943, + 1.435129, + -1.182252, + -0.641215, + -0.016848, + -1.572306, + 1.403645, + 0.887318, + -1.099361, + 1.703661, + -1.385915, + 1.95332, + 1.81476, + 1.546654, + -0.77195, + -0.823757, + -1.242978, + 0.482351, + 1.707911, + 1.209691, + -0.420466, + -0.629192, + 0.892433, + -1.258338, + 0.020568, + -1.34229, + -1.79017, + 0.585322, + 0.968747, + 0.502861, + 0.142186, + 1.88417, + 1.058965, + -1.927894, + 0.256749, + 1.819708, + -0.404311, + -0.212666, + 1.124846, + 1.214466, + -0.780946, + 1.471178, + -0.832576, + 0.866633, + -0.471158, + 0.534152, + 0.18177, + -0.840509, + -0.573385, + 0.094512, + -1.343774, + -1.83668, + -1.468479, + -1.946719, + -1.838279, + 1.869771, + 0.307415, + 0.13089, + 1.20155, + -1.829175, + 1.655511, + 0.065138, + -1.989449, + -1.91142, + -1.096768, + -0.412675, + 1.556788, + -1.784244, + -1.214541, + 0.159118, + -0.028707, + 0.256935, + 1.223319, + 1.325152, + -0.796763, + -1.303039, + -0.902127, + 1.037996, + -0.56634, + 1.264797, + -1.657047, + -0.798205, + 1.116676, + -1.507027, + -0.986354, + -1.400432, + 0.967732, + 0.631123, + 0.133839, + -0.274796, + 0.354766, + 1.778249, + -1.359054, + 0.892575, + 1.62639, + 0.141946, + 0.894344, + -1.373994, + -0.18214, + 0.095797, + 0.431419, + -0.760736, + -1.545449, + 0.545848, + 1.529729, + -0.301327, + 1.707694, + 1.508002, + 0.16446, + -1.812365, + -1.895404, + -0.480609, + -1.743986, + -0.488786, + -0.296045, + 0.103975, + -0.566746, + -0.194685, + -1.770211, + 0.385484, + 1.745716, + 0.66355, + -0.882847, + -0.9325, + 1.065406, + -1.307073, + -0.546876, + 0.482933, + -0.308312, + -1.677252, + -0.584217, + -1.91259, + 1.393836, + -0.453357, + 1.104031, + -0.146954, + -0.886085, + 0.115347, + -1.503272, + -1.968283, + -0.055238, + -1.369684, + -1.247708, + -0.77627, + 1.665116, + -1.208683, + 1.555549, + 1.413193, + -1.165445, + 1.245849, + -1.807626, + -1.664977, + -0.265194, + -0.951985, + -0.9971, + 1.818526, + -1.200008, + 0.866436, + 0.738069, + -0.368278, + 1.169063, + 0.061701, + -1.626841, + -0.279468, + -1.141986, + -1.889933, + -0.593582, + 1.86393, + 1.379484, + -0.896922, + -1.449804, + 0.82553, + -1.515206, + 1.323567, + 0.982156, + -1.609231, + -0.660548, + 0.377655, + 1.885616, + 1.106391, + 1.258789, + 1.358, + -0.952, + -0.918, + -0.147, + 0.388, + -1.391, + -0.324, + -0.309, + -1.894, + 0.401, + -0.723, + -1.469, + 1.426, + 0.24, + 0.144, + 1.977, + 1.86, + 1.713, + -1.204, + 1.009, + 1.016, + -1.149, + -0.152, + 1.844, + 0.307, + 1.446, + 1.537, + 1.662, + 0.609, + -1.249, + 0.892, + 1.85, + 0.235, + 0.081, + -1.739, + 0.972, + -0.692, + 1.006, + -1.579, + 1.906, + -1.161, + 1.952, + 1.561, + 0.686, + -0.205, + -1.23, + 1.671, + 0.677, + 0.84, + -0.96, + 1.74, + -1.944, + -0.067, + 1.991, + 1.537, + 1.333, + 1.324, + 0.029, + 0.564, + 0.583, + 1.588, + 1.823, + 0.687, + 1.934, + 0.509, + -1.088, + -0.295, + -1.283, + 1.443, + 0.679, + -1.255, + -1.948, + -1.057, + 0.093, + -1.428, + 1.025, + -1.24, + 1.114, + -0.416, + -0.938, + -0.828, + -0.944, + 1.896, + 1.927, + -0.195, + 1.808, + -0.54, + -0.099, + 0.101, + 0.921, + 1.277, + -1.418, + -0.046, + 1.716, + -0.271, + -0.95, + 1.844, + 0.562, + 0.296, + -0.332, + 1.091, + 1.634, + 0.374, + 0.877, + 1.252, + 1.544, + -1.69, + 0.429, + 0.495, + -1.287, + 1.761, + 0.339, + 1.115, + -0.081, + -0.754, + -1.015, + 1.386, + 1.882, + -0.335, + -1.939, + -0.729, + -0.6, + -0.422, + -1.63, + 1.145, + -1.832, + 1.459, + 0.3, + 1.402, + -0.269, + -0.297, + 1.588, + 0.307, + -1.939, + -1.907, + 0.81, + -0.892, + -1.677, + 1.945, + 1.923, + -0.94, + -1.208, + -0.602, + -0.577, + -1.734, + -1.667, + 0.611, + 0.672, + 0.863, + -0.114, + -1.039, + -1.944, + 1.018, + 1.609, + -1.05, + 1.805, + -1.235, + 0.273, + -1.987, + -1.725, + 0.783, + 0.654, + -0.79, + 1.352, + 0.866, + 0.04, + -1.172, + -0.47, + -1.92, + -0.118, + -1.059, + -0.33, + 1.584, + -1.389, + -0.913, + -1.023, + 0.001, + -1.402, + 0.493, + -0.689, + -1.698, + 0.077, + 1.574, + 0.761, + 0.596, + -1.553, + 1.667, + 0.946, + 1.029, + -0.269, + -1.501, + -0.516, + 1.452, + 1.89, + -1.206, + 0.01, + -1.521, + -1.179, + 0.243, + -1.623, + -0.336, + -1.994, + 1.481, + -1.188, + -0.047, + 0.046, + 1.902, + 0.401, + 0.82, + -1.616, + 0.812, + 1.747, + -1.627, + -0.021, + 0.831, + 0.465, + 0.634, + -0.67, + -0.047, + 1.228, + 0.782, + -0.081, + -0.268, + -1.252, + -0.994, + 0.199, + 1.438, + 1.992, + 1.225, + 1.634, + -0.939, + -0.463, + -1.592, + 0.213, + 1.524, + -0.714, + 1.102, + -0.844, + 0.911, + -1.653, + -0.615, + 0.621, + 0.974, + -1.165, + -1.169, + 1.021, + -0.997, + 1.379, + 1.739, + -0.131, + 1.86, + -1.75, + 1.486, + 1.41, + -0.83, + 0.261, + 1.171, + 0.555, + -0.928, + 0.725, + 1.061, + 0.092, + 1.261, + 0.924, + 1.62, + -1.427, + 1.831, + -0.684, + 1.351, + -1.343, + -1.532, + 1.484, + -1.167, + 0.809, + -0.461, + -1.365, + -0.39, + -1.131, + 1.041, + 1.912, + -1.12, + -1.849, + -1.787, + 1.513, + 1.783, + 1.451, + 1.827, + 1.495, + -1.811, + 1.697, + 1.787, + -1.669, + -1.797, + 0.515, + 0.65, + 1.008, + -0.302, + -1.645, + 1.91, + 0.194, + 0.215824, + 0.259793, + 0.308487, + 0.216793, + 0.279571, + 0.269661, + 0.253111, + 0.244345, + 0.214799, + 0.275062, + 0.240685, + 0.293875, + 0.302279, + 0.275563, + 0.233842, + 0.251704, + 0.203455, + 0.258626, + 0.332958, + 0.231181, + 0.277438, + 0.312874, + 0.237987, + 0.305026, + 0.20026, + 0.26584, + 0.330238, + 0.25181, + 0.309174, + 0.326439, + 0.307849, + 0.327352, + 0.280026, + 0.20429, + 0.333316, + 0.270085, + 0.262065, + 0.229857, + 0.268062, + 0.209416, + 0.319839, + 0.260539, + 0.288309, + 0.290876, + 0.284838, + 0.266617, + 0.225714, + 0.282607, + 0.280017, + 0.257942, + 0.26754, + 0.283718, + 0.248714, + 0.32023, + 0.289162, + 0.328085, + 0.289557, + 0.201146, + 0.293021, + 0.319126, + 0.233224, + 0.296847, + 0.218064, + 0.282008, + 0.30151, + 0.257877, + 0.222229, + 0.321179, + 0.222506, + 0.262734, + 0.332927, + 0.276192, + 0.280807, + 0.264871, + 0.276024, + 0.221977, + 0.234396, + 0.283609, + 0.323492, + 0.249429, + 0.212791, + 0.206416, + 0.237156, + 0.263619, + 0.260361, + 0.252978, + 0.23453, + 0.259149, + 0.247614, + 0.295449, + 0.265465, + 0.294622, + 0.292324, + 0.223513, + 0.273977, + 0.218675, + 0.292682, + 0.332912, + 0.2178, + 0.208164, + 0.281722, + 0.296357, + 0.31741, + 0.203831, + 0.285032, + 0.316106, + 0.210339, + 0.284156, + 0.295906, + 0.269878, + 0.255115, + 0.270314, + 0.270492, + 0.312654, + 0.223049, + 0.317125, + 0.316465, + 0.242579, + 0.279873, + 0.25855, + 0.321964, + 0.32102, + 0.216174, + 0.306272, + 0.242801, + 0.288874, + 0.285071, + 0.206457, + 0.323081, + 0.291455, + 0.288558, + 0.251298, + 0.292518, + 0.226163, + 0.284547, + 0.275421, + 0.213081, + 0.3074, + 0.265058, + 0.326302, + 0.292784, + 0.206815, + 0.211302, + 0.323163, + 0.248159, + 0.283248, + 0.212118, + 0.235562, + 0.326676, + 0.279229, + 0.235837, + 0.205696, + 0.292199, + 0.243032, + 0.234026, + 0.323889, + 0.298376, + 0.283074, + 0.320454, + 0.242904, + 0.291765, + 0.304457, + 0.272213, + 0.316862, + 0.311126, + 0.26364, + 0.233065, + 0.248107, + 0.304186, + 0.25999, + 0.281266, + 0.270565, + 0.277936, + 0.241749, + 0.243771, + 0.249678, + 0.269291, + 0.308519, + 0.286287, + 0.212648, + 0.266513, + 0.288632, + 0.315695, + 0.326338, + 0.316588, + 0.258751, + 0.251824, + 0.280902, + 0.295023, + 0.231601, + 0.27228, + 0.254924, + 0.265435, + 0.316969, + 0.310332, + 0.255867, + 0.224107, + 0.20864, + 0.253686, + 0.261529, + 0.277673, + 0.238181, + 0.255669, + 0.215683, + 0.204024, + 0.28731, + 0.216235, + 0.302358, + 0.270419, + 0.308274, + 0.309209, + 0.316459, + 0.278981, + 0.26061, + 0.29279, + 0.253607, + 0.250713, + 0.201487, + 0.244171, + 0.288321, + 0.216909, + 0.284236, + 0.307932, + 0.315627, + 0.293703, + 0.293788, + 0.292289, + 0.222035, + 0.20523, + 0.232088, + 0.31294, + 0.328936, + 0.284133, + 0.318912, + 0.331263, + 0.202918, + 0.210184, + 0.230061, + 0.205769, + 0.291389, + 0.277837, + 0.201998, + 0.221886, + 0.214518, + 0.308259, + 0.282074, + 0.222241, + 0.210344, + 0.245376, + 0.328701, + 0.297805, + 0.206614, + 0.210705, + 0.29793, + 0.301854, + 0.272532, + 0.258323, + 0.294648, + 0.306273, + 0.271804, + 0.257233, + 0.278438, + 0.288014, + 0.314312, + 0.224689, + 0.269246, + 0.317166, + 0.329623, + 0.307245, + 0.230484, + 0.2289, + 0.286172, + 0.235182, + 0.261906, + 0.328871, + 0.260942, + 0.332941, + 0.294529, + 0.213994, + 0.283685, + 0.286564, + 0.308548, + 0.24783, + 0.213724, + 0.327275, + 0.211383, + 0.203057, + 0.243174, + 0.208241, + 0.203587, + 0.328229, + 0.242066, + 0.303541, + 0.241426, + 0.25067, + 0.262386, + 0.327249, + 0.330519, + 0.2266, + 0.200111, + -0.1495, + -0.1485, + -0.1475, + -0.1465, + -0.1455, + -0.1445, + -0.1435, + -0.1425, + -0.1415, + -0.1405, + -0.1395, + -0.1385, + -0.1375, + -0.1365, + -0.1355, + -0.1345, + -0.1335, + -0.1325, + -0.1315, + -0.1305, + -0.1295, + -0.1285, + -0.1275, + -0.1265, + -0.1255, + -0.1245, + -0.1235, + -0.1225, + -0.1215, + -0.1205, + -0.1195, + -0.1185, + -0.1175, + -0.1165, + -0.1155, + -0.1145, + -0.1135, + -0.1125, + -0.1115, + -0.1105, + -0.1095, + -0.1085, + -0.1075, + -0.1065, + -0.1055, + -0.1045, + -0.1035, + -0.1025, + -0.1015, + -0.1005, + -0.0995, + -0.0985, + -0.0975, + -0.0965, + -0.0955, + -0.0945, + -0.0935, + -0.0925, + -0.0915, + -0.0905, + -0.0895, + -0.0885, + -0.0875, + -0.0865, + -0.0855, + -0.0845, + -0.0835, + -0.0825, + -0.0815, + -0.0805, + -0.0795, + -0.0785, + -0.0775, + -0.0765, + -0.0755, + -0.0745, + -0.0735, + -0.0725, + -0.0715, + -0.0705, + -0.0695, + -0.0685, + -0.0675, + -0.0665, + -0.0655, + -0.0645, + -0.0635, + -0.0625, + -0.0615, + -0.0605, + -0.0595, + -0.0585, + -0.0575, + -0.0565, + -0.0555, + -0.0545, + -0.0535, + -0.0525, + -0.0515, + -0.0505, + -0.0495, + -0.0485, + -0.0475, + -0.0465, + -0.0455, + -0.0445, + -0.0435, + -0.0425, + -0.0415, + -0.0405, + -0.0395, + -0.0385, + -0.0375, + -0.0365, + -0.0355, + -0.0345, + -0.0335, + -0.0325, + -0.0315, + -0.0305, + -0.0295, + -0.0285, + -0.0275, + -0.0265, + -0.0255, + -0.0245, + -0.0235, + -0.0225, + -0.0215, + -0.0205, + -0.0195, + -0.0185, + -0.0175, + -0.0165, + -0.0155, + -0.0145, + -0.0135, + -0.0125, + -0.0115, + -0.0105, + -0.0095, + -0.0085, + -0.0075, + -0.0065, + -0.0055, + -0.0045, + -0.0035, + -0.0025, + -0.0015, + -0.0005, + 0.0005, + 0.0015, + 0.0025, + 0.0035, + 0.0045, + 0.0055, + 0.0065, + 0.0075, + 0.0085, + 0.0095, + 0.0105, + 0.0115, + 0.0125, + 0.0135, + 0.0145, + 0.0155, + 0.0165, + 0.0175, + 0.0185, + 0.0195, + 0.0205, + 0.0215, + 0.0225, + 0.0235, + 0.0245, + 0.0255, + 0.0265, + 0.0275, + 0.0285, + 0.0295, + 0.0305, + 0.0315, + 0.0325, + 0.0335, + 0.0345, + 0.0355, + 0.0365, + 0.0375, + 0.0385, + 0.0395, + 0.0405, + 0.0415, + 0.0425, + 0.0435, + 0.0445, + 0.0455, + 0.0465, + 0.0475, + 0.0485, + 0.0495, + 0.0505, + 0.0515, + 0.0525, + 0.0535, + 0.0545, + 0.0555, + 0.0565, + 0.0575, + 0.0585, + 0.0595, + 0.0605, + 0.0615, + 0.0625, + 0.0635, + 0.0645, + 0.0655, + 0.0665, + 0.0675, + 0.0685, + 0.0695, + 0.0705, + 0.0715, + 0.0725, + 0.0735, + 0.0745, + 0.0755, + 0.0765, + 0.0775, + 0.0785, + 0.0795, + 0.0805, + 0.0815, + 0.0825, + 0.0835, + 0.0845, + 0.0855, + 0.0865, + 0.0875, + 0.0885, + 0.0895, + 0.0905, + 0.0915, + 0.0925, + 0.0935, + 0.0945, + 0.0955, + 0.0965, + 0.0975, + 0.0985, + 0.0995, + 0.1005, + 0.1015, + 0.1025, + 0.1035, + 0.1045, + 0.1055, + 0.1065, + 0.1075, + 0.1085, + 0.1095, + 0.1105, + 0.1115, + 0.1125, + 0.1135, + 0.1145, + 0.1155, + 0.1165, + 0.1175, + 0.1185, + 0.1195, + 0.1205, + 0.1215, + 0.1225, + 0.1235, + 0.1245, + 0.1255, + 0.1265, + 0.1275, + 0.1285, + 0.1295, + 0.1305, + 0.1315, + 0.1325, + 0.1335, + 0.1345, + 0.1355, + 0.1365, + 0.1375, + 0.1385, + 0.1395, + 0.1405, + 0.1415, + 0.1425, + 0.1435, + 0.1445, + 0.1455, + 0.1465, + 0.1475, + 0.1485, + 0.1495 + ] + }, + "label": "rarith.round6", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.seq.json b/tests/_r_cache/rarith.seq.json new file mode 100644 index 00000000..7ff1d4ee --- /dev/null +++ b/tests/_r_cache/rarith.seq.json @@ -0,0 +1,270 @@ +{ + "entries": { + "521efea698955d63b343252b1317bec43133ae7cd9a545562c796a0c5a5c18e7": [ + 0.0, + 0.01, + 0.02, + 0.03, + 0.04, + 0.05, + 0.06, + 0.07, + 0.08, + 0.09, + 0.1, + 0.11, + 0.12, + 0.13, + 0.14, + 0.15, + 0.16, + 0.17, + 0.18, + 0.19, + 0.2, + 0.21, + 0.22, + 0.23, + 0.24, + 0.25, + 0.26, + 0.27, + 0.28, + 0.29, + 0.3, + 0.31, + 0.32, + 0.33, + 0.34, + 0.35000000000000003, + 0.36, + 0.37, + 0.38, + 0.39, + 0.4, + 0.41000000000000003, + 0.42, + 0.43, + 0.44, + 0.45, + 0.46, + 0.47000000000000003, + 0.48, + 0.49, + 0.5, + 0.51, + 0.52, + 0.53, + 0.54, + 0.55, + 0.56, + 0.5700000000000001, + 0.58, + 0.59, + 0.6, + 0.61, + 0.62, + 0.63, + 0.64, + 0.65, + 0.66, + 0.67, + 0.68, + 0.6900000000000001, + 0.7000000000000001, + 0.71, + 0.72, + 0.73, + 0.74, + 0.75, + 0.76, + 0.77, + 0.78, + 0.79, + 0.8, + 0.81, + 0.8200000000000001, + 0.8300000000000001, + 0.84, + 0.85, + 0.86, + 0.87, + 0.88, + 0.89, + 0.9, + 0.91, + 0.92, + 0.93, + 0.9400000000000001, + 0.9500000000000001, + 0.96, + 0.97, + 0.98, + 0.99, + 1.0 + ], + "8cfbb078f3b3af441bd3acceb505b9c5a985f54f7ddb043e79fe2546dc4619b1": [ + 0.01, + 0.02, + 0.03, + 0.04, + 0.05, + 0.060000000000000005, + 0.06999999999999999, + 0.08, + 0.09, + 0.09999999999999999, + 0.11, + 0.12, + 0.13, + 0.14, + 0.15000000000000002, + 0.16, + 0.17, + 0.18000000000000002, + 0.19, + 0.2, + 0.21000000000000002, + 0.22, + 0.23, + 0.24000000000000002, + 0.25, + 0.26, + 0.27, + 0.28, + 0.29000000000000004, + 0.3, + 0.31, + 0.32, + 0.33, + 0.34, + 0.35000000000000003, + 0.36000000000000004, + 0.37, + 0.38, + 0.39, + 0.4, + 0.41000000000000003, + 0.42000000000000004, + 0.43, + 0.44, + 0.45, + 0.46, + 0.47000000000000003, + 0.48000000000000004, + 0.49, + 0.5, + 0.51, + 0.52, + 0.53, + 0.54, + 0.55, + 0.56, + 0.5700000000000001, + 0.5800000000000001, + 0.59, + 0.6, + 0.61, + 0.62, + 0.63, + 0.64, + 0.65, + 0.66, + 0.67, + 0.68, + 0.6900000000000001, + 0.7000000000000001, + 0.7100000000000001, + 0.72, + 0.73, + 0.74, + 0.75, + 0.76, + 0.77, + 0.78, + 0.79, + 0.8, + 0.81, + 0.8200000000000001, + 0.8300000000000001, + 0.8400000000000001, + 0.85, + 0.86, + 0.87, + 0.88, + 0.89, + 0.9, + 0.91, + 0.92, + 0.93, + 0.9400000000000001, + 0.9500000000000001, + 0.9600000000000001, + 0.97, + 0.98, + 0.99 + ], + "cc3a326d2df7319ff55274bcfdba49a3873958c51f7774cf08f3561088d7dc6e": [ + 0.05, + 0.1, + 0.15000000000000002, + 0.2, + 0.25, + 0.3, + 0.35000000000000003, + 0.4, + 0.45, + 0.5, + 0.55, + 0.6000000000000001, + 0.6500000000000001, + 0.7000000000000001, + 0.7500000000000001, + 0.8, + 0.8500000000000001, + 0.9000000000000001, + 0.95 + ], + "e74d2395255061214433047add46d9196942798a16c0c19ae92f9840a6ab951c": [ + -1.0, + -0.9, + -0.8, + -0.7, + -0.6, + -0.5, + -0.3999999999999999, + -0.29999999999999993, + -0.19999999999999996, + -0.09999999999999998, + 0.0, + 0.10000000000000009, + 0.20000000000000018, + 0.30000000000000004, + 0.40000000000000013, + 0.5, + 0.6000000000000001, + 0.7000000000000002, + 0.8, + 0.9000000000000001, + 1.0, + 1.1, + 1.2000000000000002, + 1.3000000000000003, + 1.4000000000000004, + 1.5, + 1.6, + 1.7000000000000002, + 1.8000000000000003, + 1.9000000000000004, + 2.0, + 2.1, + 2.2, + 2.3000000000000003, + 2.4000000000000004, + 2.5 + ] + }, + "label": "rarith.seq", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/rarith.summaries.json b/tests/_r_cache/rarith.summaries.json new file mode 100644 index 00000000..653069b2 --- /dev/null +++ b/tests/_r_cache/rarith.summaries.json @@ -0,0 +1,273 @@ +{ + "entries": { + "192c2c7ab43594a18332de862b11ce1f8dd3e797b4a86edb81a1a5a57b9be2e1": [ + 999986.9606368339, + 257001508.82032844, + 999986.9606368339, + 1000005.8708962196, + 1000098.8441126277, + 999997.8240541732, + 999952.8702706395, + 1000362.076959651, + 999777.1677085551, + 1000326.9660333532, + 1000323.6012495346, + 999083.8702606531, + 999213.8640485223, + 1000204.5501530401, + 1000053.4121415651, + 999949.5630797463, + 999468.6533858536, + 1000838.4649091763, + 999692.8076354304, + 1000083.3299642765, + 999917.603377651, + 1000681.018163088, + 1000294.3877366015, + 1000171.5198293978, + 999709.4050585319, + 1000390.6820651983, + 999950.3660465912, + 999929.1992432969, + 999561.0203719923, + 1000714.78456688, + 1000195.5365213662, + 999867.4812368234, + 1000388.7411537236, + 999439.5010561638, + 1000716.360438446, + 999280.9678818579, + 999564.8372589343, + 1000104.0696062034, + 1001182.007232749, + 1000429.401078674, + 999576.9391449121, + 1000318.9821420207, + 999874.5977197187, + 999878.3677980814, + 999644.3701320332, + 999654.6862020125, + 999839.2544181598, + 1000171.7283509639, + 1000316.1462492052, + 999777.2459861606, + 1000640.4585126565, + 999477.1392933243, + 998867.7558336277, + 1000658.8487651855, + 999711.4399467653, + 999559.9999753598, + 1000528.4824027069, + 1000610.4272419921, + 1000815.6693223926, + 1000120.4648346723, + 999838.1395601081, + 999568.397149517, + 1000221.0893415702, + 1000220.6295911742, + 1000242.7056756495, + 999565.8778527202, + 999728.3765669337, + 1000349.9026754334, + 999451.3573182095, + 1000305.2363662567, + 1000232.8067517827, + 1000613.4044290516, + 999618.9519270555, + 1000753.5283566952, + 1000355.6838542041, + 1000006.4421697852, + 999588.1298477532, + 999829.5519055446, + 1000849.2799132688, + 999847.4165867438, + 999814.5725020912, + 1000280.9154882741, + 1000201.7535961962, + 1000038.4465789786, + 999782.9070453652, + 999914.2028043718, + 1000444.9613287821, + 999829.1004025807, + 999983.0724226346, + 1000842.4114287832, + 999775.6634693478, + 999248.5846135437, + 1000257.9847669759, + 999795.5413420312, + 999863.6024531551, + 999384.113174438, + 1000269.3603603648, + 1000509.4094090151, + 999747.8299737198, + 1000399.2112278311, + 999956.3604091353, + 1000338.6834063247, + 1000066.3300360776, + 999429.0662621742, + 999697.3334933809, + 1000169.3021460896, + 999983.937352697, + 999545.2256189007, + 1000249.1838529436, + 1000215.363929503, + 1000004.2053577409, + 1000480.1905789659, + 999477.2173296568, + 999757.5144949429, + 1000322.8461846673, + 999269.7649026164, + 1000604.8860147842, + 1000400.5651615316, + 1000123.344478718, + 999466.4239981225, + 999066.2482146443, + 1000669.325464933, + 999451.3544308732, + 1000443.1616639703, + 999397.4228039066, + 999749.1081938233, + 1000127.4173225736, + 999978.3928190717, + 1000881.0688405767, + 1000681.6968685273, + 1000066.9140697839, + 999337.0620739815, + 1000301.4331332082, + 999867.8936928596, + 999953.3486693018, + 1000291.0617150991, + 999351.9657124594, + 1000740.6286404075, + 999025.5234973251, + 1000279.825060753, + 999661.5138226339, + 999962.1114428773, + 999371.9521195606, + 1000662.5399260586, + 999586.9525709478, + 1000476.7293194061, + 1000773.9674685627, + 1000239.5136290083, + 999895.418131434, + 999707.4734091839, + 1000743.50417871, + 1000104.2701609627, + 1000608.3833192427, + 1000430.3545378443, + 999493.1638204546, + 999498.6453406303, + 999756.0429261931, + 1000105.6902333193, + 999787.9305726433, + 1000090.2761236015, + 1000056.427400554, + 999867.1383049046, + 998815.9237164439, + 999742.5595938672, + 999832.7036293194, + 999507.0957059953, + 999603.7982619553, + 1001093.4724284135, + 998961.7498654773, + 1000048.6034458409, + 999610.9825854879, + 1000566.5697550745, + 1000145.6682378552, + 999998.6770373168, + 1000976.8434108916, + 1001001.280582704, + 1000135.8062749846, + 999030.5772244096, + 999796.8949002359, + 1000658.3179154233, + 1000179.1513032157, + 1000352.0924388387, + 1000559.482041638, + 999276.1551535641, + 1000262.4175277825, + 999545.8405243746, + 1001230.6442478745, + 1000661.6984898554, + 999906.4832412335, + 1000112.2915068328, + 999484.3535367892, + 999200.1387333955, + 999085.040278123, + 1000354.7361542891, + 999788.2956656548, + 1001006.4161750786, + 999738.518440162, + 1000784.5247022121, + 1000640.5192633288, + 999877.8540586829, + 999670.5968476268, + 1000328.0873590566, + 999830.6230228984, + 999390.8382473099, + 998955.3965934305, + 999508.9006961975, + 1000433.6980270142, + 999974.3066803921, + 999028.6621082311, + 1000014.3618631338, + 999851.0394828011, + 1000125.3544072674, + 1000433.2777448343, + 1000575.6471107111, + 999708.4788277403, + 999768.9855670802, + 1000010.5738907801, + 999675.7546129982, + 1000173.6548279693, + 999536.4499601116, + 1000726.5972533764, + 1000411.408033978, + 999955.8408453002, + 1000896.7422192896, + 999670.763304474, + 1000309.8727721645, + 999853.5516259723, + 999948.6143655787, + 999549.8800005068, + 999863.0852378138, + 1000256.5366548803, + 999930.5359902858, + 999597.7537515421, + 1001254.4821060139, + 1000470.9100951484, + 999978.0561397496, + 999633.9868401745, + 999617.8067112939, + 1000261.8278522021, + 999507.4033621796, + 999428.1255919469, + 1000235.6299058218, + 1000640.7293745475, + 1000488.2917613796, + 999638.9574439421, + 999470.1010619494, + 999621.6329153271, + 999786.0763430949, + 998964.2723408989, + 999462.4093634275, + 1000100.5421033636, + 1000228.4661796975, + 999235.1330707371, + 1000238.4341701778, + 1000533.0360848774, + 999785.9634528633, + 1000233.0716615351, + 1000207.5572862022, + 1000946.3352556439, + 999997.0561334698, + 999251.2384076373, + 1000185.1297125316, + 999754.7545293109, + 999900.9806296801 + ] + }, + "label": "rarith.summaries", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/reg14.dimred.class.meta.json b/tests/_r_cache/reg14.dimred.class.meta.json new file mode 100644 index 00000000..42721bca --- /dev/null +++ b/tests/_r_cache/reg14.dimred.class.meta.json @@ -0,0 +1,72 @@ +{ + "entries": { + "d7ede3d80274ae6c61d93c84fe6680b8c06ed11b2184cab1fea80d2904f36d3c": { + "ids": [ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 11.0, + 12.0, + 13.0, + 14.0, + 15.0, + 16.0, + 17.0, + 18.0, + 19.0, + 20.0, + 21.0, + 22.0, + 23.0, + 24.0, + 25.0, + 26.0, + 27.0, + 28.0, + 29.0, + 30.0, + 31.0, + 32.0, + 33.0, + 34.0, + 35.0, + 36.0, + 37.0, + 38.0, + 39.0, + 40.0, + 41.0, + 42.0, + 43.0, + 44.0, + 45.0, + 46.0, + 47.0, + 48.0, + 49.0, + 50.0, + 51.0, + 52.0, + 53.0, + 54.0, + 55.0, + 56.0, + 57.0, + 58.0, + 59.0, + 60.0 + ], + "nbest": NaN + } + }, + "label": "reg14.dimred.class.meta", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/reg14.mvcall.class.json b/tests/_r_cache/reg14.mvcall.class.json new file mode 100644 index 00000000..4f437c0e --- /dev/null +++ b/tests/_r_cache/reg14.mvcall.class.json @@ -0,0 +1,41 @@ +{ + "entries": { + "843bf7f60ae44c000ff3195be4509451217a19c375d1e3468c4c73fe4a6aa246": { + "names": [ + "R2", + "SE", + "Prediction.Accuracy", + "equation", + "x.star", + "derivative", + "Point.est", + "pred.int", + "Prediction.set", + "regression.points", + "RPM", + "RPM.probabilities", + "rhs.partitions", + "Fitted.xy", + "Fitted", + "Fitted.probabilities", + "Point.probabilities", + "n.best", + "class.levels", + "class.values" + ], + "point": [ + 0.0, + 1.0, + 1.0 + ], + "probs": [ + 0.329690162411432, + 0.621997810699811, + 0.82051282051282 + ] + } + }, + "label": "reg14.mvcall.class", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/reg14.nbest.na.json b/tests/_r_cache/reg14.nbest.na.json new file mode 100644 index 00000000..6be66643 --- /dev/null +++ b/tests/_r_cache/reg14.nbest.na.json @@ -0,0 +1,14 @@ +{ + "entries": { + "9811d3e902aa6ea1a95c853758348e9e6689fcd1a48683b156bc0a2b62448c81": [ + 0.577551277806567, + 1.1418977503081, + 0.735057305677998, + 1.28244770139491, + 0.191555209244013 + ] + }, + "label": "reg14.nbest.na", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/reg14.threshold.class.json b/tests/_r_cache/reg14.threshold.class.json new file mode 100644 index 00000000..b1c6d115 --- /dev/null +++ b/tests/_r_cache/reg14.threshold.class.json @@ -0,0 +1,28 @@ +{ + "entries": { + "16c48dd483528e7cd13a9beac9b54a9d775dba3ded85e1c21e9b66e21ccc1fb1": { + "point": [ + 0.0, + 1.0, + 1.0, + 0.0, + 0.0 + ], + "probs": [ + 1.0, + 0.217757766074835, + 0.303598198335444, + 0.669096347993739, + 1.0, + 0.0, + 0.782242233925165, + 0.696401801664556, + 0.330903652006261, + 0.0 + ] + } + }, + "label": "reg14.threshold.class", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/reg14.threshold.na.json b/tests/_r_cache/reg14.threshold.na.json new file mode 100644 index 00000000..69a54b90 --- /dev/null +++ b/tests/_r_cache/reg14.threshold.na.json @@ -0,0 +1,35 @@ +{ + "entries": { + "63318f327f1ff8040c2d8a64c79ba952aa66a6965dde5afe8cc273e0ba1eb104": [ + 1.0, + 1.0, + 0.0, + 1.0, + 0.0 + ], + "c2eddecab2e0e43bbc9b3b92168e210b7f1d387249c875cd582715fa450b19d8": [ + 1.0, + 1.0, + 0.0, + 1.0, + 0.0 + ], + "cce0d695c31d235b78592517e27f0cbb29b901d24d0a95ca09df48f5a8320d6b": [ + 0.942242974871132, + 2.94749350761631, + 0.709841131249864, + 2.12062050909837, + 1.18347914211893 + ], + "e0ea03b5f86aec3c2d13822147df2c0ce5c08eb6f9287ad72f3aacdabcc01b51": [ + 0.942242974871132, + 2.94749350761631, + 0.709841131249864, + 2.12062050909837, + 1.18347914211893 + ] + }, + "label": "reg14.threshold.na", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/parity/test_r_arithmetic.py b/tests/parity/test_r_arithmetic.py new file mode 100644 index 00000000..f40a1dea --- /dev/null +++ b/tests/parity/test_r_arithmetic.py @@ -0,0 +1,285 @@ +"""Bit-for-bit agreement with the R arithmetic NNS results depend on. + +NNS feeds several intermediate quantities into exact comparisons: the +dimension-reduction index is cut by ``findInterval`` at regression points built +from the same index, class probabilities are compared for ties, VAR rounds its +correlation cutoffs to two digits and Stack rounds a random holdout share to +three. A last-bit difference in any of them can therefore change a fitted +partition, a gradient or a selected model, so these checks compare exact +doubles (``nns_frame_call(..., exact=True)`` stores 17 significant digits). + +The R values come from the pinned oracle. Matrix products follow R's bundled +reference BLAS, which is what R uses unless it is relinked to another BLAS. +""" + +from __future__ import annotations + +from typing import Any + +import numpy as np +import pytest +from _r import nns_frame_call, r_column + +import nns +from nns._dep_r14 import dep_pair_r14 +from nns._rblas import r_crossprod_vec, r_matvec +from nns._reg_engine import _dimred_coefficients +from nns._rmean import ( + r_col_means, + r_cor, + r_cor_matrix, + r_mean, + r_quantile, + r_rank_average, + r_round, + r_round_array, + r_row_means, + r_seq_by, + r_sum, +) +from nns._rnmath import dhyper, pnorm + +pytestmark = pytest.mark.parity + + +def _num(values: Any) -> dict[str, Any]: + return r_column([float(v) for v in np.asarray(values, dtype=np.float64).ravel()], "numeric") + + +def _exact(label: str, expr: str, **data: Any) -> Any: + return nns_frame_call(label, expr, {k: _num(v) for k, v in data.items()}, exact=True) + + +def _bits(actual: Any, expected: Any) -> None: + actual_arr = np.asarray(actual, dtype=np.float64) + expected_arr = np.asarray(expected, dtype=np.float64).reshape(actual_arr.shape) + np.testing.assert_array_equal(actual_arr, expected_arr) + + +def _hyper_grid() -> np.ndarray: + rng = np.random.default_rng(20261108) + rows = [] + for m in [*range(2, 41), 57, 100, 233, 400]: + for _ in range(6): + a, b = int(rng.integers(0, m + 1)), int(rng.integers(0, m + 1)) + lo, hi = max(0, a + b - m), min(a, b) + for k in sorted({lo, (lo + hi) // 2, hi}): + rows.append((k, a, m - a, b)) + return np.asarray(sorted(set(rows)), dtype=np.float64) + + +def test_dhyper_matches_r_bitwise() -> None: + grid = _hyper_grid() + expected = _exact( + "rarith.dhyper", + "dhyper(k, r, b, n)", + k=grid[:, 0], + r=grid[:, 1], + b=grid[:, 2], + n=grid[:, 3], + ) + _bits([dhyper(*row) for row in grid], expected) + + +def test_pnorm_matches_r_bitwise() -> None: + rng = np.random.default_rng(7) + z = np.concatenate( + [ + rng.normal(size=400) * 3, + rng.uniform(-40.0, 10.0, size=200), + [0.0, 0.67448975, -0.67448975, 5.656854249492381, -5.656854249492381], + [8.2924, -38.4674, 1e-17, -1e-17, 37.0, -37.9], + ] + ) + expected = _exact("rarith.pnorm", "pnorm(z)", z=z) + _bits([pnorm(float(v)) for v in z], expected) + + +@pytest.mark.parametrize("digits", [1, 2, 3, 6, 12, -1]) +def test_round_matches_r_bitwise(digits: int) -> None: + rng = np.random.default_rng(digits + 100) + x = np.concatenate( + [ + rng.uniform(-2.0, 2.0, 600), + np.round(rng.uniform(-2.0, 2.0, 300), 3) + 0.005, + rng.uniform(0.20, 1.0 / 3.0, 300), + np.arange(-150, 150) / 1000 + 0.0005, + ] + ) + expected = _exact(f"rarith.round{digits}", f"round(x, {digits})", x=x) + _bits(r_round_array(x, digits), expected) + _bits([r_round(float(v), digits) for v in x], expected) + + +@pytest.mark.parametrize( + ("start", "stop", "by"), + [(0.01, 0.99, 0.01), (0.0, 1.0, 0.01), (0.05, 0.95, 0.05), (-1.0, 2.5, 0.1)], +) +def test_seq_by_matches_r_bitwise(start: float, stop: float, by: float) -> None: + expected = _exact("rarith.seq", f"seq({start!r}, {stop!r}, by = {by!r})") + _bits(r_seq_by(start, stop, by), expected) + + +@pytest.mark.parametrize("type_", [7, 8]) +def test_quantile_matches_r_bitwise(type_: int) -> None: + rng = np.random.default_rng(type_) + probs = [0.0, 0.01, 0.025, 0.05, 0.1, 0.25, 0.333, 0.5, 0.77, 0.95, 0.975, 1.0] + for n in (1, 2, 3, 7, 50, 401): + x = rng.normal(size=n) * 10 ** rng.uniform(-3, 3) + expected = _exact( + f"rarith.quantile{type_}", f"quantile(x, p, type = {type_}, names = FALSE)", x=x, p=probs + ) + _bits(r_quantile(x, probs, type_), expected) + + +def test_summaries_match_r_bitwise() -> None: + rng = np.random.default_rng(11) + x = rng.normal(size=(257, 5)) * 1e3 + 1e6 + expected = _exact( + "rarith.summaries", + "X <- matrix(v, ncol = 5); c(mean(X[, 1]), sum(X[, 2]), colMeans(X), rowMeans(X))", + v=x.ravel(order="F"), + ) + actual = np.concatenate( + [[r_mean(x[:, 0]), r_sum(x[:, 1])], r_col_means(x), r_row_means(x)] + ) + _bits(actual, expected) + + +def test_correlations_match_r_bitwise() -> None: + rng = np.random.default_rng(12) + x = rng.normal(size=(83, 4)) + x[:, 1] = x[:, 0] ** 2 + rng.normal(size=83) * 0.1 + x[:, 3] = np.round(x[:, 3]) + expected = _exact( + "rarith.cor", + "X <- matrix(v, ncol = 4); c(cor(X[, 1], X[, 2]), " + "cor(X[, 3], X[, 4], method = 'spearman'), as.numeric(cor(X)), " + "as.numeric(cor(X, method = 'spearman')))", + v=x.ravel(order="F"), + ) + ranks = np.column_stack([r_rank_average(x[:, j]) for j in range(4)]) + actual = np.concatenate( + [ + [r_cor(x[:, 0], x[:, 1])], + [r_cor(r_rank_average(x[:, 2]), r_rank_average(x[:, 3]))], + r_cor_matrix(x).ravel(order="F"), + r_cor_matrix(ranks).ravel(order="F"), + ] + ) + _bits(actual, expected) + + +def test_reference_blas_products_match_r_bitwise() -> None: + rng = np.random.default_rng(13) + design = rng.uniform(size=(61, 4)) + coef = np.array([0.31, -1.7, 0.0, 2.2]) + weights = rng.uniform(size=9) + shares = rng.uniform(size=(9, 3)) + expected = _exact( + "rarith.blas", + "D <- matrix(d, ncol = 4); S <- matrix(s, ncol = 3); " + "c(as.numeric(D %*% cf), as.numeric(crossprod(w, S)))", + d=design.ravel(order="F"), + cf=coef, + w=weights, + s=shares.ravel(order="F"), + ) + _bits(np.concatenate([r_matvec(design, coef), r_crossprod_vec(weights, shares)]), expected) + + +def _dependence_pairs() -> list[tuple[np.ndarray, np.ndarray]]: + rng = np.random.default_rng(14) + pairs = [] + for i, n in enumerate([8, 25, 60, 60, 150, 401, 900]): + x = rng.normal(size=n) + if i % 3 == 0: + y = x**2 + rng.normal(size=n) * 0.2 + elif i % 3 == 1: + x = np.round(x) + y = np.round(rng.normal(size=n)) + else: + y = (x > 0).astype(float) + rng.normal(size=n) * 0.3 + pairs.append((x, y)) + return pairs + + +@pytest.mark.parametrize("asym", [False, True]) +def test_nns_dep_matches_r_bitwise(asym: bool) -> None: + flag = "TRUE" if asym else "FALSE" + for x, y in _dependence_pairs(): + expected = _exact("rarith.dep", f"unname(unlist(NNS.dep(x, y, asym = {flag})))", x=x, y=y) + public = nns.nns_dep(x, y, asym=asym) + _bits([public["Correlation"], public["Dependence"]], expected) + # The independent pure-Python fallback is held to the same standard. + _bits(dep_pair_r14(x, y, asym), expected) + + +@pytest.mark.parametrize("method", ["cor", "NNS.dep", "NNS.caus", "all"]) +def test_dimension_reduction_weights_match_r_bitwise(method: str) -> None: + rng = np.random.default_rng(15) + for n, ties in ((40, False), (150, True)): + x = rng.normal(size=(n, 3)) + if ties: + x = np.round(x) + y = x[:, 0] + x[:, 1] ** 2 + rng.normal(size=n) * 0.3 + expected = _exact( + "rarith.dimred", + f"NNS:::.nns_reg_dimred_coefficients(matrix(v, ncol = 3), y, '{method}', NULL, 0)", + v=x.ravel(order="F"), + y=y, + ) + _bits(_dimred_coefficients(x, y, method, None, 0), expected) + + +def test_dimension_reduction_fit_ids_match_r_on_boundary_rows() -> None: + """An index value equal to a regression point keeps R's cell and gradient. + + Before the reference-order index and R's dependence weights, Python's index + differed from R's in the last bit, so rows sitting on regression points fell + into the neighbouring cell (different ``NNS.ID`` and ``gradient``). + """ + rng = np.random.default_rng(12) + x = rng.normal(size=(150, 3)) + x[:, 1] = (x[:, 1] > 0).astype(float) + y = x[:, 0] + 2 * x[:, 1] + rng.normal(size=150) * 0.2 + expected = _exact( + "rarith.dimred.fit", + "f <- NNS.reg(matrix(v, ncol = 3), y, dim.red.method = 'NNS.dep', threshold = 0.1, " + "plot = FALSE)$Fitted.xy; c(f$x, f$NNS.ID, f$gradient, f$y.hat)", + v=x.ravel(order="F"), + y=y, + ) + fit = nns.nns_reg(x, y, dim_red_method="NNS.dep", threshold=0.1)["Fitted.xy"] + actual = np.concatenate([fit["x"], fit["NNS.ID"], fit["gradient"], fit["y.hat"]]) + _bits(actual, expected) + + +@pytest.mark.parametrize("linear", [False, True]) +def test_nns_norm_matches_r_bitwise(linear: bool) -> None: + rng = np.random.default_rng(16) + x = np.abs(rng.normal(size=(30, 3))) * np.array([1.0, 25.0, 400.0]) + 0.5 + flag = "TRUE" if linear else "FALSE" + expected = _exact( + "rarith.norm", + f"as.numeric(NNS.norm(matrix(v, ncol = 3), linear = {flag}))", + v=x.ravel(order="F"), + ) + _bits(nns.nns_norm(x, linear=linear).ravel(order="F"), expected) + + +def test_nns_norm_constant_column_is_na_as_in_r() -> None: + """A constant column has zero spread for R's cor(), so NNS.norm is all NA. + + NumPy's one-pass mean of a repeated 0.1 is one ulp off, which made the + former np.corrcoef path report a tiny spread and finite output. + """ + x = np.column_stack([np.full(8, 0.1), np.linspace(1.0, 2.0, 8)]) + expected = _exact( + "rarith.norm.constant", + "as.numeric(suppressWarnings(NNS.norm(matrix(v, ncol = 2))))", + v=x.ravel(order="F"), + ) + # An all-NA numeric vector decodes as the strings "NA". + assert all(value in ("NA", None) or np.isnan(value) for value in expected) + assert np.all(np.isnan(nns.nns_norm(x))) diff --git a/tests/parity/test_reg_r14_contracts.py b/tests/parity/test_reg_r14_contracts.py new file mode 100644 index 00000000..f4cc7c0b --- /dev/null +++ b/tests/parity/test_reg_r14_contracts.py @@ -0,0 +1,115 @@ +"""NNS.reg argument and dispatch contracts of R 14.0. + +These follow R/Regression.R and R/Classification.R where the port used to +differ: a class response is classified even with ``multivariate.call = TRUE``; +``threshold = NULL``/``NA`` means no screening and ``n.best = NA`` the default, +so values reported by ``NNS.stack`` can be passed back; the dimension-reduction +classifier reports ``n.best = NA`` and integer row ids. +""" + +from __future__ import annotations + +from typing import Any + +import numpy as np +import pytest +from _r import nns_frame_call, r_column +from _tolerances import COMPOUND + +import nns + +pytestmark = pytest.mark.parity + + +def _num(values: Any) -> dict[str, Any]: + return r_column([float(v) for v in np.asarray(values, dtype=np.float64).ravel()], "numeric") + + +def _design(seed: int, n: int = 60) -> tuple[np.ndarray, np.ndarray, np.ndarray]: + rng = np.random.default_rng(seed) + x = rng.normal(size=(n, 3)) + y = x[:, 0] + x[:, 1] ** 2 + rng.normal(size=n) * 0.3 + classes = (x[:, 0] + x[:, 2] > 0).astype(float) + return x, y, classes + + +def test_class_response_with_multivariate_call_is_classified_as_in_r() -> None: + rng = np.random.default_rng(3) + x = rng.normal(size=80) + y = (x + rng.normal(size=80) * 0.5 > 0).astype(float) + point = np.array([-0.5, 0.25, 1.5]) + expected = nns_frame_call( + "reg14.mvcall.class", + "r <- NNS.reg(x, y, point.est = p, multivariate.call = TRUE, plot = FALSE); " + "list(names = names(r), point = r$Point.est, probs = r$Point.probabilities[, 2])", + {"x": _num(x), "y": _num(y), "p": _num(point)}, + ) + actual = nns.nns_reg(x, y, point_est=point, multivariate_call=True) + assert list(actual) == list(expected["names"]) + np.testing.assert_array_equal(actual["Point.est"], expected["point"]) + np.testing.assert_allclose( + actual["Point.probabilities"]["1"], expected["probs"], rtol=0, atol=COMPOUND + ) + + +@pytest.mark.parametrize("threshold", [None, float("nan")]) +@pytest.mark.parametrize("response", ["numeric", "class"]) +def test_threshold_null_or_na_means_no_screening(threshold: float | None, response: str) -> None: + x, y, classes = _design(4) + target = y if response == "numeric" else classes + arg = "NULL" if threshold is None else "NA" + expected = nns_frame_call( + "reg14.threshold.na", + f"NNS.reg(matrix(v, ncol = 3), y, dim.red.method = 'cor', point.est = matrix(p, ncol = 3), " + f"threshold = {arg}, plot = FALSE)$Point.est", + {"v": _num(x.ravel(order="F")), "y": _num(target), "p": _num(x[:5].ravel(order="F"))}, + ) + actual = nns.nns_reg(x, target, dim_red_method="cor", point_est=x[:5], threshold=threshold) + np.testing.assert_allclose(actual["Point.est"], np.asarray(expected), rtol=0, atol=COMPOUND) + + +def test_n_best_na_selects_the_default_as_in_r() -> None: + x, y, _ = _design(5) + expected = nns_frame_call( + "reg14.nbest.na", + "NNS.reg(matrix(v, ncol = 3), y, point.est = matrix(p, ncol = 3), n.best = NA, " + "plot = FALSE)$Point.est", + {"v": _num(x.ravel(order="F")), "y": _num(y), "p": _num(x[:5].ravel(order="F"))}, + ) + actual = nns.nns_reg(x, y, point_est=x[:5], n_best=float("nan")) + np.testing.assert_allclose(actual["Point.est"], np.asarray(expected), rtol=0, atol=COMPOUND) + + +def test_per_class_thresholds_follow_r() -> None: + x, _, classes = _design(6) + expected = nns_frame_call( + "reg14.threshold.class", + "r <- NNS.reg(matrix(v, ncol = 3), y, dim.red.method = 'cor', " + "point.est = matrix(p, ncol = 3), threshold = c(0.05, 0.3), plot = FALSE); " + "list(point = r$Point.est, probs = as.numeric(r$Point.probabilities))", + {"v": _num(x.ravel(order="F")), "y": _num(classes), "p": _num(x[:5].ravel(order="F"))}, + ) + actual = nns.nns_reg(x, classes, dim_red_method="cor", point_est=x[:5], threshold=[0.05, 0.3]) + np.testing.assert_array_equal(actual["Point.est"], expected["point"]) + probs = np.column_stack([actual["Point.probabilities"][k] for k in ("0", "1")]) + np.testing.assert_allclose(probs.ravel(order="F"), expected["probs"], rtol=0, atol=COMPOUND) + + +def test_vector_threshold_is_rejected_for_regression_as_in_r() -> None: + x, y, _ = _design(7) + with pytest.raises(ValueError, match="single number for regression"): + nns.nns_reg(x, y, dim_red_method="cor", threshold=[0.1, 0.2]) + + +def test_dimension_reduction_classifier_reports_na_n_best_and_row_ids() -> None: + x, _, classes = _design(8) + expected = nns_frame_call( + "reg14.dimred.class.meta", + "r <- NNS.reg(matrix(v, ncol = 3), y, dim.red.method = 'cor', plot = FALSE); " + "list(nbest = r$n.best, ids = r$Fitted.xy$NNS.ID)", + {"v": _num(x.ravel(order="F")), "y": _num(classes)}, + ) + actual = nns.nns_reg(x, classes, dim_red_method="cor") + assert np.isnan(np.asarray(expected["nbest"], dtype=np.float64)).all() + assert np.isnan(actual["n.best"]) + np.testing.assert_array_equal(actual["Fitted.xy"]["NNS.ID"], expected["ids"]) diff --git a/tests/property/test_norm.py b/tests/property/test_norm.py index f4f99ab7..6d35ae06 100644 --- a/tests/property/test_norm.py +++ b/tests/property/test_norm.py @@ -25,7 +25,11 @@ def test_nns_norm_shape_and_finiteness_hold_for_random_matrices( x: np.ndarray, linear: bool, ) -> None: - assume(np.all(np.std(x, axis=0) > 0.0)) + # Every column must actually vary. np.std is not a test of that: its + # pairwise mean of a repeated 0.1 is one ulp off, so a constant column has a + # tiny nonzero std, while R's cor() (two-pass long-double mean) sees zero + # spread and NNS.norm returns NA for the whole matrix, as the port does. + assume(bool(np.all(np.ptp(x, axis=0) > 0.0))) result = nns_norm(x, linear=linear) From e47a27888986c3d1aa92b791761ae299794a6e1f Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 14:53:57 +0000 Subject: [PATCH 02/16] Match R in Stack/Boost model selection and nominal cell assignment Live comparison against R 14.0 found three divergences in NNS.stack and NNS.boost: - Regression stacks choose the blend weight on seq(0, 1, by = 0.01) by the out-of-fold objective and take the middle of exactly tied weights. When both components fit the same coordinate the scores differ only by rounding, so the choice depends on R's arithmetic. The default objectives are now R's sum((predicted - actual)^2) and mean(predicted == actual) (long-double sum and count), and the grid and blends use R's seq() values. Previously the reported weights could differ (e.g. 0.26 vs R's 0.47). - Nominal cells used findInterval(..., rightmost.closed = TRUE) with every value above the last breakpoint clamped into the final interval; R moves only a value equal to that breakpoint. Breakpoints are cell means, inside the data range, so the observations above the last one lost their own cell and balanced class stacks could predict a different class. Cell centres, class shares and priors also use R's colMeans. - NNS.boost passes type = type to its learners: with a numeric 0/1 response the boost is a regression but each base NNS.reg classifies. The port forced type = "REG", so learner scores, survivors and feature weights differed. Also: the regression dependence heuristic averages with R's mean(), as does the default neighbour count and VAR's trailing-gap index fit. Tests: R-authored cases for blend-weight ties, balanced class-stack cells and binary boost learners; 9 of the 10 fail on the previous code. An invariant that compared a numpy-sum objective with the default bit for bit now uses the default's arithmetic. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt --- src/nns/_class_r14.py | 18 +- src/nns/_reg_engine.py | 7 +- src/nns/_rmean.py | 20 ++ src/nns/boost.py | 9 +- src/nns/stack.py | 6 +- src/nns/var.py | 4 +- tests/_r_cache/boost14.binary.numeric.json | 47 +++ tests/_r_cache/stack14.blend.json | 83 +++++ tests/_r_cache/stack14.class.balanced.json | 307 ++++++++++++++++++ tests/invariants/test_stack.py | 7 +- tests/parity/test_r_arithmetic.py | 5 +- .../parity/test_stack_boost_r14_contracts.py | 113 +++++++ 12 files changed, 605 insertions(+), 21 deletions(-) create mode 100644 tests/_r_cache/boost14.binary.numeric.json create mode 100644 tests/_r_cache/stack14.blend.json create mode 100644 tests/_r_cache/stack14.class.balanced.json create mode 100644 tests/parity/test_stack_boost_r14_contracts.py diff --git a/src/nns/_class_r14.py b/src/nns/_class_r14.py index ecb47f4a..20d20f5c 100644 --- a/src/nns/_class_r14.py +++ b/src/nns/_class_r14.py @@ -14,7 +14,7 @@ from numpy.typing import NDArray from nns._rblas import r_crossprod_vec -from nns._rmean import r_approx, r_tie_means +from nns._rmean import r_approx, r_col_means, r_tie_means F = NDArray[np.float64] @@ -54,6 +54,7 @@ def _class_base( from nns._reg_engine import ( _build_points, _dependence, + _find_interval, _mreg_distances, _mreg_ensemble_weights, ) @@ -95,10 +96,10 @@ def _class_base( if coord_exact: memberships.append(np.searchsorted(b, coord, side="left").astype(np.int64) + 1) else: - positions = np.searchsorted(b, coord, side="right").astype(np.int64) - # R findInterval(..., rightmost.closed=TRUE) maps the last - # breakpoint to the final interval, not to one past it. - memberships.append(np.minimum(positions, b.size - 1)) + # R findInterval(..., rightmost.closed = TRUE): only a value equal + # to the last breakpoint joins the final interval; larger values + # (the boundaries are cell means, inside the range) get their own. + memberships.append(_find_interval(coord, b)) row_ids = [".".join(str(int(membership[i])) for membership in memberships) for i in range(n)] groups: dict[str, list[int]] = {} @@ -107,9 +108,10 @@ def _class_base( # split(seq_len(n), ids) groups names in lexicographic order in R. keys = sorted(groups) - centres = np.array([np.mean(design_x[groups[key]], axis=0) for key in keys], dtype=np.float64) - shares = np.array([np.mean(indicators[groups[key]], axis=0) for key in keys], dtype=np.float64) - prior = np.mean(indicators, axis=0) + # R: colMeans(X[g, ]) and colMeans(probabilities[g, ]) per cell, colMeans(probabilities). + centres = np.array([r_col_means(design_x[groups[key]]) for key in keys], dtype=np.float64) + shares = np.array([r_col_means(indicators[groups[key]]) for key in keys], dtype=np.float64) + prior = r_col_means(indicators) if n_best == "all": k = centres.shape[0] diff --git a/src/nns/_reg_engine.py b/src/nns/_reg_engine.py index f48f9a47..fd5caaa9 100644 --- a/src/nns/_reg_engine.py +++ b/src/nns/_reg_engine.py @@ -556,11 +556,12 @@ def _dependence(x: NDArray[np.float64], y: NDArray[np.float64]) -> float: # sign convention for the response. d2_forward = float(_copula(m, _r_col_means(m), continuous=True)) d2_reverse = float(_copula(reflected, _r_col_means(reflected), continuous=True)) - d2 = 0.5 * (d2_forward + d2_reverse) + d2 = r_mean(np.array([d2_forward, d2_reverse])) # R: mean(c(...)) except Exception: d2 = float("nan") - parts = [v for v in (d1, d2) if math.isfinite(v)] - d = sum(parts) / len(parts) if parts else float("nan") + # R: mean(c(d1, d2), na.rm = TRUE). + parts = [v for v in (d1, d2) if not math.isnan(v)] + d = r_mean(np.array(parts)) if parts else float("nan") if not math.isfinite(d): d = 0.1 return min(1.0, max(0.0, d)) diff --git a/src/nns/_rmean.py b/src/nns/_rmean.py index 1bbe16e6..0cb430c2 100644 --- a/src/nns/_rmean.py +++ b/src/nns/_rmean.py @@ -397,3 +397,23 @@ def r_tie_means( groups = np.searchsorted(ux, xs) means = np.array([r_mean(ys[groups == g]) for g in range(ux.size)]) return ux, means + + +def r_mean_logical(values: NDArray[np.bool_]) -> float: + """R ``mean()`` of a logical vector: a long-double count / n (summary.c + integer path, no refinement pass).""" + v = np.asarray(values, dtype=bool).ravel() + if v.size == 0: + return float("nan") + return float(np.longdouble(int(np.count_nonzero(v))) / np.longdouble(v.size)) + + +def r_sse(predicted: NDArray[np.float64], actual: NDArray[np.float64]) -> float: + """NNS.stack / NNS.boost default objective ``sum((predicted - actual)^2)``.""" + diff = np.asarray(predicted, dtype=np.float64) - np.asarray(actual, dtype=np.float64) + return r_sum(diff * diff) + + +def r_accuracy(predicted: NDArray[np.float64], actual: NDArray[np.float64]) -> float: + """NNS.stack / NNS.boost default classification objective ``mean(predicted == actual)``.""" + return r_mean_logical(np.asarray(predicted) == np.asarray(actual)) diff --git a/src/nns/boost.py b/src/nns/boost.py index 78d3968f..15ed369e 100644 --- a/src/nns/boost.py +++ b/src/nns/boost.py @@ -28,6 +28,7 @@ from nns._indices import ordered_complement from nns._native import external_stacklevel, honours_ncores from nns._reg_engine import _validate_dist, nns_reg_engine +from nns._rmean import r_accuracy, r_sse from nns._rrng import RRNG from nns.central_tendencies import nns_gravity from nns.stack import ( @@ -190,14 +191,14 @@ def nns_boost( if np.unique(y).size < 2: raise ValueError("Classification requires at least two response classes.") if obj_fn is None: - obj_fn = lambda predicted, actual: float(np.mean(predicted == actual)) # noqa: E731 + obj_fn = r_accuracy # R: mean(predicted == actual) objective_value = "max" else: if not response_was_numeric: raise ValueError("A nonnumeric response requires type = 'CLASS'.") y = np.asarray(dv, dtype=np.float64) if obj_fn is None: - obj_fn = lambda predicted, actual: float(np.sum((predicted - actual) ** 2)) # noqa: E731 + obj_fn = r_sse # R: sum((predicted - actual)^2), with R's sum() n_obs = x.shape[0] n_features = x.shape[1] @@ -313,7 +314,9 @@ def fit_subset( point_est=test_x, dim_red_method=None, order=depth_value, - type="CLASS" if is_class else "REG", + # R passes type = type: NULL unless classification, so a numeric + # 0/1 response is classified by the base learner as in NNS.reg. + type="CLASS" if is_class else None, point_only=True, dist=dist_value, factor_levels=[code_levels[j] for j in cols], diff --git a/src/nns/stack.py b/src/nns/stack.py index 1137baf1..e887ad69 100644 --- a/src/nns/stack.py +++ b/src/nns/stack.py @@ -57,7 +57,7 @@ _validate_order, nns_reg_engine, ) -from nns._rmean import r_cor, r_rank_average, r_round, r_row_means, r_seq_by +from nns._rmean import r_accuracy, r_cor, r_rank_average, r_round, r_row_means, r_seq_by, r_sse from nns._rrng import RRNG from nns.central_tendencies import nns_gravity from nns.dependence import nns_dep @@ -285,14 +285,14 @@ def nns_stack( if np.unique(y).size < 2: raise ValueError("Classification requires at least two response classes.") if obj_fn is None: - obj_fn = lambda predicted, actual: float(np.mean(predicted == actual)) # noqa: E731 + obj_fn = r_accuracy # R: mean(predicted == actual) objective_value = "max" else: if response_categorical: raise ValueError("A nonnumeric response requires type = 'CLASS'.") y = np.asarray(dv, dtype=np.float64) if obj_fn is None: - obj_fn = lambda predicted, actual: float(np.sum((predicted - actual) ** 2)) # noqa: E731 + obj_fn = r_sse # R: sum((predicted - actual)^2), with R's sum() if balance and not is_class: raise ValueError("[balance = TRUE] requires classification.") diff --git a/src/nns/var.py b/src/nns/var.py index 27dadb57..900dd484 100644 --- a/src/nns/var.py +++ b/src/nns/var.py @@ -17,7 +17,7 @@ from nns._helpers import _warn_unsupported from nns._native import honours_ncores -from nns._rmean import r_cor_matrix, r_quantile, r_rank_average, r_round_array +from nns._rmean import r_cor_matrix, r_mean, r_quantile, r_rank_average, r_round_array from nns.core import lpm_ratio, upm_ratio ObjectiveFn = Callable[[NDArray[np.float64], NDArray[np.float64]], float] @@ -459,7 +459,7 @@ def _var_index_fit( matrix = np.column_stack( (nns_rescale(index, 0, 1), nns_rescale(index, 0, 1), nns_rescale(values, 0, 1)) ) - dep = (dep + float(nns_copula(matrix))) / 2.0 + dep = r_mean(np.array([dep, float(nns_copula(matrix))])) # R: mean(c(dep, copula)) if not np.isfinite(dep): dep = 0.1 depth = math.floor(dep * 10) diff --git a/tests/_r_cache/boost14.binary.numeric.json b/tests/_r_cache/boost14.binary.numeric.json new file mode 100644 index 00000000..f1453d39 --- /dev/null +++ b/tests/_r_cache/boost14.binary.numeric.json @@ -0,0 +1,47 @@ +{ + "entries": { + "47b7e7475d35bba1eb7df0ea28772bc695dc13ad1a95d6adf204db757311ca1f": { + "frequency": [ + 91.0, + 72.0, + 37.0 + ], + "results": [ + 1.0, + 0.0, + 1.0, + 1.0, + 0.0, + 1.0 + ], + "weights": [ + 0.455, + 0.36, + 0.185 + ] + }, + "dbdd377c4dd9486636afe041a0636dedbe651c4384a2378ecbf68ed1f45c3bef": { + "frequency": [ + 117.0, + 117.0, + 61.0 + ], + "results": [ + 0.0, + 1.0, + 1.0, + 1.0, + 0.0, + 0.0 + ], + "weights": [ + 0.396610169491525, + 0.396610169491525, + 0.206779661016949 + ] + } + }, + "label": "boost14.binary.numeric", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/stack14.blend.json b/tests/_r_cache/stack14.blend.json new file mode 100644 index 00000000..b40e675c --- /dev/null +++ b/tests/_r_cache/stack14.blend.json @@ -0,0 +1,83 @@ +{ + "entries": { + "0014be8fb046c410d7b92d3cb3e117bce43d0254b56d2a0ed56453413b14fa59": { + "obj": [ + 5.831680348956521, + 5.831680348956519 + ], + "stack": [ + 2.425292492481696, + 1.0368684891967979, + 1.434263341109289, + 2.752037302870817, + 2.552018093541598, + 3.009366615398974, + 1.2744198376891118 + ], + "w": [ + 0.07, + 0.9299999999999999 + ] + }, + "8d29e4be4a363f5582ee39eb01f617b2dc4298cb83f247f15bd188606cc25132": { + "obj": [ + 9.867450240035154, + 9.867450240035154 + ], + "stack": [ + 1.1199542148647463, + 1.5125429174800615, + 2.6103811753871167, + 2.7286306330562864, + 2.754003778605536, + 1.22306422406244, + 1.0060460484730007 + ], + "w": [ + 0.47000000000000003, + 0.53 + ] + }, + "af089561a8104646d3764c68e3343bec115ba292276162f93e0216ee48f165e6": { + "obj": [ + 6.7701505438821705, + 6.77015054388217 + ], + "stack": [ + 1.1669626050968556, + 1.1816692669866018, + 2.8183931055076727, + 2.226548727326551, + 1.1213600046229102, + 1.0364753956718031, + 2.942885710198468 + ], + "w": [ + 0.28, + 0.72 + ] + }, + "b799c4eee495bfe5166ca458c5745e80d4a3f12782ac89937c40aefeeac7d074": { + "obj": [ + 9.006329194644676, + 6.775607055395414 + ], + "stack": [ + 2.866305558850671, + 1.3861962899953506, + 1.5989568358104358, + 1.3373800011656904, + 2.396653213495685, + 1.0251759762854242, + 2.0803948493106827 + ], + "w": [ + 0.16, + 0.84 + ] + } + }, + "label": "stack14.blend", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/stack14.class.balanced.json b/tests/_r_cache/stack14.class.balanced.json new file mode 100644 index 00000000..271630ad --- /dev/null +++ b/tests/_r_cache/stack14.class.balanced.json @@ -0,0 +1,307 @@ +{ + "entries": { + "155a98b73645c82f59b2c3287e6027f7e3a82ec6f4b0d034591076990ff3898d": { + "dim": [ + 3.0, + 3.0, + 2.0, + 1.0, + 3.0, + 1.0, + 3.0 + ], + "reg": [ + 3.0, + 3.0, + 2.0, + 1.0, + 3.0, + 1.0, + 3.0 + ], + "rp": [ + 0.254208716197204, + 0.403953934844578, + 0.0, + 0.542819097553147, + 0.387437037855131, + 0.5, + 0.0, + 0.0336697295776331, + 0.131960515581066, + 0.562212535268803, + 0.3, + 0.117803175304398, + 0.326459230895626, + 0.0, + 0.712121554225163, + 0.464085549574356, + 0.437787464731197, + 0.157180902446853, + 0.494759786840472, + 0.173540769104374, + 1.0 + ], + "sp": [ + 0.100638793124647, + 0.159921489274086, + 0.0, + 0.679596559484423, + 0.153382608137168, + 0.653375402631611, + 0.0, + 0.212056727697934, + 0.122667162052722, + 0.536328003941979, + 0.258177028451001, + 0.234403419665663, + 0.277921470969534, + 0.0, + 0.687304479177419, + 0.717411348673192, + 0.463671996058021, + 0.0622264120645761, + 0.612213972197169, + 0.0687031263988548, + 1.0 + ], + "stack": [ + 3.0, + 3.0, + 2.0, + 1.0, + 3.0, + 1.0, + 3.0 + ] + }, + "b39f56681fc131f5f379918856029a403eef613a9a4dc94d03579732ee6f0f49": { + "dim": [ + 1.0, + 1.0, + 1.0, + 1.0, + 3.0, + 2.0, + 1.0 + ], + "reg": [ + 1.0, + 1.0, + 3.0, + 2.0, + 2.0, + 2.0, + 3.0 + ], + "rp": [ + 0.571233298249463, + 0.481976647585764, + 0.271722068396086, + 0.366102320438759, + 0.344236765049991, + 0.415398624530214, + 0.338654098315255, + 0.0250345553459384, + 0.427236158249439, + 0.0, + 0.380338607736744, + 0.553348896112526, + 0.442600317416105, + 0.0, + 0.403732146404599, + 0.0907871941647967, + 0.728277931603914, + 0.253559071824496, + 0.102414338837483, + 0.142001058053682, + 0.661345901684745 + ], + "sp": [ + 0.728749087941912, + 0.604009739044929, + 0.765737298159677, + 0.635911417420188, + 0.0202492214735289, + 0.204606048332478, + 0.961097299900897, + 0.247501962269582, + 0.390649837768906, + 0.191422823510681, + 0.349173343060724, + 0.391218935337561, + 0.662371683919494, + 0.0, + 0.0237489497885058, + 0.00534042318616451, + 0.042839878329642, + 0.014915239519088, + 0.58853184318891, + 0.133022267748029, + 0.0389027000991026 + ], + "stack": [ + 1.0, + 1.0, + 1.0, + 1.0, + 3.0, + 2.0, + 1.0 + ] + }, + "f259a192005de9528825526494fe1f4a794fd907775a6177cf027960ef8e9d37": { + "dim": [ + 3.0, + 3.0, + 3.0, + 3.0, + 3.0, + 3.0, + 2.0 + ], + "reg": [ + 3.0, + 3.0, + 1.0, + 1.0, + 1.0, + 3.0, + 2.0 + ], + "rp": [ + 0.367396112844367, + 0.48649041928201, + 1.0, + 0.525025797016456, + 0.605951509953305, + 0.309989276221125, + 0.1261788612846, + 0.0321915601623782, + 0.025099615150151, + 0.0, + 0.474974202983544, + 0.215238792037356, + 0.146670241259625, + 0.8738211387154, + 0.600412326993255, + 0.488409965567839, + 0.0, + 0.0, + 0.178809698009339, + 0.54334048251925, + 0.0 + ], + "sp": [ + 0.0706191471108826, + 0.0935108927019722, + 0.192215281114849, + 0.100917981166065, + 0.116473139827642, + 0.059584675871432, + 0.223612860272346, + 0.0313805629152118, + 0.0246031786698547, + 0.0, + 0.384208249704807, + 0.251652779874421, + 0.152902138827192, + 0.776387139727654, + 0.898000289973906, + 0.881885928628173, + 0.807784718885151, + 0.514873769129128, + 0.631874080297937, + 0.787513185301376, + 0.0 + ], + "stack": [ + 3.0, + 3.0, + 3.0, + 3.0, + 3.0, + 3.0, + 2.0 + ] + }, + "f81a2f3e2e628d9ed45c832f9d918e414fa0067e09ad3f05dbf902e128dfcc11": { + "dim": [ + 2.0, + 1.0, + 3.0, + 1.0, + 3.0, + 1.0, + 1.0 + ], + "reg": [ + 1.0, + 3.0, + 2.0, + 3.0, + 1.0, + 2.0, + 2.0 + ], + "rp": [ + 0.448467381839221, + 0.371322365770592, + 0.107804688574043, + 0.364721980580829, + 0.434221182398865, + 0.0573308395379879, + 0.233393419473269, + 0.27576630908039, + 0.248949247363026, + 0.607804688574043, + 0.232759049836719, + 0.282889408800568, + 0.557330839537988, + 0.487003693668921, + 0.27576630908039, + 0.379728386866382, + 0.284390622851915, + 0.402518969582452, + 0.282889408800568, + 0.385338320924024, + 0.27960288685781 + ], + "sp": [ + 0.408217687971706, + 0.694825980514663, + 0.00996551147459309, + 0.730206467394999, + 0.0401395916350635, + 0.591868371183633, + 0.693255539765542, + 0.514675810635475, + 0.270071765152989, + 0.312502774498545, + 0.232584507068415, + 0.264109611675692, + 0.372510789782716, + 0.280897849904545, + 0.0771065013928193, + 0.0351022543323483, + 0.677531714026862, + 0.0372090255365866, + 0.695750796689245, + 0.0356208390336505, + 0.0258466103299126 + ], + "stack": [ + 2.0, + 1.0, + 3.0, + 1.0, + 3.0, + 1.0, + 1.0 + ] + } + }, + "label": "stack14.class.balanced", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/invariants/test_stack.py b/tests/invariants/test_stack.py index 60129bdf..cf6f12bc 100644 --- a/tests/invariants/test_stack.py +++ b/tests/invariants/test_stack.py @@ -520,7 +520,12 @@ def test_obj_fn_must_return_one_numeric_value() -> None: nns_stack(x, y, obj_fn=lambda p, a: (p - a) ** 2, folds=2, method=1) with pytest.raises(ValueError, match=r"\[obj.fn\] must return one numeric value"): nns_stack(x, y, obj_fn=lambda p, a: "bad", folds=2, method=1) - one = nns_stack(x, y, obj_fn=lambda p, a: np.array([np.sum((p - a) ** 2)]), folds=2, method=1) + # A length-one array is accepted. The default objective is R's + # sum((predicted - actual)^2), whose sum() accumulates in long double, so + # the equivalent custom objective uses the same arithmetic (r_sse). + from nns._rmean import r_sse + + one = nns_stack(x, y, obj_fn=lambda p, a: np.array([r_sse(p, a)]), folds=2, method=1) ref = nns_stack(x, y, folds=2, method=1) assert one["OBJfn.reg"] == ref["OBJfn.reg"] diff --git a/tests/parity/test_r_arithmetic.py b/tests/parity/test_r_arithmetic.py index f40a1dea..af33777c 100644 --- a/tests/parity/test_r_arithmetic.py +++ b/tests/parity/test_r_arithmetic.py @@ -127,7 +127,10 @@ def test_quantile_matches_r_bitwise(type_: int) -> None: for n in (1, 2, 3, 7, 50, 401): x = rng.normal(size=n) * 10 ** rng.uniform(-3, 3) expected = _exact( - f"rarith.quantile{type_}", f"quantile(x, p, type = {type_}, names = FALSE)", x=x, p=probs + f"rarith.quantile{type_}", + f"quantile(x, p, type = {type_}, names = FALSE)", + x=x, + p=probs, ) _bits(r_quantile(x, probs, type_), expected) diff --git a/tests/parity/test_stack_boost_r14_contracts.py b/tests/parity/test_stack_boost_r14_contracts.py new file mode 100644 index 00000000..c295d704 --- /dev/null +++ b/tests/parity/test_stack_boost_r14_contracts.py @@ -0,0 +1,113 @@ +"""NNS.stack / NNS.boost behaviour that depends on R's exact arithmetic and dispatch. + +- Regression stacks pick the blend weight from ``seq(0, 1, by = 0.01)`` by the + out-of-fold objective, taking the middle of exactly tied weights. When both + components fit the same coordinate the 101 scores differ only by rounding, + so the default objective must be R's ``sum((predicted - actual)^2)`` (a + long-double ``sum()``) and the grid R's ``seq()`` values. +- Nominal cells use ``findInterval(..., rightmost.closed = TRUE)``: only a value + equal to the last breakpoint joins the final interval, so observations above + the last cell mean (the breakpoints are means) keep a cell of their own. +- NNS.boost passes ``type = type`` to its learners: a numeric 0/1 response is + classified by each base NNS.reg even though the boost itself is a regression. +""" + +from __future__ import annotations + +import warnings +from typing import Any + +import numpy as np +import pytest +from _r import nns_frame_call, r_column +from _tolerances import COMPOUND + +from nns import nns_boost, nns_stack + +pytestmark = pytest.mark.parity + + +def _num(values: Any) -> dict[str, Any]: + return r_column([float(v) for v in np.asarray(values, dtype=np.float64).ravel()], "numeric") + + +def _frame(x: np.ndarray, y: np.ndarray, z: np.ndarray) -> dict[str, Any]: + return {"v": _num(x.ravel(order="F")), "y": _num(y), "t": _num(z.ravel(order="F"))} + + +def _three_level(seed: int, n: int = 60) -> tuple[np.ndarray, np.ndarray, np.ndarray]: + rng = np.random.default_rng(seed) + x = rng.normal(size=(n, 2)) + y = np.digitize(x[:, 0] + rng.normal(size=n) * 0.3, [-0.5, 0.5]).astype(float) + 1 + return x, y, rng.normal(size=(7, 2)) + + +@pytest.mark.parametrize("seed", [101, 102, 103, 104]) +def test_regression_stack_blend_weight_ties_follow_r(seed: int) -> None: + x, y, z = _three_level(seed) + expected = nns_frame_call( + "stack14.blend", + "r <- NNS.stack(matrix(v, ncol = 2), y, IVs.test = matrix(t, ncol = 2), folds = 2, " + "status = FALSE, ncores = 1); list(w = unname(r$weights), stack = r$stack, " + "obj = c(r$OBJfn.reg, r$OBJfn.dim.red))", + _frame(x, y, z), + exact=True, + ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + actual = nns_stack(x, y, z, folds=2, ncores=1) + np.testing.assert_array_equal( + [actual["weights"]["reg"], actual["weights"]["dim.red"]], expected["w"] + ) + np.testing.assert_array_equal([actual["OBJfn.reg"], actual["OBJfn.dim.red"]], expected["obj"]) + np.testing.assert_allclose(actual["stack"], expected["stack"], rtol=0, atol=COMPOUND) + + +@pytest.mark.parametrize("seed", [3, 35, 46, 95]) +def test_balanced_class_stack_cells_follow_r(seed: int) -> None: + rng = np.random.default_rng(200 + seed) + x = rng.normal(size=(60, 2)) + y = np.digitize(x[:, 0] + rng.normal(size=60) * 0.3, [-0.5, 0.5]).astype(float) + 1 + z = rng.normal(size=(7, 2)) + expected = nns_frame_call( + "stack14.class.balanced", + f"r <- NNS.stack(matrix(v, ncol = 2), y, IVs.test = matrix(t, ncol = 2), folds = 2, " + f"balance = TRUE, type = 'CLASS', seed = {seed}L, status = FALSE, ncores = 1); " + "list(reg = r$reg, dim = r$dim.red, stack = r$stack, " + "rp = as.numeric(r$reg.probabilities), sp = as.numeric(r$probabilities))", + _frame(x, y, z), + ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + actual = nns_stack(x, y, z, folds=2, balance=True, type="CLASS", seed=seed, ncores=1) + np.testing.assert_array_equal(actual["reg"], expected["reg"]) + np.testing.assert_array_equal(actual["dim.red"], expected["dim"]) + np.testing.assert_array_equal(actual["stack"], expected["stack"]) + reg_probs = np.asarray(actual["reg.probabilities"], dtype=np.float64) + probs = np.asarray(actual["probabilities"], dtype=np.float64) + np.testing.assert_allclose(reg_probs.ravel(order="F"), expected["rp"], rtol=0, atol=COMPOUND) + np.testing.assert_allclose(probs.ravel(order="F"), expected["sp"], rtol=0, atol=COMPOUND) + + +@pytest.mark.parametrize("seed", [123, 9]) +def test_boost_numeric_binary_response_learners_classify_as_in_r(seed: int) -> None: + rng = np.random.default_rng(300 + seed) + x = rng.normal(size=(80, 3)) + y = (x[:, 0] - x[:, 1] + rng.normal(size=80) * 0.5 > 0).astype(float) + z = rng.normal(size=(6, 3)) + expected = nns_frame_call( + "boost14.binary.numeric", + f"r <- NNS.boost(matrix(v, ncol = 3), y, IVs.test = matrix(t, ncol = 3), " + f"learner.trials = 10, seed = {seed}L, status = FALSE, feature.importance = FALSE); " + "list(results = r$results, weights = unname(r$feature.weights[c('V1', 'V2', 'V3')]), " + "frequency = unname(r$feature.frequency[c('V1', 'V2', 'V3')]))", + _frame(x, y, z), + ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + actual = nns_boost(x, y, z, learner_trials=10, seed=seed) + np.testing.assert_allclose(actual["results"], expected["results"], rtol=0, atol=COMPOUND) + weights = [actual["feature.weights"].get(k, 0.0) for k in ("V1", "V2", "V3")] + frequency = [actual["feature.frequency"].get(k, 0.0) for k in ("V1", "V2", "V3")] + np.testing.assert_allclose(weights, np.nan_to_num(expected["weights"]), rtol=0, atol=COMPOUND) + np.testing.assert_allclose(frequency, np.nan_to_num(expected["frequency"]), rtol=0, atol=0) From c932a907d7d24ecafa716533ab95dbebf5df83e9 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 15:16:59 +0000 Subject: [PATCH 03/16] Match R in derivatives, partial-moment VaR and NNS.VAR contracts Live comparison against R 14.0 (R 4.5.0) found: - LPM.VaR / UPM.VaR at integer degree differed from R by a few ulps: R builds the prefix power sums with cumsum() and sum() (long-double accumulators) and evaluates t^2 as t * t; the port used NumPy cumsum/sum and pow(). dy.d_ derives its stencil half-widths from these quantiles, so tied coordinates could move a node and change the derivative (0.37 vs 0.367 in one case). Both now follow R, verified bit for bit over integer and fractional degrees. - dy.d_ finite-difference weights use R's solve() (reference LAPACK dgesv: recursive dgetrf2 with reciprocal scaling, dgetrs), R's power rule for the Vandermonde rows, and R's colMeans/sum/rowMeans(na.rm). NumPy's solve differed from R on 283 of 400 small systems. - dy.dx: any character eval.point requests the overall gradient (R's is.character test; the port accepted only "overall"), and both fits pass type = "REG", so a two-valued response is a regression as in R. Means use R's mean(), and the bandwidth grid the C exp/log. - NNS.diff averages with R's mean() and rounds with R's round(). - NNS.VAR accepts R's validation = "observed" ("conditional" stays as an alias), applies R's horizon check in both modes and returns R's validation element (mode, origin, horizon, observed.targets). - The pure-Python LPM/UPM fallback forms each term as R's C++ does (repeated multiplication for integer degrees, the C library pow otherwise, an in-order double sum / n) and the excess path uses Rcpp's two-pass mean; NumPy's vectorised pow differs from it in ~5% of cases. Tests: R-authored cases for NNS.diff (exact), dy.dx character and two-valued paths, dy.d_ on tied coordinates, LPM/UPM.VaR (exact), solve() (exact) and the VAR validation modes; eight fail on the previous code. VAR contract tests include the validation entry, and the fallback blocking invariant compares blocked and unblocked results. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt --- src/nns/_rblas.py | 89 +++ src/nns/_rmean.py | 49 ++ src/nns/core.py | 47 +- src/nns/diff.py | 105 +-- src/nns/var.py | 79 ++- tests/_r_cache/diff14.full.json | 247 +++++++ tests/_r_cache/dyd14.ties.json | 819 +++++++++++++++++++++++ tests/_r_cache/dydx14.binary.json | 20 + tests/_r_cache/dydx14.character.json | 11 + tests/_r_cache/solve14.bits.json | 51 ++ tests/_r_cache/var14.bits.json | 403 +++++++++++ tests/_r_cache/var14.validation.json | 45 ++ tests/invariants/test_arma.py | 1 + tests/invariants/test_core.py | 34 +- tests/invariants/test_var.py | 2 +- tests/parity/test_derivatives_var_r14.py | 205 ++++++ tests/parity/test_var.py | 4 + 17 files changed, 2127 insertions(+), 84 deletions(-) create mode 100644 tests/_r_cache/diff14.full.json create mode 100644 tests/_r_cache/dyd14.ties.json create mode 100644 tests/_r_cache/dydx14.binary.json create mode 100644 tests/_r_cache/dydx14.character.json create mode 100644 tests/_r_cache/solve14.bits.json create mode 100644 tests/_r_cache/var14.bits.json create mode 100644 tests/_r_cache/var14.validation.json create mode 100644 tests/parity/test_derivatives_var_r14.py diff --git a/src/nns/_rblas.py b/src/nns/_rblas.py index 2851127f..0603c0b2 100644 --- a/src/nns/_rblas.py +++ b/src/nns/_rblas.py @@ -43,3 +43,92 @@ def r_crossprod_vec( return np.zeros(values.shape[1], dtype=np.float64) products = values * weights[:, np.newaxis] return np.asarray(np.cumsum(products, axis=0)[-1], dtype=np.float64) + + +class RSingularError(ValueError): + """R's ``solve()``: "Lapack routine dgesv: system is exactly singular".""" + + +def _dgetrf2(a: NDArray[np.float64], r0: int, c0: int, m: int, n: int, ipiv: list[int]) -> None: + """Reference LAPACK ``DGETRF2`` (recursive LU, partial pivoting) on a block. + + ``ipiv`` holds global 0-based pivot rows. The column case scales the + multipliers by the pivot's reciprocal (``DSCAL``), as LAPACK does. + """ + if m == 1: + ipiv[r0] = r0 + if a[r0, c0] == 0.0: + raise RSingularError("Lapack routine dgesv: system is exactly singular") + return + if n == 1: + column = np.abs(a[r0 : r0 + m, c0]) + i = int(np.argmax(column)) # IDAMAX: first maximal |value| + ipiv[r0] = r0 + i + if a[r0 + i, c0] == 0.0: + raise RSingularError("Lapack routine dgesv: system is exactly singular") + if i != 0: + a[r0, c0], a[r0 + i, c0] = a[r0 + i, c0], a[r0, c0] + pivot = a[r0, c0] + if abs(pivot) >= np.finfo(np.float64).tiny: + reciprocal = 1.0 / pivot + for k in range(1, m): + a[r0 + k, c0] = reciprocal * a[r0 + k, c0] + else: + for k in range(1, m): + a[r0 + k, c0] = a[r0 + k, c0] / pivot + return + n1 = min(m, n) // 2 + n2 = n - n1 + _dgetrf2(a, r0, c0, m, n1, ipiv) + right = slice(c0 + n1, c0 + n) + for k in range(n1): # DLASWP on [A12; A22] + p = ipiv[r0 + k] + if p != r0 + k: + a[[r0 + k, p], right] = a[[p, r0 + k], right] + for j in range(c0 + n1, c0 + n): # DTRSM L L N U: A12 <- L11^-1 A12 + for k in range(n1): + if a[r0 + k, j] != 0.0: + for i in range(k + 1, n1): + a[r0 + i, j] = a[r0 + i, j] - a[r0 + k, j] * a[r0 + i, c0 + k] + for j in range(c0 + n1, c0 + n): # DGEMM: A22 <- A22 - A21 A12 + for col in range(n1): + temp = -a[r0 + col, j] + for i in range(r0 + n1, r0 + m): + a[i, j] = a[i, j] + temp * a[i, c0 + col] + _dgetrf2(a, r0 + n1, c0 + n1, m - n1, n2, ipiv) + left = slice(c0, c0 + n1) + for k in range(n1, min(m, n)): # DLASWP on A21 + p = ipiv[r0 + k] + if p != r0 + k: + a[[r0 + k, p], left] = a[[p, r0 + k], left] + + +def r_solve(a: NDArray[np.float64], b: NDArray[np.float64]) -> NDArray[np.float64]: + """R ``solve(a, b)`` for a small square system and one right-hand side. + + Reference LAPACK ``DGESV`` as R calls it: ``DGETRF`` (which uses the + recursive ``DGETRF2`` below its block size) and ``DGETRS``. R's + reciprocal-condition check is not reproduced; an exactly singular + system raises :class:`RSingularError` as R's does. + """ + lu = np.array(a, dtype=np.float64, copy=True) + x = np.array(b, dtype=np.float64, copy=True).reshape(-1) + n = lu.shape[0] + if lu.shape != (n, n) or x.size != n: + raise ValueError("solve() needs a square matrix and a matching right-hand side.") + ipiv = list(range(n)) + _dgetrf2(lu, 0, 0, n, n, ipiv) + for k in range(n): # DLASWP on b + p = ipiv[k] + if p != k: + x[k], x[p] = x[p], x[k] + for k in range(n): # DTRSM L L N U + if x[k] != 0.0: + for i in range(k + 1, n): + x[i] = x[i] - x[k] * lu[i, k] + for k in range(n - 1, -1, -1): # DTRSM L U N N + if x[k] != 0.0: + x[k] = x[k] / lu[k, k] + for i in range(k): + x[i] = x[i] - x[k] * lu[i, k] + return x diff --git a/src/nns/_rmean.py b/src/nns/_rmean.py index 0cb430c2..625e3a48 100644 --- a/src/nns/_rmean.py +++ b/src/nns/_rmean.py @@ -417,3 +417,52 @@ def r_sse(predicted: NDArray[np.float64], actual: NDArray[np.float64]) -> float: def r_accuracy(predicted: NDArray[np.float64], actual: NDArray[np.float64]) -> float: """NNS.stack / NNS.boost default classification objective ``mean(predicted == actual)``.""" return r_mean_logical(np.asarray(predicted) == np.asarray(actual)) + + +def r_row_means_na_rm(x: NDArray[np.float64]) -> NDArray[np.float64]: + """R ``rowMeans(x, na.rm = TRUE)``: non-NaN entries of each row accumulated + column by column in long double, divided by their count (0/0 is NaN).""" + m = np.asarray(x, dtype=np.float64) + sums = np.zeros(m.shape[0], dtype=np.longdouble) + counts = np.zeros(m.shape[0], dtype=np.int64) + for j in range(m.shape[1]): + column = m[:, j] + keep = ~np.isnan(column) + sums[keep] = sums[keep] + column[keep].astype(np.longdouble) + counts += keep + with np.errstate(invalid="ignore", divide="ignore"): + return np.asarray(sums / counts, dtype=np.float64) + + +def r_median(values: NDArray[np.float64]) -> float: + """R ``median()`` of a double vector: the middle order statistic, or R's + ``mean()`` of the two middle ones for an even count.""" + v = np.sort(np.asarray(values, dtype=np.float64).ravel()) + n = v.size + if n == 0: + return float("nan") + half = (n + 1) // 2 + if n % 2 == 1: + return float(v[half - 1]) + return r_mean(v[half - 1 : half + 1]) + + +def r_cumsum(values: NDArray[np.float64]) -> NDArray[np.float64]: + """R ``cumsum()`` of a double vector: a running long-double sum, each + partial sum rounded to double.""" + v = np.asarray(values, dtype=np.float64).ravel() + return np.asarray(np.cumsum(v.astype(np.longdouble)), dtype=np.float64) + + +def r_pow(values: NDArray[np.float64] | float, exponent: float) -> NDArray[np.float64]: + """R's ``x ^ y`` for a scalar exponent (arithmetic.c ``R_POW``/``R_pow``): + ``x * x`` for 2, 1 for 0 or for a base of 1, else the C library ``pow``.""" + import math + + v = np.asarray(values, dtype=np.float64) + if exponent == 2.0: + return np.asarray(v * v, dtype=np.float64) + if exponent == 0.0: + return np.ones_like(v, dtype=np.float64) + flat = [1.0 if x == 1.0 else math.pow(float(x), exponent) for x in v.ravel()] + return np.asarray(flat, dtype=np.float64).reshape(v.shape) diff --git a/src/nns/core.py b/src/nns/core.py index bc8aeff2..451f0946 100644 --- a/src/nns/core.py +++ b/src/nns/core.py @@ -1,5 +1,7 @@ from __future__ import annotations +import math + import numpy as np from numpy.typing import NDArray @@ -71,10 +73,13 @@ def _excess_moment( "When excess_ret=True, target must be length 1 or same length as x." ) diff = targets - values if lower else values - targets + # R's C++ excess path: one term per element (degree 0 counts ties as + # lower), then Rcpp's mean(), which is R's two-pass long-double mean. if degree == 0: hits = diff >= 0.0 if lower else diff > 0.0 - return float(np.mean(hits)) - return float(np.mean(np.maximum(diff, 0.0) ** degree)) + return r_mean(hits.astype(np.float64)) + keep = diff > 0.0 + return r_mean(np.where(keep, _c_components(diff, degree, True), 0.0)) def lpm_ratio( @@ -121,16 +126,40 @@ def _moment( moments = np.empty(targets.size, dtype=np.float64) for start in range(0, targets.size, _TARGET_BLOCK): grid = targets[start : start + _TARGET_BLOCK, np.newaxis] - if degree == 0: - # R convention: equality with the target counts toward the lower moment. - block = np.mean(values <= grid if lower else values > grid, axis=1) - else: - deviations = grid - values if lower else values - grid - block = np.mean(np.maximum(0.0, deviations) ** degree, axis=1) - moments[start : start + _TARGET_BLOCK] = block + deviations = grid - values if lower else values - grid + # R's LPM_C / UPM_C: one component per observation, summed in order + # in double, then divided by n. + components = _c_components(deviations, degree, lower) + moments[start : start + _TARGET_BLOCK] = np.cumsum(components, axis=1)[:, -1] / values.size return _result_for_target(moments, target) +def _c_components( + deviations: NDArray[np.float64], degree: float, lower: bool +) -> NDArray[np.float64]: + """Per-observation partial-moment terms exactly as R's C++ forms them. + + Degree 0 counts ``deviation >= 0`` (lower; ties count as lower) or ``> 0`` + (upper). An integer degree is repeated multiplication starting from 1.0 + (``repeatMultiplication``); any other degree is the C library ``pow``, + which NumPy's vectorised ``power`` does not reproduce bit for bit. + """ + keep = deviations >= 0.0 if lower else deviations > 0.0 + if degree == 0: + return keep.astype(np.float64) + positive = np.where(keep, deviations, 0.0) + if degree == int(degree): + out = np.ones_like(positive) + for _ in range(int(degree)): + out = out * positive + else: + out = np.asarray(_libm_pow(positive, degree), dtype=np.float64) + return np.where(keep, out, 0.0) + + +_libm_pow = np.frompyfunc(math.pow, 2, 1) + + def _ratio( degree: float, target: float | NDArray[np.float64] | None, diff --git a/src/nns/diff.py b/src/nns/diff.py index a3b60560..17e1a9d6 100644 --- a/src/nns/diff.py +++ b/src/nns/diff.py @@ -8,6 +8,16 @@ import numpy as np from numpy.typing import NDArray +from nns._rblas import RSingularError, r_solve +from nns._rmean import ( + r_col_means, + r_mean, + r_median, + r_round_array, + r_row_means_na_rm, + r_sum, +) + DiffResult = dict[str, float] DyDxResult = float | dict[str, NDArray[np.float64]] _DyDReducer = Callable[[list[NDArray[np.float64]]], dict[str, NDArray[np.float64]]] @@ -88,7 +98,7 @@ def _nns_diff( upper_b = max(b1, b2) if _r_all_equal(lower_b, upper_b, tolerance=math.sqrt(_DBL_EPS)): - slope = float(np.mean([left_slope, right_slope])) + slope = r_mean(np.array([left_slope, right_slope])) return _rounded_result( [ f_x, @@ -110,7 +120,7 @@ def _nns_diff( ) high_b = max(b1, b2) - new_b = float(np.mean([lower_b, upper_b])) + new_b = r_mean(np.array([lower_b, upper_b])) # R: mean(c(lower.B, upper.B)) iteration = 1 converged = False termination_code = 2 @@ -149,7 +159,7 @@ def new_f(x: float, intercept: float = current_b) -> float: upper_b = new_b else: lower_b = new_b - new_b = float(np.mean([lower_b, upper_b])) + new_b = r_mean(np.array([lower_b, upper_b])) # Converged once the bisection bracket on B is tight: inferred_h cannot reach # |h| < tol in double precision once it is accurate. The derivative is # (f(x) - B) / point, so the bracket is scaled to the derivative, not to |B|. @@ -159,12 +169,12 @@ def new_f(x: float, intercept: float = current_b) -> float: break iteration += 1 - final_b = float(np.mean([upper_b, lower_b])) + final_b = r_mean(np.array([upper_b, lower_b])) if np.isfinite(inferred_h): inferred_h = abs(float(inferred_h)) if abs(point) < np.sqrt(np.finfo(float).eps): - slope = float(np.mean(_finite_step(f, point, h_value)[:2])) + slope = r_mean(np.array(_finite_step(f, point, h_value)[:2])) else: slope = (f_x - final_b) / point @@ -221,20 +231,21 @@ def dy_dx( if np.any(np.isnan(np.column_stack((x_values, y_values)))): raise ValueError("You have some missing values, please address.") if isinstance(eval_point, str): - if eval_point.lower() != "overall": - raise ValueError("eval_point must be 'overall', numeric, or None.") + # R: any character eval.point requests the overall mean gradient, + # mean(NNS.reg(x, y, type = "REG", ...)$Fitted.xy$gradient). from nns.regression import nns_reg result = nns_reg( x_values, y_values, + type="REG", plot=False, dist=None, ) fitted = result["Fitted.xy"] if not isinstance(fitted, dict): raise TypeError("nns_reg returned an unexpected fitted table.") - return float(np.mean(np.asarray(fitted["gradient"], dtype=np.float64))) + return r_mean(np.asarray(fitted["gradient"], dtype=np.float64)) return _dy_dx_numeric(x_values, y_values, np.asarray(eval_point, dtype=np.float64).reshape(-1)) @@ -422,17 +433,6 @@ def _v057_bandwidths(x_size: int, wrt_dependence: float) -> NDArray[np.float64]: return np.asarray(h_s, dtype=np.float64) -def _dydx_step(column: NDArray[np.float64], eval_value: float, band: float) -> float: - """Locally-adaptive dy.dx step: VaR(p + H, 1, x) - VaR(p - H, 1, x), p = CDF(eval).""" - from nns.core import lpm_ratio - from nns.var import lpm_var - - p = float(lpm_ratio(1.0, float(eval_value), column)) - upper = lpm_var(min(1.0, p + band), 1.0, column) - lower = lpm_var(max(0.0, p - band), 1.0, column) - return float(upper - lower) - - def _r14_fd_weights(nodes: NDArray[np.float64], at: float, degree: int) -> NDArray[np.float64]: """R .nns_fd_weights: scaled Vandermonde finite-difference coefficients.""" n = int(nodes.size) @@ -442,13 +442,31 @@ def _r14_fd_weights(nodes: NDArray[np.float64], at: float, degree: int) -> NDArr if not np.isfinite(scale) or scale == 0.0: return np.full(n, np.nan) z = (nodes - at) / scale - powers = z[None, :] ** np.arange(n)[:, None] + # R: outer(0:(n-1), z, function(k, value) value^k), where R's ^ is x * x + # for an exponent of 2 and the C pow() otherwise; then solve() (LAPACK + # dgesv) and division by scale^degree with the same power rule. + powers = np.empty((n, n), dtype=np.float64) + for k in range(n): + powers[k] = _r_pow(z, float(k)) rhs = np.zeros(n, dtype=np.float64) rhs[degree] = float(math.factorial(degree)) try: - return np.asarray(np.linalg.solve(powers, rhs) / (scale**degree), dtype=np.float64) - except np.linalg.LinAlgError: + weights = r_solve(powers, rhs) + except RSingularError: return np.full(n, np.nan) + return np.asarray(weights / _r_pow(np.array([scale]), float(degree))[0], dtype=np.float64) + + + +def _r_pow(values: NDArray[np.float64], exponent: float) -> NDArray[np.float64]: + """R's ``x ^ y`` (arithmetic.c R_POW / R_pow) for a scalar exponent.""" + v = np.asarray(values, dtype=np.float64) + if exponent == 2.0: + return v * v + if exponent == 0.0: + return np.ones_like(v) + out = [1.0 if x == 1.0 else math.pow(float(x), exponent) for x in v.ravel()] + return np.array(out, dtype=np.float64).reshape(v.shape) def _r14_dy_stencil( @@ -589,31 +607,23 @@ def reduce(parts: list[NDArray[np.float64]]) -> dict[str, NDArray[np.float64]]: second = np.full((points.size, widths.size), np.nan) for index, band, point_index, context_rows, w_first, w_second in specifications: predicted = np.asarray(parts[index], dtype=np.float64) - means = np.mean(predicted.reshape(-1, context_rows), axis=1) + # R: colMeans(matrix(part(index), nrow = spec$n)), then + # sum(spec$stencil$first * values) with R's long-double sum(). + means = r_col_means(predicted.reshape(-1, context_rows).T) centered = means - means[0] - first[point_index, band] = float(np.sum(w_first * centered)) - second[point_index, band] = float(np.sum(w_second * centered)) + first[point_index, band] = r_sum(w_first * centered) + second[point_index, band] = r_sum(w_second * centered) def average(a: NDArray[np.float64]) -> NDArray[np.float64]: - with np.errstate(invalid="ignore"): - numerator = np.nansum(a, axis=1) - count = np.sum(~np.isnan(a), axis=1) - return np.asarray( - np.divide( - numerator, - count, - out=np.full(a.shape[0], np.nan), - where=count > 0, - ), - dtype=np.float64, - ) + # R: rowMeans(z, na.rm = TRUE), NaN (0/0) for an all-NA row. + return r_row_means_na_rm(a) result = {"First": average(first), "Second": average(second)} if mixed: matrix = np.full((points.size, widths.size), np.nan) for index, band, point_index, weights in mixed_specs: predicted = np.asarray(parts[index], dtype=np.float64) - matrix[point_index, band] = float(np.sum(weights * (predicted - predicted[0]))) + matrix[point_index, band] = r_sum(weights * (predicted - predicted[0])) result["Mixed"] = average(matrix) return result @@ -700,7 +710,9 @@ def _dy_dx_numeric( raise ValueError("eval_point must be finite.") n = x.size root_n = int(np.floor(np.sqrt(n))) - h_s = np.rint(np.exp(np.linspace(np.log(2.0), np.log(float(root_n)), 5))) + # R: round(exp(seq(log(2), log(root_n), length.out = 5))), with C exp/log. + grid = np.linspace(math.log(2.0), math.log(float(root_n)), 5) + h_s = np.rint(np.array([math.exp(float(v)) for v in grid])) # Typical spacing between neighbouring x values: diff() of x # in row order measured the gap between consecutive rows, so unsorted input # gave steps spanning the whole range and depended on row order. @@ -721,6 +733,7 @@ def _dy_dx_numeric( x, y, point_est=point_est, + type="REG", point_only=True, smooth=True, plot=False, @@ -744,8 +757,8 @@ def _dy_dx_numeric( second_out = np.empty(unique_eval.size, dtype=np.float64) for index, point in enumerate(unique_eval): mask = eval_col == point - first_out[index] = float(np.mean(first[mask])) - second_out[index] = float(np.mean(second[mask])) + first_out[index] = r_mean(first[mask]) # R: mean(first.deriv.vec[...]) + second_out[index] = r_mean(second[mask]) return { "eval.point": unique_eval, "first.derivative": first_out, @@ -761,11 +774,12 @@ def _dy_d_eval_points( if isinstance(eval_points, str): option = eval_points.lower() if option == "median": - return np.median(x, axis=0).reshape(1, -1), False + # R: matrix(apply(x, 2, median), nrow = 1) + return np.array([[r_median(x[:, j]) for j in range(x.shape[1])]]), False if option == "last": return x[-1:, :].copy(), False if option == "mean": - return np.mean(x, axis=0).reshape(1, -1), False + return r_col_means(x).reshape(1, -1), False # R: matrix(colMeans(x), nrow = 1) if option == "apd": return x[:, wrt_index].copy(), True if option == "obs": @@ -827,7 +841,7 @@ def _finite_step( f_x = _eval_real(f, point, "f(point)") neg_step = (f_x - _eval_real(f, point - h, "f(point - h)")) / h pos_step = (_eval_real(f, point + h, "f(point + h)") - f_x) / h - return neg_step, pos_step, float(np.mean([neg_step, pos_step])) + return neg_step, pos_step, r_mean(np.array([neg_step, pos_step])) # R: mean(c(...)) _DBL_EPS = float(np.finfo(float).eps) @@ -982,5 +996,6 @@ def _finite_scalar(value: float, name: str) -> float: def _rounded_result(values: list[float], digits: int) -> DiffResult: - rounded = np.round(np.asarray(values, dtype=np.float64), decimals=digits) + # R: round(as.matrix(...), digits), with R's rounding rule (nmath fround). + rounded = r_round_array(np.asarray(values, dtype=np.float64), digits) return {key: float(value) for key, value in zip(_RESULT_KEYS, rounded, strict=True)} diff --git a/src/nns/var.py b/src/nns/var.py index 900dd484..fc73d759 100644 --- a/src/nns/var.py +++ b/src/nns/var.py @@ -17,7 +17,16 @@ from nns._helpers import _warn_unsupported from nns._native import honours_ncores -from nns._rmean import r_cor_matrix, r_mean, r_quantile, r_rank_average, r_round_array +from nns._rmean import ( + r_cor_matrix, + r_cumsum, + r_mean, + r_pow, + r_quantile, + r_rank_average, + r_round_array, + r_sum, +) from nns.core import lpm_ratio, upm_ratio ObjectiveFn = Callable[[NDArray[np.float64], NDArray[np.float64]], float] @@ -34,6 +43,7 @@ class VarResult(TypedDict): univariate: NotRequired[NDArray[np.float64]] multivariate: NotRequired[NDArray[np.float64]] ensemble: NotRequired[NDArray[np.float64]] + validation: NotRequired[dict[str, Any]] class _LagSpec(TypedDict): @@ -76,8 +86,12 @@ def nns_var( ``validation="pipeline"`` (R14 default) rebuilds preprocessing and future predictors at the validation origin, before observing the held-out targets. - ``validation="conditional"`` retains the older conditional split for an - explicitly requested comparison; it is not the default forecasting test. + ``validation="observed"`` (R's compatibility option; ``"conditional"`` is + accepted as an alias) retains the older conditional split for an explicitly + requested comparison; it is not the default forecasting test. As in R, the + result's ``validation`` entry records the ``mode``, the validation + ``origin`` and ``horizon``, and, in pipeline mode, the count of finite + held-out targets per variable (``observed.targets``; ``None`` otherwise). For ``h == 0`` the result is a dictionary with ``interpolated_and_extrapolated`` and ``names`` instead of R's bare data frame. ``nowcast`` (row labelling only @@ -100,16 +114,24 @@ def nns_var( if h_value > 0: _var_check_sample_size(variables_matrix.shape[0], h_value, lag_spec["max"]) - if validation not in {"pipeline", "conditional"}: - raise ValueError("validation must be 'pipeline' or 'conditional'.") + # R: validation = c("pipeline", "observed"); "conditional" is the port's + # earlier name for "observed" and remains accepted. + if validation == "conditional": + validation = "observed" + if validation not in {"pipeline", "observed"}: + raise ValueError("validation must be 'pipeline' or 'observed'.") + n_rows = variables_matrix.shape[0] + validation_h = ( + max(2 * h_value, math.ceil(0.2 * (n_rows - lag_spec["max"]))) if h_value > 0 else 0 + ) + origin = n_rows - validation_h + if h_value > 0 and validation_h >= n_rows - lag_spec["max"]: + raise ValueError( + "The series is too short for the requested forecast horizon and lagged " + "validation window." + ) validation_data = None if h_value > 0 and validation == "pipeline": - validation_h = max( - 2 * h_value, math.ceil(0.2 * (variables_matrix.shape[0] - lag_spec["max"])) - ) - origin = variables_matrix.shape[0] - validation_h - if validation_h >= variables_matrix.shape[0] - lag_spec["max"]: - raise ValueError("The series is too short for the requested lagged validation window.") prefix = _var_interpolate_and_extrapolate( variables_matrix[:origin], validation_h, @@ -160,6 +182,18 @@ def nns_var( ) ensemble = univariate * uni_weights[np.newaxis, :] + multivariate * multi_weights[np.newaxis, :] + # R: validation = list(mode, origin, horizon, observed.targets), the last + # being colSums(is.finite(held-out targets)) in pipeline mode, else NULL. + observed_targets = ( + None + if validation_data is None + else { + name: float(count) + for name, count in zip( + names, np.sum(np.isfinite(validation_data[2]), axis=0), strict=True + ) + } + ) return { "interpolated_and_extrapolated": interpolated, "relevant_variables": relevant_variables, @@ -167,6 +201,12 @@ def nns_var( "multivariate": multivariate, "ensemble": ensemble, "names": names, + "validation": { + "mode": validation, + "origin": float(origin), + "horizon": float(validation_h), + "observed.targets": observed_targets, + }, } @@ -1090,17 +1130,19 @@ def _lpm_var_integer_solver(degree: int, values: NDArray[np.float64]) -> Callabl prefix_power[0] = k_break total_power[0] = float(n) for j in range(1, degree + 1): - x_power = x_sorted**j - prefix_power[j] = np.cumsum(x_power)[k_break - 1] - total_power[j] = float(np.sum(x_power)) + # R: x_power <- x_sorted^j; cumsum(x_power)[k_break]; sum(x_power) (long double). + x_power = r_pow(x_sorted, float(j)) + prefix_power[j] = r_cumsum(x_power)[k_break - 1] + total_power[j] = r_sum(x_power) lpm = np.zeros(unique_x.size, dtype=np.float64) upm = np.zeros(unique_x.size, dtype=np.float64) for j in range(degree + 1): coef = float(math.comb(degree, j)) - lpm += coef * (-1.0) ** j * unique_x ** (degree - j) * prefix_power[j] + t_power = r_pow(unique_x, float(degree - j)) + lpm = lpm + coef * float(r_pow(-1.0, float(j))) * t_power * prefix_power[j] suffix_power = total_power[j] - prefix_power[j] - upm += coef * (-1.0) ** (degree - j) * unique_x ** (degree - j) * suffix_power + upm = upm + coef * float(r_pow(-1.0, float(degree - j))) * t_power * suffix_power with np.errstate(invalid="ignore", divide="ignore"): ratio_break = lpm / (lpm + upm) @@ -1151,9 +1193,10 @@ def raw_moments(t: float) -> tuple[float, float]: upm_raw = 0.0 for j in range(degree + 1): coef = float(math.comb(degree, j)) - lpm_raw += coef * (-1.0) ** j * t ** (degree - j) * float(interval_prefix[j]) + t_power = float(r_pow(t, float(degree - j))) + lpm_raw += coef * float(r_pow(-1.0, float(j))) * t_power * float(interval_prefix[j]) suffix = float(total_power[j]) - float(interval_prefix[j]) - upm_raw += coef * (-1.0) ** (degree - j) * t ** (degree - j) * suffix + upm_raw += coef * float(r_pow(-1.0, float(degree - j))) * t_power * suffix return lpm_raw, upm_raw def root_value(t: float) -> float: diff --git a/tests/_r_cache/diff14.full.json b/tests/_r_cache/diff14.full.json new file mode 100644 index 00000000..9e556dcb --- /dev/null +++ b/tests/_r_cache/diff14.full.json @@ -0,0 +1,247 @@ +{ + "entries": { + "153224acd9be7120609339bdcd6f9eba68a558246a8d1fa29b86a50764362c31": [ + 22.0, + -26.00000013, + 16.00000004, + 3e-08, + 25.0, + 1.0, + 0.0, + 14.5, + 17.5, + 16.0, + 15.99999989, + 16.00000012, + 16.00000001, + 16.0 + ], + "3b86d2a1b832dc82fe447338fae9eab556b1c2c98845ee070aca4d683f380290": [ + 1.09861229, + 0.09861229, + 0.33333333, + 0.0, + 23.0, + 1.0, + 0.0, + 0.36464311, + 0.30830136, + 0.33647224, + 0.33333349, + 0.33333323, + 0.33333336, + 0.33333333 + ], + "646e699f2cf008a72e34b9d42923a70b9bb65def5622228d9c4dc694beb0587a": [ + 0.997494986604, + 0.891389186651, + 0.070737199969, + 2.274e-09, + 31.0, + 1.0, + 0.0, + 0.15006528068, + -0.009193729297, + 0.070435775691, + 0.070737209425, + 0.070737209425, + 0.070737209425, + 0.070737201668 + ], + "6d159bb178914745b09c5da25124043a695d767f077eb39f85625f8cbf1d920b": [ + 4.75, + -5.749999986254, + 6.999999990836, + 2.0902e-08, + 31.0, + 1.0, + 0.0, + 6.52, + 7.48, + 7.0, + 7.000000000072, + 7.000000000072, + 7.000000000072, + 7.0 + ], + "77f6621352e253506952c222f9e9a56b62b585af83d7155c42eb84653fe29b97": [ + 2.117000016613, + 0.529250003484, + 1.058500008752, + 2.561e-08, + 30.0, + 1.0, + 0.0, + 1.017266849855, + 1.101992022822, + 1.059629436338, + 1.058500009676, + 1.058500009676, + 1.058500009676, + 1.058500008306 + ], + "790ec3a88530949e563af2effc869514a03ef06561a33e8e2805486603d5d490": [ + 4.48168907, + -2.24084452, + 2.24084453, + 2e-08, + 25.0, + 1.0, + 0.0, + 1.98269223, + 2.54582721, + 2.26425972, + 2.24084448, + 2.24084453, + 2.24084451, + 2.24084454 + ], + "b70fc26c7b3faaaf8f7b96066bc0d9a5832098c2f07be1463569829ea8f85ba5": [ + -0.693147, + -1.693148, + 2.000001, + 0.0, + 16.0, + 1.0, + 0.0, + 2.130556, + 1.888811, + 2.009684, + 2.000001, + 1.999999, + 2.0, + 2.0 + ], + "b98a93428c890cdfd412cf91116999872377d03d932080eaa92131d377c3c54d": [ + 1.284025, + 0.963019, + 0.642013, + 1e-06, + 13.0, + 1.0, + 0.0, + 0.632478, + 0.65174, + 0.642109, + 0.642013, + 0.642013, + 0.642013, + 0.642013 + ], + "bf6fd9c98cb4b9b9d22b14c4c7c462534150288d2ff1c7793479e936949e7267": [ + 0.14112001, + 2.98888612, + -0.94925537, + NaN, + 1.0, + 0.0, + 2.0, + -0.91470427, + -0.98380647, + -0.94925537, + NaN, + NaN, + NaN, + NaN + ], + "cf3e8f3fc2d004516f6bf0a1ef5e67b2de666d76babd5b643b16adf9bce385d1": [ + 0.479426, + 0.040634, + 0.877583, + 0.0, + 12.0, + 1.0, + 0.0, + 0.891435, + 0.862678, + 0.877056, + 0.877583, + 0.877582, + 0.877583, + 0.877583 + ], + "d634082857ce055351d28299e3977f0fb57bd51de8245606a4f795bd4d1cc461": [ + 0.75, + 0.25, + 1.0, + 0.0, + 1.0, + 1.0, + 0.0, + 0.82, + 1.18, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0 + ], + "dad8a2707f27b63956960b296990fde84c1b33aa437e4aeb0ad4ad1513c42e82": [ + 0.405465108108, + -0.594534893311, + 0.666666667613, + 1.0922e-08, + 30.0, + 1.0, + 0.0, + 0.704971838408, + 0.633453089127, + 0.669212463768, + 0.666666668888, + 0.666666663806, + 0.666666666347, + 0.666666666667 + ], + "e2ec5459cf11b3bac90255c4c8948f7c583072bb02c2515703d9274cba8d7238": [ + 27.0, + -54.00000085, + 27.00000028, + 2e-08, + 25.0, + 1.0, + 0.0, + 22.75, + 31.75, + 27.25, + 27.00000016, + 27.00000038, + 27.00000027, + 27.0 + ], + "e3a02eb261451e09d25b98f0203e4dd0da9ee351c0489578aca512acf34a4da6": [ + 3.375, + -6.749999966381, + 6.749999977587, + 9.197e-09, + 31.0, + 1.0, + 0.0, + 6.0556, + 7.4956, + 6.7756, + 6.749999951712, + 6.750000048284, + 6.749999999998, + 6.75 + ], + "fb2d690c4bf0c1ed4760411b9db627f7f5febdded67d1387ca07d773f22d3027": [ + 0.125, + -0.25, + 0.749999, + 1e-06, + 16.0, + 1.0, + 0.0, + 0.6636, + 0.8436, + 0.7536, + 0.749999, + 0.750001, + 0.75, + 0.75 + ] + }, + "label": "diff14.full", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/dyd14.ties.json b/tests/_r_cache/dyd14.ties.json new file mode 100644 index 00000000..fe050a5f --- /dev/null +++ b/tests/_r_cache/dyd14.ties.json @@ -0,0 +1,819 @@ +{ + "entries": { + "b555024ed8ac7a90535dfe730daa03c6a3e3988b543dbdca96aae1e5fec502e0": { + "f": [ + -1.36396011708939, + -1.0324758117611, + 0.712940873802411, + 1.60064919323433, + 0.691657539983599, + 1.34331240586311, + 0.327329303719588, + -0.430895407043147, + -1.41380098148186, + -1.66926966481656, + 0.774805431540598, + 2.12665403285321, + -0.938378285342727, + -0.351913004660772, + -0.0934865847271338, + 0.727366978086158, + -1.46390045840654, + -0.820050421160988, + 0.126383563666302, + 1.30277058118103, + -0.0222475920732626, + 2.23082475412254, + -0.73151598683133, + 1.77694781694508, + -1.44787513329068, + 1.53815868461893, + 0.0349547820762456, + -1.57761077500113, + 0.463945360048434, + 1.73804539495779, + -1.32440834146348, + 0.753819953502539, + 1.49645701184407, + 0.0989978983069089, + -1.0775204398127, + -0.249315666261983, + 2.65108009018933, + 0.371078912157041, + 1.21444523124185, + 0.930444949985036, + -0.316963467616278, + 0.554309087381441, + 0.174172913166896, + -0.442728000411485, + 1.18808877436495, + 1.66444570054243, + 0.715934857698482, + 0.86431088404674, + -0.449791955088665, + 0.484141142676134, + -1.35526318275616, + 0.573609206540832, + -0.540418289578491, + 1.22932422252573, + 1.51873965320094, + 2.66000032601825, + 1.37055061743361, + 1.70005334666772, + -1.00073210048903, + -0.04956086814357, + 1.30174932266631, + -0.419802353090391, + -0.262899372938871, + 0.573609206540832, + 0.126861525859186, + 1.57131645651149, + 0.345137518488851, + 1.72964384814301, + -0.157810685895501, + -1.76094473177578, + 1.56712770261378, + 0.104839544668173, + 0.188384738067588, + -1.02102740966887, + 1.04608378392908, + -1.5509180161856, + 0.238324095891727, + 0.0872148326339385, + 1.38452460890432, + -1.4335075999488, + -0.462755722657015, + -1.11339790183082, + -1.63701533906988, + 0.331947463555691, + -1.40348498068538, + 2.6397042616759, + -1.21586870011083, + -0.0222475920732626, + -1.44807108251418, + 0.174172913166896, + -1.27861558629365, + -1.30096232208231, + -0.437039859074675, + 1.39920369606469, + 1.76710057621532, + -0.473100619570265, + 0.301978241980298, + 0.938037666325697, + 1.93212151850629, + 0.561046971595193, + 0.2244904774964, + 0.371078912157041, + 2.37702284075908, + 2.48526566131808, + 2.27742825329763, + -0.474031951363393, + -0.875273487262377, + 2.17213129727953, + 1.65735321563976, + 1.07694602707186, + 0.875103544286024, + -0.524117021499689, + -1.67039661523481, + -0.121295477152279, + 1.17224740536565, + 1.66030000852687, + -0.41324360031462, + -0.425446267337446, + 2.44312917155663, + 0.917346052136874, + -0.853996863368174, + -2.06061570057741, + -0.19100244246452, + -0.111031664690043, + 0.35807232680891, + 0.526057329280799, + 0.301978241980298, + 0.87384796192707, + -2.06061570057741, + 1.80243963533915, + 0.987343282303727, + -1.20019051334866, + 1.95974663099394, + 0.0232050731436206, + -0.429461711262829, + 1.03914382710735, + -0.921208669455935, + 1.44935743587801, + 0.872450998026145, + -0.719631070718949, + 1.71936425838435, + 0.508963224801551, + 0.479780822074921, + 0.0989978983069089, + 2.3061332954562, + 0.761799653608404, + -0.867543652863762, + -1.54249793157573, + 1.7216312264864, + -0.340836387005707, + -0.763992609450844, + -0.883183887798849, + -0.499061668279862, + -0.557815362097925, + 0.171875842215624, + -0.907782311378365, + 0.345137518488851, + 0.355665963672612, + 0.688228581728578, + 1.14097764583523, + 0.663250666186291, + 0.0232050731436206, + 1.34662446021368, + 1.16937100547381, + 0.26929359177894, + 3.54315358450504, + -0.501222323558238, + -0.423061054177936, + 1.98609622178348, + -0.283607741944871, + 1.18734861248877, + -0.573603591394562, + -0.865551139992807, + -0.705780804777703, + -1.54938083418438, + -1.07243024003095, + 0.256810799931205, + 1.3778315481223, + 0.556331099531248, + -1.41379578169305, + 0.988804274686562, + -0.453708827411699, + 0.430930177362908, + 1.01179848430114, + 0.725033648568186, + 1.95839625914106, + 0.715934857698482, + -0.474031951363393, + 1.7054924650251, + 0.526057329280799, + 0.93929593987871, + -0.723093981304285, + 2.58039334704557, + 1.68162612565679, + -0.37134022327532, + 1.67783206457767, + 1.30543919869755, + 0.0989978983069089, + 0.0851953125666903, + 0.79346312575435 + ], + "s": [ + 1.58721762956503, + 0.560773505058257, + 2.12861222872015, + 0.819110876835088, + 1.48988698462554, + 2.06655329984099, + 1.35891469399263, + 1.88623483322938, + 0.36714444439392, + 0.885588925000874, + 1.73228167492296, + 2.20840964647149, + 1.86513828579021, + 1.85346133509096, + -0.189223383983104, + 1.01628194599169, + -0.629342125035978, + 0.410177137061997, + 2.37955618173035, + 1.40184649248579, + 2.85136995831115, + 0.779874211702729, + 1.47606881460095, + 1.98647968391553, + 1.28859235408138, + 0.687764910315344, + -4.62939861345055, + 0.141706990822719, + -0.0356031088439785, + 1.69565046073966, + 0.310021942335427, + 1.72752411618697, + 1.2162594973926, + 1.4861000515191, + 0.662188699812589, + 0.747279763347451, + 0.0965325996933244, + 0.526208555837445, + 1.58768624773465, + 1.21711967980584, + 0.180123050642081, + 3.94942879795446, + 0.46250211018148, + 1.71282898628767, + 1.3058262963201, + 1.8274984976641, + 2.21954106632028, + 4.14427194702849, + 0.587507513599047, + 2.07257530094535, + 0.308467215760796, + 2.79225121623396, + 1.22397664470643, + 0.998348159860515, + 1.96465494835887, + -0.98443599647279, + 1.93333336162097, + 1.20754468454748, + 1.81485909478638, + 1.45981238061623, + 1.75391669706372, + 1.9865190019553, + -0.436571517605228, + 2.79225121623396, + 2.13181663086535, + 1.84134212366846, + 3.01176888857317, + 1.79465908440307, + 1.06112933981058, + 0.694093813995723, + 0.464424162624355, + 1.27811179619375, + 0.965003699472715, + -2.3276567970977, + 1.31180937028451, + 0.893641415880662, + 0.0885150320966486, + -0.544467753939079, + 2.01556300962838, + 0.0626596730937833, + 2.10816894220921, + 1.52160309425475, + 0.975636118604418, + 3.13745089369677, + 1.11499984882017, + 1.79639192175755, + 0.348911022312414, + 2.85136995831115, + 1.00330519265653, + 0.46250211018148, + -0.430708503569991, + 1.4618561504235, + -0.327512174939882, + -4.31193468148635, + 1.72220409314526, + 1.76973276556831, + 1.23805576561541, + 1.01508252822018, + 2.2854173293362, + 1.51102546960658, + 1.80374806100403, + 0.526208555837445, + 1.82902666028168, + 2.27159641156799, + 1.33182899504607, + 3.56583454414664, + 1.69862705488336, + 2.06341165997458, + 1.29173984356631, + 1.53954171881289, + 2.02144720219612, + 0.224171595533476, + 0.379044418026026, + 1.50003108338745, + 1.83115226115803, + 0.79735406709217, + 2.5537171722904, + 1.88732801104607, + 1.55374457366594, + 1.86480436000659, + 1.53252869256382, + 0.53229094048283, + 0.942961811711274, + 2.44971805038086, + 3.04418519095662, + 0.342357474802264, + 1.23805576561541, + 1.97319280047282, + 0.53229094048283, + 1.44097218767379, + 1.0318355185185, + 1.67852507341555, + 1.79686438161251, + -0.381510443618389, + 2.17084278454366, + 1.18991101877744, + 0.475186328607151, + 1.85996337109806, + 1.52863909577948, + 1.84869829525102, + 1.67486024586926, + 1.97062203971192, + 0.672068699110316, + 1.4861000515191, + 2.00883997786279, + 3.10885208594728, + 1.45006665199326, + 0.834354733813171, + 2.05778296535614, + 0.764165209161026, + 1.93788076580932, + 1.46264594957912, + -0.367704274031578, + 2.14119813949509, + 2.34953545107706, + 1.7420795095386, + 3.01176888857317, + 2.86943372676165, + 3.83403723467962, + 1.82456214310799, + 2.11443036012078, + -0.381510443618389, + 2.5051615529265, + 1.29792656836583, + 1.37220108808098, + 0.5961794747421, + 1.74270652525533, + 1.98948670809092, + 1.89220471616193, + 1.45881506522719, + 1.63462683008385, + -0.477097217529323, + 2.42167457195006, + 0.933820354857584, + 0.13526531713344, + 0.367654937464729, + 2.3365601428273, + 1.94810141820003, + 1.57037223393796, + 0.0086938301133197, + 0.946211683222861, + 0.211195148694732, + 2.98291605396485, + 1.18591128118983, + 1.70987016872648, + 1.79430767787647, + 2.21954106632028, + 3.56583454414664, + 2.11791887461752, + 0.342357474802264, + 4.44215419706847, + 1.70060009283579, + -1.6084480168253, + 2.13029749056476, + 1.73043193707232, + 1.37479969048896, + 1.20866429355833, + 1.4861000515191, + 0.835340485381629, + 0.842641345461946 + ] + }, + "f51bdcfbbf5455c7af2708fec57b4f689098a50513e378531fd10bfff35b4a1e": { + "f": [ + 0.968136581360099, + 0.723333834250645, + 0.733252553595017, + 0.605634957024345, + 0.83912580062342, + 1.07207905997362, + 0.960559798157884, + 0.850781592081074, + 0.95983417722856, + 0.849081812938768, + 0.591608214591173, + 0.905125920892326, + 0.700584961183746, + 1.02202290457229, + -0.0676911004302298, + 0.833818950204362, + 0.930996017262551, + 0.679301724173194, + 0.69935753952158, + 0.773414086613284, + 0.870357517377361, + 0.0341689874659713, + 0.316833919789044, + 0.913194648499387, + 0.866853038566294, + 0.575354925213932, + 0.43860009043723, + 1.12799165202208, + 0.573375596588743, + 1.11644234731879, + 0.670787789038165, + 0.989846339303421, + 1.46840995352402, + 0.931841227707574, + 0.984509270190102, + 0.976354430234803, + 1.60498078486958, + 0.956194481349864, + 0.853649014784819, + 0.651736955419675, + 0.734043127738563, + 0.871533622213328, + 0.992347444839347, + 0.817227656080995, + 0.653592369449874, + 0.84220102893962, + 1.00573251981105, + 0.954539126626651, + 0.745000197909851, + 0.821044960895192, + 0.808366989299527, + 0.960143869503113, + 0.769997931670659, + 0.657682490295246, + 0.909683560088494, + 0.00274681094543039, + 0.943170919151808, + 0.743242330540197, + 0.883141344758873, + 0.822681186196002, + 0.835015443113694, + 0.866398771974039, + 0.205106340623309, + 0.960143869503113, + 0.962127993302192, + 0.845011010162311, + 0.925872349713726, + 0.81510367198723, + 0.650092622075452, + 0.72018419682759, + 0.588294461535713, + 0.906317469063558, + 0.79824957526603, + 0.687248278626044, + 0.738786224798953, + 0.942679580605089, + 0.850554887990788, + 0.56767028571985, + 0.868677663172651, + 0.89711688719605, + 0.921360692210101, + 0.766689847426699, + 0.809407470667707, + 0.908871056770768, + 0.773343440925858, + 0.880934833027951, + 0.632753681371425, + 0.870357517377361, + 1.08475576783252, + 0.992347444839347, + 1.1169526783299, + 0.72146691022087, + 0.421110404613903, + 1.30130988306301, + 0.768632135819744, + 0.886781620823703, + 0.72128308476565, + 0.5714066964784, + 1.05297888985842, + 0.963979496768673, + 0.901394167999289, + 0.956194481349864, + 0.793552372851024, + 0.938789892478831, + 0.721209720349617, + 0.937220495097137, + 0.829313483519883, + 0.775297158675089, + 0.851873064365179, + 0.955155292826787, + 0.848629652536581, + 0.813266591140067, + 0.967776228080621, + 0.956305844919816, + 1.01506551046974, + 0.626262482076711, + 0.860145409210151, + 0.91306254014492, + 0.857406498249838, + 0.919879160882018, + 0.815269505101617, + 0.444689162864259, + 0.907202288856755, + 0.726506212356371, + 0.596784366427753, + 0.984346645460447, + 0.72128308476565, + 0.594803826148187, + 0.444689162864259, + 0.736984419243133, + 0.77864776983418, + 1.09186487063991, + 0.733004407792444, + 0.236678964625954, + 0.548517618905469, + 0.777657949878676, + 0.725571664209381, + 0.945712087645076, + 0.966798261025215, + 0.922770224040467, + 0.839793632722173, + 0.712214079333363, + 0.996270823100779, + 0.931841227707574, + 0.866679760588404, + 0.73777283636337, + 0.791094274230702, + 0.992380558798585, + 1.20723048139651, + 0.935026802423926, + 0.950171968700288, + 0.812700405061942, + 0.357928690785075, + 0.901084759649935, + 0.951027347488667, + 1.05563022741981, + 0.925872349713726, + 0.558588182585709, + 0.991077954869335, + 0.907511897325118, + 0.730483105053371, + 0.236678964625954, + 0.778244072097313, + 0.724684251242865, + 0.9396208715648, + 0.820326137130905, + 0.472218256646904, + 0.989320590294339, + 1.07694243660657, + 0.653630435943637, + 0.831922081458017, + 0.449275098017114, + 0.996182000245862, + 0.803598125370134, + 0.986600921197693, + 0.830397117773563, + 0.428830456424973, + 1.06300975649659, + 0.888222644776325, + 0.653325352990193, + 0.609198406722025, + 0.802223545868649, + 0.945231120276044, + 0.69644989462212, + 0.959390341310848, + 1.04709327418893, + 1.00573251981105, + 0.937220495097137, + 0.912280412397708, + 0.984346645460447, + 0.688314961055874, + 0.5436548858734, + 0.862843747013587, + 1.12430148422825, + 0.859989693561678, + 0.850914794295758, + 0.892588673601631, + 0.931841227707574, + 0.919275188288308, + 0.777426265961743 + ], + "s": [ + 1.84087390385633, + -0.154227654009793, + 0.267178088112767, + -0.726964554815025, + -0.238621986774839, + 0.49521690968069, + -0.112763051942328, + -0.242530827723955, + -1.50134991143991, + 0.842256462410533, + -0.178098702250831, + 1.046072862711, + -1.64331311194701, + 0.611489232040721, + -0.29407424022582, + -0.0795450330193591, + -0.299271822988598, + -0.236805310491213, + 0.493159109874257, + -0.0217711927307528, + 0.26212590194957, + -0.102163940616183, + 0.5629804166581, + 1.19322664007393, + 0.184234659268133, + -0.849239543390698, + -2.66723136303293, + -0.474017044728435, + -0.482881581641327, + -0.305509186397432, + -0.372004040601519, + -0.0426669138918266, + -1.64147299299083, + -0.117890035670334, + -1.02293814584993, + -0.114106596265633, + -0.289370328312489, + 0.0198489509070114, + -1.00000795346845, + -0.250756667428024, + -0.112923572414659, + 0.331793732524147, + 0.0775944194979885, + 0.103229008949593, + -0.194710154614551, + 0.256171541282087, + 0.316131915240821, + 0.0372251381197092, + -0.31024209066775, + 0.454241524558057, + -0.968236561495761, + -0.167319131532477, + 0.406579612388159, + -0.395153370793504, + 0.265976006796165, + -8.06434352346341, + 1.83816669526133, + 0.189472773343079, + -2.6542609208673, + 0.298635087428241, + -0.672783648715958, + 0.291916670693844, + -0.839930001093405, + -0.167319131532477, + 1.38328396197082, + 0.0362301606517932, + -0.288675278086085, + 0.462839723024464, + 0.539752433278685, + 0.143339191733765, + -0.793979073757341, + -0.0989530399809347, + 0.279371271350111, + -1.90702850762017, + -0.300108694828814, + -0.710600962432796, + -0.109338561665693, + -0.481953856936066, + 0.373549537521305, + -0.296321314626586, + 0.152219260974826, + 0.63958304813852, + 0.417413125641143, + -0.0505531791178219, + -0.342638242819726, + 0.263143563365358, + -0.17391852411395, + 0.26212590194957, + 0.991844621797848, + 0.0775944194979885, + 0.641020835633084, + -2.26052587832557, + -0.327527839626699, + -1.17669666085188, + 0.301849836356846, + -0.964817409058138, + 0.362870835941291, + -0.275323511955573, + -0.277638836460866, + -0.156116873129571, + -0.0869423264513496, + 0.0198489509070114, + -0.0462918080897916, + 0.129589741831089, + -0.90851754560752, + 0.165541967050543, + 0.151657071496047, + 0.283586772523623, + 0.152564243826328, + -0.894347703680746, + -0.0495193859275705, + -0.164805198401192, + -0.246515026891954, + 0.236422783948769, + -0.0351058264016622, + -0.738313070370507, + -0.0224139888236826, + 0.752245627262475, + -0.783874345304231, + -0.0649577573932869, + -0.136513374045237, + 0.931551349117951, + -0.420133379501506, + -0.279310447991663, + 0.620956265357091, + 0.871460163183323, + 0.362870835941291, + 0.456176468035764, + 0.931551349117951, + -0.281773803718986, + -0.350688205542009, + 0.00926439950917379, + -0.0606203430263652, + -0.692877853693916, + 0.74540537410268, + -0.270031335722378, + -0.203138764532239, + -0.15323337408777, + -0.409819871770882, + 0.00551964384532583, + 0.126030314059844, + 0.392824993164884, + -0.295081063283455, + -0.117890035670334, + -0.496349161460881, + 0.382301638942813, + 0.198131706948678, + 1.15402992746392, + -1.43953821792422, + -0.0868359508719243, + 0.208577477553037, + 0.229263305354028, + -0.30075758611914, + 0.143039473245782, + 0.0211470078353996, + 0.249924460733069, + -0.288675278086085, + 0.608068182088283, + 0.499728763127281, + -0.879454393260001, + 0.379099544052794, + -0.692877853693916, + 0.543462508795494, + -0.288100328699206, + -0.726965963882802, + -4.74019958290912, + 0.772507542798904, + 0.371936377318189, + -0.383983983637458, + 0.500882044039476, + 0.281876795594467, + -0.384088458906589, + 0.690686154585239, + 0.184005796810196, + -1.41406256055309, + -0.367881487951687, + 0.225790906685194, + 1.18019248291288, + 0.905755320976345, + -0.855460554693407, + -0.327475066864525, + -0.171096014525521, + 0.942678630457975, + 0.0831926905123623, + -0.174694957239853, + -1.46565330556129, + 0.316131915240821, + 0.165541967050543, + 0.589058365479285, + 0.871460163183323, + 0.585369351445155, + 0.812551333291755, + -2.0054909561463, + 1.55024812793616, + -0.00317518449627846, + 0.263028179185804, + -0.314984706925257, + -0.117890035670334, + 0.136159502472656, + -0.345525271139816 + ] + } + }, + "label": "dyd14.ties", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/dydx14.binary.json b/tests/_r_cache/dydx14.binary.json new file mode 100644 index 00000000..d3f023ac --- /dev/null +++ b/tests/_r_cache/dydx14.binary.json @@ -0,0 +1,20 @@ +{ + "entries": { + "230df0059cc954c1c72e67b1afa992b10cdf780c67e54afc889824362ba9ee9b": { + "f": [ + 0.255019178460802, + 0.252009236414486, + 0.249675874074707 + ], + "o": 0.252406896316489, + "s": [ + -1.87038046991299e-16, + -0.0412429880777657, + 1.54306388767821e-15 + ] + } + }, + "label": "dydx14.binary", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/dydx14.character.json b/tests/_r_cache/dydx14.character.json new file mode 100644 index 00000000..eb0a7571 --- /dev/null +++ b/tests/_r_cache/dydx14.character.json @@ -0,0 +1,11 @@ +{ + "entries": { + "2c099c9bfed1781ebdf80b31b8851deaaa2555d1c347ec00dcba4987c6c26539": [ + 0.0012889304430775952, + 0.0012889304430775952 + ] + }, + "label": "dydx14.character", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/solve14.bits.json b/tests/_r_cache/solve14.bits.json new file mode 100644 index 00000000..08ec0811 --- /dev/null +++ b/tests/_r_cache/solve14.bits.json @@ -0,0 +1,51 @@ +{ + "entries": { + "0861010ccb87374a956cc7d49d9f6e23e33ef7763457e542e802e5a53eb8d480": [ + -2.41722366174625, + 3.2278452930023063 + ], + "0d992cfa7f89a27ccf30be59971881045ae7322a5ecd4b704f473994f956002f": [ + -1.3511514878749245, + 1.1573160117609773, + 0.7664557874153012, + -1.1803705722916031 + ], + "1c7fc20515544f06386b3bd829ea3df1bdd0b7b6abbfe36b2b28168eb6180046": [ + -0.4263432199103354, + 0.8046315734246174, + -0.08513247150976098 + ], + "475238249894eedb9d40ba074d8593e187f3973161e63855a5d6f4cd086912af": [ + 0.5332485027591887, + 0.5913035004642756, + -0.8760922583797311, + 0.15044658748224693, + 0.02802507086406799 + ], + "5d581280872ff5cba9a14f636db1be130b80b4b0a600ede3a623069c9d887ff3": [ + 1.7122713582736138, + -5.42930880540612, + 13.97212534527896, + -17.946735149109866, + 6.575549136611922 + ], + "66582dd351e8a27fc2a44db6cdb8b9bee52707408db4323da72dfd8350b5543f": [ + 9.370679468110199, + -23.104144514007643, + 17.064818288567302, + -2.882933295065863 + ], + "887026aad5f21ee9a8ceddf3663a82040cf9b55f3052620be19536176a519650": [ + 42.70234787162444, + -50.07473285636219, + 6.216594140408235 + ], + "94db17c60fa356495fbf869481a3278ed9f3a2b8214efb1a7925ce2e7325295e": [ + -5.249669818976545, + -1.0677129165522077 + ] + }, + "label": "solve14.bits", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/var14.bits.json b/tests/_r_cache/var14.bits.json new file mode 100644 index 00000000..bbc75387 --- /dev/null +++ b/tests/_r_cache/var14.bits.json @@ -0,0 +1,403 @@ +{ + "entries": { + "0d3ed9388a7b9d51a9fad73726339d4d312fe0665f772fea02f8e58b30feecbf": [ + 999978.6069746569, + 999984.1396222012, + 999988.677520988, + 999992.8696901904, + 999998.295111573, + 999999.9470945139, + 1000003.9581901922, + 1000007.4759393607, + 1000011.0445483231, + 1000018.0962465657, + 1000018.0962465657, + 1000013.5437466208, + 1000011.0445483231, + 1000007.4759393607, + 1000001.7739374066, + 999999.9470945139, + 999996.5642127047, + 999992.8696901904, + 999988.677520988, + 999978.6069746569 + ], + "16c8dc0353e2d71801530c8535c6bea1621ecde59795186854e6622558d2953e": [ + -17.743387776531293, + -13.205659364891735, + -9.190010458551155, + -6.4966639552978585, + -2.437167890924869, + -0.33159579658471267, + 4.391869609711151, + 7.745518818488526, + 11.176778038896387, + 16.607988507906523, + 16.607988507906523, + 13.008487679021009, + 11.176778038896387, + 7.745518818488527, + 2.363689570442354, + -0.3315957965847135, + -3.9978112007890583, + -6.496663955297861, + -9.190010458551136, + -17.743387776531293 + ], + "1f7da7269ce20bca3f4bb7a97ad9a5bae51c8134e7c44f4d5269eb023ed29c3c": [ + -17.743387776531293, + -11.078876704100983, + -8.197415709918982, + -5.354072857662565, + -1.8197283529332204, + 0.08428306362496363, + 3.646309388408791, + 6.39486906120935, + 9.634160908679473, + 16.607988507906523, + 16.607988507906523, + 11.61028137705334, + 9.634160908679473, + 6.39486906120935, + 2.1529678734785023, + 0.08428306362496363, + -3.100857828974595, + -5.354072857662566, + -8.197415709918978, + -17.743387776531293 + ], + "2d636e964e0087f151d7e12ea867ec913e1f50a508a09ddf5094f6e56703e389": [ + 999978.6069746569, + 999989.91015431, + 999991.9194702693, + 999995.0676952905, + 999998.2063885775, + 999999.6794510244, + 1000002.1113538251, + 1000003.9974747199, + 1000006.6636327823, + 1000018.0962465657, + 1000018.0962465657, + 1000008.3004735913, + 1000006.6636327823, + 1000003.9974747199, + 1000001.1215646237, + 999999.6794510244, + 999997.1571901054, + 999995.0676952905, + 999991.9194702693, + 999978.6069746569 + ], + "3c02408198d2b5f3ddfe0d4035ed342652202a03e30d575e16bcf27ab680365a": [ + -17.743387776531293, + -9.113276871749905, + -6.89761975510227, + -3.972316585488672, + -0.9894985245082226, + 0.4667642779286083, + 2.9683479150101295, + 4.953740992441573, + 7.6930537098145795, + 16.607988507906523, + 16.607988507906523, + 9.327299241603544, + 7.6930537098145795, + 4.953740992441573, + 1.9356371572473259, + 0.4667642779286083, + -2.0084807721826605, + -3.972316585488673, + -6.897619755102268, + -17.743387776531293 + ], + "54fa953318cb03a27eb332be95b138097eec1464d13c02ed412b42287816cef0": [ + -14.0, + -12.381359087954104, + -9.652914471411487, + -6.872256656423819, + -1.9490565566556404, + 0.48412121253596596, + 4.0779395384174615, + 7.0754006315669855, + 10.580470553368512, + 15.0, + 15.0, + 11.955028575374547, + 10.580470553368515, + 7.075400631566988, + 2.719765920278941, + 0.4841212125359655, + -3.7067658034603954, + -6.8722566564238186, + -9.652914471411483, + -14.0 + ], + "90cfa5095326dca20043518d7f0e394d24a2b3cfbe6120460d6bc45a4e19a9aa": [ + 999978.6069746569, + 999988.6142779796, + 999991.1043103302, + 999994.7154062558, + 999998.2775232727, + 999999.9872365335, + 1000002.7836540588, + 1000004.9103775583, + 1000007.9087955403, + 1000018.0962465657, + 1000018.0962465657, + 1000009.7528026296, + 1000007.9087955403, + 1000004.9103775583, + 1000001.6571047315, + 999999.9872365335, + 999997.0831107188, + 999994.7154062558, + 999991.1043103302, + 999978.6069746569 + ], + "91e4d348b017ce40d370d00dced52e3037cc0b3f2d8acf1d7b64213f66d36987": [ + 999978.6069746569, + 999987.6800628921, + 999990.5227900362, + 999994.4100012474, + 999998.2593048048, + 1000000.079798559, + 1000003.1141410957, + 1000005.4669136122, + 1000008.7250910787, + 1000018.0962465657, + 1000018.0962465657, + 1000010.6668567585, + 1000008.7250910787, + 1000005.4669136122, + 1000001.8757854658, + 1000000.079798559, + 999996.9804088486, + 999994.4100012474, + 999990.5227900362, + 999978.6069746569 + ], + "936d4f8480fec6e0e5efdffaea0b8fbb7879cc472fa40b39640e8b52b6535b4d": [ + 999978.6069746569, + 999986.3980890857, + 999989.6877817971, + 999993.9167855752, + 999998.2386155282, + 1000000.0776913842, + 1000003.4510357039, + 1000006.2113749376, + 1000009.8049931942, + 1000018.0962465657, + 1000018.0962465657, + 1000011.8941242532, + 1000009.8049931942, + 1000006.2113749376, + 1000001.9762823483, + 1000000.0776913842, + 999996.8312222672, + 999993.9167855752, + 999989.6877817971, + 999978.6069746569 + ], + "9670486e0db6a7c9b797816b7049bf7e96cd342b894f7e286c96e2562c62f68d": [ + -14.0, + -6.677228020723377, + -5.4079814036619025, + -3.2664608687089682, + -0.9518179619381688, + 0.1762608416139143, + 2.107070233479602, + 3.6595883758394856, + 5.84439675767137, + 15.0, + 15.0, + 7.141652195932472, + 5.84439675767137, + 3.6595883758394856, + 1.3092312225228249, + 0.1762608416139143, + -1.740631343066965, + -3.266460868708969, + -5.4079814036619025, + -14.0 + ], + "a7d933291fad84f29594aaffbf887d84a828c049dc6461ff60b8a1e5e95c2ce4": [ + -14.0, + -8.94881669246278, + -7.476168463480712, + -4.675261367606248, + -1.3782284261354796, + 0.23313573038074176, + 2.946763582726616, + 5.110461751290148, + 7.931080054170882, + 15.0, + 15.0, + 9.512784471057076, + 7.931080054170883, + 5.110461751290147, + 1.8283822539886885, + 0.23313573038074215, + -2.5181797419282423, + -4.67526136760625, + -7.476168463480716, + -14.0 + ], + "ac6ad57790ad78358fb0bf9e81739e8c7827257694e8f606fcb9f188c34a6cf1": [ + -17.743387776531293, + -6.890533384588963, + -5.379064279993677, + -3.000158507850418, + -0.6673068732242032, + 0.4091624891276384, + 2.198580152141539, + 3.6199322779647076, + 5.655804088139379, + 16.607988507906523, + 16.607988507906523, + 6.901734747746275, + 5.655804088139379, + 3.6199322779647076, + 1.4650315132270395, + 0.4091624891276384, + -1.440246264304724, + -3.0001585078504185, + -5.379064279993674, + -17.743387776531293 + ], + "ada6204466f82910d566934b24691ac722b315fdd1c733bca5e8e953c8ab859c": [ + -17.743387776531293, + -10.001215890389652, + -7.501609664555701, + -4.548359718157072, + -1.3134147157127543, + 0.33195592901606835, + 3.2430264451934723, + 5.532975115744763, + 8.546432215168693, + 16.607988507906523, + 16.607988507906523, + 10.327394780614709, + 8.546432215168686, + 5.532975115744762, + 2.035020694893261, + 0.3319559290160681, + -2.450068657465974, + -4.548359718157071, + -7.501609664555699, + -17.743387776531293 + ], + "b335cf5fa67353797101c1f463b8adea5cac86c54813ccaa6a608a8a1397f850": [ + -14.0, + -8.063058396567968, + -6.630325994331004, + -4.082293453866576, + -1.2106806821506275, + 0.19361450953034254, + 2.5889789137057146, + 4.504019297057913, + 7.087221193306858, + 15.0, + 15.0, + 8.550406249333449, + 7.087221193306858, + 4.504019297057913, + 1.598943574293416, + 0.19361450953034254, + -2.19650215760734, + -4.082293453866577, + -6.630325994331005, + -14.0 + ], + "cb002b539098202669ecc7db42df583d75333420c1e89365ad877de9dbcfe3b7": [ + -14.0, + -5.675723544332255, + -4.559191911517408, + -2.7140766296078604, + -0.7589554405669849, + 0.187206245225073, + 1.804410144139739, + 3.111930639851672, + 4.990272047800976, + 15.0, + 15.0, + 6.1321655913067765, + 4.990272047800976, + 3.111930639851672, + 1.1360759864486978, + 0.187206245225073, + -1.4222994425416902, + -2.7140766296078596, + -4.55919191151741, + -14.0 + ], + "e9378cb0c68dd3021fea2c1b8a9a483f1d173f0210c82a1acb328a896cb799ba": [ + -14.0, + -10.26171875, + -8.508241758241757, + -5.519230769230769, + -1.5997854077253213, + 0.31666666666666665, + 3.4329852045256746, + 5.920731707317074, + 8.925824175824175, + 15.0, + 15.0, + 10.665929203539822, + 8.925824175824175, + 5.920731707317074, + 2.167844522968197, + 0.31666666666666665, + -2.960515378221114, + -5.51923076923077, + -8.508241758241757, + -14.0 + ], + "eebf0e9b736142f80e6f124b2712225dd0cfe4f7c14a337f293ccf84fd62f40f": [ + 999978.6069746569, + 999990.8589018258, + 999992.5500717926, + 999995.2782543094, + 999998.0436652129, + 999999.3378292635, + 1000001.4943925051, + 1000003.2028887767, + 1000005.6390311122, + 1000018.0962465657, + 1000018.0962465657, + 1000007.1251253928, + 1000005.6390311122, + 1000003.2028887767, + 1000000.6105570615, + 999999.3378292635, + 999997.1197098123, + 999995.2782543094, + 999992.5500717926, + 999978.6069746569 + ], + "f720a2a2b022f7c8e81bfb533f2929b779c5bd4444068bdc38b13412427497d7": [ + -17.743387776531293, + -7.80761947873223, + -6.002749791585908, + -3.30260049708438, + -0.6930412898608902, + 0.5232949447308279, + 2.560976191288958, + 4.183592091316831, + 6.497919218285018, + 16.607988507906523, + 16.607988507906523, + 7.909616976792057, + 6.497919218285018, + 4.183592091316831, + 1.7239603611679517, + 0.5232949447308279, + -1.5612617571177572, + -3.3026004970843807, + -6.002749791585905, + -17.743387776531293 + ] + }, + "label": "var14.bits", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/var14.validation.json b/tests/_r_cache/var14.validation.json new file mode 100644 index 00000000..2205cd2a --- /dev/null +++ b/tests/_r_cache/var14.validation.json @@ -0,0 +1,45 @@ +{ + "entries": { + "4145d6b939976a2440cd174c85d4fbd2cdf1e0787ad566c8b409fe938f4024b2": { + "ens": [ + 10.4734832199263, + 11.6463527723908, + 11.6772352529123, + 9.0494285481699, + 10.3037122155878, + 10.3168645163238, + 27.0053746921231, + 27.0482453344961, + 27.045138332413 + ], + "horizon": 12.0, + "mode": "pipeline", + "origin": 48.0, + "targets": [ + 12.0, + 12.0, + 10.0 + ] + }, + "ffad2ce893a107f45ddb7639c6e0c291d48813b234e874e64cf083414b11d5ac": { + "ens": [ + 10.4734832199263, + 11.6463527723908, + 11.6772352529123, + 9.31792899658328, + 10.1436386608512, + 10.1994852923522, + 26.6488530552818, + 27.1806258824306, + 27.1548465954292 + ], + "horizon": 12.0, + "mode": "observed", + "origin": 48.0, + "targets": null + } + }, + "label": "var14.validation", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/invariants/test_arma.py b/tests/invariants/test_arma.py index 766cea2a..18e6c566 100644 --- a/tests/invariants/test_arma.py +++ b/tests/invariants/test_arma.py @@ -386,6 +386,7 @@ def test_nns_var_public_supported_paths_return_output_contract(dim_red_method: s "multivariate", "ensemble", "names", + "validation", } assert result["interpolated_and_extrapolated"].shape == variables.shape assert result["univariate"].shape == (3, 2) diff --git a/tests/invariants/test_core.py b/tests/invariants/test_core.py index d3581d9e..9f759cc0 100644 --- a/tests/invariants/test_core.py +++ b/tests/invariants/test_core.py @@ -117,23 +117,35 @@ def test_numpy_fallback_moments_are_blocked_over_targets_bit_identically( monkeypatch: pytest.MonkeyPatch, degree: float ) -> None: monkeypatch.setattr(core_module, "native_fn", lambda name: None) - monkeypatch.setattr(core_module, "_TARGET_BLOCK", 64) rng = np.random.default_rng(7) x = rng.normal(size=301) targets = np.concatenate([x, rng.normal(size=50)]) + + def moments() -> tuple[np.ndarray, np.ndarray, np.ndarray]: + return ( + np.asarray(lpm(degree, targets, x)), + np.asarray(upm(degree, targets, x)), + np.asarray(lpm_ratio(degree, targets, x)), + ) + + # Blocking over targets must not change a single bit: the fallback forms + # each target's moment with R's C++ arithmetic whatever the block size. + monkeypatch.setattr(core_module, "_TARGET_BLOCK", 64) + blocked = moments() + monkeypatch.setattr(core_module, "_TARGET_BLOCK", 10_000) + dense = moments() + for a, b in zip(blocked, dense, strict=True): + np.testing.assert_array_equal(a, b) + deviations = targets[:, np.newaxis] - x if degree == 0.0: - dense_lower = np.mean(deviations >= 0.0, axis=1) - dense_upper = np.mean(deviations < 0.0, axis=1) + textbook_lower = np.mean(deviations >= 0.0, axis=1) + textbook_upper = np.mean(deviations < 0.0, axis=1) else: - dense_lower = np.mean(np.maximum(0.0, deviations) ** degree, axis=1) - dense_upper = np.mean(np.maximum(0.0, -deviations) ** degree, axis=1) - - np.testing.assert_array_equal(lpm(degree, targets, x), dense_lower) - np.testing.assert_array_equal(upm(degree, targets, x), dense_upper) - if degree > 0.0: - dense_ratio = dense_lower / (dense_lower + dense_upper) - np.testing.assert_array_equal(lpm_ratio(degree, targets, x), dense_ratio) + textbook_lower = np.mean(np.maximum(0.0, deviations) ** degree, axis=1) + textbook_upper = np.mean(np.maximum(0.0, -deviations) ** degree, axis=1) + np.testing.assert_allclose(dense[0], textbook_lower, rtol=1e-13, atol=0) + np.testing.assert_allclose(dense[1], textbook_upper, rtol=1e-13, atol=0) def test_numpy_fallback_cdf_does_not_allocate_an_n_by_n_matrix( diff --git a/tests/invariants/test_var.py b/tests/invariants/test_var.py index 010281b7..50c9448c 100644 --- a/tests/invariants/test_var.py +++ b/tests/invariants/test_var.py @@ -173,7 +173,7 @@ def test_nns_var_multivariate_is_nns_stack_on_exactly_the_relevant_variables( ) h = 3 - result = nns_var(variables, h, tau=tau, validation="conditional") + result = nns_var(variables, h, tau=tau, validation="observed") names = result["names"] relevant = result["relevant_variables"] diff --git a/tests/parity/test_derivatives_var_r14.py b/tests/parity/test_derivatives_var_r14.py new file mode 100644 index 00000000..285aa6b3 --- /dev/null +++ b/tests/parity/test_derivatives_var_r14.py @@ -0,0 +1,205 @@ +"""R 14.0 parity for numerical derivatives, partial-moment VaR and NNS.VAR contracts. + +The derivative routines and the degree-1 VaR inversion feed exact comparisons +(root brackets, stencil half-widths, tied rows), so these checks follow R's own +arithmetic: R's ``mean()``/``sum()``/``cumsum()``/``colMeans()`` accumulate in +long double, R's ``x^2`` is ``x * x``, ``solve()`` is reference LAPACK ``dgesv`` +and ``round()`` is nmath ``fround``. Test functions passed to ``NNS.diff`` are +written so that R and Python evaluate them with the same arithmetic (``v * v`` +rather than ``v ** 2``, which calls the C ``pow``). +""" + +from __future__ import annotations + +import cmath +import math +import warnings +from collections.abc import Callable +from typing import Any + +import numpy as np +import pytest +from _r import nns_frame_call, r_column +from _tolerances import COMPOUND + +import nns +from nns._rblas import r_solve + +pytestmark = pytest.mark.parity + + +def _num(values: Any) -> dict[str, Any]: + return r_column([float(v) for v in np.asarray(values, dtype=np.float64).ravel()], "numeric") + + +def _complex_aware(real_fn: Callable[[float], float], complex_fn: Callable[[complex], complex]): + return lambda v: complex_fn(v) if isinstance(v, complex) else real_fn(v) + + +_FUNCTIONS = { + "poly": ("function(v) 3*v^2 - 2*v + 1", lambda v: 3 * (v * v) - 2 * v + 1), + "cube": ("function(v) v^3", lambda v: v**3), + "exp": ( + "function(v) exp(v / 2)", + _complex_aware(lambda v: math.exp(v / 2), lambda v: cmath.exp(v / 2)), + ), + "sin": ("function(v) sin(v)", _complex_aware(math.sin, cmath.sin)), + "log": ("function(v) log(v)", _complex_aware(math.log, cmath.log)), +} + + +@pytest.mark.parametrize("name", sorted(_FUNCTIONS)) +@pytest.mark.parametrize( + ("point", "h", "tol", "digits"), + [(1.5, None, 1e-10, 12), (3.0, 0.5, 1e-8, 8), (0.5, None, 1e-6, 6)], +) +def test_nns_diff_matches_r_exactly( + name: str, point: float, h: float | None, tol: float, digits: int +) -> None: + r_fn, py_fn = _FUNCTIONS[name] + h_arg = "" if h is None else f", h = {h!r}" + expected = nns_frame_call( + "diff14.full", + f"res <- NNS.diff({r_fn}, {point!r}{h_arg}, tol = {tol!r}, digits = {digits}); " + "unname(as.numeric(res[, 1]))", + {}, + exact=True, + ) + actual = nns.nns_diff(py_fn, point, h=h, tol=tol, digits=digits) + values = np.array(list(actual.values()), dtype=np.float64) + np.testing.assert_array_equal(values, np.asarray(expected, dtype=np.float64)) + + +def test_dy_dx_any_character_eval_point_is_the_overall_gradient() -> None: + rng = np.random.default_rng(51) + x = np.sort(rng.uniform(-3, 3, 120)) + y = np.sin(2 * x) + expected = nns_frame_call( + "dydx14.character", "c(dy.dx(x, y, eval.point = 'obs'), dy.dx(x, y, 'overall'))", + {"x": _num(x), "y": _num(y)}, exact=True, + ) + actual = [nns.dy_dx(x, y, eval_point="obs"), nns.dy_dx(x, y, eval_point="overall")] + np.testing.assert_array_equal(actual, np.asarray(expected, dtype=np.float64)) + + +def test_dy_dx_two_valued_response_is_a_regression_as_in_r() -> None: + rng = np.random.default_rng(52) + x = np.sort(rng.uniform(0, 4, 90)) + y = (x + rng.normal(size=90) * 0.5 > 2).astype(float) + expected = nns_frame_call( + "dydx14.binary", + "r <- dy.dx(x, y, eval.point = c(1, 2, 3)); " + "list(o = dy.dx(x, y, 'overall'), f = r$first.derivative, s = r$second.derivative)", + {"x": _num(x), "y": _num(y)}, + ) + assert nns.dy_dx(x, y, eval_point="overall") == pytest.approx( + float(np.asarray(expected["o"])), abs=COMPOUND + ) + actual = nns.dy_dx(x, y, eval_point=np.array([1.0, 2.0, 3.0])) + np.testing.assert_allclose(actual["first.derivative"], expected["f"], rtol=0, atol=COMPOUND) + np.testing.assert_allclose(actual["second.derivative"], expected["s"], rtol=0, atol=COMPOUND) + + +@pytest.mark.parametrize("wrt", [1, 2]) +def test_dy_d_on_tied_coordinates_matches_r(wrt: int) -> None: + """Stencil half-widths come from LPM.VaR(., 1, x); a few-ulp difference in + that inversion moved the nodes and changed the derivative at tied rows.""" + rng = np.random.default_rng(53) + x = np.round(rng.normal(size=(200, 2)), 1) + y = x[:, 0] + x[:, 1] ** 2 + expected = nns_frame_call( + "dyd14.ties", + f"res <- dy.d_(matrix(v, ncol = 2), y, wrt = {wrt}, eval.points = 'obs', " + "messages = FALSE); list(f = res['First', 1][[1]], s = res['Second', 1][[1]])", + {"v": _num(x.ravel(order="F")), "y": _num(y)}, + ) + actual = nns.dy_d(x, y, wrt=wrt, eval_points="obs", messages=False) + np.testing.assert_allclose(actual["First"], expected["f"], rtol=0, atol=COMPOUND) + np.testing.assert_allclose(actual["Second"], expected["s"], rtol=0, atol=COMPOUND) + + +@pytest.mark.parametrize("degree", [1, 2, 3, 4, 0.5, 1.5]) +def test_partial_moment_var_matches_r_bitwise(degree: float) -> None: + rng = np.random.default_rng(54) + probs = [0.0, 0.01, 0.025, 0.1, 0.33, 0.5, 0.77, 0.9, 0.975, 1.0] + for shift, ties in ((0.0, False), (0.0, True), (1e6, False)): + x = rng.normal(size=60) * 7 + shift + if ties: + x = np.round(x) + expected = nns_frame_call( + "var14.bits", + f"c(LPM.VaR(p, {degree!r}, x), UPM.VaR(p, {degree!r}, x))", + {"x": _num(x), "p": _num(probs)}, + exact=True, + ) + actual = np.concatenate( + [nns.lpm_var(np.array(probs), degree, x), nns.upm_var(np.array(probs), degree, x)] + ) + np.testing.assert_array_equal(actual, np.asarray(expected, dtype=np.float64)) + + +def test_solve_matches_r_bitwise() -> None: + rng = np.random.default_rng(55) + for n in (2, 3, 4, 5): + for vandermonde in (False, True): + if vandermonde: + z = np.sort(rng.uniform(-1, 1, n)) + a = np.vander(z, n, increasing=True).T + else: + a = rng.normal(size=(n, n)) + b = rng.normal(size=n) + expected = nns_frame_call( + "solve14.bits", + f"as.numeric(solve(matrix(a, {n}), b))", + {"a": _num(a.ravel(order="F")), "b": _num(b)}, + exact=True, + ) + np.testing.assert_array_equal(r_solve(a, b), np.asarray(expected, dtype=np.float64)) + + +def _panel(seed: int, n: int = 60) -> np.ndarray: + rng = np.random.default_rng(seed) + t = np.arange(1, n + 1) + a = np.sin(2 * np.pi * t / 12) * 2 + rng.normal(size=n) * 0.3 + 10 + b = 0.6 * np.r_[a[0], a[:-1]] + rng.normal(size=n) * 0.3 + 3 + c = np.cumsum(rng.normal(size=n)) + 20 + x = np.column_stack([a, b, c]) + x[-2:, 2] = np.nan + return x + + +@pytest.mark.parametrize("validation", ["pipeline", "observed"]) +def test_nns_var_validation_modes_and_metadata_follow_r(validation: str) -> None: + import pandas as pd + + x = _panel(56) + flat = x.ravel(order="F") + data = {"v": _num(np.nan_to_num(flat)), "na": _num(np.flatnonzero(np.isnan(flat)) + 1)} + expected = nns_frame_call( + "var14.validation", + "M <- matrix(v, ncol = 3); M[na] <- NA; X <- as.data.frame(M); " + "colnames(X) <- c('A', 'B', 'C'); " + f"r <- NNS.VAR(X, h = 3, tau = 1, status = FALSE, ncores = 1, " + f"validation = '{validation}'); list(ens = as.numeric(as.matrix(r$ensemble)), " + "mode = r$validation$mode, origin = r$validation$origin, " + "horizon = r$validation$horizon, " + "targets = if (is.null(r$validation$observed.targets)) NULL " + "else unname(r$validation$observed.targets))", + data, + ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + actual = nns.nns_var( + pd.DataFrame(x, columns=list("ABC")), 3, tau=1, ncores=1, validation=validation + ) + np.testing.assert_allclose( + np.asarray(actual["ensemble"]).ravel(order="F"), expected["ens"], rtol=0, atol=COMPOUND + ) + meta = actual["validation"] + assert meta["mode"] == expected["mode"] + assert meta["origin"] == float(np.asarray(expected["origin"])) + assert meta["horizon"] == float(np.asarray(expected["horizon"])) + if expected["targets"] is None: + assert meta["observed.targets"] is None + else: + assert list(meta["observed.targets"].values()) == list(np.asarray(expected["targets"])) diff --git a/tests/parity/test_var.py b/tests/parity/test_var.py index 9305fa7b..749c7d62 100644 --- a/tests/parity/test_var.py +++ b/tests/parity/test_var.py @@ -380,6 +380,7 @@ def test_public_nns_var_cor_matches_r( "multivariate", "ensemble", "names", + "validation", } assert actual_result["names"] == expected_result["relevant_names"] for key in ("interpolated_and_extrapolated", "univariate", "multivariate", "ensemble"): @@ -440,6 +441,7 @@ def test_public_nns_var_nns_dep_matches_r() -> None: "multivariate", "ensemble", "names", + "validation", } assert actual_result["names"] == expected_result["relevant_names"] for key in ("interpolated_and_extrapolated", "univariate", "multivariate", "ensemble"): @@ -472,6 +474,7 @@ def test_public_nns_var_nns_caus_matches_r() -> None: "multivariate", "ensemble", "names", + "validation", } assert actual_result["names"] == expected_result["relevant_names"] for key in ("interpolated_and_extrapolated", "univariate", "multivariate", "ensemble"): @@ -504,6 +507,7 @@ def test_public_nns_var_all_matches_r() -> None: "multivariate", "ensemble", "names", + "validation", } assert actual_result["names"] == expected_result["relevant_names"] for key in ("interpolated_and_extrapolated", "univariate", "multivariate", "ensemble"): From d279d7230af1176947aa928e35914d0750c97429 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 15:24:29 +0000 Subject: [PATCH 04/16] Match R's arithmetic in NNS.ANOVA The certainty, grand statistic, VaR targets and bootstrap effect-size summaries now use R's own mean(), median(), colMeans() and sum() (long-double accumulation) and R's x^2 (x * x), so binary, multi-group, pairwise and robust results agree with R 14.0 bit for bit. The robust-bounds property assumed the gravity estimate lies between the bootstrap quantiles; R does not confine it there (documented counterexample), so the property now checks only that each value is a probability. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt --- src/nns/anova.py | 93 +++++++++----- tests/_r_cache/anova14.binary.json | 199 +++++++++++++++++++++++++++++ tests/_r_cache/anova14.multi.json | 45 +++++++ tests/parity/test_anova_r14.py | 93 ++++++++++++++ tests/property/test_anova.py | 8 +- 5 files changed, 401 insertions(+), 37 deletions(-) create mode 100644 tests/_r_cache/anova14.binary.json create mode 100644 tests/_r_cache/anova14.multi.json create mode 100644 tests/parity/test_anova_r14.py diff --git a/src/nns/anova.py b/src/nns/anova.py index 3cd365d9..1c5bb6aa 100644 --- a/src/nns/anova.py +++ b/src/nns/anova.py @@ -7,7 +7,7 @@ import numpy as np from numpy.typing import NDArray -from nns._rmean import r_mean +from nns._rmean import r_col_means, r_mean, r_mean_logical, r_median, r_sum from nns._rrng import RRNG from nns.central_tendencies import nns_gravity from nns.core import lpm_ratio, upm_ratio @@ -107,7 +107,7 @@ def _nns_anova( raise ValueError("supply both control and treatment or at least two control groups.") pooled = np.concatenate(groups) - grand = float(np.median(pooled)) if medians else r_mean(pooled) + grand = r_median(pooled) if medians else r_mean(pooled) if pairwise: out = np.full((len(groups), len(groups)), np.nan, dtype=np.float64) @@ -126,10 +126,11 @@ def _nns_anova( out[j, i] = certainty return out - upper_25 = float(np.mean([upm_var(0.25, 1, group) for group in groups])) - lower_25 = float(np.mean([lpm_var(0.25, 1, group) for group in groups])) - upper_125 = float(np.mean([upm_var(0.125, 1, group) for group in groups])) - lower_125 = float(np.mean([lpm_var(0.125, 1, group) for group in groups])) + # R: mean(sapply(groups, VaR)) with R's mean(). + upper_25 = r_mean(np.array([upm_var(0.25, 1, group) for group in groups])) + lower_25 = r_mean(np.array([lpm_var(0.25, 1, group) for group in groups])) + upper_125 = r_mean(np.array([upm_var(0.125, 1, group) for group in groups])) + lower_125 = r_mean(np.array([lpm_var(0.125, 1, group) for group in groups])) certainties = [ _anova_bin( @@ -148,7 +149,7 @@ def _nns_anova( for i in range(len(groups) - 1) for j in range(i + 1, len(groups)) ] - return {"Certainty": float(np.mean(certainties))} + return {"Certainty": r_mean(np.asarray(certainties, dtype=np.float64))} def _anova_bin( @@ -168,10 +169,10 @@ def _anova_bin( rng: RRNG | None = None, ) -> AnovaResult: if mean_of_means is None: - control_stat = float(np.median(control) if medians else np.mean(control)) - treatment_stat = float(np.median(treatment) if medians else np.mean(treatment)) + control_stat = r_median(control) if medians else r_mean(control) + treatment_stat = r_median(treatment) if medians else r_mean(treatment) if medians: - mean_of_means = float(np.median(np.concatenate((control, treatment)))) + mean_of_means = r_median(np.concatenate((control, treatment))) elif ( control.size > 0 and treatment.size > 0 @@ -187,18 +188,15 @@ def _anova_bin( control.size + treatment.size ) else: - control_stat = float(np.median(control) if medians else np.mean(control)) - treatment_stat = float(np.median(treatment) if medians else np.mean(treatment)) + control_stat = r_median(control) if medians else r_mean(control) + treatment_stat = r_median(treatment) if medians else r_mean(treatment) if upper_25_target is None or lower_25_target is None: - upper_25_target = float(np.mean([upm_var(0.25, 1, control), upm_var(0.25, 1, treatment)])) - lower_25_target = float(np.mean([lpm_var(0.25, 1, control), lpm_var(0.25, 1, treatment)])) - upper_125_target = float( - np.mean([upm_var(0.125, 1, control), upm_var(0.125, 1, treatment)]) - ) - lower_125_target = float( - np.mean([lpm_var(0.125, 1, control), lpm_var(0.125, 1, treatment)]) - ) + # R: mean(c(UPM.VaR(., 1, control), UPM.VaR(., 1, treatment))), etc. + upper_25_target = _mean2(upm_var(0.25, 1, control), upm_var(0.25, 1, treatment)) + lower_25_target = _mean2(lpm_var(0.25, 1, control), lpm_var(0.25, 1, treatment)) + upper_125_target = _mean2(upm_var(0.125, 1, control), upm_var(0.125, 1, treatment)) + lower_125_target = _mean2(lpm_var(0.125, 1, control), lpm_var(0.125, 1, treatment)) assert upper_25_target is not None assert lower_25_target is not None assert upper_125_target is not None @@ -237,10 +235,10 @@ def area_distance(r1: float, r2: float, target: float, p: float) -> float: if medians: median_distance_1 = max( - 0.0, float(np.mean(control < mean_of_means)) - 0.5, 0.5 - lpm_ratio_1 + 0.0, r_mean_logical(control < mean_of_means) - 0.5, 0.5 - lpm_ratio_1 ) median_distance_2 = max( - 0.0, float(np.mean(treatment < mean_of_means)) - 0.5, 0.5 - lpm_ratio_2 + 0.0, r_mean_logical(treatment < mean_of_means) - 0.5, 0.5 - lpm_ratio_2 ) mad_cdf = max(median_distance_1, median_distance_2) else: @@ -250,18 +248,27 @@ def area_distance(r1: float, r2: float, target: float, p: float) -> float: upper_125_cdf = area_distance(upper_125_ratio_1, upper_125_ratio_2, upper_125_target, 0.125) lower_125_cdf = area_distance(lower_125_ratio_1, lower_125_ratio_2, lower_125_target, 0.125) + # R squares with x * x and adds the five terms with sum(), which + # accumulates in long double. if means_only: - rho = ((0.5 - mad_cdf) ** 2) / 0.25 + rho = _sq(0.5 - mad_cdf) / 0.25 else: rho = ( - ((0.5 - mad_cdf) ** 2) / 0.25 - + 0.5 * (((0.25 - upper_25_cdf) ** 2) / (0.25**2)) - + 0.5 * (((0.25 - lower_25_cdf) ** 2) / (0.25**2)) - + 0.25 * (((0.125 - upper_125_cdf) ** 2) / (0.125**2)) - + 0.25 * (((0.125 - lower_125_cdf) ** 2) / (0.125**2)) - ) / 2.5 - - pop_adjustment = ((control.size + treatment.size - 2) / (control.size + treatment.size)) ** 2 + r_sum( + np.array( + [ + _sq(0.5 - mad_cdf) / 0.25, + 0.5 * (_sq(0.25 - upper_25_cdf) / _sq(0.25)), + 0.5 * (_sq(0.25 - lower_25_cdf) / _sq(0.25)), + 0.25 * (_sq(0.125 - upper_125_cdf) / _sq(0.125)), + 0.25 * (_sq(0.125 - lower_125_cdf) / _sq(0.125)), + ] + ) + ) + / 2.5 + ) + + pop_adjustment = _sq((control.size + treatment.size - 2) / (control.size + treatment.size)) result = { "Control": control_stat, "Treatment": treatment_stat, @@ -367,10 +374,9 @@ def _effect_size_bounds( # i.e. column j holds draws j * n .. (j + 1) * n - 1; row j here. control_boot = control[_r_boot_indices(rng, control.size, n_boot)] treatment_boot = treatment[_r_boot_indices(rng, treatment.size, n_boot)] - control_stats = np.median(control_boot, axis=1) if medians else np.mean(control_boot, axis=1) - treatment_stats = ( - np.median(treatment_boot, axis=1) if medians else np.mean(treatment_boot, axis=1) - ) + # R: apply(boot, 2, median) or colMeans(boot) (long-double accumulation). + control_stats = _r_column_stats(control_boot, medians) + treatment_stats = _r_column_stats(treatment_boot, medians) alpha = _ci_alpha(confidence_interval, tails) # Every tail choice needs both quantiles of both groups (R used @@ -453,3 +459,20 @@ def _tail(value: str) -> Tail: if tail not in {"left", "right", "both"}: raise ValueError("tails must be 'left', 'right', or 'both'.") return tail # type: ignore[return-value] + + +def _r_column_stats(boot: NDArray[np.float64], medians: bool) -> NDArray[np.float64]: + """Each bootstrap replicate's (row's) R median or R mean.""" + if medians: + return np.array([r_median(row) for row in boot], dtype=np.float64) + return r_col_means(boot.T) + + +def _mean2(a: float | NDArray[np.float64], b: float | NDArray[np.float64]) -> float: + """R's ``mean(c(a, b))`` of two scalars.""" + return r_mean(np.array([float(np.asarray(a)), float(np.asarray(b))])) + + +def _sq(value: float) -> float: + """R's ``value^2``, which R evaluates as ``value * value``.""" + return value * value diff --git a/tests/_r_cache/anova14.binary.json b/tests/_r_cache/anova14.binary.json new file mode 100644 index 00000000..3867d1cd --- /dev/null +++ b/tests/_r_cache/anova14.binary.json @@ -0,0 +1,199 @@ +{ + "entries": { + "1bc0c01b254e4d3b4370eed30a5d0f524503d2d1119c866d62b474bf80d7a51d": { + "names": [ + "Control", + "Treatment", + "Grand_Statistic", + "Control_CDF", + "Treatment_CDF", + "Certainty", + "Effect_Size_LB.2.5%", + "Effect_Size_UB.97.5%", + "Confidence_Level" + ], + "values": [ + -0.11666666666666667, + 0.4222222222222222, + 0.11428571428571428, + 0.6386506575185819, + 0.29220222793487555, + 0.3286418665161736, + 0.005138888888889276, + 1.0726388888888885, + 0.95 + ] + }, + "50dd16ad405142cb3396eb5bb6b26c28c2740faffaffab1a68e118576b74cb00": { + "names": [ + "Control", + "Treatment", + "Grand_Statistic", + "Control_CDF", + "Treatment_CDF", + "Certainty", + "Effect_Size_LB.2.5%", + "Effect_Size_UB.97.5%", + "Confidence_Level" + ], + "values": [ + 0.9005068341976756, + 0.9761374000055926, + 0.9322434057545756, + 0.5166666666666667, + 0.4888888888888889, + 0.4735608642161154, + -0.31033742607454684, + 0.8906599261783561, + 0.95 + ] + }, + "5e95d2a8283ebd46ef6bc4bf263ba1d6a944dd2d9eb81c4c2a9dba4d63ca6f62": { + "names": [ + "Control", + "Treatment", + "Grand_Statistic", + "Control_CDF", + "Treatment_CDF", + "Certainty", + "Effect_Size_LB.2.5%", + "Effect_Size_UB.97.5%", + "Confidence_Level" + ], + "values": [ + 0.9005068341976756, + 0.9761374000055926, + 0.9322434057545756, + 0.5166666666666667, + 0.4888888888888889, + 0.9199753198398699, + -0.31033742607454684, + 0.8906599261783561, + 0.95 + ] + }, + "5fbe70cbb96737a4001e278230f350b7fc462b89bb3b79dafa4411036da4c0a8": { + "names": [ + "Control", + "Treatment", + "Grand_Statistic", + "Control_CDF", + "Treatment_CDF", + "Certainty", + "Effect_Size_LB.2.5%", + "Effect_Size_UB.97.5%", + "Confidence_Level" + ], + "values": [ + 1.0735808595915728, + 1.6486449495576585, + 1.3200368981484665, + 0.6419592991412439, + 0.3602957115942067, + 0.30294050449254184, + -0.16458182023784063, + 1.3862506906093133, + 0.95 + ] + }, + "8689c19dd2a8afb0375baf508e55a9d75b5613f9a4b54b93683fba1fb62ab9e6": { + "names": [ + "Control", + "Treatment", + "Grand_Statistic", + "Control_CDF", + "Treatment_CDF", + "Certainty", + "Effect_Size_LB.2.5%", + "Effect_Size_UB.97.5%", + "Confidence_Level" + ], + "values": [ + 0.0, + 0.0, + 0.0, + 0.7166666666666667, + 0.5333333333333333, + 0.4508795035039696, + 0.0, + 1.0, + 0.95 + ] + }, + "c8869e871c4eed75b91949895e4cddd707257e1563b38a5d93b1b4b045088844": { + "names": [ + "Control", + "Treatment", + "Grand_Statistic", + "Control_CDF", + "Treatment_CDF", + "Certainty", + "Effect_Size_LB.2.5%", + "Effect_Size_UB.97.5%", + "Confidence_Level" + ], + "values": [ + 1.0735808595915728, + 1.6486449495576585, + 1.3200368981484665, + 0.6419592991412439, + 0.3602957115942067, + 0.4934244205309361, + -0.16458182023784063, + 1.3862506906093133, + 0.95 + ] + }, + "cad09aa967fd6a4b701a8388a2678ab88615cb5b7bfd977926827ca6b7ea23fa": { + "names": [ + "Control", + "Treatment", + "Grand_Statistic", + "Control_CDF", + "Treatment_CDF", + "Certainty", + "Effect_Size_LB.2.5%", + "Effect_Size_UB.97.5%", + "Confidence_Level" + ], + "values": [ + -0.11666666666666667, + 0.4222222222222222, + 0.11428571428571428, + 0.6386506575185819, + 0.29220222793487555, + 0.19742922063198104, + 0.005138888888889276, + 1.0726388888888885, + 0.95 + ] + }, + "e51f72ffd395db5ebc08651a9739b840f6ca8432fccc8a66db8ba45ef92372c6": { + "names": [ + "Control", + "Treatment", + "Grand_Statistic", + "Control_CDF", + "Treatment_CDF", + "Certainty", + "Effect_Size_LB.2.5%", + "Effect_Size_UB.97.5%", + "Confidence_Level" + ], + "values": [ + 0.0, + 0.0, + 0.0, + 0.7166666666666667, + 0.5333333333333333, + 0.962267573696145, + 0.0, + 1.0, + 0.95 + ] + } + }, + "label": "anova14.binary", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/anova14.multi.json b/tests/_r_cache/anova14.multi.json new file mode 100644 index 00000000..70cb2789 --- /dev/null +++ b/tests/_r_cache/anova14.multi.json @@ -0,0 +1,45 @@ +{ + "entries": { + "eaba3de2731ce936baea03754b6ad361bdabda1dfffe04367d4b39b9400392f7": [ + 0.17300383449692144, + 1.0, + 0.25799310216239224, + 0.12152384840602148, + 0.07089103163904956, + 0.25799310216239224, + 1.0, + 0.47197490799680075, + 0.34505360834264104, + 0.12152384840602148, + 0.47197490799680075, + 1.0, + 0.6444687403584645, + 0.07089103163904956, + 0.34505360834264104, + 0.6444687403584645, + 1.0 + ], + "f15019261a73b16297b8055e5b2186030910c287d57e1bf7952d3bb0b7f034da": [ + 0.2086236316638183, + 1.0, + 0.23804746176516114, + 0.1305612769945813, + 0.10208110969655668, + 0.23804746176516114, + 1.0, + 0.49589567503555665, + 0.4518769322557043, + 0.1305612769945813, + 0.49589567503555665, + 1.0, + 0.611375556159874, + 0.10208110969655668, + 0.4518769322557043, + 0.611375556159874, + 1.0 + ] + }, + "label": "anova14.multi", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/parity/test_anova_r14.py b/tests/parity/test_anova_r14.py new file mode 100644 index 00000000..10fe30e2 --- /dev/null +++ b/tests/parity/test_anova_r14.py @@ -0,0 +1,93 @@ +"""R 14.0 parity for NNS.ANOVA, down to R's own summary arithmetic. + +The certainty compares each group's mean (or median) with the grand statistic +and with partial-moment VaR targets that R averages with ``mean()``, which +accumulates in long double; the effect-size bounds are quantiles of bootstrap +replicates drawn with R's ``sample()`` and summarised with ``colMeans()`` or +``median()``. The port follows that arithmetic, so the results agree with R to +the last bit. +""" + +from __future__ import annotations + +import re +from typing import Any + +import numpy as np +import pytest +from _r import nns_frame_call, r_column + +import nns + +pytestmark = pytest.mark.parity + +def _num(values: Any) -> dict[str, Any]: + return r_column([float(v) for v in np.asarray(values, dtype=np.float64)], "numeric") + + +def _named(expected: Any) -> dict[str, float]: + # unlist() appends the quantile labels R's quantile() gives the effect-size + # bounds ("Effect_Size_LB.2.5%"); the port names the bound alone. + names = [re.sub(r"\.\d+(\.\d+)?%$", "", name) for name in expected["names"]] + return dict(zip(names, (float(v) for v in expected["values"]), strict=True)) + + +def _groups(kind: str, seed: int) -> tuple[np.ndarray, np.ndarray]: + rng = np.random.default_rng(seed) + if kind == "ties": + return np.round(rng.normal(size=60)), np.round(rng.normal(size=45) + 0.3) + return rng.exponential(size=60), rng.exponential(size=45) * 1.5 + + +@pytest.mark.parametrize("kind", ["ties", "skew"]) +@pytest.mark.parametrize( + ("means_only", "medians"), [(False, False), (True, False), (False, True), (True, True)] +) +def test_nns_anova_binary_with_effect_sizes_matches_r( + kind: str, means_only: bool, medians: bool +) -> None: + control, treatment = _groups(kind, 61) + flag = {True: "TRUE", False: "FALSE"} + expected = nns_frame_call( + "anova14.binary", + f"set.seed(7); unlist(NNS.ANOVA(a, b, means.only = {flag[means_only]}, " + f"medians = {flag[medians]}, confidence.interval = 0.95, tails = 'Both', " + "plot = FALSE))", + {"a": _num(control), "b": _num(treatment)}, + exact=True, + ) + actual = nns.nns_anova( + control, + treatment, + means_only=means_only, + medians=medians, + confidence_interval=0.95, + tails="Both", + random_seed=7, + ) + assert isinstance(actual, dict) + want = _named(expected) + assert set(actual) == set(want) + for key, value in want.items(): + assert actual[key] == value, key + + +@pytest.mark.parametrize("medians", [False, True]) +def test_nns_anova_multi_group_certainty_matches_r(medians: bool) -> None: + rng = np.random.default_rng(62) + groups = [rng.normal(size=size) + 0.2 * i for i, size in enumerate((23, 31, 17, 40))] + flag = "TRUE" if medians else "FALSE" + data = {f"g{i}": _num(group) for i, group in enumerate(groups)} + expected = nns_frame_call( + "anova14.multi", + "G <- list(g0, g1, g2, g3); " + f"c(unname(NNS.ANOVA(G, medians = {flag}, plot = FALSE)['Certainty']), " + f"as.numeric(NNS.ANOVA(G, medians = {flag}, pairwise = TRUE, plot = FALSE)))", + data, + exact=True, + ) + certainty = nns.nns_anova(groups, medians=medians) + pairwise = nns.nns_anova(groups, medians=medians, pairwise=True) + assert isinstance(certainty, dict) + actual = np.concatenate([[certainty["Certainty"]], np.asarray(pairwise).ravel(order="F")]) + np.testing.assert_array_equal(actual, np.asarray(expected, dtype=np.float64)) diff --git a/tests/property/test_anova.py b/tests/property/test_anova.py index 156ef931..9cf1e93b 100644 --- a/tests/property/test_anova.py +++ b/tests/property/test_anova.py @@ -99,5 +99,9 @@ def test_nns_anova_robust_bounds(x: np.ndarray, y: np.ndarray) -> None: result = nns_anova(x, y, robust=True, confidence_interval=0.95, random_seed=123) assert isinstance(result, dict) - assert 0.0 <= result["Lower 95% CI"] <= result["Robust Certainty Estimate"] <= 1.0 - assert result["Robust Certainty Estimate"] <= result["Upper 95% CI"] <= 1.0 + # The bounds are quantiles of the bootstrap certainties and the estimate is + # their NNS.gravity, which R does not confine to the interval: for + # x = (-1, 2, ..., 2), y = (-1, 0, ..., 0) and set.seed(123), R returns an + # estimate of 1.47e-05 with both bounds at 0. + assert 0.0 <= result["Lower 95% CI"] <= result["Upper 95% CI"] <= 1.0 + assert 0.0 <= result["Robust Certainty Estimate"] <= 1.0 From eb1a9582f11ab5324a1ccfacebdff4c4c1d9f7b0 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 15:36:21 +0000 Subject: [PATCH 05/16] Match R's stochastic-dominance contracts and efficient-set order - The efficient set lists columns by LPM(degree, max, column) with R's C++ arithmetic (repeated multiplication, row-order sum), so columns whose keys tie exactly (permutations, duplicates) keep R's index tie break. - The .uni tests, efficient set and SD clustering accept infinite values as R's C++ does: an infinite pair is compared unshifted, std::min/std::max NaN semantics are reproduced, and intervals ending at +Inf are skipped. NNS.FSD/SSD/TSD still reject them with R's LPM messages, variable first. - Inputs are validated in R's order with R's messages (missing values, empty samples, then degree in the efficient set); a vector is one column. - NNS.SD.cluster validates in its first efficient-set search, so up to min_cluster columns come back unchecked, and min_cluster is a real number. Live sweeps against R 14.0: 2388 pairwise/efficient-set/cluster/dendrogram cases and 2778 infinite-value and validation cases, all identical. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt --- docs/conventions.md | 12 + src/nns/stochastic_dominance.py | 277 +++++++++++++----- tests/_r_cache/sd14.cluster_min.json | 29 ++ tests/_r_cache/sd14.efficient_order.json | 23 ++ tests/_r_cache/sd14.matrix_infinite.json | 66 +++++ tests/_r_cache/sd14.uni_infinite.json | 37 +++ tests/parity/test_sd_cluster.py | 2 +- tests/parity/test_stochastic_dominance_r14.py | 170 +++++++++++ 8 files changed, 536 insertions(+), 80 deletions(-) create mode 100644 tests/_r_cache/sd14.cluster_min.json create mode 100644 tests/_r_cache/sd14.efficient_order.json create mode 100644 tests/_r_cache/sd14.matrix_infinite.json create mode 100644 tests/_r_cache/sd14.uni_infinite.json create mode 100644 tests/parity/test_stochastic_dominance_r14.py diff --git a/docs/conventions.md b/docs/conventions.md index f6f0e5e7..ce204122 100644 --- a/docs/conventions.md +++ b/docs/conventions.md @@ -116,6 +116,18 @@ names, or the column names of a DataFrame input. `type="continuous"` is supported for first-degree efficient sets, `fsd`, and `fsd_uni`; as in R, an unrecognised `type` warns (and `fsd` then runs its continuous branch, while `fsd_uni` and the efficient-set routines run the discrete one). +`sd_efficient_set` returns 0-based column indices in R's output order (R +returns the column names), and a vector is one column, as with R's `as.matrix`. +Inputs are checked in R's order and with R's messages: missing values raise +"You have some missing values, please address.", empty samples "Each sample +needs at least one observation.". Like R, the `.uni` tests, `sd_efficient_set` +and `nns_sd_cluster` accept infinite values and compare them as R's C++ test +does, while `fsd`, `ssd` and `tsd` reject them (R first evaluates the plotted +partial-moment curves, which check `x` and then the threshold grid). +`nns_sd_cluster` validates its data in the first efficient-set search, as R +does, so a matrix with no more than `min_cluster` columns comes back as one +cluster unchecked, and `min_cluster` is compared as a real number (`1.5` merges +a trailing single-member cluster into the previous one). `dendrogram=True` returns a plain dictionary mirroring R's `hclust` fields: `merge`, `height`, `order`, `labels`, `method`, `call`, and `dist.method`. With fewer than two clustered variables it warns, as R does, and returns the diff --git a/src/nns/stochastic_dominance.py b/src/nns/stochastic_dominance.py index 6521c66e..a5e00e3c 100644 --- a/src/nns/stochastic_dominance.py +++ b/src/nns/stochastic_dominance.py @@ -24,9 +24,12 @@ from __future__ import annotations +import functools +import math import warnings -from collections.abc import Sequence +from collections.abc import Callable, Sequence from dataclasses import dataclass +from typing import ParamSpec, TypeVar import numpy as np from numpy.typing import NDArray @@ -34,12 +37,27 @@ from scipy.sparse.csgraph import connected_components # type: ignore[import-untyped] from scipy.spatial.distance import squareform # type: ignore[import-untyped] -from nns.core import _as_1d_values - _SD_ORDER_STAT_TARGET_BLOCK_COLUMNS = 64 # R's native SD test ignores excesses up to 64 * DBL_EPSILON (relative to the data scale). _SD_EPS = float(np.finfo(np.float64).eps) _SD_EPS64 = 64.0 * _SD_EPS +# R's own messages: the NA check in every R wrapper and the C++ empty-sample check. +_MISSING_VALUES = "You have some missing values, please address." +_EMPTY_SAMPLE = "Each sample needs at least one observation." + +_P = ParamSpec("_P") +_R = TypeVar("_R") + + +def _cpp_float_semantics(function: Callable[_P, _R]) -> Callable[_P, _R]: + """Evaluate like R's C++: infinite inputs give inf/NaN intermediates silently.""" + + @functools.wraps(function) + def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R: + with np.errstate(divide="ignore", invalid="ignore", over="ignore"): + return function(*args, **kwargs) + + return wrapper @dataclass(frozen=True) @@ -58,6 +76,7 @@ class _SDOrderStatPrecomputed: identical: NDArray[np.bool_] +@_cpp_float_semantics def fsd( x: NDArray[np.float64], y: NDArray[np.float64], @@ -73,10 +92,12 @@ def fsd( partial moments vanish the ratio is the degree-0 ratio (the CDF value), as in ``lpm_ratio`` and the patched R. ``plot=True`` also draws R's FSD figure (``nns.plotting.plot_fsd``); R's default is ``plot = TRUE``. + + As in R, missing values raise, and so do infinite ones (R evaluates the + plotted ``LPM.ratio`` curves, which reject them, before the verdict). """ - x_values = _as_sd_values(x, "x") - y_values = _as_sd_values(y, "y") discrete = _checked_fsd_type(type) == "discrete" + x_values, y_values = _sd_pair(x, y, finite=True) result = _sd_result(x_values, y_values, 1, discrete=discrete) if plot: from nns.plotting.dominance import plot_fsd @@ -85,25 +106,26 @@ def fsd( return result +@_cpp_float_semantics def fsd_uni(x: NDArray[np.float64], y: NDArray[np.float64], type: str = "discrete") -> int: """Unidirectional first-order stochastic dominance: 1 if x dominates y, else 0. - Like R's ``NNS.FSD.uni``, an unrecognised ``type`` warns and runs the discrete test. + Like R's ``NNS.FSD.uni``, an unrecognised ``type`` warns and runs the discrete + test, and infinite values are compared as R's C++ test compares them. """ - x_values = _as_sd_values(x, "x") - y_values = _as_sd_values(y, "y") + x_values, y_values = _sd_pair(x, y, finite=False) discrete = _checked_fsd_type(type) != "continuous" return int(_dominates_uni(x_values, y_values, 1, discrete=discrete)) +@_cpp_float_semantics def ssd(x: NDArray[np.float64], y: NDArray[np.float64], *, plot: bool = False) -> int: """Second-order stochastic dominance; ``x`` and ``y`` may differ in length. ``plot=True`` also draws R's SSD figure (``nns.plotting.plot_ssd``); R's default is ``plot = TRUE``. """ - x_values = _as_sd_values(x, "x") - y_values = _as_sd_values(y, "y") + x_values, y_values = _sd_pair(x, y, finite=True) result = _sd_result(x_values, y_values, 2) if plot: from nns.plotting.dominance import plot_ssd @@ -112,21 +134,21 @@ def ssd(x: NDArray[np.float64], y: NDArray[np.float64], *, plot: bool = False) - return result +@_cpp_float_semantics def ssd_uni(x: NDArray[np.float64], y: NDArray[np.float64]) -> int: """Unidirectional second-order stochastic dominance: 1 if x dominates y, else 0.""" - x_values = _as_sd_values(x, "x") - y_values = _as_sd_values(y, "y") + x_values, y_values = _sd_pair(x, y, finite=False) return int(_dominates_uni(x_values, y_values, 2, discrete=True)) +@_cpp_float_semantics def tsd(x: NDArray[np.float64], y: NDArray[np.float64], *, plot: bool = False) -> int: """Third-order stochastic dominance; ``x`` and ``y`` may differ in length. ``plot=True`` also draws R's TSD figure (``nns.plotting.plot_tsd``); R's default is ``plot = TRUE``. """ - x_values = _as_sd_values(x, "x") - y_values = _as_sd_values(y, "y") + x_values, y_values = _sd_pair(x, y, finite=True) result = _sd_result(x_values, y_values, 3) if plot: from nns.plotting.dominance import plot_tsd @@ -135,10 +157,10 @@ def tsd(x: NDArray[np.float64], y: NDArray[np.float64], *, plot: bool = False) - return result +@_cpp_float_semantics def tsd_uni(x: NDArray[np.float64], y: NDArray[np.float64]) -> int: """Unidirectional third-order stochastic dominance: 1 if x dominates y, else 0.""" - x_values = _as_sd_values(x, "x") - y_values = _as_sd_values(y, "y") + x_values, y_values = _sd_pair(x, y, finite=False) return int(_dominates_uni(x_values, y_values, 3, discrete=True)) @@ -146,7 +168,7 @@ def nns_sd_cluster( data: NDArray[np.float64], degree: int = 1, type: str = "discrete", - min_cluster: int = 1, + min_cluster: float = 1, dendrogram: bool = False, names: Sequence[str] | None = None, ) -> dict[str, object]: @@ -157,21 +179,28 @@ def nns_sd_cluster( ``dendrogram=True`` returns R's ``hclust`` fields under ``"Dendrogram"`` and, as R does, draws the dendrogram; with fewer than two clustered variables it warns and returns the clusters only. + + As in R, the data are validated by the first efficient-set search, so a + matrix with no more than ``min_cluster`` columns is returned as one cluster + unchecked, and ``min_cluster`` is compared as the real number it is. """ + with np.errstate(divide="ignore", invalid="ignore", over="ignore"): + return _nns_sd_cluster(data, degree, type, min_cluster, dendrogram, names) + + +def _nns_sd_cluster( + data: NDArray[np.float64], + degree: int, + type: str, + min_cluster: float, + dendrogram: bool, + names: Sequence[str] | None, +) -> dict[str, object]: column_labels = getattr(data, "columns", None) values = np.asarray(data, dtype=np.float64) if values.ndim != 2: raise ValueError("data must be a 2D array.") - if values.shape[0] == 0: - raise ValueError("data must have at least one row.") - degree_int = _sd_degree(degree) - if not np.all(np.isfinite(values)): - raise ValueError("data must contain only finite values.") - type_value = _sd_type_value(degree_int, type) - discrete = degree_int != 1 or type_value != "continuous" - min_cluster = int(min_cluster) - if min_cluster < 0: - raise ValueError("min_cluster must be non-negative.") + min_cluster = float(min_cluster) column_count = values.shape[1] if names is None and column_labels is not None and len(column_labels) == column_count: @@ -185,17 +214,25 @@ def nns_sd_cluster( order_stat_dominance_matrix = None prefix_precomputed = None - if degree_int == 1 and discrete: - order_stat_precomputed = _order_stat_sd_precompute(values) - order_stat_dominance_matrix = _dominance_matrix_from_order_stats(order_stat_precomputed) - else: - prefix_precomputed = _prefix_sd_precompute(values, degree_int, discrete=discrete) + degree_int = 1 + discrete = True verdicts: dict[tuple[int, int], bool] = {} active = list(range(column_count)) clusters: dict[str, list[str]] = {} iteration = 1 while len(active) > min_cluster: + if not active: + break # R's efficient set of no columns is empty + if order_stat_dominance_matrix is None and prefix_precomputed is None: + # R validates in the first NNS.SD.efficient.set call, on every column. + degree_int, discrete = _validated_sd_matrix(values, degree, type) + if degree_int == 1 and discrete: + order_stat_dominance_matrix = _dominance_matrix_from_order_stats( + _order_stat_sd_precompute(values) + ) + else: + prefix_precomputed = _prefix_sd_precompute(values, degree_int, discrete=discrete) if order_stat_dominance_matrix is not None: sd_set_indices = _sd_efficient_active_indices_from_matrix( values, @@ -248,7 +285,7 @@ def nns_sd_cluster( return {"Clusters": clusters, "Order": None} return { "Clusters": clusters, - "Dendrogram": _sd_cluster_hclust(clusters, all_names, draw_degree=degree_int), + "Dendrogram": _sd_cluster_hclust(clusters, all_names, draw_degree=degree), } return {"Clusters": clusters} @@ -258,7 +295,7 @@ def _sd_cluster_hclust( clusters: dict[str, list[str]], original_names: Sequence[str], *, - draw_degree: int | None = None, + draw_degree: object = None, ) -> dict[str, object]: all_vars = [name for cluster in clusters.values() for name in cluster] cluster_labels = np.asarray( @@ -292,7 +329,7 @@ def _sd_cluster_hclust( } -def _draw_sd_dendrogram(linked: NDArray[np.float64], labels: list[str], degree: int) -> None: +def _draw_sd_dendrogram(linked: NDArray[np.float64], labels: list[str], degree: object) -> None: """Draw the cluster dendrogram, as R's ``NNS.SD.cluster(dendrogram = TRUE)`` does.""" from scipy.cluster.hierarchy import dendrogram as scipy_dendrogram @@ -302,6 +339,9 @@ def _draw_sd_dendrogram(linked: NDArray[np.float64], labels: list[str], degree: scipy_dendrogram( linked, labels=labels, ax=ax, link_color_func=lambda _: "black", leaf_rotation=90 ) + # R pastes the degree as given; a whole double prints without decimals. + if isinstance(degree, float) and degree.is_integer(): + degree = int(degree) ax.set_title(f"Hierarchical Clustering of Stochastic Dominance Sets\nSD Degree: {degree}") ax.set_xlabel("Variables") ax.set_ylabel("SD Distance") @@ -334,30 +374,25 @@ def _r_hclust_merge(linked: NDArray[np.float64], n_obs: int) -> NDArray[np.int64 return out +@_cpp_float_semantics def sd_efficient_set( returns: NDArray[np.float64], degree: int, type: str = "discrete", ) -> list[int]: - """Return indices of non-dominated columns at the requested SD degree. - - ``degree`` is coerced like R's ``as.integer``: 1.5 runs first-order (FSD) and - ``"2"`` runs second-order; anything that does not truncate to 1, 2 or 3 raises. + """Return 0-based indices of the non-dominated columns, in R's output order. + + R's ``NNS.SD.efficient.set`` returns the corresponding column names. A vector + is one column (R's ``as.matrix``). ``degree`` is coerced like R's + ``as.integer``: 1.5 runs first-order (FSD) and ``"2"`` runs second-order; + anything that does not truncate to 1, 2 or 3 raises. Validation follows R's + C++ order (no columns, empty columns, degree, missing values), and infinite + values are compared as R's C++ test compares them. """ - values = np.asarray(returns, dtype=np.float64) - if values.ndim != 2: - raise ValueError("returns must be a 2D array.") - if values.shape[0] == 0: - raise ValueError("returns must have at least one row.") - degree = _sd_degree(degree) - if not np.all(np.isfinite(values)): - raise ValueError("returns must contain only finite values.") - - type_value = _sd_type_value(degree, type) - discrete = degree != 1 or type_value != "continuous" - + values = _sd_matrix(returns, "returns") if values.shape[1] == 0: return [] + degree, discrete = _validated_sd_matrix(values, degree, type) # Every pair is evaluated on its own merged threshold grid (as R does), so # the verdict for a pair never depends on which other columns are present. @@ -380,6 +415,28 @@ def sd_efficient_set( ) +def _sd_matrix(data: object, name: str) -> NDArray[np.float64]: + values = np.asarray(data, dtype=np.float64) + if values.ndim < 2: + values = values.reshape(-1, 1) + if values.ndim != 2: + raise ValueError(f"{name} must be a 2D array.") + return values + + +def _validated_sd_matrix( + values: NDArray[np.float64], degree: object, type: str +) -> tuple[int, bool]: + """R's C++ checks for a non-empty set of columns, in its order.""" + if values.shape[0] == 0: + raise ValueError(_EMPTY_SAMPLE) + degree_int = _sd_degree(degree) + if np.isnan(values).any(): + raise ValueError(_MISSING_VALUES) + type_value = _sd_type_value(degree_int, type) + return degree_int, degree_int != 1 or type_value != "continuous" + + def _sd_degree(degree: object) -> int: """R's ``as.integer(degree)`` (truncation toward zero), restricted to 1, 2 or 3.""" try: @@ -667,12 +724,15 @@ def _dominates_sorted( gap = y_prefix1[-1] / y_count - x_prefix1[-1] / x_count if degree > 1 and gap > 2.0 * _SD_EPS64 * max(abs(lo), abs(hi)): return False - level = 0.5 * lo + 0.5 * hi - x_sorted, y_sorted = x_sorted - level, y_sorted - level - x_prefix1, y_prefix1 = _prefix_sum_1d(x_sorted), _prefix_sum_1d(y_sorted) - if degree == 3: - x_prefix2, y_prefix2 = _prefix_sum_1d(x_sorted**2), _prefix_sum_1d(y_sorted**2) - level = abs(level) + centre = 0.5 * lo + 0.5 * hi + # R compares a pair with an infinite value unshifted: it cannot be centred. + if math.isfinite(centre): + x_sorted, y_sorted = x_sorted - centre, y_sorted - centre + x_prefix1, y_prefix1 = _prefix_sum_1d(x_sorted), _prefix_sum_1d(y_sorted) + if degree == 3: + x_prefix2 = _prefix_sum_1d(x_sorted * x_sorted) + y_prefix2 = _prefix_sum_1d(y_sorted * y_sorted) + level = abs(centre) scale = float(max(abs(x_sorted[0]), abs(x_sorted[-1]), abs(y_sorted[0]), abs(y_sorted[-1]))) # Summation rounding plus the inputs' own rounding at their level |c|. din = 4.0 * _SD_EPS * level @@ -692,9 +752,9 @@ def _dominates_sorted( elif degree == 1: x_curve, x_total = _ratio_deg1(x_prefix1, x_counts, thresholds, x_count) y_curve, y_total = _ratio_deg1(y_prefix1, y_counts, thresholds, y_count) - total_min = np.minimum(x_total, y_total) - with np.errstate(divide="ignore"): - tol = np.where(total_min > 0.0, np.maximum(_SD_EPS64, e1 / total_min), _SD_EPS64) + total_min = _cpp_min(x_total, y_total) + with np.errstate(divide="ignore", invalid="ignore"): + tol = np.where(total_min > 0.0, _cpp_max(_SD_EPS64, e1 / total_min), _SD_EPS64) if not np.any(x_curve - y_curve > tol): interior_excess = _ratio_deg1_interior_excess( x_prefix1, x_counts, x_count, y_prefix1, y_counts, y_count, thresholds, e1 @@ -741,6 +801,20 @@ def _dominates_sorted( return bool(np.any(np.abs(excess) > tol)) +def _cpp_min( + a: float | NDArray[np.float64], b: float | NDArray[np.float64] +) -> NDArray[np.float64]: + """``std::min(a, b)``, i.e. ``b < a ? b : a``: a NaN in ``b`` yields ``a``.""" + return np.asarray(np.where(b < a, b, a), dtype=np.float64) + + +def _cpp_max( + a: float | NDArray[np.float64], b: float | NDArray[np.float64] +) -> NDArray[np.float64]: + """``std::max(a, b)``, i.e. ``a < b ? b : a``: a NaN in ``b`` yields ``a``.""" + return np.asarray(np.where(a < b, b, a), dtype=np.float64) + + def _merged_thresholds( x_sorted: NDArray[np.float64], y_sorted: NDArray[np.float64] ) -> tuple[NDArray[np.float64], NDArray[np.intp], NDArray[np.intp]]: @@ -761,17 +835,27 @@ def _merged_thresholds( return values[ends], x_counts, y_counts -def _ratio_deg1( +def _lpm1_and_total( prefix1: NDArray[np.float64], counts: NDArray[np.intp], thresholds: NDArray[np.float64], observations: int, ) -> tuple[NDArray[np.float64], NDArray[np.float64]]: - """Continuous FSD ratio LPM1 / (LPM1 + UPM1) at the thresholds, and LPM1 + UPM1.""" + """LPM1 and LPM1 + UPM1 at the thresholds, from prefix sums (R's ``lpm_upm_deg1``).""" sums1 = prefix1[counts] lower = (counts * thresholds - sums1) / observations upper = ((prefix1[-1] - sums1) - (observations - counts) * thresholds) / observations - total = lower + upper + return lower, lower + upper + + +def _ratio_deg1( + prefix1: NDArray[np.float64], + counts: NDArray[np.intp], + thresholds: NDArray[np.float64], + observations: int, +) -> tuple[NDArray[np.float64], NDArray[np.float64]]: + """Continuous FSD ratio LPM1 / (LPM1 + UPM1) at the thresholds, and LPM1 + UPM1.""" + lower, total = _lpm1_and_total(prefix1, counts, thresholds, observations) # 0/0 (every observation equals the threshold): the degree-0 ratio, i.e. the # right-continuous CDF value, as LPM.ratio does (it was 0). ratio: NDArray[np.float64] = np.divide( @@ -813,7 +897,7 @@ def _ratio_deg1_interior_excess( den = gy - r * gx valid &= den != 0.0 ts = (r * dx - dy) / np.where(valid, den, 1.0) - valid &= (ts > lower_knots) & (ts < upper_knots) + valid &= (ts > lower_knots) & (ts < upper_knots) & np.isfinite(upper_knots) denom_x = gx * ts + dx denom_y = gy * ts + dy valid &= (denom_x > 0.0) & (denom_y > 0.0) @@ -821,7 +905,7 @@ def _ratio_deg1_interior_excess( return False ratio_x = (ax * ts + bx) / denom_x ratio_y = (ay * ts + by) / denom_y - tol = np.maximum(_SD_EPS64, e1 / np.minimum(denom_x, denom_y)) + tol = _cpp_max(_SD_EPS64, e1 / _cpp_min(denom_x, denom_y)) return bool(np.any(valid & (ratio_x - ratio_y > tol))) @@ -847,11 +931,13 @@ def _ratio_deg1_midpoint_verdict( s = 0.5 * (thresholds[:-1] + thresholds[1:]) kx = x_counts[:-1] ky = y_counts[:-1] - x_ratio, x_total = _ratio_deg1(x_prefix1, kx, s, x_count) - y_ratio, y_total = _ratio_deg1(y_prefix1, ky, s, y_count) - d = x_ratio - y_ratio - tol = np.maximum(_SD_EPS64, e1 / np.minimum(x_total, y_total)) - differs = np.abs(d) > tol + x_lower, x_total = _lpm1_and_total(x_prefix1, kx, s, x_count) + y_lower, y_total = _lpm1_and_total(y_prefix1, ky, s, y_count) + # R divides directly here: strictly between knots a finite sample's total + # deviation is positive. + d = x_lower / x_total - y_lower / y_total + tol = _cpp_max(_SD_EPS64, e1 / _cpp_min(x_total, y_total)) + differs = (np.abs(d) > tol) & np.isfinite(thresholds[1:]) first_knot = np.flatnonzero(knot_differs) if first_knot.size: differs[first_knot[0] :] = False @@ -919,7 +1005,8 @@ def _lpm_deg2_interior_excess( ky = ky[concave] b = x_prefix1[kx] / x_count - y_prefix1[ky] / y_count ts = b / a[concave] - inside = (ts > thresholds[:-1][concave]) & (ts < thresholds[1:][concave]) + upper_knots = thresholds[1:][concave] + inside = (ts > thresholds[:-1][concave]) & (ts < upper_knots) & np.isfinite(upper_knots) if not np.any(inside): return False kx = kx[inside] @@ -989,20 +1076,52 @@ def _lpm_at_target( target: float, degree: int, ) -> NDArray[np.float64]: - deviations = np.maximum(0.0, target - values) - if degree > 1: - deviations = deviations**degree - return np.asarray(np.mean(deviations, axis=0), dtype=np.float64) + """Each column's LPM(degree, target) with the arithmetic of R's efficient-set key. + + R's C++ raises each positive deviation to the power by repeated multiplication + and sums the rows in their original order, so columns whose keys tie in exact + arithmetic (permutations, duplicates) keep their tie and the index tie break. + """ + deviations = target - values + powered = deviations + for _ in range(degree - 1): + powered = powered * deviations + terms = np.where(deviations > 0.0, powered, 0.0) + if terms.shape[0] == 0: + return np.zeros(terms.shape[1], dtype=np.float64) + return np.asarray(np.cumsum(terms, axis=0)[-1] / terms.shape[0], dtype=np.float64) def _as_sd_values(x: NDArray[np.float64], name: str) -> NDArray[np.float64]: + """R's ``to_numeric_vector``: single-column objects and any vector shape flatten.""" values = np.asarray(x, dtype=np.float64) if values.ndim == 2: # R accepts single-column matrices and data frames. if values.shape[1] != 1: raise ValueError(f"{name} must be a single-column object or numeric vector.") values = values[:, 0] - values = _as_1d_values(values) - if not np.all(np.isfinite(values)): - raise ValueError(f"{name} must contain only finite values.") - return values + return values.reshape(-1) + + +def _sd_pair( + x: NDArray[np.float64], y: NDArray[np.float64], *, finite: bool +) -> tuple[NDArray[np.float64], NDArray[np.float64]]: + """Both samples, checked in R's order: shape, missing values, finiteness, size. + + ``finite`` is for ``NNS.FSD``/``NNS.SSD``/``NNS.TSD``, which evaluate + ``LPM(.ratio)(degree, sort(c(x, y)), x)`` first: that checks the variable ``x`` + and then the target grid. The ``.uni`` tests go straight to R's C++ test, + which accepts infinite values. + """ + x_values = _as_sd_values(x, "x") + y_values = _as_sd_values(y, "y") + if np.isnan(x_values).any() or np.isnan(y_values).any(): + raise ValueError(_MISSING_VALUES) + if finite: + if not np.all(np.isfinite(x_values)): + raise ValueError("`variable` must be finite.") + if not np.all(np.isfinite(y_values)): + raise ValueError("`target` must be finite.") + if x_values.size == 0 or y_values.size == 0: + raise ValueError(_EMPTY_SAMPLE) + return x_values, y_values diff --git a/tests/_r_cache/sd14.cluster_min.json b/tests/_r_cache/sd14.cluster_min.json new file mode 100644 index 00000000..5805161c --- /dev/null +++ b/tests/_r_cache/sd14.cluster_min.json @@ -0,0 +1,29 @@ +{ + "entries": { + "04c56bf876b8d84757e8e13071ac2c3e06ad0307719160df94f82fc810e328f7": { + "Cluster_1": [ + "X_5", + "X_4" + ], + "Cluster_2": [ + "X_2", + "X_3", + "X_1" + ] + }, + "b3ea09279a07037eaf3e1da2c66e9fa74c77514d3ab19d688478eb0e51813383": { + "Cluster_1": [ + "X_5", + "X_4" + ], + "Cluster_2": [ + "X_2", + "X_3" + ], + "Cluster_3": "X_1" + } + }, + "label": "sd14.cluster_min", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/sd14.efficient_order.json b/tests/_r_cache/sd14.efficient_order.json new file mode 100644 index 00000000..4ceec4ac --- /dev/null +++ b/tests/_r_cache/sd14.efficient_order.json @@ -0,0 +1,23 @@ +{ + "entries": { + "3711ee6b9a48a905f4e2d77ba1beb5819aa8004044a3d2f5d2ac6f42c6173b24": [ + "X_1", + "X_5", + "X_3", + "X_7", + "X_6", + "X_2" + ], + "47e0dfad64d182a314f7a10b36c7f7e93a719352fd015c0f2355a8ac7dced19a": [ + "X_1", + "X_5" + ], + "67774372c5bc21573826c8f64d6a1fb02db569c83087b50127ee9e0b25cc94a9": [ + "X_1", + "X_5" + ] + }, + "label": "sd14.efficient_order", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/sd14.matrix_infinite.json b/tests/_r_cache/sd14.matrix_infinite.json new file mode 100644 index 00000000..99cf7e08 --- /dev/null +++ b/tests/_r_cache/sd14.matrix_infinite.json @@ -0,0 +1,66 @@ +{ + "entries": { + "0a2798877c8efe28b92d79ccc1326014a340a4eeee78af54034f9f5b14b01969": { + "clu": { + "Cluster_1": [ + "X_2", + "X_3", + "X_4" + ], + "Cluster_2": [ + "X_1", + "X_6" + ], + "Cluster_3": "X_5" + }, + "eff": [ + "X_2", + "X_3", + "X_4" + ] + }, + "663342d8ecaac9220f4be8aa03004719513e3d8b43bbb657c6d6ebe96915538a": { + "clu": { + "Cluster_1": [ + "X_2", + "X_3", + "X_4", + "X_5" + ], + "Cluster_2": [ + "X_1", + "X_6" + ] + }, + "eff": [ + "X_2", + "X_3", + "X_4", + "X_5" + ] + }, + "ac5b7841c9e5575bc7baacf64274b4645a25286269dfae574514843473079b39": { + "clu": { + "Cluster_1": [ + "X_2", + "X_3", + "X_4", + "X_5" + ], + "Cluster_2": [ + "X_1", + "X_6" + ] + }, + "eff": [ + "X_2", + "X_3", + "X_4", + "X_5" + ] + } + }, + "label": "sd14.matrix_infinite", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/sd14.uni_infinite.json b/tests/_r_cache/sd14.uni_infinite.json new file mode 100644 index 00000000..e590d95f --- /dev/null +++ b/tests/_r_cache/sd14.uni_infinite.json @@ -0,0 +1,37 @@ +{ + "entries": { + "6d6bbd534d527e6ab65a5c1945bafe2d9a2d6818bab8c1b093fa485f50e30343": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ], + "aed46358a9029a961039712b2a96853759ee61d94f212f17d55a3e253f526b13": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ], + "d0642d9cd4e0eff0b6870212762e71b9aff8dbea5795aff8ac30a9dbf0c38509": [ + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ] + }, + "label": "sd14.uni_infinite", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/parity/test_sd_cluster.py b/tests/parity/test_sd_cluster.py index 6e86ecd8..9ab4458b 100644 --- a/tests/parity/test_sd_cluster.py +++ b/tests/parity/test_sd_cluster.py @@ -129,7 +129,7 @@ def test_nns_sd_cluster_missing_values_raise() -> None: data = _known_matrix() data[0, 0] = np.nan - with pytest.raises(ValueError, match="finite"): + with pytest.raises(ValueError, match="You have some missing values, please address"): nns_sd_cluster(data) diff --git a/tests/parity/test_stochastic_dominance_r14.py b/tests/parity/test_stochastic_dominance_r14.py new file mode 100644 index 00000000..fec06c9e --- /dev/null +++ b/tests/parity/test_stochastic_dominance_r14.py @@ -0,0 +1,170 @@ +"""R 14.0 parity for the stochastic-dominance contracts beyond the pairwise verdict. + +* ``NNS.SD.efficient.set`` lists its columns by ``LPM(degree, max, column)``, + which R's C++ forms by repeated multiplication and a row-order sum; columns + whose keys tie in exact arithmetic (permutations, duplicates) keep their tie + and R's index tie break. +* R's ``.uni`` tests, efficient set and SD clustering accept infinite values + (only ``NNS.FSD``/``NNS.SSD``/``NNS.TSD`` reject them, through the plotted + partial-moment curves), comparing them as R's C++ does. +* Inputs are validated in R's order and with R's messages, and + ``NNS.SD.cluster`` compares ``min_cluster`` as a real number. +""" + +from __future__ import annotations + +import warnings +from typing import Any + +import numpy as np +import pytest +from _r import nns_frame_call, r_column + +import nns + +pytestmark = pytest.mark.parity + + +def _num(values: Any) -> dict[str, Any]: + return r_column([float(v) for v in np.asarray(values, dtype=np.float64).ravel()], "numeric") + + +def _strings(value: Any) -> list[str]: + if value is None: + return [] + return [str(v) for v in (value if isinstance(value, list) else [value])] + + +@pytest.mark.parametrize("degree", [1, 2, 3]) +def test_efficient_set_order_keeps_r_ties_for_permuted_and_duplicated_columns( + degree: int, +) -> None: + rng = np.random.default_rng(91) + base = rng.normal(size=(60, 4)) + columns = [base[:, i % 4] for i in range(8)] + columns[5] = rng.permutation(columns[5]) + columns[7] = rng.permutation(columns[7]) + m = np.column_stack(columns) + expected = nns_frame_call( + "sd14.efficient_order", + f"NNS.SD.efficient.set(matrix(v, ncol = 8), {degree}, status = FALSE)", + {"v": _num(m.ravel(order="F"))}, + ) + actual = [f"X_{i + 1}" for i in nns.sd_efficient_set(m, degree)] + assert actual == _strings(expected) + + +def _with_infinities(seed: int) -> tuple[np.ndarray, np.ndarray]: + rng = np.random.default_rng(seed) + return rng.normal(size=25) + 0.3, rng.normal(size=31) + + +@pytest.mark.parametrize(("x_inf", "y_inf"), [("Inf", ""), ("", "-Inf"), ("Inf", "-Inf")]) +def test_uni_tests_compare_infinite_values_as_r_does(x_inf: str, y_inf: str) -> None: + x, y = _with_infinities(92) + if x_inf: + x[4] = float(x_inf) + if y_inf: + y[7] = float(y_inf) + setup = "x <- xv; y <- yv; " + (f"x[5] <- {x_inf}; " if x_inf else "") + ( + f"y[8] <- {y_inf}; " if y_inf else "" + ) + expected = nns_frame_call( + "sd14.uni_infinite", + setup + "c(NNS.FSD.uni(x, y), NNS.FSD.uni(y, x), " + "NNS.FSD.uni(x, y, type = 'continuous'), NNS.FSD.uni(y, x, type = 'continuous'), " + "NNS.SSD.uni(x, y), NNS.SSD.uni(y, x), NNS.TSD.uni(x, y), NNS.TSD.uni(y, x))", + { + "xv": _num(np.where(np.isfinite(x), x, 0.0)), + "yv": _num(np.where(np.isfinite(y), y, 0.0)), + }, + ) + with warnings.catch_warnings(): + warnings.simplefilter("error", RuntimeWarning) + actual = [ + nns.fsd_uni(x, y), + nns.fsd_uni(y, x), + nns.fsd_uni(x, y, type="continuous"), + nns.fsd_uni(y, x, type="continuous"), + nns.ssd_uni(x, y), + nns.ssd_uni(y, x), + nns.tsd_uni(x, y), + nns.tsd_uni(y, x), + ] + assert actual == [int(v) for v in np.asarray(expected)] + + +@pytest.mark.parametrize(("degree", "type_"), [(1, "discrete"), (1, "continuous"), (2, "discrete")]) +def test_efficient_set_and_clusters_accept_infinite_values_like_r(degree: int, type_: str) -> None: + rng = np.random.default_rng(93) + m = rng.normal(size=(30, 6)) + rng.uniform(-0.5, 0.5, 6) + m[3, 1] = np.inf + m[9, 4] = -np.inf + expected = nns_frame_call( + "sd14.matrix_infinite", + "M <- matrix(v, ncol = 6); M[4, 2] <- Inf; M[10, 5] <- -Inf; " + f"list(eff = NNS.SD.efficient.set(M, {degree}, type = '{type_}', status = FALSE), " + f"clu = NNS.SD.cluster(M, degree = {degree}, type = '{type_}')$Clusters)", + {"v": _num(np.where(np.isfinite(m), m, 0.0).ravel(order="F"))}, + ) + with warnings.catch_warnings(): + warnings.simplefilter("error", RuntimeWarning) + efficient = nns.sd_efficient_set(m, degree, type=type_) + clusters = nns.nns_sd_cluster(m, degree=degree, type=type_)["Clusters"] + assert [f"X_{i + 1}" for i in efficient] == _strings(expected["eff"]) + assert clusters == {name: _strings(members) for name, members in expected["clu"].items()} + + +def test_top_level_tests_reject_infinite_values_as_r_does() -> None: + # NNS.FSD/SSD/TSD first evaluate LPM(.ratio)(degree, sort(c(x, y)), x), which + # checks the variable x and then the target grid. + x = np.array([1.0, 2.0, 3.0]) + for function in (nns.fsd, nns.ssd, nns.tsd): + with pytest.raises(ValueError, match="`variable` must be finite"): + function(np.array([1.0, np.inf]), x) + with pytest.raises(ValueError, match="`target` must be finite"): + function(x, np.array([-np.inf, 1.0])) + + +def test_validation_follows_r_order_and_messages() -> None: + missing = "You have some missing values, please address" + empty = "Each sample needs at least one observation" + with pytest.raises(ValueError, match=missing): + nns.fsd(np.array([1.0, np.nan]), np.array([np.inf])) + with pytest.raises(ValueError, match=missing): + nns.ssd_uni(np.array([1.0, 2.0]), np.array([0.0, np.nan])) + with pytest.raises(ValueError, match=empty): + nns.fsd(np.array([]), np.array([1.0, 2.0])) + with pytest.raises(ValueError, match=empty): + nns.tsd_uni(np.array([1.0, 2.0]), np.array([])) + # R's C++ efficient set: no columns, then empty columns, then the degree, + # then missing values. + assert nns.sd_efficient_set(np.zeros((3, 0)), 7) == [] + with pytest.raises(ValueError, match=empty): + nns.sd_efficient_set(np.zeros((0, 2)), 9) + with pytest.raises(ValueError, match="degree must be 1, 2, or 3"): + nns.sd_efficient_set(np.array([[1.0, 4.0], [2.0, np.nan]]), 5) + with pytest.raises(ValueError, match=missing): + nns.sd_efficient_set(np.array([[1.0, 4.0], [2.0, np.nan]]), 2) + # as.matrix(vector) is one column. + assert nns.sd_efficient_set(np.array([3.0, 1.0, 2.0]), 2) == [0] + + +def test_sd_cluster_validates_lazily_and_compares_min_cluster_as_a_real() -> None: + # No more columns than min_cluster: R never searches, so nothing is checked. + data = np.array([[1.0, np.nan], [2.0, 4.0]]) + assert nns.nns_sd_cluster(data, degree=9, min_cluster=2)["Clusters"] == { + "Cluster_1": ["X_1", "X_2"] + } + rng = np.random.default_rng(94) + m = rng.normal(size=(40, 5)) + np.linspace(0.0, 0.6, 5) + flat = _num(m.ravel(order="F")) + for min_cluster in (1.5, -1): + expected = nns_frame_call( + "sd14.cluster_min", + "NNS.SD.cluster(matrix(v, ncol = 5), degree = 2, " + f"min_cluster = {min_cluster})$Clusters", + {"v": flat}, + ) + actual = nns.nns_sd_cluster(m, degree=2, min_cluster=min_cluster)["Clusters"] + assert actual == {name: _strings(members) for name, members in expected.items()} From a07c0ac103ae346c959ef63aac09be0c56db67e5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 20:10:28 +0000 Subject: [PATCH 06/16] Reproduce R's random stream in NNS.meboot, NNS.MC, NNS.SS and NNS.ARMA NNS.meboot draws only through runif, so with random_seed = s the port now gives R's replicates after set.seed(s), bit for bit, and so do NNS.MC, NNS.SS's bootstrap bounds and NNS.ARMA's prediction intervals: - RRNG.unif_rand_array draws R's Mersenne-Twister stream in bulk; meboot uses it for NNS.meboot.part and R::runif for the expand.sd factors. - New ports of R's optimize() (Brent's fmin, with non-finite objective values replaced by the largest double), qnorm (AS 241 with R's tail extension), the trimmed mean's partial sort, and var/sd (cov.c). - force.clt spreads the replicate means by sd / sqrt(n), R 14's fix of meboot's division by the number of replicates (the port still divided by sqrt(reps)). - meboot keeps R's slope tolerance for target_drift, draws without a rho target when only target_drift(_scale) is given, accepts expand_residual_sd, and uses R's round/rowMeans/colSums arithmetic. - NNS.MC validates its arguments with R's messages, averages with R's rowMeans and labels rho values as R's as.character does. - NNS.SS draws the x and then the y replicates from one stream. - NNS.ARMA treats a constant forecast path as R 14 does, and its estimates, lag weights and NNS.seas scores now use R's long-double mean/sum and sd, so they agree with R to the last bit. Live sweeps against R 14.0 under set.seed: 1004 meboot/MC/SS/ARMA cases, 96 ARMA, 24 ARMA.optim and 144 NNS.seas cases, all identical. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt --- README.md | 2 +- docs/api_reference.md | 2 +- docs/api_status.md | 7 +- docs/conventions.md | 46 +- src/nns/_rmean.py | 95 + src/nns/_rnmath.py | 193 ++ src/nns/_roptim.py | 131 ++ src/nns/_rrng.py | 32 + src/nns/_seas_r14.py | 38 +- src/nns/arma.py | 73 +- src/nns/mc.py | 144 +- src/nns/meboot.py | 540 ++--- src/nns/stochastic_superiority.py | 45 +- tests/_r_cache/boot14.arma.json | 93 + tests/_r_cache/boot14.labels.json | 25 + tests/_r_cache/boot14.mc.json | 2809 +++++++++++++++++++++++++ tests/_r_cache/boot14.meboot.json | 3064 ++++++++++++++++++++++++++++ tests/_r_cache/boot14.seas.json | 65 + tests/_r_cache/boot14.ss.json | 100 + tests/invariants/test_mc.py | 2 +- tests/parity/test_bootstrap_r14.py | 215 ++ tests/parity/test_meboot.py | 5 +- 22 files changed, 7346 insertions(+), 380 deletions(-) create mode 100644 src/nns/_roptim.py create mode 100644 tests/_r_cache/boot14.arma.json create mode 100644 tests/_r_cache/boot14.labels.json create mode 100644 tests/_r_cache/boot14.mc.json create mode 100644 tests/_r_cache/boot14.meboot.json create mode 100644 tests/_r_cache/boot14.seas.json create mode 100644 tests/_r_cache/boot14.ss.json create mode 100644 tests/parity/test_bootstrap_r14.py diff --git a/README.md b/README.md index c105b9e0..16feb850 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ rather than R `data.table` objects and uses explicit Python errors for unsafe R coercions. - R is used for parity tests and local cache regeneration, not normal runtime. -- Exact stochastic stream parity is not expected for every randomized path. +- Seeded randomized paths reproduce R's stream: `random_seed = s` matches `set.seed(s)`. - Factor and class ordering should be supplied explicitly when it matters. - Classification codes follow the R contract and start at 1. - Compute functions return values; `plot=True` adds Matplotlib rendering as a diff --git a/docs/api_reference.md b/docs/api_reference.md index 9dba554c..5496fe61 100644 --- a/docs/api_reference.md +++ b/docs/api_reference.md @@ -287,7 +287,7 @@ Detects seasonality and supported modulo structures in a univariate series. Closest R API: `NNS.ARMA`. -Univariate ARMA-style forecasting surface. Stochastic interval streams use NumPy RNG and are structural or statistical parity targets rather than exact stream parity. +Univariate ARMA-style forecasting surface. Prediction intervals resample the forecast path with `nns_mc`; with `random_seed = s` they are R's after `set.seed(s)`, bit for bit. #### `nns_arma_optim` diff --git a/docs/api_status.md b/docs/api_status.md index 1a34c2e3..d4a1f6aa 100644 --- a/docs/api_status.md +++ b/docs/api_status.md @@ -76,8 +76,11 @@ invariant, and property coverage. - NNS Python uses explicit Python errors for some cases where R silently truncates, coerces, warns, or returns unusable values. Important divergences are recorded in `docs/conventions.md`. -- Stochastic exact stream parity is not expected. Stochastic paths use NumPy RNG - and are tested structurally/statistically. +- Seeded stochastic paths reproduce R's random stream: `random_seed = s` gives + R's results after `set.seed(s)` for `nns_meboot`, `nns_mc`, the `nns_ss` + bootstrap bounds, `nns_arma` prediction intervals, the robust and effect-size + `nns_anova` paths, `nns_stack` and `nns_boost`. Without a seed the stream is + seeded from fresh entropy. - Plotting side effects from R APIs are generally ignored; NNS Python returns data. - Stochastic-dominance performance work stays pure NumPy for now. The current implementation mirrors R's sorted-column/prefix-sum algorithm and adds diff --git a/docs/conventions.md b/docs/conventions.md index ce204122..0aad2897 100644 --- a/docs/conventions.md +++ b/docs/conventions.md @@ -104,9 +104,9 @@ stochastic-dominance tests. It returns `p_gt = P(X > Y)`, `p_tie = P(X = Y)`, and `p_star = p_gt + 0.5 * p_tie`. `NaN` values are omitted independently from `x` and `y`, matching R's `na.omit` preprocessing. With `confidence_interval=True`, intervals are computed through `nns_meboot`, -`lpm_var`, and `upm_var`; exact bootstrap parity with R is not expected because -the RNG streams differ. `random_seed` is a NNS Python-only reproducibility -convenience for that stochastic path. +`lpm_var`, and `upm_var`: with `random_seed = s` the bounds and `boot_vals` are +R's after `set.seed(s)`, the replicates of `x` and then of `y` drawn from one +stream as R draws them. `nns_sd_cluster` maps to R's `NNS.SD.cluster` default path. It iteratively peels `sd_efficient_set` results and returns a dictionary of `Cluster_1`, @@ -472,10 +472,12 @@ intervals it returns a NumPy forecast vector of length `h`; with `pred_int` it returns a dict keyed like R's data.table columns (`Estimates`, `Lower % pred.int`, `Upper % pred.int`). Forecasts are recursive: each estimate is appended before the next horizon step. Plot -arguments are ignored. Prediction intervals use `nns_mc` / `nns_meboot`; exact -stochastic parity with R is not expected because RNG streams differ. -`random_seed` is a NNS Python-only convenience for reproducible interval tests. -No-`pred_int` deterministic forecasts are parity-tested against R. +arguments are ignored. Prediction intervals use `nns_mc` / `nns_meboot`: with +`random_seed = s` they are R's after `set.seed(s)`, bit for bit. As in R 14, a +one-point or constant forecast path has no dispersion to resample, and its band +is the path widened by the per-lag spread alone. Forecasts, the seasonal lag +weights and `nns_seas` scores use R's arithmetic (long-double `mean`/`sum`, R's +`sd`), so they agree with R to the last bit. As in R, `seasonal_factor=True` with a non-`None` `best_periods` is treated as `seasonal_factor=False`: it weights the `best_periods` best periods from `nns_seas` (the default `best_periods=1` selects the best period). @@ -539,26 +541,34 @@ returns plain Python dictionaries instead of R's vectorized list-matrix wrapper. `rho`, `target_drift` and `target_drift_scale` are vectorized like R's `Vectorize`/`mapply`: the given arguments are recycled elementwise (not crossed) and a list of result dictionaries is returned in that order; when all of them are -scalars a single result dictionary is returned. `rho=None` follows installed R's empty -output behavior, and length-one input returns only `{"x": x}`. When the detrended +scalars a single result dictionary is returned. With none of the three given there +is nothing to map over and, as in R, the result is empty; length-one input returns +only `{"x": x}`. When the detrended residuals are all exactly zero (a constant or exactly linear series) `rho` has no effect, as in R: the replicates are copies of the trend with their means spread by `force_clt`, and a constant series gives constant columns. -Exact replicate parity with R is not expected because NNS Python uses NumPy's random -number generator and SciPy's optimizer while R uses its global RNG and -`optim()`. Deterministic diagnostics (`xx`, `z`, `dv`, `dvtrim`, `xmin`, -`xmax`, `desintxb`, `ordxx`, and `kappa`) are parity-tested exactly. Stochastic -outputs are tested structurally and statistically. `random_seed` is a NNS Python-only -convenience for reproducible bootstrap draws. +With `random_seed = s` the replicates are R's after `set.seed(s)`, bit for bit: +R draws only through `runif` (one per observation per replicate, then one per +shrunken column in each `expand.sd` pass), which the port reproduces from R's +Mersenne-Twister stream, and the deterministic steps follow R's arithmetic, +including `optimize()` (Brent's `fmin`, with non-finite objective values replaced +by the largest double as R does) for the `rho` target, `qnorm` for the CLT +spread and R's trimmed `mean`. Without a seed the stream is seeded from fresh +entropy. `force_clt` spreads the replicate means by `sd / sqrt(n)` with `n` the +series length, R 14's correction of meboot's division by the number of +replicates. Given only `target_drift`/`target_drift_scale` (no `rho`), the +replicates are drawn without a dependence target, as R's `Vectorize` does; +`expand_residual_sd` is R's `expand.residual.sd`. ## Monte Carlo `nns_mc` maps to R's `NNS.MC` wrapper around `NNS.meboot`. The rho grid and exponential rho transformation are parity-tested exactly against installed R. -As with `nns_meboot`, exact stochastic replicate parity is not expected because -R and NNS Python use different RNG streams and optimizer implementations. -`random_seed` is a NNS Python-only convenience passed through to `nns_meboot`. +With `random_seed = s` the replicates and ensemble are R's after +`set.seed(s)`. Arguments are validated with R's messages, the ensemble is R's +long-double `rowMeans`, and the replicate names format each rho as R's +`as.character` does (`"rho = 1e-04"`). NNS Python returns `{"ensemble": array, "replicates": dict}`. The `replicates` mapping preserves R's names, such as `"rho = 1"` and `"rho = -0.5"`, with each diff --git a/src/nns/_rmean.py b/src/nns/_rmean.py index 625e3a48..84ed5583 100644 --- a/src/nns/_rmean.py +++ b/src/nns/_rmean.py @@ -466,3 +466,98 @@ def r_pow(values: NDArray[np.float64] | float, exponent: float) -> NDArray[np.fl return np.ones_like(v, dtype=np.float64) flat = [1.0 if x == 1.0 else math.pow(float(x), exponent) for x in v.ravel()] return np.asarray(flat, dtype=np.float64).reshape(v.shape) + + +def r_var(values: NDArray[np.float64]) -> float: + """R's ``var(x)`` of a double vector (cov.c ``cov_na_1``, one column). + + Long-double deviations from the double-stored two-pass mean, long-double + products and sum, divided by ``n - 1`` and rounded to double. + """ + v = np.asarray(values, dtype=np.float64).ravel() + n = v.size + if n <= 1: + return float("nan") + ld = v.astype(np.longdouble) + dev = ld - _r_cov_mean(ld) + return float(np.cumsum(dev * dev)[-1] / np.longdouble(n - 1)) + + +def r_sd(values: NDArray[np.float64]) -> float: + """R's ``sd(x)``: ``sqrt(var(x))`` in double.""" + import math + + variance = r_var(values) + return math.sqrt(variance) if variance >= 0.0 else float("nan") + + +def _r_psort(x: list[float], indices: list[int]) -> None: + """R's ``.Internal(psort(x, partial))`` in place (sort.c ``Psort0``/``rPsort2``). + + ``indices`` are R's sorted 1-based partial positions. The values between + them stay in the order this quickselect leaves them, which is the order R's + long-double ``mean`` then sums. + """ + + def psort2(lo: int, hi: int, k: int) -> None: + left, right = lo, hi + while left < right: + v = x[k] + i, j = left, right + while i <= j: + while x[i] < v: + i += 1 + while v < x[j]: + j -= 1 + if i <= j: + x[i], x[j] = x[j], x[i] + i += 1 + j -= 1 + if j < k: + left = i + if k < i: + right = j + + def psort0(lo: int, hi: int, ind: list[int]) -> None: + if len(ind) < 1 or hi - lo < 1: + return + if len(ind) <= 1: + psort2(lo, hi, ind[0] - 1) + return + mid = (lo + hi) // 2 + this = 0 + for position, index in enumerate(ind): + if index - 1 <= mid: + this = position + z = ind[this] - 1 + psort2(lo, hi, z) + psort0(lo, z - 1, ind[:this]) + psort0(z + 1, hi, ind[this + 1 :]) + + psort0(0, len(x) - 1, indices) + + +def r_trimmed_mean(values: NDArray[np.float64], trim: float) -> float: + """R's ``mean(x, trim = trim)`` (``mean.default``). + + The kept order statistics come from R's partial sort, ``x[lo:hi]`` of + ``sort.int(x, partial = unique(c(lo, hi)))``, and are averaged in that + order by R's long-double two-pass ``mean``; a trim of 0.5 or more is the + median. + """ + import math + + v = np.asarray(values, dtype=np.float64).ravel() + n = v.size + if trim > 0 and n: + if np.isnan(v).any(): + return float("nan") + if trim >= 0.5: + return r_median(v) + lo = math.floor(n * trim) + 1 + hi = n + 1 - lo + partial = sorted({lo, hi}) + work = [float(value) for value in v] + _r_psort(work, partial) + v = np.asarray(work[lo - 1 : hi], dtype=np.float64) + return r_mean(v) diff --git a/src/nns/_rnmath.py b/src/nns/_rnmath.py index f1e53c69..6b8ab2e9 100644 --- a/src/nns/_rnmath.py +++ b/src/nns/_rnmath.py @@ -301,3 +301,196 @@ def clgamma(x: float) -> float: """``std::lgamma(x)`` as evaluated by R's compiled code on this platform.""" return _libm_lgamma()(x) + + +_M_SQRT2 = 1.41421356237309504880 # sqrt(2) +_M_2PI = 6.283185307179586476925286766559 # 2*pi + + +def qnorm(p: float, mu: float = 0.0, sigma: float = 1.0) -> float: + """R's ``qnorm(p, mu, sigma)`` on the lower tail and probability scale. + + Wichura's AS 241 with R's extension beyond ``r = 27``, as in R 4.5.0 + ``src/nmath/qnorm.c``; ``scipy.stats.norm.ppf`` (Cephes ``ndtri``) differs + from it in the last bits. + """ + if math.isnan(p) or math.isnan(mu) or math.isnan(sigma): + return p + mu + sigma + if p < 0.0 or p > 1.0: + return math.nan + if p == 0.0: + return -math.inf + if p == 1.0: + return math.inf + if sigma < 0.0: + return math.nan + if sigma == 0.0: + return mu + q = p - 0.5 + if abs(q) <= 0.425: + r = 0.180625 - q * q + val = ( + q + * ( + ( + ( + ( + ( + ((r * 2509.0809287301226727 + 33430.575583588128105) * r + + 67265.770927008700853) + * r + + 45921.953931549871457 + ) + * r + + 13731.693765509461125 + ) + * r + + 1971.5909503065514427 + ) + * r + + 133.14166789178437745 + ) + * r + + 3.387132872796366608 + ) + / ( + ( + ( + ( + ( + ((r * 5226.495278852854561 + 28729.085735721942674) * r + + 39307.89580009271061) + * r + + 21213.794301586595867 + ) + * r + + 5394.1960214247511077 + ) + * r + + 687.1870074920579083 + ) + * r + + 42.313330701600911252 + ) + * r + + 1.0 + ) + ) + return mu + sigma * val + # R_DT_CIv(p) on the lower tail is R_D_Cval(p) = 0.5 - p + 0.5. + lp = math.log((0.5 - p + 0.5) if q > 0 else p) + r = math.sqrt(-lp) + if r <= 5.0: + r = r + -1.6 + val = ( + ( + ( + ( + ( + ((r * 7.7454501427834140764e-4 + 0.0227238449892691845833) * r + + 0.24178072517745061177) + * r + + 1.27045825245236838258 + ) + * r + + 3.64784832476320460504 + ) + * r + + 5.7694972214606914055 + ) + * r + + 4.6303378461565452959 + ) + * r + + 1.42343711074968357734 + ) / ( + ( + ( + ( + ( + ((r * 1.05075007164441684324e-9 + 5.475938084995344946e-4) * r + + 0.0151986665636164571966) + * r + + 0.14810397642748007459 + ) + * r + + 0.68976733498510000455 + ) + * r + + 1.6763848301838038494 + ) + * r + + 2.05319162663775882187 + ) + * r + + 1.0 + ) + elif r <= 27.0: + r = r + -5.0 + val = ( + ( + ( + ( + ( + ((r * 2.01033439929228813265e-7 + 2.71155556874348757815e-5) * r + + 0.0012426609473880784386) + * r + + 0.026532189526576123093 + ) + * r + + 0.29656057182850489123 + ) + * r + + 1.7848265399172913358 + ) + * r + + 5.4637849111641143699 + ) + * r + + 6.6579046435011037772 + ) / ( + ( + ( + ( + ( + ((r * 2.04426310338993978564e-15 + 1.4215117583164458887e-7) * r + + 1.8463183175100546818e-5) + * r + + 7.868691311456132591e-4 + ) + * r + + 0.0148753612908506148525 + ) + * r + + 0.13692988092273580531 + ) + * r + + 0.59983220655588793769 + ) + * r + + 1.0 + ) + elif r >= 6.4e8: + val = r * _M_SQRT2 + else: + s2 = -math.ldexp(lp, 1) + x2 = s2 - math.log(_M_2PI * s2) + if r < 36000.0: + x2 = s2 - math.log(_M_2PI * x2) - 2.0 / (2.0 + x2) + if r < 840.0: + x2 = s2 - math.log(_M_2PI * x2) + 2 * math.log1p( + -(1 - 1 / (4 + x2)) / (2.0 + x2) + ) + if r < 109.0: + x2 = s2 - math.log(_M_2PI * x2) + 2 * math.log1p( + -(1 - (1 - 5 / (6 + x2)) / (4.0 + x2)) / (2.0 + x2) + ) + if r < 55.0: + x2 = s2 - math.log(_M_2PI * x2) + 2 * math.log1p( + -(1 - (1 - (5 - 9 / (8.0 + x2)) / (6.0 + x2)) / (4.0 + x2)) + / (2.0 + x2) + ) + val = math.sqrt(x2) + if q < 0.0: + val = -val + return mu + sigma * val diff --git a/src/nns/_roptim.py b/src/nns/_roptim.py new file mode 100644 index 00000000..477f2940 --- /dev/null +++ b/src/nns/_roptim.py @@ -0,0 +1,131 @@ +"""R's one-dimensional minimiser, ``optimize()``. + +``optimize(f, interval, tol)`` runs Brent's ``fmin`` (R 4.5.0 +``src/library/stats/src/optimize.c``, ``Brent_fmin``). Its golden-section and +parabolic steps, and so the point it returns, differ from SciPy's bounded +Brent method, and NNS uses the returned point directly (``NNS.meboot`` blends +each replicate with it), so the port reproduces R's iteration. As in R, a +non-finite objective value is replaced by the largest finite double (the most +negative one for ``-Inf``) with a warning. +""" + +from __future__ import annotations + +import math +import sys +import warnings +from collections.abc import Callable + +_DBL_EPSILON = sys.float_info.epsilon +_DBL_MAX = sys.float_info.max + + +def r_optimize( + f: Callable[[float], float], + lower: float, + upper: float, + tol: float = _DBL_EPSILON**0.25, +) -> float: + """``optimize(f, c(lower, upper), tol = tol)$minimum``.""" + if not (math.isfinite(lower) and math.isfinite(upper)): + raise ValueError("invalid 'xmin' or 'xmax' value") + if lower >= upper: + raise ValueError("'xmin' not less than 'xmax'") + if not (math.isfinite(tol) and tol > 0.0): + raise ValueError("invalid 'tol' value") + + def fcn(x: float) -> float: + value = float(f(x)) + if math.isfinite(value): + return value + if value == -math.inf: + warnings.warn("-Inf replaced by maximally negative value", RuntimeWarning, stacklevel=3) + return -_DBL_MAX + label = "NA/NaN" if math.isnan(value) else "Inf" + warnings.warn(f"{label} replaced by maximum positive value", RuntimeWarning, stacklevel=3) + return _DBL_MAX + + return _brent_fmin(lower, upper, fcn, tol) + + +def _brent_fmin(ax: float, bx: float, f: Callable[[float], float], tol: float) -> float: + c = (3.0 - math.sqrt(5.0)) * 0.5 + eps = math.sqrt(_DBL_EPSILON) + a = ax + b = bx + v = a + c * (b - a) + w = v + x = v + d = 0.0 + e = 0.0 + fx = f(x) + fv = fx + fw = fx + tol3 = tol / 3.0 + + while True: + xm = (a + b) * 0.5 + tol1 = eps * abs(x) + tol3 + t2 = tol1 * 2.0 + if abs(x - xm) <= t2 - (b - a) * 0.5: + break + p = 0.0 + q = 0.0 + r = 0.0 + if abs(e) > tol1: # fit a parabola + r = (x - w) * (fx - fv) + q = (x - v) * (fx - fw) + p = (x - v) * q - (x - w) * r + q = (q - r) * 2.0 + if q > 0.0: + p = -p + else: + q = -q + r = e + e = d + if abs(p) >= abs(q * 0.5 * r) or p <= q * (a - x) or p >= q * (b - x): + # a golden-section step + e = b - x if x < xm else a - x + d = c * e + else: # a parabolic-interpolation step + d = p / q + u = x + d + # f must not be evaluated too close to ax or bx + if u - a < t2 or b - u < t2: + d = tol1 + if x >= xm: + d = -d + # f must not be evaluated too close to x + if abs(d) >= tol1: + u = x + d + elif d > 0.0: + u = x + tol1 + else: + u = x - tol1 + fu = f(u) + # update a, b, v, w and x + if fu <= fx: + if u < x: + b = x + else: + a = x + v = w + w = x + x = u + fv = fw + fw = fx + fx = fu + else: + if u < x: + a = u + else: + b = u + if fu <= fw or w == x: + v = w + fv = fw + w = u + fw = fu + elif fu <= fv or v == x or v == w: + v = u + fv = fu + return x diff --git a/src/nns/_rrng.py b/src/nns/_rrng.py index fd1f8f79..a925979e 100644 --- a/src/nns/_rrng.py +++ b/src/nns/_rrng.py @@ -76,6 +76,31 @@ def unif_rand(self) -> float: def runif(self, low: float = 0.0, high: float = 1.0) -> float: return low + (high - low) * self.unif_rand() + def unif_rand_array(self, size: int) -> NDArray[np.float64]: + """``size`` successive ``unif_rand()`` draws, as R's ``runif(size)`` gives. + + Draw-for-draw identical to calling :meth:`unif_rand` ``size`` times and + leaves the generator in the same state, but runs the Mersenne-Twister in + NumPy's MT19937 (the generator R uses once seeded) instead of pure Python. + """ + size = int(size) + if size <= 0: + return np.empty(0, dtype=np.float64) + bit_generator = np.random.MT19937() + bit_generator.state = { + "bit_generator": "MT19937", + "state": {"key": np.asarray(self._mt, dtype=np.uint32), "pos": self._mti}, + } + raw = bit_generator.random_raw(size) + state = bit_generator.state["state"] + self._mt = [int(value) for value in state["key"]] + self._mti = int(state["pos"]) + draws = raw.astype(np.float64) * _I2_32M1 + # R's fixup keeps every draw strictly inside (0, 1). + draws[draws <= 0.0] = 0.5 * _I2_32M1 + draws[1.0 - draws <= 0.0] = 1.0 - 0.5 * _I2_32M1 + return draws + def _rbits(self, bits: int) -> float: v = 0 n = 0 @@ -186,3 +211,10 @@ def sample( values = np.asarray(values) idx = self.sample_int(values.size, size, replace) return values[idx - 1] + + +def r_rng(seed: int | None) -> RRNG: + """R's stream after ``set.seed(seed)``; without a seed, one seeded from fresh entropy.""" + if seed is None: + return RRNG(int(np.random.SeedSequence().generate_state(1)[0])) + return RRNG(int(seed)) diff --git a/src/nns/_seas_r14.py b/src/nns/_seas_r14.py index 47349d60..9d4d307f 100644 --- a/src/nns/_seas_r14.py +++ b/src/nns/_seas_r14.py @@ -14,6 +14,7 @@ from numpy.typing import NDArray from nns._helpers import _fast_lm +from nns._rmean import r_cumsum, r_mean, r_sum F = NDArray[np.float64] IntArray = NDArray[np.int64] @@ -26,7 +27,7 @@ def _as_float_series(values: F, dispersion: str, detrend: bool) -> F: if dispersion not in {"auto", "absolute"}: raise ValueError("dispersion must be 'auto' or 'absolute'.") if dispersion == "absolute": - x -= float(np.mean(x)) + x -= r_mean(x) n = int(x.size) if detrend and n >= 5: t = np.arange(1, n + 1, dtype=np.float64) @@ -36,26 +37,29 @@ def _as_float_series(values: F, dispersion: str, detrend: bool) -> F: x -= slope * center _, slope2 = _fast_lm(t, x) residual = x - slope2 * center - if np.max(np.abs(residual - np.mean(residual))) <= ( + if np.max(np.abs(residual - r_mean(residual))) <= ( 8.0 * np.finfo(np.float64).eps * np.max(np.abs(values)) ): - x[:] = np.mean(x) + x[:] = r_mean(x) return x -def _gini_component(values: F) -> float: - """Unbiased Gini difference of observations, including tied observations.""" +def _gini_component(values: F) -> tuple[float, float]: + """Unbiased Gini difference of observations, including tied observations, + and the component's sum, both accumulated over the values in ascending order + as R's C++ ``seas_score`` visits them.""" n = int(values.size) - if n < 2: - return 0.0 sorted_values = np.sort(values, kind="stable") - if sorted_values[0] == sorted_values[-1]: - return 0.0 + total = 0.0 + for value in sorted_values: + total += float(value) + if n < 2 or sorted_values[0] == sorted_values[-1]: + return 0.0, total first = float(sorted_values[0]) acc = 0.0 for i, value in enumerate(sorted_values): acc += (2.0 * i + 1.0 - n) * (float(value) - first) - return max(0.0, 2.0 * acc / (n * (n - 1.0))) + return max(0.0, 2.0 * acc / (n * (n - 1.0))), total def _lag_score(x: F, lag: int, use_cv: bool) -> float: @@ -64,9 +68,9 @@ def _lag_score(x: F, lag: int, use_cv: bool) -> float: acc = 0.0 for j in range(lag): values = x[j::lag] - g = _gini_component(values) + g, total = _gini_component(values) if use_cv and g != 0.0: - score = g / abs(float(np.mean(values))) + score = g / abs(total / float(values.size)) else: score = g acc += values.size * score @@ -88,11 +92,12 @@ def _delete_one_scores(x: F, lag: int, use_cv: bool) -> F | None: k = obs.size order = np.argsort(obs, kind="stable") sorted_obs = obs[order] - cs = np.cumsum(sorted_obs, dtype=np.float64) + # R's cumsum() and sum() accumulate in long double. + cs = r_cumsum(sorted_obs) total = float(cs[-1]) ranks = np.arange(1, k + 1, dtype=np.float64) contributions = sorted_obs * (2.0 * ranks - k) + total - 2.0 * cs - sum_contributions = float(np.cumsum(contributions)[-1]) + sum_contributions = r_sum(contributions) g = sum_contributions / (k * (k - 1.0)) full = g / abs(total / k) if use_cv and g != 0.0 else g g_del = (sum_contributions - 2.0 * contributions) / ((k - 1.0) * (k - 2.0)) @@ -107,7 +112,7 @@ def _delete_one_scores(x: F, lag: int, use_cv: bool) -> F | None: reordered[order] = del_values component_full[j] = full component_del.append(reordered) - weighted = float(np.sum(sizes * component_full)) + weighted = r_sum(sizes * component_full) for j in range(lag): k = sizes[j] output[j::lag] = (weighted - k * component_full[j] + (k - 1.0) * component_del[j]) / ( @@ -137,7 +142,8 @@ def _fundamental(x: F, periods: IntArray, scores: F, use_cv: bool) -> tuple[IntA if jack is None: continue diff = jack - denom - se = math.sqrt((x.size - 1.0) / x.size * float(np.sum((diff - np.mean(diff)) ** 2))) + centred = diff - r_mean(diff) + se = math.sqrt((x.size - 1.0) / x.size * r_sum(centred * centred)) if math.isfinite(se) and scores[idx] <= scores[0] + 2.0 * se: order = np.r_[idx, np.arange(periods.size)[np.arange(periods.size) != idx]] return periods[order], scores[order] diff --git a/src/nns/arma.py b/src/nns/arma.py index a4f63277..5c11e6e0 100644 --- a/src/nns/arma.py +++ b/src/nns/arma.py @@ -9,7 +9,7 @@ from nns._helpers import _fast_lm from nns._native import honours_ncores, native_fn -from nns._rmean import r_mean +from nns._rmean import r_mean, r_sd, r_sum from nns.co_moments import co_lpm, co_upm from nns.dependence import _gravity from nns.mc import nns_mc @@ -526,7 +526,8 @@ def _finish( if not negative_values: # Honour negative_values like the linear branch. regression_estimates = np.maximum(0.0, regression_estimates) - nonlin_estimate = float(np.sum(regression_estimates * lag_weights)) + # R: sum(Regression.Estimates * Weights), accumulated in long double. + nonlin_estimate = r_sum(regression_estimates * lag_weights) lin_estimate = math.nan # R also runs the linear fits for method="nonlin" when pred.int is set: the @@ -543,7 +544,7 @@ def _finish( else: linear_estimates = means - lin_estimate = float(np.sum(linear_estimates * lag_weights)) + lin_estimate = r_sum(linear_estimates * lag_weights) if not negative_values: lin_estimate = float(np.maximum(0.0, lin_estimate)) lin_regression_estimates = linear_estimates @@ -553,7 +554,7 @@ def _finish( if method_l == "lin": estimate = lin_estimate elif method_l == "both": - estimate = float(np.mean(np.array([lin_estimate, nonlin_estimate], dtype=np.float64))) + estimate = r_mean(np.array([lin_estimate, nonlin_estimate], dtype=np.float64)) elif method_l == "nonlin": estimate = nonlin_estimate else: @@ -781,9 +782,10 @@ def _as_variable(variable: NDArray[np.float64]) -> NDArray[np.float64]: def _lin_residual(lin_regression_estimates: NDArray[np.float64]) -> float: # R: lin.resid <- mean(abs(Lin.Regression.Estimates - mean(Lin.Regression.Estimates))) - lin_mean = float(np.mean(lin_regression_estimates)) - lin_resid = float(np.mean(np.abs(lin_regression_estimates - lin_mean))) - return lin_resid if np.isfinite(lin_resid) else 0.0 + # with R's mean(); then lin.resid[is.na(lin.resid)] <- 0. + lin_mean = r_mean(lin_regression_estimates) + lin_resid = r_mean(np.abs(lin_regression_estimates - lin_mean)) + return 0.0 if math.isnan(lin_resid) else lin_resid def _with_prediction_intervals( @@ -795,10 +797,11 @@ def _with_prediction_intervals( ) -> NDArray[np.float64] | dict[str, NDArray[np.float64]]: if pred_int is None: return estimates - if estimates.size == 1: - # R fails here ("incorrect number of dimensions"). NNS.meboot of a single value - # returns the value itself, so the band reduces to estimate +/- lin.resid. - intervals = estimates.reshape(1, 1) + finite_estimates = estimates[~np.isnan(estimates)] + if estimates.size < 2 or np.unique(finite_estimates).size < 2: + # R 14: a one-point (h = 1) or constant path has no dispersion to resample, + # so the band is the path widened by the per-lag spread alone. + intervals = estimates.reshape(-1, 1) else: mc_result = nns_mc( estimates, @@ -928,7 +931,7 @@ def _user_lag_weights( f"weights has {lag_weights.size} element(s) but there are {lags.size} seasonal " f"lag(s) {lags.tolist()}; supply one weight per lag." ) - total = float(np.sum(lag_weights)) + total = r_sum(lag_weights) if not np.all(np.isfinite(lag_weights)) or total == 0.0: raise ValueError("weights must be finite and must not sum to 0.") return lag_weights / total @@ -942,12 +945,13 @@ def _numeric_seasonal_weights( for index, lag in enumerate(lags): # Numeric seasonal_factor entries are actual lag values. + # R: variable[seq(length(variable), 1, -lag)], with R's sd() and mean(). rev_var = variable[:: -int(lag)] with np.errstate(invalid="ignore", divide="ignore"): - output[index] = abs(np.float64(np.std(rev_var, ddof=1)) / np.float64(np.mean(rev_var))) + output[index] = abs(np.float64(r_sd(rev_var)) / np.float64(r_mean(rev_var))) with np.errstate(invalid="ignore", divide="ignore"): - baseline_cv = abs(np.float64(np.std(variable, ddof=1)) / np.float64(np.mean(variable))) + baseline_cv = abs(np.float64(r_sd(variable)) / np.float64(r_mean(variable))) relative = output / baseline_cv # A perfectly stable lag-subsample in an otherwise-varying series has CV 0, so # relative 0 -> 1/relative = Inf -> Inf/Inf = NaN in the normalisation below, @@ -958,7 +962,7 @@ def _numeric_seasonal_weights( seasonal_weighting = 1.0 / np.maximum(relative, floor) observation_weighting = 1.0 / np.sqrt(lags.astype(np.float64)) - denom = float(np.sum(observation_weighting * seasonal_weighting)) + denom = r_sum(observation_weighting * seasonal_weighting) with np.errstate(invalid="ignore", divide="ignore"): weights = (seasonal_weighting * observation_weighting) / denom if not np.all(np.isfinite(weights)): @@ -981,10 +985,16 @@ def _arma_seas_weighting( lags = periods.astype(np.int64, copy=True) observation_weighting = 1.0 / np.sqrt(lags.astype(np.float64)) - m = min(coefficient.size, variable_coefficient.size, observation_weighting.size) - lag_weighting = variable_coefficient[:m] - coefficient[:m] + if coefficient.size == 1 and np.isnan(coefficient[0]): + # R's C++: a single NA coefficient of variation weights every lag by 1. + m = min(variable_coefficient.size, observation_weighting.size) + lag_weighting = np.ones(m, dtype=np.float64) + else: + m = min(coefficient.size, variable_coefficient.size, observation_weighting.size) + lag_weighting = variable_coefficient[:m] - coefficient[:m] weights_product = lag_weighting * observation_weighting[:m] - denom = float(np.sum(weights_product)) + # R's C++ ARMA_seas_weighting sums the products sequentially in double. + denom = float(np.cumsum(weights_product)[-1]) if weights_product.size else 0.0 with np.errstate(invalid="ignore", divide="ignore"): lag_weights = weights_product / denom if lag_weights.size and (np.all(lag_weights == 0.0) or not np.all(np.isfinite(lag_weights))): @@ -1136,7 +1146,8 @@ def _linear_forecast_for_lag( def _means_forecast_for_lag(component_series: NDArray[np.float64]) -> float: - return float(np.mean(component_series)) + # R: sapply(Component.series, mean), with R's long-double mean(). + return r_mean(component_series) def _nonlinear_forecast_for_lag( @@ -1154,16 +1165,18 @@ def _nonlinear_forecast_for_lag( ) x = np.asarray(reg_points_raw["x"], dtype=np.float64) y = np.asarray(reg_points_raw["y"], dtype=np.float64) - keep = np.isfinite(x) & np.isfinite(y) + keep = ~(np.isnan(x) | np.isnan(y)) x = x[keep] y = y[keep] - xs = x[-1] - x - ys = y[-1] - y - xs = xs[:-1] - ys = ys[:-1] - if xs.size == 0: - return last_y - weights = np.arange(1, xs.size + 1, dtype=np.int64) ** 2 - run = float(np.mean(np.repeat(xs, weights))) - rise = float(np.mean(np.repeat(ys, weights))) - return last_y + (rise / run) + xs = (x[-1] - x)[:-1] + ys = (y[-1] - y)[:-1] + # R 14: squared-index weighted means of the run and rise, sum(w * xs) / sum(w) + # with w = (1:length(xs))^2 and R's long-double sum(). With no run left, R's + # 1:0 makes w = c(1, 0) and the step 0 / 0. + weights = np.arange(1, xs.size + 1, dtype=np.float64) if xs.size else np.array([1.0, 0.0]) + weights = weights * weights + total = r_sum(weights) + run = r_sum(weights[: xs.size] * xs) / total + rise = r_sum(weights[: ys.size] * ys) / total + with np.errstate(divide="ignore", invalid="ignore"): + return float(last_y + np.float64(rise) / np.float64(run)) diff --git a/src/nns/mc.py b/src/nns/mc.py index 6ceadda3..be29db5d 100644 --- a/src/nns/mc.py +++ b/src/nns/mc.py @@ -1,12 +1,15 @@ from __future__ import annotations +import math from collections import OrderedDict from typing import Any import numpy as np from numpy.typing import NDArray -from nns.meboot import nns_meboot +from nns._rmean import r_pow, r_row_means, r_seq_by +from nns._rrng import r_rng +from nns.meboot import _nns_meboot def nns_mc( @@ -25,87 +28,138 @@ def nns_mc( random_seed: int | None = None, **kwargs: Any, ) -> dict[str, object]: - """Monte Carlo sampling over NNS.meboot's rho space. - - A vector ``target_drift`` / ``target_drift_scale`` is recycled along the rho grid - as in R (``mapply``); it cannot be longer than the grid. As in R, a given - ``target_drift`` takes precedence and ``target_drift_scale`` is then ignored. + """Monte Carlo sampling over NNS.meboot's rho space, matching R's NNS.MC. + + With ``random_seed = s`` the replicates are R's after ``set.seed(s)`` (see + :func:`nns.nns_meboot`). A vector ``target_drift`` / ``target_drift_scale`` is + recycled along the rho grid as in R (``mapply``); it cannot be longer than the + grid. As in R, a given ``target_drift`` takes precedence and + ``target_drift_scale`` is then ignored. Extra keyword arguments go to + ``nns_meboot``. """ - exp_rhos = _generate_mc_rhos(lower_rho, upper_rho, by, exp) + if not _scalar_finite(exp) or float(exp) <= 0.0: + raise ValueError("exp must be a single finite number > 0.") + if ( + not _scalar_finite(lower_rho) + or not _scalar_finite(upper_rho) + or float(lower_rho) < -1.0 + or float(upper_rho) > 1.0 + or float(lower_rho) > float(upper_rho) + ): + raise ValueError("lower_rho and upper_rho must satisfy -1 <= lower_rho <= upper_rho <= 1.") + if not _scalar_finite(by) or float(by) <= 0.0: + raise ValueError("by must be a single finite number > 0.") + + exp_rhos = _generate_mc_rhos(float(lower_rho), float(upper_rho), float(by), float(exp)) if target_drift is not None: target_drift_scale = None for name, value in (("target_drift", target_drift), ("target_drift_scale", target_drift_scale)): if value is not None and np.asarray(value).size > exp_rhos.size: raise ValueError(f"{name} must not have more elements than the rho grid.") - meboot_result = nns_meboot( - x=np.asarray(x, dtype=np.float64), - reps=reps, - rho=exp_rhos, - type=type, - drift=drift, - target_drift=target_drift, - target_drift_scale=target_drift_scale, - xmin=xmin, - xmax=xmax, - random_seed=random_seed, - **kwargs, + values = np.asarray(x, dtype=np.float64) + if values.ndim != 1: + raise ValueError("x must be a 1D numeric vector.") + meboot_args: dict[str, Any] = { + "trim": 0.10, + "reachbnd": True, + "expand_sd": True, + "force_clt": True, + "scl_adjustment": False, + "sym": False, + "digits": 6, + "expand_residual_sd": True, + } + kwargs.pop("elaps", None) + unknown = set(kwargs) - set(meboot_args) + if unknown: + raise TypeError(f"unexpected NNS.meboot argument(s): {', '.join(sorted(unknown))}") + meboot_args.update(kwargs) + meboot_result = _nns_meboot( + values, + reps, + exp_rhos, + type, + True if target_drift is not None or target_drift_scale is not None else drift, + target_drift, + target_drift_scale, + meboot_args["trim"], + xmin, + xmax, + meboot_args["reachbnd"], + meboot_args["expand_sd"], + meboot_args["force_clt"], + meboot_args["scl_adjustment"], + meboot_args["sym"], + meboot_args["digits"], + meboot_args["expand_residual_sd"], + r_rng(random_seed), ) - if isinstance(meboot_result, dict): - result_list = [meboot_result] - else: - result_list = meboot_result - + result_list = [meboot_result] if isinstance(meboot_result, dict) else meboot_result replicates: OrderedDict[str, NDArray[np.float64]] = OrderedDict() matrices: list[NDArray[np.float64]] = [] for rho_value, result in zip(exp_rhos, result_list, strict=True): matrix = np.asarray(result["replicates"], dtype=np.float64) - replicates[f"rho = {_format_r_number(rho_value)}"] = matrix + replicates[f"rho = {_format_r_number(float(rho_value))}"] = matrix matrices.append(matrix) if not matrices: raise ValueError("rho grid must contain at least one value.") - ensemble = np.mean(np.column_stack(matrices), axis=1) + # R: rowMeans(do.call(cbind, replicates)), accumulated in long double. + ensemble = r_row_means(np.column_stack(matrices)) return {"ensemble": ensemble, "replicates": replicates} +def _scalar_finite(value: object) -> bool: + """R's ``is.numeric(z) && length(z) == 1L && is.finite(z)``.""" + if isinstance(value, (bool, np.bool_)): + return False + array = np.asarray(value) + if array.size != 1 or not np.issubdtype(array.dtype, np.number): + return False + return bool(np.isfinite(array.reshape(-1)[0])) + + def _generate_mc_rhos( lower_rho: float, upper_rho: float, by: float, exp: float, ) -> NDArray[np.float64]: - if by == 0.0: - raise ValueError("'by' must be non-zero.") - rhos = _r_seq(lower_rho, upper_rho, by) + rhos = np.array(r_seq_by(lower_rho, upper_rho, by), dtype=np.float64) # Warp each side of 0 (or of the bound nearest 0) within its own span, so the grid # stays inside the bounds. if exp != 1.0: pivot = min(max(0.0, lower_rho), upper_rho) low = rhos <= pivot if pivot > lower_rho: - w = ((pivot - rhos[low]) / (pivot - lower_rho)) ** exp + w = r_pow((pivot - rhos[low]) / (pivot - lower_rho), exp) rhos[low] = (1.0 - w) * pivot + w * lower_rho if upper_rho > pivot: - w = ((rhos[~low] - pivot) / (upper_rho - pivot)) ** (1.0 / exp) + w = r_pow((rhos[~low] - pivot) / (upper_rho - pivot), 1.0 / exp) rhos[~low] = (1.0 - w) * pivot + w * upper_rho return np.asarray(rhos[::-1], dtype=np.float64) -def _r_seq(start: float, stop: float, step: float) -> NDArray[np.float64]: - span = stop - start - if span == 0.0: - return np.array([start], dtype=np.float64) - if span * step < 0.0: - return np.array([], dtype=np.float64) - # R's seq.default: a fuzz of 1e-10 on the count, and the end clamped to stop. - count = int(np.floor(span / step + 1e-10)) + 1 - values = start + step * np.arange(count, dtype=np.float64) - return np.minimum(values, stop) if step > 0.0 else np.maximum(values, stop) - - def _format_r_number(value: float) -> str: + """R's ``as.character()`` of a double: 15 significant digits, in fixed or + scientific notation, whichever is narrower (fixed on a tie).""" if value == 0.0: - value = 0.0 - return f"{value:.15g}" + return "0" + if math.isnan(value): + return "NaN" + if math.isinf(value): + return "Inf" if value > 0 else "-Inf" + mantissa, exponent_text = f"{value:.14e}".split("e") + exponent = int(exponent_text) + digits = mantissa.lstrip("-").replace(".", "").rstrip("0") + nsig = max(1, len(digits)) + neg = 1 if value < 0 else 0 + rgt = max(0, nsig - exponent - 1) + width_fixed = neg + (exponent + 1 if exponent >= 0 else 1) + (rgt + 1 if rgt > 0 else 0) + width_sci = neg + (nsig + 1 if nsig > 1 else 1) + (4 if abs(exponent) < 100 else 5) + if width_fixed <= width_sci: + return f"{value:.{rgt}f}" + sign = "+" if exponent >= 0 else "-" + return f"{float(mantissa):.{nsig - 1}f}e{sign}{abs(exponent):02d}" diff --git a/src/nns/meboot.py b/src/nns/meboot.py index ae0eef77..1f6aea47 100644 --- a/src/nns/meboot.py +++ b/src/nns/meboot.py @@ -1,5 +1,20 @@ +"""NNS.meboot: maximum-entropy bootstrap on detrended residuals. + +The port follows R 14.0's ``NNS.meboot`` step for step, including its random +draws: with ``random_seed = s`` the replicates equal R's after ``set.seed(s)``. +R draws only through ``runif`` (one per observation per replicate in the C++ +``NNS.meboot.part``, then one per shrunken column in each ``expand.sd`` pass), +which :class:`nns._rrng.RRNG` reproduces. The deterministic steps use R's own +arithmetic where the last bit can matter: long-double ``mean``/``sum``/ +``colSums``/``rowMeans``, R's ``sd``, ``rank`` and trimmed ``mean``, the C++ +helpers' sequential double sums, ``optimize`` (Brent's ``fmin``) for the +dependence target, ``qnorm`` for the CLT spread, and ``round``. +""" + from __future__ import annotations +import math +import sys import warnings from typing import NotRequired, TypedDict @@ -7,9 +22,22 @@ from numpy.typing import NDArray from nns._helpers import _fast_lm -from nns._rmean import r_mean +from nns._rmean import ( + r_mean, + r_rank_average, + r_round_array, + r_row_means, + r_sd, + r_sum, + r_trimmed_mean, +) +from nns._rnmath import qnorm +from nns._roptim import r_optimize +from nns._rrng import RRNG, r_rng from nns.dependence import nns_dep +_EPS = sys.float_info.epsilon + class MebootResult(TypedDict): """``nns_meboot`` per-``rho`` result following R's NNS.meboot diagnostics. @@ -51,16 +79,21 @@ def nns_meboot( elaps: bool = False, digits: int = 6, random_seed: int | None = None, + expand_residual_sd: bool = True, ) -> MebootResult | list[MebootResult]: - """Maximum-entropy bootstrap matching R's NNS.meboot structure. + """Maximum-entropy bootstrap matching R's NNS.meboot. - Stochastic draws use NumPy's RNG, so exact replicate parity with R is not - expected. Deterministic diagnostics follow the installed R algorithm. + With ``random_seed = s`` the replicates are R's after ``set.seed(s)``: the + draws come from R's Mersenne-Twister stream. Without a seed the stream is + seeded from fresh entropy. ``rho``, ``target_drift`` and ``target_drift_scale`` are vectorised like R's - ``Vectorize``/``mapply``: given arguments are recycled elementwise to the longest - length (not crossed) and a list with one result per position is returned. If all - of them are scalars (or length 1) a single result is returned. + ``Vectorize``/``mapply``: the given arguments are recycled elementwise to the + longest length (not crossed) and a list with one result per position is + returned; if all of them have length 1 a single result is returned, and if + none is given there is nothing to map over and, as in R, the result is empty. + Given only ``target_drift``/``target_drift_scale``, the replicates are drawn + without a dependence target. When the detrended residuals are all exactly zero (a constant or exactly linear series) there is nothing to align and ``rho`` has no effect. @@ -69,52 +102,88 @@ def nns_meboot( values = np.asarray(x, dtype=np.float64) if values.ndim != 1: raise ValueError("x must be a 1D numeric vector.") - if values.size == 0: - raise ValueError("x must be non-empty.") - if np.any(np.isnan(values)): - raise ValueError("You have some missing values, please address.") - if not np.all(np.isfinite(values)): - raise ValueError("x must contain only finite values.") - if values.size == 1: - return {"x": values.copy()} - if rho is None: - return {} - if reps < 1: - raise ValueError("reps must be positive.") + return _nns_meboot( + values, + reps, + rho, + type, + drift, + target_drift, + target_drift_scale, + trim, + xmin, + xmax, + reachbnd, + expand_sd, + force_clt, + scl_adjustment, + sym, + digits, + expand_residual_sd, + r_rng(random_seed), + ) - rng = np.random.default_rng(random_seed) - rho_values = np.asarray(rho, dtype=np.float64).reshape(-1) + +def _nns_meboot( + values: NDArray[np.float64], + reps: int, + rho: float | list[float] | np.ndarray | None, + type_: str, + drift: bool, + target_drift: float | list[float] | np.ndarray | None, + target_drift_scale: float | list[float] | np.ndarray | None, + trim: float | None, + xmin: float | None, + xmax: float | None, + reachbnd: bool, + expand_sd: bool, + force_clt: bool, + scl_adjustment: bool, + sym: bool, + digits: int, + expand_residual_sd: bool, + rng: RRNG, +) -> MebootResult | list[MebootResult]: + """R's ``Vectorize``d ``NNS.meboot`` on one shared random stream.""" + rho_values = _optional_vector(rho) drift_values = _optional_vector(target_drift) scale_values = _optional_vector(target_drift_scale) vectorised = [v for v in (rho_values, drift_values, scale_values) if v is not None] + if not vectorised: + # mapply() over no vectorised argument: R returns list(). + return {} count = 0 if any(v.size == 0 for v in vectorised) else max(v.size for v in vectorised) if any(count % v.size for v in vectorised if v.size): warnings.warn( - "longer argument not a multiple of length of shorter", RuntimeWarning, stacklevel=2 + "longer argument not a multiple of length of shorter", RuntimeWarning, stacklevel=3 ) - results = [ - _nns_meboot_one( - values, - reps, - float(rho_values[i % rho_values.size]), - type, - drift, - None if drift_values is None else float(drift_values[i % drift_values.size]), - None if scale_values is None else float(scale_values[i % scale_values.size]), - trim, - xmin, - xmax, - reachbnd, - expand_sd, - force_clt, - scl_adjustment, - sym, - digits, - rng, - ) - for i in range(count) - ] + # R lets NaN/Inf arise silently (a degenerate replicate's sd ratios, say); so + # does the port, rather than emitting NumPy floating-point warnings. + with np.errstate(divide="ignore", invalid="ignore", over="ignore"): + results = [ + _meboot_call( + values, + reps, + None if rho_values is None else float(rho_values[i % rho_values.size]), + type_.lower(), + drift, + None if drift_values is None else float(drift_values[i % drift_values.size]), + None if scale_values is None else float(scale_values[i % scale_values.size]), + trim, + xmin, + xmax, + reachbnd, + expand_sd, + force_clt, + scl_adjustment, + sym, + digits, + expand_residual_sd, + rng, + ) + for i in range(count) + ] if count == 1: return results[0] return results @@ -126,15 +195,15 @@ def _optional_vector(value: float | list[float] | np.ndarray | None) -> NDArray[ return np.asarray(value, dtype=np.float64).reshape(-1) -def _nns_meboot_one( +def _meboot_call( x: NDArray[np.float64], reps: int, - rho: float, + rho: float | None, type_: str, drift: bool, target_drift: float | None, target_drift_scale: float | None, - trim: float, + trim: float | None, xmin_arg: float | None, xmax_arg: float | None, reachbnd: bool, @@ -143,9 +212,24 @@ def _nns_meboot_one( scl_adjustment: bool, sym: bool, digits: int, - rng: np.random.Generator, + expand_residual_sd: bool, + rng: RRNG, ) -> MebootResult: + """One ``NNS.meboot`` call (one ``rho`` / drift target).""" n = x.size + if n == 1: + return {"x": x.copy()} + if n == 0: + raise ValueError("x must be non-empty.") + if np.any(np.isnan(x)): + raise ValueError("You have some missing values, please address.") + if not np.all(np.isfinite(x)): + raise ValueError("x must contain only finite values.") + if reps < 1: + raise ValueError("reps must be positive.") + trimval = 0.1 if trim is None else float(trim) + + # --- Fit the linear trend once (time order) and take its residuals. time = np.arange(1, n + 1, dtype=np.float64) intercept, orig_drift = _fast_lm(time, x) orig_res = x - (intercept + orig_drift * time) @@ -156,96 +240,104 @@ def _nns_meboot_one( drift = True if drift: if target_drift_scale is not None: - target = orig_drift * target_drift_scale + target_drift = orig_drift * target_drift_scale elif target_drift is None: - target = orig_drift - else: - target = target_drift - recon_slope = target + target_drift = orig_drift + recon_slope = float(target_drift) else: recon_slope = 0.0 - keeps_drift = recon_slope == orig_drift + # R keeps the original reconstruction whenever the slope is unchanged up to + # rounding: relative to sd(x) / n (the slope of a trend spanning one sd over + # the sample) plus the slope fit's own rounding at the level of x. + slope_rounding = 8.0 * _EPS * float(np.max(np.abs(x))) / r_sd(time) + same_slope = abs(recon_slope - orig_drift) <= math.sqrt(_EPS) * max( + abs(orig_drift), r_sd(x) / n + ) + slope_rounding + if same_slope: + recon_slope = orig_drift # A changed slope pivots about mean(x), so the replicates keep the level. baseline = ( intercept + recon_slope * time - if keeps_drift + if same_slope else r_mean(x) + recon_slope * (time - (n + 1) / 2.0) ) - xx = np.sort(orig_res) - ordxx_zero = np.argsort(orig_res, kind="stable") - ordxx = ordxx_zero.astype(np.float64) + 1.0 + # --- Meboot core on the residuals: order statistics, midpoints and tails. + ordxx = np.argsort(orig_res, kind="stable") # R's order() is stable + xx = orig_res[ordxx] if sym: - xx = float(np.mean(xx)) + 0.5 * (xx - xx[::-1]) - + xx = r_mean(xx) + 0.5 * (xx - xx[::-1]) z = (xx[1:] + xx[:-1]) / 2.0 - dv = np.abs(np.diff(orig_res.astype(np.float64))) - dvtrim = _trimmed_mean(dv, trim) - xmin = float(xx[0] - dvtrim) if xmin_arg is None else float(xmin_arg) - xmax = float(xx[-1] + dvtrim) if xmax_arg is None else float(xmax_arg) + dv = np.abs(np.diff(orig_res)) + dvtrim = r_trimmed_mean(dv, trimval) + xmin = xx[0] - dvtrim if xmin_arg is None else float(xmin_arg) + xmax = xx[-1] + dvtrim if xmax_arg is None else float(xmax_arg) if (xmin_arg is not None or xmax_arg is not None) and force_clt: # R: if (isTRUE(force.clt)) { expand.sd <- FALSE; force.clt <- FALSE } force_clt = False expand_sd = False - aux = 0.25 * xx[:-2] + 0.5 * xx[1:-1] + 0.25 * xx[2:] + # Theil-Laitinen interval means: colSums of the weighted neighbours, which R + # accumulates in long double. + if n > 2: + aux_ld = (xx[2:] * 0.25).astype(np.longdouble) + xx[1:-1] * 0.5 + xx[:-2] * 0.25 + aux = np.asarray(aux_ld, dtype=np.float64) + else: + aux = np.empty(0, dtype=np.float64) desintxb = np.concatenate( - ( - np.array([0.75 * xx[0] + 0.25 * xx[1]], dtype=np.float64), - aux, - np.array([0.25 * xx[-2] + 0.75 * xx[-1]], dtype=np.float64), - ) + ([0.75 * xx[0] + 0.25 * xx[1]], aux, [0.25 * xx[-2] + 0.75 * xx[-1]]) ) - res_mat = np.column_stack( - [_meboot_part(xx, n, z, xmin, xmax, desintxb, reachbnd, rng) for _ in range(reps)] - ) - qseq = np.sort(res_mat, axis=0) - res_mat[ordxx_zero, :] = qseq + # replicate(reps, NNS.meboot.part(...)): each call draws runif(n). + draws = rng.unif_rand_array(n * reps).reshape(reps, n).T + res_mat = _meboot_part(xx, n, z, xmin, xmax, desintxb, reachbnd, draws) + res_mat[ordxx, :] = np.sort(res_mat, axis=0) + + if rho is not None and not zero_res: + res_mat = _target_rho(res_mat, orig_res, rho, type_) - if not zero_res: - res_mat = _target_rho(res_mat, orig_res, rho, type_.lower()) - res_mat = _meboot_expand_sd(orig_res, res_mat, rng) + # Variance expansion on the residuals (match the sd of the original residuals). + if expand_residual_sd: + res_mat = _meboot_expand_sd(orig_res, res_mat, rng) # The ME density lives on the residuals, so scale them against their own mean and sd. if scl_adjustment and not zero_res: zz = np.concatenate(([xmin], z, [xmax])) - v = np.diff(zz**2) / 12.0 + v = np.diff(zz * zz) / 12.0 xb = r_mean(orig_res) - s1 = float(np.sum((desintxb - xb) ** 2)) - uv = (s1 + float(np.sum(v))) / n - desired_sd = _sample_sd(orig_res) - actual_me_sd = float(np.sqrt(uv)) + s1 = r_sum((desintxb - xb) * (desintxb - xb)) + uv = (s1 + r_sum(v)) / n + desired_sd = r_sd(orig_res) + actual_me_sd = math.sqrt(uv) if actual_me_sd <= 0.0: raise ValueError("actualME.sd<=0 Error") - kappa = (desired_sd / actual_me_sd) - 1.0 - res_mat = res_mat + kappa * (res_mat - xb) + kappa_value = (desired_sd / actual_me_sd) - 1.0 + res_mat = res_mat + kappa_value * (res_mat - xb) + kappa: float | None = kappa_value else: kappa = None + # Reconstruct the levels. (R's legacy reshuffle when ordxx equals its own + # reverse can only fire for n = 1, which returned above.) ensemble = res_mat + baseline[:, np.newaxis] - if np.array_equal(ordxx_zero[::-1], ordxx_zero) and reps > 1: - for i in range(ensemble.shape[0]): - ensemble[i, :] = rng.choice(ensemble[i, :], size=reps, replace=True) - # sd(x) includes the original drift, so expand toward it only when it is kept. - if expand_sd and keeps_drift: + if expand_sd and same_slope: ensemble = _meboot_expand_sd(x, ensemble, rng) if force_clt and reps > 1: - clt_ref = x if keeps_drift else baseline + orig_res - if _sample_sd(clt_ref) > 0.0: + clt_ref = x if same_slope else baseline + orig_res + if r_sd(clt_ref) > 0.0: ensemble = _force_clt(clt_ref, ensemble) if xmin_arg is not None: - ensemble = np.maximum(float(xmin_arg), ensemble) + ensemble = np.maximum(ensemble, float(xmin_arg)) if xmax_arg is not None: - ensemble = np.minimum(float(xmax_arg), ensemble) + ensemble = np.minimum(ensemble, float(xmax_arg)) return { "x": x.copy(), - "replicates": np.round(ensemble, digits), - "ensemble": np.mean(ensemble, axis=1), + "replicates": r_round_array(ensemble, digits), + "ensemble": r_row_means(ensemble), "xx": xx, "z": z, "dv": dv, @@ -253,7 +345,7 @@ def _nns_meboot_one( "xmin": float(xmin), "xmax": float(xmax), "desintxb": desintxb, - "ordxx": ordxx, + "ordxx": ordxx.astype(np.float64) + 1.0, "kappa": kappa, } @@ -266,40 +358,34 @@ def _meboot_part( xmax: float, desintxb: NDArray[np.float64], reachbnd: bool, - rng: np.random.Generator, + p: NDArray[np.float64], ) -> NDArray[np.float64]: - p = rng.random(n) + """R's C++ ``NNS_meboot_part`` for every replicate column of the draws ``p``.""" m = xx.size if m == 0: - q = np.full(n, np.nan, dtype=np.float64) + q = np.full(p.shape, np.nan, dtype=np.float64) elif m == 1: - q = np.full(n, xx[0], dtype=np.float64) + q = np.full(p.shape, xx[0], dtype=np.float64) else: h = 1.0 + (m - 1.0) * p - j = np.floor(h).astype(np.int64) + j = np.clip(np.floor(h), 1.0, m - 1.0) g = h - j - j = np.clip(j, 1, m - 1) - q = (1.0 - g) * xx[j - 1] + g * xx[j] - q[p <= 0.0] = xx[0] - q[p >= 1.0] = xx[-1] + index = j.astype(np.intp) + q = (1.0 - g) * xx[index - 1] + g * xx[index] + q = np.where(p <= 0.0, xx[0], q) + q = np.where(p >= 1.0, xx[m - 1], q) invn = 1.0 / n - lower = p <= invn - if np.any(lower): - vals = _linear_interp(p[lower], 0.0, invn, xmin, float(z[0])) - if not reachbnd: - vals = vals + desintxb[0] - 0.5 * (z[0] + xmin) - q[lower] = vals + lower = xmin + (p - 0.0) * (z[0] - xmin) / (invn - 0.0) + if not reachbnd: + lower = lower + desintxb[0] - 0.5 * (z[0] + xmin) + q = np.where(p <= invn, lower, q) edge = (n - 1.0) / n - upper = p >= edge - if np.any(upper): - vals = _linear_interp(p[upper], edge, 1.0, float(z[n - 2]), xmax) - if not reachbnd: - vals = vals + desintxb[n - 1] - 0.5 * (z[n - 2] + xmax) - q[upper] = vals - - return q + upper = z[n - 2] + (p - edge) * (xmax - z[n - 2]) / (1.0 - edge) + if not reachbnd: + upper = upper + desintxb[n - 1] - 0.5 * (z[n - 2] + xmax) + return np.asarray(np.where(p >= edge, upper, q), dtype=np.float64) def _target_rho( @@ -308,23 +394,32 @@ def _target_rho( rho: float, type_: str, ) -> NDArray[np.float64]: - from scipy.optimize import minimize_scalar # type: ignore[import-untyped] + """Blend each replicate's aligned and anti-aligned orderings to hit ``rho``. - r_o = _rank_average(orig_res) + R mixes ``comb = t * m + (1 - t) * e`` with the single weight ``t`` from + ``optimize(obj, c(0, 1), tol = 0.01)``; the correlation is a closed form + against the target centred and normed once. + """ + n = orig_res.size + r_o = r_rank_average(orig_res) r_anti = float(np.max(r_o)) + 1.0 - r_o - r_o_idx = np.clip(np.floor(r_o).astype(np.int64) - 1, 0, orig_res.size - 1) - r_anti_idx = np.clip(np.floor(r_anti).astype(np.int64) - 1, 0, orig_res.size - 1) - out = res_mat.copy() - target_values = _rank_average(orig_res) if type_ == "spearman" else orig_res - target_centered = target_values - float(np.mean(target_values)) - target_norm = float(np.sqrt(np.sum(target_centered * target_centered))) - if target_norm == 0.0 or not np.isfinite(target_norm): + # Average ranks can be fractional under ties: floor and clamp to 1..n, as R's + # integer indexing does. + r_o_idx = np.clip(np.floor(r_o).astype(np.int64), 1, n) - 1 + r_anti_idx = np.clip(np.floor(r_anti).astype(np.int64), 1, n) - 1 + use_spearman = type_ == "spearman" + use_corr_path = type_ in {"spearman", "pearson"} + target_values = r_o if use_spearman else orig_res + target_centered = target_values - r_mean(target_values) + target_norm = math.sqrt(r_sum(target_centered * target_centered)) + if target_norm == 0.0 or not math.isfinite(target_norm): raise ValueError("function cannot be evaluated at initial parameters") - for j in range(out.shape[1]): - res_sorted = np.sort(out[:, j]) - e_values = res_sorted[r_o_idx] - m_values = res_sorted[r_anti_idx] + out = res_mat.copy() + for i in range(out.shape[1]): + res_sorted = np.sort(out[:, i]) + e_values = res_sorted[r_o_idx] # aligned with the ranks of orig_res + m_values = res_sorted[r_anti_idx] # anti-aligned def objective( t: float, @@ -332,63 +427,58 @@ def objective( m_: NDArray[np.float64] = m_values, ) -> float: comb = t * m_ + (1.0 - t) * e_ - if type_ in {"spearman", "pearson"}: - corr = _fast_corr(comb, target_centered, target_norm, type_) + if use_corr_path: + corr = _fast_corr(comb, target_centered, target_norm, use_spearman) elif type_ == "nnsdep": - corr = nns_dep(comb, orig_res)["Dependence"] + corr = float(nns_dep(comb, orig_res)["Dependence"]) else: - corr = nns_dep(comb, orig_res)["Correlation"] - if not np.isfinite(corr): - return np.inf - return abs(float(corr) - rho) - - opt = minimize_scalar( - objective, - bounds=(0.0, 1.0), - method="bounded", - options={"xatol": 0.01, "maxiter": 20}, - ) - if np.isfinite(opt.fun): - t = float(opt.x) - else: - candidates = [ - (float(candidate_value), candidate_t) - for candidate_t in (0.0, 0.5, 1.0) - if np.isfinite(candidate_value := objective(candidate_t)) - ] - if candidates: - _, t = min(candidates, key=lambda candidate: candidate[0]) - else: - # A constant bootstrap column has no defined correlation for any - # blend. Keep the rank-aligned endpoint rather than aborting the - # entire Monte Carlo ensemble; the replicate remains finite and - # is handled by the later variance/CLT adjustments. - t = 0.0 - out[:, j] = t * m_values + (1.0 - t) * e_values + corr = float(nns_dep(comb, orig_res)["Correlation"]) + if not math.isfinite(corr): + return math.inf + return abs(corr - rho) + t_hat = r_optimize(objective, 0.0, 1.0, tol=0.01) + out[:, i] = t_hat * m_values + (1.0 - t_hat) * e_values return out +def _fast_corr( + values: NDArray[np.float64], + target_centered: NDArray[np.float64], + target_norm: float, + use_spearman: bool, +) -> float: + """R's closed-form Pearson/Spearman correlation against the fixed target.""" + xv = r_rank_average(values) if use_spearman else values + xc = xv - r_mean(xv) + xn = math.sqrt(r_sum(xc * xc)) + if xn == 0.0: + return math.nan + return r_sum(xc * target_centered) / (xn * target_norm) + + def _meboot_expand_sd( x: NDArray[np.float64], ensemble: NDArray[np.float64], - rng: np.random.Generator, + rng: RRNG, fiv: float = 5.0, ) -> NDArray[np.float64]: + """R's C++ ``NNS_meboot_expand_sd`` for a vector ``x``. + + A column whose sd falls short of ``sd(x)`` is scaled up to it times a factor + drawn by ``R::runif(1, 1 + fiv / 100)``, in column order. + """ out = ensemble.copy() - sdx = np.array([_sample_sd(np.asarray(x, dtype=np.float64))], dtype=np.float64) - ens_sd = _col_sd(out) - sdf = np.concatenate((sdx, ens_sd)) + sdf = np.concatenate(([_vec_sd(np.asarray(x, dtype=np.float64))], _col_sd(out))) with np.errstate(divide="ignore", invalid="ignore"): sdfa = sdf / sdf[0] sdfd = sdf[0] / sdf - mx = 1.0 + (fiv / 100.0) - low = sdfa < 1.0 - if np.any(low): - sdfa[low] = rng.uniform(1.0, mx, size=int(np.sum(low))) - - factors = sdfd[1:] * sdfa[1:] + for i in range(sdfa.size): + if sdfa[i] < 1.0: + sdfa[i] = rng.runif(1.0, mx) + with np.errstate(invalid="ignore"): + factors = sdfd[1:] * sdfa[1:] for j, factor in enumerate(factors): if np.floor(factor) > 0.0: out[:, j] *= factor @@ -396,83 +486,47 @@ def _meboot_expand_sd( def _force_clt(x: NDArray[np.float64], ensemble: NDArray[np.float64]) -> NDArray[np.float64]: - from scipy.stats import norm # type: ignore[import-untyped] + """R's C++ ``force_clt`` for a vector ``x``. - out = ensemble.copy() - n_reps = out.shape[1] - gm = float(np.mean(x)) - smean = _sample_sd(x) / np.sqrt(n_reps) - xbar = np.mean(out, axis=0) + The replicate means are respread like means of ``n`` observations, + ``sd(x) / sqrt(n)`` with ``n`` the series length (R 14 fixed meboot's division + by the number of replicates, which shrank the spread as ``reps`` grew), at the + ``qnorm(j / (reps + 1))`` quantiles, keeping their order. + """ + n, bigj = ensemble.shape + gm = float(np.cumsum(x)[-1]) / x.size + smean = _vec_sd(x) / math.sqrt(n) + xbar = np.cumsum(ensemble, axis=0)[-1] / n order = np.argsort(xbar, kind="stable") sortxbar = np.sort(xbar) - probs = np.arange(1, n_reps + 1, dtype=np.float64) / (n_reps + 1.0) - newbar = gm + norm.ppf(probs) * smean - sd_newbar = _sample_sd(newbar) - if sd_newbar == 0.0 or not np.isfinite(sd_newbar): - return out - scn = (newbar - np.mean(newbar)) / sd_newbar - newm = scn * smean + gm - meanfix = newm - sortxbar + newbar = np.array([gm + qnorm((j + 1) / (bigj + 1)) * smean for j in range(bigj)]) + mu_nb = float(np.cumsum(newbar)[-1]) / bigj + deviations = newbar - mu_nb + sd_nb = math.sqrt(float(np.cumsum(deviations * deviations)[-1]) / (bigj - 1)) + out = ensemble.copy() for i, col in enumerate(order): - out[:, col] = ensemble[:, col] + meanfix[i] + add = (((newbar[i] - mu_nb) / sd_nb) * smean + gm) - sortxbar[i] + out[:, col] = ensemble[:, col] + add return out -def _trimmed_mean(values: NDArray[np.float64], trim: float) -> float: - if values.size == 0: - return float("nan") - ordered = np.sort(values) - cut = int(np.floor(values.size * trim)) - if cut > 0 and 2 * cut < values.size: - ordered = ordered[cut:-cut] - return float(np.mean(ordered)) - - -def _sample_sd(values: NDArray[np.float64]) -> float: - if values.size < 2: - return float("nan") - return float(np.std(values, ddof=1)) +def _vec_sd(values: NDArray[np.float64]) -> float: + """R's C++ ``vecSd``: sequential double mean and sum of squares.""" + n = values.size + if n <= 1: + return math.nan + mu = float(np.cumsum(values)[-1]) / n + deviations = values - mu + return math.sqrt(float(np.cumsum(deviations * deviations)[-1]) / (n - 1)) def _col_sd(values: NDArray[np.float64]) -> NDArray[np.float64]: - if values.shape[0] < 2: + """R's C++ ``colSd``: :func:`_vec_sd` of each column.""" + n = values.shape[0] + if n <= 1: return np.full(values.shape[1], np.nan, dtype=np.float64) - return np.asarray(np.std(values, axis=0, ddof=1), dtype=np.float64) - - -def _linear_interp( - x: NDArray[np.float64], - x0: float, - x1: float, - y0: float, - y1: float, -) -> NDArray[np.float64]: - return y0 + (x - x0) * (y1 - y0) / (x1 - x0) - - -def _fast_corr( - x: NDArray[np.float64], - target_centered: NDArray[np.float64], - target_norm: float, - method: str, -) -> float: - x_values = _rank_average(x) if method == "spearman" else x - x_centered = x_values - float(np.mean(x_values)) - x_norm = float(np.sqrt(np.sum(x_centered * x_centered))) - if x_norm == 0.0 or target_norm == 0.0: - return float("nan") - return float(np.sum(x_centered * target_centered) / (x_norm * target_norm)) - - -def _rank_average(x: NDArray[np.float64]) -> NDArray[np.float64]: - order = np.argsort(x, kind="mergesort") - sorted_x = x[order] - ranks = np.empty(x.size, dtype=np.float64) - if x.size == 0: - return ranks - group_start = np.concatenate(([0], np.flatnonzero(sorted_x[1:] != sorted_x[:-1]) + 1)) - group_end = np.concatenate((group_start[1:], [x.size])) - group_size = group_end - group_start - group_rank = 0.5 * (group_start + 1 + group_end) - ranks[order] = np.repeat(group_rank, group_size) - return ranks + mu = np.cumsum(values, axis=0)[-1] / n + deviations = values - mu + return np.asarray( + np.sqrt(np.cumsum(deviations * deviations, axis=0)[-1] / (n - 1)), dtype=np.float64 + ) diff --git a/src/nns/stochastic_superiority.py b/src/nns/stochastic_superiority.py index 47c05f2e..ddb61011 100644 --- a/src/nns/stochastic_superiority.py +++ b/src/nns/stochastic_superiority.py @@ -4,7 +4,8 @@ from numpy.typing import NDArray from nns._native import native_fn -from nns.meboot import MebootResult, nns_meboot +from nns._rrng import RRNG, r_rng +from nns.meboot import _nns_meboot from nns.var import lpm_var, upm_var @@ -17,7 +18,13 @@ def nns_ss( rho: float = 1.0, random_seed: int | None = None, ) -> dict[str, object]: - """Stochastic superiority matching R's NNS.SS.""" + """Stochastic superiority matching R's NNS.SS. + + With ``confidence_interval=True`` the bounds come from ``reps`` maximum-entropy + bootstrap replicates of each group; with ``random_seed = s`` they are R's + after ``set.seed(s)``, the replicates of ``x`` and then of ``y`` drawn from + one stream as R draws them. + """ x_values = _omit_nan_numeric(x) y_values = _omit_nan_numeric(y) if x_values.size == 0 or y_values.size == 0: @@ -34,14 +41,9 @@ def nns_ss( if ci <= 0.0 or ci >= 1.0: raise ValueError("ci must be a single number in (0, 1).") - x_seed: int | None = None - y_seed: int | None = None - if random_seed is not None: - rng = np.random.default_rng(random_seed) - x_seed, y_seed = [int(seed) for seed in rng.integers(0, np.iinfo(np.int32).max, size=2)] - - x_reps = _bootstrap(x_values, int(reps), rho, x_seed) - y_reps = _bootstrap(y_values, int(reps), rho, y_seed) + rng = r_rng(random_seed) + x_reps = _bootstrap(x_values, int(reps), rho, rng) + y_reps = _bootstrap(y_values, int(reps), rho, rng) boot_vals = np.empty(int(reps), dtype=np.float64) for index in range(int(reps)): boot_vals[index] = _stoch_superiority(x_reps[:, index], y_reps[:, index])["p_star"] @@ -77,9 +79,10 @@ def _stoch_superiority( less_count = int(np.sum(left)) tie_count = int(np.sum(right - left)) - denominator = float(xs.size * ys.size) - p_gt = float(less_count / denominator) - p_tie = float(tie_count / denominator) + # R divides the counts in long double before rounding to double. + denominator = np.longdouble(xs.size) * np.longdouble(ys.size) + p_gt = float(np.longdouble(less_count) / denominator) + p_tie = float(np.longdouble(tie_count) / denominator) return { "p_gt": p_gt, "p_tie": p_tie, @@ -93,18 +96,18 @@ def _omit_nan_numeric(x: NDArray[np.float64]) -> NDArray[np.float64]: def _bootstrap( - values: NDArray[np.float64], reps: int, rho: float, seed: int | None + values: NDArray[np.float64], reps: int, rho: float | NDArray[np.float64], rng: RRNG ) -> NDArray[np.float64]: + """R: ``NNS.meboot(x = g, reps = reps, rho = rho)["replicates", ]$replicates``.""" # A constant group's bootstrap is the constant itself. if np.all(values == values[0]): return np.tile(values[:, np.newaxis], (1, reps)) - boot = nns_meboot(values, reps=reps, rho=rho, random_seed=seed) - if not isinstance(boot, dict): - raise ValueError("NNS.meboot returned an unexpected vectorized result.") - return _replicate_matrix(boot) - - -def _replicate_matrix(result: MebootResult) -> NDArray[np.float64]: + boot = _nns_meboot( + values, reps, rho, "spearman", True, None, None, 0.10, None, None, + True, True, True, False, False, 6, True, rng, + ) + # With several rho values every call draws, and $replicates takes the first. + result = boot[0] if isinstance(boot, list) else boot replicates = np.asarray(result.get("replicates"), dtype=np.float64) if replicates.ndim != 2: raise ValueError("NNS.meboot result does not contain a replicate matrix.") diff --git a/tests/_r_cache/boot14.arma.json b/tests/_r_cache/boot14.arma.json new file mode 100644 index 00000000..c6d8ee80 --- /dev/null +++ b/tests/_r_cache/boot14.arma.json @@ -0,0 +1,93 @@ +{ + "entries": { + "3f4b4db732742508de670d5173d6ba56c520a8c7ccc9b4fa01c46c39a297bbb3": { + "Estimates": [ + 10.366666666666667, + 12.053333333333333, + 11.966666666666665, + 11.620000000000001, + 13.18 + ], + "Lower 90% pred.int": [ + 9.082813550000003, + 9.266971450000002, + 10.158733750000001, + 10.364109950000001, + 11.303198800000002 + ], + "Upper 90% pred.int": [ + 12.351069899999999, + 13.497514349999998, + 13.410923249999998, + 14.4392868, + 14.57064525 + ] + }, + "bbd6c96c63ae69e2274b5f4b57d30abddfba539cfe915c3dbda05d882b9241ed": { + "Estimates": [ + 10.016666666666666, + 11.633333333333333, + 12.766666666666666, + 13.15, + 12.45, + 11.65 + ], + "Lower 90% pred.int": [ + 9.194961800000002, + 10.03610795, + 10.26455905, + 10.160770499999998, + 11.031115799999998, + 11.2505928 + ], + "Upper 90% pred.int": [ + 13.46455825, + 12.6920494, + 13.33215445, + 14.1364298, + 13.5827122, + 14.1038085 + ] + }, + "c3d454715f2e09596b29259604f2da2d683048c9ac48eda27737c177621dc7f3": { + "Estimates": [ + 10.17241105305223, + 11.281468520710014, + 11.319240519823481 + ], + "Lower 90% pred.int": [ + 8.214668478787878, + 8.67656467878788, + 9.361498078787879 + ], + "Upper 90% pred.int": [ + 12.48804652121212, + 13.21180392121212, + 13.63487592121212 + ] + }, + "f1f548c38809b0484cf5e118669ff56cde61b9920eb5a9a4f2c021ad1b9434ba": { + "Estimates": [ + 10.810858585858584, + 14.087626262626262, + 13.913333333333332, + 11.983333333333333 + ], + "Lower 90% pred.int": [ + 9.4193647, + 9.67830865, + 10.805505799999999, + 10.99008275 + ], + "Upper 90% pred.int": [ + 15.576683599999999, + 15.5352748, + 14.800258699999999, + 14.9864289 + ] + } + }, + "label": "boot14.arma", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/boot14.labels.json b/tests/_r_cache/boot14.labels.json new file mode 100644 index 00000000..04fbd428 --- /dev/null +++ b/tests/_r_cache/boot14.labels.json @@ -0,0 +1,25 @@ +{ + "entries": { + "e62495af496298f0c07abf1b21bbe3d92727489623351e83fcd4ec0a95fff759": [ + "1e-04", + "5e-04", + "0.0001234", + "0.1", + "0.333333333333333", + "1e+05", + "123456", + "-0.99", + "0.3", + "1e-06", + "0.001", + "2.5e-05", + "0.123456789012346", + "-0.6", + "0", + "-1" + ] + }, + "label": "boot14.labels", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/boot14.mc.json b/tests/_r_cache/boot14.mc.json new file mode 100644 index 00000000..9dd637df --- /dev/null +++ b/tests/_r_cache/boot14.mc.json @@ -0,0 +1,2809 @@ +{ + "entries": { + "0b2c4e94219869439e6a94056026a7c8b2b5d6d2cd79a422bd0f6de8e6b3b80b": { + "ensemble": [ + 10.146960857142858, + 10.193208714285714, + 11.320651547619049, + 11.160095095238095, + 11.206459357142856, + 11.460737642857143, + 11.005956023809524, + 11.376847595238095, + 11.82635442857143, + 12.142184404761904, + 12.65710619047619, + 12.980079071428571, + 11.926698571428572, + 12.51902819047619, + 13.039598023809523, + 12.977663880952381, + 13.112759619047619, + 14.061561857142857, + 14.024031571428571, + 13.670758571428571, + 14.521175452380952, + 14.067941261904762, + 14.761099476190477, + 14.685037523809523 + ], + "names": [ + "rho = 0.8", + "rho = 0.6125", + "rho = 0.45", + "rho = 0.3125", + "rho = 0.2", + "rho = 0.1125", + "rho = 0.05", + "rho = 0.0125", + "rho = 0", + "rho = -0.223606797749979", + "rho = -0.316227766016838", + "rho = -0.387298334620742", + "rho = -0.447213595499958", + "rho = -0.5" + ], + "reps": [ + [ + [ + 9.472165, + 10.426823, + 8.638927 + ], + [ + 10.557952, + 12.490011, + 10.723805 + ], + [ + 11.223972, + 13.938705, + 13.179942 + ], + [ + 13.720973, + 12.960518, + 13.460119 + ], + [ + 10.685036, + 10.355336, + 10.484389 + ], + [ + 10.350765, + 11.21174, + 10.056249 + ], + [ + 10.709186, + 11.25552, + 9.536161 + ], + [ + 9.216432, + 11.760895, + 10.383123 + ], + [ + 11.814542, + 13.032539, + 11.320161 + ], + [ + 11.535962, + 13.820828, + 13.852276 + ], + [ + 11.350826, + 12.381764, + 12.149948 + ], + [ + 8.119094, + 12.376803, + 11.31813 + ], + [ + 9.731402, + 5.686856, + 11.999959 + ], + [ + 12.019535, + 12.952412, + 11.264797 + ], + [ + 10.589774, + 12.190914, + 12.085049 + ], + [ + 17.079165, + 16.980589, + 15.759341 + ], + [ + 17.88693, + 13.388325, + 19.707218 + ], + [ + 12.794733, + 13.900999, + 13.539207 + ], + [ + 13.387376, + 14.937197, + 13.066335 + ], + [ + 11.533697, + 14.148791, + 12.577489 + ], + [ + 13.626097, + 14.624116, + 13.635576 + ], + [ + 14.46955, + 15.731058, + 13.760794 + ], + [ + 13.704409, + 14.802698, + 12.807245 + ], + [ + 13.826742, + 16.926232, + 15.537754 + ] + ], + [ + [ + 8.892261, + 8.11178, + 9.204416 + ], + [ + 10.439499, + 9.616122, + 10.932798 + ], + [ + 14.445968, + 12.745749, + 13.979221 + ], + [ + 14.681403, + 13.017769, + 9.712916 + ], + [ + 11.980902, + 9.959516, + 6.914267 + ], + [ + 10.725082, + 11.174695, + 10.086907 + ], + [ + 10.15417, + 9.30644, + 10.48776 + ], + [ + 10.535971, + 10.130997, + 11.369121 + ], + [ + 11.493456, + 10.646913, + 11.800493 + ], + [ + 12.35854, + 14.765222, + 13.381167 + ], + [ + 12.700931, + 12.382519, + 11.897008 + ], + [ + 12.358239, + 11.090469, + 11.561976 + ], + [ + 11.288088, + 10.668687, + 6.395405 + ], + [ + 11.734159, + 10.733434, + 11.880789 + ], + [ + 11.645831, + 13.398259, + 12.659642 + ], + [ + 18.059477, + 17.947137, + 16.489781 + ], + [ + 19.566105, + 18.224672, + 12.738325 + ], + [ + 14.179288, + 13.84876, + 13.31454 + ], + [ + 13.638941, + 13.174507, + 13.610154 + ], + [ + 14.454017, + 14.402969, + 13.070543 + ], + [ + 14.218945, + 14.316278, + 13.853233 + ], + [ + 14.219824, + 13.265437, + 14.561415 + ], + [ + 13.572806, + 13.240344, + 13.422787 + ], + [ + 14.937768, + 14.675318, + 16.081655 + ] + ], + [ + [ + 10.762694, + 8.454839, + 9.233053 + ], + [ + 12.895637, + 9.906868, + 10.118839 + ], + [ + 11.474044, + 11.816209, + 9.894652 + ], + [ + 12.854036, + 11.862227, + 12.477148 + ], + [ + 10.951563, + 10.527883, + 10.82576 + ], + [ + 9.41687, + 10.749102, + 12.403766 + ], + [ + 9.60136, + 9.595263, + 8.904246 + ], + [ + 11.893652, + 10.884859, + 9.979301 + ], + [ + 13.049328, + 10.651341, + 11.395504 + ], + [ + 14.49573, + 12.822565, + 11.636189 + ], + [ + 10.694619, + 12.062349, + 13.423371 + ], + [ + 10.928032, + 11.929581, + 9.154884 + ], + [ + 15.76738, + 15.323144, + 12.343995 + ], + [ + 13.592941, + 11.260656, + 11.133101 + ], + [ + 13.524058, + 12.42694, + 11.172333 + ], + [ + 13.465241, + 15.502059, + 11.883324 + ], + [ + 20.324144, + 20.772208, + 18.975519 + ], + [ + 12.131222, + 13.50181, + 14.780576 + ], + [ + 14.453694, + 12.702193, + 13.596979 + ], + [ + 11.750431, + 12.837558, + 9.599785 + ], + [ + 12.577311, + 13.849689, + 15.37233 + ], + [ + 13.873908, + 13.159117, + 12.789012 + ], + [ + 14.748696, + 12.996992, + 13.924176 + ], + [ + 17.055081, + 15.248543, + 14.388474 + ] + ], + [ + [ + 9.171455, + 9.399617, + 10.796491 + ], + [ + 11.47362, + 10.828617, + 12.360467 + ], + [ + 13.840659, + 7.914076, + 11.994092 + ], + [ + 14.020524, + 11.37123, + 11.863192 + ], + [ + 12.456232, + 10.551631, + 10.516444 + ], + [ + 11.829377, + 12.376851, + 11.709653 + ], + [ + 10.38944, + 11.178446, + 12.061703 + ], + [ + 13.161663, + 12.620563, + 9.497483 + ], + [ + 11.302038, + 11.295077, + 13.064791 + ], + [ + 15.306447, + 12.507434, + 11.767745 + ], + [ + 12.551434, + 13.130238, + 15.082303 + ], + [ + 13.92378, + 8.338695, + 12.30391 + ], + [ + 6.279862, + 12.50201, + 6.641763 + ], + [ + 12.787265, + 12.579025, + 14.233563 + ], + [ + 14.704979, + 12.479635, + 11.469042 + ], + [ + 14.155564, + 8.682469, + 11.754944 + ], + [ + 11.107211, + 15.209582, + 10.326656 + ], + [ + 14.086575, + 14.569747, + 16.669442 + ], + [ + 14.613354, + 13.508458, + 15.272899 + ], + [ + 12.427056, + 8.014782, + 10.402933 + ], + [ + 15.154642, + 15.412163, + 15.353146 + ], + [ + 14.159405, + 14.490429, + 15.872971 + ], + [ + 15.228723, + 14.023546, + 15.616974 + ], + [ + 18.150367, + 16.421998, + 14.211389 + ] + ], + [ + [ + 8.500358, + 9.88876, + 10.670678 + ], + [ + 11.333256, + 9.97975, + 10.000846 + ], + [ + 13.828213, + 14.985716, + 10.692569 + ], + [ + 13.838645, + 9.815284, + 8.575067 + ], + [ + 12.963812, + 9.241374, + 8.357608 + ], + [ + 9.348031, + 12.209176, + 13.930911 + ], + [ + 9.922417, + 10.079731, + 11.076583 + ], + [ + 14.261263, + 8.593843, + 10.642439 + ], + [ + 10.378832, + 11.73507, + 12.326824 + ], + [ + 16.157605, + 9.139014, + 8.338432 + ], + [ + 10.060104, + 13.9977, + 15.557491 + ], + [ + 14.607931, + 15.502873, + 11.915177 + ], + [ + 12.393463, + 9.60236, + 11.90722 + ], + [ + 13.383683, + 12.018524, + 12.153204 + ], + [ + 16.350574, + 9.583054, + 8.999102 + ], + [ + 14.666116, + 12.404725, + 14.082478 + ], + [ + 14.872386, + 12.336212, + 13.850529 + ], + [ + 11.565941, + 15.445757, + 16.907239 + ], + [ + 12.364779, + 14.856163, + 15.509202 + ], + [ + 14.09083, + 11.657849, + 14.304883 + ], + [ + 12.60263, + 15.289327, + 16.841019 + ], + [ + 13.310153, + 13.363294, + 14.104658 + ], + [ + 13.096516, + 15.458018, + 16.207113 + ], + [ + 18.384133, + 12.222744, + 13.892724 + ] + ], + [ + [ + 12.054401, + 11.951593, + 8.753244 + ], + [ + 10.333358, + 9.327695, + 9.449701 + ], + [ + 9.833969, + 6.632258, + 14.455212 + ], + [ + 12.2187, + 13.474351, + 12.375032 + ], + [ + 11.922969, + 13.301394, + 12.339512 + ], + [ + 13.160095, + 12.909625, + 8.129893 + ], + [ + 10.859831, + 10.091947, + 10.098657 + ], + [ + 13.081224, + 11.626579, + 10.562085 + ], + [ + 13.712801, + 13.618468, + 10.608698 + ], + [ + 14.188435, + 12.741819, + 13.130292 + ], + [ + 15.51551, + 13.206816, + 9.489398 + ], + [ + 11.034651, + 7.732263, + 15.952863 + ], + [ + 7.69546, + 12.590837, + 9.083143 + ], + [ + 12.303209, + 11.398021, + 11.944978 + ], + [ + 14.68346, + 13.407454, + 14.007286 + ], + [ + 13.396107, + 11.143197, + 15.991007 + ], + [ + 9.502284, + 14.11603, + 10.84482 + ], + [ + 16.876028, + 14.564651, + 11.025954 + ], + [ + 14.471087, + 15.51707, + 12.442847 + ], + [ + 13.599693, + 11.222677, + 16.354612 + ], + [ + 16.100104, + 15.833142, + 11.4667 + ], + [ + 13.944471, + 13.212793, + 13.485922 + ], + [ + 15.168717, + 16.228919, + 13.215414 + ], + [ + 16.62511, + 14.994395, + 14.199047 + ] + ], + [ + [ + 10.410707, + 10.056389, + 11.245691 + ], + [ + 11.991118, + 9.965497, + 10.788666 + ], + [ + 11.448489, + 13.011347, + 9.983984 + ], + [ + 13.774523, + 11.464643, + 9.269815 + ], + [ + 13.909999, + 11.623824, + 9.141021 + ], + [ + 12.222109, + 11.901662, + 11.376266 + ], + [ + 11.826712, + 10.907172, + 12.306639 + ], + [ + 12.809659, + 11.070862, + 12.126298 + ], + [ + 12.22044, + 11.731991, + 12.864321 + ], + [ + 14.886225, + 12.399599, + 10.736073 + ], + [ + 13.314838, + 13.156004, + 13.050774 + ], + [ + 13.334006, + 14.796535, + 11.371194 + ], + [ + 7.95372, + 6.703459, + 6.600438 + ], + [ + 14.641645, + 12.422388, + 12.976507 + ], + [ + 15.929638, + 13.397713, + 11.405119 + ], + [ + 9.549842, + 15.591126, + 12.788637 + ], + [ + 9.060057, + 7.689059, + 7.825855 + ], + [ + 14.886409, + 14.606982, + 14.409625 + ], + [ + 14.859814, + 13.875298, + 15.135985 + ], + [ + 10.249649, + 16.330437, + 13.205346 + ], + [ + 15.589697, + 15.013949, + 14.305032 + ], + [ + 15.212624, + 14.034319, + 15.283473 + ], + [ + 15.750617, + 14.683948, + 15.843248 + ], + [ + 16.449132, + 14.40979, + 15.366311 + ] + ], + [ + [ + 10.796916, + 11.894664, + 9.783332 + ], + [ + 9.35474, + 9.58172, + 12.604953 + ], + [ + 10.544851, + 9.335263, + 9.732989 + ], + [ + 10.164163, + 6.881481, + 12.863397 + ], + [ + 10.273892, + 7.001371, + 12.996081 + ], + [ + 13.147254, + 14.055131, + 7.747049 + ], + [ + 10.867224, + 11.158932, + 11.670727 + ], + [ + 8.632793, + 11.208363, + 13.423895 + ], + [ + 12.451599, + 13.45238, + 11.541771 + ], + [ + 9.13837, + 11.775031, + 14.367202 + ], + [ + 14.036184, + 14.885674, + 11.4087 + ], + [ + 12.287383, + 10.973963, + 11.552567 + ], + [ + 12.974681, + 11.386834, + 14.739573 + ], + [ + 11.779758, + 11.856207, + 15.111005 + ], + [ + 10.11483, + 12.694397, + 15.362398 + ], + [ + 8.98866, + 12.591038, + 16.353473 + ], + [ + 14.01423, + 12.269494, + 15.745907 + ], + [ + 15.470592, + 16.235804, + 12.91067 + ], + [ + 15.878675, + 16.113459, + 10.800429 + ], + [ + 9.658135, + 13.25923, + 17.073818 + ], + [ + 16.221066, + 16.948324, + 10.983524 + ], + [ + 13.964282, + 14.078797, + 14.942156 + ], + [ + 16.692807, + 16.874113, + 11.637615 + ], + [ + 11.953234, + 14.332324, + 16.92844 + ] + ], + [ + [ + 12.254573, + 11.416087, + 9.501358 + ], + [ + 10.376935, + 7.128574, + 10.09932 + ], + [ + 9.455586, + 16.53442, + 14.475636 + ], + [ + 9.52038, + 13.447369, + 9.439017 + ], + [ + 9.725171, + 13.681921, + 9.631622 + ], + [ + 12.149505, + 12.422432, + 9.547456 + ], + [ + 14.417157, + 11.444912, + 10.618118 + ], + [ + 8.483325, + 9.966747, + 11.40362 + ], + [ + 13.892899, + 13.292495, + 11.042194 + ], + [ + 9.246599, + 11.062538, + 9.936815 + ], + [ + 11.23279, + 11.612875, + 11.727073 + ], + [ + 11.298702, + 18.645379, + 16.209077 + ], + [ + 11.49862, + 11.679211, + 15.698647 + ], + [ + 12.834424, + 9.943186, + 12.410574 + ], + [ + 10.270552, + 12.235293, + 10.899838 + ], + [ + 10.042294, + 12.990154, + 15.880654 + ], + [ + 12.317783, + 12.617415, + 16.469065 + ], + [ + 12.666325, + 13.254732, + 13.075305 + ], + [ + 15.785859, + 14.707967, + 12.464058 + ], + [ + 10.861457, + 13.928358, + 16.651072 + ], + [ + 15.221366, + 15.940697, + 12.436523 + ], + [ + 17.489017, + 14.963177, + 13.507185 + ], + [ + 16.605022, + 15.646171, + 13.234476 + ], + [ + 11.759976, + 13.719563, + 14.485292 + ] + ], + [ + [ + 8.040809, + 9.285752, + 9.048869 + ], + [ + 8.681166, + 10.187106, + 9.331639 + ], + [ + 15.116509, + 9.943368, + 12.305488 + ], + [ + 12.756497, + 5.340703, + 10.252468 + ], + [ + 13.9435, + 5.94369, + 11.710299 + ], + [ + 13.113279, + 13.89766, + 9.756466 + ], + [ + 10.105572, + 10.35574, + 10.595904 + ], + [ + 11.852725, + 11.696698, + 11.116518 + ], + [ + 9.262393, + 10.669425, + 10.422938 + ], + [ + 12.228338, + 11.941547, + 11.25976 + ], + [ + 13.060215, + 14.099157, + 10.731178 + ], + [ + 18.109367, + 12.279704, + 16.11479 + ], + [ + 13.966049, + 13.402259, + 17.419728 + ], + [ + 11.941878, + 12.746671, + 12.157514 + ], + [ + 14.148691, + 13.163938, + 13.324219 + ], + [ + 11.000101, + 11.837405, + 14.879422 + ], + [ + 13.254448, + 13.400489, + 15.714319 + ], + [ + 14.500042, + 15.438976, + 12.0597 + ], + [ + 14.979754, + 12.605784, + 12.366178 + ], + [ + 13.292052, + 13.343325, + 17.885141 + ], + [ + 16.118494, + 16.74227, + 12.616422 + ], + [ + 12.529763, + 13.030728, + 13.192358 + ], + [ + 15.960168, + 13.527465, + 13.219028 + ], + [ + 14.319864, + 14.526459, + 13.363648 + ] + ], + [ + [ + 8.859203, + 8.998809, + 11.649408 + ], + [ + 9.046834, + 8.372968, + 9.478946 + ], + [ + 13.23057, + 9.033013, + 8.445767 + ], + [ + 13.75203, + 9.175328, + 11.284794 + ], + [ + 15.718084, + 12.727456, + 13.132003 + ], + [ + 10.161318, + 10.507519, + 12.634183 + ], + [ + 10.068276, + 11.18695, + 13.207195 + ], + [ + 13.684733, + 11.980605, + 11.910137 + ], + [ + 9.919095, + 9.510487, + 12.827154 + ], + [ + 14.385952, + 10.416849, + 11.24939 + ], + [ + 12.380611, + 11.621805, + 13.696906 + ], + [ + 17.039079, + 14.635384, + 12.461592 + ], + [ + 14.435251, + 18.839759, + 9.18373 + ], + [ + 12.696519, + 12.503549, + 12.713049 + ], + [ + 17.108379, + 14.693389, + 13.30644 + ], + [ + 12.258972, + 10.280866, + 11.117323 + ], + [ + 11.68906, + 13.200676, + 6.831076 + ], + [ + 13.858913, + 12.775338, + 14.983155 + ], + [ + 11.519185, + 11.972885, + 15.200729 + ], + [ + 15.972333, + 15.534935, + 14.236782 + ], + [ + 13.102157, + 12.905239, + 15.419649 + ], + [ + 12.535404, + 12.290315, + 15.659992 + ], + [ + 12.995221, + 13.530685, + 16.24114 + ], + [ + 15.864495, + 12.71151, + 13.973457 + ] + ], + [ + [ + 9.870608, + 10.648067, + 9.689414 + ], + [ + 10.145712, + 8.817588, + 8.776963 + ], + [ + 11.358792, + 10.409559, + 10.623762 + ], + [ + 10.06059, + 5.730698, + 10.438703 + ], + [ + 11.787891, + 7.484016, + 12.329674 + ], + [ + 10.725167, + 12.854882, + 11.118733 + ], + [ + 11.177427, + 10.30441, + 10.115911 + ], + [ + 11.607178, + 10.458712, + 11.207444 + ], + [ + 11.188901, + 11.853484, + 10.782328 + ], + [ + 11.555691, + 10.811071, + 10.341286 + ], + [ + 11.407643, + 14.048106, + 12.227771 + ], + [ + 15.388629, + 14.448955, + 14.198077 + ], + [ + 19.325563, + 12.300767, + 20.034682 + ], + [ + 12.855429, + 11.754296, + 11.976357 + ], + [ + 13.464983, + 12.736387, + 12.552959 + ], + [ + 14.911299, + 12.358284, + 12.186295 + ], + [ + 15.3503, + 9.199728, + 16.58769 + ], + [ + 12.74799, + 15.347821, + 13.573327 + ], + [ + 13.10822, + 15.188368, + 13.210306 + ], + [ + 18.851024, + 15.512784, + 15.176116 + ], + [ + 13.51084, + 15.611778, + 13.843107 + ], + [ + 13.797536, + 12.609129, + 12.283144 + ], + [ + 14.037726, + 16.096383, + 14.28262 + ], + [ + 14.046533, + 12.821044, + 13.287326 + ] + ], + [ + [ + 11.42446, + 11.678135, + 12.02608 + ], + [ + 9.336739, + 10.566383, + 10.414998 + ], + [ + 8.936311, + 7.277163, + 8.7704 + ], + [ + 9.201645, + 9.974086, + 10.590595 + ], + [ + 11.377804, + 12.197465, + 12.188521 + ], + [ + 12.679946, + 12.815823, + 13.81429 + ], + [ + 12.409296, + 12.502524, + 12.103372 + ], + [ + 11.447944, + 12.783051, + 12.694684 + ], + [ + 12.334298, + 12.787007, + 13.102548 + ], + [ + 10.518786, + 11.947616, + 11.936049 + ], + [ + 13.591339, + 13.661274, + 14.833989 + ], + [ + 12.680258, + 12.05837, + 13.591226 + ], + [ + 9.252249, + 12.53195, + 12.806298 + ], + [ + 12.746245, + 13.872782, + 13.749978 + ], + [ + 12.780347, + 14.398992, + 14.091437 + ], + [ + 11.128649, + 7.775532, + 8.714562 + ], + [ + 5.921636, + 8.201989, + 8.83977 + ], + [ + 14.63963, + 14.958469, + 16.124549 + ], + [ + 14.227463, + 14.763939, + 15.859144 + ], + [ + 13.992083, + 13.333504, + 13.785516 + ], + [ + 15.256083, + 15.537877, + 16.608719 + ], + [ + 14.545578, + 14.594016, + 14.099374 + ], + [ + 15.604421, + 15.930453, + 16.857277 + ], + [ + 13.373107, + 14.695598, + 14.678298 + ] + ], + [ + [ + 9.601951, + 10.970336, + 12.637183 + ], + [ + 9.381359, + 10.458949, + 10.457052 + ], + [ + 10.200245, + 10.024046, + 8.394581 + ], + [ + 9.725941, + 10.158904, + 11.15709 + ], + [ + 12.354305, + 11.89022, + 13.565835 + ], + [ + 10.571671, + 11.024247, + 9.328145 + ], + [ + 11.002096, + 12.310643, + 14.278383 + ], + [ + 10.271771, + 12.397291, + 13.391053 + ], + [ + 10.884844, + 12.184608, + 13.24041 + ], + [ + 10.444571, + 11.885326, + 13.555359 + ], + [ + 11.450123, + 12.045627, + 10.683475 + ], + [ + 14.825848, + 13.748788, + 12.965097 + ], + [ + 17.781822, + 9.841996, + 12.66302 + ], + [ + 12.33692, + 13.925545, + 14.473462 + ], + [ + 13.48996, + 14.194874, + 16.520958 + ], + [ + 14.616313, + 6.531003, + 11.247258 + ], + [ + 14.621796, + 6.577862, + 9.587044 + ], + [ + 12.729305, + 13.548956, + 12.080514 + ], + [ + 12.792099, + 14.409499, + 16.060994 + ], + [ + 19.159839, + 11.485565, + 14.942764 + ], + [ + 13.311817, + 14.234701, + 12.28359 + ], + [ + 13.455403, + 14.989946, + 15.991606 + ], + [ + 13.755292, + 15.732758, + 17.763431 + ], + [ + 12.078705, + 14.834629, + 15.013367 + ] + ] + ] + }, + "0cd488f5d61ee5229c3e2279a46909b3b5590863f8574e9bfd3775fa44dca750": { + "ensemble": [ + 10.1647258, + 10.4198234, + 10.9401514, + 10.951225333333333, + 11.2267064, + 11.008787866666667, + 11.280849933333334, + 11.6740992, + 11.8168878, + 11.8599002, + 12.1803774, + 12.7290908, + 12.917627133333333, + 12.720936266666667, + 12.967490533333333, + 13.380303, + 13.872989, + 13.550302466666666, + 13.594413533333334, + 14.205271, + 14.0730976, + 14.297716133333333, + 14.246016866666666, + 14.7652062 + ], + "names": [ + "rho = 1", + "rho = 0.5", + "rho = 0", + "rho = -0.5", + "rho = -1" + ], + "reps": [ + [ + [ + 9.358883, + 10.246759, + 9.059907 + ], + [ + 10.619389, + 12.464211, + 11.278283 + ], + [ + 12.829315, + 13.860368, + 13.275762 + ], + [ + 13.020375, + 13.191538, + 13.352502 + ], + [ + 9.131414, + 9.976826, + 9.607137 + ], + [ + 10.490879, + 11.26767, + 10.416473 + ], + [ + 10.510735, + 11.044716, + 10.014413 + ], + [ + 9.84232, + 11.27458, + 10.256652 + ], + [ + 11.896379, + 13.084471, + 12.018161 + ], + [ + 12.461072, + 14.105591, + 13.965597 + ], + [ + 11.593307, + 12.463907, + 12.183133 + ], + [ + 8.430937, + 11.580539, + 10.54061 + ], + [ + 7.908889, + 6.49159, + 9.728899 + ], + [ + 11.832772, + 12.523818, + 11.389253 + ], + [ + 11.00154, + 11.910776, + 11.535153 + ], + [ + 17.114858, + 16.784035, + 15.950373 + ], + [ + 18.002966, + 15.703067, + 19.121218 + ], + [ + 13.048294, + 14.020397, + 13.582358 + ], + [ + 13.547293, + 14.9486, + 13.813522 + ], + [ + 9.896519, + 13.167453, + 11.808457 + ], + [ + 13.83773, + 14.794054, + 14.163474 + ], + [ + 14.470466, + 15.865885, + 14.563474 + ], + [ + 13.73674, + 14.616954, + 13.304348 + ], + [ + 14.823246, + 16.893868, + 15.914836 + ] + ], + [ + [ + 8.72004, + 9.007405, + 9.540158 + ], + [ + 10.099855, + 10.216072, + 11.26746 + ], + [ + 14.016782, + 13.532828, + 13.178449 + ], + [ + 13.237197, + 14.326651, + 9.669997 + ], + [ + 10.618804, + 12.442089, + 7.211653 + ], + [ + 10.514408, + 10.649579, + 10.549187 + ], + [ + 9.897984, + 10.199464, + 10.713648 + ], + [ + 9.445024, + 10.508244, + 11.925307 + ], + [ + 11.075834, + 11.422842, + 12.048765 + ], + [ + 11.89072, + 12.749195, + 10.879323 + ], + [ + 12.449436, + 12.639195, + 12.379031 + ], + [ + 12.511725, + 12.36815, + 11.488575 + ], + [ + 11.767718, + 11.884785, + 6.658865 + ], + [ + 11.459994, + 11.537813, + 12.351956 + ], + [ + 11.420264, + 12.379662, + 9.592317 + ], + [ + 17.493581, + 18.81647, + 16.874403 + ], + [ + 18.713228, + 18.704492, + 12.350995 + ], + [ + 13.844697, + 14.072105, + 13.788678 + ], + [ + 12.969599, + 13.132781, + 13.846384 + ], + [ + 14.609974, + 15.842012, + 13.947435 + ], + [ + 13.773859, + 14.066856, + 14.21571 + ], + [ + 13.645214, + 13.997953, + 14.714466 + ], + [ + 12.946097, + 13.106684, + 13.733495 + ], + [ + 13.721961, + 14.678346, + 16.48006 + ] + ], + [ + [ + 11.957037, + 8.669238, + 10.420138 + ], + [ + 11.048642, + 8.962129, + 9.977329 + ], + [ + 14.918127, + 9.601562, + 8.707516 + ], + [ + 9.254653, + 11.329691, + 10.311414 + ], + [ + 9.459908, + 11.522296, + 10.504018 + ], + [ + 9.474387, + 9.548543, + 13.012609 + ], + [ + 13.270752, + 9.7367, + 9.768815 + ], + [ + 10.832397, + 10.13997, + 11.155661 + ], + [ + 13.599074, + 10.210074, + 11.960974 + ], + [ + 11.330134, + 12.254688, + 11.400624 + ], + [ + 9.884955, + 10.580257, + 14.382316 + ], + [ + 16.765419, + 11.335002, + 10.440956 + ], + [ + 15.085752, + 17.733372, + 16.994844 + ], + [ + 13.511698, + 11.273383, + 12.288583 + ], + [ + 12.356407, + 13.217711, + 12.363647 + ], + [ + 16.15509, + 14.109368, + 10.267315 + ], + [ + 15.90677, + 18.50379, + 17.765262 + ], + [ + 11.321738, + 11.928488, + 15.730547 + ], + [ + 12.666177, + 12.407416, + 14.393191 + ], + [ + 16.976109, + 14.879786, + 11.037733 + ], + [ + 12.553207, + 12.43761, + 15.901677 + ], + [ + 16.349572, + 12.625768, + 12.657883 + ], + [ + 13.487196, + 13.177834, + 15.163609 + ], + [ + 14.116472, + 13.221642, + 14.237333 + ] + ], + [ + [ + 12.067035, + 10.35272, + 11.188538 + ], + [ + 10.937135, + 10.138698, + 10.197722 + ], + [ + 10.12397, + 6.227781, + 8.916285 + ], + [ + 10.860359, + 9.941798, + 8.534212 + ], + [ + 13.526279, + 12.620954, + 11.413002 + ], + [ + 11.140213, + 10.779497, + 12.905649 + ], + [ + 13.738036, + 12.269749, + 12.251067 + ], + [ + 14.820908, + 14.128108, + 13.027763 + ], + [ + 12.912348, + 11.150894, + 11.746664 + ], + [ + 12.219611, + 11.210425, + 12.171788 + ], + [ + 12.318933, + 10.673895, + 14.080673 + ], + [ + 14.74413, + 11.298391, + 13.733498 + ], + [ + 11.177664, + 16.22047, + 11.964809 + ], + [ + 15.054877, + 14.008831, + 13.625767 + ], + [ + 15.627093, + 14.671101, + 14.639848 + ], + [ + 10.227867, + 7.121494, + 8.829796 + ], + [ + 6.303275, + 12.325714, + 7.158463 + ], + [ + 13.749894, + 11.823694, + 15.400596 + ], + [ + 14.028612, + 13.007262, + 14.656257 + ], + [ + 14.834432, + 11.465533, + 13.96849 + ], + [ + 14.219109, + 13.330877, + 15.579391 + ], + [ + 15.945149, + 14.363871, + 14.100253 + ], + [ + 15.290055, + 14.568488, + 16.023919 + ], + [ + 16.414688, + 15.706072, + 14.729545 + ] + ], + [ + [ + 10.866666, + 10.737856, + 10.278507 + ], + [ + 10.269728, + 9.596787, + 9.223911 + ], + [ + 8.528605, + 8.379932, + 8.004989 + ], + [ + 9.801384, + 9.10726, + 8.329349 + ], + [ + 14.55218, + 13.711174, + 12.102862 + ], + [ + 11.392964, + 11.732357, + 11.257403 + ], + [ + 12.209331, + 12.003382, + 11.583957 + ], + [ + 13.590179, + 12.321566, + 11.842809 + ], + [ + 11.711159, + 11.450174, + 10.965504 + ], + [ + 11.023709, + 10.463317, + 9.772709 + ], + [ + 12.319146, + 12.515732, + 12.241745 + ], + [ + 16.091475, + 15.407639, + 14.199316 + ], + [ + 16.54084, + 16.305453, + 17.300457 + ], + [ + 13.667266, + 13.352821, + 12.935212 + ], + [ + 16.061896, + 14.358896, + 13.376047 + ], + [ + 10.823624, + 9.67144, + 10.464831 + ], + [ + 10.736101, + 8.970985, + 7.828509 + ], + [ + 13.657004, + 13.856425, + 13.429622 + ], + [ + 13.66108, + 13.582496, + 13.255533 + ], + [ + 17.750195, + 16.874118, + 16.020819 + ], + [ + 14.064383, + 14.43216, + 13.726367 + ], + [ + 14.209017, + 13.678944, + 13.277827 + ], + [ + 15.009167, + 14.989691, + 14.535976 + ], + [ + 13.744574, + 13.343393, + 13.452057 + ] + ] + ] + }, + "f49ce250b0d33c4cdb12c9abfc838d69005467926bba75032db3854fb61cf978": { + "ensemble": [ + 11.368744222222222, + 12.65780111111111, + 13.785903555555556, + 13.882454222222222, + 11.437765666666667, + 11.831502333333333, + 11.655608777777777, + 11.368384333333333, + 12.346004555555556, + 13.247670555555557, + 12.140066222222222, + 10.92831611111111, + 11.084097444444446, + 11.747021111111112, + 11.675398555555557, + 15.582273222222222, + 16.734629333333334, + 12.352636666666667, + 12.488487222222222, + 12.030039444444444, + 12.593636, + 12.955716888888889, + 11.874831555555556, + 13.075006111111112 + ], + "names": [ + "rho = 0.9", + "rho = 0.75559036818887", + "rho = 0.2" + ], + "reps": [ + [ + [ + 11.546744, + 12.293271, + 10.818312 + ], + [ + 12.473632, + 14.221371, + 12.796031 + ], + [ + 13.234326, + 15.498982, + 14.868403 + ], + [ + 15.101071, + 14.38625, + 14.880885 + ], + [ + 11.797302, + 11.49492, + 11.367337 + ], + [ + 11.602964, + 12.249599, + 11.344881 + ], + [ + 11.723658, + 12.079211, + 10.706324 + ], + [ + 10.23597, + 12.372573, + 11.098634 + ], + [ + 12.518361, + 13.561366, + 12.250163 + ], + [ + 12.22597, + 14.21685, + 14.339642 + ], + [ + 11.751295, + 12.562138, + 12.424238 + ], + [ + 8.42096, + 12.249905, + 11.038727 + ], + [ + 9.450056, + 5.578097, + 10.849594 + ], + [ + 11.819595, + 12.537485, + 11.061712 + ], + [ + 10.353194, + 11.618684, + 11.394678 + ], + [ + 16.485636, + 16.283149, + 15.24863 + ], + [ + 17.122656, + 12.870431, + 18.666043 + ], + [ + 11.97823, + 12.880081, + 12.609995 + ], + [ + 12.372831, + 13.733674, + 12.293036 + ], + [ + 10.060096, + 12.614744, + 10.920783 + ], + [ + 12.275754, + 13.094145, + 12.414707 + ], + [ + 12.895539, + 14.030065, + 12.495839 + ], + [ + 11.979725, + 12.872032, + 11.224856 + ], + [ + 12.102602, + 14.8608, + 13.730544 + ] + ], + [ + [ + 10.799473, + 11.206429, + 11.314113 + ], + [ + 12.157041, + 12.448179, + 13.005694 + ], + [ + 15.750263, + 15.319791, + 14.886026 + ], + [ + 14.867486, + 14.607416, + 11.481521 + ], + [ + 11.411131, + 11.313584, + 8.189796 + ], + [ + 11.663055, + 11.972365, + 11.339872 + ], + [ + 10.955423, + 11.258501, + 11.558583 + ], + [ + 10.315737, + 11.358962, + 12.260882 + ], + [ + 11.958467, + 12.478788, + 12.642408 + ], + [ + 12.609276, + 13.48284, + 14.104729 + ], + [ + 12.645112, + 13.011174, + 12.33871 + ], + [ + 11.92522, + 11.750603, + 10.789412 + ], + [ + 10.576938, + 10.810063, + 6.080936 + ], + [ + 11.228689, + 11.413109, + 11.736376 + ], + [ + 10.936496, + 11.868211, + 12.339705 + ], + [ + 16.911308, + 18.001471, + 16.25362 + ], + [ + 18.29366, + 18.500196, + 12.309438 + ], + [ + 12.843274, + 13.257701, + 12.555435 + ], + [ + 12.254356, + 12.50567, + 12.719509 + ], + [ + 12.472383, + 13.242588, + 11.71462 + ], + [ + 12.421108, + 12.936863, + 12.611117 + ], + [ + 12.298363, + 12.747455, + 13.170082 + ], + [ + 11.435819, + 11.583638, + 11.739111 + ], + [ + 12.113918, + 13.084227, + 14.386469 + ] + ], + [ + [ + 11.784246, + 10.668948, + 11.887162 + ], + [ + 13.995658, + 10.978265, + 11.844339 + ], + [ + 12.007051, + 11.609783, + 10.898507 + ], + [ + 12.530569, + 13.080998, + 14.005892 + ], + [ + 11.736201, + 12.706518, + 12.923102 + ], + [ + 11.194292, + 10.716741, + 14.399752 + ], + [ + 14.623028, + 10.701005, + 11.294746 + ], + [ + 12.451526, + 10.919825, + 11.30135 + ], + [ + 12.23935, + 10.999196, + 12.465942 + ], + [ + 13.310981, + 12.955669, + 11.983078 + ], + [ + 9.21471, + 10.912452, + 14.400767 + ], + [ + 11.327054, + 11.253546, + 9.599418 + ], + [ + 15.022429, + 17.101593, + 14.287171 + ], + [ + 13.608552, + 11.019225, + 11.298447 + ], + [ + 12.658275, + 12.694623, + 11.214721 + ], + [ + 16.677745, + 14.022385, + 10.356515 + ], + [ + 16.612161, + 18.364704, + 17.872375 + ], + [ + 9.36675, + 11.116428, + 14.565836 + ], + [ + 11.974873, + 11.388419, + 13.154017 + ], + [ + 15.401247, + 13.233782, + 8.610112 + ], + [ + 11.66953, + 11.135958, + 14.783542 + ], + [ + 15.433899, + 11.182878, + 12.347332 + ], + [ + 11.803022, + 11.323522, + 12.911759 + ], + [ + 13.516677, + 11.441703, + 12.438115 + ] + ] + ] + } + }, + "label": "boot14.mc", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/boot14.meboot.json b/tests/_r_cache/boot14.meboot.json new file mode 100644 index 00000000..2e4c3b07 --- /dev/null +++ b/tests/_r_cache/boot14.meboot.json @@ -0,0 +1,3064 @@ +{ + "entries": { + "088a9f064932f9ca1fef35c63b1b58d11f7a85a4268520718cc61a8895c6121c": [ + { + "desintxb": [ + -3.2957243706062345, + -3.037102969373551, + -2.734749363764396, + -2.4687460550750178, + -2.2033709645513992, + -1.9698459020709664, + -1.8446903524266696, + -1.7580791834600067, + -1.5327084014108416, + -1.18206859273404, + -0.9370517339122388, + -0.8183258053724383, + -0.7079516209209451, + -0.6218249010884187, + -0.5697516179509596, + -0.5163491849400692, + -0.4249403015029349, + -0.30434584061391096, + -0.1766028124218199, + -0.05506102359946414, + 0.05506102359946468, + 0.17660281242182047, + 0.3043458406139115, + 0.42494030150293544, + 0.5163491849400696, + 0.5697516179509601, + 0.6218249010884191, + 0.7079516209209455, + 0.8183258053724387, + 0.9370517339122393, + 1.1820685927340404, + 1.532708401410842, + 1.7580791834600071, + 1.84469035242667, + 1.9698459020709669, + 2.2033709645513997, + 2.4687460550750187, + 2.7347493637643967, + 3.037102969373552, + 3.2957243706062354 + ], + "dvtrim": 1.4736952390647713, + "ens": [ + 10.825951290586119, + 11.287352414233446, + 11.336733421414225, + 11.748410452865075, + 11.509851169232395, + 9.911239755869389, + 9.92767644412444, + 12.463315149806949, + 12.613084964085635, + 12.896917441461236, + 13.20927999649202, + 13.35391941661155, + 11.34384113812039, + 11.506791747806702, + 12.94375575867486, + 13.471059767337533, + 12.62425608106667, + 13.692681638233072, + 11.616925433409268, + 11.43238352992291, + 13.997553153250749, + 15.429179652205278, + 15.583485132732156, + 15.260009495149074, + 15.132622538025075, + 13.640720807798973, + 9.953785238432891, + 15.551270996922423, + 16.471506639587638, + 16.47674608176404, + 14.888924205701358, + 17.42376348140483, + 17.489802649469656, + 11.37247156486918, + 18.064517737244625, + 16.772514561090976, + 17.724164137553398, + 19.20575690305959, + 18.869824037128332, + 18.845282606702416 + ], + "rep": [ + [ + 11.423545, + 11.875272, + 10.701743, + 9.303246 + ], + [ + 12.90874, + 10.53299, + 11.070422, + 10.637258 + ], + [ + 10.676901, + 9.995411, + 11.662519, + 13.012103 + ], + [ + 11.709597, + 10.140784, + 11.939447, + 13.203814 + ], + [ + 11.314677, + 11.857916, + 11.582409, + 11.284403 + ], + [ + 11.689947, + 9.637345, + 9.940147, + 8.377521 + ], + [ + 10.280688, + 10.957683, + 10.177119, + 8.295216 + ], + [ + 12.499165, + 10.592955, + 12.595913, + 14.165228 + ], + [ + 12.960501, + 13.492131, + 13.013417, + 10.986291 + ], + [ + 12.450505, + 14.756393, + 10.614692, + 13.76608 + ], + [ + 15.370839, + 11.639195, + 12.665252, + 13.161834 + ], + [ + 13.179018, + 11.341952, + 13.867217, + 15.027491 + ], + [ + 10.82334, + 11.405971, + 11.47262, + 11.673434 + ], + [ + 13.555748, + 9.906317, + 11.442303, + 11.122799 + ], + [ + 14.341575, + 12.276361, + 12.877945, + 12.279142 + ], + [ + 15.816681, + 11.91708, + 13.218765, + 12.931713 + ], + [ + 13.09148, + 12.194974, + 12.443631, + 12.766939 + ], + [ + 14.850049, + 12.94522, + 13.742009, + 13.233448 + ], + [ + 12.369286, + 9.96236, + 11.76797, + 12.368085 + ], + [ + 11.425869, + 13.307394, + 8.152094, + 12.844177 + ], + [ + 13.957431, + 14.426244, + 14.051697, + 13.554841 + ], + [ + 15.43933, + 14.013593, + 15.572771, + 16.691025 + ], + [ + 16.975702, + 15.766495, + 15.071893, + 14.51985 + ], + [ + 16.054444, + 13.912121, + 15.264369, + 15.809104 + ], + [ + 16.245628, + 15.424411, + 14.771337, + 14.089113 + ], + [ + 13.745116, + 14.727053, + 14.113964, + 11.97675 + ], + [ + 9.065609, + 8.256382, + 9.546632, + 12.946518 + ], + [ + 16.205373, + 14.990058, + 15.518669, + 15.490985 + ], + [ + 16.330161, + 14.490259, + 16.972045, + 18.093562 + ], + [ + 18.464497, + 16.382647, + 16.037374, + 15.022466 + ], + [ + 14.511382, + 13.43343, + 14.833565, + 16.777319 + ], + [ + 17.003206, + 16.396456, + 17.593177, + 18.702215 + ], + [ + 17.265078, + 17.613325, + 17.430887, + 17.649921 + ], + [ + 11.33646, + 10.609818, + 11.065197, + 12.478412 + ], + [ + 19.516338, + 17.346361, + 17.95691, + 17.438462 + ], + [ + 18.755842, + 15.331188, + 16.335287, + 16.667742 + ], + [ + 17.567824, + 17.784389, + 17.746289, + 17.798154 + ], + [ + 18.664243, + 19.880269, + 19.277825, + 19.000691 + ], + [ + 19.819574, + 18.336384, + 18.618377, + 18.704961 + ], + [ + 18.99081, + 17.229875, + 18.887315, + 20.27313 + ] + ] + } + ], + "48eceec64237fccc62ae092f19126a9d3fd29ec1df98852c69b4a81064514993": [ + { + "desintxb": [ + -2.815424924298305, + -2.7352960080769932, + -2.668033148613282, + -2.5911971966529475, + -2.4075202964786877, + -2.1645969404546777, + -1.9992930775608295, + -1.8702908935005036, + -1.6682894731774756, + -1.3632129847546643, + -1.0680478680429784, + -0.8602644094912462, + -0.687277991397381, + -0.5601214973695328, + -0.49245906126169103, + -0.4377936027598386, + -0.35835630986631895, + -0.2611362948830651, + -0.13905490735798587, + -0.011624111797918335, + 0.09849793540101048, + 0.2141507174856545, + 0.34755538634475736, + 0.49152429313955137, + 0.5949047671203003, + 0.6470441746402287, + 0.6835283048073051, + 0.7286252504445097, + 0.7763872012536308, + 0.8060555997814998, + 1.000924200713416, + 1.3971273296442082, + 1.6458674734195102, + 1.69008762729251, + 1.7750948636872557, + 1.9992216326241112, + 2.346294913497089, + 2.8014655789155105, + 3.3389099306701095, + 3.7760238169141647 + ], + "dvtrim": 1.5184655539099265, + "ens": [ + 10.15104196198823, + 11.681954932846857, + 11.250665353758759, + 11.860337075979933, + 11.821883158726811, + 9.963920097683111, + 9.938575015834513, + 12.43619209294578, + 11.817714891627624, + 12.418401766503596, + 12.447678144447627, + 13.186951011106812, + 11.657320871870999, + 11.163200368693758, + 13.552940039104195, + 12.607181133425199, + 13.993385125104334, + 14.041712440749217, + 11.067659028752846, + 11.909047295700883, + 14.476316318948507, + 15.40896231997219, + 16.18374593890417, + 13.832089950902446, + 16.007155091382103, + 13.458886824329664, + 11.218261037763206, + 15.357555153653667, + 16.273713437127864, + 15.714605470779029, + 14.932584327451544, + 17.076918162132955, + 17.483563277736135, + 13.943386335372855, + 17.969106593200546, + 16.301579324419663, + 17.906185476916733, + 18.521392787407446, + 18.218242407090617, + 18.617316589104103 + ], + "rep": [ + [ + 10.011672, + 10.521436, + 9.920019 + ], + [ + 13.145064, + 10.307741, + 11.59306 + ], + [ + 10.780317, + 10.53388, + 12.4378 + ], + [ + 12.402363, + 10.494519, + 12.68413 + ], + [ + 10.922348, + 12.439569, + 12.103733 + ], + [ + 11.417091, + 8.888691, + 9.585978 + ], + [ + 9.56831, + 10.354858, + 9.892558 + ], + [ + 13.297095, + 10.820411, + 13.19107 + ], + [ + 11.58249, + 11.492053, + 12.378601 + ], + [ + 10.990859, + 16.028053, + 10.236294 + ], + [ + 15.403981, + 9.937911, + 12.001142 + ], + [ + 13.292745, + 11.677171, + 14.590937 + ], + [ + 10.130257, + 13.339112, + 11.502594 + ], + [ + 13.591856, + 8.793173, + 11.104573 + ], + [ + 14.920685, + 12.181594, + 13.556541 + ], + [ + 15.206106, + 10.112723, + 12.502715 + ], + [ + 14.188765, + 15.106582, + 12.684808 + ], + [ + 15.318752, + 12.630035, + 14.17635 + ], + [ + 11.755424, + 10.011096, + 11.436457 + ], + [ + 10.730446, + 15.771105, + 9.22559 + ], + [ + 13.696499, + 15.05387, + 14.67858 + ], + [ + 15.975482, + 14.045691, + 16.205714 + ], + [ + 17.135852, + 15.864266, + 15.55112 + ], + [ + 14.661251, + 12.621362, + 14.213657 + ], + [ + 16.791059, + 15.790363, + 15.440044 + ], + [ + 13.005324, + 13.361309, + 14.010028 + ], + [ + 10.834811, + 12.048194, + 10.771779 + ], + [ + 16.500659, + 13.77293, + 15.799077 + ], + [ + 16.431333, + 14.728943, + 17.660864 + ], + [ + 17.416728, + 14.558109, + 15.16898 + ], + [ + 14.844744, + 15.408216, + 14.544793 + ], + [ + 16.739457, + 16.330152, + 18.161146 + ], + [ + 16.851651, + 17.734495, + 17.864544 + ], + [ + 13.8753, + 15.262005, + 12.692854 + ], + [ + 19.437259, + 16.480239, + 17.989822 + ], + [ + 19.24862, + 13.823853, + 15.832265 + ], + [ + 17.33706, + 18.103611, + 18.277885 + ], + [ + 17.048581, + 20.142349, + 18.373248 + ], + [ + 19.536128, + 16.593673, + 18.524927 + ], + [ + 19.566207, + 16.982688, + 19.303055 + ] + ] + } + ], + "680e7aac867c320fbe58929edeef162d36d2c88865c3afb3b44b15a7d423227e": [ + { + "desintxb": [ + -2.815424924298305, + -2.7352960080769932, + -2.668033148613282, + -2.5911971966529475, + -2.4075202964786877, + -2.1645969404546777, + -1.9992930775608295, + -1.8702908935005036, + -1.6682894731774756, + -1.3632129847546643, + -1.0680478680429784, + -0.8602644094912462, + -0.687277991397381, + -0.5601214973695328, + -0.49245906126169103, + -0.4377936027598386, + -0.35835630986631895, + -0.2611362948830651, + -0.13905490735798587, + -0.011624111797918335, + 0.09849793540101048, + 0.2141507174856545, + 0.34755538634475736, + 0.49152429313955137, + 0.5949047671203003, + 0.6470441746402287, + 0.6835283048073051, + 0.7286252504445097, + 0.7763872012536308, + 0.8060555997814998, + 1.000924200713416, + 1.3971273296442082, + 1.6458674734195102, + 1.69008762729251, + 1.7750948636872557, + 1.9992216326241112, + 2.346294913497089, + 2.8014655789155105, + 3.3389099306701095, + 3.7760238169141647 + ], + "dvtrim": 1.5184655539099265, + "ens": [ + 10.060811526815847, + 11.516590158660692, + 11.068589233398306, + 11.666289371353939, + 11.63555416359947, + 10.259369294686284, + 10.169664189270659, + 12.301345719126145, + 11.680594401238098, + 12.42579406969881, + 12.57422594822212, + 13.012344306538116, + 11.544327764346693, + 11.586196489309618, + 13.560920251446415, + 12.806594550292077, + 13.855749632438783, + 14.04277933866172, + 11.474056555978963, + 11.932694784838713, + 14.272519794939628, + 15.250722350224033, + 15.889415912838588, + 13.993591172012733, + 15.91070636903265, + 13.700768670214638, + 11.80248915932207, + 15.437009172177467, + 16.125728739193704, + 15.793215461669163, + 15.04101955974044, + 16.862768864642344, + 17.256628813291947, + 14.346407328036596, + 17.81356688726888, + 16.595566736623155, + 17.662029595643265, + 18.294666487788824, + 18.1298031169401, + 18.5162126899248 + ], + "rep": [ + [ + 8.639235, + 10.973353, + 10.569847 + ], + [ + 12.54612, + 10.4571, + 11.54655 + ], + [ + 11.143685, + 10.187649, + 11.874434 + ], + [ + 12.545008, + 10.292074, + 12.161786 + ], + [ + 10.685074, + 12.169898, + 12.05169 + ], + [ + 12.69981, + 8.586739, + 9.491559 + ], + [ + 11.135987, + 9.672304, + 9.700701 + ], + [ + 13.37557, + 10.763292, + 12.765174 + ], + [ + 9.899311, + 12.142008, + 13.000464 + ], + [ + 9.014157, + 16.177622, + 12.085604 + ], + [ + 14.494744, + 10.74554, + 12.482394 + ], + [ + 13.169014, + 11.769822, + 14.098197 + ], + [ + 12.037555, + 11.869935, + 10.725493 + ], + [ + 14.754212, + 8.990116, + 11.014261 + ], + [ + 15.659279, + 11.877431, + 13.146051 + ], + [ + 14.10762, + 11.164895, + 13.147269 + ], + [ + 10.861678, + 16.302795, + 14.402776 + ], + [ + 16.060365, + 12.354589, + 13.713384 + ], + [ + 13.537126, + 9.777361, + 11.107684 + ], + [ + 12.773795, + 14.048441, + 8.975849 + ], + [ + 14.02251, + 14.502299, + 14.29275 + ], + [ + 15.920101, + 14.073327, + 15.75874 + ], + [ + 16.443324, + 15.719769, + 15.505155 + ], + [ + 12.868862, + 13.908044, + 15.203867 + ], + [ + 17.440918, + 15.251225, + 15.039975 + ], + [ + 14.724989, + 12.896911, + 13.480406 + ], + [ + 13.765538, + 11.225588, + 10.416341 + ], + [ + 17.26769, + 13.662746, + 15.380592 + ], + [ + 16.554693, + 14.767909, + 17.054584 + ], + [ + 16.163023, + 15.401157, + 15.815466 + ], + [ + 11.816512, + 16.942705, + 16.363842 + ], + [ + 16.411425, + 16.421824, + 17.755058 + ], + [ + 16.504573, + 17.621853, + 17.64346 + ], + [ + 17.144597, + 13.80663, + 12.087995 + ], + [ + 18.623617, + 16.867268, + 17.949815 + ], + [ + 20.120712, + 14.012604, + 15.653385 + ], + [ + 17.513611, + 17.665347, + 17.807132 + ], + [ + 15.083839, + 20.670986, + 19.129174 + ], + [ + 18.615267, + 17.207639, + 18.566503 + ], + [ + 19.445481, + 17.199234, + 18.903923 + ] + ] + } + ], + "824e5b772bbf78d8546ba0afec794646ad0557cbacb59695effdd0c7225689b3": [ + { + "desintxb": [ + -2.815424924298305, + -2.7352960080769932, + -2.668033148613282, + -2.5911971966529475, + -2.4075202964786877, + -2.1645969404546777, + -1.9992930775608295, + -1.8702908935005036, + -1.6682894731774756, + -1.3632129847546643, + -1.0680478680429784, + -0.8602644094912462, + -0.687277991397381, + -0.5601214973695328, + -0.49245906126169103, + -0.4377936027598386, + -0.35835630986631895, + -0.2611362948830651, + -0.13905490735798587, + -0.011624111797918335, + 0.09849793540101048, + 0.2141507174856545, + 0.34755538634475736, + 0.49152429313955137, + 0.5949047671203003, + 0.6470441746402287, + 0.6835283048073051, + 0.7286252504445097, + 0.7763872012536308, + 0.8060555997814998, + 1.000924200713416, + 1.3971273296442082, + 1.6458674734195102, + 1.69008762729251, + 1.7750948636872557, + 1.9992216326241112, + 2.346294913497089, + 2.8014655789155105, + 3.3389099306701095, + 3.7760238169141647 + ], + "dvtrim": 1.5184655539099265, + "ens": [ + 8.33913374118924, + 9.854695512479013, + 11.136070964918652, + 11.204557615755764, + 10.566730528634004, + 12.532128611419129, + 12.891736772250134, + 11.884756838310437, + 9.628033748047173, + 8.925341390596962, + 11.03555478782693, + 12.39781192472875, + 14.928550674855664, + 13.786575262082424, + 13.759465078093395, + 11.621336551541152, + 9.388554783596714, + 14.371650737238472, + 15.567064798786284, + 16.46644959823644, + 14.65795172871933, + 14.417806262192741, + 13.944666545642292, + 12.184644369325532, + 15.644096084769933, + 16.740587309170614, + 18.12487461717064, + 16.330929977845223, + 16.00351484422861, + 14.087043219682593, + 12.216570021842202, + 16.086773414289343, + 16.053972232639822, + 20.064317095434205, + 16.05834963350184, + 17.964182797362177, + 17.686124659130346, + 14.807479362380938, + 16.644665139521674, + 17.864579396009706 + ], + "rep": [ + [ + 8.746096, + 8.059993, + 8.53405, + 8.016395 + ], + [ + 10.431083, + 9.156001, + 9.927431, + 9.904267 + ], + [ + 11.450742, + 10.538875, + 11.192164, + 11.362502 + ], + [ + 11.598451, + 10.489936, + 11.252535, + 11.477309 + ], + [ + 11.026747, + 10.146705, + 10.53527, + 10.5582 + ], + [ + 13.241591, + 11.941235, + 11.937349, + 13.00834 + ], + [ + 13.469671, + 12.545986, + 12.332563, + 13.218728 + ], + [ + 12.323709, + 11.133464, + 11.864324, + 12.217531 + ], + [ + 9.92428, + 9.4411, + 9.742744, + 9.404011 + ], + [ + 9.611433, + 9.093136, + 7.696155, + 9.300642 + ], + [ + 11.88878, + 10.252399, + 10.795609, + 11.205431 + ], + [ + 12.721481, + 11.551679, + 12.536431, + 12.781657 + ], + [ + 14.992785, + 14.920806, + 14.798866, + 15.001746 + ], + [ + 14.766192, + 13.029112, + 13.445462, + 13.905535 + ], + [ + 14.602509, + 13.146825, + 13.545422, + 13.743104 + ], + [ + 12.436377, + 10.999443, + 11.503188, + 11.546338 + ], + [ + 9.547777, + 8.915072, + 8.910138, + 10.181232 + ], + [ + 15.167501, + 13.740238, + 14.230674, + 14.34819 + ], + [ + 16.089524, + 14.979888, + 15.264897, + 15.93395 + ], + [ + 16.423739, + 16.516772, + 16.226929, + 16.698358 + ], + [ + 14.856205, + 14.268882, + 14.62738, + 14.87934 + ], + [ + 14.904602, + 13.563439, + 14.431515, + 14.771669 + ], + [ + 14.436781, + 13.52388, + 13.924303, + 13.893703 + ], + [ + 12.768405, + 11.670962, + 12.04651, + 12.252701 + ], + [ + 16.447834, + 15.06632, + 15.422843, + 15.639388 + ], + [ + 17.371528, + 16.149692, + 16.608377, + 16.832752 + ], + [ + 18.74538, + 17.824137, + 17.74168, + 18.188301 + ], + [ + 17.046693, + 15.693155, + 16.125852, + 16.45802 + ], + [ + 16.366945, + 15.228897, + 16.122475, + 16.295743 + ], + [ + 14.912211, + 13.643598, + 14.152479, + 13.639885 + ], + [ + 12.354243, + 11.916718, + 11.630321, + 12.964998 + ], + [ + 16.328735, + 15.298524, + 16.200583, + 16.519251 + ], + [ + 16.487713, + 15.503896, + 16.108276, + 16.116004 + ], + [ + 20.842335, + 20.017065, + 19.476859, + 19.921011 + ], + [ + 16.64165, + 15.37068, + 16.121538, + 16.099531 + ], + [ + 18.957995, + 17.203187, + 17.627438, + 18.068111 + ], + [ + 17.906252, + 17.263649, + 17.656517, + 17.918081 + ], + [ + 15.251296, + 14.393123, + 14.700974, + 14.884524 + ], + [ + 17.17948, + 15.903642, + 16.760442, + 16.735096 + ], + [ + 18.385448, + 16.984347, + 17.854652, + 18.233871 + ] + ] + }, + { + "desintxb": [ + -2.815424924298305, + -2.7352960080769932, + -2.668033148613282, + -2.5911971966529475, + -2.4075202964786877, + -2.1645969404546777, + -1.9992930775608295, + -1.8702908935005036, + -1.6682894731774756, + -1.3632129847546643, + -1.0680478680429784, + -0.8602644094912462, + -0.687277991397381, + -0.5601214973695328, + -0.49245906126169103, + -0.4377936027598386, + -0.35835630986631895, + -0.2611362948830651, + -0.13905490735798587, + -0.011624111797918335, + 0.09849793540101048, + 0.2141507174856545, + 0.34755538634475736, + 0.49152429313955137, + 0.5949047671203003, + 0.6470441746402287, + 0.6835283048073051, + 0.7286252504445097, + 0.7763872012536308, + 0.8060555997814998, + 1.000924200713416, + 1.3971273296442082, + 1.6458674734195102, + 1.69008762729251, + 1.7750948636872557, + 1.9992216326241112, + 2.346294913497089, + 2.8014655789155105, + 3.3389099306701095, + 3.7760238169141647 + ], + "dvtrim": 1.5184655539099265, + "ens": [ + 10.71982905080926, + 10.483825973224944, + 10.392053782119532, + 10.609299646114247, + 11.681312257422455, + 9.88483653556693, + 10.478054570327515, + 11.320285963771676, + 12.301890129063318, + 13.937413738090886, + 10.3486256298984, + 12.286673895390685, + 11.947027518927724, + 10.78590937917798, + 12.537302519717795, + 12.286830725504146, + 18.26612368691307, + 11.868428505657585, + 12.850631989493872, + 14.040851352258132, + 14.459366425039674, + 14.220208665725728, + 15.131459314589682, + 15.366415018002597, + 15.118091019172407, + 14.110362588211533, + 16.03957680059544, + 12.668095969647153, + 15.463847407839282, + 15.838100020017622, + 18.9271532580157, + 16.230009762338636, + 16.91387386050134, + 18.975079501123513, + 15.846076471079254, + 14.268389663383637, + 17.457655054008505, + 18.503436031924704, + 15.703853582242516, + 17.601071368537358 + ], + "rep": [ + [ + 9.84016, + 11.166178, + 11.396759, + 10.476219 + ], + [ + 12.284328, + 9.879027, + 10.077875, + 9.694073 + ], + [ + 11.562912, + 10.15785, + 10.473239, + 9.374214 + ], + [ + 11.706486, + 10.155259, + 11.004806, + 9.570648 + ], + [ + 13.958378, + 11.22232, + 11.392919, + 10.151632 + ], + [ + 9.156017, + 10.786231, + 10.559939, + 9.037159 + ], + [ + 9.490623, + 11.152754, + 11.11628, + 10.152561 + ], + [ + 12.111004, + 10.778225, + 12.178831, + 10.213085 + ], + [ + 12.276999, + 11.51207, + 13.49271, + 11.925781 + ], + [ + 12.195202, + 14.844615, + 15.431434, + 13.278404 + ], + [ + 9.298859, + 10.30248, + 11.02069, + 10.772473 + ], + [ + 13.683182, + 11.76278, + 12.514174, + 11.186559 + ], + [ + 10.521825, + 11.309716, + 13.850247, + 12.106322 + ], + [ + 10.611846, + 10.530598, + 11.471687, + 10.529506 + ], + [ + 14.192061, + 11.93057, + 12.190602, + 11.835976 + ], + [ + 12.359149, + 11.716064, + 13.149194, + 11.922916 + ], + [ + 13.892912, + 20.131581, + 20.056462, + 18.983539 + ], + [ + 13.878772, + 12.329821, + 10.491917, + 10.773204 + ], + [ + 12.698029, + 11.972726, + 13.996054, + 12.735719 + ], + [ + 12.394057, + 14.957729, + 15.469195, + 13.342424 + ], + [ + 16.564273, + 13.96829, + 14.268581, + 13.036322 + ], + [ + 15.475086, + 13.652465, + 14.723137, + 13.030147 + ], + [ + 17.579656, + 13.978558, + 15.063151, + 13.904473 + ], + [ + 15.336353, + 14.276779, + 16.534447, + 15.318081 + ], + [ + 17.368977, + 13.967654, + 15.117216, + 14.018517 + ], + [ + 14.067957, + 13.4136, + 15.343546, + 13.616348 + ], + [ + 14.08833, + 16.476592, + 16.062477, + 17.530908 + ], + [ + 11.652541, + 13.101481, + 12.884636, + 13.033726 + ], + [ + 16.889903, + 14.881221, + 15.723313, + 14.360953 + ], + [ + 15.523615, + 16.463925, + 16.747776, + 14.617084 + ], + [ + 16.996759, + 19.070706, + 18.982153, + 20.658995 + ], + [ + 17.635125, + 15.859916, + 16.479649, + 14.945349 + ], + [ + 19.548247, + 16.044667, + 16.661067, + 15.401514 + ], + [ + 15.182529, + 20.715884, + 20.630175, + 19.37173 + ], + [ + 18.078905, + 16.008394, + 14.701274, + 14.595733 + ], + [ + 13.17423, + 14.350259, + 14.98078, + 14.56829 + ], + [ + 19.973546, + 16.59131, + 17.224151, + 16.041613 + ], + [ + 17.269545, + 17.486833, + 20.586559, + 18.670807 + ], + [ + 14.986592, + 15.797742, + 16.02386, + 16.00722 + ], + [ + 18.620473, + 16.90834, + 18.579237, + 16.296235 + ] + ] + } + ], + "a8c9a74391d7ae9707002fcdbf8badd2f593a649f3782431d7fe7bf11dc2f95b": [ + { + "desintxb": [ + -2.815424924298305, + -2.7352960080769932, + -2.668033148613282, + -2.5911971966529475, + -2.4075202964786877, + -2.1645969404546777, + -1.9992930775608295, + -1.8702908935005036, + -1.6682894731774756, + -1.3632129847546643, + -1.0680478680429784, + -0.8602644094912462, + -0.687277991397381, + -0.5601214973695328, + -0.49245906126169103, + -0.4377936027598386, + -0.35835630986631895, + -0.2611362948830651, + -0.13905490735798587, + -0.011624111797918335, + 0.09849793540101048, + 0.2141507174856545, + 0.34755538634475736, + 0.49152429313955137, + 0.5949047671203003, + 0.6470441746402287, + 0.6835283048073051, + 0.7286252504445097, + 0.7763872012536308, + 0.8060555997814998, + 1.000924200713416, + 1.3971273296442082, + 1.6458674734195102, + 1.69008762729251, + 1.7750948636872557, + 1.9992216326241112, + 2.346294913497089, + 2.8014655789155105, + 3.3389099306701095, + 3.7760238169141647 + ], + "dvtrim": 1.5184655539099265, + "ens": [ + 13.246177229049874, + 14.944469432732467, + 15.252345275815017, + 15.520349449546906, + 14.868498621449175, + 12.439178012330887, + 12.482452616061734, + 15.304273954805668, + 13.244886503414287, + 14.336196792618756, + 13.505328559112336, + 15.351223554012336, + 13.24816148784504, + 12.011570346365211, + 14.601645365596932, + 12.594014584429194, + 14.411537284575564, + 13.792667167297537, + 12.151494065898573, + 13.076846684910818, + 14.628452639162498, + 15.580741319439129, + 15.408680679379733, + 13.109950009956892, + 15.095718583427146, + 12.375891217524307, + 11.46046169529899, + 13.4600110998109, + 15.255519880445654, + 13.108985016775327, + 12.931205955077452, + 15.385791601116313, + 15.195235489206741, + 12.77848065350582, + 14.188001781370765, + 12.98614192838958, + 14.989848728013966, + 14.322100031999586, + 13.895604462833607, + 15.329188870843792 + ], + "rep": [ + [ + 13.299748, + 14.256292, + 13.38209, + 12.051816, + 13.240941 + ], + [ + 16.657421, + 13.990758, + 15.094109, + 14.181588, + 14.798472 + ], + [ + 14.167486, + 14.231722, + 15.929913, + 16.745675, + 15.186932 + ], + [ + 15.640512, + 13.952573, + 15.969353, + 17.008251, + 15.031058 + ], + [ + 13.743885, + 15.688045, + 15.03883, + 14.443057, + 15.428676 + ], + [ + 14.469011, + 12.069205, + 12.414578, + 11.857061, + 11.386035 + ], + [ + 12.353024, + 13.467866, + 12.565588, + 11.528804, + 12.496982 + ], + [ + 15.749705, + 13.481828, + 15.686404, + 17.097266, + 14.506166 + ], + [ + 13.235261, + 13.662559, + 14.309298, + 11.654709, + 13.362605 + ], + [ + 12.314466, + 18.09306, + 11.566805, + 14.066665, + 15.639988 + ], + [ + 17.122764, + 11.723965, + 13.624579, + 14.003626, + 11.051709 + ], + [ + 14.857904, + 13.525066, + 16.336515, + 16.971823, + 15.064809 + ], + [ + 11.795488, + 15.59825, + 13.248136, + 13.074505, + 12.524429 + ], + [ + 15.102808, + 10.316235, + 12.412885, + 11.745811, + 10.480113 + ], + [ + 16.207503, + 13.632016, + 14.741019, + 14.005129, + 14.422561 + ], + [ + 15.818423, + 10.883794, + 13.125765, + 12.098349, + 11.043742 + ], + [ + 13.928161, + 15.494947, + 12.708758, + 14.513469, + 15.412352 + ], + [ + 16.010134, + 13.510102, + 14.810077, + 13.887407, + 10.745615 + ], + [ + 12.255138, + 10.766422, + 11.901844, + 12.497438, + 13.336628 + ], + [ + 11.016636, + 16.791506, + 9.530555, + 13.358814, + 14.686722 + ], + [ + 13.534704, + 15.43252, + 14.715031, + 14.277824, + 15.182184 + ], + [ + 15.669822, + 14.033519, + 16.042468, + 17.024053, + 15.133844 + ], + [ + 16.537267, + 15.689117, + 15.066021, + 14.183459, + 15.567539 + ], + [ + 13.382989, + 11.769489, + 13.181364, + 13.093453, + 14.122454 + ], + [ + 16.106399, + 15.415211, + 14.722512, + 14.007632, + 15.226838 + ], + [ + 12.120711, + 12.842484, + 13.192264, + 11.113913, + 12.610084 + ], + [ + 9.905046, + 11.531542, + 9.782786, + 12.800021, + 13.282913 + ], + [ + 15.20079, + 12.733294, + 14.524271, + 14.152628, + 10.689072 + ], + [ + 14.747458, + 13.419078, + 16.226371, + 16.934907, + 14.949786 + ], + [ + 15.231125, + 12.741463, + 13.133316, + 12.362225, + 12.076797 + ], + [ + 11.793591, + 13.102005, + 11.892213, + 13.570088, + 14.298132 + ], + [ + 14.333185, + 14.407388, + 16.082485, + 16.790548, + 15.315351 + ], + [ + 14.240097, + 15.673701, + 15.537543, + 15.224708, + 15.300128 + ], + [ + 11.797483, + 13.639939, + 10.455428, + 13.675204, + 14.32435 + ], + [ + 16.473162, + 13.886264, + 15.220381, + 14.077386, + 11.282816 + ], + [ + 16.509174, + 11.204595, + 12.984197, + 13.731176, + 10.501567 + ], + [ + 14.066, + 15.438044, + 15.295344, + 15.090493, + 15.059363 + ], + [ + 12.980698, + 16.84055, + 14.735296, + 13.700482, + 13.353474 + ], + [ + 15.717254, + 13.175628, + 14.970568, + 14.385755, + 11.228817 + ], + [ + 15.753179, + 13.534597, + 15.712367, + 17.104803, + 14.540999 + ] + ] + } + ], + "b7e8f10d3c22a49d7f6c4ac1d5af391b7e489973cf4d55fcb5f50436c807ffa5": [ + { + "desintxb": [ + -2.815424924298305, + -2.7352960080769932, + -2.668033148613282, + -2.5911971966529475, + -2.4075202964786877, + -2.1645969404546777, + -1.9992930775608295, + -1.8702908935005036, + -1.6682894731774756, + -1.3632129847546643, + -1.0680478680429784, + -0.8602644094912462, + -0.687277991397381, + -0.5601214973695328, + -0.49245906126169103, + -0.4377936027598386, + -0.35835630986631895, + -0.2611362948830651, + -0.13905490735798587, + -0.011624111797918335, + 0.09849793540101048, + 0.2141507174856545, + 0.34755538634475736, + 0.49152429313955137, + 0.5949047671203003, + 0.6470441746402287, + 0.6835283048073051, + 0.7286252504445097, + 0.7763872012536308, + 0.8060555997814998, + 1.000924200713416, + 1.3971273296442082, + 1.6458674734195102, + 1.69008762729251, + 1.7750948636872557, + 1.9992216326241112, + 2.346294913497089, + 2.8014655789155105, + 3.3389099306701095, + 3.7760238169141647 + ], + "dvtrim": 1.5184655539099265, + "ens": [ + 10.360727455116953, + 11.814292590572954, + 12.499909225945409, + 12.823441219268638, + 12.368320832596359, + 10.886256751542662, + 10.335222205508732, + 13.429305550943724, + 12.52456527440431, + 14.036115944512876, + 13.354433563963406, + 14.574436239716011, + 13.067379946844072, + 12.654559572198774, + 13.831989313749677, + 13.786083045407656, + 19.912642729869965, + 14.155548080237125, + 13.24501553734178, + 14.04460217573493, + 15.032864210461765, + 16.317282388023013, + 15.853913447025672, + 15.649169417825899, + 15.8031631797127, + 14.387846411011651, + 15.137532582369465, + 16.016806471792542, + 17.67079784007944, + 16.408623153021825, + 18.1282617057915, + 18.19086908370285, + 17.996148605035895, + 20.04910148436405, + 17.91709539937849, + 17.450499634128366, + 18.445140643719014, + 19.499618226531314, + 18.699013094188615, + 19.53283921659602 + ], + "rep": [ + [ + 11.169991, + 10.436566, + 9.349555, + 10.486798 + ], + [ + 11.368098, + 10.649169, + 13.246493, + 11.99341 + ], + [ + 11.06251, + 10.511971, + 14.359651, + 14.065505 + ], + [ + 11.499814, + 10.624012, + 14.643412, + 14.526527 + ], + [ + 11.45397, + 11.245615, + 14.001631, + 12.772068 + ], + [ + 11.68657, + 10.718944, + 11.430606, + 9.708907 + ], + [ + 11.901184, + 11.046149, + 8.805788, + 9.587768 + ], + [ + 12.280669, + 11.386383, + 14.674759, + 15.375412 + ], + [ + 12.798952, + 11.928811, + 13.681491, + 11.689008 + ], + [ + 12.646162, + 12.877886, + 16.402129, + 14.218287 + ], + [ + 13.183534, + 12.123461, + 14.417174, + 13.693564 + ], + [ + 12.929568, + 12.189171, + 17.085347, + 16.093659 + ], + [ + 12.513853, + 12.914931, + 14.933079, + 11.907658 + ], + [ + 13.420361, + 12.214785, + 13.620318, + 11.362774 + ], + [ + 13.58855, + 12.924931, + 15.040766, + 13.773711 + ], + [ + 14.14022, + 12.90835, + 14.979535, + 13.116227 + ], + [ + 23.064557, + 22.141608, + 18.313082, + 16.131324 + ], + [ + 14.125948, + 13.220921, + 15.054408, + 14.220915 + ], + [ + 13.845741, + 13.347038, + 13.194289, + 12.592994 + ], + [ + 13.899054, + 13.865829, + 15.05519, + 13.358336 + ], + [ + 14.376358, + 14.111068, + 16.478394, + 15.165637 + ], + [ + 14.933063, + 14.096093, + 18.234742, + 18.005232 + ], + [ + 15.332974, + 14.666127, + 17.435833, + 15.98072 + ], + [ + 15.365357, + 14.879662, + 16.486912, + 15.864747 + ], + [ + 15.475659, + 14.860638, + 17.201118, + 15.675238 + ], + [ + 15.550792, + 14.61952, + 14.572153, + 12.808921 + ], + [ + 17.114734, + 15.231535, + 14.094278, + 14.109583 + ], + [ + 15.893143, + 15.113334, + 16.77744, + 16.283309 + ], + [ + 16.096104, + 15.359788, + 20.050206, + 19.177093 + ], + [ + 16.63322, + 15.708019, + 17.059656, + 16.233597 + ], + [ + 19.114688, + 16.909771, + 18.618846, + 17.869741 + ], + [ + 16.659023, + 16.130175, + 20.22854, + 19.745739 + ], + [ + 16.836633, + 16.520028, + 19.911275, + 18.716658 + ], + [ + 23.500673, + 23.134679, + 17.468992, + 16.092063 + ], + [ + 17.607919, + 16.69365, + 19.178302, + 18.18851 + ], + [ + 17.593346, + 16.593589, + 18.237174, + 17.377891 + ], + [ + 17.500234, + 17.114634, + 20.212682, + 18.953013 + ], + [ + 17.886916, + 18.43268, + 22.5859, + 19.092977 + ], + [ + 18.265363, + 17.467555, + 19.783132, + 19.280003 + ], + [ + 18.357473, + 17.482701, + 20.81151, + 21.479673 + ] + ] + } + ], + "b863c8b67e89dfb6b0270a0dde1aff710d1648a5c10966af73932f0b8a61f7a9": [ + { + "desintxb": [ + -2.815424924298305, + -2.7352960080769932, + -2.668033148613282, + -2.5911971966529475, + -2.4075202964786877, + -2.1645969404546777, + -1.9992930775608295, + -1.8702908935005036, + -1.6682894731774756, + -1.3632129847546643, + -1.0680478680429784, + -0.8602644094912462, + -0.687277991397381, + -0.5601214973695328, + -0.49245906126169103, + -0.4377936027598386, + -0.35835630986631895, + -0.2611362948830651, + -0.13905490735798587, + -0.011624111797918335, + 0.09849793540101048, + 0.2141507174856545, + 0.34755538634475736, + 0.49152429313955137, + 0.5949047671203003, + 0.6470441746402287, + 0.6835283048073051, + 0.7286252504445097, + 0.7763872012536308, + 0.8060555997814998, + 1.000924200713416, + 1.3971273296442082, + 1.6458674734195102, + 1.69008762729251, + 1.7750948636872557, + 1.9992216326241112, + 2.346294913497089, + 2.8014655789155105, + 3.3389099306701095, + 3.7760238169141647 + ], + "dvtrim": 1.5184655539099265, + "ens": [ + 10.334064626821583, + 11.200506909334141, + 11.213758504877228, + 11.711999178094612, + 11.721790081337794, + 9.36989641150621, + 9.52451109284261, + 12.269568109796944, + 11.905133597976835, + 13.644028065473972, + 12.486684749520995, + 13.250875926101882, + 10.786473546574907, + 11.005700710091489, + 12.881587482541528, + 12.883033834091869, + 16.089891425165373, + 12.818709052366126, + 11.527522548837675, + 12.422537253006759, + 14.211061080101231, + 15.332480069232787, + 15.569949211377716, + 14.879082930220623, + 15.19792053659738, + 13.000223377901234, + 12.504463297340378, + 14.449115555148492, + 16.294865445946833, + 15.76113723390753, + 16.87751941687521, + 17.133619571258038, + 17.32844714755388, + 15.236964174620866, + 17.053408417665572, + 15.9730913488264, + 17.58386462000311, + 18.278788261513057, + 17.658658550807576, + 18.496395278188086 + ], + "rep": [ + [ + 10.741786, + 10.95391, + 10.307797, + 9.58797, + 9.815447, + 10.597477 + ], + [ + 13.192367, + 10.570326, + 11.405886, + 11.095521, + 11.1326, + 9.806341 + ], + [ + 10.940116, + 10.527518, + 11.825256, + 13.168957, + 11.197703, + 9.623002 + ], + [ + 12.401862, + 10.559689, + 12.107697, + 13.630173, + 11.276159, + 10.296414 + ], + [ + 11.287456, + 12.483602, + 11.919155, + 11.874328, + 12.207781, + 10.558418 + ], + [ + 11.290537, + 8.873271, + 9.373527, + 8.808848, + 7.61131, + 10.261886 + ], + [ + 9.671239, + 10.145098, + 9.601837, + 8.687487, + 8.818886, + 10.222519 + ], + [ + 13.282459, + 10.945991, + 12.704095, + 14.479121, + 11.649283, + 10.556459 + ], + [ + 12.371128, + 11.99894, + 12.760479, + 10.789954, + 11.551095, + 11.959206 + ], + [ + 11.98264, + 16.308176, + 11.637657, + 13.320901, + 14.126441, + 14.488353 + ], + [ + 15.429672, + 10.502807, + 12.273004, + 12.795669, + 9.546262, + 14.372693 + ], + [ + 13.443094, + 11.871059, + 14.057084, + 15.19734, + 13.138222, + 11.798456 + ], + [ + 10.205994, + 12.706574, + 10.742151, + 11.008219, + 9.967808, + 10.088095 + ], + [ + 13.375134, + 9.002603, + 10.914338, + 10.462812, + 8.48596, + 13.793356 + ], + [ + 14.647987, + 12.151956, + 13.100443, + 12.875332, + 12.63641, + 11.877397 + ], + [ + 15.449335, + 10.784475, + 12.921906, + 12.217243, + 10.559088, + 15.366155 + ], + [ + 15.263101, + 15.902273, + 13.992283, + 15.234355, + 15.45433, + 20.693007 + ], + [ + 15.062804, + 12.604728, + 13.683914, + 13.322449, + 9.57126, + 12.6671 + ], + [ + 11.774711, + 9.975735, + 11.062263, + 11.693232, + 11.994189, + 12.665005 + ], + [ + 10.858607, + 14.99101, + 8.923186, + 12.458984, + 13.31507, + 13.988367 + ], + [ + 13.891305, + 14.889759, + 14.256985, + 14.267438, + 14.699264, + 13.261616 + ], + [ + 15.955084, + 14.176798, + 15.733663, + 17.108922, + 15.205516, + 13.814897 + ], + [ + 17.147513, + 15.916606, + 15.413866, + 15.082831, + 16.059642, + 13.799238 + ], + [ + 15.401452, + 13.403078, + 14.936617, + 14.96664, + 15.329948, + 15.236763 + ], + [ + 16.518858, + 15.619585, + 15.016107, + 14.776861, + 15.452684, + 13.803429 + ], + [ + 13.034115, + 13.20018, + 13.48436, + 11.908365, + 12.744275, + 13.630045 + ], + [ + 10.873803, + 11.675883, + 10.398233, + 13.209819, + 13.323434, + 15.545607 + ], + [ + 16.318504, + 13.795733, + 15.36737, + 15.384959, + 11.528514, + 14.299614 + ], + [ + 16.473275, + 14.836496, + 17.073586, + 18.280671, + 16.220701, + 14.884464 + ], + [ + 17.73273, + 15.090296, + 15.622698, + 15.334942, + 14.491447, + 16.294711 + ], + [ + 16.100333, + 16.315171, + 15.970038, + 16.972116, + 17.13765, + 18.769808 + ], + [ + 16.944063, + 16.466884, + 17.746273, + 18.849316, + 17.418147, + 15.377035 + ], + [ + 17.070834, + 17.770882, + 17.605566, + 17.819367, + 17.717895, + 15.986139 + ], + [ + 13.530549, + 14.546646, + 12.12889, + 15.192765, + 15.653273, + 20.369663 + ], + [ + 19.44496, + 16.761344, + 17.885445, + 17.290571, + 14.586682, + 16.351448 + ], + [ + 18.82218, + 13.965682, + 15.619741, + 16.479239, + 12.950214, + 18.001492 + ], + [ + 17.434645, + 17.948037, + 17.822869, + 18.055348, + 17.809859, + 16.43243 + ], + [ + 17.839207, + 20.521762, + 18.933162, + 18.195277, + 17.459141, + 16.724181 + ], + [ + 19.665261, + 16.976793, + 18.497689, + 18.382301, + 15.34323, + 17.086678 + ], + [ + 19.484644, + 17.151253, + 18.913411, + 20.583404, + 18.196493, + 16.649167 + ] + ] + } + ], + "eb61f76562db36004796c4ba4cc1a06dd0e7c08e554ab80de9c617791d437a35": [ + { + "desintxb": [ + -2.815424924298305, + -2.7352960080769932, + -2.668033148613282, + -2.5911971966529475, + -2.4075202964786877, + -2.1645969404546777, + -1.9992930775608295, + -1.8702908935005036, + -1.6682894731774756, + -1.3632129847546643, + -1.0680478680429784, + -0.8602644094912462, + -0.687277991397381, + -0.5601214973695328, + -0.49245906126169103, + -0.4377936027598386, + -0.35835630986631895, + -0.2611362948830651, + -0.13905490735798587, + -0.011624111797918335, + 0.09849793540101048, + 0.2141507174856545, + 0.34755538634475736, + 0.49152429313955137, + 0.5949047671203003, + 0.6470441746402287, + 0.6835283048073051, + 0.7286252504445097, + 0.7763872012536308, + 0.8060555997814998, + 1.000924200713416, + 1.3971273296442082, + 1.6458674734195102, + 1.69008762729251, + 1.7750948636872557, + 1.9992216326241112, + 2.346294913497089, + 2.8014655789155105, + 3.3389099306701095, + 3.7760238169141647 + ], + "dvtrim": 1.5184655539099265, + "ens": [ + 14.651355137814383, + 13.96582364144213, + 12.991294973241137, + 13.240118123219215, + 13.863043517675756, + 11.398999373867698, + 11.246303269162775, + 13.490911653300694, + 15.26002534143386, + 16.203006100340957, + 14.611048237122992, + 13.979967611444994, + 11.025896504007493, + 12.008606397409146, + 12.98364204238408, + 14.879065166732836, + 17.420004123506004, + 12.904603643140138, + 11.41579505264216, + 10.906252600699132, + 13.504355288395113, + 14.4261253505999, + 14.999640149940342, + 16.185035945730196, + 13.607067375216994, + 12.001740728396168, + 10.609175735979344, + 13.262033250974609, + 14.38242945564684, + 15.893890936224617, + 17.554116314183453, + 15.053531921516695, + 15.244448990315004, + 10.78079746275924, + 15.65245902937969, + 13.409989725815528, + 14.442794212737285, + 17.293869771484523, + 15.91012735287, + 15.209937122693217 + ], + "rep": [ + [ + 15.080777, + 14.613914, + 14.103514, + 14.87629, + 14.582281 + ], + [ + 14.614437, + 13.678027, + 13.818724, + 13.893731, + 13.8242 + ], + [ + 13.094071, + 12.564461, + 13.275251, + 13.472379, + 12.550313 + ], + [ + 13.620902, + 12.794781, + 13.456251, + 13.677942, + 12.650715 + ], + [ + 13.878925, + 13.784222, + 13.911429, + 14.040357, + 13.700284 + ], + [ + 12.102401, + 11.371669, + 11.776973, + 11.045073, + 10.698881 + ], + [ + 11.489889, + 11.316297, + 11.709893, + 10.973463, + 10.741975 + ], + [ + 13.887464, + 13.005926, + 13.66628, + 13.924415, + 12.970472 + ], + [ + 15.804882, + 15.019312, + 15.170957, + 15.284305, + 15.020671 + ], + [ + 16.089847, + 16.627033, + 16.029463, + 16.369345, + 15.899342 + ], + [ + 15.45443, + 14.237246, + 14.386064, + 14.694109, + 14.283393 + ], + [ + 14.188812, + 13.564173, + 14.219434, + 14.279947, + 13.647472 + ], + [ + 11.238494, + 10.83915, + 11.128624, + 11.109322, + 10.813893 + ], + [ + 12.683378, + 11.812669, + 12.185616, + 12.028999, + 11.33237 + ], + [ + 13.415344, + 12.691072, + 13.30389, + 13.125741, + 12.382163 + ], + [ + 15.724444, + 14.502464, + 14.637388, + 14.961932, + 14.569098 + ], + [ + 18.318805, + 17.924653, + 16.96594, + 17.302957, + 16.587665 + ], + [ + 13.511706, + 12.788923, + 13.381101, + 13.204082, + 11.637207 + ], + [ + 11.716664, + 11.142259, + 11.450617, + 11.4372, + 11.332235 + ], + [ + 11.255973, + 11.187995, + 9.811924, + 11.181181, + 11.094189 + ], + [ + 13.819223, + 13.418253, + 13.643222, + 13.51258, + 13.128498 + ], + [ + 14.641363, + 14.06414, + 14.597456, + 14.704925, + 14.122743 + ], + [ + 15.609811, + 14.725269, + 14.847771, + 14.941535, + 14.873815 + ], + [ + 16.601952, + 15.969294, + 15.921724, + 16.476665, + 15.955544 + ], + [ + 13.939012, + 13.575426, + 13.814544, + 13.628621, + 13.077734 + ], + [ + 12.074256, + 12.127278, + 12.33589, + 11.788569, + 11.68271 + ], + [ + 10.277892, + 10.362691, + 10.105054, + 11.198753, + 11.101488 + ], + [ + 13.792397, + 13.056431, + 13.762541, + 13.575528, + 12.123269 + ], + [ + 14.584895, + 13.930545, + 14.70607, + 14.833299, + 13.857339 + ], + [ + 16.434567, + 15.639208, + 15.348074, + 16.304473, + 15.743133 + ], + [ + 18.239516, + 17.728773, + 17.264004, + 17.586868, + 16.951422 + ], + [ + 15.225069, + 14.816034, + 15.228584, + 15.282973, + 14.714999 + ], + [ + 15.278715, + 15.138412, + 15.296074, + 15.44029, + 15.068755 + ], + [ + 10.412057, + 10.310181, + 10.382258, + 11.416821, + 11.382671 + ], + [ + 16.264678, + 15.355361, + 15.584685, + 15.580714, + 15.476858 + ], + [ + 14.18267, + 13.117343, + 13.52346, + 13.75475, + 12.471725 + ], + [ + 14.763201, + 14.263096, + 14.697064, + 14.561766, + 13.928843 + ], + [ + 17.359521, + 17.75781, + 17.284727, + 17.389562, + 16.677729 + ], + [ + 16.464979, + 15.624821, + 15.784862, + 15.998894, + 15.677082 + ], + [ + 15.501742, + 14.846713, + 15.351931, + 15.584978, + 14.764321 + ] + ] + } + ] + }, + "label": "boot14.meboot", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/boot14.seas.json b/tests/_r_cache/boot14.seas.json new file mode 100644 index 00000000..20f433ed --- /dev/null +++ b/tests/_r_cache/boot14.seas.json @@ -0,0 +1,65 @@ +{ + "entries": { + "765bfc77f94802334816bbb90487c5712b86d35c8c43dbe4adae8ba4fc1ba727": { + "cv": [ + 0.057538992536810864, + 0.05970848729837231, + 0.14094128604314804, + 0.15788667819132546, + 0.16659919078192661, + 0.1680352658594879, + 0.16956663385723425 + ], + "p": [ + 12.0, + 24.0, + 23.0, + 6.0, + 13.0, + 4.0, + 3.0 + ], + "vcv": [ + 0.16998739297769044, + 0.16998739297769044, + 0.16998739297769044, + 0.16998739297769044, + 0.16998739297769044, + 0.16998739297769044, + 0.16998739297769044 + ] + }, + "aa2cb7a2bfaa7fa91ad9287b4ffa5947ebd5a656f19f92964060a7aaa7e201a7": { + "cv": [ + 0.05873125621808862, + 0.06050106442705748, + 0.13963968660690584, + 0.15707555754275834, + 0.16494671810828695, + 0.1669859740233193, + 0.16829128756221953 + ], + "p": [ + 12.0, + 24.0, + 23.0, + 6.0, + 13.0, + 4.0, + 3.0 + ], + "vcv": [ + 0.16873825680146123, + 0.16873825680146123, + 0.16873825680146123, + 0.16873825680146123, + 0.16873825680146123, + 0.16873825680146123, + 0.16873825680146123 + ] + } + }, + "label": "boot14.seas", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/boot14.ss.json b/tests/_r_cache/boot14.ss.json new file mode 100644 index 00000000..2e2148a0 --- /dev/null +++ b/tests/_r_cache/boot14.ss.json @@ -0,0 +1,100 @@ +{ + "entries": { + "117a7aab175e251a2ac67f4b0cac3bbf55639ec833c83ba4bd4b47bdb756587e": { + "boot_vals": [ + 0.7426666666666667, + 0.49066666666666664, + 0.5066666666666667, + 0.604, + 0.6133333333333333, + 0.74, + 0.5693333333333334, + 0.6506666666666666, + 0.724, + 0.672, + 0.6546666666666666, + 0.624, + 0.636, + 0.66, + 0.6506666666666666, + 0.7373333333333333, + 0.552, + 0.6173333333333333, + 0.752, + 0.6613333333333333 + ], + "ci": 0.9, + "lower": 0.5058666666666667, + "p_gt": 0.5893333333333334, + "p_star": 0.65, + "p_tie": 0.12133333333333333, + "reps": 20.0, + "upper": 0.7431333333333333 + }, + "2f989420361cbb95c343aa3d4469af6a761998f680f984be9df0aed41cf7a73e": { + "boot_vals": [ + 0.74, + 0.6506666666666666, + 0.644, + 0.6506666666666666, + 0.7733333333333333, + 0.804, + 0.72, + 0.748, + 0.7546666666666667, + 0.7946666666666666, + 0.6666666666666666, + 0.792, + 0.7, + 0.7666666666666667, + 0.7813333333333333, + 0.764, + 0.5826666666666667, + 0.7053333333333334, + 0.7533333333333333, + 0.7626666666666667 + ], + "ci": 0.9, + "lower": 0.6409333333333334, + "p_gt": 0.7333333333333333, + "p_star": 0.7333333333333333, + "p_tie": 0.0, + "reps": 20.0, + "upper": 0.7951333333333334 + }, + "33fc5312df221312892f660c58eb2bd37213edb2b3d230d439bc720a89dd7767": { + "boot_vals": [ + 0.9666666666666667, + 0.9666666666666667, + 0.9, + 0.9333333333333333, + 0.8666666666666667, + 0.9, + 0.9, + 0.8333333333333334, + 0.9333333333333333, + 0.9333333333333333, + 0.9666666666666667, + 0.7666666666666667, + 0.9, + 0.9333333333333333, + 0.9, + 0.9, + 0.8666666666666667, + 0.9333333333333333, + 0.9333333333333333, + 0.8666666666666667 + ], + "ci": 0.9, + "lower": 0.8300000000000001, + "p_gt": 0.8666666666666667, + "p_star": 0.8666666666666667, + "p_tie": 0.0, + "reps": 20.0, + "upper": 0.9666666666666667 + } + }, + "label": "boot14.ss", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/invariants/test_mc.py b/tests/invariants/test_mc.py index 0a8a2109..33b4e353 100644 --- a/tests/invariants/test_mc.py +++ b/tests/invariants/test_mc.py @@ -57,7 +57,7 @@ def test_nns_mc_xmin_xmax_clipping_is_respected() -> None: def test_nns_mc_lower_greater_than_upper_errors() -> None: x = np.linspace(-2.0, 4.0, 30) + 0.1 * np.sin(np.arange(30, dtype=np.float64)) - with pytest.raises(ValueError, match="rho grid"): + with pytest.raises(ValueError, match="lower_rho and upper_rho must satisfy"): nns_mc(x, lower_rho=1.0, upper_rho=-1.0, by=0.5) diff --git a/tests/parity/test_bootstrap_r14.py b/tests/parity/test_bootstrap_r14.py new file mode 100644 index 00000000..2e7e26ee --- /dev/null +++ b/tests/parity/test_bootstrap_r14.py @@ -0,0 +1,215 @@ +"""R 14.0 parity for the maximum-entropy bootstrap and what is built on it. + +``NNS.meboot`` draws only through ``runif``, which the port reproduces from R's +Mersenne-Twister stream, so with ``random_seed = s`` the replicates are R's +after ``set.seed(s)``, bit for bit; so are ``NNS.MC``'s, ``NNS.SS``'s bootstrap +bounds and ``NNS.ARMA``'s prediction intervals, which resample the forecast path +with ``NNS.MC``. The deterministic steps follow R's arithmetic (``optimize``, +``qnorm``, trimmed ``mean``, long-double sums), and ``force.clt`` spreads the +replicate means by ``sd / sqrt(n)``, R 14's correction of meboot's +``sqrt(reps)``. +""" + +from __future__ import annotations + +import warnings +from typing import Any + +import numpy as np +import pytest +from _r import nns_frame_call, r_column + +import nns +from nns.mc import _format_r_number + +pytestmark = pytest.mark.parity + + +def _num(values: Any) -> dict[str, Any]: + return r_column([float(v) for v in np.asarray(values, dtype=np.float64).ravel()], "numeric") + + +def _series(seed: int, n: int) -> np.ndarray: + rng = np.random.default_rng(seed) + t = np.arange(n, dtype=np.float64) + return 10.0 + 0.2 * t + 2.0 * np.sin(2.0 * np.pi * t / 7.0) + rng.normal(size=n) + + +def _rows(value: Any) -> np.ndarray: + return np.asarray(value, dtype=np.float64).reshape(len(value), -1) + + +@pytest.mark.parametrize( + ("r_args", "py_args"), + [ + ("reps = 6, rho = 0.5", {"reps": 6, "rho": 0.5}), + ("reps = 4, rho = c(-1, 0.3)", {"reps": 4, "rho": [-1.0, 0.3]}), + ("reps = 3, rho = 0.4, type = 'pearson'", {"reps": 3, "rho": 0.4, "type": "pearson"}), + ("reps = 3, rho = 0.6, type = 'nnsdep'", {"reps": 3, "rho": 0.6, "type": "nnsdep"}), + ("reps = 5, target_drift = 0.05", {"reps": 5, "target_drift": 0.05}), + ("reps = 5, rho = 0.2, drift = FALSE", {"reps": 5, "rho": 0.2, "drift": False}), + ( + "reps = 4, rho = 0.5, xmin = 0, xmax = 30", + {"reps": 4, "rho": 0.5, "xmin": 0.0, "xmax": 30.0}, + ), + ( + "reps = 4, rho = 0.5, scl.adjustment = TRUE, sym = TRUE, trim = 0.25", + {"reps": 4, "rho": 0.5, "scl_adjustment": True, "sym": True, "trim": 0.25}, + ), + ], +) +def test_nns_meboot_replicates_are_rs_under_set_seed(r_args: str, py_args: dict[str, Any]) -> None: + x = _series(201, 40) + expected = nns_frame_call( + "boot14.meboot", + f"set.seed(42); r <- NNS.meboot(x, {r_args}); lapply(seq_len(ncol(r)), function(j) " + "list(rep = unname(r['replicates', j][[1]]), ens = r['ensemble', j][[1]], " + "dvtrim = r['dvtrim', j][[1]], desintxb = r['desintxb', j][[1]]))", + {"x": _num(x)}, + exact=True, + ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + actual = nns.nns_meboot(x, random_seed=42, **py_args) + results = [actual] if isinstance(actual, dict) else actual + assert len(results) == len(expected) + for got, want in zip(results, expected, strict=True): + np.testing.assert_array_equal(np.asarray(got["replicates"]), _rows(want["rep"])) + np.testing.assert_array_equal(got["ensemble"], np.asarray(want["ens"], dtype=np.float64)) + assert got["dvtrim"] == float(np.asarray(want["dvtrim"])) + np.testing.assert_array_equal(got["desintxb"], np.asarray(want["desintxb"])) + + +def test_force_clt_spreads_replicate_means_like_means_of_n_observations() -> None: + x = _series(202, 30) + result = nns.nns_meboot(x, reps=200, rho=0.0, random_seed=7) + assert isinstance(result, dict) + replicate_means = np.mean(np.asarray(result["replicates"]), axis=0) + # sd(x) / sqrt(n) with n = 30, whatever the number of replicates. + assert np.std(replicate_means, ddof=1) == pytest.approx( + np.std(x, ddof=1) / np.sqrt(30.0), rel=1e-3 + ) + + +@pytest.mark.parametrize( + ("lower", "upper", "by", "exp", "extra_r", "extra_py"), + [ + (-1.0, 1.0, 0.5, 1.0, "", {}), + (-0.5, 0.8, 0.1, 0.5, "", {}), + (0.2, 0.9, 0.35, 3.0, ", target_drift = 0.02", {"target_drift": 0.02}), + ], +) +def test_nns_mc_matches_r_under_set_seed( + lower: float, upper: float, by: float, exp: float, extra_r: str, extra_py: dict[str, Any] +) -> None: + x = _series(203, 24) + expected = nns_frame_call( + "boot14.mc", + f"set.seed(9); r <- NNS.MC(x, reps = 3, lower_rho = {lower!r}, upper_rho = {upper!r}, " + f"by = {by!r}, exp = {exp!r}{extra_r}); list(ensemble = r$ensemble, " + "names = names(r$replicates), reps = lapply(unname(r$replicates), unname))", + {"x": _num(x)}, + exact=True, + ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + actual = nns.nns_mc( + x, reps=3, lower_rho=lower, upper_rho=upper, by=by, exp=exp, random_seed=9, **extra_py + ) + np.testing.assert_array_equal(actual["ensemble"], np.asarray(expected["ensemble"])) + replicates = actual["replicates"] + assert isinstance(replicates, dict) + assert list(replicates) == list(expected["names"]) + for got, want in zip(replicates.values(), expected["reps"], strict=True): + np.testing.assert_array_equal(np.asarray(got), _rows(want)) + + +def test_rho_labels_follow_r_as_character() -> None: + values = [1e-04, 5e-04, 0.0001234, 0.1, 1 / 3, 1e5, 123456.0, -0.99, 0.3, 1e-06, 0.001, + 2.5e-05, 0.12345678901234567, -0.6000000000000001, 0.0, -1.0] + expected = nns_frame_call("boot14.labels", "as.character(v)", {"v": _num(values)}) + assert [_format_r_number(v) for v in values] == list(expected) + + +def test_nns_mc_validates_its_arguments_as_r_does() -> None: + x = _series(204, 20) + with pytest.raises(ValueError, match="exp must be a single finite number > 0"): + nns.nns_mc(x, exp=0.0) + with pytest.raises(ValueError, match="lower_rho and upper_rho must satisfy"): + nns.nns_mc(x, lower_rho=-1.5) + with pytest.raises(ValueError, match="by must be a single finite number > 0"): + nns.nns_mc(x, by=-0.1) + + +@pytest.mark.parametrize(("kind", "rho"), [("shift", 1.0), ("ties", 0.0), ("constant", -0.5)]) +def test_nns_ss_bootstrap_bounds_match_r_under_set_seed(kind: str, rho: float) -> None: + rng = np.random.default_rng(205) + if kind == "shift": + x, y = rng.normal(size=25) + 0.3, rng.normal(size=30) + elif kind == "ties": + x, y = np.round(rng.normal(size=25) * 2), np.round(rng.normal(size=30) * 2) + else: + x, y = np.full(25, 1.5), rng.normal(size=30) + expected = nns_frame_call( + "boot14.ss", + "set.seed(11); NNS.SS(x, y, confidence.interval = TRUE, reps = 20, ci = 0.9, " + f"rho = {rho!r})", + {"x": _num(x), "y": _num(y)}, + exact=True, + ) + actual = nns.nns_ss(x, y, confidence_interval=True, reps=20, ci=0.9, rho=rho, random_seed=11) + for key in ("p_gt", "p_tie", "p_star", "lower", "upper"): + assert float(np.asarray(actual[key])) == float(np.asarray(expected[key])), key + np.testing.assert_array_equal(actual["boot_vals"], np.asarray(expected["boot_vals"])) + + +@pytest.mark.parametrize( + ("h", "seasonal", "method"), + [(4, "TRUE", "nonlin"), (5, "12", "lin"), (3, "c(12, 6)", "both"), (6, "FALSE", "means")], +) +def test_nns_arma_prediction_intervals_match_r_under_set_seed( + h: int, seasonal: str, method: str +) -> None: + rng = np.random.default_rng(206) + t = np.arange(72, dtype=np.float64) + x = np.round(10.0 + 3.0 * np.sin(2.0 * np.pi * t / 12.0) + rng.normal(size=72), 1) + expected = nns_frame_call( + "boot14.arma", + f"set.seed(13); r <- NNS.ARMA(x, h = {h}, seasonal.factor = {seasonal}, " + f"method = '{method}', plot = FALSE, seasonal.plot = FALSE, pred.int = 0.9); " + "lapply(as.list(r), as.numeric)", + {"x": _num(x)}, + exact=True, + ) + seasonal_py: Any = {"TRUE": True, "FALSE": False, "12": 12, "c(12, 6)": [12, 6]}[seasonal] + actual = nns.nns_arma( + x, h=h, seasonal_factor=seasonal_py, method=method, pred_int=0.9, random_seed=13 + ) + assert isinstance(actual, dict) + assert list(actual) == list(expected) + for key, values in expected.items(): + np.testing.assert_array_equal(actual[key], np.asarray(values, dtype=np.float64), key) + + +@pytest.mark.parametrize("kind", ["seasonal", "ties"]) +def test_nns_seas_scores_match_r_bitwise(kind: str) -> None: + rng = np.random.default_rng(207) + t = np.arange(120, dtype=np.float64) + x = (20.0 + 0.3 * t) * (1.0 + 0.2 * np.sin(2.0 * np.pi * t / 12.0)) + rng.normal(size=120) + if kind == "ties": + x = np.round(x) + expected = nns_frame_call( + "boot14.seas", + "a <- NNS.seas(x, plot = FALSE)$all.periods; " + "list(p = a$Period, cv = a$Coefficient.of.Variation, " + "vcv = a$Variable.Coefficient.of.Variation)", + {"x": _num(x)}, + exact=True, + ) + actual = nns.nns_seas(x)["all.periods"] + assert isinstance(actual, dict) + np.testing.assert_array_equal(actual["Period"], np.asarray(expected["p"], dtype=np.int64)) + np.testing.assert_array_equal(actual["Coefficient.of.Variation"], np.asarray(expected["cv"])) + np.testing.assert_array_equal( + actual["Variable.Coefficient.of.Variation"], np.asarray(expected["vcv"]) + ) diff --git a/tests/parity/test_meboot.py b/tests/parity/test_meboot.py index cd7268f8..fd36eba1 100644 --- a/tests/parity/test_meboot.py +++ b/tests/parity/test_meboot.py @@ -186,7 +186,8 @@ def test_nns_meboot_changed_drift_keeps_residual_scale_like_r( np.testing.assert_allclose(actual, expected, rtol=0.35, atol=0.35) assert actual[3] < 0.5 * float(np.std(x, ddof=1)) # force_clt spreads the replicate means as the new baseline plus the original - # residuals, not as x (whose sd includes the replaced drift). + # residuals, not as x (whose sd includes the replaced drift), and like means of + # n = 30 observations: R 14 divides by sqrt(n), not by sqrt(reps) as meboot did. time = np.arange(1.0, 31.0) slope, intercept = np.polyfit(time, x, 1) residuals = np.asarray(x) - (intercept + slope * time) @@ -194,6 +195,6 @@ def test_nns_meboot_changed_drift_keeps_residual_scale_like_r( reference = float(np.mean(x)) + new_slope * (time - 15.5) + residuals np.testing.assert_allclose( np.std(np.mean(replicates, axis=0), ddof=1), - np.std(reference, ddof=1) / np.sqrt(100.0), + np.std(reference, ddof=1) / np.sqrt(30.0), rtol=1e-4, ) From 50ba4019bbbd8afb447efd2cfff0f680fb60774b Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 20:21:54 +0000 Subject: [PATCH 07/16] Add R 14's permutation p-values to NNS.seas, NNS.dep and NNS.caus All four draw their permutations from R's stream, so with a seed the results are R's after set.seed(), bit for bit: - nns_seas(p_value=True, nperm=, random_seed=): each period's dispersion ratio against the best ratio of nperm shuffles scanned by NNS_seas_cpp (the scan is now separate from the fundamental-period step); periods and best.period then follow the significant periods, as in R. - nns_dep(x, y, p_value=True, random_seed=): 100 sample.int permutations of y; two-sided correlation and upper-tail dependence p-values with the shuffled values' 95% partial-moment intervals. - nns_causation(..., p_value=True, nperm=, permute=, seed=, conf_int=): the signed index against R's shift/shuffle nulls at the observed lag, with R's result fields; y=None dispatches to causal_matrix as R does, and permute accepts R's partial matching. - causal_matrix(..., p_value=True, ...): R's causality, lower_CI, upper_CI and p.value_matrix under the "both", "shift", "x" and "y" schemes. The causation and seasonality conventions now describe R 14's indices and estimator rather than R 13's. Live sweeps against R 14.0: 396 p-value cases across two seeds, all identical. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt --- docs/conventions.md | 53 ++++-- src/nns/_rrng.py | 8 +- src/nns/_seas_r14.py | 72 ++++++- src/nns/causation.py | 251 ++++++++++++++++++++++++- src/nns/dependence.py | 71 ++++++- src/nns/seasonality.py | 15 +- tests/_r_cache/pval14.caus.json | 152 +++++++++++++++ tests/_r_cache/pval14.caus_matrix.json | 217 +++++++++++++++++++++ tests/_r_cache/pval14.dep.json | 35 ++++ tests/_r_cache/pval14.seas.json | 49 +++++ tests/parity/test_pvalues_r14.py | 135 +++++++++++++ 11 files changed, 1026 insertions(+), 32 deletions(-) create mode 100644 tests/_r_cache/pval14.caus.json create mode 100644 tests/_r_cache/pval14.caus_matrix.json create mode 100644 tests/_r_cache/pval14.dep.json create mode 100644 tests/_r_cache/pval14.seas.json create mode 100644 tests/parity/test_pvalues_r14.py diff --git a/docs/conventions.md b/docs/conventions.md index 0aad2897..06e5e4d2 100644 --- a/docs/conventions.md +++ b/docs/conventions.md @@ -193,7 +193,11 @@ is on `x` and the result measures `y` as a function of `x`. `nns_dep(X)` on a 2- array or DataFrame returns R's `Correlation` and `Dependence` matrices; with `asym=True` entry `[i, j]` is `nns_dep(X[:, i], X[:, j], asym=True)` (row variable -> column variable, which is what R returns although its manual states the -reverse). R's `p.value` option is not implemented. The bivariate path includes `NNS.gravity` handling +reverse). `p_value=True` runs R's permutation test on the bivariate form: the +measures of `x` against 100 shuffles of `y` give a two-sided `Correlation p.value`, +an upper-tail `Dependence p.value` and the shuffled values' 95% partial-moment +intervals; with `random_seed = s` the shuffles are R's after `set.seed(s)`. +The bivariate path includes `NNS.gravity` handling for zero-range inputs and non-positive or non-finite bin widths. The gravity histogram (bin width `IQR / sqrt(n)`) is built from the occupied bins only, so a single outlier that makes R allocate millions of empty bins costs nothing and gives @@ -217,12 +221,24 @@ input raises `ValueError` (R stops on missing values). ## Causation -`nns_causation(x, y)` maps to R's `NNS.caus(x, y, tau = 0, p.value = FALSE)` -numeric-vector path. It returns the two directional components and the named -signed net log-ratio key selected by R, either `C(x--->y)` or `C(y--->x)`. -`causal_matrix` maps to R's `NNS.caus` on a matrix (internally `NNS.caus.matrix`): -an antisymmetric matrix whose entry `[i, j]` is the net causation of column `j` on -column `i` (positive: `j` causes `i`). A DataFrame input returns a DataFrame labelled with its columns. +`nns_causation(x, y, tau)` maps to R 14's `NNS.caus(x, y)`: the two directional +indices `C(x--->y)` and `C(y--->x)` in `[0, 1]`, which sum to one and are 0.5 when +neither direction dominates. They measure directional lagged association, not +causal identification. `causal_matrix(X)` (also `nns_causation(X)` with `y=None`, +as in R) is R's `NNS.caus.matrix`: entry `[i, j]` is `C(column i ---> column j)`, +so `[i, j] + [j, i] = 1` off the diagonal, which is 0. A DataFrame input returns +DataFrames labelled with its columns. + +`p_value=True` adds R's permutation test, with R's `nperm`, `permute`, `seed` and +`conf_int` (`conf.int`). For a pair, the signed index `C(x--->y) - C(y--->x)` is +compared with its null under `permute="auto"` (a circular shift of `y` at least +`max(tau + 1, n / 10)` steps from alignment for a positive lag, a shuffle of `y` +otherwise) or an explicit `"y"`, `"x"`, `"both"` or `"shift"`; the result is +`{"causation": ..., "p.value": {...}}` with R's fields. For a matrix, +`"auto"` shifts the second column of each pair for time series and shuffles every +column otherwise, and the result holds R's `causality`, `lower_CI`, `upper_CI` +and `p.value_matrix`. Every permutation is scored at the lag resolved on the +observed series, and with `seed = s` the draws are R's after `set.seed(s)`. `tau='ts'` uses `nns_seas(... )["periods"]` exactly like installed R: the first period not exceeding `sqrt(length(x))` is selected per variable, including harmonics when R selects them. Inputs with no eligible selected period follow @@ -455,15 +471,20 @@ example. ## Seasonality -`nns_seas` maps to installed R's `NNS.seas`; `plot=True` draws the figure as a -side effect (see Plotting). Inputs shorter than five observations -return R's sentinel period `0`. For mean-zero data, R falls back from coefficient -of variation to `abs(acf1) ** -1`; NNS Python follows the same fallback and -non-finite handling. Installed R can report harmonics rather than the visually -obvious period, so NNS Python matches R's candidate-period screening instead of a -textbook seasonality heuristic. Results are cached by input content and modulo -arguments with defensive copies on return; this preserves R semantics while -avoiding repeated reverse-step scans for identical series. +`nns_seas` maps to R 14's `NNS.seas`; `plot=True` draws the figure as a side +effect (see Plotting). A period's score is the length-weighted Gini mean +difference of its component series (relative to the component means when the +data have one sign), and periods scoring below the whole series are candidates, +sorted by score. The linear trend is removed first (`detrend=True`), the scan +covers periods up to `min(n / 2, n / 5)` (or `max_period`), `modulo` adds +multiples as in R, and the reported period is the fundamental one: the smallest +plausible divisor of the strongest period within two paired standard errors of +it. Inputs shorter than five observations return R's sentinel period `0`. The +scores follow R's arithmetic and agree with R to the last bit. `p_value=True` +adds R's permutation p-value of each period (`NaN` for period 1): its dispersion +ratio against the best ratio of each of `nperm` shuffles of the detrended series; +`periods` then lists the periods with p-value at most 0.05 (else 1). With +`random_seed = s` the shuffles are R's after `set.seed(s)`. ## ARMA diff --git a/src/nns/_rrng.py b/src/nns/_rrng.py index a925979e..46675c5c 100644 --- a/src/nns/_rrng.py +++ b/src/nns/_rrng.py @@ -11,10 +11,13 @@ from __future__ import annotations import math +from typing import TypeVar import numpy as np from numpy.typing import NDArray +_ScalarT = TypeVar("_ScalarT", bound=np.generic) + _N = 624 _MATRIX_A = 0x9908B0DF _UPPER = 0x80000000 @@ -203,12 +206,11 @@ def sample_int_replace(self, n: int, size: int) -> NDArray[np.int64]: def sample( self, - values: NDArray[np.int64], + values: NDArray[_ScalarT], size: int | None = None, replace: bool = False, - ) -> NDArray[np.int64]: + ) -> NDArray[_ScalarT]: """R's ``sample(x, size, replace)`` for an explicit vector ``x``.""" - values = np.asarray(values) idx = self.sample_int(values.size, size, replace) return values[idx - 1] diff --git a/src/nns/_seas_r14.py b/src/nns/_seas_r14.py index 9d4d307f..8ad300b2 100644 --- a/src/nns/_seas_r14.py +++ b/src/nns/_seas_r14.py @@ -15,6 +15,7 @@ from nns._helpers import _fast_lm from nns._rmean import r_cumsum, r_mean, r_sum +from nns._rrng import RRNG F = NDArray[np.float64] IntArray = NDArray[np.int64] @@ -158,12 +159,18 @@ def r14_seasonality( detrend: bool = True, max_period: int | None = None, dispersion: str = "auto", + p_value: bool = False, + nperm: int = 100, + rng: RRNG | None = None, ) -> dict[str, object]: - """R14 NNS.seas deterministic public result, except permutation p-values.""" + """R14 NNS.seas public result, with R's permutation p-values on request.""" x = _as_float_series(values, dispersion, detrend) n = int(x.size) if max_period is not None and (max_period < 1 or not np.isfinite(max_period)): raise ValueError("max.period must be NULL or a single number >= 1.") + if p_value and (not np.isfinite(nperm) or nperm < 1): + raise ValueError("nperm must be a single number >= 1.") + mp = None if max_period is None else int(max_period) def result(periods: IntArray, scores: F, cov: float) -> dict[str, object]: return { @@ -180,6 +187,66 @@ def result(periods: IntArray, scores: F, cov: float) -> dict[str, object]: return result(np.array([0], dtype=np.int64), np.array([0.0]), 0.0) use_cv = bool(np.min(x) > 0.0 or np.max(x) < 0.0) + periods, scores, base = _seas_scan(x, modulo, mod_only, mp) + periods, scores = _fundamental(x, periods, scores, use_cv) + out = result(periods, scores, base) + if p_value: + if rng is None: + raise ValueError("p_value = True needs a random stream.") + _add_permutation_p_values(out, x, int(nperm), mp, rng) + return out + + +def _add_permutation_p_values( + out: dict[str, object], x: F, nperm: int, max_period: int | None, rng: RRNG +) -> None: + """R NNS.seas(p.value = TRUE): each period's dispersion ratio against the best + ratio of each of ``nperm`` shuffled copies (``sample(x)``) of the detrended + series, scanned by ``NNS_seas_cpp`` with the observed table's out-of-scan + periods added.""" + table = out["all.periods"] + assert isinstance(table, dict) + periods = np.asarray(table["Period"], dtype=np.int64) + n = int(x.size) + scan_max = min(n // 2, n // 5) + if max_period is not None: + scan_max = max(1, min(scan_max, max_period)) + extra = periods[periods > scan_max] + null_best = np.empty(nperm, dtype=np.float64) + for b in range(nperm): + shuffled = rng.sample(x) + if extra.size: + p_null, s_null, base = _seas_scan(shuffled, extra, False, max_period) + else: + p_null, s_null, base = _seas_scan(shuffled, None, True, max_period) + with np.errstate(divide="ignore", invalid="ignore"): + null_best[b] = 1.0 if p_null[0] <= 1 else float(s_null[0] / base) + with np.errstate(divide="ignore", invalid="ignore"): + observed = np.asarray(table["Coefficient.of.Variation"], dtype=np.float64) / np.asarray( + table["Variable.Coefficient.of.Variation"], dtype=np.float64 + ) + p_values = np.array( + [(1.0 + float(np.count_nonzero(null_best <= ratio))) / (1.0 + nperm) for ratio in observed] + ) + p_values[periods <= 1] = np.nan + table["p.value"] = p_values + significant = periods[~np.isnan(p_values) & (p_values <= 0.05)] + chosen = significant.astype(np.int64) if significant.size else np.array([1], dtype=np.int64) + out["periods"] = chosen + out["best.period"] = int(chosen[0]) + + +def _seas_scan( + x: F, + modulo: int | Sequence[int] | IntArray | None, + mod_only: bool, + max_period: int | None, +) -> tuple[IntArray, F, float]: + """R's C++ ``NNS_seas_cpp``: candidate periods, their scores and the baseline.""" + n = int(x.size) + if n < 5: + return np.array([0], dtype=np.int64), np.array([0.0]), 0.0 + use_cv = bool(np.min(x) > 0.0 or np.max(x) < 0.0) max_lag = min(n // 2, n // 5) if max_period is not None: max_lag = max(1, min(max_lag, int(max_period))) @@ -223,5 +290,4 @@ def result(periods: IntArray, scores: F, cov: float) -> dict[str, object]: pairs = sorted(current.items(), key=lambda z: z[1]) # stable, R's order periods = np.asarray([lag for lag, _ in pairs], dtype=np.int64) scores = np.asarray([score for _, score in pairs], dtype=np.float64) - periods, scores = _fundamental(x, periods, scores, use_cv) - return result(periods, scores, base) + return periods, scores, base diff --git a/src/nns/causation.py b/src/nns/causation.py index 32f281a6..474fc67b 100644 --- a/src/nns/causation.py +++ b/src/nns/causation.py @@ -15,17 +15,146 @@ CausationResult = dict[str, float] +_PERMUTE_CHOICES = ("auto", "y", "x", "both", "shift") + + def nns_causation( - x: NDArray[np.float64], - y: NDArray[np.float64], + x: Any, + y: NDArray[np.float64] | None = None, tau: int | str = 0, -) -> CausationResult: + *, + p_value: bool = False, + nperm: int = 100, + permute: str = "auto", + seed: int | None = None, + conf_int: float = 0.95, +) -> Any: """R 14.0 NNS.caus: two complementary directional association indices. The index measures lagged association, not causal identification. The return fields are C(x--->y) and C(y--->x), sum to one, and have neutral value 0.5. R 13.3's three-component log ratio is obsolete. + With ``y=None`` and a matrix-like ``x`` this is :func:`causal_matrix`, as in R. + + ``p_value=True`` adds R's permutation test of the signed index + ``C(x--->y) - C(y--->x)`` and returns ``{"causation": ..., "p.value": {...}}`` + with R's fields (``two.sided``, ``x_causes_y``, ``y_causes_x``, + ``null_distribution``, ``observed_signed``, ``permute``, ``nperm``, + ``lower_CI``, ``upper_CI``). ``permute="auto"`` circularly shifts ``y`` for a + positive lag (the series keep their own dynamics) and shuffles it otherwise; + ``"y"``, ``"x"``, ``"both"`` and ``"shift"`` choose explicitly. Every + permutation is scored at the lag resolved on the observed series. With + ``seed = s`` the draws are R's after ``set.seed(s)``. """ + permute = _match_permute(permute) + if y is None: + return causal_matrix( + x, tau=tau, p_value=p_value, nperm=nperm, conf_int=conf_int, seed=seed, + permute=permute, + ) + cp = _caus_core(x, y, tau) + if not p_value: + return cp + return _caus_p_value(cp, x, y, tau, int(nperm), permute, seed, conf_int) + + +def _match_permute(permute: str) -> str: + """R's ``match.arg(permute)``: an exact choice or a unique prefix of one.""" + if permute in _PERMUTE_CHOICES: + return permute + matches = [choice for choice in _PERMUTE_CHOICES if choice.startswith(str(permute))] + if len(matches) != 1: + choices = ", ".join(f"'{choice}'" for choice in _PERMUTE_CHOICES) + raise ValueError(f"'arg' should be one of {choices}") + return matches[0] + + +def _caus_p_value( + cp: CausationResult, + x: Any, + y: Any, + tau: int | str, + nperm: int, + permute: str, + seed: int | None, + conf_int: float, +) -> dict[str, Any]: + """The permutation null of R ``NNS.caus(p.value = TRUE)``.""" + from nns._rrng import r_rng + from nns.var import lpm_var, upm_var + + rng = r_rng(seed) + x_values, y_values = _as_pair(x, y) + observed = float(cp["C(x--->y)"] - cp["C(y--->x)"]) + # One numeric lag for the observed statistic and every permutation (#96). + if isinstance(tau, str) and tau == "ts": + lag = _ts_tau_lag(x_values, y_values) + elif isinstance(tau, str) and tau == "cs": + lag = 0 + else: + lag = int(tau) + if permute == "auto": + permute = "shift" if lag > 0 else "y" + offsets: list[int] = [] + if permute == "shift": + offsets = _shift_draw(y_values.size, lag, nperm, rng) + nperm = len(offsets) + null = np.empty(nperm, dtype=np.float64) + for b in range(nperm): + x_perm, y_perm = x_values, y_values + if permute == "y": + y_perm = rng.sample(y_values, y_values.size) + if permute == "x": + x_perm = rng.sample(x_values, x_values.size) + if permute == "both": + x_perm = rng.sample(x_values, x_values.size) + y_perm = rng.sample(y_values, y_values.size) + if permute == "shift": + y_perm = _shift(y_values, offsets[b]) + cp_perm = _caus_core(x_perm, y_perm, lag) + null[b] = cp_perm["C(x--->y)"] - cp_perm["C(y--->x)"] + alpha = (1.0 - conf_int) / 2.0 + lower = float(lpm_var(alpha, 0.0, null)) + upper = float(upm_var(alpha, 0.0, null)) + return { + "causation": cp, + "p.value": { + "two.sided": (1 + int(np.count_nonzero(np.abs(null) >= abs(observed)))) / (1 + nperm), + "x_causes_y": (1 + int(np.count_nonzero(null >= observed))) / (1 + nperm), + "y_causes_x": (1 + int(np.count_nonzero(null <= observed))) / (1 + nperm), + "null_distribution": null, + "observed_signed": observed, + "permute": permute, + "nperm": nperm, + "lower_CI": 0.0 if math.isnan(lower) else lower, + "upper_CI": 0.0 if math.isnan(upper) else upper, + }, + } + + +def _shift_offsets(n: int, tau: int) -> list[int]: + """R ``.nns_caus_shift_offsets``: shifts at least max(tau + 1, n / 10) from alignment.""" + m = max(int(tau) + 1, math.ceil(n / 10)) + if n - 2 * m >= 0: + return list(range(m, n - m + 1)) + return list(range(1, n)) + + +def _shift_draw(n: int, tau: int, nperm: int, rng: Any) -> list[int]: + """R ``.nns_caus_shift_draw``: distinct offsets, ``sample.int``-drawn when too many.""" + offsets = _shift_offsets(n, tau) + if len(offsets) <= nperm: + return offsets + return [offsets[int(i) - 1] for i in rng.sample_int(len(offsets), nperm)] + + +def _shift(values: NDArray[np.float64], k: int) -> NDArray[np.float64]: + """R ``.nns_caus_shift``: ``c(v[(k + 1):n], v[1:k])``.""" + return np.concatenate((values[k:], values[:k])) + + +def _caus_core(x: Any, y: Any, tau: int | str) -> CausationResult: + """R ``NNS.caus_core`` for a pair of variables.""" x_values, y_values = _as_pair(x, y) if _is_constant(x_values) or _is_constant(y_values): raise ValueError( @@ -36,8 +165,27 @@ def nns_causation( return {"C(x--->y)": float(xy), "C(y--->x)": float(yx)} -def causal_matrix(x: NDArray[np.float64], tau: int | str = 0) -> Any: - """R14 NNS.caus.matrix, with C(i->j) + C(j->i)=1 off diagonal.""" +def causal_matrix( + x: NDArray[np.float64], + tau: int | str = 0, + *, + p_value: bool = False, + nperm: int = 100, + conf_int: float = 0.95, + seed: int | None = None, + permute: str = "auto", +) -> Any: + """R14 NNS.caus.matrix, with C(i->j) + C(j->i)=1 off diagonal. + + ``p_value=True`` returns R's ``{"causality", "lower_CI", "upper_CI", + "p.value_matrix"}``: each index's permutation null at the pair's observed + lag, its partial-moment interval and the two-sided p-value of + ``C(i--->j) - C(j--->i)``. ``permute="auto"`` circularly shifts the second + column of each pair for time series (a positive lag) and shuffles every + column otherwise; ``"x"``, ``"y"`` and ``"shift"`` permute within each pair. + With ``seed = s`` the draws are R's after ``set.seed(s)``. + """ + permute = _match_permute(permute) labels = _column_labels(x) values = _as_matrix(x, "x") if not (isinstance(tau, str) and tau == "ts"): @@ -46,11 +194,100 @@ def causal_matrix(x: NDArray[np.float64], tau: int | str = 0) -> Any: causes = np.zeros((nvars, nvars), dtype=np.float64) for i in range(nvars - 1): for j in range(i + 1, nvars): - directions = nns_causation(values[:, i], values[:, j], tau=tau) + directions = _caus_core(values[:, i], values[:, j], tau) causes[i, j] = directions["C(x--->y)"] causes[j, i] = directions["C(y--->x)"] causes[~np.isfinite(causes)] = 0.0 - return _labelled_matrix(causes, labels) + if not p_value: + return _labelled_matrix(causes, labels) + return _causal_matrix_p_value( + values, causes, labels, tau, int(nperm), conf_int, seed, permute + ) + + +def _causal_matrix_p_value( + values: NDArray[np.float64], + causes: NDArray[np.float64], + labels: list[Any] | None, + tau: int | str, + nperm: int, + conf_int: float, + seed: int | None, + permute: str, +) -> dict[str, Any]: + """The permutation intervals and p-values of R ``NNS.caus.matrix(p.value = TRUE)``.""" + from nns._rrng import r_rng + from nns.var import lpm_var, upm_var + + rng = r_rng(seed) + nrow, nvars = values.shape + pairs = [(i, j) for i in range(nvars - 1) for j in range(i + 1, nvars)] # combn order + pair_tau = [] + for i, j in pairs: + if isinstance(tau, str) and tau == "ts": + pair_tau.append(_ts_tau_lag(values[:, i], values[:, j])) + elif isinstance(tau, str) and tau == "cs": + pair_tau.append(0) + else: + pair_tau.append(int(tau)) + if permute == "auto": + permute = "shift" if any(lag > 0 for lag in pair_tau) else "both" + + def null_row(xi: NDArray[np.float64], xj: NDArray[np.float64], lag: int) -> tuple[float, float]: + cp = _caus_core(xi, xj, lag) + return cp["C(x--->y)"], cp["C(x--->y)"] - cp["C(y--->x)"] + + null_rows: list[list[tuple[float, float]]] = [[] for _ in pairs] + if permute != "both": + # Permute within each pair: shifting every column independently would + # leave pairs whose two shifts nearly coincide still aligned. + for k, (i, j) in enumerate(pairs): + draws = ( + _shift_draw(nrow, pair_tau[k], nperm, rng) + if permute == "shift" + else list(range(1, nperm + 1)) + ) + for s in draws: + xi, xj = values[:, i], values[:, j] + if permute == "shift": + xj = _shift(values[:, j], s) + if permute == "x": + xi = rng.sample(values[:, i]) + if permute == "y": + xj = rng.sample(values[:, j]) + null_rows[k].append(null_row(xi, xj, pair_tau[k])) + else: + for _ in range(nperm): + shuffled = [rng.sample(values[:, j]) for j in range(nvars)] + for k, (i, j) in enumerate(pairs): + null_rows[k].append(null_row(shuffled[i], shuffled[j], pair_tau[k])) + + alpha = (1.0 - conf_int) / 2.0 + lower = np.zeros((nvars, nvars), dtype=np.float64) + upper = np.zeros((nvars, nvars), dtype=np.float64) + p_matrix = np.zeros((nvars, nvars), dtype=np.float64) + for k, (i, j) in enumerate(pairs): + rows = np.asarray(null_rows[k], dtype=np.float64).reshape(-1, 2) + null_ij = rows[:, 0] + lower[i, j] = float(lpm_var(alpha, 0.0, null_ij)) + upper[i, j] = float(upm_var(alpha, 0.0, null_ij)) + lower[j, i] = float(lpm_var(alpha, 0.0, 1.0 - null_ij)) + upper[j, i] = float(upm_var(alpha, 0.0, 1.0 - null_ij)) + observed = causes[i, j] - causes[j, i] + null_signed = rows[:, 1] + p = (1 + int(np.count_nonzero(np.abs(null_signed) >= abs(observed)))) / ( + 1 + null_signed.size + ) + p_matrix[i, j] = p_matrix[j, i] = p + for matrix in (lower, upper, p_matrix): + np.fill_diagonal(matrix, 0.0) + matrix[np.isnan(matrix)] = 0.0 + return { + "causality": _labelled_matrix(causes, labels), + "lower_CI": _labelled_matrix(lower, labels), + "upper_CI": _labelled_matrix(upper, labels), + "p.value_matrix": _labelled_matrix(p_matrix, labels), + } def _uni_caus(x: NDArray[np.float64], y: NDArray[np.float64], tau: int) -> float: diff --git a/src/nns/dependence.py b/src/nns/dependence.py index a26cecfb..895014b1 100644 --- a/src/nns/dependence.py +++ b/src/nns/dependence.py @@ -2,7 +2,7 @@ import math from collections import defaultdict -from typing import Any, overload +from typing import Any, Literal, overload import numpy as np from numpy.typing import NDArray @@ -16,14 +16,31 @@ def nns_dep( x: NDArray[np.float64], y: NDArray[np.float64], asym: bool = False, + p_value: Literal[False] = False, + *, + random_seed: int | None = None, ) -> dict[str, float]: ... +@overload +def nns_dep( + x: NDArray[np.float64], + y: NDArray[np.float64], + asym: bool, + p_value: Literal[True], + *, + random_seed: int | None = None, +) -> dict[str, Any]: ... + + @overload def nns_dep( x: Any, y: None = None, asym: bool = False, + p_value: bool = False, + *, + random_seed: int | None = None, ) -> dict[str, Any]: ... @@ -31,6 +48,9 @@ def nns_dep( x: Any, y: NDArray[np.float64] | None = None, asym: bool = False, + p_value: bool = False, + *, + random_seed: int | None = None, ) -> dict[str, float] | dict[str, Any]: """Return NNS nonlinear correlation and dependence (R ``NNS.dep``). @@ -48,8 +68,20 @@ def nns_dep( ``[i, j]`` is ``nns_dep(X[:, i], X[:, j], asym=True)``, i.e. row variable -> column variable. A DataFrame input returns labelled DataFrames. - Inputs must be finite. R's ``p.value`` option is not implemented. + Inputs must be finite. + + ``p_value=True`` (bivariate form only) adds R's permutation test: the + measures of ``x`` against 100 shuffles of ``y`` give ``"Correlation p.value"`` + (two-sided), ``"Dependence p.value"`` (upper tail, dependence being a + magnitude) and the 95% partial-moment intervals of the shuffled values + (``"Correlation 95% CIs"``, ``"Dependence 95% CIs"``). With + ``random_seed = s`` the shuffles are R's after ``set.seed(s)``. """ + if p_value: + if y is None: + raise ValueError("p.value = TRUE requires both x and y.") + x_values, y_values = _as_pair(x, y) + return _dep_p_value(x_values, y_values, asym, random_seed) if y is None: return _dep_matrix(x, asym) x_values, y_values = _as_pair(x, y) @@ -70,6 +102,41 @@ def nns_dep( return {"Correlation": correlation, "Dependence": dependence} +def _dep_p_value( + x: NDArray[np.float64], y: NDArray[np.float64], asym: bool, random_seed: int | None +) -> dict[str, Any]: + """R ``NNS.dep(x, y, p.value = TRUE)``: 100 permutations of ``y``. + + R draws ``replicate(100, sample.int(length(y)))`` and measures ``x`` against + ``y`` and every shuffle (and against ``x`` itself, which only its plot uses). + """ + from nns._rrng import r_rng + from nns.var import lpm_var, upm_var + + rng = r_rng(random_seed) + permutations = [rng.sample_int(y.size) - 1 for _ in range(100)] + observed = nns_dep(x, y, asym=asym) + null = [nns_dep(x, y[index], asym=asym) for index in permutations] + cors = np.array([result["Correlation"] for result in null], dtype=np.float64) + deps = np.array([result["Dependence"] for result in null], dtype=np.float64) + cor = float(observed["Correlation"]) + dep = float(observed["Dependence"]) + tails = min(1 + int(np.count_nonzero(cors <= cor)), 1 + int(np.count_nonzero(cors >= cor))) + return { + "Correlation": cor, + "Correlation p.value": min(1.0, 2 * tails / (1 + cors.size)), + "Correlation 95% CIs": np.array( + [float(lpm_var(0.025, 0.0, cors)), float(upm_var(0.025, 0.0, cors))] + ), + "Dependence": dep, + # Dependence is a magnitude: only the upper tail is evidence against independence. + "Dependence p.value": (1 + int(np.count_nonzero(deps >= dep))) / (1 + deps.size), + "Dependence 95% CIs": np.array( + [float(lpm_var(0.025, 0.0, deps)), float(upm_var(0.025, 0.0, deps))] + ), + } + + def _dep_matrix(x: Any, asym: bool) -> dict[str, Any]: """R NNS.dep.matrix: pairwise NNS.dep over the columns of ``x``.""" labels = _column_labels(x) diff --git a/src/nns/seasonality.py b/src/nns/seasonality.py index be8c333d..859ad231 100644 --- a/src/nns/seasonality.py +++ b/src/nns/seasonality.py @@ -2,7 +2,7 @@ import math from collections import OrderedDict -from typing import SupportsInt, TypedDict, cast +from typing import NotRequired, SupportsInt, TypedDict, cast import numpy as np from numpy.typing import NDArray @@ -13,6 +13,7 @@ "Period": NDArray[np.int64], "Coefficient.of.Variation": NDArray[np.float64], "Variable.Coefficient.of.Variation": NDArray[np.float64], + "p.value": NotRequired[NDArray[np.float64]], }, ) """Per-period seasonality diagnostics ordered by strength (R's ``$all.periods``).""" @@ -40,13 +41,24 @@ def nns_seas( *, detrend: bool = True, max_period: int | None = None, + p_value: bool = False, + nperm: int = 100, dispersion: str = "auto", + random_seed: int | None = None, ) -> SeasonalityResult: """R 14.0 Gini-dispersion seasonal candidates and fundamental period. The R14 estimator changed its statistics (Gini rather than SD), trend adjustment and scan cap; it must not reuse the earlier R13 cached statistic. + + ``p_value=True`` adds R's permutation p-value of each period to + ``all.periods`` (``"p.value"``, ``NaN`` for period 1): its dispersion ratio + against the best ratio of each of ``nperm`` shuffled copies of the + (detrended) series. ``periods`` then holds the periods with p-value at most + 0.05 (else 1) and ``best.period`` the first of them. With + ``random_seed = s`` the shuffles are R's after ``set.seed(s)``. """ + from nns._rrng import r_rng from nns._seas_r14 import r14_seasonality result = cast( @@ -54,6 +66,7 @@ def nns_seas( r14_seasonality( variable, modulo, mod_only, detrend=detrend, max_period=max_period, dispersion=dispersion, + p_value=p_value, nperm=nperm, rng=r_rng(random_seed) if p_value else None, ), ) if plot: diff --git a/tests/_r_cache/pval14.caus.json b/tests/_r_cache/pval14.caus.json new file mode 100644 index 00000000..ed329bfb --- /dev/null +++ b/tests/_r_cache/pval14.caus.json @@ -0,0 +1,152 @@ +{ + "entries": { + "0f5a0c1e51d62e2c6348dd7795e8b7560f1beae3212394894ff4597ea584966c": { + "C(x--->y)": 0.4556806758959702, + "C(y--->x)": 0.5443193241040298, + "lower_CI": -0.1305644878655732, + "nperm": 15.0, + "null_distribution": [ + -0.056147237458529675, + -0.11994296058260728, + -0.06420177969647817, + -0.09275260232814814, + 0.1309426477315876, + 0.1222182518891135, + -0.07251775160208324, + -0.14951685218458005, + 0.13708855993854052, + -0.025624238584667614, + 0.11509896495098948, + -0.1147960171862259, + -0.039336684919263176, + 0.024651088508458097, + -0.12244204601457026 + ], + "observed_signed": -0.08863864820805961, + "permute": "both", + "two.sided": 0.625, + "upper_CI": 0.13278642139367347, + "x_causes_y": 0.6875, + "y_causes_x": 0.375 + }, + "53c0b126fff14608103d033546f4b290ef849ab58fd4e3999610762c8e006e87": { + "C(x--->y)": 0.41909276334948903, + "C(y--->x)": 0.580907236650511, + "lower_CI": -0.3126986738305784, + "nperm": 15.0, + "null_distribution": [ + 0.04640931637262624, + -0.05450065220853434, + -0.3245472271198373, + -0.19343225152718513, + 0.10162928007066396, + 0.20414335703322872, + 0.0007485468818493324, + -0.06037547157373696, + 0.13425373306798094, + 0.16420674309028183, + -0.2991416995077199, + -0.27987220416365505, + -0.2993825268329149, + 0.09074374001739294, + -0.30762072242089605 + ], + "observed_signed": -0.161814473301022, + "permute": "shift", + "two.sided": 0.5625, + "upper_CI": 0.17618772727316584, + "x_causes_y": 0.625, + "y_causes_x": 0.4375 + }, + "5a9f6ee3f27e823228aa26e85cdf10fba5c296f286d7f32c8aa19b222491d9e9": { + "C(x--->y)": 0.50960981433026, + "C(y--->x)": 0.49039018566974, + "lower_CI": -0.1997031818359712, + "nperm": 15.0, + "null_distribution": [ + -0.1085468430093584, + 0.14332969337420298, + 0.13921631289616077, + -0.1826183603892378, + 0.06719949392779867, + -0.04391655259102811, + 0.04785088248292135, + -0.16345544374683169, + 0.24615662855374998, + -0.23956776521168233, + 0.20439136604158525, + -0.123846004935664, + 0.04799910788938555, + -0.04265486044922828, + 0.15045524532066024 + ], + "observed_signed": 0.01921962866051996, + "permute": "x", + "two.sided": 1.0, + "upper_CI": 0.21692094479523463, + "x_causes_y": 0.5625, + "y_causes_x": 0.5 + }, + "79f36cbca78686962d3d0d15dc3fde331cc63f309b2964e38376e8b6a6d0f890": { + "C(x--->y)": 0.4556806758959702, + "C(y--->x)": 0.5443193241040298, + "lower_CI": -0.19505351766138782, + "nperm": 15.0, + "null_distribution": [ + -0.27332078827975637, + 0.17906469877575804, + -0.1404466197093796, + -0.09587020718211603, + 0.17973655712863312, + -0.16151040168208697, + -0.07231721495557508, + -0.028238039942849957, + 0.05488466905201239, + 0.17790714055150492, + 0.11536957066292763, + -0.08013473817849587, + 0.007134733080652822, + -0.030018423122234017, + 0.07956986116032994 + ], + "observed_signed": -0.08863864820805961, + "permute": "y", + "two.sided": 0.5625, + "upper_CI": 0.17926625628162055, + "x_causes_y": 0.75, + "y_causes_x": 0.3125 + }, + "f10d127829275bfa56f0d0372780921035c26a20b5ea4494878090df20d659e0": { + "C(x--->y)": 0.50960981433026, + "C(y--->x)": 0.49039018566974, + "lower_CI": -0.4329513949827587, + "nperm": 15.0, + "null_distribution": [ + -0.05104683912354013, + -0.36562106728806937, + -0.24018223656101045, + -0.14539385391509985, + -0.4400550731495145, + -0.34990665981503305, + 0.057254249380597866, + -0.07519888341627445, + -0.42990696148272045, + 0.5063193171037979, + -0.2526912836845754, + 0.10391216050556928, + -0.39030510828574266, + 0.11391042827575948, + -0.07363838939154355 + ], + "observed_signed": 0.01921962866051996, + "permute": "shift", + "two.sided": 1.0, + "upper_CI": 0.23163309492417058, + "x_causes_y": 0.3125, + "y_causes_x": 0.75 + } + }, + "label": "pval14.caus", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/pval14.caus_matrix.json b/tests/_r_cache/pval14.caus_matrix.json new file mode 100644 index 00000000..002db650 --- /dev/null +++ b/tests/_r_cache/pval14.caus_matrix.json @@ -0,0 +1,217 @@ +{ + "entries": { + "247fe05918a1ce9957bb33dd5b5ce46992295c2702d5f5a368be88ebfb9f8475": { + "causality": [ + [ + 0.0, + 0.4671154814744099, + 0.4480331025787668 + ], + [ + 0.5328845185255902, + 0.0, + 0.5866110728471352 + ], + [ + 0.5519668974212333, + 0.4133889271528648, + 0.0 + ] + ], + "lower_CI": [ + [ + 0.0, + 0.3846995466962333, + 0.4168690939012192 + ], + [ + 0.3368452469355, + 0.0, + 0.3122988443445739 + ], + [ + 0.3195934633321621, + 0.28581341379655645, + 0.0 + ] + ], + "p.value_matrix": [ + [ + 0.0, + 1.0, + 0.6363636363636364 + ], + [ + 1.0, + 0.0, + 0.6363636363636364 + ], + [ + 0.6363636363636364, + 0.6363636363636364, + 0.0 + ] + ], + "upper_CI": [ + [ + 0.0, + 0.6631547530645, + 0.6804065366678379 + ], + [ + 0.6153004533037667, + 0.0, + 0.7141865862034436 + ], + [ + 0.5831309060987808, + 0.6877011556554261, + 0.0 + ] + ] + }, + "7e5f8644671feb262e7b3611d4a1132e38f4f7676dacb317be676a5c49f75ac4": { + "causality": [ + [ + 0.0, + 0.6946472260944035, + 0.40471324087710514 + ], + [ + 0.30535277390559645, + 0.0, + 0.5599063062412637 + ], + [ + 0.5952867591228949, + 0.4400936937587363, + 0.0 + ] + ], + "lower_CI": [ + [ + 0.0, + 0.44884034540323703, + 0.3673688958004368 + ], + [ + 0.3416923073638722, + 0.0, + 0.4126830978368342 + ], + [ + 0.42997013828534747, + 0.4160503751422452, + 0.0 + ] + ], + "p.value_matrix": [ + [ + 0.0, + 0.09090909090909091, + 0.18181818181818182 + ], + [ + 0.09090909090909091, + 0.0, + 0.45454545454545453 + ], + [ + 0.18181818181818182, + 0.45454545454545453, + 0.0 + ] + ], + "upper_CI": [ + [ + 0.0, + 0.6583076926361279, + 0.5700298617146525 + ], + [ + 0.551159654596763, + 0.0, + 0.5839496248577549 + ], + [ + 0.6326311041995633, + 0.5873169021631659, + 0.0 + ] + ] + }, + "d5e170b4983a00272438c62a1e4399a9e6861aaed67ef35ee7a227eabfc361d9": { + "causality": [ + [ + 0.0, + 0.6946472260944035, + 0.40471324087710514 + ], + [ + 0.30535277390559645, + 0.0, + 0.5599063062412637 + ], + [ + 0.5952867591228949, + 0.4400936937587363, + 0.0 + ] + ], + "lower_CI": [ + [ + 0.0, + 0.3185291151702421, + 0.3985774193243504 + ], + [ + 0.36862101332868064, + 0.0, + 0.32031604674555225 + ], + [ + 0.3557508189255373, + 0.36616845181677127, + 0.0 + ] + ], + "p.value_matrix": [ + [ + 0.0, + 0.18181818181818182, + 0.2727272727272727 + ], + [ + 0.18181818181818182, + 0.0, + 0.5454545454545454 + ], + [ + 0.2727272727272727, + 0.5454545454545454, + 0.0 + ] + ], + "upper_CI": [ + [ + 0.0, + 0.6313789866713194, + 0.6442491810744627 + ], + [ + 0.6814708848297579, + 0.0, + 0.6338315481832287 + ], + [ + 0.6014225806756496, + 0.6796839532544477, + 0.0 + ] + ] + } + }, + "label": "pval14.caus_matrix", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/pval14.dep.json b/tests/_r_cache/pval14.dep.json new file mode 100644 index 00000000..af529acf --- /dev/null +++ b/tests/_r_cache/pval14.dep.json @@ -0,0 +1,35 @@ +{ + "entries": { + "08b52fff3d21fa02e860aa41a3866cb921376b10a759d88f2e11a39af725c23b": { + "Correlation": 0.06107720506685688, + "Correlation 95% CIs": [ + -0.2680920961911537, + 0.3175893912308998 + ], + "Correlation p.value": 0.7128712871287128, + "Dependence": 0.817949189727433, + "Dependence 95% CIs": [ + 0.06164063800479486, + 0.37266103070526085 + ], + "Dependence p.value": 0.009900990099009901 + }, + "1f43da94426161366df7168174e275ff33fa97b1223961a318d5cf2462b3cd25": { + "Correlation": 0.06107720506685688, + "Correlation 95% CIs": [ + -0.22875230175986283, + 0.25788166805041746 + ], + "Correlation p.value": 0.5148514851485149, + "Dependence": 0.817949189727433, + "Dependence 95% CIs": [ + 0.0, + 0.34582140974694453 + ], + "Dependence p.value": 0.009900990099009901 + } + }, + "label": "pval14.dep", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/pval14.seas.json b/tests/_r_cache/pval14.seas.json new file mode 100644 index 00000000..79eb4c5e --- /dev/null +++ b/tests/_r_cache/pval14.seas.json @@ -0,0 +1,49 @@ +{ + "entries": { + "0175588a3dcdbcda830f6dbbf3db1376d447c7ee3c0db5ba7aaedddb1c3941ce": { + "best": 12.0, + "p": [ + 12.0, + 13.0, + 11.0, + 6.0 + ], + "periods": [ + 12.0, + 13.0 + ], + "pv": [ + 0.038461538461538464, + 0.038461538461538464, + 0.15384615384615385, + 0.9615384615384616 + ] + }, + "c45cd8ffc081ceb505fdcb7344f69a271c11f6e6562f580e1affb2e418d32545": { + "best": 12.0, + "p": [ + 12.0, + 13.0, + 11.0, + 6.0, + 1.0, + 18.0 + ], + "periods": [ + 12.0, + 13.0 + ], + "pv": [ + 0.038461538461538464, + 0.038461538461538464, + 0.15384615384615385, + 0.9615384615384616, + NaN, + 1.0 + ] + } + }, + "label": "pval14.seas", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/parity/test_pvalues_r14.py b/tests/parity/test_pvalues_r14.py new file mode 100644 index 00000000..c323a832 --- /dev/null +++ b/tests/parity/test_pvalues_r14.py @@ -0,0 +1,135 @@ +"""R 14.0 parity for the permutation p-values of NNS.seas, NNS.dep and NNS.caus. + +R draws every permutation with ``sample()``/``sample.int()``, which the port +reproduces from R's Mersenne-Twister stream: with ``random_seed = s`` (``seed`` +for ``nns_causation``, as R's ``NNS.caus(seed = )``) the nulls, p-values and +intervals are R's after ``set.seed(s)``, bit for bit. +""" + +from __future__ import annotations + +import warnings +from typing import Any + +import numpy as np +import pytest +from _r import nns_frame_call, r_column + +import nns +from nns.causation import causal_matrix + +pytestmark = pytest.mark.parity + + +def _num(values: Any) -> dict[str, Any]: + return r_column([float(v) for v in np.asarray(values, dtype=np.float64).ravel()], "numeric") + + +def _seasonal(seed: int, n: int) -> np.ndarray: + rng = np.random.default_rng(seed) + t = np.arange(n, dtype=np.float64) + return 10.0 + 2.0 * np.sin(2.0 * np.pi * t / 12.0) + rng.normal(size=n) * 0.6 + + +@pytest.mark.parametrize(("modulo", "r_modulo"), [(None, "NULL"), (6, "6")]) +def test_nns_seas_p_values_match_r_under_set_seed(modulo: int | None, r_modulo: str) -> None: + x = _seasonal(301, 72) + expected = nns_frame_call( + "pval14.seas", + f"set.seed(5); r <- NNS.seas(x, modulo = {r_modulo}, mod.only = FALSE, plot = FALSE, " + "p.value = TRUE, nperm = 25); list(p = r$all.periods$Period, " + "pv = r$all.periods$p.value, periods = r$periods, best = r$best.period)", + {"x": _num(x)}, + exact=True, + ) + actual = nns.nns_seas(x, modulo=modulo, mod_only=False, p_value=True, nperm=25, random_seed=5) + table = actual["all.periods"] + np.testing.assert_array_equal(table["Period"], np.asarray(expected["p"], dtype=np.int64)) + np.testing.assert_array_equal(table["p.value"], np.asarray(expected["pv"], dtype=np.float64)) + np.testing.assert_array_equal(actual["periods"], np.atleast_1d(expected["periods"])) + assert actual["best.period"] == int(np.asarray(expected["best"])) + + +@pytest.mark.parametrize("asym", [False, True]) +def test_nns_dep_p_values_match_r_under_set_seed(asym: bool) -> None: + rng = np.random.default_rng(302) + x = rng.normal(size=60) + y = x * x + rng.normal(size=60) * 0.5 + flag = "TRUE" if asym else "FALSE" + expected = nns_frame_call( + "pval14.dep", + f"set.seed(21); lapply(NNS.dep(x, y, asym = {flag}, p.value = TRUE), unname)", + {"x": _num(x), "y": _num(y)}, + exact=True, + ) + actual = nns.nns_dep(x, y, asym=asym, p_value=True, random_seed=21) + assert set(actual) == set(expected) + for key, value in expected.items(): + np.testing.assert_array_equal(np.atleast_1d(actual[key]), np.atleast_1d(value), key) + + +def test_nns_dep_p_value_needs_both_variables() -> None: + with pytest.raises(ValueError, match=r"p\.value = TRUE requires both x and y"): + nns.nns_dep(np.ones((5, 2)), p_value=True) + + +@pytest.mark.parametrize( + ("tau", "r_tau", "permute"), + [(0, "0", "auto"), (0, "0", "both"), (2, "2", "auto"), (2, "2", "x"), ("ts", '"ts"', "shift")], +) +def test_nns_caus_p_values_match_r(tau: int | str, r_tau: str, permute: str) -> None: + # n = 150 so that the period of 12 is within sqrt(n), as tau = "ts" requires. + x = _seasonal(303, 150) + rng = np.random.default_rng(304) + y = np.r_[x[3:], x[:3]] + rng.normal(size=150) * 0.3 + expected = nns_frame_call( + "pval14.caus", + f"r <- NNS.caus(x, y, tau = {r_tau}, p.value = TRUE, nperm = 15, permute = '{permute}', " + "seed = 99, conf.int = 0.9); lapply(c(as.list(r$causation), r$p.value), " + "function(v) if (is.numeric(v)) unname(v) else v)", + {"x": _num(x), "y": _num(y)}, + exact=True, + ) + actual = nns.nns_causation( + x, y, tau=tau, p_value=True, nperm=15, permute=permute, seed=99, conf_int=0.9 + ) + flat = {**actual["causation"], **actual["p.value"]} + assert set(flat) == set(expected) + for key, value in expected.items(): + if isinstance(value, str): + assert flat[key] == value + else: + np.testing.assert_array_equal(np.atleast_1d(flat[key]), np.atleast_1d(value), key) + + +@pytest.mark.parametrize(("tau", "permute"), [(0, "auto"), (1, "auto"), (0, "y")]) +def test_nns_caus_matrix_p_values_match_r(tau: int, permute: str) -> None: + rng = np.random.default_rng(305) + base = rng.normal(size=40) + m = np.column_stack([base, 0.5 * base + rng.normal(size=40), rng.normal(size=40)]) + expected = nns_frame_call( + "pval14.caus_matrix", + f"lapply(NNS.caus(matrix(v, ncol = 3), tau = {tau}, p.value = TRUE, nperm = 10, " + f"permute = '{permute}', seed = 7), unname)", + {"v": _num(m.ravel(order="F"))}, + exact=True, + ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + actual = causal_matrix(m, tau=tau, p_value=True, nperm=10, permute=permute, seed=7) + assert nns.nns_causation(m, tau=tau, p_value=True, nperm=10, permute=permute, seed=7)[ + "p.value_matrix" + ].tolist() == actual["p.value_matrix"].tolist() + assert set(actual) == set(expected) + for key, rows in expected.items(): + np.testing.assert_array_equal(actual[key], np.asarray(rows, dtype=np.float64), key) + + +def test_permute_accepts_r_partial_matching() -> None: + x = _seasonal(306, 30) + y = np.roll(x, 2) + full = nns.nns_causation(x, y, tau=1, p_value=True, nperm=5, permute="shift", seed=1) + partial = nns.nns_causation(x, y, tau=1, p_value=True, nperm=5, permute="sh", seed=1) + assert full["p.value"]["permute"] == partial["p.value"]["permute"] == "shift" + with pytest.raises(ValueError, match="'arg' should be one of"): + nns.nns_causation(x, y, p_value=True, permute="bogus") From bfbc4d7769d0aee68e2b56cb614f3dee448d9dc6 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 00:46:16 +0000 Subject: [PATCH 08/16] Make multivariate regression and classification match R to the last bit - R's NNS_mreg_predict_v2_cpp weights the k nearest regression points with R's own nmath dt and dnorm4. The NumPy fallback now uses ports of those densities, libm exp (NumPy's SIMD exp differs from libm for a few percent of inputs) and R's sequential sums; the native kernel gets an R14 overlay of NNS-core's mreg_predict.cpp that calls the same nmath ports instead of the vendor's lgamma-based Student-t, registered with its hashes in sync/nns_source.json. Native and fallback are now identical. - R's classifier uses the R-level ensemble weights instead (long-double sum/var, dexp(rate = 1/k), ranks^-2, mean() for tied weights) and rowSums distances, and normalises rows with rowSums; the port follows it. - nns_m_reg gains R's categorical argument (internal NNS.M.reg default FALSE); nns_reg still sets it when every predictor is categorical. Live sweep against R 14.0 at exact equality: every multivariate and classification case identical on both paths; univariate smooth = TRUE fits remain within 1e-12 (R's Fortran smooth.spline is reimplemented, not transcribed). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt --- CMakeLists.txt | 8 +- docs/conventions.md | 8 + src/nns/_class_r14.py | 116 +++- src/nns/_r14_mreg_predict.cpp | 511 ++++++++++++++++++ src/nns/_reg_engine.py | 107 ++-- src/nns/_rnmath.hpp | 50 +- src/nns/_rnmath.py | 116 ++++ src/nns/multivariate_regression.py | 7 +- sync/nns_source.json | 16 +- tests/_r_cache/mreg14.bits.json | 149 +++++ tests/_r_cache/mreg14.categorical.json | 21 + tests/_r_cache/mreg14.class_bits.json | 629 ++++++++++++++++++++++ tests/invariants/test_diff.py | 9 +- tests/invariants/test_mreg_fast_paths.py | 72 ++- tests/invariants/test_r14_shared_paths.py | 2 +- tests/parity/test_mreg_bits_r14.py | 110 ++++ 16 files changed, 1840 insertions(+), 91 deletions(-) create mode 100644 src/nns/_r14_mreg_predict.cpp create mode 100644 tests/_r_cache/mreg14.bits.json create mode 100644 tests/_r_cache/mreg14.categorical.json create mode 100644 tests/_r_cache/mreg14.class_bits.json create mode 100644 tests/parity/test_mreg_bits_r14.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a3c2c18..0d8b009d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,16 +13,18 @@ set(NNSCORE_BUILD_TESTS OFF CACHE BOOL "Build NNS-core tests" FORCE) set(NNSCORE_BUILD_PYTHON OFF CACHE BOOL "Build NNS-core Python bindings" FORCE) add_subdirectory(extern/NNS-core) -# Preserve the accepted NNS-core vendor tree; explicitly overlay the two -# R14 central-tendency kernels that its R13.3 snapshot does not yet supply. +# Preserve the accepted NNS-core vendor tree; explicitly overlay the R14 +# central-tendency kernels that its R13.3 snapshot does not yet supply, and the +# multivariate prediction kernel with R's own nmath densities. 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") + if(NOT source STREQUAL "src/central_tendencies.cpp" AND NOT source STREQUAL "src/mreg_predict.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") set_property(TARGET nnscore PROPERTY SOURCES "${NNS_R14_CORE_SOURCES}") # The Python extension is a shared object. The vendored NNS-core target builds diff --git a/docs/conventions.md b/docs/conventions.md index 06e5e4d2..d18d0970 100644 --- a/docs/conventions.md +++ b/docs/conventions.md @@ -303,6 +303,14 @@ Inputs follow R's data-frame rules (`.nns_reg_encode_predictors`): map. `dist=None` is the default for `nns_reg`, `nns_m_reg`, `nns_stack`, and `nns_boost` and selects the native blended NNS distance, with `dist="NNS"` as the explicit alias. Continuous predictors use range-normalized coordinate differences and sum `abs(z) + z**2` across non-zero-range columns; explicit `dist="L1"`, `dist="L2"`, and `dist="FACTOR"` keep their legacy behavior. +Multivariate predictions follow R's C++ kernel to the last bit, in the native +extension and the NumPy fallback alike: the ensemble weights use R's own nmath +`dt` and `dnorm` densities and libm `exp`, summed in R's order. Classification +probabilities follow the R-level weights R's classifier uses instead (long-double +`sum`/`var`, `rowSums` distances). `nns_m_reg(categorical=...)` is the argument +of R's internal `NNS.M.reg` (default `False`): exact joint cells, maximum order +and one neighbour; `nns_reg` sets it when every predictor is categorical, as R's +`NNS.reg` does. `smooth=True` follows installed R's ordinary piecewise fallback for univariate inputs with fewer than four observations and for univariate `order="max"`; R does not call `smooth.spline` there. Spline-eligible inputs use a private diff --git a/src/nns/_class_r14.py b/src/nns/_class_r14.py index 20d20f5c..c6f61d8f 100644 --- a/src/nns/_class_r14.py +++ b/src/nns/_class_r14.py @@ -7,6 +7,7 @@ from __future__ import annotations +import functools import math from typing import Any @@ -14,16 +15,28 @@ from numpy.typing import NDArray from nns._rblas import r_crossprod_vec -from nns._rmean import r_approx, r_col_means, r_tie_means +from nns._rmean import ( + r_approx, + r_col_means, + r_mean, + r_mean_logical, + r_sd, + r_sum, + r_tie_means, + r_var, +) +from nns._rnmath import dexp, dlnorm_log, dnorm, dt, libm_exp F = NDArray[np.float64] def _normalize(probabilities: F, priors: F) -> F: + """R ``.nns_class_normalize``: clamp to [0, 1] and divide each row by its + ``rowSums`` (long-double), else use the priors.""" p = np.asarray(probabilities, dtype=np.float64).copy() p[~np.isfinite(p) | (p < 0.0)] = 0.0 p[p > 1.0] = 1.0 - totals = np.sum(p, axis=1) + totals = np.asarray(np.cumsum(p.astype(np.longdouble), axis=1)[:, -1], dtype=np.float64) valid = totals > 0.0 if np.any(valid): p[valid] /= totals[valid, None] @@ -32,6 +45,87 @@ def _normalize(probabilities: F, priors: F) -> F: return p +def _r_normalize_weights(w: F) -> F: + """R ``.nns_mreg_normalize_weights``: R's ``sum()`` is long double.""" + w = np.where(np.isfinite(w) & (w >= 0.0), w, 0.0) + total = r_sum(w) + if total <= 0.0: + return np.full(w.size, 1.0 / w.size, dtype=np.float64) + return np.asarray(w / total, dtype=np.float64) + + +@functools.lru_cache(maxsize=256) +def _r_rank_weights(k: int) -> tuple[F, F, F, F]: + """R ``.nns_mreg_rank_weights(k)``, the rank terms of the classifier's weights.""" + ranks = np.arange(1, k + 1, dtype=np.float64) + rank_sd = r_sd(ranks) + uniform = np.full(k, 1.0 / k, dtype=np.float64) + scale = 1.0 / (1.0 / k) # dexp(ranks, rate = 1 / k) passes scale = 1 / rate + exponential = _r_normalize_weights(np.array([dexp(r, scale) for r in ranks])) + if math.isfinite(rank_sd) and rank_sd > 0: + lognormal = _r_normalize_weights( + np.abs(np.array([dlnorm_log(r, 0.0, rank_sd) for r in ranks])) + )[::-1].copy() + else: + lognormal = np.zeros(k, dtype=np.float64) + power = _r_normalize_weights(np.array([math.pow(r, -2.0) for r in ranks])) + for term in (uniform, exponential, lognormal, power): + term.flags.writeable = False + return uniform, exponential, lognormal, power + + +def _r_ensemble_weights(distances: F) -> F: + """R ``.nns_mreg_ensemble_weights`` (the R-level helper the classifier calls, + not the C++ regression kernel): R's densities, ``var()`` and ``sum()``.""" + k = distances.size + if k == 1: + return np.ones(1, dtype=np.float64) + uniform, exponential, lognormal, power = _r_rank_weights(k) + student = _r_normalize_weights(np.array([dt(d, float(k)) for d in distances])) + inverse = _r_normalize_weights(1.0 / np.maximum(distances, 1e-12)) + variance = r_var(distances) + sd = math.sqrt(variance) if variance >= 0.0 else math.nan + if math.isfinite(sd) and sd > 0: + normal = _r_normalize_weights(np.array([dnorm(d, 0.0, sd) for d in distances])) + else: + normal = np.zeros(k, dtype=np.float64) + if math.isfinite(variance) and variance > 0: + rbf = _r_normalize_weights(libm_exp(-distances / (2.0 * variance))) + else: + rbf = np.zeros(k, dtype=np.float64) + return _r_normalize_weights( + uniform + student + inverse + exponential + lognormal + power + normal + rbf + ) + + +def _r_distances(centres: F, query: F, dist: str, mins: F, maxs: F) -> F: + """R ``.nns_mreg_distance_vector`` for every query row: the per-column terms + in double, summed across columns by ``rowSums`` (long double). The C++ + regression kernel adds them in double, which differs from three columns on.""" + m, n = query.shape[0], centres.shape[0] + if dist == "FACTOR": + mismatches = np.zeros((m, n), dtype=np.float64) + for j in range(centres.shape[1]): + mismatches += query[:, j, None] != centres[None, :, j] + return np.asarray(mismatches / centres.shape[1], dtype=np.float64) + ranges = maxs - mins + active = np.flatnonzero(np.isfinite(ranges) & (ranges > 0)) + if active.size == 0: + return np.zeros((m, n), dtype=np.float64) + total = np.zeros((m, n), dtype=np.longdouble) + for column in active.tolist(): + z = (centres[None, :, column] - query[:, column, None]) / ranges[column] + if dist == "NNS": + term = np.abs(z) + z * z + elif dist == "L1": + term = np.abs(z) + else: + term = z * z + total += term + out = np.asarray(total, dtype=np.float64) + return np.sqrt(out) if dist == "L2" else out + + def _select(probabilities: F | None, values: F, order: NDArray[np.intp]) -> F | None: if probabilities is None: return None @@ -55,8 +149,6 @@ def _class_base( _build_points, _dependence, _find_interval, - _mreg_distances, - _mreg_ensemble_weights, ) n, p = design_x.shape @@ -120,7 +212,7 @@ def _class_base( elif order == "max" or categorical: k = 1 else: - k = max(1, math.floor((1.0 - float(np.mean(strength))) * math.sqrt(n))) + k = max(1, math.floor((1.0 - r_mean(strength)) * math.sqrt(n))) k = max(1, min(k, centres.shape[0])) def predict(query: F | None) -> F | None: @@ -144,12 +236,12 @@ def predict(query: F | None) -> F | None: columns.append(r_approx(kx, ky, q[:, 0], ky[0], ky[-1])) out = np.column_stack(columns) return _normalize(out, prior) - ds = _mreg_distances(centres, q, dist, mins, maxs) + ds = _r_distances(centres, q, dist, mins, maxs) out = np.empty((q.shape[0], shares.shape[1]), dtype=np.float64) for i, row in enumerate(ds): matches = np.flatnonzero(row == 0.0) if matches.size: - out[i] = np.mean(shares[matches], axis=0) + out[i] = r_col_means(shares[matches]) continue ordered = np.argsort(row, kind="stable") distance_sorted = row[ordered] @@ -159,11 +251,11 @@ def predict(query: F | None) -> F | None: if chosen.size == 1: out[i] = shares[chosen[0]] continue - w = _mreg_ensemble_weights(chosen_d[None, :])[0] + w = _r_ensemble_weights(chosen_d) for value in np.unique(chosen_d): same = chosen_d == value if np.count_nonzero(same) > 1: - w[same] = np.mean(w[same]) + w[same] = r_mean(w[same]) # R: as.numeric(crossprod(weights, shares[o[seq_len(m)], ])). out[i] = r_crossprod_vec(w, shares[chosen]) return _normalize(out, prior) @@ -208,7 +300,7 @@ def classify( else [f"{v:.15g}" for v in values] ) indicators = np.asarray(y[:, None] == values[None, :], dtype=np.float64) - priors = np.mean(indicators, axis=0) + priors = r_col_means(indicators) _, first = np.unique(y, return_index=True) first_values = y[np.sort(first)] tie_order = np.array([np.flatnonzero(values == z)[0] for z in first_values], dtype=np.intp) @@ -312,7 +404,9 @@ def classify( point_pred = _select(point_prob, values, tie_order) fitted_pred = _select(fit_prob, values, tie_order) - accuracy = None if point_only else float(np.mean(fitted_pred == y)) + accuracy = ( + None if point_only else r_mean_logical(np.asarray(fitted_pred == y, dtype=np.bool_)) + ) fitted: dict[str, Any] | None = None if not point_only and fitted_pred is not None: names = encoded.get("encoded_names") or [f"X{i + 1}" for i in range(design_x.shape[1])] diff --git a/src/nns/_r14_mreg_predict.cpp b/src/nns/_r14_mreg_predict.cpp new file mode 100644 index 00000000..9400a5e0 --- /dev/null +++ b/src/nns/_r14_mreg_predict.cpp @@ -0,0 +1,511 @@ +// src/nns/_r14_mreg_predict.cpp: R14 overlay of NNS-core src/mreg_predict.cpp +// +// Identical to the vendored kernel except for its density components: the +// Student-t and normal weights call R's own nmath dt and dnorm4 +// (src/nns/_rnmath.hpp) instead of the vendor's lgamma-based closed form, so +// the ensemble weights, and the predictions, match R's NNS_mreg_predict_v2_cpp +// bit for bit. +// +// Pure C++ port of R NNS 13.3 src/NNS_mreg_predict.cpp +// (NNS_mreg_predict_v2_cpp / NNS_mreg_predict_path_v2_cpp, i.e. the +// PredictPathWorker kernel). Decoupled from Rcpp and Rmath: +// - range-normalised NNS / L2 / L1 or Hamming (FACTOR) distances, +// - stable ascending order with index tie-break via partial selection, +// - k = 1 aggregates all exact nearest-distance ties (gravity / class mode), +// - k > 1 uses the eight-component ensemble weights, +// - exact weighted class mode (max total, tie -> smallest value), +// - the O(log n + k) 1-D fast path for duplicated coordinates. +// +// The R kernel evaluates its density components with Rmath (Rf_dt, Rf_dexp, +// Rf_dlnorm, Rf_dnorm4) inside RcppParallel workers. Here dexp, dlnorm and +// dnorm are the same closed forms Rmath uses, and dt follows Rmath's +// factorisation with lgamma for the constant. No R API is touched from worker +// threads, and results agree with R to floating-point rounding. +// +// SPDX-License-Identifier: GPL-3.0-only +#include +#include +#include +#include +#include +#include +#include +#include + +#include "_rnmath.hpp" +#include "nns/central_tendencies.hpp" +#include "nns/mreg.hpp" +#include "nns/parallel.hpp" + +namespace nns { + +namespace { + +constexpr double kNaN = std::numeric_limits::quiet_NaN(); +constexpr double kInf = std::numeric_limits::infinity(); +constexpr double kLnSqrt2Pi = 0.918938533204672741780329736406; // log(sqrt(2*pi)) + +// ---- densities (closed forms; see file comment) ----------------------------- + +// Rmath dexp(x, scale, give_log = 0). +inline double dexp_scale(double x, double scale) { + return (x < 0.0) ? 0.0 : std::exp(-x / scale) / scale; +} + +// Rmath dlnorm(x, meanlog = 0, sdlog, give_log = 1) for x > 0, sdlog > 0. +inline double dlnorm_log(double x, double sdlog) { + const double y = std::log(x) / sdlog; + return -(kLnSqrt2Pi + 0.5 * y * y + std::log(x * sdlog)); +} + +// ---- weight helpers ---------------------------------------------------------- + +inline void normalize_weights(std::vector& w) { + const std::size_t k = w.size(); + double s = 0.0; + for (double& v : w) { + if (!std::isfinite(v) || v < 0.0) v = 0.0; + s += v; + } + if (s <= 0.0) { + for (double& v : w) v = 1.0 / static_cast(k); + } else { + for (double& v : w) v /= s; + } +} + +inline double sample_sd(const std::vector& v) { + const std::size_t n = v.size(); + if (n < 2) return kNaN; + double mu = 0.0; + for (double x : v) mu += x; + mu /= static_cast(n); + double acc = 0.0; + for (double x : v) { + const double d = x - mu; + acc += d * d; + } + return std::sqrt(acc / static_cast(n - 1)); +} + +// Totals per distinct value, max total, tie -> smallest value. +inline double weighted_mode(const std::vector& vals, const std::vector& w) { + std::vector> items; + items.reserve(vals.size()); + for (std::size_t i = 0; i < vals.size(); ++i) { + if (std::isfinite(vals[i]) && std::isfinite(w[i]) && w[i] >= 0.0) + items.push_back(std::make_pair(vals[i], w[i])); + } + if (items.empty()) return kNaN; + std::sort(items.begin(), items.end()); + double best_val = items[0].first; + double best_tot = -1.0; + std::size_t i = 0; + while (i < items.size()) { + const double v = items[i].first; + double tot = 0.0; + while (i < items.size() && items[i].first == v) { + tot += items[i].second; + ++i; + } + if (tot > best_tot) { + best_tot = tot; + best_val = v; + } + } + return best_val; +} + +void rank_components(int k, std::vector& uniform, std::vector& exponential, + std::vector& lognormal, std::vector& power) { + uniform.assign(k, 1.0 / static_cast(k)); + exponential.resize(k); + lognormal.assign(k, 0.0); + power.resize(k); + const double kk = static_cast(k); + for (int i = 0; i < k; ++i) exponential[i] = dexp_scale(static_cast(i + 1), kk); + normalize_weights(exponential); + std::vector ranks(k); + for (int i = 0; i < k; ++i) ranks[i] = static_cast(i + 1); + const double rank_sd = sample_sd(ranks); + if (std::isfinite(rank_sd) && rank_sd > 0.0) { + for (int i = 0; i < k; ++i) lognormal[i] = std::fabs(dlnorm_log(ranks[i], rank_sd)); + normalize_weights(lognormal); + std::reverse(lognormal.begin(), lognormal.end()); + } + for (int i = 0; i < k; ++i) { + const double r = static_cast(i + 1); + power[i] = 1.0 / (r * r); + } + normalize_weights(power); +} + +// Rank weights for k in [kmin, kmax]; every k up to kmax is O(kmax^2) memory. +struct RankComponents { + std::vector> uniform, exponential, lognormal, power; + RankComponents(int kmin, int kmax) + : uniform(kmax + 1), exponential(kmax + 1), lognormal(kmax + 1), power(kmax + 1) { + for (int k = std::max(kmin, 2); k <= kmax; ++k) { + rank_components(k, uniform[k], exponential[k], lognormal[k], power[k]); + } + } +}; + +inline void normalize_prefix(std::vector& values, int k) { + double s = 0.0; + for (int i = 0; i < k; ++i) { + if (!std::isfinite(values[i]) || values[i] < 0.0) values[i] = 0.0; + s += values[i]; + } + if (s <= 0.0) { + const double u = 1.0 / static_cast(k); + for (int i = 0; i < k; ++i) values[i] = u; + } else { + for (int i = 0; i < k; ++i) values[i] /= s; + } +} + +inline double weighted_mode_prefix(const std::vector& yk, + const std::vector& weights, int k, + std::vector& cls, std::vector& totals) { + int ncls = 0; + for (int i = 0; i < k; ++i) { + const double v = yk[i], w = weights[i]; + if (!std::isfinite(v) || !std::isfinite(w) || w < 0.0) continue; + int pos = -1; + for (int c = 0; c < ncls; ++c) + if (cls[c] == v) { + pos = c; + break; + } + if (pos < 0) { + pos = ncls++; + cls[pos] = v; + totals[pos] = 0.0; + } + totals[pos] += w; + } + if (ncls == 0) return kNaN; + double best_val = cls[0], best_tot = totals[0]; + for (int c = 1; c < ncls; ++c) { + if (totals[c] > best_tot || (totals[c] == best_tot && cls[c] < best_val)) { + best_tot = totals[c]; + best_val = cls[c]; + } + } + return best_val; +} + +struct Scratch { + std::vector student, inverse, normal, rbf, total, cls, cls_totals; + explicit Scratch(int kmax) + : student(kmax), inverse(kmax), normal(kmax), rbf(kmax), total(kmax), cls(kmax), + cls_totals(kmax) {} +}; + +double predict_sorted_k(const std::vector& dk, const std::vector& yk, int k, + bool is_class, const RankComponents& rank, Scratch& s) { + const double kk = static_cast(k); + for (int i = 0; i < k; ++i) s.student[i] = nns_rmath::dt(dk[i], kk); + normalize_prefix(s.student, k); + for (int i = 0; i < k; ++i) s.inverse[i] = 1.0 / std::max(dk[i], 1e-12); + normalize_prefix(s.inverse, k); + + double mu = 0.0; + for (int i = 0; i < k; ++i) mu += dk[i]; + mu /= kk; + double acc = 0.0; + for (int i = 0; i < k; ++i) { + const double z = dk[i] - mu; + acc += z * z; + } + const double sd = (k > 1) ? std::sqrt(acc / static_cast(k - 1)) : kNaN; + const double var = std::isfinite(sd) ? sd * sd : kNaN; + if (std::isfinite(sd) && sd > 0.0) { + for (int i = 0; i < k; ++i) s.normal[i] = nns_rmath::dnorm(dk[i], 0.0, sd); + normalize_prefix(s.normal, k); + } else { + for (int i = 0; i < k; ++i) s.normal[i] = 0.0; + } + if (std::isfinite(var) && var > 0.0) { + for (int i = 0; i < k; ++i) s.rbf[i] = std::exp(-dk[i] / (2.0 * var)); + normalize_prefix(s.rbf, k); + } else { + for (int i = 0; i < k; ++i) s.rbf[i] = 0.0; + } + + for (int i = 0; i < k; ++i) { + s.total[i] = rank.uniform[k][i] + s.student[i] + s.inverse[i] + rank.exponential[k][i] + + rank.lognormal[k][i] + rank.power[k][i] + s.normal[i] + s.rbf[i]; + } + normalize_prefix(s.total, k); + + if (is_class) return weighted_mode_prefix(yk, s.total, k, s.cls, s.cls_totals); + double dot = 0.0; + for (int i = 0; i < k; ++i) dot += yk[i] * s.total[i]; + return dot; +} + +// ---- the prediction kernel (port of PredictPathWorker) ---------------------- + +class PredictKernel { + public: + PredictKernel(const double* rpm_x, std::size_t n, std::size_t p, const double* yhat, + const double* Xtest, std::size_t m, int kmax, int dist_code, + const double* mins, const double* maxs, bool is_class, + const RankComponents& rank, double* out, int only_k) + : rpm_x_(rpm_x), yhat_(yhat), Xtest_(Xtest), out_(out), n_(static_cast(n)), + p_(static_cast(p)), m_(m), kmax_(kmax), dist_code_(dist_code), + only_k_(only_k), is_class_(is_class), rank_(rank) { + for (int j = 0; j < p_; ++j) { + const double range = maxs[j] - mins[j]; + if (std::isfinite(range) && range > 0.0) { + active_.push_back(j); + col_range_.push_back(range); + } + } + // Effectively 1-D design: all active columns identical in rpm_x and Xtest + // (FACTOR / Hamming excluded). + if (dist_code_ != kMregDistFactor && !active_.empty()) { + axis_ = active_[0]; + bool identical = true; + for (std::size_t a = 1; a < active_.size() && identical; ++a) { + const int j = active_[a]; + for (int i = 0; i < n_; ++i) + if (rx(i, j) != rx(i, axis_)) { + identical = false; + break; + } + for (std::size_t i = 0; identical && i < m_; ++i) + if (xt(i, j) != xt(i, axis_)) { + identical = false; + break; + } + } + if (identical) { + is_1d_ = true; + std::vector coord(n_); + for (int i = 0; i < n_; ++i) coord[i] = rx(i, axis_); + so_.resize(n_); + for (int i = 0; i < n_; ++i) so_[i] = i; + std::sort(so_.begin(), so_.end(), [&coord](int a, int b) { + return coord[a] < coord[b] || (coord[a] == coord[b] && a < b); + }); + sc_.resize(n_); + for (int i = 0; i < n_; ++i) sc_[i] = coord[so_[i]]; + } + } + } + + void operator()(std::size_t begin, std::size_t end) const { + std::vector d(n_), dk(kmax_), yk(kmax_), tied, one_weights; + std::vector idx(n_), tk; + Scratch s(kmax_); + auto cmp = [&d](int a, int b) { return d[a] < d[b] || (d[a] == d[b] && a < b); }; + auto out = [&](std::size_t r, std::size_t c) -> double& { return out_[r + c * m_]; }; + for (std::size_t r = begin; r < end; ++r) { + if (is_1d_) { + const double t = xt(r, axis_); + if (only_k_ > 0) { + if (only_k_ == 1) { + out(r, 0) = min_ties_1d(r, t, tied, one_weights); + } else { + topk_1d(r, t, only_k_, tk, dk); + for (int i = 0; i < only_k_; ++i) yk[i] = yhat_[tk[i]]; + out(r, 0) = predict_sorted_k(dk, yk, only_k_, is_class_, rank_, s); + } + } else { + out(r, 0) = min_ties_1d(r, t, tied, one_weights); + if (kmax_ >= 2) { + topk_1d(r, t, kmax_, tk, dk); + for (int i = 0; i < kmax_; ++i) yk[i] = yhat_[tk[i]]; + for (int k = 2; k <= kmax_; ++k) + out(r, static_cast(k - 1)) = + predict_sorted_k(dk, yk, k, is_class_, rank_, s); + } + } + continue; + } + distances_for_row(r, d); + for (int i = 0; i < n_; ++i) idx[i] = i; + if (kmax_ < n_) { + std::nth_element(idx.begin(), idx.begin() + kmax_, idx.end(), cmp); + std::sort(idx.begin(), idx.begin() + kmax_, cmp); + } else { + std::sort(idx.begin(), idx.end(), cmp); + } + for (int i = 0; i < kmax_; ++i) { + dk[i] = d[idx[i]]; + yk[i] = yhat_[idx[i]]; + } + if (only_k_ > 0) { + out(r, 0) = (only_k_ == 1) ? min_ties(d, tied, one_weights) + : predict_sorted_k(dk, yk, only_k_, is_class_, rank_, s); + } else { + out(r, 0) = min_ties(d, tied, one_weights); + for (int k = 2; k <= kmax_; ++k) + out(r, static_cast(k - 1)) = + predict_sorted_k(dk, yk, k, is_class_, rank_, s); + } + } + } + + private: + double rx(int i, int j) const { return rpm_x_[static_cast(i) + static_cast(j) * n_]; } + double xt(std::size_t i, int j) const { return Xtest_[i + static_cast(j) * m_]; } + + // Range-normalised distance of training row i to query r. The 1-D path uses + // it too, so it selects neighbours and ties exactly as the generic path does. + double distance(int i, std::size_t r) const { + double acc = 0.0; + for (std::size_t a = 0; a < active_.size(); ++a) { + const int j = active_[a]; + // Divide as R's reference does; a precomputed 1/range can flip exact ties. + const double z = (rx(i, j) - xt(r, j)) / col_range_[a]; + if (dist_code_ == kMregDistL1) acc += std::fabs(z); + else if (dist_code_ == kMregDistNNS) acc += std::fabs(z) + z * z; + else acc += z * z; + } + return dist_code_ == kMregDistL2 ? std::sqrt(acc) : acc; + } + + // distance() is non-decreasing in |coord - t|, so walking outward in that + // order and extending over distance() ties at the boundary finds the k nearest. + void topk_1d(std::size_t row, double t, int k, std::vector& out_idx, + std::vector& out_d) const { + const int nn = static_cast(sc_.size()); + k = std::min(k, nn); + int r = static_cast(std::lower_bound(sc_.begin(), sc_.end(), t) - sc_.begin()); + int lo = r - 1; + std::vector> win; + while (static_cast(win.size()) < k && (lo >= 0 || r < nn)) { + const double dl = (lo >= 0) ? std::fabs(sc_[lo] - t) : kInf; + const double dr = (r < nn) ? std::fabs(sc_[r] - t) : kInf; + const int pos = (dl <= dr) ? lo-- : r++; + win.emplace_back(distance(so_[pos], row), so_[pos]); + } + double thr = 0.0; + for (const auto& w : win) thr = std::max(thr, w.first); + for (double dd; lo >= 0 && (dd = distance(so_[lo], row)) <= thr; --lo) + win.emplace_back(dd, so_[lo]); + for (double dd; r < nn && (dd = distance(so_[r], row)) <= thr; ++r) + win.emplace_back(dd, so_[r]); + std::sort(win.begin(), win.end()); // (distance, original index) + out_idx.clear(); + for (int i = 0; i < k; ++i) { + out_idx.push_back(win[i].second); + out_d[i] = win[i].first; + } + } + + double min_ties_1d(std::size_t row, double t, std::vector& tied, + std::vector& weights) const { + const int nn = static_cast(sc_.size()); + const int r = static_cast(std::lower_bound(sc_.begin(), sc_.end(), t) - sc_.begin()); + const int lo = r - 1; + const double dl = (lo >= 0) ? distance(so_[lo], row) : kInf; + const double dr = (r < nn) ? distance(so_[r], row) : kInf; + const double mn = std::min(dl, dr); + tied.clear(); + for (int li = lo; li >= 0 && distance(so_[li], row) == mn; --li) + if (std::isfinite(yhat_[so_[li]])) tied.push_back(yhat_[so_[li]]); + for (int ri = r; ri < nn && distance(so_[ri], row) == mn; ++ri) + if (std::isfinite(yhat_[so_[ri]])) tied.push_back(yhat_[so_[ri]]); + if (is_class_) { + weights.assign(tied.size(), 1.0); + return weighted_mode(tied, weights); + } + return gravity(tied.data(), tied.size(), false); + } + + void distances_for_row(std::size_t r, std::vector& d) const { + if (dist_code_ == kMregDistFactor) { + for (int i = 0; i < n_; ++i) { + double mismatches = 0.0; + for (int j = 0; j < p_; ++j) + if (rx(i, j) != xt(r, j)) mismatches += 1.0; + d[i] = mismatches / static_cast(p_); + } + } else if (active_.empty()) { + std::fill(d.begin(), d.end(), 0.0); + } else { + for (int i = 0; i < n_; ++i) d[i] = distance(i, r); + } + } + + double min_ties(const std::vector& d, std::vector& tied, + std::vector& weights) const { + double dmin = d[0]; + for (int i = 1; i < n_; ++i) + if (d[i] < dmin) dmin = d[i]; + tied.clear(); + for (int i = 0; i < n_; ++i) + if (d[i] == dmin && std::isfinite(yhat_[i])) tied.push_back(yhat_[i]); + if (is_class_) { + weights.assign(tied.size(), 1.0); + return weighted_mode(tied, weights); + } + return gravity(tied.data(), tied.size(), false); + } + + const double* rpm_x_; + const double* yhat_; + const double* Xtest_; + double* out_; + int n_, p_; + std::size_t m_; + int kmax_, dist_code_, only_k_; + bool is_class_; + const RankComponents& rank_; + std::vector active_; + std::vector col_range_; + bool is_1d_ = false; + int axis_ = -1; + std::vector sc_; + std::vector so_; +}; + +void validate(std::size_t n, std::size_t p, std::size_t m, int k, int dist_code, + const double* mins, const double* maxs, const char* kname) { + (void)m; + if (n == 0) throw std::invalid_argument("mreg_predict: rpm_x must have at least one row"); + if (p == 0) throw std::invalid_argument("mreg_predict: rpm_x must have at least one column"); + if (mins == nullptr || maxs == nullptr) + throw std::invalid_argument("mreg_predict: mins/maxs must have one value per column"); + if (k < 1) throw std::invalid_argument(std::string("mreg_predict: ") + kname + " must be >= 1"); + if (dist_code < 0 || dist_code > 3) + throw std::invalid_argument("mreg_predict: unknown NNS multivariate distance code"); +} + +} // namespace + +std::vector mreg_predict(const double* rpm_x, std::size_t n, std::size_t p, + const double* yhat, const double* Xtest, std::size_t m, + int k, int dist_code, const double* mins, const double* maxs, + bool is_class, int n_threads) { + validate(n, p, m, k, dist_code, mins, maxs, "k"); + if (static_cast(k) > n) k = static_cast(n); + RankComponents rank(k, k); + std::vector out(m); + PredictKernel kernel(rpm_x, n, p, yhat, Xtest, m, k, dist_code, mins, maxs, is_class, rank, + out.data(), /*only_k=*/k); + parallel_for(0, m, kernel, n_threads, n * p + static_cast(k) * 16U); + return out; +} + +std::vector mreg_predict_path(const double* rpm_x, std::size_t n, std::size_t p, + const double* yhat, const double* Xtest, std::size_t m, + int kmax, int dist_code, const double* mins, + const double* maxs, bool is_class, int n_threads) { + validate(n, p, m, kmax, dist_code, mins, maxs, "kmax"); + if (static_cast(kmax) > n) kmax = static_cast(n); + RankComponents rank(2, kmax); + std::vector out(m * static_cast(kmax)); + PredictKernel kernel(rpm_x, n, p, yhat, Xtest, m, kmax, dist_code, mins, maxs, is_class, + rank, out.data(), /*only_k=*/0); + parallel_for(0, m, kernel, n_threads, + n * p + static_cast(kmax) * static_cast(kmax) * 8U); + return out; +} + +} // namespace nns diff --git a/src/nns/_reg_engine.py b/src/nns/_reg_engine.py index fd5caaa9..e8d8b3a7 100644 --- a/src/nns/_reg_engine.py +++ b/src/nns/_reg_engine.py @@ -58,6 +58,11 @@ r_row_means, r_sum, ) +from nns._rnmath import dexp as _rnmath_dexp +from nns._rnmath import dlnorm_log as _rnmath_dlnorm_log +from nns._rnmath import dnorm as _rnmath_dnorm +from nns._rnmath import dt as _rnmath_dt +from nns._rnmath import libm_exp from nns.central_tendencies import nns_gravity, nns_mode from nns.dependence import nns_dep @@ -1064,68 +1069,80 @@ def _mreg_weighted_mode(values: NDArray[np.float64], weights: NDArray[np.float64 def _normalize_weights_rows(w: NDArray[np.float64]) -> NDArray[np.float64]: + """R's C++ ``normalize_prefix`` on every row: non-finite or negative weights + become 0, the row is summed sequentially and divided by that sum, and a row + without positive weight becomes uniform.""" w = np.where(np.isfinite(w) & (w >= 0), w, 0.0) - sums = w.sum(axis=1, keepdims=True) k = w.shape[1] + sums = np.cumsum(w, axis=1)[:, -1:] if k else np.zeros((w.shape[0], 1)) uniform = np.full_like(w, 1.0 / k) with np.errstate(invalid="ignore", divide="ignore"): scaled = w / sums return np.where(sums > 0, scaled, uniform) -def _mreg_ensemble_weights(dk: NDArray[np.float64]) -> NDArray[np.float64]: - """Vectorized eight-component ensemble weights for (m, k) sorted distances.""" - from scipy import stats # type: ignore[import-untyped] +_vdt = np.frompyfunc(_rnmath_dt, 2, 1) +_vdnorm = np.frompyfunc(_rnmath_dnorm, 3, 1) + +def _mreg_ensemble_weights(dk: NDArray[np.float64]) -> NDArray[np.float64]: + """The eight-component ensemble weights of R's C++ ``predict_sorted_k`` for + (m, k) sorted distances, term for term: R's ``dt``/``dnorm`` densities, + sequential sums, and the components added in R's order.""" m, k = dk.shape if k == 1: return np.ones((m, 1), dtype=np.float64) - - total = np.full((m, k), 1.0 / k, dtype=np.float64) # uniform - total += _normalize_weights_rows(stats.t.pdf(dk, df=k)) - total += _normalize_weights_rows(1.0 / np.maximum(dk, _EPS_INV)) - for row in _rank_weight_rows(k): - total += row - - dist_sd = np.std(dk, axis=1, ddof=1, keepdims=True) - valid_sd = np.isfinite(dist_sd) & (dist_sd > 0) + kk = float(k) + student = _normalize_weights_rows(np.asarray(_vdt(dk, kk), dtype=np.float64)) + inverse = _normalize_weights_rows(1.0 / np.maximum(dk, _EPS_INV)) + + mu = np.cumsum(dk, axis=1)[:, -1:] / kk + centred = dk - mu + with np.errstate(invalid="ignore"): + sd = np.sqrt(np.cumsum(centred * centred, axis=1)[:, -1:] / (k - 1)) + valid_sd = np.isfinite(sd) & (sd > 0) + normal = np.zeros((m, k), dtype=np.float64) if np.any(valid_sd): - with np.errstate(all="ignore"): - normal = stats.norm.pdf(dk, loc=0.0, scale=np.where(valid_sd, dist_sd, 1.0)) - normal = np.where(valid_sd, normal, 0.0) - total += np.where(valid_sd, _normalize_weights_rows(normal), 0.0) - - dist_var = dist_sd**2 - valid_var = np.isfinite(dist_var) & (dist_var > 0) + rows = np.flatnonzero(valid_sd[:, 0]) + dens = np.asarray(_vdnorm(dk[rows], 0.0, sd[rows]), dtype=np.float64) + normal[rows] = _normalize_weights_rows(dens) + var = np.where(np.isfinite(sd), sd * sd, np.nan) + valid_var = np.isfinite(var) & (var > 0) + rbf = np.zeros((m, k), dtype=np.float64) if np.any(valid_var): - with np.errstate(all="ignore"): - rbf = np.exp(-dk / (2.0 * np.where(valid_var, dist_var, 1.0))) - rbf = np.where(valid_var, rbf, 0.0) - total += np.where(valid_var, _normalize_weights_rows(rbf), 0.0) + rows = np.flatnonzero(valid_var[:, 0]) + with np.errstate(over="ignore", under="ignore"): + rbf[rows] = _normalize_weights_rows(libm_exp(-dk[rows] / (2.0 * var[rows]))) + uniform, exponential, lognormal, power = _rank_weight_rows(k) + total = uniform + student + inverse + exponential + lognormal + power + normal + rbf return _normalize_weights_rows(total) @functools.lru_cache(maxsize=512) def _rank_weight_rows(k: int) -> tuple[NDArray[np.float64], ...]: - """Normalised rank-only ensemble components for k neighbours, in the order - they are added: exponential, reversed lognormal (when the rank sd is - positive) and inverse-square rank. They do not depend on the distances, so - they are computed once per k rather than per query chunk and per k of a - path; each row is normalised exactly as a row of the broadcast matrix was. - """ - from scipy import stats - + """R's ``rank_components`` for k neighbours: the uniform, exponential, + reversed lognormal (zero unless the rank sd is positive) and inverse-square + rank components. They do not depend on the distances, so they are computed + once per k.""" ranks = np.arange(1, k + 1, dtype=np.float64) - rows = [_normalize_weights_rows(stats.expon.pdf(ranks, scale=k)[None, :])[0]] - rank_sd = float(np.std(ranks, ddof=1)) + uniform = np.full(k, 1.0 / k, dtype=np.float64) + exponential = _normalize_weights_rows( + np.array([[_rnmath_dexp(r, float(k)) for r in ranks]], dtype=np.float64) + )[0] + mu = float(np.cumsum(ranks)[-1]) / k + rank_sd = math.sqrt(float(np.cumsum((ranks - mu) * (ranks - mu))[-1]) / (k - 1)) if math.isfinite(rank_sd) and rank_sd > 0: - lognormal_row = np.abs(stats.lognorm.logpdf(ranks, s=rank_sd, scale=1.0)) - rows.append(_normalize_weights_rows(lognormal_row[None, :])[0, ::-1].copy()) - rows.append(_normalize_weights_rows((ranks**-2.0)[None, :])[0]) + lognormal = _normalize_weights_rows( + np.array([[abs(_rnmath_dlnorm_log(r, 0.0, rank_sd)) for r in ranks]]) + )[0, ::-1].copy() + else: + lognormal = np.zeros(k, dtype=np.float64) + power = _normalize_weights_rows((1.0 / (ranks * ranks))[None, :])[0] + rows = (uniform, exponential, lognormal, power) for row in rows: row.flags.writeable = False - return tuple(rows) + return rows def _mreg_distances( @@ -1341,7 +1358,8 @@ def _mreg_predict( if is_class: out[rows] = _mreg_weighted_mode_rows(yk, w) else: - out[rows] = np.sum(yk * w, axis=1) + # R accumulates the weighted sum sequentially. + out[rows] = np.cumsum(yk * w, axis=1)[:, -1] return out @@ -1395,7 +1413,7 @@ def _mreg_predict_path( for k in range(2, kmax + 1): w = _mreg_ensemble_weights(d_sorted[:, :k]) - out[rows, k - 1] = np.sum(y_sorted[:, :k] * w, axis=1) + out[rows, k - 1] = np.cumsum(y_sorted[:, :k] * w, axis=1)[:, -1] return out @@ -1745,6 +1763,7 @@ def nns_reg_engine( class_levels: Any = None, factor_levels: Any = None, multivariate_only: bool = False, + categorical: bool | None = None, ) -> dict[str, Any]: """Port of R ``NNS.reg`` (and, with ``multivariate_only``, ``NNS.M.reg``). @@ -1819,7 +1838,13 @@ def nns_reg_engine( class_values=task["class_values"], class_levels=task["class_levels"], variable_names=encoded.get("encoded_names"), - categorical=all(meta["categorical"] for meta in encoded["metadata"]), + # NNS.reg tells NNS.M.reg whether every predictor is categorical; + # a direct NNS.M.reg call uses its own [categorical] argument. + categorical=( + all(meta["categorical"] for meta in encoded["metadata"]) + if categorical is None + else categorical + ), ) synthetic_equation = None diff --git a/src/nns/_rnmath.hpp b/src/nns/_rnmath.hpp index 81d8efce..17d6046e 100644 --- a/src/nns/_rnmath.hpp +++ b/src/nns/_rnmath.hpp @@ -1,13 +1,14 @@ // Bit-faithful ports of the R nmath routines called by the NNS C++ kernels. // -// R's src/NNS_dep.cpp calls R::dhyper and R::pnorm. They use Loader's +// R's src/NNS_dep.cpp calls R::dhyper and R::pnorm; src/NNS_mreg_predict.cpp +// calls Rf_dt and Rf_dnorm4. They use Loader's // saddle-point binomial (dbinom_raw with stirlerr and bd0) and Cody's // rational approximation; closed forms from binomial products or erfc agree // with them only to a few units in the last place, which NNS later feeds into // exact comparisons. These are the R routines themselves, evaluated with the // same libm calls. // -// Source: R 4.5.0 src/nmath/{dhyper,dbinom,stirlerr,bd0,pnorm}.c +// Source: R 4.5.0 src/nmath/{dhyper,dbinom,stirlerr,bd0,pnorm,dnorm,dt}.c // Copyright (C) 2000-2024 The R Core Team; Catherine Loader; W. J. Cody. // GPL-2 or later. stirlerr follows the R >= 4.4.0 series cutoffs. // Mirrored in src/nns/_rnmath.py for the pure-Python fallback. @@ -231,4 +232,49 @@ inline double pnorm(double x) { return x > 0 ? 1. : 0.; } +// R::dnorm(x, mu, sigma, 0) (dnorm4, with the split evaluation beyond |x| = 5). +inline double dnorm(double x, double mu, double sigma) { + if (std::isnan(x) || std::isnan(mu) || std::isnan(sigma)) return x + mu + sigma; + if (sigma < 0) return NAN; + if (!std::isfinite(sigma)) return 0.; + if (!std::isfinite(x) && mu == x) return NAN; + if (sigma == 0) return (x == mu) ? INFINITY : 0.; + x = (x - mu) / sigma; + if (!std::isfinite(x)) return 0.; + x = std::fabs(x); + if (x >= 2 * std::sqrt(DBL_MAX)) return 0.; + if (x < 5) return k1SqrtTwoPi * std::exp(-0.5 * x * x) / sigma; + static const double kLn2 = 0.693147180559945309417232121458; + if (x > std::sqrt(-2 * kLn2 * (DBL_MIN_EXP + 1 - DBL_MANT_DIG))) return 0.; + const double x1 = std::ldexp(std::nearbyint(std::ldexp(x, 16)), -16); + const double x2 = x - x1; + return k1SqrtTwoPi / sigma * (std::exp(-0.5 * x1 * x1) * std::exp((-0.5 * x2 - x1) * x2)); +} + +// R::dt(x, n, 0): Loader's factorisation through bd0 and stirlerr. +inline double dt(double x, double n) { + if (std::isnan(x) || std::isnan(n)) return x + n; + if (n <= 0) return NAN; + if (!std::isfinite(x)) return 0.; + if (!std::isfinite(n)) return dnorm(x, 0., 1.); + double u; + const double t = -bd0(n / 2., (n + 1) / 2.) + stirlerr((n + 1) / 2.) - stirlerr(n / 2.); + const double x2n = x * x / n; + double ax = 0., l_x2n; + const bool lrg_x2n = (x2n > 1. / DBL_EPSILON); + if (lrg_x2n) { + ax = std::fabs(x); + l_x2n = std::log(ax) - std::log(n) / 2.; + u = n * l_x2n; + } else if (x2n > 0.2) { + l_x2n = std::log(1 + x2n) / 2.; + u = n * l_x2n; + } else { + l_x2n = std::log1p(x2n) / 2.; + u = -bd0(n / 2., (n + x * x) / 2.) + x * x / 2.; + } + const double I_sqrt_ = (lrg_x2n ? std::sqrt(n) / ax : std::exp(-l_x2n)); + return std::exp(t - u) * k1SqrtTwoPi * I_sqrt_; +} + } // namespace nns_rmath diff --git a/src/nns/_rnmath.py b/src/nns/_rnmath.py index 6b8ab2e9..ee9dcd86 100644 --- a/src/nns/_rnmath.py +++ b/src/nns/_rnmath.py @@ -21,6 +21,10 @@ import math import sys from collections.abc import Callable +from typing import Any + +import numpy as np +from numpy.typing import NDArray _DBL_EPSILON = sys.float_info.epsilon _DBL_MIN = sys.float_info.min @@ -494,3 +498,115 @@ def qnorm(p: float, mu: float = 0.0, sigma: float = 1.0) -> float: if q < 0.0: val = -val return mu + sigma * val + + +def dt(x: float, n: float) -> float: + """R's ``dt(x, n)`` (density, not log): Loader's factorisation through + ``bd0`` and ``stirlerr``, as in R 4.5.0 ``src/nmath/dt.c``.""" + if math.isnan(x) or math.isnan(n): + return x + n + if n <= 0: + return math.nan + if not math.isfinite(x): + return 0.0 + if not math.isfinite(n): + return dnorm(x, 0.0, 1.0) + t = -bd0(n / 2.0, (n + 1) / 2.0) + stirlerr((n + 1) / 2.0) - stirlerr(n / 2.0) + x2n = x * x / n + large = x2n > 1.0 / _DBL_EPSILON + if large: + ax = abs(x) + l_x2n = math.log(ax) - math.log(n) / 2.0 + u = n * l_x2n + elif x2n > 0.2: + ax = 0.0 + l_x2n = math.log(1 + x2n) / 2.0 + u = n * l_x2n + else: + ax = 0.0 + l_x2n = math.log1p(x2n) / 2.0 + u = -bd0(n / 2.0, (n + x * x) / 2.0) + x * x / 2.0 + i_sqrt = math.sqrt(n) / ax if large else math.exp(-l_x2n) + return math.exp(t - u) * _M_1_SQRT_2PI * i_sqrt + + +def dnorm(x: float, mu: float = 0.0, sigma: float = 1.0) -> float: + """R's ``dnorm(x, mu, sigma)`` (``dnorm4``, density), as in R 4.5.0 + ``src/nmath/dnorm.c`` with its split evaluation beyond ``|x| = 5``.""" + if math.isnan(x) or math.isnan(mu) or math.isnan(sigma): + return x + mu + sigma + if sigma < 0: + return math.nan + if not math.isfinite(sigma): + return 0.0 + if not math.isfinite(x) and mu == x: + return math.nan + if sigma == 0: + return math.inf if x == mu else 0.0 + z = (x - mu) / sigma + if not math.isfinite(z): + return 0.0 + z = abs(z) + if z >= 2 * math.sqrt(sys.float_info.max): + return 0.0 + if z < 5: + return _M_1_SQRT_2PI * math.exp(-0.5 * z * z) / sigma + if z > math.sqrt(-2 * _M_LN2 * (sys.float_info.min_exp + 1 - sys.float_info.mant_dig)): + return 0.0 + x1 = math.ldexp(_r_forceint(math.ldexp(z, 16)), -16) + x2 = z - x1 + return _M_1_SQRT_2PI / sigma * (math.exp(-0.5 * x1 * x1) * math.exp((-0.5 * x2 - x1) * x2)) + + +def dlnorm_log(x: float, meanlog: float, sdlog: float) -> float: + """R's ``dlnorm(x, meanlog, sdlog, log = TRUE)`` (R 4.5.0 ``dlnorm.c``).""" + if math.isnan(x) or math.isnan(meanlog) or math.isnan(sdlog): + return x + meanlog + sdlog + if sdlog < 0: + return math.nan + if sdlog == 0: + return math.inf if x > 0 and math.log(x) == meanlog else -math.inf + if x <= 0: + return -math.inf + y = (math.log(x) - meanlog) / sdlog + return -(_M_LN_SQRT_2PI + 0.5 * y * y + math.log(x * sdlog)) + + +def dexp(x: float, scale: float) -> float: + """R's ``dexp(x, rate = 1 / scale)`` as C ``dexp(x, scale)`` (density).""" + if math.isnan(x) or math.isnan(scale): + return x + scale + if scale <= 0.0: + return math.nan + if x < 0.0: + return 0.0 + return math.exp(-x / scale) / scale + + +_M_LN2 = 0.693147180559945309417232121458 # log(2) + + +def _r_forceint(x: float) -> float: + """R_forceint: C ``nearbyint`` in the default round-half-even mode.""" + return float(round(x)) + + +def _c_exp(x: float) -> float: + """C ``exp``: ``math.exp`` is libm's, but raises where C returns ``inf``.""" + try: + return math.exp(x) + except OverflowError: + return math.inf + + +_libm_exp_ufunc = np.frompyfunc(_c_exp, 1, 1) + + +def libm_exp(values: Any) -> NDArray[np.float64]: + """Elementwise libm ``exp``, as R evaluates ``exp()`` on a vector. + + NumPy's vectorised ``exp`` (SIMD on AVX2/AVX-512 builds) is not libm's and + differs from it in the last bit for a few percent of inputs, which matters + wherever R's result is compared exactly or feeds a discrete choice. + """ + return np.asarray(_libm_exp_ufunc(np.asarray(values, dtype=np.float64)), dtype=np.float64) diff --git a/src/nns/multivariate_regression.py b/src/nns/multivariate_regression.py index 3c52b2fc..7cbd6275 100644 --- a/src/nns/multivariate_regression.py +++ b/src/nns/multivariate_regression.py @@ -73,6 +73,7 @@ def nns_m_reg( confidence_interval: float | None = None, class_levels: list[object] | None = None, factor_levels: Any = None, + categorical: bool = False, ) -> MRegResult: """Multivariate regression matching R's ``NNS.M.reg``. @@ -84,7 +85,10 @@ def nns_m_reg( [point_est] columns are matched by name. A string, bool or categorical response (or one with ``class_levels``) is a classification target, as in R; ``class_levels`` fixes the code order. At least two encoded predictor - columns are required. + columns are required. ``categorical=True`` (R's ``categorical``) fits exact + joint cells: maximum order and, unless ``n_best`` is given, one neighbour. + As in R, it is not inferred from the predictors here; ``nns_reg`` sets it + when every predictor is categorical. """ _warn_unsupported( location=location is not None, @@ -112,6 +116,7 @@ def nns_m_reg( class_levels=class_levels, factor_levels=factor_levels, multivariate_only=True, + categorical=bool(categorical), ), ) if (plot or residual_plot) and isinstance(result.get("Fitted.xy"), dict): diff --git a/sync/nns_source.json b/sync/nns_source.json index 4f6d29a8..fa81f5bf 100644 --- a/sync/nns_source.json +++ b/sync/nns_source.json @@ -33,6 +33,14 @@ "r_source_sha256": "2e577f57165fd38d779dcde369eabf4800135eb50e07079069c11be2fedaa115", "r_source_commit": "7b07a837c130ba7220dbe16c584221508757d5ae", "adaptation": "R14 multilevel/null-corrected dependence with vector marshalling. R::dhyper and R::pnorm are the ports of R's own nmath routines in src/nns/_rnmath.hpp, so the null expectations match R's bit for bit. Bound explicitly instead of the untouched vendor dependence estimator." + }, + { + "path": "src/nns/_r14_mreg_predict.cpp", + "sha256": "f93147dd754f415aa5d3786578d002b7931c2360d8683ce5770614c946ad038c", + "r_source_path": "src/NNS_mreg_predict.cpp", + "r_source_sha256": "e0fac52e99f33cd29fbb07b51711ded56c016d9e1a4b4a8eac8a9b4b9ed98571", + "r_source_commit": "7b07a837c130ba7220dbe16c584221508757d5ae", + "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." } ], "r_cache_summary": { @@ -44,7 +52,7 @@ "r_nmath_ports": [ { "path": "src/nns/_rnmath.hpp", - "sha256": "1e074f7aab24c9f4e1762f5c2641bf283e52a2aaf5e1b95f8a5c05cd29318556", + "sha256": "5960df5ce9939e908b8de2645c8cd655aa8901472498d61c73ff6c1412ace083", "python_mirror": "src/nns/_rnmath.py", "r_version": "4.5.0", "r_sources": [ @@ -52,10 +60,12 @@ "src/nmath/dbinom.c", "src/nmath/stirlerr.c", "src/nmath/bd0.c", - "src/nmath/pnorm.c" + "src/nmath/pnorm.c", + "src/nmath/dnorm.c", + "src/nmath/dt.c" ], "license": "GPL-2 or later (R Core Team), redistributed under GPL-3.0-only", - "notes": "dhyper/dbinom_raw/stirlerr/bd0/pow1p and the lower tail of pnorm_both, as called by R::dhyper and R::pnorm in src/NNS_dep.cpp; stirlerr follows the R >= 4.4.0 series cutoffs." + "notes": "dhyper/dbinom_raw/stirlerr/bd0/pow1p and the lower tail of pnorm_both, as called by R::dhyper and R::pnorm in src/NNS_dep.cpp; stirlerr follows the R >= 4.4.0 series cutoffs. dt and dnorm4 are the densities R::dt / Rf_dnorm4 behind src/NNS_mreg_predict.cpp's ensemble weights." } ] } diff --git a/tests/_r_cache/mreg14.bits.json b/tests/_r_cache/mreg14.bits.json new file mode 100644 index 00000000..16c3b947 --- /dev/null +++ b/tests/_r_cache/mreg14.bits.json @@ -0,0 +1,149 @@ +{ + "entries": { + "4b6cd575c4d64697583fc79c16e1a9dba1e31ef6a6bdb03af648183053b4f1ea": { + "fit": [ + 0.28436300200947096, + 0.014131561318050385, + 0.46841741352904787, + 0.12789977921318085, + -0.07869850380195968, + 0.6967473924332744, + 0.006827513330736085, + -0.056034661318905374, + 0.6217175961964153, + -0.22758286225519966, + -0.007301006602025394, + -0.04179613099976469, + 0.08792748346820588, + 0.06146398546445617, + 0.022008239661471272, + -0.003457921331149416, + 0.06716835728491247, + -0.03179398961641432, + 0.72162012914694, + -0.03316025363347803, + 0.006520086092346375, + -0.11075024133975203, + -0.2518219976759478, + 0.21197015526489804, + 0.001998831817661008, + -0.07200409850948207, + -0.051941641266492214, + -0.10845796947405825, + -0.10209696172423241, + -0.11466607837102771, + 0.26889366605511666, + -0.3448967554058242, + -0.07359398997734123, + 0.03539569863751002, + -0.012660229959752516, + 0.3645354439947468, + 0.3793051722209922, + -0.9526147151934246, + -0.09276136315904739, + -0.05856526259680626, + -0.008009314789224627, + 0.41238208472700666, + -0.09385320444732587, + 0.021098915481437414, + 0.07381696302866703, + -0.5617486654123821, + 0.04435567064683739, + 0.012312310236507078, + 0.13140617900566282, + 0.07245010652141935, + 0.149872302498116, + -0.11971489948335863, + 0.14411126403402202, + 0.03631092726204853, + 0.031443888441635016, + 0.05400662179202079, + 0.11554325438490533, + -0.004432282403522593, + 0.006266420955424609, + 0.06896463847705966 + ], + "point": [ + 0.28436300200947096, + 0.014131561318050385, + 0.46841741352904787, + 0.12789977921318085, + -0.07869850380195968 + ] + }, + "53b4aa411093b633633250085a7c896b673542a8bfaeb56c9aaa4406be91d1b4": { + "fit": [ + -0.028561266522590764, + -0.1922336877045077, + -0.06133419928425057, + 0.03422722711863955, + -0.1259549118382452, + -0.10295223286614236, + 0.007787134651678462, + 0.056017995143274706, + 0.21945745533292, + -0.40595659145845836, + -0.00344794915003329, + -0.12265132113294072, + 0.13239380986994714, + 0.032230370160898504, + 0.21223744699992486, + -0.07477217784778205, + 0.007433990800812812, + -0.17765332697348904, + 0.3032163259136277, + -0.0777265513974915, + -0.006955332724827741, + -0.10114321124893581, + -0.3403035164613025, + 0.2545768427179019, + 0.010685519374991424, + -0.11394758044466709, + -0.02165762433142611, + -0.14350247045858255, + -0.18666334442742505, + -0.1351900880595985, + 0.037521043730500706, + -0.132927001098891, + -0.08049626692281975, + -0.03778967053877623, + -0.0008338484349096065, + 0.18724165439176077, + 0.22574043898632662, + -0.3744952028465698, + -0.10629352687237333, + -0.10272890695815323, + 0.011645341548783258, + -0.02978293511035017, + -0.18034992820320792, + -0.02066834090901827, + -0.01771429392133423, + -0.38665311694880733, + 0.028155807190898878, + 0.08354540886749706, + 0.27500358058654156, + 0.11642059661063935, + 0.08344820520478871, + -0.392859125935228, + 0.2644672284224572, + 0.1278331285866814, + 0.0013272627101283963, + -0.05078700698424904, + -0.18350701273266334, + 0.043521022613798935, + -0.10396433950167203, + -0.03848950386182475 + ], + "point": [ + -0.028561266522590764, + -0.1922336877045077, + -0.06133419928425057, + 0.03422722711863955, + -0.1259549118382452 + ] + } + }, + "label": "mreg14.bits", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/mreg14.categorical.json b/tests/_r_cache/mreg14.categorical.json new file mode 100644 index 00000000..255f4bd4 --- /dev/null +++ b/tests/_r_cache/mreg14.categorical.json @@ -0,0 +1,21 @@ +{ + "entries": { + "0cc61802092c96179f86334422bbbc69beb22b150556b505914ab03b7f4c1fb0": [ + 4.204892824685064, + 0.9968075219863931, + 0.9968075219863931, + 4.204892824685064, + 0.9968075219863931 + ], + "4913e64b5ceb6d5ff7f15a315e18f4f3b3b6fd6ae9e8c14e16e59c476ab2a98d": [ + 3.5577467896454067, + 1.866584972636165, + 1.866584972636165, + 3.5577467896454067, + 1.866584972636165 + ] + }, + "label": "mreg14.categorical", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/mreg14.class_bits.json b/tests/_r_cache/mreg14.class_bits.json new file mode 100644 index 00000000..06fa70b3 --- /dev/null +++ b/tests/_r_cache/mreg14.class_bits.json @@ -0,0 +1,629 @@ +{ + "entries": { + "d84c6914293a2c6e886c98fcd039e3fc9fe4404712e554fdc733c70457be3e86": { + "fit": [ + [ + 0.26193885679295886, + 0.7380611432070411 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 0.6763845847874679, + 0.3236154152125322 + ], + [ + 1.0, + 0.0 + ], + [ + 0.8729185997063811, + 0.127081400293619 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.9766769925486362, + 0.023323007451363887 + ], + [ + 1.0, + 0.0 + ], + [ + 0.3273644983458174, + 0.6726355016541826 + ], + [ + 0.0, + 1.0 + ], + [ + 0.7661718532960087, + 0.23382814670399138 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 0.9781991355368073, + 0.021800864463192793 + ], + [ + 0.21658203187530012, + 0.7834179681246999 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.9281704361237723, + 0.07182956387622785 + ], + [ + 0.3643199698036493, + 0.6356800301963508 + ], + [ + 0.0, + 1.0 + ], + [ + 0.39961869761734703, + 0.600381302382653 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 0.17259297035941992, + 0.8274070296405801 + ], + [ + 0.06694578818100207, + 0.933054211818998 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.898203033097136, + 0.10179696690286404 + ], + [ + 1.0, + 0.0 + ], + [ + 0.3357137568582073, + 0.6642862431417927 + ], + [ + 0.9394135293796131, + 0.06058647062038695 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.9254021717403207, + 0.07459782825967927 + ], + [ + 1.0, + 0.0 + ], + [ + 0.13327607915031364, + 0.8667239208496863 + ], + [ + 0.9724958670557649, + 0.027504132944235066 + ], + [ + 0.7627726216660602, + 0.2372273783339397 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.5747855904944305, + 0.4252144095055695 + ], + [ + 0.7587749231574646, + 0.24122507684253539 + ], + [ + 1.0, + 0.0 + ], + [ + 0.8041865681778076, + 0.1958134318221924 + ], + [ + 1.0, + 0.0 + ], + [ + 0.9030780964947767, + 0.0969219035052234 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.06954935452862243, + 0.9304506454713776 + ], + [ + 0.0, + 1.0 + ], + [ + 0.0, + 1.0 + ], + [ + 0.0, + 1.0 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.7012905271764676, + 0.29870947282353244 + ], + [ + 1.0, + 0.0 + ], + [ + 0.9802561228076668, + 0.01974387719233326 + ], + [ + 0.7763332054986979, + 0.22366679450130214 + ], + [ + 1.0, + 0.0 + ], + [ + 0.6906069788045395, + 0.30939302119546047 + ], + [ + 0.25884707577328026, + 0.7411529242267197 + ], + [ + 0.6804074767737213, + 0.3195925232262788 + ], + [ + 0.2670412691754036, + 0.7329587308245964 + ], + [ + 0.8929863836851831, + 0.10701361631481693 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 0.0, + 1.0 + ], + [ + 0.7803169195898112, + 0.21968308041018883 + ], + [ + 0.0, + 1.0 + ], + [ + 0.7294463294655682, + 0.2705536705344318 + ], + [ + 0.881684882181785, + 0.11831511781821506 + ], + [ + 1.0, + 0.0 + ], + [ + 0.1340783527302299, + 0.8659216472697701 + ], + [ + 1.0, + 0.0 + ], + [ + 0.5490760460750339, + 0.4509239539249661 + ], + [ + 0.9256027263016643, + 0.07439727369833575 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 0.0, + 1.0 + ], + [ + 0.9292986005056313, + 0.07070139949436872 + ], + [ + 0.0, + 1.0 + ], + [ + 0.9395778016822202, + 0.06042219831777982 + ], + [ + 0.2482701947112941, + 0.7517298052887059 + ], + [ + 1.0, + 0.0 + ], + [ + 0.9074359400400485, + 0.09256405995995161 + ], + [ + 1.0, + 0.0 + ], + [ + 0.5341631005126666, + 0.4658368994873335 + ], + [ + 0.0, + 1.0 + ], + [ + 0.8968982781802505, + 0.10310172181974964 + ], + [ + 1.0, + 0.0 + ], + [ + 0.8526352750495727, + 0.14736472495042735 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 0.0, + 1.0 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.5962698232644927, + 0.40373017673550726 + ], + [ + 0.8095291713310416, + 0.1904708286689584 + ], + [ + 0.0, + 1.0 + ], + [ + 0.47020422479041696, + 0.5297957752095831 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.9558887179240417, + 0.044111282075958214 + ], + [ + 0.0, + 1.0 + ], + [ + 0.4427870460930371, + 0.5572129539069629 + ], + [ + 0.8779366556093211, + 0.12206334439067898 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 0.9579092478763197, + 0.042090752123680214 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 0.941229181088947, + 0.05877081891105298 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.8818311386967279, + 0.11816886130327213 + ], + [ + 0.9171761583104232, + 0.08282384168957684 + ], + [ + 0.9629749786128339, + 0.03702502138716602 + ], + [ + 1.0, + 0.0 + ], + [ + 1.0, + 0.0 + ], + [ + 0.5455806077205296, + 0.45441939227947037 + ], + [ + 0.0, + 1.0 + ], + [ + 1.0, + 0.0 + ] + ], + "point": [ + [ + 0.26193885679295886, + 0.7380611432070411 + ], + [ + 1.0, + 0.0 + ], + [ + 0.0, + 1.0 + ], + [ + 0.6763845847874679, + 0.3236154152125322 + ] + ] + } + }, + "label": "mreg14.class_bits", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/invariants/test_diff.py b/tests/invariants/test_diff.py index 2cc0195f..aad179c6 100644 --- a/tests/invariants/test_diff.py +++ b/tests/invariants/test_diff.py @@ -242,8 +242,8 @@ def test_stable_topk_matches_full_stable_sort() -> None: def test_mreg_predict_path_chunking_is_exact(monkeypatch: pytest.MonkeyPatch) -> None: - # The chunked/partial-selection NumPy path must equal a full-sort reference; - # the native kernel (R's NNS_mreg_predict_path_v2_cpp) agrees to rounding. + # The chunked/partial-selection NumPy path must equal a full-sort reference, + # as does the native kernel (R's NNS_mreg_predict_path_v2_cpp). import nns._reg_engine as engine from nns._reg_engine import _mreg_distances, _mreg_ensemble_weights, _mreg_predict_path from nns.central_tendencies import nns_gravity @@ -263,7 +263,8 @@ def reference(xtest, rpm_x, rpm_yhat, kmax, dist, mins, maxs): out[i, 0] = float(nns_gravity(t[np.isfinite(t)])) for k in range(2, kmax + 1): w = _mreg_ensemble_weights(ds[:, :k]) - out[:, k - 1] = np.sum(ys[:, :k] * w, axis=1) + # R's kernel accumulates the weighted average sequentially. + out[:, k - 1] = np.cumsum(ys[:, :k] * w, axis=1)[:, -1] return out rng = np.random.RandomState(1) @@ -278,7 +279,7 @@ def reference(xtest, rpm_x, rpm_yhat, kmax, dist, mins, maxs): patch.setattr(engine, "native_fn", lambda name: None) b = _mreg_predict_path(xt, rpm_x, rpm_y, kmax, "NNS", mins, maxs) np.testing.assert_allclose(a, b, rtol=0, atol=0) - np.testing.assert_allclose(native, b, rtol=1e-12, atol=1e-14) + np.testing.assert_array_equal(native, b) def _band_means(x: np.ndarray, y: np.ndarray, p: float) -> list[float]: diff --git a/tests/invariants/test_mreg_fast_paths.py b/tests/invariants/test_mreg_fast_paths.py index eed5021b..c5556170 100644 --- a/tests/invariants/test_mreg_fast_paths.py +++ b/tests/invariants/test_mreg_fast_paths.py @@ -17,7 +17,6 @@ _mreg_weighted_mode, _mreg_weighted_mode_rows, _nearest_tie_values, - _normalize_weights_rows, _reduce_value, _stable_topk, ) @@ -224,34 +223,57 @@ def test_multivariate_point_only_matches_full_fit(kwargs: dict[str, object]) -> def _reference_ensemble_weights(dk: np.ndarray) -> np.ndarray: - """The former implementation (rank-only components rebuilt per call).""" - from scipy import stats + """A per-row scalar transcription of R's C++ ``predict_sorted_k_noalloc`` + weights (src/NNS_mreg_predict.cpp), the oracle for the vectorised, cached + implementation.""" + import math + + from nns._rnmath import dexp, dlnorm_log, dnorm, dt + + def normalize(values: list[float]) -> list[float]: + cleaned = [v if math.isfinite(v) and v >= 0.0 else 0.0 for v in values] + total = 0.0 + for v in cleaned: + total += v + if total <= 0.0: + return [1.0 / len(cleaned)] * len(cleaned) + return [v / total for v in cleaned] + + def sample_sd(values: list[float]) -> float: + mu = 0.0 + for v in values: + mu += v + mu /= len(values) + acc = 0.0 + for v in values: + acc += (v - mu) * (v - mu) + return math.sqrt(acc / (len(values) - 1)) m, k = dk.shape if k == 1: return np.ones((m, 1)) - ranks = np.arange(1, k + 1, dtype=np.float64) - total = np.full((m, k), 1.0 / k) - total += _normalize_weights_rows(stats.t.pdf(dk, df=k)) - total += _normalize_weights_rows(1.0 / np.maximum(dk, 1e-12)) - total += _normalize_weights_rows( - np.broadcast_to(stats.expon.pdf(ranks, scale=k), (m, k)).copy() - ) - rank_sd = float(np.std(ranks, ddof=1)) - lognormal_row = np.abs(stats.lognorm.logpdf(ranks, s=rank_sd, scale=1.0)) - total += _normalize_weights_rows(np.broadcast_to(lognormal_row, (m, k)).copy())[:, ::-1] - total += _normalize_weights_rows(np.broadcast_to(ranks**-2.0, (m, k)).copy()) - dist_sd = np.std(dk, axis=1, ddof=1, keepdims=True) - valid_sd = np.isfinite(dist_sd) & (dist_sd > 0) - with np.errstate(all="ignore"): - normal = stats.norm.pdf(dk, loc=0.0, scale=np.where(valid_sd, dist_sd, 1.0)) - total += np.where(valid_sd, _normalize_weights_rows(np.where(valid_sd, normal, 0.0)), 0.0) - dist_var = dist_sd**2 - valid_var = np.isfinite(dist_var) & (dist_var > 0) - with np.errstate(all="ignore"): - rbf = np.exp(-dk / (2.0 * np.where(valid_var, dist_var, 1.0))) - total += np.where(valid_var, _normalize_weights_rows(np.where(valid_var, rbf, 0.0)), 0.0) - return _normalize_weights_rows(total) + ranks = [float(i + 1) for i in range(k)] + uniform = [1.0 / k] * k + exponential = normalize([dexp(r, float(k)) for r in ranks]) + rank_sd = sample_sd(ranks) + lognormal = normalize([abs(dlnorm_log(r, 0.0, rank_sd)) for r in ranks])[::-1] + power = normalize([1.0 / (r * r) for r in ranks]) + out = np.empty((m, k)) + for row in range(m): + d = [float(v) for v in dk[row]] + student = normalize([dt(v, float(k)) for v in d]) + inverse = normalize([1.0 / max(v, 1e-12) for v in d]) + sd = sample_sd(d) + var = sd * sd + normal = normalize([dnorm(v, 0.0, sd) for v in d]) if sd > 0 else [0.0] * k + rbf = normalize([math.exp(-v / (2.0 * var)) for v in d]) if var > 0 else [0.0] * k + total = [ + uniform[i] + student[i] + inverse[i] + exponential[i] + lognormal[i] + power[i] + + normal[i] + rbf[i] + for i in range(k) + ] + out[row] = normalize(total) + return out @pytest.mark.parametrize("k", [2, 3, 7, 40, 129]) diff --git a/tests/invariants/test_r14_shared_paths.py b/tests/invariants/test_r14_shared_paths.py index 302f07ae..a82474f0 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"]) == 2 + assert len(manifest["r14_native_overlays"]) == 3 for overlay in manifest["r14_native_overlays"]: assert overlay["r_source_commit"] == manifest["r_commit"] assert not overlay["path"].startswith("extern/") diff --git a/tests/parity/test_mreg_bits_r14.py b/tests/parity/test_mreg_bits_r14.py new file mode 100644 index 00000000..4c9b5c07 --- /dev/null +++ b/tests/parity/test_mreg_bits_r14.py @@ -0,0 +1,110 @@ +"""R 14.0 parity, to the last bit, for multivariate regression and classification. + +* R's C++ ``NNS_mreg_predict_v2_cpp`` weights the k nearest regression points + with an eight-component ensemble whose Student-t and normal terms are R's own + nmath ``dt`` and ``dnorm4``, summed sequentially; the native overlay and the + NumPy fallback both reproduce it (and its libm ``exp``). +* R's classifier uses the R-level ``.nns_mreg_ensemble_weights`` instead: + long-double ``sum()``/``var()``, ``dexp(rate = 1 / k)``, ``ranks^(-2)`` and + ``rowSums`` distances, which differ from the C++ kernel from three + predictors on. +* ``nns_m_reg`` is R's internal ``NNS.M.reg``, whose ``categorical`` argument + defaults to FALSE; ``NNS.reg`` sets it when every predictor is categorical. +""" + +from __future__ import annotations + +import importlib +from typing import Any + +import numpy as np +import pytest +from _r import nns_frame_call, r_column + +import nns + +pytestmark = pytest.mark.parity + +reg_engine = importlib.import_module("nns._reg_engine") + + +def _num(values: Any) -> dict[str, Any]: + return r_column([float(v) for v in np.asarray(values, dtype=np.float64).ravel()], "numeric") + + +def _design(seed: int, n: int, p: int) -> tuple[np.ndarray, np.ndarray]: + rng = np.random.default_rng(seed) + x = rng.normal(size=(n, p)) + y = x[:, 0] * x[:, 1] + rng.normal(size=n) * 0.2 + return x, y + + +@pytest.mark.parametrize("native", [True, False]) +@pytest.mark.parametrize(("dist", "n_best", "r_args"), [ + ("NNS", None, ""), ("L2", "all", ", order = 2, n.best = 'all', dist = 'L2'"), +]) +def test_multivariate_fit_matches_r_bitwise( + monkeypatch: pytest.MonkeyPatch, native: bool, dist: str, n_best: str | None, r_args: str +) -> None: + x, y = _design(6, 60, 3) + expected = nns_frame_call( + "mreg14.bits", + f"r <- NNS.reg(matrix(v, ncol = 3), y, point.est = matrix(v, ncol = 3)[1:5, ]{r_args}, " + "plot = FALSE); list(fit = r$Fitted.xy$y.hat, point = r$Point.est)", + {"v": _num(x.ravel(order="F")), "y": _num(y)}, + exact=True, + ) + if not native: + monkeypatch.setattr(reg_engine, "native_fn", lambda name: None) + order = 2 if n_best == "all" else None + actual = nns.nns_reg(x, y, point_est=x[:5], order=order, n_best=n_best, dist=dist) + np.testing.assert_array_equal(actual["Fitted.xy"]["y.hat"], np.asarray(expected["fit"])) + np.testing.assert_array_equal(actual["Point.est"], np.asarray(expected["point"])) + + +def test_three_predictor_class_probabilities_match_r_bitwise() -> None: + rng = np.random.default_rng(7) + x = rng.normal(size=(150, 3)) + y = (x[:, 0] + x[:, 1] > 0).astype(float) + expected = nns_frame_call( + "mreg14.class_bits", + "r <- NNS.reg(matrix(v, ncol = 3), y, point.est = matrix(v, ncol = 3)[1:4, ], " + "type = 'CLASS', plot = FALSE); list(fit = unname(r$Fitted.probabilities), " + "point = unname(r$Point.probabilities))", + {"v": _num(x.ravel(order="F")), "y": _num(y)}, + exact=True, + ) + actual = nns.nns_reg(x, y, point_est=x[:4], type="CLASS") + + def matrix(probabilities: Any) -> np.ndarray: + # The port keys each class's column by its label, in R's column order. + return np.column_stack([np.asarray(v, dtype=np.float64) for v in probabilities.values()]) + + np.testing.assert_array_equal( + matrix(actual["Fitted.probabilities"]), np.asarray(expected["fit"], dtype=np.float64) + ) + np.testing.assert_array_equal( + matrix(actual["Point.probabilities"]), np.asarray(expected["point"], dtype=np.float64) + ) + + +@pytest.mark.parametrize("categorical", [False, True]) +def test_nns_m_reg_categorical_follows_r_internal_default(categorical: bool) -> None: + import pandas as pd # type: ignore[import-untyped] + + rng = np.random.default_rng(8) + a = rng.choice(["p", "q", "r"], 40) + b = rng.choice(["u", "v"], 40) + y = (pd.Categorical(a).codes * 2.0 + pd.Categorical(b).codes) + rng.normal(size=40) * 0.3 + flag = "TRUE" if categorical else "FALSE" + expected = nns_frame_call( + "mreg14.categorical", + f"d <- data.frame(a = factor(a), b = factor(b)); NNS:::NNS.M.reg(d, y, " + f"point.est = d[1:5, ], categorical = {flag})$Point.est", + {"a": r_column(a.tolist(), "character"), "b": r_column(b.tolist(), "character"), + "y": _num(y)}, + exact=True, + ) + frame = pd.DataFrame({"a": pd.Categorical(a), "b": pd.Categorical(b)}) + actual = nns.nns_m_reg(frame, y, point_est=frame.iloc[:5], categorical=categorical) + np.testing.assert_array_equal(actual["Point.est"], np.asarray(expected, dtype=np.float64)) From d019978a7683615b19d73cccc12a34adcfbc8029 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 00:59:23 +0000 Subject: [PATCH 09/16] Match R's partial-moment arithmetic to the last bit NNS.moments now follows R 14: deviations from R's mean(), mean(z^3) and mean(z^4) with R's ^ (libm pow), and the variance raised by ^1.5. The NumPy fallbacks for the bivariate and n-dimensional co-moments and PM.matrix now reproduce R's C++: integer degrees by repeated multiplication, fractional ones by libm pow, double sums accumulated row by row and scaled by 1 / n. PM.matrix fills the upper triangle and mirrors it with DUPM and DLPM crossed, normalises in R's order, and takes its default target from Rcpp's colMeans (a row-by-row sum), whatever the input's memory layout. A live sweep of 2932 partial-moment cases now matches R exactly with the native kernels and with the fallback. New bitwise parity tests cover NNS.moments, the co-moments, the nD co-moments and PM.matrix in both modes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt --- docs/conventions.md | 15 +- src/nns/_rnmath.py | 33 ++ src/nns/classical.py | 31 +- src/nns/co_moments.py | 12 +- src/nns/dependence.py | 45 +- src/nns/pm_matrix.py | 72 ++- tests/_r_cache/pm14.co_moments.json | 85 +++ tests/_r_cache/pm14.moments.json | 19 + tests/_r_cache/pm14.nd.json | 22 + tests/_r_cache/pm14.pm_matrix.json | 535 ++++++++++++++++++ tests/parity/test_partial_moments_bits_r14.py | 127 +++++ 11 files changed, 950 insertions(+), 46 deletions(-) create mode 100644 tests/_r_cache/pm14.co_moments.json create mode 100644 tests/_r_cache/pm14.moments.json create mode 100644 tests/_r_cache/pm14.nd.json create mode 100644 tests/_r_cache/pm14.pm_matrix.json create mode 100644 tests/parity/test_partial_moments_bits_r14.py diff --git a/docs/conventions.md b/docs/conventions.md index d18d0970..8ebe69ed 100644 --- a/docs/conventions.md +++ b/docs/conventions.md @@ -69,7 +69,8 @@ check the same rules. ## PM Matrix Target Defaults R `PM.matrix` uses column means when `target` is `NULL` or any non-numeric value. -NNS Python accepts `None` and `"mean"` for this behavior. NNS Python also broadcasts a +NNS Python accepts `None` and `"mean"` for this behavior, with Rcpp's `colMeans` +arithmetic (a row-by-row double sum divided by the row count). NNS Python also broadcasts a scalar numeric target across all variables; R requires callers to pass an explicit vector such as `rep(0, ncol(variable))`. Target vectors whose length does not match the number of variables raise `ValueError`. @@ -83,6 +84,18 @@ values for a bimodal sample). Pass `multi=True` for R's default output; explicit finder also reports spurious secondary modes on unimodal data (in R as well), and the internal callers all ask for a single mode. +## Partial-Moment Arithmetic + +The partial moments, co-moments (`co_lpm`, `co_upm`, `d_lpm`, `d_upm`, the +`_nd` variants) and `pm_matrix` reproduce R's C++ to the last bit, with the +native kernels and with the NumPy fallback: integer degrees by repeated +multiplication, fractional degrees by libm `pow`, double sums accumulated +observation by observation and scaled by `1 / n`. `pm_matrix` fills the upper +triangle and mirrors it (CUPM, CLPM and the covariance plainly, DUPM and DLPM +crossed), normalising each cell by `CUPM + DUPM + DLPM + CLPM` in that order. +`nns_moments` is R 14's `NNS.moments`: deviations from R's `mean()`, +`mean(z^3)` and `mean(z^4)` with R's `^`, and the variance raised by `^1.5`. + ## Classical Moment Normalization `mean_pm`, `var_pm`, `skew_pm`, and `kurt_pm` use population normalization by diff --git a/src/nns/_rnmath.py b/src/nns/_rnmath.py index ee9dcd86..da98f24d 100644 --- a/src/nns/_rnmath.py +++ b/src/nns/_rnmath.py @@ -610,3 +610,36 @@ def libm_exp(values: Any) -> NDArray[np.float64]: wherever R's result is compared exactly or feeds a discrete choice. """ return np.asarray(_libm_exp_ufunc(np.asarray(values, dtype=np.float64)), dtype=np.float64) + + +def r_pow_scalar(x: float, y: float) -> float: + """R's ``x ^ y`` (arithmetic.c ``R_POW``/``R_pow``) for doubles: ``x * x`` + for 2, 1 for a base of 1 or an exponent of 0, R's zero-base rules, else + libm ``pow``.""" + if y == 2.0: + return x * x + if x == 1.0 or y == 0.0: + return 1.0 + if x == 0.0: + if y > 0.0: + return 0.0 + return math.inf if y < 0.0 else y + if math.isfinite(x) and math.isfinite(y): + try: + return math.pow(x, y) + except ValueError: # negative base with a fractional exponent + return math.nan + except OverflowError: + return math.copysign(math.inf, x) if float(y).is_integer() and y % 2 else math.inf + return float(np.power(np.float64(x), np.float64(y))) + + +_libm_pow_ufunc = np.frompyfunc(r_pow_scalar, 2, 1) + + +def libm_pow(values: Any, exponent: Any) -> NDArray[np.float64]: + """Elementwise R ``^``: ``x * x`` for 2, else libm ``pow``. NumPy's + vectorised power is not libm's and can differ from it in the last bit.""" + base = np.asarray(values, dtype=np.float64) + power = np.asarray(exponent, dtype=np.float64) + return np.asarray(_libm_pow_ufunc(base, power), dtype=np.float64) diff --git a/src/nns/classical.py b/src/nns/classical.py index 202bca3f..35ba086d 100644 --- a/src/nns/classical.py +++ b/src/nns/classical.py @@ -3,6 +3,8 @@ import numpy as np from numpy.typing import NDArray +from nns._rmean import r_mean +from nns._rnmath import libm_pow, r_pow_scalar from nns.core import _as_1d_values, lpm, upm @@ -58,31 +60,34 @@ def nns_moments(x: NDArray[np.float64], population: bool = True) -> dict[str, fl estimators need n > 2 (skewness) and n > 3 (kurtosis). """ values = _as_1d_values(x) - # float64 scalars give R's IEEE results (0/0 = NaN, x/0 = +-Inf) where plain - # Python floats and ints would raise ZeroDivisionError. + # R 14 NNS.moments: central moments with R's mean() (long double), z^2 as + # z * z and z^3, z^4 through R's ^ (libm pow). float64 scalars give R's IEEE + # results (0/0 = NaN, x/0 = +-Inf) where Python floats would raise. n = np.float64(values.size) - center = float(np.mean(values)) - mean = float(upm(1, 0.0, values) - lpm(1, 0.0, values)) - variance = np.float64(upm(2, center, values) + lpm(2, center, values)) - skew_base = np.float64(upm(3, center, values) - lpm(3, center, values)) - kurt_base = np.float64(upm(4, center, values) + lpm(4, center, values)) + mean = r_mean(values) + z = values - mean + variance = np.float64(r_mean(z * z)) + skew_base = np.float64(r_mean(libm_pow(z, 3.0))) + kurt_base = np.float64(r_mean(libm_pow(z, 4.0))) with np.errstate(divide="ignore", invalid="ignore"): if population: - skewness = float(skew_base / variance**1.5) - kurtosis = float(kurt_base / variance**2 - 3.0) + skewness = float(skew_base / np.float64(r_pow_scalar(float(variance), 1.5))) + kurtosis = float((kurt_base / (variance * variance)) - 3.0) else: # Adjusted Fisher-Pearson G1: n/((n-1)(n-2)) * sum(z^3) / s^3 with s the # sample (n-1) standard deviation, as in the kurtosis branch below. + scaled = variance * (n / (n - 1)) skewness = float( - (n / ((n - 1) * (n - 2))) * ((n * skew_base) / (variance * (n / (n - 1))) ** 1.5) + (n / ((n - 1) * (n - 2))) + * ((n * skew_base) / np.float64(r_pow_scalar(float(scaled), 1.5))) ) kurtosis = float( ((n * (n + 1)) / ((n - 1) * (n - 2) * (n - 3))) - * ((n * kurt_base) / (variance * (n / (n - 1))) ** 2) - - ((3 * ((n - 1) ** 2)) / ((n - 2) * (n - 3))) + * ((n * kurt_base) / (scaled * scaled)) + - ((3 * ((n - 1) * (n - 1))) / ((n - 2) * (n - 3))) ) - variance = variance * (n / (n - 1)) + variance = scaled return { "mean": mean, diff --git a/src/nns/co_moments.py b/src/nns/co_moments.py index 1b88f7a0..c5d73e4f 100644 --- a/src/nns/co_moments.py +++ b/src/nns/co_moments.py @@ -136,7 +136,8 @@ def _co_moment( y_target = y_targets[index % y_targets.size] x_deviation = _deviation(x_side, degree_x, x_target, x_values) y_deviation = _deviation(y_side, degree_y, y_target, y_values) - moments[index] = np.mean(x_deviation * y_deviation) + # R's C++ accumulates x1 * y1 sequentially in double and divides by n. + moments[index] = float(np.cumsum(x_deviation * y_deviation)[-1]) / x_values.size if _is_scalar(target_x) and _is_scalar(target_y): return float(moments[0]) @@ -177,14 +178,19 @@ def _deviation( target: float, values: NDArray[np.float64], ) -> NDArray[np.float64]: + """R's C++ per-observation term: an indicator at degree 0, otherwise the + clamped deviation raised by repeated multiplication (integer degrees) or + libm ``pow``.""" + from nns.dependence import _r_pm_terms + if side is _lower: if degree == 0: return (values <= target).astype(np.float64) - return np.maximum(0.0, target - values) ** degree + return _r_pm_terms(np.maximum(0.0, target - values), degree) if degree == 0: return (values > target).astype(np.float64) - return np.maximum(0.0, values - target) ** degree + return _r_pm_terms(np.maximum(0.0, values - target), degree) _lower = object() diff --git a/src/nns/dependence.py b/src/nns/dependence.py index 895014b1..f89bb509 100644 --- a/src/nns/dependence.py +++ b/src/nns/dependence.py @@ -9,6 +9,7 @@ from nns._helpers import _as_pair from nns._native import native_fn as native_fn +from nns._rnmath import libm_pow @overload @@ -392,9 +393,8 @@ def _dpm_nd( if degree == 0.0: return float(np.mean(discordant)) - else: - values = np.prod(np.abs(diff) ** degree, axis=1) - dpm = float(np.mean(np.where(discordant, values, 0.0))) + values = _r_row_products(_r_pm_terms(np.abs(diff), degree)) + dpm = _rcpp_mean(np.where(discordant, values, 0.0)) if not norm: return dpm @@ -410,10 +410,10 @@ def _clpm_nd(data: NDArray[np.float64], target: NDArray[np.float64], degree: flo if degree == 0.0: return float(np.mean(np.all(diff >= 0.0, axis=1))) valid = np.all(diff >= 0.0, axis=1) - # Clamp before the power: rows with a negative deviation are masked out anyway, - # and a fractional power of a negative number only produced RuntimeWarnings. - powered = np.prod(np.maximum(diff, 0.0) ** degree, axis=1) - return float(np.mean(np.where(valid, powered, 0.0))) + # Clamp before the power: rows with a negative deviation are masked out anyway + # (R stops at the first one), and a fractional power of a negative number is NaN. + powered = _r_row_products(_r_pm_terms(np.maximum(diff, 0.0), degree)) + return _rcpp_mean(np.where(valid, powered, 0.0)) def _cupm_nd(data: NDArray[np.float64], target: NDArray[np.float64], degree: float) -> float: @@ -423,8 +423,35 @@ def _cupm_nd(data: NDArray[np.float64], target: NDArray[np.float64], degree: flo # to the lower side, so CLPM + CUPM + DPM = 1 at degree 0. return float(np.mean(np.all(diff > 0.0, axis=1))) valid = np.all(diff >= 0.0, axis=1) - powered = np.prod(np.maximum(diff, 0.0) ** degree, axis=1) - return float(np.mean(np.where(valid, powered, 0.0))) + powered = _r_row_products(_r_pm_terms(np.maximum(diff, 0.0), degree)) + return _rcpp_mean(np.where(valid, powered, 0.0)) + + +def _r_pm_terms(deviations: NDArray[np.float64], degree: float) -> NDArray[np.float64]: + """R's C++ ``isInteger(degree) ? repeatMultiplication(dev, degree) : pow(dev, + degree)``: integer degrees by repeated multiplication from 1.0, others by libm + ``pow`` (NumPy's vectorised power is not libm's).""" + if degree == float(int(degree)): + terms = np.ones_like(deviations) + for _ in range(int(degree)): + terms = terms * deviations + return terms + return libm_pow(deviations, degree) + + +def _r_row_products(terms: NDArray[np.float64]) -> NDArray[np.float64]: + """Each row's product, accumulated from 1.0 column by column as R's kernel does.""" + product = np.ones(terms.shape[0], dtype=np.float64) + for column in range(terms.shape[1]): + product = product * terms[:, column] + return product + + +def _rcpp_mean(values: NDArray[np.float64]) -> float: + """Rcpp's ``sum(vals) / n``: a plain sequential double sum.""" + if values.size == 0: + return math.nan + return float(np.cumsum(values)[-1]) / values.size def co_lpm_nd( diff --git a/src/nns/pm_matrix.py b/src/nns/pm_matrix.py index 20e4a67e..9fb1ed41 100644 --- a/src/nns/pm_matrix.py +++ b/src/nns/pm_matrix.py @@ -8,6 +8,7 @@ from nns._native import native_fn from nns.core import _as_degree, _require_finite +from nns.dependence import _r_pm_terms Target: TypeAlias = float | Literal["mean"] | NDArray[np.float64] | None PMMatrixResult: TypeAlias = dict[str, Any] @@ -66,34 +67,50 @@ def pm_matrix( result["names"] = resolved_names return result + # R's PMMatrix_CPv: deviation components first, then per-pair sequential + # double sums scaled by 1 / rows (a multiplication), the population + # adjustment, and the upper triangle mirrored (DUPM/DLPM crossed). dev_lower = _lower_deviation(values, targets, lpm_degree) dev_upper = _upper_deviation(values, targets, upm_degree) - clpm = (dev_lower.T @ dev_lower) / observations - cupm = (dev_upper.T @ dev_upper) / observations - dlpm = (dev_upper.T @ dev_lower) / observations - dupm = (dev_lower.T @ dev_upper) / observations + cupm = _sequential_cross(dev_upper, dev_upper) + clpm = _sequential_cross(dev_lower, dev_lower) + dupm = _sequential_cross(dev_lower, dev_upper) + dlpm = _sequential_cross(dev_upper, dev_lower) - adjust = observations / (observations - 1) if observations > 1 else 1.0 - should_adjust = pop_adj and observations > 1 and lpm_degree > 0 and upm_degree > 0 - if should_adjust: - clpm *= adjust + inv_rows = 1.0 / float(observations) + cupm *= inv_rows + clpm *= inv_rows + dupm *= inv_rows + dlpm *= inv_rows + + if pop_adj and observations > 1 and lpm_degree > 0 and upm_degree > 0: + adjust = float(observations) / float(observations - 1) cupm *= adjust - dlpm *= adjust + clpm *= adjust dupm *= adjust + dlpm *= adjust if norm: total = cupm + dupm + dlpm + clpm - np.divide(cupm, total, out=cupm, where=total > 0.0) - np.divide(dupm, total, out=dupm, where=total > 0.0) - np.divide(dlpm, total, out=dlpm, where=total > 0.0) - np.divide(clpm, total, out=clpm, where=total > 0.0) - cupm[total <= 0.0] = 0.0 - dupm[total <= 0.0] = 0.0 - dlpm[total <= 0.0] = 0.0 - clpm[total <= 0.0] = 0.0 + positive = total > 0.0 + safe_total = np.where(positive, total, 1.0) + cupm = np.where(positive, cupm / safe_total, 0.0) + dupm = np.where(positive, dupm / safe_total, 0.0) + dlpm = np.where(positive, dlpm / safe_total, 0.0) + clpm = np.where(positive, clpm / safe_total, 0.0) cov_matrix = cupm + clpm - dupm - dlpm + # R fills only the upper triangle and mirrors it: CUPM, CLPM and the + # covariance plainly, DUPM and DLPM crossed (each from the other's transpose). + upper = np.triu(np.ones(cupm.shape, dtype=bool)) + cupm, clpm, dupm, dlpm, cov_matrix = ( + np.where(upper, cupm, cupm.T), + np.where(upper, clpm, clpm.T), + np.where(upper, dupm, dlpm.T), + np.where(upper, dlpm, dupm.T), + np.where(upper, cov_matrix, cov_matrix.T), + ) result = { "cupm": cupm, "dupm": dupm, @@ -106,6 +123,20 @@ def pm_matrix( return result +def _sequential_cross( + left: NDArray[np.float64], right: NDArray[np.float64] +) -> NDArray[np.float64]: + """``t(left) %*% right`` summed row by row in double, as R's C++ loop does.""" + rows, cols = left.shape + total = np.zeros((cols, cols), dtype=np.float64) + block = max(1, 2_000_000 // max(1, cols * cols)) + for start in range(0, rows, block): + stop = start + block + products = left[start:stop, :, np.newaxis] * right[start:stop, np.newaxis, :] + total = np.cumsum(np.concatenate((total[np.newaxis], products)), axis=0)[-1] + return total + + def _native_matrix(value: Any, dim: int) -> NDArray[np.float64]: matrix = np.asarray(value, dtype=np.float64) if matrix.shape == (dim, dim): @@ -129,7 +160,7 @@ def _lower_deviation( ) -> NDArray[np.float64]: if degree == 0: return (values <= targets[np.newaxis, :]).astype(np.float64) - return np.maximum(0.0, targets[np.newaxis, :] - values) ** degree + return _r_pm_terms(np.maximum(0.0, targets[np.newaxis, :] - values), degree) def _upper_deviation( @@ -139,7 +170,7 @@ def _upper_deviation( ) -> NDArray[np.float64]: if degree == 0: return (values > targets[np.newaxis, :]).astype(np.float64) - return np.maximum(0.0, values - targets[np.newaxis, :]) ** degree + return _r_pm_terms(np.maximum(0.0, values - targets[np.newaxis, :]), degree) def _as_matrix(variable: NDArray[np.float64]) -> NDArray[np.float64]: @@ -155,7 +186,8 @@ def _as_matrix(variable: NDArray[np.float64]) -> NDArray[np.float64]: def _as_target(target: Target, variable: NDArray[np.float64]) -> NDArray[np.float64]: if target is None or isinstance(target, str): - return cast(NDArray[np.float64], np.mean(variable, axis=0)) + # Rcpp sugar colMeans: a sequential double sum divided by the row count. + return cast(NDArray[np.float64], np.cumsum(variable, axis=0)[-1] / variable.shape[0]) targets = np.asarray(target, dtype=np.float64) if targets.ndim == 0: diff --git a/tests/_r_cache/pm14.co_moments.json b/tests/_r_cache/pm14.co_moments.json new file mode 100644 index 00000000..9b0e776d --- /dev/null +++ b/tests/_r_cache/pm14.co_moments.json @@ -0,0 +1,85 @@ +{ + "entries": { + "045823d0608b9d2c1c1bb1de15e5793162d794c42d8a25887a87e6ffad361c41": { + "clpm": [ + 0.01456940905451755, + 0.0005329984844000597, + 0.0, + 0.07706766129617428 + ], + "cupm": [ + 2.098882116877114, + 1.8001685523643745, + 0.22121009362148947, + 1.625415966946254 + ], + "dlpm": [ + 0.00018874827051198253, + 0.6520147667186046, + 6.195142944193032, + 0.0004543069862355346 + ], + "dupm": [ + 1.0790095160453081, + 0.31503120287039876, + 0.0004044399262307224, + 0.8529608826778399 + ] + }, + "544bfcda75ccca613a3fe23d001da9df7c1df4329d8cd59054807e47695f8dc3": { + "clpm": [ + 0.016936198826809357, + 2.7864149718761795e-05, + 0.0, + 0.16905187811248654 + ], + "cupm": [ + 0.5617076038778495, + 1.633300867126027, + 1.2391356116668175, + 0.35481529022166425 + ], + "dlpm": [ + 0.00021125163088095585, + 0.4410084439234831, + 19.09104993555666, + 0.002487498087074593 + ], + "dupm": [ + 0.7045519933105028, + 0.042615137944365784, + 3.943619533765588e-05, + 0.9724393761905308 + ] + }, + "ddc472b920b5793d95a60299439f3d2c1267dcbde32fa3d3576aaf721d676cdc": { + "clpm": [ + 0.003284243649347838, + 4.997899882893342e-07, + 0.0, + 0.04859481649915304 + ], + "cupm": [ + 5.912859216514368, + 19.6229982045289, + 8.567781326920011, + 3.387806143776643 + ], + "dlpm": [ + 4.613289769755108e-07, + 0.5321698549734203, + 191.31254922143364, + 4.423128478021984e-05 + ], + "dupm": [ + 3.2241018887153214, + 0.14365442066661346, + 6.607013638004929e-07, + 4.255326430325899 + ] + } + }, + "label": "pm14.co_moments", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/pm14.moments.json b/tests/_r_cache/pm14.moments.json new file mode 100644 index 00000000..b8ce7a87 --- /dev/null +++ b/tests/_r_cache/pm14.moments.json @@ -0,0 +1,19 @@ +{ + "entries": { + "06d3604569b3ff892fff89a0e73a2832f6944564aaa91c17db5975351421b5d6": { + "kurtosis": 45.39392295952675, + "mean": 2.0236216246521983, + "skewness": 6.194889769455117, + "variance": 21.625328492258852 + }, + "188d826a5c066044beb1a6c0de8e1823ffeba086d4f8840d13ff003e9a84d6a8": { + "kurtosis": 42.344676222488026, + "mean": 2.0236216246521983, + "skewness": 6.070297159023693, + "variance": 21.33699077902873 + } + }, + "label": "pm14.moments", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/pm14.nd.json b/tests/_r_cache/pm14.nd.json new file mode 100644 index 00000000..4a82196a --- /dev/null +++ b/tests/_r_cache/pm14.nd.json @@ -0,0 +1,22 @@ +{ + "entries": { + "27c23a8efd3444c63e937cfb3eb2ed5554505802ed605c9453f8749d60956723": { + "clpm": 1.4435830664618805e-05, + "cupm": 0.5310342867535147, + "dpm": 0.46895127741582054 + }, + "97f4ea92972933853ebb5d5f8dcdb119273812fd162c1040753f20705e96261c": { + "clpm": 0.0002516782538112671, + "cupm": 0.5131942709454023, + "dpm": 0.4865540508007866 + }, + "bae768fab5b3ea927c4b19260979402f3189f64735ea71a01c81266cafea3e26": { + "clpm": 0.00901146230273893, + "cupm": 0.4131882781012734, + "dpm": 0.5778002595959877 + } + }, + "label": "pm14.nd", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/pm14.pm_matrix.json b/tests/_r_cache/pm14.pm_matrix.json new file mode 100644 index 00000000..01e9ebb6 --- /dev/null +++ b/tests/_r_cache/pm14.pm_matrix.json @@ -0,0 +1,535 @@ +{ + "entries": { + "81cf164c3dc4cb90107810ec9c64bf8309ae8e5020b35cabbbd5715ae3a47b6a": { + "clpm": [ + [ + 0.7799760616810789, + 0.24330686265362764, + 0.3461543400153326, + 0.2615329553718589 + ], + [ + 0.24330686265362764, + 0.757546183045615, + 0.23486662779092676, + 0.1517861618385575 + ], + [ + 0.3461543400153326, + 0.23486662779092676, + 0.7011714740801591, + 0.22119433494697036 + ], + [ + 0.2615329553718589, + 0.1517861618385575, + 0.22119433494697036, + 0.8814625036726106 + ] + ], + "cov.matrix": [ + [ + 1.0, + -0.046344302507699664, + 0.09883354325432481, + -0.08902794605289574 + ], + [ + -0.046344302507699664, + 1.0, + -0.10273779247237944, + -0.35965292548430233 + ], + [ + 0.09883354325432481, + -0.10273779247237944, + 1.0, + 0.06097875200296621 + ], + [ + -0.08902794605289574, + -0.35965292548430233, + 0.06097875200296621, + 1.0 + ] + ], + "cupm": [ + [ + 0.2200239383189211, + 0.23352098609252256, + 0.2032624316118298, + 0.19395307160169328 + ], + [ + 0.23352098609252256, + 0.242453816954385, + 0.2137644759728835, + 0.16838737541929133 + ], + [ + 0.2032624316118298, + 0.2137644759728835, + 0.29882852591984094, + 0.3092950410545128 + ], + [ + 0.19395307160169328, + 0.16838737541929133, + 0.3092950410545128, + 0.11853749632738947 + ] + ], + "dlpm": [ + [ + 0.0, + 0.3061940607297825, + 0.19822438198690362, + 0.18840991458755604 + ], + [ + 0.21697809052406736, + 0.0, + 0.30187123906068386, + 0.34272365149910344 + ], + [ + 0.252358846385934, + 0.24949765717550584, + 0.0, + 0.1900840710799378 + ], + [ + 0.35610405843889187, + 0.3371028112430477, + 0.27942655291857915, + 0.0 + ] + ], + "dupm": [ + [ + 0.0, + 0.21697809052406736, + 0.252358846385934, + 0.35610405843889187 + ], + [ + 0.3061940607297825, + 0.0, + 0.24949765717550584, + 0.3371028112430477 + ], + [ + 0.19822438198690362, + 0.30187123906068386, + 0.0, + 0.27942655291857915 + ], + [ + 0.18840991458755604, + 0.34272365149910344, + 0.1900840710799378, + 0.0 + ] + ] + }, + "884af5da73eee2ec11349987de6f5a56f215bca5bab31a443b89c3940e37fdf9": { + "clpm": [ + [ + 0.40831569482041524, + 0.18305630737061307, + 0.1917076875451979, + 0.18359756409235867 + ], + [ + 0.18305630737061307, + 0.38237886818412853, + 0.14640633456085336, + 0.1579840655071809 + ], + [ + 0.1917076875451979, + 0.14640633456085336, + 0.40221447300394686, + 0.18767933216309654 + ], + [ + 0.18359756409235867, + 0.1579840655071809, + 0.18767933216309654, + 0.40840446352089543 + ] + ], + "cov.matrix": [ + [ + 6.274327162293882, + 2.646956512235496, + 0.43130721202654304, + -0.15137917731754802 + ], + [ + 2.646956512235496, + 14.62317179193195, + 0.15242099019808047, + -0.28273650735389494 + ], + [ + 0.43130721202654304, + 0.15242099019808047, + 19.071571237845795, + 0.4578257091148621 + ], + [ + -0.15137917731754802, + -0.28273650735389494, + 0.4578257091148621, + 6.3073215153136255 + ] + ], + "cupm": [ + [ + 5.8660114674734665, + 3.228659686600183, + 0.8685077536993396, + 0.3433685557537646 + ], + [ + 3.228659686600183, + 14.240792923747822, + 0.6535805955668232, + 0.6300261255085675 + ], + [ + 0.8685077536993396, + 0.6535805955668232, + 18.669356764841847, + 1.2014172074143483 + ], + [ + 0.3433685557537646, + 0.6300261255085675, + 1.2014172074143483, + 5.89891705179273 + ] + ], + "dlpm": [ + [ + 0.0, + 0.29576973358709147, + 0.3224052320124651, + 0.3979120166529542 + ], + [ + 0.4689897481482084, + 0.0, + 0.2701788119651994, + 0.6428367584718987 + ], + [ + 0.30650299720552937, + 0.3773871279643968, + 0.0, + 0.5353645256856696 + ], + [ + 0.2804332805107171, + 0.4279099398977447, + 0.3959063047769131, + 0.0 + ] + ], + "dupm": [ + [ + 0.0, + 0.4689897481482084, + 0.30650299720552937, + 0.2804332805107171 + ], + [ + 0.29576973358709147, + 0.0, + 0.3773871279643968, + 0.4279099398977447 + ], + [ + 0.3224052320124651, + 0.2701788119651994, + 0.0, + 0.3959063047769131 + ], + [ + 0.3979120166529542, + 0.6428367584718987, + 0.5353645256856696, + 0.0 + ] + ] + }, + "94c8b99dbbba997add1a42c0ffca34ee03e5ca5c9482380a9ddb5142ac83a856": { + "clpm": [ + [ + 10.727707670350197, + 0.24526385597335032, + 0.7733450253452837, + 0.4327491395237629 + ], + [ + 0.24526385597335032, + 9.377726379660551, + 0.28978750119135777, + 0.20425478077541206 + ], + [ + 0.7733450253452837, + 0.28978750119135777, + 6.043607804652658, + 0.2013243140771067 + ], + [ + 0.4327491395237629, + 0.20425478077541206, + 0.2013243140771067, + 58.62918464103498 + ] + ], + "cov.matrix": [ + [ + 16.593719137823662, + 2.047697163593898, + 0.3313777990943391, + -0.6053968749778094 + ], + [ + 2.047697163593898, + 23.618519303408373, + 0.1954362182623684, + -1.3420928914777712 + ], + [ + 0.3313777990943391, + 0.1954362182623684, + 24.712964569494506, + 0.43743815423166893 + ], + [ + -0.6053968749778094, + -1.3420928914777712, + 0.43743815423166893, + 64.52810169282772 + ] + ], + "cupm": [ + [ + 5.8660114674734665, + 3.228659686600183, + 0.8685077536993396, + 0.3433685557537646 + ], + [ + 3.228659686600183, + 14.240792923747822, + 0.6535805955668232, + 0.6300261255085675 + ], + [ + 0.8685077536993396, + 0.6535805955668232, + 18.669356764841847, + 1.2014172074143483 + ], + [ + 0.3433685557537646, + 0.6300261255085675, + 1.2014172074143483, + 5.89891705179273 + ] + ], + "dlpm": [ + [ + 0.0, + 0.7295199577319148, + 0.4840166652008618, + 0.5016754409645727 + ], + [ + 0.6967064212477208, + 0.0, + 0.46790060938964867, + 1.156702091200362 + ], + [ + 0.8264583147494224, + 0.28003126910616394, + 0.0, + 0.47095099813905056 + ], + [ + 0.8798391292907641, + 1.0196717065613887, + 0.4943523691207354, + 0.0 + ] + ], + "dupm": [ + [ + 0.0, + 0.6967064212477208, + 0.8264583147494224, + 0.8798391292907641 + ], + [ + 0.7295199577319148, + 0.0, + 0.28003126910616394, + 1.0196717065613887 + ], + [ + 0.4840166652008618, + 0.46790060938964867, + 0.0, + 0.4943523691207354 + ], + [ + 0.5016754409645727, + 1.156702091200362, + 0.47095099813905056, + 0.0 + ] + ] + }, + "dac4869d6e58a8a45aafca5f9f22b2caad5e9215eaf0d65a569bba5995b448d1": { + "clpm": [ + [ + 0.5, + 0.2785714285714286, + 0.2785714285714286, + 0.28571428571428575 + ], + [ + 0.2785714285714286, + 0.5071428571428571, + 0.24285714285714285, + 0.2642857142857143 + ], + [ + 0.2785714285714286, + 0.24285714285714285, + 0.5142857142857142, + 0.31428571428571433 + ], + [ + 0.28571428571428575, + 0.2642857142857143, + 0.31428571428571433, + 0.5142857142857142 + ] + ], + "cov.matrix": [ + [ + 1.0, + 0.10000000000000006, + 0.0857142857142858, + 0.11428571428571427 + ], + [ + 0.10000000000000006, + 1.0, + -0.0714285714285714, + 0.014285714285714235 + ], + [ + 0.0857142857142858, + -0.0714285714285714, + 1.0, + 0.20000000000000007 + ], + [ + 0.11428571428571427, + 0.014285714285714235, + 0.20000000000000007, + 1.0 + ] + ], + "cupm": [ + [ + 0.5, + 0.2714285714285714, + 0.2642857142857143, + 0.27142857142857146 + ], + [ + 0.2714285714285714, + 0.4928571428571428, + 0.22142857142857142, + 0.24285714285714285 + ], + [ + 0.2642857142857143, + 0.22142857142857142, + 0.4857142857142857, + 0.28571428571428575 + ], + [ + 0.27142857142857146, + 0.24285714285714285, + 0.28571428571428575, + 0.4857142857142857 + ] + ], + "dlpm": [ + [ + 0.0, + 0.22857142857142856, + 0.2357142857142857, + 0.2285714285714286 + ], + [ + 0.22142857142857142, + 0.0, + 0.2714285714285714, + 0.25 + ], + [ + 0.22142857142857142, + 0.2642857142857143, + 0.0, + 0.2 + ], + [ + 0.2142857142857143, + 0.24285714285714285, + 0.2, + 0.0 + ] + ], + "dupm": [ + [ + 0.0, + 0.22142857142857142, + 0.22142857142857142, + 0.2142857142857143 + ], + [ + 0.22857142857142856, + 0.0, + 0.2642857142857143, + 0.24285714285714285 + ], + [ + 0.2357142857142857, + 0.2714285714285714, + 0.0, + 0.2 + ], + [ + 0.2285714285714286, + 0.25, + 0.2, + 0.0 + ] + ] + } + }, + "label": "pm14.pm_matrix", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/parity/test_partial_moments_bits_r14.py b/tests/parity/test_partial_moments_bits_r14.py new file mode 100644 index 00000000..feba4acc --- /dev/null +++ b/tests/parity/test_partial_moments_bits_r14.py @@ -0,0 +1,127 @@ +"""R 14.0 parity, to the last bit, for the partial-moment family. + +* ``NNS.moments`` is R 14's: ``mean()`` deviations, ``mean(z^3)``/``mean(z^4)`` + with R's ``^`` (libm ``pow``) and the variance raised by ``^1.5``. +* ``Co.LPM``/``Co.UPM``/``D.LPM``/``D.UPM``, the ``_nD`` co-moments and + ``PM.matrix`` follow R's C++: integer degrees by repeated multiplication, + fractional ones by libm ``pow``, double sums accumulated row by row and scaled + by ``1 / n``. ``PM.matrix`` fills the upper triangle and mirrors it (DUPM and + DLPM crossed), and its default target is Rcpp's ``colMeans``. + +Each case runs with the native kernels and with the NumPy fallback. +""" + +from __future__ import annotations + +import importlib +from typing import Any + +import numpy as np +import pytest +from _r import nns_frame_call, r_column + +import nns + +pytestmark = pytest.mark.parity + +co_moments_module = importlib.import_module("nns.co_moments") +pm_matrix_module = importlib.import_module("nns.pm_matrix") + + +def _num(values: Any) -> dict[str, Any]: + return r_column([float(v) for v in np.asarray(values, dtype=np.float64).ravel()], "numeric") + + +@pytest.mark.parametrize(("population", "flag"), [(True, "TRUE"), (False, "FALSE")]) +def test_nns_moments_match_r_bitwise(population: bool, flag: str) -> None: + rng = np.random.default_rng(401) + x = rng.exponential(size=75) ** 2 + expected = nns_frame_call( + "pm14.moments", + f"NNS.moments(x, population = {flag})", + {"x": _num(x)}, + exact=True, + ) + actual = nns.nns_moments(x, population=population) + for key in ("mean", "variance", "skewness", "kurtosis"): + assert actual[key] == expected[key], key + + +@pytest.mark.parametrize("native", [True, False]) +@pytest.mark.parametrize(("degree_x", "degree_y"), [(3, 1), (0.5, 1.5), (2, 0)]) +def test_bivariate_co_moments_match_r_bitwise( + monkeypatch: pytest.MonkeyPatch, native: bool, degree_x: float, degree_y: float +) -> None: + rng = np.random.default_rng(402) + x = rng.normal(size=90) + y = x * x + rng.normal(size=90) * 0.3 + tx, ty = x[:4], y[:4] + expected = nns_frame_call( + "pm14.co_moments", + f"list(clpm = Co.LPM({degree_x}, x, y, tx, ty, degree_y = {degree_y}), " + f"cupm = Co.UPM({degree_x}, x, y, tx, ty, degree_y = {degree_y}), " + f"dlpm = D.LPM({degree_x}, {degree_y}, x, y, tx, ty), " + f"dupm = D.UPM({degree_x}, {degree_y}, x, y, tx, ty))", + {"x": _num(x), "y": _num(y), "tx": _num(tx), "ty": _num(ty)}, + exact=True, + ) + if not native: + monkeypatch.setattr(co_moments_module, "native_fn", lambda name: None) + actual = { + "clpm": nns.co_lpm(degree_x, x, y, tx, ty, degree_y=degree_y), + "cupm": nns.co_upm(degree_x, x, y, tx, ty, degree_y=degree_y), + "dlpm": nns.d_lpm(degree_x, degree_y, x, y, tx, ty), + "dupm": nns.d_upm(degree_x, degree_y, x, y, tx, ty), + } + for key, value in expected.items(): + np.testing.assert_array_equal(np.atleast_1d(actual[key]), np.atleast_1d(value), key) + + +@pytest.mark.parametrize("degree", [2, 0.5, 3]) +def test_nd_co_moments_match_r_bitwise(degree: float) -> None: + rng = np.random.default_rng(403) + data = rng.normal(size=(70, 3)) + target = data[5] + expected = nns_frame_call( + "pm14.nd", + f"X <- matrix(v, ncol = 3); list(clpm = Co.LPM_nD(X, t, degree = {degree}), " + f"cupm = Co.UPM_nD(X, t, degree = {degree}), dpm = DPM_nD(X, t, degree = {degree}))", + {"v": _num(data.ravel(order="F")), "t": _num(target)}, + exact=True, + ) + assert nns.co_lpm_nd(data, target, degree=degree) == expected["clpm"] + assert nns.co_upm_nd(data, target, degree=degree) == expected["cupm"] + assert nns.dpm_nd(data, target, degree=degree) == expected["dpm"] + + +@pytest.mark.parametrize("native", [True, False]) +@pytest.mark.parametrize( + ("lpm_degree", "upm_degree", "pop_adj", "norm"), + [(0.5, 3, True, False), (2, 1, False, True), (0, 0, True, True), (3, 3, True, False)], +) +def test_pm_matrix_matches_r_bitwise( + monkeypatch: pytest.MonkeyPatch, + native: bool, + lpm_degree: float, + upm_degree: float, + pop_adj: bool, + norm: bool, +) -> None: + rng = np.random.default_rng(404) + data = rng.normal(size=(140, 4)) + np.array([0.0, 2.0, -1.0, 5.0]) + expected = nns_frame_call( + "pm14.pm_matrix", + f"lapply(PM.matrix({lpm_degree}, {upm_degree}, 'mean', matrix(v, ncol = 4), " + f"{'TRUE' if pop_adj else 'FALSE'}, norm = {'TRUE' if norm else 'FALSE'}), unname)", + {"v": _num(data.ravel(order="F"))}, + exact=True, + ) + if not native: + monkeypatch.setattr(pm_matrix_module, "native_fn", lambda name: None) + # A Fortran-ordered copy must give the same bits (the default target is a + # row-by-row sum whatever the memory layout). + for layout in (data, np.asfortranarray(data)): + actual = nns.pm_matrix(lpm_degree, upm_degree, "mean", layout, pop_adj, norm=norm) + assert set(expected) == {"cupm", "dupm", "dlpm", "clpm", "cov.matrix"} + for key, rows in expected.items(): + np.testing.assert_array_equal(actual[key], np.asarray(rows, dtype=np.float64), key) From f3b43bd56b9949a3ab537ab4b5e02bb92737a74d Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 01:14:31 +0000 Subject: [PATCH 10/16] Match R in NNS.CDF, NNS.copula, NNS.rescale and many-target LPM Beyond 32 targets R's LPM_CPv/UPM_CPv evaluate (near-)integer degrees with a sorted-anchor prefix backend. The NumPy fallback now ports its recurrence and evaluation order, so LPM, UPM, the ratios and NNS.CDF match R bit for bit for any number of targets. NNS.CDF takes -log with libm, sums the joint survival's marginals with R's long-double rowSums, and its fallback joint CDF follows R's batch kernel (repeated multiplication or libm pow, row-by-row sums scaled by 1 / n). NNS.copula now follows R 14's arithmetic: long-double sum() over the upper triangles in column-major order, long-double prod() for the independence anchors, and mean(...)^(1/2) evaluated with pow rather than sqrt. The old native copula kernel reproduced NumPy-style sums instead, so it is retired; the exact path still runs the native PM.matrix and LPM kernels and costs the same. NNS.rescale(method = "riskneutral") takes the mean in R's single long-double pass, and the minmax path warns on identical values as R does. The live sweep of central tendencies, CDF, dependence, copula, causation and rescale now matches R exactly in all 793 cases, with and without the native extension. New bitwise parity tests cover each fix in both modes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt --- docs/conventions.md | 21 +- src/nns/_nnscore_bindings.cpp | 188 +- src/nns/_rmean.py | 20 + src/nns/_rnmath.py | 21 + src/nns/cdf.py | 49 +- src/nns/central_tendencies.py | 13 +- src/nns/copula.py | 77 +- src/nns/core.py | 93 + tests/_r_cache/cdf14.copula.json | 13 + tests/_r_cache/cdf14.multivariate.json | 267 +++ tests/_r_cache/cdf14.prefix.json | 1983 ++++++++++++++++++++++ tests/_r_cache/cdf14.rescale.json | 173 ++ tests/_r_cache/cdf14.univariate.json | 1861 ++++++++++++++++++++ tests/invariants/test_copula.py | 17 +- tests/parity/test_cdf_copula_bits_r14.py | 155 ++ tests/parity/test_copula.py | 25 +- 16 files changed, 4707 insertions(+), 269 deletions(-) create mode 100644 tests/_r_cache/cdf14.copula.json create mode 100644 tests/_r_cache/cdf14.multivariate.json create mode 100644 tests/_r_cache/cdf14.prefix.json create mode 100644 tests/_r_cache/cdf14.rescale.json create mode 100644 tests/_r_cache/cdf14.univariate.json create mode 100644 tests/parity/test_cdf_copula_bits_r14.py diff --git a/docs/conventions.md b/docs/conventions.md index 8ebe69ed..686b3e9b 100644 --- a/docs/conventions.md +++ b/docs/conventions.md @@ -14,8 +14,9 @@ cached parity tests that way too. ## Threads -The native kernels (vectorised partial moments, the PM matrix, the n-dimensional -co-moments and the copula) can use several threads. Inputs below a fixed amount of +The native kernels (vectorised partial moments, the PM matrix and the +n-dimensional co-moments, which the copula and the CDF build on) can use several +threads. Inputs below a fixed amount of work always run serially, and results never depend on the thread count. `nns.set_num_threads(n)` sets the default; `n=None` restores the automatic choice, which is the `NNS_NUM_THREADS` environment variable when it is a positive integer @@ -96,6 +97,11 @@ crossed), normalising each cell by `CUPM + DUPM + DLPM + CLPM` in that order. `nns_moments` is R 14's `NNS.moments`: deviations from R's `mean()`, `mean(z^3)` and `mean(z^4)` with R's `^`, and the variance raised by `^1.5`. +Beyond 32 targets R's `LPM`/`UPM` (and so `LPM.ratio`, `UPM.ratio` and +`NNS.CDF`) switch to a sorted-anchor prefix backend for degrees within `1e-12` +of an integer up to 32; the NumPy fallback reproduces its recurrence and +evaluation order, so the many-target results are R's bit for bit too. + ## Classical Moment Normalization `mean_pm`, `var_pm`, `skew_pm`, and `kurt_pm` use population normalization by @@ -197,7 +203,10 @@ names R returns. Plotting is ignored. As in the patched R (see [parity](parity.m values are dropped first (complete rows for a matrix), so the CDF reaches 1, and infinite values raise `ValueError`. For `d >= 3` columns the survival, hazard and cumulative hazard use the joint survival `P(X_1 > t_1, ..., X_d > t_d)` by -inclusion-exclusion over all column subsets. +inclusion-exclusion over all column subsets. The values match R to the last +bit: `-log` is libm's, the survival's marginal sum is R's long-double +`rowSums`, and the joint CDF follows R's batch kernel (row-by-row sums scaled +by `1 / n`). ## Dependence @@ -230,7 +239,11 @@ does; `target` gives the per-column targets of the matrix form. When targets are omitted NNS Python uses the column means computed like R's `colMeans` (long-double sequential sum), matching R's `target = NULL` to the last bit. The `target_x` and `target_y` arguments map to R's two-element target vector. Empty or non-finite -input raises `ValueError` (R stops on missing values). +input raises `ValueError` (R stops on missing values). The value is R's to the +last bit: the upper triangles are summed with R's long-double `sum()` in +column-major order, the independence anchors use R's long-double `prod()`, and +the result is `mean(c(...))^(1/2)`, which R evaluates with `pow` rather than +`sqrt`. ## Causation diff --git a/src/nns/_nnscore_bindings.cpp b/src/nns/_nnscore_bindings.cpp index 4826ea09..4d7619c2 100644 --- a/src/nns/_nnscore_bindings.cpp +++ b/src/nns/_nnscore_bindings.cpp @@ -441,38 +441,7 @@ nb::dict nns_part_off(const Vector& xa, const Vector& ya, bool xonly, int max_or return out; } -// --- NumPy-faithful sums and the NNS.dep partition ------------------------- -// np_pairwise_sum matches numpy's pairwise summation, so means/sums used as -// comparison thresholds by the copula port below are bit-identical to -// np.mean/np.sum. - -double np_pairwise_sum(const double* a, std::size_t n) { - if (n < 8U) { - double res = 0.0; - for (std::size_t i = 0; i < n; ++i) res += a[i]; - return res; - } - if (n <= 128U) { - double r0 = a[0], r1 = a[1], r2 = a[2], r3 = a[3]; - double r4 = a[4], r5 = a[5], r6 = a[6], r7 = a[7]; - const std::size_t limit = n - (n % 8U); - std::size_t i = 8; - for (; i < limit; i += 8U) { - r0 += a[i + 0]; r1 += a[i + 1]; r2 += a[i + 2]; r3 += a[i + 3]; - r4 += a[i + 4]; r5 += a[i + 5]; r6 += a[i + 6]; r7 += a[i + 7]; - } - double res = ((r0 + r1) + (r2 + r3)) + ((r4 + r5) + (r6 + r7)); - for (; i < n; ++i) res += a[i]; - return res; - } - std::size_t n2 = n / 2U; - n2 -= n2 % 8U; - return np_pairwise_sum(a, n2) + np_pairwise_sum(a + n2, n - n2); -} - -double np_mean(const double* a, std::size_t n) { return np_pairwise_sum(a, n) / static_cast(n); } - -double clamp01(double v) { return std::min(std::max(v, 0.0), 1.0); } +// --- The NNS.dep partition -------------------------------------------------- std::vector dep_xonly_partition(const double* x, std::size_t n, int obs_req) { int max_order = 0; @@ -531,148 +500,6 @@ nb::dict nns_dep_pair(const Vector& xa, const Vector& ya, bool asym) { return out; } -// --- NNS.copula faithful port (nns.copula._copula) -------------------------- -// General-d n-dimensional partial moments mirroring the pure-Python -// _clpm_nd/_cupm_nd/_dpm_nd, with numpy-faithful pairwise means. data is -// column-major n x d; target is the d-vector of per-column targets supplied by -// the caller (np.mean), so it is already bit-identical. pm covariance matrices -// reuse the verified nns::pm_matrix kernel. - -double nd_clpm_general(const double* data, std::size_t n, std::size_t d, const double* target, - double degree) { - std::vector v(n); - for (std::size_t i = 0; i < n; ++i) { - bool valid = true; - double prod = 1.0; - for (std::size_t c = 0; c < d; ++c) { - const double cd = target[c] - data[i + c * n]; - if (cd < 0.0) valid = false; - prod *= cd; - } - v[i] = (degree == 0.0) ? (valid ? 1.0 : 0.0) : (valid ? prod : 0.0); - } - return np_mean(v.data(), n); -} - -double nd_cupm_general(const double* data, std::size_t n, std::size_t d, const double* target, - double degree) { - std::vector v(n); - for (std::size_t i = 0; i < n; ++i) { - bool valid = true; - double prod = 1.0; - for (std::size_t c = 0; c < d; ++c) { - const double dd = data[i + c * n] - target[c]; - if (dd < 0.0) valid = false; - prod *= dd; - } - v[i] = (degree == 0.0) ? (valid ? 1.0 : 0.0) : (valid ? prod : 0.0); - } - return np_mean(v.data(), n); -} - -double nd_dpm_general(const double* data, std::size_t n, std::size_t d, const double* target, - double degree, bool norm) { - std::vector v(n); - for (std::size_t i = 0; i < n; ++i) { - bool below = true, above = true; - double prod_abs = 1.0; - for (std::size_t c = 0; c < d; ++c) { - const double diff = data[i + c * n] - target[c]; - // Degree 0 partitions the sample like CLPM (all <= target) and CUPM (all > - // target), as R's DPM_nD; at degree 1 a tied coordinate adds a - // zero factor either way. - if (!(diff <= 0.0)) below = false; - if (!(diff > 0.0)) above = false; - prod_abs *= std::abs(diff); - } - const bool discordant = !(below || above); - v[i] = (degree == 0.0) ? (discordant ? 1.0 : 0.0) : (discordant ? prod_abs : 0.0); - } - const double dpm = np_mean(v.data(), n); - if (degree == 0.0) return dpm; // Python returns mean(discordant) before norm - if (!norm) return dpm; - const double clpm = nd_clpm_general(data, n, d, target, 1.0); - const double cupm = nd_cupm_general(data, n, d, target, 1.0); - const double total = clpm + cupm + dpm; - return total > 0.0 ? dpm / total : 0.0; -} - -double upper_tri_pairwise(const std::vector& m, std::size_t d) { - std::vector vals; - vals.reserve(d * (d - 1U) / 2U); - for (std::size_t i = 0; i < d; ++i) - for (std::size_t j = i + 1U; j < d; ++j) vals.push_back(m[i + j * d]); - return np_pairwise_sum(vals.data(), vals.size()); -} - -double copula_nd(const double* data, std::size_t n, std::size_t d, const double* target, - bool continuous) { - const nns::PMMatrixResult disc = nns::pm_matrix(0.0, 0.0, target, data, n, d, false, false, call_threads()); - const double disc_co = upper_tri_pairwise(disc.cupm, d) + upper_tri_pairwise(disc.clpm, d); - // Each margin's lower-tail share at its target, also the independence anchors. - std::vector p0(d), p1(d); - for (std::size_t c = 0; c < d; ++c) { - const double* col = data + c * n; - double below = 0.0, lower = 0.0, upper = 0.0; - for (std::size_t i = 0; i < n; ++i) { - const double diff = target[c] - col[i]; - if (diff >= 0.0) below += 1.0; - if (diff > 0.0) lower += diff; else upper -= diff; - } - p0[c] = below / static_cast(n); - p1[c] = lower / (lower + upper); - } - // With every margin on one side of its target each co-partial moment equals its - // independence product, and every pair is trivially concordant or discordant. - std::size_t one_sided = 0; - for (double p : p0) one_sided += (p == 0.0 || p == 1.0) ? 1U : 0U; - if (one_sided == d) return 0.0; - // Perfect dependence: every pair fully concordant (1) or fully discordant (0). - bool perfect = one_sided == 0; - for (std::size_t i = 0; i < d && perfect; ++i) { - for (std::size_t j = i + 1U; j < d; ++j) { - const double pair = disc.cupm[i + j * d] + disc.clpm[i + j * d]; - if (!(std::abs(pair - 1.0) < 1e-9 || std::abs(pair) < 1e-9)) { - perfect = false; - break; - } - } - } - if (perfect) return 1.0; - const double disc_d = nd_dpm_general(data, n, d, target, 0.0, true); - - double cont_co, cont_d; - if (continuous) { - const nns::PMMatrixResult cont = nns::pm_matrix(1.0, 1.0, target, data, n, d, true, true, call_threads()); - cont_co = upper_tri_pairwise(cont.cupm, d) + upper_tri_pairwise(cont.clpm, d); - cont_d = nd_dpm_general(data, n, d, target, 1.0, true); - } else { - cont_co = disc_co; - cont_d = disc_d; - } - - const auto co_anchor = [d](const std::vector& p) { - std::vector terms; - for (std::size_t j = 1; j < d; ++j) - for (std::size_t i = 0; i < j; ++i) terms.push_back(p[i] * p[j] + (1.0 - p[i]) * (1.0 - p[j])); - return np_pairwise_sum(terms.data(), terms.size()); - }; - const auto d_anchor = [](const std::vector& p) { - double lo = 1.0, hi = 1.0; - for (double v : p) { lo *= v; hi *= 1.0 - v; } - return 1.0 - lo - hi; - }; - const auto rel_dev = [](double obs, double indep) { - return obs == indep ? 0.0 : std::abs(obs - indep) / indep; - }; - const std::vector& pc = continuous ? p1 : p0; - const double discrete_dep = clamp01(rel_dev(disc_co, co_anchor(p0))); - const double continuous_dep = clamp01(rel_dev(cont_co, co_anchor(pc))); - const double nd_disc = std::min(rel_dev(disc_d, d_anchor(p0)), 1.0); - const double nd_cont = std::min(rel_dev(cont_d, d_anchor(pc)), 1.0); - return std::sqrt((discrete_dep + continuous_dep + nd_disc + nd_cont) / 4.0); -} - // --- NNS.M.reg prediction kernels (NNS-core ports of R 13.3) ---------------- // Matrices are passed flattened column-major (element (i, j) at i + j * rows). // NNS-core's mreg_setup and xstar_path are not bound: the NumPy partition, RPM @@ -890,17 +717,4 @@ NB_MODULE(_nnscore, m) { m.attr("nns_dep_pair_r14") = m.attr("nns_dep_pair"); m.attr("r14_source_commit") = "7b07a837c130ba7220dbe16c584221508757d5ae"; - m.def( - "copula_nd", - [](const Vector& data, std::size_t n, std::size_t d, const Vector& target, bool continuous) { - if (data.shape(0) != n * d) { - throw std::invalid_argument("data length must equal n * d."); - } - if (target.shape(0) != d) { - throw std::invalid_argument("target length must equal d."); - } - nb::gil_scoped_release release; - return copula_nd(data.data(), n, d, target.data(), continuous); - }, - nb::arg("data"), nb::arg("n"), nb::arg("d"), nb::arg("target"), nb::arg("continuous")); } diff --git a/src/nns/_rmean.py b/src/nns/_rmean.py index 84ed5583..93a7c15e 100644 --- a/src/nns/_rmean.py +++ b/src/nns/_rmean.py @@ -36,6 +36,16 @@ def r_sum(values: NDArray[np.float64]) -> float: return float(np.cumsum(v.astype(np.longdouble))[-1] + 0.0) +def r_prod(values: NDArray[np.float64]) -> float: + """R's ``prod()`` of a double vector (summary.c ``rprod``): one sequential + long-double product from 1.0, rounded to double (overflow to +-Inf).""" + v = np.asarray(values, dtype=np.float64).ravel() + product = np.longdouble(1.0) + for value in v: + product = product * np.longdouble(value) + return float(product) + + def r_mean_no_na(values: NDArray[np.float64]) -> float: """NNS.part's C++ ``mean_no_na``: long-double sum of the non-NaN values divided by their count (single pass).""" @@ -155,6 +165,16 @@ def r_row_means(x: NDArray[np.float64]) -> NDArray[np.float64]: return np.asarray(sums / m.shape[1], dtype=np.float64) +def r_row_sums(x: NDArray[np.float64]) -> NDArray[np.float64]: + """R ``rowSums(x)`` (array.c ``do_colsum``): each row accumulated column by + column in long double, rounded to double.""" + m = np.asarray(x, dtype=np.float64) + sums = np.zeros(m.shape[0], dtype=np.longdouble) + for j in range(m.shape[1]): + sums = sums + m[:, j].astype(np.longdouble) + return np.asarray(sums, dtype=np.float64) + + def r_seq_by(start: float, stop: float, by: float) -> NDArray[np.float64]: """R ``seq(from, to, by)`` for double arguments (base ``seq.default``). diff --git a/src/nns/_rnmath.py b/src/nns/_rnmath.py index da98f24d..b7ad410b 100644 --- a/src/nns/_rnmath.py +++ b/src/nns/_rnmath.py @@ -612,6 +612,27 @@ def libm_exp(values: Any) -> NDArray[np.float64]: return np.asarray(_libm_exp_ufunc(np.asarray(values, dtype=np.float64)), dtype=np.float64) +def _c_log(x: float) -> float: + """C ``log`` (R's ``R_log``): ``-inf`` at 0 and NaN below, where ``math.log`` raises.""" + if x > 0.0: + return math.log(x) + if x == 0.0: + return -math.inf + return math.nan + + +_libm_log_ufunc = np.frompyfunc(_c_log, 1, 1) + + +def libm_log(values: Any) -> NDArray[np.float64]: + """Elementwise libm ``log``, as R evaluates ``log()`` on a vector. + + NumPy's vectorised ``log`` is not libm's on SIMD builds and can differ from + it in the last bit. + """ + return np.asarray(_libm_log_ufunc(np.asarray(values, dtype=np.float64)), dtype=np.float64) + + def r_pow_scalar(x: float, y: float) -> float: """R's ``x ^ y`` (arithmetic.c ``R_POW``/``R_pow``) for doubles: ``x * x`` for 2, 1 for a base of 1 or an exponent of 0, R's zero-base rules, else diff --git a/src/nns/cdf.py b/src/nns/cdf.py index c0701bf2..53944641 100644 --- a/src/nns/cdf.py +++ b/src/nns/cdf.py @@ -9,8 +9,10 @@ from numpy.typing import NDArray from nns._native import native_fn +from nns._rmean import r_row_sums +from nns._rnmath import libm_log from nns.core import lpm_ratio -from nns.dependence import co_lpm_nd +from nns.dependence import _r_pm_terms, co_lpm_nd from nns.regression import RegResult, nns_reg if TYPE_CHECKING: @@ -107,7 +109,7 @@ def _univariate_cdf( 1e6, ) elif type_value == "cumulative hazard": - y = np.maximum(-np.log(np.maximum(1.0 - pval, 1e-10)), 0.0) + y = np.maximum(-libm_log(np.maximum(1.0 - pval, 1e-10)), 0.0) if target_value is None: pv = np.array([], dtype=np.float64) @@ -122,7 +124,7 @@ def _univariate_cdf( point = np.asarray(fit["Point.est"], dtype=np.float64).reshape(-1) pv = np.minimum(np.maximum(point / np.maximum(1.0 - pv, 1e-10), 0.0), 1e6) elif type_value == "cumulative hazard": - pv = np.maximum(-np.log(np.maximum(1.0 - pv, 1e-10)), 0.0) + pv = np.maximum(-libm_log(np.maximum(1.0 - pv, 1e-10)), 0.0) return {"Function": {"x": x, column_name: y}, "target.value": np.asarray(pv, dtype=np.float64)} @@ -168,7 +170,7 @@ def _multivariate_cdf( fitted = cast(dict[str, NDArray[np.float64]], fit["Fitted.xy"]) cdf = np.maximum(fitted["y.hat"] / np.maximum(survival, 1e-10), 0.0) elif type_value == "cumulative hazard": - cdf = np.maximum(-np.log(np.maximum(survival, 1e-10)), 0.0) + cdf = np.maximum(-libm_log(np.maximum(survival, 1e-10)), 0.0) # The target value is the same function as the rows, evaluated at target. pv = np.array([], dtype=np.float64) @@ -296,28 +298,44 @@ def _co_lpm_nd_batch( return np.asarray( native(flat, n, d, flat_points, m, float(degree), True), dtype=np.float64 ) + # NumPy fallback reproducing R's CoLPMnDBatchWorker: per-row products of + # R's lower/upper components (repeated multiplication or libm pow), double + # sums accumulated row by row, scaled by 1 / n, then normalised. out = np.empty(m, dtype=np.float64) - # Pure-NumPy fallback, chunked so memory stays O(chunk * n * d). chunk = max(1, 2_000_000 // max(1, n * d)) + inv_n = 1.0 / float(n) for start in range(0, m, chunk): rows = points[start : start + chunk] diff = values[np.newaxis, :, :] - rows[:, np.newaxis, :] - lower_mask = np.all(diff <= 0.0, axis=2) if degree == 0.0: - out[start : start + chunk] = np.mean(lower_mask, axis=1) + below = np.all(diff <= 0.0, axis=2) + out[start : start + chunk] = np.count_nonzero(below, axis=1) / float(n) continue - upper_mask = np.all(diff >= 0.0, axis=2) - abs_pow = np.prod(np.abs(diff) ** degree, axis=2) - clpm = np.mean(np.where(lower_mask, abs_pow, 0.0), axis=1) - cupm = np.mean(np.where(upper_mask, abs_pow, 0.0), axis=1) - dpm = np.mean(np.where(~(lower_mask | upper_mask), abs_pow, 0.0), axis=1) + lower_terms = _r_pm_terms(np.maximum(-diff, 0.0), degree) + upper_terms = _r_pm_terms(np.maximum(diff, 0.0), degree) + lower_prod = _r_axis2_products(np.where(-diff < 0.0, 0.0, lower_terms)) + upper_prod = _r_axis2_products(np.where(diff < 0.0, 0.0, upper_terms)) + dpm_prod = _r_axis2_products(_r_pm_terms(np.abs(diff), degree)) + strict = np.all(diff < 0.0, axis=2) | np.all(diff > 0.0, axis=2) + clpm = np.cumsum(lower_prod, axis=1)[:, -1] * inv_n + cupm = np.cumsum(upper_prod, axis=1)[:, -1] * inv_n + dpm = np.cumsum(np.where(strict, 0.0, dpm_prod), axis=1)[:, -1] * inv_n total = clpm + cupm + dpm - out[start : start + chunk] = np.divide( - clpm, total, out=np.zeros_like(clpm), where=total > 0.0 + positive = total > 0.0 + out[start : start + chunk] = np.where( + positive, clpm / np.where(positive, total, 1.0), 0.0 ) return out +def _r_axis2_products(terms: NDArray[np.float64]) -> NDArray[np.float64]: + """Products over the last axis, multiplied in order from 1.0 as R's loop does.""" + product = np.ones(terms.shape[:2], dtype=np.float64) + for column in range(terms.shape[2]): + product = product * terms[:, :, column] + return product + + def _joint_survival( values: NDArray[np.float64], points: NDArray[np.float64], @@ -344,7 +362,8 @@ def _joint_survival( marginals = np.column_stack( [_r_lpm_ratio(degree, points[:, col], values[:, col]) for col in range(d)] ) - survival = 1.0 - np.sum(marginals, axis=1) + # R: 1 - rowSums(marginals), a long-double row sum. + survival = 1.0 - r_row_sums(marginals) for k in range(2, d): sign = -1.0 if k % 2 else 1.0 for subset in itertools.combinations(range(d), k): diff --git a/src/nns/central_tendencies.py b/src/nns/central_tendencies.py index b9beb104..258fa874 100644 --- a/src/nns/central_tendencies.py +++ b/src/nns/central_tendencies.py @@ -1,6 +1,7 @@ from __future__ import annotations import math +import warnings import numpy as np from numpy.typing import NDArray @@ -29,12 +30,11 @@ def nns_rescale( if method_l == "minmax": finite = values[np.isfinite(values)] - if finite.size == 0: + if finite.size == 0 or float(np.min(finite)) == float(np.max(finite)): + warnings.warn("All x identical: returning midpoint values", UserWarning, stacklevel=2) return np.full(values.shape, (a + b) / 2.0, dtype=np.float64) xmin = float(np.min(finite)) xmax = float(np.max(finite)) - if xmax == xmin: - return np.full(values.shape, (a + b) / 2.0, dtype=np.float64) return a + (b - a) * ((values - xmin) / (xmax - xmin)) if method_l == "riskneutral": @@ -43,7 +43,12 @@ def nns_rescale( if not a > 0.0: raise ValueError("S_0 (a) must be positive for riskneutral method.") finite = values[np.isfinite(values)] - mean_x = float(np.mean(finite)) if finite.size else float("nan") + # R's C++: one long-double pass over the finite values, divided by the count. + mean_x = ( + float(np.cumsum(finite.astype(np.longdouble))[-1] / finite.size) + if finite.size + else float("nan") + ) if not np.isfinite(mean_x) or mean_x <= 0.0: raise ValueError("Mean(x) must be positive/finite for riskneutral scaling.") target = a if type_l == "discounted" else a * math.exp(b * time_to_maturity) diff --git a/src/nns/copula.py b/src/nns/copula.py index 91d74f4c..926001af 100644 --- a/src/nns/copula.py +++ b/src/nns/copula.py @@ -1,6 +1,5 @@ from __future__ import annotations -import math import warnings from collections.abc import Sequence @@ -8,7 +7,8 @@ from numpy.typing import NDArray from nns._helpers import _as_pair -from nns._native import native_fn +from nns._rmean import r_mean, r_prod, r_sum +from nns._rnmath import r_pow_scalar from nns.core import lpm, lpm_ratio from nns.dependence import _dpm_nd from nns.pm_matrix import pm_matrix @@ -70,8 +70,14 @@ def _copula( target: NDArray[np.float64], continuous: bool, ) -> float: + """R 14's ``NNS.copula`` in R's own arithmetic. + + R sums the upper triangles with ``sum()`` (long double, in column-major + order), takes the independence products with ``prod()`` (long double), and + returns ``mean(c(...))^(1/2)``: R's two-pass mean raised by ``pow``, not + ``sqrt``. The partial-moment matrices and nD moments are the exact kernels. + """ if values.ndim != 2 or values.shape[0] == 0: - # The native kernel assumes at least one observation. raise ValueError("copula input must be non-empty.") # A constant margin is independent of the rest and has no degree-1 lower share. vary = np.any(values != values[0], axis=0) @@ -80,30 +86,20 @@ def _copula( return 0.0 values = np.ascontiguousarray(values[:, vary]) target = target[vary] - native_copula = native_fn("copula_nd") - if native_copula is not None: - return float( - native_copula( - np.ascontiguousarray(np.ravel(values, order="F")), - values.shape[0], - values.shape[1], - np.ascontiguousarray(target), - bool(continuous), - ) - ) n = values.shape[1] - upper = np.triu_indices(n, k=1) + # R's m[upper.tri(m)] lists the strict upper triangle column by column. + rows = np.array([i for j in range(n) for i in range(j)], dtype=np.intp) + cols = np.array([j for j in range(n) for _ in range(j)], dtype=np.intp) + upper = (rows, cols) discrete_pm_cov = pm_matrix(0.0, 0.0, target, values, pop_adj=False) - discrete_co_pm = float( - discrete_pm_cov["cupm"][upper].sum() + discrete_pm_cov["clpm"][upper].sum() - ) + discrete_co_pm = r_sum(discrete_pm_cov["cupm"][upper]) + r_sum(discrete_pm_cov["clpm"][upper]) # Perfect dependence: every pair fully concordant (1) or fully discordant (0). # A margin with every observation on one side of its target makes that trivially true. - p0 = np.array([float(lpm(0.0, target[j], values[:, j])) for j in range(n)]) - # Every co-partial moment then equals its independence product. - one_sided = (p0 == 0.0) | (p0 == 1.0) + lower_share = np.array([float(lpm(0.0, target[j], values[:, j])) for j in range(n)]) + # Normalized region shares have no informative contrast at this target. + one_sided = (lower_share == 0.0) | (lower_share == 1.0) if np.all(one_sided): return 0.0 pair_co_pm = discrete_pm_cov["cupm"][upper] + discrete_pm_cov["clpm"][upper] @@ -112,51 +108,50 @@ def _copula( ): return 1.0 - discrete_d_pm = _dpm_nd(values, target, 0.0, norm=True) - if continuous: continuous_pm_cov = pm_matrix(1.0, 1.0, target, values, pop_adj=True, norm=True) - continuous_co_pm = float( - continuous_pm_cov["cupm"][upper].sum() + continuous_pm_cov["clpm"][upper].sum() + continuous_co_pm = r_sum(continuous_pm_cov["cupm"][upper]) + r_sum( + continuous_pm_cov["clpm"][upper] ) - continuous_d_pm = _dpm_nd(values, target, 1.0, norm=True) else: continuous_co_pm = discrete_co_pm - continuous_d_pm = discrete_d_pm # Independence anchors from each margin's own lower-tail share at its target. - p1 = ( - np.array([float(lpm_ratio(1.0, target[j], values[:, j])) for j in range(n)]) - if continuous - else p0 - ) - discrete_co_anchor, discrete_d_anchor = _independence_anchors(p0, upper) - continuous_co_anchor, continuous_d_anchor = _independence_anchors(p1, upper) + discrete_co_anchor, discrete_d_anchor = _independence_anchors(lower_share, upper) + if continuous: + ratios = np.array([float(lpm_ratio(1.0, target[j], values[:, j])) for j in range(n)]) + continuous_co_anchor, continuous_d_anchor = _independence_anchors(ratios, upper) + else: + continuous_co_anchor, continuous_d_anchor = discrete_co_anchor, discrete_d_anchor + discrete_dep = min(max(_relative_deviation(discrete_co_pm, discrete_co_anchor), 0.0), 1.0) continuous_dep = min( max(_relative_deviation(continuous_co_pm, continuous_co_anchor), 0.0), 1.0 ) + + discrete_d_pm = _dpm_nd(values, target, 0.0, norm=True) + continuous_d_pm = _dpm_nd(values, target, 1.0, norm=True) if continuous else discrete_d_pm n_dim_discrete_dep = min(_relative_deviation(discrete_d_pm, discrete_d_anchor), 1.0) n_dim_continuous_dep = min(_relative_deviation(continuous_d_pm, continuous_d_anchor), 1.0) - return math.sqrt( - (discrete_dep + continuous_dep + n_dim_discrete_dep + n_dim_continuous_dep) / 4.0 - ) + parts = np.array([discrete_dep, continuous_dep, n_dim_discrete_dep, n_dim_continuous_dep]) + return r_pow_scalar(r_mean(parts), 0.5) def _independence_anchors( p: NDArray[np.float64], upper: tuple[NDArray[np.intp], NDArray[np.intp]] ) -> tuple[float, float]: - """Pairwise co-moment sum and nD divergent share under independence (R NNS.copula).""" - co = float(np.sum((np.outer(p, p) + np.outer(1.0 - p, 1.0 - p))[upper])) - return co, 1.0 - float(np.prod(p)) - float(np.prod(1.0 - p)) + """R's ``indep_anchors``: ``sum((outer(p, p) + outer(1 - p, 1 - p))[utr])`` and + ``1 - prod(p) - prod(1 - p)``, with R's long-double ``sum``/``prod``.""" + co = r_sum((np.outer(p, p) + np.outer(1.0 - p, 1.0 - p))[upper]) + return co, 1.0 - r_prod(p) - r_prod(1.0 - p) def _relative_deviation(observed: float, anchor: float) -> float: # A boundary target can zero an anchor; an observed 0 then matches it exactly. if observed == anchor: return 0.0 - with np.errstate(divide="ignore"): + with np.errstate(divide="ignore", invalid="ignore"): return float(np.divide(abs(observed - anchor), anchor)) diff --git a/src/nns/core.py b/src/nns/core.py index 451f0946..5693ffb1 100644 --- a/src/nns/core.py +++ b/src/nns/core.py @@ -123,6 +123,14 @@ def _moment( native_result = native(degree, native_target, np.ascontiguousarray(values)) return _result_for_target(np.asarray(native_result, dtype=np.float64).reshape(-1), target) + if targets.size > _DIRECT_PATH_MAX_TARGETS: + # R's LPM_CPv/UPM_CPv switch to the sorted-anchor prefix backend beyond + # 32 targets for (near-)integer degrees up to 32. + degree_int = _prefix_degree(degree) + if degree_int is not None: + prefix = _prefix_moments(values, targets, degree_int, lower) + return _result_for_target(prefix, target) + moments = np.empty(targets.size, dtype=np.float64) for start in range(0, targets.size, _TARGET_BLOCK): grid = targets[start : start + _TARGET_BLOCK, np.newaxis] @@ -159,6 +167,91 @@ def _c_components( _libm_pow = np.frompyfunc(math.pow, 2, 1) +# R's LPM_CPv/UPM_CPv scan each target directly up to this many targets. +_DIRECT_PATH_MAX_TARGETS = 32 +_PREFIX_MAX_DEGREE = 32 + + +def _prefix_degree(degree: float) -> int | None: + """R's ``prefix_supported_degree``: a degree within 1e-12 of an integer <= 32.""" + if not math.isfinite(degree) or degree < 0.0: + return None + rounded = math.floor(degree + 0.5) # std::round on a non-negative value + if abs(degree - rounded) > 1e-12 or rounded > _PREFIX_MAX_DEGREE: + return None + return int(rounded) + + +def _prefix_binomials(degree: int) -> list[list[float]]: + width = degree + 1 + binom = [[0.0] * width for _ in range(width)] + for j in range(width): + binom[j][0] = 1.0 + for k in range(1, j + 1): + binom[j][k] = binom[j - 1][k - 1] + (binom[j - 1][k] if k < j else 0.0) + return binom + + +def _prefix_table( + ordered: list[float], degree: int, binom: list[list[float]], lower: bool +) -> NDArray[np.float64]: + """R's ``PrefixPartialMomentBackend`` lower or upper table, ``(n + 1) x (degree + 1)``. + + Row ``k`` holds the power sums anchored at the adjacent order statistic, + built with R's recurrence ``next_j = [j == 0] + sum_l C(j, l) delta^(j - l) + prev_l`` in R's operation order (zero terms skipped, as R does). + """ + n = len(ordered) + width = degree + 1 + table = [[0.0] * width for _ in range(n + 1)] + steps = range(1, n) if lower else range(n - 1, 0, -1) + start = 1 if lower else n - 1 + table[start][0] = 1.0 + for k in steps: + delta = ordered[k] - ordered[k - 1] + powers = [1.0] * width + for p in range(1, width): + powers[p] = powers[p - 1] * delta + prev = table[k] + nxt = table[k + 1] if lower else table[k - 1] + for j in range(width): + acc = 1.0 if j == 0 else 0.0 + row = binom[j] + for m in range(j + 1): + if prev[m] != 0.0: + acc += row[m] * powers[j - m] * prev[m] + nxt[j] = acc + return np.asarray(table, dtype=np.float64) + + +def _prefix_moments( + values: NDArray[np.float64], targets: NDArray[np.float64], degree: int, lower: bool +) -> NDArray[np.float64]: + """R's prefix-backend ``lpm(t)`` / ``upm(t)`` for every target.""" + ordered = np.sort(values) + n = ordered.size + count = np.searchsorted(ordered, targets, side="right") + if degree == 0: + hits = count if lower else n - count + return np.asarray(hits / float(n), dtype=np.float64) + binom = _prefix_binomials(degree) + table = _prefix_table(ordered.tolist(), degree, binom, lower) + if lower: + empty = count == 0 + gap = targets - ordered[np.maximum(count - 1, 0)] + else: + empty = count == n + gap = ordered[np.minimum(count, n - 1)] - targets + sums = table[count] + out = np.zeros(targets.size, dtype=np.float64) + gap_power = np.ones(targets.size, dtype=np.float64) + with np.errstate(over="ignore", invalid="ignore"): + for j in range(degree, -1, -1): + column = sums[:, j] + out = np.where(column != 0.0, out + binom[degree][j] * gap_power * column, out) + gap_power = gap_power * gap + return np.asarray(np.where(empty, 0.0, out / float(n)), dtype=np.float64) + def _ratio( degree: float, diff --git a/tests/_r_cache/cdf14.copula.json b/tests/_r_cache/cdf14.copula.json new file mode 100644 index 00000000..1950b9af --- /dev/null +++ b/tests/_r_cache/cdf14.copula.json @@ -0,0 +1,13 @@ +{ + "entries": { + "4438d814c8a8f71cb09ba9d5b075983ec3129851e445e75eb828b09f9e8a034f": 0.6320474499096732, + "8afba8db0c4a9a85828f8abd5ba2ec5226d9fd89b0033eab36aec50be5c29028": 0.6776768746360905, + "d719e249ac7668b1ac51ad0bdd75606b745039d48d0529acbcee6db5e76e91e2": 0.5300824697124554, + "e7e5597a3b55a5b3f05e390445676a7b6be054d6db32f7611391a2104595ef29": 0.48876291165943353, + "f673a06f5aa4ea5a9760aaede58dbc919a3c35161adaf6cd741157c8d1b6291f": 0.4748336459636513, + "fb1cf6641f328dff726155398a2d53ac15a77df7592e6a5c66ffef256eb04b77": 0.5551536134738801 + }, + "label": "cdf14.copula", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/cdf14.multivariate.json b/tests/_r_cache/cdf14.multivariate.json new file mode 100644 index 00000000..350a4395 --- /dev/null +++ b/tests/_r_cache/cdf14.multivariate.json @@ -0,0 +1,267 @@ +{ + "entries": { + "2e18c8823d3e6fd20d1b7e99640bc793c0ac3c71050f742159f83864b5e9a71a": { + "cdf": [ + 0.28333333333333327, + 0.15000000000000002, + 0.016666666666666774, + 5.551115123125783e-17, + 0.0, + 0.23333333333333323, + 0.18333333333333332, + 0.25, + 0.0333333333333333, + 0.05000000000000006, + 5.551115123125783e-17, + 0.08333333333333322, + 1.5265566588595902e-16, + 0.033333333333333354, + 0.1333333333333333, + 0.03333333333333334, + 0.39999999999999997, + 0.0, + 0.11666666666666667, + 0.3833333333333333, + 0.01666666666666672, + 0.016666666666666663, + 0.10000000000000006, + 0.35, + 0.3000000000000001, + 0.1333333333333332, + 5.551115123125783e-17, + 0.05, + 0.21666666666666662, + 0.6000000000000001, + 8.326672684688674e-17, + 0.3166666666666666, + 0.11666666666666667, + 0.016666666666666524, + 0.016666666666666607, + 1.5265566588595902e-16, + 0.0, + 0.050000000000000044, + 0.01666666666666672, + 0.13333333333333341, + 0.01666666666666672, + 0.016666666666666705, + 0.01666666666666658, + 0.05000000000000007, + 0.01666666666666658, + 0.0, + 0.01666666666666669, + 8.326672684688674e-17, + 0.016666666666666663, + 0.01666666666666672, + 0.0, + 0.4666666666666666, + 0.26666666666666666, + 2.220446049250313e-16, + 0.01666666666666676, + 0.15000000000000002, + 0.1333333333333333, + 0.08333333333333334, + 0.1166666666666666, + 0.10000000000000003 + ], + "tv": 0.1 + }, + "65f22553c85ec7cc6debd882911edd29d01d718db562937a7d184c63d6f4b08a": { + "cdf": [ + 1.261131218165885, + 1.897119984885881, + 4.094344562222094, + 23.025850929940457, + 23.025850929940457, + 1.4552872326068425, + 1.6964492894237302, + 1.3862943611198906, + 3.4011973816621563, + 2.99573227355399, + 23.025850929940457, + 2.4849066497880017, + 23.025850929940457, + 3.4011973816621546, + 2.014903020542265, + 3.401197381662155, + 0.9162907318741551, + 23.025850929940457, + 2.1484344131667874, + 0.9588503462929511, + 4.094344562222098, + 4.094344562222101, + 2.302585092994045, + 1.0498221244986778, + 1.2039728043259357, + 2.014903020542266, + 23.025850929940457, + 2.995732273553991, + 1.5293952047605641, + 0.5108256237659905, + 23.025850929940457, + 1.1499055830556604, + 2.1484344131667874, + 4.094344562222109, + 4.094344562222104, + 23.025850929940457, + 23.025850929940457, + 2.99573227355399, + 4.094344562222098, + 2.0149030205422642, + 4.094344562222098, + 4.094344562222099, + 4.094344562222106, + 2.9957322735539895, + 4.094344562222106, + 23.025850929940457, + 4.0943445622220995, + 23.025850929940457, + 4.094344562222101, + 4.094344562222098, + 23.025850929940457, + 0.7621400520468968, + 1.3217558399823195, + 23.025850929940457, + 4.094344562222095, + 1.897119984885881, + 2.014903020542265, + 2.4849066497880004, + 2.148434413166788, + 2.3025850929940455 + ], + "tv": 2.3025850929940455 + }, + "699fb7a3a5e03e925eebcca66434e909514deef05d4f541872d7aa5401092850": { + "cdf": [ + 0.2644213402719371, + 0.07957394887465728, + 1.5815188575173363e-05, + 0.0, + 0.0, + 0.05484877925412057, + 0.17659549710334965, + 0.30737084531077413, + 0.018548358906832114, + 0.014022256809679048, + 0.0, + 0.01509538266306422, + 0.0, + 0.0024306673878275284, + 0.08357658881246555, + 0.0071299257119747245, + 0.5083425279076914, + 0.0, + 0.0495217918380036, + 0.4487415707649179, + 2.3371466522146046e-06, + 0.0020977486000969916, + 0.06049652596259325, + 0.16910250491203924, + 0.17738770277153715, + 0.09961032244036772, + 0.0, + 0.016870533602652085, + 0.04564645529439543, + 0.8369580896779663, + 0.0, + 0.2605294945081027, + 0.03191734763523361, + 0.018559042173070084, + 0.021778121774997387, + 0.0, + 0.0, + 0.00010621809343901971, + 0.0026146847108272555, + 0.09276938934284502, + 0.010274318023660838, + 0.0015559853235290113, + 0.000541723712859534, + 0.007881013907491513, + 5.261402694524338e-05, + 0.0, + 0.0003622768728046929, + 0.0, + 2.776205896404299e-06, + 0.0018593755339062256, + 0.0, + 0.3022256727020373, + 0.2874585414913056, + 0.0, + 0.0024101193922322723, + 0.08566634506527336, + 0.10027836306565714, + 0.03821003387030782, + 0.04369504218726013, + 0.024393579174948204 + ], + "tv": 0.057523278671863 + }, + "fb730f3e9cc23ac8f813a09b5403f53ba6161c2c99c3aac58d56b3894669701e": { + "cdf": [ + 0.0008073144563708696, + 0.008450938959726066, + 0.14566556962376717, + 0.11662157283113118, + 0.9613120479733159, + 0.4559161993201987, + 0.13007836568532669, + 0.0, + 0.19200236679405658, + 0.14540834090260177, + 0.0, + 0.005566650860915751, + 0.21968267338021677, + 0.2246163970276185, + 0.1392370549163871, + 0.7956835919906371, + 0.015547225313367935, + 0.2589026484114561, + 0.009985117958807726, + 0.20038781470088407, + 0.43002219618520154, + 0.08684675151946401, + 0.08690113652697998, + 0.24527823178136574, + 8.820765290079021e-05, + 0.0773960252456958, + 0.33553870487100396, + 0.029087598075230304, + 0.30656563926828645, + 0.0023546079047352206, + 0.12431126675344316, + 0.6725989675549973, + 0.09667722273357357, + 0.20371826343686728, + 0.004002325850119445, + 0.08767464840471205, + 0.066229648779479, + 0.008292386203522963, + 0.0, + 0.8045960790290643, + 0.09329391532432524, + 0.0032885274066105584, + 0.01591117140601617, + 0.0016249797899789793, + 0.0, + 0.761898539420208, + 0.0, + 0.02223999485356763, + 0.0, + 0.005006554796734987, + 0.16410225029878406, + 0.36993307441288975, + 0.3098862570325184, + 0.2311494469588828, + 0.20582569329979933, + 0.8728226443074534, + 0.052821692156074365, + 0.6231803443779603, + 0.4214211641620875, + 0.0017402331644717245 + ], + "tv": 0.17779616160611092 + } + }, + "label": "cdf14.multivariate", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/cdf14.prefix.json b/tests/_r_cache/cdf14.prefix.json new file mode 100644 index 00000000..93dccf1d --- /dev/null +++ b/tests/_r_cache/cdf14.prefix.json @@ -0,0 +1,1983 @@ +{ + "entries": { + "289da3bf5b95e395e1c5781cf3026d24ecf9599a6c0c1cec97bd3c4aab5313c8": { + "lpm": [ + 0.0, + 4.999999999999947e-05, + 0.00017499999999999816, + 0.18392500000000006, + 0.21083749999999996, + 0.2403062500000001, + 0.7227437500000002, + 0.8719312499999997, + 0.8719312499999997, + 0.8719312499999997, + 0.9546812500000001, + 0.9546812500000001, + 1.0433312499999998, + 1.0433312499999998, + 1.1383312500000002, + 1.5926062499999998, + 1.7275562500000003, + 1.7275562500000003, + 2.0272937499999992, + 2.193425, + 2.561775, + 2.983275, + 3.2159125000000004, + 3.2159125000000004, + 3.4642374999999994, + 3.7291437499999995, + 3.7291437499999995, + 3.7291437499999995, + 3.7291437499999995, + 4.31263125, + 4.63336875, + 4.63336875, + 4.974931249999998, + 5.338512499999998, + 5.338512499999998, + 5.725356249999999, + 6.136768749999999, + 6.136768749999999, + 7.038781249999997, + 7.038781249999997, + 7.532231249999997, + 7.532231249999997, + 8.055956249999998, + 8.611524999999997, + 9.200549999999998, + 9.200549999999998, + 10.485649999999996, + 10.485649999999996, + 10.485649999999996, + 11.185181249999998, + 11.185181249999998, + 11.185181249999998, + 11.185181249999998, + 11.925124999999998, + 11.925124999999998, + 13.534224999999998, + 13.534224999999998, + 13.534224999999998, + 13.534224999999998, + 14.407518749999998, + 14.407518749999998, + 14.407518749999998, + 15.32954375, + 15.32954375, + 15.32954375, + 16.302625, + 16.302625, + 16.302625, + 16.302625, + 16.302625, + 17.3292125, + 19.5533625, + 19.5533625, + 19.5533625, + 19.5533625, + 20.75626875, + 22.02341875, + 22.02341875, + 23.357668750000002, + 23.357668750000002, + 24.761943750000004, + 24.761943750000004, + 24.761943750000004, + 26.239250000000006, + 26.239250000000006, + 27.792693750000005, + 27.792693750000005, + 29.42546250000001, + 29.42546250000001, + 31.14081875000001, + 32.94209375000001, + 32.94209375000001, + 32.94209375000001, + 32.94209375000001, + 32.94209375000001, + 38.89598750000001, + 38.89598750000001, + 38.89598750000001, + 41.07583750000001, + 41.07583750000001, + 43.35935000000001, + 45.75026875000002, + 48.25238125000001, + 48.25238125000001, + 50.86951875000001, + 50.86951875000001, + 53.605581250000014, + 53.605581250000014, + 53.605581250000014, + 53.605581250000014, + 59.45054375000002, + 59.45054375000002, + 59.45054375000002, + 62.56768750000002, + 69.21238750000002, + 72.74849375000002, + 72.74849375000002, + 76.43285000000002, + 76.43285000000002, + 80.26984375000002, + 84.26393125000001, + 88.41961250000001, + 88.41961250000001, + 88.41961250000001, + 97.23403750000001, + 97.23403750000001, + 101.90207500000001, + 101.90207500000001, + 111.78342500000004, + 122.42382500000004, + 133.86197500000006, + 139.89238125000006, + 139.89238125000006, + 139.89238125000006, + 139.89238125000006, + 139.89238125000006, + 146.13690625000004, + 146.13690625000004, + 152.60063125000005, + 159.28872500000006, + 159.28872500000006, + 166.20640625000004, + 166.20640625000004, + 173.35896250000005, + 180.75175000000004, + 188.39016875000004, + 188.39016875000004, + 196.27966250000003, + 196.27966250000003, + 204.42574375000004, + 221.51003125000003, + 230.45948750000002, + 239.688025, + 290.21711875, + 324.25039375, + 374.28646875000004, + 415.5107437500001, + 491.72840000000014, + 542.2243625000001, + 1638.9163625000008, + 0.0, + 2062.514362500001 + ], + "ratio": [ + 0.0, + 6.199691537447336e-08, + 2.233924761190627e-07, + 0.0004420826243832095, + 0.000524495774912422, + 0.0006188861472261724, + 0.002673242845453511, + 0.0034788920929759324, + 0.0034788920929759324, + 0.0034788920929759324, + 0.0039577627899148255, + 0.0039577627899148255, + 0.004495362909002542, + 0.004495362909002542, + 0.005098928730283798, + 0.008354048768128848, + 0.009432425356591405, + 0.009432425356591405, + 0.012000189714036422, + 0.013522558005534825, + 0.017140206170564547, + 0.02167318766910753, + 0.02434801190821285, + 0.02434801190821285, + 0.027334688598541296, + 0.03066655030438194, + 0.03066655030438194, + 0.03066655030438194, + 0.03066655030438194, + 0.038515213381184514, + 0.043115232409875706, + 0.043115232409875706, + 0.04822644202689568, + 0.05389852869779475, + 0.05389852869779475, + 0.060184297028079335, + 0.06713955778888422, + 0.06713955778888422, + 0.08329406060218775, + 0.08329406060218775, + 0.09261530294246206, + 0.09261530294246206, + 0.1028485375984557, + 0.11405549618516493, + 0.12629593589903515, + 0.12629593589903515, + 0.1540970880064309, + 0.1540970880064309, + 0.1540970880064309, + 0.16975266367738864, + 0.16975266367738864, + 0.16975266367738864, + 0.16975266367738864, + 0.18662784430862442, + 0.18662784430862442, + 0.2241207754757923, + 0.2241207754757923, + 0.2241207754757923, + 0.2241207754757923, + 0.24474393651190013, + 0.24474393651190013, + 0.24474393651190013, + 0.26659369034125613, + 0.26659369034125613, + 0.26659369034125613, + 0.2896280766085038, + 0.2896280766085038, + 0.2896280766085038, + 0.2896280766085038, + 0.2896280766085038, + 0.3137845828890452, + 0.36510695864287585, + 0.36510695864287585, + 0.36510695864287585, + 0.36510695864287585, + 0.3920402791805905, + 0.41963376822453585, + 0.41963376822453585, + 0.4477254484769634, + 0.4477254484769634, + 0.47614063601896145, + 0.47614063601896145, + 0.47614063601896145, + 0.5046961986817224, + 0.5046961986817224, + 0.5332051849610039, + 0.5332051849610039, + 0.5614816298930602, + 0.5614816298930602, + 0.5893453877223886, + 0.6166266870635957, + 0.6166266870635957, + 0.6166266870635957, + 0.6166266870635957, + 0.6166266870635957, + 0.6935109695022053, + 0.6935109695022053, + 0.6935109695022053, + 0.7170940519723286, + 0.7170940519723286, + 0.739512559941858, + 0.7607139487098908, + 0.7806666523753745, + 0.7806666523753745, + 0.7993585389529907, + 0.7993585389529907, + 0.8167946775215911, + 0.8167946775215911, + 0.8167946775215911, + 0.8167946775215911, + 0.8479909075380996, + 0.8479909075380996, + 0.8479909075380996, + 0.8618250063490834, + 0.8862127826438378, + 0.8968824982547415, + 0.8968824982547415, + 0.9066188089106404, + 0.9066188089106404, + 0.9154850514868678, + 0.9235440830339846, + 0.9308575715612396, + 0.9308575715612396, + 0.9308575715612396, + 0.9434838048919054, + 0.9434838048919054, + 0.948907515643793, + 0.948907515643793, + 0.958230123125755, + 0.9658201508070553, + 0.9719946626764112, + 0.9746370254743166, + 0.9746370254743166, + 0.9746370254743166, + 0.9746370254743166, + 0.9746370254743166, + 0.9770227402945839, + 0.9770227402945839, + 0.9791776818353025, + 0.9811250182953809, + 0.9811250182953809, + 0.982885653172419, + 0.982885653172419, + 0.9844783028096714, + 0.9859196895051229, + 0.9872248520624787, + 0.9872248520624787, + 0.9884072939477512, + 0.9884072939477512, + 0.9894790037481962, + 0.9913316393443724, + 0.9921307617131427, + 0.9928557518635486, + 0.9955898989606184, + 0.9966978181104769, + 0.9977587437168816, + 0.9983297180356093, + 0.9989901223877603, + 0.9992628417969347, + 1.0, + 0.0, + 1.0 + ], + "upm": [ + 854.2164374999996, + 806.4916874999998, + 783.3744624999999, + 415.8582124999999, + 401.7704749999999, + 388.0479937499999, + 269.63943125000003, + 249.76281875000004, + 249.76281875000004, + 249.76281875000004, + 240.26271874999992, + 240.26271874999992, + 231.04721875, + 231.04721875, + 222.11076874999995, + 189.04624374999997, + 181.42324374999993, + 181.42324374999993, + 166.91118125, + 160.01146249999996, + 146.89821249999997, + 134.66491249999996, + 128.86519999999996, + 128.86519999999996, + 123.26987499999998, + 117.87383125, + 117.87383125, + 117.87383125, + 117.87383125, + 107.65951875, + 102.83140624999999, + 102.83140624999999, + 98.18281875000001, + 93.70895, + 93.70895, + 89.40504375, + 85.26640625000002, + 85.26640625000002, + 77.46641875000002, + 77.46641875000002, + 73.79591875000001, + 73.79591875000001, + 70.27239375, + 66.8914125, + 63.6485875, + 63.6485875, + 57.56008750000001, + 57.56008750000001, + 57.56008750000001, + 54.70586875, + 54.70586875, + 54.70586875, + 54.70586875, + 51.9727625, + 51.9727625, + 46.8538625, + 46.8538625, + 46.8538625, + 46.8538625, + 44.460206250000006, + 44.460206250000006, + 44.460206250000006, + 42.17198125000001, + 42.17198125000001, + 42.17198125000001, + 39.985512500000006, + 39.985512500000006, + 39.985512500000006, + 39.985512500000006, + 39.985512500000006, + 37.89725000000001, + 34.00180000000001, + 34.00180000000001, + 34.00180000000001, + 34.00180000000001, + 32.187956250000006, + 30.45905625000001, + 30.45905625000001, + 28.81195625, + 28.81195625, + 27.243581250000005, + 27.243581250000005, + 27.243581250000005, + 25.7509375, + 25.7509375, + 24.331131250000002, + 24.331131250000002, + 22.981350000000003, + 22.981350000000003, + 21.698856250000002, + 20.480981250000003, + 20.480981250000003, + 20.480981250000003, + 20.480981250000003, + 20.480981250000003, + 17.189625, + 17.189625, + 17.189625, + 16.205125000000002, + 16.205125000000002, + 15.272987500000005, + 14.390956250000002, + 13.556818750000001, + 13.556818750000001, + 12.768406250000002, + 12.768406250000002, + 12.023618750000002, + 12.023618750000002, + 12.023618750000002, + 12.023618750000002, + 10.656981250000003, + 10.656981250000003, + 10.656981250000003, + 10.031375000000002, + 8.886675, + 8.36413125, + 8.36413125, + 7.872537500000002, + 7.872537500000002, + 7.4102812500000015, + 6.97581875, + 6.56765, + 6.56765, + 6.56765, + 5.824475000000001, + 5.824475000000001, + 5.486762500000001, + 5.486762500000001, + 4.8727125000000004, + 4.332512500000002, + 3.856862500000001, + 3.6404187500000007, + 3.6404187500000007, + 3.6404187500000007, + 3.6404187500000007, + 3.6404187500000007, + 3.436793750000001, + 3.436793750000001, + 3.2450687500000006, + 3.0644125000000004, + 3.0644125000000004, + 2.8940437500000007, + 2.8940437500000007, + 2.7332500000000004, + 2.5813875000000004, + 2.4378562500000003, + 2.4378562500000003, + 2.3021000000000003, + 2.3021000000000003, + 2.1736312500000006, + 1.9369187500000007, + 1.8279250000000005, + 1.7247125000000003, + 1.2855562500000004, + 1.0742812500000007, + 0.8407562500000001, + 0.6951812500000003, + 0.49708750000000024, + 0.40000000000000024, + 0.0, + 1124.2024374999996, + 0.0 + ] + }, + "292af54c72d0bd0d0e3214b8e559481bcc3dec19bfb1ac5dd547cd81a22e54a8": { + "lpm": [ + 0.00625, + 0.0125, + 0.01875, + 0.025, + 0.03125, + 0.0375, + 0.04375, + 0.0625, + 0.0625, + 0.0625, + 0.075, + 0.075, + 0.0875, + 0.0875, + 0.09375, + 0.1, + 0.1125, + 0.1125, + 0.11875, + 0.125, + 0.13125, + 0.1375, + 0.15, + 0.15, + 0.15625, + 0.18125, + 0.18125, + 0.18125, + 0.18125, + 0.1875, + 0.2, + 0.2, + 0.20625, + 0.21875, + 0.21875, + 0.225, + 0.2375, + 0.2375, + 0.25, + 0.25, + 0.2625, + 0.2625, + 0.26875, + 0.275, + 0.2875, + 0.2875, + 0.30625, + 0.30625, + 0.30625, + 0.33125, + 0.33125, + 0.33125, + 0.33125, + 0.34375, + 0.34375, + 0.36875, + 0.36875, + 0.36875, + 0.36875, + 0.3875, + 0.3875, + 0.3875, + 0.40625, + 0.40625, + 0.40625, + 0.4375, + 0.4375, + 0.4375, + 0.4375, + 0.4375, + 0.44375, + 0.46875, + 0.46875, + 0.46875, + 0.46875, + 0.475, + 0.4875, + 0.4875, + 0.5, + 0.5, + 0.51875, + 0.51875, + 0.51875, + 0.53125, + 0.53125, + 0.54375, + 0.54375, + 0.55625, + 0.55625, + 0.5625, + 0.59375, + 0.59375, + 0.59375, + 0.59375, + 0.59375, + 0.6125, + 0.6125, + 0.6125, + 0.625, + 0.625, + 0.63125, + 0.6375, + 0.65, + 0.65, + 0.6625, + 0.6625, + 0.6875, + 0.6875, + 0.6875, + 0.6875, + 0.70625, + 0.70625, + 0.70625, + 0.7125, + 0.71875, + 0.73125, + 0.73125, + 0.74375, + 0.74375, + 0.75, + 0.75625, + 0.775, + 0.775, + 0.775, + 0.7875, + 0.7875, + 0.8, + 0.8, + 0.80625, + 0.8125, + 0.81875, + 0.85, + 0.85, + 0.85, + 0.85, + 0.85, + 0.8625, + 0.8625, + 0.86875, + 0.88125, + 0.88125, + 0.89375, + 0.89375, + 0.9, + 0.90625, + 0.91875, + 0.91875, + 0.93125, + 0.93125, + 0.9375, + 0.94375, + 0.95, + 0.95625, + 0.9625, + 0.96875, + 0.975, + 0.98125, + 0.9875, + 0.99375, + 1.0, + 0.0, + 1.0 + ], + "ratio": [ + 0.00625, + 0.0125, + 0.01875, + 0.025, + 0.03125, + 0.0375, + 0.04375, + 0.0625, + 0.0625, + 0.0625, + 0.075, + 0.075, + 0.0875, + 0.0875, + 0.09375, + 0.1, + 0.1125, + 0.1125, + 0.11875, + 0.125, + 0.13125, + 0.1375, + 0.15, + 0.15, + 0.15625, + 0.18125, + 0.18125, + 0.18125, + 0.18125, + 0.1875, + 0.2, + 0.2, + 0.20625, + 0.21875, + 0.21875, + 0.225, + 0.2375, + 0.2375, + 0.25, + 0.25, + 0.2625, + 0.2625, + 0.26875, + 0.275, + 0.2875, + 0.2875, + 0.30625, + 0.30625, + 0.30625, + 0.33125, + 0.33125, + 0.33125, + 0.33125, + 0.34375, + 0.34375, + 0.36875, + 0.36875, + 0.36875, + 0.36875, + 0.3875, + 0.3875, + 0.3875, + 0.40625, + 0.40625, + 0.40625, + 0.4375, + 0.4375, + 0.4375, + 0.4375, + 0.4375, + 0.44375, + 0.46875, + 0.46875, + 0.46875, + 0.46875, + 0.475, + 0.4875, + 0.4875, + 0.5, + 0.5, + 0.51875, + 0.51875, + 0.51875, + 0.53125, + 0.53125, + 0.54375, + 0.54375, + 0.55625, + 0.55625, + 0.5625, + 0.59375, + 0.59375, + 0.59375, + 0.59375, + 0.59375, + 0.6125, + 0.6125, + 0.6125, + 0.625, + 0.625, + 0.63125, + 0.6375, + 0.65, + 0.65, + 0.6625, + 0.6625, + 0.6875, + 0.6875, + 0.6875, + 0.6875, + 0.70625, + 0.70625, + 0.70625, + 0.7125, + 0.71875, + 0.73125, + 0.73125, + 0.74375, + 0.74375, + 0.75, + 0.75625, + 0.775, + 0.775, + 0.775, + 0.7875, + 0.7875, + 0.8, + 0.8, + 0.80625, + 0.8125, + 0.81875, + 0.85, + 0.85, + 0.85, + 0.85, + 0.85, + 0.8625, + 0.8625, + 0.86875, + 0.88125, + 0.88125, + 0.89375, + 0.89375, + 0.9, + 0.90625, + 0.91875, + 0.91875, + 0.93125, + 0.93125, + 0.9375, + 0.94375, + 0.95, + 0.95625, + 0.9625, + 0.96875, + 0.975, + 0.98125, + 0.9875, + 0.99375, + 1.0, + 0.0, + 1.0 + ], + "upm": [ + 0.99375, + 0.9875, + 0.98125, + 0.975, + 0.96875, + 0.9625, + 0.95625, + 0.9375, + 0.9375, + 0.9375, + 0.925, + 0.925, + 0.9125, + 0.9125, + 0.90625, + 0.9, + 0.8875, + 0.8875, + 0.88125, + 0.875, + 0.86875, + 0.8625, + 0.85, + 0.85, + 0.84375, + 0.81875, + 0.81875, + 0.81875, + 0.81875, + 0.8125, + 0.8, + 0.8, + 0.79375, + 0.78125, + 0.78125, + 0.775, + 0.7625, + 0.7625, + 0.75, + 0.75, + 0.7375, + 0.7375, + 0.73125, + 0.725, + 0.7125, + 0.7125, + 0.69375, + 0.69375, + 0.69375, + 0.66875, + 0.66875, + 0.66875, + 0.66875, + 0.65625, + 0.65625, + 0.63125, + 0.63125, + 0.63125, + 0.63125, + 0.6125, + 0.6125, + 0.6125, + 0.59375, + 0.59375, + 0.59375, + 0.5625, + 0.5625, + 0.5625, + 0.5625, + 0.5625, + 0.55625, + 0.53125, + 0.53125, + 0.53125, + 0.53125, + 0.525, + 0.5125, + 0.5125, + 0.5, + 0.5, + 0.48125, + 0.48125, + 0.48125, + 0.46875, + 0.46875, + 0.45625, + 0.45625, + 0.44375, + 0.44375, + 0.4375, + 0.40625, + 0.40625, + 0.40625, + 0.40625, + 0.40625, + 0.3875, + 0.3875, + 0.3875, + 0.375, + 0.375, + 0.36875, + 0.3625, + 0.35, + 0.35, + 0.3375, + 0.3375, + 0.3125, + 0.3125, + 0.3125, + 0.3125, + 0.29375, + 0.29375, + 0.29375, + 0.2875, + 0.28125, + 0.26875, + 0.26875, + 0.25625, + 0.25625, + 0.25, + 0.24375, + 0.225, + 0.225, + 0.225, + 0.2125, + 0.2125, + 0.2, + 0.2, + 0.19375, + 0.1875, + 0.18125, + 0.15, + 0.15, + 0.15, + 0.15, + 0.15, + 0.1375, + 0.1375, + 0.13125, + 0.11875, + 0.11875, + 0.10625, + 0.10625, + 0.1, + 0.09375, + 0.08125, + 0.08125, + 0.06875, + 0.06875, + 0.0625, + 0.05625, + 0.05, + 0.04375, + 0.0375, + 0.03125, + 0.025, + 0.01875, + 0.0125, + 0.00625, + 0.0, + 1.0, + 0.0 + ] + }, + "b387662f0356698081523a3eff85a77a6f4062e6f0b6c807111d39391f85f45d": { + "lpm": [ + 0.0, + 0.0012499999999999955, + 0.002499999999999991, + 0.04000000000000001, + 0.042499999999999996, + 0.04562500000000001, + 0.08312500000000002, + 0.09187499999999998, + 0.09187499999999998, + 0.09187499999999998, + 0.09812500000000002, + 0.09812500000000002, + 0.105625, + 0.105625, + 0.11437500000000003, + 0.15187499999999998, + 0.16187500000000005, + 0.16187500000000005, + 0.18437499999999996, + 0.19625, + 0.22125000000000003, + 0.2475, + 0.26125000000000004, + 0.26125000000000004, + 0.27625, + 0.291875, + 0.291875, + 0.291875, + 0.291875, + 0.328125, + 0.346875, + 0.346875, + 0.36687499999999995, + 0.38749999999999996, + 0.38749999999999996, + 0.409375, + 0.43187500000000006, + 0.43187500000000006, + 0.479375, + 0.479375, + 0.504375, + 0.504375, + 0.530625, + 0.5575000000000001, + 0.5850000000000002, + 0.5850000000000002, + 0.6425000000000001, + 0.6425000000000001, + 0.6425000000000001, + 0.6731250000000001, + 0.6731250000000001, + 0.6731250000000001, + 0.6731250000000001, + 0.7062500000000002, + 0.7062500000000002, + 0.7750000000000001, + 0.7750000000000001, + 0.7750000000000001, + 0.7750000000000001, + 0.8118750000000002, + 0.8118750000000002, + 0.8118750000000002, + 0.8506250000000003, + 0.8506250000000003, + 0.8506250000000003, + 0.8912500000000003, + 0.8912500000000003, + 0.8912500000000003, + 0.8912500000000003, + 0.8912500000000003, + 0.9350000000000003, + 1.0237500000000002, + 1.0237500000000002, + 1.0237500000000002, + 1.0237500000000002, + 1.0706250000000002, + 1.1181250000000003, + 1.1181250000000003, + 1.1668750000000003, + 1.1668750000000003, + 1.2168750000000004, + 1.2168750000000004, + 1.2168750000000004, + 1.2687500000000003, + 1.2687500000000003, + 1.3218750000000004, + 1.3218750000000004, + 1.3762500000000002, + 1.3762500000000002, + 1.4318750000000002, + 1.4881250000000004, + 1.4881250000000004, + 1.4881250000000004, + 1.4881250000000004, + 1.4881250000000004, + 1.6662500000000005, + 1.6662500000000005, + 1.6662500000000005, + 1.7275000000000005, + 1.7275000000000005, + 1.7900000000000005, + 1.8531250000000008, + 1.9168750000000006, + 1.9168750000000006, + 1.9818750000000005, + 1.9818750000000005, + 2.0481250000000006, + 2.0481250000000006, + 2.0481250000000006, + 2.0481250000000006, + 2.185625000000001, + 2.185625000000001, + 2.185625000000001, + 2.2562500000000005, + 2.3987500000000006, + 2.470625000000001, + 2.470625000000001, + 2.5437500000000006, + 2.5437500000000006, + 2.618125000000001, + 2.693125000000001, + 2.7687500000000007, + 2.7687500000000007, + 2.7687500000000007, + 2.923750000000001, + 2.923750000000001, + 3.002500000000001, + 3.002500000000001, + 3.162500000000001, + 3.3237500000000013, + 3.486250000000001, + 3.568125000000001, + 3.568125000000001, + 3.568125000000001, + 3.568125000000001, + 3.568125000000001, + 3.6531250000000006, + 3.6531250000000006, + 3.7393750000000012, + 3.826250000000001, + 3.826250000000001, + 3.914375000000001, + 3.914375000000001, + 4.003750000000002, + 4.093750000000002, + 4.184375000000001, + 4.184375000000001, + 4.276250000000002, + 4.276250000000002, + 4.369375000000002, + 4.556875000000002, + 4.651250000000002, + 4.746250000000002, + 5.224375000000002, + 5.513125000000001, + 5.9006250000000025, + 6.193125000000002, + 6.683750000000002, + 6.980000000000001, + 10.955000000000002, + 0.0, + 11.955000000000002 + ], + "ratio": [ + 0.0, + 0.00015156107911488278, + 0.0003067484662576677, + 0.0064257028112449845, + 0.006933115823817293, + 0.007558500724787746, + 0.016263145023233075, + 0.018640628962718748, + 0.018640628962718748, + 0.018640628962718748, + 0.020268525690679075, + 0.020268525690679075, + 0.02220762155059134, + 0.02220762155059134, + 0.02447178389943837, + 0.03492382868640416, + 0.037920937042459774, + 0.037920937042459774, + 0.04481920388939534, + 0.04860681114551086, + 0.056913183279742804, + 0.06617647058823532, + 0.07123381049761421, + 0.07123381049761421, + 0.07678943710910356, + 0.08271342543393555, + 0.08271342543393555, + 0.08271342543393555, + 0.08271342543393555, + 0.09647188533627345, + 0.10389367278172972, + 0.10389367278172972, + 0.11189477697293176, + 0.12034161490683229, + 0.12034161490683229, + 0.12939549585144214, + 0.13892239646160034, + 0.13892239646160034, + 0.15959217644610904, + 0.15959217644610904, + 0.17075751163774866, + 0.17075751163774866, + 0.18258064516129036, + 0.19493006993007, + 0.2078152753108349, + 0.2078152753108349, + 0.2353479853479854, + 0.2353479853479854, + 0.2353479853479854, + 0.2501161170459824, + 0.2501161170459824, + 0.2501161170459824, + 0.2501161170459824, + 0.26575729068673576, + 0.26575729068673576, + 0.2986512524084779, + 0.2986512524084779, + 0.2986512524084779, + 0.2986512524084779, + 0.316058394160584, + 0.316058394160584, + 0.316058394160584, + 0.33406971035837024, + 0.33406971035837024, + 0.33406971035837024, + 0.35262116716122666, + 0.35262116716122666, + 0.35262116716122666, + 0.35262116716122666, + 0.35262116716122666, + 0.37176938369781326, + 0.4107321965897694, + 0.4107321965897694, + 0.4107321965897694, + 0.4107321965897694, + 0.4306184012066365, + 0.45062972292191444, + 0.45062972292191444, + 0.4707513867876955, + 0.4707513867876955, + 0.49092284417549176, + 0.49092284417549176, + 0.49092284417549176, + 0.5110775427995973, + 0.5110775427995973, + 0.5311401305876444, + 0.5311401305876444, + 0.5510510510510511, + 0.5510510510510511, + 0.5707523667164923, + 0.5902330193356471, + 0.5902330193356471, + 0.5902330193356471, + 0.5902330193356471, + 0.5902330193356471, + 0.6464597478176528, + 0.6464597478176528, + 0.6464597478176528, + 0.664423076923077, + 0.664423076923077, + 0.681904761904762, + 0.698962753418199, + 0.715585627624825, + 0.715585627624825, + 0.7316566682048916, + 0.7316566682048916, + 0.7471500227998176, + 0.7471500227998176, + 0.7471500227998176, + 0.7471500227998176, + 0.7760763426542387, + 0.7760763426542387, + 0.7760763426542387, + 0.789588801399825, + 0.8152081563296516, + 0.8273336123901215, + 0.8273336123901215, + 0.8388293487221764, + 0.8388293487221764, + 0.8496957403651115, + 0.8600798403193614, + 0.8699921445404556, + 0.8699921445404556, + 0.8699921445404556, + 0.8880030372057708, + 0.8880030372057708, + 0.8962686567164179, + 0.8962686567164179, + 0.9113832853025936, + 0.9251913709116215, + 0.9377942165433759, + 0.9436363636363637, + 0.9436363636363637, + 0.9436363636363637, + 0.9436363636363637, + 0.9436363636363637, + 0.9485556637455371, + 0.9485556637455371, + 0.9530105129021981, + 0.9571607254534084, + 0.9571607254534084, + 0.9608775698066891, + 0.9608775698066891, + 0.9641782059000603, + 0.9672179562906085, + 0.9700086931324254, + 0.9700086931324254, + 0.972427515633883, + 0.972427515633883, + 0.9744912182882632, + 0.9781325462838745, + 0.9797261716692995, + 0.9811369509043929, + 0.9868949232585597, + 0.9895669733004262, + 0.992327096909817, + 0.9938816449348045, + 0.9957169459962757, + 0.9964311206281227, + 1.0, + 0.0, + 1.0 + ], + "upm": [ + 8.444999999999997, + 8.246249999999998, + 8.147499999999997, + 6.184999999999997, + 6.087499999999998, + 5.990624999999997, + 5.0281249999999975, + 4.836874999999997, + 4.836874999999997, + 4.836874999999997, + 4.743124999999997, + 4.743124999999997, + 4.650624999999997, + 4.650624999999997, + 4.5593749999999975, + 4.196874999999998, + 4.106874999999997, + 4.106874999999997, + 3.929374999999998, + 3.841249999999998, + 3.666249999999998, + 3.4924999999999984, + 3.4062499999999987, + 3.4062499999999987, + 3.321249999999999, + 3.236874999999999, + 3.236874999999999, + 3.236874999999999, + 3.236874999999999, + 3.073124999999999, + 2.9918749999999994, + 2.9918749999999994, + 2.9118749999999993, + 2.8324999999999996, + 2.8324999999999996, + 2.7543749999999996, + 2.6768749999999994, + 2.6768749999999994, + 2.5243749999999996, + 2.5243749999999996, + 2.4493749999999994, + 2.4493749999999994, + 2.3756249999999994, + 2.3024999999999993, + 2.2299999999999995, + 2.2299999999999995, + 2.0874999999999995, + 2.0874999999999995, + 2.0874999999999995, + 2.0181249999999995, + 2.0181249999999995, + 2.0181249999999995, + 2.0181249999999995, + 1.9512499999999995, + 1.9512499999999995, + 1.8199999999999996, + 1.8199999999999996, + 1.8199999999999996, + 1.8199999999999996, + 1.7568749999999997, + 1.7568749999999997, + 1.7568749999999997, + 1.6956249999999997, + 1.6956249999999997, + 1.6956249999999997, + 1.6362499999999998, + 1.6362499999999998, + 1.6362499999999998, + 1.6362499999999998, + 1.6362499999999998, + 1.5799999999999996, + 1.4687499999999998, + 1.4687499999999998, + 1.4687499999999998, + 1.4687499999999998, + 1.415625, + 1.3631249999999997, + 1.3631249999999997, + 1.311875, + 1.311875, + 1.2618749999999999, + 1.2618749999999999, + 1.2618749999999999, + 1.2137499999999999, + 1.2137499999999999, + 1.1668749999999999, + 1.1668749999999999, + 1.1212499999999999, + 1.1212499999999999, + 1.0768749999999998, + 1.0331249999999998, + 1.0331249999999998, + 1.0331249999999998, + 1.0331249999999998, + 1.0331249999999998, + 0.9112499999999999, + 0.9112499999999999, + 0.9112499999999999, + 0.8724999999999999, + 0.8724999999999999, + 0.835, + 0.798125, + 0.7618750000000001, + 0.7618750000000001, + 0.726875, + 0.726875, + 0.6931250000000001, + 0.6931250000000001, + 0.6931250000000001, + 0.6931250000000001, + 0.6306250000000001, + 0.6306250000000001, + 0.6306250000000001, + 0.6012500000000001, + 0.5437500000000001, + 0.5156250000000001, + 0.5156250000000001, + 0.48875000000000013, + 0.48875000000000013, + 0.46312500000000006, + 0.43812500000000004, + 0.4137500000000001, + 0.4137500000000001, + 0.4137500000000001, + 0.3687500000000001, + 0.3687500000000001, + 0.34750000000000003, + 0.34750000000000003, + 0.3075, + 0.26875000000000004, + 0.23125, + 0.21312499999999995, + 0.21312499999999995, + 0.21312499999999995, + 0.21312499999999995, + 0.21312499999999995, + 0.19812500000000002, + 0.19812500000000002, + 0.184375, + 0.17124999999999999, + 0.17124999999999999, + 0.159375, + 0.159375, + 0.14874999999999997, + 0.13874999999999998, + 0.129375, + 0.129375, + 0.12125, + 0.12125, + 0.11437499999999998, + 0.10187500000000001, + 0.09625, + 0.09125, + 0.06937499999999999, + 0.058125, + 0.045625, + 0.038125000000000006, + 0.028750000000000005, + 0.025000000000000005, + 0.0, + 9.444999999999997, + 0.0 + ] + }, + "ed40bf5fb5ffa1fe6eed5da1dbc9058c0ea498e1e6a07bdc4ef7f8b394c2958d": { + "lpm": [ + 0.0, + 0.0002499999999999982, + 0.0006249999999999956, + 0.08562500000000003, + 0.093875, + 0.10268750000000004, + 0.2314375000000001, + 0.2664375, + 0.2664375, + 0.2664375, + 0.28543750000000007, + 0.28543750000000007, + 0.3058125, + 0.3058125, + 0.3278125000000001, + 0.4343125, + 0.46568750000000014, + 0.46568750000000014, + 0.5349374999999998, + 0.5730000000000001, + 0.6565000000000001, + 0.7502500000000001, + 0.8011250000000001, + 0.8011250000000001, + 0.8548749999999998, + 0.9116874999999999, + 0.9116874999999999, + 0.9116874999999999, + 0.9116874999999999, + 1.0356874999999999, + 1.1031875, + 1.1031875, + 1.1745624999999997, + 1.2499999999999996, + 1.2499999999999996, + 1.3296874999999997, + 1.4138124999999997, + 1.4138124999999997, + 1.5960624999999995, + 1.5960624999999995, + 1.6944374999999998, + 1.6944374999999998, + 1.7979374999999997, + 1.90675, + 2.021, + 2.021, + 2.2664999999999997, + 2.2664999999999997, + 2.2664999999999997, + 2.3980625, + 2.3980625, + 2.3980625, + 2.3980625, + 2.5360000000000005, + 2.5360000000000005, + 2.83225, + 2.83225, + 2.83225, + 2.83225, + 2.9909375000000002, + 2.9909375000000002, + 2.9909375000000002, + 3.1571875, + 3.1571875, + 3.1571875, + 3.3313750000000004, + 3.3313750000000004, + 3.3313750000000004, + 3.3313750000000004, + 3.3313750000000004, + 3.5140000000000002, + 3.9057500000000003, + 3.9057500000000003, + 3.9057500000000003, + 3.9057500000000003, + 4.1151875, + 4.3340625, + 4.3340625, + 4.5625625, + 4.5625625, + 4.800937500000001, + 4.800937500000001, + 4.800937500000001, + 5.0495, + 5.0495, + 5.308562500000001, + 5.308562500000001, + 5.578375, + 5.578375, + 5.859187500000001, + 6.151187500000001, + 6.151187500000001, + 6.151187500000001, + 6.151187500000001, + 6.151187500000001, + 7.097500000000001, + 7.097500000000001, + 7.097500000000001, + 7.436875000000001, + 7.436875000000001, + 7.788625000000001, + 8.1529375, + 8.529937499999999, + 8.529937499999999, + 8.9198125, + 8.9198125, + 9.322812500000001, + 9.322812500000001, + 9.322812500000001, + 9.322812500000001, + 10.169562500000001, + 10.169562500000001, + 10.169562500000001, + 10.61375, + 11.54475, + 12.0316875, + 12.0316875, + 12.533125000000002, + 12.533125000000002, + 13.049312500000003, + 13.580437500000002, + 14.126625, + 14.126625, + 14.126625, + 15.265125000000001, + 15.265125000000001, + 15.857750000000001, + 15.857750000000001, + 17.090750000000003, + 18.388, + 19.750000000000004, + 20.455437500000006, + 20.455437500000006, + 20.455437500000006, + 20.455437500000006, + 20.455437500000006, + 21.1775625, + 21.1775625, + 21.916812500000002, + 22.673375000000004, + 22.673375000000004, + 23.447437500000007, + 23.447437500000007, + 24.239250000000006, + 25.049000000000003, + 25.876812500000007, + 25.876812500000007, + 26.722875000000005, + 26.722875000000005, + 27.587437500000004, + 29.3726875, + 30.2935, + 31.233250000000005, + 36.218562500000004, + 39.4398125, + 44.00531250000002, + 47.633437500000014, + 54.07187500000001, + 58.171000000000014, + 129.91100000000006, + 0.0, + 152.82100000000005 + ], + "ratio": [ + 0.0, + 3.210107988032696e-06, + 8.197796432318939e-06, + 0.0017965799412505263, + 0.0020213819685191976, + 0.0022697382962733764, + 0.006737235095482076, + 0.008219827852162648, + 0.008219827852162648, + 0.008219827852162648, + 0.009068705321683882, + 0.009068705321683882, + 0.010008263516167041, + 0.010008263516167041, + 0.011053461240179394, + 0.016538307756749557, + 0.018289509857827364, + 0.018289509857827364, + 0.022359868751044978, + 0.02471425490618936, + 0.030160334451233534, + 0.036724754026139325, + 0.04048130368873169, + 0.04048130368873169, + 0.04459209222262794, + 0.04908935494292484, + 0.04908935494292484, + 0.04908935494292484, + 0.04908935494292484, + 0.05940618905586784, + 0.06529668540988459, + 0.06529668540988459, + 0.07172462750366389, + 0.07873023871008375, + 0.07873023871008375, + 0.08635455903364074, + 0.0946390320637258, + 0.0946390320637258, + 0.11334866131666782, + 0.11334866131666782, + 0.12384428446133607, + 0.12384428446133607, + 0.13515278508607081, + 0.14730763288009888, + 0.1603332011106704, + 0.1603332011106704, + 0.18907983648953033, + 0.18907983648953033, + 0.18907983648953033, + 0.20482255722582848, + 0.20482255722582848, + 0.20482255722582848, + 0.20482255722582848, + 0.22150406149008647, + 0.22150406149008647, + 0.25768810845237017, + 0.25768810845237017, + 0.25768810845237017, + 0.25768810845237017, + 0.27714394922164576, + 0.27714394922164576, + 0.27714394922164576, + 0.2974830396683313, + 0.2974830396683313, + 0.2974830396683313, + 0.31866988712454564, + 0.31866988712454564, + 0.31866988712454564, + 0.31866988712454564, + 0.31866988712454564, + 0.34066892874454674, + 0.3868228186590076, + 0.3868228186590076, + 0.3868228186590076, + 0.3868228186590076, + 0.41077934717508485, + 0.43519053117782913, + 0.43519053117782913, + 0.4599357358870968, + 0.4599357358870968, + 0.48489420260579746, + 0.48489420260579746, + 0.48489420260579746, + 0.5099474853564936, + 0.5099474853564936, + 0.5349755618260608, + 0.5349755618260608, + 0.5598529706945002, + 0.5598529706945002, + 0.5844576059850375, + 0.608666881060756, + 0.608666881060756, + 0.608666881060756, + 0.608666881060756, + 0.608666881060756, + 0.6779539593084345, + 0.6779539593084345, + 0.6779539593084345, + 0.6996119473189089, + 0.6996119473189089, + 0.7204352048839145, + 0.7403684616781693, + 0.7593641502715214, + 0.7593641502715214, + 0.7773934547673, + 0.7773934547673, + 0.7944450362164466, + 0.7944450362164466, + 0.7944450362164466, + 0.7944450362164466, + 0.8256525533815052, + 0.8256525533815052, + 0.8256525533815052, + 0.8398282956163949, + 0.8654235382308846, + 0.8768812404343707, + 0.8768812404343707, + 0.8874893782750319, + 0.8874893782750319, + 0.8972916523413326, + 0.9063292512012814, + 0.914640660407899, + 0.914640660407899, + 0.914640660407899, + 0.9292704084738541, + 0.9292704084738541, + 0.9356708756195422, + 0.9356708756195422, + 0.9468559556786703, + 0.9561148086522462, + 0.9636966917146482, + 0.9669315764594659, + 0.9669315764594659, + 0.9669315764594659, + 0.9669315764594659, + 0.9669315764594659, + 0.9698462401538742, + 0.9698462401538742, + 0.9724813639792341, + 0.9748634878321438, + 0.9748634878321438, + 0.9770172715529815, + 0.9770172715529815, + 0.9789680936995153, + 0.9807368544692846, + 0.9823404638979577, + 0.9823404638979577, + 0.9837968928321614, + 0.9837968928321614, + 0.9851248928724468, + 0.9874499932764069, + 0.9884654289163702, + 0.989395907247846, + 0.9930239492227126, + 0.9945484289893082, + 0.9960460049796288, + 0.9968699642132138, + 0.9978569978593047, + 0.9982838804894373, + 1.0, + 0.0, + 1.0 + ], + "upm": [ + 81.21699999999996, + 77.87874999999997, + 76.23937499999997, + 47.574374999999975, + 46.34712499999998, + 45.139312499999974, + 34.12056249999998, + 32.147562499999985, + 32.147562499999985, + 32.147562499999985, + 31.189562499999983, + 31.189562499999983, + 30.25018749999999, + 30.25018749999999, + 29.329187499999982, + 25.82668749999999, + 24.996312499999988, + 24.996312499999988, + 23.389062499999994, + 22.611999999999988, + 21.11049999999999, + 19.678749999999994, + 18.988874999999997, + 18.988874999999997, + 18.316124999999992, + 17.660312499999996, + 17.660312499999996, + 17.660312499999996, + 17.660312499999996, + 16.398312499999996, + 15.791812499999997, + 15.791812499999997, + 15.201437499999997, + 14.626999999999999, + 14.626999999999999, + 14.068312499999996, + 13.525187500000001, + 13.525187500000001, + 12.484937500000001, + 12.484937500000001, + 11.9875625, + 11.9875625, + 11.5050625, + 11.03725, + 10.583999999999998, + 10.583999999999998, + 9.7205, + 9.7205, + 9.7205, + 9.3099375, + 9.3099375, + 9.3099375, + 9.3099375, + 8.913, + 8.913, + 8.15875, + 8.15875, + 8.15875, + 8.15875, + 7.801062499999999, + 7.801062499999999, + 7.801062499999999, + 7.4558125, + 7.4558125, + 7.4558125, + 7.122625000000001, + 7.122625000000001, + 7.122625000000001, + 7.122625000000001, + 7.122625000000001, + 6.801, + 6.191250000000001, + 6.191250000000001, + 6.191250000000001, + 6.191250000000001, + 5.9028125000000005, + 5.624937500000001, + 5.624937500000001, + 5.3574375, + 5.3574375, + 5.1000625, + 5.1000625, + 5.1000625, + 4.8525, + 4.8525, + 4.614437499999999, + 4.614437499999999, + 4.385625, + 4.385625, + 4.1658124999999995, + 3.9548125, + 3.9548125, + 3.9548125, + 3.9548125, + 3.9548125, + 3.3714999999999997, + 3.3714999999999997, + 3.3714999999999997, + 3.1931249999999993, + 3.1931249999999993, + 3.0223749999999994, + 2.8590624999999994, + 2.7030624999999997, + 2.7030624999999997, + 2.5541875000000003, + 2.5541875000000003, + 2.4121875, + 2.4121875, + 2.4121875, + 2.4121875, + 2.1474375, + 2.1474375, + 2.1474375, + 2.02425, + 1.79525, + 1.6893125000000002, + 1.6893125000000002, + 1.5888750000000003, + 1.5888750000000003, + 1.4936875, + 1.4035624999999998, + 1.318375, + 1.318375, + 1.318375, + 1.161875, + 1.161875, + 1.09025, + 1.09025, + 0.9592500000000002, + 0.844, + 0.744, + 0.6995625, + 0.6995625, + 0.6995625, + 0.6995625, + 0.6995625, + 0.6584375000000001, + 0.6584375000000001, + 0.6201875000000001, + 0.5846250000000002, + 0.5846250000000002, + 0.5515625000000001, + 0.5515625000000001, + 0.52075, + 0.49200000000000016, + 0.46518750000000014, + 0.46518750000000014, + 0.440125, + 0.440125, + 0.41656250000000006, + 0.3733125000000001, + 0.35350000000000004, + 0.33475000000000005, + 0.25443750000000004, + 0.21618750000000006, + 0.17468750000000002, + 0.14956250000000004, + 0.11612500000000003, + 0.10000000000000005, + 0.0, + 99.10699999999994, + 0.0 + ] + } + }, + "label": "cdf14.prefix", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/cdf14.rescale.json b/tests/_r_cache/cdf14.rescale.json new file mode 100644 index 00000000..33706b1a --- /dev/null +++ b/tests/_r_cache/cdf14.rescale.json @@ -0,0 +1,173 @@ +{ + "entries": { + "8360cb7401886fd4bcd5bae36409db90f448fa6bb7fd62f7b01650ff593cfb22": [ + -29.262066357684667, + -40.23976419431125, + -32.95778814304315, + -41.87572679933984, + -35.83240954248634, + -35.65888117086311, + -31.952673374873957, + -37.63153637983988, + -31.857833513521882, + -38.89520360012873, + -37.655125254261705, + -39.587732743769706, + -26.802618403046555, + -45.770635488267104, + -31.9044474563971, + -42.287479100652114, + -34.609924236473006, + -33.94926697256433, + -30.01198527395476, + -31.617471175581475, + -39.69142010218102, + -33.07718672876896, + -30.390722721649887, + -27.827474239129838, + -36.99479087081885, + -37.06227661507588, + -33.53243984904163, + -40.01599501605777, + -32.2680012560224, + -29.1987951259877, + 7.702937551602594, + 9.892041416123384, + 10.015177410437575, + 11.043577955583418, + 7.611244107614345, + 6.391985429782935, + 5.454488903681305, + 9.493932892903379, + 10.674124796616674, + 9.48158264773645, + 10.418801584885948, + 7.285184069930495, + 9.921715382512174, + 6.0884102534068205, + 7.390678014782333, + 4.695734977682593, + 9.141460937894776, + 8.293177739232364, + 7.363599876859779, + 8.6712319697489, + 6.77174301176839, + 12.100446036038187, + 7.18316310691624, + 12.427359969155118, + 10.44818708612962, + 9.458983475976813, + 11.871840597238261, + 9.302689128458704, + 9.37356629056095, + 11.31702712331911, + 8.017724692811827, + 9.827743765620635, + 14.00352351021025, + 9.287285710146259, + 7.142022944694297, + 7.071189411365646, + 6.986697662163111, + 9.040153831104599, + 10.728088779083901, + 10.319800492407147, + 6.751917635477009, + 10.127651501959868, + 11.918960372358919, + 7.720554984808929, + 10.89236765704577, + 11.155953058851063, + 11.089496515082237, + 4.553054249992839, + 9.54363394090493, + 8.57211620855773, + 8698.383641036567 + ], + "86fec351f55c20c9c5cc852fea30611c69171c98bfd97c70bb4aef662e643fbf": [ + -32.33958474131289, + -44.47181713777454, + -36.4239889797897, + -46.279835431911415, + -39.60093695093222, + -39.40915844115322, + -35.31316536868076, + -41.58927960950477, + -35.20835111204011, + -42.985847871493505, + -41.61534934750592, + -43.75121094096534, + -29.621474387326213, + -50.58437524347398, + -35.25986748608265, + -46.73489210077246, + -38.24988174295148, + -37.51974254806419, + -33.1683733184894, + -34.94270964634771, + -43.86580319405362, + -36.555944824393215, + -33.586942931268254, + -30.75411525258556, + -40.88556699071946, + -40.960150272657025, + -37.05907733328178, + -44.224513949607115, + -35.661656572584434, + -32.269659216090574, + 8.513062565784022, + 10.93239649349941, + 11.068482813383783, + 12.205041188012112, + 8.41172563811001, + 7.064236405759369, + 6.028142509314901, + 10.492418531398616, + 11.796732301130886, + 10.478769399609023, + 11.514556512816418, + 8.051373566915169, + 10.965191298176256, + 6.728733949378803, + 8.167962406798496, + 5.189589736325402, + 10.10287677728584, + 9.165378855831955, + 8.138036436250848, + 9.583193396854298, + 7.483933441288423, + 13.373061054773157, + 7.938622965557743, + 13.734356826367714, + 11.547032514203998, + 10.453793452207675, + 13.120412972097554, + 10.281061484671053, + 10.359392862982189, + 12.507249255765588, + 8.860956159632638, + 10.861336600063181, + 15.476286934072979, + 10.264038074713184, + 7.893156054705137, + 7.81487289364577, + 7.721495069609788, + 9.990915109066954, + 11.85637172517721, + 11.405143384551128, + 7.462023011971283, + 11.1927859083712, + 13.17248837722717, + 8.532532840614802, + 12.037927963554766, + 12.32923488405926, + 12.255789044570133, + 5.031903145512816, + 10.547346684247813, + 9.473653540062887, + 9613.200634338578 + ] + }, + "label": "cdf14.rescale", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/cdf14.univariate.json b/tests/_r_cache/cdf14.univariate.json new file mode 100644 index 00000000..5853f8c1 --- /dev/null +++ b/tests/_r_cache/cdf14.univariate.json @@ -0,0 +1,1861 @@ +{ + "entries": { + "22f891394531f4c80c850c119bf4b17c9b560fb58ca8cee4bad607c0532301ab": { + "f": [ + [ + 0.0025017375991542383, + 0.007260746426075224, + 0.011477552524412706, + 0.01983363596737441, + 0.024014015942464967, + 0.0488114016996851, + 0.04882866583560183, + 0.07463929508208995, + 0.08344243282147773, + 0.08406095946785207, + 0.0916836826472264, + 0.09282020353797958, + 0.09668529306953827, + 0.09837660236618552, + 0.09984242926852514, + 0.10764009880084309, + 0.11444300820803756, + 0.12689240969908433, + 0.1307925506000959, + 0.14324502777306058, + 0.1566908284613338, + 0.15886321807749368, + 0.15992305513278954, + 0.1735152694664494, + 0.21494528659312248, + 0.2434306182204962, + 0.24382214495893031, + 0.25012492748282444, + 0.2633509786971155, + 0.270905290883451, + 0.2754090190380725, + 0.29162852733090605, + 0.2992247046404025, + 0.30353789394244657, + 0.3049662434475995, + 0.3246367802861, + 0.36391930371671627, + 0.36623133922498474, + 0.38889985751007333, + 0.3895144693921217, + 0.395319629903312, + 0.398100810183879, + 0.4216188627518788, + 0.43089112364974413, + 0.44226267216170284, + 0.4652913640861715, + 0.4681020407413186, + 0.49219730589208577, + 0.49841764784016895, + 0.5089423756182937, + 0.5113449358733342, + 0.5206640661206473, + 0.5327561338467365, + 0.5467653005430981, + 0.5654169652434965, + 0.5705689139923802, + 0.5766155617203561, + 0.6060331908443756, + 0.6225037197867944, + 0.6380756794063611, + 0.661346719186576, + 0.6829753584921339, + 0.6976919107619985, + 0.7021921280098916, + 0.7165630236841162, + 0.7507029558638536, + 0.7726492209025212, + 0.824909687042792, + 0.8302867088236117, + 0.8356514262277522, + 0.8364596821057044, + 0.843973487179215, + 0.8529273383012377, + 0.8886201166862017, + 0.8892458821527571, + 0.894488238153153, + 0.9217288903029234, + 0.954511012483181, + 0.9657954042968545, + 0.9834536157637693, + 0.9955225558982556, + 1.0078480278951047, + 1.011430803091598, + 1.0136908214555849, + 1.0158332545073805, + 1.0174830194145026, + 1.0299392884172376, + 1.0389310281954847, + 1.0549709967729324, + 1.056944525841806, + 1.067179156909234, + 1.1064319054710647, + 1.148932414928497, + 1.1510510723341476, + 1.1538905363358136, + 1.162853832162505, + 1.1835402103377461, + 1.1881866089844066, + 1.205485792496495, + 1.2141943282063825, + 1.2643202272874934, + 1.2729944979654988, + 1.2829703599261217, + 1.3002233632614542, + 1.3168443518223818, + 1.3537939757960271, + 1.364182550201667, + 1.3999632339205146, + 1.4475072287372552, + 1.597423081303548, + 1.598368750555247, + 1.6142684859321508, + 1.645609776871833, + 1.6847711766969609, + 1.7171318358619652, + 1.755719372973814, + 1.8028865134607663, + 1.8114520149849438, + 1.8124706414607625, + 1.8439820664244844, + 1.8654418675004216, + 1.885814677918207, + 1.8862143449236581, + 1.8954509524453798, + 1.9186941583387385, + 2.0143605965464504, + 2.141640218692706, + 2.2585650819572285, + 2.267285307767844, + 2.2884016983859965, + 2.3045055927253033, + 2.3606220804426625, + 2.371347180833694, + 2.426673171068079, + 2.4481241419163755, + 2.5738970558594723, + 2.677714403873397, + 2.711729618794262, + 2.7776171635623226, + 2.940020819126196, + 3.0312089020872115, + 3.2171029690507713, + 3.234638806868786, + 3.57391282146812, + 3.7441531593853385, + 3.86893401199373, + 3.999828991520581, + 4.188454773635135, + 4.416394570345562, + 5.843853651476255 + ], + [ + 0.0, + 6.331765952847016e-08, + 2.760558896777022e-07, + 1.494772014319967e-06, + 2.6080365543093704e-06, + 1.9545434743862742e-05, + 1.9563737320298118e-05, + 6.151628193202834e-05, + 8.294379614956123e-05, + 8.460414719642338e-05, + 0.00010711523197460915, + 0.00011080522590018821, + 0.00012410636349022855, + 0.00013030989233713187, + 0.00013589266273896254, + 0.00016914948259187255, + 0.0002033214642915423, + 0.00027950027890868626, + 0.0003071910710366846, + 0.000409127922853475, + 0.0005438023347913472, + 0.0005680871618990271, + 0.000580206917977931, + 0.0007527342071829319, + 0.0014905843422816465, + 0.002201397613428713, + 0.0022124162940632334, + 0.0023948768765047353, + 0.002810061450998821, + 0.003067656782166643, + 0.0032286492118528223, + 0.0038571453608670946, + 0.004178660940611307, + 0.0043693056315082125, + 0.004433770485803416, + 0.005392903791025086, + 0.007732006162429459, + 0.00788823827295974, + 0.009538687626538483, + 0.009586496505173944, + 0.010046477915083094, + 0.010272316318179483, + 0.012330801943785885, + 0.013217784879531956, + 0.014366775426157698, + 0.016909783290844124, + 0.017240583105341656, + 0.020269448292694607, + 0.021108873232162473, + 0.022585300514617566, + 0.0229324124039896, + 0.024315470175394756, + 0.026199010597442677, + 0.028510809605309256, + 0.03181261134946535, + 0.03277091773692177, + 0.03392198631487264, + 0.03994184813083842, + 0.04362512317656091, + 0.0473217636878091, + 0.053246954616548416, + 0.05919712229991501, + 0.06349628578216537, + 0.06485220506241754, + 0.06931461025537095, + 0.0807397405701434, + 0.08870614756453657, + 0.1096692588502157, + 0.11198526241827023, + 0.11432575843708943, + 0.1146809725027399, + 0.11801617296138256, + 0.12206868896726387, + 0.13906785843954247, + 0.1393777993131895, + 0.1419905529636346, + 0.15603223033872732, + 0.1739407555853962, + 0.18035242306893462, + 0.19063306634980673, + 0.19782950824092566, + 0.20531817323156823, + 0.20752094894516138, + 0.20891646467281458, + 0.21024368983228928, + 0.21126859120229405, + 0.21908807688950072, + 0.22482027234456495, + 0.2352236169451644, + 0.23651899311003474, + 0.24329021818747196, + 0.2700468028890157, + 0.300250691606401, + 0.3017852488400777, + 0.3038458865877357, + 0.31038047629050153, + 0.3256244337143007, + 0.3290772212321537, + 0.3420178258537936, + 0.34857903644792076, + 0.38682197573630595, + 0.39349867862809407, + 0.40119109296706734, + 0.4145210605383757, + 0.4273795577173553, + 0.4559413999796605, + 0.46394623105402805, + 0.4913573342517861, + 0.5271913590571083, + 0.6324933875778719, + 0.6331094896513386, + 0.6433671480003677, + 0.6630187673223435, + 0.6864841027098634, + 0.7049394715532734, + 0.7258290018123871, + 0.7497144620018761, + 0.7538586946342027, + 0.7543476339385164, + 0.7690731839602858, + 0.7786629639572434, + 0.7874456467220773, + 0.7876148540641082, + 0.7914931479802778, + 0.8009834676218145, + 0.8361655242338144, + 0.874132311537065, + 0.9013948523940112, + 0.9031728372209462, + 0.9073457444642881, + 0.9104063589815269, + 0.9203043659166399, + 0.922067329715827, + 0.9305649447075844, + 0.9336057476249875, + 0.9489623939484616, + 0.9589242884724485, + 0.9617418034456928, + 0.9666634587064133, + 0.9762786110651385, + 0.9804077006301247, + 0.986744847558621, + 0.98722400753774, + 0.9937747578679684, + 0.9956398813267, + 0.996621175798456, + 0.9973993320215127, + 0.9982041922758776, + 0.9988493888911926, + 1.0 + ] + ], + "tv": 0.14068055354883 + }, + "855b7e93410f68a4ca9036d53ff66ae532d444b48af86787034e9b2c3babb3ca": { + "f": [ + [ + 0.0025017375991542383, + 0.007260746426075224, + 0.011477552524412706, + 0.01983363596737441, + 0.024014015942464967, + 0.0488114016996851, + 0.04882866583560183, + 0.07463929508208995, + 0.08344243282147773, + 0.08406095946785207, + 0.0916836826472264, + 0.09282020353797958, + 0.09668529306953827, + 0.09837660236618552, + 0.09984242926852514, + 0.10764009880084309, + 0.11444300820803756, + 0.12689240969908433, + 0.1307925506000959, + 0.14324502777306058, + 0.1566908284613338, + 0.15886321807749368, + 0.15992305513278954, + 0.1735152694664494, + 0.21494528659312248, + 0.2434306182204962, + 0.24382214495893031, + 0.25012492748282444, + 0.2633509786971155, + 0.270905290883451, + 0.2754090190380725, + 0.29162852733090605, + 0.2992247046404025, + 0.30353789394244657, + 0.3049662434475995, + 0.3246367802861, + 0.36391930371671627, + 0.36623133922498474, + 0.38889985751007333, + 0.3895144693921217, + 0.395319629903312, + 0.398100810183879, + 0.4216188627518788, + 0.43089112364974413, + 0.44226267216170284, + 0.4652913640861715, + 0.4681020407413186, + 0.49219730589208577, + 0.49841764784016895, + 0.5089423756182937, + 0.5113449358733342, + 0.5206640661206473, + 0.5327561338467365, + 0.5467653005430981, + 0.5654169652434965, + 0.5705689139923802, + 0.5766155617203561, + 0.6060331908443756, + 0.6225037197867944, + 0.6380756794063611, + 0.661346719186576, + 0.6829753584921339, + 0.6976919107619985, + 0.7021921280098916, + 0.7165630236841162, + 0.7507029558638536, + 0.7726492209025212, + 0.824909687042792, + 0.8302867088236117, + 0.8356514262277522, + 0.8364596821057044, + 0.843973487179215, + 0.8529273383012377, + 0.8886201166862017, + 0.8892458821527571, + 0.894488238153153, + 0.9217288903029234, + 0.954511012483181, + 0.9657954042968545, + 0.9834536157637693, + 0.9955225558982556, + 1.0078480278951047, + 1.011430803091598, + 1.0136908214555849, + 1.0158332545073805, + 1.0174830194145026, + 1.0299392884172376, + 1.0389310281954847, + 1.0549709967729324, + 1.056944525841806, + 1.067179156909234, + 1.1064319054710647, + 1.148932414928497, + 1.1510510723341476, + 1.1538905363358136, + 1.162853832162505, + 1.1835402103377461, + 1.1881866089844066, + 1.205485792496495, + 1.2141943282063825, + 1.2643202272874934, + 1.2729944979654988, + 1.2829703599261217, + 1.3002233632614542, + 1.3168443518223818, + 1.3537939757960271, + 1.364182550201667, + 1.3999632339205146, + 1.4475072287372552, + 1.597423081303548, + 1.598368750555247, + 1.6142684859321508, + 1.645609776871833, + 1.6847711766969609, + 1.7171318358619652, + 1.755719372973814, + 1.8028865134607663, + 1.8114520149849438, + 1.8124706414607625, + 1.8439820664244844, + 1.8654418675004216, + 1.885814677918207, + 1.8862143449236581, + 1.8954509524453798, + 1.9186941583387385, + 2.0143605965464504, + 2.141640218692706, + 2.2585650819572285, + 2.267285307767844, + 2.2884016983859965, + 2.3045055927253033, + 2.3606220804426625, + 2.371347180833694, + 2.426673171068079, + 2.4481241419163755, + 2.5738970558594723, + 2.677714403873397, + 2.711729618794262, + 2.7776171635623226, + 2.940020819126196, + 3.0312089020872115, + 3.2171029690507713, + 3.234638806868786, + 3.57391282146812, + 3.7441531593853385, + 3.86893401199373, + 3.999828991520581, + 4.188454773635135, + 4.416394570345562, + 5.843853651476255 + ], + [ + 0.0, + 2.7923027421816855e-05, + 7.768706547586693e-05, + 0.00022691370233461153, + 0.00032723604911250357, + 0.0010876357630187198, + 0.0010882810275013438, + 0.00223395138947459, + 0.0026909877047296615, + 0.002727203914800871, + 0.003224292513269298, + 0.003306055594097271, + 0.0036095057049798655, + 0.003753508118017383, + 0.0038878413289805164, + 0.004656858820310462, + 0.005378442519335005, + 0.006797813220247213, + 0.007272880820855168, + 0.008888295766722773, + 0.010753019704374686, + 0.011071789639332032, + 0.0112345317151956, + 0.013431845785507256, + 0.020666925744518262, + 0.026080243723245886, + 0.02615876381742577, + 0.027470641382589877, + 0.030348688914868017, + 0.032066064625304216, + 0.03312823594051842, + 0.03711401832976853, + 0.03906017432673521, + 0.04020243825575262, + 0.04059162964787702, + 0.046151934774927045, + 0.057915038941455846, + 0.058639837773052386, + 0.0660006435983146, + 0.06620694624611655, + 0.068201043407847, + 0.06917928633071618, + 0.07772187736662328, + 0.08120789261661103, + 0.08560196207641718, + 0.09480045693957723, + 0.095955148694522, + 0.1061317930076215, + 0.10883437889635743, + 0.11350679173210615, + 0.11459428525509761, + 0.11889063530682659, + 0.12458678482452681, + 0.13133642217759353, + 0.14054116107138323, + 0.14313534766104638, + 0.14622700551322787, + 0.16160579161703226, + 0.17042779583035716, + 0.17893023280258072, + 0.19189333153169338, + 0.20419025009476133, + 0.21270320264742987, + 0.2153396332116102, + 0.22386012713087658, + 0.24444071164007736, + 0.25789239766308236, + 0.2904926147623139, + 0.29389341848792744, + 0.29731001962264547, + 0.29782797444571324, + 0.3026726275559383, + 0.3084826145700601, + 0.33180337898886114, + 0.33221460948394804, + 0.3356752184438318, + 0.3537275886547112, + 0.3755011282066739, + 0.38300769033576104, + 0.3947689313916717, + 0.402811247195024, + 0.41102616723300106, + 0.4134155244337179, + 0.41492501795772335, + 0.41635822659327765, + 0.4174635799430099, + 0.42580494896379345, + 0.4318122337310394, + 0.4424885340881372, + 0.4437987178819656, + 0.45058193812484787, + 0.47628301995204037, + 0.5034928195277704, + 0.5048316915675105, + 0.506623054685009, + 0.5122546532243435, + 0.525114443186664, + 0.527975126205797, + 0.5385296597086185, + 0.5437841597275028, + 0.5732785803797988, + 0.578250804955066, + 0.5839112295112823, + 0.5935605559933046, + 0.6026901942105, + 0.6224261967750228, + 0.627832865015022, + 0.6459707896870481, + 0.6689995481706649, + 0.7345948686723105, + 0.7349763515935013, + 0.7412861794865918, + 0.7533324858779886, + 0.7677280832362883, + 0.7790869714670225, + 0.7920113225863953, + 0.8069564620892407, + 0.8095610718877669, + 0.8098653790622273, + 0.8189971290268161, + 0.8249582805926461, + 0.8304111095724666, + 0.8305153010918314, + 0.8328740767097461, + 0.8386102525803757, + 0.860319783788874, + 0.8853785503145604, + 0.9051582776510203, + 0.9065062793404565, + 0.9096387222143791, + 0.9119194055910568, + 0.919360299961661, + 0.9206925556777403, + 0.927125445471864, + 0.9294412276034316, + 0.9415897059476931, + 0.9501698436743526, + 0.9526805507901546, + 0.9570942591039003, + 0.9663642509801097, + 0.9707216153175808, + 0.9780727906533416, + 0.9786581715666419, + 0.9876457660485877, + 0.9909472259951435, + 0.9928327154343929, + 0.9943483116293869, + 0.9959104278151459, + 0.9971092444744525, + 1.0 + ] + ], + "tv": 0.33394491396389014 + }, + "a184f87b8a1b70f1f888b238a05b441466b54e253edcde367443af02e4d630ff": { + "f": [ + [ + 0.0025017375991542383, + 0.007260746426075224, + 0.011477552524412706, + 0.01983363596737441, + 0.024014015942464967, + 0.0488114016996851, + 0.04882866583560183, + 0.07463929508208995, + 0.08344243282147773, + 0.08406095946785207, + 0.0916836826472264, + 0.09282020353797958, + 0.09668529306953827, + 0.09837660236618552, + 0.09984242926852514, + 0.10764009880084309, + 0.11444300820803756, + 0.12689240969908433, + 0.1307925506000959, + 0.14324502777306058, + 0.1566908284613338, + 0.15886321807749368, + 0.15992305513278954, + 0.1735152694664494, + 0.21494528659312248, + 0.2434306182204962, + 0.24382214495893031, + 0.25012492748282444, + 0.2633509786971155, + 0.270905290883451, + 0.2754090190380725, + 0.29162852733090605, + 0.2992247046404025, + 0.30353789394244657, + 0.3049662434475995, + 0.3246367802861, + 0.36391930371671627, + 0.36623133922498474, + 0.38889985751007333, + 0.3895144693921217, + 0.395319629903312, + 0.398100810183879, + 0.4216188627518788, + 0.43089112364974413, + 0.44226267216170284, + 0.4652913640861715, + 0.4681020407413186, + 0.49219730589208577, + 0.49841764784016895, + 0.5089423756182937, + 0.5113449358733342, + 0.5206640661206473, + 0.5327561338467365, + 0.5467653005430981, + 0.5654169652434965, + 0.5705689139923802, + 0.5766155617203561, + 0.6060331908443756, + 0.6225037197867944, + 0.6380756794063611, + 0.661346719186576, + 0.6829753584921339, + 0.6976919107619985, + 0.7021921280098916, + 0.7165630236841162, + 0.7507029558638536, + 0.7726492209025212, + 0.824909687042792, + 0.8302867088236117, + 0.8356514262277522, + 0.8364596821057044, + 0.843973487179215, + 0.8529273383012377, + 0.8886201166862017, + 0.8892458821527571, + 0.894488238153153, + 0.9217288903029234, + 0.954511012483181, + 0.9657954042968545, + 0.9834536157637693, + 0.9955225558982556, + 1.0078480278951047, + 1.011430803091598, + 1.0136908214555849, + 1.0158332545073805, + 1.0174830194145026, + 1.0299392884172376, + 1.0389310281954847, + 1.0549709967729324, + 1.056944525841806, + 1.067179156909234, + 1.1064319054710647, + 1.148932414928497, + 1.1510510723341476, + 1.1538905363358136, + 1.162853832162505, + 1.1835402103377461, + 1.1881866089844066, + 1.205485792496495, + 1.2141943282063825, + 1.2643202272874934, + 1.2729944979654988, + 1.2829703599261217, + 1.3002233632614542, + 1.3168443518223818, + 1.3537939757960271, + 1.364182550201667, + 1.3999632339205146, + 1.4475072287372552, + 1.597423081303548, + 1.598368750555247, + 1.6142684859321508, + 1.645609776871833, + 1.6847711766969609, + 1.7171318358619652, + 1.755719372973814, + 1.8028865134607663, + 1.8114520149849438, + 1.8124706414607625, + 1.8439820664244844, + 1.8654418675004216, + 1.885814677918207, + 1.8862143449236581, + 1.8954509524453798, + 1.9186941583387385, + 2.0143605965464504, + 2.141640218692706, + 2.2585650819572285, + 2.267285307767844, + 2.2884016983859965, + 2.3045055927253033, + 2.3606220804426625, + 2.371347180833694, + 2.426673171068079, + 2.4481241419163755, + 2.5738970558594723, + 2.677714403873397, + 2.711729618794262, + 2.7776171635623226, + 2.940020819126196, + 3.0312089020872115, + 3.2171029690507713, + 3.234638806868786, + 3.57391282146812, + 3.7441531593853385, + 3.86893401199373, + 3.999828991520581, + 4.188454773635135, + 4.416394570345562, + 5.843853651476255 + ], + [ + 0.0, + 6.331766156447696e-08, + 2.760559278064649e-07, + 1.4947731314494136e-06, + 2.6080399552079094e-06, + 1.9545625758415876e-05, + 1.9563928692665567e-05, + 6.151817413614516e-05, + 8.294723617647312e-05, + 8.460772632920118e-05, + 0.00010712096922075894, + 0.00011081136525279705, + 0.0001241140653222175, + 0.00013031838340876378, + 0.00013590189698342562, + 0.00016916378997897964, + 0.00020334213690267574, + 0.00027953934639136815, + 0.00030723826387883945, + 0.00040921163851654306, + 0.0005439502489074643, + 0.0005682485845484534, + 0.0005803753031470328, + 0.0007530176538251613, + 0.0014916963683054845, + 0.0022038242511386867, + 0.002214867302760681, + 0.0023977491809249038, + 0.0028140170858005076, + 0.003072371686173808, + 0.003233872545624578, + 0.0038646033298585076, + 0.004187415942206937, + 0.004378878943353036, + 0.004443628796614213, + 0.005407497990398694, + 0.007762053104380264, + 0.00791951501197631, + 0.009584472290378577, + 0.00963274275983149, + 0.01009728434453739, + 0.010325440679424279, + 0.012407457080086687, + 0.013305917269578681, + 0.014470976773240172, + 0.017054386129473614, + 0.017390932538685434, + 0.020477692359884354, + 0.021334851248799212, + 0.022844254883909527, + 0.02319945062583639, + 0.024615972457081116, + 0.026548319211709966, + 0.028925136943280902, + 0.03232962712372445, + 0.03331991162338949, + 0.034510688522079176, + 0.04076142149112816, + 0.04460531222538322, + 0.04847806474972252, + 0.05471699553421369, + 0.06102164306260041, + 0.06560179104303471, + 0.06705069273314965, + 0.07183398604989967, + 0.08418599821048531, + 0.09288987343119401, + 0.11616226601986948, + 0.11876693975186349, + 0.12140606916497118, + 0.1218072158848594, + 0.12558155983437486, + 0.13018692185532493, + 0.14973959117898722, + 0.1500996621559987, + 0.15314016902289584, + 0.16964097272047232, + 0.19108878356035008, + 0.19888081533795968, + 0.21150290029037616, + 0.22043411046749672, + 0.22981346233770772, + 0.23258920758028934, + 0.23435170955009849, + 0.23603084924929163, + 0.23732943586699823, + 0.24729291000837683, + 0.25466036986280677, + 0.2681717976186593, + 0.26986703100488874, + 0.2787754784291382, + 0.3147748604416584, + 0.35703313894919325, + 0.3592285571211446, + 0.3621842158426557, + 0.37161524838971655, + 0.39396810325079595, + 0.39910123243661216, + 0.4185774389869988, + 0.42859920435498894, + 0.48909997042233894, + 0.5000483719163067, + 0.5128127517427958, + 0.535325066767867, + 0.5575321861474093, + 0.6086983173020202, + 0.6235208077448593, + 0.6760095409002743, + 0.7490645369513078, + 1.0010139675057348, + 1.0026918123635087, + 1.0310484521664742, + 1.0877280395642954, + 1.1599052111353756, + 1.2205747625137513, + 1.2940032862749262, + 1.385152860886757, + 1.4018494959051446, + 1.4038378885739164, + 1.4656544323372906, + 1.508068689204903, + 1.5485575431924254, + 1.5493539264483336, + 1.5677833748793273, + 1.6143673804310565, + 1.8088986549468355, + 2.072524015327094, + 2.316631811778993, + 2.3348277168408185, + 2.378880395997301, + 2.4124709322957667, + 2.529540474067089, + 2.5519100265504435, + 2.6673634195120273, + 2.712144786863617, + 2.97519254442455, + 3.192338292566077, + 3.263397452664706, + 3.4011011474852126, + 3.741378151134304, + 3.932618679238963, + 4.323368938637421, + 4.3601874580617705, + 5.079142940675924, + 5.4352560033448265, + 5.690227499373472, + 5.9519869521488316, + 6.3223003726715605, + 6.767462078786424, + 23.025850929940457 + ] + ], + "tv": 0.1516145443462704 + }, + "dcc58049cd89ab8af597b9bd143357a79de20b8221502403339a09b21781e98d": { + "f": [ + [ + 0.0025017375991542383, + 0.007260746426075224, + 0.011477552524412706, + 0.01983363596737441, + 0.024014015942464967, + 0.0488114016996851, + 0.04882866583560183, + 0.07463929508208995, + 0.08344243282147773, + 0.08406095946785207, + 0.0916836826472264, + 0.09282020353797958, + 0.09668529306953827, + 0.09837660236618552, + 0.09984242926852514, + 0.10764009880084309, + 0.11444300820803756, + 0.12689240969908433, + 0.1307925506000959, + 0.14324502777306058, + 0.1566908284613338, + 0.15886321807749368, + 0.15992305513278954, + 0.1735152694664494, + 0.21494528659312248, + 0.2434306182204962, + 0.24382214495893031, + 0.25012492748282444, + 0.2633509786971155, + 0.270905290883451, + 0.2754090190380725, + 0.29162852733090605, + 0.2992247046404025, + 0.30353789394244657, + 0.3049662434475995, + 0.3246367802861, + 0.36391930371671627, + 0.36623133922498474, + 0.38889985751007333, + 0.3895144693921217, + 0.395319629903312, + 0.398100810183879, + 0.4216188627518788, + 0.43089112364974413, + 0.44226267216170284, + 0.4652913640861715, + 0.4681020407413186, + 0.49219730589208577, + 0.49841764784016895, + 0.5089423756182937, + 0.5113449358733342, + 0.5206640661206473, + 0.5327561338467365, + 0.5467653005430981, + 0.5654169652434965, + 0.5705689139923802, + 0.5766155617203561, + 0.6060331908443756, + 0.6225037197867944, + 0.6380756794063611, + 0.661346719186576, + 0.6829753584921339, + 0.6976919107619985, + 0.7021921280098916, + 0.7165630236841162, + 0.7507029558638536, + 0.7726492209025212, + 0.824909687042792, + 0.8302867088236117, + 0.8356514262277522, + 0.8364596821057044, + 0.843973487179215, + 0.8529273383012377, + 0.8886201166862017, + 0.8892458821527571, + 0.894488238153153, + 0.9217288903029234, + 0.954511012483181, + 0.9657954042968545, + 0.9834536157637693, + 0.9955225558982556, + 1.0078480278951047, + 1.011430803091598, + 1.0136908214555849, + 1.0158332545073805, + 1.0174830194145026, + 1.0299392884172376, + 1.0389310281954847, + 1.0549709967729324, + 1.056944525841806, + 1.067179156909234, + 1.1064319054710647, + 1.148932414928497, + 1.1510510723341476, + 1.1538905363358136, + 1.162853832162505, + 1.1835402103377461, + 1.1881866089844066, + 1.205485792496495, + 1.2141943282063825, + 1.2643202272874934, + 1.2729944979654988, + 1.2829703599261217, + 1.3002233632614542, + 1.3168443518223818, + 1.3537939757960271, + 1.364182550201667, + 1.3999632339205146, + 1.4475072287372552, + 1.597423081303548, + 1.598368750555247, + 1.6142684859321508, + 1.645609776871833, + 1.6847711766969609, + 1.7171318358619652, + 1.755719372973814, + 1.8028865134607663, + 1.8114520149849438, + 1.8124706414607625, + 1.8439820664244844, + 1.8654418675004216, + 1.885814677918207, + 1.8862143449236581, + 1.8954509524453798, + 1.9186941583387385, + 2.0143605965464504, + 2.141640218692706, + 2.2585650819572285, + 2.267285307767844, + 2.2884016983859965, + 2.3045055927253033, + 2.3606220804426625, + 2.371347180833694, + 2.426673171068079, + 2.4481241419163755, + 2.5738970558594723, + 2.677714403873397, + 2.711729618794262, + 2.7776171635623226, + 2.940020819126196, + 3.0312089020872115, + 3.2171029690507713, + 3.234638806868786, + 3.57391282146812, + 3.7441531593853385, + 3.86893401199373, + 3.999828991520581, + 4.188454773635135, + 4.416394570345562, + 5.843853651476255 + ], + [ + 0.0, + 2.7923417276815345e-05, + 7.769008327220789e-05, + 0.0002269394511440292, + 0.0003272896025118625, + 0.0010882276680188535, + 0.0010888736352869899, + 0.0022364503813243975, + 0.0026946149208003736, + 0.002730929510578571, + 0.0032295017447794352, + 0.0033115326709048367, + 0.0036160356887750844, + 0.0037605702068906335, + 0.003895418630019178, + 0.004667735768759269, + 0.005392958413298337, + 0.006821023598885239, + 0.007299457154646158, + 0.008928032302833027, + 0.010811251240215775, + 0.011133538102595054, + 0.011298115739655943, + 0.013522869018382488, + 0.02088347546157644, + 0.026426364486313702, + 0.026506990491790124, + 0.027855015135532464, + 0.030818745218765577, + 0.0325914426144601, + 0.03368940446378741, + 0.03782027329007069, + 0.039843488337300025, + 0.04103288994020664, + 0.04143846538258151, + 0.04725088098755748, + 0.0596598162663241, + 0.060429468493951415, + 0.06827952983095646, + 0.06850043516017743, + 0.0706381993766796, + 0.0716885941818364, + 0.08090844946701468, + 0.08469539833971292, + 0.08948931225670052, + 0.09959987001693571, + 0.10087630554473684, + 0.11219693414755373, + 0.1152249865140927, + 0.12048181478555417, + 0.12170930428330606, + 0.12657352375466033, + 0.1330592581827044, + 0.14079936581705105, + 0.15145234485964892, + 0.1544753047834591, + 0.1580899349467811, + 0.1762668733793818, + 0.1868451277274012, + 0.19714719482418172, + 0.21306121374323933, + 0.22839512936137127, + 0.2391499766869827, + 0.24250430859412883, + 0.2534225264990502, + 0.2802970246899942, + 0.2982610297020775, + 0.3431843733626604, + 0.3479890875684609, + 0.352839479634248, + 0.3535768543291995, + 0.3605002906223391, + 0.3688669865737889, + 0.4031728059247977, + 0.4037884287468513, + 0.4089841202405749, + 0.43653417469652633, + 0.4708057559940756, + 0.4828987192318351, + 0.5021449622845836, + 0.515522046707818, + 0.5293735228590362, + 0.5334385878993207, + 0.5360152655326789, + 0.5384678860625198, + 0.5403635717938196, + 0.5547861302173622, + 0.5653033404714854, + 0.5842722092013043, + 0.5866250320464281, + 0.59889563022402, + 0.6468038549877175, + 0.7001573334217688, + 0.7028575571708067, + 0.706481802133849, + 0.7179618397075991, + 0.7446814370010756, + 0.7507235960685785, + 0.7733374951621886, + 0.7847892475240044, + 0.8516238917349727, + 0.8633444661650319, + 0.8768566508875298, + 0.9003203303245118, + 0.9230389353951772, + 0.9739892241699871, + 0.9884122380144139, + 1.0383758542442, + 1.1056355385626973, + 1.3264978229540947, + 1.3279362177076952, + 1.3520327682816298, + 1.3997139458281571, + 1.4598465386200907, + 1.509986190969893, + 1.570271636274001, + 1.6448395304878873, + 1.6584237231518466, + 1.660022926459981, + 1.7092423861118093, + 1.7427309368571293, + 1.7743780623244891, + 1.7749926281645965, + 1.7890077190262765, + 1.8239330479667706, + 1.9683996389887926, + 2.1661203221856082, + 2.355545857374341, + 2.3698610036642034, + 2.4039394464285238, + 2.4295030380784124, + 2.517764194428658, + 2.534423279309027, + 2.619015747151626, + 2.651309265442766, + 2.840263136662384, + 2.999134929514388, + 3.050833879630195, + 3.1487496415794163, + 3.392165818690819, + 3.5309057593609987, + 3.8200269774240176, + 3.847086355740605, + 4.39375644458708, + 4.7046840483642764, + 4.938228417776421, + 5.175800951806387, + 5.499314914678598, + 5.8462373834989245, + 23.025850929940457 + ] + ], + "tv": 0.4063829000962467 + }, + "e2dd8f669a6237d3348396183e939c71a90c61f8a1180f74b944a62e599dbc7e": { + "f": [ + [ + 0.0025017375991542383, + 0.007260746426075224, + 0.011477552524412706, + 0.01983363596737441, + 0.024014015942464967, + 0.0488114016996851, + 0.04882866583560183, + 0.07463929508208995, + 0.08344243282147773, + 0.08406095946785207, + 0.0916836826472264, + 0.09282020353797958, + 0.09668529306953827, + 0.09837660236618552, + 0.09984242926852514, + 0.10764009880084309, + 0.11444300820803756, + 0.12689240969908433, + 0.1307925506000959, + 0.14324502777306058, + 0.1566908284613338, + 0.15886321807749368, + 0.15992305513278954, + 0.1735152694664494, + 0.21494528659312248, + 0.2434306182204962, + 0.24382214495893031, + 0.25012492748282444, + 0.2633509786971155, + 0.270905290883451, + 0.2754090190380725, + 0.29162852733090605, + 0.2992247046404025, + 0.30353789394244657, + 0.3049662434475995, + 0.3246367802861, + 0.36391930371671627, + 0.36623133922498474, + 0.38889985751007333, + 0.3895144693921217, + 0.395319629903312, + 0.398100810183879, + 0.4216188627518788, + 0.43089112364974413, + 0.44226267216170284, + 0.4652913640861715, + 0.4681020407413186, + 0.49219730589208577, + 0.49841764784016895, + 0.5089423756182937, + 0.5113449358733342, + 0.5206640661206473, + 0.5327561338467365, + 0.5467653005430981, + 0.5654169652434965, + 0.5705689139923802, + 0.5766155617203561, + 0.6060331908443756, + 0.6225037197867944, + 0.6380756794063611, + 0.661346719186576, + 0.6829753584921339, + 0.6976919107619985, + 0.7021921280098916, + 0.7165630236841162, + 0.7507029558638536, + 0.7726492209025212, + 0.824909687042792, + 0.8302867088236117, + 0.8356514262277522, + 0.8364596821057044, + 0.843973487179215, + 0.8529273383012377, + 0.8886201166862017, + 0.8892458821527571, + 0.894488238153153, + 0.9217288903029234, + 0.954511012483181, + 0.9657954042968545, + 0.9834536157637693, + 0.9955225558982556, + 1.0078480278951047, + 1.011430803091598, + 1.0136908214555849, + 1.0158332545073805, + 1.0174830194145026, + 1.0299392884172376, + 1.0389310281954847, + 1.0549709967729324, + 1.056944525841806, + 1.067179156909234, + 1.1064319054710647, + 1.148932414928497, + 1.1510510723341476, + 1.1538905363358136, + 1.162853832162505, + 1.1835402103377461, + 1.1881866089844066, + 1.205485792496495, + 1.2141943282063825, + 1.2643202272874934, + 1.2729944979654988, + 1.2829703599261217, + 1.3002233632614542, + 1.3168443518223818, + 1.3537939757960271, + 1.364182550201667, + 1.3999632339205146, + 1.4475072287372552, + 1.597423081303548, + 1.598368750555247, + 1.6142684859321508, + 1.645609776871833, + 1.6847711766969609, + 1.7171318358619652, + 1.755719372973814, + 1.8028865134607663, + 1.8114520149849438, + 1.8124706414607625, + 1.8439820664244844, + 1.8654418675004216, + 1.885814677918207, + 1.8862143449236581, + 1.8954509524453798, + 1.9186941583387385, + 2.0143605965464504, + 2.141640218692706, + 2.2585650819572285, + 2.267285307767844, + 2.2884016983859965, + 2.3045055927253033, + 2.3606220804426625, + 2.371347180833694, + 2.426673171068079, + 2.4481241419163755, + 2.5738970558594723, + 2.677714403873397, + 2.711729618794262, + 2.7776171635623226, + 2.940020819126196, + 3.0312089020872115, + 3.2171029690507713, + 3.234638806868786, + 3.57391282146812, + 3.7441531593853385, + 3.86893401199373, + 3.999828991520581, + 4.188454773635135, + 4.416394570345562, + 5.843853651476255 + ], + [ + 1.0, + 0.9999720769725782, + 0.9999223129345242, + 0.9997730862976654, + 0.9996727639508874, + 0.9989123642369813, + 0.9989117189724986, + 0.9977660486105254, + 0.9973090122952704, + 0.9972727960851991, + 0.9967757074867307, + 0.9966939444059028, + 0.9963904942950201, + 0.9962464918819827, + 0.9961121586710194, + 0.9953431411796896, + 0.9946215574806649, + 0.9932021867797528, + 0.9927271191791448, + 0.9911117042332772, + 0.9892469802956253, + 0.9889282103606679, + 0.9887654682848044, + 0.9865681542144927, + 0.9793330742554818, + 0.9739197562767541, + 0.9738412361825742, + 0.9725293586174101, + 0.969651311085132, + 0.9679339353746957, + 0.9668717640594816, + 0.9628859816702314, + 0.9609398256732647, + 0.9597975617442474, + 0.959408370352123, + 0.9538480652250729, + 0.9420849610585441, + 0.9413601622269476, + 0.9339993564016854, + 0.9337930537538834, + 0.931798956592153, + 0.9308207136692839, + 0.9222781226333767, + 0.918792107383389, + 0.9143980379235828, + 0.9051995430604227, + 0.904044851305478, + 0.8938682069923785, + 0.8911656211036426, + 0.8864932082678938, + 0.8854057147449024, + 0.8811093646931734, + 0.8754132151754732, + 0.8686635778224064, + 0.8594588389286167, + 0.8568646523389536, + 0.8537729944867721, + 0.8383942083829677, + 0.8295722041696428, + 0.8210697671974193, + 0.8081066684683066, + 0.7958097499052387, + 0.7872967973525702, + 0.7846603667883898, + 0.7761398728691234, + 0.7555592883599227, + 0.7421076023369176, + 0.7095073852376861, + 0.7061065815120726, + 0.7026899803773545, + 0.7021720255542867, + 0.6973273724440616, + 0.6915173854299399, + 0.6681966210111389, + 0.667785390516052, + 0.6643247815561681, + 0.6462724113452888, + 0.6244988717933261, + 0.6169923096642389, + 0.6052310686083283, + 0.597188752804976, + 0.5889738327669989, + 0.5865844755662821, + 0.5850749820422767, + 0.5836417734067223, + 0.58253642005699, + 0.5741950510362066, + 0.5681877662689606, + 0.5575114659118627, + 0.5562012821180344, + 0.5494180618751521, + 0.5237169800479596, + 0.49650718047222964, + 0.4951683084324895, + 0.493376945314991, + 0.4877453467756565, + 0.474885556813336, + 0.472024873794203, + 0.46147034029138145, + 0.4562158402724972, + 0.42672141962020116, + 0.421749195044934, + 0.41608877048871773, + 0.4064394440066954, + 0.39730980578950004, + 0.37757380322497724, + 0.372167134984978, + 0.35402921031295187, + 0.33100045182933513, + 0.2654051313276895, + 0.2650236484064987, + 0.2587138205134082, + 0.24666751412201138, + 0.23227191676371173, + 0.22091302853297745, + 0.20798867741360472, + 0.19304353791075934, + 0.19043892811223306, + 0.19013462093777267, + 0.1810028709731839, + 0.1750417194073539, + 0.16958889042753345, + 0.16948469890816864, + 0.16712592329025389, + 0.1613897474196243, + 0.13968021621112603, + 0.11462144968543964, + 0.0948417223489797, + 0.09349372065954353, + 0.09036127778562086, + 0.08808059440894322, + 0.08063970003833898, + 0.07930744432225967, + 0.07287455452813596, + 0.07055877239656838, + 0.05841029405230691, + 0.04983015632564736, + 0.04731944920984543, + 0.042905740896099664, + 0.033635749019890304, + 0.029278384682419167, + 0.021927209346658372, + 0.02134182843335808, + 0.01235423395141233, + 0.00905277400485649, + 0.007167284565607113, + 0.005651688370613139, + 0.004089572184854062, + 0.002890755525547517, + 0.0 + ] + ], + "tv": 0.6660550860361099 + }, + "ed9223db5757fc9abfe82ef8b5e3403215f448dfa919749dd84d138c9d47e8c6": { + "f": [ + [ + 0.0025017375991542383, + 0.007260746426075224, + 0.011477552524412706, + 0.01983363596737441, + 0.024014015942464967, + 0.0488114016996851, + 0.04882866583560183, + 0.07463929508208995, + 0.08344243282147773, + 0.08406095946785207, + 0.0916836826472264, + 0.09282020353797958, + 0.09668529306953827, + 0.09837660236618552, + 0.09984242926852514, + 0.10764009880084309, + 0.11444300820803756, + 0.12689240969908433, + 0.1307925506000959, + 0.14324502777306058, + 0.1566908284613338, + 0.15886321807749368, + 0.15992305513278954, + 0.1735152694664494, + 0.21494528659312248, + 0.2434306182204962, + 0.24382214495893031, + 0.25012492748282444, + 0.2633509786971155, + 0.270905290883451, + 0.2754090190380725, + 0.29162852733090605, + 0.2992247046404025, + 0.30353789394244657, + 0.3049662434475995, + 0.3246367802861, + 0.36391930371671627, + 0.36623133922498474, + 0.38889985751007333, + 0.3895144693921217, + 0.395319629903312, + 0.398100810183879, + 0.4216188627518788, + 0.43089112364974413, + 0.44226267216170284, + 0.4652913640861715, + 0.4681020407413186, + 0.49219730589208577, + 0.49841764784016895, + 0.5089423756182937, + 0.5113449358733342, + 0.5206640661206473, + 0.5327561338467365, + 0.5467653005430981, + 0.5654169652434965, + 0.5705689139923802, + 0.5766155617203561, + 0.6060331908443756, + 0.6225037197867944, + 0.6380756794063611, + 0.661346719186576, + 0.6829753584921339, + 0.6976919107619985, + 0.7021921280098916, + 0.7165630236841162, + 0.7507029558638536, + 0.7726492209025212, + 0.824909687042792, + 0.8302867088236117, + 0.8356514262277522, + 0.8364596821057044, + 0.843973487179215, + 0.8529273383012377, + 0.8886201166862017, + 0.8892458821527571, + 0.894488238153153, + 0.9217288903029234, + 0.954511012483181, + 0.9657954042968545, + 0.9834536157637693, + 0.9955225558982556, + 1.0078480278951047, + 1.011430803091598, + 1.0136908214555849, + 1.0158332545073805, + 1.0174830194145026, + 1.0299392884172376, + 1.0389310281954847, + 1.0549709967729324, + 1.056944525841806, + 1.067179156909234, + 1.1064319054710647, + 1.148932414928497, + 1.1510510723341476, + 1.1538905363358136, + 1.162853832162505, + 1.1835402103377461, + 1.1881866089844066, + 1.205485792496495, + 1.2141943282063825, + 1.2643202272874934, + 1.2729944979654988, + 1.2829703599261217, + 1.3002233632614542, + 1.3168443518223818, + 1.3537939757960271, + 1.364182550201667, + 1.3999632339205146, + 1.4475072287372552, + 1.597423081303548, + 1.598368750555247, + 1.6142684859321508, + 1.645609776871833, + 1.6847711766969609, + 1.7171318358619652, + 1.755719372973814, + 1.8028865134607663, + 1.8114520149849438, + 1.8124706414607625, + 1.8439820664244844, + 1.8654418675004216, + 1.885814677918207, + 1.8862143449236581, + 1.8954509524453798, + 1.9186941583387385, + 2.0143605965464504, + 2.141640218692706, + 2.2585650819572285, + 2.267285307767844, + 2.2884016983859965, + 2.3045055927253033, + 2.3606220804426625, + 2.371347180833694, + 2.426673171068079, + 2.4481241419163755, + 2.5738970558594723, + 2.677714403873397, + 2.711729618794262, + 2.7776171635623226, + 2.940020819126196, + 3.0312089020872115, + 3.2171029690507713, + 3.234638806868786, + 3.57391282146812, + 3.7441531593853385, + 3.86893401199373, + 3.999828991520581, + 4.188454773635135, + 4.416394570345562, + 5.843853651476255 + ], + [ + 1.0, + 0.9999999366823404, + 0.9999997239441103, + 0.9999985052279857, + 0.9999973919634457, + 0.9999804545652561, + 0.9999804362626797, + 0.9999384837180679, + 0.9999170562038504, + 0.9999153958528035, + 0.9998928847680254, + 0.9998891947740998, + 0.9998758936365097, + 0.9998696901076629, + 0.999864107337261, + 0.9998308505174082, + 0.9997966785357084, + 0.9997204997210913, + 0.9996928089289633, + 0.9995908720771465, + 0.9994561976652087, + 0.999431912838101, + 0.9994197930820221, + 0.9992472657928171, + 0.9985094156577183, + 0.9977986023865713, + 0.9977875837059368, + 0.9976051231234953, + 0.9971899385490012, + 0.9969323432178333, + 0.9967713507881472, + 0.9961428546391329, + 0.9958213390593887, + 0.9956306943684918, + 0.9955662295141966, + 0.9946070962089749, + 0.9922679938375706, + 0.9921117617270403, + 0.9904613123734615, + 0.990413503494826, + 0.9899535220849169, + 0.9897276836818205, + 0.9876691980562141, + 0.986782215120468, + 0.9856332245738423, + 0.9830902167091559, + 0.9827594168946583, + 0.9797305517073054, + 0.9788911267678375, + 0.9774146994853824, + 0.9770675875960104, + 0.9756845298246053, + 0.9738009894025573, + 0.9714891903946907, + 0.9681873886505347, + 0.9672290822630782, + 0.9660780136851274, + 0.9600581518691615, + 0.9563748768234391, + 0.952678236312191, + 0.9467530453834516, + 0.940802877700085, + 0.9365037142178346, + 0.9351477949375825, + 0.930685389744629, + 0.9192602594298566, + 0.9112938524354635, + 0.8903307411497843, + 0.8880147375817298, + 0.8856742415629105, + 0.8853190274972601, + 0.8819838270386174, + 0.8779313110327361, + 0.8609321415604575, + 0.8606222006868105, + 0.8580094470363654, + 0.8439677696612726, + 0.8260592444146038, + 0.8196475769310654, + 0.8093669336501933, + 0.8021704917590744, + 0.7946818267684318, + 0.7924790510548386, + 0.7910835353271854, + 0.7897563101677108, + 0.788731408797706, + 0.7809119231104993, + 0.7751797276554351, + 0.7647763830548356, + 0.7634810068899652, + 0.756709781812528, + 0.7299531971109843, + 0.6997493083935991, + 0.6982147511599224, + 0.6961541134122643, + 0.6896195237094984, + 0.6743755662856993, + 0.6709227787678462, + 0.6579821741462064, + 0.6514209635520792, + 0.613178024263694, + 0.6065013213719059, + 0.5988089070329327, + 0.5854789394616243, + 0.5726204422826446, + 0.5440586000203396, + 0.5360537689459719, + 0.5086426657482139, + 0.47280864094289166, + 0.3675066124221281, + 0.3668905103486614, + 0.35663285199963235, + 0.33698123267765645, + 0.3135158972901366, + 0.2950605284467266, + 0.2741709981876129, + 0.2502855379981239, + 0.24614130536579726, + 0.24565236606148355, + 0.2309268160397142, + 0.22133703604275656, + 0.21255435327792271, + 0.21238514593589175, + 0.2085068520197222, + 0.1990165323781855, + 0.1638344757661856, + 0.12586768846293495, + 0.09860514760598882, + 0.09682716277905379, + 0.09265425553571194, + 0.08959364101847311, + 0.07969563408336011, + 0.07793267028417294, + 0.06943505529241556, + 0.06639425237501251, + 0.0510376060515384, + 0.04107571152755152, + 0.03825819655430718, + 0.03333654129358665, + 0.02372138893486153, + 0.019592299369875277, + 0.013255152441378981, + 0.012775992462260022, + 0.006225242132031572, + 0.004360118673299995, + 0.003378824201544006, + 0.0026006679784873477, + 0.0017958077241223558, + 0.0011506111088074311, + 0.0 + ] + ], + "tv": 0.85931944645117 + } + }, + "label": "cdf14.univariate", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/invariants/test_copula.py b/tests/invariants/test_copula.py index 63b35d77..0a873a6d 100644 --- a/tests/invariants/test_copula.py +++ b/tests/invariants/test_copula.py @@ -6,6 +6,15 @@ from nns import nns_copula +def _disable_native(monkeypatch: pytest.MonkeyPatch) -> None: + """Run NNS.copula on the NumPy fallbacks of the kernels it calls.""" + import importlib + + for module in ("nns.core", "nns.pm_matrix"): + monkeypatch.setattr(importlib.import_module(module), "native_fn", lambda name: None) + + + def test_nns_copula_is_bounded() -> None: x = np.linspace(-2.0, 2.0, 200) y = np.sin(x) @@ -66,9 +75,7 @@ def test_nns_copula_is_zero_when_every_margin_is_one_sided( ) -> None: # R NNS.copula: each pair is then trivially concordant or discordant; these returned 1. if not native: - import nns.copula as copula - - monkeypatch.setattr(copula, "native_fn", lambda name: None) + _disable_native(monkeypatch) g = _grid(range(1, 11), range(1, 11)) x = np.arange(1.0, 11.0) b = np.column_stack([np.tile([0.0, 1.0], 50), np.tile([1.0, 0.0], 50)]) @@ -92,9 +99,7 @@ def test_nns_copula_is_one_for_perfect_dependence_inside_the_data( monkeypatch: pytest.MonkeyPatch, native: bool, continuous: bool ) -> None: if not native: - import nns.copula as copula - - monkeypatch.setattr(copula, "native_fn", lambda name: None) + _disable_native(monkeypatch) x = np.arange(1.0, 11.0) assert nns_copula(np.column_stack([x, x]), continuous=continuous) == 1.0 assert nns_copula(np.column_stack([x, -x]), continuous=continuous) == 1.0 diff --git a/tests/parity/test_cdf_copula_bits_r14.py b/tests/parity/test_cdf_copula_bits_r14.py new file mode 100644 index 00000000..d2d27e98 --- /dev/null +++ b/tests/parity/test_cdf_copula_bits_r14.py @@ -0,0 +1,155 @@ +"""R 14.0 parity, to the last bit, for NNS.CDF, NNS.copula, NNS.rescale and the +many-target partial moments. + +* Beyond 32 targets R's ``LPM``/``UPM`` (and so ``LPM.ratio`` and ``NNS.CDF``) + switch to a sorted-anchor prefix backend for integer degrees; the NumPy + fallback reproduces its recurrence. +* ``NNS.CDF`` takes ``-log`` with libm, the joint survival with R's long-double + ``rowSums``, and the joint CDF with R's batch kernel. +* ``NNS.copula`` sums with R's long-double ``sum``/``prod``/``mean`` and returns + ``mean(...)^(1/2)``, which is ``pow``, not ``sqrt``. +* ``NNS.rescale(method = "riskneutral")`` takes the mean in one long-double pass. + +Each case runs with the native kernels and with the NumPy fallbacks. +""" + +from __future__ import annotations + +import importlib +from typing import Any + +import numpy as np +import pytest +from _r import nns_frame_call, r_column + +import nns + +pytestmark = pytest.mark.parity + + +def _num(values: Any) -> dict[str, Any]: + return r_column([float(v) for v in np.asarray(values, dtype=np.float64).ravel()], "numeric") + + +def _disable_native(monkeypatch: pytest.MonkeyPatch) -> None: + for module in ("nns.core", "nns.pm_matrix", "nns.cdf"): + monkeypatch.setattr(importlib.import_module(module), "native_fn", lambda name: None) + + +@pytest.mark.parametrize("native", [True, False]) +@pytest.mark.parametrize("degree", [0, 1, 2, 3]) +def test_many_target_partial_moments_match_r_bitwise( + monkeypatch: pytest.MonkeyPatch, native: bool, degree: int +) -> None: + rng = np.random.default_rng(501) + x = np.round(rng.normal(size=160) * 3.0, 1) + targets = np.concatenate([np.sort(x), [x.min() - 1.0, x.max() + 1.0]]) + expected = nns_frame_call( + "cdf14.prefix", + f"list(lpm = LPM({degree}, t, x), upm = UPM({degree}, t, x), " + f"ratio = LPM.ratio({degree}, t, x))", + {"x": _num(x), "t": _num(targets)}, + exact=True, + ) + if not native: + _disable_native(monkeypatch) + np.testing.assert_array_equal(nns.lpm(degree, targets, x), expected["lpm"]) + np.testing.assert_array_equal(nns.upm(degree, targets, x), expected["upm"]) + np.testing.assert_array_equal(nns.lpm_ratio(degree, targets, x), expected["ratio"]) + + +@pytest.mark.parametrize("native", [True, False]) +@pytest.mark.parametrize("degree", [1, 2]) +@pytest.mark.parametrize("kind", ["CDF", "survival", "cumulative hazard"]) +def test_univariate_cdf_matches_r_bitwise( + monkeypatch: pytest.MonkeyPatch, native: bool, degree: int, kind: str +) -> None: + rng = np.random.default_rng(502) + x = rng.exponential(size=150) + target = float(np.median(x)) + expected = nns_frame_call( + "cdf14.univariate", + f"r <- NNS.CDF(x, degree = {degree}, target = tg, type = '{kind}', plot = FALSE); " + "list(f = unname(as.list(r$Function)), tv = r$target.value)", + {"x": _num(x), "tg": _num([target])}, + exact=True, + ) + if not native: + _disable_native(monkeypatch) + actual = nns.nns_cdf(x, degree=degree, target=target, type=kind) + columns = list(actual["Function"].values()) + for column, want in zip(columns, expected["f"], strict=True): + np.testing.assert_array_equal(column, np.asarray(want, dtype=np.float64)) + np.testing.assert_array_equal( + np.atleast_1d(actual["target.value"]), np.atleast_1d(expected["tv"]) + ) + + +@pytest.mark.parametrize("native", [True, False]) +@pytest.mark.parametrize( + ("columns", "degree", "kind"), + [(3, 0, "survival"), (3, 0, "cumulative hazard"), (2, 1, "CDF"), (3, 2, "survival")], +) +def test_multivariate_cdf_matches_r_bitwise( + monkeypatch: pytest.MonkeyPatch, native: bool, columns: int, degree: int, kind: str +) -> None: + rng = np.random.default_rng(503) + data = rng.normal(size=(60, columns)) + target = np.median(data, axis=0) + expected = nns_frame_call( + "cdf14.multivariate", + f"r <- NNS.CDF(matrix(v, ncol = {columns}), degree = {degree}, target = tg, " + f"type = '{kind}', plot = FALSE); list(cdf = r$Function$CDF, tv = r$target.value)", + {"v": _num(data.ravel(order="F")), "tg": _num(target)}, + exact=True, + ) + if not native: + _disable_native(monkeypatch) + actual = nns.nns_cdf(data, degree=degree, target=target, type=kind) + np.testing.assert_array_equal(actual["Function"]["CDF"], np.asarray(expected["cdf"])) + np.testing.assert_array_equal( + np.atleast_1d(actual["target.value"]), np.atleast_1d(expected["tv"]) + ) + + +@pytest.mark.parametrize("native", [True, False]) +@pytest.mark.parametrize("continuous", [True, False]) +@pytest.mark.parametrize("columns", [2, 3, 4]) +def test_nns_copula_matches_r_bitwise( + monkeypatch: pytest.MonkeyPatch, native: bool, continuous: bool, columns: int +) -> None: + rng = np.random.default_rng(504) + base = rng.normal(size=300) + data = np.column_stack( + [base * (j + 1) * 0.4 + rng.normal(size=300) for j in range(columns)] + ) + flag = "TRUE" if continuous else "FALSE" + expected = nns_frame_call( + "cdf14.copula", + f"NNS.copula(matrix(v, ncol = {columns}), continuous = {flag})", + {"v": _num(data.ravel(order="F"))}, + exact=True, + ) + if not native: + _disable_native(monkeypatch) + assert nns.nns_copula(data, continuous=continuous) == expected + + +@pytest.mark.parametrize("kind", ["Terminal", "Discounted"]) +def test_risk_neutral_rescale_matches_r_bitwise(kind: str) -> None: + rng = np.random.default_rng(505) + x = np.concatenate([rng.normal(-40.0, 5.0, 30), rng.normal(10.0, 2.0, 50), [1e4]]) + expected = nns_frame_call( + "cdf14.rescale", + f"NNS.rescale(x, 100, 0.05, method = 'riskneutral', T = 2, type = '{kind}')", + {"x": _num(x)}, + exact=True, + ) + actual = nns.nns_rescale(x, 100, 0.05, method="riskneutral", time_to_maturity=2, type=kind) + np.testing.assert_array_equal(actual, np.asarray(expected, dtype=np.float64)) + + +def test_minmax_rescale_warns_on_identical_values_like_r() -> None: + with pytest.warns(UserWarning, match="All x identical: returning midpoint values"): + out = nns.nns_rescale(np.full(4, 2.0), 0.0, 1.0) + np.testing.assert_array_equal(out, np.full(4, 0.5)) diff --git a/tests/parity/test_copula.py b/tests/parity/test_copula.py index 2d69ef54..3603ee46 100644 --- a/tests/parity/test_copula.py +++ b/tests/parity/test_copula.py @@ -7,6 +7,15 @@ from nns import nns_copula + +def _disable_native(monkeypatch: pytest.MonkeyPatch) -> None: + """Run NNS.copula on the NumPy fallbacks of the kernels it calls.""" + import importlib + + for module in ("nns.core", "nns.pm_matrix"): + monkeypatch.setattr(importlib.import_module(module), "native_fn", lambda name: None) + + SIZES = [50, 200, 1000] RELATIONSHIPS = ["correlated_normal", "independent", "anti_monotonic"] @@ -105,9 +114,7 @@ def test_nns_copula_perfect_dependence_shortcut_is_dimension_generic( m = _integer_matrices()[case] expected = _scalar(nns("NNS.copula", m.tolist(), None, continuous, False, False)) if not native: - import nns.copula as copula - - monkeypatch.setattr(copula, "native_fn", lambda name: None) + _disable_native(monkeypatch) actual = nns_copula(m, continuous=continuous) np.testing.assert_allclose(actual, expected, atol=EXACT) if case < 3: @@ -129,9 +136,7 @@ def test_nns_copula_degree0_ties_partition_the_sample( _scalar(nns("NNS.copula", c.tolist(), None, True, False, False)) for c in cases ] if not native: - import nns.copula as copula - - monkeypatch.setattr(copula, "native_fn", lambda name: None) + _disable_native(monkeypatch) for c, want in zip(cases, expected, strict=True): np.testing.assert_allclose(nns_copula(c), want, atol=EXACT) @@ -155,9 +160,7 @@ def test_nns_copula_mixed_sign_perfect_dependence_is_one( _scalar(nns("NNS.copula", c.tolist(), None, continuous, False, False)) for c in cases ] if not native: - import nns.copula as copula - - monkeypatch.setattr(copula, "native_fn", lambda name: None) + _disable_native(monkeypatch) for c, want in zip(cases, expected, strict=True): assert want == 1.0 assert nns_copula(c, continuous=continuous) == 1.0 @@ -184,9 +187,7 @@ def test_nns_copula_margin_anchors_match_r( _scalar(nns("NNS.copula", m.tolist(), t, continuous, False, False)) for m, t in cases ] if not native: - import nns.copula as copula - - monkeypatch.setattr(copula, "native_fn", lambda name: None) + _disable_native(monkeypatch) for (m, t), want in zip(cases, expected, strict=True): target = None if t is None else np.asarray(t) actual = nns_copula(m, continuous=continuous, target=target) From 35915059ce6042b2c25d4693c03c93be75ceee2c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 01:28:07 +0000 Subject: [PATCH 11/16] Match R's NNS.distance kernels and dy.d_ widths to the last bit nns_distance now reproduces R 14's NNS_distance_cpp and nns_distance_bulk its NNS_distance_path_single_parallel_cpp: columns scaled by 1 / range, distances accumulated column by column, R's nmath dt, dexp, dlnorm and dnorm instead of SciPy's densities, sequential double sums, and the bulk kernel's own unnormalised Student-t and Gaussian kernels with its 1 / total scaling. k follows R (truncated and clamped to 1..n; "all" or an infinite k uses every row), and the classification vote breaks equal totals towards the smallest class value, as R scans classes in order. R orders candidates with libstdc++'s unstable std::sort and std::partial_sort, so tied distances land where those algorithms leave them. nns._stdsort replays both comparison for comparison (checked against compiled libstdc++ on 400 tie-heavy inputs); it runs only when distances tie. dy.d_'s window widths use R's log(x, 2) and log(x, 10), which are the C log2 and log10 rather than log(x) / log(base); the old ratio differed in the last bit for about half of all sample sizes. An unused helper with the same formula is removed. A live sweep of 928 distance cases, including tie-heavy and classification data, matches R exactly. New bitwise parity tests cover ties in both kernels and pin the sort replay to libstdc++ output. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt --- docs/api_status.md | 2 +- docs/conventions.md | 43 ++-- src/nns/_stdsort.py | 176 ++++++++++++++ src/nns/diff.py | 13 +- src/nns/distance.py | 320 +++++++++++++++++-------- tests/_r_cache/dist14.bulk.json | 43 ++++ tests/_r_cache/dist14.continuous.json | 11 + tests/_r_cache/dist14.scalar.json | 15 ++ tests/parity/test_distance_bits_r14.py | 126 ++++++++++ 9 files changed, 618 insertions(+), 131 deletions(-) create mode 100644 src/nns/_stdsort.py create mode 100644 tests/_r_cache/dist14.bulk.json create mode 100644 tests/_r_cache/dist14.continuous.json create mode 100644 tests/_r_cache/dist14.scalar.json create mode 100644 tests/parity/test_distance_bits_r14.py diff --git a/docs/api_status.md b/docs/api_status.md index d4a1f6aa..95d515bf 100644 --- a/docs/api_status.md +++ b/docs/api_status.md @@ -46,7 +46,7 @@ invariant, and property coverage. | Copula: `nns_copula` | implemented | high | Bivariate scalar public form is implemented. | | Causation: `nns_causation`, `causal_matrix` | implemented | medium | Numeric lag paths and `tau="ts"` behavior are covered; some internal asymmetry granularity can differ in regression dimension reduction. | | Distribution functions: `nns_cdf` | implemented | high | Deterministic non-plotting paths are implemented; plotting is ignored. | -| Distance helpers: `nns_distance`, `nns_distance_bulk` | implemented | high | Numeric and classification conventions follow installed R behavior. | +| Distance helpers: `nns_distance`, `nns_distance_bulk` | implemented | high | R 14's C++ kernels to the last bit, including where tied distances fall and the classification vote. | | Partitioning: `nns_part` | implemented | high | Returns plain dictionaries/arrays instead of R `data.table` objects. | | Regression: `nns_reg` | implemented | high | Numeric, class-code, confidence interval, smoothing, dimension-reduction, and factor paths are covered; `point_est` columns are matched by name, categorical/declared levels are honoured. | | Multivariate regression: `nns_m_reg` | implemented | high | Numeric, class, and raw factor/character predictor paths (`factor_2_dummy=True` by default, as R 13.3) are covered; column names are kept. | diff --git a/docs/conventions.md b/docs/conventions.md index 686b3e9b..3439546e 100644 --- a/docs/conventions.md +++ b/docs/conventions.md @@ -640,25 +640,30 @@ more columns. ## Distance -`nns_distance` and `nns_distance_bulk` map to R's regression-point-matrix -helpers. NNS Python accepts `rpm` as a finite 2D numeric array with R's `y.hat` -column in the final position. `nns_distance` applies R's per-target min-max -rescaling before computing weighted nearest-neighbor predictions. `nns_distance_bulk` -matches R's compiled bulk helper, including its raw-feature distance convention. -For `nns_distance` with `k > 1`, NNS Python matches the installed R 13.0 binary: -the exponential rank-weight family uses the R C API's `Rf_dexp` scale argument -as `1 / k`. This differs from the nearby source-code comment that describes it -as a rate. - -Classification distance mode returns numeric class codes, not original labels. -For single-target `nns_distance(..., class_=...)`, installed R uses weighted -mode with integer replication counts `ceil(100 * weight)`. NNS Python follows that -behavior. For equal-distance nearest-neighbor ties, NNS Python preserves RPM row order -to match installed R's first-row tie behavior. Installed R's -`NNS.distance.bulk(..., class=...)` currently ignores -the class flag in its compiled bulk helper and returns the same inverse-distance -numeric weighted average as non-class bulk distance; NNS Python matches the installed -binary rather than the higher-level classification intent. +`nns_distance` and `nns_distance_bulk` map to R 14's `NNS.distance` and +`NNS.distance.bulk`. NNS Python accepts `rpm` as a finite 2D numeric array with +R's `y.hat` column in the final position. Both reproduce R's C++ kernels to the +last bit: each column is scaled by `1 / range` (the range including the +target), the distance accumulates `diff^2 + |diff|` column by column, and the +weights are R's nmath densities summed in R's order. + +* `nns_distance` is `NNS_distance_cpp`: the `k` nearest rows in the order + libstdc++'s `std::partial_sort` (or `std::sort` when every row is used) + leaves them, which decides where tied distances fall, then the eight weights + (uniform, `dt`, inverse distance, `dexp` with scale `k`, reversed + `|dlnorm(rank, 0, sd(1:k), log = TRUE)|`, `rank^-2`, `dnorm` and an RBF), + normalised and combined. +* `nns_distance_bulk` is `NNS_distance_path_single_parallel_cpp`, which R's + `NNS.distance.bulk` calls: the `k` nearest rows ordered by distance and then + row, with unnormalised Student-t and Gaussian kernels, and the weighted sum + scaled by `1 / total`. + +`k` follows R: `"all"` or an infinite value uses every row; otherwise it is +truncated to an integer and clamped to `1..nrow(rpm)`, so `k = 0` uses the single +nearest row. Classification mode (`class_` not `None`) returns numeric class +codes, not original labels: the weighted mode with `ceil(100 * weight)` votes, +where equal vote totals go to the smallest class value, as R scans the classes +in ascending order. ## Classification diff --git a/src/nns/_stdsort.py b/src/nns/_stdsort.py new file mode 100644 index 00000000..c816c96e --- /dev/null +++ b/src/nns/_stdsort.py @@ -0,0 +1,176 @@ +"""libstdc++'s ``std::sort`` and ``std::partial_sort``, comparison for comparison. + +R's C++ kernels order candidates with ``std::sort`` / ``std::partial_sort``, +which are not stable: among equal keys the order is whatever libstdc++'s +introsort and heap select leave behind. Where that order feeds a result (rank +weights, a nearest-neighbour pick), the port replays the same algorithms on +the same comparisons (``key[a] < key[b]``) so ties land where R puts them. + +Each function sorts ``order``, a list of indices into ``key``, in place. +""" + +from __future__ import annotations + +from collections.abc import Sequence + +_S_THRESHOLD = 16 + + +def _adjust_heap( + order: list[int], key: Sequence[float], base: int, hole: int, length: int, value: int +) -> None: + top = hole + second = hole + while second < (length - 1) // 2: + second = 2 * (second + 1) + if key[order[base + second]] < key[order[base + second - 1]]: + second -= 1 + order[base + hole] = order[base + second] + hole = second + if (length & 1) == 0 and second == (length - 2) // 2: + second = 2 * (second + 1) + order[base + hole] = order[base + second - 1] + hole = second - 1 + # __push_heap + parent = (hole - 1) // 2 + while hole > top and key[order[base + parent]] < key[value]: + order[base + hole] = order[base + parent] + hole = parent + parent = (hole - 1) // 2 + order[base + hole] = value + + +def _make_heap(order: list[int], key: Sequence[float], first: int, last: int) -> None: + length = last - first + if length < 2: + return + parent = (length - 2) // 2 + while True: + _adjust_heap(order, key, first, parent, length, order[first + parent]) + if parent == 0: + return + parent -= 1 + + +def _pop_heap(order: list[int], key: Sequence[float], first: int, last: int, result: int) -> None: + value = order[result] + order[result] = order[first] + _adjust_heap(order, key, first, 0, last - first, value) + + +def heap_select(order: list[int], key: Sequence[float], middle: int) -> None: + """``std::__heap_select(begin, begin + middle, end)``: the first ``middle`` + slots end up holding the selected (smallest) elements, as a heap.""" + _heap_select(order, key, 0, middle, len(order)) + + +def _heap_select( + order: list[int], key: Sequence[float], first: int, middle: int, last: int +) -> None: + _make_heap(order, key, first, middle) + for i in range(middle, last): + if key[order[i]] < key[order[first]]: + _pop_heap(order, key, first, middle, i) + + +def _sort_heap(order: list[int], key: Sequence[float], first: int, last: int) -> None: + while last - first > 1: + last -= 1 + _pop_heap(order, key, first, last, last) + + +def partial_sort(order: list[int], key: Sequence[float], middle: int) -> None: + """``std::partial_sort(begin, begin + middle, end, comp)``.""" + _heap_select(order, key, 0, middle, len(order)) + _sort_heap(order, key, 0, middle) + + +def _unguarded_linear_insert(order: list[int], key: Sequence[float], last: int) -> None: + value = order[last] + previous = last - 1 + while key[value] < key[order[previous]]: + order[last] = order[previous] + last = previous + previous -= 1 + order[last] = value + + +def _insertion_sort(order: list[int], key: Sequence[float], first: int, last: int) -> None: + if first == last: + return + for i in range(first + 1, last): + if key[order[i]] < key[order[first]]: + value = order[i] + order[first + 1 : i + 1] = order[first:i] + order[first] = value + else: + _unguarded_linear_insert(order, key, i) + + +def _final_insertion_sort(order: list[int], key: Sequence[float], first: int, last: int) -> None: + if last - first > _S_THRESHOLD: + _insertion_sort(order, key, first, first + _S_THRESHOLD) + for i in range(first + _S_THRESHOLD, last): + _unguarded_linear_insert(order, key, i) + else: + _insertion_sort(order, key, first, last) + + +def _move_median_to_first( + order: list[int], key: Sequence[float], result: int, a: int, b: int, c: int +) -> None: + ka, kb, kc = key[order[a]], key[order[b]], key[order[c]] + if ka < kb: + if kb < kc: + pick = b + elif ka < kc: + pick = c + else: + pick = a + elif ka < kc: + pick = a + elif kb < kc: + pick = c + else: + pick = b + order[result], order[pick] = order[pick], order[result] + + +def _unguarded_partition( + order: list[int], key: Sequence[float], first: int, last: int, pivot: int +) -> int: + pivot_key = key[order[pivot]] + while True: + while key[order[first]] < pivot_key: + first += 1 + last -= 1 + while pivot_key < key[order[last]]: + last -= 1 + if not first < last: + return first + order[first], order[last] = order[last], order[first] + first += 1 + + +def _introsort_loop( + order: list[int], key: Sequence[float], first: int, last: int, depth_limit: int +) -> None: + while last - first > _S_THRESHOLD: + if depth_limit == 0: + _heap_select(order, key, first, last, last) + _sort_heap(order, key, first, last) + return + depth_limit -= 1 + middle = first + (last - first) // 2 + _move_median_to_first(order, key, first, first + 1, middle, last - 1) + cut = _unguarded_partition(order, key, first + 1, last, first) + _introsort_loop(order, key, cut, last, depth_limit) + last = cut + + +def std_sort(order: list[int], key: Sequence[float]) -> None: + """``std::sort(begin, end, comp)`` (introsort, then a final insertion sort).""" + n = len(order) + if n: + _introsort_loop(order, key, 0, n, 2 * (n.bit_length() - 1)) + _final_insertion_sort(order, key, 0, n) diff --git a/src/nns/diff.py b/src/nns/diff.py index 17e1a9d6..53e2c8b2 100644 --- a/src/nns/diff.py +++ b/src/nns/diff.py @@ -424,15 +424,6 @@ def _r_seq(start: float, stop: float, by: float) -> NDArray[np.float64]: return start + by * np.arange(0, count + 1, dtype=np.float64) -def _v057_bandwidths(x_size: int, wrt_dependence: float) -> NDArray[np.float64]: - """h_s = 1/log(length(x), c(2, 10)); c(h_s, 10*h_s); doubled if dependence < .5.""" - base = np.log(np.asarray([2.0, 10.0])) / np.log(float(x_size)) - h_s = np.concatenate([base, 10.0 * base]) - if wrt_dependence < 0.5: - h_s = 2.0 * h_s - return np.asarray(h_s, dtype=np.float64) - - def _r14_fd_weights(nodes: NDArray[np.float64], at: float, degree: int) -> NDArray[np.float64]: """R .nns_fd_weights: scaled Vandermonde finite-difference coefficients.""" n = int(nodes.size) @@ -564,7 +555,9 @@ def _dy_d_best_plan( if points.size == 0: raise ValueError("eval.points must contain at least one point.") # R: 1/log(max(nrow(x),3), c(2,10)), half and full windows capped at .25. - base = np.log(np.asarray([2.0, 10.0])) / np.log(float(max(n, 3))) + # R's log(x, 2) and log(x, 10) are the C log2 and log10, not log(x)/log(base). + rows_used = float(max(n, 3)) + base = np.array([1.0 / math.log2(rows_used), 1.0 / math.log10(rows_used)]) widths = np.minimum(0.25, np.r_[base / 2.0, base]) if dep_local_r14(x_values[:, wrt_index], y_values, asym=False) < 0.5: widths = np.minimum(0.25, 2.0 * widths) diff --git a/src/nns/distance.py b/src/nns/distance.py index 8cccf4e2..cf784d54 100644 --- a/src/nns/distance.py +++ b/src/nns/distance.py @@ -1,12 +1,15 @@ from __future__ import annotations +import math from collections import defaultdict -from typing import Literal, cast +from typing import Literal import numpy as np from numpy.typing import NDArray from nns._helpers import _as_matrix +from nns._rnmath import dexp, dlnorm_log, dnorm, dt, libm_exp +from nns._stdsort import heap_select, partial_sort, std_sort KValue = int | Literal["all"] @@ -20,27 +23,29 @@ def nns_distance( """Return R's NNS.distance prediction for one target row. ``rpm`` is a numeric matrix whose last column is R's ``y.hat`` column. + The value is R's ``NNS_distance_cpp`` to the last bit: range-scaled + distances, libstdc++'s ordering of tied distances, R's nmath ``dt``, + ``dexp``, ``dlnorm`` and ``dnorm`` weights, and sequential double sums. """ features, y_hat = _split_rpm(rpm) dest = _as_vector(dist_estimate) if dest.size != features.shape[1]: raise ValueError("dist_estimate length must match rpm feature column count.") - - scaled_features, scaled_dest = _rescale_joint(features, dest) - distances = _distance_sum(scaled_features, scaled_dest, zero_eps=1e-10) - indices = np.argsort(distances, kind="mergesort") k_value = _resolve_k(k, features.shape[0]) - selected = indices[:k_value] - selected_distances = distances[selected] - selected_y = y_hat[selected] + col_min = np.minimum(np.min(features, axis=0), dest) + col_max = np.maximum(np.max(features, axis=0), dest) + distances = _distances(features, dest, _inverse_ranges(col_min, col_max)) + order = _cpp_sorted_prefix(distances, k_value) + selected_distances = distances[order] + selected_y = y_hat[order] if k_value == 1: return float(selected_y[0]) - weights = _combined_weights(selected_distances) + weights = _scalar_weights(selected_distances) if class_ is not None: return _weighted_mode(selected_y, weights) - return float(np.dot(selected_y, weights)) + return _sequential_sum(selected_y * weights) def nns_distance_path_single_bulk( @@ -51,9 +56,10 @@ def nns_distance_path_single_bulk( ) -> NDArray[np.float64]: """Return R's NNS.distance.path.single.bulk predictions for many target rows. - This mirrors the ``NNS_distance_path_single_parallel_cpp`` kernel, which - differs from the single-point ``NNS.distance`` kernel in its lognormal rank - weight (population sd of ranks instead of sample sd). + This is R's ``NNS_distance_path_single_parallel_cpp``, which differs from the + single-point kernel: it orders the k nearest rows by distance and then row, + weights them with unnormalised Student-t and Gaussian kernels and a + ``sd(1:k)`` lognormal rank weight, and scales the weighted sum by ``1 / total``. """ features, y_hat = _split_rpm(rpm) tests = _as_matrix(x_test, "x_test") @@ -61,26 +67,20 @@ def nns_distance_path_single_bulk( raise ValueError("x_test column count must match rpm feature column count.") k_value = _resolve_k(k, features.shape[0]) + rank_weights = _bulk_rank_weights(k_value) + rpm_min = np.min(features, axis=0) + rpm_max = np.max(features, axis=0) out = np.empty(tests.shape[0], dtype=np.float64) for row_index, dest in enumerate(tests): - scaled_features, scaled_dest = _rescale_joint(features, dest) - distances = _distance_sum(scaled_features, scaled_dest, zero_eps=1e-10) - indices = np.argsort(distances, kind="mergesort") - selected = indices[:k_value] - selected_distances = distances[selected] - selected_y = y_hat[selected] - + inverse = _inverse_ranges(np.minimum(rpm_min, dest), np.maximum(rpm_max, dest)) + distances = _distances(features, dest, inverse) + selected = _cpp_selected_by_row(distances, k_value) + top_distances = distances[selected] + top_y = y_hat[selected] if k_value == 1: - out[row_index] = float(selected_y[0]) + out[row_index] = float(top_y[0]) continue - - # R14 NNS_distance_path_single_parallel_cpp now uses sample sd(1:k), - # exactly as the scalar R path (not the legacy population variance). - weights = _combined_weights(selected_distances, rank_sd_population=False) - if class_ is not None: - out[row_index] = _weighted_mode(selected_y, weights) - else: - out[row_index] = float(np.dot(selected_y, weights)) + out[row_index] = _bulk_prediction(top_distances, top_y, rank_weights, class_ is not None) return out @@ -92,104 +92,214 @@ def nns_distance_bulk( ) -> NDArray[np.float64]: """Return R's NNS.distance.bulk predictions for many target rows.""" # R14 R/NNS_Distance_bulk.R routes the public bulk API through the - # SAME eight-channel distance ensemble as the scalar call, not the old - # 1/d-only NNS_distance_bulk_cpp regression helper. + # SAME eight-channel distance ensemble as the single-path bulk kernel, not + # the old 1/d-only NNS_distance_bulk_cpp regression helper. return nns_distance_path_single_bulk(rpm, x_test, k, class_) -def _rescale_joint( - features: NDArray[np.float64], - dest: NDArray[np.float64], -) -> tuple[NDArray[np.float64], NDArray[np.float64]]: - col_min = np.minimum(np.min(features, axis=0), dest) - col_max = np.maximum(np.max(features, axis=0), dest) +def _inverse_ranges( + col_min: NDArray[np.float64], col_max: NDArray[np.float64] +) -> NDArray[np.float64]: + """R's ``invR``: ``1 / range`` for a finite positive range, else 0 (column skipped).""" ranges = col_max - col_min - scaled_features = np.zeros_like(features, dtype=np.float64) - scaled_dest = np.zeros_like(dest, dtype=np.float64) - nonzero = ranges != 0.0 - scaled_features[:, nonzero] = (features[:, nonzero] - col_min[nonzero]) / ranges[nonzero] - scaled_dest[nonzero] = (dest[nonzero] - col_min[nonzero]) / ranges[nonzero] - return scaled_features, scaled_dest + usable = np.isfinite(ranges) & (ranges > 0.0) + return np.where(usable, 1.0 / np.where(usable, ranges, 1.0), 0.0) -def _distance_sum( +def _distances( features: NDArray[np.float64], dest: NDArray[np.float64], - zero_eps: float, + inverse: NDArray[np.float64], ) -> NDArray[np.float64]: - diff = features - dest[np.newaxis, :] - distances = cast(NDArray[np.float64], np.sum(diff * diff + np.abs(diff), axis=1)) - distances[distances == 0.0] = zero_eps - return distances + """R's ``S``: per row, ``diff = (a - b) * invR`` and ``acc += diff^2 + |diff|`` + over the columns in order; an exact 0 becomes ``1e-10``.""" + acc = np.zeros(features.shape[0], dtype=np.float64) + for column in range(features.shape[1]): + if not inverse[column] > 0.0: + continue + diff = (features[:, column] - dest[column]) * inverse[column] + acc = acc + (diff * diff + np.abs(diff)) + return np.where(acc == 0.0, 1e-10, acc) + + +def _has_ties(values: NDArray[np.float64]) -> bool: + return bool(np.unique(values).size != values.size) + + +def _cpp_sorted_prefix(distances: NDArray[np.float64], count: int) -> NDArray[np.intp]: + """The first ``count`` rows in R's order: ``std::partial_sort`` when + ``count < n``, else ``std::sort``, on ``S[a] < S[b]``.""" + n = distances.size + if not _has_ties(distances): + return np.argsort(distances, kind="stable")[:count] + order = list(range(n)) + key = distances.tolist() + if count < n: + partial_sort(order, key, count) + else: + std_sort(order, key) + return np.asarray(order[:count], dtype=np.intp) + + +def _cpp_selected_by_row(distances: NDArray[np.float64], count: int) -> NDArray[np.intp]: + """The bulk kernel's k nearest rows: ``std::partial_sort`` picks them, then a + stable sort orders them by distance and row index.""" + n = distances.size + ranked = np.argsort(distances, kind="stable") + if count >= n or distances[ranked[count - 1]] < distances[ranked[count]]: + return ranked[:count] + # A tie straddles the k-th place: which tied rows are kept is libstdc++'s + # heap selection. + order = list(range(n)) + heap_select(order, distances.tolist(), count) + chosen = np.asarray(order[:count], dtype=np.intp) + return chosen[np.lexsort((chosen, distances[chosen]))] + + +def _sequential_sum(values: NDArray[np.float64]) -> float: + """``std::accumulate(begin, end, 0.0)``: a running double sum.""" + if values.size == 0: + return 0.0 + return float(np.cumsum(values)[-1]) -def _combined_weights( - distances: NDArray[np.float64], - *, - rank_sd_population: bool = False, -) -> NDArray[np.float64]: - from scipy import stats # type: ignore[import-untyped] +def _normalised(values: NDArray[np.float64]) -> NDArray[np.float64]: + """R's ``if (sum > 0) v /= sum; else 0`` with a sequential double sum.""" + total = _sequential_sum(values) + if total > 0.0: + return np.asarray(values / total, dtype=np.float64) + return np.zeros_like(values, dtype=np.float64) + + +def _sd_vec(values: NDArray[np.float64]) -> float: + """R's ``sd_vec``: double mean, sequential sum of squares, ``sqrt(acc / (n - 1))``.""" + n = values.size + if n < 2: + return math.nan + mean = _sequential_sum(values) / float(n) + deviations = values - mean + return math.sqrt(_sequential_sum(deviations * deviations) / float(n - 1)) + + +def _var_vec(values: NDArray[np.float64]) -> float: + """R's ``var_vec``: the square of ``sd_vec``.""" + sd = _sd_vec(values) + return sd * sd if math.isfinite(sd) else math.nan + + +def _finite_or_zero(values: list[float]) -> NDArray[np.float64]: + out = np.asarray(values, dtype=np.float64) + return np.where(np.isfinite(out), out, 0.0) + +def _scalar_weights(distances: NDArray[np.float64]) -> NDArray[np.float64]: + """The eight-component weights of R's ``NNS_distance_cpp``.""" count = distances.size ranks = np.arange(1, count + 1, dtype=np.float64) - uniform = np.full(count, 1.0 / count, dtype=np.float64) - t_weights = _normalized(stats.t.pdf(distances, df=count)) - empirical = _normalized( - np.divide(1.0, distances, out=np.zeros_like(distances), where=distances > 0.0) + t_weights = _normalised(_finite_or_zero([dt(float(d), float(count)) for d in distances])) + empirical = _normalised( + np.where(distances > 0.0, 1.0 / np.where(distances > 0.0, distances, 1.0), 0.0) ) - # Rf_dexp's second argument is *scale* k, corresponding to rate 1/k. - # The earlier Python port inverted it (scale=1/k), concentrating all mass - # on the nearest rank and disagreeing whenever k>1. - exponential = _normalized(stats.expon.pdf(ranks, scale=float(count))) - + exponential = _normalised(_finite_or_zero([dexp(float(r), float(count)) for r in ranks])) lognormal = np.zeros(count, dtype=np.float64) - if count >= 2: - # R's single-point NNS.distance kernel uses the sample sd of ranks; the - # bulk path kernels use the population sd sqrt((k^2 - 1) / 12). - if rank_sd_population: - sd_ranks = float(np.sqrt((count * count - 1.0) / 12.0)) - else: - sd_ranks = float(np.std(ranks, ddof=1)) - lognormal = np.abs(stats.lognorm.logpdf(ranks, s=sd_ranks, scale=1.0))[::-1] - lognormal = _normalized(lognormal) - - power_law = _normalized(ranks**-2.0) - + sd_ranks = _sd_vec(ranks) if count >= 2 else math.nan + if math.isfinite(sd_ranks): + lognormal = np.abs( + np.asarray([dlnorm_log(float(r), 0.0, sd_ranks) for r in ranks], dtype=np.float64) + )[::-1] + lognormal = _normalised(lognormal) + power_law = _normalised(np.asarray([math.pow(float(r), -2.0) for r in ranks])) normal = np.zeros(count, dtype=np.float64) - sd_distances = float(np.std(distances, ddof=1)) - if np.isfinite(sd_distances) and sd_distances > 0.0: - normal = _normalized(stats.norm.pdf(distances, loc=0.0, scale=sd_distances)) - + sd_distances = _sd_vec(distances) + if math.isfinite(sd_distances) and sd_distances > 0.0: + normal = _normalised( + _finite_or_zero([dnorm(float(d), 0.0, sd_distances) for d in distances]) + ) rbf = np.zeros(count, dtype=np.float64) - var_distances = float(np.var(distances, ddof=1)) - if np.isfinite(var_distances) and var_distances > 0.0: - rbf = _normalized(np.exp(-distances / (2.0 * var_distances))) + var_distances = _var_vec(distances) + if math.isfinite(var_distances) and var_distances > 0.0: + rbf = _normalised(libm_exp(-distances / (2.0 * var_distances))) weights = uniform + t_weights + empirical + exponential + lognormal + power_law + normal + rbf - total = float(np.sum(weights)) + total = _sequential_sum(weights) if total > 0.0: - return weights / total + return np.asarray(weights / total, dtype=np.float64) return uniform -def _normalized(values: NDArray[np.float64]) -> NDArray[np.float64]: - clean = np.where(np.isfinite(values), values, 0.0) - total = float(np.sum(clean)) - if total > 0.0: - return clean / total - return np.zeros_like(clean, dtype=np.float64) +def _bulk_rank_weights(count: int) -> NDArray[np.float64]: + """The bulk kernel's rank-only weights ``uniW + expW + lnormW + plW``, summed + in that order (each normalised as R does).""" + ranks = np.arange(1, count + 1, dtype=np.float64) + uniform = np.full(count, 1.0 / count, dtype=np.float64) + exponential = _normalised(np.asarray([dexp(float(r), float(count)) for r in ranks])) + power_law = _normalised(np.asarray([math.pow(float(r), -2.0) for r in ranks])) + lognormal = np.zeros(count, dtype=np.float64) + if count >= 2: + # R matches sd(1:k): sqrt(k (k + 1) / 12). + sdlog = math.sqrt(float(count) * (count + 1.0) / 12.0) + lognormal = _normalised( + np.abs(np.asarray([dlnorm_log(float(r), 0.0, sdlog) for r in ranks]))[::-1] + ) + return np.asarray(uniform + exponential + lognormal + power_law, dtype=np.float64) + + +def _bulk_prediction( + distances: NDArray[np.float64], + y: NDArray[np.float64], + rank_weights: NDArray[np.float64], + is_class: bool, +) -> float: + """One test row of R's ``NNS_distance_path_single_parallel_cpp`` (k >= 2).""" + count = distances.size + exponent = -float(count + 1) / 2.0 + t_weights = _normalised( + np.asarray([math.pow(1.0 + (d * d) / count, exponent) for d in distances.tolist()]) + ) + empirical = _normalised( + np.where(distances > 0.0, 1.0 / np.where(distances > 0.0, distances, 1.0), 0.0) + ) + normal = np.zeros(count, dtype=np.float64) + sd_distances = _sd_vec(distances) + if math.isfinite(sd_distances) and sd_distances > 0.0: + z = distances / sd_distances + normal = _normalised(libm_exp(-0.5 * z * z)) + rbf = np.zeros(count, dtype=np.float64) + var_distances = _var_vec(distances) + if math.isfinite(var_distances) and var_distances > 0.0: + rbf = _normalised(libm_exp(-distances / (2.0 * var_distances))) + + raw = rank_weights + t_weights + empirical + normal + rbf + total = _sequential_sum(raw) + inverse_total = 1.0 / total if total > 0.0 else 1.0 / count + if not is_class: + if total > 0.0: + return _sequential_sum(y * raw) * inverse_total + return _sequential_sum(y) / count + weights = raw * inverse_total if total > 0.0 else np.full(count, 1.0 / count) + return _weighted_mode(y, weights) def _weighted_mode(y: NDArray[np.float64], weights: NDArray[np.float64]) -> float: + """R's ``mode_class_weighted``: votes ``ceil(100 w)``; among equal totals the + smallest class value wins (R scans the classes in ascending order).""" + if y.size == 0: + return float("nan") + if y.size == 1: + return float(y[0]) counts: defaultdict[float, int] = defaultdict(int) - for value, weight in zip(y, weights, strict=True): - count = int(np.ceil(100.0 * weight)) - if count > 0: - counts[float(value)] += count + for value, weight in zip(y.tolist(), weights.tolist(), strict=True): + votes = math.ceil(100.0 * weight) + if votes > 0: + counts[value] += votes if not counts: return float("nan") - best_value, _ = max(counts.items(), key=lambda item: item[1]) + best_value = min(counts) + best_count = counts[best_value] + for value in sorted(counts): + if counts[value] > best_count: + best_value, best_count = value, counts[value] return best_value @@ -201,12 +311,20 @@ def _split_rpm(rpm: NDArray[np.float64]) -> tuple[NDArray[np.float64], NDArray[n def _resolve_k(k: KValue, row_count: int) -> int: - if k == "all": + """R's k handling: ``"all"`` or an infinite k means every row; otherwise + ``as.integer(k[1])`` (truncation; NA means every row) clamped to ``1..n``.""" + if isinstance(k, str): + if k == "all": + return row_count + try: + value = float(k) + except ValueError: + return row_count + else: + value = float(np.asarray(k, dtype=np.float64).ravel()[0]) + if not math.isfinite(value) or abs(value) >= 2.0**31: return row_count - k_value = int(k) - if k_value < 1: - raise ValueError("k must be >= 1.") - return min(k_value, row_count) + return max(1, min(int(value), row_count)) def _as_vector(x: NDArray[np.float64]) -> NDArray[np.float64]: diff --git a/tests/_r_cache/dist14.bulk.json b/tests/_r_cache/dist14.bulk.json new file mode 100644 index 00000000..78ca0a80 --- /dev/null +++ b/tests/_r_cache/dist14.bulk.json @@ -0,0 +1,43 @@ +{ + "entries": { + "073d500b3c3226b7f2be906a3633f912042a23166818bf42e0851f813ca95498": [ + -0.4155465159206852, + -1.475605108280107, + -1.8152830981850203, + -0.47632828821776585 + ], + "3e9e60afa8be0a075381f20a11df3c5e6965b5cb2f13b64dc127ae4101879efa": [ + 2.0, + 3.0, + 3.0, + 2.0 + ], + "6e3dd1d9796d467e38326f44642c8f1d991d5a7760bb9b714d786263da63f2f9": [ + -0.706636818252654, + -1.0166027429313034, + -1.0013199350030462, + -0.7518332403071705 + ], + "916804378db9ac1bcc8ee4828d1468187e3d557405b786d3c8ab0536b40668b9": [ + 2.0, + 1.0, + 3.0, + 2.0 + ], + "a50895290412d627507023e030849eceda5eb77fccec98d5e4bfcca642a960bc": [ + 2.0, + 1.0, + 3.0, + 2.0 + ], + "d154f6ae3c514e73bd399fed37409cb9304e0b3b952408b808e259db7d4f0d4d": [ + -0.8789694275948517, + -1.3244084014768032, + -0.5112405415010907, + 0.5993512265824608 + ] + }, + "label": "dist14.bulk", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/dist14.continuous.json b/tests/_r_cache/dist14.continuous.json new file mode 100644 index 00000000..ffeca9f3 --- /dev/null +++ b/tests/_r_cache/dist14.continuous.json @@ -0,0 +1,11 @@ +{ + "entries": { + "1e2486dfe2685c6e138e595a6cbc71f2dbce5d2186a0d09e6a496e7428ed6149": [ + 0.5523431811492001, + 0.5523431811492001 + ] + }, + "label": "dist14.continuous", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/dist14.scalar.json b/tests/_r_cache/dist14.scalar.json new file mode 100644 index 00000000..79227267 --- /dev/null +++ b/tests/_r_cache/dist14.scalar.json @@ -0,0 +1,15 @@ +{ + "entries": { + "11cbba4fc0a1d2850cfa6742c7c016ac6782b78e8a0ce34908022d28e6e228a4": 3.0, + "33378b1055d1d90c8369eab4217af27a451fa1deb0091d23915358f475fbe8dd": 3.0, + "426311de315beaf5dff0f2108e85a1ee91d3e92ca10e6c1d8857bae233fdc5d4": 4.03, + "45a1834a90ae7daf90a3dc6de48b7767e8cce4672ec860de8fd4779074e70f3c": 1.0, + "a21f1a817d3e06ba473aa588bc1a6606e431c5ea5e22a042f11b0d5b206a431a": 0.7944088950692105, + "d275ff09500b5e77ca7b1f592c12836aea207105d3c9e2a62d5cd8419dcc706b": 1.0, + "d5462da1b0f159399a3f6b838898062164e1d52437b9359fab9efe58b76d90d2": 2.0549101378590278, + "e953b2cacf1a730d61076c8dd6118e14e2ccae367205c4e9070d9470578e69b4": 0.6373908632966245 + }, + "label": "dist14.scalar", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/parity/test_distance_bits_r14.py b/tests/parity/test_distance_bits_r14.py new file mode 100644 index 00000000..92a9e431 --- /dev/null +++ b/tests/parity/test_distance_bits_r14.py @@ -0,0 +1,126 @@ +"""R 14.0 parity, to the last bit, for NNS.distance and NNS.distance.bulk. + +Both follow R's C++ kernels: ``1 / range`` column scaling, distances summed +column by column, R's nmath densities and sequential sums. Tied distances fall +where libstdc++'s ``std::partial_sort``/``std::sort`` leave them, which the port +replays (``nns._stdsort``); the fixtures below were produced by compiled +libstdc++. +""" + +from __future__ import annotations + +from typing import Any + +import numpy as np +import pytest +from _r import nns_frame_call, r_column + +import nns +from nns._stdsort import partial_sort, std_sort + +pytestmark = pytest.mark.parity + + +def _num(values: Any) -> dict[str, Any]: + return r_column([float(v) for v in np.asarray(values, dtype=np.float64).ravel()], "numeric") + + +def _tied_rpm(seed: int, n: int, classes: bool) -> np.ndarray: + rng = np.random.default_rng(seed) + features = rng.integers(0, 4, size=(n, 2)).astype(np.float64) + y = rng.integers(1, 4, size=n).astype(np.float64) if classes else np.round( + rng.normal(size=n) * 3.0, 2 + ) + return np.column_stack([features, y]) + + +_RPM = "rpm <- data.frame(X1 = f1, X2 = f2, y.hat = yh)" + + +def _rpm_data(rpm: np.ndarray) -> dict[str, Any]: + return {"f1": _num(rpm[:, 0]), "f2": _num(rpm[:, 1]), "yh": _num(rpm[:, 2])} + + +@pytest.mark.parametrize("classes", [False, True]) +@pytest.mark.parametrize(("k", "r_k"), [(3, "3"), (7, "7"), ("all", "'all'"), (0, "0")]) +def test_nns_distance_with_tied_distances_matches_r_bitwise( + classes: bool, k: int | str, r_k: str +) -> None: + rpm = _tied_rpm(601, 40, classes) + dest = np.array([1.0, 2.0]) + r_class = "'class'" if classes else "NULL" + expected = nns_frame_call( + "dist14.scalar", + f"{_RPM}; NNS.distance(rpm, dest, k = {r_k}, class = {r_class})", + {**_rpm_data(rpm), "dest": _num(dest)}, + exact=True, + ) + actual = nns.nns_distance(rpm, dest, k=k, class_="class" if classes else None) + assert actual == expected + + +@pytest.mark.parametrize("classes", [False, True]) +@pytest.mark.parametrize(("k", "r_k"), [(2, "2"), (5, "5"), ("all", "'all'")]) +def test_nns_distance_bulk_with_tied_distances_matches_r_bitwise( + classes: bool, k: int | str, r_k: str +) -> None: + rpm = _tied_rpm(602, 50, classes) + tests = np.array([[0.0, 0.0], [1.0, 2.0], [3.0, 1.0], [1.5, 1.5]]) + r_class = "'class'" if classes else "NULL" + expected = nns_frame_call( + "dist14.bulk", + f"{_RPM}; NNS:::NNS.distance.bulk(rpm, matrix(xv, ncol = 2), k = {r_k}, class = {r_class})", + {**_rpm_data(rpm), "xv": _num(tests.ravel(order="F"))}, + exact=True, + ) + actual = nns.nns_distance_bulk(rpm, tests, k=k, class_="class" if classes else None) + np.testing.assert_array_equal(actual, np.asarray(expected, dtype=np.float64)) + + +def test_continuous_distances_match_r_bitwise() -> None: + rng = np.random.default_rng(603) + rpm = np.column_stack([rng.normal(size=(80, 2)), rng.normal(size=80)]) + dest = rng.normal(size=2) + expected = nns_frame_call( + "dist14.continuous", + f"{_RPM}; c(NNS.distance(rpm, dest, k = 6), " + "NNS:::NNS.distance.bulk(rpm, matrix(dest, ncol = 2), k = 6))", + {**_rpm_data(rpm), "dest": _num(dest)}, + exact=True, + ) + single = nns.nns_distance(rpm, dest, k=6) + bulk = nns.nns_distance_bulk(rpm, dest.reshape(1, -1), k=6) + np.testing.assert_array_equal([single, bulk[0]], np.asarray(expected, dtype=np.float64)) + + +# Orders printed by compiled libstdc++ (g++ -O2) for these keys. +_ALL_TIED = [0] * 52 +_ALL_TIED_SORT = [ + 39, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 26, 40, 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 14, 15, 16, 17, 18, 19, 20, 21, 22, + 23, 24, 25, +] +_BINARY = [ + 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, +] +_BINARY_SORT = [ + 35, 31, 29, 28, 27, 26, 25, 24, 23, 22, 21, 32, 19, 33, 17, 36, 13, 11, 10, 7, 4, 2, 43, 34, + 37, 38, 39, 40, 41, 42, 0, 30, 20, 18, 16, 15, 14, 12, 9, 8, 6, 5, 3, 1, +] +_BINARY_PARTIAL_10 = [17, 4, 13, 7, 10, 19, 21, 22, 11, 2] + + +def test_sort_replay_matches_libstdcxx_on_ties() -> None: + order = list(range(52)) + std_sort(order, [float(v) for v in _ALL_TIED]) + assert order == _ALL_TIED_SORT + order = list(range(52)) + partial_sort(order, [float(v) for v in _ALL_TIED], 4) + assert order[:4] == [1, 3, 0, 2] + order = list(range(44)) + std_sort(order, [float(v) for v in _BINARY]) + assert order == _BINARY_SORT + order = list(range(44)) + partial_sort(order, [float(v) for v in _BINARY], 10) + assert order[:10] == _BINARY_PARTIAL_10 From 38c3fa2fbd72e8a9909b7475c732b8e6179180dc Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 05:53:03 +0000 Subject: [PATCH 12/16] Port R's smooth.spline exactly for NNS.reg(smooth = TRUE) NNS.reg smooths its regression points with stats::smooth.spline(x, y, spar = (dep + 0.5) / 2). The port previously approximated that with SciPy B-splines, BLAS products and LAPACK's banded solver, which left smoothed fits about 1e-12 off R. nns._rsmspline now ports R 4.5.0's smoothing spline line by line: smspline.R's tie grouping (long-double group sums), scaling and knot thinning; sbart.c's trace ratio and lambda = ratio * 16^(6 spar - 2); the Fortran sgram, stxwx, sslvrg, bsplvd/bsplvb and bvalue; LINPACK dpbfa/dpbsl with the reference BLAS ddot/daxpy; and predict.smooth.spline.fit's linear extrapolation. The per-observation and per-prediction loops are vectorised without changing any point's arithmetic. Where R's smooth.spline stops (fewer than four unique x after grouping, or a zero IQR), nns_reg now raises the same error instead of silently interpolating. NNS.reg's multivariate path also drops class.levels for a numeric response, as R's ans$class.levels <- NULL does; nns_m_reg (R's NNS.M.reg) keeps the NULL entry. Live sweeps: 264 smooth.spline fits match R exactly (knots, coefficients, predictions inside and outside the range); the regression sweep is now 480/480 exact and the forecasting/derivatives sweep (dy.dx included) 345/345, with and without the native extension. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt --- docs/conventions.md | 18 +- src/nns/_reg_engine.py | 140 +-- src/nns/_rsmspline.py | 537 ++++++++++++ tests/_r_cache/smspl14.dydx.json | 19 + tests/_r_cache/smspl14.fit.json | 1099 ++++++++++++++++++++++++ tests/_r_cache/smspl14.mreg_names.json | 30 + tests/_r_cache/smspl14.reg.json | 167 ++++ tests/parity/test_smooth_spline_r14.py | 108 +++ 8 files changed, 1985 insertions(+), 133 deletions(-) create mode 100644 src/nns/_rsmspline.py create mode 100644 tests/_r_cache/smspl14.dydx.json create mode 100644 tests/_r_cache/smspl14.fit.json create mode 100644 tests/_r_cache/smspl14.mreg_names.json create mode 100644 tests/_r_cache/smspl14.reg.json create mode 100644 tests/parity/test_smooth_spline_r14.py diff --git a/docs/conventions.md b/docs/conventions.md index 3439546e..df0c88bb 100644 --- a/docs/conventions.md +++ b/docs/conventions.md @@ -339,12 +339,18 @@ and one neighbour; `nns_reg` sets it when every predictor is categorical, as R's `NNS.reg` does. `smooth=True` follows installed R's ordinary piecewise fallback for univariate inputs with fewer than four observations and for univariate `order="max"`; R -does not call `smooth.spline` there. Spline-eligible inputs use a private -fixed-`spar` cubic smoothing-spline adapter matching the `stats::smooth.spline` -subset used by `NNS.reg`: `spar = (dependence + 0.5) / 2`, R's -`1e-6 * IQR(x)` tie grouping, `.nknots.smspl` knot thinning above 49 unique x, -the Fortran `sgram` penalty (with its `.3330` constant), R's interior-band -trace ratio for lambda, and a banded Cholesky solve. +does not call `smooth.spline` there. Spline-eligible inputs fit +`stats::smooth.spline(x, y, spar = (dependence + 0.5) / 2)` through a line-by-line +port of R 4.5.0's smoothing spline (`nns._rsmspline`): R's `1e-6 * IQR(x)` tie +grouping with long-double group sums, `.nknots.smspl` knot thinning, the Fortran +`sgram` penalty (with its `.3330` constant), `stxwx`, `bsplvd`, the trace ratio +and `lambda = ratio * 16^(6 spar - 2)`, LINPACK `dpbfa`/`dpbsl` with the reference +BLAS, and `bvalue` with `predict`'s linear extrapolation. Fits and predictions +match R to the last bit (so do `dy_dx` and the smoothed `nns_arma_optim` paths +that use them), and where R's `smooth.spline` stops (fewer than four unique x +after grouping, or a zero IQR) `nns_reg` raises the same error. As in R, +`nns_reg` drops `class.levels` from a numeric multivariate fit, while +`nns_m_reg` keeps it as `None`. With dimension reduction, factor predictors are encoded first (one dummy column per level with `factor_2_dummy=True`) and the synthetic `x.star` coefficients are computed on the encoded columns, as in R. For callers that want an explicit diff --git a/src/nns/_reg_engine.py b/src/nns/_reg_engine.py index e8d8b3a7..bbf85809 100644 --- a/src/nns/_reg_engine.py +++ b/src/nns/_reg_engine.py @@ -63,6 +63,7 @@ from nns._rnmath import dnorm as _rnmath_dnorm from nns._rnmath import dt as _rnmath_dt from nns._rnmath import libm_exp +from nns._rsmspline import SmoothSplineFit, smooth_spline from nns.central_tendencies import nns_gravity, nns_mode from nns.dependence import nns_dep @@ -685,132 +686,13 @@ def _derivative(rp: dict[str, NDArray[np.float64]]) -> dict[str, NDArray[np.floa } -def _nknots_smspl(n: int) -> int: - """R's ``stats:::.nknots.smspl``: number of inner knots for ``n`` unique x.""" - if n < 50: - return n - a1, a2, a3, a4 = (math.log2(v) for v in (50.0, 100.0, 140.0, 200.0)) - if n < 200: - value = 2.0 ** (a1 + (a2 - a1) * (n - 50) / 150) - elif n < 800: - value = 2.0 ** (a2 + (a3 - a2) * (n - 200) / 600) - elif n < 3200: - value = 2.0 ** (a3 + (a4 - a3) * (n - 800) / 2400) - else: - value = 200.0 + (n - 3200) ** 0.2 - return math.trunc(value) - - -def _smooth_spline_predictor(rx: NDArray[np.float64], ry: NDArray[np.float64], spar: float) -> Any: - """Port of R's ``stats::smooth.spline(x, y, spar)`` with default arguments. - - R ties the smoothing parameter to dependence via an explicit ``spar`` rather - than delegating to GCV. This reproduces R's penalized cubic B-spline fit: - group x values closer than ``tol = 1e-6 * IQR(x)``, scale x to [0, 1], place - clamped cubic knots at ``.nknots.smspl(nx)`` of the unique abscissas (all of - them when ``nx < 50``), form the weighted design cross-product and the - second-derivative penalty Sigma exactly as Fortran ``sgram`` does (linear - interpolation of B'' on each knot interval, with its ``.3330`` constant), - then solve ``(X'WX + lambda * Sigma) c = X'W y`` with - ``lambda = ratio * 256^(3*spar - 1)`` and ``ratio`` from R's partial - diagonal trace. Returns a callable evaluating the fit (with R's linear - extrapolation beyond the data range), or ``None`` when R would refuse to fit. - """ - from scipy.interpolate import BSpline # type: ignore[import-untyped] - from scipy.linalg import solveh_banded # type: ignore[import-untyped] - - x_all = np.asarray(rx, dtype=np.float64) - y_all = np.asarray(ry, dtype=np.float64) - # R smooth.spline: tol = 1e-6 * IQR(x) (type-7 quartiles). - q25, q75 = r_quantile(x_all, [0.25, 0.75], 7) - tol = 1e-6 * float(q75 - q25) - if not math.isfinite(tol) or tol <= 0.0: - return None - # R: xx <- round((x - mean(x)) / tol); one group per distinct rounded value, - # represented by its smallest x, with the group mean of y and weight = size. - rounded = np.rint((x_all - r_mean(x_all)) / tol) - order = np.argsort(x_all, kind="mergesort") - xs = x_all[order] - ys = y_all[order] - rs = rounded[order] - is_start = np.concatenate(([True], rs[:-1] < rs[1:])) - nx = int(np.count_nonzero(is_start)) - if nx < 4: - return None - ux = xs[is_start] - group = np.cumsum(is_start) - 1 - wbar = np.bincount(group).astype(np.float64) - ybar = np.bincount(group, weights=ys) / wbar - - x0 = ux[0] - r_ux = ux[-1] - ux[0] - if r_ux <= 0.0: - return None - xbar = (ux - x0) / r_ux - nknots = _nknots_smspl(nx) - # R: xbar[seq.int(1, nx, length.out = nknots)] (fractional indices truncate). - inner = xbar[np.trunc(np.linspace(1.0, float(nx), nknots)).astype(np.intp) - 1] - knot = np.concatenate(([xbar[0]] * 3, inner, [xbar[-1]] * 3)) - nk = nknots + 2 - k = 3 - - design = BSpline.design_matrix(xbar, knot, k).toarray() - xtwx = design.T @ (wbar[:, None] * design) - - # Fortran sgram: on each knot interval B'' is linear; with y1 = B''(left) - # and y2 = B''(right) - y1, Sigma += w * (y1 y1' + (y2 y1' + y1 y2')/2 - # + .3330 y2 y2'), where .3330 (not 1/3) is R's constant. - d2 = np.asarray(BSpline(knot, np.eye(nk), k)(inner, nu=2), dtype=np.float64) - y1 = d2[:-1] - y2 = d2[1:] - y1 - width = np.diff(inner)[:, None] - cross = (width * y2).T @ y1 - sigma = (width * y1).T @ y1 + 0.5 * (cross + cross.T) + 0.333 * ((width * y2).T @ y2) - - diag_h = np.diag(xtwx) - diag_s = np.diag(sigma) - denom = float(diag_s[2 : nk - 3].sum()) - if denom <= 0.0: - return None - ratio = float(diag_h[2 : nk - 3].sum()) / denom - lam = ratio * 256.0 ** (3.0 * spar - 1.0) - - # R (sslvrg) solves the 4-band system with a banded Cholesky (LINPACK dpbfa). - system = xtwx + lam * sigma - banded = np.zeros((k + 1, nk), dtype=np.float64) - for offset in range(k + 1): - banded[k - offset, offset:] = np.diagonal(system, offset) - try: - coef = solveh_banded(banded, design.T @ (wbar * ybar)) - except np.linalg.LinAlgError: - return None - spline = BSpline(knot, coef, k, extrapolate=False) - edge_lo = float(spline(0.0)) - slope_lo = float(spline(0.0, nu=1)) - edge_hi = float(spline(1.0)) - slope_hi = float(spline(1.0, nu=1)) - - def predict(xout: NDArray[np.float64]) -> NDArray[np.float64]: - scaled = (np.asarray(xout, dtype=np.float64) - x0) / r_ux - out = np.asarray(spline(np.clip(scaled, 0.0, 1.0)), dtype=np.float64) - lo = scaled < 0.0 - hi = scaled > 1.0 - if np.any(lo): - out[lo] = edge_lo + scaled[lo] * slope_lo - if np.any(hi): - out[hi] = edge_hi + (scaled[hi] - 1.0) * slope_hi - return out - - return predict - - def _predict_univariate( xout: NDArray[np.float64], rp: dict[str, NDArray[np.float64]], smooth: bool, is_class: bool, class_values: NDArray[np.float64] | None, - smooth_fit: Any = None, + smooth_fit: SmoothSplineFit | None = None, ) -> NDArray[np.float64]: if xout.size == 0: return np.asarray([], dtype=np.float64) @@ -818,10 +700,8 @@ def _predict_univariate( if rx.size == 1: pred = np.full(xout.size, ry[0], dtype=np.float64) elif smooth and smooth_fit is not None and not is_class: - pred = np.asarray(smooth_fit(xout), dtype=np.float64) - elif smooth and rx.size >= 4 and not is_class: - # Degenerate fit (e.g. < 4 unique points): fall back to interpolation. - pred = r_approx(rx, ry, xout, ry[0], ry[-1]) + # R: predict(smooth.fit, xout)$y, with R's linear extrapolation. + pred = smooth_fit.predict(xout) else: # R: approx(rp$x, rp$y, xout, method = "linear", rule = 2, ties = "ordered"). pred = r_approx(rx, ry, xout, ry[0], ry[-1]) @@ -1824,7 +1704,7 @@ def nns_reg_engine( # Full multivariate regression. if ex.shape[1] > 1 and dim_red_method is None: - return _m_reg( + ans = _m_reg( ex, y_numeric, order=order, @@ -1846,6 +1726,12 @@ def nns_reg_engine( else categorical ), ) + # NNS.reg then sets ans$class.levels <- task$class.levels, which deletes + # the element when it is NULL (a numeric response); NNS.M.reg itself + # keeps the NULL entry. + if not multivariate_only and task["class_levels"] is None: + ans.pop("class.levels", None) + return ans synthetic_equation = None x_star = None @@ -1879,11 +1765,11 @@ def nns_reg_engine( # R ties the smoothing spline's spar to dependence and fits it once, reusing # the fit for both the training and point predictions. - smooth_fit = None + smooth_fit: SmoothSplineFit | None = None if smooth and rp["x"].size >= 4 and not task["is_class"]: dependence = _dependence(ux, y_numeric) spar = (dependence + 0.5) / 2.0 - smooth_fit = _smooth_spline_predictor(rp["x"], rp["y"], spar) + smooth_fit = smooth_spline(rp["x"], rp["y"], spar) fitted_pred = _predict_univariate( ux, rp, smooth, task["is_class"], task["class_values"], smooth_fit diff --git a/src/nns/_rsmspline.py b/src/nns/_rsmspline.py new file mode 100644 index 00000000..10356bbe --- /dev/null +++ b/src/nns/_rsmspline.py @@ -0,0 +1,537 @@ +"""R's ``stats::smooth.spline(x, y, spar = )`` and its ``predict``, to the last bit. + +A line-by-line port of R 4.5.0's ``smspline.R`` (grouping, scaling, knots), +``sbart.c`` (the ``spar`` given path), the Fortran ``sgram``, ``stxwx``, +``sslvrg``, ``bsplvd``/``bsplvb`` and ``bvalue``, LINPACK ``dpbfa``/``dpbsl`` +with the reference BLAS ``ddot``/``daxpy`` they call, and +``predict.smooth.spline.fit``. Every expression keeps R's operation order; the +per-observation and per-prediction loops are vectorised over points, which +leaves each point's arithmetic unchanged. + +Arrays named after their Fortran counterparts are 1-based (index 0 unused). +""" + +from __future__ import annotations + +import math +import warnings +from dataclasses import dataclass + +import numpy as np +from numpy.typing import NDArray + +from nns._rmean import r_mean, r_quantile +from nns._rnmath import r_pow_scalar + + +@dataclass(frozen=True) +class SmoothSplineFit: + """R's ``smooth.spline.fit``: knots, coefficients and the x scaling.""" + + knot: NDArray[np.float64] # 1-based: knot[1..nk + 4] + coef: NDArray[np.float64] # 1-based: coef[1..nk] + nk: int + min: float + range: float + + def predict(self, x: NDArray[np.float64], deriv: int = 0) -> NDArray[np.float64]: + """``predict.smooth.spline.fit(object, x, deriv)$y``.""" + return _predict(self, np.asarray(x, dtype=np.float64), deriv, depth=0) + + +def nknots_smspl(n: int) -> int: + """R's ``stats:::.nknots.smspl``: number of inner knots for ``n`` unique x.""" + if n < 50: + return n + a1, a2, a3, a4 = (math.log2(v) for v in (50.0, 100.0, 140.0, 200.0)) + if n < 200: + value = 2.0 ** (a1 + (a2 - a1) * (n - 50) / 150) + elif n < 800: + value = 2.0 ** (a2 + (a3 - a2) * (n - 200) / 600) + elif n < 3200: + value = 2.0 ** (a3 + (a4 - a3) * (n - 800) / 2400) + else: + value = 200.0 + (n - 3200) ** 0.2 + return math.trunc(float(value)) + + +def smooth_spline(x: NDArray[np.float64], y: NDArray[np.float64], spar: float) -> SmoothSplineFit: + """``smooth.spline(x, y, spar = spar)`` with R's other defaults. + + Raises R's errors (as ``ValueError``) where R stops, and warns as R does + when the penalised system is not positive definite at a large ``spar``. + """ + x = np.asarray(x, dtype=np.float64).ravel() + y = np.asarray(y, dtype=np.float64).ravel() + if x.size != y.size: + raise ValueError("'x' and 'y' lengths differ") + if not (np.all(np.isfinite(x)) and np.all(np.isfinite(y))): + raise ValueError("missing or infinite values in inputs are not allowed") + n = x.size + # tol = 1e-6 * IQR(x) + q25, q75 = r_quantile(x, [0.25, 0.75], 7) + tol = 1e-6 * float(q75 - q25) + if not math.isfinite(tol) or tol <= 0.0: + raise ValueError("'tol' must be strictly positive and finite") + # Replace y[] for the same x[] (to a precision of tol) by their mean. + xx = np.rint((x - r_mean(x)) / tol) + unsorted = bool(np.any(x[1:] < x[:-1])) + order = np.argsort(x, kind="stable") if unsorted else np.arange(n) + xxs = xx[order] + first = np.concatenate(([True], xxs[:-1] < xxs[1:])) + ux = x[order][first] + nx = ux.size + if nx <= 3: + raise ValueError("need at least four unique 'x' values") + if nx == n: + wbar = np.ones(nx, dtype=np.float64) + ybar = y[order].copy() + else: + # ox <- match(xx, xxs[nd]); per group, in the original order: + # c(length(i), sum(y[i]), sum(y[i]^2)) with R's long-double sum(). + group = np.searchsorted(xxs[first], xx) + counts = np.bincount(group, minlength=nx).astype(np.float64) + sums = np.zeros(nx, dtype=np.longdouble) + np.add.at(sums, group, y.astype(np.longdouble)) + wbar = counts + ybar = np.asarray(sums, dtype=np.float64) / np.where(wbar > 0.0, wbar, 1.0) + r_ux = float(ux[-1] - ux[0]) + xbar = (ux - ux[0]) / r_ux + nknots = nknots_smspl(nx) + # xbar[seq.int(1, nx, length.out = nknots)]: fractional indices truncate. + picks = np.trunc(np.linspace(1.0, float(nx), nknots)).astype(np.intp) - 1 + knot_values = np.concatenate(([xbar[0]] * 3, xbar[picks], [xbar[-1]] * 3)) + nk = nknots + 2 + knot = np.concatenate(([0.0], knot_values)) + + coef, info = _sbart_given_spar(xbar, ybar, wbar, knot, nk, float(spar)) + if info > 0: + # R: smoothing parameter value too small/large (ier from dpbfa). + if spar < 0.5: + raise ValueError("smoothing parameter value too small") + warnings.warn( + "smoothing parameter value too large\nsetting df = 1 __use with care!__", + UserWarning, + stacklevel=2, + ) + return SmoothSplineFit(knot=knot, coef=coef, nk=nk, min=float(ux[0]), range=r_ux) + + +# -------------------------------------------------------------------------- +# sbart.c (spar supplied) and sslvrg.f +# -------------------------------------------------------------------------- + + +def _sbart_given_spar( + xs: NDArray[np.float64], + ys: NDArray[np.float64], + wbar: NDArray[np.float64], + knot: NDArray[np.float64], + nk: int, + spar: float, +) -> tuple[NDArray[np.float64], int]: + # sbart squares the weights later, so it stores sqrt(w). + ws = np.where(wbar > 0.0, np.sqrt(np.where(wbar > 0.0, wbar, 0.0)), wbar) + sg0, sg1, sg2, sg3 = _sgram(knot, nk) + xwy, hs0, hs1, hs2, hs3 = _stxwx(xs, ys, ws, knot, nk) + # ratio := tr(X'WX) / tr(SIGMA) over the interior (1-based 3..nk-3). + t1 = 0.0 + t2 = 0.0 + for i in range(3, nk - 2): + t1 += hs0[i] + t2 += sg0[i] + ratio = t1 / t2 + lam = ratio * r_pow_scalar(16.0, spar * 6.0 - 2.0) + return _sslvrg_coef(knot, nk, lam, xwy, (hs0, hs1, hs2, hs3), (sg0, sg1, sg2, sg3)) + + +def _sslvrg_coef( + knot: NDArray[np.float64], + nk: int, + lam: float, + xwy: list[float], + hs: tuple[list[float], list[float], list[float], list[float]], + sg: tuple[list[float], list[float], list[float], list[float]], +) -> tuple[NDArray[np.float64], int]: + """``sslvrg``'s coefficient solve: ``(X'WX + lambda SIGMA) coef = X'Wy``.""" + hs0, hs1, hs2, hs3 = hs + sg0, sg1, sg2, sg3 = sg + lda = 4 + abd = [0.0] * (lda * nk) # Fortran abd(4, nk), column-major + + def at(row: int, col: int) -> int: + return (row - 1) + (col - 1) * lda + + coef = [0.0] * (nk + 1) + for i in range(1, nk + 1): + coef[i] = xwy[i] + abd[at(4, i)] = hs0[i] + lam * sg0[i] + for i in range(1, nk): + abd[at(3, i + 1)] = hs1[i] + lam * sg1[i] + for i in range(1, nk - 1): + abd[at(2, i + 2)] = hs2[i] + lam * sg2[i] + for i in range(1, nk - 2): + abd[at(1, i + 3)] = hs3[i] + lam * sg3[i] + info = _dpbfa(abd, lda, nk, 3) + if info == 0: + _dpbsl(abd, lda, nk, 3, coef) + return np.asarray(coef, dtype=np.float64), info + + +# -------------------------------------------------------------------------- +# LINPACK dpbfa / dpbsl with reference BLAS ddot / daxpy +# -------------------------------------------------------------------------- + + +def _ddot(n: int, a: list[float], ia: int, b: list[float], ib: int) -> float: + """Reference BLAS ``ddot`` (unit strides): a left-to-right sum from 0.""" + total = 0.0 + for offset in range(n): + total = total + a[ia + offset] * b[ib + offset] + return total + + +def _dpbfa(abd: list[float], lda: int, n: int, m: int) -> int: + def at(row: int, col: int) -> int: + return (row - 1) + (col - 1) * lda + + for j in range(1, n + 1): + s = 0.0 + ik = m + 1 + jk = max(j - m, 1) + mu = max(m + 2 - j, 1) + if m >= mu: + for k in range(mu, m + 1): + t = abd[at(k, j)] - _ddot(k - mu, abd, at(ik, jk), abd, at(mu, j)) + t = t / abd[at(m + 1, jk)] + abd[at(k, j)] = t + s = s + t * t + ik = ik - 1 + jk = jk + 1 + s = abd[at(m + 1, j)] - s + if s <= 0.0: + return j + abd[at(m + 1, j)] = math.sqrt(s) + return 0 + + +def _dpbsl(abd: list[float], lda: int, n: int, m: int, b: list[float]) -> None: + def at(row: int, col: int) -> int: + return (row - 1) + (col - 1) * lda + + for k in range(1, n + 1): + lm = min(k - 1, m) + la = m + 1 - lm + lb = k - lm + t = _ddot(lm, abd, at(la, k), b, lb) + b[k] = (b[k] - t) / abd[at(m + 1, k)] + for kb in range(1, n + 1): + k = n + 1 - kb + lm = min(k - 1, m) + la = m + 1 - lm + lb = k - lm + b[k] = b[k] / abd[at(m + 1, k)] + t = -b[k] + if lm > 0 and t != 0.0: # daxpy returns early for n <= 0 or da == 0 + base = at(la, k) + for offset in range(lm): + b[lb + offset] = b[lb + offset] + t * abd[base + offset] + + +# -------------------------------------------------------------------------- +# interv (findInterval2), bsplvb / bsplvd +# -------------------------------------------------------------------------- + + +def _interv(xt: NDArray[np.float64], n: int, x: float) -> tuple[int, int]: + """R's ``interv(xt, n, x, rightmost_closed = 0, all_inside = 0)`` on 1-based + ``xt[1..n]``: ``(left, mflag)`` with ``xt[left] <= x < xt[left + 1]``.""" + if n == 0: + return 0, 0 + if x < xt[1]: + return 0, -1 + if x >= xt[n]: + return n, 1 + # bisect for the unique left with xt[left] <= x < xt[left + 1] + left = int(np.searchsorted(xt[1 : n + 1], x, side="right")) + return left, 0 + + +def _bsplvd_values( + t: NDArray[np.float64], x: NDArray[np.float64], left: NDArray[np.intp] +) -> list[NDArray[np.float64]]: + """``bsplvd(t, lent, 4, x, left, work, vnikx, 1)``: the four order-4 B-spline + values at each x (``vnikx(1..4, 1)``), vectorised over points.""" + biatx = [np.ones_like(x), np.zeros_like(x), np.zeros_like(x), np.zeros_like(x)] + deltar: list[NDArray[np.float64]] = [] + deltal: list[NDArray[np.float64]] = [] + for j in range(1, 4): # bsplvb(..., jhigh = 4, index = 1) + deltar.append(t[left + j] - x) + deltal.append(x - t[left + 1 - j]) + saved = np.zeros_like(x) + for i in range(1, j + 1): + term = biatx[i - 1] / (deltar[i - 1] + deltal[j + 1 - i - 1]) + biatx[i - 1] = saved + deltar[i - 1] * term + saved = deltal[j + 1 - i - 1] * term + biatx[j] = saved + return biatx + + +class _Bsplvb: + """``bsplvb``'s saved state (``j``, ``deltal``, ``deltar``).""" + + def __init__(self) -> None: + self.j = 1 + self.deltal = [0.0] * 21 + self.deltar = [0.0] * 21 + + +def _bsplvb( + state: _Bsplvb, + t: NDArray[np.float64], + jhigh: int, + index: int, + x: float, + left: int, + biatx: list[float], +) -> None: + if index != 2: + state.j = 1 + biatx[1] = 1.0 + if state.j >= jhigh: + return + while True: + j = state.j + jp1 = j + 1 + state.deltar[j] = float(t[left + j]) - x + state.deltal[j] = x - float(t[left + 1 - j]) + saved = 0.0 + for i in range(1, j + 1): + term = biatx[i] / (state.deltar[i] + state.deltal[jp1 - i]) + biatx[i] = saved + state.deltar[i] * term + saved = state.deltal[jp1 - i] * term + biatx[jp1] = saved + state.j = jp1 + if state.j >= jhigh: + return + + +def _bsplvd(t: NDArray[np.float64], k: int, x: float, left: int, nderiv: int) -> list[list[float]]: + """``bsplvd``: ``dbiatx[i][m]`` (1-based) holds the (m-1)-th derivative of the + i-th B-spline of order k that is nonzero at x.""" + mhigh = max(min(nderiv, k), 1) + kp1 = k + 1 + state = _Bsplvb() + column = [0.0] * (k + 2) # dbiatx(., 1), which bsplvb works in + dbiatx = [[0.0] * (nderiv + 1) for _ in range(k + 1)] + _bsplvb(state, t, kp1 - mhigh, 1, x, left, column) + if mhigh > 1: + ideriv = mhigh + for _ in range(2, mhigh + 1): + jp1mid = 1 + for j in range(ideriv, k + 1): + dbiatx[j][ideriv] = column[jp1mid] + jp1mid += 1 + ideriv -= 1 + _bsplvb(state, t, kp1 - ideriv, 2, x, left, column) + for i in range(1, k + 1): + dbiatx[i][1] = column[i] + if mhigh == 1: + return dbiatx + a = [[0.0] * (k + 1) for _ in range(k + 1)] # a[row][col] + for i in range(1, k + 1): + a[i][i] = 1.0 + for m in range(2, mhigh + 1): + kp1mm = kp1 - m + fkp1mm = float(kp1mm) + il = left + i = k + for _ in range(kp1mm): + factor = fkp1mm / (float(t[il + kp1mm]) - float(t[il])) + for j in range(1, i + 1): + a[i][j] = (a[i][j] - a[i - 1][j]) * factor + il -= 1 + i -= 1 + for i in range(1, k + 1): + total = 0.0 + for j in range(max(i, m), k + 1): + total = a[j][i] * dbiatx[j][m] + total + dbiatx[i][m] = total + return dbiatx + + +# -------------------------------------------------------------------------- +# sgram.f and stxwx.f +# -------------------------------------------------------------------------- + + +def _sgram( + tb: NDArray[np.float64], nb: int +) -> tuple[list[float], list[float], list[float], list[float]]: + """The banded Gram matrix of B'' (``sgram``), with R's ``.3330`` constant.""" + sg = [[0.0] * (nb + 1) for _ in range(4)] + for i in range(1, nb + 1): + ileft, _ = _interv(tb, nb + 1, float(tb[i])) + lower = _bsplvd(tb, 4, float(tb[i]), ileft, 3) + yw1 = [0.0] + [lower[ii][3] for ii in range(1, 5)] + upper = _bsplvd(tb, 4, float(tb[i + 1]), ileft, 3) + yw2 = [0.0] + [upper[ii][3] - yw1[ii] for ii in range(1, 5)] + wpt = float(tb[i + 1]) - float(tb[i]) + width = min(ileft, 4) + if width < 1: + continue + for ii in range(1, width + 1): + for band in range(4): + jj = ii + band + if jj > width: + break + value = ( + yw1[ii] * yw1[jj] + + (yw2[ii] * yw1[jj] + yw2[jj] * yw1[ii]) * 0.5 + + yw2[ii] * yw2[jj] * 0.3330 + ) + row = ileft - width + ii + sg[band][row] = sg[band][row] + wpt * value + return sg[0], sg[1], sg[2], sg[3] + + +def _stxwx( + x: NDArray[np.float64], + z: NDArray[np.float64], + w: NDArray[np.float64], + xknot: NDArray[np.float64], + n: int, +) -> tuple[list[float], list[float], list[float], list[float], list[float]]: + """``X'Wz`` and the banded ``X'WX`` (``stxwx``), accumulated point by point.""" + eps = 1e-10 + left = np.empty(x.size, dtype=np.intp) + keep = x.size + for index, value in enumerate(x.tolist()): + ileft, mflag = _interv(xknot, n + 1, value) + if mflag == 1: + if value <= float(xknot[ileft]) + eps: + ileft -= 1 + else: + keep = index # Fortran returns here, keeping what it summed. + break + left[index] = ileft + x, z, w, left = x[:keep], z[:keep], w[:keep], left[:keep] + v1, v2, v3, v4 = _bsplvd_values(xknot, x, left) + ww = w * w # w(i)**2 + wz = ww * z + # Contributions in Fortran's order (per point: rows ileft-3 .. ileft). + y_terms = np.column_stack([wz * v1, wz * v2, wz * v3, wz * v4]).ravel() + h0_terms = np.column_stack([ww * (v1 * v1), ww * (v2 * v2), ww * (v3 * v3), ww * (v4 * v4)]) + h1_terms = np.column_stack( + [(ww * v1) * v2, (ww * v2) * v3, (ww * v3) * v4, np.zeros_like(v1)] + ) + h2_terms = np.column_stack( + [(ww * v1) * v3, (ww * v2) * v4, np.zeros_like(v1), np.zeros_like(v1)] + ) + h3_terms = np.column_stack( + [(ww * v1) * v4, np.zeros_like(v1), np.zeros_like(v1), np.zeros_like(v1)] + ) + out = [] + for terms, slots in ( + (y_terms.reshape(-1, 4), 4), + (h0_terms, 4), + (h1_terms, 3), + (h2_terms, 2), + (h3_terms, 1), + ): + acc = np.zeros(n + 1, dtype=np.float64) + used = terms[:, :slots].ravel() + targets = np.column_stack([left - 3 + s for s in range(slots)]).ravel() + # Unbuffered and in order: each row sums its terms point by point. + np.add.at(acc, targets, used) + out.append(acc.tolist()) + return out[0], out[1], out[2], out[3], out[4] + + +# -------------------------------------------------------------------------- +# bvalue.f and predict.smooth.spline.fit +# -------------------------------------------------------------------------- + + +def _bvalue(fit: SmoothSplineFit, x: NDArray[np.float64], jderiv: int) -> NDArray[np.float64]: + """``bvalue(knot, coef, nk, 4, x, jderiv)`` for each x in ``[0, 1]``.""" + t = fit.knot + n = fit.nk + k = 4 + if jderiv >= k: + return np.zeros_like(x) + lefts = np.empty(x.size, dtype=np.intp) + at_end = (x == t[n + 1]) & (t[n + 1] == t[n + k]) + for index, value in enumerate(x.tolist()): + if at_end[index]: + lefts[index] = n + continue + left, mflag = _interv(t, n + k, value) + if mflag != 0: + warnings.warn("bvalue() mflag != 0: should never happen!", UserWarning, stacklevel=3) + lefts[index] = -1 + continue + lefts[index] = left + valid = lefts >= k # imk >= 0 and n - i >= 0 hold on [0, 1] with these knots + out = np.zeros_like(x) + if not np.any(valid): + return out + xv = x[valid] + i = lefts[valid] + km1 = k - 1 + dm = [xv - t[i + 1 - j] for j in range(1, km1 + 1)] # dm(1..3) + dp = [t[i + j] - xv for j in range(1, km1 + 1)] # dp(1..3) + aj = [fit.coef[i - k + jc] for jc in range(1, k + 1)] # aj(1..4) + for j in range(1, jderiv + 1): + kmj = k - j + fkmj = float(kmj) + ilo = kmj + for jj in range(1, kmj + 1): + aj[jj - 1] = ((aj[jj] - aj[jj - 1]) / (dm[ilo - 1] + dp[jj - 1])) * fkmj + ilo -= 1 + for j in range(jderiv + 1, km1 + 1): + kmj = k - j + ilo = kmj + for jj in range(1, kmj + 1): + aj[jj - 1] = (aj[jj] * dm[ilo - 1] + aj[jj - 1] * dp[jj - 1]) / ( + dm[ilo - 1] + dp[jj - 1] + ) + ilo -= 1 + out[valid] = aj[0] + return out + + +def _predict( + fit: SmoothSplineFit, x: NDArray[np.float64], deriv: int, depth: int +) -> NDArray[np.float64]: + if depth > 2: + # R's Recall() loops when (min + range - min) / range exceeds 1. + raise RuntimeError( + "evaluation nested too deeply: infinite recursion / options(expressions=)?" + ) + xs = (x - fit.min) / fit.range + left = xs < 0.0 + right = xs > 1.0 + extrap = left | right + interp = ~extrap + y = xs.copy() + if np.any(interp): + y[interp] = _bvalue(fit, xs[interp], deriv) + if np.any(extrap): + xrange = np.array([fit.min, fit.min + fit.range], dtype=np.float64) + if deriv == 0: + end_object = _predict(fit, xrange, 0, depth + 1) + end_slopes = _predict(fit, xrange, 1, depth + 1) * fit.range + if np.any(left): + y[left] = end_object[0] + end_slopes[0] * (xs[left] - 0.0) + if np.any(right): + y[right] = end_object[1] + end_slopes[1] * (xs[right] - 1.0) + elif deriv == 1: + end_slopes = _predict(fit, xrange, 1, depth + 1) * fit.range + y[left] = end_slopes[0] + y[right] = end_slopes[1] + else: + y[extrap] = 0.0 + if deriv > 0: + y = y / r_pow_scalar(fit.range, float(deriv)) + return y diff --git a/tests/_r_cache/smspl14.dydx.json b/tests/_r_cache/smspl14.dydx.json new file mode 100644 index 00000000..45b51315 --- /dev/null +++ b/tests/_r_cache/smspl14.dydx.json @@ -0,0 +1,19 @@ +{ + "entries": { + "32bc09d88452f766828135f646488f81ec033ed84d0119880cab107f04895f70": { + "first": [ + -0.03364927040776659, + 1.0396572454991586, + -0.43499479488195586 + ], + "second": [ + 1.096931122744416, + -0.12794685050811835, + -2.1390807605681155 + ] + } + }, + "label": "smspl14.dydx", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/smspl14.fit.json b/tests/_r_cache/smspl14.fit.json new file mode 100644 index 00000000..eb057018 --- /dev/null +++ b/tests/_r_cache/smspl14.fit.json @@ -0,0 +1,1099 @@ +{ + "entries": { + "28b3d4f0e2d6756691de1e67bea22666d8ad0acf673d4eb454291f9a824101cf": { + "coef": [ + -1.1738058579008221, + -1.1614873493198874, + -1.1430051772127874, + -1.120091816558805, + -1.0616000800762329, + -0.9404859714275023, + -0.8323929365218398, + -0.6859652726433687, + -0.5558440048616415, + -0.39846877855625323, + -0.25978233530387856, + -0.04568421084371434, + 0.11953923362193884, + 0.20752221245614164, + 0.2604440711238377, + 0.3100596979847766, + 0.3639857481074024, + 0.4259752503858902, + 0.5626264881767356, + 0.7694331021012256, + 0.9312034309698805, + 0.9994324521183902, + 1.0333560330803557, + 1.0506151379843638, + 1.065324915587824, + 1.063505610028452, + 1.0489733208857024, + 1.020553334864706, + 0.9832844681366829, + 0.9024604884253192, + 0.7665008874006284, + 0.618651105524701, + 0.29118771986771447, + -0.036416355708774314, + -0.2818089930329977, + -0.4905360780140284, + -0.6766682236199565, + -0.8768222677529863, + -0.9763628442382438, + -1.0261338289953112 + ], + "knot": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.033898305084745756, + 0.05084745762711861, + 0.06779661016949151, + 0.13559322033898302, + 0.16949152542372878, + 0.20338983050847456, + 0.22033898305084743, + 0.23728813559322032, + 0.28813559322033894, + 0.3220338983050847, + 0.3559322033898305, + 0.37288135593220334, + 0.38983050847457623, + 0.4067796610169491, + 0.423728813559322, + 0.44067796610169485, + 0.4576271186440678, + 0.5084745762711864, + 0.5254237288135593, + 0.5423728813559322, + 0.559322033898305, + 0.5762711864406779, + 0.5932203389830508, + 0.6101694915254237, + 0.6271186440677966, + 0.6440677966101694, + 0.6779661016949152, + 0.7118644067796609, + 0.7288135593220338, + 0.7796610169491525, + 0.7966101694915253, + 0.8813559322033897, + 0.9152542372881356, + 0.9322033898305084, + 0.9661016949152541, + 0.983050847457627, + 1.0, + 1.0, + 1.0, + 1.0 + ], + "p": [ + -1.2661946722578328, + -1.1738058579008221, + 0.05191577045595747, + 1.0613867794312222, + -1.0261338289953112, + -2.145980986029323 + ] + }, + "2f92624f65261ac22737be717f37b2290394a8ba255c6a3b6c558ef7b9e1c473": { + "coef": [ + -1.2943225391060176, + -1.2586173611357356, + -1.2050599334816143, + -1.1330016372158973, + -1.0228370064542671, + -0.8873010357668559, + -0.7182995012497689, + -0.6025236378022377, + -0.505040880192578, + -0.3798950704842656, + -0.22390065174249282, + -0.033029040036762596, + 0.10564602040581135, + 0.21468409574221248, + 0.2954960729280428, + 0.37532414741547787, + 0.45402209941623517, + 0.5311156696149971, + 0.6564108561832414, + 0.7724946010285738, + 0.8649298443339896, + 0.9117924903879168, + 0.9489933318890856, + 0.9764230698157739, + 0.9942134270922504, + 1.001598482964668, + 0.9985543923998391, + 0.9807802336973878, + 0.9337794883472567, + 0.854152118305067, + 0.7313720448229448, + 0.6007339485826703, + 0.3255327769539253, + 0.03417463558809035, + -0.27816329046629823, + -0.4795179302025075, + -0.6437329926414954, + -0.8091949847656782, + -0.8919978894212008, + -0.9333993286304438 + ], + "knot": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.033898305084745756, + 0.05084745762711861, + 0.06779661016949151, + 0.13559322033898302, + 0.16949152542372878, + 0.20338983050847456, + 0.22033898305084743, + 0.23728813559322032, + 0.28813559322033894, + 0.3220338983050847, + 0.3559322033898305, + 0.37288135593220334, + 0.38983050847457623, + 0.4067796610169491, + 0.423728813559322, + 0.44067796610169485, + 0.4576271186440678, + 0.5084745762711864, + 0.5254237288135593, + 0.5423728813559322, + 0.559322033898305, + 0.5762711864406779, + 0.5932203389830508, + 0.6101694915254237, + 0.6271186440677966, + 0.6440677966101694, + 0.6779661016949152, + 0.7118644067796609, + 0.7288135593220338, + 0.7796610169491525, + 0.7966101694915253, + 0.8813559322033897, + 0.9152542372881356, + 0.9322033898305084, + 0.9661016949152541, + 0.983050847457627, + 1.0, + 1.0, + 1.0, + 1.0 + ], + "p": [ + -1.5621113738831323, + -1.2943225391060176, + 0.05015520222650809, + 0.9998602918917935, + -0.9333993286304438, + -1.8649317108384067 + ] + }, + "4a2bc6a979ab5fada89619a3ff34f37199098a63e9f23c1ce89e37616aea1882": { + "coef": [ + -1.0834708911613515, + -1.0438587287030654, + -1.002815759677998, + -0.9355599303733261, + -0.856738425445308, + -0.7414827783935581, + -0.40608142319719603, + -0.16504498595926878, + -0.24448566033952937, + -0.27190747372950724, + -0.274186915469975 + ], + "knot": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.07503422029491452, + 0.07774427307927888, + 0.12760299962581473, + 0.22477027708613942, + 0.2919325825475321, + 0.7679454246508202, + 0.9807117169225297, + 1.0, + 1.0, + 1.0, + 1.0 + ], + "p": [ + -1.2239719931282733, + -1.0834708911613515, + -0.5362262918007988, + -0.2627224809840581, + -0.274186915469975, + -0.321364612549075 + ] + }, + "571aa25cbc46150e6d743641b455e6873260c1b5fff39838489e3310539127b1": { + "coef": [ + -1.1407783657914747, + -1.0664450974276334, + -0.9544257782913629, + -1.2628719390268723, + -1.0574780375247377, + -0.17192221884499664, + -1.41673385468998, + -0.8587651949109721, + -0.45109363602555286, + 0.029772858829277153, + -0.4466488140180407, + -0.16726505864970734, + 0.31749468565110905, + 0.2497877748610936, + 0.23286091903764322, + 0.29652081720982165, + 0.3858555898732561, + 0.25096050301698375, + 0.18392111811585732, + 0.5823155853386596, + 1.1867245121473866, + 1.3241852299048766, + 0.9987169711073759, + 0.7852257520948395, + 1.137425611978475, + 1.1594589775069022, + 1.0420699495511536, + 0.872435312656514, + 1.1290280744920345, + 0.94874159522758, + 0.7130329086578675, + 0.7178638392693166, + 0.11178318317890393, + -0.6385482009595066, + 0.17245421724146756, + -0.5558905053019901, + -0.6302184981130632, + -0.9557683475786863, + -1.0251272093842767, + -1.059870096830217 + ], + "knot": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.033898305084745756, + 0.05084745762711861, + 0.06779661016949151, + 0.13559322033898302, + 0.16949152542372878, + 0.20338983050847456, + 0.22033898305084743, + 0.23728813559322032, + 0.28813559322033894, + 0.3220338983050847, + 0.3559322033898305, + 0.37288135593220334, + 0.38983050847457623, + 0.4067796610169491, + 0.423728813559322, + 0.44067796610169485, + 0.4576271186440678, + 0.5084745762711864, + 0.5254237288135593, + 0.5423728813559322, + 0.559322033898305, + 0.5762711864406779, + 0.5932203389830508, + 0.6101694915254237, + 0.6271186440677966, + 0.6440677966101694, + 0.6779661016949152, + 0.7118644067796609, + 0.7288135593220338, + 0.7796610169491525, + 0.7966101694915253, + 0.8813559322033897, + 0.9152542372881356, + 0.9322033898305084, + 0.9661016949152541, + 0.983050847457627, + 1.0, + 1.0, + 1.0, + 1.0 + ], + "p": [ + -1.698277878520285, + -1.1407783657914747, + 0.11174100648044781, + 1.1362219119262063, + -1.059870096830217, + -1.841585064363871 + ] + }, + "59e5114b96027234bf4a66aee6573fe87a66294b4719adc6aa2f3355eae01599": { + "coef": [ + -1.0132439833113234, + -0.9911273173180515, + -0.9447256997424862, + -0.7548798802332453, + -0.714084536415775, + -1.47887828828859, + -0.625997942176755, + -1.0042269059219773, + -1.3660849523371863, + -0.9802382996439484, + -1.1653539598877203, + -1.092109163799278, + -0.9924204952461325, + -0.6918084639560528, + -0.9753868465099338, + -1.3817124166595343, + -1.2512565664152158, + -0.3830353740070554, + -0.8604456795216809, + -0.7293045273955049, + -0.8219396004816599, + -1.1702759868641461, + -0.6727991662882181, + -0.3071029431636522, + -0.7491614041964231, + -0.3690881512432187, + -0.6575524862333594, + -0.7163084523393436, + -0.7319565916376921, + -0.37958613602352487, + -0.2524604671593401, + -0.14212459994345505, + -0.3680326821866123, + -0.23224256305152752, + 0.15196293493631602, + 0.17234075928481124, + -0.2633662221164413, + 0.8011953975880194, + -0.10440700628758415, + 0.6437061339852128, + 0.5020314412259715, + 0.5074969915945462, + -0.0026429921919942766, + 1.1885586237847068, + 0.7501358693119646, + 0.42443685109430995, + 0.7491424868972036, + 0.8190055056777689, + 0.778026185118073, + 1.019771529019046, + 0.8682523969427958, + 0.9759055850552505, + 0.670531350511854, + 0.8134567429086387, + 0.7644664187284091, + 0.5061231472691865, + 0.846234702789927, + 1.1184981555309301, + 0.9306050628017237, + 0.9573888446365861, + 0.6561050231950074, + 1.2245333439813875, + 0.5213419393408246, + 1.2284735115268248, + 1.0418105069120236, + 0.8066815559944114, + 0.7684391246403949, + 0.9226331924966283, + 0.9918259406070553, + 1.0343022225867726, + 1.1486852491515358, + 1.1835465851587437, + 0.9246902863360834, + 0.731782900655948, + 0.8528472331824944, + 0.6780047022139564, + 0.8573831882889948, + 0.8569716230552763, + 0.28081794180316266, + 1.2564905590119273, + 0.41654964014241125, + 1.0185776013524717, + -0.0755051549329829, + 1.2289707951183642, + 0.27612462553550976, + -0.28265222553902525, + 0.14562921601769593, + 0.0624148776392616, + -0.31772702626602894, + 0.204595892317863, + -0.29506625235933753, + -0.2202220705691907, + -0.17286770770899032, + -0.5162362677766025, + -0.683651860725705, + -0.39868877765285815, + -1.1066715690700293, + -0.5596687567862645, + -0.5251702553986903, + -0.8584752519401386, + -0.40951299516595224, + -0.8046947109666372, + -0.8114679268038798, + -0.8082631752434044 + ], + "knot": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.002557690958812933, + 0.005071004909063755, + 0.02728211283846123, + 0.045487242340811467, + 0.058860322126205046, + 0.062141814218810834, + 0.06527493144979563, + 0.07822122023504641, + 0.09384403378455353, + 0.09633986600362585, + 0.10108861270725039, + 0.11034365819913323, + 0.1177965871284825, + 0.1274286890272719, + 0.1295809895659495, + 0.14500673254495944, + 0.1540954498423117, + 0.15727872075628693, + 0.15939973062631665, + 0.18201922642414836, + 0.18582844879120924, + 0.19518830686062866, + 0.2026977174698842, + 0.21392824421496004, + 0.22081935429943159, + 0.22886461862517826, + 0.24546930759070107, + 0.25931397404582174, + 0.26895807302484304, + 0.2818265560234045, + 0.2959402033825285, + 0.30954494135193034, + 0.3288542142900619, + 0.3388617580801391, + 0.3516128237583577, + 0.36302431348878983, + 0.371281390439147, + 0.3874319728291498, + 0.39345020133136044, + 0.39605823583079647, + 0.423335024223798, + 0.43839398365320276, + 0.4389007915220465, + 0.44673008049258794, + 0.45380932039860156, + 0.4597028956388809, + 0.4692216136978965, + 0.474520989770846, + 0.48510891199758216, + 0.5061550668328307, + 0.5173888736559094, + 0.5212861795620972, + 0.5238985828252717, + 0.5301903126560299, + 0.5482147986884455, + 0.5559480973806699, + 0.5625580370624904, + 0.5686718690849324, + 0.575104801995783, + 0.5998746768194675, + 0.6140625918263937, + 0.6161604223446423, + 0.6395710459590443, + 0.6405002274597855, + 0.6464269288347863, + 0.6493904602984352, + 0.6504654965091586, + 0.653312752637565, + 0.6605034520440973, + 0.6651135191145784, + 0.6722237330161757, + 0.6834851279292401, + 0.6881770756060527, + 0.6966577799791361, + 0.7042888013135837, + 0.7110215726556376, + 0.7250552884105403, + 0.7356934380412923, + 0.748523563268086, + 0.7539628007441942, + 0.7643918414676457, + 0.8036795183765457, + 0.8110979993404177, + 0.828251711687962, + 0.8435820882242311, + 0.8570062273194271, + 0.8676222721715174, + 0.8815731590538832, + 0.8864878490547978, + 0.9076540060646213, + 0.9236003852985373, + 0.929396480583425, + 0.9348653765970095, + 0.9463547276013852, + 0.952126421959948, + 0.9638283874445591, + 0.967758337608171, + 0.9737317586461764, + 0.9902291466331969, + 0.9937495522326038, + 1.0, + 1.0, + 1.0, + 1.0 + ], + "p": [ + -3.1981144230615, + -1.0132439833113234, + -0.021300599183046064, + 0.9581574338035432, + -0.8082631752434044, + -0.6139382119278736 + ] + }, + "7af6d312fd9f886eaa2c19d2c055cf582d99f79c110964668dad76fa6b932be6": { + "coef": [ + -1.066872387382849, + -0.9421717528052841, + -0.81255612806663, + -0.9624495172196369, + -1.4725746370387187, + -1.3002482704870078, + 1.992900362756909, + 0.4669517134492075, + -0.36216749942841, + -0.4589728548131988, + -0.4670192038683705 + ], + "knot": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.07503422029491452, + 0.07774427307927888, + 0.12760299962581473, + 0.22477027708613942, + 0.2919325825475321, + 0.7679454246508202, + 0.9807117169225297, + 1.0, + 1.0, + 1.0, + 1.0 + ], + "p": [ + -1.509175344020787, + -1.066872387382849, + 0.4623310164481035, + 0.776485841974384, + -0.4670192038683705, + -0.6335548128195764 + ] + }, + "aa4ea4bde27f09a717ae9e485168c6891da5b8295b447e21a025f39c761ef88d": { + "coef": [ + -0.9517888879337058, + -0.9488048492819873, + -0.9428605639167691, + -0.9126368524581315, + -0.901973294675232, + -1.0019416759065534, + -0.9846485741775169, + -1.0189229072960304, + -1.0554483083340602, + -1.093550372662458, + -1.0766641426954155, + -1.0470341611138503, + -1.0234765235234242, + -1.00357286267295, + -1.0484064746473492, + -1.0758597317661962, + -1.021987997175504, + -0.8541535334613648, + -0.788019948514972, + -0.7633938920835409, + -0.7315294814089892, + -0.7399232005822075, + -0.675554192950808, + -0.6248131765687002, + -0.5949151613946367, + -0.584690361172993, + -0.6085069922366131, + -0.6316759204349676, + -0.5922450121328107, + -0.4357177492006907, + -0.31176614298450345, + -0.23962652111790822, + -0.22740035811818674, + -0.14994747612209985, + 0.019566707481228702, + 0.10093440061011069, + 0.16376200358103718, + 0.3120759274549037, + 0.38975571549143395, + 0.4615235710093139, + 0.48155071501246927, + 0.49910839788228656, + 0.5507857328375048, + 0.7297450537376956, + 0.748351751057465, + 0.7460849584381493, + 0.7696985672839897, + 0.8087424482460941, + 0.8530797827724363, + 0.9014620165015752, + 0.9228170155666616, + 0.8508263000603422, + 0.7832534649507223, + 0.7708605204280549, + 0.7651673653743271, + 0.769275694118686, + 0.8776787510179088, + 0.9402696989925029, + 0.940052293250302, + 0.9230739908048445, + 0.8654581024387109, + 0.8167623142876352, + 0.8124860271245994, + 0.8679780653417726, + 0.8765337747812454, + 0.9201582041943398, + 0.939898321775079, + 0.9673349527599375, + 0.9860886194256223, + 1.0149773068635024, + 1.044888490491183, + 1.0423409304402513, + 0.9838741326651845, + 0.894492204653198, + 0.8319980239215864, + 0.7937579944482961, + 0.7768399893812601, + 0.7557599438457315, + 0.7233858456482644, + 0.7659285110643977, + 0.7421206666816841, + 0.7332319088958931, + 0.6238050980837492, + 0.6598659370824844, + 0.3359052764773088, + 0.05655811628607136, + -0.007003355948409189, + -0.034608263909952405, + -0.05776842861359995, + -0.039962204128156584, + -0.09385452185071809, + -0.19455275655001844, + -0.34537016117651903, + -0.46940355666143807, + -0.5620659260579006, + -0.6193439793345363, + -0.6686568499279747, + -0.6477940520540977, + -0.6361274468189042, + -0.6404868951512336, + -0.6618126792944669, + -0.7228804840267427, + -0.7448167208541835, + -0.758725888002886 + ], + "knot": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.002557690958812933, + 0.005071004909063755, + 0.02728211283846123, + 0.045487242340811467, + 0.058860322126205046, + 0.062141814218810834, + 0.06527493144979563, + 0.07822122023504641, + 0.09384403378455353, + 0.09633986600362585, + 0.10108861270725039, + 0.11034365819913323, + 0.1177965871284825, + 0.1274286890272719, + 0.1295809895659495, + 0.14500673254495944, + 0.1540954498423117, + 0.15727872075628693, + 0.15939973062631665, + 0.18201922642414836, + 0.18582844879120924, + 0.19518830686062866, + 0.2026977174698842, + 0.21392824421496004, + 0.22081935429943159, + 0.22886461862517826, + 0.24546930759070107, + 0.25931397404582174, + 0.26895807302484304, + 0.2818265560234045, + 0.2959402033825285, + 0.30954494135193034, + 0.3288542142900619, + 0.3388617580801391, + 0.3516128237583577, + 0.36302431348878983, + 0.371281390439147, + 0.3874319728291498, + 0.39345020133136044, + 0.39605823583079647, + 0.423335024223798, + 0.43839398365320276, + 0.4389007915220465, + 0.44673008049258794, + 0.45380932039860156, + 0.4597028956388809, + 0.4692216136978965, + 0.474520989770846, + 0.48510891199758216, + 0.5061550668328307, + 0.5173888736559094, + 0.5212861795620972, + 0.5238985828252717, + 0.5301903126560299, + 0.5482147986884455, + 0.5559480973806699, + 0.5625580370624904, + 0.5686718690849324, + 0.575104801995783, + 0.5998746768194675, + 0.6140625918263937, + 0.6161604223446423, + 0.6395710459590443, + 0.6405002274597855, + 0.6464269288347863, + 0.6493904602984352, + 0.6504654965091586, + 0.653312752637565, + 0.6605034520440973, + 0.6651135191145784, + 0.6722237330161757, + 0.6834851279292401, + 0.6881770756060527, + 0.6966577799791361, + 0.7042888013135837, + 0.7110215726556376, + 0.7250552884105403, + 0.7356934380412923, + 0.748523563268086, + 0.7539628007441942, + 0.7643918414676457, + 0.8036795183765457, + 0.8110979993404177, + 0.828251711687962, + 0.8435820882242311, + 0.8570062273194271, + 0.8676222721715174, + 0.8815731590538832, + 0.8864878490547978, + 0.9076540060646213, + 0.9236003852985373, + 0.929396480583425, + 0.9348653765970095, + 0.9463547276013852, + 0.952126421959948, + 0.9638283874445591, + 0.967758337608171, + 0.9737317586461764, + 0.9902291466331969, + 0.9937495522326038, + 1.0, + 1.0, + 1.0, + 1.0 + ], + "p": [ + -1.246577344891687, + -0.9517888879337059, + 0.16566695236057433, + 0.8484287323162574, + -0.758725888002886, + -1.6021292985063353 + ] + }, + "c15c555f928fd12294c63bfa3d2b99715dffe10fba4733b7077628959a15b390": { + "coef": [ + -0.931704802864794, + -0.932839010184685, + -0.9350868715652857, + -0.9472535551945939, + -0.9681877960283902, + -1.0019720497980324, + -1.0191411134197876, + -1.0289344481821139, + -1.03789880287312, + -1.048254007500996, + -1.0460512411803742, + -1.0377459204740334, + -1.029924962504309, + -1.0156503779597394, + -0.993832693338229, + -0.9725010584263988, + -0.9343850207887536, + -0.8868265491559048, + -0.8405159256655828, + -0.8175975573168953, + -0.7745679740690952, + -0.7367018831796105, + -0.6919142297757386, + -0.6670414438819782, + -0.636551735293542, + -0.6104502740822264, + -0.5840344721045311, + -0.549601332926571, + -0.49775418026032886, + -0.42745359416904516, + -0.357802728543975, + -0.28500445557058524, + -0.20243986759055801, + -0.09838021944195785, + 0.00733074158880173, + 0.11051875651636311, + 0.19465662601490882, + 0.276393243007006, + 0.3605572330790851, + 0.4281838517781411, + 0.4785001181401859, + 0.5491856644082805, + 0.6317640264426123, + 0.7029098361564219, + 0.7349943116693738, + 0.7542527219467411, + 0.7776653129719919, + 0.799926295975583, + 0.8166025803455385, + 0.8316868677310961, + 0.8437470222020416, + 0.8392244892615747, + 0.8357048422187987, + 0.8365075884070229, + 0.8378570570963729, + 0.8431873631587242, + 0.8586689112286754, + 0.8715455356669202, + 0.8771075197114375, + 0.8805912111891467, + 0.8844149669741015, + 0.8858413600488065, + 0.8969230282336748, + 0.9140070481241377, + 0.928535923130596, + 0.9413168555103762, + 0.9451419823246578, + 0.9481951027956512, + 0.9498910062259134, + 0.9519113048181536, + 0.9517866557495955, + 0.9457922816421694, + 0.9313982262649407, + 0.9095747769842415, + 0.8849965986424015, + 0.8636420151825009, + 0.8411430284066587, + 0.8139658456533134, + 0.7850408623671257, + 0.7524837775779479, + 0.7217977537991257, + 0.6863451195073228, + 0.6047039293177272, + 0.48441523878027315, + 0.3135855453297638, + 0.201242952553635, + 0.0932313346944691, + 0.013394745045086677, + -0.057958449170494294, + -0.11126923087641136, + -0.19037596960818762, + -0.2818627788417955, + -0.38254424502180234, + -0.4449834073967679, + -0.49493486833049133, + -0.5397891826620846, + -0.5895968483428625, + -0.6196738946859213, + -0.6472882495562289, + -0.6797797814139293, + -0.7095650077266784, + -0.7409808102505174, + -0.7526303091920605, + -0.7600794692543584 + ], + "knot": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.002557690958812933, + 0.005071004909063755, + 0.02728211283846123, + 0.045487242340811467, + 0.058860322126205046, + 0.062141814218810834, + 0.06527493144979563, + 0.07822122023504641, + 0.09384403378455353, + 0.09633986600362585, + 0.10108861270725039, + 0.11034365819913323, + 0.1177965871284825, + 0.1274286890272719, + 0.1295809895659495, + 0.14500673254495944, + 0.1540954498423117, + 0.15727872075628693, + 0.15939973062631665, + 0.18201922642414836, + 0.18582844879120924, + 0.19518830686062866, + 0.2026977174698842, + 0.21392824421496004, + 0.22081935429943159, + 0.22886461862517826, + 0.24546930759070107, + 0.25931397404582174, + 0.26895807302484304, + 0.2818265560234045, + 0.2959402033825285, + 0.30954494135193034, + 0.3288542142900619, + 0.3388617580801391, + 0.3516128237583577, + 0.36302431348878983, + 0.371281390439147, + 0.3874319728291498, + 0.39345020133136044, + 0.39605823583079647, + 0.423335024223798, + 0.43839398365320276, + 0.4389007915220465, + 0.44673008049258794, + 0.45380932039860156, + 0.4597028956388809, + 0.4692216136978965, + 0.474520989770846, + 0.48510891199758216, + 0.5061550668328307, + 0.5173888736559094, + 0.5212861795620972, + 0.5238985828252717, + 0.5301903126560299, + 0.5482147986884455, + 0.5559480973806699, + 0.5625580370624904, + 0.5686718690849324, + 0.575104801995783, + 0.5998746768194675, + 0.6140625918263937, + 0.6161604223446423, + 0.6395710459590443, + 0.6405002274597855, + 0.6464269288347863, + 0.6493904602984352, + 0.6504654965091586, + 0.653312752637565, + 0.6605034520440973, + 0.6651135191145784, + 0.6722237330161757, + 0.6834851279292401, + 0.6881770756060527, + 0.6966577799791361, + 0.7042888013135837, + 0.7110215726556376, + 0.7250552884105403, + 0.7356934380412923, + 0.748523563268086, + 0.7539628007441942, + 0.7643918414676457, + 0.8036795183765457, + 0.8110979993404177, + 0.828251711687962, + 0.8435820882242311, + 0.8570062273194271, + 0.8676222721715174, + 0.8815731590538832, + 0.8864878490547978, + 0.9076540060646213, + 0.9236003852985373, + 0.929396480583425, + 0.9348653765970095, + 0.9463547276013852, + 0.952126421959948, + 0.9638283874445591, + 0.967758337608171, + 0.9737317586461764, + 0.9902291466331969, + 0.9937495522326038, + 1.0, + 1.0, + 1.0, + 1.0 + ], + "p": [ + -0.819658256313339, + -0.931704802864794, + 0.18293320783799227, + 0.9087953193658193, + -0.7600794692543584, + -1.2117705687113238 + ] + }, + "fc876d5589c77296d4ec72c8cc898fdb5dfe1e63e1f3cb868c285987f712ae81": { + "coef": [ + -1.091299918296034, + -1.068112164255211, + -1.044085179729441, + -1.006433533809856, + -0.9637466838070434, + -0.7857779916195802, + 0.11179555554788943, + 0.5305217727354042, + -0.20054660314617878, + -0.43903772866632806, + -0.4588616027105013 + ], + "knot": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.07503422029491452, + 0.07774427307927888, + 0.12760299962581473, + 0.22477027708613942, + 0.2919325825475321, + 0.7679454246508202, + 0.9807117169225297, + 1.0, + 1.0, + 1.0, + 1.0 + ], + "p": [ + -1.1735449862926202, + -1.091299918296034, + -0.2516645575820467, + 0.25245929369889847, + -0.45886160271050125, + -0.8691571184881183 + ] + } + }, + "label": "smspl14.fit", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/smspl14.mreg_names.json b/tests/_r_cache/smspl14.mreg_names.json new file mode 100644 index 00000000..0fc951d7 --- /dev/null +++ b/tests/_r_cache/smspl14.mreg_names.json @@ -0,0 +1,30 @@ +{ + "entries": { + "c327ffc052ad601602c14cc0a4bf24f3d869d000f1b44e4acf92270e52a29708": { + "mreg": [ + "R2", + "rhs.partitions", + "RPM", + "Point.est", + "pred.int", + "Fitted.xy", + "n.best", + "dist", + "class.levels" + ], + "reg": [ + "R2", + "rhs.partitions", + "RPM", + "Point.est", + "pred.int", + "Fitted.xy", + "n.best", + "dist" + ] + } + }, + "label": "smspl14.mreg_names", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/_r_cache/smspl14.reg.json b/tests/_r_cache/smspl14.reg.json new file mode 100644 index 00000000..fedda5cf --- /dev/null +++ b/tests/_r_cache/smspl14.reg.json @@ -0,0 +1,167 @@ +{ + "entries": { + "90017558f72fc6e021d89b46c2654040a4171cd50f90e1046113097676569427": { + "fit": [ + -0.29328025025284565, + -0.3342953332759049, + 1.3246475457081686, + -7.950419137901019, + 0.3051494881813279, + -0.3251434849577541, + 1.7003504089020351, + 4.145555432320647, + 0.1522088823342331, + 1.3817029711692532, + 0.25317171907591984, + 0.29232081283550304, + 0.11516632735574155, + 1.1587830105629582, + 0.26296448226450536, + 0.13179216671225438, + -6.884888543308907, + -0.6062295659977256, + 0.3105890008983452, + 3.079040567977893, + -0.22442207010151566, + 0.3034314265012671, + 0.29710432745253645, + 0.1412489264630761, + -0.19561494698011136, + -4.975342270938762, + -2.9112303134620436, + 0.22869680447207066, + 1.2149164480266907, + 1.8209931593125928, + -4.106272470492441, + 0.2148644547451328, + 0.15246296502762702, + 0.07667923128068276, + 3.9836731600914166, + 0.23339807861676, + 0.15154277948493142, + 0.15027119443029796, + 0.36790675235002995, + 0.14609456704336496, + 0.12471277466110493, + -0.6639827480256664, + -0.1571897989068826, + 0.32073740685910535, + -4.147822705180449, + 0.2583422064336083, + -0.5459791425603786, + 0.10912730339443184, + -3.194053626276965, + 0.24101591554610516, + 0.308812252610254, + 0.474354899561971, + 0.3546850422521711, + 0.3008786774979191, + 0.8277101948416412, + 0.309179705435303, + 0.2131672759935991, + 0.12388125992197571, + 0.009377312918007344, + 0.12746635797031677, + 0.10826327157283362, + -20.3097438233458, + -8.923279515633284, + 0.2565410681112043, + 0.30241957737481207, + 0.2814289098335084, + 0.20259216343882458, + 0.2950691398875962, + 2.3540135188033786, + 0.5646179107384812, + 2.579225123708137, + 1.5868746149894202, + 0.3081731897428346, + 0.20828508884350633, + 0.10781631858965841, + 0.10768488491329897, + 0.26083532712103846, + 0.10940212235823193, + 0.2588028209058843, + -6.960738581737587, + 0.1769750766307062, + 2.587984443196795, + 0.11905982377429039, + 0.31610101854580547, + 0.1954641967297114, + 0.0007999443584572074, + 0.19197064849082732, + 0.11650182926448391, + 0.24377397764807224, + 0.3117935290208664, + 0.2901092231431358, + -0.6706941874913621, + 3.1481257492841905, + 0.1433669307207427, + -0.06488565562105097, + 0.11209067171192252, + 0.2787290978901233, + -2.4498609503827944, + 0.3031825566271443, + 3.0101106828888127, + 0.6605460130534745, + 0.22401772355562793, + 0.3062762115283446, + 0.18885493825297217, + -0.9919180082218222, + 1.2079980688335483, + 0.11028183724723786, + 0.74040022314474, + 0.24128493769134457, + 0.19951530370198017, + -0.5276544632788567, + 1.7464178631179739, + 0.11065714235084911, + -0.4779911646717182, + 0.02152479227823103, + 0.30555140192237235, + -4.603997307184246, + 0.27728698155242576, + 2.3787488937446772, + 0.31083291140742436, + 0.40067716015622334, + -0.916058362485167, + -1.013694084068619, + 0.17124929364171673, + 0.2692317568549694, + -2.0329268335561617, + 4.376544003918354, + -0.29416660806886735, + 0.1852924414966913, + 0.28083528405121294, + 0.19690135942468692, + -0.07719500495262392, + -0.3888619985829377, + 0.2346075982132038, + 0.04630473240519109, + 0.2962862369979132, + -8.199835661776413, + 0.2691649703836415, + 1.1538076240726893, + 0.1108069299366888, + 0.17208677440126766, + 0.49002355053398383, + 1.8940270148322014, + 0.3114538677058052, + 1.0891929379959606, + 2.2107978338617134, + 0.03777634436363274, + 0.2238420917524743, + 0.3110987352550536, + 0.2913366171268981 + ], + "point": [ + -29.60281616678659, + 0.2837733715605169, + 0.5693997429578752, + 9.715773651108574 + ] + } + }, + "label": "smspl14.reg", + "nns_version": "14.0", + "schema_version": 2 +} diff --git a/tests/parity/test_smooth_spline_r14.py b/tests/parity/test_smooth_spline_r14.py new file mode 100644 index 00000000..f64148f6 --- /dev/null +++ b/tests/parity/test_smooth_spline_r14.py @@ -0,0 +1,108 @@ +"""R 14.0 parity, to the last bit, for NNS.reg(smooth = TRUE) and what builds on it. + +``NNS.reg`` fits ``stats::smooth.spline(rp$x, rp$y, spar = (dep + 0.5) / 2)``; +the port reproduces R 4.5.0's smoothing spline line by line +(``nns._rsmspline``), so the knots, coefficients and predictions, ``dy.dx`` +and the smoothed fits are R's own bits. +""" + +from __future__ import annotations + +import importlib +from typing import Any + +import numpy as np +import pytest +from _r import nns_frame_call, r_column + +import nns +from nns._rsmspline import smooth_spline + +pytestmark = pytest.mark.parity + +reg_engine = importlib.import_module("nns._reg_engine") + + +def _num(values: Any) -> dict[str, Any]: + return r_column([float(v) for v in np.asarray(values, dtype=np.float64).ravel()], "numeric") + + +@pytest.mark.parametrize(("n", "duplicates"), [(9, False), (60, True), (240, False)]) +@pytest.mark.parametrize("spar", [0.3, 0.55, 0.75]) +def test_smooth_spline_matches_r_bitwise(n: int, duplicates: bool, spar: float) -> None: + rng = np.random.default_rng(701 + n) + x = rng.uniform(-2.0, 4.0, n) + if duplicates: + x = np.round(x, 1) + y = np.sin(x) + rng.normal(size=n) * 0.3 + lo, hi = float(x.min()), float(x.max()) + xo = np.array([lo - 0.5, lo, 0.1, 1.7, hi, hi + 0.75]) + expected = nns_frame_call( + "smspl14.fit", + f"f <- smooth.spline(x, y, spar = {spar!r}); " + "list(knot = f$fit$knot, coef = f$fit$coef, p = predict(f, xo)$y)", + {"x": _num(x), "y": _num(y), "xo": _num(xo)}, + exact=True, + ) + fit = smooth_spline(x, y, spar) + np.testing.assert_array_equal(fit.knot[1:], np.asarray(expected["knot"])) + np.testing.assert_array_equal(fit.coef[1:], np.asarray(expected["coef"])) + np.testing.assert_array_equal(fit.predict(xo), np.asarray(expected["p"])) + + +def test_smooth_spline_needs_four_unique_x_like_r() -> None: + with pytest.raises(ValueError, match="need at least four unique 'x' values"): + smooth_spline(np.array([1.0, 2.0, 3.0, 3.0, 2.0]), np.arange(5.0), 0.5) + + +@pytest.mark.parametrize("native", [True, False]) +def test_nns_reg_smooth_matches_r_bitwise(monkeypatch: pytest.MonkeyPatch, native: bool) -> None: + rng = np.random.default_rng(702) + x = rng.normal(size=150) + y = x**3 - x + rng.normal(size=150) * 0.4 + point = np.array([-3.5, -0.2, 0.9, 3.2]) + expected = nns_frame_call( + "smspl14.reg", + "r <- NNS.reg(x, y, smooth = TRUE, point.est = pe, plot = FALSE); " + "list(fit = r$Fitted.xy$y.hat, point = r$Point.est)", + {"x": _num(x), "y": _num(y), "pe": _num(point)}, + exact=True, + ) + if not native: + monkeypatch.setattr(reg_engine, "native_fn", lambda name: None) + actual = nns.nns_reg(x, y, smooth=True, point_est=point) + np.testing.assert_array_equal(actual["Fitted.xy"]["y.hat"], np.asarray(expected["fit"])) + np.testing.assert_array_equal(actual["Point.est"], np.asarray(expected["point"])) + + +def test_dy_dx_matches_r_bitwise() -> None: + rng = np.random.default_rng(703) + x = rng.uniform(-3.0, 3.0, 120) + y = np.sin(x) + rng.normal(size=120) * 0.1 + expected = nns_frame_call( + "smspl14.dydx", + "r <- dy.dx(x, y, eval.point = c(-1.5, 0.2, 2)); " + "list(first = r$first.derivative, second = r$second.derivative)", + {"x": _num(x), "y": _num(y)}, + exact=True, + ) + actual = nns.dy_dx(x, y, eval_point=np.array([-1.5, 0.2, 2.0])) + np.testing.assert_array_equal(actual["first.derivative"], np.asarray(expected["first"])) + np.testing.assert_array_equal(actual["second.derivative"], np.asarray(expected["second"])) + + +def test_numeric_multivariate_fit_has_no_class_levels_like_r() -> None: + rng = np.random.default_rng(704) + x = rng.normal(size=(40, 2)) + y = x[:, 0] + rng.normal(size=40) * 0.1 + expected = nns_frame_call( + "smspl14.mreg_names", + "list(reg = names(NNS.reg(matrix(v, ncol = 2), y, plot = FALSE)), " + "mreg = names(NNS:::NNS.M.reg(matrix(v, ncol = 2), y)))", + {"v": _num(x.ravel(order="F")), "y": _num(y)}, + exact=True, + ) + assert "class.levels" not in expected["reg"] + assert "class.levels" in expected["mreg"] + assert "class.levels" not in nns.nns_reg(x, y) + assert nns.nns_m_reg(x, y)["class.levels"] is None From 60815ad7f8b808f5538e905c95a934dd2fda7348 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 06:07:51 +0000 Subject: [PATCH 13/16] Document the R 14.0 target and bit-level parity status docs/parity.md still described R 13.0 as the target and the cache as R 13.3 plus patches, although every shard is stamped 14.0. It now names the pinned R 14.0 source (OVVO-Financial/NNS@7b07a837, with its SHA256), shows how to install that oracle, and records the bit-level parity status: what the port reproduces of R's arithmetic, the live exact-equality sweeps and their case counts (all matching with and without the native extension), and the platform the result was verified on. api_status.md rows that predate this work are refreshed: the copula's matrix form, causation, the CDF, ARMA/VAR (now R 14 and seeded intervals), bootstrap/Monte Carlo and stochastic superiority under random_seed, and the smoothing-spline port in nns_reg. The README names R 14.0. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt --- README.md | 10 ++-- docs/api_status.md | 16 +++--- docs/parity.md | 125 ++++++++++++++++++++++++++------------------- 3 files changed, 86 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index 16feb850..392f4c11 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ [![License](https://img.shields.io/badge/license-GPL--3.0--only-blue)](https://github.com/OVVO-Financial/NNS-python/blob/main/LICENSE) `ovvo-nns` brings Nonlinear Nonparametric Statistics to Python as the `nns` -import package. It is a parity-focused port of the R `NNS` 13.0+ package, +import package. It is a parity-focused port of the R `NNS` 14.0 package, designed for real-world data that violate symmetry, linearity, or distributional assumptions. @@ -125,10 +125,10 @@ uv run python examples/run_all_vignettes.py ## Design boundaries -NNS Python prioritizes stable public behavior from installed R NNS 13.0+, not -private helper parity. The package returns NumPy arrays and plain dictionaries -rather than R `data.table` objects and uses explicit Python errors for unsafe R -coercions. +NNS Python prioritizes stable public behavior from R NNS 14.0, matched to the +last bit where verified (see the parity page), not private helper parity. The +package returns NumPy arrays and plain dictionaries rather than R `data.table` +objects and uses explicit Python errors for unsafe R coercions. - R is used for parity tests and local cache regeneration, not normal runtime. - Seeded randomized paths reproduce R's stream: `random_seed = s` matches `set.seed(s)`. diff --git a/docs/api_status.md b/docs/api_status.md index 95d515bf..48c7fc45 100644 --- a/docs/api_status.md +++ b/docs/api_status.md @@ -43,19 +43,19 @@ invariant, and property coverage. | VaR helpers: `lpm_var`, `upm_var` | implemented | high | Used by deterministic confidence and prediction interval paths. | | Central tendencies: `nns_gravity`, `nns_mode`, `nns_rescale` | implemented | high | Public helper behavior is covered through direct and dependent tests. | | Dependence and correlation: `nns_dep`, `nns_cor` | implemented | high | Follows installed R bivariate public path; dependence can be below signed correlation magnitude in known R-compatible cases. | -| Copula: `nns_copula` | implemented | high | Bivariate scalar public form is implemented. | -| Causation: `nns_causation`, `causal_matrix` | implemented | medium | Numeric lag paths and `tau="ts"` behavior are covered; some internal asymmetry granularity can differ in regression dimension reduction. | -| Distribution functions: `nns_cdf` | implemented | high | Deterministic non-plotting paths are implemented; plotting is ignored. | +| Copula: `nns_copula` | implemented | high | Bivariate and any-column matrix forms; R 14's arithmetic to the last bit (long-double `sum`/`prod`/`mean`, `^(1/2)` as `pow`). | +| Causation: `nns_causation`, `causal_matrix` | implemented | high | Pairs, matrices, numeric lags, `tau="ts"`/`"cs"` and the permutation p-values (R's `set.seed` stream via `seed`) match R to the last bit on the live sweeps. | +| Distribution functions: `nns_cdf` | implemented | high | Univariate and multivariate types match R to the last bit (libm `log`, long-double `rowSums`, R's batch kernel, the prefix LPM backend beyond 32 targets); plotting is ignored. | | Distance helpers: `nns_distance`, `nns_distance_bulk` | implemented | high | R 14's C++ kernels to the last bit, including where tied distances fall and the classification vote. | | Partitioning: `nns_part` | implemented | high | Returns plain dictionaries/arrays instead of R `data.table` objects. | -| Regression: `nns_reg` | implemented | high | Numeric, class-code, confidence interval, smoothing, dimension-reduction, and factor paths are covered; `point_est` columns are matched by name, categorical/declared levels are honoured. | -| Multivariate regression: `nns_m_reg` | implemented | high | Numeric, class, and raw factor/character predictor paths (`factor_2_dummy=True` by default, as R 13.3) are covered; column names are kept. | +| Regression: `nns_reg` | implemented | high | Numeric, class-code, confidence interval, smoothing (an exact port of R's `smooth.spline`), dimension-reduction, and factor paths match R to the last bit on the live sweeps; `point_est` columns are matched by name, categorical/declared levels are honoured. | +| Multivariate regression: `nns_m_reg` | implemented | high | Numeric, class, and raw factor/character predictor paths (`factor_2_dummy=True` by default, as in R) match R to the last bit; column names are kept. | | Stack: `nns_stack` | implemented | medium-high | Numeric/class paths, intervals, factor predictors, `ts_test`, and R's seeded CV/balance draws (bit-for-bit) are covered; test columns are matched by name and selection outputs carry predictor names. | | Boost: `nns_boost` | implemented | medium | Learner trials, epochs, and the final stack match R's seeded stream; feature weights/frequencies are named by column. | | Seasonality: `nns_seas` | implemented | high | Non-plotting installed-R path is implemented and cached defensively. | -| ARMA and VAR: `nns_arma`, `nns_arma_optim`, `nns_var` | partial | medium | Numeric forecasting and every VAR dimension-reduction path (`cor`, `NNS.dep`, `NNS.caus`, `all`) match R NNS 13.3 exactly on driven multi-variable fixtures, including own-lag weights, `objective="max"`, vector and list `tau`, and interior/trailing missing values. VAR's `relevant_variables` are the regressors the cross-validated dimension reduction threshold keeps, and `multivariate` is `nns_stack(method=1)` (`nns_reg`) on exactly those regressors. Stochastic interval streams are structural/statistical parity only. | -| Bootstrap/Monte Carlo: `nns_meboot`, `nns_mc` | implemented | medium | Deterministic diagnostics are parity-tested; exact stochastic replicate parity with R is not expected. | -| Stochastic dominance/superiority: `fsd`, `ssd`, `tsd`, `.uni` wrappers, `nns_ss`, `nns_sd_cluster`, `sd_efficient_set` | implemented | medium | Public structures and deterministic paths are covered. SD uses exact pure-NumPy prefix-pair kernels plus a degree-1 discrete order-statistic matrix path; R's C++ core remains faster on full finance fixtures. Stochastic intervals use NNS Python RNG. | +| ARMA and VAR: `nns_arma`, `nns_arma_optim`, `nns_var` | implemented | high | Forecasts, `nns_arma_optim` (including its smoothed `nns_reg` paths) and every VAR dimension-reduction path (`cor`, `NNS.dep`, `NNS.caus`, `all`) match R NNS 14.0 to the last bit on the live sweeps, including own-lag weights, `objective="max"`, vector and list `tau`, and interior/trailing missing values. ARMA prediction intervals reproduce R's `set.seed` stream through `random_seed`. VAR's `relevant_variables` are the regressors the cross-validated dimension reduction threshold keeps, and `multivariate` is `nns_stack(method=1)` (`nns_reg`) on exactly those regressors. | +| Bootstrap/Monte Carlo: `nns_meboot`, `nns_mc` | implemented | high | With `random_seed = s` the replicates and summaries are R's after `set.seed(s)`, to the last bit. | +| Stochastic dominance/superiority: `fsd`, `ssd`, `tsd`, `.uni` wrappers, `nns_ss`, `nns_sd_cluster`, `sd_efficient_set` | implemented | high | Contracts, the efficient-set order and the clustering match R 14 to the last bit; `nns_ss` intervals reproduce R's `set.seed` stream through `random_seed`. SD uses exact pure-NumPy prefix-pair kernels plus a degree-1 discrete order-statistic matrix path; R's C++ core remains faster on full finance fixtures. | | ANOVA: `nns_anova` | implemented | high | Binary, multi-group, pairwise, and degenerate `NaN` conventions are covered. | | Normalization: `nns_norm` | implemented | high | Numeric matrix path and R's list-of-vectors path are implemented; unequal-length vectors force linear scaling as in R. | | Categorical helpers: `encode_factor_codes`, `factor_2_dummy`, `factor_2_dummy_fr`, `prepare_factor_predictors` | implemented | high | Explicit `levels=` reproduces R factor ordering. `prepare_factor_predictors(...)` builds a full-rank dummy design; the regression functions encode raw factors themselves and do not need it. | diff --git a/docs/parity.md b/docs/parity.md index 16b36bea..d737b246 100644 --- a/docs/parity.md +++ b/docs/parity.md @@ -2,12 +2,15 @@ ## Target and status -R NNS 13.0 is the release parity target. It is the tensorized architecture -target; the earlier R NNS 12.1 cache has been superseded. NNS-core is v13.0.0 -and remains the native C++ foundation for accelerated partial-moment routines. +R NNS 14.0 is the parity target: the source at +`OVVO-Financial/NNS@7b07a837c130ba7220dbe16c584221508757d5ae` +(`NNS_14.0.tar.gz`, SHA256 +`5060f6ec426c889501740bf8a585d41f37a746554cb6fdf81f301ca41232cd5b`). Every shard +in `tests/_r_cache/` is stamped `nns_version` `14.0`. NNS-core remains the +native C++ foundation; R 14 kernels it does not carry enter as hashed overlays +recorded in `sync/nns_source.json`. -Full package parity is **not** claimed. Parity is bounded by the committed tests -and cache: +Parity is bounded by the committed tests and cache: - `tests/_r_cache/` — cache-only R result fixtures, sharded into one JSON file per R function label (keys are `