Skip to content

Commit 0f887d6

Browse files
committed
Add type definitions
1 parent 6bbf4e2 commit 0f887d6

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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+
/**
22+
* Evaluates the cumulative distribution function (CDF) for a normal distribution.
23+
*
24+
* @param x - input value
25+
* @returns evaluated CDF
26+
*/
27+
type Unary = ( x: number ) => number;
28+
29+
/**
30+
* Interface for the cumulative distribution function (CDF) of a normal distribution.
31+
*/
32+
interface CDF {
33+
/**
34+
* Evaluates the cumulative distribution function (CDF) for a normal distribution with mean `mu` and standard deviation `sigma` at a value `x`.
35+
*
36+
* ## Notes
37+
*
38+
* - If provided `sigma < 0`, the function returns `NaN`.
39+
*
40+
* @param x - input value
41+
* @param mu - mean
42+
* @param sigma - standard deviation
43+
* @returns evaluated cumulative distribution function
44+
*
45+
* @example
46+
* var y = cdf( 2.0, 0.0, 1.0 );
47+
* // returns ~0.977
48+
*
49+
* @example
50+
* var y = cdf( -1.0, -1.0, 2.0 );
51+
* // returns 0.5
52+
*
53+
* @example
54+
* var y = cdf( -1.0, 4.0, 2.0 );
55+
* // returns ~0.006
56+
*
57+
* @example
58+
* var y = cdf( NaN, 0.0, 1.0 );
59+
* // returns NaN
60+
*
61+
* @example
62+
* var y = cdf( 0.0, NaN, 1.0 );
63+
* // returns NaN
64+
*
65+
* @example
66+
* var y = cdf( 0.0, 0.0, NaN );
67+
* // returns NaN
68+
*
69+
* @example
70+
* // Negative standard deviation:
71+
* var y = cdf( 2.0, 0.0, -1.0 );
72+
* // returns NaN
73+
*/
74+
( x: number, mu: number, sigma: number ): number;
75+
76+
/**
77+
* Returns a function for evaluating the cumulative distribution function (CDF) for a normal distribution.
78+
*
79+
* @param mu - mean
80+
* @param sigma - standard deviation
81+
* @returns function to evaluate the cumulative distribution function
82+
*
83+
* @example
84+
* var myCDF = cdf.factory( 10.0, 2.0 );
85+
* var y = myCDF( 10.0 );
86+
* // returns 0.5
87+
*
88+
* y = myCDF( 12.0 );
89+
* // returns ~0.841
90+
*/
91+
factory( mu: number, sigma: number ): Unary;
92+
}
93+
94+
/**
95+
* Normal distribution cumulative distribution function (CDF).
96+
*
97+
* @param x - input value
98+
* @param mu - mean
99+
* @param sigma - standard deviation
100+
* @returns evaluated CDF
101+
*
102+
* @example
103+
* var y = cdf( 2.0, 0.0, 1.0 );
104+
* // returns ~0.977
105+
*
106+
* var myCDF = cdf.factory( 10.0, 2.0 );
107+
* y = myCDF( 10.0 );
108+
* // returns 0.5
109+
*/
110+
declare var cdf: CDF;
111+
112+
113+
// EXPORTS //
114+
115+
export = cdf;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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 cdf = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
cdf( 2, 2, 4 ); // $ExpectType number
27+
cdf( 1, 2, 8 ); // $ExpectType number
28+
}
29+
30+
// The compiler throws an error if the function is provided values other than three numbers...
31+
{
32+
cdf( true, 3, 6 ); // $ExpectError
33+
cdf( false, 2, 4 ); // $ExpectError
34+
cdf( '5', 1, 2 ); // $ExpectError
35+
cdf( [], 1, 2 ); // $ExpectError
36+
cdf( {}, 2, 4 ); // $ExpectError
37+
cdf( ( x: number ): number => x, 2, 4 ); // $ExpectError
38+
39+
cdf( 9, true, 12 ); // $ExpectError
40+
cdf( 9, false, 12 ); // $ExpectError
41+
cdf( 5, '5', 10 ); // $ExpectError
42+
cdf( 8, [], 16 ); // $ExpectError
43+
cdf( 9, {}, 18 ); // $ExpectError
44+
cdf( 8, ( x: number ): number => x, 16 ); // $ExpectError
45+
46+
cdf( 9, 5, true ); // $ExpectError
47+
cdf( 9, 5, false ); // $ExpectError
48+
cdf( 5, 2, '5' ); // $ExpectError
49+
cdf( 8, 4, [] ); // $ExpectError
50+
cdf( 9, 4, {} ); // $ExpectError
51+
cdf( 8, 5, ( x: number ): number => x ); // $ExpectError
52+
}
53+
54+
// The compiler throws an error if the function is provided an unsupported number of arguments...
55+
{
56+
cdf(); // $ExpectError
57+
cdf( 2 ); // $ExpectError
58+
cdf( 2, 0 ); // $ExpectError
59+
cdf( 2, 0, 4, 1 ); // $ExpectError
60+
}
61+
62+
// Attached to main export is a `factory` method which returns a function...
63+
{
64+
cdf.factory( 3, 4 ); // $ExpectType Unary
65+
}
66+
67+
// The `factory` method returns a function which returns an number...
68+
{
69+
const fcn = cdf.factory( 3, 4 );
70+
fcn( 2 ); // $ExpectType number
71+
}
72+
73+
// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments...
74+
{
75+
const fcn = cdf.factory( 3, 4 );
76+
fcn( true ); // $ExpectError
77+
fcn( false ); // $ExpectError
78+
fcn( '5' ); // $ExpectError
79+
fcn( [] ); // $ExpectError
80+
fcn( {} ); // $ExpectError
81+
fcn( ( x: number ): number => x ); // $ExpectError
82+
}
83+
84+
// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments...
85+
{
86+
const fcn = cdf.factory( 3, 4 );
87+
fcn(); // $ExpectError
88+
fcn( 2, 0 ); // $ExpectError
89+
fcn( 2, 0, 1 ); // $ExpectError
90+
}
91+
92+
// The compiler throws an error if the `factory` method is provided values other than two numbers...
93+
{
94+
cdf.factory( true, 3 ); // $ExpectError
95+
cdf.factory( false, 2 ); // $ExpectError
96+
cdf.factory( '5', 1 ); // $ExpectError
97+
cdf.factory( [], 1 ); // $ExpectError
98+
cdf.factory( {}, 2 ); // $ExpectError
99+
cdf.factory( ( x: number ): number => x, 2 ); // $ExpectError
100+
101+
cdf.factory( 9, true ); // $ExpectError
102+
cdf.factory( 9, false ); // $ExpectError
103+
cdf.factory( 5, '5' ); // $ExpectError
104+
cdf.factory( 8, [] ); // $ExpectError
105+
cdf.factory( 9, {} ); // $ExpectError
106+
cdf.factory( 8, ( x: number ): number => x ); // $ExpectError
107+
108+
cdf.factory( [], true ); // $ExpectError
109+
cdf.factory( {}, false ); // $ExpectError
110+
cdf.factory( false, '5' ); // $ExpectError
111+
cdf.factory( {}, [] ); // $ExpectError
112+
cdf.factory( '5', ( x: number ): number => x ); // $ExpectError
113+
}
114+
115+
// The compiler throws an error if the `factory` method is provided an unsupported number of arguments...
116+
{
117+
cdf.factory( 0 ); // $ExpectError
118+
cdf.factory( 0, 4, 8 ); // $ExpectError
119+
}

lib/node_modules/@stdlib/stats/base/dists/normal/cdf/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"lib": "./lib",
2222
"test": "./test"
2323
},
24+
"types": "./docs/types",
2425
"scripts": {},
2526
"homepage": "https://github.com/stdlib-js/stdlib",
2627
"repository": {

0 commit comments

Comments
 (0)