Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive logprob of < and > operations #6662

Merged
merged 10 commits into from
Apr 19, 2023

Conversation

shreyas3156
Copy link
Contributor

@shreyas3156 shreyas3156 commented Apr 9, 2023

This PR implements the logprob inference for binary comparison Ops > and <.

It creates a MeasurableComparison variable and evaluates the logprob based on the truth value of the condition.

Addresses #6633.

Checklist

New features

  • Logprob inference for GT and LT Ops.

📚 Documentation preview 📚: https://pymc--6662.org.readthedocs.build/en/6662/

@codecov
Copy link

codecov bot commented Apr 9, 2023

Codecov Report

Merging #6662 (19ab8ad) into main (5d68bf3) will increase coverage by 0.01%.
The diff coverage is 98.24%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6662      +/-   ##
==========================================
+ Coverage   91.97%   91.99%   +0.01%     
==========================================
  Files          94       95       +1     
  Lines       15942    16000      +58     
==========================================
+ Hits        14663    14719      +56     
- Misses       1279     1281       +2     
Impacted Files Coverage Δ
pymc/logprob/binary.py 98.00% <98.00%> (ø)
pymc/logprob/__init__.py 100.00% <100.00%> (ø)
pymc/logprob/transforms.py 95.67% <100.00%> (-0.21%) ⬇️
pymc/logprob/utils.py 100.00% <100.00%> (ø)

... and 3 files with indirect coverage changes

Copy link
Member

@ricardoV94 ricardoV94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome!

I left some comments below, but I think it's nearly there.

pymc/logprob/binary.py Show resolved Hide resolved
tests/logprob/test_binary.py Show resolved Hide resolved
tests/logprob/test_binary.py Outdated Show resolved Hide resolved
tests/logprob/test_binary.py Outdated Show resolved Hide resolved
tests/logprob/test_binary.py Outdated Show resolved Hide resolved
tests/logprob/test_binary.py Outdated Show resolved Hide resolved
tests/logprob/test_binary.py Outdated Show resolved Hide resolved
pymc/logprob/binary.py Outdated Show resolved Hide resolved
@ricardoV94 ricardoV94 changed the title Logprob for binary comparison operations Derive logprob of binary comparison operations Apr 9, 2023
@ricardoV94 ricardoV94 changed the title Derive logprob of binary comparison operations Derive logprob of less and greater than comparisons Apr 9, 2023
@shreyas3156 shreyas3156 force-pushed the logprob-binary-operations-6633 branch from 04998ec to 6b40e55 Compare April 10, 2023 14:04
@shreyas3156
Copy link
Contributor Author

@ricardoV94 I have made the suggested changes and I just wanted to confirm the approach before adding a test for the separate function logprob/utils.py. (As mentioned in a comment above)

Copy link
Member

@ricardoV94 ricardoV94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Wanna tackle >= and <= next?

@shreyas3156
Copy link
Contributor Author

Looks good! Wanna tackle >= and <= next?

Definitely! Although I was wondering if >= and <= can be canonicalized to LT and GT respectively?

@ricardoV94
Copy link
Member

Looks good! Wanna tackle >= and <= next?

Definitely! Although I was wondering if >= and <= can be canonicalized to LT and GT respectively?

Yes if you allow either direction in const<dist and const>dist. In this PR only dist<const and dist>const works no?

@shreyas3156
Copy link
Contributor Author

Yes if you allow either direction in const<dist and const>dist. In this PR only dist<const and dist>const works no?

We can make this PR process expressions like const<dist by making the node rewriter check which input holds the base_rv. We can then assign the outputs to the rewritten node in a fixed order for all the comparison operations. (e.g. (base_rv, const))

But this may require adding a transform for <= and >= to GT and LT.

I think it may be better to simply add separate derivations of LE and GE like in this PR.

@ricardoV94
Copy link
Member

ricardoV94 commented Apr 18, 2023

