Hello.

Le lun. 1 août 2022 à 16:03, Yaqiang Wang <yaqiang.w...@gmail.com> a écrit :
>
> Gilles,
>
> Thanks so much for your patiently response! I know I can write a gradient
> method for a specific function, but my purpose is to make the gradient
> method suitable for any function of yi = f(xi, p1, p2, p3, ...). That means
> the users don't need to override a new fixed gradient method for a new
> function, just like the SciPy's curve_fit (
> https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html).
> So I tried to calculate gradient array through numerical differentiation (
> https://github.com/meteoinfo/MeteoInfo/blob/master/meteoinfo-math/src/main/java/org/meteoinfo/math/optimize/MyParametricUnivariateFunction.java#L33-L56,
> the code also is attache below), and please let me know whether the code is
> correct for my purpose? Thanks!

I'm still confused about your use case (maybe because I did not see
the figures of what you expect vs what you got).

I've never used the "DerivativeStructure" (and I've just noticed that the
link to the reference document is not accessible anymore).

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).

>
> @Override
> public double[] gradient(double v, double... parameters) {
>     function.setParameters(parameters);

How is "function" defined here?

>
>     // create a differentiator
>     FiniteDifferencesDifferentiator differentiator =
>             new FiniteDifferencesDifferentiator(nbPoints, stepSize);

If you assume that "function" is defined analytically, you don't need to
use "FiniteDifferentiator" (moreover, its use is not recomended IIUC
the documentation).
However, if the derivatives cannot be expressed analytically, it seems
that "DerivativeStructure" is an overly complex utility if in the end, it's
just replacing the finite differences formulae[1] which you can write in
about the same number lines as your code below.

Regards,
Gilles

[1] https://en.wikipedia.org/wiki/Numerical_differentiation

>
>     // create a new function that computes both the value and the derivatives
>     // using DerivativeStructure
>     UnivariateDifferentiableFunction diffFunc =
> differentiator.differentiate(function);
>
>     double y = function.value(v);
>     int n = parameters.length;
>     double[] gradients = new double[n];
>     for (int i = 0; i < n; i++) {
>         DerivativeStructure xDS = new DerivativeStructure(n, 1, i,
> parameters[i]);
>         DerivativeStructure yDS = diffFunc.value(xDS);
>         int[] idx = new int[n];
>         idx[i] = 1;
>         gradients[i] = yDS.getPartialDerivative(idx);
>     }
>
>     return gradients;
> }
>
>
> By the way, I am using Apache commons math 3.6.1 at present. Today I also
> tried the 4.0-SNAPSHOT version but the result is the same.
>
> Regards
> Yaqiang
>
>>> [...]

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
For additional commands, e-mail: user-h...@commons.apache.org

Reply via email to