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 java.io.BufferedReader;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  
24  import org.apache.commons.math4.core.jdkmath.JdkMath;
25  
26  /**
27   * A factory to create instances of {@link StatisticalReferenceDataset} from
28   * available resources.
29   */
30  public final class StatisticalReferenceDatasetFactory {
31  
32      private StatisticalReferenceDatasetFactory() {
33          // Do nothing
34      }
35  
36      /**
37       * Creates a new buffered reader from the specified resource name.
38       *
39       * @param name the name of the resource
40       * @return a buffered reader
41       * @throws IOException if an I/O error occured
42       */
43      public static BufferedReader createBufferedReaderFromResource(final String name)
44          throws IOException {
45          final InputStream resourceAsStream;
46          resourceAsStream = StatisticalReferenceDatasetFactory.class
47              .getResourceAsStream(name);
48          if (resourceAsStream == null) {
49              throw new IOException("could not find resource " + name);
50          }
51          return new BufferedReader(new InputStreamReader(resourceAsStream));
52      }
53  
54      public static StatisticalReferenceDataset createKirby2()
55          throws IOException {
56          final BufferedReader in = createBufferedReaderFromResource("Kirby2.dat");
57          StatisticalReferenceDataset dataset = null;
58          try {
59              dataset = new StatisticalReferenceDataset(in) {
60  
61                  @Override
62                  public double getModelValue(final double x, final double[] a) {
63                      final double p = a[0] + x * (a[1] + x * a[2]);
64                      final double q = 1.0 + x * (a[3] + x * a[4]);
65                      return p / q;
66                  }
67  
68                  @Override
69                  public double[] getModelDerivatives(final double x,
70                                                      final double[] a) {
71                      final double[] dy = new double[5];
72                      final double p = a[0] + x * (a[1] + x * a[2]);
73                      final double q = 1.0 + x * (a[3] + x * a[4]);
74                      dy[0] = 1.0 / q;
75                      dy[1] = x / q;
76                      dy[2] = x * dy[1];
77                      dy[3] = -x * p / (q * q);
78                      dy[4] = x * dy[3];
79                      return dy;
80                  }
81              };
82          } finally {
83              in.close();
84          }
85          return dataset;
86      }
87  
88      public static StatisticalReferenceDataset createHahn1()
89          throws IOException {
90          final BufferedReader in = createBufferedReaderFromResource("Hahn1.dat");
91          StatisticalReferenceDataset dataset = null;
92          try {
93              dataset = new StatisticalReferenceDataset(in) {
94  
95                  @Override
96                  public double getModelValue(final double x, final double[] a) {
97                      final double p = a[0] + x * (a[1] + x * (a[2] + x * a[3]));
98                      final double q = 1.0 + x * (a[4] + x * (a[5] + x * a[6]));
99                      return p / q;
100                 }
101 
102                 @Override
103                 public double[] getModelDerivatives(final double x,
104                                                     final double[] a) {
105                     final double[] dy = new double[7];
106                     final double p = a[0] + x * (a[1] + x * (a[2] + x * a[3]));
107                     final double q = 1.0 + x * (a[4] + x * (a[5] + x * a[6]));
108                     dy[0] = 1.0 / q;
109                     dy[1] = x * dy[0];
110                     dy[2] = x * dy[1];
111                     dy[3] = x * dy[2];
112                     dy[4] = -x * p / (q * q);
113                     dy[5] = x * dy[4];
114                     dy[6] = x * dy[5];
115                     return dy;
116                 }
117             };
118         } finally {
119             in.close();
120         }
121         return dataset;
122     }
123 
124     public static StatisticalReferenceDataset createMGH17()
125         throws IOException {
126         final BufferedReader in = createBufferedReaderFromResource("MGH17.dat");
127         StatisticalReferenceDataset dataset = null;
128         try {
129             dataset = new StatisticalReferenceDataset(in) {
130 
131                 @Override
132                 public double getModelValue(final double x, final double[] a) {
133                     return a[0] + a[1] * JdkMath.exp(-a[3] * x) + a[2] *
134                            JdkMath.exp(-a[4] * x);
135                 }
136 
137                 @Override
138                 public double[] getModelDerivatives(final double x,
139                                                     final double[] a) {
140                     final double[] dy = new double[5];
141                     dy[0] = 1.0;
142                     dy[1] = JdkMath.exp(-x * a[3]);
143                     dy[2] = JdkMath.exp(-x * a[4]);
144                     dy[3] = -x * a[1] * dy[1];
145                     dy[4] = -x * a[2] * dy[2];
146                     return dy;
147                 }
148             };
149         } finally {
150             in.close();
151         }
152         return dataset;
153     }
154 
155     public static StatisticalReferenceDataset createLanczos1()
156         throws IOException {
157         final BufferedReader in =
158             createBufferedReaderFromResource("Lanczos1.dat");
159         StatisticalReferenceDataset dataset = null;
160         try {
161             dataset = new StatisticalReferenceDataset(in) {
162 
163                 @Override
164                 public double getModelValue(final double x, final double[] a) {
165                     System.out.println(a[0]+", "+a[1]+", "+a[2]+", "+a[3]+", "+a[4]+", "+a[5]);
166                     return a[0] * JdkMath.exp(-a[3] * x) +
167                            a[1] * JdkMath.exp(-a[4] * x) +
168                            a[2] * JdkMath.exp(-a[5] * x);
169                 }
170 
171                 @Override
172                 public double[] getModelDerivatives(final double x,
173                     final double[] a) {
174                     final double[] dy = new double[6];
175                     dy[0] = JdkMath.exp(-x * a[3]);
176                     dy[1] = JdkMath.exp(-x * a[4]);
177                     dy[2] = JdkMath.exp(-x * a[5]);
178                     dy[3] = -x * a[0] * dy[0];
179                     dy[4] = -x * a[1] * dy[1];
180                     dy[5] = -x * a[2] * dy[2];
181                     return dy;
182                 }
183             };
184         } finally {
185             in.close();
186         }
187         return dataset;
188     }
189 
190     /**
191      * Returns an array with all available reference datasets.
192      *
193      * @return the array of datasets
194      * @throws IOException if an I/O error occurs
195      */
196     public StatisticalReferenceDataset[] createAll()
197         throws IOException {
198         return new StatisticalReferenceDataset[] {
199             createKirby2(), createMGH17()
200         };
201     }
202 }