Function fit

  • Minimize the sum of squared errors to fit a set of data points to a curve with a set of unknown parameters.

    Type Parameters

    Parameters

    • f: F<T>

      The model function for curve fitting.

    • data: Datum<T>[]

      The entire dataset, as an array of points.

    • params_initial: number[] = []

      The initial guess for function parameters, which defaults to an array filled with zeroes.

    • iterations: number = 1e3

      The number of parameter sets to generate.

    • maxDeviation: number = 1

      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 Summary<T>

    The set of parameters and error for the best fit.

    Example

    // 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);