Why is the linear regression wrong when I switch X and Y of a random variable Z(X, Y)?

Viewed 339

I am having a weird bug that I can not seem to understand:

  1. I draw N values (X, Y) of a random variable in two dimensions Z(X,Y).
  2. I construct the histogram and plot it with imshow.
  3. I calculate the linear regression with the (X,Y) values and plot it: enter image description here So far everything looks normal.
  4. Now I repeat 1., 2. and 3 but switching X and Y. I would expect to find the same picture but with the axis switched. However, this time the linear regression (orange dotted line) is not correct and has a slope different than the expected 1/0.25 (red dashed line). enter image description here

Any ideas where the error might lie?

The code in python:

from scipy.stats import linregress
import numpy as np
import matplotlib.pyplot as plt

#Parameters
delta = 0.2
N = 10**5

#Bins
x = y = np.arange(-3.0, 3.0, delta)

#Draw N values of the random variable Z(X,Y)
rnd = np.random.default_rng(seed = 0)
Z = rnd.uniform(0, 1, N)
X = rnd.uniform(-3, 3, N)
Y = 0.25*X + np.sqrt(np.log( 1 / Z ) ) - 0.89

#Construct histogram
H, xedges, yedges = np.histogram2d(X, Y, bins=[x, y])
#Tranpose to have x in columns and y in rows
H = H.T

#Plot
plt.imshow(H, cmap='Purples',
            origin='lower', extent=[-3, 3, -3, 3])

#Do linear regresion
lr = linregress(X, Y)
poly1d_fn = np.poly1d([lr.slope, lr.intercept])
xLine=[xedges[0], xedges[-1]]
plt.plot(xLine, poly1d_fn(xLine), 'orange', ls = ':',
            label = '$y = ax+b$\n $a = %.2f \pm %.2f$\n $b = %.2f$, $R^2 = %.2f$ '%(lr.slope, lr.stderr, lr.intercept, lr.rvalue**2))
    
plt.colorbar()
plt.legend()
plt.savefig("first.png", dpi = 300)

#Repeat but switching X with Y
plt.figure()
X2 = Y
Y2 = X
H, xedges, yedges = np.histogram2d(X2, Y2, bins=[x, y])
H = H.T

plt.imshow(H, cmap='Purples',
            origin='lower', extent=[-3, 3, -3, 3])

lr = linregress(X2, Y2)
poly1d_fn = np.poly1d([lr.slope, lr.intercept])
xLine=[xedges[0], xedges[-1]]
plt.plot(xLine, poly1d_fn(xLine), 'orange', ls = ':',
            label = '$y = ax+b$\n $a = %.2f \pm %.2f$\n $b = %.2f$, $R^2 = %.2f$ '%(lr.slope, lr.stderr, lr.intercept, lr.rvalue**2))

plt.plot(xLine, [4*z for z in xLine], 'red', ls = '--')


plt.ylim([-3, 3])
plt.colorbar()
plt.legend()
plt.savefig("second.png", dpi = 300)

2 Answers

I am quite sure that the discripency doesn't comes from the software and code. The problem is about the criteria of fitting.

If the data is not scattered it doesn't matter the criteria of fitting. The result is unique.

If the data is scattered, they are as many different "best fit" than they are different criteria of fitting. The more the scatter is large the more the results might differ depending on the critteria of fitting.

A well known example is with linear regression :

enter image description here

Of course they are a lot of other different crireria of fitting that might be specified.

Note : The above figure and formulas are copied from pp.7-8 in the paper https://fr.scribd.com/doc/14819165/Regressions-coniques-quadriques-circulaire-spherique

After some reflection of the comment and answer of @Jjacquelin reminding a linear regression generally changes when swapping the X and Y axis, I understood the particular shapes of the linear regression of my example. I will share it in case it may help other people.

The key point is that a linear regression optimizes the parameters such that the error in the vertical axis is minimized. We can get an intuitive idea behind this process with the distributions of the random variable along the vertical axis (green dashed lines) and their average (green dots). I draw these by hand in both pictures:

enter image description here enter image description here

We can see how the average of the random variable along the vertical axis (the green dots) —representing the points minimizing the errors of the linear fit in the vertical axis— fall approximately at the linear regression.

Moreover, we can understand why in the second plot the linear regression looks "wrong". That is because on the extremes, the distributions are "cut", displacing the average of the random variable inside the picture and rotating the expected linear regression (red dashed line).

Related