How does one, having two lists of prices Y and X and wishing to fit the Least Squares model, perform this in Java?
The fit is required to calculate the hedge ratio in a pairs trading strategy.
For every period, the spread is calculated as follows:
symbol1.close - hedgeRatio * symbol2
We intend to compare the performance of this formula versus using normalized moving averages.
The below Python code uses the statsmodels library and the Ordinary Least Squares (OLS) method.
import statsmodels.api as sm def hedge_ratio(Y, X): X = sm.add_constant(X) model = sm.OLS(Y, X).fit() return model.params