33. Ordinary Least Squares medium
Solve the Ordinary Least Squares (OLS) regression problem on a GPU. Given a feature matrix of size and a target vector of size , compute the coefficient vector that minimizes the sum of squared residuals: The closed-form solution to OLS is:
Implementation Requirements
- External libraries are not permitted.
- The
solvefunction signature must remain unchanged. - The final coefficients must be stored in the
betavector. - Assume that the feature matrix is full rank (i.e., is invertible).
Example:
Input:
(samples × features):
:
Output:
:
Constraints
- 1 ≤
n_samples≤ 100,000 - 1 ≤
n_features≤ 1,000 n_samples≥n_features- -1000.0 ≤ values in
Xandy≤ 1000.0 - Solutions are tested with absolute tolerance of 1e-2 and relative tolerance of 1e-2
- Performance is measured with
n_features= 32,n_samples= 32
Starter code (CUDA)
#include <cuda_runtime.h>
// X, y, beta are device pointers
extern "C" void solve(const float* X, const float* y, float* beta, int n_samples, int n_features) {}
From LeetGPU, CC BY-NC-ND 4.0.