2

I am currently trying to split the last part of the below equation into three parts, but am struggling to do so.

\begin{align*}
    \notag \mathbb{E}_{(x,y) \sim P(X,Y)}\left[(y-f(x))^2 + (f(x)-\hat{f}(x))^2\right] &= \mathbb{E}_{(x,y) \sim P(X,Y)}\left[(y-f(x))^2 + (f(x)-\hat{f}(x))^2\right]\\
    \notag &= \sigma^{2}(x) + \mathbb{E}_{x \sim P(X)}\left[\left(f(x)-\hat{f}(x)\right)^2\right]\\
    \notag &=\sigma^{2}(x)+ \mathbb{E}_{x \sim P(X)}\left[\left(f(x)-\mathbb{E}_{x}\left[\hat{f}(x)\right]\right)^{2}+2\left(\left(f(x)-\mathbb{E}_{x}\left[\hat{f}(x)\right]\right)\left(\mathbb{E}_{x}\left[\hat{f}(x)\right]-\hat{f}(x)\right)\right)+\left(\mathbb{E}_{x}\left[\hat{f}(x)\right] - \hat{f}(x)\right)^2\vphantom{\mathbb{E}_{x \sim P(X)}}\right].
\end{align*}

I've tried

\begin{align*}
    \notag \mathbb{E}_{(x,y) \sim P(X,Y)}\left[(y-f(x))^2 + (f(x)-\hat{f}(x))^2\right] &= \mathbb{E}_{(x,y) \sim P(X,Y)}\left[(y-f(x))^2 + (f(x)-\hat{f}(x))^2\right]\\
    \notag &= \sigma^{2}(x) + \mathbb{E}_{x \sim P(X)}\left[\left(f(x)-\hat{f}(x)\right)^2\right]\\
\begin{split}
    \notag &=\sigma^{2}(x)+ \mathbb{E}_{x \sim P(X)}\left[\left(f(x)-\mathbb{E}_{x}\left[\hat{f}(x)\right]\right)^{2}\right.\\
\left.+2\left(\left(f(x)-\mathbb{E}_{x}\left[\hat{f}(x)\right]\right)\left(\mathbb{E}_{x}\left[\hat{f}(x)\right]-\hat{f}(x)\right)\right)\right.\\
\left.+\left(\mathbb{E}_{x}\left[\hat{f}(x)\right] - \hat{f}(x)\right)^2\vphantom{\mathbb{E}_{x \sim P(X)}}\right].
\end{split}
\end{align*}

which seems to work but doesn't align the bottom part of the equation correctly. The problem also seems to be that I have \left and \right within my equation that I would like to split. Basically, I'd like to split the third line of the equation and align them within the \mathbb{E} sign.

Thanks in advance.

5
  • Why are you using \left...\right here in the first place. I see no reason for any of them in this example.
    – daleif
    Mar 24, 2021 at 12:45
  • \left...\right is there for the expected value notation since I have several equations wrapped within it.
    – AW27
    Mar 24, 2021 at 12:46
  • Don't, in most cases you cannot see any difference in size, and in other cases they become overly large. Scale manually if needed, and only use \left...\right in the cases that really need it.
    – daleif
    Mar 24, 2021 at 12:55
  • Okay, however there is a significant size difference when I don't use it.
    – AW27
    Mar 24, 2021 at 12:56
  • 1
    Not in the code you give here. For examply why should a hat on a vaiable need the fences to be scaled? That is just unnecessary
    – daleif
    Mar 24, 2021 at 12:57

3 Answers 3

3

A variation on daleif's theme. I'd prefer to space out the main parts, so they'll be more easily distinguishable between one another.

The alignment points are also different, taking into account the similarities among the expressions.

I also added a version where the first equality is on the same line, so you can see it won't work. The overfull, at 10pt font size is 60pt, about 2.5 centimeters.

\documentclass[a4paper]{article}
\usepackage{mathtools,amssymb}

\newcommand{\EE}{\mathbb{E}}

\begin{document}

The following display would fit on any textwidth, possibly even in two-column format.
Some more words to see what the text width is.
\begin{align*}
\lefteqn{\EE_{(x,y) \sim P(X,Y)}[(y-f(x))^2 + (f(x)-\hat{f}(x))^2]} % no space taken
\qquad&
  \\[1ex]
  &= \EE_{(x,y) \sim P(X,Y)}[(y-f(x))^2 + (f(x)-\hat{f}(x))^2]
  \\[1ex]
  &= \sigma^{2}(x) + \EE_{x \sim P(X)}[(f(x)-\hat{f}(x))^2]
  \\[1ex]
  &= \begin{aligned}[t]
     \sigma^{2}(x) &+ \EE_{x \sim  P(X)} \bigl[ (f(x)-\EE_{x}[\hat{f}(x)])^{2}
     \\
     &+ 2\bigl((f(x)-\EE_{x}[\hat{f}(x)])(\EE_{x}[\hat{f}(x)]-\hat{f}(x))\bigr)
     \\
     &+ (\EE_{x}[\hat{f}(x)] - \hat{f}(x))^2%\vphantom{\EE_{x \sim P(X)}}
    \bigr].
  \end{aligned}
