Given: $$ y=\alpha + \beta x $$ The problem on how to get regression coefficients $\alpha, \beta_0, \beta_1,...,\beta_n$ from the covariance matrix is solved here:
Is there a way to use the covariance matrix to find coefficients for multiple regression?
The same problem can be solved without the intercept ($\alpha =0$)?
$$ y=\beta x $$
My MATLAB code:
function [coefficients, intercept] = gvcoef(X,y,CovEst)
%
% Finds coefficients and intercept estimates
% Inputs:
% - y -> dependent variable vector nx1
% - X -> independent variables matrix nxp
% - cov -> given estimate of covariance matrix (cov(X) in OLS)
a = CovEst;
for i=1:size(X,2)
covariance = cov(y,X(:,i));
b(i,:)= covariance(1,2);
end
% Coefficients from the covariance matrix
coefficients = linsolve(a,b);
% Intercept
y_bar = mean(y);
x_bar = mean(X,1);
intercept = y_bar - x_bar * coefficients;
end
```