Least squares in java.

A small library for using least squres fitting in java. Build using the JAMA matrix library

License

License

Categories

Categories

Ant Build Tools Square Business Logic Libraries Financial
GroupId

GroupId

org.orangepalantir
ArtifactId

ArtifactId

leastsquares
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Least squares in java.
A small library for using least squres fitting in java. Build using the JAMA matrix library
Project URL

Project URL

https://github.com/odinsbane/least-squares-in-java
Source Code Management

Source Code Management

https://github.com/odinsbane/least-squares-in-java

Download leastsquares

How to add to project

<!-- https://jarcasting.com/artifacts/org.orangepalantir/leastsquares/ -->
<dependency>
    <groupId>org.orangepalantir</groupId>
    <artifactId>leastsquares</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.orangepalantir/leastsquares/
implementation 'org.orangepalantir:leastsquares:1.0.0'
// https://jarcasting.com/artifacts/org.orangepalantir/leastsquares/
implementation ("org.orangepalantir:leastsquares:1.0.0")
'org.orangepalantir:leastsquares:jar:1.0.0'
<dependency org="org.orangepalantir" name="leastsquares" rev="1.0.0">
  <artifact name="leastsquares" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.orangepalantir', module='leastsquares', version='1.0.0')
)
libraryDependencies += "org.orangepalantir" % "leastsquares" % "1.0.0"
[org.orangepalantir/leastsquares "1.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
gov.nist.math : jama jar 1.0.3

test (1)

Group / Artifact Type Version
junit : junit jar 4.8.2

Project Modules

There are no modules declared in this project.

least-squares-in-java

Java Least Squares fitting library.

This is a small least squares fitting library made in java. It was originally used in the development of an image analysis tool SpeckleTrackerJ. http://athena.physics.lehigh.edu/speckletrackerj/ It uses methods described in "Numerical Recipes in C", Second Edition (1992).

The outline of using this library is you have a set of data that you would like fit a function to. Define the Function, how it evaluates the input and paramters. Create a Fitter (three types currently) initialized to the function. Set the fitter with the data and make an initial guess of the parameters. Tell the fitter to find the best set of parameters which minimizes the sum of squares of error.

Example

Lets say we have some data.

double[][] xs = {
    {0}, {1}, {2}, {3}, {4}, {5}
};
double[] z = {1.1, 0.9, 1.0, 1.35, 1.82, 2.5};

And we want to fit a quadratic function f(x) = Axx + B*x + C. The parameters are A, B, C and the input is x. So we have 3 parameters and 1 input.

Function fun = new Function(){
    @Override
    public double evaluate(double[] values, double[] parameters) {
        double A = parameters[0];
        double B = parameters[1];
        double C = parameters[2];
        double x = values[0];
        return A*x*x + B*x + C;
    }
    @Override
    public int getNParameters() {
        return 3;
    }

    @Override
    public int getNInputs() {
        return 1;
    }
};

The next step is to create a fitter.

Fitter fit = new LinearFitter(fun);

Set the data, and make a guess at the initial parameters.

fit.setData(xs, zs);
fit.setParameters(new double[]{0,0,0});

fit.fitData();

The results can be obtained by using getParameters.

System.out.println(Arrays.toString(fit.getParameters()));
#[0.10000000000000023, -0.20000000000000123, 1.000000000000001]

This example is included in the examples package.

Fitter implementations

LinearFitter: for use with equations that are linearly dependent on the input parameters. Standard linear regression where derivatives are taken by setting all of the parameters to zero, except for the one of interest.

NonLinearSolver: Similar to the LinearSolver, except it will run multiple iterations, there is a damping factor, and the derivatives are calculated by taken by varying the parameters a small amount from the previous guess.

MarquardtFitter: Similar to the NonLinearSolver except the damping is adaptive.

Versions

Version
1.0.0