Deming Linear Regression [wbburgin]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © wbburgin
//
//@version=5
indicator("Deming Linear Regression [wbburgin]", overlay=true)

//Deming regression is a type of linear regression used to model the relationship between two variables when there is 
//variability in both variables.  Deming regression provides a solution by simultaneously 
//accounting for the variability in both the independent and dependent variables, resulting in a more accurate 
//estimation of the underlying relationship. In the hard-science fields, where measurements are critically important
//to judging the conclusions drawn from data, Deming regression can be used to account for measurement error.

//Tradingview's default ta.linreg() function uses least squares linear regression, which is similar but different than 
//Deming regression. In least squares regression, the regression function minimizes the sum of the squared vertical 
//distances between the data points and the fitted line. This method assumes that the errors or variability are only 
//present in the y-values (dependent variable), and that the x-values (independent variable) are measured without error. 

//In time series data used in trading, Deming regression can be more accurate than least squares regression because the 
//ratio of the variances of the x and y variables is large. X is the bar index, which is an incrementally-increasing 
//function that has little variance, while Y is the price data, which has extremely high variance when compared to the 
//bar index. In such situations, least squares regression can be heavily influenced by outliers or extreme points in the 
//data, whereas Deming regression is more resistant to such influence.

//Additionally, if your x-axis uses variable widths - such as renko blocks or other types of non-linear widths - Deming
//regression might be more effective than least-squares linear regression because it accounts for the variability in 
//your x-values as well.

//In contrast to least squares regression, Deming regression takes into account the variability or errors in both the 
//x- and y-values. It minimizes the sum of the squared perpendicular distances between the data points and the fitted 
//line, taking into account both the x- and y-variability. This makes Deming regression more robust in both variables 
//than least squares regression.


length = input.int(50,"Length")
compareToPine = input.bool(false,"Compare to Normal Least Squares Linreg")

y = array.new_float()
for i = 0 to length -1
    close_i = close[length - 1 - i]
    array.push(y,close_i)

x = array.new_float()
for i = 0 to length -1
    barindex_i = bar_index[length - 1 - i]
    array.push(x,barindex_i)

x_mean = array.avg(x)
y_mean = array.avg(y)
x_stdev = array.stdev(x)
y_stdev = array.stdev(y)

cov = 0.0
count = 0
for i = 0 to length - 1
    if not na(array.get(x,i)) and not na(array.get(y,i))
        cov += (array.get(x,i) - x_mean) * (array.get(y,i) - y_mean)
        count += 1
if count > 1
    cov := cov / (count - 1)

slope = (y_stdev / x_stdev) * (cov / (math.pow(x_stdev,2) + math.pow(y_stdev,2)))
intercept = y_mean - slope * x_mean

demingreg = slope * array.get(x,0) + intercept
pine_linreg = ta.linreg(close,length,0)

plot(demingreg, "Deming Linear Regression", color=color.white)
plot(not compareToPine ? na: pine_linreg,"Least Squares Regression",color=color.yellow)