Created
June 29, 2024 23:49
-
-
Save ProfAvery/6dc7e68f2cabeb9c7a197f853621e0a5 to your computer and use it in GitHub Desktop.
LaTeX Equation Cheat Sheet.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyPnrjfcz1vyQmH7XYwHhwDJ", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/ProfAvery/6dc7e68f2cabeb9c7a197f853621e0a5/latex-equation-cheat-sheet.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "nP-nhvpQjpkL" | |
}, | |
"source": [ | |
"# $ \\LaTeX $ equation cheat sheet\n", | |
"\n", | |
"This cheat sheet focuses specifically on the notation needed for the equations found in *A First Course in Machine Learning*, Second Edition\n", | |
"\n", | |
"**Note**: to read this document, double-click the text cell so that the screen is split between the Markdown source code and the rendered equations and text.\n", | |
"\n", | |
"\n", | |
"## Chapter 1 - Linear Modelling: A Least Squares Approach\n", | |
"\n", | |
"Equations are written with surrounded by dollar signs: $ y = mx + b $\n", | |
"\n", | |
"If you want regular $ signs, you may need to escape them with two backslashes: \\\\$.\n", | |
"\n", | |
"Equations surrounded by two dollar signs are centered on the next line:\n", | |
"$$ y = f(x) $$\n", | |
"\n", | |
"Some mathematical operators are preceded with a single backslash `\\`: $ y = \\sin(x) $\n", | |
"\n", | |
"To get an actual backslash, escape it with another slash: \\\\.\n", | |
"\n", | |
"Some operators require arguments in curly braces `{}`: $ \\sqrt{y} = mx + c $\n", | |
"\n", | |
"Superscripts use a caret `^`: $ y = mx^2 + c $\n", | |
"\n", | |
"Subscripts are written with underscores `_`:\n", | |
"$$ t = f(x; w_0, w_1) = w_0 + w_1x $$\n", | |
"\n", | |
"Loss functions are defined with \"mathematical calligraphy\":\n", | |
"$$ \\mathcal{L}_n(t_n, f(x_n; w_0, w_1)) = (t_n - f(x_n; w_0, w_1))^2 $$\n", | |
"\n", | |
"The average loss requires fractions: $$ \\frac{1}{N} $$\n", | |
"and summation: $$ \\sum_{n=1}^{N} $$\n", | |
"*Note how `\\sum` separates its arguments with an underscore `_` for the lower argument and a caret `^` for the upper argument instead of putting them directly together the way `\\frac` does.*\n", | |
"\n", | |
"Putting it all together:\n", | |
"$$ \\mathcal{L} = \\frac{1}{N} \\sum_{n=1}^{N} \\mathcal{L}_n(t_n, f(x_n; w_0, w_1)) $$\n", | |
"\n", | |
"Sometimes you need to define your own operators:\n", | |
"$$\n", | |
"\\DeclareMathOperator*{\\argmin}{argmin}\n", | |
"\\argmin_{w_0, w_1} \\frac{1}{N} \\sum_{n=1}^{N} \\mathcal{L}_n(t_n, f(x_n; w_0, w_1))\n", | |
"$$\n", | |
"\n", | |
"Absolute loss uses left and right vertical bars:\n", | |
"$$ \\mathcal{L}_n = \\lvert t_n - f(x_n; w_0, w_1) \\rvert $$\n", | |
"\n", | |
"Use `\\dots` for mathematical ellipses: $ n = 1, \\dots, N $\n", | |
"\n", | |
"The script d for partial derivatives is called `\\partial`, and usually appears in a fraction:\n", | |
"$$ \\frac{\\partial \\mathcal{L}}{\\partial w_0} $$\n", | |
"\n", | |
"If you use regular parentheses `()` they'll be regular-sized. If you want larger ones, you need to prefix them with the `\\left` and `\\right` commands:\n", | |
"$$ \\left ( \\sum_{n=1}^{N} x_n^2 \\right ) $$\n", | |
"\n", | |
"Use `\\hat` and `\\bar` for single letters: $ \\hat{w} $, $ \\bar{x} $\n", | |
"\n", | |
"But use `\\widehat` and `\\overline` for more complicated expressions:\n", | |
"$$ \\widehat{w_1} = \\frac{\\overline{xt} - \\bar{x}\\bar{t}}{\\overline{x^2} - (\\bar{x})^2} $$\n", | |
"\n", | |
"Use `\\times` or a centered dot `\\cdot` as explicit multiplication operators:\n", | |
"$$ 2 \\times 3 = 2 \\cdot 3 $$\n", | |
"\n", | |
"Use `\\mathbf` for math symbols in bold font like vector $ \\mathbf{x} $ and matrix $ \\mathbf{X} $. You can also use `\\vec` for vectors, but it uses an arrow: $ \\vec{x} $.\n", | |
"\n", | |
"Begin matrices with `\\begin{bmatrix}` and end them with `\\end{bmatrix}`. Use `&` between items in a row and `\\\\` between rows. (The `b` is for square **b**rackets).\n", | |
"\n", | |
"You can squish them together on a single line:\n", | |
"$$ \\mathbf{X}= \\begin{bmatrix} 1 & 4 \\\\ 3 & 6 \\\\ -2 & 11 \\end{bmatrix} $$\n", | |
"\n", | |
"But they'll be easier to read with if you use separate lines and indent:\n", | |
"$$ \\mathbf{A} = \\begin{bmatrix}\n", | |
" a_{11} & a_{12} \\\\\n", | |
" a_{21} & a_{22} \\\\\n", | |
" a_{31} & a_{32}\n", | |
"\\end{bmatrix} $$\n", | |
"\n", | |
"*Note that a column vector is just an $ n \\times 1 $ matrix:*\n", | |
"$$ \\mathbf{y} = \\begin{bmatrix} y_1 \\\\ y_2 \\\\ y_3 \\\\ y_4 \\end {bmatrix} $$\n", | |
"\n", | |
"Use `\\vdots` for vertical ellipses in column vectors:\n", | |
"$$ \\mathbf{t} = \\begin{bmatrix} t_1 \\\\ t_2 \\\\ \\vdots \\\\ t_n \\end {bmatrix} $$\n", | |
"\n", | |
"And `\\ddots` for diagonal dots in matrices:\n", | |
"$$ \\mathbf{A} = \\begin{bmatrix}\n", | |
" a_{11} & a_{12} & \\dots & a_{1M} \\\\\n", | |
" a_{21} & a_{22} & \\dots & a_{2M} \\\\\n", | |
" \\vdots & \\vdots & \\ddots & \\vdots \\\\\n", | |
" a_{N1} & a_{N2} & \\dots & a_{NM}\n", | |
"\\end{bmatrix} $$\n", | |
"\n", | |
"You can use a capital T for transpose $ \\mathbf{x}^T $, but it looks like the textbook uses '\\top` $ \\mathbf{x}^\\top $.\n", | |
"\n", | |
"The textbook uses the word \"gradient\" interchangeably with \"slope\", but you can use `\\nabla` to get the gradient operator:\n", | |
"$$ \\frac{\\partial \\mathcal{L}}{\\partial \\mathbf{w}} =\n", | |
"\\nabla_{\\mathbf{w}} \\mathcal{L} =\n", | |
"\\begin{bmatrix}\n", | |
" \\frac{\\partial \\mathcal{L}}{\\partial w_0} \\\\\n", | |
" \\frac{\\partial \\mathcal{L}}{\\partial w_1}\n", | |
"\\end{bmatrix}\n", | |
"$$\n", | |
"\n", | |
"Finally, use `\\lambda` for the lowercase Greek letter:\n", | |
"$$ \\mathcal{L}' = \\mathcal{L} + \\lambda \\mathbf{w}^\\top \\mathbf{w} $$\n", | |
"\n", | |
"\n", | |
"## Chapter 2 - Linear Modelling: A Maximum Likelihood Approach\n", | |
"\n", | |
"The ordinary `<` and `>` signs work for strict inequality, but use `\\leq` and `\\geq` for to allow equality:\n", | |
"$$ 0 \\leq P(Y = y) \\leq 1 $$\n", | |
"\n", | |
"Similarly, `\\neq`:\n", | |
"$$ P(Y \\neq 4) + P(Y = 4) = 1 $$\n", | |
"\n", | |
"You can use either `|` or `\\mid` for conditional probabilities, but they produce different spacing:\n", | |
"$$ P(Y = y | X = x) $$\n", | |
"$$ P(Y = y \\mid X = x) $$\n", | |
"The textbook tends to use `|`.\n", | |
"\n", | |
"`\\prod` works the same way as `\\sum`:\n", | |
"$$ P(y_1, y_2, \\dots, y_J) = P(y_1) \\times P(y_2) \\times \\dots \\times P(y_J) = \\prod_{j=1}^J{P(y_j)} \\$$\n", | |
"\n", | |
"Curly braces need to be escaped `\\{` and `\\}` since they are also used for arguments:\n", | |
"$$ \\mathbf{E}_{P(x)}\\{f(X)\\} = \\sum_{x}f(x)P(x) $$\n", | |
"\n", | |
"As with parentheses, you can prefix `\\{` and `\\}` with `\\left` and `\\right` to make them stretch to fit their contents. Some functions in the textbook use a sans-serif font, specified with `\\mathsf`:\n", | |
"$$\n", | |
"\\mathsf{var}\\{X\\} = \\mathbf{E}_{P(x)}\\left \\{(X - \\mathbf{E}_{P(x)}\\{X\\})^2\\right \\} $$\n", | |
"$$\n", | |
"\\mathsf{Tr}(\\mathbf{A}) = \\sum_{d=1}^{D}A_{dd}\n", | |
"$$\n", | |
"\n", | |
"Use `{ N \\choose y }` to specify a combination:\n", | |
"$$ P(Y = y) = P(y) = {N \\choose y} q^y(1-q)^{N-y} $$\n", | |
"\n", | |
"Integrals use `\\int`:\n", | |
"$$ P(x_1 \\leq X \\leq x_2) = \\int_{x_1}^{x_2}p(x) dx$$\n", | |
"\n", | |
"Specify that values are approximately equal with `\\approx`:\n", | |
"$$ \\mathbf{E}_{p(x)}\\{f(x)\\} \\approx \\frac{1}{S} \\sum_{s=1}^{S}f(x_s) $$\n", | |
"\n", | |
"Functions defined piecewise work like matrices, only with `cases` rather than `bmatrix`:\n", | |
"$$\n", | |
"p(y) =\n", | |
"\\begin{cases}\n", | |
" r & \\text{for $a \\leq y \\leq b$} \\\\\n", | |
" 0 & \\text{otherwise.}\n", | |
"\\end{cases}\n", | |
"$$\n", | |
"If you want the right-hand side to include non-italic text, use `\\text`. Inside `\\text`, you can switch back to math mode with `$`.\n", | |
"\n", | |
"As with `\\lambda`, other Greek letters are specified with their names (e.g. `\\alpha`, `\\beta`). Upper-case Greek letters have their named capitalized (e.g., `\\Gamma, \\Sigma`):\n", | |
"$$\n", | |
"p(r) = \\frac{\\Gamma(\\alpha + \\beta)}{\\Gamma(\\alpha)\\Gamma(\\beta)}r^{\\alpha - 1}(1 - r)^{\\beta - 1}\n", | |
"$$\n", | |
"\n", | |
"Use `\\enspace` to add a space between an equation and its conditions, `\\sim` for \"is distributed as,\" and don't forget the `\\mathcal` font:\n", | |
"$$\n", | |
"t_n = w_0 + w_1x_n + \\epsilon_n,\\enspace \\epsilon_n \\sim \\mathcal{N}(0, \\sigma^2)\n", | |
"$$\n", | |
"\n", | |
"$$ \\mathsf{Tr}{f} $$\n", | |
"\n", | |
"\n", | |
"## For more information\n", | |
"Check out these links for $ \\LaTeX $:\n", | |
" * [LaTex in Jupyter notebook](https://nbviewer.jupyter.org/github/dingran/latex-ipynb/blob/master/latex-cheatsheet.ipynb)\n", | |
" * [LaTeX/Mathematics](https://en.wikibooks.org/wiki/LaTeX/Mathematics) on [Wikibooks](https://en.wikibooks.org/wiki/LaTeX).\n", | |
"\n", | |
"The Google Docs Equation Editor is mostly a GUI, but it does support a subset of $ \\LaTeX $ notation:\n", | |
"* [Use equations in a document](https://support.google.com/docs/answer/160749)\n", | |
"* [Google Docs Equation Editor Shortcuts](https://equation-shortcuts.notuom.com/)" | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment