Skip to content

Commit aadee57

Browse files
committed
Add Typescript definition
1 parent d679e15 commit aadee57

File tree

3 files changed

+223
-0
lines changed

3 files changed

+223
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
* Smoother span (proportion of points which influence smoothing at each value; default: 2/3).
31+
*/
32+
f?: number;
33+
34+
/**
35+
* Number of iterations in the robust fit (fewer iterations translates to faster function execution; default: 3).
36+
*/
37+
nsteps?: number;
38+
39+
/**
40+
* Nonnegative parameter which may be used to reduce the number of computations.
41+
*/
42+
delta?: number;
43+
44+
/**
45+
* Boolean indicating if the input array `x` is already in sorted order (default: false).
46+
*/
47+
sorted?: boolean;
48+
}
49+
50+
/**
51+
* LOWESS output.
52+
*/
53+
interface Output {
54+
/**
55+
* Ordered x-values.
56+
*/
57+
x: Array<number>;
58+
59+
/**
60+
* Fitted values.
61+
*/
62+
y: Array<number>;
63+
}
64+
65+
66+
/**
67+
* Locally-weighted polynomial regression via the LOWESS algorithm.
68+
*
69+
* ## References
70+
*
71+
* - Cleveland, William S. 1979. "Robust Locally and Smoothing Weighted Regression Scatterplots." _Journal of the American Statistical Association_ 74 (368): 829–36. doi:[10.1080/01621459.1979.10481038](https://doi.org/10.1080/01621459.1979.10481038).
72+
* - Cleveland, William S. 1981. "Lowess: A program for smoothing scatterplots by robust locally weighted regression." _American Statistician_ 35 (1): 54–55. doi:[10.2307/2683591](https://doi.org/10.2307/2683591).
73+
*
74+
* @param x - ordered x-axis values (abscissa values)
75+
* @param y - corresponding y-axis values (ordinate values)
76+
* @param options - function options
77+
* @param options.f - smoother span (proportion of points which influence smoothing at each value)
78+
* @param options.nsteps - number of iterations in the robust fit (fewer iterations translates to faster function execution)
79+
* @param options.delta - nonnegative parameter which may be used to reduce the number of computations
80+
* @param options.sorted - boolean indicating if the input array `x` is already in sorted order
81+
* @throws arguments `x` and `y` must have the same length
82+
* @returns ordered x-values and fitted values
83+
*/
84+
declare function lowess( x: NumericArray, y: NumericArray, options?: Options ): Output; // tslint-disable-line max-line-length
85+
86+
87+
// EXPORTS //
88+
89+
export = lowess;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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 lowess = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an object with ordered x-values and fitted values...
25+
{
26+
const x = [ 1.0, 2.0, 3.0 ];
27+
const y = [ 2.0, 3.5, 3.9 ];
28+
lowess( x, y ); // $ExpectType Output
29+
lowess( x, y, { 'nsteps': 3 } ); // $ExpectType Output
30+
lowess( x, y, { 'f': 2 / 3 } ); // $ExpectType Output
31+
lowess( x, y, { 'sorted': true } ); // $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 = [ 2.0, 3.5, 3.9 ];
37+
lowess( 'abc', y ); // $ExpectError
38+
lowess( true, y ); // $ExpectError
39+
lowess( false, y ); // $ExpectError
40+
lowess( null, y ); // $ExpectError
41+
lowess( undefined, y ); // $ExpectError
42+
lowess( 5, y ); // $ExpectError
43+
lowess( {}, y ); // $ExpectError
44+
lowess( ( x: number ): number => x, y ); // $ExpectError
45+
}
46+
47+
// The function does not compile if provided a second argument that is not an array of numbers...
48+
{
49+
const x = [ 1.0, 2.0, 3.0 ];
50+
lowess( x, 'abc' ); // $ExpectError
51+
lowess( x, true ); // $ExpectError
52+
lowess( x, false ); // $ExpectError
53+
lowess( x, null ); // $ExpectError
54+
lowess( x, undefined ); // $ExpectError
55+
lowess( x, 5 ); // $ExpectError
56+
lowess( x, {} ); // $ExpectError
57+
lowess( x, ( x: number ): number => x ); // $ExpectError
58+
}
59+
60+
// The compiler throws an error if the function is provided a third argument which is not an options object...
61+
{
62+
const x = [ 1.0, 2.0, 3.0 ];
63+
const y = [ 2.0, 3.5, 3.9 ];
64+
lowess( x, y, true ); // $ExpectError
65+
lowess( x, y, false ); // $ExpectError
66+
lowess( x, y, null ); // $ExpectError
67+
lowess( x, y, 5 ); // $ExpectError
68+
lowess( x, y, 'abc' ); // $ExpectError
69+
lowess( x, y, ( x: number ): number => x ); // $ExpectError
70+
}
71+
72+
// The compiler throws an error if the function is provided a `f` option which is not a number...
73+
{
74+
const x = [ 1.0, 2.0, 3.0 ];
75+
const y = [ 2.0, 3.5, 3.9 ];
76+
lowess( x, y, { 'f': 'abc' } ); // $ExpectError
77+
lowess( x, y, { 'f': '123' } ); // $ExpectError
78+
lowess( x, y, { 'f': true } ); // $ExpectError
79+
lowess( x, y, { 'f': false } ); // $ExpectError
80+
lowess( x, y, { 'f': null } ); // $ExpectError
81+
lowess( x, y, { 'f': [] } ); // $ExpectError
82+
lowess( x, y, { 'f': {} } ); // $ExpectError
83+
lowess( x, y, { 'f': ( x: number ): number => x } ); // $ExpectError
84+
}
85+
86+
// The compiler throws an error if the function is provided a `nsteps` option which is not a number...
87+
{
88+
const x = [ 1.0, 2.0, 3.0 ];
89+
const y = [ 2.0, 3.5, 3.9 ];
90+
lowess( x, y, { 'nsteps': 'abc' } ); // $ExpectError
91+
lowess( x, y, { 'nsteps': '123' } ); // $ExpectError
92+
lowess( x, y, { 'nsteps': true } ); // $ExpectError
93+
lowess( x, y, { 'nsteps': false } ); // $ExpectError
94+
lowess( x, y, { 'nsteps': null } ); // $ExpectError
95+
lowess( x, y, { 'nsteps': [] } ); // $ExpectError
96+
lowess( x, y, { 'nsteps': {} } ); // $ExpectError
97+
lowess( x, y, { 'nsteps': ( x: number ): number => x } ); // $ExpectError
98+
}
99+
100+
// The compiler throws an error if the function is provided a `delta` option which is not a number...
101+
{
102+
const x = [ 1.0, 2.0, 3.0 ];
103+
const y = [ 2.0, 3.5, 3.9 ];
104+
lowess( x, y, { 'delta': 'abc' } ); // $ExpectError
105+
lowess( x, y, { 'delta': '123' } ); // $ExpectError
106+
lowess( x, y, { 'delta': true } ); // $ExpectError
107+
lowess( x, y, { 'delta': false } ); // $ExpectError
108+
lowess( x, y, { 'delta': null } ); // $ExpectError
109+
lowess( x, y, { 'delta': [] } ); // $ExpectError
110+
lowess( x, y, { 'delta': {} } ); // $ExpectError
111+
lowess( x, y, { 'delta': ( x: number ): number => x } ); // $ExpectError
112+
}
113+
114+
// The compiler throws an error if the function is provided a `sorted` option which is not a boolean...
115+
{
116+
const x = [ 1.0, 2.0, 3.0 ];
117+
const y = [ 2.0, 3.5, 3.9 ];
118+
lowess( x, y, { 'sorted': 'abc' } ); // $ExpectError
119+
lowess( x, y, { 'sorted': '123' } ); // $ExpectError
120+
lowess( x, y, { 'sorted': 123 } ); // $ExpectError
121+
lowess( x, y, { 'sorted': null } ); // $ExpectError
122+
lowess( x, y, { 'sorted': [] } ); // $ExpectError
123+
lowess( x, y, { 'sorted': {} } ); // $ExpectError
124+
lowess( x, y, { 'sorted': ( x: number ): number => x } ); // $ExpectError
125+
}
126+
127+
// The function does not compile if provided an invalid number of arguments...
128+
{
129+
const x = [ 1.0, 2.0, 3.0 ];
130+
const y = [ 2.0, 3.5, 3.9 ];
131+
lowess( x ); // $ExpectError
132+
lowess( x, y, {}, {} ); // $ExpectError
133+
}

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

+1
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)