[Home] DMelt member access . Java source code: flanagan.analysis.Regression
/*
* Class Regression
*
* Contains methods for simple linear regression
* (straight line), for multiple linear regression,
* for fitting data to a polynomial and for non-linear
* regression (Nelder and Mead Simplex method) for both user
* supplied functions and for a wide range of standard functions
*
* The sum of squares function needed by the non-linear regression methods
* non-linear regression methods is supplied by means of the interfaces,
* RegressionFunction, Regression2 or RegressionFunction3
*
* WRITTEN BY: Dr Michael Thomas Flanagan
*
* DATE: February 2002
* MODIFIED: 5-6 July 2004,
* 7 January 2006, 28 July 2006, 9 August 2006, 4 November 2006, 21 November 2006, 21 December 2006,
* 14 April 2007, 9 June 2007, 25 July 2007, 23/24 August 2007, 14 September 2007, 28 December 2007,
* 18-26 March 2008, 7 April 2008, 27 April 2008, 10/12/19 May 2008, 28 July 2008, 29 August 2008, 5 September 2008, 6 October 2008,
* 13-15 October 2009, 13 November 2009, 10 December 2009, 20 December 2009,
* 12 January 2010, 18-25 May 2010, 9 July 2010, 10-16 August 2010, 21-29 October 2010, 2-7 November 2010,
* 2 January 2011, 20-31 January 2011, 2-7 February 2011, 21-27 February 2011, 30 March 2011, 13 November 2011
* 16 January 2012, 21 January 2012,
* 20-31 March 2012(MAJOR REVISION),
* 22-25 June 2012
*
* DOCUMENTATION:
* See Michael Thomas Flanagan's Java library on-line web page:
* http://www.ee.ucl.ac.uk/~mflanaga/java/Regression.html
* http://www.ee.ucl.ac.uk/~mflanaga/java/
*
* Copyright (c) 2002 - 2012 Michael Thomas Flanagan
*
* PERMISSION TO COPY:
*
* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
* and associated documentation or publications.
*
* Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice, this list of conditions
* and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
*
* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
*
* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
* or its derivatives.
*
***************************************************************************************/
package flanagan.analysis;
import java.util.*;
import javax.swing.JOptionPane;
import flanagan.math.*;
import flanagan.io.*;
import flanagan.plot.Plot;
import flanagan.plot.PlotGraph;
import flanagan.analysis.*;
import flanagan.circuits.Impedance;
import flanagan.interpolation.CubicSpline;
// Regression class
public class Regression{
protected int nData0=0; // number of y data points inputted (in a single array if multiple y arrays)
protected int nData=0; // number of y data points (nData0 times the number of y arrays)
protected double nEffective = 0; // effective nData for weighte data
protected int nXarrays=1; // number of x arrays
protected int nYarrays=1; // number of y arrays
protected int nParam=0; // number of unknown parameters to be estimated
// multiple linear (a + b.x1 +c.x2 + . . ., = nXarrays + 1
// polynomial fitting; = polynomial degree + 1
// generalised linear; = nXarrays
// simplex = no. of parameters to be estimated
protected int degreesOfFreedom=0; // degrees of freedom = nData - nParam
protected double[][] xData=null; // x data values
protected double[][] xErrors=null; // y error values
protected boolean xErrorsEntered = false; // = true when x errors entered
protected double[] yData=null; // y data values
protected double[] yErrors=null; // y error values
protected boolean yErrorsEntered = false; // = true when y errors entered
protected boolean dualErrorsRequired = false; // = true if both x and y errors allowed and used by the method
protected boolean trueErrors = true; // = true if entered y or x and y errors are true estimates of the standard deviations;
protected double[] weight=null; // weighting factors; either y errors or calculated x and y propagated errors
// scaled to the sd of the observations if weight set as arbitrary
protected double[] yCalc=null; // calculated y values using the regression coefficients
protected double[] residual=null; // residuals
protected double[] residualW=null; // weighted residuals
protected boolean weightOpt=false; // weighting factor option
// = true; weight supplied
// = false; weigths set to unity in regression
// average error used in statistical methods
// if more than 40% this.weight[i] = zero,
// weighOpt is set to false and
// all weight set to unity
// else zero weight set to average of neighbours
protected int weightFlag = 0; // weighting flag - weightOpt = false, weightFlag = 0; weightOpt = true, weightFlag = 1
protected String[] weightWord = {"", "Weighted "};
protected double[] best = null; // best estimates vector of the unknown parameters
protected double[] bestSd =null; // standard deviation estimates of the best estimates of the unknown parameters
protected double[] pseudoSd = null; // Pseudo-nonlinear sd
protected double[] tValues = null; // t-values of the best estimates
protected double[] pValues = null; // p-values of the best estimates
protected double fixedInterceptL = 0.0; // Fixed intercept (linear regression)
protected double fixedInterceptP = 0.0; // Fixed intercept (polynomial fitting)
protected double yMean=Double.NaN; // mean of y data
protected double yWeightedMean=Double.NaN; // weighted mean of y data
protected double chiSquare=Double.NaN; // chi square (observed-calculated)^2/variance; weighted error sum of squares
protected double reducedChiSquare=Double.NaN; // reduced chi square
protected double sumOfSquaresError=Double.NaN; // Sum of the squares of the residuals; unweighted error sum of squares
protected double sumOfSquaresTotal=Double.NaN; // Total sum of the squares
protected double sumOfSquaresRegrn=Double.NaN; // Regression sum of the squares
protected double lastSSnoConstraint=0.0D; // Last sum of the squares of the residuals with no constraint penalty
protected double[][] covar=null; // Covariance matrix
protected double[][] corrCoeff=null; // Correlation coefficient matrix
protected double xyR = Double.NaN; // correlation coefficient between x and y data (y = a + b.x only)
protected double yyR = Double.NaN; // correlation coefficient between y calculted and y data (all regressions)
protected double multR = Double.NaN; // coefficient of determination
protected double adjustedR = Double.NaN; // adjusted coefficient of determination
protected double multipleF = Double.NaN; // coefficient of determination: F-ratio
protected double multipleFprob = Double.NaN; // coefficient of determination: F-ratio probability
protected String[] paraName = null; // names of parameters, eg, mean, sd; c[0], c[1], c[2] . . .
protected int prec = 4; // number of places to which double variables are truncated on output to text files
protected int field = 13; // field width on output to text files
protected int simplexFlag = 1; // = 1; unweighted or y weight; y 1D; RegressionFunction, at least, required
// = 2; unweighted or y weight; y 2D; RegressionFunction2, at least, required
// = 3; x and y weight; y 1D; RegressionFunction3 required
// = 4; x and y weight; y 2D; RegressionFunction3 required
protected int derivFlag = 1; // = 1; RegressionDerivativeFunction
// = 2; RegressionDerivativeFunction2
protected boolean nonLinStatsNeeded = true; // = false if non linear stats not to be called from nelderMead
protected int lastMethod = -1; // code indicating the last regression procedure attempted
// = 0 multiple linear regression, y = a + b.x1 +c.x2 . . .
// = 1 polynomial fitting, y = a +b.x +c.x^2 . . .
// = 2 generalised multiple linear y = a.f1(x) + b.f2(x) . . .
// = 3 Nelder and Mead simplex
// = 4 Fit to a Gaussian distribution (see also 38 below)
// = 5 Fit to a Lorentzian distribution
// = 6 Fit to a Poisson distribution
// = 7 Fit to a Two Parameter Gumbel distribution (minimum order statistic)
// = 8 Fit to a Two Parameter Gumbel distribution (maximum order statistic)
// = 9 Fit to a One Parameter Gumbel distribution (minimum order statistic)
// = 10 Fit to One Parameter Gumbel distribution (maximum order statistic)
// = 11 Fit to a Standard Gumbel distribution (minimum order statistic)
// = 12 Fit to a Standard Gumbel distribution (maximum order statistic)
// = 13 Fit to a Three parameter Frechet distribution
// = 14 Fit to a Two Parameter Frechet distribution
// = 15 Fit to a Standard Frechet distribution
// = 16 Fit to a Three parameter Weibull distribution
// = 17 Fit to a Two Parameter Weibull distribution
// = 18 Fit to a Standard Weibull distribution
// = 19 Fit to a Two Parameter Exponential distribution
// = 20 Fit to a One Parameter Exponential distribution
// = 21 Fit to a Standard Parameter Exponential distribution
// = 22 Fit to a Rayleigh distribution
// = 23 Fit to a Two Parameter Pareto distribution
// = 24 Fit to a One Parameter Pareto distribution
// = 25 Fit to a Sigmoidal Threshold Function
// = 26 Fit to a rectangular Hyperbola
// = 27 Fit to a scaled Heaviside Step Function
// = 28 Fit to a Hills/Sips Sigmoid
// = 29 Fit to a Shifted Pareto distribution
// = 30 Fit to a Logistic distribution
// = 31 Fit to a Beta distribution - [0, 1] interval
// = 32 Fit to a Beta distribution - [min, max] interval
// = 33 Fit to a Three Parameter Gamma distribution
// = 34 Fit to a Standard Gamma distribution
// = 35 Fit to an Erlang distribution
// = 36 Fit to a two parameter log-normal distribution
// = 37 Fit to a three parameter log-normal distribution
// = 38 Fit to a Gaussian distribution [allows fixed p-arameters] (see also 4 above)
// = 39 Fit to a EC50 dose response curve (four parameter logistic)
// = 40 Fit to a EC50 dose response curve - top and bottom fixed
// = 41 Fit to a EC50 dose response curve (four parameter logistic) - bottom constrained
// = 42 Five parameter logistic function - top and bottom fixed (see also method 51)
// = 43 Fit to a simple exponential, A.exp(Bx)
// = 44 Fit to multiple exponentials
// = 45 Fit to a A(1 - exp(Bx))
// = 46 Fit to a constant
// = 47 Linear fit with fixed intercept
// = 48 Polynomial fit with a fixed intercept
// = 49 Multiple Gaussians
// = 50 Non-integer polynomial
// = 51 Five parameter logistic function (see also method 42 above)
// = 52 Shifted rectangular hyperbola
// non-linear special function methods accepting both x and y errors
protected int[] dualMethods = {19, 20, 21, 25, 26, 28, 39, 40, 41, 42, 43, 44, 45, 50, 51, 52};
protected int nSpecDual = dualMethods.length;
protected boolean bestPolyFlag = false; // = true if bestPolynomial called
protected int bestPolynomialDegree = 0; // degree of best polynomial fit
protected double fProbSignificance = 0.05; // significance level used in F-test in bestPolynomial method
protected ArrayList bestPolyArray = new ArrayList(); // array storing history of bestPolynomial search pathway
protected boolean userSupplied = true; // = true - user supplies the initial estimates for non-linear regression
// = false - the initial estimates for non-linear regression are calculated internally
protected double kayValue = 0.0D; // rate parameter value in Erlang distribution (method 35)
protected boolean frechetWeibull = true; // Frechet Weibull switch - if true Frechet, if false Weibull
protected boolean linNonLin = true; // if true linear method, if false non-linear method
protected boolean trueFreq = false; // true if xData values are true frequencies, e.g. in a fit to Gaussian
// false if not
// if true chiSquarePoisson (see above) is also calculated
protected String xLegend = "x axis values"; // x axis legend in X-Y plot
protected String yLegend = "y axis values"; // y axis legend in X-Y plot
protected String graphTitle = " "; // user supplied graph title
protected String graphTitle2 = " "; // second line graph title
protected boolean legendCheck = false; // = true if above legends overwritten by user supplied legends
protected boolean supressPrint = false; // = true if print results is to be supressed
protected boolean supressYYplot= false; // = true if plot of experimental versus calculated is to be supressed
protected boolean supressErrorMessages= false; // = true if some designated error messages are to be supressed
// Non-linear members
protected boolean nlrStatus=true; // Status of non-linear regression on exiting regression method
// = true - convergence criterion was met
// = false - convergence criterion not met - current estimates returned
protected int scaleOpt=0; // if = 0; no scaling of initial estimates
// if = 1; initial simplex estimates scaled to unity
// if = 2; initial estimates scaled by user provided values in scale[]
// (default = 0)
protected double[] scale = null; // values to scale initial estimate (see scaleOpt above)
protected boolean zeroCheck = false; // true if any best estimate value is zero
// if true the scale factor replaces the best estimate in numerical differentiation
protected boolean penalty = false; // true if single parameter penalty function is included
protected boolean sumPenalty = false; // true if multiple parameter penalty function is included
protected int nConstraints = 0; // number of single parameter constraints
protected int nSumConstraints = 0; // number of multiple parameter constraints
protected int maxConstraintIndex = -1; // maximum index of constrained parameter/s
protected double constraintTolerance = 1e-4; // tolerance in constraining parameter/s to a fixed value
protected ArrayList penalties = new ArrayList(); // constrant method index,
// number of single parameter constraints,
// then repeated for each constraint:
// penalty parameter index,
// below or above constraint flag,
// constraint boundary value
protected ArrayList sumPenalties = new ArrayList(); // constraint method index,
// number of multiple parameter constraints,
// then repeated for each constraint:
// number of parameters in summation
// penalty parameter indices,
// summation signs
// below or above constraint flag,
// constraint boundary value
protected int[] penaltyCheck = null; // = -1 values below the single constraint boundary not allowed
// = +1 values above the single constraint boundary not allowed
protected int[] sumPenaltyCheck = null; // = -1 values below the multiple constraint boundary not allowed
// = +1 values above the multiple constraint boundary not allowed
protected double penaltyWeight = 1.0e30; // weight for the penalty functions
protected int[] penaltyParam = null; // indices of paramaters subject to single parameter constraint
protected int[][] sumPenaltyParam = null; // indices of paramaters subject to multiple parameter constraint
protected double[][] sumPlusOrMinus = null; // valueall before each parameter in multiple parameter summation
protected int[] sumPenaltyNumber = null; // number of paramaters in each multiple parameter constraint
protected double[] constraints = null; // single parameter constraint values
protected double[] sumConstraints = null; // multiple parameter constraint values
protected int constraintMethod = 0; // constraint method number
// =0: cliff to the power two (only method at present)
protected ArrayList constrainedSingle = new ArrayList(); // single constraint phrase
protected ArrayList constrainedMultiple = new ArrayList(); // multiple constraint phrase
String[] constraintString = null; // all constraint phrases
protected boolean scaleFlag = true; // if true ordinate scale factor, Ao, included as unknown in fitting to special functions
// if false Ao set to unity (default value) or user provided value (in yScaleFactor)
protected double yScaleFactor = 1.0D; // y axis factor - set if scaleFlag (above) = false
protected int nMax = 3000; // Nelder and Mead simplex maximum number of iterations allowed
protected int minIter = 300; // Nelder and Mead simplex minimum number of iterations required
protected int nIter = 0; // Nelder and Mead simplex number of iterations performed
protected int konvge = 3; // Nelder and Mead simplex number of restarts allowed
protected int kRestart = 0; // Nelder and Mead simplex number of restarts taken
protected double fMin = -1.0D; // Nelder and Mead simplex minimum value
protected double fTol = 1e-9; // Nelder and Mead simplex convergence tolerance factor
protected double rCoeff = 1.0D; // Nelder and Mead simplex reflection coefficient
protected double eCoeff = 2.0D; // Nelder and Mead simplex extension coefficient
protected double cCoeff = 0.5D; // Nelder and Mead simplex contraction coefficient
protected double[] startH = null; // Nelder and Mead simplex unscaled initial estimates
protected double[] stepH = null; // Nelder and Mead simplex unscaled initial step values
protected double[] startSH = null; // Nelder and Mead simplex scaled initial estimates
protected double[] stepSH = null; // Nelder and Mead simplex scaled initial step values
protected double dStep = 0.1D; // Nelder and Mead simplex default step value
protected double[][] grad = null; // Non-linear regression gradients
protected double delta = 1e-4; // Fractional step in numerical differentiation
protected boolean invertFlag=true; // Hessian Matrix ('linear' non-linear statistics) check
// true matrix successfully inverted, false inversion failed
protected boolean posVarFlag=true; // Hessian Matrix ('linear' non-linear statistics) check
// true - all variances are positive; false - at least one is negative
protected int minTest = 0; // Nelder and Mead minimum test
// = 0; tests simplex sd < fTol
// = 1; tests reduced chi suare or sum of squares < mean of abs(y values)*fTol
protected double simplexSd = 0.0D; // simplex standard deviation
protected boolean statFlag = true; // if true - statistical method called
// if false - no statistical analysis
protected boolean plotOpt = true; // if true - plot of calculated values is cubic spline interpolation between the calculated values
// if false - calculated values linked by straight lines (accomodates Poiwsson distribution plots)
protected boolean multipleY = false; // = true if y variable consists of more than set of data each needing a different calculation in RegressionFunction
// when set to true - the index of the y value is passed to the function in Regression function
protected boolean ignoreDofFcheck = false; // when set to true, the check on whether degrees of freedom are greater than zero is ignored
protected double[] values = null; // values entered into gaussianFixed
protected boolean[] fixed = null; // true if above values[i] is fixed, false if it is not
protected int nGaussians = 0; // Number of Gaussian distributions in multiple Gaussian fitting
protected double[] multGaussFract = null; // Best estimates of multiple Gaussian fractional contributions
protected double[] multGaussFractErrors = null; // Errors in the estmated of multiple Gaussian fractional contributions
protected double[] multGaussCoeffVar = null; // Coefficients of variation of multiple Gaussian fractional contributions
protected double[] multGaussTvalue = null; // t-values for multiple Gaussian fractional contributions
protected double[] multGaussPvalue = null; // p-values for multiple Gaussian fractional contributions
protected double multGaussScale = 1.0; // Scale factor for multiple Gaussian fractional contributions
protected double multGaussScaleError = 0.0; // error in the scale factor for multiple Gaussian fractional contributions
protected double multGaussScaleCoeffVar = 0.0; // coeff. of var. in the scale factor for multiple Gaussian fractional contributions
protected double multGaussScaleTvalue = 0.0; // t-value of the scale factor for multiple Gaussian fractional contributions
protected double multGaussScalePvalue = 0.0; // p-value of the scale factor for multiple Gaussian fractional contributions
protected boolean plotWindowCloseChoice = false;// if false: closing window terminates program
// if true: closing window does not terminate program
protected double minimumY = 0; // minimum y-value
protected double minimumYindex = 0; // index of minimum y-value
protected double maximumY = 0; // maximum y-value
protected double maximumYindex = 0; // index of maximum y-value
protected double bottom = 0; // supplied logistic /EC50 curve bottom
protected double top = 0; // supplied logistic /EC50 curve top
protected double bottomS = 0.0; // calculated bottom value of sigmoid curves
protected double bottomSindex = 0.0; // index of the calculated bottom value of a sigmoid curve
protected double topS = 0.0; // calculated top value of a sigmoid curve
protected double topSindex = 0.0; // index of the calculated top value of a sigmoid curve
protected int midPointLowerIndex = 0; // lower index of the mid point of a sigmoid curve
protected int midPointUpperIndex = 0; // upper index of the mid point of a sigmoid curve
protected double midPointXvalue = 0.0; // x-value of the mid point of a sigmoid curve
protected double midPointYvalue = 0.0; // y-value of the mid point of a sigmoid curve
protected int directionFlag = 0; // = 1, gradient of a sigmoid curve is positive
// = -1, gradient of a sigmoid curve is negative
protected double dDurbinWatson = Double.NaN; // Durbin-Watson d statistic
protected boolean dDurbinWatsonDone = false; // = true when Durbin-Watson d calculated
protected double[][] firstDerivs = null; // first derivatives of the functionwith respect to each parameter for each data point
protected boolean analyticalDerivative = false; // = true if analytical calculation of first derivatives performed
// = false if numerical differencing used to obtain first derivatives
protected double obsnVariance = 0.0; // variance of the observations
// HISTOGRAM CONSTRUCTION
// Tolerance used in including an upper point in last histogram bin when it is outside due to rounding errors
protected static double histTol = 1.0001D;
//CONSTRUCTORS
// Default constructor - primarily facilitating the subclass ImpedSpecRegression
public Regression(){
}
// Constructor with data with x as 2D array and y errors provided
public Regression(double[][] xData, double[] yData, double[] yErrors){
this.simplexFlag = 1;
this.nData0 = yData.length;
yErrors = this.checkForZeroWeights(yErrors);
if(this.weightOpt)this.weightFlag = 1;
this.setDefaultValues(Conv.copy(xData), Conv.copy(yData), Conv.copy(yErrors));
}
// Constructor with data with x as 2D array and x and y errors provided
public Regression(double[][] xData, double[] yData, double[][] xErrors, double[] yErrors){
this.simplexFlag = 3;
this.nData0 = yData.length;
this.jointZeroCheck(xErrors, yErrors);
this.yErrorsEntered = true;
this.xErrorsEntered = true;
this.weightOpt = true;
this.weightFlag = 1;
this.setDefaultValues(Conv.copy(xData), Conv.copy(yData), Conv.copy(xErrors), Conv.copy(yErrors));
}
// Constructor with data with x and y as 2D arrays and y errors provided
public Regression(double[][] xxData, double[][] yyData, double[][] yyErrors){
this.simplexFlag = 2;
this.multipleY = true;
int nY1 = yyData.length;
this.nYarrays = nY1;
int nY2 = yyData[0].length;
this.nData0 = nY2;
int nX1 = xxData.length;
int nX2 = xxData[0].length;
double[] yData = new double[nY1*nY2];
double[] yErrors = new double[nY1*nY2];
double[][] xData = new double[nY1*nY2][nX1];
int ii=0;
for(int i=0; i40.0){
System.out.println(perCentZeros + "% of the weighting errors are zero or less; all weighting errors set to 1.0");
for(int i=0; i0.0D){
for(int i=0; i0.0D){
double ww = error[0];
error[0] = error[ii];
if(flag==1){
System.out.println("y error at point " + i + ", " + ww + ", replaced by "+ error[i]);
}
else{
System.out.println("x error at point " + (i/mm) + "," + (i%mm) + " " + ww + " replaced by "+ error[i]);
}
test = false;
}
else{
ii++;
}
}
}
if(i==(n-1)){
int ii=n-2;
boolean test = true;
while(test){
if(error[ii]>0.0D){
double ww = error[i];
error[i] = error[ii];
if(flag==1){
System.out.println("y error at point " + i + ", " + ww + ", replaced by "+ error[i]);
}
else{
System.out.println("x error at point " + (i/mm) + "," + (i%mm) + " " + ww + " replaced by "+ error[i]);
}
test = false;
}
else{
ii--;
}
}
}
if(i>0 && i<(n-2)){
double lower = 0.0;
double upper = 0.0;
int ii=i-1;
boolean test = true;
while(test){
if(error[ii]>0.0D){
lower = error[ii];
test = false;
}
else{
ii--;
if(ii==0)test = false;
}
}
ii=i+1;
test = true;
while(test){
if(error[ii]>0.0D){
upper = error[ii];
test = false;
}
else{
ii++;
if(ii==(n-1))test = false;
}
}
double ww = error[i];
if(lower==0.0){
error[i] = upper;
}
else{
if(upper==0.0){
error[i] = lower;
}
else{
error[i] = (lower + upper)/2.0;
}
}
if(flag==1){
System.out.println("y error at point " + i + ", " + ww + ", replaced by "+ error[i]);
}
else{
System.out.println("x error at point " + (i/mm) + "," + (i%mm) + " " + ww + " replaced by "+ error[i]);
}
}
}
}
}
}
return error;
}
// Joint check for matching x and y weighting error zeros
protected void jointZeroCheck(double[][] xErrors, double[] yErrors){
int n = xErrors[0].length;
int m = yErrors.length;
int q = xErrors.length;
int zeroSum = 0;
if(m!=n)throw new IllegalArgumentException("The number of x weightimg errors, " + n + ", does not match the number of y weighting errors. " + m);
for(int i=0; i=nData){
testNext = false;
}
else{
if(weight[iNext]==0.0D){
iNext++;
}
else{
next = weight[iNext];
testNext = false;
}
}
}
// Take average
weight[ii]=(last + next)/2.0D;
}
}
return flag;
}
// Set data and default values
protected void setDefaultValues(double[][] xData, double[] yData, double[] yErrors){
this.setDefaultValues(xData, yData, null, yErrors);
}
protected void setDefaultValues(double[][] xData, double[] yData, double[][] xErrors, double[] yErrors){
boolean check = true;
if(xErrors==null)check = false;
this.nData = yData.length;
this.nXarrays = xData.length;
this.nParam = this.nXarrays;
this.yData = new double[nData];
this.yCalc = new double[nData];
this.yErrors = new double[nData];
this.weight = new double[nData];
this.residual = new double[nData];
this.residualW = new double[nData];
this.xData = new double[nXarrays][nData];
if(check)this.xErrors = new double[nXarrays][nData];
int n=yErrors.length;
if(n!=this.nData)throw new IllegalArgumentException("The y error and the y data lengths do not agree");
for(int i=0; ithis.maximumY){
this.maximumY = this.yData[i];
this.maximumYindex = i;
}
}
// effective number of data points
this.effectiveNumber();
// Reassess non-linear tolerance fTol
// this.reassessFtol();
}
// Set true error flag
public void setErrorsAsSD(){
this.trueErrors = true;
}
public void setErrorsAsScaled(){
this.trueErrors = false;
}
// Calculate effective number
protected void effectiveNumber(){
double numer = 0.0;
double denom = 0.0;
double hold = 0.0;
for(int i=0; i1)throw new IllegalArgumentException("This class will only perform a polynomial regression on a single x array");
if(deg<1)throw new IllegalArgumentException("Polynomial degree must be greater than zero");
this.lastMethod = 1;
this.linNonLin = true;
this.dualErrorsRequired = false;
this.nParam = deg+1;
this.degreesOfFreedom = this.nData - this.nParam;
if(this.degreesOfFreedom<1 && !this.ignoreDofFcheck)throw new IllegalArgumentException("Degrees of freedom must be greater than 0");
double[][] aa = new double[this.nParam][this.nData];
for(int j=0; j1)throw new IllegalArgumentException("This class will only perform a polynomial regression on a single x array");
if(deg<1)throw new IllegalArgumentException("Polynomial degree must be greater than zero");
this.lastMethod = 1;
this.linNonLin = true;
this.dualErrorsRequired = true;
this.nonLinStatsNeeded = false;
this.nParam = deg+1;
this.degreesOfFreedom = this.nData - this.nParam;
if(this.degreesOfFreedom<1 && !this.ignoreDofFcheck)throw new IllegalArgumentException("Degrees of freedom must be greater than 0");
// Use unweighted linear regression to obtain initial estimates for weighted nonlinear regression
Regression regl = new Regression(this.xData, this.yData);
regl.polynomial(deg);
double[] start = regl.getBestEstimates();
// Non-linear regression
// Create a function to evaluates y and combined weight
PolyXYDEfunction dualFunc = new PolyXYDEfunction();
dualFunc.setDeg(deg);
dualFunc.setYerrors(this.yErrors);
dualFunc.setXerrors(this.xErrors);
this.linNonLin = false;
double[] step = new double[this.nParam];
for(int i=0; i1)throw new IllegalArgumentException("This class will only perform a polynomial regression on a single x array");
if(deg<1)throw new IllegalArgumentException("Polynomial degree must be greater than zero");
this.lastMethod = 48;
this.fixedInterceptP = intercept;
this.linNonLin = true;
this.dualErrorsRequired = false;
this.nParam = deg;
this.degreesOfFreedom = this.nData - this.nParam;
if(this.degreesOfFreedom<1 && !this.ignoreDofFcheck)throw new IllegalArgumentException("Degrees of freedom must be greater than 0");
double[][] aa = new double[this.nParam][this.nData];
for(int j=0; j1)throw new IllegalArgumentException("This class will only perform a polynomial regression on a single x array");
if(deg<1)throw new IllegalArgumentException("Polynomial degree must be greater than zero");
this.lastMethod = 48;
this.fixedInterceptP = intercept;
this.linNonLin = true;
this.dualErrorsRequired = true;
this.nonLinStatsNeeded = false;
this.nParam = deg;
this.degreesOfFreedom = this.nData - this.nParam;
if(this.degreesOfFreedom<1 && !this.ignoreDofFcheck)throw new IllegalArgumentException("Degrees of freedom must be greater than 0");
// Use unweighted linear regression to obtain initial estimates for weighted nonlinear regression
Regression regl = new Regression(this.xData, this.yData);
regl.polynomial(deg, intercept);
double[] start = regl.getBestEstimates();
// Non-linear regression
// Create a function to evaluates y and combined weight
PolyIXYDEfunction dualFunc = new PolyIXYDEfunction();
dualFunc.setDeg(deg);
dualFunc.setIntercept(intercept);
dualFunc.setYerrors(this.yErrors);
dualFunc.setXerrors(this.xErrors);
this.linNonLin = false;
double[] step = new double[this.nParam];
for(int i=0; i bestPolynomial(){
return polynomialBest(0);
}
// Internal method finding the best polynomial fit
public ArrayList polynomialBest(int flag){
if(this.multipleY)throw new IllegalArgumentException("This method cannot handle multiply dimensioned y arrays");
if(this.nXarrays>1)throw new IllegalArgumentException("This class will only perform a polynomial regression on a single x array");
this.bestPolyFlag = true;
this.linNonLin = true;
ArrayList array0 = null;
ArrayList array1 = new ArrayList();
int deg = 0;
int degBest = 0;
int degMax = this.nData-2;
int nComp = 0;
double fRatio = 0.0;
double fProb = 0.0;
double fSign = 0.0;
boolean test0 = true;
// Regression for a constant value
Regression reg = new Regression(xData[0], yData);
reg.constant();
double chi = reg.getChiSquare();
double chiLast = chi;
// Regression for a straight line
deg++;
if(deg>degMax){
test0 = false;
System.out.println("Method bestPolynomial requires at least three data points: null returned");
return null;
}
else{
reg.linear();
chi = reg.getChiSquare();
// first comparison
array0 = Regression.testOfAdditionalTerms_ArrayList(chiLast, 0, chi, 1, nData, this.fProbSignificance);
nComp++;
fRatio = ((Double)array0.get(0)).doubleValue();
fProb = ((Double)array0.get(1)).doubleValue();
fSign = ((Double)array0.get(8)).doubleValue();
array1.add(new Double(chiLast));
array1.add(new Double(chi));
array1.add(new Double(fRatio));
array1.add(new Double(fProb));
array1.add(new Double(fSign));
// First comparison test
if(fRatiodegMax)test0 = false;
while(test0){
chiLast = chi;
// polynomial regression
reg.polynomial(deg);
chi = reg.getChiSquare();
// comparison
array0 = Regression.testOfAdditionalTerms_ArrayList(chiLast, deg-1, chi, deg, nData, this.fProbSignificance);
nComp++;
fRatio = ((Double)array0.get(0)).doubleValue();
fProb = ((Double)array0.get(1)).doubleValue();
fSign = ((Double)array0.get(8)).doubleValue();
array1.add(new Double(chiLast));
array1.add(new Double(chi));
array1.add(new Double(fRatio));
array1.add(new Double(fProb));
array1.add(new Double(fSign));
// comparison test
if(fRatiodegMax)test0 = false;
}
this.bestPolynomialDegree = degBest;
// Repack ArrayList
int[] deg0s = new int[nComp];
int[] deg1s = new int[nComp];
double[] chi0s = new double[nComp];
double[] chi1s = new double[nComp];
double[] fRatios = new double[nComp];
double[] fProbs = new double[nComp];
double[] fSigns = new double[nComp];
for(int i=0; i=nComp)testZero = false;
}
}
switch(flag){
case 0: // No plot
switch(this.bestPolynomialDegree){
case 0: this.constant();
break;
case 1: this.linear();
break;
default: this.polynomial(this.bestPolynomialDegree);
}
break;
case 1: // Plot
switch(this.bestPolynomialDegree){
case 0: this.constantPlot();
break;
case 1: this.linearPlot();
break;
default: this.polynomialPlot(this.bestPolynomialDegree);
}
}
return this.bestPolyArray;
}
// Best polynomial
// Finds the best polynomial fit
// plus plot and output file
// Legends provided
public ArrayList bestPolynomialPlot(String xLegend, String yLegend){
this.xLegend = xLegend;
this.yLegend = yLegend;
this.legendCheck = true;
return this.polynomialBest(1);
}
// Best polynomial
// Finds the best polynomial fit
// plus plot and output file
// No legends provided
public ArrayList bestPolynomialPlot(){
return this.polynomialBest(1);
}
// Best polynomial
// Finds the best polynomial fit
// Fixed intercept
public ArrayList bestPolynomial(double fixedIntercept){
this.fixedInterceptP = fixedIntercept;
return polynomialBest(fixedIntercept, 0);
}
// Internal method finding the best polynomial fit
// Fixed intercept
public ArrayList polynomialBest(double intercept, int flag){
if(this.multipleY)throw new IllegalArgumentException("This method cannot handle multiply dimensioned y arrays");
if(this.nXarrays>1)throw new IllegalArgumentException("This class will only perform a polynomial regression on a single x array");
this.bestPolyFlag = true;
this.linNonLin = true;
this.dualErrorsRequired = false;
ArrayList array0 = null;
ArrayList array1 = new ArrayList();
int deg = 1;
int degBest = 1;
int degMax = this.nData-2;
int nComp = 0;
double fRatio = 0.0;
double fProb = 0.0;
double fSign = 0.0;
boolean test0 = true;
Regression reg = null;
if(this.xErrorsEntered){
reg = new Regression(this.xData, this.yData, this.xErrors, this.yErrors);
}
else{
if(this.weightOpt){
reg = new Regression(this.xData[0], this.yData, this.yErrors);
}
else{
reg = new Regression(this.xData[0], this.yData);
}
}
// Regression for a straight line
double chi = 0.0;
double chiLast = 0.0;
if(deg>degMax){
test0 = false;
System.out.println("Method bestPolynomial requires at least three data points: null returned");
return null;
}
else{
reg.linear(intercept);
chi = reg.getChiSquare();
}
// Iterate polynomial fittings and comparisons
deg++;
if(deg>degMax)test0 = false;
if(!test0 && degBest==0)degBest=1;
while(test0){
chiLast = chi;
// polynomial regression
reg.polynomial(deg, intercept);
chi = reg.getChiSquare();
// comparison
array0 = Regression.testOfAdditionalTerms_ArrayList(chiLast, deg-1, chi, deg, nData, this.fProbSignificance);
nComp++;
fRatio = ((Double)array0.get(0)).doubleValue();
fProb = ((Double)array0.get(1)).doubleValue();
fSign = ((Double)array0.get(8)).doubleValue();
array1.add(new Double(chiLast));
array1.add(new Double(chi));
array1.add(new Double(fRatio));
array1.add(new Double(fProb));
array1.add(new Double(fSign));
// comparison test
if(fRatiodegMax)test0 = false;
}
this.bestPolynomialDegree = degBest;
// Repack ArrayList
int[] deg0s = new int[nComp];
int[] deg1s = new int[nComp];
double[] chi0s = new double[nComp];
double[] chi1s = new double[nComp];
double[] fRatios = new double[nComp];
double[] fProbs = new double[nComp];
double[] fSigns = new double[nComp];
for(int i=0; i=nComp)testZero = false;
}
}
switch(flag){
case 0: // No plot
switch(this.bestPolynomialDegree){
case 1: this.linear(intercept);
break;
default: this.polynomial(this.bestPolynomialDegree, intercept);
}
break;
case 1: // Plot
switch(this.bestPolynomialDegree){
case 1: this.linearPlot(intercept);
break;
default: this.polynomialPlot(this.bestPolynomialDegree, intercept);
}
}
return this.bestPolyArray;
}
// Best polynomial
// Finds the best polynomial fit with a fixed intercept
// plus plot and output file
// Legends provided
public ArrayList bestPolynomialPlot(double fixedIntercept, String xLegend, String yLegend){
this.xLegend = xLegend;
this.yLegend = yLegend;
this.legendCheck = true;
return this.polynomialBest(fixedIntercept, 1);
}
// Best polynomial
// Finds the best polynomial fit
// plus plot and output file
// No legends provided
public ArrayList bestPolynomialPlot(double fixedIntercept){
return this.polynomialBest(fixedIntercept, 1);
}
// Set significance level used in bestPolynomial F-test
public void setFtestSignificance(double signif){
this.fProbSignificance = signif;
}
// get significance level used in bestPolynomial F-test
public double getFtestSignificance(double signif){
return this.fProbSignificance;
}
// Method for fitting data to a non-integer polynomial
// y = a[0] + a[1].x + a[2].x^a[n+1] + a[3].x^a[n+2] + . . . + a[n].x^a[2n-1]
// nTerms = n+1;
// No plotting
public void nonIntegerPolynomial(int nTerms){
this.fitNonIntegerPolynomial(nTerms, 0);
}
public void nonIntegerPolynomial(){
this.fitNonIntegerPolynomial(3, 0);
}
// Method for fitting data to a non-integer polynomial
// y = a[0] + a[1].x + a[2].x^a[n+1] + a[3].x^a[n+2] + . . . + a[n].x^a[2n-1]
// nTerms = n+1;
// with plotting
public void nonIntegerPolynomialPlot(int nTerms){
this.fitNonIntegerPolynomial(nTerms, 1);
}
public void nonIntegerPolynomialPlot(){
this.fitNonIntegerPolynomial(3, 1);
}
// Method for fitting data to a non-integer polynomial
// y = a[0] + a[1].x + a[2].x^a[n+1] + a[3].x^a[n+2] + . . . + a[n].x^a[2n-1]
// nTerms = n+1;
// with plotting and user supplied legends
public void nonIntegerPolynomialPlot(int nTerms, String xLegend, String yLegend){
this.xLegend = xLegend;
this.yLegend = yLegend;
this.legendCheck = true;
this.fitNonIntegerPolynomial(nTerms, 1);
}
public void nonIntegerPolynomialPlot(String xLegend, String yLegend){
this.xLegend = xLegend;
this.xLegend = xLegend;
this.yLegend = yLegend;
this.legendCheck = true;
this.fitNonIntegerPolynomial(3, 1);
}
// Internal method for fitting data to a non-integer polynomial
// y = a[0] + a[1].x + a[2].x^a[n+1] + a[3].x^a[n+2] + . . . + a[n].x^a[2n-1]
// nTerms = n+1;
protected void fitNonIntegerPolynomial(int nTerms, int plotFlag){
if(this.xErrorsEntered){
this.fitNonIntegerPolynomialDual(nTerms, plotFlag);
}
else{
this.fitNonIntegerPolynomialMono(nTerms, plotFlag);
}
}
// Internal method for fitting data to a non-integer polynomial
// y = a[0] + a[1].x + a[2].x^a[n+1] + a[3].x^a[n+2] + . . . + a[n].x^a[2n-1]
// nTerms = n+1;
// no x errors
protected void fitNonIntegerPolynomialMono(int nTerms, int plotFlag){
if(this.multipleY)throw new IllegalArgumentException("This method cannot handle multiply dimensioned y arrays");
this.lastMethod = 50;
this.userSupplied = false;
this.linNonLin = false;
this.dualErrorsRequired = false;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.zeroCheck = false;
int deg = nTerms - 1;
this.nParam = 2*nTerms - 1;
this.degreesOfFreedom = this.nData - this.nParam;
if(this.degreesOfFreedom<1 && !this.ignoreDofFcheck)throw new IllegalArgumentException("Degrees of freedom must be greater than 0");
// order data into ascending order of the abscissae
Regression.sort(this.xData[0], this.yData, this.weight);
// Estimate of parameters
Regression reg = new Regression(this.xData[0], this.yData, this.weight);
reg.polynomial(deg);
double[] start = new double[this.nParam];
double[] step = new double[this.nParam];
double[] best = reg.getBestEstimates();
double sum = 0.0;
for(int i=0; i=np)throw new IllegalArgumentException("You have entered more constrained parameters ("+this.maxConstraintIndex+") than minimisation parameters (" + np + ")");
this.nlrStatus = true; // -> false if convergence criterion not met
this.nParam = np; // number of parameters whose best estimates are to be determined
int nnp = np+1; // number of simplex apices
this.lastSSnoConstraint=0.0D; // last sum of squares without a penalty constraint being applied
if(this.scaleOpt<2)this.scale = new double[np]; // scaling factors
if(scaleOpt==2 && scale.length!=start.length)throw new IllegalArgumentException("scale array and initial estimate array are of different lengths");
if(step.length!=start.length)throw new IllegalArgumentException("step array length " + step.length + " and initial estimate array length " + start.length + " are of different");
// check for zero step sizes
for(int i=0; ithis.nMax)this.nMax=this.minIter;
// set statistic arrays to NaN if degrees of freedom check ignored
if(this.ignoreDofFcheck){
this.bestSd = new double[this.nParam];
this.pseudoSd = new double[this.nParam];
this.tValues = new double[this.nParam];
this.pValues = new double[this.nParam];
this.covar = new double[this.nParam][this.nParam];
this.corrCoeff = new double[this.nParam][this.nParam];;
for(int i=0; i0){
boolean testzero=false;
for(int i=0; iynewlo){
ynewlo=yy[i];
ihi=i;
}
}
// Calculate pbar
for (int i=0; iyi, i!=h
ln=0;
for (int i=0; i yy[i]) ++ln;
if (ln==np ){
// y*>= all yi; Check if y*>yh
if(ystar<=yy[ihi]){
// Replace ph by p*
for (int i=0; iyh
if(y2star>yy[ihi]){
//Replace all pi by (pi+pl)/2
for (int j=0; jyy[i]){
ynewlo=yy[i];
ilo=i;
}
}
sumnm /= (double)(nnp);
summnm=0.0;
for (int i=0; ithis.minIter)test=false;
break;
case 1: // terminate if the reduced chi square [weighted data] or the reduced sum of squares [unweighted data] at the lowest apex
// of the simplex is less than the mean of the absolute values of the dependent variable (y values) multiplied by the tolerance, fTol.
if(Math.sqrt(ynewlo/this.degreesOfFreedom)this.minIter)test=false;
break;
default: throw new IllegalArgumentException("Simplex standard deviation test option " + this.minTest + " not recognised");
}
this.sumOfSquaresError=ynewlo;
if(!test){
// temporary store of best estimates
for (int i=0; i0){
test=true;
for (int j=0; jthis.nMax){
if(!this.supressErrorMessages){
System.out.println("Maximum iteration number reached, in Regression.simplex(...)");
System.out.println("without the convergence criterion being satisfied");
System.out.println("Current parameter estimates and sum of squares values returned");
}
this.nlrStatus = false;
// store current estimates
for (int i=0; iconstraints[i]*(1.0+this.constraintTolerance)){
ss = tempFunctVal + this.penaltyWeight*Fmath.square(param[k]-constraints[i]*(1.0+this.constraintTolerance));
test=false;
}
break;
case 1: // parameter constrained to lie below a given constraint value
if(param[k]>constraints[i]){
ss = tempFunctVal + this.penaltyWeight*Fmath.square(param[k]-constraints[i]);
test=false;
}
break;
default: throw new IllegalArgumentException("The " + i + "th penalty check " + penaltyCheck[i] + " not recognised");
}
}
}
// multiple parameter penalty functions
if(this.sumPenalty){
int kk = 0;
double pSign = 0;
for(int i=0; isumConstraints[i]*(1.0+this.constraintTolerance)){
ss = tempFunctVal + this.penaltyWeight*Fmath.square(sumPenaltySum-sumConstraints[i]*(1.0+this.constraintTolerance));
test=false;
}
break;
case 1: // designated 'parameter sum' constrained to lie below a given constraint value
if(sumPenaltySum>sumConstraints[i]){
ss = tempFunctVal + this.penaltyWeight*Fmath.square(sumPenaltySum-sumConstraints[i]);
test=false;
}
break;
default: throw new IllegalArgumentException("The " + i + "th summation penalty check " + sumPenaltyCheck[i] + " not recognised");
}
}
}
// call function calculation and calculate the sum of squares if constraints have not intervened
if(test){
ss = 0.0D;
double hold = 0.0;
double[] yy = null;
for(int i=0; ithis.maxConstraintIndex)this.maxConstraintIndex = paramIndex;
}
// add a multiple parameter constraint boundary for the non-linear regression
public void addConstraint(int[] paramIndices, int[] plusOrMinus, int conDir, double constraint){
ArrayMaths am = new ArrayMaths(plusOrMinus);
double[] dpom = am.getArray_as_double();
addConstraint(paramIndices, dpom, conDir, constraint);
}
// add a multiple parameter constraint boundary for the non-linear regression
public void addConstraint(int[] paramIndices, double[] plusOrMinus, int conDir, double constraint){
int nCon = paramIndices.length;
int nPorM = plusOrMinus.length;
if(nCon!=nPorM)throw new IllegalArgumentException("num of parameters, " + nCon + ", does not equal number of parameter signs, " + nPorM);
this.sumPenalty=true;
// First element reserved for method number if other methods than 'cliff' are added later
if(this.sumPenalties.isEmpty())this.sumPenalties.add(new Integer(this.constraintMethod));
// add constraint
if(sumPenalties.size()==1){
this.sumPenalties.add(new Integer(1));
}
else{
int nPC = ((Integer)this.sumPenalties.get(1)).intValue();
nPC++;
this.sumPenalties.set(1, new Integer(nPC));
}
this.sumPenalties.add(new Integer(nCon));
this.sumPenalties.add(paramIndices);
this.sumPenalties.add(plusOrMinus);
this.sumPenalties.add(new Integer(conDir));
this.sumPenalties.add(new Double(constraint));
ArrayMaths am = new ArrayMaths(paramIndices);
int maxI = am.getMaximum_as_int();
if(maxI>this.maxConstraintIndex)this.maxConstraintIndex = maxI;
String chold = "";
for(int i=0; i=0){
if(i>0)chold += " + ";
}
else{
if(i>0){
chold += " - ";
}
else{
chold += "-";
}
}
chold += "p[" + ii + "]." + Math.abs(plusOrMinus[i]);
}
switch(conDir){
case -1: chold += " >= " + constraint;
break;
case 0: chold += " = " + constraint;
break;
case +1: chold += " <= " + constraint;
break;
default: throw new IllegalArgumentException("Constraint direction " + conDir + " not recognised");
}
for(int i=0; i=0; i--){
this.penalties.remove(i);
}
}
this.penalty = false;
this.nConstraints = 0;
this.constrainedSingle.clear();
// check if mutiple parameter constraints already set
if(!this.sumPenalties.isEmpty()){
int m=this.sumPenalties.size();
// remove multiple parameter constraints
for(int i=m-1; i>=0; i--){
this.sumPenalties.remove(i);
}
}
this.sumPenalty = false;
this.nSumConstraints = 0;
this.maxConstraintIndex = -1;
this.constrainedMultiple.clear();
}
// Reset the tolerance used in a fixed value constraint
public void setConstraintTolerance(double tolerance){
this.constraintTolerance = tolerance;
}
// STATISTICAL ANALYSES
// Statistical analyses for linear models
// Generalised linear regression statistics
protected void generalLinearStats(double[][] xd){
double sde=0.0D, sum=0.0D, yCalctemp=0.0D;
double[][] a = new double[this.nParam][this.nParam];
double[][] h = new double[this.nParam][this.nParam];
double[][] stat = new double[this.nParam][this.nParam];
double[][] cov = new double[this.nParam][this.nParam];
this.covar = new double[this.nParam][this.nParam];
this.corrCoeff = new double[this.nParam][this.nParam];
double[]coeffSd = new double[this.nParam];
double[]coeff = new double[this.nParam];
for(int i=0; i0){
this.adjustedR = 1.0 - (1.0 - multR)*(this.nData - 1 )/(this.nData - this.nParam - 1);
}
// F-ratio
this.multipleF = multR*(this.nData-this.nParam-1.0)/((1.0D-this.multR)*this.nParam);
if(this.multipleF>=0.0)this.multipleFprob = Stat.fTestProb(this.multipleF, this.nXarrays, this.nData-this.nParam-1);
// Durbin Watson d statistic
this.calcDurbinWatson();
// Variance of the observations
this.varianceOfObservations();
}
// linear statistics applied to a non-linear regression
// analytical Hessian derivatives
protected int pseudoLinearStats(Object regFun, Object regFunDeriv){
double f1 = 0.0D, f2 = 0.0D; // intermdiate values in step gradient calculation
int flag = 0; // returned as 0 if method fully successful;
// negative if partially successful or unsuccessful: check posVarFlag and invertFlag
// -1 posVarFlag or invertFlag is false;
// -2 posVarFlag and invertFlag are false
int np = this.nParam;
double[] f = new double[np];
double[] pmin = new double[np];
double[] coeffSd = new double[np];
double[] xd = new double[this.nXarrays];
double[][]stat = new double[np][np];
pseudoSd = new double[np];
Double temp = null;
this.grad = new double[np][2];
this.covar = new double[np][np];
this.corrCoeff = new double[np][np];
// get best estimates
pmin = Conv.copy(best);
// claculate sum ofsquares
double ss=0.0D;
for(int i=0; i=0.0D){
pseudoSd[i] = Math.sqrt(pseudoSd[i]);
}
else{
pseudoSd[i] = Double.NaN;
}
}
// calculate covariance matrix
if(np==1){
stat[0][0]=1.0D/stat[0][0];
this.covar[0][0] = stat[0][0]*red;
if(covar[0][0]>=0.0D){
coeffSd[0]=Math.sqrt(this.covar[0][0]);
corrCoeff[0][0]=1.0D;
}
else{
coeffSd[0]=Double.NaN;
corrCoeff[0][0]=Double.NaN;
this.posVarFlag=false;
}
}
else{
Matrix cov = new Matrix(stat);
if(this.supressErrorMessages)cov.supressErrorMessage();
double determinant = cov.determinant();
if(determinant==0){
this.invertFlag=false;
}
else{
cov = cov.inverse();
this.invertFlag = cov.getMatrixCheck();
}
if(this.invertFlag==false)flag--;
stat = cov.getArrayCopy();
this.posVarFlag=true;
if (this.invertFlag){
for (int i=0; i=0.0D){
coeffSd[i]=Math.sqrt(this.covar[i][i]);
}
else{
coeffSd[i]=Double.NaN;
this.posVarFlag=false;
}
}
for (int i=0; i0){
this.adjustedR = 1.0 - (1.0 - this.multR)*(this.nData - 1 )/(this.nData - this.nXarrays - 1);
}
// F-ratio
this.multipleF = this.multR*(this.nData-this.nXarrays-1.0)/((1.0D-this.multR)*this.nXarrays);
if(this.multipleF>=0.0)this.multipleFprob = Stat.fTestProb(this.multipleF, this.nXarrays, this.nData-this.nXarrays-1);
// Durbin-Watson d statistic
this.calcDurbinWatson();
// Variance of the observations
this.varianceOfObservations();
return flag;
}
// linear statistics applied to a non-linear regression
// Numerical differencing Hessian
protected int pseudoLinearStats(Object regFun){
double f1 = 0.0D, f2 = 0.0D, f3 = 0.0D, f4 = 0.0D; // intermdiate values in numerical differentiation
int flag = 0; // returned as 0 if method fully successful;
// negative if partially successful or unsuccessful: check posVarFlag and invertFlag
// -1 posVarFlag or invertFlag is false;
// -2 posVarFlag and invertFlag are false
int np = this.nParam;
double[] f = new double[np];
double[] pmin = new double[np];
double[] coeffSd = new double[np];
double[] xd = new double[this.nXarrays];
double[][]stat = new double[np][np];
pseudoSd = new double[np];
Double temp = null;
this.grad = new double[np][2];
this.covar = new double[np][np];
this.corrCoeff = new double[np][np];
// get best estimates
pmin = Conv.copy(this.best);
// gradient both sides of the minimum and first derivative at each data point
double hold0 = 1.0D;
double hold1 = 1.0D;
this.firstDerivs = new double[this.nParam][this.nData];
for (int i=0;i=0.0D){
pseudoSd[i] = Math.sqrt(pseudoSd[i]);
}
else{
pseudoSd[i] = Double.NaN;
}
}
// calculate covariance matrix
if(np==1){
hold0=pmin[0];
if(hold0==0.0D)hold0=this.stepH[0];
stat[0][0]=1.0D/stat[0][0];
this.covar[0][0] = stat[0][0]*red*hold0*hold0;
if(covar[0][0]>=0.0D){
coeffSd[0]=Math.sqrt(this.covar[0][0]);
corrCoeff[0][0]=1.0D;
}
else{
coeffSd[0]=Double.NaN;
corrCoeff[0][0]=Double.NaN;
this.posVarFlag=false;
}
}
else{
Matrix cov = new Matrix(stat);
if(this.supressErrorMessages)cov.supressErrorMessage();
double determinant = cov.determinant();
if(determinant==0){
this.invertFlag=false;
}
else{
cov = cov.inverse();
this.invertFlag = cov.getMatrixCheck();
}
if(this.invertFlag==false)flag--;
stat = cov.getArrayCopy();
this.posVarFlag=true;
if (this.invertFlag){
for (int i=0; i=0.0D){
coeffSd[i]=Math.sqrt(this.covar[i][i]);
}
else{
coeffSd[i]=Double.NaN;
this.posVarFlag=false;
}
}
for (int i=0; i0){
this.adjustedR = 1.0 - (1.0 - this.multR)*(this.nData - 1 )/(this.nData - this.nXarrays - 1);
}
// F-ratio
this.multipleF = this.multR*(this.nData-this.nXarrays-1.0)/((1.0D-this.multR)*this.nXarrays);
if(this.multipleF>=0.0)this.multipleFprob = Stat.fTestProb(this.multipleF, this.nXarrays, this.nData-this.nXarrays-1);
// Durbin-Watson d statistic
this.calcDurbinWatson();
// Variance of the observations
this.varianceOfObservations();
return flag;
}
// Calculates second derivatives for Hessian -> covariance matrices
private double secondDerivative(Object regFun, Object regDerivFun, double[] pmin, int ii, int jj){
if(ii==0 && jj==0)this.firstDerivs = new double[this.nParam][this.nData];
double[] derivs = null;
double[] xd = new double[this.nXarrays];
double sderiv =0.0;
switch(this.simplexFlag){
case 1:
RegressionDerivativeFunction regd = (RegressionDerivativeFunction)regDerivFun;
RegressionFunction reg = (RegressionFunction)regFun;
for(int i=0; i |t|");
for(int i=0; i |t|");
}
else{
fout.printtab(" ", this.field);
fout.println("estimate");
}
if(this.invertFlag){
fout.printtab(" ", this.field);
fout.printtab(" ", this.field);
fout.printtab(" ", this.field);
fout.println("variation (%)");
}
else{
fout.println(" ");
}
if(this.lastMethod==38){
int nT = 3;
int ii = 0;
for(int i=0; i |t|");
}
else{
fout.printtab(" ", this.field);
fout.println("estimate");
}
if(this.invertFlag){
fout.printtab(" ", this.field);
fout.printtab(" ", this.field);
fout.printtab(" ", this.field);
fout.println("variation (%)");
}
else{
fout.println(" ");
}
for(int i=0; i0){
System.out.println("You attempted to use Regression.plotXY() for a non-linear regression without providing the function reference in the plotXY argument list");
System.out.println("No plot attempted");
flag=-1;
return flag;
}
flag = this.plotXYlinear(title);
return flag;
}
// plot experimental x against experimental y and against calculated y
// Linear regression data
// no title provided
public int plotXY(){
int flag = -2;
if(this.multipleY){
System.out.println("You attempted to use Regression.plotXY() for a multiply dimensioned y array");
System.out.println("No plot attempted");
}
else{
flag = plotXY(this.graphTitle);
}
return flag;
}
// plot experimental x against experimental y and against calculated y
// non-linear regression data
// title provided
protected int plotXY(Object regFun, String title){
int flag = -2;
if(this.multipleY){
System.out.println("You attempted to use Regression.plotXY() for a multiply dimensioned y array");
System.out.println("No plot attempted");
}
else{
flag = this.plotXYnonlinear(regFun, title);
}
return flag;
}
protected int plotXY(RegressionFunction g, String title){
return plotXY((Object)g, title);
}
protected int plotXY(RegressionFunction2 g, String title){
return plotXY((Object)g, title);
}
protected int plotXY(RegressionFunction3 g, String title){
return plotXY((Object)g, title);
}
// plot experimental x against experimental y and against calculated y
// non-linear regression data
// no title provided
protected int plotXY(Object regFun){
int flag = -2;
if(this.multipleY){
System.out.println("You attempted to use Regression.plotXY() for a multiply dimensioned y array");
System.out.println("No plot attempted");
}
else{
flag = this.plotXYnonlinear(regFun, this.graphTitle);
}
return flag;
}
protected int plotXY(RegressionFunction g){
return plotXY((Object)g);
}
protected int plotXY(RegressionFunction2 g){
return plotXY((Object)g);
}
protected int plotXY(RegressionFunction3 g){
return plotXY((Object)g);
}
// Add legends option
public void addLegends(){
int ans = JOptionPane.showConfirmDialog(null, "Do you wish to add your own legends to the x and y axes", "Axis Legends", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(ans==0){
this.xLegend = JOptionPane.showInputDialog("Type the legend for the abscissae (x-axis) [first data set]" );
this.yLegend = JOptionPane.showInputDialog("Type the legend for the ordinates (y-axis) [second data set]" );
this.legendCheck = true;
}
}
// protected method for plotting experimental x against experimental y and against calculated y
// Linear regression
// title provided
protected int plotXYlinear(String title){
this.graphTitle = title;
int flag=0; //Returned as 0 if plot data can be plotted, -1 if not, -2 if tried multiple regression plot
if(this.nXarrays>1){
System.out.println("You attempted to use Regression.plotXY() for a multiple regression");
System.out.println("No plot attempted");
flag=-2;
return flag;
}
int ncurves = 2;
int npoints = 200;
if(npoints1){
System.out.println("Multiple Linear Regression with more than one independent variable cannot be plotted in two dimensions");
System.out.println("plotYY() called instead of plotXY()");
this.plotYY(title);
flag=-2;
}
else{
int ncurves = 2;
int npoints = 200;
if(npoints1){
System.out.println("Multiple Linear Regression with more than one independent variable cannot be plotted in two dimensions");
System.out.println("plotYY() called instead of plotXY()");
this.plotYY(title);
flag=-2;
}
else{
int ncurves = 2;
int npoints = 200;
if(npoints1)throw new IllegalArgumentException("The argument must be 0 (no scaling) 1(initial estimates all scaled to unity) or the array of scaling factors");
this.scaleOpt=n;
}
// Reset scaling factors (scaleOpt 2, see above for scaleOpt 0 and 1)
public void setScale(double[] sc){
this.scale=sc;
this.scaleOpt=2;
}
// Get scaling factors
public double[] getScale(){
return Conv.copy(this.scale);
}
// Reset the non-linear regression convergence test option
public void setMinTest(int n){
if(n<0 || n>1)throw new IllegalArgumentException("minTest must be 0 or 1");
this.minTest=n;
}
// Get the non-linear regression convergence test option
public int getMinTest(){
return this.minTest;
}
// Get the simplex sd at the minimum
public double getSimplexSd(){
return this.simplexSd;
}
// Get variance of the observations
public double getVarianceOfObserations(){
return this.obsnVariance;
}
// Get the best estimates of the unknown parameters
public double[] getBestEstimates(){
return Conv.copy(best);
}
// Get the best estimates of the unknown parameters
public double[] getCoeff(){
return Conv.copy(best);
}
// Get the estimates of the standard deviations of the best estimates of the unknown parameters
public double[] getbestestimatesStandardDeviations(){
return Conv.copy(bestSd);
}
// Get the estimates of the errors of the best estimates of the unknown parameters
public double[] getBestEstimatesStandardDeviations(){
return Conv.copy(bestSd);
}
// Get the estimates of the errors of the best estimates of the unknown parameters
public double[] getCoeffSd(){
return Conv.copy(bestSd);
}
// Get the estimates of the errors of the best estimates of the unknown parameters
public double[] getBestEstimatesErrors(){
return Conv.copy(bestSd);
}
// Get the unscaled initial estimates of the unknown parameters
public double[] getInitialEstimates(){
return Conv.copy(startH);
}
// Get the scaled initial estimates of the unknown parameters
public double[] getScaledInitialEstimates(){
return Conv.copy(startSH);
}
// Get the unscaled initial step sizes
public double[] getInitialSteps(){
return Conv.copy(stepH);
}
// Get the scaled initial step sizesp
public double[] getScaledInitialSteps(){
return Conv.copy(stepSH);
}
// Get the cofficients of variations of the best estimates of the unknown parameters
public double[] getCoeffVar(){
double[] coeffVar = new double[this.nParam];
for(int i=0; i testOfAdditionalTerms(double chiSquareR, int nParametersR, double chiSquareF, int nParametersF, int nPoints){
return Regression.testOfAdditionalTerms(chiSquareR, nParametersR, chiSquareF, nParametersF, nPoints, 0.05);
}
public static ArrayList testOfAdditionalTerms(double chiSquareR, int nParametersR, double chiSquareF, int nParametersF, int nPoints, double significance){
ArrayList res = Regression.testOfAdditionalTerms_ArrayList(chiSquareR, nParametersR, chiSquareF, nParametersF, nPoints);
ArrayList ret = null;
if(res!=null){
int n = ret.size();
ret = new ArrayList(n);
for(int i=0; i testOfAdditionalTerms_ArrayList(double chiSquareR, int nParametersR, double chiSquareF, int nParametersF, int nPoints){
return Regression.testOfAdditionalTerms(chiSquareR, nParametersR, chiSquareF, nParametersF, nPoints, 0.05);
}
public static ArrayList testOfAdditionalTerms_ArrayList(double chiSquareR, int nParametersR, double chiSquareF, int nParametersF, int nPoints, double significance){
return Regression.testOfAdditionalTerms(chiSquareR, nParametersR, chiSquareF, nParametersF, nPoints, significance);
}
// Test of an additional terms {extra sum of squares]
// Enter reduced model parameters first and full model parameters second
// returns as an ArrayList:
// F-ratio
// F-ratio probability
// a boolean which = false if model order retained, = true if model order reversed;
// chi-square of the reduced model
// number of parameters of the reduced model
// chi-square of the full model
// number of parameters of the full model
// F-ratio at the chosen significance level (program default value = 0.05)
//public static ArrayList testOfAdditionalTerms_ArrayList(double chiSquareR, int nParametersR, double chiSquareF, int nParametersF, int nPoints){
// return Regression.testOfAdditionalTerms_ArrayList(chiSquareR, nParametersR, chiSquareF, nParametersF, nPoints, 0.05);
//}
/*public static ArrayList testOfAdditionalTerms_ArrayList(double chiSquareR, int nParametersR, double chiSquareF, int nParametersF, int nPoints, double significance){
int degFreedomR = nPoints - nParametersR;
int degFreedomF = nPoints - nParametersF;
// Check that model 2 has the lowest degrees of freedom
boolean reversed = false;
if(degFreedomRchiSquareF){
fProb = Stat.fTestProb(fRatio, degFreedomD, degFreedomF);
}
// Return arraylist
ArrayList arrayl = new ArrayList();
arrayl.add(new Double(fRatio));
arrayl.add(new Double(fProb));
arrayl.add(new Boolean(reversed));
arrayl.add(new Double(chiSquareR));
arrayl.add(new Integer(nParametersR));
arrayl.add(new Double(chiSquareF));
arrayl.add(new Integer(nParametersF));
arrayl.add(new Integer(nPoints));
arrayl.add(new Double(Stat.fTestValueGivenFprob(significance, degFreedomD, degFreedomF)));
return arrayl;
}
*/
// Test of an additional terms {extra sum of squares]
// return F-ratio only
public static double testOfAdditionalTermsFratio(double chiSquareR, int nParametersR, double chiSquareF, int nParametersF, int nPoints){
return Regression.testOfAdditionalTermsFratio(chiSquareR, nParametersR, chiSquareF, nParametersF, nPoints, 0.05);
}
public static double testOfAdditionalTermsFratio(double chiSquareR, int nParametersR, double chiSquareF, int nParametersF, int nPoints, double significance){
int degFreedomR = nPoints - nParametersR;
int degFreedomF = nPoints - nParametersF;
// Check that model 2 has the lowest degrees of freedom
boolean reversed = false;
if(degFreedomRchiSquareF){
fProb = Stat.fTestProb(fRatio, degFreedomD, degFreedomF);
}
return fProb;
}
// name typo: kept for compatibility
public static double testOfAdditionalTermsFprobabilty(double chiSquareR, int nParametersR, double chiSquareF, int nParametersF, int nPoints){
return Regression.testOfAdditionalTermsFprobability(chiSquareR, nParametersR, chiSquareF, nParametersF, nPoints, 0.05);
}
// FIT TO SPECIAL FUNCTIONS
// Fit to a Poisson distribution
public void poisson(){
this.userSupplied = false;
this.fitPoisson(0);
}
// Fit to a Poisson distribution
public void poissonPlot(){
this.userSupplied = false;
this.fitPoisson(1);
}
protected void fitPoisson(int plotFlag){
if(this.multipleY)throw new IllegalArgumentException("This method cannot handle multiply dimensioned y arrays");
this.lastMethod = 6;
this.linNonLin = false;
this.zeroCheck = false;
this.nParam=2;
if(!this.scaleFlag)this.nParam=2;
this.degreesOfFreedom=this.nData - this.nParam;
if(this.degreesOfFreedom<1)throw new IllegalArgumentException("Degrees of freedom must be greater than 0");
// Check all abscissae are integers
for(int i=0; i ret1 = Regression.dataSign(yData);
Double tempd = null;
Integer tempi = null;
tempi = (Integer)ret1.get(5);
int peaki = tempi.intValue();
double mean = xData[0][peaki];
// Calculate peak value
tempd = (Double)ret1.get(4);
double peak = tempd.doubleValue();
// Fill arrays needed by the Simplex
double[] start = new double[this.nParam];
double[] step = new double[this.nParam];
start[0] = mean;
if(this.scaleFlag){
start[1] = peak/(Math.exp(mean*Math.log(mean)-Stat.logFactorial(mean))*Math.exp(-mean));
}
step[0] = 0.1D*start[0];
if(step[0]==0.0D){
ArrayList ret0 = Regression.dataSign(xData[0]);
Double tempdd = null;
tempdd = (Double)ret0.get(2);
double xmax = tempdd.doubleValue();
if(xmax==0.0D){
tempdd = (Double)ret0.get(0);
xmax = tempdd.doubleValue();
}
step[0]=xmax*0.1D;
}
if(this.scaleFlag)step[1] = 0.1D*start[1];
// Nelder and Mead Simplex Regression
PoissonFunction f = new PoissonFunction();
this.addConstraint(1,-1,0.0D);
f.setScaleOption(this.scaleFlag);
f.setScaleFactor(this.yScaleFactor);
Object regFun2 = (Object) f;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun2, null, start, step, this.fTol, this.nMax);
if(plotFlag==1){
// Print results
if(!this.supressPrint)this.print();
// Plot results
this.plotOpt=false;
int flag = this.plotXY(f);
if(flag!=-2 && !this.supressYYplot)this.plotYY();
}
}
// FIT TO A NORMAL (GAUSSIAN) DISTRIBUTION
// Fit to a Gaussian
public void gaussian(){
this.userSupplied = false;
this.fitGaussian(0);
}
public void normal(){
this.userSupplied = false;
this.fitGaussian(0);
}
// Fit to a Gaussian
public void gaussianPlot(){
this.userSupplied = false;
this.fitGaussian(1);
}
// Fit to a Gaussian
public void normalPlot(){
this.userSupplied = false;
this.fitGaussian(1);
}
// Fit data to a Gaussian (normal) probability function
protected void fitGaussian(int plotFlag){
if(this.multipleY)throw new IllegalArgumentException("This method cannot handle multiply dimensioned y arrays");
this.lastMethod = 4;
this.linNonLin = false;
this.zeroCheck = false;
this.nParam=3;
if(!this.scaleFlag)this.nParam=2;
this.degreesOfFreedom=this.nData - this.nParam;
if(this.degreesOfFreedom<1 && !this.ignoreDofFcheck)throw new IllegalArgumentException("Degrees of freedom must be greater than 0");
// order data into ascending order of the abscissae
Regression.sort(this.xData[0], this.yData, this.weight);
// check sign of y data
Double tempd=null;
ArrayList retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
boolean yFlag = false;
if(yPeak<0.0D){
System.out.println("Regression.fitGaussian(): This implementation of the Gaussian distribution takes only positive y values\n(noise taking low values below zero are allowed)");
System.out.println("All y values have been multiplied by -1 before fitting");
for(int i =0; i ret1 = Regression.dataSign(yData);
Integer tempi = null;
tempi = (Integer)ret1.get(5);
int peaki = tempi.intValue();
double mean = xData[0][peaki];
// Calculate an estimate of the sd
double sd = Math.sqrt(2.0D)*halfWidth(xData[0], yData);
// Calculate estimate of y scale
tempd = (Double)ret1.get(4);
double ym = tempd.doubleValue();
ym=ym*sd*Math.sqrt(2.0D*Math.PI);
// Fill arrays needed by the Simplex
double[] start = new double[this.nParam];
double[] step = new double[this.nParam];
start[0] = mean;
start[1] = sd;
if(this.scaleFlag){
start[2] = ym;
}
step[0] = 0.1D*sd;
step[1] = 0.1D*start[1];
if(step[1]==0.0D){
ArrayList ret0 = Regression.dataSign(xData[0]);
Double tempdd = null;
tempdd = (Double)ret0.get(2);
double xmax = tempdd.doubleValue();
if(xmax==0.0D){
tempdd = (Double)ret0.get(0);
xmax = tempdd.doubleValue();
}
step[1]=xmax*0.1D;
}
if(this.scaleFlag)step[2] = 0.1D*start[2];
// Nelder and Mead Simplex Regression
GaussianFunction f = new GaussianFunction();
this.addConstraint(1,-1, 0.0D);
f.setScaleOption(this.scaleFlag);
f.setScaleFactor(this.yScaleFactor);
Object regFun2 = (Object)f;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun2, null, start, step, this.fTol, this.nMax);
if(plotFlag==1){
// Print results
if(!this.supressPrint)this.print();
// Plot results
int flag = this.plotXY(f);
if(flag!=-2 && !this.supressYYplot)this.plotYY();
}
if(yFlag){
// restore data
for(int i=0; i retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
boolean yFlag = false;
if(yPeak<0.0D){
System.out.println("Regression.fitGaussian(): This implementation of the Gaussian distribution takes only positive y values\n(noise taking low values below zero are allowed)");
System.out.println("All y values have been multiplied by -1 before fitting");
for(int i =0; i retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
boolean yFlag = false;
if(yPeak<0.0D){
System.out.println("Regression.fitGaussian(): This implementation of the Gaussian distribution takes only positive y values\n(noise taking low values below zero are allowed)");
System.out.println("All y values have been multiplied by -1 before fitting");
for(int i =0; i ret1 = Regression.dataSign(yData);
Integer tempi = null;
tempi = (Integer)ret1.get(5);
int peaki = tempi.intValue();
double mean = xData[0][peaki];
// Calculate an estimate of the sd
double sd = Math.sqrt(2.0D)*halfWidth(xData[0], yData);
// Calculate estimate of y scale
tempd = (Double)ret1.get(4);
double ym = tempd.doubleValue();
// Fill arrays needed by the Simplex
double[] start = new double[this.nParam];
double[] step = new double[this.nParam];
int counter = 0;
for(int i=0; i ret0 = Regression.dataSign(xData[0]);
Double tempdd = null;
tempdd = (Double)ret0.get(2);
double xmax = tempdd.doubleValue();
if(xmax==0.0D){
tempdd = (Double)ret0.get(0);
xmax = tempdd.doubleValue();
}
step[counter+1]=Math.abs(xmax*0.1D);
}
start[counter+2] = initFracts[i]*Math.sqrt(2.0*Math.PI)*start[counter+1]*ym;
step[counter+2] = Math.abs(0.1D*start[counter+2]);
counter += 3;
}
// Nelder and Mead Simplex Regression
MultipleGaussianFunction f = new MultipleGaussianFunction();
f.setScaleOption(this.scaleFlag);
double ysf = this.yScaleFactor;
if(!this.scaleFlag)ysf = 1.0;
f.setScaleFactor(ysf);
f.setNgaussians(this.nGaussians);
// Add constraints
for(int i=0; i retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
boolean yFlag = false;
if(yPeak<0.0D){
System.out.println("Regression.fitLogNormalTwoPar(): This implementation of the two parameter log-nprmal distribution takes only positive y values\n(noise taking low values below zero are allowed)");
System.out.println("All y values have been multiplied by -1 before fitting");
for(int i =0; i ret1 = Regression.dataSign(yData);
Integer tempi = null;
tempi = (Integer)ret1.get(5);
int peaki = tempi.intValue();
double mean = xData[0][peaki];
// Calculate an estimate of the mu
double mu = 0.0D;
for(int i=0; i ret0 = Regression.dataSign(xData[0]);
Double tempdd = null;
tempdd = (Double)ret0.get(2);
double xmax = tempdd.doubleValue();
if(xmax==0.0D){
tempdd = (Double)ret0.get(0);
xmax = tempdd.doubleValue();
}
step[0]=xmax*0.1D;
}
if(step[0]==0.0D){
ArrayList ret0 = Regression.dataSign(xData[0]);
Double tempdd = null;
tempdd = (Double)ret0.get(2);
double xmax = tempdd.doubleValue();
if(xmax==0.0D){
tempdd = (Double)ret0.get(0);
xmax = tempdd.doubleValue();
}
step[1]=xmax*0.1D;
}
if(this.scaleFlag)step[2] = 0.1D*start[2];
// Nelder and Mead Simplex Regression
LogNormalTwoParFunction f = new LogNormalTwoParFunction();
this.addConstraint(1,-1,0.0D);
f.setScaleOption(this.scaleFlag);
f.setScaleFactor(this.yScaleFactor);
Object regFun2 = (Object)f;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun2, null, start, step, this.fTol, this.nMax);
if(plotFlag==1){
// Print results
if(!this.supressPrint)this.print();
// Plot results
int flag = this.plotXY(f);
if(flag!=-2 && !this.supressYYplot)this.plotYY();
}
if(yFlag){
// restore data
for(int i=0; i retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
boolean yFlag = false;
if(yPeak<0.0D){
System.out.println("Regression.fitLogNormalThreePar(): This implementation of the three parameter log-normal distribution takes only positive y values\n(noise taking low values below zero are allowed)");
System.out.println("All y values have been multiplied by -1 before fitting");
for(int i =0; i ret1 = Regression.dataSign(yData);
Integer tempi = null;
tempi = (Integer)ret1.get(5);
int peaki = tempi.intValue();
double mean = xData[0][peaki];
// Calculate an estimate of the gamma
double gamma = 0.0D;
for(int i=0; i ret0 = Regression.dataSign(xData[0]);
Double tempdd = null;
tempdd = (Double)ret0.get(0);
double xmin = tempdd.doubleValue();
tempdd = (Double)ret0.get(2);
double xmax = tempdd.doubleValue();
double alpha = xmin - (xmax - xmin)/100.0D;;
if(xmin==0.0D)alpha -= (xmax - xmin)/100.0D;
// Calculate estimate of y scale
tempd = (Double)ret1.get(4);
double ym = tempd.doubleValue();
ym=ym*(gamma+alpha)*Math.exp(- beta*beta/2);
// Fill arrays needed by the Simplex
double[] start = new double[this.nParam];
double[] step = new double[this.nParam];
start[0] = alpha;
start[1] = beta;
start[2] = gamma;
if(this.scaleFlag){
start[3] = ym;
}
step[0] = 0.1D*start[0];
step[1] = 0.1D*start[1];
step[2] = 0.1D*start[2];
for(int i=0; i<3; i++){
if(step[i]==0.0D)step[i]=xmax*0.1D;
}
if(this.scaleFlag)step[3] = 0.1D*start[3];
// Nelder and Mead Simplex Regression
LogNormalThreeParFunction f = new LogNormalThreeParFunction();
this.addConstraint(0,+1,xmin);
this.addConstraint(1,-1,0.0D);
this.addConstraint(2,-1,0.0D);
f.setScaleOption(this.scaleFlag);
f.setScaleFactor(this.yScaleFactor);
Object regFun2 = (Object)f;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun2, null, start, step, this.fTol, this.nMax);
if(plotFlag==1){
// Print results
if(!this.supressPrint)this.print();
// Plot results
int flag = this.plotXY(f);
if(flag!=-2 && !this.supressYYplot)this.plotYY();
}
if(yFlag){
// restore data
for(int i=0; i retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
boolean yFlag = false;
if(yPeak<0.0D){
System.out.println("Regression.fitLorentzian(): This implementation of the Lorentzian distribution takes only positive y values\n(noise taking low values below zero are allowed)");
System.out.println("All y values have been multiplied by -1 before fitting");
for(int i =0; i ret0 = Regression.dataSign(xData[0]);
Double tempdd = null;
tempdd = (Double)ret0.get(2);
double xmax = tempdd.doubleValue();
if(xmax==0.0D){
tempdd = (Double)ret0.get(0);
xmax = tempdd.doubleValue();
}
step[0]=xmax*0.1D;
}
step[1] = 0.2D*start[1];
if(this.scaleFlag)step[2] = 0.2D*start[2];
// Nelder and Mead Simplex Regression
LorentzianFunction f = new LorentzianFunction();
this.addConstraint(1,-1,0.0D);
f.setScaleOption(this.scaleFlag);
f.setScaleFactor(this.yScaleFactor);
Object regFun2 = (Object)f;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun2, null, start, step, this.fTol, this.nMax);
if(allTest==1){
// Print results
if(!this.supressPrint)this.print();
// Plot results
int flag = this.plotXY(f);
if(flag!=-2 && !this.supressYYplot)this.plotYY();
}
if(yFlag){
// restore data
for(int i=0; i dataSign(double[] data){
ArrayList ret = new ArrayList();
int n = data.length;
double max=data[0]; // maximum
int maxi=0; // index of above
double min=data[0]; // minimum
int mini=0; // index of above
double peak=0.0D; // peak: larger of maximum and any abs(negative minimum)
int peaki=-1; // index of above
int signFlag=-1; // 0 all positive; 1 all negative; 2 positive and negative
double shift=0.0D; // shift to make all positive if a mixture of positive and negative
double mean = 0.0D; // mean value
int signCheckZero=0; // number of zero values
int signCheckNeg=0; // number of positive values
int signCheckPos=0; // number of negative values
for(int i=0; imax){
max=data[i];
maxi=i;
}
if(data[i]0.0D)signCheckPos++;
if(data[i]<0.0D)signCheckNeg++;
}
mean /= (double)n;
if((signCheckZero+signCheckPos)==n){
peak=max;
peaki=maxi;
signFlag=0;
}
else{
if((signCheckZero+signCheckNeg)==n){
peak=min;
peaki=mini;
signFlag=1;
}
else{
peak=max;
peaki=maxi;
if(-min>max){
peak=min;
peak=mini;
}
signFlag=2;
shift=-min;
}
}
// transfer results to the ArrayList
ret.add(new Double(min));
ret.add(new Integer(mini));
ret.add(new Double(max));
ret.add(new Integer(maxi));
ret.add(new Double(peak));
ret.add(new Integer(peaki));
ret.add(new Integer(signFlag));
ret.add(new Double(shift));
ret.add(new Double(mean));
ret.add(new Integer(signCheckZero));
ret.add(new Integer(signCheckPos));
ret.add(new Integer(signCheckNeg));
return ret;
}
public void frechet(){
this.fitFrechet(0, 0);
}
public void frechetPlot(){
this.fitFrechet(1, 0);
}
public void frechetTwoPar(){
this.fitFrechet(0, 1);
}
public void frechetTwoParPlot(){
this.fitFrechet(1, 1);
}
public void frechetStandard(){
this.fitFrechet(0, 2);
}
public void frechetStandardPlot(){
this.fitFrechet(1, 2);
}
protected void fitFrechet(int allTest, int typeFlag){
if(this.multipleY)throw new IllegalArgumentException("This method cannot handle multiply dimensioned y arrays");
this.userSupplied = false;
switch(typeFlag){
case 0: this.lastMethod = 13;
this.nParam=4;
break;
case 1: this.lastMethod = 14;
this.nParam=3;
break;
case 2: this.lastMethod = 15;
this.nParam=2;
break;
}
if(!this.scaleFlag)this.nParam=this.nParam-1;
this.frechetWeibull=true;
this.fitFrechetWeibull(allTest, typeFlag);
}
// method for fitting data to either a Frechet or a Weibull distribution
protected void fitFrechetWeibull(int allTest, int typeFlag){
if(this.multipleY)throw new IllegalArgumentException("This method cannot handle multiply dimensioned y arrays");
this.linNonLin = false;
this.zeroCheck = false;
this.degreesOfFreedom=this.nData - this.nParam;
if(this.degreesOfFreedom<1 && !this.ignoreDofFcheck)throw new IllegalArgumentException("Degrees of freedom must be greater than 0");
// order data into ascending order of the abscissae
Regression.sort(this.xData[0], this.yData, this.weight);
// check y data
Double tempd=null;
ArrayList retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
Integer tempi = null;
tempi = (Integer)retY.get(5);
int peaki = tempi.intValue();
tempd = (Double)retY.get(8);
double mean = tempd.doubleValue();
// check for infinity
boolean testInf = true;
double dof = this.degreesOfFreedom;
while(testInf){
if(this.infinityCheck(yPeak, peaki)){
dof--;
if(dof<1 && !this.ignoreDofFcheck)throw new IllegalArgumentException("The effective degrees of freedom have been reduced to zero");
retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
yPeak = tempd.doubleValue();
tempi = (Integer)retY.get(5);
peaki = tempi.intValue();
tempd = (Double)retY.get(8);
mean = tempd.doubleValue();
}
else{
testInf = false;
}
}
// check sign of y data
String ss = "Weibull";
if(this.frechetWeibull)ss = "Frechet";
boolean ySignFlag = false;
if(yPeak<0.0D){
this.reverseYsign(ss);
retY = Regression.dataSign(this.yData);
yPeak = -yPeak;
ySignFlag = true;
}
// check y values for all very small values
boolean magCheck=false;
double magScale = this.checkYallSmall(yPeak, ss);
if(magScale!=1.0D){
magCheck=true;
yPeak=1.0D;
}
// minimum value of x
ArrayList retX = Regression.dataSign(this.xData[0]);
tempd = (Double)retX.get(0);
double xMin = tempd.doubleValue();
// maximum value of x
tempd = (Double)retX.get(2);
double xMax = tempd.doubleValue();
// Calculate x value at peak y (estimate of the 'distribution mode')
double distribMode = xData[0][peaki];
// Calculate an estimate of the half-height width
double sd = Math.log(2.0D)*halfWidth(xData[0], yData);
// Save x-y-w data
double[] xx = new double[this.nData];
double[] yy = new double[this.nData];
double[] ww = new double[this.nData];
for(int i=0; i ret0 = Regression.dataSign(xData[0]);
Double tempdd = null;
tempdd = (Double)ret0.get(2);
double xmax = tempdd.doubleValue();
if(xmax==0.0D){
tempdd = (Double)ret0.get(0);
xmax = tempdd.doubleValue();
}
step[0]=xmax*0.1D;
}
step[1] = 0.2D*start[1];
step[2] = 0.5D*start[2];
this.addConstraint(0,+1,xMin);
this.addConstraint(1,-1,0.0D);
this.addConstraint(2,-1,0.0D);
break;
case 1: start[0] = sd; //sigma
start[1] = 4.0; // gamma
// step sizes
step[0] = 0.2D*start[0];
step[1] = 0.5D*start[1];
this.addConstraint(0,-1,0.0D);
this.addConstraint(1,-1,0.0D);
break;
case 2: start[0] = 4.0; // gamma
// step size
step[0] = 0.5D*start[0];
this.addConstraint(0,-1,0.0D);
break;
}
// Create instance of loglog function and perform regression
if(this.frechetWeibull){
FrechetFunctionTwo f = new FrechetFunctionTwo();
f.setTypeFlag(typeFlag);
Object regFun2 = (Object)f;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun2, null, start, step, this.fTol, this.nMax);
}
else{
WeibullFunctionTwo f = new WeibullFunctionTwo();
f.setTypeFlag(typeFlag);
Object regFun2 = (Object)f;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun2, null, start, step, this.fTol, this.nMax);
}
// Get best estimates of loglog regression
double[] ests = Conv.copy(this.best);
// Nelder and Mead Simplex Regression for Frechet or Weibull
// using best estimates from loglog regression as initial estimates
// re-enable statistical analysis if statFlag was set to true
this.statFlag = statFlagHold;
// restore data reversing the loglog transform but maintaining any sign reversals
this.weightOpt=weightOptHold;
for(int i =0; i ret0 = Regression.dataSign(xData[0]);
Double tempdd = null;
tempdd = (Double)ret0.get(2);
double xmax = tempdd.doubleValue();
if(xmax==0.0D){
tempdd = (Double)ret0.get(0);
xmax = tempdd.doubleValue();
}
step[0]=xmax*0.1D;
}
step[1] = 0.1D*start[1];
step[2] = 0.1D*start[2];
if(this.scaleFlag){
step[3] = 0.1D*start[3];
}
break;
case 1: start[0] = ests[0]; //sigma
start[1] = ests[1]; //gamma
if(this.scaleFlag){
start[2] = 1.0/yScale; //y axis scaling factor
}
step[0] = 0.1D*start[0];
step[1] = 0.1D*start[1];
if(this.scaleFlag)step[2] = 0.1D*start[2];
break;
case 2: start[0] = ests[0]; //gamma
if(this.scaleFlag){
start[1] = 1.0/yScale; //y axis scaling factor
}
step[0] = 0.1D*start[0];
if(this.scaleFlag)step[1] = 0.1D*start[1];
break;
}
// Create instance of Frechet function and perform regression
if(this.frechetWeibull){
FrechetFunctionOne ff = new FrechetFunctionOne();
ff.setScaleOption(this.scaleFlag);
ff.setScaleFactor(this.yScaleFactor);
ff.setTypeFlag(typeFlag);
Object regFun3 = (Object)ff;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun3, null, start, step, this.fTol, this.nMax);
if(allTest==1){
// Print results
if(!this.supressPrint)this.print();
// Plot results
int flag = this.plotXY(ff);
if(flag!=-2 && !this.supressYYplot)this.plotYY();
}
}
else{
WeibullFunctionOne ff = new WeibullFunctionOne();
ff.setScaleOption(this.scaleFlag);
ff.setScaleFactor(this.yScaleFactor);
ff.setTypeFlag(typeFlag);
Object regFun3 = (Object)ff;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun3, null, start, step, this.fTol, this.nMax);
if(allTest==1){
// Print results
if(!this.supressPrint)this.print();
// Plot results
int flag = this.plotXY(ff);
if(flag!=-2 && !this.supressYYplot)this.plotYY();
}
}
// restore data
this.weightOpt = weightOptHold;
if(magCheck){
for(int i =0; idistribMode*0.02D){
ErrorProp x0 = new ErrorProp(0.0D, 0.0D);
x0 = yE[0].times(this.xData[0][1]-this.xData[0][0]);
x0 = x0.over(yE[1].minus(yE[0]));
x0 = ErrorProp.minus(this.xData[0][0],x0);
if(this.yData[0]>=0.9D*yPeak)x0=(x0.plus(this.xData[0][0])).over(2.0D);
if(x0.getValue()<0.0D)x0.reset(0.0D, 0.0D);
cumYe[0] = yE[0].over(2.0D);
cumYe[0] = cumYe[0].times(ErrorProp.minus(this.xData[0][0], x0));
}
else{
cumYe[0].reset(0.0D, this.weight[0]);
}
}
else{
cumYe[0].reset(0.0D, this.weight[0]);
}
// cumulative calculation for rest of the points (trapezium approximation)
for(int i=1; idistribMode*0.02D){
ErrorProp xn = new ErrorProp();
xn = yE[this.nData-1].times(this.xData[0][this.nData-2]-this.xData[0][this.nData-1]);
xn = xn.over(yE[this.nData-2].minus(yE[this.nData-1]));
xn = ErrorProp.minus(this.xData[0][this.nData-1], xn);
if(this.yData[0]>=0.9D*yPeak)xn=(xn.plus(this.xData[0][this.nData-1])).over(2.0D);
cumYtotal = cumYtotal.plus(ErrorProp.times(0.5D,(yE[this.nData-1].times(xn.minus(this.xData[0][this.nData-1])))));
}
}
// Fill cumulative Y and W arrays
for(int i=0; i=this.nData)throw new ArithmeticException("all zero cumulative data!!");
if(cumYe[jj].getValue()>0.0D){
cumYe[i]=cumYe[jj].copy();
cumX[i]=cumX[jj];
test=false;
}
}
}
else{
if(i==this.nData-1){
cumYe[i]=cumYe[i-1].copy();
cumX[i]=cumX[i-1];
}
else{
cumYe[i]=cumYe[i-1].plus(cumYe[i+1]);
cumYe[i]=cumYe[i].over(2.0D);
cumX[i]=(cumX[i-1]+cumX[i+1])/2.0D;
}
}
}
}
// check for unity value
jj = this.nData-1;
for(int i=this.nData-1; i>=0; i--){
if(cumYe[i].getValue()>=1.0D){
if(i>=jj){
test=true;
jj = this.nData-1;
while(test){
jj--;
if(jj<0)throw new ArithmeticException("all unity cumulative data!!");
if(cumYe[jj].getValue()<1.0D){
cumYe[i]=cumYe[jj].copy();
cumX[i]=cumX[jj];
test=false;
}
}
}
else{
if(i==0){
cumYe[i]=cumYe[i+1].copy();
cumX[i]=cumX[i+1];
}
else{
cumYe[i]=cumYe[i-1].plus(cumYe[i+1]);
cumYe[i]=cumYe[i].over(2.0D);
cumX[i]=(cumX[i-1]+cumX[i+1])/2.0D;
}
}
}
}
return yScale;
}
public void weibull(){
this.fitWeibull(0, 0);
}
public void weibullPlot(){
this.fitWeibull(1, 0);
}
public void weibullTwoPar(){
this.fitWeibull(0, 1);
}
public void weibullTwoParPlot(){
this.fitWeibull(1, 1);
}
public void weibullStandard(){
this.fitWeibull(0, 2);
}
public void weibullStandardPlot(){
this.fitWeibull(1, 2);
}
protected void fitWeibull(int allTest, int typeFlag){
if(this.multipleY)throw new IllegalArgumentException("This method cannot handle multiply dimensioned y arrays");
this.userSupplied = false;
switch(typeFlag){
case 0: this.lastMethod = 16;
this.nParam=4;
break;
case 1: this.lastMethod = 17;
this.nParam=3;
break;
case 2: this.lastMethod = 18;
this.nParam=2;
break;
}
if(!this.scaleFlag)this.nParam=this.nParam-1;
this.frechetWeibull=false;
this.fitFrechetWeibull(allTest, typeFlag);
}
public void gumbelMin(){
this.fitGumbel(0, 0);
}
public void gumbelMinPlot(){
this.fitGumbel(1, 0);
}
public void gumbelMax(){
this.fitGumbel(0, 1);
}
public void gumbelMaxPlot(){
this.fitGumbel(1, 1);
}
public void gumbelMinOnePar(){
this.fitGumbel(0, 2);
}
public void gumbelMinOneParPlot(){
this.fitGumbel(1, 2);
}
public void gumbelMaxOnePar(){
this.fitGumbel(0, 3);
}
public void gumbelMaxOneParPlot(){
this.fitGumbel(1, 3);
}
public void gumbelMinStandard(){
this.fitGumbel(0, 4);
}
public void gumbelMinStandardPlot(){
this.fitGumbel(1, 4);
}
public void gumbelMaxStandard(){
this.fitGumbel(0, 5);
}
public void gumbelMaxStandardPlot(){
this.fitGumbel(1, 5);
}
// No parameters set for estimation
// Correlation coefficient and plot
protected void noParameters(String ss){
System.out.println(ss+" Regression");
System.out.println("No parameters set for estimation");
System.out.println("Theoretical curve obtained");
String filename1="RegressOutput.txt";
String filename2="RegressOutputN.txt";
FileOutput fout = new FileOutput(filename1, 'n');
System.out.println("Results printed to the file "+filename2);
fout.dateAndTimeln(filename1);
fout.println("No parameters set for estimation");
switch(this.lastMethod){
case 11: fout.println("Minimal Standard Gumbel p(x) = exp(x)exp(-exp(x))");
for(int i=0; i retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
boolean yFlag = false;
if(yPeak<0.0D){
System.out.println("Regression.fitGumbel(): This implementation of the Gumbel distribution takes only positive y values\n(noise taking low values below zero are allowed)");
System.out.println("All y values have been multiplied by -1 before fitting");
for(int i =0; i retX = Regression.dataSign(xData[0]);
Integer tempi = null;
// Calculate x value at peak y (estimate of the 'distribution mode')
tempi = (Integer)retY.get(5);
int peaki = tempi.intValue();
double distribMode = xData[0][peaki];
// Calculate an estimate of the half-height width
double sd = halfWidth(xData[0], yData);
// Nelder and Mead Simplex Regression for Gumbel
// Fill arrays needed by the Simplex
double[] start = new double[this.nParam];
double[] step = new double[this.nParam];
switch(typeFlag){
case 0:
case 1:
start[0] = distribMode; //mu
start[1] = sd*Math.sqrt(6.0D)/Math.PI; //sigma
if(this.scaleFlag){
start[2] = yPeak*start[1]*Math.exp(1); //y axis scaling factor
}
step[0] = 0.1D*start[0];
if(step[0]==0.0D){
ArrayList ret0 = Regression.dataSign(xData[0]);
Double tempdd = null;
tempdd = (Double)ret0.get(2);
double xmax = tempdd.doubleValue();
if(xmax==0.0D){
tempdd = (Double)ret0.get(0);
xmax = tempdd.doubleValue();
}
step[0]=xmax*0.1D;
}
step[1] = 0.1D*start[1];
if(this.scaleFlag)step[2] = 0.1D*start[2];
// Add constraints
this.addConstraint(1,-1,0.0D);
break;
case 2:
case 3:
start[0] = sd*Math.sqrt(6.0D)/Math.PI; //sigma
if(this.scaleFlag){
start[1] = yPeak*start[0]*Math.exp(1); //y axis scaling factor
}
step[0] = 0.1D*start[0];
if(this.scaleFlag)step[1] = 0.1D*start[1];
// Add constraints
this.addConstraint(0,-1,0.0D);
break;
case 4:
case 5:
if(this.scaleFlag){
start[0] = yPeak*Math.exp(1); //y axis scaling factor
step[0] = 0.1D*start[0];
}
break;
}
// Create instance of Gumbel function
GumbelFunction ff = new GumbelFunction();
// Set minimum type / maximum type option
ff.setTypeFlag(typeFlag);
// Set ordinate scaling option
ff.setScaleOption(this.scaleFlag);
ff.setScaleFactor(this.yScaleFactor);
if(typeFlag<4){
// Perform simplex regression
Object regFun3 = (Object)ff;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun3, null, start, step, this.fTol, this.nMax);
if(allTest==1){
// Print results
if(!this.supressPrint)this.print();
// Plot results
int flag = this.plotXY(ff);
if(flag!=-2 && !this.supressYYplot)this.plotYY();
}
}
else{
// calculate exp exp term
double[][] xxx = new double[1][this.nData];
double aa=1.0D;
if(typeFlag==5)aa=-1.0D;
for(int i=0; iymax){
ymax=yData[i];
imax=i;
}
}
ymax /= 2.0D;
// Find index of point at half maximum value on the low side of the maximum
double halfXlow=-1.0D;
double halfYlow=-1.0D;
double temp = -1.0D;
int ihl=-1;
if(imax>0){
ihl=imax-1;
halfYlow=Math.abs(ymax-yData[ihl]);
for(int i=imax-2; i>=0; i--){
temp=Math.abs(ymax-yData[i]);
if(tempMath.abs(maxY)){
testDirection = -1.0;
maxY = minY;
maxYhalf = minY/2.0;
}
double timeHalf = Double.NaN;
boolean test = true;
int ii=0;
while(test){
if(this.yData[ii]==maxYhalf){
timeHalf = this.xData[0][ii] - this.xData[0][0];
test = false;
}
else{
if(this.yData[ii]maxYhalf){
timeHalf = (this.xData[0][ii] + this.xData[0][ii+1])/2.0 - this.xData[0][0];
test = false;
}
else{
if(this.yData[ii]>maxYhalf && this.yData[ii+1]=this.nData-1)test = false;
}
}
}
}
if(timeHalf!=timeHalf){
timeHalf = am.maximumDifference();
}
double guessB = -testDirection/timeHalf;
double[] start = {maxY, guessB};
double[] step = {Math.abs(start[0]/5.0), Math.abs(start[1]/5.0)};
// Nelder and Mead Simplex Regression
OneMinusExponentialFunction f = new OneMinusExponentialFunction();
f.setScaleOption(this.scaleFlag);
f.setScaleFactor(this.yScaleFactor);
Object regFun2 = (Object)f;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun2, null, start, step, this.fTol, this.nMax);
double ss0 = this.sumOfSquaresError;
double[] bestEstimates0 = this.best;
// Repeat with A and B guess of opposite sign
start[0] = -maxY;
start[1] = -guessB;
step[0] = Math.abs(start[0]/5.0);
step[1] = Math.abs(start[1]/5.0);
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun2, null, start, step, this.fTol, this.nMax);
// Choose better result
if(this.sumOfSquaresError>ss0){
start[0] = bestEstimates0[0];
start[1] = bestEstimates0[1];
step[0] = Math.abs(start[0]/20.0);
step[1] = Math.abs(start[1]/20.0);
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun2, null, start, step, this.fTol, this.nMax);
}
// Plotting
if(plotFlag==1){
// Print results
if(!this.supressPrint)this.print();
// Plot results
int flag = this.plotXY(f);
if(flag!=-2 && !this.supressYYplot)this.plotYY();
}
}
// FIT TO AN EXPOPNENTIAL DISTRIBUTION
public void exponential(){
this.fitExponential(0, 0);
}
public void exponentialPlot(){
this.fitExponential(1, 0);
}
public void exponentialOnePar(){
this.fitExponential(0, 1);
}
public void exponentialOneParPlot(){
this.fitExponential(1, 1);
}
public void exponentialStandard(){
this.fitExponential(0, 2);
}
public void exponentialStandardPlot(){
this.fitExponential(1, 2);
}
protected void fitExponential(int allTest, int typeFlag){
if(this.multipleY)throw new IllegalArgumentException("This method cannot handle multiply dimensioned y arrays");
this.userSupplied = false;
switch(typeFlag){
case 0: this.lastMethod = 19;
this.nParam=3;
break;
case 1: this.lastMethod = 20;
this.nParam=2;
break;
case 2: this.lastMethod = 21;
this.nParam=1;
break;
}
if(!this.scaleFlag)this.nParam=this.nParam-1;
this.linNonLin = false;
this.zeroCheck = false;
this.degreesOfFreedom=this.nData - this.nParam;
if(this.degreesOfFreedom<1 && !this.ignoreDofFcheck)throw new IllegalArgumentException("Degrees of freedom must be greater than 0");
if(this.nParam==0){
this.noParameters("Exponential");
}
else{
// Save x-y-w data
double[] xx = new double[this.nData];
double[] yy = new double[this.nData];
double[] ww = new double[this.nData];
for(int i=0; i retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
Integer tempi = null;
tempi = (Integer)retY.get(5);
int peaki = tempi.intValue();
// check sign of y data
String ss = "Exponential";
boolean ySignFlag = false;
if(yPeak<0.0D){
this.reverseYsign(ss);
retY = Regression.dataSign(this.yData);
yPeak = -yPeak;
ySignFlag = true;
}
// check y values for all very small values
boolean magCheck=false;
double magScale = this.checkYallSmall(yPeak, ss);
if(magScale!=1.0D){
magCheck=true;
yPeak=1.0D;
}
// minimum value of x
ArrayList retX = Regression.dataSign(this.xData[0]);
tempd = (Double)retX.get(0);
double xMin = tempd.doubleValue();
// estimate of sigma
double yE = yPeak/Math.exp(1.0D);
if(this.yData[0] ret0 = Regression.dataSign(xData[0]);
Double tempdd = null;
tempdd = (Double)ret0.get(2);
double xmax = tempdd.doubleValue();
if(xmax==0.0D){
tempdd = (Double)ret0.get(0);
xmax = tempdd.doubleValue();
}
step[0]=xmax*0.1D;
}
step[1] = 0.1D*start[1];
if(this.scaleFlag)step[2] = 0.1D*start[2];
break;
case 1: start[0] = sigma; //sigma
if(this.scaleFlag){
start[1] = yPeak*sigma; //y axis scaling factor
}
step[0] = 0.1D*start[0];
if(this.scaleFlag)step[1] = 0.1D*start[1];
break;
case 2: if(this.scaleFlag){
start[0] = yPeak; //y axis scaling factor
step[0] = 0.1D*start[0];
}
break;
}
// Create instance of Exponential function and perform regression
ExponentialFunction ff = new ExponentialFunction();
ff.setScaleOption(this.scaleFlag);
ff.setScaleFactor(this.yScaleFactor);
ff.setTypeFlag(typeFlag);
Object regFun3 = (Object)ff;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun3, null, start, step, this.fTol, this.nMax);
if(allTest==1){
// Print results
if(!this.supressPrint)this.print();
// Plot results
int flag = this.plotXY(ff);
if(flag!=-2 && !this.supressYYplot)this.plotYY();
}
// restore data
if(magCheck){
for(int i =0; i=this.nData)throw new ArithmeticException("all zero cumulative data!!");
if(yy[jj]>0.0D){
yy[i]=yy[jj];
xx[i]=xx[jj];
ww[i]=ww[jj];
test=false;
}
}
}
else{
if(i==this.nData-1){
yy[i]=yy[i-1];
xx[i]=xx[i-1];
ww[i]=ww[i-1];
}
else{
yy[i]=(yy[i-1] + yy[i+1])/2.0D;
xx[i]=(xx[i-1] + xx[i+1])/2.0D;
ww[i]=(ww[i-1] + ww[i+1])/2.0D;
}
}
}
}
}
public void rayleigh(){
this.fitRayleigh(0, 0);
}
public void rayleighPlot(){
this.fitRayleigh(1, 0);
}
protected void fitRayleigh(int allTest, int typeFlag){
if(this.multipleY)throw new IllegalArgumentException("This method cannot handle multiply dimensioned y arrays");
this.lastMethod = 22;
this.userSupplied = false;
this.nParam=2;
if(!this.scaleFlag)this.nParam=this.nParam-1;
this.linNonLin = false;
this.zeroCheck = false;
this.degreesOfFreedom=this.nData - this.nParam;
if(this.degreesOfFreedom<1 && !this.ignoreDofFcheck)throw new IllegalArgumentException("Degrees of freedom must be greater than 0");
// order data into ascending order of the abscissae
Regression.sort(this.xData[0], this.yData, this.weight);
// check y data
Double tempd=null;
ArrayList retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
Integer tempi = null;
tempi = (Integer)retY.get(5);
int peaki = tempi.intValue();
// check sign of y data
String ss = "Rayleigh";
boolean ySignFlag = false;
if(yPeak<0.0D){
this.reverseYsign(ss);
retY = Regression.dataSign(this.yData);
yPeak = -yPeak;
ySignFlag = true;
}
// check y values for all very small values
boolean magCheck=false;
double magScale = this.checkYallSmall(yPeak, ss);
if(magScale!=1.0D){
magCheck=true;
yPeak=1.0D;
}
// Save x-y-w data
double[] xx = new double[this.nData];
double[] yy = new double[this.nData];
double[] ww = new double[this.nData];
for(int i=0; i retX = Regression.dataSign(this.xData[0]);
tempd = (Double)retX.get(0);
double xMin = tempd.doubleValue();
// maximum value of x
tempd = (Double)retX.get(2);
double xMax = tempd.doubleValue();
// Calculate x value at peak y (estimate of the 'distribution mode')
double distribMode = xData[0][peaki];
// Calculate an estimate of the half-height width
double sd = Math.log(2.0D)*halfWidth(xData[0], yData);
// Calculate the cumulative probability and return ordinate scaling factor estimate
double[] cumX = new double[this.nData];
double[] cumY = new double[this.nData];
double[] cumW = new double[this.nData];
ErrorProp[] cumYe = ErrorProp.oneDarray(this.nData);
double yScale = this.calculateCumulativeValues(cumX, cumY, cumW, cumYe, peaki, yPeak, distribMode, ss);
//Calculate log transform
for(int i=0; i retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
Integer tempi = null;
tempi = (Integer)retY.get(5);
int peaki = tempi.intValue();
// check for infinity
if(this.infinityCheck(yPeak, peaki)){
retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
yPeak = tempd.doubleValue();
tempi = null;
tempi = (Integer)retY.get(5);
peaki = tempi.intValue();
}
// check sign of y data
boolean ySignFlag = false;
if(yPeak<0.0D){
this.reverseYsign(ss);
retY = Regression.dataSign(this.yData);
yPeak = -yPeak;
ySignFlag = true;
}
// check y values for all very small values
boolean magCheck=false;
double magScale = this.checkYallSmall(yPeak, ss);
if(magScale!=1.0D){
magCheck=true;
yPeak=1.0D;
}
// minimum value of x
ArrayList retX = Regression.dataSign(this.xData[0]);
tempd = (Double)retX.get(0);
double xMin = tempd.doubleValue();
// maximum value of x
tempd = (Double)retX.get(2);
double xMax = tempd.doubleValue();
// Calculate x value at peak y (estimate of the 'distribution mode')
double distribMode = xData[0][peaki];
// Calculate an estimate of the half-height width
double sd = Math.log(2.0D)*halfWidth(xData[0], yData);
// Save x-y-w data
double[] xx = new double[this.nData];
double[] yy = new double[this.nData];
double[] ww = new double[this.nData];
for(int i=0; i=dirFlag*yyymid){
yyxmidl = xData[0][ii];
test = false;
}
else{
ii++;
if(ii>=nLen){
yyxmidl = Stat.mean(this.xData[0]);
ii=nLen-1;
test = false;
}
}
}
double yyxmidh = xData[0][nLen-1];
int jj = nLen-1;
test = true;
while(test){
if(this.yData[jj]<=dirFlag*yyymid){
yyxmidh = xData[0][jj];
test = false;
}
else{
jj--;
if(jj<0){
yyxmidh = Stat.mean(this.xData[0]);
jj=1;
test = false;
}
}
}
int thetaPos = (ii+jj)/2;
double theta0 = xData[0][thetaPos];
// estimate of slope
double thetaSlope1 = 2.0D*(yData[nLen-1] - theta0)/(xData[0][nLen-1] - xData[0][thetaPos]);
double thetaSlope2 = 2.0D*theta0/(xData[0][thetaPos] - xData[0][nLen-1]);
double thetaSlope = Math.max(thetaSlope1, thetaSlope2);
// initial estimates
double[] start = new double[nParam];
start[0] = 4.0D*thetaSlope;
if(dirFlag==1){
start[0] /= yymax;
}
else{
start[0] /= yymin;
}
start[1] = theta0;
if(this.scaleFlag){
if(dirFlag==1){
start[2] = yymax;
}
else{
start[2] = yymin;
}
}
// initial step sizes
double[] step = new double[nParam];
for(int i=0; ithis.yData[this.nData-1]){
yReverseFlag = -1;
for(int i=0; i=nLen)throw new IllegalArgumentException("This should not be possible - check coding");
}
}
test = true;
ii = 0;
while(test){
if(this.topS==this.yData[ii]){
this.topSindex = ii;
test = false;
}
else{
ii++;
if(ii>=nLen)throw new IllegalArgumentException("This should not be possible - check coding");
}
}
this.directionFlag = 1;
if(this.topSindex=yyymid){
yyxmidl = this.xData[0][ii];
test = false;
}
else{
ii++;
if(ii>=nLen){
yyxmidl = Stat.mean(this.xData[0]);
ii=nLen-1;
test = false;
}
}
}
double yyxmidh = this.xData[0][nLen-1];
int jj = nLen-1;
test = true;
while(test){
if(this.yData[jj]<=yyymid){
yyxmidh = this.xData[0][jj];
test = false;
}
else{
jj--;
if(jj<0){
yyxmidh = Stat.mean(this.xData[0]);
jj=1;
test = false;
}
}
}
if(ii=nLen){
yyxmidl = Stat.mean(this.xData[0]);
ii=nLen-1;
test = false;
}
}
}
double yyxmidh = this.xData[0][nLen-1];
int jj = nLen-1;
test = true;
while(test){
if(this.yData[jj]>=yyymid){
yyxmidh = this.xData[0][jj];
test = false;
}
else{
jj--;
if(jj<0){
yyxmidh = Stat.mean(this.xData[0]);
jj=1;
test = false;
}
}
}
if(ii>jj){
int jjh = jj;
jj = ii;
ii = jjh;
}
if(ii=0)hillSlope = -1.0;
// initial estimates
double[] start = new double[nParam];
start[0] = this.topS;
start[1] = this.bottomS;
start[2] = this.midPointXvalue;
start[3] = hillSlope;
start[4] = 1.0;
// initial step sizes
double[] step = new double[nParam];
for(int i=0; i=dirFlag*yyymid){
yyxmidl = xData[0][ii];
test = false;
}
else{
ii++;
if(ii>=nLen){
yyxmidl = Stat.mean(this.xData[0]);
ii=nLen-1;
test = false;
}
}
}
double yyxmidh = xData[0][nLen-1];
int jj = nLen-1;
test = true;
while(test){
if(this.yData[jj]<=dirFlag*yyymid){
yyxmidh = xData[0][jj];
test = false;
}
else{
jj--;
if(jj<0){
yyxmidh = Stat.mean(this.xData[0]);
jj=1;
test = false;
}
}
}
int thetaPos = (ii+jj)/2;
double theta0 = xData[0][thetaPos];
// initial estimates
double[] start = new double[nParam];
start[0] = theta0;
if(this.scaleFlag){
if(dirFlag==1){
start[1] = yymax;
}
else{
start[1] = yymin;
}
}
// initial step sizes
double[] step = new double[nParam];
for(int i=0; i=dirFlag*yyymid){
yyxmidl = xData[0][ii];
test = false;
}
else{
ii++;
if(ii>=nLen){
yyxmidl = Stat.mean(this.xData[0]);
ii=nLen-1;
test = false;
}
}
}
double yyxmidh = xData[0][nLen-1];
int jj = nLen-1;
test = true;
while(test){
if(this.yData[jj]<=dirFlag*yyymid){
yyxmidh = xData[0][jj];
test = false;
}
else{
jj--;
if(jj<0){
yyxmidh = Stat.mean(this.xData[0]);
jj=1;
test = false;
}
}
}
int thetaPos = (ii+jj)/2;
double theta0 = xData[0][thetaPos];
// initial estimates
double[] start = new double[nParam];
start[0] = theta0;
start[1] = this.yData[0];
if(dirFlag==1){
start[2] = yymax;
}
else{
start[2] = yymin;
}
// initial step sizes
double[] step = new double[nParam];
for(int i=0; i=dirFlag*yyymid){
yyxmidl = xData[0][ii];
test = false;
}
else{
ii++;
if(ii>=nLen){
yyxmidl = Stat.mean(this.xData[0]);
ii=nLen-1;
test = false;
}
}
}
double yyxmidh = xData[0][nLen-1];
int jj = nLen-1;
test = true;
while(test){
if(this.yData[jj]<=dirFlag*yyymid){
yyxmidh = xData[0][jj];
test = false;
}
else{
jj--;
if(jj<0){
yyxmidh = Stat.mean(this.xData[0]);
jj=1;
test = false;
}
}
}
int thetaPos = (ii+jj)/2;
double theta0 = xData[0][thetaPos];
// initial estimates
double[] start = new double[nParam];
start[0] = theta0;
if(this.scaleFlag){
if(dirFlag==1){
start[1] = yymax;
}
else{
start[1] = yymin;
}
}
// initial step sizes
double[] step = new double[nParam];
for(int i=0; i retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
boolean yFlag = false;
if(yPeak<0.0D){
System.out.println("Regression.fitLogistic(): This implementation of the Logistic distribution takes only positive y values\n(noise taking low values below zero are allowed)");
System.out.println("All y values have been multiplied by -1 before fitting");
for(int i =0; i ret1 = Regression.dataSign(yData);
Integer tempi = null;
tempi = (Integer)ret1.get(5);
int peaki = tempi.intValue();
double mu = xData[0][peaki];
// Calculate an estimate of the beta
double beta = Math.sqrt(6.0D)*halfWidth(xData[0], yData)/Math.PI;
// Calculate estimate of y scale
tempd = (Double)ret1.get(4);
double ym = tempd.doubleValue();
ym=ym*beta*Math.sqrt(2.0D*Math.PI);
// Fill arrays needed by the Simplex
double[] start = new double[this.nParam];
double[] step = new double[this.nParam];
start[0] = mu;
start[1] = beta;
if(this.scaleFlag){
start[2] = ym;
}
step[0] = 0.1D*beta;
step[1] = 0.1D*start[1];
if(step[1]==0.0D){
ArrayList ret0 = Regression.dataSign(xData[0]);
Double tempdd = null;
tempdd = (Double)ret0.get(2);
double xmax = tempdd.doubleValue();
if(xmax==0.0D){
tempdd = (Double)ret0.get(0);
xmax = tempdd.doubleValue();
}
step[0]=xmax*0.1D;
}
if(this.scaleFlag)step[2] = 0.1D*start[2];
// Nelder and Mead Simplex Regression
LogisticFunction f = new LogisticFunction();
this.addConstraint(1,-1,0.0D);
f.setScaleOption(this.scaleFlag);
f.setScaleFactor(this.yScaleFactor);
Object regFun2 = (Object)f;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun2, null, start, step, this.fTol, this.nMax);
if(plotFlag==1){
// Print results
if(!this.supressPrint)this.print();
// Plot results
int flag = this.plotXY(f);
if(flag!=-2 && !this.supressYYplot)this.plotYY();
}
if(yFlag){
// restore data
for(int i=0; i retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
boolean yFlag = false;
if(yPeak<0.0D){
System.out.println("Regression.fitBeta(): This implementation of the Beta distribution takes only positive y values\n(noise taking low values below zero are allowed)");
System.out.println("All y values have been multiplied by -1 before fitting");
for(int i =0; i retX = Regression.dataSign(xData[0]);
Integer tempi = null;
// Calculate x value at peak y (estimate of the 'distribution mode')
tempi = (Integer)retY.get(5);
int peaki = tempi.intValue();
double distribMode = xData[0][peaki];
// minimum value
tempd = (Double)retX.get(0);
double minX = tempd.doubleValue();
// maximum value
tempd = (Double)retX.get(2);
double maxX = tempd.doubleValue();
// mean value
tempd = (Double)retX.get(8);
double meanX = tempd.doubleValue();
// test that data is within range
if(typeFlag==0){
if(minX<0.0D){
System.out.println("Regression: beta: data points must be greater than or equal to 0");
System.out.println("method betaMinMax used in place of method beta");
typeFlag = 1;
this.lastMethod = 32;
this.nParam=5;
}
if(maxX>1.0D){
System.out.println("Regression: beta: data points must be less than or equal to 1");
System.out.println("method betaMinMax used in place of method beta");
typeFlag = 1;
this.lastMethod = 32;
this.nParam=5;
}
}
// Calculate an estimate of the alpha, beta and scale factor
double dMode = distribMode;
double dMean = meanX;
if(typeFlag==1){
dMode = (distribMode - minX*0.9D)/(maxX*1.2D - minX*0.9D);
dMean = (meanX - minX*0.9D)/(maxX*1.2D - minX*0.9D);
}
double alphaGuess = 2.0D*dMode*dMean/(dMode - dMean);
if(alphaGuess<1.3)alphaGuess = 1.6D;
double betaGuess = alphaGuess*(1.0D - dMean)/dMean;
if(betaGuess<=1.3)betaGuess = 1.6D;
double scaleGuess = 0.0D;
if(typeFlag==0){
scaleGuess = yPeak/Stat.betaPDF(alphaGuess, betaGuess, distribMode);
}
else{
scaleGuess = yPeak/Stat.betaPDF(minX, maxX, alphaGuess, betaGuess, distribMode);
}
if(scaleGuess<0)scaleGuess=1;
// Nelder and Mead Simplex Regression for Gumbel
// Fill arrays needed by the Simplex
double[] start = new double[this.nParam];
double[] step = new double[this.nParam];
switch(typeFlag){
case 0: start[0] = alphaGuess; //alpha
start[1] = betaGuess; //beta
if(this.scaleFlag){
start[2] = scaleGuess; //y axis scaling factor
}
step[0] = 0.1D*start[0];
step[1] = 0.1D*start[1];
if(this.scaleFlag)step[2] = 0.1D*start[2];
// Add constraints
this.addConstraint(0,-1,1.0D);
this.addConstraint(1,-1,1.0D);
break;
case 1: start[0] = alphaGuess; //alpha
start[1] = betaGuess; //beta
start[2] = 0.9D*minX; // min
start[3] = 1.1D*maxX; // max
if(this.scaleFlag){
start[4] = scaleGuess; //y axis scaling factor
}
step[0] = 0.1D*start[0];
step[1] = 0.1D*start[1];
step[2] = 0.1D*start[2];
step[3] = 0.1D*start[3];
if(this.scaleFlag)step[4] = 0.1D*start[4];
// Add constraints
this.addConstraint(0,-1,1.0D);
this.addConstraint(1,-1,1.0D);
this.addConstraint(2,+1,minX);
this.addConstraint(3,-1,maxX);
break;
}
// Create instance of Beta function
BetaFunction ff = new BetaFunction();
// Set minimum maximum type option
ff.setTypeFlag(typeFlag);
// Set ordinate scaling option
ff.setScaleOption(this.scaleFlag);
ff.setScaleFactor(this.yScaleFactor);
// Perform simplex regression
Object regFun3 = (Object)ff;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun3, null, start, step, this.fTol, this.nMax);
if(allTest==1){
// Print results
if(!this.supressPrint)this.print();
// Plot results
int flag = this.plotXY(ff);
if(flag!=-2 && !this.supressYYplot)this.plotYY();
}
if(yFlag){
// restore data
for(int i=0; i retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
boolean yFlag = false;
if(yPeak<0.0D){
System.out.println("Regression.fitGamma(): This implementation of the Gamma distribution takes only positive y values\n(noise taking low values below zero are allowed)");
System.out.println("All y values have been multiplied by -1 before fitting");
for(int i =0; i retX = Regression.dataSign(xData[0]);
Integer tempi = null;
// Calculate x value at peak y (estimate of the 'distribution mode')
tempi = (Integer)retY.get(5);
int peaki = tempi.intValue();
double distribMode = xData[0][peaki];
// minimum value
tempd = (Double)retX.get(0);
double minX = tempd.doubleValue();
// maximum value
tempd = (Double)retX.get(2);
double maxX = tempd.doubleValue();
// mean value
tempd = (Double)retX.get(8);
double meanX = tempd.doubleValue();
// test that data is within range
if(typeFlag==1){
if(minX<0.0D){
System.out.println("Regression: gammaStandard: data points must be greater than or equal to 0");
System.out.println("method gamma used in place of method gammaStandard");
typeFlag = 0;
this.lastMethod = 33;
this.nParam=2;
}
}
// Calculate an estimate of the mu, beta, gamma and scale factor
double muGuess = 0.8D*minX;
if(muGuess==0.0D)muGuess = -0.1D;
double betaGuess = meanX - distribMode;
if(betaGuess<=0.0D)betaGuess = 1.0D;
double gammaGuess = (meanX + muGuess)/betaGuess;
if(typeFlag==1)gammaGuess = meanX;
if(gammaGuess<=0.0D)gammaGuess = 1.0D;
double scaleGuess = 0.0D;
if(typeFlag==0){
scaleGuess = yPeak/Stat.gammaPDF(muGuess, betaGuess, gammaGuess, distribMode);
}
else{
scaleGuess = yPeak/Stat.gammaPDF(gammaGuess, distribMode);
}
if(scaleGuess<0)scaleGuess=1;
// Nelder and Mead Simplex Regression for Gamma
// Fill arrays needed by the Simplex
double[] start = new double[this.nParam];
double[] step = new double[this.nParam];
switch(typeFlag){
case 1: start[0] = gammaGuess; //gamma
if(this.scaleFlag){
start[1] = scaleGuess; //y axis scaling factor
}
step[0] = 0.1D*start[0];
if(this.scaleFlag)step[1] = 0.1D*start[1];
// Add constraints
this.addConstraint(0,-1,0.0D);
break;
case 0: start[0] = muGuess; // mu
start[1] = betaGuess; // beta
start[2] = gammaGuess; // gamma
if(this.scaleFlag){
start[3] = scaleGuess; //y axis scaling factor
}
step[0] = 0.1D*start[0];
step[1] = 0.1D*start[1];
step[2] = 0.1D*start[2];
if(this.scaleFlag)step[3] = 0.1D*start[3];
// Add constraints
this.addConstraint(1,-1,0.0D);
this.addConstraint(2,-1,0.0D);
break;
}
// Create instance of Gamma function
GammaFunction ff = new GammaFunction();
// Set type option
ff.setTypeFlag(typeFlag);
// Set ordinate scaling option
ff.setScaleOption(this.scaleFlag);
ff.setScaleFactor(this.yScaleFactor);
// Perform simplex regression
Object regFun3 = (Object)ff;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun3, null, start, step, this.fTol, this.nMax);
if(allTest==1){
// Print results
if(!this.supressPrint)this.print();
// Plot results
int flag = this.plotXY(ff);
if(flag!=-2 && !this.supressYYplot)this.plotYY();
}
if(yFlag){
// restore data
for(int i=0; i retY = Regression.dataSign(yData);
tempd = (Double)retY.get(4);
double yPeak = tempd.doubleValue();
boolean yFlag = false;
if(yPeak<0.0D){
System.out.println("Regression.fitGamma(): This implementation of the Erlang distribution takes only positive y values\n(noise taking low values below zero are allowed)");
System.out.println("All y values have been multiplied by -1 before fitting");
for(int i =0; i retX = Regression.dataSign(xData[0]);
Integer tempi = null;
// Calculate x value at peak y (estimate of the 'distribution mode')
tempi = (Integer)retY.get(5);
int peaki = tempi.intValue();
double distribMode = xData[0][peaki];
// minimum value
tempd = (Double)retX.get(0);
double minX = tempd.doubleValue();
// maximum value
tempd = (Double)retX.get(2);
double maxX = tempd.doubleValue();
// mean value
tempd = (Double)retX.get(8);
double meanX = tempd.doubleValue();
// test that data is within range
if(minX<0.0D)throw new IllegalArgumentException("data points must be greater than or equal to 0");
// FIT TO GAMMA DISTRIBUTION TO OBTAIN INITIAL ESTIMATES
// Calculate an estimate of the mu, beta, gamma and scale factor
double muGuess = 0.8D*minX;
if(muGuess==0.0D)muGuess = -0.1D;
double betaGuess = meanX - distribMode;
if(betaGuess<=0.0D)betaGuess = 1.0D;
double gammaGuess = (meanX + muGuess)/betaGuess;
if(typeFlag==1)gammaGuess = meanX;
if(gammaGuess<=0.0D)gammaGuess = 1.0D;
double scaleGuess = 0.0D;
scaleGuess = yPeak/Stat.gammaPDF(muGuess, betaGuess, gammaGuess, distribMode);
if(scaleGuess<0)scaleGuess=1;
// Nelder and Mead Simplex Regression for Gamma
// Fill arrays needed by the Simplex
double[] start = new double[this.nParam];
double[] step = new double[this.nParam];
start[0] = muGuess; // mu
start[1] = betaGuess; // beta
start[2] = gammaGuess; // gamma
if(this.scaleFlag)start[3] = scaleGuess; //y axis scaling factor
step[0] = 0.1D*start[0];
step[1] = 0.1D*start[1];
step[2] = 0.1D*start[2];
if(this.scaleFlag)step[3] = 0.1D*start[3];
// Add constraints
this.addConstraint(1,-1,0.0D);
this.addConstraint(2,-1,0.0D);
// Create instance of Gamma function
GammaFunction ff = new GammaFunction();
// Set type option
ff.setTypeFlag(typeFlag);
// Set ordinate scaling option
ff.setScaleOption(this.scaleFlag);
ff.setScaleFactor(this.yScaleFactor);
// Perform simplex regression
Object regFun3 = (Object)ff;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun3, null, start, step, this.fTol, this.nMax);
// FIT TO ERLANG DISTRIBUTION USING GAMMA BEST ESTIMATES AS INITIAL ESTIMATES
// AND VARYING RATE PARAMETER BY UNIT STEPS
this.removeConstraints();
// Initial estimates
double[] bestGammaEst = this.getCoeff();
// Swap from Gamma dimensions to Erlang dimensions
this.nParam = nParam0;
start = new double[this.nParam];
step = new double[this.nParam];
if(bestGammaEst[3]<0.0)bestGammaEst[3] *= -1.0;
// initial estimates
start[0] = 1.0D/bestGammaEst[1]; // lambda
if(this.scaleFlag)start[1] = bestGammaEst[3]; //y axis scaling factor
step[0] = 0.1D*start[0];
if(this.scaleFlag)step[1] = 0.1D*start[1];
// Add constraints
this.addConstraint(0,-1,0.0D);
// fix initial integer rate parameter
double kay0 = Math.round(bestGammaEst[2]);
double kay = kay0;
// Create instance of Erlang function
ErlangFunction ef = new ErlangFunction();
// Set ordinate scaling option
ef.setScaleOption(this.scaleFlag);
ef.setScaleFactor(this.yScaleFactor);
ef.setKay(kay);
// Fit stepping up
boolean testKay = true;
double ssMin = Double.NaN;
double upSS = Double.NaN;
double upKay = Double.NaN;
double kayFinal = Double.NaN;
int iStart = 1;
int ssSame = 0;
while(testKay){
// Perform simplex regression
Object regFun4 = (Object)ef;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun4, null, start, step, this.fTol, this.nMax);
double sumOfSquaresError = this.getSumOfSquares();
if(iStart==1){
iStart = 2;
ssMin = sumOfSquaresError;
kay = kay + 1;
start[0] = 1.0D/bestGammaEst[1]; // lambda
if(this.scaleFlag)start[1] = bestGammaEst[3]; //y axis scaling factor
step[0] = 0.1D*start[0];
if(this.scaleFlag)step[1] = 0.1D*start[1];
this.addConstraint(0,-1,0.0D);
ef.setKay(kay);
}
else{
if(sumOfSquaresError<=ssMin){
if(sumOfSquaresError==ssMin){
ssSame++;
if(ssSame==10){
upSS = ssMin;
upKay = kay - 5;
testKay = false;
}
}
ssMin = sumOfSquaresError;
kay = kay + 1;
start[0] = 1.0D/bestGammaEst[1]; // lambda
if(this.scaleFlag)start[1] = bestGammaEst[3]; //y axis scaling factor
step[0] = 0.1D*start[0];
if(this.scaleFlag)step[1] = 0.1D*start[1];
this.addConstraint(0,-1,0.0D);
ef.setKay(kay);
}
else{
upSS = ssMin;
upKay = kay - 1;
testKay = false;
}
}
}
if(kay0==1){
kayFinal = upKay;
}
else{
// Fit stepping down
iStart = 1;
testKay = true;
ssMin = Double.NaN;
double downSS = Double.NaN;
double downKay = Double.NaN;
// initial estimates
start[0] = 1.0D/bestGammaEst[1]; // lambda
if(this.scaleFlag)start[1] = bestGammaEst[3]; //y axis scaling factor
step[0] = 0.1D*start[0];
if(this.scaleFlag)step[1] = 0.1D*start[1];
// Add constraints
this.addConstraint(0,-1,0.0D);
kay = kay0;
ef.setKay(kay);
while(testKay){
// Perform simplex regression
Object regFun5 = (Object)ef;
this.simplexFlag = 1;
this.nonLinStatsNeeded = true;
this.dualErrorsRequired = false;
this.nelderMead(regFun5, null, start, step, this.fTol, this.nMax);
double sumOfSquaresError = this.getSumOfSquares();
if(iStart==1){
iStart = 2;
ssMin = sumOfSquaresError;
kay = kay - 1;
if(Math.rint(kay)<1L){
downSS = ssMin;
downKay = kay + 1;
testKay = false;
}
else{
start[0] = 1.0D/bestGammaEst[1]; // lambda
if(this.scaleFlag)start[1] = bestGammaEst[3]; //y axis scaling factor
step[0] = 0.1D*start[0];
if(this.scaleFlag)step[1] = 0.1D*start[1];
this.addConstraint(0,-1,0.0D);
ef.setKay(kay);
}
}
else{
if(sumOfSquaresError<=ssMin){
ssMin = sumOfSquaresError;
kay = kay - 1;
if(Math.rint(kay)<1L){
downSS = ssMin;
downKay = kay + 1;
testKay = false;
}
else{
start[0] = 1.0D/bestGammaEst[1]; // lambda
if(this.scaleFlag)start[1] = bestGammaEst[3]; //y axis scaling factor
step[0] = 0.1D*start[0];
if(this.scaleFlag)step[1] = 0.1D*start[1];
this.addConstraint(0,-1,0.0D);
ef.setKay(kay);
}
}
else{
downSS = ssMin;
downKay = kay + 1;
testKay = false;
}
}
}
if(downSSdmax)nBins++;
int nPoints = data.length;
int[] dataCheck = new int[nPoints];
for(int i=0; i=binWall[j] && data[i]<=binWall[j+1]*(1.0D + Regression.histTol)){
binFreq[1][j]+= 1.0D;
dataCheck[i]=1;
test=false;
}
}
else{
if(data[i]>=binWall[j] && data[i]0)System.out.println(nMissed+" data points, outside histogram limits, excluded in histogramBins");
return binFreq;
}
// Distribute data into bins to obtain histogram
// zero bin position calculated
public static double[][] histogramBins(double[] data, double binWidth){
double dmin = Fmath.minimum(data);
double dmax = Fmath.maximum(data);
double span = dmax - dmin;
double binZero = dmin;
int nBins = (int) Math.ceil(span/binWidth);
double histoSpan = ((double)nBins)*binWidth;
double rem = histoSpan - span;
if(rem>=0){
binZero -= rem/2.0D;
}
else{
if(Math.abs(rem)/span>Regression.histTol){
// readjust binWidth
boolean testBw = true;
double incr = Regression.histTol/nBins;
int iTest = 0;
while(testBw){
binWidth += incr;
histoSpan = ((double)nBins)*binWidth;
rem = histoSpan - span;
if(rem<0){
iTest++;
if(iTest>1000){
testBw = false;
System.out.println("histogram method could not encompass all data within histogram\nContact Michael thomas Flanagan");
}
}
else{
testBw = false;
}
}
}
}
return Regression.histogramBins(data, binWidth, binZero);
}
// DEPRECATED METHODS
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// Numerical differencing Hessian
public void simplex2(RegressionFunction2 g, double[] start, double[] step, double fTol, int nMax){
this.simplex(g, start, step, fTol, nMax);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// Analytical Hessian derivatives
public void simplex2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start, double[] step, double fTol, int nMax){
this.simplex(g, gd, start, step, fTol, nMax);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Numerical differencing Hessian
public void simplexPlot2(RegressionFunction2 g, double[] start, double[] step, double fTol, int nMax){
this.simplexPlot(g, start, step, fTol, nMax);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Analytical Hessian derivatives
public void simplexPlot2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start, double[] step, double fTol, int nMax){
this.simplexPlot(g, gd, start, step, fTol, nMax);
}
// Nelder and Mead simplex
// Default maximum iterations
// Numerical differencing Hessian
public void simplex2(RegressionFunction2 g, double[] start, double[] step, double fTol){
this.simplex(g, start, step, fTol);
}
// Nelder and Mead simplex
// Default maximum iterations
// Analytical Hessian derivatives
public void simplex2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start, double[] step, double fTol){
this.simplex(g, gd, start, step, fTol);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Default maximum iterations
// Numerical differencing Hessian
public void simplexPlot2(RegressionFunction2 g, double[] start, double[] step, double fTol){
this.simplexPlot(g, start, step, fTol);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Default maximum iterations
// Analytical Hessian derivatives
public void simplexPlot2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start, double[] step, double fTol){
this.simplexPlot(g, gd, start, step, fTol);
}
// Nelder and Mead simplex
// Default tolerance
// Numerical differencing Hessian
public void simplex2(RegressionFunction2 g, double[] start, double[] step, int nMax){
this.simplex(g, start, step, nMax);
}
// Nelder and Mead simplex
// Default tolerance
// Analytical Hessian derivatives
public void simplex2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start, double[] step, int nMax){
this.simplex(g, gd, start, step, nMax);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Default tolerance
// Numerical differencing Hessian
public void simplexPlot2(RegressionFunction2 g, double[] start, double[] step, int nMax){
this.simplexPlot(g, start, step, nMax);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Default tolerance
// Analytical Hessian derivatives
public void simplexPlot2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start, double[] step, int nMax){
this.simplexPlot(g, gd, start, step, nMax);
}
// Nelder and Mead simplex
// Default tolerance
// Default maximum iterations
// Numerical differencing Hessian
public void simplex2(RegressionFunction2 g, double[] start, double[] step){
this.simplex(g, start, step);
}
// Nelder and Mead simplex
// Default tolerance
// Default maximum iterations
// Analytical Hessian derivatives
public void simplex2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start, double[] step){
this.simplex(g, gd, start, step);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Default tolerance
// Default maximum iterations
// Numerical differencing Hessian
public void simplexPlot2(RegressionFunction2 g, double[] start, double[] step){
this.simplexPlot(g, start, step);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Default tolerance
// Default maximum iterations
// Analytical Hessian derivatives
public void simplexPlot2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start, double[] step){
this.simplexPlot(g, gd, start, step);
}
// Nelder and Mead simplex
// Default step option - all step[i] = dStep
// Numerical differencing Hessian
public void simplex2(RegressionFunction2 g, double[] start, double fTol, int nMax){
this.simplex(g, start, fTol, nMax);
}
// Nelder and Mead simplex
// Default step option - all step[i] = dStep
// Analytical Hessian derivatives
public void simplex2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start, double fTol, int nMax){
this.simplex(g, gd, start, fTol, nMax);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Default step option - all step[i] = dStep
// Numerical differencing Hessian
public void simplexPlot2(RegressionFunction2 g, double[] start, double fTol, int nMax){
this.simplexPlot(g, start, fTol, nMax);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Default step option - all step[i] = dStep
// Analytical Hessian derivatives
public void simplexPlot2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start, double fTol, int nMax){
this.simplexPlot(g, gd, start, fTol, nMax);
}
// Nelder and Mead simplex
// Default maximum iterations
// Default step option - all step[i] = dStep
// Numerical differencing Hessian
public void simplex2(RegressionFunction2 g, double[] start, double fTol){
this.simplex(g, start, fTol);
}
// Nelder and Mead simplex
// Default maximum iterations
// Default step option - all step[i] = dStep
// Analytical Hessian derivatives
public void simplex2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start, double fTol){
this.simplex(g, gd, start, fTol);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Default maximum iterations
// Default step option - all step[i] = dStep
// Numerical differencing Hessian
public void simplexPlot2(RegressionFunction2 g, double[] start, double fTol){
this.simplexPlot(g, start, fTol);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Default maximum iterations
// Default step option - all step[i] = dStep
// Analytical Hessian derivatives
public void simplexPlot2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start, double fTol){
this.simplexPlot(g, gd, start, fTol);
}
// Nelder and Mead simplex
// Default tolerance
// Default step option - all step[i] = dStep
// Numerical differencing Hessian
public void simplex2(RegressionFunction2 g, double[] start, int nMax){
this.simplex(g, start, nMax);
}
// Nelder and Mead simplex
// Default tolerance
// Default step option - all step[i] = dStep
// Analytical Hessian derivatives
public void simplex2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start, int nMax){
this.simplex(g, gd, start, nMax);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Default tolerance
// Default step option - all step[i] = dStep
// Numerical differencing Hessian
public void simplexPlot2(RegressionFunction2 g, double[] start, int nMax){
this.simplexPlot(g, start, nMax);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Default tolerance
// Default step option - all step[i] = dStep
// Analytical Hessian derivatives
public void simplexPlot2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start, int nMax){
this.simplexPlot(g, gd, start, nMax);
}
// Nelder and Mead simplex
// Default tolerance
// Default maximum iterations
// Default step option - all step[i] = dStep
// Numerical differencing Hessian
public void simplex2(RegressionFunction2 g, double[] start){
this.simplex(g, start);
}
// Nelder and Mead simplex
// Default tolerance
// Default maximum iterations
// Default step option - all step[i] = dStep
// Analytical Hessian derivatives
public void simplex2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start){
this.simplex(g, gd, start);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Default tolerance
// Default maximum iterations
// Default step option - all step[i] = dStep
// Numerical differencing Hessian
public void simplexPlot2(RegressionFunction2 g, double[] start){
this.simplexPlot(g, start);
}
// Nelder and Mead Simplex Simplex2 Non-linear Regression
// plus plot and output file
// Default tolerance
// Default maximum iterations
// Default step option - all step[i] = dStep
// Analytical Hessian derivatives
public void simplexPlot2(RegressionFunction2 g, RegressionDerivativeFunction2 gd, double[] start){
this.simplexPlot(g, gd, start);
}
// plot experimental x against experimental y and against calculated y
// non-linear regression data
// title provided
// matching simplex2
protected int plotXY2(RegressionFunction2 g, String title){
return plotXY(g, title);
}
// plot experimental x against experimental y and against calculated y
// non-linear regression data
// no title provided
// matches simplex2
protected int plotXY2(RegressionFunction2 g){
return plotXY(g);
}
// Pareto
public void pareto(){
this.fitPareto(0, 2);
}
public void paretoPlot(){
this.fitPareto(1, 2);
}
}
// CLASSES TO EVALUATE THE SPECIAL FUNCTIONS
// Class to evaluate derivatives for the Gausian (normal) function y = (yscale/sd.sqrt(2.pi)).exp(-0.5[(x - xmean)/sd]^2).
class GaussianFunction implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
public double function(double[] p, double[] x){
double yScale = scaleFactor;
if(scaleOption)yScale = p[2];
double y = (yScale/(p[1]*Math.sqrt(2.0D*Math.PI)))*Math.exp(-0.5D*Fmath.square((x[0]-p[0])/p[1]));
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
}
// Class to evaluate the Gausian (normal) function y = (yscale/sd.sqrt(2.pi)).exp(-0.5[(x - xmean)/sd]^2).
// Some parameters may be fixed
class GaussianFunctionFixed implements RegressionFunction{
private double[] param = new double[3];
private boolean[] fixed = new boolean[3];
public double function(double[] p, double[] x){
int ii = 0;
for(int i=0; i<3; i++){
if(!fixed[i]){
param[i] = p[ii];
ii++;
}
}
double y = (param[2]/(param[1]*Math.sqrt(2.0D*Math.PI)))*Math.exp(-0.5D*Fmath.square((x[0]-param[0])/param[1]));
return y;
}
public void setParam(double[] param){
this.param = param;
}
public void setFixed(boolean[] fixed){
this.fixed = fixed;
}
}
// Class to evaluate the multiple Gausian (normal) function y = Sum[(A(i)/sd(i).sqrt(2.pi)).exp(-0.5[(x - xmean(i))/sd(i)]^2)].
class MultipleGaussianFunction implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
private int nGaussians = 1;
public double function(double[] p, double[] x){
double y = 0.0;
int counter = 0;
for(int i=0; i Minimum Mode Gumbel
// reset to 1 -> Maximum Mode Gumbel
// reset to 2 -> one parameter Minimum Mode Gumbel
// reset to 3 -> one parameter Maximum Mode Gumbel
// reset to 4 -> standard Minimum Mode Gumbel
// reset to 5 -> standard Maximum Mode Gumbel
public double function(double[] p, double[] x){
double y=0.0D;
double arg=0.0D;
double yScale = scaleFactor;
switch(this.typeFlag){
case 0:
// y = yscale*(1/gamma)*exp((x-mu)/gamma)*exp(-exp((x-mu)/gamma))
arg = (x[0]-p[0])/p[1];
if(scaleOption)yScale = p[2];
y = (yScale/p[1])*Math.exp(arg)*Math.exp(-(Math.exp(arg)));
break;
case 1:
// y = yscale*(1/gamma)*exp((mu-x)/gamma)*exp(-exp((mu-x)/gamma))
arg = (p[0]-x[0])/p[1];
if(scaleOption)yScale = p[2];
y = (yScale/p[1])*Math.exp(arg)*Math.exp(-(Math.exp(arg)));
break;
case 2:
// y = yscale*(1/gamma)*exp((x)/gamma)*exp(-exp((x)/gamma))
arg = x[0]/p[0];
if(scaleOption)yScale = p[1];
y = (yScale/p[0])*Math.exp(arg)*Math.exp(-(Math.exp(arg)));
break;
case 3:
// y = yscale*(1/gamma)*exp((-x)/gamma)*exp(-exp((-x)/gamma))
arg = -x[0]/p[0];
if(scaleOption)yScale = p[1];
y = (yScale/p[0])*Math.exp(arg)*Math.exp(-(Math.exp(arg)));
break;
case 4:
// y = yscale*exp(x)*exp(-exp(x))
if(scaleOption)yScale = p[0];
y = yScale*Math.exp(x[0])*Math.exp(-(Math.exp(x[0])));
break;
case 5:
// y = yscale*exp(-x)*exp(-exp(-x))
if(scaleOption)yScale = p[0];
y = yScale*Math.exp(-x[0])*Math.exp(-(Math.exp(-x[0])));
break;
}
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
public void setTypeFlag(int typeFlag){
this.typeFlag = typeFlag;
}
}
// Class to evaluate the Frechet function
// y = yscale.(gamma/sigma)*((x - mu)/sigma)^(-gamma-1)*exp(-((x-mu)/sigma)^-gamma
class FrechetFunctionOne implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
private int typeFlag = 0; // set to 0 -> Three Parameter Frechet
// reset to 1 -> Two Parameter Frechet
// reset to 2 -> Standard Frechet
public double function(double[] p, double[] x){
double y = 0.0D;
boolean test = false;
double yScale = scaleFactor;
switch(typeFlag){
case 0: if(x[0]>=p[0]){
double arg = (x[0] - p[0])/p[1];
if(scaleOption)yScale = p[3];
y = yScale*(p[2]/p[1])*Math.pow(arg,-p[2]-1.0D)*Math.exp(-Math.pow(arg,-p[2]));
}
break;
case 1: if(x[0]>=0.0D){
double arg = x[0]/p[0];
if(scaleOption)yScale = p[2];
y = yScale*(p[1]/p[0])*Math.pow(arg,-p[1]-1.0D)*Math.exp(-Math.pow(arg,-p[1]));
}
break;
case 2: if(x[0]>=0.0D){
double arg = x[0];
if(scaleOption)yScale = p[1];
y = yScale*p[0]*Math.pow(arg,-p[0]-1.0D)*Math.exp(-Math.pow(arg,-p[0]));
}
break;
}
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
public void setTypeFlag(int typeFlag){
this.typeFlag = typeFlag;
}
}
// Class to evaluate the semi-linearised Frechet function
// log(log(1/(1-Cumulative y) = gamma*log((x-mu)/sigma)
class FrechetFunctionTwo implements RegressionFunction{
private int typeFlag = 0; // set to 0 -> Three Parameter Frechet
// reset to 1 -> Two Parameter Frechet
// reset to 2 -> Standard Frechet
public double function(double[] p, double[] x){
double y=0.0D;
switch(typeFlag){
case 0: y = -p[2]*Math.log(Math.abs(x[0]-p[0])/p[1]);
break;
case 1: y = -p[1]*Math.log(Math.abs(x[0])/p[0]);
break;
case 2: y = -p[0]*Math.log(Math.abs(x[0]));
break;
}
return y;
}
public void setTypeFlag(int typeFlag){
this.typeFlag = typeFlag;
}
}
// Class to evaluate the Weibull function
// y = yscale.(gamma/sigma)*((x - mu)/sigma)^(gamma-1)*exp(-((x-mu)/sigma)^gamma
class WeibullFunctionOne implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
private int typeFlag = 0; // set to 0 -> Three Parameter Weibull
// reset to 1 -> Two Parameter Weibull
// reset to 2 -> Standard Weibull
public double function(double[] p, double[] x){
double y = 0.0D;
boolean test = false;
double yScale = scaleFactor;
switch(typeFlag){
case 0: if(x[0]>=p[0]){
double arg = (x[0] - p[0])/p[1];
if(scaleOption)yScale = p[3];
y = yScale*(p[2]/p[1])*Math.pow(arg,p[2]-1.0D)*Math.exp(-Math.pow(arg,p[2]));
}
break;
case 1: if(x[0]>=0.0D){
double arg = x[0]/p[0];
if(scaleOption)yScale = p[2];
y = yScale*(p[1]/p[0])*Math.pow(arg,p[1]-1.0D)*Math.exp(-Math.pow(arg,p[1]));
}
break;
case 2: if(x[0]>=0.0D){
double arg = x[0];
if(scaleOption)yScale = p[1];
y = yScale*p[0]*Math.pow(arg,p[0]-1.0D)*Math.exp(-Math.pow(arg,p[0]));
}
break;
}
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
public void setTypeFlag(int typeFlag){
this.typeFlag = typeFlag;
}
}
// Class to evaluate the semi-linearised Weibull function
// log(log(1/(1-Cumulative y) = gamma*log((x-mu)/sigma)
class WeibullFunctionTwo implements RegressionFunction{
private int typeFlag = 0; // set to 0 -> Three Parameter Weibull
// reset to 1 -> Two Parameter Weibull
// reset to 2 -> Standard Weibull
public double function(double[] p, double[] x){
double y=0.0D;
switch(typeFlag){
case 0: y = p[2]*Math.log(Math.abs(x[0]-p[0])/p[1]);
break;
case 1: y = p[1]*Math.log(Math.abs(x[0])/p[0]);
break;
case 2: y = p[0]*Math.log(Math.abs(x[0]));
break;
}
return y;
}
public void setTypeFlag(int typeFlag){
this.typeFlag = typeFlag;
}
}
// Class to evaluate the Rayleigh function
// y = (yscale/sigma)*(x/sigma)*exp(-0.5((x-mu)/sigma)^2
class RayleighFunctionOne implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
public double function(double[] p, double[] x){
double y = 0.0D;
boolean test = false;
double yScale = scaleFactor;
if(scaleOption)yScale = p[1];
if(x[0]>=0.0D){
double arg = x[0]/p[0];
y = (yScale/p[0])*arg*Math.exp(-0.5D*Math.pow(arg,2));
}
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
}
// Class to evaluate the semi-linearised Rayleigh function
// log(1/(1-Cumulative y) = 0.5*(x/sigma)^2
class RayleighFunctionTwo implements RegressionFunction{
public double function(double[] p, double[] x){
double y = 0.5D*Math.pow(x[0]/p[0],2);
return y;
}
}
// class to evaluate a simple exponential function
class ExponentialSimpleFunction implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
public double function(double[] p, double[] x){
double yScale = scaleFactor;
if(scaleOption)yScale = p[1];
double y = yScale*Math.exp(p[0]*x[0]);
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
}
// class to evaluate multiple exponentials function
class ExponentialMultipleFunction implements RegressionFunction{
private int nExps = 0;
public double function(double[] p, double[] x){
double y = 0;
for(int i=0; i Two Parameter Exponential
// reset to 1 -> One Parameter Exponential
// reset to 2 -> Standard Exponential
public double function(double[] p, double[] x){
double y = 0.0D;
boolean test = false;
double yScale = scaleFactor;
switch(typeFlag){
case 0: if(x[0]>=p[0]){
if(scaleOption)yScale = p[2];
double arg = (x[0] - p[0])/p[1];
y = (yScale/p[1])*Math.exp(-arg);
}
break;
case 1: if(x[0]>=0.0D){
double arg = x[0]/p[0];
if(scaleOption)yScale = p[1];
y = (yScale/p[0])*Math.exp(-arg);
}
break;
case 2: if(x[0]>=0.0D){
double arg = x[0];
if(scaleOption)yScale = p[0];
y = yScale*Math.exp(-arg);
}
break;
}
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
public void setTypeFlag(int typeFlag){
this.typeFlag = typeFlag;
}
}
// class to evaluate a Pareto scaled pdf
class ParetoFunctionOne implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
private int typeFlag = 0; // set to 3 -> Shifted Pareto
// set to 2 -> Two Parameter Pareto
// set to 1 -> One Parameter Pareto
public double function(double[] p, double[] x){
double y = 0.0D;
boolean test = false;
double yScale = scaleFactor;
switch(typeFlag){
case 3: if(x[0]>=p[1]+p[2]){
if(scaleOption)yScale = p[3];
y = yScale*p[0]*Math.pow(p[1],p[0])/Math.pow((x[0]-p[2]),p[0]+1.0D);
}
break;
case 2: if(x[0]>=p[1]){
if(scaleOption)yScale = p[2];
y = yScale*p[0]*Math.pow(p[1],p[0])/Math.pow(x[0],p[0]+1.0D);
}
break;
case 1: if(x[0]>=1.0D){
double arg = x[0]/p[0];
if(scaleOption)yScale = p[1];
y = yScale*p[0]/Math.pow(x[0],p[0]+1.0D);
}
break;
}
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
public void setTypeFlag(int typeFlag){
this.typeFlag = typeFlag;
}
}
// class to evaluate a Pareto cdf
class ParetoFunctionTwo implements RegressionFunction{
private int typeFlag = 0; // set to 3 -> Shifted Pareto
// set to 2 -> Two Parameter Pareto
// set to 1 -> One Parameter Pareto
public double function(double[] p, double[] x){
double y = 0.0D;
switch(typeFlag){
case 3: if(x[0]>=p[1]+p[2]){
y = 1.0D - Math.pow(p[1]/(x[0]-p[2]),p[0]);
}
break;
case 2: if(x[0]>=p[1]){
y = 1.0D - Math.pow(p[1]/x[0],p[0]);
}
break;
case 1: if(x[0]>=1.0D){
y = 1.0D - Math.pow(1.0D/x[0],p[0]);
}
break;
}
return y;
}
public void setTypeFlag(int typeFlag){
this.typeFlag = typeFlag;
}
}
// class to evaluate a Sigmoidal threshold function
class SigmoidThresholdFunction implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
public double function(double[] p, double[] x){
double yScale = scaleFactor;
if(scaleOption)yScale = p[2];
double y = yScale/(1.0D + Math.exp(-p[0]*(x[0] - p[1])));
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
}
// class to evaluate a Sigmoidal threshold function
// x and y erors
class SigmoidThresholdFunctionDual implements RegressionFunction3{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
private double[][] xErrors = null;
private double[] yErrors = null;
public double[] function(double[] p, double[] x, int ii){
double yScale = scaleFactor;
if(scaleOption)yScale = p[2];
double[] y = new double[2];
double hold = Math.exp(-p[0]*(x[0] - p[1]));
y[0] = yScale/(1.0 + hold);
if(ii>=0){
y[1] = yScale*p[0]*hold/((1.0 + hold)*(1.0 + hold));
y[1] += this.yErrors[ii]*this.yErrors[ii] + y[1]*y[1]*this.xErrors[0][ii]*this.xErrors[0][ii];
}
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
public void setXerrors(double[][] xErrors){
this.xErrors = xErrors;
}
public void setYerrors(double[] yErrors){
this.yErrors = yErrors;
}
}
// class to evaluate a Rectangular Hyberbola
class RectangularHyperbolaFunction implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
public double function(double[] p, double[] x){
double yScale = scaleFactor;
if(scaleOption)yScale = p[1];
double y = yScale*x[0]/(p[0] + x[0]);
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
}
class RectangularHyperbolaFunctionDual implements RegressionFunction3{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
private double[][] xErrors = null;
private double[] yErrors = null;
public double[] function(double[] p, double[] x, int ii){
double yScale = scaleFactor;
if(scaleOption)yScale = p[1];
double[] y = new double[2];
double hold = p[0] + x[0];
y[0] = yScale*x[0]/hold;
y[1] = p[0]*yScale/(hold*hold); // dy/dx
y[1] = this.yErrors[ii]*this.yErrors[ii] + y[1]*y[1]*this.xErrors[0][ii]*this.xErrors[0][ii]; // w*w
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
public void setXerrors(double[][] xErrors){
this.xErrors = xErrors;
}
public void setYerrors(double[] yErrors){
this.yErrors = yErrors;
}
}
// class to evaluate a Shifted Rectangular Hyberbola
class ShiftedRectangularHyperbolaFunction implements RegressionFunction{
public double function(double[] p, double[] x){
double y = p[2]*x[0]/(p[0] + x[0]) + p[1];
return y;
}
}
class ShiftedRectangularHyperbolaFunctionDual implements RegressionFunction3{
private double[][] xErrors = null;
private double[] yErrors = null;
public double[] function(double[] p, double[] x, int ii){
double[] y = new double[2];
double hold = p[0] + x[0];
// Calculated y value
y[0] = p[2]*x[0]/hold + p[1];
// Calculated the square of the weight
y[1] = p[0]*p[2]/(hold*hold); // dy/dx
y[1] = this.yErrors[ii]*this.yErrors[ii] + y[1]*y[1]*this.xErrors[0][ii]*this.xErrors[0][ii]; // w*w
return y;
}
public void setXerrors(double[][] xErrors){
this.xErrors = xErrors;
}
public void setYerrors(double[] yErrors){
this.yErrors = yErrors;
}
}
// class to evaluate a scaled Heaviside Step Function
class StepFunctionFunction implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
public double function(double[] p, double[] x){
double yScale = scaleFactor;
if(scaleOption)yScale = p[1];
double y = 0.0D;
if(x[0]>p[0])y = yScale;
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
}
// class to evaluate a Hill or Sips sigmoidal function
class SigmoidHillSipsFunction implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
public double function(double[] p, double[] x){
double yScale = scaleFactor;
if(scaleOption)yScale = p[2];
double xterm = Math.pow(x[0],p[1]);
double y = yScale*xterm/(Math.pow(p[0], p[1]) + xterm);
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
}
class SigmoidHillSipsFunctionDual implements RegressionFunction3{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
private double[][] xErrors = null;
private double[] yErrors = null;
public double[] function(double[] p, double[] x, int ii){
double yScale = scaleFactor;
if(scaleOption)yScale = p[2];
double[] y = new double[2];
double xterm = Math.pow(x[0],p[1]);
double thetaterm = Math.pow(p[0], p[1]);
y[0] = yScale*xterm/(thetaterm + xterm);
y[1] = 0.0;
if(ii>=0){
y[1] = yScale*thetaterm*p[1]*Math.pow(x[0],p[1]-1.0)/((thetaterm + xterm)*(thetaterm + xterm));
y[1] = this.yErrors[ii]*this.yErrors[ii] + y[1]*y[1]*this.xErrors[0][ii]*this.xErrors[0][ii];
}
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
public void setXerrors(double[][] xErrors){
this.xErrors = xErrors;
}
public void setYerrors(double[] yErrors){
this.yErrors = yErrors;
}
}
// Class to evaluate the Logistic probability function y = yscale*exp(-(x-mu)/beta)/(beta*(1 + exp(-(x-mu)/beta))^2.
class LogisticFunction implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
public double function(double[] p, double[] x){
double yScale = scaleFactor;
if(scaleOption)yScale = p[2];
double y = yScale*Fmath.square(Fmath.sech((x[0] - p[0])/(2.0D*p[1])))/(4.0D*p[1]);
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
}
// class to evaluate a Beta scaled pdf
class BetaFunction implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
private int typeFlag = 0; // set to 0 -> Beta Distibution - [0, 1] interval
// set to 1 -> Beta Distibution - [min, max] interval
public double function(double[] p, double[] x){
double y = 0.0D;
boolean test = false;
double yScale = scaleFactor;
switch(typeFlag){
case 0: if(scaleOption)yScale = p[2];
y = yScale*Math.pow(x[0],p[0]-1.0D)*Math.pow(1.0D-x[0],p[1]-1.0D)/Stat.betaFunction(p[0],p[1]);
break;
case 1: if(scaleOption)yScale = p[4];
y = yScale*Math.pow(x[0]-p[2],p[0]-1.0D)*Math.pow(p[3]-x[0],p[1]-1.0D)/Stat.betaFunction(p[0],p[1]);
y = y/Math.pow(p[3]-p[2],p[0]+p[1]-1.0D);
break;
}
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
public void setTypeFlag(int typeFlag){
this.typeFlag = typeFlag;
}
}
// class to evaluate a Gamma scaled pdf
class GammaFunction implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
private int typeFlag = 0; // set to 0 -> Three parameter Gamma Distribution
// set to 1 -> Standard Gamma Distribution
public double function(double[] p, double[] x){
double y = 0.0D;
boolean test = false;
double yScale = scaleFactor;
switch(typeFlag){
case 0: if(scaleOption)yScale = p[3];
double xTerm = (x[0] - p[0])/p[1];
y = yScale*Math.pow(xTerm,p[2]-1.0D)*Math.exp(-xTerm)/(p[1]*Stat.gammaFunction(p[2]));
break;
case 1: if(scaleOption)yScale = p[1];
y = yScale*Math.pow(x[0],p[0]-1.0D)*Math.exp(-x[0])/Stat.gammaFunction(p[0]);
break;
}
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
public void setTypeFlag(int typeFlag){
this.typeFlag = typeFlag;
}
}
// class to evaluate a Erlang scaled pdf
// rate parameter is fixed
class ErlangFunction implements RegressionFunction{
private boolean scaleOption = true;
private double scaleFactor = 1.0D;
private double kay = 1.0D; // rate parameter
public double function(double[] p, double[] x){
boolean test = false;
double yScale = scaleFactor;
if(scaleOption)yScale = p[1];
double y = kay*Math.log(p[0]) + (kay - 1)*Math.log(x[0]) - x[0]*p[0] - Fmath.logFactorial(kay - 1);
y = yScale*Math.exp(y);
return y;
}
public void setScaleFactor(double scaleFactor){
this.scaleFactor = scaleFactor;
}
public void setScaleOption(boolean scaleFlag){
this.scaleOption = scaleFlag;
}
public void setKay(double kay){
this.kay = kay;
}
}
// class to evaluate a EC50 function (four point logistic)
class EC50Function implements RegressionFunction{
public double function(double[] p, double[] x){
double y = p[0] + (p[1] - p[0])/(1.0D + Math.pow(x[0]/p[2], p[3]));
return y;
}
}
class EC50FunctionDual implements RegressionFunction3{
private double[][] xErrors = null;
private double[] yErrors = null;
public double[] function(double[] p, double[] x, int ii){
double[] y = new double[2];
y[0] = p[0] + (p[1] - p[0])/(1.0D + Math.pow(x[0]/p[2], p[3]));
if(ii>=0){
y[1] = (p[0] - p[1])*Math.pow(x[0]/p[2], p[3]-1.0)/(p[2]*Math.pow((1.0D + Math.pow(x[0]/p[2], p[3])), 2.0));
y[1] = this.yErrors[ii]*this.yErrors[ii] + y[1]*y[1]*this.xErrors[0][ii]*this.xErrors[0][ii];
}
return y;
}
public void setXerrors(double[][] xErrors){
this.xErrors = xErrors;
}
public void setYerrors(double[] yErrors){
this.yErrors = yErrors;
}
}
// class to evaluate a EC50 function (four point logistic)
// top and bottom fixed
class EC50FixedFunction implements RegressionFunction{
private double bottom = 0.0;
private double top = 0.0;
public double function(double[] p, double[] x){
double y = top + (bottom - top)/(1.0D + Math.pow(x[0]/p[0], p[1]));
return y;
}
public void setBottom(double bottom){
this.bottom = bottom;
}
public void setTop(double top){
this.top = top;
}
}
class EC50FixedFunctionDual implements RegressionFunction3{
private double bottom = 0.0;
private double top = 0.0;
private double[][] xErrors = null;
private double[] yErrors = null;
public double[] function(double[] p, double[] x, int ii){
double[] y = new double[2];
y[0] = top + (bottom - top)/(1.0D + Math.pow(x[0]/p[0], p[1]));
if(ii>=0){
y[1] = (top - bottom)*Math.pow(x[0]/p[0], p[1]-1.0)/(p[0]*Math.pow((1.0D + Math.pow(x[0]/p[0], p[1])), 2.0));
y[1] = this.yErrors[ii]*this.yErrors[ii] + y[1]*y[1]*this.xErrors[0][ii]*this.xErrors[0][ii];
}
return y;
}
public void setBottom(double bottom){
this.bottom = bottom;
}
public void setTop(double top){
this.top = top;
}
public void setXerrors(double[][] xErrors){
this.xErrors = xErrors;
}
public void setYerrors(double[] yErrors){
this.yErrors = yErrors;
}
}
// class to evaluate a five parameter logistic function
class Logistic5Function implements RegressionFunction{
public double function(double[] p, double[] x){
double y = p[0] + (p[1] - p[0])/Math.pow((1.0D + Math.pow(x[0]/p[2], p[3])),p[4]);
return y;
}
}
class Logistic5FunctionDual implements RegressionFunction3{
private double[][] xErrors = null;
private double[] yErrors = null;
public double[] function(double[] p, double[] x, int ii){
double[] y = new double[2];
y[0] = p[0] + (p[1] - p[0])/Math.pow((1.0D + Math.pow(x[0]/p[2], p[3])),p[4]);
if(ii>=0.0){
y[1] = (p[0] - p[1])*p[4]*p[3]*Math.pow(x[0]/p[2],p[3]-1.0)/(p[2]*Math.pow((1.0D + Math.pow(x[0]/p[2], p[3])),p[4]+1.0));
y[1] = this.yErrors[ii]*this.yErrors[ii] + y[1]*y[1]*this.xErrors[0][ii]*this.xErrors[0][ii];
}
return y;
}
public void setXerrors(double[][] xErrors){
this.xErrors = xErrors;
}
public void setYerrors(double[] yErrors){
this.yErrors = yErrors;
}
}
// class to evaluate a five parameter logistic function
// top and bottom fixed
class Logistic5FixedFunction implements RegressionFunction{
private double bottom = 0.0;
private double top = 0.0;
public double function(double[] p, double[] x){
double y = top + (bottom - top)/Math.pow((1.0D + Math.pow(x[0]/p[0], p[1])),p[2]);
return y;
}
public void setBottom(double bottom){
this.bottom = bottom;
}
public void setTop(double top){
this.top = top;
}
}
class Logistic5FixedFunctionDual implements RegressionFunction3{
private double bottom = 0.0;
private double top = 0.0;
private double[][] xErrors = null;
private double[] yErrors = null;
public double[] function(double[] p, double[] x, int ii){
double[] y = new double[2];
y[0] = top + (bottom - top)/Math.pow((1.0D + Math.pow(x[0]/p[0], p[1])),p[2]);
if(ii>=0.0){
y[1] = (top - bottom)*p[2]*p[1]*Math.pow(x[0]/p[0],p[1]-1.0)/(p[0]*Math.pow((1.0D + Math.pow(x[0]/p[0], p[1])),p[2]+1.0));
y[1] = this.yErrors[ii]*this.yErrors[ii] + y[1]*y[1]*this.xErrors[0][ii]*this.xErrors[0][ii];
}
return y;
}
public void setBottom(double bottom){
this.bottom = bottom;
}
public void setTop(double top){
this.top = top;
}
public void setXerrors(double[][] xErrors){
this.xErrors = xErrors;
}
public void setYerrors(double[] yErrors){
this.yErrors = yErrors;
}
}
// class to evaluate a Non-Integer Polynomial function
// y = a[0] + a[1].x + a[2].x^a[n+1] + a[3].x^a[n+2] + . . . + a[n].x^a[2n-1]
// nTerms = n+1;
// no x error version
class NonIntegerPolyFunction implements RegressionFunction{
private int nTerms = 0;
public double function(double[] p, double[] x){
double y = p[0];
for(int i=1; i=0){
for(int i=1; i
Ads help maintain this website.