# Moving Linear Regression / Moving Average - Linear Regression

The Moving Linear Regression (MLA) and the Moving Average - Linear Regression (LRMA) studies calculate and display the value of a linear regression function of the selected Input Data (Open, High, Low, Close) over the specified Length.

For the calculation method, refer to the LinearRegressionIndicator_S function in the /ACS_Source/SCStudyFunctions.cpp file in the folder Sierra_Chart is installed to. We give a detailed mathematical description of this method below.

If you draw a Linear Regression Chart Drawing over the same Length that you have set in the study Inputs for this study, then where that drawing ends, it will have the same value as the Moving Average - Linear Regression study.

Next we describe the calculation of the Linear Regression Indicator (LRI). Let $$n$$ be the Input Length. Let $$T$$ be the variable measured along the horiztonal axis, let $$X$$ be a random variable denoting the Input Data, which is measured along the vertical axis. We denote the values of these variables at Index $$i$$ as $$T_i = i$$ and $$X_i$$, respectively , where $$i$$ runs from $$1$$ to $$n$$ in each calculation. We denote the Current Index Value as $$t$$. The LRI function computes each of the following sums for $$t \geq n - 1$$. These sums are used to calculate the regression statistics.

$$\displaystyle{\sum_{i=1}^n {i} = \frac{n(n+1)}{2}}$$

$$\displaystyle{\left(\sum_{i=1}^n {i}\right)^2 = \frac{n^2(n+1)^2}{4}}$$

$$\displaystyle{\sum_{i=1}^n {i^2} = \frac{n(n+1)(2n+1)}{6}}$$

$$\displaystyle{\sum_{i=t-n+1}^t {X_i}}$$

$$\displaystyle{\sum_{i=t-n+1}^t{T_{i - t + n}X_i} = \sum_{i=t-n+1}^t{(i-t+n)X_i}}$$

For an explanation of the Sigma ($$\Sigma$$) notation for summation, refer to our description here.

Note: The sums over the $$T-$$ values (that is, the first three sums) do not move, as the sums over the $$X-$$ values (the last two sums) do. This is compensated for by using the Length $$n$$ in certain places instead of the current value $$t$$ of the Index.

These sums are used to compute the regression statistics for $$t \geq n - 1$$, as shown below.

Slope:

$$\displaystyle{b_t(X,n) = \frac{n \cdot \sum_{i=t-n+1}^t{(i-t+n)X_i} - \frac{n(n+1)}{2} \cdot \sum_{i=t-n+1}^t {X_i}}{n \cdot \frac{n(n+1)(2n+1)}{6} - \frac{n^2(n+1)^2}{4}}}$$

Intercept:

$$\displaystyle{a_t(X,n) = \frac{\sum_{i=t-n+1}^t {X_i} - b_t(X,n) \cdot \frac{n(n+1)}{2}}{n}}$$

The regression model is of the form $$\hat{X} = a_t(X,n) + b_t(X,n)\cdot T$$, where $$\hat{X}$$ is an estimator of $$X$$.

Linear Regression Indicator:

The Linear Regression Indicator is the vertical coordinate of the right endpoint of the linear regression trendline of Length $$n$$. Its value $$LRI_t(X,n)$$ at Index $$t$$ for the given Inputs is calculated for $$t \geq n - 1$$ as $$LRI_t(X,n) = a_t(X,n) + b_t(X,n)\cdot n$$.

The Moving Average - Linear Regression and Moving Linear Regression are both mathematically identical to the Linear Regression Indicator for $$t \geq n - 1$$. Their respective values at Index $$t$$ for the given Inputs are denoted as $$LRMA_t(X,n)$$ and $$MLR_t(X,n)$$.