Skip to content

Commit 97109f7

Browse files
committed
feat!: refactor test to return class instance and be more performant
--- 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: na - 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: na - 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: na - task: lint_license_headers status: passed ---
1 parent d51a71a commit 97109f7

File tree

9 files changed

+371
-168
lines changed

9 files changed

+371
-168
lines changed

lib/node_modules/@stdlib/stats/fligner-test/README.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ limitations under the License.
3636
var flignerTest = require( '@stdlib/stats/fligner-test' );
3737
```
3838

39-
#### flignerTest( a\[,b,...,k]\[, opts] )
39+
#### flignerTest( a\[,b,...,k]\[, options] )
4040

4141
For input arrays `a`, `b`, ... holding numeric observations, this function calculates the Fligner-Killeen test, which tests the null hypothesis that the variances in all `k` groups are the same.
4242

@@ -46,7 +46,9 @@ var x = [ 2.9, 3.0, 2.5, 2.6, 3.2 ];
4646
var y = [ 3.8, 2.7, 4.0, 2.4 ];
4747
var z = [ 2.8, 3.4, 3.7, 2.2, 2.0 ];
4848

49-
var out = flignerTest( x, y, z );
49+
var res = flignerTest( x, y, z );
50+
51+
var o = res.toJSON();
5052
/* returns
5153
{
5254
'rejected': false,
@@ -106,23 +108,34 @@ var out = flignerTest( arr, {
106108
});
107109
```
108110

109-
The returned object comes with a `.print()` method which when invoked will print a formatted output of the results of the hypothesis test. `print` accepts a `digits` option that controls the number of decimal digits displayed for the outputs and a `decision` option, which when set to `false` will hide the test decision.
111+
The function returns a results `object` having the following properties:
112+
113+
- **alpha**: significance level.
114+
- **rejected**: `boolean` indicating the test decision.
115+
- **pValue**: test p-value.
116+
- **statistic**: test statistic.
117+
- **df**: degrees of freedom.
118+
- **method**: test name.
119+
- **toString**: serializes results as formatted test output.
120+
- **toJSON**: serializes results as a JSON object.
121+
122+
The returned object comes with a `.toString()` method which when invoked will print a formatted output of the results of the hypothesis test. `toString` accepts a `digits` option that controls the number of decimal digits displayed for the outputs and a `decision` option, which when set to `false` will hide the test decision.
110123

