14 Describing mathematical models: notation
We started this section by exploring how to create DAGs. Such models do not, in their basic form, contain magnitudes or forms of relationships between variables. For example, an exposure could affect an outcome in different ways, including negative or positive linear or nonlinear associations. Additionally, the variables themselves can take on different scales and types, such as binary, count, or continuous variables. Measured variables can also be thought of as descendants of an unobserved data-generating process that, together with a measurement process, creates the observed variable. To go beyond describing causal relationships across variables, we might want to mathematically characterize these relationships. The purpose of this chapter is to describe a system for setting up mathematical notation in quarto documents.
14.1 Mathematical notation
14.1.1 \(\LaTeX\)
\(\LaTeX\) is a typesetting system used for preparing technical documents and, popularly1, typesetting mathematical notation. Quarto accepts \(\LaTeX\) math syntax inline and as a display math. Inline syntax could look like this: $F = m \times a$, which renders into \(F = m \times a\). Display math requires:
$$
F = m \times a
$$Which will place the output on its own line, in the middle of the document:
\[ F = m \times a \]
LaTeX syntax contains all the arithmetic operator symbols (e.g., \(+\quad-\quad/\quad*\quad(\quad)\quad\)). Additionally, symbols for greek letters, arrows, binary operators and set notation are available as commands, such as \alpha \rightarrow \leq \in, producing \(\alpha \rightarrow \leq \in\).
The Greek alphabet is often used for describing parameters in mathematical notation (Table 14.1). However, different conventions exists in different fields and traditions.
| Name | Lowercase | Command | Uppercase | Command |
|---|---|---|---|---|
| Alpha | \(\alpha\) | \alpha |
\(A\) | A |
| Beta | \(\beta\) | \beta |
\(B\) | B |
| Gamma | \(\gamma\) | \gamma |
\(\Gamma\) | \Gamma |
| Delta | \(\delta\) | \delta |
\(\Delta\) | \Delta |
| Epsilon | \(\epsilon\), \(\varepsilon\) | \epsilon, \varepsilon |
\(E\) | E |
| Zeta | \(\zeta\) | \zeta |
\(Z\) | Z |
| Eta | \(\eta\) | \eta |
\(H\) | H |
| Theta | \(\theta\), \(\vartheta\) | \theta, \vartheta |
\(\Theta\) | \Theta |
| Iota | \(\iota\) | \iota |
\(I\) | I |
| Kappa | \(\kappa\) | \kappa |
\(K\) | K |
| Lambda | \(\lambda\) | \lambda |
\(\Lambda\) | \Lambda |
| Mu | \(\mu\) | \mu |
\(M\) | M |
| Nu | \(\nu\) | \nu |
\(N\) | N |
| Xi | \(\xi\) | \xi |
\(\Xi\) | \Xi |
| Omicron | \(o\) | o |
\(O\) | O |
| Pi | \(\pi\) | \pi |
\(\Pi\) | \Pi |
| Rho | \(\rho\) | \rho |
\(P\) | P |
| Sigma | \(\sigma\) | \sigma |
\(\Sigma\) | \Sigma |
| Tau | \(\tau\) | \tau |
\(T\) | T |
| Upsilon | \(\upsilon\) | \upsilon |
\(\Upsilon\) | \Upsilon |
| Phi | \(\phi\), \(\varphi\) | \phi, \varphi |
\(\Phi\) | \Phi |
| Chi | \(\chi\) | \chi |
\(X\) | X |
| Psi | \(\psi\) | \psi |
\(\Psi\) | \Psi |
| Omega | \(\omega\) | \omega |
\(\Omega\) | \Omega |
14.1.1.1 Formatting notation
We can use colors to, e.g., highlight parts of an equation using curly brackets {}:
$$
y = {\color{red}x^2} + x
$$\[ y = {\color{red}x^2} + x \]
When using text to define an operator, e.g., to describe the normal distribution with its parameters, we can use \operatorname{Normal} to get the text-part correctly formatted, i.e., \(\operatorname{Normal}(\mu, \sigma)\), instead of \(Normal(\mu, \sigma)\). In addition of establishing your own operators, specific operator exists for a set of commonly used mathematical operators (Table 14.2).
\operatorname{}.
| Operator | Rendered | Command |
|---|---|---|
| Natural logarithm | \(\ln(x)\) | \ln(x) |
| Logarithm | \(\log(x)\), \(\log_{10}(x)\) | \log(x), \log_{10}(x) |
| Exponential | \(\exp(x)\) | \exp(x) |
| Sine | \(\sin(x)\) | \sin(x) |
| Cosine | \(\cos(x)\) | \cos(x) |
| Tangent | \(\tan(x)\) | \tan(x) |
| Inverse sine | \(\arcsin(x)\) | \arcsin(x) |
| Maximum | \(\max(x)\) | \max(x) |
| Minimum | \(\min(x)\) | \min(x) |
| Argument of max | \(\arg\max_{\theta} f(\theta)\) | \arg\max_{\theta} f(\theta) |
| Limit | \(\lim_{x \to \infty} f(x)\) | \lim_{x \to \infty} f(x) |
| Determinant | \(\det(A)\) | \det(A) |
| Sum | \(\sum_{i=1}^{n} x_i\) | \sum_{i=1}^{n} x_i |
| Product | \(\prod_{i=1}^{n} x_i\) | \prod_{i=1}^{n} x_i |
| Integral | \(\int_{a}^{b} f(x)\,dx\) | \int_{a}^{b} f(x)\,dx |
| Square root | \(\sqrt{x}\), \(\sqrt[3]{x}\) | \sqrt{x}, \sqrt[3]{x} |
| Fraction | \(\frac{a}{b}\) | \frac{a}{b} |
| Custom operator | \(\operatorname{logit}(p)\) | \operatorname{logit}(p) |
Instead of \operatorname, the \text operator can be used to declare what is intended to be text as text. We might want to write “5 cars”, this could be accomplished using 5 \text{cars} (\(5 \text{cars}\)), instead of 5 cars (\(5 cars\)). Although the text-operator does not format the text as expected in the math environment we still loose a space. This can be added inside the text operator (5 \text{ cars}: \(5 \text{ cars}\)). This behavior has consequences when we use text instead of a custom operator. E.g., a \log b will correctly place spaces between operators and variables but a \text{log} b will not (\(a \log b\), vs. \(a \text{log} b\)).
Superscripts and subscripts are common features of mathematical notation indicating e.g., power and indices. For example the ith element of the vector x can be written as \(x_i\) using the syntax x_i. When additional information needs to go into the indices we can add the next level index in brackets, to accomplish this we use curley brackets to indicate what should be included as subscript, e.g., x_{i[j]} becomes \(x_{i[j]}\).
Special symbols exists for describing relationships (Table 14.3)
| Relationship | Rendered | Command |
|---|---|---|
| Equal / not equal | \(=\), \(\neq\) | =, \neq |
| Approximately equal | \(\approx\) | \approx |
| Defined as | \(:=\), \(\equiv\) | :=, \equiv |
| Less / greater than | \(<\), \(>\) | <, > |
| Less / greater than or equal | \(\leq\), \(\geq\) | \leq, \geq |
| Much less / greater than | \(\ll\), \(\gg\) | \ll, \gg |
| Distributed as | \(y \sim \operatorname{Normal}(\mu, \sigma)\) | \sim |
| Proportional to | \(p(\theta \mid y) \propto p(y \mid \theta)\,p(\theta)\) | \propto |
| Conditional on (given) | \(P(y \mid x)\) | \mid |
| Independent of | \(x \perp\!\!\!\perp y\) | \perp\!\!\!\perp |
| Plus-minus | \(\pm\) | \pm |
| Element of / not element of | \(\in\), \(\notin\) | \in, \notin |
| Subset | \(\subset\), \(\subseteq\) | \subset, \subseteq |
| Approaches / maps to | \(\to\), \(\mapsto\) | \to, \mapsto |
| Implies / if and only if | \(\Rightarrow\), \(\Leftrightarrow\) | \Rightarrow, \Leftrightarrow |
| For all / there exists | \(\forall\), \(\exists\) | \forall, \exists |
14.1.2 Describing a model
A statistical model can be written as a formula over multiple lines. For example, to describe a parametric model using the normal distribution to connect parameters to data, we can start by defining this assumption, i.e., \(y \sim \operatorname{Normal}(\mu, \sigma)\). Next we might want to describe how μ relates to a variable, x, i.e. \(\mu = \alpha + \beta x\). This approach separates the distributional assumption from the deterministic model. To write all this in latex we can use the aligned environment2.
The syntax described below uses \\ as line breaks and & to align signs.
$$
\begin{aligned}
y &\sim \operatorname{Normal}(\mu, \sigma) \\
\mu &= \alpha + \beta x
\end{aligned}
$$\[ \begin{aligned} y &\sim \operatorname{Normal}(\mu, \sigma) \\ \mu &= \alpha + \beta x \end{aligned} \] We can read the resulting formula as “y is distributed following a normal distribution with mean μ and standard deviation σ. μ is linearly related to x through an intercept α and a slope β”. This way of expression statistical models is common and transparent
14.1.3 Cross-referencing
A formula can be cross-referenced in a quarto document. Using a specific tag (e.g., #eq-model1), we can use it to reference the formula in text (e.g., @eq-model1, giving us Equation 15.1). The syntax for adding the reference to an equation must use the eq- label3.
$$
\begin{aligned}
y &\sim \operatorname{Normal}(\mu, \sigma) \\
\mu &= \alpha + \beta x
\end{aligned}
$$ {#eq-model1}\[ \begin{aligned} y &\sim \operatorname{Normal}(\mu, \sigma) \\ \mu &= \alpha + \beta x \end{aligned} \tag{14.1}\]
14.2 Read more
See the Wikibook for details on the syntax. See the quarto documentation for implementation in quarto documentations.
See the excellent Wikipedia for historical details and the Wikibook for technical details. See the quarto documentation for LaTeX implementation in Quarto documents.↩︎
See the Quarto documentation↩︎