yat  0.21pre
NegativeBinomialMixture.h
1 #ifndef theplu_yat_statistics_negative_binomial_mixture_
2 #define theplu_yat_statistics_negative_binomial_mixture_
3 
4 // $Id: NegativeBinomialMixture.h 4192 2022-08-11 07:35:35Z peter $
5 
6 /*
7  Copyright (C) 2022 Peter Johansson
8 
9  This file is part of the yat library, https://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <https://www.gnu.org/licenses/>.
23 */
24 
25 
26 #include <yat/utility/Matrix.h>
27 #include <yat/utility/Vector.h>
28 
29 #include <map>
30 
31 namespace theplu {
32 namespace yat {
33  namespace utility {
34  class Matrix;
35  class MultiMinimizerDerivative;
36  }
37 namespace statistics {
38 
39 
51  {
52  public:
56  void add(unsigned long int k, unsigned long int n=1);
57 
61  const utility::VectorBase& alpha(void) const;
62 
66  void clear(void);
67 
71  void fit(size_t n);
72 
77  void fit(const utility::VectorBase& m,
79  const utility::VectorBase& tau);
80 
84  double logL(void) const;
85 
89  double pdf(unsigned long int k) const;
90 
94  double pdf(unsigned long int k, size_t i) const;
95 
101  const utility::VectorBase& m(void) const;
102 
108  const utility::VectorBase& tau(void) const;
109  private:
110  std::map<unsigned long int, unsigned long int> count_;
111 
112  utility::Vector tau_;
113 
114  utility::Vector m_;
115  utility::Vector alpha_;
116 
117 
118  // p = (alpha-1.0) / alpha = 1.0 - 1.0/alpha
119  double p(size_t i) const;
120  // r = m*(1-p)/p = m / alpha^2
121  double r(size_t i) const;
122 
123  void init_fit(size_t n);
124  void optimize(void);
125 
126  bool expectation_step(utility::Matrix& H);
127  bool optimize_tau(const utility::Matrix& H);
128  bool optimize_param(const utility::Matrix& H,
130 
131  double pdf(unsigned long int k, double m, double alpha) const;
132 
133  class Q
134  {
135  public:
136  Q(const std::map<unsigned long int, unsigned long int>& count,
137  const utility::VectorBase& h);
138  double operator()(const utility::VectorBase& x);
139  void operator()(const utility::VectorBase& x,
140  utility::VectorMutable& gradient);
141 
142  private:
143  std::map<unsigned long int, unsigned long int> count_;
144  const utility::VectorBase& h_;
145 
146  double log_L(double m, double alpha, unsigned long int k) const;
147 
148  // return <dlogL/dm, dlogL/dalpha>
150  calc_gradient(double m, double alpha, unsigned long int k) const;
151 
152  double calculate_dp(double p, double r, unsigned long int k) const;
153  double calculate_dr(double p, double r, unsigned long int k) const;
154  };
155  };
156 
157 }}}
158 #endif
const utility::VectorBase & tau(void) const
Definition: NegativeBinomialMixture.h:50
The Department of Theoretical Physics namespace as we define it.
const utility::VectorBase & alpha(void) const
This is the yat interface to GSL vector.
Definition: Vector.h:59
This is the yat interface to GSL vector.
Definition: VectorBase.h:55
double pdf(unsigned long int k) const
Wrapper class around gsl_multimin_fdfminimizer in GSL.
Definition: MultiMinimizerDerivative.h:43
This is the mutable interface to GSL vector.
Definition: VectorMutable.h:56
const utility::VectorBase & m(void) const
Interface to GSL matrix.
Definition: Matrix.h:104
void add(unsigned long int k, unsigned long int n=1)

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14