111124
```javascript
112125
var x = [ 2.9, 3.0, 2.5, 2.6, 3.2 ];
113126
var y = [ 3.8, 2.7, 4.0, 2.4 ];
114127
var z = [ 2.8, 3.4, 3.7, 2.2, 2.0 ];
115128

116129
var out = flignerTest( x, y, z );
117-
console.log( out.print() );
118-
/* =>
130+
var table = out.toString();
131+
/* returns
119132
Fligner-Killeen test of homogeneity of variances
120133
121-
Null hypothesis: The variances in all groups are the same.
134+
Null hypothesis: The variances in all groups are the same
122135
123136
pValue: 0.0739
124137
statistic: 5.2092
125-
df: 2
138+
degrees of freedom: 2
126139
127140
Test Decision: Fail to reject null in favor of alternative at 5% significance level
128141
*/
@@ -162,11 +175,11 @@ var table = out.print();
162175
/* returns
163176
Fligner-Killeen test of homogeneity of variances
164177
165-
Null hypothesis: The variances in all groups are the same.
178+
Null hypothesis: The variances in all groups are the same
166179
167180
pValue: 0.0739
168181
statistic: 5.2092
169-
df: 2
182+
degrees of freedom: 2
170183
171184
Test Decision: Fail to reject null in favor of alternative at 5% significance level
172185
*/

lib/node_modules/@stdlib/stats/fligner-test/docs/repl.txt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@
4040
out.df: Object
4141
Degrees of freedom.
4242

43-
out.print: Function
44-
Function to print formatted output.
43+
out.toString: Function
44+
Serializes results as formatted output.
45+
46+
out.toJSON: Function
47+
Serializes results as JSON.
4548

4649
Examples
4750
--------
@@ -50,7 +53,11 @@
5053
> var y = [ 3.8, 2.7, 4.0, 2.4 ];
5154
> var z = [ 2.8, 3.4, 3.7, 2.2, 2.0 ];
5255

53-
> var out = {{alias}}( x, y, z )
56+
> var out = {{alias}}( x, y, z );
57+
> var o = out.toJSON()
58+
{...}
59+
> out.toString()
60+
<string>
5461

5562
> var arr = [ 2.9, 3.0, 2.5, 2.6, 3.2,
5663
... 3.8, 2.7, 4.0, 2.4,
@@ -61,7 +68,11 @@
6168
... 'b', 'b', 'b', 'b',
6269
... 'c', 'c', 'c', 'c', 'c'
6370
... ];
64-
> out = {{alias}}( arr, { 'groups': groups } )
71+
> out = {{alias}}( arr, { 'groups': groups } );
72+
> o = out.toJSON()
73+
{...}
74+
> out.toString()
75+
<string>
6576

6677
See Also
6778
--------

lib/node_modules/@stdlib/stats/fligner-test/docs/types/index.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,14 @@ interface Results {
7272
df: number;
7373

7474
/**
75-
* Function to print formatted output.
75+
* Serializes results as formatted test output.
7676
*/
77-
print: Function;
77+
toString: Function; // FIXME: provide better type
78+
79+
/**
80+
* Serializes results as JSON.
81+
*/
82+
toJSON: Function; // FIXME: provide better type
7883
}
7984

8085
/**

lib/node_modules/@stdlib/stats/fligner-test/examples/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ console.log( 'Output object: ' );
3030
console.dir( out );
3131
console.log( '\n' );
3232

33-
var table = out.print();
33+
var table = out.toString();
3434
console.log( table );

lib/node_modules/@stdlib/stats/fligner-test/lib/main.js

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,20 @@
2222

2323
var isCollection = require( '@stdlib/assert/is-collection' );
2424
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
25-
var setReadOnly = require( '@stdlib/utils/define-read-only-property' );
2625
var objectKeys = require( '@stdlib/utils/keys' );
2726
var qnorm = require( '@stdlib/stats/base/dists/normal/quantile' );
2827
var chisqCDF = require( '@stdlib/stats/base/dists/chisquare/cdf' );
2928
var group = require( '@stdlib/utils/group' );
3029
var ranks = require( '@stdlib/stats/ranks' );
31-
var abs = require( '@stdlib/math/base/special/abs' );
30+
var abs = require( '@stdlib/math/strided/special/abs' );
3231
var pow = require( '@stdlib/math/base/special/pow' );
3332
var indexOf = require( '@stdlib/utils/index-of' );
3433
var format = require( '@stdlib/string/format' );
34+
var filled = require( '@stdlib/array/base/filled' );
35+
var Float64Array = require( '@stdlib/array/float64' );
3536
var median = require( './median.js' );
3637
var validate = require( './validate.js' );
37-
var print = require( './print.js' ); // eslint-disable-line stdlib/no-redeclare
38-
39-
40-
// FUNCTIONS //
41-
42-
/**
43-
* Returns an array of a chosen length filled with the supplied value.
44-
*
45-
* @private
46-
* @param {*} val - value to repeat
47-
* @param {NonNegativeInteger} len - array length
48-
* @returns {Array} filled array
49-
*/
50-
function repeat( val, len ) {
51-
var out = new Array( len );
52-
var i;
53-
54-
for ( i = 0; i < len; i++ ) {
55-
out[ i ] = val;
56-
}
57-
return out;
58-
}
38+
var Results = require( './results.js' );
5939

6040

6141
// MAIN //
@@ -102,7 +82,6 @@ function fligner() {
10282
var stat;
10383
var err;
10484
var loc;
105-
var out;
10685
var df;
10786
var M2;
10887
var a;
@@ -137,7 +116,7 @@ function fligner() {
137116
groups = [];
138117
for ( i = 0; i < ngroups; i++ ) {
139118
args.push( arguments[ i ] );
140-
groups = groups.concat( repeat( i, arguments[ i ].length ) );
119+
groups = groups.concat( filled( i, arguments[ i ].length ) );
141120
}
142121
}
143122
if ( opts.alpha === void 0 ) {
@@ -163,15 +142,13 @@ function fligner() {
163142
x = x.concat( args[ i ] );
164143
}
165144
n = x.length;
166-
xabs = new Array( n );
167-
for ( i = 0; i < n; i++ ) {
168-
xabs[ i ] = abs( x[ i ] );
169-
}
145+
xabs = new Float64Array( n );
146+
abs( n, 'float64', x, 1, 'float64', xabs, 1 );
170147
scores = ranks( xabs );
171-
a = new Array( n );
148+
a = new Float64Array( n );
172149
mean = 0.0;
173150
M2 = 0.0;
174-
sums = repeat( 0.0, ngroups );
151+
sums = filled( 0.0, ngroups );
175152
for ( i = 0; i < n; i++ ) {
176153
a[ i ] = qnorm( ( 1.0 + ( scores[ i ]/(n+1) ) ) / 2.0, 0.0, 1.0 );
177154
sums[ ( levels ) ? indexOf( levels, groups[i] ) : groups[i] ] += a[ i ];
@@ -188,15 +165,7 @@ function fligner() {
188165
df = ngroups - 1;
189166
pval = 1.0 - chisqCDF( stat, df );
190167

191-
out = {};
192-
setReadOnly( out, 'rejected', pval <= alpha );
193-
setReadOnly( out, 'alpha', alpha );
194-
setReadOnly( out, 'pValue', pval );
195-
setReadOnly( out, 'statistic', stat );
196-
setReadOnly( out, 'df', df );
197-
setReadOnly( out, 'method', 'Fligner-Killeen test of homogeneity of variances' );
198-
setReadOnly( out, 'print', print );
199-
return out;
168+
return new Results( pval, opts.alpha, stat, df );
200169
}
201170

202171

lib/node_modules/@stdlib/stats/fligner-test/lib/print.js

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)