I found the solution to calculate partial gradients of the parameters
unsing FiniteDifferencesDifferentiator and DerivativeStructure:
https://github.com/meteoinfo/MeteoInfo/blob/master/meteoinfo-math/src/main/java/org/meteoinfo/math/optimize/MyParametricUnivariateFunction.java.
A test case was also added:
https://github.com/meteoinfo/MeteoInfo/blob/master/meteoinfo-math/src/test/java/org/meteoinfo/math/optimize/FiniteDifferencesDifferentiatorTest.java
.

Hope this will be helpful on this issue.

Regards
Yaqiang

On Tue, Sep 20, 2022 at 3:44 PM Yaqiang Wang <yaqiang.w...@gmail.com> wrote:

> Unfortunately, I can not figure out how to calculate partial gradients
> using DerivativeStructure, so I used a simple way to calculate the
> gradients of the parameters (
> https://github.com/meteoinfo/MeteoInfo/blob/9586b1a850721e418370a8a3d90af1e8fea03674/meteoinfo-math/src/main/java/org/meteoinfo/math/optimize/MyParametricUnivariateFunction.java).
> The result looks reasonable:
> http://www.meteothink.org/downloads/temp/curve_fit.png.
>
> Regards
> Yaqiang
>
> On Tue, Aug 2, 2022 at 7:34 PM Gilles Sadowski <gillese...@gmail.com>
> wrote:
>
>> Hello.
>>
>> Le mar. 2 août 2022 à 10:42, Yaqiang Wang <yaqiang.w...@gmail.com> a
>> écrit :
>> >
>> > Gilles,
>> >
>> > What I gather from the documentation is that the intended purpose is
>> > > to track the values of some function and all its derivatives when the
>> > > function is defined programmatically (using the usual arithmetical
>> > > operators, and generalizations of the functions defined in the "Math"
>> > > JDK class).  IIUC, one gains automatic access to the derivatives
>> > > without defining them analytically (only the function need be
>> defined).
>> > >
>> >
>> > Yes, the function is defined programmatically.
>>
>> Perhaps have a look at
>>
>> https://commons.apache.org/proper/commons-math/commons-math4-legacy/apidocs/org/apache/commons/math4/legacy/analysis/differentiation/GradientFunction.html
>> The associated unit test could also help figure out how to use
>> "DerivativeStructure":
>>
>> https://commons.apache.org/proper/commons-math/commons-math4-legacy/xref-test/org/apache/commons/math4/legacy/analysis/differentiation/GradientFunctionTest.html
>>
>> >
>> > How is "function" defined here?
>> > >
>> >
>> > "function" is a ParameUnivariateFunction instance which implements
>> > UnivariateFunction interface with parameters (
>> >
>> https://github.com/meteoinfo/MeteoInfo/blob/master/meteoinfo-math/src/main/java/org/meteoinfo/math/optimize/ParamUnivariateFunction.java
>> ).
>> > Its is used to wrap the Jython function (
>> >
>> https://github.com/meteoinfo/MeteoInfo/blob/master/meteoinfo-lab/pylib/mipylib/numeric/optimize/minpack.py#L78-L135
>> > ).
>> >
>> > The test Jython script and result figure from current curve_fit function
>> > can be found here:
>> http://www.meteothink.org/downloads/temp/curve_fit-1.png
>> > , http://www.meteothink.org/downloads/temp/curve_fit-2.png,
>> > http://www.meteothink.org/downloads/temp/curve_fit-3.png.
>> >
>> > Your code of gradient method (as below) was also tested with good
>> result (
>> > http://www.meteothink.org/downloads/temp/curve_fit-4.png). But of
>> course it
>> > can only work for that specific function.
>>
>> Sure, but I still can't figure out where the problem is:  Do you want to
>> implement the fitting using numerical derivatives because the gradient
>> cannot be expressed simply, or just to save one input (the equivalent
>> of the code below)?
>> In the latter case, I believe that "DerivativeStructure" could do it.  But
>> so should it, with a package that performs symbolic differentiation (?).
>>
>> >     public double[] gradient(double x, double ... parameters) {
>> > >         final double a = parameters[0];
>> > >         final double b = parameters[1];
>> > >         final double c = parameters[2];
>> > >         final double[] grad = new double[3];
>> > >         grad[0] = Math.exp(-b * x);
>> > >         grad[1] = -a * x * grad[0];
>> > >         grad[2] = 1;
>> > >         return grad;
>> > >     }
>> > >
>>
>> Regards,
>> Gilles
>>
>> P.S. If you succeed in framing the solution to your problem with
>> "DrivativeStructure",
>>        we are interested in documenting it in "SimpleCurveFitter".
>>
>> >>> [...]
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
>> For additional commands, e-mail: user-h...@commons.apache.org
>>
>>
>
> --
> *************************************************
> Dr. Yaqiang Wang
> Chinese Academy of Meteorological Sciences (CAMS)
> 46, Zhong-Guan-Cun South Avenue
> Beijing, 100081
> China
>
> yaqiang.w...@gmail.com
>
> www.meteothink.org
> **************************************************
>


-- 
*************************************************
Dr. Yaqiang Wang
Chinese Academy of Meteorological Sciences (CAMS)
46, Zhong-Guan-Cun South Avenue
Beijing, 100081
China

yaqiang.w...@gmail.com

www.meteothink.org
**************************************************

Reply via email to