-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add Box-Cox transform #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kgryte
merged 19 commits into
stdlib-js:develop
from
justin1dennison:rfc-117-boxcox-transform
Oct 22, 2018
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9baa1b7
initial implementation of boxcox transform package
justin1dennison fff2b8f
added benchmarking
justin1dennison 479e477
added the python benchmark for scipy boxcox
justin1dennison 79cb918
altered to use float64 eps constant
justin1dennison a803e8f
added tests for boxcox
justin1dennison 0c755ab
fixed tests and test fixtures
justin1dennison 2621037
added negative lambda parameters and removed extraneous new lines
justin1dennison b14d250
updated keywords and description in package.json
justin1dennison 64c64ff
updated jsdoc for boxcox
justin1dennison 2a1acb1
updated import of boxcox in examples
justin1dennison 64bb683
updated formatting and output of repl.txt
justin1dennison 43b28b8
added end of file newline and removed odd indentation
justin1dennison b6b5170
fixed formatting of readme including examples and equation
justin1dennison bd82695
updated benchmarking code
justin1dennison 47908b4
altered the data fixture for testing the implementation of boxcox
justin1dennison 79531f3
updated the threshold for tolerance testing of values
justin1dennison e8d1d0b
removed equation image
justin1dennison e3cfbbd
mirrored epsilon usage from scipy instead of float62-eps
justin1dennison 4d705b9
altered testing to testing more random bit arrangements. Had to alter…
justin1dennison File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
lib/node_modules/@stdlib/math/base/special/boxcox/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2018 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # boxcox | ||
|
|
||
| > Compute the [Box-Cox transformation][box-cox-transformation]. | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| The [Box-Cox transformation][box-cox-transformation] is defined as | ||
|
|
||
| <!-- <equation class="equation" label="eq:boxcox_transformation_one_parameter" align="center" raw="y_{i}^{\lambda } = \left\{\begin{matrix}\frac{y_{i}^{\lambda} - 1}{\lambda} & if \lambda \neq 0,\\ln(y) & if \lambda = 0,\end{matrix}\right." alt="One Parameter Box-Cox Transformation"> --> | ||
|
|
||
| <div class="equation" align="center" data-raw-text="y_{i}^{\lambda } = \left\{\begin{matrix\frac{y_{i}^{\lambda} - 1}{\lambda} & if \lambda \neq 0,\\ln(y) & if \lambda = 0, | ||
| \end{matrix}\right." data-equation="eq:boxcox_transformation_one_parameter"> | ||
| <img src="" alt="One Parameter Box-Cox Transformation" /> | ||
| <br> | ||
| </div> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| </section> | ||
justin1dennison marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- Package usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var boxcox = require( '@stdlib/math/base/special/boxcox' ); | ||
| ``` | ||
|
|
||
| #### boxcox( x, lambda) | ||
|
|
||
| Compute the [Box-Cox transformation][box-cox-transformation]. | ||
|
|
||
| ```javascript | ||
| var v = boxcox( 1.0, 2.5 ); | ||
| // returns 0.0 | ||
|
|
||
| v = boxcox( 4.0, 2.5 ); | ||
| // returns 12.4 | ||
|
|
||
| v = boxcox( 10.0, 2.5 ); | ||
| // returns ~126.0911 | ||
|
|
||
| v = boxcox( 2.0, 0.0 ); | ||
| // returns ~0.6931 | ||
|
|
||
| v = boxcox( -1.0, 2.5 ); | ||
| // returns NaN | ||
|
|
||
| v = boxcox( 0.0, -1.0 ); | ||
| // returns -Infinity | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var incrspace = require( '@stdlib/math/utils/incrspace' ); | ||
| var boxcox = require( '@stdlib/math/base/special/boxcox' ); | ||
|
|
||
| var x = incrspace( -1.0, 10.0, 1.0 ); | ||
| var l = incrspace( -0.5, 5.0, 0.5 ); | ||
| var b; | ||
| var i; | ||
| var j; | ||
|
|
||
| for ( i = 0; i < x.length; i++ ) { | ||
| for ( j = 0; j < l.length; j++ ) { | ||
| b = boxcox( x[ i ], l[ j ] ); | ||
| console.log( 'x: %d, lambda: %d, b(x, lambda): %d', x[ i ], l[ j ], b ); | ||
| } | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="references"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.references --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [box-cox-transformation]: https://en.wikipedia.org/wiki/Power_transform#Box-Cox_transformation | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
53 changes: 53 additions & 0 deletions
53
lib/node_modules/@stdlib/math/base/special/boxcox/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2018 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var randu = require( '@stdlib/random/base/randu' ); | ||
| var boxcox = require( '@stdlib/math/base/special/boxcox' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var pkg = require( './../package.json' ).name; | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var r; | ||
| var i; | ||
| var x; | ||
| var y; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| x = ( randu() * 10.0 ) + 1.0; | ||
| y = ( randu() * 10.0 ) + 1.0; | ||
| r = boxcox( x, y ); | ||
| if ( isnan( r ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( r ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
97 changes: 97 additions & 0 deletions
97
lib/node_modules/@stdlib/math/base/special/boxcox/benchmark/python/benchmark.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| #!/usr/bin/env python | ||
| # | ||
| # @license Apache-2.0 | ||
| # | ||
| # Copyright (c) 2018 The Stdlib Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Benchmark scipy.special.boxcox.""" | ||
|
|
||
| from __future__ import print_function | ||
| import timeit | ||
|
|
||
| NAME = "boxcox" | ||
| REPEATS = 3 | ||
| ITERATIONS = 1000000 | ||
|
|
||
|
|
||
| def print_version(): | ||
| """Print the TAP version.""" | ||
| print("TAP version 13") | ||
|
|
||
|
|
||
| def print_summary(total, passing): | ||
| """Print the benchmark summary. | ||
|
|
||
| # Arguments | ||
|
|
||
| * `total`: total number of tests | ||
| * `passing`: number of passing tests | ||
|
|
||
| """ | ||
| print("#") | ||
| print("1.." + str(total)) | ||
| print("# total " + str(total)) | ||
| print("# pass " + str(passing)) | ||
| print("#") | ||
| print("# ok") | ||
|
|
||
|
|
||
| def print_results(elapsed): | ||
| """Print benchmark results. | ||
|
|
||
| # Arguments | ||
|
|
||
| * `elapsed`: elapsed time (in seconds) | ||
|
|
||
| # Examples | ||
|
|
||
| ```python | ||
| python> print_results(0.2543188374) | ||
| ``` | ||
| """ | ||
| rate = ITERATIONS / elapsed | ||
|
|
||
| print(" ---") | ||
| print(" iterations: " + str(ITERATIONS)) | ||
| print(" elapsed: " + str(elapsed)) | ||
| print(" rate: " + str(rate)) | ||
| print(" ...") | ||
|
|
||
|
|
||
| def benchmark(): | ||
| """Run the benchmark and print benchmark results.""" | ||
| setup = "from scipy.special import boxcox; from random import random;" | ||
| stmt = "boxcox((100.0 * random()) - 50.0, (100.0 * random()) - 50.0)" | ||
|
|
||
| t = timeit.Timer(stmt, setup=setup) | ||
|
|
||
| print_version() | ||
|
|
||
| for i in range(REPEATS): | ||
| print("# python::scipy::special::" + NAME) | ||
| elapsed = t.timeit(number=ITERATIONS) | ||
| print_results(elapsed) | ||
| print("ok " + str(i+1) + " benchmark finished") | ||
|
|
||
| print_summary(REPEATS, REPEATS) | ||
|
|
||
|
|
||
| def main(): | ||
| """Run the benchmark.""" | ||
| benchmark() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
34 changes: 34 additions & 0 deletions
34
lib/node_modules/@stdlib/math/base/special/boxcox/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
|
|
||
| {{alias}}( x, lambda ) | ||
| Computes the Box-Cox transformation. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x: number | ||
| Input value. | ||
|
|
||
| lambda: number | ||
| Power parameter. | ||
|
|
||
| Returns | ||
| ------- | ||
| b: number | ||
| Function value. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var v = {{alias}}( 1.0, 2.5 ) | ||
| 0.0 | ||
| > v = {{alias}}( 4.0, 2.5 ) | ||
| 12.4 | ||
| > v = {{alias}}( 10.0, 2.5 ) | ||
| ~126.0911 | ||
| > v = {{alias}}( 2.0, 0.0 ) | ||
| ~0.6931 | ||
| > v = {{alias}}( -1.0, 2.5 ) | ||
| NaN | ||
| > v = {{alias}}( 0.0, -1.0 ) | ||
| -Infinity | ||
|
|
||
| See Also | ||
| -------- |
35 changes: 35 additions & 0 deletions
35
lib/node_modules/@stdlib/math/base/special/boxcox/examples/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2018 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| var incrspace = require( '@stdlib/math/utils/incrspace' ); | ||
| var boxcox = require( './../lib' ); | ||
|
|
||
| var x = incrspace( -1.0, 10.0, 1.0 ); | ||
| var l = incrspace( -0.5, 5.0, 0.5 ); | ||
| var b; | ||
| var i; | ||
| var j; | ||
|
|
||
| for ( i = 0; i < x.length; i++ ) { | ||
| for ( j = 0; j < l.length; j++ ) { | ||
| b = boxcox( x[ i ], l[ j ] ); | ||
| console.log( 'x: %d, lambda: %d, b(x, lambda): %d', x[ i ], l[ j ], b ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.