- https://in.mathworks.com/help/stats/fitlm.html
- https://in.mathworks.com/help/stats/fitlm.html
- https://in.mathworks.com/help/matlab/ref/polyfit.html
- https://in.mathworks.com/help/curvefit/fit.html
- https://in.mathworks.com/help/matlab/ref/cov.html
- https://in.mathworks.com/help/matlab/ref/var.html
- https://in.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html
How to compute beta between stock and index returns, when the index returns are negative.
8 views (last 30 days)
Show older comments
Hello!, im having troubles when trying to calculate the beta of a stock in relation with an index returns, with the condition that the calculation of the beta only takes into consideration when the index returns are negative. The code i have up to is the following:
% Find the beta
mdl = fitlm(as51r,bhpr,'linear','Intercept',true);
coeffs = mdl.Coefficients.Estimate;
bhp_beta = coeffs(2);
this will give me the normal beta of the stock, but i need it when the index returns are negative.
Cheers.
0 Comments
Answers (1)
Saarthak Gupta
on 5 Sep 2023
Edited: Saarthak Gupta
on 5 Sep 2023
Hi Marlon,
I believe you are trying to calculate the beta of a stock through linear regression. ‘fitlm’ works for any set of data points, which may or may not be negative. You can also use functions like fit and polyfit to fit a polynomial.
In case you wish to calculate beta only for those set of data points, for which the index returns are negative, you can filter them using logical indexing in MATLAB, and subsequently use ‘fitlm’ or the analytical formula for calculating beta:
neg_indx = as51r < 0;
as51r_neg = as51r[neg_indx];
bhpr_neg = bhpr[neg_indx];
mdl = fitlm(as51r_neg,bhpr_neg,'linear','Intercept',true);
coeffs = mdl.Coefficients.Estimate;
bhp_beta = coeffs(2);
Or use the analytical formula:
Covariance (RS , RM) / Variance(RM)
Where RS is the return on the stock, and RM is the return on the index
neg_indx = as51r < 0;
as51r_neg = as51r[neg_indx];
bhpr_neg = bhpr[neg_indx];
bhp_beta = cov(as51r_neg, bhpr_neg) / var(as51r_neg);
Please refer to the following MATLAB documentation for more details
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!