1 | #ifndef theplu_yat_classifier_perceptron |
---|
2 | #define theplu_yat_classifier_perceptron |
---|
3 | |
---|
4 | // $Id: Perceptron.h 3709 2017-11-08 22:49:06Z 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$ y = \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 | \param x each row corresponds to a data point and each column a |
---|
89 | feature. |
---|
90 | \param target describes the class label for each data |
---|
91 | point. Data that has binary set are trained to output 1. |
---|
92 | */ |
---|
93 | void train(const utility::Matrix& x, const Target& target); |
---|
94 | |
---|
95 | /** |
---|
96 | \return trained weight vector, \f$w\f$. |
---|
97 | */ |
---|
98 | const utility::Vector& weight(void) const; |
---|
99 | private: |
---|
100 | utility::Vector weight_; |
---|
101 | utility::Matrix covariance_; |
---|
102 | double margin(size_t i, double alpha) const; |
---|
103 | // using compiler generated copy |
---|
104 | //Perceptron(const Perceptron&) |
---|
105 | //Perceptron& operator=(const Perceptron&) |
---|
106 | }; |
---|
107 | |
---|
108 | |
---|
109 | }}}// end of namespace classifier, yat, and theplu |
---|
110 | #endif |
---|