diff 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
line wrap: on
line diff
--- a/bayesianArraySrc/BayesianArrayStructure.cpp	Wed Feb 01 16:05:26 2012 +0000
+++ b/bayesianArraySrc/BayesianArrayStructure.cpp	Thu Feb 02 12:13:44 2012 +0000
@@ -15,7 +15,7 @@
 BayesianArrayStructure::BayesianArrayStructure(){
 	printf("Bayesian structure: DeFault constructor called");
 
-	usingIntegratedTempoEstimate = true;// use max index
+	usingIntegratedTempoEstimate = false;// use max index
 	
 	relativeSpeedLikelihoodStdDev = 5.0;
 	
@@ -29,7 +29,7 @@
 	lastEventTime = 0;//ofGetElapsedTimeMillis();
 
 	tmpBestEstimate = 0;
-	crossUpdateTimeThreshold = 60;
+	crossUpdateTimeThreshold = 60000;
 	priorWidth = 20;
 	
 }
@@ -103,6 +103,9 @@
 	int index = relativeSpeedPosterior.getRealTermsAsIndex(speedPriorValue);
 	relativeSpeedPosterior.zero();
 	relativeSpeedPosterior.addGaussianShape(index, priorWidth, 0.8);
+	printf("speed adding to index for 1 = %f\n", relativeSpeedPosterior.getRealTermsAsIndex(1));
+	relativeSpeedPosterior.addToIndex(relativeSpeedPosterior.getRealTermsAsIndex(1), 1);
+	
 	relativeSpeedPosterior.renormalise();
 	relativeSpeedPosterior.getMaximum();
 	relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
@@ -188,7 +191,7 @@
 
 }
 
