slope 6.2.1
Loading...
Searching...
No Matches
estimate_alpha.h
Go to the documentation of this file.
1
7#pragma once
8
9#include "ols.h"
10
11namespace slope {
12
27template<typename T>
28double
29estimateNoise(Eigen::EigenBase<T>& x,
30 Eigen::MatrixXd& y,
31 const bool fit_intercept)
32{
33 int n = x.rows();
34 int p = x.cols();
35
36 Eigen::VectorXd residuals;
37 int df; // Degrees of freedom
38
39 if (p == 0) {
40 // Handle the case when X has zero columns
41 residuals = y;
42 df = n - 1;
43
44 if (fit_intercept) {
45 y.array() -= y.mean();
46 df -= 1;
47 }
48 } else {
49 // Normal case with predictors
50 auto [ols_intercept, ols_coefs] =
51 detail::fitOls(x.derived(), y, fit_intercept);
52 residuals = y - x.derived() * ols_coefs;
53
54 if (fit_intercept) {
55 residuals.array() -= ols_intercept;
56 }
57
58 df = n - p - static_cast<int>(fit_intercept) - 1;
59 }
60
61 assert(df > 0);
62
63 return residuals.norm() / std::sqrt(df);
64}
65
66} // namespace slope
Namespace containing SLOPE regression implementation.
Definition clusters.h:11
double estimateNoise(Eigen::EigenBase< T > &x, Eigen::MatrixXd &y, const bool fit_intercept)
Estimates noise (standard error) in a linear model using OLS residuals.
Ordinary Least Squares (OLS) regression functionality.