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