Separate optimization from statistic#1914
Conversation
|
Cool! I can have a look in a few days, just one comment about this:
Is it possible to have these inputs to the fit statistic more flexible? One of the fit statistics I'd like to implement in It doesn't take "weights" as input, like chi^2 statistics do, but instead a few other quantities. Here's an example where @registerrier implemented It would be nice if the If this is already the case, apologies. Thanks for doing this! |
|
The only hard requirement is that statistic is callable. This can probably be improved but I am putting it out for discussion. It'd be great to have more examples, looking forward to the |
|
The current Travis failure is an example in whatsnew/0.3.rst, failing due to a name change. The proposed name change, if accepted, is going to be in 0.4 ( Is there a way to skip a test based on the version of astropy? |
|
@nden I don't know how to get rid of the travis-ci test failure you mentioned in the what's new example. Maybe @astrofrog knows or you can ask on the |
|
@cdeil I just pushed an update which I hope will make it easier to add a wstat statistic. Can you confirm this? I need to work some momre on this but wanted to give you an idea. |
|
I'll try this out now ... maybe @astrofrog and / or @embray, you can have a look, too? |
|
@nden Can you please add an entry to the CHANGES document? |
|
It's not quite ready for review. I just want to make sure it's flexible enough to add other statistic functions and optimizers. I don't know if there's much sense in working on new features until #2049 is resolved since everything in modeling depends on how parameters are handled but @embray is working on it. |
|
The fit statistics functions ( I have re-implemented all build-in fit statistics from Sherpa in gammapy.stats.fit_statistics, see e.g. gammapy.stats.fit_statistics.cash for an example. Some points for discussion about the fit statistics functions:
|
|
Just one more comment ... I suggest we rename |
|
All good questions. |
|
I am in favor of |
There was a problem hiding this comment.
I'm not really clear on what chi_line does.
I presume it's one method to take errors on x into account when fitting a 1-dim model to (x, y) data points.
Some reference I'm aware of:
http://adsabs.harvard.edu/abs/2010arXiv1008.4686H
http://root.cern.ch/root/html/TGraph.html#TGraph:Chisquare
Please add a reference and a test case with a few points for this fit statistic.
Also I would suggest to rename it ... to me "line" sounds like straight line, whereas I think this can handle non-linear functions f(x)?
There was a problem hiding this comment.
It's a specific fitter which fits a straight line with uncertainties in x and y. I'm not sure how to generalize this (except may be in the case of a plane, hence z_sigma in the code).
I'll check the references you mentioned.
|
@cdeil After mulling over this for a while, I decided it's best to limit this PR to only separating statistics from optimizers and add additional statistics in separate PRs. |
|
This got stuck for some reason. There are so many changes in upstream/master that even rebasing or manually merging upstream locally does not produce diffs that are easy to review. So I prepared a new branch based on the current trunk. I am wondering if I replace the current branch with the new branch with "git branch -M ..." would Travis pick up the new branch? Or is it better to make a separate PR? |
There was a problem hiding this comment.
This is not an astropy doc rule, but I would suggest to use ~ only for astropy-internal references
and to leave it off for astropy-external references like the links to the numpy and scipy docs here.
IMO this makes it simpler to know what's what when reading the docs and means less surprises when clicking links.
There was a problem hiding this comment.
In this case, the text does say Numpy's lstsq function, so maybe it's ok?
There was a problem hiding this comment.
Sure, it's OK! This is a very minor point and probably just my personal preference when reading the astropy html docs. If @nden prefers how it's written now she's very welcome to ignore this comment.
There was a problem hiding this comment.
We should not use import * in examples.
|
Ah, I see. The goal was to make it possible to write a statistic without having to modify the objective function, defined in the fitter. Adding This is easy to add. It may be good also to add a function ( |
|
@taldcroft Another option is to pass the statistic to |
|
@nden @taldcroft @cdeil - just to check, what is the current status of this? I think we had planned to re-apply the changes rather than trying to rebase? If so, would it make sense to do this this week so we can merge it (or have it ready for review) before the feature freeze? |
|
I would like to keep all of these open as reference--all of them address issues that should be tracked regardless of whether any attached implementations are used as-is. |
|
Actually, were there still any major lingering issues with this? I know there were some minor niggles but they could be addressed later. In the interest of getting some of this functionality sooner rather than later I can rebase this to merge into the current master if people thing that would be better. |
|
I've completely lost track with the many open |
|
Something that needs to be added here is a simple way for a user to change the statistic function (See Tom's comments above). I did some work in this direction but it wasn't completed. |
|
Okay, I've rebased this on master in a local branch of my own and ensured all the tests are passing on all supported Python versions. I think I'd rather go ahead and merge this as-is, and then build any additional enhancements on top of what's in there. The only thing this is lacking is a changelog entry, and perhaps also warrants a mention in the 0.4 what's new page. |
|
Unless you don't want to advertise it too broadly if the feature is considered "incomplete". But there are still API changes that deserve mention. |
|
I think we can work on the 'what's new' after feature freeze, so I'd just add the CHANGES.rst entry for now, and we can worry about the 'what's new' after. |
|
Okay...I just worry about things slipping through the cracks with that. I wasn't really following if people were writing that or not as is. |
|
Okay thanks, sounds good. Will have to go through and find out what else needs to be in there. Need to ping other devs too. |
|
Will ping some devs now on the gitter channel (since a few are hanging out there) |
|
@nden @astrofrog @embray - what's the story with this? Can it be rebased and merged for 0.4? Or do we need to push it off? |
|
Closed by #2441 |
This PR separates optimization from statistic and allows creating fitters by combining optimizers and statistic functions.
Because of the many changes It's probably best to review this by looking at the entire files (fitting.py, optimizers.py, statistic.py) and not the diffs in the PR.
I am proposing that the name of the fitter is constructed from the names of the optimizer and the statistic, so some existing fitters have been renamed:
SLSQPFitter-->SLSQPLSQFitterNonLinearLSQFitter-->LevMarLSQFitterStatistics are implemented as functions (but can be any callable) and are stored currently in
~astropy.modeling.statistic.py. Currently there are twoleastsquareand (following @cdeil's example in a different PR)cash. The input to the statistic functions isfunc(measured_values, evaluated_values, weights).Optimization methods are stored in
~astropy.modeling.optimizers.py. They are implemented as classes in order to store additional information about the optimization properties but can be any callable.Dealing with model's constraints has been moved to optimizers. Some methods of the old
Fitterclass were made functions.I still need to add tests for the new fitter and possibly write a custom_fitter. Other than that I have pretty much completed what I had in mind and would appreciate comments on overall design, statistic implementation, names and any other improvements.