Generalvertical bar for conditional expectation

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
evencoil
Posts: 9
Joined: Sun Dec 02, 2007 11:28 pm

vertical bar for conditional expectation

Post by evencoil »

I'm trying to get something like

E \left( Y \vert X \right)

...the complication is that frequently Y (sometimes X) is a tall expression, like a fraction, and I would like \vert to comform to the size of the taller of Y or X.

I was surprised that I couldn't find anything on the web for this since many people must have run into it. Any suggestions? Or pre-existing commands in amslatex that I'm not aware of? Thanks!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

vertical bar for conditional expectation

Post by gmedina »

With \vert you can use the vertical extensions generated by \left and \right, as the following example suggests:

Code: Select all

E\left(X \left\vert \dfrac{X}{Z} \right. \right)
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

vertical bar for conditional expectation

Post by localghost »

I got an idea, but I think this can only serve as a workaround. Like every other delimiter symbol, the \vert can also be scaled automatically. The trick is that you need a pseudo delimiter. This could be done by

Code: Select all

\left\vert\right.
but the result is that there is no difference in the height of the \vert. As you mentioned, you want to have fractures in your expression. So the height of the \vert has to be modified to fit the height of the fractures. This is achieved by

Code: Select all

\left\vert\vphantom{\frac{1}{1}}\right.
This gives you phantom expression with determines the height of the \vert but does not appear in the text. If you need the whole expression several times in your document, it would be useful to declare a new command with at least two arguments in the preamble and name it as you like.

Code: Select all

\newcommand{\xcmd}[2]{\left(#1\,\left\vert\vphantom{#1#2}\right. #2\right)}
Pay attention to the additional thin space right after the first argument. It is necessary to balance the thin space caused by the right phantom delimiter ("."). Omit that and you will see what I mean.

Test the new command by inserting a normal expression and a fracture or only two normal expressions as arguments. I'm sure there is better method to realise this but for the present it should the job.


Best regards
Thorsten
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

vertical bar for conditional expectation

Post by gmedina »

Mi first solution works only if the fraction goes after the vertical bar; in the general case, the \middle command gives the vertical extension without defining new commands:

Code: Select all

E\left(X \middle\vert \dfrac{X}{Z} \right)
E\left( \dfrac{X}{Z} \middle\vert Y  \right)
1,1,2,3,5,8,13,21,34,55,89,144,233,...
balf
Posts: 158
Joined: Sat Jan 12, 2008 1:11 am

Re: vertical bar for conditional expectation

Post by balf »

Here's a solution, with e-tex's primitive \middle:
$E\left(X\middle\vertY\right)$
That construct is implemented in the braket package, which defines a\Set command. You might do the same to implement an \expectation command, to avoid typing the \left, \right and \middle primitives (or ask the author of braket, Donald Arseneau, to do so and add it to braket).

Regards,
B.A.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

vertical bar for conditional expectation

Post by localghost »

Very good. Didn't know this \middle command. Thanks for that. Are there any other references to find this TeX primitive described?
evencoil
Posts: 9
Joined: Sun Dec 02, 2007 11:28 pm

Re: vertical bar for conditional expectation

Post by evencoil »

\middle command..excellent, works great. Thanks to all for the replies!
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

vertical bar for conditional expectation

Post by Juanjo »

Fine control on the size of delimiters can be obtained through the following command groups which replace the \left - \middle - \right group:
  1. \bigl - \bigm - \bigr
  2. \Bigl - \Bigm - \Bigr
  3. \biggl - \biggm - \biggr
  4. \Biggl - \Biggm - \Biggr
These groups provide delimiters of increasing sizes.

There are many cases in which the above commands should replace the \left - \right ones, mainly when the latter yield too big delimiters and when there are several delimiters together. Just a simple example: I find better this

Code: Select all

\[ \lambda \bigl( (x_1,x_2) + (y_1,y_2)\bigr) = \lambda (x_1,x_2) + \lambda (y_1,y_2) \]
than any of the following alternatives

Code: Select all

\[ \lambda ( (x_1,x_2) + (y_1,y_2) ) = \lambda (x_1,x_2) + \lambda (y_1,y_2) \]

\[ \lambda \left( (x_1,x_2) + (y_1,y_2)\right) = \lambda (x_1,x_2) + \lambda (y_1,y_2) \]
There is also a subtle difference between the \XXXm and \middle commands. To start with, compare

Code: Select all

\[ E ( X \vert Y ) \]
with

Code: Select all

\[ E ( X \mid Y ) \]
Clearly, the spacing around the vertical bar is different. That also happens if \middle is replaced by \bigm and like. Once again, compare

Code: Select all

\[ E\left( X \middle\vert \dfrac{X}{Z} \right) \quad
   E\left( \dfrac{X}{Z} \middle\vert Y  \right) \]
with

Code: Select all

\[ E\biggl( X \biggm\vert \dfrac{X}{Z} \biggr) \quad
   E\biggl( \dfrac{X}{Z} \biggm\vert Y  \biggr) \]
I prefer this last option.

To be complete, there also exist the commands \big, \Big, \bigg and \Bigg, which can be used before any delimiter. Look at this:

Code: Select all

\[ E\bigg(X \bigg\vert \dfrac{X}{Z} \bigg) \quad
   E \bigg( \dfrac{X}{Z} \bigg\vert Y  \bigg) \]
Once more, spacing is different. Observe in this example, however, than \bigg\vert behaves as \middle\vert.

To help typesetting, I propose the following command:

Code: Select all

\newcommand{\expectation}[3][0]{%
  \ifcase#1 
     E( #2 \mid #3 ) 
     \or E \bigl( #2 \bigm\vert #3 \bigr)
     \or E \Bigl( #2 \Bigm\vert #3 \Bigr) 
     \or E \biggl( #2 \biggm\vert #3 \biggr)
     \or E \Biggl( #2 \Biggm\vert #3 \Biggr) 
  \else 
     E \left( #2  \;\middle\vert\; #3 \right)
  \fi}
It has an optional argument, whose value is 0 by default, and two mandatory arguments. The optional argument is a non-negative integer that controls the size of delimiters: 0 means minimal size, 4 is maximal size, 5 or greater means automatic choice of size.
Here it is in action:

Code: Select all

\[ \expectation{X}{Y}\quad  \expectation[1]{X}{Y^2} \quad \expectation[2]{X}{\dfrac{Y}{Z}}
   \quad \expectation[3]{X}{\dfrac{Y}{Z}}\quad \expectation[5]{X}{\dfrac{Y}{Z}} \]
Of course, if you don't like the spacing which surrounds the vertical bar, you can replace \bigm and like by \big and like.
Post Reply