Skip to content

Commit e382604

Browse files
committed
Add Typescript definition
1 parent acf656f commit e382604

File tree

3 files changed

+324
-0
lines changed

3 files changed

+324
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { NumericArray } from '@stdlib/types/array';
24+
25+
/**
26+
* Interface defining function options.
27+
*/
28+
interface Options {
29+
/**
30+
* Significance level (default: 0.05).
31+
*/
32+
alpha?: number;
33+
34+
/**
35+
* Alternative hypothesis (`two-sided`, `less`, or `greater`; default: 'two-sided').
36+
*/
37+
alternative?: 'two-sided' | 'less' | 'greater';
38+
39+
/**
40+
* Method governing how zero-differences are handled (`pratt`, `wilcox`, or `zsplit`; default: 'wilcox').
41+
*/
42+
zeroMethod?: 'pratt' | 'wilcox' | 'zsplit';
43+
44+
/**
45+
* Continuity correction adjusting the Wilcoxon rank statistic by 0.5 towards the mean (default: true).
46+
*/
47+
correction?: boolean;
48+
49+
/**
50+
* Whether to force using the exact distribution instead of a normal approximation when there are more than fifty data points (default: false).
51+
*/
52+
exact?: boolean;
53+
54+
/**
55+
* Location parameter under H0 (default: 0).
56+
*/
57+
mu?: number;
58+
}
59+
60+
/**
61+
* Test result.
62+
*/
63+
interface Output {
64+
/**
65+
* Used significance level.
66+
*/
67+
alpha: number;
68+
69+
/**
70+
* Test decision.
71+
*/
72+
rejected: boolean;
73+
74+
/**
75+
* p-value of the test.
76+
*/
77+
pValue: number;
78+
79+
/**
80+
* Value of test statistic.
81+
*/
82+
statistic: number;
83+
84+
/**
85+
* Assumed mean under H0 (or difference in means when `y` is supplied).
86+
*/
87+
nullValue: number;
88+
89+
/**
90+
* Alternative hypothesis (`two-sided`, `less` or `greater`).
91+
*/
92+
alternative: string;
93+
94+
/**
95+
* Name of test.
96+
*/
97+
method: string;
98+
99+
/**
100+
* Function to print formatted output.
101+
*/
102+
print: Function;
103+
}
104+
105+
/**
106+
* Computes a Wilcoxon signed rank test.
107+
*
108+
* @param x - data array
109+
* @param options - function options
110+
* @param options.alpha - significance level (default: 0.05)
111+
* @param options.alternative - alternative hypothesis (`two-sided`, `less`, or `greater`; default: 'two-sided')
112+
* @param options.zeroMethod - method governing how zero-differences are handled (`pratt`, `wilcox`, or `zsplit`; default: 'wilcox')
113+
* @param options.correction - continuity correction adjusting the Wilcoxon rank statistic by 0.5 towards the mean (default: true)
114+
* @param options.exact - whether to force using the exact distribution instead of a normal approximation when there are more than fifty data points (default: false)
115+
* @param options.mu - location parameter under H0 (default: 0)
116+
* @throws must provide valid options
117+
* @returns test result object
118+
*
119+
* @example
120+
* var x = [ 6, 8, 14, 16, 23, 24, 28, 29, 41, -48, 49, 56, 60, -67, 75 ];
121+
* var out = wilcoxon( x, {
122+
* 'mu': 2
123+
* });
124+
*
125+
* @example
126+
* var x = [ 6, 8, 14, 16, 23, 24, 28, 29, 41, -48, 49, 56, 60, -67, 75 ];
127+
* var out = wilcoxon( x, {
128+
* 'alternative': 'greater'
129+
* });
130+
*/
131+
declare function wilcoxon( x: NumericArray, options?: Options ): Output; // tslint-disable-line max-line-length
132+
133+
/**
134+
* Computes a Wilcoxon signed rank test.
135+
*
136+
* @param x - data array
137+
* @param y - optional paired data array
138+
* @param options - function options
139+
* @param options.alpha - significance level (default: 0.05)
140+
* @param options.alternative - alternative hypothesis (`two-sided`, `less`, or `greater`; default: 'two-sided')
141+
* @param options.zeroMethod - method governing how zero-differences are handled (`pratt`, `wilcox`, or `zsplit`; default: 'wilcox')
142+
* @param options.correction - continuity correction adjusting the Wilcoxon rank statistic by 0.5 towards the mean (default: true)
143+
* @param options.exact - whether to force using the exact distribution instead of a normal approximation when there are more than fifty data points (default: false)
144+
* @param options.mu - location parameter under H0 (default: 0)
145+
* @throws must provide valid options
146+
* @returns test result object
147+
*
148+
* @example
149+
* var x = [ 1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30 ];
150+
* var y = [ 0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29 ];
151+
* var out = wilcoxon( x, y );
152+
*/
153+
declare function wilcoxon( x: NumericArray, y: NumericArray, options?: Options ): Output; // tslint-disable-line max-line-length
154+
155+
156+
// EXPORTS //
157+
158+
export = wilcoxon;
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 wilcoxon = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an object with ordered x-values and fitted values...
25+
{
26+
const arr = [ 6, 8, 14, 16, 23, 24, 28, 29, 41, -48, 49, 56, 60, -67, 75 ];
27+
wilcoxon( arr ); // $ExpectType Output
28+
wilcoxon( arr, { 'mu': 2 } ); // $ExpectType Output
29+
const x = [ 1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30 ];
30+
const y = [ 0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29 ];
31+
wilcoxon( x, y, { 'correction': false } ); // $ExpectType Output
32+
}
33+
34+
// The function does not compile if provided a first argument that is not an array of numbers...
35+
{
36+
const y = [ 0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29 ];
37+
wilcoxon( 'abc' ); // $ExpectError
38+
wilcoxon( true ); // $ExpectError
39+
wilcoxon( false ); // $ExpectError
40+
wilcoxon( null ); // $ExpectError
41+
wilcoxon( undefined ); // $ExpectError
42+
wilcoxon( 5 ); // $ExpectError
43+
wilcoxon( {} ); // $ExpectError
44+
wilcoxon( ( x: number ): number => x ); // $ExpectError
45+
46+
wilcoxon( 'abc', y ); // $ExpectError
47+
wilcoxon( true, y ); // $ExpectError
48+
wilcoxon( false, y ); // $ExpectError
49+
wilcoxon( null, y ); // $ExpectError
50+
wilcoxon( undefined, y ); // $ExpectError
51+
wilcoxon( 5, y ); // $ExpectError
52+
wilcoxon( {}, y ); // $ExpectError
53+
wilcoxon( ( x: number ): number => x, y ); // $ExpectError
54+
}
55+
56+
// The function does not compile if provided a second argument that is not an array of numbers or options object...
57+
{
58+
const x = [ 6, 8, 14, 16, 23, 24, 28, 29, 41, -48, 49, 56, 60, -67, 75 ];
59+
wilcoxon( x, 'abc' ); // $ExpectError
60+
wilcoxon( x, true ); // $ExpectError
61+
wilcoxon( x, false ); // $ExpectError
62+
wilcoxon( x, null ); // $ExpectError
63+
wilcoxon( x, 5 ); // $ExpectError
64+
wilcoxon( x, ( x: number ): number => x ); // $ExpectError
65+
}
66+
67+
// The compiler throws an error if the function is provided a third argument which is not an options object...
68+
{
69+
const x = [ 1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30 ];
70+
const y = [ 0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29 ];
71+
wilcoxon( x, y, true ); // $ExpectError
72+
wilcoxon( x, y, false ); // $ExpectError
73+
wilcoxon( x, y, null ); // $ExpectError
74+
wilcoxon( x, y, 5 ); // $ExpectError
75+
wilcoxon( x, y, 'abc' ); // $ExpectError
76+
wilcoxon( x, y, ( x: number ): number => x ); // $ExpectError
77+
}
78+
79+
// The compiler throws an error if the function is provided an `alpha` option which is not a number...
80+
{
81+
const x = [ 1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30 ];
82+
const y = [ 0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29 ];
83+
wilcoxon( x, y, { 'alpha': 'abc' } ); // $ExpectError
84+
wilcoxon( x, y, { 'alpha': true } ); // $ExpectError
85+
wilcoxon( x, y, { 'alpha': false } ); // $ExpectError
86+
wilcoxon( x, y, { 'alpha': null } ); // $ExpectError
87+
wilcoxon( x, y, { 'alpha': [] } ); // $ExpectError
88+
wilcoxon( x, y, { 'alpha': {} } ); // $ExpectError
89+
wilcoxon( x, y, { 'alpha': ( x: number ): number => x } ); // $ExpectError
90+
}
91+
92+
// The compiler throws an error if the function is provided a `alternative` option which is not a recognized alternative...
93+
{
94+
const x = [ 1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30 ];
95+
const y = [ 0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29 ];
96+
wilcoxon( x, y, { 'alternative': 'abc' } ); // $ExpectError
97+
wilcoxon( x, y, { 'alternative': 123 } ); // $ExpectError
98+
wilcoxon( x, y, { 'alternative': true } ); // $ExpectError
99+
wilcoxon( x, y, { 'alternative': false } ); // $ExpectError
100+
wilcoxon( x, y, { 'alternative': null } ); // $ExpectError
101+
wilcoxon( x, y, { 'alternative': [] } ); // $ExpectError
102+
wilcoxon( x, y, { 'alternative': {} } ); // $ExpectError
103+
wilcoxon( x, y, { 'alternative': ( x: number ): number => x } ); // $ExpectError
104+
}
105+
106+
// The compiler throws an error if the function is provided a `zeroMethod` option which is not a recognized method...
107+
{
108+
const x = [ 1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30 ];
109+
const y = [ 0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29 ];
110+
wilcoxon( x, y, { 'zeroMethod': 'abc' } ); // $ExpectError
111+
wilcoxon( x, y, { 'zeroMethod': 123 } ); // $ExpectError
112+
wilcoxon( x, y, { 'zeroMethod': true } ); // $ExpectError
113+
wilcoxon( x, y, { 'zeroMethod': false } ); // $ExpectError
114+
wilcoxon( x, y, { 'zeroMethod': null } ); // $ExpectError
115+
wilcoxon( x, y, { 'zeroMethod': [] } ); // $ExpectError
116+
wilcoxon( x, y, { 'zeroMethod': {} } ); // $ExpectError
117+
wilcoxon( x, y, { 'zeroMethod': ( x: number ): number => x } ); // $ExpectError
118+
}
119+
120+
// The compiler throws an error if the function is provided a `correction` option which is not a boolean...
121+
{
122+
const x = [ 1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30 ];
123+
const y = [ 0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29 ];
124+
wilcoxon( x, y, { 'correction': 'abc' } ); // $ExpectError
125+
wilcoxon( x, y, { 'correction': '123' } ); // $ExpectError
126+
wilcoxon( x, y, { 'correction': 123 } ); // $ExpectError
127+
wilcoxon( x, y, { 'correction': null } ); // $ExpectError
128+
wilcoxon( x, y, { 'correction': [] } ); // $ExpectError
129+
wilcoxon( x, y, { 'correction': {} } ); // $ExpectError
130+
wilcoxon( x, y, { 'correction': ( x: number ): number => x } ); // $ExpectError
131+
}
132+
133+
// The compiler throws an error if the function is provided an `exact` option which is not a boolean...
134+
{
135+
const x = [ 1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30 ];
136+
const y = [ 0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29 ];
137+
wilcoxon( x, y, { 'exact': 'abc' } ); // $ExpectError
138+
wilcoxon( x, y, { 'exact': '123' } ); // $ExpectError
139+
wilcoxon( x, y, { 'exact': 123 } ); // $ExpectError
140+
wilcoxon( x, y, { 'exact': null } ); // $ExpectError
141+
wilcoxon( x, y, { 'exact': [] } ); // $ExpectError
142+
wilcoxon( x, y, { 'exact': {} } ); // $ExpectError
143+
wilcoxon( x, y, { 'exact': ( x: number ): number => x } ); // $ExpectError
144+
}
145+
146+
// The compiler throws an error if the function is provided a `mu` option which is not a number...
147+
{
148+
const x = [ 1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30 ];
149+
const y = [ 0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29 ];
150+
wilcoxon( x, y, { 'mu': 'abc' } ); // $ExpectError
151+
wilcoxon( x, y, { 'mu': true } ); // $ExpectError
152+
wilcoxon( x, y, { 'mu': false } ); // $ExpectError
153+
wilcoxon( x, y, { 'mu': null } ); // $ExpectError
154+
wilcoxon( x, y, { 'mu': [] } ); // $ExpectError
155+
wilcoxon( x, y, { 'mu': {} } ); // $ExpectError
156+
wilcoxon( x, y, { 'mu': ( x: number ): number => x } ); // $ExpectError
157+
}
158+
159+
// The function does not compile if provided an invalid number of arguments...
160+
{
161+
const x = [ 1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30 ];
162+
const y = [ 0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29 ];
163+
wilcoxon(); // $ExpectError
164+
wilcoxon( x, y, {}, {} ); // $ExpectError
165+
}

lib/node_modules/@stdlib/stats/wilcoxon/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"lib": "./lib",
2121
"test": "./test"
2222
},
23+
"types": "./docs/types",
2324
"scripts": {},
2425
"homepage": "https://github.com/stdlib-js/stdlib",
2526
"repository": {

0 commit comments

Comments
 (0)