peakml.math.function
Class LinearFunction

java.lang.Object
  extended by jaolho.data.lma.LMAFunction
      extended by peakml.math.function.LinearFunction
All Implemented Interfaces:
Function

public class LinearFunction
extends jaolho.data.lma.LMAFunction
implements Function

Implementation of a linear function f(x)=ax+b. This class provides tight integration with the LMA fit package for function fitting.


Field Summary
static int PARAM_A
          The a-parameter from the array returned by getParameters().
static int PARAM_B
          The b-parameter from the array returned by getParameters().
 
Constructor Summary
LinearFunction(double... parameters)
          Constructs a new linear function with the a and b parameters stored in the array.
LinearFunction(double a, double b)
          Constructs a new linear function with the given a and b parameters.
 
Method Summary
static LinearFunction fit(double[] xvals, double[] yvals)
          Fits a linear function through the given data.
 jaolho.data.lma.LMA getLMA()
          When a fit has been performed by calling fit(double[], double[]) the returned object contains data on the fit.
 double[] getParameters()
          Returns the array with the a (first element) and b (second element) parameters stored in the instance of this class.
 double getPartialDerivate(double x, double[] a, int parameterIndex)
           
 double[] getXVals()
          When a fit has been performed by calling fit(double[], double[]) the returned array contains the x-values used for the fit.
 double getY(double x)
          Returns the y-value for the given x-value with the parameters stored in the instance of the class.
 double getY(double x, double[] a)
          Returns the y-value for the given x-value with the given parameters
 double[] getYVals()
          When a fit has been performed by calling fit(double[], double[]) the returned array contains the y-values used for the fit.
static double[] MLE(double[] xvals)
          Performs an estimation of the best starting parameters for the fitting procedure.
 double rSquare()
           
 
Methods inherited from class jaolho.data.lma.LMAFunction
constructWeights, generateData, generateData, generateData
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PARAM_A

public static final int PARAM_A
The a-parameter from the array returned by getParameters().

See Also:
Constant Field Values

PARAM_B

public static final int PARAM_B
The b-parameter from the array returned by getParameters().

See Also:
Constant Field Values
Constructor Detail

LinearFunction

public LinearFunction(double... parameters)
               throws java.lang.RuntimeException
Constructs a new linear function with the a and b parameters stored in the array. The size of the array must be 2 and the first element needs to contain the a-parameter and the second element the b-parameter.

Parameters:
parameters - The array with the a and b parameters.
Throws:
java.lang.RuntimeException - Thrown when the parameters array has a different size than 2 elements.

LinearFunction

public LinearFunction(double a,
                      double b)
Constructs a new linear function with the given a and b parameters.

Parameters:
a - The a parameter.
b - The b parameter.
Method Detail

getY

public double getY(double x)
            throws java.lang.NullPointerException
Returns the y-value for the given x-value with the parameters stored in the instance of the class.

Specified by:
getY in interface Function
Parameters:
x - The x-value.
Returns:
The y-value for the given x-value.
Throws:
java.lang.NullPointerException - When no parameters are stored in the class.

getY

public double getY(double x,
                   double[] a)
            throws java.lang.RuntimeException,
                   java.lang.NullPointerException
Returns the y-value for the given x-value with the given parameters

Specified by:
getY in class jaolho.data.lma.LMAFunction
Parameters:
x - The x-value.
a - The parameters for the function.
Returns:
The y-value for the given x-value.
Throws:
java.lang.RuntimeException - Thrown when the parameters array a has a different size than 2 elements.
java.lang.NullPointerException - When the a is null.

getPartialDerivate

public double getPartialDerivate(double x,
                                 double[] a,
                                 int parameterIndex)
Specified by:
getPartialDerivate in class jaolho.data.lma.LMAFunction

getLMA

public jaolho.data.lma.LMA getLMA()
When a fit has been performed by calling fit(double[], double[]) the returned object contains data on the fit. This is provided for integration purposes with the LMA fit package. It's encouraged to use the access methods of this class to get to the information in this object.

Returns:
The result of the fitting procedure.

getParameters

public double[] getParameters()
Returns the array with the a (first element) and b (second element) parameters stored in the instance of this class.

Returns:
The parameters of this function.

getXVals

public double[] getXVals()
When a fit has been performed by calling fit(double[], double[]) the returned array contains the x-values used for the fit.

Returns:
The x-values for this fit.

getYVals

public double[] getYVals()
When a fit has been performed by calling fit(double[], double[]) the returned array contains the y-values used for the fit.

Returns:
The y-values for this fit.

rSquare

public double rSquare()

MLE

public static double[] MLE(double[] xvals)
Performs an estimation of the best starting parameters for the fitting procedure. This function is used by fit(double[], double[]) and has been made public for integration purposes.

Returns:
The best parameters for starting the fit on the provided x-values.

fit

public static LinearFunction fit(double[] xvals,
                                 double[] yvals)
                          throws java.lang.RuntimeException
Fits a linear function through the given data. This method makes use of the LMA fit package for making the fit. The parameters are estimated and returned in the new LinearFunction object.

Parameters:
xvals - The x-values to fit the line to.
yvals - The y-values to fit the line to.
Returns:
A LinearFunction instance fitted to the data.
Throws:
java.lang.RuntimeException - Thrown when an error is encountered in the fitting procedure.