I think Dave's answer points out the most pressing issues:
- translational invariance
- Absolute scale invariance
In Tensorflow we can define our correlation function:
class CorrLoss(tf.keras.losses.Loss):
def call(self, y_true, y_pred):
res_true = y_true - tf.reduce_mean(y_true)
res_pred = y_pred - tf.reduce_mean(y_pred)
cov = tf.reduce_mean(res_true * res_pred)
var_true = tf.reduce_mean(res_true**2)
var_pred = tf.reduce_mean(res_pred**2)
sigma_true = tf.sqrt(var_true)
sigma_pred = tf.sqrt(var_pred)
return - cov / (sigma_true * sigma_pred)
And quickly whip up a simple linear model:
model = tf.keras.Sequential([
layers.Dense(input_shape=[1,], units=1)
])
And a data set that is learnable by this model:
x = tf.random.normal((1000,))
y = 5 * x + 10 + tf.random.normal((1000,))
Training with our choice of loss function, model, and data, we can visually understand that correlation alone is not sufficient. As Dave describes, least squares is often effective.
Mostly for my own amusement, I considered if maximizing $\mathbb{E}[Y \hat Y]$ would fare any better than maximizing Pearson's correlation.
Here is the custom loss function:
class ProdLoss(tf.keras.losses.Loss):
def call(self, y_true, y_pred):
return -tf.reduce_mean(y_true * y_pred)
The following is a close success over the consistently-horrible choice of correlation:
And often it would look better, but it wasn't reliable! It would also often look like this:
Interestingly, the product moment will tend to ignore the true values by making the predicted values extreme. I noticed this by taking the same problem and increasing the number of epochs to $10^4$.
Thus the correlation and mixed moment are unreliable loss functions for achieving $Y \approx \hat Y$.