-
+/*
 void BayesianArrayStructure::updateTmpBestEstimate(const double& timeDifference){
 	//input is the time since the start of playing
 //	double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;//lastBestEstimateUpdateTime;
@@ -202,11 +205,12 @@
 	//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));
 	//lastBestEstimateUpdateTime = ofGetElapsedTimeMillis();
 }
-	
+*/
+
 void BayesianArrayStructure::updateBestEstimate(const double& timeDifference){
 //	double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;//
 	double tmp = bestEstimate;
-	printf("post offest %i\n", posterior.offset);
+	printf("best est routine: posterior offset %f\n", posterior.offset);
 	
 	double timeDiff = timeDifference;
 	
@@ -216,21 +220,24 @@
 		timeDiff = ofGetElapsedTimeMillis() - lastBestEstimateUpdateTime;
 	
 	//lastbest is time we started playing
-	
+	/*
 	if (usingIntegratedTempoEstimate)
 		speedEstimateIndex = relativeSpeedPosterior.getIntegratedEstimate();
 	else
-		speedEstimateIndex = relativeSpeedPosterior.MAPestimate;
+		speedEstimateIndex = relativeSpeedPosterior.getMAPestimate();
+	*/
+	speedEstimateIndex = getSpeedEstimateIndex();
 	
 	speedEstimate = relativeSpeedPosterior.getIndexInRealTerms(speedEstimateIndex);
 	bestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*speedEstimate;
 	
-	printf("best estimate update from %f to %f; time diff %f MAP %i = %f ms speed %f\n", tmp, bestEstimate, timeDiff,
-		   posterior.MAPestimate, posterior.getIndexInRealTerms(posterior.MAPestimate), speedEstimate);
+	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,
+		   posterior.MAPestimate, posterior.getIndexInRealTerms(posterior.MAPestimate), speedEstimateIndex, speedEstimate, timeDiff*speedEstimate);
 }
 
 void BayesianArrayStructure::calculatePosterior(){
 	//posterior.doProduct(prior, likelihood);
+	assert(posterior.offset == prior.offset);
 	
 	int i;
 	for (i = 0;i < posterior.length;i++){
@@ -242,11 +249,21 @@
 }
 
 
+double  BayesianArrayStructure::getSpeedEstimateIndex(){
+	if (usingIntegratedTempoEstimate)
+		return relativeSpeedPosterior.getIntegratedEstimate();
+	else
+		return relativeSpeedPosterior.getMAPestimate();
+}
 
 
 void BayesianArrayStructure::setNewDistributionOffsets(const double& newOffset){
+	
+	printf("prior offset was %f now %f\n", prior.offset, newOffset);
+	
 	prior.offset = newOffset;
 	likelihood.offset = newOffset;
+	
 	//posterior.offset = newOffset;
 }
 
@@ -258,9 +275,12 @@
 	
 	double timeDifferenceInPositionVectorUnits = timeDifference / prior.scalar;
 	
-
+	printf("CROSS UPDATE time diff %f ms is %f units; ", timeDifference, timeDifferenceInPositionVectorUnits);
 	prior.zero();//kill prior
-	calculateNewPriorOffset(timeDifference);//set new prior offset here
+	
+//	calculateNewPriorOffset(timeDifference);//dioesnt do anything
+	
+	printf("new prior offset %f and post offset %f\n", prior.offset, posterior.offset);
 	
 	if (timeDifferenceInPositionVectorUnits > crossUpdateTimeThreshold)
 		complexCrossUpdate(timeDifferenceInPositionVectorUnits);
@@ -274,6 +294,7 @@
 }
 
 void BayesianArrayStructure::complexCrossUpdate(const double& timeDifferenceInPositionVectorUnits){
+
 	int distanceMoved, newPriorIndex;
 	
 	double speedValue = relativeSpeedPosterior.offset;
@@ -286,21 +307,31 @@
 		distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value
 	
 		if (relativeSpeedPosterior.array[i] != 0){
+			
 			double speedContribution = relativeSpeedPosterior.array[i];
-			//	printf("speed [%i] gives %f moved %i in %f units \n", i, speedValue, distanceMoved, timeDifferenceInPositionVectorUnits);
 			
-			newPriorIndex = posterior.offset - prior.offset + distanceMoved;
+		//	printf("speed [%i](val[%f]) gives %f moved %i in %f units \n", i, relativeSpeedPosterior.array[i], speedValue, distanceMoved, timeDifferenceInPositionVectorUnits);
 			
-			for (int postIndex = 0;postIndex < posterior.arraySize;postIndex++){
+			//1/2/12 deleted line
+			newPriorIndex = posterior.offset - prior.offset + distanceMoved;//i.e. where post[0] goes to in terms of prior at this speed
+			int postIndex = 0;
+			while (postIndex < posterior.arraySize && newPriorIndex < prior.arraySize){
+
+			//did use a for loop
+			//	for (postIndex = 0;postIndex < posterior.arraySize;postIndex++){
 				//old posterior contributing to new prior
-			//	newPriorIndex = postIndex + posterior.offset - prior.offset + distanceMoved;
+			
+				//would use this method
+				//newPriorIndex = postIndex + posterior.offset - prior.offset + distanceMoved;
 				
-				if (newPriorIndex >= 0 && newPriorIndex < prior.arraySize){
+				if (newPriorIndex >= 0){
 					prior.addToIndex(newPriorIndex, posterior.array[postIndex]*speedContribution);
+			//		printf("speed index %i new prior index %i post val %f speed contrib %f dist %i\n", i, newPriorIndex, posterior.array[postIndex], speedContribution, distanceMoved);
 				}
-				
-				newPriorIndex++;//optimised code - the commented line above explains how this works
-			}//end for
+				//but we actually do this for simplicity
+				newPriorIndex++;
+				postIndex++;
+			}//end for. now while
 			
 			
 		}//if not zero
@@ -363,7 +394,9 @@
 
 void BayesianArrayStructure::calculateNewPriorOffset(const double& timeDifference){
 	
-	double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
+//	double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
+	
+	double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(getSpeedEstimateIndex());//either integrated or MAP
 	//	printf("Maxspeed is %f\n", maxSpeed);
 	
 	double priorMax = posterior.getMaximum();