1
$\begingroup$

I want to let the gradient be a constant, say $3$ and then regress on the offset. Its obvious that one can do GD (or SGD) on something like the L2 loss of this. But this seems such an easy problem that I thought there should be something simpler that uses linear algebra. Is there?

Obviously:

$$ \theta = X^{-1}y$$

doesn't work since $\theta = [3 , c]$ since that doesn't respect the gradient being some constant value.

$\endgroup$
2
  • $\begingroup$ Write a new $y^*$ as the old $y$ minus all the fixed coefficients times their corresponding predictors and regress that on the remainder. If your program allows an offset, use that instead. $\endgroup$
    – Glen_b
    Commented Aug 4, 2018 at 9:45
  • $\begingroup$ Could you clarify what you mean by "2D regression," "gradient," and "offset"? Each of these terms has several possible meanings in a regression setting. $\endgroup$
    – whuber
    Commented Aug 4, 2018 at 12:35

1 Answer 1

3
$\begingroup$

If I understand your question correctly, you want to fit a function of the form:

$$y = w \cdot x + c$$

where $w$ takes a specified value, and $c$ is the the only free parameter.

Suppose the training set is $\{(x_i, y_i)\}_{i=1}^n$ and we want to minimize the squared error. Then $c$ is the mean of $\{y_i - w \cdot x_i\}$, which follows from the fact that the mean of a set is the point that minimizes the squared distance to each element.

Just to write things out, the problem is:

$$\min_c \sum_{i=1}^n [y_i - (w \cdot x_i + c)]^2$$

Take the derivative of the loss function and set it to zero:

$$-2 \sum_{i=1}^n [y_i - (w \cdot x_i + c)] = 0$$

Solve for $c$:

$$c = \frac{1}{n} \sum_{i=1}^n (y_i - w \cdot x_i)$$

$\endgroup$
5
  • $\begingroup$ I don't think there should be a 1/n there... $\endgroup$ Commented Aug 15, 2018 at 22:02
  • $\begingroup$ oh I think ur right...its cuz of the sum over c. But is there an easy way to vectorize that? $\endgroup$ Commented Aug 15, 2018 at 22:08
  • $\begingroup$ hmmm this is unintuitive for me, how the solution just $$ \frac{1}{n} (Y - Xw) $$ seems like an odd coincidence... $\endgroup$ Commented Aug 15, 2018 at 22:25
  • $\begingroup$ If you want to use vector notation, you could write the sum as a dot product: $c = \frac{1}{n} \vec{1}_n \cdot (y - X w)$ where $\vec{1}_n$ is a length $n$ vector of ones. $\endgroup$
    – user20160
    Commented Aug 16, 2018 at 0:23
  • 1
    $\begingroup$ For an intuitive explanation, we can start with a fixed function $f_0(x)=w \cdot x$ (no constant term). Adding the constant term $c$ shifts the curve up and down. We should shift it so that it lies in the middle of the data points. $y-Xw$ are the residuals of $f_0$. So, if we set $c$ to the mean of these residuals, then the residuals of the shifted function will be zero on average. More formally, this choice of $c$ follows from the relationship between the mean and squared distance that I mentioned in the answer. $\endgroup$
    – user20160
    Commented Aug 16, 2018 at 0:40

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.