1 | #ifndef theplu_yat_classifier_perceptron |
---|
2 | #define theplu_yat_classifier_perceptron |
---|
3 | |
---|
4 | // $Id: Perceptron.h 4052 2021-03-26 02:26:19Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2017 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://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 <http://www.gnu.org/licenses/>. |
---|
23 | */ |
---|
24 | |
---|
25 | #include <yat/utility/Matrix.h> |
---|
26 | #include <yat/utility/Vector.h> |
---|
27 | |
---|
28 | namespace theplu { |
---|
29 | namespace yat { |
---|
30 | namespace classifier { |
---|
31 | |
---|
32 | class Target; |
---|
33 | |
---|
34 | /** |
---|
35 | \brief A Single-layer Perceptron |
---|
36 | |
---|
37 | Data are modeled as \f$ \mu = \frac{1}{1 + \exp(-wx)} \f$ |
---|
38 | |
---|
39 | \since New in yat 0.16 |
---|
40 | */ |
---|
41 | class Perceptron |
---|
42 | { |
---|
43 | public: |
---|
44 | /** |
---|
45 | Estimated covariance of weight vector. |
---|
46 | |
---|
47 | Covariance is estimated as \f$ \left(X'SX\right)^{-1} \f$ where |
---|
48 | \f$ S \f$ is a diagnoal matrix with \f$ S_{ii} = \mu_i |
---|
49 | (1-\mu_i) \f$ where \f$ \mu_i \f$ is the expected value of |
---|
50 | sample i. |
---|
51 | */ |
---|
52 | const utility::Matrix& covariance(void) const; |
---|
53 | |
---|
54 | /** |
---|
55 | The odds ratio is defined as \f$ \textrm{OR} = \exp(w_i) \f$ |
---|
56 | */ |
---|
57 | double oddsratio(size_t i) const; |
---|
58 | |
---|
59 | /** |
---|
60 | The lower end of the confidence interval of estimation of |
---|
61 | oddsratio \a i with confidence 1 - \a alpha. The true value is |
---|
62 | estimated to be within confidence interval with probability 1 - |
---|
63 | \a alpha. |
---|
64 | */ |
---|
65 | double oddsratio_lower_CI(size_t i, double alpha=0.05) const; |
---|
66 | |
---|
67 | /** |
---|
68 | The lower end of the confidence interval of estimation of |
---|
69 | oddsratio \a i with confidence 1 - \a alpha. The true value is |
---|
70 | estimated to be within confidence interval with probability 1 - |
---|
71 | \a alpha. |
---|
72 | */ |
---|
73 | double oddsratio_upper_CI(size_t i, double alpha=0.05) const; |
---|
74 | |
---|
75 | /** |
---|
76 | \return p-value that for null hypothesis that ith weight is zero |
---|
77 | */ |
---|
78 | double p_value(size_t i) const; |
---|
79 | |
---|
80 | /** |
---|
81 | \return \f$ \frac{1}{1 + \exp(-wx)} \f$ |
---|
82 | */ |
---|
83 | double predict(const utility::VectorBase& x) const; |
---|
84 | |
---|
85 | /** |
---|
86 | \brief train the model |
---|
87 | |
---|
88 | Model parameters, \f$w\f$, are calculated such that the |
---|
89 | log-likelihood, |
---|
90 | \f$ \log \mathcal{L} = \sum y_i \log \left(\mu_i\right) + |
---|
91 | (1-y_i) \log \left(1 - \mu_i\right) \f$, |
---|
92 | is maximized. |
---|
93 | |
---|
94 | \param x each row corresponds to a data point and each column a |
---|
95 | feature. |
---|
96 | \param target describes the class label for each data |
---|
97 | point. Data that has binary set are trained to output 1. |
---|
98 | */ |
---|
99 | void train(const utility::Matrix& x, const Target& target); |
---|
100 | |
---|
101 | /** |
---|
102 | \return trained weight vector, \f$w\f$. |
---|
103 | */ |
---|
104 | const utility::Vector& weight(void) const; |
---|
105 | private: |
---|
106 | utility::Vector weight_; |
---|
107 | utility::Matrix covariance_; |
---|
108 | double margin(size_t i, double alpha) const; |
---|
109 | // using compiler generated copy |
---|
110 | //Perceptron(const Perceptron&) |
---|
111 | //Perceptron& operator=(const Perceptron&) |
---|
112 | }; |
---|
113 | |
---|
114 | |
---|
115 | }}}// end of namespace classifier, yat, and theplu |
---|
116 | #endif |
---|