peakml.math.function
Class PolynomialFunction

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

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

Implementation of a polynomial function, which can be plugged with the order (n) to use. The polynomial has the form f(x) = x^n + x^n-1 + .. + x. This class provides tight integration with the LMA fit package for function fitting.


Constructor Summary
PolynomialFunction(double... parameters)
          Constructs a new polynomial function with the given parameters.
PolynomialFunction(int order)
          Constructs a new polynomial function of the given order.
 
Method Summary
static PolynomialFunction fit(int order, double[] xvals, double[] yvals)
          Fits a polynomial function of the given order through the given data.
 jaolho.data.lma.LMA getLMA()
          When a fit has been performed by calling fit(int, double[], double[]) the returned object contains data on the fit.
 int getOrder()
          Returns the order of the polynomial function.
 double[] getParameters()
          Returns the array with the parameters of this function.
 double getPartialDerivate(double x, double[] a, int parameterIndex)
           
 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
static double[] MLE(int order, double[] xvals)
          Performs an estimation of the best starting parameters for the fitting procedure.
 
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
 

Constructor Detail

PolynomialFunction

public PolynomialFunction(int order)
Constructs a new polynomial function of the given order. All the parameters are initialized to 0 (i.e. this type of function does not do anything).

Parameters:
order - The order of the polynomial.

PolynomialFunction

public PolynomialFunction(double... parameters)
Constructs a new polynomial function with the given parameters. Necessarily, the order of the function will be the number of parameters minus 1.

Parameters:
parameters - The parameters for the function.
Method Detail

getOrder

public int getOrder()
Returns the order of the polynomial function.

Returns:
The order of the function.

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(int, 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 parameters of this function. This array is necessarily order-1 of size.

Returns:
The parameters of the function.

MLE

public static double[] MLE(int order,
                           double[] xvals)
Performs an estimation of the best starting parameters for the fitting procedure. This function is used by fit(int, 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 PolynomialFunction fit(int order,
                                     double[] xvals,
                                     double[] yvals)
                              throws java.lang.RuntimeException
Fits a polynomial function of the given order 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:
order - The order of the polynomial function.
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.