Type definition for the library 'regression' (#31728)

* Type definition for the library 'regression'

* Fix to match the commonJS style used by the real library
This commit is contained in:
MattiasMartens
2018-12-31 08:44:53 -08:00
committed by Wesley Wigham
parent 5ab05e7f2d
commit cdd6ada582
4 changed files with 126 additions and 0 deletions
+95
View File
@@ -0,0 +1,95 @@
// Type definitions for regression 2.0
// Project: https://github.com/Tom-Alexander/regression-js
// Definitions by: Mattias B. Martens <https://github.com/MattiasMartens>
// Definitions: https://github.com/MattiasMartens/DefinitelyTyped
/**
* [x, y]
*/
export type DataPoint = [number, number];
export interface Options {
/**
* The number of decimal places to round to.
* This is used to round the calculated fitting coefficients,
* the output predictions, and the value of r^2.
*/
precision?: number;
/**
* The number of terms to solve for (and therefore
* the number of coefficients to calculate). Only
* relevant for polynomial fitting.
*/
order?: number;
}
export interface Result {
/**
* A human-readable string representation of the derived
* formula in the form y = f(x) where f depends on the
* fitting method used and the coefficients that were
* calculated.
*/
string: string;
/**
* For each point (x, y) in the input data, a point
* corresponding to the regression prediction for that
* value of x.
* One could use this to directly evaluate the quality
* of the fit.
*/
points: ReadonlyArray<DataPoint>;
/**
* Function that takes an arbitrary value of x and
* produces a coordinate representing the y-value of
* the regression curve at that point.
* Both the resulting x- and y-values are rounded to
* a number of decimal places defined in the options
* (default is 2).
*/
predict: (x: number) => DataPoint;
/**
* The generated coefficients describing the equation
* of best fit.
*
* For a linear fit, the coefficients are `[a, b]` in `y = a * x + b`.
* For an exponential fit, the coefficients are `[a, b]` in `y = a * e ^ (b * x)`.
* For a logarithmic fit, the coefficients are `[a, b]` in `y = a + b * ln(x)`.
* For a power fit, the coefficients are `[a, b]` in `y = a * x^b`.
* For a polynomial fit, the coefficients are `[a0, a1, a2, ...aN]` in:
* ```y = a0 * x ^ N + a1 * x ^ (N - 1) + ... + aN```
* where N is the order (default 2).
*/
equation: number[];
/**
* The value of R squared, a statistical measure of the conformance of the
* fitted curve to the input data where 1 is an exact fit and 0 is no fit
* at all.
*
* This value is rounded to the number of decimal places defined by
* the precision option (default 2).
*/
r2: number;
}
export function _round(number: number, precision: number): number;
export function linear(
data: ReadonlyArray<DataPoint>,
options?: Options
): Result;
export function exponential(
data: ReadonlyArray<DataPoint>,
options?: Options
): Result;
export function logarithmic(
data: ReadonlyArray<DataPoint>,
options?: Options
): Result;
export function power(
data: ReadonlyArray<DataPoint>,
options?: Options
): Result;
export function polynomial(
data: ReadonlyArray<DataPoint>,
options?: Options
): Result;
+12
View File
@@ -0,0 +1,12 @@
import * as Regression from "regression";
const data: ReadonlyArray<[number, number]> = [[0, 0], [1, 1], [2, 2]];
const result1 = Regression.linear(data);
const result2 = Regression.exponential(data);
const result3 = Regression.logarithmic(data);
const result4 = Regression.power(data);
const result5 = Regression.polynomial(data);
const result6 = Regression._round(10.312, 3);
const result7 = Regression.polynomial(data, { order: 4 });
const result8 = Regression.polynomial(data, { precision: 4 });
const result9 = Regression.polynomial(data, { order: 4, precision: 4 });
+16
View File
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.d.ts", "regression-tests.ts"]
}
+3
View File
@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}