Skip to content

Commit c171387

Browse files
feat: wasm implementation for stats/strided/dnanvariancewd
1 parent fe96d97 commit c171387

35 files changed

+5141
-1
lines changed

lib/node_modules/@stdlib/stats/strided/dnanvariancewd/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
]
8282
},
8383
{
84-
"task": "",
84+
"task": "build",
8585
"wasm": true,
8686
"src": [
8787
"./src/main.c"
Lines changed: 329 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,329 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# dnanvariancewd
22+
23+
> Compute the [variance][variance] of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var dnanvariancewd = require( '@stdlib/stats/strided/wasm/dnanvariancewd' );
31+
```
32+
33+
#### dnanvariancewd.main( N, correction, x, strideX )
34+
35+
Computes the [variance][variance] of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm.
36+
37+
```javascript
38+
var Float64Array = require( '@stdlib/array/float64' );
39+
40+
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
41+
42+
var v = dnanvariancewd.main( x.length, 1, x, 1 );
43+
// returns ~4.3333
44+
```
45+
46+
The function has the following parameters:
47+
48+
- **N**: number of indexed elements.
49+
- **correction**: degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [variance][variance] according to `n-c` where `c` corresponds to the provided degrees of freedom adjustment and `n` corresponds to the number of non-`NaN` indexed elements. When computing the [variance][variance] of a population, setting this parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the unbiased sample [variance][variance], setting this parameter to `1` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction).
50+
- **x**: input [`Float64Array`][@stdlib/array/float64].
51+
- **strideX**: stride length for `x`.
52+
53+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element in `x`,
54+
55+
```javascript
56+
var Float64Array = require( '@stdlib/array/float64' );
57+
58+
// eslint-disable-next-line max-len
59+
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ] );
60+
61+
var v = dnanvariancewd.main( 5, 1, x, 2 );
62+
// returns 6.25
63+
```
64+
65+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
66+
67+
<!-- eslint-disable stdlib/capitalized-comments -->
68+
69+
```javascript
70+
var Float64Array = require( '@stdlib/array/float64' );
71+
72+
// eslint-disable-next-line max-len
73+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
74+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
75+
76+
var v = dnanvariancewd.main( 5, 1, x1, 2 );
77+
// returns 6.25
78+
```
79+
80+
#### dnanvariancewd.ndarray( N, correction, x, strideX, offsetX )
81+
82+
Computes the [variance][variance] of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics.
83+
84+
```javascript
85+
var Float64Array = require( '@stdlib/array/float64' );
86+
87+
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
88+
89+
var v = dnanvariancewd.ndarray( x.length, 1, x, 1, 0 );
90+
// returns ~4.3333
91+
```
92+
93+
The function has the following additional parameters:
94+
95+
- **offsetX**: starting index for `x`.
96+
97+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access every other element starting from the second element:
98+
99+
```javascript
100+
var Float64Array = require( '@stdlib/array/float64' );
101+
102+
// eslint-disable-next-line max-len
103+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
104+
105+
var v = dnanvariancewd.ndarray( 5, 1, x, 2, 1 );
106+
// returns 6.25
107+
```
108+
109+
* * *
110+
111+
### Module
112+
113+
#### dnanvariancewd.Module( memory )
114+
115+
Returns a new WebAssembly [module wrapper][@stdlib/wasm/module-wrapper] instance which uses the provided WebAssembly [memory][@stdlib/wasm/memory] instance as its underlying memory.
116+
117+
<!-- eslint-disable node/no-sync -->
118+
119+
```javascript
120+
var Memory = require( '@stdlib/wasm/memory' );
121+
122+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
123+
var mem = new Memory({
124+
'initial': 10,
125+
'maximum': 100
126+
});
127+
128+
// Create a new routine:
129+
var mod = new dnanvariancewd.Module( mem );
130+
// returns <Module>
131+
132+
// Initialize the routine:
133+
mod.initializeSync();
134+
```
135+
136+
#### dnanvariancewd.Module.prototype.main( N, correction, xp, sx )
137+
138+
Computes the [variance][variance] of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm.
139+
140+
<!-- eslint-disable node/no-sync -->
141+
142+
```javascript
143+
var Memory = require( '@stdlib/wasm/memory' );
144+
var oneTo = require( '@stdlib/array/one-to' );
145+
var zeros = require( '@stdlib/array/zeros' );
146+
147+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
148+
var mem = new Memory({
149+
'initial': 10,
150+
'maximum': 100
151+
});
152+
153+
// Create a new routine:
154+
var mod = new dnanvariancewd.Module( mem );
155+
// returns <Module>
156+
157+
// Initialize the routine:
158+
mod.initializeSync();
159+
160+
// Define a vector data type:
161+
var dtype = 'float64';
162+
163+
// Specify a vector length:
164+
var N = 3;
165+
166+
// Define a pointer (i.e., byte offset) for storing the input vector:
167+
var xptr = 0;
168+
169+
// Write vector values to module memory:
170+
mod.write( xptr, oneTo( N, dtype ) );
171+
172+
// Perform computation:
173+
var v = mod.main( N, 1, xptr, 1 );
174+
// returns 1.0
175+
```
176+
177+
The function has the following parameters:
178+
179+
- **N**: number of indexed elements.
180+
- **correction**: degrees of freedom adjustment.
181+
- **xp**: input [`Float64Array`][@stdlib/array/float64] pointer (i.e., byte offset).
182+
- **sx**: stride length for `x`.
183+
184+
#### dnanvariancewd.Module.prototype.ndarray( N, correction, xp, sx, ox )
185+
186+
Computes the [variance][variance] of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics.
187+
188+
<!-- eslint-disable node/no-sync -->
189+
190+
```javascript
191+
var Memory = require( '@stdlib/wasm/memory' );
192+
var oneTo = require( '@stdlib/array/one-to' );
193+
var zeros = require( '@stdlib/array/zeros' );
194+
195+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
196+
var mem = new Memory({
197+
'initial': 10,
198+
'maximum': 100
199+
});
200+
201+
// Create a new routine:
202+
var mod = new dnanvariancewd.Module( mem );
203+
// returns <Module>
204+
205+
// Initialize the routine:
206+
mod.initializeSync();
207+
208+
// Define a vector data type:
209+
var dtype = 'float64';
210+
211+
// Specify a vector length:
212+
var N = 3;
213+
214+
// Define a pointer (i.e., byte offset) for storing the input vector:
215+
var xptr = 0;
216+
217+
// Write vector values to module memory:
218+
mod.write( xptr, oneTo( N, dtype ) );
219+
220+
// Perform computation:
221+
var v = mod.ndarray( N, 1, xptr, 1, 0 );
222+
// returns 1.0
223+
```
224+
225+
The function has the following additional parameters:
226+
227+
- **ox**: starting index for `x`.
228+
229+
</section>
230+
231+
<!-- /.usage -->
232+
233+
<section class="notes">
234+
235+
* * *
236+
237+
## Notes
238+
239+
- If `N <= 0`, both `main` and `ndarray` methods return `NaN`.
240+
- If `n - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment and `n` corresponds to the number of non-`NaN` indexed elements), both `main` and `ndarray` methods return `NaN`.
241+
- This package implements routines using WebAssembly. When provided arrays which are not allocated on a `dnanvariancewd` module memory instance, data must be explicitly copied to module memory prior to computation. Data movement may entail a performance cost, and, thus, if you are using arrays external to module memory, you should prefer using [`@stdlib/stats/strided/dnanvariancewd`][@stdlib/stats/strided/dnanvariancewd]. However, if working with arrays which are allocated and explicitly managed on module memory, you can achieve better performance when compared to the pure JavaScript implementations found in [`@stdlib/stats/strided/dnanvariancewd`][@stdlib/stats/strided/dnanvariancewd]. Beware that such performance gains may come at the cost of additional complexity when having to perform manual memory management. Choosing between implementations depends heavily on the particular needs and constraints of your application, with no one choice universally better than the other.
242+
243+
</section>
244+
245+
<!-- /.notes -->
246+
247+
<section class="examples">
248+
249+
* * *
250+
251+
## Examples
252+
253+
<!-- eslint no-undef: "error" -->
254+
255+
```javascript
256+
var randu = require( '@stdlib/random/base/randu' );
257+
var round = require( '@stdlib/math/base/special/round' );
258+
var Float64Array = require( '@stdlib/array/float64' );
259+
var dnanvariancewd = require( '@stdlib/stats/strided/wasm/dnanvariancewd' );
260+
261+
var x;
262+
var i;
263+
264+
x = new Float64Array( 10 );
265+
for ( i = 0; i < x.length; i++ ) {
266+
if ( randu() < 0.2 ) {
267+
x[ i ] = NaN;
268+
} else {
269+
x[ i ] = round( randu() * 10.0 );
270+
}
271+
}
272+
console.log( x );
273+
274+
var v = dnanvariancewd.ndarray( x.length, 1, x, 1, 0 );
275+
console.log( v );
276+
```
277+
278+
</section>
279+
280+
<!-- /.examples -->
281+
282+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
283+
284+
<section class="related">
285+
286+
* * *
287+
288+
## See Also
289+
290+
- <span class="package-name">[`@stdlib/stats/strided/dvariancewd`][@stdlib/stats/strided/dvariancewd]</span><span class="delimiter">: </span><span class="description">calculate the variance of a double-precision floating-point strided array using Welford's algorithm.</span>
291+
- <span class="package-name">[`@stdlib/stats/strided/dnanvariance`][@stdlib/stats/strided/dnanvariance]</span><span class="delimiter">: </span><span class="description">calculate the variance of a double-precision floating-point strided array ignoring NaN values.</span>
292+
- <span class="package-name">[`@stdlib/stats/strided/nanvariancewd`][@stdlib/stats/strided/nanvariancewd]</span><span class="delimiter">: </span><span class="description">calculate the variance of a strided array ignoring NaN values and using Welford's algorithm.</span>
293+
- <span class="package-name">[`@stdlib/stats/base/snanvariancewd`][@stdlib/stats/base/snanvariancewd]</span><span class="delimiter">: </span><span class="description">calculate the variance of a single-precision floating-point strided array ignoring NaN values and using Welford's algorithm.</span>
294+
295+
</section>
296+
297+
<!-- /.related -->
298+
299+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
300+
301+
<section class="links">
302+
303+
[variance]: https://en.wikipedia.org/wiki/Variance
304+
305+
[@stdlib/wasm/memory]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/memory
306+
307+
[@stdlib/wasm/module-wrapper]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/module-wrapper
308+
309+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
310+
311+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
312+
313+
<!-- <related-links> -->
314+
315+
[@stdlib/stats/strided/dvariancewd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dvariancewd
316+
317+
[@stdlib/stats/strided/dnanvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanvariance
318+
319+
[@stdlib/stats/strided/nanvariancewd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/nanvariancewd
320+
321+
[@stdlib/stats/base/snanvariancewd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/snanvariancewd
322+
323+
[@stdlib/stats/strided/dnanvariancewd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanvariancewd
324+
325+
<!-- </related-links> -->
326+
327+
</section>
328+
329+
<!-- /.links -->

0 commit comments

Comments
 (0)