TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Linear regression example using Ordinary least squares More...
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
Go to the source code of this file.
Functions | |
template<typename T > | |
std::ostream & | operator<< (std::ostream &out, std::vector< std::vector< T > > const &v) |
template<typename T > | |
std::ostream & | operator<< (std::ostream &out, std::vector< T > const &v) |
template<typename T > | |
bool | is_square (std::vector< std::vector< T > > const &A) |
template<typename T > | |
std::vector< std::vector< T > > | operator* (std::vector< std::vector< T > > const &A, std::vector< std::vector< T > > const &B) |
template<typename T > | |
std::vector< T > | operator* (std::vector< std::vector< T > > const &A, std::vector< T > const &B) |
template<typename T > | |
std::vector< float > | operator* (float const scalar, std::vector< T > const &A) |
template<typename T > | |
std::vector< float > | operator* (std::vector< T > const &A, float const scalar) |
template<typename T > | |
std::vector< float > | operator/ (std::vector< T > const &A, float const scalar) |
template<typename T > | |
std::vector< T > | operator- (std::vector< T > const &A, std::vector< T > const &B) |
template<typename T > | |
std::vector< T > | operator+ (std::vector< T > const &A, std::vector< T > const &B) |
template<typename T > | |
std::vector< std::vector< float > > | get_inverse (std::vector< std::vector< T > > const &A) |
template<typename T > | |
std::vector< std::vector< T > > | get_transpose (std::vector< std::vector< T > > const &A) |
template<typename T > | |
std::vector< float > | fit_OLS_regressor (std::vector< std::vector< T > > const &X, std::vector< T > const &Y) |
template<typename T > | |
std::vector< float > | predict_OLS_regressor (std::vector< std::vector< T > > const &X, std::vector< float > const &beta) |
void | ols_test () |
int | main () |
Linear regression example using Ordinary least squares
Program that gets the number of data samples and number of features per sample along with output per sample. It applies OLS regression to compute the regression output for additional test data samples.
Definition in file ordinary_least_squares_regressor.cpp.
std::vector< float > fit_OLS_regressor | ( | std::vector< std::vector< T > > const & | X, |
std::vector< T > const & | Y ) |
Perform Ordinary Least Squares curve fit. This operation is defined as
\[\beta = \left(X^TXX^T\right)Y\]
X | feature matrix with rows representing sample vector of features |
Y | known regression value for each sample |
Definition at line 321 of file ordinary_least_squares_regressor.cpp.
std::vector< std::vector< float > > get_inverse | ( | std::vector< std::vector< T > > const & | A | ) |
Get matrix inverse using Row-trasnformations. Given matrix must be a square and non-singular.
Definition at line 226 of file ordinary_least_squares_regressor.cpp.
std::vector< std::vector< T > > get_transpose | ( | std::vector< std::vector< T > > const & | A | ) |
matrix transpose
Definition at line 300 of file ordinary_least_squares_regressor.cpp.
|
inline |
function to check if given matrix is a square matrix
Definition at line 59 of file ordinary_least_squares_regressor.cpp.
int main | ( | void | ) |
main function
Definition at line 423 of file ordinary_least_squares_regressor.cpp.
void ols_test | ( | ) |
Self test checks
Definition at line 369 of file ordinary_least_squares_regressor.cpp.
std::vector< float > operator* | ( | float const | scalar, |
std::vector< T > const & | A ) |
pre-multiplication of a vector by a scalar
Definition at line 138 of file ordinary_least_squares_regressor.cpp.
std::vector< std::vector< T > > operator* | ( | std::vector< std::vector< T > > const & | A, |
std::vector< std::vector< T > > const & | B ) |
Matrix multiplication such that if A is size (mxn) and B is of size (pxq) then the multiplication is defined only when n = p and the resultant matrix is of size (mxq)
Definition at line 78 of file ordinary_least_squares_regressor.cpp.
std::vector< T > operator* | ( | std::vector< std::vector< T > > const & | A, |
std::vector< T > const & | B ) |
multiplication of a matrix with a column vector
Definition at line 112 of file ordinary_least_squares_regressor.cpp.
std::vector< float > operator* | ( | std::vector< T > const & | A, |
float const | scalar ) |
post-multiplication of a vector by a scalar
Definition at line 156 of file ordinary_least_squares_regressor.cpp.
std::vector< T > operator+ | ( | std::vector< T > const & | A, |
std::vector< T > const & | B ) |
addition of two vectors of identical lengths
Definition at line 204 of file ordinary_least_squares_regressor.cpp.
std::vector< T > operator- | ( | std::vector< T > const & | A, |
std::vector< T > const & | B ) |
subtraction of two vectors of identical lengths
Definition at line 183 of file ordinary_least_squares_regressor.cpp.
std::vector< float > operator/ | ( | std::vector< T > const & | A, |
float const | scalar ) |
division of a vector by a scalar
Definition at line 174 of file ordinary_least_squares_regressor.cpp.
std::ostream & operator<< | ( | std::ostream & | out, |
std::vector< std::vector< T > > const & | v ) |
operator to print a matrix
Definition at line 22 of file ordinary_least_squares_regressor.cpp.
std::ostream & operator<< | ( | std::ostream & | out, |
std::vector< T > const & | v ) |
operator to print a vector
Definition at line 42 of file ordinary_least_squares_regressor.cpp.
std::vector< float > predict_OLS_regressor | ( | std::vector< std::vector< T > > const & | X, |
std::vector< float > const & | beta ) |
Given data and OLS model coeffficients, predict regression estimates. This operation is defined as
\[y_{\text{row}=i} = \sum_{j=\text{columns}}\beta_j\cdot X_{i,j}\]
X | feature matrix with rows representing sample vector of features |
beta | fitted regression model |
Definition at line 352 of file ordinary_least_squares_regressor.cpp.