math icon indicating copy to clipboard operation
math copied to clipboard

pow applied to mixed variables

Open jeanchristopheruel opened this issue 2 years ago • 4 comments

~~AD of pow<fvar<var>>(0, 0) causes NAN adjoint. pow<fvar<var>>(0, 0) and pow<var>(0, 0) both behave as expected.~~

Update pow<var>(0, 0) should return NAN adjoint since the partials are undefined.

Steps to reproduce

stan::math::nested_rev_autodiff nested{};
stan::math::var t{0.0};
auto foo = stan::math::pow(t,0);
foo.grad();
assert(not std::isfinite(t.adj()));  // fails

Current Version:

v4.7.0

jeanchristopheruel avatar Dec 29 '23 01:12 jeanchristopheruel

I think this is correct, as the second derivative of pow(x, y) w.r.t. x:

$$ \frac{d^2}{dx^2}(x^y) = x^{(-2 + y)} \cdot (-1 + y) \cdot y $$

Is not defined when both x & y are 0

andrjohns avatar Dec 29 '23 05:12 andrjohns

Ah I see, we handle this in the rev header by skipping the partials update. Thanks for catching this!

andrjohns avatar Dec 29 '23 05:12 andrjohns

Yes exacly! It is handled for rev here. You are also right, the partials are undefined.

jeanchristopheruel avatar Dec 29 '23 05:12 jeanchristopheruel

Actually, I think pow<var> is the issue. Check out my suggestion!

jeanchristopheruel avatar Dec 29 '23 07:12 jeanchristopheruel

Covered as part of #3031

andrjohns avatar Mar 21 '24 19:03 andrjohns