Hello. Le mar. 11 août 2020 à 12:08, Christoph Läubrich <lae...@googlemail.com.invalid> a écrit : > > Thanks for your patience, maybe a better/simpler example would be [1], I > want to find the best fit using LSB
"LSB" ? > under the constraint that the height > of the curve never is above 0.7 (even though without constrains the > optimizer would find a better solution around 8.5). It occurs to me that this would be more properly defined as a least-square problem by assigning an error bar (weight) to each data point. > So my first idea way to copy/extend GausianCurveFitter to accept some > kind of "maxHeight(...)", adjust the function to return INF for > h>maxHeight, I was just unsure that return INF is allowed (per > definition) as it is not mentioned anywhere in the userdocs. And if I > want to limit others (like max position) it would work the same way. Hopefully "infinity" will not cause an issue; you could try and check. [Then if it does, just use any suitably large value.] > In my final problem I need to limit the height, width and position of > the bell-curve to fall into certain bounds, but there is no direct > relation (e.g. width must be three times the height). Then, width is totally correlated (to height) and there are only 2 parameters to fit (height and mean); hence it's probably better (and more efficient) to use that fact instead of defining the correlation as a constraint i.e. define (untested): ---CUT--- public class MyFunc implements ParametricUnivariateFunction { public double value(double x, double ... param) { final double diff = x - param[1]; // param[1] is the mean. final double norm = param[0]; // param[0] is the height. final double s = 3 * norm; // "constraint". final double i2s2 = 1 / (2 * s * s); return Gaussian.value(diff, norm, i2s2); } // And similarly for the "gradient": // https://gitbox.apache.org/repos/asf?p=commons-math.git;a=blob;f=src/main/java/org/apache/commons/math4/analysis/function/Gaussian.java;h=08dcac0d4d37179bc85a7071c84a6cb289c09f02;hb=HEAD#l143 } ---CUT--- to be passed to "SimpleCurveFitter". Regards, Gilles > > > > [1] > https://crlbucophysics101.files.wordpress.com/2015/02/gaussian.png?w=538&h=294 > > > > > Am 11.08.20 um 11:18 schrieb Gilles Sadowski: > > Hi. > > > > 2020-08-11 8:51 UTC+02:00, Christoph Läubrich > > <lae...@googlemail.com.invalid>: > >> Hi Gilles, > >> > >> > >> Just to make clear I don't suspect any error with GausianCurveFitter, I > >> just don't understand how the advice in the user-doc to restrict > >> parameter (for general problems) could be applied to a concrete problem > >> and thus chosen GausianCurvefitter as an example as it uses > >> LeastSquaresBuilder. > >> > >> I also noticed that Gaussian Fitter has a restriction on parameters > >> (norm can't be negative) that is handled in a third way (returning > >> Double.POSITIVE_INFINITY instead of Parameter Validator) not mentioned > >> in the userdoc at all, so I wonder if this is a general purpose solution > >> for restricting parameters (seems the simplest approach). > > > > I'd indeed suggest to first try the same trick as in "GaussianCurveFitter" > > (i.e. return a "high" value for arguments outside a known range). > > That way, you only have to define a suitable "ParametricUnivariateFunction" > > and pass it to "SimpleCurveFitter". > > > > One case for the "ParameterValidator" is when some of the model > > parameters might be correlated to others. > > But using it makes it necessary that you handle yourself all the > > arguments to be passed to the "LeastSquaresProblem". > > > >> To take the gausian example for my use case, consider an observed signal > >> similar to [1], given I know (from other source as the plain data) for > >> example that the result must be found in the range of 2...3 and I wanted > >> to restrict valid solutions to this area. The same might apply to the > >> norm: I know it must be between a given range and I want to restrict the > >> optimizer here even though there might be a solution outside of the > >> range that (compared of the R^2) fits better, e.g. a gausian fit well > >> inside the -1..1. > >> > >> I hope it is a little bit clearer. > > > > I'm not sure. The picture shows a function that is not a Gaussian. > > Do you mean that you want to fit only *part* of the data with a > > function that would not fit well *all* the data? > > > > Regards, > > Gilles > > > >> > >> > >> [1] > >> https://ascelibrary.org/cms/asset/6ca2b016-1a4f-4eed-80da-71219777cac1/1.jpg > >> > >> Am 11.08.20 um 00:42 schrieb Gilles Sadowski: > >>> Hello. > >>> > >>> Le lun. 10 août 2020 à 17:09, Christoph Läubrich > >>> <lae...@googlemail.com.invalid> a écrit : > >>>> > >>>> The userguide [1] mentions that it is currently not directly possible to > >>>> contrain parameters directly but suggest one can use the > >>>> ParameterValidator, is there any example code for both mentioned > >>>> alternatives? > >>>> > >>>> For example GaussianCurveFitter uses LeastSquaresBuilder and I wan't to > >>>> archive that the mean is within a closed bound e.g from 5 to 6 where my > >>>> datapoints ranges from 0..90, how would this be archived? > >>> > >>> Could you set up a unit test as a practical example of what > >>> you need to achieve? > >>> > >>>> I'm especially interested because the FUNCTION inside > >>>> GaussianCurveFitter seems to reject invalid values (e.g. negative > >>>> valuenorm) by simply return Double.POSITIVE_INFINITY instead of using > >>>> either approach described in the user-docs. > >>> > >>> What I don't quite get is why you need to force the mean within a > >>> certain range; if the data match a Gaussian with a mean within that > >>> range, I would assume that the fitter will find the correct value... > >>> Sorry if I missed something. Hopefully the example will clarify. > >>> > >>> Best, > >>> Gilles > >>> > >>>> > >>>> > >>>> [1] > >>>> https://commons.apache.org/proper/commons-math/userguide/leastsquares.html --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscr...@commons.apache.org For additional commands, e-mail: user-h...@commons.apache.org