R and Python: Gradient Descent

December 22, 2015
By

(This article was first published on Analysis with Programming, and kindly contributed to R-bloggers)

One of the problems often dealt in Statistics is minimization of the objective function. And contrary to the linear models, there is no analytical solution for models that are nonlinear on the parameters such as logistic regression, neural networks, and nonlinear regression models (like Michaelis-Menten model). In this situation, we have to use mathematical programming or optimization. And one popular optimization algorithm is the gradient descent, which we’re going to illustrate here. To start with, let’s consider a simple function with closed-form solution given by begin{equation} f(beta) triangleq beta^4 – 3beta^3 + 2. end{equation} We want to minimize this function with respect to $beta$. The quick solution to this, as what calculus taught us, is to compute for the first derivative of the function, that is begin{equation} frac{text{d}f(beta)}{text{d}beta}=4beta^3-9beta^2. end{equation} Setting this to 0 to obtain the stationary point gives us begin{align} frac{text{d}f(beta)}{text{d}beta}&overset{text{set}}{=}0nonumber\ 4hat{beta}^3-9hat{beta}^2&=0nonumber\ 4hat{beta}^3&=9hat{beta}^2nonumber\ 4hat{beta}&=9nonumber\ hat{beta}&=frac{9}{4}. end{align} The following plot shows the minimum of the function at $hat{beta}=frac{9}{4}$ (red line in the plot below).

R ScriptNow let’s consider minimizing this problem using gradient descent with the following algorithm:

  1. Initialize $mathbf{x}_{r},r=0$
  2. while $lVert mathbf{x}_{r}-mathbf{x}_{r+1}rVert > nu$
  3.         $mathbf{x}_{r+1}leftarrow mathbf{x}_{r} – gammanabla f(mathbf{x}_r)$
  4.         $rleftarrow r + 1$
  5. end while
  6. return $mathbf{x}_{r}$ and $r$

where $nabla f(mathbf{x}_r)$ is the gradient of the cost function, $gamma$ is the learning-rate parameter of the algorithm, and $nu$ is the precision parameter. For the function above, let the initial guess be $hat{beta}_0=4$ and $gamma=.001$ with $nu=.00001$. Then $nabla f(hat{beta}_0)=112$, so that [hat{beta}_1=hat{beta}_0-.001(112)=3.888.] And $|hat{beta}_1 – hat{beta}_0| = 0.112> nu$. Repeat the process until at some $r$, $|hat{beta}_{r}-hat{beta}_{r+1}| ngtr nu$. It will turn out that 350 iterations are needed to satisfy the desired inequality, the plot of which is in the following figure with estimated minimum $hat{beta}_{350}=2.250483approxfrac{9}{4}$.

R Script with PlotPython ScriptObviously the convergence is slow, and we can adjust this by tuning the learning-rate parameter, for example if we try to increase it into $gamma=.01$ (change gamma to .01 in the codes above) the algorithm will converge at 42nd iteration. To support that claim, see the steps of its gradient in the plot below.

If we try to change the starting value from 4 to .1 (change beta_new to .1) with $gamma=.01$, the algorithm converges at 173rd iteration with estimate $hat{beta}_{173}=2.249962approxfrac{9}{4}$ (see the plot below).

Now let’s consider another function known as Rosenbrock defined as begin{equation} f(mathbf{w})triangleq(1 – w_1) ^ 2 + 100 (w_2 – w_1^2)^2. end{equation} The gradient is begin{align} nabla f(mathbf{w})&=[-2(1 – w_1) – 400(w_2 – w_1^2) w_1]mathbf{i}+200(w_2-w_1^2)mathbf{j}nonumber\ &=left[begin{array}{c} -2(1 – w_1) – 400(w_2 – w_1^2) w_1\ 200(w_2-w_1^2) end{array}right]. end{align} Let the initial guess be $hat{mathbf{w}}_0=left[begin{array}{c}-1.8\-.8end{array}right]$, $gamma=.0002$, and $nu=.00001$. Then $nabla f(hat{mathbf{w}}_0)=left[begin{array}{c} -2914.4\-808.0end{array}right]$. So that begin{equation}nonumber hat{mathbf{w}}_1=hat{mathbf{w}}_0-gammanabla f(hat{mathbf{w}}_0)=left[begin{array}{c} -1.21712 \-0.63840end{array}right]. end{equation} And $lVerthat{mathbf{w}}_0-hat{mathbf{w}}_1rVert=0.6048666>nu$. Repeat the process until at some $r$, $lVerthat{mathbf{w}}_r-hat{mathbf{w}}_{r+1}rVertngtr nu$. It will turn out that 23,374 iterations are needed for the desired inequality with estimate $hat{mathbf{w}}_{23375}=left[begin{array}{c} 0.9464841 \0.8956111end{array}right]$, the contour plot is depicted in the figure below.
R Script with Contour PlotPython ScriptNotice that I did not use ggplot for the contour plot, this is because the plot needs to be updated 23,374 times just to accommodate for the arrows for the trajectory of the gradient vectors, and ggplot is just slow. Finally, we can also visualize the gradient points in the surface as shown in the following figure.
R ScriptIn my future blog post, I hope to apply this algorithm on statistical models like linear/nonlinear regression models for simple illustration.

To leave a comment for the author, please follow the link and comment on their blog: Analysis with Programming.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)