Linear regression is widely used in financial engineering for modeling and forecasting financial data, such as stock prices, interest rates, and macroeconomic indicators. In machine learning, it is a fundamental technique for supervised learning tasks, such as regression and classification, and is often used as a baseline model or as a component in more complex models.
We provide three functions to perform linear regression.
LinearRegression1: Returns the bias and coefficient of y after performing linear regression on the data
LinearRegression2: Returns the bias and coefficients of y and y2 after performing linear regression on the data
LinearRegressionAny: Returns the bias and coefficient of each variable after performing linear regression on the data. Uses more gas than LinearRegression1 and LinearRegression2 functions above, when possible use the aforementioned functions.
LinearRegressionPredict: Given the coefficients and the variables predicts the value
The formal definition for linear regression is provided using the following equation. Where y is the response variable, x is the predictor variable, beta is the coefficients, and epsilon is the error term.
y=β0+β1x1+β2x2+…+βpxp+ϵ
To implement the functions, see the method signatures below. Precision represents the number of decimal places and can be set between 0 and 18. It is applied to all inputs and outputs.