Skip to content

Commit 8b8cfc1

Browse files
gururaj1512kgryte
andauthored
feat: add stats/base/ztest/two-sample/results/struct-factory
PR-URL: #7447 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 8905d11 commit 8b8cfc1

File tree

10 files changed

+908
-0
lines changed

10 files changed

+908
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
# structFactory
22+
23+
> Create a new [`struct`][@stdlib/dstructs/struct] constructor tailored to a specified floating-point data type.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var structFactory = require( '@stdlib/stats/base/ztest/two-sample/results/struct-factory' );
41+
```
42+
43+
#### structFactory( dtype )
44+
45+
Returns a new [`struct`][@stdlib/dstructs/struct] constructor tailored to a specified floating-point data type.
46+
47+
```javascript
48+
var Struct = structFactory( 'float64' );
49+
// returns <Function>
50+
51+
var s = new Struct();
52+
// returns <Struct>
53+
```
54+
55+
The function supports the following parameters:
56+
57+
- **dtype**: floating-point data type for storing floating-point results. Must be either `'float64'` or `'float32'`.
58+
59+
</section>
60+
61+
<!-- /.usage -->
62+
63+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
64+
65+
<section class="notes">
66+
67+
## Notes
68+
69+
- A [`struct`][@stdlib/dstructs/struct] provides a fixed-width composite data structure for storing two-sample Z-test results and provides an ABI-stable data layout for JavaScript-C interoperation.
70+
71+
</section>
72+
73+
<!-- /.notes -->
74+
75+
<!-- Package usage examples. -->
76+
77+
<section class="examples">
78+
79+
## Examples
80+
81+
<!-- eslint no-undef: "error" -->
82+
83+
```javascript
84+
var resolveEnum = require( '@stdlib/stats/base/ztest/alternative-resolve-enum' );
85+
var Float64Array = require( '@stdlib/array/float64' );
86+
var Float32Array = require( '@stdlib/array/float32' );
87+
var structFactory = require( '@stdlib/stats/base/ztest/two-sample/results/struct-factory' );
88+
89+
var Struct = structFactory( 'float64' );
90+
var results = new Struct({
91+
'rejected': true,
92+
'alpha': 0.05,
93+
'pValue': 0.0132,
94+
'statistic': 2.4773,
95+
'nullValue': 0.0,
96+
'xmean': 3.7561,
97+
'ymean': 3.0129,
98+
'ci': new Float64Array( [ 0.1552, 1.3311 ] ),
99+
'alternative': resolveEnum( 'two-sided' )
100+
});
101+
102+
var str = results.toString({
103+
'format': 'linear'
104+
});
105+
console.log( str );
106+
107+
Struct = structFactory( 'float32' );
108+
results = new Struct({
109+
'rejected': true,
110+
'alpha': 0.05,
111+
'pValue': 0.0132,
112+
'statistic': 2.4773,
113+
'nullValue': 0.0,
114+
'xmean': 3.7561,
115+
'ymean': 3.0129,
116+
'ci': new Float32Array( [ 0.1552, 1.3311 ] ),
117+
'alternative': resolveEnum( 'two-sided' )
118+
});
119+
120+
str = results.toString({
121+
'format': 'linear'
122+
});
123+
console.log( str );
124+
```
125+
126+
</section>
127+
128+
<!-- /.examples -->
129+
130+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
131+
132+
<section class="references">
133+
134+
</section>
135+
136+
<!-- /.references -->
137+
138+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
139+
140+
<section class="related">
141+
142+
</section>
143+
144+
<!-- /.related -->
145+
146+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
147+
148+
<section class="links">
149+
150+
[@stdlib/dstructs/struct]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/dstructs/struct
151+
152+
</section>
153+
154+
<!-- /.links -->
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isFunction = require( '@stdlib/assert/is-function' );
25+
var pkg = require( './../package.json' ).name;
26+
var factory = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var v;
34+
var i;
35+
36+
values = [
37+
'float64',
38+
'float32'
39+
];
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
v = factory( values[ i%values.length ] );
44+
if ( typeof v !== 'function' ) {
45+
b.fail( 'should return a function' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isFunction( v ) ) {
50+
b.fail( 'should return a function' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
{{alias}}( dtype )
3+
Returns a new struct constructor tailored to a specified floating-point data
4+
type.
5+
6+
Parameters
7+
----------
8+
dtype: string
9+
Floating-point data type for storing floating-point results.
10+
11+
Returns
12+
-------
13+
fcn: Function
14+
Struct constructor.
15+
16+
Examples
17+
--------
18+
> var S = {{alias}}( 'float64' );
19+
> var r = new S();
20+
> r.toString( { 'format': 'linear' } )
21+
<string>
22+
23+
See Also
24+
--------
25+

0 commit comments

Comments
 (0)