Skip to content

Commit c203ba7

Browse files
committed
Add Typescript definition
1 parent 88bcbd1 commit c203ba7

3 files changed

Lines changed: 364 additions & 0 deletions

File tree

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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+
* Mean under `H0` (default: 0).
41+
*/
42+
mu?: number;
43+
}
44+
45+
/**
46+
* Test result.
47+
*/
48+
interface Output {
49+
/**
50+
* Used significance level.
51+
*/
52+
alpha: number;
53+
54+
/**
55+
* Test decision.
56+
*/
57+
rejected: boolean;
58+
59+
/**
60+
* p-value of the test.
61+
*/
62+
pValue: number;
63+
64+
/**
65+
* Value of test statistic.
66+
*/
67+
statistic: number;
68+
69+
/**
70+
* 1-alpha confidence interval for the mean.
71+
*/
72+
ci: Array<number>;
73+
74+
/**
75+
* Assumed mean under H0 (or difference in means when `y` is supplied).
76+
*/
77+
nullValue: number;
78+
79+
/**
80+
* Alternative hypothesis (`two-sided`, `less` or `greater`).
81+
*/
82+
alternative: string;
83+
84+
/**
85+
* Degrees of freedom.
86+
*/
87+
df: number;
88+
89+
/**
90+
* Sample mean of `x` or `x - y`, respectively.
91+
*/
92+
mean: number;
93+
94+
/**
95+
* Standard error of the mean.
96+
*/
97+
sd: number;
98+
99+
/**
100+
* Name of test.
101+
*/
102+
method: string;
103+
104+
/**
105+
* Function to print formatted output.
106+
*/
107+
print: Function;
108+
}
109+
110+
/**
111+
* Interface for Student's t test.
112+
*/
113+
interface TTest {
114+
/**
115+
* Computes a paired Student's t test.
116+
*
117+
* @param x - input array
118+
* @param y - optional paired array
119+
* @param options - function options
120+
* @param options.alpha - significance level (default: 0.05)
121+
* @param options.alternative - alternative hypothesis (`two-sided`, `less`, or `greater`; default: 'two-sided')
122+
* @param options.mu - mean under `H0` (default: 0)
123+
* @throws first argument must have at least two elements
124+
* @throws paired array must have the same length as the first argument
125+
* @throws must provide valid options
126+
* @returns test results
127+
*
128+
* @example
129+
* var x = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
130+
* var y = [ 5.0, 5.0, 5.5, 7.0, 5.8 ];
131+
* var opts = {
132+
* 'alpha': 0.1
133+
* };
134+
* var out = ttest( x, opts );
135+
* // returns {...}
136+
*/
137+
( x: NumericArray, y: NumericArray, options?: Options ): Output;
138+
139+
/**
140+
* Computes a one-sample Student's t test.
141+
*
142+
* @param x - input array
143+
* @param options - function options
144+
* @param options.alpha - significance level (default: 0.05)
145+
* @param options.alternative - alternative hypothesis (`two-sided`, `less`, or `greater`; default: 'two-sided')
146+
* @param options.mu - mean under `H0` (default: 0)
147+
* @throws first argument must have at least two elements
148+
* @throws must provide valid options
149+
* @returns test results
150+
*
151+
* @example
152+
* var x = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
153+
* var opts = {
154+
* 'mu': 5.0
155+
* };
156+
* var out = ttest( x, opts );
157+
* // returns {...}
158+
*
159+
* @example
160+
* var x = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
161+
* var opts = {
162+
* 'alternative': 'greater'
163+
* };
164+
* var out = ttest( x, opts );
165+
* // returns {...}
166+
*/
167+
( x: NumericArray, options?: Options ): Output;
168+
}
169+
170+
171+
/**
172+
* Computes a one-sample or paired Student's t test.
173+
*
174+
* @param x - input array
175+
* @param y - optional paired array
176+
* @param options - function options
177+
* @param options.alpha - significance level (default: 0.05)
178+
* @param options.alternative - alternative hypothesis (`two-sided`, `less`, or `greater`; default: 'two-sided')
179+
* @param options.mu - mean under `H0` (default: 0)
180+
* @throws first argument must have at least two elements
181+
* @throws paired array must have the same length as the first argument
182+
* @throws second argument must be either a numeric array or an options object
183+
* @throws must provide valid options
184+
* @returns test results
185+
*
186+
* @example
187+
* var x = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
188+
* var opts = {
189+
* 'mu': 5.0
190+
* };
191+
* var out = ttest( x, opts );
192+
* // returns {...}
193+
*
194+
* @example
195+
* var x = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
196+
* var y = [ 5.0, 5.0, 5.5, 7.0, 5.8 ];
197+
* var opts = {
198+
* 'alpha': 0.1
199+
* };
200+
* var out = ttest( x, opts );
201+
* // returns {...}
202+
*/
203+
declare var ttest: TTest;
204+
205+
206+
// EXPORTS //
207+
208+
export = ttest;
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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 ttest = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a test result object...
25+
{
26+
const x = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
27+
const y = [ 5.0, 5.0, 5.5, 7.0, 5.8 ];
28+
ttest( x ); // $ExpectType Output
29+
ttest( x, { 'alpha': 0.1 } ); // $ExpectType Output
30+
ttest( x, y ); // $ExpectType Output
31+
ttest( x, y, { 'alpha': 0.1 } ); // $ExpectType Output
32+
}
33+
34+
// The function does not compile if provided a first argument that is not a numeric array...
35+
{
36+
const y = [ 5.0, 5.0, 5.5, 7.0, 5.8 ];
37+
ttest( 'abc' ); // $ExpectError
38+
ttest( 123 ); // $ExpectError
39+
ttest( true ); // $ExpectError
40+
ttest( false ); // $ExpectError
41+
ttest( null ); // $ExpectError
42+
ttest( undefined ); // $ExpectError
43+
ttest( {} ); // $ExpectError
44+
ttest( ( x: number ): number => x ); // $ExpectError
45+
46+
ttest( 'abc', y ); // $ExpectError
47+
ttest( 123, y ); // $ExpectError
48+
ttest( true, y ); // $ExpectError
49+
ttest( false, y ); // $ExpectError
50+
ttest( null, y ); // $ExpectError
51+
ttest( undefined, y ); // $ExpectError
52+
ttest( {}, y ); // $ExpectError
53+
ttest( ( x: number ): number => x, y ); // $ExpectError
54+
}
55+
56+
// The function does not compile if provided a second argument that is neither a numeric array nor an options object...
57+
{
58+
const x = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
59+
ttest( x, 'abc' ); // $ExpectError
60+
ttest( x, 123 ); // $ExpectError
61+
ttest( x, true ); // $ExpectError
62+
ttest( x, false ); // $ExpectError
63+
ttest( x, null ); // $ExpectError
64+
ttest( 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 = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
70+
const y = [ 5.0, 5.0, 5.5, 7.0, 5.8 ];
71+
ttest( x, y, true ); // $ExpectError
72+
ttest( x, y, false ); // $ExpectError
73+
ttest( x, y, null ); // $ExpectError
74+
ttest( x, y, 5 ); // $ExpectError
75+
ttest( x, y, 'abc' ); // $ExpectError
76+
ttest( x, y, [] ); // $ExpectError
77+
ttest( x, y, ( x: number ): number => x ); // $ExpectError
78+
}
79+
80+
// The compiler throws an error if the function is provided an `alpha` option which is not a number...
81+
{
82+
const x = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
83+
const y = [ 5.0, 5.0, 5.5, 7.0, 5.8 ];
84+
ttest( x, { 'alpha': 'abc' } ); // $ExpectError
85+
ttest( x, { 'alpha': '123' } ); // $ExpectError
86+
ttest( x, { 'alpha': true } ); // $ExpectError
87+
ttest( x, { 'alpha': false } ); // $ExpectError
88+
ttest( x, { 'alpha': null } ); // $ExpectError
89+
ttest( x, { 'alpha': [] } ); // $ExpectError
90+
ttest( x, { 'alpha': {} } ); // $ExpectError
91+
ttest( x, { 'alpha': ( x: number ): number => x } ); // $ExpectError
92+
93+
ttest( x, y, { 'alpha': 'abc' } ); // $ExpectError
94+
ttest( x, y, { 'alpha': '123' } ); // $ExpectError
95+
ttest( x, y, { 'alpha': true } ); // $ExpectError
96+
ttest( x, y, { 'alpha': false } ); // $ExpectError
97+
ttest( x, y, { 'alpha': null } ); // $ExpectError
98+
ttest( x, y, { 'alpha': [] } ); // $ExpectError
99+
ttest( x, y, { 'alpha': {} } ); // $ExpectError
100+
ttest( x, y, { 'alpha': ( x: number ): number => x } ); // $ExpectError
101+
}
102+
103+
// The compiler throws an error if the function is provided an `alternative` option which is not a recognized alternative...
104+
{
105+
const x = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
106+
const y = [ 5.0, 5.0, 5.5, 7.0, 5.8 ];
107+
ttest( x, { 'alternative': 'abc' } ); // $ExpectError
108+
ttest( x, { 'alternative': 123 } ); // $ExpectError
109+
ttest( x, { 'alternative': true } ); // $ExpectError
110+
ttest( x, { 'alternative': false } ); // $ExpectError
111+
ttest( x, { 'alternative': null } ); // $ExpectError
112+
ttest( x, { 'alternative': [] } ); // $ExpectError
113+
ttest( x, { 'alternative': {} } ); // $ExpectError
114+
ttest( x, { 'alternative': ( x: number ): number => x } ); // $ExpectError
115+
116+
ttest( x, y, { 'alternative': 'abc' } ); // $ExpectError
117+
ttest( x, y, { 'alternative': 123 } ); // $ExpectError
118+
ttest( x, y, { 'alternative': true } ); // $ExpectError
119+
ttest( x, y, { 'alternative': false } ); // $ExpectError
120+
ttest( x, y, { 'alternative': null } ); // $ExpectError
121+
ttest( x, y, { 'alternative': [] } ); // $ExpectError
122+
ttest( x, y, { 'alternative': {} } ); // $ExpectError
123+
ttest( x, y, { 'alternative': ( x: number ): number => x } ); // $ExpectError
124+
}
125+
126+
// The compiler throws an error if the function is provided a `mu` option which is not a number...
127+
{
128+
const x = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
129+
const y = [ 5.0, 5.0, 5.5, 7.0, 5.8 ];
130+
ttest( x, { 'mu': 'abc' } ); // $ExpectError
131+
ttest( x, { 'mu': '123' } ); // $ExpectError
132+
ttest( x, { 'mu': true } ); // $ExpectError
133+
ttest( x, { 'mu': false } ); // $ExpectError
134+
ttest( x, { 'mu': null } ); // $ExpectError
135+
ttest( x, { 'mu': [] } ); // $ExpectError
136+
ttest( x, { 'mu': {} } ); // $ExpectError
137+
ttest( x, { 'mu': ( x: number ): number => x } ); // $ExpectError
138+
139+
ttest( x, y, { 'mu': 'abc' } ); // $ExpectError
140+
ttest( x, y, { 'mu': '123' } ); // $ExpectError
141+
ttest( x, y, { 'mu': true } ); // $ExpectError
142+
ttest( x, y, { 'mu': false } ); // $ExpectError
143+
ttest( x, y, { 'mu': null } ); // $ExpectError
144+
ttest( x, y, { 'mu': [] } ); // $ExpectError
145+
ttest( x, y, { 'mu': {} } ); // $ExpectError
146+
ttest( x, y, { 'mu': ( x: number ): number => x } ); // $ExpectError
147+
}
148+
149+
// The function does not compile if provided an invalid number of arguments...
150+
{
151+
const x = [ 4.0, 4.0, 6.0, 6.0, 5.0 ];
152+
const y = [ 5.0, 5.0, 5.5, 7.0, 5.8 ];
153+
ttest(); // $ExpectError
154+
ttest( x, y, {}, {} ); // $ExpectError
155+
}

lib/node_modules/@stdlib/stats/ttest/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)