diff --git a/README.md b/README.md index 03bc8d06..a8a758f9 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,10 @@ The repository includes a unit testing library with over 4,000 tests that also s Contributions are welcome. Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on bug reports, feature requests, and pull requests. +## AI Disclosure + +Frontier generative AI models and coding agents are used to assist with software development, documentation, testing, and verification workflows. Project maintainers remain responsible for reviewing contributions, validating scientific methods and results, and deciding what is accepted or released. + ## License See [LICENSE](LICENSE) for details. diff --git a/python/pynumerics/tests/test_calculus.py b/python/pynumerics/tests/test_calculus.py index 6ac20185..b1d8ecb3 100644 --- a/python/pynumerics/tests/test_calculus.py +++ b/python/pynumerics/tests/test_calculus.py @@ -564,6 +564,7 @@ def test_python_defaults_are_the_numerics_defaults(fn, type_name, member, count) # Speed: a compiled integrand avoids the per-evaluation crossing into Python. +@pytest.mark.benchmark @needs_roslyn def test_compiled_integrand_is_faster_than_a_python_callback() -> None: # Arrange diff --git a/python/pynumerics/tests/test_callbacks.py b/python/pynumerics/tests/test_callbacks.py index 6bcb774d..4430b96e 100644 --- a/python/pynumerics/tests/test_callbacks.py +++ b/python/pynumerics/tests/test_callbacks.py @@ -41,15 +41,27 @@ def test_scalar_python_callback_integrates_like_scipy() -> None: @needs_roslyn -def test_compiled_lambda_gives_the_python_callback_bits_and_runs_faster() -> None: +def test_compiled_lambda_gives_the_python_callback_bits() -> None: # Arrange python = callbacks.scalar_function(lambda x: math.exp(-x * x)) compiled = callbacks.compile_csharp("x => Math.Exp(-x * x)") # Act - python_result, python_count, python_cost = _integrate(python) - compiled_result, compiled_count, compiled_cost = _integrate(compiled) + python_result, python_count, _ = _integrate(python) + compiled_result, compiled_count, _ = _integrate(compiled) # Assert assert compiled_result == python_result and compiled_count == python_count + + +@pytest.mark.benchmark +@needs_roslyn +def test_compiled_lambda_runs_faster_than_the_python_callback() -> None: + # Arrange + python = callbacks.scalar_function(lambda x: math.exp(-x * x)) + compiled = callbacks.compile_csharp("x => Math.Exp(-x * x)") + # Act + _, _, python_cost = _integrate(python) + _, _, compiled_cost = _integrate(compiled) + # Assert assert compiled_cost * 5.0 < python_cost, (compiled_cost, python_cost) @@ -279,6 +291,7 @@ def _per_call(delegate: object, argument: object, calls: int = 20000) -> float: return best +@pytest.mark.benchmark def test_array_callback_costs_close_to_a_raw_pythonnet_delegate() -> None: # Arrange: measured on a desktop, the overhead over a raw pythonnet delegate is about 0.4 us at # 2 to 50 values and 1 us at 1,000; the bound leaves a wide margin for slower machines. diff --git a/python/pynumerics/tests/test_download.py b/python/pynumerics/tests/test_download.py index 89e2ea73..bc4a9573 100644 --- a/python/pynumerics/tests/test_download.py +++ b/python/pynumerics/tests/test_download.py @@ -293,18 +293,32 @@ def test_downloaded_series_fields() -> None: result.utc = True # frozen -def test_parse_usgs_bulk_speed() -> None: - # Arrange: 40,000 daily records with qualifiers, about a century of record +def _usgs_century_text() -> str: + """Return 40,000 daily USGS records with qualifiers, about a century of record.""" days = np.arange(np.datetime64("1900-01-01"), np.datetime64("1900-01-01") + np.timedelta64(40000, "D")) - text = "x,y,time,value,approval_status,qualifier\n" + "".join( + return "x,y,time,value,approval_status,qualifier\n" + "".join( f",,{d},{i % 997 + 0.5},Approved,{'ICE' if i % 50 == 0 else ''}\n" for i, d in enumerate(days)) + + +def test_parse_usgs_reads_a_century_of_records_with_qualifiers() -> None: + # Arrange + text = _usgs_century_text() + # Act + result = download.parse_usgs(text, qualifiers=True) + # Assert + assert len(result.values) == 40000 and result.qualifiers.count("ICE") == 800 + + +@pytest.mark.benchmark +def test_parse_usgs_bulk_speed() -> None: + # Arrange + text = _usgs_century_text() download.parse_usgs(text[:2000]) # warm up # Act started = time.perf_counter() - result = download.parse_usgs(text, qualifiers=True) + download.parse_usgs(text, qualifiers=True) elapsed = time.perf_counter() - started # Assert: the parse runs in Numerics and the dates, values and qualifiers cross in bulk - assert len(result.values) == 40000 and result.qualifiers.count("ICE") == 800 assert elapsed < 1.0, f"{elapsed:.3f} s" diff --git a/python/pynumerics/tests/test_functions.py b/python/pynumerics/tests/test_functions.py index e106c74b..bb729439 100644 --- a/python/pynumerics/tests/test_functions.py +++ b/python/pynumerics/tests/test_functions.py @@ -343,6 +343,19 @@ def test_array_arguments_cross_once_with_the_scalar_bits(case: int) -> None: np.testing.assert_array_equal(_bits(array.ravel()), _bits(expected)) +def test_link_arrays_equal_element_calls_and_scipy() -> None: + # Arrange + link = fn.LogitLink() + points = np.linspace(0.001, 0.999, 20000) + # Act + array = link.link(points) + elements = np.array([link.net.Link(p) for p in points.tolist()]) + # Assert + np.testing.assert_array_equal(array, elements) + np.testing.assert_allclose(array, special.logit(points), rtol=1e-13, atol=1e-15) + + +@pytest.mark.benchmark def test_link_arrays_are_faster_than_element_calls() -> None: # Arrange link = fn.LogitLink() @@ -350,13 +363,11 @@ def test_link_arrays_are_faster_than_element_calls() -> None: link.link(points[:5]) # Act begin = time.perf_counter() - fast = link.link(points) + link.link(points) fast_seconds = time.perf_counter() - begin scalar = link.net.Link begin = time.perf_counter() - slow = np.array([scalar(p) for p in points.tolist()]) + _ = [scalar(p) for p in points.tolist()] slow_seconds = time.perf_counter() - begin # Assert - np.testing.assert_array_equal(fast, slow) - np.testing.assert_allclose(fast, special.logit(points), rtol=1e-13, atol=1e-15) assert fast_seconds < slow_seconds / 3 diff --git a/python/pynumerics/tests/test_goodness_of_fit.py b/python/pynumerics/tests/test_goodness_of_fit.py index 51de5a97..dfe2cc93 100644 --- a/python/pynumerics/tests/test_goodness_of_fit.py +++ b/python/pynumerics/tests/test_goodness_of_fit.py @@ -144,16 +144,25 @@ def test_distribution_statistics_equal_scipy() -> None: gof.kolmogorov_smirnov([], model) +def test_paired_metrics_on_large_series_equal_sklearn() -> None: + # Arrange + o, m = _pair(500_000, 3) + # Act + nse = gof.nash_sutcliffe_efficiency(o, m) + # Assert + assert nse == pytest.approx(metrics.r2_score(o, m), rel=1e-10) + + +@pytest.mark.benchmark def test_paired_metrics_cross_once_for_large_series() -> None: # Arrange o, m = _pair(500_000, 3) gof.nash_sutcliffe_efficiency(o[:10], m[:10]) # Act start = time.perf_counter() - nse = gof.nash_sutcliffe_efficiency(o, m) + gof.nash_sutcliffe_efficiency(o, m) elapsed = time.perf_counter() - start # Assert: bulk copies, not one crossing per value - assert nse == pytest.approx(metrics.r2_score(o, m), rel=1e-10) assert elapsed < 0.5 diff --git a/python/pynumerics/tests/test_hypothesis_tests.py b/python/pynumerics/tests/test_hypothesis_tests.py index 6a687edd..7b2d9d31 100644 --- a/python/pynumerics/tests/test_hypothesis_tests.py +++ b/python/pynumerics/tests/test_hypothesis_tests.py @@ -181,6 +181,17 @@ def test_multiple_grubbs_beck_matches_hec_ssp() -> None: assert isinstance(ht.multiple_grubbs_beck_test(lognormal), int) +def test_hot_path_tests_on_large_samples_equal_scipy() -> None: + # Arrange + rng = np.random.default_rng(1) + a, b = rng.normal(size=200_000), rng.normal(0.01, 1.0, 200_000) + # Act + p = ht.equal_variance_t_test(a, b) + # Assert + assert p == pytest.approx(stats.ttest_ind(a, b).pvalue, rel=1e-8) + + +@pytest.mark.benchmark def test_hot_path_tests_cross_once_for_large_samples() -> None: # Arrange rng = np.random.default_rng(1) @@ -188,10 +199,9 @@ def test_hot_path_tests_cross_once_for_large_samples() -> None: ht.equal_variance_t_test(a[:10], b[:10]) # Act start = time.perf_counter() - p = ht.equal_variance_t_test(a, b) + ht.equal_variance_t_test(a, b) elapsed = time.perf_counter() - start # Assert: one bulk copy each, not one crossing per value (a per-element path takes seconds) - assert p == pytest.approx(stats.ttest_ind(a, b).pvalue, rel=1e-8) assert elapsed < 0.5 diff --git a/python/pynumerics/tests/test_interpolation.py b/python/pynumerics/tests/test_interpolation.py index ca5696b6..eba98d6d 100644 --- a/python/pynumerics/tests/test_interpolation.py +++ b/python/pynumerics/tests/test_interpolation.py @@ -180,41 +180,68 @@ def test_search_functions_match_numpy_searchsorted(method: data.SearchMethod) -> np.testing.assert_array_equal(descending, len(KNOTS) - 2 - expected) +def test_array_interpolation_gives_the_scalar_call_bits() -> None: + # Arrange + spline = data.CubicSpline(KNOTS, VALUES) + x = np.random.default_rng(4).uniform(0.0, 5.5, 20000) + # Act + bulk = spline(x) + scalar = np.array([spline(v) for v in x]) + # Assert + np.testing.assert_array_equal(bulk.view(np.uint64), scalar.view(np.uint64)) + + +@pytest.mark.benchmark def test_array_interpolation_crosses_once_faster_than_scalar_calls() -> None: # Arrange spline = data.CubicSpline(KNOTS, VALUES) x = np.random.default_rng(4).uniform(0.0, 5.5, 20000) # Act start = time.perf_counter() - bulk = spline(x) + spline(x) bulk_time = time.perf_counter() - start start = time.perf_counter() - scalar = np.array([spline(v) for v in x]) + _ = [spline(v) for v in x] scalar_time = time.perf_counter() - start # Assert - np.testing.assert_array_equal(bulk.view(np.uint64), scalar.view(np.uint64)) assert bulk_time * 10.0 < scalar_time, (bulk_time, scalar_time) # List overloads ------------------------------------------------------------------------------- -def test_bilinear_arrays_cross_once_with_the_scalar_bits() -> None: - # Arrange +def _bilinear_grids() -> tuple[data.Bilinear, data.Bilinear, np.ndarray]: + """Return two identical logarithmic-x1 bilinear grids and 3,000 query points.""" x1, x2 = np.array([1.0, 2.0, 4.0, 8.0]), np.array([10.0, 20.0, 35.0]) y = np.add.outer(np.log(x1), np.sqrt(x2)) points = np.random.default_rng(4).uniform([0.5, 5.0], [9.0, 40.0], (3000, 2)) grid = data.Bilinear(x1, x2, y, x1_transform="logarithmic") reference = data.Bilinear(x1, x2, y, x1_transform="logarithmic") + return grid, reference, points + + +def test_bilinear_arrays_give_the_scalar_bits() -> None: + # Arrange + grid, reference, points = _bilinear_grids() # Act - begin = time.perf_counter() values = grid(points[:, 0], points[:, 1]) + expected = np.array([reference.net.Interpolate(float(a), float(b)) for a, b in points]) + # Assert + np.testing.assert_array_equal(values.view(np.uint64), expected.view(np.uint64)) + + +@pytest.mark.benchmark +def test_bilinear_arrays_cross_once_faster_than_scalar_calls() -> None: + # Arrange + grid, reference, points = _bilinear_grids() + # Act + begin = time.perf_counter() + grid(points[:, 0], points[:, 1]) list_seconds = time.perf_counter() - begin begin = time.perf_counter() - expected = np.array([reference.net.Interpolate(float(a), float(b)) for a, b in points]) + _ = [reference.net.Interpolate(float(a), float(b)) for a, b in points] loop_seconds = time.perf_counter() - begin - # Assert (one crossing gives the scalar calls' bits, in a fraction of the time) - np.testing.assert_array_equal(values.view(np.uint64), expected.view(np.uint64)) + # Assert (one crossing takes a fraction of the time of the scalar calls) assert list_seconds < loop_seconds / 3 diff --git a/python/pynumerics/tests/test_linalg.py b/python/pynumerics/tests/test_linalg.py index 4cf732b4..10adc9dc 100644 --- a/python/pynumerics/tests/test_linalg.py +++ b/python/pynumerics/tests/test_linalg.py @@ -359,6 +359,16 @@ def test_decomposition_method_enum_resolves_names() -> None: assert linalg.DecompositionMethod("Cholesky") is linalg.DecompositionMethod.CHOLESKY +def test_large_matrices_round_trip_bit_exact() -> None: + # Arrange + a = np.random.default_rng(19).normal(size=(500, 500)) + # Act + values = linalg.Matrix(a).to_array() + # Assert + np.testing.assert_array_equal(_bits(values), _bits(a)) + + +@pytest.mark.benchmark def test_large_matrices_cross_in_one_copy() -> None: # Arrange a = np.random.default_rng(19).normal(size=(500, 500)) @@ -366,11 +376,10 @@ def test_large_matrices_cross_in_one_copy() -> None: # Act start = time.perf_counter() for _ in range(5): - values = m.to_array() + m.to_array() linalg.Matrix(a) elapsed = time.perf_counter() - start # Assert: 250,000 doubles element by element would take seconds - np.testing.assert_array_equal(_bits(values), _bits(a)) assert elapsed < 0.5 diff --git a/python/pynumerics/tests/test_machine_learning.py b/python/pynumerics/tests/test_machine_learning.py index 33775ce2..f56121c3 100644 --- a/python/pynumerics/tests/test_machine_learning.py +++ b/python/pynumerics/tests/test_machine_learning.py @@ -734,6 +734,18 @@ def test_kriging_custom_distance_and_universal() -> None: # to the Numerics call, never a per-row crossing. +def test_predict_returns_one_value_per_row_of_a_large_feature_matrix() -> None: + # Arrange + x, y = _regression(30, 200) + queries = np.random.default_rng(31).normal(size=(20_000, 3)) + model = ml.KNearestNeighbors(x, y, 3) + # Act + wrapped = model.predict(queries) + # Assert + assert wrapped.shape == (20_000,) + + +@pytest.mark.benchmark def test_predict_crosses_the_feature_matrix_once() -> None: # Arrange x, y = _regression(30, 200) @@ -748,10 +760,9 @@ def test_predict_crosses_the_feature_matrix_once() -> None: raw = time.perf_counter() - start start = time.perf_counter() for _ in range(3): - wrapped = model.predict(queries) + model.predict(queries) total = time.perf_counter() - start # Assert: the copies of 60,000 inputs and 20,000 outputs cost far less than the search itself - assert wrapped.shape == (20_000,) assert total < 1.5 * raw + 0.05, f"wrapper {total:.3f} s against {raw:.3f} s for the bare call" diff --git a/python/pynumerics/tests/test_multivariate_copulas.py b/python/pynumerics/tests/test_multivariate_copulas.py index 3de534fb..925bf41a 100644 --- a/python/pynumerics/tests/test_multivariate_copulas.py +++ b/python/pynumerics/tests/test_multivariate_copulas.py @@ -274,6 +274,17 @@ def test_categorical_indices_refuse_invalid_tables_and_variates(table, uniforms, multivariate.Multinomial.categorical_indices(table, uniforms) +def test_categorical_indices_return_one_index_per_uniform() -> None: + # Arrange + cumulative = np.cumsum(np.full(50, 0.02)) + uniforms = np.random.default_rng(6).random(200000) + # Act + bulk = multivariate.Multinomial.categorical_indices(cumulative, uniforms) + # Assert + assert bulk.size == uniforms.size + + +@pytest.mark.benchmark def test_categorical_indices_cross_once_and_beat_per_element_calls() -> None: # Arrange cumulative = np.cumsum(np.full(50, 0.02)) @@ -284,14 +295,13 @@ def test_categorical_indices_cross_once_and_beat_per_element_calls() -> None: multivariate.Multinomial.categorical_indices(cumulative, uniforms[:10]) # Act start = time.perf_counter() - bulk = multivariate.Multinomial.categorical_indices(cumulative, uniforms) + multivariate.Multinomial.categorical_indices(cumulative, uniforms) bulk_seconds = time.perf_counter() - start start = time.perf_counter() for _ in range(2000): nd.Multinomial.CategoricalIndices(net_table, one) per_call = (time.perf_counter() - start) / 2000 # Assert: the whole array costs less than a twentieth of crossing per element - assert bulk.size == uniforms.size assert bulk_seconds < per_call * uniforms.size / 20 diff --git a/python/pynumerics/tests/test_optimization.py b/python/pynumerics/tests/test_optimization.py index 4960492c..4e80df74 100644 --- a/python/pynumerics/tests/test_optimization.py +++ b/python/pynumerics/tests/test_optimization.py @@ -505,31 +505,43 @@ def test_augmented_lagrange_applies_the_policy_to_python_constraints() -> None: assert not inner.use_parallel -@needs_roslyn -def test_differential_evolution_compiled_objective_gives_the_python_bits_faster() -> None: - # Arrange - def run(objective: object) -> tuple[np.ndarray, float, float]: - best = np.inf - for _ in range(3): - optimizer = DifferentialEvolution(objective, [-5.0, -5.0], [5.0, 5.0], population_size=40, - record_traces=False, compute_hessian=False) - start = time.perf_counter() - result = optimizer.minimize() - best = min(best, time.perf_counter() - start) - return result.parameters, result.value, best +def _run_ackley_differential_evolution(objective: object) -> tuple[np.ndarray, float, float]: + """Minimize Ackley with DifferentialEvolution three times; return the parameters, value and best seconds.""" + best = np.inf + for _ in range(3): + optimizer = DifferentialEvolution(objective, [-5.0, -5.0], [5.0, 5.0], population_size=40, + record_traces=False, compute_hessian=False) + start = time.perf_counter() + result = optimizer.minimize() + best = min(best, time.perf_counter() - start) + return result.parameters, result.value, best + - def ackley_math(x: np.ndarray) -> float: - import math # noqa: PLC0415 +def _ackley_math(x: np.ndarray) -> float: + """Return the two-dimensional Ackley function with the math module, as ACKLEY_CS computes it.""" + import math # noqa: PLC0415 - return (-20.0 * math.exp(-0.2 * math.sqrt(0.5 * (x[0] * x[0] + x[1] * x[1]))) - - math.exp(0.5 * (math.cos(2 * math.pi * x[0]) + math.cos(2 * math.pi * x[1]))) + math.e + 20.0) + return (-20.0 * math.exp(-0.2 * math.sqrt(0.5 * (x[0] * x[0] + x[1] * x[1]))) + - math.exp(0.5 * (math.cos(2 * math.pi * x[0]) + math.cos(2 * math.pi * x[1]))) + math.e + 20.0) + +@needs_roslyn +def test_differential_evolution_compiled_objective_gives_the_python_bits() -> None: # Act - python_x, python_value, python_seconds = run(ackley_math) - compiled_x, compiled_value, compiled_seconds = run(ACKLEY_CS) + python_x, python_value, _ = _run_ackley_differential_evolution(_ackley_math) + compiled_x, compiled_value, _ = _run_ackley_differential_evolution(ACKLEY_CS) # Assert np.testing.assert_allclose(compiled_x, python_x, atol=1e-12) assert compiled_value == pytest.approx(python_value, abs=1e-14) + + +@pytest.mark.benchmark +@needs_roslyn +def test_differential_evolution_compiled_objective_is_faster_than_python() -> None: + # Act + _, _, python_seconds = _run_ackley_differential_evolution(_ackley_math) + _, _, compiled_seconds = _run_ackley_differential_evolution(ACKLEY_CS) + # Assert assert compiled_seconds * 3 < python_seconds, (compiled_seconds, python_seconds) diff --git a/python/pynumerics/tests/test_sampling.py b/python/pynumerics/tests/test_sampling.py index 61324ea8..78ce7188 100644 --- a/python/pynumerics/tests/test_sampling.py +++ b/python/pynumerics/tests/test_sampling.py @@ -296,15 +296,34 @@ def jackknife(d: np.ndarray, i: int) -> np.ndarray: assert np.float64(serial_bca.upper_ci).view(np.uint64) == np.float64(parallel_bca.upper_ci).view(np.uint64) -@needs_roslyn -def test_csharp_bootstrap_functions_draw_the_same_replicates_faster() -> None: - # Arrange - compiled = sampling.Bootstrap( +def _compiled_mean_bootstrap() -> sampling.Bootstrap: + """Return the 2,000-replicate mean bootstrap of _mean_bootstrap with C# resample, fit and statistic.""" + return sampling.Bootstrap( X, [X.mean()], resample="(d, p, r) => { var o = new double[d.Length]; for (int i = 0; i < d.Length; i++) " "o[i] = d[r.Next(d.Length)]; return o; }", fit="d => new Numerics.Mathematics.Optimization.ParameterSet(new[] { d.Average() }, double.NaN)", statistic="p => p.Values", replicates=2000) + + +@needs_roslyn +def test_csharp_bootstrap_functions_draw_the_same_replicates() -> None: + # Arrange + compiled = _compiled_mean_bootstrap() + python = _mean_bootstrap() + # Act + python.run() + compiled.run() + # Assert: the same resamples; the means differ only in summation order + assert compiled.bootstrap_statistics == pytest.approx(python.bootstrap_statistics, rel=1e-14) + assert compiled.use_parallel and not python.use_parallel + + +@pytest.mark.benchmark +@needs_roslyn +def test_csharp_bootstrap_functions_draw_the_replicates_faster() -> None: + # Arrange + compiled = _compiled_mean_bootstrap() python = _mean_bootstrap() # Act start = time.perf_counter() @@ -313,11 +332,9 @@ def test_csharp_bootstrap_functions_draw_the_same_replicates_faster() -> None: start = time.perf_counter() compiled.run() compiled_seconds = time.perf_counter() - start - # Assert: the same resamples; the means differ only in summation order - assert compiled.bootstrap_statistics == pytest.approx(python.bootstrap_statistics, rel=1e-14) + # Assert assert compiled_seconds < python_seconds, ( f"Bootstrap, 2000 replicates: Python {python_seconds:.3f} s, compiled {compiled_seconds:.3f} s") - assert compiled.use_parallel and not python.use_parallel @needs_roslyn diff --git a/python/pynumerics/tests/test_special.py b/python/pynumerics/tests/test_special.py index 5f749fbf..b8a07cd9 100644 --- a/python/pynumerics/tests/test_special.py +++ b/python/pynumerics/tests/test_special.py @@ -366,16 +366,17 @@ def test_debye_functions_match_quadrature() -> None: np.testing.assert_allclose(special.debye3(x), [_debye(3, v) for v in x], rtol=1e-14) -def test_factorials_and_binomial_coefficients_match_scipy() -> None: - # Arrange +def test_factorials_and_binomial_coefficients_match_exact_values() -> None: + # Arrange: the oracle is the exact integer rounded to double. SciPy's floating factorial is a gamma + # approximation whose accuracy depends on the SciPy version and platform (1.17 on Windows: 8e-14). n = np.arange(0, 171) # Act / Assert - np.testing.assert_allclose(special.factorial(n), sp.factorial(n), rtol=2e-15) + np.testing.assert_allclose(special.factorial(n), [float(math.factorial(k)) for k in n], rtol=2e-15) np.testing.assert_array_equal(special.factorial(np.arange(0, 23)), [math.factorial(k) for k in range(23)]) assert special.factorial(171) == math.inf np.testing.assert_allclose(special.log_factorial(np.arange(2, 500)), sp.gammaln(np.arange(3, 501)), rtol=1e-15) nn, kk = np.meshgrid(np.arange(60), np.arange(60), indexing="ij") - expected = np.where(kk <= nn, sp.comb(nn, kk, exact=False), 0.0) + expected = np.array([[float(math.comb(a, b)) for b in range(60)] for a in range(60)]) np.testing.assert_allclose(special.binomial_coefficient(nn, kk), expected, rtol=1e-15) assert special.binomial_coefficient(52, 5) == math.comb(52, 5) with pytest.raises(ValueError): @@ -439,6 +440,7 @@ def test_gamma_max_and_poles() -> None: special.gamma_function(0.0) +@pytest.mark.benchmark def test_scalar_special_functions_are_bound_once() -> None: # Arrange x = np.linspace(0.5, 5.0, 2000) @@ -451,6 +453,23 @@ def test_scalar_special_functions_are_bound_once() -> None: assert elapsed < 0.2 +def test_array_arguments_into_the_list_overloads_give_the_scalar_bits() -> None: + # Arrange: 20000 arguments through the IList overload, against the scalar members + x = np.linspace(0.5, 30.0, 20000) + a = np.full_like(x, 3.5) + gamma = _sf().Gamma + j0 = _sf().Bessel.J0.Overloads[float] + # Act + vector_j0 = special.bessel_j0(x) + vector_p = special.lower_incomplete_gamma(a, x) + scalar_j0 = [j0(float(v)) for v in x] + scalar_p = [gamma.LowerIncomplete(3.5, float(v)) for v in x] + # Assert + np.testing.assert_array_equal(_bits(vector_j0), _bits(scalar_j0)) + np.testing.assert_array_equal(_bits(vector_p), _bits(scalar_p)) + + +@pytest.mark.benchmark def test_array_arguments_cross_into_the_list_overloads_once() -> None: # Arrange: 20000 arguments; one crossing into the IList overload, against one crossing per value x = np.linspace(0.5, 30.0, 20000) @@ -461,16 +480,14 @@ def test_array_arguments_cross_into_the_list_overloads_once() -> None: j0 = _sf().Bessel.J0.Overloads[float] # Act start = time.perf_counter() - vector_j0 = special.bessel_j0(x) - vector_p = special.lower_incomplete_gamma(a, x) + special.bessel_j0(x) + special.lower_incomplete_gamma(a, x) vectorized = time.perf_counter() - start start = time.perf_counter() - scalar_j0 = [j0(float(v)) for v in x] - scalar_p = [gamma.LowerIncomplete(3.5, float(v)) for v in x] + _ = [j0(float(v)) for v in x] + _ = [gamma.LowerIncomplete(3.5, float(v)) for v in x] per_value = time.perf_counter() - start - # Assert: the same bits, at least five times faster than a crossing per value - np.testing.assert_array_equal(_bits(vector_j0), _bits(scalar_j0)) - np.testing.assert_array_equal(_bits(vector_p), _bits(scalar_p)) + # Assert: at least five times faster than a crossing per value assert vectorized * 5 < per_value diff --git a/python/pynumerics/tests/test_statistics_coverage.py b/python/pynumerics/tests/test_statistics_coverage.py index aeb0adc5..81417b61 100644 --- a/python/pynumerics/tests/test_statistics_coverage.py +++ b/python/pynumerics/tests/test_statistics_coverage.py @@ -264,19 +264,30 @@ def test_given_data_sensitivity_indices_match_scipy_and_analytic_values() -> Non statistics.first_order_sobol(x1[:10], y[:10]) -def test_bulk_statistics_cross_once_for_large_arrays() -> None: +def test_bulk_statistics_on_large_arrays_equal_numpy_and_scipy() -> None: # Arrange x = np.random.default_rng(1).normal(size=500_000) - statistics.skewness(x[:10]) # Act - start = time.perf_counter() summary = statistics.seven_number_summary(x) skew = statistics.skewness(x) ess = statistics.effective_sample_size(np.abs(x)) - elapsed = time.perf_counter() - start - # Assert: bulk copies, not one crossing per value + # Assert assert summary[3] == pytest.approx(np.median(x), rel=1e-12) assert skew == pytest.approx(stats.skew(x, bias=False), rel=1e-9) and ess > 0 + + +@pytest.mark.benchmark +def test_bulk_statistics_cross_once_for_large_arrays() -> None: + # Arrange + x = np.random.default_rng(1).normal(size=500_000) + statistics.skewness(x[:10]) + # Act + start = time.perf_counter() + statistics.seven_number_summary(x) + statistics.skewness(x) + statistics.effective_sample_size(np.abs(x)) + elapsed = time.perf_counter() - start + # Assert: bulk copies, not one crossing per value assert elapsed < 1.0 diff --git a/python/pynumerics/tests/test_timeseries.py b/python/pynumerics/tests/test_timeseries.py index 954dce59..d50aa231 100644 --- a/python/pynumerics/tests/test_timeseries.py +++ b/python/pynumerics/tests/test_timeseries.py @@ -201,6 +201,16 @@ def test_index_of_date_on_an_unsorted_series_returns_the_first_match() -> None: assert empty.index_of_date("2020-01-01") == -1 +def test_index_of_date_finds_the_last_of_many_dates() -> None: + # Arrange + series = _daily(np.zeros(100_000), start="1800-01-01") + # Act + position = series.index_of_date(series.dates[-1]) + # Assert + assert position == 99_999 + + +@pytest.mark.benchmark def test_index_of_date_crosses_the_dates_once() -> None: # Arrange: 100,000 ordinates; one lookup copies the ticks in one transfer (about 0.3 ms measured) series = _daily(np.zeros(100_000), start="1800-01-01") @@ -208,10 +218,9 @@ def test_index_of_date_crosses_the_dates_once() -> None: series.index_of_date(last) # Act begin = time.perf_counter() - position = series.index_of_date(last) + series.index_of_date(last) seconds = time.perf_counter() - begin # Assert - assert position == 99_999 assert seconds < 0.02, seconds @@ -699,38 +708,69 @@ def test_invalid_arguments_raise_value_error() -> None: # Speed ---------------------------------------------------------------------------------------- +def test_values_and_dates_in_bulk_equal_the_per_ordinate_values() -> None: + # Arrange + series = _daily(_gamma(20000)) + # Act + values, dates = series.values, series.dates + per_ordinate = np.array([float(o.Value) for o in series.net]) + # Assert + np.testing.assert_array_equal(values, per_ordinate) + assert len(dates) == 20000 + + +@pytest.mark.benchmark def test_values_and_dates_cross_in_bulk_faster_than_per_ordinate() -> None: # Arrange series = _daily(_gamma(20000)) net = series.net # Act start = time.perf_counter() - values, dates = series.values, series.dates + _ = series.values, series.dates bulk = time.perf_counter() - start start = time.perf_counter() - slow = np.array([float(o.Value) for o in net]) + _ = [float(o.Value) for o in net] per_element = time.perf_counter() - start # Assert - np.testing.assert_array_equal(values, slow) - assert len(dates) == 20000 assert bulk * 10.0 < per_element, (bulk, per_element) -def test_explicit_dates_build_faster_than_appending_each_ordinate() -> None: - # Arrange +def _irregular_dates_and_values() -> tuple[np.ndarray, np.ndarray]: + """Return 5,000 sorted irregular daily dates and gamma values.""" n = 5000 dates = np.datetime64("2000-01-01") + np.sort(np.random.default_rng(1).choice(20000, n, replace=False)) - values = _gamma(n) + return dates, _gamma(n) + + +def _append_each_ordinate(dates: np.ndarray, values: np.ndarray) -> TimeSeries: + """Build an irregular series by appending one ordinate at a time.""" + series = TimeSeries(interval="irregular") + for d, v in zip(dates, values, strict=True): + series.append(d, v) + return series + + +def test_explicit_dates_build_the_series_that_appending_each_ordinate_builds() -> None: + # Arrange + dates, values = _irregular_dates_and_values() # Act - start = time.perf_counter() bulk = TimeSeries(values, interval="irregular", dates=dates) + appended = _append_each_ordinate(dates, values) + # Assert + np.testing.assert_array_equal(bulk.ticks, appended.ticks) + np.testing.assert_array_equal(bulk.values, appended.values) + + +@pytest.mark.benchmark +def test_explicit_dates_build_faster_than_appending_each_ordinate() -> None: + # Arrange + dates, values = _irregular_dates_and_values() + # Act + start = time.perf_counter() + TimeSeries(values, interval="irregular", dates=dates) bulk_time = time.perf_counter() - start start = time.perf_counter() - slow = TimeSeries(interval="irregular") - for d, v in zip(dates, values, strict=True): - slow.append(d, v) + _append_each_ordinate(dates, values) append_time = time.perf_counter() - start # Assert - np.testing.assert_array_equal(bulk.ticks, slow.ticks) - np.testing.assert_array_equal(bulk.values, slow.values) assert bulk_time < append_time, (bulk_time, append_time) diff --git a/python/pynumerics/tests/test_tools_fourier.py b/python/pynumerics/tests/test_tools_fourier.py index 02470f60..4e33b691 100644 --- a/python/pynumerics/tests/test_tools_fourier.py +++ b/python/pynumerics/tests/test_tools_fourier.py @@ -107,6 +107,7 @@ def test_log_sum_exp_axis_matches_scipy_and_list() -> None: m.log_sum_exp(matrix, 1.0, axis=0) +@pytest.mark.benchmark def test_log_sum_exp_reduces_in_one_crossing() -> None: # Arrange values = np.random.default_rng(2).normal(size=200_000) @@ -297,19 +298,28 @@ def test_compensated_cumulative_sum_equals_prefix_fsum() -> None: assert tail[1] == math.inf and math.isnan(tail[3]) +def test_compensated_sum_equals_math_fsum_on_a_large_array() -> None: + # Arrange + values = np.random.default_rng(12).standard_normal(1_000_000) + # Act + total = m.compensated_sum(values) + # Assert + assert total == pytest.approx(math.fsum(values), rel=1e-15, abs=1e-12) + + +@pytest.mark.benchmark def test_compensated_sum_beats_math_fsum_on_a_large_array() -> None: # Arrange values = np.random.default_rng(12).standard_normal(1_000_000) m.compensated_sum(values[:10]) # Act start = time.perf_counter() - total = m.compensated_sum(values) + m.compensated_sum(values) numerics_seconds = time.perf_counter() - start start = time.perf_counter() - exact = math.fsum(values) + math.fsum(values) fsum_seconds = time.perf_counter() - start # Assert - assert total == pytest.approx(exact, rel=1e-15, abs=1e-12) assert numerics_seconds < fsum_seconds, (numerics_seconds, fsum_seconds) @@ -430,6 +440,7 @@ def test_autocorrelation_matches_statsmodels() -> None: np.testing.assert_allclose(table[:, 1], acf(series, nlags=20, fft=True), rtol=0, atol=1e-14) +@pytest.mark.benchmark def test_large_fft_crosses_once() -> None: # Arrange x = np.random.default_rng(10).normal(size=2**18).astype(complex) diff --git a/python/pynumerics/tests/test_univariate.py b/python/pynumerics/tests/test_univariate.py index 8990d847..8d3bd88e 100644 --- a/python/pynumerics/tests/test_univariate.py +++ b/python/pynumerics/tests/test_univariate.py @@ -402,24 +402,35 @@ def test_inverse_ccdf_keeps_survival_precision_for_the_generalized_pareto(u, sig assert quantile == pytest.approx(float(stats.genpareto(xi, u, sigma).isf(q)), rel=1e-13) -def test_inverse_ccdf_array_crosses_once_and_equals_the_scalar_member() -> None: +def test_inverse_ccdf_array_equals_the_scalar_member_and_scipy() -> None: # Arrange gpa = d.GeneralizedPareto(1.0, 2.0, -0.2) q = np.geomspace(1e-14, 0.9, 20000) # Act - start = time.perf_counter() array = gpa.inverse_ccdf(q) + loop = [float(gpa.net.InverseCCDF(float(v))) for v in q] + # Assert + assert _bits(array) == _bits(loop) + np.testing.assert_allclose(array, stats.genpareto(0.2, 1.0, 2.0).isf(q), rtol=1e-12) + assert d.Normal().inverse_ccdf(0.025) == d.Normal().inverse_cdf(0.975) + assert gpa.inverse_ccdf(np.array([[0.1, 0.2]])).shape == (1, 2) + + +@pytest.mark.benchmark +def test_inverse_ccdf_array_crosses_once_faster_than_scalar_calls() -> None: + # Arrange + gpa = d.GeneralizedPareto(1.0, 2.0, -0.2) + q = np.geomspace(1e-14, 0.9, 20000) + # Act + start = time.perf_counter() + gpa.inverse_ccdf(q) vector_time = time.perf_counter() - start scalar = gpa.net.InverseCCDF start = time.perf_counter() - loop = [float(scalar(float(v))) for v in q] + _ = [float(scalar(float(v))) for v in q] loop_time = time.perf_counter() - start # Assert - assert _bits(array) == _bits(loop) assert vector_time * 5 < loop_time, (vector_time, loop_time) - np.testing.assert_allclose(array, stats.genpareto(0.2, 1.0, 2.0).isf(q), rtol=1e-12) - assert d.Normal().inverse_ccdf(0.025) == d.Normal().inverse_cdf(0.975) - assert gpa.inverse_ccdf(np.array([[0.1, 0.2]])).shape == (1, 2) POISSON_GPD = [ # (rate, u, sigma, xi hydrologic, location, scale): Stochastics reference vectors