29 #ifndef CDPL_MATH_MLRMODEL_HPP
30 #define CDPL_MATH_MLRMODEL_HPP
78 template <
typename T =
double>
104 chiSquare(0), Q(0), r(0), stdDeviation(0) {}
133 template <
typename V>
150 template <
typename V>
197 template <
typename V>
211 template <
typename V>
318 template <
typename T>
321 if (num_points == xMatrix.getSize1() && num_vars == xMatrix.getSize2())
324 xMatrix.resize(num_points, num_vars,
true,
ValueType());
328 template <
typename T>
332 xMatrix.resize(0, 0,
false);
335 template <
typename T>
336 template <
typename V>
339 SizeType x_mtx_size1 = xMatrix.getSize1();
340 SizeType x_mtx_size2 = xMatrix.getSize2();
341 SizeType x_vars_size = x_vars().getSize();
342 SizeType y_vals_size = yValues.getSize();
344 resizeDataSet(std::max(i + 1, std::max(x_mtx_size1, y_vals_size)), std::max(x_vars_size, x_mtx_size2));
346 for (
SizeType j = 0; j < x_vars_size; j++)
347 xMatrix(i, j) = x_vars()(j);
349 if (x_vars_size < x_mtx_size2)
350 for (
SizeType j = x_vars_size; j < x_mtx_size2; j++)
356 template <
typename T>
357 template <
typename V>
361 SizeType x_mtx_size2 = xMatrix.getSize2();
362 SizeType x_vars_size = x_vars().getSize();
364 resizeDataSet(i + 1, std::max(x_mtx_size2, x_vars_size));
366 for (
SizeType j = 0; j < x_vars_size; j++)
367 xMatrix(i, j) = x_vars()(j);
369 if (x_vars_size < x_mtx_size2)
370 for (
SizeType j = x_vars_size; j < x_mtx_size2; j++)
376 template <
typename T>
383 template <
typename T>
390 template <
typename T>
397 template <
typename T>
404 template <
typename T>
412 if (
m == 0 || n == 0)
415 if (n !=
SizeType(yValues.getSize()))
416 resizeDataSet(std::max(
SizeType(yValues.getSize()), n),
m);
418 svdU.resize(n,
m,
false);
419 svdV.resize(
m,
m,
false);
420 svdW.resize(
m,
false);
439 coefficients.resize(
m,
false);
444 template <
typename T>
445 template <
typename V>
450 throw Base::CalculationFailed(
"MLRModel: number of regression coefficients does not match number of independent variables");
455 template <
typename T>
456 template <
typename V>
460 return calcYValue(x);
463 template <
typename T>
470 template <
typename T>
477 template <
typename T>
484 template <
typename T>
491 template <
typename T>
498 template <
typename T>
509 if (
m !=
SizeType(coefficients.getSize()))
510 throw Base::CalculationFailed(
"MLRModel: number of independent variables does not match number of regression coefficients");
512 if (n !=
SizeType(yValues.getSize()))
513 throw Base::CalculationFailed(
"MLRModel: number of dependent variables does not match number of vectors with independent variables");
515 calcYValues.resize(n);
522 mean_data_y += data_y;
523 mean_calc_y += calc_y;
524 chiSquare += y_diff * y_diff;
526 calcYValues(i) = calc_y;
538 ValueType yt = calcYValues(i) - mean_calc_y;
Definition of exception classes.
Definition of matrix data types.
Implementation of matrix singular value decomposition and associated operations.
Provides miscellaneous special mathematical functions.
Definition of type traits.
Definition of vector data types.
Thrown to indicate that some requested calculation has failed.
Definition: Base/Exceptions.hpp:230
Performs Multiple Linear Regression [WLIREG] on a set of data points .
Definition: MLRModel.hpp:80
ValueType operator()(const VectorExpression< V > &x_vars) const
Predicts the value of the dependent variable for a vector of independent variables given by x_vars.
ValueType getCorrelationCoefficient() const
Returns the correlation coefficient .
Definition: MLRModel.hpp:486
MLRModel()
Constructs and initializes a regression model with an empty data set.
Definition: MLRModel.hpp:103
const VectorType & getCoefficients() const
Returns a read-only vector containing the estimated regression coefficients which were calculated by...
Definition: MLRModel.hpp:465
void resizeDataSet(SizeType num_points, SizeType num_vars)
Resizes the data set to hold num_points data points with num_vars independent variables.
Definition: MLRModel.hpp:319
ValueType getGoodnessOfFit() const
Returns the goodness of fit .
Definition: MLRModel.hpp:479
CommonType< typename Vector< T >::SizeType, typename Matrix< T >::SizeType >::Type SizeType
An unsigned integral type used to represent sizes and indices.
Definition: MLRModel.hpp:86
void buildModel()
Performs linear least squares regression modeling of the set of currently stored data points .
Definition: MLRModel.hpp:405
ValueType getStandardDeviation() const
Returns the standard deviation of the residuals .
Definition: MLRModel.hpp:493
void setXYData(SizeType i, const VectorExpression< V > &x_vars, ValueType y)
Sets the i-th data point of the data set.
Definition: MLRModel.hpp:337
Vector< T > VectorType
The vector type used for the storage of response values and regression coefficients.
Definition: MLRModel.hpp:98
void calcStatistics()
Calculates various statistical parameters describing the built regression model.
Definition: MLRModel.hpp:499
void clearDataSet()
Clears the data set.
Definition: MLRModel.hpp:329
VectorType & getYValues()
Returns a vector containing the dependent variables of the currently stored data points .
Definition: MLRModel.hpp:392
T ValueType
The value type used in calculations and for the storage of data points.
Definition: MLRModel.hpp:90
ValueType calcYValue(const VectorExpression< V > &x_vars) const
Predicts the value of the dependent variable for a vector of independent variables given by x_vars.
ValueType getChiSquare() const
Returns the sum of squared residuals .
Definition: MLRModel.hpp:472
MatrixType & getXMatrix()
Returns a matrix where each row represents the vector with independent variables of the currently st...
Definition: MLRModel.hpp:378
Matrix< T > MatrixType
The matrix type used for the storage of the regression input data.
Definition: MLRModel.hpp:94
void addXYData(const VectorExpression< V > &x_vars, ValueType y)
Adds a new data point to the current data set.
Definition: MLRModel.hpp:358
Dynamically-sized dense row-major matrix with configurable underlying storage.
Definition: Matrix.hpp:510
A::size_type SizeType
The unsigned size type used by the underlying storage container.
Definition: Matrix.hpp:533
CRTP base class of all vector expression types.
Definition: Expression.hpp:68
Dynamically-sized dense vector with configurable underlying storage.
Definition: Vector.hpp:480
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
constexpr unsigned int Q
Generic type that covers any element except hydrogen and carbon.
Definition: AtomType.hpp:647
constexpr unsigned int r
Specifies that the stereocenter has r configuration.
Definition: CIPDescriptor.hpp:76
constexpr unsigned int m
Specifies that the stereocenter has m configuration.
Definition: CIPDescriptor.hpp:116
VectorInnerProduct< E1, E2 >::ResultType innerProd(const VectorExpression< E1 > &e1, const VectorExpression< E2 > &e2)
Returns the inner (dot) product of the vector expressions e1 and e2.
Definition: VectorExpression.hpp:1033
MatrixRow< M > row(MatrixExpression< M > &e, typename MatrixRow< M >::SizeType i)
Returns a mutable row proxy for row i of the matrix expression e.
Definition: MatrixProxy.hpp:1372
void svSubstitute(const MatrixExpression< U > &u, const VectorExpression< W > &w, const MatrixExpression< V > &v, const VectorExpression< B > &b, VectorExpression< X > &x)
Solves for a vector where is given by its Singular Value Decomposition [WSVD].
Definition: SVDecomposition.hpp:466
bool svDecompose(MatrixExpression< A > &a, VectorExpression< W > &w, MatrixExpression< V > &v, std::size_t max_iter=0)
Computes the Singular Value Decomposition [WSVD] of a -dimensional matrix a.
Definition: SVDecomposition.hpp:70
T gammaQ(const T &a, const T &x)
Computes the incomplete gamma function (see [NRIC] for details).
CDPL_PHARM_API const Base::LookupKey TOLERANCE
Specifies the positional tolerance of the feature.
The namespace of the Chemical Data Processing Library.
Trait that resolves the common arithmetic type of T1 and T2 via std::common_type.
Definition: CommonType.hpp:46
Primary traits template for scalar arithmetic value types.
Definition: TypeTraits.hpp:307