y0-causal-inference / y0

❓y0 (pronounced "why not?") is for causal inference in Python
https://y0.readthedocs.io
BSD 3-Clause "New" or "Revised" License
44 stars 10 forks source link

LaTeX representations of complex expressions involving summations should sometimes include brackets but do not #208

Open rjc955 opened 9 months ago

rjc955 commented 9 months ago

In some cases, when an expression contains a summation applied over a product of several terms, the LaTeX representation of that expression may omit necessary brackets. To reproduce this issue, run this demo notebook down to the cells at the end. Here is a screenshot:

Summation_Representation_Issue

A visual check of the string representations of result_4 and expected_result shows that they are the same up to a permutation in the order of the terms in the products, and the notebook runs an assert_expr_equal that verifies the same. But the LaTeX representation of result_4 is misleading in that it appears that, in the numerator for example, the summation $\Sigma{W{1}}{\Sigma{W{2},X,Y}{\Sigma{W{3}}}}$ is applied to all three terms in the product, whereas in fact only $\Sigma{W{1}}$ is applied to all three terms, and $\Sigma{W{2},X,Y}{\Sigma{W{3}}}$ is applied only to the first term. To make this fact clear, the LaTeX representation needs to include the red brackets in the numerator in the screenshot. Similarly, there should be brackets around the entire expression following $\Sigma{Y}{\Sigma{W_{1}}}$ in the denominator.

cthoyt commented 9 months ago

That's a good point. Latex output isn't strictly correct in terms of the math and would benefit from grouping symbols in some places to make it more "correct". We haven't done this since the current output more resembles what people write in the literature. Maybe a ruleset could be made for when grouping symbols increase legibility