luminsports/levenberg-marquardt

Maintainers

Package info

github.com/luminsports/php-levenberg-marquardt

pkg:composer/luminsports/levenberg-marquardt

Transparency log

Statistics

Installs: 17

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

1.0.0 2022-08-18 07:27 UTC

This package is auto-updated.

Last update: 2026-07-15 03:59:01 UTC


README

Code Style PHPStan Tests

Fit nonlinear parameterized functions to paired numeric data with a fluent, dependency-light PHP API. The package supports bounds, weights, forward or central finite differences, damping controls, error and iteration reporting, and predictions from the fitted curve.

Requirements

  • PHP 8.1 or later
  • markbaker/matrix 2.1 or later

Installation

composer require luminsports/levenberg-marquardt

Example

use LuminSports\LevenbergMarquardt\LevenbergMarquardt;

$xValues = [0, 1, 2, 3, 4];
$yValues = [1, 3, 5, 7, 9];

$model = (new LevenbergMarquardt)
    ->setParameterizedFunction(
        fn (float $slope, float $intercept) =>
            fn (float $x): float => $slope * $x + $intercept,
    )
    ->setInitialValues([1, 0])
    ->setXValues($xValues)
    ->setYValues($yValues);

$curve = $model->getCurve();

$curve->getParameters(); // approximately ['slope' => 2.0, 'intercept' => 1.0]
$curve->getError();
$curve->getIterations();
$model->predict([5, 6]); // array of Point objects

The parameterized function receives the values to fit and must return a function of the independent variable. Initial, minimum, and maximum values follow the same parameter order. predict() returns Point objects; call toArray() when an ['x' => ..., 'y' => ...] representation is needed.

Development

composer install
composer check-style
composer phpstan
composer test

Generate a text coverage report with:

composer test -- --coverage-text

Background and Credit

The implementation is based on the derivative-free nonlinear least-squares methods described by Brown and Dennis and is a PHP port of mljs/levenberg-marquardt. The upstream MIT license and attribution are retained in LICENSE.md.

License

Released under the MIT License. See LICENSE.md and COPYRIGHT.