PProbabilityEstimator TProbabilityEstimatorConstructor_m::operator()(PDistribution frequencies, PDistribution apriori, PExampleGenerator, const long &weightID, const int &) const { TProbabilityEstimator_FromDistribution *pefd = mlnew TProbabilityEstimator_FromDistribution(CLONE(TDistribution, frequencies)); PProbabilityEstimator estimator = pefd; TDiscDistribution *ddist = pefd->probabilities.AS(TDiscDistribution); if (ddist && (ddist->cases > 1e-20) && apriori) { TDiscDistribution *dapriori = apriori.AS(TDiscDistribution); if (!dapriori || (dapriori->abs < 1e-20)) raiseError("invalid apriori distribution"); float mabs = m/dapriori->abs; const float &abs = ddist->abs; const float &cases = ddist->cases; const float div = cases + m; if ((abs==cases) || !renormalize) { int i = 0; for(TDiscDistribution::iterator di(ddist->begin()), de(ddist->end()), ai(dapriori->begin()); di != de; di++, ai++, i++) ddist->setint(i, (*di+*ai*mabs)/div); } else { int i = 0; for(TDiscDistribution::iterator di(ddist->begin()), de(ddist->end()), ai(dapriori->begin()); di != de; di++, ai++, i++) ddist->setint(i, (*di / abs * cases + *ai*mabs)/div); } } else pefd->probabilities->normalize(); return estimator; }
PProbabilityEstimator TProbabilityEstimatorConstructor_Laplace::operator()(PDistribution frequencies, PDistribution, PExampleGenerator, const long &, const int &) const { TProbabilityEstimator_FromDistribution *pefd = mlnew TProbabilityEstimator_FromDistribution(CLONE(TDistribution, frequencies)); PProbabilityEstimator estimator = pefd; TDiscDistribution *ddist = pefd->probabilities.AS(TDiscDistribution); if (ddist) { const float &abs = ddist->abs; const float &cases = ddist->cases; const float div = cases + l * ddist->noOfElements(); int i = 0; if (div) { if ((cases == abs) || !renormalize || (abs<1e-20)) PITERATE(TDiscDistribution, di, ddist) ddist->setint(i++, (*di + l) / div); else PITERATE(TDiscDistribution, di, ddist) ddist->setint(i++, (*di / abs * cases + l) / div); } else pefd->probabilities->normalize(); } else pefd->probabilities->normalize(); return estimator; }