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