2
$\begingroup$

How can I fit a polynomial to my data using gradient descent in python?

$\endgroup$
4
  • $\begingroup$ Is your question about methods of fitting polynomials via gradient descent (that you may be able to implement in Python) or are you specifically seeking Python code/functions to call? $\endgroup$
    – Glen_b
    Commented Nov 19, 2017 at 0:19
  • $\begingroup$ Too little work was done to find the solution, -1 $\endgroup$ Commented Nov 19, 2017 at 1:02
  • $\begingroup$ I've written a code for this using linear regression and gradient descent, but there seems to be something wrong with it: stats.stackexchange.com/questions/314502/… $\endgroup$
    – mBabaee
    Commented Nov 19, 2017 at 7:06
  • $\begingroup$ It makes sense to downvote, but not to close, as this is the site where this kind of question can be asked. If you were a stats prof and a student asked this question you'd be delighted, you wouldn't tell them they've come to the wrong department $\endgroup$
    – dduhaime
    Commented Nov 15, 2018 at 13:59

2 Answers 2

4
$\begingroup$

Agreed with Appletree that it should be done directly. But for the sake of practicing gradient descent and/or gaining some intuition on a problem for which we already know the answer it could be useful. With the math complete the Python can be found here. So,

Consider $\textbf{y}=\textbf{X}\mathbf{\beta}+\mathbf{\epsilon}$. With $\textbf{y} \in \mathbb{R}^n$, $\textbf{X} \in \mathbb{R}^{n\times d}$, $\mathbf{\beta} \in \mathbb{R}^d$, $\mathbf{\epsilon} \sim N(0,\sigma^2)$.

The goal of linear regression is to find the $\beta$ which minimizes the squared loss, i.e. $\text{argmin}_{\beta} \| \mathbf{y - X\beta}\|_2^2 $. To do this by gradient descent we must first find the gradient of the loss function with respect to $\mathbf{\beta}$:

$\frac{\partial}{\partial \mathbf{\beta}} \| \mathbf{y - X\beta}\|_2^2 = 2 \mathbf{X}^T(\mathbf{y-X\beta})$

Now, we follow the algorithm for gradient descent.

  1. Select an initial guess $\mathbf{\beta_0}$
  2. Set $\beta_{k+1} = \beta_k - \alpha_k \mathbf{X}^T(\mathbf{y-X\beta}_k)$

Where $\alpha_k$ can be a constant or adaptive stepsize. You may notice that the 2 from the gradient disappeared. That can just be absorbed into the $\alpha_k$.

$\endgroup$
1
$\begingroup$

Why would you even want that? There's an analytic solution based on linear regression. You simply expand your predictor matrix. For instance, this

$$ X = \begin{pmatrix}1 & x_1 & x_1^2 \\ 1 &x_2&x_2^2\\ \vdots& & \vdots\end{pmatrix} $$

specifies the predictor matrix for a quadratic polynomial. You can add higher order polynomials as additional columns. If you have multiple features/variables, you have to add a quadratic, cubic etc. term for each of them.

If y is the vector containing your responses, your regression model then becomes

$$ y = X\ \begin{pmatrix}\beta_0\\\beta_1\\\beta_2\end{pmatrix} + \epsilon $$

and $\beta_0$ is the intercept term, and $\beta_1$ and $\beta_2$ are the linear and quadratic terms. The solution is given as

$$ \hat{\mathbf{\beta}} = X^\dagger y $$

where $X^\dagger$ is the pseudo-inverse of X. No iterative methods required. Python pseudo-inverse code is here.

$\endgroup$
8
  • 1
    $\begingroup$ I don't understand this to answer the question regarding Python gradient descent. $\endgroup$ Commented Nov 19, 2017 at 0:48
  • $\begingroup$ @JamesPhillips You are correct, it does not answer the question. However, it shows that the question is sub-optimal, and, it answers the question that should have been asked. $\endgroup$
    – Carl
    Commented Nov 19, 2017 at 1:28
  • 2
    $\begingroup$ Hmm @Carl I disagree. Assuming the OP really cares about gradient descent, there are very few examples that are simpler to derive it. Furthermore, having the direct solution so easily available to check how well the implementation of gradient descent has performed is valuable. $\endgroup$ Commented Nov 19, 2017 at 1:33
  • 2
    $\begingroup$ I guess I should be clear that I do agree with you when you say the question was sub-optimal; it showed too little effort. But I would not presume to say that the question that appletree answered is the question that should have been asked. $\endgroup$ Commented Nov 19, 2017 at 1:35
  • 1
    $\begingroup$ Well, you cannot minimize the sum-of-squared relative error, or maximum value of absolute error, or orthogonal distance, etc. using the answer from @Carl - some form of non-linear optimization would be needed. $\endgroup$ Commented Nov 19, 2017 at 11:29

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