See also this drawing tool for visualizing dual curves DualSnakes.
Given a function f from real numbers to real numbers, its Legendre-Fenchel transformation is defined by the formula \[f^*(g) = \max_x g.x - f(x) \; .\] This transformation is a ubiquitous concept throughout Science. It appears in thermodynamics and classical mechanics as the Legendre transform (a special case), in convex optimization and machine learning as the Fenchel conjugate or the convex conjugate. At first sight this definition seems arbitrary, but it admits geometrical interpretations along with many properties that make it a useful tool. Among others, it offers a generalization of Lagrangian duality.
Unfortunately understanding these properties typically require newcomers to assimilate numerous formulas. So far I have found no widely accessible visualization to help one gain an intuitive knowledge of this transformation. I tried to fill this gap with an interactive visualization that runs in the browser. I programmed it with a combination of the open source Python library Bokeh and custom JavaScript callbacks.
This code takes a real function \(f\) defined on a certain range \([x_0,x_1]\), and defined to be plus infinity \(+\infty\) outside this range. (I know it sounds exotic but it is a convention in convex analysis.) The code displays 6 functions defined over three dimensions: the \(X, G \) and \(Y\) spaces. Note that \(G\) stands for ‘gradient’ because of the higher dimensional generalization.
Hovering over the dual plot shows how \(f^*\) is built via \( f^*(g) = \max_x g.x - f(x) = \max_x g.x - f^{**}(x)\), highlighting a primal point \( (\hat x(g), f(\hat x(g))) \) which achieves this max. A similar behavior is encoded in the primal plot, building \(f^{**}) \) from \(f^*\). Hovering over \( (X,G)\rightarrow Y \) plots also reveal the building process along with the duality gap.
I hope you will be able to figure out the meaning of all plot elements by playing with them. Source code at https://github.com/remilepriol/dualityviz.