-
Notifications
You must be signed in to change notification settings - Fork 343
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
Let grdtrend fit model along xx or yy only. #5496
Conversation
Hi @joa-quim. I agree that finding just x or y trends is very useful. However, I wish you would have first proposed this feature so we could discuss how to do this rather than implementing it, making it harder to suggest changes. I spent a lot of time a few years ago rewriting how trend1d works to allow a wide mix of Fourier and polynomial coefficients, and the plan was to expand this to trend2d and grdtrend. If we add +x and +y then that sets in place things that is hard to undo later (backwards compatibility). Have you tested the new functionality and are there any test scripts so we know it works, etc etc.? |
Yep, Running this script shows it works but I don't know how to turn it into a test that says if 0; 0 then it passed
|
I've decided to accept this PR but will first work on that test script. |
Wait a bit. I just tried the above script and it fails. it may print 0 0 but the grids are screwed, no? The info says they are both all zeros. |
Oops, sorry, I see you are using -D to get the residuals. Anyway, I am making some changes so that you dont have to set -N10 just go get 4 terms in y, and improve the docs. I removed the special treatment and just solve for the exact solution with the parameters that are needed only. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested and documented. Good to go, @joa-quim.
Since I made many changes I think we may need an independent review on this, @meghanrjones. |
@PaulWessel, is this rounding error within expectations for GMT?
|
I would expect these two commands to produce the same trend, since the linear x term should be included in both equations. But, -N3+x results in a trend grid with a constant value: Example 1:
Example 2:
|
I misinterpret the documentation for how n_model works when +x|+y is used. Still surprised by the output from the second example. |
The cutoff is different for +x or +y since we are doing a linear fit only.
I forgot to update the decision on whether we have a trivial solution or not. The check is different now for +x +y. Hopefully this fix is OK. |
Much better. Are the rounding errors acceptable (#5496 (comment))?
|
Yes. gmt math -Q 0.1 0.09999999 SUB = so for 4-byte floats that is as good as it gets. |
Sorry, but I am skeptical about the least-squares results. For this example with a synthetic grid Z = 0.5 + X + X^2 + X^3, the magnitude of the residuals seems unreasonably large. Am I missing something?
|
That looks pretty bad. Then I go to master and do the same but fit -N10 which ought to give a good fit and it is just as bad. So something is not working as we expect here. Could you perhaps modify this script to show a crossection at y = 0.5 instead? |
Here you go:
|
Hm, that is pretty revealing. It is the same that master gives (using -N10 instead of -N4+x). |
Yes, I think this is not specific to this branch. So this could be merged and the bug fixed as a separate issue. |
Because this PR (a) adds new capability (that is working) and (b) retains the same problem as documented above as in master, we will merge this PR in and then work on addressing the wider issue in a new PR. |
Sometimes it may be desirable to fit a model that varies only along one direction. An example will be to remove a N-S gradient only. This PR add +x or +y as optional modifiers to -N. However I had some difficulties because the
gauss
function didn't like to have a set of coords all equal to zero. The solution was to let the lastxvar
oryvar
= 1 and write a function to remove the vertical slice shown in attached image.Maybe something more clever can be done but meanwhile this now works well.