diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/test/fixtures/python/data.json b/lib/node_modules/@stdlib/stats/incr/wmean/test/fixtures/python/data.json new file mode 100644 index 000000000000..9a1cc4fb45a4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/test/fixtures/python/data.json @@ -0,0 +1 @@ +[{"step": 1, "x": 3.0471707975443136, "w": 3.9559294497604784, "mean": 3.0471707975443136}, {"step": 2, "x": -10.399841062404956, "w": 3.3577691973009576, "mean": -3.126444550583255}, {"step": 3, "x": 7.5045119580645725, "w": 3.555310355269042, "mean": 0.3509975744077484}, {"step": 4, "x": 9.405647163912139, "w": 3.925572252007643, "mean": 2.753544869877046}, {"step": 5, "x": -19.510351886538363, "w": 2.348687300137866, "mean": -0.2966862687887889}, {"step": 6, "x": -13.021795068623181, "w": 2.88683186016918, "mean": -2.1306885327326004}, {"step": 7, "x": 1.2784040316728538, "w": 0.7850052908255215, "mean": -2.0021205716643617}, {"step": 8, "x": -3.162425923435822, "w": 0.6611973603262699, "mean": -2.0378432392675006}, {"step": 9, "x": -0.16801157504288794, "w": 3.3751745127733113, "mean": -1.7838942273298977}, {"step": 10, "x": -8.5304392757358, "w": 2.4083714101013496, "mean": -2.379942511320534}, {"step": 11, "x": 8.793979748628285, "w": 2.8696569217578256, "mean": -1.3156926211374218}, {"step": 12, "x": 7.777919354289483, "w": 3.8484944013385256, "mean": -0.28571103145409305}, {"step": 13, "x": 0.6603069756121605, "w": 3.2101197680028952, "mean": -0.2040497079223875}, {"step": 14, "x": 11.272412069680328, "w": 2.81253906322418, "mean": 0.6028869213396181}, {"step": 15, "x": 4.675093422520456, "w": 2.8401150876525265, "mean": 0.872852516773853}, {"step": 16, "x": -8.592924628832382, "w": 1.5893554805068, "mean": 0.5342425491363226}, {"step": 17, "x": 3.6875078408249884, "w": 0.2510073893829031, "mean": 0.5519568003899903}, {"step": 18, "x": -9.588826008289988, "w": 2.239915207238576, "mean": 0.0678565402877354}, {"step": 19, "x": 8.784503013072726, "w": 1.1514648968156933, "mean": 0.2766434525404932}, {"step": 20, "x": -0.49925910986252897, "w": 2.1017903542507175, "mean": 0.2441410703285499}, {"step": 21, "x": -1.8486236354526056, "w": 4.281675059014014, "mean": 0.07959462355871812}, {"step": 22, "x": -6.809295444039414, "w": 1.2463034807401698, "mean": -0.07453994202242903}, {"step": 23, "x": 12.225413386740303, "w": 0.3856834342764235, "mean": 0.010039483490497911}, {"step": 24, "x": -1.5452948206880215, "w": 1.4787810709077833, "mean": -0.029914116527385744}, {"step": 25, "x": -4.283278221631072, "w": 1.5386094130567498, "mean": -0.14063615675192645}, {"step": 26, "x": -3.5213355048822956, "w": 3.343390922161786, "mean": -0.32163256326353584}, {"step": 27, "x": 5.323091855533487, "w": 2.829457546472264, "mean": -0.07696426621497057}, {"step": 28, "x": 3.6544406436407835, "w": 3.9411012246214265, "mean": 0.13548865366219276}, {"step": 29, "x": 4.1273261159598835, "w": 3.355136347604199, "mean": 0.3200323834598058}, {"step": 30, "x": 4.308210030078827, "w": 2.0912956210563456, "mean": 0.4317363357791127}, {"step": 31, "x": 21.416476008704613, "w": 4.08869988486357, "mean": 1.5212025421425204}, {"step": 32, "x": -4.064150163846156, "w": 0.9181673075477492, "mean": 1.4568355240616275}, {"step": 33, "x": -5.122427290715374, "w": 0.21128915835591636, "mean": 1.4394336829268486}, {"step": 34, "x": -8.137727282478778, "w": 0.5412345178006446, "mean": 1.3749825600895866}, {"step": 35, "x": 6.159794225754957, "w": 3.639560817922607, "mean": 1.582139838161509}, {"step": 36, "x": 11.289722927208917, "w": 2.3631984282317986, "mean": 1.8475743480392357}, {"step": 37, "x": -1.1394745765487508, "w": 0.8902317172646488, "mean": 1.8171205821224452}, {"step": 38, "x": -8.401564769625281, "w": 2.5551193980064815, "mean": 1.5266007308371616}, {"step": 39, "x": -8.244812156912396, "w": 0.8463293032945253, "mean": 1.435442529141028}, {"step": 40, "x": 6.505927878247011, "w": 3.511969837880907, "mean": 1.6244173800465052}, {"step": 41, "x": 7.432541712034423, "w": 2.2861657503127506, "mean": 1.761991460740914}, {"step": 42, "x": 5.4315426830519495, "w": 1.9670040078727644, "mean": 1.8352822469844088}, {"step": 43, "x": -6.655097072886943, "w": 1.577409236824595, "mean": 1.7014373651904717}, {"step": 44, "x": 2.3216132306671975, "w": 3.188384706282554, "mean": 1.7205884505077298}, {"step": 45, "x": 1.1668580914072821, "w": 1.8728817917116134, "mean": 1.7107231718926006}, {"step": 46, "x": 2.1868859672901295, "w": 0.5294846046488949, "mean": 1.7131094853391238}, {"step": 47, "x": 8.714287779481898, "w": 0.6782289203905251, "mean": 1.7577662372038756}, {"step": 48, "x": 2.2359554877468226, "w": 4.813298556292621, "mean": 1.7784750415795718}, {"step": 49, "x": 6.789135630718949, "w": 4.552045384467275, "mean": 1.9756181293054198}, {"step": 50, "x": 0.6757906948889146, "w": 3.5285649556726733, "mean": 1.9371486375865798}] diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/test/fixtures/python/runner.py b/lib/node_modules/@stdlib/stats/incr/wmean/test/fixtures/python/runner.py new file mode 100644 index 000000000000..2540b53c5af5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/wmean/test/fixtures/python/runner.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# +# @license Apache-2.0 +# +# Copyright (c) 2026 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Generate fixtures.""" + +import os +import json +import numpy as np + +# Get the file path: +FILE = os.path.realpath(__file__) + +# Extract the directory in which this file resides: +DIR = os.path.dirname(FILE) + + +def gen(n, seed, name): + """Generate fixture data and write to file. + + # Arguments + + * `n`: number of samples + * `seed`: random number generator seed + * `name::str`: output filename + + # Examples + + ``` python + python> gen(50, 42, './data.json') + ``` + """ + # Initialize random number generator for reproducibility: + rng = np.random.default_rng(seed) + + # Generate data: + x = rng.normal(loc=0.0, scale=10.0, size=n) + w = rng.uniform(0.1, 5.0, size=n) + + # Store data to be written to file as a list of records: + records = [] + + for k in range(1, n + 1): + x_vals = x[:k] + w_vals = w[:k] + + weighted_mean = np.average(x_vals, weights=w_vals) + + records.append({ + "step": k, + "x": float(x[k - 1]), + "w": float(w[k - 1]), + "mean": float(weighted_mean) + }) + + # Based on the script directory, create an output filepath: + filepath = os.path.join(DIR, name) + + # Write the data to the output filepath as JSON: + with open(filepath, "w", encoding="utf-8") as outfile: + json.dump(records, outfile) + + # Include trailing newline: + with open(filepath, "a", encoding="utf-8") as outfile: + outfile.write("\n") + + +def main(): + """Generate fixture data.""" + gen(50, 42, "data.json") + + +if __name__ == "__main__": + main() diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js index b987122ec405..0498ea3a0a9f 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js @@ -21,12 +21,16 @@ // MODULES // var tape = require( 'tape' ); -var EPS = require( '@stdlib/constants/float64/eps' ); -var abs = require( '@stdlib/math/base/special/abs' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' ); var incrwmean = require( './../lib' ); +// FIXTURES // + +var data = require( './fixtures/python/data.json' ); + + // TESTS // tape( 'main export is a function', function test( t ) { @@ -49,53 +53,37 @@ tape( 'the initial accumulated value is `null`', function test( t ) { tape( 'the accumulator function incrementally computes a weighted arithmetic mean', function test( t ) { var expected; var actual; - var delta; - var dataX; - var dataW; - var xwSum; - var wSum; var acc; - var tol; var N; var x; var w; var i; - dataX = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; - dataW = [ 1.0, 2.0, 0.1, 1.8, 9.9, 3.6 ]; - N = dataX.length; + N = data.length; acc = incrwmean(); - xwSum = 0.0; - wSum = 0.0; for ( i = 0; i < N; i++ ) { - x = dataX[ i ]; - w = dataW[ i ]; - xwSum += x * w; - wSum += w; - expected = xwSum / wSum; + x = data[ i ].x; + w = data[ i ].w; + expected = data[ i ].mean; actual = acc( x, w ); - delta = abs( actual - expected ); - tol = EPS * abs( expected ); - t.ok( delta <= tol, 'within tolerance. x: ' + x + '. Value: ' + actual + '. Expected: ' + expected + '. Tolerance: ' + tol + '.' ); + t.strictEqual( isAlmostSameValue( actual, expected, 150.0 ), true, 'within tolerance. x: ' + x + '. w: ' + w + '. Value: ' + actual + '. Expected: ' + expected + '.' ); } t.end(); }); tape( 'if not provided arguments, the accumulator function returns the current weighted mean', function test( t ) { - var dataX; - var dataW; var acc; + var N; var i; - dataX = [ 2.0, 3.0, 1.0 ]; - dataW = [ 2.0, 2.0, 1.0 ]; + N = data.length; acc = incrwmean(); - for ( i = 0; i < dataX.length; i++ ) { - acc( dataX[ i ], dataW[ i ] ); + for ( i = 0; i < N; i++ ) { + acc( data[ i ].x, data[ i ].w ); } - t.strictEqual( acc(), 2.2, 'returns the current accumulated mean' ); + t.strictEqual( isAlmostSameValue( acc(), data[ N - 1 ].mean, 150.0 ), true, 'returns the current accumulated mean' ); t.end(); });