diff --git a/types/regression/index.d.ts b/types/regression/index.d.ts new file mode 100644 index 0000000000..a2959be3d2 --- /dev/null +++ b/types/regression/index.d.ts @@ -0,0 +1,95 @@ +// Type definitions for regression 2.0 +// Project: https://github.com/Tom-Alexander/regression-js +// Definitions by: Mattias B. Martens +// 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; + /** + * 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, + options?: Options +): Result; +export function exponential( + data: ReadonlyArray, + options?: Options +): Result; +export function logarithmic( + data: ReadonlyArray, + options?: Options +): Result; +export function power( + data: ReadonlyArray, + options?: Options +): Result; +export function polynomial( + data: ReadonlyArray, + options?: Options +): Result; diff --git a/types/regression/regression-tests.ts b/types/regression/regression-tests.ts new file mode 100644 index 0000000000..0d9532aacf --- /dev/null +++ b/types/regression/regression-tests.ts @@ -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 }); diff --git a/types/regression/tsconfig.json b/types/regression/tsconfig.json new file mode 100644 index 0000000000..e57fee7c91 --- /dev/null +++ b/types/regression/tsconfig.json @@ -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"] +} diff --git a/types/regression/tslint.json b/types/regression/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/regression/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}