Debian Codegraph @

src/dr/inference/operators/hmc/BouncyParticleOperator.java

raw symbols SCIP
1 package dr.inference.operators.hmc;
2
3 import dr.inference.hmc.GradientWrtParameterProvider;
4 import dr.inference.model.Likelihood;
5 import dr.inference.model.Parameter;
6 import dr.inference.operators.*;
7 import dr.math.MathUtils;
8 import dr.math.distributions.NormalDistribution;
9 import dr.util.Transform;
10
11 import java.util.Arrays;
12 import java.io.FileWriter;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15
16 import static org.netlib.lapack.Dlacon.j;
17
18 public class BouncyParticleOperator {
19
20 private double t; //todo randomize the length a little bit.
21 final double t0;
22 private double[] phi_w;
23 private double[] mu;
24 private double[] location;
25 private double[] v;
26 private double[][] precisionmatrix;
27 final NormalDistribution drawDistribution;
28
29 public static void main(String[] args)throws Exception{
30
31 FileWriter fw = new FileWriter("data.csv");
32 int iterations = 1000000;
33 int dim = 10;
34 double[][] result = new double[iterations][dim];
35 BouncyParticleOperator BPS1 = new BouncyParticleOperator();
36
37 for (int i = 0; i < iterations; i ++){
38
39 BPS1.bpsOneStep();
40 BPS1.t = BPS1.t0;
41 for (int j = 0; j < dim; j ++){
42 result[i][j] = BPS1.location[j];
43 }
44
45 }
46
47
48
49 try{
50 for (int i = 0; i < iterations; i ++) {
51 for (int j = 0; j < dim; j ++) {
52 fw.write(result[i][j] + ",");
53 }
54 fw.write("\n");
55 }
56 fw.close();
57
58 } catch (IOException e) {
59 e.printStackTrace();
60 }
61
62 }
63
64
65 //todo resume the constructor
66 // public BouncyParticleOperator (double[][] precisionmatrix,double[] location, double[] mu, double t) {
67 //
68 // this.location = location;
69 // this.mu = mu;
70 // this.t = t;
71 // this.precisionmatrix = precisionmatrix;
72 // this.drawDistribution = new NormalDistribution(0, Math.sqrt(1));
73 // this.v = drawV(drawDistribution, location.length);
74 // }
75
76 public BouncyParticleOperator () {
77
78
79 double[][] precisionmatrix = new double[][] {
80 {11.1016047,-12.0787204, 5.58064211,-3.1689313, 1.094961,-4.961749,5.11981149,-1.5834688, 3.5623476,-0.7376637},
81 { -12.0787204,18.3400620,-11.73593592, 0.9061316,-2.016980, 6.451601,-1.05863904, 0.2369949,-3.8070834,-0.5602705},
82 { 5.5806421,-11.7359359,28.49214394,-20.7446633, 8.302015,-4.912537,-0.03486475, 3.2872308, 3.4449720,-2.2412823},
83 {-3.1689313, 0.9061316,-20.74466331,40.7345307,-18.290551, 2.218922,-7.73442938,-4.2918288,-0.3767421,8.7189560},
84 { 1.0949613,-2.0169801, 8.30201463,-18.2905513,21.939836,-10.470282,-3.05356573, 1.8893231, 5.0052153,-2.9950543},
85 {-4.9617490, 6.4516010,-4.91253689, 2.2189222,-10.470282,20.157010,-7.83388716, 5.2679596,-12.3805480,1.4309123},
86 { 5.1198115,-1.0586390,-0.03486475,-7.7344294,-3.053566,-7.833887, 25.75186700,-9.0823931, 4.4477436,-4.5515479},
87 {-1.5834688, 0.2369949, 3.28723080,-4.2918288, 1.889323, 5.267960,-9.08239305,13.3924830,-12.0389557,0.3323174},
88 { 3.5623476,-3.8070834, 3.44497199,-0.3767421, 5.005215,-12.380548,4.44774359,-12.0389557,25.0322293,-6.4872396},
89 {-0.7376637,-0.5602705,-2.24128232, 8.7189560,-2.995054, 1.430912,-4.55154794, 0.3323174,-6.4872396,8.1990433}
90 };
91 double[] location = new double[]{1.0973772,1.0654628,0.9832338,0.9924755,1.1105650,1.1999598,0.8974071, 1.0889359,0.8875886,1.0883105};
92 double[] mu = new double[]{1.0460811,1.1343102,1.0997168,0.9632376,0.9868017,1.1777099,0.9535731,0.9826153,0.9595986,1.1414815};
93
94 double t = 2.131453;
95 double t0 = 2.131453;
96
97 this.location = location;
98 this.mu = mu;
99 this.t = t;
100 this.t0 = t0;
101 this.precisionmatrix = precisionmatrix;
102 this.drawDistribution = new NormalDistribution(0, Math.sqrt(1));
103
104 }
105
106 public double doOperation(double[] location, double[] v, double t, double[] phi_w) {
107
108
109 bpsOneStep();
110
111 return 0.0;
112 }
113
114 public void bpsOneStep() {
115
116 v = drawV(drawDistribution, location.length);
117
118 phi_w = matrixMultiplier(precisionmatrix, addArray(location, mu, true));
119 while (t > 0) {
120
121 double[] w = addArray(location, mu, true);
122 double[] phi_v = matrixMultiplier(precisionmatrix, v);
123 double w_phi_w = getDotProduct(w, phi_w);//todo multiple precision matrix should be a class.
124 double v_phi_w = getDotProduct(v, phi_w);//todo use a construct to store all of the temporary values.
125 double v_phi_v = getDotProduct(v, phi_v);
126
127 double tMin = Math.max(0.0, - v_phi_w/v_phi_v);
128
129 double U_min = energyProvider(phi_w, phi_v, w, tMin);
130
131
132 if( Double.isNaN(v_phi_w)){
133
134 System.exit(-99);
135 }
136 double bounceTime = getBounceTime(v_phi_v, v_phi_w, U_min, w_phi_w);
137
138
139 TravelTime time_to_bdry = getTimeToBoundary(location, v);
140
141
142 bpsUpdate(time_to_bdry, bounceTime, t);
143
144
145
146 }
147
148
149 }
150
151 public void bpsUpdate(TravelTime traveltime, double bouncetime, double timeRemain) {
152
153 if (timeRemain < Math.min(traveltime.minTime, bouncetime)) { //no event
154
155 location = addArray(location, getConstantVector(v, timeRemain), false);
156 t = 0.0;
157
158
159 } else if (traveltime.minTime <= bouncetime) { //against the boundary
160
161 location = addArray(location, getConstantVector(v, traveltime.minTime), false);
162 location[traveltime.minIndex] = 0.0;
163 v[traveltime.minIndex] = getConstantVector(v, -1.0)[traveltime.minIndex];
164 t -= traveltime.minTime;
165 phi_w = matrixMultiplier(precisionmatrix, addArray(location, mu, true)); //todo to deal with the duplicated code for updating phi_w
166
167
168 } else { //against the gradient
169
170 location = addArray(location, getConstantVector(v, bouncetime), false);
171 phi_w = matrixMultiplier(precisionmatrix,addArray(location, mu, true));
172 v = bounceAgainst(v, phi_w);
173 t -= bouncetime;
174
175 }
176
177 }
178
179 public double[] bounceAgainst(double[] v, double[] minusGrad) {
180
181 double[] finalV;
182 double[] verticalV;
183
184 verticalV = getConstantVector(minusGrad, getDotProduct(v, minusGrad)/getDotProduct(minusGrad, minusGrad));
185 finalV = addArray(v, getConstantVector(verticalV, -2.0), false);
186
187 return finalV;
188
189 }
190
191 public double getBounceTime(double v_phi_v, double v_phi_w, double U_min, double w_phi_w) {
192
193 double a = v_phi_v;
194 double b = 2 * v_phi_w;
195 double c = 2.0 * Math.log(1 - MathUtils.nextDouble()) - U_min + w_phi_w; //TODO CHCEK IF ROOT EXISTS
196
197 return (- b + Math.sqrt(b * b - 4 * a * c))/2/a;
198
199 }
200
201 public double energyProvider(double[] phi_w, double[] phi_v, double[] w, double t){
202
203 return getDotProduct(addArray(w, getConstantVector(v, t), false),
204 addArray(phi_w, getConstantVector(phi_v, t), false));
205
206 }
207
208 static double[] drawV(final NormalDistribution distribution, final int dim) {
209
210 double[] v = new double[dim];
211 for (int i = 0; i < dim; i++) {
212 v[i] = (Double) distribution.nextRandom();
213 }
214 return v;
215 }
216
217 public double[] matrixMultiplier(double[][] A, double[] B){
218
219 final int dim = B.length;
220 double[] mResult = new double[dim];
221
222 for (int i = 0; i < dim; i ++){
223 for (int j = 0; j< dim; j ++) {
224 mResult[i] += A[i][j] * B[j];
225 }
226
227 }
228 return mResult;
229 }
230
231 public TravelTime getTimeToBoundary(double[] location, double[] v) {
232
233 double[] travelTime = elementWiseVectorDivisionAbsValue(location, v);
234
235 int index = 0;
236
237 double minTime = Double.MAX_VALUE;
238
239 for (int i = 0; i < travelTime.length; i ++) {
240 if (travelTime[i] != 0.0 && location[i] * v[i] < 0) {
241 if (travelTime[i] < minTime) {
242 index = i;
243 minTime = travelTime[i];
244 }
245 }
246 }
247
248 return new TravelTime(travelTime, minTime, index);
249
250 }
251
252 public double[] elementWiseVectorDivisionAbsValue(double[] location, double[] y) {
253
254 final int dim = location.length;
255 double[] z = new double[dim];
256 for (int i = 0; i < dim; i++) {
257 z[i] = Math.abs(location[i] / y[i]); //todo make sure b[i] != 0
258 }
259 return z;
260 }
261
262 public static double[] addArray(double[] a, double[] b, boolean subtract) {
263
264 assert (a.length == b.length);
265 final int dim = a.length;
266
267 double result[] = new double[dim];
268 for (int i = 0; i < dim; i++) {
269 if(!subtract) {
270 result[i] = a[i] + b[i];
271 } else {
272 result[i] = a[i] - b[i];
273 }
274
275 }
276
277 return result;
278 }
279
280 public static double getDotProduct(double[] a, double[] b) { //todo get rid of code duplication
281
282 assert (a.length == b.length);
283 final int dim = a.length;
284
285 double total = 0.0;
286 for (int i = 0; i < dim; i++) {
287
288 total += a[i]*b[i];
289 }
290 return total;
291 }
292
293 public static double[] getConstantVector(double[] a, double c){
294
295 double[] cx = new double[a.length];
296 for (int i = 0; i < a.length; i ++){
297 cx[i] = a[i] * c;
298 }
299
300 return cx;
301 }
302
303 public class TravelTime {
304
305 double[] traveltime;
306 double minTime;
307 int minIndex;
308
309 public TravelTime (double[] traveltime, double minTime, int minIndex){
310 this.traveltime = traveltime;
311 this.minTime = minTime;
312 this.minIndex = minIndex;
313 }
314 }
315
316 }