\end{align*}

The following display instead would not fit on any reasonable text width.
Some more words to see what the text width is.
\begin{align*}
\EE_{(x,y) \sim P(X,Y)}[(y-f(x))^2 + (f(x)-\hat{f}(x))^2]
  &= \EE_{(x,y) \sim P(X,Y)}[(y-f(x))^2 + (f(x)-\hat{f}(x))^2]
  \\
  &= \sigma^{2}(x) + \EE_{x \sim P(X)}[(f(x)-\hat{f}(x))^2]
  \\
  &= \begin{aligned}[t]
     \sigma^{2}(x) &+ \EE_{x \sim  P(X)} \bigl[ (f(x)-\EE_{x}[\hat{f}(x)])^{2}
     \\
     &+ 2\bigl((f(x)-\EE_{x}[\hat{f}(x)])(\EE_{x}[\hat{f}(x)]-\hat{f}(x))\bigr)
     \\
     &+ (\EE_{x}[\hat{f}(x)] - \hat{f}(x))^2\vphantom{\EE_{x \sim P(X)}}
    \bigr].
  \end{aligned}
\end{align*}

\end{document}

enter image description here

3

I'd probably do something like this. Note how much less cluttered the code is without all those \left...\right. I'd probably also make a macro for the E operator that automatically adds [] to the argument. But that is for another day.

\documentclass[a4paper]{article}
\usepackage{mathtools,amssymb}

\begin{document}

\begin{align*}
  \MoveEqLeft[3] \mathbb{E}_{(x,y) \sim P(X,Y)}
  [(y-f(x))^2 + (f(x)-\hat{f}(x))^2]
  \\
  = {} & \mathbb{E}_{(x,y) \sim P(X,Y)}[(y-f(x))^2 + (f(x)-\hat{f}(x))^2]
  \\
  = {} & \sigma^{2}(x) + \mathbb{E}_{x \sim P(X)}[(f(x)-\hat{f}(x))^2]
  \\
  = {} &\sigma^{2}(x)
  \begin{aligned}[t]
    {}+{} & \mathbb{E}_{x \sim  P(X)}
    \bigl[
    (f(x)-\mathbb{E}_{x}[\hat{f}(x)])^{2}
    \\
    &+2\bigl((f(x)-\mathbb{E}_{x}[\hat{f}(x)])
    (\mathbb{E}_{x}[\hat{f}(x)]-\hat{f}(x))
    \bigr)
    \\
    &+(\mathbb{E}_{x}[\hat{f}(x)]
    - \hat{f}(x))^2\vphantom{\mathbb{E}_{x \sim P(X)}}
    \bigr].
  \end{aligned}
\end{align*}

\end{document}

enter image description here

2
  • Thanks, is it possible to align the top two equations first?
    – AW27
    Mar 24, 2021 at 13:24
  • @AW27 just remove the \MoveEqLeft[3] and the first \\, note that this then wastes a lot of space below the first half of the first line, which is why I write it like this.
    – daleif
    Mar 24, 2021 at 13:49
2

Here's another solution. It gets rid of all \left and \right autosizing directives, gets rid of the \notag and \vphantom directives, uses curly braces instead of square brackets to enclose the term that spans three rows, gets rid of four pairs of square brackets, gets rid of an unnecessary pair of round parentheses in the penultimate row, and uses \bigl and \bigr only where necessary.

enter image description here

\documentclass{article}
\usepackage{amsmath,amssymb}
\DeclareMathOperator{\E}{\mathbb{E}} % expectations operator
\newcommand\hf{\hat{f}} % handy shortcut macro
\begin{document}
\begin{align*}
&\E_{(x,y) \sim P(X,Y)}\bigl[(y-f(x))^2 + (f(x)-\hf(x))^2\,\bigr] \\
&\quad=\E_{(x,y) \sim P(X,Y)}\bigl[(y-f(x))^2 + (f(x)-\hf(x))^2\,\bigr]\\
&\quad=\sigma^{2}(x) + \E_{x \sim P(X)}[(f(x)-\hf(x))^2\,]\\
&\quad=\sigma^{2}(x)+ \E_{x \sim P(X)}\bigl\{
   \bigl(f(x)-\E_x\hf(x) \bigr)^2 \\
&\qquad\quad+2(f(x)-\E_x\hf(x))(\E_x\hf(x)-\hf(x))\\
&\qquad\quad+(\E_x\hf(x) - \hf(x))^2 \bigr\}\,.
\end{align*}
\end{document}
3
  • 1
    Just a note for the OP, wen I use the \MoveEqLeft in my solution, we don't have to add these \quad's manually. Use whatever fits you best.
    – daleif
    Mar 24, 2021 at 13:51
  • 1
    Good one on the use of braces. Too many have a tendency to use ))))), this is typography, not lisp ;-)
    – daleif
    Mar 24, 2021 at 13:52
  • 1
    @daleif - I absolutely love "this is typography, not lisp".
    – Mico
    Mar 24, 2021 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.