3
$\begingroup$

I am trying to fit a line that best predicts the production of energy Y given the speed of wind X, a typical Y = xm + b , using deming regression. I am looking for the slope and the intercept of that line using the following formula:

enter image description here

enter image description here

I assume that 𝛿 = 1.

This is my following python implementation of deming regression:

def deming_regresion(df, X, y, delta = 1):
'''Takes a pandas DataFrame, name of the 
columns as strings and the value of delta, 
and returns the slope and intercept following deming regression formula'''

    cov = df.cov()
    mean_x = df[X].mean()
    mean_y = df[y].mean()
    s_xx = cov[X][X]
    s_yy = cov[y][y]
    s_xy = cov[X][y]

    slope = (s_yy  - delta * s_xx + np.sqrt((s_yy - delta * s_xx) ** 2 + 4 * delta * s_xy ** 2)) / (2 * s_xy)

    intercept = media_y - pendiente  * media_x

    return slope, intercept

I meassure the % of MSE and MAE in the predictions and I get the following results when trying to predict Y = Energy Production and X = Wind Speed: enter image description here

And the % of MSE and MAE are (MSE 97.72, MAE 69.85), slope, intercept of 17.85353671, -345.34106788.

When I switch variables, X = Energy Production and Y = Wind Speed I get this:

enter image description here

With these % of errors (MSE 44.9, MAE 32.23) and slope intercept of 0.04957782881808902, 21.051520903377014.

Why this happens? What am I doing wrong? I used Orthogonal Regression from scipy because my delta is equal to 1 and I still get very similar results. Maybe is a very stupid question but I will appreciate your help.

If you need any more info you can ask, I tried to put as much info as I could but maybe I missed something important.

$\endgroup$
2
  • $\begingroup$ Can you show your full code. How do you call this function in the two situations. And what is the output of df.cov() each of these times? $\endgroup$ Commented Oct 9, 2020 at 10:24
  • $\begingroup$ Where is pendiente defined in your code? I see it is used in intercept = media_y - pendiente * media_x but I do not see where it is defined. $\endgroup$
    – Galen
    Commented Dec 18, 2021 at 22:59

2 Answers 2

2
$\begingroup$

You are getting very close to similar answer - but remember when you flip the axis, you flip the way the parameters come out. You are initially fitting

$$y=ax+b$$

Then you fit

$$x=cy+d$$

For orthogonal regression these should indeed "match", but that doesn't mean $c=a$, instead, if we rearrange the second equation you get $$y=\frac{x-d}{c}$$ so $a=c^{-1}$ and $b=-\frac{d}{c}$

Checking with your numbers then $$17.85^{-1} = 0.056$$ $$\frac{-21.05}{0.0496} = -424.4$$

So not very far off. I'm not sure where the rest of the difference will be coming from, but possibly you covariance estimates from pandas are not quite the calculation you expect? Possibly an $n-1$ that should or should not be there.

$\endgroup$
1
$\begingroup$

When I try your function then it works as expected

import pandas as pd 
import numpy as np

np.random.seed(1)
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))

mod1 = deming_regresion(df, 'A', 'B')
mod2 = deming_regresion(df, 'B', 'A')


print(1/mod1[0])
print(mod2[0])

returns

1.3756144526391498
1.3756144526391496

So it seems like you need to look into your auxiliary code. How do you call your function? Do you get errors in the computations? That sort of stuff.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.