2

I would like to draw the following contour plots in $\LaTeX$ using the TikZ package.

from Hastie (2015) "Statistical Learning with Sparsity"

My script for drawing the contour plots for ridge (q=2) and lasso regression (q=1) are below. The first block of code is ridge (q=2) which is the 2nd plot in the figure above with a circle. The second block of code for lasso (q=1) rotates a rectangle to get the diamond, shown in the 3rd plot.

\usepackage{tikz}

\begin{tikzpicture}
\draw[-] (-0.75,0) -- (0.75,0) node[anchor=north west] {$\beta_1$};
\draw[-] (0,-0.75) -- (0,0.75) node[anchor=south east] {$\beta_2$};
\draw[thick] (0,0) circle (0.5cm);
\end{tikzpicture}

\begin{tikzpicture}
\draw[-] (-0.75,0) -- (0.75,0) node[anchor=north west] {$\beta_1$};
\draw[-] (0,-0.75) -- (0,0.75) node[anchor=south east] {$\beta_2$};
\draw[thick,cm={cos(45) ,-sin(45) ,sin(45) ,cos(45),(0,0)}] (-0.4,-0.4) rectangle (0.4,0.4);
\end{tikzpicture}

With these two completed, I am wondering how to draw the 4th, 5th and 1st plots in the figure above that have non-convex and convex geometric shapes. Finally, I would like to create the following plots for elastic net (q between 1 and 2) using TikZ:

from Hastie 2009 "Elements of Statistical Learning"

Is there a way to make non-convex (pinched) geometric shapes without resorting to packages outside of TikZ as done in this hyperbola graph? I know there are parabola and arc commands in TikZ, but I think the contour plots require control over how much bending there is towards the corners depending on the value used for q.

2
  • Welcome to TeX-SE! Do you have an actual formula or do you just want to draw cartoons, i.e. some graphs that resemble these?
    – user121799
    Commented Jun 14, 2019 at 15:43
  • it would be alright to just draw them, but they are just the $\ell_q$ norms from maths assuming two regression coefficients, therefore the two axes, x for beta_1, y for beta_2. Plots for q=0.75, q=1.5, q=3 could be nice afterwards
    – develarist
    Commented Jun 14, 2019 at 15:52

1 Answer 1

3

This is something that draws cartoons. It has a pic for that, which accepts a parameter. If the parameter is larger than 0, it is more circle-like, and if it is smaller than 0, more cross-like (concave and convex are not uniquely defined here, I think). In addition you can play with the looseness. (You could also combine the axis and norm penalty pics to one, but I feel that this is more restrictive.)

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/axis/.style={code={
 \draw[-] (-0.75,0) -- (0.75,0) node[anchor=north west] {$\beta_1$};
 \draw[-] (0,-0.75) -- (0,0.75) node[anchor=south east] {$\beta_2$};
 }},pics/norm penalty/.style={code={
 \draw (0,0.5) to[out=-45+#1,in=135-#1] 
 (0.5,0) to[out=-135+#1,in=45-#1] 
 (0,-0.5) to[out=135+#1,in=-45-#1] 
 (-0.5,0) to[out=45+#1,in=-135-#1]  cycle;}}]
\path (0,0) pic{axis} (0,0) pic[blue,thick,looseness=1.5]{norm penalty=45}
(2,0) pic{axis} (2,0) pic[blue,thick,looseness=1]{norm penalty=45}
(4,0) pic{axis} (4,0) pic[blue,thick,looseness=1]{norm penalty=0}
(6,0) pic{axis} (6,0) pic[blue,thick,looseness=1]{norm penalty=-20}
(8,0) pic{axis} (8,0) pic[blue,thick,looseness=2]{norm penalty=-45}
(2,-2) pic{axis} (2,-2) pic[blue,thick,looseness=0.5]{norm penalty=45}
(4,-2) pic{axis} (4,-2) pic[blue,thick,looseness=0.2]{norm penalty=45};
\end{tikzpicture}
\end{document}

enter image description here

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/axis/.style 2 args={code={
 \draw[-] (-0.75,0) -- (0.75,0) node[anchor=north west] {$\beta_1$};
 \draw[-] (0,-0.75) node[below]{#2}-- (0,0.75) node[anchor=south east] {$\beta_2$}
 node[above=0.4cm] {#1};
 }},pics/norm penalty/.style={code={
 \draw (0,0.5) to[out=-45+#1,in=135-#1] 
 (0.5,0) to[out=-135+#1,in=45-#1] 
 (0,-0.5) to[out=135+#1,in=-45-#1] 
 (-0.5,0) to[out=45+#1,in=-135-#1]  cycle;}}]
\path (0,0) pic{axis={$q=1$}{title 1}} (0,0) pic[blue,thick,looseness=1.5]{norm penalty=45}
(2,0) pic{axis={$q=4$}{title 2}} (2,0) pic[blue,thick,looseness=1]{norm penalty=45}
(4,0) pic{axis={$q=2$}{title 3}} (4,0) pic[blue,thick,looseness=1]{norm penalty=0}
(6,0) pic{axis={$q=0.5$}{title 4}} (6,0) pic[blue,thick,looseness=1]{norm penalty=-20}
(8,0) pic{axis={$q=0.1$}{title 5}} (8,0) pic[blue,thick,looseness=2]{norm penalty=-45}
(2,-3) pic{axis={$q=2$}{title 6}} (2,-3) pic[blue,thick,looseness=0.5]{norm penalty=45}
(4,-3) pic{axis={$q=3$}{title 7}} (4,-3) pic[blue,thick,looseness=0.2]{norm penalty=45};
\end{tikzpicture}
\end{document}

enter image description here

3
  • what's a good way to add centered text above (parameter value) and below (name) each contour plot, like in the 2nd figure of the question, while maintaining row sequence as shown?
    – develarist
    Commented Jun 16, 2019 at 4:16
  • @develarist You could add them to the definition of the axis pic. Will the axis labels stay?
    – user121799
    Commented Jun 16, 2019 at 4:19
  • thanks alot. when tuning q=0.1 plot by itself, when very enlarged at length 2 rather than the current 0.5, so that looseness>2 and/or norm penalty<-45, it becomes a very untamed polyhelical shape, beyond hyperbolic, that weaves like a vine around the axes rather than asymptotically approaching them. is there a way to have it hug the axes tighter, because, when enlarged, it's looking like a 0.2 rather than a 0.1, and tuning it to get to the real 0.1 makes it twist on itself
    – develarist
    Commented Jun 27, 2019 at 10:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .