UNPKG

datafit

Version:

Simple curve-fitting algorithm

28 lines (27 loc) 1.3 kB
import { Datum, F, Summary, VariableType } from './types'; /** * Minimize the sum of squared errors to fit a set of data * points to a curve with a set of unknown parameters. * @param f The model function for curve fitting. * @param data The entire dataset, as an array of points. * @param params_initial The initial guess for function * parameters, which defaults to an array filled with zeroes. * @param iterations The number of parameter sets to generate. * @param maxDeviation The relative standard parameter deviation. * This is a number [0.0-1.0] and affects the standard deviation * on the first iteration. Every subsequent iteration has a * decayed standard deviation until the final iteration. * @returns The set of parameters and error for the best fit. * @example * ```ts * // Define model function * function f(x: number, a2: number = -0.5, a1: number = 3.9, a0: number = -1.2): number { * return a2 * x ** 2 + a1 * x + a0; * } * // Construct a data set * const data: Datum<number>[] = [0, 2, 4].map(x => ({ x: x, y: f(x) })); * // Compute best-fit summary * const summary = fit(f, data); * ``` */ export declare function fit<T extends VariableType>(f: F<T>, data: Datum<T>[], params_initial?: number[], iterations?: number, maxDeviation?: number): Summary<T>;