source: trunk/yat/classifier/NBC.h @ 1615

Last change on this file since 1615 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1#ifndef _theplu_yat_classifier_nbc_
2#define _theplu_yat_classifier_nbc_
3
4// $Id: NBC.h 1487 2008-09-10 08:41:36Z jari $
5
6/*
7  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
8  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2008 Peter Johansson, Markus Ringnér
10
11  This file is part of the yat library, http://dev.thep.lu.se/yat
12
13  The yat library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU General Public License as
15  published by the Free Software Foundation; either version 3 of the
16  License, or (at your option) any later version.
17
18  The yat library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22
23  You should have received a copy of the GNU General Public License
24  along with yat. If not, see <http://www.gnu.org/licenses/>.
25*/
26
27#include "SupervisedClassifier.h"
28#include "yat/utility/Matrix.h"
29
30namespace theplu {
31namespace yat {
32namespace classifier { 
33
34  class MatrixLookup;
35  class MatrixLookupWeighted;
36  class Target;
37
38  /**
39     @brief Naive Bayesian Classifier.
40 
41     Each class is modelled as a multinormal distribution with
42     features being independent: \f$ P(x|c) \propto \prod
43     \frac{1}{\sqrt{2\pi\sigma_i^2}} \exp \left(
44     -\frac{(x_i-\mu_i)^2}{2\sigma_i^2)} \right)\f$
45  */
46  class NBC : public SupervisedClassifier
47  {
48 
49  public:
50    ///
51    /// @brief Constructor
52    ///
53    NBC(void);
54   
55
56    ///
57    /// @brief Destructor
58    ///
59    virtual ~NBC();
60
61
62    NBC* make_classifier(void) const;
63   
64    ///
65    /// \brief Train the NBC using training data and targets.
66    ///
67    /// For each class mean and variance are estimated for each
68    /// feature (see statistics::Averager for details).
69    ///
70    /// If there is only one (or zero) samples in a class, parameters
71    /// cannot be estimated. In that case, parameters are set to NaN
72    /// for that particular class.
73    ///
74    void train(const MatrixLookup&, const Target&);
75
76    ///
77    /// \brief Train the NBC using weighted training data and
78    /// targets.
79    ///
80    /// For each class mean and variance are estimated for each
81    /// feature (see statistics::AveragerWeighted for details).
82    ///
83    /// To estimate the parameters of a class, each feature of the
84    /// class must have at least two non-zero data points. Otherwise
85    /// the parameters are set to NaN and any prediction will result
86    /// in NaN for that particular class.
87    ///
88    void train(const MatrixLookupWeighted&, const Target&);
89   
90    /**
91       \brief Predict samples using unweighted data
92
93       Each sample (column) in \a data is predicted and predictions
94       are returned in the corresponding column in passed \a
95       result. Each row in \a result corresponds to a class. The
96       prediction is the estimated probability that sample belong to
97       class \f$ j \f$:
98
99       \f$ P_j = \frac{1}{Z}\prod_i\frac{1}{\sqrt{2\pi\sigma_i^2}}
100       \exp\left(-\frac{(x_i-\mu_i)^2}{2\sigma_i^2}\right)\f$, where \f$ \mu_i
101       \f$ and \f$ \sigma_i^2 \f$ are the estimated mean and variance,
102       respectively. Z is chosen such that total probability equals unity, \f$
103       \sum P_j = 1 \f$.
104
105       \note If parameters could not be estimated during training, due
106       to lack of number of sufficient data points, the output for
107       that class is NaN and not included in calculation of
108       normalization factor \f$ Z \f$.
109    */
110    void predict(const MatrixLookup& data, utility::Matrix& result) const;
111
112    /**
113       \brief Predict samples using weighted data
114
115       Each sample (column) in \a data is predicted and predictions
116       are returned in the corresponding column in passed \a
117       result. Each row in \a result corresponds to a class. The
118       prediction is the estimated probability that sample belong to
119       class \f$ j \f$:
120
121       \f$ P_j = \frac{1}{Z} \exp\left(-N\frac{\sum
122       {w_i(x_i-\mu_i)^2}/(2\sigma_i^2)}{\sum w_i}\right)
123       \prod_i\frac{1}{\sqrt{2\pi\sigma_i^2}}\f$, where \f$ \mu_i \f$
124       and \f$ \sigma_i^2 \f$ are the estimated mean and variance,
125       respectively. Z is chosen such that total probability equals
126       unity, \f$ \sum P_j = 1 \f$.
127
128       \note If parameters could not be estimated during training, due
129       to lack of number of sufficient data points, the output for
130       that class is NaN and not included in calculation of
131       normalization factor \f$ Z \f$.
132     */
133    void predict(const MatrixLookupWeighted& data,utility::Matrix& result) const;
134
135
136  private:
137    void standardize_lnP(utility::Matrix& prediction) const;
138
139    utility::Matrix centroids_;
140    utility::Matrix sigma2_;
141
142    double sum_logsigma(size_t i) const;
143
144
145  };
146 
147}}} // of namespace classifier, yat, and theplu
148
149#endif
Note: See TracBrowser for help on using the repository browser.