View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math4.legacy.fitting.leastsquares;
18  
19  import org.apache.commons.math4.legacy.analysis.MultivariateMatrixFunction;
20  import org.apache.commons.math4.legacy.analysis.MultivariateVectorFunction;
21  import org.apache.commons.math4.core.jdkmath.JdkMath;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  class BevingtonProblem {
27      private List<Double> time;
28      private List<Double> count;
29  
30      BevingtonProblem() {
31          time = new ArrayList<>();
32          count = new ArrayList<>();
33      }
34  
35      public void addPoint(double t, double c) {
36          time.add(t);
37          count.add(c);
38      }
39  
40      public MultivariateVectorFunction getModelFunction() {
41          return new MultivariateVectorFunction() {
42              @Override
43              public double[] value(double[] params) {
44                  double[] values = new double[time.size()];
45                  for (int i = 0; i < values.length; ++i) {
46                      final double t = time.get(i);
47                      values[i] = params[0] +
48                          params[1] * JdkMath.exp(-t / params[3]) +
49                          params[2] * JdkMath.exp(-t / params[4]);
50                  }
51                  return values;
52              }
53          };
54      }
55  
56      public MultivariateMatrixFunction getModelFunctionJacobian() {
57          return new MultivariateMatrixFunction() {
58              @Override
59              public double[][] value(double[] params) {
60                  double[][] jacobian = new double[time.size()][5];
61  
62                  for (int i = 0; i < jacobian.length; ++i) {
63                      final double t = time.get(i);
64                      jacobian[i][0] = 1;
65  
66                      final double p3 =  params[3];
67                      final double p4 =  params[4];
68                      final double tOp3 = t / p3;
69                      final double tOp4 = t / p4;
70                      jacobian[i][1] = JdkMath.exp(-tOp3);
71                      jacobian[i][2] = JdkMath.exp(-tOp4);
72                      jacobian[i][3] = params[1] * JdkMath.exp(-tOp3) * tOp3 / p3;
73                      jacobian[i][4] = params[2] * JdkMath.exp(-tOp4) * tOp4 / p4;
74                  }
75                  return jacobian;
76              }
77          };
78      }
79  }