A library for working with phylogenetic and population genetic data.
v0.27.0
utils/math/regression/link.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_MATH_REGRESSION_LINK_H_
2 #define GENESIS_UTILS_MATH_REGRESSION_LINK_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <cmath>
35 #include <cstdint>
36 #include <functional>
37 
38 namespace genesis {
39 namespace utils {
40 
41 // =================================================================================================
42 // Link Functions
43 // =================================================================================================
44 
45 struct GlmLink
46 {
50  enum Link {
56  };
57 
61  Link id = kUnknown;
62 
66  std::function<double( double mu )> link;
67 
71  std::function<double( double eta )> inverse_link;
72 
76  std::function<double( double mu )> derivative;
77 
78 };
79 
83 inline bool is_defined( GlmLink const& link )
84 {
85  return link.link && link.inverse_link && link.derivative;
86 }
87 
88 // =================================================================================================
89 // Logit Link
90 // =================================================================================================
91 
98 {
99  GlmLink link;
100  link.id = GlmLink::kLogit;
101  link.link = []( double mu )
102  {
103  return std::log( mu / ( 1.0 - mu ));
104  };
105  link.inverse_link = []( double eta )
106  {
107  return std::exp( eta ) / ( 1.0 + std::exp( eta ));
108  };
109  link.derivative = []( double mu )
110  {
111  return 1.0 / ( mu * ( 1.0 - mu ));
112  };
113  return link;
114 }
115 
116 // =================================================================================================
117 // Log Link
118 // =================================================================================================
119 
126 {
127  GlmLink link;
128  link.id = GlmLink::kLog;
129  link.link = []( double mu )
130  {
131  return std::log( mu );
132  };
133  link.inverse_link = []( double eta )
134  {
135  return std::exp( eta );
136  };
137  link.derivative = []( double mu )
138  {
139  return 1.0 / mu;
140  };
141  return link;
142 }
143 
144 // =================================================================================================
145 // Identity Link
146 // =================================================================================================
147 
154 {
155  GlmLink link;
156  link.id = GlmLink::kIdentity;
157  link.link = []( double mu )
158  {
159  return mu;
160  };
161  link.inverse_link = []( double eta )
162  {
163  return eta;
164  };
165  link.derivative = []( double mu )
166  {
167  (void) mu;
168  return 1.0;
169  };
170  return link;
171 }
172 
173 // =================================================================================================
174 // Inverse Link
175 // =================================================================================================
176 
183 {
184  GlmLink link;
185  link.id = GlmLink::kInverse;
186  link.link = []( double mu )
187  {
188  return 1.0 / mu;
189  };
190  link.inverse_link = []( double eta )
191  {
192  return 1.0 / eta;
193  };
194  link.derivative = []( double mu )
195  {
196  return 1.0 / ( mu * mu );
197  };
198  return link;
199 }
200 
201 } // namespace utils
202 } // namespace genesis
203 
204 #endif // include guard
genesis::utils::is_defined
bool is_defined(GlmFamily const &family)
Check whether all necessary values and functors of a GlmFamily are set.
Definition: family.hpp:101
genesis::utils::glm_link_identity
GlmLink glm_link_identity()
Identity link functions.
Definition: utils/math/regression/link.hpp:153
genesis::utils::glm_link_logit
GlmLink glm_link_logit()
Logit link functions.
Definition: utils/math/regression/link.hpp:97
genesis::utils::glm_link_inverse
GlmLink glm_link_inverse()
Inverse link functions.
Definition: utils/math/regression/link.hpp:182
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::glm_link_log
GlmLink glm_link_log()
Log link functions.
Definition: utils/math/regression/link.hpp:125