npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
semiMixLocalLogLikelihood.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_SEMIMIXLOCALLOGLIKELIHOOD_HH_
2 #define NPSTAT_SEMIMIXLOCALLOGLIKELIHOOD_HH_
3 
4 /*!
5 // \file semiMixLocalLogLikelihood.hh
6 //
7 // \brief Local likelihood for fitting semiparametric mixture models
8 //
9 // Author: I. Volobouev
10 //
11 // August 2022
12 */
13 
14 #include <cmath>
15 #include <vector>
16 #include <cassert>
17 #include <climits>
18 #include <stdexcept>
19 
21 #include "npstat/nm/coordAndWeight.hh"
22 #include "npstat/nm/fcnOrConst.hh"
23 
25 
26 namespace npstat {
27  // WFcn can be just a constant (convertible to double)
28  template <typename Numeric, typename WFcn>
29  inline double semiMixLocalLogLikelihood(
30  const std::vector<Numeric>& sortedCoords,
31  const AbsDistribution1D& signal,
32  const double alpha,
33  const Functor1<double, double>& bgDensityFcn,
34  const WFcn& localizingWeight,
35  const double localizingWeightXmin,
36  const double localizingWeightXmax)
37  {
38  if (localizingWeightXmin >= localizingWeightXmax) throw std::invalid_argument(
39  "In npstat::semiMixLocalLogLikelihood: invalid weight boundaries");
40 
41  const double logMin = std::log(DBL_MIN);
42 
43  long double sum = 0.0L;
44  const unsigned long sz = sortedCoords.size();
45  double x, dummy;
46  for (unsigned long i=0; i<sz; ++i)
47  {
48  Private::coordAndWeight(sortedCoords[i], &x, &dummy);
49 
50  // In what follows, the point weights (p.second) are ignored.
51  // We assume that those weights were used to solve the Fredholm
52  // equation and not imposed by some reason beyond our control.
53  if (x >= localizingWeightXmin && x <= localizingWeightXmax)
54  {
55  const double w = fcnOrConst(localizingWeight, x);
56  assert(w >= 0.0);
57  if (w > 0.0)
58  {
59  const double s = signal.density(x);
60  const double bg = bgDensityFcn(x);
61  const double d = alpha*s + (1.0-alpha)*bg;
62  const double logd = d > DBL_MIN ? std::log(d) : logMin;
63  sum += w*logd;
64  }
65  }
66  }
67  return sum;
68  }
69 }
70 
71 #endif // NPSTAT_SEMIMIXLOCALLOGLIKELIHOOD_HH_
Interface definition for 1-d continuous statistical distributions.
Interface definitions and concrete simple functors for a variety of functor-based calculations.
Templated utilities for use in various density estimation codes, etc.
Definition: AbsArrayProjector.hh:14