comparison bayesianArraySrc/BayesianArrayStructure.cpp @ 4:45b5cf9be377

checking through Bayesian update procedure - working without cross update method.
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Thu, 02 Feb 2012 12:13:44 +0000
parents 5e188c0035b6
children 5ef00d1dfe68
comparison
equal deleted inserted replaced
3:5e188c0035b6 4:45b5cf9be377
13 #include "BayesianArrayStructure.h" 13 #include "BayesianArrayStructure.h"
14 14
15 BayesianArrayStructure::BayesianArrayStructure(){ 15 BayesianArrayStructure::BayesianArrayStructure(){
16 printf("Bayesian structure: DeFault constructor called"); 16 printf("Bayesian structure: DeFault constructor called");
17 17
18 usingIntegratedTempoEstimate = true;// use max index 18 usingIntegratedTempoEstimate = false;// use max index
19 19
20 relativeSpeedLikelihoodStdDev = 5.0; 20 relativeSpeedLikelihoodStdDev = 5.0;
21 21
22 prior.createVector(1); 22 prior.createVector(1);
23 likelihood.createVector(1); 23 likelihood.createVector(1);
27 speedEstimate = speedPriorValue; 27 speedEstimate = speedPriorValue;
28 28
29 lastEventTime = 0;//ofGetElapsedTimeMillis(); 29 lastEventTime = 0;//ofGetElapsedTimeMillis();
30 30
31 tmpBestEstimate = 0; 31 tmpBestEstimate = 0;
32 crossUpdateTimeThreshold = 60; 32 crossUpdateTimeThreshold = 60000;
33 priorWidth = 20; 33 priorWidth = 20;
34 34
35 } 35 }
36 36
37 BayesianArrayStructure::BayesianArrayStructure(int length){ 37 BayesianArrayStructure::BayesianArrayStructure(int length){
101 void BayesianArrayStructure::setSpeedPrior(double f){ 101 void BayesianArrayStructure::setSpeedPrior(double f){
102 speedPriorValue = f; 102 speedPriorValue = f;
103 int index = relativeSpeedPosterior.getRealTermsAsIndex(speedPriorValue); 103 int index = relativeSpeedPosterior.getRealTermsAsIndex(speedPriorValue);
104 relativeSpeedPosterior.zero(); 104 relativeSpeedPosterior.zero();
105 relativeSpeedPosterior.addGaussianShape(index, priorWidth, 0.8); 105 relativeSpeedPosterior.addGaussianShape(index, priorWidth, 0.8);
106 printf("speed adding to index for 1 = %f\n", relativeSpeedPosterior.getRealTermsAsIndex(1));
107 relativeSpeedPosterior.addToIndex(relativeSpeedPosterior.getRealTermsAsIndex(1), 1);
108
106 relativeSpeedPosterior.renormalise(); 109 relativeSpeedPosterior.renormalise();
107 relativeSpeedPosterior.getMaximum(); 110 relativeSpeedPosterior.getMaximum();
108 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior); 111 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
109 printf("BAYES STRUCTU ' SPEED PRIOR %f . index %i\n", speedPriorValue, index); 112 printf("BAYES STRUCTU ' SPEED PRIOR %f . index %i\n", speedPriorValue, index);
110 113
186 relativeSpeedPosterior.zero(); 189 relativeSpeedPosterior.zero();
187 relativeSpeedLikelihood.zero(); 190 relativeSpeedLikelihood.zero();
188 191
189 } 192 }
190 193
191 194 /*
192 void BayesianArrayStructure::updateTmpBestEstimate(const double& timeDifference){ 195 void BayesianArrayStructure::updateTmpBestEstimate(const double& timeDifference){
193 //input is the time since the start of playing 196 //input is the time since the start of playing
194 // double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;//lastBestEstimateUpdateTime; 197 // double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;//lastBestEstimateUpdateTime;
195 double timeDiff = timeDifference; 198 double timeDiff = timeDifference;
196 if (*realTimeMode) 199 if (*realTimeMode)
200 tmpBestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate); 203 tmpBestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
201 // 204 //
202 //printf("tmp best %f and best %f time diff %f posterior MAP %f at speed %f\n", 0Estimate, bestEstimate, timeDifference, posterior.getIndexInRealTerms(posterior.MAPestimate), relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate)); 205 //printf("tmp best %f and best %f time diff %f posterior MAP %f at speed %f\n", 0Estimate, bestEstimate, timeDifference, posterior.getIndexInRealTerms(posterior.MAPestimate), relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate));
203 //lastBestEstimateUpdateTime = ofGetElapsedTimeMillis(); 206 //lastBestEstimateUpdateTime = ofGetElapsedTimeMillis();
204 } 207 }
205 208 */
209
206 void BayesianArrayStructure::updateBestEstimate(const double& timeDifference){ 210 void BayesianArrayStructure::updateBestEstimate(const double& timeDifference){
207 // double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;// 211 // double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;//
208 double tmp = bestEstimate; 212 double tmp = bestEstimate;
209 printf("post offest %i\n", posterior.offset); 213 printf("best est routine: posterior offset %f\n", posterior.offset);
210 214
211 double timeDiff = timeDifference; 215 double timeDiff = timeDifference;
212 216
213 //Using timedifferencfe here will make it go wrong. Is time since beginning of playing 217 //Using timedifferencfe here will make it go wrong. Is time since beginning of playing
214 218
215 if (*realTimeMode) 219 if (*realTimeMode)
216 timeDiff = ofGetElapsedTimeMillis() - lastBestEstimateUpdateTime; 220 timeDiff = ofGetElapsedTimeMillis() - lastBestEstimateUpdateTime;
217 221
218 //lastbest is time we started playing 222 //lastbest is time we started playing
219 223 /*
220 if (usingIntegratedTempoEstimate) 224 if (usingIntegratedTempoEstimate)
221 speedEstimateIndex = relativeSpeedPosterior.getIntegratedEstimate(); 225 speedEstimateIndex = relativeSpeedPosterior.getIntegratedEstimate();
222 else 226 else
223 speedEstimateIndex = relativeSpeedPosterior.MAPestimate; 227 speedEstimateIndex = relativeSpeedPosterior.getMAPestimate();
228 */
229 speedEstimateIndex = getSpeedEstimateIndex();
224 230
225 speedEstimate = relativeSpeedPosterior.getIndexInRealTerms(speedEstimateIndex); 231 speedEstimate = relativeSpeedPosterior.getIndexInRealTerms(speedEstimateIndex);
226 bestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*speedEstimate; 232 bestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*speedEstimate;
227 233
228 printf("best estimate update from %f to %f; time diff %f MAP %i = %f ms speed %f\n", tmp, bestEstimate, timeDiff, 234 printf("best estimate update from %f to %f; time diff %f MAP %i = %f ms speed index %f est %f SpeedxTime %f\n", tmp, bestEstimate, timeDiff,
229 posterior.MAPestimate, posterior.getIndexInRealTerms(posterior.MAPestimate), speedEstimate); 235 posterior.MAPestimate, posterior.getIndexInRealTerms(posterior.MAPestimate), speedEstimateIndex, speedEstimate, timeDiff*speedEstimate);
230 } 236 }
231 237
232 void BayesianArrayStructure::calculatePosterior(){ 238 void BayesianArrayStructure::calculatePosterior(){
233 //posterior.doProduct(prior, likelihood); 239 //posterior.doProduct(prior, likelihood);
240 assert(posterior.offset == prior.offset);
234 241
235 int i; 242 int i;
236 for (i = 0;i < posterior.length;i++){ 243 for (i = 0;i < posterior.length;i++){
237 posterior.array[i] = likelihood.array[i] * prior.array[i]; 244 posterior.array[i] = likelihood.array[i] * prior.array[i];
238 } 245 }
240 posterior.renormalise(); 247 posterior.renormalise();
241 248
242 } 249 }
243 250
244 251
252 double BayesianArrayStructure::getSpeedEstimateIndex(){
253 if (usingIntegratedTempoEstimate)
254 return relativeSpeedPosterior.getIntegratedEstimate();
255 else
256 return relativeSpeedPosterior.getMAPestimate();
257 }
245 258
246 259
247 void BayesianArrayStructure::setNewDistributionOffsets(const double& newOffset){ 260 void BayesianArrayStructure::setNewDistributionOffsets(const double& newOffset){
261
262 printf("prior offset was %f now %f\n", prior.offset, newOffset);
263
248 prior.offset = newOffset; 264 prior.offset = newOffset;
249 likelihood.offset = newOffset; 265 likelihood.offset = newOffset;
266
250 //posterior.offset = newOffset; 267 //posterior.offset = newOffset;
251 } 268 }
252 269
253 270
254 void BayesianArrayStructure::crossUpdateArrays(DynamicVector& position, DynamicVector& speed, double timeDifference){ 271 void BayesianArrayStructure::crossUpdateArrays(DynamicVector& position, DynamicVector& speed, double timeDifference){
256 273
257 // printf("time difference %f, ", timeDifference); 274 // printf("time difference %f, ", timeDifference);
258 275
259 double timeDifferenceInPositionVectorUnits = timeDifference / prior.scalar; 276 double timeDifferenceInPositionVectorUnits = timeDifference / prior.scalar;
260 277
261 278 printf("CROSS UPDATE time diff %f ms is %f units; ", timeDifference, timeDifferenceInPositionVectorUnits);
262 prior.zero();//kill prior 279 prior.zero();//kill prior
263 calculateNewPriorOffset(timeDifference);//set new prior offset here 280
281 // calculateNewPriorOffset(timeDifference);//dioesnt do anything
282
283 printf("new prior offset %f and post offset %f\n", prior.offset, posterior.offset);
264 284
265 if (timeDifferenceInPositionVectorUnits > crossUpdateTimeThreshold) 285 if (timeDifferenceInPositionVectorUnits > crossUpdateTimeThreshold)
266 complexCrossUpdate(timeDifferenceInPositionVectorUnits); 286 complexCrossUpdate(timeDifferenceInPositionVectorUnits);
267 else 287 else
268 translateByMaximumSpeed(timeDifferenceInPositionVectorUnits); 288 translateByMaximumSpeed(timeDifferenceInPositionVectorUnits);
272 prior.renormalise(); 292 prior.renormalise();
273 293
274 } 294 }
275 295
276 void BayesianArrayStructure::complexCrossUpdate(const double& timeDifferenceInPositionVectorUnits){ 296 void BayesianArrayStructure::complexCrossUpdate(const double& timeDifferenceInPositionVectorUnits){
297
277 int distanceMoved, newPriorIndex; 298 int distanceMoved, newPriorIndex;
278 299
279 double speedValue = relativeSpeedPosterior.offset; 300 double speedValue = relativeSpeedPosterior.offset;
280 301
281 for (int i = 0;i < relativeSpeedPosterior.arraySize;i++){ 302 for (int i = 0;i < relativeSpeedPosterior.arraySize;i++){
284 305
285 //so we have moved 306 //so we have moved
286 distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value 307 distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value
287 308
288 if (relativeSpeedPosterior.array[i] != 0){ 309 if (relativeSpeedPosterior.array[i] != 0){
310
289 double speedContribution = relativeSpeedPosterior.array[i]; 311 double speedContribution = relativeSpeedPosterior.array[i];
290 // printf("speed [%i] gives %f moved %i in %f units \n", i, speedValue, distanceMoved, timeDifferenceInPositionVectorUnits); 312
291 313 // printf("speed [%i](val[%f]) gives %f moved %i in %f units \n", i, relativeSpeedPosterior.array[i], speedValue, distanceMoved, timeDifferenceInPositionVectorUnits);
292 newPriorIndex = posterior.offset - prior.offset + distanceMoved; 314
293 315 //1/2/12 deleted line
294 for (int postIndex = 0;postIndex < posterior.arraySize;postIndex++){ 316 newPriorIndex = posterior.offset - prior.offset + distanceMoved;//i.e. where post[0] goes to in terms of prior at this speed
317 int postIndex = 0;
318 while (postIndex < posterior.arraySize && newPriorIndex < prior.arraySize){
319
320 //did use a for loop
321 // for (postIndex = 0;postIndex < posterior.arraySize;postIndex++){
295 //old posterior contributing to new prior 322 //old posterior contributing to new prior
296 // newPriorIndex = postIndex + posterior.offset - prior.offset + distanceMoved; 323
324 //would use this method
325 //newPriorIndex = postIndex + posterior.offset - prior.offset + distanceMoved;
297 326
298 if (newPriorIndex >= 0 && newPriorIndex < prior.arraySize){ 327 if (newPriorIndex >= 0){
299 prior.addToIndex(newPriorIndex, posterior.array[postIndex]*speedContribution); 328 prior.addToIndex(newPriorIndex, posterior.array[postIndex]*speedContribution);
329 // printf("speed index %i new prior index %i post val %f speed contrib %f dist %i\n", i, newPriorIndex, posterior.array[postIndex], speedContribution, distanceMoved);
300 } 330 }
301 331 //but we actually do this for simplicity
302 newPriorIndex++;//optimised code - the commented line above explains how this works 332 newPriorIndex++;
303 }//end for 333 postIndex++;
334 }//end for. now while
304 335
305 336
306 }//if not zero 337 }//if not zero
307 speedValue += relativeSpeedPosterior.scalar; 338 speedValue += relativeSpeedPosterior.scalar;
308 //optimised line 339 //optimised line
361 relativeSpeedPosterior.copyFromDynamicVector(tmpPosteriorForStorage); 392 relativeSpeedPosterior.copyFromDynamicVector(tmpPosteriorForStorage);
362 } 393 }
363 394
364 void BayesianArrayStructure::calculateNewPriorOffset(const double& timeDifference){ 395 void BayesianArrayStructure::calculateNewPriorOffset(const double& timeDifference){
365 396
366 double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate); 397 // double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
398
399 double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(getSpeedEstimateIndex());//either integrated or MAP
367 // printf("Maxspeed is %f\n", maxSpeed); 400 // printf("Maxspeed is %f\n", maxSpeed);
368 401
369 double priorMax = posterior.getMaximum(); 402 double priorMax = posterior.getMaximum();
370 double distanceTravelled = maxSpeed * (timeDifference / prior.scalar); 403 double distanceTravelled = maxSpeed * (timeDifference / prior.scalar);
371 double newMaxLocation = posterior.MAPestimate + distanceTravelled; 404 double newMaxLocation = posterior.MAPestimate + distanceTravelled;