Yes if you allow either direction in const<dist and const>dist. In this PR only dist<const and dist>const works no?

We can make this PR process expressions like const<dist by making the node rewriter check which input holds the base_rv. We can then assign the outputs to the rewritten node in a fixed order for all the comparison operations. (e.g. (base_rv, const))

But this may require adding a transform for <= and >= to GT and LT.

I think it may be better to simply add separate derivations of LE and GE like in this PR.

You can see that for other MeasurableElemwise like Add we store a measurable_input_idx, so that we can have the right logp later on, regardless of the order in which the dist and consts came in. So maybe something like that would be simpler?

measurable_input_idx: int

@shreyas3156
Copy link
Contributor Author

You can see that for other MeasurableElemwise like Add we store a measurable_input_idx, so that we can have the right logp later on, regardless of the order in which the dist and consts came in. So maybe something like that would be simpler?

Sure, I'll look into this approach.

Meanwhile, I had a question about the expression used in this PR to calculate logprob for discrete distributions. I think I should correct it to the following (please correct me if I've misunderstood)

  1. For <: CDF - PMF (x = const) when True; 1 - CDF + PMF (x = const) when False
  2. For >: 1-CDF when True; CDF when False

@ricardoV94
Copy link
Member

You can see that for other MeasurableElemwise like Add we store a measurable_input_idx, so that we can have the right logp later on, regardless of the order in which the dist and consts came in. So maybe something like that would be simpler?

Sure, I'll look into this approach.

Meanwhile, I had a question about the expression used in this PR to calculate logprob for discrete distributions. I think I should correct it to the following (please correct me if I've misunderstood)

1. For `<`: `CDF - PMF (x = const) when True; 1 - CDF + PMF (x = const) when False`

2. For `>`: `1-CDF when True; CDF when False`

For discrete variables the CDF gives us the probability <=, so yes. You can do
logp(dist < const, True) = logcdf(dist, const - 1).

Copy link
Member

@ricardoV94 ricardoV94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flagging it so we don't merge with the wrong logp by accident

@shreyas3156 shreyas3156 requested a review from ricardoV94 April 18, 2023 20:56
pymc/logprob/binary.py Outdated Show resolved Hide resolved
@shreyas3156 shreyas3156 force-pushed the logprob-binary-operations-6633 branch from a5c48c2 to 19ab8ad Compare April 19, 2023 08:07
Copy link
Member

@ricardoV94 ricardoV94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great

@ricardoV94 ricardoV94 merged commit 9b712bf into pymc-devs:main Apr 19, 2023
@shreyas3156
Copy link
Contributor Author

You can see that for other MeasurableElemwise like Add we store a measurable_input_idx, so that we can have the right logp later on, regardless of the order in which the dist and consts came in. So maybe something like that would be simpler?

I went through transforms.py and could not understand why using the index is required. We already extract the measurable_input from the list of inputs first.

measurable_inputs = [
inp
for idx, inp in enumerate(node.inputs)
if inp.owner
and isinstance(inp.owner.op, MeasurableVariable)
and inp not in rv_map_feature.rv_values
]

We then make a MeasurableTransform node from transform_inputs which always has measurable_input as its first element, which is also why the measurable_input_idx always seems to have the value 0.

measurable_input_idx = 0
transform_inputs: Tuple[TensorVariable, ...] = (measurable_input,)

@ricardoV94
Copy link
Member

You're right, so maybe we don't need it at all? Just make it so the first input is always the measurable one?

@ricardoV94 ricardoV94 changed the title Derive logprob of less and greater than comparisons Derive logprob of < and > than comparisons Apr 26, 2023
@ricardoV94 ricardoV94 changed the title Derive logprob of < and > than comparisons Derive logprob of < and > comparisons Apr 26, 2023
@ricardoV94 ricardoV94 changed the title Derive logprob of < and > comparisons Derive logprob of < and > operations Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants