Skip to content

Commit abad8ce

Browse files
committed
feat: add stats/base/ztest/one-sample/results/to-json
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 355789c commit abad8ce

File tree

10 files changed

+656
-0
lines changed

10 files changed

+656
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
# res2json
22+
23+
> Serialize a one-sample Z-test results object as a JSON object.
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 res2json = require( '@stdlib/stats/base/ztest/one-sample/results/to-json' );
41+
```
42+
43+
#### res2json( results )
44+
45+
Serializes a one-sample Z-test results object as a JSON object.
46+
47+
```javascript
48+
var Float64Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
49+
50+
var results = new Float64Results();
51+
52+
// ...
53+
54+
var o = res2json( results );
55+
// returns {...}
56+
```
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
63+
64+
<section class="notes">
65+
66+
</section>
67+
68+
<!-- /.notes -->
69+
70+
<!-- Package usage examples. -->
71+
72+
<section class="examples">
73+
74+
## Examples
75+
76+
<!-- eslint no-undef: "error" -->
77+
78+
```javascript
79+
var Float64Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
80+
var resolveEnum = require( '@stdlib/stats/base/ztest/alternative-resolve-enum' );
81+
var Float64Array = require( '@stdlib/array/float64' );
82+
var res2json = require( '@stdlib/stats/base/ztest/one-sample/results/to-json' );
83+
84+
var results = new Float64Results();
85+
results.rejected = true;
86+
results.alpha = 0.05;
87+
results.pValue = 0.3364;
88+
results.statistic = 11.7586;
89+
results.nullValue = 0.0;
90+
results.sd = 0.4563;
91+
results.ci = new Float64Array( [ 9.9983, 11.4123 ] );
92+
results.alternative = resolveEnum( 'two-sided' );
93+
94+
var o = res2json( results );
95+
console.log( o );
96+
```
97+
98+
</section>
99+
100+
<!-- /.examples -->
101+
102+
<!-- 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. -->
103+
104+
<section class="references">
105+
106+
</section>
107+
108+
<!-- /.references -->
109+
110+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
111+
112+
<section class="related">
113+
114+
</section>
115+
116+
<!-- /.related -->
117+
118+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
119+
120+
<section class="links">
121+
122+
</section>
123+
124+
<!-- /.links -->
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 Float64Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
25+
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
26+
var pkg = require( './../package.json' ).name;
27+
var res2json = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var values;
34+
var v;
35+
var i;
36+
37+
values = [
38+
new Float64Results(),
39+
new Float64Results()
40+
];
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
v = res2json( values[ i%values.length ] );
45+
if ( typeof v !== 'object' ) {
46+
b.fail( 'should return an object' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isPlainObject( v ) ) {
51+
b.fail( 'should return an object' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( results )
3+
Serializes a one-sample Z-test results object as a JSON object.
4+
5+
Parameters
6+
----------
7+
results: Object
8+
One-sample Z-test results object.
9+
10+
Returns
11+
-------
12+
out: Object
13+
Serialized object.
14+
15+
Examples
16+
--------
17+
> var res = {
18+
... 'rejected': false,
19+
... 'alpha': 0.05,
20+
... 'pValue': 0.3364,
21+
... 'statistic': 11.7586,
22+
... 'nullValue': 0.0,
23+
... 'sd': 0.4563,
24+
... 'ci': new {{alias:@stdlib/array/float64}}( [ 9.9983, 11.4123 ] ),
25+
... 'alternative': 'two-sided',
26+
... 'method': 'One sample Z-test'
27+
... };
28+
> var o = {{alias}}( res )
29+
{...}
30+
31+
See Also
32+
--------
33+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Interface describing a results object.
23+
*/
24+
interface Results {
25+
/**
26+
* Boolean indicating whether the null hypothesis was rejected.
27+
*/
28+
rejected: boolean;
29+
30+
/**
31+
* Alternative hypothesis.
32+
*/
33+
alternative: string;
34+
35+
/**
36+
* Significance level.
37+
*/
38+
alpha: number;
39+
40+
/**
41+
* p-value.
42+
*/
43+
pValue: number;
44+
45+
/**
46+
* Test statistic.
47+
*/
48+
statistic: number;
49+
50+
/**
51+
* Confidence interval.
52+
*/
53+
ci: Float64Array | Float32Array;
54+
55+
/**
56+
* Value of the mean under the null hypothesis
57+
*/
58+
nullValue: number;
59+
60+
/**
61+
* Standard error of the mean.
62+
*/
63+
sd: number;
64+
65+
/**
66+
* Test method.
67+
*/
68+
method: string;
69+
}
70+
71+
/**
72+
* Serializes a one-sample Z-test results object as a JSON object.
73+
*
74+
* @param results - one-sample Z-test results object
75+
* @returns serialized object
76+
*
77+
* @example
78+
* var Float64Array = require( '@stdlib/array/float64' );
79+
*
80+
* var results = {
81+
* 'rejected': false,
82+
* 'alpha': 0.05,
83+
* 'pValue': 0.3364,
84+
* 'statistic': 11.7586,
85+
* 'nullValue': 0.0,
86+
* 'sd': 0.4563,
87+
* 'ci': new Float64Array( [ 9.9983, 11.4123 ] ),
88+
* 'alternative': 'two-sided',
89+
* 'method': 'One sample Z-test'
90+
* };
91+
*
92+
* var obj = toJSON( results );
93+
* // returns {...}
94+
*/
95+
declare function res2json( results: Results ): Results;
96+
97+
98+
// EXPORTS //
99+
100+
export = res2json;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
import res2json = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an object...
25+
{
26+
const res = {
27+
'rejected': true,
28+
'alpha': 0.05,
29+
'pValue': 0.3364,
30+
'statistic': 11.7586,
31+
'nullValue': 0.0,
32+
'sd': 0.4563,
33+
'ci': new Float64Array( [ 9.9983, 11.4123 ] ),
34+
'alternative': 'two-sided',
35+
'method': 'One sample Z-test'
36+
};
37+
res2json( res ); // $ExpectType Results
38+
}
39+
40+
// The compiler throws an error if not provided a results object...
41+
{
42+
res2json( 10 ); // $ExpectError
43+
res2json( true ); // $ExpectError
44+
res2json( false ); // $ExpectError
45+
res2json( null ); // $ExpectError
46+
res2json( undefined ); // $ExpectError
47+
res2json( [] ); // $ExpectError
48+
res2json( {} ); // $ExpectError
49+
res2json( ( x: number ): number => x ); // $ExpectError
50+
}

0 commit comments

Comments
 (0)