Skip to content

Commit 4976e95

Browse files
justin1dennisonkgryte
authored andcommitted
Add Box-Cox transformation with special consideration for small x values (#223)
* created readme for boxcox1p * created package.json * initial implementation of boxcox1p function * added benchmarking ofboxcox1p * added tests and fixtures for tests * added repl docs * added boxcox1p simple examples * fixed typo in jsdoc * applied suggestions to README * applied suggestions to repl.txt * applied suggestions to boxcox1p implementation * updated package.json * updated tests
1 parent 447265b commit 4976e95

File tree

13 files changed

+799
-0
lines changed

13 files changed

+799
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# boxcox1p
22+
23+
> Compute a one-parameter [Box-Cox transformation][box-cox-transformation] of `1+x`.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
A one-parameter [Box-Cox transformation][box-cox-transformation] is defined as
30+
31+
<!-- <equation class="equation" label="eq:boxcox_transformation_one_parameter" align="center" raw="y^{\lambda} = \begin{cases}\frac{(y + 1)^{\lambda} - 1}{\lambda} & \textrm{if}\ \lambda \neq 0 \\ \log(y + 1) & \textrm{if}\ \lambda = 0 \end{cases}" alt="One-Parameter Box-Cox Transformation"> -->
32+
33+
<div class="equation" align="center" data-raw-text="y^{\lambda} = \begin{cases}\frac{(y + 1)^{\lambda} - 1}{\lambda} & \textrm{if}\ \lambda \neq 0 \\ \log(y + 1) & \textrm{if}\ \lambda = 0 \end{cases}" data-equation="eq:boxcox_transformation_one_parameter">
34+
<img src="" alt="One-Parameter Box-Cox Transformation" />
35+
<br>
36+
</div>
37+
38+
<!-- </equation> -->
39+
40+
</section>
41+
42+
<!-- /.intro -->
43+
44+
<!-- Package usage documentation. -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var boxcox1p = require( '@stdlib/math/base/special/boxcox1p' );
52+
```
53+
54+
#### boxcox1p( x, lambda )
55+
56+
Compute a one-parameter [Box-Cox transformation][box-cox-transformation].
57+
58+
```javascript
59+
var v = boxcox1p( 1.0, 2.5 );
60+
// returns ~1.8627
61+
62+
v = boxcox1p( 4.0, 2.5 );
63+
// returns ~21.9607
64+
65+
v = boxcox1p( 10.0, 2.5 );
66+
// returns ~160.1246
67+
68+
v = boxcox1p( 2.0, 0.0 );
69+
// returns ~1.0986
70+
71+
v = boxcox1p( -1.0, 2.5 );
72+
// returns -0.4
73+
74+
v = boxcox1p( 0.0, -1.0 );
75+
// returns 0.0
76+
77+
v = boxcox1p( -1.0, -1.0 );
78+
// returns -Infinity
79+
```
80+
81+
</section>
82+
83+
<!-- /.usage -->
84+
85+
<!-- Package usage examples. -->
86+
87+
<section class="examples">
88+
89+
## Examples
90+
91+
<!-- eslint no-undef: "error" -->
92+
93+
```javascript
94+
var incrspace = require( '@stdlib/math/utils/incrspace' );
95+
var boxcox1p = require( '@stdlib/math/base/special/boxcox1p' );
96+
97+
var x = incrspace( -1.0, 10.0, 1.0 );
98+
var l = incrspace( -0.5, 5.0, 0.5 );
99+
var b;
100+
var i;
101+
var j;
102+
103+
for ( i = 0; i < x.length; i++ ) {
104+
for ( j = 0; j < l.length; j++ ) {
105+
b = boxcox1p( x[ i ], l[ j ] );
106+
console.log( 'boxcox1p(%d, %d) = %d', x[ i ], l[ j ], b );
107+
}
108+
}
109+
```
110+
111+
</section>
112+
113+
<!-- /.examples -->
114+
115+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
116+
117+
<section class="links">
118+
119+
[box-cox-transformation]: https://en.wikipedia.org/wiki/Power_transform#Box-Cox_transformation
120+
121+
</section>
122+
123+
<!-- /.links -->
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var boxcox1p = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var r;
36+
var i;
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
x = ( randu()*10.0 ) + 1.0;
41+
y = ( randu()*10.0 ) + 1.0;
42+
r = boxcox1p( x, y );
43+
if ( isnan( r ) ) {
44+
b.fail( 'should not return NaN' );
45+
}
46+
}
47+
b.toc();
48+
if ( isnan( r ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
#
3+
# @license Apache-2.0
4+
#
5+
# Copyright (c) 2018 The Stdlib Authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
"""Benchmark scipy.special.boxcox1p."""
20+
21+
from __future__ import print_function
22+
import timeit
23+
24+
NAME = "boxcox1p"
25+
REPEATS = 3
26+
ITERATIONS = 1000000
27+
28+
29+
def print_version():
30+
"""Print the TAP version."""
31+
print("TAP version 13")
32+
33+
34+
def print_summary(total, passing):
35+
"""Print the benchmark summary.
36+
37+
# Arguments
38+
39+
* `total`: total number of tests
40+
* `passing`: number of passing tests
41+
42+
"""
43+
print("#")
44+
print("1.." + str(total))
45+
print("# total " + str(total))
46+
print("# pass " + str(passing))
47+
print("#")
48+
print("# ok")
49+
50+
51+
def print_results(elapsed):
52+
"""Print benchmark results.
53+
54+
# Arguments
55+
56+
* `elapsed`: elapsed time (in seconds)
57+
58+
# Examples
59+
60+
```python
61+
python> print_results(0.2543188374)
62+
```
63+
"""
64+
rate = ITERATIONS / elapsed
65+
66+
print(" ---")
67+
print(" iterations: " + str(ITERATIONS))
68+
print(" elapsed: " + str(elapsed))
69+
print(" rate: " + str(rate))
70+
print(" ...")
71+
72+
73+
def benchmark():
74+
"""Run the benchmark and print benchmark results."""
75+
setup = "from scipy.special import boxcox1p; from random import random;"
76+
stmt = "boxcox1p((100.0 * random()) - 50.0, (100.0 * random()) - 50.0)"
77+
78+
t = timeit.Timer(stmt, setup=setup)
79+
80+
print_version()
81+
82+
for i in range(REPEATS):
83+
print("# python::scipy::special::" + NAME)
84+
elapsed = t.timeit(number=ITERATIONS)
85+
print_results(elapsed)
86+
print("ok " + str(i+1) + " benchmark finished")
87+
88+
print_summary(REPEATS, REPEATS)
89+
90+
91+
def main():
92+
"""Run the benchmark."""
93+
benchmark()
94+
95+
96+
if __name__ == "__main__":
97+
main()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
{{alias}}( x, lambda )
3+
Computes a one-parameter Box-Cox transformation of 1+x.
4+
5+
Parameters
6+
----------
7+
x: number
8+
Input value.
9+
10+
lambda: number
11+
Power parameter.
12+
13+
Returns
14+
-------
15+
b: number
16+
Function value.
17+
18+
Examples
19+
--------
20+
> var v = {{alias}}( 1.0, 2.5 )
21+
~1.8627
22+
> v = {{alias}}( 4.0, 2.5 )
23+
~21.9607
24+
> v = {{alias}}( 10.0, 2.5 )
25+
~160.1246
26+
> v = {{alias}}( 2.0, 0.0 )
27+
~1.0986
28+
> v = {{alias}}( -1.0, 2.5 )
29+
-0.4
30+
> v = {{alias}}( 0.0, -1.0 )
31+
0.0
32+
> v = {{alias}}( -1.0, -1.0 )
33+
-Infinity
34+
35+
See Also
36+
--------
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
'use strict';
20+
21+
var incrspace = require( '@stdlib/math/utils/incrspace' );
22+
var boxcox1p = require( './../lib' );
23+
24+
var x = incrspace( -1.0, 10.0, 1.0 );
25+
var l = incrspace( -0.5, 5.0, 0.5 );
26+
var b;
27+
var i;
28+
var j;
29+
30+
for ( i = 0; i < x.length; i++ ) {
31+
for ( j = 0; j < l.length; j++ ) {
32+
b = boxcox1p( x[ i ], l[ j ] );
33+
console.log( 'boxcox1p(%d, %d) = %d', x[ i ], l[ j ], b );
34+
}
35+
}

0 commit comments

Comments
 (0)