A library for working with phylogenetic and population genetic data.
v0.27.0
utils/math/regression/helper.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_MATH_REGRESSION_HELPER_H_
2 #define GENESIS_UTILS_MATH_REGRESSION_HELPER_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 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 <cstddef>
35 #include <vector>
36 
37 namespace genesis {
38 namespace utils {
39 
40 // =================================================================================================
41 // Linear Algebra Helper Functions
42 // =================================================================================================
43 
47 struct GlmFreedom
48 {
52  size_t valid_entries = 0;
53 
57  size_t empty_strata = 0;
58 
62  size_t max_stratum = 1;
63 
67  long degrees_of_freedom( size_t rank ) const
68  {
69  auto const vi = static_cast<long>( valid_entries );
70  auto const mi = static_cast<long>( max_stratum );
71  auto const ei = static_cast<long>( empty_strata );
72  auto const ri = static_cast<long>( rank );
73 
74  return vi - mi + ei - ri;
75  }
76 };
77 
88 GlmFreedom weighted_mean_centering(
89  std::vector<double> const& y_input,
90  std::vector<double> const& weights,
91  std::vector<size_t> const& strata,
92  bool with_intercept,
93  bool centering,
94  std::vector<double>& y_output
95 );
96 
105 double weighted_residuals(
106  std::vector<double> const& x_input,
107  std::vector<double> const& y_input,
108  std::vector<double> const& weights,
109  std::vector<double>& y_output
110 );
111 
118  std::vector<double> const& x_input,
119  std::vector<double> const& weights = std::vector<double>{}
120 );
121 
129  std::vector<double> const& x_input,
130  std::vector<double> const& y_input,
131  std::vector<double> const& weights = std::vector<double>{}
132 );
133 
139 double weighted_sum(
140  std::vector<double> const& x_input,
141  std::vector<double> const& weights = std::vector<double>{}
142 );
143 
144 } // namespace utils
145 } // namespace genesis
146 
147 #endif // include guard
genesis::utils::weighted_inner_product
double weighted_inner_product(std::vector< double > const &x_input, std::vector< double > const &y_input, std::vector< double > const &weights)
(Weighted) inner product of two vectors.
Definition: utils/math/regression/helper.cpp:322
genesis::utils::weighted_mean_centering
GlmFreedom weighted_mean_centering(std::vector< double > const &y_input, std::vector< double > const &weights, std::vector< size_t > const &strata, bool with_intercept, bool centering, std::vector< double > &y_output)
(Weighted) mean and centering.
Definition: utils/math/regression/helper.cpp:71
genesis::utils::weighted_residuals
double weighted_residuals(std::vector< double > const &x_input, std::vector< double > const &y_input, std::vector< double > const &weights, std::vector< double > &y_output)
Calculate the residuals from (weighted) regression through the origin.
Definition: utils/math/regression/helper.cpp:221
genesis::utils::GlmFreedom
Internal helper structure for GLMs to calcualte the residual degrees of freedom.
Definition: utils/math/regression/helper.hpp:47
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::GlmFreedom::valid_entries
size_t valid_entries
Number of valid priors (Nu).
Definition: utils/math/regression/helper.hpp:52
genesis::utils::GlmFreedom::max_stratum
size_t max_stratum
Maximum stratum found (S).
Definition: utils/math/regression/helper.hpp:62
genesis::utils::GlmFreedom::degrees_of_freedom
long degrees_of_freedom(size_t rank) const
Calculate the degrees of freedom (dfr).
Definition: utils/math/regression/helper.hpp:67
genesis::utils::weighted_sum_of_squares
double weighted_sum_of_squares(std::vector< double > const &x_input, std::vector< double > const &weights)
(Weighted) sum of squares.
Definition: utils/math/regression/helper.cpp:290
genesis::utils::GlmFreedom::empty_strata
size_t empty_strata
Number of empty strata.
Definition: utils/math/regression/helper.hpp:57
genesis::utils::weighted_sum
double weighted_sum(std::vector< double > const &x_input, std::vector< double > const &weights)
(Weighted) sum of a vector of values.
Definition: utils/math/regression/helper.cpp:364