00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if !defined(ALIZE_ScoreWarp_cpp)
00056 #define ALIZE_ScoreWarp_cpp
00057
00058 #include <iostream>
00059 #include <fstream>
00060 #include <cstdio>
00061 #include <cassert>
00062 #include <cmath>
00063 #include "TrainTools.h"
00064 #include "ScoreWarp.h"
00065 const double PI2 = 3.14159265358979323846*2;
00066 static double x1;
00067 static double x2;
00068 void boxMullerGeneratorInit(){
00069 x1= (rand()/ (float)RAND_MAX);
00070 }
00071 double boxMullerGenerator(double mean, double cov){
00072 x2=x1;
00073 x1= (rand()/ (float)RAND_MAX);
00074 if (debug) cout <<"boxMullerGenerator x1["<<x1<<"] x2["<<x2<<"]"<<endl;
00075 double y= sqrt(-2.0*log(x1))*cos(PI2*x2);
00076 double ret= (y*cov)+mean;
00077 if (debug) cout <<"boxMullerGenerator["<<ret<<"]"<<endl;
00078 return ret;
00079 }
00080
00081
00082
00083 Histo makeGausHisto(unsigned long nbSample,double mean, double cov,unsigned long nbBins){
00084 if (verbose) cout << "makeGaussHisto, nbSample["<<nbSample<<"] mean["<<mean<<"] cov["<<cov<<"] nbBin["<<nbBins<<"]"<<endl;
00085 Histo histo(nbBins);
00086 boxMullerGeneratorInit();
00087 for (unsigned long i=0;i<nbSample;i++)
00088 histo.accumulateValue(boxMullerGenerator(mean,cov));
00089 histo.computeHisto();
00090 if (verbose){
00091 double tot=0;
00092 for (unsigned long i=0;i<histo.size();i++)
00093 tot+=histo.count(i)*(histo.higherBound(i)-histo.lowerBound(i));
00094 cout <<"makeGaussHisto tot of the final pdf["<<tot<<"]"<<endl;
00095 }
00096 return histo;
00097 }
00098
00099
00100 double centralSpace(const Histo &warpH,double a){
00101 if (a==0) return 0;
00102 unsigned long inf,sup;
00103 double t=(1.0-a)/2.0;
00104 inf=0;
00105 for (double tot=0.0;(inf<warpH.size()) && (tot<t);inf++)
00106 tot+=areaHisto(warpH,inf);
00107 sup=warpH.size()-1;
00108 for (double tot=1.0;(sup>=0) && (tot>1-t);sup--)
00109 tot-=areaHisto(warpH,sup);
00110 return (warpH.lowerBound(sup)-warpH.higherBound(inf));
00111 }
00112
00113
00114 double scoreWarping(double score, const Histo& warpH, const Histo& destH, double nonObserved,double refArea)
00115 {
00116 if (debug) cout << "scoreWarping score ["<<score<<"] nonObserved["<<nonObserved<<"] refArea["<<refArea<<"] ";
00117
00118 if (score<warpH.lowerBound(0)){
00119 double infBound=warpH.lowerBound(0)-centralSpace(warpH,refArea);
00120 return destH.lowerBound(0)-(linearInterpolation(score,infBound,warpH.lowerBound(0))*centralSpace(destH,refArea));
00121 }
00122 if (score>warpH.higherBound(warpH.size()-1)){
00123 double supBound=warpH.higherBound(warpH.size()-1)+centralSpace(warpH,refArea);
00124 return destH.higherBound(destH.size()-1)+(linearInterpolation(score,warpH.higherBound(warpH.size()-1),supBound)*centralSpace(destH,refArea));
00125 }
00126
00127 double totalWarp=0.0;
00128
00129 unsigned long idxW=0;
00130 for(;(idxW<warpH.size())&&(warpH.higherBound(idxW)<score); idxW++);
00131 if (debug) cout << " idxW["<<idxW<<"]";
00132 totalWarp=nonObserved/2;
00133 for (unsigned long idx=0;idx<idxW;idx++)
00134 totalWarp+=areaHisto(warpH,idx);
00135 double percentW=linearInterpolation(score,warpH.lowerBound(idxW),warpH.higherBound(idxW));
00136 if (debug) cout << " percentW["<<percentW<<"]";
00137 totalWarp+=areaHisto(warpH,idxW)*percentW;
00138 if (debug) cout << " totalW["<<totalWarp<<"]";
00139
00140
00141 double totalDest;
00142 unsigned long idxD;
00143 for (idxD=0,totalDest=0;(idxD<destH.size())&&(totalDest<totalWarp);idxD++)
00144 totalDest+=areaHisto(destH,idxD);
00145 if (idxD==destH.size()){
00146 idxD--;
00147 totalDest-=areaHisto(destH,idxD);
00148 }
00149 else
00150 if (idxD>0){
00151 totalDest-=areaHisto(destH,idxD);
00152 idxD--;
00153 }
00154
00155 double ret=destH.lowerBound(idxD);
00156 double percentH=linearInterpolation(totalWarp,totalDest,areaHisto(destH,idxD)+totalDest);
00157 if (debug) cout << " idxD["<<idxD<<"] TotalDet["<<totalDest<<"] PercentD["<<percentH<<"]";
00158 ret+=(destH.higherBound(idxD)-destH.lowerBound(idxD))*percentH;
00159 if (debug) cout <<" final["<<ret<<"]"<<endl;
00160 return ret;
00161 }
00162
00163
00164
00165
00166
00167
00168 double warping(double score, const Histo& warpH, const Histo& destH)
00169 {
00170 if (debug) cout << "warping input ["<<score<<"] ";
00171
00172 double totalWarp=0.0;
00173
00174 unsigned long idxW=0;
00175 for(;(idxW<warpH.size())&&(warpH.higherBound(idxW)<score); idxW++);
00176 if (debug) cout << " idxW["<<idxW<<"] ";
00177
00178 for (unsigned long idx=0;idx<idxW;idx++)
00179 totalWarp+=areaHisto(warpH,idx);
00180 double percentW=linearInterpolation(score,warpH.lowerBound(idxW),warpH.higherBound(idxW));
00181 if (debug) cout << " percentW["<<percentW<<"] ";
00182 totalWarp+=areaHisto(warpH,idxW)*percentW;
00183 if (debug) cout << " totalW["<<totalWarp<<"] ";
00184
00185
00186 double totalDest;
00187 unsigned long idxD;
00188 for (idxD=0,totalDest=0;(idxD<destH.size())&&(totalDest<totalWarp);idxD++)
00189 totalDest+=areaHisto(destH,idxD);
00190 if (idxD==destH.size()){
00191 idxD--;
00192 totalDest-=areaHisto(destH,idxD);
00193 }
00194 else
00195 if (idxD>0){
00196 totalDest-=areaHisto(destH,idxD);
00197 idxD--;
00198 }
00199
00200 double ret=destH.lowerBound(idxD);
00201 double percentH=linearInterpolation(totalWarp,totalDest,areaHisto(destH,idxD)+totalDest);
00202 if (debug) cout << " idxD["<<idxD<<"] TotalDet["<<totalDest<<"] Percent last bin["<<percentH<<"] ";
00203 ret+=(destH.higherBound(idxD)-destH.lowerBound(idxD))*percentH;
00204 if (debug) cout <<" warping output["<<ret<<"] "<<endl;
00205 return ret;
00206 }
00207
00208 #endif // !defined(ALIZE_ScoreWarp_cpp)