Debian Codegraph @

src/dr/inference/model/SplineBasis.java

raw symbols SCIP
1 /*
2 * SplineBasis.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.inference.model;
27
28 import dr.math.IntegrableUnivariateFunction;
29 import dr.xml.*;
30 import no.uib.cipr.matrix.BandMatrix;
31 import no.uib.cipr.matrix.DenseVector;
32 import no.uib.cipr.matrix.Matrix;
33 import no.uib.cipr.matrix.Vector;
34
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.logging.Logger;
39
40 /**
41 * @author Marc A. Suchard
42 */
43 public class SplineBasis extends AbstractModel implements IntegrableUnivariateFunction {
44
45 public static final String SPLINE_BASIS = "splineFunction";
46 public static final String KNOT_POINTS = "knotLocations";
47 public static final String KNOT_VALUES = "knotValues";
48 public static final String DEGREE = "degree";
49
50 public SplineBasis(String name, Variable<Double> knotLocations, Variable<Double> knotValues, int degree) {
51 super(name);
52 this.knotLocations = knotLocations;
53 this.knotValues = knotValues;
54 addVariable(knotLocations);
55 addVariable(knotValues);
56 this.degree = degree;
57 updateBasis = true;
58
59 n = knotValues.getSize();
60 h = new double[n - 1];
61 deltaY = new double[n - 1];
62
63 hMatrix = new BandMatrix(n, 1, 1);
64 yByH = new DenseVector(n);
65 z = new DenseVector(n);
66
67 calculateBasis();
68
69 StringBuilder buffer = new StringBuilder();
70 buffer.append("Constructing spline basis:\n");
71 buffer.append("\tDegree: ").append(degree).append("\n");
72 buffer.append("\tRange: [").append(getLowerBound()).append(", ").append(getUpperBound()).append("\n");
73
74 Logger.getLogger("dr.math").info(buffer.toString());
75
76 }
77
78 public int getDegree() {
79 return degree;
80 }
81
82 public double evaluate(double location) {
83 calculateBasis();
84
85 int i = 0;
86 double xi = knotLocations.getValue(i);
87
88 while( xi < location) {
89 i++;
90 xi = knotLocations.getValue(i);
91 } // TODO Keep a sorted list of knotLocations for a O(log N) tree search.
92
93 double xip1 = knotLocations.getValue(i+1);
94 double yi = knotValues.getValue(i);
95 double yip1 = knotValues.getValue(i+1);
96 double zi = z.get(i);
97 double zip1 = z.get(i+1);
98 double hi = xip1 - xi;
99
100 return
101 zip1 * Math.pow(location - xi, 3) +
102 zi * Math.pow(zip1 - location, 3) +
103 (yip1/hi - hi/6*zip1)*(location - xi) +
104 (yi/hi - hi/6*zi)*(xip1 - location);
105
106 }
107
108 public double getLowerBound() {
109 return rangeMin;
110 }
111
112 public double getUpperBound() {
113 return rangeMax;
114 }
115
116 public double evaluateIntegral(double startLocation, double endLocation) {
117 calculateBasis();
118 // TODO
119 return 0;
120 }
121
122 private void calculateBasis() {
123 if (updateBasis) {
124
125 Double[] x = knotLocations.getValues();
126 Double[] y = knotValues.getValues();
127
128 rangeMin = x[0];
129 rangeMax = x[x.length-1];
130
131 for (int i = 0; i < n - 1; i++) {
132 h[i] = x[i + 1] - x[i];
133 deltaY[i] = y[i + 1] - y[i];
134 }
135
136 hMatrix.set(0, 0, 1.0); // TODO Do not need to update
137 yByH.set(0, 0.0);
138
139 for (int i = 1; i < n - 2; i++) {
140 hMatrix.set(i, i - 1, h[i - 1]);
141 hMatrix.set(i, i, 2 * (h[i] + h[i - 1]));
142 hMatrix.set(i, i + 1, h[i]);
143 yByH.set(i, 6 * (deltaY[i]/h[i] - deltaY[i - 1]/h[i - 1]) );
144 }
145
146 hMatrix.set(n - 1, n - 1, 1.0); // TODO Do not need to update
147 yByH.set(n - 1, 0.0);
148
149 hMatrix.solve(yByH, z);
150
151 updateBasis = false;
152 }
153 }
154
155 public void addModelListener(ModelListener listener) {
156
157 }
158
159 protected void handleModelChangedEvent(Model model, Object object, int index) {
160
161 }
162
163 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
164 updateBasis = true;
165 }
166
167 protected void storeState() {
168
169 }
170
171 protected void restoreState() {
172
173 }
174
175 protected void acceptState() {
176
177 }
178
179 /**
180 * The XML parser
181 */
182 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
183
184 public String getParserName() {
185 return SPLINE_BASIS;
186 }
187
188 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
189
190 int degree = xo.getAttribute(DEGREE, 3);
191 Parameter knotLocations = (Parameter) xo.getElementFirstChild(KNOT_POINTS);
192 Parameter knotValues = (Parameter) xo.getElementFirstChild(KNOT_VALUES);
193
194 if (knotLocations.getDimension() != knotValues.getDimension())
195 throw new XMLParseException("Spline basis knot locations and values must have the same dimension");
196
197 List<XY> xyList = new ArrayList<XY>();
198 for(int i=0; i<knotLocations.getDimension(); i++)
199 xyList.add(new XY(knotLocations.getParameterValue(i),knotValues.getParameterValue(i)));
200
201 Collections.sort(xyList);
202
203 for(int i=0; i<knotLocations.getDimension(); i++) {
204 XY xy = xyList.get(i);
205 knotLocations.setParameterValue(i,xy.x);
206 knotValues.setParameterValue(i,xy.y);
207 }
208
209 return new SplineBasis(xo.getId(), knotLocations, knotValues, degree);
210 }
211
212 class XY implements Comparable {
213
214 private final double x;
215 private final double y;
216
217 public XY(double x, double y) {
218 this.x = x;
219 this.y = y;
220 }
221
222 public int compareTo(Object o) {
223 double z = ((XY)o).x;
224 if (z == x)
225 throw new RuntimeException("No ties accepted in spline basis");
226 return Double.compare(x, z);
227 }
228
229 }
230
231 //************************************************************************
232 // AbstractXMLObjectParser implementation
233 //************************************************************************
234
235 public String getParserDescription() {
236 return "This element represents the a spline interpolation of discrete data.";
237 }
238
239 public Class getReturnType() {
240 return SplineBasis.class;
241 }
242
243 public XMLSyntaxRule[] getSyntaxRules() {
244 return rules;
245 }
246
247 private final XMLSyntaxRule[] rules = {
248 AttributeRule.newIntegerRule(DEGREE, true),
249 new ElementRule(KNOT_POINTS, new XMLSyntaxRule[]{
250 new ElementRule(Parameter.class)
251 }),
252 new ElementRule(KNOT_VALUES, new XMLSyntaxRule[]{
253 new ElementRule(Parameter.class)
254 }),
255 };
256 };
257
258 private final int degree;
259 private final int n;
260 private final Variable<Double> knotLocations;
261 private final Variable<Double> knotValues;
262 // private double[] splineCoefficients;
263 // private double[] storedSplineCoefficients;
264 private boolean updateBasis;
265 private double rangeMax;
266 private double rangeMin;
267
268 private final double[] h;
269 private final double[] deltaY;
270
271 private final Matrix hMatrix;
272 private final Vector yByH;
273 private final Vector z;
274
275 }