py-why / causal-learn

Causal Discovery in Python. It also includes (conditional) independence tests and score functions.
https://causal-learn.readthedocs.io/en/latest/
MIT License
1.19k stars 196 forks source link

Add Error Handling for cit.py line 165/166 #107

Closed maddiehill closed 1 year ago

maddiehill commented 1 year ago

Hi, I experienced a ValueError: math domain error on this line from log as the value of (1 + r) / (1 - r) was negative. Please see the screenshot below and the value of r from the debugger. I'm not sure why this might be happening, but it would be good to catch potential errors here :)

Screenshot 2023-04-03 at 16 57 04
kunwuz commented 1 year ago

Thanks for reporting. Math errors are a little bit tricky, and sometimes we just don't know why, especially with finite samples :) @chenweiDelight @MarkDana Any thoughts on potential causes? Or shall we first add handling there?

MarkDana commented 1 year ago

Hi @maddiehill Thanks so much for identifying this! I just made a pr https://github.com/py-why/causal-learn/pull/108 to solve this. The numerical issue of |sample partial correlation|>=1 happens when the samplesize is very small, or the relationship is deterministic. So we set the abs to 1. - machine epsilon to prevent log(<=0) errors.

Thanks again!

maddiehill commented 1 year ago

Great @MarkDana @kunwuz thanks for fixing this so fast!