/* The code can be release under the GNU Lesser General Pulic License (LGPL) Please do not use fastem without understanding how it works. Please see the reference for more details Yi Zhang (University of California, Santa Cruz) Wei Xu (NEC Research America) Reference: Yi Zhang, Wei Xu, Fast Exact Maximum Likelihood Estimation for Mixture of Language Models, Poster in Proceedings of the 30st Annual International ACM SIGIR Conference on Research and Development in Information Retrieval, Amsterdam, Netherland */#include #include #include double get_time() { __int64 count; static __int64 freq=0,base=0; if (freq==0) { QueryPerformanceFrequency((LARGE_INTEGER*)&freq); QueryPerformanceCounter((LARGE_INTEGER*)&base); } QueryPerformanceCounter((LARGE_INTEGER*)&count); return (double)1000.0*(double)(count-base)/(double)freq; } void fastmix(int n,const double* p,const double* f,double* q,double alpha,double beta) { double gamma=beta/alpha; int i; int t=-1; double Sf=0,Sp=0; int start=0,end=n; for (i=0;if[s]*p[i]) { Sf_new+=f[i]; Sp_new+=p[i]; std::swap(q[a],q[j]); a++; j++; } else if (f[i]*p[s]0) { start=b+1; Sp=Sp_new; Sf=Sf_new; t=s; } else { end=a; } } while(end>start); gamma=1/gamma; double lambda=(1+gamma*Sp)/Sf; for (i=0;i=f[t]*p[i]) q[i]=lambda*f[i]-gamma*p[i]; else q[i]=0; } } void em(int n,const double* p,const double* f,double* q,double alpha,double beta) { int i=0; double Sq=0; for (i=0;i