-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement exponential regression
- Loading branch information
0 parents
commit 0718866
Showing
11 changed files
with
2,984 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,3 @@ | ||
{ | ||
"plugins": ["transform-es2015-modules-commonjs"] | ||
} |
This file contains 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,5 @@ | ||
extends: 'eslint-config-cheminfo' | ||
parserOptions: | ||
sourceType: module | ||
env: | ||
jest: true |
This file contains 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,39 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules | ||
jspm_packages | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
lib |
This file contains 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,4 @@ | ||
language: node_js | ||
node_js: | ||
- node | ||
- lts/boron |
This file contains 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 mljs | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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,40 @@ | ||
# regression-exponential | ||
|
||
[![NPM version][npm-image]][npm-url] | ||
[![build status][travis-image]][travis-url] | ||
[![npm download][download-image]][download-url] | ||
|
||
Exponential Regression. | ||
|
||
## Installation | ||
|
||
`$ npm install --save ml-regression-exponential` | ||
|
||
## Usage | ||
|
||
This calculates parameters A and B for the equation `y = A * e^(B * x)`. | ||
|
||
```js | ||
import ExponentialRegression from 'ml-regression-exponential'; | ||
|
||
const x = [0, 1, 2, 3, 4]; | ||
const y = [1.5, 2.5, 3.5, 5.0, 7.5]; | ||
|
||
const regression = new ExponentialRegression(x, y); | ||
|
||
regression.A // 0.391202 | ||
regression.B // 1.579909 | ||
regression.predict(2); // 3.454825 | ||
regression.toString(3); // f(x) = 1.58 * exp(0.391 * x) | ||
``` | ||
|
||
## License | ||
|
||
[MIT](./LICENSE) | ||
|
||
[npm-image]: https://img.shields.io/npm/v/ml-regression-exponential.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/ml-regression-exponential | ||
[travis-image]: https://img.shields.io/travis/mljs/regression-exponential/master.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/mljs/regression-exponential | ||
[download-image]: https://img.shields.io/npm/dm/ml-regression-exponential.svg?style=flat-square | ||
[download-url]: https://npmjs.org/package/ml-regression-exponential |
This file contains 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,46 @@ | ||
{ | ||
"name": "ml-regression-exponential", | ||
"version": "0.0.0", | ||
"description": "Exponential Regression", | ||
"main": "lib/index.js", | ||
"module": "src/index.js", | ||
"files": [ | ||
"lib", | ||
"src" | ||
], | ||
"scripts": { | ||
"eslint": "eslint src", | ||
"eslint-fix": "npm run eslint -- --fix", | ||
"prepublish": "rollup -c", | ||
"test": "run-s testonly eslint", | ||
"testonly": "jest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/mljs/regression-exponential.git" | ||
}, | ||
"keywords": [], | ||
"author": "Michaël Zasso", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mljs/regression-exponential/issues" | ||
}, | ||
"homepage": "https://github.com/mljs/regression-exponential#readme", | ||
"jest": { | ||
"testEnvironment": "node" | ||
}, | ||
"devDependencies": { | ||
"babel-jest": "^19.0.0", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1", | ||
"eslint": "^3.19.0", | ||
"eslint-config-cheminfo": "^1.7.0", | ||
"eslint-plugin-no-only-tests": "^1.1.0", | ||
"jest": "^19.0.2", | ||
"npm-run-all": "^4.0.2", | ||
"rollup": "^0.41.6" | ||
}, | ||
"dependencies": { | ||
"ml-regression-base": "^1.1.1", | ||
"ml-regression-simple-linear": "^1.0.1" | ||
} | ||
} |
This file contains 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,5 @@ | ||
export default { | ||
entry: 'src/index.js', | ||
format: 'cjs', | ||
dest: 'lib/index.js' | ||
}; |
This file contains 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 @@ | ||
import ExponentialRegression from '..'; | ||
|
||
describe('Exponential regression', () => { | ||
it('basic test', () =>{ | ||
const x = [0, 1, 2, 3, 4]; | ||
const y = [1.5, 2.5, 3.5, 5.0, 7.5]; | ||
const result = new ExponentialRegression(x, y); | ||
expect(result.A).toBeCloseTo(0.3912023, 10e-7); | ||
expect(result.B).toBeCloseTo(1.579910, 10e-7); | ||
|
||
const score = result.score(x, y); | ||
expect(score.r2).toBeGreaterThan(0.8); | ||
expect(score.chi2).toBeLessThan(0.1); | ||
expect(score.rmsd).toBeLessThan(0.01); | ||
expect(result.toString(4)).toEqual('f(x) = 1.580 * e^(0.3912 * x)'); | ||
expect(result.toLaTeX(4)).toEqual('f(x) = 1.580e^{0.3912x}'); | ||
}); | ||
|
||
it('toJSON / load model', function () { | ||
const regression = ExponentialRegression.load({ | ||
name: 'exponentialRegression', | ||
A: -1, | ||
B: 1 | ||
}); | ||
|
||
expect(regression.predict(1)).toEqual(0.36787944117144233); | ||
|
||
const model = regression.toJSON(); | ||
expect(model).toEqual({ | ||
name: 'exponentialRegression', | ||
A: -1, | ||
B: 1 | ||
}); | ||
}); | ||
}); |
This file contains 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,62 @@ | ||
import BaseRegression, {maybeToPrecision} from 'ml-regression-base'; | ||
import SimpleLinearRegression from 'ml-regression-simple-linear'; | ||
|
||
export default class ExponentialRegression extends BaseRegression { | ||
constructor(x, y) { | ||
super(); | ||
if (x === true) { | ||
this.A = y.A; | ||
this.B = y.B; | ||
} else { | ||
const n = x.length; | ||
if (n !== y.length) { | ||
throw new RangeError('input and output array have a different length'); | ||
} | ||
|
||
regress(this, x, y, n); | ||
} | ||
} | ||
|
||
_predict(input) { | ||
return this.B * Math.exp(input * this.A); | ||
} | ||
|
||
toJSON() { | ||
return { | ||
name: 'exponentialRegression', | ||
A: this.A, | ||
B: this.B | ||
}; | ||
} | ||
|
||
toString(precision) { | ||
return 'f(x) = ' + maybeToPrecision(this.B, precision) + ' * e^(' + maybeToPrecision(this.A, precision) + ' * x)'; | ||
} | ||
|
||
toLaTeX(precision) { | ||
if (this.A >= 0) { | ||
return 'f(x) = ' + maybeToPrecision(this.B, precision) + 'e^{' + maybeToPrecision(this.A, precision) + 'x}'; | ||
} else { | ||
return 'f(x) = \\frac{' + maybeToPrecision(this.B, precision) + '}{e^{' + maybeToPrecision(-this.A, precision) + 'x}}'; | ||
} | ||
|
||
} | ||
|
||
static load(json) { | ||
if (json.name !== 'exponentialRegression') { | ||
throw new TypeError('not a exponential regression model'); | ||
} | ||
return new ExponentialRegression(true, json); | ||
} | ||
} | ||
|
||
function regress(er, x, y, n) { | ||
const yl = new Array(n); | ||
for (let i = 0; i < n; i++) { | ||
yl[i] = Math.log(y[i]); | ||
} | ||
|
||
const linear = new SimpleLinearRegression(x, yl); | ||
er.A = linear.slope; | ||
er.B = Math.exp(linear.intercept); | ||
} |
Oops, something went wrong.