Skip to content

Commit c5c7c08

Browse files
committed
feat: add stats/base/ztest/one-sample/results/to-string
--- 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 cbf51ef commit c5c7c08

File tree

10 files changed

+1235
-0
lines changed

10 files changed

+1235
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
# res2str
22+
23+
> Serialize a one-sample Z-test results object as a formatted string.
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 res2str = require( '@stdlib/stats/base/ztest/one-sample/results/to-string' );
41+
```
42+
43+
#### res2str( results\[, options] )
44+
45+
Serializes a one-sample Z-test results object as a formatted string.
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 s = res2str( results );
55+
// returns <string>
56+
```
57+
58+
The function supports the following parameters:
59+
60+
- **results**: one-sample Z-test results object.
61+
- **options**: function options.
62+
63+
The function supports the following options:
64+
65+
- **digits**: number of digits to display after decimal points. Default: `4`.
66+
- **decision**: boolean indicating whether to show the test decision. Default: `true`.
67+
68+
</section>
69+
70+
<!-- /.usage -->
71+
72+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
73+
74+
<section class="notes">
75+
76+
## Notes
77+
78+
- Example output:
79+
80+
```text
81+
82+
One-sample Z-test
83+
84+
Alternative hypothesis: True mean is less than 1.0
85+
86+
pValue: 0.0406
87+
statistic: 9.9901
88+
95% confidence interval: [9.7821, 10.4451]
89+
90+
Test Decision: Reject null in favor of alternative at 5% significance level
91+
92+
```
93+
94+
</section>
95+
96+
<!-- /.notes -->
97+
98+
<!-- Package usage examples. -->
99+
100+
<section class="examples">
101+
102+
## Examples
103+
104+
<!-- eslint no-undef: "error" -->
105+
106+
```javascript
107+
var Float64Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
108+
var resolveEnum = require( '@stdlib/stats/base/ztest/alternative-resolve-enum' );
109+
var Float64Array = require( '@stdlib/array/float64' );
110+
var res2str = require( '@stdlib/stats/base/ztest/one-sample/results/to-string' );
111+
112+
var results = new Float64Results();
113+
results.rejected = true;
114+
results.alpha = 0.05;
115+
results.pValue = 0.3364;
116+
results.statistic = 11.7586;
117+
results.nullValue = 0.0;
118+
results.sd = 0.4563;
119+
results.ci = new Float64Array( [ 9.9983, 11.4123 ] );
120+
results.alternative = resolveEnum( 'two-sided' );
121+
122+
var s = res2str( results );
123+
console.log( s );
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+
</section>
151+
152+
<!-- /.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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var res2str = 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 = res2str( values[ i%values.length ] );
45+
if ( typeof v !== 'string' ) {
46+
b.fail( 'should return a string' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isString( v ) ) {
51+
b.fail( 'should return a string' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
{{alias}}( results[, options] )
3+
Serializes a one-sample Z-test results object as a formatted string.
4+
5+
Parameters
6+
----------
7+
results: Object
8+
One-sample Z-test results object.
9+
10+
options: Object (optional)
11+
Function options.
12+
13+
options.digits: number (optional)
14+
Number of digits to display after decimal points. Default: 4.
15+
16+
options.decision: boolean (optional)
17+
Boolean indicating whether to show the test decision. Default: true.
18+
19+
Returns
20+
-------
21+
out: string
22+
Serialized results.
23+
24+
Examples
25+
--------
26+
> var res = {
27+
... 'rejected': false,
28+
... 'alpha': 0.05,
29+
... 'pValue': 0.3364,
30+
... 'statistic': 11.7586,
31+
... 'nullValue': 0.0,
32+
... 'sd': 0.4563,
33+
... 'ci': new {{alias:@stdlib/array/float64}}( [ 9.9983, 11.4123 ] ),
34+
... 'alternative': 'two-sided',
35+
... 'method': 'One-sample Z-test'
36+
... };
37+
> var s = {{alias}}( res )
38+
<string>
39+
40+
See Also
41+
--------
42+
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
* Interface describing function options.
73+
*/
74+
interface Options {
75+
/**
76+
* Number of digits to display after decimal points. Default: 4.
77+
*/
78+
digits?: number;
79+
80+
/**
81+
* Boolean indicating whether to show the test decision.
82+
*/
83+
decision?: boolean;
84+
}
85+
86+
/**
87+
* Serializes a one-sample Z-test results object as a formatted string.
88+
*
89+
* ## Notes
90+
*
91+
* - Example output:
92+
*
93+
* ```text
94+
*
95+
* One-sample Z-test
96+
*
97+
* Alternative hypothesis: True mean is less than 1.0
98+
*
99+
* pValue: 0.0406
100+
* statistic: 9.9901
101+
* 95% confidence interval: [9.7821, 10.4451]
102+
*
103+
* Test Decision: Reject null in favor of alternative at 5% significance level
104+
*
105+
* ```
106+
*
107+
* @param results - one-sample Z-test results object
108+
* @param options - options object
109+
* @param options.digits - number of digits to display after decimal points
110+
* @param options.decision - boolean indicating whether to show the test decision
111+
* @returns serialized results
112+
*
113+
* @example
114+
* var Float64Array = require( '@stdlib/array/float64' );
115+
*
116+
* var results = {
117+
* 'rejected': false,
118+
* 'alpha': 0.05,
119+
* 'pValue': 0.3364,
120+
* 'statistic': 11.7586,
121+
* 'nullValue': 0.0,
122+
* 'sd': 0.4563,
123+
* 'ci': new Float64Array( [ 9.9983, 11.4123 ] ),
124+
* 'alternative': 'two-sided',
125+
* 'method': 'One-sample Z-test'
126+
* };
127+
*
128+
* var str = res2str( results );
129+
* // returns <string>
130+
*/
131+
declare function res2str( results: Results, options?: Options ): string;
132+
133+
134+
// EXPORTS //
135+
136+
export = res2str;

0 commit comments

Comments
 (0)