Example 3: Nonlinear Regression using Set Methods

In this example some nondefault tolerances and scales are used to fit a nonlinear model. The data is 1.e-10 times the data of example 1. In order to fit this model without rescaling the data we first set the absolute function tolerance to 0.0. The default value would have caused the program to terminate after one iteration because the residual sum of squares is roughly 1.e-19. We also set the relative function tolerance to 0.0. The gradient tolerance is properly scaled for this problem so we leave it at \ its default value. Finally, we set the elements of scale to be the absolute value of the recipricol of the starting value.The derivatives are obtained by finite differences.

import com.imsl.stat.*;

public class NonlinearRegressionEx3 {

    public static void main(String args[]) throws Exception {
        NonlinearRegression.Function f = new NonlinearRegression.Function() {

            public boolean f(double theta[], int iobs, double frq[], double wt[],
                    double e[]) {

                double ydata[] = {54.e-10, 50.e-10, 45.e-10, 37.e-10, 35.e-10,
                    25.e-10, 20.e-10, 16.e-10, 18.e-10, 13.e-10, 8.e-10, 11.e-10,
                    8.e-10, 4.e-10, 6.e-10};
                double xdata[] = {2.0, 5.0, 7.0, 10.0, 14.0, 19.0, 26.0, 31.0,
                    34.0, 38.0, 45.0, 52.0, 53.0, 60.0, 65.0};
                boolean iend;
                int nobs = 15;
                if (iobs < nobs) {
                    wt[0] = 1.0;
                    frq[0] = 1.0;
                    iend = true;
                    e[0] = ydata[iobs] - theta[0] * Math.exp(theta[1]
                            * xdata[iobs]);
                } else {
                    iend = false;
                }
                return iend;
            }
        };
        int nparm = 2;
        double theta[] = {6.e-9, -0.03};
        double scale[] = new double[nparm];
        NonlinearRegression regression = new NonlinearRegression(nparm);
        regression.setGuess(theta);
        regression.setAbsoluteTolerance(0.0);
        regression.setRelativeTolerance(0.0);
        scale[0] = 1.0 / Math.abs(theta[0]);
        scale[1] = 1.0 / Math.abs(theta[1]);
        regression.setScale(scale);
        double coef[] = regression.solve(f);
        System.out.println("The computed regression coefficients are {"
                + coef[0] + ", " + coef[1] + "}");
        int rank = regression.getRank();
        System.out.println("The computed rank is " + rank);
        double dfe = regression.getDFError();
        System.out.println("The degrees of freedom for error are " + dfe);
        double sse = regression.getSSE();
        System.out.println("The sums of squares for error is " + sse);
        double r[][] = regression.getR();
        System.out.println("R from the QR decomposition is "
                + r[0][0] + " " + r[0][1]);
        System.out.println("                               "
                + r[1][0] + " " + r[1][1]);
    }
}

Output

The computed regression coefficients are {5.7837836210879824E-9, -0.0396252538296399}
The computed rank is 2
The degrees of freedom for error are 13.0
The sums of squares for error is 5.166376610434158E-19
R from the QR decomposition is 1.873105632124423 5.7473458654105505E-9
                               0.0 5.837139910539398E-11
Link to Java source.