1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with this 4 * work for additional information regarding copyright ownership. The ASF 5 * licenses this file to You under the Apache License, Version 2.0 (the 6 * "License"); you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law 9 * or agreed to in writing, software distributed under the License is 10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language 12 * governing permissions and limitations under the License. 13 */ 14 package org.apache.commons.math3.optim.nonlinear.vector.jacobian; 15 16 import java.io.IOException; 17 import java.util.Arrays; 18 19 import org.apache.commons.math3.optim.PointVectorValuePair; 20 import org.apache.commons.math3.optim.InitialGuess; 21 import org.apache.commons.math3.optim.MaxEval; 22 import org.apache.commons.math3.optim.nonlinear.vector.Target; 23 import org.apache.commons.math3.optim.nonlinear.vector.Weight; 24 import org.apache.commons.math3.util.FastMath; 25 import org.junit.Test; 26 import org.junit.Assert; 27 28 @Deprecated 29 public class AbstractLeastSquaresOptimizerTest { 30 createOptimizer()31 public static AbstractLeastSquaresOptimizer createOptimizer() { 32 return new AbstractLeastSquaresOptimizer(null) { 33 34 @Override 35 protected PointVectorValuePair doOptimize() { 36 final double[] params = getStartPoint(); 37 final double[] res = computeResiduals(computeObjectiveValue(params)); 38 setCost(computeCost(res)); 39 return new PointVectorValuePair(params, null); 40 } 41 }; 42 } 43 44 @Test 45 public void testGetChiSquare() throws IOException { 46 final StatisticalReferenceDataset dataset 47 = StatisticalReferenceDatasetFactory.createKirby2(); 48 final AbstractLeastSquaresOptimizer optimizer = createOptimizer(); 49 final double[] a = dataset.getParameters(); 50 final double[] y = dataset.getData()[1]; 51 final double[] w = new double[y.length]; 52 Arrays.fill(w, 1.0); 53 54 StatisticalReferenceDataset.LeastSquaresProblem problem 55 = dataset.getLeastSquaresProblem(); 56 57 optimizer.optimize(new MaxEval(1), 58 problem.getModelFunction(), 59 problem.getModelFunctionJacobian(), 60 new Target(y), 61 new Weight(w), 62 new InitialGuess(a)); 63 final double expected = dataset.getResidualSumOfSquares(); 64 final double actual = optimizer.getChiSquare(); 65 Assert.assertEquals(dataset.getName(), expected, actual, 66 1E-11 * expected); 67 } 68 69 @Test 70 public void testGetRMS() throws IOException { 71 final StatisticalReferenceDataset dataset 72 = StatisticalReferenceDatasetFactory.createKirby2(); 73 final AbstractLeastSquaresOptimizer optimizer = createOptimizer(); 74 final double[] a = dataset.getParameters(); 75 final double[] y = dataset.getData()[1]; 76 final double[] w = new double[y.length]; 77 Arrays.fill(w, 1); 78 79 StatisticalReferenceDataset.LeastSquaresProblem problem 80 = dataset.getLeastSquaresProblem(); 81 82 optimizer.optimize(new MaxEval(1), 83 problem.getModelFunction(), 84 problem.getModelFunctionJacobian(), 85 new Target(y), 86 new Weight(w), 87 new InitialGuess(a)); 88 89 final double expected = FastMath 90 .sqrt(dataset.getResidualSumOfSquares() / 91 dataset.getNumObservations()); 92 final double actual = optimizer.getRMS(); 93 Assert.assertEquals(dataset.getName(), expected, actual, 94 1E-11 * expected); 95 } 96 97 @Test 98 public void testComputeSigma() throws IOException { 99 final StatisticalReferenceDataset dataset 100 = StatisticalReferenceDatasetFactory.createKirby2(); 101 final AbstractLeastSquaresOptimizer optimizer = createOptimizer(); 102 final double[] a = dataset.getParameters(); 103 final double[] y = dataset.getData()[1]; 104 final double[] w = new double[y.length]; 105 Arrays.fill(w, 1); 106 107 StatisticalReferenceDataset.LeastSquaresProblem problem 108 = dataset.getLeastSquaresProblem(); 109 110 final PointVectorValuePair optimum 111 = optimizer.optimize(new MaxEval(1), 112 problem.getModelFunction(), 113 problem.getModelFunctionJacobian(), 114 new Target(y), 115 new Weight(w), 116 new InitialGuess(a)); 117 118 final double[] sig = optimizer.computeSigma(optimum.getPoint(), 1e-14); 119 120 final int dof = y.length - a.length; 121 final double[] expected = dataset.getParametersStandardDeviations(); 122 for (int i = 0; i < sig.length; i++) { 123 final double actual = FastMath.sqrt(optimizer.getChiSquare() / dof) * sig[i]; 124 Assert.assertEquals(dataset.getName() + ", parameter #" + i, 125 expected[i], actual, 1e-6 * expected[i]); 126 } 127 } 128 } 129