-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpolynomial_regression.hpp
97 lines (85 loc) · 4.88 KB
/
polynomial_regression.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef _POLYNOMIAL_REGRESSION_ABC_H
#define _POLYNOMIAL_REGRESSION_ABC_H __POLYNOMIAL_REGRESSION_ABC_H
/**
* PURPOSE:
*
* Polynomial Regression aims to fit a non-linear relationship to a set of
* points. It approximates this by solving a series of linear equations using
* a least-squares approach.
*
* We can model the expected value y as an nth degree polynomial, yielding
* the general polynomial regression model:
*
* y = a0 + a1 * x + a2 * x^2 + ... + an * x^n
*
* LICENSE:
*
* MIT License
*
* Copyright (c) 2020 Chris Engelsma, Audrius Meskauskas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author Chris Engelsma (initial version, all algorithm)
* @author Audrius Meskauskas (later changes starting from August 19, 2020)
*/
#include <vector>
#include <cstdlib>
#include <stdexcept>
#include <cmath>
#include <array>
#include <vector>
#include "Polynomial.hpp"
#include "internal/polynomial_regression_internals.hpp"
namespace andviane {
// Perform polynomial regression over two collections that may have different type but expecting the same size
// This function only works with containers that provide the size operator.
template<int order, typename TYPE=double, typename PRECISION=TYPE,
typename COLLECTION_X=std::vector<TYPE>, typename COLLECTION_Y=std::vector<TYPE>>
Polynomial<order, TYPE, PRECISION> polynomial_regression(const COLLECTION_X &x,
const COLLECTION_Y &y, bool compute_residual = false);
// Perform polynomial regression using X and Y iterators. This function also works with containers that do not provide
// the size operator (like std::forward_list)
template<int order, typename TYPE=double, typename PRECISION=TYPE,
typename COLLECTION_X=std::vector<TYPE>, typename COLLECTION_Y=std::vector<TYPE>>
Polynomial<order, TYPE, PRECISION> polynomial_regression(COLLECTION_X &x,
COLLECTION_Y &y,
bool compute_residual, size_t size);
// Perform polynomial regression over single collection (x simply changes 0 to N)
template<int order, typename TYPE=double, typename PRECISION=TYPE, typename COLLECTION_Y=std::vector<TYPE>>
Polynomial<order, TYPE, PRECISION> polynomial_regression(const COLLECTION_Y &y, bool compute_residual = false);
// Perform polynomial regression over single collection assuming the fixed sample size (x simply changes 0 to N)
// Assuming fixed size allows to compute the x_raised matrix only once.
template<int order, int fixed_size, typename TYPE=double, typename PRECISION=TYPE, typename COLLECTION_Y=std::vector<TYPE>>
Polynomial<order, TYPE, PRECISION> polynomial_regression_fixed(const COLLECTION_Y &y, bool compute_residual = false);
// Perform polynomial regression using X and Y iterators.
template<int n, typename TYPE=double, typename PRECISION=TYPE, typename ITERATOR_X, typename ITERATOR_Y>
Polynomial<n, TYPE, PRECISION> polynomial_regression_iter(ITERATOR_X x_iter,
ITERATOR_Y y_iter,
size_t N, bool compute_residual = false);
// Perform polynomial regression Y iterator only (X enumerates 0 to N)
template<int n, typename TYPE=double, typename PRECISION=TYPE, typename ITERATOR_Y>
Polynomial<n, TYPE, PRECISION> polynomial_regression_iter(ITERATOR_Y y_iter,
size_t N, bool compute_residual = false);
// Perform polynomial regression using Y iterator only (X enumerates 0 to N assuming the fixed sample size)
template<int n, int fixed_size, typename TYPE=double, typename PRECISION=TYPE, typename ITERATOR_Y>
Polynomial<n, TYPE, PRECISION> polynomial_regression_iter(ITERATOR_Y y_iter, bool compute_residual = false);
#include "internal/polynomial_regression_internals.tpp"
}
#endif