In this post we discuss the fundamental role second order derivatives play in describing the curvature of functions. In particular we describe how second order derivatives describe the convexity or concavity of a function locally and globally.
# imports from custom library
import sys
sys.path.append('../../')
import matplotlib.pyplot as plt
from mlrefined_libraries import basics_library as baslib
from mlrefined_libraries import calculus_library as callib
import autograd.numpy as np
# this is needed to compensate for %matplotlib notebook's tendancy to blow up images when plotted inline
%matplotlib notebook
from matplotlib import rcParams
rcParams['figure.autolayout'] = True
%load_ext autoreload
%autoreload 2
In our previous posts we have seen how the second order Taylor Series is a quadratic function built from first and second order derivatives closely matching a given function at a point. It is purposefully designed to be the very best quadratic approximation near the fixed point, sharing the value of the function there, as well as the value of its first and second derivative(s). This of course makes the second order Taylor Series match an underlying function's shape near the point on which it is defined quite closely and - in particular - reflects whether or not a function is convex or concave there.
You can use the slider widget below the cell to get a feel for just what the Taylor series quadratic approximation captures about the underlying function as the point about which it is defined moves across the input range. This is shown in the left panel where the function
\begin{equation} g(w) = \text{sin}(3w) + 0.1w^2 \end{equation}is drawn in black, the point about which the approximation is defined drawn in green, and the second order Taylor series itself is shown in torquoise.
# what function should we play with? Defined in the next line.
g = lambda w: np.sin(3*w) + 0.1*w**2
# create an instance of the visualizer with this function
taylor_viz = callib.second_order_convexity.visualizer(g = g)
# run the visualizer for our chosen input function
taylor_viz.draw_it(num_frames = 200,max_val = 3.5)