Skip to content

Moffat1D and Moffat2D derivatives #8094

Closed

Description

@ycopin

The derivatives in Moffat1D are wrong:

def fit_deriv(x, amplitude, x_0, gamma, alpha):

They should be replaced by:

    @staticmethod
    def fit_deriv(x, amplitude, x_0, gamma, alpha):
        """One dimensional Moffat model derivative with respect to parameters"""

        fac = (1 + (x - x_0) ** 2 / gamma ** 2)
        d_A = fac ** (-alpha)
        d_x_0 = (2 * amplitude * alpha * (x - x_0) * d_A / (fac * gamma ** 2))
        d_gamma = (2 * amplitude * alpha * (x - x_0) ** 2 * d_A /
                   (fac * gamma ** 3))
        d_alpha = -amplitude * d_A * np.log(fac)
        return [d_A, d_x_0, d_gamma, d_alpha]

In Moffat2D:

def fit_deriv(x, y, amplitude, x_0, y_0, gamma, alpha):

only derivative wrt gamma is wrong. It should be

        d_gamma = 2 * amplitude * alpha * d_A * rr_gg / ((1 + rr_gg) * gamma ** 3)

Activity

  1. pllim commented on Nov 9, 2018

    @pllim
    Member

    These were originally added by @adonath as part of #1251 and has since been renamed a few times. It was Beta... in that PR.

  2. adonath commented on Nov 9, 2018

    @adonath
    Member

    I remember that for the Beta1D and Beta2D models I computed the derivatives and reference values in a notebook using using Sympy (http://nbviewer.jupyter.org/gist/adonath/6088712). So I'm pretty sure it used to be correct at that time. Maybe when the rename to Moffat1D and Moffat2D happened, the parametrization changed as well and the derivatives where not adapted? From a quick look in the code it seems there are no unit tests for the model derivatives.

    @ycopin I didn't check whether your proposed formulae are correct, please check against Sympy or some other algebra library and post the result as a reference. In case you still find the current formulae are incorrect, would you mind preparing a minimal pull request with the correct ones? Ideally one should add unit tests for the model derivatives, but this is a somewhat larger project...

  3. ycopin commented on Nov 9, 2018

    @ycopin
    ContributorAuthor

    I used Wolfram Alpha for the derivatives, but I trust the derivatives in the aforementioned notebook are correct. The issue is that they were not properly implemented in the code (in particular the d_A ** alpha terms, where it should just be d_A).

    For sure, the derivatives are currently incorrect, and the fit does not converge if not using ad-hoc numerical derivative estimates. New derivatives solve the issue (at least in my 1D case).

    I'll try to prepare a PR, but that might take more time...

  4. ycopin commented on Nov 10, 2018

    @ycopin
    ContributorAuthor

    BTW, I wonder why this was not detected before, as it seems there are some derivative tests in modeling.

  5. added a commit that references this issue on Nov 25, 2018
  6. added a commit that references this issue on Nov 28, 2018
  7. added a commit that references this issue on May 3, 2019
  8. added a commit that references this issue on May 24, 2019
  9. chris-simpson commented on Sep 29, 2020

    @chris-simpson
    Contributor

    You got this one wrong. The derivative in Moffat2D was correct and the PR made it wrong. There's only a single power of gamma in the denominator, not 3, because 2 of them have been subsumed into rr_gg. I'll use Wolfram alpha for the calculus so you don't have to check mine...
    Screen Shot 2020-09-29 at 10 59 20 AM
    Screen Shot 2020-09-29 at 11 45 51 AM

    So there's only a single isolated power of gamma remaining in the denominator.

  10. saimn commented on Sep 29, 2020

    @saimn
    Contributor

    Finding the same issue with d_gamma with Sympy:
    image

    Full notebook here: https://gist.github.com/saimn/f79197fe45bb96d90f6ca3fa54910ac2
    The other fixes (implemented in #8108) for Moffat1D are correct.

  11. ycopin commented on Sep 30, 2020

    @ycopin
    ContributorAuthor

    I'm sorry I introduced a bug in Moffat2D derivatives... :-/ As mentioned earlier, verifying the formal derivatives using e.g. Wolfram is not necessarily sufficient, but one should check the actual implementation, thanks @saimn for doing so (even if the tested implementation might not be the most efficient). I still wonder why this implementation error was not caught by a test comparing formal and numerical derivatives...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions